- Value *UB = ExitCondition->getOperand(ExitValueNum);
- const Type *Ty = NV->getType();
- bool Sign = ExitCondition->isSignedPredicate();
- BasicBlock *Preheader = L->getLoopPreheader();
- Instruction *PHTerminator = Preheader->getTerminator();
-
- assert (NV && "Unexpected value");
-
- switch (CI->getPredicate()) {
- case ICmpInst::ICMP_ULE:
- case ICmpInst::ICMP_SLE:
- // for (i = LB; i < UB; ++i)
- // if (i <= NV && ...)
- // LOOP_BODY
- //
- // is transformed into
- // NUB = min (NV+1, UB)
- // for (i = LB; i < NUB ; ++i)
- // LOOP_BODY
- //
- if (ExitCondition->getPredicate() == ICmpInst::ICMP_SLT
- || ExitCondition->getPredicate() == ICmpInst::ICMP_ULT) {
- Value *A = BinaryOperator::CreateAdd(NV, ConstantInt::get(Ty, 1, Sign),
- "lsplit.add", PHTerminator);
- Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
- A, UB,"lsplit,c", PHTerminator);
- NUB = SelectInst::Create(C, A, UB, "lsplit.nub", PHTerminator);
- }
-
- // for (i = LB; i <= UB; ++i)
- // if (i <= NV && ...)
- // LOOP_BODY
- //
- // is transformed into
- // NUB = min (NV, UB)
- // for (i = LB; i <= NUB ; ++i)
- // LOOP_BODY
- //
- else if (ExitCondition->getPredicate() == ICmpInst::ICMP_SLE
- || ExitCondition->getPredicate() == ICmpInst::ICMP_ULE) {
- Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
- NV, UB, "lsplit.c", PHTerminator);
- NUB = SelectInst::Create(C, NV, UB, "lsplit.nub", PHTerminator);
- }
- break;
- case ICmpInst::ICMP_ULT:
- case ICmpInst::ICMP_SLT:
- // for (i = LB; i < UB; ++i)
- // if (i < NV && ...)
- // LOOP_BODY
- //
- // is transformed into
- // NUB = min (NV, UB)
- // for (i = LB; i < NUB ; ++i)
- // LOOP_BODY
- //
- if (ExitCondition->getPredicate() == ICmpInst::ICMP_SLT
- || ExitCondition->getPredicate() == ICmpInst::ICMP_ULT) {
- Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
- NV, UB, "lsplit.c", PHTerminator);
- NUB = SelectInst::Create(C, NV, UB, "lsplit.nub", PHTerminator);
- }
-
- // for (i = LB; i <= UB; ++i)
- // if (i < NV && ...)
- // LOOP_BODY
- //
- // is transformed into
- // NUB = min (NV -1 , UB)
- // for (i = LB; i <= NUB ; ++i)
- // LOOP_BODY
- //
- else if (ExitCondition->getPredicate() == ICmpInst::ICMP_SLE
- || ExitCondition->getPredicate() == ICmpInst::ICMP_ULE) {
- Value *S = BinaryOperator::CreateSub(NV, ConstantInt::get(Ty, 1, Sign),
- "lsplit.add", PHTerminator);
- Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
- S, UB, "lsplit.c", PHTerminator);
- NUB = SelectInst::Create(C, S, UB, "lsplit.nub", PHTerminator);
- }
- break;
- case ICmpInst::ICMP_UGE:
- case ICmpInst::ICMP_SGE:
- // for (i = LB; i (< or <=) UB; ++i)
- // if (i >= NV && ...)
- // LOOP_BODY
- //
- // is transformed into
- // NLB = max (NV, LB)
- // for (i = NLB; i (< or <=) UB ; ++i)
- // LOOP_BODY
- //
- {
- Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
- NV, StartValue, "lsplit.c", PHTerminator);
- NLB = SelectInst::Create(C, StartValue, NV, "lsplit.nlb", PHTerminator);
- }
- break;
- case ICmpInst::ICMP_UGT:
- case ICmpInst::ICMP_SGT:
- // for (i = LB; i (< or <=) UB; ++i)
- // if (i > NV && ...)
- // LOOP_BODY
- //
- // is transformed into
- // NLB = max (NV+1, LB)
- // for (i = NLB; i (< or <=) UB ; ++i)
- // LOOP_BODY
- //
- {
- Value *A = BinaryOperator::CreateAdd(NV, ConstantInt::get(Ty, 1, Sign),
- "lsplit.add", PHTerminator);
- Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
- A, StartValue, "lsplit.c", PHTerminator);
- NLB = SelectInst::Create(C, StartValue, A, "lsplit.nlb", PHTerminator);
- }
- break;
- default:
- assert ( 0 && "Unexpected split condition predicate");
- }
+ Value *NUB = NULL;
+ if (Value *V = IVisLT(Op)) {
+ // Restrict upper bound.
+ if (IVisLE(*ExitCondition))
+ V = getMinusOne(V, Sign, PHTerm);
+ NUB = getMin(V, IVExitValue, Sign, PHTerm);
+ } else if (Value *V = IVisLE(Op)) {
+ // Restrict upper bound.
+ if (IVisLT(*ExitCondition))
+ V = getPlusOne(V, Sign, PHTerm);
+ NUB = getMin(V, IVExitValue, Sign, PHTerm);
+ } else if (Value *V = IVisGT(Op)) {
+ // Restrict lower bound.
+ V = getPlusOne(V, Sign, PHTerm);
+ NLB = getMax(V, IVStartValue, Sign, PHTerm);
+ } else if (Value *V = IVisGE(Op))
+ // Restrict lower bound.
+ NLB = getMax(V, IVStartValue, Sign, PHTerm);
+
+ if (!NLB && !NUB)
+ return false;