diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 31f83095c7436..40ab1f1ead05a 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2794,13 +2794,12 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, // in the predecessor blocks can be promoted as well. If not, we won't be able // to get rid of the control flow, so it's not worth promoting to select // instructions. - BasicBlock *DomBlock = nullptr; + BasicBlock *DomBlock = DomBI->getParent(); BasicBlock *IfBlock1 = PN->getIncomingBlock(0); BasicBlock *IfBlock2 = PN->getIncomingBlock(1); if (cast(IfBlock1->getTerminator())->isConditional()) { IfBlock1 = nullptr; } else { - DomBlock = *pred_begin(IfBlock1); for (BasicBlock::iterator I = IfBlock1->begin(); !I->isTerminator(); ++I) if (!AggressiveInsts.count(&*I) && !isa(I) && !isa(I)) { @@ -2814,7 +2813,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, if (cast(IfBlock2->getTerminator())->isConditional()) { IfBlock2 = nullptr; } else { - DomBlock = *pred_begin(IfBlock2); for (BasicBlock::iterator I = IfBlock2->begin(); !I->isTerminator(); ++I) if (!AggressiveInsts.count(&*I) && !isa(I) && !isa(I)) { @@ -2824,7 +2822,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, return Changed; } } - assert(DomBlock && "Failed to find root DomBlock"); // If either of the blocks has it's address taken, we can't do this fold. if ((IfBlock1 && IfBlock1->hasAddressTaken()) || @@ -2837,16 +2834,15 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, // If we can still promote the PHI nodes after this gauntlet of tests, // do all of the PHI's now. - Instruction *InsertPt = DomBlock->getTerminator(); - IRBuilder Builder(InsertPt); // Move all 'aggressive' instructions, which are defined in the // conditional parts of the if's up to the dominating block. if (IfBlock1) - hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock1); + hoistAllInstructionsInto(DomBlock, DomBI, IfBlock1); if (IfBlock2) - hoistAllInstructionsInto(DomBlock, InsertPt, IfBlock2); + hoistAllInstructionsInto(DomBlock, DomBI, IfBlock2); + IRBuilder Builder(DomBI); // Propagate fast-math-flags from phi nodes to replacement selects. IRBuilder<>::FastMathFlagGuard FMFGuard(Builder); while (PHINode *PN = dyn_cast(BB->begin())) { @@ -2857,7 +2853,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse); Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue); - Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", InsertPt); + Value *Sel = Builder.CreateSelect(IfCond, TrueVal, FalseVal, "", DomBI); PN->replaceAllUsesWith(Sel); Sel->takeName(PN); PN->eraseFromParent(); @@ -2866,8 +2862,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement // has been flattened. Change DomBlock to jump directly to our new block to // avoid other simplifycfg's kicking in on the diamond. - Instruction *OldTI = DomBlock->getTerminator(); - Builder.SetInsertPoint(OldTI); Builder.CreateBr(BB); SmallVector Updates; @@ -2877,7 +2871,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, Updates.push_back({DominatorTree::Delete, DomBlock, Successor}); } - OldTI->eraseFromParent(); + DomBI->eraseFromParent(); if (DTU) DTU->applyUpdates(Updates);