2003-09-10 05:08:19 +00:00
|
|
|
//===- InductionVariable.cpp - Induction variable classification ----------===//
|
2003-10-20 19:43:21 +00: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-11-26 18:41:20 +00:00
|
|
|
//
|
2003-09-10 05:08:19 +00:00
|
|
|
// This file implements identification and classification of induction
|
|
|
|
// variables. Induction variables must contain a PHI node that exists in a
|
|
|
|
// loop header. Because of this, they are identified an managed by this PHI
|
|
|
|
// node.
|
2001-11-26 18:41:20 +00:00
|
|
|
//
|
|
|
|
// Induction variables are classified into a type. Knowing that an induction
|
|
|
|
// variable is of a specific type can constrain the values of the start and
|
|
|
|
// step. For example, a SimpleLinear induction variable must have a start and
|
|
|
|
// step values that are constants.
|
|
|
|
//
|
|
|
|
// Induction variables can be created with or without loop information. If no
|
|
|
|
// loop information is available, induction variables cannot be recognized to be
|
|
|
|
// more than SimpleLinear variables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/InductionVariable.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/Expressions.h"
|
2002-10-11 05:34:32 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
2003-12-22 05:26:29 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2001-11-26 18:41:20 +00:00
|
|
|
#include "llvm/Type.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2002-10-11 05:34:32 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2002-07-27 01:12:17 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2003-08-01 22:15:03 +00:00
|
|
|
#include "Support/Debug.h"
|
2003-12-22 05:26:29 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-04-28 16:21:30 +00:00
|
|
|
static bool isLoopInvariant(const Value *V, const Loop *L) {
|
2003-09-10 04:49:10 +00:00
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
return !L->contains(I->getParent());
|
|
|
|
// non-instructions all dominate instructions/blocks
|
|
|
|
return true;
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum InductionVariable::iType
|
|
|
|
InductionVariable::Classify(const Value *Start, const Value *Step,
|
2002-10-11 05:34:32 +00:00
|
|
|
const Loop *L) {
|
2003-09-10 05:24:09 +00:00
|
|
|
// Check for canonical and simple linear expressions now...
|
2002-06-25 16:13:24 +00:00
|
|
|
if (const ConstantInt *CStart = dyn_cast<ConstantInt>(Start))
|
|
|
|
if (const ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) {
|
2003-09-10 04:49:10 +00:00
|
|
|
if (CStart->isNullValue() && CStep->equalsInt(1))
|
2003-09-10 05:24:09 +00:00
|
|
|
return Canonical;
|
2001-11-26 18:41:20 +00:00
|
|
|
else
|
2002-10-11 05:34:32 +00:00
|
|
|
return SimpleLinear;
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Without loop information, we cannot do any better, so bail now...
|
|
|
|
if (L == 0) return Unknown;
|
|
|
|
|
|
|
|
if (isLoopInvariant(Start, L) && isLoopInvariant(Step, L))
|
|
|
|
return Linear;
|
|
|
|
return Unknown;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an induction variable for the specified value. If it is a PHI, and
|
|
|
|
// if it's recognizable, classify it and fill in instance variables.
|
|
|
|
//
|
2002-10-11 05:34:32 +00:00
|
|
|
InductionVariable::InductionVariable(PHINode *P, LoopInfo *LoopInfo): End(0) {
|
2001-11-26 18:41:20 +00:00
|
|
|
InductionType = Unknown; // Assume the worst
|
2001-12-03 17:27:42 +00:00
|
|
|
Phi = P;
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2001-12-03 17:27:42 +00:00
|
|
|
// If the PHI node has more than two predecessors, we don't know how to
|
2001-11-26 18:41:20 +00:00
|
|
|
// handle it.
|
|
|
|
//
|
2001-12-03 17:27:42 +00:00
|
|
|
if (Phi->getNumIncomingValues() != 2) return;
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2001-12-05 06:32:30 +00:00
|
|
|
// FIXME: Handle FP induction variables.
|
|
|
|
if (Phi->getType() == Type::FloatTy || Phi->getType() == Type::DoubleTy)
|
|
|
|
return;
|
|
|
|
|
2001-11-26 18:41:20 +00:00
|
|
|
// If we have loop information, make sure that this PHI node is in the header
|
|
|
|
// of a loop...
|
|
|
|
//
|
2002-04-28 16:21:30 +00:00
|
|
|
const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
|
2001-11-26 18:41:20 +00:00
|
|
|
if (L && L->getHeader() != Phi->getParent())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Value *V1 = Phi->getIncomingValue(0);
|
|
|
|
Value *V2 = Phi->getIncomingValue(1);
|
|
|
|
|
|
|
|
if (L == 0) { // No loop information? Base everything on expression analysis
|
2003-12-23 08:04:08 +00:00
|
|
|
ExprType E1 = ClassifyExpr(V1);
|
|
|
|
ExprType E2 = ClassifyExpr(V2);
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression
|
2002-01-20 22:54:45 +00:00
|
|
|
std::swap(E1, E2);
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
// E1 must be a constant incoming value, and E2 must be a linear expression
|
|
|
|
// with respect to the PHI node.
|
|
|
|
//
|
|
|
|
if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear ||
|
2002-10-11 05:34:32 +00:00
|
|
|
E2.Var != Phi)
|
2001-11-26 18:41:20 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Okay, we have found an induction variable. Save the start and step values
|
|
|
|
const Type *ETy = Phi->getType();
|
2002-05-06 16:15:30 +00:00
|
|
|
if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0));
|
|
|
|
Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0));
|
2001-11-26 18:41:20 +00:00
|
|
|
} else {
|
|
|
|
// Okay, at this point, we know that we have loop information...
|
|
|
|
|
|
|
|
// Make sure that V1 is the incoming value, and V2 is from the backedge of
|
|
|
|
// the loop.
|
|
|
|
if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now.
|
2002-01-20 22:54:45 +00:00
|
|
|
std::swap(V1, V2);
|
2001-11-26 18:41:20 +00:00
|
|
|
|
|
|
|
Start = V1; // We know that Start has to be loop invariant...
|
|
|
|
Step = 0;
|
|
|
|
|
|
|
|
if (V2 == Phi) { // referencing the PHI directly? Must have zero step
|
2002-04-27 02:25:14 +00:00
|
|
|
Step = Constant::getNullValue(Phi->getType());
|
2001-11-26 18:41:20 +00:00
|
|
|
} else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) {
|
|
|
|
if (I->getOpcode() == Instruction::Add) {
|
2002-10-11 05:34:32 +00:00
|
|
|
if (I->getOperand(0) == Phi)
|
|
|
|
Step = I->getOperand(1);
|
|
|
|
else if (I->getOperand(1) == Phi)
|
|
|
|
Step = I->getOperand(0);
|
2003-12-22 05:26:29 +00:00
|
|
|
} else if (I->getOpcode() == Instruction::Sub &&
|
|
|
|
I->getOperand(0) == Phi) {
|
|
|
|
// If the incoming value is a constant, just form a constant negative
|
|
|
|
// step. Otherwise, negate the step outside of the loop and use it.
|
|
|
|
Value *V = I->getOperand(1);
|
|
|
|
Constant *Zero = Constant::getNullValue(V->getType());
|
|
|
|
if (Constant *CV = dyn_cast<Constant>(V))
|
|
|
|
Step = ConstantExpr::get(Instruction::Sub, Zero, CV);
|
|
|
|
else if (Instruction *I = dyn_cast<Instruction>(V)) {
|
|
|
|
Step = BinaryOperator::create(Instruction::Sub, Zero, V,
|
|
|
|
V->getName()+".neg", I->getNext());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Step = BinaryOperator::create(Instruction::Sub, Zero, V,
|
|
|
|
V->getName()+".neg",
|
|
|
|
Phi->getParent()->getParent()->begin()->begin());
|
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
2003-12-22 05:26:29 +00:00
|
|
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V2)) {
|
|
|
|
if (GEP->getNumOperands() == 2 &&
|
|
|
|
GEP->getOperand(0) == Phi)
|
|
|
|
Step = GEP->getOperand(1);
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Step == 0) { // Unrecognized step value...
|
2003-12-23 08:04:08 +00:00
|
|
|
ExprType StepE = ClassifyExpr(V2);
|
2001-11-26 18:41:20 +00:00
|
|
|
if (StepE.ExprTy != ExprType::Linear ||
|
2002-10-11 05:34:32 +00:00
|
|
|
StepE.Var != Phi) return;
|
2001-11-26 18:41:20 +00:00
|
|
|
|
2001-12-04 08:12:47 +00:00
|
|
|
const Type *ETy = Phi->getType();
|
2002-05-06 16:15:30 +00:00
|
|
|
if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
|
2001-12-04 08:12:47 +00:00
|
|
|
Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0));
|
|
|
|
} else { // We were able to get a step value, simplify with expr analysis
|
2003-12-23 08:04:08 +00:00
|
|
|
ExprType StepE = ClassifyExpr(Step);
|
2001-12-04 08:12:47 +00:00
|
|
|
if (StepE.ExprTy == ExprType::Linear && StepE.Offset == 0) {
|
|
|
|
// No offset from variable? Grab the variable
|
|
|
|
Step = StepE.Var;
|
|
|
|
} else if (StepE.ExprTy == ExprType::Constant) {
|
|
|
|
if (StepE.Offset)
|
|
|
|
Step = (Value*)StepE.Offset;
|
|
|
|
else
|
2002-04-27 02:25:14 +00:00
|
|
|
Step = Constant::getNullValue(Step->getType());
|
2001-12-05 06:32:30 +00:00
|
|
|
const Type *ETy = Phi->getType();
|
2002-05-06 16:15:30 +00:00
|
|
|
if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
|
2001-12-05 06:32:30 +00:00
|
|
|
Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy,0));
|
2001-12-04 08:12:47 +00:00
|
|
|
}
|
2001-11-26 18:41:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Classify the induction variable type now...
|
|
|
|
InductionType = InductionVariable::Classify(Start, Step, L);
|
|
|
|
}
|
2002-07-27 01:12:17 +00:00
|
|
|
|
2002-10-11 05:34:32 +00:00
|
|
|
|
2003-09-10 14:51:49 +00:00
|
|
|
Value *InductionVariable::getExecutionCount(LoopInfo *LoopInfo) {
|
|
|
|
if (InductionType != Canonical) return 0;
|
|
|
|
|
2002-10-11 05:34:32 +00:00
|
|
|
DEBUG(std::cerr << "entering getExecutionCount\n");
|
|
|
|
|
|
|
|
// Don't recompute if already available
|
|
|
|
if (End) {
|
|
|
|
DEBUG(std::cerr << "returning cached End value.\n");
|
|
|
|
return End;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
|
|
|
|
if (!L) {
|
|
|
|
DEBUG(std::cerr << "null loop. oops\n");
|
2003-09-10 14:51:49 +00:00
|
|
|
return 0;
|
2002-10-11 05:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// >1 backedge => cannot predict number of iterations
|
|
|
|
if (Phi->getNumIncomingValues() != 2) {
|
|
|
|
DEBUG(std::cerr << ">2 incoming values. oops\n");
|
2003-09-10 14:51:49 +00:00
|
|
|
return 0;
|
2002-10-11 05:34:32 +00:00
|
|
|
}
|
|
|
|
|
2003-09-11 18:14:24 +00:00
|
|
|
// Find final node: predecessor of the loop header that's also an exit
|
2002-11-09 00:49:43 +00:00
|
|
|
BasicBlock *terminator = 0;
|
2003-09-10 14:51:49 +00:00
|
|
|
for (pred_iterator PI = pred_begin(L->getHeader()),
|
|
|
|
PE = pred_end(L->getHeader()); PI != PE; ++PI)
|
2002-10-11 05:34:32 +00:00
|
|
|
if (L->isLoopExit(*PI)) {
|
|
|
|
terminator = *PI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Break in the loop => cannot predict number of iterations
|
|
|
|
// break: any block which is an exit node whose successor is not in loop,
|
|
|
|
// and this block is not marked as the terminator
|
|
|
|
//
|
|
|
|
const std::vector<BasicBlock*> &blocks = L->getBlocks();
|
2003-09-10 14:51:49 +00:00
|
|
|
for (std::vector<BasicBlock*>::const_iterator I = blocks.begin(),
|
|
|
|
e = blocks.end(); I != e; ++I)
|
|
|
|
if (L->isLoopExit(*I) && *I != terminator)
|
|
|
|
for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
|
|
|
|
if (!L->contains(*SI)) {
|
2002-10-11 05:34:32 +00:00
|
|
|
DEBUG(std::cerr << "break found in loop");
|
2003-09-10 14:51:49 +00:00
|
|
|
return 0;
|
2002-10-11 05:34:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BranchInst *B = dyn_cast<BranchInst>(terminator->getTerminator());
|
|
|
|
if (!B) {
|
2003-09-10 14:51:49 +00:00
|
|
|
DEBUG(std::cerr << "Terminator is not a cond branch!");
|
|
|
|
return 0;
|
2002-10-11 05:34:32 +00:00
|
|
|
}
|
2003-04-23 16:36:11 +00:00
|
|
|
SetCondInst *SCI = dyn_cast<SetCondInst>(B->getCondition());
|
2003-09-10 14:51:49 +00:00
|
|
|
if (!SCI) {
|
|
|
|
DEBUG(std::cerr << "Not a cond branch on setcc!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2002-10-11 05:34:32 +00:00
|
|
|
|
2003-09-10 14:51:49 +00:00
|
|
|
DEBUG(std::cerr << "sci:" << *SCI);
|
|
|
|
Value *condVal0 = SCI->getOperand(0);
|
|
|
|
Value *condVal1 = SCI->getOperand(1);
|
2002-10-11 05:34:32 +00:00
|
|
|
|
2003-09-10 14:55:05 +00:00
|
|
|
// The induction variable is the one coming from the backedge
|
|
|
|
Value *indVar = Phi->getIncomingValue(L->contains(Phi->getIncomingBlock(1)));
|
2002-10-11 05:34:32 +00:00
|
|
|
|
|
|
|
|
2003-09-10 14:51:49 +00:00
|
|
|
// Check to see if indVar is one of the parameters in SCI and if the other is
|
|
|
|
// loop-invariant, it is the UB
|
|
|
|
if (indVar == condVal0) {
|
|
|
|
if (isLoopInvariant(condVal1, L))
|
|
|
|
End = condVal1;
|
|
|
|
else {
|
|
|
|
DEBUG(std::cerr << "not loop invariant 1\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (indVar == condVal1) {
|
|
|
|
if (isLoopInvariant(condVal0, L))
|
|
|
|
End = condVal0;
|
|
|
|
else {
|
|
|
|
DEBUG(std::cerr << "not loop invariant 0\n");
|
|
|
|
return 0;
|
2002-10-11 05:34:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-09-10 14:51:49 +00:00
|
|
|
DEBUG(std::cerr << "Loop condition doesn't directly uses indvar\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (SCI->getOpcode()) {
|
|
|
|
case Instruction::SetLT:
|
|
|
|
case Instruction::SetNE: return End; // already done
|
|
|
|
case Instruction::SetLE:
|
|
|
|
// if compared to a constant int N, then predict N+1 iterations
|
|
|
|
if (ConstantSInt *ubSigned = dyn_cast<ConstantSInt>(End)) {
|
|
|
|
DEBUG(std::cerr << "signed int constant\n");
|
|
|
|
return ConstantSInt::get(ubSigned->getType(), ubSigned->getValue()+1);
|
|
|
|
} else if (ConstantUInt *ubUnsigned = dyn_cast<ConstantUInt>(End)) {
|
|
|
|
DEBUG(std::cerr << "unsigned int constant\n");
|
|
|
|
return ConstantUInt::get(ubUnsigned->getType(),
|
|
|
|
ubUnsigned->getValue()+1);
|
|
|
|
} else {
|
|
|
|
DEBUG(std::cerr << "symbolic bound\n");
|
|
|
|
// new expression N+1, insert right before the SCI. FIXME: If End is loop
|
|
|
|
// invariant, then so is this expression. We should insert it in the loop
|
|
|
|
// preheader if it exists.
|
|
|
|
return BinaryOperator::create(Instruction::Add, End,
|
|
|
|
ConstantInt::get(End->getType(), 1),
|
|
|
|
"tripcount", SCI);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0; // cannot predict
|
2002-10-11 05:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-27 01:12:17 +00:00
|
|
|
void InductionVariable::print(std::ostream &o) const {
|
|
|
|
switch (InductionType) {
|
2003-09-10 05:24:09 +00:00
|
|
|
case InductionVariable::Canonical: o << "Canonical "; break;
|
2002-07-27 01:12:17 +00:00
|
|
|
case InductionVariable::SimpleLinear: o << "SimpleLinear "; break;
|
|
|
|
case InductionVariable::Linear: o << "Linear "; break;
|
|
|
|
case InductionVariable::Unknown: o << "Unrecognized "; break;
|
|
|
|
}
|
2002-09-10 15:35:39 +00:00
|
|
|
o << "Induction Variable: ";
|
2002-07-27 01:12:17 +00:00
|
|
|
if (Phi) {
|
|
|
|
WriteAsOperand(o, Phi);
|
|
|
|
o << ":\n" << Phi;
|
|
|
|
} else {
|
|
|
|
o << "\n";
|
|
|
|
}
|
|
|
|
if (InductionType == InductionVariable::Unknown) return;
|
|
|
|
|
2002-09-10 15:35:39 +00:00
|
|
|
o << " Start = "; WriteAsOperand(o, Start);
|
|
|
|
o << " Step = " ; WriteAsOperand(o, Step);
|
2002-10-11 05:34:32 +00:00
|
|
|
if (End) {
|
|
|
|
o << " End = " ; WriteAsOperand(o, End);
|
|
|
|
}
|
2002-07-27 01:12:17 +00:00
|
|
|
o << "\n";
|
|
|
|
}
|