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