@@ -313,7 +313,8 @@ class ConstraintInfo {
313313 // / New variables that need to be added to the system are collected in
314314 // / \p NewVariables.
315315 ConstraintTy getConstraint (CmpInst::Predicate Pred, Value *Op0, Value *Op1,
316- SmallVectorImpl<Value *> &NewVariables) const ;
316+ SmallVectorImpl<Value *> &NewVariables,
317+ bool ForceSignedSystem = false ) const ;
317318
318319 // / Turns a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
319320 // / constraints using getConstraint. Returns an empty constraint if the result
@@ -330,6 +331,14 @@ class ConstraintInfo {
330331 void transferToOtherSystem (CmpInst::Predicate Pred, Value *A, Value *B,
331332 unsigned NumIn, unsigned NumOut,
332333 SmallVectorImpl<StackEntry> &DFSInStack);
334+
335+ private:
336+ // / Adds facts into constraint system. \p ForceSignedSystem can be set when
337+ // / the \p Pred is eq/ne, and signed constraint system is used when it's
338+ // / specified.
339+ void addFactImpl (CmpInst::Predicate Pred, Value *A, Value *B, unsigned NumIn,
340+ unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack,
341+ bool ForceSignedSystem);
333342};
334343
335344// / Represents a (Coefficient * Variable) entry after IR decomposition.
@@ -636,8 +645,12 @@ static Decomposition decompose(Value *V,
636645
637646ConstraintTy
638647ConstraintInfo::getConstraint (CmpInst::Predicate Pred, Value *Op0, Value *Op1,
639- SmallVectorImpl<Value *> &NewVariables) const {
648+ SmallVectorImpl<Value *> &NewVariables,
649+ bool ForceSignedSystem) const {
640650 assert (NewVariables.empty () && " NewVariables must be empty when passed in" );
651+ assert ((!ForceSignedSystem || CmpInst::isEquality (Pred)) &&
652+ " signed system can only be forced on eq/ne" );
653+
641654 bool IsEq = false ;
642655 bool IsNe = false ;
643656
@@ -652,15 +665,15 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
652665 break ;
653666 }
654667 case CmpInst::ICMP_EQ:
655- if (match (Op1, m_Zero ())) {
668+ if (!ForceSignedSystem && match (Op1, m_Zero ())) {
656669 Pred = CmpInst::ICMP_ULE;
657670 } else {
658671 IsEq = true ;
659672 Pred = CmpInst::ICMP_ULE;
660673 }
661674 break ;
662675 case CmpInst::ICMP_NE:
663- if (match (Op1, m_Zero ())) {
676+ if (!ForceSignedSystem && match (Op1, m_Zero ())) {
664677 Pred = CmpInst::getSwappedPredicate (CmpInst::ICMP_UGT);
665678 std::swap (Op0, Op1);
666679 } else {
@@ -677,7 +690,7 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
677690 return {};
678691
679692 SmallVector<ConditionTy, 4 > Preconditions;
680- bool IsSigned = CmpInst::isSigned (Pred);
693+ bool IsSigned = ForceSignedSystem || CmpInst::isSigned (Pred);
681694 auto &Value2Index = getValue2Index (IsSigned);
682695 auto ADec = decompose (Op0->stripPointerCastsSameRepresentation (),
683696 Preconditions, IsSigned, DL);
@@ -737,7 +750,7 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
737750 int64_t OffsetSum;
738751 if (AddOverflow (Offset1, Offset2, OffsetSum))
739752 return {};
740- if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT) )
753+ if (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT)
741754 if (AddOverflow (OffsetSum, int64_t (-1 ), OffsetSum))
742755 return {};
743756 R[0 ] = OffsetSum;
@@ -1580,10 +1593,20 @@ static bool checkOrAndOpImpliedByOther(
15801593void ConstraintInfo::addFact (CmpInst::Predicate Pred, Value *A, Value *B,
15811594 unsigned NumIn, unsigned NumOut,
15821595 SmallVectorImpl<StackEntry> &DFSInStack) {
1596+ addFactImpl (Pred, A, B, NumIn, NumOut, DFSInStack, false );
1597+ // If the Pred is eq/ne, also add the fact to signed system.
1598+ if (CmpInst::isEquality (Pred))
1599+ addFactImpl (Pred, A, B, NumIn, NumOut, DFSInStack, true );
1600+ }
1601+
1602+ void ConstraintInfo::addFactImpl (CmpInst::Predicate Pred, Value *A, Value *B,
1603+ unsigned NumIn, unsigned NumOut,
1604+ SmallVectorImpl<StackEntry> &DFSInStack,
1605+ bool ForceSignedSystem) {
15831606 // If the constraint has a pre-condition, skip the constraint if it does not
15841607 // hold.
15851608 SmallVector<Value *> NewVariables;
1586- auto R = getConstraint (Pred, A, B, NewVariables);
1609+ auto R = getConstraint (Pred, A, B, NewVariables, ForceSignedSystem );
15871610
15881611 // TODO: Support non-equality for facts as well.
15891612 if (!R.isValid (*this ) || R.isNe ())
0 commit comments