blob: 485586a018cd0f66ce9f15806da0a0b2c6c6c6fb [file] [log] [blame]
Chris Lattner0bbe58f2001-11-26 18:41:20 +00001//===- llvm/Analysis/InductionVariable.h - Induction variable ----*- C++ -*--=//
2//
3// This interface is used to identify and classify induction variables that
4// exist in the program. Induction variables must contain a PHI node that
5// exists in a loop header. Because of this, they are identified an managed by
6// this PHI node.
7//
8// Induction variables are classified into a type. Knowing that an induction
9// variable is of a specific type can constrain the values of the start and
10// step. For example, a SimpleLinear induction variable must have a start and
11// step values that are constants.
12//
13// Induction variables can be created with or without loop information. If no
14// loop information is available, induction variables cannot be recognized to be
15// more than SimpleLinear variables.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Analysis/InductionVariable.h"
20#include "llvm/Analysis/LoopInfo.h"
21#include "llvm/Analysis/Expressions.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000022#include "llvm/iPHINode.h"
23#include "llvm/InstrTypes.h"
Chris Lattner0bbe58f2001-11-26 18:41:20 +000024#include "llvm/Type.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000025#include "llvm/Constants.h"
Chris Lattnera59cbb22002-07-27 01:12:17 +000026#include "llvm/Assembly/Writer.h"
Chris Lattner0bbe58f2001-11-26 18:41:20 +000027
28using analysis::ExprType;
29
30
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000031static bool isLoopInvariant(const Value *V, const Loop *L) {
Chris Lattner73e21422002-04-09 19:48:49 +000032 if (isa<Constant>(V) || isa<Argument>(V) || isa<GlobalValue>(V))
Chris Lattner0bbe58f2001-11-26 18:41:20 +000033 return true;
34
Chris Lattner7e708292002-06-25 16:13:24 +000035 const Instruction *I = cast<Instruction>(V);
36 const BasicBlock *BB = I->getParent();
Chris Lattner0bbe58f2001-11-26 18:41:20 +000037
38 return !L->contains(BB);
39}
40
41enum InductionVariable::iType
42InductionVariable::Classify(const Value *Start, const Value *Step,
Chris Lattnerceadf052002-07-24 22:40:36 +000043 const Loop *L) {
Chris Lattner0bbe58f2001-11-26 18:41:20 +000044 // Check for cannonical and simple linear expressions now...
Chris Lattner7e708292002-06-25 16:13:24 +000045 if (const ConstantInt *CStart = dyn_cast<ConstantInt>(Start))
46 if (const ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) {
Chris Lattner0bbe58f2001-11-26 18:41:20 +000047 if (CStart->equalsInt(0) && CStep->equalsInt(1))
48 return Cannonical;
49 else
50 return SimpleLinear;
51 }
52
53 // Without loop information, we cannot do any better, so bail now...
54 if (L == 0) return Unknown;
55
56 if (isLoopInvariant(Start, L) && isLoopInvariant(Step, L))
57 return Linear;
58 return Unknown;
59}
60
61// Create an induction variable for the specified value. If it is a PHI, and
62// if it's recognizable, classify it and fill in instance variables.
63//
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000064InductionVariable::InductionVariable(PHINode *P, LoopInfo *LoopInfo) {
Chris Lattner0bbe58f2001-11-26 18:41:20 +000065 InductionType = Unknown; // Assume the worst
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000066 Phi = P;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000067
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000068 // If the PHI node has more than two predecessors, we don't know how to
Chris Lattner0bbe58f2001-11-26 18:41:20 +000069 // handle it.
70 //
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000071 if (Phi->getNumIncomingValues() != 2) return;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000072
Chris Lattner6de230a2001-12-05 06:32:30 +000073 // FIXME: Handle FP induction variables.
74 if (Phi->getType() == Type::FloatTy || Phi->getType() == Type::DoubleTy)
75 return;
76
Chris Lattner0bbe58f2001-11-26 18:41:20 +000077 // If we have loop information, make sure that this PHI node is in the header
78 // of a loop...
79 //
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000080 const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000081 if (L && L->getHeader() != Phi->getParent())
82 return;
83
84 Value *V1 = Phi->getIncomingValue(0);
85 Value *V2 = Phi->getIncomingValue(1);
86
87 if (L == 0) { // No loop information? Base everything on expression analysis
88 ExprType E1 = analysis::ClassifyExpression(V1);
89 ExprType E2 = analysis::ClassifyExpression(V2);
90
91 if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression
Chris Lattner697954c2002-01-20 22:54:45 +000092 std::swap(E1, E2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +000093
94 // E1 must be a constant incoming value, and E2 must be a linear expression
95 // with respect to the PHI node.
96 //
97 if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear ||
98 E2.Var != Phi)
99 return;
100
101 // Okay, we have found an induction variable. Save the start and step values
102 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000103 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000104
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000105 Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0));
106 Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0));
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000107 } else {
108 // Okay, at this point, we know that we have loop information...
109
110 // Make sure that V1 is the incoming value, and V2 is from the backedge of
111 // the loop.
112 if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now.
Chris Lattner697954c2002-01-20 22:54:45 +0000113 std::swap(V1, V2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000114
115 Start = V1; // We know that Start has to be loop invariant...
116 Step = 0;
117
118 if (V2 == Phi) { // referencing the PHI directly? Must have zero step
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000119 Step = Constant::getNullValue(Phi->getType());
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000120 } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) {
121 // TODO: This could be much better...
122 if (I->getOpcode() == Instruction::Add) {
123 if (I->getOperand(0) == Phi)
124 Step = I->getOperand(1);
125 else if (I->getOperand(1) == Phi)
126 Step = I->getOperand(0);
127 }
128 }
129
130 if (Step == 0) { // Unrecognized step value...
131 ExprType StepE = analysis::ClassifyExpression(V2);
132 if (StepE.ExprTy != ExprType::Linear ||
133 StepE.Var != Phi) return;
134
135 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000136 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000137 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0));
Chris Lattner621c9922001-12-04 08:12:47 +0000138 } else { // We were able to get a step value, simplify with expr analysis
139 ExprType StepE = analysis::ClassifyExpression(Step);
140 if (StepE.ExprTy == ExprType::Linear && StepE.Offset == 0) {
141 // No offset from variable? Grab the variable
142 Step = StepE.Var;
143 } else if (StepE.ExprTy == ExprType::Constant) {
144 if (StepE.Offset)
145 Step = (Value*)StepE.Offset;
146 else
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000147 Step = Constant::getNullValue(Step->getType());
Chris Lattner6de230a2001-12-05 06:32:30 +0000148 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000149 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner6de230a2001-12-05 06:32:30 +0000150 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy,0));
Chris Lattner621c9922001-12-04 08:12:47 +0000151 }
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000152 }
153 }
154
155 // Classify the induction variable type now...
156 InductionType = InductionVariable::Classify(Start, Step, L);
157}
Chris Lattnera59cbb22002-07-27 01:12:17 +0000158
159void InductionVariable::print(std::ostream &o) const {
160 switch (InductionType) {
161 case InductionVariable::Cannonical: o << "Cannonical "; break;
162 case InductionVariable::SimpleLinear: o << "SimpleLinear "; break;
163 case InductionVariable::Linear: o << "Linear "; break;
164 case InductionVariable::Unknown: o << "Unrecognized "; break;
165 }
166 o << "Induction Variable";
167 if (Phi) {
168 WriteAsOperand(o, Phi);
169 o << ":\n" << Phi;
170 } else {
171 o << "\n";
172 }
173 if (InductionType == InductionVariable::Unknown) return;
174
175 o << " Start ="; WriteAsOperand(o, Start);
176 o << " Step =" ; WriteAsOperand(o, Step);
177 o << "\n";
178}