LLVM IR:获取 LVALUE 操作数

LLVM IR: Get LVALUE operand

我有以下指示:

%ptrA = getelementptr float, float addrspace(1)* %A, i32 %id

我可以使用 getOperand(0)getOperand(1) 获得操作数 %A%id。我想知道 getOperand 是否适用于 %ptrA?如果是,会是getOperand(3)吗?

------------------------------------编辑-------- ------------------

所以我更改了我的代码如下:

for (Instruction &I : instructions(F)){
    if (cast<Operator>(I).getOpcode() == Instruction::GetElementPtr){
        Value* AddrPointer = cast<Value>(I);

我不断收到错误消息:

error: cannot convert ‘llvm::Value’ to ‘llvm::Value*’ in initialization
       Value* AddrPointer = cast<Value>(I);
                                         ^

我发现类型不匹配存在一些问题。

谢谢。

您的问题缺乏相当多的上下文,但我假设您正在使用代表特定 getelementptr 指令的 llvm::Instruction *。不,getOperand() 不允许您访问 %ptrA。通常,getOperand() 只允许访问指令的操作数或参数,但不允许访问其 return 值。在 IR 中,%ptrA 与其说是传统汇编中指令的操作数,不如说是指令的 return 值

您要执行的操作的语法实际上非常方便。 llvm::Instruction 对象本身代表它自己的 return 值。事实上,llvm::Instructionllvm::Value 的派生 class。您可以使用 llvm::cast,将 llvm::Value 作为模板参数,结果实际上是一个 llvm::Value *,表示 getelementptr.[=29 的 return 值=]

llvm::Instruction * instruc;

//next line assumes instruc has your getelementptr instruction

llvm::Value * returnval = llvm::cast<llvm::Value>(instruc);
//returnval now contains the result of the instruction
//you could potentially create new instructions with IRBuilder using returnval as an argument, and %ptrA would actually be passed as an operand to those instructions

此外,许多实际创建指令(例如 llvm::IRBuilder::Create* 指令)的函数甚至不 return llvm::Instruction *s 而是 llvm::Value *s .这非常方便,因为大多数情况下,如果您需要将一条指令的 return 值提供给另一条指令,您可以简单地传递您调用的任何 Create 函数的 return 值进入下一个 Create 函数,无需进行任何转换。