gpt4 book ai didi

c++ - LLVM IR : Get LVALUE operand

转载 作者:行者123 更新时间:2023-11-30 02:28:23 24 4
gpt4 key购买 nike

我有以下说明:

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com