+
+ // Simplify everything in the worklist until the cows come home.
+ while (!Worklist.empty()) {
+ Instruction *I = *Worklist.begin();
+ Worklist.erase(Worklist.begin());
+ Value *V = SimplifyInstruction(I, TD, DT);
+ if (!V) continue;
+
+ // This instruction simplifies! Replace it with its simplification and
+ // add all uses to the worklist, since they may now simplify.
+ I->replaceAllUsesWith(V);
+ for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
+ UI != UE; ++UI)
+ // In unreachable code an instruction can use itself, in which case
+ // don't add it to the worklist since we are about to erase it.
+ if (*UI != I) Worklist.insert(cast<Instruction>(*UI));
+ if (isInstructionTriviallyDead(I))
+ I->eraseFromParent();
+ ++NumSimplified;
+ Changed = true;
+ }
+