blob: d2beb03a695fbdd29948b3c2b47c0ed560a2ea1e [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
Tobias Grosser4dca4392011-11-22 19:40:19 +000035#define DEBUG_TYPE "polly-opt-isl"
Tobias Grosser30aa24c2011-05-14 19:02:06 +000036#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 Grosser1ae9a602011-11-17 12:56:03 +0000200// getScheduleForBand - Get the schedule for this band.
201//
202// In case tiling is enabled, the schedule of the band is tiled.
203isl_union_map *getScheduleForBand(isl_band *Band) {
204 isl_union_map *PartialSchedule;
205 int Dimensions;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000206 isl_ctx *ctx;
Tobias Grosserf5338802011-10-06 00:03:35 +0000207 isl_space *Space;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000208 isl_basic_map *TileMap;
209 isl_union_map *TileUMap;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000210
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000211 PartialSchedule = isl_band_get_partial_schedule(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000212
Tobias Grosser79b30202011-11-17 12:56:00 +0000213 if (DisableTiling)
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000214 return PartialSchedule;
Tobias Grosser353a2682011-10-23 20:59:26 +0000215
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000216 ctx = isl_union_map_get_ctx(PartialSchedule);
217 Space = isl_union_map_get_space(PartialSchedule);
218 Dimensions = isl_band_n_member(Band);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000219
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000220 TileMap = getTileMap(ctx, Dimensions, Space);
221 TileUMap = isl_union_map_from_map(isl_map_from_basic_map(TileMap));
222 TileUMap = isl_union_map_align_params(TileUMap, Space);
223 return isl_union_map_apply_range(PartialSchedule, TileUMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000224}
225
Tobias Grosserc6699b72011-06-30 20:29:13 +0000226static isl_map *getPrevectorMap(isl_ctx *ctx, int vectorDimension,
227 int scheduleDimensions,
228 int parameterDimensions,
229 int vectorWidth = 4) {
Tobias Grosser2bd3af12011-08-01 22:39:00 +0000230 assert (0 <= vectorDimension && vectorDimension < scheduleDimensions);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000231
Tobias Grosserf5338802011-10-06 00:03:35 +0000232 isl_space *Space = isl_space_alloc(ctx, parameterDimensions,
233 scheduleDimensions, scheduleDimensions + 2);
234 isl_basic_map *tilingMap = isl_basic_map_universe(isl_space_copy(Space));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000235
236 isl_constraint *c;
237
Tobias Grosserf5338802011-10-06 00:03:35 +0000238 isl_local_space *LocalSpace = isl_local_space_from_space(Space);
239
Tobias Grosserc6699b72011-06-30 20:29:13 +0000240 for (int i = 0; i < vectorDimension; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000241 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000242 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
243 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
244 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
245 }
246
247 for (int i = vectorDimension + 1; i < scheduleDimensions; i++) {
Tobias Grosserf5338802011-10-06 00:03:35 +0000248 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000249 isl_constraint_set_coefficient_si(c, isl_dim_in, i, -1);
250 isl_constraint_set_coefficient_si(c, isl_dim_out, i, 1);
251 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
252 }
253
254 int stepDimension = scheduleDimensions;
255 int auxilaryDimension = scheduleDimensions + 1;
256
Tobias Grosserf5338802011-10-06 00:03:35 +0000257 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000258 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
259 isl_constraint_set_coefficient_si(c, isl_dim_out, auxilaryDimension,
260 -vectorWidth);
261 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
262
Tobias Grosserf5338802011-10-06 00:03:35 +0000263 c = isl_equality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000264 isl_constraint_set_coefficient_si(c, isl_dim_in, vectorDimension, -1);
265 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
266 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
267
Tobias Grosserf5338802011-10-06 00:03:35 +0000268 c = isl_inequality_alloc(isl_local_space_copy(LocalSpace));
Tobias Grosserc6699b72011-06-30 20:29:13 +0000269 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, -1);
270 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, 1);
271 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
272
Tobias Grosserf5338802011-10-06 00:03:35 +0000273 c = isl_inequality_alloc(LocalSpace);
Tobias Grosserc6699b72011-06-30 20:29:13 +0000274 isl_constraint_set_coefficient_si(c, isl_dim_out, vectorDimension, 1);
275 isl_constraint_set_coefficient_si(c, isl_dim_out, stepDimension, -1);
276 isl_constraint_set_constant_si(c, vectorWidth- 1);
277 tilingMap = isl_basic_map_add_constraint(tilingMap, c);
278
279 // Project out auxilary dimensions (introduced to ensure 'ii % tileSize = 0')
280 //
281 // The real dimensions are transformed into existentially quantified ones.
282 // This reduces the number of visible scattering dimensions. Also, Cloog
283 // produces better code, if auxilary dimensions are existentially quantified.
284 tilingMap = isl_basic_map_project_out(tilingMap, isl_dim_out,
285 scheduleDimensions + 1, 1);
286
287 return isl_map_from_basic_map(tilingMap);
288}
289
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000290// getScheduleForBandList - Get the scheduling map for a list of bands.
Tobias Grosserde68cc92011-06-30 20:01:02 +0000291//
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000292// We walk recursively the forest of bands to combine the schedules of the
293// individual bands to the overall schedule. In case tiling is requested,
294// the individual bands are tiled.
295static isl_union_map *getScheduleForBandList(isl_band_list *BandList) {
296 int NumBands;
297 isl_union_map *Schedule;
298 isl_ctx *ctx;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000299
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000300 ctx = isl_band_list_get_ctx(BandList);
301 NumBands = isl_band_list_n_band(BandList);
Tobias Grosser62872012011-11-17 12:56:04 +0000302 Schedule = isl_union_map_empty(isl_space_params_alloc(ctx, 0));
Tobias Grosserde68cc92011-06-30 20:01:02 +0000303
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000304 for (int i = 0; i < NumBands; i++) {
305 isl_band *Band;
306 isl_union_map *PartialSchedule;
307 int ScheduleDimensions;
308 isl_space *Space;
Tobias Grosser44f19ac2011-07-05 22:15:53 +0000309
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000310 Band = isl_band_list_get_band(BandList, i);
311 PartialSchedule = getScheduleForBand(Band);
312 ScheduleDimensions = isl_band_n_member(Band);
313 Space = isl_union_map_get_space(PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000314
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000315 if (isl_band_has_children(Band)) {
316 isl_band_list *Children;
317 isl_union_map *SuffixSchedule;
318
319 Children = isl_band_get_children(Band);
320 SuffixSchedule = getScheduleForBandList(Children);
321 PartialSchedule = isl_union_map_flat_range_product(PartialSchedule,
322 SuffixSchedule);
323 isl_band_list_free(Children);
Tobias Grosser67707b72011-10-23 20:59:40 +0000324 } else if (EnablePollyVector) {
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000325 for (int i = ScheduleDimensions - 1 ; i >= 0 ; i--) {
326 if (isl_band_member_is_zero_distance(Band, i)) {
327 isl_map *TileMap;
328 isl_union_map *TileUMap;
Tobias Grosserc6699b72011-06-30 20:29:13 +0000329
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000330 TileMap = getPrevectorMap(ctx, ScheduleDimensions + i,
331 ScheduleDimensions * 2, 0);
332 TileUMap = isl_union_map_from_map(TileMap);
333 TileUMap = isl_union_map_align_params(TileUMap,
334 isl_space_copy(Space));
335 PartialSchedule = isl_union_map_apply_range(PartialSchedule,
336 TileUMap);
Tobias Grosser7c5ba832011-06-30 20:29:20 +0000337 break;
338 }
339 }
Tobias Grosserde68cc92011-06-30 20:01:02 +0000340 }
341
Tobias Grosser62872012011-11-17 12:56:04 +0000342 Schedule = isl_union_map_union(Schedule, PartialSchedule);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000343
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000344 isl_band_free(Band);
Tobias Grosserf5338802011-10-06 00:03:35 +0000345 isl_space_free(Space);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000346 }
347
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000348 return Schedule;
Tobias Grosserde68cc92011-06-30 20:01:02 +0000349}
350
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000351static isl_union_map *getScheduleMap(isl_schedule *Schedule) {
352 isl_band_list *BandList = isl_schedule_get_band_forest(Schedule);
353 isl_union_map *ScheduleMap = getScheduleForBandList(BandList);
354 isl_band_list_free(BandList);
355 return ScheduleMap;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000356}
357
Tobias Grosser73600b82011-10-08 00:30:40 +0000358bool IslScheduleOptimizer::runOnScop(Scop &S) {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000359 Dependences *D = &getAnalysis<Dependences>();
360
361 // Build input data.
362 int dependencyKinds = Dependences::TYPE_RAW
363 | Dependences::TYPE_WAR
364 | Dependences::TYPE_WAW;
365
366 isl_union_map *validity = D->getDependences(dependencyKinds);
367 isl_union_map *proximity = D->getDependences(dependencyKinds);
368 isl_union_set *domain = NULL;
369
370 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
371 if ((*SI)->isFinalRead())
372 continue;
373 else if (!domain)
374 domain = isl_union_set_from_set((*SI)->getDomain());
375 else
376 domain = isl_union_set_union(domain,
377 isl_union_set_from_set((*SI)->getDomain()));
378
379 if (!domain)
380 return false;
381
382 DEBUG(dbgs() << "\n\nCompute schedule from: ");
383 DEBUG(dbgs() << "Domain := "; isl_union_set_dump(domain); dbgs() << ";\n");
384 DEBUG(dbgs() << "Proximity := "; isl_union_map_dump(proximity);
385 dbgs() << ";\n");
386 DEBUG(dbgs() << "Validity := "; isl_union_map_dump(validity);
387 dbgs() << ";\n");
388
389 isl_schedule *schedule;
390
391 schedule = isl_union_set_compute_schedule(domain, validity, proximity);
392
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000393 DEBUG(dbgs() << "Computed schedule: ");
Tobias Grosserde68cc92011-06-30 20:01:02 +0000394 DEBUG(dbgs() << stringFromIslObj(schedule));
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000395 DEBUG(dbgs() << "Individual bands: ");
396
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000397 isl_union_map *ScheduleMap = getScheduleMap(schedule);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000398
Tobias Grosserde68cc92011-06-30 20:01:02 +0000399 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI) {
400 ScopStmt *stmt = *SI;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000401
Tobias Grosserde68cc92011-06-30 20:01:02 +0000402 if (stmt->isFinalRead())
403 continue;
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000404
Tobias Grosserde68cc92011-06-30 20:01:02 +0000405 isl_set *domain = stmt->getDomain();
406 isl_union_map *stmtBand;
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000407 stmtBand = isl_union_map_intersect_domain(isl_union_map_copy(ScheduleMap),
Tobias Grosserde68cc92011-06-30 20:01:02 +0000408 isl_union_set_from_set(domain));
409 isl_map *stmtSchedule;
410 isl_union_map_foreach_map(stmtBand, getSingleMap, &stmtSchedule);
Tobias Grossercf3942d2011-10-06 00:04:05 +0000411 stmt->setScattering(stmtSchedule);
Tobias Grosser6e0fdca2011-08-23 12:31:14 +0000412 isl_union_map_free(stmtBand);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000413 }
414
Tobias Grosser1ae9a602011-11-17 12:56:03 +0000415 isl_union_map_free(ScheduleMap);
Tobias Grosserde68cc92011-06-30 20:01:02 +0000416 isl_schedule_free(schedule);
417
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000418 unsigned maxScatDims = 0;
419
420 for (Scop::iterator SI = S.begin(), SE = S.end(); SI != SE; ++SI)
Tobias Grossercf3942d2011-10-06 00:04:05 +0000421 maxScatDims = std::max((*SI)->getNumScattering(), maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000422
423 extendScattering(S, maxScatDims);
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000424 return false;
425}
426
Tobias Grosser73600b82011-10-08 00:30:40 +0000427void IslScheduleOptimizer::printScop(raw_ostream &OS) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000428}
429
Tobias Grosser73600b82011-10-08 00:30:40 +0000430void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000431 ScopPass::getAnalysisUsage(AU);
432 AU.addRequired<Dependences>();
433}
434
Tobias Grosser4dca4392011-11-22 19:40:19 +0000435INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000436 "Polly - Optimize schedule of SCoP", false, false)
437INITIALIZE_PASS_DEPENDENCY(Dependences)
438INITIALIZE_PASS_DEPENDENCY(ScopInfo)
Tobias Grosser4dca4392011-11-22 19:40:19 +0000439INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl",
Tobias Grosser73600b82011-10-08 00:30:40 +0000440 "Polly - Optimize schedule of SCoP", false, false)
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000441
Tobias Grosser73600b82011-10-08 00:30:40 +0000442Pass* polly::createIslScheduleOptimizerPass() {
443 return new IslScheduleOptimizer();
Tobias Grosser30aa24c2011-05-14 19:02:06 +0000444}