2002-05-10 22:44:58 +00:00
|
|
|
//===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
|
|
|
|
//
|
|
|
|
// This pass is a simple loop invariant code motion pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2002-08-22 21:39:55 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2002-09-29 21:46:09 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2002-05-10 22:44:58 +00:00
|
|
|
#include "llvm/iOperators.h"
|
2002-08-22 21:39:55 +00:00
|
|
|
#include "llvm/iMemory.h"
|
2002-05-10 22:44:58 +00:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
|
|
|
#include "Support/STLExtras.h"
|
|
|
|
#include "Support/StatisticReporter.h"
|
|
|
|
#include <algorithm>
|
2002-06-25 21:07:58 +00:00
|
|
|
using std::string;
|
2002-05-10 22:44:58 +00:00
|
|
|
|
|
|
|
namespace {
|
2002-09-26 16:38:03 +00:00
|
|
|
Statistic<>NumHoisted("licm\t\t- Number of instructions hoisted out of loop");
|
|
|
|
Statistic<> NumHoistedLoads("licm\t\t- Number of load insts hoisted");
|
|
|
|
|
2002-05-10 22:44:58 +00:00
|
|
|
struct LICM : public FunctionPass, public InstVisitor<LICM> {
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-05-10 22:44:58 +00:00
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// This transformation requires natural loop information & requires that
|
|
|
|
/// loop preheaders be inserted into the CFG...
|
|
|
|
///
|
2002-05-10 22:44:58 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.preservesCFG();
|
2002-09-26 16:19:31 +00:00
|
|
|
AU.addRequiredID(LoopPreheadersID);
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<LoopInfo>();
|
2002-09-29 21:46:09 +00:00
|
|
|
AU.addRequired<DominatorTree>();
|
2002-08-22 21:39:55 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2002-05-10 22:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2002-09-26 19:40:25 +00:00
|
|
|
Loop *CurLoop; // The current loop we are working on...
|
|
|
|
BasicBlock *Preheader; // The preheader block of the current loop...
|
|
|
|
bool Changed; // Set to true when we change anything.
|
|
|
|
AliasAnalysis *AA; // Currently AliasAnalysis information
|
2002-05-10 22:44:58 +00:00
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// visitLoop - Hoist expressions out of the specified loop...
|
|
|
|
///
|
2002-05-10 22:44:58 +00:00
|
|
|
void visitLoop(Loop *L);
|
|
|
|
|
2002-09-29 21:46:09 +00:00
|
|
|
/// HoistRegion - Walk the specified region of the CFG (defined by all
|
|
|
|
/// blocks dominated by the specified block, and that are in the current
|
|
|
|
/// loop) in depth first order w.r.t the DominatorTree. This allows us to
|
|
|
|
/// visit defintions before uses, allowing us to hoist a loop body in one
|
|
|
|
/// pass without iteration.
|
|
|
|
///
|
|
|
|
void HoistRegion(DominatorTree::Node *N);
|
|
|
|
|
2002-09-26 19:40:25 +00:00
|
|
|
/// inCurrentLoop - Little predicate that returns false if the specified
|
2002-09-26 16:52:07 +00:00
|
|
|
/// basic block is in a subloop of the current one, not the current one
|
|
|
|
/// itself.
|
|
|
|
///
|
2002-09-26 19:40:25 +00:00
|
|
|
bool inCurrentLoop(BasicBlock *BB) {
|
2002-05-10 22:44:58 +00:00
|
|
|
for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
|
|
|
|
if (CurLoop->getSubLoops()[i]->contains(BB))
|
2002-09-26 19:40:25 +00:00
|
|
|
return false; // A subloop actually contains this block!
|
|
|
|
return true;
|
2002-05-10 22:44:58 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// hoist - When an instruction is found to only use loop invariant operands
|
|
|
|
/// that is safe to hoist, this instruction is called to do the dirty work.
|
|
|
|
///
|
2002-06-25 16:13:24 +00:00
|
|
|
void hoist(Instruction &I);
|
2002-05-10 22:44:58 +00:00
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// pointerInvalidatedByLoop - Return true if the body of this loop may
|
|
|
|
/// store into the memory location pointed to by V.
|
|
|
|
///
|
2002-08-22 21:39:55 +00:00
|
|
|
bool pointerInvalidatedByLoop(Value *V);
|
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// isLoopInvariant - Return true if the specified value is loop invariant
|
|
|
|
///
|
2002-05-10 22:44:58 +00:00
|
|
|
inline bool isLoopInvariant(Value *V) {
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
return !CurLoop->contains(I->getParent());
|
|
|
|
return true; // All non-instructions are loop invariant
|
|
|
|
}
|
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// Instruction visitation handlers... these basically control whether or
|
|
|
|
/// not the specified instruction types are hoisted.
|
|
|
|
///
|
2002-05-10 22:44:58 +00:00
|
|
|
friend class InstVisitor<LICM>;
|
2002-06-25 16:13:24 +00:00
|
|
|
void visitBinaryOperator(Instruction &I) {
|
|
|
|
if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
|
2002-05-10 22:44:58 +00:00
|
|
|
hoist(I);
|
|
|
|
}
|
2002-08-14 18:22:19 +00:00
|
|
|
void visitCastInst(CastInst &CI) {
|
|
|
|
Instruction &I = (Instruction&)CI;
|
|
|
|
if (isLoopInvariant(I.getOperand(0))) hoist(I);
|
2002-08-14 18:18:02 +00:00
|
|
|
}
|
2002-06-25 16:13:24 +00:00
|
|
|
void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
|
2002-05-10 22:44:58 +00:00
|
|
|
|
2002-09-26 16:19:31 +00:00
|
|
|
void visitLoadInst(LoadInst &LI);
|
2002-08-22 21:39:55 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
|
|
|
|
Instruction &I = (Instruction&)GEPI;
|
|
|
|
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
|
|
|
|
if (!isLoopInvariant(I.getOperand(i))) return;
|
2002-05-10 22:44:58 +00:00
|
|
|
hoist(I);
|
|
|
|
}
|
|
|
|
};
|
2002-07-23 18:06:35 +00:00
|
|
|
|
2002-07-26 21:12:46 +00:00
|
|
|
RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
|
2002-05-10 22:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createLICMPass() { return new LICM(); }
|
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// runOnFunction - For LICM, this simply traverses the loop structure of the
|
|
|
|
/// function, hoisting expressions out of loops if possible.
|
|
|
|
///
|
2002-06-25 16:13:24 +00:00
|
|
|
bool LICM::runOnFunction(Function &) {
|
2002-09-26 19:40:25 +00:00
|
|
|
// Get information about the top level loops in the function...
|
2002-05-10 22:44:58 +00:00
|
|
|
const std::vector<Loop*> &TopLevelLoops =
|
|
|
|
getAnalysis<LoopInfo>().getTopLevelLoops();
|
|
|
|
|
2002-08-22 21:39:55 +00:00
|
|
|
// Get our alias analysis information...
|
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
|
|
|
|
2002-05-10 22:44:58 +00:00
|
|
|
// Traverse loops in postorder, hoisting expressions out of the deepest loops
|
|
|
|
// first.
|
|
|
|
//
|
|
|
|
Changed = false;
|
|
|
|
std::for_each(TopLevelLoops.begin(), TopLevelLoops.end(),
|
|
|
|
bind_obj(this, &LICM::visitLoop));
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
|
|
|
|
/// visitLoop - Hoist expressions out of the specified loop...
|
|
|
|
///
|
2002-05-10 22:44:58 +00:00
|
|
|
void LICM::visitLoop(Loop *L) {
|
|
|
|
// Recurse through all subloops before we process this loop...
|
|
|
|
std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(),
|
|
|
|
bind_obj(this, &LICM::visitLoop));
|
|
|
|
CurLoop = L;
|
|
|
|
|
2002-09-26 19:40:25 +00:00
|
|
|
// Get the preheader block to move instructions into...
|
|
|
|
Preheader = L->getLoopPreheader();
|
|
|
|
assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
|
|
|
|
|
2002-05-10 22:44:58 +00:00
|
|
|
// We want to visit all of the instructions in this loop... that are not parts
|
|
|
|
// of our subloops (they have already had their invariants hoisted out of
|
|
|
|
// their loop, into this loop, so there is no need to process the BODIES of
|
|
|
|
// the subloops).
|
|
|
|
//
|
2002-09-29 21:46:09 +00:00
|
|
|
// Traverse the body of the loop in depth first order on the dominator tree so
|
|
|
|
// that we are guaranteed to see definitions before we see uses. This allows
|
|
|
|
// us to perform the LICM transformation in one pass, without iteration.
|
|
|
|
//
|
|
|
|
HoistRegion(getAnalysis<DominatorTree>()[L->getHeader()]);
|
2002-05-10 22:44:58 +00:00
|
|
|
|
|
|
|
// Clear out loops state information for the next iteration
|
|
|
|
CurLoop = 0;
|
2002-09-26 19:40:25 +00:00
|
|
|
Preheader = 0;
|
2002-05-10 22:44:58 +00:00
|
|
|
}
|
|
|
|
|
2002-09-29 21:46:09 +00:00
|
|
|
/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
|
|
|
|
/// dominated by the specified block, and that are in the current loop) in depth
|
|
|
|
/// first order w.r.t the DominatorTree. This allows us to visit defintions
|
|
|
|
/// before uses, allowing us to hoist a loop body in one pass without iteration.
|
|
|
|
///
|
|
|
|
void LICM::HoistRegion(DominatorTree::Node *N) {
|
|
|
|
assert(N != 0 && "Null dominator tree node?");
|
|
|
|
|
|
|
|
// This subregion is not in the loop, it has already been already been hoisted
|
|
|
|
if (!inCurrentLoop(N->getNode()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
visit(*N->getNode());
|
|
|
|
|
|
|
|
const std::vector<DominatorTree::Node*> &Children = N->getChildren();
|
|
|
|
for (unsigned i = 0, e = Children.size(); i != e; ++i)
|
|
|
|
HoistRegion(Children[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// hoist - When an instruction is found to only use loop invariant operands
|
|
|
|
/// that is safe to hoist, this instruction is called to do the dirty work.
|
|
|
|
///
|
2002-06-25 16:13:24 +00:00
|
|
|
void LICM::hoist(Instruction &Inst) {
|
2002-09-29 21:46:09 +00:00
|
|
|
DEBUG(std::cerr << "LICM hoisting: " << Inst);
|
2002-05-10 22:44:58 +00:00
|
|
|
|
|
|
|
BasicBlock *Header = CurLoop->getHeader();
|
|
|
|
|
2002-09-26 19:40:25 +00:00
|
|
|
// Remove the instruction from its current basic block... but don't delete the
|
|
|
|
// instruction.
|
|
|
|
Inst.getParent()->getInstList().remove(&Inst);
|
2002-05-10 22:44:58 +00:00
|
|
|
|
2002-09-26 16:38:03 +00:00
|
|
|
// Insert the new node in Preheader, before the terminator.
|
2002-09-26 19:40:25 +00:00
|
|
|
Preheader->getInstList().insert(Preheader->getTerminator(), &Inst);
|
2002-09-26 16:38:03 +00:00
|
|
|
|
|
|
|
++NumHoisted;
|
2002-05-10 22:44:58 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2002-09-26 16:19:31 +00:00
|
|
|
|
|
|
|
void LICM::visitLoadInst(LoadInst &LI) {
|
|
|
|
if (isLoopInvariant(LI.getOperand(0)) &&
|
2002-09-26 19:40:25 +00:00
|
|
|
!pointerInvalidatedByLoop(LI.getOperand(0))) {
|
2002-09-26 16:19:31 +00:00
|
|
|
hoist(LI);
|
2002-09-26 19:40:25 +00:00
|
|
|
++NumHoistedLoads;
|
|
|
|
}
|
2002-09-26 16:19:31 +00:00
|
|
|
}
|
|
|
|
|
2002-09-26 16:52:07 +00:00
|
|
|
/// pointerInvalidatedByLoop - Return true if the body of this loop may store
|
|
|
|
/// into the memory location pointed to by V.
|
|
|
|
///
|
2002-08-22 21:39:55 +00:00
|
|
|
bool LICM::pointerInvalidatedByLoop(Value *V) {
|
|
|
|
// Check to see if any of the basic blocks in CurLoop invalidate V.
|
|
|
|
for (unsigned i = 0, e = CurLoop->getBlocks().size(); i != e; ++i)
|
|
|
|
if (AA->canBasicBlockModify(*CurLoop->getBlocks()[i], V))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|