Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
35271 views
//===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// BreakCriticalEdges pass - Break all of the critical edges in the CFG by9// inserting a dummy basic block. This pass may be "required" by passes that10// cannot deal with critical edges. For this usage, the structure type is11// forward declared. This pass obviously invalidates the CFG, but can update12// dominator trees.13//14//===----------------------------------------------------------------------===//1516#include "llvm/Transforms/Utils/BreakCriticalEdges.h"17#include "llvm/ADT/SetVector.h"18#include "llvm/ADT/SmallVector.h"19#include "llvm/ADT/Statistic.h"20#include "llvm/Analysis/BlockFrequencyInfo.h"21#include "llvm/Analysis/BranchProbabilityInfo.h"22#include "llvm/Analysis/CFG.h"23#include "llvm/Analysis/LoopInfo.h"24#include "llvm/Analysis/MemorySSAUpdater.h"25#include "llvm/Analysis/PostDominators.h"26#include "llvm/IR/CFG.h"27#include "llvm/IR/Dominators.h"28#include "llvm/IR/Instructions.h"29#include "llvm/InitializePasses.h"30#include "llvm/Transforms/Utils.h"31#include "llvm/Transforms/Utils/BasicBlockUtils.h"32#include "llvm/Transforms/Utils/Cloning.h"33#include "llvm/Transforms/Utils/ValueMapper.h"34using namespace llvm;3536#define DEBUG_TYPE "break-crit-edges"3738STATISTIC(NumBroken, "Number of blocks inserted");3940namespace {41struct BreakCriticalEdges : public FunctionPass {42static char ID; // Pass identification, replacement for typeid43BreakCriticalEdges() : FunctionPass(ID) {44initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());45}4647bool runOnFunction(Function &F) override {48auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();49auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;5051auto *PDTWP = getAnalysisIfAvailable<PostDominatorTreeWrapperPass>();52auto *PDT = PDTWP ? &PDTWP->getPostDomTree() : nullptr;5354auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();55auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;56unsigned N =57SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI, nullptr, PDT));58NumBroken += N;59return N > 0;60}6162void getAnalysisUsage(AnalysisUsage &AU) const override {63AU.addPreserved<DominatorTreeWrapperPass>();64AU.addPreserved<LoopInfoWrapperPass>();6566// No loop canonicalization guarantees are broken by this pass.67AU.addPreservedID(LoopSimplifyID);68}69};70}7172char BreakCriticalEdges::ID = 0;73INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",74"Break critical edges in CFG", false, false)7576// Publicly exposed interface to pass...77char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;78FunctionPass *llvm::createBreakCriticalEdgesPass() {79return new BreakCriticalEdges();80}8182PreservedAnalyses BreakCriticalEdgesPass::run(Function &F,83FunctionAnalysisManager &AM) {84auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);85auto *LI = AM.getCachedResult<LoopAnalysis>(F);86unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));87NumBroken += N;88if (N == 0)89return PreservedAnalyses::all();90PreservedAnalyses PA;91PA.preserve<DominatorTreeAnalysis>();92PA.preserve<LoopAnalysis>();93return PA;94}9596//===----------------------------------------------------------------------===//97// Implementation of the external critical edge manipulation functions98//===----------------------------------------------------------------------===//99100BasicBlock *llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,101const CriticalEdgeSplittingOptions &Options,102const Twine &BBName) {103if (!isCriticalEdge(TI, SuccNum, Options.MergeIdenticalEdges))104return nullptr;105106return SplitKnownCriticalEdge(TI, SuccNum, Options, BBName);107}108109BasicBlock *110llvm::SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum,111const CriticalEdgeSplittingOptions &Options,112const Twine &BBName) {113assert(!isa<IndirectBrInst>(TI) &&114"Cannot split critical edge from IndirectBrInst");115116BasicBlock *TIBB = TI->getParent();117BasicBlock *DestBB = TI->getSuccessor(SuccNum);118119// Splitting the critical edge to a pad block is non-trivial. Don't do120// it in this generic function.121if (DestBB->isEHPad()) return nullptr;122123if (Options.IgnoreUnreachableDests &&124isa<UnreachableInst>(DestBB->getFirstNonPHIOrDbgOrLifetime()))125return nullptr;126127auto *LI = Options.LI;128SmallVector<BasicBlock *, 4> LoopPreds;129// Check if extra modifications will be required to preserve loop-simplify130// form after splitting. If it would require splitting blocks with IndirectBr131// terminators, bail out if preserving loop-simplify form is requested.132if (LI) {133if (Loop *TIL = LI->getLoopFor(TIBB)) {134135// The only way that we can break LoopSimplify form by splitting a136// critical edge is if after the split there exists some edge from TIL to137// DestBB *and* the only edge into DestBB from outside of TIL is that of138// NewBB. If the first isn't true, then LoopSimplify still holds, NewBB139// is the new exit block and it has no non-loop predecessors. If the140// second isn't true, then DestBB was not in LoopSimplify form prior to141// the split as it had a non-loop predecessor. In both of these cases,142// the predecessor must be directly in TIL, not in a subloop, or again143// LoopSimplify doesn't hold.144for (BasicBlock *P : predecessors(DestBB)) {145if (P == TIBB)146continue; // The new block is known.147if (LI->getLoopFor(P) != TIL) {148// No need to re-simplify, it wasn't to start with.149LoopPreds.clear();150break;151}152LoopPreds.push_back(P);153}154// Loop-simplify form can be preserved, if we can split all in-loop155// predecessors.156if (any_of(LoopPreds, [](BasicBlock *Pred) {157return isa<IndirectBrInst>(Pred->getTerminator());158})) {159if (Options.PreserveLoopSimplify)160return nullptr;161LoopPreds.clear();162}163}164}165166// Create a new basic block, linking it into the CFG.167BasicBlock *NewBB = nullptr;168if (BBName.str() != "")169NewBB = BasicBlock::Create(TI->getContext(), BBName);170else171NewBB = BasicBlock::Create(TI->getContext(), TIBB->getName() + "." +172DestBB->getName() +173"_crit_edge");174// Create our unconditional branch.175BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);176NewBI->setDebugLoc(TI->getDebugLoc());177178// Insert the block into the function... right after the block TI lives in.179Function &F = *TIBB->getParent();180Function::iterator FBBI = TIBB->getIterator();181F.insert(++FBBI, NewBB);182183// Branch to the new block, breaking the edge.184TI->setSuccessor(SuccNum, NewBB);185186// If there are any PHI nodes in DestBB, we need to update them so that they187// merge incoming values from NewBB instead of from TIBB.188{189unsigned BBIdx = 0;190for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {191// We no longer enter through TIBB, now we come in through NewBB.192// Revector exactly one entry in the PHI node that used to come from193// TIBB to come from NewBB.194PHINode *PN = cast<PHINode>(I);195196// Reuse the previous value of BBIdx if it lines up. In cases where we197// have multiple phi nodes with *lots* of predecessors, this is a speed198// win because we don't have to scan the PHI looking for TIBB. This199// happens because the BB list of PHI nodes are usually in the same200// order.201if (PN->getIncomingBlock(BBIdx) != TIBB)202BBIdx = PN->getBasicBlockIndex(TIBB);203PN->setIncomingBlock(BBIdx, NewBB);204}205}206207// If there are any other edges from TIBB to DestBB, update those to go208// through the split block, making those edges non-critical as well (and209// reducing the number of phi entries in the DestBB if relevant).210if (Options.MergeIdenticalEdges) {211for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {212if (TI->getSuccessor(i) != DestBB) continue;213214// Remove an entry for TIBB from DestBB phi nodes.215DestBB->removePredecessor(TIBB, Options.KeepOneInputPHIs);216217// We found another edge to DestBB, go to NewBB instead.218TI->setSuccessor(i, NewBB);219}220}221222// If we have nothing to update, just return.223auto *DT = Options.DT;224auto *PDT = Options.PDT;225auto *MSSAU = Options.MSSAU;226if (MSSAU)227MSSAU->wireOldPredecessorsToNewImmediatePredecessor(228DestBB, NewBB, {TIBB}, Options.MergeIdenticalEdges);229230if (!DT && !PDT && !LI)231return NewBB;232233if (DT || PDT) {234// Update the DominatorTree.235// ---> NewBB -----\236// / V237// TIBB -------\\------> DestBB238//239// First, inform the DT about the new path from TIBB to DestBB via NewBB,240// then delete the old edge from TIBB to DestBB. By doing this in that order241// DestBB stays reachable in the DT the whole time and its subtree doesn't242// get disconnected.243SmallVector<DominatorTree::UpdateType, 3> Updates;244Updates.push_back({DominatorTree::Insert, TIBB, NewBB});245Updates.push_back({DominatorTree::Insert, NewBB, DestBB});246if (!llvm::is_contained(successors(TIBB), DestBB))247Updates.push_back({DominatorTree::Delete, TIBB, DestBB});248249if (DT)250DT->applyUpdates(Updates);251if (PDT)252PDT->applyUpdates(Updates);253}254255// Update LoopInfo if it is around.256if (LI) {257if (Loop *TIL = LI->getLoopFor(TIBB)) {258// If one or the other blocks were not in a loop, the new block is not259// either, and thus LI doesn't need to be updated.260if (Loop *DestLoop = LI->getLoopFor(DestBB)) {261if (TIL == DestLoop) {262// Both in the same loop, the NewBB joins loop.263DestLoop->addBasicBlockToLoop(NewBB, *LI);264} else if (TIL->contains(DestLoop)) {265// Edge from an outer loop to an inner loop. Add to the outer loop.266TIL->addBasicBlockToLoop(NewBB, *LI);267} else if (DestLoop->contains(TIL)) {268// Edge from an inner loop to an outer loop. Add to the outer loop.269DestLoop->addBasicBlockToLoop(NewBB, *LI);270} else {271// Edge from two loops with no containment relation. Because these272// are natural loops, we know that the destination block must be the273// header of its loop (adding a branch into a loop elsewhere would274// create an irreducible loop).275assert(DestLoop->getHeader() == DestBB &&276"Should not create irreducible loops!");277if (Loop *P = DestLoop->getParentLoop())278P->addBasicBlockToLoop(NewBB, *LI);279}280}281282// If TIBB is in a loop and DestBB is outside of that loop, we may need283// to update LoopSimplify form and LCSSA form.284if (!TIL->contains(DestBB)) {285assert(!TIL->contains(NewBB) &&286"Split point for loop exit is contained in loop!");287288// Update LCSSA form in the newly created exit block.289if (Options.PreserveLCSSA) {290createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);291}292293if (!LoopPreds.empty()) {294assert(!DestBB->isEHPad() && "We don't split edges to EH pads!");295BasicBlock *NewExitBB = SplitBlockPredecessors(296DestBB, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);297if (Options.PreserveLCSSA)298createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB);299}300}301}302}303304return NewBB;305}306307// Return the unique indirectbr predecessor of a block. This may return null308// even if such a predecessor exists, if it's not useful for splitting.309// If a predecessor is found, OtherPreds will contain all other (non-indirectbr)310// predecessors of BB.311static BasicBlock *312findIBRPredecessor(BasicBlock *BB, SmallVectorImpl<BasicBlock *> &OtherPreds) {313// Verify we have exactly one IBR predecessor.314// Conservatively bail out if one of the other predecessors is not a "regular"315// terminator (that is, not a switch or a br).316BasicBlock *IBB = nullptr;317for (BasicBlock *PredBB : predecessors(BB)) {318Instruction *PredTerm = PredBB->getTerminator();319switch (PredTerm->getOpcode()) {320case Instruction::IndirectBr:321if (IBB)322return nullptr;323IBB = PredBB;324break;325case Instruction::Br:326case Instruction::Switch:327OtherPreds.push_back(PredBB);328continue;329default:330return nullptr;331}332}333334return IBB;335}336337bool llvm::SplitIndirectBrCriticalEdges(Function &F,338bool IgnoreBlocksWithoutPHI,339BranchProbabilityInfo *BPI,340BlockFrequencyInfo *BFI) {341// Check whether the function has any indirectbrs, and collect which blocks342// they may jump to. Since most functions don't have indirect branches,343// this lowers the common case's overhead to O(Blocks) instead of O(Edges).344SmallSetVector<BasicBlock *, 16> Targets;345for (auto &BB : F) {346if (isa<IndirectBrInst>(BB.getTerminator()))347for (BasicBlock *Succ : successors(&BB))348Targets.insert(Succ);349}350351if (Targets.empty())352return false;353354bool ShouldUpdateAnalysis = BPI && BFI;355bool Changed = false;356for (BasicBlock *Target : Targets) {357if (IgnoreBlocksWithoutPHI && Target->phis().empty())358continue;359360SmallVector<BasicBlock *, 16> OtherPreds;361BasicBlock *IBRPred = findIBRPredecessor(Target, OtherPreds);362// If we did not found an indirectbr, or the indirectbr is the only363// incoming edge, this isn't the kind of edge we're looking for.364if (!IBRPred || OtherPreds.empty())365continue;366367// Don't even think about ehpads/landingpads.368Instruction *FirstNonPHI = Target->getFirstNonPHI();369if (FirstNonPHI->isEHPad() || Target->isLandingPad())370continue;371372// Remember edge probabilities if needed.373SmallVector<BranchProbability, 4> EdgeProbabilities;374if (ShouldUpdateAnalysis) {375EdgeProbabilities.reserve(Target->getTerminator()->getNumSuccessors());376for (unsigned I = 0, E = Target->getTerminator()->getNumSuccessors();377I < E; ++I)378EdgeProbabilities.emplace_back(BPI->getEdgeProbability(Target, I));379BPI->eraseBlock(Target);380}381382BasicBlock *BodyBlock = Target->splitBasicBlock(FirstNonPHI, ".split");383if (ShouldUpdateAnalysis) {384// Copy the BFI/BPI from Target to BodyBlock.385BPI->setEdgeProbability(BodyBlock, EdgeProbabilities);386BFI->setBlockFreq(BodyBlock, BFI->getBlockFreq(Target));387}388// It's possible Target was its own successor through an indirectbr.389// In this case, the indirectbr now comes from BodyBlock.390if (IBRPred == Target)391IBRPred = BodyBlock;392393// At this point Target only has PHIs, and BodyBlock has the rest of the394// block's body. Create a copy of Target that will be used by the "direct"395// preds.396ValueToValueMapTy VMap;397BasicBlock *DirectSucc = CloneBasicBlock(Target, VMap, ".clone", &F);398399BlockFrequency BlockFreqForDirectSucc;400for (BasicBlock *Pred : OtherPreds) {401// If the target is a loop to itself, then the terminator of the split402// block (BodyBlock) needs to be updated.403BasicBlock *Src = Pred != Target ? Pred : BodyBlock;404Src->getTerminator()->replaceUsesOfWith(Target, DirectSucc);405if (ShouldUpdateAnalysis)406BlockFreqForDirectSucc += BFI->getBlockFreq(Src) *407BPI->getEdgeProbability(Src, DirectSucc);408}409if (ShouldUpdateAnalysis) {410BFI->setBlockFreq(DirectSucc, BlockFreqForDirectSucc);411BlockFrequency NewBlockFreqForTarget =412BFI->getBlockFreq(Target) - BlockFreqForDirectSucc;413BFI->setBlockFreq(Target, NewBlockFreqForTarget);414}415416// Ok, now fix up the PHIs. We know the two blocks only have PHIs, and that417// they are clones, so the number of PHIs are the same.418// (a) Remove the edge coming from IBRPred from the "Direct" PHI419// (b) Leave that as the only edge in the "Indirect" PHI.420// (c) Merge the two in the body block.421BasicBlock::iterator Indirect = Target->begin(),422End = Target->getFirstNonPHIIt();423BasicBlock::iterator Direct = DirectSucc->begin();424BasicBlock::iterator MergeInsert = BodyBlock->getFirstInsertionPt();425426assert(&*End == Target->getTerminator() &&427"Block was expected to only contain PHIs");428429while (Indirect != End) {430PHINode *DirPHI = cast<PHINode>(Direct);431PHINode *IndPHI = cast<PHINode>(Indirect);432BasicBlock::iterator InsertPt = Indirect;433434// Now, clean up - the direct block shouldn't get the indirect value,435// and vice versa.436DirPHI->removeIncomingValue(IBRPred);437Direct++;438439// Advance the pointer here, to avoid invalidation issues when the old440// PHI is erased.441Indirect++;442443PHINode *NewIndPHI = PHINode::Create(IndPHI->getType(), 1, "ind", InsertPt);444NewIndPHI->addIncoming(IndPHI->getIncomingValueForBlock(IBRPred),445IBRPred);446447// Create a PHI in the body block, to merge the direct and indirect448// predecessors.449PHINode *MergePHI = PHINode::Create(IndPHI->getType(), 2, "merge");450MergePHI->insertBefore(MergeInsert);451MergePHI->addIncoming(NewIndPHI, Target);452MergePHI->addIncoming(DirPHI, DirectSucc);453454IndPHI->replaceAllUsesWith(MergePHI);455IndPHI->eraseFromParent();456}457458Changed = true;459}460461return Changed;462}463464465