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