1d1508812SMichael Kruse //===------ ISLTools.cpp ----------------------------------------*- C++ -*-===//
2d1508812SMichael Kruse //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d1508812SMichael Kruse //
7d1508812SMichael Kruse //===----------------------------------------------------------------------===//
8d1508812SMichael Kruse //
9d1508812SMichael Kruse // Tools, utilities, helpers and extensions useful in conjunction with the
10d1508812SMichael Kruse // Integer Set Library (isl).
11d1508812SMichael Kruse //
12d1508812SMichael Kruse //===----------------------------------------------------------------------===//
13d1508812SMichael Kruse
14d1508812SMichael Kruse #include "polly/Support/ISLTools.h"
15cfe117deSpatacca #include "polly/Support/GICHelper.h"
16031bb165SMichael Kruse #include "llvm/Support/raw_ostream.h"
17031bb165SMichael Kruse #include <cassert>
18031bb165SMichael Kruse #include <vector>
19d1508812SMichael Kruse
20d1508812SMichael Kruse using namespace polly;
21d1508812SMichael Kruse
22d1508812SMichael Kruse namespace {
23d1508812SMichael Kruse /// Create a map that shifts one dimension by an offset.
24d1508812SMichael Kruse ///
25d1508812SMichael Kruse /// Example:
26d1508812SMichael Kruse /// makeShiftDimAff({ [i0, i1] -> [o0, o1] }, 1, -2)
27d1508812SMichael Kruse /// = { [i0, i1] -> [i0, i1 - 1] }
28d1508812SMichael Kruse ///
29d1508812SMichael Kruse /// @param Space The map space of the result. Must have equal number of in- and
30d1508812SMichael Kruse /// out-dimensions.
31d1508812SMichael Kruse /// @param Pos Position to shift.
32d1508812SMichael Kruse /// @param Amount Value added to the shifted dimension.
33d1508812SMichael Kruse ///
34d1508812SMichael Kruse /// @return An isl_multi_aff for the map with this shifted dimension.
makeShiftDimAff(isl::space Space,int Pos,int Amount)35deaef15fSTobias Grosser isl::multi_aff makeShiftDimAff(isl::space Space, int Pos, int Amount) {
36d3d3d6b7STobias Grosser auto Identity = isl::multi_aff::identity(Space);
37d1508812SMichael Kruse if (Amount == 0)
38d1508812SMichael Kruse return Identity;
39d3fdbda6SRiccardo Mori auto ShiftAff = Identity.at(Pos);
40d3d3d6b7STobias Grosser ShiftAff = ShiftAff.set_constant_si(Amount);
41d3d3d6b7STobias Grosser return Identity.set_aff(Pos, ShiftAff);
42d1508812SMichael Kruse }
43d1508812SMichael Kruse
44d1508812SMichael Kruse /// Construct a map that swaps two nested tuples.
45d1508812SMichael Kruse ///
46d1508812SMichael Kruse /// @param FromSpace1 { Space1[] }
47d1508812SMichael Kruse /// @param FromSpace2 { Space2[] }
48d1508812SMichael Kruse ///
49d1508812SMichael Kruse /// @return { [Space1[] -> Space2[]] -> [Space2[] -> Space1[]] }
makeTupleSwapBasicMap(isl::space FromSpace1,isl::space FromSpace2)50deaef15fSTobias Grosser isl::basic_map makeTupleSwapBasicMap(isl::space FromSpace1,
51deaef15fSTobias Grosser isl::space FromSpace2) {
52d6c2ca8dSMichael Kruse // Fast-path on out-of-quota.
537c7978a1Spatacca if (FromSpace1.is_null() || FromSpace2.is_null())
54d6c2ca8dSMichael Kruse return {};
55d6c2ca8dSMichael Kruse
56d3d3d6b7STobias Grosser assert(FromSpace1.is_set());
57d3d3d6b7STobias Grosser assert(FromSpace2.is_set());
58d1508812SMichael Kruse
5944596fe6SRiccardo Mori unsigned Dims1 = unsignedFromIslSize(FromSpace1.dim(isl::dim::set));
6044596fe6SRiccardo Mori unsigned Dims2 = unsignedFromIslSize(FromSpace2.dim(isl::dim::set));
61d1508812SMichael Kruse
62d3d3d6b7STobias Grosser isl::space FromSpace =
63d3d3d6b7STobias Grosser FromSpace1.map_from_domain_and_range(FromSpace2).wrap();
64d3d3d6b7STobias Grosser isl::space ToSpace = FromSpace2.map_from_domain_and_range(FromSpace1).wrap();
65d3d3d6b7STobias Grosser isl::space MapSpace = FromSpace.map_from_domain_and_range(ToSpace);
66d3d3d6b7STobias Grosser
67d3d3d6b7STobias Grosser isl::basic_map Result = isl::basic_map::universe(MapSpace);
6884c934a5SMichael Kruse for (unsigned i = 0u; i < Dims1; i += 1)
69d3d3d6b7STobias Grosser Result = Result.equate(isl::dim::in, i, isl::dim::out, Dims2 + i);
7084c934a5SMichael Kruse for (unsigned i = 0u; i < Dims2; i += 1) {
71d3d3d6b7STobias Grosser Result = Result.equate(isl::dim::in, Dims1 + i, isl::dim::out, i);
72d1508812SMichael Kruse }
73d1508812SMichael Kruse
74d1508812SMichael Kruse return Result;
75d1508812SMichael Kruse }
76d1508812SMichael Kruse
77deaef15fSTobias Grosser /// Like makeTupleSwapBasicMap(isl::space,isl::space), but returns
78d1508812SMichael Kruse /// an isl_map.
makeTupleSwapMap(isl::space FromSpace1,isl::space FromSpace2)79deaef15fSTobias Grosser isl::map makeTupleSwapMap(isl::space FromSpace1, isl::space FromSpace2) {
80d3d3d6b7STobias Grosser isl::basic_map BMapResult = makeTupleSwapBasicMap(FromSpace1, FromSpace2);
81d3d3d6b7STobias Grosser return isl::map(BMapResult);
82d1508812SMichael Kruse }
83d1508812SMichael Kruse } // anonymous namespace
84d1508812SMichael Kruse
beforeScatter(isl::map Map,bool Strict)85deaef15fSTobias Grosser isl::map polly::beforeScatter(isl::map Map, bool Strict) {
86d3d3d6b7STobias Grosser isl::space RangeSpace = Map.get_space().range();
87d3d3d6b7STobias Grosser isl::map ScatterRel =
88d3d3d6b7STobias Grosser Strict ? isl::map::lex_gt(RangeSpace) : isl::map::lex_ge(RangeSpace);
89d3d3d6b7STobias Grosser return Map.apply_range(ScatterRel);
90d1508812SMichael Kruse }
91d1508812SMichael Kruse
beforeScatter(isl::union_map UMap,bool Strict)92deaef15fSTobias Grosser isl::union_map polly::beforeScatter(isl::union_map UMap, bool Strict) {
93bad3ebbaSRiccardo Mori isl::union_map Result = isl::union_map::empty(UMap.ctx());
941696e48eSTobias Grosser
951696e48eSTobias Grosser for (isl::map Map : UMap.get_map_list()) {
96d3d3d6b7STobias Grosser isl::map After = beforeScatter(Map, Strict);
97d5ee355fSRiccardo Mori Result = Result.unite(After);
981696e48eSTobias Grosser }
991696e48eSTobias Grosser
100d1508812SMichael Kruse return Result;
101d1508812SMichael Kruse }
102d1508812SMichael Kruse
afterScatter(isl::map Map,bool Strict)103deaef15fSTobias Grosser isl::map polly::afterScatter(isl::map Map, bool Strict) {
104d3d3d6b7STobias Grosser isl::space RangeSpace = Map.get_space().range();
105d3d3d6b7STobias Grosser isl::map ScatterRel =
106d3d3d6b7STobias Grosser Strict ? isl::map::lex_lt(RangeSpace) : isl::map::lex_le(RangeSpace);
107d3d3d6b7STobias Grosser return Map.apply_range(ScatterRel);
108d1508812SMichael Kruse }
109d1508812SMichael Kruse
afterScatter(const isl::union_map & UMap,bool Strict)110deaef15fSTobias Grosser isl::union_map polly::afterScatter(const isl::union_map &UMap, bool Strict) {
111bad3ebbaSRiccardo Mori isl::union_map Result = isl::union_map::empty(UMap.ctx());
1121696e48eSTobias Grosser for (isl::map Map : UMap.get_map_list()) {
113d3d3d6b7STobias Grosser isl::map After = afterScatter(Map, Strict);
114d5ee355fSRiccardo Mori Result = Result.unite(After);
1151696e48eSTobias Grosser }
116d1508812SMichael Kruse return Result;
117d1508812SMichael Kruse }
118d1508812SMichael Kruse
betweenScatter(isl::map From,isl::map To,bool InclFrom,bool InclTo)119deaef15fSTobias Grosser isl::map polly::betweenScatter(isl::map From, isl::map To, bool InclFrom,
120deaef15fSTobias Grosser bool InclTo) {
121d3d3d6b7STobias Grosser isl::map AfterFrom = afterScatter(From, !InclFrom);
122d3d3d6b7STobias Grosser isl::map BeforeTo = beforeScatter(To, !InclTo);
123d1508812SMichael Kruse
124d3d3d6b7STobias Grosser return AfterFrom.intersect(BeforeTo);
125d1508812SMichael Kruse }
126d1508812SMichael Kruse
betweenScatter(isl::union_map From,isl::union_map To,bool InclFrom,bool InclTo)127deaef15fSTobias Grosser isl::union_map polly::betweenScatter(isl::union_map From, isl::union_map To,
128d1508812SMichael Kruse bool InclFrom, bool InclTo) {
129d3d3d6b7STobias Grosser isl::union_map AfterFrom = afterScatter(From, !InclFrom);
130d3d3d6b7STobias Grosser isl::union_map BeforeTo = beforeScatter(To, !InclTo);
131d1508812SMichael Kruse
132d3d3d6b7STobias Grosser return AfterFrom.intersect(BeforeTo);
133d1508812SMichael Kruse }
134d1508812SMichael Kruse
singleton(isl::union_map UMap,isl::space ExpectedSpace)135deaef15fSTobias Grosser isl::map polly::singleton(isl::union_map UMap, isl::space ExpectedSpace) {
1367c7978a1Spatacca if (UMap.is_null())
1379b41d095Spatacca return {};
138d1508812SMichael Kruse
139d3d3d6b7STobias Grosser if (isl_union_map_n_map(UMap.get()) == 0)
1408703e383STobias Grosser return isl::map::empty(ExpectedSpace);
141d1508812SMichael Kruse
1428703e383STobias Grosser isl::map Result = isl::map::from_union_map(UMap);
1437c7978a1Spatacca assert(Result.is_null() ||
1447c7978a1Spatacca Result.get_space().has_equal_tuples(ExpectedSpace));
1458703e383STobias Grosser
146d1508812SMichael Kruse return Result;
147d1508812SMichael Kruse }
148d1508812SMichael Kruse
singleton(isl::union_set USet,isl::space ExpectedSpace)149deaef15fSTobias Grosser isl::set polly::singleton(isl::union_set USet, isl::space ExpectedSpace) {
1507c7978a1Spatacca if (USet.is_null())
1519b41d095Spatacca return {};
152d1508812SMichael Kruse
153d3d3d6b7STobias Grosser if (isl_union_set_n_set(USet.get()) == 0)
1548703e383STobias Grosser return isl::set::empty(ExpectedSpace);
155d1508812SMichael Kruse
1568703e383STobias Grosser isl::set Result(USet);
1577c7978a1Spatacca assert(Result.is_null() ||
1587c7978a1Spatacca Result.get_space().has_equal_tuples(ExpectedSpace));
1598703e383STobias Grosser
160d1508812SMichael Kruse return Result;
161d1508812SMichael Kruse }
162d1508812SMichael Kruse
getNumScatterDims(const isl::union_map & Schedule)16344596fe6SRiccardo Mori unsigned polly::getNumScatterDims(const isl::union_map &Schedule) {
16444596fe6SRiccardo Mori unsigned Dims = 0;
1651ef55ac9SMichael Kruse for (isl::map Map : Schedule.get_map_list()) {
1667c7978a1Spatacca if (Map.is_null())
1671ef55ac9SMichael Kruse continue;
1681ef55ac9SMichael Kruse
16944596fe6SRiccardo Mori Dims = std::max(Dims, unsignedFromIslSize(Map.range_tuple_dim()));
1701ef55ac9SMichael Kruse }
171d1508812SMichael Kruse return Dims;
172d1508812SMichael Kruse }
173d1508812SMichael Kruse
getScatterSpace(const isl::union_map & Schedule)174deaef15fSTobias Grosser isl::space polly::getScatterSpace(const isl::union_map &Schedule) {
1757c7978a1Spatacca if (Schedule.is_null())
1769b41d095Spatacca return {};
177d3d3d6b7STobias Grosser unsigned Dims = getNumScatterDims(Schedule);
178d3d3d6b7STobias Grosser isl::space ScatterSpace = Schedule.get_space().set_from_params();
179d3d3d6b7STobias Grosser return ScatterSpace.add_dims(isl::dim::set, Dims);
180d1508812SMichael Kruse }
181d1508812SMichael Kruse
makeIdentityMap(const isl::set & Set,bool RestrictDomain)182a56bd7deSMichael Kruse isl::map polly::makeIdentityMap(const isl::set &Set, bool RestrictDomain) {
183a56bd7deSMichael Kruse isl::map Result = isl::map::identity(Set.get_space().map_from_set());
184a56bd7deSMichael Kruse if (RestrictDomain)
185a56bd7deSMichael Kruse Result = Result.intersect_domain(Set);
186a56bd7deSMichael Kruse return Result;
187a56bd7deSMichael Kruse }
188a56bd7deSMichael Kruse
makeIdentityMap(const isl::union_set & USet,bool RestrictDomain)189deaef15fSTobias Grosser isl::union_map polly::makeIdentityMap(const isl::union_set &USet,
190d1508812SMichael Kruse bool RestrictDomain) {
191bad3ebbaSRiccardo Mori isl::union_map Result = isl::union_map::empty(USet.ctx());
1921696e48eSTobias Grosser for (isl::set Set : USet.get_set_list()) {
193a56bd7deSMichael Kruse isl::map IdentityMap = makeIdentityMap(Set, RestrictDomain);
194d5ee355fSRiccardo Mori Result = Result.unite(IdentityMap);
1951696e48eSTobias Grosser }
196d1508812SMichael Kruse return Result;
197d1508812SMichael Kruse }
198d1508812SMichael Kruse
reverseDomain(isl::map Map)199deaef15fSTobias Grosser isl::map polly::reverseDomain(isl::map Map) {
200d3d3d6b7STobias Grosser isl::space DomSpace = Map.get_space().domain().unwrap();
201d3d3d6b7STobias Grosser isl::space Space1 = DomSpace.domain();
202d3d3d6b7STobias Grosser isl::space Space2 = DomSpace.range();
203d3d3d6b7STobias Grosser isl::map Swap = makeTupleSwapMap(Space1, Space2);
204d3d3d6b7STobias Grosser return Map.apply_domain(Swap);
205d1508812SMichael Kruse }
206d1508812SMichael Kruse
reverseDomain(const isl::union_map & UMap)207deaef15fSTobias Grosser isl::union_map polly::reverseDomain(const isl::union_map &UMap) {
208bad3ebbaSRiccardo Mori isl::union_map Result = isl::union_map::empty(UMap.ctx());
2091696e48eSTobias Grosser for (isl::map Map : UMap.get_map_list()) {
210d1508812SMichael Kruse auto Reversed = reverseDomain(std::move(Map));
211d5ee355fSRiccardo Mori Result = Result.unite(Reversed);
2121696e48eSTobias Grosser }
213d1508812SMichael Kruse return Result;
214d1508812SMichael Kruse }
215d1508812SMichael Kruse
shiftDim(isl::set Set,int Pos,int Amount)216deaef15fSTobias Grosser isl::set polly::shiftDim(isl::set Set, int Pos, int Amount) {
21744596fe6SRiccardo Mori unsigned NumDims = unsignedFromIslSize(Set.tuple_dim());
218d1508812SMichael Kruse if (Pos < 0)
219d1508812SMichael Kruse Pos = NumDims + Pos;
2200b39ec83SFangrui Song assert(unsigned(Pos) < NumDims && "Dimension index must be in range");
221d3d3d6b7STobias Grosser isl::space Space = Set.get_space();
222d3d3d6b7STobias Grosser Space = Space.map_from_domain_and_range(Space);
223d3d3d6b7STobias Grosser isl::multi_aff Translator = makeShiftDimAff(Space, Pos, Amount);
224d3d3d6b7STobias Grosser isl::map TranslatorMap = isl::map::from_multi_aff(Translator);
225d3d3d6b7STobias Grosser return Set.apply(TranslatorMap);
226d1508812SMichael Kruse }
227d1508812SMichael Kruse
shiftDim(isl::union_set USet,int Pos,int Amount)228deaef15fSTobias Grosser isl::union_set polly::shiftDim(isl::union_set USet, int Pos, int Amount) {
229bad3ebbaSRiccardo Mori isl::union_set Result = isl::union_set::empty(USet.ctx());
2301696e48eSTobias Grosser for (isl::set Set : USet.get_set_list()) {
231d3d3d6b7STobias Grosser isl::set Shifted = shiftDim(Set, Pos, Amount);
232b55aedd0Spatacca Result = Result.unite(Shifted);
2331696e48eSTobias Grosser }
234d1508812SMichael Kruse return Result;
235d1508812SMichael Kruse }
236d1508812SMichael Kruse
shiftDim(isl::map Map,isl::dim Dim,int Pos,int Amount)237174f4839SMichael Kruse isl::map polly::shiftDim(isl::map Map, isl::dim Dim, int Pos, int Amount) {
23844596fe6SRiccardo Mori unsigned NumDims = unsignedFromIslSize(Map.dim(Dim));
239174f4839SMichael Kruse if (Pos < 0)
240174f4839SMichael Kruse Pos = NumDims + Pos;
2410b39ec83SFangrui Song assert(unsigned(Pos) < NumDims && "Dimension index must be in range");
242d3d3d6b7STobias Grosser isl::space Space = Map.get_space();
243174f4839SMichael Kruse switch (Dim) {
244174f4839SMichael Kruse case isl::dim::in:
245d3d3d6b7STobias Grosser Space = Space.domain();
246174f4839SMichael Kruse break;
247174f4839SMichael Kruse case isl::dim::out:
248d3d3d6b7STobias Grosser Space = Space.range();
249174f4839SMichael Kruse break;
250174f4839SMichael Kruse default:
251174f4839SMichael Kruse llvm_unreachable("Unsupported value for 'dim'");
252174f4839SMichael Kruse }
253d3d3d6b7STobias Grosser Space = Space.map_from_domain_and_range(Space);
254d3d3d6b7STobias Grosser isl::multi_aff Translator = makeShiftDimAff(Space, Pos, Amount);
255d3d3d6b7STobias Grosser isl::map TranslatorMap = isl::map::from_multi_aff(Translator);
256174f4839SMichael Kruse switch (Dim) {
257174f4839SMichael Kruse case isl::dim::in:
258174f4839SMichael Kruse return Map.apply_domain(TranslatorMap);
259174f4839SMichael Kruse case isl::dim::out:
260174f4839SMichael Kruse return Map.apply_range(TranslatorMap);
261174f4839SMichael Kruse default:
262174f4839SMichael Kruse llvm_unreachable("Unsupported value for 'dim'");
263174f4839SMichael Kruse }
264174f4839SMichael Kruse }
265174f4839SMichael Kruse
getConstant(isl::map Map,isl::dim Dim,int Pos)266*b02c7e2bSRoman Gareev isl::val polly::getConstant(isl::map Map, isl::dim Dim, int Pos) {
267*b02c7e2bSRoman Gareev unsigned NumDims = unsignedFromIslSize(Map.dim(Dim));
268*b02c7e2bSRoman Gareev if (Pos < 0)
269*b02c7e2bSRoman Gareev Pos = NumDims + Pos;
270*b02c7e2bSRoman Gareev assert(unsigned(Pos) < NumDims && "Dimension index must be in range");
271*b02c7e2bSRoman Gareev // TODO: The isl_map_plain_get_val_if_fixed function is not robust, since its
272*b02c7e2bSRoman Gareev // result is different depending on the internal representation.
273*b02c7e2bSRoman Gareev // Replace it with a different implementation.
274*b02c7e2bSRoman Gareev return isl::manage(isl_map_plain_get_val_if_fixed(
275*b02c7e2bSRoman Gareev Map.get(), static_cast<enum isl_dim_type>(Dim), Pos));
276*b02c7e2bSRoman Gareev }
277*b02c7e2bSRoman Gareev
shiftDim(isl::union_map UMap,isl::dim Dim,int Pos,int Amount)278174f4839SMichael Kruse isl::union_map polly::shiftDim(isl::union_map UMap, isl::dim Dim, int Pos,
279174f4839SMichael Kruse int Amount) {
280bad3ebbaSRiccardo Mori isl::union_map Result = isl::union_map::empty(UMap.ctx());
281174f4839SMichael Kruse
2821696e48eSTobias Grosser for (isl::map Map : UMap.get_map_list()) {
283d3d3d6b7STobias Grosser isl::map Shifted = shiftDim(Map, Dim, Pos, Amount);
284d5ee355fSRiccardo Mori Result = Result.unite(Shifted);
2851696e48eSTobias Grosser }
286174f4839SMichael Kruse return Result;
287174f4839SMichael Kruse }
288174f4839SMichael Kruse
simplify(isl::set & Set)289deaef15fSTobias Grosser void polly::simplify(isl::set &Set) {
290d3d3d6b7STobias Grosser Set = isl::manage(isl_set_compute_divs(Set.copy()));
291d3d3d6b7STobias Grosser Set = Set.detect_equalities();
292d3d3d6b7STobias Grosser Set = Set.coalesce();
293d1508812SMichael Kruse }
294d1508812SMichael Kruse
simplify(isl::union_set & USet)295deaef15fSTobias Grosser void polly::simplify(isl::union_set &USet) {
296d3d3d6b7STobias Grosser USet = isl::manage(isl_union_set_compute_divs(USet.copy()));
297d3d3d6b7STobias Grosser USet = USet.detect_equalities();
298d3d3d6b7STobias Grosser USet = USet.coalesce();
299d1508812SMichael Kruse }
300d1508812SMichael Kruse
simplify(isl::map & Map)301deaef15fSTobias Grosser void polly::simplify(isl::map &Map) {
302d3d3d6b7STobias Grosser Map = isl::manage(isl_map_compute_divs(Map.copy()));
303d3d3d6b7STobias Grosser Map = Map.detect_equalities();
304d3d3d6b7STobias Grosser Map = Map.coalesce();
305d1508812SMichael Kruse }
306d1508812SMichael Kruse
simplify(isl::union_map & UMap)307deaef15fSTobias Grosser void polly::simplify(isl::union_map &UMap) {
308d3d3d6b7STobias Grosser UMap = isl::manage(isl_union_map_compute_divs(UMap.copy()));
309d3d3d6b7STobias Grosser UMap = UMap.detect_equalities();
310d3d3d6b7STobias Grosser UMap = UMap.coalesce();
311d1508812SMichael Kruse }
312f4dc133eSMichael Kruse
computeReachingWrite(isl::union_map Schedule,isl::union_map Writes,bool Reverse,bool InclPrevDef,bool InclNextDef)313deaef15fSTobias Grosser isl::union_map polly::computeReachingWrite(isl::union_map Schedule,
314deaef15fSTobias Grosser isl::union_map Writes, bool Reverse,
315f4dc133eSMichael Kruse bool InclPrevDef, bool InclNextDef) {
316f4dc133eSMichael Kruse
317f4dc133eSMichael Kruse // { Scatter[] }
31825bd602bSMichael Kruse isl::space ScatterSpace = getScatterSpace(Schedule);
319f4dc133eSMichael Kruse
320f4dc133eSMichael Kruse // { ScatterRead[] -> ScatterWrite[] }
321deaef15fSTobias Grosser isl::map Relation;
322f4dc133eSMichael Kruse if (Reverse)
32325bd602bSMichael Kruse Relation = InclPrevDef ? isl::map::lex_lt(ScatterSpace)
32425bd602bSMichael Kruse : isl::map::lex_le(ScatterSpace);
325f4dc133eSMichael Kruse else
32625bd602bSMichael Kruse Relation = InclNextDef ? isl::map::lex_gt(ScatterSpace)
32725bd602bSMichael Kruse : isl::map::lex_ge(ScatterSpace);
328f4dc133eSMichael Kruse
329f4dc133eSMichael Kruse // { ScatterWrite[] -> [ScatterRead[] -> ScatterWrite[]] }
33025bd602bSMichael Kruse isl::map RelationMap = Relation.range_map().reverse();
331f4dc133eSMichael Kruse
332f4dc133eSMichael Kruse // { Element[] -> ScatterWrite[] }
33325bd602bSMichael Kruse isl::union_map WriteAction = Schedule.apply_domain(Writes);
334f4dc133eSMichael Kruse
335f4dc133eSMichael Kruse // { ScatterWrite[] -> Element[] }
33625bd602bSMichael Kruse isl::union_map WriteActionRev = WriteAction.reverse();
337f4dc133eSMichael Kruse
338f4dc133eSMichael Kruse // { Element[] -> [ScatterUse[] -> ScatterWrite[]] }
33925bd602bSMichael Kruse isl::union_map DefSchedRelation =
34025bd602bSMichael Kruse isl::union_map(RelationMap).apply_domain(WriteActionRev);
341f4dc133eSMichael Kruse
342f4dc133eSMichael Kruse // For each element, at every point in time, map to the times of previous
343f4dc133eSMichael Kruse // definitions. { [Element[] -> ScatterRead[]] -> ScatterWrite[] }
34425bd602bSMichael Kruse isl::union_map ReachableWrites = DefSchedRelation.uncurry();
345f4dc133eSMichael Kruse if (Reverse)
34625bd602bSMichael Kruse ReachableWrites = ReachableWrites.lexmin();
347f4dc133eSMichael Kruse else
34825bd602bSMichael Kruse ReachableWrites = ReachableWrites.lexmax();
349f4dc133eSMichael Kruse
350f4dc133eSMichael Kruse // { [Element[] -> ScatterWrite[]] -> ScatterWrite[] }
35125bd602bSMichael Kruse isl::union_map SelfUse = WriteAction.range_map();
352f4dc133eSMichael Kruse
353f4dc133eSMichael Kruse if (InclPrevDef && InclNextDef) {
354f4dc133eSMichael Kruse // Add the Def itself to the solution.
35525bd602bSMichael Kruse ReachableWrites = ReachableWrites.unite(SelfUse).coalesce();
356f4dc133eSMichael Kruse } else if (!InclPrevDef && !InclNextDef) {
357f4dc133eSMichael Kruse // Remove Def itself from the solution.
35825bd602bSMichael Kruse ReachableWrites = ReachableWrites.subtract(SelfUse);
359f4dc133eSMichael Kruse }
360f4dc133eSMichael Kruse
361f4dc133eSMichael Kruse // { [Element[] -> ScatterRead[]] -> Domain[] }
36225bd602bSMichael Kruse return ReachableWrites.apply_range(Schedule.reverse());
363f4dc133eSMichael Kruse }
364ec67d364SMichael Kruse
365deaef15fSTobias Grosser isl::union_map
computeArrayUnused(isl::union_map Schedule,isl::union_map Writes,isl::union_map Reads,bool ReadEltInSameInst,bool IncludeLastRead,bool IncludeWrite)366deaef15fSTobias Grosser polly::computeArrayUnused(isl::union_map Schedule, isl::union_map Writes,
367deaef15fSTobias Grosser isl::union_map Reads, bool ReadEltInSameInst,
368deaef15fSTobias Grosser bool IncludeLastRead, bool IncludeWrite) {
369ec67d364SMichael Kruse // { Element[] -> Scatter[] }
370d3d3d6b7STobias Grosser isl::union_map ReadActions = Schedule.apply_domain(Reads);
371d3d3d6b7STobias Grosser isl::union_map WriteActions = Schedule.apply_domain(Writes);
372ec67d364SMichael Kruse
373ec67d364SMichael Kruse // { [Element[] -> DomainWrite[]] -> Scatter[] }
374d3d3d6b7STobias Grosser isl::union_map EltDomWrites =
375d3d3d6b7STobias Grosser Writes.reverse().range_map().apply_range(Schedule);
376ec67d364SMichael Kruse
377ec67d364SMichael Kruse // { [Element[] -> Scatter[]] -> DomainWrite[] }
378d3d3d6b7STobias Grosser isl::union_map ReachingOverwrite = computeReachingWrite(
379ec67d364SMichael Kruse Schedule, Writes, true, ReadEltInSameInst, !ReadEltInSameInst);
380ec67d364SMichael Kruse
381ec67d364SMichael Kruse // { [Element[] -> Scatter[]] -> DomainWrite[] }
382d3d3d6b7STobias Grosser isl::union_map ReadsOverwritten =
383d3d3d6b7STobias Grosser ReachingOverwrite.intersect_domain(ReadActions.wrap());
384ec67d364SMichael Kruse
385ec67d364SMichael Kruse // { [Element[] -> DomainWrite[]] -> Scatter[] }
386d3d3d6b7STobias Grosser isl::union_map ReadsOverwrittenRotated =
387d3d3d6b7STobias Grosser reverseDomain(ReadsOverwritten).curry().reverse();
388d3d3d6b7STobias Grosser isl::union_map LastOverwrittenRead = ReadsOverwrittenRotated.lexmax();
389ec67d364SMichael Kruse
390ec67d364SMichael Kruse // { [Element[] -> DomainWrite[]] -> Scatter[] }
391d3d3d6b7STobias Grosser isl::union_map BetweenLastReadOverwrite = betweenScatter(
392ec67d364SMichael Kruse LastOverwrittenRead, EltDomWrites, IncludeLastRead, IncludeWrite);
393ec67d364SMichael Kruse
394ade14269SMichael Kruse // { [Element[] -> Scatter[]] -> DomainWrite[] }
395ade14269SMichael Kruse isl::union_map ReachingOverwriteZone = computeReachingWrite(
396ade14269SMichael Kruse Schedule, Writes, true, IncludeLastRead, IncludeWrite);
397ade14269SMichael Kruse
398ade14269SMichael Kruse // { [Element[] -> DomainWrite[]] -> Scatter[] }
399ade14269SMichael Kruse isl::union_map ReachingOverwriteRotated =
400ade14269SMichael Kruse reverseDomain(ReachingOverwriteZone).curry().reverse();
401ade14269SMichael Kruse
402ade14269SMichael Kruse // { [Element[] -> DomainWrite[]] -> Scatter[] }
403ade14269SMichael Kruse isl::union_map WritesWithoutReads = ReachingOverwriteRotated.subtract_domain(
404ade14269SMichael Kruse ReadsOverwrittenRotated.domain());
405ade14269SMichael Kruse
406ade14269SMichael Kruse return BetweenLastReadOverwrite.unite(WritesWithoutReads)
407ade14269SMichael Kruse .domain_factor_domain();
408ec67d364SMichael Kruse }
409acb08aaeSMichael Kruse
convertZoneToTimepoints(isl::union_set Zone,bool InclStart,bool InclEnd)410deaef15fSTobias Grosser isl::union_set polly::convertZoneToTimepoints(isl::union_set Zone,
411deaef15fSTobias Grosser bool InclStart, bool InclEnd) {
412acb08aaeSMichael Kruse if (!InclStart && InclEnd)
413acb08aaeSMichael Kruse return Zone;
414acb08aaeSMichael Kruse
415acb08aaeSMichael Kruse auto ShiftedZone = shiftDim(Zone, -1, -1);
416acb08aaeSMichael Kruse if (InclStart && !InclEnd)
417acb08aaeSMichael Kruse return ShiftedZone;
418acb08aaeSMichael Kruse else if (!InclStart && !InclEnd)
419d3d3d6b7STobias Grosser return Zone.intersect(ShiftedZone);
420acb08aaeSMichael Kruse
421acb08aaeSMichael Kruse assert(InclStart && InclEnd);
422d3d3d6b7STobias Grosser return Zone.unite(ShiftedZone);
423acb08aaeSMichael Kruse }
424174f4839SMichael Kruse
convertZoneToTimepoints(isl::union_map Zone,isl::dim Dim,bool InclStart,bool InclEnd)425174f4839SMichael Kruse isl::union_map polly::convertZoneToTimepoints(isl::union_map Zone, isl::dim Dim,
426174f4839SMichael Kruse bool InclStart, bool InclEnd) {
427174f4839SMichael Kruse if (!InclStart && InclEnd)
428174f4839SMichael Kruse return Zone;
429174f4839SMichael Kruse
430174f4839SMichael Kruse auto ShiftedZone = shiftDim(Zone, Dim, -1, -1);
431174f4839SMichael Kruse if (InclStart && !InclEnd)
432174f4839SMichael Kruse return ShiftedZone;
433174f4839SMichael Kruse else if (!InclStart && !InclEnd)
434d3d3d6b7STobias Grosser return Zone.intersect(ShiftedZone);
435174f4839SMichael Kruse
436174f4839SMichael Kruse assert(InclStart && InclEnd);
437d3d3d6b7STobias Grosser return Zone.unite(ShiftedZone);
438174f4839SMichael Kruse }
439174f4839SMichael Kruse
convertZoneToTimepoints(isl::map Zone,isl::dim Dim,bool InclStart,bool InclEnd)44070af4f57SMichael Kruse isl::map polly::convertZoneToTimepoints(isl::map Zone, isl::dim Dim,
44170af4f57SMichael Kruse bool InclStart, bool InclEnd) {
44270af4f57SMichael Kruse if (!InclStart && InclEnd)
44370af4f57SMichael Kruse return Zone;
44470af4f57SMichael Kruse
44570af4f57SMichael Kruse auto ShiftedZone = shiftDim(Zone, Dim, -1, -1);
44670af4f57SMichael Kruse if (InclStart && !InclEnd)
44770af4f57SMichael Kruse return ShiftedZone;
44870af4f57SMichael Kruse else if (!InclStart && !InclEnd)
449d3d3d6b7STobias Grosser return Zone.intersect(ShiftedZone);
45070af4f57SMichael Kruse
45170af4f57SMichael Kruse assert(InclStart && InclEnd);
452d3d3d6b7STobias Grosser return Zone.unite(ShiftedZone);
45370af4f57SMichael Kruse }
45470af4f57SMichael Kruse
distributeDomain(isl::map Map)455174f4839SMichael Kruse isl::map polly::distributeDomain(isl::map Map) {
456174f4839SMichael Kruse // Note that we cannot take Map apart into { Domain[] -> Range1[] } and {
457174f4839SMichael Kruse // Domain[] -> Range2[] } and combine again. We would loose any relation
458174f4839SMichael Kruse // between Range1[] and Range2[] that is not also a constraint to Domain[].
459174f4839SMichael Kruse
460d3d3d6b7STobias Grosser isl::space Space = Map.get_space();
461d3d3d6b7STobias Grosser isl::space DomainSpace = Space.domain();
4627c7978a1Spatacca if (DomainSpace.is_null())
4631ef55ac9SMichael Kruse return {};
46444596fe6SRiccardo Mori unsigned DomainDims = unsignedFromIslSize(DomainSpace.dim(isl::dim::set));
465d3d3d6b7STobias Grosser isl::space RangeSpace = Space.range().unwrap();
466d3d3d6b7STobias Grosser isl::space Range1Space = RangeSpace.domain();
4677c7978a1Spatacca if (Range1Space.is_null())
4681ef55ac9SMichael Kruse return {};
46944596fe6SRiccardo Mori unsigned Range1Dims = unsignedFromIslSize(Range1Space.dim(isl::dim::set));
470d3d3d6b7STobias Grosser isl::space Range2Space = RangeSpace.range();
4717c7978a1Spatacca if (Range2Space.is_null())
4721ef55ac9SMichael Kruse return {};
47344596fe6SRiccardo Mori unsigned Range2Dims = unsignedFromIslSize(Range2Space.dim(isl::dim::set));
474174f4839SMichael Kruse
475d3d3d6b7STobias Grosser isl::space OutputSpace =
476d3d3d6b7STobias Grosser DomainSpace.map_from_domain_and_range(Range1Space)
477d3d3d6b7STobias Grosser .wrap()
478d3d3d6b7STobias Grosser .map_from_domain_and_range(
479d3d3d6b7STobias Grosser DomainSpace.map_from_domain_and_range(Range2Space).wrap());
480174f4839SMichael Kruse
481d3d3d6b7STobias Grosser isl::basic_map Translator = isl::basic_map::universe(
482d3d3d6b7STobias Grosser Space.wrap().map_from_domain_and_range(OutputSpace.wrap()));
483174f4839SMichael Kruse
484174f4839SMichael Kruse for (unsigned i = 0; i < DomainDims; i += 1) {
485d3d3d6b7STobias Grosser Translator = Translator.equate(isl::dim::in, i, isl::dim::out, i);
486d3d3d6b7STobias Grosser Translator = Translator.equate(isl::dim::in, i, isl::dim::out,
487d3d3d6b7STobias Grosser DomainDims + Range1Dims + i);
488174f4839SMichael Kruse }
489d3d3d6b7STobias Grosser for (unsigned i = 0; i < Range1Dims; i += 1)
490d3d3d6b7STobias Grosser Translator = Translator.equate(isl::dim::in, DomainDims + i, isl::dim::out,
491d3d3d6b7STobias Grosser DomainDims + i);
492d3d3d6b7STobias Grosser for (unsigned i = 0; i < Range2Dims; i += 1)
493d3d3d6b7STobias Grosser Translator = Translator.equate(isl::dim::in, DomainDims + Range1Dims + i,
494d3d3d6b7STobias Grosser isl::dim::out,
495d3d3d6b7STobias Grosser DomainDims + Range1Dims + DomainDims + i);
496174f4839SMichael Kruse
497d3d3d6b7STobias Grosser return Map.wrap().apply(Translator).unwrap();
498174f4839SMichael Kruse }
499174f4839SMichael Kruse
distributeDomain(isl::union_map UMap)500174f4839SMichael Kruse isl::union_map polly::distributeDomain(isl::union_map UMap) {
501bad3ebbaSRiccardo Mori isl::union_map Result = isl::union_map::empty(UMap.ctx());
5021696e48eSTobias Grosser for (isl::map Map : UMap.get_map_list()) {
503174f4839SMichael Kruse auto Distributed = distributeDomain(Map);
504d5ee355fSRiccardo Mori Result = Result.unite(Distributed);
5051696e48eSTobias Grosser }
506174f4839SMichael Kruse return Result;
507174f4839SMichael Kruse }
508174f4839SMichael Kruse
liftDomains(isl::union_map UMap,isl::union_set Factor)509174f4839SMichael Kruse isl::union_map polly::liftDomains(isl::union_map UMap, isl::union_set Factor) {
510174f4839SMichael Kruse
511174f4839SMichael Kruse // { Factor[] -> Factor[] }
512d3d3d6b7STobias Grosser isl::union_map Factors = makeIdentityMap(Factor, true);
513174f4839SMichael Kruse
514d3d3d6b7STobias Grosser return Factors.product(UMap);
515174f4839SMichael Kruse }
516174f4839SMichael Kruse
applyDomainRange(isl::union_map UMap,isl::union_map Func)517174f4839SMichael Kruse isl::union_map polly::applyDomainRange(isl::union_map UMap,
518174f4839SMichael Kruse isl::union_map Func) {
519174f4839SMichael Kruse // This implementation creates unnecessary cross products of the
520174f4839SMichael Kruse // DomainDomain[] and Func. An alternative implementation could reverse
521174f4839SMichael Kruse // domain+uncurry,apply Func to what now is the domain, then undo the
522174f4839SMichael Kruse // preparing transformation. Another alternative implementation could create a
523174f4839SMichael Kruse // translator map for each piece.
524174f4839SMichael Kruse
525174f4839SMichael Kruse // { DomainDomain[] }
526d3d3d6b7STobias Grosser isl::union_set DomainDomain = UMap.domain().unwrap().domain();
527174f4839SMichael Kruse
528174f4839SMichael Kruse // { [DomainDomain[] -> DomainRange[]] -> [DomainDomain[] -> NewDomainRange[]]
529174f4839SMichael Kruse // }
530d3d3d6b7STobias Grosser isl::union_map LifetedFunc = liftDomains(std::move(Func), DomainDomain);
531174f4839SMichael Kruse
532d3d3d6b7STobias Grosser return UMap.apply_domain(LifetedFunc);
533174f4839SMichael Kruse }
53447281843SMichael Kruse
intersectRange(isl::map Map,isl::union_set Range)53547281843SMichael Kruse isl::map polly::intersectRange(isl::map Map, isl::union_set Range) {
53647281843SMichael Kruse isl::set RangeSet = Range.extract_set(Map.get_space().range());
53747281843SMichael Kruse return Map.intersect_range(RangeSet);
53847281843SMichael Kruse }
539ed787e75SMichael Kruse
subtractParams(isl::map Map,isl::set Params)5402698390cSMichael Kruse isl::map polly::subtractParams(isl::map Map, isl::set Params) {
5412698390cSMichael Kruse auto MapSpace = Map.get_space();
5422698390cSMichael Kruse auto ParamsMap = isl::map::universe(MapSpace).intersect_params(Params);
5432698390cSMichael Kruse return Map.subtract(ParamsMap);
5442698390cSMichael Kruse }
5452698390cSMichael Kruse
subtractParams(isl::set Set,isl::set Params)546bc633fe4SMichael Kruse isl::set polly::subtractParams(isl::set Set, isl::set Params) {
547bc633fe4SMichael Kruse isl::space SetSpace = Set.get_space();
548bc633fe4SMichael Kruse isl::set ParamsSet = isl::set::universe(SetSpace).intersect_params(Params);
549bc633fe4SMichael Kruse return Set.subtract(ParamsSet);
550bc633fe4SMichael Kruse }
551bc633fe4SMichael Kruse
getConstant(isl::pw_aff PwAff,bool Max,bool Min)552ed787e75SMichael Kruse isl::val polly::getConstant(isl::pw_aff PwAff, bool Max, bool Min) {
553ed787e75SMichael Kruse assert(!Max || !Min); // Cannot return min and max at the same time.
554ed787e75SMichael Kruse isl::val Result;
555a2fd4419STobias Grosser isl::stat Stat = PwAff.foreach_piece(
556a2fd4419STobias Grosser [=, &Result](isl::set Set, isl::aff Aff) -> isl::stat {
5577c7978a1Spatacca if (!Result.is_null() && Result.is_nan())
558a2fd4419STobias Grosser return isl::stat::ok();
559ed787e75SMichael Kruse
560ed787e75SMichael Kruse // TODO: If Min/Max, we can also determine a minimum/maximum value if
561ed787e75SMichael Kruse // Set is constant-bounded.
562ed787e75SMichael Kruse if (!Aff.is_cst()) {
5630813bd16SRiccardo Mori Result = isl::val::nan(Aff.ctx());
564a2fd4419STobias Grosser return isl::stat::error();
565ed787e75SMichael Kruse }
566ed787e75SMichael Kruse
567ed787e75SMichael Kruse isl::val ThisVal = Aff.get_constant_val();
5687c7978a1Spatacca if (Result.is_null()) {
569ed787e75SMichael Kruse Result = ThisVal;
570a2fd4419STobias Grosser return isl::stat::ok();
571ed787e75SMichael Kruse }
572ed787e75SMichael Kruse
573ed787e75SMichael Kruse if (Result.eq(ThisVal))
574a2fd4419STobias Grosser return isl::stat::ok();
575ed787e75SMichael Kruse
576ed787e75SMichael Kruse if (Max && ThisVal.gt(Result)) {
577ed787e75SMichael Kruse Result = ThisVal;
578a2fd4419STobias Grosser return isl::stat::ok();
579ed787e75SMichael Kruse }
580ed787e75SMichael Kruse
581ed787e75SMichael Kruse if (Min && ThisVal.lt(Result)) {
582ed787e75SMichael Kruse Result = ThisVal;
583a2fd4419STobias Grosser return isl::stat::ok();
584ed787e75SMichael Kruse }
585ed787e75SMichael Kruse
586ed787e75SMichael Kruse // Not compatible
5870813bd16SRiccardo Mori Result = isl::val::nan(Aff.ctx());
588a2fd4419STobias Grosser return isl::stat::error();
589ed787e75SMichael Kruse });
590a2fd4419STobias Grosser
591a2fd4419STobias Grosser if (Stat.is_error())
592a2fd4419STobias Grosser return {};
593a2fd4419STobias Grosser
594ed787e75SMichael Kruse return Result;
595ed787e75SMichael Kruse }
596ed787e75SMichael Kruse
rangeIslSize(unsigned Begin,isl::size End)59744596fe6SRiccardo Mori llvm::iota_range<unsigned> polly::rangeIslSize(unsigned Begin, isl::size End) {
59844596fe6SRiccardo Mori unsigned UEnd = unsignedFromIslSize(End);
59944596fe6SRiccardo Mori return llvm::seq<unsigned>(std::min(Begin, UEnd), UEnd);
60044596fe6SRiccardo Mori }
60144596fe6SRiccardo Mori
602ed787e75SMichael Kruse #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
foreachPoint(const isl::set & Set,const std::function<void (isl::point P)> & F)603ed787e75SMichael Kruse static void foreachPoint(const isl::set &Set,
604ed787e75SMichael Kruse const std::function<void(isl::point P)> &F) {
605d3d3d6b7STobias Grosser Set.foreach_point([&](isl::point P) -> isl::stat {
606d3d3d6b7STobias Grosser F(P);
607a2fd4419STobias Grosser return isl::stat::ok();
608d3d3d6b7STobias Grosser });
609ed787e75SMichael Kruse }
610ed787e75SMichael Kruse
foreachPoint(isl::basic_set BSet,const std::function<void (isl::point P)> & F)611ed787e75SMichael Kruse static void foreachPoint(isl::basic_set BSet,
612ed787e75SMichael Kruse const std::function<void(isl::point P)> &F) {
613d3d3d6b7STobias Grosser foreachPoint(isl::set(BSet), F);
614ed787e75SMichael Kruse }
615ed787e75SMichael Kruse
616ed787e75SMichael Kruse /// Determine the sorting order of the sets @p A and @p B without considering
617ed787e75SMichael Kruse /// the space structure.
618ed787e75SMichael Kruse ///
619ed787e75SMichael Kruse /// Ordering is based on the lower bounds of the set's dimensions. First
620ed787e75SMichael Kruse /// dimensions are considered first.
flatCompare(const isl::basic_set & A,const isl::basic_set & B)621ed787e75SMichael Kruse static int flatCompare(const isl::basic_set &A, const isl::basic_set &B) {
6221ef55ac9SMichael Kruse // Quick bail-out on out-of-quota.
6237c7978a1Spatacca if (A.is_null() || B.is_null())
6241ef55ac9SMichael Kruse return 0;
6251ef55ac9SMichael Kruse
62644596fe6SRiccardo Mori unsigned ALen = unsignedFromIslSize(A.dim(isl::dim::set));
62744596fe6SRiccardo Mori unsigned BLen = unsignedFromIslSize(B.dim(isl::dim::set));
628d3d3d6b7STobias Grosser unsigned Len = std::min(ALen, BLen);
629ed787e75SMichael Kruse
630d3d3d6b7STobias Grosser for (unsigned i = 0; i < Len; i += 1) {
631ed787e75SMichael Kruse isl::basic_set ADim =
63244596fe6SRiccardo Mori A.project_out(isl::dim::param, 0,
63344596fe6SRiccardo Mori unsignedFromIslSize(A.dim(isl::dim::param)))
634ed787e75SMichael Kruse .project_out(isl::dim::set, i + 1, ALen - i - 1)
635ed787e75SMichael Kruse .project_out(isl::dim::set, 0, i);
636ed787e75SMichael Kruse isl::basic_set BDim =
63744596fe6SRiccardo Mori B.project_out(isl::dim::param, 0,
63844596fe6SRiccardo Mori unsignedFromIslSize(B.dim(isl::dim::param)))
639ed787e75SMichael Kruse .project_out(isl::dim::set, i + 1, BLen - i - 1)
640ed787e75SMichael Kruse .project_out(isl::dim::set, 0, i);
641ed787e75SMichael Kruse
642ed787e75SMichael Kruse isl::basic_set AHull = isl::set(ADim).convex_hull();
643ed787e75SMichael Kruse isl::basic_set BHull = isl::set(BDim).convex_hull();
644ed787e75SMichael Kruse
645ed787e75SMichael Kruse bool ALowerBounded =
646ed787e75SMichael Kruse bool(isl::set(AHull).dim_has_any_lower_bound(isl::dim::set, 0));
647ed787e75SMichael Kruse bool BLowerBounded =
648ed787e75SMichael Kruse bool(isl::set(BHull).dim_has_any_lower_bound(isl::dim::set, 0));
649ed787e75SMichael Kruse
650ed787e75SMichael Kruse int BoundedCompare = BLowerBounded - ALowerBounded;
651ed787e75SMichael Kruse if (BoundedCompare != 0)
652ed787e75SMichael Kruse return BoundedCompare;
653ed787e75SMichael Kruse
654ed787e75SMichael Kruse if (!ALowerBounded || !BLowerBounded)
655ed787e75SMichael Kruse continue;
656ed787e75SMichael Kruse
657ed787e75SMichael Kruse isl::pw_aff AMin = isl::set(ADim).dim_min(0);
658ed787e75SMichael Kruse isl::pw_aff BMin = isl::set(BDim).dim_min(0);
659ed787e75SMichael Kruse
660ed787e75SMichael Kruse isl::val AMinVal = polly::getConstant(AMin, false, true);
661ed787e75SMichael Kruse isl::val BMinVal = polly::getConstant(BMin, false, true);
662ed787e75SMichael Kruse
663ed787e75SMichael Kruse int MinCompare = AMinVal.sub(BMinVal).sgn();
664ed787e75SMichael Kruse if (MinCompare != 0)
665ed787e75SMichael Kruse return MinCompare;
666ed787e75SMichael Kruse }
667ed787e75SMichael Kruse
668ed787e75SMichael Kruse // If all the dimensions' lower bounds are equal or incomparable, sort based
669ed787e75SMichael Kruse // on the number of dimensions.
670ed787e75SMichael Kruse return ALen - BLen;
671ed787e75SMichael Kruse }
672ed787e75SMichael Kruse
673a0db63a1SMichael Kruse /// Compare the sets @p A and @p B according to their nested space structure.
674a0db63a1SMichael Kruse /// Returns 0 if the structure is considered equal.
675a0db63a1SMichael Kruse /// If @p ConsiderTupleLen is false, the number of dimensions in a tuple are
676a0db63a1SMichael Kruse /// ignored, i.e. a tuple with the same name but different number of dimensions
677a0db63a1SMichael Kruse /// are considered equal.
structureCompare(const isl::space & ASpace,const isl::space & BSpace,bool ConsiderTupleLen)678a0db63a1SMichael Kruse static int structureCompare(const isl::space &ASpace, const isl::space &BSpace,
679a0db63a1SMichael Kruse bool ConsiderTupleLen) {
680ed787e75SMichael Kruse int WrappingCompare = bool(ASpace.is_wrapping()) - bool(BSpace.is_wrapping());
681ed787e75SMichael Kruse if (WrappingCompare != 0)
682ed787e75SMichael Kruse return WrappingCompare;
683ed787e75SMichael Kruse
684a0db63a1SMichael Kruse if (ASpace.is_wrapping() && BSpace.is_wrapping()) {
685a0db63a1SMichael Kruse isl::space AMap = ASpace.unwrap();
686a0db63a1SMichael Kruse isl::space BMap = BSpace.unwrap();
687ed787e75SMichael Kruse
688a0db63a1SMichael Kruse int FirstResult =
689a0db63a1SMichael Kruse structureCompare(AMap.domain(), BMap.domain(), ConsiderTupleLen);
690ed787e75SMichael Kruse if (FirstResult != 0)
691ed787e75SMichael Kruse return FirstResult;
692ed787e75SMichael Kruse
693a0db63a1SMichael Kruse return structureCompare(AMap.range(), BMap.range(), ConsiderTupleLen);
694ed787e75SMichael Kruse }
695ed787e75SMichael Kruse
696a0db63a1SMichael Kruse std::string AName;
69702e8a5adSMichael Kruse if (!ASpace.is_params() && ASpace.has_tuple_name(isl::dim::set))
698a0db63a1SMichael Kruse AName = ASpace.get_tuple_name(isl::dim::set);
699a0db63a1SMichael Kruse
700a0db63a1SMichael Kruse std::string BName;
70102e8a5adSMichael Kruse if (!BSpace.is_params() && BSpace.has_tuple_name(isl::dim::set))
702a0db63a1SMichael Kruse BName = BSpace.get_tuple_name(isl::dim::set);
703ed787e75SMichael Kruse
704ed787e75SMichael Kruse int NameCompare = AName.compare(BName);
705ed787e75SMichael Kruse if (NameCompare != 0)
706ed787e75SMichael Kruse return NameCompare;
707ed787e75SMichael Kruse
708a0db63a1SMichael Kruse if (ConsiderTupleLen) {
70944596fe6SRiccardo Mori int LenCompare = (int)unsignedFromIslSize(BSpace.dim(isl::dim::set)) -
71044596fe6SRiccardo Mori (int)unsignedFromIslSize(ASpace.dim(isl::dim::set));
711a0db63a1SMichael Kruse if (LenCompare != 0)
712a0db63a1SMichael Kruse return LenCompare;
713ed787e75SMichael Kruse }
714ed787e75SMichael Kruse
715a0db63a1SMichael Kruse return 0;
716a0db63a1SMichael Kruse }
717a0db63a1SMichael Kruse
718a0db63a1SMichael Kruse /// Compare the sets @p A and @p B according to their nested space structure. If
719a0db63a1SMichael Kruse /// the structure is the same, sort using the dimension lower bounds.
720a0db63a1SMichael Kruse /// Returns an std::sort compatible bool.
orderComparer(const isl::basic_set & A,const isl::basic_set & B)721ed787e75SMichael Kruse static bool orderComparer(const isl::basic_set &A, const isl::basic_set &B) {
722a0db63a1SMichael Kruse isl::space ASpace = A.get_space();
723a0db63a1SMichael Kruse isl::space BSpace = B.get_space();
724a0db63a1SMichael Kruse
725a0db63a1SMichael Kruse // Ignoring number of dimensions first ensures that structures with same tuple
726a0db63a1SMichael Kruse // names, but different number of dimensions are still sorted close together.
727a0db63a1SMichael Kruse int TupleNestingCompare = structureCompare(ASpace, BSpace, false);
728a0db63a1SMichael Kruse if (TupleNestingCompare != 0)
729a0db63a1SMichael Kruse return TupleNestingCompare < 0;
730a0db63a1SMichael Kruse
731a0db63a1SMichael Kruse int TupleCompare = structureCompare(ASpace, BSpace, true);
732a0db63a1SMichael Kruse if (TupleCompare != 0)
733a0db63a1SMichael Kruse return TupleCompare < 0;
734a0db63a1SMichael Kruse
735a0db63a1SMichael Kruse return flatCompare(A, B) < 0;
736ed787e75SMichael Kruse }
737ed787e75SMichael Kruse
738ed787e75SMichael Kruse /// Print a string representation of @p USet to @p OS.
739ed787e75SMichael Kruse ///
740ed787e75SMichael Kruse /// The pieces of @p USet are printed in a sorted order. Spaces with equal or
741ed787e75SMichael Kruse /// similar nesting structure are printed together. Compared to isl's own
742ed787e75SMichael Kruse /// printing function the uses the structure itself as base of the sorting, not
743ed787e75SMichael Kruse /// a hash of it. It ensures that e.g. maps spaces with same domain structure
744ed787e75SMichael Kruse /// are printed together. Set pieces with same structure are printed in order of
745ed787e75SMichael Kruse /// their lower bounds.
746ed787e75SMichael Kruse ///
747ed787e75SMichael Kruse /// @param USet Polyhedra to print.
748ed787e75SMichael Kruse /// @param OS Target stream.
749ed787e75SMichael Kruse /// @param Simplify Whether to simplify the polyhedron before printing.
750ed787e75SMichael Kruse /// @param IsMap Whether @p USet is a wrapped map. If true, sets are
751ed787e75SMichael Kruse /// unwrapped before printing to again appear as a map.
printSortedPolyhedra(isl::union_set USet,llvm::raw_ostream & OS,bool Simplify,bool IsMap)752ed787e75SMichael Kruse static void printSortedPolyhedra(isl::union_set USet, llvm::raw_ostream &OS,
753ed787e75SMichael Kruse bool Simplify, bool IsMap) {
7547c7978a1Spatacca if (USet.is_null()) {
755ed787e75SMichael Kruse OS << "<null>\n";
756ed787e75SMichael Kruse return;
757ed787e75SMichael Kruse }
758ed787e75SMichael Kruse
759ed787e75SMichael Kruse if (Simplify)
760ed787e75SMichael Kruse simplify(USet);
761ed787e75SMichael Kruse
762ed787e75SMichael Kruse // Get all the polyhedra.
763ed787e75SMichael Kruse std::vector<isl::basic_set> BSets;
7641696e48eSTobias Grosser
7651696e48eSTobias Grosser for (isl::set Set : USet.get_set_list()) {
7661696e48eSTobias Grosser for (isl::basic_set BSet : Set.get_basic_set_list()) {
767ed787e75SMichael Kruse BSets.push_back(BSet);
7681696e48eSTobias Grosser }
7691696e48eSTobias Grosser }
770ed787e75SMichael Kruse
771ed787e75SMichael Kruse if (BSets.empty()) {
772ed787e75SMichael Kruse OS << "{\n}\n";
773ed787e75SMichael Kruse return;
774ed787e75SMichael Kruse }
775ed787e75SMichael Kruse
776ed787e75SMichael Kruse // Sort the polyhedra.
7778108b7a6SMandeep Singh Grang llvm::sort(BSets, orderComparer);
778ed787e75SMichael Kruse
779ed787e75SMichael Kruse // Print the polyhedra.
780ed787e75SMichael Kruse bool First = true;
781ed787e75SMichael Kruse for (const isl::basic_set &BSet : BSets) {
782ed787e75SMichael Kruse std::string Str;
783ed787e75SMichael Kruse if (IsMap)
784cfe117deSpatacca Str = stringFromIslObj(isl::map(BSet.unwrap()));
785ed787e75SMichael Kruse else
786cfe117deSpatacca Str = stringFromIslObj(isl::set(BSet));
787ed787e75SMichael Kruse size_t OpenPos = Str.find_first_of('{');
788ed787e75SMichael Kruse assert(OpenPos != std::string::npos);
789ed787e75SMichael Kruse size_t ClosePos = Str.find_last_of('}');
790ed787e75SMichael Kruse assert(ClosePos != std::string::npos);
791ed787e75SMichael Kruse
792ed787e75SMichael Kruse if (First)
793ed787e75SMichael Kruse OS << llvm::StringRef(Str).substr(0, OpenPos + 1) << "\n ";
794ed787e75SMichael Kruse else
795ed787e75SMichael Kruse OS << ";\n ";
796ed787e75SMichael Kruse
797ed787e75SMichael Kruse OS << llvm::StringRef(Str).substr(OpenPos + 1, ClosePos - OpenPos - 2);
798ed787e75SMichael Kruse First = false;
799ed787e75SMichael Kruse }
800ed787e75SMichael Kruse assert(!First);
801ed787e75SMichael Kruse OS << "\n}\n";
802ed787e75SMichael Kruse }
803ed787e75SMichael Kruse
recursiveExpand(isl::basic_set BSet,unsigned Dim,isl::set & Expanded)80444596fe6SRiccardo Mori static void recursiveExpand(isl::basic_set BSet, unsigned Dim,
80544596fe6SRiccardo Mori isl::set &Expanded) {
80644596fe6SRiccardo Mori unsigned Dims = unsignedFromIslSize(BSet.dim(isl::dim::set));
807ed787e75SMichael Kruse if (Dim >= Dims) {
808ed787e75SMichael Kruse Expanded = Expanded.unite(BSet);
809ed787e75SMichael Kruse return;
810ed787e75SMichael Kruse }
811ed787e75SMichael Kruse
812ed787e75SMichael Kruse isl::basic_set DimOnly =
81344596fe6SRiccardo Mori BSet.project_out(isl::dim::param, 0,
81444596fe6SRiccardo Mori unsignedFromIslSize(BSet.dim(isl::dim::param)))
815ed787e75SMichael Kruse .project_out(isl::dim::set, Dim + 1, Dims - Dim - 1)
816ed787e75SMichael Kruse .project_out(isl::dim::set, 0, Dim);
817ed787e75SMichael Kruse if (!DimOnly.is_bounded()) {
818ed787e75SMichael Kruse recursiveExpand(BSet, Dim + 1, Expanded);
819ed787e75SMichael Kruse return;
820ed787e75SMichael Kruse }
821ed787e75SMichael Kruse
822ed787e75SMichael Kruse foreachPoint(DimOnly, [&, Dim](isl::point P) {
823ed787e75SMichael Kruse isl::val Val = P.get_coordinate_val(isl::dim::set, 0);
824ed787e75SMichael Kruse isl::basic_set FixBSet = BSet.fix_val(isl::dim::set, Dim, Val);
825ed787e75SMichael Kruse recursiveExpand(FixBSet, Dim + 1, Expanded);
826ed787e75SMichael Kruse });
827ed787e75SMichael Kruse }
828ed787e75SMichael Kruse
829ed787e75SMichael Kruse /// Make each point of a set explicit.
830ed787e75SMichael Kruse ///
831ed787e75SMichael Kruse /// "Expanding" makes each point a set contains explicit. That is, the result is
832ed787e75SMichael Kruse /// a set of singleton polyhedra. Unbounded dimensions are not expanded.
833ed787e75SMichael Kruse ///
834ed787e75SMichael Kruse /// Example:
835ed787e75SMichael Kruse /// { [i] : 0 <= i < 2 }
836ed787e75SMichael Kruse /// is expanded to:
837ed787e75SMichael Kruse /// { [0]; [1] }
expand(const isl::set & Set)838ed787e75SMichael Kruse static isl::set expand(const isl::set &Set) {
839ed787e75SMichael Kruse isl::set Expanded = isl::set::empty(Set.get_space());
8401696e48eSTobias Grosser for (isl::basic_set BSet : Set.get_basic_set_list())
841ed787e75SMichael Kruse recursiveExpand(BSet, 0, Expanded);
842ed787e75SMichael Kruse return Expanded;
843ed787e75SMichael Kruse }
844ed787e75SMichael Kruse
845ed787e75SMichael Kruse /// Expand all points of a union set explicit.
846ed787e75SMichael Kruse ///
847ed787e75SMichael Kruse /// @see expand(const isl::set)
expand(const isl::union_set & USet)848ed787e75SMichael Kruse static isl::union_set expand(const isl::union_set &USet) {
849bad3ebbaSRiccardo Mori isl::union_set Expanded = isl::union_set::empty(USet.ctx());
8501696e48eSTobias Grosser for (isl::set Set : USet.get_set_list()) {
851ed787e75SMichael Kruse isl::set SetExpanded = expand(Set);
852b55aedd0Spatacca Expanded = Expanded.unite(SetExpanded);
8531696e48eSTobias Grosser }
854ed787e75SMichael Kruse return Expanded;
855ed787e75SMichael Kruse }
856ed787e75SMichael Kruse
dumpPw(const isl::set & Set)857ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(const isl::set &Set) {
858ed787e75SMichael Kruse printSortedPolyhedra(Set, llvm::errs(), true, false);
859ed787e75SMichael Kruse }
860ed787e75SMichael Kruse
dumpPw(const isl::map & Map)861ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(const isl::map &Map) {
862ed787e75SMichael Kruse printSortedPolyhedra(Map.wrap(), llvm::errs(), true, true);
863ed787e75SMichael Kruse }
864ed787e75SMichael Kruse
dumpPw(const isl::union_set & USet)865ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(const isl::union_set &USet) {
866ed787e75SMichael Kruse printSortedPolyhedra(USet, llvm::errs(), true, false);
867ed787e75SMichael Kruse }
868ed787e75SMichael Kruse
dumpPw(const isl::union_map & UMap)869ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(const isl::union_map &UMap) {
870ed787e75SMichael Kruse printSortedPolyhedra(UMap.wrap(), llvm::errs(), true, true);
871ed787e75SMichael Kruse }
872ed787e75SMichael Kruse
dumpPw(__isl_keep isl_set * Set)873ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_set *Set) {
874718d04c6STobias Grosser dumpPw(isl::manage_copy(Set));
875ed787e75SMichael Kruse }
876ed787e75SMichael Kruse
dumpPw(__isl_keep isl_map * Map)877ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_map *Map) {
878718d04c6STobias Grosser dumpPw(isl::manage_copy(Map));
879ed787e75SMichael Kruse }
880ed787e75SMichael Kruse
dumpPw(__isl_keep isl_union_set * USet)881ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_union_set *USet) {
882718d04c6STobias Grosser dumpPw(isl::manage_copy(USet));
883ed787e75SMichael Kruse }
884ed787e75SMichael Kruse
dumpPw(__isl_keep isl_union_map * UMap)885ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpPw(__isl_keep isl_union_map *UMap) {
886718d04c6STobias Grosser dumpPw(isl::manage_copy(UMap));
887ed787e75SMichael Kruse }
888ed787e75SMichael Kruse
dumpExpanded(const isl::set & Set)889ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::set &Set) {
890ed787e75SMichael Kruse printSortedPolyhedra(expand(Set), llvm::errs(), false, false);
891ed787e75SMichael Kruse }
892ed787e75SMichael Kruse
dumpExpanded(const isl::map & Map)893ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::map &Map) {
894ed787e75SMichael Kruse printSortedPolyhedra(expand(Map.wrap()), llvm::errs(), false, true);
895ed787e75SMichael Kruse }
896ed787e75SMichael Kruse
dumpExpanded(const isl::union_set & USet)897ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::union_set &USet) {
898ed787e75SMichael Kruse printSortedPolyhedra(expand(USet), llvm::errs(), false, false);
899ed787e75SMichael Kruse }
900ed787e75SMichael Kruse
dumpExpanded(const isl::union_map & UMap)901ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(const isl::union_map &UMap) {
902ed787e75SMichael Kruse printSortedPolyhedra(expand(UMap.wrap()), llvm::errs(), false, true);
903ed787e75SMichael Kruse }
904ed787e75SMichael Kruse
dumpExpanded(__isl_keep isl_set * Set)905ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_set *Set) {
906718d04c6STobias Grosser dumpExpanded(isl::manage_copy(Set));
907ed787e75SMichael Kruse }
908ed787e75SMichael Kruse
dumpExpanded(__isl_keep isl_map * Map)909ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_map *Map) {
910718d04c6STobias Grosser dumpExpanded(isl::manage_copy(Map));
911ed787e75SMichael Kruse }
912ed787e75SMichael Kruse
dumpExpanded(__isl_keep isl_union_set * USet)913ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_union_set *USet) {
914718d04c6STobias Grosser dumpExpanded(isl::manage_copy(USet));
915ed787e75SMichael Kruse }
916ed787e75SMichael Kruse
dumpExpanded(__isl_keep isl_union_map * UMap)917ed787e75SMichael Kruse LLVM_DUMP_METHOD void polly::dumpExpanded(__isl_keep isl_union_map *UMap) {
918718d04c6STobias Grosser dumpExpanded(isl::manage_copy(UMap));
919ed787e75SMichael Kruse }
920ed787e75SMichael Kruse #endif
921