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