blob: 8f622973f96bcd54678da4e410a51b049bd8b43e [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 Lattner4c307d42003-12-22 05:26:29 +000030#include "llvm/Instructions.h"
Chris Lattner0bbe58f2001-11-26 18:41:20 +000031#include "llvm/Type.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000032#include "llvm/Constants.h"
Misha Brukmana2722902002-10-11 05:34:32 +000033#include "llvm/Support/CFG.h"
Chris Lattnera59cbb22002-07-27 01:12:17 +000034#include "llvm/Assembly/Writer.h"
Chris Lattner6806f562003-08-01 22:15:03 +000035#include "Support/Debug.h"
Chris Lattner4c307d42003-12-22 05:26:29 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000038static bool isLoopInvariant(const Value *V, const Loop *L) {
Chris Lattner36836a62003-09-10 04:49:10 +000039 if (const Instruction *I = dyn_cast<Instruction>(V))
40 return !L->contains(I->getParent());
41 // non-instructions all dominate instructions/blocks
42 return true;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000043}
44
45enum InductionVariable::iType
46InductionVariable::Classify(const Value *Start, const Value *Step,
Misha Brukmana2722902002-10-11 05:34:32 +000047 const Loop *L) {
Chris Lattner69ecd0d2003-09-10 05:24:09 +000048 // Check for canonical and simple linear expressions now...
Chris Lattner7e708292002-06-25 16:13:24 +000049 if (const ConstantInt *CStart = dyn_cast<ConstantInt>(Start))
50 if (const ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) {
Chris Lattner36836a62003-09-10 04:49:10 +000051 if (CStart->isNullValue() && CStep->equalsInt(1))
Chris Lattner69ecd0d2003-09-10 05:24:09 +000052 return Canonical;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000053 else
Misha Brukmana2722902002-10-11 05:34:32 +000054 return SimpleLinear;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000055 }
56
57 // Without loop information, we cannot do any better, so bail now...
58 if (L == 0) return Unknown;
59
60 if (isLoopInvariant(Start, L) && isLoopInvariant(Step, L))
61 return Linear;
62 return Unknown;
63}
64
65// Create an induction variable for the specified value. If it is a PHI, and
66// if it's recognizable, classify it and fill in instance variables.
67//
Misha Brukmana2722902002-10-11 05:34:32 +000068InductionVariable::InductionVariable(PHINode *P, LoopInfo *LoopInfo): End(0) {
Chris Lattner0bbe58f2001-11-26 18:41:20 +000069 InductionType = Unknown; // Assume the worst
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000070 Phi = P;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000071
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000072 // If the PHI node has more than two predecessors, we don't know how to
Chris Lattner0bbe58f2001-11-26 18:41:20 +000073 // handle it.
74 //
Chris Lattnerdf89f6e2001-12-03 17:27:42 +000075 if (Phi->getNumIncomingValues() != 2) return;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000076
Chris Lattner6de230a2001-12-05 06:32:30 +000077 // FIXME: Handle FP induction variables.
78 if (Phi->getType() == Type::FloatTy || Phi->getType() == Type::DoubleTy)
79 return;
80
Chris Lattner0bbe58f2001-11-26 18:41:20 +000081 // If we have loop information, make sure that this PHI node is in the header
82 // of a loop...
83 //
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000084 const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
Chris Lattner0bbe58f2001-11-26 18:41:20 +000085 if (L && L->getHeader() != Phi->getParent())
86 return;
87
88 Value *V1 = Phi->getIncomingValue(0);
89 Value *V2 = Phi->getIncomingValue(1);
90
91 if (L == 0) { // No loop information? Base everything on expression analysis
Chris Lattner9a0a41f2003-12-23 08:04:08 +000092 ExprType E1 = ClassifyExpr(V1);
93 ExprType E2 = ClassifyExpr(V2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +000094
95 if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression
Chris Lattner697954c2002-01-20 22:54:45 +000096 std::swap(E1, E2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +000097
98 // E1 must be a constant incoming value, and E2 must be a linear expression
99 // with respect to the PHI node.
100 //
101 if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear ||
Misha Brukmana2722902002-10-11 05:34:32 +0000102 E2.Var != Phi)
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000103 return;
104
105 // Okay, we have found an induction variable. Save the start and step values
106 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000107 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000108
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000109 Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0));
110 Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0));
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000111 } else {
112 // Okay, at this point, we know that we have loop information...
113
114 // Make sure that V1 is the incoming value, and V2 is from the backedge of
115 // the loop.
116 if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now.
Chris Lattner697954c2002-01-20 22:54:45 +0000117 std::swap(V1, V2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000118
119 Start = V1; // We know that Start has to be loop invariant...
120 Step = 0;
121
122 if (V2 == Phi) { // referencing the PHI directly? Must have zero step
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000123 Step = Constant::getNullValue(Phi->getType());
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000124 } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) {
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000125 if (I->getOpcode() == Instruction::Add) {
Misha Brukmana2722902002-10-11 05:34:32 +0000126 if (I->getOperand(0) == Phi)
127 Step = I->getOperand(1);
128 else if (I->getOperand(1) == Phi)
129 Step = I->getOperand(0);
Chris Lattner4c307d42003-12-22 05:26:29 +0000130 } else if (I->getOpcode() == Instruction::Sub &&
131 I->getOperand(0) == Phi) {
132 // If the incoming value is a constant, just form a constant negative
133 // step. Otherwise, negate the step outside of the loop and use it.
134 Value *V = I->getOperand(1);
135 Constant *Zero = Constant::getNullValue(V->getType());
136 if (Constant *CV = dyn_cast<Constant>(V))
137 Step = ConstantExpr::get(Instruction::Sub, Zero, CV);
138 else if (Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattnerc6b0c4b2004-03-10 21:42:19 +0000139 BasicBlock::iterator InsertPt = I;
140 for (++InsertPt; isa<PHINode>(InsertPt); ++InsertPt)
141 /*empty*/;
Chris Lattner4c307d42003-12-22 05:26:29 +0000142 Step = BinaryOperator::create(Instruction::Sub, Zero, V,
Chris Lattnerc6b0c4b2004-03-10 21:42:19 +0000143 V->getName()+".neg", InsertPt);
Chris Lattner4c307d42003-12-22 05:26:29 +0000144
145 } else {
Chris Lattnerc6b0c4b2004-03-10 21:42:19 +0000146 // Must be loop invariant
Chris Lattner4c307d42003-12-22 05:26:29 +0000147 Step = BinaryOperator::create(Instruction::Sub, Zero, V,
148 V->getName()+".neg",
149 Phi->getParent()->getParent()->begin()->begin());
150 }
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000151 }
Chris Lattner4c307d42003-12-22 05:26:29 +0000152 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V2)) {
153 if (GEP->getNumOperands() == 2 &&
154 GEP->getOperand(0) == Phi)
155 Step = GEP->getOperand(1);
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000156 }
157
158 if (Step == 0) { // Unrecognized step value...
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000159 ExprType StepE = ClassifyExpr(V2);
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000160 if (StepE.ExprTy != ExprType::Linear ||
Misha Brukmana2722902002-10-11 05:34:32 +0000161 StepE.Var != Phi) return;
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000162
163 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000164 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000165 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0));
Chris Lattner621c9922001-12-04 08:12:47 +0000166 } else { // We were able to get a step value, simplify with expr analysis
Chris Lattner9a0a41f2003-12-23 08:04:08 +0000167 ExprType StepE = ClassifyExpr(Step);
Chris Lattner621c9922001-12-04 08:12:47 +0000168 if (StepE.ExprTy == ExprType::Linear && StepE.Offset == 0) {
169 // No offset from variable? Grab the variable
170 Step = StepE.Var;
171 } else if (StepE.ExprTy == ExprType::Constant) {
172 if (StepE.Offset)
173 Step = (Value*)StepE.Offset;
174 else
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000175 Step = Constant::getNullValue(Step->getType());
Chris Lattner6de230a2001-12-05 06:32:30 +0000176 const Type *ETy = Phi->getType();
Chris Lattner9b625032002-05-06 16:15:30 +0000177 if (isa<PointerType>(ETy)) ETy = Type::ULongTy;
Chris Lattner6de230a2001-12-05 06:32:30 +0000178 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy,0));
Chris Lattner621c9922001-12-04 08:12:47 +0000179 }
Chris Lattner0bbe58f2001-11-26 18:41:20 +0000180 }
181 }
182
183 // Classify the induction variable type now...
184 InductionType = InductionVariable::Classify(Start, Step, L);
185}
Chris Lattnera59cbb22002-07-27 01:12:17 +0000186
Misha Brukmana2722902002-10-11 05:34:32 +0000187
Chris Lattner44abf852003-09-10 14:51:49 +0000188Value *InductionVariable::getExecutionCount(LoopInfo *LoopInfo) {
189 if (InductionType != Canonical) return 0;
190
Misha Brukmana2722902002-10-11 05:34:32 +0000191 DEBUG(std::cerr << "entering getExecutionCount\n");
192
193 // Don't recompute if already available
194 if (End) {
195 DEBUG(std::cerr << "returning cached End value.\n");
196 return End;
197 }
198
199 const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
200 if (!L) {
201 DEBUG(std::cerr << "null loop. oops\n");
Chris Lattner44abf852003-09-10 14:51:49 +0000202 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000203 }
204
205 // >1 backedge => cannot predict number of iterations
206 if (Phi->getNumIncomingValues() != 2) {
207 DEBUG(std::cerr << ">2 incoming values. oops\n");
Chris Lattner44abf852003-09-10 14:51:49 +0000208 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000209 }
210
Misha Brukman2f2d0652003-09-11 18:14:24 +0000211 // Find final node: predecessor of the loop header that's also an exit
Chris Lattner0006bd72002-11-09 00:49:43 +0000212 BasicBlock *terminator = 0;
Chris Lattner44abf852003-09-10 14:51:49 +0000213 for (pred_iterator PI = pred_begin(L->getHeader()),
214 PE = pred_end(L->getHeader()); PI != PE; ++PI)
Misha Brukmana2722902002-10-11 05:34:32 +0000215 if (L->isLoopExit(*PI)) {
216 terminator = *PI;
217 break;
218 }
Misha Brukmana2722902002-10-11 05:34:32 +0000219
220 // Break in the loop => cannot predict number of iterations
221 // break: any block which is an exit node whose successor is not in loop,
222 // and this block is not marked as the terminator
223 //
224 const std::vector<BasicBlock*> &blocks = L->getBlocks();
Chris Lattner44abf852003-09-10 14:51:49 +0000225 for (std::vector<BasicBlock*>::const_iterator I = blocks.begin(),
226 e = blocks.end(); I != e; ++I)
227 if (L->isLoopExit(*I) && *I != terminator)
228 for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
229 if (!L->contains(*SI)) {
Misha Brukmana2722902002-10-11 05:34:32 +0000230 DEBUG(std::cerr << "break found in loop");
Chris Lattner44abf852003-09-10 14:51:49 +0000231 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000232 }
Misha Brukmana2722902002-10-11 05:34:32 +0000233
234 BranchInst *B = dyn_cast<BranchInst>(terminator->getTerminator());
235 if (!B) {
Chris Lattner44abf852003-09-10 14:51:49 +0000236 DEBUG(std::cerr << "Terminator is not a cond branch!");
237 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000238 }
Chris Lattner2ee82e02003-04-23 16:36:11 +0000239 SetCondInst *SCI = dyn_cast<SetCondInst>(B->getCondition());
Chris Lattner44abf852003-09-10 14:51:49 +0000240 if (!SCI) {
241 DEBUG(std::cerr << "Not a cond branch on setcc!\n");
242 return 0;
Misha Brukmana2722902002-10-11 05:34:32 +0000243 }
Chris Lattner44abf852003-09-10 14:51:49 +0000244
245 DEBUG(std::cerr << "sci:" << *SCI);
246 Value *condVal0 = SCI->getOperand(0);
247 Value *condVal1 = SCI->getOperand(1);
Chris Lattner44abf852003-09-10 14:51:49 +0000248
Chris Lattnera176a8b2003-09-10 14:55:05 +0000249 // The induction variable is the one coming from the backedge
250 Value *indVar = Phi->getIncomingValue(L->contains(Phi->getIncomingBlock(1)));
Chris Lattner44abf852003-09-10 14:51:49 +0000251
252
253 // Check to see if indVar is one of the parameters in SCI and if the other is
254 // loop-invariant, it is the UB
255 if (indVar == condVal0) {
256 if (isLoopInvariant(condVal1, L))
257 End = condVal1;
258 else {
259 DEBUG(std::cerr << "not loop invariant 1\n");
260 return 0;
261 }
262 } else if (indVar == condVal1) {
263 if (isLoopInvariant(condVal0, L))
264 End = condVal0;
265 else {
266 DEBUG(std::cerr << "not loop invariant 0\n");
267 return 0;
268 }
269 } else {
270 DEBUG(std::cerr << "Loop condition doesn't directly uses indvar\n");
271 return 0;
272 }
273
274 switch (SCI->getOpcode()) {
275 case Instruction::SetLT:
276 case Instruction::SetNE: return End; // already done
277 case Instruction::SetLE:
278 // if compared to a constant int N, then predict N+1 iterations
279 if (ConstantSInt *ubSigned = dyn_cast<ConstantSInt>(End)) {
280 DEBUG(std::cerr << "signed int constant\n");
281 return ConstantSInt::get(ubSigned->getType(), ubSigned->getValue()+1);
282 } else if (ConstantUInt *ubUnsigned = dyn_cast<ConstantUInt>(End)) {
283 DEBUG(std::cerr << "unsigned int constant\n");
284 return ConstantUInt::get(ubUnsigned->getType(),
285 ubUnsigned->getValue()+1);
286 } else {
287 DEBUG(std::cerr << "symbolic bound\n");
288 // new expression N+1, insert right before the SCI. FIXME: If End is loop
289 // invariant, then so is this expression. We should insert it in the loop
290 // preheader if it exists.
291 return BinaryOperator::create(Instruction::Add, End,
292 ConstantInt::get(End->getType(), 1),
293 "tripcount", SCI);
294 }
295
296 default:
297 return 0; // cannot predict
298 }
Misha Brukmana2722902002-10-11 05:34:32 +0000299}
300
301
Chris Lattnera59cbb22002-07-27 01:12:17 +0000302void InductionVariable::print(std::ostream &o) const {
303 switch (InductionType) {
Chris Lattner69ecd0d2003-09-10 05:24:09 +0000304 case InductionVariable::Canonical: o << "Canonical "; break;
Chris Lattnera59cbb22002-07-27 01:12:17 +0000305 case InductionVariable::SimpleLinear: o << "SimpleLinear "; break;
306 case InductionVariable::Linear: o << "Linear "; break;
307 case InductionVariable::Unknown: o << "Unrecognized "; break;
308 }
Chris Lattner74493a42002-09-10 15:35:39 +0000309 o << "Induction Variable: ";
Chris Lattnera59cbb22002-07-27 01:12:17 +0000310 if (Phi) {
311 WriteAsOperand(o, Phi);
312 o << ":\n" << Phi;
313 } else {
314 o << "\n";
315 }
316 if (InductionType == InductionVariable::Unknown) return;
317
Chris Lattner74493a42002-09-10 15:35:39 +0000318 o << " Start = "; WriteAsOperand(o, Start);
319 o << " Step = " ; WriteAsOperand(o, Step);
Misha Brukmana2722902002-10-11 05:34:32 +0000320 if (End) {
321 o << " End = " ; WriteAsOperand(o, End);
322 }
Chris Lattnera59cbb22002-07-27 01:12:17 +0000323 o << "\n";
324}