c++ - LLVM IR : Get LVALUE operand

标签 c++ llvm-ir

我有以下说明:

%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::Instructionllvm::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/

相关文章:

C++自动更新程序

c++ - 检查线是否穿过二维 map 上的墙

c++ - 程序在代码块中调试时收到信号 SIGSEGV,段错误

c++ - 如何使用 llvm 库

c++ - 如何访问与 C++ 中的线程关联的机器状态?

llvm - 如何生成供 emscripten 使用的 LLVM 位码?

architecture - NVPTX 通用内存空间在架构中的位置

c++ - 在llvm中获取ICmpInst和FCmpInst的LHS和RHS?

LLVM IR getelementptr LLVM C API 等效项

c++ - 如何使用 Attribute 作为 TinyXML2(C++) 的搜索关键字