@@ -168,13 +168,22 @@ class ConstraintInfo {
168
168
SmallVectorImpl<StackEntry> &DFSInStack);
169
169
};
170
170
171
+ // / Represents a (Coefficient * Variable) entry after IR decomposition.
172
+ struct DecompEntry {
173
+ int64_t Coefficient;
174
+ Value *Variable;
175
+
176
+ DecompEntry (int64_t Coefficient, Value *Variable)
177
+ : Coefficient(Coefficient), Variable(Variable) {}
178
+ };
179
+
171
180
} // namespace
172
181
173
- // Decomposes \p V into a vector of pairs of the form { c, X } where c * X. The
174
- // sum of the pairs equals \p V. The first pair is the constant-factor and X
175
- // must be nullptr. If the expression cannot be decomposed, returns an empty
176
- // vector.
177
- static SmallVector<std::pair< int64_t , Value *> , 4 >
182
+ // Decomposes \p V into a vector of entries of the form { Coefficient, Variable
183
+ // } where Coefficient * Variable. The sum of the pairs equals \p V. The first
184
+ // pair is the constant-factor and X must be nullptr. If the expression cannot
185
+ // be decomposed, returns an empty vector.
186
+ static SmallVector<DecompEntry , 4 >
178
187
decompose (Value *V, SmallVector<PreconditionTy, 4 > &Preconditions,
179
188
bool IsSigned) {
180
189
@@ -195,7 +204,7 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
195
204
if (auto *CI = dyn_cast<ConstantInt>(V)) {
196
205
if (CI->uge (MaxConstraintValue))
197
206
return {};
198
- return {{CI->getZExtValue (), nullptr }};
207
+ return {{int64_t ( CI->getZExtValue () ), nullptr }};
199
208
}
200
209
auto *GEP = dyn_cast<GetElementPtrInst>(V);
201
210
if (GEP && GEP->getNumOperands () == 2 && GEP->isInBounds ()) {
@@ -209,7 +218,7 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
209
218
CanUseSExt (CI))
210
219
return {{0 , nullptr },
211
220
{1 , GEP->getPointerOperand ()},
212
- {std::pow (int64_t (2 ), CI->getSExtValue ()), Op1}};
221
+ {int64_t ( std::pow (int64_t (2 ), CI->getSExtValue () )), Op1}};
213
222
if (match (Op0, m_NSWAdd (m_Value (Op1), m_ConstantInt (CI))) &&
214
223
CanUseSExt (CI))
215
224
return {{CI->getSExtValue (), nullptr },
@@ -222,13 +231,13 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
222
231
!CI->isNegative () && CanUseSExt (CI))
223
232
return {{CI->getSExtValue (), nullptr }, {1 , GEP->getPointerOperand ()}};
224
233
225
- SmallVector<std::pair< int64_t , Value *> , 4 > Result;
234
+ SmallVector<DecompEntry , 4 > Result;
226
235
if (match (GEP->getOperand (GEP->getNumOperands () - 1 ),
227
236
m_NUWShl (m_Value (Op0), m_ConstantInt (CI))) &&
228
237
CanUseSExt (CI))
229
238
Result = {{0 , nullptr },
230
239
{1 , GEP->getPointerOperand ()},
231
- {std::pow (int64_t (2 ), CI->getSExtValue ()), Op0}};
240
+ {int ( std::pow (int64_t (2 ), CI->getSExtValue () )), Op0}};
232
241
else if (match (GEP->getOperand (GEP->getNumOperands () - 1 ),
233
242
m_NSWAdd (m_Value (Op0), m_ConstantInt (CI))) &&
234
243
CanUseSExt (CI))
@@ -254,7 +263,7 @@ decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
254
263
ConstantInt *CI;
255
264
if (match (V, m_NUWAdd (m_Value (Op0), m_ConstantInt (CI))) &&
256
265
!CI->uge (MaxConstraintValue))
257
- return {{CI->getZExtValue (), nullptr }, {1 , Op0}};
266
+ return {{int ( CI->getZExtValue () ), nullptr }, {1 , Op0}};
258
267
if (match (V, m_Add (m_Value (Op0), m_ConstantInt (CI))) && CI->isNegative () &&
259
268
CanUseSExt (CI)) {
260
269
Preconditions.emplace_back (
@@ -321,8 +330,8 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
321
330
if (ADec.empty () || BDec.empty ())
322
331
return {};
323
332
324
- int64_t Offset1 = ADec[0 ].first ;
325
- int64_t Offset2 = BDec[0 ].first ;
333
+ int64_t Offset1 = ADec[0 ].Coefficient ;
334
+ int64_t Offset2 = BDec[0 ].Coefficient ;
326
335
Offset1 *= -1 ;
327
336
328
337
// Create iterator ranges that skip the constant-factor.
@@ -341,9 +350,8 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
341
350
};
342
351
343
352
// Make sure all variables have entries in Value2Index or NewIndices.
344
- for (const auto &KV :
345
- concat<std::pair<int64_t , Value *>>(VariablesA, VariablesB))
346
- GetOrAddIndex (KV.second );
353
+ for (const auto &KV : concat<DecompEntry>(VariablesA, VariablesB))
354
+ GetOrAddIndex (KV.Variable );
347
355
348
356
// Build result constraint, by first adding all coefficients from A and then
349
357
// subtracting all coefficients from B.
@@ -353,10 +361,10 @@ ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
353
361
Res.IsEq = IsEq;
354
362
auto &R = Res.Coefficients ;
355
363
for (const auto &KV : VariablesA)
356
- R[GetOrAddIndex (KV.second )] += KV.first ;
364
+ R[GetOrAddIndex (KV.Variable )] += KV.Coefficient ;
357
365
358
366
for (const auto &KV : VariablesB)
359
- R[GetOrAddIndex (KV.second )] -= KV.first ;
367
+ R[GetOrAddIndex (KV.Variable )] -= KV.Coefficient ;
360
368
361
369
int64_t OffsetSum;
362
370
if (AddOverflow (Offset1, Offset2, OffsetSum))
0 commit comments