#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");
-
+namespace {
+ Statistic<> NumRemoved ("indvars\t\t- Number of aux indvars removed");
+ Statistic<> NumInserted("indvars\t\t- Number of cannonical indvars added");
+}
// 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;
+static Instruction *InsertCast(Value *Val, const Type *Ty,
+ Instruction *InsertBefore) {
+ return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore);
}
static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
bool FoundIndVars = false;
InductionVariable *Cannonical = 0;
for (unsigned i = 0; i < IndVars.size(); ++i) {
- if (IndVars[i].InductionType == InductionVariable::Cannonical)
+ if (IndVars[i].InductionType == InductionVariable::Cannonical &&
+ !isa<PointerType>(IndVars[i].Phi->getType()))
Cannonical = &IndVars[i];
if (IndVars[i].InductionType != InductionVariable::Unknown)
FoundIndVars = true;
// Okay, we want to convert other induction variables to use a cannonical
// indvar. If we don't have one, add one now...
if (!Cannonical) {
- // 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...
- AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN);
+ // 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);
// 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(AfterPHIIt, Add);
+ "add1-indvar", AfterPHIIt);
// Figure out which block is incoming and which is the backedge for the loop
BasicBlock *Incoming, *BackEdgeBlock;
DEBUG(IV->print(std::cerr));
+ // Don't do math with pointers...
+ const Type *IVTy = IV->Phi->getType();
+ if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy;
+
// Don't modify the cannonical indvar or unrecognized indvars...
if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
Instruction *Val = IterCount;
if (!isa<ConstantInt>(IV->Step) || // If the step != 1
!cast<ConstantInt>(IV->Step)->equalsInt(1)) {
- std::string Name; // Create a scale by the step value...
- 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(), AfterPHIIt);
+ if (Val->getType() != IVTy)
+ Val = InsertCast(Val, IVTy, AfterPHIIt);
+ if (IV->Step->getType() != IVTy)
+ IV->Step = InsertCast(IV->Step, IVTy, 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(AfterPHIIt, Val);
+ Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
+ IV->Phi->getName()+"-scale", AfterPHIIt);
}
- if (!isa<Constant>(IV->Start) || // If the start != 0
- !cast<Constant>(IV->Start)->isNullValue()) {
- std::string Name; // Create a offset by the start value...
- if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-offset";
-
+ // If the start != 0
+ if (IV->Start != Constant::getNullValue(IV->Start->getType())) {
// If the types are not compatible, insert a cast now...
- if (Val->getType() != IV->Start->getType())
- 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(AfterPHIIt, Val);
+ if (Val->getType() != IVTy)
+ Val = InsertCast(Val, IVTy, AfterPHIIt);
+ if (IV->Start->getType() != IVTy)
+ IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
+
+ // Insert the instruction after the phi nodes...
+ Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
+ IV->Phi->getName()+"-offset", AfterPHIIt);
}
// If the PHI node has a different type than val is, insert a cast now...
}
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequired(LoopInfo::ID);
+ AU.addRequired<LoopInfo>();
AU.preservesCFG();
}
};