Accessing Return Variable Names


I'm new to llvm development and I have a question which I think should be straight forward, but I am having trouble figuring it out. I want to be able to access the return variable name for an instruction. For some instructions I can get this value through the "getName" method. For example, with the instruction:

> %arg11 = bitcast i32* %arg1 to i8*, !dbg !42, !id !43

calling the "getName" method returns:

> arg11

However, for the instruction:

> %4 = load i32* %arg1, align 4, !dbg !57, !id !59

the "getName" method returns the empty string. How would I find the value "4" for this load instruction? Thanks in advance.

- John

Hi John,

Welcome, and this is a frequent newbie question: in LLVM names are
more for human readability of the code, they are not really used as
references internally. Instead engine operates instructions, which
could be "used by" or "user of" other instructions (see use_iterator
of Value.h). Also the Function class has an iterator of BasicBlocks,
and each BasicBlock has an iterator of Instructions. For example, you
can iterate through all instructions in the scope of your interest and
find those, whose type is ReturnInst.

Hope it helps,
- Dima.

Hi John,

%4 is not returned, because it’s a temp register value and not a named variable.
But you can implement something like the code below to get this value.
(Not the most elegant, but more or less working solution).

static std::string getName(Instruction* i)
if(i->getOpcode() == Instruction::Store)
return getName((dyn_cast(&*i))->getPointerOperand());
else if(i->getOpcode() == Instruction::Ret)
return “Return”;

std::string name;
raw_string_ostream s(name);

s << *i; // string representation of an instruction

size_t var_start = name.find(’%’); // var starts with a %
size_t var_end = name.find(" “, var_start+1); // till space
size_t var_comma = name.find(”,", var_start+1); // or comma

return var_comma<var_end?
name.substr(var_start,var_comma-var_start) :
name.substr(var_start, var_end-var_start);

Have fun

Welcome, and this is a frequent newbie question: [...]

If not already there, could you find an appropriate place to put this
in the documentation?

--Sean Silva

There is also the Instruction Namer pass that gives all anon registers names.

Not in FAQ, but mentioned here:

"One nice thing about LLVM is that the name is just a hint. For
instance, if the code above emits multiple "addtmp" variables, LLVM
will automatically provide each one with an increasing, unique numeric
suffix. Local value names for instructions are purely optional, but it
makes it much easier to read the IR dumps."

Probably could be taken here but I don't
understand where this file is hosted to send a patch commit.

- D.