blob: 032d38ec2e6ce1f6794de118676c984109245639 [file] [log] [blame]
Chris Lattnerd213f0f2001-06-20 19:27:11 +00001//===- 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 Lattner364b1472001-06-22 02:24:38 +00006// - There is a single induction variable for each loop (at least loops that
7// used to contain at least one induction variable)
Chris Lattnerd213f0f2001-06-20 19:27:11 +00008// - This induction variable starts at 0 and steps by 1 per iteration
Chris Lattner364b1472001-06-22 02:24:38 +00009// - This induction variable is represented by the first PHI node in the
10// Header block, allowing it to be found easily.
Chris Lattnerd213f0f2001-06-20 19:27:11 +000011// - All other preexisting induction variables are adjusted to operate in
12// terms of this primary induction variable
Chris Lattnerd473a0a2001-06-25 07:32:19 +000013// - Induction variables with a step size of 0 have been eliminated.
Chris Lattnerd213f0f2001-06-20 19:27:11 +000014//
Chris Lattner364b1472001-06-22 02:24:38 +000015// 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 Lattnerd213f0f2001-06-20 19:27:11 +000020//===----------------------------------------------------------------------===//
21
Chris Lattnerd473a0a2001-06-25 07:32:19 +000022#include "llvm/Opt/AllOpts.h"
Chris Lattnerc9f39b22001-06-24 04:05:45 +000023#include "llvm/ConstPoolVals.h"
24#include "llvm/Analysis/IntervalPartition.h"
Chris Lattnerd213f0f2001-06-20 19:27:11 +000025#include "llvm/Assembly/Writer.h"
Chris Lattnerda956802001-06-21 05:27:22 +000026#include "llvm/Tools/STLExtras.h"
Chris Lattnerd473a0a2001-06-25 07:32:19 +000027#include "llvm/SymbolTable.h"
Chris Lattner364b1472001-06-22 02:24:38 +000028#include "llvm/iOther.h"
Chris Lattnerd473a0a2001-06-25 07:32:19 +000029#include "llvm/CFG.h"
Chris Lattnerc9f39b22001-06-24 04:05:45 +000030#include <algorithm>
Chris Lattnerd213f0f2001-06-20 19:27:11 +000031
Chris Lattner364b1472001-06-22 02:24:38 +000032// isLoopInvariant - Return true if the specified value/basic block source is
33// an interval invariant computation.
34//
35static bool isLoopInvariant(cfg::Interval *Int, Value *V) {
36 assert(V->getValueType() == Value::ConstantVal ||
37 V->getValueType() == Value::InstructionVal ||
38 V->getValueType() == Value::MethodArgumentVal);
Chris Lattnerd213f0f2001-06-20 19:27:11 +000039
Chris Lattner364b1472001-06-22 02:24:38 +000040 if (V->getValueType() != Value::InstructionVal)
41 return true; // Constants and arguments are always loop invariant
42
43 BasicBlock *ValueBlock = ((Instruction*)V)->getParent();
44 assert(ValueBlock && "Instruction not embedded in basic block!");
45
46 // For now, only consider values from outside of the interval, regardless of
47 // whether the expression could be lifted out of the loop by some LICM.
48 //
49 // TODO: invoke LICM library if we find out it would be useful.
50 //
51 return !Int->contains(ValueBlock);
52}
53
54
55// isLinearInductionVariableH - Return isLIV if the expression V is a linear
56// expression defined in terms of loop invariant computations, and a single
57// instance of the PHI node PN. Return isLIC if the expression V is a loop
58// invariant computation. Return isNLIV if the expression is a negated linear
59// induction variable. Return isOther if it is neither.
60//
61// Currently allowed operators are: ADD, SUB, NEG
62// TODO: This should allow casts!
63//
64enum LIVType { isLIV, isLIC, isNLIV, isOther };
65//
66// neg - Negate the sign of a LIV expression.
67inline LIVType neg(LIVType T) {
68 assert(T == isLIV || T == isNLIV && "Negate Only works on LIV expressions");
69 return T == isLIV ? isNLIV : isLIV;
70}
71//
72static LIVType isLinearInductionVariableH(cfg::Interval *Int, Value *V,
73 PHINode *PN) {
74 if (V == PN) { return isLIV; } // PHI node references are (0+PHI)
75 if (isLoopInvariant(Int, V)) return isLIC;
76
77 assert(V->getValueType() == Value::InstructionVal &&
78 "loop noninvariant computations must be instructions!");
79
80 Instruction *I = (Instruction*)V;
81 switch (I->getInstType()) { // Handle each instruction seperately
82 case Instruction::Neg: {
83 Value *SubV = ((UnaryOperator*)I)->getOperand(0);
84 LIVType SubLIVType = isLinearInductionVariableH(Int, SubV, PN);
85 switch (SubLIVType) {
86 case isLIC: // Loop invariant & other computations remain the same
87 case isOther: return SubLIVType;
88 case isLIV: // Return the opposite signed LIV type
89 case isNLIV: return neg(isLIV);
90 }
91 }
92 case Instruction::Add:
93 case Instruction::Sub: {
94 Value *SubV1 = ((BinaryOperator*)I)->getOperand(0);
95 Value *SubV2 = ((BinaryOperator*)I)->getOperand(1);
96 LIVType SubLIVType1 = isLinearInductionVariableH(Int, SubV1, PN);
97 if (SubLIVType1 == isOther) return isOther; // Early bailout
98 LIVType SubLIVType2 = isLinearInductionVariableH(Int, SubV2, PN);
99
100 switch (SubLIVType2) {
101 case isOther: return isOther; // Unknown subexpression type
102 case isLIC: return SubLIVType1; // Constant offset, return type #1
103 case isLIV:
104 case isNLIV:
105 // So now we know that we have a linear induction variable on the RHS of
106 // the ADD or SUB instruction. SubLIVType1 cannot be isOther, so it is
107 // either a Loop Invariant computation, or a LIV type.
108 if (SubLIVType1 == isLIC) {
109 // Loop invariant computation, we know this is a LIV then.
110 return (I->getInstType() == Instruction::Add) ?
111 SubLIVType2 : neg(SubLIVType2);
112 }
113
114 // If the LHS is also a LIV Expression, we cannot add two LIVs together
115 if (I->getInstType() == Instruction::Add) return isOther;
116
117 // We can only subtract two LIVs if they are the same type, which yields
118 // a LIC, because the LIVs cancel each other out.
119 return (SubLIVType1 == SubLIVType2) ? isLIC : isOther;
120 }
121 // NOT REACHED
122 }
123
124 default: // Any other instruction is not a LINEAR induction var
125 return isOther;
126 }
127}
128
129// isLinearInductionVariable - Return true if the specified expression is a
130// "linear induction variable", which is an expression involving a single
131// instance of the PHI node and a loop invariant value that is added or
132// subtracted to the PHI node. This is calculated by walking the SSA graph
133//
134static inline bool isLinearInductionVariable(cfg::Interval *Int, Value *V,
135 PHINode *PN) {
136 return isLinearInductionVariableH(Int, V, PN) == isLIV;
137}
138
139
140// isSimpleInductionVar - Return true iff the cannonical induction variable PN
141// has an initializer of the constant value 0, and has a step size of constant
142// 1.
143static inline bool isSimpleInductionVar(PHINode *PN) {
144 assert(PN->getNumIncomingValues() == 2 && "Must have cannonical PHI node!");
145 Value *Initializer = PN->getIncomingValue(0);
146 if (Initializer->getValueType() != Value::ConstantVal)
147 return false;
148
Chris Lattnerc9f39b22001-06-24 04:05:45 +0000149 if (Initializer->getType()->isSigned()) { // Signed constant value...
150 if (((ConstPoolSInt*)Initializer)->getValue() != 0) return false;
151 } else if (Initializer->getType()->isUnsigned()) { // Unsigned constant value
152 if (((ConstPoolUInt*)Initializer)->getValue() != 0) return false;
153 } else {
154 return false; // Not signed or unsigned? Must be FP type or something
155 }
156
Chris Lattnerd473a0a2001-06-25 07:32:19 +0000157 Value *StepExpr = PN->getIncomingValue(1);
Chris Lattner364b1472001-06-22 02:24:38 +0000158 assert(StepExpr->getValueType() == Value::InstructionVal && "No ADD node?");
159 assert(((Instruction*)StepExpr)->getInstType() == Instruction::Add &&
160 "No ADD node? Not a cannonical PHI!");
161 BinaryOperator *I = (BinaryOperator*)StepExpr;
162 assert(I->getOperand(0)->getValueType() == Value::InstructionVal &&
163 ((Instruction*)I->getOperand(0))->getInstType() == Instruction::PHINode &&
164 "PHI node should be first operand of ADD instruction!");
165
166 // Get the right hand side of the ADD node. See if it is a constant 1.
167 Value *StepSize = I->getOperand(1);
168 if (StepSize->getValueType() != Value::ConstantVal) return false;
169
Chris Lattnerc9f39b22001-06-24 04:05:45 +0000170 if (StepSize->getType()->isSigned()) { // Signed constant value...
171 if (((ConstPoolSInt*)StepSize)->getValue() != 1) return false;
172 } else if (StepSize->getType()->isUnsigned()) { // Unsigned constant value
173 if (((ConstPoolUInt*)StepSize)->getValue() != 1) return false;
174 } else {
175 return false; // Not signed or unsigned? Must be FP type or something
176 }
Chris Lattner364b1472001-06-22 02:24:38 +0000177
Chris Lattnerc9f39b22001-06-24 04:05:45 +0000178 // At this point, we know the initializer is a constant value 0 and the step
179 // size is a constant value 1. This is our simple induction variable!
180 return true;
Chris Lattnerda956802001-06-21 05:27:22 +0000181}
182
Chris Lattnerd473a0a2001-06-25 07:32:19 +0000183// InjectSimpleInductionVariable - Insert a cannonical induction variable into
184// the interval header Header. This assumes that the flow graph is in
185// simplified form (so we know that the header block has exactly 2 predecessors)
186//
187// TODO: This should inherit the largest type that is being used by the already
188// present induction variables (instead of always using uint)
189//
190static PHINode *InjectSimpleInductionVariable(cfg::Interval *Int) {
191 string PHIName, AddName;
192
193 BasicBlock *Header = Int->getHeaderNode();
194 Method *M = Header->getParent();
195
196 if (M->hasSymbolTable()) {
197 // Only name the induction variable if the method isn't stripped.
198 PHIName = M->getSymbolTable()->getUniqueName(Type::UIntTy, "ind_var");
199 AddName = M->getSymbolTable()->getUniqueName(Type::UIntTy, "ind_var_next");
200 }
201
202 // Create the neccesary instructions...
203 PHINode *PN = new PHINode(Type::UIntTy, PHIName);
204 ConstPoolVal *One = new ConstPoolUInt(Type::UIntTy, 1);
205 ConstPoolVal *Zero = new ConstPoolUInt(Type::UIntTy, 0);
206 BinaryOperator *AddNode = BinaryOperator::create(Instruction::Add,
207 PN, One, AddName);
208
209 // Figure out which predecessors I have to play with... there should be
210 // exactly two... one of which is a loop predecessor, and one of which is not.
211 //
212 cfg::pred_iterator PI = cfg::pred_begin(Header);
213 assert(PI != cfg::pred_end(Header) && "Header node should have 2 preds!");
214 BasicBlock *Pred1 = *PI; ++PI;
215 assert(PI != cfg::pred_end(Header) && "Header node should have 2 preds!");
216 BasicBlock *Pred2 = *PI;
217 assert(++PI == cfg::pred_end(Header) && "Header node should have 2 preds!");
218
219 // Make Pred1 be the loop entrance predecessor, Pred2 be the Loop predecessor
220 if (Int->contains(Pred1)) swap(Pred1, Pred2);
221
222 assert(!Int->contains(Pred1) && "Pred1 should be loop entrance!");
223 assert( Int->contains(Pred2) && "Pred2 should be looping edge!");
224
225 // Link the instructions into the PHI node...
226 PN->addIncoming(Zero, Pred1); // The initializer is first argument
227 PN->addIncoming(AddNode, Pred2); // The step size is second PHI argument
228
229 // Insert the PHI node into the Header of the loop. It shall be the first
230 // instruction, because the "Simple" Induction Variable must be first in the
231 // block.
232 //
233 BasicBlock::InstListType &IL = Header->getInstList();
234 IL.push_front(PN);
235
236 // Insert the Add instruction as the first (non-phi) instruction in the
237 // header node's basic block.
238 BasicBlock::InstListType::iterator I = IL.begin();
239 while ((*I)->isPHINode()) ++I;
240 IL.insert(I, AddNode);
241
242 // Insert the constants into the constant pool for the method...
243 M->getConstantPool().insert(One);
244 M->getConstantPool().insert(Zero);
245 return PN;
246}
247
Chris Lattner364b1472001-06-22 02:24:38 +0000248// ProcessInterval - This function is invoked once for each interval in the
249// IntervalPartition of the program. It looks for auxilliary induction
250// variables in loops. If it finds one, it:
251// * Cannonicalizes the induction variable. This consists of:
252// A. Making the first element of the PHI node be the loop invariant
253// computation, and the second element be the linear induction portion.
254// B. Changing the first element of the linear induction portion of the PHI
255// node to be of the form ADD(PHI, <loop invariant expr>).
256// * Add the induction variable PHI to a list of induction variables found.
257//
258// After this, a list of cannonical induction variables is known. This list
259// is searched to see if there is an induction variable that counts from
260// constant 0 with a step size of constant 1. If there is not one, one is
261// injected into the loop. Thus a "simple" induction variable is always known
262//
263// One a simple induction variable is known, all other induction variables are
264// modified to refer to the "simple" induction variable.
265//
266static bool ProcessInterval(cfg::Interval *Int) {
267 if (!Int->isLoop()) return false; // Not a loop? Ignore it!
268
269 vector<PHINode *> InductionVars;
270
271 BasicBlock *Header = Int->getHeaderNode();
272 // Loop over all of the PHI nodes in the interval header...
273 for (BasicBlock::InstListType::iterator I = Header->getInstList().begin(),
274 E = Header->getInstList().end();
275 I != E && (*I)->getInstType() == Instruction::PHINode; ++I) {
276
277 PHINode *PN = (PHINode*)*I;
278 if (PN->getNumIncomingValues() != 2) { // These should be eliminated by now.
279 cerr << "Found interval header with more than 2 predecessors! Ignoring\n";
280 return false; // Todo, make an assertion.
281 }
282
283 // For this to be an induction variable, one of the arguments must be a
284 // loop invariant expression, and the other must be an expression involving
285 // the PHI node, along with possible additions and subtractions of loop
286 // invariant values.
287 //
288 BasicBlock *BB1 = PN->getIncomingBlock(0);
289 Value *V1 = PN->getIncomingValue(0);
290 BasicBlock *BB2 = PN->getIncomingBlock(1);
291 Value *V2 = PN->getIncomingValue(1);
292
293 // Figure out which computation is loop invariant...
294 if (!isLoopInvariant(Int, V1)) {
295 // V1 is *not* loop invariant. Check to see if V2 is:
296 if (isLoopInvariant(Int, V2)) {
297 // They *are* loop invariant. Exchange BB1/BB2 and V1/V2 so that
298 // V1 is always the loop invariant computation.
299 swap(V1, V2); swap(BB1, BB2);
300 } else {
301 // Neither value is loop invariant. Must not be an induction variable.
302 // This case can happen if there is an unreachable loop in the CFG that
303 // has two tail loops in it that was not split by the cleanup phase
304 // before.
305 continue;
306 }
307 }
308
309 // At this point, we know that BB1/V1 are loop invariant. We don't know
310 // anything about BB2/V2. Check now to see if V2 is a linear induction
311 // variable.
312 //
Chris Lattnerd473a0a2001-06-25 07:32:19 +0000313 cerr << "Found loop invariant computation: " << V1 << endl;
Chris Lattner364b1472001-06-22 02:24:38 +0000314
315 if (!isLinearInductionVariable(Int, V2, PN))
316 continue; // No, it is not a linear ind var, ignore the PHI node.
317 cerr << "Found linear induction variable: " << V2;
318
319 // TODO: Cannonicalize V2
320
321 // Add this PHI node to the list of induction variables found...
322 InductionVars.push_back(PN);
323 }
324
325 // No induction variables found?
326 if (InductionVars.empty()) return false;
327
Chris Lattner364b1472001-06-22 02:24:38 +0000328 // Search to see if there is already a "simple" induction variable.
329 vector<PHINode*>::iterator It =
330 find_if(InductionVars.begin(), InductionVars.end(), isSimpleInductionVar);
331
Chris Lattnerc9f39b22001-06-24 04:05:45 +0000332 PHINode *PrimaryIndVar;
333
Chris Lattner364b1472001-06-22 02:24:38 +0000334 // A simple induction variable was not found, inject one now...
335 if (It == InductionVars.end()) {
Chris Lattnerd473a0a2001-06-25 07:32:19 +0000336 PrimaryIndVar = InjectSimpleInductionVariable(Int);
Chris Lattnerc9f39b22001-06-24 04:05:45 +0000337 } else {
338 // Move the PHI node for this induction variable to the start of the PHI
339 // list in HeaderNode... we do not need to do this for the inserted case
340 // because the inserted node will always be placed at the beginning of
341 // HeaderNode.
342 //
343 PrimaryIndVar = *It;
344 BasicBlock::InstListType::iterator i =
345 find(Header->getInstList().begin(), Header->getInstList().end(),
346 PrimaryIndVar);
347 assert(i != Header->getInstList().end() &&
348 "How could Primary IndVar not be in the header!?!!?");
349
350 if (i != Header->getInstList().begin())
351 iter_swap(i, Header->getInstList().begin());
Chris Lattner364b1472001-06-22 02:24:38 +0000352 }
353
Chris Lattnerc9f39b22001-06-24 04:05:45 +0000354 // Now we know that there is a simple induction variable PrimaryIndVar.
355 // Simplify all of the other induction variables to use this induction
356 // variable as their counter, and destroy the PHI nodes that correspond to
357 // the old indvars.
Chris Lattner364b1472001-06-22 02:24:38 +0000358 //
359 // TODO
360
Chris Lattnerc9f39b22001-06-24 04:05:45 +0000361
362 cerr << "Found Interval Header with indvars (primary indvar should be first "
363 << "phi): \n" << Header << "\nPrimaryIndVar = " << PrimaryIndVar;
364
Chris Lattner364b1472001-06-22 02:24:38 +0000365 return false; // TODO: true;
366}
367
368
369// ProcessIntervalPartition - This function loops over the interval partition
370// processing each interval with ProcessInterval
371//
Chris Lattnerda956802001-06-21 05:27:22 +0000372static bool ProcessIntervalPartition(cfg::IntervalPartition &IP) {
373 // This currently just prints out information about the interval structure
374 // of the method...
Chris Lattnerd473a0a2001-06-25 07:32:19 +0000375#if 0
Chris Lattnerda956802001-06-21 05:27:22 +0000376 static unsigned N = 0;
377 cerr << "\n***********Interval Partition #" << (++N) << "************\n\n";
378 copy(IP.begin(), IP.end(), ostream_iterator<cfg::Interval*>(cerr, "\n"));
379
380 cerr << "\n*********** PERFORMING WORK ************\n\n";
Chris Lattnerd473a0a2001-06-25 07:32:19 +0000381#endif
Chris Lattnerda956802001-06-21 05:27:22 +0000382 // Loop over all of the intervals in the partition and look for induction
383 // variables in intervals that represent loops.
384 //
385 return reduce_apply(IP.begin(), IP.end(), bitwise_or<bool>(), false,
386 ptr_fun(ProcessInterval));
Chris Lattnerd213f0f2001-06-20 19:27:11 +0000387}
388
Chris Lattner53b1c012001-06-25 03:55:37 +0000389#include "llvm/Analysis/LoopDepth.h"
Chris Lattner364b1472001-06-22 02:24:38 +0000390
391// DoInductionVariableCannonicalize - Simplify induction variables in loops.
392// This function loops over an interval partition of a program, reducing it
393// until the graph is gone.
Chris Lattnerd213f0f2001-06-20 19:27:11 +0000394//
395bool DoInductionVariableCannonicalize(Method *M) {
Chris Lattnerd473a0a2001-06-25 07:32:19 +0000396 // TODO: REMOVE
397 if (0) { // Print basic blocks with their depth
Chris Lattner53b1c012001-06-25 03:55:37 +0000398 LoopDepthCalculator LDC(M);
399 for (Method::iterator I = M->getBasicBlocks().begin();
400 I != M->getBasicBlocks().end(); ++I) {
401 cerr << "Basic Block Depth: " << LDC.getLoopDepth(*I) << *I;
402 }
Chris Lattner53b1c012001-06-25 03:55:37 +0000403 }
404
405
Chris Lattnerda956802001-06-21 05:27:22 +0000406 cfg::IntervalPartition *IP = new cfg::IntervalPartition(M);
407 bool Changed = false;
Chris Lattnerd213f0f2001-06-20 19:27:11 +0000408
Chris Lattnerda956802001-06-21 05:27:22 +0000409 while (!IP->isDegeneratePartition()) {
410 Changed |= ProcessIntervalPartition(*IP);
Chris Lattner56832052001-06-20 22:44:38 +0000411
Chris Lattnerda956802001-06-21 05:27:22 +0000412 // Calculate the reduced version of this graph until we get to an
413 // irreducible graph or a degenerate graph...
414 //
415 cfg::IntervalPartition *NewIP = new cfg::IntervalPartition(*IP, false);
416 if (NewIP->size() == IP->size()) {
417 cerr << "IRREDUCIBLE GRAPH FOUND!!!\n";
418 return Changed;
419 }
420 delete IP;
421 IP = NewIP;
422 }
Chris Lattner56832052001-06-20 22:44:38 +0000423
Chris Lattnerda956802001-06-21 05:27:22 +0000424 delete IP;
425 return Changed;
Chris Lattnerd213f0f2001-06-20 19:27:11 +0000426}