2002-05-11 06:44:58 +08: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-23 05:39:55 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2002-05-11 06:44:58 +08:00
|
|
|
#include "llvm/iOperators.h"
|
2002-08-23 05:39:55 +08:00
|
|
|
#include "llvm/iMemory.h"
|
2002-05-11 06:44:58 +08:00
|
|
|
#include "llvm/Support/InstVisitor.h"
|
|
|
|
#include "Support/STLExtras.h"
|
|
|
|
#include "Support/StatisticReporter.h"
|
|
|
|
#include <algorithm>
|
2002-06-26 05:07:58 +08:00
|
|
|
using std::string;
|
2002-05-11 06:44:58 +08:00
|
|
|
|
|
|
|
namespace {
|
2002-09-27 00:38:03 +08: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-11 06:44:58 +08:00
|
|
|
struct LICM : public FunctionPass, public InstVisitor<LICM> {
|
2002-06-26 00:13:24 +08:00
|
|
|
virtual bool runOnFunction(Function &F);
|
2002-05-11 06:44:58 +08:00
|
|
|
|
|
|
|
// This transformation requires natural loop information...
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.preservesCFG();
|
2002-09-27 00:19:31 +08:00
|
|
|
AU.addRequiredID(LoopPreheadersID);
|
2002-08-09 03:01:30 +08:00
|
|
|
AU.addRequired<LoopInfo>();
|
2002-08-23 05:39:55 +08:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2002-05-11 06:44:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2002-08-23 05:39:55 +08:00
|
|
|
Loop *CurLoop; // The current loop we are working on...
|
|
|
|
bool Changed; // Set to true when we change anything.
|
|
|
|
AliasAnalysis *AA; // Currently AliasAnalysis information
|
2002-05-11 06:44:58 +08:00
|
|
|
|
|
|
|
// visitLoop - Hoist expressions out of the specified loop...
|
|
|
|
void visitLoop(Loop *L);
|
|
|
|
|
|
|
|
// notInCurrentLoop - Little predicate that returns true if the specified
|
|
|
|
// basic block is in a subloop of the current one, not the current one
|
|
|
|
// itself.
|
|
|
|
//
|
|
|
|
bool notInCurrentLoop(BasicBlock *BB) {
|
|
|
|
for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
|
|
|
|
if (CurLoop->getSubLoops()[i]->contains(BB))
|
|
|
|
return true; // A subloop actually contains this block!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-26 00:13:24 +08:00
|
|
|
void hoist(Instruction &I);
|
2002-05-11 06:44:58 +08:00
|
|
|
|
2002-08-23 05:39:55 +08:00
|
|
|
// pointerInvalidatedByLoop - Return true if the body of this loop may store
|
|
|
|
// into the memory location pointed to by V.
|
|
|
|
//
|
|
|
|
bool pointerInvalidatedByLoop(Value *V);
|
|
|
|
|
2002-05-11 06:44:58 +08:00
|
|
|
// isLoopInvariant - Return true if the specified value is loop invariant
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// visitBasicBlock - Run LICM on a particular block.
|
|
|
|
void visitBasicBlock(BasicBlock *BB);
|
|
|
|
|
|
|
|
// Instruction visitation handlers... these basically control whether or not
|
|
|
|
// the specified instruction types are hoisted.
|
|
|
|
//
|
|
|
|
friend class InstVisitor<LICM>;
|
2002-06-26 00:13:24 +08:00
|
|
|
void visitBinaryOperator(Instruction &I) {
|
|
|
|
if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
|
2002-05-11 06:44:58 +08:00
|
|
|
hoist(I);
|
|
|
|
}
|
2002-08-15 02:22:19 +08:00
|
|
|
void visitCastInst(CastInst &CI) {
|
|
|
|
Instruction &I = (Instruction&)CI;
|
|
|
|
if (isLoopInvariant(I.getOperand(0))) hoist(I);
|
2002-08-15 02:18:02 +08:00
|
|
|
}
|
2002-06-26 00:13:24 +08:00
|
|
|
void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
|
2002-05-11 06:44:58 +08:00
|
|
|
|
2002-09-27 00:19:31 +08:00
|
|
|
void visitLoadInst(LoadInst &LI);
|
2002-08-23 05:39:55 +08:00
|
|
|
|
2002-06-26 00:13:24 +08: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-11 06:44:58 +08:00
|
|
|
hoist(I);
|
|
|
|
}
|
|
|
|
};
|
2002-07-24 02:06:35 +08:00
|
|
|
|
2002-07-27 05:12:46 +08:00
|
|
|
RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
|
2002-05-11 06:44:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createLICMPass() { return new LICM(); }
|
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
bool LICM::runOnFunction(Function &) {
|
2002-05-11 06:44:58 +08:00
|
|
|
// get our loop information...
|
|
|
|
const std::vector<Loop*> &TopLevelLoops =
|
|
|
|
getAnalysis<LoopInfo>().getTopLevelLoops();
|
|
|
|
|
2002-08-23 05:39:55 +08:00
|
|
|
// Get our alias analysis information...
|
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
|
|
|
|
2002-05-11 06:44:58 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
//
|
|
|
|
std::vector<BasicBlock*> BBs(L->getBlocks().begin(), L->getBlocks().end());
|
|
|
|
|
|
|
|
// Remove blocks that are actually in subloops...
|
|
|
|
BBs.erase(std::remove_if(BBs.begin(), BBs.end(),
|
|
|
|
bind_obj(this, &LICM::notInCurrentLoop)), BBs.end());
|
|
|
|
|
|
|
|
// Visit all of the basic blocks we have chosen, hoisting out the instructions
|
|
|
|
// as neccesary. This leaves dead copies of the instruction in the loop
|
|
|
|
// unfortunately...
|
|
|
|
//
|
|
|
|
for_each(BBs.begin(), BBs.end(), bind_obj(this, &LICM::visitBasicBlock));
|
|
|
|
|
|
|
|
// Clear out loops state information for the next iteration
|
|
|
|
CurLoop = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LICM::visitBasicBlock(BasicBlock *BB) {
|
2002-06-26 00:13:24 +08:00
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
|
|
|
|
visit(*I);
|
2002-05-11 06:44:58 +08:00
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
if (dceInstruction(I))
|
2002-05-11 06:44:58 +08:00
|
|
|
Changed = true;
|
|
|
|
else
|
2002-06-26 00:13:24 +08:00
|
|
|
++I;
|
2002-05-11 06:44:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
void LICM::hoist(Instruction &Inst) {
|
|
|
|
if (Inst.use_empty()) return; // Don't (re) hoist dead instructions!
|
2002-05-11 06:44:58 +08:00
|
|
|
//cerr << "Hoisting " << Inst;
|
|
|
|
|
|
|
|
BasicBlock *Header = CurLoop->getHeader();
|
|
|
|
|
|
|
|
// Old instruction will be removed, so take it's name...
|
2002-06-26 00:13:24 +08:00
|
|
|
string InstName = Inst.getName();
|
|
|
|
Inst.setName("");
|
2002-05-11 06:44:58 +08:00
|
|
|
|
2002-08-23 05:39:55 +08:00
|
|
|
if (isa<LoadInst>(Inst))
|
|
|
|
++NumHoistedLoads;
|
|
|
|
|
2002-05-11 06:44:58 +08:00
|
|
|
// The common case is that we have a pre-header. Generate special case code
|
|
|
|
// that is faster if that is the case.
|
|
|
|
//
|
2002-09-27 00:38:03 +08:00
|
|
|
BasicBlock *Preheader = CurLoop->getLoopPreheader();
|
|
|
|
assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
|
2002-05-11 06:44:58 +08:00
|
|
|
|
2002-09-27 00:38:03 +08:00
|
|
|
// Create a new copy of the instruction, for insertion into Preheader.
|
|
|
|
Instruction *New = Inst.clone();
|
|
|
|
New->setName(InstName);
|
2002-05-11 06:44:58 +08:00
|
|
|
|
2002-09-27 00:38:03 +08:00
|
|
|
// Insert the new node in Preheader, before the terminator.
|
|
|
|
Preheader->getInstList().insert(--Preheader->end(), New);
|
|
|
|
|
|
|
|
// Kill the old instruction...
|
|
|
|
Inst.replaceAllUsesWith(New);
|
|
|
|
++NumHoisted;
|
2002-05-11 06:44:58 +08:00
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2002-09-27 00:19:31 +08:00
|
|
|
|
|
|
|
void LICM::visitLoadInst(LoadInst &LI) {
|
|
|
|
if (isLoopInvariant(LI.getOperand(0)) &&
|
|
|
|
!pointerInvalidatedByLoop(LI.getOperand(0)))
|
|
|
|
hoist(LI);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-08-23 05:39:55 +08:00
|
|
|
// pointerInvalidatedByLoop - Return true if the body of this loop may store
|
|
|
|
// into the memory location pointed to by V.
|
|
|
|
//
|
|
|
|
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;
|
|
|
|
}
|