[LLVMdev] Check the location of the definition of a Value object

John Criswell criswell at uiuc.edu
Thu Apr 8 09:25:00 PDT 2010


Zheng Wang wrote:
> Hello,
>
> How to check whether a value object is within a particular basic block or not.
>   

There is a much simpler way to do this.  Assuming that Value * pb holds 
the pointer to the value of %2, you do the following:

1) Check to see if %2 is an instruction (use dyn_cast<Instruction>(pb) 
and see if the result is NULL).  If it's not an instruction, then it's 
definitely not defined in the entry block.

2) Use the result of dyn_cast<Instruction>(pb) to get the basic block in 
which the instruction is defined.

So, you do

// Entry is a Basic Block pointer pointing to the function's entry block
if (Instruction * PBI = dyn_cast<Instruction>(pb)) {
    if (PBI->getParent() == Entry)
       // pb is an instruction defined in the entry basic block.


-- John T.

> I have the following LLVM bytecode:
>
> ------------------------
> entry:
>   ...
>   %2 = call i32 (...)* @atoi(i8*%1) nounwind
>   ...
>
> --
> bb1:
>   ..
>   %5 = icmp sgt i32 %2, %i.0
>   ...
> -----------------
>
> Here I got the corresponding pointer of the Value object of *%2*. How
> can I check whether it is defined in *entry* or not?
>
> I tried:
> ---------------------
> //Value* pb corresponding to %2
> for (BasicBlock::iterator I=entry->begin(), e=entry.end(); I != e; I++)
> {
>        if (I == pb)
>           return true;
> }
> return false;
> ----------------------
>
> However, I got  errors when compiled the code. Could anyone give some
> simple examples about this issue?
>
> Thanks.
>
> Cheers,
> Zheng
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>   




More information about the llvm-dev mailing list