Revert r283057 and r283058

They've broken the sanitizer-bootstrap bots.  Reverting while I investigate.

Original commit messages:

r283057: "[ConstantRange] Make getEquivalentICmp smarter"

r283058: "[SCEV] Rely on ConstantRange instead of custom logic; NFCI"
llvm-svn: 283062
This commit is contained in:
Sanjoy Das 2016-10-02 02:40:27 +00:00
parent 35dddafa6b
commit ce09fb6c0a
3 changed files with 136 additions and 103 deletions

View File

@ -7337,77 +7337,149 @@ bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
// cases, and canonicalize *-or-equal comparisons to regular comparisons. // cases, and canonicalize *-or-equal comparisons to regular comparisons.
if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
const APInt &RA = RC->getAPInt(); const APInt &RA = RC->getAPInt();
switch (Pred) {
bool SimplifiedByConstantRange = false; default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
case ICmpInst::ICMP_EQ:
if (!ICmpInst::isEquality(Pred)) { case ICmpInst::ICMP_NE:
ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA); // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
if (ExactCR.isFullSet()) if (!RA)
goto trivially_true; if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
else if (ExactCR.isEmptySet()) if (const SCEVMulExpr *ME = dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
goto trivially_false; if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
ME->getOperand(0)->isAllOnesValue()) {
APInt NewRHS; RHS = AE->getOperand(1);
CmpInst::Predicate NewPred; LHS = ME->getOperand(1);
if (ExactCR.getEquivalentICmp(NewPred, NewRHS) && Changed = true;
ICmpInst::isEquality(NewPred)) { }
// We were able to convert an inequality to an equality. break;
Pred = NewPred; case ICmpInst::ICMP_UGE:
RHS = getConstant(NewRHS); if ((RA - 1).isMinValue()) {
Changed = SimplifiedByConstantRange = true; Pred = ICmpInst::ICMP_NE;
RHS = getConstant(RA - 1);
Changed = true;
break;
} }
} if (RA.isMaxValue()) {
Pred = ICmpInst::ICMP_EQ;
if (!SimplifiedByConstantRange) {
switch (Pred) {
default:
break;
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_NE:
// Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
if (!RA)
if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
if (const SCEVMulExpr *ME =
dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
ME->getOperand(0)->isAllOnesValue()) {
RHS = AE->getOperand(1);
LHS = ME->getOperand(1);
Changed = true;
}
break;
// The "Should have been caught earlier!" messages refer to the fact
// that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
// should have fired on the corresponding cases, and canonicalized the
// check to trivially_true or trivially_false.
case ICmpInst::ICMP_UGE:
assert(!RA.isMinValue() && "Should have been caught earlier!");
Pred = ICmpInst::ICMP_UGT;
RHS = getConstant(RA - 1);
Changed = true; Changed = true;
break; break;
case ICmpInst::ICMP_ULE: }
assert(!RA.isMaxValue() && "Should have been caught earlier!"); if (RA.isMinValue()) goto trivially_true;
Pred = ICmpInst::ICMP_ULT;
RHS = getConstant(RA + 1); Pred = ICmpInst::ICMP_UGT;
Changed = true; RHS = getConstant(RA - 1);
break; Changed = true;
case ICmpInst::ICMP_SGE: break;
assert(!RA.isMinSignedValue() && "Should have been caught earlier!"); case ICmpInst::ICMP_ULE:
Pred = ICmpInst::ICMP_SGT; if ((RA + 1).isMaxValue()) {
RHS = getConstant(RA - 1); Pred = ICmpInst::ICMP_NE;
Changed = true;
break;
case ICmpInst::ICMP_SLE:
assert(!RA.isMaxSignedValue() && "Should have been caught earlier!");
Pred = ICmpInst::ICMP_SLT;
RHS = getConstant(RA + 1); RHS = getConstant(RA + 1);
Changed = true; Changed = true;
break; break;
} }
if (RA.isMinValue()) {
Pred = ICmpInst::ICMP_EQ;
Changed = true;
break;
}
if (RA.isMaxValue()) goto trivially_true;
Pred = ICmpInst::ICMP_ULT;
RHS = getConstant(RA + 1);
Changed = true;
break;
case ICmpInst::ICMP_SGE:
if ((RA - 1).isMinSignedValue()) {
Pred = ICmpInst::ICMP_NE;
RHS = getConstant(RA - 1);
Changed = true;
break;
}
if (RA.isMaxSignedValue()) {
Pred = ICmpInst::ICMP_EQ;
Changed = true;
break;
}
if (RA.isMinSignedValue()) goto trivially_true;
Pred = ICmpInst::ICMP_SGT;
RHS = getConstant(RA - 1);
Changed = true;
break;
case ICmpInst::ICMP_SLE:
if ((RA + 1).isMaxSignedValue()) {
Pred = ICmpInst::ICMP_NE;
RHS = getConstant(RA + 1);
Changed = true;
break;
}
if (RA.isMinSignedValue()) {
Pred = ICmpInst::ICMP_EQ;
Changed = true;
break;
}
if (RA.isMaxSignedValue()) goto trivially_true;
Pred = ICmpInst::ICMP_SLT;
RHS = getConstant(RA + 1);
Changed = true;
break;
case ICmpInst::ICMP_UGT:
if (RA.isMinValue()) {
Pred = ICmpInst::ICMP_NE;
Changed = true;
break;
}
if ((RA + 1).isMaxValue()) {
Pred = ICmpInst::ICMP_EQ;
RHS = getConstant(RA + 1);
Changed = true;
break;
}
if (RA.isMaxValue()) goto trivially_false;
break;
case ICmpInst::ICMP_ULT:
if (RA.isMaxValue()) {
Pred = ICmpInst::ICMP_NE;
Changed = true;
break;
}
if ((RA - 1).isMinValue()) {
Pred = ICmpInst::ICMP_EQ;
RHS = getConstant(RA - 1);
Changed = true;
break;
}
if (RA.isMinValue()) goto trivially_false;
break;
case ICmpInst::ICMP_SGT:
if (RA.isMinSignedValue()) {
Pred = ICmpInst::ICMP_NE;
Changed = true;
break;
}
if ((RA + 1).isMaxSignedValue()) {
Pred = ICmpInst::ICMP_EQ;
RHS = getConstant(RA + 1);
Changed = true;
break;
}
if (RA.isMaxSignedValue()) goto trivially_false;
break;
case ICmpInst::ICMP_SLT:
if (RA.isMaxSignedValue()) {
Pred = ICmpInst::ICMP_NE;
Changed = true;
break;
}
if ((RA - 1).isMinSignedValue()) {
Pred = ICmpInst::ICMP_EQ;
RHS = getConstant(RA - 1);
Changed = true;
break;
}
if (RA.isMinSignedValue()) goto trivially_false;
break;
} }
} }

