+
+ // Loop over all of the PHI nodes and see if there are any that we can get
+ // rid of because they merge all of the same incoming values. This can
+ // happen due to undef values coming into the PHI nodes. This process is
+ // iterative, because eliminating one PHI node can cause others to be removed.
+ bool EliminatedAPHI = true;
+ while (EliminatedAPHI) {
+ EliminatedAPHI = false;
+
+ for (std::map<BasicBlock*, std::vector<PHINode *> >::iterator I =
+ NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E; ++I) {
+ std::vector<PHINode*> &PNs = I->second;
+ for (unsigned i = 0, e = PNs.size(); i != e; ++i) {
+ if (!PNs[i]) continue;
+
+ // If this PHI node merges one value and/or undefs, get the value.
+ if (Value *V = PNs[i]->hasConstantValue(true)) {
+ if (!isa<Instruction>(V) ||
+ properlyDominates(cast<Instruction>(V), PNs[i])) {
+ if (AST && isa<PointerType>(PNs[i]->getType()))
+ AST->deleteValue(PNs[i]);
+ PNs[i]->replaceAllUsesWith(V);
+ PNs[i]->eraseFromParent();
+ PNs[i] = 0;
+ EliminatedAPHI = true;
+ continue;
+ }
+ }
+ }
+ }
+ }
+