@@ -313,7 +313,8 @@ class ConstraintInfo {
313
313
// / New variables that need to be added to the system are collected in
314
314
// / \p NewVariables.
315
315
ConstraintTy getConstraint (CmpInst::Predicate Pred, Value *Op0, Value *Op1,
316
- SmallVectorImpl<Value *> &NewVariables) const ;
316
+ SmallVectorImpl<Value *> &NewVariables,
317
+ bool ForceSignedSystem = false ) const ;
317
318
318
319
// / Turns a comparison of the form \p Op0 \p Pred \p Op1 into a vector of
319
320
// / constraints using getConstraint. Returns an empty constraint if the result
@@ -330,6 +331,14 @@ class ConstraintInfo {
330
331
void transferToOtherSystem (CmpInst::Predicate Pred, Value *A, Value *B,
331
332
unsigned NumIn, unsigned NumOut,
332
333
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);
333
342
};
334
343
335
344
// / Represents a (Coefficient * Variable) entry after IR decomposition.
@@ -636,8 +645,13 @@ static Decomposition decompose(Value *V,
636
645
637
646
ConstraintTy
638
647
ConstraintInfo::getConstraint (CmpInst::Predicate Pred, Value *Op0, Value *Op1,
639
- SmallVectorImpl<Value *> &NewVariables) const {
648
+ SmallVectorImpl<Value *> &NewVariables,
649
+ bool ForceSignedSystem) const {
640
650
assert (NewVariables.empty () && " NewVariables must be empty when passed in" );
651
+ assert ((!ForceSignedSystem || Pred == CmpInst::ICMP_EQ ||
652
+ Pred == CmpInst::ICMP_NE) &&
653
+ " signed system can only be forced on eq/ne" );
654
+
641
655
bool IsEq = false ;
642
656
bool IsNe = false ;
643
657
@@ -652,15 +666,15 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
652
666
break ;
653
667
}
654
668
case CmpInst::ICMP_EQ:
655
- if (match (Op1, m_Zero ())) {
669
+ if (!ForceSignedSystem && match (Op1, m_Zero ())) {
656
670
Pred = CmpInst::ICMP_ULE;
657
671
} else {
658
672
IsEq = true ;
659
673
Pred = CmpInst::ICMP_ULE;
660
674
}
661
675
break ;
662
676
case CmpInst::ICMP_NE:
663
- if (match (Op1, m_Zero ())) {
677
+ if (!ForceSignedSystem && match (Op1, m_Zero ())) {
664
678
Pred = CmpInst::getSwappedPredicate (CmpInst::ICMP_UGT);
665
679
std::swap (Op0, Op1);
666
680
} else {
@@ -677,7 +691,7 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
677
691
return {};
678
692
679
693
SmallVector<ConditionTy, 4 > Preconditions;
680
- bool IsSigned = CmpInst::isSigned (Pred);
694
+ bool IsSigned = ForceSignedSystem || CmpInst::isSigned (Pred);
681
695
auto &Value2Index = getValue2Index (IsSigned);
682
696
auto ADec = decompose (Op0->stripPointerCastsSameRepresentation (),
683
697
Preconditions, IsSigned, DL);
@@ -737,7 +751,7 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
737
751
int64_t OffsetSum;
738
752
if (AddOverflow (Offset1, Offset2, OffsetSum))
739
753
return {};
740
- if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT) )
754
+ if (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT)
741
755
if (AddOverflow (OffsetSum, int64_t (-1 ), OffsetSum))
742
756
return {};
743
757
R[0 ] = OffsetSum;
@@ -1580,10 +1594,20 @@ static bool checkOrAndOpImpliedByOther(
1580
1594
void ConstraintInfo::addFact (CmpInst::Predicate Pred, Value *A, Value *B,
1581
1595
unsigned NumIn, unsigned NumOut,
1582
1596
SmallVectorImpl<StackEntry> &DFSInStack) {
1597
+ addFactImpl (Pred, A, B, NumIn, NumOut, DFSInStack, false );
1598
+ // If the Pred is eq/ne, also add the fact to signed system.
1599
+ if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)
1600
+ addFactImpl (Pred, A, B, NumIn, NumOut, DFSInStack, true );
1601
+ }
1602
+
1603
+ void ConstraintInfo::addFactImpl (CmpInst::Predicate Pred, Value *A, Value *B,
1604
+ unsigned NumIn, unsigned NumOut,
1605
+ SmallVectorImpl<StackEntry> &DFSInStack,
1606
+ bool ForceSignedSystem) {
1583
1607
// If the constraint has a pre-condition, skip the constraint if it does not
1584
1608
// hold.
1585
1609
SmallVector<Value *> NewVariables;
1586
- auto R = getConstraint (Pred, A, B, NewVariables);
1610
+ auto R = getConstraint (Pred, A, B, NewVariables, ForceSignedSystem );
1587
1611
1588
1612
// TODO: Support non-equality for facts as well.
1589
1613
if (!R.isValid (*this ) || R.isNe ())
0 commit comments