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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
|
|
|
|
#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"
|
2001-12-03 17:28:42 +00:00
|
|
|
#include "Support/STLExtras.h"
|
|
|
|
|
2001-12-04 04:32:29 +00:00
|
|
|
#if 0
|
|
|
|
#define DEBUG
|
|
|
|
#include "llvm/Analysis/Writer.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a
|
|
|
|
// name...
|
|
|
|
//
|
|
|
|
static Instruction *InsertCast(Instruction *Val, const Type *Ty,
|
|
|
|
BasicBlock::iterator It) {
|
|
|
|
Instruction *Cast = new CastInst(Val, Ty);
|
|
|
|
if (Val->hasName()) Cast->setName(Val->getName()+"-casted");
|
|
|
|
Val->getParent()->getInstList().insert(It, Cast);
|
|
|
|
return Cast;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
BasicBlock *Header = (BasicBlock*)Loop->getBlocks().front();
|
|
|
|
|
|
|
|
// 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
|
2001-12-03 17:28:42 +00:00
|
|
|
for (BasicBlock::iterator I = Header->begin();
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(*I); ++I)
|
|
|
|
IndVars.push_back(InductionVariable(PN, Loops));
|
|
|
|
|
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) {
|
|
|
|
if (IndVars[i].InductionType == InductionVariable::Cannonical)
|
|
|
|
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) {
|
2001-12-04 04:32:29 +00:00
|
|
|
// Create the PHI node for the new induction variable
|
|
|
|
PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar");
|
|
|
|
|
|
|
|
// Insert the phi node at the end of the other phi nodes...
|
|
|
|
Header->getInstList().insert(Header->begin()+IndVars.size(), PN);
|
|
|
|
|
|
|
|
// Create the increment instruction to add one to the counter...
|
|
|
|
Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
|
|
|
|
ConstantUInt::get(Type::UIntTy,1),
|
|
|
|
"add1-indvar");
|
|
|
|
|
|
|
|
// Insert the add instruction after all of the PHI nodes...
|
|
|
|
Header->getInstList().insert(Header->begin()+(IndVars.size()+1), Add);
|
|
|
|
|
|
|
|
// 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();
|
2001-12-05 19:41:33 +00:00
|
|
|
Changed = true;
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
cerr << "Induction variables:\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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...
|
|
|
|
//
|
|
|
|
unsigned InsertPos = IndVars.size();
|
|
|
|
for (unsigned i = 0; i < IndVars.size(); ++i) {
|
|
|
|
InductionVariable *IV = &IndVars[i];
|
|
|
|
#ifdef DEBUG
|
|
|
|
cerr << IndVars[i];
|
|
|
|
#endif
|
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)) {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string Name; // Create a scale by the step value...
|
2001-12-04 04:32:29 +00:00
|
|
|
if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-scale";
|
|
|
|
|
|
|
|
// If the types are not compatible, insert a cast now...
|
|
|
|
if (Val->getType() != IV->Step->getType())
|
|
|
|
Val = InsertCast(Val, IV->Step->getType(),
|
|
|
|
Header->begin()+InsertPos++);
|
|
|
|
|
|
|
|
Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step, Name);
|
|
|
|
// Insert the phi node at the end of the other phi nodes...
|
|
|
|
Header->getInstList().insert(Header->begin()+InsertPos++, Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isa<Constant>(IV->Start) || // If the start != 0
|
|
|
|
!cast<Constant>(IV->Start)->isNullValue()) {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string Name; // Create a offset by the start value...
|
2001-12-04 04:32:29 +00:00
|
|
|
if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-offset";
|
|
|
|
|
|
|
|
// If the types are not compatible, insert a cast now...
|
|
|
|
if (Val->getType() != IV->Start->getType())
|
|
|
|
Val = InsertCast(Val, IV->Start->getType(),
|
|
|
|
Header->begin()+InsertPos++);
|
|
|
|
|
|
|
|
Val = BinaryOperator::create(Instruction::Add, Val, IV->Start, Name);
|
|
|
|
// Insert the phi node at the end of the other phi nodes...
|
|
|
|
Header->getInstList().insert(Header->begin()+InsertPos++, Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the PHI node has a different type than val is, insert a cast now...
|
|
|
|
if (Val->getType() != IV->Phi->getType())
|
|
|
|
Val = InsertCast(Val, IV->Phi->getType(),
|
|
|
|
Header->begin()+InsertPos++);
|
|
|
|
|
|
|
|
// 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...
|
|
|
|
Header->getInstList().remove(IV->Phi);
|
|
|
|
delete IV->Phi;
|
|
|
|
InsertPos--; // Deleted an instr, decrement insert position
|
2001-12-05 19:41:33 +00:00
|
|
|
Changed = true;
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
2001-12-03 17:28:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2002-04-28 16:21:30 +00:00
|
|
|
static bool doit(Function *M, LoopInfo &Loops) {
|
2002-04-27 06:56:12 +00:00
|
|
|
// Induction Variables live in the header nodes of the loops of the function
|
2001-12-03 17:28:42 +00:00
|
|
|
return reduce_apply_bool(Loops.getTopLevelLoops().begin(),
|
|
|
|
Loops.getTopLevelLoops().end(),
|
|
|
|
std::bind1st(std::ptr_fun(TransformLoop), &Loops));
|
|
|
|
}
|
2002-01-31 00:45:11 +00:00
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
namespace {
|
2002-04-27 06:56:12 +00:00
|
|
|
struct InductionVariableSimplify : public FunctionPass {
|
2002-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const {
|
|
|
|
return "Induction Variable Cannonicalize";
|
|
|
|
}
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual bool runOnFunction(Function *F) {
|
2002-04-28 16:21:30 +00:00
|
|
|
return doit(F, getAnalysis<LoopInfo>());
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-04-28 16:21:30 +00:00
|
|
|
AU.addRequired(LoopInfo::ID);
|
2002-04-28 21:27:06 +00:00
|
|
|
AU.preservesCFG();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
};
|
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
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|