2016-04-27 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / sese.c
blob08ea47d010d5947fb209771912d9ab008c03a1bc
1 /* Single entry single exit control flow regions.
2 Copyright (C) 2008-2016 Free Software Foundation, Inc.
3 Contributed by Jan Sjodin <jan.sjodin@amd.com> and
4 Sebastian Pop <sebastian.pop@amd.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "cfghooks.h"
29 #include "tree-pass.h"
30 #include "ssa.h"
31 #include "tree-pretty-print.h"
32 #include "fold-const.h"
33 #include "gimplify.h"
34 #include "gimple-iterator.h"
35 #include "gimple-pretty-print.h"
36 #include "gimplify-me.h"
37 #include "tree-cfg.h"
38 #include "tree-ssa-loop.h"
39 #include "tree-into-ssa.h"
40 #include "cfgloop.h"
41 #include "tree-data-ref.h"
42 #include "tree-scalar-evolution.h"
43 #include "sese.h"
44 #include "tree-ssa-propagate.h"
46 /* For a USE in BB, if BB is outside REGION, mark the USE in the
47 LIVEOUTS set. */
49 static void
50 sese_build_liveouts_use (sese_info_p region, bitmap liveouts, basic_block bb,
51 tree use)
53 gcc_assert (!bb_in_sese_p (bb, region->region));
54 if (TREE_CODE (use) != SSA_NAME)
55 return;
57 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
59 if (!def_bb || !bb_in_sese_p (def_bb, region->region))
60 return;
62 unsigned ver = SSA_NAME_VERSION (use);
63 bitmap_set_bit (liveouts, ver);
66 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
67 used in BB that is outside of the REGION. */
69 static void
70 sese_build_liveouts_bb (sese_info_p region, bitmap liveouts, basic_block bb)
72 edge e;
73 edge_iterator ei;
74 ssa_op_iter iter;
75 use_operand_p use_p;
77 FOR_EACH_EDGE (e, ei, bb->succs)
78 for (gphi_iterator bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi);
79 gsi_next (&bsi))
80 sese_build_liveouts_use (region, liveouts, bb,
81 PHI_ARG_DEF_FROM_EDGE (bsi.phi (), e));
83 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
84 gsi_next (&bsi))
86 gimple *stmt = gsi_stmt (bsi);
88 if (is_gimple_debug (stmt))
89 continue;
91 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
92 sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
96 /* For a USE in BB, return true if BB is outside REGION and it's not
97 in the LIVEOUTS set. */
99 static bool
100 sese_bad_liveouts_use (sese_info_p region, bitmap liveouts, basic_block bb,
101 tree use)
103 gcc_assert (!bb_in_sese_p (bb, region->region));
105 if (TREE_CODE (use) != SSA_NAME)
106 return false;
108 unsigned ver = SSA_NAME_VERSION (use);
110 /* If it's in liveouts, the variable will get a new PHI node, and
111 the debug use will be properly adjusted. */
112 if (bitmap_bit_p (liveouts, ver))
113 return false;
115 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
117 if (!def_bb || !bb_in_sese_p (def_bb, region->region))
118 return false;
120 return true;
123 /* Reset debug stmts that reference SSA_NAMES defined in REGION that
124 are not marked as liveouts. */
126 static void
127 sese_reset_debug_liveouts_bb (sese_info_p region, bitmap liveouts,
128 basic_block bb)
130 gimple_stmt_iterator bsi;
131 ssa_op_iter iter;
132 use_operand_p use_p;
134 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
136 gimple *stmt = gsi_stmt (bsi);
138 if (!is_gimple_debug (stmt))
139 continue;
141 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
142 if (sese_bad_liveouts_use (region, liveouts, bb,
143 USE_FROM_PTR (use_p)))
145 gimple_debug_bind_reset_value (stmt);
146 update_stmt (stmt);
147 break;
152 /* Build the LIVEOUTS of REGION: the set of variables defined inside
153 and used outside the REGION. */
155 static void
156 sese_build_liveouts (sese_info_p region, bitmap liveouts)
158 basic_block bb;
160 /* FIXME: We could start iterating form the successor of sese. */
161 FOR_EACH_BB_FN (bb, cfun)
162 if (!bb_in_sese_p (bb, region->region))
163 sese_build_liveouts_bb (region, liveouts, bb);
165 /* FIXME: We could start iterating form the successor of sese. */
166 if (MAY_HAVE_DEBUG_STMTS)
167 FOR_EACH_BB_FN (bb, cfun)
168 if (!bb_in_sese_p (bb, region->region))
169 sese_reset_debug_liveouts_bb (region, liveouts, bb);
172 /* Builds a new SESE region from edges ENTRY and EXIT. */
174 sese_info_p
175 new_sese_info (edge entry, edge exit)
177 sese_info_p region = XNEW (struct sese_info_t);
179 region->region.entry = entry;
180 region->region.exit = exit;
181 region->loop_nest.create (3);
182 region->params.create (3);
183 region->rename_map = new rename_map_t;
184 region->parameter_rename_map = new parameter_rename_map_t;
185 region->copied_bb_map = new bb_map_t;
186 region->bbs.create (3);
187 region->incomplete_phis.create (3);
190 return region;
193 /* Deletes REGION. */
195 void
196 free_sese_info (sese_info_p region)
198 region->params.release ();
199 region->loop_nest.release ();
201 for (rename_map_t::iterator it = region->rename_map->begin ();
202 it != region->rename_map->begin (); ++it)
203 (*it).second.release ();
205 for (bb_map_t::iterator it = region->copied_bb_map->begin ();
206 it != region->copied_bb_map->begin (); ++it)
207 (*it).second.release ();
209 delete region->rename_map;
210 delete region->parameter_rename_map;
211 delete region->copied_bb_map;
213 region->rename_map = NULL;
214 region->parameter_rename_map = NULL;
215 region->copied_bb_map = NULL;
217 region->bbs.release ();
218 region->incomplete_phis.release ();
220 XDELETE (region);
223 /* Add exit phis for USE on EXIT. */
225 static void
226 sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
228 gphi *phi = create_phi_node (NULL_TREE, exit);
229 create_new_def_for (use, phi, gimple_phi_result_ptr (phi));
230 add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
231 add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
232 update_stmt (phi);
235 /* Insert in the block BB phi nodes for variables defined in REGION
236 and used outside the REGION. The code generation moves REGION in
237 the else clause of an "if (1)" and generates code in the then
238 clause that is at this point empty:
240 | if (1)
241 | empty;
242 | else
243 | REGION;
246 void
247 sese_insert_phis_for_liveouts (sese_info_p region, basic_block bb,
248 edge false_e, edge true_e)
250 unsigned i;
251 bitmap_iterator bi;
252 bitmap liveouts = BITMAP_ALLOC (NULL);
254 sese_build_liveouts (region, liveouts);
256 EXECUTE_IF_SET_IN_BITMAP (liveouts, 0, i, bi)
257 if (!virtual_operand_p (ssa_name (i)))
258 sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
260 BITMAP_FREE (liveouts);
263 /* Returns the outermost loop in SCOP that contains BB. */
265 struct loop *
266 outermost_loop_in_sese_1 (sese_l &region, basic_block bb)
268 struct loop *nest;
270 nest = bb->loop_father;
271 while (loop_outer (nest)
272 && loop_in_sese_p (loop_outer (nest), region))
273 nest = loop_outer (nest);
275 return nest;
278 /* Same as outermost_loop_in_sese_1, returns the outermost loop
279 containing BB in REGION, but makes sure that the returned loop
280 belongs to the REGION, and so this returns the first loop in the
281 REGION when the loop containing BB does not belong to REGION. */
283 loop_p
284 outermost_loop_in_sese (sese_l &region, basic_block bb)
286 loop_p nest = outermost_loop_in_sese_1 (region, bb);
288 if (loop_in_sese_p (nest, region))
289 return nest;
291 /* When the basic block BB does not belong to a loop in the region,
292 return the first loop in the region. */
293 nest = nest->inner;
294 while (nest)
295 if (loop_in_sese_p (nest, region))
296 break;
297 else
298 nest = nest->next;
300 gcc_assert (nest);
301 return nest;
304 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
306 edge
307 get_true_edge_from_guard_bb (basic_block bb)
309 edge e;
310 edge_iterator ei;
312 FOR_EACH_EDGE (e, ei, bb->succs)
313 if (e->flags & EDGE_TRUE_VALUE)
314 return e;
316 gcc_unreachable ();
317 return NULL;
320 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
322 edge
323 get_false_edge_from_guard_bb (basic_block bb)
325 edge e;
326 edge_iterator ei;
328 FOR_EACH_EDGE (e, ei, bb->succs)
329 if (!(e->flags & EDGE_TRUE_VALUE))
330 return e;
332 gcc_unreachable ();
333 return NULL;
336 /* Sets the false region of an IF_REGION to REGION. */
338 void
339 if_region_set_false_region (ifsese if_region, sese_info_p region)
341 basic_block condition = if_region_get_condition_block (if_region);
342 edge false_edge = get_false_edge_from_guard_bb (condition);
343 basic_block dummy = false_edge->dest;
344 edge entry_region = region->region.entry;
345 edge exit_region = region->region.exit;
346 basic_block before_region = entry_region->src;
347 basic_block last_in_region = exit_region->src;
348 hashval_t hash = htab_hash_pointer (exit_region);
349 loop_exit **slot
350 = current_loops->exits->find_slot_with_hash (exit_region, hash, NO_INSERT);
352 entry_region->flags = false_edge->flags;
353 false_edge->flags = exit_region->flags;
355 redirect_edge_pred (entry_region, condition);
356 redirect_edge_pred (exit_region, before_region);
357 redirect_edge_pred (false_edge, last_in_region);
358 redirect_edge_succ (false_edge, single_succ (dummy));
359 delete_basic_block (dummy);
361 exit_region->flags = EDGE_FALLTHRU;
362 recompute_all_dominators ();
364 region->region.exit = false_edge;
366 free (if_region->false_region);
367 if_region->false_region = region;
369 if (slot)
371 struct loop_exit *loop_exit = ggc_cleared_alloc<struct loop_exit> ();
373 memcpy (loop_exit, *((struct loop_exit **) slot),
374 sizeof (struct loop_exit));
375 current_loops->exits->clear_slot (slot);
377 hashval_t hash = htab_hash_pointer (false_edge);
378 slot = current_loops->exits->find_slot_with_hash (false_edge, hash,
379 INSERT);
380 loop_exit->e = false_edge;
381 *slot = loop_exit;
382 false_edge->src->loop_father->exits->next = loop_exit;
386 /* Creates an IFSESE with CONDITION on edge ENTRY. */
388 static ifsese
389 create_if_region_on_edge (edge entry, tree condition)
391 edge e;
392 edge_iterator ei;
393 sese_info_p sese_region = XNEW (struct sese_info_t);
394 sese_info_p true_region = XNEW (struct sese_info_t);
395 sese_info_p false_region = XNEW (struct sese_info_t);
396 ifsese if_region = XNEW (struct ifsese_s);
397 edge exit = create_empty_if_region_on_edge (entry, condition);
399 if_region->region = sese_region;
400 if_region->region->region.entry = entry;
401 if_region->region->region.exit = exit;
403 FOR_EACH_EDGE (e, ei, entry->dest->succs)
405 if (e->flags & EDGE_TRUE_VALUE)
407 true_region->region.entry = e;
408 true_region->region.exit = single_succ_edge (e->dest);
409 if_region->true_region = true_region;
411 else if (e->flags & EDGE_FALSE_VALUE)
413 false_region->region.entry = e;
414 false_region->region.exit = single_succ_edge (e->dest);
415 if_region->false_region = false_region;
419 return if_region;
422 /* Moves REGION in a condition expression:
423 | if (1)
425 | else
426 | REGION;
429 ifsese
430 move_sese_in_condition (sese_info_p region)
432 basic_block pred_block = split_edge (region->region.entry);
433 ifsese if_region;
435 region->region.entry = single_succ_edge (pred_block);
436 if_region = create_if_region_on_edge (single_pred_edge (pred_block),
437 integer_one_node);
438 if_region_set_false_region (if_region, region);
440 return if_region;
443 /* Replaces the condition of the IF_REGION with CONDITION:
444 | if (CONDITION)
445 | true_region;
446 | else
447 | false_region;
450 void
451 set_ifsese_condition (ifsese if_region, tree condition)
453 sese_info_p region = if_region->region;
454 edge entry = region->region.entry;
455 basic_block bb = entry->dest;
456 gimple *last = last_stmt (bb);
457 gimple_stmt_iterator gsi = gsi_last_bb (bb);
458 gcond *cond_stmt;
460 gcc_assert (gimple_code (last) == GIMPLE_COND);
462 gsi_remove (&gsi, true);
463 gsi = gsi_last_bb (bb);
464 condition = force_gimple_operand_gsi (&gsi, condition, true, NULL,
465 false, GSI_NEW_STMT);
466 cond_stmt = gimple_build_cond_from_tree (condition, NULL_TREE, NULL_TREE);
467 gsi = gsi_last_bb (bb);
468 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
471 /* Return true when T is defined outside REGION or when no definitions are
472 variant in REGION. When HAS_VDEFS is a valid pointer, sets HAS_VDEFS to true
473 when T depends on memory that may change in REGION. */
475 bool
476 invariant_in_sese_p_rec (tree t, const sese_l &region, bool *has_vdefs)
478 if (!defined_in_sese_p (t, region))
479 return true;
481 gimple *stmt = SSA_NAME_DEF_STMT (t);
483 if (gimple_code (stmt) == GIMPLE_PHI
484 || gimple_code (stmt) == GIMPLE_CALL)
485 return false;
487 /* VDEF is variant when it is in the region. */
488 if (gimple_vdef (stmt))
490 if (has_vdefs)
491 *has_vdefs = true;
492 return false;
495 /* A VUSE may or may not be variant following the VDEFs. */
496 if (tree vuse = gimple_vuse (stmt))
497 return invariant_in_sese_p_rec (vuse, region, has_vdefs);
499 ssa_op_iter iter;
500 use_operand_p use_p;
501 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
503 tree use = USE_FROM_PTR (use_p);
505 if (!defined_in_sese_p (use, region))
506 continue;
508 if (!invariant_in_sese_p_rec (use, region, has_vdefs))
509 return false;
512 return true;
515 /* Return true when DEF can be analyzed in REGION by the scalar
516 evolution analyzer. */
518 bool
519 scev_analyzable_p (tree def, sese_l &region)
521 loop_p loop;
522 tree scev;
523 tree type = TREE_TYPE (def);
525 /* When Graphite generates code for a scev, the code generator
526 expresses the scev in function of a single induction variable.
527 This is unsafe for floating point computations, as it may replace
528 a floating point sum reduction with a multiplication. The
529 following test returns false for non integer types to avoid such
530 problems. */
531 if (!INTEGRAL_TYPE_P (type)
532 && !POINTER_TYPE_P (type))
533 return false;
535 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (def));
536 scev = scalar_evolution_in_region (region, loop, def);
538 return !chrec_contains_undetermined (scev)
539 && (TREE_CODE (scev) != SSA_NAME
540 || !defined_in_sese_p (scev, region))
541 && (tree_does_not_contain_chrecs (scev)
542 || evolution_function_is_affine_p (scev));
545 /* Returns the scalar evolution of T in REGION. Every variable that
546 is not defined in the REGION is considered a parameter. */
548 tree
549 scalar_evolution_in_region (const sese_l &region, loop_p loop, tree t)
551 gimple *def;
552 struct loop *def_loop;
553 basic_block before = region.entry->src;
555 /* SCOP parameters. */
556 if (TREE_CODE (t) == SSA_NAME
557 && !defined_in_sese_p (t, region))
558 return t;
560 if (TREE_CODE (t) != SSA_NAME
561 || loop_in_sese_p (loop, region))
562 /* FIXME: we would need instantiate SCEV to work on a region, and be more
563 flexible wrt. memory loads that may be invariant in the region. */
564 return instantiate_scev (before, loop,
565 analyze_scalar_evolution (loop, t));
567 def = SSA_NAME_DEF_STMT (t);
568 def_loop = loop_containing_stmt (def);
570 if (loop_in_sese_p (def_loop, region))
572 t = analyze_scalar_evolution (def_loop, t);
573 def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
574 t = compute_overall_effect_of_inner_loop (def_loop, t);
575 return t;
578 bool has_vdefs = false;
579 if (invariant_in_sese_p_rec (t, region, &has_vdefs))
580 return t;
582 /* T variates in REGION. */
583 if (has_vdefs)
584 return chrec_dont_know;
586 return instantiate_scev (before, loop, t);
589 /* Pretty print edge E to FILE. */
591 void
592 print_edge (FILE *file, const_edge e)
594 fprintf (file, "edge (bb_%d, bb_%d)", e->src->index, e->dest->index);
597 /* Pretty print sese S to FILE. */
599 void
600 print_sese (FILE *file, const sese_l &s)
602 fprintf (file, "(entry_"); print_edge (file, s.entry);
603 fprintf (file, ", exit_"); print_edge (file, s.exit);
604 fprintf (file, ")\n");
607 /* Pretty print edge E to STDERR. */
609 DEBUG_FUNCTION void
610 debug_edge (const_edge e)
612 print_edge (stderr, e);
615 /* Pretty print sese S to STDERR. */
617 DEBUG_FUNCTION void
618 debug_sese (const sese_l &s)
620 print_sese (stderr, s);