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