View File

@ -147,14 +147,6 @@ bool ConstantRange::getEquivalentICmp(CmpInst::Predicate &Pred,
Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE; Pred = isEmptySet() ? CmpInst::ICMP_ULT : CmpInst::ICMP_UGE;
RHS = APInt(getBitWidth(), 0); RHS = APInt(getBitWidth(), 0);
Success = true; Success = true;
} else if (auto *OnlyElt = getSingleElement()) {
Pred = CmpInst::ICMP_EQ;
RHS = *OnlyElt;
Success = true;
} else if (auto *OnlyMissingElt = inverse().getSingleElement()) {
Pred = CmpInst::ICMP_NE;
RHS = *OnlyMissingElt;
Success = true;
} else if (getLower().isMinSignedValue() || getLower().isMinValue()) { } else if (getLower().isMinSignedValue() || getLower().isMinValue()) {
Pred = Pred =
getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT; getLower().isMinSignedValue() ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT;

View File

@ -760,37 +760,6 @@ TEST(ConstantRange, GetEquivalentICmp) {
EXPECT_FALSE(ConstantRange(APInt::getMinValue(32) - APInt(32, 100), EXPECT_FALSE(ConstantRange(APInt::getMinValue(32) - APInt(32, 100),
APInt::getMinValue(32) + APInt(32, 100)) APInt::getMinValue(32) + APInt(32, 100))
.getEquivalentICmp(Pred, RHS)); .getEquivalentICmp(Pred, RHS));
EXPECT_TRUE(ConstantRange(APInt(32, 100)).getEquivalentICmp(Pred, RHS));
EXPECT_EQ(Pred, CmpInst::ICMP_EQ);
EXPECT_EQ(RHS, APInt(32, 100));
EXPECT_TRUE(
ConstantRange(APInt(32, 100)).inverse().getEquivalentICmp(Pred, RHS));
EXPECT_EQ(Pred, CmpInst::ICMP_NE);
EXPECT_EQ(RHS, APInt(32, 100));
// NB! It would be correct for the following four calls to getEquivalentICmp
// to return ordered predicates like CmpInst::ICMP_ULT or CmpInst::ICMP_UGT.
// However, that's not the case today.
EXPECT_TRUE(ConstantRange(APInt(32, 0)).getEquivalentICmp(Pred, RHS));
EXPECT_EQ(Pred, CmpInst::ICMP_EQ);
EXPECT_EQ(RHS, APInt(32, 0));
EXPECT_TRUE(
ConstantRange(APInt(32, 0)).inverse().getEquivalentICmp(Pred, RHS));
EXPECT_EQ(Pred, CmpInst::ICMP_NE);
EXPECT_EQ(RHS, APInt(32, 0));
EXPECT_TRUE(ConstantRange(APInt(32, -1)).getEquivalentICmp(Pred, RHS));
EXPECT_EQ(Pred, CmpInst::ICMP_EQ);
EXPECT_EQ(RHS, APInt(32, -1));
EXPECT_TRUE(
ConstantRange(APInt(32, -1)).inverse().getEquivalentICmp(Pred, RHS));
EXPECT_EQ(Pred, CmpInst::ICMP_NE);
EXPECT_EQ(RHS, APInt(32, -1));
} }
} // anonymous namespace } // anonymous namespace