Chris Lattner | f6e5233f | 2003-09-10 05:08:19 +0000 | [diff] [blame^] | 1 | //===- InductionVariable.cpp - Induction variable classification ----------===// |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | f6e5233f | 2003-09-10 05:08:19 +0000 | [diff] [blame^] | 3 | // 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 Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 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" |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 22 | #include "llvm/BasicBlock.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 23 | #include "llvm/iPHINode.h" |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 24 | #include "llvm/iOperators.h" |
| 25 | #include "llvm/iTerminators.h" |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 26 | #include "llvm/Type.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CFG.h" |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 29 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 30 | #include "Support/Debug.h" |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 32 | static bool isLoopInvariant(const Value *V, const Loop *L) { |
Chris Lattner | 36836a6 | 2003-09-10 04:49:10 +0000 | [diff] [blame] | 33 | 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 Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | enum InductionVariable::iType |
| 40 | InductionVariable::Classify(const Value *Start, const Value *Step, |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 41 | const Loop *L) { |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 42 | // Check for cannonical and simple linear expressions now... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 43 | if (const ConstantInt *CStart = dyn_cast<ConstantInt>(Start)) |
| 44 | if (const ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) { |
Chris Lattner | 36836a6 | 2003-09-10 04:49:10 +0000 | [diff] [blame] | 45 | if (CStart->isNullValue() && CStep->equalsInt(1)) |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 46 | return Cannonical; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 47 | else |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 48 | return SimpleLinear; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 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 | // |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 62 | InductionVariable::InductionVariable(PHINode *P, LoopInfo *LoopInfo): End(0) { |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 63 | InductionType = Unknown; // Assume the worst |
Chris Lattner | df89f6e | 2001-12-03 17:27:42 +0000 | [diff] [blame] | 64 | Phi = P; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 65 | |
Chris Lattner | df89f6e | 2001-12-03 17:27:42 +0000 | [diff] [blame] | 66 | // If the PHI node has more than two predecessors, we don't know how to |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 67 | // handle it. |
| 68 | // |
Chris Lattner | df89f6e | 2001-12-03 17:27:42 +0000 | [diff] [blame] | 69 | if (Phi->getNumIncomingValues() != 2) return; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 6de230a | 2001-12-05 06:32:30 +0000 | [diff] [blame] | 71 | // FIXME: Handle FP induction variables. |
| 72 | if (Phi->getType() == Type::FloatTy || Phi->getType() == Type::DoubleTy) |
| 73 | return; |
| 74 | |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 75 | // If we have loop information, make sure that this PHI node is in the header |
| 76 | // of a loop... |
| 77 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 78 | const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 79 | 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 Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 86 | ExprType E1 = ClassifyExpression(V1); |
| 87 | ExprType E2 = ClassifyExpression(V2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 88 | |
| 89 | if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 90 | std::swap(E1, E2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 91 | |
| 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 Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 96 | E2.Var != Phi) |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 97 | return; |
| 98 | |
| 99 | // Okay, we have found an induction variable. Save the start and step values |
| 100 | const Type *ETy = Phi->getType(); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 101 | if (isa<PointerType>(ETy)) ETy = Type::ULongTy; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 102 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 103 | Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0)); |
| 104 | Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0)); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 105 | } 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 Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 111 | std::swap(V1, V2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 112 | |
| 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 Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 117 | Step = Constant::getNullValue(Phi->getType()); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 118 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) { |
| 119 | // TODO: This could be much better... |
| 120 | if (I->getOpcode() == Instruction::Add) { |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 121 | if (I->getOperand(0) == Phi) |
| 122 | Step = I->getOperand(1); |
| 123 | else if (I->getOperand(1) == Phi) |
| 124 | Step = I->getOperand(0); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | if (Step == 0) { // Unrecognized step value... |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 129 | ExprType StepE = ClassifyExpression(V2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 130 | if (StepE.ExprTy != ExprType::Linear || |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 131 | StepE.Var != Phi) return; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 132 | |
| 133 | const Type *ETy = Phi->getType(); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 134 | if (isa<PointerType>(ETy)) ETy = Type::ULongTy; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 135 | Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0)); |
Chris Lattner | 621c992 | 2001-12-04 08:12:47 +0000 | [diff] [blame] | 136 | } else { // We were able to get a step value, simplify with expr analysis |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 137 | ExprType StepE = ClassifyExpression(Step); |
Chris Lattner | 621c992 | 2001-12-04 08:12:47 +0000 | [diff] [blame] | 138 | 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 Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 145 | Step = Constant::getNullValue(Step->getType()); |
Chris Lattner | 6de230a | 2001-12-05 06:32:30 +0000 | [diff] [blame] | 146 | const Type *ETy = Phi->getType(); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 147 | if (isa<PointerType>(ETy)) ETy = Type::ULongTy; |
Chris Lattner | 6de230a | 2001-12-05 06:32:30 +0000 | [diff] [blame] | 148 | Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy,0)); |
Chris Lattner | 621c992 | 2001-12-04 08:12:47 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | // Classify the induction variable type now... |
| 154 | InductionType = InductionVariable::Classify(Start, Step, L); |
| 155 | } |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 156 | |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 157 | |
| 158 | Value* InductionVariable::getExecutionCount(LoopInfo *LoopInfo) { |
| 159 | DEBUG(std::cerr << "entering getExecutionCount\n"); |
| 160 | |
| 161 | // Don't recompute if already available |
| 162 | if (End) { |
| 163 | DEBUG(std::cerr << "returning cached End value.\n"); |
| 164 | return End; |
| 165 | } |
| 166 | |
| 167 | const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0; |
| 168 | if (!L) { |
| 169 | DEBUG(std::cerr << "null loop. oops\n"); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | // >1 backedge => cannot predict number of iterations |
| 174 | if (Phi->getNumIncomingValues() != 2) { |
| 175 | DEBUG(std::cerr << ">2 incoming values. oops\n"); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | // Find final node: predecesor of the loop header that's also an exit |
Chris Lattner | 0006bd7 | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 180 | BasicBlock *terminator = 0; |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 181 | BasicBlock *header = L->getHeader(); |
| 182 | for (pred_iterator PI = pred_begin(header), PE = pred_end(header); |
| 183 | PI != PE; ++PI) { |
| 184 | if (L->isLoopExit(*PI)) { |
| 185 | terminator = *PI; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 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(); |
| 195 | for (std::vector<BasicBlock*>::const_iterator i = blocks.begin(), e = blocks.end(); |
| 196 | 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)) { |
| 200 | DEBUG(std::cerr << "break found in loop"); |
| 201 | return NULL; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | BranchInst *B = dyn_cast<BranchInst>(terminator->getTerminator()); |
| 208 | if (!B) { |
| 209 | // this really should not happen |
| 210 | DEBUG(std::cerr << "no terminator instruction!"); |
| 211 | return NULL; |
| 212 | } |
Chris Lattner | 2ee82e0 | 2003-04-23 16:36:11 +0000 | [diff] [blame] | 213 | SetCondInst *SCI = dyn_cast<SetCondInst>(B->getCondition()); |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 214 | |
| 215 | if (SCI && InductionType == Cannonical) { |
| 216 | DEBUG(std::cerr << "sci:" << *SCI); |
| 217 | Value *condVal0 = SCI->getOperand(0); |
| 218 | Value *condVal1 = SCI->getOperand(1); |
| 219 | Value *indVar = 0; |
| 220 | |
| 221 | // the induction variable is the one coming from the backedge |
| 222 | if (L->contains(Phi->getIncomingBlock(0))) { |
| 223 | indVar = Phi->getIncomingValue(0); |
| 224 | } else { |
| 225 | indVar = Phi->getIncomingValue(1); |
| 226 | } |
| 227 | |
| 228 | // check to see if indVar is one of the parameters in SCI |
| 229 | // and if the other is loop-invariant, it is the UB |
| 230 | if (indVar == condVal0) { |
| 231 | if (isLoopInvariant(condVal1, L)) { |
| 232 | End = condVal1; |
| 233 | } else { |
| 234 | DEBUG(std::cerr << "not loop invariant 1\n"); |
| 235 | } |
| 236 | } else if (indVar == condVal1) { |
| 237 | if (isLoopInvariant(condVal0, L)) { |
| 238 | End = condVal0; |
| 239 | } else { |
| 240 | DEBUG(std::cerr << "not loop invariant 0\n"); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (End) { |
| 245 | switch (SCI->getOpcode()) { |
| 246 | case Instruction::SetLT: |
| 247 | case Instruction::SetNE: break; // 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 | End = ConstantSInt::get(ubSigned->getType(), ubSigned->getValue()+1); |
| 252 | DEBUG(std::cerr << "signed int constant\n"); |
| 253 | } else if (ConstantUInt *ubUnsigned = dyn_cast<ConstantUInt>(End)) { |
Chris Lattner | bfcdf14 | 2003-07-23 15:17:01 +0000 | [diff] [blame] | 254 | End = ConstantUInt::get(ubUnsigned->getType(), |
| 255 | ubUnsigned->getValue()+1); |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 256 | DEBUG(std::cerr << "unsigned int constant\n"); |
| 257 | } else { |
| 258 | DEBUG(std::cerr << "symbolic bound\n"); |
| 259 | //End = NULL; |
| 260 | // new expression N+1 |
| 261 | End = BinaryOperator::create(Instruction::Add, End, |
Chris Lattner | bfcdf14 | 2003-07-23 15:17:01 +0000 | [diff] [blame] | 262 | ConstantUInt::get(ubUnsigned->getType(), |
| 263 | 1)); |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 264 | } |
| 265 | break; |
| 266 | } |
| 267 | default: End = NULL; // cannot predict |
| 268 | } |
| 269 | } |
| 270 | return End; |
| 271 | } else { |
| 272 | DEBUG(std::cerr << "SCI null or non-cannonical ind var\n"); |
| 273 | } |
| 274 | return NULL; |
| 275 | } |
| 276 | |
| 277 | |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 278 | void InductionVariable::print(std::ostream &o) const { |
| 279 | switch (InductionType) { |
| 280 | case InductionVariable::Cannonical: o << "Cannonical "; break; |
| 281 | case InductionVariable::SimpleLinear: o << "SimpleLinear "; break; |
| 282 | case InductionVariable::Linear: o << "Linear "; break; |
| 283 | case InductionVariable::Unknown: o << "Unrecognized "; break; |
| 284 | } |
Chris Lattner | 74493a4 | 2002-09-10 15:35:39 +0000 | [diff] [blame] | 285 | o << "Induction Variable: "; |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 286 | if (Phi) { |
| 287 | WriteAsOperand(o, Phi); |
| 288 | o << ":\n" << Phi; |
| 289 | } else { |
| 290 | o << "\n"; |
| 291 | } |
| 292 | if (InductionType == InductionVariable::Unknown) return; |
| 293 | |
Chris Lattner | 74493a4 | 2002-09-10 15:35:39 +0000 | [diff] [blame] | 294 | o << " Start = "; WriteAsOperand(o, Start); |
| 295 | o << " Step = " ; WriteAsOperand(o, Step); |
Misha Brukman | a272290 | 2002-10-11 05:34:32 +0000 | [diff] [blame] | 296 | if (End) { |
| 297 | o << " End = " ; WriteAsOperand(o, End); |
| 298 | } |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 299 | o << "\n"; |
| 300 | } |