Confusion with GetElementPtr and Defs/Uses


I’ve been hung up on some issues for several weeks now, and some of them seem to stem from GetElementPtr instructions.

Say we have a Definition: %29 = getelementptr inbounds [10 x i32]* @c, i32 0, i32 %28

Now, I’d like to see the uses of that definition. In my code, I do this twice, in slightly different ways. This first method works and shows instructions that use %29:

for(Value::use_iterator useIt = defInsn->use_begin(), useIt_e = defInsn->use_end();
useIt != useIt_e; useIt++){
Instruction *pUseInstr = dyn_cast(*useIt); //This is the current use of the defInsn

However, this code later on does not work and shows the def of %29 to have no uses [not the case] (pIRI here is a data structure we’ve created which keeps track of defs and useBBs. The defs part works fine.):

for (std::map<BasicBlock*, InstrRelInfo::UseInfo*>::iterator useBB_iter = pIRI->useBBs.begin();
useBB_iter != pIRI->useBBs.end(); ++useBB_iter) {
for (InstrRelInfo::UseInfo::iterator use_iter = (*useBB_iter).second->begin();
use_iter != (*useBB_iter).second->end(); ++use_iter) {
DEBUG(dbgs() << *((*use_iter).first));

As far as I can tell, this is only an issue for getelementptr instructions, so I was wondering if there’s some special thing I need to do here to handle that or what. getelementptr instructions do successfully show up in the second technique when it is the getelementptr instruction itself that has a use of some def.


There's nothing special about GEP which would make it not show its uses. My first thought is that the instruction may be replaced and you're holding onto an old pointer value. You should verify that the instruction you're caching still exists in the basic block. Check its parent to see if it is null:

  if (Inst->getParent() == 0)
          // This instruction isn't in a basic block.