我有以下说明:
%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);
^
我发现存在类型不匹配的问题。
谢谢。
最佳答案
您的问题缺乏相当多的上下文,但我假设您正在使用 llvm::Instruction *
代表那个特定的getelementptr
操作说明。不,getOperand()
将不允许您访问%ptrA
。一般来说,getOperand()
只允许访问指令的操作数或参数,但不允许访问其返回值。在IR中,%ptrA
与其说是传统汇编中的指令操作数,不如说它是指令的返回值。
您想要执行的操作的语法实际上非常方便。安llvm::Instruction
对象本身代表它自己的返回值。事实上,llvm::Instruction
是 llvm::Value
的派生类。您可以使用llvm::cast
,与 llvm::Value
作为模板参数,结果实际上是 llvm::Value *
代表getelementptr
的返回值.
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*
指令)甚至不返回 llvm::Instruction *
而是llvm::Value *
s。这非常方便,因为大多数时候,如果您需要将一条指令的返回值提供给另一条指令,您只需传递任何 Create
的返回值即可。您调用下一个 Create
的函数函数,无需进行任何转换。
关于c++ - LLVM IR : Get LVALUE operand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794380/