1 //===------ BPFAbstractMemberAccess.cpp - Abstracting Member Accesses -----===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass abstracted struct/union member accesses in order to support
10 // compile-once run-everywhere (CO-RE). The CO-RE intends to compile the program
11 // which can run on different kernels. In particular, if bpf program tries to
12 // access a particular kernel data structure member, the details of the
13 // intermediate member access will be remembered so bpf loader can do
14 // necessary adjustment right before program loading.
28 // For the member access e.c.b, the compiler will generate code
31 // The compile-once run-everywhere instead generates the following code
34 // The "4" in "r = 4" can be changed based on a particular kernel version.
35 // For example, on a particular kernel version, if struct s is changed to
43 // By repeating the member access on the host, the bpf loader can
44 // adjust "r = 4" as "r = 8".
46 // This feature relies on the following three intrinsic calls:
47 // addr = preserve_array_access_index(base, dimension, index)
48 // addr = preserve_union_access_index(base, di_index)
49 // !llvm.preserve.access.index <union_ditype>
50 // addr = preserve_struct_access_index(base, gep_index, di_index)
51 // !llvm.preserve.access.index <struct_ditype>
53 //===----------------------------------------------------------------------===//
57 #include "BPFTargetMachine.h"
58 #include "llvm/IR/DebugInfoMetadata.h"
59 #include "llvm/IR/GlobalVariable.h"
60 #include "llvm/IR/Instruction.h"
61 #include "llvm/IR/Instructions.h"
62 #include "llvm/IR/Module.h"
63 #include "llvm/IR/Type.h"
64 #include "llvm/IR/User.h"
65 #include "llvm/IR/Value.h"
66 #include "llvm/Pass.h"
67 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
70 #define DEBUG_TYPE "bpf-abstract-member-access"
73 const std::string
BPFCoreSharedInfo::AmaAttr
= "btf_ama";
74 const std::string
BPFCoreSharedInfo::PatchableExtSecName
=
75 ".BPF.patchable_externs";
82 class BPFAbstractMemberAccess final
: public ModulePass
{
83 StringRef
getPassName() const override
{
84 return "BPF Abstract Member Access";
87 bool runOnModule(Module
&M
) override
;
91 BPFAbstractMemberAccess() : ModulePass(ID
) {}
95 BPFPreserveArrayAI
= 1,
96 BPFPreserveUnionAI
= 2,
97 BPFPreserveStructAI
= 3,
100 std::map
<std::string
, GlobalVariable
*> GEPGlobals
;
101 // A map to link preserve_*_access_index instrinsic calls.
102 std::map
<CallInst
*, std::pair
<CallInst
*, uint32_t>> AIChain
;
103 // A map to hold all the base preserve_*_access_index instrinsic calls.
104 // The base call is not an input of any other preserve_*_access_index
106 std::map
<CallInst
*, uint32_t> BaseAICalls
;
108 bool doTransformation(Module
&M
);
110 void traceAICall(CallInst
*Call
, uint32_t Kind
, const MDNode
*ParentMeta
,
112 void traceBitCast(BitCastInst
*BitCast
, CallInst
*Parent
, uint32_t Kind
,
113 const MDNode
*ParentMeta
, uint32_t ParentAI
);
114 void traceGEP(GetElementPtrInst
*GEP
, CallInst
*Parent
, uint32_t Kind
,
115 const MDNode
*ParentMeta
, uint32_t ParentAI
);
116 void collectAICallChains(Module
&M
, Function
&F
);
118 bool IsPreserveDIAccessIndexCall(const CallInst
*Call
, uint32_t &Kind
,
119 const MDNode
*&TypeMeta
, uint32_t &AccessIndex
);
120 bool IsValidAIChain(const MDNode
*ParentMeta
, uint32_t ParentAI
,
121 const MDNode
*ChildMeta
);
122 bool removePreserveAccessIndexIntrinsic(Module
&M
);
123 void replaceWithGEP(std::vector
<CallInst
*> &CallList
,
124 uint32_t NumOfZerosIndex
, uint32_t DIIndex
);
126 Value
*computeBaseAndAccessKey(CallInst
*Call
, std::string
&AccessKey
,
127 uint32_t Kind
, MDNode
*&BaseMeta
);
128 bool getAccessIndex(const Value
*IndexValue
, uint64_t &AccessIndex
);
129 bool transformGEPChain(Module
&M
, CallInst
*Call
, uint32_t Kind
);
131 } // End anonymous namespace
133 char BPFAbstractMemberAccess::ID
= 0;
134 INITIALIZE_PASS(BPFAbstractMemberAccess
, DEBUG_TYPE
,
135 "abstracting struct/union member accessees", false, false)
137 ModulePass
*llvm::createBPFAbstractMemberAccess() {
138 return new BPFAbstractMemberAccess();
141 bool BPFAbstractMemberAccess::runOnModule(Module
&M
) {
142 LLVM_DEBUG(dbgs() << "********** Abstract Member Accesses **********\n");
144 // Bail out if no debug info.
145 if (empty(M
.debug_compile_units()))
148 return doTransformation(M
);
151 static bool SkipDIDerivedTag(unsigned Tag
) {
152 if (Tag
!= dwarf::DW_TAG_typedef
&& Tag
!= dwarf::DW_TAG_const_type
&&
153 Tag
!= dwarf::DW_TAG_volatile_type
&&
154 Tag
!= dwarf::DW_TAG_restrict_type
&&
155 Tag
!= dwarf::DW_TAG_member
)
160 static DIType
* stripQualifiers(DIType
*Ty
) {
161 while (auto *DTy
= dyn_cast
<DIDerivedType
>(Ty
)) {
162 if (!SkipDIDerivedTag(DTy
->getTag()))
164 Ty
= DTy
->getBaseType();
169 static const DIType
* stripQualifiers(const DIType
*Ty
) {
170 while (auto *DTy
= dyn_cast
<DIDerivedType
>(Ty
)) {
171 if (!SkipDIDerivedTag(DTy
->getTag()))
173 Ty
= DTy
->getBaseType();
178 static uint32_t calcArraySize(const DICompositeType
*CTy
, uint32_t StartDim
) {
179 DINodeArray Elements
= CTy
->getElements();
180 uint32_t DimSize
= 1;
181 for (uint32_t I
= StartDim
; I
< Elements
.size(); ++I
) {
182 if (auto *Element
= dyn_cast_or_null
<DINode
>(Elements
[I
]))
183 if (Element
->getTag() == dwarf::DW_TAG_subrange_type
) {
184 const DISubrange
*SR
= cast
<DISubrange
>(Element
);
185 auto *CI
= SR
->getCount().dyn_cast
<ConstantInt
*>();
186 DimSize
*= CI
->getSExtValue();
193 /// Check whether a call is a preserve_*_access_index intrinsic call or not.
194 bool BPFAbstractMemberAccess::IsPreserveDIAccessIndexCall(const CallInst
*Call
,
196 const MDNode
*&TypeMeta
,
197 uint32_t &AccessIndex
) {
201 const auto *GV
= dyn_cast
<GlobalValue
>(Call
->getCalledValue());
204 if (GV
->getName().startswith("llvm.preserve.array.access.index")) {
205 Kind
= BPFPreserveArrayAI
;
206 TypeMeta
= Call
->getMetadata(LLVMContext::MD_preserve_access_index
);
208 report_fatal_error("Missing metadata for llvm.preserve.array.access.index intrinsic");
209 AccessIndex
= cast
<ConstantInt
>(Call
->getArgOperand(2))
213 if (GV
->getName().startswith("llvm.preserve.union.access.index")) {
214 Kind
= BPFPreserveUnionAI
;
215 TypeMeta
= Call
->getMetadata(LLVMContext::MD_preserve_access_index
);
217 report_fatal_error("Missing metadata for llvm.preserve.union.access.index intrinsic");
218 AccessIndex
= cast
<ConstantInt
>(Call
->getArgOperand(1))
222 if (GV
->getName().startswith("llvm.preserve.struct.access.index")) {
223 Kind
= BPFPreserveStructAI
;
224 TypeMeta
= Call
->getMetadata(LLVMContext::MD_preserve_access_index
);
226 report_fatal_error("Missing metadata for llvm.preserve.struct.access.index intrinsic");
227 AccessIndex
= cast
<ConstantInt
>(Call
->getArgOperand(2))
235 void BPFAbstractMemberAccess::replaceWithGEP(std::vector
<CallInst
*> &CallList
,
236 uint32_t DimensionIndex
,
238 for (auto Call
: CallList
) {
239 uint32_t Dimension
= 1;
240 if (DimensionIndex
> 0)
241 Dimension
= cast
<ConstantInt
>(Call
->getArgOperand(DimensionIndex
))
245 ConstantInt::get(Type::getInt32Ty(Call
->getParent()->getContext()), 0);
246 SmallVector
<Value
*, 4> IdxList
;
247 for (unsigned I
= 0; I
< Dimension
; ++I
)
248 IdxList
.push_back(Zero
);
249 IdxList
.push_back(Call
->getArgOperand(GEPIndex
));
251 auto *GEP
= GetElementPtrInst::CreateInBounds(Call
->getArgOperand(0),
253 Call
->replaceAllUsesWith(GEP
);
254 Call
->eraseFromParent();
258 bool BPFAbstractMemberAccess::removePreserveAccessIndexIntrinsic(Module
&M
) {
259 std::vector
<CallInst
*> PreserveArrayIndexCalls
;
260 std::vector
<CallInst
*> PreserveUnionIndexCalls
;
261 std::vector
<CallInst
*> PreserveStructIndexCalls
;
264 for (Function
&F
: M
)
267 auto *Call
= dyn_cast
<CallInst
>(&I
);
269 const MDNode
*TypeMeta
;
270 uint32_t AccessIndex
;
271 if (!IsPreserveDIAccessIndexCall(Call
, Kind
, TypeMeta
, AccessIndex
))
275 if (Kind
== BPFPreserveArrayAI
)
276 PreserveArrayIndexCalls
.push_back(Call
);
277 else if (Kind
== BPFPreserveUnionAI
)
278 PreserveUnionIndexCalls
.push_back(Call
);
280 PreserveStructIndexCalls
.push_back(Call
);
283 // do the following transformation:
284 // . addr = preserve_array_access_index(base, dimension, index)
286 // addr = GEP(base, dimenion's zero's, index)
287 // . addr = preserve_union_access_index(base, di_index)
289 // addr = base, i.e., all usages of "addr" are replaced by "base".
290 // . addr = preserve_struct_access_index(base, gep_index, di_index)
292 // addr = GEP(base, 0, gep_index)
293 replaceWithGEP(PreserveArrayIndexCalls
, 1, 2);
294 replaceWithGEP(PreserveStructIndexCalls
, 0, 1);
295 for (auto Call
: PreserveUnionIndexCalls
) {
296 Call
->replaceAllUsesWith(Call
->getArgOperand(0));
297 Call
->eraseFromParent();
303 /// Check whether the access index chain is valid. We check
304 /// here because there may be type casts between two
305 /// access indexes. We want to ensure memory access still valid.
306 bool BPFAbstractMemberAccess::IsValidAIChain(const MDNode
*ParentType
,
308 const MDNode
*ChildType
) {
309 const DIType
*PType
= stripQualifiers(cast
<DIType
>(ParentType
));
310 const DIType
*CType
= stripQualifiers(cast
<DIType
>(ChildType
));
312 // Child is a derived/pointer type, which is due to type casting.
313 // Pointer type cannot be in the middle of chain.
314 if (isa
<DIDerivedType
>(CType
))
317 // Parent is a pointer type.
318 if (const auto *PtrTy
= dyn_cast
<DIDerivedType
>(PType
)) {
319 if (PtrTy
->getTag() != dwarf::DW_TAG_pointer_type
)
321 return stripQualifiers(PtrTy
->getBaseType()) == CType
;
324 // Otherwise, struct/union/array types
325 const auto *PTy
= dyn_cast
<DICompositeType
>(PType
);
326 const auto *CTy
= dyn_cast
<DICompositeType
>(CType
);
327 assert(PTy
&& CTy
&& "ParentType or ChildType is null or not composite");
329 uint32_t PTyTag
= PTy
->getTag();
330 assert(PTyTag
== dwarf::DW_TAG_array_type
||
331 PTyTag
== dwarf::DW_TAG_structure_type
||
332 PTyTag
== dwarf::DW_TAG_union_type
);
334 uint32_t CTyTag
= CTy
->getTag();
335 assert(CTyTag
== dwarf::DW_TAG_array_type
||
336 CTyTag
== dwarf::DW_TAG_structure_type
||
337 CTyTag
== dwarf::DW_TAG_union_type
);
339 // Multi dimensional arrays, base element should be the same
340 if (PTyTag
== dwarf::DW_TAG_array_type
&& PTyTag
== CTyTag
)
341 return PTy
->getBaseType() == CTy
->getBaseType();
344 if (PTyTag
== dwarf::DW_TAG_array_type
)
345 Ty
= PTy
->getBaseType();
347 Ty
= dyn_cast
<DIType
>(PTy
->getElements()[ParentAI
]);
349 return dyn_cast
<DICompositeType
>(stripQualifiers(Ty
)) == CTy
;
352 void BPFAbstractMemberAccess::traceAICall(CallInst
*Call
, uint32_t Kind
,
353 const MDNode
*ParentMeta
,
355 for (User
*U
: Call
->users()) {
356 Instruction
*Inst
= dyn_cast
<Instruction
>(U
);
360 if (auto *BI
= dyn_cast
<BitCastInst
>(Inst
)) {
361 traceBitCast(BI
, Call
, Kind
, ParentMeta
, ParentAI
);
362 } else if (auto *CI
= dyn_cast
<CallInst
>(Inst
)) {
364 const MDNode
*ChildMeta
;
366 if (IsPreserveDIAccessIndexCall(CI
, CIKind
, ChildMeta
, ChildAI
) &&
367 IsValidAIChain(ParentMeta
, ParentAI
, ChildMeta
)) {
368 AIChain
[CI
] = std::make_pair(Call
, Kind
);
369 traceAICall(CI
, CIKind
, ChildMeta
, ChildAI
);
371 BaseAICalls
[Call
] = Kind
;
373 } else if (auto *GI
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
374 if (GI
->hasAllZeroIndices())
375 traceGEP(GI
, Call
, Kind
, ParentMeta
, ParentAI
);
377 BaseAICalls
[Call
] = Kind
;
379 BaseAICalls
[Call
] = Kind
;
384 void BPFAbstractMemberAccess::traceBitCast(BitCastInst
*BitCast
,
385 CallInst
*Parent
, uint32_t Kind
,
386 const MDNode
*ParentMeta
,
388 for (User
*U
: BitCast
->users()) {
389 Instruction
*Inst
= dyn_cast
<Instruction
>(U
);
393 if (auto *BI
= dyn_cast
<BitCastInst
>(Inst
)) {
394 traceBitCast(BI
, Parent
, Kind
, ParentMeta
, ParentAI
);
395 } else if (auto *CI
= dyn_cast
<CallInst
>(Inst
)) {
397 const MDNode
*ChildMeta
;
399 if (IsPreserveDIAccessIndexCall(CI
, CIKind
, ChildMeta
, ChildAI
) &&
400 IsValidAIChain(ParentMeta
, ParentAI
, ChildMeta
)) {
401 AIChain
[CI
] = std::make_pair(Parent
, Kind
);
402 traceAICall(CI
, CIKind
, ChildMeta
, ChildAI
);
404 BaseAICalls
[Parent
] = Kind
;
406 } else if (auto *GI
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
407 if (GI
->hasAllZeroIndices())
408 traceGEP(GI
, Parent
, Kind
, ParentMeta
, ParentAI
);
410 BaseAICalls
[Parent
] = Kind
;
412 BaseAICalls
[Parent
] = Kind
;
417 void BPFAbstractMemberAccess::traceGEP(GetElementPtrInst
*GEP
, CallInst
*Parent
,
418 uint32_t Kind
, const MDNode
*ParentMeta
,
420 for (User
*U
: GEP
->users()) {
421 Instruction
*Inst
= dyn_cast
<Instruction
>(U
);
425 if (auto *BI
= dyn_cast
<BitCastInst
>(Inst
)) {
426 traceBitCast(BI
, Parent
, Kind
, ParentMeta
, ParentAI
);
427 } else if (auto *CI
= dyn_cast
<CallInst
>(Inst
)) {
429 const MDNode
*ChildMeta
;
431 if (IsPreserveDIAccessIndexCall(CI
, CIKind
, ChildMeta
, ChildAI
) &&
432 IsValidAIChain(ParentMeta
, ParentAI
, ChildMeta
)) {
433 AIChain
[CI
] = std::make_pair(Parent
, Kind
);
434 traceAICall(CI
, CIKind
, ChildMeta
, ChildAI
);
436 BaseAICalls
[Parent
] = Kind
;
438 } else if (auto *GI
= dyn_cast
<GetElementPtrInst
>(Inst
)) {
439 if (GI
->hasAllZeroIndices())
440 traceGEP(GI
, Parent
, Kind
, ParentMeta
, ParentAI
);
442 BaseAICalls
[Parent
] = Kind
;
444 BaseAICalls
[Parent
] = Kind
;
449 void BPFAbstractMemberAccess::collectAICallChains(Module
&M
, Function
&F
) {
456 const MDNode
*TypeMeta
;
457 uint32_t AccessIndex
;
458 auto *Call
= dyn_cast
<CallInst
>(&I
);
459 if (!IsPreserveDIAccessIndexCall(Call
, Kind
, TypeMeta
, AccessIndex
) ||
460 AIChain
.find(Call
) != AIChain
.end())
463 traceAICall(Call
, Kind
, TypeMeta
, AccessIndex
);
467 /// Get access index from the preserve_*_access_index intrinsic calls.
468 bool BPFAbstractMemberAccess::getAccessIndex(const Value
*IndexValue
,
469 uint64_t &AccessIndex
) {
470 const ConstantInt
*CV
= dyn_cast
<ConstantInt
>(IndexValue
);
474 AccessIndex
= CV
->getValue().getZExtValue();
478 /// Compute the base of the whole preserve_*_access_index chains, i.e., the base
479 /// pointer of the first preserve_*_access_index call, and construct the access
480 /// string, which will be the name of a global variable.
481 Value
*BPFAbstractMemberAccess::computeBaseAndAccessKey(CallInst
*Call
,
482 std::string
&AccessKey
,
485 Value
*Base
= nullptr;
486 std::string TypeName
;
487 std::stack
<std::pair
<CallInst
*, uint32_t>> CallStack
;
489 // Put the access chain into a stack with the top as the head of the chain.
491 CallStack
.push(std::make_pair(Call
, Kind
));
492 Kind
= AIChain
[Call
].second
;
493 Call
= AIChain
[Call
].first
;
496 // The access offset from the base of the head of chain is also
497 // calculated here as all debuginfo types are available.
499 // Get type name and calculate the first index.
500 // We only want to get type name from structure or union.
501 // If user wants a relocation like
502 // int *p; ... __builtin_preserve_access_index(&p[4]) ...
504 // int a[10][20]; ... __builtin_preserve_access_index(&a[2][3]) ...
505 // we will skip them.
506 uint32_t FirstIndex
= 0;
507 uint32_t AccessOffset
= 0;
508 while (CallStack
.size()) {
509 auto StackElem
= CallStack
.top();
510 Call
= StackElem
.first
;
511 Kind
= StackElem
.second
;
514 Base
= Call
->getArgOperand(0);
516 MDNode
*MDN
= Call
->getMetadata(LLVMContext::MD_preserve_access_index
);
517 DIType
*Ty
= stripQualifiers(cast
<DIType
>(MDN
));
518 if (Kind
== BPFPreserveUnionAI
|| Kind
== BPFPreserveStructAI
) {
519 // struct or union type
520 TypeName
= Ty
->getName();
522 AccessOffset
+= FirstIndex
* Ty
->getSizeInBits() >> 3;
526 // Array entries will always be consumed for accumulative initial index.
529 // BPFPreserveArrayAI
530 uint64_t AccessIndex
;
531 if (!getAccessIndex(Call
->getArgOperand(2), AccessIndex
))
534 DIType
*BaseTy
= nullptr;
535 bool CheckElemType
= false;
536 if (const auto *CTy
= dyn_cast
<DICompositeType
>(Ty
)) {
538 assert(CTy
->getTag() == dwarf::DW_TAG_array_type
);
541 FirstIndex
+= AccessIndex
* calcArraySize(CTy
, 1);
542 BaseTy
= stripQualifiers(CTy
->getBaseType());
543 CheckElemType
= CTy
->getElements().size() == 1;
546 auto *DTy
= cast
<DIDerivedType
>(Ty
);
547 assert(DTy
->getTag() == dwarf::DW_TAG_pointer_type
);
549 BaseTy
= stripQualifiers(DTy
->getBaseType());
550 CTy
= dyn_cast
<DICompositeType
>(BaseTy
);
552 CheckElemType
= true;
553 } else if (CTy
->getTag() != dwarf::DW_TAG_array_type
) {
554 FirstIndex
+= AccessIndex
;
555 CheckElemType
= true;
557 FirstIndex
+= AccessIndex
* calcArraySize(CTy
, 0);
562 auto *CTy
= dyn_cast
<DICompositeType
>(BaseTy
);
566 unsigned CTag
= CTy
->getTag();
567 if (CTag
!= dwarf::DW_TAG_structure_type
&& CTag
!= dwarf::DW_TAG_union_type
)
570 TypeName
= CTy
->getName();
572 AccessOffset
+= FirstIndex
* CTy
->getSizeInBits() >> 3;
576 assert(TypeName
.size());
577 AccessKey
+= std::to_string(FirstIndex
);
579 // Traverse the rest of access chain to complete offset calculation
580 // and access key construction.
581 while (CallStack
.size()) {
582 auto StackElem
= CallStack
.top();
583 Call
= StackElem
.first
;
584 Kind
= StackElem
.second
;
588 uint64_t AccessIndex
;
589 uint32_t ArgIndex
= (Kind
== BPFPreserveUnionAI
) ? 1 : 2;
590 if (!getAccessIndex(Call
->getArgOperand(ArgIndex
), AccessIndex
))
592 AccessKey
+= ":" + std::to_string(AccessIndex
);
594 MDNode
*MDN
= Call
->getMetadata(LLVMContext::MD_preserve_access_index
);
595 // At this stage, it cannot be pointer type.
596 auto *CTy
= cast
<DICompositeType
>(stripQualifiers(cast
<DIType
>(MDN
)));
597 uint32_t Tag
= CTy
->getTag();
598 if (Tag
== dwarf::DW_TAG_structure_type
) {
599 auto *MemberTy
= cast
<DIDerivedType
>(CTy
->getElements()[AccessIndex
]);
600 AccessOffset
+= MemberTy
->getOffsetInBits() >> 3;
601 } else if (Tag
== dwarf::DW_TAG_array_type
) {
602 auto *EltTy
= stripQualifiers(CTy
->getBaseType());
603 AccessOffset
+= AccessIndex
* calcArraySize(CTy
, 1) *
604 EltTy
->getSizeInBits() >> 3;
608 // Access key is the type name + access string, uniquely identifying
609 // one kernel memory access.
610 AccessKey
= TypeName
+ ":" + std::to_string(AccessOffset
) + "$" + AccessKey
;
615 /// Call/Kind is the base preserve_*_access_index() call. Attempts to do
616 /// transformation to a chain of relocable GEPs.
617 bool BPFAbstractMemberAccess::transformGEPChain(Module
&M
, CallInst
*Call
,
619 std::string AccessKey
;
622 computeBaseAndAccessKey(Call
, AccessKey
, Kind
, TypeMeta
);
626 // Do the transformation
627 // For any original GEP Call and Base %2 like
628 // %4 = bitcast %struct.net_device** %dev1 to i64*
629 // it is transformed to:
630 // %6 = load sk_buff:50:$0:0:0:2:0
631 // %7 = bitcast %struct.sk_buff* %2 to i8*
632 // %8 = getelementptr i8, i8* %7, %6
633 // %9 = bitcast i8* %8 to i64*
634 // using %9 instead of %4
635 // The original Call inst is removed.
636 BasicBlock
*BB
= Call
->getParent();
639 if (GEPGlobals
.find(AccessKey
) == GEPGlobals
.end()) {
640 GV
= new GlobalVariable(M
, Type::getInt64Ty(BB
->getContext()), false,
641 GlobalVariable::ExternalLinkage
, NULL
, AccessKey
);
642 GV
->addAttribute(BPFCoreSharedInfo::AmaAttr
);
643 GV
->setMetadata(LLVMContext::MD_preserve_access_index
, TypeMeta
);
644 GEPGlobals
[AccessKey
] = GV
;
646 GV
= GEPGlobals
[AccessKey
];
649 // Load the global variable.
650 auto *LDInst
= new LoadInst(Type::getInt64Ty(BB
->getContext()), GV
);
651 BB
->getInstList().insert(Call
->getIterator(), LDInst
);
653 // Generate a BitCast
654 auto *BCInst
= new BitCastInst(Base
, Type::getInt8PtrTy(BB
->getContext()));
655 BB
->getInstList().insert(Call
->getIterator(), BCInst
);
657 // Generate a GetElementPtr
658 auto *GEP
= GetElementPtrInst::Create(Type::getInt8Ty(BB
->getContext()),
660 BB
->getInstList().insert(Call
->getIterator(), GEP
);
662 // Generate a BitCast
663 auto *BCInst2
= new BitCastInst(GEP
, Call
->getType());
664 BB
->getInstList().insert(Call
->getIterator(), BCInst2
);
666 Call
->replaceAllUsesWith(BCInst2
);
667 Call
->eraseFromParent();
672 bool BPFAbstractMemberAccess::doTransformation(Module
&M
) {
673 bool Transformed
= false;
675 for (Function
&F
: M
) {
676 // Collect PreserveDIAccessIndex Intrinsic call chains.
677 // The call chains will be used to generate the access
678 // patterns similar to GEP.
679 collectAICallChains(M
, F
);
681 for (auto &C
: BaseAICalls
)
682 Transformed
= transformGEPChain(M
, C
.first
, C
.second
) || Transformed
;
685 return removePreserveAccessIndexIntrinsic(M
) || Transformed
;