clang 24.0.0git
BPF.cpp
Go to the documentation of this file.
1//===- BPF.cpp ------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ABIInfoImpl.h"
10#include "TargetInfo.h"
11
12using namespace clang;
13using namespace clang::CodeGen;
14
15//===----------------------------------------------------------------------===//
16// BPF ABI Implementation
17//===----------------------------------------------------------------------===//
18
19namespace {
20
21class BPFABIInfo : public DefaultABIInfo {
22public:
23 BPFABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
24
25 // Classify an aggregate (struct/union) used as an argument or a return
26 // value. Aggregates that fit in 1 or 2 registers are passed/returned
27 // directly, coerced to an integer or a pair of 64-bit integers; larger
28 // ones use an indirect reference.
29 ABIArgInfo classifyAggregateType(QualType Ty) const {
30 uint64_t Bits = getContext().getTypeSize(Ty);
31 if (Bits == 0)
32 return ABIArgInfo::getIgnore();
33
34 // Larger aggregates use an indirect reference.
35 if (Bits > 128)
36 return getNaturalAlignIndirect(Ty, getDataLayout().getAllocaAddrSpace());
37
38 // If the aggregate needs 1 or 2 registers, do not use reference.
39 llvm::Type *CoerceTy;
40 if (Bits <= 64) {
41 CoerceTy = llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
42 } else {
43 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), 64);
44 CoerceTy = llvm::ArrayType::get(RegTy, 2);
45 }
46 return ABIArgInfo::getDirect(CoerceTy);
47 }
48
49 ABIArgInfo classifyArgumentType(QualType Ty) const {
51
53 return classifyAggregateType(Ty);
54
55 if (const auto *ED = Ty->getAsEnumDecl())
56 Ty = ED->getIntegerType();
57
58 ASTContext &Context = getContext();
59 if (const auto *EIT = Ty->getAs<BitIntType>())
60 if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
61 return getNaturalAlignIndirect(Ty,
62 getDataLayout().getAllocaAddrSpace());
63
64 return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
66 }
67
68 ABIArgInfo classifyReturnType(QualType RetTy) const {
69 if (RetTy->isVoidType())
70 return ABIArgInfo::getIgnore();
71
72 if (isAggregateTypeForABI(RetTy))
73 return classifyAggregateType(RetTy);
74
75 // Treat an enum type as its underlying type.
76 if (const auto *ED = RetTy->getAsEnumDecl())
77 RetTy = ED->getIntegerType();
78
79 ASTContext &Context = getContext();
80 if (const auto *EIT = RetTy->getAs<BitIntType>())
81 if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
82 return getNaturalAlignIndirect(RetTy,
83 getDataLayout().getAllocaAddrSpace());
84
85 // Caller will do necessary sign/zero extension.
86 return ABIArgInfo::getDirect();
87 }
88
89 void computeInfo(CGFunctionInfo &FI) const override {
91 for (auto &I : FI.arguments())
92 I.info = classifyArgumentType(I.type);
93 }
94
95};
96
97class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
98public:
99 BPFTargetCodeGenInfo(CodeGenTypes &CGT)
100 : TargetCodeGenInfo(std::make_unique<BPFABIInfo>(CGT)) {}
101};
102
103}
104
105std::unique_ptr<TargetCodeGenInfo>
107 return std::make_unique<BPFTargetCodeGenInfo>(CGM.getTypes());
108}
CanQualType Int128Ty
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
static ABIArgInfo getIgnore()
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
static ABIArgInfo getExtend(QualType Ty, llvm::Type *T=nullptr, llvm::Type *Padding=nullptr)
CanQualType getReturnType() const
MutableArrayRef< ArgInfo > arguments()
This class organizes the cross-function state that is used while generating LLVM code.
DefaultABIInfo - The default implementation for ABI specific details.
Definition ABIInfoImpl.h:21
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition TargetInfo.h:80
bool isVoidType() const
Definition TypeBase.h:9111
EnumDecl * getAsEnumDecl() const
Retrieves the EnumDecl this type refers to.
Definition Type.h:53
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9338
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
std::unique_ptr< TargetCodeGenInfo > createBPFTargetCodeGenInfo(CodeGenModule &CGM)
Definition BPF.cpp:106
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
bool isAggregateTypeForABI(QualType T)
QualType useFirstFieldIfTransparentUnion(QualType Ty)
Pass transparent unions as if they were the type of the first element.
Top level wrappers for InstallAPI frontend operations.
unsigned long uint64_t