problem with visitBranchInst()

hi,

my code inherits InstVisitor class, and visitBranchInst() method.

however, i notice that inside the virtual method
visitBranchInst(BranchInst &I), on the LLVM instruction like:

br i1 %1, label %2, label %3

my code doesnt return expected info. for ex, the code

I.getCondition->getName().str()

would return empty string. and at the same time, the code

I.getSuccessor(0)->getName()

would also return emtpy string.

i am pretty confused, as i am expecting non-empty strings returned
from the above functions.
any idea on why this happens?

this is on LLVM 3.0, Ubuntu 12.04.

thanks so much,
Jun

Jun Koi wrote:

hi,

my code inherits InstVisitor class, and visitBranchInst() method.

however, i notice that inside the virtual method
visitBranchInst(BranchInst&I), on the LLVM instruction like:

br i1 %1, label %2, label %3

my code doesnt return expected info. for ex, the code

I.getCondition->getName().str()

would return empty string. and at the same time, the code

I.getSuccessor(0)->getName()

would also return emtpy string.

i am pretty confused, as i am expecting non-empty strings returned
from the above functions.
any idea on why this happens?

Value names are optional, and your instructions don't have any. Thus, getName() returns the empty string.

The "%1" etc., is being written from the AsmWriter because it needs some way to refer to the instruction when it's used, so it just numbers anonymous instructions as it goes. %1 is the second (presumably there was a %0 earlier). If the instruction was actually named '1' then the instruction would have been written 'br i1 %"1" ...'

There is no way to recreate the %number except by counting from the top as the AsmWriter does. Of course, you should never need to do this. If you want to operate on IR that has names (for instance, they make debugging your pass easier), run the instruction naming pass (opt -instnamer) over your IR first.

Finally, this topic was discussed at length on this list a few days ago. Please check the list archives before posting. See the thread starting here:

   http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-July/051489.html

Nick

Hi Jun,

my code inherits InstVisitor class, and visitBranchInst() method.

however, i notice that inside the virtual method
visitBranchInst(BranchInst &I), on the LLVM instruction like:

br i1 %1, label %2, label %3

when you see numbers %1, %2 etc like this, it means that the instruction,
basic block etc doesn't have a name. So why the numbers? These numbers
only occur in the human readable IR and are used to identify two things as
being the same, which is needed to turn the IR into bitcode. For example,
considering reading in the following IR, and turning it into bitcode, by
which I mean creating the corresponding CmpInst, BranchInst, i.e. the internal
objects you access and manipulate using the C++ API:

   %1 = icmp eq ...
   br i1 %1, ...

When the IR reader sees two occurrences of %1, it understands that the
operand of the branch is the result of the icmp instruction, and it
constructs the BranchInst object that way. Without some kind of labels
this would be impossible.

Ciao, Duncan.