llvm.org GIT mirror llvm / 0f4b285
Replace the original ad-hoc code for determining whether (v pred w) implies (x pred y) with more thorough code that does more complete canonicalization before resorting to range checks. This helps it find more cases where the canonicalized expressions match. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76671 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
3 changed file(s) with 255 addition(s) and 119 deletion(s). Raw diff Collapse all Expand all
336336 /// found.
337337 BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
338338
339 /// isNecessaryCond - Test whether the condition described by Pred, LHS,
340 /// and RHS is a necessary condition for the given Cond value to evaluate
341 /// to true.
342 bool isNecessaryCond(Value *Cond, ICmpInst::Predicate Pred,
343 const SCEV *LHS, const SCEV *RHS,
344 bool Inverse);
345
346 /// isNecessaryCondOperands - Test whether the condition described by Pred,
347 /// LHS, and RHS is a necessary condition for the condition described by
348 /// Pred, FoundLHS, and FoundRHS to evaluate to true.
349 bool isNecessaryCondOperands(ICmpInst::Predicate Pred,
350 const SCEV *LHS, const SCEV *RHS,
351 const SCEV *FoundLHS, const SCEV *FoundRHS);
339 /// isImpliedCond - Test whether the condition described by Pred, LHS,
340 /// and RHS is true whenever the given Cond value evaluates to true.
341 bool isImpliedCond(Value *Cond, ICmpInst::Predicate Pred,
342 const SCEV *LHS, const SCEV *RHS,
343 bool Inverse);
344
345 /// isImpliedCondOperands - Test whether the condition described by Pred,
346 /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
347 /// and FoundRHS is true.
348 bool isImpliedCondOperands(ICmpInst::Predicate Pred,
349 const SCEV *LHS, const SCEV *RHS,
350 const SCEV *FoundLHS, const SCEV *FoundRHS);
351
352 /// isImpliedCondOperandsHelper - Test whether the condition described by
353 /// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
354 /// FoundLHS, and FoundRHS is true.
355 bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
356 const SCEV *LHS, const SCEV *RHS,
357 const SCEV *FoundLHS, const SCEV *FoundRHS);
352358
353359 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
354360 /// in the header of its containing loop, we know the loop executes a
43574357 LoopContinuePredicate->isUnconditional())
43584358 return false;
43594359
4360 return
4361 isNecessaryCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS,
4362 LoopContinuePredicate->getSuccessor(0) != L->getHeader());
4360 return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS,
4361 LoopContinuePredicate->getSuccessor(0) != L->getHeader());
43634362 }
43644363
43654364 /// isLoopGuardedByCond - Test whether entry to the loop is protected
43894388 LoopEntryPredicate->isUnconditional())
43904389 continue;
43914390
4392 if (isNecessaryCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS,
4393 LoopEntryPredicate->getSuccessor(0) != PredecessorDest))
4391 if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS,
4392 LoopEntryPredicate->getSuccessor(0) != PredecessorDest))
43944393 return true;
43954394 }
43964395
43974396 return false;
43984397 }
43994398
4400 /// isNecessaryCond - Test whether the condition described by Pred, LHS,
4401 /// and RHS is a necessary condition for the given Cond value to evaluate
4402 /// to true.
4403 bool ScalarEvolution::isNecessaryCond(Value *CondValue,
4404 ICmpInst::Predicate Pred,
4405 const SCEV *LHS, const SCEV *RHS,
4406 bool Inverse) {
4399 /// isImpliedCond - Test whether the condition described by Pred, LHS,
4400 /// and RHS is true whenever the given Cond value evaluates to true.
4401 bool ScalarEvolution::isImpliedCond(Value *CondValue,
4402 ICmpInst::Predicate Pred,
4403 const SCEV *LHS, const SCEV *RHS,
4404 bool Inverse) {
44074405 // Recursivly handle And and Or conditions.
44084406 if (BinaryOperator *BO = dyn_cast(CondValue)) {
44094407 if (BO->getOpcode() == Instruction::And) {
44104408 if (!Inverse)
4411 return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4412 isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
4409 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4410 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
44134411 } else if (BO->getOpcode() == Instruction::Or) {
44144412 if (Inverse)
4415 return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4416 isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
4413 return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4414 isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
44174415 }
44184416 }
44194417
44204418 ICmpInst *ICI = dyn_cast(CondValue);
44214419 if (!ICI) return false;
4422
4423 // Now that we found a conditional branch that dominates the loop, check to
4424 // see if it is the comparison we are looking for.
4425 Value *PreCondLHS = ICI->getOperand(0);
4426 Value *PreCondRHS = ICI->getOperand(1);
4427 ICmpInst::Predicate FoundPred;
4428 if (Inverse)
4429 FoundPred = ICI->getInversePredicate();
4430 else
4431 FoundPred = ICI->getPredicate();
4432
4433 if (FoundPred == Pred)
4434 ; // An exact match.
4435 else if (!ICmpInst::isTrueWhenEqual(FoundPred) && Pred == ICmpInst::ICMP_NE) {
4436 // The actual condition is beyond sufficient.
4437 FoundPred = ICmpInst::ICMP_NE;
4438 // NE is symmetric but the original comparison may not be. Swap
4439 // the operands if necessary so that they match below.
4440 if (isa(LHS))
4441 std::swap(PreCondLHS, PreCondRHS);
4442 } else
4443 // Check a few special cases.
4444 switch (FoundPred) {
4445 case ICmpInst::ICMP_UGT:
4446 if (Pred == ICmpInst::ICMP_ULT) {
4447 std::swap(PreCondLHS, PreCondRHS);
4448 FoundPred = ICmpInst::ICMP_ULT;
4449 break;
4450 }
4451 return false;
4452 case ICmpInst::ICMP_SGT:
4453 if (Pred == ICmpInst::ICMP_SLT) {
4454 std::swap(PreCondLHS, PreCondRHS);
4455 FoundPred = ICmpInst::ICMP_SLT;
4456 break;
4457 }
4458 return false;
4459 case ICmpInst::ICMP_NE:
4460 // Expressions like (x >u 0) are often canonicalized to (x != 0),
4461 // so check for this case by checking if the NE is comparing against
4462 // a minimum or maximum constant.
4463 if (!ICmpInst::isTrueWhenEqual(Pred))
4464 if (const SCEVConstant *C = dyn_cast(RHS)) {
4465 const APInt &A = C->getValue()->getValue();
4466 switch (Pred) {
4467 case ICmpInst::ICMP_SLT:
4468 if (A.isMaxSignedValue()) break;
4469 return false;
4470 case ICmpInst::ICMP_SGT:
4471 if (A.isMinSignedValue()) break;
4472 return false;
4473 case ICmpInst::ICMP_ULT:
4474 if (A.isMaxValue()) break;
4475 return false;
4476 case ICmpInst::ICMP_UGT:
4477 if (A.isMinValue()) break;
4478 return false;
4479 default:
4480 return false;
4481 }
4482 FoundPred = Pred;
4483 // NE is symmetric but the original comparison may not be. Swap
4484 // the operands if necessary so that they match below.
4485 if (isa(LHS))
4486 std::swap(PreCondLHS, PreCondRHS);
4487 break;
4488 }
4489 return false;
4490 default:
4491 // We weren't able to reconcile the condition.
4492 return false;
4493 }
4494
4495 assert(Pred == FoundPred && "Conditions were not reconciled!");
44964420
44974421 // Bail if the ICmp's operands' types are wider than the needed type
44984422 // before attempting to call getSCEV on them. This avoids infinite
45004424 // exit condition information for overflow checking, which would
45014425 // lead back here.
45024426 if (getTypeSizeInBits(LHS->getType()) <
4503 getTypeSizeInBits(PreCondLHS->getType()))
4427 getTypeSizeInBits(ICI->getOperand(0)->getType()))
45044428 return false;
45054429
4506 const SCEV *FoundLHS = getSCEV(PreCondLHS);
4507 const SCEV *FoundRHS = getSCEV(PreCondRHS);
4430 // Now that we found a conditional branch that dominates the loop, check to
4431 // see if it is the comparison we are looking for.
4432 ICmpInst::Predicate FoundPred;
4433 if (Inverse)
4434 FoundPred = ICI->getInversePredicate();
4435 else
4436 FoundPred = ICI->getPredicate();
4437
4438 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
4439 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
45084440
45094441 // Balance the types. The case where FoundLHS' type is wider than
45104442 // LHS' type is checked for above.
45194451 }
45204452 }
45214453
4522 return isNecessaryCondOperands(Pred, LHS, RHS,
4523 FoundLHS, FoundRHS) ||
4454 // Canonicalize the query to match the way instcombine will have
4455 // canonicalized the comparison.
4456 // First, put a constant operand on the right.
4457 if (isa(LHS)) {
4458 std::swap(LHS, RHS);
4459 Pred = ICmpInst::getSwappedPredicate(Pred);
4460 }
4461 // Then, canonicalize comparisons with boundary cases.
4462 if (const SCEVConstant *RC = dyn_cast(RHS)) {
4463 const APInt &RA = RC->getValue()->getValue();
4464 switch (Pred) {
4465 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4466 case ICmpInst::ICMP_EQ:
4467 case ICmpInst::ICMP_NE:
4468 break;
4469 case ICmpInst::ICMP_UGE:
4470 if ((RA - 1).isMinValue()) {
4471 Pred = ICmpInst::ICMP_NE;
4472 RHS = getConstant(RA - 1);
4473 break;
4474 }
4475 if (RA.isMaxValue()) {
4476 Pred = ICmpInst::ICMP_EQ;
4477 break;
4478 }
4479 if (RA.isMinValue()) return true;
4480 break;
4481 case ICmpInst::ICMP_ULE:
4482 if ((RA + 1).isMaxValue()) {
4483 Pred = ICmpInst::ICMP_NE;
4484 RHS = getConstant(RA + 1);
4485 break;
4486 }
4487 if (RA.isMinValue()) {
4488 Pred = ICmpInst::ICMP_EQ;
4489 break;
4490 }
4491 if (RA.isMaxValue()) return true;
4492 break;
4493 case ICmpInst::ICMP_SGE:
4494 if ((RA - 1).isMinSignedValue()) {
4495 Pred = ICmpInst::ICMP_NE;
4496 RHS = getConstant(RA - 1);
4497 break;
4498 }
4499 if (RA.isMaxSignedValue()) {
4500 Pred = ICmpInst::ICMP_EQ;
4501 break;
4502 }
4503 if (RA.isMinSignedValue()) return true;
4504 break;
4505 case ICmpInst::ICMP_SLE:
4506 if ((RA + 1).isMaxSignedValue()) {
4507 Pred = ICmpInst::ICMP_NE;
4508 RHS = getConstant(RA + 1);
4509 break;
4510 }
4511 if (RA.isMinSignedValue()) {
4512 Pred = ICmpInst::ICMP_EQ;
4513 break;
4514 }
4515 if (RA.isMaxSignedValue()) return true;
4516 break;
4517 case ICmpInst::ICMP_UGT:
4518 if (RA.isMinValue()) {
4519 Pred = ICmpInst::ICMP_NE;
4520 break;
4521 }
4522 if ((RA + 1).isMaxValue()) {
4523 Pred = ICmpInst::ICMP_EQ;
4524 RHS = getConstant(RA + 1);
4525 break;
4526 }
4527 if (RA.isMaxValue()) return false;
4528 break;
4529 case ICmpInst::ICMP_ULT:
4530 if (RA.isMaxValue()) {
4531 Pred = ICmpInst::ICMP_NE;
4532 break;
4533 }
4534 if ((RA - 1).isMinValue()) {
4535 Pred = ICmpInst::ICMP_EQ;
4536 RHS = getConstant(RA - 1);
4537 break;
4538 }
4539 if (RA.isMinValue()) return false;
4540 break;
4541 case ICmpInst::ICMP_SGT:
4542 if (RA.isMinSignedValue()) {
4543 Pred = ICmpInst::ICMP_NE;
4544 break;
4545 }
4546 if ((RA + 1).isMaxSignedValue()) {
4547 Pred = ICmpInst::ICMP_EQ;
4548 RHS = getConstant(RA + 1);
4549 break;
4550 }
4551 if (RA.isMaxSignedValue()) return false;
4552 break;
4553 case ICmpInst::ICMP_SLT:
4554 if (RA.isMaxSignedValue()) {
4555 Pred = ICmpInst::ICMP_NE;
4556 break;
4557 }
4558 if ((RA - 1).isMinSignedValue()) {
4559 Pred = ICmpInst::ICMP_EQ;
4560 RHS = getConstant(RA - 1);
4561 break;
4562 }
4563 if (RA.isMinSignedValue()) return false;
4564 break;
4565 }
4566 }
4567
4568 // Check to see if we can make the LHS or RHS match.
4569 if (LHS == FoundRHS || RHS == FoundLHS) {
4570 if (isa(RHS)) {
4571 std::swap(FoundLHS, FoundRHS);
4572 FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
4573 } else {
4574 std::swap(LHS, RHS);
4575 Pred = ICmpInst::getSwappedPredicate(Pred);
4576 }
4577 }
4578
4579 // Check whether the found predicate is the same as the desired predicate.
4580 if (FoundPred == Pred)
4581 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
4582
4583 // Check whether swapping the found predicate makes it the same as the
4584 // desired predicate.
4585 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
4586 if (isa(RHS))
4587 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
4588 else
4589 return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
4590 RHS, LHS, FoundLHS, FoundRHS);
4591 }
4592
4593 // Check whether the actual condition is beyond sufficient.
4594 if (FoundPred == ICmpInst::ICMP_EQ)
4595 if (ICmpInst::isTrueWhenEqual(Pred))
4596 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
4597 return true;
4598 if (Pred == ICmpInst::ICMP_NE)
4599 if (!ICmpInst::isTrueWhenEqual(FoundPred))
4600 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
4601 return true;
4602
4603 // Otherwise assume the worst.
4604 return false;
4605 }
4606
4607 /// isImpliedCondOperands - Test whether the condition described by Pred,
4608 /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
4609 /// and FoundRHS is true.
4610 bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
4611 const SCEV *LHS, const SCEV *RHS,
4612 const SCEV *FoundLHS,
4613 const SCEV *FoundRHS) {
4614 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
4615 FoundLHS, FoundRHS) ||
45244616 // ~x < ~y --> x > y
4525 isNecessaryCondOperands(Pred, LHS, RHS,
4526 getNotSCEV(FoundRHS), getNotSCEV(FoundLHS));
4527 }
4528
4529 /// isNecessaryCondOperands - Test whether the condition described by Pred,
4530 /// LHS, and RHS is a necessary condition for the condition described by
4531 /// Pred, FoundLHS, and FoundRHS to evaluate to true.
4617 isImpliedCondOperandsHelper(Pred, LHS, RHS,
4618 getNotSCEV(FoundRHS),
4619 getNotSCEV(FoundLHS));
4620 }
4621
4622 /// isImpliedCondOperandsHelper - Test whether the condition described by
4623 /// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
4624 /// FoundLHS, and FoundRHS is true.
45324625 bool
4533 ScalarEvolution::isNecessaryCondOperands(ICmpInst::Predicate Pred,
4534 const SCEV *LHS, const SCEV *RHS,
4535 const SCEV *FoundLHS,
4536 const SCEV *FoundRHS) {
4626 ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
4627 const SCEV *LHS, const SCEV *RHS,
4628 const SCEV *FoundLHS,
4629 const SCEV *FoundRHS) {
45374630 switch (Pred) {
45384631 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
45394632 case ICmpInst::ICMP_EQ:
0 ; RUN: llvm-as < %s | opt -analyze -scalar-evolution -disable-output \
1 ; RUN: | grep {Loop for\\.body: backedge-taken count is (-1 + \[%\]ecx)}
2 ; PR4599
3
4 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
5
6 define i32 @foo(i32 %ecx) nounwind {
7 entry:
8 %cmp2 = icmp eq i32 %ecx, 0 ; [#uses=1]
9 br i1 %cmp2, label %for.end, label %bb.nph
10
11 for.cond: ; preds = %for.inc
12 %cmp = icmp ult i32 %inc, %ecx ; [#uses=1]
13 br i1 %cmp, label %for.body, label %for.cond.for.end_crit_edge
14
15 for.cond.for.end_crit_edge: ; preds = %for.cond
16 %phitmp = add i32 %i.01, 2 ; [#uses=1]
17 br label %for.end
18
19 bb.nph: ; preds = %entry
20 br label %for.body
21
22 for.body: ; preds = %bb.nph, %for.cond
23 %i.01 = phi i32 [ %inc, %for.cond ], [ 0, %bb.nph ] ; [#uses=3]
24 %call = call i32 @bar(i32 %i.01) nounwind ; [#uses=0]
25 br label %for.inc
26
27 for.inc: ; preds = %for.body
28 %inc = add i32 %i.01, 1 ; [#uses=2]
29 br label %for.cond
30
31 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
32 %i.0.lcssa = phi i32 [ %phitmp, %for.cond.for.end_crit_edge ], [ 1, %entry ] ; [#uses=1]
33 ret i32 %i.0.lcssa
34 }
35
36 declare i32 @bar(i32)