Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Utils/CodeMoverUtils.cpp
35271 views
//===- CodeMoverUtils.cpp - CodeMover 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 movements on basic blocks, and instructions9// contained within a function.10//11//===----------------------------------------------------------------------===//1213#include "llvm/Transforms/Utils/CodeMoverUtils.h"14#include "llvm/ADT/Statistic.h"15#include "llvm/Analysis/DependenceAnalysis.h"16#include "llvm/Analysis/PostDominators.h"17#include "llvm/Analysis/ValueTracking.h"18#include "llvm/IR/Dominators.h"1920using namespace llvm;2122#define DEBUG_TYPE "codemover-utils"2324STATISTIC(HasDependences,25"Cannot move across instructions that has memory dependences");26STATISTIC(MayThrowException, "Cannot move across instructions that may throw");27STATISTIC(NotControlFlowEquivalent,28"Instructions are not control flow equivalent");29STATISTIC(NotMovedPHINode, "Movement of PHINodes are not supported");30STATISTIC(NotMovedTerminator, "Movement of Terminator are not supported");3132namespace {33/// Represent a control condition. A control condition is a condition of a34/// terminator to decide which successors to execute. The pointer field35/// represents the address of the condition of the terminator. The integer field36/// is a bool, it is true when the basic block is executed when V is true. For37/// example, `br %cond, bb0, bb1` %cond is a control condition of bb0 with the38/// integer field equals to true, while %cond is a control condition of bb1 with39/// the integer field equals to false.40using ControlCondition = PointerIntPair<Value *, 1, bool>;41#ifndef NDEBUG42raw_ostream &operator<<(raw_ostream &OS, const ControlCondition &C) {43OS << "[" << *C.getPointer() << ", " << (C.getInt() ? "true" : "false")44<< "]";45return OS;46}47#endif4849/// Represent a set of control conditions required to execute ToBB from FromBB.50class ControlConditions {51using ConditionVectorTy = SmallVector<ControlCondition, 6>;5253/// A SmallVector of control conditions.54ConditionVectorTy Conditions;5556public:57/// Return a ControlConditions which stores all conditions required to execute58/// \p BB from \p Dominator. If \p MaxLookup is non-zero, it limits the59/// number of conditions to collect. Return std::nullopt if not all conditions60/// are collected successfully, or we hit the limit.61static const std::optional<ControlConditions>62collectControlConditions(const BasicBlock &BB, const BasicBlock &Dominator,63const DominatorTree &DT,64const PostDominatorTree &PDT,65unsigned MaxLookup = 6);6667/// Return true if there exists no control conditions required to execute ToBB68/// from FromBB.69bool isUnconditional() const { return Conditions.empty(); }7071/// Return a constant reference of Conditions.72const ConditionVectorTy &getControlConditions() const { return Conditions; }7374/// Add \p V as one of the ControlCondition in Condition with IsTrueCondition75/// equals to \p True. Return true if inserted successfully.76bool addControlCondition(ControlCondition C);7778/// Return true if for all control conditions in Conditions, there exists an79/// equivalent control condition in \p Other.Conditions.80bool isEquivalent(const ControlConditions &Other) const;8182/// Return true if \p C1 and \p C2 are equivalent.83static bool isEquivalent(const ControlCondition &C1,84const ControlCondition &C2);8586private:87ControlConditions() = default;8889static bool isEquivalent(const Value &V1, const Value &V2);90static bool isInverse(const Value &V1, const Value &V2);91};92} // namespace9394static bool domTreeLevelBefore(DominatorTree *DT, const Instruction *InstA,95const Instruction *InstB) {96// Use ordered basic block in case the 2 instructions are in the same97// block.98if (InstA->getParent() == InstB->getParent())99return InstA->comesBefore(InstB);100101DomTreeNode *DA = DT->getNode(InstA->getParent());102DomTreeNode *DB = DT->getNode(InstB->getParent());103return DA->getLevel() < DB->getLevel();104}105106const std::optional<ControlConditions>107ControlConditions::collectControlConditions(const BasicBlock &BB,108const BasicBlock &Dominator,109const DominatorTree &DT,110const PostDominatorTree &PDT,111unsigned MaxLookup) {112assert(DT.dominates(&Dominator, &BB) && "Expecting Dominator to dominate BB");113114ControlConditions Conditions;115unsigned NumConditions = 0;116117// BB is executed unconditional from itself.118if (&Dominator == &BB)119return Conditions;120121const BasicBlock *CurBlock = &BB;122// Walk up the dominator tree from the associated DT node for BB to the123// associated DT node for Dominator.124do {125assert(DT.getNode(CurBlock) && "Expecting a valid DT node for CurBlock");126BasicBlock *IDom = DT.getNode(CurBlock)->getIDom()->getBlock();127assert(DT.dominates(&Dominator, IDom) &&128"Expecting Dominator to dominate IDom");129130// Limitation: can only handle branch instruction currently.131const BranchInst *BI = dyn_cast<BranchInst>(IDom->getTerminator());132if (!BI)133return std::nullopt;134135bool Inserted = false;136if (PDT.dominates(CurBlock, IDom)) {137LLVM_DEBUG(dbgs() << CurBlock->getName()138<< " is executed unconditionally from "139<< IDom->getName() << "\n");140} else if (PDT.dominates(CurBlock, BI->getSuccessor(0))) {141LLVM_DEBUG(dbgs() << CurBlock->getName() << " is executed when \""142<< *BI->getCondition() << "\" is true from "143<< IDom->getName() << "\n");144Inserted = Conditions.addControlCondition(145ControlCondition(BI->getCondition(), true));146} else if (PDT.dominates(CurBlock, BI->getSuccessor(1))) {147LLVM_DEBUG(dbgs() << CurBlock->getName() << " is executed when \""148<< *BI->getCondition() << "\" is false from "149<< IDom->getName() << "\n");150Inserted = Conditions.addControlCondition(151ControlCondition(BI->getCondition(), false));152} else153return std::nullopt;154155if (Inserted)156++NumConditions;157158if (MaxLookup != 0 && NumConditions > MaxLookup)159return std::nullopt;160161CurBlock = IDom;162} while (CurBlock != &Dominator);163164return Conditions;165}166167bool ControlConditions::addControlCondition(ControlCondition C) {168bool Inserted = false;169if (none_of(Conditions, [&](ControlCondition &Exists) {170return ControlConditions::isEquivalent(C, Exists);171})) {172Conditions.push_back(C);173Inserted = true;174}175176LLVM_DEBUG(dbgs() << (Inserted ? "Inserted " : "Not inserted ") << C << "\n");177return Inserted;178}179180bool ControlConditions::isEquivalent(const ControlConditions &Other) const {181if (Conditions.empty() && Other.Conditions.empty())182return true;183184if (Conditions.size() != Other.Conditions.size())185return false;186187return all_of(Conditions, [&](const ControlCondition &C) {188return any_of(Other.Conditions, [&](const ControlCondition &OtherC) {189return ControlConditions::isEquivalent(C, OtherC);190});191});192}193194bool ControlConditions::isEquivalent(const ControlCondition &C1,195const ControlCondition &C2) {196if (C1.getInt() == C2.getInt()) {197if (isEquivalent(*C1.getPointer(), *C2.getPointer()))198return true;199} else if (isInverse(*C1.getPointer(), *C2.getPointer()))200return true;201202return false;203}204205// FIXME: Use SCEV and reuse GVN/CSE logic to check for equivalence between206// Values.207// Currently, isEquivalent rely on other passes to ensure equivalent conditions208// have the same value, e.g. GVN.209bool ControlConditions::isEquivalent(const Value &V1, const Value &V2) {210return &V1 == &V2;211}212213bool ControlConditions::isInverse(const Value &V1, const Value &V2) {214if (const CmpInst *Cmp1 = dyn_cast<CmpInst>(&V1))215if (const CmpInst *Cmp2 = dyn_cast<CmpInst>(&V2)) {216if (Cmp1->getPredicate() == Cmp2->getInversePredicate() &&217Cmp1->getOperand(0) == Cmp2->getOperand(0) &&218Cmp1->getOperand(1) == Cmp2->getOperand(1))219return true;220221if (Cmp1->getPredicate() ==222CmpInst::getSwappedPredicate(Cmp2->getInversePredicate()) &&223Cmp1->getOperand(0) == Cmp2->getOperand(1) &&224Cmp1->getOperand(1) == Cmp2->getOperand(0))225return true;226}227return false;228}229230bool llvm::isControlFlowEquivalent(const Instruction &I0, const Instruction &I1,231const DominatorTree &DT,232const PostDominatorTree &PDT) {233return isControlFlowEquivalent(*I0.getParent(), *I1.getParent(), DT, PDT);234}235236bool llvm::isControlFlowEquivalent(const BasicBlock &BB0, const BasicBlock &BB1,237const DominatorTree &DT,238const PostDominatorTree &PDT) {239if (&BB0 == &BB1)240return true;241242if ((DT.dominates(&BB0, &BB1) && PDT.dominates(&BB1, &BB0)) ||243(PDT.dominates(&BB0, &BB1) && DT.dominates(&BB1, &BB0)))244return true;245246// If the set of conditions required to execute BB0 and BB1 from their common247// dominator are the same, then BB0 and BB1 are control flow equivalent.248const BasicBlock *CommonDominator = DT.findNearestCommonDominator(&BB0, &BB1);249LLVM_DEBUG(dbgs() << "The nearest common dominator of " << BB0.getName()250<< " and " << BB1.getName() << " is "251<< CommonDominator->getName() << "\n");252253const std::optional<ControlConditions> BB0Conditions =254ControlConditions::collectControlConditions(BB0, *CommonDominator, DT,255PDT);256if (BB0Conditions == std::nullopt)257return false;258259const std::optional<ControlConditions> BB1Conditions =260ControlConditions::collectControlConditions(BB1, *CommonDominator, DT,261PDT);262if (BB1Conditions == std::nullopt)263return false;264265return BB0Conditions->isEquivalent(*BB1Conditions);266}267268static bool reportInvalidCandidate(const Instruction &I,269llvm::Statistic &Stat) {270++Stat;271LLVM_DEBUG(dbgs() << "Unable to move instruction: " << I << ". "272<< Stat.getDesc());273return false;274}275276/// Collect all instructions in between \p StartInst and \p EndInst, and store277/// them in \p InBetweenInsts.278static void279collectInstructionsInBetween(Instruction &StartInst, const Instruction &EndInst,280SmallPtrSetImpl<Instruction *> &InBetweenInsts) {281assert(InBetweenInsts.empty() && "Expecting InBetweenInsts to be empty");282283/// Get the next instructions of \p I, and push them to \p WorkList.284auto getNextInsts = [](Instruction &I,285SmallPtrSetImpl<Instruction *> &WorkList) {286if (Instruction *NextInst = I.getNextNode())287WorkList.insert(NextInst);288else {289assert(I.isTerminator() && "Expecting a terminator instruction");290for (BasicBlock *Succ : successors(&I))291WorkList.insert(&Succ->front());292}293};294295SmallPtrSet<Instruction *, 10> WorkList;296getNextInsts(StartInst, WorkList);297while (!WorkList.empty()) {298Instruction *CurInst = *WorkList.begin();299WorkList.erase(CurInst);300301if (CurInst == &EndInst)302continue;303304if (!InBetweenInsts.insert(CurInst).second)305continue;306307getNextInsts(*CurInst, WorkList);308}309}310311bool llvm::isSafeToMoveBefore(Instruction &I, Instruction &InsertPoint,312DominatorTree &DT, const PostDominatorTree *PDT,313DependenceInfo *DI, bool CheckForEntireBlock) {314// Skip tests when we don't have PDT or DI315if (!PDT || !DI)316return false;317318// Cannot move itself before itself.319if (&I == &InsertPoint)320return false;321322// Not moved.323if (I.getNextNode() == &InsertPoint)324return true;325326if (isa<PHINode>(I) || isa<PHINode>(InsertPoint))327return reportInvalidCandidate(I, NotMovedPHINode);328329if (I.isTerminator())330return reportInvalidCandidate(I, NotMovedTerminator);331332// TODO remove this limitation.333if (!isControlFlowEquivalent(I, InsertPoint, DT, *PDT))334return reportInvalidCandidate(I, NotControlFlowEquivalent);335336if (isReachedBefore(&I, &InsertPoint, &DT, PDT))337for (const Use &U : I.uses())338if (auto *UserInst = dyn_cast<Instruction>(U.getUser())) {339// If InsertPoint is in a BB that comes after I, then we cannot move if340// I is used in the terminator of the current BB.341if (I.getParent() == InsertPoint.getParent() &&342UserInst == I.getParent()->getTerminator())343return false;344if (UserInst != &InsertPoint && !DT.dominates(&InsertPoint, U)) {345// If UserInst is an instruction that appears later in the same BB as346// I, then it is okay to move since I will still be available when347// UserInst is executed.348if (CheckForEntireBlock && I.getParent() == UserInst->getParent() &&349DT.dominates(&I, UserInst))350continue;351return false;352}353}354if (isReachedBefore(&InsertPoint, &I, &DT, PDT))355for (const Value *Op : I.operands())356if (auto *OpInst = dyn_cast<Instruction>(Op)) {357if (&InsertPoint == OpInst)358return false;359// If OpInst is an instruction that appears earlier in the same BB as360// I, then it is okay to move since OpInst will still be available.361if (CheckForEntireBlock && I.getParent() == OpInst->getParent() &&362DT.dominates(OpInst, &I))363continue;364if (!DT.dominates(OpInst, &InsertPoint))365return false;366}367368DT.updateDFSNumbers();369const bool MoveForward = domTreeLevelBefore(&DT, &I, &InsertPoint);370Instruction &StartInst = (MoveForward ? I : InsertPoint);371Instruction &EndInst = (MoveForward ? InsertPoint : I);372SmallPtrSet<Instruction *, 10> InstsToCheck;373collectInstructionsInBetween(StartInst, EndInst, InstsToCheck);374if (!MoveForward)375InstsToCheck.insert(&InsertPoint);376377// Check if there exists instructions which may throw, may synchonize, or may378// never return, from I to InsertPoint.379if (!isSafeToSpeculativelyExecute(&I))380if (llvm::any_of(InstsToCheck, [](Instruction *I) {381if (I->mayThrow())382return true;383384const CallBase *CB = dyn_cast<CallBase>(I);385if (!CB)386return false;387if (!CB->hasFnAttr(Attribute::WillReturn))388return true;389if (!CB->hasFnAttr(Attribute::NoSync))390return true;391392return false;393})) {394return reportInvalidCandidate(I, MayThrowException);395}396397// Check if I has any output/flow/anti dependences with instructions from \p398// StartInst to \p EndInst.399if (llvm::any_of(InstsToCheck, [&DI, &I](Instruction *CurInst) {400auto DepResult = DI->depends(&I, CurInst, true);401if (DepResult && (DepResult->isOutput() || DepResult->isFlow() ||402DepResult->isAnti()))403return true;404return false;405}))406return reportInvalidCandidate(I, HasDependences);407408return true;409}410411bool llvm::isSafeToMoveBefore(BasicBlock &BB, Instruction &InsertPoint,412DominatorTree &DT, const PostDominatorTree *PDT,413DependenceInfo *DI) {414return llvm::all_of(BB, [&](Instruction &I) {415if (BB.getTerminator() == &I)416return true;417418return isSafeToMoveBefore(I, InsertPoint, DT, PDT, DI,419/*CheckForEntireBlock=*/true);420});421}422423void llvm::moveInstructionsToTheBeginning(BasicBlock &FromBB, BasicBlock &ToBB,424DominatorTree &DT,425const PostDominatorTree &PDT,426DependenceInfo &DI) {427for (Instruction &I :428llvm::make_early_inc_range(llvm::drop_begin(llvm::reverse(FromBB)))) {429Instruction *MovePos = ToBB.getFirstNonPHIOrDbg();430431if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))432I.moveBeforePreserving(MovePos);433}434}435436void llvm::moveInstructionsToTheEnd(BasicBlock &FromBB, BasicBlock &ToBB,437DominatorTree &DT,438const PostDominatorTree &PDT,439DependenceInfo &DI) {440Instruction *MovePos = ToBB.getTerminator();441while (FromBB.size() > 1) {442Instruction &I = FromBB.front();443if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))444I.moveBeforePreserving(MovePos);445}446}447448bool llvm::nonStrictlyPostDominate(const BasicBlock *ThisBlock,449const BasicBlock *OtherBlock,450const DominatorTree *DT,451const PostDominatorTree *PDT) {452assert(isControlFlowEquivalent(*ThisBlock, *OtherBlock, *DT, *PDT) &&453"ThisBlock and OtherBlock must be CFG equivalent!");454const BasicBlock *CommonDominator =455DT->findNearestCommonDominator(ThisBlock, OtherBlock);456if (CommonDominator == nullptr)457return false;458459/// Recursively check the predecessors of \p ThisBlock up to460/// their common dominator, and see if any of them post-dominates461/// \p OtherBlock.462SmallVector<const BasicBlock *, 8> WorkList;463SmallPtrSet<const BasicBlock *, 8> Visited;464WorkList.push_back(ThisBlock);465while (!WorkList.empty()) {466const BasicBlock *CurBlock = WorkList.back();467WorkList.pop_back();468Visited.insert(CurBlock);469if (PDT->dominates(CurBlock, OtherBlock))470return true;471472for (const auto *Pred : predecessors(CurBlock)) {473if (Pred == CommonDominator || Visited.count(Pred))474continue;475WorkList.push_back(Pred);476}477}478return false;479}480481bool llvm::isReachedBefore(const Instruction *I0, const Instruction *I1,482const DominatorTree *DT,483const PostDominatorTree *PDT) {484const BasicBlock *BB0 = I0->getParent();485const BasicBlock *BB1 = I1->getParent();486if (BB0 == BB1)487return DT->dominates(I0, I1);488489return nonStrictlyPostDominate(BB1, BB0, DT, PDT);490}491492493