Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 1 | //===- 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" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 22 | #include "llvm/iPHINode.h" |
| 23 | #include "llvm/InstrTypes.h" |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 24 | #include "llvm/Type.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 26 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 28 | static bool isLoopInvariant(const Value *V, const Loop *L) { |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 29 | if (isa<Constant>(V) || isa<Argument>(V) || isa<GlobalValue>(V)) |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 30 | return true; |
| 31 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 32 | const Instruction *I = cast<Instruction>(V); |
| 33 | const BasicBlock *BB = I->getParent(); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 34 | |
| 35 | return !L->contains(BB); |
| 36 | } |
| 37 | |
| 38 | enum InductionVariable::iType |
| 39 | InductionVariable::Classify(const Value *Start, const Value *Step, |
Chris Lattner | ceadf05 | 2002-07-24 22:40:36 +0000 | [diff] [blame] | 40 | const Loop *L) { |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 41 | // Check for cannonical and simple linear expressions now... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 42 | if (const ConstantInt *CStart = dyn_cast<ConstantInt>(Start)) |
| 43 | if (const ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) { |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 44 | if (CStart->equalsInt(0) && CStep->equalsInt(1)) |
| 45 | return Cannonical; |
| 46 | else |
| 47 | return SimpleLinear; |
| 48 | } |
| 49 | |
| 50 | // Without loop information, we cannot do any better, so bail now... |
| 51 | if (L == 0) return Unknown; |
| 52 | |
| 53 | if (isLoopInvariant(Start, L) && isLoopInvariant(Step, L)) |
| 54 | return Linear; |
| 55 | return Unknown; |
| 56 | } |
| 57 | |
| 58 | // Create an induction variable for the specified value. If it is a PHI, and |
| 59 | // if it's recognizable, classify it and fill in instance variables. |
| 60 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 61 | InductionVariable::InductionVariable(PHINode *P, LoopInfo *LoopInfo) { |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 62 | InductionType = Unknown; // Assume the worst |
Chris Lattner | df89f6e | 2001-12-03 17:27:42 +0000 | [diff] [blame] | 63 | Phi = P; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 64 | |
Chris Lattner | df89f6e | 2001-12-03 17:27:42 +0000 | [diff] [blame] | 65 | // 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] | 66 | // handle it. |
| 67 | // |
Chris Lattner | df89f6e | 2001-12-03 17:27:42 +0000 | [diff] [blame] | 68 | if (Phi->getNumIncomingValues() != 2) return; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 6de230a | 2001-12-05 06:32:30 +0000 | [diff] [blame] | 70 | // FIXME: Handle FP induction variables. |
| 71 | if (Phi->getType() == Type::FloatTy || Phi->getType() == Type::DoubleTy) |
| 72 | return; |
| 73 | |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 74 | // If we have loop information, make sure that this PHI node is in the header |
| 75 | // of a loop... |
| 76 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 77 | const Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 78 | if (L && L->getHeader() != Phi->getParent()) |
| 79 | return; |
| 80 | |
| 81 | Value *V1 = Phi->getIncomingValue(0); |
| 82 | Value *V2 = Phi->getIncomingValue(1); |
| 83 | |
| 84 | if (L == 0) { // No loop information? Base everything on expression analysis |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 85 | ExprType E1 = ClassifyExpression(V1); |
| 86 | ExprType E2 = ClassifyExpression(V2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 87 | |
| 88 | if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 89 | std::swap(E1, E2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 90 | |
| 91 | // E1 must be a constant incoming value, and E2 must be a linear expression |
| 92 | // with respect to the PHI node. |
| 93 | // |
| 94 | if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear || |
| 95 | E2.Var != Phi) |
| 96 | return; |
| 97 | |
| 98 | // Okay, we have found an induction variable. Save the start and step values |
| 99 | const Type *ETy = Phi->getType(); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 100 | if (isa<PointerType>(ETy)) ETy = Type::ULongTy; |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 101 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 102 | Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0)); |
| 103 | Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0)); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 104 | } else { |
| 105 | // Okay, at this point, we know that we have loop information... |
| 106 | |
| 107 | // Make sure that V1 is the incoming value, and V2 is from the backedge of |
| 108 | // the loop. |
| 109 | if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 110 | std::swap(V1, V2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 111 | |
| 112 | Start = V1; // We know that Start has to be loop invariant... |
| 113 | Step = 0; |
| 114 | |
| 115 | if (V2 == Phi) { // referencing the PHI directly? Must have zero step |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 116 | Step = Constant::getNullValue(Phi->getType()); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 117 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) { |
| 118 | // TODO: This could be much better... |
| 119 | if (I->getOpcode() == Instruction::Add) { |
| 120 | if (I->getOperand(0) == Phi) |
| 121 | Step = I->getOperand(1); |
| 122 | else if (I->getOperand(1) == Phi) |
| 123 | Step = I->getOperand(0); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (Step == 0) { // Unrecognized step value... |
Chris Lattner | c74cb86 | 2002-08-30 22:53:53 +0000 | [diff] [blame] | 128 | ExprType StepE = ClassifyExpression(V2); |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 129 | if (StepE.ExprTy != ExprType::Linear || |
| 130 | StepE.Var != Phi) return; |
| 131 | |
| 132 | const Type *ETy = Phi->getType(); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 133 | if (isa<PointerType>(ETy)) ETy = Type::ULongTy; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 134 | Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0)); |
Chris Lattner | 621c992 | 2001-12-04 08:12:47 +0000 | [diff] [blame] | 135 | } 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] | 136 | ExprType StepE = ClassifyExpression(Step); |
Chris Lattner | 621c992 | 2001-12-04 08:12:47 +0000 | [diff] [blame] | 137 | if (StepE.ExprTy == ExprType::Linear && StepE.Offset == 0) { |
| 138 | // No offset from variable? Grab the variable |
| 139 | Step = StepE.Var; |
| 140 | } else if (StepE.ExprTy == ExprType::Constant) { |
| 141 | if (StepE.Offset) |
| 142 | Step = (Value*)StepE.Offset; |
| 143 | else |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 144 | Step = Constant::getNullValue(Step->getType()); |
Chris Lattner | 6de230a | 2001-12-05 06:32:30 +0000 | [diff] [blame] | 145 | const Type *ETy = Phi->getType(); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 146 | if (isa<PointerType>(ETy)) ETy = Type::ULongTy; |
Chris Lattner | 6de230a | 2001-12-05 06:32:30 +0000 | [diff] [blame] | 147 | Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy,0)); |
Chris Lattner | 621c992 | 2001-12-04 08:12:47 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | 0bbe58f | 2001-11-26 18:41:20 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | // Classify the induction variable type now... |
| 153 | InductionType = InductionVariable::Classify(Start, Step, L); |
| 154 | } |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 155 | |
| 156 | void InductionVariable::print(std::ostream &o) const { |
| 157 | switch (InductionType) { |
| 158 | case InductionVariable::Cannonical: o << "Cannonical "; break; |
| 159 | case InductionVariable::SimpleLinear: o << "SimpleLinear "; break; |
| 160 | case InductionVariable::Linear: o << "Linear "; break; |
| 161 | case InductionVariable::Unknown: o << "Unrecognized "; break; |
| 162 | } |
Chris Lattner | 74493a4 | 2002-09-10 15:35:39 +0000 | [diff] [blame] | 163 | o << "Induction Variable: "; |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 164 | if (Phi) { |
| 165 | WriteAsOperand(o, Phi); |
| 166 | o << ":\n" << Phi; |
| 167 | } else { |
| 168 | o << "\n"; |
| 169 | } |
| 170 | if (InductionType == InductionVariable::Unknown) return; |
| 171 | |
Chris Lattner | 74493a4 | 2002-09-10 15:35:39 +0000 | [diff] [blame] | 172 | o << " Start = "; WriteAsOperand(o, Start); |
| 173 | o << " Step = " ; WriteAsOperand(o, Step); |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 174 | o << "\n"; |
| 175 | } |