Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/CodeGen/Targets/BPF.cpp
Line
Count
Source (jump to first uncovered line)
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
12
using namespace clang;
13
using namespace clang::CodeGen;
14
15
//===----------------------------------------------------------------------===//
16
// BPF ABI Implementation
17
//===----------------------------------------------------------------------===//
18
19
namespace {
20
21
class BPFABIInfo : public DefaultABIInfo {
22
public:
23
0
  BPFABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
24
25
0
  ABIArgInfo classifyArgumentType(QualType Ty) const {
26
0
    Ty = useFirstFieldIfTransparentUnion(Ty);
27
28
0
    if (isAggregateTypeForABI(Ty)) {
29
0
      uint64_t Bits = getContext().getTypeSize(Ty);
30
0
      if (Bits == 0)
31
0
        return ABIArgInfo::getIgnore();
32
33
      // If the aggregate needs 1 or 2 registers, do not use reference.
34
0
      if (Bits <= 128) {
35
0
        llvm::Type *CoerceTy;
36
0
        if (Bits <= 64) {
37
0
          CoerceTy =
38
0
              llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
39
0
        } else {
40
0
          llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), 64);
41
0
          CoerceTy = llvm::ArrayType::get(RegTy, 2);
42
0
        }
43
0
        return ABIArgInfo::getDirect(CoerceTy);
44
0
      } else {
45
0
        return getNaturalAlignIndirect(Ty);
46
0
      }
47
0
    }
48
49
0
    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
50
0
      Ty = EnumTy->getDecl()->getIntegerType();
51
52
0
    ASTContext &Context = getContext();
53
0
    if (const auto *EIT = Ty->getAs<BitIntType>())
54
0
      if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
55
0
        return getNaturalAlignIndirect(Ty);
56
57
0
    return (isPromotableIntegerTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
58
0
                                              : ABIArgInfo::getDirect());
59
0
  }
60
61
0
  ABIArgInfo classifyReturnType(QualType RetTy) const {
62
0
    if (RetTy->isVoidType())
63
0
      return ABIArgInfo::getIgnore();
64
65
0
    if (isAggregateTypeForABI(RetTy))
66
0
      return getNaturalAlignIndirect(RetTy);
67
68
    // Treat an enum type as its underlying type.
69
0
    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
70
0
      RetTy = EnumTy->getDecl()->getIntegerType();
71
72
0
    ASTContext &Context = getContext();
73
0
    if (const auto *EIT = RetTy->getAs<BitIntType>())
74
0
      if (EIT->getNumBits() > Context.getTypeSize(Context.Int128Ty))
75
0
        return getNaturalAlignIndirect(RetTy);
76
77
    // Caller will do necessary sign/zero extension.
78
0
    return ABIArgInfo::getDirect();
79
0
  }
80
81
0
  void computeInfo(CGFunctionInfo &FI) const override {
82
0
    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
83
0
    for (auto &I : FI.arguments())
84
0
      I.info = classifyArgumentType(I.type);
85
0
  }
86
87
};
88
89
class BPFTargetCodeGenInfo : public TargetCodeGenInfo {
90
public:
91
  BPFTargetCodeGenInfo(CodeGenTypes &CGT)
92
0
      : TargetCodeGenInfo(std::make_unique<BPFABIInfo>(CGT)) {}
93
};
94
95
}
96
97
std::unique_ptr<TargetCodeGenInfo>
98
0
CodeGen::createBPFTargetCodeGenInfo(CodeGenModule &CGM) {
99
0
  return std::make_unique<BPFTargetCodeGenInfo>(CGM.getTypes());
100
0
}