Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 1 | //===- InductionVars.cpp - Induction Variable Cannonicalization code --------=// |
| 2 | // |
| 3 | // This file implements induction variable cannonicalization of loops. |
| 4 | // |
| 5 | // Specifically, after this executes, the following is true: |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 6 | // - There is a single induction variable for each loop (at least loops that |
| 7 | // used to contain at least one induction variable) |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 8 | // * This induction variable starts at 0 and steps by 1 per iteration |
| 9 | // * This induction variable is represented by the first PHI node in the |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 10 | // Header block, allowing it to be found easily. |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 11 | // - All other preexisting induction variables are adjusted to operate in |
| 12 | // terms of this primary induction variable |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 13 | // - Induction variables with a step size of 0 have been eliminated. |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 15 | // This code assumes the following is true to perform its full job: |
| 16 | // - The CFG has been simplified to not have multiple entrances into an |
| 17 | // interval header. Interval headers should only have two predecessors, |
| 18 | // one from inside of the loop and one from outside of the loop. |
| 19 | // |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chris Lattner | 59b6b8e | 2002-01-21 23:17:48 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Scalar/InductionVars.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 23 | #include "llvm/ConstantVals.h" |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/IntervalPartition.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 25 | #include "llvm/iPHINode.h" |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 27 | #include "llvm/BasicBlock.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 28 | #include "llvm/InstrTypes.h" |
Chris Lattner | 237e6d1 | 2002-04-08 22:03:00 +0000 | [diff] [blame] | 29 | #include "llvm/Type.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CFG.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 31 | #include "Support/STLExtras.h" |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 33 | #include <iostream> |
| 34 | using std::cerr; |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 36 | // isLoopInvariant - Return true if the specified value/basic block source is |
| 37 | // an interval invariant computation. |
| 38 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 39 | static bool isLoopInvariant(Interval *Int, Value *V) { |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 40 | assert(isa<Constant>(V) || isa<Instruction>(V) || isa<Argument>(V)); |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 42 | if (!isa<Instruction>(V)) |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 43 | return true; // Constants and arguments are always loop invariant |
| 44 | |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 45 | BasicBlock *ValueBlock = cast<Instruction>(V)->getParent(); |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 46 | assert(ValueBlock && "Instruction not embedded in basic block!"); |
| 47 | |
| 48 | // For now, only consider values from outside of the interval, regardless of |
| 49 | // whether the expression could be lifted out of the loop by some LICM. |
| 50 | // |
| 51 | // TODO: invoke LICM library if we find out it would be useful. |
| 52 | // |
| 53 | return !Int->contains(ValueBlock); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | // isLinearInductionVariableH - Return isLIV if the expression V is a linear |
| 58 | // expression defined in terms of loop invariant computations, and a single |
| 59 | // instance of the PHI node PN. Return isLIC if the expression V is a loop |
| 60 | // invariant computation. Return isNLIV if the expression is a negated linear |
| 61 | // induction variable. Return isOther if it is neither. |
| 62 | // |
| 63 | // Currently allowed operators are: ADD, SUB, NEG |
| 64 | // TODO: This should allow casts! |
| 65 | // |
| 66 | enum LIVType { isLIV, isLIC, isNLIV, isOther }; |
| 67 | // |
| 68 | // neg - Negate the sign of a LIV expression. |
| 69 | inline LIVType neg(LIVType T) { |
| 70 | assert(T == isLIV || T == isNLIV && "Negate Only works on LIV expressions"); |
| 71 | return T == isLIV ? isNLIV : isLIV; |
| 72 | } |
| 73 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 74 | static LIVType isLinearInductionVariableH(Interval *Int, Value *V, |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 75 | PHINode *PN) { |
| 76 | if (V == PN) { return isLIV; } // PHI node references are (0+PHI) |
| 77 | if (isLoopInvariant(Int, V)) return isLIC; |
| 78 | |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 79 | // loop variant computations must be instructions! |
Chris Lattner | 9636a91 | 2001-10-01 16:18:37 +0000 | [diff] [blame] | 80 | Instruction *I = cast<Instruction>(V); |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 81 | switch (I->getOpcode()) { // Handle each instruction seperately |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 82 | case Instruction::Add: |
| 83 | case Instruction::Sub: { |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 84 | Value *SubV1 = cast<BinaryOperator>(I)->getOperand(0); |
| 85 | Value *SubV2 = cast<BinaryOperator>(I)->getOperand(1); |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 86 | LIVType SubLIVType1 = isLinearInductionVariableH(Int, SubV1, PN); |
| 87 | if (SubLIVType1 == isOther) return isOther; // Early bailout |
| 88 | LIVType SubLIVType2 = isLinearInductionVariableH(Int, SubV2, PN); |
| 89 | |
| 90 | switch (SubLIVType2) { |
| 91 | case isOther: return isOther; // Unknown subexpression type |
| 92 | case isLIC: return SubLIVType1; // Constant offset, return type #1 |
| 93 | case isLIV: |
| 94 | case isNLIV: |
| 95 | // So now we know that we have a linear induction variable on the RHS of |
| 96 | // the ADD or SUB instruction. SubLIVType1 cannot be isOther, so it is |
| 97 | // either a Loop Invariant computation, or a LIV type. |
| 98 | if (SubLIVType1 == isLIC) { |
| 99 | // Loop invariant computation, we know this is a LIV then. |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 100 | return (I->getOpcode() == Instruction::Add) ? |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 101 | SubLIVType2 : neg(SubLIVType2); |
| 102 | } |
| 103 | |
| 104 | // If the LHS is also a LIV Expression, we cannot add two LIVs together |
Chris Lattner | a41f50d | 2001-07-07 19:24:15 +0000 | [diff] [blame] | 105 | if (I->getOpcode() == Instruction::Add) return isOther; |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 106 | |
| 107 | // We can only subtract two LIVs if they are the same type, which yields |
| 108 | // a LIC, because the LIVs cancel each other out. |
| 109 | return (SubLIVType1 == SubLIVType2) ? isLIC : isOther; |
| 110 | } |
| 111 | // NOT REACHED |
| 112 | } |
| 113 | |
| 114 | default: // Any other instruction is not a LINEAR induction var |
| 115 | return isOther; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // isLinearInductionVariable - Return true if the specified expression is a |
| 120 | // "linear induction variable", which is an expression involving a single |
| 121 | // instance of the PHI node and a loop invariant value that is added or |
| 122 | // subtracted to the PHI node. This is calculated by walking the SSA graph |
| 123 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 124 | static inline bool isLinearInductionVariable(Interval *Int, Value *V, |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 125 | PHINode *PN) { |
| 126 | return isLinearInductionVariableH(Int, V, PN) == isLIV; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | // isSimpleInductionVar - Return true iff the cannonical induction variable PN |
| 131 | // has an initializer of the constant value 0, and has a step size of constant |
| 132 | // 1. |
| 133 | static inline bool isSimpleInductionVar(PHINode *PN) { |
| 134 | assert(PN->getNumIncomingValues() == 2 && "Must have cannonical PHI node!"); |
| 135 | Value *Initializer = PN->getIncomingValue(0); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 136 | if (!isa<Constant>(Initializer)) return false; |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 137 | |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 138 | if (Initializer->getType()->isSigned()) { // Signed constant value... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 139 | if (((ConstantSInt*)Initializer)->getValue() != 0) return false; |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 140 | } else if (Initializer->getType()->isUnsigned()) { // Unsigned constant value |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 141 | if (((ConstantUInt*)Initializer)->getValue() != 0) return false; |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 142 | } else { |
| 143 | return false; // Not signed or unsigned? Must be FP type or something |
| 144 | } |
| 145 | |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 146 | Value *StepExpr = PN->getIncomingValue(1); |
Chris Lattner | 1d87bcf | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 147 | if (!isa<Instruction>(StepExpr) || |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 148 | cast<Instruction>(StepExpr)->getOpcode() != Instruction::Add) |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 149 | return false; |
| 150 | |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 151 | BinaryOperator *I = cast<BinaryOperator>(StepExpr); |
| 152 | assert(isa<PHINode>(I->getOperand(0)) && |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 153 | "PHI node should be first operand of ADD instruction!"); |
| 154 | |
| 155 | // Get the right hand side of the ADD node. See if it is a constant 1. |
| 156 | Value *StepSize = I->getOperand(1); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 157 | if (!isa<Constant>(StepSize)) return false; |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 158 | |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 159 | if (StepSize->getType()->isSigned()) { // Signed constant value... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 160 | if (((ConstantSInt*)StepSize)->getValue() != 1) return false; |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 161 | } else if (StepSize->getType()->isUnsigned()) { // Unsigned constant value |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 162 | if (((ConstantUInt*)StepSize)->getValue() != 1) return false; |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 163 | } else { |
| 164 | return false; // Not signed or unsigned? Must be FP type or something |
| 165 | } |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 166 | |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 167 | // At this point, we know the initializer is a constant value 0 and the step |
| 168 | // size is a constant value 1. This is our simple induction variable! |
| 169 | return true; |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 172 | // InjectSimpleInductionVariable - Insert a cannonical induction variable into |
| 173 | // the interval header Header. This assumes that the flow graph is in |
| 174 | // simplified form (so we know that the header block has exactly 2 predecessors) |
| 175 | // |
| 176 | // TODO: This should inherit the largest type that is being used by the already |
| 177 | // present induction variables (instead of always using uint) |
| 178 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 179 | static PHINode *InjectSimpleInductionVariable(Interval *Int) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 180 | std::string PHIName, AddName; |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 181 | |
| 182 | BasicBlock *Header = Int->getHeaderNode(); |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 183 | Function *M = Header->getParent(); |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 184 | |
| 185 | if (M->hasSymbolTable()) { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 186 | // Only name the induction variable if the function isn't stripped. |
Chris Lattner | 237e6d1 | 2002-04-08 22:03:00 +0000 | [diff] [blame] | 187 | PHIName = "ind_var"; |
| 188 | AddName = "ind_var_next"; |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Create the neccesary instructions... |
| 192 | PHINode *PN = new PHINode(Type::UIntTy, PHIName); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 193 | Constant *One = ConstantUInt::get(Type::UIntTy, 1); |
| 194 | Constant *Zero = ConstantUInt::get(Type::UIntTy, 0); |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 195 | BinaryOperator *AddNode = BinaryOperator::create(Instruction::Add, |
| 196 | PN, One, AddName); |
| 197 | |
| 198 | // Figure out which predecessors I have to play with... there should be |
| 199 | // exactly two... one of which is a loop predecessor, and one of which is not. |
| 200 | // |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 201 | pred_iterator PI = pred_begin(Header); |
| 202 | assert(PI != pred_end(Header) && "Header node should have 2 preds!"); |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 203 | BasicBlock *Pred1 = *PI; ++PI; |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 204 | assert(PI != pred_end(Header) && "Header node should have 2 preds!"); |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 205 | BasicBlock *Pred2 = *PI; |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 206 | assert(++PI == pred_end(Header) && "Header node should have 2 preds!"); |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 207 | |
| 208 | // Make Pred1 be the loop entrance predecessor, Pred2 be the Loop predecessor |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 209 | if (Int->contains(Pred1)) std::swap(Pred1, Pred2); |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 210 | |
| 211 | assert(!Int->contains(Pred1) && "Pred1 should be loop entrance!"); |
| 212 | assert( Int->contains(Pred2) && "Pred2 should be looping edge!"); |
| 213 | |
| 214 | // Link the instructions into the PHI node... |
| 215 | PN->addIncoming(Zero, Pred1); // The initializer is first argument |
| 216 | PN->addIncoming(AddNode, Pred2); // The step size is second PHI argument |
| 217 | |
| 218 | // Insert the PHI node into the Header of the loop. It shall be the first |
| 219 | // instruction, because the "Simple" Induction Variable must be first in the |
| 220 | // block. |
| 221 | // |
| 222 | BasicBlock::InstListType &IL = Header->getInstList(); |
| 223 | IL.push_front(PN); |
| 224 | |
| 225 | // Insert the Add instruction as the first (non-phi) instruction in the |
| 226 | // header node's basic block. |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 227 | BasicBlock::iterator I = IL.begin(); |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 228 | while (isa<PHINode>(*I)) ++I; |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 229 | IL.insert(I, AddNode); |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 230 | return PN; |
| 231 | } |
| 232 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 233 | // ProcessInterval - This function is invoked once for each interval in the |
| 234 | // IntervalPartition of the program. It looks for auxilliary induction |
| 235 | // variables in loops. If it finds one, it: |
| 236 | // * Cannonicalizes the induction variable. This consists of: |
| 237 | // A. Making the first element of the PHI node be the loop invariant |
| 238 | // computation, and the second element be the linear induction portion. |
| 239 | // B. Changing the first element of the linear induction portion of the PHI |
| 240 | // node to be of the form ADD(PHI, <loop invariant expr>). |
| 241 | // * Add the induction variable PHI to a list of induction variables found. |
| 242 | // |
| 243 | // After this, a list of cannonical induction variables is known. This list |
| 244 | // is searched to see if there is an induction variable that counts from |
| 245 | // constant 0 with a step size of constant 1. If there is not one, one is |
| 246 | // injected into the loop. Thus a "simple" induction variable is always known |
| 247 | // |
| 248 | // One a simple induction variable is known, all other induction variables are |
| 249 | // modified to refer to the "simple" induction variable. |
| 250 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 251 | static bool ProcessInterval(Interval *Int) { |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 252 | if (!Int->isLoop()) return false; // Not a loop? Ignore it! |
| 253 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 254 | std::vector<PHINode *> InductionVars; |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 255 | |
| 256 | BasicBlock *Header = Int->getHeaderNode(); |
| 257 | // Loop over all of the PHI nodes in the interval header... |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 258 | for (BasicBlock::iterator I = Header->begin(), E = Header->end(); |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 259 | I != E && isa<PHINode>(*I); ++I) { |
| 260 | PHINode *PN = cast<PHINode>(*I); |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 261 | if (PN->getNumIncomingValues() != 2) { // These should be eliminated by now. |
| 262 | cerr << "Found interval header with more than 2 predecessors! Ignoring\n"; |
| 263 | return false; // Todo, make an assertion. |
| 264 | } |
| 265 | |
| 266 | // For this to be an induction variable, one of the arguments must be a |
| 267 | // loop invariant expression, and the other must be an expression involving |
| 268 | // the PHI node, along with possible additions and subtractions of loop |
| 269 | // invariant values. |
| 270 | // |
| 271 | BasicBlock *BB1 = PN->getIncomingBlock(0); |
| 272 | Value *V1 = PN->getIncomingValue(0); |
| 273 | BasicBlock *BB2 = PN->getIncomingBlock(1); |
| 274 | Value *V2 = PN->getIncomingValue(1); |
| 275 | |
| 276 | // Figure out which computation is loop invariant... |
| 277 | if (!isLoopInvariant(Int, V1)) { |
| 278 | // V1 is *not* loop invariant. Check to see if V2 is: |
| 279 | if (isLoopInvariant(Int, V2)) { |
| 280 | // They *are* loop invariant. Exchange BB1/BB2 and V1/V2 so that |
| 281 | // V1 is always the loop invariant computation. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 282 | std::swap(V1, V2); std::swap(BB1, BB2); |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 283 | } else { |
| 284 | // Neither value is loop invariant. Must not be an induction variable. |
| 285 | // This case can happen if there is an unreachable loop in the CFG that |
| 286 | // has two tail loops in it that was not split by the cleanup phase |
| 287 | // before. |
| 288 | continue; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // At this point, we know that BB1/V1 are loop invariant. We don't know |
| 293 | // anything about BB2/V2. Check now to see if V2 is a linear induction |
| 294 | // variable. |
| 295 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 296 | cerr << "Found loop invariant computation: " << V1 << "\n"; |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 297 | |
| 298 | if (!isLinearInductionVariable(Int, V2, PN)) |
| 299 | continue; // No, it is not a linear ind var, ignore the PHI node. |
| 300 | cerr << "Found linear induction variable: " << V2; |
| 301 | |
| 302 | // TODO: Cannonicalize V2 |
| 303 | |
| 304 | // Add this PHI node to the list of induction variables found... |
| 305 | InductionVars.push_back(PN); |
| 306 | } |
| 307 | |
| 308 | // No induction variables found? |
| 309 | if (InductionVars.empty()) return false; |
| 310 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 311 | // Search to see if there is already a "simple" induction variable. |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 312 | std::vector<PHINode*>::iterator It = |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 313 | find_if(InductionVars.begin(), InductionVars.end(), isSimpleInductionVar); |
| 314 | |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 315 | PHINode *PrimaryIndVar; |
| 316 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 317 | // A simple induction variable was not found, inject one now... |
| 318 | if (It == InductionVars.end()) { |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 319 | PrimaryIndVar = InjectSimpleInductionVariable(Int); |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 320 | } else { |
| 321 | // Move the PHI node for this induction variable to the start of the PHI |
| 322 | // list in HeaderNode... we do not need to do this for the inserted case |
| 323 | // because the inserted node will always be placed at the beginning of |
| 324 | // HeaderNode. |
| 325 | // |
| 326 | PrimaryIndVar = *It; |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 327 | BasicBlock::iterator i = |
| 328 | find(Header->begin(), Header->end(), PrimaryIndVar); |
| 329 | assert(i != Header->end() && |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 330 | "How could Primary IndVar not be in the header!?!!?"); |
| 331 | |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 332 | if (i != Header->begin()) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 333 | std::iter_swap(i, Header->begin()); |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 336 | // Now we know that there is a simple induction variable PrimaryIndVar. |
| 337 | // Simplify all of the other induction variables to use this induction |
| 338 | // variable as their counter, and destroy the PHI nodes that correspond to |
| 339 | // the old indvars. |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 340 | // |
| 341 | // TODO |
| 342 | |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 343 | |
| 344 | cerr << "Found Interval Header with indvars (primary indvar should be first " |
Chris Lattner | 3b34c59 | 2001-06-27 23:36:09 +0000 | [diff] [blame] | 345 | << "phi): \n" << Header << "\nPrimaryIndVar: " << PrimaryIndVar; |
Chris Lattner | c9f39b2 | 2001-06-24 04:05:45 +0000 | [diff] [blame] | 346 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 347 | return false; // TODO: true; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | // ProcessIntervalPartition - This function loops over the interval partition |
| 352 | // processing each interval with ProcessInterval |
| 353 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 354 | static bool ProcessIntervalPartition(IntervalPartition &IP) { |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 355 | // This currently just prints out information about the interval structure |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 356 | // of the function... |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 357 | #if 0 |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 358 | static unsigned N = 0; |
| 359 | cerr << "\n***********Interval Partition #" << (++N) << "************\n\n"; |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 360 | copy(IP.begin(), IP.end(), ostream_iterator<Interval*>(cerr, "\n")); |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 361 | |
| 362 | cerr << "\n*********** PERFORMING WORK ************\n\n"; |
Chris Lattner | d473a0a | 2001-06-25 07:32:19 +0000 | [diff] [blame] | 363 | #endif |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 364 | // Loop over all of the intervals in the partition and look for induction |
| 365 | // variables in intervals that represent loops. |
| 366 | // |
| 367 | return reduce_apply(IP.begin(), IP.end(), bitwise_or<bool>(), false, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 368 | std::ptr_fun(ProcessInterval)); |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame] | 371 | // DoInductionVariableCannonicalize - Simplify induction variables in loops. |
| 372 | // This function loops over an interval partition of a program, reducing it |
| 373 | // until the graph is gone. |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 374 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 375 | bool InductionVariableCannonicalize::doIt(Function *M, IntervalPartition &IP) { |
| 376 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 377 | bool Changed = false; |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 379 | #if 0 |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 380 | while (!IP->isDegeneratePartition()) { |
| 381 | Changed |= ProcessIntervalPartition(*IP); |
Chris Lattner | 5683205 | 2001-06-20 22:44:38 +0000 | [diff] [blame] | 382 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 383 | // Calculate the reduced version of this graph until we get to an |
| 384 | // irreducible graph or a degenerate graph... |
| 385 | // |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 386 | IntervalPartition *NewIP = new IntervalPartition(*IP, false); |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 387 | if (NewIP->size() == IP->size()) { |
| 388 | cerr << "IRREDUCIBLE GRAPH FOUND!!!\n"; |
| 389 | return Changed; |
| 390 | } |
| 391 | delete IP; |
| 392 | IP = NewIP; |
| 393 | } |
Chris Lattner | 5683205 | 2001-06-20 22:44:38 +0000 | [diff] [blame] | 394 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 395 | delete IP; |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 396 | #endif |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 397 | return Changed; |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 399 | |
| 400 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 401 | bool InductionVariableCannonicalize::runOnFunction(Function *F) { |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 402 | return doIt(F, getAnalysis<IntervalPartition>()); |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 405 | // getAnalysisUsage - This function works on the call graph of a module. |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 406 | // It is capable of updating the call graph to reflect the new state of the |
| 407 | // module. |
| 408 | // |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 409 | void InductionVariableCannonicalize::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 410 | AU.addRequired(IntervalPartition::ID); |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 411 | } |