2001-12-03 17:28:42 +00:00
|
|
|
//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
|
|
|
|
//
|
|
|
|
// InductionVariableSimplify - Transform induction variables in a program
|
|
|
|
// to all use a single cannonical induction variable per loop.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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");
|
|
|
|
Statistic<> NumInserted("indvars", "Number of cannonical 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
|
|
|
//
|
|
|
|
BasicBlock *Header = Loop->getBlocks().front();
|
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));
|
2002-06-25 16:13:24 +00:00
|
|
|
// AfterPHIIt now points to first nonphi 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;
|
|
|
|
|
|
|
|
// Loop over the induction variables, looking for a cannonical induction
|
|
|
|
// variable, and checking to make sure they are not all unknown induction
|
|
|
|
// variables.
|
|
|
|
//
|
|
|
|
bool FoundIndVars = false;
|
|
|
|
InductionVariable *Cannonical = 0;
|
|
|
|
for (unsigned i = 0; i < IndVars.size(); ++i) {
|
2002-09-10 05:24:05 +00:00
|
|
|
if (IndVars[i].InductionType == InductionVariable::Cannonical &&
|
|
|
|
!isa<PointerType>(IndVars[i].Phi->getType()))
|
2001-12-03 17:28:42 +00:00
|
|
|
Cannonical = &IndVars[i];
|
|
|
|
if (IndVars[i].InductionType != InductionVariable::Unknown)
|
|
|
|
FoundIndVars = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No induction variables, bail early... don't add a cannonnical indvar
|
|
|
|
if (!FoundIndVars) return Changed;
|
|
|
|
|
|
|
|
// Okay, we want to convert other induction variables to use a cannonical
|
|
|
|
// indvar. If we don't have one, add one now...
|
|
|
|
if (!Cannonical) {
|
2002-09-10 17:04:02 +00:00
|
|
|
// Create the PHI node for the new induction variable, and insert the phi
|
|
|
|
// node at the end of the other phi nodes...
|
|
|
|
PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar", AfterPHIIt);
|
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));
|
|
|
|
assert(IndVars.back().InductionType == InductionVariable::Cannonical &&
|
|
|
|
"Just inserted cannonical indvar that is not cannonical!");
|
|
|
|
Cannonical = &IndVars.back();
|
2002-05-10 15:38:35 +00:00
|
|
|
++NumInserted;
|
2001-12-05 19:41:33 +00:00
|
|
|
Changed = true;
|
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
|
|
|
|
// cannonical phi node...
|
|
|
|
//
|
|
|
|
PHINode *IterCount = Cannonical->Phi;
|
|
|
|
|
|
|
|
// Loop through and replace all of the auxillary induction variables with
|
|
|
|
// references to the primary induction variable...
|
|
|
|
//
|
|
|
|
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;
|
|
|
|
|
2001-12-04 08:13:06 +00:00
|
|
|
// Don't modify the cannonical indvar or unrecognized indvars...
|
|
|
|
if (IV != Cannonical && 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>();
|
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",
|
2002-07-23 18:06:35 +00:00
|
|
|
"Cannonicalize 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
|
|
|
}
|