Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1 | //===-- MachineBlockPlacement.cpp - Basic Block Code Layout optimization --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 10 | // This file implements basic block placement transformations using the CFG |
| 11 | // structure and branch probability estimates. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 12 | // |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 13 | // The pass strives to preserve the structure of the CFG (that is, retain |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 14 | // a topological ordering of basic blocks) in the absence of a *strong* signal |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 15 | // to the contrary from probabilities. However, within the CFG structure, it |
| 16 | // attempts to choose an ordering which favors placing more likely sequences of |
| 17 | // blocks adjacent to each other. |
| 18 | // |
| 19 | // The algorithm works from the inner-most loop within a function outward, and |
| 20 | // at each stage walks through the basic blocks, trying to coalesce them into |
| 21 | // sequential chains where allowed by the CFG (or demanded by heavy |
| 22 | // probabilities). Finally, it walks the blocks in topological order, and the |
| 23 | // first time it reaches a chain of basic blocks, it schedules them in the |
| 24 | // function in-order. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/Passes.h" |
| 29 | #include "llvm/ADT/DenseMap.h" |
| 30 | #include "llvm/ADT/SmallPtrSet.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
| 32 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
| 35 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineDominators.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 39 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 40 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Allocator.h" |
Nadav Rotem | c3b0f50 | 2013-04-12 00:48:32 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetLowering.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 47 | #include <algorithm> |
| 48 | using namespace llvm; |
| 49 | |
Chandler Carruth | d0dced5 | 2015-03-05 02:28:25 +0000 | [diff] [blame] | 50 | #define DEBUG_TYPE "block-placement" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 51 | |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 52 | STATISTIC(NumCondBranches, "Number of conditional branches"); |
| 53 | STATISTIC(NumUncondBranches, "Number of uncondittional branches"); |
| 54 | STATISTIC(CondBranchTakenFreq, |
| 55 | "Potential frequency of taking conditional branches"); |
| 56 | STATISTIC(UncondBranchTakenFreq, |
| 57 | "Potential frequency of taking unconditional branches"); |
| 58 | |
Nadav Rotem | c3b0f50 | 2013-04-12 00:48:32 +0000 | [diff] [blame] | 59 | static cl::opt<unsigned> AlignAllBlock("align-all-blocks", |
| 60 | cl::desc("Force the alignment of all " |
| 61 | "blocks in the function."), |
| 62 | cl::init(0), cl::Hidden); |
| 63 | |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 64 | // FIXME: Find a good default for this flag and remove the flag. |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 65 | static cl::opt<unsigned> ExitBlockBias( |
| 66 | "block-placement-exit-block-bias", |
| 67 | cl::desc("Block frequency percentage a loop exit block needs " |
| 68 | "over the original exit to be considered the new exit."), |
| 69 | cl::init(0), cl::Hidden); |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 70 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 71 | static cl::opt<bool> OutlineOptionalBranches( |
| 72 | "outline-optional-branches", |
| 73 | cl::desc("Put completely optional branches, i.e. branches with a common " |
| 74 | "post dominator, out of line."), |
| 75 | cl::init(false), cl::Hidden); |
| 76 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 77 | namespace { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 78 | class BlockChain; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 79 | /// \brief Type for our function-wide basic block -> block chain mapping. |
| 80 | typedef DenseMap<MachineBasicBlock *, BlockChain *> BlockToChainMapType; |
| 81 | } |
| 82 | |
| 83 | namespace { |
| 84 | /// \brief A chain of blocks which will be laid out contiguously. |
| 85 | /// |
| 86 | /// This is the datastructure representing a chain of consecutive blocks that |
| 87 | /// are profitable to layout together in order to maximize fallthrough |
Chandler Carruth | 9139f44 | 2012-06-26 05:16:37 +0000 | [diff] [blame] | 88 | /// probabilities and code locality. We also can use a block chain to represent |
| 89 | /// a sequence of basic blocks which have some external (correctness) |
| 90 | /// requirement for sequential layout. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 91 | /// |
Chandler Carruth | 9139f44 | 2012-06-26 05:16:37 +0000 | [diff] [blame] | 92 | /// Chains can be built around a single basic block and can be merged to grow |
| 93 | /// them. They participate in a block-to-chain mapping, which is updated |
| 94 | /// automatically as chains are merged together. |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 95 | class BlockChain { |
| 96 | /// \brief The sequence of blocks belonging to this chain. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 97 | /// |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 98 | /// This is the sequence of blocks for a particular chain. These will be laid |
| 99 | /// out in-order within the function. |
| 100 | SmallVector<MachineBasicBlock *, 4> Blocks; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 101 | |
| 102 | /// \brief A handle to the function-wide basic block to block chain mapping. |
| 103 | /// |
| 104 | /// This is retained in each block chain to simplify the computation of child |
| 105 | /// block chains for SCC-formation and iteration. We store the edges to child |
| 106 | /// basic blocks, and map them back to their associated chains using this |
| 107 | /// structure. |
| 108 | BlockToChainMapType &BlockToChain; |
| 109 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 110 | public: |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 111 | /// \brief Construct a new BlockChain. |
| 112 | /// |
| 113 | /// This builds a new block chain representing a single basic block in the |
| 114 | /// function. It also registers itself as the chain that block participates |
| 115 | /// in with the BlockToChain mapping. |
| 116 | BlockChain(BlockToChainMapType &BlockToChain, MachineBasicBlock *BB) |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 117 | : Blocks(1, BB), BlockToChain(BlockToChain), LoopPredecessors(0) { |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 118 | assert(BB && "Cannot create a chain with a null basic block"); |
| 119 | BlockToChain[BB] = this; |
| 120 | } |
| 121 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 122 | /// \brief Iterator over blocks within the chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 123 | typedef SmallVectorImpl<MachineBasicBlock *>::iterator iterator; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 124 | |
| 125 | /// \brief Beginning of blocks within the chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 126 | iterator begin() { return Blocks.begin(); } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 127 | |
| 128 | /// \brief End of blocks within the chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 129 | iterator end() { return Blocks.end(); } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 130 | |
| 131 | /// \brief Merge a block chain into this one. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 132 | /// |
| 133 | /// This routine merges a block chain into this one. It takes care of forming |
| 134 | /// a contiguous sequence of basic blocks, updating the edge list, and |
| 135 | /// updating the block -> chain mapping. It does not free or tear down the |
| 136 | /// old chain, but the old chain's block list is no longer valid. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 137 | void merge(MachineBasicBlock *BB, BlockChain *Chain) { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 138 | assert(BB); |
| 139 | assert(!Blocks.empty()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 140 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 141 | // Fast path in case we don't have a chain already. |
| 142 | if (!Chain) { |
| 143 | assert(!BlockToChain[BB]); |
| 144 | Blocks.push_back(BB); |
| 145 | BlockToChain[BB] = this; |
| 146 | return; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 149 | assert(BB == *Chain->begin()); |
| 150 | assert(Chain->begin() != Chain->end()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 151 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 152 | // Update the incoming blocks to point to this chain, and add them to the |
| 153 | // chain structure. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 154 | for (MachineBasicBlock *ChainBB : *Chain) { |
| 155 | Blocks.push_back(ChainBB); |
| 156 | assert(BlockToChain[ChainBB] == Chain && "Incoming blocks not in chain"); |
| 157 | BlockToChain[ChainBB] = this; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 158 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 159 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 160 | |
Chandler Carruth | 4915890 | 2012-04-08 14:37:01 +0000 | [diff] [blame] | 161 | #ifndef NDEBUG |
| 162 | /// \brief Dump the blocks in this chain. |
Nico Weber | 7408c70 | 2014-01-03 22:53:37 +0000 | [diff] [blame] | 163 | LLVM_DUMP_METHOD void dump() { |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 164 | for (MachineBasicBlock *MBB : *this) |
| 165 | MBB->dump(); |
Chandler Carruth | 4915890 | 2012-04-08 14:37:01 +0000 | [diff] [blame] | 166 | } |
| 167 | #endif // NDEBUG |
| 168 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 169 | /// \brief Count of predecessors within the loop currently being processed. |
| 170 | /// |
| 171 | /// This count is updated at each loop we process to represent the number of |
| 172 | /// in-loop predecessors of this chain. |
| 173 | unsigned LoopPredecessors; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 174 | }; |
| 175 | } |
| 176 | |
| 177 | namespace { |
| 178 | class MachineBlockPlacement : public MachineFunctionPass { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 179 | /// \brief A typedef for a block filter set. |
| 180 | typedef SmallPtrSet<MachineBasicBlock *, 16> BlockFilterSet; |
| 181 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 182 | /// \brief A handle to the branch probability pass. |
| 183 | const MachineBranchProbabilityInfo *MBPI; |
| 184 | |
| 185 | /// \brief A handle to the function-wide block frequency pass. |
| 186 | const MachineBlockFrequencyInfo *MBFI; |
| 187 | |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 188 | /// \brief A handle to the loop info. |
| 189 | const MachineLoopInfo *MLI; |
| 190 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 191 | /// \brief A handle to the target's instruction info. |
| 192 | const TargetInstrInfo *TII; |
| 193 | |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 194 | /// \brief A handle to the target's lowering info. |
Benjamin Kramer | 56b31bd | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 195 | const TargetLoweringBase *TLI; |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 196 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 197 | /// \brief A handle to the post dominator tree. |
| 198 | MachineDominatorTree *MDT; |
| 199 | |
| 200 | /// \brief A set of blocks that are unavoidably execute, i.e. they dominate |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 201 | /// all terminators of the MachineFunction. |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 202 | SmallPtrSet<MachineBasicBlock *, 4> UnavoidableBlocks; |
| 203 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 204 | /// \brief Allocator and owner of BlockChain structures. |
| 205 | /// |
Chandler Carruth | 9139f44 | 2012-06-26 05:16:37 +0000 | [diff] [blame] | 206 | /// We build BlockChains lazily while processing the loop structure of |
| 207 | /// a function. To reduce malloc traffic, we allocate them using this |
| 208 | /// slab-like allocator, and destroy them after the pass completes. An |
| 209 | /// important guarantee is that this allocator produces stable pointers to |
| 210 | /// the chains. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 211 | SpecificBumpPtrAllocator<BlockChain> ChainAllocator; |
| 212 | |
| 213 | /// \brief Function wide BasicBlock to BlockChain mapping. |
| 214 | /// |
| 215 | /// This mapping allows efficiently moving from any given basic block to the |
| 216 | /// BlockChain it participates in, if any. We use it to, among other things, |
| 217 | /// allow implicitly defining edges between chains as the existing edges |
| 218 | /// between basic blocks. |
| 219 | DenseMap<MachineBasicBlock *, BlockChain *> BlockToChain; |
| 220 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 221 | void markChainSuccessors(BlockChain &Chain, MachineBasicBlock *LoopHeaderBB, |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 222 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 223 | const BlockFilterSet *BlockFilter = nullptr); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 224 | MachineBasicBlock *selectBestSuccessor(MachineBasicBlock *BB, |
| 225 | BlockChain &Chain, |
| 226 | const BlockFilterSet *BlockFilter); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 227 | MachineBasicBlock * |
| 228 | selectBestCandidateBlock(BlockChain &Chain, |
| 229 | SmallVectorImpl<MachineBasicBlock *> &WorkList, |
| 230 | const BlockFilterSet *BlockFilter); |
| 231 | MachineBasicBlock * |
| 232 | getFirstUnplacedBlock(MachineFunction &F, const BlockChain &PlacedChain, |
| 233 | MachineFunction::iterator &PrevUnplacedBlockIt, |
| 234 | const BlockFilterSet *BlockFilter); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 235 | void buildChain(MachineBasicBlock *BB, BlockChain &Chain, |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 236 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 237 | const BlockFilterSet *BlockFilter = nullptr); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 238 | MachineBasicBlock *findBestLoopTop(MachineLoop &L, |
| 239 | const BlockFilterSet &LoopBlockSet); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 240 | MachineBasicBlock *findBestLoopExit(MachineFunction &F, MachineLoop &L, |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 241 | const BlockFilterSet &LoopBlockSet); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 242 | void buildLoopChains(MachineFunction &F, MachineLoop &L); |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 243 | void rotateLoop(BlockChain &LoopChain, MachineBasicBlock *ExitingBB, |
| 244 | const BlockFilterSet &LoopBlockSet); |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 245 | void buildCFGChains(MachineFunction &F); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 246 | |
| 247 | public: |
| 248 | static char ID; // Pass identification, replacement for typeid |
| 249 | MachineBlockPlacement() : MachineFunctionPass(ID) { |
| 250 | initializeMachineBlockPlacementPass(*PassRegistry::getPassRegistry()); |
| 251 | } |
| 252 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 253 | bool runOnMachineFunction(MachineFunction &F) override; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 254 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 255 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 256 | AU.addRequired<MachineBranchProbabilityInfo>(); |
| 257 | AU.addRequired<MachineBlockFrequencyInfo>(); |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 258 | AU.addRequired<MachineDominatorTree>(); |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 259 | AU.addRequired<MachineLoopInfo>(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 260 | MachineFunctionPass::getAnalysisUsage(AU); |
| 261 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 262 | }; |
| 263 | } |
| 264 | |
| 265 | char MachineBlockPlacement::ID = 0; |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 266 | char &llvm::MachineBlockPlacementID = MachineBlockPlacement::ID; |
Chandler Carruth | d0dced5 | 2015-03-05 02:28:25 +0000 | [diff] [blame] | 267 | INITIALIZE_PASS_BEGIN(MachineBlockPlacement, "block-placement", |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 268 | "Branch Probability Basic Block Placement", false, false) |
| 269 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
| 270 | INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 271 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 272 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
Chandler Carruth | d0dced5 | 2015-03-05 02:28:25 +0000 | [diff] [blame] | 273 | INITIALIZE_PASS_END(MachineBlockPlacement, "block-placement", |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 274 | "Branch Probability Basic Block Placement", false, false) |
| 275 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 276 | #ifndef NDEBUG |
| 277 | /// \brief Helper to print the name of a MBB. |
| 278 | /// |
| 279 | /// Only used by debug logging. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 280 | static std::string getBlockName(MachineBasicBlock *BB) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 281 | std::string Result; |
| 282 | raw_string_ostream OS(Result); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 283 | OS << "BB#" << BB->getNumber(); |
| 284 | OS << " (derived from LLVM BB '" << BB->getName() << "')"; |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 285 | OS.flush(); |
| 286 | return Result; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 289 | /// \brief Helper to print the number of a MBB. |
| 290 | /// |
| 291 | /// Only used by debug logging. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 292 | static std::string getBlockNum(MachineBasicBlock *BB) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 293 | std::string Result; |
| 294 | raw_string_ostream OS(Result); |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 295 | OS << "BB#" << BB->getNumber(); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 296 | OS.flush(); |
| 297 | return Result; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 298 | } |
| 299 | #endif |
| 300 | |
Chandler Carruth | eb4ec3a | 2011-11-13 11:34:55 +0000 | [diff] [blame] | 301 | /// \brief Mark a chain's successors as having one fewer preds. |
| 302 | /// |
| 303 | /// When a chain is being merged into the "placed" chain, this routine will |
| 304 | /// quickly walk the successors of each block in the chain and mark them as |
| 305 | /// having one fewer active predecessor. It also adds any successors of this |
| 306 | /// chain which reach the zero-predecessor state to the worklist passed in. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 307 | void MachineBlockPlacement::markChainSuccessors( |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 308 | BlockChain &Chain, MachineBasicBlock *LoopHeaderBB, |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 309 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 310 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 311 | // Walk all the blocks in this chain, marking their successors as having |
| 312 | // a predecessor placed. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 313 | for (MachineBasicBlock *MBB : Chain) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 314 | // Add any successors for which this is the only un-placed in-loop |
| 315 | // predecessor to the worklist as a viable candidate for CFG-neutral |
| 316 | // placement. No subsequent placement of this block will violate the CFG |
| 317 | // shape, so we get to use heuristics to choose a favorable placement. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 318 | for (MachineBasicBlock *Succ : MBB->successors()) { |
| 319 | if (BlockFilter && !BlockFilter->count(Succ)) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 320 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 321 | BlockChain &SuccChain = *BlockToChain[Succ]; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 322 | // Disregard edges within a fixed chain, or edges to the loop header. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 323 | if (&Chain == &SuccChain || Succ == LoopHeaderBB) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 324 | continue; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 325 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 326 | // This is a cross-chain edge that is within the loop, so decrement the |
| 327 | // loop predecessor count of the destination chain. |
| 328 | if (SuccChain.LoopPredecessors > 0 && --SuccChain.LoopPredecessors == 0) |
Chandler Carruth | d394baf | 2011-11-24 08:46:04 +0000 | [diff] [blame] | 329 | BlockWorkList.push_back(*SuccChain.begin()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 332 | } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 333 | |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 334 | /// \brief Select the best successor for a block. |
| 335 | /// |
| 336 | /// This looks across all successors of a particular block and attempts to |
| 337 | /// select the "best" one to be the layout successor. It only considers direct |
| 338 | /// successors which also pass the block filter. It will attempt to avoid |
| 339 | /// breaking CFG structure, but cave and break such structures in the case of |
| 340 | /// very hot successor edges. |
| 341 | /// |
| 342 | /// \returns The best successor block found, or null if none are viable. |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 343 | MachineBasicBlock * |
| 344 | MachineBlockPlacement::selectBestSuccessor(MachineBasicBlock *BB, |
| 345 | BlockChain &Chain, |
| 346 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 347 | const BranchProbability HotProb(4, 5); // 80% |
| 348 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 349 | MachineBasicBlock *BestSucc = nullptr; |
Chandler Carruth | 84cd44c | 2011-11-14 09:12:57 +0000 | [diff] [blame] | 350 | // FIXME: Due to the performance of the probability and weight routines in |
| 351 | // the MBPI analysis, we manually compute probabilities using the edge |
| 352 | // weights. This is suboptimal as it means that the somewhat subtle |
| 353 | // definition of edge weight semantics is encoded here as well. We should |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 354 | // improve the MBPI interface to efficiently support query patterns such as |
Chandler Carruth | 84cd44c | 2011-11-14 09:12:57 +0000 | [diff] [blame] | 355 | // this. |
| 356 | uint32_t BestWeight = 0; |
| 357 | uint32_t WeightScale = 0; |
| 358 | uint32_t SumWeight = MBPI->getSumForBlock(BB, WeightScale); |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 359 | DEBUG(dbgs() << "Attempting merge from: " << getBlockName(BB) << "\n"); |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 360 | for (MachineBasicBlock *Succ : BB->successors()) { |
| 361 | if (BlockFilter && !BlockFilter->count(Succ)) |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 362 | continue; |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 363 | BlockChain &SuccChain = *BlockToChain[Succ]; |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 364 | if (&SuccChain == &Chain) { |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 365 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> Already merged!\n"); |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 366 | continue; |
| 367 | } |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 368 | if (Succ != *SuccChain.begin()) { |
| 369 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> Mid chain!\n"); |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 370 | continue; |
| 371 | } |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 372 | |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 373 | uint32_t SuccWeight = MBPI->getEdgeWeight(BB, Succ); |
Chandler Carruth | 84cd44c | 2011-11-14 09:12:57 +0000 | [diff] [blame] | 374 | BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight); |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 375 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 376 | // If we outline optional branches, look whether Succ is unavoidable, i.e. |
| 377 | // dominates all terminators of the MachineFunction. If it does, other |
| 378 | // successors must be optional. Don't do this for cold branches. |
| 379 | if (OutlineOptionalBranches && SuccProb > HotProb.getCompl() && |
| 380 | UnavoidableBlocks.count(Succ) > 0) |
| 381 | return Succ; |
| 382 | |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 383 | // Only consider successors which are either "hot", or wouldn't violate |
| 384 | // any CFG constraints. |
Chandler Carruth | 18dfac3 | 2011-11-20 11:22:06 +0000 | [diff] [blame] | 385 | if (SuccChain.LoopPredecessors != 0) { |
| 386 | if (SuccProb < HotProb) { |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 387 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> " << SuccProb |
Chandler Carruth | 260258b | 2013-11-25 00:43:41 +0000 | [diff] [blame] | 388 | << " (prob) (CFG conflict)\n"); |
Chandler Carruth | 18dfac3 | 2011-11-20 11:22:06 +0000 | [diff] [blame] | 389 | continue; |
| 390 | } |
| 391 | |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 392 | // Make sure that a hot successor doesn't have a globally more |
| 393 | // important predecessor. |
| 394 | BlockFrequency CandidateEdgeFreq = |
| 395 | MBFI->getBlockFreq(BB) * SuccProb * HotProb.getCompl(); |
| 396 | bool BadCFGConflict = false; |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 397 | for (MachineBasicBlock *Pred : Succ->predecessors()) { |
| 398 | if (Pred == Succ || (BlockFilter && !BlockFilter->count(Pred)) || |
| 399 | BlockToChain[Pred] == &Chain) |
Chandler Carruth | e328814 | 2015-01-14 20:19:29 +0000 | [diff] [blame] | 400 | continue; |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 401 | BlockFrequency PredEdgeFreq = |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 402 | MBFI->getBlockFreq(Pred) * MBPI->getEdgeProbability(Pred, Succ); |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 403 | if (PredEdgeFreq >= CandidateEdgeFreq) { |
| 404 | BadCFGConflict = true; |
| 405 | break; |
Chandler Carruth | e328814 | 2015-01-14 20:19:29 +0000 | [diff] [blame] | 406 | } |
Chandler Carruth | 18dfac3 | 2011-11-20 11:22:06 +0000 | [diff] [blame] | 407 | } |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 408 | if (BadCFGConflict) { |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 409 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> " << SuccProb |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 410 | << " (prob) (non-cold CFG conflict)\n"); |
| 411 | continue; |
| 412 | } |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 415 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> " << SuccProb |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 416 | << " (prob)" |
| 417 | << (SuccChain.LoopPredecessors != 0 ? " (CFG break)" : "") |
| 418 | << "\n"); |
Chandler Carruth | 84cd44c | 2011-11-14 09:12:57 +0000 | [diff] [blame] | 419 | if (BestSucc && BestWeight >= SuccWeight) |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 420 | continue; |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 421 | BestSucc = Succ; |
Chandler Carruth | 84cd44c | 2011-11-14 09:12:57 +0000 | [diff] [blame] | 422 | BestWeight = SuccWeight; |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 423 | } |
| 424 | return BestSucc; |
| 425 | } |
| 426 | |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 427 | /// \brief Select the best block from a worklist. |
| 428 | /// |
| 429 | /// This looks through the provided worklist as a list of candidate basic |
| 430 | /// blocks and select the most profitable one to place. The definition of |
| 431 | /// profitable only really makes sense in the context of a loop. This returns |
| 432 | /// the most frequently visited block in the worklist, which in the case of |
| 433 | /// a loop, is the one most desirable to be physically close to the rest of the |
| 434 | /// loop body in order to improve icache behavior. |
| 435 | /// |
| 436 | /// \returns The best block found, or null if none are viable. |
| 437 | MachineBasicBlock *MachineBlockPlacement::selectBestCandidateBlock( |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 438 | BlockChain &Chain, SmallVectorImpl<MachineBasicBlock *> &WorkList, |
| 439 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | 0af6a0b | 2011-11-14 09:46:33 +0000 | [diff] [blame] | 440 | // Once we need to walk the worklist looking for a candidate, cleanup the |
| 441 | // worklist of already placed entries. |
| 442 | // FIXME: If this shows up on profiles, it could be folded (at the cost of |
| 443 | // some code complexity) into the loop below. |
| 444 | WorkList.erase(std::remove_if(WorkList.begin(), WorkList.end(), |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 445 | [&](MachineBasicBlock *BB) { |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 446 | return BlockToChain.lookup(BB) == &Chain; |
| 447 | }), |
Chandler Carruth | 0af6a0b | 2011-11-14 09:46:33 +0000 | [diff] [blame] | 448 | WorkList.end()); |
| 449 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 450 | MachineBasicBlock *BestBlock = nullptr; |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 451 | BlockFrequency BestFreq; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 452 | for (MachineBasicBlock *MBB : WorkList) { |
| 453 | BlockChain &SuccChain = *BlockToChain[MBB]; |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 454 | if (&SuccChain == &Chain) { |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 455 | DEBUG(dbgs() << " " << getBlockName(MBB) << " -> Already merged!\n"); |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 456 | continue; |
| 457 | } |
| 458 | assert(SuccChain.LoopPredecessors == 0 && "Found CFG-violating block"); |
| 459 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 460 | BlockFrequency CandidateFreq = MBFI->getBlockFreq(MBB); |
| 461 | DEBUG(dbgs() << " " << getBlockName(MBB) << " -> "; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 462 | MBFI->printBlockFreq(dbgs(), CandidateFreq) << " (freq)\n"); |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 463 | if (BestBlock && BestFreq >= CandidateFreq) |
| 464 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 465 | BestBlock = MBB; |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 466 | BestFreq = CandidateFreq; |
| 467 | } |
| 468 | return BestBlock; |
| 469 | } |
| 470 | |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 471 | /// \brief Retrieve the first unplaced basic block. |
| 472 | /// |
| 473 | /// This routine is called when we are unable to use the CFG to walk through |
| 474 | /// all of the basic blocks and form a chain due to unnatural loops in the CFG. |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 475 | /// We walk through the function's blocks in order, starting from the |
| 476 | /// LastUnplacedBlockIt. We update this iterator on each call to avoid |
| 477 | /// re-scanning the entire sequence on repeated calls to this routine. |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 478 | MachineBasicBlock *MachineBlockPlacement::getFirstUnplacedBlock( |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 479 | MachineFunction &F, const BlockChain &PlacedChain, |
| 480 | MachineFunction::iterator &PrevUnplacedBlockIt, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 481 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 482 | for (MachineFunction::iterator I = PrevUnplacedBlockIt, E = F.end(); I != E; |
| 483 | ++I) { |
| 484 | if (BlockFilter && !BlockFilter->count(I)) |
| 485 | continue; |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 486 | if (BlockToChain[I] != &PlacedChain) { |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 487 | PrevUnplacedBlockIt = I; |
Chandler Carruth | 4a87aa0 | 2011-11-23 03:03:21 +0000 | [diff] [blame] | 488 | // Now select the head of the chain to which the unplaced block belongs |
| 489 | // as the block to place. This will force the entire chain to be placed, |
| 490 | // and satisfies the requirements of merging chains. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 491 | return *BlockToChain[I]->begin(); |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 494 | return nullptr; |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 497 | void MachineBlockPlacement::buildChain( |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 498 | MachineBasicBlock *BB, BlockChain &Chain, |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 499 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 500 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 501 | assert(BB); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 502 | assert(BlockToChain[BB] == &Chain); |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 503 | MachineFunction &F = *BB->getParent(); |
| 504 | MachineFunction::iterator PrevUnplacedBlockIt = F.begin(); |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 505 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 506 | MachineBasicBlock *LoopHeaderBB = BB; |
| 507 | markChainSuccessors(Chain, LoopHeaderBB, BlockWorkList, BlockFilter); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 508 | BB = *std::prev(Chain.end()); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 509 | for (;;) { |
| 510 | assert(BB); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 511 | assert(BlockToChain[BB] == &Chain); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 512 | assert(*std::prev(Chain.end()) == BB); |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 513 | |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 514 | // Look for the best viable successor if there is one to place immediately |
| 515 | // after this block. |
Duncan Sands | 291d47e | 2012-09-14 09:00:11 +0000 | [diff] [blame] | 516 | MachineBasicBlock *BestSucc = selectBestSuccessor(BB, Chain, BlockFilter); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 517 | |
| 518 | // If an immediate successor isn't available, look for the best viable |
| 519 | // block among those we've identified as not violating the loop's CFG at |
| 520 | // this point. This won't be a fallthrough, but it will increase locality. |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 521 | if (!BestSucc) |
| 522 | BestSucc = selectBestCandidateBlock(Chain, BlockWorkList, BlockFilter); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 523 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 524 | if (!BestSucc) { |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 525 | BestSucc = |
| 526 | getFirstUnplacedBlock(F, Chain, PrevUnplacedBlockIt, BlockFilter); |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 527 | if (!BestSucc) |
| 528 | break; |
| 529 | |
| 530 | DEBUG(dbgs() << "Unnatural loop CFG detected, forcibly merging the " |
| 531 | "layout successor until the CFG reduces\n"); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 532 | } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 533 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 534 | // Place this block, updating the datastructures to reflect its placement. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 535 | BlockChain &SuccChain = *BlockToChain[BestSucc]; |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 536 | // Zero out LoopPredecessors for the successor we're about to merge in case |
| 537 | // we selected a successor that didn't fit naturally into the CFG. |
| 538 | SuccChain.LoopPredecessors = 0; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 539 | DEBUG(dbgs() << "Merging from " << getBlockNum(BB) << " to " |
| 540 | << getBlockNum(BestSucc) << "\n"); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 541 | markChainSuccessors(SuccChain, LoopHeaderBB, BlockWorkList, BlockFilter); |
| 542 | Chain.merge(BestSucc, &SuccChain); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 543 | BB = *std::prev(Chain.end()); |
Jakub Staszak | 190c712 | 2011-12-07 19:46:10 +0000 | [diff] [blame] | 544 | } |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 545 | |
| 546 | DEBUG(dbgs() << "Finished forming chain for header block " |
| 547 | << getBlockNum(*Chain.begin()) << "\n"); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 550 | /// \brief Find the best loop top block for layout. |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 551 | /// |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 552 | /// Look for a block which is strictly better than the loop header for laying |
| 553 | /// out at the top of the loop. This looks for one and only one pattern: |
| 554 | /// a latch block with no conditional exit. This block will cause a conditional |
| 555 | /// jump around it or will be the bottom of the loop if we lay it out in place, |
| 556 | /// but if it it doesn't end up at the bottom of the loop for any reason, |
| 557 | /// rotation alone won't fix it. Because such a block will always result in an |
| 558 | /// unconditional jump (for the backedge) rotating it in front of the loop |
| 559 | /// header is always profitable. |
| 560 | MachineBasicBlock * |
| 561 | MachineBlockPlacement::findBestLoopTop(MachineLoop &L, |
| 562 | const BlockFilterSet &LoopBlockSet) { |
| 563 | // Check that the header hasn't been fused with a preheader block due to |
| 564 | // crazy branches. If it has, we need to start with the header at the top to |
| 565 | // prevent pulling the preheader into the loop body. |
| 566 | BlockChain &HeaderChain = *BlockToChain[L.getHeader()]; |
| 567 | if (!LoopBlockSet.count(*HeaderChain.begin())) |
| 568 | return L.getHeader(); |
| 569 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 570 | DEBUG(dbgs() << "Finding best loop top for: " << getBlockName(L.getHeader()) |
| 571 | << "\n"); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 572 | |
| 573 | BlockFrequency BestPredFreq; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 574 | MachineBasicBlock *BestPred = nullptr; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 575 | for (MachineBasicBlock *Pred : L.getHeader()->predecessors()) { |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 576 | if (!LoopBlockSet.count(Pred)) |
| 577 | continue; |
| 578 | DEBUG(dbgs() << " header pred: " << getBlockName(Pred) << ", " |
Michael Gottesman | b78dec8 | 2013-12-14 00:25:45 +0000 | [diff] [blame] | 579 | << Pred->succ_size() << " successors, "; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 580 | MBFI->printBlockFreq(dbgs(), Pred) << " freq\n"); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 581 | if (Pred->succ_size() > 1) |
| 582 | continue; |
| 583 | |
| 584 | BlockFrequency PredFreq = MBFI->getBlockFreq(Pred); |
| 585 | if (!BestPred || PredFreq > BestPredFreq || |
| 586 | (!(PredFreq < BestPredFreq) && |
| 587 | Pred->isLayoutSuccessor(L.getHeader()))) { |
| 588 | BestPred = Pred; |
| 589 | BestPredFreq = PredFreq; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // If no direct predecessor is fine, just use the loop header. |
| 594 | if (!BestPred) |
| 595 | return L.getHeader(); |
| 596 | |
| 597 | // Walk backwards through any straight line of predecessors. |
| 598 | while (BestPred->pred_size() == 1 && |
| 599 | (*BestPred->pred_begin())->succ_size() == 1 && |
| 600 | *BestPred->pred_begin() != L.getHeader()) |
| 601 | BestPred = *BestPred->pred_begin(); |
| 602 | |
| 603 | DEBUG(dbgs() << " final top: " << getBlockName(BestPred) << "\n"); |
| 604 | return BestPred; |
| 605 | } |
| 606 | |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 607 | /// \brief Find the best loop exiting block for layout. |
| 608 | /// |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 609 | /// This routine implements the logic to analyze the loop looking for the best |
| 610 | /// block to layout at the top of the loop. Typically this is done to maximize |
| 611 | /// fallthrough opportunities. |
| 612 | MachineBasicBlock * |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 613 | MachineBlockPlacement::findBestLoopExit(MachineFunction &F, MachineLoop &L, |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 614 | const BlockFilterSet &LoopBlockSet) { |
Chandler Carruth | 6806261 | 2012-04-10 13:35:57 +0000 | [diff] [blame] | 615 | // We don't want to layout the loop linearly in all cases. If the loop header |
| 616 | // is just a normal basic block in the loop, we want to look for what block |
| 617 | // within the loop is the best one to layout at the top. However, if the loop |
| 618 | // header has be pre-merged into a chain due to predecessors not having |
| 619 | // analyzable branches, *and* the predecessor it is merged with is *not* part |
| 620 | // of the loop, rotating the header into the middle of the loop will create |
| 621 | // a non-contiguous range of blocks which is Very Bad. So start with the |
| 622 | // header and only rotate if safe. |
| 623 | BlockChain &HeaderChain = *BlockToChain[L.getHeader()]; |
| 624 | if (!LoopBlockSet.count(*HeaderChain.begin())) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 625 | return nullptr; |
Chandler Carruth | 6806261 | 2012-04-10 13:35:57 +0000 | [diff] [blame] | 626 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 627 | BlockFrequency BestExitEdgeFreq; |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 628 | unsigned BestExitLoopDepth = 0; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 629 | MachineBasicBlock *ExitingBB = nullptr; |
Chandler Carruth | 4f56720 | 2011-11-27 20:18:00 +0000 | [diff] [blame] | 630 | // If there are exits to outer loops, loop rotation can severely limit |
| 631 | // fallthrough opportunites unless it selects such an exit. Keep a set of |
| 632 | // blocks where rotating to exit with that block will reach an outer loop. |
| 633 | SmallPtrSet<MachineBasicBlock *, 4> BlocksExitingToOuterLoop; |
| 634 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 635 | DEBUG(dbgs() << "Finding best loop exit for: " << getBlockName(L.getHeader()) |
| 636 | << "\n"); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 637 | for (MachineBasicBlock *MBB : L.getBlocks()) { |
| 638 | BlockChain &Chain = *BlockToChain[MBB]; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 639 | // Ensure that this block is at the end of a chain; otherwise it could be |
| 640 | // mid-way through an inner loop or a successor of an analyzable branch. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 641 | if (MBB != *std::prev(Chain.end())) |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 642 | continue; |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 643 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 644 | // Now walk the successors. We need to establish whether this has a viable |
| 645 | // exiting successor and whether it has a viable non-exiting successor. |
| 646 | // We store the old exiting state and restore it if a viable looping |
| 647 | // successor isn't found. |
| 648 | MachineBasicBlock *OldExitingBB = ExitingBB; |
| 649 | BlockFrequency OldBestExitEdgeFreq = BestExitEdgeFreq; |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 650 | bool HasLoopingSucc = false; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 651 | // FIXME: Due to the performance of the probability and weight routines in |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 652 | // the MBPI analysis, we use the internal weights and manually compute the |
| 653 | // probabilities to avoid quadratic behavior. |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 654 | uint32_t WeightScale = 0; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 655 | uint32_t SumWeight = MBPI->getSumForBlock(MBB, WeightScale); |
| 656 | for (MachineBasicBlock *Succ : MBB->successors()) { |
| 657 | if (Succ->isLandingPad()) |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 658 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 659 | if (Succ == MBB) |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 660 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 661 | BlockChain &SuccChain = *BlockToChain[Succ]; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 662 | // Don't split chains, either this chain or the successor's chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 663 | if (&Chain == &SuccChain) { |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 664 | DEBUG(dbgs() << " exiting: " << getBlockName(MBB) << " -> " |
| 665 | << getBlockName(Succ) << " (chain conflict)\n"); |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 666 | continue; |
| 667 | } |
| 668 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 669 | uint32_t SuccWeight = MBPI->getEdgeWeight(MBB, Succ); |
| 670 | if (LoopBlockSet.count(Succ)) { |
| 671 | DEBUG(dbgs() << " looping: " << getBlockName(MBB) << " -> " |
| 672 | << getBlockName(Succ) << " (" << SuccWeight << ")\n"); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 673 | HasLoopingSucc = true; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 674 | continue; |
| 675 | } |
| 676 | |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 677 | unsigned SuccLoopDepth = 0; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 678 | if (MachineLoop *ExitLoop = MLI->getLoopFor(Succ)) { |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 679 | SuccLoopDepth = ExitLoop->getLoopDepth(); |
| 680 | if (ExitLoop->contains(&L)) |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 681 | BlocksExitingToOuterLoop.insert(MBB); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 684 | BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 685 | BlockFrequency ExitEdgeFreq = MBFI->getBlockFreq(MBB) * SuccProb; |
| 686 | DEBUG(dbgs() << " exiting: " << getBlockName(MBB) << " -> " |
| 687 | << getBlockName(Succ) << " [L:" << SuccLoopDepth << "] ("; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 688 | MBFI->printBlockFreq(dbgs(), ExitEdgeFreq) << ")\n"); |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 689 | // Note that we bias this toward an existing layout successor to retain |
| 690 | // incoming order in the absence of better information. The exit must have |
| 691 | // a frequency higher than the current exit before we consider breaking |
| 692 | // the layout. |
| 693 | BranchProbability Bias(100 - ExitBlockBias, 100); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 694 | if (!ExitingBB || BestExitLoopDepth < SuccLoopDepth || |
| 695 | ExitEdgeFreq > BestExitEdgeFreq || |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 696 | (MBB->isLayoutSuccessor(Succ) && |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 697 | !(ExitEdgeFreq < BestExitEdgeFreq * Bias))) { |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 698 | BestExitEdgeFreq = ExitEdgeFreq; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 699 | ExitingBB = MBB; |
Chandler Carruth | a054580 | 2011-11-27 09:22:53 +0000 | [diff] [blame] | 700 | } |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 701 | } |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 702 | |
| 703 | // Restore the old exiting state, no viable looping successor was found. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 704 | if (!HasLoopingSucc) { |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 705 | ExitingBB = OldExitingBB; |
| 706 | BestExitEdgeFreq = OldBestExitEdgeFreq; |
Chandler Carruth | a054580 | 2011-11-27 09:22:53 +0000 | [diff] [blame] | 707 | continue; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 708 | } |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 709 | } |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 710 | // Without a candidate exiting block or with only a single block in the |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 711 | // loop, just use the loop header to layout the loop. |
| 712 | if (!ExitingBB || L.getNumBlocks() == 1) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 713 | return nullptr; |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 714 | |
Chandler Carruth | 4f56720 | 2011-11-27 20:18:00 +0000 | [diff] [blame] | 715 | // Also, if we have exit blocks which lead to outer loops but didn't select |
| 716 | // one of them as the exiting block we are rotating toward, disable loop |
| 717 | // rotation altogether. |
| 718 | if (!BlocksExitingToOuterLoop.empty() && |
| 719 | !BlocksExitingToOuterLoop.count(ExitingBB)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 720 | return nullptr; |
Chandler Carruth | 4f56720 | 2011-11-27 20:18:00 +0000 | [diff] [blame] | 721 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 722 | DEBUG(dbgs() << " Best exiting block: " << getBlockName(ExitingBB) << "\n"); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 723 | return ExitingBB; |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 726 | /// \brief Attempt to rotate an exiting block to the bottom of the loop. |
| 727 | /// |
| 728 | /// Once we have built a chain, try to rotate it to line up the hot exit block |
| 729 | /// with fallthrough out of the loop if doing so doesn't introduce unnecessary |
| 730 | /// branches. For example, if the loop has fallthrough into its header and out |
| 731 | /// of its bottom already, don't rotate it. |
| 732 | void MachineBlockPlacement::rotateLoop(BlockChain &LoopChain, |
| 733 | MachineBasicBlock *ExitingBB, |
| 734 | const BlockFilterSet &LoopBlockSet) { |
| 735 | if (!ExitingBB) |
| 736 | return; |
| 737 | |
| 738 | MachineBasicBlock *Top = *LoopChain.begin(); |
| 739 | bool ViableTopFallthrough = false; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 740 | for (MachineBasicBlock *Pred : Top->predecessors()) { |
| 741 | BlockChain *PredChain = BlockToChain[Pred]; |
| 742 | if (!LoopBlockSet.count(Pred) && |
| 743 | (!PredChain || Pred == *std::prev(PredChain->end()))) { |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 744 | ViableTopFallthrough = true; |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | // If the header has viable fallthrough, check whether the current loop |
| 750 | // bottom is a viable exiting block. If so, bail out as rotating will |
| 751 | // introduce an unnecessary branch. |
| 752 | if (ViableTopFallthrough) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 753 | MachineBasicBlock *Bottom = *std::prev(LoopChain.end()); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 754 | for (MachineBasicBlock *Succ : Bottom->successors()) { |
| 755 | BlockChain *SuccChain = BlockToChain[Succ]; |
| 756 | if (!LoopBlockSet.count(Succ) && |
| 757 | (!SuccChain || Succ == *SuccChain->begin())) |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 758 | return; |
| 759 | } |
| 760 | } |
| 761 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 762 | BlockChain::iterator ExitIt = |
| 763 | std::find(LoopChain.begin(), LoopChain.end(), ExitingBB); |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 764 | if (ExitIt == LoopChain.end()) |
| 765 | return; |
| 766 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 767 | std::rotate(LoopChain.begin(), std::next(ExitIt), LoopChain.end()); |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 770 | /// \brief Forms basic block chains from the natural loop structures. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 771 | /// |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 772 | /// These chains are designed to preserve the existing *structure* of the code |
| 773 | /// as much as possible. We can then stitch the chains together in a way which |
| 774 | /// both preserves the topological structure and minimizes taken conditional |
| 775 | /// branches. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 776 | void MachineBlockPlacement::buildLoopChains(MachineFunction &F, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 777 | MachineLoop &L) { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 778 | // First recurse through any nested loops, building chains for those inner |
| 779 | // loops. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 780 | for (MachineLoop *InnerLoop : L) |
| 781 | buildLoopChains(F, *InnerLoop); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 782 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 783 | SmallVector<MachineBasicBlock *, 16> BlockWorkList; |
| 784 | BlockFilterSet LoopBlockSet(L.block_begin(), L.block_end()); |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 785 | |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 786 | // First check to see if there is an obviously preferable top block for the |
| 787 | // loop. This will default to the header, but may end up as one of the |
| 788 | // predecessors to the header if there is one which will result in strictly |
| 789 | // fewer branches in the loop body. |
| 790 | MachineBasicBlock *LoopTop = findBestLoopTop(L, LoopBlockSet); |
| 791 | |
| 792 | // If we selected just the header for the loop top, look for a potentially |
| 793 | // profitable exit block in the event that rotating the loop can eliminate |
| 794 | // branches by placing an exit edge at the bottom. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 795 | MachineBasicBlock *ExitingBB = nullptr; |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 796 | if (LoopTop == L.getHeader()) |
| 797 | ExitingBB = findBestLoopExit(F, L, LoopBlockSet); |
| 798 | |
| 799 | BlockChain &LoopChain = *BlockToChain[LoopTop]; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 800 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 801 | // FIXME: This is a really lame way of walking the chains in the loop: we |
| 802 | // walk the blocks, and use a set to prevent visiting a particular chain |
| 803 | // twice. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 804 | SmallPtrSet<BlockChain *, 4> UpdatedPreds; |
Jakub Staszak | 190c712 | 2011-12-07 19:46:10 +0000 | [diff] [blame] | 805 | assert(LoopChain.LoopPredecessors == 0); |
| 806 | UpdatedPreds.insert(&LoopChain); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 807 | for (MachineBasicBlock *LoopBB : L.getBlocks()) { |
| 808 | BlockChain &Chain = *BlockToChain[LoopBB]; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 809 | if (!UpdatedPreds.insert(&Chain).second) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 810 | continue; |
| 811 | |
| 812 | assert(Chain.LoopPredecessors == 0); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 813 | for (MachineBasicBlock *ChainBB : Chain) { |
| 814 | assert(BlockToChain[ChainBB] == &Chain); |
| 815 | for (MachineBasicBlock *Pred : ChainBB->predecessors()) { |
| 816 | if (BlockToChain[Pred] == &Chain || !LoopBlockSet.count(Pred)) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 817 | continue; |
| 818 | ++Chain.LoopPredecessors; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if (Chain.LoopPredecessors == 0) |
Chandler Carruth | d394baf | 2011-11-24 08:46:04 +0000 | [diff] [blame] | 823 | BlockWorkList.push_back(*Chain.begin()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 824 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 825 | |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 826 | buildChain(LoopTop, LoopChain, BlockWorkList, &LoopBlockSet); |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 827 | rotateLoop(LoopChain, ExitingBB, LoopBlockSet); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 828 | |
| 829 | DEBUG({ |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 830 | // Crash at the end so we get all of the debugging output first. |
| 831 | bool BadLoop = false; |
| 832 | if (LoopChain.LoopPredecessors) { |
| 833 | BadLoop = true; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 834 | dbgs() << "Loop chain contains a block without its preds placed!\n" |
| 835 | << " Loop header: " << getBlockName(*L.block_begin()) << "\n" |
| 836 | << " Chain header: " << getBlockName(*LoopChain.begin()) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 837 | } |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 838 | for (MachineBasicBlock *ChainBB : LoopChain) { |
| 839 | dbgs() << " ... " << getBlockName(ChainBB) << "\n"; |
| 840 | if (!LoopBlockSet.erase(ChainBB)) { |
Chandler Carruth | 0a31d14 | 2011-11-14 10:55:53 +0000 | [diff] [blame] | 841 | // We don't mark the loop as bad here because there are real situations |
| 842 | // where this can occur. For example, with an unanalyzable fallthrough |
Chandler Carruth | 99fe42f | 2011-11-23 10:35:36 +0000 | [diff] [blame] | 843 | // from a loop block to a non-loop block or vice versa. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 844 | dbgs() << "Loop chain contains a block not contained by the loop!\n" |
| 845 | << " Loop header: " << getBlockName(*L.block_begin()) << "\n" |
| 846 | << " Chain header: " << getBlockName(*LoopChain.begin()) << "\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 847 | << " Bad block: " << getBlockName(ChainBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 848 | } |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 849 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 850 | |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 851 | if (!LoopBlockSet.empty()) { |
| 852 | BadLoop = true; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 853 | for (MachineBasicBlock *LoopBB : LoopBlockSet) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 854 | dbgs() << "Loop contains blocks never placed into a chain!\n" |
| 855 | << " Loop header: " << getBlockName(*L.block_begin()) << "\n" |
| 856 | << " Chain header: " << getBlockName(*LoopChain.begin()) << "\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 857 | << " Bad block: " << getBlockName(LoopBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 858 | } |
| 859 | assert(!BadLoop && "Detected problems with the placement of this loop."); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 860 | }); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 863 | void MachineBlockPlacement::buildCFGChains(MachineFunction &F) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 864 | // Ensure that every BB in the function has an associated chain to simplify |
| 865 | // the assumptions of the remaining algorithm. |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 866 | SmallVector<MachineOperand, 4> Cond; // For AnalyzeBranch. |
| 867 | for (MachineFunction::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) { |
| 868 | MachineBasicBlock *BB = FI; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 869 | BlockChain *Chain = |
| 870 | new (ChainAllocator.Allocate()) BlockChain(BlockToChain, BB); |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 871 | // Also, merge any blocks which we cannot reason about and must preserve |
| 872 | // the exact fallthrough behavior for. |
| 873 | for (;;) { |
| 874 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 875 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch. |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 876 | if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond) || !FI->canFallThrough()) |
| 877 | break; |
| 878 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 879 | MachineFunction::iterator NextFI(std::next(FI)); |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 880 | MachineBasicBlock *NextBB = NextFI; |
| 881 | // Ensure that the layout successor is a viable block, as we know that |
| 882 | // fallthrough is a possibility. |
| 883 | assert(NextFI != FE && "Can't fallthrough past the last block."); |
| 884 | DEBUG(dbgs() << "Pre-merging due to unanalyzable fallthrough: " |
| 885 | << getBlockName(BB) << " -> " << getBlockName(NextBB) |
| 886 | << "\n"); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 887 | Chain->merge(NextBB, nullptr); |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 888 | FI = NextFI; |
| 889 | BB = NextBB; |
| 890 | } |
| 891 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 892 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 893 | if (OutlineOptionalBranches) { |
| 894 | // Find the nearest common dominator of all of F's terminators. |
| 895 | MachineBasicBlock *Terminator = nullptr; |
| 896 | for (MachineBasicBlock &MBB : F) { |
| 897 | if (MBB.succ_size() == 0) { |
| 898 | if (Terminator == nullptr) |
| 899 | Terminator = &MBB; |
| 900 | else |
| 901 | Terminator = MDT->findNearestCommonDominator(Terminator, &MBB); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | // MBBs dominating this common dominator are unavoidable. |
| 906 | UnavoidableBlocks.clear(); |
| 907 | for (MachineBasicBlock &MBB : F) { |
| 908 | if (MDT->dominates(&MBB, Terminator)) { |
| 909 | UnavoidableBlocks.insert(&MBB); |
| 910 | } |
| 911 | } |
| 912 | } |
| 913 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 914 | // Build any loop-based chains. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 915 | for (MachineLoop *L : *MLI) |
| 916 | buildLoopChains(F, *L); |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 917 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 918 | SmallVector<MachineBasicBlock *, 16> BlockWorkList; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 919 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 920 | SmallPtrSet<BlockChain *, 4> UpdatedPreds; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 921 | for (MachineBasicBlock &MBB : F) { |
| 922 | BlockChain &Chain = *BlockToChain[&MBB]; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 923 | if (!UpdatedPreds.insert(&Chain).second) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 924 | continue; |
| 925 | |
| 926 | assert(Chain.LoopPredecessors == 0); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 927 | for (MachineBasicBlock *ChainBB : Chain) { |
| 928 | assert(BlockToChain[ChainBB] == &Chain); |
| 929 | for (MachineBasicBlock *Pred : ChainBB->predecessors()) { |
| 930 | if (BlockToChain[Pred] == &Chain) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 931 | continue; |
| 932 | ++Chain.LoopPredecessors; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | if (Chain.LoopPredecessors == 0) |
Chandler Carruth | d394baf | 2011-11-24 08:46:04 +0000 | [diff] [blame] | 937 | BlockWorkList.push_back(*Chain.begin()); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | BlockChain &FunctionChain = *BlockToChain[&F.front()]; |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 941 | buildChain(&F.front(), FunctionChain, BlockWorkList); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 942 | |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 943 | #ifndef NDEBUG |
Matt Arsenault | 79d55f5 | 2013-12-05 20:02:18 +0000 | [diff] [blame] | 944 | typedef SmallPtrSet<MachineBasicBlock *, 16> FunctionBlockSetType; |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 945 | #endif |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 946 | DEBUG({ |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 947 | // Crash at the end so we get all of the debugging output first. |
| 948 | bool BadFunc = false; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 949 | FunctionBlockSetType FunctionBlockSet; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 950 | for (MachineBasicBlock &MBB : F) |
| 951 | FunctionBlockSet.insert(&MBB); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 952 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 953 | for (MachineBasicBlock *ChainBB : FunctionChain) |
| 954 | if (!FunctionBlockSet.erase(ChainBB)) { |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 955 | BadFunc = true; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 956 | dbgs() << "Function chain contains a block not in the function!\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 957 | << " Bad block: " << getBlockName(ChainBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 958 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 959 | |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 960 | if (!FunctionBlockSet.empty()) { |
| 961 | BadFunc = true; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 962 | for (MachineBasicBlock *RemainingBB : FunctionBlockSet) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 963 | dbgs() << "Function contains blocks never placed into a chain!\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 964 | << " Bad block: " << getBlockName(RemainingBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 965 | } |
| 966 | assert(!BadFunc && "Detected problems with the block placement."); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 967 | }); |
| 968 | |
| 969 | // Splice the blocks into place. |
| 970 | MachineFunction::iterator InsertPos = F.begin(); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 971 | for (MachineBasicBlock *ChainBB : FunctionChain) { |
| 972 | DEBUG(dbgs() << (ChainBB == *FunctionChain.begin() ? "Placing chain " |
| 973 | : " ... ") |
| 974 | << getBlockName(ChainBB) << "\n"); |
| 975 | if (InsertPos != MachineFunction::iterator(ChainBB)) |
| 976 | F.splice(InsertPos, ChainBB); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 977 | else |
| 978 | ++InsertPos; |
| 979 | |
| 980 | // Update the terminator of the previous block. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 981 | if (ChainBB == *FunctionChain.begin()) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 982 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 983 | MachineBasicBlock *PrevBB = std::prev(MachineFunction::iterator(ChainBB)); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 984 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 985 | // FIXME: It would be awesome of updateTerminator would just return rather |
| 986 | // than assert when the branch cannot be analyzed in order to remove this |
| 987 | // boiler plate. |
| 988 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 989 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch. |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 990 | if (!TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) { |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 991 | // The "PrevBB" is not yet updated to reflect current code layout, so, |
| 992 | // o. it may fall-through to a block without explict "goto" instruction |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 993 | // before layout, and no longer fall-through it after layout; or |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 994 | // o. just opposite. |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 995 | // |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 996 | // AnalyzeBranch() may return erroneous value for FBB when these two |
| 997 | // situations take place. For the first scenario FBB is mistakenly set |
| 998 | // NULL; for the 2nd scenario, the FBB, which is expected to be NULL, |
| 999 | // is mistakenly pointing to "*BI". |
| 1000 | // |
| 1001 | bool needUpdateBr = true; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1002 | if (!Cond.empty() && (!FBB || FBB == ChainBB)) { |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1003 | PrevBB->updateTerminator(); |
| 1004 | needUpdateBr = false; |
| 1005 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1006 | TBB = FBB = nullptr; |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1007 | if (TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) { |
| 1008 | // FIXME: This should never take place. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1009 | TBB = FBB = nullptr; |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1013 | // If PrevBB has a two-way branch, try to re-order the branches |
| 1014 | // such that we branch to the successor with higher weight first. |
| 1015 | if (TBB && !Cond.empty() && FBB && |
| 1016 | MBPI->getEdgeWeight(PrevBB, FBB) > MBPI->getEdgeWeight(PrevBB, TBB) && |
| 1017 | !TII->ReverseBranchCondition(Cond)) { |
| 1018 | DEBUG(dbgs() << "Reverse order of the two branches: " |
| 1019 | << getBlockName(PrevBB) << "\n"); |
| 1020 | DEBUG(dbgs() << " Edge weight: " << MBPI->getEdgeWeight(PrevBB, FBB) |
| 1021 | << " vs " << MBPI->getEdgeWeight(PrevBB, TBB) << "\n"); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1022 | DebugLoc dl; // FIXME: this is nowhere |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1023 | TII->RemoveBranch(*PrevBB); |
| 1024 | TII->InsertBranch(*PrevBB, FBB, TBB, Cond, dl); |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1025 | needUpdateBr = true; |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1026 | } |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1027 | if (needUpdateBr) |
| 1028 | PrevBB->updateTerminator(); |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1029 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1030 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Fixup the last block. |
| 1033 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1034 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1035 | if (!TII->AnalyzeBranch(F.back(), TBB, FBB, Cond)) |
| 1036 | F.back().updateTerminator(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1037 | |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 1038 | // Walk through the backedges of the function now that we have fully laid out |
| 1039 | // the basic blocks and align the destination of each backedge. We don't rely |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1040 | // exclusively on the loop info here so that we can align backedges in |
| 1041 | // unnatural CFGs and backedges that were introduced purely because of the |
| 1042 | // loop rotations done during this layout pass. |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 1043 | if (F.getFunction()->hasFnAttribute(Attribute::OptimizeForSize)) |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1044 | return; |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1045 | if (FunctionChain.begin() == FunctionChain.end()) |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1046 | return; // Empty chain. |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1047 | |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1048 | const BranchProbability ColdProb(1, 5); // 20% |
| 1049 | BlockFrequency EntryFreq = MBFI->getBlockFreq(F.begin()); |
| 1050 | BlockFrequency WeightedEntryFreq = EntryFreq * ColdProb; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1051 | for (MachineBasicBlock *ChainBB : FunctionChain) { |
| 1052 | if (ChainBB == *FunctionChain.begin()) |
| 1053 | continue; |
| 1054 | |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1055 | // Don't align non-looping basic blocks. These are unlikely to execute |
| 1056 | // enough times to matter in practice. Note that we'll still handle |
| 1057 | // unnatural CFGs inside of a natural outer loop (the common case) and |
| 1058 | // rotated loops. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1059 | MachineLoop *L = MLI->getLoopFor(ChainBB); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1060 | if (!L) |
| 1061 | continue; |
| 1062 | |
Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 1063 | unsigned Align = TLI->getPrefLoopAlignment(L); |
| 1064 | if (!Align) |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1065 | continue; // Don't care about loop alignment. |
Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 1066 | |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1067 | // If the block is cold relative to the function entry don't waste space |
| 1068 | // aligning it. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1069 | BlockFrequency Freq = MBFI->getBlockFreq(ChainBB); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1070 | if (Freq < WeightedEntryFreq) |
| 1071 | continue; |
| 1072 | |
| 1073 | // If the block is cold relative to its loop header, don't align it |
| 1074 | // regardless of what edges into the block exist. |
| 1075 | MachineBasicBlock *LoopHeader = L->getHeader(); |
| 1076 | BlockFrequency LoopHeaderFreq = MBFI->getBlockFreq(LoopHeader); |
| 1077 | if (Freq < (LoopHeaderFreq * ColdProb)) |
| 1078 | continue; |
| 1079 | |
| 1080 | // Check for the existence of a non-layout predecessor which would benefit |
| 1081 | // from aligning this block. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1082 | MachineBasicBlock *LayoutPred = |
| 1083 | &*std::prev(MachineFunction::iterator(ChainBB)); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1084 | |
| 1085 | // Force alignment if all the predecessors are jumps. We already checked |
| 1086 | // that the block isn't cold above. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1087 | if (!LayoutPred->isSuccessor(ChainBB)) { |
| 1088 | ChainBB->setAlignment(Align); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1089 | continue; |
| 1090 | } |
| 1091 | |
| 1092 | // Align this block if the layout predecessor's edge into this block is |
Nadav Rotem | 6036f58 | 2013-03-29 16:34:23 +0000 | [diff] [blame] | 1093 | // cold relative to the block. When this is true, other predecessors make up |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1094 | // all of the hot entries into the block and thus alignment is likely to be |
| 1095 | // important. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1096 | BranchProbability LayoutProb = |
| 1097 | MBPI->getEdgeProbability(LayoutPred, ChainBB); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1098 | BlockFrequency LayoutEdgeFreq = MBFI->getBlockFreq(LayoutPred) * LayoutProb; |
| 1099 | if (LayoutEdgeFreq <= (Freq * ColdProb)) |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1100 | ChainBB->setAlignment(Align); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 1101 | } |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1104 | bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &F) { |
| 1105 | // Check for single-block functions and skip them. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1106 | if (std::next(F.begin()) == F.end()) |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 1109 | if (skipOptnoneFunction(*F.getFunction())) |
| 1110 | return false; |
| 1111 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1112 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
| 1113 | MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1114 | MLI = &getAnalysis<MachineLoopInfo>(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1115 | TII = F.getSubtarget().getInstrInfo(); |
| 1116 | TLI = F.getSubtarget().getTargetLowering(); |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 1117 | MDT = &getAnalysis<MachineDominatorTree>(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1118 | assert(BlockToChain.empty()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1119 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1120 | buildCFGChains(F); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1121 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1122 | BlockToChain.clear(); |
Chandler Carruth | fd9b4d9 | 2011-11-14 10:57:23 +0000 | [diff] [blame] | 1123 | ChainAllocator.DestroyAll(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1124 | |
Nadav Rotem | c0adc9f | 2013-04-12 01:24:16 +0000 | [diff] [blame] | 1125 | if (AlignAllBlock) |
| 1126 | // Align all of the blocks in the function to a specific alignment. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1127 | for (MachineBasicBlock &MBB : F) |
| 1128 | MBB.setAlignment(AlignAllBlock); |
Nadav Rotem | c0adc9f | 2013-04-12 01:24:16 +0000 | [diff] [blame] | 1129 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1130 | // We always return true as we have no way to track whether the final order |
| 1131 | // differs from the original order. |
| 1132 | return true; |
| 1133 | } |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1134 | |
| 1135 | namespace { |
| 1136 | /// \brief A pass to compute block placement statistics. |
| 1137 | /// |
| 1138 | /// A separate pass to compute interesting statistics for evaluating block |
| 1139 | /// placement. This is separate from the actual placement pass so that they can |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 1140 | /// be computed in the absence of any placement transformations or when using |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1141 | /// alternative placement strategies. |
| 1142 | class MachineBlockPlacementStats : public MachineFunctionPass { |
| 1143 | /// \brief A handle to the branch probability pass. |
| 1144 | const MachineBranchProbabilityInfo *MBPI; |
| 1145 | |
| 1146 | /// \brief A handle to the function-wide block frequency pass. |
| 1147 | const MachineBlockFrequencyInfo *MBFI; |
| 1148 | |
| 1149 | public: |
| 1150 | static char ID; // Pass identification, replacement for typeid |
| 1151 | MachineBlockPlacementStats() : MachineFunctionPass(ID) { |
| 1152 | initializeMachineBlockPlacementStatsPass(*PassRegistry::getPassRegistry()); |
| 1153 | } |
| 1154 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1155 | bool runOnMachineFunction(MachineFunction &F) override; |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1156 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1157 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1158 | AU.addRequired<MachineBranchProbabilityInfo>(); |
| 1159 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 1160 | AU.setPreservesAll(); |
| 1161 | MachineFunctionPass::getAnalysisUsage(AU); |
| 1162 | } |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1163 | }; |
| 1164 | } |
| 1165 | |
| 1166 | char MachineBlockPlacementStats::ID = 0; |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 1167 | char &llvm::MachineBlockPlacementStatsID = MachineBlockPlacementStats::ID; |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1168 | INITIALIZE_PASS_BEGIN(MachineBlockPlacementStats, "block-placement-stats", |
| 1169 | "Basic Block Placement Stats", false, false) |
| 1170 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
| 1171 | INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) |
| 1172 | INITIALIZE_PASS_END(MachineBlockPlacementStats, "block-placement-stats", |
| 1173 | "Basic Block Placement Stats", false, false) |
| 1174 | |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1175 | bool MachineBlockPlacementStats::runOnMachineFunction(MachineFunction &F) { |
| 1176 | // Check for single-block functions and skip them. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1177 | if (std::next(F.begin()) == F.end()) |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1178 | return false; |
| 1179 | |
| 1180 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
| 1181 | MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); |
| 1182 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1183 | for (MachineBasicBlock &MBB : F) { |
| 1184 | BlockFrequency BlockFreq = MBFI->getBlockFreq(&MBB); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1185 | Statistic &NumBranches = |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1186 | (MBB.succ_size() > 1) ? NumCondBranches : NumUncondBranches; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1187 | Statistic &BranchTakenFreq = |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1188 | (MBB.succ_size() > 1) ? CondBranchTakenFreq : UncondBranchTakenFreq; |
| 1189 | for (MachineBasicBlock *Succ : MBB.successors()) { |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1190 | // Skip if this successor is a fallthrough. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1191 | if (MBB.isLayoutSuccessor(Succ)) |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1192 | continue; |
| 1193 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1194 | BlockFrequency EdgeFreq = |
| 1195 | BlockFreq * MBPI->getEdgeProbability(&MBB, Succ); |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1196 | ++NumBranches; |
| 1197 | BranchTakenFreq += EdgeFreq.getFrequency(); |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | return false; |
| 1202 | } |
| 1203 | |