[LLVMdev] How to get the Instruction where one function use the global variable.

Criswell, John T criswell at illinois.edu
Sun Apr 7 12:36:42 PDT 2013


Your example is a little difficult to follow due (I think) to line-wrapping in your email.  However, the problem seems to be that you have a load instruction that uses a constant expression that uses the global.

What it sounds like you want to do is to find all *transitive* uses of the global (i.e., if a ConstantExpr uses a Global, and an Instruction uses the ConstantExpr, then you want to find the Instruction, too).

In that case, you can't just find all direct uses of the global.  You have to iterate through all uses of the ConstantExpr as well (and potentially Constants and Instructions, depending on what you consider to be a "use" of the Global).

-- John T.

________________________________
From: llvmdev-bounces at cs.uiuc.edu [llvmdev-bounces at cs.uiuc.edu] on behalf of xiaoyaollvm [xiaoyaollvm at 126.com]
Sent: Saturday, April 06, 2013 10:19 PM
To: llvmdev
Subject: [LLVMdev] How to get the Instruction where one function use the global variable.

 Hi, all
I try to get the Instructions where one function use the global variable.

for (llvm::Module::global_iterator gvar_iter = M.global_begin(); gvar_iter != M.global_end(); gvar_iter++)
    {
        llvm::GlobalVariable *gvar = &*gvar_iter;
        llvm::errs() << "const global var: " << gvar->getName() << "\n";

        for ( llvm::GlobalVariable::use_iterator iter = gvar->use_begin(); iter != gvar->use_end(); iter++ )
        {
                        llvm::User *user = llvm::dyn_cast<llvm::User>(*iter);

       &nbs! p;                if ( llvm::isa<llvm::Constant>(user))
                          llvm::errs() << "isa Constant :";
                        else if (llvm::isa<llvm::Instruction>(user))
                          llvm::errs() << "isa Instruction";
                        else if (llvm::isa<llvm::Operator>(user))
            &nbs! p;           &n bsp; llvm::errs() << "isa Operator";

                        if ( llvm::isa<llvm::Instruction>(user))
                        {
                                llvm::Instruction *instr = llvm::dyn_cast<llvm::Instruction>(*iter);
                                llvm::errs() << *instr << "\n";
                  ! ;      }
                        else if ( llvm::isa<llvm::ConstantExpr>(user))
                        {
                                llvm::ConstantExpr *const_expr = llvm::dyn_cast<llvm::ConstantExpr>(*iter);
                                llvm::errs() <<*const_expr << "\n";
                 !        }
    &nbs p;           }
                llvm::errs() << "\n";
    }
by this method ,I want to get the instruction in the function,but there will be retrun constantExp.
for example:
in the IR:
@gNoise = common global %struct.rs_allocation.0 zeroinitializer, align 4
tail call void @_Z13rsClearObjectP13rs_allocation(%struct.rs_allocation.0* @gNoise) nounwind
%7 = load [1 x i32]* bitcast (%struct.rs_allocation.0* @gNoise to [1 x i32]*), align 4

but the user will return the use as follows:
1.tail call void @_Z13rsClearObjectP13rs_allocation(%struct.rs_allocation.0* @gNoise) #0
2. [1 x i32]* bitcast (%struct.rs_allocation.0* @gNoise to [1 x i32]*)
I want to get the " instruction %7 = load [1 x i32]*! bitcast (%struct.rs_allocation.0* @gNoise to [1 x i32]*), align 4",
not "[1 x i32]* bitcast (%struct.rs_allocation.0* @gNoise to [1 x i32]*)"
How could I get this?
Thanks,
Yaoxiao
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130407/dc70ded5/attachment.html>


More information about the llvm-dev mailing list