Chang May
Chang May

Reputation: 165

How to get the variable's name from the llvm instruction

Assume my target .bc file has two instruction,

%3 = load volatile i32* %i, align 4
%4 = load i32** %sum, align 8

Both of them are Load instruction. I would like to know how to extract the different variables %i and %sum for some conditional compare. I have tried to print something like:

errs()<< instruction->getOperand(i)->getName();   // print out the ith operand's name

But it turns out that the things returned are garbled. Hope anyone with same experience could help me.

Upvotes: 2

Views: 2082

Answers (1)

echristo
echristo

Reputation: 1727

This won't work in general - in particular either: a) release mode can avoid putting names on individual instructions, they'll just be the next number in sequence (as you can see from the load instructions above), or b) optimization passes will occasionally change the name as well.

The only way to do this is to either keep track of the variables as you emit them, or perform some analysis that tells you where you want to perform the compare.

Upvotes: 1

Related Questions