-bool DeadCodeElimination::dceInstruction(BasicBlock::InstListType &BBIL,
- BasicBlock::iterator &BBI) {
- // Look for un"used" definitions...
- if ((*BBI)->use_empty() && !(*BBI)->hasSideEffects() &&
- !isa<TerminatorInst>(*BBI)) {
- delete BBIL.remove(BBI); // Bye bye
- return true;
- }
- return false;
-}
-
-static inline bool RemoveUnusedDefs(BasicBlock::InstListType &Vals) {
- bool Changed = false;
- for (BasicBlock::InstListType::iterator DI = Vals.begin();
- DI != Vals.end(); )
- if (DeadCodeElimination::dceInstruction(Vals, DI))
- Changed = true;
- else
- ++DI;
- return Changed;
-}
-
-bool DeadInstElimination::runOnBasicBlock(BasicBlock *BB) {
- return RemoveUnusedDefs(BB->getInstList());
-}
-
-// RemoveSingularPHIs - This removes PHI nodes from basic blocks that have only
-// a single predecessor. This means that the PHI node must only have a single
-// RHS value and can be eliminated.
-//
-// This routine is very simple because we know that PHI nodes must be the first
-// things in a basic block, if they are present.
-//
-static bool RemoveSingularPHIs(BasicBlock *BB) {
- pred_iterator PI(pred_begin(BB));
- if (PI == pred_end(BB) || ++PI != pred_end(BB))
- return false; // More than one predecessor...
-
- Instruction *I = BB->front();
- if (!isa<PHINode>(I)) return false; // No PHI nodes
-
- //cerr << "Killing PHIs from " << BB;
- //cerr << "Pred #0 = " << *pred_begin(BB);
-
- //cerr << "Method == " << BB->getParent();
-
- do {
- PHINode *PN = cast<PHINode>(I);
- assert(PN->getNumOperands() == 2 && "PHI node should only have one value!");
- Value *V = PN->getOperand(0);
-
- PN->replaceAllUsesWith(V); // Replace PHI node with its single value.
- delete BB->getInstList().remove(BB->begin());
-
- I = BB->front();
- } while (isa<PHINode>(I));
-
- return true; // Yes, we nuked at least one phi node
-}
-
-static void ReplaceUsesWithConstant(Instruction *I) {
- Constant *CPV = Constant::getNullConstant(I->getType());
-
- // Make all users of this instruction reference the constant instead
- I->replaceAllUsesWith(CPV);
-}
-
-// PropogatePredecessors - This gets "Succ" ready to have the predecessors from
-// "BB". This is a little tricky because "Succ" has PHI nodes, which need to
-// have extra slots added to them to hold the merge edges from BB's
-// predecessors. This function returns true (failure) if the Succ BB already
-// has a predecessor that is a predecessor of BB.