@@ -205,6 +205,8 @@ 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
+ using Identifier = presburger::Identifier;
209
+
208
210
// / Constructs a constraint system reserving memory for the specified number
209
211
// / of constraints and variables. `valArgs` are the optional SSA values
210
212
// / associated with each dimension/symbol. These must either be empty or match
@@ -217,11 +219,11 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
217
219
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
218
220
numReservedCols, numDims, numSymbols, numLocals) {
219
221
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 () );
222
+ // Store Values in space's identifiers.
223
+ space. resetIds ();
224
+ for ( unsigned i = 0 , e = valArgs. size (); i < e; ++i)
225
+ if (valArgs[i])
226
+ setValue (i, * valArgs[i] );
225
227
}
226
228
227
229
// / Constructs a constraint system reserving memory for the specified number
@@ -236,11 +238,11 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
236
238
: FlatLinearConstraints(numReservedInequalities, numReservedEqualities,
237
239
numReservedCols, numDims, numSymbols, numLocals) {
238
240
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 () );
241
+ // Store Values in space's identifiers.
242
+ space. resetIds ();
243
+ for ( unsigned i = 0 , e = valArgs. size (); i < e; ++i)
244
+ if (valArgs[i])
245
+ setValue (i , valArgs[i] );
244
246
}
245
247
246
248
// / Constructs a constraint system with the specified number of dimensions
@@ -272,11 +274,15 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
272
274
FlatLinearValueConstraints (const IntegerPolyhedron &fac,
273
275
ArrayRef<std::optional<Value>> valArgs = {})
274
276
: FlatLinearConstraints(fac) {
275
- assert (valArgs. empty () || valArgs. size () == getNumDimAndSymbolVars ());
277
+ // Do not reset values assigned by FlatLinearConstraints' constructor.
276
278
if (valArgs.empty ())
277
- values.resize (getNumDimAndSymbolVars (), std::nullopt);
278
- else
279
- values.append (valArgs.begin (), valArgs.end ());
279
+ return ;
280
+ assert (valArgs.size () == getNumDimAndSymbolVars ());
281
+ // Store Values in space's identifiers.
282
+ space.resetIds ();
283
+ for (unsigned i = 0 , e = valArgs.size (); i < e; ++i)
284
+ if (valArgs[i])
285
+ setValue (i, *valArgs[i]);
280
286
}
281
287
282
288
// / Creates an affine constraint system from an IntegerSet.
@@ -290,9 +296,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
290
296
cst->getKind () <= Kind::FlatAffineRelation;
291
297
}
292
298
293
- // / Replaces the contents of this FlatLinearValueConstraints with `other`.
294
- void clearAndCopyFrom (const IntegerRelation &other) override ;
295
-
296
299
// / Adds a constant bound for the variable associated with the given Value.
297
300
void addBound (presburger::BoundType type, Value val, int64_t value);
298
301
using FlatLinearConstraints::addBound;
@@ -302,7 +305,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
302
305
inline Value getValue (unsigned pos) const {
303
306
assert (pos < getNumDimAndSymbolVars () && " Invalid position" );
304
307
assert (hasValue (pos) && " variable's Value not set" );
305
- return *values[pos];
308
+ VarKind kind = getVarKindAt (pos);
309
+ unsigned relativePos = pos - getVarKindOffset (kind);
310
+ return space.getId (kind, relativePos).getValue <Value>();
306
311
}
307
312
308
313
// / Returns the Values associated with variables in range [start, end).
@@ -313,27 +318,47 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
313
318
assert (start <= end && " invalid start position" );
314
319
values->clear ();
315
320
values->reserve (end - start);
316
- for (unsigned i = start; i < end; i++ )
321
+ for (unsigned i = start; i < end; ++i )
317
322
values->push_back (getValue (i));
318
323
}
319
324
320
- inline ArrayRef<std::optional<Value>> getMaybeValues () const {
321
- return {values.data (), values.size ()};
325
+ inline SmallVector<std::optional<Value>> getMaybeValues () const {
326
+ SmallVector<std::optional<Value>> maybeValues;
327
+ maybeValues.reserve (getNumDimAndSymbolVars ());
328
+ for (unsigned i = 0 , e = getNumDimAndSymbolVars (); i < e; ++i)
329
+ if (hasValue (i))
330
+ maybeValues.push_back (getValue (i));
331
+ else
332
+ maybeValues.push_back (std::nullopt);
333
+ return maybeValues;
322
334
}
323
335
324
- inline ArrayRef <std::optional<Value>>
336
+ inline SmallVector <std::optional<Value>>
325
337
getMaybeValues (presburger::VarKind kind) const {
326
338
assert (kind != VarKind::Local &&
327
339
" Local variables do not have any value attached to them." );
328
- return {values.data () + getVarKindOffset (kind), getNumVarKind (kind)};
340
+ SmallVector<std::optional<Value>> maybeValues;
341
+ maybeValues.reserve (getNumVarKind (kind));
342
+ for (unsigned i = 0 , e = getNumVarKind (kind); i < e; ++i) {
343
+ const Identifier id = space.getId (kind, i);
344
+ if (id.hasValue ())
345
+ maybeValues.push_back (id.getValue <Value>());
346
+ else
347
+ maybeValues.push_back (std::nullopt);
348
+ }
349
+ return maybeValues;
329
350
}
330
351
331
352
// / Returns true if the pos^th variable has an associated Value.
332
353
inline bool hasValue (unsigned pos) const {
333
354
assert (pos < getNumDimAndSymbolVars () && " Invalid position" );
334
- return values[pos].has_value ();
355
+ VarKind kind = getVarKindAt (pos);
356
+ unsigned relativePos = pos - getVarKindOffset (kind);
357
+ return space.getId (kind, relativePos).hasValue ();
335
358
}
336
359
360
+ void resetValues () { space.resetIds (); }
361
+
337
362
unsigned appendDimVar (ValueRange vals);
338
363
using FlatLinearConstraints::appendDimVar;
339
364
@@ -360,7 +385,9 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
360
385
// / Sets the Value associated with the pos^th variable.
361
386
inline void setValue (unsigned pos, Value val) {
362
387
assert (pos < getNumDimAndSymbolVars () && " invalid var position" );
363
- values[pos] = val;
388
+ VarKind kind = getVarKindAt (pos);
389
+ unsigned relativePos = pos - getVarKindOffset (kind);
390
+ space.getId (kind, relativePos) = presburger::Identifier (val);
364
391
}
365
392
366
393
// / Sets the Values associated with the variables in the range [start, end).
@@ -387,9 +414,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
387
414
void projectOut (Value val);
388
415
using IntegerPolyhedron::projectOut;
389
416
390
- // / Swap the posA^th variable with the posB^th variable.
391
- void swapVar (unsigned posA, unsigned posB) override ;
392
-
393
417
// / Prints the number of constraints, dimensions, symbols and locals in the
394
418
// / FlatAffineValueConstraints. Also, prints for each variable whether there
395
419
// / is an SSA Value attached to it.
@@ -444,28 +468,6 @@ class FlatLinearValueConstraints : public FlatLinearConstraints {
444
468
// / output = {0 <= d0 <= 6, 1 <= d1 <= 15}
445
469
LogicalResult unionBoundingBox (const FlatLinearValueConstraints &other);
446
470
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
471
};
470
472
471
473
// / Flattens 'expr' into 'flattenedExpr', which contains the coefficients of the
0 commit comments