blob: 3e06fe619b47329c2c5e4b10cd553bd1f734fad5 [file] [log] [blame]
Tobias Grosser30aa24c2011-05-14 19:02:06 +00001//===- Schedule.cpp - Calculate an optimized schedule ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass the isl to calculate a schedule that is optimized for parallelism
11// and tileablility. The algorithm used in isl is an optimized version of the
12// algorithm described in following paper:
13//
14// U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan.
15// A Practical Automatic Polyhedral Parallelizer and Locality Optimizer.
16// In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language
17// Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008.
18//===----------------------------------------------------------------------===//
19
Tobias Grosser967239c2011-10-23 20:59:44 +000020#include "polly/ScheduleOptimizer.h"
21
Tobias Grosser30aa24c2011-05-14 19:02:06 +000022#include "polly/Cloog.h"
23#include "polly/LinkAllPasses.h"
Tobias Grosser67707b72011-10-23 20:59:40 +000024#include "polly/CodeGeneration.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000025#include "polly/Support/GICHelper.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000026#include "polly/Dependences.h"
27#include "polly/ScopInfo.h"
28
Tobias Grosser2493e922011-12-07 07:42:57 +000029#include "isl/aff.h"
Tobias Grosserf5338802011-10-06 00:03:35 +000030#include "isl/space.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000031#include "isl/map.h"
32#include "isl/constraint.h"
33#include "isl/schedule.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000034#include "isl/band.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000035
Tobias Grosser4dca4392011-11-22 19:40:19 +000036#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000037#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000038#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000039
40using namespace llvm;
41using namespace polly;
42
Tobias Grosser967239c2011-10-23 20:59:44 +000043namespace polly {
44 bool DisablePollyTiling;
45}
46static cl::opt<bool, true>
Tobias Grosser353a2682011-10-23 20:59:26 +000047DisableTiling("polly-no-tiling",
Tobias Grosser967239c2011-10-23 20:59:44 +000048 cl::desc("Disable tiling in the scheduler"), cl::Hidden,
49 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000050
Tobias Grosser30aa24c2011-05-14 19:02:06 +000051namespace {
52
Tobias Grosser73600b82011-10-08 00:30:40 +000053 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000054
55 public:
56 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000057 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000058
59 virtual bool runOnScop(Scop &S);
60 void printScop(llvm::raw_ostream &OS) const;
61 void getAnalysisUsage(AnalysisUsage &AU) const;
62 };
63
64}
65
Tobias Grosser73600b82011-10-08 00:30:40 +000066char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000067
68static int getSingleMap(__isl_take isl_map *map, void *user) {
69 isl_map **singleMap = (isl_map **) user;
70 *singleMap = map;
71
72 return 0;
73}
74
Tobias Grossercf3942d2011-10-06 00:04:05 +000075static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000076 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +000077 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000078
Tobias Grossercf3942d2011-10-06 00:04:05 +000079 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +000080 continue;
81
Tobias Grossercf3942d2011-10-06 00:04:05 +000082 unsigned OldDimensions = Stmt->getNumScattering();
83 isl_space *Space;
84 isl_basic_map *ChangeScattering;
85
86 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
87 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +000088 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000089
Tobias Grossercf3942d2011-10-06 00:04:05 +000090 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +000091 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000092 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
93 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +000094 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000095 }
96
Tobias Grossercf3942d2011-10-06 00:04:05 +000097 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +000098 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000099 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000100 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000101 }
102
Tobias Grossercf3942d2011-10-06 00:04:05 +0000103 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000104
Tobias Grossercf3942d2011-10-06 00:04:05 +0000105 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
106 S.getParamSpace());
107 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
108 ChangeScatteringMap);
109 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000110 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000111 }
112}
113
Tobias Grosserde68cc92011-06-30 20:01:02 +0000114// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000115//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000116// getTileMap creates a map from a n-dimensional scattering space into an
117// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000118//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000119// Example:
120// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
121//
122// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
123// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
124// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
125//
126// Before tiling:
127//
128// for (i = 0; i < N; i++)
129// for (j = 0; j < M; j++)
130// S(i,j)
131//
132// After tiling:
133//
134// for (t_i = 0; t_i < N; i+=32)
135// for (t_j = 0; t_j < M; j+=32)
136// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
137// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
138// S(i,j)
139//
140static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000141 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000142 // We construct
143 //
144 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
145 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
146 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
147 //
148 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000149 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
150 scheduleDimensions * 3);
151 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
152
153 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000154
Tobias Grosserde68cc92011-06-30 20:01:02 +0000155 for (int x = 0; x < scheduleDimensions; x++) {
156 int sX = x;
157 int tX = x;
158 int pX = scheduleDimensions + x;
159 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000160
Tobias Grosserde68cc92011-06-30 20:01:02 +0000161 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000162
Tobias Grosserde68cc92011-06-30 20:01:02 +0000163 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000164 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000165 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
166 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
167 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000168
Tobias Grosserde68cc92011-06-30 20:01:02 +0000169 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000170 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000171 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
172 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
173 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000174
Tobias Grosserde68cc92011-06-30 20:01:02 +0000175 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000176 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000177 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
178 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
179 tileMap = isl_basic_map_add_constraint(tileMap, c);
180
181 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000182 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000183 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
184 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000185 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000186 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000187 }
188
Tobias Grosserde68cc92011-06-30 20:01:02 +0000189 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000190 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000191 // The auxilary dimensions are transformed into existentially quantified ones.
192 // This reduces the number of visible scattering dimensions and allows Cloog
193 // to produces better code.
194 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
195 2 * scheduleDimensions,
196 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000197 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000198 return tileMap;
199}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000200
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000201// getScheduleForBand - Get the schedule for this band.
202//
203// In case tiling is enabled, the schedule of the band is tiled.
204isl_union_map *getScheduleForBand(isl_band *Band) {
205 isl_union_map *PartialSchedule;
206 int Dimensions;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000207 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000208 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000209 isl_basic_map *TileMap;
210 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000211
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000212 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000213
Tobias Grosser79b30202011-11-17 12:56:00 +0000214 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000215 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000216
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000217 ctx = isl_union_map_get_ctx(PartialSchedule);
218 Space = isl_union_map_get_space(PartialSchedule);
219 Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000220
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000221 TileMap = getTileMap(ctx, Dimensions, Space);
222 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
223 TileUMap = isl_union_map_align_params(TileUMap, Space);
224 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000225}
226
Tobias Grosser2493e922011-12-07 07:42:57 +0000227// Create a map that pre-vectorizes one scheduling dimension.
228//
229// getPrevectorMap creates a map that maps each input dimension to the same
230// output dimension, except for the dimension DimToVectorize. DimToVectorize is
231// strip mined by 'VectorWidth' and the newly created point loop of
232// DimToVectorize is moved to the innermost level.
233//
234// Example (DimToVectorize=0, ScheduleDimensions=2, VectorWidth=4):
235//
236// | Before transformation
237// |
238// | A[i,j] -> [i,j]
239// |
240// | for (i = 0; i < 128; i++)
241// | for (j = 0; j < 128; j++)
242// | A(i,j);
243//
244// Prevector map:
245// [i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
246//
247// | After transformation:
248// |
249// | A[i,j] -> [it,j,ip] : it % 4 = 0 and it <= ip <= it + 3 and i = ip
250// |
251// | for (it = 0; it < 128; it+=4)
252// | for (j = 0; j < 128; j++)
253// | for (ip = max(0,it); ip < min(128, it + 3); ip++)
254// | A(ip,j);
255//
256// The goal of this transformation is to create a trivially vectorizable loop.
257// This means a parallel loop at the innermost level that has a constant number
258// of iterations corresponding to the target vector width.
259//
260// This transformation creates a loop at the innermost level. The loop has a
261// constant number of iterations, if the number of loop iterations at
262// DimToVectorize can be devided by VectorWidth. The default VectorWidth is
263// currently constant and not yet target specific. This function does not reason
264// about parallelism.
265static isl_map *getPrevectorMap(isl_ctx *ctx, int DimToVectorize,
266 int ScheduleDimensions,
267 int VectorWidth = 4) {
268 isl_space *Space;
269 isl_local_space *LocalSpace, *LocalSpaceRange;
270 isl_set *Modulo;
271 isl_map *TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000272 isl_constraint *c;
Tobias Grosser2493e922011-12-07 07:42:57 +0000273 isl_aff *Aff;
274 int PointDimension; /* ip */
275 int TileDimension; /* it */
276 isl_int VectorWidthMP;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000277
Tobias Grosser2493e922011-12-07 07:42:57 +0000278 assert (0 <= DimToVectorize && DimToVectorize < ScheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000279
Tobias Grosser2493e922011-12-07 07:42:57 +0000280 Space = isl_space_alloc(ctx, 0, ScheduleDimensions, ScheduleDimensions + 1);
281 TilingMap = isl_map_universe(isl_space_copy(Space));
282 LocalSpace = isl_local_space_from_space(Space);
283 PointDimension = ScheduleDimensions;
284 TileDimension = DimToVectorize;
285
286 // Create an identity map for everything except DimToVectorize and map
287 // DimToVectorize to the point loop at the innermost dimension.
288 for (int i = 0; i < ScheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000289 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000290 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
Tobias Grosser2493e922011-12-07 07:42:57 +0000291
292 if (i == DimToVectorize)
293 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
294 else
295 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
296
297 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000298 }
299
Tobias Grosser2493e922011-12-07 07:42:57 +0000300 // it % 'VectorWidth' = 0
301 LocalSpaceRange = isl_local_space_range(isl_local_space_copy(LocalSpace));
302 Aff = isl_aff_zero_on_domain(LocalSpaceRange);
303 Aff = isl_aff_set_constant_si(Aff, VectorWidth);
304 Aff = isl_aff_set_coefficient_si(Aff, isl_dim_in, TileDimension, 1);
305 isl_int_init(VectorWidthMP);
306 isl_int_set_si(VectorWidthMP, VectorWidth);
307 Aff = isl_aff_mod(Aff, VectorWidthMP);
308 isl_int_clear(VectorWidthMP);
309 Modulo = isl_pw_aff_zero_set(isl_pw_aff_from_aff(Aff));
310 TilingMap = isl_map_intersect_range(TilingMap, Modulo);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000311
Tobias Grosser2493e922011-12-07 07:42:57 +0000312 // it <= ip
Tobias Grosserf5338802011-10-06 00:03:35 +0000313 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser2493e922011-12-07 07:42:57 +0000314 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, -1);
315 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, 1);
316 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000317
Tobias Grosser2493e922011-12-07 07:42:57 +0000318 // ip <= it + ('VectorWidth' - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000319 c = isl_inequality_alloc(LocalSpace);
Tobias Grosser2493e922011-12-07 07:42:57 +0000320 isl_constraint_set_coefficient_si(c, isl_dim_out, TileDimension, 1);
321 isl_constraint_set_coefficient_si(c, isl_dim_out, PointDimension, -1);
322 isl_constraint_set_constant_si(c, VectorWidth - 1);
323 TilingMap = isl_map_add_constraint(TilingMap, c);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000324
Tobias Grosser2493e922011-12-07 07:42:57 +0000325 isl_map_dump(TilingMap);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000326
Tobias Grosser2493e922011-12-07 07:42:57 +0000327 return TilingMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000328}
329
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000330// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000331//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000332// We walk recursively the forest of bands to combine the schedules of the
333// individual bands to the overall schedule. In case tiling is requested,
334// the individual bands are tiled.
335static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
336 int NumBands;
337 isl_union_map *Schedule;
338 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000339
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000340 ctx = isl_band_list_get_ctx(BandList);
341 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000342 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000343
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000344 for (int i = 0; i < NumBands; i++) {
345 isl_band *Band;
346 isl_union_map *PartialSchedule;
347 int ScheduleDimensions;
348 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000349
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000350 Band = isl_band_list_get_band(BandList, i);
351 PartialSchedule = getScheduleForBand(Band);
352 ScheduleDimensions = isl_band_n_member(Band);
353 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000354
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000355 if (isl_band_has_children(Band)) {
356 isl_band_list *Children;
357 isl_union_map *SuffixSchedule;
358
359 Children = isl_band_get_children(Band);
360 SuffixSchedule = getScheduleForBandList(Children);
361 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
362 SuffixSchedule);
363 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000364 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000365 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
366 if (isl_band_member_is_zero_distance(Band, i)) {
367 isl_map *TileMap;
368 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000369
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000370 TileMap = getPrevectorMap(ctx, ScheduleDimensions + i,
Tobias Grosser2493e922011-12-07 07:42:57 +0000371 ScheduleDimensions * 2);
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000372 TileUMap = isl_union_map_from_map(TileMap);
373 TileUMap = isl_union_map_align_params(TileUMap,
374 isl_space_copy(Space));
375 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
376 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000377 break;
378 }
379 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000380 }
381
Tobias Grosser62872012011-11-17 12:56:04 +0000382 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000383
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000384 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000385 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000386 }
387
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000388 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000389}
390
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000391static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
392 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
393 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
394 isl_band_list_free(BandList);
395 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000396}
397
Tobias Grosser73600b82011-10-08 00:30:40 +0000398bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000399 Dependences *D = &getAnalysis<Dependences>();
400
401 // Build input data.
402 int dependencyKinds = Dependences::TYPE_RAW
403 | Dependences::TYPE_WAR
404 | Dependences::TYPE_WAW;
405
406 isl_union_map *validity = D->getDependences(dependencyKinds);
407 isl_union_map *proximity = D->getDependences(dependencyKinds);
408 isl_union_set *domain = NULL;
409
410 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
411 if ((*SI)->isFinalRead())
412 continue;
413 else if (!domain)
414 domain = isl_union_set_from_set((*SI)->getDomain());
415 else
416 domain = isl_union_set_union(domain,
417 isl_union_set_from_set((*SI)->getDomain()));
418
419 if (!domain)
420 return false;
421
422 DEBUG(dbgs() << "\n\nCompute schedule from: ");
423 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
424 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
425 dbgs() << ";\n");
426 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
427 dbgs() << ";\n");
428
429 isl_schedule *schedule;
430
431 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
432
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000433 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000434 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000435 DEBUG(dbgs() << "Individual bands: ");
436
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000437 isl_union_map *ScheduleMap = getScheduleMap(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000438
Tobias Grosserde68cc92011-06-30 20:01:02 +0000439 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
440 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000441
Tobias Grosserde68cc92011-06-30 20:01:02 +0000442 if (stmt->isFinalRead())
443 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000444
Tobias Grosserde68cc92011-06-30 20:01:02 +0000445 isl_set *domain = stmt->getDomain();
446 isl_union_map *stmtBand;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000447 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosserde68cc92011-06-30 20:01:02 +0000448 isl_union_set_from_set(domain));
449 isl_map *stmtSchedule;
450 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000451 stmt->setScattering(stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000452 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000453 }
454
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000455 isl_union_map_free(ScheduleMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000456 isl_schedule_free(schedule);
457
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000458 unsigned maxScatDims = 0;
459
460 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grossercf3942d2011-10-06 00:04:05 +0000461 maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000462
463 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000464 return false;
465}
466
Tobias Grosser73600b82011-10-08 00:30:40 +0000467void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000468}
469
Tobias Grosser73600b82011-10-08 00:30:40 +0000470void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000471 ScopPass::getAnalysisUsage(AU);
472 AU.addRequired<Dependences>();
473}
474
Tobias Grosser4dca4392011-11-22 19:40:19 +0000475INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000476 "Polly - Optimize schedule of SCoP", false, false)
477INITIALIZE_PASS_DEPENDENCY(Dependences)
478INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000479INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000480 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000481
Tobias Grosser73600b82011-10-08 00:30:40 +0000482Pass* polly::createIslScheduleOptimizerPass() {
483 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000484}