Jianzhou Zhao
2010-Feb-26 14:56 UTC
[LLVMdev] a question of the simple constant propagation pass
Hi, I copied the code from LLVM2.6 below. line 81 removes 'I' from the worklist. But 'I' is already removed from the list at line 67 when it is popped from the list. Could the loop (line 73-75) insert this I into the worklist again? In the case where 'I' can be constant-folding, Line 80 says 'I' is a dead instruction. Can 'I' appears in its def-use chain? But I was probably confused. Jianzhou 57 bool ConstantPropagation::runOnFunction(Function &F) { 58 // Initialize the worklist to all of the instructions ready to process... 59 std::set<Instruction*> WorkList; 60 for(inst_iterator i = inst_begin(F), e = inst_end(F); i != e; ++i) { 61 WorkList.insert(&*i); 62 } 63 bool Changed = false; 64 65 while (!WorkList.empty()) { 66 Instruction *I = *WorkList.begin(); 67 WorkList.erase(WorkList.begin()); // Get an element from the worklist... 68 69 if (!I->use_empty()) // Don't muck with dead instructions... 70 if (Constant *C = ConstantFoldInstruction(I, F.getContext())) { 71 // Add all of the users of this instruction to the worklist, they might 72 // be constant propagatable now... 73 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); 74 UI != UE; ++UI) 75 WorkList.insert(cast<Instruction>(*UI)); 76 77 // Replace all of the uses of a variable with uses of the constant. 78 I->replaceAllUsesWith(C); 79 80 // Remove the dead instruction. 81 WorkList.erase(I); 82 I->eraseFromParent(); 83 84 // We made a change to the function... 85 Changed = true; 86 ++NumInstKilled; 87 } 88 } 89 return Changed; 90 }