How to get the destination in a LoadInst

Hi all,
I have %5 = load i32, i32* %3, align 4 LoadInst and I would like to get reference to the destination ( in this case %5 ). How can I do that?


The LoadInst is %5. I'm not sure what you mean by reference though.

I would like to get the ‘5’. I would like to know where the loaded value is stored. I don’t know which method I should use of the LoadInst class.

Hope this clarifies


Take a look at the users of the load instruction:

Is this what you are looking for?


Thanks Florian,
I’m not sure yet but tomorrow I’ll try. At the moment I don’t see why I should iterate on a list just for getting the 5 out that LoadInst CTI on but I’m.sure I’m missing something :slight_smile:

Thanks a lot

Thanks Florian,
I'm not sure yet but tomorrow I'll try. At the moment I don't see why I should iterate on a list just for getting the 5 out that LoadInst CTI on but I'm.sure I'm missing something :slight_smile:

In LLVM an instruction and its result are the same thing. Instruction
is a subclass of Value. LoadInst is %5.

I suspect you have some confusion as to how LLVM works.

In LLVM terms, an Instruction is essentially an opcode, some opcode-specific flags, and a list of Value operands that it uses. Instructions are themselves Values, so that means that to use the result of an instruction, you pass the Instruction itself to the method that accepts a Value. There is no “destination” of an Instruction in structural terms.

In the textual format of LLVM, Instructions are represented by printing out % = , and subsequent uses of the instruction are referred to in the % format. Names of instructions (and a few other values) must be unique, with the exception of the empty string. The % representation for an empty name string is replaced with a unique autoincrementing integer that is not readily accessible via the APIs (you need to go through the ModuleSlotTracker to get to it). If you’re really trying to get that exact string for an instruction, you are generally doing something wrong and there is an easier way to go about whatever it is you want to do.

Once you have a 'reference to %5' what do you intend to do with it?
This might help to understand question.


Thanks Joshua and Michael,
Just to to clarify, I’m experimenting with the Interpreter class and observing the instructions that are executed by it. Just for becoming more confident with LLVM in general I’d like for each instruction to access to the various parts of it. In this instance I would like to access to the %Name that is shown in the textual representation. When I call Instruction.dump() all is printed correctly so I thought there was a way to get the %Name ( in my case 5). So my final goal is to obtain it. I understand that is dinamically generated based on. The execution but I think that in my case I could access to it. Am I wrong? I hope now all is more clear :slight_smile:

Thanks again

As Joshua and others already explained, the instruction and the value
it computes are both the same object: the 'Instruction' you are
calling dump() on. For instance, to enumerate where this values is
used, try:

    for (auto &U : Instruction.uses()) { ... }

If you want the name of the value/instruction as string, as it appears
on stdout when calling Instruction::dump()?

Short version: Use Instruction.getName()

Long version: In you example, getName() will return the empty string,
i.e. it has no name. There is some disambiguation going on to print
unique names. You can get the uniqued name using
Instruction.printAsOperand(). You will have to pass a string stream
(such as llvm::raw_string_ostream) and extract the string using



Thanks Michael,
Indeed as you said the Instruction.getName() returns an empty string. I think I should have mentioned that I’m trying to print it from the visitLoadInst() method of my class that extends Interpreter.

Anyway I’ll try as you suggested and get back to you.

Thanks again

"Doerfert, Johannes Rudolf via llvm-dev" <>

The LoadInst is %5. I'm not sure what you mean by reference though.

This is a common misunderstanding, one that I think is encouraged by the
textual form of IR. "%5 = load" makes it looks like %5 is a destination
operand in LoadInst. LLVM IR looks like assembly and in (most) assembly
syntax, destinations are instruction operands.

Something like "%5 -> load" might have made things more clear, but of
course it's too late for that. :slight_smile:


Honestly a bit yes but now all should be fine :slight_smile: