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

xiaoyaollvm xiaoyaollvm at 126.com
Sat Apr 6 20:19:36 PDT 2013


 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);

                        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))
                          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";
                        }
                }
                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/1d9f3773/attachment.html>


More information about the llvm-dev mailing list