@@ -205,6 +205,10 @@ class FlatLinearConstraints : public presburger::IntegerPolyhedron {
205
205
// / where each non-local variable can have an SSA Value attached to it.
206
206
class FlatLinearValueConstraints : public FlatLinearConstraints {
207
207
public:
208
+ // / The SSA Values attached to each non-local variable are stored as
209
+ // / identifiers in the constraint system's space.
210
+ using Identifier = presburger::Identifier;
211
+
208
212
// / Constructs a constraint system reserving memory for the specified number
209
213
// / of constraints and variables. `valArgs` are the optional SSA values
210
214
// / associated with each dimension/symbol. These must either be empty or match
@@ -217,11 +221,11 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
217
221
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
218
222
numReservedCols, numDims, numSymbols, numLocals) {
219
223
assert (valArgs.empty () || valArgs.size () == getNumDimAndSymbolVars ());
220
- values. reserve (numReservedCols);
221
- if (valArgs. empty ())
222
- values. resize ( getNumDimAndSymbolVars (), std::nullopt);
223
- else
224
- values. append (valArgs. begin (), valArgs. end () );
224
+ // Store Values in space's identifiers.
225
+ space. resetIds ();
226
+ for ( unsigned i = 0 , e = valArgs. size (); i < e; ++i)
227
+ if (valArgs[i])
228
+ setValue (i, * valArgs[i] );
225
229
}
226
230
227
231
// / Constructs a constraint system reserving memory for the specified number
@@ -236,11 +240,11 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
236
240
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
237
241
numReservedCols, numDims, numSymbols, numLocals) {
238
242
assert (valArgs.empty () || valArgs.size () == getNumDimAndSymbolVars ());
239
- values. reserve (numReservedCols);
240
- if (valArgs. empty ())
241
- values. resize ( getNumDimAndSymbolVars (), std::nullopt);
242
- else
243
- values. append (valArgs. begin () , valArgs. end () );
243
+ // Store Values in space's identifiers.
244
+ space. resetIds ();
245
+ for ( unsigned i = 0 , e = valArgs. size (); i < e; ++i)
246
+ if (valArgs[i])
247
+ setValue (i , valArgs[i] );
244
248
}
245
249
246
250
// / Constructs a constraint system with the specified number of dimensions
@@ -272,11 +276,15 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
272
276
FlatLinearValueConstraints (const IntegerPolyhedron &fac,
273
277
ArrayRef<std::optional<Value>> valArgs = {})
274
278
: FlatLinearConstraints(fac) {
275
- assert (valArgs. empty () || valArgs. size () == getNumDimAndSymbolVars ());
279
+ // Do not reset values assigned by FlatLinearConstraints' constructor.
276
280
if (valArgs.empty ())
277
- values.resize (getNumDimAndSymbolVars (), std::nullopt);
278
- else
279
- values.append (valArgs.begin (), valArgs.end ());
281
+ return ;
282
+ assert (valArgs.size () == getNumDimAndSymbolVars ());
283
+ // Store Values in space's identifiers.
284
+ space.resetIds ();
285
+ for (unsigned i = 0 , e = valArgs.size (); i < e; ++i)
286
+ if (valArgs[i])
287
+ setValue (i, *valArgs[i]);
280
288
}
281
289
282
290
// / Creates an affine constraint system from an IntegerSet.
@@ -290,9 +298,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
290
298
cst->getKind () <= Kind::FlatAffineRelation;
291
299
}
292
300
293
- // / Replaces the contents of this FlatLinearValueConstraints with `other`.
294
- void clearAndCopyFrom (const IntegerRelation &other) override ;
295
-
296
301
// / Adds a constant bound for the variable associated with the given Value.
297
302
void addBound (presburger::BoundType type, Value val, int64_t value);
298
303
using FlatLinearConstraints::addBound;
@@ -302,7 +307,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
302
307
inline Value getValue (unsigned pos) const {
303
308
assert (pos < getNumDimAndSymbolVars () && " Invalid position" );
304
309
assert (hasValue (pos) && " variable's Value not set" );
305
- return *values[pos];
310
+ VarKind kind = getVarKindAt (pos);
311
+ unsigned relativePos = pos - getVarKindOffset (kind);
312
+ return space.getId (kind, relativePos).getValue <Value>();
306
313
}
307
314
308
315
// / Returns the Values associated with variables in range [start, end).
@@ -313,25 +320,44 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
313
320
assert (start <= end && " invalid start position" );
314
321
values->clear ();
315
322
values->reserve (end - start);
316
- for (unsigned i = start; i < end; i++ )
323
+ for (unsigned i = start; i < end; ++i )
317
324
values->push_back (getValue (i));
318
325
}
319
326
320
- inline ArrayRef<std::optional<Value>> getMaybeValues () const {
321
- return {values.data (), values.size ()};
327
+ inline SmallVector<std::optional<Value>> getMaybeValues () const {
328
+ SmallVector<std::optional<Value>> maybeValues;
329
+ maybeValues.reserve (getNumDimAndSymbolVars ());
330
+ for (unsigned i = 0 , e = getNumDimAndSymbolVars (); i < e; ++i)
331
+ if (hasValue (i)) {
332
+ maybeValues.push_back (getValue (i));
333
+ } else {
334
+ maybeValues.push_back (std::nullopt);
335
+ }
336
+ return maybeValues;
322
337
}
323
338
324
- inline ArrayRef <std::optional<Value>>
339
+ inline SmallVector <std::optional<Value>>
325
340
getMaybeValues (presburger::VarKind kind) const {
326
341
assert (kind != VarKind::Local &&
327
342
" Local variables do not have any value attached to them." );
328
- return {values.data () + getVarKindOffset (kind), getNumVarKind (kind)};
343
+ SmallVector<std::optional<Value>> maybeValues;
344
+ maybeValues.reserve (getNumVarKind (kind));
345
+ const unsigned offset = space.getVarKindOffset (kind);
346
+ for (unsigned i = 0 , e = getNumVarKind (kind); i < e; ++i) {
347
+ if (hasValue (offset + i))
348
+ maybeValues.push_back (getValue (offset + i));
349
+ else
350
+ maybeValues.push_back (std::nullopt);
351
+ }
352
+ return maybeValues;
329
353
}
330
354
331
355
// / Returns true if the pos^th variable has an associated Value.
332
356
inline bool hasValue (unsigned pos) const {
333
357
assert (pos < getNumDimAndSymbolVars () && " Invalid position" );
334
- return values[pos].has_value ();
358
+ VarKind kind = getVarKindAt (pos);
359
+ unsigned relativePos = pos - getVarKindOffset (kind);
360
+ return space.getId (kind, relativePos).hasValue ();
335
361
}
336
362
337
363
unsigned appendDimVar (ValueRange vals);
@@ -358,9 +384,15 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
358
384
using IntegerPolyhedron::removeVarRange;
359
385
360
386
// / Sets the Value associated with the pos^th variable.
387
+ // / Stores the Value in the space's identifiers.
361
388
inline void setValue (unsigned pos, Value val) {
362
389
assert (pos < getNumDimAndSymbolVars () && " invalid var position" );
363
- values[pos] = val;
390
+ VarKind kind = getVarKindAt (pos);
391
+ unsigned relativePos = pos - getVarKindOffset (kind);
392
+ if (!space.isUsingIds ()) {
393
+ space.resetIds ();
394
+ }
395
+ space.getId (kind, relativePos) = presburger::Identifier (val);
364
396
}
365
397
366
398
// / Sets the Values associated with the variables in the range [start, end).
@@ -387,9 +419,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
387
419
void projectOut (Value val);
388
420
using IntegerPolyhedron::projectOut;
389
421
390
- // / Swap the posA^th variable with the posB^th variable.
391
- void swapVar (unsigned posA, unsigned posB) override ;
392
-
393
422
// / Prints the number of constraints, dimensions, symbols and locals in the
394
423
// / FlatAffineValueConstraints. Also, prints for each variable whether there
395
424
// / is an SSA Value attached to it.
@@ -444,28 +473,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
444
473
// / output = {0 <= d0 <= 6, 1 <= d1 <= 15}
445
474
LogicalResult unionBoundingBox (const FlatLinearValueConstraints &other);
446
475
using IntegerPolyhedron::unionBoundingBox;
447
-
448
- protected:
449
- // / Eliminates the variable at the specified position using Fourier-Motzkin
450
- // / variable elimination, but uses Gaussian elimination if there is an
451
- // / equality involving that variable. If the result of the elimination is
452
- // / integer exact, `*isResultIntegerExact` is set to true. If `darkShadow` is
453
- // / set to true, a potential under approximation (subset) of the rational
454
- // / shadow / exact integer shadow is computed.
455
- // See implementation comments for more details.
456
- void fourierMotzkinEliminate (unsigned pos, bool darkShadow = false ,
457
- bool *isResultIntegerExact = nullptr ) override ;
458
-
459
- // / Returns false if the fields corresponding to various variable counts, or
460
- // / equality/inequality buffer sizes aren't consistent; true otherwise. This
461
- // / is meant to be used within an assert internally.
462
- bool hasConsistentState () const override ;
463
-
464
- // / Values corresponding to the (column) non-local variables of this
465
- // / constraint system appearing in the order the variables correspond to
466
- // / columns. Variables that aren't associated with any Value are set to
467
- // / std::nullopt.
468
- SmallVector<std::optional<Value>, 8 > values;
469
476
};
470
477
471
478
// / Flattens 'expr' into 'flattenedExpr', which contains the coefficients of the
0 commit comments