pass machinemoduleinfo down into getSymbolForDwarfGlobalReference,
[llvm/avr.git] / lib / Transforms / Scalar / LoopIndexSplit.cpp
blob5f9d3703da99d610a73d568b5267b79c35e3d830
1 //===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements Loop Index Splitting Pass. This pass handles three
11 // kinds of loops.
13 // [1] A loop may be eliminated if the body is executed exactly once.
14 // For example,
16 // for (i = 0; i < N; ++i) {
17 // if (i == X) {
18 // body;
19 // }
20 // }
22 // is transformed to
24 // i = X;
25 // body;
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,
30 // for (i = 0; i < N; ++i) {
31 // if (i > A && i < B) {
32 // ...
33 // }
34 // }
36 // is transformed to iterators from A to B, if A > 0 and B < N.
38 // [3] A loop may be split if the loop body is dominated by a branch.
39 // For example,
41 // for (i = LB; i < UB; ++i) { if (i < SV) A; else B; }
43 // is transformed into
45 // 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;
51 //===----------------------------------------------------------------------===//
53 #define DEBUG_TYPE "loop-index-split"
54 #include "llvm/Transforms/Scalar.h"
55 #include "llvm/IntrinsicInst.h"
56 #include "llvm/LLVMContext.h"
57 #include "llvm/Analysis/LoopPass.h"
58 #include "llvm/Analysis/ScalarEvolution.h"
59 #include "llvm/Analysis/Dominators.h"
60 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
61 #include "llvm/Transforms/Utils/Cloning.h"
62 #include "llvm/Transforms/Utils/Local.h"
63 #include "llvm/ADT/DepthFirstIterator.h"
64 #include "llvm/ADT/Statistic.h"
66 using namespace llvm;
68 STATISTIC(NumIndexSplit, "Number of loop index split");
69 STATISTIC(NumIndexSplitRemoved, "Number of loops eliminated by loop index split");
70 STATISTIC(NumRestrictBounds, "Number of loop iteration space restricted");
72 namespace {
74 class LoopIndexSplit : public LoopPass {
75 public:
76 static char ID; // Pass ID, replacement for typeid
77 LoopIndexSplit() : LoopPass(&ID) {}
79 // Index split Loop L. Return true if loop is split.
80 bool runOnLoop(Loop *L, LPPassManager &LPM);
82 void getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.addPreserved<ScalarEvolution>();
84 AU.addRequiredID(LCSSAID);
85 AU.addPreservedID(LCSSAID);
86 AU.addRequired<LoopInfo>();
87 AU.addPreserved<LoopInfo>();
88 AU.addRequiredID(LoopSimplifyID);
89 AU.addPreservedID(LoopSimplifyID);
90 AU.addRequired<DominatorTree>();
91 AU.addRequired<DominanceFrontier>();
92 AU.addPreserved<DominatorTree>();
93 AU.addPreserved<DominanceFrontier>();
96 private:
97 /// 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 /// }
105 bool processOneIterationLoop();
107 // -- Routines used by updateLoopIterationSpace();
109 /// 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 /// }
117 /// is transformed to iterators from A to B, if A > 0 and B < N.
119 bool updateLoopIterationSpace();
121 /// 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);
126 // --- Routines used by splitLoop(). --- /
128 bool splitLoop();
130 /// 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.
133 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB);
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);
141 /// 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
149 /// Update ExitBB PHINodes' to reflect this change.
150 void updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
151 BasicBlock *Header,
152 PHINode *IV, Instruction *IVIncrement, Loop *LP);
154 // --- Utility routines --- /
156 /// cleanBlock - A block is considered clean if all non terminal
157 /// instructions are either PHINodes or IV based values.
158 bool cleanBlock(BasicBlock *BB);
160 /// IVisLT - If Op is comparing IV based value with an loop invariant and
161 /// IV based value is less than the loop invariant then return the loop
162 /// invariant. Otherwise return NULL.
163 Value * IVisLT(ICmpInst &Op);
165 /// IVisLE - If Op is comparing IV based value with an loop invariant and
166 /// 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);
170 /// IVisGT - If Op is comparing IV based value with an loop invariant and
171 /// IV based value is greater than the loop invariant then return the loop
172 /// invariant. Otherwise return NULL.
173 Value * IVisGT(ICmpInst &Op);
175 /// IVisGE - If Op is comparing IV based value with an loop invariant and
176 /// 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);
180 private:
182 // Current Loop information.
183 Loop *L;
184 LPPassManager *LPM;
185 LoopInfo *LI;
186 DominatorTree *DT;
187 DominanceFrontier *DF;
189 PHINode *IndVar;
190 ICmpInst *ExitCondition;
191 ICmpInst *SplitCondition;
192 Value *IVStartValue;
193 Value *IVExitValue;
194 Instruction *IVIncrement;
195 SmallPtrSet<Value *, 4> IVBasedValues;
199 char LoopIndexSplit::ID = 0;
200 static RegisterPass<LoopIndexSplit>
201 X("loop-index-split", "Index Split Loops");
203 Pass *llvm::createLoopIndexSplitPass() {
204 return new LoopIndexSplit();
207 // Index split Loop L. Return true if loop is split.
208 bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
209 L = IncomingLoop;
210 LPM = &LPM_Ref;
212 // FIXME - Nested loops make dominator info updates tricky.
213 if (!L->getSubLoops().empty())
214 return false;
216 DT = &getAnalysis<DominatorTree>();
217 LI = &getAnalysis<LoopInfo>();
218 DF = &getAnalysis<DominanceFrontier>();
220 // Initialize loop data.
221 IndVar = L->getCanonicalInductionVariable();
222 if (!IndVar) return false;
224 bool P1InLoop = L->contains(IndVar->getIncomingBlock(1));
225 IVStartValue = IndVar->getIncomingValue(!P1InLoop);
226 IVIncrement = dyn_cast<Instruction>(IndVar->getIncomingValue(P1InLoop));
227 if (!IVIncrement) return false;
229 IVBasedValues.clear();
230 IVBasedValues.insert(IndVar);
231 IVBasedValues.insert(IVIncrement);
232 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
233 I != E; ++I)
234 for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end();
235 BI != BE; ++BI) {
236 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BI))
237 if (BO != IVIncrement
238 && (BO->getOpcode() == Instruction::Add
239 || BO->getOpcode() == Instruction::Sub))
240 if (IVBasedValues.count(BO->getOperand(0))
241 && L->isLoopInvariant(BO->getOperand(1)))
242 IVBasedValues.insert(BO);
245 // Reject loop if loop exit condition is not suitable.
246 BasicBlock *ExitingBlock = L->getExitingBlock();
247 if (!ExitingBlock)
248 return false;
249 BranchInst *EBR = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
250 if (!EBR) return false;
251 ExitCondition = dyn_cast<ICmpInst>(EBR->getCondition());
252 if (!ExitCondition) return false;
253 if (ExitingBlock != L->getLoopLatch()) return false;
254 IVExitValue = ExitCondition->getOperand(1);
255 if (!L->isLoopInvariant(IVExitValue))
256 IVExitValue = ExitCondition->getOperand(0);
257 if (!L->isLoopInvariant(IVExitValue))
258 return false;
259 if (!IVBasedValues.count(
260 ExitCondition->getOperand(IVExitValue == ExitCondition->getOperand(0))))
261 return false;
263 // If start value is more then exit value where induction variable
264 // increments by 1 then we are potentially dealing with an infinite loop.
265 // Do not index split this loop.
266 if (ConstantInt *SV = dyn_cast<ConstantInt>(IVStartValue))
267 if (ConstantInt *EV = dyn_cast<ConstantInt>(IVExitValue))
268 if (SV->getSExtValue() > EV->getSExtValue())
269 return false;
271 if (processOneIterationLoop())
272 return true;
274 if (updateLoopIterationSpace())
275 return true;
277 if (splitLoop())
278 return true;
280 return false;
283 // --- Helper routines ---
284 // isUsedOutsideLoop - Returns true iff V is used outside the loop L.
285 static bool isUsedOutsideLoop(Value *V, Loop *L) {
286 for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
287 if (!L->contains(cast<Instruction>(*UI)->getParent()))
288 return true;
289 return false;
292 // Return V+1
293 static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt,
294 LLVMContext &Context) {
295 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
296 return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt);
299 // Return V-1
300 static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt,
301 LLVMContext &Context) {
302 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
303 return BinaryOperator::CreateSub(V, One, "lsp", InsertPt);
306 // Return min(V1, V1)
307 static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
309 Value *C = new ICmpInst(InsertPt,
310 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
311 V1, V2, "lsp");
312 return SelectInst::Create(C, V1, V2, "lsp", InsertPt);
315 // Return max(V1, V2)
316 static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) {
318 Value *C = new ICmpInst(InsertPt,
319 Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
320 V1, V2, "lsp");
321 return SelectInst::Create(C, V2, V1, "lsp", InsertPt);
324 /// processOneIterationLoop -- Eliminate loop if loop body is executed
325 /// only once. For example,
326 /// for (i = 0; i < N; ++i) {
327 /// if ( i == X) {
328 /// ...
329 /// }
330 /// }
332 bool LoopIndexSplit::processOneIterationLoop() {
333 SplitCondition = NULL;
334 BasicBlock *Latch = L->getLoopLatch();
335 BasicBlock *Header = L->getHeader();
336 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
337 if (!BR) return false;
338 if (!isa<BranchInst>(Latch->getTerminator())) return false;
339 if (BR->isUnconditional()) return false;
340 SplitCondition = dyn_cast<ICmpInst>(BR->getCondition());
341 if (!SplitCondition) return false;
342 if (SplitCondition == ExitCondition) return false;
343 if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false;
344 if (BR->getOperand(1) != Latch) return false;
345 if (!IVBasedValues.count(SplitCondition->getOperand(0))
346 && !IVBasedValues.count(SplitCondition->getOperand(1)))
347 return false;
349 // If IV is used outside the loop then this loop traversal is required.
350 // FIXME: Calculate and use last IV value.
351 if (isUsedOutsideLoop(IVIncrement, L))
352 return false;
354 // If BR operands are not IV or not loop invariants then skip this loop.
355 Value *OPV = SplitCondition->getOperand(0);
356 Value *SplitValue = SplitCondition->getOperand(1);
357 if (!L->isLoopInvariant(SplitValue))
358 std::swap(OPV, SplitValue);
359 if (!L->isLoopInvariant(SplitValue))
360 return false;
361 Instruction *OPI = dyn_cast<Instruction>(OPV);
362 if (!OPI)
363 return false;
364 if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L))
365 return false;
366 Value *StartValue = IVStartValue;
367 Value *ExitValue = IVExitValue;;
369 if (OPV != IndVar) {
370 // If BR operand is IV based then use this operand to calculate
371 // effective conditions for loop body.
372 BinaryOperator *BOPV = dyn_cast<BinaryOperator>(OPV);
373 if (!BOPV)
374 return false;
375 if (BOPV->getOpcode() != Instruction::Add)
376 return false;
377 StartValue = BinaryOperator::CreateAdd(OPV, StartValue, "" , BR);
378 ExitValue = BinaryOperator::CreateAdd(OPV, ExitValue, "" , BR);
381 if (!cleanBlock(Header))
382 return false;
384 if (!cleanBlock(Latch))
385 return false;
387 // If the merge point for BR is not loop latch then skip this loop.
388 if (BR->getSuccessor(0) != Latch) {
389 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
390 assert (DF0 != DF->end() && "Unable to find dominance frontier");
391 if (!DF0->second.count(Latch))
392 return false;
395 if (BR->getSuccessor(1) != Latch) {
396 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
397 assert (DF1 != DF->end() && "Unable to find dominance frontier");
398 if (!DF1->second.count(Latch))
399 return false;
402 // Now, Current loop L contains compare instruction
403 // that compares induction variable, IndVar, against loop invariant. And
404 // entire (i.e. meaningful) loop body is dominated by this compare
405 // instruction. In such case eliminate
406 // loop structure surrounding this loop body. For example,
407 // for (int i = start; i < end; ++i) {
408 // if ( i == somevalue) {
409 // loop_body
410 // }
411 // }
412 // can be transformed into
413 // if (somevalue >= start && somevalue < end) {
414 // i = somevalue;
415 // loop_body
416 // }
418 // Replace index variable with split value in loop body. Loop body is executed
419 // only when index variable is equal to split value.
420 IndVar->replaceAllUsesWith(SplitValue);
422 // Replace split condition in header.
423 // Transform
424 // SplitCondition : icmp eq i32 IndVar, SplitValue
425 // into
426 // c1 = icmp uge i32 SplitValue, StartValue
427 // c2 = icmp ult i32 SplitValue, ExitValue
428 // and i32 c1, c2
429 Instruction *C1 = new ICmpInst(BR, ExitCondition->isSignedPredicate() ?
430 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
431 SplitValue, StartValue, "lisplit");
433 CmpInst::Predicate C2P = ExitCondition->getPredicate();
434 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
435 if (LatchBR->getOperand(1) != Header)
436 C2P = CmpInst::getInversePredicate(C2P);
437 Instruction *C2 = new ICmpInst(BR, C2P, SplitValue, ExitValue, "lisplit");
438 Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR);
440 SplitCondition->replaceAllUsesWith(NSplitCond);
441 SplitCondition->eraseFromParent();
443 // Remove Latch to Header edge.
444 BasicBlock *LatchSucc = NULL;
445 Header->removePredecessor(Latch);
446 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
447 SI != E; ++SI) {
448 if (Header != *SI)
449 LatchSucc = *SI;
452 // Clean up latch block.
453 Value *LatchBRCond = LatchBR->getCondition();
454 LatchBR->setUnconditionalDest(LatchSucc);
455 RecursivelyDeleteTriviallyDeadInstructions(LatchBRCond);
457 LPM->deleteLoopFromQueue(L);
459 // Update Dominator Info.
460 // Only CFG change done is to remove Latch to Header edge. This
461 // does not change dominator tree because Latch did not dominate
462 // Header.
463 if (DF) {
464 DominanceFrontier::iterator HeaderDF = DF->find(Header);
465 if (HeaderDF != DF->end())
466 DF->removeFromFrontier(HeaderDF, Header);
468 DominanceFrontier::iterator LatchDF = DF->find(Latch);
469 if (LatchDF != DF->end())
470 DF->removeFromFrontier(LatchDF, Header);
473 ++NumIndexSplitRemoved;
474 return true;
477 /// restrictLoopBound - Op dominates loop body. Op compares an IV based value
478 /// with a loop invariant value. Update loop's lower and upper bound based on
479 /// the loop invariant value.
480 bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) {
481 bool Sign = Op.isSignedPredicate();
482 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
484 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
485 BranchInst *EBR =
486 cast<BranchInst>(ExitCondition->getParent()->getTerminator());
487 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
488 BasicBlock *T = EBR->getSuccessor(0);
489 EBR->setSuccessor(0, EBR->getSuccessor(1));
490 EBR->setSuccessor(1, T);
493 LLVMContext &Context = Op.getContext();
495 // New upper and lower bounds.
496 Value *NLB = NULL;
497 Value *NUB = NULL;
498 if (Value *V = IVisLT(Op)) {
499 // Restrict upper bound.
500 if (IVisLE(*ExitCondition))
501 V = getMinusOne(V, Sign, PHTerm, Context);
502 NUB = getMin(V, IVExitValue, Sign, PHTerm);
503 } else if (Value *V = IVisLE(Op)) {
504 // Restrict upper bound.
505 if (IVisLT(*ExitCondition))
506 V = getPlusOne(V, Sign, PHTerm, Context);
507 NUB = getMin(V, IVExitValue, Sign, PHTerm);
508 } else if (Value *V = IVisGT(Op)) {
509 // Restrict lower bound.
510 V = getPlusOne(V, Sign, PHTerm, Context);
511 NLB = getMax(V, IVStartValue, Sign, PHTerm);
512 } else if (Value *V = IVisGE(Op))
513 // Restrict lower bound.
514 NLB = getMax(V, IVStartValue, Sign, PHTerm);
516 if (!NLB && !NUB)
517 return false;
519 if (NLB) {
520 unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader());
521 IndVar->setIncomingValue(i, NLB);
524 if (NUB) {
525 unsigned i = (ExitCondition->getOperand(0) != IVExitValue);
526 ExitCondition->setOperand(i, NUB);
528 return true;
531 /// updateLoopIterationSpace -- Update loop's iteration space if loop
532 /// body is executed for certain IV range only. For example,
533 ///
534 /// for (i = 0; i < N; ++i) {
535 /// if ( i > A && i < B) {
536 /// ...
537 /// }
538 /// }
539 /// is transformed to iterators from A to B, if A > 0 and B < N.
541 bool LoopIndexSplit::updateLoopIterationSpace() {
542 SplitCondition = NULL;
543 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
544 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
545 return false;
546 BasicBlock *Latch = L->getLoopLatch();
547 BasicBlock *Header = L->getHeader();
548 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator());
549 if (!BR) return false;
550 if (!isa<BranchInst>(Latch->getTerminator())) return false;
551 if (BR->isUnconditional()) return false;
552 BinaryOperator *AND = dyn_cast<BinaryOperator>(BR->getCondition());
553 if (!AND) return false;
554 if (AND->getOpcode() != Instruction::And) return false;
555 ICmpInst *Op0 = dyn_cast<ICmpInst>(AND->getOperand(0));
556 ICmpInst *Op1 = dyn_cast<ICmpInst>(AND->getOperand(1));
557 if (!Op0 || !Op1)
558 return false;
559 IVBasedValues.insert(AND);
560 IVBasedValues.insert(Op0);
561 IVBasedValues.insert(Op1);
562 if (!cleanBlock(Header)) return false;
563 BasicBlock *ExitingBlock = ExitCondition->getParent();
564 if (!cleanBlock(ExitingBlock)) return false;
566 // If the merge point for BR is not loop latch then skip this loop.
567 if (BR->getSuccessor(0) != Latch) {
568 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
569 assert (DF0 != DF->end() && "Unable to find dominance frontier");
570 if (!DF0->second.count(Latch))
571 return false;
574 if (BR->getSuccessor(1) != Latch) {
575 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
576 assert (DF1 != DF->end() && "Unable to find dominance frontier");
577 if (!DF1->second.count(Latch))
578 return false;
581 // Verify that loop exiting block has only two predecessor, where one pred
582 // is split condition block. The other predecessor will become exiting block's
583 // dominator after CFG is updated. TODO : Handle CFG's where exiting block has
584 // more then two predecessors. This requires extra work in updating dominator
585 // information.
586 BasicBlock *ExitingBBPred = NULL;
587 for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock);
588 PI != PE; ++PI) {
589 BasicBlock *BB = *PI;
590 if (Header == BB)
591 continue;
592 if (ExitingBBPred)
593 return false;
594 else
595 ExitingBBPred = BB;
598 if (!restrictLoopBound(*Op0))
599 return false;
601 if (!restrictLoopBound(*Op1))
602 return false;
604 // Update CFG.
605 if (BR->getSuccessor(0) == ExitingBlock)
606 BR->setUnconditionalDest(BR->getSuccessor(1));
607 else
608 BR->setUnconditionalDest(BR->getSuccessor(0));
610 AND->eraseFromParent();
611 if (Op0->use_empty())
612 Op0->eraseFromParent();
613 if (Op1->use_empty())
614 Op1->eraseFromParent();
616 // Update domiantor info. Now, ExitingBlock has only one predecessor,
617 // ExitingBBPred, and it is ExitingBlock's immediate domiantor.
618 DT->changeImmediateDominator(ExitingBlock, ExitingBBPred);
620 BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1);
621 if (L->contains(ExitBlock))
622 ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0);
624 // If ExitingBlock is a member of the loop basic blocks' DF list then
625 // replace ExitingBlock with header and exit block in the DF list
626 DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock);
627 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
628 I != E; ++I) {
629 BasicBlock *BB = *I;
630 if (BB == Header || BB == ExitingBlock)
631 continue;
632 DominanceFrontier::iterator BBDF = DF->find(BB);
633 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
634 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
635 while (DomSetI != DomSetE) {
636 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
637 ++DomSetI;
638 BasicBlock *DFBB = *CurrentItr;
639 if (DFBB == ExitingBlock) {
640 BBDF->second.erase(DFBB);
641 for (DominanceFrontier::DomSetType::iterator
642 EBI = ExitingBlockDF->second.begin(),
643 EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI)
644 BBDF->second.insert(*EBI);
648 NumRestrictBounds++;
649 return true;
652 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
653 /// This routine is used to remove split condition's dead branch, dominated by
654 /// DeadBB. LiveBB dominates split conidition's other branch.
655 void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
656 BasicBlock *LiveBB) {
658 // First update DeadBB's dominance frontier.
659 SmallVector<BasicBlock *, 8> FrontierBBs;
660 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
661 if (DeadBBDF != DF->end()) {
662 SmallVector<BasicBlock *, 8> PredBlocks;
664 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
665 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
666 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI)
668 BasicBlock *FrontierBB = *DeadBBSetI;
669 FrontierBBs.push_back(FrontierBB);
671 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
672 PredBlocks.clear();
673 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
674 PI != PE; ++PI) {
675 BasicBlock *P = *PI;
676 if (P == DeadBB || DT->dominates(DeadBB, P))
677 PredBlocks.push_back(P);
680 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
681 FBI != FBE; ++FBI) {
682 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
683 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
684 PE = PredBlocks.end(); PI != PE; ++PI) {
685 BasicBlock *P = *PI;
686 PN->removeIncomingValue(P);
689 else
690 break;
695 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
696 SmallVector<BasicBlock *, 32> WorkList;
697 DomTreeNode *DN = DT->getNode(DeadBB);
698 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
699 E = df_end(DN); DI != E; ++DI) {
700 BasicBlock *BB = DI->getBlock();
701 WorkList.push_back(BB);
702 BB->replaceAllUsesWith(UndefValue::get(
703 Type::getLabelTy(DeadBB->getContext())));
706 while (!WorkList.empty()) {
707 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
708 LPM->deleteSimpleAnalysisValue(BB, LP);
709 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
710 BBI != BBE; ) {
711 Instruction *I = BBI;
712 ++BBI;
713 I->replaceAllUsesWith(UndefValue::get(I->getType()));
714 LPM->deleteSimpleAnalysisValue(I, LP);
715 I->eraseFromParent();
717 DT->eraseNode(BB);
718 DF->removeBlock(BB);
719 LI->removeBlock(BB);
720 BB->eraseFromParent();
723 // Update Frontier BBs' dominator info.
724 while (!FrontierBBs.empty()) {
725 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
726 BasicBlock *NewDominator = FBB->getSinglePredecessor();
727 if (!NewDominator) {
728 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
729 NewDominator = *PI;
730 ++PI;
731 if (NewDominator != LiveBB) {
732 for(; PI != PE; ++PI) {
733 BasicBlock *P = *PI;
734 if (P == LiveBB) {
735 NewDominator = LiveBB;
736 break;
738 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
742 assert (NewDominator && "Unable to fix dominator info.");
743 DT->changeImmediateDominator(FBB, NewDominator);
744 DF->changeImmediateDominator(FBB, NewDominator, DT);
749 // moveExitCondition - Move exit condition EC into split condition block CondBB.
750 void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB,
751 BasicBlock *ExitBB, ICmpInst *EC,
752 ICmpInst *SC, PHINode *IV,
753 Instruction *IVAdd, Loop *LP,
754 unsigned ExitValueNum) {
756 BasicBlock *ExitingBB = EC->getParent();
757 Instruction *CurrentBR = CondBB->getTerminator();
759 // Move exit condition into split condition block.
760 EC->moveBefore(CurrentBR);
761 EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV);
763 // Move exiting block's branch into split condition block. Update its branch
764 // destination.
765 BranchInst *ExitingBR = cast<BranchInst>(ExitingBB->getTerminator());
766 ExitingBR->moveBefore(CurrentBR);
767 BasicBlock *OrigDestBB = NULL;
768 if (ExitingBR->getSuccessor(0) == ExitBB) {
769 OrigDestBB = ExitingBR->getSuccessor(1);
770 ExitingBR->setSuccessor(1, ActiveBB);
772 else {
773 OrigDestBB = ExitingBR->getSuccessor(0);
774 ExitingBR->setSuccessor(0, ActiveBB);
777 // Remove split condition and current split condition branch.
778 SC->eraseFromParent();
779 CurrentBR->eraseFromParent();
781 // Connect exiting block to original destination.
782 BranchInst::Create(OrigDestBB, ExitingBB);
784 // Update PHINodes
785 updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP);
787 // Fix dominator info.
788 // ExitBB is now dominated by CondBB
789 DT->changeImmediateDominator(ExitBB, CondBB);
790 DF->changeImmediateDominator(ExitBB, CondBB, DT);
792 // Blocks outside the loop may have been in the dominance frontier of blocks
793 // inside the condition; this is now impossible because the blocks inside the
794 // condition no loger dominate the exit. Remove the relevant blocks from
795 // the dominance frontiers.
796 for (Loop::block_iterator I = LP->block_begin(), E = LP->block_end();
797 I != E; ++I) {
798 if (*I == CondBB || !DT->dominates(CondBB, *I)) continue;
799 DominanceFrontier::iterator BBDF = DF->find(*I);
800 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin();
801 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end();
802 while (DomSetI != DomSetE) {
803 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI;
804 ++DomSetI;
805 BasicBlock *DFBB = *CurrentItr;
806 if (!LP->contains(DFBB))
807 BBDF->second.erase(DFBB);
812 /// updatePHINodes - CFG has been changed.
813 /// Before
814 /// - ExitBB's single predecessor was Latch
815 /// - Latch's second successor was Header
816 /// Now
817 /// - ExitBB's single predecessor is Header
818 /// - Latch's one and only successor is Header
820 /// Update ExitBB PHINodes' to reflect this change.
821 void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch,
822 BasicBlock *Header,
823 PHINode *IV, Instruction *IVIncrement,
824 Loop *LP) {
826 for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end();
827 BI != BE; ) {
828 PHINode *PN = dyn_cast<PHINode>(BI);
829 ++BI;
830 if (!PN)
831 break;
833 Value *V = PN->getIncomingValueForBlock(Latch);
834 if (PHINode *PHV = dyn_cast<PHINode>(V)) {
835 // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use
836 // in Header which is new incoming value for PN.
837 Value *NewV = NULL;
838 for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end();
839 UI != E; ++UI)
840 if (PHINode *U = dyn_cast<PHINode>(*UI))
841 if (LP->contains(U->getParent())) {
842 NewV = U;
843 break;
846 // Add incoming value from header only if PN has any use inside the loop.
847 if (NewV)
848 PN->addIncoming(NewV, Header);
850 } else if (Instruction *PHI = dyn_cast<Instruction>(V)) {
851 // If this instruction is IVIncrement then IV is new incoming value
852 // from header otherwise this instruction must be incoming value from
853 // header because loop is in LCSSA form.
854 if (PHI == IVIncrement)
855 PN->addIncoming(IV, Header);
856 else
857 PN->addIncoming(V, Header);
858 } else
859 // Otherwise this is an incoming value from header because loop is in
860 // LCSSA form.
861 PN->addIncoming(V, Header);
863 // Remove incoming value from Latch.
864 PN->removeIncomingValue(Latch);
868 bool LoopIndexSplit::splitLoop() {
869 SplitCondition = NULL;
870 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE
871 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ)
872 return false;
873 BasicBlock *Header = L->getHeader();
874 BasicBlock *Latch = L->getLoopLatch();
875 BranchInst *SBR = NULL; // Split Condition Branch
876 BranchInst *EBR = cast<BranchInst>(ExitCondition->getParent()->getTerminator());
877 // If Exiting block includes loop variant instructions then this
878 // loop may not be split safely.
879 BasicBlock *ExitingBlock = ExitCondition->getParent();
880 if (!cleanBlock(ExitingBlock)) return false;
882 LLVMContext &Context = Header->getContext();
884 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
885 I != E; ++I) {
886 BranchInst *BR = dyn_cast<BranchInst>((*I)->getTerminator());
887 if (!BR || BR->isUnconditional()) continue;
888 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
889 if (!CI || CI == ExitCondition
890 || CI->getPredicate() == ICmpInst::ICMP_NE
891 || CI->getPredicate() == ICmpInst::ICMP_EQ)
892 continue;
894 // Unable to handle triangle loops at the moment.
895 // In triangle loop, split condition is in header and one of the
896 // the split destination is loop latch. If split condition is EQ
897 // then such loops are already handle in processOneIterationLoop().
898 if (Header == (*I)
899 && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1)))
900 continue;
902 // If the block does not dominate the latch then this is not a diamond.
903 // Such loop may not benefit from index split.
904 if (!DT->dominates((*I), Latch))
905 continue;
907 // If split condition branches heads do not have single predecessor,
908 // SplitCondBlock, then is not possible to remove inactive branch.
909 if (!BR->getSuccessor(0)->getSinglePredecessor()
910 || !BR->getSuccessor(1)->getSinglePredecessor())
911 return false;
913 // If the merge point for BR is not loop latch then skip this condition.
914 if (BR->getSuccessor(0) != Latch) {
915 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0));
916 assert (DF0 != DF->end() && "Unable to find dominance frontier");
917 if (!DF0->second.count(Latch))
918 continue;
921 if (BR->getSuccessor(1) != Latch) {
922 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1));
923 assert (DF1 != DF->end() && "Unable to find dominance frontier");
924 if (!DF1->second.count(Latch))
925 continue;
927 SplitCondition = CI;
928 SBR = BR;
929 break;
932 if (!SplitCondition)
933 return false;
935 // If the predicate sign does not match then skip.
936 if (ExitCondition->isSignedPredicate() != SplitCondition->isSignedPredicate())
937 return false;
939 unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue);
940 unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0));
941 Value *SplitValue = SplitCondition->getOperand(SVOpNum);
942 if (!L->isLoopInvariant(SplitValue))
943 return false;
944 if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum)))
945 return false;
947 // Normalize loop conditions so that it is easier to calculate new loop
948 // bounds.
949 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) {
950 ExitCondition->setPredicate(ExitCondition->getInversePredicate());
951 BasicBlock *T = EBR->getSuccessor(0);
952 EBR->setSuccessor(0, EBR->getSuccessor(1));
953 EBR->setSuccessor(1, T);
956 if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) {
957 SplitCondition->setPredicate(SplitCondition->getInversePredicate());
958 BasicBlock *T = SBR->getSuccessor(0);
959 SBR->setSuccessor(0, SBR->getSuccessor(1));
960 SBR->setSuccessor(1, T);
963 //[*] Calculate new loop bounds.
964 Value *AEV = SplitValue;
965 Value *BSV = SplitValue;
966 bool Sign = SplitCondition->isSignedPredicate();
967 Instruction *PHTerm = L->getLoopPreheader()->getTerminator();
969 if (IVisLT(*ExitCondition)) {
970 if (IVisLT(*SplitCondition)) {
971 /* Do nothing */
973 else if (IVisLE(*SplitCondition)) {
974 AEV = getPlusOne(SplitValue, Sign, PHTerm, Context);
975 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
976 } else {
977 assert (0 && "Unexpected split condition!");
980 else if (IVisLE(*ExitCondition)) {
981 if (IVisLT(*SplitCondition)) {
982 AEV = getMinusOne(SplitValue, Sign, PHTerm, Context);
984 else if (IVisLE(*SplitCondition)) {
985 BSV = getPlusOne(SplitValue, Sign, PHTerm, Context);
986 } else {
987 assert (0 && "Unexpected split condition!");
989 } else {
990 assert (0 && "Unexpected exit condition!");
992 AEV = getMin(AEV, IVExitValue, Sign, PHTerm);
993 BSV = getMax(BSV, IVStartValue, Sign, PHTerm);
995 // [*] Clone Loop
996 DenseMap<const Value *, Value *> ValueMap;
997 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this);
998 Loop *ALoop = L;
1000 // [*] ALoop's exiting edge enters BLoop's header.
1001 // ALoop's original exit block becomes BLoop's exit block.
1002 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]);
1003 BasicBlock *A_ExitingBlock = ExitCondition->getParent();
1004 BranchInst *A_ExitInsn =
1005 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator());
1006 assert (A_ExitInsn && "Unable to find suitable loop exit branch");
1007 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1);
1008 BasicBlock *B_Header = BLoop->getHeader();
1009 if (ALoop->contains(B_ExitBlock)) {
1010 B_ExitBlock = A_ExitInsn->getSuccessor(0);
1011 A_ExitInsn->setSuccessor(0, B_Header);
1012 } else
1013 A_ExitInsn->setSuccessor(1, B_Header);
1015 // [*] Update ALoop's exit value using new exit value.
1016 ExitCondition->setOperand(EVOpNum, AEV);
1018 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from
1019 // original loop's preheader. Add incoming PHINode values from
1020 // ALoop's exiting block. Update BLoop header's domiantor info.
1022 // Collect inverse map of Header PHINodes.
1023 DenseMap<Value *, Value *> InverseMap;
1024 for (BasicBlock::iterator BI = ALoop->getHeader()->begin(),
1025 BE = ALoop->getHeader()->end(); BI != BE; ++BI) {
1026 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1027 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
1028 InverseMap[PNClone] = PN;
1029 } else
1030 break;
1033 BasicBlock *A_Preheader = ALoop->getLoopPreheader();
1034 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1035 BI != BE; ++BI) {
1036 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1037 // Remove incoming value from original preheader.
1038 PN->removeIncomingValue(A_Preheader);
1040 // Add incoming value from A_ExitingBlock.
1041 if (PN == B_IndVar)
1042 PN->addIncoming(BSV, A_ExitingBlock);
1043 else {
1044 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
1045 Value *V2 = NULL;
1046 // If loop header is also loop exiting block then
1047 // OrigPN is incoming value for B loop header.
1048 if (A_ExitingBlock == ALoop->getHeader())
1049 V2 = OrigPN;
1050 else
1051 V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock);
1052 PN->addIncoming(V2, A_ExitingBlock);
1054 } else
1055 break;
1058 DT->changeImmediateDominator(B_Header, A_ExitingBlock);
1059 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT);
1061 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit
1062 // block. Remove incoming PHINode values from ALoop's exiting block.
1063 // Add new incoming values from BLoop's incoming exiting value.
1064 // Update BLoop exit block's dominator info..
1065 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]);
1066 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end();
1067 BI != BE; ++BI) {
1068 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1069 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)],
1070 B_ExitingBlock);
1071 PN->removeIncomingValue(A_ExitingBlock);
1072 } else
1073 break;
1076 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock);
1077 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT);
1079 //[*] Split ALoop's exit edge. This creates a new block which
1080 // serves two purposes. First one is to hold PHINode defnitions
1081 // to ensure that ALoop's LCSSA form. Second use it to act
1082 // as a preheader for BLoop.
1083 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this);
1085 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes
1086 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop.
1087 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end();
1088 BI != BE; ++BI) {
1089 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
1090 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock);
1091 PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName());
1092 newPHI->addIncoming(V1, A_ExitingBlock);
1093 A_ExitBlock->getInstList().push_front(newPHI);
1094 PN->removeIncomingValue(A_ExitBlock);
1095 PN->addIncoming(newPHI, A_ExitBlock);
1096 } else
1097 break;
1100 //[*] Eliminate split condition's inactive branch from ALoop.
1101 BasicBlock *A_SplitCondBlock = SplitCondition->getParent();
1102 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator());
1103 BasicBlock *A_InactiveBranch = NULL;
1104 BasicBlock *A_ActiveBranch = NULL;
1105 A_ActiveBranch = A_BR->getSuccessor(0);
1106 A_InactiveBranch = A_BR->getSuccessor(1);
1107 A_BR->setUnconditionalDest(A_ActiveBranch);
1108 removeBlocks(A_InactiveBranch, L, A_ActiveBranch);
1110 //[*] Eliminate split condition's inactive branch in from BLoop.
1111 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]);
1112 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator());
1113 BasicBlock *B_InactiveBranch = NULL;
1114 BasicBlock *B_ActiveBranch = NULL;
1115 B_ActiveBranch = B_BR->getSuccessor(1);
1116 B_InactiveBranch = B_BR->getSuccessor(0);
1117 B_BR->setUnconditionalDest(B_ActiveBranch);
1118 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch);
1120 BasicBlock *A_Header = ALoop->getHeader();
1121 if (A_ExitingBlock == A_Header)
1122 return true;
1124 //[*] Move exit condition into split condition block to avoid
1125 // executing dead loop iteration.
1126 ICmpInst *B_ExitCondition = cast<ICmpInst>(ValueMap[ExitCondition]);
1127 Instruction *B_IndVarIncrement = cast<Instruction>(ValueMap[IVIncrement]);
1128 ICmpInst *B_SplitCondition = cast<ICmpInst>(ValueMap[SplitCondition]);
1130 moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition,
1131 cast<ICmpInst>(SplitCondition), IndVar, IVIncrement,
1132 ALoop, EVOpNum);
1134 moveExitCondition(B_SplitCondBlock, B_ActiveBranch,
1135 B_ExitBlock, B_ExitCondition,
1136 B_SplitCondition, B_IndVar, B_IndVarIncrement,
1137 BLoop, EVOpNum);
1139 NumIndexSplit++;
1140 return true;
1143 /// cleanBlock - A block is considered clean if all non terminal instructions
1144 /// are either, PHINodes, IV based.
1145 bool LoopIndexSplit::cleanBlock(BasicBlock *BB) {
1146 Instruction *Terminator = BB->getTerminator();
1147 for(BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1148 BI != BE; ++BI) {
1149 Instruction *I = BI;
1151 if (isa<PHINode>(I) || I == Terminator || I == ExitCondition
1152 || I == SplitCondition || IVBasedValues.count(I)
1153 || isa<DbgInfoIntrinsic>(I))
1154 continue;
1156 if (I->mayHaveSideEffects())
1157 return false;
1159 // I is used only inside this block then it is OK.
1160 bool usedOutsideBB = false;
1161 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
1162 UI != UE; ++UI) {
1163 Instruction *U = cast<Instruction>(UI);
1164 if (U->getParent() != BB)
1165 usedOutsideBB = true;
1167 if (!usedOutsideBB)
1168 continue;
1170 // Otherwise we have a instruction that may not allow loop spliting.
1171 return false;
1173 return true;
1176 /// IVisLT - If Op is comparing IV based value with an loop invariant and
1177 /// IV based value is less than the loop invariant then return the loop
1178 /// invariant. Otherwise return NULL.
1179 Value * LoopIndexSplit::IVisLT(ICmpInst &Op) {
1180 ICmpInst::Predicate P = Op.getPredicate();
1181 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1182 && IVBasedValues.count(Op.getOperand(0))
1183 && L->isLoopInvariant(Op.getOperand(1)))
1184 return Op.getOperand(1);
1186 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1187 && IVBasedValues.count(Op.getOperand(1))
1188 && L->isLoopInvariant(Op.getOperand(0)))
1189 return Op.getOperand(0);
1191 return NULL;
1194 /// IVisLE - If Op is comparing IV based value with an loop invariant and
1195 /// IV based value is less than or equal to the loop invariant then
1196 /// return the loop invariant. Otherwise return NULL.
1197 Value * LoopIndexSplit::IVisLE(ICmpInst &Op) {
1198 ICmpInst::Predicate P = Op.getPredicate();
1199 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1200 && IVBasedValues.count(Op.getOperand(0))
1201 && L->isLoopInvariant(Op.getOperand(1)))
1202 return Op.getOperand(1);
1204 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1205 && IVBasedValues.count(Op.getOperand(1))
1206 && L->isLoopInvariant(Op.getOperand(0)))
1207 return Op.getOperand(0);
1209 return NULL;
1212 /// IVisGT - If Op is comparing IV based value with an loop invariant and
1213 /// IV based value is greater than the loop invariant then return the loop
1214 /// invariant. Otherwise return NULL.
1215 Value * LoopIndexSplit::IVisGT(ICmpInst &Op) {
1216 ICmpInst::Predicate P = Op.getPredicate();
1217 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT)
1218 && IVBasedValues.count(Op.getOperand(0))
1219 && L->isLoopInvariant(Op.getOperand(1)))
1220 return Op.getOperand(1);
1222 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT)
1223 && IVBasedValues.count(Op.getOperand(1))
1224 && L->isLoopInvariant(Op.getOperand(0)))
1225 return Op.getOperand(0);
1227 return NULL;
1230 /// IVisGE - If Op is comparing IV based value with an loop invariant and
1231 /// IV based value is greater than or equal to the loop invariant then
1232 /// return the loop invariant. Otherwise return NULL.
1233 Value * LoopIndexSplit::IVisGE(ICmpInst &Op) {
1234 ICmpInst::Predicate P = Op.getPredicate();
1235 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE)
1236 && IVBasedValues.count(Op.getOperand(0))
1237 && L->isLoopInvariant(Op.getOperand(1)))
1238 return Op.getOperand(1);
1240 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE)
1241 && IVBasedValues.count(Op.getOperand(1))
1242 && L->isLoopInvariant(Op.getOperand(0)))
1243 return Op.getOperand(0);
1245 return NULL;