2017-10-11 06:33:29 +08:00
|
|
|
//===- BranchFolding.h - Fold machine code branch instructions --*- C++ -*-===//
|
2009-09-04 15:47:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-09-04 15:47:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_BRANCHFOLDING_H
|
|
|
|
#define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2011-06-14 23:31:54 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2016-07-13 02:44:33 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2009-09-04 15:47:40 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2014-08-08 03:30:13 +08:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2017-10-11 06:33:29 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include <cstdint>
|
2009-09-04 15:47:40 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
2017-10-11 06:33:29 +08:00
|
|
|
|
|
|
|
class BasicBlock;
|
|
|
|
class MachineBlockFrequencyInfo;
|
|
|
|
class MachineBranchProbabilityInfo;
|
|
|
|
class MachineFunction;
|
|
|
|
class MachineLoopInfo;
|
|
|
|
class MachineModuleInfo;
|
|
|
|
class MachineRegisterInfo;
|
|
|
|
class raw_ostream;
|
|
|
|
class TargetInstrInfo;
|
|
|
|
class TargetRegisterInfo;
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2015-07-01 22:47:39 +08:00
|
|
|
class LLVM_LIBRARY_VISIBILITY BranchFolder {
|
2009-09-04 15:47:40 +08:00
|
|
|
public:
|
2016-06-09 23:24:29 +08:00
|
|
|
class MBFIWrapper;
|
|
|
|
|
2016-08-19 02:57:29 +08:00
|
|
|
explicit BranchFolder(bool defaultEnableTailMerge,
|
|
|
|
bool CommonHoist,
|
2018-07-17 02:51:40 +08:00
|
|
|
MBFIWrapper &FreqInfo,
|
|
|
|
const MachineBranchProbabilityInfo &ProbInfo,
|
2016-08-19 02:57:29 +08:00
|
|
|
// Min tail length to merge. Defaults to commandline
|
|
|
|
// flag. Ignored for optsize.
|
2018-07-17 02:51:40 +08:00
|
|
|
unsigned MinTailLength = 0);
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2017-03-15 14:29:23 +08:00
|
|
|
/// Perhaps branch folding, tail merging and other CFG optimizations on the
|
|
|
|
/// given function. Block placement changes the layout and may create new
|
|
|
|
/// tail merging opportunities.
|
2016-06-09 23:24:29 +08:00
|
|
|
bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii,
|
|
|
|
const TargetRegisterInfo *tri, MachineModuleInfo *mmi,
|
|
|
|
MachineLoopInfo *mli = nullptr,
|
|
|
|
bool AfterPlacement = false);
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
private:
|
2009-11-12 05:57:02 +08:00
|
|
|
class MergePotentialsElt {
|
|
|
|
unsigned Hash;
|
|
|
|
MachineBasicBlock *Block;
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
public:
|
|
|
|
MergePotentialsElt(unsigned h, MachineBasicBlock *b)
|
|
|
|
: Hash(h), Block(b) {}
|
|
|
|
|
|
|
|
unsigned getHash() const { return Hash; }
|
|
|
|
MachineBasicBlock *getBlock() const { return Block; }
|
|
|
|
|
|
|
|
void setBlock(MachineBasicBlock *MBB) {
|
|
|
|
Block = MBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const MergePotentialsElt &) const;
|
|
|
|
};
|
2017-10-11 06:33:29 +08:00
|
|
|
|
|
|
|
using MPIterator = std::vector<MergePotentialsElt>::iterator;
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
std::vector<MergePotentialsElt> MergePotentials;
|
2011-06-14 23:31:54 +08:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 2> TriedMerging;
|
2018-06-01 08:03:21 +08:00
|
|
|
DenseMap<const MachineBasicBlock *, int> EHScopeMembership;
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
class SameTailElt {
|
|
|
|
MPIterator MPIter;
|
|
|
|
MachineBasicBlock::iterator TailStartPos;
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
public:
|
|
|
|
SameTailElt(MPIterator mp, MachineBasicBlock::iterator tsp)
|
|
|
|
: MPIter(mp), TailStartPos(tsp) {}
|
|
|
|
|
|
|
|
MPIterator getMPIter() const {
|
|
|
|
return MPIter;
|
|
|
|
}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
MergePotentialsElt &getMergePotentialsElt() const {
|
|
|
|
return *getMPIter();
|
|
|
|
}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
MachineBasicBlock::iterator getTailStartPos() const {
|
|
|
|
return TailStartPos;
|
|
|
|
}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
unsigned getHash() const {
|
|
|
|
return getMergePotentialsElt().getHash();
|
|
|
|
}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
MachineBasicBlock *getBlock() const {
|
|
|
|
return getMergePotentialsElt().getBlock();
|
|
|
|
}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
bool tailIsWholeBlock() const {
|
|
|
|
return TailStartPos == getBlock()->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setBlock(MachineBasicBlock *MBB) {
|
|
|
|
getMergePotentialsElt().setBlock(MBB);
|
|
|
|
}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2009-11-12 05:57:02 +08:00
|
|
|
void setTailStartPos(MachineBasicBlock::iterator Pos) {
|
|
|
|
TailStartPos = Pos;
|
|
|
|
}
|
|
|
|
};
|
2009-09-04 15:47:40 +08:00
|
|
|
std::vector<SameTailElt> SameTails;
|
|
|
|
|
2016-06-09 23:24:29 +08:00
|
|
|
bool AfterBlockPlacement;
|
2009-09-04 15:47:40 +08:00
|
|
|
bool EnableTailMerge;
|
2011-05-12 08:56:58 +08:00
|
|
|
bool EnableHoistCommonCode;
|
2016-07-13 02:44:33 +08:00
|
|
|
bool UpdateLiveIns;
|
2016-08-19 02:57:29 +08:00
|
|
|
unsigned MinCommonTailLength;
|
2009-09-04 15:47:40 +08:00
|
|
|
const TargetInstrInfo *TII;
|
2017-05-26 14:32:31 +08:00
|
|
|
const MachineRegisterInfo *MRI;
|
2009-09-04 15:47:40 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
MachineModuleInfo *MMI;
|
2016-06-09 23:24:29 +08:00
|
|
|
MachineLoopInfo *MLI;
|
2016-07-13 02:44:33 +08:00
|
|
|
LivePhysRegs LiveRegs;
|
2009-09-04 15:47:40 +08:00
|
|
|
|
2016-06-09 23:24:29 +08:00
|
|
|
public:
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This class keeps track of branch frequencies of newly created
|
2014-08-08 03:30:13 +08:00
|
|
|
/// blocks and tail-merged blocks.
|
|
|
|
class MBFIWrapper {
|
|
|
|
public:
|
|
|
|
MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
|
2017-10-11 06:33:29 +08:00
|
|
|
|
2014-08-08 03:30:13 +08:00
|
|
|
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
|
|
|
|
void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
|
2016-06-09 23:24:29 +08:00
|
|
|
raw_ostream &printBlockFreq(raw_ostream &OS,
|
|
|
|
const MachineBasicBlock *MBB) const;
|
|
|
|
raw_ostream &printBlockFreq(raw_ostream &OS,
|
|
|
|
const BlockFrequency Freq) const;
|
2017-02-16 03:21:04 +08:00
|
|
|
void view(const Twine &Name, bool isSimple = true);
|
2017-02-01 07:48:32 +08:00
|
|
|
uint64_t getEntryFreq() const;
|
2014-08-08 03:30:13 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
const MachineBlockFrequencyInfo &MBFI;
|
|
|
|
DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
|
|
|
|
};
|
|
|
|
|
2016-06-09 23:24:29 +08:00
|
|
|
private:
|
|
|
|
MBFIWrapper &MBBFreqInfo;
|
2014-08-08 03:30:13 +08:00
|
|
|
const MachineBranchProbabilityInfo &MBPI;
|
|
|
|
|
2009-09-04 15:47:40 +08:00
|
|
|
bool TailMergeBlocks(MachineFunction &MF);
|
2009-11-12 03:49:34 +08:00
|
|
|
bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
|
2016-08-19 02:57:29 +08:00
|
|
|
MachineBasicBlock* PredBB,
|
|
|
|
unsigned MinCommonTailLength);
|
2014-08-08 03:30:13 +08:00
|
|
|
void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// Delete the instruction OldInst and everything after it, replacing it
|
|
|
|
/// with an unconditional branch to NewDest.
|
2017-09-07 04:45:24 +08:00
|
|
|
void replaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
|
|
|
|
MachineBasicBlock &NewDest);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// Given a machine basic block and an iterator into it, split the MBB so
|
|
|
|
/// that the part before the iterator falls into the part starting at the
|
|
|
|
/// iterator. This returns the new MBB.
|
2009-09-04 15:47:40 +08:00
|
|
|
MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
|
2013-06-24 09:55:01 +08:00
|
|
|
MachineBasicBlock::iterator BBI1,
|
|
|
|
const BasicBlock *BB);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// Look through all the blocks in MergePotentials that have hash CurHash
|
|
|
|
/// (guaranteed to match the last element). Build the vector SameTails of
|
|
|
|
/// all those that have the (same) largest number of instructions in common
|
|
|
|
/// of any pair of these blocks. SameTails entries contain an iterator into
|
|
|
|
/// MergePotentials (from which the MachineBasicBlock can be found) and a
|
|
|
|
/// MachineBasicBlock::iterator into that MBB indicating the instruction
|
|
|
|
/// where the matching code sequence begins. Order of elements in SameTails
|
|
|
|
/// is the reverse of the order in which those blocks appear in
|
|
|
|
/// MergePotentials (where they are not necessarily consecutive).
|
2009-11-12 03:49:34 +08:00
|
|
|
unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength,
|
|
|
|
MachineBasicBlock *SuccBB,
|
|
|
|
MachineBasicBlock *PredBB);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// Remove all blocks with hash CurHash from MergePotentials, restoring
|
|
|
|
/// branches at ends of blocks as appropriate.
|
2009-09-04 15:47:40 +08:00
|
|
|
void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
|
|
|
|
MachineBasicBlock* PredBB);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// None of the blocks to be tail-merged consist only of the common tail.
|
|
|
|
/// Create a block that does by splitting one.
|
2010-06-22 09:18:16 +08:00
|
|
|
bool CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
|
2013-06-24 09:55:01 +08:00
|
|
|
MachineBasicBlock *SuccBB,
|
2010-06-22 09:18:16 +08:00
|
|
|
unsigned maxCommonTailLength,
|
|
|
|
unsigned &commonTailIndex);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// Create merged DebugLocs of identical instructions across SameTails and
|
2017-09-07 04:45:24 +08:00
|
|
|
/// assign it to the instruction in common tail; merge MMOs and undef flags.
|
|
|
|
void mergeCommonTails(unsigned commonTailIndex);
|
2009-09-04 15:47:40 +08:00
|
|
|
|
|
|
|
bool OptimizeBranches(MachineFunction &MF);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// Analyze and optimize control flow related to the specified block. This
|
|
|
|
/// is never called on the entry block.
|
2009-09-04 15:47:40 +08:00
|
|
|
bool OptimizeBlock(MachineBasicBlock *MBB);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// Remove the specified dead machine basic block from the function,
|
|
|
|
/// updating the CFG.
|
2009-09-04 15:47:40 +08:00
|
|
|
void RemoveDeadBlock(MachineBasicBlock *MBB);
|
2011-05-12 08:56:58 +08:00
|
|
|
|
2017-03-15 14:29:23 +08:00
|
|
|
/// Hoist common instruction sequences at the start of basic blocks to their
|
|
|
|
/// common predecessor.
|
2011-05-12 08:56:58 +08:00
|
|
|
bool HoistCommonCode(MachineFunction &MF);
|
2017-03-15 14:29:23 +08:00
|
|
|
|
|
|
|
/// If the successors of MBB has common instruction sequence at the start of
|
|
|
|
/// the function, move the instructions before MBB terminator if it's legal.
|
2011-05-12 08:56:58 +08:00
|
|
|
bool HoistCommonCodeInSuccs(MachineBasicBlock *MBB);
|
2009-09-04 15:47:40 +08:00
|
|
|
};
|
|
|
|
|
2017-10-11 06:33:29 +08:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_LIB_CODEGEN_BRANCHFOLDING_H
|