algoProg
algoProg

Reputation: 728

LLVM IR: Get LVALUE operand

I have following instruction:

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

I can get the operands %A and %id using getOperand(0) and getOperand(1). I was wondering if getOperand will work on %ptrA? If yes, would it be getOperand(3)?

------------------------------------Edit----------------------------

So I changed my code as follows:

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

I keep getting error:

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

I see that there is some problem with type mismatch.

Thank you.

Upvotes: 2

Views: 3489

Answers (1)

acheron
acheron

Reputation: 116

Your question lacks quite a bit of context, but I will assume you're working with an llvm::Instruction * representing that particular getelementptr instruction. No, getOperand() will not allow you to access %ptrA. In general, getOperand() only allows access to the instruction's operands, or arguments, but not its return value. In IR, %ptrA is not so much an operand of the instruction like in traditional assembly, but can be thought of more like the return value of the instruction.

The syntax for what you're trying to do is actually very convenient. An llvm::Instruction object itself represents its own return value. In fact, llvm::Instruction is a derived class of llvm::Value. You can use llvm::cast, with llvm::Value as the template argument and the result will actually be an llvm::Value * which represents the return value of 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

Furthermore, many of the functions that actually create instructions (the llvm::IRBuilder::Create* instructions, for instance) don't even return llvm::Instruction *s but rather llvm::Value *s. This is very convenient, because most of the time if you need to feed the return value of an instruction into another instruction, you can simply pass the return value of whatever Create function you called into the next Create function, without needing to do any casting.

Upvotes: 5

Related Questions