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