]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/sese.c
Allow automatics in equivalences
[thirdparty/gcc.git] / gcc / sese.c
CommitLineData
c6bb733d 1/* Single entry single exit control flow regions.
fbd26352 2 Copyright (C) 2008-2019 Free Software Foundation, Inc.
c6bb733d 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"
9ef16211 25#include "backend.h"
0d9585ca 26#include "tree.h"
9ef16211 27#include "gimple.h"
7c29e30e 28#include "cfghooks.h"
29#include "tree-pass.h"
9ef16211 30#include "ssa.h"
ce084dfc 31#include "tree-pretty-print.h"
7c29e30e 32#include "fold-const.h"
a8783bee 33#include "gimplify.h"
dcf1a1ec 34#include "gimple-iterator.h"
30162daa 35#include "gimple-pretty-print.h"
e795d6e1 36#include "gimplify-me.h"
073c1fd5 37#include "tree-cfg.h"
073c1fd5 38#include "tree-ssa-loop.h"
39#include "tree-into-ssa.h"
c6bb733d 40#include "cfgloop.h"
c6bb733d 41#include "tree-data-ref.h"
42#include "tree-scalar-evolution.h"
0f4161b1 43#include "tree-ssa-propagate.h"
4c03ed5f 44#include "cfganal.h"
45#include "sese.h"
c6bb733d 46
c6bb733d 47/* For a USE in BB, if BB is outside REGION, mark the USE in the
48 LIVEOUTS set. */
49
50static void
f032380c 51sese_build_liveouts_use (sese_info_p region, bitmap liveouts, basic_block bb,
c6bb733d 52 tree use)
53{
f032380c 54 gcc_assert (!bb_in_sese_p (bb, region->region));
c6bb733d 55 if (TREE_CODE (use) != SSA_NAME)
56 return;
57
f1537fda 58 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
c6bb733d 59
f032380c 60 if (!def_bb || !bb_in_sese_p (def_bb, region->region))
c6bb733d 61 return;
62
f1537fda 63 unsigned ver = SSA_NAME_VERSION (use);
c6bb733d 64 bitmap_set_bit (liveouts, ver);
65}
66
67/* Marks for rewrite all the SSA_NAMES defined in REGION and that are
68 used in BB that is outside of the REGION. */
69
70static void
74936b22 71sese_build_liveouts_bb (sese_info_p region, basic_block bb)
c6bb733d 72{
c6bb733d 73 ssa_op_iter iter;
74 use_operand_p use_p;
75
74936b22 76 for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
77 gsi_next (&bsi))
78 FOR_EACH_PHI_ARG (use_p, bsi.phi (), iter, SSA_OP_USE)
79 sese_build_liveouts_use (region, region->liveout,
80 bb, USE_FROM_PTR (use_p));
c6bb733d 81
1a91d914 82 for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
83 gsi_next (&bsi))
3f6c0a40 84 {
42acab1c 85 gimple *stmt = gsi_stmt (bsi);
3f6c0a40 86
74936b22 87 bitmap liveouts = region->liveout;
3f6c0a40 88 if (is_gimple_debug (stmt))
74936b22 89 liveouts = region->debug_liveout;
3f6c0a40 90
74936b22 91 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
3f6c0a40 92 sese_build_liveouts_use (region, liveouts, bb, USE_FROM_PTR (use_p));
93 }
94}
95
3f6c0a40 96/* Reset debug stmts that reference SSA_NAMES defined in REGION that
97 are not marked as liveouts. */
98
99static void
74936b22 100sese_reset_debug_liveouts (sese_info_p region)
3f6c0a40 101{
74936b22 102 bitmap_iterator bi;
103 unsigned i;
104 EXECUTE_IF_AND_COMPL_IN_BITMAP (region->debug_liveout, region->liveout,
105 0, i, bi)
3f6c0a40 106 {
74936b22 107 tree name = ssa_name (i);
108 auto_vec<gimple *, 4> stmts;
109 gimple *use_stmt;
110 imm_use_iterator use_iter;
111 FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, name)
112 {
113 if (! is_gimple_debug (use_stmt)
114 || bb_in_sese_p (gimple_bb (use_stmt), region->region))
115 continue;
116 stmts.safe_push (use_stmt);
117 }
118 while (!stmts.is_empty ())
119 {
120 gimple *stmt = stmts.pop ();
121 gimple_debug_bind_reset_value (stmt);
122 update_stmt (stmt);
123 }
3f6c0a40 124 }
c6bb733d 125}
126
127/* Build the LIVEOUTS of REGION: the set of variables defined inside
128 and used outside the REGION. */
129
74936b22 130void
131sese_build_liveouts (sese_info_p region)
c6bb733d 132{
133 basic_block bb;
134
74936b22 135 gcc_assert (region->liveout == NULL
136 && region->debug_liveout == NULL);
137
138 region->liveout = BITMAP_ALLOC (NULL);
139 region->debug_liveout = BITMAP_ALLOC (NULL);
140
f1537fda 141 /* FIXME: We could start iterating form the successor of sese. */
fc00614f 142 FOR_EACH_BB_FN (bb, cfun)
f032380c 143 if (!bb_in_sese_p (bb, region->region))
74936b22 144 sese_build_liveouts_bb (region, bb);
c6bb733d 145}
146
147/* Builds a new SESE region from edges ENTRY and EXIT. */
148
f032380c 149sese_info_p
150new_sese_info (edge entry, edge exit)
c6bb733d 151{
2e966e2a 152 sese_info_p region = XNEW (class sese_info_t);
c6bb733d 153
f032380c 154 region->region.entry = entry;
155 region->region.exit = exit;
74936b22 156 region->liveout = NULL;
157 region->debug_liveout = NULL;
30162daa 158 region->params.create (3);
cbd0be31 159 region->rename_map = new hash_map <tree, tree>;
0e526381 160 region->bbs.create (3);
c3a14714 161
c6bb733d 162 return region;
163}
164
165/* Deletes REGION. */
166
167void
f032380c 168free_sese_info (sese_info_p region)
c6bb733d 169{
30162daa 170 region->params.release ();
74936b22 171 BITMAP_FREE (region->liveout);
172 BITMAP_FREE (region->debug_liveout);
30162daa 173
30162daa 174 delete region->rename_map;
30162daa 175 region->rename_map = NULL;
30162daa 176 region->bbs.release ();
c6bb733d 177
c6bb733d 178 XDELETE (region);
179}
180
181/* Add exit phis for USE on EXIT. */
182
183static void
184sese_add_exit_phis_edge (basic_block exit, tree use, edge false_e, edge true_e)
185{
1a91d914 186 gphi *phi = create_phi_node (NULL_TREE, exit);
9c06f260 187 create_new_def_for (use, phi, gimple_phi_result_ptr (phi));
60d535d2 188 add_phi_arg (phi, use, false_e, UNKNOWN_LOCATION);
189 add_phi_arg (phi, use, true_e, UNKNOWN_LOCATION);
fa4dba85 190 update_stmt (phi);
c6bb733d 191}
192
193/* Insert in the block BB phi nodes for variables defined in REGION
194 and used outside the REGION. The code generation moves REGION in
195 the else clause of an "if (1)" and generates code in the then
196 clause that is at this point empty:
197
198 | if (1)
199 | empty;
200 | else
201 | REGION;
202*/
203
204void
f032380c 205sese_insert_phis_for_liveouts (sese_info_p region, basic_block bb,
c6bb733d 206 edge false_e, edge true_e)
207{
c64f38bf 208 if (MAY_HAVE_DEBUG_BIND_STMTS)
74936b22 209 sese_reset_debug_liveouts (region);
210
c6bb733d 211 unsigned i;
212 bitmap_iterator bi;
74936b22 213 EXECUTE_IF_SET_IN_BITMAP (region->liveout, 0, i, bi)
30162daa 214 if (!virtual_operand_p (ssa_name (i)))
215 sese_add_exit_phis_edge (bb, ssa_name (i), false_e, true_e);
c6bb733d 216}
217
30162daa 218/* Returns the outermost loop in SCOP that contains BB. */
c6bb733d 219
2e966e2a 220class loop *
f032380c 221outermost_loop_in_sese_1 (sese_l &region, basic_block bb)
c6bb733d 222{
2e966e2a 223 class loop *nest;
c6bb733d 224
225 nest = bb->loop_father;
226 while (loop_outer (nest)
227 && loop_in_sese_p (loop_outer (nest), region))
228 nest = loop_outer (nest);
229
230 return nest;
231}
232
443b5bd0 233/* Same as outermost_loop_in_sese_1, returns the outermost loop
234 containing BB in REGION, but makes sure that the returned loop
235 belongs to the REGION, and so this returns the first loop in the
236 REGION when the loop containing BB does not belong to REGION. */
237
238loop_p
f032380c 239outermost_loop_in_sese (sese_l &region, basic_block bb)
443b5bd0 240{
241 loop_p nest = outermost_loop_in_sese_1 (region, bb);
242
243 if (loop_in_sese_p (nest, region))
244 return nest;
245
246 /* When the basic block BB does not belong to a loop in the region,
247 return the first loop in the region. */
248 nest = nest->inner;
249 while (nest)
250 if (loop_in_sese_p (nest, region))
251 break;
252 else
253 nest = nest->next;
254
255 gcc_assert (nest);
256 return nest;
257}
258
2bca9286 259/* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag set. */
260
261edge
262get_true_edge_from_guard_bb (basic_block bb)
263{
264 edge e;
265 edge_iterator ei;
266
267 FOR_EACH_EDGE (e, ei, bb->succs)
268 if (e->flags & EDGE_TRUE_VALUE)
269 return e;
270
271 gcc_unreachable ();
272 return NULL;
273}
274
275/* Returns the first successor edge of BB with EDGE_TRUE_VALUE flag cleared. */
276
277edge
278get_false_edge_from_guard_bb (basic_block bb)
279{
280 edge e;
281 edge_iterator ei;
282
283 FOR_EACH_EDGE (e, ei, bb->succs)
284 if (!(e->flags & EDGE_TRUE_VALUE))
285 return e;
286
287 gcc_unreachable ();
288 return NULL;
289}
290
c6bb733d 291/* Moves REGION in a condition expression:
292 | if (1)
293 | ;
294 | else
295 | REGION;
296*/
297
298ifsese
f032380c 299move_sese_in_condition (sese_info_p region)
c6bb733d 300{
4c03ed5f 301 basic_block region_entry_dest = region->region.entry->dest;
f032380c 302 basic_block pred_block = split_edge (region->region.entry);
4c03ed5f 303 basic_block merge_block = split_edge (region->region.exit);
304
305 edge true_edge = make_edge (pred_block, merge_block, EDGE_TRUE_VALUE);
306 edge false_edge = find_edge (pred_block, region_entry_dest);
307 false_edge->flags &= ~EDGE_FALLTHRU;
308 false_edge->flags |= EDGE_FALSE_VALUE;
309 gimple_stmt_iterator gsi = gsi_last_bb (pred_block);
310 gcond *cond = gimple_build_cond (NE_EXPR, integer_one_node, integer_zero_node,
311 NULL_TREE, NULL_TREE);
312 gsi_insert_after (&gsi, cond, GSI_CONTINUE_LINKING);
313 if (dom_info_available_p (CDI_DOMINATORS))
314 set_immediate_dominator (CDI_DOMINATORS, merge_block, pred_block);
315
316 ifsese if_region = XNEW (ifsese_s);
317 if_region->region = XCNEW (sese_info_t);
318 if_region->true_region = XCNEW (sese_info_t);
319 if_region->false_region = XCNEW (sese_info_t);
320 if_region->region->region.entry = single_pred_edge (pred_block);
321 if_region->region->region.exit = single_succ_edge (merge_block);
322 if_region->false_region->region.entry = false_edge;
323 if_region->false_region->region.exit = region->region.exit;
324 if_region->true_region->region.entry = true_edge;
325 if_region->true_region->region.exit
326 = single_succ_edge (split_edge (true_edge));
c6bb733d 327
74936b22 328 region->region = if_region->false_region->region;
329
c6bb733d 330 return if_region;
331}
332
2487de19 333/* Replaces the condition of the IF_REGION with CONDITION:
334 | if (CONDITION)
335 | true_region;
336 | else
337 | false_region;
338*/
339
340void
341set_ifsese_condition (ifsese if_region, tree condition)
342{
f032380c 343 sese_info_p region = if_region->region;
344 edge entry = region->region.entry;
2487de19 345 basic_block bb = entry->dest;
42acab1c 346 gimple *last = last_stmt (bb);
2487de19 347 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1a91d914 348 gcond *cond_stmt;
2487de19 349
350 gcc_assert (gimple_code (last) == GIMPLE_COND);
351
352 gsi_remove (&gsi, true);
353 gsi = gsi_last_bb (bb);
354 condition = force_gimple_operand_gsi (&gsi, condition, true, NULL,
355 false, GSI_NEW_STMT);
356 cond_stmt = gimple_build_cond_from_tree (condition, NULL_TREE, NULL_TREE);
357 gsi = gsi_last_bb (bb);
358 gsi_insert_after (&gsi, cond_stmt, GSI_NEW_STMT);
359}
360
9c0cc377 361/* Return true when T is defined outside REGION or when no definitions are
13f421d5 362 variant in REGION. When HAS_VDEFS is a valid pointer, sets HAS_VDEFS to true
363 when T depends on memory that may change in REGION. */
fa4dba85 364
9c0cc377 365bool
d523d0c4 366invariant_in_sese_p_rec (tree t, const sese_l &region, bool *has_vdefs)
fa4dba85 367{
fa4dba85 368 if (!defined_in_sese_p (t, region))
369 return true;
370
42acab1c 371 gimple *stmt = SSA_NAME_DEF_STMT (t);
fa4dba85 372
373 if (gimple_code (stmt) == GIMPLE_PHI
374 || gimple_code (stmt) == GIMPLE_CALL)
375 return false;
376
9c0cc377 377 /* VDEF is variant when it is in the region. */
ec6135ce 378 if (gimple_vdef (stmt))
13f421d5 379 {
380 if (has_vdefs)
381 *has_vdefs = true;
382 return false;
383 }
9c0cc377 384
385 /* A VUSE may or may not be variant following the VDEFs. */
386 if (tree vuse = gimple_vuse (stmt))
13f421d5 387 return invariant_in_sese_p_rec (vuse, region, has_vdefs);
9c0cc377 388
30162daa 389 ssa_op_iter iter;
390 use_operand_p use_p;
fa4dba85 391 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
392 {
393 tree use = USE_FROM_PTR (use_p);
9c0cc377 394
fa4dba85 395 if (!defined_in_sese_p (use, region))
396 continue;
397
13f421d5 398 if (!invariant_in_sese_p_rec (use, region, has_vdefs))
fa4dba85 399 return false;
400 }
401
402 return true;
403}
404
c604a230 405/* Return true when DEF can be analyzed in REGION by the scalar
406 evolution analyzer. */
407
408bool
409scev_analyzable_p (tree def, sese_l &region)
410{
411 loop_p loop;
412 tree scev;
413 tree type = TREE_TYPE (def);
414
415 /* When Graphite generates code for a scev, the code generator
416 expresses the scev in function of a single induction variable.
417 This is unsafe for floating point computations, as it may replace
418 a floating point sum reduction with a multiplication. The
419 following test returns false for non integer types to avoid such
420 problems. */
421 if (!INTEGRAL_TYPE_P (type)
422 && !POINTER_TYPE_P (type))
423 return false;
424
425 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (def));
426 scev = scalar_evolution_in_region (region, loop, def);
427
51a2c146 428 return (!chrec_contains_undetermined (scev)
429 && (TREE_CODE (scev) != SSA_NAME
430 || !defined_in_sese_p (scev, region))
431 && scev_is_linear_expression (scev)
432 && (! loop
433 || ! loop_in_sese_p (loop, region)
434 || ! chrec_contains_symbols_defined_in_loop (scev, loop->num)));
c604a230 435}
436
c6bb733d 437/* Returns the scalar evolution of T in REGION. Every variable that
438 is not defined in the REGION is considered a parameter. */
439
440tree
d523d0c4 441scalar_evolution_in_region (const sese_l &region, loop_p loop, tree t)
c6bb733d 442{
ae6346fd 443 /* SCOP parameters. */
444 if (TREE_CODE (t) == SSA_NAME
445 && !defined_in_sese_p (t, region))
446 return t;
447
44e2f332 448 if (!loop_in_sese_p (loop, region))
449 loop = NULL;
13f421d5 450
44e2f332 451 return instantiate_scev (region.entry, loop,
452 analyze_scalar_evolution (loop, t));
c6bb733d 453}
c1616982 454
74936b22 455/* Return true if BB is empty, contains only DEBUG_INSNs. */
456
457bool
458sese_trivially_empty_bb_p (basic_block bb)
459{
460 gimple_stmt_iterator gsi;
461
462 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
bce107d7 463 if (!is_gimple_debug (gsi_stmt (gsi))
74936b22 464 && gimple_code (gsi_stmt (gsi)) != GIMPLE_LABEL)
465 return false;
466
467 return true;
468}
469
c1616982 470/* Pretty print edge E to FILE. */
471
472void
473print_edge (FILE *file, const_edge e)
474{
475 fprintf (file, "edge (bb_%d, bb_%d)", e->src->index, e->dest->index);
476}
477
478/* Pretty print sese S to FILE. */
479
480void
481print_sese (FILE *file, const sese_l &s)
482{
483 fprintf (file, "(entry_"); print_edge (file, s.entry);
484 fprintf (file, ", exit_"); print_edge (file, s.exit);
485 fprintf (file, ")\n");
486}
487
488/* Pretty print edge E to STDERR. */
489
490DEBUG_FUNCTION void
491debug_edge (const_edge e)
492{
493 print_edge (stderr, e);
494}
495
496/* Pretty print sese S to STDERR. */
497
498DEBUG_FUNCTION void
499debug_sese (const sese_l &s)
500{
501 print_sese (stderr, s);
502}