@@ -101,6 +101,58 @@ ConstantRangeList GetCRL(ArrayRef<std::pair<APInt, APInt>> Pairs) {
101
101
return ConstantRangeList (Ranges);
102
102
}
103
103
104
+ TEST_F (ConstantRangeListTest, Subtract) {
105
+ APInt AP0 = APInt (64 , 0 , /* isSigned=*/ true );
106
+ APInt AP2 = APInt (64 , 2 , /* isSigned=*/ true );
107
+ APInt AP3 = APInt (64 , 3 , /* isSigned=*/ true );
108
+ APInt AP4 = APInt (64 , 4 , /* isSigned=*/ true );
109
+ APInt AP8 = APInt (64 , 8 , /* isSigned=*/ true );
110
+ APInt AP10 = APInt (64 , 10 , /* isSigned=*/ true );
111
+ APInt AP11 = APInt (64 , 11 , /* isSigned=*/ true );
112
+ APInt AP12 = APInt (64 , 12 , /* isSigned=*/ true );
113
+ ConstantRangeList CRL = GetCRL ({{AP0, AP4}, {AP8, AP12}});
114
+
115
+ // Execute ConstantRangeList::subtract(ConstantRange) and check the result
116
+ // is expected. Pass "CRL" by value so that subtract() does not affect the
117
+ // argument in caller.
118
+ auto SubtractAndCheck = [](ConstantRangeList CRL,
119
+ const std::pair<int64_t , int64_t > &Range,
120
+ const ConstantRangeList &ExpectedCRL) {
121
+ CRL.subtract (ConstantRange (APInt (64 , Range.first , /* isSigned=*/ true ),
122
+ APInt (64 , Range.second , /* isSigned=*/ true )));
123
+ EXPECT_EQ (CRL, ExpectedCRL);
124
+ };
125
+
126
+ // No overlap
127
+ SubtractAndCheck (CRL, {-4 , 0 }, CRL);
128
+ SubtractAndCheck (CRL, {4 , 8 }, CRL);
129
+ SubtractAndCheck (CRL, {12 , 16 }, CRL);
130
+
131
+ // Overlap (left, right, or both)
132
+ SubtractAndCheck (CRL, {-4 , 2 }, GetCRL ({{AP2, AP4}, {AP8, AP12}}));
133
+ SubtractAndCheck (CRL, {-4 , 4 }, GetCRL ({{AP8, AP12}}));
134
+ SubtractAndCheck (CRL, {-4 , 8 }, GetCRL ({{AP8, AP12}}));
135
+ SubtractAndCheck (CRL, {0 , 2 }, GetCRL ({{AP2, AP4}, {AP8, AP12}}));
136
+ SubtractAndCheck (CRL, {0 , 4 }, GetCRL ({{AP8, AP12}}));
137
+ SubtractAndCheck (CRL, {0 , 8 }, GetCRL ({{AP8, AP12}}));
138
+ SubtractAndCheck (CRL, {10 , 12 }, GetCRL ({{AP0, AP4}, {AP8, AP10}}));
139
+ SubtractAndCheck (CRL, {8 , 12 }, GetCRL ({{AP0, AP4}}));
140
+ SubtractAndCheck (CRL, {6 , 12 }, GetCRL ({{AP0, AP4}}));
141
+ SubtractAndCheck (CRL, {10 , 16 }, GetCRL ({{AP0, AP4}, {AP8, AP10}}));
142
+ SubtractAndCheck (CRL, {8 , 16 }, GetCRL ({{AP0, AP4}}));
143
+ SubtractAndCheck (CRL, {6 , 16 }, GetCRL ({{AP0, AP4}}));
144
+ SubtractAndCheck (CRL, {2 , 10 }, GetCRL ({{AP0, AP2}, {AP10, AP12}}));
145
+
146
+ // Subset
147
+ SubtractAndCheck (CRL, {2 , 3 }, GetCRL ({{AP0, AP2}, {AP3, AP4}, {AP8, AP12}}));
148
+ SubtractAndCheck (CRL, {10 , 11 },
149
+ GetCRL ({{AP0, AP4}, {AP8, AP10}, {AP11, AP12}}));
150
+
151
+ // Superset
152
+ SubtractAndCheck (CRL, {0 , 12 }, GetCRL ({}));
153
+ SubtractAndCheck (CRL, {-4 , 16 }, GetCRL ({}));
154
+ }
155
+
104
156
TEST_F (ConstantRangeListTest, Union) {
105
157
APInt APN4 = APInt (64 , -4 , /* isSigned=*/ true );
106
158
APInt APN2 = APInt (64 , -2 , /* isSigned=*/ true );
0 commit comments