]>
Commit | Line | Data |
---|---|---|
f6cc3103 | 1 | /* Translation of ISL AST to Gimple. |
5624e564 | 2 | Copyright (C) 2014-2015 Free Software Foundation, Inc. |
f6cc3103 RG |
3 | Contributed by Roman Gareev <gareevroman@gmail.com>. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
4d776011 DE |
21 | #define USES_ISL |
22 | ||
f6cc3103 RG |
23 | #include "config.h" |
24 | ||
eae1a5d4 | 25 | #ifdef HAVE_isl |
f6cc3103 RG |
26 | |
27 | #include "system.h" | |
28 | #include "coretypes.h" | |
c7131fb2 | 29 | #include "backend.h" |
9fdcd34e | 30 | #include "cfghooks.h" |
40e23961 | 31 | #include "tree.h" |
c7131fb2 | 32 | #include "gimple.h" |
49b8fe6c | 33 | #include "params.h" |
c7131fb2 | 34 | #include "fold-const.h" |
2ecf4eca | 35 | #include "gimple-fold.h" |
f6cc3103 | 36 | #include "gimple-iterator.h" |
2ecf4eca AK |
37 | #include "gimplify.h" |
38 | #include "gimplify-me.h" | |
39 | #include "tree-eh.h" | |
f6cc3103 | 40 | #include "tree-ssa-loop.h" |
2ecf4eca AK |
41 | #include "tree-ssa-operands.h" |
42 | #include "tree-ssa-propagate.h" | |
f6cc3103 RG |
43 | #include "tree-pass.h" |
44 | #include "cfgloop.h" | |
45 | #include "tree-data-ref.h" | |
a78cfa7f RG |
46 | #include "tree-ssa-loop-manip.h" |
47 | #include "tree-scalar-evolution.h" | |
5493d313 | 48 | #include "gimple-ssa.h" |
74032f47 | 49 | #include "tree-phinodes.h" |
5493d313 | 50 | #include "tree-into-ssa.h" |
74032f47 | 51 | #include "ssa-iterators.h" |
1b38d3ec | 52 | #include "tree-cfg.h" |
65b016eb | 53 | #include "gimple-pretty-print.h" |
2ecf4eca AK |
54 | #include "cfganal.h" |
55 | #include "value-prof.h" | |
56 | ||
4d776011 DE |
57 | #include <isl/constraint.h> |
58 | #include <isl/set.h> | |
59 | #include <isl/union_set.h> | |
60 | #include <isl/map.h> | |
61 | #include <isl/union_map.h> | |
62 | #include <isl/ast_build.h> | |
4d776011 | 63 | #include <isl/val_gmp.h> |
4d776011 | 64 | |
cf98f0f4 | 65 | #include "graphite.h" |
4d776011 | 66 | |
2ecf4eca | 67 | #include <map> |
f6cc3103 | 68 | |
55d1bd59 RG |
69 | /* We always try to use signed 128 bit types, but fall back to smaller types |
70 | in case a platform does not provide types of these sizes. In the future we | |
71 | should use isl to derive the optimal type for each subexpression. */ | |
a78cfa7f | 72 | |
55d1bd59 RG |
73 | static int max_mode_int_precision = |
74 | GET_MODE_PRECISION (mode_for_size (MAX_FIXED_MODE_SIZE, MODE_INT, 0)); | |
75 | static int graphite_expression_type_precision = 128 <= max_mode_int_precision ? | |
76 | 128 : max_mode_int_precision; | |
a78cfa7f | 77 | |
574921c2 RG |
78 | struct ast_build_info |
79 | { | |
80 | ast_build_info() | |
81 | : is_parallelizable(false) | |
2ecf4eca | 82 | { } |
574921c2 RG |
83 | bool is_parallelizable; |
84 | }; | |
85 | ||
a78cfa7f RG |
86 | /* Converts a GMP constant VAL to a tree and returns it. */ |
87 | ||
88 | static tree | |
89 | gmp_cst_to_tree (tree type, mpz_t val) | |
90 | { | |
91 | tree t = type ? type : integer_type_node; | |
92 | mpz_t tmp; | |
93 | ||
94 | mpz_init (tmp); | |
95 | mpz_set (tmp, val); | |
96 | wide_int wi = wi::from_mpz (t, tmp, true); | |
97 | mpz_clear (tmp); | |
98 | ||
99 | return wide_int_to_tree (t, wi); | |
100 | } | |
101 | ||
102 | /* Verifies properties that GRAPHITE should maintain during translation. */ | |
103 | ||
104 | static inline void | |
105 | graphite_verify (void) | |
106 | { | |
b2b29377 MM |
107 | checking_verify_loop_structure (); |
108 | checking_verify_loop_closed_ssa (true); | |
a78cfa7f RG |
109 | } |
110 | ||
111 | /* IVS_PARAMS maps ISL's scattering and parameter identifiers | |
112 | to corresponding trees. */ | |
113 | ||
114 | typedef std::map<isl_id *, tree> ivs_params; | |
115 | ||
116 | /* Free all memory allocated for ISL's identifiers. */ | |
117 | ||
118 | void ivs_params_clear (ivs_params &ip) | |
119 | { | |
120 | std::map<isl_id *, tree>::iterator it; | |
121 | for (it = ip.begin (); | |
122 | it != ip.end (); it++) | |
123 | { | |
124 | isl_id_free (it->first); | |
125 | } | |
126 | } | |
127 | ||
050e1371 AK |
128 | class translate_isl_ast_to_gimple |
129 | { | |
130 | public: | |
bafcb153 | 131 | translate_isl_ast_to_gimple (sese_info_p r) |
65b016eb | 132 | : region (r), codegen_error (false) |
2ecf4eca | 133 | { } |
050e1371 AK |
134 | |
135 | /* Translates an ISL AST node NODE to GCC representation in the | |
136 | context of a SESE. */ | |
137 | edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node, | |
138 | edge next_e, ivs_params &ip); | |
139 | ||
140 | /* Translates an isl_ast_node_for to Gimple. */ | |
141 | edge translate_isl_ast_node_for (loop_p context_loop, | |
142 | __isl_keep isl_ast_node *node, | |
143 | edge next_e, ivs_params &ip); | |
144 | ||
145 | /* Create the loop for a isl_ast_node_for. | |
146 | ||
147 | - NEXT_E is the edge where new generated code should be attached. */ | |
148 | edge translate_isl_ast_for_loop (loop_p context_loop, | |
149 | __isl_keep isl_ast_node *node_for, | |
150 | edge next_e, | |
151 | tree type, tree lb, tree ub, | |
152 | ivs_params &ip); | |
153 | ||
154 | /* Translates an isl_ast_node_if to Gimple. */ | |
155 | edge translate_isl_ast_node_if (loop_p context_loop, | |
156 | __isl_keep isl_ast_node *node, | |
157 | edge next_e, ivs_params &ip); | |
158 | ||
159 | /* Translates an isl_ast_node_user to Gimple. | |
160 | ||
161 | FIXME: We should remove iv_map.create (loop->num + 1), if it is | |
162 | possible. */ | |
163 | edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node, | |
164 | edge next_e, ivs_params &ip); | |
165 | ||
166 | /* Translates an isl_ast_node_block to Gimple. */ | |
167 | edge translate_isl_ast_node_block (loop_p context_loop, | |
168 | __isl_keep isl_ast_node *node, | |
169 | edge next_e, ivs_params &ip); | |
170 | ||
171 | /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of | |
172 | type TYPE. */ | |
173 | tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, | |
174 | ivs_params &ip); | |
175 | ||
176 | /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of | |
177 | type TYPE. */ | |
178 | tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, | |
179 | ivs_params &ip); | |
180 | ||
181 | /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of | |
182 | type TYPE. */ | |
183 | tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, | |
184 | ivs_params &ip); | |
185 | ||
186 | /* Converts an isl_ast_expr_op expression E with unknown number of arguments | |
187 | to a GCC expression tree of type TYPE. */ | |
188 | tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, | |
189 | ivs_params &ip); | |
190 | ||
191 | /* Converts an ISL AST expression E back to a GCC expression tree of | |
192 | type TYPE. */ | |
193 | tree gcc_expression_from_isl_expression (tree type, | |
194 | __isl_take isl_ast_expr *, | |
195 | ivs_params &ip); | |
196 | ||
197 | /* Return the tree variable that corresponds to the given isl ast identifier | |
198 | expression (an isl_ast_expr of type isl_ast_expr_id). | |
199 | ||
200 | FIXME: We should replace blind conversation of id's type with derivation | |
201 | of the optimal type when we get the corresponding isl support. Blindly | |
202 | converting type sizes may be problematic when we switch to smaller | |
203 | types. */ | |
204 | tree gcc_expression_from_isl_ast_expr_id (tree type, | |
205 | __isl_keep isl_ast_expr *expr_id, | |
206 | ivs_params &ip); | |
207 | ||
208 | /* Converts an isl_ast_expr_int expression E to a GCC expression tree of | |
209 | type TYPE. */ | |
210 | tree gcc_expression_from_isl_expr_int (tree type, | |
211 | __isl_take isl_ast_expr *expr); | |
212 | ||
213 | /* Converts an isl_ast_expr_op expression E to a GCC expression tree of | |
214 | type TYPE. */ | |
215 | tree gcc_expression_from_isl_expr_op (tree type, | |
216 | __isl_take isl_ast_expr *expr, | |
217 | ivs_params &ip); | |
218 | ||
219 | /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an | |
220 | induction variable for the new LOOP. New LOOP is attached to CFG | |
221 | starting at ENTRY_EDGE. LOOP is inserted into the loop tree and | |
222 | becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds | |
223 | ISL's scattering name to the induction variable created for the | |
224 | loop of STMT. The new induction variable is inserted in the NEWIVS | |
225 | vector and is of type TYPE. */ | |
226 | struct loop *graphite_create_new_loop (edge entry_edge, | |
227 | __isl_keep isl_ast_node *node_for, | |
228 | loop_p outer, tree type, | |
229 | tree lb, tree ub, ivs_params &ip); | |
230 | ||
231 | /* All loops generated by create_empty_loop_on_edge have the form of | |
232 | a post-test loop: | |
233 | ||
234 | do | |
235 | ||
236 | { | |
237 | body of the loop; | |
238 | } while (lower bound < upper bound); | |
239 | ||
240 | We create a new if region protecting the loop to be executed, if | |
241 | the execution count is zero (lower bound > upper bound). */ | |
242 | edge graphite_create_new_loop_guard (edge entry_edge, | |
243 | __isl_keep isl_ast_node *node_for, | |
244 | tree *type, | |
245 | tree *lb, tree *ub, ivs_params &ip); | |
246 | ||
247 | /* Creates a new if region corresponding to ISL's cond. */ | |
248 | edge graphite_create_new_guard (edge entry_edge, | |
249 | __isl_take isl_ast_expr *if_cond, | |
250 | ivs_params &ip); | |
251 | ||
252 | /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction | |
253 | variables of the loops around GBB in SESE. | |
254 | ||
255 | FIXME: Instead of using a vec<tree> that maps each loop id to a possible | |
256 | chrec, we could consider using a map<int, tree> that maps loop ids to the | |
257 | corresponding tree expressions. */ | |
65ef70d6 | 258 | void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb, |
050e1371 | 259 | __isl_keep isl_ast_expr *user_expr, ivs_params &ip, |
bafcb153 | 260 | sese_l ®ion); |
65b016eb | 261 | |
2ecf4eca AK |
262 | /* Patch the missing arguments of the phi nodes. */ |
263 | ||
65b016eb AK |
264 | void translate_pending_phi_nodes (void); |
265 | ||
2ecf4eca AK |
266 | /* Add ISL's parameter identifiers and corresponding trees to ivs_params. */ |
267 | ||
268 | void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip); | |
269 | ||
270 | /* Get the maximal number of schedule dimensions in the scop SCOP. */ | |
271 | ||
272 | int get_max_schedule_dimensions (scop_p scop); | |
273 | ||
274 | /* Generates a build, which specifies the constraints on the parameters. */ | |
275 | ||
276 | __isl_give isl_ast_build *generate_isl_context (scop_p scop); | |
277 | ||
278 | /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions. | |
279 | ||
280 | For schedules with different dimensionality, the isl AST generator can not | |
281 | define an order and will just randomly choose an order. The solution to | |
282 | this problem is to extend all schedules to the maximal number of schedule | |
283 | dimensions (using '0's for the remaining values). */ | |
284 | ||
285 | __isl_give isl_map *extend_schedule (__isl_take isl_map *schedule, | |
286 | int nb_schedule_dims); | |
287 | ||
288 | /* Generates a schedule, which specifies an order used to | |
289 | visit elements in a domain. */ | |
290 | ||
291 | __isl_give isl_union_map *generate_isl_schedule (scop_p scop); | |
292 | ||
293 | /* Set the separate option for all dimensions. | |
294 | This helps to reduce control overhead. */ | |
295 | ||
296 | __isl_give isl_ast_build * set_options (__isl_take isl_ast_build *control, | |
297 | __isl_keep isl_union_map *schedule); | |
298 | ||
299 | /* Generate isl AST from schedule of SCOP. Also, collects IVS_PARAMS in | |
300 | IP. */ | |
301 | ||
302 | __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop, ivs_params &ip); | |
303 | ||
304 | ||
305 | /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The | |
306 | definition should flow into use, and the use should respect the loop-closed | |
307 | SSA form. */ | |
308 | ||
309 | bool is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb, | |
310 | bool loop_phi, tree old_name, basic_block old_bb) const; | |
311 | ||
312 | /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in | |
313 | NEW_BB from RENAME_MAP. LOOP_PHI is true when we want to rename OLD_NAME | |
314 | within a loop PHI instruction. */ | |
315 | ||
316 | tree get_rename (basic_block new_bb, tree old_name, | |
317 | basic_block old_bb, bool loop_phi) const; | |
318 | ||
319 | /* For ops which are scev_analyzeable, we can regenerate a new name from | |
320 | its scalar evolution around LOOP. */ | |
321 | ||
322 | tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop, | |
323 | basic_block new_bb, basic_block old_bb, | |
324 | vec<tree> iv_map); | |
325 | ||
326 | /* Returns a basic block that could correspond to where a constant was defined | |
327 | in the original code. In the original code OLD_BB had the definition, we | |
328 | need to find which basic block out of the copies of old_bb, in the new | |
329 | region, should a definition correspond to if it has to reach BB. */ | |
330 | ||
331 | basic_block get_def_bb_for_const (basic_block bb, basic_block old_bb) const; | |
332 | ||
333 | /* Get the new name of OP (from OLD_BB) to be used in NEW_BB. LOOP_PHI is | |
334 | true when we want to rename an OP within a loop PHI instruction. */ | |
335 | ||
336 | tree get_new_name (basic_block new_bb, tree op, | |
337 | basic_block old_bb, bool loop_phi) const; | |
338 | ||
339 | /* Collect all the operands of NEW_EXPR by recursively visiting each | |
340 | operand. */ | |
341 | ||
342 | void collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa); | |
343 | ||
344 | /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to | |
345 | NEW_PHI must be found unless they can be POSTPONEd for later. */ | |
346 | ||
21c7259c | 347 | bool copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb, |
2ecf4eca AK |
348 | gphi *new_phi, init_back_edge_pair_t &ibp_new_bb, |
349 | bool postpone); | |
350 | ||
351 | /* Copy loop phi nodes from BB to NEW_BB. */ | |
352 | ||
353 | bool copy_loop_phi_nodes (basic_block bb, basic_block new_bb); | |
354 | ||
d6416b38 AK |
355 | /* Add phi nodes to all merge points of all the diamonds enclosing the loop of |
356 | the close phi node PHI. */ | |
357 | ||
358 | bool add_close_phis_to_merge_points (gphi *old_phi, gphi *new_phi, | |
359 | tree default_value); | |
360 | ||
361 | tree add_close_phis_to_outer_loops (tree last_merge_name, edge merge_e, | |
362 | gimple *old_close_phi); | |
363 | ||
2ecf4eca AK |
364 | /* Copy all the loop-close phi args from BB to NEW_BB. */ |
365 | ||
366 | bool copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb, | |
367 | bool postpone); | |
368 | ||
369 | /* Copy loop close phi nodes from BB to NEW_BB. */ | |
370 | ||
371 | bool copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb); | |
372 | ||
373 | /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated | |
374 | region. If postpone is true and it isn't possible to copy any arg of PHI, | |
375 | the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later. | |
376 | Returns false if the copying was unsuccessful. */ | |
377 | ||
378 | bool copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map, | |
379 | bool postpone); | |
380 | ||
381 | /* Copy cond phi nodes from BB to NEW_BB. A cond-phi node is a basic block | |
382 | containing phi nodes coming from two predecessors, and none of them are back | |
383 | edges. */ | |
384 | ||
385 | bool copy_cond_phi_nodes (basic_block bb, basic_block new_bb, | |
386 | vec<tree> iv_map); | |
387 | ||
388 | /* Duplicates the statements of basic block BB into basic block NEW_BB | |
389 | and compute the new induction variables according to the IV_MAP. | |
390 | CODEGEN_ERROR is set when the code generation cannot continue. */ | |
391 | ||
392 | bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb, | |
393 | vec<tree> iv_map); | |
394 | ||
395 | /* Copies BB and includes in the copied BB all the statements that can | |
396 | be reached following the use-def chains from the memory accesses, | |
397 | and returns the next edge following this new block. codegen_error is | |
398 | set when the code generation cannot continue. */ | |
399 | ||
400 | edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e, | |
401 | vec<tree> iv_map); | |
402 | ||
2927ca4b AK |
403 | /* Given a basic block containing close-phi it returns the new basic block |
404 | where to insert a copy of the close-phi nodes. All the uses in close phis | |
405 | should come from a single loop otherwise it returns NULL. */ | |
406 | edge edge_for_new_close_phis (basic_block bb); | |
407 | ||
2ecf4eca AK |
408 | /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB. |
409 | DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates | |
410 | the other pred of OLD_BB as well. If no such basic block exists then it is | |
411 | NULL. NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it | |
412 | cannot be NULL. | |
413 | ||
414 | Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice | |
415 | versa. In this case DOMINATING_PRED = NULL. | |
416 | ||
417 | Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2. | |
418 | ||
419 | Returns true on successful copy of the args, false otherwise. */ | |
420 | ||
421 | bool add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2], | |
422 | edge old_bb_dominating_edge, | |
423 | edge old_bb_non_dominating_edge, | |
424 | gphi *phi, gphi *new_phi, | |
425 | basic_block new_bb); | |
426 | ||
427 | /* Renames the scalar uses of the statement COPY, using the substitution map | |
428 | RENAME_MAP, inserting the gimplification code at GSI_TGT, for the | |
429 | translation REGION, with the original copied statement in LOOP, and using | |
430 | the induction variable renaming map IV_MAP. Returns true when something | |
431 | has been renamed. codegen_error is set when the code generation cannot | |
432 | continue. */ | |
433 | ||
434 | bool rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt, | |
435 | basic_block old_bb, loop_p loop, vec<tree> iv_map); | |
436 | ||
437 | /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR). | |
438 | When OLD_NAME and EXPR are the same we assert. */ | |
439 | ||
440 | void set_rename (tree old_name, tree expr); | |
441 | ||
442 | /* Create new names for all the definitions created by COPY and add | |
443 | replacement mappings for each new name. */ | |
444 | ||
445 | void set_rename_for_each_def (gimple *stmt); | |
446 | ||
447 | /* Insert each statement from SEQ at its earliest insertion p. */ | |
448 | ||
449 | void gsi_insert_earliest (gimple_seq seq); | |
450 | ||
451 | /* Rename all the operands of NEW_EXPR by recursively visiting each | |
452 | operand. */ | |
453 | ||
454 | tree rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb); | |
455 | ||
456 | bool codegen_error_p () const | |
457 | { return codegen_error; } | |
458 | ||
459 | /* Prints NODE to FILE. */ | |
460 | ||
461 | void print_isl_ast_node (FILE *file, __isl_keep isl_ast_node *node, | |
462 | __isl_keep isl_ctx *ctx) const; | |
65b016eb | 463 | |
ff17c262 AK |
464 | /* Return true when OP is a constant tree. */ |
465 | ||
466 | bool is_constant (tree op) const | |
467 | { | |
468 | return TREE_CODE (op) == INTEGER_CST | |
469 | || TREE_CODE (op) == REAL_CST | |
470 | || TREE_CODE (op) == COMPLEX_CST | |
471 | || TREE_CODE (op) == VECTOR_CST; | |
472 | } | |
473 | ||
050e1371 | 474 | private: |
d6416b38 | 475 | /* The region to be translated. */ |
bafcb153 | 476 | sese_info_p region; |
65b016eb AK |
477 | |
478 | /* This flag is set when an error occurred during the translation of ISL AST | |
479 | to Gimple. */ | |
480 | bool codegen_error; | |
d6416b38 AK |
481 | |
482 | /* A vector of all the edges at if_condition merge points. */ | |
483 | auto_vec<edge, 2> merge_points; | |
050e1371 | 484 | }; |
a78cfa7f RG |
485 | |
486 | /* Return the tree variable that corresponds to the given isl ast identifier | |
6a7d8936 RG |
487 | expression (an isl_ast_expr of type isl_ast_expr_id). |
488 | ||
489 | FIXME: We should replace blind conversation of id's type with derivation | |
490 | of the optimal type when we get the corresponding isl support. Blindly | |
491 | converting type sizes may be problematic when we switch to smaller | |
492 | types. */ | |
a78cfa7f | 493 | |
050e1371 AK |
494 | tree |
495 | translate_isl_ast_to_gimple:: | |
6a7d8936 | 496 | gcc_expression_from_isl_ast_expr_id (tree type, |
610a61a7 | 497 | __isl_take isl_ast_expr *expr_id, |
a78cfa7f RG |
498 | ivs_params &ip) |
499 | { | |
500 | gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id); | |
501 | isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id); | |
502 | std::map<isl_id *, tree>::iterator res; | |
503 | res = ip.find (tmp_isl_id); | |
504 | isl_id_free (tmp_isl_id); | |
505 | gcc_assert (res != ip.end () && | |
050e1371 | 506 | "Could not map isl_id to tree expression"); |
a78cfa7f | 507 | isl_ast_expr_free (expr_id); |
74032f47 | 508 | tree t = res->second; |
24bc7503 | 509 | return fold_convert (type, t); |
a78cfa7f RG |
510 | } |
511 | ||
512 | /* Converts an isl_ast_expr_int expression E to a GCC expression tree of | |
513 | type TYPE. */ | |
514 | ||
050e1371 AK |
515 | tree |
516 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
517 | gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr) |
518 | { | |
519 | gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int); | |
520 | isl_val *val = isl_ast_expr_get_val (expr); | |
521 | mpz_t val_mpz_t; | |
522 | mpz_init (val_mpz_t); | |
523 | tree res; | |
524 | if (isl_val_get_num_gmp (val, val_mpz_t) == -1) | |
525 | res = NULL_TREE; | |
526 | else | |
527 | res = gmp_cst_to_tree (type, val_mpz_t); | |
528 | isl_val_free (val); | |
529 | isl_ast_expr_free (expr); | |
530 | mpz_clear (val_mpz_t); | |
531 | return res; | |
532 | } | |
533 | ||
534 | /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of | |
535 | type TYPE. */ | |
536 | ||
050e1371 AK |
537 | tree |
538 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
539 | binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip) |
540 | { | |
541 | isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); | |
542 | tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
543 | arg_expr = isl_ast_expr_get_op_arg (expr, 1); | |
544 | tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
610a61a7 | 545 | |
a78cfa7f RG |
546 | enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr); |
547 | isl_ast_expr_free (expr); | |
610a61a7 AK |
548 | |
549 | if (codegen_error) | |
550 | return NULL_TREE; | |
551 | ||
a78cfa7f RG |
552 | switch (expr_type) |
553 | { | |
554 | case isl_ast_op_add: | |
555 | return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
556 | ||
557 | case isl_ast_op_sub: | |
558 | return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
559 | ||
560 | case isl_ast_op_mul: | |
561 | return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
562 | ||
563 | case isl_ast_op_div: | |
610a61a7 AK |
564 | /* As ISL operates on arbitrary precision numbers, we may end up with |
565 | division by 2^64 that is folded to 0. */ | |
566 | if (integer_zerop (tree_rhs_expr)) | |
567 | { | |
568 | codegen_error = true; | |
569 | return NULL_TREE; | |
570 | } | |
a78cfa7f RG |
571 | return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr); |
572 | ||
c4bc3399 | 573 | case isl_ast_op_pdiv_q: |
610a61a7 AK |
574 | /* As ISL operates on arbitrary precision numbers, we may end up with |
575 | division by 2^64 that is folded to 0. */ | |
576 | if (integer_zerop (tree_rhs_expr)) | |
577 | { | |
578 | codegen_error = true; | |
579 | return NULL_TREE; | |
580 | } | |
c4bc3399 RG |
581 | return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr); |
582 | ||
71819f45 | 583 | #if HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS |
560d18d3 | 584 | /* ISL-0.15 or later. */ |
49ed2fa4 | 585 | case isl_ast_op_zdiv_r: |
71819f45 | 586 | #endif |
c4bc3399 | 587 | case isl_ast_op_pdiv_r: |
610a61a7 AK |
588 | /* As ISL operates on arbitrary precision numbers, we may end up with |
589 | division by 2^64 that is folded to 0. */ | |
590 | if (integer_zerop (tree_rhs_expr)) | |
591 | { | |
592 | codegen_error = true; | |
593 | return NULL_TREE; | |
594 | } | |
c4bc3399 RG |
595 | return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr); |
596 | ||
a78cfa7f | 597 | case isl_ast_op_fdiv_q: |
610a61a7 AK |
598 | /* As ISL operates on arbitrary precision numbers, we may end up with |
599 | division by 2^64 that is folded to 0. */ | |
600 | if (integer_zerop (tree_rhs_expr)) | |
601 | { | |
602 | codegen_error = true; | |
603 | return NULL_TREE; | |
604 | } | |
a78cfa7f RG |
605 | return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr); |
606 | ||
607 | case isl_ast_op_and: | |
608 | return fold_build2 (TRUTH_ANDIF_EXPR, type, | |
609 | tree_lhs_expr, tree_rhs_expr); | |
610 | ||
611 | case isl_ast_op_or: | |
612 | return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
613 | ||
614 | case isl_ast_op_eq: | |
615 | return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
616 | ||
617 | case isl_ast_op_le: | |
618 | return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
619 | ||
620 | case isl_ast_op_lt: | |
621 | return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
622 | ||
623 | case isl_ast_op_ge: | |
624 | return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
625 | ||
626 | case isl_ast_op_gt: | |
627 | return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr); | |
628 | ||
629 | default: | |
630 | gcc_unreachable (); | |
631 | } | |
632 | } | |
633 | ||
634 | /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of | |
635 | type TYPE. */ | |
636 | ||
050e1371 AK |
637 | tree |
638 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
639 | ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip) |
640 | { | |
641 | gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus); | |
642 | isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); | |
643 | tree tree_first_expr | |
644 | = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
645 | arg_expr = isl_ast_expr_get_op_arg (expr, 1); | |
646 | tree tree_second_expr | |
647 | = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
648 | arg_expr = isl_ast_expr_get_op_arg (expr, 2); | |
649 | tree tree_third_expr | |
650 | = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
651 | isl_ast_expr_free (expr); | |
610a61a7 AK |
652 | |
653 | if (codegen_error) | |
654 | return NULL_TREE; | |
a78cfa7f RG |
655 | return fold_build3 (COND_EXPR, type, tree_first_expr, |
656 | tree_second_expr, tree_third_expr); | |
657 | } | |
658 | ||
659 | /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of | |
660 | type TYPE. */ | |
661 | ||
050e1371 AK |
662 | tree |
663 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
664 | unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip) |
665 | { | |
666 | gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus); | |
667 | isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); | |
668 | tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
669 | isl_ast_expr_free (expr); | |
610a61a7 | 670 | return codegen_error ? NULL_TREE : fold_build1 (NEGATE_EXPR, type, tree_expr); |
a78cfa7f RG |
671 | } |
672 | ||
673 | /* Converts an isl_ast_expr_op expression E with unknown number of arguments | |
674 | to a GCC expression tree of type TYPE. */ | |
675 | ||
050e1371 AK |
676 | tree |
677 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
678 | nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip) |
679 | { | |
680 | enum tree_code op_code; | |
681 | switch (isl_ast_expr_get_op_type (expr)) | |
682 | { | |
683 | case isl_ast_op_max: | |
684 | op_code = MAX_EXPR; | |
685 | break; | |
686 | ||
687 | case isl_ast_op_min: | |
688 | op_code = MIN_EXPR; | |
689 | break; | |
690 | ||
691 | default: | |
692 | gcc_unreachable (); | |
693 | } | |
694 | isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0); | |
695 | tree res = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
610a61a7 AK |
696 | |
697 | if (codegen_error) | |
698 | { | |
699 | isl_ast_expr_free (expr); | |
700 | return NULL_TREE; | |
701 | } | |
702 | ||
a78cfa7f RG |
703 | int i; |
704 | for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++) | |
705 | { | |
706 | arg_expr = isl_ast_expr_get_op_arg (expr, i); | |
707 | tree t = gcc_expression_from_isl_expression (type, arg_expr, ip); | |
610a61a7 AK |
708 | |
709 | if (codegen_error) | |
710 | { | |
711 | isl_ast_expr_free (expr); | |
712 | return NULL_TREE; | |
713 | } | |
714 | ||
a78cfa7f RG |
715 | res = fold_build2 (op_code, type, res, t); |
716 | } | |
717 | isl_ast_expr_free (expr); | |
718 | return res; | |
719 | } | |
720 | ||
a78cfa7f RG |
721 | /* Converts an isl_ast_expr_op expression E to a GCC expression tree of |
722 | type TYPE. */ | |
723 | ||
050e1371 AK |
724 | tree |
725 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
726 | gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr, |
727 | ivs_params &ip) | |
728 | { | |
610a61a7 AK |
729 | if (codegen_error) |
730 | { | |
731 | isl_ast_expr_free (expr); | |
732 | return NULL_TREE; | |
733 | } | |
734 | ||
a78cfa7f RG |
735 | gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op); |
736 | switch (isl_ast_expr_get_op_type (expr)) | |
737 | { | |
738 | /* These isl ast expressions are not supported yet. */ | |
739 | case isl_ast_op_error: | |
740 | case isl_ast_op_call: | |
741 | case isl_ast_op_and_then: | |
742 | case isl_ast_op_or_else: | |
a78cfa7f RG |
743 | case isl_ast_op_select: |
744 | gcc_unreachable (); | |
745 | ||
746 | case isl_ast_op_max: | |
747 | case isl_ast_op_min: | |
748 | return nary_op_to_tree (type, expr, ip); | |
749 | ||
750 | case isl_ast_op_add: | |
751 | case isl_ast_op_sub: | |
752 | case isl_ast_op_mul: | |
753 | case isl_ast_op_div: | |
c4bc3399 RG |
754 | case isl_ast_op_pdiv_q: |
755 | case isl_ast_op_pdiv_r: | |
a78cfa7f | 756 | case isl_ast_op_fdiv_q: |
71819f45 | 757 | #if HAVE_ISL_OPTIONS_SET_SCHEDULE_SERIALIZE_SCCS |
560d18d3 | 758 | /* ISL-0.15 or later. */ |
49ed2fa4 | 759 | case isl_ast_op_zdiv_r: |
71819f45 | 760 | #endif |
a78cfa7f RG |
761 | case isl_ast_op_and: |
762 | case isl_ast_op_or: | |
763 | case isl_ast_op_eq: | |
764 | case isl_ast_op_le: | |
765 | case isl_ast_op_lt: | |
766 | case isl_ast_op_ge: | |
767 | case isl_ast_op_gt: | |
768 | return binary_op_to_tree (type, expr, ip); | |
769 | ||
770 | case isl_ast_op_minus: | |
771 | return unary_op_to_tree (type, expr, ip); | |
772 | ||
773 | case isl_ast_op_cond: | |
774 | return ternary_op_to_tree (type, expr, ip); | |
775 | ||
776 | default: | |
777 | gcc_unreachable (); | |
778 | } | |
779 | ||
780 | return NULL_TREE; | |
781 | } | |
782 | ||
783 | /* Converts an ISL AST expression E back to a GCC expression tree of | |
784 | type TYPE. */ | |
785 | ||
050e1371 AK |
786 | tree |
787 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
788 | gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr, |
789 | ivs_params &ip) | |
790 | { | |
610a61a7 AK |
791 | if (codegen_error) |
792 | { | |
793 | isl_ast_expr_free (expr); | |
794 | return NULL_TREE; | |
795 | } | |
796 | ||
a78cfa7f RG |
797 | switch (isl_ast_expr_get_type (expr)) |
798 | { | |
799 | case isl_ast_expr_id: | |
6a7d8936 | 800 | return gcc_expression_from_isl_ast_expr_id (type, expr, ip); |
a78cfa7f RG |
801 | |
802 | case isl_ast_expr_int: | |
803 | return gcc_expression_from_isl_expr_int (type, expr); | |
804 | ||
805 | case isl_ast_expr_op: | |
806 | return gcc_expression_from_isl_expr_op (type, expr, ip); | |
807 | ||
808 | default: | |
809 | gcc_unreachable (); | |
810 | } | |
811 | ||
812 | return NULL_TREE; | |
813 | } | |
814 | ||
815 | /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an | |
816 | induction variable for the new LOOP. New LOOP is attached to CFG | |
817 | starting at ENTRY_EDGE. LOOP is inserted into the loop tree and | |
818 | becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds | |
819 | ISL's scattering name to the induction variable created for the | |
820 | loop of STMT. The new induction variable is inserted in the NEWIVS | |
821 | vector and is of type TYPE. */ | |
822 | ||
050e1371 AK |
823 | struct loop * |
824 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
825 | graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for, |
826 | loop_p outer, tree type, tree lb, tree ub, | |
827 | ivs_params &ip) | |
828 | { | |
829 | isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for); | |
830 | tree stride = gcc_expression_from_isl_expression (type, for_inc, ip); | |
610a61a7 AK |
831 | |
832 | /* To fail code generation, we generate wrong code until we discard it. */ | |
833 | if (codegen_error) | |
834 | stride = integer_zero_node; | |
835 | ||
a78cfa7f RG |
836 | tree ivvar = create_tmp_var (type, "graphite_IV"); |
837 | tree iv, iv_after_increment; | |
838 | loop_p loop = create_empty_loop_on_edge | |
839 | (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment, | |
840 | outer ? outer : entry_edge->src->loop_father); | |
841 | ||
842 | isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for); | |
843 | isl_id *id = isl_ast_expr_get_id (for_iterator); | |
a6631027 RG |
844 | std::map<isl_id *, tree>::iterator res; |
845 | res = ip.find (id); | |
846 | if (ip.count (id)) | |
847 | isl_id_free (res->first); | |
a78cfa7f RG |
848 | ip[id] = iv; |
849 | isl_ast_expr_free (for_iterator); | |
850 | return loop; | |
851 | } | |
852 | ||
a78cfa7f RG |
853 | /* Create the loop for a isl_ast_node_for. |
854 | ||
855 | - NEXT_E is the edge where new generated code should be attached. */ | |
856 | ||
050e1371 AK |
857 | edge |
858 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
859 | translate_isl_ast_for_loop (loop_p context_loop, |
860 | __isl_keep isl_ast_node *node_for, edge next_e, | |
861 | tree type, tree lb, tree ub, | |
862 | ivs_params &ip) | |
863 | { | |
864 | gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for); | |
865 | struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop, | |
866 | type, lb, ub, ip); | |
867 | edge last_e = single_exit (loop); | |
868 | edge to_body = single_succ_edge (loop->header); | |
869 | basic_block after = to_body->dest; | |
870 | ||
a78cfa7f RG |
871 | /* Translate the body of the loop. */ |
872 | isl_ast_node *for_body = isl_ast_node_for_get_body (node_for); | |
873 | next_e = translate_isl_ast (loop, for_body, to_body, ip); | |
874 | isl_ast_node_free (for_body); | |
65b016eb AK |
875 | |
876 | /* Early return if we failed to translate loop body. */ | |
2ecf4eca | 877 | if (!next_e || codegen_error_p ()) |
65b016eb AK |
878 | return NULL; |
879 | ||
d6416b38 AK |
880 | if (next_e->dest != after) |
881 | redirect_edge_succ_nodup (next_e, after); | |
a78cfa7f RG |
882 | set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src); |
883 | ||
574921c2 | 884 | if (flag_loop_parallelize_all) |
2ecf4eca AK |
885 | { |
886 | isl_id *id = isl_ast_node_get_annotation (node_for); | |
887 | gcc_assert (id); | |
888 | ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id); | |
889 | loop->can_be_parallel = for_info->is_parallelizable; | |
890 | free (for_info); | |
891 | isl_id_free (id); | |
892 | } | |
a78cfa7f RG |
893 | |
894 | return last_e; | |
895 | } | |
896 | ||
897 | /* We use this function to get the upper bound because of the form, | |
898 | which is used by isl to represent loops: | |
899 | ||
900 | for (iterator = init; cond; iterator += inc) | |
901 | ||
902 | { | |
903 | ||
2ecf4eca | 904 | ... |
a78cfa7f RG |
905 | |
906 | } | |
907 | ||
908 | The loop condition is an arbitrary expression, which contains the | |
909 | current loop iterator. | |
910 | ||
911 | (e.g. iterator + 3 < B && C > iterator + A) | |
912 | ||
913 | We have to know the upper bound of the iterator to generate a loop | |
914 | in Gimple form. It can be obtained from the special representation | |
915 | of the loop condition, which is generated by isl, | |
916 | if the ast_build_atomic_upper_bound option is set. In this case, | |
917 | isl generates a loop condition that consists of the current loop | |
918 | iterator, + an operator (< or <=) and an expression not involving | |
919 | the iterator, which is processed and returned by this function. | |
920 | ||
921 | (e.g iterator <= upper-bound-expression-without-iterator) */ | |
922 | ||
923 | static __isl_give isl_ast_expr * | |
924 | get_upper_bound (__isl_keep isl_ast_node *node_for) | |
925 | { | |
926 | gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for); | |
927 | isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for); | |
928 | gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op); | |
929 | isl_ast_expr *res; | |
930 | switch (isl_ast_expr_get_op_type (for_cond)) | |
931 | { | |
932 | case isl_ast_op_le: | |
933 | res = isl_ast_expr_get_op_arg (for_cond, 1); | |
934 | break; | |
935 | ||
936 | case isl_ast_op_lt: | |
937 | { | |
2ecf4eca | 938 | /* (iterator < ub) => (iterator <= ub - 1). */ |
2a466686 RG |
939 | isl_val *one = |
940 | isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1); | |
a78cfa7f RG |
941 | isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1); |
942 | res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one)); | |
943 | break; | |
944 | } | |
945 | ||
946 | default: | |
947 | gcc_unreachable (); | |
948 | } | |
949 | isl_ast_expr_free (for_cond); | |
950 | return res; | |
951 | } | |
952 | ||
953 | /* All loops generated by create_empty_loop_on_edge have the form of | |
954 | a post-test loop: | |
955 | ||
956 | do | |
957 | ||
958 | { | |
959 | body of the loop; | |
960 | } while (lower bound < upper bound); | |
961 | ||
962 | We create a new if region protecting the loop to be executed, if | |
963 | the execution count is zero (lower bound > upper bound). */ | |
964 | ||
050e1371 AK |
965 | edge |
966 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
967 | graphite_create_new_loop_guard (edge entry_edge, |
968 | __isl_keep isl_ast_node *node_for, tree *type, | |
969 | tree *lb, tree *ub, ivs_params &ip) | |
970 | { | |
971 | gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for); | |
972 | tree cond_expr; | |
973 | edge exit_edge; | |
974 | ||
55d1bd59 RG |
975 | *type = |
976 | build_nonstandard_integer_type (graphite_expression_type_precision, 0); | |
a78cfa7f RG |
977 | isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for); |
978 | *lb = gcc_expression_from_isl_expression (*type, for_init, ip); | |
610a61a7 AK |
979 | /* To fail code generation, we generate wrong code until we discard it. */ |
980 | if (codegen_error) | |
981 | *lb = integer_zero_node; | |
a78cfa7f RG |
982 | isl_ast_expr *upper_bound = get_upper_bound (node_for); |
983 | *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip); | |
610a61a7 AK |
984 | /* To fail code generation, we generate wrong code until we discard it. */ |
985 | if (codegen_error) | |
986 | *ub = integer_zero_node; | |
a78cfa7f RG |
987 | |
988 | /* When ub is simply a constant or a parameter, use lb <= ub. */ | |
989 | if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME) | |
990 | cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub); | |
991 | else | |
992 | { | |
993 | tree one = (POINTER_TYPE_P (*type) | |
994 | ? convert_to_ptrofftype (integer_one_node) | |
995 | : fold_convert (*type, integer_one_node)); | |
996 | /* Adding +1 and using LT_EXPR helps with loop latches that have a | |
997 | loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this | |
998 | becomes 2^k-1 due to integer overflow, and the condition lb <= ub | |
999 | is true, even if we do not want this. However lb < ub + 1 is false, | |
1000 | as expected. */ | |
1001 | tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR | |
1002 | : PLUS_EXPR, *type, *ub, one); | |
1003 | ||
1004 | cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one); | |
1005 | } | |
1006 | ||
77ec8b8c AK |
1007 | if (integer_onep (cond_expr)) |
1008 | exit_edge = entry_edge; | |
1009 | else | |
1010 | exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr); | |
a78cfa7f RG |
1011 | |
1012 | return exit_edge; | |
1013 | } | |
1014 | ||
1015 | /* Translates an isl_ast_node_for to Gimple. */ | |
1016 | ||
050e1371 AK |
1017 | edge |
1018 | translate_isl_ast_to_gimple:: | |
a78cfa7f RG |
1019 | translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node, |
1020 | edge next_e, ivs_params &ip) | |
1021 | { | |
1022 | gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for); | |
1023 | tree type, lb, ub; | |
1024 | edge last_e = graphite_create_new_loop_guard (next_e, node, &type, | |
1025 | &lb, &ub, ip); | |
a78cfa7f | 1026 | |
77ec8b8c | 1027 | if (last_e == next_e) |
d6416b38 AK |
1028 | { |
1029 | /* There was no guard generated. */ | |
1030 | last_e = single_succ_edge (split_edge (last_e)); | |
1031 | ||
1032 | translate_isl_ast_for_loop (context_loop, node, next_e, | |
1033 | type, lb, ub, ip); | |
1034 | return last_e; | |
1035 | } | |
77ec8b8c AK |
1036 | |
1037 | edge true_e = get_true_edge_from_guard_bb (next_e->dest); | |
d6416b38 AK |
1038 | merge_points.safe_push (last_e); |
1039 | ||
1040 | last_e = single_succ_edge (split_edge (last_e)); | |
77ec8b8c | 1041 | translate_isl_ast_for_loop (context_loop, node, true_e, type, lb, ub, ip); |
d6416b38 | 1042 | |
a78cfa7f RG |
1043 | return last_e; |
1044 | } | |
1045 | ||
5493d313 RG |
1046 | /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction |
1047 | variables of the loops around GBB in SESE. | |
1048 | ||
1049 | FIXME: Instead of using a vec<tree> that maps each loop id to a possible | |
1050 | chrec, we could consider using a map<int, tree> that maps loop ids to the | |
1051 | corresponding tree expressions. */ | |
1052 | ||
050e1371 AK |
1053 | void |
1054 | translate_isl_ast_to_gimple:: | |
65ef70d6 | 1055 | build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb, |
5493d313 | 1056 | __isl_keep isl_ast_expr *user_expr, ivs_params &ip, |
bafcb153 | 1057 | sese_l ®ion) |
5493d313 RG |
1058 | { |
1059 | gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op && | |
050e1371 | 1060 | isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call); |
5493d313 RG |
1061 | int i; |
1062 | isl_ast_expr *arg_expr; | |
1063 | for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++) | |
1064 | { | |
1065 | arg_expr = isl_ast_expr_get_op_arg (user_expr, i); | |
1066 | tree type = | |
050e1371 | 1067 | build_nonstandard_integer_type (graphite_expression_type_precision, 0); |
5493d313 | 1068 | tree t = gcc_expression_from_isl_expression (type, arg_expr, ip); |
610a61a7 AK |
1069 | /* To fail code generation, we generate wrong code until we discard it. */ |
1070 | if (codegen_error) | |
1071 | t = integer_zero_node; | |
1072 | ||
5493d313 RG |
1073 | loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1); |
1074 | iv_map[old_loop->num] = t; | |
1075 | } | |
5493d313 RG |
1076 | } |
1077 | ||
1078 | /* Translates an isl_ast_node_user to Gimple. | |
1079 | ||
1080 | FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */ | |
1081 | ||
050e1371 AK |
1082 | edge |
1083 | translate_isl_ast_to_gimple:: | |
5493d313 RG |
1084 | translate_isl_ast_node_user (__isl_keep isl_ast_node *node, |
1085 | edge next_e, ivs_params &ip) | |
1086 | { | |
1087 | gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user); | |
65b016eb | 1088 | |
5493d313 RG |
1089 | isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node); |
1090 | isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0); | |
1091 | gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id); | |
65b016eb | 1092 | |
5493d313 RG |
1093 | isl_id *name_id = isl_ast_expr_get_id (name_expr); |
1094 | poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id); | |
1095 | gcc_assert (pbb); | |
65b016eb | 1096 | |
65ef70d6 | 1097 | gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb); |
65b016eb | 1098 | |
5493d313 RG |
1099 | isl_ast_expr_free (name_expr); |
1100 | isl_id_free (name_id); | |
1101 | ||
1102 | gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) && | |
1103 | "The entry block should not even appear within a scop"); | |
1104 | ||
2ecf4eca | 1105 | const int nb_loops = number_of_loops (cfun); |
65b016eb | 1106 | vec<tree> iv_map; |
b5bdf598 RG |
1107 | iv_map.create (nb_loops); |
1108 | iv_map.safe_grow_cleared (nb_loops); | |
5493d313 | 1109 | |
d37fc3aa | 1110 | build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region); |
5493d313 | 1111 | isl_ast_expr_free (user_expr); |
1b38d3ec | 1112 | |
e7b9f153 | 1113 | basic_block old_bb = GBB_BB (gbb); |
1b38d3ec AK |
1114 | if (dump_file) |
1115 | { | |
e7b9f153 AK |
1116 | fprintf (dump_file, |
1117 | "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n", | |
1118 | old_bb->index, next_e->src->index, next_e->dest->index); | |
1b38d3ec | 1119 | print_loops_bb (dump_file, GBB_BB (gbb), 0, 3); |
e7b9f153 | 1120 | |
1b38d3ec AK |
1121 | } |
1122 | ||
e7b9f153 | 1123 | next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map); |
2ecf4eca AK |
1124 | |
1125 | iv_map.release (); | |
1126 | ||
1127 | if (codegen_error_p ()) | |
65b016eb AK |
1128 | return NULL; |
1129 | ||
1b38d3ec AK |
1130 | if (dump_file) |
1131 | { | |
ff17c262 | 1132 | fprintf (dump_file, "[codegen] (after copy) new basic block\n"); |
1b38d3ec AK |
1133 | print_loops_bb (dump_file, next_e->src, 0, 3); |
1134 | } | |
1135 | ||
5493d313 RG |
1136 | return next_e; |
1137 | } | |
1138 | ||
322a0b39 RG |
1139 | /* Translates an isl_ast_node_block to Gimple. */ |
1140 | ||
050e1371 AK |
1141 | edge |
1142 | translate_isl_ast_to_gimple:: | |
322a0b39 RG |
1143 | translate_isl_ast_node_block (loop_p context_loop, |
1144 | __isl_keep isl_ast_node *node, | |
1145 | edge next_e, ivs_params &ip) | |
1146 | { | |
1147 | gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block); | |
1148 | isl_ast_node_list *node_list = isl_ast_node_block_get_children (node); | |
1149 | int i; | |
1150 | for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++) | |
1151 | { | |
1152 | isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i); | |
1153 | next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip); | |
1154 | isl_ast_node_free (tmp_node); | |
1155 | } | |
1156 | isl_ast_node_list_free (node_list); | |
1157 | return next_e; | |
1158 | } | |
333cc518 RG |
1159 | |
1160 | /* Creates a new if region corresponding to ISL's cond. */ | |
1161 | ||
050e1371 AK |
1162 | edge |
1163 | translate_isl_ast_to_gimple:: | |
333cc518 RG |
1164 | graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond, |
1165 | ivs_params &ip) | |
1166 | { | |
1167 | tree type = | |
1168 | build_nonstandard_integer_type (graphite_expression_type_precision, 0); | |
1169 | tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip); | |
610a61a7 AK |
1170 | /* To fail code generation, we generate wrong code until we discard it. */ |
1171 | if (codegen_error) | |
1172 | cond_expr = integer_zero_node; | |
1173 | ||
333cc518 RG |
1174 | edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr); |
1175 | return exit_edge; | |
1176 | } | |
1177 | ||
1178 | /* Translates an isl_ast_node_if to Gimple. */ | |
1179 | ||
050e1371 AK |
1180 | edge |
1181 | translate_isl_ast_to_gimple:: | |
333cc518 RG |
1182 | translate_isl_ast_node_if (loop_p context_loop, |
1183 | __isl_keep isl_ast_node *node, | |
1184 | edge next_e, ivs_params &ip) | |
1185 | { | |
1186 | gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if); | |
1187 | isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node); | |
1188 | edge last_e = graphite_create_new_guard (next_e, if_cond, ip); | |
333cc518 | 1189 | edge true_e = get_true_edge_from_guard_bb (next_e->dest); |
d6416b38 AK |
1190 | merge_points.safe_push (last_e); |
1191 | ||
333cc518 RG |
1192 | isl_ast_node *then_node = isl_ast_node_if_get_then (node); |
1193 | translate_isl_ast (context_loop, then_node, true_e, ip); | |
1194 | isl_ast_node_free (then_node); | |
1195 | ||
1196 | edge false_e = get_false_edge_from_guard_bb (next_e->dest); | |
1197 | isl_ast_node *else_node = isl_ast_node_if_get_else (node); | |
1198 | if (isl_ast_node_get_type (else_node) != isl_ast_node_error) | |
1199 | translate_isl_ast (context_loop, else_node, false_e, ip); | |
d6416b38 | 1200 | |
333cc518 RG |
1201 | isl_ast_node_free (else_node); |
1202 | return last_e; | |
1203 | } | |
322a0b39 | 1204 | |
a78cfa7f RG |
1205 | /* Translates an ISL AST node NODE to GCC representation in the |
1206 | context of a SESE. */ | |
1207 | ||
050e1371 AK |
1208 | edge |
1209 | translate_isl_ast_to_gimple::translate_isl_ast (loop_p context_loop, | |
1210 | __isl_keep isl_ast_node *node, | |
1211 | edge next_e, ivs_params &ip) | |
a78cfa7f | 1212 | { |
2ecf4eca | 1213 | if (codegen_error_p ()) |
65b016eb AK |
1214 | return NULL; |
1215 | ||
a78cfa7f RG |
1216 | switch (isl_ast_node_get_type (node)) |
1217 | { | |
1218 | case isl_ast_node_error: | |
1219 | gcc_unreachable (); | |
1220 | ||
1221 | case isl_ast_node_for: | |
1222 | return translate_isl_ast_node_for (context_loop, node, | |
1223 | next_e, ip); | |
1224 | ||
1225 | case isl_ast_node_if: | |
333cc518 RG |
1226 | return translate_isl_ast_node_if (context_loop, node, |
1227 | next_e, ip); | |
a78cfa7f RG |
1228 | |
1229 | case isl_ast_node_user: | |
5493d313 | 1230 | return translate_isl_ast_node_user (node, next_e, ip); |
a78cfa7f RG |
1231 | |
1232 | case isl_ast_node_block: | |
322a0b39 RG |
1233 | return translate_isl_ast_node_block (context_loop, node, |
1234 | next_e, ip); | |
a78cfa7f RG |
1235 | |
1236 | default: | |
1237 | gcc_unreachable (); | |
1238 | } | |
1239 | } | |
1240 | ||
2ecf4eca AK |
1241 | /* Return true when BB contains loop close phi nodes. A loop close phi node is |
1242 | at the exit of loop which takes one argument that is the last value of the | |
1243 | variable being used out of the loop. */ | |
a78cfa7f | 1244 | |
d6416b38 | 1245 | static bool |
2ecf4eca | 1246 | bb_contains_loop_close_phi_nodes (basic_block bb) |
f6cc3103 | 1247 | { |
2ecf4eca AK |
1248 | return single_pred_p (bb) |
1249 | && bb->loop_father != single_pred_edge (bb)->src->loop_father; | |
f6cc3103 RG |
1250 | } |
1251 | ||
2ecf4eca AK |
1252 | /* Return true when BB contains loop phi nodes. A loop phi node is the loop |
1253 | header containing phi nodes which has one init-edge and one back-edge. */ | |
fb3764d1 | 1254 | |
d6416b38 | 1255 | static bool |
2ecf4eca | 1256 | bb_contains_loop_phi_nodes (basic_block bb) |
fb3764d1 | 1257 | { |
2ecf4eca | 1258 | gcc_assert (EDGE_COUNT (bb->preds) <= 2); |
fb3764d1 | 1259 | |
2ecf4eca AK |
1260 | if (bb->preds->length () == 1) |
1261 | return false; | |
fb3764d1 | 1262 | |
2ecf4eca | 1263 | unsigned depth = loop_depth (bb->loop_father); |
fb3764d1 | 1264 | |
2ecf4eca | 1265 | edge preds[2] = { (*bb->preds)[0], (*bb->preds)[1] }; |
fb3764d1 | 1266 | |
2ecf4eca AK |
1267 | if (depth > loop_depth (preds[0]->src->loop_father) |
1268 | || depth > loop_depth (preds[1]->src->loop_father)) | |
1269 | return true; | |
fb3764d1 | 1270 | |
2ecf4eca AK |
1271 | /* When one of the edges correspond to the same loop father and other |
1272 | doesn't. */ | |
1273 | if (bb->loop_father != preds[0]->src->loop_father | |
1274 | && bb->loop_father == preds[1]->src->loop_father) | |
1275 | return true; | |
1276 | ||
1277 | if (bb->loop_father != preds[1]->src->loop_father | |
1278 | && bb->loop_father == preds[0]->src->loop_father) | |
1279 | return true; | |
1280 | ||
1281 | return false; | |
fb3764d1 RG |
1282 | } |
1283 | ||
2ecf4eca AK |
1284 | /* Check if USE is defined in a basic block from where the definition of USE can |
1285 | propagate from all the paths. FIXME: Verify checks for virtual operands. */ | |
f6cc3103 | 1286 | |
2ecf4eca AK |
1287 | static bool |
1288 | is_loop_closed_ssa_use (basic_block bb, tree use) | |
f6cc3103 | 1289 | { |
2ecf4eca AK |
1290 | if (TREE_CODE (use) != SSA_NAME || virtual_operand_p (use)) |
1291 | return true; | |
f6cc3103 | 1292 | |
2ecf4eca AK |
1293 | /* For close-phi nodes def always comes from a loop which has a back-edge. */ |
1294 | if (bb_contains_loop_close_phi_nodes (bb)) | |
1295 | return true; | |
f6cc3103 | 1296 | |
2ecf4eca AK |
1297 | gimple *def = SSA_NAME_DEF_STMT (use); |
1298 | basic_block def_bb = gimple_bb (def); | |
1299 | return (!def_bb | |
1300 | || flow_bb_inside_loop_p (def_bb->loop_father, bb)); | |
f6cc3103 RG |
1301 | } |
1302 | ||
2ecf4eca AK |
1303 | /* Return the number of phi nodes in BB. */ |
1304 | ||
1305 | static int | |
1306 | number_of_phi_nodes (basic_block bb) | |
574921c2 | 1307 | { |
2ecf4eca AK |
1308 | int num_phis = 0; |
1309 | for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi); | |
1310 | gsi_next (&psi)) | |
1311 | num_phis++; | |
1312 | return num_phis; | |
574921c2 RG |
1313 | } |
1314 | ||
2ecf4eca | 1315 | /* Returns true if BB uses name in one of its PHIs. */ |
6a12a004 | 1316 | |
2ecf4eca AK |
1317 | static bool |
1318 | phi_uses_name (basic_block bb, tree name) | |
6a12a004 | 1319 | { |
2ecf4eca AK |
1320 | for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi); |
1321 | gsi_next (&psi)) | |
1322 | { | |
1323 | gphi *phi = psi.phi (); | |
1324 | for (unsigned i = 0; i < gimple_phi_num_args (phi); i++) | |
1325 | { | |
1326 | tree use_arg = gimple_phi_arg_def (phi, i); | |
1327 | if (use_arg == name) | |
1328 | return true; | |
1329 | } | |
1330 | } | |
1331 | return false; | |
6a12a004 RG |
1332 | } |
1333 | ||
2ecf4eca AK |
1334 | /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The |
1335 | definition should flow into use, and the use should respect the loop-closed | |
1336 | SSA form. */ | |
1337 | ||
1338 | bool | |
1339 | translate_isl_ast_to_gimple:: | |
1340 | is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb, | |
1341 | bool loop_phi, tree old_name, basic_block old_bb) const | |
f6cc3103 | 1342 | { |
2ecf4eca AK |
1343 | /* The def of the rename must either dominate the uses or come from a |
1344 | back-edge. Also the def must respect the loop closed ssa form. */ | |
1345 | if (!is_loop_closed_ssa_use (use_bb, rename)) | |
1346 | { | |
1347 | if (dump_file) | |
1348 | { | |
ff17c262 | 1349 | fprintf (dump_file, "[codegen] rename not in loop closed ssa:"); |
2ecf4eca | 1350 | print_generic_expr (dump_file, rename, 0); |
ff17c262 | 1351 | fprintf (dump_file, "\n"); |
2ecf4eca AK |
1352 | } |
1353 | return false; | |
1354 | } | |
a78cfa7f | 1355 | |
2ecf4eca AK |
1356 | if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb)) |
1357 | return true; | |
f6cc3103 | 1358 | |
2ecf4eca AK |
1359 | if (bb_contains_loop_phi_nodes (use_bb) && loop_phi) |
1360 | { | |
1361 | /* The loop-header dominates the loop-body. */ | |
1362 | if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb)) | |
1363 | return false; | |
1364 | ||
1365 | /* RENAME would be used in loop-phi. */ | |
1366 | gcc_assert (number_of_phi_nodes (use_bb)); | |
1367 | ||
1368 | /* For definitions coming from back edges, we should check that | |
1369 | old_name is used in a loop PHI node. | |
1370 | FIXME: Verify if this is true. */ | |
1371 | if (phi_uses_name (old_bb, old_name)) | |
1372 | return true; | |
1373 | } | |
1374 | return false; | |
1375 | } | |
f6cc3103 | 1376 | |
2ecf4eca AK |
1377 | /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in |
1378 | NEW_BB from RENAME_MAP. LOOP_PHI is true when we want to rename OLD_NAME | |
1379 | within a loop PHI instruction. */ | |
f6cc3103 | 1380 | |
2ecf4eca AK |
1381 | tree |
1382 | translate_isl_ast_to_gimple::get_rename (basic_block new_bb, | |
1383 | tree old_name, | |
1384 | basic_block old_bb, | |
1385 | bool loop_phi) const | |
f6cc3103 | 1386 | { |
2ecf4eca AK |
1387 | gcc_assert (TREE_CODE (old_name) == SSA_NAME); |
1388 | vec <tree> *renames = region->rename_map->get (old_name); | |
a78cfa7f | 1389 | |
2ecf4eca AK |
1390 | if (!renames || renames->is_empty ()) |
1391 | return NULL_TREE; | |
a78cfa7f | 1392 | |
2ecf4eca | 1393 | if (1 == renames->length ()) |
f6cc3103 | 1394 | { |
2ecf4eca | 1395 | tree rename = (*renames)[0]; |
ff17c262 AK |
1396 | if (TREE_CODE (rename) == SSA_NAME) |
1397 | { | |
1398 | basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (rename)); | |
1399 | if (is_valid_rename (rename, bb, new_bb, loop_phi, old_name, old_bb)) | |
1400 | return rename; | |
1401 | return NULL_TREE; | |
1402 | } | |
1403 | ||
1404 | if (is_constant (rename)) | |
2ecf4eca | 1405 | return rename; |
ff17c262 | 1406 | |
2ecf4eca | 1407 | return NULL_TREE; |
f6cc3103 | 1408 | } |
a78cfa7f | 1409 | |
2ecf4eca AK |
1410 | /* More than one renames corresponding to the old_name. Find the rename for |
1411 | which the definition flows into usage at new_bb. */ | |
1412 | int i; | |
1413 | tree t1 = NULL_TREE, t2; | |
1414 | basic_block t1_bb = NULL; | |
1415 | FOR_EACH_VEC_ELT (*renames, i, t2) | |
1416 | { | |
1417 | basic_block t2_bb = gimple_bb (SSA_NAME_DEF_STMT (t2)); | |
a78cfa7f | 1418 | |
2ecf4eca AK |
1419 | /* Defined in the same basic block as used. */ |
1420 | if (t2_bb == new_bb) | |
1421 | return t2; | |
a78cfa7f | 1422 | |
2ecf4eca AK |
1423 | /* NEW_BB and T2_BB are in two unrelated if-clauses. */ |
1424 | if (!dominated_by_p (CDI_DOMINATORS, new_bb, t2_bb)) | |
1425 | continue; | |
1426 | ||
1427 | /* Compute the nearest dominator. */ | |
1428 | if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb)) | |
1429 | { | |
1430 | t1_bb = t2_bb; | |
1431 | t1 = t2; | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | return t1; | |
1436 | } | |
1437 | ||
1438 | /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR). | |
1439 | When OLD_NAME and EXPR are the same we assert. */ | |
1440 | ||
1441 | void | |
1442 | translate_isl_ast_to_gimple::set_rename (tree old_name, tree expr) | |
1443 | { | |
1444 | if (dump_file) | |
1445 | { | |
ff17c262 | 1446 | fprintf (dump_file, "[codegen] setting rename: old_name = "); |
2ecf4eca AK |
1447 | print_generic_expr (dump_file, old_name, 0); |
1448 | fprintf (dump_file, ", new_name = "); | |
1449 | print_generic_expr (dump_file, expr, 0); | |
ff17c262 | 1450 | fprintf (dump_file, "\n"); |
2ecf4eca AK |
1451 | } |
1452 | ||
1453 | if (old_name == expr) | |
1454 | return; | |
1455 | ||
1456 | vec <tree> *renames = region->rename_map->get (old_name); | |
1457 | ||
1458 | if (renames) | |
1459 | renames->safe_push (expr); | |
1460 | else | |
1461 | { | |
1462 | vec<tree> r; | |
1463 | r.create (2); | |
1464 | r.safe_push (expr); | |
1465 | region->rename_map->put (old_name, r); | |
1466 | } | |
1467 | } | |
1468 | ||
1469 | /* Return an iterator to the instructions comes last in the execution order. | |
1470 | Either GSI1 and GSI2 should belong to the same basic block or one of their | |
1471 | respective basic blocks should dominate the other. */ | |
1472 | ||
1473 | gimple_stmt_iterator | |
1474 | later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2) | |
1475 | { | |
1476 | basic_block bb1 = gsi_bb (gsi1); | |
1477 | basic_block bb2 = gsi_bb (gsi2); | |
1478 | ||
1479 | /* Find the iterator which is the latest. */ | |
1480 | if (bb1 == bb2) | |
1481 | { | |
1482 | /* For empty basic blocks gsis point to the end of the sequence. Since | |
1483 | there is no operator== defined for gimple_stmt_iterator and for gsis | |
1484 | not pointing to a valid statement gsi_next would assert. */ | |
1485 | gimple_stmt_iterator gsi = gsi1; | |
1486 | do { | |
1487 | if (gsi_stmt (gsi) == gsi_stmt (gsi2)) | |
1488 | return gsi2; | |
1489 | gsi_next (&gsi); | |
1490 | } while (!gsi_end_p (gsi)); | |
1491 | ||
1492 | return gsi1; | |
1493 | } | |
1494 | ||
1495 | /* Find the basic block closest to the basic block which defines stmt. */ | |
1496 | if (dominated_by_p (CDI_DOMINATORS, bb1, bb2)) | |
1497 | return gsi1; | |
1498 | ||
1499 | gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1)); | |
1500 | return gsi2; | |
1501 | } | |
1502 | ||
1503 | /* Insert each statement from SEQ at its earliest insertion p. */ | |
1504 | ||
1505 | void | |
1506 | translate_isl_ast_to_gimple::gsi_insert_earliest (gimple_seq seq) | |
1507 | { | |
1508 | update_modified_stmts (seq); | |
1509 | sese_l &codegen_region = region->if_region->true_region->region; | |
1510 | basic_block begin_bb = get_entry_bb (codegen_region); | |
1511 | ||
1512 | /* Inserting the gimple statements in a vector because gimple_seq behave | |
1513 | in strage ways when inserting the stmts from it into different basic | |
1514 | blocks one at a time. */ | |
1515 | auto_vec<gimple *, 3> stmts; | |
1516 | for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi); | |
1517 | gsi_next (&gsi)) | |
1518 | stmts.safe_push (gsi_stmt (gsi)); | |
1519 | ||
1520 | int i; | |
1521 | gimple *use_stmt; | |
1522 | FOR_EACH_VEC_ELT (stmts, i, use_stmt) | |
1523 | { | |
1524 | gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI); | |
1525 | gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb); | |
1526 | ||
1527 | use_operand_p use_p; | |
1528 | ssa_op_iter op_iter; | |
1529 | FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE) | |
1530 | { | |
1531 | /* Iterator to the current def of use_p. For function parameters or | |
1532 | anything where def is not found, insert at the beginning of the | |
1533 | generated region. */ | |
1534 | gimple_stmt_iterator gsi_stmt = gsi_def_stmt; | |
1535 | ||
1536 | tree op = USE_FROM_PTR (use_p); | |
1537 | gimple *stmt = SSA_NAME_DEF_STMT (op); | |
1538 | if (stmt && (gimple_code (stmt) != GIMPLE_NOP)) | |
1539 | gsi_stmt = gsi_for_stmt (stmt); | |
1540 | ||
1541 | /* For region parameters, insert at the beginning of the generated | |
1542 | region. */ | |
1543 | if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region)) | |
1544 | gsi_stmt = gsi_def_stmt; | |
1545 | ||
1546 | gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt); | |
1547 | } | |
1548 | ||
1549 | if (!gsi_stmt (gsi_def_stmt)) | |
1550 | { | |
1551 | gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt)); | |
1552 | gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT); | |
1553 | } | |
1554 | else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI) | |
1555 | { | |
1556 | gimple_stmt_iterator bsi | |
1557 | = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt)); | |
1558 | /* Insert right after the PHI statements. */ | |
1559 | gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT); | |
1560 | } | |
1561 | else | |
1562 | gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT); | |
1563 | ||
1564 | if (dump_file) | |
1565 | { | |
ff17c262 | 1566 | fprintf (dump_file, "[codegen] inserting statement: "); |
2ecf4eca AK |
1567 | print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS); |
1568 | print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3); | |
1569 | } | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | /* Collect all the operands of NEW_EXPR by recursively visiting each | |
1574 | operand. */ | |
1575 | ||
1576 | void | |
1577 | translate_isl_ast_to_gimple::collect_all_ssa_names (tree new_expr, | |
1578 | vec<tree> *vec_ssa) | |
1579 | { | |
1580 | ||
1581 | /* Rename all uses in new_expr. */ | |
1582 | if (TREE_CODE (new_expr) == SSA_NAME) | |
1583 | { | |
1584 | vec_ssa->safe_push (new_expr); | |
1585 | return; | |
1586 | } | |
1587 | ||
1588 | /* Iterate over SSA_NAMES in NEW_EXPR. */ | |
1589 | for (int i = 0; i < (TREE_CODE_LENGTH (TREE_CODE (new_expr))); i++) | |
1590 | { | |
1591 | tree op = TREE_OPERAND (new_expr, i); | |
1592 | collect_all_ssa_names (op, vec_ssa); | |
1593 | } | |
1594 | } | |
1595 | ||
e7b9f153 AK |
1596 | /* This is abridged version of the function copied from: |
1597 | tree.c:substitute_in_expr (tree exp, tree f, tree r). */ | |
2ecf4eca AK |
1598 | |
1599 | static tree | |
1600 | substitute_ssa_name (tree exp, tree f, tree r) | |
1601 | { | |
1602 | enum tree_code code = TREE_CODE (exp); | |
1603 | tree op0, op1, op2, op3; | |
1604 | tree new_tree; | |
1605 | ||
1606 | /* We handle TREE_LIST and COMPONENT_REF separately. */ | |
1607 | if (code == TREE_LIST) | |
1608 | { | |
1609 | op0 = substitute_ssa_name (TREE_CHAIN (exp), f, r); | |
1610 | op1 = substitute_ssa_name (TREE_VALUE (exp), f, r); | |
1611 | if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp)) | |
1612 | return exp; | |
1613 | ||
1614 | return tree_cons (TREE_PURPOSE (exp), op1, op0); | |
1615 | } | |
1616 | else if (code == COMPONENT_REF) | |
1617 | { | |
1618 | tree inner; | |
1619 | ||
1620 | /* If this expression is getting a value from a PLACEHOLDER_EXPR | |
1621 | and it is the right field, replace it with R. */ | |
1622 | for (inner = TREE_OPERAND (exp, 0); | |
1623 | REFERENCE_CLASS_P (inner); | |
1624 | inner = TREE_OPERAND (inner, 0)) | |
1625 | ; | |
1626 | ||
1627 | /* The field. */ | |
1628 | op1 = TREE_OPERAND (exp, 1); | |
1629 | ||
1630 | if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f) | |
1631 | return r; | |
1632 | ||
1633 | /* If this expression hasn't been completed let, leave it alone. */ | |
1634 | if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner)) | |
1635 | return exp; | |
1636 | ||
1637 | op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r); | |
1638 | if (op0 == TREE_OPERAND (exp, 0)) | |
1639 | return exp; | |
1640 | ||
1641 | new_tree | |
1642 | = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE); | |
1643 | } | |
1644 | else | |
1645 | switch (TREE_CODE_CLASS (code)) | |
1646 | { | |
1647 | case tcc_constant: | |
1648 | return exp; | |
1649 | ||
1650 | case tcc_declaration: | |
1651 | if (exp == f) | |
1652 | return r; | |
1653 | else | |
1654 | return exp; | |
1655 | ||
1656 | case tcc_expression: | |
1657 | if (exp == f) | |
1658 | return r; | |
1659 | ||
1660 | /* Fall through... */ | |
1661 | ||
1662 | case tcc_exceptional: | |
1663 | case tcc_unary: | |
1664 | case tcc_binary: | |
1665 | case tcc_comparison: | |
1666 | case tcc_reference: | |
1667 | switch (TREE_CODE_LENGTH (code)) | |
1668 | { | |
1669 | case 0: | |
1670 | if (exp == f) | |
1671 | return r; | |
1672 | return exp; | |
1673 | ||
1674 | case 1: | |
1675 | op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r); | |
1676 | if (op0 == TREE_OPERAND (exp, 0)) | |
1677 | return exp; | |
1678 | ||
1679 | new_tree = fold_build1 (code, TREE_TYPE (exp), op0); | |
1680 | break; | |
1681 | ||
1682 | case 2: | |
1683 | op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r); | |
1684 | op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r); | |
1685 | ||
1686 | if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)) | |
1687 | return exp; | |
1688 | ||
1689 | new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1); | |
1690 | break; | |
1691 | ||
1692 | case 3: | |
1693 | op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r); | |
1694 | op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r); | |
1695 | op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r); | |
1696 | ||
1697 | if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1) | |
1698 | && op2 == TREE_OPERAND (exp, 2)) | |
1699 | return exp; | |
1700 | ||
1701 | new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2); | |
1702 | break; | |
1703 | ||
1704 | case 4: | |
1705 | op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r); | |
1706 | op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r); | |
1707 | op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r); | |
1708 | op3 = substitute_ssa_name (TREE_OPERAND (exp, 3), f, r); | |
1709 | ||
1710 | if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1) | |
1711 | && op2 == TREE_OPERAND (exp, 2) | |
1712 | && op3 == TREE_OPERAND (exp, 3)) | |
1713 | return exp; | |
1714 | ||
1715 | new_tree | |
1716 | = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3)); | |
1717 | break; | |
1718 | ||
1719 | default: | |
1720 | gcc_unreachable (); | |
1721 | } | |
1722 | break; | |
1723 | ||
1724 | case tcc_vl_exp: | |
1725 | default: | |
1726 | gcc_unreachable (); | |
1727 | } | |
1728 | ||
1729 | TREE_READONLY (new_tree) |= TREE_READONLY (exp); | |
1730 | ||
1731 | if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF) | |
1732 | TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp); | |
1733 | ||
1734 | return new_tree; | |
1735 | } | |
1736 | ||
1737 | /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */ | |
1738 | ||
1739 | tree | |
1740 | translate_isl_ast_to_gimple::rename_all_uses (tree new_expr, basic_block new_bb, | |
1741 | basic_block old_bb) | |
1742 | { | |
1743 | auto_vec<tree, 2> ssa_names; | |
1744 | collect_all_ssa_names (new_expr, &ssa_names); | |
1745 | tree t; | |
1746 | int i; | |
1747 | FOR_EACH_VEC_ELT (ssa_names, i, t) | |
1748 | if (tree r = get_rename (new_bb, t, old_bb, false)) | |
1749 | new_expr = substitute_ssa_name (new_expr, t, r); | |
1750 | ||
1751 | return new_expr; | |
1752 | } | |
1753 | ||
2927ca4b AK |
1754 | /* For ops which are scev_analyzeable, we can regenerate a new name from its |
1755 | scalar evolution around LOOP. */ | |
2ecf4eca AK |
1756 | |
1757 | tree | |
1758 | translate_isl_ast_to_gimple:: | |
1759 | get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop, | |
1760 | basic_block new_bb, basic_block old_bb, | |
1761 | vec<tree> iv_map) | |
1762 | { | |
1763 | tree scev = scalar_evolution_in_region (region->region, loop, old_name); | |
1764 | ||
1765 | /* At this point we should know the exact scev for each | |
1766 | scalar SSA_NAME used in the scop: all the other scalar | |
1767 | SSA_NAMEs should have been translated out of SSA using | |
1768 | arrays with one element. */ | |
1769 | tree new_expr; | |
1770 | if (chrec_contains_undetermined (scev)) | |
1771 | { | |
1772 | codegen_error = true; | |
1773 | return build_zero_cst (TREE_TYPE (old_name)); | |
1774 | } | |
1775 | ||
1776 | new_expr = chrec_apply_map (scev, iv_map); | |
1777 | ||
1778 | /* The apply should produce an expression tree containing | |
1779 | the uses of the new induction variables. We should be | |
1780 | able to use new_expr instead of the old_name in the newly | |
1781 | generated loop nest. */ | |
1782 | if (chrec_contains_undetermined (new_expr) | |
1783 | || tree_contains_chrecs (new_expr, NULL)) | |
1784 | { | |
1785 | codegen_error = true; | |
1786 | return build_zero_cst (TREE_TYPE (old_name)); | |
1787 | } | |
1788 | ||
1789 | /* We should check all the operands and all of them should dominate the use at | |
1790 | new_expr. */ | |
1791 | if (TREE_CODE (new_expr) == SSA_NAME) | |
1792 | { | |
1793 | basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr)); | |
1794 | if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb)) | |
1795 | { | |
2ecf4eca | 1796 | codegen_error = true; |
2ecf4eca AK |
1797 | return build_zero_cst (TREE_TYPE (old_name)); |
1798 | } | |
1799 | } | |
1800 | ||
1801 | new_expr = rename_all_uses (new_expr, new_bb, old_bb); | |
e7b9f153 AK |
1802 | |
1803 | /* We check all the operands and all of them should dominate the use at | |
2ecf4eca | 1804 | new_expr. */ |
e7b9f153 AK |
1805 | auto_vec <tree, 2> new_ssa_names; |
1806 | collect_all_ssa_names (new_expr, &new_ssa_names); | |
1807 | int i; | |
1808 | tree new_ssa_name; | |
1809 | FOR_EACH_VEC_ELT (new_ssa_names, i, new_ssa_name) | |
2ecf4eca | 1810 | { |
e7b9f153 | 1811 | if (TREE_CODE (new_ssa_name) == SSA_NAME) |
2ecf4eca | 1812 | { |
e7b9f153 AK |
1813 | basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_ssa_name)); |
1814 | if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb)) | |
1815 | { | |
1816 | codegen_error = true; | |
1817 | return build_zero_cst (TREE_TYPE (old_name)); | |
1818 | } | |
2ecf4eca AK |
1819 | } |
1820 | } | |
1821 | ||
1822 | /* Replace the old_name with the new_expr. */ | |
1823 | return force_gimple_operand (unshare_expr (new_expr), stmts, | |
1824 | true, NULL_TREE); | |
1825 | } | |
1826 | ||
1827 | /* Renames the scalar uses of the statement COPY, using the | |
1828 | substitution map RENAME_MAP, inserting the gimplification code at | |
1829 | GSI_TGT, for the translation REGION, with the original copied | |
1830 | statement in LOOP, and using the induction variable renaming map | |
1831 | IV_MAP. Returns true when something has been renamed. codegen_error | |
1832 | is set when the code generation cannot continue. */ | |
1833 | ||
1834 | bool | |
1835 | translate_isl_ast_to_gimple::rename_uses (gimple *copy, | |
1836 | gimple_stmt_iterator *gsi_tgt, | |
1837 | basic_block old_bb, | |
1838 | loop_p loop, vec<tree> iv_map) | |
1839 | { | |
1840 | bool changed = false; | |
1841 | ||
1842 | if (is_gimple_debug (copy)) | |
1843 | { | |
1844 | if (gimple_debug_bind_p (copy)) | |
1845 | gimple_debug_bind_reset_value (copy); | |
1846 | else if (gimple_debug_source_bind_p (copy)) | |
1847 | return false; | |
1848 | else | |
1849 | gcc_unreachable (); | |
1850 | ||
1851 | return false; | |
1852 | } | |
1853 | ||
1854 | if (dump_file) | |
1855 | { | |
ff17c262 | 1856 | fprintf (dump_file, "[codegen] renaming uses of stmt: "); |
2ecf4eca AK |
1857 | print_gimple_stmt (dump_file, copy, 0, 0); |
1858 | } | |
1859 | ||
1860 | use_operand_p use_p; | |
1861 | ssa_op_iter op_iter; | |
1862 | FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE) | |
1863 | { | |
1864 | tree old_name = USE_FROM_PTR (use_p); | |
1865 | ||
1866 | if (dump_file) | |
1867 | { | |
ff17c262 | 1868 | fprintf (dump_file, "[codegen] renaming old_name = "); |
2ecf4eca | 1869 | print_generic_expr (dump_file, old_name, 0); |
ff17c262 | 1870 | fprintf (dump_file, "\n"); |
2ecf4eca AK |
1871 | } |
1872 | ||
1873 | if (TREE_CODE (old_name) != SSA_NAME | |
1874 | || SSA_NAME_IS_DEFAULT_DEF (old_name)) | |
1875 | continue; | |
1876 | ||
1877 | changed = true; | |
1878 | tree new_expr = get_rename (gsi_tgt->bb, old_name, | |
1879 | old_bb, false); | |
1880 | ||
1881 | if (new_expr) | |
1882 | { | |
1883 | tree type_old_name = TREE_TYPE (old_name); | |
1884 | tree type_new_expr = TREE_TYPE (new_expr); | |
1885 | ||
1886 | if (dump_file) | |
1887 | { | |
ff17c262 | 1888 | fprintf (dump_file, "[codegen] from rename_map: new_name = "); |
2ecf4eca | 1889 | print_generic_expr (dump_file, new_expr, 0); |
ff17c262 | 1890 | fprintf (dump_file, "\n"); |
2ecf4eca AK |
1891 | } |
1892 | ||
1893 | if (type_old_name != type_new_expr | |
1894 | || TREE_CODE (new_expr) != SSA_NAME) | |
1895 | { | |
1896 | tree var = create_tmp_var (type_old_name, "var"); | |
1897 | ||
1898 | if (!useless_type_conversion_p (type_old_name, type_new_expr)) | |
1899 | new_expr = fold_convert (type_old_name, new_expr); | |
1900 | ||
1901 | gimple_seq stmts; | |
1902 | new_expr = force_gimple_operand (new_expr, &stmts, true, var); | |
1903 | gsi_insert_earliest (stmts); | |
1904 | } | |
1905 | ||
1906 | replace_exp (use_p, new_expr); | |
1907 | continue; | |
1908 | } | |
1909 | ||
1910 | gimple_seq stmts; | |
1911 | new_expr = get_rename_from_scev (old_name, &stmts, loop, gimple_bb (copy), | |
1912 | old_bb, iv_map); | |
1913 | if (!new_expr || codegen_error_p ()) | |
1914 | return false; | |
1915 | ||
1916 | if (dump_file) | |
1917 | { | |
ff17c262 | 1918 | fprintf (dump_file, "[codegen] not in rename map, scev: "); |
2ecf4eca | 1919 | print_generic_expr (dump_file, new_expr, 0); |
ff17c262 | 1920 | fprintf (dump_file, "\n"); |
2ecf4eca AK |
1921 | } |
1922 | ||
1923 | gsi_insert_earliest (stmts); | |
1924 | replace_exp (use_p, new_expr); | |
1925 | ||
1926 | if (TREE_CODE (new_expr) == INTEGER_CST | |
1927 | && is_gimple_assign (copy)) | |
1928 | { | |
1929 | tree rhs = gimple_assign_rhs1 (copy); | |
1930 | ||
1931 | if (TREE_CODE (rhs) == ADDR_EXPR) | |
1932 | recompute_tree_invariant_for_addr_expr (rhs); | |
1933 | } | |
1934 | ||
1935 | set_rename (old_name, new_expr); | |
1936 | } | |
1937 | ||
1938 | return changed; | |
1939 | } | |
1940 | ||
1941 | /* Returns a basic block that could correspond to where a constant was defined | |
1942 | in the original code. In the original code OLD_BB had the definition, we | |
1943 | need to find which basic block out of the copies of old_bb, in the new | |
1944 | region, should a definition correspond to if it has to reach BB. */ | |
1945 | ||
1946 | basic_block | |
1947 | translate_isl_ast_to_gimple::get_def_bb_for_const (basic_block bb, | |
1948 | basic_block old_bb) const | |
1949 | { | |
1950 | vec <basic_block> *bbs = region->copied_bb_map->get (old_bb); | |
1951 | ||
1952 | if (!bbs || bbs->is_empty ()) | |
1953 | return NULL; | |
1954 | ||
1955 | if (1 == bbs->length ()) | |
1956 | return (*bbs)[0]; | |
1957 | ||
1958 | int i; | |
1959 | basic_block b1 = NULL, b2; | |
1960 | FOR_EACH_VEC_ELT (*bbs, i, b2) | |
1961 | { | |
1962 | if (b2 == bb) | |
1963 | return bb; | |
1964 | ||
1965 | /* BB and B2 are in two unrelated if-clauses. */ | |
1966 | if (!dominated_by_p (CDI_DOMINATORS, bb, b2)) | |
1967 | continue; | |
1968 | ||
1969 | /* Compute the nearest dominator. */ | |
1970 | if (!b1 || dominated_by_p (CDI_DOMINATORS, b2, b1)) | |
1971 | b1 = b2; | |
1972 | } | |
1973 | ||
1974 | gcc_assert (b1); | |
1975 | return b1; | |
1976 | } | |
1977 | ||
1978 | /* Get the new name of OP (from OLD_BB) to be used in NEW_BB. LOOP_PHI is true | |
1979 | when we want to rename an OP within a loop PHI instruction. */ | |
1980 | ||
1981 | tree | |
1982 | translate_isl_ast_to_gimple:: | |
1983 | get_new_name (basic_block new_bb, tree op, | |
1984 | basic_block old_bb, bool loop_phi) const | |
1985 | { | |
1986 | /* For constants the names are the same. */ | |
ff17c262 | 1987 | if (is_constant (op)) |
2ecf4eca AK |
1988 | return op; |
1989 | ||
1990 | return get_rename (new_bb, op, old_bb, loop_phi); | |
1991 | } | |
1992 | ||
1993 | /* Return a debug location for OP. */ | |
1994 | ||
1995 | static location_t | |
1996 | get_loc (tree op) | |
1997 | { | |
1998 | location_t loc = UNKNOWN_LOCATION; | |
1999 | ||
2000 | if (TREE_CODE (op) == SSA_NAME) | |
2001 | loc = gimple_location (SSA_NAME_DEF_STMT (op)); | |
2002 | return loc; | |
2003 | } | |
2004 | ||
2005 | /* Returns the incoming edges of basic_block BB in the pair. The first edge is | |
2006 | the init edge (from outside the loop) and the second one is the back edge | |
2007 | from the same loop. */ | |
2008 | ||
2009 | std::pair<edge, edge> | |
2010 | get_edges (basic_block bb) | |
2011 | { | |
2012 | std::pair<edge, edge> edges; | |
2013 | edge e; | |
2014 | edge_iterator ei; | |
2015 | FOR_EACH_EDGE (e, ei, bb->preds) | |
2016 | if (bb->loop_father != e->src->loop_father) | |
2017 | edges.first = e; | |
2018 | else | |
2019 | edges.second = e; | |
2020 | return edges; | |
2021 | } | |
2022 | ||
2023 | /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI | |
2024 | must be found unless they can be POSTPONEd for later. */ | |
2025 | ||
21c7259c | 2026 | bool |
2ecf4eca AK |
2027 | translate_isl_ast_to_gimple:: |
2028 | copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb, | |
2029 | gphi *new_phi, init_back_edge_pair_t &ibp_new_bb, | |
2030 | bool postpone) | |
2031 | { | |
2032 | gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi)); | |
2033 | ||
2034 | basic_block new_bb = gimple_bb (new_phi); | |
2035 | for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++) | |
2036 | { | |
2037 | edge e; | |
2038 | if (gimple_phi_arg_edge (old_phi, i) == ibp_old_bb.first) | |
2039 | e = ibp_new_bb.first; | |
2040 | else | |
2041 | e = ibp_new_bb.second; | |
2042 | ||
2043 | tree old_name = gimple_phi_arg_def (old_phi, i); | |
2044 | tree new_name = get_new_name (new_bb, old_name, | |
2045 | gimple_bb (old_phi), true); | |
2046 | if (new_name) | |
2047 | { | |
2048 | add_phi_arg (new_phi, new_name, e, get_loc (old_name)); | |
2049 | continue; | |
2050 | } | |
2051 | ||
2052 | gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name); | |
2053 | if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP) | |
2054 | /* If the phi arg was a function arg, or wasn't defined, just use the | |
2055 | old name. */ | |
2056 | add_phi_arg (new_phi, old_name, e, get_loc (old_name)); | |
2057 | else if (postpone) | |
2058 | { | |
2059 | /* Postpone code gen for later for those back-edges we don't have the | |
2060 | names yet. */ | |
2061 | region->incomplete_phis.safe_push (std::make_pair (old_phi, new_phi)); | |
2062 | if (dump_file) | |
ff17c262 | 2063 | fprintf (dump_file, "[codegen] postpone loop phi nodes.\n"); |
2ecf4eca AK |
2064 | } |
2065 | else | |
2066 | /* Either we should add the arg to phi or, we should postpone. */ | |
21c7259c | 2067 | return false; |
2ecf4eca | 2068 | } |
21c7259c | 2069 | return true; |
2ecf4eca AK |
2070 | } |
2071 | ||
2072 | /* Copy loop phi nodes from BB to NEW_BB. */ | |
2073 | ||
2074 | bool | |
2075 | translate_isl_ast_to_gimple::copy_loop_phi_nodes (basic_block bb, | |
2076 | basic_block new_bb) | |
2077 | { | |
2078 | if (dump_file) | |
ff17c262 | 2079 | fprintf (dump_file, "[codegen] copying loop phi nodes in bb_%d.\n", |
2ecf4eca AK |
2080 | new_bb->index); |
2081 | ||
2082 | /* Loop phi nodes should have only two arguments. */ | |
2083 | gcc_assert (2 == EDGE_COUNT (bb->preds)); | |
2084 | ||
2085 | /* First edge is the init edge and second is the back edge. */ | |
2086 | init_back_edge_pair_t ibp_old_bb = get_edges (bb); | |
2087 | ||
2088 | /* First edge is the init edge and second is the back edge. */ | |
2089 | init_back_edge_pair_t ibp_new_bb = get_edges (new_bb); | |
2090 | ||
2091 | for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi); | |
2092 | gsi_next (&psi)) | |
2093 | { | |
2094 | gphi *phi = psi.phi (); | |
2095 | tree res = gimple_phi_result (phi); | |
2096 | if (virtual_operand_p (res)) | |
2097 | continue; | |
2098 | if (is_gimple_reg (res) && scev_analyzable_p (res, region->region)) | |
2099 | continue; | |
2100 | ||
2101 | gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb); | |
2102 | tree new_res = create_new_def_for (res, new_phi, | |
2103 | gimple_phi_result_ptr (new_phi)); | |
2104 | set_rename (res, new_res); | |
21c7259c AK |
2105 | codegen_error = !copy_loop_phi_args (phi, ibp_old_bb, new_phi, |
2106 | ibp_new_bb, true); | |
2ecf4eca | 2107 | update_stmt (new_phi); |
ca377fc3 AK |
2108 | |
2109 | if (dump_file) | |
2110 | { | |
2111 | fprintf (dump_file, "[codegen] creating loop-phi node: "); | |
2112 | print_gimple_stmt (dump_file, new_phi, 0, 0); | |
2113 | } | |
2ecf4eca AK |
2114 | } |
2115 | ||
2116 | return true; | |
2117 | } | |
2118 | ||
2119 | /* Return the init value of PHI, the value coming from outside the loop. */ | |
2120 | ||
2121 | static tree | |
2122 | get_loop_init_value (gphi *phi) | |
2123 | { | |
2124 | ||
2125 | loop_p loop = gimple_bb (phi)->loop_father; | |
2126 | ||
2127 | edge e; | |
2128 | edge_iterator ei; | |
2129 | FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds) | |
2130 | if (e->src->loop_father != loop) | |
2131 | return gimple_phi_arg_def (phi, e->dest_idx); | |
2132 | ||
2133 | return NULL_TREE; | |
2134 | } | |
2135 | ||
2136 | /* Find the init value (the value which comes from outside the loop), of one of | |
2137 | the operands of DEF which is defined by a loop phi. */ | |
2138 | ||
2139 | static tree | |
2140 | find_init_value (gimple *def) | |
2141 | { | |
2142 | if (gimple_code (def) == GIMPLE_PHI) | |
2143 | return get_loop_init_value (as_a <gphi*> (def)); | |
2144 | ||
2145 | if (gimple_vuse (def)) | |
2146 | return NULL_TREE; | |
2147 | ||
2148 | ssa_op_iter iter; | |
2149 | use_operand_p use_p; | |
2150 | FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE) | |
2151 | { | |
2152 | tree use = USE_FROM_PTR (use_p); | |
2153 | if (TREE_CODE (use) == SSA_NAME) | |
2154 | { | |
2155 | if (tree res = find_init_value (SSA_NAME_DEF_STMT (use))) | |
2156 | return res; | |
2157 | } | |
2158 | } | |
2159 | ||
2160 | return NULL_TREE; | |
2161 | } | |
2162 | ||
2163 | /* Return the init value, the value coming from outside the loop. */ | |
2164 | ||
2165 | static tree | |
2166 | find_init_value_close_phi (gphi *phi) | |
2167 | { | |
2168 | gcc_assert (gimple_phi_num_args (phi) == 1); | |
2169 | tree use_arg = gimple_phi_arg_def (phi, 0); | |
2170 | gimple *def = SSA_NAME_DEF_STMT (use_arg); | |
2171 | return find_init_value (def); | |
2172 | } | |
2173 | ||
d6416b38 AK |
2174 | |
2175 | tree translate_isl_ast_to_gimple:: | |
2176 | add_close_phis_to_outer_loops (tree last_merge_name, edge last_e, | |
2177 | gimple *old_close_phi) | |
2178 | { | |
2179 | sese_l &codegen_region = region->if_region->true_region->region; | |
2180 | gimple *stmt = SSA_NAME_DEF_STMT (last_merge_name); | |
2181 | basic_block bb = gimple_bb (stmt); | |
2182 | if (!bb_in_sese_p (bb, codegen_region)) | |
2183 | return last_merge_name; | |
2184 | ||
2185 | loop_p loop = bb->loop_father; | |
2186 | if (!loop_in_sese_p (loop, codegen_region)) | |
2187 | return last_merge_name; | |
2188 | ||
2189 | edge e = single_exit (loop); | |
2190 | ||
2191 | if (dominated_by_p (CDI_DOMINATORS, e->dest, last_e->src)) | |
2192 | return last_merge_name; | |
2193 | ||
2194 | tree old_name = gimple_phi_arg_def (old_close_phi, 0); | |
2195 | tree old_close_phi_name = gimple_phi_result (old_close_phi); | |
2196 | ||
2197 | bb = e->dest; | |
2198 | if (!bb_contains_loop_close_phi_nodes (bb) || !single_succ_p (bb)) | |
2199 | bb = split_edge (e); | |
2200 | ||
2201 | gphi *close_phi = create_phi_node (SSA_NAME_VAR (last_merge_name), bb); | |
2202 | tree res = create_new_def_for (last_merge_name, close_phi, | |
2203 | gimple_phi_result_ptr (close_phi)); | |
2204 | set_rename (old_close_phi_name, res); | |
2205 | add_phi_arg (close_phi, last_merge_name, e, get_loc (old_name)); | |
2206 | last_merge_name = res; | |
2207 | ||
2208 | return add_close_phis_to_outer_loops (last_merge_name, last_e, old_close_phi); | |
2209 | } | |
2210 | ||
2211 | /* Add phi nodes to all merge points of all the diamonds enclosing the loop of | |
2212 | the close phi node PHI. */ | |
2213 | ||
2214 | bool translate_isl_ast_to_gimple:: | |
2215 | add_close_phis_to_merge_points (gphi *old_close_phi, gphi *new_close_phi, | |
2216 | tree default_value) | |
2217 | { | |
2218 | sese_l &codegen_region = region->if_region->true_region->region; | |
2219 | basic_block default_value_bb = get_entry_bb (codegen_region); | |
2220 | if (SSA_NAME == TREE_CODE (default_value)) | |
2221 | { | |
2222 | gimple *stmt = SSA_NAME_DEF_STMT (default_value); | |
2223 | if (!stmt || gimple_code (stmt) == GIMPLE_NOP) | |
2224 | return false; | |
2225 | default_value_bb = gimple_bb (stmt); | |
2226 | } | |
2227 | ||
2228 | basic_block new_close_phi_bb = gimple_bb (new_close_phi); | |
2229 | ||
2230 | tree old_close_phi_name = gimple_phi_result (old_close_phi); | |
2231 | tree new_close_phi_name = gimple_phi_result (new_close_phi); | |
2232 | tree last_merge_name = new_close_phi_name; | |
2233 | tree old_name = gimple_phi_arg_def (old_close_phi, 0); | |
2234 | ||
2235 | int i; | |
2236 | edge merge_e; | |
2237 | FOR_EACH_VEC_ELT_REVERSE (merge_points, i, merge_e) | |
2238 | { | |
2239 | basic_block new_merge_bb = merge_e->src; | |
2240 | if (!dominated_by_p (CDI_DOMINATORS, new_merge_bb, default_value_bb)) | |
2241 | continue; | |
2242 | ||
2243 | last_merge_name = add_close_phis_to_outer_loops (last_merge_name, merge_e, | |
2244 | old_close_phi); | |
2245 | ||
2246 | gphi *merge_phi = create_phi_node (SSA_NAME_VAR (old_close_phi_name), new_merge_bb); | |
2247 | tree merge_res = create_new_def_for (old_close_phi_name, merge_phi, | |
2248 | gimple_phi_result_ptr (merge_phi)); | |
2249 | set_rename (old_close_phi_name, merge_res); | |
2250 | ||
2251 | edge from_loop = NULL, from_default_value = NULL; | |
2252 | edge e; | |
2253 | edge_iterator ei; | |
2254 | FOR_EACH_EDGE (e, ei, new_merge_bb->preds) | |
2255 | if (dominated_by_p (CDI_DOMINATORS, e->src, new_close_phi_bb)) | |
2256 | from_loop = e; | |
2257 | else | |
2258 | from_default_value = e; | |
2259 | ||
2260 | /* Because CDI_POST_DOMINATORS are not updated, we only rely on | |
2261 | CDI_DOMINATORS, which may not handle all cases where new_close_phi_bb | |
2262 | is contained in another condition. */ | |
2263 | if (!from_default_value || !from_loop) | |
2264 | return false; | |
2265 | ||
2266 | add_phi_arg (merge_phi, last_merge_name, from_loop, get_loc (old_name)); | |
2267 | add_phi_arg (merge_phi, default_value, from_default_value, get_loc (old_name)); | |
2268 | ||
2269 | if (dump_file) | |
2270 | { | |
2271 | fprintf (dump_file, "[codegen] Adding guard-phi: "); | |
2272 | print_gimple_stmt (dump_file, merge_phi, 0, 0); | |
2273 | } | |
2274 | ||
2275 | update_stmt (merge_phi); | |
2276 | last_merge_name = merge_res; | |
2277 | } | |
2278 | ||
2279 | return true; | |
2280 | } | |
2281 | ||
2ecf4eca AK |
2282 | /* Copy all the loop-close phi args from BB to NEW_BB. */ |
2283 | ||
2284 | bool | |
2285 | translate_isl_ast_to_gimple::copy_loop_close_phi_args (basic_block old_bb, | |
2286 | basic_block new_bb, | |
2287 | bool postpone) | |
2288 | { | |
2ecf4eca AK |
2289 | for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi); |
2290 | gsi_next (&psi)) | |
2291 | { | |
d6416b38 AK |
2292 | gphi *old_close_phi = psi.phi (); |
2293 | tree res = gimple_phi_result (old_close_phi); | |
2ecf4eca AK |
2294 | if (virtual_operand_p (res)) |
2295 | continue; | |
2296 | ||
2297 | if (is_gimple_reg (res) && scev_analyzable_p (res, region->region)) | |
2298 | /* Loop close phi nodes should not be scev_analyzable_p. */ | |
2299 | gcc_unreachable (); | |
2300 | ||
d6416b38 AK |
2301 | gphi *new_close_phi = create_phi_node (SSA_NAME_VAR (res), new_bb); |
2302 | tree new_res = create_new_def_for (res, new_close_phi, | |
2303 | gimple_phi_result_ptr (new_close_phi)); | |
2ecf4eca AK |
2304 | set_rename (res, new_res); |
2305 | ||
d6416b38 | 2306 | tree old_name = gimple_phi_arg_def (old_close_phi, 0); |
2ecf4eca AK |
2307 | tree new_name = get_new_name (new_bb, old_name, old_bb, false); |
2308 | ||
2309 | /* Predecessor basic blocks of a loop close phi should have been code | |
2310 | generated before. FIXME: This is fixable by merging PHIs from inner | |
2311 | loops as well. See: gfortran.dg/graphite/interchange-3.f90. */ | |
2312 | if (!new_name) | |
2313 | return false; | |
2314 | ||
d6416b38 | 2315 | add_phi_arg (new_close_phi, new_name, single_pred_edge (new_bb), |
2ecf4eca AK |
2316 | get_loc (old_name)); |
2317 | if (dump_file) | |
2318 | { | |
49385686 | 2319 | fprintf (dump_file, "[codegen] Adding loop close phi: "); |
d6416b38 | 2320 | print_gimple_stmt (dump_file, new_close_phi, 0, 0); |
2ecf4eca AK |
2321 | } |
2322 | ||
d6416b38 | 2323 | update_stmt (new_close_phi); |
2ecf4eca AK |
2324 | |
2325 | /* When there is no loop guard around this codegenerated loop, there is no | |
2326 | need to collect the close-phi arg. */ | |
d6416b38 | 2327 | if (merge_points.is_empty ()) |
2ecf4eca AK |
2328 | continue; |
2329 | ||
2927ca4b | 2330 | /* Add a PHI in the succ_new_bb for each close phi of the loop. */ |
d6416b38 | 2331 | tree default_value = find_init_value_close_phi (new_close_phi); |
2ecf4eca | 2332 | |
d6416b38 AK |
2333 | /* A close phi must come from a loop-phi having a default value. */ |
2334 | if (!default_value) | |
2ecf4eca | 2335 | { |
21c7259c AK |
2336 | if (!postpone) |
2337 | return false; | |
2338 | ||
d6416b38 AK |
2339 | region->incomplete_phis.safe_push (std::make_pair (old_close_phi, |
2340 | new_close_phi)); | |
2ecf4eca AK |
2341 | if (dump_file) |
2342 | { | |
ff17c262 | 2343 | fprintf (dump_file, "[codegen] postpone close phi nodes: "); |
d6416b38 | 2344 | print_gimple_stmt (dump_file, new_close_phi, 0, 0); |
2ecf4eca AK |
2345 | } |
2346 | continue; | |
2347 | } | |
2348 | ||
d6416b38 AK |
2349 | if (!add_close_phis_to_merge_points (old_close_phi, new_close_phi, |
2350 | default_value)) | |
2351 | return false; | |
2ecf4eca AK |
2352 | } |
2353 | ||
2354 | return true; | |
2355 | } | |
2356 | ||
2357 | /* Copy loop close phi nodes from BB to NEW_BB. */ | |
2358 | ||
2359 | bool | |
2360 | translate_isl_ast_to_gimple::copy_loop_close_phi_nodes (basic_block old_bb, | |
2361 | basic_block new_bb) | |
2362 | { | |
2363 | if (dump_file) | |
49385686 | 2364 | fprintf (dump_file, "[codegen] copying loop close phi nodes in bb_%d.\n", |
2ecf4eca AK |
2365 | new_bb->index); |
2366 | /* Loop close phi nodes should have only one argument. */ | |
2367 | gcc_assert (1 == EDGE_COUNT (old_bb->preds)); | |
2368 | ||
2369 | return copy_loop_close_phi_args (old_bb, new_bb, true); | |
2370 | } | |
2371 | ||
2372 | ||
2373 | /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB. | |
2374 | DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the | |
2375 | other pred of OLD_BB as well. If no such basic block exists then it is NULL. | |
2376 | NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be | |
2377 | NULL. | |
2378 | ||
2379 | Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa. | |
2380 | In this case DOMINATING_PRED = NULL. | |
2381 | ||
2382 | Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2. | |
2383 | ||
2384 | Returns true on successful copy of the args, false otherwise. */ | |
2385 | ||
2386 | bool | |
2387 | translate_isl_ast_to_gimple:: | |
2388 | add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2], | |
2389 | edge old_bb_dominating_edge, | |
2390 | edge old_bb_non_dominating_edge, | |
2391 | gphi *phi, gphi *new_phi, | |
2392 | basic_block new_bb) | |
2393 | { | |
21c7259c | 2394 | basic_block def_pred[2] = { NULL, NULL }; |
2ecf4eca AK |
2395 | int not_found_bb_index = -1; |
2396 | for (int i = 0; i < 2; i++) | |
2397 | { | |
2398 | /* If the corresponding def_bb could not be found the entry will be | |
2399 | NULL. */ | |
2400 | if (TREE_CODE (old_phi_args[i]) == INTEGER_CST) | |
2401 | def_pred[i] = get_def_bb_for_const (new_bb, | |
2402 | gimple_phi_arg_edge (phi, i)->src); | |
21c7259c | 2403 | else if (new_phi_args[i] && (TREE_CODE (new_phi_args[i]) == SSA_NAME)) |
2ecf4eca | 2404 | def_pred[i] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args[i])); |
21c7259c | 2405 | |
2ecf4eca AK |
2406 | if (!def_pred[i]) |
2407 | { | |
21c7259c AK |
2408 | /* When non are available bail out. */ |
2409 | if (not_found_bb_index != -1) | |
2410 | return false; | |
2ecf4eca AK |
2411 | not_found_bb_index = i; |
2412 | } | |
2413 | } | |
2414 | ||
2415 | /* Here we are pattern matching on the structure of CFG w.r.t. old one. */ | |
2416 | if (old_bb_dominating_edge) | |
2417 | { | |
21c7259c AK |
2418 | if (not_found_bb_index != -1) |
2419 | return false; | |
2420 | ||
2ecf4eca AK |
2421 | basic_block new_pred1 = (*new_bb->preds)[0]->src; |
2422 | basic_block new_pred2 = (*new_bb->preds)[1]->src; | |
2423 | vec <basic_block> *bbs | |
2424 | = region->copied_bb_map->get (old_bb_non_dominating_edge->src); | |
21c7259c AK |
2425 | |
2426 | /* Could not find a mapping. */ | |
2427 | if (!bbs) | |
2428 | return false; | |
2429 | ||
2ecf4eca AK |
2430 | basic_block new_pred = NULL; |
2431 | basic_block b; | |
2432 | int i; | |
2433 | FOR_EACH_VEC_ELT (*bbs, i, b) | |
21c7259c AK |
2434 | { |
2435 | if (dominated_by_p (CDI_DOMINATORS, new_pred1, b)) | |
2436 | { | |
2437 | /* FIXME: If we have already found new_pred then we have to | |
2438 | disambiguate, bail out for now. */ | |
2439 | if (new_pred) | |
2440 | return false; | |
2441 | new_pred = new_pred1; | |
2442 | } | |
2443 | if (dominated_by_p (CDI_DOMINATORS, new_pred2, b)) | |
2444 | { | |
2445 | /* FIXME: If we have already found new_pred then we have to either | |
2446 | it dominates both or we have to disambiguate, bail out. */ | |
2447 | if (new_pred) | |
2448 | return false; | |
2449 | new_pred = new_pred2; | |
2450 | } | |
2451 | } | |
2ecf4eca | 2452 | |
21c7259c AK |
2453 | if (!new_pred) |
2454 | return false; | |
2ecf4eca AK |
2455 | |
2456 | edge new_non_dominating_edge = find_edge (new_pred, new_bb); | |
21c7259c AK |
2457 | gcc_assert (new_non_dominating_edge); |
2458 | /* FIXME: Validate each args just like in loop-phis. */ | |
2ecf4eca AK |
2459 | /* By the process of elimination we first insert insert phi-edge for |
2460 | non-dominating pred which is computed above and then we insert the | |
2461 | remaining one. */ | |
2462 | int inserted_edge = 0; | |
2463 | for (; inserted_edge < 2; inserted_edge++) | |
2464 | { | |
21c7259c | 2465 | edge new_bb_pred_edge = gimple_phi_arg_edge (new_phi, inserted_edge); |
2ecf4eca AK |
2466 | if (new_non_dominating_edge == new_bb_pred_edge) |
2467 | { | |
2468 | add_phi_arg (new_phi, new_phi_args[inserted_edge], | |
2469 | new_non_dominating_edge, | |
2470 | get_loc (old_phi_args[inserted_edge])); | |
2471 | break; | |
2472 | } | |
2473 | } | |
21c7259c AK |
2474 | if (inserted_edge == 2) |
2475 | return false; | |
2ecf4eca | 2476 | |
21c7259c | 2477 | int edge_dominating = inserted_edge == 0 ? 1 : 0; |
2ecf4eca AK |
2478 | |
2479 | edge new_dominating_edge = NULL; | |
21c7259c | 2480 | for (inserted_edge = 0; inserted_edge < 2; inserted_edge++) |
2ecf4eca | 2481 | { |
21c7259c | 2482 | edge e = gimple_phi_arg_edge (new_phi, inserted_edge); |
2ecf4eca | 2483 | if (e != new_non_dominating_edge) |
21c7259c AK |
2484 | { |
2485 | new_dominating_edge = e; | |
2486 | add_phi_arg (new_phi, new_phi_args[edge_dominating], | |
2487 | new_dominating_edge, | |
2488 | get_loc (old_phi_args[inserted_edge])); | |
2489 | break; | |
2490 | } | |
2ecf4eca | 2491 | } |
21c7259c | 2492 | gcc_assert (new_dominating_edge); |
2ecf4eca AK |
2493 | } |
2494 | else | |
2495 | { | |
2496 | /* Classic diamond structure: both edges are non-dominating. We need to | |
2497 | find one unique edge then the other can be found be elimination. If | |
2498 | any definition (def_pred) dominates both the preds of new_bb then we | |
2499 | bail out. Entries of def_pred maybe NULL, in that case we must | |
2500 | uniquely find pred with help of only one entry. */ | |
2501 | edge new_e[2] = { NULL, NULL }; | |
2502 | for (int i = 0; i < 2; i++) | |
2503 | { | |
2504 | edge e; | |
2505 | edge_iterator ei; | |
2506 | FOR_EACH_EDGE (e, ei, new_bb->preds) | |
2507 | if (def_pred[i] | |
2508 | && dominated_by_p (CDI_DOMINATORS, e->src, def_pred[i])) | |
2509 | { | |
2510 | if (new_e[i]) | |
2511 | /* We do not know how to handle the case when def_pred | |
2512 | dominates more than a predecessor. */ | |
2513 | return false; | |
2514 | new_e[i] = e; | |
2515 | } | |
2516 | } | |
2517 | ||
2518 | gcc_assert (new_e[0] || new_e[1]); | |
2519 | ||
2520 | /* Find the other edge by process of elimination. */ | |
2521 | if (not_found_bb_index != -1) | |
2522 | { | |
2523 | gcc_assert (!new_e[not_found_bb_index]); | |
2524 | int found_bb_index = not_found_bb_index == 1 ? 0 : 1; | |
2525 | edge e; | |
2526 | edge_iterator ei; | |
2527 | FOR_EACH_EDGE (e, ei, new_bb->preds) | |
2528 | { | |
2529 | if (new_e[found_bb_index] == e) | |
2530 | continue; | |
2531 | new_e[not_found_bb_index] = e; | |
2532 | } | |
2533 | } | |
2534 | ||
2535 | /* Add edges to phi args. */ | |
2536 | for (int i = 0; i < 2; i++) | |
2537 | add_phi_arg (new_phi, new_phi_args[i], new_e[i], | |
2538 | get_loc (old_phi_args[i])); | |
2539 | } | |
2540 | ||
2541 | return true; | |
2542 | } | |
2543 | ||
2544 | /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated | |
2545 | region. If postpone is true and it isn't possible to copy any arg of PHI, | |
2546 | the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later. | |
2547 | Returns false if the copying was unsuccessful. */ | |
2548 | ||
2549 | bool | |
2550 | translate_isl_ast_to_gimple::copy_cond_phi_args (gphi *phi, gphi *new_phi, | |
2551 | vec<tree> iv_map, | |
2552 | bool postpone) | |
2553 | { | |
2554 | if (dump_file) | |
ff17c262 | 2555 | fprintf (dump_file, "[codegen] copying cond phi args.\n"); |
2ecf4eca AK |
2556 | gcc_assert (2 == gimple_phi_num_args (phi)); |
2557 | ||
2558 | basic_block new_bb = gimple_bb (new_phi); | |
2559 | loop_p loop = gimple_bb (phi)->loop_father; | |
2560 | ||
2561 | basic_block old_bb = gimple_bb (phi); | |
2562 | edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL; | |
2563 | ||
2564 | edge e; | |
2565 | edge_iterator ei; | |
2566 | FOR_EACH_EDGE (e, ei, old_bb->preds) | |
2567 | if (!dominated_by_p (CDI_DOMINATORS, old_bb, e->src)) | |
2568 | old_bb_non_dominating_edge = e; | |
2569 | else | |
2570 | old_bb_dominating_edge = e; | |
2571 | ||
2572 | gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb, | |
2573 | old_bb_non_dominating_edge->src)); | |
2574 | ||
2575 | tree new_phi_args[2]; | |
2576 | tree old_phi_args[2]; | |
2577 | ||
2578 | for (unsigned i = 0; i < gimple_phi_num_args (phi); i++) | |
2579 | { | |
2580 | tree old_name = gimple_phi_arg_def (phi, i); | |
2581 | tree new_name = get_new_name (new_bb, old_name, old_bb, false); | |
2582 | old_phi_args[i] = old_name; | |
2583 | if (new_name) | |
2584 | { | |
2585 | new_phi_args [i] = new_name; | |
2586 | continue; | |
2587 | } | |
2588 | ||
2589 | /* If the phi-arg was a parameter. */ | |
21c7259c | 2590 | if (vec_find (region->params, old_name) != -1) |
2ecf4eca AK |
2591 | { |
2592 | new_phi_args [i] = old_name; | |
2593 | if (dump_file) | |
2594 | { | |
2595 | fprintf (dump_file, | |
ff17c262 | 2596 | "[codegen] parameter argument to phi, new_expr: "); |
21c7259c | 2597 | print_generic_expr (dump_file, new_phi_args[i], 0); |
ff17c262 | 2598 | fprintf (dump_file, "\n"); |
2ecf4eca AK |
2599 | } |
2600 | continue; | |
2601 | } | |
2602 | ||
21c7259c AK |
2603 | gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name); |
2604 | if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP) | |
2605 | /* FIXME: If the phi arg was a function arg, or wasn't defined, just use | |
2606 | the old name. */ | |
2607 | return false; | |
2ecf4eca | 2608 | |
21c7259c AK |
2609 | if (postpone) |
2610 | { | |
2611 | /* If the phi-arg is scev-analyzeable but only in the first stage. */ | |
2612 | if (is_gimple_reg (old_name) | |
2613 | && scev_analyzable_p (old_name, region->region)) | |
2ecf4eca | 2614 | { |
21c7259c AK |
2615 | gimple_seq stmts; |
2616 | tree new_expr = get_rename_from_scev (old_name, &stmts, loop, | |
2617 | new_bb, old_bb, iv_map); | |
2618 | if (codegen_error_p ()) | |
2619 | return false; | |
2620 | ||
2621 | gcc_assert (new_expr); | |
2622 | if (dump_file) | |
2623 | { | |
2624 | fprintf (dump_file, | |
ff17c262 | 2625 | "[codegen] scev analyzeable, new_expr: "); |
21c7259c | 2626 | print_generic_expr (dump_file, new_expr, 0); |
ff17c262 | 2627 | fprintf (dump_file, "\n"); |
21c7259c AK |
2628 | } |
2629 | gsi_insert_earliest (stmts); | |
2630 | new_phi_args [i] = new_name; | |
2631 | continue; | |
2ecf4eca | 2632 | } |
2ecf4eca | 2633 | |
2ecf4eca AK |
2634 | /* Postpone code gen for later for back-edges. */ |
2635 | region->incomplete_phis.safe_push (std::make_pair (phi, new_phi)); | |
2636 | ||
2637 | if (dump_file) | |
2638 | { | |
ff17c262 | 2639 | fprintf (dump_file, "[codegen] postpone cond phi nodes: "); |
2ecf4eca AK |
2640 | print_gimple_stmt (dump_file, new_phi, 0, 0); |
2641 | } | |
2642 | ||
2643 | new_phi_args [i] = NULL_TREE; | |
2644 | continue; | |
2645 | } | |
2646 | else | |
21c7259c AK |
2647 | /* Either we should add the arg to phi or, we should postpone. */ |
2648 | return false; | |
2ecf4eca AK |
2649 | } |
2650 | ||
21c7259c AK |
2651 | /* If none of the args have been determined in the first stage then wait until |
2652 | later. */ | |
2653 | if (postpone && !new_phi_args[0] && !new_phi_args[1]) | |
2654 | return true; | |
2655 | ||
2ecf4eca AK |
2656 | return add_phi_arg_for_new_expr (old_phi_args, new_phi_args, |
2657 | old_bb_dominating_edge, | |
2658 | old_bb_non_dominating_edge, | |
2659 | phi, new_phi, new_bb); | |
2660 | } | |
2661 | ||
2662 | /* Copy cond phi nodes from BB to NEW_BB. A cond-phi node is a basic block | |
2663 | containing phi nodes coming from two predecessors, and none of them are back | |
2664 | edges. */ | |
2665 | ||
2666 | bool | |
2667 | translate_isl_ast_to_gimple::copy_cond_phi_nodes (basic_block bb, | |
2668 | basic_block new_bb, | |
2669 | vec<tree> iv_map) | |
2670 | { | |
2671 | ||
2672 | gcc_assert (!bb_contains_loop_close_phi_nodes (bb)); | |
2673 | ||
2674 | if (dump_file) | |
ff17c262 | 2675 | fprintf (dump_file, "[codegen] copying cond phi nodes in bb_%d.\n", |
2ecf4eca AK |
2676 | new_bb->index); |
2677 | ||
2678 | /* Cond phi nodes should have exactly two arguments. */ | |
2679 | gcc_assert (2 == EDGE_COUNT (bb->preds)); | |
2680 | ||
2681 | for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi); | |
2682 | gsi_next (&psi)) | |
2683 | { | |
2684 | gphi *phi = psi.phi (); | |
2685 | tree res = gimple_phi_result (phi); | |
2686 | if (virtual_operand_p (res)) | |
2687 | continue; | |
2688 | if (is_gimple_reg (res) && scev_analyzable_p (res, region->region)) | |
2689 | /* Cond phi nodes should not be scev_analyzable_p. */ | |
2690 | gcc_unreachable (); | |
2691 | ||
2692 | gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb); | |
2693 | tree new_res = create_new_def_for (res, new_phi, | |
2694 | gimple_phi_result_ptr (new_phi)); | |
2695 | set_rename (res, new_res); | |
2696 | ||
2697 | if (!copy_cond_phi_args (phi, new_phi, iv_map, true)) | |
2698 | return false; | |
2699 | ||
2700 | update_stmt (new_phi); | |
2701 | } | |
2702 | ||
2703 | return true; | |
2704 | } | |
2705 | ||
2706 | /* Return true if STMT should be copied from region to the new code-generated | |
2707 | region. LABELs, CONDITIONS, induction-variables and region parameters need | |
2708 | not be copied. */ | |
2709 | ||
2710 | static bool | |
2711 | should_copy_to_new_region (gimple *stmt, sese_info_p region) | |
2712 | { | |
2713 | /* Do not copy labels or conditions. */ | |
2714 | if (gimple_code (stmt) == GIMPLE_LABEL | |
2715 | || gimple_code (stmt) == GIMPLE_COND) | |
2716 | return false; | |
2717 | ||
2718 | tree lhs; | |
2719 | /* Do not copy induction variables. */ | |
2720 | if (is_gimple_assign (stmt) | |
2721 | && (lhs = gimple_assign_lhs (stmt)) | |
2722 | && TREE_CODE (lhs) == SSA_NAME | |
2723 | && is_gimple_reg (lhs) | |
2724 | && scev_analyzable_p (lhs, region->region)) | |
2725 | return false; | |
2726 | ||
2727 | return true; | |
2728 | } | |
2729 | ||
2730 | /* Create new names for all the definitions created by COPY and add replacement | |
2731 | mappings for each new name. */ | |
2732 | ||
2733 | void | |
2734 | translate_isl_ast_to_gimple::set_rename_for_each_def (gimple *stmt) | |
2735 | { | |
2736 | def_operand_p def_p; | |
2737 | ssa_op_iter op_iter; | |
2738 | FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS) | |
2739 | { | |
2740 | tree old_name = DEF_FROM_PTR (def_p); | |
2741 | tree new_name = create_new_def_for (old_name, stmt, def_p); | |
2742 | set_rename (old_name, new_name); | |
2743 | } | |
2744 | } | |
2745 | ||
2746 | /* Duplicates the statements of basic block BB into basic block NEW_BB | |
2747 | and compute the new induction variables according to the IV_MAP. | |
2748 | CODEGEN_ERROR is set when the code generation cannot continue. */ | |
2749 | ||
2750 | bool | |
2751 | translate_isl_ast_to_gimple::graphite_copy_stmts_from_block (basic_block bb, | |
2752 | basic_block new_bb, | |
2753 | vec<tree> iv_map) | |
2754 | { | |
2755 | /* Iterator poining to the place where new statement (s) will be inserted. */ | |
2756 | gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb); | |
2757 | ||
2758 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); | |
2759 | gsi_next (&gsi)) | |
2760 | { | |
2761 | gimple *stmt = gsi_stmt (gsi); | |
2762 | if (!should_copy_to_new_region (stmt, region)) | |
2763 | continue; | |
2764 | ||
2765 | /* Create a new copy of STMT and duplicate STMT's virtual | |
2766 | operands. */ | |
2767 | gimple *copy = gimple_copy (stmt); | |
2768 | gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT); | |
2769 | ||
2770 | if (dump_file) | |
2771 | { | |
ff17c262 | 2772 | fprintf (dump_file, "[codegen] inserting statement: "); |
2ecf4eca AK |
2773 | print_gimple_stmt (dump_file, copy, 0, 0); |
2774 | } | |
2775 | ||
2776 | maybe_duplicate_eh_stmt (copy, stmt); | |
2777 | gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt); | |
2778 | ||
2779 | /* Crete new names for each def in the copied stmt. */ | |
2780 | set_rename_for_each_def (copy); | |
2781 | ||
2782 | loop_p loop = bb->loop_father; | |
2783 | if (rename_uses (copy, &gsi_tgt, bb, loop, iv_map)) | |
2784 | { | |
2785 | fold_stmt_inplace (&gsi_tgt); | |
2786 | gcc_assert (gsi_stmt (gsi_tgt) == copy); | |
2787 | } | |
2788 | ||
2789 | if (codegen_error_p ()) | |
2790 | return false; | |
2791 | ||
2792 | update_stmt (copy); | |
2793 | } | |
2794 | ||
2795 | return true; | |
2796 | } | |
2797 | ||
2927ca4b AK |
2798 | |
2799 | /* Given a basic block containing close-phi it returns the new basic block where | |
2800 | to insert a copy of the close-phi nodes. All the uses in close phis should | |
2801 | come from a single loop otherwise it returns NULL. */ | |
2802 | ||
2803 | edge | |
2804 | translate_isl_ast_to_gimple::edge_for_new_close_phis (basic_block bb) | |
2805 | { | |
2806 | /* Make sure that NEW_BB is the new_loop->exit->dest. We find the definition | |
2807 | of close phi in the original code and then find the mapping of basic block | |
2808 | defining that variable. If there are multiple close-phis and they are | |
2809 | defined in different loops (in the original or in the new code) because of | |
2810 | loop splitting, then we bail out. */ | |
2811 | loop_p new_loop = NULL; | |
2812 | for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi); | |
2813 | gsi_next (&psi)) | |
2814 | { | |
2815 | gphi *phi = psi.phi (); | |
2816 | tree name = gimple_phi_arg_def (phi, 0); | |
2817 | basic_block old_loop_bb = gimple_bb (SSA_NAME_DEF_STMT (name)); | |
2818 | ||
2819 | vec <basic_block> *bbs = region->copied_bb_map->get (old_loop_bb); | |
2820 | if (!bbs || bbs->length () != 1) | |
2821 | /* This is one of the places which shows preserving original structure | |
2822 | is not always possible, as we may need to insert close PHI for a loop | |
2823 | where the latch does not have any mapping, or the mapping is | |
2824 | ambiguous. */ | |
2825 | return NULL; | |
2826 | ||
2827 | if (!new_loop) | |
2828 | new_loop = (*bbs)[0]->loop_father; | |
2829 | else if (new_loop != (*bbs)[0]->loop_father) | |
2830 | return NULL; | |
2831 | } | |
2832 | ||
2833 | if (!new_loop) | |
2834 | return NULL; | |
2835 | ||
2836 | return single_exit (new_loop); | |
2837 | } | |
2838 | ||
2ecf4eca AK |
2839 | /* Copies BB and includes in the copied BB all the statements that can |
2840 | be reached following the use-def chains from the memory accesses, | |
2841 | and returns the next edge following this new block. codegen_error is | |
2842 | set when the code generation cannot continue. */ | |
2843 | ||
2844 | edge | |
2845 | translate_isl_ast_to_gimple::copy_bb_and_scalar_dependences (basic_block bb, | |
2846 | edge next_e, | |
2847 | vec<tree> iv_map) | |
2848 | { | |
2849 | int num_phis = number_of_phi_nodes (bb); | |
2850 | ||
2851 | if (region->copied_bb_map->get (bb)) | |
2852 | { | |
2853 | /* FIXME: we should be able to handle phi nodes with args coming from | |
2854 | outside the region. */ | |
2855 | if (num_phis) | |
2856 | { | |
2857 | codegen_error = true; | |
2858 | return NULL; | |
2859 | } | |
2860 | } | |
2861 | ||
d6416b38 AK |
2862 | basic_block new_bb = NULL; |
2863 | if (bb_contains_loop_close_phi_nodes (bb)) | |
2ecf4eca AK |
2864 | { |
2865 | if (dump_file) | |
ff17c262 | 2866 | fprintf (dump_file, "[codegen] bb_%d contains close phi nodes.\n", |
2ecf4eca AK |
2867 | bb->index); |
2868 | ||
2927ca4b AK |
2869 | edge e = edge_for_new_close_phis (bb); |
2870 | if (!e) | |
2ecf4eca | 2871 | { |
2927ca4b AK |
2872 | codegen_error = true; |
2873 | return NULL; | |
2ecf4eca AK |
2874 | } |
2875 | ||
d6416b38 AK |
2876 | basic_block phi_bb = e->dest; |
2877 | ||
2878 | if (!bb_contains_loop_close_phi_nodes (phi_bb) || !single_succ_p (phi_bb)) | |
2879 | phi_bb = split_edge (e); | |
2880 | ||
2927ca4b AK |
2881 | gcc_assert (single_pred_edge (phi_bb)->src->loop_father |
2882 | != single_pred_edge (phi_bb)->dest->loop_father); | |
2ecf4eca AK |
2883 | |
2884 | if (!copy_loop_close_phi_nodes (bb, phi_bb)) | |
2885 | { | |
2886 | codegen_error = true; | |
2887 | return NULL; | |
2888 | } | |
d6416b38 AK |
2889 | |
2890 | if (e == next_e) | |
2891 | new_bb = phi_bb; | |
2892 | else | |
2893 | new_bb = split_edge (next_e); | |
2ecf4eca | 2894 | } |
d6416b38 | 2895 | else |
2ecf4eca | 2896 | { |
d6416b38 AK |
2897 | new_bb = split_edge (next_e); |
2898 | if (num_phis > 0 && bb_contains_loop_phi_nodes (bb)) | |
2899 | { | |
2900 | basic_block phi_bb = next_e->dest->loop_father->header; | |
2ecf4eca | 2901 | |
d6416b38 AK |
2902 | /* At this point we are unable to codegenerate by still preserving the SSA |
2903 | structure because maybe the loop is completely unrolled and the PHIs | |
2904 | and cross-bb scalar dependencies are untrackable w.r.t. the original | |
2905 | code. See gfortran.dg/graphite/pr29832.f90. */ | |
2906 | if (EDGE_COUNT (bb->preds) != EDGE_COUNT (phi_bb->preds)) | |
2907 | { | |
2908 | codegen_error = true; | |
2909 | return NULL; | |
2910 | } | |
2ecf4eca | 2911 | |
ca377fc3 AK |
2912 | /* In case ISL did some loop peeling, like this: |
2913 | ||
2914 | S_8(0); | |
2915 | for (int c1 = 1; c1 <= 5; c1 += 1) { | |
2916 | S_8(c1); | |
2917 | } | |
2918 | S_8(6); | |
2919 | ||
2920 | there should be no loop-phi nodes in S_8(0). | |
2921 | ||
2922 | FIXME: We need to reason about dynamic instances of S_8, i.e., the | |
2923 | values of all scalar variables: for the moment we instantiate only | |
2924 | SCEV analyzable expressions on the iteration domain, and we need to | |
2925 | extend that to reductions that cannot be analyzed by SCEV. */ | |
2926 | if (!bb_in_sese_p (phi_bb, region->if_region->true_region->region)) | |
2927 | { | |
2928 | codegen_error = true; | |
2929 | return NULL; | |
2930 | } | |
2931 | ||
d6416b38 AK |
2932 | if (dump_file) |
2933 | fprintf (dump_file, "[codegen] bb_%d contains loop phi nodes.\n", | |
2934 | bb->index); | |
2935 | if (!copy_loop_phi_nodes (bb, phi_bb)) | |
2936 | { | |
2937 | codegen_error = true; | |
2938 | return NULL; | |
2939 | } | |
2940 | } | |
2941 | else if (num_phis > 0) | |
2ecf4eca | 2942 | { |
d6416b38 AK |
2943 | if (dump_file) |
2944 | fprintf (dump_file, "[codegen] bb_%d contains cond phi nodes.\n", | |
2945 | bb->index); | |
2946 | ||
2947 | basic_block phi_bb = single_pred (new_bb); | |
2948 | loop_p loop_father = new_bb->loop_father; | |
2949 | ||
2950 | /* Move back until we find the block with two predecessors. */ | |
2951 | while (single_pred_p (phi_bb)) | |
2952 | phi_bb = single_pred_edge (phi_bb)->src; | |
2953 | ||
2954 | /* If a corresponding merge-point was not found, then abort codegen. */ | |
2955 | if (phi_bb->loop_father != loop_father | |
1a67d2cd | 2956 | || !bb_in_sese_p (phi_bb, region->if_region->true_region->region) |
d6416b38 AK |
2957 | || !copy_cond_phi_nodes (bb, phi_bb, iv_map)) |
2958 | { | |
2959 | codegen_error = true; | |
2960 | return NULL; | |
2961 | } | |
2ecf4eca AK |
2962 | } |
2963 | } | |
2964 | ||
2965 | if (dump_file) | |
ff17c262 | 2966 | fprintf (dump_file, "[codegen] copying from bb_%d to bb_%d.\n", |
2ecf4eca AK |
2967 | bb->index, new_bb->index); |
2968 | ||
2969 | vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb); | |
2970 | if (copied_bbs) | |
2971 | copied_bbs->safe_push (new_bb); | |
2972 | else | |
2973 | { | |
2974 | vec<basic_block> bbs; | |
2975 | bbs.create (2); | |
2976 | bbs.safe_push (new_bb); | |
2977 | region->copied_bb_map->put (bb, bbs); | |
2978 | } | |
2979 | ||
2980 | if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map)) | |
2981 | { | |
2982 | codegen_error = true; | |
2983 | return NULL; | |
2984 | } | |
2985 | ||
2986 | return single_succ_edge (new_bb); | |
2987 | } | |
2988 | ||
2989 | /* Patch the missing arguments of the phi nodes. */ | |
2990 | ||
2991 | void | |
2992 | translate_isl_ast_to_gimple::translate_pending_phi_nodes () | |
2993 | { | |
2994 | int i; | |
2995 | phi_rename *rename; | |
2996 | FOR_EACH_VEC_ELT (region->incomplete_phis, i, rename) | |
2997 | { | |
2998 | gphi *old_phi = rename->first; | |
2999 | gphi *new_phi = rename->second; | |
3000 | basic_block old_bb = gimple_bb (old_phi); | |
3001 | basic_block new_bb = gimple_bb (new_phi); | |
3002 | ||
3003 | /* First edge is the init edge and second is the back edge. */ | |
3004 | init_back_edge_pair_t ibp_old_bb = get_edges (old_bb); | |
3005 | init_back_edge_pair_t ibp_new_bb = get_edges (new_bb); | |
3006 | ||
3007 | if (dump_file) | |
3008 | { | |
ff17c262 | 3009 | fprintf (dump_file, "[codegen] translating pending old-phi: "); |
2ecf4eca AK |
3010 | print_gimple_stmt (dump_file, old_phi, 0, 0); |
3011 | } | |
3012 | ||
3013 | auto_vec <tree, 1> iv_map; | |
3014 | if (bb_contains_loop_phi_nodes (new_bb)) | |
21c7259c AK |
3015 | codegen_error = !copy_loop_phi_args (old_phi, ibp_old_bb, new_phi, |
3016 | ibp_new_bb, false); | |
2ecf4eca | 3017 | else if (bb_contains_loop_close_phi_nodes (new_bb)) |
21c7259c AK |
3018 | codegen_error = !copy_loop_close_phi_args (old_bb, new_bb, false); |
3019 | else | |
3020 | codegen_error = !copy_cond_phi_args (old_phi, new_phi, iv_map, false); | |
2ecf4eca AK |
3021 | |
3022 | if (dump_file) | |
3023 | { | |
3024 | fprintf (dump_file, "[codegen] to new-phi: "); | |
3025 | print_gimple_stmt (dump_file, new_phi, 0, 0); | |
3026 | } | |
4c936604 AK |
3027 | if (codegen_error) |
3028 | return; | |
2ecf4eca AK |
3029 | } |
3030 | } | |
3031 | ||
3032 | /* Prints NODE to FILE. */ | |
3033 | ||
3034 | void | |
3035 | translate_isl_ast_to_gimple::print_isl_ast_node (FILE *file, | |
3036 | __isl_keep isl_ast_node *node, | |
3037 | __isl_keep isl_ctx *ctx) const | |
3038 | { | |
3039 | isl_printer *prn = isl_printer_to_file (ctx, file); | |
3040 | prn = isl_printer_set_output_format (prn, ISL_FORMAT_C); | |
3041 | prn = isl_printer_print_ast_node (prn, node); | |
3042 | prn = isl_printer_print_str (prn, "\n"); | |
3043 | isl_printer_free (prn); | |
3044 | } | |
3045 | ||
3046 | /* Add ISL's parameter identifiers and corresponding trees to ivs_params. */ | |
3047 | ||
3048 | void | |
3049 | translate_isl_ast_to_gimple::add_parameters_to_ivs_params (scop_p scop, | |
3050 | ivs_params &ip) | |
3051 | { | |
3052 | sese_info_p region = scop->scop_info; | |
3053 | unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param); | |
3054 | gcc_assert (nb_parameters == region->params.length ()); | |
3055 | unsigned i; | |
3056 | for (i = 0; i < nb_parameters; i++) | |
3057 | { | |
3058 | isl_id *tmp_id = isl_set_get_dim_id (scop->param_context, | |
3059 | isl_dim_param, i); | |
3060 | ip[tmp_id] = region->params[i]; | |
3061 | } | |
3062 | } | |
3063 | ||
3064 | ||
3065 | /* Generates a build, which specifies the constraints on the parameters. */ | |
3066 | ||
3067 | __isl_give isl_ast_build * | |
3068 | translate_isl_ast_to_gimple::generate_isl_context (scop_p scop) | |
3069 | { | |
3070 | isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context)); | |
3071 | return isl_ast_build_from_context (context_isl); | |
3072 | } | |
3073 | ||
3074 | /* Get the maximal number of schedule dimensions in the scop SCOP. */ | |
3075 | ||
3076 | int | |
3077 | translate_isl_ast_to_gimple::get_max_schedule_dimensions (scop_p scop) | |
3078 | { | |
3079 | int i; | |
3080 | poly_bb_p pbb; | |
3081 | int schedule_dims = 0; | |
3082 | ||
3083 | FOR_EACH_VEC_ELT (scop->pbbs, i, pbb) | |
3084 | { | |
3085 | int pbb_schedule_dims = isl_map_dim (pbb->transformed, isl_dim_out); | |
3086 | if (pbb_schedule_dims > schedule_dims) | |
3087 | schedule_dims = pbb_schedule_dims; | |
3088 | } | |
3089 | ||
3090 | return schedule_dims; | |
3091 | } | |
3092 | ||
3093 | /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions. | |
3094 | ||
3095 | For schedules with different dimensionality, the isl AST generator can not | |
3096 | define an order and will just randomly choose an order. The solution to this | |
3097 | problem is to extend all schedules to the maximal number of schedule | |
3098 | dimensions (using '0's for the remaining values). */ | |
3099 | ||
3100 | __isl_give isl_map * | |
3101 | translate_isl_ast_to_gimple::extend_schedule (__isl_take isl_map *schedule, | |
3102 | int nb_schedule_dims) | |
3103 | { | |
3104 | int tmp_dims = isl_map_dim (schedule, isl_dim_out); | |
3105 | schedule = | |
3106 | isl_map_add_dims (schedule, isl_dim_out, nb_schedule_dims - tmp_dims); | |
3107 | isl_val *zero = | |
3108 | isl_val_int_from_si (isl_map_get_ctx (schedule), 0); | |
3109 | int i; | |
3110 | for (i = tmp_dims; i < nb_schedule_dims; i++) | |
3111 | { | |
3112 | schedule | |
3113 | = isl_map_fix_val (schedule, isl_dim_out, i, isl_val_copy (zero)); | |
3114 | } | |
3115 | isl_val_free (zero); | |
3116 | return schedule; | |
3117 | } | |
3118 | ||
3119 | /* Generates a schedule, which specifies an order used to | |
3120 | visit elements in a domain. */ | |
3121 | ||
3122 | __isl_give isl_union_map * | |
3123 | translate_isl_ast_to_gimple::generate_isl_schedule (scop_p scop) | |
3124 | { | |
3125 | int nb_schedule_dims = get_max_schedule_dimensions (scop); | |
3126 | int i; | |
3127 | poly_bb_p pbb; | |
3128 | isl_union_map *schedule_isl = | |
3129 | isl_union_map_empty (isl_set_get_space (scop->param_context)); | |
3130 | ||
3131 | FOR_EACH_VEC_ELT (scop->pbbs, i, pbb) | |
3132 | { | |
3133 | /* Dead code elimination: when the domain of a PBB is empty, | |
3134 | don't generate code for the PBB. */ | |
3135 | if (isl_set_is_empty (pbb->domain)) | |
3136 | continue; | |
3137 | ||
3138 | isl_map *bb_schedule = isl_map_copy (pbb->transformed); | |
3139 | bb_schedule = isl_map_intersect_domain (bb_schedule, | |
3140 | isl_set_copy (pbb->domain)); | |
3141 | bb_schedule = extend_schedule (bb_schedule, nb_schedule_dims); | |
3142 | schedule_isl | |
3143 | = isl_union_map_union (schedule_isl, | |
3144 | isl_union_map_from_map (bb_schedule)); | |
3145 | } | |
3146 | return schedule_isl; | |
3147 | } | |
3148 | ||
3149 | /* This method is executed before the construction of a for node. */ | |
3150 | __isl_give isl_id * | |
3151 | ast_build_before_for (__isl_keep isl_ast_build *build, void *user) | |
3152 | { | |
3153 | isl_union_map *dependences = (isl_union_map *) user; | |
3154 | ast_build_info *for_info = XNEW (struct ast_build_info); | |
3155 | isl_union_map *schedule = isl_ast_build_get_schedule (build); | |
3156 | isl_space *schedule_space = isl_ast_build_get_schedule_space (build); | |
3157 | int dimension = isl_space_dim (schedule_space, isl_dim_out); | |
3158 | for_info->is_parallelizable = | |
3159 | !carries_deps (schedule, dependences, dimension); | |
3160 | isl_union_map_free (schedule); | |
3161 | isl_space_free (schedule_space); | |
3162 | isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info); | |
3163 | return id; | |
3164 | } | |
3165 | ||
3166 | /* Set the separate option for all dimensions. | |
3167 | This helps to reduce control overhead. */ | |
3168 | ||
3169 | __isl_give isl_ast_build * | |
3170 | translate_isl_ast_to_gimple::set_options (__isl_take isl_ast_build *control, | |
3171 | __isl_keep isl_union_map *schedule) | |
3172 | { | |
3173 | isl_ctx *ctx = isl_union_map_get_ctx (schedule); | |
3174 | isl_space *range_space = isl_space_set_alloc (ctx, 0, 1); | |
3175 | range_space = | |
3176 | isl_space_set_tuple_name (range_space, isl_dim_set, "separate"); | |
3177 | isl_union_set *range = | |
3178 | isl_union_set_from_set (isl_set_universe (range_space)); | |
3179 | isl_union_set *domain = isl_union_map_range (isl_union_map_copy (schedule)); | |
3180 | domain = isl_union_set_universe (domain); | |
3181 | isl_union_map *options = isl_union_map_from_domain_and_range (domain, range); | |
3182 | return isl_ast_build_set_options (control, options); | |
3183 | } | |
3184 | ||
3185 | /* Generate isl AST from schedule of SCOP. Also, collects IVS_PARAMS in IP. */ | |
3186 | ||
3187 | __isl_give isl_ast_node * | |
3188 | translate_isl_ast_to_gimple::scop_to_isl_ast (scop_p scop, ivs_params &ip) | |
3189 | { | |
3190 | /* Generate loop upper bounds that consist of the current loop iterator, an | |
3191 | operator (< or <=) and an expression not involving the iterator. If this | |
3192 | option is not set, then the current loop iterator may appear several times | |
3193 | in the upper bound. See the isl manual for more details. */ | |
3194 | isl_options_set_ast_build_atomic_upper_bound (scop->isl_context, true); | |
3195 | ||
3196 | add_parameters_to_ivs_params (scop, ip); | |
3197 | isl_union_map *schedule_isl = generate_isl_schedule (scop); | |
3198 | isl_ast_build *context_isl = generate_isl_context (scop); | |
3199 | context_isl = set_options (context_isl, schedule_isl); | |
3200 | isl_union_map *dependences = NULL; | |
3201 | if (flag_loop_parallelize_all) | |
3202 | { | |
3203 | dependences = scop_get_dependences (scop); | |
3204 | context_isl = | |
3205 | isl_ast_build_set_before_each_for (context_isl, ast_build_before_for, | |
3206 | dependences); | |
3207 | } | |
3208 | isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl, | |
3209 | schedule_isl); | |
3210 | if (dependences) | |
3211 | isl_union_map_free (dependences); | |
3212 | isl_ast_build_free (context_isl); | |
3213 | return ast_isl; | |
3214 | } | |
3215 | ||
3216 | /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for | |
3217 | the given SCOP. Return true if code generation succeeded. | |
3218 | ||
3219 | FIXME: This is not yet a full implementation of the code generator | |
3220 | with ISL ASTs. Generation of GIMPLE code has to be completed. */ | |
3221 | ||
3222 | bool | |
3223 | graphite_regenerate_ast_isl (scop_p scop) | |
3224 | { | |
3225 | sese_info_p region = scop->scop_info; | |
3226 | translate_isl_ast_to_gimple t (region); | |
3227 | ||
3228 | ifsese if_region = NULL; | |
3229 | isl_ast_node *root_node; | |
3230 | ivs_params ip; | |
3231 | ||
3232 | timevar_push (TV_GRAPHITE_CODE_GEN); | |
3233 | root_node = t.scop_to_isl_ast (scop, ip); | |
3234 | ||
3235 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3236 | { | |
ff17c262 | 3237 | fprintf (dump_file, "ISL AST generated by ISL: \n"); |
2ecf4eca AK |
3238 | t.print_isl_ast_node (dump_file, root_node, scop->isl_context); |
3239 | } | |
3240 | ||
3241 | recompute_all_dominators (); | |
3242 | graphite_verify (); | |
3243 | ||
3244 | if_region = move_sese_in_condition (region); | |
3245 | region->if_region = if_region; | |
3246 | recompute_all_dominators (); | |
3247 | ||
3248 | loop_p context_loop = region->region.entry->src->loop_father; | |
3249 | ||
3250 | edge e = single_succ_edge (if_region->true_region->region.entry->dest); | |
3251 | basic_block bb = split_edge (e); | |
a78cfa7f | 3252 | |
65b016eb AK |
3253 | /* Update the true_region exit edge. */ |
3254 | region->if_region->true_region->region.exit = single_succ_edge (bb); | |
d819fedb | 3255 | |
65b016eb AK |
3256 | t.translate_isl_ast (context_loop, root_node, e, ip); |
3257 | if (t.codegen_error_p ()) | |
3258 | { | |
3259 | if (dump_file) | |
ff17c262 AK |
3260 | fprintf (dump_file, "[codegen] unsuccessful," |
3261 | " reverting back to the original code.\n"); | |
65b016eb AK |
3262 | set_ifsese_condition (if_region, integer_zero_node); |
3263 | } | |
3264 | else | |
3265 | { | |
3266 | t.translate_pending_phi_nodes (); | |
3267 | if (!t.codegen_error_p ()) | |
3268 | { | |
3269 | sese_insert_phis_for_liveouts (region, | |
3270 | if_region->region->region.exit->src, | |
3271 | if_region->false_region->region.exit, | |
3272 | if_region->true_region->region.exit); | |
3273 | mark_virtual_operands_for_renaming (cfun); | |
3274 | update_ssa (TODO_update_ssa); | |
3275 | ||
3276 | ||
3277 | graphite_verify (); | |
3278 | scev_reset (); | |
3279 | recompute_all_dominators (); | |
3280 | graphite_verify (); | |
3281 | } | |
21c7259c AK |
3282 | else |
3283 | { | |
3284 | if (dump_file) | |
ff17c262 AK |
3285 | fprintf (dump_file, "[codegen] unsuccessful in translating" |
3286 | " pending phis, reverting back to the original code.\n"); | |
21c7259c AK |
3287 | set_ifsese_condition (if_region, integer_zero_node); |
3288 | } | |
65b016eb | 3289 | } |
a78cfa7f RG |
3290 | |
3291 | free (if_region->true_region); | |
3292 | free (if_region->region); | |
3293 | free (if_region); | |
3294 | ||
3295 | ivs_params_clear (ip); | |
f6cc3103 RG |
3296 | isl_ast_node_free (root_node); |
3297 | timevar_pop (TV_GRAPHITE_CODE_GEN); | |
574921c2 RG |
3298 | |
3299 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3300 | { | |
3301 | loop_p loop; | |
3302 | int num_no_dependency = 0; | |
3303 | ||
3304 | FOR_EACH_LOOP (loop, 0) | |
3305 | if (loop->can_be_parallel) | |
3306 | num_no_dependency++; | |
3307 | ||
ff17c262 | 3308 | fprintf (dump_file, "%d loops carried no dependency.\n", |
574921c2 RG |
3309 | num_no_dependency); |
3310 | } | |
3311 | ||
65b016eb | 3312 | return !t.codegen_error_p (); |
f6cc3103 | 3313 | } |
2ecf4eca | 3314 | |
9c358739 | 3315 | #endif /* HAVE_isl */ |