LLVM Discussion Forums

How to get the value of a result of an instruction

Hi, experts,

As the topic says. When I tried to draw a data flow graph, I found I can’t get the left assignment of an instruction. for named value, it can be solved by getValueName(), which I spent a week trolling out the answer, but how can I get an unnamed one? For example, the %2 in
%2 = load i32, i32* %i, align 4
Is that a Value? or something else?
I can get an unnamed operand with printAsOperand(), because I can get the Value, but why don’t the authors offer a convenient interface allowing us to get the string on the left of the equality sign?

Thanks in advance.

I am not entirely sure about this, but this is what I think happens.

The string %2 is deduced at while generating IR; the in-memory data structure does not have it. So, I don’t think you will be able to get it without generating code. Depending on your needs, you can either check its uses or the pointer to that instruction can be used as an alias to the name. All uses of that value %2 will have the same pointer - the current instruction.

I am also a beginner at LLVM but have a little experience writing passes - and I was able to handle it with the help of pointers and users. It would be great if someone could confirm this.

%2 on the left-hand-side of the assignment or used as an operand should not make a difference right?
Why isn’t printAsOperand on the Value enough for you?

Because to use printAsOperand(), I need to get the Value object first, while my problem is not knowing how to get the value from a instruction.

I’ve solve the problem by using Instruction::print(), and extract the name manually, as following:
static std::string getResult(const Instruction *Node)
{
if(Node->getValueName())
return Node->getValueName()->getValue()->getName();

        std::string Str;
        raw_string_ostream OS(Str);
        Node->print(OS);
        size_t i;
        if((i = OS.str().find_first_of("=")) == string::npos)
            return string("");
        else
        {
            return string(OS.str(), 0, i);
        }
    }

An Instruction is a subclass of Value, if you have an instruction you should be able to directly access the methods from the Value class.

I->printAsOperand(…)

In addition, local value names are purely there for debugging purposes, and you should absolutely not be writing code that relies on them.

Never assume uers’ demands arbitarily. That’s the first law for a programmer.

You are not a “user”. You are a programmer learning to use a library, and you need to learn the rules of the abstraction provided by that library. Programmers who familiarize themselves with LLVM by looking at textual IR output often misunderstand the role of value names and treat them as a meaningful source of information. The names of local IR values are not stable, especially the numeric labels assigned to anonymous instructions, and should not be thought of as anything but an unreliable hint about (1) the purpose of an instruction and (2) the code that originally created it. If you need to preserve information through IR transformations, you need to be using intrinsics and/or metadata, depending on your use case.

OK, rjmccall. Let me tell you the context. my task is to derive a data flow graph from llvm IR, particularly aimming to loops where I need the value of results calculated by operators. Without those intermediate value, the data flow seems cannot be connected together. While the thing is llvm provides getOpcode() and getOperand(), but doesn’t provide getResult() or the like. Despite that I may deduce the value from an instruction’s user which is another instruction, but what if the user has 2 or more operands? Which one is the needed? I knew llvm team discourage users to use the anonymous value, but I did that out of having no ways.

In LLVM, all instructions have exactly one result (possibly an unusable result, for things like stores), and as Mehdi said, you can directly use an llvm::Instruction* as an llvm::Value*. So if you want to find all the uses of the result of an instruction inst, you can iterate over inst->uses():

  for (Use &use: inst->uses()) {
    assert(use.get() == inst);
    User *user = use.getUser();
    ...
  }

You can also walk the opposite direction by looking at all the operands of an instruction (although not all operands necessarily represent data flow, it’s a reasonable first approximation):

for (Use &use: inst->operands()) {
  assert(use.getUser() == inst);
  Value *value = use.get();
  ...
}

All of these pointers — instructions, users, and uses — will be stable in memory as long as you’re not destroying instructions, so generally you should just use them to identify instructions instead of using value names. That means you can use them as e.g. keys in a DenseMap; however, that’s not safe if you are simultaneously modifying the function, and if you need to do so you will need to protect yourself against dangling references by using ValueHandles and ValueMaps.

I tried your answer, however, it seems doesn’t work.
here are my code:

        Module * md = Bs->getModule();
        for(auto I = Bs->begin(); I != Bs->end(); ++I)  //blocks -> instructions
        {
            Instruction *Ins = dyn_cast<Instruction>(I);
            std::string tmpS;
            raw_string_ostream os(tmpS);
            Ins->printAsOperand(os, true, md);
            errs() << "joker-eph 's answer is "<<tmpS << "\n";
       }

while what printed are no more than
joker-eph 's answer is
joker-eph 's answer is

But still thanks for your reply.

Thanks for such detailed reply. It’s plausible as it provides us a way to get a stable value, but seems a bit too inconvenient, besides, how can I know which value of them got from iteration is generated by the inst? Why don’t the authors offer us a convient interface? :thinking:

If you’re iterating the uses of a value, you can check which operand of the user a particular use is with getOperandNo(). So given this IR:

%0 = alloca i64
%1 = call void @foo(i64* %0, i64* %0)

The use list of %0 will include two Uses: one with a User equal to %1 and an operand index of 0 and another with a User equal to %1 but an operand index of 1. The order of these entries is not guaranteed.

Bravo. That’s very what I need. You must be an authentic expert.
Finally, the function can be like:

static Value *getResultValue(Instruction* &inst)
        {
            Value *value = nullptr;
            for (Use &use: inst->uses())
            {
                //assert(use.get() == inst);
                User *user = use.getUser();
                unsigned n = use.getOperandNo();
                Instruction* userInst = dyn_cast<Instruction>(user);
                value = userInst->getOperand(n);
                break;
            }
            return value;
        }

Not all users are Instructions, but the bigger issue is that — like we’ve said repeatedly — that function can just be return inst;. Instruction is a subclass of Value, and the value represented by an Instruction instance is the instruction’s result.

So,the above function can be directly like

 static Value *getResultValue(Instruction* &inst)
 {
            return dyn_cast_or_null<Value>(inst);
 }

I tried it which works. Why doesn’t a document say that, making me confused for so long~

You don’t need the dyn_cast_or_null. Instruction* implicitly converts to Value* because it’s a subclass; this is standard C++ / object-oriented programming. The LLVM documentation assumes some basic familiarity with that.

You made it clear. Again, your are appreciated for answering.