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 { |
| 27 | const TargetLowering *TLI; |
| 28 | |
| 29 | public: |
| 30 | static char ID; |
| 31 | LoopAligner() : MachineFunctionPass((intptr_t)&ID) {} |
| 32 | |
| 33 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 34 | virtual const char *getPassName() const { return "Loop aligner"; } |
| 35 | |
| 36 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 37 | AU.addRequired<MachineLoopInfo>(); |
| 38 | AU.addPreserved<MachineLoopInfo>(); |
| 39 | MachineFunctionPass::getAnalysisUsage(AU); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | char LoopAligner::ID = 0; |
| 44 | } // end anonymous namespace |
| 45 | |
| 46 | FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); } |
| 47 | |
| 48 | bool LoopAligner::runOnMachineFunction(MachineFunction &MF) { |
| 49 | const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>(); |
| 50 | |
| 51 | if (MLI->begin() == MLI->end()) |
| 52 | return false; // No loops. |
| 53 | |
| 54 | unsigned Align = MF.getTarget().getTargetLowering()->getPrefLoopAlignment(); |
| 55 | if (!Align) |
| 56 | return false; // Don't care about loop alignment. |
| 57 | |
| 58 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 59 | MachineBasicBlock *MBB = I; |
| 60 | if (MLI->isLoopHeader(MBB)) |
| 61 | MBB->setAlignment(Align); |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |