blob: 44f5bb461dc0650927e49d7decf45c18a3de8981 [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 Chengfb8075d2008-02-28 00:43:03 +000021#include "llvm/Support/Compiler.h"
22#include "llvm/Support/Debug.h"
23using namespace llvm;
24
25namespace {
26 class LoopAligner : public MachineFunctionPass {
Evan Chengfb8075d2008-02-28 00:43:03 +000027 public:
28 static char ID;
29 LoopAligner() : MachineFunctionPass((intptr_t)&ID) {}
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>();
37 MachineFunctionPass::getAnalysisUsage(AU);
38 }
39 };
40
41 char LoopAligner::ID = 0;
42} // end anonymous namespace
43
44FunctionPass *llvm::createLoopAlignerPass() { return new LoopAligner(); }
45
46bool LoopAligner::runOnMachineFunction(MachineFunction &MF) {
47 const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>();
48
Dan Gohmana8c763b2008-08-14 18:13:49 +000049 if (MLI->empty())
Evan Chengfb8075d2008-02-28 00:43:03 +000050 return false; // No loops.
51
Evan Cheng4f658e92008-02-29 17:52:15 +000052 const TargetLowering *TLI = MF.getTarget().getTargetLowering();
53 if (!TLI)
54 return false;
55
56 unsigned Align = TLI->getPrefLoopAlignment();
Evan Chengfb8075d2008-02-28 00:43:03 +000057 if (!Align)
58 return false; // Don't care about loop alignment.
59
60 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
61 MachineBasicBlock *MBB = I;
62 if (MLI->isLoopHeader(MBB))
63 MBB->setAlignment(Align);
64 }
65
66 return true;
67}