Added option -align-loops=<true/false> to disable loop aligner pass.
[oota-llvm.git] / lib / CodeGen / LoopAligner.cpp
1 //===-- LoopAligner.cpp - Loop aligner pass. ------------------------------===//
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 //
10 // This file implements the pass that align loop headers to target specific
11 // alignment boundary.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "loopalign"
16 #include "llvm/CodeGen/MachineLoopInfo.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Target/TargetLowering.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 using namespace llvm;
25
26 namespace {
27   class LoopAligner : public MachineFunctionPass {
28     const TargetLowering *TLI;
29
30   public:
31     static char ID;
32     LoopAligner() : MachineFunctionPass((intptr_t)&ID) {}
33
34     virtual bool runOnMachineFunction(MachineFunction &MF);
35     virtual const char *getPassName() const { return "Loop aligner"; }
36
37     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38       AU.addRequired<MachineLoopInfo>();
39       AU.addPreserved<MachineLoopInfo>();
40       MachineFunctionPass::getAnalysisUsage(AU);
41     }
42   };
43
44   char LoopAligner::ID = 0;
45 } // end anonymous namespace
46
47 FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); }
48
49 bool LoopAligner::runOnMachineFunction(MachineFunction &MF) {
50   const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>();
51
52   if (MLI->begin() == MLI->end())
53     return false;  // No loops.
54
55   unsigned Align = MF.getTarget().getTargetLowering()->getPrefLoopAlignment();
56   if (!Align)
57     return false;  // Don't care about loop alignment.
58
59   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
60     MachineBasicBlock *MBB = I;
61     if (MLI->isLoopHeader(MBB))
62       MBB->setAlignment(Align);
63   }
64
65   return true;
66 }