1 //===- llvm/Analysis/InductionVariable.h - Induction variable ----*- C++ -*--=//
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
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.
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.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/InductionVariable.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/Expressions.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/InstrTypes.h"
24 #include "llvm/Type.h"
25 #include "llvm/ConstantVals.h"
27 using analysis::ExprType;
30 static bool isLoopInvariant(const Value *V, const cfg::Loop *L) {
31 if (isa<Constant>(V) || isa<MethodArgument>(V) || isa<GlobalValue>(V))
34 const Instruction *I = cast<Instruction>(V);
35 const BasicBlock *BB = I->getParent();
37 return !L->contains(BB);
40 enum InductionVariable::iType
41 InductionVariable::Classify(const Value *Start, const Value *Step,
42 const cfg::Loop *L = 0) {
43 // Check for cannonical and simple linear expressions now...
44 if (ConstantInt *CStart = dyn_cast<ConstantInt>(Start))
45 if (ConstantInt *CStep = dyn_cast<ConstantInt>(Step)) {
46 if (CStart->equalsInt(0) && CStep->equalsInt(1))
52 // Without loop information, we cannot do any better, so bail now...
53 if (L == 0) return Unknown;
55 if (isLoopInvariant(Start, L) && isLoopInvariant(Step, L))
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.
63 InductionVariable::InductionVariable(PHINode *P, cfg::LoopInfo *LoopInfo) {
64 InductionType = Unknown; // Assume the worst
67 // If the PHI node has more than two predecessors, we don't know how to
70 if (Phi->getNumIncomingValues() != 2) return;
72 // If we have loop information, make sure that this PHI node is in the header
75 const cfg::Loop *L = LoopInfo ? LoopInfo->getLoopFor(Phi->getParent()) : 0;
76 if (L && L->getHeader() != Phi->getParent())
79 Value *V1 = Phi->getIncomingValue(0);
80 Value *V2 = Phi->getIncomingValue(1);
82 if (L == 0) { // No loop information? Base everything on expression analysis
83 ExprType E1 = analysis::ClassifyExpression(V1);
84 ExprType E2 = analysis::ClassifyExpression(V2);
86 if (E1.ExprTy > E2.ExprTy) // Make E1 be the simpler expression
89 // E1 must be a constant incoming value, and E2 must be a linear expression
90 // with respect to the PHI node.
92 if (E1.ExprTy > ExprType::Constant || E2.ExprTy != ExprType::Linear ||
96 // Okay, we have found an induction variable. Save the start and step values
97 const Type *ETy = Phi->getType();
98 if (ETy->isPointerType()) ETy = Type::ULongTy;
100 Start = (Value*)(E1.Offset ? E1.Offset : ConstantInt::get(ETy, 0));
101 Step = (Value*)(E2.Offset ? E2.Offset : ConstantInt::get(ETy, 0));
103 // Okay, at this point, we know that we have loop information...
105 // Make sure that V1 is the incoming value, and V2 is from the backedge of
107 if (L->contains(Phi->getIncomingBlock(0))) // Wrong order. Swap now.
110 Start = V1; // We know that Start has to be loop invariant...
113 if (V2 == Phi) { // referencing the PHI directly? Must have zero step
114 Step = Constant::getNullConstant(Phi->getType());
115 } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(V2)) {
116 // TODO: This could be much better...
117 if (I->getOpcode() == Instruction::Add) {
118 if (I->getOperand(0) == Phi)
119 Step = I->getOperand(1);
120 else if (I->getOperand(1) == Phi)
121 Step = I->getOperand(0);
125 if (Step == 0) { // Unrecognized step value...
126 ExprType StepE = analysis::ClassifyExpression(V2);
127 if (StepE.ExprTy != ExprType::Linear ||
128 StepE.Var != Phi) return;
130 const Type *ETy = Phi->getType();
131 if (ETy->isPointerType()) ETy = Type::ULongTy;
132 Step = (Value*)(StepE.Offset ? StepE.Offset : ConstantInt::get(ETy, 0));
136 // Classify the induction variable type now...
137 InductionType = InductionVariable::Classify(Start, Step, L);