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"
|
2009-05-08 06:34:09 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2008-02-28 00:43:03 +00:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-05-08 06:34:09 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2008-02-28 00:43:03 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-05-08 06:34:09 +00:00
|
|
|
STATISTIC(NumHeaderAligned, "Number of loop header aligned");
|
|
|
|
STATISTIC(NumIntraElim, "Number of intra loop branches eliminated");
|
|
|
|
STATISTIC(NumIntraMoved, "Number of intra loop branches moved");
|
|
|
|
|
2008-02-28 00:43:03 +00:00
|
|
|
namespace {
|
2009-05-07 05:42:24 +00:00
|
|
|
class CodePlacementOpt : public MachineFunctionPass {
|
2009-05-07 05:49:39 +00:00
|
|
|
const MachineLoopInfo *MLI;
|
2009-05-08 06:34:09 +00:00
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const TargetLowering *TLI;
|
|
|
|
|
|
|
|
/// ChangedMBBs - BBs which are modified by OptimizeIntraLoopEdges.
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> ChangedMBBs;
|
|
|
|
|
|
|
|
/// UncondJmpMBBs - A list of BBs which are in loops and end with
|
|
|
|
/// unconditional branches.
|
|
|
|
SmallVector<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 4>
|
|
|
|
UncondJmpMBBs;
|
|
|
|
|
|
|
|
/// LoopHeaders - A list of BBs which are loop headers.
|
|
|
|
SmallVector<MachineBasicBlock*, 4> LoopHeaders;
|
2009-05-07 05:49:39 +00:00
|
|
|
|
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>();
|
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:49:39 +00:00
|
|
|
|
|
|
|
private:
|
2009-05-08 06:34:09 +00:00
|
|
|
bool OptimizeIntraLoopEdges();
|
2009-05-12 23:58:14 +00:00
|
|
|
bool HeaderShouldBeAligned(MachineBasicBlock *MBB, MachineLoop *L,
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 4> &DoNotAlign);
|
2009-05-07 05:49:39 +00:00
|
|
|
bool AlignLoops(MachineFunction &MF);
|
2008-02-28 00:43:03 +00:00
|
|
|
};
|
|
|
|
|
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-08 06:34:09 +00:00
|
|
|
/// OptimizeBackEdges - Place loop back edges to move unconditional branches
|
|
|
|
/// out of the loop.
|
|
|
|
///
|
|
|
|
/// A:
|
|
|
|
/// ...
|
|
|
|
/// <fallthrough to B>
|
|
|
|
///
|
|
|
|
/// B: --> loop header
|
|
|
|
/// ...
|
|
|
|
/// jcc <cond> C, [exit]
|
|
|
|
///
|
|
|
|
/// C:
|
|
|
|
/// ...
|
|
|
|
/// jmp B
|
|
|
|
///
|
|
|
|
/// ==>
|
|
|
|
///
|
|
|
|
/// A:
|
|
|
|
/// ...
|
|
|
|
/// jmp B
|
|
|
|
///
|
|
|
|
/// C: --> new loop header
|
|
|
|
/// ...
|
|
|
|
/// <fallthough to B>
|
|
|
|
///
|
|
|
|
/// B:
|
|
|
|
/// ...
|
|
|
|
/// jcc <cond> C, [exit]
|
|
|
|
///
|
|
|
|
bool CodePlacementOpt::OptimizeIntraLoopEdges() {
|
2009-05-13 21:42:09 +00:00
|
|
|
if (!TLI->shouldOptimizeCodePlacement())
|
|
|
|
return false;
|
|
|
|
|
2009-05-08 06:34:09 +00:00
|
|
|
bool Changed = false;
|
|
|
|
for (unsigned i = 0, e = UncondJmpMBBs.size(); i != e; ++i) {
|
|
|
|
MachineBasicBlock *MBB = UncondJmpMBBs[i].first;
|
|
|
|
MachineBasicBlock *SuccMBB = UncondJmpMBBs[i].second;
|
|
|
|
MachineLoop *L = MLI->getLoopFor(MBB);
|
|
|
|
assert(L && "BB is expected to be in a loop!");
|
|
|
|
|
|
|
|
if (ChangedMBBs.count(MBB)) {
|
|
|
|
// BB has been modified, re-analyze.
|
|
|
|
MachineBasicBlock *TBB = 0, *FBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond) || !Cond.empty())
|
|
|
|
continue;
|
|
|
|
if (MLI->getLoopFor(TBB) != L || TBB->isLandingPad())
|
|
|
|
continue;
|
|
|
|
SuccMBB = TBB;
|
|
|
|
} else {
|
|
|
|
assert(MLI->getLoopFor(SuccMBB) == L &&
|
|
|
|
"Successor is not in the same loop!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MBB->isLayoutSuccessor(SuccMBB)) {
|
|
|
|
// Successor is right after MBB, just eliminate the unconditional jmp.
|
|
|
|
// Can this happen?
|
|
|
|
TII->RemoveBranch(*MBB);
|
|
|
|
ChangedMBBs.insert(MBB);
|
|
|
|
++NumIntraElim;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now check if the predecessor is fallthrough from any BB. If there is,
|
|
|
|
// that BB should be from outside the loop since edge will become a jmp.
|
|
|
|
bool OkToMove = true;
|
|
|
|
MachineBasicBlock *FtMBB = 0, *FtTBB = 0, *FtFBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> FtCond;
|
|
|
|
for (MachineBasicBlock::pred_iterator PI = SuccMBB->pred_begin(),
|
|
|
|
PE = SuccMBB->pred_end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *PredMBB = *PI;
|
|
|
|
if (PredMBB->isLayoutSuccessor(SuccMBB)) {
|
|
|
|
if (TII->AnalyzeBranch(*PredMBB, FtTBB, FtFBB, FtCond)) {
|
|
|
|
OkToMove = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!FtTBB)
|
|
|
|
FtTBB = SuccMBB;
|
|
|
|
else if (!FtFBB) {
|
|
|
|
assert(FtFBB != SuccMBB && "Unexpected control flow!");
|
|
|
|
FtFBB = SuccMBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A fallthrough.
|
|
|
|
FtMBB = PredMBB;
|
|
|
|
MachineLoop *PL = MLI->getLoopFor(PredMBB);
|
2009-05-12 03:48:10 +00:00
|
|
|
if (PL && (PL == L || PL->getLoopDepth() >= L->getLoopDepth()))
|
2009-05-08 06:34:09 +00:00
|
|
|
OkToMove = false;
|
2009-05-12 03:48:10 +00:00
|
|
|
|
|
|
|
break;
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!OkToMove)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Is it profitable? If SuccMBB can fallthrough itself, that can be changed
|
|
|
|
// into a jmp.
|
|
|
|
MachineBasicBlock *TBB = 0, *FBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
if (TII->AnalyzeBranch(*SuccMBB, TBB, FBB, Cond))
|
|
|
|
continue;
|
|
|
|
if (!TBB && Cond.empty())
|
|
|
|
TBB = next(MachineFunction::iterator(SuccMBB));
|
|
|
|
else if (!FBB && !Cond.empty())
|
|
|
|
FBB = next(MachineFunction::iterator(SuccMBB));
|
|
|
|
|
|
|
|
// This calculate the cost of the transformation. Also, it finds the *only*
|
|
|
|
// intra-loop edge if there is one.
|
|
|
|
int Cost = 0;
|
|
|
|
bool HasOneIntraSucc = true;
|
|
|
|
MachineBasicBlock *IntraSucc = 0;
|
|
|
|
for (MachineBasicBlock::succ_iterator SI = SuccMBB->succ_begin(),
|
|
|
|
SE = SuccMBB->succ_end(); SI != SE; ++SI) {
|
|
|
|
MachineBasicBlock *SSMBB = *SI;
|
|
|
|
if (MLI->getLoopFor(SSMBB) == L) {
|
|
|
|
if (!IntraSucc)
|
|
|
|
IntraSucc = SSMBB;
|
|
|
|
else
|
|
|
|
HasOneIntraSucc = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SuccMBB->isLayoutSuccessor(SSMBB))
|
|
|
|
// This will become a jmp.
|
|
|
|
++Cost;
|
2009-05-08 06:57:41 +00:00
|
|
|
else if (MBB->isLayoutSuccessor(SSMBB)) {
|
2009-05-08 06:34:09 +00:00
|
|
|
// One of the successor will become the new fallthrough.
|
|
|
|
if (SSMBB == FBB) {
|
|
|
|
FBB = 0;
|
|
|
|
--Cost;
|
|
|
|
} else if (!FBB && SSMBB == TBB && Cond.empty()) {
|
|
|
|
TBB = 0;
|
|
|
|
--Cost;
|
2009-05-08 09:35:53 +00:00
|
|
|
} else if (!Cond.empty() && !TII->ReverseBranchCondition(Cond)) {
|
|
|
|
assert(SSMBB == TBB);
|
2009-05-08 06:34:09 +00:00
|
|
|
TBB = FBB;
|
|
|
|
FBB = 0;
|
|
|
|
--Cost;
|
|
|
|
}
|
2009-05-08 06:57:41 +00:00
|
|
|
}
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
|
|
|
if (Cost)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Now, let's move the successor to below the BB to eliminate the jmp.
|
|
|
|
SuccMBB->moveAfter(MBB);
|
|
|
|
TII->RemoveBranch(*MBB);
|
|
|
|
TII->RemoveBranch(*SuccMBB);
|
|
|
|
if (TBB)
|
|
|
|
TII->InsertBranch(*SuccMBB, TBB, FBB, Cond);
|
|
|
|
ChangedMBBs.insert(MBB);
|
|
|
|
ChangedMBBs.insert(SuccMBB);
|
|
|
|
if (FtMBB) {
|
|
|
|
TII->RemoveBranch(*FtMBB);
|
|
|
|
TII->InsertBranch(*FtMBB, FtTBB, FtFBB, FtCond);
|
|
|
|
ChangedMBBs.insert(FtMBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If BB is the loop latch, we may have a new loop headr.
|
|
|
|
if (MBB == L->getLoopLatch()) {
|
|
|
|
assert(MLI->isLoopHeader(SuccMBB) &&
|
|
|
|
"Only succ of loop latch is not the header?");
|
|
|
|
if (HasOneIntraSucc && IntraSucc)
|
|
|
|
std::replace(LoopHeaders.begin(),LoopHeaders.end(), SuccMBB, IntraSucc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++NumIntraMoved;
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2009-05-08 19:01:44 +00:00
|
|
|
/// HeaderShouldBeAligned - Return true if the specified loop header block
|
|
|
|
/// should be aligned. For now, we will not align it if all the predcessors
|
|
|
|
/// (i.e. loop back edges) are laid out above the header. FIXME: Do not
|
|
|
|
/// align small loops.
|
2009-05-12 23:58:14 +00:00
|
|
|
bool
|
|
|
|
CodePlacementOpt::HeaderShouldBeAligned(MachineBasicBlock *MBB, MachineLoop *L,
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 4> &DoNotAlign) {
|
|
|
|
if (DoNotAlign.count(MBB))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool BackEdgeBelow = false;
|
2009-05-08 19:01:44 +00:00
|
|
|
for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
|
|
|
|
PE = MBB->pred_end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *PredMBB = *PI;
|
2009-05-12 23:58:14 +00:00
|
|
|
if (PredMBB == MBB || PredMBB->getNumber() > MBB->getNumber()) {
|
|
|
|
BackEdgeBelow = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BackEdgeBelow)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Ok, we are going to align this loop header. If it's an inner loop,
|
|
|
|
// do not align its outer loop.
|
|
|
|
MachineBasicBlock *PreHeader = L->getLoopPreheader();
|
|
|
|
if (PreHeader) {
|
|
|
|
MachineLoop *L = MLI->getLoopFor(PreHeader);
|
|
|
|
if (L) {
|
|
|
|
MachineBasicBlock *HeaderBlock = L->getHeader();
|
|
|
|
HeaderBlock->setAlignment(0);
|
|
|
|
DoNotAlign.insert(HeaderBlock);
|
|
|
|
}
|
2009-05-08 19:01:44 +00:00
|
|
|
}
|
2009-05-12 23:58:14 +00:00
|
|
|
return true;
|
2009-05-08 19:01:44 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 05:49:39 +00:00
|
|
|
/// AlignLoops - Align loop headers to target preferred alignments.
|
|
|
|
///
|
|
|
|
bool CodePlacementOpt::AlignLoops(MachineFunction &MF) {
|
2009-05-08 06:34:09 +00:00
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
if (F->hasFnAttr(Attribute::OptimizeForSize))
|
2008-02-29 17:52:15 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned Align = TLI->getPrefLoopAlignment();
|
2008-02-28 00:43:03 +00:00
|
|
|
if (!Align)
|
|
|
|
return false; // Don't care about loop alignment.
|
|
|
|
|
2009-05-08 06:34:09 +00:00
|
|
|
// Make sure blocks are numbered in order
|
|
|
|
MF.RenumberBlocks();
|
2008-10-01 23:18:38 +00:00
|
|
|
|
2009-05-07 05:49:39 +00:00
|
|
|
bool Changed = false;
|
2009-05-12 23:58:14 +00:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 4> DoNotAlign;
|
2009-05-08 06:34:09 +00:00
|
|
|
for (unsigned i = 0, e = LoopHeaders.size(); i != e; ++i) {
|
|
|
|
MachineBasicBlock *HeaderMBB = LoopHeaders[i];
|
|
|
|
MachineBasicBlock *PredMBB = prior(MachineFunction::iterator(HeaderMBB));
|
2009-05-12 23:58:14 +00:00
|
|
|
MachineLoop *L = MLI->getLoopFor(HeaderMBB);
|
|
|
|
if (L == MLI->getLoopFor(PredMBB))
|
2009-05-08 06:34:09 +00:00
|
|
|
// If previously BB is in the same loop, don't align this BB. We want
|
|
|
|
// to prevent adding noop's inside a loop.
|
2009-05-08 19:01:44 +00:00
|
|
|
continue;
|
2009-05-12 23:58:14 +00:00
|
|
|
if (HeaderShouldBeAligned(HeaderMBB, L, DoNotAlign)) {
|
2009-05-08 06:34:09 +00:00
|
|
|
HeaderMBB->setAlignment(Align);
|
2009-05-07 05:49:39 +00:00
|
|
|
Changed = true;
|
2009-05-08 06:34:09 +00:00
|
|
|
++NumHeaderAligned;
|
2008-11-27 01:16:00 +00:00
|
|
|
}
|
2008-02-28 00:43:03 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 05:49:39 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CodePlacementOpt::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
MLI = &getAnalysis<MachineLoopInfo>();
|
|
|
|
if (MLI->empty())
|
|
|
|
return false; // No loops.
|
|
|
|
|
2009-05-08 06:34:09 +00:00
|
|
|
TLI = MF.getTarget().getTargetLowering();
|
|
|
|
TII = MF.getTarget().getInstrInfo();
|
|
|
|
|
|
|
|
// Analyze the BBs first and keep track of loop headers and BBs that
|
|
|
|
// end with an unconditional jmp to another block in the same loop.
|
|
|
|
for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock *MBB = I;
|
|
|
|
if (MBB->isLandingPad())
|
|
|
|
continue;
|
|
|
|
MachineLoop *L = MLI->getLoopFor(MBB);
|
|
|
|
if (!L)
|
|
|
|
continue;
|
|
|
|
if (MLI->isLoopHeader(MBB))
|
|
|
|
LoopHeaders.push_back(MBB);
|
|
|
|
|
|
|
|
MachineBasicBlock *TBB = 0, *FBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond) || !Cond.empty())
|
|
|
|
continue;
|
|
|
|
if (MLI->getLoopFor(TBB) == L && !TBB->isLandingPad())
|
|
|
|
UncondJmpMBBs.push_back(std::make_pair(MBB, TBB));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Changed = OptimizeIntraLoopEdges();
|
|
|
|
|
2009-05-07 05:49:39 +00:00
|
|
|
Changed |= AlignLoops(MF);
|
|
|
|
|
2009-05-08 06:34:09 +00:00
|
|
|
ChangedMBBs.clear();
|
|
|
|
UncondJmpMBBs.clear();
|
|
|
|
LoopHeaders.clear();
|
|
|
|
|
2009-05-07 05:49:39 +00:00
|
|
|
return Changed;
|
2008-02-28 00:43:03 +00:00
|
|
|
}
|