blob: 4c53c967863cfb7e01673ed2eb8e9fc787eaf4a7 [file] [log] [blame]
Chris Lattner19b34d12003-09-10 05:08:19 +00001//===- InductionVariable.cpp - Induction variable classification ----------===//
Chris Lattner6de99422001-11-26 18:41:20 +00002//
Chris Lattner19b34d12003-09-10 05:08:19 +00003// This file implements identification and classification of induction
4// variables. Induction variables must contain a PHI node that exists in a
5// loop header. Because of this, they are identified an managed by this PHI
6// node.
Chris Lattner6de99422001-11-26 18:41:20 +00007//
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"
Misha Brukman33022f02002-10-11 05:34:32 +000022#include "llvm/BasicBlock.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000023#include "llvm/iPHINode.h"
Misha Brukman33022f02002-10-11 05:34:32 +000024#include "llvm/iOperators.h"
25#include "llvm/iTerminators.h"
Chris Lattner6de99422001-11-26 18:41:20 +000026#include "llvm/Type.h"
Chris Lattnerca142372002-04-28 19:55:58 +000027#include "llvm/Constants.h"
Misha Brukman33022f02002-10-11 05:34:32 +000028#include "llvm/Support/CFG.h"
Chris Lattner26750072002-07-27 01:12:17 +000029#include "llvm/Assembly/Writer.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000030#include "Support/Debug.h"
Chris Lattner6de99422001-11-26 18:41:20 +000031
Chris Lattner78dd56f2002-04-28 16:21:30 +000032static bool isLoopInvariant(const Value *V, const Loop *L) {
Chris Lattner42e7abc2003-09-10 04:49:10 +000033 if (const Instruction *I = dyn_cast<Instruction>(V))
34 return !L->contains(I->getParent());
35 // non-instructions all dominate instructions/blocks
36 return true;
Chris Lattner6de99422001-11-26 18:41:20 +000037}
38
39enum InductionVariable::iType
40InductionVariable::Classify(const Value *Start, const Value *Step,
Misha Brukman33022f02002-10-11 05:34:32 +000041 const Loop *L) {
Chris Lattner7ff9fe12003-09-10 05:24:09 +000042 // Check for canonical and simple linear expressions now...
Chris Lattner113f4f42002-06-25 16:13:24 +000043 if (const ConstantInt *CStart = dyn_cast<ConstantInt>(Start))
44 if (const ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) {
Chris Lattner42e7abc2003-09-10 04:49:10 +000045 if (CStart->isNullValue() && CStep->equalsInt(1))
Chris Lattner7ff9fe12003-09-10 05:24:09 +000046 return Canonical;
Chris Lattner6de99422001-11-26 18:41:20 +000047 else
Misha Brukman33022f02002-10-11 05:34:32 +000048 return SimpleLinear;
Chris Lattner6de99422001-11-26 18:41:20 +000049 }
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//
Misha Brukman33022f02002-10-11 05:34:32 +000062InductionVariable::InductionVariable(PHINode *P, LoopInfo *LoopInfo): End(0) {
Chris Lattner6de99422001-11-26 18:41:20 +000063 InductionType = Unknown; // Assume the worst
Chris Lattner66d9cea2001-12-03 17:27:42 +000064 Phi = P;
Chris Lattner6de99422001-11-26 18:41:20 +000065
Chris Lattner66d9cea2001-12-03 17:27:42 +000066 // If the PHI node has more than two predecessors, we don't know how to
Chris Lattner6de99422001-11-26 18:41:20 +000067 // handle it.
68 //
Chris Lattner66d9cea2001-12-03 17:27:42 +000069 if (Phi->getNumIncomingValues() != 2) return;
Chris Lattner6de99422001-11-26 18:41:20 +000070
Chris Lattner1bedac92001-12-05 06:32:30 +000071 // FIXME: Handle FP induction variables.
72 if (Phi->getType() == Type::FloatTy || Phi->getType() == Type::DoubleTy)
73 return;
74
Chris Lattner6de99422001-11-26 18:41:20 +000075 // If we have loop information, make sure that this PHI node is in the header
76 // of a loop...
77 //
Chris Lattner78dd56f2002-04-28 16:21:30 +000078 const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
Chris Lattner6de99422001-11-26 18:41:20 +000079 if (L && L->getHeader() != Phi->getParent())
80 return;
81
82 Value *V1 = Phi->getIncomingValue(0);
83 Value *V2 = Phi->getIncomingValue(1);
84
85 if (L == 0) { // No loop information? Base everything on expression analysis
Chris Lattnered39cca2002-08-30 22:53:53 +000086 ExprType E1 = ClassifyExpression(V1);
87 ExprType E2 = ClassifyExpression(V2);
Chris Lattner6de99422001-11-26 18:41:20 +000088
89 if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression
Chris Lattner7f74a562002-01-20 22:54:45 +000090 std::swap(E1, E2);
Chris Lattner6de99422001-11-26 18:41:20 +000091
92 // E1 must be a constant incoming value, and E2 must be a linear expression
93 // with respect to the PHI node.
94 //
95 if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear ||
Misha Brukman33022f02002-10-11 05:34:32 +000096 E2.Var != Phi)
Chris Lattner6de99422001-11-26 18:41:20 +000097 return;
98
99 // Okay, we have found an induction variable. Save the start and step values
100 const Type *ETy = Phi->getType();
Chris Lattner181cc322002-05-06 16:15:30 +0000101 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner6de99422001-11-26 18:41:20 +0000102
Chris Lattner3462ae32001-12-03 22:26:30 +0000103 Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0));
104 Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0));
Chris Lattner6de99422001-11-26 18:41:20 +0000105 } else {
106 // Okay, at this point, we know that we have loop information...
107
108 // Make sure that V1 is the incoming value, and V2 is from the backedge of
109 // the loop.
110 if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now.
Chris Lattner7f74a562002-01-20 22:54:45 +0000111 std::swap(V1, V2);
Chris Lattner6de99422001-11-26 18:41:20 +0000112
113 Start = V1; // We know that Start has to be loop invariant...
114 Step = 0;
115
116 if (V2 == Phi) { // referencing the PHI directly? Must have zero step
Chris Lattner2716b5e2002-04-27 02:25:14 +0000117 Step = Constant::getNullValue(Phi->getType());
Chris Lattner6de99422001-11-26 18:41:20 +0000118 } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) {
119 // TODO: This could be much better...
120 if (I->getOpcode() == Instruction::Add) {
Misha Brukman33022f02002-10-11 05:34:32 +0000121 if (I->getOperand(0) == Phi)
122 Step = I->getOperand(1);
123 else if (I->getOperand(1) == Phi)
124 Step = I->getOperand(0);
Chris Lattner6de99422001-11-26 18:41:20 +0000125 }
126 }
127
128 if (Step == 0) { // Unrecognized step value...
Chris Lattnered39cca2002-08-30 22:53:53 +0000129 ExprType StepE = ClassifyExpression(V2);
Chris Lattner6de99422001-11-26 18:41:20 +0000130 if (StepE.ExprTy != ExprType::Linear ||
Misha Brukman33022f02002-10-11 05:34:32 +0000131 StepE.Var != Phi) return;
Chris Lattner6de99422001-11-26 18:41:20 +0000132
133 const Type *ETy = Phi->getType();
Chris Lattner181cc322002-05-06 16:15:30 +0000134 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner3462ae32001-12-03 22:26:30 +0000135 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0));
Chris Lattner54794fe2001-12-04 08:12:47 +0000136 } else { // We were able to get a step value, simplify with expr analysis
Chris Lattnered39cca2002-08-30 22:53:53 +0000137 ExprType StepE = ClassifyExpression(Step);
Chris Lattner54794fe2001-12-04 08:12:47 +0000138 if (StepE.ExprTy == ExprType::Linear && StepE.Offset == 0) {
139 // No offset from variable? Grab the variable
140 Step = StepE.Var;
141 } else if (StepE.ExprTy == ExprType::Constant) {
142 if (StepE.Offset)
143 Step = (Value*)StepE.Offset;
144 else
Chris Lattner2716b5e2002-04-27 02:25:14 +0000145 Step = Constant::getNullValue(Step->getType());
Chris Lattner1bedac92001-12-05 06:32:30 +0000146 const Type *ETy = Phi->getType();
Chris Lattner181cc322002-05-06 16:15:30 +0000147 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner1bedac92001-12-05 06:32:30 +0000148 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy,0));
Chris Lattner54794fe2001-12-04 08:12:47 +0000149 }
Chris Lattner6de99422001-11-26 18:41:20 +0000150 }
151 }
152
153 // Classify the induction variable type now...
154 InductionType = InductionVariable::Classify(Start, Step, L);
155}
Chris Lattner26750072002-07-27 01:12:17 +0000156
Misha Brukman33022f02002-10-11 05:34:32 +0000157
Chris Lattner960a8572003-09-10 14:51:49 +0000158Value *InductionVariable::getExecutionCount(LoopInfo *LoopInfo) {
159 if (InductionType != Canonical) return 0;
160
Misha Brukman33022f02002-10-11 05:34:32 +0000161 DEBUG(std::cerr << "entering getExecutionCount\n");
162
163 // Don't recompute if already available
164 if (End) {
165 DEBUG(std::cerr << "returning cached End value.\n");
166 return End;
167 }
168
169 const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
170 if (!L) {
171 DEBUG(std::cerr << "null loop. oops\n");
Chris Lattner960a8572003-09-10 14:51:49 +0000172 return 0;
Misha Brukman33022f02002-10-11 05:34:32 +0000173 }
174
175 // >1 backedge => cannot predict number of iterations
176 if (Phi->getNumIncomingValues() != 2) {
177 DEBUG(std::cerr << ">2 incoming values. oops\n");
Chris Lattner960a8572003-09-10 14:51:49 +0000178 return 0;
Misha Brukman33022f02002-10-11 05:34:32 +0000179 }
180
181 // Find final node: predecesor of the loop header that's also an exit
Chris Lattnera2620ac2002-11-09 00:49:43 +0000182 BasicBlock *terminator = 0;
Chris Lattner960a8572003-09-10 14:51:49 +0000183 for (pred_iterator PI = pred_begin(L->getHeader()),
184 PE = pred_end(L->getHeader()); PI != PE; ++PI)
Misha Brukman33022f02002-10-11 05:34:32 +0000185 if (L->isLoopExit(*PI)) {
186 terminator = *PI;
187 break;
188 }
Misha Brukman33022f02002-10-11 05:34:32 +0000189
190 // Break in the loop => cannot predict number of iterations
191 // break: any block which is an exit node whose successor is not in loop,
192 // and this block is not marked as the terminator
193 //
194 const std::vector<BasicBlock*> &blocks = L->getBlocks();
Chris Lattner960a8572003-09-10 14:51:49 +0000195 for (std::vector<BasicBlock*>::const_iterator I = blocks.begin(),
196 e = blocks.end(); I != e; ++I)
197 if (L->isLoopExit(*I) && *I != terminator)
198 for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
199 if (!L->contains(*SI)) {
Misha Brukman33022f02002-10-11 05:34:32 +0000200 DEBUG(std::cerr << "break found in loop");
Chris Lattner960a8572003-09-10 14:51:49 +0000201 return 0;
Misha Brukman33022f02002-10-11 05:34:32 +0000202 }
Misha Brukman33022f02002-10-11 05:34:32 +0000203
204 BranchInst *B = dyn_cast<BranchInst>(terminator->getTerminator());
205 if (!B) {
Chris Lattner960a8572003-09-10 14:51:49 +0000206 DEBUG(std::cerr << "Terminator is not a cond branch!");
207 return 0;
Misha Brukman33022f02002-10-11 05:34:32 +0000208 }
Chris Lattner6ee2cf52003-04-23 16:36:11 +0000209 SetCondInst *SCI = dyn_cast<SetCondInst>(B->getCondition());
Chris Lattner960a8572003-09-10 14:51:49 +0000210 if (!SCI) {
211 DEBUG(std::cerr << "Not a cond branch on setcc!\n");
212 return 0;
Misha Brukman33022f02002-10-11 05:34:32 +0000213 }
Chris Lattner960a8572003-09-10 14:51:49 +0000214
215 DEBUG(std::cerr << "sci:" << *SCI);
216 Value *condVal0 = SCI->getOperand(0);
217 Value *condVal1 = SCI->getOperand(1);
218 Value *indVar = 0;
219
220 // the induction variable is the one coming from the backedge
221 indVar = Phi->getIncomingValue(L->contains(Phi->getIncomingBlock(1)));
222
223
224 // Check to see if indVar is one of the parameters in SCI and if the other is
225 // loop-invariant, it is the UB
226 if (indVar == condVal0) {
227 if (isLoopInvariant(condVal1, L))
228 End = condVal1;
229 else {
230 DEBUG(std::cerr << "not loop invariant 1\n");
231 return 0;
232 }
233 } else if (indVar == condVal1) {
234 if (isLoopInvariant(condVal0, L))
235 End = condVal0;
236 else {
237 DEBUG(std::cerr << "not loop invariant 0\n");
238 return 0;
239 }
240 } else {
241 DEBUG(std::cerr << "Loop condition doesn't directly uses indvar\n");
242 return 0;
243 }
244
245 switch (SCI->getOpcode()) {
246 case Instruction::SetLT:
247 case Instruction::SetNE: return End; // already done
248 case Instruction::SetLE:
249 // if compared to a constant int N, then predict N+1 iterations
250 if (ConstantSInt *ubSigned = dyn_cast<ConstantSInt>(End)) {
251 DEBUG(std::cerr << "signed int constant\n");
252 return ConstantSInt::get(ubSigned->getType(), ubSigned->getValue()+1);
253 } else if (ConstantUInt *ubUnsigned = dyn_cast<ConstantUInt>(End)) {
254 DEBUG(std::cerr << "unsigned int constant\n");
255 return ConstantUInt::get(ubUnsigned->getType(),
256 ubUnsigned->getValue()+1);
257 } else {
258 DEBUG(std::cerr << "symbolic bound\n");
259 // new expression N+1, insert right before the SCI. FIXME: If End is loop
260 // invariant, then so is this expression. We should insert it in the loop
261 // preheader if it exists.
262 return BinaryOperator::create(Instruction::Add, End,
263 ConstantInt::get(End->getType(), 1),
264 "tripcount", SCI);
265 }
266
267 default:
268 return 0; // cannot predict
269 }
Misha Brukman33022f02002-10-11 05:34:32 +0000270}
271
272
Chris Lattner26750072002-07-27 01:12:17 +0000273void InductionVariable::print(std::ostream &o) const {
274 switch (InductionType) {
Chris Lattner7ff9fe12003-09-10 05:24:09 +0000275 case InductionVariable::Canonical: o << "Canonical "; break;
Chris Lattner26750072002-07-27 01:12:17 +0000276 case InductionVariable::SimpleLinear: o << "SimpleLinear "; break;
277 case InductionVariable::Linear: o << "Linear "; break;
278 case InductionVariable::Unknown: o << "Unrecognized "; break;
279 }
Chris Lattnere1751db2002-09-10 15:35:39 +0000280 o << "Induction Variable: ";
Chris Lattner26750072002-07-27 01:12:17 +0000281 if (Phi) {
282 WriteAsOperand(o, Phi);
283 o << ":\n" << Phi;
284 } else {
285 o << "\n";
286 }
287 if (InductionType == InductionVariable::Unknown) return;
288
Chris Lattnere1751db2002-09-10 15:35:39 +0000289 o << " Start = "; WriteAsOperand(o, Start);
290 o << " Step = " ; WriteAsOperand(o, Step);
Misha Brukman33022f02002-10-11 05:34:32 +0000291 if (End) {
292 o << " End = " ; WriteAsOperand(o, End);
293 }
Chris Lattner26750072002-07-27 01:12:17 +0000294 o << "\n";
295}