2001-12-03 17:28:42 +00:00
|
|
|
//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
|
|
|
|
//
|
2003-09-10 05:10:34 +00:00
|
|
|
// Guarantees that all loops with identifiable, linear, induction variables will
|
2003-09-10 05:24:46 +00:00
|
|
|
// be transformed to have a single, canonical, induction variable. After this
|
2003-09-10 05:10:34 +00:00
|
|
|
// pass runs, it guarantees the the first PHI node of the header block in the
|
2003-09-10 05:24:46 +00:00
|
|
|
// loop is the canonical induction variable if there is one.
|
2001-12-03 17:28:42 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2001-12-03 17:28:42 +00:00
|
|
|
#include "llvm/Analysis/InductionVariable.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/iPHINode.h"
|
2001-12-04 04:32:29 +00:00
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/Type.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-02-12 22:39:50 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2003-08-01 22:15:03 +00:00
|
|
|
#include "Support/Debug.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2003-08-01 22:15:03 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2002-05-10 15:38:35 +00:00
|
|
|
|
2002-09-10 05:24:05 +00:00
|
|
|
namespace {
|
2002-10-01 22:38:41 +00:00
|
|
|
Statistic<> NumRemoved ("indvars", "Number of aux indvars removed");
|
2003-09-10 05:24:46 +00:00
|
|
|
Statistic<> NumInserted("indvars", "Number of canonical indvars added");
|
2002-09-10 05:24:05 +00:00
|
|
|
}
|
2001-12-04 04:32:29 +00:00
|
|
|
|
|
|
|
// InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a
|
|
|
|
// name...
|
|
|
|
//
|
2002-09-10 05:24:05 +00:00
|
|
|
static Instruction *InsertCast(Value *Val, const Type *Ty,
|
2002-09-10 17:04:02 +00:00
|
|
|
Instruction *InsertBefore) {
|
|
|
|
return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore);
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 16:21:30 +00:00
|
|
|
static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
|
2001-12-03 17:28:42 +00:00
|
|
|
// Transform all subloops before this loop...
|
|
|
|
bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(),
|
|
|
|
Loop->getSubLoops().end(),
|
2002-01-20 22:54:45 +00:00
|
|
|
std::bind1st(std::ptr_fun(TransformLoop), Loops));
|
2001-12-03 17:28:42 +00:00
|
|
|
// Get the header node for this loop. All of the phi nodes that could be
|
|
|
|
// induction variables must live in this basic block.
|
2002-05-10 15:38:35 +00:00
|
|
|
//
|
2003-09-10 05:24:46 +00:00
|
|
|
BasicBlock *Header = Loop->getHeader();
|
2001-12-03 17:28:42 +00:00
|
|
|
|
|
|
|
// Loop over all of the PHI nodes in the basic block, calculating the
|
|
|
|
// induction variables that they represent... stuffing the induction variable
|
|
|
|
// info into a vector...
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<InductionVariable> IndVars; // Induction variables for block
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator AfterPHIIt = Header->begin();
|
2003-04-23 16:37:45 +00:00
|
|
|
for (; PHINode *PN = dyn_cast<PHINode>(AfterPHIIt); ++AfterPHIIt)
|
2001-12-03 17:28:42 +00:00
|
|
|
IndVars.push_back(InductionVariable(PN, Loops));
|
2003-10-10 17:57:28 +00:00
|
|
|
// AfterPHIIt now points to first non-phi instruction...
|
2001-12-03 17:28:42 +00:00
|
|
|
|
2001-12-04 04:32:29 +00:00
|
|
|
// If there are no phi nodes in this basic block, there can't be indvars...
|
2001-12-03 17:28:42 +00:00
|
|
|
if (IndVars.empty()) return Changed;
|
|
|
|
|
2003-09-10 05:24:46 +00:00
|
|
|
// Loop over the induction variables, looking for a canonical induction
|
2001-12-03 17:28:42 +00:00
|
|
|
// variable, and checking to make sure they are not all unknown induction
|
|
|
|
// variables.
|
|
|
|
//
|
|
|
|
bool FoundIndVars = false;
|
2003-09-10 05:24:46 +00:00
|
|
|
InductionVariable *Canonical = 0;
|
2001-12-03 17:28:42 +00:00
|
|
|
for (unsigned i = 0; i < IndVars.size(); ++i) {
|
2003-09-10 05:24:46 +00:00
|
|
|
if (IndVars[i].InductionType == InductionVariable::Canonical &&
|
2002-09-10 05:24:05 +00:00
|
|
|
!isa<PointerType>(IndVars[i].Phi->getType()))
|
2003-09-10 05:24:46 +00:00
|
|
|
Canonical = &IndVars[i];
|
2001-12-03 17:28:42 +00:00
|
|
|
if (IndVars[i].InductionType != InductionVariable::Unknown)
|
|
|
|
FoundIndVars = true;
|
|
|
|
}
|
|
|
|
|
2003-09-10 05:24:46 +00:00
|
|
|
// No induction variables, bail early... don't add a canonical indvar
|
2001-12-03 17:28:42 +00:00
|
|
|
if (!FoundIndVars) return Changed;
|
|
|
|
|
2003-09-10 05:24:46 +00:00
|
|
|
// Okay, we want to convert other induction variables to use a canonical
|
2001-12-03 17:28:42 +00:00
|
|
|
// indvar. If we don't have one, add one now...
|
2003-09-10 05:24:46 +00:00
|
|
|
if (!Canonical) {
|
2002-09-10 17:04:02 +00:00
|
|
|
// Create the PHI node for the new induction variable, and insert the phi
|
2003-09-23 20:26:48 +00:00
|
|
|
// node at the start of the PHI nodes...
|
|
|
|
PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar", Header->begin());
|
2001-12-04 04:32:29 +00:00
|
|
|
|
|
|
|
// Create the increment instruction to add one to the counter...
|
|
|
|
Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
|
|
|
|
ConstantUInt::get(Type::UIntTy,1),
|
2002-09-10 17:04:02 +00:00
|
|
|
"add1-indvar", AfterPHIIt);
|
2001-12-04 04:32:29 +00:00
|
|
|
|
|
|
|
// Figure out which block is incoming and which is the backedge for the loop
|
|
|
|
BasicBlock *Incoming, *BackEdgeBlock;
|
2002-02-12 22:39:50 +00:00
|
|
|
pred_iterator PI = pred_begin(Header);
|
|
|
|
assert(PI != pred_end(Header) && "Loop headers should have 2 preds!");
|
2001-12-04 04:32:29 +00:00
|
|
|
if (Loop->contains(*PI)) { // First pred is back edge...
|
|
|
|
BackEdgeBlock = *PI++;
|
|
|
|
Incoming = *PI++;
|
|
|
|
} else {
|
|
|
|
Incoming = *PI++;
|
|
|
|
BackEdgeBlock = *PI++;
|
|
|
|
}
|
2002-02-12 22:39:50 +00:00
|
|
|
assert(PI == pred_end(Header) && "Loop headers should have 2 preds!");
|
2001-12-04 04:32:29 +00:00
|
|
|
|
|
|
|
// Add incoming values for the PHI node...
|
2002-04-27 02:25:14 +00:00
|
|
|
PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming);
|
2001-12-04 04:32:29 +00:00
|
|
|
PN->addIncoming(Add, BackEdgeBlock);
|
|
|
|
|
|
|
|
// Analyze the new induction variable...
|
|
|
|
IndVars.push_back(InductionVariable(PN, Loops));
|
2003-09-10 05:24:46 +00:00
|
|
|
assert(IndVars.back().InductionType == InductionVariable::Canonical &&
|
|
|
|
"Just inserted canonical indvar that is not canonical!");
|
|
|
|
Canonical = &IndVars.back();
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumInserted;
|
2001-12-05 19:41:33 +00:00
|
|
|
Changed = true;
|
2003-09-23 20:26:48 +00:00
|
|
|
} else {
|
|
|
|
// If we have a canonical induction variable, make sure that it is the first
|
|
|
|
// one in the basic block.
|
|
|
|
if (&Header->front() != Canonical->Phi)
|
|
|
|
Header->getInstList().splice(Header->begin(), Header->getInstList(),
|
|
|
|
Canonical->Phi);
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 21:07:58 +00:00
|
|
|
DEBUG(std::cerr << "Induction variables:\n");
|
2001-12-04 04:32:29 +00:00
|
|
|
|
|
|
|
// Get the current loop iteration count, which is always the value of the
|
2003-09-10 05:24:46 +00:00
|
|
|
// canonical phi node...
|
2001-12-04 04:32:29 +00:00
|
|
|
//
|
2003-09-10 05:24:46 +00:00
|
|
|
PHINode *IterCount = Canonical->Phi;
|
2001-12-04 04:32:29 +00:00
|
|
|
|
2003-09-10 05:24:46 +00:00
|
|
|
// Loop through and replace all of the auxiliary induction variables with
|
|
|
|
// references to the canonical induction variable...
|
2001-12-04 04:32:29 +00:00
|
|
|
//
|
|
|
|
for (unsigned i = 0; i < IndVars.size(); ++i) {
|
|
|
|
InductionVariable *IV = &IndVars[i];
|
2002-05-22 17:17:27 +00:00
|
|
|
|
2002-07-27 01:12:17 +00:00
|
|
|
DEBUG(IV->print(std::cerr));
|
2002-05-22 17:17:27 +00:00
|
|
|
|
2002-09-10 05:24:05 +00:00
|
|
|
// Don't do math with pointers...
|
|
|
|
const Type *IVTy = IV->Phi->getType();
|
|
|
|
if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy;
|
|
|
|
|
2003-09-10 05:24:46 +00:00
|
|
|
// Don't modify the canonical indvar or unrecognized indvars...
|
|
|
|
if (IV != Canonical && IV->InductionType != InductionVariable::Unknown) {
|
2001-12-04 04:32:29 +00:00
|
|
|
Instruction *Val = IterCount;
|
|
|
|
if (!isa<ConstantInt>(IV->Step) || // If the step != 1
|
|
|
|
!cast<ConstantInt>(IV->Step)->equalsInt(1)) {
|
|
|
|
|
|
|
|
// If the types are not compatible, insert a cast now...
|
2002-09-10 05:24:05 +00:00
|
|
|
if (Val->getType() != IVTy)
|
|
|
|
Val = InsertCast(Val, IVTy, AfterPHIIt);
|
|
|
|
if (IV->Step->getType() != IVTy)
|
|
|
|
IV->Step = InsertCast(IV->Step, IVTy, AfterPHIIt);
|
2001-12-04 04:32:29 +00:00
|
|
|
|
2002-09-10 05:24:05 +00:00
|
|
|
Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
|
2002-09-10 17:04:02 +00:00
|
|
|
IV->Phi->getName()+"-scale", AfterPHIIt);
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
2002-09-10 05:24:05 +00:00
|
|
|
// If the start != 0
|
|
|
|
if (IV->Start != Constant::getNullValue(IV->Start->getType())) {
|
2001-12-04 04:32:29 +00:00
|
|
|
// If the types are not compatible, insert a cast now...
|
2002-09-10 05:24:05 +00:00
|
|
|
if (Val->getType() != IVTy)
|
|
|
|
Val = InsertCast(Val, IVTy, AfterPHIIt);
|
|
|
|
if (IV->Start->getType() != IVTy)
|
|
|
|
IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
|
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
// Insert the instruction after the phi nodes...
|
2002-09-10 05:24:05 +00:00
|
|
|
Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
|
2002-09-10 17:04:02 +00:00
|
|
|
IV->Phi->getName()+"-offset", AfterPHIIt);
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the PHI node has a different type than val is, insert a cast now...
|
|
|
|
if (Val->getType() != IV->Phi->getType())
|
2002-06-25 16:13:24 +00:00
|
|
|
Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt);
|
2001-12-04 04:32:29 +00:00
|
|
|
|
|
|
|
// Replace all uses of the old PHI node with the new computed value...
|
|
|
|
IV->Phi->replaceAllUsesWith(Val);
|
|
|
|
|
|
|
|
// Move the PHI name to it's new equivalent value...
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string OldName = IV->Phi->getName();
|
2001-12-04 04:32:29 +00:00
|
|
|
IV->Phi->setName("");
|
|
|
|
Val->setName(OldName);
|
2001-12-03 17:28:42 +00:00
|
|
|
|
2001-12-04 04:32:29 +00:00
|
|
|
// Delete the old, now unused, phi node...
|
2002-06-25 16:13:24 +00:00
|
|
|
Header->getInstList().erase(IV->Phi);
|
2001-12-05 19:41:33 +00:00
|
|
|
Changed = true;
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumRemoved;
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
2001-12-03 17:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-04-27 06:56:12 +00:00
|
|
|
struct InductionVariableSimplify : public FunctionPass {
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &) {
|
2002-05-10 15:38:35 +00:00
|
|
|
LoopInfo &LI = getAnalysis<LoopInfo>();
|
|
|
|
|
|
|
|
// Induction Variables live in the header nodes of loops
|
|
|
|
return reduce_apply_bool(LI.getTopLevelLoops().begin(),
|
|
|
|
LI.getTopLevelLoops().end(),
|
|
|
|
std::bind1st(std::ptr_fun(TransformLoop), &LI));
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-08-08 19:01:30 +00:00
|
|
|
AU.addRequired<LoopInfo>();
|
2003-10-12 21:52:28 +00:00
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
2002-10-21 20:00:28 +00:00
|
|
|
AU.setPreservesCFG();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
};
|
2002-07-26 21:12:46 +00:00
|
|
|
RegisterOpt<InductionVariableSimplify> X("indvars",
|
2003-09-10 05:24:46 +00:00
|
|
|
"Canonicalize Induction Variables");
|
2002-01-31 00:45:11 +00:00
|
|
|
}
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
Pass *createIndVarSimplifyPass() {
|
|
|
|
return new InductionVariableSimplify();
|
2002-01-31 00:45:11 +00:00
|
|
|
}
|