[LLVMdev] LLVM load instruction query

John Criswell criswell at illinois.edu
Wed Mar 6 07:56:02 PST 2013


On 3/6/13 7:53 AM, Anshul wrote:
>
>
>
> Duncan Sands <baldrick <at> free.fr> writes:
>
>> Hi Anshul,
>>
>>   > I am creating a pass that will pass loaded value by load instruction to an
>>> external function.
>>> I don't know how to do it.Please Help.
>> your question is too vague for anyone to be able to help you.  Add details,
>> for example provide the code for your pass.
>>
>> Ciao, Duncan.
>>
> Here is the code that i did.
> I tried to found error.I think whatever i am doing for getting loaded value is
> wrong.But i dont know how to correct it

There are a few things for which you should look or fix:

1) You should make sure that certain values are not NULL (namely CI).

2) Inserting a call instruction may invalidate the BasicBlock iterator 
BI.  If you want to be clever, read up on the BasicBlock::iterator and 
see under what conditions it can be invalidated.  If you're lazy like 
me, you may want to record (in a std::set or std::vector) the Load 
instructions that you wish to instrument in one loop and then use a 
second loop to add the call instructions.

3) Better yet, you should use the InstVistor class to find load 
instructions.  I believe the InstVistor's internal iterator doesn't get 
invalidated when its visitLoadInstruction() method adds instructions.  
See 
http://llvm.org/viewvc/llvm-project/safecode/branches/release_32/lib/CommonMemorySafety/InstrumentMemoryAccesses.cpp?revision=168085&view=markup 
for an example.

4) I think your runOnModule() method is returning false 
unconditionally.  It should return true if it modifies the program.

-- John T.

>
>          virtual bool runOnModule(Module &M)
>          {
>              Constant *cpProfFunc;
>              Context = &M.getContext();
>              cpProfFunc =
> M.getOrInsertFunction("_Z6cpProfi",Type::getVoidTy(*Context),
>                                                  
> PointerType::getUnqual(Type::getInt8Ty(*Context)),
>                                                  ,NULL);
>              cpProf= cast<Function>(cpProfFunc);
>
>
>              for(Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)
>              {
>              
>                      for(Function::iterator BB = F->begin(), E = F->end(); BB !=
> E; ++BB)
>                      {
>                          anshul_insert::runOnBasicBlock(BB);
>                      }
>               }
>
>              return false;
>          }
>          virtual bool runOnBasicBlock(Function::iterator &BB)
>          {
>              for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;
> ++BI)
>              {
>                  
>                      if(isa<LoadInst>(&(*BI)) )
>                      {
>                             
>                              std::vector<Value*> a1(1);
>                              LoadInst *CI = dyn_cast<LoadInst>(BI);
>                              
> a1[0]=ConstantInt::get(Type::getInt32Ty(*Context),CI->getPointerOperand());
>                             
>                           CallInst* newInst = CallInst::Create(cpProf,a1,"");
>                           BB->getInstList().insert((Instruction*)CI, newInst);
>                          
>                                                  
>                      }
>                     
>              }
>              return true;
>          }
>      };
> }
>
> _______________________________________________
> 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