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