+
+static __isl_give isl_band_list *construct_band_list(
+ __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
+ int band_nr, int *parent_active, int n_active);
+
+/* Construct an isl_band structure for the band in the given schedule
+ * with sequence number band_nr for the n_active nodes marked by active.
+ * If the nodes don't have a band with the given sequence number,
+ * then a band without members is created.
+ *
+ * Because of the way the schedule is constructed, we know that
+ * the position of the band inside the schedule of a node is the same
+ * for all active nodes.
+ */
+static __isl_give isl_band *construct_band(__isl_keep isl_schedule *schedule,
+ __isl_keep isl_band *parent,
+ int band_nr, int *active, int n_active)
+{
+ int i, j;
+ isl_ctx *ctx = isl_schedule_get_ctx(schedule);
+ isl_band *band;
+ unsigned start, end;
+
+ band = isl_calloc_type(ctx, isl_band);
+ if (!band)
+ return NULL;
+
+ band->ref = 1;
+ band->schedule = schedule;
+ band->parent = parent;
+
+ for (i = 0; i < schedule->n; ++i)
+ if (active[i] && schedule->node[i].n_band > band_nr + 1)
+ break;
+
+ if (i < schedule->n) {
+ band->children = construct_band_list(schedule, band,
+ band_nr + 1, active, n_active);
+ if (!band->children)
+ goto error;
+ }
+
+ for (i = 0; i < schedule->n; ++i)
+ if (active[i])
+ break;
+
+ if (i >= schedule->n)
+ isl_die(ctx, isl_error_internal,
+ "band without active statements", goto error);
+
+ start = band_nr ? schedule->node[i].band_end[band_nr - 1] : 0;
+ end = band_nr < schedule->node[i].n_band ?
+ schedule->node[i].band_end[band_nr] : start;
+ band->n = end - start;
+
+ band->parallel = isl_alloc_array(ctx, int, band->n);
+ if (!band->parallel)
+ goto error;
+
+ for (j = 0; j < band->n; ++j)
+ band->parallel[j] = schedule->node[i].parallel[start + j];
+
+ band->map = isl_union_map_empty(isl_dim_copy(schedule->dim));
+ for (i = 0; i < schedule->n; ++i) {
+ isl_map *map;
+ unsigned n_out;
+
+ if (!active[i])
+ continue;
+
+ map = isl_map_copy(schedule->node[i].sched);
+ n_out = isl_map_dim(map, isl_dim_out);
+ map = isl_map_project_out(map, isl_dim_out, end, n_out - end);
+ map = isl_map_project_out(map, isl_dim_out, 0, start);
+ band->map = isl_union_map_union(band->map,
+ isl_union_map_from_map(map));
+ }
+ if (!band->map)
+ goto error;
+
+ return band;
+error:
+ isl_band_free(band);
+ return NULL;
+}
+
+/* Construct a list of bands that start at the same position (with
+ * sequence number band_nr) in the schedules of the nodes that
+ * were active in the parent band.
+ *
+ * A separate isl_band structure is created for each band_id
+ * and for each node that does not have a band with sequence
+ * number band_nr. In the latter case, a band without members
+ * is created.
+ * This ensures that if a band has any children, then each node
+ * that was active in the band is active in exactly one of the children.
+ */
+static __isl_give isl_band_list *construct_band_list(
+ __isl_keep isl_schedule *schedule, __isl_keep isl_band *parent,
+ int band_nr, int *parent_active, int n_active)
+{
+ int i, j;
+ isl_ctx *ctx = isl_schedule_get_ctx(schedule);
+ int *active;
+ int n_band;
+ isl_band_list *list;
+
+ n_band = 0;
+ for (i = 0; i < n_active; ++i) {
+ for (j = 0; j < schedule->n; ++j) {
+ if (!parent_active[j])
+ continue;
+ if (schedule->node[j].n_band <= band_nr)
+ continue;
+ if (schedule->node[j].band_id[band_nr] == i) {
+ n_band++;
+ break;
+ }
+ }
+ }
+ for (j = 0; j < schedule->n; ++j)
+ if (schedule->node[j].n_band <= band_nr)
+ n_band++;
+
+ if (n_band == 1) {
+ isl_band *band;
+ list = isl_band_list_alloc(ctx, n_band);
+ band = construct_band(schedule, parent, band_nr,
+ parent_active, n_active);
+ return isl_band_list_add(list, band);
+ }
+
+ active = isl_alloc_array(ctx, int, schedule->n);
+ if (!active)
+ return NULL;
+
+ list = isl_band_list_alloc(ctx, n_band);
+
+ for (i = 0; i < n_active; ++i) {
+ int n = 0;
+ isl_band *band;
+
+ for (j = 0; j < schedule->n; ++j) {
+ active[j] = parent_active[j] &&
+ schedule->node[j].n_band > band_nr &&
+ schedule->node[j].band_id[band_nr] == i;
+ if (active[j])
+ n++;
+ }
+ if (n == 0)
+ continue;
+
+ band = construct_band(schedule, parent, band_nr, active, n);
+
+ list = isl_band_list_add(list, band);
+ }
+ for (i = 0; i < schedule->n; ++i) {
+ isl_band *band;
+ if (!parent_active[i])
+ continue;
+ if (schedule->node[i].n_band > band_nr)
+ continue;
+ for (j = 0; j < schedule->n; ++j)
+ active[j] = j == i;
+ band = construct_band(schedule, parent, band_nr, active, 1);
+ list = isl_band_list_add(list, band);
+ }
+
+ free(active);
+
+ return list;
+}
+
+/* Construct a band forest representation of the schedule and
+ * return the list of roots.
+ */
+static __isl_give isl_band_list *construct_forest(
+ __isl_keep isl_schedule *schedule)
+{
+ int i;
+ isl_ctx *ctx = isl_schedule_get_ctx(schedule);
+ isl_band_list *forest;
+ int *active;
+
+ active = isl_alloc_array(ctx, int, schedule->n);
+ if (!active)
+ return NULL;
+
+ for (i = 0; i < schedule->n; ++i)
+ active[i] = 1;
+
+ forest = construct_band_list(schedule, NULL, 0, active, schedule->n);
+
+ free(active);
+
+ return forest;
+}
+
+/* Return the roots of a band forest representation of the schedule.
+ */
+__isl_give isl_band_list *isl_schedule_get_band_forest(
+ __isl_keep isl_schedule *schedule)
+{
+ if (!schedule)
+ return NULL;
+ if (!schedule->band_forest)
+ schedule->band_forest = construct_forest(schedule);
+ return isl_band_list_copy(schedule->band_forest);
+}