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