LLVM 24.0.0git
BPF.cpp
Go to the documentation of this file.
1//===- BPF.cpp - BPF ABI Implementation ----------------------------------===//
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
10#include "llvm/ABI/TargetInfo.h"
11#include "llvm/ABI/Types.h"
14
15namespace llvm::abi {
16
17class BPFTargetInfo : public TargetInfo {
18private:
19 ABICompatInfo CompatInfo;
20
21 ArgInfo classifyReturnType(const Type *RetTy) const {
22 if (RetTy->isVoid())
23 return ArgInfo::getIgnore();
24
25 if (isAggregateTypeForABI(RetTy)) {
26 if (RetTy->isZeroSize())
27 return ArgInfo::getIgnore();
29 /*ByVal=*/false);
30 }
31
32 if (const auto *IntTy = dyn_cast<IntegerType>(RetTy)) {
33 if (IntTy->isBitInt() && IntTy->getSizeInBits().getFixedValue() > 128)
35 /*ByVal=*/false);
36 }
37
38 return ArgInfo::getDirect();
39 }
40
41 ArgInfo classifyArgumentType(const Type *ArgTy) const {
42 if (const auto *RT = dyn_cast<RecordType>(ArgTy))
43 if (RT->isTransparentUnion() && RT->getNumFields() > 0)
44 ArgTy = RT->getFields()[0].FieldType;
45
46 if (isAggregateTypeForABI(ArgTy)) {
47 if (ArgTy->isZeroSize())
48 return ArgInfo::getIgnore();
49
50 auto SizeInBits = ArgTy->getSizeInBits().getFixedValue();
51 if (SizeInBits <= 128) {
52 const Type *CoerceTy;
53 if (SizeInBits <= 64) {
54 CoerceTy = TB.getIntegerType(alignTo(SizeInBits, 8), Align(8), false);
55 } else {
56 const Type *RegTy = TB.getIntegerType(64, Align(8), false);
57 CoerceTy = TB.getArrayType(RegTy, 2, 128);
58 }
59 return ArgInfo::getDirect(CoerceTy);
60 }
61
63 }
64
65 if (const auto *IntTy = dyn_cast<IntegerType>(ArgTy)) {
66 if (IntTy->isBitInt() && IntTy->getSizeInBits().getFixedValue() > 128)
68
69 if (isPromotableInteger(IntTy))
70 return ArgInfo::getExtend(ArgTy);
71 }
72
73 return ArgInfo::getDirect();
74 }
75
76public:
77 BPFTargetInfo(TypeBuilder &Builder) : TargetInfo(Builder) {}
78
79 const ABICompatInfo &getABICompatInfo() const override { return CompatInfo; }
80
81 void computeInfo(FunctionInfo &FI) const override {
82 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
83 for (auto &I : FI.arguments())
84 I.Info = classifyArgumentType(I.ABIType);
85 }
86};
87
88std::unique_ptr<TargetInfo> createBPFTargetInfo(TypeBuilder &TB) {
89 return std::make_unique<BPFTargetInfo>(TB);
90}
91
92} // namespace llvm::abi
#define I(x, y, z)
Definition MD5.cpp:57
Target-specific ABI information and factory functions.
Helper class to encapsulate information about how a specific type should be passed to or returned fro...
static ArgInfo getIgnore()
static ArgInfo getExtend(const Type *T)
static ArgInfo getDirect(const Type *T=nullptr, unsigned Offset=0, MaybeAlign Align=std::nullopt, bool CanBeFlattened=true)
const ABICompatInfo & getABICompatInfo() const override
Return this target's ABI compatibility flags.
Definition BPF.cpp:79
void computeInfo(FunctionInfo &FI) const override
Populate FI with the target's ABI-lowering decisions for each argument and return value.
Definition BPF.cpp:81
BPFTargetInfo(TypeBuilder &Builder)
Definition BPF.cpp:77
ArrayRef< ArgEntry > arguments() const
const Type * getReturnType() const
virtual unsigned getAllocaAddrSpace() const
Address space in which indirect arguments are allocated (the target's alloca/stack space).
Definition TargetInfo.h:85
LLVM_ABI bool isPromotableInteger(const IntegerType *IT) const
TargetInfo(TypeBuilder &Builder)
Definition TargetInfo.h:70
LLVM_ABI bool isAggregateTypeForABI(const Type *Ty) const
TypeBuilder & TB
Definition TargetInfo.h:67
LLVM_ABI ArgInfo getNaturalAlignIndirect(const Type *Ty, unsigned AddrSpace, bool ByVal=true) const
TypeBuilder manages the lifecycle of ABI types using bump pointer allocation.
Definition Types.h:443
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:46
bool isVoid() const
Definition Types.h:92
TypeSize getSizeInBits() const
Definition Types.h:74
bool isZeroSize() const
Definition Types.h:103
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
This file defines the type system for the LLVMABI library, which mirrors ABI-relevant aspects of fron...
LLVM_ABI std::unique_ptr< TargetInfo > createBPFTargetInfo(TypeBuilder &TB)
Definition BPF.cpp:88
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Flags controlling ABI compatibility behaviour that applies to every target.
Definition TargetInfo.h:43