Coverage Report

Created: 2018-02-20 23:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_ast_codegen.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012-2014 Ecole Normale Superieure
3
 * Copyright 2014      INRIA Rocquencourt
4
 *
5
 * Use of this software is governed by the MIT license
6
 *
7
 * Written by Sven Verdoolaege,
8
 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9
 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10
 * B.P. 105 - 78153 Le Chesnay, France
11
 */
12
13
#include <limits.h>
14
#include <isl/val.h>
15
#include <isl/space.h>
16
#include <isl/aff.h>
17
#include <isl/constraint.h>
18
#include <isl/set.h>
19
#include <isl/ilp.h>
20
#include <isl/union_set.h>
21
#include <isl/union_map.h>
22
#include <isl/schedule_node.h>
23
#include <isl_sort.h>
24
#include <isl_tarjan.h>
25
#include <isl_ast_private.h>
26
#include <isl_ast_build_expr.h>
27
#include <isl_ast_build_private.h>
28
#include <isl_ast_graft_private.h>
29
30
/* Data used in generate_domain.
31
 *
32
 * "build" is the input build.
33
 * "list" collects the results.
34
 */
35
struct isl_generate_domain_data {
36
  isl_ast_build *build;
37
38
  isl_ast_graft_list *list;
39
};
40
41
static __isl_give isl_ast_graft_list *generate_next_level(
42
  __isl_take isl_union_map *executed,
43
  __isl_take isl_ast_build *build);
44
static __isl_give isl_ast_graft_list *generate_code(
45
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
46
  int internal);
47
48
/* Generate an AST for a single domain based on
49
 * the (non single valued) inverse schedule "executed".
50
 *
51
 * We extend the schedule with the iteration domain
52
 * and continue generating through a call to generate_code.
53
 *
54
 * In particular, if executed has the form
55
 *
56
 *  S -> D
57
 *
58
 * then we continue generating code on
59
 *
60
 *  [S -> D] -> D
61
 *
62
 * The extended inverse schedule is clearly single valued
63
 * ensuring that the nested generate_code will not reach this function,
64
 * but will instead create calls to all elements of D that need
65
 * to be executed from the current schedule domain.
66
 */
67
static isl_stat generate_non_single_valued(__isl_take isl_map *executed,
68
  struct isl_generate_domain_data *data)
69
22
{
70
22
  isl_map *identity;
71
22
  isl_ast_build *build;
72
22
  isl_ast_graft_list *list;
73
22
74
22
  build = isl_ast_build_copy(data->build);
75
22
76
22
  identity = isl_set_identity(isl_map_range(isl_map_copy(executed)));
77
22
  executed = isl_map_domain_product(executed, identity);
78
22
  build = isl_ast_build_set_single_valued(build, 1);
79
22
80
22
  list = generate_code(isl_union_map_from_map(executed), build, 1);
81
22
82
22
  data->list = isl_ast_graft_list_concat(data->list, list);
83
22
84
22
  return isl_stat_ok;
85
22
}
86
87
/* Call the at_each_domain callback, if requested by the user,
88
 * after recording the current inverse schedule in the build.
89
 */
90
static __isl_give isl_ast_graft *at_each_domain(__isl_take isl_ast_graft *graft,
91
  __isl_keep isl_map *executed, __isl_keep isl_ast_build *build)
92
2.15k
{
93
2.15k
  if (!graft || !build)
94
0
    return isl_ast_graft_free(graft);
95
2.15k
  if (!build->at_each_domain)
96
10
    return graft;
97
2.14k
98
2.14k
  build = isl_ast_build_copy(build);
99
2.14k
  build = isl_ast_build_set_executed(build,
100
2.14k
      isl_union_map_from_map(isl_map_copy(executed)));
101
2.14k
  if (!build)
102
0
    return isl_ast_graft_free(graft);
103
2.14k
104
2.14k
  graft->node = build->at_each_domain(graft->node,
105
2.14k
          build, build->at_each_domain_user);
106
2.14k
  isl_ast_build_free(build);
107
2.14k
108
2.14k
  if (!graft->node)
109
0
    graft = isl_ast_graft_free(graft);
110
2.14k
111
2.14k
  return graft;
112
2.14k
}
113
114
/* Generate a call expression for the single executed
115
 * domain element "map" and put a guard around it based its (simplified)
116
 * domain.  "executed" is the original inverse schedule from which "map"
117
 * has been derived.  In particular, "map" is either identical to "executed"
118
 * or it is the result of gisting "executed" with respect to the build domain.
119
 * "executed" is only used if there is an at_each_domain callback.
120
 *
121
 * At this stage, any pending constraints in the build can no longer
122
 * be simplified with respect to any enforced constraints since
123
 * the call node does not have any enforced constraints.
124
 * Since all pending constraints not covered by any enforced constraints
125
 * will be added as a guard to the graft in create_node_scaled,
126
 * even in the eliminated case, the pending constraints
127
 * can be considered to have been generated by outer constructs.
128
 *
129
 * If the user has set an at_each_domain callback, it is called
130
 * on the constructed call expression node.
131
 */
132
static isl_stat add_domain(__isl_take isl_map *executed,
133
  __isl_take isl_map *map, struct isl_generate_domain_data *data)
134
2.15k
{
135
2.15k
  isl_ast_build *build;
136
2.15k
  isl_ast_graft *graft;
137
2.15k
  isl_ast_graft_list *list;
138
2.15k
  isl_set *guard, *pending;
139
2.15k
140
2.15k
  build = isl_ast_build_copy(data->build);
141
2.15k
  pending = isl_ast_build_get_pending(build);
142
2.15k
  build = isl_ast_build_replace_pending_by_guard(build, pending);
143
2.15k
144
2.15k
  guard = isl_map_domain(isl_map_copy(map));
145
2.15k
  guard = isl_set_compute_divs(guard);
146
2.15k
  guard = isl_set_coalesce(guard);
147
2.15k
  guard = isl_set_gist(guard, isl_ast_build_get_generated(build));
148
2.15k
  guard = isl_ast_build_specialize(build, guard);
149
2.15k
150
2.15k
  graft = isl_ast_graft_alloc_domain(map, build);
151
2.15k
  graft = at_each_domain(graft, executed, build);
152
2.15k
  isl_ast_build_free(build);
153
2.15k
  isl_map_free(executed);
154
2.15k
  graft = isl_ast_graft_add_guard(graft, guard, data->build);
155
2.15k
156
2.15k
  list = isl_ast_graft_list_from_ast_graft(graft);
157
2.15k
  data->list = isl_ast_graft_list_concat(data->list, list);
158
2.15k
159
2.15k
  return isl_stat_ok;
160
2.15k
}
161
162
/* Generate an AST for a single domain based on
163
 * the inverse schedule "executed" and add it to data->list.
164
 *
165
 * If there is more than one domain element associated to the current
166
 * schedule "time", then we need to continue the generation process
167
 * in generate_non_single_valued.
168
 * Note that the inverse schedule being single-valued may depend
169
 * on constraints that are only available in the original context
170
 * domain specified by the user.  We therefore first introduce
171
 * some of the constraints of data->build->domain.  In particular,
172
 * we intersect with a single-disjunct approximation of this set.
173
 * We perform this approximation to avoid further splitting up
174
 * the executed relation, possibly introducing a disjunctive guard
175
 * on the statement.
176
 *
177
 * On the other hand, we only perform the test after having taken the gist
178
 * of the domain as the resulting map is the one from which the call
179
 * expression is constructed.  Using this map to construct the call
180
 * expression usually yields simpler results in cases where the original
181
 * map is not obviously single-valued.
182
 * If the original map is obviously single-valued, then the gist
183
 * operation is skipped.
184
 *
185
 * Because we perform the single-valuedness test on the gisted map,
186
 * we may in rare cases fail to recognize that the inverse schedule
187
 * is single-valued.  This becomes problematic if this happens
188
 * from the recursive call through generate_non_single_valued
189
 * as we would then end up in an infinite recursion.
190
 * We therefore check if we are inside a call to generate_non_single_valued
191
 * and revert to the ungisted map if the gisted map turns out not to be
192
 * single-valued.
193
 *
194
 * Otherwise, call add_domain to generate a call expression (with guard) and
195
 * to call the at_each_domain callback, if any.
196
 */
197
static isl_stat generate_domain(__isl_take isl_map *executed, void *user)
198
2.17k
{
199
2.17k
  struct isl_generate_domain_data *data = user;
200
2.17k
  isl_set *domain;
201
2.17k
  isl_map *map = NULL;
202
2.17k
  int empty, sv;
203
2.17k
204
2.17k
  domain = isl_ast_build_get_domain(data->build);
205
2.17k
  domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
206
2.17k
  executed = isl_map_intersect_domain(executed, domain);
207
2.17k
  empty = isl_map_is_empty(executed);
208
2.17k
  if (empty < 0)
209
0
    goto error;
210
2.17k
  if (empty) {
211
0
    isl_map_free(executed);
212
0
    return isl_stat_ok;
213
0
  }
214
2.17k
215
2.17k
  sv = isl_map_plain_is_single_valued(executed);
216
2.17k
  if (sv < 0)
217
0
    goto error;
218
2.17k
  if (sv)
219
2.12k
    return add_domain(executed, isl_map_copy(executed), data);
220
48
221
48
  executed = isl_map_coalesce(executed);
222
48
  map = isl_map_copy(executed);
223
48
  map = isl_ast_build_compute_gist_map_domain(data->build, map);
224
48
  sv = isl_map_is_single_valued(map);
225
48
  if (sv < 0)
226
0
    goto error;
227
48
  if (!sv) {
228
22
    isl_map_free(map);
229
22
    if (data->build->single_valued)
230
0
      map = isl_map_copy(executed);
231
22
    else
232
22
      return generate_non_single_valued(executed, data);
233
26
  }
234
26
235
26
  return add_domain(executed, map, data);
236
0
error:
237
0
  isl_map_free(map);
238
0
  isl_map_free(executed);
239
0
  return isl_stat_error;
240
26
}
241
242
/* Call build->create_leaf to a create "leaf" node in the AST,
243
 * encapsulate the result in an isl_ast_graft and return the result
244
 * as a 1-element list.
245
 *
246
 * Note that the node returned by the user may be an entire tree.
247
 *
248
 * Since the node itself cannot enforce any constraints, we turn
249
 * all pending constraints into guards and add them to the resulting
250
 * graft to ensure that they will be generated.
251
 *
252
 * Before we pass control to the user, we first clear some information
253
 * from the build that is (presumbably) only meaningful
254
 * for the current code generation.
255
 * This includes the create_leaf callback itself, so we make a copy
256
 * of the build first.
257
 */
