blob: c5a3f549a76535826eb2e1a2ae77b7baf36d3c81 [file] [log] [blame]
Evan Chengfb8075d2008-02-28 00:43:03 +00001//===-- 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 Chengd703ed62008-02-28 23:29:57 +000021#include "llvm/Support/CommandLine.h"
Evan Chengfb8075d2008-02-28 00:43:03 +000022#include "llvm/Support/Compiler.h"
23#include "llvm/Support/Debug.h"
24using namespace llvm;
25
26namespace {
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
47FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); }
48
49bool 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}