2009-05-07 05:42:24 +00:00
|
|
|
//===-- CodePlacementOpt.cpp - Code Placement pass. -----------------------===//
|
2008-02-28 00:43:03 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-05-07 05:42:24 +00:00
|
|
|
// This file implements the pass that optimize code placement and align loop
|
|
|
|
// headers to target specific alignment boundary.
|
2008-02-28 00:43:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-07 05:42:24 +00:00
|
|
|
#define DEBUG_TYPE "code-placement"
|
2008-02-28 00:43:03 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-05-07 05:42:24 +00:00
|
|
|
class CodePlacementOpt : public MachineFunctionPass {
|
2008-02-28 00:43:03 +00:00
|
|
|
public:
|
|
|
|
static char ID;
|
2009-05-07 05:42:24 +00:00
|
|
|
CodePlacementOpt() : MachineFunctionPass(&ID) {}
|
2008-02-28 00:43:03 +00:00
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &MF);
|
2009-05-07 05:42:24 +00:00
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Code Placement Optimizater";
|
|
|
|
}
|
2008-02-28 00:43:03 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
2008-09-22 22:21:38 +00:00
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2008-02-28 00:43:03 +00:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-07 05:42:24 +00:00
|
|
|
char CodePlacementOpt::ID = 0;
|
2008-02-28 00:43:03 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-05-07 05:42:24 +00:00
|
|
|
FunctionPass *llvm::createCodePlacementOptPass() {
|
|
|
|
return new CodePlacementOpt();
|
|
|
|
}
|
2008-02-28 00:43:03 +00:00
|
|
|
|
2009-05-07 05:42:24 +00:00
|
|
|
bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
|
2008-02-28 00:43:03 +00:00
|
|
|
const MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfo>();
|
|
|
|
|
2008-08-14 18:13:49 +00:00
|
|
|
if (MLI->empty())
|
2008-02-28 00:43:03 +00:00
|
|
|
return false; // No loops.
|
|
|
|
|
2008-02-29 17:52:15 +00:00
|
|
|
const TargetLowering *TLI = MF.getTarget().getTargetLowering();
|
|
|
|
if (!TLI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned Align = TLI->getPrefLoopAlignment();
|
2008-02-28 00:43:03 +00:00
|
|
|
if (!Align)
|
|
|
|
return false; // Don't care about loop alignment.
|
|
|
|
|
2008-10-01 23:18:38 +00:00
|
|
|
const Function *F = MF.getFunction();
|
2008-10-06 17:30:07 +00:00
|
|
|
if (F->hasFnAttr(Attribute::OptimizeForSize))
|
2008-10-01 23:18:38 +00:00
|
|
|
return false;
|
|
|
|
|
2008-02-28 00:43:03 +00:00
|
|
|
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock *MBB = I;
|
2008-11-27 01:16:00 +00:00
|
|
|
if (MLI->isLoopHeader(MBB)) {
|
|
|
|
MachineBasicBlock *PredBB = prior(I);
|
|
|
|
if (MLI->getLoopFor(MBB) == MLI->getLoopFor(PredBB))
|
|
|
|
// If previously BB is in the same loop, don't align this BB. We want
|
|
|
|
// to prevent adding noop's inside a loop.
|
|
|
|
continue;
|
2008-02-28 00:43:03 +00:00
|
|
|
MBB->setAlignment(Align);
|
2008-11-27 01:16:00 +00:00
|
|
|
}
|
2008-02-28 00:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|