]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/sese.c
remove parameter_rename_map
[thirdparty/gcc.git] / gcc / sese.c
1 /* Single entry single exit control flow regions.
2 Copyright (C) 2008-2015 Free Software Foundation, Inc.
3 Contributed by Jan Sjodin <jan.sjodin@amd.com> and
4 Sebastian Pop <sebastian.pop@amd.com>.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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 "gimple-fold.h"
34 #include "tree-eh.h"
35 #include "gimplify.h"
36 #include "gimple-iterator.h"
37 #include "gimplify-me.h"
38 #include "tree-cfg.h"
39 #include "tree-ssa-loop.h"
40 #include "tree-into-ssa.h"
41 #include "cfgloop.h"
42 #include "tree-data-ref.h"
43 #include "tree-scalar-evolution.h"
44 #include "value-prof.h"
45 #include "sese.h"
46 #include "tree-ssa-propagate.h"
47 #include "tree-hash-traits.h"
48
49 /* Helper function for debug_rename_map. */
50
51 bool
52 debug_rename_map_1 (tree_node *const &old_name, tree_node *const &expr,
53 void *)
54 {
55 fprintf (stderr, "(");
56 print_generic_expr (stderr, old_name, 0);
57 fprintf (stderr, ", ");
58 print_generic_expr (stderr, expr, 0);
59 fprintf (stderr, ")\n");
60 return true;
61 }
62 \f
63 typedef hash_map<tree_ssa_name_hash, tree> rename_map_type;
64 \f
65
66 /* Print to stderr all the elements of RENAME_MAP. */
67
68 DEBUG_FUNCTION void
69 debug_rename_map (rename_map_type *rename_map)
70 {
71 rename_map->traverse <void *, debug_rename_map_1> (NULL);
72 }
73 \f
74
75 /* Record LOOP as occurring in REGION. */
76
77 static void
78 sese_record_loop (sese_info_p region, loop_p loop)
79 {
80 if (sese_contains_loop (region, loop))
81 return;
82
83 bitmap_set_bit (SESE_LOOPS (region), loop->num);
84 SESE_LOOP_NEST (region).safe_push (loop);
85 }
86
87 /* Build the loop nests contained in REGION. Returns true when the
88 operation was successful. */
89
90 void
91 build_sese_loop_nests (sese_info_p region)
92 {
93 unsigned i;
94 basic_block bb;
95 struct loop *loop0, *loop1;
96
97 FOR_EACH_BB_FN (bb, cfun)
98 if (bb_in_sese_p (bb, region->region))
99 {
100 struct loop *loop = bb->loop_father;
101
102 /* Only add loops if they are completely contained in the SCoP. */
103 if (loop->header == bb
104 && bb_in_sese_p (loop->latch, region->region))
105 sese_record_loop (region, loop);
106 }
107
108 /* Make sure that the loops in the SESE_LOOP_NEST are ordered. It
109 can be the case that an inner loop is inserted before an outer
110 loop. To avoid this, semi-sort once. */
111 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), i, loop0)
112 {
113 if (SESE_LOOP_NEST (region).length () == i + 1)
114 break;
115
116 loop1 = SESE_LOOP_NEST (region)[i + 1];
117 if (loop0->num > loop1->num)
118 {
119 SESE_LOOP_NEST (region)[i] = loop1;
120 SESE_LOOP_NEST (region)[i + 1] = loop0;
121 }
122 }
123 }
124
125 /* For a USE in BB, if BB is outside REGION, mark the USE in the
126 LIVEOUTS set. */
127
128 static void
129 sese_build_liveouts_use (sese_info_p region, bitmap liveouts, basic_block bb,
130 tree use)
131 {
132 gcc_assert (!bb_in_sese_p (bb, region->region));
133 if (TREE_CODE (use) != SSA_NAME)
134 return;
135
136 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
137
138 if (!def_bb || !bb_in_sese_p (def_bb, region->region))
139 return;
140
141 unsigned ver = SSA_NAME_VERSION (use);
142 bitmap_set_bit (liveouts, ver);
143 }
144
145 /* Marks for rewrite all the SSA_NAMES defined in REGION and that are
146 used in BB that is outside of the REGION. */
147
148 static void
149 sese_build_liveouts_bb (sese_info_p region, bitmap liveouts, basic_block bb)
150 {
151 edge e;
152 edge_iterator ei;
153 ssa_op_iter iter;
154 use_operand_p use_p;
155
156 FOR_EACH_EDGE (e, ei, bb->succs)
157 for (gphi_iterator bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi);
158 gsi_next (&bsi))
159 sese_build_liveouts_use (region, liveouts, bb,
160 PHI_ARG_DEF_FROM_EDGE (bsi.phi (), e));
161
162 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
163 gsi_next (&bsi))
164 {
165 gimple *stmt = gsi_stmt (bsi);
166
167 if (is_gimple_debug (stmt))
168 continue;
169
170 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
171 sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
172 }
173 }
174
175 /* For a USE in BB, return true if BB is outside REGION and it's not
176 in the LIVEOUTS set. */
177
178 static bool
179 sese_bad_liveouts_use (sese_info_p region, bitmap liveouts, basic_block bb,
180 tree use)
181 {
182 gcc_assert (!bb_in_sese_p (bb, region->region));
183
184 if (TREE_CODE (use) != SSA_NAME)
185 return false;
186
187 unsigned ver = SSA_NAME_VERSION (use);
188
189 /* If it's in liveouts, the variable will get a new PHI node, and
190 the debug use will be properly adjusted. */
191 if (bitmap_bit_p (liveouts, ver))
192 return false;
193
194 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
195
196 if (!def_bb || !bb_in_sese_p (def_bb, region->region))
197 return false;
198
199 return true;
200 }
201
202 /* Reset debug stmts that reference SSA_NAMES defined in REGION that
203 are not marked as liveouts. */
204
205 static void
206 sese_reset_debug_liveouts_bb (sese_info_p region, bitmap liveouts, basic_block bb)
207 {
208 gimple_stmt_iterator bsi;
209 ssa_op_iter iter;
210 use_operand_p use_p;
211
212 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
213 {
214 gimple *stmt = gsi_stmt (bsi);
215
216 if (!is_gimple_debug (stmt))
217 continue;
218
219 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
220 if (sese_bad_liveouts_use (region, liveouts, bb,
221 USE_FROM_PTR (use_p)))
222 {
223 gimple_debug_bind_reset_value (stmt);
224 update_stmt (stmt);
225 break;
226 }
227 }
228 }
229
230 /* Build the LIVEOUTS of REGION: the set of variables defined inside
231 and used outside the REGION. */
232
233 static void
234 sese_build_liveouts (sese_info_p region, bitmap liveouts)
235 {
236 basic_block bb;
237
238 /* FIXME: We could start iterating form the successor of sese. */
239 FOR_EACH_BB_FN (bb, cfun)
240 if (!bb_in_sese_p (bb, region->region))
241 sese_build_liveouts_bb (region, liveouts, bb);
242
243 /* FIXME: We could start iterating form the successor of sese. */
244 if (MAY_HAVE_DEBUG_STMTS)
245 FOR_EACH_BB_FN (bb, cfun)
246 if (!bb_in_sese_p (bb, region->region))
247 sese_reset_debug_liveouts_bb (region, liveouts, bb);
248 }
249
250 /* Builds a new SESE region from edges ENTRY and EXIT. */
251
252 sese_info_p
253 new_sese_info (edge entry, edge exit)
254 {
255 sese_info_p region = XNEW (struct sese_info_t);
256
257 region->region.entry = entry;
258 region->region.exit = exit;
259 SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
260 SESE_LOOP_NEST (region).create (3);
261 SESE_PARAMS (region).create (3);
262 region->bbs.create (3);
263
264 return region;
265 }
266
267 /* Deletes REGION. */
268
269 void
270 free_sese_info (sese_info_p region)
271 {
272 if (SESE_LOOPS (region))
273 SESE_LOOPS (region) = BITMAP_ALLOC (NULL);
274
275 SESE_PARAMS (region).release ();
276 SESE_LOOP_NEST (region).release ();
277
278 XDELETE (region);
279 }
280
281 /* Add exit phis for USE on EXIT. */
282
283 static void
284 sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
285 {
286 gphi *phi = create_phi_node (NULL_TREE, exit);
287 create_new_def_for (use, phi, gimple_phi_result_ptr (phi));
288 add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
289 add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
290 update_stmt (phi);
291 }
292
293 /* Insert in the block BB phi nodes for variables defined in REGION
294 and used outside the REGION. The code generation moves REGION in
295 the else clause of an "if (1)" and generates code in the then
296 clause that is at this point empty:
297
298 | if (1)
299 | empty;
300 | else
301 | REGION;
302 */
303
304 void
305 sese_insert_phis_for_liveouts (sese_info_p region, basic_block bb,
306 edge false_e, edge true_e)
307 {
308 unsigned i;
309 bitmap_iterator bi;
310 bitmap liveouts = BITMAP_ALLOC (NULL);
311
312 update_ssa (TODO_update_ssa);
313
314 sese_build_liveouts (region, liveouts);
315 EXECUTE_IF_SET_IN_BITMAP (liveouts, 0, i, bi)
316 sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
317 BITMAP_FREE (liveouts);
318
319 update_ssa (TODO_update_ssa);
320 }
321
322 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
323
324 edge
325 get_true_edge_from_guard_bb (basic_block bb)
326 {
327 edge e;
328 edge_iterator ei;
329
330 FOR_EACH_EDGE (e, ei, bb->succs)
331 if (e->flags & EDGE_TRUE_VALUE)
332 return e;
333
334 gcc_unreachable ();
335 return NULL;
336 }
337
338 /* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
339
340 edge
341 get_false_edge_from_guard_bb (basic_block bb)
342 {
343 edge e;
344 edge_iterator ei;
345
346 FOR_EACH_EDGE (e, ei, bb->succs)
347 if (!(e->flags & EDGE_TRUE_VALUE))
348 return e;
349
350 gcc_unreachable ();
351 return NULL;
352 }
353
354 /* Returns the expression associated to OLD_NAME in RENAME_MAP. */
355
356 static tree
357 get_rename (rename_map_type *rename_map, tree old_name)
358 {
359 gcc_assert (TREE_CODE (old_name) == SSA_NAME);
360 tree *expr = rename_map->get (old_name);
361 if (expr)
362 return *expr;
363
364 return NULL_TREE;
365 }
366
367 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR). */
368
369 static void
370 set_rename (rename_map_type *rename_map, tree old_name, tree expr)
371 {
372 if (dump_file)
373 {
374 fprintf (dump_file, "[codegen] setting rename: old_name = ");
375 print_generic_expr (dump_file, old_name, 0);
376 fprintf (dump_file, ", new_name = ");
377 print_generic_expr (dump_file, expr, 0);
378 fprintf (dump_file, "\n");
379 }
380
381 if (old_name == expr)
382 return;
383
384 rename_map->put (old_name, expr);
385 }
386
387 /* Renames the scalar uses of the statement COPY, using the
388 substitution map RENAME_MAP, inserting the gimplification code at
389 GSI_TGT, for the translation REGION, with the original copied
390 statement in LOOP, and using the induction variable renaming map
391 IV_MAP. Returns true when something has been renamed. GLOOG_ERROR
392 is set when the code generation cannot continue. */
393
394 static bool
395 rename_uses (gimple *copy, rename_map_type *rename_map,
396 gimple_stmt_iterator *gsi_tgt,
397 sese_info_p region, loop_p loop, vec<tree> iv_map,
398 bool *gloog_error)
399 {
400 use_operand_p use_p;
401 ssa_op_iter op_iter;
402 bool changed = false;
403
404 if (is_gimple_debug (copy))
405 {
406 if (gimple_debug_bind_p (copy))
407 gimple_debug_bind_reset_value (copy);
408 else if (gimple_debug_source_bind_p (copy))
409 return false;
410 else
411 gcc_unreachable ();
412
413 return false;
414 }
415
416 FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE)
417 {
418 tree old_name = USE_FROM_PTR (use_p);
419 tree new_expr, scev;
420 gimple_seq stmts;
421
422 if (TREE_CODE (old_name) != SSA_NAME
423 || SSA_NAME_IS_DEFAULT_DEF (old_name))
424 continue;
425
426 changed = true;
427 new_expr = get_rename (rename_map, old_name);
428 if (new_expr)
429 {
430 tree type_old_name = TREE_TYPE (old_name);
431 tree type_new_expr = TREE_TYPE (new_expr);
432
433 if (type_old_name != type_new_expr
434 || TREE_CODE (new_expr) != SSA_NAME)
435 {
436 tree var = create_tmp_var (type_old_name, "var");
437
438 if (!useless_type_conversion_p (type_old_name, type_new_expr))
439 new_expr = fold_convert (type_old_name, new_expr);
440
441 new_expr = force_gimple_operand (new_expr, &stmts, true, var);
442 gsi_insert_seq_before (gsi_tgt, stmts, GSI_SAME_STMT);
443 }
444
445 replace_exp (use_p, new_expr);
446 continue;
447 }
448
449 scev = scalar_evolution_in_region (region->region, loop, old_name);
450
451 /* At this point we should know the exact scev for each
452 scalar SSA_NAME used in the scop: all the other scalar
453 SSA_NAMEs should have been translated out of SSA using
454 arrays with one element. */
455 if (chrec_contains_undetermined (scev))
456 {
457 *gloog_error = true;
458 new_expr = build_zero_cst (TREE_TYPE (old_name));
459 }
460 else
461 new_expr = chrec_apply_map (scev, iv_map);
462
463 /* The apply should produce an expression tree containing
464 the uses of the new induction variables. We should be
465 able to use new_expr instead of the old_name in the newly
466 generated loop nest. */
467 if (chrec_contains_undetermined (new_expr)
468 || tree_contains_chrecs (new_expr, NULL))
469 {
470 *gloog_error = true;
471 new_expr = build_zero_cst (TREE_TYPE (old_name));
472 }
473 else
474 /* Replace the old_name with the new_expr. */
475 new_expr = force_gimple_operand (unshare_expr (new_expr), &stmts,
476 true, NULL_TREE);
477
478 gsi_insert_seq_before (gsi_tgt, stmts, GSI_SAME_STMT);
479 replace_exp (use_p, new_expr);
480
481 if (TREE_CODE (new_expr) == INTEGER_CST
482 && is_gimple_assign (copy))
483 {
484 tree rhs = gimple_assign_rhs1 (copy);
485
486 if (TREE_CODE (rhs) == ADDR_EXPR)
487 recompute_tree_invariant_for_addr_expr (rhs);
488 }
489
490 set_rename (rename_map, old_name, new_expr);
491 }
492
493 return changed;
494 }
495
496 /* Duplicates the statements of basic block BB into basic block NEW_BB
497 and compute the new induction variables according to the IV_MAP.
498 GLOOG_ERROR is set when the code generation cannot continue. */
499
500 static void
501 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
502 rename_map_type *rename_map,
503 vec<tree> iv_map, sese_info_p region,
504 bool *gloog_error)
505 {
506 gimple_stmt_iterator gsi, gsi_tgt;
507 loop_p loop = bb->loop_father;
508
509 gsi_tgt = gsi_start_bb (new_bb);
510 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
511 {
512 def_operand_p def_p;
513 ssa_op_iter op_iter;
514 gimple *stmt = gsi_stmt (gsi);
515 gimple *copy;
516 tree lhs;
517
518 /* Do not copy labels or conditions. */
519 if (gimple_code (stmt) == GIMPLE_LABEL
520 || gimple_code (stmt) == GIMPLE_COND)
521 continue;
522
523 /* Do not copy induction variables. */
524 if (is_gimple_assign (stmt)
525 && (lhs = gimple_assign_lhs (stmt))
526 && TREE_CODE (lhs) == SSA_NAME
527 && is_gimple_reg (lhs)
528 && scev_analyzable_p (lhs, region->region))
529 continue;
530
531 /* Create a new copy of STMT and duplicate STMT's virtual
532 operands. */
533 copy = gimple_copy (stmt);
534 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
535
536 maybe_duplicate_eh_stmt (copy, stmt);
537 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
538
539 /* Create new names for all the definitions created by COPY and
540 add replacement mappings for each new name. */
541 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
542 {
543 tree old_name = DEF_FROM_PTR (def_p);
544 tree new_name = create_new_def_for (old_name, copy, def_p);
545 set_rename (rename_map, old_name, new_name);
546 }
547
548 if (rename_uses (copy, rename_map, &gsi_tgt, region, loop, iv_map,
549 gloog_error))
550 {
551 gcc_assert (gsi_stmt (gsi_tgt) == copy);
552 fold_stmt_inplace (&gsi_tgt);
553 }
554
555 update_stmt (copy);
556 }
557 }
558
559 /* Copies BB and includes in the copied BB all the statements that can
560 be reached following the use-def chains from the memory accesses,
561 and returns the next edge following this new block. GLOOG_ERROR is
562 set when the code generation cannot continue. */
563
564 edge
565 copy_bb_and_scalar_dependences (basic_block bb, sese_info_p region,
566 edge next_e, vec<tree> iv_map,
567 bool *gloog_error)
568 {
569 basic_block new_bb = split_edge (next_e);
570 rename_map_type rename_map (10);
571
572 next_e = single_succ_edge (new_bb);
573 graphite_copy_stmts_from_block (bb, new_bb, &rename_map, iv_map, region,
574 gloog_error);
575 remove_phi_nodes (new_bb);
576
577 return next_e;
578 }
579
580 /* Returns the outermost loop in SCOP that contains BB. */
581
582 struct loop *
583 outermost_loop_in_sese_1 (sese_l &region, basic_block bb)
584 {
585 struct loop *nest;
586
587 nest = bb->loop_father;
588 while (loop_outer (nest)
589 && loop_in_sese_p (loop_outer (nest), region))
590 nest = loop_outer (nest);
591
592 return nest;
593 }
594
595 /* Same as outermost_loop_in_sese_1, returns the outermost loop
596 containing BB in REGION, but makes sure that the returned loop
597 belongs to the REGION, and so this returns the first loop in the
598 REGION when the loop containing BB does not belong to REGION. */
599
600 loop_p
601 outermost_loop_in_sese (sese_l &region, basic_block bb)
602 {
603 loop_p nest = outermost_loop_in_sese_1 (region, bb);
604
605 if (loop_in_sese_p (nest, region))
606 return nest;
607
608 /* When the basic block BB does not belong to a loop in the region,
609 return the first loop in the region. */
610 nest = nest->inner;
611 while (nest)
612 if (loop_in_sese_p (nest, region))
613 break;
614 else
615 nest = nest->next;
616
617 gcc_assert (nest);
618 return nest;
619 }
620
621 /* Sets the false region of an IF_REGION to REGION. */
622
623 void
624 if_region_set_false_region (ifsese if_region, sese_info_p region)
625 {
626 basic_block condition = if_region_get_condition_block (if_region);
627 edge false_edge = get_false_edge_from_guard_bb (condition);
628 basic_block dummy = false_edge->dest;
629 edge entry_region = region->region.entry;
630 edge exit_region = region->region.exit;
631 basic_block before_region = entry_region->src;
632 basic_block last_in_region = exit_region->src;
633 hashval_t hash = htab_hash_pointer (exit_region);
634 loop_exit **slot
635 = current_loops->exits->find_slot_with_hash (exit_region, hash, NO_INSERT);
636
637 entry_region->flags = false_edge->flags;
638 false_edge->flags = exit_region->flags;
639
640 redirect_edge_pred (entry_region, condition);
641 redirect_edge_pred (exit_region, before_region);
642 redirect_edge_pred (false_edge, last_in_region);
643 redirect_edge_succ (false_edge, single_succ (dummy));
644 delete_basic_block (dummy);
645
646 exit_region->flags = EDGE_FALLTHRU;
647 recompute_all_dominators ();
648
649 region->region.exit = false_edge;
650
651 free (if_region->false_region);
652 if_region->false_region = region;
653
654 if (slot)
655 {
656 struct loop_exit *loop_exit = ggc_cleared_alloc<struct loop_exit> ();
657
658 memcpy (loop_exit, *((struct loop_exit **) slot), sizeof (struct loop_exit));
659 current_loops->exits->clear_slot (slot);
660
661 hashval_t hash = htab_hash_pointer (false_edge);
662 slot = current_loops->exits->find_slot_with_hash (false_edge, hash,
663 INSERT);
664 loop_exit->e = false_edge;
665 *slot = loop_exit;
666 false_edge->src->loop_father->exits->next = loop_exit;
667 }
668 }
669
670 /* Creates an IFSESE with CONDITION on edge ENTRY. */
671
672 static ifsese
673 create_if_region_on_edge (edge entry, tree condition)
674 {
675 edge e;
676 edge_iterator ei;
677 sese_info_p sese_region = XNEW (struct sese_info_t);
678 sese_info_p true_region = XNEW (struct sese_info_t);
679 sese_info_p false_region = XNEW (struct sese_info_t);
680 ifsese if_region = XNEW (struct ifsese_s);
681 edge exit = create_empty_if_region_on_edge (entry, condition);
682
683 if_region->region = sese_region;
684 if_region->region->region.entry = entry;
685 if_region->region->region.exit = exit;
686
687 FOR_EACH_EDGE (e, ei, entry->dest->succs)
688 {
689 if (e->flags & EDGE_TRUE_VALUE)
690 {
691 true_region->region.entry = e;
692 true_region->region.exit = single_succ_edge (e->dest);
693 if_region->true_region = true_region;
694 }
695 else if (e->flags & EDGE_FALSE_VALUE)
696 {
697 false_region->region.entry = e;
698 false_region->region.exit = single_succ_edge (e->dest);
699 if_region->false_region = false_region;
700 }
701 }
702
703 return if_region;
704 }
705
706 /* Moves REGION in a condition expression:
707 | if (1)
708 | ;
709 | else
710 | REGION;
711 */
712
713 ifsese
714 move_sese_in_condition (sese_info_p region)
715 {
716 basic_block pred_block = split_edge (region->region.entry);
717 ifsese if_region;
718
719 region->region.entry = single_succ_edge (pred_block);
720 if_region = create_if_region_on_edge (single_pred_edge (pred_block),
721 integer_one_node);
722 if_region_set_false_region (if_region, region);
723
724 return if_region;
725 }
726
727 /* Replaces the condition of the IF_REGION with CONDITION:
728 | if (CONDITION)
729 | true_region;
730 | else
731 | false_region;
732 */
733
734 void
735 set_ifsese_condition (ifsese if_region, tree condition)
736 {
737 sese_info_p region = if_region->region;
738 edge entry = region->region.entry;
739 basic_block bb = entry->dest;
740 gimple *last = last_stmt (bb);
741 gimple_stmt_iterator gsi = gsi_last_bb (bb);
742 gcond *cond_stmt;
743
744 gcc_assert (gimple_code (last) == GIMPLE_COND);
745
746 gsi_remove (&gsi, true);
747 gsi = gsi_last_bb (bb);
748 condition = force_gimple_operand_gsi (&gsi, condition, true, NULL,
749 false, GSI_NEW_STMT);
750 cond_stmt = gimple_build_cond_from_tree (condition, NULL_TREE, NULL_TREE);
751 gsi = gsi_last_bb (bb);
752 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
753 }
754
755 /* Return true when T is defined outside REGION or when no definitions are
756 variant in REGION. When HAS_VDEFS is a valid pointer, sets HAS_VDEFS to true
757 when T depends on memory that may change in REGION. */
758
759 bool
760 invariant_in_sese_p_rec (tree t, sese_l &region, bool *has_vdefs)
761 {
762 ssa_op_iter iter;
763 use_operand_p use_p;
764 if (!defined_in_sese_p (t, region))
765 return true;
766
767 gimple *stmt = SSA_NAME_DEF_STMT (t);
768
769 if (gimple_code (stmt) == GIMPLE_PHI
770 || gimple_code (stmt) == GIMPLE_CALL)
771 return false;
772
773 /* VDEF is variant when it is in the region. */
774 if (gimple_vdef (stmt))
775 {
776 if (has_vdefs)
777 *has_vdefs = true;
778 return false;
779 }
780
781 /* A VUSE may or may not be variant following the VDEFs. */
782 if (tree vuse = gimple_vuse (stmt))
783 return invariant_in_sese_p_rec (vuse, region, has_vdefs);
784
785 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
786 {
787 tree use = USE_FROM_PTR (use_p);
788
789 if (!defined_in_sese_p (use, region))
790 continue;
791
792 if (!invariant_in_sese_p_rec (use, region, has_vdefs))
793 return false;
794 }
795
796 return true;
797 }
798
799 /* Returns the scalar evolution of T in REGION. Every variable that
800 is not defined in the REGION is considered a parameter. */
801
802 tree
803 scalar_evolution_in_region (sese_l &region, loop_p loop, tree t)
804 {
805 gimple *def;
806 struct loop *def_loop;
807 basic_block before = region.entry->src;
808
809 /* SCOP parameters. */
810 if (TREE_CODE (t) == SSA_NAME
811 && !defined_in_sese_p (t, region))
812 return t;
813
814 if (TREE_CODE (t) != SSA_NAME
815 || loop_in_sese_p (loop, region))
816 return instantiate_scev (before, loop,
817 analyze_scalar_evolution (loop, t));
818
819 def = SSA_NAME_DEF_STMT (t);
820 def_loop = loop_containing_stmt (def);
821
822 if (loop_in_sese_p (def_loop, region))
823 {
824 t = analyze_scalar_evolution (def_loop, t);
825 def_loop = superloop_at_depth (def_loop, loop_depth (loop) + 1);
826 t = compute_overall_effect_of_inner_loop (def_loop, t);
827 return t;
828 }
829
830 bool has_vdefs = false;
831 if (invariant_in_sese_p_rec (t, region, &has_vdefs))
832 return t;
833
834 /* T variates in REGION. */
835 if (has_vdefs)
836 return chrec_dont_know;
837
838 return instantiate_scev (before, loop, t);
839 }