Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
35271 views
//===- BasicBlockUtils.cpp - BasicBlock Utilities --------------------------==//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// This family of functions perform manipulations on basic blocks, and9// instructions contained within basic blocks.10//11//===----------------------------------------------------------------------===//1213#include "llvm/Transforms/Utils/BasicBlockUtils.h"14#include "llvm/ADT/ArrayRef.h"15#include "llvm/ADT/SmallPtrSet.h"16#include "llvm/ADT/SmallVector.h"17#include "llvm/ADT/Twine.h"18#include "llvm/Analysis/CFG.h"19#include "llvm/Analysis/DomTreeUpdater.h"20#include "llvm/Analysis/LoopInfo.h"21#include "llvm/Analysis/MemoryDependenceAnalysis.h"22#include "llvm/Analysis/MemorySSAUpdater.h"23#include "llvm/IR/BasicBlock.h"24#include "llvm/IR/CFG.h"25#include "llvm/IR/Constants.h"26#include "llvm/IR/DebugInfo.h"27#include "llvm/IR/DebugInfoMetadata.h"28#include "llvm/IR/Dominators.h"29#include "llvm/IR/Function.h"30#include "llvm/IR/InstrTypes.h"31#include "llvm/IR/Instruction.h"32#include "llvm/IR/Instructions.h"33#include "llvm/IR/IntrinsicInst.h"34#include "llvm/IR/IRBuilder.h"35#include "llvm/IR/LLVMContext.h"36#include "llvm/IR/Type.h"37#include "llvm/IR/User.h"38#include "llvm/IR/Value.h"39#include "llvm/IR/ValueHandle.h"40#include "llvm/Support/Casting.h"41#include "llvm/Support/CommandLine.h"42#include "llvm/Support/Debug.h"43#include "llvm/Support/raw_ostream.h"44#include "llvm/Transforms/Utils/Local.h"45#include <cassert>46#include <cstdint>47#include <string>48#include <utility>49#include <vector>5051using namespace llvm;5253#define DEBUG_TYPE "basicblock-utils"5455static cl::opt<unsigned> MaxDeoptOrUnreachableSuccessorCheckDepth(56"max-deopt-or-unreachable-succ-check-depth", cl::init(8), cl::Hidden,57cl::desc("Set the maximum path length when checking whether a basic block "58"is followed by a block that either has a terminating "59"deoptimizing call or is terminated with an unreachable"));6061void llvm::detachDeadBlocks(62ArrayRef<BasicBlock *> BBs,63SmallVectorImpl<DominatorTree::UpdateType> *Updates,64bool KeepOneInputPHIs) {65for (auto *BB : BBs) {66// Loop through all of our successors and make sure they know that one67// of their predecessors is going away.68SmallPtrSet<BasicBlock *, 4> UniqueSuccessors;69for (BasicBlock *Succ : successors(BB)) {70Succ->removePredecessor(BB, KeepOneInputPHIs);71if (Updates && UniqueSuccessors.insert(Succ).second)72Updates->push_back({DominatorTree::Delete, BB, Succ});73}7475// Zap all the instructions in the block.76while (!BB->empty()) {77Instruction &I = BB->back();78// If this instruction is used, replace uses with an arbitrary value.79// Because control flow can't get here, we don't care what we replace the80// value with. Note that since this block is unreachable, and all values81// contained within it must dominate their uses, that all uses will82// eventually be removed (they are themselves dead).83if (!I.use_empty())84I.replaceAllUsesWith(PoisonValue::get(I.getType()));85BB->back().eraseFromParent();86}87new UnreachableInst(BB->getContext(), BB);88assert(BB->size() == 1 &&89isa<UnreachableInst>(BB->getTerminator()) &&90"The successor list of BB isn't empty before "91"applying corresponding DTU updates.");92}93}9495void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU,96bool KeepOneInputPHIs) {97DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs);98}99100void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU,101bool KeepOneInputPHIs) {102#ifndef NDEBUG103// Make sure that all predecessors of each dead block is also dead.104SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end());105assert(Dead.size() == BBs.size() && "Duplicating blocks?");106for (auto *BB : Dead)107for (BasicBlock *Pred : predecessors(BB))108assert(Dead.count(Pred) && "All predecessors must be dead!");109#endif110111SmallVector<DominatorTree::UpdateType, 4> Updates;112detachDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs);113114if (DTU)115DTU->applyUpdates(Updates);116117for (BasicBlock *BB : BBs)118if (DTU)119DTU->deleteBB(BB);120else121BB->eraseFromParent();122}123124bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU,125bool KeepOneInputPHIs) {126df_iterator_default_set<BasicBlock*> Reachable;127128// Mark all reachable blocks.129for (BasicBlock *BB : depth_first_ext(&F, Reachable))130(void)BB/* Mark all reachable blocks */;131132// Collect all dead blocks.133std::vector<BasicBlock*> DeadBlocks;134for (BasicBlock &BB : F)135if (!Reachable.count(&BB))136DeadBlocks.push_back(&BB);137138// Delete the dead blocks.139DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs);140141return !DeadBlocks.empty();142}143144bool llvm::FoldSingleEntryPHINodes(BasicBlock *BB,145MemoryDependenceResults *MemDep) {146if (!isa<PHINode>(BB->begin()))147return false;148149while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {150if (PN->getIncomingValue(0) != PN)151PN->replaceAllUsesWith(PN->getIncomingValue(0));152else153PN->replaceAllUsesWith(PoisonValue::get(PN->getType()));154155if (MemDep)156MemDep->removeInstruction(PN); // Memdep updates AA itself.157158PN->eraseFromParent();159}160return true;161}162163bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI,164MemorySSAUpdater *MSSAU) {165// Recursively deleting a PHI may cause multiple PHIs to be deleted166// or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.167SmallVector<WeakTrackingVH, 8> PHIs;168for (PHINode &PN : BB->phis())169PHIs.push_back(&PN);170171bool Changed = false;172for (unsigned i = 0, e = PHIs.size(); i != e; ++i)173if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))174Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU);175176return Changed;177}178179bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,180LoopInfo *LI, MemorySSAUpdater *MSSAU,181MemoryDependenceResults *MemDep,182bool PredecessorWithTwoSuccessors,183DominatorTree *DT) {184if (BB->hasAddressTaken())185return false;186187// Can't merge if there are multiple predecessors, or no predecessors.188BasicBlock *PredBB = BB->getUniquePredecessor();189if (!PredBB) return false;190191// Don't break self-loops.192if (PredBB == BB) return false;193194// Don't break unwinding instructions or terminators with other side-effects.195Instruction *PTI = PredBB->getTerminator();196if (PTI->isSpecialTerminator() || PTI->mayHaveSideEffects())197return false;198199// Can't merge if there are multiple distinct successors.200if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB)201return false;202203// Currently only allow PredBB to have two predecessors, one being BB.204// Update BI to branch to BB's only successor instead of BB.205BranchInst *PredBB_BI;206BasicBlock *NewSucc = nullptr;207unsigned FallThruPath;208if (PredecessorWithTwoSuccessors) {209if (!(PredBB_BI = dyn_cast<BranchInst>(PTI)))210return false;211BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator());212if (!BB_JmpI || !BB_JmpI->isUnconditional())213return false;214NewSucc = BB_JmpI->getSuccessor(0);215FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1;216}217218// Can't merge if there is PHI loop.219for (PHINode &PN : BB->phis())220if (llvm::is_contained(PN.incoming_values(), &PN))221return false;222223LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "224<< PredBB->getName() << "\n");225226// Begin by getting rid of unneeded PHIs.227SmallVector<AssertingVH<Value>, 4> IncomingValues;228if (isa<PHINode>(BB->front())) {229for (PHINode &PN : BB->phis())230if (!isa<PHINode>(PN.getIncomingValue(0)) ||231cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB)232IncomingValues.push_back(PN.getIncomingValue(0));233FoldSingleEntryPHINodes(BB, MemDep);234}235236if (DT) {237assert(!DTU && "cannot use both DT and DTU for updates");238DomTreeNode *PredNode = DT->getNode(PredBB);239DomTreeNode *BBNode = DT->getNode(BB);240if (PredNode) {241assert(BBNode && "PredNode unreachable but BBNode reachable?");242for (DomTreeNode *C : to_vector(BBNode->children()))243C->setIDom(PredNode);244}245}246// DTU update: Collect all the edges that exit BB.247// These dominator edges will be redirected from Pred.248std::vector<DominatorTree::UpdateType> Updates;249if (DTU) {250assert(!DT && "cannot use both DT and DTU for updates");251// To avoid processing the same predecessor more than once.252SmallPtrSet<BasicBlock *, 8> SeenSuccs;253SmallPtrSet<BasicBlock *, 2> SuccsOfPredBB(succ_begin(PredBB),254succ_end(PredBB));255Updates.reserve(Updates.size() + 2 * succ_size(BB) + 1);256// Add insert edges first. Experimentally, for the particular case of two257// blocks that can be merged, with a single successor and single predecessor258// respectively, it is beneficial to have all insert updates first. Deleting259// edges first may lead to unreachable blocks, followed by inserting edges260// making the blocks reachable again. Such DT updates lead to high compile261// times. We add inserts before deletes here to reduce compile time.262for (BasicBlock *SuccOfBB : successors(BB))263// This successor of BB may already be a PredBB's successor.264if (!SuccsOfPredBB.contains(SuccOfBB))265if (SeenSuccs.insert(SuccOfBB).second)266Updates.push_back({DominatorTree::Insert, PredBB, SuccOfBB});267SeenSuccs.clear();268for (BasicBlock *SuccOfBB : successors(BB))269if (SeenSuccs.insert(SuccOfBB).second)270Updates.push_back({DominatorTree::Delete, BB, SuccOfBB});271Updates.push_back({DominatorTree::Delete, PredBB, BB});272}273274Instruction *STI = BB->getTerminator();275Instruction *Start = &*BB->begin();276// If there's nothing to move, mark the starting instruction as the last277// instruction in the block. Terminator instruction is handled separately.278if (Start == STI)279Start = PTI;280281// Move all definitions in the successor to the predecessor...282PredBB->splice(PTI->getIterator(), BB, BB->begin(), STI->getIterator());283284if (MSSAU)285MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start);286287// Make all PHI nodes that referred to BB now refer to Pred as their288// source...289BB->replaceAllUsesWith(PredBB);290291if (PredecessorWithTwoSuccessors) {292// Delete the unconditional branch from BB.293BB->back().eraseFromParent();294295// Update branch in the predecessor.296PredBB_BI->setSuccessor(FallThruPath, NewSucc);297} else {298// Delete the unconditional branch from the predecessor.299PredBB->back().eraseFromParent();300301// Move terminator instruction.302BB->back().moveBeforePreserving(*PredBB, PredBB->end());303304// Terminator may be a memory accessing instruction too.305if (MSSAU)306if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>(307MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator())))308MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End);309}310// Add unreachable to now empty BB.311new UnreachableInst(BB->getContext(), BB);312313// Inherit predecessors name if it exists.314if (!PredBB->hasName())315PredBB->takeName(BB);316317if (LI)318LI->removeBlock(BB);319320if (MemDep)321MemDep->invalidateCachedPredecessors();322323if (DTU)324DTU->applyUpdates(Updates);325326if (DT) {327assert(succ_empty(BB) &&328"successors should have been transferred to PredBB");329DT->eraseNode(BB);330}331332// Finally, erase the old block and update dominator info.333DeleteDeadBlock(BB, DTU);334335return true;336}337338bool llvm::MergeBlockSuccessorsIntoGivenBlocks(339SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU,340LoopInfo *LI) {341assert(!MergeBlocks.empty() && "MergeBlocks should not be empty");342343bool BlocksHaveBeenMerged = false;344while (!MergeBlocks.empty()) {345BasicBlock *BB = *MergeBlocks.begin();346BasicBlock *Dest = BB->getSingleSuccessor();347if (Dest && (!L || L->contains(Dest))) {348BasicBlock *Fold = Dest->getUniquePredecessor();349(void)Fold;350if (MergeBlockIntoPredecessor(Dest, DTU, LI)) {351assert(Fold == BB &&352"Expecting BB to be unique predecessor of the Dest block");353MergeBlocks.erase(Dest);354BlocksHaveBeenMerged = true;355} else356MergeBlocks.erase(BB);357} else358MergeBlocks.erase(BB);359}360return BlocksHaveBeenMerged;361}362363/// Remove redundant instructions within sequences of consecutive dbg.value364/// instructions. This is done using a backward scan to keep the last dbg.value365/// describing a specific variable/fragment.366///367/// BackwardScan strategy:368/// ----------------------369/// Given a sequence of consecutive DbgValueInst like this370///371/// dbg.value ..., "x", FragmentX1 (*)372/// dbg.value ..., "y", FragmentY1373/// dbg.value ..., "x", FragmentX2374/// dbg.value ..., "x", FragmentX1 (**)375///376/// then the instruction marked with (*) can be removed (it is guaranteed to be377/// obsoleted by the instruction marked with (**) as the latter instruction is378/// describing the same variable using the same fragment info).379///380/// Possible improvements:381/// - Check fully overlapping fragments and not only identical fragments.382/// - Support dbg.declare. dbg.label, and possibly other meta instructions being383/// part of the sequence of consecutive instructions.384static bool385DbgVariableRecordsRemoveRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {386SmallVector<DbgVariableRecord *, 8> ToBeRemoved;387SmallDenseSet<DebugVariable> VariableSet;388for (auto &I : reverse(*BB)) {389for (DbgRecord &DR : reverse(I.getDbgRecordRange())) {390if (isa<DbgLabelRecord>(DR)) {391// Emulate existing behaviour (see comment below for dbg.declares).392// FIXME: Don't do this.393VariableSet.clear();394continue;395}396397DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);398// Skip declare-type records, as the debug intrinsic method only works399// on dbg.value intrinsics.400if (DVR.getType() == DbgVariableRecord::LocationType::Declare) {401// The debug intrinsic method treats dbg.declares are "non-debug"402// instructions (i.e., a break in a consecutive range of debug403// intrinsics). Emulate that to create identical outputs. See404// "Possible improvements" above.405// FIXME: Delete the line below.406VariableSet.clear();407continue;408}409410DebugVariable Key(DVR.getVariable(), DVR.getExpression(),411DVR.getDebugLoc()->getInlinedAt());412auto R = VariableSet.insert(Key);413// If the same variable fragment is described more than once it is enough414// to keep the last one (i.e. the first found since we for reverse415// iteration).416if (R.second)417continue;418419if (DVR.isDbgAssign()) {420// Don't delete dbg.assign intrinsics that are linked to instructions.421if (!at::getAssignmentInsts(&DVR).empty())422continue;423// Unlinked dbg.assign intrinsics can be treated like dbg.values.424}425426ToBeRemoved.push_back(&DVR);427continue;428}429// Sequence with consecutive dbg.value instrs ended. Clear the map to430// restart identifying redundant instructions if case we find another431// dbg.value sequence.432VariableSet.clear();433}434435for (auto &DVR : ToBeRemoved)436DVR->eraseFromParent();437438return !ToBeRemoved.empty();439}440441static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {442if (BB->IsNewDbgInfoFormat)443return DbgVariableRecordsRemoveRedundantDbgInstrsUsingBackwardScan(BB);444445SmallVector<DbgValueInst *, 8> ToBeRemoved;446SmallDenseSet<DebugVariable> VariableSet;447for (auto &I : reverse(*BB)) {448if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {449DebugVariable Key(DVI->getVariable(),450DVI->getExpression(),451DVI->getDebugLoc()->getInlinedAt());452auto R = VariableSet.insert(Key);453// If the variable fragment hasn't been seen before then we don't want454// to remove this dbg intrinsic.455if (R.second)456continue;457458if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI)) {459// Don't delete dbg.assign intrinsics that are linked to instructions.460if (!at::getAssignmentInsts(DAI).empty())461continue;462// Unlinked dbg.assign intrinsics can be treated like dbg.values.463}464465// If the same variable fragment is described more than once it is enough466// to keep the last one (i.e. the first found since we for reverse467// iteration).468ToBeRemoved.push_back(DVI);469continue;470}471// Sequence with consecutive dbg.value instrs ended. Clear the map to472// restart identifying redundant instructions if case we find another473// dbg.value sequence.474VariableSet.clear();475}476477for (auto &Instr : ToBeRemoved)478Instr->eraseFromParent();479480return !ToBeRemoved.empty();481}482483/// Remove redundant dbg.value instructions using a forward scan. This can484/// remove a dbg.value instruction that is redundant due to indicating that a485/// variable has the same value as already being indicated by an earlier486/// dbg.value.487///488/// ForwardScan strategy:489/// ---------------------490/// Given two identical dbg.value instructions, separated by a block of491/// instructions that isn't describing the same variable, like this492///493/// dbg.value X1, "x", FragmentX1 (**)494/// <block of instructions, none being "dbg.value ..., "x", ...">495/// dbg.value X1, "x", FragmentX1 (*)496///497/// then the instruction marked with (*) can be removed. Variable "x" is already498/// described as being mapped to the SSA value X1.499///500/// Possible improvements:501/// - Keep track of non-overlapping fragments.502static bool503DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {504SmallVector<DbgVariableRecord *, 8> ToBeRemoved;505DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>>506VariableMap;507for (auto &I : *BB) {508for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {509if (DVR.getType() == DbgVariableRecord::LocationType::Declare)510continue;511DebugVariable Key(DVR.getVariable(), std::nullopt,512DVR.getDebugLoc()->getInlinedAt());513auto VMI = VariableMap.find(Key);514// A dbg.assign with no linked instructions can be treated like a515// dbg.value (i.e. can be deleted).516bool IsDbgValueKind =517(!DVR.isDbgAssign() || at::getAssignmentInsts(&DVR).empty());518519// Update the map if we found a new value/expression describing the520// variable, or if the variable wasn't mapped already.521SmallVector<Value *, 4> Values(DVR.location_ops());522if (VMI == VariableMap.end() || VMI->second.first != Values ||523VMI->second.second != DVR.getExpression()) {524if (IsDbgValueKind)525VariableMap[Key] = {Values, DVR.getExpression()};526else527VariableMap[Key] = {Values, nullptr};528continue;529}530// Don't delete dbg.assign intrinsics that are linked to instructions.531if (!IsDbgValueKind)532continue;533// Found an identical mapping. Remember the instruction for later removal.534ToBeRemoved.push_back(&DVR);535}536}537538for (auto *DVR : ToBeRemoved)539DVR->eraseFromParent();540541return !ToBeRemoved.empty();542}543544static bool545DbgVariableRecordsRemoveUndefDbgAssignsFromEntryBlock(BasicBlock *BB) {546assert(BB->isEntryBlock() && "expected entry block");547SmallVector<DbgVariableRecord *, 8> ToBeRemoved;548DenseSet<DebugVariable> SeenDefForAggregate;549// Returns the DebugVariable for DVI with no fragment info.550auto GetAggregateVariable = [](const DbgVariableRecord &DVR) {551return DebugVariable(DVR.getVariable(), std::nullopt,552DVR.getDebugLoc().getInlinedAt());553};554555// Remove undef dbg.assign intrinsics that are encountered before556// any non-undef intrinsics from the entry block.557for (auto &I : *BB) {558for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {559if (!DVR.isDbgValue() && !DVR.isDbgAssign())560continue;561bool IsDbgValueKind =562(DVR.isDbgValue() || at::getAssignmentInsts(&DVR).empty());563DebugVariable Aggregate = GetAggregateVariable(DVR);564if (!SeenDefForAggregate.contains(Aggregate)) {565bool IsKill = DVR.isKillLocation() && IsDbgValueKind;566if (!IsKill) {567SeenDefForAggregate.insert(Aggregate);568} else if (DVR.isDbgAssign()) {569ToBeRemoved.push_back(&DVR);570}571}572}573}574575for (DbgVariableRecord *DVR : ToBeRemoved)576DVR->eraseFromParent();577578return !ToBeRemoved.empty();579}580581static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {582if (BB->IsNewDbgInfoFormat)583return DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan(BB);584585SmallVector<DbgValueInst *, 8> ToBeRemoved;586DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>>587VariableMap;588for (auto &I : *BB) {589if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {590DebugVariable Key(DVI->getVariable(), std::nullopt,591DVI->getDebugLoc()->getInlinedAt());592auto VMI = VariableMap.find(Key);593auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);594// A dbg.assign with no linked instructions can be treated like a595// dbg.value (i.e. can be deleted).596bool IsDbgValueKind = (!DAI || at::getAssignmentInsts(DAI).empty());597598// Update the map if we found a new value/expression describing the599// variable, or if the variable wasn't mapped already.600SmallVector<Value *, 4> Values(DVI->getValues());601if (VMI == VariableMap.end() || VMI->second.first != Values ||602VMI->second.second != DVI->getExpression()) {603// Use a sentinel value (nullptr) for the DIExpression when we see a604// linked dbg.assign so that the next debug intrinsic will never match605// it (i.e. always treat linked dbg.assigns as if they're unique).606if (IsDbgValueKind)607VariableMap[Key] = {Values, DVI->getExpression()};608else609VariableMap[Key] = {Values, nullptr};610continue;611}612613// Don't delete dbg.assign intrinsics that are linked to instructions.614if (!IsDbgValueKind)615continue;616ToBeRemoved.push_back(DVI);617}618}619620for (auto &Instr : ToBeRemoved)621Instr->eraseFromParent();622623return !ToBeRemoved.empty();624}625626/// Remove redundant undef dbg.assign intrinsic from an entry block using a627/// forward scan.628/// Strategy:629/// ---------------------630/// Scanning forward, delete dbg.assign intrinsics iff they are undef, not631/// linked to an intrinsic, and don't share an aggregate variable with a debug632/// intrinsic that didn't meet the criteria. In other words, undef dbg.assigns633/// that come before non-undef debug intrinsics for the variable are634/// deleted. Given:635///636/// dbg.assign undef, "x", FragmentX1 (*)637/// <block of instructions, none being "dbg.value ..., "x", ...">638/// dbg.value %V, "x", FragmentX2639/// <block of instructions, none being "dbg.value ..., "x", ...">640/// dbg.assign undef, "x", FragmentX1641///642/// then (only) the instruction marked with (*) can be removed.643/// Possible improvements:644/// - Keep track of non-overlapping fragments.645static bool removeUndefDbgAssignsFromEntryBlock(BasicBlock *BB) {646if (BB->IsNewDbgInfoFormat)647return DbgVariableRecordsRemoveUndefDbgAssignsFromEntryBlock(BB);648649assert(BB->isEntryBlock() && "expected entry block");650SmallVector<DbgAssignIntrinsic *, 8> ToBeRemoved;651DenseSet<DebugVariable> SeenDefForAggregate;652// Returns the DebugVariable for DVI with no fragment info.653auto GetAggregateVariable = [](DbgValueInst *DVI) {654return DebugVariable(DVI->getVariable(), std::nullopt,655DVI->getDebugLoc()->getInlinedAt());656};657658// Remove undef dbg.assign intrinsics that are encountered before659// any non-undef intrinsics from the entry block.660for (auto &I : *BB) {661DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I);662if (!DVI)663continue;664auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);665bool IsDbgValueKind = (!DAI || at::getAssignmentInsts(DAI).empty());666DebugVariable Aggregate = GetAggregateVariable(DVI);667if (!SeenDefForAggregate.contains(Aggregate)) {668bool IsKill = DVI->isKillLocation() && IsDbgValueKind;669if (!IsKill) {670SeenDefForAggregate.insert(Aggregate);671} else if (DAI) {672ToBeRemoved.push_back(DAI);673}674}675}676677for (DbgAssignIntrinsic *DAI : ToBeRemoved)678DAI->eraseFromParent();679680return !ToBeRemoved.empty();681}682683bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) {684bool MadeChanges = false;685// By using the "backward scan" strategy before the "forward scan" strategy we686// can remove both dbg.value (2) and (3) in a situation like this:687//688// (1) dbg.value V1, "x", DIExpression()689// ...690// (2) dbg.value V2, "x", DIExpression()691// (3) dbg.value V1, "x", DIExpression()692//693// The backward scan will remove (2), it is made obsolete by (3). After694// getting (2) out of the way, the foward scan will remove (3) since "x"695// already is described as having the value V1 at (1).696MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB);697if (BB->isEntryBlock() &&698isAssignmentTrackingEnabled(*BB->getParent()->getParent()))699MadeChanges |= removeUndefDbgAssignsFromEntryBlock(BB);700MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB);701702if (MadeChanges)703LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: "704<< BB->getName() << "\n");705return MadeChanges;706}707708void llvm::ReplaceInstWithValue(BasicBlock::iterator &BI, Value *V) {709Instruction &I = *BI;710// Replaces all of the uses of the instruction with uses of the value711I.replaceAllUsesWith(V);712713// Make sure to propagate a name if there is one already.714if (I.hasName() && !V->hasName())715V->takeName(&I);716717// Delete the unnecessary instruction now...718BI = BI->eraseFromParent();719}720721void llvm::ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI,722Instruction *I) {723assert(I->getParent() == nullptr &&724"ReplaceInstWithInst: Instruction already inserted into basic block!");725726// Copy debug location to newly added instruction, if it wasn't already set727// by the caller.728if (!I->getDebugLoc())729I->setDebugLoc(BI->getDebugLoc());730731// Insert the new instruction into the basic block...732BasicBlock::iterator New = I->insertInto(BB, BI);733734// Replace all uses of the old instruction, and delete it.735ReplaceInstWithValue(BI, I);736737// Move BI back to point to the newly inserted instruction738BI = New;739}740741bool llvm::IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB) {742// Remember visited blocks to avoid infinite loop743SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;744unsigned Depth = 0;745while (BB && Depth++ < MaxDeoptOrUnreachableSuccessorCheckDepth &&746VisitedBlocks.insert(BB).second) {747if (isa<UnreachableInst>(BB->getTerminator()) ||748BB->getTerminatingDeoptimizeCall())749return true;750BB = BB->getUniqueSuccessor();751}752return false;753}754755void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {756BasicBlock::iterator BI(From);757ReplaceInstWithInst(From->getParent(), BI, To);758}759760BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,761LoopInfo *LI, MemorySSAUpdater *MSSAU,762const Twine &BBName) {763unsigned SuccNum = GetSuccessorNumber(BB, Succ);764765Instruction *LatchTerm = BB->getTerminator();766767CriticalEdgeSplittingOptions Options =768CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA();769770if ((isCriticalEdge(LatchTerm, SuccNum, Options.MergeIdenticalEdges))) {771// If it is a critical edge, and the succesor is an exception block, handle772// the split edge logic in this specific function773if (Succ->isEHPad())774return ehAwareSplitEdge(BB, Succ, nullptr, nullptr, Options, BBName);775776// If this is a critical edge, let SplitKnownCriticalEdge do it.777return SplitKnownCriticalEdge(LatchTerm, SuccNum, Options, BBName);778}779780// If the edge isn't critical, then BB has a single successor or Succ has a781// single pred. Split the block.782if (BasicBlock *SP = Succ->getSinglePredecessor()) {783// If the successor only has a single pred, split the top of the successor784// block.785assert(SP == BB && "CFG broken");786(void)SP;787return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName,788/*Before=*/true);789}790791// Otherwise, if BB has a single successor, split it at the bottom of the792// block.793assert(BB->getTerminator()->getNumSuccessors() == 1 &&794"Should have a single succ!");795return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName);796}797798void llvm::setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ) {799if (auto *II = dyn_cast<InvokeInst>(TI))800II->setUnwindDest(Succ);801else if (auto *CS = dyn_cast<CatchSwitchInst>(TI))802CS->setUnwindDest(Succ);803else if (auto *CR = dyn_cast<CleanupReturnInst>(TI))804CR->setUnwindDest(Succ);805else806llvm_unreachable("unexpected terminator instruction");807}808809void llvm::updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,810BasicBlock *NewPred, PHINode *Until) {811int BBIdx = 0;812for (PHINode &PN : DestBB->phis()) {813// We manually update the LandingPadReplacement PHINode and it is the last814// PHI Node. So, if we find it, we are done.815if (Until == &PN)816break;817818// Reuse the previous value of BBIdx if it lines up. In cases where we819// have multiple phi nodes with *lots* of predecessors, this is a speed820// win because we don't have to scan the PHI looking for TIBB. This821// happens because the BB list of PHI nodes are usually in the same822// order.823if (PN.getIncomingBlock(BBIdx) != OldPred)824BBIdx = PN.getBasicBlockIndex(OldPred);825826assert(BBIdx != -1 && "Invalid PHI Index!");827PN.setIncomingBlock(BBIdx, NewPred);828}829}830831BasicBlock *llvm::ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,832LandingPadInst *OriginalPad,833PHINode *LandingPadReplacement,834const CriticalEdgeSplittingOptions &Options,835const Twine &BBName) {836837auto *PadInst = Succ->getFirstNonPHI();838if (!LandingPadReplacement && !PadInst->isEHPad())839return SplitEdge(BB, Succ, Options.DT, Options.LI, Options.MSSAU, BBName);840841auto *LI = Options.LI;842SmallVector<BasicBlock *, 4> LoopPreds;843// Check if extra modifications will be required to preserve loop-simplify844// form after splitting. If it would require splitting blocks with IndirectBr845// terminators, bail out if preserving loop-simplify form is requested.846if (Options.PreserveLoopSimplify && LI) {847if (Loop *BBLoop = LI->getLoopFor(BB)) {848849// The only way that we can break LoopSimplify form by splitting a850// critical edge is when there exists some edge from BBLoop to Succ *and*851// the only edge into Succ from outside of BBLoop is that of NewBB after852// the split. If the first isn't true, then LoopSimplify still holds,853// NewBB is the new exit block and it has no non-loop predecessors. If the854// second isn't true, then Succ was not in LoopSimplify form prior to855// the split as it had a non-loop predecessor. In both of these cases,856// the predecessor must be directly in BBLoop, not in a subloop, or again857// LoopSimplify doesn't hold.858for (BasicBlock *P : predecessors(Succ)) {859if (P == BB)860continue; // The new block is known.861if (LI->getLoopFor(P) != BBLoop) {862// Loop is not in LoopSimplify form, no need to re simplify after863// splitting edge.864LoopPreds.clear();865break;866}867LoopPreds.push_back(P);868}869// Loop-simplify form can be preserved, if we can split all in-loop870// predecessors.871if (any_of(LoopPreds, [](BasicBlock *Pred) {872return isa<IndirectBrInst>(Pred->getTerminator());873})) {874return nullptr;875}876}877}878879auto *NewBB =880BasicBlock::Create(BB->getContext(), BBName, BB->getParent(), Succ);881setUnwindEdgeTo(BB->getTerminator(), NewBB);882updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement);883884if (LandingPadReplacement) {885auto *NewLP = OriginalPad->clone();886auto *Terminator = BranchInst::Create(Succ, NewBB);887NewLP->insertBefore(Terminator);888LandingPadReplacement->addIncoming(NewLP, NewBB);889} else {890Value *ParentPad = nullptr;891if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst))892ParentPad = FuncletPad->getParentPad();893else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst))894ParentPad = CatchSwitch->getParentPad();895else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(PadInst))896ParentPad = CleanupPad->getParentPad();897else if (auto *LandingPad = dyn_cast<LandingPadInst>(PadInst))898ParentPad = LandingPad->getParent();899else900llvm_unreachable("handling for other EHPads not implemented yet");901902auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, BBName, NewBB);903CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB);904}905906auto *DT = Options.DT;907auto *MSSAU = Options.MSSAU;908if (!DT && !LI)909return NewBB;910911if (DT) {912DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);913SmallVector<DominatorTree::UpdateType, 3> Updates;914915Updates.push_back({DominatorTree::Insert, BB, NewBB});916Updates.push_back({DominatorTree::Insert, NewBB, Succ});917Updates.push_back({DominatorTree::Delete, BB, Succ});918919DTU.applyUpdates(Updates);920DTU.flush();921922if (MSSAU) {923MSSAU->applyUpdates(Updates, *DT);924if (VerifyMemorySSA)925MSSAU->getMemorySSA()->verifyMemorySSA();926}927}928929if (LI) {930if (Loop *BBLoop = LI->getLoopFor(BB)) {931// If one or the other blocks were not in a loop, the new block is not932// either, and thus LI doesn't need to be updated.933if (Loop *SuccLoop = LI->getLoopFor(Succ)) {934if (BBLoop == SuccLoop) {935// Both in the same loop, the NewBB joins loop.936SuccLoop->addBasicBlockToLoop(NewBB, *LI);937} else if (BBLoop->contains(SuccLoop)) {938// Edge from an outer loop to an inner loop. Add to the outer loop.939BBLoop->addBasicBlockToLoop(NewBB, *LI);940} else if (SuccLoop->contains(BBLoop)) {941// Edge from an inner loop to an outer loop. Add to the outer loop.942SuccLoop->addBasicBlockToLoop(NewBB, *LI);943} else {944// Edge from two loops with no containment relation. Because these945// are natural loops, we know that the destination block must be the946// header of its loop (adding a branch into a loop elsewhere would947// create an irreducible loop).948assert(SuccLoop->getHeader() == Succ &&949"Should not create irreducible loops!");950if (Loop *P = SuccLoop->getParentLoop())951P->addBasicBlockToLoop(NewBB, *LI);952}953}954955// If BB is in a loop and Succ is outside of that loop, we may need to956// update LoopSimplify form and LCSSA form.957if (!BBLoop->contains(Succ)) {958assert(!BBLoop->contains(NewBB) &&959"Split point for loop exit is contained in loop!");960961// Update LCSSA form in the newly created exit block.962if (Options.PreserveLCSSA) {963createPHIsForSplitLoopExit(BB, NewBB, Succ);964}965966if (!LoopPreds.empty()) {967BasicBlock *NewExitBB = SplitBlockPredecessors(968Succ, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);969if (Options.PreserveLCSSA)970createPHIsForSplitLoopExit(LoopPreds, NewExitBB, Succ);971}972}973}974}975976return NewBB;977}978979void llvm::createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,980BasicBlock *SplitBB, BasicBlock *DestBB) {981// SplitBB shouldn't have anything non-trivial in it yet.982assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||983SplitBB->isLandingPad()) &&984"SplitBB has non-PHI nodes!");985986// For each PHI in the destination block.987for (PHINode &PN : DestBB->phis()) {988int Idx = PN.getBasicBlockIndex(SplitBB);989assert(Idx >= 0 && "Invalid Block Index");990Value *V = PN.getIncomingValue(Idx);991992// If the input is a PHI which already satisfies LCSSA, don't create993// a new one.994if (const PHINode *VP = dyn_cast<PHINode>(V))995if (VP->getParent() == SplitBB)996continue;997998// Otherwise a new PHI is needed. Create one and populate it.999PHINode *NewPN = PHINode::Create(PN.getType(), Preds.size(), "split");1000BasicBlock::iterator InsertPos =1001SplitBB->isLandingPad() ? SplitBB->begin()1002: SplitBB->getTerminator()->getIterator();1003NewPN->insertBefore(InsertPos);1004for (BasicBlock *BB : Preds)1005NewPN->addIncoming(V, BB);10061007// Update the original PHI.1008PN.setIncomingValue(Idx, NewPN);1009}1010}10111012unsigned1013llvm::SplitAllCriticalEdges(Function &F,1014const CriticalEdgeSplittingOptions &Options) {1015unsigned NumBroken = 0;1016for (BasicBlock &BB : F) {1017Instruction *TI = BB.getTerminator();1018if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))1019for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)1020if (SplitCriticalEdge(TI, i, Options))1021++NumBroken;1022}1023return NumBroken;1024}10251026static BasicBlock *SplitBlockImpl(BasicBlock *Old, BasicBlock::iterator SplitPt,1027DomTreeUpdater *DTU, DominatorTree *DT,1028LoopInfo *LI, MemorySSAUpdater *MSSAU,1029const Twine &BBName, bool Before) {1030if (Before) {1031DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);1032return splitBlockBefore(Old, SplitPt,1033DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU,1034BBName);1035}1036BasicBlock::iterator SplitIt = SplitPt;1037while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) {1038++SplitIt;1039assert(SplitIt != SplitPt->getParent()->end());1040}1041std::string Name = BBName.str();1042BasicBlock *New = Old->splitBasicBlock(1043SplitIt, Name.empty() ? Old->getName() + ".split" : Name);10441045// The new block lives in whichever loop the old one did. This preserves1046// LCSSA as well, because we force the split point to be after any PHI nodes.1047if (LI)1048if (Loop *L = LI->getLoopFor(Old))1049L->addBasicBlockToLoop(New, *LI);10501051if (DTU) {1052SmallVector<DominatorTree::UpdateType, 8> Updates;1053// Old dominates New. New node dominates all other nodes dominated by Old.1054SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfOld;1055Updates.push_back({DominatorTree::Insert, Old, New});1056Updates.reserve(Updates.size() + 2 * succ_size(New));1057for (BasicBlock *SuccessorOfOld : successors(New))1058if (UniqueSuccessorsOfOld.insert(SuccessorOfOld).second) {1059Updates.push_back({DominatorTree::Insert, New, SuccessorOfOld});1060Updates.push_back({DominatorTree::Delete, Old, SuccessorOfOld});1061}10621063DTU->applyUpdates(Updates);1064} else if (DT)1065// Old dominates New. New node dominates all other nodes dominated by Old.1066if (DomTreeNode *OldNode = DT->getNode(Old)) {1067std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());10681069DomTreeNode *NewNode = DT->addNewBlock(New, Old);1070for (DomTreeNode *I : Children)1071DT->changeImmediateDominator(I, NewNode);1072}10731074// Move MemoryAccesses still tracked in Old, but part of New now.1075// Update accesses in successor blocks accordingly.1076if (MSSAU)1077MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));10781079return New;1080}10811082BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,1083DominatorTree *DT, LoopInfo *LI,1084MemorySSAUpdater *MSSAU, const Twine &BBName,1085bool Before) {1086return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName,1087Before);1088}1089BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,1090DomTreeUpdater *DTU, LoopInfo *LI,1091MemorySSAUpdater *MSSAU, const Twine &BBName,1092bool Before) {1093return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName,1094Before);1095}10961097BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt,1098DomTreeUpdater *DTU, LoopInfo *LI,1099MemorySSAUpdater *MSSAU,1100const Twine &BBName) {11011102BasicBlock::iterator SplitIt = SplitPt;1103while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())1104++SplitIt;1105std::string Name = BBName.str();1106BasicBlock *New = Old->splitBasicBlock(1107SplitIt, Name.empty() ? Old->getName() + ".split" : Name,1108/* Before=*/true);11091110// The new block lives in whichever loop the old one did. This preserves1111// LCSSA as well, because we force the split point to be after any PHI nodes.1112if (LI)1113if (Loop *L = LI->getLoopFor(Old))1114L->addBasicBlockToLoop(New, *LI);11151116if (DTU) {1117SmallVector<DominatorTree::UpdateType, 8> DTUpdates;1118// New dominates Old. The predecessor nodes of the Old node dominate1119// New node.1120SmallPtrSet<BasicBlock *, 8> UniquePredecessorsOfOld;1121DTUpdates.push_back({DominatorTree::Insert, New, Old});1122DTUpdates.reserve(DTUpdates.size() + 2 * pred_size(New));1123for (BasicBlock *PredecessorOfOld : predecessors(New))1124if (UniquePredecessorsOfOld.insert(PredecessorOfOld).second) {1125DTUpdates.push_back({DominatorTree::Insert, PredecessorOfOld, New});1126DTUpdates.push_back({DominatorTree::Delete, PredecessorOfOld, Old});1127}11281129DTU->applyUpdates(DTUpdates);11301131// Move MemoryAccesses still tracked in Old, but part of New now.1132// Update accesses in successor blocks accordingly.1133if (MSSAU) {1134MSSAU->applyUpdates(DTUpdates, DTU->getDomTree());1135if (VerifyMemorySSA)1136MSSAU->getMemorySSA()->verifyMemorySSA();1137}1138}1139return New;1140}11411142/// Update DominatorTree, LoopInfo, and LCCSA analysis information.1143/// Invalidates DFS Numbering when DTU or DT is provided.1144static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,1145ArrayRef<BasicBlock *> Preds,1146DomTreeUpdater *DTU, DominatorTree *DT,1147LoopInfo *LI, MemorySSAUpdater *MSSAU,1148bool PreserveLCSSA, bool &HasLoopExit) {1149// Update dominator tree if available.1150if (DTU) {1151// Recalculation of DomTree is needed when updating a forward DomTree and1152// the Entry BB is replaced.1153if (NewBB->isEntryBlock() && DTU->hasDomTree()) {1154// The entry block was removed and there is no external interface for1155// the dominator tree to be notified of this change. In this corner-case1156// we recalculate the entire tree.1157DTU->recalculate(*NewBB->getParent());1158} else {1159// Split block expects NewBB to have a non-empty set of predecessors.1160SmallVector<DominatorTree::UpdateType, 8> Updates;1161SmallPtrSet<BasicBlock *, 8> UniquePreds;1162Updates.push_back({DominatorTree::Insert, NewBB, OldBB});1163Updates.reserve(Updates.size() + 2 * Preds.size());1164for (auto *Pred : Preds)1165if (UniquePreds.insert(Pred).second) {1166Updates.push_back({DominatorTree::Insert, Pred, NewBB});1167Updates.push_back({DominatorTree::Delete, Pred, OldBB});1168}1169DTU->applyUpdates(Updates);1170}1171} else if (DT) {1172if (OldBB == DT->getRootNode()->getBlock()) {1173assert(NewBB->isEntryBlock());1174DT->setNewRoot(NewBB);1175} else {1176// Split block expects NewBB to have a non-empty set of predecessors.1177DT->splitBlock(NewBB);1178}1179}11801181// Update MemoryPhis after split if MemorySSA is available1182if (MSSAU)1183MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);11841185// The rest of the logic is only relevant for updating the loop structures.1186if (!LI)1187return;11881189if (DTU && DTU->hasDomTree())1190DT = &DTU->getDomTree();1191assert(DT && "DT should be available to update LoopInfo!");1192Loop *L = LI->getLoopFor(OldBB);11931194// If we need to preserve loop analyses, collect some information about how1195// this split will affect loops.1196bool IsLoopEntry = !!L;1197bool SplitMakesNewLoopHeader = false;1198for (BasicBlock *Pred : Preds) {1199// Preds that are not reachable from entry should not be used to identify if1200// OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks1201// are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader1202// as true and make the NewBB the header of some loop. This breaks LI.1203if (!DT->isReachableFromEntry(Pred))1204continue;1205// If we need to preserve LCSSA, determine if any of the preds is a loop1206// exit.1207if (PreserveLCSSA)1208if (Loop *PL = LI->getLoopFor(Pred))1209if (!PL->contains(OldBB))1210HasLoopExit = true;12111212// If we need to preserve LoopInfo, note whether any of the preds crosses1213// an interesting loop boundary.1214if (!L)1215continue;1216if (L->contains(Pred))1217IsLoopEntry = false;1218else1219SplitMakesNewLoopHeader = true;1220}12211222// Unless we have a loop for OldBB, nothing else to do here.1223if (!L)1224return;12251226if (IsLoopEntry) {1227// Add the new block to the nearest enclosing loop (and not an adjacent1228// loop). To find this, examine each of the predecessors and determine which1229// loops enclose them, and select the most-nested loop which contains the1230// loop containing the block being split.1231Loop *InnermostPredLoop = nullptr;1232for (BasicBlock *Pred : Preds) {1233if (Loop *PredLoop = LI->getLoopFor(Pred)) {1234// Seek a loop which actually contains the block being split (to avoid1235// adjacent loops).1236while (PredLoop && !PredLoop->contains(OldBB))1237PredLoop = PredLoop->getParentLoop();12381239// Select the most-nested of these loops which contains the block.1240if (PredLoop && PredLoop->contains(OldBB) &&1241(!InnermostPredLoop ||1242InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))1243InnermostPredLoop = PredLoop;1244}1245}12461247if (InnermostPredLoop)1248InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);1249} else {1250L->addBasicBlockToLoop(NewBB, *LI);1251if (SplitMakesNewLoopHeader)1252L->moveToHeader(NewBB);1253}1254}12551256/// Update the PHI nodes in OrigBB to include the values coming from NewBB.1257/// This also updates AliasAnalysis, if available.1258static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,1259ArrayRef<BasicBlock *> Preds, BranchInst *BI,1260bool HasLoopExit) {1261// Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.1262SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());1263for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {1264PHINode *PN = cast<PHINode>(I++);12651266// Check to see if all of the values coming in are the same. If so, we1267// don't need to create a new PHI node, unless it's needed for LCSSA.1268Value *InVal = nullptr;1269if (!HasLoopExit) {1270InVal = PN->getIncomingValueForBlock(Preds[0]);1271for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {1272if (!PredSet.count(PN->getIncomingBlock(i)))1273continue;1274if (!InVal)1275InVal = PN->getIncomingValue(i);1276else if (InVal != PN->getIncomingValue(i)) {1277InVal = nullptr;1278break;1279}1280}1281}12821283if (InVal) {1284// If all incoming values for the new PHI would be the same, just don't1285// make a new PHI. Instead, just remove the incoming values from the old1286// PHI.1287PN->removeIncomingValueIf(1288[&](unsigned Idx) {1289return PredSet.contains(PN->getIncomingBlock(Idx));1290},1291/* DeletePHIIfEmpty */ false);12921293// Add an incoming value to the PHI node in the loop for the preheader1294// edge.1295PN->addIncoming(InVal, NewBB);1296continue;1297}12981299// If the values coming into the block are not the same, we need a new1300// PHI.1301// Create the new PHI node, insert it into NewBB at the end of the block1302PHINode *NewPHI =1303PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI->getIterator());13041305// NOTE! This loop walks backwards for a reason! First off, this minimizes1306// the cost of removal if we end up removing a large number of values, and1307// second off, this ensures that the indices for the incoming values aren't1308// invalidated when we remove one.1309for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {1310BasicBlock *IncomingBB = PN->getIncomingBlock(i);1311if (PredSet.count(IncomingBB)) {1312Value *V = PN->removeIncomingValue(i, false);1313NewPHI->addIncoming(V, IncomingBB);1314}1315}13161317PN->addIncoming(NewPHI, NewBB);1318}1319}13201321static void SplitLandingPadPredecessorsImpl(1322BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,1323const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,1324DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,1325MemorySSAUpdater *MSSAU, bool PreserveLCSSA);13261327static BasicBlock *1328SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,1329const char *Suffix, DomTreeUpdater *DTU,1330DominatorTree *DT, LoopInfo *LI,1331MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {1332// Do not attempt to split that which cannot be split.1333if (!BB->canSplitPredecessors())1334return nullptr;13351336// For the landingpads we need to act a bit differently.1337// Delegate this work to the SplitLandingPadPredecessors.1338if (BB->isLandingPad()) {1339SmallVector<BasicBlock*, 2> NewBBs;1340std::string NewName = std::string(Suffix) + ".split-lp";13411342SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs,1343DTU, DT, LI, MSSAU, PreserveLCSSA);1344return NewBBs[0];1345}13461347// Create new basic block, insert right before the original block.1348BasicBlock *NewBB = BasicBlock::Create(1349BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);13501351// The new block unconditionally branches to the old block.1352BranchInst *BI = BranchInst::Create(BB, NewBB);13531354Loop *L = nullptr;1355BasicBlock *OldLatch = nullptr;1356// Splitting the predecessors of a loop header creates a preheader block.1357if (LI && LI->isLoopHeader(BB)) {1358L = LI->getLoopFor(BB);1359// Using the loop start line number prevents debuggers stepping into the1360// loop body for this instruction.1361BI->setDebugLoc(L->getStartLoc());13621363// If BB is the header of the Loop, it is possible that the loop is1364// modified, such that the current latch does not remain the latch of the1365// loop. If that is the case, the loop metadata from the current latch needs1366// to be applied to the new latch.1367OldLatch = L->getLoopLatch();1368} else1369BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());13701371// Move the edges from Preds to point to NewBB instead of BB.1372for (BasicBlock *Pred : Preds) {1373// This is slightly more strict than necessary; the minimum requirement1374// is that there be no more than one indirectbr branching to BB. And1375// all BlockAddress uses would need to be updated.1376assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&1377"Cannot split an edge from an IndirectBrInst");1378Pred->getTerminator()->replaceSuccessorWith(BB, NewBB);1379}13801381// Insert a new PHI node into NewBB for every PHI node in BB and that new PHI1382// node becomes an incoming value for BB's phi node. However, if the Preds1383// list is empty, we need to insert dummy entries into the PHI nodes in BB to1384// account for the newly created predecessor.1385if (Preds.empty()) {1386// Insert dummy values as the incoming value.1387for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)1388cast<PHINode>(I)->addIncoming(PoisonValue::get(I->getType()), NewBB);1389}13901391// Update DominatorTree, LoopInfo, and LCCSA analysis information.1392bool HasLoopExit = false;1393UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA,1394HasLoopExit);13951396if (!Preds.empty()) {1397// Update the PHI nodes in BB with the values coming from NewBB.1398UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);1399}14001401if (OldLatch) {1402BasicBlock *NewLatch = L->getLoopLatch();1403if (NewLatch != OldLatch) {1404MDNode *MD = OldLatch->getTerminator()->getMetadata(LLVMContext::MD_loop);1405NewLatch->getTerminator()->setMetadata(LLVMContext::MD_loop, MD);1406// It's still possible that OldLatch is the latch of another inner loop,1407// in which case we do not remove the metadata.1408Loop *IL = LI->getLoopFor(OldLatch);1409if (IL && IL->getLoopLatch() != OldLatch)1410OldLatch->getTerminator()->setMetadata(LLVMContext::MD_loop, nullptr);1411}1412}14131414return NewBB;1415}14161417BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,1418ArrayRef<BasicBlock *> Preds,1419const char *Suffix, DominatorTree *DT,1420LoopInfo *LI, MemorySSAUpdater *MSSAU,1421bool PreserveLCSSA) {1422return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI,1423MSSAU, PreserveLCSSA);1424}1425BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,1426ArrayRef<BasicBlock *> Preds,1427const char *Suffix,1428DomTreeUpdater *DTU, LoopInfo *LI,1429MemorySSAUpdater *MSSAU,1430bool PreserveLCSSA) {1431return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU,1432/*DT=*/nullptr, LI, MSSAU, PreserveLCSSA);1433}14341435static void SplitLandingPadPredecessorsImpl(1436BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,1437const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,1438DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,1439MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {1440assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");14411442// Create a new basic block for OrigBB's predecessors listed in Preds. Insert1443// it right before the original block.1444BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),1445OrigBB->getName() + Suffix1,1446OrigBB->getParent(), OrigBB);1447NewBBs.push_back(NewBB1);14481449// The new block unconditionally branches to the old block.1450BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);1451BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());14521453// Move the edges from Preds to point to NewBB1 instead of OrigBB.1454for (BasicBlock *Pred : Preds) {1455// This is slightly more strict than necessary; the minimum requirement1456// is that there be no more than one indirectbr branching to BB. And1457// all BlockAddress uses would need to be updated.1458assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&1459"Cannot split an edge from an IndirectBrInst");1460Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);1461}14621463bool HasLoopExit = false;1464UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU,1465PreserveLCSSA, HasLoopExit);14661467// Update the PHI nodes in OrigBB with the values coming from NewBB1.1468UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);14691470// Move the remaining edges from OrigBB to point to NewBB2.1471SmallVector<BasicBlock*, 8> NewBB2Preds;1472for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);1473i != e; ) {1474BasicBlock *Pred = *i++;1475if (Pred == NewBB1) continue;1476assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&1477"Cannot split an edge from an IndirectBrInst");1478NewBB2Preds.push_back(Pred);1479e = pred_end(OrigBB);1480}14811482BasicBlock *NewBB2 = nullptr;1483if (!NewBB2Preds.empty()) {1484// Create another basic block for the rest of OrigBB's predecessors.1485NewBB2 = BasicBlock::Create(OrigBB->getContext(),1486OrigBB->getName() + Suffix2,1487OrigBB->getParent(), OrigBB);1488NewBBs.push_back(NewBB2);14891490// The new block unconditionally branches to the old block.1491BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);1492BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());14931494// Move the remaining edges from OrigBB to point to NewBB2.1495for (BasicBlock *NewBB2Pred : NewBB2Preds)1496NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);14971498// Update DominatorTree, LoopInfo, and LCCSA analysis information.1499HasLoopExit = false;1500UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU,1501PreserveLCSSA, HasLoopExit);15021503// Update the PHI nodes in OrigBB with the values coming from NewBB2.1504UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);1505}15061507LandingPadInst *LPad = OrigBB->getLandingPadInst();1508Instruction *Clone1 = LPad->clone();1509Clone1->setName(Twine("lpad") + Suffix1);1510Clone1->insertInto(NewBB1, NewBB1->getFirstInsertionPt());15111512if (NewBB2) {1513Instruction *Clone2 = LPad->clone();1514Clone2->setName(Twine("lpad") + Suffix2);1515Clone2->insertInto(NewBB2, NewBB2->getFirstInsertionPt());15161517// Create a PHI node for the two cloned landingpad instructions only1518// if the original landingpad instruction has some uses.1519if (!LPad->use_empty()) {1520assert(!LPad->getType()->isTokenTy() &&1521"Split cannot be applied if LPad is token type. Otherwise an "1522"invalid PHINode of token type would be created.");1523PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad->getIterator());1524PN->addIncoming(Clone1, NewBB1);1525PN->addIncoming(Clone2, NewBB2);1526LPad->replaceAllUsesWith(PN);1527}1528LPad->eraseFromParent();1529} else {1530// There is no second clone. Just replace the landing pad with the first1531// clone.1532LPad->replaceAllUsesWith(Clone1);1533LPad->eraseFromParent();1534}1535}15361537void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,1538ArrayRef<BasicBlock *> Preds,1539const char *Suffix1, const char *Suffix2,1540SmallVectorImpl<BasicBlock *> &NewBBs,1541DomTreeUpdater *DTU, LoopInfo *LI,1542MemorySSAUpdater *MSSAU,1543bool PreserveLCSSA) {1544return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2,1545NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU,1546PreserveLCSSA);1547}15481549ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,1550BasicBlock *Pred,1551DomTreeUpdater *DTU) {1552Instruction *UncondBranch = Pred->getTerminator();1553// Clone the return and add it to the end of the predecessor.1554Instruction *NewRet = RI->clone();1555NewRet->insertInto(Pred, Pred->end());15561557// If the return instruction returns a value, and if the value was a1558// PHI node in "BB", propagate the right value into the return.1559for (Use &Op : NewRet->operands()) {1560Value *V = Op;1561Instruction *NewBC = nullptr;1562if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {1563// Return value might be bitcasted. Clone and insert it before the1564// return instruction.1565V = BCI->getOperand(0);1566NewBC = BCI->clone();1567NewBC->insertInto(Pred, NewRet->getIterator());1568Op = NewBC;1569}15701571Instruction *NewEV = nullptr;1572if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {1573V = EVI->getOperand(0);1574NewEV = EVI->clone();1575if (NewBC) {1576NewBC->setOperand(0, NewEV);1577NewEV->insertInto(Pred, NewBC->getIterator());1578} else {1579NewEV->insertInto(Pred, NewRet->getIterator());1580Op = NewEV;1581}1582}15831584if (PHINode *PN = dyn_cast<PHINode>(V)) {1585if (PN->getParent() == BB) {1586if (NewEV) {1587NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred));1588} else if (NewBC)1589NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));1590else1591Op = PN->getIncomingValueForBlock(Pred);1592}1593}1594}15951596// Update any PHI nodes in the returning block to realize that we no1597// longer branch to them.1598BB->removePredecessor(Pred);1599UncondBranch->eraseFromParent();16001601if (DTU)1602DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}});16031604return cast<ReturnInst>(NewRet);1605}16061607Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,1608BasicBlock::iterator SplitBefore,1609bool Unreachable,1610MDNode *BranchWeights,1611DomTreeUpdater *DTU, LoopInfo *LI,1612BasicBlock *ThenBlock) {1613SplitBlockAndInsertIfThenElse(1614Cond, SplitBefore, &ThenBlock, /* ElseBlock */ nullptr,1615/* UnreachableThen */ Unreachable,1616/* UnreachableElse */ false, BranchWeights, DTU, LI);1617return ThenBlock->getTerminator();1618}16191620Instruction *llvm::SplitBlockAndInsertIfElse(Value *Cond,1621BasicBlock::iterator SplitBefore,1622bool Unreachable,1623MDNode *BranchWeights,1624DomTreeUpdater *DTU, LoopInfo *LI,1625BasicBlock *ElseBlock) {1626SplitBlockAndInsertIfThenElse(1627Cond, SplitBefore, /* ThenBlock */ nullptr, &ElseBlock,1628/* UnreachableThen */ false,1629/* UnreachableElse */ Unreachable, BranchWeights, DTU, LI);1630return ElseBlock->getTerminator();1631}16321633void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore,1634Instruction **ThenTerm,1635Instruction **ElseTerm,1636MDNode *BranchWeights,1637DomTreeUpdater *DTU, LoopInfo *LI) {1638BasicBlock *ThenBlock = nullptr;1639BasicBlock *ElseBlock = nullptr;1640SplitBlockAndInsertIfThenElse(1641Cond, SplitBefore, &ThenBlock, &ElseBlock, /* UnreachableThen */ false,1642/* UnreachableElse */ false, BranchWeights, DTU, LI);16431644*ThenTerm = ThenBlock->getTerminator();1645*ElseTerm = ElseBlock->getTerminator();1646}16471648void llvm::SplitBlockAndInsertIfThenElse(1649Value *Cond, BasicBlock::iterator SplitBefore, BasicBlock **ThenBlock,1650BasicBlock **ElseBlock, bool UnreachableThen, bool UnreachableElse,1651MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI) {1652assert((ThenBlock || ElseBlock) &&1653"At least one branch block must be created");1654assert((!UnreachableThen || !UnreachableElse) &&1655"Split block tail must be reachable");16561657SmallVector<DominatorTree::UpdateType, 8> Updates;1658SmallPtrSet<BasicBlock *, 8> UniqueOrigSuccessors;1659BasicBlock *Head = SplitBefore->getParent();1660if (DTU) {1661UniqueOrigSuccessors.insert(succ_begin(Head), succ_end(Head));1662Updates.reserve(4 + 2 * UniqueOrigSuccessors.size());1663}16641665LLVMContext &C = Head->getContext();1666BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);1667BasicBlock *TrueBlock = Tail;1668BasicBlock *FalseBlock = Tail;1669bool ThenToTailEdge = false;1670bool ElseToTailEdge = false;16711672// Encapsulate the logic around creation/insertion/etc of a new block.1673auto handleBlock = [&](BasicBlock **PBB, bool Unreachable, BasicBlock *&BB,1674bool &ToTailEdge) {1675if (PBB == nullptr)1676return; // Do not create/insert a block.16771678if (*PBB)1679BB = *PBB; // Caller supplied block, use it.1680else {1681// Create a new block.1682BB = BasicBlock::Create(C, "", Head->getParent(), Tail);1683if (Unreachable)1684(void)new UnreachableInst(C, BB);1685else {1686(void)BranchInst::Create(Tail, BB);1687ToTailEdge = true;1688}1689BB->getTerminator()->setDebugLoc(SplitBefore->getDebugLoc());1690// Pass the new block back to the caller.1691*PBB = BB;1692}1693};16941695handleBlock(ThenBlock, UnreachableThen, TrueBlock, ThenToTailEdge);1696handleBlock(ElseBlock, UnreachableElse, FalseBlock, ElseToTailEdge);16971698Instruction *HeadOldTerm = Head->getTerminator();1699BranchInst *HeadNewTerm =1700BranchInst::Create(/*ifTrue*/ TrueBlock, /*ifFalse*/ FalseBlock, Cond);1701HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);1702ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);17031704if (DTU) {1705Updates.emplace_back(DominatorTree::Insert, Head, TrueBlock);1706Updates.emplace_back(DominatorTree::Insert, Head, FalseBlock);1707if (ThenToTailEdge)1708Updates.emplace_back(DominatorTree::Insert, TrueBlock, Tail);1709if (ElseToTailEdge)1710Updates.emplace_back(DominatorTree::Insert, FalseBlock, Tail);1711for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)1712Updates.emplace_back(DominatorTree::Insert, Tail, UniqueOrigSuccessor);1713for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)1714Updates.emplace_back(DominatorTree::Delete, Head, UniqueOrigSuccessor);1715DTU->applyUpdates(Updates);1716}17171718if (LI) {1719if (Loop *L = LI->getLoopFor(Head); L) {1720if (ThenToTailEdge)1721L->addBasicBlockToLoop(TrueBlock, *LI);1722if (ElseToTailEdge)1723L->addBasicBlockToLoop(FalseBlock, *LI);1724L->addBasicBlockToLoop(Tail, *LI);1725}1726}1727}17281729std::pair<Instruction*, Value*>1730llvm::SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore) {1731BasicBlock *LoopPred = SplitBefore->getParent();1732BasicBlock *LoopBody = SplitBlock(SplitBefore->getParent(), SplitBefore);1733BasicBlock *LoopExit = SplitBlock(SplitBefore->getParent(), SplitBefore);17341735auto *Ty = End->getType();1736auto &DL = SplitBefore->getDataLayout();1737const unsigned Bitwidth = DL.getTypeSizeInBits(Ty);17381739IRBuilder<> Builder(LoopBody->getTerminator());1740auto *IV = Builder.CreatePHI(Ty, 2, "iv");1741auto *IVNext =1742Builder.CreateAdd(IV, ConstantInt::get(Ty, 1), IV->getName() + ".next",1743/*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);1744auto *IVCheck = Builder.CreateICmpEQ(IVNext, End,1745IV->getName() + ".check");1746Builder.CreateCondBr(IVCheck, LoopExit, LoopBody);1747LoopBody->getTerminator()->eraseFromParent();17481749// Populate the IV PHI.1750IV->addIncoming(ConstantInt::get(Ty, 0), LoopPred);1751IV->addIncoming(IVNext, LoopBody);17521753return std::make_pair(LoopBody->getFirstNonPHI(), IV);1754}17551756void llvm::SplitBlockAndInsertForEachLane(ElementCount EC,1757Type *IndexTy, Instruction *InsertBefore,1758std::function<void(IRBuilderBase&, Value*)> Func) {17591760IRBuilder<> IRB(InsertBefore);17611762if (EC.isScalable()) {1763Value *NumElements = IRB.CreateElementCount(IndexTy, EC);17641765auto [BodyIP, Index] =1766SplitBlockAndInsertSimpleForLoop(NumElements, InsertBefore);17671768IRB.SetInsertPoint(BodyIP);1769Func(IRB, Index);1770return;1771}17721773unsigned Num = EC.getFixedValue();1774for (unsigned Idx = 0; Idx < Num; ++Idx) {1775IRB.SetInsertPoint(InsertBefore);1776Func(IRB, ConstantInt::get(IndexTy, Idx));1777}1778}17791780void llvm::SplitBlockAndInsertForEachLane(1781Value *EVL, Instruction *InsertBefore,1782std::function<void(IRBuilderBase &, Value *)> Func) {17831784IRBuilder<> IRB(InsertBefore);1785Type *Ty = EVL->getType();17861787if (!isa<ConstantInt>(EVL)) {1788auto [BodyIP, Index] = SplitBlockAndInsertSimpleForLoop(EVL, InsertBefore);1789IRB.SetInsertPoint(BodyIP);1790Func(IRB, Index);1791return;1792}17931794unsigned Num = cast<ConstantInt>(EVL)->getZExtValue();1795for (unsigned Idx = 0; Idx < Num; ++Idx) {1796IRB.SetInsertPoint(InsertBefore);1797Func(IRB, ConstantInt::get(Ty, Idx));1798}1799}18001801BranchInst *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,1802BasicBlock *&IfFalse) {1803PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());1804BasicBlock *Pred1 = nullptr;1805BasicBlock *Pred2 = nullptr;18061807if (SomePHI) {1808if (SomePHI->getNumIncomingValues() != 2)1809return nullptr;1810Pred1 = SomePHI->getIncomingBlock(0);1811Pred2 = SomePHI->getIncomingBlock(1);1812} else {1813pred_iterator PI = pred_begin(BB), PE = pred_end(BB);1814if (PI == PE) // No predecessor1815return nullptr;1816Pred1 = *PI++;1817if (PI == PE) // Only one predecessor1818return nullptr;1819Pred2 = *PI++;1820if (PI != PE) // More than two predecessors1821return nullptr;1822}18231824// We can only handle branches. Other control flow will be lowered to1825// branches if possible anyway.1826BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());1827BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());1828if (!Pred1Br || !Pred2Br)1829return nullptr;18301831// Eliminate code duplication by ensuring that Pred1Br is conditional if1832// either are.1833if (Pred2Br->isConditional()) {1834// If both branches are conditional, we don't have an "if statement". In1835// reality, we could transform this case, but since the condition will be1836// required anyway, we stand no chance of eliminating it, so the xform is1837// probably not profitable.1838if (Pred1Br->isConditional())1839return nullptr;18401841std::swap(Pred1, Pred2);1842std::swap(Pred1Br, Pred2Br);1843}18441845if (Pred1Br->isConditional()) {1846// The only thing we have to watch out for here is to make sure that Pred21847// doesn't have incoming edges from other blocks. If it does, the condition1848// doesn't dominate BB.1849if (!Pred2->getSinglePredecessor())1850return nullptr;18511852// If we found a conditional branch predecessor, make sure that it branches1853// to BB and Pred2Br. If it doesn't, this isn't an "if statement".1854if (Pred1Br->getSuccessor(0) == BB &&1855Pred1Br->getSuccessor(1) == Pred2) {1856IfTrue = Pred1;1857IfFalse = Pred2;1858} else if (Pred1Br->getSuccessor(0) == Pred2 &&1859Pred1Br->getSuccessor(1) == BB) {1860IfTrue = Pred2;1861IfFalse = Pred1;1862} else {1863// We know that one arm of the conditional goes to BB, so the other must1864// go somewhere unrelated, and this must not be an "if statement".1865return nullptr;1866}18671868return Pred1Br;1869}18701871// Ok, if we got here, both predecessors end with an unconditional branch to1872// BB. Don't panic! If both blocks only have a single (identical)1873// predecessor, and THAT is a conditional branch, then we're all ok!1874BasicBlock *CommonPred = Pred1->getSinglePredecessor();1875if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())1876return nullptr;18771878// Otherwise, if this is a conditional branch, then we can use it!1879BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());1880if (!BI) return nullptr;18811882assert(BI->isConditional() && "Two successors but not conditional?");1883if (BI->getSuccessor(0) == Pred1) {1884IfTrue = Pred1;1885IfFalse = Pred2;1886} else {1887IfTrue = Pred2;1888IfFalse = Pred1;1889}1890return BI;1891}18921893// After creating a control flow hub, the operands of PHINodes in an outgoing1894// block Out no longer match the predecessors of that block. Predecessors of Out1895// that are incoming blocks to the hub are now replaced by just one edge from1896// the hub. To match this new control flow, the corresponding values from each1897// PHINode must now be moved a new PHINode in the first guard block of the hub.1898//1899// This operation cannot be performed with SSAUpdater, because it involves one1900// new use: If the block Out is in the list of Incoming blocks, then the newly1901// created PHI in the Hub will use itself along that edge from Out to Hub.1902static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,1903const SetVector<BasicBlock *> &Incoming,1904BasicBlock *FirstGuardBlock) {1905auto I = Out->begin();1906while (I != Out->end() && isa<PHINode>(I)) {1907auto Phi = cast<PHINode>(I);1908auto NewPhi =1909PHINode::Create(Phi->getType(), Incoming.size(),1910Phi->getName() + ".moved", FirstGuardBlock->begin());1911for (auto *In : Incoming) {1912Value *V = UndefValue::get(Phi->getType());1913if (In == Out) {1914V = NewPhi;1915} else if (Phi->getBasicBlockIndex(In) != -1) {1916V = Phi->removeIncomingValue(In, false);1917}1918NewPhi->addIncoming(V, In);1919}1920assert(NewPhi->getNumIncomingValues() == Incoming.size());1921if (Phi->getNumOperands() == 0) {1922Phi->replaceAllUsesWith(NewPhi);1923I = Phi->eraseFromParent();1924continue;1925}1926Phi->addIncoming(NewPhi, GuardBlock);1927++I;1928}1929}19301931using BBPredicates = DenseMap<BasicBlock *, Instruction *>;1932using BBSetVector = SetVector<BasicBlock *>;19331934// Redirects the terminator of the incoming block to the first guard1935// block in the hub. The condition of the original terminator (if it1936// was conditional) and its original successors are returned as a1937// tuple <condition, succ0, succ1>. The function additionally filters1938// out successors that are not in the set of outgoing blocks.1939//1940// - condition is non-null iff the branch is conditional.1941// - Succ1 is non-null iff the sole/taken target is an outgoing block.1942// - Succ2 is non-null iff condition is non-null and the fallthrough1943// target is an outgoing block.1944static std::tuple<Value *, BasicBlock *, BasicBlock *>1945redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock,1946const BBSetVector &Outgoing) {1947assert(isa<BranchInst>(BB->getTerminator()) &&1948"Only support branch terminator.");1949auto Branch = cast<BranchInst>(BB->getTerminator());1950auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr;19511952BasicBlock *Succ0 = Branch->getSuccessor(0);1953BasicBlock *Succ1 = nullptr;1954Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;19551956if (Branch->isUnconditional()) {1957Branch->setSuccessor(0, FirstGuardBlock);1958assert(Succ0);1959} else {1960Succ1 = Branch->getSuccessor(1);1961Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;1962assert(Succ0 || Succ1);1963if (Succ0 && !Succ1) {1964Branch->setSuccessor(0, FirstGuardBlock);1965} else if (Succ1 && !Succ0) {1966Branch->setSuccessor(1, FirstGuardBlock);1967} else {1968Branch->eraseFromParent();1969BranchInst::Create(FirstGuardBlock, BB);1970}1971}19721973assert(Succ0 || Succ1);1974return std::make_tuple(Condition, Succ0, Succ1);1975}1976// Setup the branch instructions for guard blocks.1977//1978// Each guard block terminates in a conditional branch that transfers1979// control to the corresponding outgoing block or the next guard1980// block. The last guard block has two outgoing blocks as successors1981// since the condition for the final outgoing block is trivially1982// true. So we create one less block (including the first guard block)1983// than the number of outgoing blocks.1984static void setupBranchForGuard(SmallVectorImpl<BasicBlock *> &GuardBlocks,1985const BBSetVector &Outgoing,1986BBPredicates &GuardPredicates) {1987// To help keep the loop simple, temporarily append the last1988// outgoing block to the list of guard blocks.1989GuardBlocks.push_back(Outgoing.back());19901991for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) {1992auto Out = Outgoing[i];1993assert(GuardPredicates.count(Out));1994BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out],1995GuardBlocks[i]);1996}19971998// Remove the last block from the guard list.1999GuardBlocks.pop_back();2000}20012002/// We are using one integer to represent the block we are branching to. Then at2003/// each guard block, the predicate was calcuated using a simple `icmp eq`.2004static void calcPredicateUsingInteger(2005const BBSetVector &Incoming, const BBSetVector &Outgoing,2006SmallVectorImpl<BasicBlock *> &GuardBlocks, BBPredicates &GuardPredicates) {2007auto &Context = Incoming.front()->getContext();2008auto FirstGuardBlock = GuardBlocks.front();20092010auto Phi = PHINode::Create(Type::getInt32Ty(Context), Incoming.size(),2011"merged.bb.idx", FirstGuardBlock);20122013for (auto In : Incoming) {2014Value *Condition;2015BasicBlock *Succ0;2016BasicBlock *Succ1;2017std::tie(Condition, Succ0, Succ1) =2018redirectToHub(In, FirstGuardBlock, Outgoing);2019Value *IncomingId = nullptr;2020if (Succ0 && Succ1) {2021// target_bb_index = Condition ? index_of_succ0 : index_of_succ1.2022auto Succ0Iter = find(Outgoing, Succ0);2023auto Succ1Iter = find(Outgoing, Succ1);2024Value *Id0 = ConstantInt::get(Type::getInt32Ty(Context),2025std::distance(Outgoing.begin(), Succ0Iter));2026Value *Id1 = ConstantInt::get(Type::getInt32Ty(Context),2027std::distance(Outgoing.begin(), Succ1Iter));2028IncomingId = SelectInst::Create(Condition, Id0, Id1, "target.bb.idx",2029In->getTerminator()->getIterator());2030} else {2031// Get the index of the non-null successor.2032auto SuccIter = Succ0 ? find(Outgoing, Succ0) : find(Outgoing, Succ1);2033IncomingId = ConstantInt::get(Type::getInt32Ty(Context),2034std::distance(Outgoing.begin(), SuccIter));2035}2036Phi->addIncoming(IncomingId, In);2037}20382039for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {2040auto Out = Outgoing[i];2041auto Cmp = ICmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, Phi,2042ConstantInt::get(Type::getInt32Ty(Context), i),2043Out->getName() + ".predicate", GuardBlocks[i]);2044GuardPredicates[Out] = Cmp;2045}2046}20472048/// We record the predicate of each outgoing block using a phi of boolean.2049static void calcPredicateUsingBooleans(2050const BBSetVector &Incoming, const BBSetVector &Outgoing,2051SmallVectorImpl<BasicBlock *> &GuardBlocks, BBPredicates &GuardPredicates,2052SmallVectorImpl<WeakVH> &DeletionCandidates) {2053auto &Context = Incoming.front()->getContext();2054auto BoolTrue = ConstantInt::getTrue(Context);2055auto BoolFalse = ConstantInt::getFalse(Context);2056auto FirstGuardBlock = GuardBlocks.front();20572058// The predicate for the last outgoing is trivially true, and so we2059// process only the first N-1 successors.2060for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {2061auto Out = Outgoing[i];2062LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n");20632064auto Phi =2065PHINode::Create(Type::getInt1Ty(Context), Incoming.size(),2066StringRef("Guard.") + Out->getName(), FirstGuardBlock);2067GuardPredicates[Out] = Phi;2068}20692070for (auto *In : Incoming) {2071Value *Condition;2072BasicBlock *Succ0;2073BasicBlock *Succ1;2074std::tie(Condition, Succ0, Succ1) =2075redirectToHub(In, FirstGuardBlock, Outgoing);20762077// Optimization: Consider an incoming block A with both successors2078// Succ0 and Succ1 in the set of outgoing blocks. The predicates2079// for Succ0 and Succ1 complement each other. If Succ0 is visited2080// first in the loop below, control will branch to Succ0 using the2081// corresponding predicate. But if that branch is not taken, then2082// control must reach Succ1, which means that the incoming value of2083// the predicate from `In` is true for Succ1.2084bool OneSuccessorDone = false;2085for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {2086auto Out = Outgoing[i];2087PHINode *Phi = cast<PHINode>(GuardPredicates[Out]);2088if (Out != Succ0 && Out != Succ1) {2089Phi->addIncoming(BoolFalse, In);2090} else if (!Succ0 || !Succ1 || OneSuccessorDone) {2091// Optimization: When only one successor is an outgoing block,2092// the incoming predicate from `In` is always true.2093Phi->addIncoming(BoolTrue, In);2094} else {2095assert(Succ0 && Succ1);2096if (Out == Succ0) {2097Phi->addIncoming(Condition, In);2098} else {2099auto Inverted = invertCondition(Condition);2100DeletionCandidates.push_back(Condition);2101Phi->addIncoming(Inverted, In);2102}2103OneSuccessorDone = true;2104}2105}2106}2107}21082109// Capture the existing control flow as guard predicates, and redirect2110// control flow from \p Incoming block through the \p GuardBlocks to the2111// \p Outgoing blocks.2112//2113// There is one guard predicate for each outgoing block OutBB. The2114// predicate represents whether the hub should transfer control flow2115// to OutBB. These predicates are NOT ORTHOGONAL. The Hub evaluates2116// them in the same order as the Outgoing set-vector, and control2117// branches to the first outgoing block whose predicate evaluates to true.2118static void2119convertToGuardPredicates(SmallVectorImpl<BasicBlock *> &GuardBlocks,2120SmallVectorImpl<WeakVH> &DeletionCandidates,2121const BBSetVector &Incoming,2122const BBSetVector &Outgoing, const StringRef Prefix,2123std::optional<unsigned> MaxControlFlowBooleans) {2124BBPredicates GuardPredicates;2125auto F = Incoming.front()->getParent();21262127for (int i = 0, e = Outgoing.size() - 1; i != e; ++i)2128GuardBlocks.push_back(2129BasicBlock::Create(F->getContext(), Prefix + ".guard", F));21302131// When we are using an integer to record which target block to jump to, we2132// are creating less live values, actually we are using one single integer to2133// store the index of the target block. When we are using booleans to store2134// the branching information, we need (N-1) boolean values, where N is the2135// number of outgoing block.2136if (!MaxControlFlowBooleans || Outgoing.size() <= *MaxControlFlowBooleans)2137calcPredicateUsingBooleans(Incoming, Outgoing, GuardBlocks, GuardPredicates,2138DeletionCandidates);2139else2140calcPredicateUsingInteger(Incoming, Outgoing, GuardBlocks, GuardPredicates);21412142setupBranchForGuard(GuardBlocks, Outgoing, GuardPredicates);2143}21442145BasicBlock *llvm::CreateControlFlowHub(2146DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,2147const BBSetVector &Incoming, const BBSetVector &Outgoing,2148const StringRef Prefix, std::optional<unsigned> MaxControlFlowBooleans) {2149if (Outgoing.size() < 2)2150return Outgoing.front();21512152SmallVector<DominatorTree::UpdateType, 16> Updates;2153if (DTU) {2154for (auto *In : Incoming) {2155for (auto Succ : successors(In))2156if (Outgoing.count(Succ))2157Updates.push_back({DominatorTree::Delete, In, Succ});2158}2159}21602161SmallVector<WeakVH, 8> DeletionCandidates;2162convertToGuardPredicates(GuardBlocks, DeletionCandidates, Incoming, Outgoing,2163Prefix, MaxControlFlowBooleans);2164auto FirstGuardBlock = GuardBlocks.front();21652166// Update the PHINodes in each outgoing block to match the new control flow.2167for (int i = 0, e = GuardBlocks.size(); i != e; ++i)2168reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock);21692170reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock);21712172if (DTU) {2173int NumGuards = GuardBlocks.size();2174assert((int)Outgoing.size() == NumGuards + 1);21752176for (auto In : Incoming)2177Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock});21782179for (int i = 0; i != NumGuards - 1; ++i) {2180Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]});2181Updates.push_back(2182{DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]});2183}2184Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],2185Outgoing[NumGuards - 1]});2186Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],2187Outgoing[NumGuards]});2188DTU->applyUpdates(Updates);2189}21902191for (auto I : DeletionCandidates) {2192if (I->use_empty())2193if (auto Inst = dyn_cast_or_null<Instruction>(I))2194Inst->eraseFromParent();2195}21962197return FirstGuardBlock;2198}21992200void llvm::InvertBranch(BranchInst *PBI, IRBuilderBase &Builder) {2201Value *NewCond = PBI->getCondition();2202// If this is a "cmp" instruction, only used for branching (and nowhere2203// else), then we can simply invert the predicate.2204if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {2205CmpInst *CI = cast<CmpInst>(NewCond);2206CI->setPredicate(CI->getInversePredicate());2207} else2208NewCond = Builder.CreateNot(NewCond, NewCond->getName() + ".not");22092210PBI->setCondition(NewCond);2211PBI->swapSuccessors();2212}22132214bool llvm::hasOnlySimpleTerminator(const Function &F) {2215for (auto &BB : F) {2216auto *Term = BB.getTerminator();2217if (!(isa<ReturnInst>(Term) || isa<UnreachableInst>(Term) ||2218isa<BranchInst>(Term)))2219return false;2220}2221return true;2222}22232224bool llvm::isPresplitCoroSuspendExitEdge(const BasicBlock &Src,2225const BasicBlock &Dest) {2226assert(Src.getParent() == Dest.getParent());2227if (!Src.getParent()->isPresplitCoroutine())2228return false;2229if (auto *SW = dyn_cast<SwitchInst>(Src.getTerminator()))2230if (auto *Intr = dyn_cast<IntrinsicInst>(SW->getCondition()))2231return Intr->getIntrinsicID() == Intrinsic::coro_suspend &&2232SW->getDefaultDest() == &Dest;2233return false;2234}223522362237