258
static __isl_give isl_ast_graft_list *call_create_leaf(
259
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
260
3
{
261
3
  isl_set *guard;
262
3
  isl_ast_node *node;
263
3
  isl_ast_graft *graft;
264
3
  isl_ast_build *user_build;
265
3
266
3
  guard = isl_ast_build_get_pending(build);
267
3
  user_build = isl_ast_build_copy(build);
268
3
  user_build = isl_ast_build_replace_pending_by_guard(user_build,
269
3
              isl_set_copy(guard));
270
3
  user_build = isl_ast_build_set_executed(user_build, executed);
271
3
  user_build = isl_ast_build_clear_local_info(user_build);
272
3
  if (!user_build)
273
0
    node = NULL;
274
3
  else
275
3
    node = build->create_leaf(user_build, build->create_leaf_user);
276
3
  graft = isl_ast_graft_alloc(node, build);
277
3
  graft = isl_ast_graft_add_guard(graft, guard, build);
278
3
  isl_ast_build_free(build);
279
3
  return isl_ast_graft_list_from_ast_graft(graft);
280
3
}
281
282
static __isl_give isl_ast_graft_list *build_ast_from_child(
283
  __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
284
  __isl_take isl_union_map *executed);
285
286
/* Generate an AST after having handled the complete schedule
287
 * of this call to the code generator or the complete band
288
 * if we are generating an AST from a schedule tree.
289
 *
290
 * If we are inside a band node, then move on to the child of the band.
291
 *
292
 * If the user has specified a create_leaf callback, control
293
 * is passed to the user in call_create_leaf.
294
 *
295
 * Otherwise, we generate one or more calls for each individual
296
 * domain in generate_domain.
297
 */
298
static __isl_give isl_ast_graft_list *generate_inner_level(
299
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
300
4.24k
{
301
4.24k
  isl_ctx *ctx;
302
4.24k
  struct isl_generate_domain_data data = { build };
303
4.24k
304
4.24k
  if (!build || !executed)
305
0
    goto error;
306
4.24k
307
4.24k
  if (isl_ast_build_has_schedule_node(build)) {
308
2.07k
    isl_schedule_node *node;
309
2.07k
    node = isl_ast_build_get_schedule_node(build);
310
2.07k
    build = isl_ast_build_reset_schedule_node(build);
311
2.07k
    return build_ast_from_child(build, node, executed);
312
2.07k
  }
313
2.17k
314
2.17k
  if (build->create_leaf)
315
3
    return call_create_leaf(executed, build);
316
2.17k
317
2.17k
  ctx = isl_union_map_get_ctx(executed);
318
2.17k
  data.list = isl_ast_graft_list_alloc(ctx, 0);
319
2.17k
  if (isl_union_map_foreach_map(executed, &generate_domain, &data) < 0)
320
0
    data.list = isl_ast_graft_list_free(data.list);
321
2.17k
322
2.17k
  if (0)
323
0
error:    data.list = NULL;
324
2.17k
  isl_ast_build_free(build);
325
2.17k
  isl_union_map_free(executed);
326
2.17k
  return data.list;
327
2.17k
}
328
329
/* Call the before_each_for callback, if requested by the user.
330
 */
331
static __isl_give isl_ast_node *before_each_for(__isl_take isl_ast_node *node,
332
  __isl_keep isl_ast_build *build)
333
699
{
334
699
  isl_id *id;
335
699
336
699
  if (!node || !build)
337
0
    return isl_ast_node_free(node);
338
699
  if (!build->before_each_for)
339
466
    return node;
340
233
  id = build->before_each_for(build, build->before_each_for_user);
341
233
  node = isl_ast_node_set_annotation(node, id);
342
233
  return node;
343
233
}
344
345
/* Call the after_each_for callback, if requested by the user.
346
 */
347
static __isl_give isl_ast_graft *after_each_for(__isl_take isl_ast_graft *graft,
348
  __isl_keep isl_ast_build *build)
349
699
{
350
699
  if (!graft || !build)
351
0
    return isl_ast_graft_free(graft);
352
699
  if (!build->after_each_for)
353
466
    return graft;
354
233
  graft->node = build->after_each_for(graft->node, build,
355
233
            build->after_each_for_user);
356
233
  if (!graft->node)
357
0
    return isl_ast_graft_free(graft);
358
233
  return graft;
359
233
}
360
361
/* Plug in all the know values of the current and outer dimensions
362
 * in the domain of "executed".  In principle, we only need to plug
363
 * in the known value of the current dimension since the values of
364
 * outer dimensions have been plugged in already.
365
 * However, it turns out to be easier to just plug in all known values.
366
 */
367
static __isl_give isl_union_map *plug_in_values(
368
  __isl_take isl_union_map *executed, __isl_keep isl_ast_build *build)
369
3.15k
{
370
3.15k
  return isl_ast_build_substitute_values_union_map_domain(build,
371
3.15k
                    executed);
372
3.15k
}
373
374
/* Check if the constraint "c" is a lower bound on dimension "pos",
375
 * an upper bound, or independent of dimension "pos".
376
 */
377
static int constraint_type(isl_constraint *c, int pos)
378
1.87k
{
379
1.87k
  if (isl_constraint_is_lower_bound(c, isl_dim_set, pos))
380
817
    return 1;
381
1.06k
  if (isl_constraint_is_upper_bound(c, isl_dim_set, pos))
382
948
    return 2;
383
113
  return 0;
384
113
}
385
386
/* Compare the types of the constraints "a" and "b",
387
 * resulting in constraints that are independent of "depth"
388
 * to be sorted before the lower bounds on "depth", which in
389
 * turn are sorted before the upper bounds on "depth".
390
 */
391
static int cmp_constraint(__isl_keep isl_constraint *a,
392
  __isl_keep isl_constraint *b, void *user)
393
939
{
394
939
  int *depth = user;
395
939
  int t1 = constraint_type(a, *depth);
396
939
  int t2 = constraint_type(b, *depth);
397
939
398
939
  return t1 - t2;
399
939
}
400
401
/* Extract a lower bound on dimension "pos" from constraint "c".
402
 *
403
 * If the constraint is of the form
404
 *
405
 *  a x + f(...) >= 0
406
 *
407
 * then we essentially return
408
 *
409
 *  l = ceil(-f(...)/a)
410
 *
411
 * However, if the current dimension is strided, then we need to make
412
 * sure that the lower bound we construct is of the form
413
 *
414
 *  f + s a
415
 *
416
 * with f the offset and s the stride.
417
 * We therefore compute
418
 *
419
 *  f + s * ceil((l - f)/s)
420
 */
421
static __isl_give isl_aff *lower_bound(__isl_keep isl_constraint *c,
422
  int pos, __isl_keep isl_ast_build *build)
423
708
{
424
708
  isl_aff *aff;
425
708
426
708
  aff = isl_constraint_get_bound(c, isl_dim_set, pos);
427
708
  aff = isl_aff_ceil(aff);
428
708
429
708
  if (isl_ast_build_has_stride(build, pos)) {
430
11
    isl_aff *offset;
431
11
    isl_val *stride;
432
11
433
11
    offset = isl_ast_build_get_offset(build, pos);
434
11
    stride = isl_ast_build_get_stride(build, pos);
435
11
436
11
    aff = isl_aff_sub(aff, isl_aff_copy(offset));
437
11
    aff = isl_aff_scale_down_val(aff, isl_val_copy(stride));
438
11
    aff = isl_aff_ceil(aff);
439
11
    aff = isl_aff_scale_val(aff, stride);
440
11
    aff = isl_aff_add(aff, offset);
441
11
  }
442
708
443
708
  aff = isl_ast_build_compute_gist_aff(build, aff);
444
708
445
708
  return aff;
446
708
}
447
448
/* Return the exact lower bound (or upper bound if "upper" is set)
449
 * of "domain" as a piecewise affine expression.
450
 *
451
 * If we are computing a lower bound (of a strided dimension), then
452
 * we need to make sure it is of the form
453
 *
454
 *  f + s a
455
 *
456
 * where f is the offset and s is the stride.
457
 * We therefore need to include the stride constraint before computing
458
 * the minimum.
459
 */
460
static __isl_give isl_pw_aff *exact_bound(__isl_keep isl_set *domain,
461
  __isl_keep isl_ast_build *build, int upper)
462
0
{
463
0
  isl_set *stride;
464
0
  isl_map *it_map;
465
0
  isl_pw_aff *pa;
466
0
  isl_pw_multi_aff *pma;
467
0
468
0
  domain = isl_set_copy(domain);
469
0
  if (!upper) {
470
0
    stride = isl_ast_build_get_stride_constraint(build);
471
0
    domain = isl_set_intersect(domain, stride);
472
0
  }
473
0
  it_map = isl_ast_build_map_to_iterator(build, domain);
474
0
  if (upper)
475
0
    pma = isl_map_lexmax_pw_multi_aff(it_map);
476
0
  else
477
0
    pma = isl_map_lexmin_pw_multi_aff(it_map);
478
0
  pa = isl_pw_multi_aff_get_pw_aff(pma, 0);
479
0
  isl_pw_multi_aff_free(pma);
480
0
  pa = isl_ast_build_compute_gist_pw_aff(build, pa);
481
0
  pa = isl_pw_aff_coalesce(pa);
482
0
483
0
  return pa;
484
0
}
485
486
/* Callback for sorting the isl_pw_aff_list passed to reduce_list and
487
 * remove_redundant_lower_bounds.
488
 */
489
static int reduce_list_cmp(__isl_keep isl_pw_aff *a, __isl_keep isl_pw_aff *b,
490
  void *user)
491
83
{
492
83
  return isl_pw_aff_plain_cmp(a, b);
493
83
}
494
495
/* Given a list of lower bounds "list", remove those that are redundant
496
 * with respect to the other bounds in "list" and the domain of "build".
497
 *
498
 * We first sort the bounds in the same way as they would be sorted
499
 * by set_for_node_expressions so that we can try and remove the last
500
 * bounds first.
501
 *
502
 * For a lower bound to be effective, there needs to be at least
503
 * one domain element for which it is larger than all other lower bounds.
504
 * For each lower bound we therefore intersect the domain with
505
 * the conditions that it is larger than all other bounds and
506
 * check whether the result is empty.  If so, the bound can be removed.
507
 */
508
static __isl_give isl_pw_aff_list *remove_redundant_lower_bounds(
509
  __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
510
11
{
511
11
  int i, j, n;
512
11
  isl_set *domain;
513
11
514
11
  list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
515
11
  if (!list)
516
0
    return NULL;
517
11
518
11
  n = isl_pw_aff_list_n_pw_aff(list);
519
11
  if (n <= 1)
520
11
    return list;
521
0
522
0
  domain = isl_ast_build_get_domain(build);
523
0
524
0
  for (i = n - 1; i >= 0; --i) {
525
0
    isl_pw_aff *pa_i;
526
0
    isl_set *domain_i;
527
0
    int empty;
528
0
529
0
    domain_i = isl_set_copy(domain);
530
0
    pa_i = isl_pw_aff_list_get_pw_aff(list, i);
531
0
532
0
    for (j = 0; j < n; ++j) {
533
0
      isl_pw_aff *pa_j;
534
0
      isl_set *better;
535
0
536
0
      if (j == i)
537
0
        continue;
538
0
539
0
      pa_j = isl_pw_aff_list_get_pw_aff(list, j);
540
0
      better = isl_pw_aff_gt_set(isl_pw_aff_copy(pa_i), pa_j);
541
0
      domain_i = isl_set_intersect(domain_i, better);
542
0
    }
543
0
544
0
    empty = isl_set_is_empty(domain_i);
545
0
546
0
    isl_set_free(domain_i);
547
0
    isl_pw_aff_free(pa_i);
548
0
549
0
    if (empty < 0)
550
0
      goto error;
551
0
    if (!empty)
552
0
      continue;
553
0
    list = isl_pw_aff_list_drop(list, i, 1);
554
0
    n--;
555
0
  }
556
0
557
0
  isl_set_free(domain);
558
0
559
0
  return list;
560
0
error:
561
0
  isl_set_free(domain);
562
0
  return isl_pw_aff_list_free(list);
563
0
}
564
565
/* Extract a lower bound on dimension "pos" from each constraint
566
 * in "constraints" and return the list of lower bounds.
567
 * If "constraints" has zero elements, then we extract a lower bound
568
 * from "domain" instead.
569
 *
570
 * If the current dimension is strided, then the lower bound
571
 * is adjusted by lower_bound to match the stride information.
572
 * This modification may make one or more lower bounds redundant
573
 * with respect to the other lower bounds.  We therefore check
574
 * for this condition and remove the redundant lower bounds.
575
 */
576
static __isl_give isl_pw_aff_list *lower_bounds(
577
  __isl_keep isl_constraint_list *constraints, int pos,
578
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
579
699
{
580
699
  isl_ctx *ctx;
581
699
  isl_pw_aff_list *list;
582
699
  int i, n;
583
699
584
699
  if (!build)
585
0
    return NULL;
586
699
587
699
  n = isl_constraint_list_n_constraint(constraints);
588
699
  if (n == 0) {
589
0
    isl_pw_aff *pa;
590
0
    pa = exact_bound(domain, build, 0);
591
0
    return isl_pw_aff_list_from_pw_aff(pa);
592
0
  }
593
699
594
699
  ctx = isl_ast_build_get_ctx(build);
595
699
  list = isl_pw_aff_list_alloc(ctx,n);
596
699
597
1.40k
  for (i = 0; i < n; 
++i708
) {
598
708
    isl_aff *aff;
599
708
    isl_constraint *c;
600
708
601
708
    c = isl_constraint_list_get_constraint(constraints, i);
602
708
    aff = lower_bound(c, pos, build);
603
708
    isl_constraint_free(c);
604
708
    list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
605
708
  }
606
699
607
699
  if (isl_ast_build_has_stride(build, pos))
608
11
    list = remove_redundant_lower_bounds(list, build);
609
699
610
699
  return list;
611
699
}
612
613
/* Extract an upper bound on dimension "pos" from each constraint
614
 * in "constraints" and return the list of upper bounds.
615
 * If "constraints" has zero elements, then we extract an upper bound
616
 * from "domain" instead.
617
 */
618
static __isl_give isl_pw_aff_list *upper_bounds(
619
  __isl_keep isl_constraint_list *constraints, int pos,
620
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
621
699
{
622
699
  isl_ctx *ctx;
623
699
  isl_pw_aff_list *list;
624
699
  int i, n;
625
699
626
699
  n = isl_constraint_list_n_constraint(constraints);
627
699
  if (n == 0) {
628
0
    isl_pw_aff *pa;
629
0
    pa = exact_bound(domain, build, 1);
630
0
    return isl_pw_aff_list_from_pw_aff(pa);
631
0
  }
632
699
633
699
  ctx = isl_ast_build_get_ctx(build);
634
699
  list = isl_pw_aff_list_alloc(ctx,n);
635
699
636
1.46k
  for (i = 0; i < n; 
++i769
) {
637
769
    isl_aff *aff;
638
769
    isl_constraint *c;
639
769
640
769
    c = isl_constraint_list_get_constraint(constraints, i);
641
769
    aff = isl_constraint_get_bound(c, isl_dim_set, pos);
642
769
    isl_constraint_free(c);
643
769
    aff = isl_aff_floor(aff);
644
769
    list = isl_pw_aff_list_add(list, isl_pw_aff_from_aff(aff));
645
769
  }
646
699
647
699
  return list;
648
699
}
649
650
/* Return an isl_ast_expr that performs the reduction of type "type"
651
 * on AST expressions corresponding to the elements in "list".
652
 *
653
 * The list is assumed to contain at least one element.
654
 * If the list contains exactly one element, then the returned isl_ast_expr
655
 * simply computes that affine expression.
656
 * If the list contains more than one element, then we sort it
657
 * using a fairly abitrary but hopefully reasonably stable order.
658
 */
659
static __isl_give isl_ast_expr *reduce_list(enum isl_ast_op_type type,
660
  __isl_keep isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
661
1.39k
{
662
1.39k
  int i, n;
663
1.39k
  isl_ctx *ctx;
664
1.39k
  isl_ast_expr *expr;
665
1.39k
666
1.39k
  if (!list)
667
0
    return NULL;
668
1.39k
669
1.39k
  n = isl_pw_aff_list_n_pw_aff(list);
670
1.39k
671
1.39k
  if (n == 1)
672
1.32k
    return isl_ast_build_expr_from_pw_aff_internal(build,
673
1.32k
        isl_pw_aff_list_get_pw_aff(list, 0));
674
73
675
73
  ctx = isl_pw_aff_list_get_ctx(list);
676
73
  expr = isl_ast_expr_alloc_op(ctx, type, n);
677
73
  if (!expr)
678
0
    return NULL;
679
73
680
73
  list = isl_pw_aff_list_copy(list);
681
73
  list = isl_pw_aff_list_sort(list, &reduce_list_cmp, NULL);
682
73
  if (!list)
683
0
    return isl_ast_expr_free(expr);
684
73
685
225
  
for (i = 0; 73
i < n;
++i152
) {
686
152
    isl_ast_expr *expr_i;
687
152
688
152
    expr_i = isl_ast_build_expr_from_pw_aff_internal(build,
689
152
        isl_pw_aff_list_get_pw_aff(list, i));
690
152
    if (!expr_i)
691
0
      goto error;
692
152
    expr->u.op.args[i] = expr_i;
693
152
  }
694
73
695
73
  isl_pw_aff_list_free(list);
696
73
  return expr;
697
0
error:
698
0
  isl_pw_aff_list_free(list);
699
0
  isl_ast_expr_free(expr);
700
0
  return NULL;
701
73
}
702
703
/* Add guards implied by the "generated constraints",
704
 * but not (necessarily) enforced by the generated AST to "guard".
705
 * In particular, if there is any stride constraints,
706
 * then add the guard implied by those constraints.
707
 * If we have generated a degenerate loop, then add the guard
708
 * implied by "bounds" on the outer dimensions, i.e., the guard
709
 * that ensures that the single value actually exists.
710
 * Since there may also be guards implied by a combination
711
 * of these constraints, we first combine them before
712
 * deriving the implied constraints.
713
 */
714
static __isl_give isl_set *add_implied_guards(__isl_take isl_set *guard,
715
  int degenerate, __isl_keep isl_basic_set *bounds,
716
  __isl_keep isl_ast_build *build)
717
699
{
718
699
  int depth, has_stride;
719
699
  isl_space *space;
720
699
  isl_set *dom, *set;
721
699
722
699
  depth = isl_ast_build_get_depth(build);
723
699
  has_stride = isl_ast_build_has_stride(build, depth);
724
699
  if (!has_stride && 
!degenerate688
)
725
688
    return guard;
726
11
727
11
  space = isl_basic_set_get_space(bounds);
728
11
  dom = isl_set_universe(space);
729
11
730
11
  if (degenerate) {
731
0
    bounds = isl_basic_set_copy(bounds);
732
0
    bounds = isl_basic_set_drop_constraints_not_involving_dims(
733
0
          bounds, isl_dim_set, depth, 1);
734
0
    set = isl_set_from_basic_set(bounds);
735
0
    dom = isl_set_intersect(dom, set);
736
0
  }
737
11
738
11
  if (has_stride) {
739
11
    set = isl_ast_build_get_stride_constraint(build);
740
11
    dom = isl_set_intersect(dom, set);
741
11
  }
742
11
743
11
  dom = isl_set_eliminate(dom, isl_dim_set, depth, 1);
744
11
  dom = isl_ast_build_compute_gist(build, dom);
745
11
  guard = isl_set_intersect(guard, dom);
746
11
747
11
  return guard;
748
11
}
749
750
/* Update "graft" based on "sub_build" for the degenerate case.
751
 *
752
 * "build" is the build in which graft->node was created
753
 * "sub_build" contains information about the current level itself,
754
 * including the single value attained.
755
 *
756
 * We set the initialization part of the for loop to the single
757
 * value attained by the current dimension.
758
 * The increment and condition are not strictly needed as the are known
759
 * to be "1" and "iterator <= value" respectively.
760
 */
761
static __isl_give isl_ast_graft *refine_degenerate(
762
  __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build,
763
  __isl_keep isl_ast_build *sub_build)
764
0
{
765
0
  isl_pw_aff *value;
766
0
767
0
  if (!graft || !sub_build)
768
0
    return isl_ast_graft_free(graft);
769
0
770
0
  value = isl_pw_aff_copy(sub_build->value);
771
0
772
0
  graft->node->u.f.init = isl_ast_build_expr_from_pw_aff_internal(build,
773
0
            value);
774
0
  if (!graft->node->u.f.init)
775
0
    return isl_ast_graft_free(graft);
776
0
777
0
  return graft;
778
0
}
779
780
/* Return the intersection of constraints in "list" as a set.
781
 */
782
static __isl_give isl_set *intersect_constraints(
783
  __isl_keep isl_constraint_list *list)
784
0
{
785
0
  int i, n;
786
0
  isl_basic_set *bset;
787
0
788
0
  n = isl_constraint_list_n_constraint(list);
789
0
  if (n < 1)
790
0
    isl_die(isl_constraint_list_get_ctx(list), isl_error_internal,
791
0
      "expecting at least one constraint", return NULL);
792
0
793
0
  bset = isl_basic_set_from_constraint(
794
0
        isl_constraint_list_get_constraint(list, 0));
795
0
  for (i = 1; i < n; ++i) {
796
0
    isl_basic_set *bset_i;
797
0
798
0
    bset_i = isl_basic_set_from_constraint(
799
0
        isl_constraint_list_get_constraint(list, i));
800
0
    bset = isl_basic_set_intersect(bset, bset_i);
801
0
  }
802
0
803
0
  return isl_set_from_basic_set(bset);
804
0
}
805
806
/* Compute the constraints on the outer dimensions enforced by
807
 * graft->node and add those constraints to graft->enforced,
808
 * in case the upper bound is expressed as a set "upper".
809
 *
810
 * In particular, if l(...) is a lower bound in "lower", and
811
 *
812
 *  -a i + f(...) >= 0    or  a i <= f(...)
813
 *
814
 * is an upper bound ocnstraint on the current dimension i,
815
 * then the for loop enforces the constraint
816
 *
817
 *  -a l(...) + f(...) >= 0   or  a l(...) <= f(...)
818
 *
819
 * We therefore simply take each lower bound in turn, plug it into
820
 * the upper bounds and compute the intersection over all lower bounds.
821
 *
822
 * If a lower bound is a rational expression, then
823
 * isl_basic_set_preimage_multi_aff will force this rational
824
 * expression to have only integer values.  However, the loop
825
 * itself does not enforce this integrality constraint.  We therefore
826
 * use the ceil of the lower bounds instead of the lower bounds themselves.
827
 * Other constraints will make sure that the for loop is only executed
828
 * when each of the lower bounds attains an integral value.
829
 * In particular, potentially rational values only occur in
830
 * lower_bound if the offset is a (seemingly) rational expression,
831
 * but then outer conditions will make sure that this rational expression
832
 * only attains integer values.
833
 */
834
static __isl_give isl_ast_graft *set_enforced_from_set(
835
  __isl_take isl_ast_graft *graft,
836
  __isl_keep isl_pw_aff_list *lower, int pos, __isl_keep isl_set *upper)
837
0
{
838
0
  isl_space *space;
839
0
  isl_basic_set *enforced;
840
0
  isl_pw_multi_aff *pma;
841
0
  int i, n;
842
0
843
0
  if (!graft || !lower)
844
0
    return isl_ast_graft_free(graft);
845
0
846
0
  space = isl_set_get_space(upper);
847
0
  enforced = isl_basic_set_universe(isl_space_copy(space));
848
0
849
0
  space = isl_space_map_from_set(space);
850
0
  pma = isl_pw_multi_aff_identity(space);
851
0
852
0
  n = isl_pw_aff_list_n_pw_aff(lower);
853
0
  for (i = 0; i < n; ++i) {
854
0
    isl_pw_aff *pa;
855
0
    isl_set *enforced_i;
856
0
    isl_basic_set *hull;
857
0
    isl_pw_multi_aff *pma_i;
858
0
859
0
    pa = isl_pw_aff_list_get_pw_aff(lower, i);
860
0
    pa = isl_pw_aff_ceil(pa);
861
0
    pma_i = isl_pw_multi_aff_copy(pma);
862
0
    pma_i = isl_pw_multi_aff_set_pw_aff(pma_i, pos, pa);
863
0
    enforced_i = isl_set_copy(upper);
864
0
    enforced_i = isl_set_preimage_pw_multi_aff(enforced_i, pma_i);
865
0
    hull = isl_set_simple_hull(enforced_i);
866
0
    enforced = isl_basic_set_intersect(enforced, hull);
867
0
  }
868
0
869
0
  isl_pw_multi_aff_free(pma);
870
0
871
0
  graft = isl_ast_graft_enforce(graft, enforced);
872
0
873
0
  return graft;
874
0
}
875
876
/* Compute the constraints on the outer dimensions enforced by
877
 * graft->node and add those constraints to graft->enforced,
878
 * in case the upper bound is expressed as
879
 * a list of affine expressions "upper".
880
 *
881
 * The enforced condition is that each lower bound expression is less
882
 * than or equal to each upper bound expression.
883
 */
884
static __isl_give isl_ast_graft *set_enforced_from_list(
885
  __isl_take isl_ast_graft *graft,
886
  __isl_keep isl_pw_aff_list *lower, __isl_keep isl_pw_aff_list *upper)
887
699
{
888
699
  isl_set *cond;
889
699
  isl_basic_set *enforced;
890
699
891
699
  lower = isl_pw_aff_list_copy(lower);
892
699
  upper = isl_pw_aff_list_copy(upper);
893
699
  cond = isl_pw_aff_list_le_set(lower, upper);
894
699
  enforced = isl_set_simple_hull(cond);
895
699
  graft = isl_ast_graft_enforce(graft, enforced);
896
699
897
699
  return graft;
898
699
}
899
900
/* Does "aff" have a negative constant term?
901
 */
902
static isl_stat aff_constant_is_negative(__isl_take isl_set *set,
903
  __isl_take isl_aff *aff, void *user)
904
708
{
905
708
  int *neg = user;
906
708
  isl_val *v;
907
708
908
708
  v = isl_aff_get_constant_val(aff);
909
708
  *neg = isl_val_is_neg(v);
910
708
  isl_val_free(v);
911
708
  isl_set_free(set);
912
708
  isl_aff_free(aff);
913
708
914
708
  return *neg ? 
isl_stat_ok175
:
isl_stat_error533
;
915
708
}
916
917
/* Does "pa" have a negative constant term over its entire domain?
918
 */
919
static isl_stat pw_aff_constant_is_negative(__isl_take isl_pw_aff *pa,
920
  void *user)
921
708
{
922
708
  isl_stat r;
923
708
  int *neg = user;
924
708
925
708
  r = isl_pw_aff_foreach_piece(pa, &aff_constant_is_negative, user);
926
708
  isl_pw_aff_free(pa);
927
708
928
708
  return (*neg && 
r >= 0175
) ?
isl_stat_ok175
:
isl_stat_error533
;
929
708
}
930
931
/* Does each element in "list" have a negative constant term?
932
 *
933
 * The callback terminates the iteration as soon an element has been
934
 * found that does not have a negative constant term.
935
 */
936
static int list_constant_is_negative(__isl_keep isl_pw_aff_list *list)
937
699
{
938
699
  int neg = 1;
939
699
940
699
  if (isl_pw_aff_list_foreach(list,
941
699
        &pw_aff_constant_is_negative, &neg) < 0 && 
neg533
)
942
0
    return -1;
943
699
944
699
  return neg;
945
699
}
946
947
/* Add 1 to each of the elements in "list", where each of these elements
948
 * is defined over the internal schedule space of "build".
949
 */
950
static __isl_give isl_pw_aff_list *list_add_one(
951
  __isl_take isl_pw_aff_list *list, __isl_keep isl_ast_build *build)
952
166
{
953
166
  int i, n;
954
166
  isl_space *space;
955
166
  isl_aff *aff;
956
166
  isl_pw_aff *one;
957
166
958
166
  space = isl_ast_build_get_space(build, 1);
959
166
  aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
960
166
  aff = isl_aff_add_constant_si(aff, 1);
961
166
  one = isl_pw_aff_from_aff(aff);
962
166
963
166
  n = isl_pw_aff_list_n_pw_aff(list);
964
332
  for (i = 0; i < n; 
++i166
) {
965
166
    isl_pw_aff *pa;
966
166
    pa = isl_pw_aff_list_get_pw_aff(list, i);
967
166
    pa = isl_pw_aff_add(pa, isl_pw_aff_copy(one));
968
166
    list = isl_pw_aff_list_set_pw_aff(list, i, pa);
969
166
  }
970
166
971
166
  isl_pw_aff_free(one);
972
166
973
166
  return list;
974
166
}
975
976
/* Set the condition part of the for node graft->node in case
977
 * the upper bound is represented as a list of piecewise affine expressions.
978
 *
979
 * In particular, set the condition to
980
 *
981
 *  iterator <= min(list of upper bounds)
982
 *
983
 * If each of the upper bounds has a negative constant term, then
984
 * set the condition to
985
 *
986
 *  iterator < min(list of (upper bound + 1)s)
987
 *
988
 */
989
static __isl_give isl_ast_graft *set_for_cond_from_list(
990
  __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *list,
991
  __isl_keep isl_ast_build *build)
992
699
{
993
699
  int neg;
994
699
  isl_ast_expr *bound, *iterator, *cond;
995
699
  enum isl_ast_op_type type = isl_ast_op_le;
996
699
997
699
  if (!graft || !list)
998
0
    return isl_ast_graft_free(graft);
999
699
1000
699
  neg = list_constant_is_negative(list);
1001
699
  if (neg < 0)
1002
0
    return isl_ast_graft_free(graft);
1003
699
  list = isl_pw_aff_list_copy(list);
1004
699
  if (neg) {
1005
166
    list = list_add_one(list, build);
1006
166
    type = isl_ast_op_lt;
1007
166
  }
1008
699
1009
699
  bound = reduce_list(isl_ast_op_min, list, build);
1010
699
  iterator = isl_ast_expr_copy(graft->node->u.f.iterator);
1011
699
  cond = isl_ast_expr_alloc_binary(type, iterator, bound);
1012
699
  graft->node->u.f.cond = cond;
1013
699
1014
699
  isl_pw_aff_list_free(list);
1015
699
  if (!graft->node->u.f.cond)
1016
0
    return isl_ast_graft_free(graft);
1017
699
  return graft;
1018
699
}
1019
1020
/* Set the condition part of the for node graft->node in case
1021
 * the upper bound is represented as a set.
1022
 */
1023
static __isl_give isl_ast_graft *set_for_cond_from_set(
1024
  __isl_take isl_ast_graft *graft, __isl_keep isl_set *set,
1025
  __isl_keep isl_ast_build *build)
1026
0
{
1027
0
  isl_ast_expr *cond;
1028
0
1029
0
  if (!graft)
1030
0
    return NULL;
1031
0
1032
0
  cond = isl_ast_build_expr_from_set_internal(build, isl_set_copy(set));
1033
0
  graft->node->u.f.cond = cond;
1034
0
  if (!graft->node->u.f.cond)
1035
0
    return isl_ast_graft_free(graft);
1036
0
  return graft;
1037
0
}
1038
1039
/* Construct an isl_ast_expr for the increment (i.e., stride) of
1040
 * the current dimension.
1041
 */
1042
static __isl_give isl_ast_expr *for_inc(__isl_keep isl_ast_build *build)
1043
699
{
1044
699
  int depth;
1045
699
  isl_val *v;
1046
699
  isl_ctx *ctx;
1047
699
1048
699
  if (!build)
1049
0
    return NULL;
1050
699
  ctx = isl_ast_build_get_ctx(build);
1051
699
  depth = isl_ast_build_get_depth(build);
1052
699
1053
699
  if (!isl_ast_build_has_stride(build, depth))
1054
688
    return isl_ast_expr_alloc_int_si(ctx, 1);
1055
11
1056
11
  v = isl_ast_build_get_stride(build, depth);
1057
11
  return isl_ast_expr_from_val(v);
1058
11
}
1059
1060
/* Should we express the loop condition as
1061
 *
1062
 *  iterator <= min(list of upper bounds)
1063
 *
1064
 * or as a conjunction of constraints?
1065
 *
1066
 * The first is constructed from a list of upper bounds.
1067
 * The second is constructed from a set.
1068
 *
1069
 * If there are no upper bounds in "constraints", then this could mean
1070
 * that "domain" simply doesn't have an upper bound or that we didn't
1071
 * pick any upper bound.  In the first case, we want to generate the
1072
 * loop condition as a(n empty) conjunction of constraints
1073
 * In the second case, we will compute
1074
 * a single upper bound from "domain" and so we use the list form.
1075
 *
1076
 * If there are upper bounds in "constraints",
1077
 * then we use the list form iff the atomic_upper_bound option is set.
1078
 */
1079
static int use_upper_bound_list(isl_ctx *ctx, int n_upper,
1080
  __isl_keep isl_set *domain, int depth)
1081
699
{
1082
699
  if (n_upper > 0)
1083
699
    return isl_options_get_ast_build_atomic_upper_bound(ctx);
1084
0
  else
1085
0
    return isl_set_dim_has_upper_bound(domain, isl_dim_set, depth);
1086
699
}
1087
1088
/* Fill in the expressions of the for node in graft->node.
1089
 *
1090
 * In particular,
1091
 * - set the initialization part of the loop to the maximum of the lower bounds
1092
 * - extract the increment from the stride of the current dimension
1093
 * - construct the for condition either based on a list of upper bounds
1094
 *  or on a set of upper bound constraints.
1095
 */
1096
static __isl_give isl_ast_graft *set_for_node_expressions(
1097
  __isl_take isl_ast_graft *graft, __isl_keep isl_pw_aff_list *lower,
1098
  int use_list, __isl_keep isl_pw_aff_list *upper_list,
1099
  __isl_keep isl_set *upper_set, __isl_keep isl_ast_build *build)
1100
699
{
1101
699
  isl_ast_node *node;
1102
699
1103
699
  if (!graft)
1104
0
    return NULL;
1105
699
1106
699
  build = isl_ast_build_copy(build);
1107
699
1108
699
  node = graft->node;
1109
699
  node->u.f.init = reduce_list(isl_ast_op_max, lower, build);
1110
699
  node->u.f.inc = for_inc(build);
1111
699
1112
699
  if (!node->u.f.init || !node->u.f.inc)
1113
0
    graft = isl_ast_graft_free(graft);
1114
699
1115
699
  if (use_list)
1116
699
    graft = set_for_cond_from_list(graft, upper_list, build);
1117
0
  else
1118
0
    graft = set_for_cond_from_set(graft, upper_set, build);
1119
699
1120
699
  isl_ast_build_free(build);
1121
699
1122
699
  return graft;
1123
699
}
1124
1125
/* Update "graft" based on "bounds" and "domain" for the generic,
1126
 * non-degenerate, case.
1127
 *
1128
 * "c_lower" and "c_upper" contain the lower and upper bounds
1129
 * that the loop node should express.
1130
 * "domain" is the subset of the intersection of the constraints
1131
 * for which some code is executed.
1132
 *
1133
 * There may be zero lower bounds or zero upper bounds in "constraints"
1134
 * in case the list of constraints was created
1135
 * based on the atomic option or based on separation with explicit bounds.
1136
 * In that case, we use "domain" to derive lower and/or upper bounds.
1137
 *
1138
 * We first compute a list of one or more lower bounds.
1139
 *
1140
 * Then we decide if we want to express the condition as
1141
 *
1142
 *  iterator <= min(list of upper bounds)
1143
 *
1144
 * or as a conjunction of constraints.
1145
 *
1146
 * The set of enforced constraints is then computed either based on
1147
 * a list of upper bounds or on a set of upper bound constraints.
1148
 * We do not compute any enforced constraints if we were forced
1149
 * to compute a lower or upper bound using exact_bound.  The domains
1150
 * of the resulting expressions may imply some bounds on outer dimensions
1151
 * that we do not want to appear in the enforced constraints since
1152
 * they are not actually enforced by the corresponding code.
1153
 *
1154
 * Finally, we fill in the expressions of the for node.
1155
 */
1156
static __isl_give isl_ast_graft *refine_generic_bounds(
1157
  __isl_take isl_ast_graft *graft,
1158
  __isl_take isl_constraint_list *c_lower,
1159
  __isl_take isl_constraint_list *c_upper,
1160
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1161
699
{
1162
699
  int depth;
1163
699
  isl_ctx *ctx;
1164
699
  isl_pw_aff_list *lower;
1165
699
  int use_list;
1166
699
  isl_set *upper_set = NULL;
1167
699
  isl_pw_aff_list *upper_list = NULL;
1168
699
  int n_lower, n_upper;
1169
699
1170
699
  if (!graft || !c_lower || !c_upper || !build)
1171
0
    goto error;
1172
699
1173
699
  depth = isl_ast_build_get_depth(build);
1174
699
  ctx = isl_ast_graft_get_ctx(graft);
1175
699
1176
699
  n_lower = isl_constraint_list_n_constraint(c_lower);
1177
699
  n_upper = isl_constraint_list_n_constraint(c_upper);
1178
699
1179
699
  use_list = use_upper_bound_list(ctx, n_upper, domain, depth);
1180
699
1181
699
  lower = lower_bounds(c_lower, depth, domain, build);
1182
699
1183
699
  if (use_list)
1184
699
    upper_list = upper_bounds(c_upper, depth, domain, build);
1185
0
  else if (n_upper > 0)
1186
0
    upper_set = intersect_constraints(c_upper);
1187
0
  else
1188
0
    upper_set = isl_set_universe(isl_set_get_space(domain));
1189
699
1190
699
  if (n_lower == 0 || n_upper == 0)
1191
0
    ;
1192
699
  else if (use_list)
1193
699
    graft = set_enforced_from_list(graft, lower, upper_list);
1194
0
  else
1195
0
    graft = set_enforced_from_set(graft, lower, depth, upper_set);
1196
699
1197
699
  graft = set_for_node_expressions(graft, lower, use_list, upper_list,
1198
699
          upper_set, build);
1199
699
1200
699
  isl_pw_aff_list_free(lower);
1201
699
  isl_pw_aff_list_free(upper_list);
1202
699
  isl_set_free(upper_set);
1203
699
  isl_constraint_list_free(c_lower);
1204
699
  isl_constraint_list_free(c_upper);
1205
699
1206
699
  return graft;
1207
0
error:
1208
0
  isl_constraint_list_free(c_lower);
1209
0
  isl_constraint_list_free(c_upper);
1210
0
  return isl_ast_graft_free(graft);
1211
699
}
1212
1213
/* Internal data structure used inside count_constraints to keep
1214
 * track of the number of constraints that are independent of dimension "pos",
1215
 * the lower bounds in "pos" and the upper bounds in "pos".
1216
 */
1217
struct isl_ast_count_constraints_data {
1218
  int pos;
1219
1220
  int n_indep;
1221
  int n_lower;
1222
  int n_upper;
1223
};
1224
1225
/* Increment data->n_indep, data->lower or data->upper depending
1226
 * on whether "c" is independenct of dimensions data->pos,
1227
 * a lower bound or an upper bound.
1228
 */
1229
static isl_stat count_constraints(__isl_take isl_constraint *c, void *user)
1230
1.53k
{
1231
1.53k
  struct isl_ast_count_constraints_data *data = user;
1232
1.53k
1233
1.53k
  if (isl_constraint_is_lower_bound(c, isl_dim_set, data->pos))
1234
708
    data->n_lower++;
1235
823
  else if (isl_constraint_is_upper_bound(c, isl_dim_set, data->pos))
1236
769
    data->n_upper++;
1237
54
  else
1238
54
    data->n_indep++;
1239
1.53k
1240
1.53k
  isl_constraint_free(c);
1241
1.53k
1242
1.53k
  return isl_stat_ok;
1243
1.53k
}
1244
1245
/* Update "graft" based on "bounds" and "domain" for the generic,
1246
 * non-degenerate, case.
1247
 *
1248
 * "list" respresent the list of bounds that need to be encoded by
1249
 * the for loop.  Only the constraints that involve the iterator
1250
 * are relevant here.  The other constraints are taken care of by
1251
 * the caller and are included in the generated constraints of "build".
1252
 * "domain" is the subset of the intersection of the constraints
1253
 * for which some code is executed.
1254
 * "build" is the build in which graft->node was created.
1255
 *
1256
 * We separate lower bounds, upper bounds and constraints that
1257
 * are independent of the loop iterator.
1258
 *
1259
 * The actual for loop bounds are generated in refine_generic_bounds.
1260
 */
1261
static __isl_give isl_ast_graft *refine_generic_split(
1262
  __isl_take isl_ast_graft *graft, __isl_take isl_constraint_list *list,
1263
  __isl_keep isl_set *domain, __isl_keep isl_ast_build *build)
1264
699
{
1265
699
  struct isl_ast_count_constraints_data data;
1266
699
  isl_constraint_list *lower;
1267
699
  isl_constraint_list *upper;
1268
699
1269
699
  if (!list)
1270
0
    return isl_ast_graft_free(graft);
1271
699
1272
699
  data.pos = isl_ast_build_get_depth(build);
1273
699
1274
699
  list = isl_constraint_list_sort(list, &cmp_constraint, &data.pos);
1275
699
  if (!list)
1276
0
    return isl_ast_graft_free(graft);
1277
699
1278
699
  data.n_indep = data.n_lower = data.n_upper = 0;
1279
699
  if (isl_constraint_list_foreach(list, &count_constraints, &data) < 0) {
1280
0
    isl_constraint_list_free(list);
1281
0
    return isl_ast_graft_free(graft);
1282
0
  }
1283
699
1284
699
  lower = isl_constraint_list_drop(list, 0, data.n_indep);
1285
699
  upper = isl_constraint_list_copy(lower);
1286
699
  lower = isl_constraint_list_drop(lower, data.n_lower, data.n_upper);
1287
699
  upper = isl_constraint_list_drop(upper, 0, data.n_lower);
1288
699
1289
699
  return refine_generic_bounds(graft, lower, upper, domain, build);
1290
699
}
1291
1292
/* Update "graft" based on "bounds" and "domain" for the generic,
1293
 * non-degenerate, case.
1294
 *
1295
 * "bounds" respresent the bounds that need to be encoded by
1296
 * the for loop (or a guard around the for loop).
1297
 * "domain" is the subset of "bounds" for which some code is executed.
1298
 * "build" is the build in which graft->node was created.
1299
 *
1300
 * We break up "bounds" into a list of constraints and continue with
1301
 * refine_generic_split.
1302
 */
1303
static __isl_give isl_ast_graft *refine_generic(
1304
  __isl_take isl_ast_graft *graft,
1305
  __isl_keep isl_basic_set *bounds, __isl_keep isl_set *domain,
1306
  __isl_keep isl_ast_build *build)
1307
699
{
1308
699
  isl_constraint_list *list;
1309
699
1310
699
  if (!build || !graft)
1311
0
    return isl_ast_graft_free(graft);
1312
699
1313
699
  list = isl_basic_set_get_constraint_list(bounds);
1314
699
1315
699
  graft = refine_generic_split(graft, list, domain, build);
1316
699
1317
699
  return graft;
1318
699
}
1319
1320
/* Create a for node for the current level.
1321
 *
1322
 * Mark the for node degenerate if "degenerate" is set.
1323
 */
1324
static __isl_give isl_ast_node *create_for(__isl_keep isl_ast_build *build,
1325
  int degenerate)
1326
699
{
1327
699
  int depth;
1328
699
  isl_id *id;
1329
699
  isl_ast_node *node;
1330
699
1331
699
  if (!build)
1332
0
    return NULL;
1333
699
1334
699
  depth = isl_ast_build_get_depth(build);
1335
699
  id = isl_ast_build_get_iterator_id(build, depth);
1336
699
  node = isl_ast_node_alloc_for(id);
1337
699
  if (degenerate)
1338
0
    node = isl_ast_node_for_mark_degenerate(node);
1339
699
1340
699
  return node;
1341
699
}
1342
1343
/* If the ast_build_exploit_nested_bounds option is set, then return
1344
 * the constraints enforced by all elements in "list".
1345
 * Otherwise, return the universe.
1346
 */
1347
static __isl_give isl_basic_set *extract_shared_enforced(
1348
  __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
1349
3.85k
{
1350
3.85k
  isl_ctx *ctx;
1351
3.85k
  isl_space *space;
1352
3.85k
1353
3.85k
  if (!list)
1354
0
    return NULL;
1355
3.85k
1356
3.85k
  ctx = isl_ast_graft_list_get_ctx(list);
1357
3.85k
  if (isl_options_get_ast_build_exploit_nested_bounds(ctx))
1358
3.85k
    return isl_ast_graft_list_extract_shared_enforced(list, build);
1359
2
1360
2
  space = isl_ast_build_get_space(build, 1);
1361
2
  return isl_basic_set_universe(space);
1362
2
}
1363
1364
/* Return the pending constraints of "build" that are not already taken
1365
 * care of (by a combination of "enforced" and the generated constraints
1366
 * of "build").
1367
 */
1368
static __isl_give isl_set *extract_pending(__isl_keep isl_ast_build *build,
1369
  __isl_keep isl_basic_set *enforced)
1370
3.85k
{
1371
3.85k
  isl_set *guard, *context;
1372
3.85k
1373
3.85k
  guard = isl_ast_build_get_pending(build);
1374
3.85k
  context = isl_set_from_basic_set(isl_basic_set_copy(enforced));
1375
3.85k
  context = isl_set_intersect(context,
1376
3.85k
          isl_ast_build_get_generated(build));
1377
3.85k
  return isl_set_gist(guard, context);
1378
3.85k
}
1379
1380
/* Create an AST node for the current dimension based on
1381
 * the schedule domain "bounds" and return the node encapsulated
1382
 * in an isl_ast_graft.
1383
 *
1384
 * "executed" is the current inverse schedule, taking into account
1385
 * the bounds in "bounds"
1386
 * "domain" is the domain of "executed", with inner dimensions projected out.
1387
 * It may be a strict subset of "bounds" in case "bounds" was created
1388
 * based on the atomic option or based on separation with explicit bounds.
1389
 *
1390
 * "domain" may satisfy additional equalities that result
1391
 * from intersecting "executed" with "bounds" in add_node.
1392
 * It may also satisfy some global constraints that were dropped out because
1393
 * we performed separation with explicit bounds.
1394
 * The very first step is then to copy these constraints to "bounds".
1395
 *
1396
 * Since we may be calling before_each_for and after_each_for
1397
 * callbacks, we record the current inverse schedule in the build.
1398
 *
1399
 * We consider three builds,
1400
 * "build" is the one in which the current level is created,
1401
 * "body_build" is the build in which the next level is created,
1402
 * "sub_build" is essentially the same as "body_build", except that
1403
 * the depth has not been increased yet.
1404
 *
1405
 * "build" already contains information (in strides and offsets)
1406
 * about the strides at the current level, but this information is not
1407
 * reflected in the build->domain.
1408
 * We first add this information and the "bounds" to the sub_build->domain.
1409
 * isl_ast_build_set_loop_bounds adds the stride information and
1410
 * checks whether the current dimension attains
1411
 * only a single value and whether this single value can be represented using
1412
 * a single affine expression.
1413
 * In the first case, the current level is considered "degenerate".
1414
 * In the second, sub-case, the current level is considered "eliminated".
1415
 * Eliminated levels don't need to be reflected in the AST since we can
1416
 * simply plug in the affine expression.  For degenerate, but non-eliminated,
1417
 * levels, we do introduce a for node, but mark is as degenerate so that
1418
 * it can be printed as an assignment of the single value to the loop
1419
 * "iterator".
1420
 *
1421
 * If the current level is eliminated, we explicitly plug in the value
1422
 * for the current level found by isl_ast_build_set_loop_bounds in the
1423
 * inverse schedule.  This ensures that if we are working on a slice
1424
 * of the domain based on information available in the inverse schedule
1425
 * and the build domain, that then this information is also reflected
1426
 * in the inverse schedule.  This operation also eliminates the current
1427
 * dimension from the inverse schedule making sure no inner dimensions depend
1428
 * on the current dimension.  Otherwise, we create a for node, marking
1429
 * it degenerate if appropriate.  The initial for node is still incomplete
1430
 * and will be completed in either refine_degenerate or refine_generic.
1431
 *
1432
 * We then generate a sequence of grafts for the next level,
1433
 * create a surrounding graft for the current level and insert
1434
 * the for node we created (if the current level is not eliminated).
1435
 * Before creating a graft for the current level, we first extract
1436
 * hoistable constraints from the child guards and combine them
1437
 * with the pending constraints in the build.  These constraints
1438
 * are used to simplify the child guards and then added to the guard
1439
 * of the current graft to ensure that they will be generated.
1440
 * If the hoisted guard is a disjunction, then we use it directly
1441
 * to gist the guards on the children before intersect it with the
1442
 * pending constraints.  We do so because this disjunction is typically
1443
 * identical to the guards on the children such that these guards
1444
 * can be effectively removed completely.  After the intersection,
1445
 * the gist operation would have a harder time figuring this out.
1446
 *
1447
 * Finally, we set the bounds of the for loop in either
1448
 * refine_degenerate or refine_generic.
1449
 * We do so in a context where the pending constraints of the build
1450
 * have been replaced by the guard of the current graft.
1451
 */
1452
static __isl_give isl_ast_graft *create_node_scaled(
1453
  __isl_take isl_union_map *executed,
1454
  __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1455
  __isl_take isl_ast_build *build)
1456
3.85k
{
1457
3.85k
  int depth;
1458
3.85k
  int degenerate;
1459
3.85k
  isl_bool eliminated;
1460
3.85k
  isl_basic_set *hull;
1461
3.85k
  isl_basic_set *enforced;
1462
3.85k
  isl_set *guard, *hoisted;
1463
3.85k
  isl_ast_node *node = NULL;
1464
3.85k
  isl_ast_graft *graft;
1465
3.85k
  isl_ast_graft_list *children;
1466
3.85k
  isl_ast_build *sub_build;
1467
3.85k
  isl_ast_build *body_build;
1468
3.85k
1469
3.85k
  domain = isl_ast_build_eliminate_divs(build, domain);
1470
3.85k
  domain = isl_set_detect_equalities(domain);
1471
3.85k
  hull = isl_set_unshifted_simple_hull(isl_set_copy(domain));
1472
3.85k
  bounds = isl_basic_set_intersect(bounds, hull);
1473
3.85k
  build = isl_ast_build_set_executed(build, isl_union_map_copy(executed));
1474
3.85k
1475
3.85k
  depth = isl_ast_build_get_depth(build);
1476
3.85k
  sub_build = isl_ast_build_copy(build);
1477
3.85k
  bounds = isl_basic_set_remove_redundancies(bounds);
1478
3.85k
  bounds = isl_ast_build_specialize_basic_set(sub_build, bounds);
1479
3.85k
  sub_build = isl_ast_build_set_loop_bounds(sub_build,
1480
3.85k
            isl_basic_set_copy(bounds));
1481
3.85k
  degenerate = isl_ast_build_has_value(sub_build);
1482
3.85k
  eliminated = isl_ast_build_has_affine_value(sub_build, depth);
1483
3.85k
  if (degenerate < 0 || eliminated < 0)
1484
0
    executed = isl_union_map_free(executed);
1485
3.85k
  if (!degenerate)
1486
699
    bounds = isl_ast_build_compute_gist_basic_set(build, bounds);
1487
3.85k
  sub_build = isl_ast_build_set_pending_generated(sub_build,
1488
3.85k
            isl_basic_set_copy(bounds));
1489
3.85k
  if (eliminated)
1490
3.15k
    executed = plug_in_values(executed, sub_build);
1491
699
  else
1492
699
    node = create_for(build, degenerate);
1493
3.85k
1494
3.85k
  body_build = isl_ast_build_copy(sub_build);
1495
3.85k
  body_build = isl_ast_build_increase_depth(body_build);
1496
3.85k
  if (!eliminated)
1497
699
    node = before_each_for(node, body_build);
1498
3.85k
  children = generate_next_level(executed,
1499
3.85k
            isl_ast_build_copy(body_build));
1500
3.85k
1501
3.85k
  enforced = extract_shared_enforced(children, build);
1502
3.85k
  guard = extract_pending(sub_build, enforced);
1503
3.85k
  hoisted = isl_ast_graft_list_extract_hoistable_guard(children, build);
1504
3.85k
  if (isl_set_n_basic_set(hoisted) > 1)
1505
7
    children = isl_ast_graft_list_gist_guards(children,
1506
7
                isl_set_copy(hoisted));
1507
3.85k
  guard = isl_set_intersect(guard, hoisted);
1508
3.85k
  if (!eliminated)
1509
699
    guard = add_implied_guards(guard, degenerate, bounds, build);
1510
3.85k
1511
3.85k
  graft = isl_ast_graft_alloc_from_children(children,
1512
3.85k
          isl_set_copy(guard), enforced, build, sub_build);
1513
3.85k
1514
3.85k
  if (!eliminated) {
1515
699
    isl_ast_build *for_build;
1516
699
1517
699
    graft = isl_ast_graft_insert_for(graft, node);
1518
699
    for_build = isl_ast_build_copy(build);
1519
699
    for_build = isl_ast_build_replace_pending_by_guard(for_build,
1520
699
              isl_set_copy(guard));
1521
699
    if (degenerate)
1522
0
      graft = refine_degenerate(graft, for_build, sub_build);
1523
699
    else
1524
699
      graft = refine_generic(graft, bounds,
1525
699
          domain, for_build);
1526
699
    isl_ast_build_free(for_build);
1527
699
  }
1528
3.85k
  isl_set_free(guard);
1529
3.85k
  if (!eliminated)
1530
699
    graft = after_each_for(graft, body_build);
1531
3.85k
1532
3.85k
  isl_ast_build_free(body_build);
1533
3.85k
  isl_ast_build_free(sub_build);
1534
3.85k
  isl_ast_build_free(build);
1535
3.85k
  isl_basic_set_free(bounds);
1536
3.85k
  isl_set_free(domain);
1537
3.85k
1538
3.85k
  return graft;
1539
3.85k
}
1540
1541
/* Internal data structure for checking if all constraints involving
1542
 * the input dimension "depth" are such that the other coefficients
1543
 * are multiples of "m", reducing "m" if they are not.
1544
 * If "m" is reduced all the way down to "1", then the check has failed
1545
 * and we break out of the iteration.
1546
 */
1547
struct isl_check_scaled_data {
1548
  int depth;
1549
  isl_val *m;
1550
};
1551
1552
/* If constraint "c" involves the input dimension data->depth,
1553
 * then make sure that all the other coefficients are multiples of data->m,
1554
 * reducing data->m if needed.
1555
 * Break out of the iteration if data->m has become equal to "1".
1556
 */
1557
static isl_stat constraint_check_scaled(__isl_take isl_constraint *c,
1558
  void *user)
1559
21
{
1560
21
  struct isl_check_scaled_data *data = user;
1561
21
  int i, j, n;
1562
21
  enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_out,
1563
21
            isl_dim_div };
1564
21
1565
21
  if (!isl_constraint_involves_dims(c, isl_dim_in, data->depth, 1)) {
1566
9
    isl_constraint_free(c);
1567
9
    return isl_stat_ok;
1568
9
  }
1569
12
1570
45
  
for (i = 0; 12
i < 4;
++i33
) {
1571
39
    n = isl_constraint_dim(c, t[i]);
1572
80
    for (j = 0; j < n; 
++j41
) {
1573
47
      isl_val *d;
1574
47
1575
47
      if (t[i] == isl_dim_in && 
j == data->depth26
)
1576
12
        continue;
1577
35
      if (!isl_constraint_involves_dims(c, t[i], j, 1))
1578
23
        continue;
1579
12
      d = isl_constraint_get_coefficient_val(c, t[i], j);
1580
12
      data->m = isl_val_gcd(data->m, d);
1581
12
      if (isl_val_is_one(data->m))
1582
6
        break;
1583
12
    }
1584
39
    if (j < n)
1585
6
      break;
1586
39
  }
1587
12
1588
12
  isl_constraint_free(c);
1589
12
1590
12
  return i < 4 ? 
isl_stat_error6
:
isl_stat_ok6
;
1591
12
}
1592
1593
/* For each constraint of "bmap" that involves the input dimension data->depth,
1594
 * make sure that all the other coefficients are multiples of data->m,
1595
 * reducing data->m if needed.
1596
 * Break out of the iteration if data->m has become equal to "1".
1597
 */
1598
static isl_stat basic_map_check_scaled(__isl_take isl_basic_map *bmap,
1599
  void *user)
1600
6
{
1601
6
  isl_stat r;
1602
6
1603
6
  r = isl_basic_map_foreach_constraint(bmap,
1604
6
            &constraint_check_scaled, user);
1605
6
  isl_basic_map_free(bmap);
1606
6
1607
6
  return r;
1608
6
}
1609
1610
/* For each constraint of "map" that involves the input dimension data->depth,
1611
 * make sure that all the other coefficients are multiples of data->m,
1612
 * reducing data->m if needed.
1613
 * Break out of the iteration if data->m has become equal to "1".
1614
 */
1615
static isl_stat map_check_scaled(__isl_take isl_map *map, void *user)
1616
6
{
1617
6
  isl_stat r;
1618
6
1619
6
  r = isl_map_foreach_basic_map(map, &basic_map_check_scaled, user);
1620
6
  isl_map_free(map);
1621
6
1622
6
  return r;
1623
6
}
1624
1625
/* Create an AST node for the current dimension based on
1626
 * the schedule domain "bounds" and return the node encapsulated
1627
 * in an isl_ast_graft.
1628
 *
1629
 * "executed" is the current inverse schedule, taking into account
1630
 * the bounds in "bounds"
1631
 * "domain" is the domain of "executed", with inner dimensions projected out.
1632
 *
1633
 *
1634
 * Before moving on to the actual AST node construction in create_node_scaled,
1635
 * we first check if the current dimension is strided and if we can scale
1636
 * down this stride.  Note that we only do this if the ast_build_scale_strides
1637
 * option is set.
1638
 *
1639
 * In particular, let the current dimension take on values
1640
 *
1641
 *  f + s a
1642
 *
1643
 * with a an integer.  We check if we can find an integer m that (obviously)
1644
 * divides both f and s.
1645
 *
1646
 * If so, we check if the current dimension only appears in constraints
1647
 * where the coefficients of the other variables are multiples of m.
1648
 * We perform this extra check to avoid the risk of introducing
1649
 * divisions by scaling down the current dimension.
1650
 *
1651
 * If so, we scale the current dimension down by a factor of m.
1652
 * That is, we plug in
1653
 *
1654
 *  i = m i'              (1)
1655
 *
1656
 * Note that in principle we could always scale down strided loops
1657
 * by plugging in
1658
 *
1659
 *  i = f + s i'
1660
 *
1661
 * but this may result in i' taking on larger values than the original i,
1662
 * due to the shift by "f".
1663
 * By constrast, the scaling in (1) can only reduce the (absolute) value "i".
1664
 */
1665
static __isl_give isl_ast_graft *create_node(__isl_take isl_union_map *executed,
1666
  __isl_take isl_basic_set *bounds, __isl_take isl_set *domain,
1667
  __isl_take isl_ast_build *build)
1668
3.85k
{
1669
3.85k
  struct isl_check_scaled_data data;
1670
3.85k
  isl_ctx *ctx;
1671
3.85k
  isl_aff *offset;
1672
3.85k
  isl_val *d;
1673
3.85k
1674
3.85k
  ctx = isl_ast_build_get_ctx(build);
1675
3.85k
  if (!isl_options_get_ast_build_scale_strides(ctx))
1676
0
    return create_node_scaled(executed, bounds, domain, build);
1677
3.85k
1678
3.85k
  data.depth = isl_ast_build_get_depth(build);
1679
3.85k
  if (!isl_ast_build_has_stride(build, data.depth))
1680
3.84k
    return create_node_scaled(executed, bounds, domain, build);
1681
14
1682
14
  offset = isl_ast_build_get_offset(build, data.depth);
1683
14
  data.m = isl_ast_build_get_stride(build, data.depth);
1684
14
  if (!data.m)
1685
0
    offset = isl_aff_free(offset);
1686
14
  offset = isl_aff_scale_down_val(offset, isl_val_copy(data.m));
1687
14
  d = isl_aff_get_denominator_val(offset);
1688
14
  if (!d)
1689
0
    executed = isl_union_map_free(executed);
1690
14
1691
14
  if (executed && isl_val_is_divisible_by(data.m, d))
1692
12
    data.m = isl_val_div(data.m, d);
1693
2
  else {
1694
2
    data.m = isl_val_set_si(data.m, 1);
1695
2
    isl_val_free(d);
1696
2
  }
1697
14
1698
14
  if (!isl_val_is_one(data.m)) {
1699
6
    if (isl_union_map_foreach_map(executed, &map_check_scaled,
1700
6
            &data) < 0 &&
1701
6
        !isl_val_is_one(data.m))
1702
0
      executed = isl_union_map_free(executed);
1703
6
  }
1704
14
1705
14
  if (!isl_val_is_one(data.m)) {
1706
0
    isl_space *space;
1707
0
    isl_multi_aff *ma;
1708
0
    isl_aff *aff;
1709
0
    isl_map *map;
1710
0
    isl_union_map *umap;
1711
0
1712
0
    space = isl_ast_build_get_space(build, 1);
1713
0
    space = isl_space_map_from_set(space);
1714
0
    ma = isl_multi_aff_identity(space);
1715
0
    aff = isl_multi_aff_get_aff(ma, data.depth);
1716
0
    aff = isl_aff_scale_val(aff, isl_val_copy(data.m));
1717
0
    ma = isl_multi_aff_set_aff(ma, data.depth, aff);
1718
0
1719
0
    bounds = isl_basic_set_preimage_multi_aff(bounds,
1720
0
            isl_multi_aff_copy(ma));
1721
0
    domain = isl_set_preimage_multi_aff(domain,
1722
0
            isl_multi_aff_copy(ma));
1723
0
    map = isl_map_reverse(isl_map_from_multi_aff(ma));
1724
0
    umap = isl_union_map_from_map(map);
1725
0
    executed = isl_union_map_apply_domain(executed,
1726
0
            isl_union_map_copy(umap));
1727
0
    build = isl_ast_build_scale_down(build, isl_val_copy(data.m),
1728
0
            umap);
1729
0
  }
1730
14
  isl_aff_free(offset);
1731
14
  isl_val_free(data.m);
1732
14
1733
14
  return create_node_scaled(executed, bounds, domain, build);
1734
14
}
1735
1736
/* Add the basic set to the list that "user" points to.
1737
 */
1738
static isl_stat collect_basic_set(__isl_take isl_basic_set *bset, void *user)
1739
938
{
1740
938
  isl_basic_set_list **list = user;
1741
938
1742
938
  *list = isl_basic_set_list_add(*list, bset);
1743
938
1744
938
  return isl_stat_ok;
1745
938
}
1746
1747
/* Extract the basic sets of "set" and collect them in an isl_basic_set_list.
1748
 */
1749
static __isl_give isl_basic_set_list *isl_basic_set_list_from_set(
1750
  __isl_take isl_set *set)
1751
898
{
1752
898
  int n;
1753
898
  isl_ctx *ctx;
1754
898
  isl_basic_set_list *list;
1755
898
1756
898
  if (!set)
1757
0
    return NULL;
1758
898
1759
898
  ctx = isl_set_get_ctx(set);
1760
898
1761
898
  n = isl_set_n_basic_set(set);
1762
898
  list = isl_basic_set_list_alloc(ctx, n);
1763
898
  if (isl_set_foreach_basic_set(set, &collect_basic_set, &list) < 0)
1764
0
    list = isl_basic_set_list_free(list);
1765
898
1766
898
  isl_set_free(set);
1767
898
  return list;
1768
898
}
1769
1770
/* Generate code for the schedule domain "bounds"
1771
 * and add the result to "list".
1772
 *
1773
 * We mainly detect strides here and check if the bounds do not
1774
 * conflict with the current build domain
1775
 * and then pass over control to create_node.
1776
 *
1777
 * "bounds" reflects the bounds on the current dimension and possibly
1778
 * some extra conditions on outer dimensions.
1779
 * It does not, however, include any divs involving the current dimension,
1780
 * so it does not capture any stride constraints.
1781
 * We therefore need to compute that part of the schedule domain that
1782
 * intersects with "bounds" and derive the strides from the result.
1783
 */
1784
static __isl_give isl_ast_graft_list *add_node(
1785
  __isl_take isl_ast_graft_list *list, __isl_take isl_union_map *executed,
1786
  __isl_take isl_basic_set *bounds, __isl_take isl_ast_build *build)
1787
5.35k
{
1788
5.35k
  isl_ast_graft *graft;
1789
5.35k
  isl_set *domain = NULL;
1790
5.35k
  isl_union_set *uset;
1791
5.35k
  int empty, disjoint;
1792
5.35k
1793
5.35k
  uset = isl_union_set_from_basic_set(isl_basic_set_copy(bounds));
1794
5.35k
  executed = isl_union_map_intersect_domain(executed, uset);
1795
5.35k
  empty = isl_union_map_is_empty(executed);
1796
5.35k
  if (empty < 0)
1797
0
    goto error;
1798
5.35k
  if (empty)
1799
0
    goto done;
1800
5.35k
1801
5.35k
  uset = isl_union_map_domain(isl_union_map_copy(executed));
1802
5.35k
  domain = isl_set_from_union_set(uset);
1803
5.35k
  domain = isl_ast_build_specialize(build, domain);
1804
5.35k
1805
5.35k
  domain = isl_set_compute_divs(domain);
1806
5.35k
  domain = isl_ast_build_eliminate_inner(build, domain);
1807
5.35k
  disjoint = isl_set_is_disjoint(domain, build->domain);
1808
5.35k
  if (disjoint < 0)
1809
0
    goto error;
1810
5.35k
  if (disjoint)
1811
1.49k
    goto done;
1812
3.85k
1813
3.85k
  build = isl_ast_build_detect_strides(build, isl_set_copy(domain));
1814
3.85k
1815
3.85k
  graft = create_node(executed, bounds, domain,
1816
3.85k
        isl_ast_build_copy(build));
1817
3.85k
  list = isl_ast_graft_list_add(list, graft);
1818
3.85k
  isl_ast_build_free(build);
1819
3.85k
  return list;
1820
0
error:
1821
0
  list = isl_ast_graft_list_free(list);
1822
1.49k
done:
1823
1.49k
  isl_set_free(domain);
1824
1.49k
  isl_basic_set_free(bounds);
1825
1.49k
  isl_union_map_free(executed);
1826
1.49k
  isl_ast_build_free(build);
1827
1.49k
  return list;
1828
0
}
1829
1830
/* Does any element of i follow or coincide with any element of j
1831
 * at the current depth for equal values of the outer dimensions?
1832
 */
1833
static isl_bool domain_follows_at_depth(__isl_keep isl_basic_set *i,
1834
  __isl_keep isl_basic_set *j, void *user)
1835
37
{
1836
37
  int depth = *(int *) user;
1837
37
  isl_basic_map *test;
1838
37
  isl_bool empty;
1839
37
  int l;
1840
37
1841
37
  test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
1842
37
                isl_basic_set_copy(j));
1843
55
  for (l = 0; l < depth; 
++l18
)
1844
18
    test = isl_basic_map_equate(test, isl_dim_in, l,
1845
18
            isl_dim_out, l);
1846
37
  test = isl_basic_map_order_ge(test, isl_dim_in, depth,
1847
37
          isl_dim_out, depth);
1848
37
  empty = isl_basic_map_is_empty(test);
1849
37
  isl_basic_map_free(test);
1850
37
1851
37
  return empty < 0 ? 
isl_bool_error0
: !empty;
1852
37
}
1853
1854
/* Split up each element of "list" into a part that is related to "bset"
1855
 * according to "gt" and a part that is not.
1856
 * Return a list that consist of "bset" and all the pieces.
1857
 */
1858
static __isl_give isl_basic_set_list *add_split_on(
1859
  __isl_take isl_basic_set_list *list, __isl_take isl_basic_set *bset,
1860
  __isl_keep isl_basic_map *gt)
1861
0
{
1862
0
  int i, n;
1863
0
  isl_basic_set_list *res;
1864
0
1865
0
  if (!list)
1866
0
    bset = isl_basic_set_free(bset);
1867
0
1868
0
  gt = isl_basic_map_copy(gt);
1869
0
  gt = isl_basic_map_intersect_domain(gt, isl_basic_set_copy(bset));
1870
0
  n = isl_basic_set_list_n_basic_set(list);
1871
0
  res = isl_basic_set_list_from_basic_set(bset);
1872
0
  for (i = 0; res && i < n; ++i) {
1873
0
    isl_basic_set *bset;
1874
0
    isl_set *set1, *set2;
1875
0
    isl_basic_map *bmap;
1876
0
    int empty;
1877
0
1878
0
    bset = isl_basic_set_list_get_basic_set(list, i);
1879
0
    bmap = isl_basic_map_copy(gt);
1880
0
    bmap = isl_basic_map_intersect_range(bmap, bset);
1881
0
    bset = isl_basic_map_range(bmap);
1882
0
    empty = isl_basic_set_is_empty(bset);
1883
0
    if (empty < 0)
1884
0
      res = isl_basic_set_list_free(res);
1885
0
    if (empty)  {
1886
0
      isl_basic_set_free(bset);
1887
0
      bset = isl_basic_set_list_get_basic_set(list, i);
1888
0
      res = isl_basic_set_list_add(res, bset);
1889
0
      continue;
1890
0
    }
1891
0
1892
0
    res = isl_basic_set_list_add(res, isl_basic_set_copy(bset));
1893
0
    set1 = isl_set_from_basic_set(bset);
1894
0
    bset = isl_basic_set_list_get_basic_set(list, i);
1895
0
    set2 = isl_set_from_basic_set(bset);
1896
0
    set1 = isl_set_subtract(set2, set1);
1897
0
    set1 = isl_set_make_disjoint(set1);
1898
0
1899
0
    res = isl_basic_set_list_concat(res,
1900
0
              isl_basic_set_list_from_set(set1));
1901
0
  }
1902
0
  isl_basic_map_free(gt);
1903
0
  isl_basic_set_list_free(list);
1904
0
  return res;
1905
0
}
1906
1907
static __isl_give isl_ast_graft_list *generate_sorted_domains(
1908
  __isl_keep isl_basic_set_list *domain_list,
1909
  __isl_keep isl_union_map *executed,
1910
  __isl_keep isl_ast_build *build);
1911
1912
/* Internal data structure for add_nodes.
1913
 *
1914
 * "executed" and "build" are extra arguments to be passed to add_node.
1915
 * "list" collects the results.
1916
 */
1917
struct isl_add_nodes_data {
1918
  isl_union_map *executed;
1919
  isl_ast_build *build;
1920
1921
  isl_ast_graft_list *list;
1922
};
1923
1924
/* Generate code for the schedule domains in "scc"
1925
 * and add the results to "list".
1926
 *
1927
 * The domains in "scc" form a strongly connected component in the ordering.
1928
 * If the number of domains in "scc" is larger than 1, then this means
1929
 * that we cannot determine a valid ordering for the domains in the component.
1930
 * This should be fairly rare because the individual domains
1931
 * have been made disjoint first.
1932
 * The problem is that the domains may be integrally disjoint but not
1933
 * rationally disjoint.  For example, we may have domains
1934
 *
1935
 *  { [i,i] : 0 <= i <= 1 }   and { [i,1-i] : 0 <= i <= 1 }
1936
 *
1937
 * These two domains have an empty intersection, but their rational
1938
 * relaxations do intersect.  It is impossible to order these domains
1939
 * in the second dimension because the first should be ordered before
1940
 * the second for outer dimension equal to 0, while it should be ordered
1941
 * after for outer dimension equal to 1.
1942
 *
1943
 * This may happen in particular in case of unrolling since the domain
1944
 * of each slice is replaced by its simple hull.
1945
 *
1946
 * For each basic set i in "scc" and for each of the following basic sets j,
1947
 * we split off that part of the basic set i that shares the outer dimensions
1948
 * with j and lies before j in the current dimension.
1949
 * We collect all the pieces in a new list that replaces "scc".
1950
 *
1951
 * While the elements in "scc" should be disjoint, we double-check
1952
 * this property to avoid running into an infinite recursion in case
1953
 * they intersect due to some internal error.
1954
 */
1955
static isl_stat add_nodes(__isl_take isl_basic_set_list *scc, void *user)
1956
32
{
1957
32
  struct isl_add_nodes_data *data = user;
1958
32
  int i, n, depth;
1959
32
  isl_basic_set *bset, *first;
1960
32
  isl_basic_set_list *list;
1961
32
  isl_space *space;
1962
32
  isl_basic_map *gt;
1963
32
1964
32
  n = isl_basic_set_list_n_basic_set(scc);
1965
32
  bset = isl_basic_set_list_get_basic_set(scc, 0);
1966
32
  if (n == 1) {
1967
32
    isl_basic_set_list_free(scc);
1968
32
    data->list = add_node(data->list,
1969
32
        isl_union_map_copy(data->executed), bset,
1970
32
        isl_ast_build_copy(data->build));
1971
32
    return data->list ? isl_stat_ok : 
isl_stat_error0
;
1972
32
  }
1973
0
1974
0
  depth = isl_ast_build_get_depth(data->build);
1975
0
  space = isl_basic_set_get_space(bset);
1976
0
  space = isl_space_map_from_set(space);
1977
0
  gt = isl_basic_map_universe(space);
1978
0
  for (i = 0; i < depth; ++i)
1979
0
    gt = isl_basic_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
1980
0
  gt = isl_basic_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
1981
0
1982
0
  first = isl_basic_set_copy(bset);
1983
0
  list = isl_basic_set_list_from_basic_set(bset);
1984
0
  for (i = 1; i < n; ++i) {
1985
0
    int disjoint;
1986
0
1987
0
    bset = isl_basic_set_list_get_basic_set(scc, i);
1988
0
1989
0
    disjoint = isl_basic_set_is_disjoint(bset, first);
1990
0
    if (disjoint < 0)
1991
0
      list = isl_basic_set_list_free(list);
1992
0
    else if (!disjoint)
1993
0
      isl_die(isl_basic_set_list_get_ctx(scc),
1994
0
        isl_error_internal,
1995
0
        "basic sets in scc are assumed to be disjoint",
1996
0
        list = isl_basic_set_list_free(list));
1997
0
1998
0
    list = add_split_on(list, bset, gt);
1999
0
  }
2000
0
  isl_basic_set_free(first);
2001
0
  isl_basic_map_free(gt);
2002
0
  isl_basic_set_list_free(scc);
2003
0
  scc = list;
2004
0
  data->list = isl_ast_graft_list_concat(data->list,
2005
0
        generate_sorted_domains(scc, data->executed, data->build));
2006
0
  isl_basic_set_list_free(scc);
2007
0
2008
0
  return data->list ? isl_stat_ok : isl_stat_error;
2009
0
}
2010
2011
/* Sort the domains in "domain_list" according to the execution order
2012
 * at the current depth (for equal values of the outer dimensions),
2013
 * generate code for each of them, collecting the results in a list.
2014
 * If no code is generated (because the intersection of the inverse schedule
2015
 * with the domains turns out to be empty), then an empty list is returned.
2016
 *
2017
 * The caller is responsible for ensuring that the basic sets in "domain_list"
2018
 * are pair-wise disjoint.  It can, however, in principle happen that
2019
 * two basic sets should be ordered one way for one value of the outer
2020
 * dimensions and the other way for some other value of the outer dimensions.
2021
 * We therefore play safe and look for strongly connected components.
2022
 * The function add_nodes takes care of handling non-trivial components.
2023
 */
2024
static __isl_give isl_ast_graft_list *generate_sorted_domains(
2025
  __isl_keep isl_basic_set_list *domain_list,
2026
  __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2027
932
{
2028
932
  isl_ctx *ctx;
2029
932
  struct isl_add_nodes_data data;
2030
932
  int depth;
2031
932
  int n;
2032
932
2033
932
  if (!domain_list)
2034
0
    return NULL;
2035
932
2036
932
  ctx = isl_basic_set_list_get_ctx(domain_list);
2037
932
  n = isl_basic_set_list_n_basic_set(domain_list);
2038
932
  data.list = isl_ast_graft_list_alloc(ctx, n);
2039
932
  if (n == 0)
2040
0
    return data.list;
2041
932
  if (n == 1)
2042
919
    return add_node(data.list, isl_union_map_copy(executed),
2043
919
      isl_basic_set_list_get_basic_set(domain_list, 0),
2044
919
      isl_ast_build_copy(build));
2045
13
2046
13
  depth = isl_ast_build_get_depth(build);
2047
13
  data.executed = executed;
2048
13
  data.build = build;
2049
13
  if (isl_basic_set_list_foreach_scc(domain_list,
2050
13
          &domain_follows_at_depth, &depth,
2051
13
          &add_nodes, &data) < 0)
2052
0
    data.list = isl_ast_graft_list_free(data.list);
2053
13
2054
13
  return data.list;
2055
13
}
2056
2057
/* Do i and j share any values for the outer dimensions?
2058
 */
2059
static isl_bool shared_outer(__isl_keep isl_basic_set *i,
2060
  __isl_keep isl_basic_set *j, void *user)
2061
83
{
2062
83
  int depth = *(int *) user;
2063
83
  isl_basic_map *test;
2064
83
  isl_bool empty;
2065
83
  int l;
2066
83
2067
83
  test = isl_basic_map_from_domain_and_range(isl_basic_set_copy(i),
2068
83
                isl_basic_set_copy(j));
2069
155
  for (l = 0; l < depth; 
++l72
)
2070
72
    test = isl_basic_map_equate(test, isl_dim_in, l,
2071
72
            isl_dim_out, l);
2072
83
  empty = isl_basic_map_is_empty(test);
2073
83
  isl_basic_map_free(test);
2074
83
2075
83
  return empty < 0 ? 
isl_bool_error0
: !empty;
2076
83
}
2077
2078
/* Internal data structure for generate_sorted_domains_wrap.
2079
 *
2080
 * "n" is the total number of basic sets
2081
 * "executed" and "build" are extra arguments to be passed
2082
 *  to generate_sorted_domains.
2083
 *
2084
 * "single" is set to 1 by generate_sorted_domains_wrap if there
2085
 * is only a single component.
2086
 * "list" collects the results.
2087
 */
2088
struct isl_ast_generate_parallel_domains_data {
2089
  int n;
2090
  isl_union_map *executed;
2091
  isl_ast_build *build;
2092
2093
  int single;
2094
  isl_ast_graft_list *list;
2095
};
2096
2097
/* Call generate_sorted_domains on "scc", fuse the result into a list
2098
 * with either zero or one graft and collect the these single element
2099
 * lists into data->list.
2100
 *
2101
 * If there is only one component, i.e., if the number of basic sets
2102
 * in the current component is equal to the total number of basic sets,
2103
 * then data->single is set to 1 and the result of generate_sorted_domains
2104
 * is not fused.
2105
 */
2106
static isl_stat generate_sorted_domains_wrap(__isl_take isl_basic_set_list *scc,
2107
  void *user)
2108
71
{
2109
71
  struct isl_ast_generate_parallel_domains_data *data = user;
2110
71
  isl_ast_graft_list *list;
2111
71
2112
71
  list = generate_sorted_domains(scc, data->executed, data->build);
2113
71
  data->single = isl_basic_set_list_n_basic_set(scc) == data->n;
2114
71
  if (!data->single)
2115
58
    list = isl_ast_graft_list_fuse(list, data->build);
2116
71
  if (!data->list)
2117
41
    data->list = list;
2118
30
  else
2119
30
    data->list = isl_ast_graft_list_concat(data->list, list);
2120
71
2121
71
  isl_basic_set_list_free(scc);
2122
71
  if (!data->list)
2123
0
    return isl_stat_error;
2124
71
2125
71
  return isl_stat_ok;
2126
71
}
2127
2128
/* Look for any (weakly connected) components in the "domain_list"
2129
 * of domains that share some values of the outer dimensions.
2130
 * That is, domains in different components do not share any values
2131
 * of the outer dimensions.  This means that these components
2132
 * can be freely reordered.
2133
 * Within each of the components, we sort the domains according
2134
 * to the execution order at the current depth.
2135
 *
2136
 * If there is more than one component, then generate_sorted_domains_wrap
2137
 * fuses the result of each call to generate_sorted_domains
2138
 * into a list with either zero or one graft and collects these (at most)
2139
 * single element lists into a bigger list. This means that the elements of the
2140
 * final list can be freely reordered.  In particular, we sort them
2141
 * according to an arbitrary but fixed ordering to ease merging of
2142
 * graft lists from different components.
2143
 */
2144
static __isl_give isl_ast_graft_list *generate_parallel_domains(
2145
  __isl_keep isl_basic_set_list *domain_list,
2146
  __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
2147
902
{
2148
902
  int depth;
2149
902
  struct isl_ast_generate_parallel_domains_data data;
2150
902
2151
902
  if (!domain_list)
2152
0
    return NULL;
2153
902
2154
902
  data.n = isl_basic_set_list_n_basic_set(domain_list);
2155
902
  if (data.n <= 1)
2156
861
    return generate_sorted_domains(domain_list, executed, build);
2157
41
2158
41
  depth = isl_ast_build_get_depth(build);
2159
41
  data.list = NULL;
2160
41
  data.executed = executed;
2161
41
  data.build = build;
2162
41
  data.single = 0;
2163
41
  if (isl_basic_set_list_foreach_scc(domain_list, &shared_outer, &depth,
2164
41
              &generate_sorted_domains_wrap,
2165
41
              &data) < 0)
2166
0
    data.list = isl_ast_graft_list_free(data.list);
2167
41
2168
41
  if (!data.single)
2169
28
    data.list = isl_ast_graft_list_sort_guard(data.list);
2170
41
2171
41
  return data.list;
2172
41
}
2173
2174
/* Internal data for separate_domain.
2175
 *
2176
 * "explicit" is set if we only want to use explicit bounds.
2177
 *
2178
 * "domain" collects the separated domains.
2179
 */
2180
struct isl_separate_domain_data {
2181
  isl_ast_build *build;
2182
  int explicit;
2183
  isl_set *domain;
2184
};
2185
2186
/* Extract implicit bounds on the current dimension for the executed "map".
2187
 *
2188
 * The domain of "map" may involve inner dimensions, so we
2189
 * need to eliminate them.
2190
 */
2191
static __isl_give isl_set *implicit_bounds(__isl_take isl_map *map,
2192
  __isl_keep isl_ast_build *build)
2193
0
{
2194
0
  isl_set *domain;
2195
0
2196
0
  domain = isl_map_domain(map);
2197
0
  domain = isl_ast_build_eliminate(build, domain);
2198
0
2199
0
  return domain;
2200
0
}
2201
2202
/* Extract explicit bounds on the current dimension for the executed "map".
2203
 *
2204
 * Rather than eliminating the inner dimensions as in implicit_bounds,
2205
 * we simply drop any constraints involving those inner dimensions.
2206
 * The idea is that most bounds that are implied by constraints on the
2207
 * inner dimensions will be enforced by for loops and not by explicit guards.
2208
 * There is then no need to separate along those bounds.
2209
 */
2210
static __isl_give isl_set *explicit_bounds(__isl_take isl_map *map,
2211
  __isl_keep isl_ast_build *build)
2212
24
{
2213
24
  isl_set *domain;
2214
24
  int depth, dim;
2215
24
2216
24
  dim = isl_map_dim(map, isl_dim_out);
2217
24
  map = isl_map_drop_constraints_involving_dims(map, isl_dim_out, 0, dim);
2218
24
2219
24
  domain = isl_map_domain(map);
2220
24
  depth = isl_ast_build_get_depth(build);
2221
24
  dim = isl_set_dim(domain, isl_dim_set);
2222
24
  domain = isl_set_detect_equalities(domain);
2223
24
  domain = isl_set_drop_constraints_involving_dims(domain,
2224
24
        isl_dim_set, depth + 1, dim - (depth + 1));
2225
24
  domain = isl_set_remove_divs_involving_dims(domain,
2226
24
        isl_dim_set, depth, 1);
2227
24
  domain = isl_set_remove_unknown_divs(domain);
2228
24
2229
24
  return domain;
2230
24
}
2231
2232
/* Split data->domain into pieces that intersect with the range of "map"
2233
 * and pieces that do not intersect with the range of "map"
2234
 * and then add that part of the range of "map" that does not intersect
2235
 * with data->domain.
2236
 */
2237
static isl_stat separate_domain(__isl_take isl_map *map, void *user)
2238
24
{
2239
24
  struct isl_separate_domain_data *data = user;
2240
24
  isl_set *domain;
2241
24
  isl_set *d1, *d2;
2242
24
2243
24
  if (data->explicit)
2244
24
    domain = explicit_bounds(map, data->build);
2245
0
  else
2246
0
    domain = implicit_bounds(map, data->build);
2247
24
2248
24
  domain = isl_set_coalesce(domain);
2249
24
  domain = isl_set_make_disjoint(domain);
2250
24
  d1 = isl_set_subtract(isl_set_copy(domain), isl_set_copy(data->domain));
2251
24
  d2 = isl_set_subtract(isl_set_copy(data->domain), isl_set_copy(domain));
2252
24
  data->domain = isl_set_intersect(data->domain, domain);
2253
24
  data->domain = isl_set_union(data->domain, d1);
2254
24
  data->domain = isl_set_union(data->domain, d2);
2255
24
2256
24
  return isl_stat_ok;
2257
24
}
2258
2259
/* Separate the schedule domains of "executed".
2260
 *
2261
 * That is, break up the domain of "executed" into basic sets,
2262
 * such that for each basic set S, every element in S is associated with
2263
 * the same domain spaces.
2264
 *
2265
 * "space" is the (single) domain space of "executed".
2266
 */
2267
static __isl_give isl_set *separate_schedule_domains(
2268
  __isl_take isl_space *space, __isl_take isl_union_map *executed,
2269
  __isl_keep isl_ast_build *build)
2270
24
{
2271
24
  struct isl_separate_domain_data data = { build };
2272
24
  isl_ctx *ctx;
2273
24
2274
24
  ctx = isl_ast_build_get_ctx(build);
2275
24
  data.explicit = isl_options_get_ast_build_separation_bounds(ctx) ==
2276
24
            ISL_AST_BUILD_SEPARATION_BOUNDS_EXPLICIT;
2277
24
  data.domain = isl_set_empty(space);
2278
24
  if (isl_union_map_foreach_map(executed, &separate_domain, &data) < 0)
2279
0
    data.domain = isl_set_free(data.domain);
2280
24
2281
24
  isl_union_map_free(executed);
2282
24
  return data.domain;
2283
24
}
2284
2285
/* Temporary data used during the search for a lower bound for unrolling.
2286
 *
2287
 * "build" is the build in which the unrolling will be performed
2288
 * "domain" is the original set for which to find a lower bound
2289
 * "depth" is the dimension for which to find a lower boudn
2290
 * "expansion" is the expansion that needs to be applied to "domain"
2291
 * in the unrolling that will be performed
2292
 *
2293
 * "lower" is the best lower bound found so far.  It is NULL if we have not
2294
 * found any yet.
2295
 * "n" is the corresponding size.  If lower is NULL, then the value of n
2296
 * is undefined.
2297
 * "n_div" is the maximal number of integer divisions in the first
2298
 * unrolled iteration (after expansion).  It is set to -1 if it hasn't
2299
 * been computed yet.
2300
 */
2301
struct isl_find_unroll_data {
2302
  isl_ast_build *build;
2303
  isl_set *domain;
2304
  int depth;
2305
  isl_basic_map *expansion;
2306
2307
  isl_aff *lower;
2308
  int *n;
2309
  int n_div;
2310
};
2311
2312
/* Return the constraint
2313
 *
2314
 *  i_"depth" = aff + offset
2315
 */
2316
static __isl_give isl_constraint *at_offset(int depth, __isl_keep isl_aff *aff,
2317
  int offset)
2318
4.41k
{
2319
4.41k
  aff = isl_aff_copy(aff);
2320
4.41k
  aff = isl_aff_add_coefficient_si(aff, isl_dim_in, depth, -1);
2321
4.41k
  aff = isl_aff_add_constant_si(aff, offset);
2322
4.41k
  return isl_equality_from_aff(aff);
2323
4.41k
}
2324
2325
/* Update *user to the number of integer divsions in the first element
2326
 * of "ma", if it is larger than the current value.
2327
 */
2328
static isl_stat update_n_div(__isl_take isl_set *set,
2329
  __isl_take isl_multi_aff *ma, void *user)
2330
2
{
2331
2
  isl_aff *aff;
2332
2
  int *n = user;
2333
2
  int n_div;
2334
2
2335
2
  aff = isl_multi_aff_get_aff(ma, 0);
2336
2
  n_div = isl_aff_dim(aff, isl_dim_div);
2337
2
  isl_aff_free(aff);
2338
2
  isl_multi_aff_free(ma);
2339
2
  isl_set_free(set);
2340
2
2341
2
  if (n_div > *n)
2342
2
    *n = n_div;
2343
2
2344
2
  return aff ? isl_stat_ok : 
isl_stat_error0
;
2345
2
}
2346
2347
/* Get the number of integer divisions in the expression for the iterator
2348
 * value at the first slice in the unrolling based on lower bound "lower",
2349
 * taking into account the expansion that needs to be performed on this slice.
2350
 */
2351
static int get_expanded_n_div(struct isl_find_unroll_data *data,
2352
  __isl_keep isl_aff *lower)
2353
2
{
2354
2
  isl_constraint *c;
2355
2
  isl_set *set;
2356
2
  isl_map *it_map, *expansion;
2357
2
  isl_pw_multi_aff *pma;
2358
2
  int n;
2359
2
2360
2
  c = at_offset(data->depth, lower, 0);
2361
2
  set = isl_set_copy(data->domain);
2362
2
  set = isl_set_add_constraint(set, c);
2363
2
  expansion = isl_map_from_basic_map(isl_basic_map_copy(data->expansion));
2364
2
  set = isl_set_apply(set, expansion);
2365
2
  it_map = isl_ast_build_map_to_iterator(data->build, set);
2366
2
  pma = isl_pw_multi_aff_from_map(it_map);
2367
2
  n = 0;
2368
2
  if (isl_pw_multi_aff_foreach_piece(pma, &update_n_div, &n) < 0)
2369
0
    n = -1;
2370
2
  isl_pw_multi_aff_free(pma);
2371
2
2372
2
  return n;
2373
2
}
2374
2375
/* Is the lower bound "lower" with corresponding iteration count "n"
2376
 * better than the one stored in "data"?
2377
 * If there is no upper bound on the iteration count ("n" is infinity) or
2378
 * if the count is too large, then we cannot use this lower bound.
2379
 * Otherwise, if there was no previous lower bound or
2380
 * if the iteration count of the new lower bound is smaller than
2381
 * the iteration count of the previous lower bound, then we consider
2382
 * the new lower bound to be better.
2383
 * If the iteration count is the same, then compare the number
2384
 * of integer divisions that would be needed to express
2385
 * the iterator value at the first slice in the unrolling
2386
 * according to the lower bound.  If we end up computing this
2387
 * number, then store the lowest value in data->n_div.
2388
 */
2389
static int is_better_lower_bound(struct isl_find_unroll_data *data,
2390
  __isl_keep isl_aff *lower, __isl_keep isl_val *n)
2391
2.41k
{
2392
2.41k
  int cmp;
2393
2.41k
  int n_div;
2394
2.41k
2395
2.41k
  if (!n)
2396
0
    return -1;
2397
2.41k
  if (isl_val_is_infty(n))
2398
0
    return 0;
2399
2.41k
  if (isl_val_cmp_si(n, INT_MAX) > 0)
2400
0
    return 0;
2401
2.41k
  if (!data->lower)
2402
2.31k
    return 1;
2403
101
  cmp = isl_val_cmp_si(n, *data->n);
2404
101
  if (cmp < 0)
2405
91
    return 1;
2406
10
  if (cmp > 0)
2407
9
    return 0;
2408
1
  if (data->n_div < 0)
2409
1
    data->n_div = get_expanded_n_div(data, data->lower);
2410
1
  if (data->n_div < 0)
2411
0
    return -1;
2412
1
  if (data->n_div == 0)
2413
0
    return 0;
2414
1
  n_div = get_expanded_n_div(data, lower);
2415
1
  if (n_div < 0)
2416
0
    return -1;
2417
1
  if (n_div >= data->n_div)
2418
0
    return 0;
2419
1
  data->n_div = n_div;
2420
1
2421
1
  return 1;
2422
1
}
2423
2424
/* Check if we can use "c" as a lower bound and if it is better than
2425
 * any previously found lower bound.
2426
 *
2427
 * If "c" does not involve the dimension at the current depth,
2428
 * then we cannot use it.
2429
 * Otherwise, let "c" be of the form
2430
 *
2431
 *  i >= f(j)/a
2432
 *
2433
 * We compute the maximal value of
2434
 *
2435
 *  -ceil(f(j)/a)) + i + 1
2436
 *
2437
 * over the domain.  If there is such a value "n", then we know
2438
 *
2439
 *  -ceil(f(j)/a)) + i + 1 <= n
2440
 *
2441
 * or
2442
 *
2443
 *  i < ceil(f(j)/a)) + n
2444
 *
2445
 * meaning that we can use ceil(f(j)/a)) as a lower bound for unrolling.
2446
 * We just need to check if we have found any lower bound before and
2447
 * if the new lower bound is better (smaller n or fewer integer divisions)
2448
 * than the previously found lower bounds.
2449
 */
2450
static isl_stat update_unrolling_lower_bound(struct isl_find_unroll_data *data,
2451
  __isl_keep isl_constraint *c)
2452
31.3k
{
2453
31.3k
  isl_aff *aff, *lower;
2454
31.3k
  isl_val *max;
2455
31.3k
  int better;
2456
31.3k
2457
31.3k
  if (!isl_constraint_is_lower_bound(c, isl_dim_set, data->depth))
2458
28.9k
    return isl_stat_ok;
2459
2.41k
2460
2.41k
  lower = isl_constraint_get_bound(c, isl_dim_set, data->depth);
2461
2.41k
  lower = isl_aff_ceil(lower);
2462
2.41k
  aff = isl_aff_copy(lower);
2463
2.41k
  aff = isl_aff_neg(aff);
2464
2.41k
  aff = isl_aff_add_coefficient_si(aff, isl_dim_in, data->depth, 1);
2465
2.41k
  aff = isl_aff_add_constant_si(aff, 1);
2466
2.41k
  max = isl_set_max_val(data->domain, aff);
2467
2.41k
  isl_aff_free(aff);
2468
2.41k
2469
2.41k
  better = is_better_lower_bound(data, lower, max);
2470
2.41k
  if (better < 0 || !better) {
2471
9
    isl_val_free(max);
2472
9
    isl_aff_free(lower);
2473
9
    return better < 0 ? 
isl_stat_error0
: isl_stat_ok;
2474
9
  }
2475
2.40k
2476
2.40k
  isl_aff_free(data->lower);
2477
2.40k
  data->lower = lower;
2478
2.40k
  *data->n = isl_val_get_num_si(max);
2479
2.40k
  isl_val_free(max);
2480
2.40k
2481
2.40k
  return isl_stat_ok;
2482
2.40k
}
2483
2484
/* Check if we can use "c" as a lower bound and if it is better than
2485
 * any previously found lower bound.
2486
 */
2487
static isl_stat constraint_find_unroll(__isl_take isl_constraint *c, void *user)
2488
31.3k
{
2489
31.3k
  struct isl_find_unroll_data *data;
2490
31.3k
  isl_stat r;
2491
31.3k
2492
31.3k
  data = (struct isl_find_unroll_data *) user;
2493
31.3k
  r = update_unrolling_lower_bound(data, c);
2494
31.3k
  isl_constraint_free(c);
2495
31.3k
2496
31.3k
  return r;
2497
31.3k
}
2498
2499
/* Look for a lower bound l(i) on the dimension at "depth"
2500
 * and a size n such that "domain" is a subset of
2501
 *
2502
 *  { [i] : l(i) <= i_d < l(i) + n }
2503
 *
2504
 * where d is "depth" and l(i) depends only on earlier dimensions.
2505
 * Furthermore, try and find a lower bound such that n is as small as possible.
2506
 * In particular, "n" needs to be finite.
2507
 * "build" is the build in which the unrolling will be performed.
2508
 * "expansion" is the expansion that needs to be applied to "domain"
2509
 * in the unrolling that will be performed.
2510
 *
2511
 * Inner dimensions have been eliminated from "domain" by the caller.
2512
 *
2513
 * We first construct a collection of lower bounds on the input set
2514
 * by computing its simple hull.  We then iterate through them,
2515
 * discarding those that we cannot use (either because they do not
2516
 * involve the dimension at "depth" or because they have no corresponding
2517
 * upper bound, meaning that "n" would be unbounded) and pick out the
2518
 * best from the remaining ones.
2519
 *
2520
 * If we cannot find a suitable lower bound, then we consider that
2521
 * to be an error.
2522
 */
2523
static __isl_give isl_aff *find_unroll_lower_bound(
2524
  __isl_keep isl_ast_build *build, __isl_keep isl_set *domain,
2525
  int depth, __isl_keep isl_basic_map *expansion, int *n)
2526
2.31k
{
2527
2.31k
  struct isl_find_unroll_data data =
2528
2.31k
      { build, domain, depth, expansion, NULL, n, -1 };
2529
2.31k
  isl_basic_set *hull;
2530
2.31k
2531
2.31k
  hull = isl_set_simple_hull(isl_set_copy(domain));
2532
2.31k
2533
2.31k
  if (isl_basic_set_foreach_constraint(hull,
2534
2.31k
              &constraint_find_unroll, &data) < 0)
2535
0
    goto error;
2536
2.31k
2537
2.31k
  isl_basic_set_free(hull);
2538
2.31k
2539
2.31k
  if (!data.lower)
2540
2.31k
    
isl_die0
(isl_set_get_ctx(domain), isl_error_invalid,
2541
2.31k
      "cannot find lower bound for unrolling", return NULL);
2542
2.31k
2543
2.31k
  return data.lower;
2544
0
error:
2545
0
  isl_basic_set_free(hull);
2546
0
  return isl_aff_free(data.lower);
2547
2.31k
}
2548
2549
/* Call "fn" on each iteration of the current dimension of "domain".
2550
 * If "init" is not NULL, then it is called with the number of
2551
 * iterations before any call to "fn".
2552
 * Return -1 on failure.
2553
 *
2554
 * Since we are going to be iterating over the individual values,
2555
 * we first check if there are any strides on the current dimension.
2556
 * If there is, we rewrite the current dimension i as
2557
 *
2558
 *    i = stride i' + offset
2559
 *
2560
 * and then iterate over individual values of i' instead.
2561
 *
2562
 * We then look for a lower bound on i' and a size such that the domain
2563
 * is a subset of
2564
 *
2565
 *  { [j,i'] : l(j) <= i' < l(j) + n }
2566
 *
2567
 * and then take slices of the domain at values of i'
2568
 * between l(j) and l(j) + n - 1.
2569
 *
2570
 * We compute the unshifted simple hull of each slice to ensure that
2571
 * we have a single basic set per offset.  The slicing constraint
2572
 * may get simplified away before the unshifted simple hull is taken
2573
 * and may therefore in some rare cases disappear from the result.
2574
 * We therefore explicitly add the constraint back after computing
2575
 * the unshifted simple hull to ensure that the basic sets
2576
 * remain disjoint.  The constraints that are dropped by taking the hull
2577
 * will be taken into account at the next level, as in the case of the
2578
 * atomic option.
2579
 *
2580
 * Finally, we map i' back to i and call "fn".
2581
 */
2582
static int foreach_iteration(__isl_take isl_set *domain,
2583
  __isl_keep isl_ast_build *build, int (*init)(int n, void *user),
2584
  int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
2585
2.31k
{
2586
2.31k
  int i, n;
2587
2.31k
  int empty;
2588
2.31k
  int depth;
2589
2.31k
  isl_multi_aff *expansion;
2590
2.31k
  isl_basic_map *bmap;
2591
2.31k
  isl_aff *lower = NULL;
2592
2.31k
  isl_ast_build *stride_build;
2593
2.31k
2594
2.31k
  depth = isl_ast_build_get_depth(build);
2595
2.31k
2596
2.31k
  domain = isl_ast_build_eliminate_inner(build, domain);
2597
2.31k
  domain = isl_set_intersect(domain, isl_ast_build_get_domain(build));
2598
2.31k
  stride_build = isl_ast_build_copy(build);
2599
2.31k
  stride_build = isl_ast_build_detect_strides(stride_build,
2600
2.31k
              isl_set_copy(domain));
2601
2.31k
  expansion = isl_ast_build_get_stride_expansion(stride_build);
2602
2.31k
2603
2.31k
  domain = isl_set_preimage_multi_aff(domain,
2604
2.31k
              isl_multi_aff_copy(expansion));
2605
2.31k
  domain = isl_ast_build_eliminate_divs(stride_build, domain);
2606
2.31k
  isl_ast_build_free(stride_build);
2607
2.31k
2608
2.31k
  bmap = isl_basic_map_from_multi_aff(expansion);
2609
2.31k
2610
2.31k
  empty = isl_set_is_empty(domain);
2611
2.31k
  if (empty < 0) {
2612
0
    n = -1;
2613
2.31k
  } else if (empty) {
2614
0
    n = 0;
2615
2.31k
  } else {
2616
2.31k
    lower = find_unroll_lower_bound(build, domain, depth, bmap, &n);
2617
2.31k
    if (!lower)
2618
0
      n = -1;
2619
2.31k
  }
2620
2.31k
  if (n >= 0 && init && 
init(n, user) < 02.31k
)
2621
0
    n = -1;
2622
6.72k
  for (i = 0; i < n; 
++i4.41k
) {
2623
4.41k
    isl_set *set;
2624
4.41k
    isl_basic_set *bset;
2625
4.41k
    isl_constraint *slice;
2626
4.41k
2627
4.41k
    slice = at_offset(depth, lower, i);
2628
4.41k
    set = isl_set_copy(domain);
2629
4.41k
    set = isl_set_add_constraint(set, isl_constraint_copy(slice));
2630
4.41k
    bset = isl_set_unshifted_simple_hull(set);
2631
4.41k
    bset = isl_basic_set_add_constraint(bset, slice);
2632
4.41k
    bset = isl_basic_set_apply(bset, isl_basic_map_copy(bmap));
2633
4.41k
2634
4.41k
    if (fn(bset, user) < 0)
2635
0
      break;
2636
4.41k
  }
2637
2.31k
2638
2.31k
  isl_aff_free(lower);
2639
2.31k
  isl_set_free(domain);
2640
2.31k
  isl_basic_map_free(bmap);
2641
2.31k
2642
2.31k
  return n < 0 || i < n ? 
-10
: 0;
2643
2.31k
}
2644
2645
/* Data structure for storing the results and the intermediate objects
2646
 * of compute_domains.
2647
 *
2648
 * "list" is the main result of the function and contains a list
2649
 * of disjoint basic sets for which code should be generated.
2650
 *
2651
 * "executed" and "build" are inputs to compute_domains.
2652
 * "schedule_domain" is the domain of "executed".
2653
 *
2654
 * "option" constains the domains at the current depth that should by
2655
 * atomic, separated or unrolled.  These domains are as specified by
2656
 * the user, except that inner dimensions have been eliminated and
2657
 * that they have been made pair-wise disjoint.
2658
 *
2659
 * "sep_class" contains the user-specified split into separation classes
2660
 * specialized to the current depth.
2661
 * "done" contains the union of the separation domains that have already
2662
 * been handled.
2663
 */
2664
struct isl_codegen_domains {
2665
  isl_basic_set_list *list;
2666
2667
  isl_union_map *executed;
2668
  isl_ast_build *build;
2669
  isl_set *schedule_domain;
2670
2671
  isl_set *option[4];
2672
2673
  isl_map *sep_class;
2674
  isl_set *done;
2675
};
2676
2677
/* Internal data structure for do_unroll.
2678
 *
2679
 * "domains" stores the results of compute_domains.
2680
 * "class_domain" is the original class domain passed to do_unroll.
2681
 * "unroll_domain" collects the unrolled iterations.
2682
 */
2683
struct isl_ast_unroll_data {
2684
  struct isl_codegen_domains *domains;
2685
  isl_set *class_domain;
2686
  isl_set *unroll_domain;
2687
};
2688
2689
/* Given an iteration of an unrolled domain represented by "bset",
2690
 * add it to data->domains->list.
2691
 * Since we may have dropped some constraints, we intersect with
2692
 * the class domain again to ensure that each element in the list
2693
 * is disjoint from the other class domains.
2694
 */
2695
static int do_unroll_iteration(__isl_take isl_basic_set *bset, void *user)
2696
8
{
2697
8
  struct isl_ast_unroll_data *data = user;
2698
8
  isl_set *set;
2699
8
  isl_basic_set_list *list;
2700
8
2701
8
  set = isl_set_from_basic_set(bset);
2702
8
  data->unroll_domain = isl_set_union(data->unroll_domain,
2703
8
              isl_set_copy(set));
2704
8
  set = isl_set_intersect(set, isl_set_copy(data->class_domain));
2705
8
  set = isl_set_make_disjoint(set);
2706
8
  list = isl_basic_set_list_from_set(set);
2707
8
  data->domains->list = isl_basic_set_list_concat(data->domains->list,
2708
8
              list);
2709
8
2710
8
  return 0;
2711
8
}
2712
2713
/* Extend domains->list with a list of basic sets, one for each value
2714
 * of the current dimension in "domain" and remove the corresponding
2715
 * sets from the class domain.  Return the updated class domain.
2716
 * The divs that involve the current dimension have not been projected out
2717
 * from this domain.
2718
 *
2719
 * We call foreach_iteration to iterate over the individual values and
2720
 * in do_unroll_iteration we collect the individual basic sets in
2721
 * domains->list and their union in data->unroll_domain, which is then
2722
 * used to update the class domain.
2723
 */
2724
static __isl_give isl_set *do_unroll(struct isl_codegen_domains *domains,
2725
  __isl_take isl_set *domain, __isl_take isl_set *class_domain)
2726
3
{
2727
3
  struct isl_ast_unroll_data data;
2728
3
2729
3
  if (!domain)
2730
0
    return isl_set_free(class_domain);
2731
3
  if (!class_domain)
2732
0
    return isl_set_free(domain);
2733
3
2734
3
  data.domains = domains;
2735
3
  data.class_domain = class_domain;
2736
3
  data.unroll_domain = isl_set_empty(isl_set_get_space(domain));
2737
3
2738
3
  if (foreach_iteration(domain, domains->build, NULL,
2739
3
        &do_unroll_iteration, &data) < 0)
2740
0
    data.unroll_domain = isl_set_free(data.unroll_domain);
2741
3
2742
3
  class_domain = isl_set_subtract(class_domain, data.unroll_domain);
2743
3
2744
3
  return class_domain;
2745
3
}
2746
2747
/* Add domains to domains->list for each individual value of the current
2748
 * dimension, for that part of the schedule domain that lies in the
2749
 * intersection of the option domain and the class domain.
2750
 * Remove the corresponding sets from the class domain and
2751
 * return the updated class domain.
2752
 *
2753
 * We first break up the unroll option domain into individual pieces
2754
 * and then handle each of them separately.  The unroll option domain
2755
 * has been made disjoint in compute_domains_init_options,
2756
 *
2757
 * Note that we actively want to combine different pieces of the
2758
 * schedule domain that have the same value at the current dimension.
2759
 * We therefore need to break up the unroll option domain before
2760
 * intersecting with class and schedule domain, hoping that the
2761
 * unroll option domain specified by the user is relatively simple.
2762
 */
2763
static __isl_give isl_set *compute_unroll_domains(
2764
  struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2765
84
{
2766
84
  isl_set *unroll_domain;
2767
84
  isl_basic_set_list *unroll_list;
2768
84
  int i, n;
2769
84
  int empty;
2770
84
2771
84
  empty = isl_set_is_empty(domains->option[isl_ast_loop_unroll]);
2772
84
  if (empty < 0)
2773
0
    return isl_set_free(class_domain);
2774
84
  if (empty)
2775
81
    return class_domain;
2776
3
2777
3
  unroll_domain = isl_set_copy(domains->option[isl_ast_loop_unroll]);
2778
3
  unroll_list = isl_basic_set_list_from_set(unroll_domain);
2779
3
2780
3
  n = isl_basic_set_list_n_basic_set(unroll_list);
2781
6
  for (i = 0; i < n; 
++i3
) {
2782
3
    isl_basic_set *bset;
2783
3
2784
3
    bset = isl_basic_set_list_get_basic_set(unroll_list, i);
2785
3
    unroll_domain = isl_set_from_basic_set(bset);
2786
3
    unroll_domain = isl_set_intersect(unroll_domain,
2787
3
                isl_set_copy(class_domain));
2788
3
    unroll_domain = isl_set_intersect(unroll_domain,
2789
3
          isl_set_copy(domains->schedule_domain));
2790
3
2791
3
    empty = isl_set_is_empty(unroll_domain);
2792
3
    if (empty >= 0 && empty) {
2793
0
      isl_set_free(unroll_domain);
2794
0
      continue;
2795
0
    }
2796
3
2797
3
    class_domain = do_unroll(domains, unroll_domain, class_domain);
2798
3
  }
2799
3
2800
3
  isl_basic_set_list_free(unroll_list);
2801
3
2802
3
  return class_domain;
2803
3
}
2804
2805
/* Try and construct a single basic set that includes the intersection of
2806
 * the schedule domain, the atomic option domain and the class domain.
2807
 * Add the resulting basic set(s) to domains->list and remove them
2808
 * from class_domain.  Return the updated class domain.
2809
 *
2810
 * We construct a single domain rather than trying to combine
2811
 * the schedule domains of individual domains because we are working
2812
 * within a single component so that non-overlapping schedule domains
2813
 * should already have been separated.
2814
 * We do however need to make sure that this single domains is a subset
2815
 * of the class domain so that it would not intersect with any other
2816
 * class domains.  This means that we may end up splitting up the atomic
2817
 * domain in case separation classes are being used.
2818
 *
2819
 * "domain" is the intersection of the schedule domain and the class domain,
2820
 * with inner dimensions projected out.
2821
 */
2822
static __isl_give isl_set *compute_atomic_domain(
2823
  struct isl_codegen_domains *domains, __isl_take isl_set *class_domain)
2824
84
{
2825
84
  isl_basic_set *bset;
2826
84
  isl_basic_set_list *list;
2827
84
  isl_set *domain, *atomic_domain;
2828
84
  int empty;
2829
84
2830
84
  domain = isl_set_copy(domains->option[isl_ast_loop_atomic]);
2831
84
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2832
84
  domain = isl_set_intersect(domain,
2833
84
        isl_set_copy(domains->schedule_domain));
2834
84
  empty = isl_set_is_empty(domain);
2835
84
  if (empty < 0)
2836
0
    class_domain = isl_set_free(class_domain);
2837
84
  if (empty) {
2838
83
    isl_set_free(domain);
2839
83
    return class_domain;
2840
83
  }
2841
1
2842
1
  domain = isl_ast_build_eliminate(domains->build, domain);
2843
1
  domain = isl_set_coalesce(domain);
2844
1
  bset = isl_set_unshifted_simple_hull(domain);
2845
1
  domain = isl_set_from_basic_set(bset);
2846
1
  atomic_domain = isl_set_copy(domain);
2847
1
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2848
1
  class_domain = isl_set_subtract(class_domain, atomic_domain);
2849
1
  domain = isl_set_make_disjoint(domain);
2850
1
  list = isl_basic_set_list_from_set(domain);
2851
1
  domains->list = isl_basic_set_list_concat(domains->list, list);
2852
1
2853
1
  return class_domain;
2854
1
}
2855
2856
/* Split up the schedule domain into uniform basic sets,
2857
 * in the sense that each element in a basic set is associated to
2858
 * elements of the same domains, and add the result to domains->list.
2859
 * Do this for that part of the schedule domain that lies in the
2860
 * intersection of "class_domain" and the separate option domain.
2861
 *
2862
 * "class_domain" may or may not include the constraints
2863
 * of the schedule domain, but this does not make a difference
2864
 * since we are going to intersect it with the domain of the inverse schedule.
2865
 * If it includes schedule domain constraints, then they may involve
2866
 * inner dimensions, but we will eliminate them in separation_domain.
2867
 */
2868
static int compute_separate_domain(struct isl_codegen_domains *domains,
2869
  __isl_keep isl_set *class_domain)
2870
84
{
2871
84
  isl_space *space;
2872
84
  isl_set *domain;
2873
84
  isl_union_map *executed;
2874
84
  isl_basic_set_list *list;
2875
84
  int empty;
2876
84
2877
84
  domain = isl_set_copy(domains->option[isl_ast_loop_separate]);
2878
84
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2879
84
  executed = isl_union_map_copy(domains->executed);
2880
84
  executed = isl_union_map_intersect_domain(executed,
2881
84
            isl_union_set_from_set(domain));
2882
84
  empty = isl_union_map_is_empty(executed);
2883
84
  if (empty < 0 || empty) {
2884
84
    isl_union_map_free(executed);
2885
84
    return empty < 0 ? 
-10
: 0;
2886
84
  }
2887
0
2888
0
  space = isl_set_get_space(class_domain);
2889
0
  domain = separate_schedule_domains(space, executed, domains->build);
2890
0
2891
0
  list = isl_basic_set_list_from_set(domain);
2892
0
  domains->list = isl_basic_set_list_concat(domains->list, list);
2893
0
2894
0
  return 0;
2895
0
}
2896
2897
/* Split up the domain at the current depth into disjoint
2898
 * basic sets for which code should be generated separately
2899
 * for the given separation class domain.
2900
 *
2901
 * If any separation classes have been defined, then "class_domain"
2902
 * is the domain of the current class and does not refer to inner dimensions.
2903
 * Otherwise, "class_domain" is the universe domain.
2904
 *
2905
 * We first make sure that the class domain is disjoint from
2906
 * previously considered class domains.
2907
 *
2908
 * The separate domains can be computed directly from the "class_domain".
2909
 *
2910
 * The unroll, atomic and remainder domains need the constraints
2911
 * from the schedule domain.
2912
 *
2913
 * For unrolling, the actual schedule domain is needed (with divs that
2914
 * may refer to the current dimension) so that stride detection can be
2915
 * performed.
2916
 *
2917
 * For atomic and remainder domains, inner dimensions and divs involving
2918
 * the current dimensions should be eliminated.
2919
 * In case we are working within a separation class, we need to intersect
2920
 * the result with the current "class_domain" to ensure that the domains
2921
 * are disjoint from those generated from other class domains.
2922
 *
2923
 * The domain that has been made atomic may be larger than specified
2924
 * by the user since it needs to be representable as a single basic set.
2925
 * This possibly larger domain is removed from class_domain by
2926
 * compute_atomic_domain.  It is computed first so that the extended domain
2927
 * would not overlap with any domains computed before.
2928
 * Similary, the unrolled domains may have some constraints removed and
2929
 * may therefore also be larger than specified by the user.
2930
 *
2931
 * If anything is left after handling separate, unroll and atomic,
2932
 * we split it up into basic sets and append the basic sets to domains->list.
2933
 */
2934
static isl_stat compute_partial_domains(struct isl_codegen_domains *domains,
2935
  __isl_take isl_set *class_domain)
2936
84
{
2937
84
  isl_basic_set_list *list;
2938
84
  isl_set *domain;
2939
84
2940
84
  class_domain = isl_set_subtract(class_domain,
2941
84
          isl_set_copy(domains->done));
2942
84
  domains->done = isl_set_union(domains->done,
2943
84
          isl_set_copy(class_domain));
2944
84
2945
84
  class_domain = compute_atomic_domain(domains, class_domain);
2946
84
  class_domain = compute_unroll_domains(domains, class_domain);
2947
84
2948
84
  domain = isl_set_copy(class_domain);
2949
84
2950
84
  if (compute_separate_domain(domains, domain) < 0)
2951
0
    goto error;
2952
84
  domain = isl_set_subtract(domain,
2953
84
      isl_set_copy(domains->option[isl_ast_loop_separate]));
2954
84
2955
84
  domain = isl_set_intersect(domain,
2956
84
        isl_set_copy(domains->schedule_domain));
2957
84
2958
84
  domain = isl_ast_build_eliminate(domains->build, domain);
2959
84
  domain = isl_set_intersect(domain, isl_set_copy(class_domain));
2960
84
2961
84
  domain = isl_set_coalesce(domain);
2962
84
  domain = isl_set_make_disjoint(domain);
2963
84
2964
84
  list = isl_basic_set_list_from_set(domain);
2965
84
  domains->list = isl_basic_set_list_concat(domains->list, list);
2966
84
2967
84
  isl_set_free(class_domain);
2968
84
2969
84
  return isl_stat_ok;
2970
0
error:
2971
0
  isl_set_free(domain);
2972
0
  isl_set_free(class_domain);
2973
0
  return isl_stat_error;
2974
84
}
2975
2976
/* Split up the domain at the current depth into disjoint
2977
 * basic sets for which code should be generated separately
2978
 * for the separation class identified by "pnt".
2979
 *
2980
 * We extract the corresponding class domain from domains->sep_class,
2981
 * eliminate inner dimensions and pass control to compute_partial_domains.
2982
 */
2983
static isl_stat compute_class_domains(__isl_take isl_point *pnt, void *user)
2984
0
{
2985
0
  struct isl_codegen_domains *domains = user;
2986
0
  isl_set *class_set;
2987
0
  isl_set *domain;
2988
0
  int disjoint;
2989
0
2990
0
  class_set = isl_set_from_point(pnt);
2991
0
  domain = isl_map_domain(isl_map_intersect_range(
2992
0
        isl_map_copy(domains->sep_class), class_set));
2993
0
  domain = isl_ast_build_compute_gist(domains->build, domain);
2994
0
  domain = isl_ast_build_eliminate(domains->build, domain);
2995
0
2996
0
  disjoint = isl_set_plain_is_disjoint(domain, domains->schedule_domain);
2997
0
  if (disjoint < 0)
2998
0
    return isl_stat_error;
2999
0
  if (disjoint) {
3000
0
    isl_set_free(domain);
3001
0
    return isl_stat_ok;
3002
0
  }
3003
0
3004
0
  return compute_partial_domains(domains, domain);
3005
0
}
3006
3007
/* Extract the domains at the current depth that should be atomic,
3008
 * separated or unrolled and store them in option.
3009
 *
3010
 * The domains specified by the user might overlap, so we make
3011
 * them disjoint by subtracting earlier domains from later domains.
3012
 */
3013
static void compute_domains_init_options(isl_set *option[4],
3014
  __isl_keep isl_ast_build *build)
3015
84
{
3016
84
  enum isl_ast_loop_type type, type2;
3017
84
  isl_set *unroll;
3018
84
3019
84
  for (type = isl_ast_loop_atomic;
3020
336
      type <= isl_ast_loop_separate; 
++type252
) {
3021
252
    option[type] = isl_ast_build_get_option_domain(build, type);
3022
504
    for (type2 = isl_ast_loop_atomic; type2 < type; 
++type2252
)
3023
252
      option[type] = isl_set_subtract(option[type],
3024
252
            isl_set_copy(option[type2]));
3025
252
  }
3026
84
3027
84
  unroll = option[isl_ast_loop_unroll];
3028
84
  unroll = isl_set_coalesce(unroll);
3029
84
  unroll = isl_set_make_disjoint(unroll);
3030
84
  option[isl_ast_loop_unroll] = unroll;
3031
84
}
3032
3033
/* Split up the domain at the current depth into disjoint
3034
 * basic sets for which code should be generated separately,
3035
 * based on the user-specified options.
3036
 * Return the list of disjoint basic sets.
3037
 *
3038
 * There are three kinds of domains that we need to keep track of.
3039
 * - the "schedule domain" is the domain of "executed"
3040
 * - the "class domain" is the domain corresponding to the currrent
3041
 *  separation class
3042
 * - the "option domain" is the domain corresponding to one of the options
3043
 *  atomic, unroll or separate
3044
 *
3045
 * We first consider the individial values of the separation classes
3046
 * and split up the domain for each of them separately.
3047
 * Finally, we consider the remainder.  If no separation classes were
3048
 * specified, then we call compute_partial_domains with the universe
3049
 * "class_domain".  Otherwise, we take the "schedule_domain" as "class_domain",
3050
 * with inner dimensions removed.  We do this because we want to
3051
 * avoid computing the complement of the class domains (i.e., the difference
3052
 * between the universe and domains->done).
3053
 */
3054
static __isl_give isl_basic_set_list *compute_domains(
3055
  __isl_keep isl_union_map *executed, __isl_keep isl_ast_build *build)
3056
84
{
3057
84
  struct isl_codegen_domains domains;
3058
84
  isl_ctx *ctx;
3059
84
  isl_set *domain;
3060
84
  isl_union_set *schedule_domain;
3061
84
  isl_set *classes;
3062
84
  isl_space *space;
3063
84
  int n_param;
3064
84
  enum isl_ast_loop_type type;
3065
84
  int empty;
3066
84
3067
84
  if (!executed)
3068
0
    return NULL;
3069
84
3070
84
  ctx = isl_union_map_get_ctx(executed);
3071
84
  domains.list = isl_basic_set_list_alloc(ctx, 0);
3072
84
3073
84
  schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3074
84
  domain = isl_set_from_union_set(schedule_domain);
3075
84
3076
84
  compute_domains_init_options(domains.option, build);
3077
84
3078
84
  domains.sep_class = isl_ast_build_get_separation_class(build);
3079
84
  classes = isl_map_range(isl_map_copy(domains.sep_class));
3080
84
  n_param = isl_set_dim(classes, isl_dim_param);
3081
84
  classes = isl_set_project_out(classes, isl_dim_param, 0, n_param);
3082
84
3083
84
  space = isl_set_get_space(domain);
3084
84
  domains.build = build;
3085
84
  domains.schedule_domain = isl_set_copy(domain);
3086
84
  domains.executed = executed;
3087
84
  domains.done = isl_set_empty(space);
3088
84
3089
84
  if (isl_set_foreach_point(classes, &compute_class_domains, &domains) < 0)
3090
0
    domains.list = isl_basic_set_list_free(domains.list);
3091
84
  isl_set_free(classes);
3092
84
3093
84
  empty = isl_set_is_empty(domains.done);
3094
84
  if (empty < 0) {
3095
0
    domains.list = isl_basic_set_list_free(domains.list);
3096
0
    domain = isl_set_free(domain);
3097
84
  } else if (empty) {
3098
84
    isl_set_free(domain);
3099
84
    domain = isl_set_universe(isl_set_get_space(domains.done));
3100
84
  } else {
3101
0
    domain = isl_ast_build_eliminate(build, domain);
3102
0
  }
3103
84
  if (compute_partial_domains(&domains, domain) < 0)
3104
0
    domains.list = isl_basic_set_list_free(domains.list);
3105
84
3106
84
  isl_set_free(domains.schedule_domain);
3107
84
  isl_set_free(domains.done);
3108
84
  isl_map_free(domains.sep_class);
3109
336
  for (type = isl_ast_loop_atomic; type <= isl_ast_loop_separate; 
++type252
)
3110
252
    isl_set_free(domains.option[type]);
3111
84
3112
84
  return domains.list;
3113
84
}
3114
3115
/* Generate code for a single component, after shifting (if any)
3116
 * has been applied, in case the schedule was specified as a union map.
3117
 *
3118
 * We first split up the domain at the current depth into disjoint
3119
 * basic sets based on the user-specified options.
3120
 * Then we generated code for each of them and concatenate the results.
3121
 */
3122
static __isl_give isl_ast_graft_list *generate_shifted_component_flat(
3123
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3124
84
{
3125
84
  isl_basic_set_list *domain_list;
3126
84
  isl_ast_graft_list *list = NULL;
3127
84
3128
84
  domain_list = compute_domains(executed, build);
3129
84
  list = generate_parallel_domains(domain_list, executed, build);
3130
84
3131
84
  isl_basic_set_list_free(domain_list);
3132
84
  isl_union_map_free(executed);
3133
84
  isl_ast_build_free(build);
3134
84
3135
84
  return list;
3136
84
}
3137
3138
/* Generate code for a single component, after shifting (if any)
3139
 * has been applied, in case the schedule was specified as a schedule tree
3140
 * and the separate option was specified.
3141
 *
3142
 * We perform separation on the domain of "executed" and then generate
3143
 * an AST for each of the resulting disjoint basic sets.
3144
 */
3145
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_separate(
3146
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3147
24
{
3148
24
  isl_space *space;
3149
24
  isl_set *domain;
3150
24
  isl_basic_set_list *domain_list;
3151
24
  isl_ast_graft_list *list;
3152
24
3153
24
  space = isl_ast_build_get_space(build, 1);
3154
24
  domain = separate_schedule_domains(space,
3155
24
          isl_union_map_copy(executed), build);
3156
24
  domain_list = isl_basic_set_list_from_set(domain);
3157
24
3158
24
  list = generate_parallel_domains(domain_list, executed, build);
3159
24
3160
24
  isl_basic_set_list_free(domain_list);
3161
24
  isl_union_map_free(executed);
3162
24
  isl_ast_build_free(build);
3163
24
3164
24
  return list;
3165
24
}
3166
3167
/* Internal data structure for generate_shifted_component_tree_unroll.
3168
 *
3169
 * "executed" and "build" are inputs to generate_shifted_component_tree_unroll.
3170
 * "list" collects the constructs grafts.
3171
 */
3172
struct isl_ast_unroll_tree_data {
3173
  isl_union_map *executed;
3174
  isl_ast_build *build;
3175
  isl_ast_graft_list *list;
3176
};
3177
3178
/* Initialize data->list to a list of "n" elements.
3179
 */
3180
static int init_unroll_tree(int n, void *user)
3181
2.31k
{
3182
2.31k
  struct isl_ast_unroll_tree_data *data = user;
3183
2.31k
  isl_ctx *ctx;
3184
2.31k
3185
2.31k
  ctx = isl_ast_build_get_ctx(data->build);
3186
2.31k
  data->list = isl_ast_graft_list_alloc(ctx, n);
3187
2.31k
3188
2.31k
  return 0;
3189
2.31k
}
3190
3191
/* Given an iteration of an unrolled domain represented by "bset",
3192
 * generate the corresponding AST and add the result to data->list.
3193
 */
3194
static int do_unroll_tree_iteration(__isl_take isl_basic_set *bset, void *user)
3195
4.40k
{
3196
4.40k
  struct isl_ast_unroll_tree_data *data = user;
3197
4.40k
3198
4.40k
  data->list = add_node(data->list, isl_union_map_copy(data->executed),
3199
4.40k
        bset, isl_ast_build_copy(data->build));
3200
4.40k
3201
4.40k
  return 0;
3202
4.40k
}
3203
3204
/* Generate code for a single component, after shifting (if any)
3205
 * has been applied, in case the schedule was specified as a schedule tree
3206
 * and the unroll option was specified.
3207
 *
3208
 * We call foreach_iteration to iterate over the individual values and
3209
 * construct and collect the corresponding grafts in do_unroll_tree_iteration.
3210
 */
3211
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_unroll(
3212
  __isl_take isl_union_map *executed, __isl_take isl_set *domain,
3213
  __isl_take isl_ast_build *build)
3214
2.31k
{
3215
2.31k
  struct isl_ast_unroll_tree_data data = { executed, build, NULL };
3216
2.31k
3217
2.31k
  if (foreach_iteration(domain, build, &init_unroll_tree,
3218
2.31k
        &do_unroll_tree_iteration, &data) < 0)
3219
0
    data.list = isl_ast_graft_list_free(data.list);
3220
2.31k
3221
2.31k
  isl_union_map_free(executed);
3222
2.31k
  isl_ast_build_free(build);
3223
2.31k
3224
2.31k
  return data.list;
3225
2.31k
}
3226
3227
/* Does "domain" involve a disjunction that is purely based on
3228
 * constraints involving only outer dimension?
3229
 *
3230
 * In particular, is there a disjunction such that the constraints
3231
 * involving the current and later dimensions are the same over
3232
 * all the disjuncts?
3233
 */
3234
static isl_bool has_pure_outer_disjunction(__isl_keep isl_set *domain,
3235
  __isl_keep isl_ast_build *build)
3236
794
{
3237
794
  isl_basic_set *hull;
3238
794
  isl_set *shared, *inner;
3239
794
  isl_bool equal;
3240
794
  int depth, dim;
3241
794
3242
794
  if (isl_set_n_basic_set(domain) <= 1)
3243
750
    return isl_bool_false;
3244
44
3245
44
  inner = isl_set_copy(domain);
3246
44
  depth = isl_ast_build_get_depth(build);
3247
44
  dim = isl_set_dim(inner, isl_dim_set);
3248
44
  inner = isl_set_drop_constraints_not_involving_dims(inner,
3249
44
              isl_dim_set, depth, dim - depth);
3250
44
  hull = isl_set_plain_unshifted_simple_hull(isl_set_copy(inner));
3251
44
  shared = isl_set_from_basic_set(hull);
3252
44
  equal = isl_set_plain_is_equal(inner, shared);
3253
44
  isl_set_free(inner);
3254
44
  isl_set_free(shared);
3255
44
3256
44
  return equal;
3257
44
}
3258
3259
/* Generate code for a single component, after shifting (if any)
3260
 * has been applied, in case the schedule was specified as a schedule tree.
3261
 * In particular, handle the base case where there is either no isolated
3262
 * set or we are within the isolated set (in which case "isolated" is set)
3263
 * or the iterations that precede or follow the isolated set.
3264
 *
3265
 * The schedule domain is broken up or combined into basic sets
3266
 * according to the AST generation option specified in the current
3267
 * schedule node, which may be either atomic, separate, unroll or
3268
 * unspecified.  If the option is unspecified, then we currently simply
3269
 * split the schedule domain into disjoint basic sets.
3270
 *
3271
 * In case the separate option is specified, the AST generation is
3272
 * handled by generate_shifted_component_tree_separate.
3273
 * In the other cases, we need the global schedule domain.
3274
 * In the unroll case, the AST generation is then handled by
3275
 * generate_shifted_component_tree_unroll which needs the actual
3276
 * schedule domain (with divs that may refer to the current dimension)
3277
 * so that stride detection can be performed.
3278
 * In the atomic or unspecified case, inner dimensions and divs involving
3279
 * the current dimensions should be eliminated.
3280
 * The result is then either combined into a single basic set or
3281
 * split up into disjoint basic sets.
3282
 * Finally an AST is generated for each basic set and the results are
3283
 * concatenated.
3284
 *
3285
 * If the schedule domain involves a disjunction that is purely based on
3286
 * constraints involving only outer dimension, then it is treated as
3287
 * if atomic was specified.  This ensures that only a single loop
3288
 * is generated instead of a sequence of identical loops with
3289
 * different guards.
3290
 */
3291
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_base(
3292
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
3293
  int isolated)
3294
3.12k
{
3295
3.12k
  isl_bool outer_disjunction;
3296
3.12k
  isl_union_set *schedule_domain;
3297
3.12k
  isl_set *domain;
3298
3.12k
  isl_basic_set_list *domain_list;
3299
3.12k
  isl_ast_graft_list *list;
3300
3.12k
  enum isl_ast_loop_type type;
3301
3.12k
3302
3.12k
  type = isl_ast_build_get_loop_type(build, isolated);
3303
3.12k
  if (type < 0)
3304
0
    goto error;
3305
3.12k
3306
3.12k
  if (type == isl_ast_loop_separate)
3307
24
    return generate_shifted_component_tree_separate(executed,
3308
24
                build);
3309
3.10k
3310
3.10k
  schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3311
3.10k
  domain = isl_set_from_union_set(schedule_domain);
3312
3.10k
3313
3.10k
  if (type == isl_ast_loop_unroll)
3314
2.31k
    return generate_shifted_component_tree_unroll(executed, domain,
3315
2.31k
                build);
3316
794
3317
794
  domain = isl_ast_build_eliminate(build, domain);
3318
794
  domain = isl_set_coalesce(domain);
3319
794
3320
794
  outer_disjunction = has_pure_outer_disjunction(domain, build);
3321
794
  if (outer_disjunction < 0)
3322
0
    domain = isl_set_free(domain);
3323
794
3324
794
  if (outer_disjunction || 
type == isl_ast_loop_atomic779
) {
3325
16
    isl_basic_set *hull;
3326
16
    hull = isl_set_unshifted_simple_hull(domain);
3327
16
    domain_list = isl_basic_set_list_from_basic_set(hull);
3328
778
  } else {
3329
778
    domain = isl_set_make_disjoint(domain);
3330
778
    domain_list = isl_basic_set_list_from_set(domain);
3331
778
  }
3332
794
3333
794
  list = generate_parallel_domains(domain_list, executed, build);
3334
794
3335
794
  isl_basic_set_list_free(domain_list);
3336
794
  isl_union_map_free(executed);
3337
794
  isl_ast_build_free(build);
3338
794
3339
794
  return list;
3340
0
error:
3341
0
  isl_union_map_free(executed);
3342
0
  isl_ast_build_free(build);
3343
0
  return NULL;
3344
794
}
3345
3346
/* Extract out the disjunction imposed by "domain" on the outer
3347
 * schedule dimensions.
3348
 *
3349
 * In particular, remove all inner dimensions from "domain" (including
3350
 * the current dimension) and then remove the constraints that are shared
3351
 * by all disjuncts in the result.
3352
 */
3353
static __isl_give isl_set *extract_disjunction(__isl_take isl_set *domain,
3354
  __isl_keep isl_ast_build *build)
3355
796
{
3356
796
  isl_set *hull;
3357
796
  int depth, dim;
3358
796
3359
796
  domain = isl_ast_build_specialize(build, domain);
3360
796
  depth = isl_ast_build_get_depth(build);
3361
796
  dim = isl_set_dim(domain, isl_dim_set);
3362
796
  domain = isl_set_eliminate(domain, isl_dim_set, depth, dim - depth);
3363
796
  domain = isl_set_remove_unknown_divs(domain);
3364
796
  hull = isl_set_copy(domain);
3365
796
  hull = isl_set_from_basic_set(isl_set_unshifted_simple_hull(hull));
3366
796
  domain = isl_set_gist(domain, hull);
3367
796
3368
796
  return domain;
3369
796
}
3370
3371
/* Add "guard" to the grafts in "list".
3372
 * "build" is the outer AST build, while "sub_build" includes "guard"
3373
 * in its generated domain.
3374
 *
3375
 * First combine the grafts into a single graft and then add the guard.
3376
 * If the list is empty, or if some error occurred, then simply return
3377
 * the list.
3378
 */
3379
static __isl_give isl_ast_graft_list *list_add_guard(
3380
  __isl_take isl_ast_graft_list *list, __isl_keep isl_set *guard,
3381
  __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
3382
796
{
3383
796
  isl_ast_graft *graft;
3384
796
3385
796
  list = isl_ast_graft_list_fuse(list, sub_build);
3386
796
3387
796
  if (isl_ast_graft_list_n_ast_graft(list) != 1)
3388
789
    return list;
3389
7
3390
7
  graft = isl_ast_graft_list_get_ast_graft(list, 0);
3391
7
  graft = isl_ast_graft_add_guard(graft, isl_set_copy(guard), build);
3392
7
  list = isl_ast_graft_list_set_ast_graft(list, 0, graft);
3393
7
3394
7
  return list;
3395
7
}
3396
3397
/* Generate code for a single component, after shifting (if any)
3398
 * has been applied, in case the schedule was specified as a schedule tree.
3399
 * In particular, do so for the specified subset of the schedule domain.
3400
 *
3401
 * If we are outside of the isolated part, then "domain" may include
3402
 * a disjunction.  Explicitly generate this disjunction at this point
3403
 * instead of relying on the disjunction getting hoisted back up
3404
 * to this level.
3405
 */
3406
static __isl_give isl_ast_graft_list *generate_shifted_component_tree_part(
3407
  __isl_keep isl_union_map *executed, __isl_take isl_set *domain,
3408
  __isl_keep isl_ast_build *build, int isolated)
3409
3.65k
{
3410
3.65k
  isl_union_set *uset;
3411
3.65k
  isl_ast_graft_list *list;
3412
3.65k
  isl_ast_build *sub_build;
3413
3.65k
  int empty;
3414
3.65k
3415
3.65k
  uset = isl_union_set_from_set(isl_set_copy(domain));
3416
3.65k
  executed = isl_union_map_copy(executed);
3417
3.65k
  executed = isl_union_map_intersect_domain(executed, uset);
3418
3.65k
  empty = isl_union_map_is_empty(executed);
3419
3.65k
  if (empty < 0)
3420
0
    goto error;
3421
3.65k
  if (empty) {
3422
1.94k
    isl_ctx *ctx;
3423
1.94k
    isl_union_map_free(executed);
3424
1.94k
    isl_set_free(domain);
3425
1.94k
    ctx = isl_ast_build_get_ctx(build);
3426
1.94k
    return isl_ast_graft_list_alloc(ctx, 0);
3427
1.94k
  }
3428
1.70k
3429
1.70k
  sub_build = isl_ast_build_copy(build);
3430
1.70k
  if (!isolated) {
3431
796
    domain = extract_disjunction(domain, build);
3432
796
    sub_build = isl_ast_build_restrict_generated(sub_build,
3433
796
              isl_set_copy(domain));
3434
796
  }
3435
1.70k
  list = generate_shifted_component_tree_base(executed,
3436
1.70k
        isl_ast_build_copy(sub_build), isolated);
3437
1.70k
  if (!isolated)
3438
796
    list = list_add_guard(list, domain, build, sub_build);
3439
1.70k
  isl_ast_build_free(sub_build);
3440
1.70k
  isl_set_free(domain);
3441
1.70k
  return list;
3442
0
error:
3443
0
  isl_union_map_free(executed);
3444
0
  isl_set_free(domain);
3445
0
  return NULL;
3446
1.70k
}
3447
3448
/* Generate code for a single component, after shifting (if any)
3449
 * has been applied, in case the schedule was specified as a schedule tree.
3450
 * In particular, do so for the specified sequence of subsets
3451
 * of the schedule domain, "before", "isolated", "after" and "other",
3452
 * where only the "isolated" part is considered to be isolated.
3453
 */
3454
static __isl_give isl_ast_graft_list *generate_shifted_component_parts(
3455
  __isl_take isl_union_map *executed, __isl_take isl_set *before,
3456
  __isl_take isl_set *isolated, __isl_take isl_set *after,
3457
  __isl_take isl_set *other, __isl_take isl_ast_build *build)
3458
913
{
3459
913
  isl_ast_graft_list *list, *res;
3460
913
3461
913
  res = generate_shifted_component_tree_part(executed, before, build, 0);
3462
913
  list = generate_shifted_component_tree_part(executed, isolated,
3463
913
                build, 1);
3464
913
  res = isl_ast_graft_list_concat(res, list);
3465
913
  list = generate_shifted_component_tree_part(executed, after, build, 0);
3466
913
  res = isl_ast_graft_list_concat(res, list);
3467
913
  list = generate_shifted_component_tree_part(executed, other, build, 0);
3468
913
  res = isl_ast_graft_list_concat(res, list);
3469
913
3470
913
  isl_union_map_free(executed);
3471
913
  isl_ast_build_free(build);
3472
913
3473
913
  return res;
3474
913
}
3475
3476
/* Does "set" intersect "first", but not "second"?
3477
 */
3478
static isl_bool only_intersects_first(__isl_keep isl_set *set,
3479
  __isl_keep isl_set *first, __isl_keep isl_set *second)
3480
913
{
3481
913
  isl_bool disjoint;
3482
913
3483
913
  disjoint = isl_set_is_disjoint(set, first);
3484
913
  if (disjoint < 0)
3485
0
    return isl_bool_error;
3486
913
  if (disjoint)
3487
906
    return isl_bool_false;
3488
7
3489
7
  return isl_set_is_disjoint(set, second);
3490
7
}
3491
3492
/* Generate code for a single component, after shifting (if any)
3493
 * has been applied, in case the schedule was specified as a schedule tree.
3494
 * In particular, do so in case of isolation where there is
3495
 * only an "isolated" part and an "after" part.
3496
 * "dead1" and "dead2" are freed by this function in order to simplify
3497
 * the caller.
3498
 *
3499
 * The "before" and "other" parts are set to empty sets.
3500
 */
3501
static __isl_give isl_ast_graft_list *generate_shifted_component_only_after(
3502
  __isl_take isl_union_map *executed, __isl_take isl_set *isolated,
3503
  __isl_take isl_set *after, __isl_take isl_ast_build *build,
3504
  __isl_take isl_set *dead1, __isl_take isl_set *dead2)
3505
7
{
3506
7
  isl_set *empty;
3507
7
3508
7
  empty = isl_set_empty(isl_set_get_space(after));
3509
7
  isl_set_free(dead1);
3510
7
  isl_set_free(dead2);
3511
7
  return generate_shifted_component_parts(executed, isl_set_copy(empty),
3512
7
            isolated, after, empty, build);
3513
7
}
3514
3515
/* Generate code for a single component, after shifting (if any)
3516
 * has been applied, in case the schedule was specified as a schedule tree.
3517
 *
3518
 * We first check if the user has specified an isolated schedule domain
3519
 * and that we are not already outside of this isolated schedule domain.
3520
 * If so, we break up the schedule domain into iterations that
3521
 * precede the isolated domain, the isolated domain itself,
3522
 * the iterations that follow the isolated domain and
3523
 * the remaining iterations (those that are incomparable
3524
 * to the isolated domain).
3525
 * We generate an AST for each piece and concatenate the results.
3526
 *
3527
 * If the isolated domain is not convex, then it is replaced
3528
 * by a convex superset to ensure that the sets of preceding and
3529
 * following iterations are properly defined and, in particular,
3530
 * that there are no intermediate iterations that do not belong
3531
 * to the isolated domain.
3532
 *
3533
 * In the special case where at least one element of the schedule
3534
 * domain that does not belong to the isolated domain needs
3535
 * to be scheduled after this isolated domain, but none of those
3536
 * elements need to be scheduled before, break up the schedule domain
3537
 * in only two parts, the isolated domain, and a part that will be
3538
 * scheduled after the isolated domain.
3539
 *
3540
 * If no isolated set has been specified, then we generate an
3541
 * AST for the entire inverse schedule.
3542
 */
3543
static __isl_give isl_ast_graft_list *generate_shifted_component_tree(
3544
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3545
2.33k
{
3546
2.33k
  int i, depth;
3547
2.33k
  int empty, has_isolate;
3548
2.33k
  isl_space *space;
3549
2.33k
  isl_union_set *schedule_domain;
3550
2.33k
  isl_set *domain;
3551
2.33k
  isl_basic_set *hull;
3552
2.33k
  isl_set *isolated, *before, *after, *test;
3553
2.33k
  isl_map *gt, *lt;
3554
2.33k
  isl_bool pure;
3555
2.33k
3556
2.33k
  build = isl_ast_build_extract_isolated(build);
3557
2.33k
  has_isolate = isl_ast_build_has_isolated(build);
3558
2.33k
  if (has_isolate < 0)
3559
0
    executed = isl_union_map_free(executed);
3560
2.33k
  else if (!has_isolate)
3561
915
    return generate_shifted_component_tree_base(executed, build, 0);
3562
1.41k
3563
1.41k
  schedule_domain = isl_union_map_domain(isl_union_map_copy(executed));
3564
1.41k
  domain = isl_set_from_union_set(schedule_domain);
3565
1.41k
3566
1.41k
  isolated = isl_ast_build_get_isolated(build);
3567
1.41k
  isolated = isl_set_intersect(isolated, isl_set_copy(domain));
3568
1.41k
  test = isl_ast_build_specialize(build, isl_set_copy(isolated));
3569
1.41k
  empty = isl_set_is_empty(test);
3570
1.41k
  isl_set_free(test);
3571
1.41k
  if (empty < 0)
3572
0
    goto error;
3573
1.41k
  if (empty) {
3574
504
    isl_set_free(isolated);
3575
504
    isl_set_free(domain);
3576
504
    return generate_shifted_component_tree_base(executed, build, 0);
3577
504
  }
3578
913
  isolated = isl_ast_build_eliminate(build, isolated);
3579
913
  hull = isl_set_unshifted_simple_hull(isolated);
3580
913
  isolated = isl_set_from_basic_set(hull);
3581
913
3582
913
  depth = isl_ast_build_get_depth(build);
3583
913
  space = isl_space_map_from_set(isl_set_get_space(isolated));
3584
913
  gt = isl_map_universe(space);
3585
7.95k
  for (i = 0; i < depth; 
++i7.03k
)
3586
7.03k
    gt = isl_map_equate(gt, isl_dim_in, i, isl_dim_out, i);
3587
913
  gt = isl_map_order_gt(gt, isl_dim_in, depth, isl_dim_out, depth);
3588
913
  lt = isl_map_reverse(isl_map_copy(gt));
3589
913
  before = isl_set_apply(isl_set_copy(isolated), gt);
3590
913
  after = isl_set_apply(isl_set_copy(isolated), lt);
3591
913
3592
913
  domain = isl_set_subtract(domain, isl_set_copy(isolated));
3593
913
  pure = only_intersects_first(domain, after, before);
3594
913
  if (pure < 0)
3595
0
    executed = isl_union_map_free(executed);
3596
913
  else if (pure)
3597
7
    return generate_shifted_component_only_after(executed, isolated,
3598
7
            domain, build, before, after);
3599
906
  domain = isl_set_subtract(domain, isl_set_copy(before));
3600
906
  domain = isl_set_subtract(domain, isl_set_copy(after));
3601
906
  after = isl_set_subtract(after, isl_set_copy(isolated));
3602
906
  after = isl_set_subtract(after, isl_set_copy(before));
3603
906
  before = isl_set_subtract(before, isl_set_copy(isolated));
3604
906
3605
906
  return generate_shifted_component_parts(executed, before, isolated,
3606
906
            after, domain, build);
3607
0
error:
3608
0
  isl_set_free(domain);
3609
0
  isl_set_free(isolated);
3610
0
  isl_union_map_free(executed);
3611
0
  isl_ast_build_free(build);
3612
0
  return NULL;
3613
906
}
3614
3615
/* Generate code for a single component, after shifting (if any)
3616
 * has been applied.
3617
 *
3618
 * Call generate_shifted_component_tree or generate_shifted_component_flat
3619
 * depending on whether the schedule was specified as a schedule tree.
3620
 */
3621
static __isl_give isl_ast_graft_list *generate_shifted_component(
3622
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
3623
2.41k
{
3624
2.41k
  if (isl_ast_build_has_schedule_node(build))
3625
2.33k
    return generate_shifted_component_tree(executed, build);
3626
84
  else
3627
84
    return generate_shifted_component_flat(executed, build);
3628
2.41k
}
3629
3630
struct isl_set_map_pair {
3631
  isl_set *set;
3632
  isl_map *map;
3633
};
3634
3635
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3636
 * of indices into the "domain" array,
3637
 * return the union of the "map" fields of the elements
3638
 * indexed by the first "n" elements of "order".
3639
 */
3640
static __isl_give isl_union_map *construct_component_executed(
3641
  struct isl_set_map_pair *domain, int *order, int n)
3642
157
{
3643
157
  int i;
3644
157
  isl_map *map;
3645
157
  isl_union_map *executed;
3646
157
3647
157
  map = isl_map_copy(domain[order[0]].map);
3648
157
  executed = isl_union_map_from_map(map);
3649
299
  for (i = 1; i < n; 
++i142
) {
3650
142
    map = isl_map_copy(domain[order[i]].map);
3651
142
    executed = isl_union_map_add_map(executed, map);
3652
142
  }
3653
157
3654
157
  return executed;
3655
157
}
3656
3657
/* Generate code for a single component, after shifting (if any)
3658
 * has been applied.
3659
 *
3660
 * The component inverse schedule is specified as the "map" fields
3661
 * of the elements of "domain" indexed by the first "n" elements of "order".
3662
 */
3663
static __isl_give isl_ast_graft_list *generate_shifted_component_from_list(
3664
  struct isl_set_map_pair *domain, int *order, int n,
3665
  __isl_take isl_ast_build *build)
3666
157
{
3667
157
  isl_union_map *executed;
3668
157
3669
157
  executed = construct_component_executed(domain, order, n);
3670
157
  return generate_shifted_component(executed, build);
3671
157
}
3672
3673
/* Does set dimension "pos" of "set" have an obviously fixed value?
3674
 */
3675
static int dim_is_fixed(__isl_keep isl_set *set, int pos)
3676
392
{
3677
392
  int fixed;
3678
392
  isl_val *v;
3679
392
3680
392
  v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, pos);
3681
392
  if (!v)
3682
0
    return -1;
3683
392
  fixed = !isl_val_is_nan(v);
3684
392
  isl_val_free(v);
3685
392
3686
392
  return fixed;
3687
392
}
3688
3689
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3690
 * of indices into the "domain" array,
3691
 * do all (except for at most one) of the "set" field of the elements
3692
 * indexed by the first "n" elements of "order" have a fixed value
3693
 * at position "depth"?
3694
 */
3695
static int at_most_one_non_fixed(struct isl_set_map_pair *domain,
3696
  int *order, int n, int depth)
3697
82
{
3698
82
  int i;
3699
82
  int non_fixed = -1;
3700
82
3701
177
  for (i = 0; i < n; 
++i95
) {
3702
169
    int f;
3703
169
3704
169
    f = dim_is_fixed(domain[order[i]].set, depth);
3705
169
    if (f < 0)
3706
0
      return -1;
3707
169
    if (f)
3708
16
      continue;
3709
153
    if (non_fixed >= 0)
3710
74
      return 0;
3711
79
    non_fixed = i;
3712
79
  }
3713
82
3714
82
  
return 18
;
3715
82
}
3716
3717
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3718
 * of indices into the "domain" array,
3719
 * eliminate the inner dimensions from the "set" field of the elements
3720
 * indexed by the first "n" elements of "order", provided the current
3721
 * dimension does not have a fixed value.
3722
 *
3723
 * Return the index of the first element in "order" with a corresponding
3724
 * "set" field that does not have an (obviously) fixed value.
3725
 */
3726
static int eliminate_non_fixed(struct isl_set_map_pair *domain,
3727
  int *order, int n, int depth, __isl_keep isl_ast_build *build)
3728
74
{
3729
74
  int i;
3730
74
  int base = -1;
3731
74
3732
284
  for (i = n - 1; i >= 0; 
--i210
) {
3733
210
    int f;
3734
210
    f = dim_is_fixed(domain[order[i]].set, depth);
3735
210
    if (f < 0)
3736
0
      return -1;
3737
210
    if (f)
3738
1
      continue;
3739
209
    domain[order[i]].set = isl_ast_build_eliminate_inner(build,
3740
209
              domain[order[i]].set);
3741
209
    base = i;
3742
209
  }
3743
74
3744
74
  return base;
3745
74
}
3746
3747
/* Given an array "domain" of isl_set_map_pairs and an array "order"
3748
 * of indices into the "domain" array,
3749
 * find the element of "domain" (amongst those indexed by the first "n"
3750
 * elements of "order") with the "set" field that has the smallest
3751
 * value for the current iterator.
3752
 *
3753
 * Note that the domain with the smallest value may depend on the parameters
3754
 * and/or outer loop dimension.  Since the result of this function is only
3755
 * used as heuristic, we only make a reasonable attempt at finding the best
3756
 * domain, one that should work in case a single domain provides the smallest
3757
 * value for the current dimension over all values of the parameters
3758
 * and outer dimensions.
3759
 *
3760
 * In particular, we compute the smallest value of the first domain
3761
 * and replace it by that of any later domain if that later domain
3762
 * has a smallest value that is smaller for at least some value
3763
 * of the parameters and outer dimensions.
3764
 */
3765
static int first_offset(struct isl_set_map_pair *domain, int *order, int n,
3766
  __isl_keep isl_ast_build *build)
3767
3
{
3768
3
  int i;
3769
3
  isl_map *min_first;
3770
3
  int first = 0;
3771
3
3772
3
  min_first = isl_ast_build_map_to_iterator(build,
3773
3
          isl_set_copy(domain[order[0]].set));
3774
3
  min_first = isl_map_lexmin(min_first);
3775
3
3776
9
  for (i = 1; i < n; 
++i6
) {
3777
6
    isl_map *min, *test;
3778
6
    int empty;
3779
6
3780
6
    min = isl_ast_build_map_to_iterator(build,
3781
6
          isl_set_copy(domain[order[i]].set));
3782
6
    min = isl_map_lexmin(min);
3783
6
    test = isl_map_copy(min);
3784
6
    test = isl_map_apply_domain(isl_map_copy(min_first), test);
3785
6
    test = isl_map_order_lt(test, isl_dim_in, 0, isl_dim_out, 0);
3786
6
    empty = isl_map_is_empty(test);
3787
6
    isl_map_free(test);
3788
6
    if (empty >= 0 && !empty) {
3789
1
      isl_map_free(min_first);
3790
1
      first = i;
3791
1
      min_first = min;
3792
1
    } else
3793
5
      isl_map_free(min);
3794
6
3795
6
    if (empty < 0)
3796
0
      break;
3797
6
  }
3798
3
3799
3
  isl_map_free(min_first);
3800
3
3801
3
  return i < n ? 
-10
: first;
3802
3
}
3803
3804
/* Construct a shifted inverse schedule based on the original inverse schedule,
3805
 * the stride and the offset.
3806
 *
3807
 * The original inverse schedule is specified as the "map" fields
3808
 * of the elements of "domain" indexed by the first "n" elements of "order".
3809
 *
3810
 * "stride" and "offset" are such that the difference
3811
 * between the values of the current dimension of domain "i"
3812
 * and the values of the current dimension for some reference domain are
3813
 * equal to
3814
 *
3815
 *  stride * integer + offset[i]
3816
 *
3817
 * Moreover, 0 <= offset[i] < stride.
3818
 *
3819
 * For each domain, we create a map
3820
 *
3821
 *  { [..., j, ...] -> [..., j - offset[i], offset[i], ....] }
3822
 *
3823
 * where j refers to the current dimension and the other dimensions are
3824
 * unchanged, and apply this map to the original schedule domain.
3825
 *
3826
 * For example, for the original schedule
3827
 *
3828
 *  { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
3829
 *
3830
 * and assuming the offset is 0 for the A domain and 1 for the B domain,
3831
 * we apply the mapping
3832
 *
3833
 *  { [j] -> [j, 0] }
3834
 *
3835
 * to the schedule of the "A" domain and the mapping
3836
 *
3837
 *  { [j - 1] -> [j, 1] }
3838
 *
3839
 * to the schedule of the "B" domain.
3840
 *
3841
 *
3842
 * Note that after the transformation, the differences between pairs
3843
 * of values of the current dimension over all domains are multiples
3844
 * of stride and that we have therefore exposed the stride.
3845
 *
3846
 *
3847
 * To see that the mapping preserves the lexicographic order,
3848
 * first note that each of the individual maps above preserves the order.
3849
 * If the value of the current iterator is j1 in one domain and j2 in another,
3850
 * then if j1 = j2, we know that the same map is applied to both domains
3851
 * and the order is preserved.
3852
 * Otherwise, let us assume, without loss of generality, that j1 < j2.
3853
 * If c1 >= c2 (with c1 and c2 the corresponding offsets), then
3854
 *
3855
 *  j1 - c1 < j2 - c2
3856
 *
3857
 * and the order is preserved.
3858
 * If c1 < c2, then we know
3859
 *
3860
 *  0 <= c2 - c1 < s
3861
 *
3862
 * We also have
3863
 *
3864
 *  j2 - j1 = n * s + r
3865
 *
3866
 * with n >= 0 and 0 <= r < s.
3867
 * In other words, r = c2 - c1.
3868
 * If n > 0, then
3869
 *
3870
 *  j1 - c1 < j2 - c2
3871
 *
3872
 * If n = 0, then
3873
 *
3874
 *  j1 - c1 = j2 - c2
3875
 *
3876
 * and so
3877
 *
3878
 *  (j1 - c1, c1) << (j2 - c2, c2)
3879
 *
3880
 * with "<<" the lexicographic order, proving that the order is preserved
3881
 * in all cases.
3882
 */
3883
static __isl_give isl_union_map *contruct_shifted_executed(
3884
  struct isl_set_map_pair *domain, int *order, int n,
3885
  __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3886
  __isl_take isl_ast_build *build)
3887
3
{
3888
3
  int i;
3889
3
  isl_union_map *executed;
3890
3
  isl_space *space;
3891
3
  isl_map *map;
3892
3
  int depth;
3893
3
  isl_constraint *c;
3894
3
3895
3
  depth = isl_ast_build_get_depth(build);
3896
3
  space = isl_ast_build_get_space(build, 1);
3897
3
  executed = isl_union_map_empty(isl_space_copy(space));
3898
3
  space = isl_space_map_from_set(space);
3899
3
  map = isl_map_identity(isl_space_copy(space));
3900
3
  map = isl_map_eliminate(map, isl_dim_out, depth, 1);
3901
3
  map = isl_map_insert_dims(map, isl_dim_out, depth + 1, 1);
3902
3
  space = isl_space_insert_dims(space, isl_dim_out, depth + 1, 1);
3903
3
3904
3
  c = isl_constraint_alloc_equality(isl_local_space_from_space(space));
3905
3
  c = isl_constraint_set_coefficient_si(c, isl_dim_in, depth, 1);
3906
3
  c = isl_constraint_set_coefficient_si(c, isl_dim_out, depth, -1);
3907
3
3908
12
  for (i = 0; i < n; 
++i9
) {
3909
9
    isl_map *map_i;
3910
9
    isl_val *v;
3911
9
3912
9
    v = isl_multi_val_get_val(offset, i);
3913
9
    if (!v)
3914
0
      break;
3915
9
    map_i = isl_map_copy(map);
3916
9
    map_i = isl_map_fix_val(map_i, isl_dim_out, depth + 1,
3917
9
          isl_val_copy(v));
3918
9
    v = isl_val_neg(v);
3919
9
    c = isl_constraint_set_constant_val(c, v);
3920
9
    map_i = isl_map_add_constraint(map_i, isl_constraint_copy(c));
3921
9
3922
9
    map_i = isl_map_apply_domain(isl_map_copy(domain[order[i]].map),
3923
9
            map_i);
3924
9
    executed = isl_union_map_add_map(executed, map_i);
3925
9
  }
3926
3
3927
3
  isl_constraint_free(c);
3928
3
  isl_map_free(map);
3929
3
3930
3
  if (i < n)
3931
0
    executed = isl_union_map_free(executed);
3932
3
3933
3
  return executed;
3934
3
}
3935
3936
/* Generate code for a single component, after exposing the stride,
3937
 * given that the schedule domain is "shifted strided".
3938
 *
3939
 * The component inverse schedule is specified as the "map" fields
3940
 * of the elements of "domain" indexed by the first "n" elements of "order".
3941
 *
3942
 * The schedule domain being "shifted strided" means that the differences
3943
 * between the values of the current dimension of domain "i"
3944
 * and the values of the current dimension for some reference domain are
3945
 * equal to
3946
 *
3947
 *  stride * integer + offset[i]
3948
 *
3949
 * We first look for the domain with the "smallest" value for the current
3950
 * dimension and adjust the offsets such that the offset of the "smallest"
3951
 * domain is equal to zero.  The other offsets are reduced modulo stride.
3952
 *
3953
 * Based on this information, we construct a new inverse schedule in
3954
 * contruct_shifted_executed that exposes the stride.
3955
 * Since this involves the introduction of a new schedule dimension,
3956
 * the build needs to be changed accodingly.
3957
 * After computing the AST, the newly introduced dimension needs
3958
 * to be removed again from the list of grafts.  We do this by plugging
3959
 * in a mapping that represents the new schedule domain in terms of the
3960
 * old schedule domain.
3961
 */
3962
static __isl_give isl_ast_graft_list *generate_shift_component(
3963
  struct isl_set_map_pair *domain, int *order, int n,
3964
  __isl_keep isl_val *stride, __isl_keep isl_multi_val *offset,
3965
  __isl_take isl_ast_build *build)
3966
3
{
3967
3
  isl_ast_graft_list *list;
3968
3
  int first;
3969
3
  int depth;
3970
3
  isl_val *val;
3971
3
  isl_multi_val *mv;
3972
3
  isl_space *space;
3973
3
  isl_multi_aff *ma, *zero;
3974
3
  isl_union_map *executed;
3975
3
3976
3
  depth = isl_ast_build_get_depth(build);
3977
3
3978
3
  first = first_offset(domain, order, n, build);
3979
3
  if (first < 0)
3980
0
    goto error;
3981
3
3982
3
  mv = isl_multi_val_copy(offset);
3983
3
  val = isl_multi_val_get_val(offset, first);
3984
3
  val = isl_val_neg(val);
3985
3
  mv = isl_multi_val_add_val(mv, val);
3986
3
  mv = isl_multi_val_mod_val(mv, isl_val_copy(stride));
3987
3
3988
3
  executed = contruct_shifted_executed(domain, order, n, stride, mv,
3989
3
            build);
3990
3
  space = isl_ast_build_get_space(build, 1);
3991
3
  space = isl_space_map_from_set(space);
3992
3
  ma = isl_multi_aff_identity(isl_space_copy(space));
3993
3
  space = isl_space_from_domain(isl_space_domain(space));
3994
3
  space = isl_space_add_dims(space, isl_dim_out, 1);
3995
3
  zero = isl_multi_aff_zero(space);
3996
3
  ma = isl_multi_aff_range_splice(ma, depth + 1, zero);
3997
3
  build = isl_ast_build_insert_dim(build, depth + 1);
3998
3
  list = generate_shifted_component(executed, build);
3999
3
4000
3
  list = isl_ast_graft_list_preimage_multi_aff(list, ma);
4001
3
4002
3
  isl_multi_val_free(mv);
4003
3
4004
3
  return list;
4005
0
error:
4006
0
  isl_ast_build_free(build);
4007
0
  return NULL;
4008
3
}
4009
4010
/* Does any node in the schedule tree rooted at the current schedule node
4011
 * of "build" depend on outer schedule nodes?
4012
 */
4013
static int has_anchored_subtree(__isl_keep isl_ast_build *build)
4014
73
{
4015
73
  isl_schedule_node *node;
4016
73
  int dependent = 0;
4017
73
4018
73
  node = isl_ast_build_get_schedule_node(build);
4019
73
  dependent = isl_schedule_node_is_subtree_anchored(node);
4020
73
  isl_schedule_node_free(node);
4021
73
4022
73
  return dependent;
4023
73
}
4024
4025
/* Generate code for a single component.
4026
 *
4027
 * The component inverse schedule is specified as the "map" fields
4028
 * of the elements of "domain" indexed by the first "n" elements of "order".
4029
 *
4030
 * This function may modify the "set" fields of "domain".
4031
 *
4032
 * Before proceeding with the actual code generation for the component,
4033
 * we first check if there are any "shifted" strides, meaning that
4034
 * the schedule domains of the individual domains are all strided,
4035
 * but that they have different offsets, resulting in the union
4036
 * of schedule domains not being strided anymore.
4037
 *
4038
 * The simplest example is the schedule
4039
 *
4040
 *  { A[i] -> [2i]: 0 <= i < 10; B[i] -> [2i+1] : 0 <= i < 10 }
4041
 *
4042
 * Both schedule domains are strided, but their union is not.
4043
 * This function detects such cases and then rewrites the schedule to
4044
 *
4045
 *  { A[i] -> [2i, 0]: 0 <= i < 10; B[i] -> [2i, 1] : 0 <= i < 10 }
4046
 *
4047
 * In the new schedule, the schedule domains have the same offset (modulo
4048
 * the stride), ensuring that the union of schedule domains is also strided.
4049
 *
4050
 *
4051
 * If there is only a single domain in the component, then there is
4052
 * nothing to do.   Similarly, if the current schedule dimension has
4053
 * a fixed value for almost all domains then there is nothing to be done.
4054
 * In particular, we need at least two domains where the current schedule
4055
 * dimension does not have a fixed value.
4056
 * Finally, in case of a schedule map input,
4057
 * if any of the options refer to the current schedule dimension,
4058
 * then we bail out as well.  It would be possible to reformulate the options
4059
 * in terms of the new schedule domain, but that would introduce constraints
4060
 * that separate the domains in the options and that is something we would
4061
 * like to avoid.
4062
 * In the case of a schedule tree input, we bail out if any of
4063
 * the descendants of the current schedule node refer to outer
4064
 * schedule nodes in any way.
4065
 *
4066
 *
4067
 * To see if there is any shifted stride, we look at the differences
4068
 * between the values of the current dimension in pairs of domains
4069
 * for equal values of outer dimensions.  These differences should be
4070
 * of the form
4071
 *
4072
 *  m x + r
4073
 *
4074
 * with "m" the stride and "r" a constant.  Note that we cannot perform
4075
 * this analysis on individual domains as the lower bound in each domain
4076
 * may depend on parameters or outer dimensions and so the current dimension
4077
 * itself may not have a fixed remainder on division by the stride.
4078
 *
4079
 * In particular, we compare the first domain that does not have an
4080
 * obviously fixed value for the current dimension to itself and all
4081
 * other domains and collect the offsets and the gcd of the strides.
4082
 * If the gcd becomes one, then we failed to find shifted strides.
4083
 * If the gcd is zero, then the differences were all fixed, meaning
4084
 * that some domains had non-obviously fixed values for the current dimension.
4085
 * If all the offsets are the same (for those domains that do not have
4086
 * an obviously fixed value for the current dimension), then we do not
4087
 * apply the transformation.
4088
 * If none of the domains were skipped, then there is nothing to do.
4089
 * If some of them were skipped, then if we apply separation, the schedule
4090
 * domain should get split in pieces with a (non-shifted) stride.
4091
 *
4092
 * Otherwise, we apply a shift to expose the stride in
4093
 * generate_shift_component.
4094
 */
4095
static __isl_give isl_ast_graft_list *generate_component(
4096
  struct isl_set_map_pair *domain, int *order, int n,
4097
  __isl_take isl_ast_build *build)
4098
160
{
4099
160
  int i, d;
4100
160
  int depth;
4101
160
  isl_ctx *ctx;
4102
160
  isl_map *map;
4103
160
  isl_set *deltas;
4104
160
  isl_val *gcd = NULL;
4105
160
  isl_multi_val *mv;
4106
160
  int fixed, skip;
4107
160
  int base;
4108
160
  isl_ast_graft_list *list;
4109
160
  int res = 0;
4110
160
4111
160
  depth = isl_ast_build_get_depth(build);
4112
160
4113
160
  skip = n == 1;
4114
160
  if (skip >= 0 && !skip)
4115
82
    skip = at_most_one_non_fixed(domain, order, n, depth);
4116
160
  if (skip >= 0 && !skip) {
4117
74
    if (isl_ast_build_has_schedule_node(build))
4118
73
      skip = has_anchored_subtree(build);
4119
1
    else
4120
1
      skip = isl_ast_build_options_involve_depth(build);
4121
74
  }
4122
160
  if (skip < 0)
4123
0
    goto error;
4124
160
  if (skip)
4125
86
    return generate_shifted_component_from_list(domain,
4126
86
                  order, n, build);
4127
74
4128
74
  base = eliminate_non_fixed(domain, order, n, depth, build);
4129
74
  if (base < 0)
4130
0
    goto error;
4131
74
4132
74
  ctx = isl_ast_build_get_ctx(build);
4133
74
4134
74
  mv = isl_multi_val_zero(isl_space_set_alloc(ctx, 0, n));
4135
74
4136
74
  fixed = 1;
4137
87
  for (i = 0; i < n; 
++i13
) {
4138
84
    isl_val *r, *m;
4139
84
4140
84
    map = isl_map_from_domain_and_range(
4141
84
          isl_set_copy(domain[order[base]].set),
4142
84
          isl_set_copy(domain[order[i]].set));
4143
106
    for (d = 0; d < depth; 
++d22
)
4144
22
      map = isl_map_equate(map, isl_dim_in, d,
4145
22
                isl_dim_out, d);
4146
84
    deltas = isl_map_deltas(map);
4147
84
    res = isl_set_dim_residue_class_val(deltas, depth, &m, &r);
4148
84
    isl_set_free(deltas);
4149
84
    if (res < 0)
4150
0
      break;
4151
84
4152
84
    if (i == 0)
4153
74
      gcd = m;
4154
10
    else
4155
10
      gcd = isl_val_gcd(gcd, m);
4156
84
    if (isl_val_is_one(gcd)) {
4157
71
      isl_val_free(r);
4158
71
      break;
4159
71
    }
4160
13
    mv = isl_multi_val_set_val(mv, i, r);
4161
13
4162
13
    res = dim_is_fixed(domain[order[i]].set, depth);
4163
13
    if (res < 0)
4164
0
      break;
4165
13
    if (res)
4166
0
      continue;
4167
13
4168
13
    if (fixed && 
i > base10
) {
4169
5
      isl_val *a, *b;
4170
5
      a = isl_multi_val_get_val(mv, i);
4171
5
      b = isl_multi_val_get_val(mv, base);
4172
5
      if (isl_val_ne(a, b))
4173
4
        fixed = 0;
4174
5
      isl_val_free(a);
4175
5
      isl_val_free(b);
4176
5
    }
4177
13
  }
4178
74
4179
74
  if (res < 0 || !gcd) {
4180
0
    isl_ast_build_free(build);
4181
0
    list = NULL;
4182
74
  } else if (i < n || 
fixed3
||
isl_val_is_zero(gcd)3
) {
4183
71
    list = generate_shifted_component_from_list(domain,
4184
71
                  order, n, build);
4185
71
  } else {
4186
3
    list = generate_shift_component(domain, order, n, gcd, mv,
4187
3
            build);
4188
3
  }
4189
74
4190
74
  isl_val_free(gcd);
4191
74
  isl_multi_val_free(mv);
4192
74
4193
74
  return list;
4194
0
error:
4195
0
  isl_ast_build_free(build);
4196
0
  return NULL;
4197
74
}
4198
4199
/* Store both "map" itself and its domain in the
4200
 * structure pointed to by *next and advance to the next array element.
4201
 */
4202
static isl_stat extract_domain(__isl_take isl_map *map, void *user)
4203
308
{
4204
308
  struct isl_set_map_pair **next = user;
4205
308
4206
308
  (*next)->map = isl_map_copy(map);
4207
308
  (*next)->set = isl_map_domain(map);
4208
308
  (*next)++;
4209
308
4210
308
  return isl_stat_ok;
4211
308
}
4212
4213
static int after_in_tree(__isl_keep isl_union_map *umap,
4214
  __isl_keep isl_schedule_node *node);
4215
4216
/* Is any domain element of "umap" scheduled after any of
4217
 * the corresponding image elements by the tree rooted at
4218
 * the child of "node"?
4219
 */
4220
static int after_in_child(__isl_keep isl_union_map *umap,
4221
  __isl_keep isl_schedule_node *node)
4222
41
{
4223
41
  isl_schedule_node *child;
4224
41
  int after;
4225
41
4226
41
  child = isl_schedule_node_get_child(node, 0);
4227
41
  after = after_in_tree(umap, child);
4228
41
  isl_schedule_node_free(child);
4229
41
4230
41
  return after;
4231
41
}
4232
4233
/* Is any domain element of "umap" scheduled after any of
4234
 * the corresponding image elements by the tree rooted at
4235
 * the band node "node"?
4236
 *
4237
 * We first check if any domain element is scheduled after any
4238
 * of the corresponding image elements by the band node itself.
4239
 * If not, we restrict "map" to those pairs of element that
4240
 * are scheduled together by the band node and continue with
4241
 * the child of the band node.
4242
 * If there are no such pairs then the map passed to after_in_child
4243
 * will be empty causing it to return 0.
4244
 */
4245
static int after_in_band(__isl_keep isl_union_map *umap,
4246
  __isl_keep isl_schedule_node *node)
4247
0
{
4248
0
  isl_multi_union_pw_aff *mupa;
4249
0
  isl_union_map *partial, *test, *gt, *universe, *umap1, *umap2;
4250
0
  isl_union_set *domain, *range;
4251
0
  isl_space *space;
4252
0
  int empty;
4253
0
  int after;
4254
0
4255
0
  if (isl_schedule_node_band_n_member(node) == 0)
4256
0
    return after_in_child(umap, node);
4257
0
4258
0
  mupa = isl_schedule_node_band_get_partial_schedule(node);
4259
0
  space = isl_multi_union_pw_aff_get_space(mupa);
4260
0
  partial = isl_union_map_from_multi_union_pw_aff(mupa);
4261
0
  test = isl_union_map_copy(umap);
4262
0
  test = isl_union_map_apply_domain(test, isl_union_map_copy(partial));
4263
0
  test = isl_union_map_apply_range(test, isl_union_map_copy(partial));
4264
0
  gt = isl_union_map_from_map(isl_map_lex_gt(space));
4265
0
  test = isl_union_map_intersect(test, gt);
4266
0
  empty = isl_union_map_is_empty(test);
4267
0
  isl_union_map_free(test);
4268
0
4269
0
  if (empty < 0 || !empty) {
4270
0
    isl_union_map_free(partial);
4271
0
    return empty < 0 ? -1 : 1;
4272
0
  }
4273
0
4274
0
  universe = isl_union_map_universe(isl_union_map_copy(umap));
4275
0
  domain = isl_union_map_domain(isl_union_map_copy(universe));
4276
0
  range = isl_union_map_range(universe);
4277
0
  umap1 = isl_union_map_copy(partial);
4278
0
  umap1 = isl_union_map_intersect_domain(umap1, domain);
4279
0
  umap2 = isl_union_map_intersect_domain(partial, range);
4280
0
  test = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4281
0
  test = isl_union_map_intersect(test, isl_union_map_copy(umap));
4282
0
  after = after_in_child(test, node);
4283
0
  isl_union_map_free(test);
4284
0
  return after;
4285
0
}
4286
4287
/* Is any domain element of "umap" scheduled after any of
4288
 * the corresponding image elements by the tree rooted at
4289
 * the context node "node"?
4290
 *
4291
 * The context constraints apply to the schedule domain,
4292
 * so we cannot apply them directly to "umap", which contains
4293
 * pairs of statement instances.  Instead, we add them
4294
 * to the range of the prefix schedule for both domain and
4295
 * range of "umap".
4296
 */
4297
static int after_in_context(__isl_keep isl_union_map *umap,
4298
  __isl_keep isl_schedule_node *node)
4299
0
{
4300
0
  isl_union_map *prefix, *universe, *umap1, *umap2;
4301
0
  isl_union_set *domain, *range;
4302
0
  isl_set *context;
4303
0
  int after;
4304
0
4305
0
  umap = isl_union_map_copy(umap);
4306
0
  context = isl_schedule_node_context_get_context(node);
4307
0
  prefix = isl_schedule_node_get_prefix_schedule_union_map(node);
4308
0
  universe = isl_union_map_universe(isl_union_map_copy(umap));
4309
0
  domain = isl_union_map_domain(isl_union_map_copy(universe));
4310
0
  range = isl_union_map_range(universe);
4311
0
  umap1 = isl_union_map_copy(prefix);
4312
0
  umap1 = isl_union_map_intersect_domain(umap1, domain);
4313
0
  umap2 = isl_union_map_intersect_domain(prefix, range);
4314
0
  umap1 = isl_union_map_intersect_range(umap1,
4315
0
              isl_union_set_from_set(context));
4316
0
  umap1 = isl_union_map_apply_range(umap1, isl_union_map_reverse(umap2));
4317
0
  umap = isl_union_map_intersect(umap, umap1);
4318
0
4319
0
  after = after_in_child(umap, node);
4320
0
4321
0
  isl_union_map_free(umap);
4322
0
4323
0
  return after;
4324
0
}
4325
4326
/* Is any domain element of "umap" scheduled after any of
4327
 * the corresponding image elements by the tree rooted at
4328
 * the expansion node "node"?
4329
 *
4330
 * We apply the expansion to domain and range of "umap" and
4331
 * continue with its child.
4332
 */
4333
static int after_in_expansion(__isl_keep isl_union_map *umap,
4334
  __isl_keep isl_schedule_node *node)
4335
0
{
4336
0
  isl_union_map *expansion;
4337
0
  int after;
4338
0
4339
0
  expansion = isl_schedule_node_expansion_get_expansion(node);
4340
0
  umap = isl_union_map_copy(umap);
4341
0
  umap = isl_union_map_apply_domain(umap, isl_union_map_copy(expansion));
4342
0
  umap = isl_union_map_apply_range(umap, expansion);
4343
0
4344
0
  after = after_in_child(umap, node);
4345
0
4346
0
  isl_union_map_free(umap);
4347
0
4348
0
  return after;
4349
0
}
4350
4351
/* Is any domain element of "umap" scheduled after any of
4352
 * the corresponding image elements by the tree rooted at
4353
 * the extension node "node"?
4354
 *
4355
 * Since the extension node may add statement instances before or
4356
 * after the pairs of statement instances in "umap", we return 1
4357
 * to ensure that these pairs are not broken up.
4358
 */
4359
static int after_in_extension(__isl_keep isl_union_map *umap,
4360
  __isl_keep isl_schedule_node *node)
4361
0
{
4362
0
  return 1;
4363
0
}
4364
4365
/* Is any domain element of "umap" scheduled after any of
4366
 * the corresponding image elements by the tree rooted at
4367
 * the filter node "node"?
4368
 *
4369
 * We intersect domain and range of "umap" with the filter and
4370
 * continue with its child.
4371
 */
4372
static int after_in_filter(__isl_keep isl_union_map *umap,
4373
  __isl_keep isl_schedule_node *node)
4374
41
{
4375
41
  isl_union_set *filter;
4376
41
  int after;
4377
41
4378
41
  umap = isl_union_map_copy(umap);
4379
41
  filter = isl_schedule_node_filter_get_filter(node);
4380
41
  umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(filter));
4381
41
  umap = isl_union_map_intersect_range(umap, filter);
4382
41
4383
41
  after = after_in_child(umap, node);
4384
41
4385
41
  isl_union_map_free(umap);
4386
41
4387
41
  return after;
4388
41
}
4389
4390
/* Is any domain element of "umap" scheduled after any of
4391
 * the corresponding image elements by the tree rooted at
4392
 * the set node "node"?
4393
 *
4394
 * This is only the case if this condition holds in any
4395
 * of the (filter) children of the set node.
4396
 * In particular, if the domain and the range of "umap"
4397
 * are contained in different children, then the condition
4398
 * does not hold.
4399
 */
4400
static int after_in_set(__isl_keep isl_union_map *umap,
4401
  __isl_keep isl_schedule_node *node)
4402
16
{
4403
16
  int i, n;
4404
16
4405
16
  n = isl_schedule_node_n_children(node);
4406
57
  for (i = 0; i < n; 
++i41
) {
4407
41
    isl_schedule_node *child;
4408
41
    int after;
4409
41
4410
41
    child = isl_schedule_node_get_child(node, i);
4411
41
    after = after_in_tree(umap, child);
4412
41
    isl_schedule_node_free(child);
4413
41
4414
41
    if (after < 0 || after)
4415
0
      return after;
4416
41
  }
4417
16
4418
16
  return 0;
4419
16
}
4420
4421
/* Return the filter of child "i" of "node".
4422
 */
4423
static __isl_give isl_union_set *child_filter(
4424
  __isl_keep isl_schedule_node *node, int i)
4425
36
{
4426
36
  isl_schedule_node *child;
4427
36
  isl_union_set *filter;
4428
36
4429
36
  child = isl_schedule_node_get_child(node, i);
4430
36
  filter = isl_schedule_node_filter_get_filter(child);
4431
36
  isl_schedule_node_free(child);
4432
36
4433
36
  return filter;
4434
36
}
4435
4436
/* Is any domain element of "umap" scheduled after any of
4437
 * the corresponding image elements by the tree rooted at
4438
 * the sequence node "node"?
4439
 *
4440
 * This happens in particular if any domain element is
4441
 * contained in a later child than one containing a range element or
4442
 * if the condition holds within a given child in the sequence.
4443
 * The later part of the condition is checked by after_in_set.
4444
 */
4445
static int after_in_sequence(__isl_keep isl_union_map *umap,
4446
  __isl_keep isl_schedule_node *node)
4447
19
{
4448
19
  int i, j, n;
4449
19
  isl_union_map *umap_i;
4450
19
  int empty, after = 0;
4451
19
4452
19
  n = isl_schedule_node_n_children(node);
4453
45
  for (i = 1; i < n; 
++i26
) {
4454
29
    isl_union_set *filter_i;
4455
29
4456
29
    umap_i = isl_union_map_copy(umap);
4457
29
    filter_i = child_filter(node, i);
4458
29
    umap_i = isl_union_map_intersect_domain(umap_i, filter_i);
4459
29
    empty = isl_union_map_is_empty(umap_i);
4460
29
    if (empty < 0)
4461
0
      goto error;
4462
29
    if (empty) {
4463
23
      isl_union_map_free(umap_i);
4464
23
      continue;
4465
23
    }
4466
6
4467
10
    
for (j = 0; 6
j < i;
++j4
) {
4468
7
      isl_union_set *filter_j;
4469
7
      isl_union_map *umap_ij;
4470
7
4471
7
      umap_ij = isl_union_map_copy(umap_i);
4472
7
      filter_j = child_filter(node, j);
4473
7
      umap_ij = isl_union_map_intersect_range(umap_ij,
4474
7
                filter_j);
4475
7
      empty = isl_union_map_is_empty(umap_ij);
4476
7
      isl_union_map_free(umap_ij);
4477
7
4478
7
      if (empty < 0)
4479
0
        goto error;
4480
7
      if (!empty)
4481
3
        after = 1;
4482
7
      if (after)
4483
3
        break;
4484
7
    }
4485
6
4486
6
    isl_union_map_free(umap_i);
4487
6
    if (after)
4488
3
      break;
4489
6
  }
4490
19
4491
19
  if (after < 0 || after)
4492
3
    return after;
4493
16
4494
16
  return after_in_set(umap, node);
4495
0
error:
4496
0
  isl_union_map_free(umap_i);
4497
0
  return -1;
4498
16
}
4499
4500
/* Is any domain element of "umap" scheduled after any of
4501
 * the corresponding image elements by the tree rooted at "node"?
4502
 *
4503
 * If "umap" is empty, then clearly there is no such element.
4504
 * Otherwise, consider the different types of nodes separately.
4505
 */
4506
static int after_in_tree(__isl_keep isl_union_map *umap,
4507
  __isl_keep isl_schedule_node *node)
4508
101
{
4509
101
  int empty;
4510
101
  enum isl_schedule_node_type type;
4511
101
4512
101
  empty = isl_union_map_is_empty(umap);
4513
101
  if (empty < 0)
4514
0
    return -1;
4515
101
  if (empty)
4516
41
    return 0;
4517
60
  if (!node)
4518
0
    return -1;
4519
60
4520
60
  type = isl_schedule_node_get_type(node);
4521
60
  switch (type) {
4522
60
  case isl_schedule_node_error:
4523
0
    return -1;
4524
60
  case isl_schedule_node_leaf:
4525
0
    return 0;
4526
60
  case isl_schedule_node_band:
4527
0
    return after_in_band(umap, node);
4528
60
  case isl_schedule_node_domain:
4529
0
    isl_die(isl_schedule_node_get_ctx(node), isl_error_internal,
4530
0
      "unexpected internal domain node", return -1);
4531
0
  case isl_schedule_node_context:
4532
0
    return after_in_context(umap, node);
4533
0
  case isl_schedule_node_expansion:
4534
0
    return after_in_expansion(umap, node);
4535
0
  case isl_schedule_node_extension:
4536
0
    return after_in_extension(umap, node);
4537
41
  case isl_schedule_node_filter:
4538
41
    return after_in_filter(umap, node);
4539
0
  case isl_schedule_node_guard:
4540
0
  case isl_schedule_node_mark:
4541
0
    return after_in_child(umap, node);
4542
0
  case isl_schedule_node_set:
4543
0
    return after_in_set(umap, node);
4544
19
  case isl_schedule_node_sequence:
4545
19
    return after_in_sequence(umap, node);
4546
0
  }
4547
0
4548
0
  return 1;
4549
0
}
4550
4551
/* Is any domain element of "map1" scheduled after any domain
4552
 * element of "map2" by the subtree underneath the current band node,
4553
 * while at the same time being scheduled together by the current
4554
 * band node, i.e., by "map1" and "map2?
4555
 *
4556
 * If the child of the current band node is a leaf, then
4557
 * no element can be scheduled after any other element.
4558
 *
4559
 * Otherwise, we construct a relation between domain elements
4560
 * of "map1" and domain elements of "map2" that are scheduled
4561
 * together and then check if the subtree underneath the current
4562
 * band node determines their relative order.
4563
 */
4564
static int after_in_subtree(__isl_keep isl_ast_build *build,
4565
  __isl_keep isl_map *map1, __isl_keep isl_map *map2)
4566
19
{
4567
19
  isl_schedule_node *node;
4568
19
  isl_map *map;
4569
19
  isl_union_map *umap;
4570
19
  int after;
4571
19
4572
19
  node = isl_ast_build_get_schedule_node(build);
4573
19
  if (!node)
4574
0
    return -1;
4575
19
  node = isl_schedule_node_child(node, 0);
4576
19
  if (isl_schedule_node_get_type(node) == isl_schedule_node_leaf) {
4577
0
    isl_schedule_node_free(node);
4578
0
    return 0;
4579
0
  }
4580
19
  map = isl_map_copy(map2);
4581
19
  map = isl_map_apply_domain(map, isl_map_copy(map1));
4582
19
  umap = isl_union_map_from_map(map);
4583
19
  after = after_in_tree(umap, node);
4584
19
  isl_union_map_free(umap);
4585
19
  isl_schedule_node_free(node);
4586
19
  return after;
4587
19
}
4588
4589
/* Internal data for any_scheduled_after.
4590
 *
4591
 * "build" is the build in which the AST is constructed.
4592
 * "depth" is the number of loops that have already been generated
4593
 * "group_coscheduled" is a local copy of options->ast_build_group_coscheduled
4594
 * "domain" is an array of set-map pairs corresponding to the different
4595
 * iteration domains.  The set is the schedule domain, i.e., the domain
4596
 * of the inverse schedule, while the map is the inverse schedule itself.
4597
 */
4598
struct isl_any_scheduled_after_data {
4599
  isl_ast_build *build;
4600
  int depth;
4601
  int group_coscheduled;
4602
  struct isl_set_map_pair *domain;
4603
};
4604
4605
/* Is any element of domain "i" scheduled after any element of domain "j"
4606
 * (for a common iteration of the first data->depth loops)?
4607
 *
4608
 * data->domain[i].set contains the domain of the inverse schedule
4609
 * for domain "i", i.e., elements in the schedule domain.
4610
 *
4611
 * If we are inside a band of a schedule tree and there is a pair
4612
 * of elements in the two domains that is schedule together by
4613
 * the current band, then we check if any element of "i" may be schedule
4614
 * after element of "j" by the descendants of the band node.
4615
 *
4616
 * If data->group_coscheduled is set, then we also return 1 if there
4617
 * is any pair of elements in the two domains that are scheduled together.
4618
 */
4619
static isl_bool any_scheduled_after(int i, int j, void *user)
4620
390
{
4621
390
  struct isl_any_scheduled_after_data *data = user;
4622
390
  int dim = isl_set_dim(data->domain[i].set, isl_dim_set);
4623
390
  int pos;
4624
390
4625
429
  for (pos = data->depth; pos < dim; 
++pos39
) {
4626
410
    int follows;
4627
410
4628
410
    follows = isl_set_follows_at(data->domain[i].set,
4629
410
            data->domain[j].set, pos);
4630
410
4631
410
    if (follows < -1)
4632
0
      return isl_bool_error;
4633
410
    if (follows > 0)
4634
315
      return isl_bool_true;
4635
95
    if (follows < 0)
4636
56
      return isl_bool_false;
4637
95
  }
4638
390
4639
390
  
if (19
isl_ast_build_has_schedule_node(data->build)19
) {
4640
19
    int after;
4641
19
4642
19
    after = after_in_subtree(data->build, data->domain[i].map,
4643
19
              data->domain[j].map);
4644
19
    if (after < 0 || after)
4645
3
      return after;
4646
16
  }
4647
16
4648
16
  return data->group_coscheduled;
4649
16
}
4650
4651
/* Look for independent components at the current depth and generate code
4652
 * for each component separately.  The resulting lists of grafts are
4653
 * merged in an attempt to combine grafts with identical guards.
4654
 *
4655
 * Code for two domains can be generated separately if all the elements
4656
 * of one domain are scheduled before (or together with) all the elements
4657
 * of the other domain.  We therefore consider the graph with as nodes
4658
 * the domains and an edge between two nodes if any element of the first
4659
 * node is scheduled after any element of the second node.
4660
 * If the ast_build_group_coscheduled is set, then we also add an edge if
4661
 * there is any pair of elements in the two domains that are scheduled
4662
 * together.
4663
 * Code is then generated (by generate_component)
4664
 * for each of the strongly connected components in this graph
4665
 * in their topological order.
4666
 *
4667
 * Since the test is performed on the domain of the inverse schedules of
4668
 * the different domains, we precompute these domains and store
4669
 * them in data.domain.
4670
 */
4671
static __isl_give isl_ast_graft_list *generate_components(
4672
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4673
114
{
4674
114
  int i;
4675
114
  isl_ctx *ctx = isl_ast_build_get_ctx(build);
4676
114
  int n = isl_union_map_n_map(executed);
4677
114
  struct isl_any_scheduled_after_data data;
4678
114
  struct isl_set_map_pair *next;
4679
114
  struct isl_tarjan_graph *g = NULL;
4680
114
  isl_ast_graft_list *list = NULL;
4681
114
  int n_domain = 0;
4682
114
4683
114
  data.domain = isl_calloc_array(ctx, struct isl_set_map_pair, n);
4684
114
  if (!data.domain)
4685
0
    goto error;
4686
114
  n_domain = n;
4687
114
4688
114
  next = data.domain;
4689
114
  if (isl_union_map_foreach_map(executed, &extract_domain, &next) < 0)
4690
0
    goto error;
4691
114
4692
114
  if (!build)
4693
0
    goto error;
4694
114
  data.build = build;
4695
114
  data.depth = isl_ast_build_get_depth(build);
4696
114
  data.group_coscheduled = isl_options_get_ast_build_group_coscheduled(ctx);
4697
114
  g = isl_tarjan_graph_init(ctx, n, &any_scheduled_after, &data);
4698
114
  if (!g)
4699
0
    goto error;
4700
114
4701
114
  list = isl_ast_graft_list_alloc(ctx, 0);
4702
114
4703
114
  i = 0;
4704
274
  while (list && n) {
4705
160
    isl_ast_graft_list *list_c;
4706
160
    int first = i;
4707
160
4708
160
    if (g->order[i] == -1)
4709
160
      
isl_die0
(ctx, isl_error_internal, "cannot happen",
4710
160
        goto error);
4711
160
    ++i; --n;
4712
308
    while (g->order[i] != -1) {
4713
148
      ++i; --n;
4714
148
    }
4715
160
4716
160
    list_c = generate_component(data.domain,
4717
160
              g->order + first, i - first,
4718
160
              isl_ast_build_copy(build));
4719
160
    list = isl_ast_graft_list_merge(list, list_c, build);
4720
160
4721
160
    ++i;
4722
160
  }
4723
114
4724
114
  if (0)
4725
0
error:    list = isl_ast_graft_list_free(list);
4726
114
  isl_tarjan_graph_free(g);
4727
422
  for (i = 0; i < n_domain; 
++i308
) {
4728
308
    isl_map_free(data.domain[i].map);
4729
308
    isl_set_free(data.domain[i].set);
4730
308
  }
4731
114
  free(data.domain);
4732
114
  isl_union_map_free(executed);
4733
114
  isl_ast_build_free(build);
4734
114
4735
114
  return list;
4736
114
}
4737
4738
/* Generate code for the next level (and all inner levels).
4739
 *
4740
 * If "executed" is empty, i.e., no code needs to be generated,
4741
 * then we return an empty list.
4742
 *
4743
 * If we have already generated code for all loop levels, then we pass
4744
 * control to generate_inner_level.
4745
 *
4746
 * If "executed" lives in a single space, i.e., if code needs to be
4747
 * generated for a single domain, then there can only be a single
4748
 * component and we go directly to generate_shifted_component.
4749
 * Otherwise, we call generate_components to detect the components
4750
 * and to call generate_component on each of them separately.
4751
 */
4752
static __isl_give isl_ast_graft_list *generate_next_level(
4753
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build)
4754
4.48k
{
4755
4.48k
  int depth;
4756
4.48k
4757
4.48k
  if (!build || !executed)
4758
0
    goto error;
4759
4.48k
4760
4.48k
  if (isl_union_map_is_empty(executed)) {
4761
0
    isl_ctx *ctx = isl_ast_build_get_ctx(build);
4762
0
    isl_union_map_free(executed);
4763
0
    isl_ast_build_free(build);
4764
0
    return isl_ast_graft_list_alloc(ctx, 0);
4765
0
  }
4766
4.48k
4767
4.48k
  depth = isl_ast_build_get_depth(build);
4768
4.48k
  if (depth >= isl_ast_build_dim(build, isl_dim_set))
4769
2.11k
    return generate_inner_level(executed, build);
4770
2.37k
4771
2.37k
  if (isl_union_map_n_map(executed) == 1)
4772
2.25k
    return generate_shifted_component(executed, build);
4773
114
4774
114
  return generate_components(executed, build);
4775
0
error:
4776
0
  isl_union_map_free(executed);
4777
0
  isl_ast_build_free(build);
4778
0
  return NULL;
4779
114
}
4780
4781
/* Internal data structure used by isl_ast_build_node_from_schedule_map.
4782
 * internal, executed and build are the inputs to generate_code.
4783
 * list collects the output.
4784
 */
4785
struct isl_generate_code_data {
4786
  int internal;
4787
  isl_union_map *executed;
4788
  isl_ast_build *build;
4789
4790
  isl_ast_graft_list *list;
4791
};
4792
4793
/* Given an inverse schedule in terms of the external build schedule, i.e.,
4794
 *
4795
 *  [E -> S] -> D
4796
 *
4797
 * with E the external build schedule and S the additional schedule "space",
4798
 * reformulate the inverse schedule in terms of the internal schedule domain,
4799
 * i.e., return
4800
 *
4801
 *  [I -> S] -> D
4802
 *
4803
 * We first obtain a mapping
4804
 *
4805
 *  I -> E
4806
 *
4807
 * take the inverse and the product with S -> S, resulting in
4808
 *
4809
 *  [I -> S] -> [E -> S]
4810
 *
4811
 * Applying the map to the input produces the desired result.
4812
 */
4813
static __isl_give isl_union_map *internal_executed(
4814
  __isl_take isl_union_map *executed, __isl_keep isl_space *space,
4815
  __isl_keep isl_ast_build *build)
4816
3
{
4817
3
  isl_map *id, *proj;
4818
3
4819
3
  proj = isl_ast_build_get_schedule_map(build);
4820
3
  proj = isl_map_reverse(proj);
4821
3
  space = isl_space_map_from_set(isl_space_copy(space));
4822
3
  id = isl_map_identity(space);
4823
3
  proj = isl_map_product(proj, id);
4824
3
  executed = isl_union_map_apply_domain(executed,
4825
3
            isl_union_map_from_map(proj));
4826
3
  return executed;
4827
3
}
4828
4829
/* Generate an AST that visits the elements in the range of data->executed
4830
 * in the relative order specified by the corresponding domain element(s)
4831
 * for those domain elements that belong to "set".
4832
 * Add the result to data->list.
4833
 *
4834
 * The caller ensures that "set" is a universe domain.
4835
 * "space" is the space of the additional part of the schedule.
4836
 * It is equal to the space of "set" if build->domain is parametric.
4837
 * Otherwise, it is equal to the range of the wrapped space of "set".
4838
 *
4839
 * If the build space is not parametric and
4840
 * if isl_ast_build_node_from_schedule_map
4841
 * was called from an outside user (data->internal not set), then
4842
 * the (inverse) schedule refers to the external build domain and needs to
4843
 * be transformed to refer to the internal build domain.
4844
 *
4845
 * If the build space is parametric, then we add some of the parameter
4846
 * constraints to the executed relation.  Adding these constraints
4847
 * allows for an earlier detection of conflicts in some cases.
4848
 * However, we do not want to divide the executed relation into
4849
 * more disjuncts than necessary.  We therefore approximate
4850
 * the constraints on the parameters by a single disjunct set.
4851
 *
4852
 * The build is extended to include the additional part of the schedule.
4853
 * If the original build space was not parametric, then the options
4854
 * in data->build refer only to the additional part of the schedule
4855
 * and they need to be adjusted to refer to the complete AST build
4856
 * domain.
4857
 *
4858
 * After having adjusted inverse schedule and build, we start generating
4859
 * code with the outer loop of the current code generation
4860
 * in generate_next_level.
4861
 *
4862
 * If the original build space was not parametric, we undo the embedding
4863
 * on the resulting isl_ast_node_list so that it can be used within
4864
 * the outer AST build.
4865
 */
4866
static isl_stat generate_code_in_space(struct isl_generate_code_data *data,
4867
  __isl_take isl_set *set, __isl_take isl_space *space)
4868
31
{
4869
31
  isl_union_map *executed;
4870
31
  isl_ast_build *build;
4871
31
  isl_ast_graft_list *list;
4872
31
  int embed;
4873
31
4874
31
  executed = isl_union_map_copy(data->executed);
4875
31
  executed = isl_union_map_intersect_domain(executed,
4876
31
             isl_union_set_from_set(set));
4877
31
4878
31
  embed = !isl_set_is_params(data->build->domain);
4879
31
  if (embed && 
!data->internal25
)
4880
3
    executed = internal_executed(executed, space, data->build);
4881
31
  if (!embed) {
4882
6
    isl_set *domain;
4883
6
    domain = isl_ast_build_get_domain(data->build);
4884
6
    domain = isl_set_from_basic_set(isl_set_simple_hull(domain));
4885
6
    executed = isl_union_map_intersect_params(executed, domain);
4886
6
  }
4887
31
4888
31
  build = isl_ast_build_copy(data->build);
4889
31
  build = isl_ast_build_product(build, space);
4890
31
4891
31
  list = generate_next_level(executed, build);
4892
31
4893
31
  list = isl_ast_graft_list_unembed(list, embed);
4894
31
4895
31
  data->list = isl_ast_graft_list_concat(data->list, list);
4896
31
4897
31
  return isl_stat_ok;
4898
31
}
4899
4900
/* Generate an AST that visits the elements in the range of data->executed
4901
 * in the relative order specified by the corresponding domain element(s)
4902
 * for those domain elements that belong to "set".
4903
 * Add the result to data->list.
4904
 *
4905
 * The caller ensures that "set" is a universe domain.
4906
 *
4907
 * If the build space S is not parametric, then the space of "set"
4908
 * need to be a wrapped relation with S as domain.  That is, it needs
4909
 * to be of the form
4910
 *
4911
 *  [S -> T]
4912
 *
4913
 * Check this property and pass control to generate_code_in_space
4914
 * passing along T.
4915
 * If the build space is not parametric, then T is the space of "set".
4916
 */
4917
static isl_stat generate_code_set(__isl_take isl_set *set, void *user)
4918
31
{
4919
31
  struct isl_generate_code_data *data = user;
4920
31
  isl_space *space, *build_space;
4921
31
  int is_domain;
4922
31
4923
31
  space = isl_set_get_space(set);
4924
31
4925
31
  if (isl_set_is_params(data->build->domain))
4926
6
    return generate_code_in_space(data, set, space);
4927
25
4928
25
  build_space = isl_ast_build_get_space(data->build, data->internal);
4929
25
  space = isl_space_unwrap(space);
4930
25
  is_domain = isl_space_is_domain(build_space, space);
4931
25
  isl_space_free(build_space);
4932
25
  space = isl_space_range(space);
4933
25
4934
25
  if (is_domain < 0)
4935
0
    goto error;
4936
25
  if (!is_domain)
4937
25
    
isl_die0
(isl_set_get_ctx(set), isl_error_invalid,
4938
25
      "invalid nested schedule space", goto error);
4939
25
4940
25
  return generate_code_in_space(data, set, space);
4941
0
error:
4942
0
  isl_set_free(set);
4943
0
  isl_space_free(space);
4944
0
  return isl_stat_error;
4945
25
}
4946
4947
/* Generate an AST that visits the elements in the range of "executed"
4948
 * in the relative order specified by the corresponding domain element(s).
4949
 *
4950
 * "build" is an isl_ast_build that has either been constructed by
4951
 * isl_ast_build_from_context or passed to a callback set by
4952
 * isl_ast_build_set_create_leaf.
4953
 * In the first case, the space of the isl_ast_build is typically
4954
 * a parametric space, although this is currently not enforced.
4955
 * In the second case, the space is never a parametric space.
4956
 * If the space S is not parametric, then the domain space(s) of "executed"
4957
 * need to be wrapped relations with S as domain.
4958
 *
4959
 * If the domain of "executed" consists of several spaces, then an AST
4960
 * is generated for each of them (in arbitrary order) and the results
4961
 * are concatenated.
4962
 *
4963
 * If "internal" is set, then the domain "S" above refers to the internal
4964
 * schedule domain representation.  Otherwise, it refers to the external
4965
 * representation, as returned by isl_ast_build_get_schedule_space.
4966
 *
4967
 * We essentially run over all the spaces in the domain of "executed"
4968
 * and call generate_code_set on each of them.
4969
 */
4970
static __isl_give isl_ast_graft_list *generate_code(
4971
  __isl_take isl_union_map *executed, __isl_take isl_ast_build *build,
4972
  int internal)
4973
31
{
4974
31
  isl_ctx *ctx;
4975
31
  struct isl_generate_code_data data = { 0 };
4976
31
  isl_space *space;
4977
31
  isl_union_set *schedule_domain;
4978
31
  isl_union_map *universe;
4979
31
4980
31
  if (!build)
4981
0
    goto error;
4982
31
  space = isl_ast_build_get_space(build, 1);
4983
31
  space = isl_space_align_params(space,
4984
31
            isl_union_map_get_space(executed));
4985
31
  space = isl_space_align_params(space,
4986
31
            isl_union_map_get_space(build->options));
4987
31
  build = isl_ast_build_align_params(build, isl_space_copy(space));
4988
31
  executed = isl_union_map_align_params(executed, space);
4989
31
  if (!executed || !build)
4990
0
    goto error;
4991
31
4992
31
  ctx = isl_ast_build_get_ctx(build);
4993
31
4994
31
  data.internal = internal;
4995
31
  data.executed = executed;
4996
31
  data.build = build;
4997
31
  data.list = isl_ast_graft_list_alloc(ctx, 0);
4998
31
4999
31
  universe = isl_union_map_universe(isl_union_map_copy(executed));
5000
31
  schedule_domain = isl_union_map_domain(universe);
5001
31
  if (isl_union_set_foreach_set(schedule_domain, &generate_code_set,
5002
31
          &data) < 0)
5003
0
    data.list = isl_ast_graft_list_free(data.list);
5004
31
5005
31
  isl_union_set_free(schedule_domain);
5006
31
  isl_union_map_free(executed);
5007
31
5008
31
  isl_ast_build_free(build);
5009
31
  return data.list;
5010
0
error:
5011
0
  isl_union_map_free(executed);
5012
0
  isl_ast_build_free(build);
5013
0
  return NULL;
5014
31
}
5015
5016
/* Generate an AST that visits the elements in the domain of "schedule"
5017
 * in the relative order specified by the corresponding image element(s).
5018
 *
5019
 * "build" is an isl_ast_build that has either been constructed by
5020
 * isl_ast_build_from_context or passed to a callback set by
5021
 * isl_ast_build_set_create_leaf.
5022
 * In the first case, the space of the isl_ast_build is typically
5023
 * a parametric space, although this is currently not enforced.
5024
 * In the second case, the space is never a parametric space.
5025
 * If the space S is not parametric, then the range space(s) of "schedule"
5026
 * need to be wrapped relations with S as domain.
5027
 *
5028
 * If the range of "schedule" consists of several spaces, then an AST
5029
 * is generated for each of them (in arbitrary order) and the results
5030
 * are concatenated.
5031
 *
5032
 * We first initialize the local copies of the relevant options.
5033
 * We do this here rather than when the isl_ast_build is created
5034
 * because the options may have changed between the construction
5035
 * of the isl_ast_build and the call to isl_generate_code.
5036
 *
5037
 * The main computation is performed on an inverse schedule (with
5038
 * the schedule domain in the domain and the elements to be executed
5039
 * in the range) called "executed".
5040
 */
5041
__isl_give isl_ast_node *isl_ast_build_node_from_schedule_map(
5042
  __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5043
9
{
5044
9
  isl_ast_graft_list *list;
5045
9
  isl_ast_node *node;
5046
9
  isl_union_map *executed;
5047
9
5048
9
  build = isl_ast_build_copy(build);
5049
9
  build = isl_ast_build_set_single_valued(build, 0);
5050
9
  schedule = isl_union_map_coalesce(schedule);
5051
9
  schedule = isl_union_map_remove_redundancies(schedule);
5052
9
  executed = isl_union_map_reverse(schedule);
5053
9
  list = generate_code(executed, isl_ast_build_copy(build), 0);
5054
9
  node = isl_ast_node_from_graft_list(list, build);
5055
9
  isl_ast_build_free(build);
5056
9
5057
9
  return node;
5058
9
}
5059
5060
/* The old name for isl_ast_build_node_from_schedule_map.
5061
 * It is being kept for backward compatibility, but
5062
 * it will be removed in the future.
5063
 */
5064
__isl_give isl_ast_node *isl_ast_build_ast_from_schedule(
5065
  __isl_keep isl_ast_build *build, __isl_take isl_union_map *schedule)
5066
0
{
5067
0
  return isl_ast_build_node_from_schedule_map(build, schedule);
5068
0
}
5069
5070
/* Generate an AST that visits the elements in the domain of "executed"
5071
 * in the relative order specified by the band node "node" and its descendants.
5072
 *
5073
 * The relation "executed" maps the outer generated loop iterators
5074
 * to the domain elements executed by those iterations.
5075
 *
5076
 * If the band is empty, we continue with its descendants.
5077
 * Otherwise, we extend the build and the inverse schedule with
5078
 * the additional space/partial schedule and continue generating
5079
 * an AST in generate_next_level.
5080
 * As soon as we have extended the inverse schedule with the additional
5081
 * partial schedule, we look for equalities that may exists between
5082
 * the old and the new part.
5083
 */
5084
static __isl_give isl_ast_graft_list *build_ast_from_band(
5085
  __isl_take isl_ast_build *build, __isl_take isl_schedule_node *node,
5086
  __isl_take isl_union_map *executed)
5087
592
{
5088
592
  isl_space *space;
5089
592
  isl_multi_union_pw_aff *extra;
5090
592
  isl_union_map *extra_umap;
5091
592
  isl_ast_graft_list *list;
5092
592
  unsigned n1, n2;
5093
592
5094
592
  if (!build || !node || !executed)
5095
0
    goto error;
5096
592
5097
592
  if (isl_schedule_node_band_n_member(node) == 0)
5098
0
    return build_ast_from_child(build, node, executed);
5099
592
5100
592
  extra = isl_schedule_node_band_get_partial_schedule(node);
5101
592
  extra = isl_multi_union_pw_aff_align_params(extra,
5102
592
        isl_ast_build_get_space(build, 1));
5103
592
  space = isl_multi_union_pw_aff_get_space(extra);
5104
592
5105
592
  extra_umap = isl_union_map_from_multi_union_pw_aff(extra);
5106
592
  extra_umap = isl_union_map_reverse(extra_umap);
5107
592
5108
592
  executed = isl_union_map_domain_product(executed, extra_umap);
5109
592
  executed = isl_union_map_detect_equalities(executed);
5110
592
5111
592
  n1 = isl_ast_build_dim(build, isl_dim_param);
5112
592
  build = isl_ast_build_product(build, space);
5113
592
  n2 = isl_ast_build_dim(build, isl_dim_param);
5114
592
  if (n2 > n1)
5115
592
    
isl_die0
(isl_ast_build_get_ctx(build), isl_error_invalid,
5116
592
      "band node is not allowed to introduce new parameters",
5117
592
      build = isl_ast_build_free(build));
5118
592
  build = isl_ast_build_set_schedule_node(build, node);
5119
592
5120
592
  list = generate_next_level(executed, build);
5121
592
5122
592
  list = isl_ast_graft_list_unembed(list, 1);
5123
592
5124
592
  return list;
5125
0
error:
5126
0
  isl_schedule_node_free(node);
5127
0
  isl_union_map_free(executed);
5128
0
  isl_ast_build_free(build);
5129
0
  return NULL;
5130
592
}
5131
5132
/* Hoist a list of grafts (in practice containing a single graft)
5133
 * from "sub_build" (which includes extra context information)
5134
 * to "build".
5135
 *
5136
 * In particular, project out all additional parameters introduced
5137
 * by the context node from the enforced constraints and the guard
5138
 * of the single graft.
5139
 */
5140
static __isl_give isl_ast_graft_list *hoist_out_of_context(
5141
  __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
5142
  __isl_keep isl_ast_build *sub_build)
5143
0
{
5144
0
  isl_ast_graft *graft;
5145
0
  isl_basic_set *enforced;
5146
0
  isl_set *guard;
5147
0
  unsigned n_param, extra_param;
5148
0
5149
0
  if (!build || !sub_build)
5150
0
    return isl_ast_graft_list_free(list);
5151
0
5152
0
  n_param = isl_ast_build_dim(build, isl_dim_param);
5153
0
  extra_param = isl_ast_build_dim(sub_build, isl_dim_param);
5154
0
5155
0
  if (extra_param == n_param)
5156
0
    return list;
5157
0
5158
0
  extra_param -= n_param;
5159
0
  enforced = isl_ast_graft_list_extract_shared_enforced(list, sub_build);
5160
0
  enforced = isl_basic_set_project_out(enforced, isl_dim_param,
5161
0
              n_param, extra_param);
5162
0
  enforced = isl_basic_set_remove_unknown_divs(enforced);
5163
0
  guard = isl_ast_graft_list_e