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-10-15 00:36:22 +00:00
|
|
|
STATISTIC(NumLoopsAligned, "Number of loops aligned");
|
2009-05-08 06:34:09 +00:00
|
|
|
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;
|
|
|
|
|
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-10-17 00:32:43 +00:00
|
|
|
bool HasFallthrough(MachineBasicBlock *MBB);
|
|
|
|
bool HasAnalyzableTerminator(MachineBasicBlock *MBB);
|
|
|
|
void Splice(MachineFunction &MF,
|
|
|
|
MachineFunction::iterator InsertPt,
|
|
|
|
MachineFunction::iterator Begin,
|
|
|
|
MachineFunction::iterator End);
|
|
|
|
void UpdateTerminator(MachineBasicBlock *MBB);
|
|
|
|
bool OptimizeIntraLoopEdges(MachineFunction &MF);
|
|
|
|
bool OptimizeIntraLoopEdgesInLoop(MachineFunction &MF, MachineLoop *L);
|
2009-05-07 05:49:39 +00:00
|
|
|
bool AlignLoops(MachineFunction &MF);
|
2009-10-15 00:36:22 +00:00
|
|
|
bool AlignLoop(MachineFunction &MF, MachineLoop *L, unsigned Align);
|
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-10-17 00:32:43 +00:00
|
|
|
/// HasFallthrough - Test whether the given branch has a fallthrough, either as
|
|
|
|
/// a plain fallthrough or as a fallthrough case of a conditional branch.
|
2009-05-08 06:34:09 +00:00
|
|
|
///
|
2009-10-17 00:32:43 +00:00
|
|
|
bool CodePlacementOpt::HasFallthrough(MachineBasicBlock *MBB) {
|
|
|
|
MachineBasicBlock *TBB = 0, *FBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
|
|
|
|
return false;
|
|
|
|
// This conditional branch has no fallthrough.
|
|
|
|
if (FBB)
|
|
|
|
return false;
|
|
|
|
// An unconditional branch has no fallthrough.
|
|
|
|
if (Cond.empty() && TBB)
|
|
|
|
return false;
|
|
|
|
// It has a fallthrough.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HasAnalyzableTerminator - Test whether AnalyzeBranch will succeed on MBB.
|
|
|
|
/// This is called before major changes are begun to test whether it will be
|
|
|
|
/// possible to complete the changes.
|
2009-05-08 06:34:09 +00:00
|
|
|
///
|
2009-10-17 00:32:43 +00:00
|
|
|
/// Target-specific code is hereby encouraged to make AnalyzeBranch succeed
|
|
|
|
/// whenever possible.
|
2009-05-08 06:34:09 +00:00
|
|
|
///
|
2009-10-17 00:32:43 +00:00
|
|
|
bool CodePlacementOpt::HasAnalyzableTerminator(MachineBasicBlock *MBB) {
|
|
|
|
// Conservatively ignore EH landing pads.
|
|
|
|
if (MBB->isLandingPad()) return false;
|
|
|
|
|
|
|
|
// Ignore blocks which look like they might have EH-related control flow.
|
|
|
|
// At the time of this writing, there are blocks which AnalyzeBranch
|
|
|
|
// thinks end in single uncoditional branches, yet which have two CFG
|
|
|
|
// successors. Code in this file is not prepared to reason about such things.
|
|
|
|
if (!MBB->empty() && MBB->back().getOpcode() == TargetInstrInfo::EH_LABEL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Aggressively handle return blocks and similar constructs.
|
|
|
|
if (MBB->succ_empty()) return true;
|
|
|
|
|
|
|
|
// Ask the target's AnalyzeBranch if it can handle this block.
|
|
|
|
MachineBasicBlock *TBB = 0, *FBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
// Make the the terminator is understood.
|
|
|
|
if (TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
|
|
|
|
return false;
|
|
|
|
// Make sure we have the option of reversing the condition.
|
|
|
|
if (!Cond.empty() && TII->ReverseBranchCondition(Cond))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Splice - Move the sequence of instructions [Begin,End) to just before
|
|
|
|
/// InsertPt. Update branch instructions as needed to account for broken
|
|
|
|
/// fallthrough edges and to take advantage of newly exposed fallthrough
|
|
|
|
/// opportunities.
|
2009-05-08 06:34:09 +00:00
|
|
|
///
|
2009-10-17 00:32:43 +00:00
|
|
|
void CodePlacementOpt::Splice(MachineFunction &MF,
|
|
|
|
MachineFunction::iterator InsertPt,
|
|
|
|
MachineFunction::iterator Begin,
|
|
|
|
MachineFunction::iterator End) {
|
|
|
|
assert(Begin != MF.begin() && End != MF.begin() && InsertPt != MF.begin() &&
|
|
|
|
"Splice can't change the entry block!");
|
|
|
|
MachineFunction::iterator OldBeginPrior = prior(Begin);
|
|
|
|
MachineFunction::iterator OldEndPrior = prior(End);
|
|
|
|
|
|
|
|
MF.splice(InsertPt, Begin, End);
|
|
|
|
|
|
|
|
UpdateTerminator(prior(Begin));
|
|
|
|
UpdateTerminator(OldBeginPrior);
|
|
|
|
UpdateTerminator(OldEndPrior);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// UpdateTerminator - Update the terminator instructions in MBB to account
|
|
|
|
/// for changes to the layout. If the block previously used a fallthrough,
|
|
|
|
/// it may now need a branch, and if it previously used branching it may now
|
|
|
|
/// be able to use a fallthrough.
|
2009-05-08 06:34:09 +00:00
|
|
|
///
|
2009-10-17 00:32:43 +00:00
|
|
|
void CodePlacementOpt::UpdateTerminator(MachineBasicBlock *MBB) {
|
|
|
|
// A block with no successors has no concerns with fall-through edges.
|
|
|
|
if (MBB->succ_empty()) return;
|
|
|
|
|
|
|
|
MachineBasicBlock *TBB = 0, *FBB = 0;
|
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2009-10-17 23:15:04 +00:00
|
|
|
bool B = TII->AnalyzeBranch(*MBB, TBB, FBB, Cond);
|
|
|
|
(void) B;
|
|
|
|
assert(!B && "UpdateTerminators requires analyzable predecessors!");
|
2009-10-17 00:32:43 +00:00
|
|
|
if (Cond.empty()) {
|
|
|
|
if (TBB) {
|
|
|
|
// The block has an unconditional branch. If its successor is now
|
|
|
|
// its layout successor, delete the branch.
|
|
|
|
if (MBB->isLayoutSuccessor(TBB))
|
|
|
|
TII->RemoveBranch(*MBB);
|
|
|
|
} else {
|
|
|
|
// The block has an unconditional fallthrough. If its successor is not
|
|
|
|
// its layout successor, insert a branch.
|
|
|
|
TBB = *MBB->succ_begin();
|
|
|
|
if (!MBB->isLayoutSuccessor(TBB))
|
|
|
|
TII->InsertBranch(*MBB, TBB, 0, Cond);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (FBB) {
|
|
|
|
// The block has a non-fallthrough conditional branch. If one of its
|
|
|
|
// successors is its layout successor, rewrite it to a fallthrough
|
|
|
|
// conditional branch.
|
|
|
|
if (MBB->isLayoutSuccessor(TBB)) {
|
|
|
|
TII->RemoveBranch(*MBB);
|
|
|
|
TII->ReverseBranchCondition(Cond);
|
|
|
|
TII->InsertBranch(*MBB, FBB, 0, Cond);
|
|
|
|
} else if (MBB->isLayoutSuccessor(FBB)) {
|
|
|
|
TII->RemoveBranch(*MBB);
|
|
|
|
TII->InsertBranch(*MBB, TBB, 0, Cond);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The block has a fallthrough conditional branch.
|
|
|
|
MachineBasicBlock *MBBA = *MBB->succ_begin();
|
|
|
|
MachineBasicBlock *MBBB = *next(MBB->succ_begin());
|
|
|
|
if (MBBA == TBB) std::swap(MBBB, MBBA);
|
|
|
|
if (MBB->isLayoutSuccessor(TBB)) {
|
|
|
|
TII->RemoveBranch(*MBB);
|
|
|
|
TII->ReverseBranchCondition(Cond);
|
|
|
|
TII->InsertBranch(*MBB, MBBA, 0, Cond);
|
|
|
|
} else if (!MBB->isLayoutSuccessor(MBBA)) {
|
|
|
|
TII->RemoveBranch(*MBB);
|
|
|
|
TII->InsertBranch(*MBB, TBB, MBBA, Cond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// OptimizeIntraLoopEdges - Reposition loop blocks to minimize
|
|
|
|
/// intra-loop branching and to form contiguous loops.
|
2009-05-08 06:34:09 +00:00
|
|
|
///
|
2009-10-17 00:32:43 +00:00
|
|
|
bool CodePlacementOpt::OptimizeIntraLoopEdges(MachineFunction &MF) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2009-05-13 21:42:09 +00:00
|
|
|
if (!TLI->shouldOptimizeCodePlacement())
|
2009-10-17 00:32:43 +00:00
|
|
|
return Changed;
|
|
|
|
|
|
|
|
for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
|
|
|
|
I != E; ++I)
|
|
|
|
Changed |= OptimizeIntraLoopEdgesInLoop(MF, *I);
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2009-05-13 21:42:09 +00:00
|
|
|
|
2009-10-17 00:32:43 +00:00
|
|
|
/// OptimizeIntraLoopEdgesInLoop - Reposition loop blocks to minimize
|
|
|
|
/// intra-loop branching and to form contiguous loops.
|
|
|
|
///
|
|
|
|
/// This code takes the approach of making minor changes to the existing
|
|
|
|
/// layout to fix specific loop-oriented problems. Also, it depends on
|
|
|
|
/// AnalyzeBranch, which can't understand complex control instructions.
|
|
|
|
///
|
|
|
|
bool CodePlacementOpt::OptimizeIntraLoopEdgesInLoop(MachineFunction &MF,
|
|
|
|
MachineLoop *L) {
|
2009-05-08 06:34:09 +00:00
|
|
|
bool Changed = false;
|
2009-10-17 00:32:43 +00:00
|
|
|
|
|
|
|
// Do optimization for nested loops.
|
|
|
|
for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
|
|
|
|
Changed |= OptimizeIntraLoopEdgesInLoop(MF, *I);
|
|
|
|
|
|
|
|
// Keep a record of which blocks are in the portion of the loop contiguous
|
|
|
|
// with the loop header.
|
|
|
|
SmallPtrSet<MachineBasicBlock *, 8> ContiguousBlocks;
|
|
|
|
ContiguousBlocks.insert(L->getHeader());
|
|
|
|
|
|
|
|
// Find the loop "top", ignoring any discontiguous parts.
|
|
|
|
MachineBasicBlock *TopMBB = L->getHeader();
|
|
|
|
if (TopMBB != MF.begin()) {
|
|
|
|
MachineBasicBlock *PriorMBB = prior(MachineFunction::iterator(TopMBB));
|
|
|
|
while (L->contains(PriorMBB)) {
|
|
|
|
ContiguousBlocks.insert(PriorMBB);
|
|
|
|
TopMBB = PriorMBB;
|
|
|
|
if (TopMBB == MF.begin()) break;
|
|
|
|
PriorMBB = prior(MachineFunction::iterator(TopMBB));
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
2009-10-17 00:32:43 +00:00
|
|
|
}
|
2009-05-08 06:34:09 +00:00
|
|
|
|
2009-10-17 00:32:43 +00:00
|
|
|
// Find the loop "bottom", ignoring any discontiguous parts.
|
|
|
|
MachineBasicBlock *BotMBB = L->getHeader();
|
|
|
|
if (BotMBB != prior(MF.end())) {
|
|
|
|
MachineBasicBlock *NextMBB = next(MachineFunction::iterator(BotMBB));
|
|
|
|
while (L->contains(NextMBB)) {
|
|
|
|
ContiguousBlocks.insert(NextMBB);
|
|
|
|
BotMBB = NextMBB;
|
|
|
|
if (BotMBB == next(MachineFunction::iterator(BotMBB))) break;
|
|
|
|
NextMBB = next(MachineFunction::iterator(BotMBB));
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
2009-10-17 00:32:43 +00:00
|
|
|
}
|
2009-05-08 06:34:09 +00:00
|
|
|
|
2009-10-17 00:32:43 +00:00
|
|
|
// First, move blocks which unconditionally jump to the loop top to the
|
|
|
|
// top of the loop so that they have a fall through. This can introduce a
|
|
|
|
// branch on entry to the loop, but it can eliminate a branch within the
|
|
|
|
// loop. See the @simple case in test/CodeGen/X86/loop_blocks.ll for an
|
|
|
|
// example of this.
|
|
|
|
|
|
|
|
bool BotHasFallthrough = HasFallthrough(BotMBB);
|
|
|
|
|
|
|
|
if (TopMBB == MF.begin() ||
|
|
|
|
HasAnalyzableTerminator(prior(MachineFunction::iterator(TopMBB)))) {
|
|
|
|
new_top:
|
|
|
|
for (MachineBasicBlock::pred_iterator PI = TopMBB->pred_begin(),
|
|
|
|
PE = TopMBB->pred_end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *Pred = *PI;
|
|
|
|
if (Pred == TopMBB) continue;
|
|
|
|
if (HasFallthrough(Pred)) continue;
|
|
|
|
if (!L->contains(Pred)) continue;
|
|
|
|
|
|
|
|
// Verify that we can analyze all the loop entry edges before beginning
|
|
|
|
// any changes which will require us to be able to analyze them.
|
|
|
|
if (Pred == MF.begin())
|
|
|
|
continue;
|
|
|
|
if (!HasAnalyzableTerminator(Pred))
|
|
|
|
continue;
|
|
|
|
if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Pred))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Move the block.
|
|
|
|
Changed = true;
|
|
|
|
ContiguousBlocks.insert(Pred);
|
|
|
|
|
|
|
|
// Move it and all the blocks that can reach it via fallthrough edges
|
|
|
|
// exclusively, to keep existing fallthrough-edges intact.
|
|
|
|
MachineFunction::iterator Begin = Pred;
|
|
|
|
MachineFunction::iterator End = next(Begin);
|
|
|
|
while (Begin != MF.begin()) {
|
|
|
|
MachineFunction::iterator Prior = prior(Begin);
|
|
|
|
if (Prior == MF.begin())
|
|
|
|
break;
|
|
|
|
// Stop when a non-fallthrough edge is found.
|
|
|
|
if (!HasFallthrough(Prior))
|
|
|
|
break;
|
|
|
|
// Stop if a block which could fall-through out of the loop is found.
|
|
|
|
if (Prior->isSuccessor(End))
|
|
|
|
break;
|
|
|
|
// If we've reached the top, stop scanning.
|
|
|
|
if (Prior == MachineFunction::iterator(TopMBB)) {
|
|
|
|
// We know top currently has a fall through (because we just checked
|
|
|
|
// it) which would be lost if we do the transformation, so it isn't
|
|
|
|
// worthwhile to do the transformation unless it would expose a new
|
|
|
|
// fallthrough edge.
|
|
|
|
if (!Prior->isSuccessor(End))
|
|
|
|
goto next_pred;
|
|
|
|
// Otherwise we can stop scanning and procede to move the blocks.
|
2009-05-08 06:34:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-10-17 00:32:43 +00:00
|
|
|
// If we hit a switch or something complicated, don't move anything
|
|
|
|
// for this predecessor.
|
|
|
|
if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(Prior))))
|
|
|
|
break;
|
|
|
|
Begin = Prior;
|
|
|
|
ContiguousBlocks.insert(Begin);
|
|
|
|
++NumIntraMoved;
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
2009-10-17 00:32:43 +00:00
|
|
|
|
|
|
|
// Update BotMBB, before moving Begin/End around and forgetting where
|
|
|
|
// the new bottom is.
|
|
|
|
if (BotMBB == prior(End))
|
|
|
|
BotMBB = prior(Begin);
|
|
|
|
|
|
|
|
// Move the blocks.
|
|
|
|
Splice(MF, TopMBB, Begin, End);
|
|
|
|
|
|
|
|
// Update TopMBB, now that all the updates requiring the old top are
|
|
|
|
// complete.
|
|
|
|
TopMBB = Begin;
|
|
|
|
|
|
|
|
// We have a new loop top. Iterate on it. We shouldn't have to do this
|
|
|
|
// too many times if BranchFolding has done a reasonable job.
|
|
|
|
goto new_top;
|
|
|
|
next_pred:;
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
2009-10-17 00:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the loop previously didn't exit with a fall-through and it now does,
|
|
|
|
// we eliminated a branch.
|
|
|
|
if (!BotHasFallthrough && HasFallthrough(BotMBB)) {
|
|
|
|
++NumIntraElim;
|
|
|
|
BotHasFallthrough = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, move any loop blocks that are not in the portion of the loop
|
|
|
|
// contiguous with the header. This makes the loop contiguous, provided that
|
|
|
|
// AnalyzeBranch can handle all the relevant branching. See the @cfg_islands
|
|
|
|
// case in test/CodeGen/X86/loop_blocks.ll for an example of this.
|
|
|
|
|
|
|
|
// Determine a position to move orphaned loop blocks to. If TopMBB is not
|
|
|
|
// entered via fallthrough and BotMBB is exited via fallthrough, prepend them
|
|
|
|
// to the top of the loop to avoid loosing that fallthrough. Otherwise append
|
|
|
|
// them to the bottom, even if it previously had a fallthrough, on the theory
|
|
|
|
// that it's worth an extra branch to keep the loop contiguous.
|
|
|
|
MachineFunction::iterator InsertPt = next(MachineFunction::iterator(BotMBB));
|
|
|
|
bool InsertAtTop = false;
|
|
|
|
if (TopMBB != MF.begin() &&
|
|
|
|
!HasFallthrough(prior(MachineFunction::iterator(TopMBB))) &&
|
|
|
|
HasFallthrough(BotMBB)) {
|
|
|
|
InsertPt = TopMBB;
|
|
|
|
InsertAtTop = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find non-contigous blocks and fix them.
|
|
|
|
if (InsertPt != MF.begin() && HasAnalyzableTerminator(prior(InsertPt)))
|
|
|
|
for (MachineLoop::block_iterator BI = L->block_begin(), BE = L->block_end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
MachineBasicBlock *BB = *BI;
|
|
|
|
|
|
|
|
// Verify that we can analyze all the loop entry edges before beginning
|
|
|
|
// any changes which will require us to be able to analyze them.
|
|
|
|
if (!HasAnalyzableTerminator(BB))
|
|
|
|
continue;
|
|
|
|
if (!HasAnalyzableTerminator(prior(MachineFunction::iterator(BB))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the layout predecessor is part of the loop, this block will be
|
|
|
|
// processed along with it. This keeps them in their relative order.
|
|
|
|
if (BB != MF.begin() &&
|
|
|
|
L->contains(prior(MachineFunction::iterator(BB))))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check to see if this block is already contiguous with the main
|
|
|
|
// portion of the loop.
|
|
|
|
if (!ContiguousBlocks.insert(BB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Move the block.
|
|
|
|
Changed = true;
|
2009-05-08 06:34:09 +00:00
|
|
|
|
2009-10-17 00:32:43 +00:00
|
|
|
// Process this block and all loop blocks contiguous with it, to keep
|
|
|
|
// them in their relative order.
|
|
|
|
MachineFunction::iterator Begin = BB;
|
|
|
|
MachineFunction::iterator End = next(MachineFunction::iterator(BB));
|
|
|
|
for (; End != MF.end(); ++End) {
|
|
|
|
if (!L->contains(End)) break;
|
|
|
|
if (!HasAnalyzableTerminator(End)) break;
|
|
|
|
ContiguousBlocks.insert(End);
|
|
|
|
++NumIntraMoved;
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 00:32:43 +00:00
|
|
|
// Update BotMBB.
|
|
|
|
if (!InsertAtTop)
|
|
|
|
BotMBB = prior(End);
|
|
|
|
|
|
|
|
// If we're inserting at the bottom of the loop, and the code we're
|
|
|
|
// moving originally had fall-through successors, bring the sucessors
|
|
|
|
// up with the loop blocks to preserve the fall-through edges.
|
|
|
|
if (!InsertAtTop)
|
|
|
|
for (; End != MF.end(); ++End) {
|
|
|
|
if (L->contains(End)) break;
|
|
|
|
if (!HasAnalyzableTerminator(End)) break;
|
|
|
|
if (!HasFallthrough(prior(End))) break;
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
2009-10-17 00:32:43 +00:00
|
|
|
|
|
|
|
// Move the blocks.
|
|
|
|
Splice(MF, InsertPt, Begin, End);
|
|
|
|
|
|
|
|
// Update TopMBB.
|
|
|
|
if (InsertAtTop)
|
|
|
|
TopMBB = Begin;
|
2009-05-08 06:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
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-10-15 00:36:22 +00:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
|
|
|
|
I != E; ++I)
|
|
|
|
Changed |= AlignLoop(MF, *I, Align);
|
2008-10-01 23:18:38 +00:00
|
|
|
|
2009-10-15 00:36:22 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2009-10-17 00:32:43 +00:00
|
|
|
/// AlignLoop - Align loop headers to target preferred alignments.
|
|
|
|
///
|
2009-10-15 00:36:22 +00:00
|
|
|
bool CodePlacementOpt::AlignLoop(MachineFunction &MF, MachineLoop *L,
|
|
|
|
unsigned Align) {
|
2009-05-07 05:49:39 +00:00
|
|
|
bool Changed = false;
|
2009-10-15 00:36:22 +00:00
|
|
|
|
|
|
|
// Do alignment for nested loops.
|
|
|
|
for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I)
|
|
|
|
Changed |= AlignLoop(MF, *I, Align);
|
|
|
|
|
|
|
|
MachineBasicBlock *TopMBB = L->getHeader();
|
2009-10-17 00:32:43 +00:00
|
|
|
if (TopMBB != MF.begin()) {
|
|
|
|
MachineBasicBlock *PredMBB = prior(MachineFunction::iterator(TopMBB));
|
|
|
|
while (L->contains(PredMBB)) {
|
|
|
|
TopMBB = PredMBB;
|
|
|
|
if (TopMBB == MF.begin()) break;
|
|
|
|
PredMBB = prior(MachineFunction::iterator(TopMBB));
|
|
|
|
}
|
2008-02-28 00:43:03 +00:00
|
|
|
}
|
|
|
|
|
2009-10-15 00:36:22 +00:00
|
|
|
TopMBB->setAlignment(Align);
|
|
|
|
Changed = true;
|
|
|
|
++NumLoopsAligned;
|
|
|
|
|
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();
|
|
|
|
|
2009-10-17 00:32:43 +00:00
|
|
|
bool Changed = OptimizeIntraLoopEdges(MF);
|
2009-05-08 06:34:09 +00:00
|
|
|
|
2009-05-07 05:49:39 +00:00
|
|
|
Changed |= AlignLoops(MF);
|
|
|
|
|
|
|
|
return Changed;
|
2008-02-28 00:43:03 +00:00
|
|
|
}
|