LLVM IR:获取 LVALUE 操作数

alg*_*rog 2 c++ llvm-ir

我有以下指示:

\n\n
%ptrA = getelementptr float, float addrspace(1)* %A, i32 %id\n
Run Code Online (Sandbox Code Playgroud)\n\n

我可以获取操作数%A%id使用getOperand(0)and getOperand(1)。我想知道是否getOperand会继续工作%ptrA?如果是的话,会是吗getOperand(3)

\n\n

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

\n\n

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

\n\n
for (Instruction &I : instructions(F)){\n    if (cast<Operator>(I).getOpcode() == Instruction::GetElementPtr){\n        Value* AddrPointer = cast<Value>(I);\n
Run Code Online (Sandbox Code Playgroud)\n\n

我不断收到错误:

\n\n
%ptrA = getelementptr float, float addrspace(1)* %A, i32 %id\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

谢谢。

\n

小智 5

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

您想要执行的操作的语法实际上非常方便。对象llvm::Instruction本身代表它自己的返回值。事实上,llvm::Instruction是 的派生类llvm::Value。您可以使用llvm::cast, withllvm::Value作为模板参数,结果实际上是 an 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
Run Code Online (Sandbox Code Playgroud)

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