Insert GetElementpointer statement in LLVM IR

I am wondering how to insert a GetElementPointer statement into LLVM IR through LLVM Pass, let's say we have an array

%arr4 = alloca [100000 x i32], align 4

and want to insert gep like

 %arrayidx = getelementptr inbounds [100000 x i32]* %arr, i32 0, i32 %some value

what will be the sequence of instructions for writing, as in the IRBuilder class, there are so many instructions for creating getelementpointer. Which one to use and what its parameters will be. can someone explain this with an example Any help would be appreciated.

+4
source share
1 answer

Let's start with the documentation for GetElementPtrInst , since IRBuilder provides a wrapper for its constructors. If we want to add this instruction, I usually go straight ahead and call create.

GetElementPtrInst::Create(ptr, IdxList, name, insertpoint)
  • Ptr: *, ptr, GetElementPtr (GEP). % arr.
  • IdxList: , , GEP. 0 % .
  • : IR. "% arrayidx", "arrayidx".
  • insertpoint: IRBuilder , ( , ).

, :

Value* arr = ...; // This is the instruction producing %arr
Value* someValue = ...; // This is the instruction producing %some value

// We need an array of index values
//   Note - we need a type for constants, so use someValue type
Value* indexList[2] = {ConstantInt::get(someValue->getType(), 0), someValue};
GetElementPtrInst* gepInst = GetElementPtrInst::Create(arr, ArrayRef<Value*>(indexList, 2), "arrayIdx", <some location to insert>);

IRBuilder, :

IRBuilder::CreateGEP(ptr, idxList, name)

IRBuilder, IRBuilder.

+7
source

All Articles