blob: 42fb9b91471a68ace952dd917c3726c1583c6a40 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This transformation analyzes and transforms the induction variables (and
11// computations derived from them) into simpler forms suitable for subsequent
12// analysis and transformation.
13//
14// This transformation makes the following changes to each loop with an
15// identifiable induction variable:
16// 1. All loops are transformed to have a SINGLE canonical induction variable
17// which starts at zero and steps by one.
18// 2. The canonical induction variable is guaranteed to be the first PHI node
19// in the loop header block.
20// 3. Any pointer arithmetic recurrences are raised to use array subscripts.
21//
22// If the trip count of a loop is computable, this pass also makes the following
23// changes:
24// 1. The exit condition for the loop is canonicalized to compare the
25// induction value against the exit value. This turns loops like:
26// 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
27// 2. Any use outside of the loop of an expression derived from the indvar
28// is changed to compute the derived value outside of the loop, eliminating
29// the dependence on the exit value of the induction variable. If the only
30// purpose of the loop is to compute the exit value of some derived
31// expression, this transformation will make the loop dead.
32//
33// This transformation should be followed by strength reduction after all of the
34// desired loop transformations have been performed. Additionally, on targets
35// where it is profitable, the loop could be transformed to count down to zero
36// (the "do loop" optimization).
37//
38//===----------------------------------------------------------------------===//
39
40#define DEBUG_TYPE "indvars"
41#include "llvm/Transforms/Scalar.h"
42#include "llvm/BasicBlock.h"
43#include "llvm/Constants.h"
44#include "llvm/Instructions.h"
45#include "llvm/Type.h"
46#include "llvm/Analysis/ScalarEvolutionExpander.h"
47#include "llvm/Analysis/LoopInfo.h"
48#include "llvm/Analysis/LoopPass.h"
49#include "llvm/Support/CFG.h"
50#include "llvm/Support/Compiler.h"
51#include "llvm/Support/Debug.h"
52#include "llvm/Support/GetElementPtrTypeIterator.h"
Dan Gohman01c2ee72009-04-16 03:18:22 +000053#include "llvm/Target/TargetData.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054#include "llvm/Transforms/Utils/Local.h"
55#include "llvm/Support/CommandLine.h"
56#include "llvm/ADT/SmallVector.h"
Dan Gohmancacd2012009-02-12 22:19:27 +000057#include "llvm/ADT/SetVector.h"
Chris Lattnerb25465e2008-11-16 07:17:51 +000058#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059#include "llvm/ADT/Statistic.h"
60using namespace llvm;
61
62STATISTIC(NumRemoved , "Number of aux indvars removed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063STATISTIC(NumInserted, "Number of canonical indvars added");
64STATISTIC(NumReplaced, "Number of exit values replaced");
65STATISTIC(NumLFTR , "Number of loop exit tests replaced");
66
67namespace {
68 class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
69 LoopInfo *LI;
Dan Gohman01c2ee72009-04-16 03:18:22 +000070 TargetData *TD;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 ScalarEvolution *SE;
72 bool Changed;
73 public:
74
75 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000076 IndVarSimplify() : LoopPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
Dan Gohmanf3a060a2009-02-17 20:49:49 +000078 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
79
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Devang Patele6a8d482007-09-10 18:08:23 +000081 AU.addRequired<ScalarEvolution>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 AU.addRequiredID(LCSSAID);
83 AU.addRequiredID(LoopSimplifyID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 AU.addRequired<LoopInfo>();
Dan Gohman01c2ee72009-04-16 03:18:22 +000085 AU.addRequired<TargetData>();
Dan Gohman0d35b112009-02-23 16:29:41 +000086 AU.addPreserved<ScalarEvolution>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 AU.addPreservedID(LoopSimplifyID);
88 AU.addPreservedID(LCSSAID);
89 AU.setPreservesCFG();
90 }
91
92 private:
93
Dan Gohmanf3a060a2009-02-17 20:49:49 +000094 void RewriteNonIntegerIVs(Loop *L);
95
Dan Gohman76d5a0d2009-02-24 18:55:53 +000096 void LinearFunctionTestReplace(Loop *L, SCEVHandle BackedgeTakenCount,
Dan Gohman1247dc32009-02-17 15:57:39 +000097 Value *IndVar,
Dan Gohmancacd2012009-02-12 22:19:27 +000098 BasicBlock *ExitingBlock,
99 BranchInst *BI,
Dan Gohmanebac2542009-02-23 23:20:35 +0000100 SCEVExpander &Rewriter);
Dan Gohman9a769972009-04-18 17:56:28 +0000101 void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
Chris Lattnerb25465e2008-11-16 07:17:51 +0000103 void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts);
Devang Patelbda43802008-09-09 21:41:07 +0000104
Dan Gohman963fc812009-02-17 19:13:57 +0000105 void HandleFloatingPointIV(Loop *L, PHINode *PH,
Devang Patelc8dac622008-11-17 21:32:02 +0000106 SmallPtrSet<Instruction*, 16> &DeadInsts);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108}
109
Dan Gohman089efff2008-05-13 00:00:25 +0000110char IndVarSimplify::ID = 0;
111static RegisterPass<IndVarSimplify>
112X("indvars", "Canonicalize Induction Variables");
113
Daniel Dunbar163555a2008-10-22 23:32:42 +0000114Pass *llvm::createIndVarSimplifyPass() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 return new IndVarSimplify();
116}
117
118/// DeleteTriviallyDeadInstructions - If any of the instructions is the
119/// specified set are trivially dead, delete them and see if this makes any of
120/// their operands subsequently dead.
121void IndVarSimplify::
Chris Lattnerb25465e2008-11-16 07:17:51 +0000122DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 while (!Insts.empty()) {
124 Instruction *I = *Insts.begin();
Chris Lattnerb25465e2008-11-16 07:17:51 +0000125 Insts.erase(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 if (isInstructionTriviallyDead(I)) {
127 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
128 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
129 Insts.insert(U);
130 SE->deleteValueFromRecords(I);
131 DOUT << "INDVARS: Deleting: " << *I;
132 I->eraseFromParent();
133 Changed = true;
134 }
135 }
136}
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138/// LinearFunctionTestReplace - This method rewrites the exit condition of the
139/// loop to be a canonical != comparison against the incremented loop induction
140/// variable. This pass is able to rewrite the exit tests of any loop where the
141/// SCEV analysis can determine a loop-invariant trip count of the loop, which
142/// is actually a much broader range than just linear tests.
Dan Gohmancacd2012009-02-12 22:19:27 +0000143void IndVarSimplify::LinearFunctionTestReplace(Loop *L,
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000144 SCEVHandle BackedgeTakenCount,
Dan Gohmancacd2012009-02-12 22:19:27 +0000145 Value *IndVar,
146 BasicBlock *ExitingBlock,
147 BranchInst *BI,
Dan Gohmanebac2542009-02-23 23:20:35 +0000148 SCEVExpander &Rewriter) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 // If the exiting block is not the same as the backedge block, we must compare
150 // against the preincremented value, otherwise we prefer to compare against
151 // the post-incremented value.
Dan Gohmancacd2012009-02-12 22:19:27 +0000152 Value *CmpIndVar;
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000153 SCEVHandle RHS = BackedgeTakenCount;
Dan Gohmancacd2012009-02-12 22:19:27 +0000154 if (ExitingBlock == L->getLoopLatch()) {
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000155 // Add one to the "backedge-taken" count to get the trip count.
156 // If this addition may overflow, we have to be more pessimistic and
157 // cast the induction variable before doing the add.
158 SCEVHandle Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
Dan Gohmancacd2012009-02-12 22:19:27 +0000159 SCEVHandle N =
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000160 SE->getAddExpr(BackedgeTakenCount,
161 SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
Dan Gohmancacd2012009-02-12 22:19:27 +0000162 if ((isa<SCEVConstant>(N) && !N->isZero()) ||
163 SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
164 // No overflow. Cast the sum.
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000165 RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
Dan Gohmancacd2012009-02-12 22:19:27 +0000166 } else {
167 // Potential overflow. Cast before doing the add.
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000168 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
169 IndVar->getType());
170 RHS = SE->getAddExpr(RHS,
171 SE->getIntegerSCEV(1, IndVar->getType()));
Dan Gohmancacd2012009-02-12 22:19:27 +0000172 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000174 // The BackedgeTaken expression contains the number of times that the
175 // backedge branches to the loop header. This is one less than the
176 // number of times the loop executes, so use the incremented indvar.
Dan Gohmancacd2012009-02-12 22:19:27 +0000177 CmpIndVar = L->getCanonicalInductionVariableIncrement();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 } else {
179 // We have to use the preincremented value...
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000180 RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
181 IndVar->getType());
Dan Gohmancacd2012009-02-12 22:19:27 +0000182 CmpIndVar = IndVar;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184
185 // Expand the code for the iteration count into the preheader of the loop.
186 BasicBlock *Preheader = L->getLoopPreheader();
Dan Gohman01c2ee72009-04-16 03:18:22 +0000187 Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(),
Dan Gohmancacd2012009-02-12 22:19:27 +0000188 Preheader->getTerminator());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189
190 // Insert a new icmp_ne or icmp_eq instruction before the branch.
191 ICmpInst::Predicate Opcode;
192 if (L->contains(BI->getSuccessor(0)))
193 Opcode = ICmpInst::ICMP_NE;
194 else
195 Opcode = ICmpInst::ICMP_EQ;
196
Dan Gohmancacd2012009-02-12 22:19:27 +0000197 DOUT << "INDVARS: Rewriting loop exit condition to:\n"
198 << " LHS:" << *CmpIndVar // includes a newline
199 << " op:\t"
Dan Gohman8555ff72009-02-14 02:26:50 +0000200 << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000201 << " RHS:\t" << *RHS << "\n";
Dan Gohmancacd2012009-02-12 22:19:27 +0000202
203 Value *Cond = new ICmpInst(Opcode, CmpIndVar, ExitCnt, "exitcond", BI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 BI->setCondition(Cond);
205 ++NumLFTR;
206 Changed = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207}
208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209/// RewriteLoopExitValues - Check to see if this loop has a computable
210/// loop-invariant execution count. If so, this means that we can compute the
211/// final value of any expressions that are recurrent in the loop, and
212/// substitute the exit values from the loop into any instructions outside of
213/// the loop that use the final values of the current expressions.
Dan Gohman9a769972009-04-18 17:56:28 +0000214void IndVarSimplify::RewriteLoopExitValues(Loop *L,
215 const SCEV *BackedgeTakenCount) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 BasicBlock *Preheader = L->getLoopPreheader();
217
218 // Scan all of the instructions in the loop, looking at those that have
219 // extra-loop users and which are recurrences.
Dan Gohman01c2ee72009-04-16 03:18:22 +0000220 SCEVExpander Rewriter(*SE, *LI, *TD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221
222 // We insert the code into the preheader of the loop if the loop contains
223 // multiple exit blocks, or in the exit block if there is exactly one.
224 BasicBlock *BlockToInsertInto;
Devang Patel02451fa2007-08-21 00:31:24 +0000225 SmallVector<BasicBlock*, 8> ExitBlocks;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 L->getUniqueExitBlocks(ExitBlocks);
227 if (ExitBlocks.size() == 1)
228 BlockToInsertInto = ExitBlocks[0];
229 else
230 BlockToInsertInto = Preheader;
Dan Gohman514277c2008-05-23 21:05:58 +0000231 BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000233 bool HasConstantItCount = isa<SCEVConstant>(BackedgeTakenCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234
Chris Lattnerb25465e2008-11-16 07:17:51 +0000235 SmallPtrSet<Instruction*, 16> InstructionsToDelete;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 std::map<Instruction*, Value*> ExitValues;
237
238 // Find all values that are computed inside the loop, but used outside of it.
239 // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
240 // the exit blocks of the loop to find them.
241 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
242 BasicBlock *ExitBB = ExitBlocks[i];
Dan Gohman963fc812009-02-17 19:13:57 +0000243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 // If there are no PHI nodes in this exit block, then no values defined
245 // inside the loop are used on this path, skip it.
246 PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
247 if (!PN) continue;
Dan Gohman963fc812009-02-17 19:13:57 +0000248
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 unsigned NumPreds = PN->getNumIncomingValues();
Dan Gohman963fc812009-02-17 19:13:57 +0000250
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 // Iterate over all of the PHI nodes.
252 BasicBlock::iterator BBI = ExitBB->begin();
253 while ((PN = dyn_cast<PHINode>(BBI++))) {
Dan Gohman963fc812009-02-17 19:13:57 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 // Iterate over all of the values in all the PHI nodes.
256 for (unsigned i = 0; i != NumPreds; ++i) {
257 // If the value being merged in is not integer or is not defined
258 // in the loop, skip it.
259 Value *InVal = PN->getIncomingValue(i);
260 if (!isa<Instruction>(InVal) ||
261 // SCEV only supports integer expressions for now.
Dan Gohman01c2ee72009-04-16 03:18:22 +0000262 (!isa<IntegerType>(InVal->getType()) &&
263 !isa<PointerType>(InVal->getType())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 continue;
265
266 // If this pred is for a subloop, not L itself, skip it.
Dan Gohman963fc812009-02-17 19:13:57 +0000267 if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 continue; // The Block is in a subloop, skip it.
269
270 // Check that InVal is defined in the loop.
271 Instruction *Inst = cast<Instruction>(InVal);
272 if (!L->contains(Inst->getParent()))
273 continue;
Dan Gohman963fc812009-02-17 19:13:57 +0000274
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 // We require that this value either have a computable evolution or that
276 // the loop have a constant iteration count. In the case where the loop
277 // has a constant iteration count, we can sometimes force evaluation of
278 // the exit value through brute force.
279 SCEVHandle SH = SE->getSCEV(Inst);
280 if (!SH->hasComputableLoopEvolution(L) && !HasConstantItCount)
281 continue; // Cannot get exit evolution for the loop value.
Dan Gohman963fc812009-02-17 19:13:57 +0000282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 // Okay, this instruction has a user outside of the current loop
284 // and varies predictably *inside* the loop. Evaluate the value it
285 // contains when the loop exits, if possible.
286 SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
287 if (isa<SCEVCouldNotCompute>(ExitValue) ||
288 !ExitValue->isLoopInvariant(L))
289 continue;
290
291 Changed = true;
292 ++NumReplaced;
Dan Gohman963fc812009-02-17 19:13:57 +0000293
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 // See if we already computed the exit value for the instruction, if so,
295 // just reuse it.
296 Value *&ExitVal = ExitValues[Inst];
297 if (!ExitVal)
Dan Gohman01c2ee72009-04-16 03:18:22 +0000298 ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), InsertPt);
Dan Gohman963fc812009-02-17 19:13:57 +0000299
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal
301 << " LoopVal = " << *Inst << "\n";
302
303 PN->setIncomingValue(i, ExitVal);
Dan Gohman963fc812009-02-17 19:13:57 +0000304
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 // If this instruction is dead now, schedule it to be removed.
306 if (Inst->use_empty())
307 InstructionsToDelete.insert(Inst);
Dan Gohman963fc812009-02-17 19:13:57 +0000308
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 // See if this is a single-entry LCSSA PHI node. If so, we can (and
310 // have to) remove
311 // the PHI entirely. This is safe, because the NewVal won't be variant
312 // in the loop, so we don't need an LCSSA phi node anymore.
313 if (NumPreds == 1) {
314 SE->deleteValueFromRecords(PN);
315 PN->replaceAllUsesWith(ExitVal);
316 PN->eraseFromParent();
317 break;
318 }
319 }
320 }
321 }
Dan Gohman963fc812009-02-17 19:13:57 +0000322
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 DeleteTriviallyDeadInstructions(InstructionsToDelete);
324}
325
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000326void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
Dan Gohman01c2ee72009-04-16 03:18:22 +0000327 // First step. Check to see if there are any floating-point recurrences.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 // If there are, change them into integer recurrences, permitting analysis by
329 // the SCEV routines.
330 //
331 BasicBlock *Header = L->getHeader();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
Chris Lattnerb25465e2008-11-16 07:17:51 +0000333 SmallPtrSet<Instruction*, 16> DeadInsts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
335 PHINode *PN = cast<PHINode>(I);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000336 HandleFloatingPointIV(L, PN, DeadInsts);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 }
338
Dan Gohman01c2ee72009-04-16 03:18:22 +0000339 // If the loop previously had floating-point IV, ScalarEvolution
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000340 // may not have been able to compute a trip count. Now that we've done some
341 // re-writing, the trip count may be computable.
342 if (Changed)
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000343 SE->forgetLoopBackedgeTakenCount(L);
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 if (!DeadInsts.empty())
346 DeleteTriviallyDeadInstructions(DeadInsts);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347}
348
Dan Gohmancacd2012009-02-12 22:19:27 +0000349/// getEffectiveIndvarType - Determine the widest type that the
350/// induction-variable PHINode Phi is cast to.
351///
Dan Gohman01c2ee72009-04-16 03:18:22 +0000352static const Type *getEffectiveIndvarType(const PHINode *Phi,
353 const TargetData *TD) {
Dan Gohmancacd2012009-02-12 22:19:27 +0000354 const Type *Ty = Phi->getType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355
Dan Gohmancacd2012009-02-12 22:19:27 +0000356 for (Value::use_const_iterator UI = Phi->use_begin(), UE = Phi->use_end();
357 UI != UE; ++UI) {
358 const Type *CandidateType = NULL;
359 if (const ZExtInst *ZI = dyn_cast<ZExtInst>(UI))
360 CandidateType = ZI->getDestTy();
361 else if (const SExtInst *SI = dyn_cast<SExtInst>(UI))
362 CandidateType = SI->getDestTy();
363 if (CandidateType &&
Dan Gohman01c2ee72009-04-16 03:18:22 +0000364 TD->getTypeSizeInBits(CandidateType) > TD->getTypeSizeInBits(Ty))
Dan Gohmancacd2012009-02-12 22:19:27 +0000365 Ty = CandidateType;
366 }
367
368 return Ty;
369}
370
Dan Gohmancecc80f2009-02-14 02:31:09 +0000371/// TestOrigIVForWrap - Analyze the original induction variable
Dan Gohmana730da32009-02-18 00:52:00 +0000372/// that controls the loop's iteration to determine whether it
Dan Gohmana5d38012009-02-18 17:22:41 +0000373/// would ever undergo signed or unsigned overflow. Also, check
374/// whether an induction variable in the same type that starts
375/// at 0 would undergo signed overflow.
Dan Gohmana730da32009-02-18 00:52:00 +0000376///
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000377/// In addition to setting the NoSignedWrap and NoUnsignedWrap
378/// variables to true when appropriate (they are not set to false here),
379/// return the PHI for this induction variable. Also record the initial
380/// and final values and the increment; these are not meaningful unless
381/// either NoSignedWrap or NoUnsignedWrap is true, and are always meaningful
382/// in that case, although the final value may be 0 indicating a nonconstant.
Dan Gohmancacd2012009-02-12 22:19:27 +0000383///
384/// TODO: This duplicates a fair amount of ScalarEvolution logic.
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000385/// Perhaps this can be merged with
386/// ScalarEvolution::getBackedgeTakenCount
Dan Gohmancecc80f2009-02-14 02:31:09 +0000387/// and/or ScalarEvolution::get{Sign,Zero}ExtendExpr.
Dan Gohmancacd2012009-02-12 22:19:27 +0000388///
Dan Gohmana730da32009-02-18 00:52:00 +0000389static const PHINode *TestOrigIVForWrap(const Loop *L,
390 const BranchInst *BI,
391 const Instruction *OrigCond,
Dan Gohman01c2ee72009-04-16 03:18:22 +0000392 const TargetData *TD,
Dan Gohmana730da32009-02-18 00:52:00 +0000393 bool &NoSignedWrap,
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000394 bool &NoUnsignedWrap,
395 const ConstantInt* &InitialVal,
396 const ConstantInt* &IncrVal,
397 const ConstantInt* &LimitVal) {
Dan Gohmancacd2012009-02-12 22:19:27 +0000398 // Verify that the loop is sane and find the exit condition.
399 const ICmpInst *Cmp = dyn_cast<ICmpInst>(OrigCond);
Dan Gohmana730da32009-02-18 00:52:00 +0000400 if (!Cmp) return 0;
Dan Gohmancacd2012009-02-12 22:19:27 +0000401
Dan Gohmancecc80f2009-02-14 02:31:09 +0000402 const Value *CmpLHS = Cmp->getOperand(0);
403 const Value *CmpRHS = Cmp->getOperand(1);
404 const BasicBlock *TrueBB = BI->getSuccessor(0);
405 const BasicBlock *FalseBB = BI->getSuccessor(1);
406 ICmpInst::Predicate Pred = Cmp->getPredicate();
Dan Gohmancacd2012009-02-12 22:19:27 +0000407
Dan Gohmancecc80f2009-02-14 02:31:09 +0000408 // Canonicalize a constant to the RHS.
409 if (isa<ConstantInt>(CmpLHS)) {
410 Pred = ICmpInst::getSwappedPredicate(Pred);
411 std::swap(CmpLHS, CmpRHS);
412 }
413 // Canonicalize SLE to SLT.
414 if (Pred == ICmpInst::ICMP_SLE)
415 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS))
416 if (!CI->getValue().isMaxSignedValue()) {
417 CmpRHS = ConstantInt::get(CI->getValue() + 1);
418 Pred = ICmpInst::ICMP_SLT;
419 }
420 // Canonicalize SGT to SGE.
421 if (Pred == ICmpInst::ICMP_SGT)
422 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS))
423 if (!CI->getValue().isMaxSignedValue()) {
424 CmpRHS = ConstantInt::get(CI->getValue() + 1);
425 Pred = ICmpInst::ICMP_SGE;
426 }
427 // Canonicalize SGE to SLT.
428 if (Pred == ICmpInst::ICMP_SGE) {
429 std::swap(TrueBB, FalseBB);
430 Pred = ICmpInst::ICMP_SLT;
431 }
432 // Canonicalize ULE to ULT.
433 if (Pred == ICmpInst::ICMP_ULE)
434 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS))
435 if (!CI->getValue().isMaxValue()) {
436 CmpRHS = ConstantInt::get(CI->getValue() + 1);
437 Pred = ICmpInst::ICMP_ULT;
438 }
439 // Canonicalize UGT to UGE.
440 if (Pred == ICmpInst::ICMP_UGT)
441 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS))
442 if (!CI->getValue().isMaxValue()) {
443 CmpRHS = ConstantInt::get(CI->getValue() + 1);
444 Pred = ICmpInst::ICMP_UGE;
445 }
446 // Canonicalize UGE to ULT.
447 if (Pred == ICmpInst::ICMP_UGE) {
448 std::swap(TrueBB, FalseBB);
449 Pred = ICmpInst::ICMP_ULT;
450 }
451 // For now, analyze only LT loops for signed overflow.
452 if (Pred != ICmpInst::ICMP_SLT && Pred != ICmpInst::ICMP_ULT)
Dan Gohmana730da32009-02-18 00:52:00 +0000453 return 0;
Dan Gohmancecc80f2009-02-14 02:31:09 +0000454
455 bool isSigned = Pred == ICmpInst::ICMP_SLT;
456
457 // Get the increment instruction. Look past casts if we will
Dan Gohmancacd2012009-02-12 22:19:27 +0000458 // be able to prove that the original induction variable doesn't
Dan Gohmancecc80f2009-02-14 02:31:09 +0000459 // undergo signed or unsigned overflow, respectively.
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000460 const Value *IncrInst = CmpLHS;
Dan Gohmancecc80f2009-02-14 02:31:09 +0000461 if (isSigned) {
462 if (const SExtInst *SI = dyn_cast<SExtInst>(CmpLHS)) {
463 if (!isa<ConstantInt>(CmpRHS) ||
464 !cast<ConstantInt>(CmpRHS)->getValue()
Dan Gohman01c2ee72009-04-16 03:18:22 +0000465 .isSignedIntN(TD->getTypeSizeInBits(IncrInst->getType())))
Dan Gohmana730da32009-02-18 00:52:00 +0000466 return 0;
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000467 IncrInst = SI->getOperand(0);
Dan Gohmancecc80f2009-02-14 02:31:09 +0000468 }
469 } else {
470 if (const ZExtInst *ZI = dyn_cast<ZExtInst>(CmpLHS)) {
471 if (!isa<ConstantInt>(CmpRHS) ||
472 !cast<ConstantInt>(CmpRHS)->getValue()
Dan Gohman01c2ee72009-04-16 03:18:22 +0000473 .isIntN(TD->getTypeSizeInBits(IncrInst->getType())))
Dan Gohmana730da32009-02-18 00:52:00 +0000474 return 0;
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000475 IncrInst = ZI->getOperand(0);
Dan Gohmancecc80f2009-02-14 02:31:09 +0000476 }
Dan Gohmancacd2012009-02-12 22:19:27 +0000477 }
478
479 // For now, only analyze induction variables that have simple increments.
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000480 const BinaryOperator *IncrOp = dyn_cast<BinaryOperator>(IncrInst);
481 if (!IncrOp || IncrOp->getOpcode() != Instruction::Add)
482 return 0;
483 IncrVal = dyn_cast<ConstantInt>(IncrOp->getOperand(1));
484 if (!IncrVal)
Dan Gohmana730da32009-02-18 00:52:00 +0000485 return 0;
Dan Gohmancacd2012009-02-12 22:19:27 +0000486
487 // Make sure the PHI looks like a normal IV.
488 const PHINode *PN = dyn_cast<PHINode>(IncrOp->getOperand(0));
489 if (!PN || PN->getNumIncomingValues() != 2)
Dan Gohmana730da32009-02-18 00:52:00 +0000490 return 0;
Dan Gohmancacd2012009-02-12 22:19:27 +0000491 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
492 unsigned BackEdge = !IncomingEdge;
493 if (!L->contains(PN->getIncomingBlock(BackEdge)) ||
494 PN->getIncomingValue(BackEdge) != IncrOp)
Dan Gohmana730da32009-02-18 00:52:00 +0000495 return 0;
Dan Gohmancecc80f2009-02-14 02:31:09 +0000496 if (!L->contains(TrueBB))
Dan Gohmana730da32009-02-18 00:52:00 +0000497 return 0;
Dan Gohmancacd2012009-02-12 22:19:27 +0000498
499 // For now, only analyze loops with a constant start value, so that
Dan Gohmancecc80f2009-02-14 02:31:09 +0000500 // we can easily determine if the start value is not a maximum value
501 // which would wrap on the first iteration.
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000502 InitialVal = dyn_cast<ConstantInt>(PN->getIncomingValue(IncomingEdge));
Dan Gohman6f2a83e2009-02-18 16:54:33 +0000503 if (!InitialVal)
Dan Gohmana730da32009-02-18 00:52:00 +0000504 return 0;
Dan Gohmancacd2012009-02-12 22:19:27 +0000505
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000506 // The upper limit need not be a constant; we'll check later.
507 LimitVal = dyn_cast<ConstantInt>(CmpRHS);
508
509 // We detect the impossibility of wrapping in two cases, both of
510 // which require starting with a non-max value:
511 // - The IV counts up by one, and the loop iterates only while it remains
512 // less than a limiting value (any) in the same type.
513 // - The IV counts up by a positive increment other than 1, and the
514 // constant limiting value + the increment is less than the max value
515 // (computed as max-increment to avoid overflow)
Dan Gohmana5d38012009-02-18 17:22:41 +0000516 if (isSigned && !InitialVal->getValue().isMaxSignedValue()) {
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000517 if (IncrVal->equalsInt(1))
518 NoSignedWrap = true; // LimitVal need not be constant
519 else if (LimitVal) {
520 uint64_t numBits = LimitVal->getValue().getBitWidth();
521 if (IncrVal->getValue().sgt(APInt::getNullValue(numBits)) &&
522 (APInt::getSignedMaxValue(numBits) - IncrVal->getValue())
523 .sgt(LimitVal->getValue()))
524 NoSignedWrap = true;
525 }
526 } else if (!isSigned && !InitialVal->getValue().isMaxValue()) {
527 if (IncrVal->equalsInt(1))
528 NoUnsignedWrap = true; // LimitVal need not be constant
529 else if (LimitVal) {
530 uint64_t numBits = LimitVal->getValue().getBitWidth();
531 if (IncrVal->getValue().ugt(APInt::getNullValue(numBits)) &&
532 (APInt::getMaxValue(numBits) - IncrVal->getValue())
533 .ugt(LimitVal->getValue()))
534 NoUnsignedWrap = true;
535 }
536 }
Dan Gohmana730da32009-02-18 00:52:00 +0000537 return PN;
Dan Gohmancacd2012009-02-12 22:19:27 +0000538}
539
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000540static Value *getSignExtendedTruncVar(const SCEVAddRecExpr *AR,
541 ScalarEvolution *SE,
542 const Type *LargestType, Loop *L,
543 const Type *myType,
544 SCEVExpander &Rewriter,
545 BasicBlock::iterator InsertPt) {
546 SCEVHandle ExtendedStart =
547 SE->getSignExtendExpr(AR->getStart(), LargestType);
548 SCEVHandle ExtendedStep =
549 SE->getSignExtendExpr(AR->getStepRecurrence(*SE), LargestType);
550 SCEVHandle ExtendedAddRec =
551 SE->getAddRecExpr(ExtendedStart, ExtendedStep, L);
552 if (LargestType != myType)
553 ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000554 return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt);
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000555}
556
557static Value *getZeroExtendedTruncVar(const SCEVAddRecExpr *AR,
558 ScalarEvolution *SE,
559 const Type *LargestType, Loop *L,
560 const Type *myType,
561 SCEVExpander &Rewriter,
562 BasicBlock::iterator InsertPt) {
563 SCEVHandle ExtendedStart =
564 SE->getZeroExtendExpr(AR->getStart(), LargestType);
565 SCEVHandle ExtendedStep =
566 SE->getZeroExtendExpr(AR->getStepRecurrence(*SE), LargestType);
567 SCEVHandle ExtendedAddRec =
568 SE->getAddRecExpr(ExtendedStart, ExtendedStep, L);
569 if (LargestType != myType)
570 ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000571 return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt);
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000572}
573
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000574/// allUsesAreSameTyped - See whether all Uses of I are instructions
575/// with the same Opcode and the same type.
576static bool allUsesAreSameTyped(unsigned int Opcode, Instruction *I) {
577 const Type* firstType = NULL;
578 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
579 UI != UE; ++UI) {
580 Instruction *II = dyn_cast<Instruction>(*UI);
581 if (!II || II->getOpcode() != Opcode)
582 return false;
583 if (!firstType)
584 firstType = II->getType();
585 else if (firstType != II->getType())
586 return false;
587 }
588 return true;
589}
590
Dan Gohmancacd2012009-02-12 22:19:27 +0000591bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 LI = &getAnalysis<LoopInfo>();
Dan Gohman01c2ee72009-04-16 03:18:22 +0000593 TD = &getAnalysis<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 SE = &getAnalysis<ScalarEvolution>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 Changed = false;
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000596
Dan Gohman01c2ee72009-04-16 03:18:22 +0000597 // If there are any floating-point recurrences, attempt to
Dan Gohmanf3a060a2009-02-17 20:49:49 +0000598 // transform them to use integer recurrences.
599 RewriteNonIntegerIVs(L);
600
Dan Gohmancacd2012009-02-12 22:19:27 +0000601 BasicBlock *Header = L->getHeader();
602 BasicBlock *ExitingBlock = L->getExitingBlock();
Chris Lattnerb25465e2008-11-16 07:17:51 +0000603 SmallPtrSet<Instruction*, 16> DeadInsts;
Dan Gohmancacd2012009-02-12 22:19:27 +0000604
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000605 // Verify the input to the pass in already in LCSSA form.
606 assert(L->isLCSSAForm());
607
608 // Check to see if this loop has a computable loop-invariant execution count.
609 // If so, this means that we can compute the final value of any expressions
610 // that are recurrent in the loop, and substitute the exit values from the
611 // loop into any instructions outside of the loop that use the final values of
612 // the current expressions.
613 //
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000614 SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
615 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
616 RewriteLoopExitValues(L, BackedgeTakenCount);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617
618 // Next, analyze all of the induction variables in the loop, canonicalizing
619 // auxillary induction variables.
620 std::vector<std::pair<PHINode*, SCEVHandle> > IndVars;
621
622 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
623 PHINode *PN = cast<PHINode>(I);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000624 if (PN->getType()->isInteger() || isa<PointerType>(PN->getType())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000625 SCEVHandle SCEV = SE->getSCEV(PN);
Dan Gohman173d9142009-02-14 02:25:19 +0000626 // FIXME: It is an extremely bad idea to indvar substitute anything more
627 // complex than affine induction variables. Doing so will put expensive
628 // polynomial evaluations inside of the loop, and the str reduction pass
629 // currently can only reduce affine polynomials. For now just disable
630 // indvar subst on anything more complex than an affine addrec.
Dan Gohman9a769972009-04-18 17:56:28 +0000631 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV))
Dan Gohman173d9142009-02-14 02:25:19 +0000632 if (AR->getLoop() == L && AR->isAffine())
633 IndVars.push_back(std::make_pair(PN, SCEV));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 }
635 }
636
Dan Gohmancacd2012009-02-12 22:19:27 +0000637 // Compute the type of the largest recurrence expression, and collect
638 // the set of the types of the other recurrence expressions.
639 const Type *LargestType = 0;
640 SmallSetVector<const Type *, 4> SizesToInsert;
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000641 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
642 LargestType = BackedgeTakenCount->getType();
Dan Gohman01c2ee72009-04-16 03:18:22 +0000643 if (isa<PointerType>(LargestType))
644 LargestType = TD->getIntPtrType();
645 SizesToInsert.insert(LargestType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000646 }
Dan Gohmancacd2012009-02-12 22:19:27 +0000647 for (unsigned i = 0, e = IndVars.size(); i != e; ++i) {
648 const PHINode *PN = IndVars[i].first;
Dan Gohman01c2ee72009-04-16 03:18:22 +0000649 const Type *PNTy = PN->getType();
650 if (isa<PointerType>(PNTy)) PNTy = TD->getIntPtrType();
651 SizesToInsert.insert(PNTy);
652 const Type *EffTy = getEffectiveIndvarType(PN, TD);
653 if (isa<PointerType>(EffTy)) EffTy = TD->getIntPtrType();
Dan Gohmancacd2012009-02-12 22:19:27 +0000654 SizesToInsert.insert(EffTy);
655 if (!LargestType ||
Dan Gohman01c2ee72009-04-16 03:18:22 +0000656 TD->getTypeSizeInBits(EffTy) >
657 TD->getTypeSizeInBits(LargestType))
Dan Gohmancacd2012009-02-12 22:19:27 +0000658 LargestType = EffTy;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 }
660
661 // Create a rewriter object which we'll use to transform the code with.
Dan Gohman01c2ee72009-04-16 03:18:22 +0000662 SCEVExpander Rewriter(*SE, *LI, *TD);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000663
664 // Now that we know the largest of of the induction variables in this loop,
665 // insert a canonical induction variable of the largest size.
Dan Gohmancacd2012009-02-12 22:19:27 +0000666 Value *IndVar = 0;
667 if (!SizesToInsert.empty()) {
668 IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType);
669 ++NumInserted;
670 Changed = true;
671 DOUT << "INDVARS: New CanIV: " << *IndVar;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 }
673
Dan Gohmancacd2012009-02-12 22:19:27 +0000674 // If we have a trip count expression, rewrite the loop's exit condition
675 // using it. We can currently only handle loops with a single exit.
Dan Gohmancecc80f2009-02-14 02:31:09 +0000676 bool NoSignedWrap = false;
677 bool NoUnsignedWrap = false;
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000678 const ConstantInt* InitialVal, * IncrVal, * LimitVal;
Dan Gohmana730da32009-02-18 00:52:00 +0000679 const PHINode *OrigControllingPHI = 0;
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000680 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock)
Dan Gohmancacd2012009-02-12 22:19:27 +0000681 // Can't rewrite non-branch yet.
682 if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) {
683 if (Instruction *OrigCond = dyn_cast<Instruction>(BI->getCondition())) {
Dan Gohmancecc80f2009-02-14 02:31:09 +0000684 // Determine if the OrigIV will ever undergo overflow.
Dan Gohmana730da32009-02-18 00:52:00 +0000685 OrigControllingPHI =
Dan Gohman01c2ee72009-04-16 03:18:22 +0000686 TestOrigIVForWrap(L, BI, OrigCond, TD,
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000687 NoSignedWrap, NoUnsignedWrap,
688 InitialVal, IncrVal, LimitVal);
Dan Gohmancacd2012009-02-12 22:19:27 +0000689
690 // We'll be replacing the original condition, so it'll be dead.
691 DeadInsts.insert(OrigCond);
692 }
693
Dan Gohman76d5a0d2009-02-24 18:55:53 +0000694 LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
Dan Gohmanebac2542009-02-23 23:20:35 +0000695 ExitingBlock, BI, Rewriter);
Dan Gohmancacd2012009-02-12 22:19:27 +0000696 }
697
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000698 // Now that we have a canonical induction variable, we can rewrite any
699 // recurrences in terms of the induction variable. Start with the auxillary
700 // induction variables, and recursively rewrite any of their uses.
Dan Gohman514277c2008-05-23 21:05:58 +0000701 BasicBlock::iterator InsertPt = Header->getFirstNonPHI();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702
703 // If there were induction variables of other sizes, cast the primary
704 // induction variable to the right size for them, avoiding the need for the
705 // code evaluation methods to insert induction variables of different sizes.
Dan Gohmancacd2012009-02-12 22:19:27 +0000706 for (unsigned i = 0, e = SizesToInsert.size(); i != e; ++i) {
707 const Type *Ty = SizesToInsert[i];
708 if (Ty != LargestType) {
709 Instruction *New = new TruncInst(IndVar, Ty, "indvar", InsertPt);
710 Rewriter.addInsertedValue(New, SE->getSCEV(New));
711 DOUT << "INDVARS: Made trunc IV for type " << *Ty << ": "
712 << *New << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000713 }
714 }
715
716 // Rewrite all induction variables in terms of the canonical induction
717 // variable.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 while (!IndVars.empty()) {
719 PHINode *PN = IndVars.back().first;
Dan Gohman9a769972009-04-18 17:56:28 +0000720 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(IndVars.back().second);
Dan Gohman01c2ee72009-04-16 03:18:22 +0000721 Value *NewVal = Rewriter.expandCodeFor(AR, PN->getType(), InsertPt);
Dan Gohmanc71cac12009-02-17 00:10:53 +0000722 DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *PN
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000723 << " into = " << *NewVal << "\n";
724 NewVal->takeName(PN);
725
Dan Gohmancacd2012009-02-12 22:19:27 +0000726 /// If the new canonical induction variable is wider than the original,
727 /// and the original has uses that are casts to wider types, see if the
728 /// truncate and extend can be omitted.
Dan Gohmana730da32009-02-18 00:52:00 +0000729 if (PN == OrigControllingPHI && PN->getType() != LargestType)
Dan Gohmancacd2012009-02-12 22:19:27 +0000730 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
Dan Gohmancecc80f2009-02-14 02:31:09 +0000731 UI != UE; ++UI) {
Dale Johannesencb91b762009-04-15 20:41:02 +0000732 Instruction *UInst = dyn_cast<Instruction>(*UI);
733 if (UInst && isa<SExtInst>(UInst) && NoSignedWrap) {
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000734 Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType, L,
Dale Johannesencb91b762009-04-15 20:41:02 +0000735 UInst->getType(), Rewriter, InsertPt);
736 UInst->replaceAllUsesWith(TruncIndVar);
737 DeadInsts.insert(UInst);
Dan Gohmancacd2012009-02-12 22:19:27 +0000738 }
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000739 // See if we can figure out sext(i+constant) doesn't wrap, so we can
740 // use a larger add. This is common in subscripting.
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000741 if (UInst && UInst->getOpcode()==Instruction::Add &&
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000742 allUsesAreSameTyped(Instruction::SExt, UInst) &&
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000743 isa<ConstantInt>(UInst->getOperand(1)) &&
Dale Johannesencb91b762009-04-15 20:41:02 +0000744 NoSignedWrap && LimitVal) {
745 uint64_t oldBitSize = LimitVal->getValue().getBitWidth();
746 uint64_t newBitSize = LargestType->getPrimitiveSizeInBits();
747 ConstantInt* AddRHS = dyn_cast<ConstantInt>(UInst->getOperand(1));
748 if (((APInt::getSignedMaxValue(oldBitSize) - IncrVal->getValue()) -
749 AddRHS->getValue()).sgt(LimitVal->getValue())) {
750 // We've determined this is (i+constant) and it won't overflow.
751 if (isa<SExtInst>(UInst->use_begin())) {
752 SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin());
753 Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
754 L, oldSext->getType(), Rewriter,
755 InsertPt);
756 APInt APcopy = APInt(AddRHS->getValue());
757 ConstantInt* newAddRHS =ConstantInt::get(APcopy.sext(newBitSize));
758 Value *NewAdd =
759 BinaryOperator::CreateAdd(TruncIndVar, newAddRHS,
760 UInst->getName()+".nosex", UInst);
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000761 for (Value::use_iterator UI2 = UInst->use_begin(),
762 UE2 = UInst->use_end(); UI2 != UE2; ++UI2) {
763 Instruction *II = dyn_cast<Instruction>(UI2);
764 II->replaceAllUsesWith(NewAdd);
765 DeadInsts.insert(II);
766 }
Dale Johannesencb91b762009-04-15 20:41:02 +0000767 DeadInsts.insert(UInst);
768 }
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000769 }
770 }
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000771 // Try for sext(i | constant). This is safe as long as the
772 // high bit of the constant is not set.
773 if (UInst && UInst->getOpcode()==Instruction::Or &&
774 allUsesAreSameTyped(Instruction::SExt, UInst) && NoSignedWrap &&
775 isa<ConstantInt>(UInst->getOperand(1))) {
776 ConstantInt* RHS = dyn_cast<ConstantInt>(UInst->getOperand(1));
777 if (!RHS->getValue().isNegative()) {
778 uint64_t newBitSize = LargestType->getPrimitiveSizeInBits();
779 SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin());
780 Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
781 L, oldSext->getType(), Rewriter,
782 InsertPt);
783 APInt APcopy = APInt(RHS->getValue());
784 ConstantInt* newRHS =ConstantInt::get(APcopy.sext(newBitSize));
785 Value *NewAdd =
786 BinaryOperator::CreateOr(TruncIndVar, newRHS,
787 UInst->getName()+".nosex", UInst);
788 for (Value::use_iterator UI2 = UInst->use_begin(),
789 UE2 = UInst->use_end(); UI2 != UE2; ++UI2) {
790 Instruction *II = dyn_cast<Instruction>(UI2);
791 II->replaceAllUsesWith(NewAdd);
792 DeadInsts.insert(II);
793 }
794 DeadInsts.insert(UInst);
795 }
796 }
797 // A zext of a signed variable known not to overflow is still safe.
798 if (UInst && isa<ZExtInst>(UInst) && (NoUnsignedWrap || NoSignedWrap)) {
Dale Johannesenb6f383e2009-04-15 01:10:12 +0000799 Value *TruncIndVar = getZeroExtendedTruncVar(AR, SE, LargestType, L,
Dale Johannesencb91b762009-04-15 20:41:02 +0000800 UInst->getType(), Rewriter, InsertPt);
801 UInst->replaceAllUsesWith(TruncIndVar);
802 DeadInsts.insert(UInst);
803 }
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000804 // If we have zext(i&constant), it's always safe to use the larger
805 // variable. This is not common but is a bottleneck in Openssl.
Dale Johannesencb91b762009-04-15 20:41:02 +0000806 // (RHS doesn't have to be constant. There should be a better approach
807 // than bottom-up pattern matching for this...)
808 if (UInst && UInst->getOpcode()==Instruction::And &&
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000809 allUsesAreSameTyped(Instruction::ZExt, UInst) &&
810 isa<ConstantInt>(UInst->getOperand(1))) {
Dale Johannesencb91b762009-04-15 20:41:02 +0000811 uint64_t newBitSize = LargestType->getPrimitiveSizeInBits();
812 ConstantInt* AndRHS = dyn_cast<ConstantInt>(UInst->getOperand(1));
813 ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst->use_begin());
814 Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
815 L, oldZext->getType(), Rewriter, InsertPt);
816 APInt APcopy = APInt(AndRHS->getValue());
817 ConstantInt* newAndRHS = ConstantInt::get(APcopy.zext(newBitSize));
818 Value *NewAnd =
819 BinaryOperator::CreateAnd(TruncIndVar, newAndRHS,
820 UInst->getName()+".nozex", UInst);
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000821 for (Value::use_iterator UI2 = UInst->use_begin(),
822 UE2 = UInst->use_end(); UI2 != UE2; ++UI2) {
823 Instruction *II = dyn_cast<Instruction>(UI2);
824 II->replaceAllUsesWith(NewAnd);
825 DeadInsts.insert(II);
826 }
Dale Johannesencb91b762009-04-15 20:41:02 +0000827 DeadInsts.insert(UInst);
828 }
829 // If we have zext((i+constant)&constant), we can use the larger
830 // variable even if the add does overflow. This works whenever the
831 // constant being ANDed is the same size as i, which it presumably is.
832 // We don't need to restrict the expression being and'ed to i+const,
833 // but we have to promote everything in it, so it's convenient.
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000834 // zext((i | constant)&constant) is also valid and accepted here.
835 if (UInst && (UInst->getOpcode()==Instruction::Add ||
836 UInst->getOpcode()==Instruction::Or) &&
Dale Johannesencb91b762009-04-15 20:41:02 +0000837 UInst->hasOneUse() &&
838 isa<ConstantInt>(UInst->getOperand(1))) {
839 uint64_t newBitSize = LargestType->getPrimitiveSizeInBits();
840 ConstantInt* AddRHS = dyn_cast<ConstantInt>(UInst->getOperand(1));
841 Instruction *UInst2 = dyn_cast<Instruction>(UInst->use_begin());
842 if (UInst2 && UInst2->getOpcode() == Instruction::And &&
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000843 allUsesAreSameTyped(Instruction::ZExt, UInst2) &&
844 isa<ConstantInt>(UInst2->getOperand(1))) {
Dale Johannesencb91b762009-04-15 20:41:02 +0000845 ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst2->use_begin());
846 Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType,
847 L, oldZext->getType(), Rewriter, InsertPt);
848 ConstantInt* AndRHS = dyn_cast<ConstantInt>(UInst2->getOperand(1));
849 APInt APcopy = APInt(AddRHS->getValue());
850 ConstantInt* newAddRHS = ConstantInt::get(APcopy.zext(newBitSize));
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000851 Value *NewAdd = ((UInst->getOpcode()==Instruction::Add) ?
Dale Johannesencb91b762009-04-15 20:41:02 +0000852 BinaryOperator::CreateAdd(TruncIndVar, newAddRHS,
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000853 UInst->getName()+".nozex", UInst2) :
854 BinaryOperator::CreateOr(TruncIndVar, newAddRHS,
855 UInst->getName()+".nozex", UInst2));
Dale Johannesencb91b762009-04-15 20:41:02 +0000856 APInt APcopy2 = APInt(AndRHS->getValue());
857 ConstantInt* newAndRHS = ConstantInt::get(APcopy2.zext(newBitSize));
858 Value *NewAnd =
859 BinaryOperator::CreateAnd(NewAdd, newAndRHS,
860 UInst->getName()+".nozex", UInst2);
Dale Johannesen3c25cb22009-04-15 23:31:51 +0000861 for (Value::use_iterator UI2 = UInst2->use_begin(),
862 UE2 = UInst2->use_end(); UI2 != UE2; ++UI2) {
863 Instruction *II = dyn_cast<Instruction>(UI2);
864 II->replaceAllUsesWith(NewAnd);
865 DeadInsts.insert(II);
866 }
Dale Johannesencb91b762009-04-15 20:41:02 +0000867 DeadInsts.insert(UInst);
868 DeadInsts.insert(UInst2);
869 }
Dan Gohmancecc80f2009-02-14 02:31:09 +0000870 }
871 }
Dan Gohmancacd2012009-02-12 22:19:27 +0000872
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 // Replace the old PHI Node with the inserted computation.
874 PN->replaceAllUsesWith(NewVal);
875 DeadInsts.insert(PN);
876 IndVars.pop_back();
877 ++NumRemoved;
878 Changed = true;
879 }
880
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 DeleteTriviallyDeadInstructions(DeadInsts);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 assert(L->isLCSSAForm());
883 return Changed;
884}
Devang Patelbda43802008-09-09 21:41:07 +0000885
Devang Patelb8ccf572008-11-18 00:40:02 +0000886/// Return true if it is OK to use SIToFPInst for an inducation variable
887/// with given inital and exit values.
888static bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV,
889 uint64_t intIV, uint64_t intEV) {
890
Dan Gohman963fc812009-02-17 19:13:57 +0000891 if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative())
Devang Patelb8ccf572008-11-18 00:40:02 +0000892 return true;
893
894 // If the iteration range can be handled by SIToFPInst then use it.
895 APInt Max = APInt::getSignedMaxValue(32);
Bill Wendlingb9a5a682008-11-18 10:57:27 +0000896 if (Max.getZExtValue() > static_cast<uint64_t>(abs(intEV - intIV)))
Devang Patelb8ccf572008-11-18 00:40:02 +0000897 return true;
Dan Gohman963fc812009-02-17 19:13:57 +0000898
Devang Patelb8ccf572008-11-18 00:40:02 +0000899 return false;
900}
901
902/// convertToInt - Convert APF to an integer, if possible.
Devang Patele2ba01d2008-11-17 23:27:13 +0000903static bool convertToInt(const APFloat &APF, uint64_t *intVal) {
904
905 bool isExact = false;
Evan Cheng30e65f62008-11-26 01:11:57 +0000906 if (&APF.getSemantics() == &APFloat::PPCDoubleDouble)
907 return false;
Dan Gohman963fc812009-02-17 19:13:57 +0000908 if (APF.convertToInteger(intVal, 32, APF.isNegative(),
Devang Patele2ba01d2008-11-17 23:27:13 +0000909 APFloat::rmTowardZero, &isExact)
910 != APFloat::opOK)
911 return false;
Dan Gohman963fc812009-02-17 19:13:57 +0000912 if (!isExact)
Devang Patele2ba01d2008-11-17 23:27:13 +0000913 return false;
914 return true;
915
916}
917
Devang Patel7ca23c92008-11-03 18:32:19 +0000918/// HandleFloatingPointIV - If the loop has floating induction variable
919/// then insert corresponding integer induction variable if possible.
Devang Patelc8dac622008-11-17 21:32:02 +0000920/// For example,
921/// for(double i = 0; i < 10000; ++i)
922/// bar(i)
923/// is converted into
924/// for(int i = 0; i < 10000; ++i)
925/// bar((double)i);
926///
Dan Gohman963fc812009-02-17 19:13:57 +0000927void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH,
Devang Patelc8dac622008-11-17 21:32:02 +0000928 SmallPtrSet<Instruction*, 16> &DeadInsts) {
Devang Patel7ca23c92008-11-03 18:32:19 +0000929
Devang Patelc8dac622008-11-17 21:32:02 +0000930 unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
931 unsigned BackEdge = IncomingEdge^1;
Dan Gohman963fc812009-02-17 19:13:57 +0000932
Devang Patelc8dac622008-11-17 21:32:02 +0000933 // Check incoming value.
Devang Patele2ba01d2008-11-17 23:27:13 +0000934 ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge));
935 if (!InitValue) return;
936 uint64_t newInitValue = Type::Int32Ty->getPrimitiveSizeInBits();
937 if (!convertToInt(InitValue->getValueAPF(), &newInitValue))
938 return;
939
940 // Check IV increment. Reject this PH if increement operation is not
941 // an add or increment value can not be represented by an integer.
Dan Gohman963fc812009-02-17 19:13:57 +0000942 BinaryOperator *Incr =
Devang Patelc8dac622008-11-17 21:32:02 +0000943 dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge));
944 if (!Incr) return;
945 if (Incr->getOpcode() != Instruction::Add) return;
946 ConstantFP *IncrValue = NULL;
947 unsigned IncrVIndex = 1;
948 if (Incr->getOperand(1) == PH)
949 IncrVIndex = 0;
950 IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex));
951 if (!IncrValue) return;
Devang Patele2ba01d2008-11-17 23:27:13 +0000952 uint64_t newIncrValue = Type::Int32Ty->getPrimitiveSizeInBits();
953 if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue))
954 return;
Dan Gohman963fc812009-02-17 19:13:57 +0000955
Devang Patele2ba01d2008-11-17 23:27:13 +0000956 // Check Incr uses. One user is PH and the other users is exit condition used
957 // by the conditional terminator.
Devang Patelc8dac622008-11-17 21:32:02 +0000958 Value::use_iterator IncrUse = Incr->use_begin();
959 Instruction *U1 = cast<Instruction>(IncrUse++);
960 if (IncrUse == Incr->use_end()) return;
961 Instruction *U2 = cast<Instruction>(IncrUse++);
962 if (IncrUse != Incr->use_end()) return;
Dan Gohman963fc812009-02-17 19:13:57 +0000963
Devang Patelc8dac622008-11-17 21:32:02 +0000964 // Find exit condition.
965 FCmpInst *EC = dyn_cast<FCmpInst>(U1);
966 if (!EC)
967 EC = dyn_cast<FCmpInst>(U2);
968 if (!EC) return;
969
970 if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) {
971 if (!BI->isConditional()) return;
972 if (BI->getCondition() != EC) return;
Devang Patel7ca23c92008-11-03 18:32:19 +0000973 }
Devang Patel7ca23c92008-11-03 18:32:19 +0000974
Devang Patele2ba01d2008-11-17 23:27:13 +0000975 // Find exit value. If exit value can not be represented as an interger then
976 // do not handle this floating point PH.
Devang Patelc8dac622008-11-17 21:32:02 +0000977 ConstantFP *EV = NULL;
978 unsigned EVIndex = 1;
979 if (EC->getOperand(1) == Incr)
980 EVIndex = 0;
981 EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex));
982 if (!EV) return;
Devang Patelc8dac622008-11-17 21:32:02 +0000983 uint64_t intEV = Type::Int32Ty->getPrimitiveSizeInBits();
Devang Patele2ba01d2008-11-17 23:27:13 +0000984 if (!convertToInt(EV->getValueAPF(), &intEV))
Devang Patelc8dac622008-11-17 21:32:02 +0000985 return;
Dan Gohman963fc812009-02-17 19:13:57 +0000986
Devang Patelc8dac622008-11-17 21:32:02 +0000987 // Find new predicate for integer comparison.
988 CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
989 switch (EC->getPredicate()) {
990 case CmpInst::FCMP_OEQ:
991 case CmpInst::FCMP_UEQ:
992 NewPred = CmpInst::ICMP_EQ;
993 break;
994 case CmpInst::FCMP_OGT:
995 case CmpInst::FCMP_UGT:
996 NewPred = CmpInst::ICMP_UGT;
997 break;
998 case CmpInst::FCMP_OGE:
999 case CmpInst::FCMP_UGE:
1000 NewPred = CmpInst::ICMP_UGE;
1001 break;
1002 case CmpInst::FCMP_OLT:
1003 case CmpInst::FCMP_ULT:
1004 NewPred = CmpInst::ICMP_ULT;
1005 break;
1006 case CmpInst::FCMP_OLE:
1007 case CmpInst::FCMP_ULE:
1008 NewPred = CmpInst::ICMP_ULE;
1009 break;
1010 default:
1011 break;
Devang Patel7ca23c92008-11-03 18:32:19 +00001012 }
Devang Patelc8dac622008-11-17 21:32:02 +00001013 if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return;
Dan Gohman963fc812009-02-17 19:13:57 +00001014
Devang Patelc8dac622008-11-17 21:32:02 +00001015 // Insert new integer induction variable.
1016 PHINode *NewPHI = PHINode::Create(Type::Int32Ty,
1017 PH->getName()+".int", PH);
Devang Patele2ba01d2008-11-17 23:27:13 +00001018 NewPHI->addIncoming(ConstantInt::get(Type::Int32Ty, newInitValue),
Devang Patelc8dac622008-11-17 21:32:02 +00001019 PH->getIncomingBlock(IncomingEdge));
1020
Dan Gohman963fc812009-02-17 19:13:57 +00001021 Value *NewAdd = BinaryOperator::CreateAdd(NewPHI,
1022 ConstantInt::get(Type::Int32Ty,
Devang Patele2ba01d2008-11-17 23:27:13 +00001023 newIncrValue),
Devang Patelc8dac622008-11-17 21:32:02 +00001024 Incr->getName()+".int", Incr);
1025 NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge));
1026
1027 ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV);
1028 Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(BackEdge) : NewEV);
1029 Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(BackEdge));
Dan Gohman963fc812009-02-17 19:13:57 +00001030 ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(),
Devang Patelc8dac622008-11-17 21:32:02 +00001031 EC->getParent()->getTerminator());
Dan Gohman963fc812009-02-17 19:13:57 +00001032
Devang Patelc8dac622008-11-17 21:32:02 +00001033 // Delete old, floating point, exit comparision instruction.
1034 EC->replaceAllUsesWith(NewEC);
1035 DeadInsts.insert(EC);
Dan Gohman963fc812009-02-17 19:13:57 +00001036
Devang Patelc8dac622008-11-17 21:32:02 +00001037 // Delete old, floating point, increment instruction.
1038 Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
1039 DeadInsts.insert(Incr);
Dan Gohman963fc812009-02-17 19:13:57 +00001040
Devang Patelb8ccf572008-11-18 00:40:02 +00001041 // Replace floating induction variable. Give SIToFPInst preference over
1042 // UIToFPInst because it is faster on platforms that are widely used.
1043 if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) {
Dan Gohman963fc812009-02-17 19:13:57 +00001044 SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv",
Devang Patele2ba01d2008-11-17 23:27:13 +00001045 PH->getParent()->getFirstNonPHI());
1046 PH->replaceAllUsesWith(Conv);
1047 } else {
Dan Gohman963fc812009-02-17 19:13:57 +00001048 UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv",
Devang Patele2ba01d2008-11-17 23:27:13 +00001049 PH->getParent()->getFirstNonPHI());
1050 PH->replaceAllUsesWith(Conv);
1051 }
Devang Patelc8dac622008-11-17 21:32:02 +00001052 DeadInsts.insert(PH);
Devang Patel7ca23c92008-11-03 18:32:19 +00001053}
1054