//
//===----------------------------------------------------------------------===//
-#include "llvm/Transforms/Scalar/IndVarSimplify.h"
+#include "llvm/Transforms/Scalar.h"
#include "llvm/Analysis/InductionVariable.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/iPHINode.h"
#include "llvm/iOther.h"
#include "llvm/Type.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/ConstantVals.h"
+#include "llvm/Constants.h"
#include "llvm/Support/CFG.h"
#include "Support/STLExtras.h"
+#include "Support/StatisticReporter.h"
+
+static Statistic<> NumRemoved ("indvars\t\t- Number of aux indvars removed");
+static Statistic<> NumInserted("indvars\t\t- Number of cannonical indvars added");
-#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...
return Cast;
}
-static bool TransformLoop(cfg::LoopInfo *Loops, cfg::Loop *Loop) {
+static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
// Transform all subloops before this loop...
bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(),
Loop->getSubLoops().end(),
std::bind1st(std::ptr_fun(TransformLoop), Loops));
// 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();
+ //
+ BasicBlock *Header = 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...
//
std::vector<InductionVariable> IndVars; // Induction variables for block
- for (BasicBlock::iterator I = Header->begin();
- PHINode *PN = dyn_cast<PHINode>(*I); ++I)
+ BasicBlock::iterator AfterPHIIt = Header->begin();
+ for (; PHINode *PN = dyn_cast<PHINode>(&*AfterPHIIt); ++AfterPHIIt)
IndVars.push_back(InductionVariable(PN, Loops));
+ // AfterPHIIt now points to first nonphi instruction...
// If there are no phi nodes in this basic block, there can't be indvars...
if (IndVars.empty()) return Changed;
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);
+ AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN);
// Create the increment instruction to add one to the counter...
Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
"add1-indvar");
// Insert the add instruction after all of the PHI nodes...
- Header->getInstList().insert(Header->begin()+(IndVars.size()+1), Add);
+ Header->getInstList().insert(AfterPHIIt, Add);
// Figure out which block is incoming and which is the backedge for the loop
BasicBlock *Incoming, *BackEdgeBlock;
assert(PI == pred_end(Header) && "Loop headers should have 2 preds!");
// Add incoming values for the PHI node...
- PN->addIncoming(Constant::getNullConstant(Type::UIntTy), Incoming);
+ PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming);
PN->addIncoming(Add, BackEdgeBlock);
// Analyze the new induction variable...
assert(IndVars.back().InductionType == InductionVariable::Cannonical &&
"Just inserted cannonical indvar that is not cannonical!");
Cannonical = &IndVars.back();
+ ++NumInserted;
Changed = true;
}
-#ifdef DEBUG
- cerr << "Induction variables:\n";
-#endif
+ DEBUG(std::cerr << "Induction variables:\n");
// Get the current loop iteration count, which is always the value of the
// cannonical phi node...
// 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
+
+ DEBUG(IV->print(std::cerr));
+
// Don't modify the cannonical indvar or unrecognized indvars...
if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
Instruction *Val = IterCount;
// 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 = InsertCast(Val, IV->Step->getType(), AfterPHIIt);
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);
+ Header->getInstList().insert(AfterPHIIt, Val);
}
if (!isa<Constant>(IV->Start) || // If the start != 0
// 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 = InsertCast(Val, IV->Start->getType(), AfterPHIIt);
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);
+ Header->getInstList().insert(AfterPHIIt, 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++);
+ Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt);
// Replace all uses of the old PHI node with the new computed value...
IV->Phi->replaceAllUsesWith(Val);
Val->setName(OldName);
// Delete the old, now unused, phi node...
- Header->getInstList().remove(IV->Phi);
- delete IV->Phi;
- InsertPos--; // Deleted an instr, decrement insert position
+ Header->getInstList().erase(IV->Phi);
Changed = true;
+ ++NumRemoved;
}
}
return Changed;
}
-bool InductionVariableSimplify::doit(Method *M, cfg::LoopInfo &Loops) {
- // Induction Variables live in the header nodes of the loops of the method...
- return reduce_apply_bool(Loops.getTopLevelLoops().begin(),
- Loops.getTopLevelLoops().end(),
- std::bind1st(std::ptr_fun(TransformLoop), &Loops));
-}
+namespace {
+ struct InductionVariableSimplify : public FunctionPass {
+ virtual bool runOnFunction(Function &) {
+ LoopInfo &LI = getAnalysis<LoopInfo>();
-bool InductionVariableSimplify::runOnMethod(Method *M) {
- return doit(M, getAnalysis<cfg::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));
+ }
+
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.addRequired<LoopInfo>();
+ AU.preservesCFG();
+ }
+ };
+ RegisterOpt<InductionVariableSimplify> X("indvars",
+ "Cannonicalize Induction Variables");
}
-void InductionVariableSimplify::getAnalysisUsageInfo(Pass::AnalysisSet &Req,
- Pass::AnalysisSet &Dest,
- Pass::AnalysisSet &Prov) {
- Req.push_back(cfg::LoopInfo::ID);
+Pass *createIndVarSimplifyPass() {
+ return new InductionVariableSimplify();
}