Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 1 | //===-- CodePlacementOpt.cpp - Code Placement pass. -----------------------===// |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 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 | // |
Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 10 | // This file implements the pass that optimize code placement and align loop |
| 11 | // headers to target specific alignment boundary. |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "code-placement" |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 18 | #include "llvm/CodeGen/Passes.h" |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetInstrInfo.h" |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetLowering.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "llvm/Support/Debug.h" |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Dan Gohman | cd2ae14 | 2009-10-15 00:36:22 +0000 | [diff] [blame] | 27 | STATISTIC(NumLoopsAligned, "Number of loops aligned"); |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 28 | STATISTIC(NumIntraElim, "Number of intra loop branches eliminated"); |
| 29 | STATISTIC(NumIntraMoved, "Number of intra loop branches moved"); |
| 30 | |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 31 | namespace { |
Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 32 | class CodePlacementOpt : public MachineFunctionPass { |
Evan Cheng | 7132e12 | 2009-05-07 05:49:39 +0000 | [diff] [blame] | 33 | const MachineLoopInfo *MLI; |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 34 | const TargetInstrInfo *TII; |
| 35 | const TargetLowering *TLI; |
| 36 | |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 37 | public: |
| 38 | static char ID; |
Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 39 | CodePlacementOpt() : MachineFunctionPass(&ID) {} |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 40 | |
| 41 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 42 | virtual const char *getPassName() const { |
| 43 | return "Code Placement Optimizater"; |
| 44 | } |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 45 | |
| 46 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 47 | AU.addRequired<MachineLoopInfo>(); |
Evan Cheng | 8b56a90 | 2008-09-22 22:21:38 +0000 | [diff] [blame] | 48 | AU.addPreservedID(MachineDominatorsID); |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 49 | MachineFunctionPass::getAnalysisUsage(AU); |
| 50 | } |
Evan Cheng | 7132e12 | 2009-05-07 05:49:39 +0000 | [diff] [blame] | 51 | |
| 52 | private: |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 53 | bool HasFallthrough(MachineBasicBlock *MBB); |
| 54 | bool HasAnalyzableTerminator(MachineBasicBlock *MBB); |
| 55 | void Splice(MachineFunction &MF, |
| 56 | MachineFunction::iterator InsertPt, |
| 57 | MachineFunction::iterator Begin, |
| 58 | MachineFunction::iterator End); |
| 59 | void UpdateTerminator(MachineBasicBlock *MBB); |
| 60 | bool OptimizeIntraLoopEdges(MachineFunction &MF); |
| 61 | bool OptimizeIntraLoopEdgesInLoop(MachineFunction &MF, MachineLoop *L); |
Evan Cheng | 7132e12 | 2009-05-07 05:49:39 +0000 | [diff] [blame] | 62 | bool AlignLoops(MachineFunction &MF); |
Dan Gohman | cd2ae14 | 2009-10-15 00:36:22 +0000 | [diff] [blame] | 63 | bool AlignLoop(MachineFunction &MF, MachineLoop *L, unsigned Align); |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 66 | char CodePlacementOpt::ID = 0; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 67 | } // end anonymous namespace |
| 68 | |
Evan Cheng | bbf1db7 | 2009-05-07 05:42:24 +0000 | [diff] [blame] | 69 | FunctionPass *llvm::createCodePlacementOptPass() { |
| 70 | return new CodePlacementOpt(); |
| 71 | } |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 72 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 73 | /// HasFallthrough - Test whether the given branch has a fallthrough, either as |
| 74 | /// a plain fallthrough or as a fallthrough case of a conditional branch. |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 75 | /// |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 76 | bool CodePlacementOpt::HasFallthrough(MachineBasicBlock *MBB) { |
| 77 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 78 | SmallVector<MachineOperand, 4> Cond; |
| 79 | if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond)) |
| 80 | return false; |
| 81 | // This conditional branch has no fallthrough. |
| 82 | if (FBB) |
| 83 | return false; |
| 84 | // An unconditional branch has no fallthrough. |
| 85 | if (Cond.empty() && TBB) |
| 86 | return false; |
| 87 | // It has a fallthrough. |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | /// HasAnalyzableTerminator - Test whether AnalyzeBranch will succeed on MBB. |
| 92 | /// This is called before major changes are begun to test whether it will be |
| 93 | /// possible to complete the changes. |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 94 | /// |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 95 | /// Target-specific code is hereby encouraged to make AnalyzeBranch succeed |
| 96 | /// whenever possible. |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 97 | /// |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 98 | bool CodePlacementOpt::HasAnalyzableTerminator(MachineBasicBlock *MBB) { |
| 99 | // Conservatively ignore EH landing pads. |
| 100 | if (MBB->isLandingPad()) return false; |
| 101 | |
| 102 | // Ignore blocks which look like they might have EH-related control flow. |
| 103 | // At the time of this writing, there are blocks which AnalyzeBranch |
| 104 | // thinks end in single uncoditional branches, yet which have two CFG |
| 105 | // successors. Code in this file is not prepared to reason about such things. |
| 106 | if (!MBB->empty() && MBB->back().getOpcode() == TargetInstrInfo::EH_LABEL) |
Evan Cheng | 6ebf7bc | 2009-05-13 21:42:09 +0000 | [diff] [blame] | 107 | return false; |
| 108 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 109 | // Aggressively handle return blocks and similar constructs. |
| 110 | if (MBB->succ_empty()) return true; |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 111 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 112 | // Ask the target's AnalyzeBranch if it can handle this block. |
| 113 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 114 | SmallVector<MachineOperand, 4> Cond; |
| 115 | // Make the the terminator is understood. |
| 116 | if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond)) |
| 117 | return false; |
| 118 | // Make sure we have the option of reversing the condition. |
| 119 | if (!Cond.empty() && TII->ReverseBranchCondition(Cond)) |
| 120 | return false; |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | /// Splice - Move the sequence of instructions [Begin,End) to just before |
| 125 | /// InsertPt. Update branch instructions as needed to account for broken |
| 126 | /// fallthrough edges and to take advantage of newly exposed fallthrough |
| 127 | /// opportunities. |
| 128 | /// |
| 129 | void CodePlacementOpt::Splice(MachineFunction &MF, |
| 130 | MachineFunction::iterator InsertPt, |
| 131 | MachineFunction::iterator Begin, |
| 132 | MachineFunction::iterator End) { |
| 133 | assert(Begin != MF.begin() && End != MF.begin() && InsertPt != MF.begin() && |
| 134 | "Splice can't change the entry block!"); |
| 135 | MachineFunction::iterator OldBeginPrior = prior(Begin); |
| 136 | MachineFunction::iterator OldEndPrior = prior(End); |
| 137 | |
| 138 | MF.splice(InsertPt, Begin, End); |
| 139 | |
| 140 | UpdateTerminator(prior(Begin)); |
| 141 | UpdateTerminator(OldBeginPrior); |
| 142 | UpdateTerminator(OldEndPrior); |
| 143 | } |
| 144 | |
| 145 | /// UpdateTerminator - Update the terminator instructions in MBB to account |
| 146 | /// for changes to the layout. If the block previously used a fallthrough, |
| 147 | /// it may now need a branch, and if it previously used branching it may now |
| 148 | /// be able to use a fallthrough. |
| 149 | /// |
| 150 | void CodePlacementOpt::UpdateTerminator(MachineBasicBlock *MBB) { |
| 151 | // A block with no successors has no concerns with fall-through edges. |
| 152 | if (MBB->succ_empty()) return; |
| 153 | |
| 154 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 155 | SmallVector<MachineOperand, 4> Cond; |
Daniel Dunbar | c499e32 | 2009-10-17 23:15:04 +0000 | [diff] [blame] | 156 | bool B = TII->AnalyzeBranch(*MBB, TBB, FBB, Cond); |
| 157 | (void) B; |
| 158 | assert(!B && "UpdateTerminators requires analyzable predecessors!"); |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 159 | if (Cond.empty()) { |
| 160 | if (TBB) { |
| 161 | // The block has an unconditional branch. If its successor is now |
| 162 | // its layout successor, delete the branch. |
| 163 | if (MBB->isLayoutSuccessor(TBB)) |
| 164 | TII->RemoveBranch(*MBB); |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 165 | } else { |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 166 | // The block has an unconditional fallthrough. If its successor is not |
| 167 | // its layout successor, insert a branch. |
| 168 | TBB = *MBB->succ_begin(); |
| 169 | if (!MBB->isLayoutSuccessor(TBB)) |
| 170 | TII->InsertBranch(*MBB, TBB, 0, Cond); |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 171 | } |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 172 | } else { |
| 173 | if (FBB) { |
| 174 | // The block has a non-fallthrough conditional branch. If one of its |
| 175 | // successors is its layout successor, rewrite it to a fallthrough |
| 176 | // conditional branch. |
| 177 | if (MBB->isLayoutSuccessor(TBB)) { |
| 178 | TII->RemoveBranch(*MBB); |
| 179 | TII->ReverseBranchCondition(Cond); |
| 180 | TII->InsertBranch(*MBB, FBB, 0, Cond); |
| 181 | } else if (MBB->isLayoutSuccessor(FBB)) { |
| 182 | TII->RemoveBranch(*MBB); |
| 183 | TII->InsertBranch(*MBB, TBB, 0, Cond); |
| 184 | } |
| 185 | } else { |
| 186 | // The block has a fallthrough conditional branch. |
| 187 | MachineBasicBlock *MBBA = *MBB->succ_begin(); |
| 188 | MachineBasicBlock *MBBB = *next(MBB->succ_begin()); |
| 189 | if (MBBA == TBB) std::swap(MBBB, MBBA); |
| 190 | if (MBB->isLayoutSuccessor(TBB)) { |
| 191 | TII->RemoveBranch(*MBB); |
| 192 | TII->ReverseBranchCondition(Cond); |
| 193 | TII->InsertBranch(*MBB, MBBA, 0, Cond); |
| 194 | } else if (!MBB->isLayoutSuccessor(MBBA)) { |
| 195 | TII->RemoveBranch(*MBB); |
| 196 | TII->InsertBranch(*MBB, TBB, MBBA, Cond); |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 201 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 202 | /// OptimizeIntraLoopEdges - Reposition loop blocks to minimize |
| 203 | /// intra-loop branching and to form contiguous loops. |
| 204 | /// |
| 205 | bool CodePlacementOpt::OptimizeIntraLoopEdges(MachineFunction &MF) { |
| 206 | bool Changed = false; |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 207 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 208 | if (!TLI->shouldOptimizeCodePlacement()) |
| 209 | return Changed; |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 210 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 211 | for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end(); |
| 212 | I != E; ++I) |
| 213 | Changed |= OptimizeIntraLoopEdgesInLoop(MF, *I); |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 214 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 215 | return Changed; |
| 216 | } |
| 217 | |
| 218 | /// OptimizeIntraLoopEdgesInLoop - Reposition loop blocks to minimize |
| 219 | /// intra-loop branching and to form contiguous loops. |
| 220 | /// |
| 221 | /// This code takes the approach of making minor changes to the existing |
| 222 | /// layout to fix specific loop-oriented problems. Also, it depends on |
| 223 | /// AnalyzeBranch, which can't understand complex control instructions. |
| 224 | /// |
| 225 | bool CodePlacementOpt::OptimizeIntraLoopEdgesInLoop(MachineFunction &MF, |
| 226 | MachineLoop *L) { |
| 227 | bool Changed = false; |
| 228 | |
| 229 | // Do optimization for nested loops. |
| 230 | for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 231 | Changed |= OptimizeIntraLoopEdgesInLoop(MF, *I); |
| 232 | |
| 233 | // Keep a record of which blocks are in the portion of the loop contiguous |
| 234 | // with the loop header. |
| 235 | SmallPtrSet<MachineBasicBlock *, 8> ContiguousBlocks; |
| 236 | ContiguousBlocks.insert(L->getHeader()); |
| 237 | |
| 238 | // Find the loop "top", ignoring any discontiguous parts. |
| 239 | MachineBasicBlock *TopMBB = L->getHeader(); |
| 240 | if (TopMBB != MF.begin()) { |
| 241 | MachineBasicBlock *PriorMBB = prior(MachineFunction::iterator(TopMBB)); |
| 242 | while (L->contains(PriorMBB)) { |
| 243 | ContiguousBlocks.insert(PriorMBB); |
| 244 | TopMBB = PriorMBB; |
| 245 | if (TopMBB == MF.begin()) break; |
| 246 | PriorMBB = prior(MachineFunction::iterator(TopMBB)); |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 247 | } |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 250 | // Find the loop "bottom", ignoring any discontiguous parts. |
| 251 | MachineBasicBlock *BotMBB = L->getHeader(); |
| 252 | if (BotMBB != prior(MF.end())) { |
| 253 | MachineBasicBlock *NextMBB = next(MachineFunction::iterator(BotMBB)); |
| 254 | while (L->contains(NextMBB)) { |
| 255 | ContiguousBlocks.insert(NextMBB); |
| 256 | BotMBB = NextMBB; |
| 257 | if (BotMBB == next(MachineFunction::iterator(BotMBB))) break; |
| 258 | NextMBB = next(MachineFunction::iterator(BotMBB)); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // First, move blocks which unconditionally jump to the loop top to the |
| 263 | // top of the loop so that they have a fall through. This can introduce a |
| 264 | // branch on entry to the loop, but it can eliminate a branch within the |
| 265 | // loop. See the @simple case in test/CodeGen/X86/loop_blocks.ll for an |
| 266 | // example of this. |
| 267 | |
| 268 | bool BotHasFallthrough = HasFallthrough(BotMBB); |
| 269 | |
| 270 | if (TopMBB == MF.begin() || |
| 271 | HasAnalyzableTerminator(prior(MachineFunction::iterator(TopMBB)))) { |
| 272 | new_top: |
| 273 | for (MachineBasicBlock::pred_iterator PI = TopMBB->pred_begin(), |
| 274 | PE = TopMBB->pred_end(); PI != PE; ++PI) { |
| 275 | MachineBasicBlock *Pred = *PI; |
| 276 | if (Pred == TopMBB) continue; |
| 277 | if (HasFallthrough(Pred)) continue; |
| 278 | if (!L->contains(Pred)) continue; |
| 279 | |
| 280 | // Verify that we can analyze all the loop entry edges before beginning |
| 281 | // any changes which will require us to be able to analyze them. |
| 282 | if (Pred == MF.begin()) |
| 283 | continue; |
| 284 | if (!HasAnalyzableTerminator(Pred)) |
| 285 | continue; |
| 286 | if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Pred)))) |
| 287 | continue; |
| 288 | |
| 289 | // Move the block. |
| 290 | Changed = true; |
| 291 | ContiguousBlocks.insert(Pred); |
| 292 | |
| 293 | // Move it and all the blocks that can reach it via fallthrough edges |
| 294 | // exclusively, to keep existing fallthrough-edges intact. |
| 295 | MachineFunction::iterator Begin = Pred; |
| 296 | MachineFunction::iterator End = next(Begin); |
| 297 | while (Begin != MF.begin()) { |
| 298 | MachineFunction::iterator Prior = prior(Begin); |
| 299 | if (Prior == MF.begin()) |
| 300 | break; |
| 301 | // Stop when a non-fallthrough edge is found. |
| 302 | if (!HasFallthrough(Prior)) |
| 303 | break; |
| 304 | // Stop if a block which could fall-through out of the loop is found. |
| 305 | if (Prior->isSuccessor(End)) |
| 306 | break; |
| 307 | // If we've reached the top, stop scanning. |
| 308 | if (Prior == MachineFunction::iterator(TopMBB)) { |
| 309 | // We know top currently has a fall through (because we just checked |
| 310 | // it) which would be lost if we do the transformation, so it isn't |
| 311 | // worthwhile to do the transformation unless it would expose a new |
| 312 | // fallthrough edge. |
| 313 | if (!Prior->isSuccessor(End)) |
| 314 | goto next_pred; |
| 315 | // Otherwise we can stop scanning and procede to move the blocks. |
| 316 | break; |
| 317 | } |
| 318 | // If we hit a switch or something complicated, don't move anything |
| 319 | // for this predecessor. |
| 320 | if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Prior)))) |
| 321 | break; |
| 322 | Begin = Prior; |
| 323 | ContiguousBlocks.insert(Begin); |
| 324 | ++NumIntraMoved; |
| 325 | } |
| 326 | |
| 327 | // Update BotMBB, before moving Begin/End around and forgetting where |
| 328 | // the new bottom is. |
| 329 | if (BotMBB == prior(End)) |
| 330 | BotMBB = prior(Begin); |
| 331 | |
| 332 | // Move the blocks. |
| 333 | Splice(MF, TopMBB, Begin, End); |
| 334 | |
| 335 | // Update TopMBB, now that all the updates requiring the old top are |
| 336 | // complete. |
| 337 | TopMBB = Begin; |
| 338 | |
| 339 | // We have a new loop top. Iterate on it. We shouldn't have to do this |
| 340 | // too many times if BranchFolding has done a reasonable job. |
| 341 | goto new_top; |
| 342 | next_pred:; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | // If the loop previously didn't exit with a fall-through and it now does, |
| 347 | // we eliminated a branch. |
| 348 | if (!BotHasFallthrough && HasFallthrough(BotMBB)) { |
| 349 | ++NumIntraElim; |
| 350 | BotHasFallthrough = true; |
| 351 | } |
| 352 | |
| 353 | // Next, move any loop blocks that are not in the portion of the loop |
| 354 | // contiguous with the header. This makes the loop contiguous, provided that |
| 355 | // AnalyzeBranch can handle all the relevant branching. See the @cfg_islands |
| 356 | // case in test/CodeGen/X86/loop_blocks.ll for an example of this. |
| 357 | |
| 358 | // Determine a position to move orphaned loop blocks to. If TopMBB is not |
| 359 | // entered via fallthrough and BotMBB is exited via fallthrough, prepend them |
| 360 | // to the top of the loop to avoid loosing that fallthrough. Otherwise append |
| 361 | // them to the bottom, even if it previously had a fallthrough, on the theory |
| 362 | // that it's worth an extra branch to keep the loop contiguous. |
| 363 | MachineFunction::iterator InsertPt = next(MachineFunction::iterator(BotMBB)); |
| 364 | bool InsertAtTop = false; |
| 365 | if (TopMBB != MF.begin() && |
| 366 | !HasFallthrough(prior(MachineFunction::iterator(TopMBB))) && |
| 367 | HasFallthrough(BotMBB)) { |
| 368 | InsertPt = TopMBB; |
| 369 | InsertAtTop = true; |
| 370 | } |
| 371 | |
| 372 | // Find non-contigous blocks and fix them. |
| 373 | if (InsertPt != MF.begin() && HasAnalyzableTerminator(prior(InsertPt))) |
| 374 | for (MachineLoop::block_iterator BI = L->block_begin(), BE = L->block_end(); |
| 375 | BI != BE; ++BI) { |
| 376 | MachineBasicBlock *BB = *BI; |
| 377 | |
| 378 | // Verify that we can analyze all the loop entry edges before beginning |
| 379 | // any changes which will require us to be able to analyze them. |
| 380 | if (!HasAnalyzableTerminator(BB)) |
| 381 | continue; |
| 382 | if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(BB)))) |
| 383 | continue; |
| 384 | |
| 385 | // If the layout predecessor is part of the loop, this block will be |
| 386 | // processed along with it. This keeps them in their relative order. |
| 387 | if (BB != MF.begin() && |
| 388 | L->contains(prior(MachineFunction::iterator(BB)))) |
| 389 | continue; |
| 390 | |
| 391 | // Check to see if this block is already contiguous with the main |
| 392 | // portion of the loop. |
| 393 | if (!ContiguousBlocks.insert(BB)) |
| 394 | continue; |
| 395 | |
| 396 | // Move the block. |
| 397 | Changed = true; |
| 398 | |
| 399 | // Process this block and all loop blocks contiguous with it, to keep |
| 400 | // them in their relative order. |
| 401 | MachineFunction::iterator Begin = BB; |
| 402 | MachineFunction::iterator End = next(MachineFunction::iterator(BB)); |
| 403 | for (; End != MF.end(); ++End) { |
| 404 | if (!L->contains(End)) break; |
| 405 | if (!HasAnalyzableTerminator(End)) break; |
| 406 | ContiguousBlocks.insert(End); |
| 407 | ++NumIntraMoved; |
| 408 | } |
| 409 | |
| 410 | // Update BotMBB. |
| 411 | if (!InsertAtTop) |
| 412 | BotMBB = prior(End); |
| 413 | |
| 414 | // If we're inserting at the bottom of the loop, and the code we're |
| 415 | // moving originally had fall-through successors, bring the sucessors |
| 416 | // up with the loop blocks to preserve the fall-through edges. |
| 417 | if (!InsertAtTop) |
| 418 | for (; End != MF.end(); ++End) { |
| 419 | if (L->contains(End)) break; |
| 420 | if (!HasAnalyzableTerminator(End)) break; |
| 421 | if (!HasFallthrough(prior(End))) break; |
| 422 | } |
| 423 | |
| 424 | // Move the blocks. |
| 425 | Splice(MF, InsertPt, Begin, End); |
| 426 | |
| 427 | // Update TopMBB. |
| 428 | if (InsertAtTop) |
| 429 | TopMBB = Begin; |
| 430 | } |
| 431 | |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 432 | return Changed; |
| 433 | } |
| 434 | |
Evan Cheng | 7132e12 | 2009-05-07 05:49:39 +0000 | [diff] [blame] | 435 | /// AlignLoops - Align loop headers to target preferred alignments. |
| 436 | /// |
| 437 | bool CodePlacementOpt::AlignLoops(MachineFunction &MF) { |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 438 | const Function *F = MF.getFunction(); |
| 439 | if (F->hasFnAttr(Attribute::OptimizeForSize)) |
Evan Cheng | 4f658e9 | 2008-02-29 17:52:15 +0000 | [diff] [blame] | 440 | return false; |
| 441 | |
| 442 | unsigned Align = TLI->getPrefLoopAlignment(); |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 443 | if (!Align) |
| 444 | return false; // Don't care about loop alignment. |
| 445 | |
Evan Cheng | 7132e12 | 2009-05-07 05:49:39 +0000 | [diff] [blame] | 446 | bool Changed = false; |
Dan Gohman | cd2ae14 | 2009-10-15 00:36:22 +0000 | [diff] [blame] | 447 | |
| 448 | for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end(); |
| 449 | I != E; ++I) |
| 450 | Changed |= AlignLoop(MF, *I, Align); |
| 451 | |
| 452 | return Changed; |
| 453 | } |
| 454 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 455 | /// AlignLoop - Align loop headers to target preferred alignments. |
| 456 | /// |
Dan Gohman | cd2ae14 | 2009-10-15 00:36:22 +0000 | [diff] [blame] | 457 | bool CodePlacementOpt::AlignLoop(MachineFunction &MF, MachineLoop *L, |
| 458 | unsigned Align) { |
| 459 | bool Changed = false; |
| 460 | |
| 461 | // Do alignment for nested loops. |
| 462 | for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 463 | Changed |= AlignLoop(MF, *I, Align); |
| 464 | |
| 465 | MachineBasicBlock *TopMBB = L->getHeader(); |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 466 | if (TopMBB != MF.begin()) { |
| 467 | MachineBasicBlock *PredMBB = prior(MachineFunction::iterator(TopMBB)); |
| 468 | while (L->contains(PredMBB)) { |
| 469 | TopMBB = PredMBB; |
| 470 | if (TopMBB == MF.begin()) break; |
| 471 | PredMBB = prior(MachineFunction::iterator(TopMBB)); |
| 472 | } |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Dan Gohman | cd2ae14 | 2009-10-15 00:36:22 +0000 | [diff] [blame] | 475 | TopMBB->setAlignment(Align); |
| 476 | Changed = true; |
| 477 | ++NumLoopsAligned; |
| 478 | |
Evan Cheng | 7132e12 | 2009-05-07 05:49:39 +0000 | [diff] [blame] | 479 | return Changed; |
| 480 | } |
| 481 | |
| 482 | bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) { |
| 483 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 484 | if (MLI->empty()) |
| 485 | return false; // No loops. |
| 486 | |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 487 | TLI = MF.getTarget().getTargetLowering(); |
| 488 | TII = MF.getTarget().getInstrInfo(); |
| 489 | |
Dan Gohman | 3bdd8de | 2009-10-17 00:32:43 +0000 | [diff] [blame] | 490 | bool Changed = OptimizeIntraLoopEdges(MF); |
Evan Cheng | 45e0010 | 2009-05-08 06:34:09 +0000 | [diff] [blame] | 491 | |
Evan Cheng | 7132e12 | 2009-05-07 05:49:39 +0000 | [diff] [blame] | 492 | Changed |= AlignLoops(MF); |
| 493 | |
| 494 | return Changed; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 495 | } |