clang 18.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
27
28 if (isAggregateTypeForABI(Ty)) {
29 uint64_t Bits = getContext().getTypeSize(Ty);
30 if (Bits == 0)
31 return ABIArgInfo::getIgnore();
32
33 // If the aggregate needs 1 or 2 registers, do not use reference.
34 if (Bits <= 128) {
35 llvm::Type *CoerceTy;
36 if (Bits <= 64) {
37 CoerceTy =
38 llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
39 } else {
40 llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), 64);
41 CoerceTy = llvm::ArrayType::get(RegTy, 2);
42 }
43 return ABIArgInfo::getDirect(CoerceTy);
44 } else {
45 return getNaturalAlignIndirect(Ty);
46 }
47 }
48
49 if (const EnumType *EnumTy = Ty->getAs<EnumType>())
50 Ty = EnumTy->getDecl()->getIntegerType();
51
52 ASTContext &Context = getContext();
53 if (const auto *EIT = Ty->getAs<BitIntType>())
54 if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
55 return getNaturalAlignIndirect(Ty);
56
59 }
60
62 if (RetTy->isVoidType())
63 return ABIArgInfo::getIgnore();
64
65 if (isAggregateTypeForABI(RetTy))
66 return getNaturalAlignIndirect(RetTy);
67
68 // Treat an enum type as its underlying type.
69 if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
70 RetTy = EnumTy->getDecl()->getIntegerType();
71
72 ASTContext &Context = getContext();
73 if (const auto *EIT = RetTy->getAs<BitIntType>())
74 if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
75 return getNaturalAlignIndirect(RetTy);
76
77 // Caller will do necessary sign/zero extension.
78 return ABIArgInfo::getDirect();
79 }
80
81 void computeInfo(CGFunctionInfo &FI) const override {
83 for (auto &I : FI.arguments())
84 I.info = classifyArgumentType(I.type);
85 }
86
87};
88
89class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
90public:
91 BPFTargetCodeGenInfo(CodeGenTypes &CGT)
92 : TargetCodeGenInfo(std::make_unique<BPFABIInfo>(CGT)) {}
93};
94
95}
96
97std::unique_ptr<TargetCodeGenInfo>
99 return std::make_unique<BPFTargetCodeGenInfo>(CGM.getTypes());
100}
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
CanQualType Int128Ty
Definition: ASTContext.h:1092
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2307
A fixed int type of a specified bitwidth.
Definition: Type.h:6664
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
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)
ASTContext & getContext() const
Definition: ABIInfo.cpp:20
bool isPromotableIntegerTypeForABI(QualType Ty) const
Definition: ABIInfo.cpp:163
CodeGen::ABIArgInfo getNaturalAlignIndirect(QualType Ty, bool ByVal=true, bool Realign=false, llvm::Type *Padding=nullptr) const
A convenience method to return an indirect ABIArgInfo with an expected alignment equal to the ABI ali...
Definition: ABIInfo.cpp:174
llvm::LLVMContext & getVMContext() const
Definition: ABIInfo.cpp:22
CGFunctionInfo - Class to encapsulate the information about a function definition.
CanQualType getReturnType() const
MutableArrayRef< ArgInfo > arguments()
This class organizes the cross-function state that is used while generating LLVM code.
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
Definition: CodeGenTypes.h:54
DefaultABIInfo - The default implementation for ABI specific details.
Definition: ABIInfoImpl.h:21
ABIArgInfo classifyArgumentType(QualType RetTy) const
Definition: ABIInfoImpl.cpp:17
ABIArgInfo classifyReturnType(QualType RetTy) const
Definition: ABIInfoImpl.cpp:45
void computeInfo(CGFunctionInfo &FI) const override
Definition: ABIInfoImpl.cpp:67
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition: TargetInfo.h:46
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:4997
A (possibly-)qualified type.
Definition: Type.h:736
bool isVoidType() const
Definition: Type.h:7352
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7558
std::unique_ptr< TargetCodeGenInfo > createBPFTargetCodeGenInfo(CodeGenModule &CGM)
Definition: BPF.cpp:98
bool isAggregateTypeForABI(QualType T)
QualType useFirstFieldIfTransparentUnion(QualType Ty)
Pass transparent unions as if they were the type of the first element.
unsigned long uint64_t
Definition: Format.h:5226