blob: a13a310dec97c57bf808e8257c2f80c284e2cd81 [file] [log] [blame]
Evan Chengbbf1db72009-05-07 05:42:24 +00001//===-- CodePlacementOpt.cpp - Code Placement pass. -----------------------===//
Evan Chengfb8075d2008-02-28 00:43:03 +00002//
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 Chengbbf1db72009-05-07 05:42:24 +000010// This file implements the pass that optimize code placement and align loop
11// headers to target specific alignment boundary.
Evan Chengfb8075d2008-02-28 00:43:03 +000012//
13//===----------------------------------------------------------------------===//
14
Evan Chengbbf1db72009-05-07 05:42:24 +000015#define DEBUG_TYPE "code-placement"
Evan Chengfb8075d2008-02-28 00:43:03 +000016#include "llvm/CodeGen/MachineLoopInfo.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/Passes.h"
Evan Cheng45e00102009-05-08 06:34:09 +000019#include "llvm/Target/TargetInstrInfo.h"
Evan Chengfb8075d2008-02-28 00:43:03 +000020#include "llvm/Target/TargetLowering.h"
21#include "llvm/Target/TargetMachine.h"
Evan Chengfb8075d2008-02-28 00:43:03 +000022#include "llvm/Support/Compiler.h"
23#include "llvm/Support/Debug.h"
Evan Cheng45e00102009-05-08 06:34:09 +000024#include "llvm/ADT/Statistic.h"
Evan Chengfb8075d2008-02-28 00:43:03 +000025using namespace llvm;
26
Dan Gohmancd2ae142009-10-15 00:36:22 +000027STATISTIC(NumLoopsAligned, "Number of loops aligned");
Evan Cheng45e00102009-05-08 06:34:09 +000028STATISTIC(NumIntraElim, "Number of intra loop branches eliminated");
29STATISTIC(NumIntraMoved, "Number of intra loop branches moved");
30
Evan Chengfb8075d2008-02-28 00:43:03 +000031namespace {
Evan Chengbbf1db72009-05-07 05:42:24 +000032 class CodePlacementOpt : public MachineFunctionPass {
Evan Cheng7132e122009-05-07 05:49:39 +000033 const MachineLoopInfo *MLI;
Evan Cheng45e00102009-05-08 06:34:09 +000034 const TargetInstrInfo *TII;
35 const TargetLowering *TLI;
36
Evan Chengfb8075d2008-02-28 00:43:03 +000037 public:
38 static char ID;
Evan Chengbbf1db72009-05-07 05:42:24 +000039 CodePlacementOpt() : MachineFunctionPass(&ID) {}
Evan Chengfb8075d2008-02-28 00:43:03 +000040
41 virtual bool runOnMachineFunction(MachineFunction &MF);
Evan Chengbbf1db72009-05-07 05:42:24 +000042 virtual const char *getPassName() const {
43 return "Code Placement Optimizater";
44 }
Evan Chengfb8075d2008-02-28 00:43:03 +000045
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.addRequired<MachineLoopInfo>();
Evan Cheng8b56a902008-09-22 22:21:38 +000048 AU.addPreservedID(MachineDominatorsID);
Evan Chengfb8075d2008-02-28 00:43:03 +000049 MachineFunctionPass::getAnalysisUsage(AU);
50 }
Evan Cheng7132e122009-05-07 05:49:39 +000051
52 private:
Dan Gohman07adb852009-10-20 04:50:37 +000053 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);
Dan Gohman07adb852009-10-20 04:50:37 +000059 bool EliminateUnconditionalJumpsToTop(MachineFunction &MF,
60 MachineLoop *L);
61 bool MoveDiscontiguousLoopBlocks(MachineFunction &MF,
62 MachineLoop *L);
63 bool OptimizeIntraLoopEdgesInLoopNest(MachineFunction &MF, MachineLoop *L);
64 bool OptimizeIntraLoopEdges(MachineFunction &MF);
Evan Cheng7132e122009-05-07 05:49:39 +000065 bool AlignLoops(MachineFunction &MF);
Dan Gohmancd2ae142009-10-15 00:36:22 +000066 bool AlignLoop(MachineFunction &MF, MachineLoop *L, unsigned Align);
Evan Chengfb8075d2008-02-28 00:43:03 +000067 };
68
Evan Chengbbf1db72009-05-07 05:42:24 +000069 char CodePlacementOpt::ID = 0;
Evan Chengfb8075d2008-02-28 00:43:03 +000070} // end anonymous namespace
71
Evan Chengbbf1db72009-05-07 05:42:24 +000072FunctionPass *llvm::createCodePlacementOptPass() {
73 return new CodePlacementOpt();
74}
Evan Chengfb8075d2008-02-28 00:43:03 +000075
Dan Gohman07adb852009-10-20 04:50:37 +000076/// HasFallthrough - Test whether the given branch has a fallthrough, either as
77/// a plain fallthrough or as a fallthrough case of a conditional branch.
Evan Cheng45e00102009-05-08 06:34:09 +000078///
Dan Gohman07adb852009-10-20 04:50:37 +000079bool CodePlacementOpt::HasFallthrough(MachineBasicBlock *MBB) {
80 MachineBasicBlock *TBB = 0, *FBB = 0;
81 SmallVector<MachineOperand, 4> Cond;
82 if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
83 return false;
84 // This conditional branch has no fallthrough.
85 if (FBB)
86 return false;
87 // An unconditional branch has no fallthrough.
88 if (Cond.empty() && TBB)
89 return false;
90 // It has a fallthrough.
91 return true;
92}
93
94/// HasAnalyzableTerminator - Test whether AnalyzeBranch will succeed on MBB.
95/// This is called before major changes are begun to test whether it will be
96/// possible to complete the changes.
Evan Cheng45e00102009-05-08 06:34:09 +000097///
Dan Gohman07adb852009-10-20 04:50:37 +000098/// Target-specific code is hereby encouraged to make AnalyzeBranch succeed
99/// whenever possible.
Evan Cheng45e00102009-05-08 06:34:09 +0000100///
Dan Gohman07adb852009-10-20 04:50:37 +0000101bool CodePlacementOpt::HasAnalyzableTerminator(MachineBasicBlock *MBB) {
102 // Conservatively ignore EH landing pads.
103 if (MBB->isLandingPad()) return false;
104
105 // Ignore blocks which look like they might have EH-related control flow.
106 // At the time of this writing, there are blocks which AnalyzeBranch
107 // thinks end in single uncoditional branches, yet which have two CFG
108 // successors. Code in this file is not prepared to reason about such things.
Chris Lattner518bb532010-02-09 19:54:29 +0000109 if (!MBB->empty() && MBB->back().isEHLabel())
Anton Korobeynikov766fc1d2009-10-19 18:21:09 +0000110 return false;
Evan Cheng45e00102009-05-08 06:34:09 +0000111
Dan Gohman07adb852009-10-20 04:50:37 +0000112 // Aggressively handle return blocks and similar constructs.
113 if (MBB->succ_empty()) return true;
Dan Gohman3bdd8de2009-10-17 00:32:43 +0000114
Dan Gohman07adb852009-10-20 04:50:37 +0000115 // Ask the target's AnalyzeBranch if it can handle this block.
116 MachineBasicBlock *TBB = 0, *FBB = 0;
117 SmallVector<MachineOperand, 4> Cond;
Dan Gohmanf3b11aa2010-02-10 20:04:19 +0000118 // Make sure the terminator is understood.
Dan Gohman07adb852009-10-20 04:50:37 +0000119 if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
120 return false;
121 // Make sure we have the option of reversing the condition.
122 if (!Cond.empty() && TII->ReverseBranchCondition(Cond))
123 return false;
124 return true;
125}
126
127/// Splice - Move the sequence of instructions [Begin,End) to just before
128/// InsertPt. Update branch instructions as needed to account for broken
129/// fallthrough edges and to take advantage of newly exposed fallthrough
130/// opportunities.
131///
132void CodePlacementOpt::Splice(MachineFunction &MF,
133 MachineFunction::iterator InsertPt,
134 MachineFunction::iterator Begin,
135 MachineFunction::iterator End) {
136 assert(Begin != MF.begin() && End != MF.begin() && InsertPt != MF.begin() &&
137 "Splice can't change the entry block!");
138 MachineFunction::iterator OldBeginPrior = prior(Begin);
139 MachineFunction::iterator OldEndPrior = prior(End);
140
141 MF.splice(InsertPt, Begin, End);
142
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000143 prior(Begin)->updateTerminator();
144 OldBeginPrior->updateTerminator();
145 OldEndPrior->updateTerminator();
Dan Gohman07adb852009-10-20 04:50:37 +0000146}
Evan Cheng45e00102009-05-08 06:34:09 +0000147
Dan Gohman07adb852009-10-20 04:50:37 +0000148/// EliminateUnconditionalJumpsToTop - Move blocks which unconditionally jump
149/// to the loop top to the top of the loop so that they have a fall through.
150/// This can introduce a branch on entry to the loop, but it can eliminate a
151/// branch within the loop. See the @simple case in
152/// test/CodeGen/X86/loop_blocks.ll for an example of this.
153bool CodePlacementOpt::EliminateUnconditionalJumpsToTop(MachineFunction &MF,
154 MachineLoop *L) {
155 bool Changed = false;
156 MachineBasicBlock *TopMBB = L->getTopBlock();
157
158 bool BotHasFallthrough = HasFallthrough(L->getBottomBlock());
159
160 if (TopMBB == MF.begin() ||
161 HasAnalyzableTerminator(prior(MachineFunction::iterator(TopMBB)))) {
162 new_top:
163 for (MachineBasicBlock::pred_iterator PI = TopMBB->pred_begin(),
164 PE = TopMBB->pred_end(); PI != PE; ++PI) {
165 MachineBasicBlock *Pred = *PI;
166 if (Pred == TopMBB) continue;
167 if (HasFallthrough(Pred)) continue;
168 if (!L->contains(Pred)) continue;
169
170 // Verify that we can analyze all the loop entry edges before beginning
171 // any changes which will require us to be able to analyze them.
172 if (Pred == MF.begin())
173 continue;
174 if (!HasAnalyzableTerminator(Pred))
175 continue;
176 if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Pred))))
177 continue;
178
179 // Move the block.
Dan Gohman3bdd8de2009-10-17 00:32:43 +0000180 Changed = true;
Dan Gohman3bdd8de2009-10-17 00:32:43 +0000181
Dan Gohman07adb852009-10-20 04:50:37 +0000182 // Move it and all the blocks that can reach it via fallthrough edges
183 // exclusively, to keep existing fallthrough edges intact.
184 MachineFunction::iterator Begin = Pred;
Chris Lattner7896c9f2009-12-03 00:50:42 +0000185 MachineFunction::iterator End = llvm::next(Begin);
Dan Gohman07adb852009-10-20 04:50:37 +0000186 while (Begin != MF.begin()) {
187 MachineFunction::iterator Prior = prior(Begin);
188 if (Prior == MF.begin())
189 break;
190 // Stop when a non-fallthrough edge is found.
191 if (!HasFallthrough(Prior))
192 break;
193 // Stop if a block which could fall-through out of the loop is found.
194 if (Prior->isSuccessor(End))
195 break;
196 // If we've reached the top, stop scanning.
197 if (Prior == MachineFunction::iterator(TopMBB)) {
198 // We know top currently has a fall through (because we just checked
199 // it) which would be lost if we do the transformation, so it isn't
200 // worthwhile to do the transformation unless it would expose a new
201 // fallthrough edge.
202 if (!Prior->isSuccessor(End))
203 goto next_pred;
204 // Otherwise we can stop scanning and procede to move the blocks.
Dan Gohman3bdd8de2009-10-17 00:32:43 +0000205 break;
206 }
Dan Gohman07adb852009-10-20 04:50:37 +0000207 // If we hit a switch or something complicated, don't move anything
208 // for this predecessor.
209 if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Prior))))
210 break;
211 // Ok, the block prior to Begin will be moved along with the rest.
212 // Extend the range to include it.
213 Begin = Prior;
214 ++NumIntraMoved;
Anton Korobeynikov766fc1d2009-10-19 18:21:09 +0000215 }
216
Dan Gohman07adb852009-10-20 04:50:37 +0000217 // Move the blocks.
218 Splice(MF, TopMBB, Begin, End);
Anton Korobeynikov766fc1d2009-10-19 18:21:09 +0000219
Dan Gohman07adb852009-10-20 04:50:37 +0000220 // Update TopMBB.
221 TopMBB = L->getTopBlock();
222
223 // We have a new loop top. Iterate on it. We shouldn't have to do this
224 // too many times if BranchFolding has done a reasonable job.
225 goto new_top;
226 next_pred:;
Anton Korobeynikov766fc1d2009-10-19 18:21:09 +0000227 }
Anton Korobeynikov766fc1d2009-10-19 18:21:09 +0000228 }
229
Dan Gohman07adb852009-10-20 04:50:37 +0000230 // If the loop previously didn't exit with a fall-through and it now does,
231 // we eliminated a branch.
232 if (Changed &&
233 !BotHasFallthrough &&
234 HasFallthrough(L->getBottomBlock())) {
235 ++NumIntraElim;
Dan Gohman07adb852009-10-20 04:50:37 +0000236 }
237
238 return Changed;
239}
240
241/// MoveDiscontiguousLoopBlocks - Move any loop blocks that are not in the
242/// portion of the loop contiguous with the header. This usually makes the loop
243/// contiguous, provided that AnalyzeBranch can handle all the relevant
244/// branching. See the @cfg_islands case in test/CodeGen/X86/loop_blocks.ll
245/// for an example of this.
246bool CodePlacementOpt::MoveDiscontiguousLoopBlocks(MachineFunction &MF,
247 MachineLoop *L) {
248 bool Changed = false;
249 MachineBasicBlock *TopMBB = L->getTopBlock();
250 MachineBasicBlock *BotMBB = L->getBottomBlock();
251
252 // Determine a position to move orphaned loop blocks to. If TopMBB is not
253 // entered via fallthrough and BotMBB is exited via fallthrough, prepend them
254 // to the top of the loop to avoid loosing that fallthrough. Otherwise append
255 // them to the bottom, even if it previously had a fallthrough, on the theory
256 // that it's worth an extra branch to keep the loop contiguous.
Chris Lattner7896c9f2009-12-03 00:50:42 +0000257 MachineFunction::iterator InsertPt =
258 llvm::next(MachineFunction::iterator(BotMBB));
Dan Gohman07adb852009-10-20 04:50:37 +0000259 bool InsertAtTop = false;
260 if (TopMBB != MF.begin() &&
261 !HasFallthrough(prior(MachineFunction::iterator(TopMBB))) &&
262 HasFallthrough(BotMBB)) {
263 InsertPt = TopMBB;
264 InsertAtTop = true;
265 }
266
267 // Keep a record of which blocks are in the portion of the loop contiguous
268 // with the loop header.
269 SmallPtrSet<MachineBasicBlock *, 8> ContiguousBlocks;
270 for (MachineFunction::iterator I = TopMBB,
Chris Lattner7896c9f2009-12-03 00:50:42 +0000271 E = llvm::next(MachineFunction::iterator(BotMBB)); I != E; ++I)
Dan Gohman07adb852009-10-20 04:50:37 +0000272 ContiguousBlocks.insert(I);
273
274 // Find non-contigous blocks and fix them.
275 if (InsertPt != MF.begin() && HasAnalyzableTerminator(prior(InsertPt)))
276 for (MachineLoop::block_iterator BI = L->block_begin(), BE = L->block_end();
277 BI != BE; ++BI) {
278 MachineBasicBlock *BB = *BI;
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 (!HasAnalyzableTerminator(BB))
283 continue;
284 if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(BB))))
285 continue;
286
287 // If the layout predecessor is part of the loop, this block will be
288 // processed along with it. This keeps them in their relative order.
289 if (BB != MF.begin() &&
290 L->contains(prior(MachineFunction::iterator(BB))))
291 continue;
292
293 // Check to see if this block is already contiguous with the main
294 // portion of the loop.
295 if (!ContiguousBlocks.insert(BB))
296 continue;
297
298 // Move the block.
299 Changed = true;
300
301 // Process this block and all loop blocks contiguous with it, to keep
302 // them in their relative order.
303 MachineFunction::iterator Begin = BB;
Chris Lattner7896c9f2009-12-03 00:50:42 +0000304 MachineFunction::iterator End = llvm::next(MachineFunction::iterator(BB));
Dan Gohman07adb852009-10-20 04:50:37 +0000305 for (; End != MF.end(); ++End) {
306 if (!L->contains(End)) break;
307 if (!HasAnalyzableTerminator(End)) break;
308 ContiguousBlocks.insert(End);
309 ++NumIntraMoved;
310 }
311
312 // If we're inserting at the bottom of the loop, and the code we're
313 // moving originally had fall-through successors, bring the sucessors
314 // up with the loop blocks to preserve the fall-through edges.
315 if (!InsertAtTop)
316 for (; End != MF.end(); ++End) {
317 if (L->contains(End)) break;
318 if (!HasAnalyzableTerminator(End)) break;
319 if (!HasFallthrough(prior(End))) break;
320 }
321
322 // Move the blocks. This may invalidate TopMBB and/or BotMBB, but
323 // we don't need them anymore at this point.
324 Splice(MF, InsertPt, Begin, End);
325 }
326
327 return Changed;
328}
329
330/// OptimizeIntraLoopEdgesInLoopNest - Reposition loop blocks to minimize
331/// intra-loop branching and to form contiguous loops.
332///
333/// This code takes the approach of making minor changes to the existing
334/// layout to fix specific loop-oriented problems. Also, it depends on
335/// AnalyzeBranch, which can't understand complex control instructions.
336///
337bool CodePlacementOpt::OptimizeIntraLoopEdgesInLoopNest(MachineFunction &MF,
338 MachineLoop *L) {
339 bool Changed = false;
340
341 // Do optimization for nested loops.
342 for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
343 Changed |= OptimizeIntraLoopEdgesInLoopNest(MF, *I);
344
345 // Do optimization for this loop.
346 Changed |= EliminateUnconditionalJumpsToTop(MF, L);
347 Changed |= MoveDiscontiguousLoopBlocks(MF, L);
348
349 return Changed;
350}
351
352/// OptimizeIntraLoopEdges - Reposition loop blocks to minimize
353/// intra-loop branching and to form contiguous loops.
354///
355bool CodePlacementOpt::OptimizeIntraLoopEdges(MachineFunction &MF) {
356 bool Changed = false;
357
358 if (!TLI->shouldOptimizeCodePlacement())
359 return Changed;
360
361 // Do optimization for each loop in the function.
362 for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
363 I != E; ++I)
364 if (!(*I)->getParentLoop())
365 Changed |= OptimizeIntraLoopEdgesInLoopNest(MF, *I);
366
Evan Cheng45e00102009-05-08 06:34:09 +0000367 return Changed;
368}
369
Evan Cheng7132e122009-05-07 05:49:39 +0000370/// AlignLoops - Align loop headers to target preferred alignments.
371///
372bool CodePlacementOpt::AlignLoops(MachineFunction &MF) {
Evan Cheng45e00102009-05-08 06:34:09 +0000373 const Function *F = MF.getFunction();
374 if (F->hasFnAttr(Attribute::OptimizeForSize))
Evan Cheng4f658e92008-02-29 17:52:15 +0000375 return false;
376
377 unsigned Align = TLI->getPrefLoopAlignment();
Evan Chengfb8075d2008-02-28 00:43:03 +0000378 if (!Align)
379 return false; // Don't care about loop alignment.
380
Evan Cheng7132e122009-05-07 05:49:39 +0000381 bool Changed = false;
Dan Gohmancd2ae142009-10-15 00:36:22 +0000382
383 for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
384 I != E; ++I)
385 Changed |= AlignLoop(MF, *I, Align);
386
387 return Changed;
388}
389
Dan Gohman07adb852009-10-20 04:50:37 +0000390/// AlignLoop - Align loop headers to target preferred alignments.
391///
Dan Gohmancd2ae142009-10-15 00:36:22 +0000392bool CodePlacementOpt::AlignLoop(MachineFunction &MF, MachineLoop *L,
393 unsigned Align) {
394 bool Changed = false;
395
396 // Do alignment for nested loops.
397 for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
398 Changed |= AlignLoop(MF, *I, Align);
399
Dan Gohman07adb852009-10-20 04:50:37 +0000400 L->getTopBlock()->setAlignment(Align);
Dan Gohmancd2ae142009-10-15 00:36:22 +0000401 Changed = true;
402 ++NumLoopsAligned;
403
Evan Cheng7132e122009-05-07 05:49:39 +0000404 return Changed;
405}
406
407bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
408 MLI = &getAnalysis<MachineLoopInfo>();
409 if (MLI->empty())
410 return false; // No loops.
411
Evan Cheng45e00102009-05-08 06:34:09 +0000412 TLI = MF.getTarget().getTargetLowering();
413 TII = MF.getTarget().getInstrInfo();
414
Dan Gohman07adb852009-10-20 04:50:37 +0000415 bool Changed = OptimizeIntraLoopEdges(MF);
Evan Cheng45e00102009-05-08 06:34:09 +0000416
Evan Cheng7132e122009-05-07 05:49:39 +0000417 Changed |= AlignLoops(MF);
418
419 return Changed;
Evan Chengfb8075d2008-02-28 00:43:03 +0000420}