blob: 3119d31ded625585c8589116a5fa8ed562de331e [file] [log] [blame]
Chris Lattnerf6e5233f2003-09-10 05:08:19 +00001//===- InductionVariable.cpp - Induction variable classification ----------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner0bbe58f2001-11-26 18:41:20 +00009//
Chris Lattnerf6e5233f2003-09-10 05:08:19 +000010// This file implements identification and classification of induction
11// variables. Induction variables must contain a PHI node that exists in a
12// loop header. Because of this, they are identified an managed by this PHI
13// node.
Chris Lattner0bbe58f2001-11-26 18:41:20 +000014//
15// Induction variables are classified into a type. Knowing that an induction
16// variable is of a specific type can constrain the values of the start and
17// step. For example, a SimpleLinear induction variable must have a start and
18// step values that are constants.
19//
20// Induction variables can be created with or without loop information. If no
21// loop information is available, induction variables cannot be recognized to be
22// more than SimpleLinear variables.
23//
24//===----------------------------------------------------------------------===//
25
26#include "llvm/Analysis/InductionVariable.h"
27#include "llvm/Analysis/LoopInfo.h"
28#include "llvm/Analysis/Expressions.h"
Misha Brukmana2722902002-10-11 05:34:32 +000029#include "llvm/BasicBlock.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000030#include "llvm/iPHINode.h"
Misha Brukmana2722902002-10-11 05:34:32 +000031#include "llvm/iOperators.h"
32#include "llvm/iTerminators.h"
Chris Lattner0bbe58f2001-11-26 18:41:20 +000033#include "llvm/Type.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000034#include "llvm/Constants.h"
Misha Brukmana2722902002-10-11 05:34:32 +000035#include "llvm/Support/CFG.h"
Chris Lattnera59cbb22002-07-27 01:12:17 +000036#include "llvm/Assembly/Writer.h"
Chris Lattner6806f562003-08-01 22:15:03 +000037#include "Support/Debug.h"
Chris Lattner0bbe58f2001-11-26 18:41:20 +000038
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000039static bool isLoopInvariant(const Value *V, const Loop *L) {
Chris Lattner36836a62003-09-10 04:49:10 +000040 if (const Instruction *I = dyn_cast<Instruction>(V))
41 return !L->contains(I->getParent());
42 // non-instructions all dominate instructions/blocks
43 return true;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000044}
45
46enum InductionVariable::iType
47InductionVariable::Classify(const Value *Start, const Value *Step,
Misha Brukmana2722902002-10-11 05:34:32 +000048 const Loop *L) {
Chris Lattner69ecd0d2003-09-10 05:24:09 +000049 // Check for canonical and simple linear expressions now...
Chris Lattner7e708292002-06-25 16:13:24 +000050 if (const ConstantInt *CStart = dyn_cast<ConstantInt>(Start))
51 if (const ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) {
Chris Lattner36836a62003-09-10 04:49:10 +000052 if (CStart->isNullValue() && CStep->equalsInt(1))
Chris Lattner69ecd0d2003-09-10 05:24:09 +000053 return Canonical;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000054 else
Misha Brukmana2722902002-10-11 05:34:32 +000055 return SimpleLinear;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000056 }
57
58 // Without loop information, we cannot do any better, so bail now...
59 if (L == 0) return Unknown;
60
61 if (isLoopInvariant(Start, L) && isLoopInvariant(Step, L))
62 return Linear;
63 return Unknown;
64}
65
66// Create an induction variable for the specified value. If it is a PHI, and
67// if it's recognizable, classify it and fill in instance variables.
68//
Misha Brukmana2722902002-10-11 05:34:32 +000069InductionVariable::InductionVariable(PHINode *P, LoopInfo *LoopInfo): End(0) {
Chris Lattner0bbe58f2001-11-26 18:41:20 +000070 InductionType = Unknown; // Assume the worst
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000071 Phi = P;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000072
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000073 // If the PHI node has more than two predecessors, we don't know how to
Chris Lattner0bbe58f2001-11-26 18:41:20 +000074 // handle it.
75 //
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000076 if (Phi->getNumIncomingValues() != 2) return;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000077
Chris Lattner6de230a2001-12-05 06:32:30 +000078 // FIXME: Handle FP induction variables.
79 if (Phi->getType() == Type::FloatTy || Phi->getType() == Type::DoubleTy)
80 return;
81
Chris Lattner0bbe58f2001-11-26 18:41:20 +000082 // If we have loop information, make sure that this PHI node is in the header
83 // of a loop...
84 //
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000085 const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000086 if (L && L->getHeader() != Phi->getParent())
87 return;
88
89 Value *V1 = Phi->getIncomingValue(0);
90 Value *V2 = Phi->getIncomingValue(1);
91
92 if (L == 0) { // No loop information? Base everything on expression analysis
Chris Lattnerc74cb862002-08-30 22:53:53 +000093 ExprType E1 = ClassifyExpression(V1);
94 ExprType E2 = ClassifyExpression(V2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +000095
96 if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression
Chris Lattner697954c2002-01-20 22:54:45 +000097 std::swap(E1, E2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +000098
99 // E1 must be a constant incoming value, and E2 must be a linear expression
100 // with respect to the PHI node.
101 //
102 if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear ||
Misha Brukmana2722902002-10-11 05:34:32 +0000103 E2.Var != Phi)
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000104 return;
105
106 // Okay, we have found an induction variable. Save the start and step values
107 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000108 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000109
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000110 Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0));
111 Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0));
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000112 } else {
113 // Okay, at this point, we know that we have loop information...
114
115 // Make sure that V1 is the incoming value, and V2 is from the backedge of
116 // the loop.
117 if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now.
Chris Lattner697954c2002-01-20 22:54:45 +0000118 std::swap(V1, V2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000119
120 Start = V1; // We know that Start has to be loop invariant...
121 Step = 0;
122
123 if (V2 == Phi) { // referencing the PHI directly? Must have zero step
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000124 Step = Constant::getNullValue(Phi->getType());
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000125 } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) {
126 // TODO: This could be much better...
127 if (I->getOpcode() == Instruction::Add) {
Misha Brukmana2722902002-10-11 05:34:32 +0000128 if (I->getOperand(0) == Phi)
129 Step = I->getOperand(1);
130 else if (I->getOperand(1) == Phi)
131 Step = I->getOperand(0);
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000132 }
133 }
134
135 if (Step == 0) { // Unrecognized step value...
Chris Lattnerc74cb862002-08-30 22:53:53 +0000136 ExprType StepE = ClassifyExpression(V2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000137 if (StepE.ExprTy != ExprType::Linear ||
Misha Brukmana2722902002-10-11 05:34:32 +0000138 StepE.Var != Phi) return;
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000139
140 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000141 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000142 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0));
Chris Lattner621c9922001-12-04 08:12:47 +0000143 } else { // We were able to get a step value, simplify with expr analysis
Chris Lattnerc74cb862002-08-30 22:53:53 +0000144 ExprType StepE = ClassifyExpression(Step);
Chris Lattner621c9922001-12-04 08:12:47 +0000145 if (StepE.ExprTy == ExprType::Linear && StepE.Offset == 0) {
146 // No offset from variable? Grab the variable
147 Step = StepE.Var;
148 } else if (StepE.ExprTy == ExprType::Constant) {
149 if (StepE.Offset)
150 Step = (Value*)StepE.Offset;
151 else
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000152 Step = Constant::getNullValue(Step->getType());
Chris Lattner6de230a2001-12-05 06:32:30 +0000153 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000154 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner6de230a2001-12-05 06:32:30 +0000155 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy,0));
Chris Lattner621c9922001-12-04 08:12:47 +0000156 }
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000157 }
158 }
159
160 // Classify the induction variable type now...
161 InductionType = InductionVariable::Classify(Start, Step, L);
162}
Chris Lattnera59cbb22002-07-27 01:12:17 +0000163
Misha Brukmana2722902002-10-11 05:34:32 +0000164
Chris Lattner44abf852003-09-10 14:51:49 +0000165Value *InductionVariable::getExecutionCount(LoopInfo *LoopInfo) {
166 if (InductionType != Canonical) return 0;
167
Misha Brukmana2722902002-10-11 05:34:32 +0000168 DEBUG(std::cerr << "entering getExecutionCount\n");
169
170 // Don't recompute if already available
171 if (End) {
172 DEBUG(std::cerr << "returning cached End value.\n");
173 return End;
174 }
175
176 const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
177 if (!L) {
178 DEBUG(std::cerr << "null loop. oops\n");
Chris Lattner44abf852003-09-10 14:51:49 +0000179 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000180 }
181
182 // >1 backedge => cannot predict number of iterations
183 if (Phi->getNumIncomingValues() != 2) {
184 DEBUG(std::cerr << ">2 incoming values. oops\n");
Chris Lattner44abf852003-09-10 14:51:49 +0000185 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000186 }
187
Misha Brukman2f2d0652003-09-11 18:14:24 +0000188 // Find final node: predecessor of the loop header that's also an exit
Chris Lattner0006bd72002-11-09 00:49:43 +0000189 BasicBlock *terminator = 0;
Chris Lattner44abf852003-09-10 14:51:49 +0000190 for (pred_iterator PI = pred_begin(L->getHeader()),
191 PE = pred_end(L->getHeader()); PI != PE; ++PI)
Misha Brukmana2722902002-10-11 05:34:32 +0000192 if (L->isLoopExit(*PI)) {
193 terminator = *PI;
194 break;
195 }
Misha Brukmana2722902002-10-11 05:34:32 +0000196
197 // Break in the loop => cannot predict number of iterations
198 // break: any block which is an exit node whose successor is not in loop,
199 // and this block is not marked as the terminator
200 //
201 const std::vector<BasicBlock*> &blocks = L->getBlocks();
Chris Lattner44abf852003-09-10 14:51:49 +0000202 for (std::vector<BasicBlock*>::const_iterator I = blocks.begin(),
203 e = blocks.end(); I != e; ++I)
204 if (L->isLoopExit(*I) && *I != terminator)
205 for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
206 if (!L->contains(*SI)) {
Misha Brukmana2722902002-10-11 05:34:32 +0000207 DEBUG(std::cerr << "break found in loop");
Chris Lattner44abf852003-09-10 14:51:49 +0000208 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000209 }
Misha Brukmana2722902002-10-11 05:34:32 +0000210
211 BranchInst *B = dyn_cast<BranchInst>(terminator->getTerminator());
212 if (!B) {
Chris Lattner44abf852003-09-10 14:51:49 +0000213 DEBUG(std::cerr << "Terminator is not a cond branch!");
214 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000215 }
Chris Lattner2ee82e02003-04-23 16:36:11 +0000216 SetCondInst *SCI = dyn_cast<SetCondInst>(B->getCondition());
Chris Lattner44abf852003-09-10 14:51:49 +0000217 if (!SCI) {
218 DEBUG(std::cerr << "Not a cond branch on setcc!\n");
219 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000220 }
Chris Lattner44abf852003-09-10 14:51:49 +0000221
222 DEBUG(std::cerr << "sci:" << *SCI);
223 Value *condVal0 = SCI->getOperand(0);
224 Value *condVal1 = SCI->getOperand(1);
Chris Lattner44abf852003-09-10 14:51:49 +0000225
Chris Lattnera176a8b2003-09-10 14:55:05 +0000226 // The induction variable is the one coming from the backedge
227 Value *indVar = Phi->getIncomingValue(L->contains(Phi->getIncomingBlock(1)));
Chris Lattner44abf852003-09-10 14:51:49 +0000228
229
230 // Check to see if indVar is one of the parameters in SCI and if the other is
231 // loop-invariant, it is the UB
232 if (indVar == condVal0) {
233 if (isLoopInvariant(condVal1, L))
234 End = condVal1;
235 else {
236 DEBUG(std::cerr << "not loop invariant 1\n");
237 return 0;
238 }
239 } else if (indVar == condVal1) {
240 if (isLoopInvariant(condVal0, L))
241 End = condVal0;
242 else {
243 DEBUG(std::cerr << "not loop invariant 0\n");
244 return 0;
245 }
246 } else {
247 DEBUG(std::cerr << "Loop condition doesn't directly uses indvar\n");
248 return 0;
249 }
250
251 switch (SCI->getOpcode()) {
252 case Instruction::SetLT:
253 case Instruction::SetNE: return End; // already done
254 case Instruction::SetLE:
255 // if compared to a constant int N, then predict N+1 iterations
256 if (ConstantSInt *ubSigned = dyn_cast<ConstantSInt>(End)) {
257 DEBUG(std::cerr << "signed int constant\n");
258 return ConstantSInt::get(ubSigned->getType(), ubSigned->getValue()+1);
259 } else if (ConstantUInt *ubUnsigned = dyn_cast<ConstantUInt>(End)) {
260 DEBUG(std::cerr << "unsigned int constant\n");
261 return ConstantUInt::get(ubUnsigned->getType(),
262 ubUnsigned->getValue()+1);
263 } else {
264 DEBUG(std::cerr << "symbolic bound\n");
265 // new expression N+1, insert right before the SCI. FIXME: If End is loop
266 // invariant, then so is this expression. We should insert it in the loop
267 // preheader if it exists.
268 return BinaryOperator::create(Instruction::Add, End,
269 ConstantInt::get(End->getType(), 1),
270 "tripcount", SCI);
271 }
272
273 default:
274 return 0; // cannot predict
275 }
Misha Brukmana2722902002-10-11 05:34:32 +0000276}
277
278
Chris Lattnera59cbb22002-07-27 01:12:17 +0000279void InductionVariable::print(std::ostream &o) const {
280 switch (InductionType) {
Chris Lattner69ecd0d2003-09-10 05:24:09 +0000281 case InductionVariable::Canonical: o << "Canonical "; break;
Chris Lattnera59cbb22002-07-27 01:12:17 +0000282 case InductionVariable::SimpleLinear: o << "SimpleLinear "; break;
283 case InductionVariable::Linear: o << "Linear "; break;
284 case InductionVariable::Unknown: o << "Unrecognized "; break;
285 }
Chris Lattner74493a42002-09-10 15:35:39 +0000286 o << "Induction Variable: ";
Chris Lattnera59cbb22002-07-27 01:12:17 +0000287 if (Phi) {
288 WriteAsOperand(o, Phi);
289 o << ":\n" << Phi;
290 } else {
291 o << "\n";
292 }
293 if (InductionType == InductionVariable::Unknown) return;
294
Chris Lattner74493a42002-09-10 15:35:39 +0000295 o << " Start = "; WriteAsOperand(o, Start);
296 o << " Step = " ; WriteAsOperand(o, Step);
Misha Brukmana2722902002-10-11 05:34:32 +0000297 if (End) {
298 o << " End = " ; WriteAsOperand(o, End);
299 }
Chris Lattnera59cbb22002-07-27 01:12:17 +0000300 o << "\n";
301}