[LLVMdev] Problem with getting a result of an instruction.

Nick Lewycky nicholas at mxc.ca
Mon Jul 9 02:17:12 PDT 2012


Yaroslav Markov wrote:
> Hello,
>
> I'm a newbie in LLVM. Now I'm trying to implement a pass that does some
> simple form of dynamic dataflow analysis.
>
> In my dataflow analysis I want to know if a specific variable is
> "dependent" on another one. "Dependent" means the following: if we have
> three variables in a program - a, b and c and in some places of this
> program we have the following assignments: b = a and then c = b. Then
> variable b is dependent on a and variable c is dependent on both a and
> b. Basically "dependent" means that a result in some variable is
> calculated basing on some other one.
>
> While implementing this pass I have faced the following problem - let's
> say we have the following piece of code:
>
> store i32 0, i32* %a, align 4
> %0 = load i32* %a, align 4
> store i32 %0, i32* %b, align 4
>
> This piece of code basically means an assignment b = a. My problem is
> that I need to detect what kind of virtual register we use in load
> instruction (in that example we use virtual register 0) and my pass
> cannot do it. I'm using the following piece of code in my pass:
>
> for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
> if((*I).getOpcode() == Instruction::Load){
> errs() << cast<LoadInst>(*I).getValueName() << "\n";
> }
> }
>
> The method getValueName() gives nothing as well as getName() or
> getValueID(). Because of this I'm not able to detect that b is dependent
> on a.

As for getName(), see this reply:

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

but you don't need getName. You never need getName, except for debugging.

> However if I have something like this:
>
> store i32 0, i32* %a, align 4
> %tmp = load i32* %a, align 4
> store i32 %tmp, i32* %b, align 4
>
> I that case my pass is able to tell that we are using "tmp" to perform
> this assignment.
>
> How can I resolve this issue and get a name or an ID of result?

An Instruction* is its result, the equals sign is a lie. The Value* 
serves as the ID you're looking for.

It's a common mistake to interpret the '%x = add i32 %a, %b' format as 
having two llvm values and an assignment occurring between them. In 
reality there is one llvm::Value (an Instruction) and it is the add. The 
instruction's name is 'x'. You can uniquely refer to the result of the 
add by the Instruction* given. Recall that LLVM IR is in static single 
assignment form, so there is no '%tmp' to be reassigned; it *is* the 
load instruction (well, it's the name of it).

As for the actual analysis you're trying to do, see the 
MemoryDependenceAnalysis pass, which in turn uses AliasAnalysis. You 
have many things to consider, such as the case where we call an external 
function and want to decide whether it could have modified the data 
inside pointer %a.

Nick



More information about the llvm-dev mailing list