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" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 44 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 45 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetLowering.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 47 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 48 | #include <algorithm> |
| 49 | using namespace llvm; |
| 50 | |
Chandler Carruth | d0dced5 | 2015-03-05 02:28:25 +0000 | [diff] [blame] | 51 | #define DEBUG_TYPE "block-placement" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 52 | |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 53 | STATISTIC(NumCondBranches, "Number of conditional branches"); |
Craig Topper | 77ec077 | 2015-09-16 03:52:32 +0000 | [diff] [blame] | 54 | STATISTIC(NumUncondBranches, "Number of unconditional branches"); |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 55 | STATISTIC(CondBranchTakenFreq, |
| 56 | "Potential frequency of taking conditional branches"); |
| 57 | STATISTIC(UncondBranchTakenFreq, |
| 58 | "Potential frequency of taking unconditional branches"); |
| 59 | |
Nadav Rotem | c3b0f50 | 2013-04-12 00:48:32 +0000 | [diff] [blame] | 60 | static cl::opt<unsigned> AlignAllBlock("align-all-blocks", |
| 61 | cl::desc("Force the alignment of all " |
| 62 | "blocks in the function."), |
| 63 | cl::init(0), cl::Hidden); |
| 64 | |
Geoff Berry | 10494ac | 2016-01-21 17:25:52 +0000 | [diff] [blame] | 65 | static cl::opt<unsigned> AlignAllNonFallThruBlocks( |
| 66 | "align-all-nofallthru-blocks", |
| 67 | cl::desc("Force the alignment of all " |
| 68 | "blocks that have no fall-through predecessors (i.e. don't add " |
| 69 | "nops that are executed)."), |
| 70 | cl::init(0), cl::Hidden); |
| 71 | |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 72 | // FIXME: Find a good default for this flag and remove the flag. |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 73 | static cl::opt<unsigned> ExitBlockBias( |
| 74 | "block-placement-exit-block-bias", |
| 75 | cl::desc("Block frequency percentage a loop exit block needs " |
| 76 | "over the original exit to be considered the new exit."), |
| 77 | cl::init(0), cl::Hidden); |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 78 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 79 | static cl::opt<bool> OutlineOptionalBranches( |
| 80 | "outline-optional-branches", |
| 81 | cl::desc("Put completely optional branches, i.e. branches with a common " |
| 82 | "post dominator, out of line."), |
| 83 | cl::init(false), cl::Hidden); |
| 84 | |
Daniel Jasper | 214997c | 2015-03-20 10:00:37 +0000 | [diff] [blame] | 85 | static cl::opt<unsigned> OutlineOptionalThreshold( |
| 86 | "outline-optional-threshold", |
| 87 | cl::desc("Don't outline optional branches that are a single block with an " |
| 88 | "instruction count below this threshold"), |
| 89 | cl::init(4), cl::Hidden); |
| 90 | |
Cong Hou | b90b9e0 | 2015-11-02 21:24:00 +0000 | [diff] [blame] | 91 | static cl::opt<unsigned> LoopToColdBlockRatio( |
| 92 | "loop-to-cold-block-ratio", |
| 93 | cl::desc("Outline loop blocks from loop chain if (frequency of loop) / " |
| 94 | "(frequency of block) is greater than this ratio"), |
| 95 | cl::init(5), cl::Hidden); |
| 96 | |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 97 | static cl::opt<bool> |
| 98 | PreciseRotationCost("precise-rotation-cost", |
| 99 | cl::desc("Model the cost of loop rotation more " |
| 100 | "precisely by using profile data."), |
| 101 | cl::init(false), cl::Hidden); |
| 102 | |
| 103 | static cl::opt<unsigned> MisfetchCost( |
| 104 | "misfetch-cost", |
| 105 | cl::desc("Cost that models the probablistic risk of an instruction " |
| 106 | "misfetch due to a jump comparing to falling through, whose cost " |
| 107 | "is zero."), |
| 108 | cl::init(1), cl::Hidden); |
| 109 | |
| 110 | static cl::opt<unsigned> JumpInstCost("jump-inst-cost", |
| 111 | cl::desc("Cost of jump instructions."), |
| 112 | cl::init(1), cl::Hidden); |
| 113 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 114 | namespace { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 115 | class BlockChain; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 116 | /// \brief Type for our function-wide basic block -> block chain mapping. |
| 117 | typedef DenseMap<MachineBasicBlock *, BlockChain *> BlockToChainMapType; |
| 118 | } |
| 119 | |
| 120 | namespace { |
| 121 | /// \brief A chain of blocks which will be laid out contiguously. |
| 122 | /// |
| 123 | /// This is the datastructure representing a chain of consecutive blocks that |
| 124 | /// are profitable to layout together in order to maximize fallthrough |
Chandler Carruth | 9139f44 | 2012-06-26 05:16:37 +0000 | [diff] [blame] | 125 | /// probabilities and code locality. We also can use a block chain to represent |
| 126 | /// a sequence of basic blocks which have some external (correctness) |
| 127 | /// requirement for sequential layout. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 128 | /// |
Chandler Carruth | 9139f44 | 2012-06-26 05:16:37 +0000 | [diff] [blame] | 129 | /// Chains can be built around a single basic block and can be merged to grow |
| 130 | /// them. They participate in a block-to-chain mapping, which is updated |
| 131 | /// automatically as chains are merged together. |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 132 | class BlockChain { |
| 133 | /// \brief The sequence of blocks belonging to this chain. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 134 | /// |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 135 | /// This is the sequence of blocks for a particular chain. These will be laid |
| 136 | /// out in-order within the function. |
| 137 | SmallVector<MachineBasicBlock *, 4> Blocks; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 138 | |
| 139 | /// \brief A handle to the function-wide basic block to block chain mapping. |
| 140 | /// |
| 141 | /// This is retained in each block chain to simplify the computation of child |
| 142 | /// block chains for SCC-formation and iteration. We store the edges to child |
| 143 | /// basic blocks, and map them back to their associated chains using this |
| 144 | /// structure. |
| 145 | BlockToChainMapType &BlockToChain; |
| 146 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 147 | public: |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 148 | /// \brief Construct a new BlockChain. |
| 149 | /// |
| 150 | /// This builds a new block chain representing a single basic block in the |
| 151 | /// function. It also registers itself as the chain that block participates |
| 152 | /// in with the BlockToChain mapping. |
| 153 | BlockChain(BlockToChainMapType &BlockToChain, MachineBasicBlock *BB) |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 154 | : Blocks(1, BB), BlockToChain(BlockToChain), UnscheduledPredecessors(0) { |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 155 | assert(BB && "Cannot create a chain with a null basic block"); |
| 156 | BlockToChain[BB] = this; |
| 157 | } |
| 158 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 159 | /// \brief Iterator over blocks within the chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 160 | typedef SmallVectorImpl<MachineBasicBlock *>::iterator iterator; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 161 | |
| 162 | /// \brief Beginning of blocks within the chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 163 | iterator begin() { return Blocks.begin(); } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 164 | |
| 165 | /// \brief End of blocks within the chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 166 | iterator end() { return Blocks.end(); } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 167 | |
| 168 | /// \brief Merge a block chain into this one. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 169 | /// |
| 170 | /// This routine merges a block chain into this one. It takes care of forming |
| 171 | /// a contiguous sequence of basic blocks, updating the edge list, and |
| 172 | /// updating the block -> chain mapping. It does not free or tear down the |
| 173 | /// 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] | 174 | void merge(MachineBasicBlock *BB, BlockChain *Chain) { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 175 | assert(BB); |
| 176 | assert(!Blocks.empty()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 177 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 178 | // Fast path in case we don't have a chain already. |
| 179 | if (!Chain) { |
| 180 | assert(!BlockToChain[BB]); |
| 181 | Blocks.push_back(BB); |
| 182 | BlockToChain[BB] = this; |
| 183 | return; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 186 | assert(BB == *Chain->begin()); |
| 187 | assert(Chain->begin() != Chain->end()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 188 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 189 | // Update the incoming blocks to point to this chain, and add them to the |
| 190 | // chain structure. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 191 | for (MachineBasicBlock *ChainBB : *Chain) { |
| 192 | Blocks.push_back(ChainBB); |
| 193 | assert(BlockToChain[ChainBB] == Chain && "Incoming blocks not in chain"); |
| 194 | BlockToChain[ChainBB] = this; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 195 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 196 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 197 | |
Chandler Carruth | 4915890 | 2012-04-08 14:37:01 +0000 | [diff] [blame] | 198 | #ifndef NDEBUG |
| 199 | /// \brief Dump the blocks in this chain. |
Nico Weber | 7408c70 | 2014-01-03 22:53:37 +0000 | [diff] [blame] | 200 | LLVM_DUMP_METHOD void dump() { |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 201 | for (MachineBasicBlock *MBB : *this) |
| 202 | MBB->dump(); |
Chandler Carruth | 4915890 | 2012-04-08 14:37:01 +0000 | [diff] [blame] | 203 | } |
| 204 | #endif // NDEBUG |
| 205 | |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 206 | /// \brief Count of predecessors of any block within the chain which have not |
| 207 | /// yet been scheduled. In general, we will delay scheduling this chain |
| 208 | /// until those predecessors are scheduled (or we find a sufficiently good |
| 209 | /// reason to override this heuristic.) Note that when forming loop chains, |
| 210 | /// blocks outside the loop are ignored and treated as if they were already |
| 211 | /// scheduled. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 212 | /// |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 213 | /// Note: This field is reinitialized multiple times - once for each loop, |
| 214 | /// and then once for the function as a whole. |
| 215 | unsigned UnscheduledPredecessors; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 216 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 217 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 218 | |
| 219 | namespace { |
| 220 | class MachineBlockPlacement : public MachineFunctionPass { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 221 | /// \brief A typedef for a block filter set. |
| 222 | typedef SmallPtrSet<MachineBasicBlock *, 16> BlockFilterSet; |
| 223 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 224 | /// \brief A handle to the branch probability pass. |
| 225 | const MachineBranchProbabilityInfo *MBPI; |
| 226 | |
| 227 | /// \brief A handle to the function-wide block frequency pass. |
| 228 | const MachineBlockFrequencyInfo *MBFI; |
| 229 | |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 230 | /// \brief A handle to the loop info. |
| 231 | const MachineLoopInfo *MLI; |
| 232 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 233 | /// \brief A handle to the target's instruction info. |
| 234 | const TargetInstrInfo *TII; |
| 235 | |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 236 | /// \brief A handle to the target's lowering info. |
Benjamin Kramer | 56b31bd | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 237 | const TargetLoweringBase *TLI; |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 238 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 239 | /// \brief A handle to the post dominator tree. |
| 240 | MachineDominatorTree *MDT; |
| 241 | |
| 242 | /// \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] | 243 | /// all terminators of the MachineFunction. |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 244 | SmallPtrSet<MachineBasicBlock *, 4> UnavoidableBlocks; |
| 245 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 246 | /// \brief Allocator and owner of BlockChain structures. |
| 247 | /// |
Chandler Carruth | 9139f44 | 2012-06-26 05:16:37 +0000 | [diff] [blame] | 248 | /// We build BlockChains lazily while processing the loop structure of |
| 249 | /// a function. To reduce malloc traffic, we allocate them using this |
| 250 | /// slab-like allocator, and destroy them after the pass completes. An |
| 251 | /// important guarantee is that this allocator produces stable pointers to |
| 252 | /// the chains. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 253 | SpecificBumpPtrAllocator<BlockChain> ChainAllocator; |
| 254 | |
| 255 | /// \brief Function wide BasicBlock to BlockChain mapping. |
| 256 | /// |
| 257 | /// This mapping allows efficiently moving from any given basic block to the |
| 258 | /// BlockChain it participates in, if any. We use it to, among other things, |
| 259 | /// allow implicitly defining edges between chains as the existing edges |
| 260 | /// between basic blocks. |
| 261 | DenseMap<MachineBasicBlock *, BlockChain *> BlockToChain; |
| 262 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 263 | void markChainSuccessors(BlockChain &Chain, MachineBasicBlock *LoopHeaderBB, |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 264 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 265 | const BlockFilterSet *BlockFilter = nullptr); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 266 | MachineBasicBlock *selectBestSuccessor(MachineBasicBlock *BB, |
| 267 | BlockChain &Chain, |
| 268 | const BlockFilterSet *BlockFilter); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 269 | MachineBasicBlock * |
| 270 | selectBestCandidateBlock(BlockChain &Chain, |
Amaury Sechet | 9ee4ddd | 2016-04-07 06:34:47 +0000 | [diff] [blame^] | 271 | SmallVectorImpl<MachineBasicBlock *> &WorkList); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 272 | MachineBasicBlock * |
| 273 | getFirstUnplacedBlock(MachineFunction &F, const BlockChain &PlacedChain, |
| 274 | MachineFunction::iterator &PrevUnplacedBlockIt, |
| 275 | const BlockFilterSet *BlockFilter); |
Amaury Sechet | eae09c2 | 2016-03-14 21:24:11 +0000 | [diff] [blame] | 276 | |
| 277 | /// \brief Add a basic block to the work list if it is apropriate. |
| 278 | /// |
| 279 | /// If the optional parameter BlockFilter is provided, only MBB |
| 280 | /// present in the set will be added to the worklist. If nullptr |
| 281 | /// is provided, no filtering occurs. |
| 282 | void fillWorkLists(MachineBasicBlock *MBB, |
| 283 | SmallPtrSetImpl<BlockChain *> &UpdatedPreds, |
| 284 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
| 285 | const BlockFilterSet *BlockFilter); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 286 | void buildChain(MachineBasicBlock *BB, BlockChain &Chain, |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 287 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 288 | const BlockFilterSet *BlockFilter = nullptr); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 289 | MachineBasicBlock *findBestLoopTop(MachineLoop &L, |
| 290 | const BlockFilterSet &LoopBlockSet); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 291 | MachineBasicBlock *findBestLoopExit(MachineFunction &F, MachineLoop &L, |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 292 | const BlockFilterSet &LoopBlockSet); |
Cong Hou | b90b9e0 | 2015-11-02 21:24:00 +0000 | [diff] [blame] | 293 | BlockFilterSet collectLoopBlockSet(MachineFunction &F, MachineLoop &L); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 294 | void buildLoopChains(MachineFunction &F, MachineLoop &L); |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 295 | void rotateLoop(BlockChain &LoopChain, MachineBasicBlock *ExitingBB, |
| 296 | const BlockFilterSet &LoopBlockSet); |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 297 | void rotateLoopWithProfile(BlockChain &LoopChain, MachineLoop &L, |
| 298 | const BlockFilterSet &LoopBlockSet); |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 299 | void buildCFGChains(MachineFunction &F); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 300 | |
| 301 | public: |
| 302 | static char ID; // Pass identification, replacement for typeid |
| 303 | MachineBlockPlacement() : MachineFunctionPass(ID) { |
| 304 | initializeMachineBlockPlacementPass(*PassRegistry::getPassRegistry()); |
| 305 | } |
| 306 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 307 | bool runOnMachineFunction(MachineFunction &F) override; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 308 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 309 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 310 | AU.addRequired<MachineBranchProbabilityInfo>(); |
| 311 | AU.addRequired<MachineBlockFrequencyInfo>(); |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 312 | AU.addRequired<MachineDominatorTree>(); |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 313 | AU.addRequired<MachineLoopInfo>(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 314 | MachineFunctionPass::getAnalysisUsage(AU); |
| 315 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 316 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 317 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 318 | |
| 319 | char MachineBlockPlacement::ID = 0; |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 320 | char &llvm::MachineBlockPlacementID = MachineBlockPlacement::ID; |
Chandler Carruth | d0dced5 | 2015-03-05 02:28:25 +0000 | [diff] [blame] | 321 | INITIALIZE_PASS_BEGIN(MachineBlockPlacement, "block-placement", |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 322 | "Branch Probability Basic Block Placement", false, false) |
| 323 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
| 324 | INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 325 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 326 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
Chandler Carruth | d0dced5 | 2015-03-05 02:28:25 +0000 | [diff] [blame] | 327 | INITIALIZE_PASS_END(MachineBlockPlacement, "block-placement", |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 328 | "Branch Probability Basic Block Placement", false, false) |
| 329 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 330 | #ifndef NDEBUG |
| 331 | /// \brief Helper to print the name of a MBB. |
| 332 | /// |
| 333 | /// Only used by debug logging. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 334 | static std::string getBlockName(MachineBasicBlock *BB) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 335 | std::string Result; |
| 336 | raw_string_ostream OS(Result); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 337 | OS << "BB#" << BB->getNumber(); |
Philip Reames | b9688f4 | 2016-03-02 21:45:13 +0000 | [diff] [blame] | 338 | OS << " ('" << BB->getName() << "')"; |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 339 | OS.flush(); |
| 340 | return Result; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 341 | } |
| 342 | #endif |
| 343 | |
Chandler Carruth | eb4ec3a | 2011-11-13 11:34:55 +0000 | [diff] [blame] | 344 | /// \brief Mark a chain's successors as having one fewer preds. |
| 345 | /// |
| 346 | /// When a chain is being merged into the "placed" chain, this routine will |
| 347 | /// quickly walk the successors of each block in the chain and mark them as |
| 348 | /// having one fewer active predecessor. It also adds any successors of this |
| 349 | /// chain which reach the zero-predecessor state to the worklist passed in. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 350 | void MachineBlockPlacement::markChainSuccessors( |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 351 | BlockChain &Chain, MachineBasicBlock *LoopHeaderBB, |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 352 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 353 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 354 | // Walk all the blocks in this chain, marking their successors as having |
| 355 | // a predecessor placed. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 356 | for (MachineBasicBlock *MBB : Chain) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 357 | // Add any successors for which this is the only un-placed in-loop |
| 358 | // predecessor to the worklist as a viable candidate for CFG-neutral |
| 359 | // placement. No subsequent placement of this block will violate the CFG |
| 360 | // shape, so we get to use heuristics to choose a favorable placement. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 361 | for (MachineBasicBlock *Succ : MBB->successors()) { |
| 362 | if (BlockFilter && !BlockFilter->count(Succ)) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 363 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 364 | BlockChain &SuccChain = *BlockToChain[Succ]; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 365 | // Disregard edges within a fixed chain, or edges to the loop header. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 366 | if (&Chain == &SuccChain || Succ == LoopHeaderBB) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 367 | continue; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 368 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 369 | // This is a cross-chain edge that is within the loop, so decrement the |
| 370 | // loop predecessor count of the destination chain. |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 371 | if (SuccChain.UnscheduledPredecessors > 0 && --SuccChain.UnscheduledPredecessors == 0) |
Chandler Carruth | d394baf | 2011-11-24 08:46:04 +0000 | [diff] [blame] | 372 | BlockWorkList.push_back(*SuccChain.begin()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 375 | } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 376 | |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 377 | /// \brief Select the best successor for a block. |
| 378 | /// |
| 379 | /// This looks across all successors of a particular block and attempts to |
| 380 | /// select the "best" one to be the layout successor. It only considers direct |
| 381 | /// successors which also pass the block filter. It will attempt to avoid |
| 382 | /// breaking CFG structure, but cave and break such structures in the case of |
| 383 | /// very hot successor edges. |
| 384 | /// |
| 385 | /// \returns The best successor block found, or null if none are viable. |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 386 | MachineBasicBlock * |
| 387 | MachineBlockPlacement::selectBestSuccessor(MachineBasicBlock *BB, |
| 388 | BlockChain &Chain, |
| 389 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 390 | const BranchProbability HotProb(4, 5); // 80% |
| 391 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 392 | MachineBasicBlock *BestSucc = nullptr; |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 393 | auto BestProb = BranchProbability::getZero(); |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 394 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 395 | // Adjust edge probabilities by excluding edges pointing to blocks that is |
| 396 | // either not in BlockFilter or is already in the current chain. Consider the |
| 397 | // following CFG: |
Cong Hou | 41cf1a5 | 2015-11-18 00:52:52 +0000 | [diff] [blame] | 398 | // |
| 399 | // --->A |
| 400 | // | / \ |
| 401 | // | B C |
| 402 | // | \ / \ |
| 403 | // ----D E |
| 404 | // |
| 405 | // Assume A->C is very hot (>90%), and C->D has a 50% probability, then after |
| 406 | // A->C is chosen as a fall-through, D won't be selected as a successor of C |
| 407 | // due to CFG constraint (the probability of C->D is not greater than |
| 408 | // HotProb). If we exclude E that is not in BlockFilter when calculating the |
| 409 | // probability of C->D, D will be selected and we will get A C D B as the |
| 410 | // layout of this loop. |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 411 | auto AdjustedSumProb = BranchProbability::getOne(); |
Cong Hou | 41cf1a5 | 2015-11-18 00:52:52 +0000 | [diff] [blame] | 412 | SmallVector<MachineBasicBlock *, 4> Successors; |
| 413 | for (MachineBasicBlock *Succ : BB->successors()) { |
| 414 | bool SkipSucc = false; |
| 415 | if (BlockFilter && !BlockFilter->count(Succ)) { |
| 416 | SkipSucc = true; |
| 417 | } else { |
| 418 | BlockChain *SuccChain = BlockToChain[Succ]; |
| 419 | if (SuccChain == &Chain) { |
Cong Hou | 41cf1a5 | 2015-11-18 00:52:52 +0000 | [diff] [blame] | 420 | SkipSucc = true; |
| 421 | } else if (Succ != *SuccChain->begin()) { |
| 422 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> Mid chain!\n"); |
| 423 | continue; |
| 424 | } |
| 425 | } |
| 426 | if (SkipSucc) |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 427 | AdjustedSumProb -= MBPI->getEdgeProbability(BB, Succ); |
Cong Hou | 41cf1a5 | 2015-11-18 00:52:52 +0000 | [diff] [blame] | 428 | else |
| 429 | Successors.push_back(Succ); |
| 430 | } |
| 431 | |
| 432 | DEBUG(dbgs() << "Attempting merge from: " << getBlockName(BB) << "\n"); |
| 433 | for (MachineBasicBlock *Succ : Successors) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 434 | BranchProbability SuccProb; |
| 435 | uint32_t SuccProbN = MBPI->getEdgeProbability(BB, Succ).getNumerator(); |
| 436 | uint32_t SuccProbD = AdjustedSumProb.getNumerator(); |
| 437 | if (SuccProbN >= SuccProbD) |
| 438 | SuccProb = BranchProbability::getOne(); |
| 439 | else |
| 440 | SuccProb = BranchProbability(SuccProbN, SuccProbD); |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 441 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 442 | // If we outline optional branches, look whether Succ is unavoidable, i.e. |
| 443 | // dominates all terminators of the MachineFunction. If it does, other |
| 444 | // successors must be optional. Don't do this for cold branches. |
| 445 | if (OutlineOptionalBranches && SuccProb > HotProb.getCompl() && |
Daniel Jasper | 214997c | 2015-03-20 10:00:37 +0000 | [diff] [blame] | 446 | UnavoidableBlocks.count(Succ) > 0) { |
| 447 | auto HasShortOptionalBranch = [&]() { |
| 448 | for (MachineBasicBlock *Pred : Succ->predecessors()) { |
| 449 | // Check whether there is an unplaced optional branch. |
| 450 | if (Pred == Succ || (BlockFilter && !BlockFilter->count(Pred)) || |
| 451 | BlockToChain[Pred] == &Chain) |
| 452 | continue; |
| 453 | // Check whether the optional branch has exactly one BB. |
| 454 | if (Pred->pred_size() > 1 || *Pred->pred_begin() != BB) |
| 455 | continue; |
| 456 | // Check whether the optional branch is small. |
| 457 | if (Pred->size() < OutlineOptionalThreshold) |
| 458 | return true; |
| 459 | } |
| 460 | return false; |
| 461 | }; |
| 462 | if (!HasShortOptionalBranch()) |
| 463 | return Succ; |
| 464 | } |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 465 | |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 466 | // Only consider successors which are either "hot", or wouldn't violate |
| 467 | // any CFG constraints. |
Cong Hou | 41cf1a5 | 2015-11-18 00:52:52 +0000 | [diff] [blame] | 468 | BlockChain &SuccChain = *BlockToChain[Succ]; |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 469 | if (SuccChain.UnscheduledPredecessors != 0) { |
Chandler Carruth | 18dfac3 | 2011-11-20 11:22:06 +0000 | [diff] [blame] | 470 | if (SuccProb < HotProb) { |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 471 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> " << SuccProb |
Chandler Carruth | 260258b | 2013-11-25 00:43:41 +0000 | [diff] [blame] | 472 | << " (prob) (CFG conflict)\n"); |
Chandler Carruth | 18dfac3 | 2011-11-20 11:22:06 +0000 | [diff] [blame] | 473 | continue; |
| 474 | } |
| 475 | |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 476 | // Make sure that a hot successor doesn't have a globally more |
| 477 | // important predecessor. |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 478 | auto RealSuccProb = MBPI->getEdgeProbability(BB, Succ); |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 479 | BlockFrequency CandidateEdgeFreq = |
Cong Hou | 41cf1a5 | 2015-11-18 00:52:52 +0000 | [diff] [blame] | 480 | MBFI->getBlockFreq(BB) * RealSuccProb * HotProb.getCompl(); |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 481 | bool BadCFGConflict = false; |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 482 | for (MachineBasicBlock *Pred : Succ->predecessors()) { |
Philip Reames | 23d9339 | 2016-03-03 00:01:42 +0000 | [diff] [blame] | 483 | if (Pred == Succ || BlockToChain[Pred] == &SuccChain || |
| 484 | (BlockFilter && !BlockFilter->count(Pred)) || |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 485 | BlockToChain[Pred] == &Chain) |
Chandler Carruth | e328814 | 2015-01-14 20:19:29 +0000 | [diff] [blame] | 486 | continue; |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 487 | BlockFrequency PredEdgeFreq = |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 488 | MBFI->getBlockFreq(Pred) * MBPI->getEdgeProbability(Pred, Succ); |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 489 | if (PredEdgeFreq >= CandidateEdgeFreq) { |
| 490 | BadCFGConflict = true; |
| 491 | break; |
Chandler Carruth | e328814 | 2015-01-14 20:19:29 +0000 | [diff] [blame] | 492 | } |
Chandler Carruth | 18dfac3 | 2011-11-20 11:22:06 +0000 | [diff] [blame] | 493 | } |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 494 | if (BadCFGConflict) { |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 495 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> " << SuccProb |
Daniel Jasper | 4d7b043 | 2015-02-18 08:18:07 +0000 | [diff] [blame] | 496 | << " (prob) (non-cold CFG conflict)\n"); |
| 497 | continue; |
| 498 | } |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 501 | DEBUG(dbgs() << " " << getBlockName(Succ) << " -> " << SuccProb |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 502 | << " (prob)" |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 503 | << (SuccChain.UnscheduledPredecessors != 0 ? " (CFG break)" : "") |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 504 | << "\n"); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 505 | if (BestSucc && BestProb >= SuccProb) |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 506 | continue; |
Daniel Jasper | ed9eb72 | 2015-02-18 08:19:16 +0000 | [diff] [blame] | 507 | BestSucc = Succ; |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 508 | BestProb = SuccProb; |
Chandler Carruth | b336172 | 2011-11-13 11:34:53 +0000 | [diff] [blame] | 509 | } |
| 510 | return BestSucc; |
| 511 | } |
| 512 | |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 513 | /// \brief Select the best block from a worklist. |
| 514 | /// |
| 515 | /// This looks through the provided worklist as a list of candidate basic |
| 516 | /// blocks and select the most profitable one to place. The definition of |
| 517 | /// profitable only really makes sense in the context of a loop. This returns |
| 518 | /// the most frequently visited block in the worklist, which in the case of |
| 519 | /// a loop, is the one most desirable to be physically close to the rest of the |
| 520 | /// loop body in order to improve icache behavior. |
| 521 | /// |
| 522 | /// \returns The best block found, or null if none are viable. |
| 523 | MachineBasicBlock *MachineBlockPlacement::selectBestCandidateBlock( |
Amaury Sechet | 9ee4ddd | 2016-04-07 06:34:47 +0000 | [diff] [blame^] | 524 | BlockChain &Chain, SmallVectorImpl<MachineBasicBlock *> &WorkList) { |
Chandler Carruth | 0af6a0b | 2011-11-14 09:46:33 +0000 | [diff] [blame] | 525 | // Once we need to walk the worklist looking for a candidate, cleanup the |
| 526 | // worklist of already placed entries. |
| 527 | // FIXME: If this shows up on profiles, it could be folded (at the cost of |
| 528 | // some code complexity) into the loop below. |
| 529 | WorkList.erase(std::remove_if(WorkList.begin(), WorkList.end(), |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 530 | [&](MachineBasicBlock *BB) { |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 531 | return BlockToChain.lookup(BB) == &Chain; |
| 532 | }), |
Chandler Carruth | 0af6a0b | 2011-11-14 09:46:33 +0000 | [diff] [blame] | 533 | WorkList.end()); |
| 534 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 535 | MachineBasicBlock *BestBlock = nullptr; |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 536 | BlockFrequency BestFreq; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 537 | for (MachineBasicBlock *MBB : WorkList) { |
| 538 | BlockChain &SuccChain = *BlockToChain[MBB]; |
Philip Reames | 02e1132 | 2016-03-02 22:40:51 +0000 | [diff] [blame] | 539 | if (&SuccChain == &Chain) |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 540 | continue; |
Junmo Park | 4ba6cf6 | 2016-03-11 05:07:07 +0000 | [diff] [blame] | 541 | |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 542 | assert(SuccChain.UnscheduledPredecessors == 0 && "Found CFG-violating block"); |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 543 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 544 | BlockFrequency CandidateFreq = MBFI->getBlockFreq(MBB); |
| 545 | DEBUG(dbgs() << " " << getBlockName(MBB) << " -> "; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 546 | MBFI->printBlockFreq(dbgs(), CandidateFreq) << " (freq)\n"); |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 547 | if (BestBlock && BestFreq >= CandidateFreq) |
| 548 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 549 | BestBlock = MBB; |
Chandler Carruth | f9213fe | 2011-11-13 11:42:26 +0000 | [diff] [blame] | 550 | BestFreq = CandidateFreq; |
| 551 | } |
| 552 | return BestBlock; |
| 553 | } |
| 554 | |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 555 | /// \brief Retrieve the first unplaced basic block. |
| 556 | /// |
| 557 | /// This routine is called when we are unable to use the CFG to walk through |
| 558 | /// 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] | 559 | /// We walk through the function's blocks in order, starting from the |
| 560 | /// LastUnplacedBlockIt. We update this iterator on each call to avoid |
| 561 | /// re-scanning the entire sequence on repeated calls to this routine. |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 562 | MachineBasicBlock *MachineBlockPlacement::getFirstUnplacedBlock( |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 563 | MachineFunction &F, const BlockChain &PlacedChain, |
| 564 | MachineFunction::iterator &PrevUnplacedBlockIt, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 565 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 566 | for (MachineFunction::iterator I = PrevUnplacedBlockIt, E = F.end(); I != E; |
| 567 | ++I) { |
Duncan P. N. Exon Smith | 6ac07fd | 2015-10-09 19:36:12 +0000 | [diff] [blame] | 568 | if (BlockFilter && !BlockFilter->count(&*I)) |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 569 | continue; |
Duncan P. N. Exon Smith | 6ac07fd | 2015-10-09 19:36:12 +0000 | [diff] [blame] | 570 | if (BlockToChain[&*I] != &PlacedChain) { |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 571 | PrevUnplacedBlockIt = I; |
Chandler Carruth | 4a87aa0 | 2011-11-23 03:03:21 +0000 | [diff] [blame] | 572 | // Now select the head of the chain to which the unplaced block belongs |
| 573 | // as the block to place. This will force the entire chain to be placed, |
| 574 | // and satisfies the requirements of merging chains. |
Duncan P. N. Exon Smith | 6ac07fd | 2015-10-09 19:36:12 +0000 | [diff] [blame] | 575 | return *BlockToChain[&*I]->begin(); |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 578 | return nullptr; |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Amaury Sechet | eae09c2 | 2016-03-14 21:24:11 +0000 | [diff] [blame] | 581 | void MachineBlockPlacement::fillWorkLists( |
| 582 | MachineBasicBlock *MBB, |
| 583 | SmallPtrSetImpl<BlockChain *> &UpdatedPreds, |
| 584 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
| 585 | const BlockFilterSet *BlockFilter = nullptr) { |
| 586 | BlockChain &Chain = *BlockToChain[MBB]; |
| 587 | if (!UpdatedPreds.insert(&Chain).second) |
| 588 | return; |
| 589 | |
| 590 | assert(Chain.UnscheduledPredecessors == 0); |
| 591 | for (MachineBasicBlock *ChainBB : Chain) { |
| 592 | assert(BlockToChain[ChainBB] == &Chain); |
| 593 | for (MachineBasicBlock *Pred : ChainBB->predecessors()) { |
| 594 | if (BlockFilter && !BlockFilter->count(Pred)) |
| 595 | continue; |
| 596 | if (BlockToChain[Pred] == &Chain) |
| 597 | continue; |
| 598 | ++Chain.UnscheduledPredecessors; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (Chain.UnscheduledPredecessors == 0) |
| 603 | BlockWorkList.push_back(*Chain.begin()); |
| 604 | } |
| 605 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 606 | void MachineBlockPlacement::buildChain( |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 607 | MachineBasicBlock *BB, BlockChain &Chain, |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 608 | SmallVectorImpl<MachineBasicBlock *> &BlockWorkList, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 609 | const BlockFilterSet *BlockFilter) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 610 | assert(BB); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 611 | assert(BlockToChain[BB] == &Chain); |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 612 | MachineFunction &F = *BB->getParent(); |
| 613 | MachineFunction::iterator PrevUnplacedBlockIt = F.begin(); |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 614 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 615 | MachineBasicBlock *LoopHeaderBB = BB; |
| 616 | markChainSuccessors(Chain, LoopHeaderBB, BlockWorkList, BlockFilter); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 617 | BB = *std::prev(Chain.end()); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 618 | for (;;) { |
| 619 | assert(BB); |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 620 | assert(BlockToChain[BB] == &Chain); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 621 | assert(*std::prev(Chain.end()) == BB); |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 622 | |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 623 | // Look for the best viable successor if there is one to place immediately |
| 624 | // after this block. |
Duncan Sands | 291d47e | 2012-09-14 09:00:11 +0000 | [diff] [blame] | 625 | MachineBasicBlock *BestSucc = selectBestSuccessor(BB, Chain, BlockFilter); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 626 | |
| 627 | // If an immediate successor isn't available, look for the best viable |
| 628 | // block among those we've identified as not violating the loop's CFG at |
| 629 | // 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] | 630 | if (!BestSucc) |
Amaury Sechet | 9ee4ddd | 2016-04-07 06:34:47 +0000 | [diff] [blame^] | 631 | BestSucc = selectBestCandidateBlock(Chain, BlockWorkList); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 632 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 633 | if (!BestSucc) { |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 634 | BestSucc = |
| 635 | getFirstUnplacedBlock(F, Chain, PrevUnplacedBlockIt, BlockFilter); |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 636 | if (!BestSucc) |
| 637 | break; |
| 638 | |
| 639 | DEBUG(dbgs() << "Unnatural loop CFG detected, forcibly merging the " |
| 640 | "layout successor until the CFG reduces\n"); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 641 | } |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 642 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 643 | // Place this block, updating the datastructures to reflect its placement. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 644 | BlockChain &SuccChain = *BlockToChain[BestSucc]; |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 645 | // Zero out UnscheduledPredecessors for the successor we're about to merge in case |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 646 | // we selected a successor that didn't fit naturally into the CFG. |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 647 | SuccChain.UnscheduledPredecessors = 0; |
Philip Reames | b9688f4 | 2016-03-02 21:45:13 +0000 | [diff] [blame] | 648 | DEBUG(dbgs() << "Merging from " << getBlockName(BB) << " to " |
| 649 | << getBlockName(BestSucc) << "\n"); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 650 | markChainSuccessors(SuccChain, LoopHeaderBB, BlockWorkList, BlockFilter); |
| 651 | Chain.merge(BestSucc, &SuccChain); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 652 | BB = *std::prev(Chain.end()); |
Jakub Staszak | 190c712 | 2011-12-07 19:46:10 +0000 | [diff] [blame] | 653 | } |
Chandler Carruth | 1071cfa | 2011-11-14 00:00:35 +0000 | [diff] [blame] | 654 | |
| 655 | DEBUG(dbgs() << "Finished forming chain for header block " |
Philip Reames | b9688f4 | 2016-03-02 21:45:13 +0000 | [diff] [blame] | 656 | << getBlockName(*Chain.begin()) << "\n"); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 659 | /// \brief Find the best loop top block for layout. |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 660 | /// |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 661 | /// Look for a block which is strictly better than the loop header for laying |
| 662 | /// out at the top of the loop. This looks for one and only one pattern: |
| 663 | /// a latch block with no conditional exit. This block will cause a conditional |
| 664 | /// jump around it or will be the bottom of the loop if we lay it out in place, |
| 665 | /// but if it it doesn't end up at the bottom of the loop for any reason, |
| 666 | /// rotation alone won't fix it. Because such a block will always result in an |
| 667 | /// unconditional jump (for the backedge) rotating it in front of the loop |
| 668 | /// header is always profitable. |
| 669 | MachineBasicBlock * |
| 670 | MachineBlockPlacement::findBestLoopTop(MachineLoop &L, |
| 671 | const BlockFilterSet &LoopBlockSet) { |
| 672 | // Check that the header hasn't been fused with a preheader block due to |
| 673 | // crazy branches. If it has, we need to start with the header at the top to |
| 674 | // prevent pulling the preheader into the loop body. |
| 675 | BlockChain &HeaderChain = *BlockToChain[L.getHeader()]; |
| 676 | if (!LoopBlockSet.count(*HeaderChain.begin())) |
| 677 | return L.getHeader(); |
| 678 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 679 | DEBUG(dbgs() << "Finding best loop top for: " << getBlockName(L.getHeader()) |
| 680 | << "\n"); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 681 | |
| 682 | BlockFrequency BestPredFreq; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 683 | MachineBasicBlock *BestPred = nullptr; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 684 | for (MachineBasicBlock *Pred : L.getHeader()->predecessors()) { |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 685 | if (!LoopBlockSet.count(Pred)) |
| 686 | continue; |
| 687 | DEBUG(dbgs() << " header pred: " << getBlockName(Pred) << ", " |
Michael Gottesman | b78dec8 | 2013-12-14 00:25:45 +0000 | [diff] [blame] | 688 | << Pred->succ_size() << " successors, "; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 689 | MBFI->printBlockFreq(dbgs(), Pred) << " freq\n"); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 690 | if (Pred->succ_size() > 1) |
| 691 | continue; |
| 692 | |
| 693 | BlockFrequency PredFreq = MBFI->getBlockFreq(Pred); |
| 694 | if (!BestPred || PredFreq > BestPredFreq || |
| 695 | (!(PredFreq < BestPredFreq) && |
| 696 | Pred->isLayoutSuccessor(L.getHeader()))) { |
| 697 | BestPred = Pred; |
| 698 | BestPredFreq = PredFreq; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // If no direct predecessor is fine, just use the loop header. |
Philip Reames | b9688f4 | 2016-03-02 21:45:13 +0000 | [diff] [blame] | 703 | if (!BestPred) { |
| 704 | DEBUG(dbgs() << " final top unchanged\n"); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 705 | return L.getHeader(); |
Philip Reames | b9688f4 | 2016-03-02 21:45:13 +0000 | [diff] [blame] | 706 | } |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 707 | |
| 708 | // Walk backwards through any straight line of predecessors. |
| 709 | while (BestPred->pred_size() == 1 && |
| 710 | (*BestPred->pred_begin())->succ_size() == 1 && |
| 711 | *BestPred->pred_begin() != L.getHeader()) |
| 712 | BestPred = *BestPred->pred_begin(); |
| 713 | |
| 714 | DEBUG(dbgs() << " final top: " << getBlockName(BestPred) << "\n"); |
| 715 | return BestPred; |
| 716 | } |
| 717 | |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 718 | /// \brief Find the best loop exiting block for layout. |
| 719 | /// |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 720 | /// This routine implements the logic to analyze the loop looking for the best |
| 721 | /// block to layout at the top of the loop. Typically this is done to maximize |
| 722 | /// fallthrough opportunities. |
| 723 | MachineBasicBlock * |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 724 | MachineBlockPlacement::findBestLoopExit(MachineFunction &F, MachineLoop &L, |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 725 | const BlockFilterSet &LoopBlockSet) { |
Chandler Carruth | 6806261 | 2012-04-10 13:35:57 +0000 | [diff] [blame] | 726 | // We don't want to layout the loop linearly in all cases. If the loop header |
| 727 | // is just a normal basic block in the loop, we want to look for what block |
| 728 | // within the loop is the best one to layout at the top. However, if the loop |
| 729 | // header has be pre-merged into a chain due to predecessors not having |
| 730 | // analyzable branches, *and* the predecessor it is merged with is *not* part |
| 731 | // of the loop, rotating the header into the middle of the loop will create |
| 732 | // a non-contiguous range of blocks which is Very Bad. So start with the |
| 733 | // header and only rotate if safe. |
| 734 | BlockChain &HeaderChain = *BlockToChain[L.getHeader()]; |
| 735 | if (!LoopBlockSet.count(*HeaderChain.begin())) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 736 | return nullptr; |
Chandler Carruth | 6806261 | 2012-04-10 13:35:57 +0000 | [diff] [blame] | 737 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 738 | BlockFrequency BestExitEdgeFreq; |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 739 | unsigned BestExitLoopDepth = 0; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 740 | MachineBasicBlock *ExitingBB = nullptr; |
Chandler Carruth | 4f56720 | 2011-11-27 20:18:00 +0000 | [diff] [blame] | 741 | // If there are exits to outer loops, loop rotation can severely limit |
| 742 | // fallthrough opportunites unless it selects such an exit. Keep a set of |
| 743 | // blocks where rotating to exit with that block will reach an outer loop. |
| 744 | SmallPtrSet<MachineBasicBlock *, 4> BlocksExitingToOuterLoop; |
| 745 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 746 | DEBUG(dbgs() << "Finding best loop exit for: " << getBlockName(L.getHeader()) |
| 747 | << "\n"); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 748 | for (MachineBasicBlock *MBB : L.getBlocks()) { |
| 749 | BlockChain &Chain = *BlockToChain[MBB]; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 750 | // Ensure that this block is at the end of a chain; otherwise it could be |
Chandler Carruth | 9a512a4 | 2015-04-15 13:19:54 +0000 | [diff] [blame] | 751 | // mid-way through an inner loop or a successor of an unanalyzable branch. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 752 | if (MBB != *std::prev(Chain.end())) |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 753 | continue; |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 754 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 755 | // Now walk the successors. We need to establish whether this has a viable |
| 756 | // exiting successor and whether it has a viable non-exiting successor. |
| 757 | // We store the old exiting state and restore it if a viable looping |
| 758 | // successor isn't found. |
| 759 | MachineBasicBlock *OldExitingBB = ExitingBB; |
| 760 | BlockFrequency OldBestExitEdgeFreq = BestExitEdgeFreq; |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 761 | bool HasLoopingSucc = false; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 762 | for (MachineBasicBlock *Succ : MBB->successors()) { |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 763 | if (Succ->isEHPad()) |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 764 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 765 | if (Succ == MBB) |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 766 | continue; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 767 | BlockChain &SuccChain = *BlockToChain[Succ]; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 768 | // Don't split chains, either this chain or the successor's chain. |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 769 | if (&Chain == &SuccChain) { |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 770 | DEBUG(dbgs() << " exiting: " << getBlockName(MBB) << " -> " |
| 771 | << getBlockName(Succ) << " (chain conflict)\n"); |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 772 | continue; |
| 773 | } |
| 774 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 775 | auto SuccProb = MBPI->getEdgeProbability(MBB, Succ); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 776 | if (LoopBlockSet.count(Succ)) { |
| 777 | DEBUG(dbgs() << " looping: " << getBlockName(MBB) << " -> " |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 778 | << getBlockName(Succ) << " (" << SuccProb << ")\n"); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 779 | HasLoopingSucc = true; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 780 | continue; |
| 781 | } |
| 782 | |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 783 | unsigned SuccLoopDepth = 0; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 784 | if (MachineLoop *ExitLoop = MLI->getLoopFor(Succ)) { |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 785 | SuccLoopDepth = ExitLoop->getLoopDepth(); |
| 786 | if (ExitLoop->contains(&L)) |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 787 | BlocksExitingToOuterLoop.insert(MBB); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 790 | BlockFrequency ExitEdgeFreq = MBFI->getBlockFreq(MBB) * SuccProb; |
| 791 | DEBUG(dbgs() << " exiting: " << getBlockName(MBB) << " -> " |
| 792 | << getBlockName(Succ) << " [L:" << SuccLoopDepth << "] ("; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 793 | MBFI->printBlockFreq(dbgs(), ExitEdgeFreq) << ")\n"); |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 794 | // Note that we bias this toward an existing layout successor to retain |
| 795 | // incoming order in the absence of better information. The exit must have |
| 796 | // a frequency higher than the current exit before we consider breaking |
| 797 | // the layout. |
| 798 | BranchProbability Bias(100 - ExitBlockBias, 100); |
Chandler Carruth | 26d3017 | 2015-04-15 13:39:42 +0000 | [diff] [blame] | 799 | if (!ExitingBB || SuccLoopDepth > BestExitLoopDepth || |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 800 | ExitEdgeFreq > BestExitEdgeFreq || |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 801 | (MBB->isLayoutSuccessor(Succ) && |
Benjamin Kramer | c8160d6 | 2013-11-20 19:08:44 +0000 | [diff] [blame] | 802 | !(ExitEdgeFreq < BestExitEdgeFreq * Bias))) { |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 803 | BestExitEdgeFreq = ExitEdgeFreq; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 804 | ExitingBB = MBB; |
Chandler Carruth | a054580 | 2011-11-27 09:22:53 +0000 | [diff] [blame] | 805 | } |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 806 | } |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 807 | |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 808 | if (!HasLoopingSucc) { |
Chandler Carruth | cfb2b9d | 2015-04-15 13:26:41 +0000 | [diff] [blame] | 809 | // Restore the old exiting state, no viable looping successor was found. |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 810 | ExitingBB = OldExitingBB; |
| 811 | BestExitEdgeFreq = OldBestExitEdgeFreq; |
Amaury Sechet | 41474a5 | 2016-04-07 04:28:40 +0000 | [diff] [blame] | 812 | continue; |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 813 | } |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 814 | } |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 815 | // 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] | 816 | // loop, just use the loop header to layout the loop. |
| 817 | if (!ExitingBB || L.getNumBlocks() == 1) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 818 | return nullptr; |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 819 | |
Chandler Carruth | 4f56720 | 2011-11-27 20:18:00 +0000 | [diff] [blame] | 820 | // Also, if we have exit blocks which lead to outer loops but didn't select |
| 821 | // one of them as the exiting block we are rotating toward, disable loop |
| 822 | // rotation altogether. |
| 823 | if (!BlocksExitingToOuterLoop.empty() && |
| 824 | !BlocksExitingToOuterLoop.count(ExitingBB)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 825 | return nullptr; |
Chandler Carruth | 4f56720 | 2011-11-27 20:18:00 +0000 | [diff] [blame] | 826 | |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 827 | DEBUG(dbgs() << " Best exiting block: " << getBlockName(ExitingBB) << "\n"); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 828 | return ExitingBB; |
Chandler Carruth | 9ffb97e | 2011-11-27 00:38:03 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 831 | /// \brief Attempt to rotate an exiting block to the bottom of the loop. |
| 832 | /// |
| 833 | /// Once we have built a chain, try to rotate it to line up the hot exit block |
| 834 | /// with fallthrough out of the loop if doing so doesn't introduce unnecessary |
| 835 | /// branches. For example, if the loop has fallthrough into its header and out |
| 836 | /// of its bottom already, don't rotate it. |
| 837 | void MachineBlockPlacement::rotateLoop(BlockChain &LoopChain, |
| 838 | MachineBasicBlock *ExitingBB, |
| 839 | const BlockFilterSet &LoopBlockSet) { |
| 840 | if (!ExitingBB) |
| 841 | return; |
| 842 | |
| 843 | MachineBasicBlock *Top = *LoopChain.begin(); |
| 844 | bool ViableTopFallthrough = false; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 845 | for (MachineBasicBlock *Pred : Top->predecessors()) { |
| 846 | BlockChain *PredChain = BlockToChain[Pred]; |
| 847 | if (!LoopBlockSet.count(Pred) && |
| 848 | (!PredChain || Pred == *std::prev(PredChain->end()))) { |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 849 | ViableTopFallthrough = true; |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | // If the header has viable fallthrough, check whether the current loop |
| 855 | // bottom is a viable exiting block. If so, bail out as rotating will |
| 856 | // introduce an unnecessary branch. |
| 857 | if (ViableTopFallthrough) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 858 | MachineBasicBlock *Bottom = *std::prev(LoopChain.end()); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 859 | for (MachineBasicBlock *Succ : Bottom->successors()) { |
| 860 | BlockChain *SuccChain = BlockToChain[Succ]; |
| 861 | if (!LoopBlockSet.count(Succ) && |
| 862 | (!SuccChain || Succ == *SuccChain->begin())) |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 863 | return; |
| 864 | } |
| 865 | } |
| 866 | |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 867 | BlockChain::iterator ExitIt = |
| 868 | std::find(LoopChain.begin(), LoopChain.end(), ExitingBB); |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 869 | if (ExitIt == LoopChain.end()) |
| 870 | return; |
| 871 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 872 | std::rotate(LoopChain.begin(), std::next(ExitIt), LoopChain.end()); |
Chandler Carruth | 8c74c7b | 2012-04-16 09:31:23 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 875 | /// \brief Attempt to rotate a loop based on profile data to reduce branch cost. |
| 876 | /// |
| 877 | /// With profile data, we can determine the cost in terms of missed fall through |
| 878 | /// opportunities when rotating a loop chain and select the best rotation. |
| 879 | /// Basically, there are three kinds of cost to consider for each rotation: |
| 880 | /// 1. The possibly missed fall through edge (if it exists) from BB out of |
| 881 | /// the loop to the loop header. |
| 882 | /// 2. The possibly missed fall through edges (if they exist) from the loop |
| 883 | /// exits to BB out of the loop. |
| 884 | /// 3. The missed fall through edge (if it exists) from the last BB to the |
| 885 | /// first BB in the loop chain. |
| 886 | /// Therefore, the cost for a given rotation is the sum of costs listed above. |
| 887 | /// We select the best rotation with the smallest cost. |
| 888 | void MachineBlockPlacement::rotateLoopWithProfile( |
| 889 | BlockChain &LoopChain, MachineLoop &L, const BlockFilterSet &LoopBlockSet) { |
| 890 | auto HeaderBB = L.getHeader(); |
| 891 | auto HeaderIter = std::find(LoopChain.begin(), LoopChain.end(), HeaderBB); |
| 892 | auto RotationPos = LoopChain.end(); |
| 893 | |
| 894 | BlockFrequency SmallestRotationCost = BlockFrequency::getMaxFrequency(); |
| 895 | |
| 896 | // A utility lambda that scales up a block frequency by dividing it by a |
| 897 | // branch probability which is the reciprocal of the scale. |
| 898 | auto ScaleBlockFrequency = [](BlockFrequency Freq, |
| 899 | unsigned Scale) -> BlockFrequency { |
| 900 | if (Scale == 0) |
| 901 | return 0; |
| 902 | // Use operator / between BlockFrequency and BranchProbability to implement |
| 903 | // saturating multiplication. |
| 904 | return Freq / BranchProbability(1, Scale); |
| 905 | }; |
| 906 | |
| 907 | // Compute the cost of the missed fall-through edge to the loop header if the |
| 908 | // chain head is not the loop header. As we only consider natural loops with |
| 909 | // single header, this computation can be done only once. |
| 910 | BlockFrequency HeaderFallThroughCost(0); |
| 911 | for (auto *Pred : HeaderBB->predecessors()) { |
| 912 | BlockChain *PredChain = BlockToChain[Pred]; |
| 913 | if (!LoopBlockSet.count(Pred) && |
| 914 | (!PredChain || Pred == *std::prev(PredChain->end()))) { |
| 915 | auto EdgeFreq = |
| 916 | MBFI->getBlockFreq(Pred) * MBPI->getEdgeProbability(Pred, HeaderBB); |
| 917 | auto FallThruCost = ScaleBlockFrequency(EdgeFreq, MisfetchCost); |
| 918 | // If the predecessor has only an unconditional jump to the header, we |
| 919 | // need to consider the cost of this jump. |
| 920 | if (Pred->succ_size() == 1) |
| 921 | FallThruCost += ScaleBlockFrequency(EdgeFreq, JumpInstCost); |
| 922 | HeaderFallThroughCost = std::max(HeaderFallThroughCost, FallThruCost); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | // Here we collect all exit blocks in the loop, and for each exit we find out |
| 927 | // its hottest exit edge. For each loop rotation, we define the loop exit cost |
| 928 | // as the sum of frequencies of exit edges we collect here, excluding the exit |
| 929 | // edge from the tail of the loop chain. |
| 930 | SmallVector<std::pair<MachineBasicBlock *, BlockFrequency>, 4> ExitsWithFreq; |
| 931 | for (auto BB : LoopChain) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 932 | auto LargestExitEdgeProb = BranchProbability::getZero(); |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 933 | for (auto *Succ : BB->successors()) { |
| 934 | BlockChain *SuccChain = BlockToChain[Succ]; |
| 935 | if (!LoopBlockSet.count(Succ) && |
| 936 | (!SuccChain || Succ == *SuccChain->begin())) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 937 | auto SuccProb = MBPI->getEdgeProbability(BB, Succ); |
| 938 | LargestExitEdgeProb = std::max(LargestExitEdgeProb, SuccProb); |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 941 | if (LargestExitEdgeProb > BranchProbability::getZero()) { |
| 942 | auto ExitFreq = MBFI->getBlockFreq(BB) * LargestExitEdgeProb; |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 943 | ExitsWithFreq.emplace_back(BB, ExitFreq); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | // In this loop we iterate every block in the loop chain and calculate the |
| 948 | // cost assuming the block is the head of the loop chain. When the loop ends, |
| 949 | // we should have found the best candidate as the loop chain's head. |
| 950 | for (auto Iter = LoopChain.begin(), TailIter = std::prev(LoopChain.end()), |
| 951 | EndIter = LoopChain.end(); |
| 952 | Iter != EndIter; Iter++, TailIter++) { |
| 953 | // TailIter is used to track the tail of the loop chain if the block we are |
| 954 | // checking (pointed by Iter) is the head of the chain. |
| 955 | if (TailIter == LoopChain.end()) |
| 956 | TailIter = LoopChain.begin(); |
| 957 | |
| 958 | auto TailBB = *TailIter; |
| 959 | |
| 960 | // Calculate the cost by putting this BB to the top. |
| 961 | BlockFrequency Cost = 0; |
| 962 | |
| 963 | // If the current BB is the loop header, we need to take into account the |
| 964 | // cost of the missed fall through edge from outside of the loop to the |
| 965 | // header. |
| 966 | if (Iter != HeaderIter) |
| 967 | Cost += HeaderFallThroughCost; |
| 968 | |
| 969 | // Collect the loop exit cost by summing up frequencies of all exit edges |
| 970 | // except the one from the chain tail. |
| 971 | for (auto &ExitWithFreq : ExitsWithFreq) |
| 972 | if (TailBB != ExitWithFreq.first) |
| 973 | Cost += ExitWithFreq.second; |
| 974 | |
| 975 | // The cost of breaking the once fall-through edge from the tail to the top |
| 976 | // of the loop chain. Here we need to consider three cases: |
| 977 | // 1. If the tail node has only one successor, then we will get an |
| 978 | // additional jmp instruction. So the cost here is (MisfetchCost + |
| 979 | // JumpInstCost) * tail node frequency. |
| 980 | // 2. If the tail node has two successors, then we may still get an |
| 981 | // additional jmp instruction if the layout successor after the loop |
| 982 | // chain is not its CFG successor. Note that the more frequently executed |
| 983 | // jmp instruction will be put ahead of the other one. Assume the |
| 984 | // frequency of those two branches are x and y, where x is the frequency |
| 985 | // of the edge to the chain head, then the cost will be |
| 986 | // (x * MisfetechCost + min(x, y) * JumpInstCost) * tail node frequency. |
| 987 | // 3. If the tail node has more than two successors (this rarely happens), |
| 988 | // we won't consider any additional cost. |
| 989 | if (TailBB->isSuccessor(*Iter)) { |
| 990 | auto TailBBFreq = MBFI->getBlockFreq(TailBB); |
| 991 | if (TailBB->succ_size() == 1) |
| 992 | Cost += ScaleBlockFrequency(TailBBFreq.getFrequency(), |
| 993 | MisfetchCost + JumpInstCost); |
| 994 | else if (TailBB->succ_size() == 2) { |
| 995 | auto TailToHeadProb = MBPI->getEdgeProbability(TailBB, *Iter); |
| 996 | auto TailToHeadFreq = TailBBFreq * TailToHeadProb; |
| 997 | auto ColderEdgeFreq = TailToHeadProb > BranchProbability(1, 2) |
| 998 | ? TailBBFreq * TailToHeadProb.getCompl() |
| 999 | : TailToHeadFreq; |
| 1000 | Cost += ScaleBlockFrequency(TailToHeadFreq, MisfetchCost) + |
| 1001 | ScaleBlockFrequency(ColderEdgeFreq, JumpInstCost); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Philip Reames | b9688f4 | 2016-03-02 21:45:13 +0000 | [diff] [blame] | 1005 | DEBUG(dbgs() << "The cost of loop rotation by making " << getBlockName(*Iter) |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 1006 | << " to the top: " << Cost.getFrequency() << "\n"); |
| 1007 | |
| 1008 | if (Cost < SmallestRotationCost) { |
| 1009 | SmallestRotationCost = Cost; |
| 1010 | RotationPos = Iter; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | if (RotationPos != LoopChain.end()) { |
Philip Reames | b9688f4 | 2016-03-02 21:45:13 +0000 | [diff] [blame] | 1015 | DEBUG(dbgs() << "Rotate loop by making " << getBlockName(*RotationPos) |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 1016 | << " to the top\n"); |
| 1017 | std::rotate(LoopChain.begin(), RotationPos, LoopChain.end()); |
| 1018 | } |
| 1019 | } |
| 1020 | |
Cong Hou | b90b9e0 | 2015-11-02 21:24:00 +0000 | [diff] [blame] | 1021 | /// \brief Collect blocks in the given loop that are to be placed. |
| 1022 | /// |
| 1023 | /// When profile data is available, exclude cold blocks from the returned set; |
| 1024 | /// otherwise, collect all blocks in the loop. |
| 1025 | MachineBlockPlacement::BlockFilterSet |
| 1026 | MachineBlockPlacement::collectLoopBlockSet(MachineFunction &F, MachineLoop &L) { |
| 1027 | BlockFilterSet LoopBlockSet; |
| 1028 | |
| 1029 | // Filter cold blocks off from LoopBlockSet when profile data is available. |
| 1030 | // Collect the sum of frequencies of incoming edges to the loop header from |
| 1031 | // outside. If we treat the loop as a super block, this is the frequency of |
| 1032 | // the loop. Then for each block in the loop, we calculate the ratio between |
| 1033 | // its frequency and the frequency of the loop block. When it is too small, |
| 1034 | // don't add it to the loop chain. If there are outer loops, then this block |
| 1035 | // will be merged into the first outer loop chain for which this block is not |
| 1036 | // cold anymore. This needs precise profile data and we only do this when |
| 1037 | // profile data is available. |
| 1038 | if (F.getFunction()->getEntryCount()) { |
| 1039 | BlockFrequency LoopFreq(0); |
| 1040 | for (auto LoopPred : L.getHeader()->predecessors()) |
| 1041 | if (!L.contains(LoopPred)) |
| 1042 | LoopFreq += MBFI->getBlockFreq(LoopPred) * |
| 1043 | MBPI->getEdgeProbability(LoopPred, L.getHeader()); |
| 1044 | |
| 1045 | for (MachineBasicBlock *LoopBB : L.getBlocks()) { |
| 1046 | auto Freq = MBFI->getBlockFreq(LoopBB).getFrequency(); |
| 1047 | if (Freq == 0 || LoopFreq.getFrequency() / Freq > LoopToColdBlockRatio) |
| 1048 | continue; |
| 1049 | LoopBlockSet.insert(LoopBB); |
| 1050 | } |
| 1051 | } else |
| 1052 | LoopBlockSet.insert(L.block_begin(), L.block_end()); |
| 1053 | |
| 1054 | return LoopBlockSet; |
| 1055 | } |
| 1056 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1057 | /// \brief Forms basic block chains from the natural loop structures. |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1058 | /// |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1059 | /// These chains are designed to preserve the existing *structure* of the code |
| 1060 | /// as much as possible. We can then stitch the chains together in a way which |
| 1061 | /// both preserves the topological structure and minimizes taken conditional |
| 1062 | /// branches. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1063 | void MachineBlockPlacement::buildLoopChains(MachineFunction &F, |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 1064 | MachineLoop &L) { |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1065 | // First recurse through any nested loops, building chains for those inner |
| 1066 | // loops. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1067 | for (MachineLoop *InnerLoop : L) |
| 1068 | buildLoopChains(F, *InnerLoop); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1069 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1070 | SmallVector<MachineBasicBlock *, 16> BlockWorkList; |
Cong Hou | b90b9e0 | 2015-11-02 21:24:00 +0000 | [diff] [blame] | 1071 | BlockFilterSet LoopBlockSet = collectLoopBlockSet(F, L); |
Chandler Carruth | 03adbd4 | 2011-11-27 13:34:33 +0000 | [diff] [blame] | 1072 | |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 1073 | // Check if we have profile data for this function. If yes, we will rotate |
| 1074 | // this loop by modeling costs more precisely which requires the profile data |
| 1075 | // for better layout. |
| 1076 | bool RotateLoopWithProfile = |
| 1077 | PreciseRotationCost && F.getFunction()->getEntryCount(); |
| 1078 | |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 1079 | // First check to see if there is an obviously preferable top block for the |
| 1080 | // loop. This will default to the header, but may end up as one of the |
| 1081 | // predecessors to the header if there is one which will result in strictly |
| 1082 | // fewer branches in the loop body. |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 1083 | // When we use profile data to rotate the loop, this is unnecessary. |
| 1084 | MachineBasicBlock *LoopTop = |
| 1085 | RotateLoopWithProfile ? L.getHeader() : findBestLoopTop(L, LoopBlockSet); |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 1086 | |
| 1087 | // If we selected just the header for the loop top, look for a potentially |
| 1088 | // profitable exit block in the event that rotating the loop can eliminate |
| 1089 | // branches by placing an exit edge at the bottom. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1090 | MachineBasicBlock *ExitingBB = nullptr; |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 1091 | if (!RotateLoopWithProfile && LoopTop == L.getHeader()) |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 1092 | ExitingBB = findBestLoopExit(F, L, LoopBlockSet); |
| 1093 | |
| 1094 | BlockChain &LoopChain = *BlockToChain[LoopTop]; |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1095 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1096 | // FIXME: This is a really lame way of walking the chains in the loop: we |
| 1097 | // walk the blocks, and use a set to prevent visiting a particular chain |
| 1098 | // twice. |
Jakub Staszak | 9061616 | 2011-12-21 23:02:08 +0000 | [diff] [blame] | 1099 | SmallPtrSet<BlockChain *, 4> UpdatedPreds; |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 1100 | assert(LoopChain.UnscheduledPredecessors == 0); |
Jakub Staszak | 190c712 | 2011-12-07 19:46:10 +0000 | [diff] [blame] | 1101 | UpdatedPreds.insert(&LoopChain); |
Cong Hou | b90b9e0 | 2015-11-02 21:24:00 +0000 | [diff] [blame] | 1102 | |
Amaury Sechet | eae09c2 | 2016-03-14 21:24:11 +0000 | [diff] [blame] | 1103 | for (MachineBasicBlock *LoopBB : LoopBlockSet) |
| 1104 | fillWorkLists(LoopBB, UpdatedPreds, BlockWorkList, &LoopBlockSet); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1105 | |
Chandler Carruth | 8c0b41d | 2012-04-16 13:33:36 +0000 | [diff] [blame] | 1106 | buildChain(LoopTop, LoopChain, BlockWorkList, &LoopBlockSet); |
Cong Hou | 7745dbc | 2015-10-19 23:16:40 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (RotateLoopWithProfile) |
| 1109 | rotateLoopWithProfile(LoopChain, L, LoopBlockSet); |
| 1110 | else |
| 1111 | rotateLoop(LoopChain, ExitingBB, LoopBlockSet); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1112 | |
| 1113 | DEBUG({ |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1114 | // Crash at the end so we get all of the debugging output first. |
| 1115 | bool BadLoop = false; |
Philip Reames | ae27b23 | 2016-03-03 00:58:43 +0000 | [diff] [blame] | 1116 | if (LoopChain.UnscheduledPredecessors) { |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1117 | BadLoop = true; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1118 | dbgs() << "Loop chain contains a block without its preds placed!\n" |
| 1119 | << " Loop header: " << getBlockName(*L.block_begin()) << "\n" |
| 1120 | << " Chain header: " << getBlockName(*LoopChain.begin()) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1121 | } |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1122 | for (MachineBasicBlock *ChainBB : LoopChain) { |
| 1123 | dbgs() << " ... " << getBlockName(ChainBB) << "\n"; |
| 1124 | if (!LoopBlockSet.erase(ChainBB)) { |
Chandler Carruth | 0a31d14 | 2011-11-14 10:55:53 +0000 | [diff] [blame] | 1125 | // We don't mark the loop as bad here because there are real situations |
| 1126 | // where this can occur. For example, with an unanalyzable fallthrough |
Chandler Carruth | 99fe42f | 2011-11-23 10:35:36 +0000 | [diff] [blame] | 1127 | // from a loop block to a non-loop block or vice versa. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1128 | dbgs() << "Loop chain contains a block not contained by the loop!\n" |
| 1129 | << " Loop header: " << getBlockName(*L.block_begin()) << "\n" |
| 1130 | << " Chain header: " << getBlockName(*LoopChain.begin()) << "\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1131 | << " Bad block: " << getBlockName(ChainBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1132 | } |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 1133 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1134 | |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1135 | if (!LoopBlockSet.empty()) { |
| 1136 | BadLoop = true; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1137 | for (MachineBasicBlock *LoopBB : LoopBlockSet) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1138 | dbgs() << "Loop contains blocks never placed into a chain!\n" |
| 1139 | << " Loop header: " << getBlockName(*L.block_begin()) << "\n" |
| 1140 | << " Chain header: " << getBlockName(*LoopChain.begin()) << "\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1141 | << " Bad block: " << getBlockName(LoopBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1142 | } |
| 1143 | assert(!BadLoop && "Detected problems with the placement of this loop."); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1144 | }); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1147 | void MachineBlockPlacement::buildCFGChains(MachineFunction &F) { |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1148 | // Ensure that every BB in the function has an associated chain to simplify |
| 1149 | // the assumptions of the remaining algorithm. |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 1150 | SmallVector<MachineOperand, 4> Cond; // For AnalyzeBranch. |
| 1151 | for (MachineFunction::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) { |
Duncan P. N. Exon Smith | 6ac07fd | 2015-10-09 19:36:12 +0000 | [diff] [blame] | 1152 | MachineBasicBlock *BB = &*FI; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1153 | BlockChain *Chain = |
| 1154 | new (ChainAllocator.Allocate()) BlockChain(BlockToChain, BB); |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 1155 | // Also, merge any blocks which we cannot reason about and must preserve |
| 1156 | // the exact fallthrough behavior for. |
| 1157 | for (;;) { |
| 1158 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1159 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch. |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 1160 | if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond) || !FI->canFallThrough()) |
| 1161 | break; |
| 1162 | |
Duncan P. N. Exon Smith | 6ac07fd | 2015-10-09 19:36:12 +0000 | [diff] [blame] | 1163 | MachineFunction::iterator NextFI = std::next(FI); |
| 1164 | MachineBasicBlock *NextBB = &*NextFI; |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 1165 | // Ensure that the layout successor is a viable block, as we know that |
| 1166 | // fallthrough is a possibility. |
| 1167 | assert(NextFI != FE && "Can't fallthrough past the last block."); |
| 1168 | DEBUG(dbgs() << "Pre-merging due to unanalyzable fallthrough: " |
| 1169 | << getBlockName(BB) << " -> " << getBlockName(NextBB) |
| 1170 | << "\n"); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1171 | Chain->merge(NextBB, nullptr); |
Chandler Carruth | f3dc9ef | 2011-11-19 10:26:02 +0000 | [diff] [blame] | 1172 | FI = NextFI; |
| 1173 | BB = NextBB; |
| 1174 | } |
| 1175 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1176 | |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 1177 | if (OutlineOptionalBranches) { |
| 1178 | // Find the nearest common dominator of all of F's terminators. |
| 1179 | MachineBasicBlock *Terminator = nullptr; |
| 1180 | for (MachineBasicBlock &MBB : F) { |
| 1181 | if (MBB.succ_size() == 0) { |
| 1182 | if (Terminator == nullptr) |
| 1183 | Terminator = &MBB; |
| 1184 | else |
| 1185 | Terminator = MDT->findNearestCommonDominator(Terminator, &MBB); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | // MBBs dominating this common dominator are unavoidable. |
| 1190 | UnavoidableBlocks.clear(); |
| 1191 | for (MachineBasicBlock &MBB : F) { |
| 1192 | if (MDT->dominates(&MBB, Terminator)) { |
| 1193 | UnavoidableBlocks.insert(&MBB); |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1198 | // Build any loop-based chains. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1199 | for (MachineLoop *L : *MLI) |
| 1200 | buildLoopChains(F, *L); |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1201 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1202 | SmallVector<MachineBasicBlock *, 16> BlockWorkList; |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1203 | |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1204 | SmallPtrSet<BlockChain *, 4> UpdatedPreds; |
Amaury Sechet | eae09c2 | 2016-03-14 21:24:11 +0000 | [diff] [blame] | 1205 | for (MachineBasicBlock &MBB : F) |
| 1206 | fillWorkLists(&MBB, UpdatedPreds, BlockWorkList); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1207 | |
| 1208 | BlockChain &FunctionChain = *BlockToChain[&F.front()]; |
Chandler Carruth | 9b548a7f | 2011-11-15 06:26:43 +0000 | [diff] [blame] | 1209 | buildChain(&F.front(), FunctionChain, BlockWorkList); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1210 | |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 1211 | #ifndef NDEBUG |
Matt Arsenault | 79d55f5 | 2013-12-05 20:02:18 +0000 | [diff] [blame] | 1212 | typedef SmallPtrSet<MachineBasicBlock *, 16> FunctionBlockSetType; |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 1213 | #endif |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1214 | DEBUG({ |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1215 | // Crash at the end so we get all of the debugging output first. |
| 1216 | bool BadFunc = false; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1217 | FunctionBlockSetType FunctionBlockSet; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1218 | for (MachineBasicBlock &MBB : F) |
| 1219 | FunctionBlockSet.insert(&MBB); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1220 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1221 | for (MachineBasicBlock *ChainBB : FunctionChain) |
| 1222 | if (!FunctionBlockSet.erase(ChainBB)) { |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1223 | BadFunc = true; |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1224 | dbgs() << "Function chain contains a block not in the function!\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1225 | << " Bad block: " << getBlockName(ChainBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1226 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1227 | |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1228 | if (!FunctionBlockSet.empty()) { |
| 1229 | BadFunc = true; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1230 | for (MachineBasicBlock *RemainingBB : FunctionBlockSet) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1231 | dbgs() << "Function contains blocks never placed into a chain!\n" |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1232 | << " Bad block: " << getBlockName(RemainingBB) << "\n"; |
Chandler Carruth | 8e1d906 | 2011-11-13 21:39:51 +0000 | [diff] [blame] | 1233 | } |
| 1234 | assert(!BadFunc && "Detected problems with the block placement."); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1235 | }); |
| 1236 | |
| 1237 | // Splice the blocks into place. |
| 1238 | MachineFunction::iterator InsertPos = F.begin(); |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1239 | for (MachineBasicBlock *ChainBB : FunctionChain) { |
| 1240 | DEBUG(dbgs() << (ChainBB == *FunctionChain.begin() ? "Placing chain " |
| 1241 | : " ... ") |
| 1242 | << getBlockName(ChainBB) << "\n"); |
| 1243 | if (InsertPos != MachineFunction::iterator(ChainBB)) |
| 1244 | F.splice(InsertPos, ChainBB); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1245 | else |
| 1246 | ++InsertPos; |
| 1247 | |
| 1248 | // Update the terminator of the previous block. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1249 | if (ChainBB == *FunctionChain.begin()) |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1250 | continue; |
Duncan P. N. Exon Smith | 6ac07fd | 2015-10-09 19:36:12 +0000 | [diff] [blame] | 1251 | MachineBasicBlock *PrevBB = &*std::prev(MachineFunction::iterator(ChainBB)); |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1252 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1253 | // FIXME: It would be awesome of updateTerminator would just return rather |
| 1254 | // than assert when the branch cannot be analyzed in order to remove this |
| 1255 | // boiler plate. |
| 1256 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1257 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch. |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1258 | if (!TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) { |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1259 | // The "PrevBB" is not yet updated to reflect current code layout, so, |
| 1260 | // o. it may fall-through to a block without explict "goto" instruction |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1261 | // before layout, and no longer fall-through it after layout; or |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1262 | // o. just opposite. |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1263 | // |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1264 | // AnalyzeBranch() may return erroneous value for FBB when these two |
| 1265 | // situations take place. For the first scenario FBB is mistakenly set |
| 1266 | // NULL; for the 2nd scenario, the FBB, which is expected to be NULL, |
| 1267 | // is mistakenly pointing to "*BI". |
| 1268 | // |
| 1269 | bool needUpdateBr = true; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1270 | if (!Cond.empty() && (!FBB || FBB == ChainBB)) { |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1271 | PrevBB->updateTerminator(); |
| 1272 | needUpdateBr = false; |
| 1273 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1274 | TBB = FBB = nullptr; |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1275 | if (TII->AnalyzeBranch(*PrevBB, TBB, FBB, Cond)) { |
| 1276 | // FIXME: This should never take place. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1277 | TBB = FBB = nullptr; |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1281 | // If PrevBB has a two-way branch, try to re-order the branches |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1282 | // such that we branch to the successor with higher probability first. |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1283 | if (TBB && !Cond.empty() && FBB && |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1284 | MBPI->getEdgeProbability(PrevBB, FBB) > |
| 1285 | MBPI->getEdgeProbability(PrevBB, TBB) && |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1286 | !TII->ReverseBranchCondition(Cond)) { |
| 1287 | DEBUG(dbgs() << "Reverse order of the two branches: " |
| 1288 | << getBlockName(PrevBB) << "\n"); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1289 | DEBUG(dbgs() << " Edge probability: " |
| 1290 | << MBPI->getEdgeProbability(PrevBB, FBB) << " vs " |
| 1291 | << MBPI->getEdgeProbability(PrevBB, TBB) << "\n"); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1292 | DebugLoc dl; // FIXME: this is nowhere |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1293 | TII->RemoveBranch(*PrevBB); |
| 1294 | TII->InsertBranch(*PrevBB, FBB, TBB, Cond, dl); |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1295 | needUpdateBr = true; |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1296 | } |
Shuxin Yang | 8b8fd21 | 2013-06-04 01:00:57 +0000 | [diff] [blame] | 1297 | if (needUpdateBr) |
| 1298 | PrevBB->updateTerminator(); |
Manman Ren | 2b6a0df | 2012-07-31 01:11:07 +0000 | [diff] [blame] | 1299 | } |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1300 | } |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1301 | |
| 1302 | // Fixup the last block. |
| 1303 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1304 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch. |
Chandler Carruth | 8d15078 | 2011-11-13 11:20:44 +0000 | [diff] [blame] | 1305 | if (!TII->AnalyzeBranch(F.back(), TBB, FBB, Cond)) |
| 1306 | F.back().updateTerminator(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1307 | |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 1308 | // Walk through the backedges of the function now that we have fully laid out |
| 1309 | // 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] | 1310 | // exclusively on the loop info here so that we can align backedges in |
| 1311 | // unnatural CFGs and backedges that were introduced purely because of the |
| 1312 | // loop rotations done during this layout pass. |
Sanjay Patel | 924879a | 2015-08-04 15:49:57 +0000 | [diff] [blame] | 1313 | // FIXME: Use Function::optForSize(). |
Duncan P. N. Exon Smith | 70eb9c5 | 2015-02-14 01:44:41 +0000 | [diff] [blame] | 1314 | if (F.getFunction()->hasFnAttribute(Attribute::OptimizeForSize)) |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1315 | return; |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1316 | if (FunctionChain.begin() == FunctionChain.end()) |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1317 | return; // Empty chain. |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1318 | |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1319 | const BranchProbability ColdProb(1, 5); // 20% |
Duncan P. N. Exon Smith | 6ac07fd | 2015-10-09 19:36:12 +0000 | [diff] [blame] | 1320 | BlockFrequency EntryFreq = MBFI->getBlockFreq(&F.front()); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1321 | BlockFrequency WeightedEntryFreq = EntryFreq * ColdProb; |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1322 | for (MachineBasicBlock *ChainBB : FunctionChain) { |
| 1323 | if (ChainBB == *FunctionChain.begin()) |
| 1324 | continue; |
| 1325 | |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1326 | // Don't align non-looping basic blocks. These are unlikely to execute |
| 1327 | // enough times to matter in practice. Note that we'll still handle |
| 1328 | // unnatural CFGs inside of a natural outer loop (the common case) and |
| 1329 | // rotated loops. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1330 | MachineLoop *L = MLI->getLoopFor(ChainBB); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1331 | if (!L) |
| 1332 | continue; |
| 1333 | |
Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 1334 | unsigned Align = TLI->getPrefLoopAlignment(L); |
| 1335 | if (!Align) |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1336 | continue; // Don't care about loop alignment. |
Hal Finkel | 5772566 | 2015-01-03 17:58:24 +0000 | [diff] [blame] | 1337 | |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1338 | // If the block is cold relative to the function entry don't waste space |
| 1339 | // aligning it. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1340 | BlockFrequency Freq = MBFI->getBlockFreq(ChainBB); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1341 | if (Freq < WeightedEntryFreq) |
| 1342 | continue; |
| 1343 | |
| 1344 | // If the block is cold relative to its loop header, don't align it |
| 1345 | // regardless of what edges into the block exist. |
| 1346 | MachineBasicBlock *LoopHeader = L->getHeader(); |
| 1347 | BlockFrequency LoopHeaderFreq = MBFI->getBlockFreq(LoopHeader); |
| 1348 | if (Freq < (LoopHeaderFreq * ColdProb)) |
| 1349 | continue; |
| 1350 | |
| 1351 | // Check for the existence of a non-layout predecessor which would benefit |
| 1352 | // from aligning this block. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1353 | MachineBasicBlock *LayoutPred = |
| 1354 | &*std::prev(MachineFunction::iterator(ChainBB)); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Force alignment if all the predecessors are jumps. We already checked |
| 1357 | // that the block isn't cold above. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1358 | if (!LayoutPred->isSuccessor(ChainBB)) { |
| 1359 | ChainBB->setAlignment(Align); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | // 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] | 1364 | // 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] | 1365 | // all of the hot entries into the block and thus alignment is likely to be |
| 1366 | // important. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1367 | BranchProbability LayoutProb = |
| 1368 | MBPI->getEdgeProbability(LayoutPred, ChainBB); |
Chandler Carruth | 881d0a7 | 2012-08-07 09:45:24 +0000 | [diff] [blame] | 1369 | BlockFrequency LayoutEdgeFreq = MBFI->getBlockFreq(LayoutPred) * LayoutProb; |
| 1370 | if (LayoutEdgeFreq <= (Freq * ColdProb)) |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1371 | ChainBB->setAlignment(Align); |
Chandler Carruth | ccc7e42 | 2012-04-16 01:12:56 +0000 | [diff] [blame] | 1372 | } |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1375 | bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &F) { |
| 1376 | // Check for single-block functions and skip them. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1377 | if (std::next(F.begin()) == F.end()) |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1378 | return false; |
| 1379 | |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 1380 | if (skipOptnoneFunction(*F.getFunction())) |
| 1381 | return false; |
| 1382 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1383 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
| 1384 | MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); |
Chandler Carruth | 8b9737c | 2011-10-21 08:57:37 +0000 | [diff] [blame] | 1385 | MLI = &getAnalysis<MachineLoopInfo>(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 1386 | TII = F.getSubtarget().getInstrInfo(); |
| 1387 | TLI = F.getSubtarget().getTargetLowering(); |
Daniel Jasper | 471e856 | 2015-03-04 11:05:34 +0000 | [diff] [blame] | 1388 | MDT = &getAnalysis<MachineDominatorTree>(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1389 | assert(BlockToChain.empty()); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1390 | |
Chandler Carruth | bd1be4d | 2011-10-23 09:18:45 +0000 | [diff] [blame] | 1391 | buildCFGChains(F); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1392 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1393 | BlockToChain.clear(); |
Chandler Carruth | fd9b4d9 | 2011-11-14 10:57:23 +0000 | [diff] [blame] | 1394 | ChainAllocator.DestroyAll(); |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1395 | |
Nadav Rotem | c0adc9f | 2013-04-12 01:24:16 +0000 | [diff] [blame] | 1396 | if (AlignAllBlock) |
| 1397 | // Align all of the blocks in the function to a specific alignment. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1398 | for (MachineBasicBlock &MBB : F) |
| 1399 | MBB.setAlignment(AlignAllBlock); |
Geoff Berry | 10494ac | 2016-01-21 17:25:52 +0000 | [diff] [blame] | 1400 | else if (AlignAllNonFallThruBlocks) { |
| 1401 | // Align all of the blocks that have no fall-through predecessors to a |
| 1402 | // specific alignment. |
| 1403 | for (auto MBI = std::next(F.begin()), MBE = F.end(); MBI != MBE; ++MBI) { |
| 1404 | auto LayoutPred = std::prev(MBI); |
| 1405 | if (!LayoutPred->isSuccessor(&*MBI)) |
| 1406 | MBI->setAlignment(AlignAllNonFallThruBlocks); |
| 1407 | } |
| 1408 | } |
Nadav Rotem | c0adc9f | 2013-04-12 01:24:16 +0000 | [diff] [blame] | 1409 | |
Chandler Carruth | 1028142 | 2011-10-21 06:46:38 +0000 | [diff] [blame] | 1410 | // We always return true as we have no way to track whether the final order |
| 1411 | // differs from the original order. |
| 1412 | return true; |
| 1413 | } |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1414 | |
| 1415 | namespace { |
| 1416 | /// \brief A pass to compute block placement statistics. |
| 1417 | /// |
| 1418 | /// A separate pass to compute interesting statistics for evaluating block |
| 1419 | /// 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] | 1420 | /// be computed in the absence of any placement transformations or when using |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1421 | /// alternative placement strategies. |
| 1422 | class MachineBlockPlacementStats : public MachineFunctionPass { |
| 1423 | /// \brief A handle to the branch probability pass. |
| 1424 | const MachineBranchProbabilityInfo *MBPI; |
| 1425 | |
| 1426 | /// \brief A handle to the function-wide block frequency pass. |
| 1427 | const MachineBlockFrequencyInfo *MBFI; |
| 1428 | |
| 1429 | public: |
| 1430 | static char ID; // Pass identification, replacement for typeid |
| 1431 | MachineBlockPlacementStats() : MachineFunctionPass(ID) { |
| 1432 | initializeMachineBlockPlacementStatsPass(*PassRegistry::getPassRegistry()); |
| 1433 | } |
| 1434 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1435 | bool runOnMachineFunction(MachineFunction &F) override; |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1436 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 1437 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1438 | AU.addRequired<MachineBranchProbabilityInfo>(); |
| 1439 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 1440 | AU.setPreservesAll(); |
| 1441 | MachineFunctionPass::getAnalysisUsage(AU); |
| 1442 | } |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1443 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 1444 | } |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1445 | |
| 1446 | char MachineBlockPlacementStats::ID = 0; |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 1447 | char &llvm::MachineBlockPlacementStatsID = MachineBlockPlacementStats::ID; |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1448 | INITIALIZE_PASS_BEGIN(MachineBlockPlacementStats, "block-placement-stats", |
| 1449 | "Basic Block Placement Stats", false, false) |
| 1450 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
| 1451 | INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo) |
| 1452 | INITIALIZE_PASS_END(MachineBlockPlacementStats, "block-placement-stats", |
| 1453 | "Basic Block Placement Stats", false, false) |
| 1454 | |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1455 | bool MachineBlockPlacementStats::runOnMachineFunction(MachineFunction &F) { |
| 1456 | // Check for single-block functions and skip them. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1457 | if (std::next(F.begin()) == F.end()) |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1458 | return false; |
| 1459 | |
| 1460 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
| 1461 | MBFI = &getAnalysis<MachineBlockFrequencyInfo>(); |
| 1462 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1463 | for (MachineBasicBlock &MBB : F) { |
| 1464 | BlockFrequency BlockFreq = MBFI->getBlockFreq(&MBB); |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1465 | Statistic &NumBranches = |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1466 | (MBB.succ_size() > 1) ? NumCondBranches : NumUncondBranches; |
Chandler Carruth | 2fc3fe1 | 2015-03-05 02:35:31 +0000 | [diff] [blame] | 1467 | Statistic &BranchTakenFreq = |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1468 | (MBB.succ_size() > 1) ? CondBranchTakenFreq : UncondBranchTakenFreq; |
| 1469 | for (MachineBasicBlock *Succ : MBB.successors()) { |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1470 | // Skip if this successor is a fallthrough. |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1471 | if (MBB.isLayoutSuccessor(Succ)) |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1472 | continue; |
| 1473 | |
Chandler Carruth | 7a715da | 2015-03-05 03:19:05 +0000 | [diff] [blame] | 1474 | BlockFrequency EdgeFreq = |
| 1475 | BlockFreq * MBPI->getEdgeProbability(&MBB, Succ); |
Chandler Carruth | ae4e800 | 2011-11-02 07:17:12 +0000 | [diff] [blame] | 1476 | ++NumBranches; |
| 1477 | BranchTakenFreq += EdgeFreq.getFrequency(); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | return false; |
| 1482 | } |