- // The common case is that we have a pre-header. Generate special case code
- // that is faster if that is the case.
- //
- if (LoopPreds.size() == 1) {
- BasicBlock *Pred = LoopPreds[0];
-
- // Create a new copy of the instruction, for insertion into Pred.
- Instruction *New = Inst.clone();
- New->setName(InstName);
-
- // Insert the new node in Pred, before the terminator.
- Pred->getInstList().insert(--Pred->end(), New);
-
- // Kill the old instruction...
- Inst.replaceAllUsesWith(New);
- ++NumHoistedPH;
-
- } else {
- // No loop pre-header, insert a PHI node into header to capture all of the
- // incoming versions of the value.
- //
- PHINode *LoopVal = new PHINode(Inst.getType(), InstName+".phi");
-
- // Insert the new PHI node into the loop header...
- Header->getInstList().push_front(LoopVal);
-
- // Insert cloned versions of the instruction into all of the loop preds.
- for (unsigned i = 0, e = LoopPreds.size(); i != e; ++i) {
- BasicBlock *Pred = LoopPreds[i];
-
- // Create a new copy of the instruction, for insertion into Pred.
- Instruction *New = Inst.clone();
- New->setName(InstName);
-
- // Insert the new node in Pred, before the terminator.
- Pred->getInstList().insert(--Pred->end(), New);
-
- // Add the incoming value to the PHI node.
- LoopVal->addIncoming(New, Pred);
- }