blob: b64daa25b3058f84fb593b4ffa7cead1a9f1ab72 [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"
22#include "llvm/iOther.h"
23#include "llvm/Type.h"
24#include "llvm/ConstPoolVals.h"
25
26using analysis::ExprType;
27
28
29static bool isLoopInvariant(const Value *V, const cfg::Loop *L) {
30 if (isa<ConstPoolVal>(V) || isa<MethodArgument>(V) || isa<GlobalValue>(V))
31 return true;
32
33 const Instruction *I = cast<Instruction>(V);
34 const BasicBlock *BB = I->getParent();
35
36 return !L->contains(BB);
37}
38
39enum InductionVariable::iType
40InductionVariable::Classify(const Value *Start, const Value *Step,
41 const cfg::Loop *L = 0) {
42 // Check for cannonical and simple linear expressions now...
43 if (ConstPoolInt *CStart = dyn_cast<ConstPoolInt>(Start))
44 if (ConstPoolInt *CStep = dyn_cast<ConstPoolInt>(Step)) {
45 if (CStart->equalsInt(0) && CStep->equalsInt(1))
46 return Cannonical;
47 else
48 return SimpleLinear;
49 }
50
51 // Without loop information, we cannot do any better, so bail now...
52 if (L == 0) return Unknown;
53
54 if (isLoopInvariant(Start, L) && isLoopInvariant(Step, L))
55 return Linear;
56 return Unknown;
57}
58
59// Create an induction variable for the specified value. If it is a PHI, and
60// if it's recognizable, classify it and fill in instance variables.
61//
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000062InductionVariable::InductionVariable(PHINode *P, cfg::LoopInfo *LoopInfo) {
Chris Lattner0bbe58f2001-11-26 18:41:20 +000063 InductionType = Unknown; // Assume the worst
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000064 Phi = P;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000065
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000066 // If the PHI node has more than two predecessors, we don't know how to
Chris Lattner0bbe58f2001-11-26 18:41:20 +000067 // handle it.
68 //
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000069 if (Phi->getNumIncomingValues() != 2) return;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000070
71 // If we have loop information, make sure that this PHI node is in the header
72 // of a loop...
73 //
74 const cfg::Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
75 if (L && L->getHeader() != Phi->getParent())
76 return;
77
78 Value *V1 = Phi->getIncomingValue(0);
79 Value *V2 = Phi->getIncomingValue(1);
80
81 if (L == 0) { // No loop information? Base everything on expression analysis
82 ExprType E1 = analysis::ClassifyExpression(V1);
83 ExprType E2 = analysis::ClassifyExpression(V2);
84
85 if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression
86 swap(E1, E2);
87
88 // E1 must be a constant incoming value, and E2 must be a linear expression
89 // with respect to the PHI node.
90 //
91 if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear ||
92 E2.Var != Phi)
93 return;
94
95 // Okay, we have found an induction variable. Save the start and step values
96 const Type *ETy = Phi->getType();
97 if (ETy->isPointerType()) ETy = Type::ULongTy;
98
99 Start = (Value*)(E1.Offset ? E1.Offset : ConstPoolInt::get(ETy, 0));
100 Step = (Value*)(E2.Offset ? E2.Offset : ConstPoolInt::get(ETy, 0));
101 } else {
102 // Okay, at this point, we know that we have loop information...
103
104 // Make sure that V1 is the incoming value, and V2 is from the backedge of
105 // the loop.
106 if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now.
107 swap(V1, V2);
108
109 Start = V1; // We know that Start has to be loop invariant...
110 Step = 0;
111
112 if (V2 == Phi) { // referencing the PHI directly? Must have zero step
113 Step = ConstPoolVal::getNullConstant(Phi->getType());
114 } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) {
115 // TODO: This could be much better...
116 if (I->getOpcode() == Instruction::Add) {
117 if (I->getOperand(0) == Phi)
118 Step = I->getOperand(1);
119 else if (I->getOperand(1) == Phi)
120 Step = I->getOperand(0);
121 }
122 }
123
124 if (Step == 0) { // Unrecognized step value...
125 ExprType StepE = analysis::ClassifyExpression(V2);
126 if (StepE.ExprTy != ExprType::Linear ||
127 StepE.Var != Phi) return;
128
129 const Type *ETy = Phi->getType();
130 if (ETy->isPointerType()) ETy = Type::ULongTy;
131 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstPoolInt::get(ETy, 0));
132 }
133 }
134
135 // Classify the induction variable type now...
136 InductionType = InductionVariable::Classify(Start, Step, L);
137}