1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- CodePlacementOpt.cpp - Code Placement pass. -----------------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
1019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// This file implements the pass that optimizes code placement and aligns loop
1119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman// headers to target-specific alignment boundaries.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define DEBUG_TYPE "code-placement"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineLoopInfo.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunctionPass.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/Passes.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetInstrInfo.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetLowering.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetMachine.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Compiler.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/Statistic.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumLoopsAligned,  "Number of loops aligned");
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumIntraElim,     "Number of intra loop branches eliminated");
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumIntraMoved,    "Number of intra loop branches moved");
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  class CodePlacementOpt : public MachineFunctionPass {
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const MachineLoopInfo *MLI;
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetInstrInfo *TII;
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const TargetLowering  *TLI;
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  public:
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    static char ID;
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CodePlacementOpt() : MachineFunctionPass(ID) {}
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    virtual bool runOnMachineFunction(MachineFunction &MF);
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    virtual const char *getPassName() const {
4319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      return "Code Placement Optimizer";
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AU.addRequired<MachineLoopInfo>();
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      AU.addPreservedID(MachineDominatorsID);
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineFunctionPass::getAnalysisUsage(AU);
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  private:
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool HasFallthrough(MachineBasicBlock *MBB);
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool HasAnalyzableTerminator(MachineBasicBlock *MBB);
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void Splice(MachineFunction &MF,
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                MachineFunction::iterator InsertPt,
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                MachineFunction::iterator Begin,
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                MachineFunction::iterator End);
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool EliminateUnconditionalJumpsToTop(MachineFunction &MF,
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          MachineLoop *L);
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool MoveDiscontiguousLoopBlocks(MachineFunction &MF,
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     MachineLoop *L);
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool OptimizeIntraLoopEdgesInLoopNest(MachineFunction &MF, MachineLoop *L);
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool OptimizeIntraLoopEdges(MachineFunction &MF);
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool AlignLoops(MachineFunction &MF);
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool AlignLoop(MachineFunction &MF, MachineLoop *L, unsigned Align);
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  char CodePlacementOpt::ID = 0;
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // end anonymous namespace
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanFunctionPass *llvm::createCodePlacementOptPass() {
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return new CodePlacementOpt();
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// HasFallthrough - Test whether the given branch has a fallthrough, either as
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// a plain fallthrough or as a fallthrough case of a conditional branch.
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::HasFallthrough(MachineBasicBlock *MBB) {
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *TBB = 0, *FBB = 0;
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<MachineOperand, 4> Cond;
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This conditional branch has no fallthrough.
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (FBB)
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // An unconditional branch has no fallthrough.
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Cond.empty() && TBB)
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // It has a fallthrough.
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// HasAnalyzableTerminator - Test whether AnalyzeBranch will succeed on MBB.
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This is called before major changes are begun to test whether it will be
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// possible to complete the changes.
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Target-specific code is hereby encouraged to make AnalyzeBranch succeed
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// whenever possible.
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::HasAnalyzableTerminator(MachineBasicBlock *MBB) {
10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Conservatively ignore EH landing pads.
10319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MBB->isLandingPad()) return false;
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Aggressively handle return blocks and similar constructs.
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MBB->succ_empty()) return true;
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Ask the target's AnalyzeBranch if it can handle this block.
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *TBB = 0, *FBB = 0;
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<MachineOperand, 4> Cond;
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Make sure the terminator is understood.
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   // Ignore blocks which look like they might have EH-related control flow.
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   // AnalyzeBranch thinks it knows how to analyze such things, but it doesn't
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   // recognize the possibility of a control transfer through an unwind.
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   // Such blocks contain EH_LABEL instructions, however they may be in the
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   // middle of the block. Instead of searching for them, just check to see
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman   // if the CFG disagrees with AnalyzeBranch.
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (1u + !Cond.empty() != MBB->succ_size())
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Make sure we have the option of reversing the condition.
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Cond.empty() && TII->ReverseBranchCondition(Cond))
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// Splice - Move the sequence of instructions [Begin,End) to just before
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// InsertPt. Update branch instructions as needed to account for broken
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// fallthrough edges and to take advantage of newly exposed fallthrough
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// opportunities.
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid CodePlacementOpt::Splice(MachineFunction &MF,
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MachineFunction::iterator InsertPt,
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MachineFunction::iterator Begin,
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MachineFunction::iterator End) {
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Begin != MF.begin() && End != MF.begin() && InsertPt != MF.begin() &&
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Splice can't change the entry block!");
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator OldBeginPrior = prior(Begin);
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator OldEndPrior = prior(End);
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MF.splice(InsertPt, Begin, End);
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  prior(Begin)->updateTerminator();
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OldBeginPrior->updateTerminator();
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OldEndPrior->updateTerminator();
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// EliminateUnconditionalJumpsToTop - Move blocks which unconditionally jump
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// to the loop top to the top of the loop so that they have a fall through.
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This can introduce a branch on entry to the loop, but it can eliminate a
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// branch within the loop. See the @simple case in
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// test/CodeGen/X86/loop_blocks.ll for an example of this.
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::EliminateUnconditionalJumpsToTop(MachineFunction &MF,
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                        MachineLoop *L) {
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = false;
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *TopMBB = L->getTopBlock();
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool BotHasFallthrough = HasFallthrough(L->getBottomBlock());
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TopMBB == MF.begin() ||
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HasAnalyzableTerminator(prior(MachineFunction::iterator(TopMBB)))) {
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  new_top:
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (MachineBasicBlock::pred_iterator PI = TopMBB->pred_begin(),
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         PE = TopMBB->pred_end(); PI != PE; ++PI) {
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineBasicBlock *Pred = *PI;
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Pred == TopMBB) continue;
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (HasFallthrough(Pred)) continue;
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!L->contains(Pred)) continue;
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Verify that we can analyze all the loop entry edges before beginning
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // any changes which will require us to be able to analyze them.
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Pred == MF.begin())
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!HasAnalyzableTerminator(Pred))
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Pred))))
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Move the block.
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DEBUG(dbgs() << "CGP: Moving blocks starting at BB#" << Pred->getNumber()
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   << " to top of loop.\n");
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Changed = true;
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Move it and all the blocks that can reach it via fallthrough edges
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // exclusively, to keep existing fallthrough edges intact.
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineFunction::iterator Begin = Pred;
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineFunction::iterator End = llvm::next(Begin);
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      while (Begin != MF.begin()) {
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MachineFunction::iterator Prior = prior(Begin);
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Prior == MF.begin())
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Stop when a non-fallthrough edge is found.
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!HasFallthrough(Prior))
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Stop if a block which could fall-through out of the loop is found.
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Prior->isSuccessor(End))
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If we've reached the top, stop scanning.
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (Prior == MachineFunction::iterator(TopMBB)) {
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // We know top currently has a fall through (because we just checked
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // it) which would be lost if we do the transformation, so it isn't
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // worthwhile to do the transformation unless it would expose a new
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // fallthrough edge.
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!Prior->isSuccessor(End))
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            goto next_pred;
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // Otherwise we can stop scanning and procede to move the blocks.
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If we hit a switch or something complicated, don't move anything
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // for this predecessor.
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Prior))))
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Ok, the block prior to Begin will be moved along with the rest.
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Extend the range to include it.
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Begin = Prior;
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ++NumIntraMoved;
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Move the blocks.
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Splice(MF, TopMBB, Begin, End);
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Update TopMBB.
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TopMBB = L->getTopBlock();
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // We have a new loop top. Iterate on it. We shouldn't have to do this
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // too many times if BranchFolding has done a reasonable job.
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      goto new_top;
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    next_pred:;
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the loop previously didn't exit with a fall-through and it now does,
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // we eliminated a branch.
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Changed &&
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      !BotHasFallthrough &&
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HasFallthrough(L->getBottomBlock())) {
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ++NumIntraElim;
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Changed;
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// MoveDiscontiguousLoopBlocks - Move any loop blocks that are not in the
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// portion of the loop contiguous with the header. This usually makes the loop
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// contiguous, provided that AnalyzeBranch can handle all the relevant
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// branching. See the @cfg_islands case in test/CodeGen/X86/loop_blocks.ll
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// for an example of this.
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::MoveDiscontiguousLoopBlocks(MachineFunction &MF,
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   MachineLoop *L) {
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = false;
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *TopMBB = L->getTopBlock();
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *BotMBB = L->getBottomBlock();
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Determine a position to move orphaned loop blocks to. If TopMBB is not
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // entered via fallthrough and BotMBB is exited via fallthrough, prepend them
25719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // to the top of the loop to avoid losing that fallthrough. Otherwise append
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // them to the bottom, even if it previously had a fallthrough, on the theory
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // that it's worth an extra branch to keep the loop contiguous.
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator InsertPt =
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm::next(MachineFunction::iterator(BotMBB));
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool InsertAtTop = false;
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TopMBB != MF.begin() &&
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      !HasFallthrough(prior(MachineFunction::iterator(TopMBB))) &&
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      HasFallthrough(BotMBB)) {
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InsertPt = TopMBB;
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InsertAtTop = true;
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Keep a record of which blocks are in the portion of the loop contiguous
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // with the loop header.
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallPtrSet<MachineBasicBlock *, 8> ContiguousBlocks;
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineFunction::iterator I = TopMBB,
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       E = llvm::next(MachineFunction::iterator(BotMBB)); I != E; ++I)
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ContiguousBlocks.insert(I);
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Find non-contigous blocks and fix them.
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (InsertPt != MF.begin() && HasAnalyzableTerminator(prior(InsertPt)))
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (MachineLoop::block_iterator BI = L->block_begin(), BE = L->block_end();
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         BI != BE; ++BI) {
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineBasicBlock *BB = *BI;
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Verify that we can analyze all the loop entry edges before beginning
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // any changes which will require us to be able to analyze them.
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!HasAnalyzableTerminator(BB))
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(BB))))
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If the layout predecessor is part of the loop, this block will be
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // processed along with it. This keeps them in their relative order.
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (BB != MF.begin() &&
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          L->contains(prior(MachineFunction::iterator(BB))))
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Check to see if this block is already contiguous with the main
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // portion of the loop.
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!ContiguousBlocks.insert(BB))
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        continue;
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Move the block.
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DEBUG(dbgs() << "CGP: Moving blocks starting at BB#" << BB->getNumber()
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   << " to be contiguous with loop.\n");
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Changed = true;
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Process this block and all loop blocks contiguous with it, to keep
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // them in their relative order.
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineFunction::iterator Begin = BB;
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineFunction::iterator End = llvm::next(MachineFunction::iterator(BB));
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (; End != MF.end(); ++End) {
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!L->contains(End)) break;
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!HasAnalyzableTerminator(End)) break;
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ContiguousBlocks.insert(End);
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        ++NumIntraMoved;
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If we're inserting at the bottom of the loop, and the code we're
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // moving originally had fall-through successors, bring the sucessors
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // up with the loop blocks to preserve the fall-through edges.
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!InsertAtTop)
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (; End != MF.end(); ++End) {
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (L->contains(End)) break;
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!HasAnalyzableTerminator(End)) break;
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (!HasFallthrough(prior(End))) break;
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Move the blocks. This may invalidate TopMBB and/or BotMBB, but
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // we don't need them anymore at this point.
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Splice(MF, InsertPt, Begin, End);
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Changed;
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// OptimizeIntraLoopEdgesInLoopNest - Reposition loop blocks to minimize
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// intra-loop branching and to form contiguous loops.
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// This code takes the approach of making minor changes to the existing
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// layout to fix specific loop-oriented problems. Also, it depends on
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// AnalyzeBranch, which can't understand complex control instructions.
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::OptimizeIntraLoopEdgesInLoopNest(MachineFunction &MF,
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                        MachineLoop *L) {
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = false;
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Do optimization for nested loops.
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Changed |= OptimizeIntraLoopEdgesInLoopNest(MF, *I);
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Do optimization for this loop.
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Changed |= EliminateUnconditionalJumpsToTop(MF, L);
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Changed |= MoveDiscontiguousLoopBlocks(MF, L);
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Changed;
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// OptimizeIntraLoopEdges - Reposition loop blocks to minimize
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// intra-loop branching and to form contiguous loops.
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::OptimizeIntraLoopEdges(MachineFunction &MF) {
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = false;
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!TLI->shouldOptimizeCodePlacement())
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Changed;
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Do optimization for each loop in the function.
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!(*I)->getParentLoop())
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Changed |= OptimizeIntraLoopEdgesInLoopNest(MF, *I);
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Changed;
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// AlignLoops - Align loop headers to target preferred alignments.
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::AlignLoops(MachineFunction &MF) {
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Function *F = MF.getFunction();
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (F->hasFnAttr(Attribute::OptimizeForSize))
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Align = TLI->getPrefLoopAlignment();
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Align)
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;  // Don't care about loop alignment.
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = false;
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       I != E; ++I)
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Changed |= AlignLoop(MF, *I, Align);
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Changed;
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// AlignLoop - Align loop headers to target preferred alignments.
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::AlignLoop(MachineFunction &MF, MachineLoop *L,
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 unsigned Align) {
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = false;
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Do alignment for nested loops.
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Changed |= AlignLoop(MF, *I, Align);
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  L->getTopBlock()->setAlignment(Align);
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Changed = true;
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++NumLoopsAligned;
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Changed;
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MLI = &getAnalysis<MachineLoopInfo>();
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MLI->empty())
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;  // No loops.
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TLI = MF.getTarget().getTargetLowering();
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  TII = MF.getTarget().getInstrInfo();
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool Changed = OptimizeIntraLoopEdges(MF);
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Changed |= AlignLoops(MF);
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
424894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Changed;
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
426