blob: 8b6a2334315e9f7f4d4a3fe978de91e7b607041f [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
Dan Gohman03e896b2009-11-05 21:11:53 +0000212 // If LoopSimplify form is not available, stay out of trouble.
213 if (!L->isLoopSimplifyForm())
214 return false;
215
Devang Patel3fe4f212007-08-15 02:14:55 +0000216 // FIXME - Nested loops make dominator info updates tricky.
Devang Patel4e8061c2007-08-14 23:53:57 +0000217 if (!L->getSubLoops().empty())
218 return false;
219
Devang Patel9704fcf2007-08-08 22:25:28 +0000220 DT = &getAnalysis<DominatorTree>();
Devang Patel423c8b22007-08-10 18:07:13 +0000221 LI = &getAnalysis<LoopInfo>();
Devang Patel7375bb92007-08-15 03:34:53 +0000222 DF = &getAnalysis<DominanceFrontier>();
Devang Patelfee76bd2007-08-07 00:25:56 +0000223
Devang Patel38310052008-12-04 21:38:42 +0000224 // Initialize loop data.
225 IndVar = L->getCanonicalInductionVariable();
226 if (!IndVar) return false;
Devang Patelbacf5192007-08-10 00:33:50 +0000227
Devang Patel38310052008-12-04 21:38:42 +0000228 bool P1InLoop = L->contains(IndVar->getIncomingBlock(1));
229 IVStartValue = IndVar->getIncomingValue(!P1InLoop);
230 IVIncrement = dyn_cast<Instruction>(IndVar->getIncomingValue(P1InLoop));
231 if (!IVIncrement) return false;
Devang Patel71554b82007-08-08 21:02:17 +0000232
Devang Patel38310052008-12-04 21:38:42 +0000233 IVBasedValues.clear();
234 IVBasedValues.insert(IndVar);
235 IVBasedValues.insert(IVIncrement);
Devang Patelbacf5192007-08-10 00:33:50 +0000236 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
Devang Patel38310052008-12-04 21:38:42 +0000237 I != E; ++I)
238 for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end();
239 BI != BE; ++BI) {
240 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BI))
241 if (BO != IVIncrement
242 && (BO->getOpcode() == Instruction::Add
243 || BO->getOpcode() == Instruction::Sub))
244 if (IVBasedValues.count(BO->getOperand(0))
245 && L->isLoopInvariant(BO->getOperand(1)))
246 IVBasedValues.insert(BO);
247 }
Devang Patelbacf5192007-08-10 00:33:50 +0000248
Devang Patel38310052008-12-04 21:38:42 +0000249 // Reject loop if loop exit condition is not suitable.
Dan Gohmanc8332462009-02-12 18:08:24 +0000250 BasicBlock *ExitingBlock = L->getExitingBlock();
251 if (!ExitingBlock)
Devang Patel38310052008-12-04 21:38:42 +0000252 return false;
Dan Gohmanc8332462009-02-12 18:08:24 +0000253 BranchInst *EBR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Devang Patel38310052008-12-04 21:38:42 +0000254 if (!EBR) return false;
255 ExitCondition = dyn_cast<ICmpInst>(EBR->getCondition());
256 if (!ExitCondition) return false;
Dan Gohmanc8332462009-02-12 18:08:24 +0000257 if (ExitingBlock != L->getLoopLatch()) return false;
Devang Patel38310052008-12-04 21:38:42 +0000258 IVExitValue = ExitCondition->getOperand(1);
259 if (!L->isLoopInvariant(IVExitValue))
260 IVExitValue = ExitCondition->getOperand(0);
261 if (!L->isLoopInvariant(IVExitValue))
262 return false;
Dan Gohmanf7ca1612009-06-27 22:58:27 +0000263 if (!IVBasedValues.count(
264 ExitCondition->getOperand(IVExitValue == ExitCondition->getOperand(0))))
265 return false;
Devang Patela5e27f82008-07-09 00:12:01 +0000266
267 // If start value is more then exit value where induction variable
268 // increments by 1 then we are potentially dealing with an infinite loop.
269 // Do not index split this loop.
Devang Patel38310052008-12-04 21:38:42 +0000270 if (ConstantInt *SV = dyn_cast<ConstantInt>(IVStartValue))
271 if (ConstantInt *EV = dyn_cast<ConstantInt>(IVExitValue))
272 if (SV->getSExtValue() > EV->getSExtValue())
273 return false;
Devang Patelc9d123d2007-08-09 01:39:01 +0000274
Devang Patel38310052008-12-04 21:38:42 +0000275 if (processOneIterationLoop())
276 return true;
Devang Patelfee76bd2007-08-07 00:25:56 +0000277
Devang Patel38310052008-12-04 21:38:42 +0000278 if (updateLoopIterationSpace())
279 return true;
Devang Patelfee76bd2007-08-07 00:25:56 +0000280
Devang Patel38310052008-12-04 21:38:42 +0000281 if (splitLoop())
282 return true;
Devang Pateld35ed2c2007-09-11 00:42:56 +0000283
284 return false;
Devang Patelfee76bd2007-08-07 00:25:56 +0000285}
286
Devang Patel38310052008-12-04 21:38:42 +0000287// --- Helper routines ---
288// isUsedOutsideLoop - Returns true iff V is used outside the loop L.
289static bool isUsedOutsideLoop(Value *V, Loop *L) {
290 for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
291 if (!L->contains(cast<Instruction>(*UI)->getParent()))
292 return true;
293 return false;
294}
Devang Patelfee76bd2007-08-07 00:25:56 +0000295
Devang Patel38310052008-12-04 21:38:42 +0000296// Return V+1
Owen Anderson1ff50b32009-07-03 00:54:20 +0000297static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt,
Owen Andersone922c022009-07-22 00:24:57 +0000298 LLVMContext &Context) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000299 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
Devang Patel38310052008-12-04 21:38:42 +0000300 return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt);
301}
Devang Patelfee76bd2007-08-07 00:25:56 +0000302
Devang Patel38310052008-12-04 21:38:42 +0000303// Return V-1
Owen Anderson1ff50b32009-07-03 00:54:20 +0000304static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt,
Owen Andersone922c022009-07-22 00:24:57 +0000305 LLVMContext &Context) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000306 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
Devang Patel38310052008-12-04 21:38:42 +0000307 return BinaryOperator::CreateSub(V, One, "lsp", InsertPt);
308}
Devang Patelfee76bd2007-08-07 00:25:56 +0000309
Devang Patel38310052008-12-04 21:38:42 +0000310// Return min(V1, V1)
311static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
312
Owen Anderson333c4002009-07-09 23:48:35 +0000313 Value *C = new ICmpInst(InsertPt,
314 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
315 V1, V2, "lsp");
Devang Patel38310052008-12-04 21:38:42 +0000316 return SelectInst::Create(C, V1, V2, "lsp", InsertPt);
317}
Devang Patelfee76bd2007-08-07 00:25:56 +0000318
Devang Patel38310052008-12-04 21:38:42 +0000319// Return max(V1, V2)
320static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
321
Owen Anderson333c4002009-07-09 23:48:35 +0000322 Value *C = new ICmpInst(InsertPt,
323 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
324 V1, V2, "lsp");
Devang Patel38310052008-12-04 21:38:42 +0000325 return SelectInst::Create(C, V2, V1, "lsp", InsertPt);
326}
Devang Patel968eee22007-09-19 00:15:16 +0000327
Devang Patel38310052008-12-04 21:38:42 +0000328/// processOneIterationLoop -- Eliminate loop if loop body is executed
329/// only once. For example,
330/// for (i = 0; i < N; ++i) {
331/// if ( i == X) {
332/// ...
333/// }
334/// }
335///
336bool LoopIndexSplit::processOneIterationLoop() {
337 SplitCondition = NULL;
Devang Patel84ef08b2007-09-19 00:11:01 +0000338 BasicBlock *Latch = L->getLoopLatch();
Devang Patel38310052008-12-04 21:38:42 +0000339 BasicBlock *Header = L->getHeader();
340 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
341 if (!BR) return false;
342 if (!isa<BranchInst>(Latch->getTerminator())) return false;
343 if (BR->isUnconditional()) return false;
344 SplitCondition = dyn_cast<ICmpInst>(BR->getCondition());
345 if (!SplitCondition) return false;
346 if (SplitCondition == ExitCondition) return false;
347 if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false;
348 if (BR->getOperand(1) != Latch) return false;
349 if (!IVBasedValues.count(SplitCondition->getOperand(0))
350 && !IVBasedValues.count(SplitCondition->getOperand(1)))
Devang Patel84ef08b2007-09-19 00:11:01 +0000351 return false;
352
Devang Patel38310052008-12-04 21:38:42 +0000353 // If IV is used outside the loop then this loop traversal is required.
354 // FIXME: Calculate and use last IV value.
355 if (isUsedOutsideLoop(IVIncrement, L))
356 return false;
357
358 // If BR operands are not IV or not loop invariants then skip this loop.
359 Value *OPV = SplitCondition->getOperand(0);
360 Value *SplitValue = SplitCondition->getOperand(1);
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000361 if (!L->isLoopInvariant(SplitValue))
362 std::swap(OPV, SplitValue);
Devang Patel38310052008-12-04 21:38:42 +0000363 if (!L->isLoopInvariant(SplitValue))
364 return false;
365 Instruction *OPI = dyn_cast<Instruction>(OPV);
Devang Patelb23c2322009-03-30 22:24:10 +0000366 if (!OPI)
367 return false;
Devang Patel38310052008-12-04 21:38:42 +0000368 if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L))
369 return false;
Devang Patelb23c2322009-03-30 22:24:10 +0000370 Value *StartValue = IVStartValue;
371 Value *ExitValue = IVExitValue;;
372
373 if (OPV != IndVar) {
374 // If BR operand is IV based then use this operand to calculate
375 // effective conditions for loop body.
376 BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV);
377 if (!BOPV)
378 return false;
379 if (BOPV->getOpcode() != Instruction::Add)
380 return false;
381 StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR);
382 ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR);
383 }
384
Devang Patel38310052008-12-04 21:38:42 +0000385 if (!cleanBlock(Header))
386 return false;
387
388 if (!cleanBlock(Latch))
389 return false;
390
391 // If the merge point for BR is not loop latch then skip this loop.
392 if (BR->getSuccessor(0) != Latch) {
393 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
394 assert (DF0 != DF->end() && "Unable to find dominance frontier");
395 if (!DF0->second.count(Latch))
396 return false;
397 }
398
399 if (BR->getSuccessor(1) != Latch) {
400 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
401 assert (DF1 != DF->end() && "Unable to find dominance frontier");
402 if (!DF1->second.count(Latch))
403 return false;
404 }
405
406 // Now, Current loop L contains compare instruction
407 // that compares induction variable, IndVar, against loop invariant. And
408 // entire (i.e. meaningful) loop body is dominated by this compare
409 // instruction. In such case eliminate
410 // loop structure surrounding this loop body. For example,
411 // for (int i = start; i < end; ++i) {
412 // if ( i == somevalue) {
413 // loop_body
414 // }
415 // }
416 // can be transformed into
417 // if (somevalue >= start && somevalue < end) {
418 // i = somevalue;
419 // loop_body
420 // }
Devang Patel6a2bfda2007-08-08 01:51:27 +0000421
Devang Patelebc5fea2007-08-20 20:49:01 +0000422 // Replace index variable with split value in loop body. Loop body is executed
423 // only when index variable is equal to split value.
Devang Patel38310052008-12-04 21:38:42 +0000424 IndVar->replaceAllUsesWith(SplitValue);
Devang Patelfee76bd2007-08-07 00:25:56 +0000425
Devang Patelfee76bd2007-08-07 00:25:56 +0000426 // Replace split condition in header.
427 // Transform
428 // SplitCondition : icmp eq i32 IndVar, SplitValue
429 // into
430 // c1 = icmp uge i32 SplitValue, StartValue
Devang Patelba32a5f2007-09-10 23:57:58 +0000431 // c2 = icmp ult i32 SplitValue, ExitValue
Devang Patelfee76bd2007-08-07 00:25:56 +0000432 // and i32 c1, c2
Nick Lewycky4a134af2009-10-25 05:20:17 +0000433 Instruction *C1 = new ICmpInst(BR, ExitCondition->isSigned() ?
Devang Patelfee76bd2007-08-07 00:25:56 +0000434 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
Owen Anderson333c4002009-07-09 23:48:35 +0000435 SplitValue, StartValue, "lisplit");
Devang Patel38310052008-12-04 21:38:42 +0000436
437 CmpInst::Predicate C2P = ExitCondition->getPredicate();
438 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
Chris Lattnerde648942009-08-28 00:43:14 +0000439 if (LatchBR->getOperand(1) != Header)
Devang Patel38310052008-12-04 21:38:42 +0000440 C2P = CmpInst::getInversePredicate(C2P);
Owen Anderson333c4002009-07-09 23:48:35 +0000441 Instruction *C2 = new ICmpInst(BR, C2P, SplitValue, ExitValue, "lisplit");
Devang Patel38310052008-12-04 21:38:42 +0000442 Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR);
443
444 SplitCondition->replaceAllUsesWith(NSplitCond);
445 SplitCondition->eraseFromParent();
Devang Patelfee76bd2007-08-07 00:25:56 +0000446
Devang Patelfc19fbd2008-10-10 22:02:57 +0000447 // Remove Latch to Header edge.
448 BasicBlock *LatchSucc = NULL;
449 Header->removePredecessor(Latch);
450 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
451 SI != E; ++SI) {
452 if (Header != *SI)
453 LatchSucc = *SI;
454 }
Devang Patelfc19fbd2008-10-10 22:02:57 +0000455
Devang Patelb23c2322009-03-30 22:24:10 +0000456 // Clean up latch block.
457 Value *LatchBRCond = LatchBR->getCondition();
458 LatchBR->setUnconditionalDest(LatchSucc);
459 RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond);
Devang Patel38310052008-12-04 21:38:42 +0000460
Devang Patel423c8b22007-08-10 18:07:13 +0000461 LPM->deleteLoopFromQueue(L);
Devang Patel787a7132007-08-08 21:39:47 +0000462
463 // Update Dominator Info.
464 // Only CFG change done is to remove Latch to Header edge. This
465 // does not change dominator tree because Latch did not dominate
466 // Header.
Devang Patelfc4c5f82007-08-13 22:13:24 +0000467 if (DF) {
Devang Patel787a7132007-08-08 21:39:47 +0000468 DominanceFrontier::iterator HeaderDF = DF->find(Header);
469 if (HeaderDF != DF->end())
470 DF->removeFromFrontier(HeaderDF, Header);
471
472 DominanceFrontier::iterator LatchDF = DF->find(Latch);
473 if (LatchDF != DF->end())
474 DF->removeFromFrontier(LatchDF, Header);
475 }
Devang Patel38310052008-12-04 21:38:42 +0000476
477 ++NumIndexSplitRemoved;
Devang Patelfee76bd2007-08-07 00:25:56 +0000478 return true;
479}
480
Devang Patel38310052008-12-04 21:38:42 +0000481/// restrictLoopBound - Op dominates loop body. Op compares an IV based value
482/// with a loop invariant value. Update loop's lower and upper bound based on
483/// the loop invariant value.
484bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) {
Nick Lewycky4a134af2009-10-25 05:20:17 +0000485 bool Sign = Op.isSigned();
Devang Patel38310052008-12-04 21:38:42 +0000486 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
Devang Patelfee76bd2007-08-07 00:25:56 +0000487
Devang Patel38310052008-12-04 21:38:42 +0000488 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
489 BranchInst *EBR =
490 cast<BranchInst>(ExitCondition->getParent()->getTerminator());
491 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
492 BasicBlock *T = EBR->getSuccessor(0);
493 EBR->setSuccessor(0, EBR->getSuccessor(1));
494 EBR->setSuccessor(1, T);
Devang Patelfee76bd2007-08-07 00:25:56 +0000495 }
496
Owen Andersone922c022009-07-22 00:24:57 +0000497 LLVMContext &Context = Op.getContext();
498
Devang Patel38310052008-12-04 21:38:42 +0000499 // New upper and lower bounds.
Devang Patel453a8442007-09-25 17:31:19 +0000500 Value *NLB = NULL;
Devang Patel38310052008-12-04 21:38:42 +0000501 Value *NUB = NULL;
502 if (Value *V = IVisLT(Op)) {
503 // Restrict upper bound.
504 if (IVisLE(*ExitCondition))
Owen Anderson1ff50b32009-07-03 00:54:20 +0000505 V = getMinusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000506 NUB = getMin(V, IVExitValue, Sign, PHTerm);
507 } else if (Value *V = IVisLE(Op)) {
508 // Restrict upper bound.
509 if (IVisLT(*ExitCondition))
Owen Anderson1ff50b32009-07-03 00:54:20 +0000510 V = getPlusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000511 NUB = getMin(V, IVExitValue, Sign, PHTerm);
512 } else if (Value *V = IVisGT(Op)) {
513 // Restrict lower bound.
Owen Anderson1ff50b32009-07-03 00:54:20 +0000514 V = getPlusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000515 NLB = getMax(V, IVStartValue, Sign, PHTerm);
516 } else if (Value *V = IVisGE(Op))
517 // Restrict lower bound.
518 NLB = getMax(V, IVStartValue, Sign, PHTerm);
Devang Patel453a8442007-09-25 17:31:19 +0000519
Devang Patel38310052008-12-04 21:38:42 +0000520 if (!NLB && !NUB)
521 return false;
Devang Patel453a8442007-09-25 17:31:19 +0000522
523 if (NLB) {
Devang Patel38310052008-12-04 21:38:42 +0000524 unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader());
Devang Patel453a8442007-09-25 17:31:19 +0000525 IndVar->setIncomingValue(i, NLB);
526 }
527
528 if (NUB) {
Devang Patel38310052008-12-04 21:38:42 +0000529 unsigned i = (ExitCondition->getOperand(0) != IVExitValue);
530 ExitCondition->setOperand(i, NUB);
Devang Patel453a8442007-09-25 17:31:19 +0000531 }
Devang Patel38310052008-12-04 21:38:42 +0000532 return true;
Devang Patel5279d062007-09-17 20:39:48 +0000533}
Devang Patel38310052008-12-04 21:38:42 +0000534
535/// updateLoopIterationSpace -- Update loop's iteration space if loop
536/// body is executed for certain IV range only. For example,
537///
538/// for (i = 0; i < N; ++i) {
539/// if ( i > A && i < B) {
540/// ...
541/// }
542/// }
Devang Patel042b8772008-12-08 17:07:24 +0000543/// is transformed to iterators from A to B, if A > 0 and B < N.
Devang Patel38310052008-12-04 21:38:42 +0000544///
545bool LoopIndexSplit::updateLoopIterationSpace() {
546 SplitCondition = NULL;
547 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
548 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
549 return false;
550 BasicBlock *Latch = L->getLoopLatch();
Devang Patel5279d062007-09-17 20:39:48 +0000551 BasicBlock *Header = L->getHeader();
Devang Patel38310052008-12-04 21:38:42 +0000552 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
553 if (!BR) return false;
554 if (!isa<BranchInst>(Latch->getTerminator())) return false;
555 if (BR->isUnconditional()) return false;
556 BinaryOperator *AND = dyn_cast<BinaryOperator>(BR->getCondition());
557 if (!AND) return false;
558 if (AND->getOpcode() != Instruction::And) return false;
559 ICmpInst *Op0 = dyn_cast<ICmpInst>(AND->getOperand(0));
560 ICmpInst *Op1 = dyn_cast<ICmpInst>(AND->getOperand(1));
561 if (!Op0 || !Op1)
562 return false;
563 IVBasedValues.insert(AND);
564 IVBasedValues.insert(Op0);
565 IVBasedValues.insert(Op1);
566 if (!cleanBlock(Header)) return false;
Devang Patel453a8442007-09-25 17:31:19 +0000567 BasicBlock *ExitingBlock = ExitCondition->getParent();
Devang Patel38310052008-12-04 21:38:42 +0000568 if (!cleanBlock(ExitingBlock)) return false;
Devang Patel453a8442007-09-25 17:31:19 +0000569
Devang Patelcf42ee42009-03-02 23:39:14 +0000570 // If the merge point for BR is not loop latch then skip this loop.
571 if (BR->getSuccessor(0) != Latch) {
572 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
573 assert (DF0 != DF->end() && "Unable to find dominance frontier");
574 if (!DF0->second.count(Latch))
575 return false;
576 }
577
578 if (BR->getSuccessor(1) != Latch) {
579 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
580 assert (DF1 != DF->end() && "Unable to find dominance frontier");
581 if (!DF1->second.count(Latch))
582 return false;
583 }
584
Devang Patel38310052008-12-04 21:38:42 +0000585 // Verify that loop exiting block has only two predecessor, where one pred
Devang Patel453a8442007-09-25 17:31:19 +0000586 // is split condition block. The other predecessor will become exiting block's
587 // dominator after CFG is updated. TODO : Handle CFG's where exiting block has
588 // more then two predecessors. This requires extra work in updating dominator
589 // information.
590 BasicBlock *ExitingBBPred = NULL;
591 for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock);
592 PI != PE; ++PI) {
593 BasicBlock *BB = *PI;
Devang Patel38310052008-12-04 21:38:42 +0000594 if (Header == BB)
Devang Patel453a8442007-09-25 17:31:19 +0000595 continue;
596 if (ExitingBBPred)
597 return false;
598 else
599 ExitingBBPred = BB;
600 }
Devang Patel453a8442007-09-25 17:31:19 +0000601
Devang Patel38310052008-12-04 21:38:42 +0000602 if (!restrictLoopBound(*Op0))
603 return false;
Devang Patel453a8442007-09-25 17:31:19 +0000604
Devang Patel38310052008-12-04 21:38:42 +0000605 if (!restrictLoopBound(*Op1))
606 return false;
607
608 // Update CFG.
609 if (BR->getSuccessor(0) == ExitingBlock)
610 BR->setUnconditionalDest(BR->getSuccessor(1));
Devang Patel453a8442007-09-25 17:31:19 +0000611 else
Devang Patel38310052008-12-04 21:38:42 +0000612 BR->setUnconditionalDest(BR->getSuccessor(0));
Devang Patel453a8442007-09-25 17:31:19 +0000613
Devang Patel38310052008-12-04 21:38:42 +0000614 AND->eraseFromParent();
Dan Gohmana8c763b2008-08-14 18:13:49 +0000615 if (Op0->use_empty())
Devang Patel453a8442007-09-25 17:31:19 +0000616 Op0->eraseFromParent();
Dan Gohmana8c763b2008-08-14 18:13:49 +0000617 if (Op1->use_empty())
Devang Patel453a8442007-09-25 17:31:19 +0000618 Op1->eraseFromParent();
Devang Patel453a8442007-09-25 17:31:19 +0000619
620 // Update domiantor info. Now, ExitingBlock has only one predecessor,
621 // ExitingBBPred, and it is ExitingBlock's immediate domiantor.
622 DT->changeImmediateDominator(ExitingBlock, ExitingBBPred);
Devang Patel38310052008-12-04 21:38:42 +0000623
624 BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1);
625 if (L->contains(ExitBlock))
626 ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0);
627
628 // If ExitingBlock is a member of the loop basic blocks' DF list then
629 // replace ExitingBlock with header and exit block in the DF list
630 DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock);
Devang Patel453a8442007-09-25 17:31:19 +0000631 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
632 I != E; ++I) {
633 BasicBlock *BB = *I;
634 if (BB == Header || BB == ExitingBlock)
635 continue;
636 DominanceFrontier::iterator BBDF = DF->find(BB);
637 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
638 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
639 while (DomSetI != DomSetE) {
640 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
641 ++DomSetI;
642 BasicBlock *DFBB = *CurrentItr;
643 if (DFBB == ExitingBlock) {
644 BBDF->second.erase(DFBB);
Devang Patel38310052008-12-04 21:38:42 +0000645 for (DominanceFrontier::DomSetType::iterator
646 EBI = ExitingBlockDF->second.begin(),
647 EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI)
648 BBDF->second.insert(*EBI);
Devang Patel453a8442007-09-25 17:31:19 +0000649 }
650 }
651 }
Devang Patel38310052008-12-04 21:38:42 +0000652 NumRestrictBounds++;
Devang Patel1c013502007-09-25 17:43:08 +0000653 return true;
Devang Patel5279d062007-09-17 20:39:48 +0000654}
655
Devang Patela6a86632007-08-14 18:35:57 +0000656/// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
657/// This routine is used to remove split condition's dead branch, dominated by
658/// DeadBB. LiveBB dominates split conidition's other branch.
659void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
660 BasicBlock *LiveBB) {
Devang Patel98147a32007-08-12 07:02:51 +0000661
Devang Patel5b8ec612007-08-15 03:31:47 +0000662 // First update DeadBB's dominance frontier.
Devang Patel96bf5242007-08-17 21:59:16 +0000663 SmallVector<BasicBlock *, 8> FrontierBBs;
Devang Patel5b8ec612007-08-15 03:31:47 +0000664 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
665 if (DeadBBDF != DF->end()) {
666 SmallVector<BasicBlock *, 8> PredBlocks;
667
668 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
669 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
Devang Patel38310052008-12-04 21:38:42 +0000670 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI)
671 {
Devang Patel5b8ec612007-08-15 03:31:47 +0000672 BasicBlock *FrontierBB = *DeadBBSetI;
Devang Patel96bf5242007-08-17 21:59:16 +0000673 FrontierBBs.push_back(FrontierBB);
674
Devang Patel5b8ec612007-08-15 03:31:47 +0000675 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
676 PredBlocks.clear();
677 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
678 PI != PE; ++PI) {
679 BasicBlock *P = *PI;
680 if (P == DeadBB || DT->dominates(DeadBB, P))
681 PredBlocks.push_back(P);
Devang Patelfc4c5f82007-08-13 22:13:24 +0000682 }
Devang Patel96bf5242007-08-17 21:59:16 +0000683
Devang Patel5b8ec612007-08-15 03:31:47 +0000684 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
685 FBI != FBE; ++FBI) {
686 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
687 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
688 PE = PredBlocks.end(); PI != PE; ++PI) {
689 BasicBlock *P = *PI;
690 PN->removeIncomingValue(P);
691 }
692 }
693 else
694 break;
Devang Patel96bf5242007-08-17 21:59:16 +0000695 }
Devang Patel98147a32007-08-12 07:02:51 +0000696 }
Devang Patel98147a32007-08-12 07:02:51 +0000697 }
Devang Patel5b8ec612007-08-15 03:31:47 +0000698
699 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
700 SmallVector<BasicBlock *, 32> WorkList;
701 DomTreeNode *DN = DT->getNode(DeadBB);
702 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
703 E = df_end(DN); DI != E; ++DI) {
704 BasicBlock *BB = DI->getBlock();
705 WorkList.push_back(BB);
Owen Anderson1d0be152009-08-13 21:58:54 +0000706 BB->replaceAllUsesWith(UndefValue::get(
707 Type::getLabelTy(DeadBB->getContext())));
Devang Patel5b8ec612007-08-15 03:31:47 +0000708 }
709
710 while (!WorkList.empty()) {
711 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
Devang Patel575ec802009-03-25 23:57:48 +0000712 LPM->deleteSimpleAnalysisValue(BB, LP);
Devang Patel5b8ec612007-08-15 03:31:47 +0000713 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
Devang Pateld15dd8c2007-09-20 23:01:50 +0000714 BBI != BBE; ) {
Devang Patel5b8ec612007-08-15 03:31:47 +0000715 Instruction *I = BBI;
Devang Pateld15dd8c2007-09-20 23:01:50 +0000716 ++BBI;
Devang Patel5b8ec612007-08-15 03:31:47 +0000717 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Owen Anderson0b2a1532009-04-14 01:04:19 +0000718 LPM->deleteSimpleAnalysisValue(I, LP);
Devang Patel5b8ec612007-08-15 03:31:47 +0000719 I->eraseFromParent();
720 }
Devang Patel5b8ec612007-08-15 03:31:47 +0000721 DT->eraseNode(BB);
722 DF->removeBlock(BB);
723 LI->removeBlock(BB);
724 BB->eraseFromParent();
725 }
Devang Patel96bf5242007-08-17 21:59:16 +0000726
727 // Update Frontier BBs' dominator info.
728 while (!FrontierBBs.empty()) {
729 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
730 BasicBlock *NewDominator = FBB->getSinglePredecessor();
731 if (!NewDominator) {
732 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
733 NewDominator = *PI;
734 ++PI;
735 if (NewDominator != LiveBB) {
736 for(; PI != PE; ++PI) {
737 BasicBlock *P = *PI;
738 if (P == LiveBB) {
739 NewDominator = LiveBB;
740 break;
741 }
742 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
743 }
744 }
745 }
746 assert (NewDominator && "Unable to fix dominator info.");
747 DT->changeImmediateDominator(FBB, NewDominator);
748 DF->changeImmediateDominator(FBB, NewDominator, DT);
749 }
750
Devang Patel98147a32007-08-12 07:02:51 +0000751}
752
Devang Pateld79faee2007-08-25 02:39:24 +0000753// moveExitCondition - Move exit condition EC into split condition block CondBB.
754void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
Devang Patel38310052008-12-04 21:38:42 +0000755 BasicBlock *ExitBB, ICmpInst *EC,
756 ICmpInst *SC, PHINode *IV,
757 Instruction *IVAdd, Loop *LP,
758 unsigned ExitValueNum) {
Devang Pateld79faee2007-08-25 02:39:24 +0000759
760 BasicBlock *ExitingBB = EC->getParent();
761 Instruction *CurrentBR = CondBB->getTerminator();
762
763 // Move exit condition into split condition block.
764 EC->moveBefore(CurrentBR);
765 EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV);
766
767 // Move exiting block's branch into split condition block. Update its branch
768 // destination.
769 BranchInst *ExitingBR = cast<BranchInst>(ExitingBB->getTerminator());
770 ExitingBR->moveBefore(CurrentBR);
Devang Patel23067df2008-02-13 22:06:36 +0000771 BasicBlock *OrigDestBB = NULL;
772 if (ExitingBR->getSuccessor(0) == ExitBB) {
773 OrigDestBB = ExitingBR->getSuccessor(1);
Devang Pateld79faee2007-08-25 02:39:24 +0000774 ExitingBR->setSuccessor(1, ActiveBB);
Devang Patel23067df2008-02-13 22:06:36 +0000775 }
776 else {
777 OrigDestBB = ExitingBR->getSuccessor(0);
Devang Pateld79faee2007-08-25 02:39:24 +0000778 ExitingBR->setSuccessor(0, ActiveBB);
Devang Patel23067df2008-02-13 22:06:36 +0000779 }
Devang Pateld79faee2007-08-25 02:39:24 +0000780
781 // Remove split condition and current split condition branch.
782 SC->eraseFromParent();
783 CurrentBR->eraseFromParent();
784
Devang Patel23067df2008-02-13 22:06:36 +0000785 // Connect exiting block to original destination.
Gabor Greif051a9502008-04-06 20:25:17 +0000786 BranchInst::Create(OrigDestBB, ExitingBB);
Devang Pateld79faee2007-08-25 02:39:24 +0000787
788 // Update PHINodes
Devang Patelea069062008-02-13 22:23:07 +0000789 updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP);
Devang Pateld79faee2007-08-25 02:39:24 +0000790
791 // Fix dominator info.
792 // ExitBB is now dominated by CondBB
793 DT->changeImmediateDominator(ExitBB, CondBB);
794 DF->changeImmediateDominator(ExitBB, CondBB, DT);
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000795
796 // Blocks outside the loop may have been in the dominance frontier of blocks
797 // inside the condition; this is now impossible because the blocks inside the
798 // condition no loger dominate the exit. Remove the relevant blocks from
799 // the dominance frontiers.
800 for (Loop::block_iterator I = LP->block_begin(), E = LP->block_end();
801 I != E; ++I) {
802 if (*I == CondBB || !DT->dominates(CondBB, *I)) continue;
803 DominanceFrontier::iterator BBDF = DF->find(*I);
Devang Pateld79faee2007-08-25 02:39:24 +0000804 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
805 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
806 while (DomSetI != DomSetE) {
807 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
808 ++DomSetI;
809 BasicBlock *DFBB = *CurrentItr;
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000810 if (!LP->contains(DFBB))
Devang Pateld79faee2007-08-25 02:39:24 +0000811 BBDF->second.erase(DFBB);
Devang Pateld79faee2007-08-25 02:39:24 +0000812 }
813 }
814}
815
816/// updatePHINodes - CFG has been changed.
817/// Before
818/// - ExitBB's single predecessor was Latch
819/// - Latch's second successor was Header
820/// Now
Devang Patel82ada542008-02-08 22:49:13 +0000821/// - ExitBB's single predecessor is Header
822/// - Latch's one and only successor is Header
Devang Pateld79faee2007-08-25 02:39:24 +0000823///
824/// Update ExitBB PHINodes' to reflect this change.
825void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
826 BasicBlock *Header,
Devang Patelea069062008-02-13 22:23:07 +0000827 PHINode *IV, Instruction *IVIncrement,
828 Loop *LP) {
Devang Pateld79faee2007-08-25 02:39:24 +0000829
830 for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end();
Devang Patel4a3c0ac2008-03-27 17:32:46 +0000831 BI != BE; ) {
Devang Pateld79faee2007-08-25 02:39:24 +0000832 PHINode *PN = dyn_cast<PHINode>(BI);
Devang Patel4a3c0ac2008-03-27 17:32:46 +0000833 ++BI;
Devang Pateld79faee2007-08-25 02:39:24 +0000834 if (!PN)
835 break;
836
837 Value *V = PN->getIncomingValueForBlock(Latch);
838 if (PHINode *PHV = dyn_cast<PHINode>(V)) {
Devang Patel82ada542008-02-08 22:49:13 +0000839 // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use
840 // in Header which is new incoming value for PN.
Devang Pateld79faee2007-08-25 02:39:24 +0000841 Value *NewV = NULL;
842 for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end();
Devang Patel82ada542008-02-08 22:49:13 +0000843 UI != E; ++UI)
844 if (PHINode *U = dyn_cast<PHINode>(*UI))
Devang Patelea069062008-02-13 22:23:07 +0000845 if (LP->contains(U->getParent())) {
Devang Patel82ada542008-02-08 22:49:13 +0000846 NewV = U;
847 break;
848 }
849
Devang Patel60a12902008-03-24 20:16:14 +0000850 // Add incoming value from header only if PN has any use inside the loop.
851 if (NewV)
852 PN->addIncoming(NewV, Header);
Devang Pateld79faee2007-08-25 02:39:24 +0000853
854 } else if (Instruction *PHI = dyn_cast<Instruction>(V)) {
855 // If this instruction is IVIncrement then IV is new incoming value
856 // from header otherwise this instruction must be incoming value from
857 // header because loop is in LCSSA form.
858 if (PHI == IVIncrement)
859 PN->addIncoming(IV, Header);
860 else
861 PN->addIncoming(V, Header);
862 } else
863 // Otherwise this is an incoming value from header because loop is in
864 // LCSSA form.
865 PN->addIncoming(V, Header);
866
867 // Remove incoming value from Latch.
868 PN->removeIncomingValue(Latch);
869 }
870}
Devang Patel38310052008-12-04 21:38:42 +0000871
872bool LoopIndexSplit::splitLoop() {
873 SplitCondition = NULL;
874 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
875 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
876 return false;
877 BasicBlock *Header = L->getHeader();
878 BasicBlock *Latch = L->getLoopLatch();
879 BranchInst *SBR = NULL; // Split Condition Branch
880 BranchInst *EBR = cast<BranchInst>(ExitCondition->getParent()->getTerminator());
881 // If Exiting block includes loop variant instructions then this
882 // loop may not be split safely.
883 BasicBlock *ExitingBlock = ExitCondition->getParent();
884 if (!cleanBlock(ExitingBlock)) return false;
885
Owen Andersone922c022009-07-22 00:24:57 +0000886 LLVMContext &Context = Header->getContext();
887
Devang Patel38310052008-12-04 21:38:42 +0000888 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
889 I != E; ++I) {
890 BranchInst *BR = dyn_cast<BranchInst>((*I)->getTerminator());
891 if (!BR || BR->isUnconditional()) continue;
892 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
893 if (!CI || CI == ExitCondition
894 || CI->getPredicate() == ICmpInst::ICMP_NE
895 || CI->getPredicate() == ICmpInst::ICMP_EQ)
896 continue;
897
898 // Unable to handle triangle loops at the moment.
899 // In triangle loop, split condition is in header and one of the
900 // the split destination is loop latch. If split condition is EQ
901 // then such loops are already handle in processOneIterationLoop().
902 if (Header == (*I)
903 && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1)))
904 continue;
905
906 // If the block does not dominate the latch then this is not a diamond.
907 // Such loop may not benefit from index split.
908 if (!DT->dominates((*I), Latch))
909 continue;
910
911 // If split condition branches heads do not have single predecessor,
912 // SplitCondBlock, then is not possible to remove inactive branch.
913 if (!BR->getSuccessor(0)->getSinglePredecessor()
914 || !BR->getSuccessor(1)->getSinglePredecessor())
915 return false;
916
917 // If the merge point for BR is not loop latch then skip this condition.
918 if (BR->getSuccessor(0) != Latch) {
919 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
920 assert (DF0 != DF->end() && "Unable to find dominance frontier");
921 if (!DF0->second.count(Latch))
922 continue;
923 }
924
925 if (BR->getSuccessor(1) != Latch) {
926 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
927 assert (DF1 != DF->end() && "Unable to find dominance frontier");
928 if (!DF1->second.count(Latch))
929 continue;
930 }
931 SplitCondition = CI;
932 SBR = BR;
933 break;
934 }
935
936 if (!SplitCondition)
937 return false;
938
939 // If the predicate sign does not match then skip.
Nick Lewycky4a134af2009-10-25 05:20:17 +0000940 if (ExitCondition->isSigned() != SplitCondition->isSigned())
Devang Patel38310052008-12-04 21:38:42 +0000941 return false;
942
943 unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue);
944 unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0));
945 Value *SplitValue = SplitCondition->getOperand(SVOpNum);
946 if (!L->isLoopInvariant(SplitValue))
947 return false;
948 if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum)))
949 return false;
950
951 // Normalize loop conditions so that it is easier to calculate new loop
952 // bounds.
953 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
954 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
955 BasicBlock *T = EBR->getSuccessor(0);
956 EBR->setSuccessor(0, EBR->getSuccessor(1));
957 EBR->setSuccessor(1, T);
958 }
959
960 if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) {
961 SplitCondition->setPredicate(SplitCondition->getInversePredicate());
962 BasicBlock *T = SBR->getSuccessor(0);
963 SBR->setSuccessor(0, SBR->getSuccessor(1));
964 SBR->setSuccessor(1, T);
965 }
966
967 //[*] Calculate new loop bounds.
968 Value *AEV = SplitValue;
969 Value *BSV = SplitValue;
Nick Lewycky4a134af2009-10-25 05:20:17 +0000970 bool Sign = SplitCondition->isSigned();
Devang Patel38310052008-12-04 21:38:42 +0000971 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
972
973 if (IVisLT(*ExitCondition)) {
974 if (IVisLT(*SplitCondition)) {
975 /* Do nothing */
976 }
977 else if (IVisLE(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000978 AEV = getPlusOne(SplitValue, Sign, PHTerm, Context);
979 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000980 } else {
981 assert (0 && "Unexpected split condition!");
982 }
983 }
984 else if (IVisLE(*ExitCondition)) {
985 if (IVisLT(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000986 AEV = getMinusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000987 }
988 else if (IVisLE(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000989 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000990 } else {
991 assert (0 && "Unexpected split condition!");
992 }
993 } else {
994 assert (0 && "Unexpected exit condition!");
995 }
996 AEV = getMin(AEV, IVExitValue, Sign, PHTerm);
997 BSV = getMax(BSV, IVStartValue, Sign, PHTerm);
998
999 // [*] Clone Loop
1000 DenseMap<const Value *, Value *> ValueMap;
1001 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this);
1002 Loop *ALoop = L;
1003
1004 // [*] ALoop's exiting edge enters BLoop's header.
1005 // ALoop's original exit block becomes BLoop's exit block.
1006 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]);
1007 BasicBlock *A_ExitingBlock = ExitCondition->getParent();
1008 BranchInst *A_ExitInsn =
1009 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator());
1010 assert (A_ExitInsn && "Unable to find suitable loop exit branch");
1011 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1);
1012 BasicBlock *B_Header = BLoop->getHeader();
1013 if (ALoop->contains(B_ExitBlock)) {
1014 B_ExitBlock = A_ExitInsn->getSuccessor(0);
1015 A_ExitInsn->setSuccessor(0, B_Header);
1016 } else
1017 A_ExitInsn->setSuccessor(1, B_Header);
1018
1019 // [*] Update ALoop's exit value using new exit value.
1020 ExitCondition->setOperand(EVOpNum, AEV);
1021
1022 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from
1023 // original loop's preheader. Add incoming PHINode values from
1024 // ALoop's exiting block. Update BLoop header's domiantor info.
1025
1026 // Collect inverse map of Header PHINodes.
1027 DenseMap<Value *, Value *> InverseMap;
1028 for (BasicBlock::iterator BI = ALoop->getHeader()->begin(),
1029 BE = ALoop->getHeader()->end(); BI != BE; ++BI) {
1030 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1031 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
1032 InverseMap[PNClone] = PN;
1033 } else
1034 break;
1035 }
1036
1037 BasicBlock *A_Preheader = ALoop->getLoopPreheader();
1038 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1039 BI != BE; ++BI) {
1040 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1041 // Remove incoming value from original preheader.
1042 PN->removeIncomingValue(A_Preheader);
1043
1044 // Add incoming value from A_ExitingBlock.
1045 if (PN == B_IndVar)
1046 PN->addIncoming(BSV, A_ExitingBlock);
1047 else {
1048 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
1049 Value *V2 = NULL;
1050 // If loop header is also loop exiting block then
1051 // OrigPN is incoming value for B loop header.
1052 if (A_ExitingBlock == ALoop->getHeader())
1053 V2 = OrigPN;
1054 else
1055 V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
1056 PN->addIncoming(V2, A_ExitingBlock);
1057 }
1058 } else
1059 break;
1060 }
1061
1062 DT->changeImmediateDominator(B_Header, A_ExitingBlock);
1063 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT);
1064
1065 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit
1066 // block. Remove incoming PHINode values from ALoop's exiting block.
1067 // Add new incoming values from BLoop's incoming exiting value.
1068 // Update BLoop exit block's dominator info..
1069 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]);
1070 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end();
1071 BI != BE; ++BI) {
1072 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1073 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)],
1074 B_ExitingBlock);
1075 PN->removeIncomingValue(A_ExitingBlock);
1076 } else
1077 break;
1078 }
1079
1080 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock);
1081 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT);
1082
Dan Gohmanf159ccd2009-04-29 22:01:05 +00001083 //[*] Split ALoop's exit edge. This creates a new block which
Devang Patel38310052008-12-04 21:38:42 +00001084 // serves two purposes. First one is to hold PHINode defnitions
1085 // to ensure that ALoop's LCSSA form. Second use it to act
1086 // as a preheader for BLoop.
1087 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this);
1088
1089 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes
1090 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop.
1091 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1092 BI != BE; ++BI) {
1093 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1094 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
1095 PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName());
1096 newPHI->addIncoming(V1, A_ExitingBlock);
1097 A_ExitBlock->getInstList().push_front(newPHI);
1098 PN->removeIncomingValue(A_ExitBlock);
1099 PN->addIncoming(newPHI, A_ExitBlock);
1100 } else
1101 break;
1102 }
1103
1104 //[*] Eliminate split condition's inactive branch from ALoop.
1105 BasicBlock *A_SplitCondBlock = SplitCondition->getParent();
1106 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator());
1107 BasicBlock *A_InactiveBranch = NULL;
1108 BasicBlock *A_ActiveBranch = NULL;
1109 A_ActiveBranch = A_BR->getSuccessor(0);
1110 A_InactiveBranch = A_BR->getSuccessor(1);
1111 A_BR->setUnconditionalDest(A_ActiveBranch);
1112 removeBlocks(A_InactiveBranch, L, A_ActiveBranch);
1113
1114 //[*] Eliminate split condition's inactive branch in from BLoop.
1115 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]);
1116 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator());
1117 BasicBlock *B_InactiveBranch = NULL;
1118 BasicBlock *B_ActiveBranch = NULL;
1119 B_ActiveBranch = B_BR->getSuccessor(1);
1120 B_InactiveBranch = B_BR->getSuccessor(0);
1121 B_BR->setUnconditionalDest(B_ActiveBranch);
1122 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch);
1123
1124 BasicBlock *A_Header = ALoop->getHeader();
1125 if (A_ExitingBlock == A_Header)
1126 return true;
1127
1128 //[*] Move exit condition into split condition block to avoid
1129 // executing dead loop iteration.
1130 ICmpInst *B_ExitCondition = cast<ICmpInst>(ValueMap[ExitCondition]);
1131 Instruction *B_IndVarIncrement = cast<Instruction>(ValueMap[IVIncrement]);
1132 ICmpInst *B_SplitCondition = cast<ICmpInst>(ValueMap[SplitCondition]);
1133
1134 moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition,
1135 cast<ICmpInst>(SplitCondition), IndVar, IVIncrement,
1136 ALoop, EVOpNum);
1137
1138 moveExitCondition(B_SplitCondBlock, B_ActiveBranch,
1139 B_ExitBlock, B_ExitCondition,
1140 B_SplitCondition, B_IndVar, B_IndVarIncrement,
1141 BLoop, EVOpNum);
1142
1143 NumIndexSplit++;
1144 return true;
1145}
1146
1147/// cleanBlock - A block is considered clean if all non terminal instructions
1148/// are either, PHINodes, IV based.
1149bool LoopIndexSplit::cleanBlock(BasicBlock *BB) {
1150 Instruction *Terminator = BB->getTerminator();
1151 for(BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1152 BI != BE; ++BI) {
1153 Instruction *I = BI;
1154
1155 if (isa<PHINode>(I) || I == Terminator || I == ExitCondition
Devang Pateld96c60d2009-02-06 06:19:06 +00001156 || I == SplitCondition || IVBasedValues.count(I)
1157 || isa<DbgInfoIntrinsic>(I))
Devang Patel38310052008-12-04 21:38:42 +00001158 continue;
1159
Duncan Sands7af1c782009-05-06 06:49:50 +00001160 if (I->mayHaveSideEffects())
Devang Patel38310052008-12-04 21:38:42 +00001161 return false;
1162
1163 // I is used only inside this block then it is OK.
1164 bool usedOutsideBB = false;
1165 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
1166 UI != UE; ++UI) {
1167 Instruction *U = cast<Instruction>(UI);
1168 if (U->getParent() != BB)
1169 usedOutsideBB = true;
1170 }
1171 if (!usedOutsideBB)
1172 continue;
1173
1174 // Otherwise we have a instruction that may not allow loop spliting.
1175 return false;
1176 }
1177 return true;
1178}
1179
Devang Patel042b8772008-12-08 17:07:24 +00001180/// IVisLT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001181/// IV based value is less than the loop invariant then return the loop
1182/// invariant. Otherwise return NULL.
1183Value * LoopIndexSplit::IVisLT(ICmpInst &Op) {
1184 ICmpInst::Predicate P = Op.getPredicate();
1185 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1186 && IVBasedValues.count(Op.getOperand(0))
1187 && L->isLoopInvariant(Op.getOperand(1)))
1188 return Op.getOperand(1);
1189
1190 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1191 && IVBasedValues.count(Op.getOperand(1))
1192 && L->isLoopInvariant(Op.getOperand(0)))
1193 return Op.getOperand(0);
1194
1195 return NULL;
1196}
1197
Devang Patel042b8772008-12-08 17:07:24 +00001198/// IVisLE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001199/// IV based value is less than or equal to the loop invariant then
1200/// return the loop invariant. Otherwise return NULL.
1201Value * LoopIndexSplit::IVisLE(ICmpInst &Op) {
1202 ICmpInst::Predicate P = Op.getPredicate();
1203 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1204 && IVBasedValues.count(Op.getOperand(0))
1205 && L->isLoopInvariant(Op.getOperand(1)))
1206 return Op.getOperand(1);
1207
1208 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1209 && IVBasedValues.count(Op.getOperand(1))
1210 && L->isLoopInvariant(Op.getOperand(0)))
1211 return Op.getOperand(0);
1212
1213 return NULL;
1214}
1215
Devang Patel042b8772008-12-08 17:07:24 +00001216/// IVisGT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001217/// IV based value is greater than the loop invariant then return the loop
1218/// invariant. Otherwise return NULL.
1219Value * LoopIndexSplit::IVisGT(ICmpInst &Op) {
1220 ICmpInst::Predicate P = Op.getPredicate();
1221 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1222 && IVBasedValues.count(Op.getOperand(0))
1223 && L->isLoopInvariant(Op.getOperand(1)))
1224 return Op.getOperand(1);
1225
1226 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1227 && IVBasedValues.count(Op.getOperand(1))
1228 && L->isLoopInvariant(Op.getOperand(0)))
1229 return Op.getOperand(0);
1230
1231 return NULL;
1232}
1233
Devang Patel042b8772008-12-08 17:07:24 +00001234/// IVisGE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001235/// IV based value is greater than or equal to the loop invariant then
1236/// return the loop invariant. Otherwise return NULL.
1237Value * LoopIndexSplit::IVisGE(ICmpInst &Op) {
1238 ICmpInst::Predicate P = Op.getPredicate();
1239 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1240 && IVBasedValues.count(Op.getOperand(0))
1241 && L->isLoopInvariant(Op.getOperand(1)))
1242 return Op.getOperand(1);
1243
1244 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1245 && IVBasedValues.count(Op.getOperand(1))
1246 && L->isLoopInvariant(Op.getOperand(0)))
1247 return Op.getOperand(0);
1248
1249 return NULL;
1250}
1251