@@ -622,7 +622,7 @@ ExtractCodeRegion(const std::vector<BasicBlock*> &code) {
622
622
Function *oldFunction = header->getParent ();
623
623
624
624
// This takes place of the original loop
625
- BasicBlock *codeReplacer = new BasicBlock (" codeRepl" , oldFunction);
625
+ BasicBlock *codeReplacer = new BasicBlock (" codeRepl" , oldFunction, header );
626
626
627
627
// The new function needs a root node because other nodes can branch to the
628
628
// head of the region, but the entry node of a function cannot have preds.
@@ -657,10 +657,19 @@ ExtractCodeRegion(const std::vector<BasicBlock*> &code) {
657
657
succ_end (codeReplacer));
658
658
for (unsigned i = 0 , e = Succs.size (); i != e; ++i)
659
659
for (BasicBlock::iterator I = Succs[i]->begin ();
660
- PHINode *PN = dyn_cast<PHINode>(I); ++I)
660
+ PHINode *PN = dyn_cast<PHINode>(I); ++I) {
661
+ std::set<BasicBlock*> ProcessedPreds;
661
662
for (unsigned i = 0 , e = PN->getNumIncomingValues (); i != e; ++i)
662
663
if (BlocksToExtract.count (PN->getIncomingBlock (i)))
663
- PN->setIncomingBlock (i, codeReplacer);
664
+ if (ProcessedPreds.insert (PN->getIncomingBlock (i)).second )
665
+ PN->setIncomingBlock (i, codeReplacer);
666
+ else {
667
+ // There were multiple entries in the PHI for this block, now there
668
+ // is only one, so remove the duplicated entries.
669
+ PN->removeIncomingValue (i, false );
670
+ --i; --e;
671
+ }
672
+ }
664
673
665
674
// std::cerr << "NEW FUNCTION: " << *newFunction;
666
675
// verifyFunction(*newFunction);
0 commit comments