LLVM 23.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 TypeBuilder &TB;
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();
28 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
29 }
30
31 if (const auto *IntTy = dyn_cast<IntegerType>(RetTy)) {
32 if (IntTy->isBitInt() && IntTy->getSizeInBits().getFixedValue() > 128)
33 return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
34 }
35
36 return ArgInfo::getDirect();
37 }
38
39 ArgInfo classifyArgumentType(const Type *ArgTy) const {
40 if (const auto *RT = dyn_cast<RecordType>(ArgTy))
41 if (RT->isTransparentUnion() && RT->getNumFields() > 0)
42 ArgTy = RT->getFields()[0].FieldType;
43
44 if (isAggregateTypeForABI(ArgTy)) {
45 if (ArgTy->isZeroSize())
46 return ArgInfo::getIgnore();
47
48 auto SizeInBits = ArgTy->getSizeInBits().getFixedValue();
49 if (SizeInBits <= 128) {
50 const Type *CoerceTy;
51 if (SizeInBits <= 64) {
52 CoerceTy = TB.getIntegerType(alignTo(SizeInBits, 8), Align(8), false);
53 } else {
54 const Type *RegTy = TB.getIntegerType(64, Align(8), false);
55 CoerceTy = TB.getArrayType(RegTy, 2, 128);
56 }
57 return ArgInfo::getDirect(CoerceTy);
58 }
59
60 return getNaturalAlignIndirect(ArgTy, /*ByVal=*/true);
61 }
62
63 if (const auto *IntTy = dyn_cast<IntegerType>(ArgTy)) {
64 if (IntTy->isBitInt() && IntTy->getSizeInBits().getFixedValue() > 128)
65 return getNaturalAlignIndirect(ArgTy, /*ByVal=*/true);
66
67 if (isPromotableInteger(IntTy))
68 return ArgInfo::getExtend(ArgTy);
69 }
70
71 return ArgInfo::getDirect();
72 }
73
74public:
75 BPFTargetInfo(TypeBuilder &TB) : TB(TB) {}
76
77 void computeInfo(FunctionInfo &FI) const override {
78 FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
79 for (auto &I : FI.arguments())
80 I.Info = classifyArgumentType(I.ABIType);
81 }
82};
83
84std::unique_ptr<TargetInfo> createBPFTargetInfo(TypeBuilder &TB) {
85 return std::make_unique<BPFTargetInfo>(TB);
86}
87
88} // 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 getDirect(const Type *T=nullptr, unsigned Offset=0, MaybeAlign Align=std::nullopt)
static ArgInfo getIgnore()
static ArgInfo getExtend(const Type *T)
void computeInfo(FunctionInfo &FI) const override
Populate FI with the target's ABI-lowering decisions for each argument and return value.
Definition BPF.cpp:77
BPFTargetInfo(TypeBuilder &TB)
Definition BPF.cpp:75
ArrayRef< ArgEntry > arguments() const
const Type * getReturnType() const
ArgInfo getNaturalAlignIndirect(const Type *Ty, bool ByVal=true) const
bool isPromotableInteger(const IntegerType *IT) const
bool isAggregateTypeForABI(const Type *Ty) const
TypeBuilder manages the lifecycle of ABI types using bump pointer allocation.
Definition Types.h:328
Represents the ABI-specific view of a type in LLVM.
Definition Types.h:43
bool isVoid() const
Definition Types.h:74
TypeSize getSizeInBits() const
Definition Types.h:67
bool isZeroSize() const
Definition Types.h:83
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...
std::unique_ptr< TargetInfo > createBPFTargetInfo(TypeBuilder &TB)
Definition BPF.cpp:84
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