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