Index: llvm/lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- llvm/lib/CodeGen/CodeGenPrepare.cpp +++ llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -304,7 +304,7 @@ const TargetTransformInfo *TTI = nullptr; const BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr; const TargetLibraryInfo *TLInfo = nullptr; - const LoopInfo *LI = nullptr; + LoopInfo *LI = nullptr; std::unique_ptr BFI; std::unique_ptr BPI; ProfileSummaryInfo *PSI = nullptr; @@ -1105,6 +1105,7 @@ // The PHIs are now updated, change everything that refers to BB to use // DestBB and remove BB. BB->replaceAllUsesWith(DestBB); + LI->removeBlock(BB); BB->eraseFromParent(); ++NumBlocksElim; @@ -2135,6 +2136,7 @@ /// /// If the transform is performed, return true and set ModifiedDT to true. static bool despeculateCountZeros(IntrinsicInst *CountZeros, + LoopInfo &LI, const TargetLowering *TLI, const DataLayout *DL, ModifyDT &ModifiedDT, SmallSet &FreshBBs, @@ -2174,6 +2176,13 @@ if (IsHugeFunc) FreshBBs.insert(EndBlock); + // Update the LoopInfo. The new blocks are in the same loop as the start + // block. + if (Loop *L = LI.getLoopFor(StartBlock)) { + L->addBasicBlockToLoop(CallBlock, LI); + L->addBasicBlockToLoop(EndBlock, LI); + } + // Set up a builder to create a compare, conditional branch, and PHI. IRBuilder<> Builder(CountZeros->getContext()); Builder.SetInsertPoint(StartBlock->getTerminator()); @@ -2348,7 +2357,7 @@ case Intrinsic::cttz: case Intrinsic::ctlz: // If counting zeros is expensive, try to avoid it. - return despeculateCountZeros(II, TLI, DL, ModifiedDT, FreshBBs, + return despeculateCountZeros(II, *LI, TLI, DL, ModifiedDT, FreshBBs, IsHugeFunc); case Intrinsic::fshl: case Intrinsic::fshr: @@ -2526,8 +2535,10 @@ } // If we eliminated all predecessors of the block, delete the block now. - if (Changed && !BB->hasAddressTaken() && pred_empty(BB)) + if (Changed && !BB->hasAddressTaken() && pred_empty(BB)) { + LI->removeBlock(BB); BB->eraseFromParent(); + } return Changed; } @@ -6935,8 +6946,10 @@ BasicBlock *EndBlock = StartBlock->splitBasicBlock(SplitPt, "select.end"); if (IsHugeFunc) FreshBBs.insert(EndBlock); + Loop *L = LI->getLoopFor(StartBlock); + if (L) + L->addBasicBlockToLoop(EndBlock, *LI); BFI->setBlockFreq(EndBlock, BFI->getBlockFreq(StartBlock).getFrequency()); - // Delete the unconditional branch that was just created by the split. StartBlock->getTerminator()->eraseFromParent(); @@ -6957,6 +6970,8 @@ TrueBranch = BranchInst::Create(EndBlock, TrueBlock); if (IsHugeFunc) FreshBBs.insert(TrueBlock); + if (L) + L->addBasicBlockToLoop(TrueBlock, *LI); TrueBranch->setDebugLoc(SI->getDebugLoc()); } auto *TrueInst = cast(SI->getTrueValue()); @@ -6968,6 +6983,8 @@ EndBlock->getParent(), EndBlock); if (IsHugeFunc) FreshBBs.insert(FalseBlock); + if (L) + L->addBasicBlockToLoop(FalseBlock, *LI); FalseBranch = BranchInst::Create(EndBlock, FalseBlock); FalseBranch->setDebugLoc(SI->getDebugLoc()); } @@ -6986,6 +7003,8 @@ EndBlock->getParent(), EndBlock); if (IsHugeFunc) FreshBBs.insert(FalseBlock); + if (L) + L->addBasicBlockToLoop(FalseBlock, *LI); auto *FalseBranch = BranchInst::Create(EndBlock, FalseBlock); FalseBranch->setDebugLoc(SI->getDebugLoc()); } @@ -8464,6 +8483,8 @@ BB.getParent(), BB.getNextNode()); if (IsHugeFunc) FreshBBs.insert(TmpBB); + if (Loop *L = LI->getLoopFor(&BB)) + L->addBasicBlockToLoop(TmpBB, *LI); // Update original basic block by using the first condition directly by the // branch instruction and removing the no longer needed and/or instruction.