]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sese.c
df-scan.c (df_ref_create): Don't mark BB as dirty on debug insns.
[thirdparty/gcc.git] / gcc / sese.c
CommitLineData
2abae5f1
SP
1/* Single entry single exit control flow regions.
2 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Jan Sjodin <jan.sjodin@amd.com> and
4 Sebastian Pop <sebastian.pop@amd.com>.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "ggc.h"
27#include "tree.h"
28#include "rtl.h"
29#include "basic-block.h"
30#include "diagnostic.h"
31#include "tree-flow.h"
32#include "toplev.h"
33#include "tree-dump.h"
34#include "timevar.h"
35#include "cfgloop.h"
36#include "tree-chrec.h"
37#include "tree-data-ref.h"
38#include "tree-scalar-evolution.h"
39#include "tree-pass.h"
40#include "domwalk.h"
41#include "value-prof.h"
42#include "pointer-set.h"
43#include "gimple.h"
44#include "sese.h"
45
46/* Print to stderr the element ELT. */
47
48static void
49debug_rename_elt (rename_map_elt elt)
50{
51 fprintf (stderr, "(");
52 print_generic_expr (stderr, elt->old_name, 0);
53 fprintf (stderr, ", ");
54 print_generic_expr (stderr, elt->expr, 0);
55 fprintf (stderr, ")\n");
56}
57
58/* Helper function for debug_rename_map. */
59
60static int
61debug_rename_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
62{
63 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
64 debug_rename_elt (entry);
65 return 1;
66}
67
68/* Print to stderr all the elements of MAP. */
69
70void
71debug_rename_map (htab_t map)
72{
73 htab_traverse (map, debug_rename_map_1, NULL);
74}
75
76/* Computes a hash function for database element ELT. */
77
78hashval_t
79rename_map_elt_info (const void *elt)
80{
81 return htab_hash_pointer (((const struct rename_map_elt_s *) elt)->old_name);
82}
83
84/* Compares database elements E1 and E2. */
85
86int
87eq_rename_map_elts (const void *e1, const void *e2)
88{
89 const struct rename_map_elt_s *elt1 = (const struct rename_map_elt_s *) e1;
90 const struct rename_map_elt_s *elt2 = (const struct rename_map_elt_s *) e2;
91
92 return (elt1->old_name == elt2->old_name);
93}
94
95\f
96
97/* Print to stderr the element ELT. */
98
99static void
100debug_ivtype_elt (ivtype_map_elt elt)
101{
102 fprintf (stderr, "(%s, ", elt->cloog_iv);
103 print_generic_expr (stderr, elt->type, 0);
104 fprintf (stderr, ")\n");
105}
106
107/* Helper function for debug_ivtype_map. */
108
109static int
110debug_ivtype_map_1 (void **slot, void *s ATTRIBUTE_UNUSED)
111{
112 struct ivtype_map_elt_s *entry = (struct ivtype_map_elt_s *) *slot;
113 debug_ivtype_elt (entry);
114 return 1;
115}
116
117/* Print to stderr all the elements of MAP. */
118
119void
120debug_ivtype_map (htab_t map)
121{
122 htab_traverse (map, debug_ivtype_map_1, NULL);
123}
124
125/* Computes a hash function for database element ELT. */
126
127hashval_t
128ivtype_map_elt_info (const void *elt)
129{
130 return htab_hash_pointer (((const struct ivtype_map_elt_s *) elt)->cloog_iv);
131}
132
133/* Compares database elements E1 and E2. */
134
135int
136eq_ivtype_map_elts (const void *e1, const void *e2)
137{
138 const struct ivtype_map_elt_s *elt1 = (const struct ivtype_map_elt_s *) e1;
139 const struct ivtype_map_elt_s *elt2 = (const struct ivtype_map_elt_s *) e2;
140
141 return (elt1->cloog_iv == elt2->cloog_iv);
142}
143
144\f
145
146/* Record LOOP as occuring in REGION. */
147
148static void
149sese_record_loop (sese region, loop_p loop)
150{
151 if (sese_contains_loop (region, loop))
152 return;
153
154 bitmap_set_bit (SESE_LOOPS (region), loop->num);
155 VEC_safe_push (loop_p, heap, SESE_LOOP_NEST (region), loop);
156}
157
158/* Build the loop nests contained in REGION. Returns true when the
159 operation was successful. */
160
161void
162build_sese_loop_nests (sese region)
163{
164 unsigned i;
165 basic_block bb;
166 struct loop *loop0, *loop1;
167
168 FOR_EACH_BB (bb)
169 if (bb_in_sese_p (bb, region))
170 {
171 struct loop *loop = bb->loop_father;
172
173 /* Only add loops if they are completely contained in the SCoP. */
174 if (loop->header == bb
175 && bb_in_sese_p (loop->latch, region))
176 sese_record_loop (region, loop);
177 }
178
179 /* Make sure that the loops in the SESE_LOOP_NEST are ordered. It
180 can be the case that an inner loop is inserted before an outer
181 loop. To avoid this, semi-sort once. */
182 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop0); i++)
183 {
184 if (VEC_length (loop_p, SESE_LOOP_NEST (region)) == i + 1)
185 break;
186
187 loop1 = VEC_index (loop_p, SESE_LOOP_NEST (region), i + 1);
188 if (loop0->num > loop1->num)
189 {
190 VEC_replace (loop_p, SESE_LOOP_NEST (region), i, loop1);
191 VEC_replace (loop_p, SESE_LOOP_NEST (region), i + 1, loop0);
192 }
193 }
194}
195
196/* For a USE in BB, if BB is outside REGION, mark the USE in the
197 LIVEOUTS set. */
198
199static void
200sese_build_liveouts_use (sese region, bitmap liveouts, basic_block bb,
201 tree use)
202{
203 unsigned ver;
204 basic_block def_bb;
205
206 if (TREE_CODE (use) != SSA_NAME)
207 return;
208
209 ver = SSA_NAME_VERSION (use);
210 def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
211
212 if (!def_bb
213 || !bb_in_sese_p (def_bb, region)
214 || bb_in_sese_p (bb, region))
215 return;
216
217 bitmap_set_bit (liveouts, ver);
218}
219
220/* Marks for rewrite all the SSA_NAMES defined in REGION and that are
221 used in BB that is outside of the REGION. */
222
223static void
224sese_build_liveouts_bb (sese region, bitmap liveouts, basic_block bb)
225{
226 gimple_stmt_iterator bsi;
227 edge e;
228 edge_iterator ei;
229 ssa_op_iter iter;
230 use_operand_p use_p;
231
232 FOR_EACH_EDGE (e, ei, bb->succs)
233 for (bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi); gsi_next (&bsi))
234 sese_build_liveouts_use (region, liveouts, bb,
235 PHI_ARG_DEF_FROM_EDGE (gsi_stmt (bsi), e));
236
237 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
238 FOR_EACH_SSA_USE_OPERAND (use_p, gsi_stmt (bsi), iter, SSA_OP_ALL_USES)
239 sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
240}
241
242/* Build the LIVEOUTS of REGION: the set of variables defined inside
243 and used outside the REGION. */
244
245static void
246sese_build_liveouts (sese region, bitmap liveouts)
247{
248 basic_block bb;
249
250 FOR_EACH_BB (bb)
251 sese_build_liveouts_bb (region, liveouts, bb);
252}
253
254/* Builds a new SESE region from edges ENTRY and EXIT. */
255
256sese
257new_sese (edge entry, edge exit)
258{
259 sese region = XNEW (struct sese_s);
260
261 SESE_ENTRY (region) = entry;
262 SESE_EXIT (region) = exit;
263 SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
264 SESE_LOOP_NEST (region) = VEC_alloc (loop_p, heap, 3);
265 SESE_ADD_PARAMS (region) = true;
266 SESE_PARAMS (region) = VEC_alloc (tree, heap, 3);
267 SESE_PARAMS_INDEX (region) = htab_create (10, clast_name_index_elt_info,
268 eq_clast_name_indexes, free);
269 SESE_PARAMS_NAMES (region) = XNEWVEC (char *, num_ssa_names);
270
271 return region;
272}
273
274/* Deletes REGION. */
275
276void
277free_sese (sese region)
278{
279 if (SESE_LOOPS (region))
280 SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
281
282 VEC_free (tree, heap, SESE_PARAMS (region));
283 VEC_free (loop_p, heap, SESE_LOOP_NEST (region));
284
285 if (SESE_PARAMS_INDEX (region))
286 htab_delete (SESE_PARAMS_INDEX (region));
287
288 /* Do not free SESE_PARAMS_NAMES: CLooG does that. */
289
290 XDELETE (region);
291}
292
293/* Add exit phis for USE on EXIT. */
294
295static void
296sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
297{
298 gimple phi = create_phi_node (use, exit);
299
300 create_new_def_for (gimple_phi_result (phi), phi,
301 gimple_phi_result_ptr (phi));
302 add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
303 add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
304}
305
306/* Insert in the block BB phi nodes for variables defined in REGION
307 and used outside the REGION. The code generation moves REGION in
308 the else clause of an "if (1)" and generates code in the then
309 clause that is at this point empty:
310
311 | if (1)
312 | empty;
313 | else
314 | REGION;
315*/
316
317void
318sese_insert_phis_for_liveouts (sese region, basic_block bb,
319 edge false_e, edge true_e)
320{
321 unsigned i;
322 bitmap_iterator bi;
323 bitmap liveouts = BITMAP_ALLOC (NULL);
324
325 update_ssa (TODO_update_ssa);
326
327 sese_build_liveouts (region, liveouts);
328 EXECUTE_IF_SET_IN_BITMAP (liveouts, 0, i, bi)
329 sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
330 BITMAP_FREE (liveouts);
331
332 update_ssa (TODO_update_ssa);
333}
334
335/* Get the definition of NAME before the SESE. Keep track of the
336 basic blocks that have been VISITED in a bitmap. */
337
338static tree
339get_vdef_before_sese (sese region, tree name, sbitmap visited)
340{
341 unsigned i;
342 gimple def_stmt = SSA_NAME_DEF_STMT (name);
343 basic_block def_bb = gimple_bb (def_stmt);
344
345 if (!def_bb || !bb_in_sese_p (def_bb, region))
346 return name;
347
348 if (TEST_BIT (visited, def_bb->index))
349 return NULL_TREE;
350
351 SET_BIT (visited, def_bb->index);
352
353 switch (gimple_code (def_stmt))
354 {
355 case GIMPLE_PHI:
356 for (i = 0; i < gimple_phi_num_args (def_stmt); i++)
357 {
358 tree arg = gimple_phi_arg_def (def_stmt, i);
359 tree res = get_vdef_before_sese (region, arg, visited);
360 if (res)
361 return res;
362 }
363 return NULL_TREE;
364
365 default:
366 return NULL_TREE;
367 }
368}
369
370/* Adjust a virtual phi node PHI that is placed at the end of the
371 generated code for SCOP:
372
373 | if (1)
374 | generated code from REGION;
375 | else
376 | REGION;
377
378 The FALSE_E edge comes from the original code, TRUE_E edge comes
379 from the code generated for the SCOP. */
380
381static void
382sese_adjust_vphi (sese region, gimple phi, edge true_e)
383{
384 unsigned i;
385
386 gcc_assert (gimple_phi_num_args (phi) == 2);
387
388 for (i = 0; i < gimple_phi_num_args (phi); i++)
389 if (gimple_phi_arg_edge (phi, i) == true_e)
390 {
391 tree true_arg, false_arg, before_scop_arg;
392 sbitmap visited;
393
394 true_arg = gimple_phi_arg_def (phi, i);
395 if (!SSA_NAME_IS_DEFAULT_DEF (true_arg))
396 return;
397
398 false_arg = gimple_phi_arg_def (phi, i == 0 ? 1 : 0);
399 if (SSA_NAME_IS_DEFAULT_DEF (false_arg))
400 return;
401
402 visited = sbitmap_alloc (last_basic_block);
403 sbitmap_zero (visited);
404 before_scop_arg = get_vdef_before_sese (region, false_arg, visited);
405 gcc_assert (before_scop_arg != NULL_TREE);
406 SET_PHI_ARG_DEF (phi, i, before_scop_arg);
407 sbitmap_free (visited);
408 }
409}
410
411/* Returns the name associated to OLD_NAME in MAP. */
412
413static tree
414get_rename (htab_t map, tree old_name)
415{
416 struct rename_map_elt_s tmp;
417 PTR *slot;
418
419 tmp.old_name = old_name;
420 slot = htab_find_slot (map, &tmp, NO_INSERT);
421
422 if (slot && *slot)
423 return ((rename_map_elt) *slot)->expr;
424
425 return old_name;
426}
427
428/* Register in MAP the rename tuple (old_name, expr). */
429
430void
431set_rename (htab_t map, tree old_name, tree expr)
432{
433 struct rename_map_elt_s tmp;
434 PTR *slot;
435
436 if (old_name == expr)
437 return;
438
439 tmp.old_name = old_name;
440 slot = htab_find_slot (map, &tmp, INSERT);
441
442 if (!slot)
443 return;
444
445 if (*slot)
446 free (*slot);
447
448 *slot = new_rename_map_elt (old_name, expr);
449}
450
451/* Adjusts the phi nodes in the block BB for variables defined in
452 SCOP_REGION and used outside the SCOP_REGION. The code generation
453 moves SCOP_REGION in the else clause of an "if (1)" and generates
454 code in the then clause:
455
456 | if (1)
457 | generated code from REGION;
458 | else
459 | REGION;
460
461 To adjust the phi nodes after the condition, the RENAME_MAP is
462 used. */
463
464void
465sese_adjust_liveout_phis (sese region, htab_t rename_map, basic_block bb,
466 edge false_e, edge true_e)
467{
468 gimple_stmt_iterator si;
469
470 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
471 {
472 unsigned i;
473 unsigned false_i = 0;
474 gimple phi = gsi_stmt (si);
475
476 if (!is_gimple_reg (PHI_RESULT (phi)))
477 {
478 sese_adjust_vphi (region, phi, true_e);
479 continue;
480 }
481
482 for (i = 0; i < gimple_phi_num_args (phi); i++)
483 if (gimple_phi_arg_edge (phi, i) == false_e)
484 {
485 false_i = i;
486 break;
487 }
488
489 for (i = 0; i < gimple_phi_num_args (phi); i++)
490 if (gimple_phi_arg_edge (phi, i) == true_e)
491 {
492 tree old_name = gimple_phi_arg_def (phi, false_i);
493 tree expr = get_rename (rename_map, old_name);
494 gimple_seq stmts;
495
496 gcc_assert (old_name != expr);
497
498 if (TREE_CODE (expr) != SSA_NAME
499 && is_gimple_reg (old_name))
500 {
501 tree type = TREE_TYPE (old_name);
502 tree var = create_tmp_var (type, "var");
503
504 expr = build2 (MODIFY_EXPR, type, var, expr);
505 expr = force_gimple_operand (expr, &stmts, true, NULL);
506 gsi_insert_seq_on_edge_immediate (true_e, stmts);
507 }
508
509 SET_PHI_ARG_DEF (phi, i, expr);
510 }
511 }
512}
513
514/* Rename the SSA_NAMEs used in STMT and that appear in MAP. */
515
516static void
517rename_variables_in_stmt (gimple stmt, htab_t map, gimple_stmt_iterator *insert_gsi)
518{
519 ssa_op_iter iter;
520 use_operand_p use_p;
521
522 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
523 {
524 tree use = USE_FROM_PTR (use_p);
525 tree expr = get_rename (map, use);
526 tree type_use = TREE_TYPE (use);
527 tree type_expr = TREE_TYPE (expr);
528 gimple_seq stmts;
529
530 if (use == expr)
531 continue;
532
533 if (type_use != type_expr
534 || (TREE_CODE (expr) != SSA_NAME
535 && is_gimple_reg (use)))
536 {
537 tree var = create_tmp_var (type_use, "var");
538
539 if (type_use != type_expr)
540 expr = fold_convert (type_use, expr);
541
542 expr = build2 (MODIFY_EXPR, type_use, var, expr);
543 expr = force_gimple_operand (expr, &stmts, true, NULL);
544 gsi_insert_seq_before (insert_gsi, stmts, GSI_SAME_STMT);
545 }
546
547 replace_exp (use_p, expr);
548 }
549
550 update_stmt (stmt);
551}
552
553/* Returns true if NAME is a parameter of SESE. */
554
555static bool
556is_parameter (sese region, tree name)
557{
558 int i;
559 tree p;
560
561 for (i = 0; VEC_iterate (tree, SESE_PARAMS (region), i, p); i++)
562 if (p == name)
563 return true;
564
565 return false;
566}
567
568/* Returns true if NAME is an induction variable. */
569
570static bool
571is_iv (tree name)
572{
573 return gimple_code (SSA_NAME_DEF_STMT (name)) == GIMPLE_PHI;
574}
575
576static void expand_scalar_variables_stmt (gimple, basic_block, sese,
577 htab_t, gimple_stmt_iterator *);
578static tree
579expand_scalar_variables_expr (tree, tree, enum tree_code, tree, basic_block,
580 sese, htab_t, gimple_stmt_iterator *);
581
582static tree
583expand_scalar_variables_call (gimple stmt, basic_block bb, sese region,
584 htab_t map, gimple_stmt_iterator *gsi)
585{
586 int i, nargs = gimple_call_num_args (stmt);
587 VEC (tree, gc) *args = VEC_alloc (tree, gc, nargs);
588 tree fn_type = TREE_TYPE (gimple_call_fn (stmt));
589 tree fn = gimple_call_fndecl (stmt);
590 tree call_expr, var, lhs;
591 gimple call;
592
593 for (i = 0; i < nargs; i++)
594 {
595 tree arg = gimple_call_arg (stmt, i);
596 tree t = TREE_TYPE (arg);
597
598 var = create_tmp_var (t, "var");
599 arg = expand_scalar_variables_expr (t, arg, TREE_CODE (arg), NULL,
600 bb, region, map, gsi);
601 arg = build2 (MODIFY_EXPR, t, var, arg);
602 arg = force_gimple_operand_gsi (gsi, arg, true, NULL,
603 true, GSI_SAME_STMT);
604 VEC_quick_push (tree, args, arg);
605 }
606
607 lhs = gimple_call_lhs (stmt);
608 var = create_tmp_var (TREE_TYPE (lhs), "var");
609 call_expr = build_call_vec (fn_type, fn, args);
610 call = gimple_build_call_from_tree (call_expr);
611 var = make_ssa_name (var, call);
612 gimple_call_set_lhs (call, var);
613 gsi_insert_before (gsi, call, GSI_SAME_STMT);
614
615 return var;
616}
617
618/* Copies at GSI all the scalar computations on which the ssa_name OP0
619 depends on in the SESE: these are all the scalar variables used in
620 the definition of OP0, that are defined outside BB and still in the
621 SESE, i.e. not a parameter of the SESE. The expression that is
622 returned contains only induction variables from the generated code:
623 MAP contains the induction variables renaming mapping, and is used
624 to translate the names of induction variables. */
625
626static tree
627expand_scalar_variables_ssa_name (tree op0, basic_block bb,
628 sese region, htab_t map,
629 gimple_stmt_iterator *gsi)
630{
631 gimple def_stmt;
632 tree new_op;
633
634 if (is_parameter (region, op0)
635 || is_iv (op0))
636 return get_rename (map, op0);
637
638 def_stmt = SSA_NAME_DEF_STMT (op0);
639
640 /* Check whether we already have a rename for OP0. */
641 new_op = get_rename (map, op0);
642
643 if (new_op != op0
644 && gimple_bb (SSA_NAME_DEF_STMT (new_op)) == bb)
645 return new_op;
646
647 if (gimple_bb (def_stmt) == bb)
648 {
649 /* If the defining statement is in the basic block already
650 we do not need to create a new expression for it, we
651 only need to ensure its operands are expanded. */
652 expand_scalar_variables_stmt (def_stmt, bb, region, map, gsi);
653 return new_op;
654 }
655 else
656 {
657 if (!gimple_bb (def_stmt)
658 || !bb_in_sese_p (gimple_bb (def_stmt), region))
659 return new_op;
660
661 switch (gimple_code (def_stmt))
662 {
663 case GIMPLE_ASSIGN:
664 {
665 tree var0 = gimple_assign_rhs1 (def_stmt);
666 enum tree_code subcode = gimple_assign_rhs_code (def_stmt);
667 tree var1 = gimple_assign_rhs2 (def_stmt);
668 tree type = gimple_expr_type (def_stmt);
669
670 return expand_scalar_variables_expr (type, var0, subcode, var1, bb,
671 region, map, gsi);
672 }
673
674 case GIMPLE_CALL:
675 return expand_scalar_variables_call (def_stmt, bb, region, map, gsi);
676
677 default:
678 gcc_unreachable ();
679 return new_op;
680 }
681 }
682}
683
684/* Copies at GSI all the scalar computations on which the expression
685 OP0 CODE OP1 depends on in the SESE: these are all the scalar
686 variables used in OP0 and OP1, defined outside BB and still defined
687 in the SESE, i.e. not a parameter of the SESE. The expression that
688 is returned contains only induction variables from the generated
689 code: MAP contains the induction variables renaming mapping, and is
690 used to translate the names of induction variables. */
691
692static tree
693expand_scalar_variables_expr (tree type, tree op0, enum tree_code code,
694 tree op1, basic_block bb, sese region,
695 htab_t map, gimple_stmt_iterator *gsi)
696{
697 if (TREE_CODE_CLASS (code) == tcc_constant
698 || TREE_CODE_CLASS (code) == tcc_declaration)
699 return op0;
700
701 /* For data references we have to duplicate also its memory
702 indexing. */
703 if (TREE_CODE_CLASS (code) == tcc_reference)
704 {
705 switch (code)
706 {
707 case REALPART_EXPR:
708 case IMAGPART_EXPR:
709 {
710 tree op = TREE_OPERAND (op0, 0);
711 tree res = expand_scalar_variables_expr
712 (type, op, TREE_CODE (op), NULL, bb, region, map, gsi);
713 return build1 (code, type, res);
714 }
715
716 case INDIRECT_REF:
717 {
718 tree old_name = TREE_OPERAND (op0, 0);
719 tree expr = expand_scalar_variables_ssa_name
720 (old_name, bb, region, map, gsi);
721
722 if (TREE_CODE (expr) != SSA_NAME
723 && is_gimple_reg (old_name))
724 {
725 tree type = TREE_TYPE (old_name);
726 tree var = create_tmp_var (type, "var");
727
728 expr = build2 (MODIFY_EXPR, type, var, expr);
729 expr = force_gimple_operand_gsi (gsi, expr, true, NULL,
730 true, GSI_SAME_STMT);
731 }
732
733 return fold_build1 (code, type, expr);
734 }
735
736 case ARRAY_REF:
737 {
738 tree op00 = TREE_OPERAND (op0, 0);
739 tree op01 = TREE_OPERAND (op0, 1);
740 tree op02 = TREE_OPERAND (op0, 2);
741 tree op03 = TREE_OPERAND (op0, 3);
742 tree base = expand_scalar_variables_expr
743 (TREE_TYPE (op00), op00, TREE_CODE (op00), NULL, bb, region,
744 map, gsi);
745 tree subscript = expand_scalar_variables_expr
746 (TREE_TYPE (op01), op01, TREE_CODE (op01), NULL, bb, region,
747 map, gsi);
748
749 return build4 (ARRAY_REF, type, base, subscript, op02, op03);
750 }
751
752 default:
753 /* The above cases should catch everything. */
754 gcc_unreachable ();
755 }
756 }
757
758 if (TREE_CODE_CLASS (code) == tcc_unary)
759 {
760 tree op0_type = TREE_TYPE (op0);
761 enum tree_code op0_code = TREE_CODE (op0);
762 tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
763 NULL, bb, region, map, gsi);
764
765 return fold_build1 (code, type, op0_expr);
766 }
767
768 if (TREE_CODE_CLASS (code) == tcc_binary
769 || TREE_CODE_CLASS (code) == tcc_comparison)
770 {
771 tree op0_type = TREE_TYPE (op0);
772 enum tree_code op0_code = TREE_CODE (op0);
773 tree op0_expr = expand_scalar_variables_expr (op0_type, op0, op0_code,
774 NULL, bb, region, map, gsi);
775 tree op1_type = TREE_TYPE (op1);
776 enum tree_code op1_code = TREE_CODE (op1);
777 tree op1_expr = expand_scalar_variables_expr (op1_type, op1, op1_code,
778 NULL, bb, region, map, gsi);
779
780 return fold_build2 (code, type, op0_expr, op1_expr);
781 }
782
783 if (code == SSA_NAME)
784 return expand_scalar_variables_ssa_name (op0, bb, region, map, gsi);
785
786 if (code == ADDR_EXPR)
787 return op0;
788
789 gcc_unreachable ();
790 return NULL;
791}
792
793/* Copies at the beginning of BB all the scalar computations on which
794 STMT depends on in the SESE: these are all the scalar variables used
795 in STMT, defined outside BB and still defined in the SESE, i.e. not a
796 parameter of the SESE. The expression that is returned contains
797 only induction variables from the generated code: MAP contains the
798 induction variables renaming mapping, and is used to translate the
799 names of induction variables. */
800
801static void
802expand_scalar_variables_stmt (gimple stmt, basic_block bb, sese region,
803 htab_t map, gimple_stmt_iterator *gsi)
804{
805 ssa_op_iter iter;
806 use_operand_p use_p;
807
808 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
809 {
810 tree use = USE_FROM_PTR (use_p);
811 tree type = TREE_TYPE (use);
812 enum tree_code code = TREE_CODE (use);
813 tree use_expr;
814
815 if (!is_gimple_reg (use))
816 continue;
817
818 /* Don't expand USE if we already have a rename for it. */
819 use_expr = get_rename (map, use);
820 if (use_expr != use)
821 continue;
822
823 use_expr = expand_scalar_variables_expr (type, use, code, NULL, bb,
824 region, map, gsi);
825 use_expr = fold_convert (type, use_expr);
826
827 if (use_expr == use)
828 continue;
829
830 if (TREE_CODE (use_expr) != SSA_NAME)
831 {
832 tree var = create_tmp_var (type, "var");
833
834 use_expr = build2 (MODIFY_EXPR, type, var, use_expr);
835 use_expr = force_gimple_operand_gsi (gsi, use_expr, true, NULL,
836 true, GSI_SAME_STMT);
837 }
838
839 replace_exp (use_p, use_expr);
840 }
841
842 update_stmt (stmt);
843}
844
845/* Copies at the beginning of BB all the scalar computations on which
846 BB depends on in the SESE: these are all the scalar variables used
847 in BB, defined outside BB and still defined in the SESE, i.e. not a
848 parameter of the SESE. The expression that is returned contains
849 only induction variables from the generated code: MAP contains the
850 induction variables renaming mapping, and is used to translate the
851 names of induction variables. */
852
853static void
854expand_scalar_variables (basic_block bb, sese region, htab_t map)
855{
856 gimple_stmt_iterator gsi;
857
858 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);)
859 {
860 gimple stmt = gsi_stmt (gsi);
861 expand_scalar_variables_stmt (stmt, bb, region, map, &gsi);
862 gsi_next (&gsi);
863 }
864}
865
866/* Rename all the SSA_NAMEs from block BB according to the MAP. */
867
868static void
869rename_variables (basic_block bb, htab_t map)
870{
871 gimple_stmt_iterator gsi;
872 gimple_stmt_iterator insert_gsi = gsi_start_bb (bb);
873
874 for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
875 rename_variables_in_stmt (gsi_stmt (gsi), map, &insert_gsi);
876}
877
878/* Remove condition from BB. */
879
880static void
881remove_condition (basic_block bb)
882{
883 gimple last = last_stmt (bb);
884
885 if (last && gimple_code (last) == GIMPLE_COND)
886 {
887 gimple_stmt_iterator gsi = gsi_last_bb (bb);
888 gsi_remove (&gsi, true);
889 }
890}
891
892/* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
893
894edge
895get_true_edge_from_guard_bb (basic_block bb)
896{
897 edge e;
898 edge_iterator ei;
899
900 FOR_EACH_EDGE (e, ei, bb->succs)
901 if (e->flags & EDGE_TRUE_VALUE)
902 return e;
903
904 gcc_unreachable ();
905 return NULL;
906}
907
908/* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
909
910edge
911get_false_edge_from_guard_bb (basic_block bb)
912{
913 edge e;
914 edge_iterator ei;
915
916 FOR_EACH_EDGE (e, ei, bb->succs)
917 if (!(e->flags & EDGE_TRUE_VALUE))
918 return e;
919
920 gcc_unreachable ();
921 return NULL;
922}
923
924/* Returns true when NAME is defined in LOOP. */
925
926static bool
927defined_in_loop_p (tree name, loop_p loop)
928{
929 gimple stmt = SSA_NAME_DEF_STMT (name);
930
931 return (gimple_bb (stmt)->loop_father == loop);
932}
933
934/* Returns the gimple statement that uses NAME outside the loop it is
935 defined in, returns NULL if there is no such loop close phi node.
936 An invariant of the loop closed SSA form is that the only use of a
937 variable, outside the loop it is defined in, is in the loop close
938 phi node that just follows the loop. */
939
940static gimple
941alive_after_loop (tree name)
942{
943 use_operand_p use_p;
944 imm_use_iterator imm_iter;
945 loop_p loop = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_father;
946
947 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, name)
948 {
949 gimple stmt = USE_STMT (use_p);
950
951 if (gimple_code (stmt) == GIMPLE_PHI
952 && gimple_bb (stmt)->loop_father != loop)
953 return stmt;
954 }
955
956 return NULL;
957}
958
959/* Return true if a close phi has not yet been inserted for the use of
960 variable NAME on the single exit of LOOP. */
961
962static bool
963close_phi_not_yet_inserted_p (loop_p loop, tree name)
964{
965 gimple_stmt_iterator psi;
966 basic_block bb = single_exit (loop)->dest;
967
968 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
969 if (gimple_phi_arg_def (gsi_stmt (psi), 0) == name)
970 return false;
971
972 return true;
973}
974
975/* A structure for passing parameters to add_loop_exit_phis. */
976
977typedef struct alep {
978 loop_p loop;
979 VEC (rename_map_elt, heap) *new_renames;
980} *alep_p;
981
982/* Helper function for htab_traverse in insert_loop_close_phis. */
983
984static int
985add_loop_exit_phis (void **slot, void *data)
986{
987 struct rename_map_elt_s *entry;
988 alep_p a;
989 loop_p loop;
990 tree expr, new_name;
991 bool def_in_loop_p, used_outside_p, need_close_phi_p;
992 gimple old_close_phi;
993
994 if (!slot || !data)
995 return 1;
996
997 entry = (struct rename_map_elt_s *) *slot;
998 a = (alep_p) data;
999 loop = a->loop;
1000 expr = entry->expr;
1001
1002 if (TREE_CODE (expr) != SSA_NAME)
1003 return 1;
1004
1005 new_name = expr;
1006 def_in_loop_p = defined_in_loop_p (new_name, loop);
1007 old_close_phi = alive_after_loop (entry->old_name);
1008 used_outside_p = (old_close_phi != NULL);
1009 need_close_phi_p = (def_in_loop_p && used_outside_p
1010 && close_phi_not_yet_inserted_p (loop, new_name));
1011
1012 /* Insert a loop close phi node. */
1013 if (need_close_phi_p)
1014 {
1015 basic_block bb = single_exit (loop)->dest;
1016 gimple phi = create_phi_node (new_name, bb);
1017 tree new_res = create_new_def_for (gimple_phi_result (phi), phi,
1018 gimple_phi_result_ptr (phi));
1019
1020 add_phi_arg (phi, new_name, single_pred_edge (bb), UNKNOWN_LOCATION);
1021 VEC_safe_push (rename_map_elt, heap, a->new_renames,
1022 new_rename_map_elt (gimple_phi_result (old_close_phi),
1023 new_res));
1024 }
1025
1026 /* Remove the old rename from the map. */
1027 if (def_in_loop_p && *slot)
1028 {
1029 free (*slot);
1030 *slot = NULL;
1031 }
1032
1033 return 1;
1034}
1035
1036/* Traverses MAP and removes from it all the tuples (OLD, NEW) where
1037 NEW is defined in LOOP. Inserts on the exit of LOOP the close phi
1038 node "RES = phi (NEW)" corresponding to "OLD_RES = phi (OLD)" in
1039 the original code. Inserts in MAP the tuple (OLD_RES, RES). */
1040
1041void
1042insert_loop_close_phis (htab_t map, loop_p loop)
1043{
1044 int i;
1045 struct alep a;
1046 rename_map_elt elt;
1047
1048 a.loop = loop;
1049 a.new_renames = VEC_alloc (rename_map_elt, heap, 3);
1050 update_ssa (TODO_update_ssa);
1051 htab_traverse (map, add_loop_exit_phis, &a);
1052 update_ssa (TODO_update_ssa);
1053
1054 for (i = 0; VEC_iterate (rename_map_elt, a.new_renames, i, elt); i++)
1055 {
1056 set_rename (map, elt->old_name, elt->expr);
1057 free (elt);
1058 }
1059
1060 VEC_free (rename_map_elt, heap, a.new_renames);
1061}
1062
1063/* Helper structure for htab_traverse in insert_guard_phis. */
1064
1065struct igp {
1066 basic_block bb;
1067 edge true_edge, false_edge;
1068 htab_t before_guard;
1069};
1070
1071/* Return the default name that is before the guard. */
1072
1073static tree
1074default_before_guard (htab_t before_guard, tree old_name)
1075{
1076 tree res = get_rename (before_guard, old_name);
1077
1078 if (res == old_name)
1079 {
1080 if (is_gimple_reg (res))
1081 return fold_convert (TREE_TYPE (res), integer_zero_node);
1082 return gimple_default_def (cfun, SSA_NAME_VAR (res));
1083 }
1084
1085 return res;
1086}
1087
30de1632
SP
1088/* Prepares EXPR to be a good phi argument when the phi result is
1089 RES. Insert needed statements on edge E. */
1090
1091static tree
1092convert_for_phi_arg (tree expr, tree res, edge e)
1093{
1094 tree type = TREE_TYPE (res);
1095
1096 if (TREE_TYPE (expr) != type)
1097 expr = fold_convert (type, expr);
1098
1099 if (TREE_CODE (expr) != SSA_NAME
1100 && !is_gimple_min_invariant (expr))
1101 {
1102 tree var = create_tmp_var (type, "var");
1103 gimple_seq stmts;
1104
1105 expr = build2 (MODIFY_EXPR, type, var, expr);
1106 expr = force_gimple_operand (expr, &stmts, true, NULL);
1107 gsi_insert_seq_on_edge_immediate (e, stmts);
1108 }
1109
1110 return expr;
1111}
1112
2abae5f1
SP
1113/* Helper function for htab_traverse in insert_guard_phis. */
1114
1115static int
1116add_guard_exit_phis (void **slot, void *s)
1117{
1118 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
1119 struct igp *i = (struct igp *) s;
1120 basic_block bb = i->bb;
1121 edge true_edge = i->true_edge;
1122 edge false_edge = i->false_edge;
30de1632 1123 tree res = entry->old_name;
2abae5f1 1124 tree name1 = entry->expr;
30de1632 1125 tree name2 = default_before_guard (i->before_guard, res);
2abae5f1 1126 gimple phi;
2abae5f1
SP
1127
1128 /* Nothing to be merged if the name before the guard is the same as
1129 the one after. */
1130 if (name1 == name2)
1131 return 1;
1132
30de1632
SP
1133 name1 = convert_for_phi_arg (name1, res, true_edge);
1134 name2 = convert_for_phi_arg (name2, res, false_edge);
2abae5f1 1135
30de1632 1136 phi = create_phi_node (res, bb);
2abae5f1
SP
1137 res = create_new_def_for (gimple_phi_result (phi), phi,
1138 gimple_phi_result_ptr (phi));
1139
1140 add_phi_arg (phi, name1, true_edge, UNKNOWN_LOCATION);
1141 add_phi_arg (phi, name2, false_edge, UNKNOWN_LOCATION);
1142
1143 entry->expr = res;
1144 *slot = entry;
1145 return 1;
1146}
1147
1148/* Iterate over RENAME_MAP and get tuples of the form (OLD, NAME1).
1149 If there is a correspondent tuple (OLD, NAME2) in BEFORE_GUARD,
1150 with NAME1 different than NAME2, then insert in BB the phi node:
30de1632 1151
2abae5f1
SP
1152 | RES = phi (NAME1 (on TRUE_EDGE), NAME2 (on FALSE_EDGE))"
1153
1154 if there is no tuple for OLD in BEFORE_GUARD, insert
1155
1156 | RES = phi (NAME1 (on TRUE_EDGE),
1157 | DEFAULT_DEFINITION of NAME1 (on FALSE_EDGE))".
1158
1159 Finally register in RENAME_MAP the tuple (OLD, RES). */
1160
1161void
1162insert_guard_phis (basic_block bb, edge true_edge, edge false_edge,
1163 htab_t before_guard, htab_t rename_map)
1164{
1165 struct igp i;
1166 i.bb = bb;
1167 i.true_edge = true_edge;
1168 i.false_edge = false_edge;
1169 i.before_guard = before_guard;
1170
1171 update_ssa (TODO_update_ssa);
1172 htab_traverse (rename_map, add_guard_exit_phis, &i);
1173 update_ssa (TODO_update_ssa);
1174}
1175
1176/* Create a duplicate of the basic block BB. NOTE: This does not
1177 preserve SSA form. */
1178
1179static void
1180graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb, htab_t map)
1181{
1182 gimple_stmt_iterator gsi, gsi_tgt;
1183
1184 gsi_tgt = gsi_start_bb (new_bb);
1185 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1186 {
1187 def_operand_p def_p;
1188 ssa_op_iter op_iter;
2abae5f1
SP
1189 gimple stmt = gsi_stmt (gsi);
1190 gimple copy;
1191
1192 if (gimple_code (stmt) == GIMPLE_LABEL)
1193 continue;
1194
1195 /* Create a new copy of STMT and duplicate STMT's virtual
1196 operands. */
1197 copy = gimple_copy (stmt);
1198 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1199 mark_sym_for_renaming (gimple_vop (cfun));
1200
1d65f45c 1201 maybe_duplicate_eh_stmt (copy, stmt);
2abae5f1
SP
1202 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1203
1204 /* Create new names for all the definitions created by COPY and
1205 add replacement mappings for each new name. */
1206 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
1207 {
1208 tree old_name = DEF_FROM_PTR (def_p);
1209 tree new_name = create_new_def_for (old_name, copy, def_p);
1210 set_rename (map, old_name, new_name);
1211 }
1212 }
1213}
1214
1215/* Copies BB and includes in the copied BB all the statements that can
1216 be reached following the use-def chains from the memory accesses,
1217 and returns the next edge following this new block. */
1218
1219edge
1220copy_bb_and_scalar_dependences (basic_block bb, sese region,
1221 edge next_e, htab_t map)
1222{
1223 basic_block new_bb = split_edge (next_e);
1224
1225 next_e = single_succ_edge (new_bb);
1226 graphite_copy_stmts_from_block (bb, new_bb, map);
1227 remove_condition (new_bb);
1228 remove_phi_nodes (new_bb);
1229 expand_scalar_variables (new_bb, region, map);
1230 rename_variables (new_bb, map);
1231
1232 return next_e;
1233}
1234
1235/* Returns the outermost loop in SCOP that contains BB. */
1236
1237struct loop *
1238outermost_loop_in_sese (sese region, basic_block bb)
1239{
1240 struct loop *nest;
1241
1242 nest = bb->loop_father;
1243 while (loop_outer (nest)
1244 && loop_in_sese_p (loop_outer (nest), region))
1245 nest = loop_outer (nest);
1246
1247 return nest;
1248}
1249
1250/* Sets the false region of an IF_REGION to REGION. */
1251
1252void
1253if_region_set_false_region (ifsese if_region, sese region)
1254{
1255 basic_block condition = if_region_get_condition_block (if_region);
1256 edge false_edge = get_false_edge_from_guard_bb (condition);
1257 basic_block dummy = false_edge->dest;
1258 edge entry_region = SESE_ENTRY (region);
1259 edge exit_region = SESE_EXIT (region);
1260 basic_block before_region = entry_region->src;
1261 basic_block last_in_region = exit_region->src;
1262 void **slot = htab_find_slot_with_hash (current_loops->exits, exit_region,
1263 htab_hash_pointer (exit_region),
1264 NO_INSERT);
1265
1266 entry_region->flags = false_edge->flags;
1267 false_edge->flags = exit_region->flags;
1268
1269 redirect_edge_pred (entry_region, condition);
1270 redirect_edge_pred (exit_region, before_region);
1271 redirect_edge_pred (false_edge, last_in_region);
1272 redirect_edge_succ (false_edge, single_succ (dummy));
1273 delete_basic_block (dummy);
1274
1275 exit_region->flags = EDGE_FALLTHRU;
1276 recompute_all_dominators ();
1277
1278 SESE_EXIT (region) = false_edge;
1279 if_region->false_region = region;
1280
1281 if (slot)
1282 {
1283 struct loop_exit *loop_exit = GGC_CNEW (struct loop_exit);
1284
1285 memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
1286 htab_clear_slot (current_loops->exits, slot);
1287
1288 slot = htab_find_slot_with_hash (current_loops->exits, false_edge,
1289 htab_hash_pointer (false_edge),
1290 INSERT);
1291 loop_exit->e = false_edge;
1292 *slot = loop_exit;
1293 false_edge->src->loop_father->exits->next = loop_exit;
1294 }
1295}
1296
1297/* Creates an IFSESE with CONDITION on edge ENTRY. */
1298
1299ifsese
1300create_if_region_on_edge (edge entry, tree condition)
1301{
1302 edge e;
1303 edge_iterator ei;
1304 sese sese_region = GGC_NEW (struct sese_s);
1305 sese true_region = GGC_NEW (struct sese_s);
1306 sese false_region = GGC_NEW (struct sese_s);
1307 ifsese if_region = GGC_NEW (struct ifsese_s);
1308 edge exit = create_empty_if_region_on_edge (entry, condition);
1309
1310 if_region->region = sese_region;
1311 if_region->region->entry = entry;
1312 if_region->region->exit = exit;
1313
1314 FOR_EACH_EDGE (e, ei, entry->dest->succs)
1315 {
1316 if (e->flags & EDGE_TRUE_VALUE)
1317 {
1318 true_region->entry = e;
1319 true_region->exit = single_succ_edge (e->dest);
1320 if_region->true_region = true_region;
1321 }
1322 else if (e->flags & EDGE_FALSE_VALUE)
1323 {
1324 false_region->entry = e;
1325 false_region->exit = single_succ_edge (e->dest);
1326 if_region->false_region = false_region;
1327 }
1328 }
1329
1330 return if_region;
1331}
1332
1333/* Moves REGION in a condition expression:
1334 | if (1)
1335 | ;
1336 | else
1337 | REGION;
1338*/
1339
1340ifsese
1341move_sese_in_condition (sese region)
1342{
1343 basic_block pred_block = split_edge (SESE_ENTRY (region));
1344 ifsese if_region = NULL;
1345
1346 SESE_ENTRY (region) = single_succ_edge (pred_block);
1347 if_region = create_if_region_on_edge (single_pred_edge (pred_block), integer_one_node);
1348 if_region_set_false_region (if_region, region);
1349
1350 return if_region;
1351}
1352
1353/* Reset the loop->aux pointer for all loops in REGION. */
1354
1355void
1356sese_reset_aux_in_loops (sese region)
1357{
1358 int i;
1359 loop_p loop;
1360
1361 for (i = 0; VEC_iterate (loop_p, SESE_LOOP_NEST (region), i, loop); i++)
1362 loop->aux = NULL;
1363}
1364
1365/* Returns the scalar evolution of T in REGION. Every variable that
1366 is not defined in the REGION is considered a parameter. */
1367
1368tree
1369scalar_evolution_in_region (sese region, loop_p loop, tree t)
1370{
1371 gimple def;
1372 struct loop *def_loop;
1373 basic_block before = block_before_sese (region);
1374
1375 if (TREE_CODE (t) != SSA_NAME
1376 || loop_in_sese_p (loop, region))
1377 return instantiate_scev (before, loop,
1378 analyze_scalar_evolution (loop, t));
1379
1380 if (!defined_in_sese_p (t, region))
1381 return t;
1382
1383 def = SSA_NAME_DEF_STMT (t);
1384 def_loop = loop_containing_stmt (def);
1385
1386 if (loop_in_sese_p (def_loop, region))
1387 {
1388 t = analyze_scalar_evolution (def_loop, t);
1389 def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
1390 t = compute_overall_effect_of_inner_loop (def_loop, t);
1391 return t;
1392 }
1393 else
1394 return instantiate_scev (before, loop, t);
1395}