/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_ast_graft.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2012 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 <isl/id.h> |
14 | | #include <isl/space.h> |
15 | | #include <isl_ast_private.h> |
16 | | #include <isl_ast_build_expr.h> |
17 | | #include <isl_ast_build_private.h> |
18 | | #include <isl_ast_graft_private.h> |
19 | | |
20 | | static __isl_give isl_ast_graft *isl_ast_graft_copy( |
21 | | __isl_keep isl_ast_graft *graft); |
22 | | |
23 | | #undef BASE |
24 | | #define BASE ast_graft |
25 | | |
26 | | #include <isl_list_templ.c> |
27 | | |
28 | | #undef BASE |
29 | | #define BASE ast_graft |
30 | | #include <print_templ.c> |
31 | | |
32 | | isl_ctx *isl_ast_graft_get_ctx(__isl_keep isl_ast_graft *graft) |
33 | 3.02k | { |
34 | 3.02k | if (!graft) |
35 | 0 | return NULL; |
36 | 3.02k | return isl_basic_set_get_ctx(graft->enforced); |
37 | 3.02k | } |
38 | | |
39 | | __isl_give isl_ast_node *isl_ast_graft_get_node( |
40 | | __isl_keep isl_ast_graft *graft) |
41 | 6.17k | { |
42 | 6.17k | return graft ? isl_ast_node_copy(graft->node) : NULL; |
43 | 6.17k | } |
44 | | |
45 | | /* Create a graft for "node" with no guards and no enforced conditions. |
46 | | */ |
47 | | __isl_give isl_ast_graft *isl_ast_graft_alloc( |
48 | | __isl_take isl_ast_node *node, __isl_keep isl_ast_build *build) |
49 | 6.36k | { |
50 | 6.36k | isl_ctx *ctx; |
51 | 6.36k | isl_space *space; |
52 | 6.36k | isl_ast_graft *graft; |
53 | 6.36k | |
54 | 6.36k | if (!node) |
55 | 0 | return NULL; |
56 | 6.36k | |
57 | 6.36k | ctx = isl_ast_node_get_ctx(node); |
58 | 6.36k | graft = isl_calloc_type(ctx, isl_ast_graft); |
59 | 6.36k | if (!graft) |
60 | 0 | goto error; |
61 | 6.36k | |
62 | 6.36k | space = isl_ast_build_get_space(build, 1); |
63 | 6.36k | |
64 | 6.36k | graft->ref = 1; |
65 | 6.36k | graft->node = node; |
66 | 6.36k | graft->guard = isl_set_universe(isl_space_copy(space)); |
67 | 6.36k | graft->enforced = isl_basic_set_universe(space); |
68 | 6.36k | |
69 | 6.36k | if (!graft->guard || !graft->enforced) |
70 | 0 | return isl_ast_graft_free(graft); |
71 | 6.36k | |
72 | 6.36k | return graft; |
73 | 0 | error: |
74 | 0 | isl_ast_node_free(node); |
75 | 0 | return NULL; |
76 | 6.36k | } |
77 | | |
78 | | /* Create a graft with no guards and no enforced conditions |
79 | | * encapsulating a call to the domain element specified by "executed". |
80 | | * "executed" is assumed to be single-valued. |
81 | | */ |
82 | | __isl_give isl_ast_graft *isl_ast_graft_alloc_domain( |
83 | | __isl_take isl_map *executed, __isl_keep isl_ast_build *build) |
84 | 2.23k | { |
85 | 2.23k | isl_ast_node *node; |
86 | 2.23k | |
87 | 2.23k | node = isl_ast_build_call_from_executed(build, executed); |
88 | 2.23k | |
89 | 2.23k | return isl_ast_graft_alloc(node, build); |
90 | 2.23k | } |
91 | | |
92 | | static __isl_give isl_ast_graft *isl_ast_graft_copy( |
93 | | __isl_keep isl_ast_graft *graft) |
94 | 40.3k | { |
95 | 40.3k | if (!graft) |
96 | 0 | return NULL; |
97 | 40.3k | |
98 | 40.3k | graft->ref++; |
99 | 40.3k | return graft; |
100 | 40.3k | } |
101 | | |
102 | | /* Do all the grafts in "list" have the same guard and is this guard |
103 | | * independent of the current depth? |
104 | | */ |
105 | | static int equal_independent_guards(__isl_keep isl_ast_graft_list *list, |
106 | | __isl_keep isl_ast_build *build) |
107 | 4.12k | { |
108 | 4.12k | int i, n; |
109 | 4.12k | int depth; |
110 | 4.12k | isl_ast_graft *graft_0; |
111 | 4.12k | int equal = 1; |
112 | 4.12k | int skip; |
113 | 4.12k | |
114 | 4.12k | graft_0 = isl_ast_graft_list_get_ast_graft(list, 0); |
115 | 4.12k | if (!graft_0) |
116 | 0 | return -1; |
117 | 4.12k | |
118 | 4.12k | depth = isl_ast_build_get_depth(build); |
119 | 4.12k | if (isl_set_dim(graft_0->guard, isl_dim_set) <= depth) |
120 | 35 | skip = 0; |
121 | 4.09k | else |
122 | 4.09k | skip = isl_set_involves_dims(graft_0->guard, |
123 | 4.09k | isl_dim_set, depth, 1); |
124 | 4.12k | if (skip < 0 || skip) { |
125 | 17 | isl_ast_graft_free(graft_0); |
126 | 17 | return skip < 0 ? -10 : 0; |
127 | 17 | } |
128 | 4.11k | |
129 | 4.11k | n = isl_ast_graft_list_n_ast_graft(list); |
130 | 5.50k | for (i = 1; i < n; ++i1.39k ) { |
131 | 1.47k | isl_ast_graft *graft; |
132 | 1.47k | graft = isl_ast_graft_list_get_ast_graft(list, i); |
133 | 1.47k | if (!graft) |
134 | 0 | equal = -1; |
135 | 1.47k | else |
136 | 1.47k | equal = isl_set_is_equal(graft_0->guard, graft->guard); |
137 | 1.47k | isl_ast_graft_free(graft); |
138 | 1.47k | if (equal < 0 || !equal) |
139 | 73 | break; |
140 | 1.47k | } |
141 | 4.11k | |
142 | 4.11k | isl_ast_graft_free(graft_0); |
143 | 4.11k | |
144 | 4.11k | return equal; |
145 | 4.11k | } |
146 | | |
147 | | /* Hoist "guard" out of the current level (given by "build"). |
148 | | * |
149 | | * In particular, eliminate the dimension corresponding to the current depth. |
150 | | */ |
151 | | static __isl_give isl_set *hoist_guard(__isl_take isl_set *guard, |
152 | | __isl_keep isl_ast_build *build) |
153 | 90 | { |
154 | 90 | int depth; |
155 | 90 | |
156 | 90 | depth = isl_ast_build_get_depth(build); |
157 | 90 | if (depth < isl_set_dim(guard, isl_dim_set)) { |
158 | 73 | guard = isl_set_remove_divs_involving_dims(guard, |
159 | 73 | isl_dim_set, depth, 1); |
160 | 73 | guard = isl_set_eliminate(guard, isl_dim_set, depth, 1); |
161 | 73 | guard = isl_set_compute_divs(guard); |
162 | 73 | } |
163 | 90 | |
164 | 90 | return guard; |
165 | 90 | } |
166 | | |
167 | | /* Extract a common guard from the grafts in "list" that can be hoisted |
168 | | * out of the current level. If no such guard can be found, then return |
169 | | * a universal set. |
170 | | * |
171 | | * If all the grafts in the list have the same guard and if this guard |
172 | | * is independent of the current level, then it can be hoisted out. |
173 | | * If there is only one graft in the list and if its guard |
174 | | * depends on the current level, then we eliminate this level and |
175 | | * return the result. |
176 | | * |
177 | | * Otherwise, we return the unshifted simple hull of the guards. |
178 | | * In order to be able to hoist as many constraints as possible, |
179 | | * but at the same time avoid hoisting constraints that did not |
180 | | * appear in the guards in the first place, we intersect the guards |
181 | | * with all the information that is available (i.e., the domain |
182 | | * from the build and the enforced constraints of the graft) and |
183 | | * compute the unshifted hull of the result using only constraints |
184 | | * from the original guards. |
185 | | * In particular, intersecting the guards with other known information |
186 | | * allows us to hoist guards that are only explicit is some of |
187 | | * the grafts and implicit in the others. |
188 | | * |
189 | | * The special case for equal guards is needed in case those guards |
190 | | * are non-convex. Taking the simple hull would remove information |
191 | | * and would not allow for these guards to be hoisted completely. |
192 | | */ |
193 | | __isl_give isl_set *isl_ast_graft_list_extract_hoistable_guard( |
194 | | __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build) |
195 | 4.12k | { |
196 | 4.12k | int i, n; |
197 | 4.12k | int equal; |
198 | 4.12k | isl_ctx *ctx; |
199 | 4.12k | isl_set *guard; |
200 | 4.12k | isl_set_list *set_list; |
201 | 4.12k | isl_basic_set *hull; |
202 | 4.12k | |
203 | 4.12k | if (!list || !build) |
204 | 0 | return NULL; |
205 | 4.12k | |
206 | 4.12k | n = isl_ast_graft_list_n_ast_graft(list); |
207 | 4.12k | if (n == 0) |
208 | 0 | return isl_set_universe(isl_ast_build_get_space(build, 1)); |
209 | 4.12k | |
210 | 4.12k | equal = equal_independent_guards(list, build); |
211 | 4.12k | if (equal < 0) |
212 | 0 | return NULL; |
213 | 4.12k | |
214 | 4.12k | if (equal || n == 190 ) { |
215 | 4.03k | isl_ast_graft *graft_0; |
216 | 4.03k | |
217 | 4.03k | graft_0 = isl_ast_graft_list_get_ast_graft(list, 0); |
218 | 4.03k | if (!graft_0) |
219 | 0 | return NULL; |
220 | 4.03k | guard = isl_set_copy(graft_0->guard); |
221 | 4.03k | if (!equal) |
222 | 0 | guard = hoist_guard(guard, build); |
223 | 4.03k | isl_ast_graft_free(graft_0); |
224 | 4.03k | return guard; |
225 | 4.03k | } |
226 | 90 | |
227 | 90 | ctx = isl_ast_build_get_ctx(build); |
228 | 90 | set_list = isl_set_list_alloc(ctx, n); |
229 | 90 | guard = isl_set_empty(isl_ast_build_get_space(build, 1)); |
230 | 503 | for (i = 0; i < n; ++i413 ) { |
231 | 413 | isl_ast_graft *graft; |
232 | 413 | isl_basic_set *enforced; |
233 | 413 | isl_set *guard_i; |
234 | 413 | |
235 | 413 | graft = isl_ast_graft_list_get_ast_graft(list, i); |
236 | 413 | enforced = isl_ast_graft_get_enforced(graft); |
237 | 413 | guard_i = isl_set_copy(graft->guard); |
238 | 413 | isl_ast_graft_free(graft); |
239 | 413 | set_list = isl_set_list_add(set_list, isl_set_copy(guard_i)); |
240 | 413 | guard_i = isl_set_intersect(guard_i, |
241 | 413 | isl_set_from_basic_set(enforced)); |
242 | 413 | guard_i = isl_set_intersect(guard_i, |
243 | 413 | isl_ast_build_get_domain(build)); |
244 | 413 | guard = isl_set_union(guard, guard_i); |
245 | 413 | } |
246 | 90 | hull = isl_set_unshifted_simple_hull_from_set_list(guard, set_list); |
247 | 90 | guard = isl_set_from_basic_set(hull); |
248 | 90 | return hoist_guard(guard, build); |
249 | 90 | } |
250 | | |
251 | | /* Internal data structure used inside insert_if. |
252 | | * |
253 | | * list is the list of guarded nodes created by each call to insert_if. |
254 | | * node is the original node that is guarded by insert_if. |
255 | | * build is the build in which the AST is constructed. |
256 | | */ |
257 | | struct isl_insert_if_data { |
258 | | isl_ast_node_list *list; |
259 | | isl_ast_node *node; |
260 | | isl_ast_build *build; |
261 | | }; |
262 | | |
263 | | static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user); |
264 | | |
265 | | /* Insert an if node around "node" testing the condition encoded |
266 | | * in guard "guard". |
267 | | * |
268 | | * If the user does not want any disjunctions in the if conditions |
269 | | * and if "guard" does involve a disjunction, then we make the different |
270 | | * disjuncts disjoint and insert an if node corresponding to each disjunct |
271 | | * around a copy of "node". The result is then a block node containing |
272 | | * this sequence of guarded copies of "node". |
273 | | */ |
274 | | static __isl_give isl_ast_node *ast_node_insert_if( |
275 | | __isl_take isl_ast_node *node, __isl_take isl_set *guard, |
276 | | __isl_keep isl_ast_build *build) |
277 | 241 | { |
278 | 241 | struct isl_insert_if_data data; |
279 | 241 | isl_ctx *ctx; |
280 | 241 | |
281 | 241 | ctx = isl_ast_build_get_ctx(build); |
282 | 241 | if (isl_options_get_ast_build_allow_or(ctx) || |
283 | 241 | isl_set_n_basic_set(guard) <= 10 ) { |
284 | 241 | isl_ast_node *if_node; |
285 | 241 | isl_ast_expr *expr; |
286 | 241 | |
287 | 241 | expr = isl_ast_build_expr_from_set_internal(build, guard); |
288 | 241 | |
289 | 241 | if_node = isl_ast_node_alloc_if(expr); |
290 | 241 | return isl_ast_node_if_set_then(if_node, node); |
291 | 241 | } |
292 | 0 | |
293 | 0 | guard = isl_set_make_disjoint(guard); |
294 | 0 |
|
295 | 0 | data.list = isl_ast_node_list_alloc(ctx, 0); |
296 | 0 | data.node = node; |
297 | 0 | data.build = build; |
298 | 0 | if (isl_set_foreach_basic_set(guard, &insert_if, &data) < 0) |
299 | 0 | data.list = isl_ast_node_list_free(data.list); |
300 | 0 |
|
301 | 0 | isl_set_free(guard); |
302 | 0 | isl_ast_node_free(data.node); |
303 | 0 | return isl_ast_node_alloc_block(data.list); |
304 | 0 | } |
305 | | |
306 | | /* Insert an if node around a copy of "data->node" testing the condition |
307 | | * encoded in guard "bset" and add the result to data->list. |
308 | | */ |
309 | | static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user) |
310 | 0 | { |
311 | 0 | struct isl_insert_if_data *data = user; |
312 | 0 | isl_ast_node *node; |
313 | 0 | isl_set *set; |
314 | 0 |
|
315 | 0 | set = isl_set_from_basic_set(bset); |
316 | 0 | node = isl_ast_node_copy(data->node); |
317 | 0 | node = ast_node_insert_if(node, set, data->build); |
318 | 0 | data->list = isl_ast_node_list_add(data->list, node); |
319 | 0 |
|
320 | 0 | return isl_stat_ok; |
321 | 0 | } |
322 | | |
323 | | /* Insert an if node around graft->node testing the condition encoded |
324 | | * in guard "guard", assuming guard involves any conditions. |
325 | | */ |
326 | | static __isl_give isl_ast_graft *insert_if_node( |
327 | | __isl_take isl_ast_graft *graft, __isl_take isl_set *guard, |
328 | | __isl_keep isl_ast_build *build) |
329 | 6.36k | { |
330 | 6.36k | int univ; |
331 | 6.36k | |
332 | 6.36k | if (!graft) |
333 | 0 | goto error; |
334 | 6.36k | |
335 | 6.36k | univ = isl_set_plain_is_universe(guard); |
336 | 6.36k | if (univ < 0) |
337 | 0 | goto error; |
338 | 6.36k | if (univ) { |
339 | 6.12k | isl_set_free(guard); |
340 | 6.12k | return graft; |
341 | 6.12k | } |
342 | 241 | |
343 | 241 | build = isl_ast_build_copy(build); |
344 | 241 | graft->node = ast_node_insert_if(graft->node, guard, build); |
345 | 241 | isl_ast_build_free(build); |
346 | 241 | |
347 | 241 | if (!graft->node) |
348 | 0 | return isl_ast_graft_free(graft); |
349 | 241 | |
350 | 241 | return graft; |
351 | 0 | error: |
352 | 0 | isl_set_free(guard); |
353 | 0 | return isl_ast_graft_free(graft); |
354 | 241 | } |
355 | | |
356 | | /* Insert an if node around graft->node testing the condition encoded |
357 | | * in graft->guard, assuming graft->guard involves any conditions. |
358 | | */ |
359 | | static __isl_give isl_ast_graft *insert_pending_guard_node( |
360 | | __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build) |
361 | 6.36k | { |
362 | 6.36k | if (!graft) |
363 | 0 | return NULL; |
364 | 6.36k | |
365 | 6.36k | return insert_if_node(graft, isl_set_copy(graft->guard), build); |
366 | 6.36k | } |
367 | | |
368 | | /* Replace graft->enforced by "enforced". |
369 | | */ |
370 | | __isl_give isl_ast_graft *isl_ast_graft_set_enforced( |
371 | | __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced) |
372 | 197 | { |
373 | 197 | if (!graft || !enforced) |
374 | 0 | goto error; |
375 | 197 | |
376 | 197 | isl_basic_set_free(graft->enforced); |
377 | 197 | graft->enforced = enforced; |
378 | 197 | |
379 | 197 | return graft; |
380 | 0 | error: |
381 | 0 | isl_basic_set_free(enforced); |
382 | 0 | return isl_ast_graft_free(graft); |
383 | 197 | } |
384 | | |
385 | | /* Update "enforced" such that it only involves constraints that are |
386 | | * also enforced by "graft". |
387 | | */ |
388 | | static __isl_give isl_basic_set *update_enforced( |
389 | | __isl_take isl_basic_set *enforced, __isl_keep isl_ast_graft *graft, |
390 | | int depth) |
391 | 6.18k | { |
392 | 6.18k | isl_basic_set *enforced_g; |
393 | 6.18k | |
394 | 6.18k | enforced_g = isl_ast_graft_get_enforced(graft); |
395 | 6.18k | if (depth < isl_basic_set_dim(enforced_g, isl_dim_set)) |
396 | 5.99k | enforced_g = isl_basic_set_eliminate(enforced_g, |
397 | 5.99k | isl_dim_set, depth, 1); |
398 | 6.18k | enforced_g = isl_basic_set_remove_unknown_divs(enforced_g); |
399 | 6.18k | enforced_g = isl_basic_set_align_params(enforced_g, |
400 | 6.18k | isl_basic_set_get_space(enforced)); |
401 | 6.18k | enforced = isl_basic_set_align_params(enforced, |
402 | 6.18k | isl_basic_set_get_space(enforced_g)); |
403 | 6.18k | enforced = isl_set_simple_hull(isl_basic_set_union(enforced, |
404 | 6.18k | enforced_g)); |
405 | 6.18k | |
406 | 6.18k | return enforced; |
407 | 6.18k | } |
408 | | |
409 | | /* Extend the node at *body with node. |
410 | | * |
411 | | * If body points to the else branch, then *body may still be NULL. |
412 | | * If so, we simply attach node to this else branch. |
413 | | * Otherwise, we attach a list containing the statements already |
414 | | * attached at *body followed by node. |
415 | | */ |
416 | | static void extend_body(__isl_keep isl_ast_node **body, |
417 | | __isl_take isl_ast_node *node) |
418 | 197 | { |
419 | 197 | isl_ast_node_list *list; |
420 | 197 | |
421 | 197 | if (!*body) { |
422 | 31 | *body = node; |
423 | 31 | return; |
424 | 31 | } |
425 | 166 | |
426 | 166 | if ((*body)->type == isl_ast_node_block) { |
427 | 86 | list = isl_ast_node_block_get_children(*body); |
428 | 86 | isl_ast_node_free(*body); |
429 | 86 | } else |
430 | 80 | list = isl_ast_node_list_from_ast_node(*body); |
431 | 166 | list = isl_ast_node_list_add(list, node); |
432 | 166 | *body = isl_ast_node_alloc_block(list); |
433 | 166 | } |
434 | | |
435 | | /* Merge "graft" into the last graft of "list". |
436 | | * body points to the then or else branch of an if node in that last graft. |
437 | | * |
438 | | * We attach graft->node to this branch and update the enforced |
439 | | * set of the last graft of "list" to take into account the enforced |
440 | | * set of "graft". |
441 | | */ |
442 | | static __isl_give isl_ast_graft_list *graft_extend_body( |
443 | | __isl_take isl_ast_graft_list *list, |
444 | | __isl_keep isl_ast_node **body, __isl_take isl_ast_graft *graft, |
445 | | __isl_keep isl_ast_build *build) |
446 | 197 | { |
447 | 197 | int n; |
448 | 197 | int depth; |
449 | 197 | isl_ast_graft *last; |
450 | 197 | isl_space *space; |
451 | 197 | isl_basic_set *enforced; |
452 | 197 | |
453 | 197 | if (!list || !graft) |
454 | 0 | goto error; |
455 | 197 | extend_body(body, isl_ast_node_copy(graft->node)); |
456 | 197 | if (!*body) |
457 | 0 | goto error; |
458 | 197 | |
459 | 197 | n = isl_ast_graft_list_n_ast_graft(list); |
460 | 197 | last = isl_ast_graft_list_get_ast_graft(list, n - 1); |
461 | 197 | |
462 | 197 | depth = isl_ast_build_get_depth(build); |
463 | 197 | space = isl_ast_build_get_space(build, 1); |
464 | 197 | enforced = isl_basic_set_empty(space); |
465 | 197 | enforced = update_enforced(enforced, last, depth); |
466 | 197 | enforced = update_enforced(enforced, graft, depth); |
467 | 197 | last = isl_ast_graft_set_enforced(last, enforced); |
468 | 197 | |
469 | 197 | list = isl_ast_graft_list_set_ast_graft(list, n - 1, last); |
470 | 197 | isl_ast_graft_free(graft); |
471 | 197 | return list; |
472 | 0 | error: |
473 | 0 | isl_ast_graft_free(graft); |
474 | 0 | return isl_ast_graft_list_free(list); |
475 | 197 | } |
476 | | |
477 | | /* Merge "graft" into the last graft of "list", attaching graft->node |
478 | | * to the then branch of "last_if". |
479 | | */ |
480 | | static __isl_give isl_ast_graft_list *extend_then( |
481 | | __isl_take isl_ast_graft_list *list, |
482 | | __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft, |
483 | | __isl_keep isl_ast_build *build) |
484 | 162 | { |
485 | 162 | return graft_extend_body(list, &last_if->u.i.then, graft, build); |
486 | 162 | } |
487 | | |
488 | | /* Merge "graft" into the last graft of "list", attaching graft->node |
489 | | * to the else branch of "last_if". |
490 | | */ |
491 | | static __isl_give isl_ast_graft_list *extend_else( |
492 | | __isl_take isl_ast_graft_list *list, |
493 | | __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft, |
494 | | __isl_keep isl_ast_build *build) |
495 | 35 | { |
496 | 35 | return graft_extend_body(list, &last_if->u.i.else_node, graft, build); |
497 | 35 | } |
498 | | |
499 | | /* This data structure keeps track of an if node. |
500 | | * |
501 | | * "node" is the actual if-node |
502 | | * "guard" is the original, non-simplified guard of the node |
503 | | * "complement" is the complement of "guard" in the context of outer if nodes |
504 | | */ |
505 | | struct isl_if_node { |
506 | | isl_ast_node *node; |
507 | | isl_set *guard; |
508 | | isl_set *complement; |
509 | | }; |
510 | | |
511 | | /* Given a list of "n" if nodes, clear those starting at "first" |
512 | | * and return "first" (i.e., the updated size of the array). |
513 | | */ |
514 | | static int clear_if_nodes(struct isl_if_node *if_node, int first, int n) |
515 | 4.82k | { |
516 | 4.82k | int i; |
517 | 4.82k | |
518 | 4.97k | for (i = first; i < n; ++i146 ) { |
519 | 146 | isl_set_free(if_node[i].guard); |
520 | 146 | isl_set_free(if_node[i].complement); |
521 | 146 | } |
522 | 4.82k | |
523 | 4.82k | return first; |
524 | 4.82k | } |
525 | | |
526 | | /* For each graft in "list", |
527 | | * insert an if node around graft->node testing the condition encoded |
528 | | * in graft->guard, assuming graft->guard involves any conditions. |
529 | | * |
530 | | * We keep track of a list of generated if nodes that can be extended |
531 | | * without changing the order of the elements in "list". |
532 | | * If the guard of a graft is a subset of either the guard or its complement |
533 | | * of one of those if nodes, then the node |
534 | | * of the new graft is inserted into the then or else branch of the last graft |
535 | | * and the current graft is discarded. |
536 | | * The guard of the node is then simplified based on the conditions |
537 | | * enforced at that then or else branch. |
538 | | * Otherwise, the current graft is appended to the list. |
539 | | * |
540 | | * We only construct else branches if allowed by the user. |
541 | | */ |
542 | | static __isl_give isl_ast_graft_list *insert_pending_guard_nodes( |
543 | | __isl_take isl_ast_graft_list *list, |
544 | | __isl_keep isl_ast_build *build) |
545 | 4.59k | { |
546 | 4.59k | int i, j, n, n_if; |
547 | 4.59k | int allow_else; |
548 | 4.59k | isl_ctx *ctx; |
549 | 4.59k | isl_ast_graft_list *res; |
550 | 4.59k | struct isl_if_node *if_node = NULL; |
551 | 4.59k | |
552 | 4.59k | if (!build || !list) |
553 | 0 | return isl_ast_graft_list_free(list); |
554 | 4.59k | |
555 | 4.59k | ctx = isl_ast_build_get_ctx(build); |
556 | 4.59k | n = isl_ast_graft_list_n_ast_graft(list); |
557 | 4.59k | |
558 | 4.59k | allow_else = isl_options_get_ast_build_allow_else(ctx); |
559 | 4.59k | |
560 | 4.59k | n_if = 0; |
561 | 4.59k | if (n > 1) { |
562 | 352 | if_node = isl_alloc_array(ctx, struct isl_if_node, n - 1); |
563 | 352 | if (!if_node) |
564 | 0 | return isl_ast_graft_list_free(list); |
565 | 4.59k | } |
566 | 4.59k | |
567 | 4.59k | res = isl_ast_graft_list_alloc(ctx, n); |
568 | 4.59k | |
569 | 10.9k | for (i = 0; i < n; ++i6.36k ) { |
570 | 6.36k | isl_set *guard; |
571 | 6.36k | isl_ast_graft *graft; |
572 | 6.36k | int subset, found_then, found_else; |
573 | 6.36k | isl_ast_node *node; |
574 | 6.36k | |
575 | 6.36k | graft = isl_ast_graft_list_get_ast_graft(list, i); |
576 | 6.36k | if (!graft) |
577 | 0 | break; |
578 | 6.36k | subset = 0; |
579 | 6.36k | found_then = found_else = -1; |
580 | 6.36k | if (n_if > 0) { |
581 | 233 | isl_set *test; |
582 | 233 | test = isl_set_copy(graft->guard); |
583 | 233 | test = isl_set_intersect(test, |
584 | 233 | isl_set_copy(build->domain)); |
585 | 274 | for (j = n_if - 1; j >= 0; --j41 ) { |
586 | 238 | subset = isl_set_is_subset(test, |
587 | 238 | if_node[j].guard); |
588 | 238 | if (subset < 0 || subset) { |
589 | 162 | found_then = j; |
590 | 162 | break; |
591 | 162 | } |
592 | 76 | if (!allow_else) |
593 | 0 | continue; |
594 | 76 | subset = isl_set_is_subset(test, |
595 | 76 | if_node[j].complement); |
596 | 76 | if (subset < 0 || subset) { |
597 | 35 | found_else = j; |
598 | 35 | break; |
599 | 35 | } |
600 | 76 | } |
601 | 233 | n_if = clear_if_nodes(if_node, j + 1, n_if); |
602 | 233 | isl_set_free(test); |
603 | 233 | } |
604 | 6.36k | if (subset < 0) { |
605 | 0 | graft = isl_ast_graft_free(graft); |
606 | 0 | break; |
607 | 0 | } |
608 | 6.36k | |
609 | 6.36k | guard = isl_set_copy(graft->guard); |
610 | 6.36k | if (found_then >= 0) |
611 | 162 | graft->guard = isl_set_gist(graft->guard, |
612 | 162 | isl_set_copy(if_node[found_then].guard)); |
613 | 6.20k | else if (found_else >= 0) |
614 | 35 | graft->guard = isl_set_gist(graft->guard, |
615 | 35 | isl_set_copy(if_node[found_else].complement)); |
616 | 6.36k | |
617 | 6.36k | node = graft->node; |
618 | 6.36k | if (!graft->guard) |
619 | 0 | graft = isl_ast_graft_free(graft); |
620 | 6.36k | graft = insert_pending_guard_node(graft, build); |
621 | 6.36k | if (graft && graft->node != node && i != n - 1241 ) { |
622 | 146 | isl_set *set; |
623 | 146 | if_node[n_if].node = graft->node; |
624 | 146 | if_node[n_if].guard = guard; |
625 | 146 | if (found_then >= 0) |
626 | 55 | set = if_node[found_then].guard; |
627 | 91 | else if (found_else >= 0) |
628 | 5 | set = if_node[found_else].complement; |
629 | 86 | else |
630 | 86 | set = build->domain; |
631 | 146 | set = isl_set_copy(set); |
632 | 146 | set = isl_set_subtract(set, isl_set_copy(guard)); |
633 | 146 | if_node[n_if].complement = set; |
634 | 146 | n_if++; |
635 | 146 | } else |
636 | 6.22k | isl_set_free(guard); |
637 | 6.36k | if (!graft) |
638 | 0 | break; |
639 | 6.36k | |
640 | 6.36k | if (found_then >= 0) |
641 | 162 | res = extend_then(res, if_node[found_then].node, |
642 | 162 | graft, build); |
643 | 6.20k | else if (found_else >= 0) |
644 | 35 | res = extend_else(res, if_node[found_else].node, |
645 | 35 | graft, build); |
646 | 6.17k | else |
647 | 6.17k | res = isl_ast_graft_list_add(res, graft); |
648 | 6.36k | } |
649 | 4.59k | if (i < n) |
650 | 0 | res = isl_ast_graft_list_free(res); |
651 | 4.59k | |
652 | 4.59k | isl_ast_graft_list_free(list); |
653 | 4.59k | clear_if_nodes(if_node, 0, n_if); |
654 | 4.59k | free(if_node); |
655 | 4.59k | return res; |
656 | 4.59k | } |
657 | | |
658 | | /* For each graft in "list", |
659 | | * insert an if node around graft->node testing the condition encoded |
660 | | * in graft->guard, assuming graft->guard involves any conditions. |
661 | | * Subsequently remove the guards from the grafts. |
662 | | */ |
663 | | __isl_give isl_ast_graft_list *isl_ast_graft_list_insert_pending_guard_nodes( |
664 | | __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build) |
665 | 0 | { |
666 | 0 | int i, n; |
667 | 0 | isl_set *universe; |
668 | 0 |
|
669 | 0 | list = insert_pending_guard_nodes(list, build); |
670 | 0 | if (!list) |
671 | 0 | return NULL; |
672 | 0 | |
673 | 0 | universe = isl_set_universe(isl_ast_build_get_space(build, 1)); |
674 | 0 | n = isl_ast_graft_list_n_ast_graft(list); |
675 | 0 | for (i = 0; i < n; ++i) { |
676 | 0 | isl_ast_graft *graft; |
677 | 0 |
|
678 | 0 | graft = isl_ast_graft_list_get_ast_graft(list, i); |
679 | 0 | if (!graft) |
680 | 0 | break; |
681 | 0 | isl_set_free(graft->guard); |
682 | 0 | graft->guard = isl_set_copy(universe); |
683 | 0 | if (!graft->guard) |
684 | 0 | graft = isl_ast_graft_free(graft); |
685 | 0 | list = isl_ast_graft_list_set_ast_graft(list, i, graft); |
686 | 0 | } |
687 | 0 | isl_set_free(universe); |
688 | 0 | if (i < n) |
689 | 0 | return isl_ast_graft_list_free(list); |
690 | 0 | |
691 | 0 | return list; |
692 | 0 | } |
693 | | |
694 | | /* Collect the nodes contained in the grafts in "list" in a node list. |
695 | | */ |
696 | | static __isl_give isl_ast_node_list *extract_node_list( |
697 | | __isl_keep isl_ast_graft_list *list) |
698 | 4.59k | { |
699 | 4.59k | int i, n; |
700 | 4.59k | isl_ctx *ctx; |
701 | 4.59k | isl_ast_node_list *node_list; |
702 | 4.59k | |
703 | 4.59k | if (!list) |
704 | 0 | return NULL; |
705 | 4.59k | ctx = isl_ast_graft_list_get_ctx(list); |
706 | 4.59k | n = isl_ast_graft_list_n_ast_graft(list); |
707 | 4.59k | node_list = isl_ast_node_list_alloc(ctx, n); |
708 | 10.7k | for (i = 0; i < n; ++i6.17k ) { |
709 | 6.17k | isl_ast_node *node; |
710 | 6.17k | isl_ast_graft *graft; |
711 | 6.17k | |
712 | 6.17k | graft = isl_ast_graft_list_get_ast_graft(list, i); |
713 | 6.17k | node = isl_ast_graft_get_node(graft); |
714 | 6.17k | node_list = isl_ast_node_list_add(node_list, node); |
715 | 6.17k | isl_ast_graft_free(graft); |
716 | 6.17k | } |
717 | 4.59k | |
718 | 4.59k | return node_list; |
719 | 4.59k | } |
720 | | |
721 | | /* Look for shared enforced constraints by all the elements in "list" |
722 | | * on outer loops (with respect to the current depth) and return the result. |
723 | | * |
724 | | * If there are no elements in "list", then return the empty set. |
725 | | */ |
726 | | __isl_give isl_basic_set *isl_ast_graft_list_extract_shared_enforced( |
727 | | __isl_keep isl_ast_graft_list *list, |
728 | | __isl_keep isl_ast_build *build) |
729 | 4.12k | { |
730 | 4.12k | int i, n; |
731 | 4.12k | int depth; |
732 | 4.12k | isl_space *space; |
733 | 4.12k | isl_basic_set *enforced; |
734 | 4.12k | |
735 | 4.12k | if (!list) |
736 | 0 | return NULL; |
737 | 4.12k | |
738 | 4.12k | space = isl_ast_build_get_space(build, 1); |
739 | 4.12k | enforced = isl_basic_set_empty(space); |
740 | 4.12k | |
741 | 4.12k | depth = isl_ast_build_get_depth(build); |
742 | 4.12k | n = isl_ast_graft_list_n_ast_graft(list); |
743 | 9.91k | for (i = 0; i < n; ++i5.78k ) { |
744 | 5.78k | isl_ast_graft *graft; |
745 | 5.78k | |
746 | 5.78k | graft = isl_ast_graft_list_get_ast_graft(list, i); |
747 | 5.78k | enforced = update_enforced(enforced, graft, depth); |
748 | 5.78k | isl_ast_graft_free(graft); |
749 | 5.78k | } |
750 | 4.12k | |
751 | 4.12k | return enforced; |
752 | 4.12k | } |
753 | | |
754 | | /* Record "guard" in "graft" so that it will be enforced somewhere |
755 | | * up the tree. If the graft already has a guard, then it may be partially |
756 | | * redundant in combination with the new guard and in the context |
757 | | * the generated constraints of "build". In fact, the new guard |
758 | | * may in itself have some redundant constraints. |
759 | | * We therefore (re)compute the gist of the intersection |
760 | | * and coalesce the result. |
761 | | */ |
762 | | static __isl_give isl_ast_graft *store_guard(__isl_take isl_ast_graft *graft, |
763 | | __isl_take isl_set *guard, __isl_keep isl_ast_build *build) |
764 | 6.37k | { |
765 | 6.37k | int is_universe; |
766 | 6.37k | |
767 | 6.37k | if (!graft) |
768 | 0 | goto error; |
769 | 6.37k | |
770 | 6.37k | is_universe = isl_set_plain_is_universe(guard); |
771 | 6.37k | if (is_universe < 0) |
772 | 0 | goto error; |
773 | 6.37k | if (is_universe) { |
774 | 5.13k | isl_set_free(guard); |
775 | 5.13k | return graft; |
776 | 5.13k | } |
777 | 1.24k | |
778 | 1.24k | graft->guard = isl_set_intersect(graft->guard, guard); |
779 | 1.24k | graft->guard = isl_set_gist(graft->guard, |
780 | 1.24k | isl_ast_build_get_generated(build)); |
781 | 1.24k | graft->guard = isl_set_coalesce(graft->guard); |
782 | 1.24k | if (!graft->guard) |
783 | 0 | return isl_ast_graft_free(graft); |
784 | 1.24k | |
785 | 1.24k | return graft; |
786 | 0 | error: |
787 | 0 | isl_set_free(guard); |
788 | 0 | return isl_ast_graft_free(graft); |
789 | 1.24k | } |
790 | | |
791 | | /* For each graft in "list", replace its guard with the gist with |
792 | | * respect to "context". |
793 | | */ |
794 | | static __isl_give isl_ast_graft_list *gist_guards( |
795 | | __isl_take isl_ast_graft_list *list, __isl_keep isl_set *context) |
796 | 4.13k | { |
797 | 4.13k | int i, n; |
798 | 4.13k | |
799 | 4.13k | if (!list) |
800 | 0 | return NULL; |
801 | 4.13k | |
802 | 4.13k | n = isl_ast_graft_list_n_ast_graft(list); |
803 | 9.93k | for (i = 0; i < n; ++i5.79k ) { |
804 | 5.79k | isl_ast_graft *graft; |
805 | 5.79k | |
806 | 5.79k | graft = isl_ast_graft_list_get_ast_graft(list, i); |
807 | 5.79k | if (!graft) |
808 | 0 | break; |
809 | 5.79k | graft->guard = isl_set_gist(graft->guard, |
810 | 5.79k | isl_set_copy(context)); |
811 | 5.79k | if (!graft->guard) |
812 | 0 | graft = isl_ast_graft_free(graft); |
813 | 5.79k | list = isl_ast_graft_list_set_ast_graft(list, i, graft); |
814 | 5.79k | } |
815 | 4.13k | if (i < n) |
816 | 0 | return isl_ast_graft_list_free(list); |
817 | 4.13k | |
818 | 4.13k | return list; |
819 | 4.13k | } |
820 | | |
821 | | /* For each graft in "list", replace its guard with the gist with |
822 | | * respect to "context". |
823 | | */ |
824 | | __isl_give isl_ast_graft_list *isl_ast_graft_list_gist_guards( |
825 | | __isl_take isl_ast_graft_list *list, __isl_take isl_set *context) |
826 | 7 | { |
827 | 7 | list = gist_guards(list, context); |
828 | 7 | isl_set_free(context); |
829 | 7 | |
830 | 7 | return list; |
831 | 7 | } |
832 | | |
833 | | /* Allocate a graft in "build" based on the list of grafts in "sub_build". |
834 | | * "guard" and "enforced" are the guard and enforced constraints |
835 | | * of the allocated graft. The guard is used to simplify the guards |
836 | | * of the elements in "list". |
837 | | * |
838 | | * The node is initialized to either a block containing the nodes of "children" |
839 | | * or, if there is only a single child, the node of that child. |
840 | | * If the current level requires a for node, it should be inserted by |
841 | | * a subsequent call to isl_ast_graft_insert_for. |
842 | | */ |
843 | | __isl_give isl_ast_graft *isl_ast_graft_alloc_from_children( |
844 | | __isl_take isl_ast_graft_list *list, __isl_take isl_set *guard, |
845 | | __isl_take isl_basic_set *enforced, __isl_keep isl_ast_build *build, |
846 | | __isl_keep isl_ast_build *sub_build) |
847 | 4.12k | { |
848 | 4.12k | isl_ast_build *guard_build; |
849 | 4.12k | isl_ast_node *node; |
850 | 4.12k | isl_ast_node_list *node_list; |
851 | 4.12k | isl_ast_graft *graft; |
852 | 4.12k | |
853 | 4.12k | guard_build = isl_ast_build_copy(sub_build); |
854 | 4.12k | guard_build = isl_ast_build_replace_pending_by_guard(guard_build, |
855 | 4.12k | isl_set_copy(guard)); |
856 | 4.12k | list = gist_guards(list, guard); |
857 | 4.12k | list = insert_pending_guard_nodes(list, guard_build); |
858 | 4.12k | isl_ast_build_free(guard_build); |
859 | 4.12k | |
860 | 4.12k | node_list = extract_node_list(list); |
861 | 4.12k | node = isl_ast_node_from_ast_node_list(node_list); |
862 | 4.12k | isl_ast_graft_list_free(list); |
863 | 4.12k | |
864 | 4.12k | graft = isl_ast_graft_alloc(node, build); |
865 | 4.12k | graft = store_guard(graft, guard, build); |
866 | 4.12k | graft = isl_ast_graft_enforce(graft, enforced); |
867 | 4.12k | |
868 | 4.12k | return graft; |
869 | 4.12k | } |
870 | | |
871 | | /* Combine the grafts in the list into a single graft. |
872 | | * |
873 | | * The guard is initialized to the shared guard of the list elements (if any), |
874 | | * provided it does not depend on the current dimension. |
875 | | * The guards in the elements are then simplified with respect to the |
876 | | * hoisted guard and materialized as if nodes around the contained AST nodes |
877 | | * in the context of "sub_build". |
878 | | * |
879 | | * The enforced set is initialized to the simple hull of the enforced sets |
880 | | * of the elements, provided the ast_build_exploit_nested_bounds option is set |
881 | | * or the new graft will be used at the same level. |
882 | | * |
883 | | * The node is initialized to either a block containing the nodes of "list" |
884 | | * or, if there is only a single element, the node of that element. |
885 | | */ |
886 | | static __isl_give isl_ast_graft *ast_graft_list_fuse( |
887 | | __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build) |
888 | 62 | { |
889 | 62 | isl_ast_graft *graft; |
890 | 62 | isl_basic_set *enforced; |
891 | 62 | isl_set *guard; |
892 | 62 | |
893 | 62 | if (!list) |
894 | 0 | return NULL; |
895 | 62 | |
896 | 62 | enforced = isl_ast_graft_list_extract_shared_enforced(list, build); |
897 | 62 | guard = isl_ast_graft_list_extract_hoistable_guard(list, build); |
898 | 62 | graft = isl_ast_graft_alloc_from_children(list, guard, enforced, |
899 | 62 | build, build); |
900 | 62 | |
901 | 62 | return graft; |
902 | 62 | } |
903 | | |
904 | | /* Combine the grafts in the list into a single graft. |
905 | | * Return a list containing this single graft. |
906 | | * If the original list is empty, then return an empty list. |
907 | | */ |
908 | | __isl_give isl_ast_graft_list *isl_ast_graft_list_fuse( |
909 | | __isl_take isl_ast_graft_list *list, |
910 | | __isl_keep isl_ast_build *build) |
911 | 1.05k | { |
912 | 1.05k | isl_ast_graft *graft; |
913 | 1.05k | |
914 | 1.05k | if (!list) |
915 | 0 | return NULL; |
916 | 1.05k | if (isl_ast_graft_list_n_ast_graft(list) <= 1) |
917 | 1.01k | return list; |
918 | 37 | graft = ast_graft_list_fuse(list, build); |
919 | 37 | return isl_ast_graft_list_from_ast_graft(graft); |
920 | 37 | } |
921 | | |
922 | | /* Combine the two grafts into a single graft. |
923 | | * Return a list containing this single graft. |
924 | | */ |
925 | | static __isl_give isl_ast_graft *isl_ast_graft_fuse( |
926 | | __isl_take isl_ast_graft *graft1, __isl_take isl_ast_graft *graft2, |
927 | | __isl_keep isl_ast_build *build) |
928 | 25 | { |
929 | 25 | isl_ctx *ctx; |
930 | 25 | isl_ast_graft_list *list; |
931 | 25 | |
932 | 25 | ctx = isl_ast_build_get_ctx(build); |
933 | 25 | |
934 | 25 | list = isl_ast_graft_list_alloc(ctx, 2); |
935 | 25 | list = isl_ast_graft_list_add(list, graft1); |
936 | 25 | list = isl_ast_graft_list_add(list, graft2); |
937 | 25 | |
938 | 25 | return ast_graft_list_fuse(list, build); |
939 | 25 | } |
940 | | |
941 | | /* Insert a for node enclosing the current graft->node. |
942 | | */ |
943 | | __isl_give isl_ast_graft *isl_ast_graft_insert_for( |
944 | | __isl_take isl_ast_graft *graft, __isl_take isl_ast_node *node) |
945 | 749 | { |
946 | 749 | if (!graft) |
947 | 0 | goto error; |
948 | 749 | |
949 | 749 | graft->node = isl_ast_node_for_set_body(node, graft->node); |
950 | 749 | if (!graft->node) |
951 | 0 | return isl_ast_graft_free(graft); |
952 | 749 | |
953 | 749 | return graft; |
954 | 0 | error: |
955 | 0 | isl_ast_node_free(node); |
956 | 0 | isl_ast_graft_free(graft); |
957 | 0 | return NULL; |
958 | 749 | } |
959 | | |
960 | | /* Insert a mark governing the current graft->node. |
961 | | */ |
962 | | __isl_give isl_ast_graft *isl_ast_graft_insert_mark( |
963 | | __isl_take isl_ast_graft *graft, __isl_take isl_id *mark) |
964 | 179 | { |
965 | 179 | if (!graft) |
966 | 0 | goto error; |
967 | 179 | |
968 | 179 | graft->node = isl_ast_node_alloc_mark(mark, graft->node); |
969 | 179 | if (!graft->node) |
970 | 0 | return isl_ast_graft_free(graft); |
971 | 179 | |
972 | 179 | return graft; |
973 | 0 | error: |
974 | 0 | isl_id_free(mark); |
975 | 0 | isl_ast_graft_free(graft); |
976 | 0 | return NULL; |
977 | 179 | } |
978 | | |
979 | | /* Represent the graft list as an AST node. |
980 | | * This operation drops the information about guards in the grafts, so |
981 | | * if there are any pending guards, then they are materialized as if nodes. |
982 | | */ |
983 | | __isl_give isl_ast_node *isl_ast_node_from_graft_list( |
984 | | __isl_take isl_ast_graft_list *list, |
985 | | __isl_keep isl_ast_build *build) |
986 | 467 | { |
987 | 467 | isl_ast_node_list *node_list; |
988 | 467 | |
989 | 467 | list = insert_pending_guard_nodes(list, build); |
990 | 467 | node_list = extract_node_list(list); |
991 | 467 | isl_ast_graft_list_free(list); |
992 | 467 | |
993 | 467 | return isl_ast_node_from_ast_node_list(node_list); |
994 | 467 | } |
995 | | |
996 | | __isl_null isl_ast_graft *isl_ast_graft_free(__isl_take isl_ast_graft *graft) |
997 | 46.7k | { |
998 | 46.7k | if (!graft) |
999 | 0 | return NULL; |
1000 | 46.7k | |
1001 | 46.7k | if (--graft->ref > 0) |
1002 | 40.3k | return NULL; |
1003 | 6.36k | |
1004 | 6.36k | isl_ast_node_free(graft->node); |
1005 | 6.36k | isl_set_free(graft->guard); |
1006 | 6.36k | isl_basic_set_free(graft->enforced); |
1007 | 6.36k | free(graft); |
1008 | 6.36k | |
1009 | 6.36k | return NULL; |
1010 | 6.36k | } |
1011 | | |
1012 | | /* Record that the grafted tree enforces |
1013 | | * "enforced" by intersecting graft->enforced with "enforced". |
1014 | | */ |
1015 | | __isl_give isl_ast_graft *isl_ast_graft_enforce( |
1016 | | __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced) |
1017 | 4.87k | { |
1018 | 4.87k | if (!graft || !enforced) |
1019 | 0 | goto error; |
1020 | 4.87k | |
1021 | 4.87k | enforced = isl_basic_set_align_params(enforced, |
1022 | 4.87k | isl_basic_set_get_space(graft->enforced)); |
1023 | 4.87k | graft->enforced = isl_basic_set_align_params(graft->enforced, |
1024 | 4.87k | isl_basic_set_get_space(enforced)); |
1025 | 4.87k | graft->enforced = isl_basic_set_intersect(graft->enforced, enforced); |
1026 | 4.87k | if (!graft->enforced) |
1027 | 0 | return isl_ast_graft_free(graft); |
1028 | 4.87k | |
1029 | 4.87k | return graft; |
1030 | 0 | error: |
1031 | 0 | isl_basic_set_free(enforced); |
1032 | 0 | return isl_ast_graft_free(graft); |
1033 | 4.87k | } |
1034 | | |
1035 | | __isl_give isl_basic_set *isl_ast_graft_get_enforced( |
1036 | | __isl_keep isl_ast_graft *graft) |
1037 | 6.59k | { |
1038 | 6.59k | return graft ? isl_basic_set_copy(graft->enforced) : NULL; |
1039 | 6.59k | } |
1040 | | |
1041 | | __isl_give isl_set *isl_ast_graft_get_guard(__isl_keep isl_ast_graft *graft) |
1042 | 0 | { |
1043 | 0 | return graft ? isl_set_copy(graft->guard) : NULL; |
1044 | 0 | } |
1045 | | |
1046 | | /* Record that "guard" needs to be inserted in "graft". |
1047 | | */ |
1048 | | __isl_give isl_ast_graft *isl_ast_graft_add_guard( |
1049 | | __isl_take isl_ast_graft *graft, |
1050 | | __isl_take isl_set *guard, __isl_keep isl_ast_build *build) |
1051 | 2.25k | { |
1052 | 2.25k | return store_guard(graft, guard, build); |
1053 | 2.25k | } |
1054 | | |
1055 | | /* Reformulate the "graft", which was generated in the context |
1056 | | * of an inner code generation, in terms of the outer code generation |
1057 | | * AST build. |
1058 | | * |
1059 | | * If "product" is set, then the domain of the inner code generation build is |
1060 | | * |
1061 | | * [O -> S] |
1062 | | * |
1063 | | * with O the domain of the outer code generation build. |
1064 | | * We essentially need to project out S. |
1065 | | * |
1066 | | * If "product" is not set, then we need to project the domains onto |
1067 | | * their parameter spaces. |
1068 | | */ |
1069 | | __isl_give isl_ast_graft *isl_ast_graft_unembed(__isl_take isl_ast_graft *graft, |
1070 | | int product) |
1071 | 813 | { |
1072 | 813 | isl_basic_set *enforced; |
1073 | 813 | |
1074 | 813 | if (!graft) |
1075 | 0 | return NULL; |
1076 | 813 | |
1077 | 813 | if (product) { |
1078 | 805 | enforced = graft->enforced; |
1079 | 805 | enforced = isl_basic_map_domain(isl_basic_set_unwrap(enforced)); |
1080 | 805 | graft->enforced = enforced; |
1081 | 805 | graft->guard = isl_map_domain(isl_set_unwrap(graft->guard)); |
1082 | 805 | } else { |
1083 | 8 | graft->enforced = isl_basic_set_params(graft->enforced); |
1084 | 8 | graft->guard = isl_set_params(graft->guard); |
1085 | 8 | } |
1086 | 813 | graft->guard = isl_set_compute_divs(graft->guard); |
1087 | 813 | |
1088 | 813 | if (!graft->enforced || !graft->guard) |
1089 | 0 | return isl_ast_graft_free(graft); |
1090 | 813 | |
1091 | 813 | return graft; |
1092 | 813 | } |
1093 | | |
1094 | | /* Reformulate the grafts in "list", which were generated in the context |
1095 | | * of an inner code generation, in terms of the outer code generation |
1096 | | * AST build. |
1097 | | */ |
1098 | | __isl_give isl_ast_graft_list *isl_ast_graft_list_unembed( |
1099 | | __isl_take isl_ast_graft_list *list, int product) |
1100 | 684 | { |
1101 | 684 | int i, n; |
1102 | 684 | |
1103 | 684 | n = isl_ast_graft_list_n_ast_graft(list); |
1104 | 1.49k | for (i = 0; i < n; ++i813 ) { |
1105 | 813 | isl_ast_graft *graft; |
1106 | 813 | |
1107 | 813 | graft = isl_ast_graft_list_get_ast_graft(list, i); |
1108 | 813 | graft = isl_ast_graft_unembed(graft, product); |
1109 | 813 | list = isl_ast_graft_list_set_ast_graft(list, i, graft); |
1110 | 813 | } |
1111 | 684 | |
1112 | 684 | return list; |
1113 | 684 | } |
1114 | | |
1115 | | /* Compute the preimage of "graft" under the function represented by "ma". |
1116 | | * In other words, plug in "ma" in "enforced" and "guard" fields of "graft". |
1117 | | */ |
1118 | | __isl_give isl_ast_graft *isl_ast_graft_preimage_multi_aff( |
1119 | | __isl_take isl_ast_graft *graft, __isl_take isl_multi_aff *ma) |
1120 | 3 | { |
1121 | 3 | isl_basic_set *enforced; |
1122 | 3 | |
1123 | 3 | if (!graft) |
1124 | 0 | return NULL; |
1125 | 3 | |
1126 | 3 | enforced = graft->enforced; |
1127 | 3 | graft->enforced = isl_basic_set_preimage_multi_aff(enforced, |
1128 | 3 | isl_multi_aff_copy(ma)); |
1129 | 3 | graft->guard = isl_set_preimage_multi_aff(graft->guard, ma); |
1130 | 3 | |
1131 | 3 | if (!graft->enforced || !graft->guard) |
1132 | 0 | return isl_ast_graft_free(graft); |
1133 | 3 | |
1134 | 3 | return graft; |
1135 | 3 | } |
1136 | | |
1137 | | /* Compute the preimage of all the grafts in "list" under |
1138 | | * the function represented by "ma". |
1139 | | */ |
1140 | | __isl_give isl_ast_graft_list *isl_ast_graft_list_preimage_multi_aff( |
1141 | | __isl_take isl_ast_graft_list *list, __isl_take isl_multi_aff *ma) |
1142 | 3 | { |
1143 | 3 | int i, n; |
1144 | 3 | |
1145 | 3 | n = isl_ast_graft_list_n_ast_graft(list); |
1146 | 6 | for (i = 0; i < n; ++i3 ) { |
1147 | 3 | isl_ast_graft *graft; |
1148 | 3 | |
1149 | 3 | graft = isl_ast_graft_list_get_ast_graft(list, i); |
1150 | 3 | graft = isl_ast_graft_preimage_multi_aff(graft, |
1151 | 3 | isl_multi_aff_copy(ma)); |
1152 | 3 | list = isl_ast_graft_list_set_ast_graft(list, i, graft); |
1153 | 3 | } |
1154 | 3 | |
1155 | 3 | isl_multi_aff_free(ma); |
1156 | 3 | return list; |
1157 | 3 | } |
1158 | | |
1159 | | /* Compare two grafts based on their guards. |
1160 | | */ |
1161 | | static int cmp_graft(__isl_keep isl_ast_graft *a, __isl_keep isl_ast_graft *b, |
1162 | | void *user) |
1163 | 30 | { |
1164 | 30 | return isl_set_plain_cmp(a->guard, b->guard); |
1165 | 30 | } |
1166 | | |
1167 | | /* Order the elements in "list" based on their guards. |
1168 | | */ |
1169 | | __isl_give isl_ast_graft_list *isl_ast_graft_list_sort_guard( |
1170 | | __isl_take isl_ast_graft_list *list) |
1171 | 36 | { |
1172 | 36 | return isl_ast_graft_list_sort(list, &cmp_graft, NULL); |
1173 | 36 | } |
1174 | | |
1175 | | /* Merge the given two lists into a single list of grafts, |
1176 | | * merging grafts with the same guard into a single graft. |
1177 | | * |
1178 | | * "list2" has been sorted using isl_ast_graft_list_sort. |
1179 | | * "list1" may be the result of a previous call to isl_ast_graft_list_merge |
1180 | | * and may therefore not be completely sorted. |
1181 | | * |
1182 | | * The elements in "list2" need to be executed after those in "list1", |
1183 | | * but if the guard of a graft in "list2" is disjoint from the guards |
1184 | | * of some final elements in "list1", then it can be moved up to before |
1185 | | * those final elements. |
1186 | | * |
1187 | | * In particular, we look at each element g of "list2" in turn |
1188 | | * and move it up beyond elements of "list1" that would be sorted |
1189 | | * after g as long as each of these elements has a guard that is disjoint |
1190 | | * from that of g. |
1191 | | * |
1192 | | * We do not allow the second or any later element of "list2" to be moved |
1193 | | * before a previous elements of "list2" even if the reason that |
1194 | | * that element didn't move up further was that its guard was not disjoint |
1195 | | * from that of the previous element in "list1". |
1196 | | */ |
1197 | | __isl_give isl_ast_graft_list *isl_ast_graft_list_merge( |
1198 | | __isl_take isl_ast_graft_list *list1, |
1199 | | __isl_take isl_ast_graft_list *list2, |
1200 | | __isl_keep isl_ast_build *build) |
1201 | 203 | { |
1202 | 203 | int i, j, first; |
1203 | 203 | |
1204 | 203 | if (!list1 || !list2 || !build) |
1205 | 0 | goto error; |
1206 | 203 | if (list2->n == 0) { |
1207 | 0 | isl_ast_graft_list_free(list2); |
1208 | 0 | return list1; |
1209 | 0 | } |
1210 | 203 | if (list1->n == 0) { |
1211 | 153 | isl_ast_graft_list_free(list1); |
1212 | 153 | return list2; |
1213 | 153 | } |
1214 | 50 | |
1215 | 50 | first = 0; |
1216 | 101 | for (i = 0; i < list2->n; ++i51 ) { |
1217 | 51 | isl_ast_graft *graft; |
1218 | 51 | graft = isl_ast_graft_list_get_ast_graft(list2, i); |
1219 | 51 | if (!graft) |
1220 | 0 | break; |
1221 | 51 | |
1222 | 53 | for (j = list1->n; 51 j >= 0; --j2 ) { |
1223 | 53 | int cmp, disjoint; |
1224 | 53 | isl_ast_graft *graft_j; |
1225 | 53 | |
1226 | 53 | if (j == first) |
1227 | 2 | cmp = -1; |
1228 | 51 | else |
1229 | 51 | cmp = isl_set_plain_cmp(list1->p[j - 1]->guard, |
1230 | 51 | graft->guard); |
1231 | 53 | if (cmp > 0) { |
1232 | 10 | disjoint = isl_set_is_disjoint(graft->guard, |
1233 | 10 | list1->p[j - 1]->guard); |
1234 | 10 | if (disjoint < 0) { |
1235 | 0 | isl_ast_graft_free(graft); |
1236 | 0 | list1 = isl_ast_graft_list_free(list1); |
1237 | 0 | break; |
1238 | 0 | } |
1239 | 10 | if (!disjoint) |
1240 | 8 | cmp = -1; |
1241 | 10 | } |
1242 | 53 | if (cmp > 0) |
1243 | 2 | continue; |
1244 | 51 | if (cmp < 0) { |
1245 | 26 | list1 = isl_ast_graft_list_insert(list1, j, |
1246 | 26 | graft); |
1247 | 26 | break; |
1248 | 26 | } |
1249 | 25 | |
1250 | 25 | --j; |
1251 | 25 | |
1252 | 25 | graft_j = isl_ast_graft_list_get_ast_graft(list1, j); |
1253 | 25 | graft_j = isl_ast_graft_fuse(graft_j, graft, build); |
1254 | 25 | list1 = isl_ast_graft_list_set_ast_graft(list1, j, |
1255 | 25 | graft_j); |
1256 | 25 | break; |
1257 | 25 | } |
1258 | 51 | |
1259 | 51 | if (j < 0) { |
1260 | 0 | isl_ast_graft_free(graft); |
1261 | 0 | isl_die(isl_ast_build_get_ctx(build), |
1262 | 0 | isl_error_internal, |
1263 | 0 | "element failed to get inserted", break); |
1264 | 0 | } |
1265 | 51 | |
1266 | 51 | first = j + 1; |
1267 | 51 | if (!list1) |
1268 | 0 | break; |
1269 | 51 | } |
1270 | 50 | if (i < list2->n) |
1271 | 0 | list1 = isl_ast_graft_list_free(list1); |
1272 | 50 | isl_ast_graft_list_free(list2); |
1273 | 50 | |
1274 | 50 | return list1; |
1275 | 0 | error: |
1276 | 0 | isl_ast_graft_list_free(list1); |
1277 | 0 | isl_ast_graft_list_free(list2); |
1278 | 0 | return NULL; |
1279 | 50 | } |
1280 | | |
1281 | | __isl_give isl_printer *isl_printer_print_ast_graft(__isl_take isl_printer *p, |
1282 | | __isl_keep isl_ast_graft *graft) |
1283 | 0 | { |
1284 | 0 | if (!p) |
1285 | 0 | return NULL; |
1286 | 0 | if (!graft) |
1287 | 0 | return isl_printer_free(p); |
1288 | 0 | |
1289 | 0 | p = isl_printer_print_str(p, "("); |
1290 | 0 | p = isl_printer_print_str(p, "guard: "); |
1291 | 0 | p = isl_printer_print_set(p, graft->guard); |
1292 | 0 | p = isl_printer_print_str(p, ", "); |
1293 | 0 | p = isl_printer_print_str(p, "enforced: "); |
1294 | 0 | p = isl_printer_print_basic_set(p, graft->enforced); |
1295 | 0 | p = isl_printer_print_str(p, ", "); |
1296 | 0 | p = isl_printer_print_str(p, "node: "); |
1297 | 0 | p = isl_printer_print_ast_node(p, graft->node); |
1298 | 0 | p = isl_printer_print_str(p, ")"); |
1299 | 0 |
|
1300 | 0 | return p; |
1301 | 0 | } |