我想知道如何通过LLVM Pass在LLVM IR中插入GetElementPointer指令,假设我有一个数组
%arr4 = alloca [100000 x i32], align 4
并希望插入类似
的gep %arrayidx = getelementptr inbounds [100000 x i32]* %arr, i32 0, i32 %some value
在IRBuilder类中写入的指令序列将有如此多的指令来创建getelementpointer。使用哪一个以及它的参数是什么。 任何人都可以用例子解释它 任何帮助将不胜感激。
答案 0 :(得分:7)
让我们从GetElementPtrInst的文档开始,因为IRBuilder为其构造函数提供了一个包装器。如果我们想要添加此指令,我通常会直接调用create。
GetElementPtrInst::Create(ptr, IdxList, name, insertpoint)
将这些部分放在一起,我们有以下代码序列:
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's 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,它具有非常相似的function:
IRBuilder::CreateGEP(ptr, idxList, name)
如果您想使用IRBuilder,那么您可以使用类似的IRBuilder调用替换代码段的最后一行。