blob: 5f9d3703da99d610a73d568b5267b79c35e3d830 [file] [log] [blame]
Devang Patelfee76bd2007-08-07 00:25:56 +00001//===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patelfee76bd2007-08-07 00:25:56 +00007//
8//===----------------------------------------------------------------------===//
9//
Devang Patel38310052008-12-04 21:38:42 +000010// This file implements Loop Index Splitting Pass. This pass handles three
11// kinds of loops.
Devang Patelfee76bd2007-08-07 00:25:56 +000012//
Dan Gohmanf159ccd2009-04-29 22:01:05 +000013// [1] A loop may be eliminated if the body is executed exactly once.
14// For example,
15//
Devang Patel38310052008-12-04 21:38:42 +000016// for (i = 0; i < N; ++i) {
Dan Gohmanf159ccd2009-04-29 22:01:05 +000017// if (i == X) {
18// body;
19// }
20// }
21//
22// is transformed to
23//
24// i = X;
25// body;
26//
27// [2] A loop's iteration space may be shrunk if the loop body is executed
28// for a proper sub-range of the loop's iteration space. For example,
29//
30// for (i = 0; i < N; ++i) {
31// if (i > A && i < B) {
Devang Patel38310052008-12-04 21:38:42 +000032// ...
33// }
34// }
35//
Dan Gohmanf159ccd2009-04-29 22:01:05 +000036// is transformed to iterators from A to B, if A > 0 and B < N.
Devang Patel38310052008-12-04 21:38:42 +000037//
Dan Gohmanf159ccd2009-04-29 22:01:05 +000038// [3] A loop may be split if the loop body is dominated by a branch.
39// For example,
Devang Patel38310052008-12-04 21:38:42 +000040//
41// for (i = LB; i < UB; ++i) { if (i < SV) A; else B; }
42//
43// is transformed into
Dan Gohmanf159ccd2009-04-29 22:01:05 +000044//
Devang Patel38310052008-12-04 21:38:42 +000045// AEV = BSV = SV
46// for (i = LB; i < min(UB, AEV); ++i)
47// A;
48// for (i = max(LB, BSV); i < UB; ++i);
49// B;
Dan Gohmanf159ccd2009-04-29 22:01:05 +000050//
Devang Patelfee76bd2007-08-07 00:25:56 +000051//===----------------------------------------------------------------------===//
52
53#define DEBUG_TYPE "loop-index-split"
Devang Patelfee76bd2007-08-07 00:25:56 +000054#include "llvm/Transforms/Scalar.h"
Devang Pateld96c60d2009-02-06 06:19:06 +000055#include "llvm/IntrinsicInst.h"
Owen Anderson1ff50b32009-07-03 00:54:20 +000056#include "llvm/LLVMContext.h"
Devang Patelfee76bd2007-08-07 00:25:56 +000057#include "llvm/Analysis/LoopPass.h"
Dan Gohman97b6e2c2009-02-17 20:50:11 +000058#include "llvm/Analysis/ScalarEvolution.h"
Devang Patel787a7132007-08-08 21:39:47 +000059#include "llvm/Analysis/Dominators.h"
Devang Patel423c8b22007-08-10 18:07:13 +000060#include "llvm/Transforms/Utils/BasicBlockUtils.h"
61#include "llvm/Transforms/Utils/Cloning.h"
Devang Patelb23c2322009-03-30 22:24:10 +000062#include "llvm/Transforms/Utils/Local.h"
Devang Patel5b8ec612007-08-15 03:31:47 +000063#include "llvm/ADT/DepthFirstIterator.h"
Devang Patelfee76bd2007-08-07 00:25:56 +000064#include "llvm/ADT/Statistic.h"
65
66using namespace llvm;
67
Devang Patel38310052008-12-04 21:38:42 +000068STATISTIC(NumIndexSplit, "Number of loop index split");
69STATISTIC(NumIndexSplitRemoved, "Number of loops eliminated by loop index split");
70STATISTIC(NumRestrictBounds, "Number of loop iteration space restricted");
Devang Patelfee76bd2007-08-07 00:25:56 +000071
72namespace {
73
Chris Lattner3e8b6632009-09-02 06:11:42 +000074 class LoopIndexSplit : public LoopPass {
Devang Patelfee76bd2007-08-07 00:25:56 +000075 public:
76 static char ID; // Pass ID, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000077 LoopIndexSplit() : LoopPass(&ID) {}
Devang Patelfee76bd2007-08-07 00:25:56 +000078
79 // Index split Loop L. Return true if loop is split.
80 bool runOnLoop(Loop *L, LPPassManager &LPM);
81
82 void getAnalysisUsage(AnalysisUsage &AU) const {
Devang Patelfee76bd2007-08-07 00:25:56 +000083 AU.addPreserved<ScalarEvolution>();
84 AU.addRequiredID(LCSSAID);
85 AU.addPreservedID(LCSSAID);
Devang Patel423c8b22007-08-10 18:07:13 +000086 AU.addRequired<LoopInfo>();
Devang Patelfee76bd2007-08-07 00:25:56 +000087 AU.addPreserved<LoopInfo>();
88 AU.addRequiredID(LoopSimplifyID);
89 AU.addPreservedID(LoopSimplifyID);
Devang Patel9704fcf2007-08-08 22:25:28 +000090 AU.addRequired<DominatorTree>();
Devang Patel5b8ec612007-08-15 03:31:47 +000091 AU.addRequired<DominanceFrontier>();
Devang Patel787a7132007-08-08 21:39:47 +000092 AU.addPreserved<DominatorTree>();
93 AU.addPreserved<DominanceFrontier>();
Devang Patelfee76bd2007-08-07 00:25:56 +000094 }
95
96 private:
Devang Patel38310052008-12-04 21:38:42 +000097 /// processOneIterationLoop -- Eliminate loop if loop body is executed
98 /// only once. For example,
99 /// for (i = 0; i < N; ++i) {
100 /// if ( i == X) {
101 /// ...
102 /// }
103 /// }
104 ///
105 bool processOneIterationLoop();
Devang Patel71554b82007-08-08 21:02:17 +0000106
Devang Patel38310052008-12-04 21:38:42 +0000107 // -- Routines used by updateLoopIterationSpace();
Devang Patelc9d123d2007-08-09 01:39:01 +0000108
Devang Patel38310052008-12-04 21:38:42 +0000109 /// updateLoopIterationSpace -- Update loop's iteration space if loop
110 /// body is executed for certain IV range only. For example,
111 ///
112 /// for (i = 0; i < N; ++i) {
113 /// if ( i > A && i < B) {
114 /// ...
115 /// }
116 /// }
Devang Patel042b8772008-12-08 17:07:24 +0000117 /// is transformed to iterators from A to B, if A > 0 and B < N.
Devang Patel38310052008-12-04 21:38:42 +0000118 ///
119 bool updateLoopIterationSpace();
Devang Patel71554b82007-08-08 21:02:17 +0000120
Devang Patel38310052008-12-04 21:38:42 +0000121 /// restrictLoopBound - Op dominates loop body. Op compares an IV based value
122 /// with a loop invariant value. Update loop's lower and upper bound based on
123 /// the loop invariant value.
124 bool restrictLoopBound(ICmpInst &Op);
Devang Patel4a69da92007-08-25 00:56:38 +0000125
Devang Patel38310052008-12-04 21:38:42 +0000126 // --- Routines used by splitLoop(). --- /
Devang Patel4a69da92007-08-25 00:56:38 +0000127
Devang Patel38310052008-12-04 21:38:42 +0000128 bool splitLoop();
Devang Patel4a69da92007-08-25 00:56:38 +0000129
Devang Patel38310052008-12-04 21:38:42 +0000130 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by
131 /// DeadBB. This routine is used to remove split condition's dead branch,
132 /// dominated by DeadBB. LiveBB dominates split conidition's other branch.
Devang Patela6a86632007-08-14 18:35:57 +0000133 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB);
Devang Patel38310052008-12-04 21:38:42 +0000134
135 /// moveExitCondition - Move exit condition EC into split condition block.
136 void moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
137 BasicBlock *ExitBB, ICmpInst *EC, ICmpInst *SC,
138 PHINode *IV, Instruction *IVAdd, Loop *LP,
139 unsigned);
140
Devang Pateld79faee2007-08-25 02:39:24 +0000141 /// updatePHINodes - CFG has been changed.
142 /// Before
143 /// - ExitBB's single predecessor was Latch
144 /// - Latch's second successor was Header
145 /// Now
146 /// - ExitBB's single predecessor was Header
147 /// - Latch's one and only successor was Header
148 ///
149 /// Update ExitBB PHINodes' to reflect this change.
150 void updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
151 BasicBlock *Header,
Devang Patelea069062008-02-13 22:23:07 +0000152 PHINode *IV, Instruction *IVIncrement, Loop *LP);
Devang Pateld79faee2007-08-25 02:39:24 +0000153
Devang Patel38310052008-12-04 21:38:42 +0000154 // --- Utility routines --- /
Devang Pateld79faee2007-08-25 02:39:24 +0000155
Devang Patel38310052008-12-04 21:38:42 +0000156 /// cleanBlock - A block is considered clean if all non terminal
157 /// instructions are either PHINodes or IV based values.
158 bool cleanBlock(BasicBlock *BB);
Devang Patelfee76bd2007-08-07 00:25:56 +0000159
Devang Patel042b8772008-12-08 17:07:24 +0000160 /// IVisLT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000161 /// IV based value is less than the loop invariant then return the loop
162 /// invariant. Otherwise return NULL.
163 Value * IVisLT(ICmpInst &Op);
164
Devang Patel042b8772008-12-08 17:07:24 +0000165 /// IVisLE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000166 /// IV based value is less than or equal to the loop invariant then
167 /// return the loop invariant. Otherwise return NULL.
168 Value * IVisLE(ICmpInst &Op);
169
Devang Patel042b8772008-12-08 17:07:24 +0000170 /// IVisGT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000171 /// IV based value is greater than the loop invariant then return the loop
172 /// invariant. Otherwise return NULL.
173 Value * IVisGT(ICmpInst &Op);
174
Devang Patel042b8772008-12-08 17:07:24 +0000175 /// IVisGE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000176 /// IV based value is greater than or equal to the loop invariant then
177 /// return the loop invariant. Otherwise return NULL.
178 Value * IVisGE(ICmpInst &Op);
Devang Patelbacf5192007-08-10 00:33:50 +0000179
Devang Patelfee76bd2007-08-07 00:25:56 +0000180 private:
181
Devang Patel38310052008-12-04 21:38:42 +0000182 // Current Loop information.
Devang Patelfee76bd2007-08-07 00:25:56 +0000183 Loop *L;
Devang Patel423c8b22007-08-10 18:07:13 +0000184 LPPassManager *LPM;
185 LoopInfo *LI;
Devang Patel9704fcf2007-08-08 22:25:28 +0000186 DominatorTree *DT;
Devang Patelfc4c5f82007-08-13 22:13:24 +0000187 DominanceFrontier *DF;
Devang Patelbacf5192007-08-10 00:33:50 +0000188
Devang Patelbacf5192007-08-10 00:33:50 +0000189 PHINode *IndVar;
Devang Patelbacf5192007-08-10 00:33:50 +0000190 ICmpInst *ExitCondition;
Devang Patel38310052008-12-04 21:38:42 +0000191 ICmpInst *SplitCondition;
192 Value *IVStartValue;
193 Value *IVExitValue;
194 Instruction *IVIncrement;
195 SmallPtrSet<Value *, 4> IVBasedValues;
Devang Patelfee76bd2007-08-07 00:25:56 +0000196 };
Devang Patelfee76bd2007-08-07 00:25:56 +0000197}
198
Dan Gohman844731a2008-05-13 00:00:25 +0000199char LoopIndexSplit::ID = 0;
200static RegisterPass<LoopIndexSplit>
201X("loop-index-split", "Index Split Loops");
202
Daniel Dunbar394f0442008-10-22 23:32:42 +0000203Pass *llvm::createLoopIndexSplitPass() {
Devang Patelfee76bd2007-08-07 00:25:56 +0000204 return new LoopIndexSplit();
205}
206
207// Index split Loop L. Return true if loop is split.
Devang Patel423c8b22007-08-10 18:07:13 +0000208bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
Devang Patelfee76bd2007-08-07 00:25:56 +0000209 L = IncomingLoop;
Devang Patel423c8b22007-08-10 18:07:13 +0000210 LPM = &LPM_Ref;
Devang Patel71554b82007-08-08 21:02:17 +0000211
Devang Patel3fe4f212007-08-15 02:14:55 +0000212 // FIXME - Nested loops make dominator info updates tricky.
Devang Patel4e8061c2007-08-14 23:53:57 +0000213 if (!L->getSubLoops().empty())
214 return false;
215
Devang Patel9704fcf2007-08-08 22:25:28 +0000216 DT = &getAnalysis<DominatorTree>();
Devang Patel423c8b22007-08-10 18:07:13 +0000217 LI = &getAnalysis<LoopInfo>();
Devang Patel7375bb92007-08-15 03:34:53 +0000218 DF = &getAnalysis<DominanceFrontier>();
Devang Patelfee76bd2007-08-07 00:25:56 +0000219
Devang Patel38310052008-12-04 21:38:42 +0000220 // Initialize loop data.
221 IndVar = L->getCanonicalInductionVariable();
222 if (!IndVar) return false;
Devang Patelbacf5192007-08-10 00:33:50 +0000223
Devang Patel38310052008-12-04 21:38:42 +0000224 bool P1InLoop = L->contains(IndVar->getIncomingBlock(1));
225 IVStartValue = IndVar->getIncomingValue(!P1InLoop);
226 IVIncrement = dyn_cast<Instruction>(IndVar->getIncomingValue(P1InLoop));
227 if (!IVIncrement) return false;
Devang Patel71554b82007-08-08 21:02:17 +0000228
Devang Patel38310052008-12-04 21:38:42 +0000229 IVBasedValues.clear();
230 IVBasedValues.insert(IndVar);
231 IVBasedValues.insert(IVIncrement);
Devang Patelbacf5192007-08-10 00:33:50 +0000232 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
Devang Patel38310052008-12-04 21:38:42 +0000233 I != E; ++I)
234 for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end();
235 BI != BE; ++BI) {
236 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BI))
237 if (BO != IVIncrement
238 && (BO->getOpcode() == Instruction::Add
239 || BO->getOpcode() == Instruction::Sub))
240 if (IVBasedValues.count(BO->getOperand(0))
241 && L->isLoopInvariant(BO->getOperand(1)))
242 IVBasedValues.insert(BO);
243 }
Devang Patelbacf5192007-08-10 00:33:50 +0000244
Devang Patel38310052008-12-04 21:38:42 +0000245 // Reject loop if loop exit condition is not suitable.
Dan Gohmanc8332462009-02-12 18:08:24 +0000246 BasicBlock *ExitingBlock = L->getExitingBlock();
247 if (!ExitingBlock)
Devang Patel38310052008-12-04 21:38:42 +0000248 return false;
Dan Gohmanc8332462009-02-12 18:08:24 +0000249 BranchInst *EBR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Devang Patel38310052008-12-04 21:38:42 +0000250 if (!EBR) return false;
251 ExitCondition = dyn_cast<ICmpInst>(EBR->getCondition());
252 if (!ExitCondition) return false;
Dan Gohmanc8332462009-02-12 18:08:24 +0000253 if (ExitingBlock != L->getLoopLatch()) return false;
Devang Patel38310052008-12-04 21:38:42 +0000254 IVExitValue = ExitCondition->getOperand(1);
255 if (!L->isLoopInvariant(IVExitValue))
256 IVExitValue = ExitCondition->getOperand(0);
257 if (!L->isLoopInvariant(IVExitValue))
258 return false;
Dan Gohmanf7ca1612009-06-27 22:58:27 +0000259 if (!IVBasedValues.count(
260 ExitCondition->getOperand(IVExitValue == ExitCondition->getOperand(0))))
261 return false;
Devang Patela5e27f82008-07-09 00:12:01 +0000262
263 // If start value is more then exit value where induction variable
264 // increments by 1 then we are potentially dealing with an infinite loop.
265 // Do not index split this loop.
Devang Patel38310052008-12-04 21:38:42 +0000266 if (ConstantInt *SV = dyn_cast<ConstantInt>(IVStartValue))
267 if (ConstantInt *EV = dyn_cast<ConstantInt>(IVExitValue))
268 if (SV->getSExtValue() > EV->getSExtValue())
269 return false;
Devang Patelc9d123d2007-08-09 01:39:01 +0000270
Devang Patel38310052008-12-04 21:38:42 +0000271 if (processOneIterationLoop())
272 return true;
Devang Patelfee76bd2007-08-07 00:25:56 +0000273
Devang Patel38310052008-12-04 21:38:42 +0000274 if (updateLoopIterationSpace())
275 return true;
Devang Patelfee76bd2007-08-07 00:25:56 +0000276
Devang Patel38310052008-12-04 21:38:42 +0000277 if (splitLoop())
278 return true;
Devang Pateld35ed2c2007-09-11 00:42:56 +0000279
280 return false;
Devang Patelfee76bd2007-08-07 00:25:56 +0000281}
282
Devang Patel38310052008-12-04 21:38:42 +0000283// --- Helper routines ---
284// isUsedOutsideLoop - Returns true iff V is used outside the loop L.
285static bool isUsedOutsideLoop(Value *V, Loop *L) {
286 for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
287 if (!L->contains(cast<Instruction>(*UI)->getParent()))
288 return true;
289 return false;
290}
Devang Patelfee76bd2007-08-07 00:25:56 +0000291
Devang Patel38310052008-12-04 21:38:42 +0000292// Return V+1
Owen Anderson1ff50b32009-07-03 00:54:20 +0000293static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt,
Owen Andersone922c022009-07-22 00:24:57 +0000294 LLVMContext &Context) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000295 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
Devang Patel38310052008-12-04 21:38:42 +0000296 return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt);
297}
Devang Patelfee76bd2007-08-07 00:25:56 +0000298
Devang Patel38310052008-12-04 21:38:42 +0000299// Return V-1
Owen Anderson1ff50b32009-07-03 00:54:20 +0000300static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt,
Owen Andersone922c022009-07-22 00:24:57 +0000301 LLVMContext &Context) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000302 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
Devang Patel38310052008-12-04 21:38:42 +0000303 return BinaryOperator::CreateSub(V, One, "lsp", InsertPt);
304}
Devang Patelfee76bd2007-08-07 00:25:56 +0000305
Devang Patel38310052008-12-04 21:38:42 +0000306// Return min(V1, V1)
307static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
308
Owen Anderson333c4002009-07-09 23:48:35 +0000309 Value *C = new ICmpInst(InsertPt,
310 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
311 V1, V2, "lsp");
Devang Patel38310052008-12-04 21:38:42 +0000312 return SelectInst::Create(C, V1, V2, "lsp", InsertPt);
313}
Devang Patelfee76bd2007-08-07 00:25:56 +0000314
Devang Patel38310052008-12-04 21:38:42 +0000315// Return max(V1, V2)
316static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
317
Owen Anderson333c4002009-07-09 23:48:35 +0000318 Value *C = new ICmpInst(InsertPt,
319 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
320 V1, V2, "lsp");
Devang Patel38310052008-12-04 21:38:42 +0000321 return SelectInst::Create(C, V2, V1, "lsp", InsertPt);
322}
Devang Patel968eee22007-09-19 00:15:16 +0000323
Devang Patel38310052008-12-04 21:38:42 +0000324/// processOneIterationLoop -- Eliminate loop if loop body is executed
325/// only once. For example,
326/// for (i = 0; i < N; ++i) {
327/// if ( i == X) {
328/// ...
329/// }
330/// }
331///
332bool LoopIndexSplit::processOneIterationLoop() {
333 SplitCondition = NULL;
Devang Patel84ef08b2007-09-19 00:11:01 +0000334 BasicBlock *Latch = L->getLoopLatch();
Devang Patel38310052008-12-04 21:38:42 +0000335 BasicBlock *Header = L->getHeader();
336 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
337 if (!BR) return false;
338 if (!isa<BranchInst>(Latch->getTerminator())) return false;
339 if (BR->isUnconditional()) return false;
340 SplitCondition = dyn_cast<ICmpInst>(BR->getCondition());
341 if (!SplitCondition) return false;
342 if (SplitCondition == ExitCondition) return false;
343 if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false;
344 if (BR->getOperand(1) != Latch) return false;
345 if (!IVBasedValues.count(SplitCondition->getOperand(0))
346 && !IVBasedValues.count(SplitCondition->getOperand(1)))
Devang Patel84ef08b2007-09-19 00:11:01 +0000347 return false;
348
Devang Patel38310052008-12-04 21:38:42 +0000349 // If IV is used outside the loop then this loop traversal is required.
350 // FIXME: Calculate and use last IV value.
351 if (isUsedOutsideLoop(IVIncrement, L))
352 return false;
353
354 // If BR operands are not IV or not loop invariants then skip this loop.
355 Value *OPV = SplitCondition->getOperand(0);
356 Value *SplitValue = SplitCondition->getOperand(1);
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000357 if (!L->isLoopInvariant(SplitValue))
358 std::swap(OPV, SplitValue);
Devang Patel38310052008-12-04 21:38:42 +0000359 if (!L->isLoopInvariant(SplitValue))
360 return false;
361 Instruction *OPI = dyn_cast<Instruction>(OPV);
Devang Patelb23c2322009-03-30 22:24:10 +0000362 if (!OPI)
363 return false;
Devang Patel38310052008-12-04 21:38:42 +0000364 if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L))
365 return false;
Devang Patelb23c2322009-03-30 22:24:10 +0000366 Value *StartValue = IVStartValue;
367 Value *ExitValue = IVExitValue;;
368
369 if (OPV != IndVar) {
370 // If BR operand is IV based then use this operand to calculate
371 // effective conditions for loop body.
372 BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV);
373 if (!BOPV)
374 return false;
375 if (BOPV->getOpcode() != Instruction::Add)
376 return false;
377 StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR);
378 ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR);
379 }
380
Devang Patel38310052008-12-04 21:38:42 +0000381 if (!cleanBlock(Header))
382 return false;
383
384 if (!cleanBlock(Latch))
385 return false;
386
387 // If the merge point for BR is not loop latch then skip this loop.
388 if (BR->getSuccessor(0) != Latch) {
389 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
390 assert (DF0 != DF->end() && "Unable to find dominance frontier");
391 if (!DF0->second.count(Latch))
392 return false;
393 }
394
395 if (BR->getSuccessor(1) != Latch) {
396 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
397 assert (DF1 != DF->end() && "Unable to find dominance frontier");
398 if (!DF1->second.count(Latch))
399 return false;
400 }
401
402 // Now, Current loop L contains compare instruction
403 // that compares induction variable, IndVar, against loop invariant. And
404 // entire (i.e. meaningful) loop body is dominated by this compare
405 // instruction. In such case eliminate
406 // loop structure surrounding this loop body. For example,
407 // for (int i = start; i < end; ++i) {
408 // if ( i == somevalue) {
409 // loop_body
410 // }
411 // }
412 // can be transformed into
413 // if (somevalue >= start && somevalue < end) {
414 // i = somevalue;
415 // loop_body
416 // }
Devang Patel6a2bfda2007-08-08 01:51:27 +0000417
Devang Patelebc5fea2007-08-20 20:49:01 +0000418 // Replace index variable with split value in loop body. Loop body is executed
419 // only when index variable is equal to split value.
Devang Patel38310052008-12-04 21:38:42 +0000420 IndVar->replaceAllUsesWith(SplitValue);
Devang Patelfee76bd2007-08-07 00:25:56 +0000421
Devang Patelfee76bd2007-08-07 00:25:56 +0000422 // Replace split condition in header.
423 // Transform
424 // SplitCondition : icmp eq i32 IndVar, SplitValue
425 // into
426 // c1 = icmp uge i32 SplitValue, StartValue
Devang Patelba32a5f2007-09-10 23:57:58 +0000427 // c2 = icmp ult i32 SplitValue, ExitValue
Devang Patelfee76bd2007-08-07 00:25:56 +0000428 // and i32 c1, c2
Owen Anderson333c4002009-07-09 23:48:35 +0000429 Instruction *C1 = new ICmpInst(BR, ExitCondition->isSignedPredicate() ?
Devang Patelfee76bd2007-08-07 00:25:56 +0000430 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
Owen Anderson333c4002009-07-09 23:48:35 +0000431 SplitValue, StartValue, "lisplit");
Devang Patel38310052008-12-04 21:38:42 +0000432
433 CmpInst::Predicate C2P = ExitCondition->getPredicate();
434 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
Chris Lattnerde648942009-08-28 00:43:14 +0000435 if (LatchBR->getOperand(1) != Header)
Devang Patel38310052008-12-04 21:38:42 +0000436 C2P = CmpInst::getInversePredicate(C2P);
Owen Anderson333c4002009-07-09 23:48:35 +0000437 Instruction *C2 = new ICmpInst(BR, C2P, SplitValue, ExitValue, "lisplit");
Devang Patel38310052008-12-04 21:38:42 +0000438 Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR);
439
440 SplitCondition->replaceAllUsesWith(NSplitCond);
441 SplitCondition->eraseFromParent();
Devang Patelfee76bd2007-08-07 00:25:56 +0000442
Devang Patelfc19fbd2008-10-10 22:02:57 +0000443 // Remove Latch to Header edge.
444 BasicBlock *LatchSucc = NULL;
445 Header->removePredecessor(Latch);
446 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
447 SI != E; ++SI) {
448 if (Header != *SI)
449 LatchSucc = *SI;
450 }
Devang Patelfc19fbd2008-10-10 22:02:57 +0000451
Devang Patelb23c2322009-03-30 22:24:10 +0000452 // Clean up latch block.
453 Value *LatchBRCond = LatchBR->getCondition();
454 LatchBR->setUnconditionalDest(LatchSucc);
455 RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond);
Devang Patel38310052008-12-04 21:38:42 +0000456
Devang Patel423c8b22007-08-10 18:07:13 +0000457 LPM->deleteLoopFromQueue(L);
Devang Patel787a7132007-08-08 21:39:47 +0000458
459 // Update Dominator Info.
460 // Only CFG change done is to remove Latch to Header edge. This
461 // does not change dominator tree because Latch did not dominate
462 // Header.
Devang Patelfc4c5f82007-08-13 22:13:24 +0000463 if (DF) {
Devang Patel787a7132007-08-08 21:39:47 +0000464 DominanceFrontier::iterator HeaderDF = DF->find(Header);
465 if (HeaderDF != DF->end())
466 DF->removeFromFrontier(HeaderDF, Header);
467
468 DominanceFrontier::iterator LatchDF = DF->find(Latch);
469 if (LatchDF != DF->end())
470 DF->removeFromFrontier(LatchDF, Header);
471 }
Devang Patel38310052008-12-04 21:38:42 +0000472
473 ++NumIndexSplitRemoved;
Devang Patelfee76bd2007-08-07 00:25:56 +0000474 return true;
475}
476
Devang Patel38310052008-12-04 21:38:42 +0000477/// restrictLoopBound - Op dominates loop body. Op compares an IV based value
478/// with a loop invariant value. Update loop's lower and upper bound based on
479/// the loop invariant value.
480bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) {
481 bool Sign = Op.isSignedPredicate();
482 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
Devang Patelfee76bd2007-08-07 00:25:56 +0000483
Devang Patel38310052008-12-04 21:38:42 +0000484 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
485 BranchInst *EBR =
486 cast<BranchInst>(ExitCondition->getParent()->getTerminator());
487 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
488 BasicBlock *T = EBR->getSuccessor(0);
489 EBR->setSuccessor(0, EBR->getSuccessor(1));
490 EBR->setSuccessor(1, T);
Devang Patelfee76bd2007-08-07 00:25:56 +0000491 }
492
Owen Andersone922c022009-07-22 00:24:57 +0000493 LLVMContext &Context = Op.getContext();
494
Devang Patel38310052008-12-04 21:38:42 +0000495 // New upper and lower bounds.
Devang Patel453a8442007-09-25 17:31:19 +0000496 Value *NLB = NULL;
Devang Patel38310052008-12-04 21:38:42 +0000497 Value *NUB = NULL;
498 if (Value *V = IVisLT(Op)) {
499 // Restrict upper bound.
500 if (IVisLE(*ExitCondition))
Owen Anderson1ff50b32009-07-03 00:54:20 +0000501 V = getMinusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000502 NUB = getMin(V, IVExitValue, Sign, PHTerm);
503 } else if (Value *V = IVisLE(Op)) {
504 // Restrict upper bound.
505 if (IVisLT(*ExitCondition))
Owen Anderson1ff50b32009-07-03 00:54:20 +0000506 V = getPlusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000507 NUB = getMin(V, IVExitValue, Sign, PHTerm);
508 } else if (Value *V = IVisGT(Op)) {
509 // Restrict lower bound.
Owen Anderson1ff50b32009-07-03 00:54:20 +0000510 V = getPlusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000511 NLB = getMax(V, IVStartValue, Sign, PHTerm);
512 } else if (Value *V = IVisGE(Op))
513 // Restrict lower bound.
514 NLB = getMax(V, IVStartValue, Sign, PHTerm);
Devang Patel453a8442007-09-25 17:31:19 +0000515
Devang Patel38310052008-12-04 21:38:42 +0000516 if (!NLB && !NUB)
517 return false;
Devang Patel453a8442007-09-25 17:31:19 +0000518
519 if (NLB) {
Devang Patel38310052008-12-04 21:38:42 +0000520 unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader());
Devang Patel453a8442007-09-25 17:31:19 +0000521 IndVar->setIncomingValue(i, NLB);
522 }
523
524 if (NUB) {
Devang Patel38310052008-12-04 21:38:42 +0000525 unsigned i = (ExitCondition->getOperand(0) != IVExitValue);
526 ExitCondition->setOperand(i, NUB);
Devang Patel453a8442007-09-25 17:31:19 +0000527 }
Devang Patel38310052008-12-04 21:38:42 +0000528 return true;
Devang Patel5279d062007-09-17 20:39:48 +0000529}
Devang Patel38310052008-12-04 21:38:42 +0000530
531/// updateLoopIterationSpace -- Update loop's iteration space if loop
532/// body is executed for certain IV range only. For example,
533///
534/// for (i = 0; i < N; ++i) {
535/// if ( i > A && i < B) {
536/// ...
537/// }
538/// }
Devang Patel042b8772008-12-08 17:07:24 +0000539/// is transformed to iterators from A to B, if A > 0 and B < N.
Devang Patel38310052008-12-04 21:38:42 +0000540///
541bool LoopIndexSplit::updateLoopIterationSpace() {
542 SplitCondition = NULL;
543 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
544 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
545 return false;
546 BasicBlock *Latch = L->getLoopLatch();
Devang Patel5279d062007-09-17 20:39:48 +0000547 BasicBlock *Header = L->getHeader();
Devang Patel38310052008-12-04 21:38:42 +0000548 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
549 if (!BR) return false;
550 if (!isa<BranchInst>(Latch->getTerminator())) return false;
551 if (BR->isUnconditional()) return false;
552 BinaryOperator *AND = dyn_cast<BinaryOperator>(BR->getCondition());
553 if (!AND) return false;
554 if (AND->getOpcode() != Instruction::And) return false;
555 ICmpInst *Op0 = dyn_cast<ICmpInst>(AND->getOperand(0));
556 ICmpInst *Op1 = dyn_cast<ICmpInst>(AND->getOperand(1));
557 if (!Op0 || !Op1)
558 return false;
559 IVBasedValues.insert(AND);
560 IVBasedValues.insert(Op0);
561 IVBasedValues.insert(Op1);
562 if (!cleanBlock(Header)) return false;
Devang Patel453a8442007-09-25 17:31:19 +0000563 BasicBlock *ExitingBlock = ExitCondition->getParent();
Devang Patel38310052008-12-04 21:38:42 +0000564 if (!cleanBlock(ExitingBlock)) return false;
Devang Patel453a8442007-09-25 17:31:19 +0000565
Devang Patelcf42ee42009-03-02 23:39:14 +0000566 // If the merge point for BR is not loop latch then skip this loop.
567 if (BR->getSuccessor(0) != Latch) {
568 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
569 assert (DF0 != DF->end() && "Unable to find dominance frontier");
570 if (!DF0->second.count(Latch))
571 return false;
572 }
573
574 if (BR->getSuccessor(1) != Latch) {
575 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
576 assert (DF1 != DF->end() && "Unable to find dominance frontier");
577 if (!DF1->second.count(Latch))
578 return false;
579 }
580
Devang Patel38310052008-12-04 21:38:42 +0000581 // Verify that loop exiting block has only two predecessor, where one pred
Devang Patel453a8442007-09-25 17:31:19 +0000582 // is split condition block. The other predecessor will become exiting block's
583 // dominator after CFG is updated. TODO : Handle CFG's where exiting block has
584 // more then two predecessors. This requires extra work in updating dominator
585 // information.
586 BasicBlock *ExitingBBPred = NULL;
587 for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock);
588 PI != PE; ++PI) {
589 BasicBlock *BB = *PI;
Devang Patel38310052008-12-04 21:38:42 +0000590 if (Header == BB)
Devang Patel453a8442007-09-25 17:31:19 +0000591 continue;
592 if (ExitingBBPred)
593 return false;
594 else
595 ExitingBBPred = BB;
596 }
Devang Patel453a8442007-09-25 17:31:19 +0000597
Devang Patel38310052008-12-04 21:38:42 +0000598 if (!restrictLoopBound(*Op0))
599 return false;
Devang Patel453a8442007-09-25 17:31:19 +0000600
Devang Patel38310052008-12-04 21:38:42 +0000601 if (!restrictLoopBound(*Op1))
602 return false;
603
604 // Update CFG.
605 if (BR->getSuccessor(0) == ExitingBlock)
606 BR->setUnconditionalDest(BR->getSuccessor(1));
Devang Patel453a8442007-09-25 17:31:19 +0000607 else
Devang Patel38310052008-12-04 21:38:42 +0000608 BR->setUnconditionalDest(BR->getSuccessor(0));
Devang Patel453a8442007-09-25 17:31:19 +0000609
Devang Patel38310052008-12-04 21:38:42 +0000610 AND->eraseFromParent();
Dan Gohmana8c763b2008-08-14 18:13:49 +0000611 if (Op0->use_empty())
Devang Patel453a8442007-09-25 17:31:19 +0000612 Op0->eraseFromParent();
Dan Gohmana8c763b2008-08-14 18:13:49 +0000613 if (Op1->use_empty())
Devang Patel453a8442007-09-25 17:31:19 +0000614 Op1->eraseFromParent();
Devang Patel453a8442007-09-25 17:31:19 +0000615
616 // Update domiantor info. Now, ExitingBlock has only one predecessor,
617 // ExitingBBPred, and it is ExitingBlock's immediate domiantor.
618 DT->changeImmediateDominator(ExitingBlock, ExitingBBPred);
Devang Patel38310052008-12-04 21:38:42 +0000619
620 BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1);
621 if (L->contains(ExitBlock))
622 ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0);
623
624 // If ExitingBlock is a member of the loop basic blocks' DF list then
625 // replace ExitingBlock with header and exit block in the DF list
626 DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock);
Devang Patel453a8442007-09-25 17:31:19 +0000627 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
628 I != E; ++I) {
629 BasicBlock *BB = *I;
630 if (BB == Header || BB == ExitingBlock)
631 continue;
632 DominanceFrontier::iterator BBDF = DF->find(BB);
633 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
634 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
635 while (DomSetI != DomSetE) {
636 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
637 ++DomSetI;
638 BasicBlock *DFBB = *CurrentItr;
639 if (DFBB == ExitingBlock) {
640 BBDF->second.erase(DFBB);
Devang Patel38310052008-12-04 21:38:42 +0000641 for (DominanceFrontier::DomSetType::iterator
642 EBI = ExitingBlockDF->second.begin(),
643 EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI)
644 BBDF->second.insert(*EBI);
Devang Patel453a8442007-09-25 17:31:19 +0000645 }
646 }
647 }
Devang Patel38310052008-12-04 21:38:42 +0000648 NumRestrictBounds++;
Devang Patel1c013502007-09-25 17:43:08 +0000649 return true;
Devang Patel5279d062007-09-17 20:39:48 +0000650}
651
Devang Patela6a86632007-08-14 18:35:57 +0000652/// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
653/// This routine is used to remove split condition's dead branch, dominated by
654/// DeadBB. LiveBB dominates split conidition's other branch.
655void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
656 BasicBlock *LiveBB) {
Devang Patel98147a32007-08-12 07:02:51 +0000657
Devang Patel5b8ec612007-08-15 03:31:47 +0000658 // First update DeadBB's dominance frontier.
Devang Patel96bf5242007-08-17 21:59:16 +0000659 SmallVector<BasicBlock *, 8> FrontierBBs;
Devang Patel5b8ec612007-08-15 03:31:47 +0000660 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
661 if (DeadBBDF != DF->end()) {
662 SmallVector<BasicBlock *, 8> PredBlocks;
663
664 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
665 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
Devang Patel38310052008-12-04 21:38:42 +0000666 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI)
667 {
Devang Patel5b8ec612007-08-15 03:31:47 +0000668 BasicBlock *FrontierBB = *DeadBBSetI;
Devang Patel96bf5242007-08-17 21:59:16 +0000669 FrontierBBs.push_back(FrontierBB);
670
Devang Patel5b8ec612007-08-15 03:31:47 +0000671 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
672 PredBlocks.clear();
673 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
674 PI != PE; ++PI) {
675 BasicBlock *P = *PI;
676 if (P == DeadBB || DT->dominates(DeadBB, P))
677 PredBlocks.push_back(P);
Devang Patelfc4c5f82007-08-13 22:13:24 +0000678 }
Devang Patel96bf5242007-08-17 21:59:16 +0000679
Devang Patel5b8ec612007-08-15 03:31:47 +0000680 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
681 FBI != FBE; ++FBI) {
682 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
683 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
684 PE = PredBlocks.end(); PI != PE; ++PI) {
685 BasicBlock *P = *PI;
686 PN->removeIncomingValue(P);
687 }
688 }
689 else
690 break;
Devang Patel96bf5242007-08-17 21:59:16 +0000691 }
Devang Patel98147a32007-08-12 07:02:51 +0000692 }
Devang Patel98147a32007-08-12 07:02:51 +0000693 }
Devang Patel5b8ec612007-08-15 03:31:47 +0000694
695 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
696 SmallVector<BasicBlock *, 32> WorkList;
697 DomTreeNode *DN = DT->getNode(DeadBB);
698 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
699 E = df_end(DN); DI != E; ++DI) {
700 BasicBlock *BB = DI->getBlock();
701 WorkList.push_back(BB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000702 BB->replaceAllUsesWith(UndefValue::get(
703 Type::getLabelTy(DeadBB->getContext())));
Devang Patel5b8ec612007-08-15 03:31:47 +0000704 }
705
706 while (!WorkList.empty()) {
707 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
Devang Patel575ec802009-03-25 23:57:48 +0000708 LPM->deleteSimpleAnalysisValue(BB, LP);
Devang Patel5b8ec612007-08-15 03:31:47 +0000709 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
Devang Pateld15dd8c2007-09-20 23:01:50 +0000710 BBI != BBE; ) {
Devang Patel5b8ec612007-08-15 03:31:47 +0000711 Instruction *I = BBI;
Devang Pateld15dd8c2007-09-20 23:01:50 +0000712 ++BBI;
Devang Patel5b8ec612007-08-15 03:31:47 +0000713 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Owen Anderson0b2a1532009-04-14 01:04:19 +0000714 LPM->deleteSimpleAnalysisValue(I, LP);
Devang Patel5b8ec612007-08-15 03:31:47 +0000715 I->eraseFromParent();
716 }
Devang Patel5b8ec612007-08-15 03:31:47 +0000717 DT->eraseNode(BB);
718 DF->removeBlock(BB);
719 LI->removeBlock(BB);
720 BB->eraseFromParent();
721 }
Devang Patel96bf5242007-08-17 21:59:16 +0000722
723 // Update Frontier BBs' dominator info.
724 while (!FrontierBBs.empty()) {
725 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
726 BasicBlock *NewDominator = FBB->getSinglePredecessor();
727 if (!NewDominator) {
728 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
729 NewDominator = *PI;
730 ++PI;
731 if (NewDominator != LiveBB) {
732 for(; PI != PE; ++PI) {
733 BasicBlock *P = *PI;
734 if (P == LiveBB) {
735 NewDominator = LiveBB;
736 break;
737 }
738 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
739 }
740 }
741 }
742 assert (NewDominator && "Unable to fix dominator info.");
743 DT->changeImmediateDominator(FBB, NewDominator);
744 DF->changeImmediateDominator(FBB, NewDominator, DT);
745 }
746
Devang Patel98147a32007-08-12 07:02:51 +0000747}
748
Devang Pateld79faee2007-08-25 02:39:24 +0000749// moveExitCondition - Move exit condition EC into split condition block CondBB.
750void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
Devang Patel38310052008-12-04 21:38:42 +0000751 BasicBlock *ExitBB, ICmpInst *EC,
752 ICmpInst *SC, PHINode *IV,
753 Instruction *IVAdd, Loop *LP,
754 unsigned ExitValueNum) {
Devang Pateld79faee2007-08-25 02:39:24 +0000755
756 BasicBlock *ExitingBB = EC->getParent();
757 Instruction *CurrentBR = CondBB->getTerminator();
758
759 // Move exit condition into split condition block.
760 EC->moveBefore(CurrentBR);
761 EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV);
762
763 // Move exiting block's branch into split condition block. Update its branch
764 // destination.
765 BranchInst *ExitingBR = cast<BranchInst>(ExitingBB->getTerminator());
766 ExitingBR->moveBefore(CurrentBR);
Devang Patel23067df2008-02-13 22:06:36 +0000767 BasicBlock *OrigDestBB = NULL;
768 if (ExitingBR->getSuccessor(0) == ExitBB) {
769 OrigDestBB = ExitingBR->getSuccessor(1);
Devang Pateld79faee2007-08-25 02:39:24 +0000770 ExitingBR->setSuccessor(1, ActiveBB);
Devang Patel23067df2008-02-13 22:06:36 +0000771 }
772 else {
773 OrigDestBB = ExitingBR->getSuccessor(0);
Devang Pateld79faee2007-08-25 02:39:24 +0000774 ExitingBR->setSuccessor(0, ActiveBB);
Devang Patel23067df2008-02-13 22:06:36 +0000775 }
Devang Pateld79faee2007-08-25 02:39:24 +0000776
777 // Remove split condition and current split condition branch.
778 SC->eraseFromParent();
779 CurrentBR->eraseFromParent();
780
Devang Patel23067df2008-02-13 22:06:36 +0000781 // Connect exiting block to original destination.
Gabor Greif051a9502008-04-06 20:25:17 +0000782 BranchInst::Create(OrigDestBB, ExitingBB);
Devang Pateld79faee2007-08-25 02:39:24 +0000783
784 // Update PHINodes
Devang Patelea069062008-02-13 22:23:07 +0000785 updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP);
Devang Pateld79faee2007-08-25 02:39:24 +0000786
787 // Fix dominator info.
788 // ExitBB is now dominated by CondBB
789 DT->changeImmediateDominator(ExitBB, CondBB);
790 DF->changeImmediateDominator(ExitBB, CondBB, DT);
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000791
792 // Blocks outside the loop may have been in the dominance frontier of blocks
793 // inside the condition; this is now impossible because the blocks inside the
794 // condition no loger dominate the exit. Remove the relevant blocks from
795 // the dominance frontiers.
796 for (Loop::block_iterator I = LP->block_begin(), E = LP->block_end();
797 I != E; ++I) {
798 if (*I == CondBB || !DT->dominates(CondBB, *I)) continue;
799 DominanceFrontier::iterator BBDF = DF->find(*I);
Devang Pateld79faee2007-08-25 02:39:24 +0000800 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
801 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
802 while (DomSetI != DomSetE) {
803 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
804 ++DomSetI;
805 BasicBlock *DFBB = *CurrentItr;
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000806 if (!LP->contains(DFBB))
Devang Pateld79faee2007-08-25 02:39:24 +0000807 BBDF->second.erase(DFBB);
Devang Pateld79faee2007-08-25 02:39:24 +0000808 }
809 }
810}
811
812/// updatePHINodes - CFG has been changed.
813/// Before
814/// - ExitBB's single predecessor was Latch
815/// - Latch's second successor was Header
816/// Now
Devang Patel82ada542008-02-08 22:49:13 +0000817/// - ExitBB's single predecessor is Header
818/// - Latch's one and only successor is Header
Devang Pateld79faee2007-08-25 02:39:24 +0000819///
820/// Update ExitBB PHINodes' to reflect this change.
821void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
822 BasicBlock *Header,
Devang Patelea069062008-02-13 22:23:07 +0000823 PHINode *IV, Instruction *IVIncrement,
824 Loop *LP) {
Devang Pateld79faee2007-08-25 02:39:24 +0000825
826 for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end();
Devang Patel4a3c0ac2008-03-27 17:32:46 +0000827 BI != BE; ) {
Devang Pateld79faee2007-08-25 02:39:24 +0000828 PHINode *PN = dyn_cast<PHINode>(BI);
Devang Patel4a3c0ac2008-03-27 17:32:46 +0000829 ++BI;
Devang Pateld79faee2007-08-25 02:39:24 +0000830 if (!PN)
831 break;
832
833 Value *V = PN->getIncomingValueForBlock(Latch);
834 if (PHINode *PHV = dyn_cast<PHINode>(V)) {
Devang Patel82ada542008-02-08 22:49:13 +0000835 // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use
836 // in Header which is new incoming value for PN.
Devang Pateld79faee2007-08-25 02:39:24 +0000837 Value *NewV = NULL;
838 for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end();
Devang Patel82ada542008-02-08 22:49:13 +0000839 UI != E; ++UI)
840 if (PHINode *U = dyn_cast<PHINode>(*UI))
Devang Patelea069062008-02-13 22:23:07 +0000841 if (LP->contains(U->getParent())) {
Devang Patel82ada542008-02-08 22:49:13 +0000842 NewV = U;
843 break;
844 }
845
Devang Patel60a12902008-03-24 20:16:14 +0000846 // Add incoming value from header only if PN has any use inside the loop.
847 if (NewV)
848 PN->addIncoming(NewV, Header);
Devang Pateld79faee2007-08-25 02:39:24 +0000849
850 } else if (Instruction *PHI = dyn_cast<Instruction>(V)) {
851 // If this instruction is IVIncrement then IV is new incoming value
852 // from header otherwise this instruction must be incoming value from
853 // header because loop is in LCSSA form.
854 if (PHI == IVIncrement)
855 PN->addIncoming(IV, Header);
856 else
857 PN->addIncoming(V, Header);
858 } else
859 // Otherwise this is an incoming value from header because loop is in
860 // LCSSA form.
861 PN->addIncoming(V, Header);
862
863 // Remove incoming value from Latch.
864 PN->removeIncomingValue(Latch);
865 }
866}
Devang Patel38310052008-12-04 21:38:42 +0000867
868bool LoopIndexSplit::splitLoop() {
869 SplitCondition = NULL;
870 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
871 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
872 return false;
873 BasicBlock *Header = L->getHeader();
874 BasicBlock *Latch = L->getLoopLatch();
875 BranchInst *SBR = NULL; // Split Condition Branch
876 BranchInst *EBR = cast<BranchInst>(ExitCondition->getParent()->getTerminator());
877 // If Exiting block includes loop variant instructions then this
878 // loop may not be split safely.
879 BasicBlock *ExitingBlock = ExitCondition->getParent();
880 if (!cleanBlock(ExitingBlock)) return false;
881
Owen Andersone922c022009-07-22 00:24:57 +0000882 LLVMContext &Context = Header->getContext();
883
Devang Patel38310052008-12-04 21:38:42 +0000884 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
885 I != E; ++I) {
886 BranchInst *BR = dyn_cast<BranchInst>((*I)->getTerminator());
887 if (!BR || BR->isUnconditional()) continue;
888 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
889 if (!CI || CI == ExitCondition
890 || CI->getPredicate() == ICmpInst::ICMP_NE
891 || CI->getPredicate() == ICmpInst::ICMP_EQ)
892 continue;
893
894 // Unable to handle triangle loops at the moment.
895 // In triangle loop, split condition is in header and one of the
896 // the split destination is loop latch. If split condition is EQ
897 // then such loops are already handle in processOneIterationLoop().
898 if (Header == (*I)
899 && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1)))
900 continue;
901
902 // If the block does not dominate the latch then this is not a diamond.
903 // Such loop may not benefit from index split.
904 if (!DT->dominates((*I), Latch))
905 continue;
906
907 // If split condition branches heads do not have single predecessor,
908 // SplitCondBlock, then is not possible to remove inactive branch.
909 if (!BR->getSuccessor(0)->getSinglePredecessor()
910 || !BR->getSuccessor(1)->getSinglePredecessor())
911 return false;
912
913 // If the merge point for BR is not loop latch then skip this condition.
914 if (BR->getSuccessor(0) != Latch) {
915 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
916 assert (DF0 != DF->end() && "Unable to find dominance frontier");
917 if (!DF0->second.count(Latch))
918 continue;
919 }
920
921 if (BR->getSuccessor(1) != Latch) {
922 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
923 assert (DF1 != DF->end() && "Unable to find dominance frontier");
924 if (!DF1->second.count(Latch))
925 continue;
926 }
927 SplitCondition = CI;
928 SBR = BR;
929 break;
930 }
931
932 if (!SplitCondition)
933 return false;
934
935 // If the predicate sign does not match then skip.
936 if (ExitCondition->isSignedPredicate() != SplitCondition->isSignedPredicate())
937 return false;
938
939 unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue);
940 unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0));
941 Value *SplitValue = SplitCondition->getOperand(SVOpNum);
942 if (!L->isLoopInvariant(SplitValue))
943 return false;
944 if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum)))
945 return false;
946
947 // Normalize loop conditions so that it is easier to calculate new loop
948 // bounds.
949 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
950 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
951 BasicBlock *T = EBR->getSuccessor(0);
952 EBR->setSuccessor(0, EBR->getSuccessor(1));
953 EBR->setSuccessor(1, T);
954 }
955
956 if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) {
957 SplitCondition->setPredicate(SplitCondition->getInversePredicate());
958 BasicBlock *T = SBR->getSuccessor(0);
959 SBR->setSuccessor(0, SBR->getSuccessor(1));
960 SBR->setSuccessor(1, T);
961 }
962
963 //[*] Calculate new loop bounds.
964 Value *AEV = SplitValue;
965 Value *BSV = SplitValue;
966 bool Sign = SplitCondition->isSignedPredicate();
967 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
968
969 if (IVisLT(*ExitCondition)) {
970 if (IVisLT(*SplitCondition)) {
971 /* Do nothing */
972 }
973 else if (IVisLE(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000974 AEV = getPlusOne(SplitValue, Sign, PHTerm, Context);
975 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000976 } else {
977 assert (0 && "Unexpected split condition!");
978 }
979 }
980 else if (IVisLE(*ExitCondition)) {
981 if (IVisLT(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000982 AEV = getMinusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000983 }
984 else if (IVisLE(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000985 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000986 } else {
987 assert (0 && "Unexpected split condition!");
988 }
989 } else {
990 assert (0 && "Unexpected exit condition!");
991 }
992 AEV = getMin(AEV, IVExitValue, Sign, PHTerm);
993 BSV = getMax(BSV, IVStartValue, Sign, PHTerm);
994
995 // [*] Clone Loop
996 DenseMap<const Value *, Value *> ValueMap;
997 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this);
998 Loop *ALoop = L;
999
1000 // [*] ALoop's exiting edge enters BLoop's header.
1001 // ALoop's original exit block becomes BLoop's exit block.
1002 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]);
1003 BasicBlock *A_ExitingBlock = ExitCondition->getParent();
1004 BranchInst *A_ExitInsn =
1005 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator());
1006 assert (A_ExitInsn && "Unable to find suitable loop exit branch");
1007 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1);
1008 BasicBlock *B_Header = BLoop->getHeader();
1009 if (ALoop->contains(B_ExitBlock)) {
1010 B_ExitBlock = A_ExitInsn->getSuccessor(0);
1011 A_ExitInsn->setSuccessor(0, B_Header);
1012 } else
1013 A_ExitInsn->setSuccessor(1, B_Header);
1014
1015 // [*] Update ALoop's exit value using new exit value.
1016 ExitCondition->setOperand(EVOpNum, AEV);
1017
1018 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from
1019 // original loop's preheader. Add incoming PHINode values from
1020 // ALoop's exiting block. Update BLoop header's domiantor info.
1021
1022 // Collect inverse map of Header PHINodes.
1023 DenseMap<Value *, Value *> InverseMap;
1024 for (BasicBlock::iterator BI = ALoop->getHeader()->begin(),
1025 BE = ALoop->getHeader()->end(); BI != BE; ++BI) {
1026 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1027 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
1028 InverseMap[PNClone] = PN;
1029 } else
1030 break;
1031 }
1032
1033 BasicBlock *A_Preheader = ALoop->getLoopPreheader();
1034 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1035 BI != BE; ++BI) {
1036 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1037 // Remove incoming value from original preheader.
1038 PN->removeIncomingValue(A_Preheader);
1039
1040 // Add incoming value from A_ExitingBlock.
1041 if (PN == B_IndVar)
1042 PN->addIncoming(BSV, A_ExitingBlock);
1043 else {
1044 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
1045 Value *V2 = NULL;
1046 // If loop header is also loop exiting block then
1047 // OrigPN is incoming value for B loop header.
1048 if (A_ExitingBlock == ALoop->getHeader())
1049 V2 = OrigPN;
1050 else
1051 V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
1052 PN->addIncoming(V2, A_ExitingBlock);
1053 }
1054 } else
1055 break;
1056 }
1057
1058 DT->changeImmediateDominator(B_Header, A_ExitingBlock);
1059 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT);
1060
1061 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit
1062 // block. Remove incoming PHINode values from ALoop's exiting block.
1063 // Add new incoming values from BLoop's incoming exiting value.
1064 // Update BLoop exit block's dominator info..
1065 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]);
1066 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end();
1067 BI != BE; ++BI) {
1068 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1069 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)],
1070 B_ExitingBlock);
1071 PN->removeIncomingValue(A_ExitingBlock);
1072 } else
1073 break;
1074 }
1075
1076 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock);
1077 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT);
1078
Dan Gohmanf159ccd2009-04-29 22:01:05 +00001079 //[*] Split ALoop's exit edge. This creates a new block which
Devang Patel38310052008-12-04 21:38:42 +00001080 // serves two purposes. First one is to hold PHINode defnitions
1081 // to ensure that ALoop's LCSSA form. Second use it to act
1082 // as a preheader for BLoop.
1083 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this);
1084
1085 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes
1086 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop.
1087 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1088 BI != BE; ++BI) {
1089 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1090 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
1091 PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName());
1092 newPHI->addIncoming(V1, A_ExitingBlock);
1093 A_ExitBlock->getInstList().push_front(newPHI);
1094 PN->removeIncomingValue(A_ExitBlock);
1095 PN->addIncoming(newPHI, A_ExitBlock);
1096 } else
1097 break;
1098 }
1099
1100 //[*] Eliminate split condition's inactive branch from ALoop.
1101 BasicBlock *A_SplitCondBlock = SplitCondition->getParent();
1102 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator());
1103 BasicBlock *A_InactiveBranch = NULL;
1104 BasicBlock *A_ActiveBranch = NULL;
1105 A_ActiveBranch = A_BR->getSuccessor(0);
1106 A_InactiveBranch = A_BR->getSuccessor(1);
1107 A_BR->setUnconditionalDest(A_ActiveBranch);
1108 removeBlocks(A_InactiveBranch, L, A_ActiveBranch);
1109
1110 //[*] Eliminate split condition's inactive branch in from BLoop.
1111 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]);
1112 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator());
1113 BasicBlock *B_InactiveBranch = NULL;
1114 BasicBlock *B_ActiveBranch = NULL;
1115 B_ActiveBranch = B_BR->getSuccessor(1);
1116 B_InactiveBranch = B_BR->getSuccessor(0);
1117 B_BR->setUnconditionalDest(B_ActiveBranch);
1118 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch);
1119
1120 BasicBlock *A_Header = ALoop->getHeader();
1121 if (A_ExitingBlock == A_Header)
1122 return true;
1123
1124 //[*] Move exit condition into split condition block to avoid
1125 // executing dead loop iteration.
1126 ICmpInst *B_ExitCondition = cast<ICmpInst>(ValueMap[ExitCondition]);
1127 Instruction *B_IndVarIncrement = cast<Instruction>(ValueMap[IVIncrement]);
1128 ICmpInst *B_SplitCondition = cast<ICmpInst>(ValueMap[SplitCondition]);
1129
1130 moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition,
1131 cast<ICmpInst>(SplitCondition), IndVar, IVIncrement,
1132 ALoop, EVOpNum);
1133
1134 moveExitCondition(B_SplitCondBlock, B_ActiveBranch,
1135 B_ExitBlock, B_ExitCondition,
1136 B_SplitCondition, B_IndVar, B_IndVarIncrement,
1137 BLoop, EVOpNum);
1138
1139 NumIndexSplit++;
1140 return true;
1141}
1142
1143/// cleanBlock - A block is considered clean if all non terminal instructions
1144/// are either, PHINodes, IV based.
1145bool LoopIndexSplit::cleanBlock(BasicBlock *BB) {
1146 Instruction *Terminator = BB->getTerminator();
1147 for(BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1148 BI != BE; ++BI) {
1149 Instruction *I = BI;
1150
1151 if (isa<PHINode>(I) || I == Terminator || I == ExitCondition
Devang Pateld96c60d2009-02-06 06:19:06 +00001152 || I == SplitCondition || IVBasedValues.count(I)
1153 || isa<DbgInfoIntrinsic>(I))
Devang Patel38310052008-12-04 21:38:42 +00001154 continue;
1155
Duncan Sands7af1c782009-05-06 06:49:50 +00001156 if (I->mayHaveSideEffects())
Devang Patel38310052008-12-04 21:38:42 +00001157 return false;
1158
1159 // I is used only inside this block then it is OK.
1160 bool usedOutsideBB = false;
1161 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
1162 UI != UE; ++UI) {
1163 Instruction *U = cast<Instruction>(UI);
1164 if (U->getParent() != BB)
1165 usedOutsideBB = true;
1166 }
1167 if (!usedOutsideBB)
1168 continue;
1169
1170 // Otherwise we have a instruction that may not allow loop spliting.
1171 return false;
1172 }
1173 return true;
1174}
1175
Devang Patel042b8772008-12-08 17:07:24 +00001176/// IVisLT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001177/// IV based value is less than the loop invariant then return the loop
1178/// invariant. Otherwise return NULL.
1179Value * LoopIndexSplit::IVisLT(ICmpInst &Op) {
1180 ICmpInst::Predicate P = Op.getPredicate();
1181 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1182 && IVBasedValues.count(Op.getOperand(0))
1183 && L->isLoopInvariant(Op.getOperand(1)))
1184 return Op.getOperand(1);
1185
1186 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1187 && IVBasedValues.count(Op.getOperand(1))
1188 && L->isLoopInvariant(Op.getOperand(0)))
1189 return Op.getOperand(0);
1190
1191 return NULL;
1192}
1193
Devang Patel042b8772008-12-08 17:07:24 +00001194/// IVisLE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001195/// IV based value is less than or equal to the loop invariant then
1196/// return the loop invariant. Otherwise return NULL.
1197Value * LoopIndexSplit::IVisLE(ICmpInst &Op) {
1198 ICmpInst::Predicate P = Op.getPredicate();
1199 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1200 && IVBasedValues.count(Op.getOperand(0))
1201 && L->isLoopInvariant(Op.getOperand(1)))
1202 return Op.getOperand(1);
1203
1204 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1205 && IVBasedValues.count(Op.getOperand(1))
1206 && L->isLoopInvariant(Op.getOperand(0)))
1207 return Op.getOperand(0);
1208
1209 return NULL;
1210}
1211
Devang Patel042b8772008-12-08 17:07:24 +00001212/// IVisGT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001213/// IV based value is greater than the loop invariant then return the loop
1214/// invariant. Otherwise return NULL.
1215Value * LoopIndexSplit::IVisGT(ICmpInst &Op) {
1216 ICmpInst::Predicate P = Op.getPredicate();
1217 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1218 && IVBasedValues.count(Op.getOperand(0))
1219 && L->isLoopInvariant(Op.getOperand(1)))
1220 return Op.getOperand(1);
1221
1222 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1223 && IVBasedValues.count(Op.getOperand(1))
1224 && L->isLoopInvariant(Op.getOperand(0)))
1225 return Op.getOperand(0);
1226
1227 return NULL;
1228}
1229
Devang Patel042b8772008-12-08 17:07:24 +00001230/// IVisGE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001231/// IV based value is greater than or equal to the loop invariant then
1232/// return the loop invariant. Otherwise return NULL.
1233Value * LoopIndexSplit::IVisGE(ICmpInst &Op) {
1234 ICmpInst::Predicate P = Op.getPredicate();
1235 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1236 && IVBasedValues.count(Op.getOperand(0))
1237 && L->isLoopInvariant(Op.getOperand(1)))
1238 return Op.getOperand(1);
1239
1240 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1241 && IVBasedValues.count(Op.getOperand(1))
1242 && L->isLoopInvariant(Op.getOperand(0)))
1243 return Op.getOperand(0);
1244
1245 return NULL;
1246}
1247