Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 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" |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | class LoopAligner : public MachineFunctionPass { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 27 | public: |
| 28 | static char ID; |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 29 | LoopAligner() : MachineFunctionPass(&ID) {} |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 30 | |
| 31 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 32 | virtual const char *getPassName() const { return "Loop aligner"; } |
| 33 | |
| 34 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 35 | AU.addRequired<MachineLoopInfo>(); |
| 36 | AU.addPreserved<MachineLoopInfo>(); |
Evan Cheng | 8b56a90 | 2008-09-22 22:21:38 +0000 | [diff] [blame] | 37 | AU.addPreservedID(MachineDominatorsID); |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 38 | MachineFunctionPass::getAnalysisUsage(AU); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | char LoopAligner::ID = 0; |
| 43 | } // end anonymous namespace |
| 44 | |
| 45 | FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); } |
| 46 | |
| 47 | bool LoopAligner::runOnMachineFunction(MachineFunction &MF) { |
| 48 | const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>(); |
| 49 | |
Dan Gohman | a8c763b | 2008-08-14 18:13:49 +0000 | [diff] [blame] | 50 | if (MLI->empty()) |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 51 | return false; // No loops. |
| 52 | |
Evan Cheng | 4f658e9 | 2008-02-29 17:52:15 +0000 | [diff] [blame] | 53 | const TargetLowering *TLI = MF.getTarget().getTargetLowering(); |
| 54 | if (!TLI) |
| 55 | return false; |
| 56 | |
| 57 | unsigned Align = TLI->getPrefLoopAlignment(); |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 58 | if (!Align) |
| 59 | return false; // Don't care about loop alignment. |
| 60 | |
| 61 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 62 | MachineBasicBlock *MBB = I; |
| 63 | if (MLI->isLoopHeader(MBB)) |
| 64 | MBB->setAlignment(Align); |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |