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 | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 8 | // - This induction variable starts at 0 and steps by 1 per iteration |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame^] | 9 | // - This induction variable is represented by the first PHI node in the |
| 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 |
| 13 | // |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame^] | 14 | // This code assumes the following is true to perform its full job: |
| 15 | // - The CFG has been simplified to not have multiple entrances into an |
| 16 | // interval header. Interval headers should only have two predecessors, |
| 17 | // one from inside of the loop and one from outside of the loop. |
| 18 | // |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #include "llvm/Analysis/Intervals.h" |
| 22 | #include "llvm/Opt/AllOpts.h" |
| 23 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 24 | #include "llvm/Tools/STLExtras.h" |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame^] | 25 | #include "llvm/iOther.h" |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame^] | 27 | // isLoopInvariant - Return true if the specified value/basic block source is |
| 28 | // an interval invariant computation. |
| 29 | // |
| 30 | static bool isLoopInvariant(cfg::Interval *Int, Value *V) { |
| 31 | assert(V->getValueType() == Value::ConstantVal || |
| 32 | V->getValueType() == Value::InstructionVal || |
| 33 | V->getValueType() == Value::MethodArgumentVal); |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame^] | 35 | if (V->getValueType() != Value::InstructionVal) |
| 36 | return true; // Constants and arguments are always loop invariant |
| 37 | |
| 38 | BasicBlock *ValueBlock = ((Instruction*)V)->getParent(); |
| 39 | assert(ValueBlock && "Instruction not embedded in basic block!"); |
| 40 | |
| 41 | // For now, only consider values from outside of the interval, regardless of |
| 42 | // whether the expression could be lifted out of the loop by some LICM. |
| 43 | // |
| 44 | // TODO: invoke LICM library if we find out it would be useful. |
| 45 | // |
| 46 | return !Int->contains(ValueBlock); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | // isLinearInductionVariableH - Return isLIV if the expression V is a linear |
| 51 | // expression defined in terms of loop invariant computations, and a single |
| 52 | // instance of the PHI node PN. Return isLIC if the expression V is a loop |
| 53 | // invariant computation. Return isNLIV if the expression is a negated linear |
| 54 | // induction variable. Return isOther if it is neither. |
| 55 | // |
| 56 | // Currently allowed operators are: ADD, SUB, NEG |
| 57 | // TODO: This should allow casts! |
| 58 | // |
| 59 | enum LIVType { isLIV, isLIC, isNLIV, isOther }; |
| 60 | // |
| 61 | // neg - Negate the sign of a LIV expression. |
| 62 | inline LIVType neg(LIVType T) { |
| 63 | assert(T == isLIV || T == isNLIV && "Negate Only works on LIV expressions"); |
| 64 | return T == isLIV ? isNLIV : isLIV; |
| 65 | } |
| 66 | // |
| 67 | static LIVType isLinearInductionVariableH(cfg::Interval *Int, Value *V, |
| 68 | PHINode *PN) { |
| 69 | if (V == PN) { return isLIV; } // PHI node references are (0+PHI) |
| 70 | if (isLoopInvariant(Int, V)) return isLIC; |
| 71 | |
| 72 | assert(V->getValueType() == Value::InstructionVal && |
| 73 | "loop noninvariant computations must be instructions!"); |
| 74 | |
| 75 | Instruction *I = (Instruction*)V; |
| 76 | switch (I->getInstType()) { // Handle each instruction seperately |
| 77 | case Instruction::Neg: { |
| 78 | Value *SubV = ((UnaryOperator*)I)->getOperand(0); |
| 79 | LIVType SubLIVType = isLinearInductionVariableH(Int, SubV, PN); |
| 80 | switch (SubLIVType) { |
| 81 | case isLIC: // Loop invariant & other computations remain the same |
| 82 | case isOther: return SubLIVType; |
| 83 | case isLIV: // Return the opposite signed LIV type |
| 84 | case isNLIV: return neg(isLIV); |
| 85 | } |
| 86 | } |
| 87 | case Instruction::Add: |
| 88 | case Instruction::Sub: { |
| 89 | Value *SubV1 = ((BinaryOperator*)I)->getOperand(0); |
| 90 | Value *SubV2 = ((BinaryOperator*)I)->getOperand(1); |
| 91 | LIVType SubLIVType1 = isLinearInductionVariableH(Int, SubV1, PN); |
| 92 | if (SubLIVType1 == isOther) return isOther; // Early bailout |
| 93 | LIVType SubLIVType2 = isLinearInductionVariableH(Int, SubV2, PN); |
| 94 | |
| 95 | switch (SubLIVType2) { |
| 96 | case isOther: return isOther; // Unknown subexpression type |
| 97 | case isLIC: return SubLIVType1; // Constant offset, return type #1 |
| 98 | case isLIV: |
| 99 | case isNLIV: |
| 100 | // So now we know that we have a linear induction variable on the RHS of |
| 101 | // the ADD or SUB instruction. SubLIVType1 cannot be isOther, so it is |
| 102 | // either a Loop Invariant computation, or a LIV type. |
| 103 | if (SubLIVType1 == isLIC) { |
| 104 | // Loop invariant computation, we know this is a LIV then. |
| 105 | return (I->getInstType() == Instruction::Add) ? |
| 106 | SubLIVType2 : neg(SubLIVType2); |
| 107 | } |
| 108 | |
| 109 | // If the LHS is also a LIV Expression, we cannot add two LIVs together |
| 110 | if (I->getInstType() == Instruction::Add) return isOther; |
| 111 | |
| 112 | // We can only subtract two LIVs if they are the same type, which yields |
| 113 | // a LIC, because the LIVs cancel each other out. |
| 114 | return (SubLIVType1 == SubLIVType2) ? isLIC : isOther; |
| 115 | } |
| 116 | // NOT REACHED |
| 117 | } |
| 118 | |
| 119 | default: // Any other instruction is not a LINEAR induction var |
| 120 | return isOther; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // isLinearInductionVariable - Return true if the specified expression is a |
| 125 | // "linear induction variable", which is an expression involving a single |
| 126 | // instance of the PHI node and a loop invariant value that is added or |
| 127 | // subtracted to the PHI node. This is calculated by walking the SSA graph |
| 128 | // |
| 129 | static inline bool isLinearInductionVariable(cfg::Interval *Int, Value *V, |
| 130 | PHINode *PN) { |
| 131 | return isLinearInductionVariableH(Int, V, PN) == isLIV; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | // isSimpleInductionVar - Return true iff the cannonical induction variable PN |
| 136 | // has an initializer of the constant value 0, and has a step size of constant |
| 137 | // 1. |
| 138 | static inline bool isSimpleInductionVar(PHINode *PN) { |
| 139 | assert(PN->getNumIncomingValues() == 2 && "Must have cannonical PHI node!"); |
| 140 | Value *Initializer = PN->getIncomingValue(0); |
| 141 | if (Initializer->getValueType() != Value::ConstantVal) |
| 142 | return false; |
| 143 | |
| 144 | // How do I check for 0 for any integral value? Use |
| 145 | // ConstPoolVal::getNullConstant? |
| 146 | |
| 147 | Value *StepExpr = PN->getIncomingValue(1); |
| 148 | assert(StepExpr->getValueType() == Value::InstructionVal && "No ADD node?"); |
| 149 | assert(((Instruction*)StepExpr)->getInstType() == Instruction::Add && |
| 150 | "No ADD node? Not a cannonical PHI!"); |
| 151 | BinaryOperator *I = (BinaryOperator*)StepExpr; |
| 152 | assert(I->getOperand(0)->getValueType() == Value::InstructionVal && |
| 153 | ((Instruction*)I->getOperand(0))->getInstType() == Instruction::PHINode && |
| 154 | "PHI node should be first operand of ADD instruction!"); |
| 155 | |
| 156 | // Get the right hand side of the ADD node. See if it is a constant 1. |
| 157 | Value *StepSize = I->getOperand(1); |
| 158 | if (StepSize->getValueType() != Value::ConstantVal) return false; |
| 159 | |
| 160 | // How do I check for 1 for any integral value? |
| 161 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame^] | 165 | // ProcessInterval - This function is invoked once for each interval in the |
| 166 | // IntervalPartition of the program. It looks for auxilliary induction |
| 167 | // variables in loops. If it finds one, it: |
| 168 | // * Cannonicalizes the induction variable. This consists of: |
| 169 | // A. Making the first element of the PHI node be the loop invariant |
| 170 | // computation, and the second element be the linear induction portion. |
| 171 | // B. Changing the first element of the linear induction portion of the PHI |
| 172 | // node to be of the form ADD(PHI, <loop invariant expr>). |
| 173 | // * Add the induction variable PHI to a list of induction variables found. |
| 174 | // |
| 175 | // After this, a list of cannonical induction variables is known. This list |
| 176 | // is searched to see if there is an induction variable that counts from |
| 177 | // constant 0 with a step size of constant 1. If there is not one, one is |
| 178 | // injected into the loop. Thus a "simple" induction variable is always known |
| 179 | // |
| 180 | // One a simple induction variable is known, all other induction variables are |
| 181 | // modified to refer to the "simple" induction variable. |
| 182 | // |
| 183 | static bool ProcessInterval(cfg::Interval *Int) { |
| 184 | if (!Int->isLoop()) return false; // Not a loop? Ignore it! |
| 185 | |
| 186 | vector<PHINode *> InductionVars; |
| 187 | |
| 188 | BasicBlock *Header = Int->getHeaderNode(); |
| 189 | // Loop over all of the PHI nodes in the interval header... |
| 190 | for (BasicBlock::InstListType::iterator I = Header->getInstList().begin(), |
| 191 | E = Header->getInstList().end(); |
| 192 | I != E && (*I)->getInstType() == Instruction::PHINode; ++I) { |
| 193 | |
| 194 | PHINode *PN = (PHINode*)*I; |
| 195 | if (PN->getNumIncomingValues() != 2) { // These should be eliminated by now. |
| 196 | cerr << "Found interval header with more than 2 predecessors! Ignoring\n"; |
| 197 | return false; // Todo, make an assertion. |
| 198 | } |
| 199 | |
| 200 | // For this to be an induction variable, one of the arguments must be a |
| 201 | // loop invariant expression, and the other must be an expression involving |
| 202 | // the PHI node, along with possible additions and subtractions of loop |
| 203 | // invariant values. |
| 204 | // |
| 205 | BasicBlock *BB1 = PN->getIncomingBlock(0); |
| 206 | Value *V1 = PN->getIncomingValue(0); |
| 207 | BasicBlock *BB2 = PN->getIncomingBlock(1); |
| 208 | Value *V2 = PN->getIncomingValue(1); |
| 209 | |
| 210 | // Figure out which computation is loop invariant... |
| 211 | if (!isLoopInvariant(Int, V1)) { |
| 212 | // V1 is *not* loop invariant. Check to see if V2 is: |
| 213 | if (isLoopInvariant(Int, V2)) { |
| 214 | // They *are* loop invariant. Exchange BB1/BB2 and V1/V2 so that |
| 215 | // V1 is always the loop invariant computation. |
| 216 | swap(V1, V2); swap(BB1, BB2); |
| 217 | } else { |
| 218 | // Neither value is loop invariant. Must not be an induction variable. |
| 219 | // This case can happen if there is an unreachable loop in the CFG that |
| 220 | // has two tail loops in it that was not split by the cleanup phase |
| 221 | // before. |
| 222 | continue; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // At this point, we know that BB1/V1 are loop invariant. We don't know |
| 227 | // anything about BB2/V2. Check now to see if V2 is a linear induction |
| 228 | // variable. |
| 229 | // |
| 230 | cerr << "Found loop invariant computation: " << V1; |
| 231 | |
| 232 | if (!isLinearInductionVariable(Int, V2, PN)) |
| 233 | continue; // No, it is not a linear ind var, ignore the PHI node. |
| 234 | cerr << "Found linear induction variable: " << V2; |
| 235 | |
| 236 | // TODO: Cannonicalize V2 |
| 237 | |
| 238 | // Add this PHI node to the list of induction variables found... |
| 239 | InductionVars.push_back(PN); |
| 240 | } |
| 241 | |
| 242 | // No induction variables found? |
| 243 | if (InductionVars.empty()) return false; |
| 244 | |
| 245 | cerr << "Found Interval Header with indvars: \n" << Header; |
| 246 | |
| 247 | // Search to see if there is already a "simple" induction variable. |
| 248 | vector<PHINode*>::iterator It = |
| 249 | find_if(InductionVars.begin(), InductionVars.end(), isSimpleInductionVar); |
| 250 | |
| 251 | // A simple induction variable was not found, inject one now... |
| 252 | if (It == InductionVars.end()) { |
| 253 | cerr << "WARNING, Induction variable injection not implemented yet!\n"; |
| 254 | // TODO: Inject induction variable |
| 255 | It = InductionVars.end(); --It; // Point it at the new indvar |
| 256 | } |
| 257 | |
| 258 | // Now we know that there is a simple induction variable *It. Simplify all |
| 259 | // of the other induction variables to use this induction variable as their |
| 260 | // counter, and destroy the PHI nodes that correspond to the old indvars. |
| 261 | // |
| 262 | // TODO |
| 263 | |
| 264 | return false; // TODO: true; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | // ProcessIntervalPartition - This function loops over the interval partition |
| 269 | // processing each interval with ProcessInterval |
| 270 | // |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 271 | static bool ProcessIntervalPartition(cfg::IntervalPartition &IP) { |
| 272 | // This currently just prints out information about the interval structure |
| 273 | // of the method... |
| 274 | static unsigned N = 0; |
| 275 | cerr << "\n***********Interval Partition #" << (++N) << "************\n\n"; |
| 276 | copy(IP.begin(), IP.end(), ostream_iterator<cfg::Interval*>(cerr, "\n")); |
| 277 | |
| 278 | cerr << "\n*********** PERFORMING WORK ************\n\n"; |
| 279 | |
| 280 | // Loop over all of the intervals in the partition and look for induction |
| 281 | // variables in intervals that represent loops. |
| 282 | // |
| 283 | return reduce_apply(IP.begin(), IP.end(), bitwise_or<bool>(), false, |
| 284 | ptr_fun(ProcessInterval)); |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Chris Lattner | 364b147 | 2001-06-22 02:24:38 +0000 | [diff] [blame^] | 287 | |
| 288 | // DoInductionVariableCannonicalize - Simplify induction variables in loops. |
| 289 | // This function loops over an interval partition of a program, reducing it |
| 290 | // until the graph is gone. |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 291 | // |
| 292 | bool DoInductionVariableCannonicalize(Method *M) { |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 293 | cfg::IntervalPartition *IP = new cfg::IntervalPartition(M); |
| 294 | bool Changed = false; |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 295 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 296 | while (!IP->isDegeneratePartition()) { |
| 297 | Changed |= ProcessIntervalPartition(*IP); |
Chris Lattner | 5683205 | 2001-06-20 22:44:38 +0000 | [diff] [blame] | 298 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 299 | // Calculate the reduced version of this graph until we get to an |
| 300 | // irreducible graph or a degenerate graph... |
| 301 | // |
| 302 | cfg::IntervalPartition *NewIP = new cfg::IntervalPartition(*IP, false); |
| 303 | if (NewIP->size() == IP->size()) { |
| 304 | cerr << "IRREDUCIBLE GRAPH FOUND!!!\n"; |
| 305 | return Changed; |
| 306 | } |
| 307 | delete IP; |
| 308 | IP = NewIP; |
| 309 | } |
Chris Lattner | 5683205 | 2001-06-20 22:44:38 +0000 | [diff] [blame] | 310 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 311 | delete IP; |
| 312 | return Changed; |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 313 | } |