- // Remove the exit branch from the loop
- BB->getInstList().erase(BI);
-
- assert(TripCount != 0 && "Trip count of 0 is impossible!");
- for (unsigned It = 1; It != TripCount; ++It) {
- char SuffixBuffer[100];
- sprintf(SuffixBuffer, ".%d", It);
- std::map<const Value*, Value*> ValueMap;
- BasicBlock *New = CloneBasicBlock(BB, ValueMap, SuffixBuffer);
-
- // Loop over all of the PHI nodes in the block, changing them to use the
- // incoming values from the previous block.
- for (unsigned i = 0, e = OrigPHINode.size(); i != e; ++i) {
- PHINode *NewPHI = cast<PHINode>(ValueMap[OrigPHINode[i]]);
- Value *InVal = NewPHI->getIncomingValueForBlock(BB);
- if (Instruction *InValI = dyn_cast<Instruction>(InVal))
- if (InValI->getParent() == BB)
- InVal = LastValueMap[InValI];
- ValueMap[OrigPHINode[i]] = InVal;
- New->getInstList().erase(NewPHI);
- }
-
- for (BasicBlock::iterator I = New->begin(), E = New->end(); I != E; ++I)
- RemapInstruction(I, ValueMap);
-
- // Now that all of the instructions are remapped, splice them into the end
- // of the NewBlock.
- NewBlock->getInstList().splice(NewBlock->end(), New->getInstList());
- delete New;
-
- // LastValue map now contains values from this iteration.
- std::swap(LastValueMap, ValueMap);
- }
-
- // If there was more than one iteration, replace any uses of values computed
- // in the loop with values computed during the last iteration of the loop.
- if (TripCount != 1) {
- std::set<User*> Users;
- for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
- Users.insert(I->use_begin(), I->use_end());
-
- // We don't want to reprocess entries with PHI nodes in them. For this
- // reason, we look at each operand of each user exactly once, performing the
- // stubstitution exactly once.
- for (std::set<User*>::iterator UI = Users.begin(), E = Users.end(); UI != E;
- ++UI) {
- Instruction *I = cast<Instruction>(*UI);
- if (I->getParent() != BB && I->getParent() != NewBlock)
- RemapInstruction(I, LastValueMap);