1//===- BPFCORE.h - Common info for Compile-Once Run-EveryWhere -*- C++ -*-===//
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#ifndef LLVM_LIB_TARGET_BPF_BPFCORE_H
10#define LLVM_LIB_TARGET_BPF_BPFCORE_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/BinaryFormat/Dwarf.h"
14#include "llvm/DebugInfo/BTF/BTF.h"
15#include "llvm/IR/DebugInfoMetadata.h"
16#include "llvm/IR/Instructions.h"
17#include "llvm/Support/ErrorHandling.h"
18
19namespace llvm {
20
21class BasicBlock;
22class Instruction;
23class Module;
24
25/// Return the bit offset used to order an element of a BTF structure record.
26inline uint64_t getBTFRecordElementOffset(const DINode *Element) {
27 switch (Element->getTag()) {
28 case dwarf::DW_TAG_member:
29 return cast<DIDerivedType>(Val: Element)->getOffsetInBits();
30 case dwarf::DW_TAG_variant_part:
31 return cast<DICompositeType>(Val: Element)->getOffsetInBits();
32 default:
33 llvm_unreachable("Unexpected DI tag of a struct element");
34 }
35}
36
37class BPFCoreSharedInfo {
38public:
39 enum BTFTypeIdFlag : uint32_t {
40 BTF_TYPE_ID_LOCAL_RELOC = 0,
41 BTF_TYPE_ID_REMOTE_RELOC,
42
43 MAX_BTF_TYPE_ID_FLAG,
44 };
45
46 enum PreserveTypeInfo : uint32_t {
47 PRESERVE_TYPE_INFO_EXISTENCE = 0,
48 PRESERVE_TYPE_INFO_SIZE,
49 PRESERVE_TYPE_INFO_MATCH,
50
51 MAX_PRESERVE_TYPE_INFO_FLAG,
52 };
53
54 enum PreserveEnumValue : uint32_t {
55 PRESERVE_ENUM_VALUE_EXISTENCE = 0,
56 PRESERVE_ENUM_VALUE,
57
58 MAX_PRESERVE_ENUM_VALUE_FLAG,
59 };
60
61 /// The attribute attached to globals representing a field access
62 static constexpr StringRef AmaAttr = "btf_ama";
63 /// The attribute attached to globals representing a type id
64 static constexpr StringRef TypeIdAttr = "btf_type_id";
65
66 struct AmaRelocation {
67 BTF::PatchableRelocKind Kind;
68 int64_t PatchImm;
69 StringRef AccessString;
70
71 static AmaRelocation decode(StringRef Name);
72 };
73
74 /// llvm.bpf.passthrough builtin seq number
75 static uint32_t SeqNum;
76
77 /// Insert a bpf passthrough builtin function.
78 static Instruction *insertPassThrough(Module *M, BasicBlock *BB,
79 Instruction *Input,
80 Instruction *Before);
81 static void removeArrayAccessCall(CallInst *Call);
82 static void removeStructAccessCall(CallInst *Call);
83 static void removeUnionAccessCall(CallInst *Call);
84};
85
86} // namespace llvm
87
88#endif
89