Path: blob/main/contrib/llvm-project/llvm/lib/CodeGen/BranchFolding.h
35233 views
//===- BranchFolding.h - Fold machine code branch instructions --*- C++ -*-===//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//===----------------------------------------------------------------------===//78#ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H9#define LLVM_LIB_CODEGEN_BRANCHFOLDING_H1011#include "llvm/ADT/DenseMap.h"12#include "llvm/ADT/SmallPtrSet.h"13#include "llvm/CodeGen/LivePhysRegs.h"14#include "llvm/CodeGen/MachineBasicBlock.h"15#include "llvm/Support/Compiler.h"16#include <vector>1718namespace llvm {1920class BasicBlock;21class MachineBranchProbabilityInfo;22class MachineFunction;23class MachineLoopInfo;24class MachineRegisterInfo;25class MBFIWrapper;26class ProfileSummaryInfo;27class TargetInstrInfo;28class TargetRegisterInfo;2930class LLVM_LIBRARY_VISIBILITY BranchFolder {31public:32explicit BranchFolder(bool DefaultEnableTailMerge, bool CommonHoist,33MBFIWrapper &FreqInfo,34const MachineBranchProbabilityInfo &ProbInfo,35ProfileSummaryInfo *PSI,36// Min tail length to merge. Defaults to commandline37// flag. Ignored for optsize.38unsigned MinTailLength = 0);3940/// Perhaps branch folding, tail merging and other CFG optimizations on the41/// given function. Block placement changes the layout and may create new42/// tail merging opportunities.43bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,44const TargetRegisterInfo *tri,45MachineLoopInfo *mli = nullptr,46bool AfterPlacement = false);4748private:49class MergePotentialsElt {50unsigned Hash;51MachineBasicBlock *Block;52DebugLoc BranchDebugLoc;5354public:55MergePotentialsElt(unsigned h, MachineBasicBlock *b, DebugLoc bdl)56: Hash(h), Block(b), BranchDebugLoc(std::move(bdl)) {}5758unsigned getHash() const { return Hash; }59MachineBasicBlock *getBlock() const { return Block; }6061void setBlock(MachineBasicBlock *MBB) {62Block = MBB;63}6465const DebugLoc &getBranchDebugLoc() { return BranchDebugLoc; }6667bool operator<(const MergePotentialsElt &) const;68};6970using MPIterator = std::vector<MergePotentialsElt>::iterator;7172std::vector<MergePotentialsElt> MergePotentials;73SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;74DenseMap<const MachineBasicBlock *, int> EHScopeMembership;7576class SameTailElt {77MPIterator MPIter;78MachineBasicBlock::iterator TailStartPos;7980public:81SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)82: MPIter(mp), TailStartPos(tsp) {}8384MPIterator getMPIter() const {85return MPIter;86}8788MergePotentialsElt &getMergePotentialsElt() const {89return *getMPIter();90}9192MachineBasicBlock::iterator getTailStartPos() const {93return TailStartPos;94}9596unsigned getHash() const {97return getMergePotentialsElt().getHash();98}99100MachineBasicBlock *getBlock() const {101return getMergePotentialsElt().getBlock();102}103104bool tailIsWholeBlock() const {105return TailStartPos == getBlock()->begin();106}107108void setBlock(MachineBasicBlock *MBB) {109getMergePotentialsElt().setBlock(MBB);110}111112void setTailStartPos(MachineBasicBlock::iterator Pos) {113TailStartPos = Pos;114}115};116std::vector<SameTailElt> SameTails;117118bool AfterBlockPlacement = false;119bool EnableTailMerge = false;120bool EnableHoistCommonCode = false;121bool UpdateLiveIns = false;122unsigned MinCommonTailLength;123const TargetInstrInfo *TII = nullptr;124const MachineRegisterInfo *MRI = nullptr;125const TargetRegisterInfo *TRI = nullptr;126MachineLoopInfo *MLI = nullptr;127LivePhysRegs LiveRegs;128129private:130MBFIWrapper &MBBFreqInfo;131const MachineBranchProbabilityInfo &MBPI;132ProfileSummaryInfo *PSI;133134bool TailMergeBlocks(MachineFunction &MF);135bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,136MachineBasicBlock* PredBB,137unsigned MinCommonTailLength);138void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);139140/// Delete the instruction OldInst and everything after it, replacing it141/// with an unconditional branch to NewDest.142void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,143MachineBasicBlock &NewDest);144145/// Given a machine basic block and an iterator into it, split the MBB so146/// that the part before the iterator falls into the part starting at the147/// iterator. This returns the new MBB.148MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,149MachineBasicBlock::iterator BBI1,150const BasicBlock *BB);151152/// Look through all the blocks in MergePotentials that have hash CurHash153/// (guaranteed to match the last element). Build the vector SameTails of154/// all those that have the (same) largest number of instructions in common155/// of any pair of these blocks. SameTails entries contain an iterator into156/// MergePotentials (from which the MachineBasicBlock can be found) and a157/// MachineBasicBlock::iterator into that MBB indicating the instruction158/// where the matching code sequence begins. Order of elements in SameTails159/// is the reverse of the order in which those blocks appear in160/// MergePotentials (where they are not necessarily consecutive).161unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,162MachineBasicBlock *SuccBB,163MachineBasicBlock *PredBB);164165/// Remove all blocks with hash CurHash from MergePotentials, restoring166/// branches at ends of blocks as appropriate.167void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock *SuccBB,168MachineBasicBlock *PredBB,169const DebugLoc &BranchDL);170171/// None of the blocks to be tail-merged consist only of the common tail.172/// Create a block that does by splitting one.173bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,174MachineBasicBlock *SuccBB,175unsigned maxCommonTailLength,176unsigned &commonTailIndex);177178/// Create merged DebugLocs of identical instructions across SameTails and179/// assign it to the instruction in common tail; merge MMOs and undef flags.180void mergeCommonTails(unsigned commonTailIndex);181182bool OptimizeBranches(MachineFunction &MF);183184/// Analyze and optimize control flow related to the specified block. This185/// is never called on the entry block.186bool OptimizeBlock(MachineBasicBlock *MBB);187188/// Remove the specified dead machine basic block from the function,189/// updating the CFG.190void RemoveDeadBlock(MachineBasicBlock *MBB);191192/// Hoist common instruction sequences at the start of basic blocks to their193/// common predecessor.194bool HoistCommonCode(MachineFunction &MF);195196/// If the successors of MBB has common instruction sequence at the start of197/// the function, move the instructions before MBB terminator if it's legal.198bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);199};200201} // end namespace llvm202203#endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H204205206