blob: f5e5d350de510188ec48a63b4a7bb28b61a6232b [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"
54
Devang Patelfee76bd2007-08-07 00:25:56 +000055#include "llvm/Transforms/Scalar.h"
Devang Pateld96c60d2009-02-06 06:19:06 +000056#include "llvm/IntrinsicInst.h"
Owen Anderson1ff50b32009-07-03 00:54:20 +000057#include "llvm/LLVMContext.h"
Devang Patelfee76bd2007-08-07 00:25:56 +000058#include "llvm/Analysis/LoopPass.h"
Dan Gohman97b6e2c2009-02-17 20:50:11 +000059#include "llvm/Analysis/ScalarEvolution.h"
Devang Patel787a7132007-08-08 21:39:47 +000060#include "llvm/Analysis/Dominators.h"
Devang Patel423c8b22007-08-10 18:07:13 +000061#include "llvm/Transforms/Utils/BasicBlockUtils.h"
62#include "llvm/Transforms/Utils/Cloning.h"
Devang Patelb23c2322009-03-30 22:24:10 +000063#include "llvm/Transforms/Utils/Local.h"
Devang Patelfee76bd2007-08-07 00:25:56 +000064#include "llvm/Support/Compiler.h"
Devang Patel5b8ec612007-08-15 03:31:47 +000065#include "llvm/ADT/DepthFirstIterator.h"
Devang Patelfee76bd2007-08-07 00:25:56 +000066#include "llvm/ADT/Statistic.h"
67
68using namespace llvm;
69
Devang Patel38310052008-12-04 21:38:42 +000070STATISTIC(NumIndexSplit, "Number of loop index split");
71STATISTIC(NumIndexSplitRemoved, "Number of loops eliminated by loop index split");
72STATISTIC(NumRestrictBounds, "Number of loop iteration space restricted");
Devang Patelfee76bd2007-08-07 00:25:56 +000073
74namespace {
75
76 class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass {
77
78 public:
79 static char ID; // Pass ID, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000080 LoopIndexSplit() : LoopPass(&ID) {}
Devang Patelfee76bd2007-08-07 00:25:56 +000081
82 // Index split Loop L. Return true if loop is split.
83 bool runOnLoop(Loop *L, LPPassManager &LPM);
84
85 void getAnalysisUsage(AnalysisUsage &AU) const {
Devang Patelfee76bd2007-08-07 00:25:56 +000086 AU.addPreserved<ScalarEvolution>();
87 AU.addRequiredID(LCSSAID);
88 AU.addPreservedID(LCSSAID);
Devang Patel423c8b22007-08-10 18:07:13 +000089 AU.addRequired<LoopInfo>();
Devang Patelfee76bd2007-08-07 00:25:56 +000090 AU.addPreserved<LoopInfo>();
91 AU.addRequiredID(LoopSimplifyID);
92 AU.addPreservedID(LoopSimplifyID);
Devang Patel9704fcf2007-08-08 22:25:28 +000093 AU.addRequired<DominatorTree>();
Devang Patel5b8ec612007-08-15 03:31:47 +000094 AU.addRequired<DominanceFrontier>();
Devang Patel787a7132007-08-08 21:39:47 +000095 AU.addPreserved<DominatorTree>();
96 AU.addPreserved<DominanceFrontier>();
Devang Patelfee76bd2007-08-07 00:25:56 +000097 }
98
99 private:
Devang Patel38310052008-12-04 21:38:42 +0000100 /// processOneIterationLoop -- Eliminate loop if loop body is executed
101 /// only once. For example,
102 /// for (i = 0; i < N; ++i) {
103 /// if ( i == X) {
104 /// ...
105 /// }
106 /// }
107 ///
108 bool processOneIterationLoop();
Devang Patel71554b82007-08-08 21:02:17 +0000109
Devang Patel38310052008-12-04 21:38:42 +0000110 // -- Routines used by updateLoopIterationSpace();
Devang Patelc9d123d2007-08-09 01:39:01 +0000111
Devang Patel38310052008-12-04 21:38:42 +0000112 /// updateLoopIterationSpace -- Update loop's iteration space if loop
113 /// body is executed for certain IV range only. For example,
114 ///
115 /// for (i = 0; i < N; ++i) {
116 /// if ( i > A && i < B) {
117 /// ...
118 /// }
119 /// }
Devang Patel042b8772008-12-08 17:07:24 +0000120 /// is transformed to iterators from A to B, if A > 0 and B < N.
Devang Patel38310052008-12-04 21:38:42 +0000121 ///
122 bool updateLoopIterationSpace();
Devang Patel71554b82007-08-08 21:02:17 +0000123
Devang Patel38310052008-12-04 21:38:42 +0000124 /// restrictLoopBound - Op dominates loop body. Op compares an IV based value
125 /// with a loop invariant value. Update loop's lower and upper bound based on
126 /// the loop invariant value.
127 bool restrictLoopBound(ICmpInst &Op);
Devang Patel4a69da92007-08-25 00:56:38 +0000128
Devang Patel38310052008-12-04 21:38:42 +0000129 // --- Routines used by splitLoop(). --- /
Devang Patel4a69da92007-08-25 00:56:38 +0000130
Devang Patel38310052008-12-04 21:38:42 +0000131 bool splitLoop();
Devang Patel4a69da92007-08-25 00:56:38 +0000132
Devang Patel38310052008-12-04 21:38:42 +0000133 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by
134 /// DeadBB. This routine is used to remove split condition's dead branch,
135 /// dominated by DeadBB. LiveBB dominates split conidition's other branch.
Devang Patela6a86632007-08-14 18:35:57 +0000136 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB);
Devang Patel38310052008-12-04 21:38:42 +0000137
138 /// moveExitCondition - Move exit condition EC into split condition block.
139 void moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
140 BasicBlock *ExitBB, ICmpInst *EC, ICmpInst *SC,
141 PHINode *IV, Instruction *IVAdd, Loop *LP,
142 unsigned);
143
Devang Pateld79faee2007-08-25 02:39:24 +0000144 /// updatePHINodes - CFG has been changed.
145 /// Before
146 /// - ExitBB's single predecessor was Latch
147 /// - Latch's second successor was Header
148 /// Now
149 /// - ExitBB's single predecessor was Header
150 /// - Latch's one and only successor was Header
151 ///
152 /// Update ExitBB PHINodes' to reflect this change.
153 void updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
154 BasicBlock *Header,
Devang Patelea069062008-02-13 22:23:07 +0000155 PHINode *IV, Instruction *IVIncrement, Loop *LP);
Devang Pateld79faee2007-08-25 02:39:24 +0000156
Devang Patel38310052008-12-04 21:38:42 +0000157 // --- Utility routines --- /
Devang Pateld79faee2007-08-25 02:39:24 +0000158
Devang Patel38310052008-12-04 21:38:42 +0000159 /// cleanBlock - A block is considered clean if all non terminal
160 /// instructions are either PHINodes or IV based values.
161 bool cleanBlock(BasicBlock *BB);
Devang Patelfee76bd2007-08-07 00:25:56 +0000162
Devang Patel042b8772008-12-08 17:07:24 +0000163 /// IVisLT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000164 /// IV based value is less than the loop invariant then return the loop
165 /// invariant. Otherwise return NULL.
166 Value * IVisLT(ICmpInst &Op);
167
Devang Patel042b8772008-12-08 17:07:24 +0000168 /// IVisLE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000169 /// IV based value is less than or equal to the loop invariant then
170 /// return the loop invariant. Otherwise return NULL.
171 Value * IVisLE(ICmpInst &Op);
172
Devang Patel042b8772008-12-08 17:07:24 +0000173 /// IVisGT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000174 /// IV based value is greater than the loop invariant then return the loop
175 /// invariant. Otherwise return NULL.
176 Value * IVisGT(ICmpInst &Op);
177
Devang Patel042b8772008-12-08 17:07:24 +0000178 /// IVisGE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +0000179 /// IV based value is greater than or equal to the loop invariant then
180 /// return the loop invariant. Otherwise return NULL.
181 Value * IVisGE(ICmpInst &Op);
Devang Patelbacf5192007-08-10 00:33:50 +0000182
Devang Patelfee76bd2007-08-07 00:25:56 +0000183 private:
184
Devang Patel38310052008-12-04 21:38:42 +0000185 // Current Loop information.
Devang Patelfee76bd2007-08-07 00:25:56 +0000186 Loop *L;
Devang Patel423c8b22007-08-10 18:07:13 +0000187 LPPassManager *LPM;
188 LoopInfo *LI;
Devang Patel9704fcf2007-08-08 22:25:28 +0000189 DominatorTree *DT;
Devang Patelfc4c5f82007-08-13 22:13:24 +0000190 DominanceFrontier *DF;
Devang Patelbacf5192007-08-10 00:33:50 +0000191
Devang Patelbacf5192007-08-10 00:33:50 +0000192 PHINode *IndVar;
Devang Patelbacf5192007-08-10 00:33:50 +0000193 ICmpInst *ExitCondition;
Devang Patel38310052008-12-04 21:38:42 +0000194 ICmpInst *SplitCondition;
195 Value *IVStartValue;
196 Value *IVExitValue;
197 Instruction *IVIncrement;
198 SmallPtrSet<Value *, 4> IVBasedValues;
Devang Patelfee76bd2007-08-07 00:25:56 +0000199 };
Devang Patelfee76bd2007-08-07 00:25:56 +0000200}
201
Dan Gohman844731a2008-05-13 00:00:25 +0000202char LoopIndexSplit::ID = 0;
203static RegisterPass<LoopIndexSplit>
204X("loop-index-split", "Index Split Loops");
205
Daniel Dunbar394f0442008-10-22 23:32:42 +0000206Pass *llvm::createLoopIndexSplitPass() {
Devang Patelfee76bd2007-08-07 00:25:56 +0000207 return new LoopIndexSplit();
208}
209
210// Index split Loop L. Return true if loop is split.
Devang Patel423c8b22007-08-10 18:07:13 +0000211bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
Devang Patelfee76bd2007-08-07 00:25:56 +0000212 L = IncomingLoop;
Devang Patel423c8b22007-08-10 18:07:13 +0000213 LPM = &LPM_Ref;
Devang Patel71554b82007-08-08 21:02:17 +0000214
Devang Patel3fe4f212007-08-15 02:14:55 +0000215 // FIXME - Nested loops make dominator info updates tricky.
Devang Patel4e8061c2007-08-14 23:53:57 +0000216 if (!L->getSubLoops().empty())
217 return false;
218
Devang Patel9704fcf2007-08-08 22:25:28 +0000219 DT = &getAnalysis<DominatorTree>();
Devang Patel423c8b22007-08-10 18:07:13 +0000220 LI = &getAnalysis<LoopInfo>();
Devang Patel7375bb92007-08-15 03:34:53 +0000221 DF = &getAnalysis<DominanceFrontier>();
Devang Patelfee76bd2007-08-07 00:25:56 +0000222
Devang Patel38310052008-12-04 21:38:42 +0000223 // Initialize loop data.
224 IndVar = L->getCanonicalInductionVariable();
225 if (!IndVar) return false;
Devang Patelbacf5192007-08-10 00:33:50 +0000226
Devang Patel38310052008-12-04 21:38:42 +0000227 bool P1InLoop = L->contains(IndVar->getIncomingBlock(1));
228 IVStartValue = IndVar->getIncomingValue(!P1InLoop);
229 IVIncrement = dyn_cast<Instruction>(IndVar->getIncomingValue(P1InLoop));
230 if (!IVIncrement) return false;
Devang Patel71554b82007-08-08 21:02:17 +0000231
Devang Patel38310052008-12-04 21:38:42 +0000232 IVBasedValues.clear();
233 IVBasedValues.insert(IndVar);
234 IVBasedValues.insert(IVIncrement);
Devang Patelbacf5192007-08-10 00:33:50 +0000235 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
Devang Patel38310052008-12-04 21:38:42 +0000236 I != E; ++I)
237 for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end();
238 BI != BE; ++BI) {
239 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BI))
240 if (BO != IVIncrement
241 && (BO->getOpcode() == Instruction::Add
242 || BO->getOpcode() == Instruction::Sub))
243 if (IVBasedValues.count(BO->getOperand(0))
244 && L->isLoopInvariant(BO->getOperand(1)))
245 IVBasedValues.insert(BO);
246 }
Devang Patelbacf5192007-08-10 00:33:50 +0000247
Devang Patel38310052008-12-04 21:38:42 +0000248 // Reject loop if loop exit condition is not suitable.
Dan Gohmanc8332462009-02-12 18:08:24 +0000249 BasicBlock *ExitingBlock = L->getExitingBlock();
250 if (!ExitingBlock)
Devang Patel38310052008-12-04 21:38:42 +0000251 return false;
Dan Gohmanc8332462009-02-12 18:08:24 +0000252 BranchInst *EBR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
Devang Patel38310052008-12-04 21:38:42 +0000253 if (!EBR) return false;
254 ExitCondition = dyn_cast<ICmpInst>(EBR->getCondition());
255 if (!ExitCondition) return false;
Dan Gohmanc8332462009-02-12 18:08:24 +0000256 if (ExitingBlock != L->getLoopLatch()) return false;
Devang Patel38310052008-12-04 21:38:42 +0000257 IVExitValue = ExitCondition->getOperand(1);
258 if (!L->isLoopInvariant(IVExitValue))
259 IVExitValue = ExitCondition->getOperand(0);
260 if (!L->isLoopInvariant(IVExitValue))
261 return false;
Dan Gohmanf7ca1612009-06-27 22:58:27 +0000262 if (!IVBasedValues.count(
263 ExitCondition->getOperand(IVExitValue == ExitCondition->getOperand(0))))
264 return false;
Devang Patela5e27f82008-07-09 00:12:01 +0000265
266 // If start value is more then exit value where induction variable
267 // increments by 1 then we are potentially dealing with an infinite loop.
268 // Do not index split this loop.
Devang Patel38310052008-12-04 21:38:42 +0000269 if (ConstantInt *SV = dyn_cast<ConstantInt>(IVStartValue))
270 if (ConstantInt *EV = dyn_cast<ConstantInt>(IVExitValue))
271 if (SV->getSExtValue() > EV->getSExtValue())
272 return false;
Devang Patelc9d123d2007-08-09 01:39:01 +0000273
Devang Patel38310052008-12-04 21:38:42 +0000274 if (processOneIterationLoop())
275 return true;
Devang Patelfee76bd2007-08-07 00:25:56 +0000276
Devang Patel38310052008-12-04 21:38:42 +0000277 if (updateLoopIterationSpace())
278 return true;
Devang Patelfee76bd2007-08-07 00:25:56 +0000279
Devang Patel38310052008-12-04 21:38:42 +0000280 if (splitLoop())
281 return true;
Devang Pateld35ed2c2007-09-11 00:42:56 +0000282
283 return false;
Devang Patelfee76bd2007-08-07 00:25:56 +0000284}
285
Devang Patel38310052008-12-04 21:38:42 +0000286// --- Helper routines ---
287// isUsedOutsideLoop - Returns true iff V is used outside the loop L.
288static bool isUsedOutsideLoop(Value *V, Loop *L) {
289 for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
290 if (!L->contains(cast<Instruction>(*UI)->getParent()))
291 return true;
292 return false;
293}
Devang Patelfee76bd2007-08-07 00:25:56 +0000294
Devang Patel38310052008-12-04 21:38:42 +0000295// Return V+1
Owen Anderson1ff50b32009-07-03 00:54:20 +0000296static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt,
Owen Andersone922c022009-07-22 00:24:57 +0000297 LLVMContext &Context) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000298 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
Devang Patel38310052008-12-04 21:38:42 +0000299 return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt);
300}
Devang Patelfee76bd2007-08-07 00:25:56 +0000301
Devang Patel38310052008-12-04 21:38:42 +0000302// Return V-1
Owen Anderson1ff50b32009-07-03 00:54:20 +0000303static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt,
Owen Andersone922c022009-07-22 00:24:57 +0000304 LLVMContext &Context) {
Owen Andersoneed707b2009-07-24 23:12:02 +0000305 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
Devang Patel38310052008-12-04 21:38:42 +0000306 return BinaryOperator::CreateSub(V, One, "lsp", InsertPt);
307}
Devang Patelfee76bd2007-08-07 00:25:56 +0000308
Devang Patel38310052008-12-04 21:38:42 +0000309// Return min(V1, V1)
310static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
311
Owen Anderson333c4002009-07-09 23:48:35 +0000312 Value *C = new ICmpInst(InsertPt,
313 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
314 V1, V2, "lsp");
Devang Patel38310052008-12-04 21:38:42 +0000315 return SelectInst::Create(C, V1, V2, "lsp", InsertPt);
316}
Devang Patelfee76bd2007-08-07 00:25:56 +0000317
Devang Patel38310052008-12-04 21:38:42 +0000318// Return max(V1, V2)
319static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
320
Owen Anderson333c4002009-07-09 23:48:35 +0000321 Value *C = new ICmpInst(InsertPt,
322 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
323 V1, V2, "lsp");
Devang Patel38310052008-12-04 21:38:42 +0000324 return SelectInst::Create(C, V2, V1, "lsp", InsertPt);
325}
Devang Patel968eee22007-09-19 00:15:16 +0000326
Devang Patel38310052008-12-04 21:38:42 +0000327/// processOneIterationLoop -- Eliminate loop if loop body is executed
328/// only once. For example,
329/// for (i = 0; i < N; ++i) {
330/// if ( i == X) {
331/// ...
332/// }
333/// }
334///
335bool LoopIndexSplit::processOneIterationLoop() {
336 SplitCondition = NULL;
Devang Patel84ef08b2007-09-19 00:11:01 +0000337 BasicBlock *Latch = L->getLoopLatch();
Devang Patel38310052008-12-04 21:38:42 +0000338 BasicBlock *Header = L->getHeader();
339 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
340 if (!BR) return false;
341 if (!isa<BranchInst>(Latch->getTerminator())) return false;
342 if (BR->isUnconditional()) return false;
343 SplitCondition = dyn_cast<ICmpInst>(BR->getCondition());
344 if (!SplitCondition) return false;
345 if (SplitCondition == ExitCondition) return false;
346 if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false;
347 if (BR->getOperand(1) != Latch) return false;
348 if (!IVBasedValues.count(SplitCondition->getOperand(0))
349 && !IVBasedValues.count(SplitCondition->getOperand(1)))
Devang Patel84ef08b2007-09-19 00:11:01 +0000350 return false;
351
Devang Patel38310052008-12-04 21:38:42 +0000352 // If IV is used outside the loop then this loop traversal is required.
353 // FIXME: Calculate and use last IV value.
354 if (isUsedOutsideLoop(IVIncrement, L))
355 return false;
356
357 // If BR operands are not IV or not loop invariants then skip this loop.
358 Value *OPV = SplitCondition->getOperand(0);
359 Value *SplitValue = SplitCondition->getOperand(1);
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000360 if (!L->isLoopInvariant(SplitValue))
361 std::swap(OPV, SplitValue);
Devang Patel38310052008-12-04 21:38:42 +0000362 if (!L->isLoopInvariant(SplitValue))
363 return false;
364 Instruction *OPI = dyn_cast<Instruction>(OPV);
Devang Patelb23c2322009-03-30 22:24:10 +0000365 if (!OPI)
366 return false;
Devang Patel38310052008-12-04 21:38:42 +0000367 if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L))
368 return false;
Devang Patelb23c2322009-03-30 22:24:10 +0000369 Value *StartValue = IVStartValue;
370 Value *ExitValue = IVExitValue;;
371
372 if (OPV != IndVar) {
373 // If BR operand is IV based then use this operand to calculate
374 // effective conditions for loop body.
375 BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV);
376 if (!BOPV)
377 return false;
378 if (BOPV->getOpcode() != Instruction::Add)
379 return false;
380 StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR);
381 ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR);
382 }
383
Devang Patel38310052008-12-04 21:38:42 +0000384 if (!cleanBlock(Header))
385 return false;
386
387 if (!cleanBlock(Latch))
388 return false;
389
390 // If the merge point for BR is not loop latch then skip this loop.
391 if (BR->getSuccessor(0) != Latch) {
392 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
393 assert (DF0 != DF->end() && "Unable to find dominance frontier");
394 if (!DF0->second.count(Latch))
395 return false;
396 }
397
398 if (BR->getSuccessor(1) != Latch) {
399 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
400 assert (DF1 != DF->end() && "Unable to find dominance frontier");
401 if (!DF1->second.count(Latch))
402 return false;
403 }
404
405 // Now, Current loop L contains compare instruction
406 // that compares induction variable, IndVar, against loop invariant. And
407 // entire (i.e. meaningful) loop body is dominated by this compare
408 // instruction. In such case eliminate
409 // loop structure surrounding this loop body. For example,
410 // for (int i = start; i < end; ++i) {
411 // if ( i == somevalue) {
412 // loop_body
413 // }
414 // }
415 // can be transformed into
416 // if (somevalue >= start && somevalue < end) {
417 // i = somevalue;
418 // loop_body
419 // }
Devang Patel6a2bfda2007-08-08 01:51:27 +0000420
Devang Patelebc5fea2007-08-20 20:49:01 +0000421 // Replace index variable with split value in loop body. Loop body is executed
422 // only when index variable is equal to split value.
Devang Patel38310052008-12-04 21:38:42 +0000423 IndVar->replaceAllUsesWith(SplitValue);
Devang Patelfee76bd2007-08-07 00:25:56 +0000424
Devang Patelfee76bd2007-08-07 00:25:56 +0000425 // Replace split condition in header.
426 // Transform
427 // SplitCondition : icmp eq i32 IndVar, SplitValue
428 // into
429 // c1 = icmp uge i32 SplitValue, StartValue
Devang Patelba32a5f2007-09-10 23:57:58 +0000430 // c2 = icmp ult i32 SplitValue, ExitValue
Devang Patelfee76bd2007-08-07 00:25:56 +0000431 // and i32 c1, c2
Owen Anderson333c4002009-07-09 23:48:35 +0000432 Instruction *C1 = new ICmpInst(BR, ExitCondition->isSignedPredicate() ?
Devang Patelfee76bd2007-08-07 00:25:56 +0000433 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
Owen Anderson333c4002009-07-09 23:48:35 +0000434 SplitValue, StartValue, "lisplit");
Devang Patel38310052008-12-04 21:38:42 +0000435
436 CmpInst::Predicate C2P = ExitCondition->getPredicate();
437 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
438 if (LatchBR->getOperand(0) != Header)
439 C2P = CmpInst::getInversePredicate(C2P);
Owen Anderson333c4002009-07-09 23:48:35 +0000440 Instruction *C2 = new ICmpInst(BR, C2P, SplitValue, ExitValue, "lisplit");
Devang Patel38310052008-12-04 21:38:42 +0000441 Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR);
442
443 SplitCondition->replaceAllUsesWith(NSplitCond);
444 SplitCondition->eraseFromParent();
Devang Patelfee76bd2007-08-07 00:25:56 +0000445
Devang Patelfc19fbd2008-10-10 22:02:57 +0000446 // Remove Latch to Header edge.
447 BasicBlock *LatchSucc = NULL;
448 Header->removePredecessor(Latch);
449 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
450 SI != E; ++SI) {
451 if (Header != *SI)
452 LatchSucc = *SI;
453 }
Devang Patelfc19fbd2008-10-10 22:02:57 +0000454
Devang Patelb23c2322009-03-30 22:24:10 +0000455 // Clean up latch block.
456 Value *LatchBRCond = LatchBR->getCondition();
457 LatchBR->setUnconditionalDest(LatchSucc);
458 RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond);
Devang Patel38310052008-12-04 21:38:42 +0000459
Devang Patel423c8b22007-08-10 18:07:13 +0000460 LPM->deleteLoopFromQueue(L);
Devang Patel787a7132007-08-08 21:39:47 +0000461
462 // Update Dominator Info.
463 // Only CFG change done is to remove Latch to Header edge. This
464 // does not change dominator tree because Latch did not dominate
465 // Header.
Devang Patelfc4c5f82007-08-13 22:13:24 +0000466 if (DF) {
Devang Patel787a7132007-08-08 21:39:47 +0000467 DominanceFrontier::iterator HeaderDF = DF->find(Header);
468 if (HeaderDF != DF->end())
469 DF->removeFromFrontier(HeaderDF, Header);
470
471 DominanceFrontier::iterator LatchDF = DF->find(Latch);
472 if (LatchDF != DF->end())
473 DF->removeFromFrontier(LatchDF, Header);
474 }
Devang Patel38310052008-12-04 21:38:42 +0000475
476 ++NumIndexSplitRemoved;
Devang Patelfee76bd2007-08-07 00:25:56 +0000477 return true;
478}
479
Devang Patel38310052008-12-04 21:38:42 +0000480/// restrictLoopBound - Op dominates loop body. Op compares an IV based value
481/// with a loop invariant value. Update loop's lower and upper bound based on
482/// the loop invariant value.
483bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) {
484 bool Sign = Op.isSignedPredicate();
485 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
Devang Patelfee76bd2007-08-07 00:25:56 +0000486
Devang Patel38310052008-12-04 21:38:42 +0000487 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
488 BranchInst *EBR =
489 cast<BranchInst>(ExitCondition->getParent()->getTerminator());
490 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
491 BasicBlock *T = EBR->getSuccessor(0);
492 EBR->setSuccessor(0, EBR->getSuccessor(1));
493 EBR->setSuccessor(1, T);
Devang Patelfee76bd2007-08-07 00:25:56 +0000494 }
495
Owen Andersone922c022009-07-22 00:24:57 +0000496 LLVMContext &Context = Op.getContext();
497
Devang Patel38310052008-12-04 21:38:42 +0000498 // New upper and lower bounds.
Devang Patel453a8442007-09-25 17:31:19 +0000499 Value *NLB = NULL;
Devang Patel38310052008-12-04 21:38:42 +0000500 Value *NUB = NULL;
501 if (Value *V = IVisLT(Op)) {
502 // Restrict upper bound.
503 if (IVisLE(*ExitCondition))
Owen Anderson1ff50b32009-07-03 00:54:20 +0000504 V = getMinusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000505 NUB = getMin(V, IVExitValue, Sign, PHTerm);
506 } else if (Value *V = IVisLE(Op)) {
507 // Restrict upper bound.
508 if (IVisLT(*ExitCondition))
Owen Anderson1ff50b32009-07-03 00:54:20 +0000509 V = getPlusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000510 NUB = getMin(V, IVExitValue, Sign, PHTerm);
511 } else if (Value *V = IVisGT(Op)) {
512 // Restrict lower bound.
Owen Anderson1ff50b32009-07-03 00:54:20 +0000513 V = getPlusOne(V, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000514 NLB = getMax(V, IVStartValue, Sign, PHTerm);
515 } else if (Value *V = IVisGE(Op))
516 // Restrict lower bound.
517 NLB = getMax(V, IVStartValue, Sign, PHTerm);
Devang Patel453a8442007-09-25 17:31:19 +0000518
Devang Patel38310052008-12-04 21:38:42 +0000519 if (!NLB && !NUB)
520 return false;
Devang Patel453a8442007-09-25 17:31:19 +0000521
522 if (NLB) {
Devang Patel38310052008-12-04 21:38:42 +0000523 unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader());
Devang Patel453a8442007-09-25 17:31:19 +0000524 IndVar->setIncomingValue(i, NLB);
525 }
526
527 if (NUB) {
Devang Patel38310052008-12-04 21:38:42 +0000528 unsigned i = (ExitCondition->getOperand(0) != IVExitValue);
529 ExitCondition->setOperand(i, NUB);
Devang Patel453a8442007-09-25 17:31:19 +0000530 }
Devang Patel38310052008-12-04 21:38:42 +0000531 return true;
Devang Patel5279d062007-09-17 20:39:48 +0000532}
Devang Patel38310052008-12-04 21:38:42 +0000533
534/// updateLoopIterationSpace -- Update loop's iteration space if loop
535/// body is executed for certain IV range only. For example,
536///
537/// for (i = 0; i < N; ++i) {
538/// if ( i > A && i < B) {
539/// ...
540/// }
541/// }
Devang Patel042b8772008-12-08 17:07:24 +0000542/// is transformed to iterators from A to B, if A > 0 and B < N.
Devang Patel38310052008-12-04 21:38:42 +0000543///
544bool LoopIndexSplit::updateLoopIterationSpace() {
545 SplitCondition = NULL;
546 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
547 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
548 return false;
549 BasicBlock *Latch = L->getLoopLatch();
Devang Patel5279d062007-09-17 20:39:48 +0000550 BasicBlock *Header = L->getHeader();
Devang Patel38310052008-12-04 21:38:42 +0000551 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
552 if (!BR) return false;
553 if (!isa<BranchInst>(Latch->getTerminator())) return false;
554 if (BR->isUnconditional()) return false;
555 BinaryOperator *AND = dyn_cast<BinaryOperator>(BR->getCondition());
556 if (!AND) return false;
557 if (AND->getOpcode() != Instruction::And) return false;
558 ICmpInst *Op0 = dyn_cast<ICmpInst>(AND->getOperand(0));
559 ICmpInst *Op1 = dyn_cast<ICmpInst>(AND->getOperand(1));
560 if (!Op0 || !Op1)
561 return false;
562 IVBasedValues.insert(AND);
563 IVBasedValues.insert(Op0);
564 IVBasedValues.insert(Op1);
565 if (!cleanBlock(Header)) return false;
Devang Patel453a8442007-09-25 17:31:19 +0000566 BasicBlock *ExitingBlock = ExitCondition->getParent();
Devang Patel38310052008-12-04 21:38:42 +0000567 if (!cleanBlock(ExitingBlock)) return false;
Devang Patel453a8442007-09-25 17:31:19 +0000568
Devang Patelcf42ee42009-03-02 23:39:14 +0000569 // If the merge point for BR is not loop latch then skip this loop.
570 if (BR->getSuccessor(0) != Latch) {
571 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
572 assert (DF0 != DF->end() && "Unable to find dominance frontier");
573 if (!DF0->second.count(Latch))
574 return false;
575 }
576
577 if (BR->getSuccessor(1) != Latch) {
578 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
579 assert (DF1 != DF->end() && "Unable to find dominance frontier");
580 if (!DF1->second.count(Latch))
581 return false;
582 }
583
Devang Patel38310052008-12-04 21:38:42 +0000584 // Verify that loop exiting block has only two predecessor, where one pred
Devang Patel453a8442007-09-25 17:31:19 +0000585 // is split condition block. The other predecessor will become exiting block's
586 // dominator after CFG is updated. TODO : Handle CFG's where exiting block has
587 // more then two predecessors. This requires extra work in updating dominator
588 // information.
589 BasicBlock *ExitingBBPred = NULL;
590 for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock);
591 PI != PE; ++PI) {
592 BasicBlock *BB = *PI;
Devang Patel38310052008-12-04 21:38:42 +0000593 if (Header == BB)
Devang Patel453a8442007-09-25 17:31:19 +0000594 continue;
595 if (ExitingBBPred)
596 return false;
597 else
598 ExitingBBPred = BB;
599 }
Devang Patel453a8442007-09-25 17:31:19 +0000600
Devang Patel38310052008-12-04 21:38:42 +0000601 if (!restrictLoopBound(*Op0))
602 return false;
Devang Patel453a8442007-09-25 17:31:19 +0000603
Devang Patel38310052008-12-04 21:38:42 +0000604 if (!restrictLoopBound(*Op1))
605 return false;
606
607 // Update CFG.
608 if (BR->getSuccessor(0) == ExitingBlock)
609 BR->setUnconditionalDest(BR->getSuccessor(1));
Devang Patel453a8442007-09-25 17:31:19 +0000610 else
Devang Patel38310052008-12-04 21:38:42 +0000611 BR->setUnconditionalDest(BR->getSuccessor(0));
Devang Patel453a8442007-09-25 17:31:19 +0000612
Devang Patel38310052008-12-04 21:38:42 +0000613 AND->eraseFromParent();
Dan Gohmana8c763b2008-08-14 18:13:49 +0000614 if (Op0->use_empty())
Devang Patel453a8442007-09-25 17:31:19 +0000615 Op0->eraseFromParent();
Dan Gohmana8c763b2008-08-14 18:13:49 +0000616 if (Op1->use_empty())
Devang Patel453a8442007-09-25 17:31:19 +0000617 Op1->eraseFromParent();
Devang Patel453a8442007-09-25 17:31:19 +0000618
619 // Update domiantor info. Now, ExitingBlock has only one predecessor,
620 // ExitingBBPred, and it is ExitingBlock's immediate domiantor.
621 DT->changeImmediateDominator(ExitingBlock, ExitingBBPred);
Devang Patel38310052008-12-04 21:38:42 +0000622
623 BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1);
624 if (L->contains(ExitBlock))
625 ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0);
626
627 // If ExitingBlock is a member of the loop basic blocks' DF list then
628 // replace ExitingBlock with header and exit block in the DF list
629 DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock);
Devang Patel453a8442007-09-25 17:31:19 +0000630 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
631 I != E; ++I) {
632 BasicBlock *BB = *I;
633 if (BB == Header || BB == ExitingBlock)
634 continue;
635 DominanceFrontier::iterator BBDF = DF->find(BB);
636 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
637 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
638 while (DomSetI != DomSetE) {
639 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
640 ++DomSetI;
641 BasicBlock *DFBB = *CurrentItr;
642 if (DFBB == ExitingBlock) {
643 BBDF->second.erase(DFBB);
Devang Patel38310052008-12-04 21:38:42 +0000644 for (DominanceFrontier::DomSetType::iterator
645 EBI = ExitingBlockDF->second.begin(),
646 EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI)
647 BBDF->second.insert(*EBI);
Devang Patel453a8442007-09-25 17:31:19 +0000648 }
649 }
650 }
Devang Patel38310052008-12-04 21:38:42 +0000651 NumRestrictBounds++;
Devang Patel1c013502007-09-25 17:43:08 +0000652 return true;
Devang Patel5279d062007-09-17 20:39:48 +0000653}
654
Devang Patela6a86632007-08-14 18:35:57 +0000655/// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
656/// This routine is used to remove split condition's dead branch, dominated by
657/// DeadBB. LiveBB dominates split conidition's other branch.
658void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
659 BasicBlock *LiveBB) {
Devang Patel98147a32007-08-12 07:02:51 +0000660
Devang Patel5b8ec612007-08-15 03:31:47 +0000661 // First update DeadBB's dominance frontier.
Devang Patel96bf5242007-08-17 21:59:16 +0000662 SmallVector<BasicBlock *, 8> FrontierBBs;
Devang Patel5b8ec612007-08-15 03:31:47 +0000663 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
664 if (DeadBBDF != DF->end()) {
665 SmallVector<BasicBlock *, 8> PredBlocks;
666
667 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
668 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
Devang Patel38310052008-12-04 21:38:42 +0000669 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI)
670 {
Devang Patel5b8ec612007-08-15 03:31:47 +0000671 BasicBlock *FrontierBB = *DeadBBSetI;
Devang Patel96bf5242007-08-17 21:59:16 +0000672 FrontierBBs.push_back(FrontierBB);
673
Devang Patel5b8ec612007-08-15 03:31:47 +0000674 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
675 PredBlocks.clear();
676 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
677 PI != PE; ++PI) {
678 BasicBlock *P = *PI;
679 if (P == DeadBB || DT->dominates(DeadBB, P))
680 PredBlocks.push_back(P);
Devang Patelfc4c5f82007-08-13 22:13:24 +0000681 }
Devang Patel96bf5242007-08-17 21:59:16 +0000682
Devang Patel5b8ec612007-08-15 03:31:47 +0000683 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
684 FBI != FBE; ++FBI) {
685 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
686 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
687 PE = PredBlocks.end(); PI != PE; ++PI) {
688 BasicBlock *P = *PI;
689 PN->removeIncomingValue(P);
690 }
691 }
692 else
693 break;
Devang Patel96bf5242007-08-17 21:59:16 +0000694 }
Devang Patel98147a32007-08-12 07:02:51 +0000695 }
Devang Patel98147a32007-08-12 07:02:51 +0000696 }
Devang Patel5b8ec612007-08-15 03:31:47 +0000697
698 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
699 SmallVector<BasicBlock *, 32> WorkList;
700 DomTreeNode *DN = DT->getNode(DeadBB);
701 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
702 E = df_end(DN); DI != E; ++DI) {
703 BasicBlock *BB = DI->getBlock();
704 WorkList.push_back(BB);
Devang Patel96bf5242007-08-17 21:59:16 +0000705 BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy));
Devang Patel5b8ec612007-08-15 03:31:47 +0000706 }
707
708 while (!WorkList.empty()) {
709 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
Devang Patel575ec802009-03-25 23:57:48 +0000710 LPM->deleteSimpleAnalysisValue(BB, LP);
Devang Patel5b8ec612007-08-15 03:31:47 +0000711 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
Devang Pateld15dd8c2007-09-20 23:01:50 +0000712 BBI != BBE; ) {
Devang Patel5b8ec612007-08-15 03:31:47 +0000713 Instruction *I = BBI;
Devang Pateld15dd8c2007-09-20 23:01:50 +0000714 ++BBI;
Devang Patel5b8ec612007-08-15 03:31:47 +0000715 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Owen Anderson0b2a1532009-04-14 01:04:19 +0000716 LPM->deleteSimpleAnalysisValue(I, LP);
Devang Patel5b8ec612007-08-15 03:31:47 +0000717 I->eraseFromParent();
718 }
Devang Patel5b8ec612007-08-15 03:31:47 +0000719 DT->eraseNode(BB);
720 DF->removeBlock(BB);
721 LI->removeBlock(BB);
722 BB->eraseFromParent();
723 }
Devang Patel96bf5242007-08-17 21:59:16 +0000724
725 // Update Frontier BBs' dominator info.
726 while (!FrontierBBs.empty()) {
727 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
728 BasicBlock *NewDominator = FBB->getSinglePredecessor();
729 if (!NewDominator) {
730 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
731 NewDominator = *PI;
732 ++PI;
733 if (NewDominator != LiveBB) {
734 for(; PI != PE; ++PI) {
735 BasicBlock *P = *PI;
736 if (P == LiveBB) {
737 NewDominator = LiveBB;
738 break;
739 }
740 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
741 }
742 }
743 }
744 assert (NewDominator && "Unable to fix dominator info.");
745 DT->changeImmediateDominator(FBB, NewDominator);
746 DF->changeImmediateDominator(FBB, NewDominator, DT);
747 }
748
Devang Patel98147a32007-08-12 07:02:51 +0000749}
750
Devang Pateld79faee2007-08-25 02:39:24 +0000751// moveExitCondition - Move exit condition EC into split condition block CondBB.
752void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
Devang Patel38310052008-12-04 21:38:42 +0000753 BasicBlock *ExitBB, ICmpInst *EC,
754 ICmpInst *SC, PHINode *IV,
755 Instruction *IVAdd, Loop *LP,
756 unsigned ExitValueNum) {
Devang Pateld79faee2007-08-25 02:39:24 +0000757
758 BasicBlock *ExitingBB = EC->getParent();
759 Instruction *CurrentBR = CondBB->getTerminator();
760
761 // Move exit condition into split condition block.
762 EC->moveBefore(CurrentBR);
763 EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV);
764
765 // Move exiting block's branch into split condition block. Update its branch
766 // destination.
767 BranchInst *ExitingBR = cast<BranchInst>(ExitingBB->getTerminator());
768 ExitingBR->moveBefore(CurrentBR);
Devang Patel23067df2008-02-13 22:06:36 +0000769 BasicBlock *OrigDestBB = NULL;
770 if (ExitingBR->getSuccessor(0) == ExitBB) {
771 OrigDestBB = ExitingBR->getSuccessor(1);
Devang Pateld79faee2007-08-25 02:39:24 +0000772 ExitingBR->setSuccessor(1, ActiveBB);
Devang Patel23067df2008-02-13 22:06:36 +0000773 }
774 else {
775 OrigDestBB = ExitingBR->getSuccessor(0);
Devang Pateld79faee2007-08-25 02:39:24 +0000776 ExitingBR->setSuccessor(0, ActiveBB);
Devang Patel23067df2008-02-13 22:06:36 +0000777 }
Devang Pateld79faee2007-08-25 02:39:24 +0000778
779 // Remove split condition and current split condition branch.
780 SC->eraseFromParent();
781 CurrentBR->eraseFromParent();
782
Devang Patel23067df2008-02-13 22:06:36 +0000783 // Connect exiting block to original destination.
Gabor Greif051a9502008-04-06 20:25:17 +0000784 BranchInst::Create(OrigDestBB, ExitingBB);
Devang Pateld79faee2007-08-25 02:39:24 +0000785
786 // Update PHINodes
Devang Patelea069062008-02-13 22:23:07 +0000787 updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP);
Devang Pateld79faee2007-08-25 02:39:24 +0000788
789 // Fix dominator info.
790 // ExitBB is now dominated by CondBB
791 DT->changeImmediateDominator(ExitBB, CondBB);
792 DF->changeImmediateDominator(ExitBB, CondBB, DT);
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000793
794 // Blocks outside the loop may have been in the dominance frontier of blocks
795 // inside the condition; this is now impossible because the blocks inside the
796 // condition no loger dominate the exit. Remove the relevant blocks from
797 // the dominance frontiers.
798 for (Loop::block_iterator I = LP->block_begin(), E = LP->block_end();
799 I != E; ++I) {
800 if (*I == CondBB || !DT->dominates(CondBB, *I)) continue;
801 DominanceFrontier::iterator BBDF = DF->find(*I);
Devang Pateld79faee2007-08-25 02:39:24 +0000802 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
803 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
804 while (DomSetI != DomSetE) {
805 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
806 ++DomSetI;
807 BasicBlock *DFBB = *CurrentItr;
Eli Friedmanf7cca7b2009-05-22 03:22:46 +0000808 if (!LP->contains(DFBB))
Devang Pateld79faee2007-08-25 02:39:24 +0000809 BBDF->second.erase(DFBB);
Devang Pateld79faee2007-08-25 02:39:24 +0000810 }
811 }
812}
813
814/// updatePHINodes - CFG has been changed.
815/// Before
816/// - ExitBB's single predecessor was Latch
817/// - Latch's second successor was Header
818/// Now
Devang Patel82ada542008-02-08 22:49:13 +0000819/// - ExitBB's single predecessor is Header
820/// - Latch's one and only successor is Header
Devang Pateld79faee2007-08-25 02:39:24 +0000821///
822/// Update ExitBB PHINodes' to reflect this change.
823void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
824 BasicBlock *Header,
Devang Patelea069062008-02-13 22:23:07 +0000825 PHINode *IV, Instruction *IVIncrement,
826 Loop *LP) {
Devang Pateld79faee2007-08-25 02:39:24 +0000827
828 for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end();
Devang Patel4a3c0ac2008-03-27 17:32:46 +0000829 BI != BE; ) {
Devang Pateld79faee2007-08-25 02:39:24 +0000830 PHINode *PN = dyn_cast<PHINode>(BI);
Devang Patel4a3c0ac2008-03-27 17:32:46 +0000831 ++BI;
Devang Pateld79faee2007-08-25 02:39:24 +0000832 if (!PN)
833 break;
834
835 Value *V = PN->getIncomingValueForBlock(Latch);
836 if (PHINode *PHV = dyn_cast<PHINode>(V)) {
Devang Patel82ada542008-02-08 22:49:13 +0000837 // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use
838 // in Header which is new incoming value for PN.
Devang Pateld79faee2007-08-25 02:39:24 +0000839 Value *NewV = NULL;
840 for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end();
Devang Patel82ada542008-02-08 22:49:13 +0000841 UI != E; ++UI)
842 if (PHINode *U = dyn_cast<PHINode>(*UI))
Devang Patelea069062008-02-13 22:23:07 +0000843 if (LP->contains(U->getParent())) {
Devang Patel82ada542008-02-08 22:49:13 +0000844 NewV = U;
845 break;
846 }
847
Devang Patel60a12902008-03-24 20:16:14 +0000848 // Add incoming value from header only if PN has any use inside the loop.
849 if (NewV)
850 PN->addIncoming(NewV, Header);
Devang Pateld79faee2007-08-25 02:39:24 +0000851
852 } else if (Instruction *PHI = dyn_cast<Instruction>(V)) {
853 // If this instruction is IVIncrement then IV is new incoming value
854 // from header otherwise this instruction must be incoming value from
855 // header because loop is in LCSSA form.
856 if (PHI == IVIncrement)
857 PN->addIncoming(IV, Header);
858 else
859 PN->addIncoming(V, Header);
860 } else
861 // Otherwise this is an incoming value from header because loop is in
862 // LCSSA form.
863 PN->addIncoming(V, Header);
864
865 // Remove incoming value from Latch.
866 PN->removeIncomingValue(Latch);
867 }
868}
Devang Patel38310052008-12-04 21:38:42 +0000869
870bool LoopIndexSplit::splitLoop() {
871 SplitCondition = NULL;
872 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
873 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
874 return false;
875 BasicBlock *Header = L->getHeader();
876 BasicBlock *Latch = L->getLoopLatch();
877 BranchInst *SBR = NULL; // Split Condition Branch
878 BranchInst *EBR = cast<BranchInst>(ExitCondition->getParent()->getTerminator());
879 // If Exiting block includes loop variant instructions then this
880 // loop may not be split safely.
881 BasicBlock *ExitingBlock = ExitCondition->getParent();
882 if (!cleanBlock(ExitingBlock)) return false;
883
Owen Andersone922c022009-07-22 00:24:57 +0000884 LLVMContext &Context = Header->getContext();
885
Devang Patel38310052008-12-04 21:38:42 +0000886 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
887 I != E; ++I) {
888 BranchInst *BR = dyn_cast<BranchInst>((*I)->getTerminator());
889 if (!BR || BR->isUnconditional()) continue;
890 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
891 if (!CI || CI == ExitCondition
892 || CI->getPredicate() == ICmpInst::ICMP_NE
893 || CI->getPredicate() == ICmpInst::ICMP_EQ)
894 continue;
895
896 // Unable to handle triangle loops at the moment.
897 // In triangle loop, split condition is in header and one of the
898 // the split destination is loop latch. If split condition is EQ
899 // then such loops are already handle in processOneIterationLoop().
900 if (Header == (*I)
901 && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1)))
902 continue;
903
904 // If the block does not dominate the latch then this is not a diamond.
905 // Such loop may not benefit from index split.
906 if (!DT->dominates((*I), Latch))
907 continue;
908
909 // If split condition branches heads do not have single predecessor,
910 // SplitCondBlock, then is not possible to remove inactive branch.
911 if (!BR->getSuccessor(0)->getSinglePredecessor()
912 || !BR->getSuccessor(1)->getSinglePredecessor())
913 return false;
914
915 // If the merge point for BR is not loop latch then skip this condition.
916 if (BR->getSuccessor(0) != Latch) {
917 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
918 assert (DF0 != DF->end() && "Unable to find dominance frontier");
919 if (!DF0->second.count(Latch))
920 continue;
921 }
922
923 if (BR->getSuccessor(1) != Latch) {
924 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
925 assert (DF1 != DF->end() && "Unable to find dominance frontier");
926 if (!DF1->second.count(Latch))
927 continue;
928 }
929 SplitCondition = CI;
930 SBR = BR;
931 break;
932 }
933
934 if (!SplitCondition)
935 return false;
936
937 // If the predicate sign does not match then skip.
938 if (ExitCondition->isSignedPredicate() != SplitCondition->isSignedPredicate())
939 return false;
940
941 unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue);
942 unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0));
943 Value *SplitValue = SplitCondition->getOperand(SVOpNum);
944 if (!L->isLoopInvariant(SplitValue))
945 return false;
946 if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum)))
947 return false;
948
949 // Normalize loop conditions so that it is easier to calculate new loop
950 // bounds.
951 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
952 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
953 BasicBlock *T = EBR->getSuccessor(0);
954 EBR->setSuccessor(0, EBR->getSuccessor(1));
955 EBR->setSuccessor(1, T);
956 }
957
958 if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) {
959 SplitCondition->setPredicate(SplitCondition->getInversePredicate());
960 BasicBlock *T = SBR->getSuccessor(0);
961 SBR->setSuccessor(0, SBR->getSuccessor(1));
962 SBR->setSuccessor(1, T);
963 }
964
965 //[*] Calculate new loop bounds.
966 Value *AEV = SplitValue;
967 Value *BSV = SplitValue;
968 bool Sign = SplitCondition->isSignedPredicate();
969 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
970
971 if (IVisLT(*ExitCondition)) {
972 if (IVisLT(*SplitCondition)) {
973 /* Do nothing */
974 }
975 else if (IVisLE(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000976 AEV = getPlusOne(SplitValue, Sign, PHTerm, Context);
977 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000978 } else {
979 assert (0 && "Unexpected split condition!");
980 }
981 }
982 else if (IVisLE(*ExitCondition)) {
983 if (IVisLT(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000984 AEV = getMinusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000985 }
986 else if (IVisLE(*SplitCondition)) {
Owen Anderson1ff50b32009-07-03 00:54:20 +0000987 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
Devang Patel38310052008-12-04 21:38:42 +0000988 } else {
989 assert (0 && "Unexpected split condition!");
990 }
991 } else {
992 assert (0 && "Unexpected exit condition!");
993 }
994 AEV = getMin(AEV, IVExitValue, Sign, PHTerm);
995 BSV = getMax(BSV, IVStartValue, Sign, PHTerm);
996
997 // [*] Clone Loop
998 DenseMap<const Value *, Value *> ValueMap;
999 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this);
1000 Loop *ALoop = L;
1001
1002 // [*] ALoop's exiting edge enters BLoop's header.
1003 // ALoop's original exit block becomes BLoop's exit block.
1004 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]);
1005 BasicBlock *A_ExitingBlock = ExitCondition->getParent();
1006 BranchInst *A_ExitInsn =
1007 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator());
1008 assert (A_ExitInsn && "Unable to find suitable loop exit branch");
1009 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1);
1010 BasicBlock *B_Header = BLoop->getHeader();
1011 if (ALoop->contains(B_ExitBlock)) {
1012 B_ExitBlock = A_ExitInsn->getSuccessor(0);
1013 A_ExitInsn->setSuccessor(0, B_Header);
1014 } else
1015 A_ExitInsn->setSuccessor(1, B_Header);
1016
1017 // [*] Update ALoop's exit value using new exit value.
1018 ExitCondition->setOperand(EVOpNum, AEV);
1019
1020 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from
1021 // original loop's preheader. Add incoming PHINode values from
1022 // ALoop's exiting block. Update BLoop header's domiantor info.
1023
1024 // Collect inverse map of Header PHINodes.
1025 DenseMap<Value *, Value *> InverseMap;
1026 for (BasicBlock::iterator BI = ALoop->getHeader()->begin(),
1027 BE = ALoop->getHeader()->end(); BI != BE; ++BI) {
1028 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1029 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
1030 InverseMap[PNClone] = PN;
1031 } else
1032 break;
1033 }
1034
1035 BasicBlock *A_Preheader = ALoop->getLoopPreheader();
1036 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1037 BI != BE; ++BI) {
1038 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1039 // Remove incoming value from original preheader.
1040 PN->removeIncomingValue(A_Preheader);
1041
1042 // Add incoming value from A_ExitingBlock.
1043 if (PN == B_IndVar)
1044 PN->addIncoming(BSV, A_ExitingBlock);
1045 else {
1046 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
1047 Value *V2 = NULL;
1048 // If loop header is also loop exiting block then
1049 // OrigPN is incoming value for B loop header.
1050 if (A_ExitingBlock == ALoop->getHeader())
1051 V2 = OrigPN;
1052 else
1053 V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
1054 PN->addIncoming(V2, A_ExitingBlock);
1055 }
1056 } else
1057 break;
1058 }
1059
1060 DT->changeImmediateDominator(B_Header, A_ExitingBlock);
1061 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT);
1062
1063 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit
1064 // block. Remove incoming PHINode values from ALoop's exiting block.
1065 // Add new incoming values from BLoop's incoming exiting value.
1066 // Update BLoop exit block's dominator info..
1067 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]);
1068 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end();
1069 BI != BE; ++BI) {
1070 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1071 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)],
1072 B_ExitingBlock);
1073 PN->removeIncomingValue(A_ExitingBlock);
1074 } else
1075 break;
1076 }
1077
1078 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock);
1079 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT);
1080
Dan Gohmanf159ccd2009-04-29 22:01:05 +00001081 //[*] Split ALoop's exit edge. This creates a new block which
Devang Patel38310052008-12-04 21:38:42 +00001082 // serves two purposes. First one is to hold PHINode defnitions
1083 // to ensure that ALoop's LCSSA form. Second use it to act
1084 // as a preheader for BLoop.
1085 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this);
1086
1087 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes
1088 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop.
1089 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1090 BI != BE; ++BI) {
1091 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1092 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
1093 PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName());
1094 newPHI->addIncoming(V1, A_ExitingBlock);
1095 A_ExitBlock->getInstList().push_front(newPHI);
1096 PN->removeIncomingValue(A_ExitBlock);
1097 PN->addIncoming(newPHI, A_ExitBlock);
1098 } else
1099 break;
1100 }
1101
1102 //[*] Eliminate split condition's inactive branch from ALoop.
1103 BasicBlock *A_SplitCondBlock = SplitCondition->getParent();
1104 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator());
1105 BasicBlock *A_InactiveBranch = NULL;
1106 BasicBlock *A_ActiveBranch = NULL;
1107 A_ActiveBranch = A_BR->getSuccessor(0);
1108 A_InactiveBranch = A_BR->getSuccessor(1);
1109 A_BR->setUnconditionalDest(A_ActiveBranch);
1110 removeBlocks(A_InactiveBranch, L, A_ActiveBranch);
1111
1112 //[*] Eliminate split condition's inactive branch in from BLoop.
1113 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]);
1114 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator());
1115 BasicBlock *B_InactiveBranch = NULL;
1116 BasicBlock *B_ActiveBranch = NULL;
1117 B_ActiveBranch = B_BR->getSuccessor(1);
1118 B_InactiveBranch = B_BR->getSuccessor(0);
1119 B_BR->setUnconditionalDest(B_ActiveBranch);
1120 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch);
1121
1122 BasicBlock *A_Header = ALoop->getHeader();
1123 if (A_ExitingBlock == A_Header)
1124 return true;
1125
1126 //[*] Move exit condition into split condition block to avoid
1127 // executing dead loop iteration.
1128 ICmpInst *B_ExitCondition = cast<ICmpInst>(ValueMap[ExitCondition]);
1129 Instruction *B_IndVarIncrement = cast<Instruction>(ValueMap[IVIncrement]);
1130 ICmpInst *B_SplitCondition = cast<ICmpInst>(ValueMap[SplitCondition]);
1131
1132 moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition,
1133 cast<ICmpInst>(SplitCondition), IndVar, IVIncrement,
1134 ALoop, EVOpNum);
1135
1136 moveExitCondition(B_SplitCondBlock, B_ActiveBranch,
1137 B_ExitBlock, B_ExitCondition,
1138 B_SplitCondition, B_IndVar, B_IndVarIncrement,
1139 BLoop, EVOpNum);
1140
1141 NumIndexSplit++;
1142 return true;
1143}
1144
1145/// cleanBlock - A block is considered clean if all non terminal instructions
1146/// are either, PHINodes, IV based.
1147bool LoopIndexSplit::cleanBlock(BasicBlock *BB) {
1148 Instruction *Terminator = BB->getTerminator();
1149 for(BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1150 BI != BE; ++BI) {
1151 Instruction *I = BI;
1152
1153 if (isa<PHINode>(I) || I == Terminator || I == ExitCondition
Devang Pateld96c60d2009-02-06 06:19:06 +00001154 || I == SplitCondition || IVBasedValues.count(I)
1155 || isa<DbgInfoIntrinsic>(I))
Devang Patel38310052008-12-04 21:38:42 +00001156 continue;
1157
Duncan Sands7af1c782009-05-06 06:49:50 +00001158 if (I->mayHaveSideEffects())
Devang Patel38310052008-12-04 21:38:42 +00001159 return false;
1160
1161 // I is used only inside this block then it is OK.
1162 bool usedOutsideBB = false;
1163 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
1164 UI != UE; ++UI) {
1165 Instruction *U = cast<Instruction>(UI);
1166 if (U->getParent() != BB)
1167 usedOutsideBB = true;
1168 }
1169 if (!usedOutsideBB)
1170 continue;
1171
1172 // Otherwise we have a instruction that may not allow loop spliting.
1173 return false;
1174 }
1175 return true;
1176}
1177
Devang Patel042b8772008-12-08 17:07:24 +00001178/// IVisLT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001179/// IV based value is less than the loop invariant then return the loop
1180/// invariant. Otherwise return NULL.
1181Value * LoopIndexSplit::IVisLT(ICmpInst &Op) {
1182 ICmpInst::Predicate P = Op.getPredicate();
1183 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1184 && IVBasedValues.count(Op.getOperand(0))
1185 && L->isLoopInvariant(Op.getOperand(1)))
1186 return Op.getOperand(1);
1187
1188 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1189 && IVBasedValues.count(Op.getOperand(1))
1190 && L->isLoopInvariant(Op.getOperand(0)))
1191 return Op.getOperand(0);
1192
1193 return NULL;
1194}
1195
Devang Patel042b8772008-12-08 17:07:24 +00001196/// IVisLE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001197/// IV based value is less than or equal to the loop invariant then
1198/// return the loop invariant. Otherwise return NULL.
1199Value * LoopIndexSplit::IVisLE(ICmpInst &Op) {
1200 ICmpInst::Predicate P = Op.getPredicate();
1201 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1202 && IVBasedValues.count(Op.getOperand(0))
1203 && L->isLoopInvariant(Op.getOperand(1)))
1204 return Op.getOperand(1);
1205
1206 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1207 && IVBasedValues.count(Op.getOperand(1))
1208 && L->isLoopInvariant(Op.getOperand(0)))
1209 return Op.getOperand(0);
1210
1211 return NULL;
1212}
1213
Devang Patel042b8772008-12-08 17:07:24 +00001214/// IVisGT - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001215/// IV based value is greater than the loop invariant then return the loop
1216/// invariant. Otherwise return NULL.
1217Value * LoopIndexSplit::IVisGT(ICmpInst &Op) {
1218 ICmpInst::Predicate P = Op.getPredicate();
1219 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1220 && IVBasedValues.count(Op.getOperand(0))
1221 && L->isLoopInvariant(Op.getOperand(1)))
1222 return Op.getOperand(1);
1223
1224 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1225 && IVBasedValues.count(Op.getOperand(1))
1226 && L->isLoopInvariant(Op.getOperand(0)))
1227 return Op.getOperand(0);
1228
1229 return NULL;
1230}
1231
Devang Patel042b8772008-12-08 17:07:24 +00001232/// IVisGE - If Op is comparing IV based value with an loop invariant and
Devang Patel38310052008-12-04 21:38:42 +00001233/// IV based value is greater than or equal to the loop invariant then
1234/// return the loop invariant. Otherwise return NULL.
1235Value * LoopIndexSplit::IVisGE(ICmpInst &Op) {
1236 ICmpInst::Predicate P = Op.getPredicate();
1237 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1238 && IVBasedValues.count(Op.getOperand(0))
1239 && L->isLoopInvariant(Op.getOperand(1)))
1240 return Op.getOperand(1);
1241
1242 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1243 && IVBasedValues.count(Op.getOperand(1))
1244 && L->isLoopInvariant(Op.getOperand(0)))
1245 return Op.getOperand(0);
1246
1247 return NULL;
1248}
1249