-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 (!I->isPHINode()) return false; // No PHI nodes
-
- //cerr << "Killing PHIs from " << BB;
- //cerr << "Pred #0 = " << *pred_begin(BB);
-
- //cerr << "Method == " << BB->getParent();
-
- do {
- PHINode *PN = (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 (I->isPHINode());
-
- return true; // Yes, we nuked at least one phi node
-}
-
-bool opt::DoRemoveUnusedConstants(SymTabValue *S) {
- bool Changed = false;
- ConstantPool &CP = S->getConstantPool();
- for (ConstantPool::plane_iterator PI = CP.begin(); PI != CP.end(); ++PI)
- Changed |= RemoveUnusedDefs(**PI, ConstPoolDCE());
- return Changed;
-}
-
-static void ReplaceUsesWithConstant(Instruction *I) {
- // Get the method level constant pool
- ConstantPool &CP = I->getParent()->getParent()->getConstantPool();
-
- ConstPoolVal *CPV = 0;
- ConstantPool::PlaneType *P;
- if (!CP.getPlane(I->getType(), P)) { // Does plane exist?
- // Yes, is it empty?
- if (!P->empty()) CPV = P->front();
- }
-
- if (CPV == 0) { // We don't have an existing constant to reuse. Just add one.
- CPV = ConstPoolVal::getNullConstant(I->getType()); // Create a new constant
-
- // Add the new value to the constant pool...
- CP.insert(CPV);
- }
-
- // 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.
-//
-// Assumption: BB is the single predecessor of Succ.