Coverage Report

Created: 2019-02-23 12:57

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/Support/ISLTools.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------ ISLTools.cpp ----------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// Tools, utilities, helpers and extensions useful in conjunction with the
10
// Integer Set Library (isl).
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "polly/Support/ISLTools.h"
15
#include "llvm/ADT/StringRef.h"
16
17
using namespace polly;
18
19
namespace {
20
/// Create a map that shifts one dimension by an offset.
21
///
22
/// Example:
23
/// makeShiftDimAff({ [i0, i1] -> [o0, o1] }, 1, -2)
24
///   = { [i0, i1] -> [i0, i1 - 1] }
25
///
26
/// @param Space  The map space of the result. Must have equal number of in- and
27
///               out-dimensions.
28
/// @param Pos    Position to shift.
29
/// @param Amount Value added to the shifted dimension.
30
///
31
/// @return An isl_multi_aff for the map with this shifted dimension.
32
942
isl::multi_aff makeShiftDimAff(isl::space Space, int Pos, int Amount) {
33
942
  auto Identity = isl::multi_aff::identity(Space);
34
942
  if (Amount == 0)
35
0
    return Identity;
36
942
  auto ShiftAff = Identity.get_aff(Pos);
37
942
  ShiftAff = ShiftAff.set_constant_si(Amount);
38
942
  return Identity.set_aff(Pos, ShiftAff);
39
942
}
40
41
/// Construct a map that swaps two nested tuples.
42
///
43
/// @param FromSpace1 { Space1[] }
44
/// @param FromSpace2 { Space2[] }
45
///
46
/// @return { [Space1[] -> Space2[]] -> [Space2[] -> Space1[]] }
47
isl::basic_map makeTupleSwapBasicMap(isl::space FromSpace1,
48
125
                                     isl::space FromSpace2) {
49
125
  // Fast-path on out-of-quota.
50
125
  if (!FromSpace1 || !FromSpace2)
51
0
    return {};
52
125
53
125
  assert(FromSpace1.is_set());
54
125
  assert(FromSpace2.is_set());
55
125
56
125
  unsigned Dims1 = FromSpace1.dim(isl::dim::set);
57
125
  unsigned Dims2 = FromSpace2.dim(isl::dim::set);
58
125
59
125
  isl::space FromSpace =
60
125
      FromSpace1.map_from_domain_and_range(FromSpace2).wrap();
61
125
  isl::space ToSpace = FromSpace2.map_from_domain_and_range(FromSpace1).wrap();
62
125
  isl::space MapSpace = FromSpace.map_from_domain_and_range(ToSpace);
63
125
64
125
  isl::basic_map Result = isl::basic_map::universe(MapSpace);
65
188
  for (auto i = Dims1 - Dims1; i < Dims1; 
i += 163
)
66
63
    Result = Result.equate(isl::dim::in, i, isl::dim::out, Dims2 + i);
67
336
  for (auto i = Dims2 - Dims2; i < Dims2; 
i += 1211
) {
68
211
    Result = Result.equate(isl::dim::in, Dims1 + i, isl::dim::out, i);
69
211
  }
70
125
71
125
  return Result;
72
125
}
73
74
/// Like makeTupleSwapBasicMap(isl::space,isl::space), but returns
75
/// an isl_map.
76
125
isl::map makeTupleSwapMap(isl::space FromSpace1, isl::space FromSpace2) {
77
125
  isl::basic_map BMapResult = makeTupleSwapBasicMap(FromSpace1, FromSpace2);
78
125
  return isl::map(BMapResult);
79
125
}
80
} // anonymous namespace
81
82
243
isl::map polly::beforeScatter(isl::map Map, bool Strict) {
83
243
  isl::space RangeSpace = Map.get_space().range();
84
243
  isl::map ScatterRel =
85
243
      Strict ? 
isl::map::lex_gt(RangeSpace)57
:
isl::map::lex_ge(RangeSpace)186
;
86
243
  return Map.apply_range(ScatterRel);
87
243
}
88
89
98
isl::union_map polly::beforeScatter(isl::union_map UMap, bool Strict) {
90
98
  isl::union_map Result = isl::union_map::empty(UMap.get_space());
91
98
92
109
  for (isl::map Map : UMap.get_map_list()) {
93
109
    isl::map After = beforeScatter(Map, Strict);
94
109
    Result = Result.add_map(After);
95
109
  }
96
98
97
98
  return Result;
98
98
}
99
100
149
isl::map polly::afterScatter(isl::map Map, bool Strict) {
101
149
  isl::space RangeSpace = Map.get_space().range();
102
149
  isl::map ScatterRel =
103
149
      Strict ? 
isl::map::lex_lt(RangeSpace)131
:
isl::map::lex_le(RangeSpace)18
;
104
149
  return Map.apply_range(ScatterRel);
105
149
}
106
107
98
isl::union_map polly::afterScatter(const isl::union_map &UMap, bool Strict) {
108
98
  isl::union_map Result = isl::union_map::empty(UMap.get_space());
109
98
  for (isl::map Map : UMap.get_map_list()) {
110
54
    isl::map After = afterScatter(Map, Strict);
111
54
    Result = Result.add_map(After);
112
54
  }
113
98
  return Result;
114
98
}
115
116
isl::map polly::betweenScatter(isl::map From, isl::map To, bool InclFrom,
117
93
                               bool InclTo) {
118
93
  isl::map AfterFrom = afterScatter(From, !InclFrom);
119
93
  isl::map BeforeTo = beforeScatter(To, !InclTo);
120
93
121
93
  return AfterFrom.intersect(BeforeTo);
122
93
}
123
124
isl::union_map polly::betweenScatter(isl::union_map From, isl::union_map To,
125
86
                                     bool InclFrom, bool InclTo) {
126
86
  isl::union_map AfterFrom = afterScatter(From, !InclFrom);
127
86
  isl::union_map BeforeTo = beforeScatter(To, !InclTo);
128
86
129
86
  return AfterFrom.intersect(BeforeTo);
130
86
}
131
132
289
isl::map polly::singleton(isl::union_map UMap, isl::space ExpectedSpace) {
133
289
  if (!UMap)
134
0
    return nullptr;
135
289
136
289
  if (isl_union_map_n_map(UMap.get()) == 0)
137
3
    return isl::map::empty(ExpectedSpace);
138
286
139
286
  isl::map Result = isl::map::from_union_map(UMap);
140
286
  assert(!Result || Result.get_space().has_equal_tuples(ExpectedSpace));
141
286
142
286
  return Result;
143
286
}
144
145
130
isl::set polly::singleton(isl::union_set USet, isl::space ExpectedSpace) {
146
130
  if (!USet)
147
0
    return nullptr;
148
130
149
130
  if (isl_union_set_n_set(USet.get()) == 0)
150
36
    return isl::set::empty(ExpectedSpace);
151
94
152
94
  isl::set Result(USet);
153
94
  assert(!Result || Result.get_space().has_equal_tuples(ExpectedSpace));
154
94
155
94
  return Result;
156
94
}
157
158
583
unsigned polly::getNumScatterDims(const isl::union_map &Schedule) {
159
583
  unsigned Dims = 0;
160
583
  for (isl::map Map : Schedule.get_map_list())
161
2.34k
    Dims = std::max(Dims, Map.dim(isl::dim::out));
162
583
  return Dims;
163
583
}
164
165
575
isl::space polly::getScatterSpace(const isl::union_map &Schedule) {
166
575
  if (!Schedule)
167
0
    return nullptr;
168
575
  unsigned Dims = getNumScatterDims(Schedule);
169
575
  isl::space ScatterSpace = Schedule.get_space().set_from_params();
170
575
  return ScatterSpace.add_dims(isl::dim::set, Dims);
171
575
}
172
173
isl::union_map polly::makeIdentityMap(const isl::union_set &USet,
174
97
                                      bool RestrictDomain) {
175
97
  isl::union_map Result = isl::union_map::empty(USet.get_space());
176
114
  for (isl::set Set : USet.get_set_list()) {
177
114
    isl::map IdentityMap = isl::map::identity(Set.get_space().map_from_set());
178
114
    if (RestrictDomain)
179
57
      IdentityMap = IdentityMap.intersect_domain(Set);
180
114
    Result = Result.add_map(IdentityMap);
181
114
  }
182
97
  return Result;
183
97
}
184
185
125
isl::map polly::reverseDomain(isl::map Map) {
186
125
  isl::space DomSpace = Map.get_space().domain().unwrap();
187
125
  isl::space Space1 = DomSpace.domain();
188
125
  isl::space Space2 = DomSpace.range();
189
125
  isl::map Swap = makeTupleSwapMap(Space1, Space2);
190
125
  return Map.apply_domain(Swap);
191
125
}
192
193
165
isl::union_map polly::reverseDomain(const isl::union_map &UMap) {
194
165
  isl::union_map Result = isl::union_map::empty(UMap.get_space());
195
165
  for (isl::map Map : UMap.get_map_list()) {
196
124
    auto Reversed = reverseDomain(std::move(Map));
197
124
    Result = Result.add_map(Reversed);
198
124
  }
199
165
  return Result;
200
165
}
201
202
566
isl::set polly::shiftDim(isl::set Set, int Pos, int Amount) {
203
566
  int NumDims = Set.dim(isl::dim::set);
204
566
  if (Pos < 0)
205
563
    Pos = NumDims + Pos;
206
566
  assert(Pos < NumDims && "Dimension index must be in range");
207
566
  isl::space Space = Set.get_space();
208
566
  Space = Space.map_from_domain_and_range(Space);
209
566
  isl::multi_aff Translator = makeShiftDimAff(Space, Pos, Amount);
210
566
  isl::map TranslatorMap = isl::map::from_multi_aff(Translator);
211
566
  return Set.apply(TranslatorMap);
212
566
}
213
214
550
isl::union_set polly::shiftDim(isl::union_set USet, int Pos, int Amount) {
215
550
  isl::union_set Result = isl::union_set::empty(USet.get_space());
216
565
  for (isl::set Set : USet.get_set_list()) {
217
565
    isl::set Shifted = shiftDim(Set, Pos, Amount);
218
565
    Result = Result.add_set(Shifted);
219
565
  }
220
550
  return Result;
221
550
}
222
223
376
isl::map polly::shiftDim(isl::map Map, isl::dim Dim, int Pos, int Amount) {
224
376
  int NumDims = Map.dim(Dim);
225
376
  if (Pos < 0)
226
372
    Pos = NumDims + Pos;
227
376
  assert(Pos < NumDims && "Dimension index must be in range");
228
376
  isl::space Space = Map.get_space();
229
376
  switch (Dim) {
230
376
  case isl::dim::in:
231
371
    Space = Space.domain();
232
371
    break;
233
376
  case isl::dim::out:
234
5
    Space = Space.range();
235
5
    break;
236
376
  default:
237
0
    llvm_unreachable("Unsupported value for 'dim'");
238
376
  }
239
376
  Space = Space.map_from_domain_and_range(Space);
240
376
  isl::multi_aff Translator = makeShiftDimAff(Space, Pos, Amount);
241
376
  isl::map TranslatorMap = isl::map::from_multi_aff(Translator);
242
376
  switch (Dim) {
243
376
  case isl::dim::in:
244
371
    return Map.apply_domain(TranslatorMap);
245
376
  case isl::dim::out:
246
5
    return Map.apply_range(TranslatorMap);
247
376
  default:
248
0
    llvm_unreachable("Unsupported value for 'dim'");
249
376
  }
250
376
}
251
252
isl::union_map polly::shiftDim(isl::union_map UMap, isl::dim Dim, int Pos,
253
538
                               int Amount) {
254
538
  isl::union_map Result = isl::union_map::empty(UMap.get_space());
255
538
256
538
  for (isl::map Map : UMap.get_map_list()) {
257
374
    isl::map Shifted = shiftDim(Map, Dim, Pos, Amount);
258
374
    Result = Result.add_map(Shifted);
259
374
  }
260
538
  return Result;
261
538
}
262
263
604
void polly::simplify(isl::set &Set) {
264
604
  Set = isl::manage(isl_set_compute_divs(Set.copy()));
265
604
  Set = Set.detect_equalities();
266
604
  Set = Set.coalesce();
267
604
}
268
269
84
void polly::simplify(isl::union_set &USet) {
270
84
  USet = isl::manage(isl_union_set_compute_divs(USet.copy()));
271
84
  USet = USet.detect_equalities();
272
84
  USet = USet.coalesce();
273
84
}
274
275
719
void polly::simplify(isl::map &Map) {
276
719
  Map = isl::manage(isl_map_compute_divs(Map.copy()));
277
719
  Map = Map.detect_equalities();
278
719
  Map = Map.coalesce();
279
719
}
280
281
525
void polly::simplify(isl::union_map &UMap) {
282
525
  UMap = isl::manage(isl_union_map_compute_divs(UMap.copy()));
283
525
  UMap = UMap.detect_equalities();
284
525
  UMap = UMap.coalesce();
285
525
}
286
287
isl::union_map polly::computeReachingWrite(isl::union_map Schedule,
288
                                           isl::union_map Writes, bool Reverse,
289
375
                                           bool InclPrevDef, bool InclNextDef) {
290
375
291
375
  // { Scatter[] }
292
375
  isl::space ScatterSpace = getScatterSpace(Schedule);
293
375
294
375
  // { ScatterRead[] -> ScatterWrite[] }
295
375
  isl::map Relation;
296
375
  if (Reverse)
297
212
    Relation = InclPrevDef ? 
isl::map::lex_lt(ScatterSpace)25
298
212
                           : 
isl::map::lex_le(ScatterSpace)187
;
299
163
  else
300
163
    Relation = InclNextDef ? 
isl::map::lex_gt(ScatterSpace)157
301
163
                           : 
isl::map::lex_ge(ScatterSpace)6
;
302
375
303
375
  // { ScatterWrite[] -> [ScatterRead[] -> ScatterWrite[]] }
304
375
  isl::map RelationMap = Relation.range_map().reverse();
305
375
306
375
  // { Element[] -> ScatterWrite[] }
307
375
  isl::union_map WriteAction = Schedule.apply_domain(Writes);
308
375
309
375
  // { ScatterWrite[] -> Element[] }
310
375
  isl::union_map WriteActionRev = WriteAction.reverse();
311
375
312
375
  // { Element[] -> [ScatterUse[] -> ScatterWrite[]] }
313
375
  isl::union_map DefSchedRelation =
314
375
      isl::union_map(RelationMap).apply_domain(WriteActionRev);
315
375
316
375
  // For each element, at every point in time, map to the times of previous
317
375
  // definitions. { [Element[] -> ScatterRead[]] -> ScatterWrite[] }
318
375
  isl::union_map ReachableWrites = DefSchedRelation.uncurry();
319
375
  if (Reverse)
320
212
    ReachableWrites = ReachableWrites.lexmin();
321
163
  else
322
163
    ReachableWrites = ReachableWrites.lexmax();
323
375
324
375
  // { [Element[] -> ScatterWrite[]] -> ScatterWrite[] }
325
375
  isl::union_map SelfUse = WriteAction.range_map();
326
375
327
375
  if (InclPrevDef && 
InclNextDef29
) {
328
6
    // Add the Def itself to the solution.
329
6
    ReachableWrites = ReachableWrites.unite(SelfUse).coalesce();
330
369
  } else if (!InclPrevDef && 
!InclNextDef346
) {
331
10
    // Remove Def itself from the solution.
332
10
    ReachableWrites = ReachableWrites.subtract(SelfUse);
333
10
  }
334
375
335
375
  // { [Element[] -> ScatterRead[]] -> Domain[] }
336
375
  return ReachableWrites.apply_range(Schedule.reverse());
337
375
}
338
339
isl::union_map
340
polly::computeArrayUnused(isl::union_map Schedule, isl::union_map Writes,
341
                          isl::union_map Reads, bool ReadEltInSameInst,
342
82
                          bool IncludeLastRead, bool IncludeWrite) {
343
82
  // { Element[] -> Scatter[] }
344
82
  isl::union_map ReadActions = Schedule.apply_domain(Reads);
345
82
  isl::union_map WriteActions = Schedule.apply_domain(Writes);
346
82
347
82
  // { [Element[] -> DomainWrite[]] -> Scatter[] }
348
82
  isl::union_map EltDomWrites =
349
82
      Writes.reverse().range_map().apply_range(Schedule);
350
82
351
82
  // { [Element[] -> Scatter[]] -> DomainWrite[] }
352
82
  isl::union_map ReachingOverwrite = computeReachingWrite(
353
82
      Schedule, Writes, true, ReadEltInSameInst, !ReadEltInSameInst);
354
82
355
82
  // { [Element[] -> Scatter[]] -> DomainWrite[] }
356
82
  isl::union_map ReadsOverwritten =
357
82
      ReachingOverwrite.intersect_domain(ReadActions.wrap());
358
82
359
82
  // { [Element[] -> DomainWrite[]] -> Scatter[] }
360
82
  isl::union_map ReadsOverwrittenRotated =
361
82
      reverseDomain(ReadsOverwritten).curry().reverse();
362
82
  isl::union_map LastOverwrittenRead = ReadsOverwrittenRotated.lexmax();
363
82
364
82
  // { [Element[] -> DomainWrite[]] -> Scatter[] }
365
82
  isl::union_map BetweenLastReadOverwrite = betweenScatter(
366
82
      LastOverwrittenRead, EltDomWrites, IncludeLastRead, IncludeWrite);
367
82
368
82
  // { [Element[] -> Scatter[]] -> DomainWrite[] }
369
82
  isl::union_map ReachingOverwriteZone = computeReachingWrite(
370
82
      Schedule, Writes, true, IncludeLastRead, IncludeWrite);
371
82
372
82
  // { [Element[] -> DomainWrite[]] -> Scatter[] }
373
82
  isl::union_map ReachingOverwriteRotated =
374
82
      reverseDomain(ReachingOverwriteZone).curry().reverse();
375
82
376
82
  // { [Element[] -> DomainWrite[]] -> Scatter[] }
377
82
  isl::union_map WritesWithoutReads = ReachingOverwriteRotated.subtract_domain(
378
82
      ReadsOverwrittenRotated.domain());
379
82
380
82
  return BetweenLastReadOverwrite.unite(WritesWithoutReads)
381
82
      .domain_factor_domain();
382
82
}
383
384
isl::union_set polly::convertZoneToTimepoints(isl::union_set Zone,
385
550
                                              bool InclStart, bool InclEnd) {
386
550
  if (!InclStart && 
InclEnd10
)
387
5
    return Zone;
388
545
389
545
  auto ShiftedZone = shiftDim(Zone, -1, -1);
390
545
  if (InclStart && 
!InclEnd540
)
391
535
    return ShiftedZone;
392
10
  else if (!InclStart && 
!InclEnd5
)
393
5
    return Zone.intersect(ShiftedZone);
394
5
395
5
  assert(InclStart && InclEnd);
396
5
  return Zone.unite(ShiftedZone);
397
5
}
398
399
isl::union_map polly::convertZoneToTimepoints(isl::union_map Zone, isl::dim Dim,
400
603
                                              bool InclStart, bool InclEnd) {
401
603
  if (!InclStart && 
InclEnd69
)
402
67
    return Zone;
403
536
404
536
  auto ShiftedZone = shiftDim(Zone, Dim, -1, -1);
405
536
  if (InclStart && 
!InclEnd534
)
406
532
    return ShiftedZone;
407
4
  else if (!InclStart && 
!InclEnd2
)
408
2
    return Zone.intersect(ShiftedZone);
409
2
410
2
  assert(InclStart && InclEnd);
411
2
  return Zone.unite(ShiftedZone);
412
2
}
413
414
isl::map polly::convertZoneToTimepoints(isl::map Zone, isl::dim Dim,
415
45
                                        bool InclStart, bool InclEnd) {
416
45
  if (!InclStart && InclEnd)
417
45
    return Zone;
418
0
419
0
  auto ShiftedZone = shiftDim(Zone, Dim, -1, -1);
420
0
  if (InclStart && !InclEnd)
421
0
    return ShiftedZone;
422
0
  else if (!InclStart && !InclEnd)
423
0
    return Zone.intersect(ShiftedZone);
424
0
425
0
  assert(InclStart && InclEnd);
426
0
  return Zone.unite(ShiftedZone);
427
0
}
428
429
211
isl::map polly::distributeDomain(isl::map Map) {
430
211
  // Note that we cannot take Map apart into { Domain[] -> Range1[] } and {
431
211
  // Domain[] -> Range2[] } and combine again. We would loose any relation
432
211
  // between Range1[] and Range2[] that is not also a constraint to Domain[].
433
211
434
211
  isl::space Space = Map.get_space();
435
211
  isl::space DomainSpace = Space.domain();
436
211
  unsigned DomainDims = DomainSpace.dim(isl::dim::set);
437
211
  isl::space RangeSpace = Space.range().unwrap();
438
211
  isl::space Range1Space = RangeSpace.domain();
439
211
  unsigned Range1Dims = Range1Space.dim(isl::dim::set);
440
211
  isl::space Range2Space = RangeSpace.range();
441
211
  unsigned Range2Dims = Range2Space.dim(isl::dim::set);
442
211
443
211
  isl::space OutputSpace =
444
211
      DomainSpace.map_from_domain_and_range(Range1Space)
445
211
          .wrap()
446
211
          .map_from_domain_and_range(
447
211
              DomainSpace.map_from_domain_and_range(Range2Space).wrap());
448
211
449
211
  isl::basic_map Translator = isl::basic_map::universe(
450
211
      Space.wrap().map_from_domain_and_range(OutputSpace.wrap()));
451
211
452
425
  for (unsigned i = 0; i < DomainDims; 
i += 1214
) {
453
214
    Translator = Translator.equate(isl::dim::in, i, isl::dim::out, i);
454
214
    Translator = Translator.equate(isl::dim::in, i, isl::dim::out,
455
214
                                   DomainDims + Range1Dims + i);
456
214
  }
457
687
  for (unsigned i = 0; i < Range1Dims; 
i += 1476
)
458
476
    Translator = Translator.equate(isl::dim::in, DomainDims + i, isl::dim::out,
459
476
                                   DomainDims + i);
460
378
  for (unsigned i = 0; i < Range2Dims; 
i += 1167
)
461
167
    Translator = Translator.equate(isl::dim::in, DomainDims + Range1Dims + i,
462
167
                                   isl::dim::out,
463
167
                                   DomainDims + Range1Dims + DomainDims + i);
464
211
465
211
  return Map.wrap().apply(Translator).unwrap();
466
211
}
467
468
120
isl::union_map polly::distributeDomain(isl::union_map UMap) {
469
120
  isl::union_map Result = isl::union_map::empty(UMap.get_space());
470
209
  for (isl::map Map : UMap.get_map_list()) {
471
209
    auto Distributed = distributeDomain(Map);
472
209
    Result = Result.add_map(Distributed);
473
209
  }
474
120
  return Result;
475
120
}
476
477
57
isl::union_map polly::liftDomains(isl::union_map UMap, isl::union_set Factor) {
478
57
479
57
  // { Factor[] -> Factor[] }
480
57
  isl::union_map Factors = makeIdentityMap(Factor, true);
481
57
482
57
  return Factors.product(UMap);
483
57
}
484
485
isl::union_map polly::applyDomainRange(isl::union_map UMap,
486
54
                                       isl::union_map Func) {
487
54
  // This implementation creates unnecessary cross products of the
488
54
  // DomainDomain[] and Func. An alternative implementation could reverse
489
54
  // domain+uncurry,apply Func to what now is the domain, then undo the
490
54
  // preparing transformation. Another alternative implementation could create a
491
54
  // translator map for each piece.
492
54
493
54
  // { DomainDomain[] }
494
54
  isl::union_set DomainDomain = UMap.domain().unwrap().domain();
495
54
496
54
  // { [DomainDomain[] -> DomainRange[]] -> [DomainDomain[] -> NewDomainRange[]]
497
54
  // }
498
54
  isl::union_map LifetedFunc = liftDomains(std::move(Func), DomainDomain);
499
54
500
54
  return UMap.apply_domain(LifetedFunc);
501
54
}
502
503
184
isl::map polly::intersectRange(isl::map Map, isl::union_set Range) {
504
184
  isl::set RangeSet = Range.extract_set(Map.get_space().range());
505
184
  return Map.intersect_range(RangeSet);
506
184
}
507
508
158
isl::val polly::getConstant(isl::pw_aff PwAff, bool Max, bool Min) {
509
158
  assert(!Max || !Min); // Cannot return min and max at the same time.
510
158
  isl::val Result;
511
158
  isl::stat Stat = PwAff.foreach_piece(
512
158
      [=, &Result](isl::set Set, isl::aff Aff) -> isl::stat {
513
158
        if (Result && 
Result.is_nan()0
)
514
0
          return isl::stat::ok();
515
158
516
158
        // TODO: If Min/Max, we can also determine a minimum/maximum value if
517
158
        // Set is constant-bounded.
518
158
        if (!Aff.is_cst()) {
519
0
          Result = isl::val::nan(Aff.get_ctx());
520
0
          return isl::stat::error();
521
0
        }
522
158
523
158
        isl::val ThisVal = Aff.get_constant_val();
524
158
        if (!Result) {
525
158
          Result = ThisVal;
526
158
          return isl::stat::ok();
527
158
        }
528
0
529
0
        if (Result.eq(ThisVal))
530
0
          return isl::stat::ok();
531
0
532
0
        if (Max && ThisVal.gt(Result)) {
533
0
          Result = ThisVal;
534
0
          return isl::stat::ok();
535
0
        }
536
0
537
0
        if (Min && ThisVal.lt(Result)) {
538
0
          Result = ThisVal;
539
0
          return isl::stat::ok();
540
0
        }
541
0
542
0
        // Not compatible
543
0
        Result = isl::val::nan(Aff.get_ctx());
544
0
        return isl::stat::error();
545
0
      });
546
158
547
158
  if (Stat.is_error())
548
0
    return {};
549
158
550
158
  return Result;
551
158
}
552
553
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
554
static void foreachPoint(const isl::set &Set,
555
                         const std::function<void(isl::point P)> &F) {
556
  Set.foreach_point([&](isl::point P) -> isl::stat {
557
    F(P);
558
    return isl::stat::ok();
559
  });
560
}
561
562
static void foreachPoint(isl::basic_set BSet,
563
                         const std::function<void(isl::point P)> &F) {
564
  foreachPoint(isl::set(BSet), F);
565
}
566
567
/// Determine the sorting order of the sets @p A and @p B without considering
568
/// the space structure.
569
///
570
/// Ordering is based on the lower bounds of the set's dimensions. First
571
/// dimensions are considered first.
572
static int flatCompare(const isl::basic_set &A, const isl::basic_set &B) {
573
  unsigned ALen = A.dim(isl::dim::set);
574
  unsigned BLen = B.dim(isl::dim::set);
575
  unsigned Len = std::min(ALen, BLen);
576
577
  for (unsigned i = 0; i < Len; i += 1) {
578
    isl::basic_set ADim =
579
        A.project_out(isl::dim::param, 0, A.dim(isl::dim::param))
580
            .project_out(isl::dim::set, i + 1, ALen - i - 1)
581
            .project_out(isl::dim::set, 0, i);
582
    isl::basic_set BDim =
583
        B.project_out(isl::dim::param, 0, B.dim(isl::dim::param))
584
            .project_out(isl::dim::set, i + 1, BLen - i - 1)
585
            .project_out(isl::dim::set, 0, i);
586
587
    isl::basic_set AHull = isl::set(ADim).convex_hull();
588
    isl::basic_set BHull = isl::set(BDim).convex_hull();
589
590
    bool ALowerBounded =
591
        bool(isl::set(AHull).dim_has_any_lower_bound(isl::dim::set, 0));
592
    bool BLowerBounded =
593
        bool(isl::set(BHull).dim_has_any_lower_bound(isl::dim::set, 0));
594
595
    int BoundedCompare = BLowerBounded - ALowerBounded;
596
    if (BoundedCompare != 0)
597
      return BoundedCompare;
598
599
    if (!ALowerBounded || !BLowerBounded)
600
      continue;
601
602
    isl::pw_aff AMin = isl::set(ADim).dim_min(0);
603
    isl::pw_aff BMin = isl::set(BDim).dim_min(0);
604
605
    isl::val AMinVal = polly::getConstant(AMin, false, true);
606
    isl::val BMinVal = polly::getConstant(BMin, false, true);
607
608
    int MinCompare = AMinVal.sub(BMinVal).sgn();
609
    if (MinCompare != 0)
610
      return MinCompare;
611
  }
612
613
  // If all the dimensions' lower bounds are equal or incomparable, sort based
614
  // on the number of dimensions.
615
  return ALen - BLen;
616
}
617
618
/// Compare the sets @p A and @p B according to their nested space structure.
619
/// Returns 0 if the structure is considered equal.
620
/// If @p ConsiderTupleLen is false, the number of dimensions in a tuple are
621
/// ignored, i.e. a tuple with the same name but different number of dimensions
622
/// are considered equal.
623
static int structureCompare(const isl::space &ASpace, const isl::space &BSpace,
624
                            bool ConsiderTupleLen) {
625
  int WrappingCompare = bool(ASpace.is_wrapping()) - bool(BSpace.is_wrapping());
626
  if (WrappingCompare != 0)
627
    return WrappingCompare;
628
629
  if (ASpace.is_wrapping() && BSpace.is_wrapping()) {
630
    isl::space AMap = ASpace.unwrap();
631
    isl::space BMap = BSpace.unwrap();
632
633
    int FirstResult =
634
        structureCompare(AMap.domain(), BMap.domain(), ConsiderTupleLen);
635
    if (FirstResult != 0)
636
      return FirstResult;
637
638
    return structureCompare(AMap.range(), BMap.range(), ConsiderTupleLen);
639
  }
640
641
  std::string AName;
642
  if (ASpace.has_tuple_name(isl::dim::set))
643
    AName = ASpace.get_tuple_name(isl::dim::set);
644
645
  std::string BName;
646
  if (BSpace.has_tuple_name(isl::dim::set))
647
    BName = BSpace.get_tuple_name(isl::dim::set);
648
649
  int NameCompare = AName.compare(BName);
650
  if (NameCompare != 0)
651
    return NameCompare;
652
653
  if (ConsiderTupleLen) {
654
    int LenCompare = BSpace.dim(isl::dim::set) - ASpace.dim(isl::dim::set);
655
    if (LenCompare != 0)
656
      return LenCompare;
657
  }
658
659
  return 0;
660
}
661
662
/// Compare the sets @p A and @p B according to their nested space structure. If
663
/// the structure is the same, sort using the dimension lower bounds.
664
/// Returns an std::sort compatible bool.
665
static bool orderComparer(const isl::basic_set &A, const isl::basic_set &B) {
666
  isl::space ASpace = A.get_space();
667
  isl::space BSpace = B.get_space();
668
669
  // Ignoring number of dimensions first ensures that structures with same tuple
670
  // names, but different number of dimensions are still sorted close together.
671
  int TupleNestingCompare = structureCompare(ASpace, BSpace, false);
672
  if (TupleNestingCompare != 0)
673
    return TupleNestingCompare < 0;
674
675
  int TupleCompare = structureCompare(ASpace, BSpace, true);
676
  if (TupleCompare != 0)
677
    return TupleCompare < 0;
678
679
  return flatCompare(A, B) < 0;
680
}
681
682
/// Print a string representation of @p USet to @p OS.
683
///
684
/// The pieces of @p USet are printed in a sorted order. Spaces with equal or
685
/// similar nesting structure are printed together. Compared to isl's own
686
/// printing function the uses the structure itself as base of the sorting, not
687
/// a hash of it. It ensures that e.g. maps spaces with same domain structure
688
/// are printed together. Set pieces with same structure are printed in order of
689
/// their lower bounds.
690
///
691
/// @param USet     Polyhedra to print.
692
/// @param OS       Target stream.
693
/// @param Simplify Whether to simplify the polyhedron before printing.
694
/// @param IsMap    Whether @p USet is a wrapped map. If true, sets are
695
///                 unwrapped before printing to again appear as a map.
696
static void printSortedPolyhedra(isl::union_set USet, llvm::raw_ostream &OS,
697
                                 bool Simplify, bool IsMap) {
698
  if (!USet) {
699
    OS << "<null>\n";
700
    return;
701
  }
702
703
  if (Simplify)
704
    simplify(USet);
705
706
  // Get all the polyhedra.
707
  std::vector<isl::basic_set> BSets;
708
709
  for (isl::set Set : USet.get_set_list()) {
710
    for (isl::basic_set BSet : Set.get_basic_set_list()) {
711
      BSets.push_back(BSet);
712
    }
713
  }
714
715
  if (BSets.empty()) {
716
    OS << "{\n}\n";
717
    return;
718
  }
719
720
  // Sort the polyhedra.
721
  llvm::sort(BSets, orderComparer);
722
723
  // Print the polyhedra.
724
  bool First = true;
725
  for (const isl::basic_set &BSet : BSets) {
726
    std::string Str;
727
    if (IsMap)
728
      Str = isl::map(BSet.unwrap()).to_str();
729
    else
730
      Str = isl::set(BSet).to_str();
731
    size_t OpenPos = Str.find_first_of('{');
732
    assert(OpenPos != std::string::npos);
733
    size_t ClosePos = Str.find_last_of('}');
734
    assert(ClosePos != std::string::npos);
735
736
    if (First)
737
      OS << llvm::StringRef(Str).substr(0, OpenPos + 1) << "\n ";
738
    else
739
      OS << ";\n ";
740
741
    OS << llvm::StringRef(Str).substr(OpenPos + 1, ClosePos - OpenPos - 2);
742
    First = false;
743
  }
744
  assert(!First);
745
  OS << "\n}\n";
746
}
747
748
static void recursiveExpand(isl::basic_set BSet, int Dim, isl::set &Expanded) {
749
  int Dims = BSet.dim(isl::dim::set);
750
  if (Dim >= Dims) {
751
    Expanded = Expanded.unite(BSet);
752
    return;
753
  }
754
755
  isl::basic_set DimOnly =
756
      BSet.project_out(isl::dim::param, 0, BSet.dim(isl::dim::param))
757
          .project_out(isl::dim::set, Dim + 1, Dims - Dim - 1)
758
          .project_out(isl::dim::set, 0, Dim);
759
  if (!DimOnly.is_bounded()) {
760
    recursiveExpand(BSet, Dim + 1, Expanded);
761
    return;
762
  }
763
764
  foreachPoint(DimOnly, [&, Dim](isl::point P) {
765
    isl::val Val = P.get_coordinate_val(isl::dim::set, 0);
766
    isl::basic_set FixBSet = BSet.fix_val(isl::dim::set, Dim, Val);
767
    recursiveExpand(FixBSet, Dim + 1, Expanded);
768
  });
769
}
770
771
/// Make each point of a set explicit.
772
///
773
/// "Expanding" makes each point a set contains explicit. That is, the result is
774
/// a set of singleton polyhedra. Unbounded dimensions are not expanded.
775
///
776
/// Example:
777
///   { [i] : 0 <= i < 2 }
778
/// is expanded to:
779
///   { [0]; [1] }
780
static isl::set expand(const isl::set &Set) {
781
  isl::set Expanded = isl::set::empty(Set.get_space());
782
  for (isl::basic_set BSet : Set.get_basic_set_list())
783
    recursiveExpand(BSet, 0, Expanded);
784
  return Expanded;
785
}
786
787
/// Expand all points of a union set explicit.
788
///
789
/// @see expand(const isl::set)
790
static isl::union_set expand(const isl::union_set &USet) {
791
  isl::union_set Expanded = isl::union_set::empty(USet.get_space());
792
  for (isl::set Set : USet.get_set_list()) {
793
    isl::set SetExpanded = expand(Set);
794
    Expanded = Expanded.add_set(SetExpanded);
795
  }
796
  return Expanded;
797
}
798
799
LLVM_DUMP_METHOD void polly::dumpPw(const isl::set &Set) {
800
  printSortedPolyhedra(Set, llvm::errs(), true, false);
801
}
802
803
LLVM_DUMP_METHOD void polly::dumpPw(const isl::map &Map) {
804
  printSortedPolyhedra(Map.wrap(), llvm::errs(), true, true);
805
}
806
807
LLVM_DUMP_METHOD void polly::dumpPw(const isl::union_set &USet) {
808
  printSortedPolyhedra(USet, llvm::errs(), true, false);
809
}
810
811
LLVM_DUMP_METHOD void polly::dumpPw(const isl::union_map &UMap) {
812
  printSortedPolyhedra(UMap.wrap(), llvm::errs(), true, true);
813
}
814
815
LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_set *Set) {
816
  dumpPw(isl::manage_copy(Set));
817
}
818
819
LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_map *Map) {
820
  dumpPw(isl::manage_copy(Map));
821
}
822
823
LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_union_set *USet) {
824
  dumpPw(isl::manage_copy(USet));
825
}
826
827
LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_union_map *UMap) {
828
  dumpPw(isl::manage_copy(UMap));
829
}
830
831
LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::set &Set) {
832
  printSortedPolyhedra(expand(Set), llvm::errs(), false, false);
833
}
834
835
LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::map &Map) {
836
  printSortedPolyhedra(expand(Map.wrap()), llvm::errs(), false, true);
837
}
838
839
LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::union_set &USet) {
840
  printSortedPolyhedra(expand(USet), llvm::errs(), false, false);
841
}
842
843
LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::union_map &UMap) {
844
  printSortedPolyhedra(expand(UMap.wrap()), llvm::errs(), false, true);
845
}
846
847
LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_set *Set) {
848
  dumpExpanded(isl::manage_copy(Set));
849
}
850
851
LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_map *Map) {
852
  dumpExpanded(isl::manage_copy(Map));
853
}
854
855
LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_union_set *USet) {
856
  dumpExpanded(isl::manage_copy(USet));
857
}
858
859
LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_union_map *UMap) {
860
  dumpExpanded(isl::manage_copy(UMap));
861
}
862
#endif