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