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