diff --git a/llvm/include/llvm/Transforms/Scalar/JumpThreading.h b/llvm/include/llvm/Transforms/Scalar/JumpThreading.h --- a/llvm/include/llvm/Transforms/Scalar/JumpThreading.h +++ b/llvm/include/llvm/Transforms/Scalar/JumpThreading.h @@ -116,7 +116,7 @@ BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB); - bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl &PredBBs, + void ThreadEdge(BasicBlock *BB, const SmallVectorImpl &PredBBs, BasicBlock *SuccBB); bool DuplicateCondBranchOnPHIIntoPred( BasicBlock *BB, const SmallVectorImpl &PredBBs); diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -1717,8 +1717,40 @@ MostPopularDest = BB->getTerminator()-> getSuccessor(GetBestDestForJumpOnUndef(BB)); + // If threading to the same block as we come from, we would infinite loop. + if (MostPopularDest == BB) { + LLVM_DEBUG(dbgs() << " Not threading across BB '" << BB->getName() + << "' - would thread to self!\n"); + return false; + } + + // If threading this would thread across a loop header, don't thread the edge. + // See the comments above FindLoopHeaders for justifications and caveats. + if (LoopHeaders.count(BB) || LoopHeaders.count(MostPopularDest)) { + LLVM_DEBUG({ + bool BBIsHeader = LoopHeaders.count(BB); + bool SuccIsHeader = LoopHeaders.count(MostPopularDest); + dbgs() << " Not threading across " + << (BBIsHeader ? "loop header BB '" : "block BB '") + << BB->getName() << "' to dest " + << (SuccIsHeader ? "loop header BB '" : "block BB '") + << MostPopularDest->getName() + << "' - it might create an irreducible loop!\n"; + }); + return false; + } + + unsigned JumpThreadCost = + getJumpThreadDuplicationCost(BB, BB->getTerminator(), BBDupThreshold); + if (JumpThreadCost > BBDupThreshold) { + LLVM_DEBUG(dbgs() << " Not threading BB '" << BB->getName() + << "' - Cost is too high: " << JumpThreadCost << "\n"); + return false; + } + // Ok, try to thread it! - return ThreadEdge(BB, PredsToFactor, MostPopularDest); + ThreadEdge(BB, PredsToFactor, MostPopularDest); + return true; } /// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on @@ -2019,38 +2051,9 @@ /// ThreadEdge - We have decided that it is safe and profitable to factor the /// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB /// across BB. Transform the IR to reflect this change. -bool JumpThreadingPass::ThreadEdge(BasicBlock *BB, +void JumpThreadingPass::ThreadEdge(BasicBlock *BB, const SmallVectorImpl &PredBBs, BasicBlock *SuccBB) { - // If threading to the same block as we come from, we would infinite loop. - if (SuccBB == BB) { - LLVM_DEBUG(dbgs() << " Not threading across BB '" << BB->getName() - << "' - would thread to self!\n"); - return false; - } - - // If threading this would thread across a loop header, don't thread the edge. - // See the comments above FindLoopHeaders for justifications and caveats. - if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) { - LLVM_DEBUG({ - bool BBIsHeader = LoopHeaders.count(BB); - bool SuccIsHeader = LoopHeaders.count(SuccBB); - dbgs() << " Not threading across " - << (BBIsHeader ? "loop header BB '" : "block BB '") << BB->getName() - << "' to dest " << (SuccIsHeader ? "loop header BB '" : "block BB '") - << SuccBB->getName() << "' - it might create an irreducible loop!\n"; - }); - return false; - } - - unsigned JumpThreadCost = - getJumpThreadDuplicationCost(BB, BB->getTerminator(), BBDupThreshold); - if (JumpThreadCost > BBDupThreshold) { - LLVM_DEBUG(dbgs() << " Not threading BB '" << BB->getName() - << "' - Cost is too high: " << JumpThreadCost << "\n"); - return false; - } - // And finally, do it! Start by factoring the predecessors if needed. BasicBlock *PredBB; if (PredBBs.size() == 1) @@ -2064,7 +2067,6 @@ // And finally, do it! LLVM_DEBUG(dbgs() << " Threading edge from '" << PredBB->getName() << "' to '" << SuccBB->getName() - << "' with cost: " << JumpThreadCost << ", across block:\n " << *BB << "\n"); if (DTU->hasPendingDomTreeUpdates()) @@ -2125,7 +2127,6 @@ // Threaded an edge! ++NumThreads; - return true; } /// Create a new basic block that will be the predecessor of BB and successor of