-
- // Don't revisit blocks.
- if (VMap.count(BB))
- continue;
-
- BasicBlock *NewBB = BasicBlock::Create(BB->getContext());
- if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
-
- // It is only legal to clone a function if a block address within that
- // function is never referenced outside of the function. Given that, we
- // want to map block addresses from the old function to block addresses in
- // the clone. (This is different from the generic ValueMapper
- // implementation, which generates an invalid blockaddress when
- // cloning a function.)
- //
- // Note that we don't need to fix the mapping for unreachable blocks;
- // the default mapping there is safe.
- if (BB->hasAddressTaken()) {
- Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc),
- const_cast<BasicBlock*>(BB));
- VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB);
- }
-
- OrigBBs.insert(BB);
- VMap[BB] = NewBB;
- // Iterate over all possible successors and add them to the CloneWorklist.
- const TerminatorInst *Term = BB->getTerminator();
- for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
- BasicBlock *Succ = Term->getSuccessor(i);
- CloneWorklist.push_back(Succ);
- }
- }
-
- // Now, fill only the reachable blocks with the cloned contents
- // of the originals.
- assert(CloneWorklist.empty() && "Dirty worklist before re-use\n");
- CloneWorklist.push_back(&OldFunc->getEntryBlock());
- while (!CloneWorklist.empty()) {
- const BasicBlock *BB = CloneWorklist.back();
- CloneWorklist.pop_back();
- PFC.CloneBlock(BB, cast<BasicBlock>(VMap[BB]), CloneWorklist,
- OrigBBs);