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