blob: b3d4cbd1ce8d9a00d90394e77b455e8d6fe37c34 [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 Grosserf5338802011-10-06 00:03:35 +000029#include "isl/space.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000030#include "isl/map.h"
31#include "isl/constraint.h"
32#include "isl/schedule.h"
Tobias Grosserde68cc92011-06-30 20:01:02 +000033#include "isl/band.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000034
35#define DEBUG_TYPE "polly-optimize-isl"
36#include "llvm/Support/Debug.h"
Tobias Grosserc6699b72011-06-30 20:29:13 +000037#include "llvm/Support/CommandLine.h"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000038
39using namespace llvm;
40using namespace polly;
41
Tobias Grosser967239c2011-10-23 20:59:44 +000042namespace polly {
43 bool DisablePollyTiling;
44}
45static cl::opt<bool, true>
Tobias Grosser353a2682011-10-23 20:59:26 +000046DisableTiling("polly-no-tiling",
Tobias Grosser967239c2011-10-23 20:59:44 +000047 cl::desc("Disable tiling in the scheduler"), cl::Hidden,
48 cl::location(polly::DisablePollyTiling), cl::init(false));
Tobias Grosser353a2682011-10-23 20:59:26 +000049
Tobias Grosser30aa24c2011-05-14 19:02:06 +000050namespace {
51
Tobias Grosser73600b82011-10-08 00:30:40 +000052 class IslScheduleOptimizer : public ScopPass {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000053
54 public:
55 static char ID;
Tobias Grosser73600b82011-10-08 00:30:40 +000056 explicit IslScheduleOptimizer() : ScopPass(ID) {}
Tobias Grosser30aa24c2011-05-14 19:02:06 +000057
58 virtual bool runOnScop(Scop &S);
59 void printScop(llvm::raw_ostream &OS) const;
60 void getAnalysisUsage(AnalysisUsage &AU) const;
61 };
62
63}
64
Tobias Grosser73600b82011-10-08 00:30:40 +000065char IslScheduleOptimizer::ID = 0;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000066
67static int getSingleMap(__isl_take isl_map *map, void *user) {
68 isl_map **singleMap = (isl_map **) user;
69 *singleMap = map;
70
71 return 0;
72}
73
Tobias Grossercf3942d2011-10-06 00:04:05 +000074static void extendScattering(Scop &S, unsigned NewDimensions) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +000075 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
Tobias Grossercf3942d2011-10-06 00:04:05 +000076 ScopStmt *Stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +000077
Tobias Grossercf3942d2011-10-06 00:04:05 +000078 if (Stmt->isFinalRead())
Tobias Grosser30aa24c2011-05-14 19:02:06 +000079 continue;
80
Tobias Grossercf3942d2011-10-06 00:04:05 +000081 unsigned OldDimensions = Stmt->getNumScattering();
82 isl_space *Space;
83 isl_basic_map *ChangeScattering;
84
85 Space = isl_space_alloc(Stmt->getIslCtx(), 0, OldDimensions, NewDimensions);
86 ChangeScattering = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserf5338802011-10-06 00:03:35 +000087 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000088
Tobias Grossercf3942d2011-10-06 00:04:05 +000089 for (unsigned i = 0; i < OldDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +000090 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000091 isl_constraint_set_coefficient_si(c, isl_dim_in, i, 1);
92 isl_constraint_set_coefficient_si(c, isl_dim_out, i, -1);
Tobias Grossercf3942d2011-10-06 00:04:05 +000093 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +000094 }
95
Tobias Grossercf3942d2011-10-06 00:04:05 +000096 for (unsigned i = OldDimensions; i < NewDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +000097 isl_constraint *c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosser30aa24c2011-05-14 19:02:06 +000098 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
Tobias Grossercf3942d2011-10-06 00:04:05 +000099 ChangeScattering = isl_basic_map_add_constraint(ChangeScattering, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000100 }
101
Tobias Grossercf3942d2011-10-06 00:04:05 +0000102 isl_map *ChangeScatteringMap = isl_map_from_basic_map(ChangeScattering);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000103
Tobias Grossercf3942d2011-10-06 00:04:05 +0000104 ChangeScatteringMap = isl_map_align_params(ChangeScatteringMap,
105 S.getParamSpace());
106 isl_map *NewScattering = isl_map_apply_range(Stmt->getScattering(),
107 ChangeScatteringMap);
108 Stmt->setScattering(NewScattering);
Tobias Grosserf5338802011-10-06 00:03:35 +0000109 isl_local_space_free(LocalSpace);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000110 }
111}
112
Tobias Grosserde68cc92011-06-30 20:01:02 +0000113// getTileMap - Create a map that describes a n-dimensonal tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000114//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000115// getTileMap creates a map from a n-dimensional scattering space into an
116// 2*n-dimensional scattering space. The map describes a rectangular tiling.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000117//
Tobias Grosserde68cc92011-06-30 20:01:02 +0000118// Example:
119// scheduleDimensions = 2, parameterDimensions = 1, tileSize = 32
120//
121// tileMap := [p0] -> {[s0, s1] -> [t0, t1, s0, s1]:
122// t0 % 32 = 0 and t0 <= s0 < t0 + 32 and
123// t1 % 32 = 0 and t1 <= s1 < t1 + 32}
124//
125// Before tiling:
126//
127// for (i = 0; i < N; i++)
128// for (j = 0; j < M; j++)
129// S(i,j)
130//
131// After tiling:
132//
133// for (t_i = 0; t_i < N; i+=32)
134// for (t_j = 0; t_j < M; j+=32)
135// for (i = t_i; i < min(t_i + 32, N); i++) | Unknown that N % 32 = 0
136// for (j = t_j; j < t_j + 32; j++) | Known that M % 32 = 0
137// S(i,j)
138//
139static isl_basic_map *getTileMap(isl_ctx *ctx, int scheduleDimensions,
Tobias Grosserf5338802011-10-06 00:03:35 +0000140 isl_space *SpaceModel, int tileSize = 32) {
Tobias Grosserde68cc92011-06-30 20:01:02 +0000141 // We construct
142 //
143 // tileMap := [p0] -> {[s0, s1] -> [t0, t1, p0, p1, a0, a1]:
144 // s0 = a0 * 32 and s0 = p0 and t0 <= p0 < t0 + 32 and
145 // s1 = a1 * 32 and s1 = p1 and t1 <= p1 < t1 + 32}
146 //
147 // and project out the auxilary dimensions a0 and a1.
Tobias Grosserf5338802011-10-06 00:03:35 +0000148 isl_space *Space = isl_space_alloc(ctx, 0, scheduleDimensions,
149 scheduleDimensions * 3);
150 isl_basic_map *tileMap = isl_basic_map_universe(isl_space_copy(Space));
151
152 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000153
Tobias Grosserde68cc92011-06-30 20:01:02 +0000154 for (int x = 0; x < scheduleDimensions; x++) {
155 int sX = x;
156 int tX = x;
157 int pX = scheduleDimensions + x;
158 int aX = 2 * scheduleDimensions + x;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000159
Tobias Grosserde68cc92011-06-30 20:01:02 +0000160 isl_constraint *c;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000161
Tobias Grosserde68cc92011-06-30 20:01:02 +0000162 // sX = aX * tileSize;
Tobias Grosserf5338802011-10-06 00:03:35 +0000163 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000164 isl_constraint_set_coefficient_si(c, isl_dim_out, sX, 1);
165 isl_constraint_set_coefficient_si(c, isl_dim_out, aX, -tileSize);
166 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000167
Tobias Grosserde68cc92011-06-30 20:01:02 +0000168 // pX = sX;
Tobias Grosserf5338802011-10-06 00:03:35 +0000169 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000170 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
171 isl_constraint_set_coefficient_si(c, isl_dim_in, sX, -1);
172 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000173
Tobias Grosserde68cc92011-06-30 20:01:02 +0000174 // tX <= pX
Tobias Grosserf5338802011-10-06 00:03:35 +0000175 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000176 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, 1);
177 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, -1);
178 tileMap = isl_basic_map_add_constraint(tileMap, c);
179
180 // pX <= tX + (tileSize - 1)
Tobias Grosserf5338802011-10-06 00:03:35 +0000181 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000182 isl_constraint_set_coefficient_si(c, isl_dim_out, tX, 1);
183 isl_constraint_set_coefficient_si(c, isl_dim_out, pX, -1);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000184 isl_constraint_set_constant_si(c, tileSize - 1);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000185 tileMap = isl_basic_map_add_constraint(tileMap, c);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000186 }
187
Tobias Grosserde68cc92011-06-30 20:01:02 +0000188 // Project out auxilary dimensions.
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000189 //
Tobias Grosserde68cc92011-06-30 20:01:02 +0000190 // The auxilary dimensions are transformed into existentially quantified ones.
191 // This reduces the number of visible scattering dimensions and allows Cloog
192 // to produces better code.
193 tileMap = isl_basic_map_project_out(tileMap, isl_dim_out,
194 2 * scheduleDimensions,
195 scheduleDimensions);
Tobias Grosserf5338802011-10-06 00:03:35 +0000196 isl_local_space_free(LocalSpace);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000197 return tileMap;
198}
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000199
Tobias Grosserde68cc92011-06-30 20:01:02 +0000200isl_union_map *getTiledPartialSchedule(isl_band *band) {
201 isl_union_map *partialSchedule;
Tobias Grosserc532f122011-08-25 08:40:59 +0000202 int scheduleDimensions;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000203 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000204 isl_space *Space;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000205 isl_basic_map *tileMap;
206 isl_union_map *tileUnionMap;
207
208 partialSchedule = isl_band_get_partial_schedule(band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000209
Tobias Grosser353a2682011-10-23 20:59:26 +0000210 if (!DisableTiling) {
211 ctx = isl_union_map_get_ctx(partialSchedule);
212 Space= isl_union_map_get_space(partialSchedule);
213 scheduleDimensions = isl_band_n_member(band);
214
215 tileMap = getTileMap(ctx, scheduleDimensions, Space);
216 tileUnionMap = isl_union_map_from_map(isl_map_from_basic_map(tileMap));
217 tileUnionMap = isl_union_map_align_params(tileUnionMap, Space);
218 partialSchedule = isl_union_map_apply_range(partialSchedule, tileUnionMap);
219 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000220
Tobias Grosserde68cc92011-06-30 20:01:02 +0000221 return partialSchedule;
222}
223
Tobias Grosserc6699b72011-06-30 20:29:13 +0000224static isl_map *getPrevectorMap(isl_ctx *ctx, int vectorDimension,
225 int scheduleDimensions,
226 int parameterDimensions,
227 int vectorWidth = 4) {
Tobias Grosser2bd3af12011-08-01 22:39:00 +0000228 assert (0 <= vectorDimension && vectorDimension < scheduleDimensions);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000229
Tobias Grosserf5338802011-10-06 00:03:35 +0000230 isl_space *Space = isl_space_alloc(ctx, parameterDimensions,
231 scheduleDimensions, scheduleDimensions + 2);
232 isl_basic_map *tilingMap = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000233
234 isl_constraint *c;
235
Tobias Grosserf5338802011-10-06 00:03:35 +0000236 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
237
Tobias Grosserc6699b72011-06-30 20:29:13 +0000238 for (int i = 0; i < vectorDimension; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000239 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000240 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
241 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
242 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
243 }
244
245 for (int i = vectorDimension + 1; i < scheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000246 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000247 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
248 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
249 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
250 }
251
252 int stepDimension = scheduleDimensions;
253 int auxilaryDimension = scheduleDimensions + 1;
254
Tobias Grosserf5338802011-10-06 00:03:35 +0000255 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000256 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
257 isl_constraint_set_coefficient_si(c, isl_dim_out, auxilaryDimension,
258 -vectorWidth);
259 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
260
Tobias Grosserf5338802011-10-06 00:03:35 +0000261 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000262 isl_constraint_set_coefficient_si(c, isl_dim_in, vectorDimension, -1);
263 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
264 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
265
Tobias Grosserf5338802011-10-06 00:03:35 +0000266 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000267 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, -1);
268 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
269 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
270
Tobias Grosserf5338802011-10-06 00:03:35 +0000271 c = isl_inequality_alloc(LocalSpace);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000272 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
273 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, -1);
274 isl_constraint_set_constant_si(c, vectorWidth- 1);
275 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
276
277 // Project out auxilary dimensions (introduced to ensure 'ii % tileSize = 0')
278 //
279 // The real dimensions are transformed into existentially quantified ones.
280 // This reduces the number of visible scattering dimensions. Also, Cloog
281 // produces better code, if auxilary dimensions are existentially quantified.
282 tilingMap = isl_basic_map_project_out(tilingMap, isl_dim_out,
283 scheduleDimensions + 1, 1);
284
285 return isl_map_from_basic_map(tilingMap);
286}
287
Tobias Grosserde68cc92011-06-30 20:01:02 +0000288// tileBandList - Tile all bands contained in a band forest.
289//
290// Recursively walk the band forest and tile all bands in the forest. Return
291// a schedule that describes the tiled scattering.
292static isl_union_map *tileBandList(isl_band_list *blist) {
293 int numBands = isl_band_list_n_band(blist);
294
295 isl_union_map *finalSchedule = 0;
296
297 for (int i = 0; i < numBands; i++) {
298 isl_band *band;
299 isl_union_map *partialSchedule;
300 band = isl_band_list_get_band(blist, i);
301 partialSchedule = getTiledPartialSchedule(band);
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000302 int scheduleDimensions = isl_band_n_member(band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000303 isl_space *Space = isl_union_map_get_space(partialSchedule);
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000304
Tobias Grosserde68cc92011-06-30 20:01:02 +0000305
306 if (isl_band_has_children(band)) {
307 isl_band_list *children = isl_band_get_children(band);
308 isl_union_map *suffixSchedule = tileBandList(children);
309 partialSchedule = isl_union_map_flat_range_product(partialSchedule,
310 suffixSchedule);
Tobias Grossera9542422011-08-23 22:35:23 +0000311 isl_band_list_free(children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000312 } else if (EnablePollyVector) {
Tobias Grosserc6699b72011-06-30 20:29:13 +0000313 isl_map *tileMap;
314 isl_union_map *tileUnionMap;
315 isl_ctx *ctx;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000316
317 ctx = isl_union_map_get_ctx(partialSchedule);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000318 for (int i = scheduleDimensions - 1 ; i >= 0 ; i--) {
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000319 if (isl_band_member_is_zero_distance(band, i)) {
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000320 tileMap = getPrevectorMap(ctx, scheduleDimensions + i,
Tobias Grosserc532f122011-08-25 08:40:59 +0000321 scheduleDimensions * 2, 0);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000322 tileUnionMap = isl_union_map_from_map(tileMap);
Tobias Grosserc532f122011-08-25 08:40:59 +0000323 tileUnionMap = isl_union_map_align_params(tileUnionMap,
Tobias Grosserf5338802011-10-06 00:03:35 +0000324 isl_space_copy(Space));
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000325 partialSchedule = isl_union_map_apply_range(partialSchedule,
326 tileUnionMap);
327 break;
328 }
329 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000330 }
331
332 if (finalSchedule)
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000333 finalSchedule = isl_union_map_union(finalSchedule, partialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000334 else
335 finalSchedule = partialSchedule;
336
337 isl_band_free(band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000338 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000339 }
340
341 return finalSchedule;
342}
343
344static isl_union_map *tileSchedule(isl_schedule *schedule) {
345 isl_band_list *blist = isl_schedule_get_band_forest(schedule);
346 isl_union_map *tiledSchedule = tileBandList(blist);
347 isl_band_list_free(blist);
348 return tiledSchedule;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000349}
350
Tobias Grosser73600b82011-10-08 00:30:40 +0000351bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000352 Dependences *D = &getAnalysis<Dependences>();
353
354 // Build input data.
355 int dependencyKinds = Dependences::TYPE_RAW
356 | Dependences::TYPE_WAR
357 | Dependences::TYPE_WAW;
358
359 isl_union_map *validity = D->getDependences(dependencyKinds);
360 isl_union_map *proximity = D->getDependences(dependencyKinds);
361 isl_union_set *domain = NULL;
362
363 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
364 if ((*SI)->isFinalRead())
365 continue;
366 else if (!domain)
367 domain = isl_union_set_from_set((*SI)->getDomain());
368 else
369 domain = isl_union_set_union(domain,
370 isl_union_set_from_set((*SI)->getDomain()));
371
372 if (!domain)
373 return false;
374
375 DEBUG(dbgs() << "\n\nCompute schedule from: ");
376 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
377 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
378 dbgs() << ";\n");
379 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
380 dbgs() << ";\n");
381
382 isl_schedule *schedule;
383
384 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
385
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000386 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000387 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000388 DEBUG(dbgs() << "Individual bands: ");
389
Tobias Grosserde68cc92011-06-30 20:01:02 +0000390 isl_union_map *tiledSchedule = tileSchedule(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000391
Tobias Grosserde68cc92011-06-30 20:01:02 +0000392 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
393 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000394
Tobias Grosserde68cc92011-06-30 20:01:02 +0000395 if (stmt->isFinalRead())
396 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000397
Tobias Grosserde68cc92011-06-30 20:01:02 +0000398 isl_set *domain = stmt->getDomain();
399 isl_union_map *stmtBand;
400 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(tiledSchedule),
401 isl_union_set_from_set(domain));
402 isl_map *stmtSchedule;
403 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000404 stmt->setScattering(stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000405 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000406 }
407
Tobias Grosserde68cc92011-06-30 20:01:02 +0000408 isl_union_map_free(tiledSchedule);
409 isl_schedule_free(schedule);
410
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000411 unsigned maxScatDims = 0;
412
413 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grossercf3942d2011-10-06 00:04:05 +0000414 maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000415
416 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000417 return false;
418}
419
Tobias Grosser73600b82011-10-08 00:30:40 +0000420void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000421}
422
Tobias Grosser73600b82011-10-08 00:30:40 +0000423void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000424 ScopPass::getAnalysisUsage(AU);
425 AU.addRequired<Dependences>();
426}
427
Tobias Grosser73600b82011-10-08 00:30:40 +0000428INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-optimize-isl",
429 "Polly - Optimize schedule of SCoP", false, false)
430INITIALIZE_PASS_DEPENDENCY(Dependences)
431INITIALIZE_PASS_DEPENDENCY(ScopInfo)
432INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-optimize-isl",
433 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000434
Tobias Grosser73600b82011-10-08 00:30:40 +0000435Pass* polly::createIslScheduleOptimizerPass() {
436 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000437}