0

When I insert an Instruction into a BasicBlock with the method

BasicBlock::getInstList().push_front(*Instruction); 

But when did it set the Instruction's parent to current BasicBlock?

The code is as follow and worked well. I just want to know when and where set the parent of the cloned Instruction.

Thanks.

Instruction *ori_inst = cur_inst->clone();  
//until now, the ori_inst does not have it's parent
CUR_BB->getInstList().push_front(ori_inst); 
//now, the ori_inst has CUR_BB as it's parent, why?
gre_gor
  • 6,669
  • 9
  • 47
  • 52
wangbo15
  • 191
  • 1
  • 13

1 Answers1

3

I think this is getting set in

void SymbolTableListTraits<ValueSubClass,ItemParentClass>
      ::addNodeToList(ValueSubClass *V) {

in lib/IR/SymbolTableListTraitsImpl.h

addNodeToList is invoked by ilist's insert method, which is called by push_front. Thus, whenever you add an instruction to an instruction list in a basic block, its parent is automatically set to the basic block itself.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Thanks a lot. But how did the iplist call this addNodeToList() ? In "ilist.h" it looks like "void addNodeToList(NodeTy *) {}", would you please tell me how did it call the one in lib/IR/SymbolTableListTraitsImpl.h ? Thanks – wangbo15 May 20 '15 at 09:24
  • @UDNightWish: through the traits mechanism. If you're having trouble following it through the code, I suggest just putting a breakpoint in the leaf method in gdb and then looking at the stack trace. – Eli Bendersky May 20 '15 at 11:58
  • May be I have trouble in understanding C++ templete code. Thank you for your guide ! – wangbo15 May 20 '15 at 12:38