Line data Source code
1 : /* Convert a program in SSA form into Normal form.
2 : Copyright (C) 2004-2026 Free Software Foundation, Inc.
3 : Contributed by Andrew Macleod <amacleod@redhat.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "gimple-expr.h"
29 : #include "cfghooks.h"
30 : #include "ssa.h"
31 : #include "tree-ssa.h"
32 : #include "memmodel.h"
33 : #include "emit-rtl.h"
34 : #include "gimple-pretty-print.h"
35 : #include "diagnostic-core.h"
36 : #include "tree-dfa.h"
37 : #include "stor-layout.h"
38 : #include "cfgrtl.h"
39 : #include "cfganal.h"
40 : #include "tree-eh.h"
41 : #include "gimple-iterator.h"
42 : #include "tree-cfg.h"
43 : #include "dumpfile.h"
44 : #include "tree-ssa-live.h"
45 : #include "tree-ssa-ter.h"
46 : #include "tree-ssa-coalesce.h"
47 : #include "tree-outof-ssa.h"
48 : #include "cfgexpand.h"
49 : #include "dojump.h"
50 : #include "internal-fn.h"
51 : #include "gimple-fold.h"
52 :
53 : /* FIXME: A lot of code here deals with expanding to RTL. All that code
54 : should be in cfgexpand.cc. */
55 : #include "explow.h"
56 : #include "expr.h"
57 :
58 : /* Return TRUE if expression STMT is suitable for replacement. */
59 :
60 : bool
61 45058646 : ssa_is_replaceable_p (gimple *stmt)
62 : {
63 45058646 : use_operand_p use_p;
64 45058646 : tree def;
65 45058646 : gimple *use_stmt;
66 :
67 : /* Only consider modify stmts and direct internal fn calls that are
68 : not also tail-calls. */
69 45058646 : gcall *call;
70 45058646 : if (!is_gimple_assign (stmt)
71 50652879 : && (!(call = dyn_cast <gcall *> (stmt))
72 5594233 : || gimple_call_tail_p (call)
73 5415446 : || !gimple_call_internal_p (call)
74 211430 : || !direct_internal_fn_p (gimple_call_internal_fn (call))))
75 : return false;
76 :
77 : /* If the statement may throw an exception, it cannot be replaced. */
78 33557313 : if (stmt_could_throw_p (cfun, stmt))
79 : return false;
80 :
81 : /* Punt if there is more than 1 def. */
82 32525423 : def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
83 32525423 : if (!def)
84 : return false;
85 :
86 : /* Only consider definitions which have a single use. */
87 25406040 : if (!single_imm_use (def, &use_p, &use_stmt))
88 : return false;
89 :
90 : /* Used in this block, but at the TOP of the block, not the end. */
91 21545008 : if (gimple_code (use_stmt) == GIMPLE_PHI)
92 : return false;
93 :
94 : /* There must be no VDEFs. */
95 40264688 : if (gimple_vdef (stmt))
96 : return false;
97 :
98 : /* Float expressions must go through memory if float-store is on. */
99 20132344 : if (flag_float_store
100 20132344 : && FLOAT_TYPE_P (TREE_TYPE (def)))
101 : return false;
102 :
103 : /* An assignment with a register variable on the RHS is not
104 : replaceable. */
105 20131877 : auto assign = dyn_cast<gassign *> (stmt);
106 20055188 : if (assign
107 20055188 : && gimple_assign_rhs_code (assign) == VAR_DECL
108 1015112 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (assign)))
109 : return false;
110 :
111 : /* Leave any stmt with volatile operands alone as well. */
112 40262064 : if (gimple_has_volatile_ops (stmt))
113 262320 : return false;
114 :
115 : return true;
116 : }
117 :
118 :
119 : /* Used to hold all the components required to do SSA PHI elimination.
120 : The node and pred/succ list is a simple linear list of nodes and
121 : edges represented as pairs of nodes.
122 :
123 : The predecessor and successor list: Nodes are entered in pairs, where
124 : [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
125 : predecessors, all the odd elements are successors.
126 :
127 : Rationale:
128 : When implemented as bitmaps, very large programs SSA->Normal times were
129 : being dominated by clearing the interference graph.
130 :
131 : Typically this list of edges is extremely small since it only includes
132 : PHI results and uses from a single edge which have not coalesced with
133 : each other. This means that no virtual PHI nodes are included, and
134 : empirical evidence suggests that the number of edges rarely exceed
135 : 3, and in a bootstrap of GCC, the maximum size encountered was 7.
136 : This also limits the number of possible nodes that are involved to
137 : rarely more than 6, and in the bootstrap of gcc, the maximum number
138 : of nodes encountered was 12. */
139 :
140 : class elim_graph
141 : {
142 : public:
143 : elim_graph (var_map map);
144 :
145 : /* Size of the elimination vectors. */
146 : int size;
147 :
148 : /* List of nodes in the elimination graph. */
149 : auto_vec<int> nodes;
150 :
151 : /* The predecessor and successor edge list. */
152 : auto_vec<int> edge_list;
153 :
154 : /* Source locus on each edge */
155 : auto_vec<location_t> edge_locus;
156 :
157 : /* Visited vector. */
158 : auto_sbitmap visited;
159 :
160 : /* Stack for visited nodes. */
161 : auto_vec<int> stack;
162 :
163 : /* The variable partition map. */
164 : var_map map;
165 :
166 : /* Edge being eliminated by this graph. */
167 : edge e;
168 :
169 : /* List of constant copies to emit. These are pushed on in pairs. */
170 : auto_vec<int> const_dests;
171 : auto_vec<tree> const_copies;
172 :
173 : /* Source locations for any constant copies. */
174 : auto_vec<location_t> copy_locus;
175 : };
176 :
177 :
178 : /* For an edge E find out a good source location to associate with
179 : instructions inserted on edge E. If E has an implicit goto set,
180 : use its location. Otherwise search instructions in predecessors
181 : of E for a location, and use that one. That makes sense because
182 : we insert on edges for PHI nodes, and effects of PHIs happen on
183 : the end of the predecessor conceptually. An exception is made
184 : for EH edges because we don't want to drag the source location
185 : of unrelated statements at the beginning of handlers; they would
186 : be further reused for various EH constructs, which would damage
187 : the coverage information. */
188 :
189 : static void
190 1820784 : set_location_for_edge (edge e)
191 : {
192 1820784 : if (e->goto_locus)
193 133255 : set_curr_insn_location (e->goto_locus);
194 1687529 : else if (e->flags & EDGE_EH)
195 : {
196 8004 : basic_block bb = e->dest;
197 8166 : gimple_stmt_iterator gsi;
198 :
199 8166 : do
200 : {
201 154688 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
202 : {
203 146351 : gimple *stmt = gsi_stmt (gsi);
204 146351 : if (is_gimple_debug (stmt))
205 114672 : continue;
206 31679 : if (gimple_has_location (stmt) || gimple_block (stmt))
207 : {
208 7995 : set_curr_insn_location (gimple_location (stmt));
209 7995 : return;
210 : }
211 : }
212 : /* Nothing found in this basic block. Make a half-assed attempt
213 : to continue with another block. */
214 171 : if (single_succ_p (bb))
215 162 : bb = single_succ (bb);
216 : else
217 9 : bb = e->dest;
218 : }
219 171 : while (bb != e->dest);
220 : }
221 : else
222 : {
223 1679525 : basic_block bb = e->src;
224 2066327 : gimple_stmt_iterator gsi;
225 :
226 2066327 : do
227 : {
228 8002203 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
229 : {
230 4917528 : gimple *stmt = gsi_stmt (gsi);
231 4917528 : if (is_gimple_debug (stmt))
232 1711995 : continue;
233 3205533 : if (gimple_has_location (stmt) || gimple_block (stmt))
234 : {
235 1421214 : set_curr_insn_location (gimple_location (stmt));
236 1421214 : return;
237 : }
238 : }
239 : /* Nothing found in this basic block. Make a half-assed attempt
240 : to continue with another block. */
241 645113 : if (single_pred_p (bb))
242 386802 : bb = single_pred (bb);
243 : else
244 258311 : bb = e->src;
245 : }
246 645113 : while (bb != e->src);
247 : }
248 : }
249 :
250 : /* Emit insns to copy SRC into DEST converting SRC if necessary. As
251 : SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
252 : which we deduce the size to copy in that case. */
253 :
254 : static inline rtx_insn *
255 517659 : emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
256 : {
257 517659 : start_sequence ();
258 :
259 517659 : if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
260 0 : src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
261 517659 : if (GET_MODE (src) == BLKmode)
262 : {
263 18 : gcc_assert (GET_MODE (dest) == BLKmode);
264 18 : emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
265 : }
266 : else
267 517641 : emit_move_insn (dest, src);
268 517659 : do_pending_stack_adjust ();
269 :
270 517659 : return end_sequence ();
271 : }
272 :
273 : /* Insert a copy instruction from partition SRC to DEST onto edge E. */
274 :
275 : static void
276 509067 : insert_partition_copy_on_edge (edge e, int dest, int src, location_t locus)
277 : {
278 509067 : tree var;
279 509067 : if (dump_file && (dump_flags & TDF_DETAILS))
280 : {
281 0 : fprintf (dump_file,
282 : "Inserting a partition copy on edge BB%d->BB%d : "
283 : "PART.%d = PART.%d",
284 0 : e->src->index,
285 0 : e->dest->index, dest, src);
286 0 : fprintf (dump_file, "\n");
287 : }
288 :
289 509067 : gcc_assert (SA.partition_to_pseudo[dest]);
290 509067 : gcc_assert (SA.partition_to_pseudo[src]);
291 :
292 509067 : set_location_for_edge (e);
293 : /* If a locus is provided, override the default. */
294 509067 : if (locus)
295 216797 : set_curr_insn_location (locus);
296 :
297 509067 : var = partition_to_var (SA.map, src);
298 509067 : rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
299 509067 : copy_rtx (SA.partition_to_pseudo[src]),
300 509067 : TYPE_UNSIGNED (TREE_TYPE (var)),
301 : var);
302 :
303 509067 : insert_insn_on_edge (seq, e);
304 509067 : }
305 :
306 : /* Insert a copy instruction from expression SRC to partition DEST
307 : onto edge E. */
308 :
309 : static void
310 1303125 : insert_value_copy_on_edge (edge e, int dest, tree src, location_t locus)
311 : {
312 1303125 : rtx dest_rtx, x;
313 1303125 : machine_mode dest_mode, src_mode;
314 1303125 : int unsignedp;
315 :
316 1303125 : if (dump_file && (dump_flags & TDF_DETAILS))
317 : {
318 8 : fprintf (dump_file,
319 : "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
320 8 : e->src->index,
321 8 : e->dest->index, dest);
322 8 : print_generic_expr (dump_file, src, TDF_SLIM);
323 8 : fprintf (dump_file, "\n");
324 : }
325 :
326 1303125 : dest_rtx = copy_rtx (SA.partition_to_pseudo[dest]);
327 1303125 : gcc_assert (dest_rtx);
328 :
329 1303125 : set_location_for_edge (e);
330 : /* If a locus is provided, override the default. */
331 1303125 : if (locus)
332 707607 : set_curr_insn_location (locus);
333 :
334 1303125 : start_sequence ();
335 :
336 1303125 : tree name = partition_to_var (SA.map, dest);
337 1303125 : src_mode = TYPE_MODE (TREE_TYPE (src));
338 1303125 : dest_mode = GET_MODE (dest_rtx);
339 1303125 : gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (name)));
340 1303125 : gcc_assert (!REG_P (dest_rtx)
341 : || dest_mode == promote_ssa_mode (name, &unsignedp));
342 :
343 1303125 : if (src_mode != dest_mode)
344 : {
345 0 : x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
346 0 : x = convert_modes (dest_mode, src_mode, x, unsignedp);
347 : }
348 1303125 : else if (src_mode == BLKmode)
349 : {
350 27 : x = dest_rtx;
351 27 : store_expr (src, x, 0, false, false);
352 : }
353 : else
354 1303098 : x = expand_expr (src, dest_rtx, dest_mode, EXPAND_NORMAL);
355 :
356 1303125 : if (x != dest_rtx)
357 1250963 : emit_move_insn (dest_rtx, x);
358 1303125 : do_pending_stack_adjust ();
359 :
360 1303125 : rtx_insn *seq = end_sequence ();
361 :
362 1303125 : insert_insn_on_edge (seq, e);
363 1303125 : }
364 :
365 : /* Insert a copy instruction from RTL expression SRC to partition DEST
366 : onto edge E. */
367 :
368 : static void
369 4296 : insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
370 : location_t locus)
371 : {
372 4296 : if (dump_file && (dump_flags & TDF_DETAILS))
373 : {
374 0 : fprintf (dump_file,
375 : "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
376 0 : e->src->index,
377 0 : e->dest->index, dest);
378 0 : print_simple_rtl (dump_file, src);
379 0 : fprintf (dump_file, "\n");
380 : }
381 :
382 4296 : gcc_assert (SA.partition_to_pseudo[dest]);
383 :
384 4296 : set_location_for_edge (e);
385 : /* If a locus is provided, override the default. */
386 4296 : if (locus)
387 1910 : set_curr_insn_location (locus);
388 :
389 : /* We give the destination as sizeexp in case src/dest are BLKmode
390 : mems. Usually we give the source. As we result from SSA names
391 : the left and right size should be the same (and no WITH_SIZE_EXPR
392 : involved), so it doesn't matter. */
393 4296 : rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
394 : src, unsignedsrcp,
395 : partition_to_var (SA.map, dest));
396 :
397 4296 : insert_insn_on_edge (seq, e);
398 4296 : }
399 :
400 : /* Insert a copy instruction from partition SRC to RTL lvalue DEST
401 : onto edge E. */
402 :
403 : static void
404 4296 : insert_part_to_rtx_on_edge (edge e, rtx dest, int src, location_t locus)
405 : {
406 4296 : tree var;
407 4296 : if (dump_file && (dump_flags & TDF_DETAILS))
408 : {
409 0 : fprintf (dump_file,
410 : "Inserting a temp copy on edge BB%d->BB%d : ",
411 0 : e->src->index,
412 0 : e->dest->index);
413 0 : print_simple_rtl (dump_file, dest);
414 0 : fprintf (dump_file, "= PART.%d\n", src);
415 : }
416 :
417 4296 : gcc_assert (SA.partition_to_pseudo[src]);
418 :
419 4296 : set_location_for_edge (e);
420 : /* If a locus is provided, override the default. */
421 4296 : if (locus)
422 0 : set_curr_insn_location (locus);
423 :
424 4296 : var = partition_to_var (SA.map, src);
425 4296 : rtx_insn *seq = emit_partition_copy (dest,
426 4296 : copy_rtx (SA.partition_to_pseudo[src]),
427 4296 : TYPE_UNSIGNED (TREE_TYPE (var)),
428 : var);
429 :
430 4296 : insert_insn_on_edge (seq, e);
431 4296 : }
432 :
433 :
434 : /* Create an elimination graph for map. */
435 :
436 1531890 : elim_graph::elim_graph (var_map map) :
437 1531890 : nodes (30), edge_list (20), edge_locus (10), visited (map->num_partitions),
438 1531890 : stack (30), map (map), const_dests (20), const_copies (20), copy_locus (10)
439 : {
440 1531890 : }
441 :
442 :
443 : /* Empty elimination graph G. */
444 :
445 : static inline void
446 3791844 : clear_elim_graph (elim_graph *g)
447 : {
448 3791844 : g->nodes.truncate (0);
449 3791844 : g->edge_list.truncate (0);
450 3791844 : g->edge_locus.truncate (0);
451 3791844 : }
452 :
453 :
454 : /* Return the number of nodes in graph G. */
455 :
456 : static inline int
457 3791844 : elim_graph_size (elim_graph *g)
458 : {
459 11375532 : return g->nodes.length ();
460 : }
461 :
462 :
463 : /* Add NODE to graph G, if it doesn't exist already. */
464 :
465 : static inline void
466 1026726 : elim_graph_add_node (elim_graph *g, int node)
467 : {
468 1026726 : int x;
469 1026726 : int t;
470 :
471 2370456 : FOR_EACH_VEC_ELT (g->nodes, x, t)
472 1383067 : if (t == node)
473 1026726 : return;
474 987389 : g->nodes.safe_push (node);
475 : }
476 :
477 :
478 : /* Add the edge PRED->SUCC to graph G. */
479 :
480 : static inline void
481 513363 : elim_graph_add_edge (elim_graph *g, int pred, int succ, location_t locus)
482 : {
483 513363 : g->edge_list.safe_push (pred);
484 513363 : g->edge_list.safe_push (succ);
485 513363 : g->edge_locus.safe_push (locus);
486 513363 : }
487 :
488 :
489 : /* Remove an edge from graph G for which NODE is the predecessor, and
490 : return the successor node. -1 is returned if there is no such edge. */
491 :
492 : static inline int
493 978287 : elim_graph_remove_succ_edge (elim_graph *g, int node, location_t *locus)
494 : {
495 978287 : int y;
496 978287 : unsigned x;
497 2084866 : for (x = 0; x < g->edge_list.length (); x += 2)
498 1610840 : if (g->edge_list[x] == node)
499 : {
500 504261 : g->edge_list[x] = -1;
501 504261 : y = g->edge_list[x + 1];
502 504261 : g->edge_list[x + 1] = -1;
503 504261 : *locus = g->edge_locus[x / 2];
504 504261 : g->edge_locus[x / 2] = UNKNOWN_LOCATION;
505 504261 : return y;
506 : }
507 474026 : *locus = UNKNOWN_LOCATION;
508 474026 : return -1;
509 : }
510 :
511 :
512 : /* Find all the nodes in GRAPH which are successors to NODE in the
513 : edge list. VAR will hold the partition number found. CODE is the
514 : code fragment executed for every node found. */
515 :
516 : #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
517 : do { \
518 : unsigned x_; \
519 : int y_; \
520 : for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
521 : { \
522 : y_ = (GRAPH)->edge_list[x_]; \
523 : if (y_ != (NODE)) \
524 : continue; \
525 : (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
526 : (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
527 : CODE; \
528 : } \
529 : } while (0)
530 :
531 :
532 : /* Find all the nodes which are predecessors of NODE in the edge list for
533 : GRAPH. VAR will hold the partition number found. CODE is the
534 : code fragment executed for every node found. */
535 :
536 : #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
537 : do { \
538 : unsigned x_; \
539 : int y_; \
540 : for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
541 : { \
542 : y_ = (GRAPH)->edge_list[x_ + 1]; \
543 : if (y_ != (NODE)) \
544 : continue; \
545 : (void) ((VAR) = (GRAPH)->edge_list[x_]); \
546 : (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
547 : CODE; \
548 : } \
549 : } while (0)
550 :
551 :
552 : /* Add T to elimination graph G. */
553 :
554 : static inline void
555 1026726 : eliminate_name (elim_graph *g, int T)
556 : {
557 1026726 : elim_graph_add_node (g, T);
558 : }
559 :
560 : /* Return true if this phi argument T should have a copy queued when using
561 : var_map MAP. PHI nodes should contain only ssa_names and invariants. A
562 : test for ssa_name is definitely simpler, but don't let invalid contents
563 : slip through in the meantime. */
564 :
565 : static inline bool
566 7158611 : queue_phi_copy_p (var_map map, tree t)
567 : {
568 7158611 : if (TREE_CODE (t) == SSA_NAME)
569 : {
570 5855486 : if (var_to_partition (map, t) == NO_PARTITION)
571 : return true;
572 5855486 : return false;
573 : }
574 1303125 : gcc_checking_assert (is_gimple_min_invariant (t));
575 : return true;
576 : }
577 :
578 : /* Build elimination graph G for basic block BB on incoming PHI edge
579 : G->e. */
580 :
581 : static void
582 3791844 : eliminate_build (elim_graph *g)
583 : {
584 3791844 : tree Ti;
585 3791844 : int p0, pi;
586 3791844 : gphi_iterator gsi;
587 :
588 3791844 : clear_elim_graph (g);
589 :
590 10950455 : for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
591 : {
592 7158611 : gphi *phi = gsi.phi ();
593 7158611 : location_t locus;
594 :
595 7158611 : p0 = var_to_partition (g->map, gimple_phi_result (phi));
596 : /* Ignore results which are not in partitions. */
597 7158611 : if (p0 == NO_PARTITION)
598 0 : continue;
599 :
600 7158611 : Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
601 : /* See set_location_for_edge for the rationale. */
602 7158611 : if (g->e->flags & EDGE_EH)
603 : locus = UNKNOWN_LOCATION;
604 : else
605 7116788 : locus = gimple_phi_arg_location_from_edge (phi, g->e);
606 :
607 : /* If this argument is a constant, or a SSA_NAME which is being
608 : left in SSA form, just queue a copy to be emitted on this
609 : edge. */
610 7158611 : if (queue_phi_copy_p (g->map, Ti))
611 : {
612 : /* Save constant copies until all other copies have been emitted
613 : on this edge. */
614 1303125 : g->const_dests.safe_push (p0);
615 1303125 : g->const_copies.safe_push (Ti);
616 1303125 : g->copy_locus.safe_push (locus);
617 : }
618 : else
619 : {
620 5855486 : pi = var_to_partition (g->map, Ti);
621 5855486 : if (p0 != pi)
622 : {
623 513363 : eliminate_name (g, p0);
624 513363 : eliminate_name (g, pi);
625 513363 : elim_graph_add_edge (g, p0, pi, locus);
626 : }
627 : }
628 : }
629 3791844 : }
630 :
631 :
632 : /* Push successors of T onto the elimination stack for G. */
633 :
634 : static void
635 987389 : elim_forward (elim_graph *g, int T)
636 : {
637 987389 : int S;
638 987389 : location_t locus;
639 :
640 987389 : bitmap_set_bit (g->visited, T);
641 3866795 : FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
642 : {
643 : if (!bitmap_bit_p (g->visited, S))
644 : elim_forward (g, S);
645 : });
646 987389 : g->stack.safe_push (T);
647 987389 : }
648 :
649 :
650 : /* Return 1 if there unvisited predecessors of T in graph G. */
651 :
652 : static int
653 982583 : elim_unvisited_predecessor (elim_graph *g, int T)
654 : {
655 982583 : int P;
656 982583 : location_t locus;
657 :
658 2842592 : FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
659 : {
660 : if (!bitmap_bit_p (g->visited, P))
661 : return 1;
662 : });
663 : return 0;
664 : }
665 :
666 : /* Process predecessors first, and insert a copy. */
667 :
668 : static void
669 9102 : elim_backward (elim_graph *g, int T)
670 : {
671 9102 : int P;
672 9102 : location_t locus;
673 :
674 9102 : bitmap_set_bit (g->visited, T);
675 62882 : FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
676 : {
677 : if (!bitmap_bit_p (g->visited, P))
678 : {
679 : elim_backward (g, P);
680 : insert_partition_copy_on_edge (g->e, P, T, locus);
681 : }
682 : });
683 9102 : }
684 :
685 : /* Allocate a new pseudo register usable for storing values sitting
686 : in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
687 :
688 : static rtx
689 4296 : get_temp_reg (tree name)
690 : {
691 4296 : tree type = TREE_TYPE (name);
692 4296 : int unsignedp;
693 4296 : machine_mode reg_mode = promote_ssa_mode (name, &unsignedp);
694 4296 : if (reg_mode == BLKmode)
695 1 : return assign_temp (type, 0, 0);
696 4295 : rtx x = gen_reg_rtx (reg_mode);
697 4295 : if (POINTER_TYPE_P (type))
698 876 : mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
699 : return x;
700 : }
701 :
702 : /* Insert required copies for T in graph G. Check for a strongly connected
703 : region, and create a temporary to break the cycle if one is found. */
704 :
705 : static void
706 982583 : elim_create (elim_graph *g, int T)
707 : {
708 982583 : int P, S;
709 982583 : location_t locus;
710 :
711 982583 : if (elim_unvisited_predecessor (g, T))
712 : {
713 4296 : tree var = partition_to_var (g->map, T);
714 4296 : rtx U = get_temp_reg (var);
715 4296 : int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
716 :
717 4296 : insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
718 28694 : FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
719 : {
720 : if (!bitmap_bit_p (g->visited, P))
721 : {
722 : elim_backward (g, P);
723 : insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
724 : }
725 : });
726 : }
727 : else
728 : {
729 978287 : S = elim_graph_remove_succ_edge (g, T, &locus);
730 978287 : if (S != -1)
731 : {
732 504261 : bitmap_set_bit (g->visited, T);
733 504261 : insert_partition_copy_on_edge (g->e, T, S, locus);
734 : }
735 : }
736 982583 : }
737 :
738 :
739 : /* Eliminate all the phi nodes on edge E in graph G. */
740 :
741 : static void
742 3796430 : eliminate_phi (edge e, elim_graph *g)
743 : {
744 3796430 : int x;
745 :
746 3796430 : gcc_assert (g->const_copies.length () == 0);
747 3796430 : gcc_assert (g->copy_locus.length () == 0);
748 :
749 : /* Abnormal edges already have everything coalesced. */
750 3796430 : if (e->flags & EDGE_ABNORMAL)
751 : return;
752 :
753 3791844 : g->e = e;
754 :
755 3791844 : eliminate_build (g);
756 :
757 3791844 : if (elim_graph_size (g) != 0)
758 : {
759 392406 : int part;
760 :
761 392406 : bitmap_clear (g->visited);
762 392406 : g->stack.truncate (0);
763 :
764 1772201 : FOR_EACH_VEC_ELT (g->nodes, x, part)
765 : {
766 987389 : if (!bitmap_bit_p (g->visited, part))
767 502740 : elim_forward (g, part);
768 : }
769 :
770 392406 : bitmap_clear (g->visited);
771 5171639 : while (g->stack.length () > 0)
772 : {
773 987389 : x = g->stack.pop ();
774 987389 : if (!bitmap_bit_p (g->visited, x))
775 982583 : elim_create (g, x);
776 : }
777 : }
778 :
779 : /* If there are any pending constant copies, issue them now. */
780 5094969 : while (g->const_copies.length () > 0)
781 : {
782 1303125 : int dest;
783 1303125 : tree src;
784 1303125 : location_t locus;
785 :
786 1303125 : src = g->const_copies.pop ();
787 1303125 : dest = g->const_dests.pop ();
788 1303125 : locus = g->copy_locus.pop ();
789 1303125 : insert_value_copy_on_edge (e, dest, src, locus);
790 : }
791 : }
792 :
793 :
794 : /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
795 : check to see if this allows another PHI node to be removed. */
796 :
797 : void
798 256 : remove_gimple_phi_args (gphi *phi)
799 : {
800 256 : use_operand_p arg_p;
801 256 : ssa_op_iter iter;
802 :
803 256 : if (dump_file && (dump_flags & TDF_DETAILS))
804 : {
805 0 : fprintf (dump_file, "Removing Dead PHI definition: ");
806 0 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
807 : }
808 :
809 771 : FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
810 : {
811 515 : tree arg = USE_FROM_PTR (arg_p);
812 515 : if (TREE_CODE (arg) == SSA_NAME)
813 : {
814 : /* Remove the reference to the existing argument. */
815 353 : SET_USE (arg_p, NULL_TREE);
816 353 : if (has_zero_uses (arg))
817 : {
818 223 : gimple *stmt;
819 223 : gimple_stmt_iterator gsi;
820 :
821 223 : stmt = SSA_NAME_DEF_STMT (arg);
822 :
823 : /* Also remove the def if it is a PHI node. */
824 223 : if (gimple_code (stmt) == GIMPLE_PHI)
825 : {
826 3 : remove_gimple_phi_args (as_a <gphi *> (stmt));
827 3 : gsi = gsi_for_stmt (stmt);
828 3 : remove_phi_node (&gsi, true);
829 : }
830 :
831 : }
832 : }
833 : }
834 256 : }
835 :
836 : /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
837 :
838 : static void
839 1531890 : eliminate_useless_phis (void)
840 : {
841 1531890 : basic_block bb;
842 1531890 : gphi_iterator gsi;
843 1531890 : tree result;
844 :
845 14554838 : FOR_EACH_BB_FN (bb, cfun)
846 : {
847 18715205 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
848 : {
849 5692257 : gphi *phi = gsi.phi ();
850 5692257 : result = gimple_phi_result (phi);
851 11384514 : if (virtual_operand_p (result))
852 2678397 : remove_phi_node (&gsi, true);
853 : else
854 : {
855 : /* Also remove real PHIs with no uses. */
856 3013860 : if (has_zero_uses (result))
857 : {
858 245 : remove_gimple_phi_args (phi);
859 245 : remove_phi_node (&gsi, true);
860 : }
861 : else
862 3013615 : gsi_next (&gsi);
863 : }
864 : }
865 : }
866 1531890 : }
867 :
868 :
869 : /* This function will rewrite the current program using the variable mapping
870 : found in MAP. If the replacement vector VALUES is provided, any
871 : occurrences of partitions with non-null entries in the vector will be
872 : replaced with the expression in the vector instead of its mapped
873 : variable. */
874 :
875 : static void
876 1531890 : rewrite_trees (var_map map)
877 : {
878 1531890 : if (!flag_checking)
879 : return;
880 :
881 1531870 : basic_block bb;
882 : /* Search for PHIs where the destination has no partition, but one
883 : or more arguments has a partition. This should not happen and can
884 : create incorrect code. */
885 14554739 : FOR_EACH_BB_FN (bb, cfun)
886 : {
887 13022869 : gphi_iterator gsi;
888 16036458 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
889 : {
890 3013589 : gphi *phi = gsi.phi ();
891 3013589 : tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
892 3013589 : if (T0 == NULL_TREE)
893 : {
894 : size_t i;
895 0 : for (i = 0; i < gimple_phi_num_args (phi); i++)
896 : {
897 0 : tree arg = PHI_ARG_DEF (phi, i);
898 :
899 0 : if (TREE_CODE (arg) == SSA_NAME
900 0 : && var_to_partition (map, arg) != NO_PARTITION)
901 : {
902 0 : fprintf (stderr, "Argument of PHI is in a partition :(");
903 0 : print_generic_expr (stderr, arg, TDF_SLIM);
904 0 : fprintf (stderr, "), but the result is not :");
905 0 : print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
906 0 : internal_error ("SSA corruption");
907 : }
908 : }
909 : }
910 : }
911 : }
912 : }
913 :
914 : /* Create a default def for VAR. */
915 :
916 : static void
917 4017147 : create_default_def (tree var, void *arg ATTRIBUTE_UNUSED)
918 : {
919 4017147 : if (!is_gimple_reg (var))
920 : return;
921 :
922 3708068 : tree ssa = get_or_create_ssa_default_def (cfun, var);
923 3708068 : gcc_assert (ssa);
924 : }
925 :
926 : /* Call CALLBACK for all PARM_DECLs and RESULT_DECLs for which
927 : assign_parms may ask for a default partition. */
928 :
929 : static void
930 3063780 : for_all_parms (void (*callback)(tree var, void *arg), void *arg)
931 : {
932 9430462 : for (tree var = DECL_ARGUMENTS (current_function_decl); var;
933 6366682 : var = DECL_CHAIN (var))
934 6366682 : callback (var, arg);
935 3063780 : if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
936 1628346 : callback (DECL_RESULT (current_function_decl), arg);
937 3063780 : if (cfun->static_chain_decl)
938 39266 : callback (cfun->static_chain_decl, arg);
939 3063780 : }
940 :
941 : /* We need to pass two arguments to set_parm_default_def_partition,
942 : but for_all_parms only supports one. Use a pair. */
943 :
944 : typedef std::pair<var_map, bitmap> parm_default_def_partition_arg;
945 :
946 : /* Set in ARG's PARTS bitmap the bit corresponding to the partition in
947 : ARG's MAP containing VAR's default def. */
948 :
949 : static void
950 4017147 : set_parm_default_def_partition (tree var, void *arg_)
951 : {
952 4017147 : parm_default_def_partition_arg *arg = (parm_default_def_partition_arg *)arg_;
953 4017147 : var_map map = arg->first;
954 4017147 : bitmap parts = arg->second;
955 :
956 4017147 : if (!is_gimple_reg (var))
957 : return;
958 :
959 3708068 : tree ssa = ssa_default_def (cfun, var);
960 3708068 : gcc_assert (ssa);
961 :
962 3708068 : int version = var_to_partition (map, ssa);
963 3708068 : gcc_assert (version != NO_PARTITION);
964 :
965 3708068 : bool changed = bitmap_set_bit (parts, version);
966 3708068 : gcc_assert (changed);
967 : }
968 :
969 : /* Allocate and return a bitmap that has a bit set for each partition
970 : that contains a default def for a parameter. */
971 :
972 : static bitmap
973 1531890 : get_parm_default_def_partitions (var_map map)
974 : {
975 1531890 : bitmap parm_default_def_parts = BITMAP_ALLOC (NULL);
976 :
977 1531890 : parm_default_def_partition_arg
978 1531890 : arg = std::make_pair (map, parm_default_def_parts);
979 :
980 1531890 : for_all_parms (set_parm_default_def_partition, &arg);
981 :
982 1531890 : return parm_default_def_parts;
983 : }
984 :
985 : /* Allocate and return a bitmap that has a bit set for each partition
986 : that contains an undefined value. */
987 :
988 : static bitmap
989 1531890 : get_undefined_value_partitions (var_map map)
990 : {
991 1531890 : bitmap undefined_value_parts = BITMAP_ALLOC (NULL);
992 :
993 77541724 : for (unsigned int i = 1; i < num_ssa_names; i++)
994 : {
995 74477944 : tree var = ssa_name (i);
996 74477944 : if (var
997 50642132 : && !virtual_operand_p (var)
998 32836711 : && !has_zero_uses (var)
999 105006727 : && ssa_undefined_value_p (var))
1000 : {
1001 69431 : const int p = var_to_partition (map, var);
1002 69431 : if (p != NO_PARTITION)
1003 69431 : bitmap_set_bit (undefined_value_parts, p);
1004 : }
1005 : }
1006 :
1007 1531890 : return undefined_value_parts;
1008 : }
1009 :
1010 : /* Given the out-of-ssa info object SA (with prepared partitions)
1011 : eliminate all phi nodes in all basic blocks. Afterwards there
1012 : are possibly some RTL instructions inserted on edges. */
1013 :
1014 : void
1015 1531890 : expand_phi_nodes (struct ssaexpand *sa)
1016 : {
1017 1531890 : basic_block bb;
1018 1531890 : elim_graph g (sa->map);
1019 :
1020 14554842 : FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
1021 : EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1022 13022952 : if (!gimple_seq_empty_p (phi_nodes (bb)))
1023 : {
1024 1630106 : edge e;
1025 1630106 : edge_iterator ei;
1026 5426536 : FOR_EACH_EDGE (e, ei, bb->preds)
1027 3796430 : eliminate_phi (e, &g);
1028 : /* We can't redirect EH edges in RTL land, so we need to do this
1029 : here. Redirection happens only when splitting is necessary,
1030 : which it is only for critical edges, normally. For EH edges
1031 : it might also be necessary when the successor has more than
1032 : one predecessor. In that case the edge is either required to
1033 : be fallthru (which EH edges aren't), or the predecessor needs
1034 : to end with a jump (which again, isn't the case with EH edges).
1035 : Hence, split all EH edges on which we inserted instructions
1036 : and whose successor has multiple predecessors. */
1037 5429523 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1038 : {
1039 1262873 : if (e->insns.r && (e->flags & EDGE_EH)
1040 3802404 : && !single_pred_p (e->dest))
1041 : {
1042 2987 : rtx_insn *insns = e->insns.r;
1043 2987 : basic_block bb;
1044 2987 : e->insns.r = NULL;
1045 2987 : bb = split_edge (e);
1046 2987 : single_pred_edge (bb)->insns.r = insns;
1047 : }
1048 : else
1049 3796430 : ei_next (&ei);
1050 : }
1051 : }
1052 1531890 : }
1053 :
1054 :
1055 : /* Out-of-SSA can leave several partitions sharing one base VAR_DECL, when
1056 : that variable's SSA versions are simultaneously live and so cannot all be
1057 : coalesced. When such a partition is spilled (it has no register mode,
1058 : e.g. an oversized vector that is BLKmode), set_mem_attributes would give
1059 : every one of those slots that single decl as its MEM_EXPR at offset 0, so
1060 : the distinct slots appear to be the same object and mislead MEM_EXPR-based
1061 : disambiguation, and the load/store pair-fusion pass then fuses across the
1062 : slots and corrupts one. Give every partition but one of such a decl its
1063 : own artificial decl so the slots are distinguished at the source.
1064 : The new decl carries a DECL_DEBUG_EXPR back to the user variable so debug
1065 : info still attributes the storage to it (cf. create_access_replacement in
1066 : tree-sra.cc). A PARM_DECL or RESULT_DECL keeps the partition of its default
1067 : definition, which holds the canonical RTL, and only its other partitions are
1068 : split. */
1069 :
1070 : static void
1071 1531890 : split_overlapping_partition_decls (var_map map)
1072 : {
1073 1531890 : unsigned n = num_var_partitions (map);
1074 1531890 : hash_set<tree> seen;
1075 1531890 : auto_vec<tree> new_decl;
1076 1531890 : new_decl.safe_grow_cleared (n);
1077 1531890 : bool any = false;
1078 1531890 : unsigned ver;
1079 1531890 : tree name;
1080 :
1081 : /* set_rtl attaches the base variable of any name in a partition to that
1082 : partition's location, not just the one of its representative, so collect
1083 : what the names of each partition contribute. A name with no base
1084 : variable contributes nothing, since set_rtl passes a type rather than a
1085 : decl for those and leaves the MEM_EXPR it has in place. */
1086 1531890 : auto_vec<tree> part_var;
1087 1531890 : part_var.safe_grow_cleared (n);
1088 77541724 : FOR_EACH_SSA_NAME (ver, name, cfun)
1089 : {
1090 50642132 : int p = var_to_partition (map, name);
1091 50642132 : if (p == NO_PARTITION)
1092 18068401 : continue;
1093 32573731 : tree var = SSA_NAME_VAR (name);
1094 32573731 : if (!var)
1095 21770144 : continue;
1096 10803587 : part_var[p] = expand_leader_merge (part_var[p], var);
1097 : }
1098 :
1099 28514322 : for (unsigned i = 0; i < n; i++)
1100 : {
1101 26982432 : tree repr = partition_to_var (map, i);
1102 26982432 : if (!repr)
1103 26982180 : continue;
1104 : /* Expansion hands set_rtl the representative before the other names,
1105 : and expand_leader_merge keeps the variable it is given first unless a
1106 : later one is DECL_IGNORED_P, so merging the two gives the variable
1107 : this partition ends up with. A partition holding the default
1108 : definition of a parameter or of the result is instead seeded with that
1109 : decl, and is given it back once its RTL is restored at the end of
1110 : expansion, so the variable it ends up with is one that the rule below
1111 : keeps for it alone. */
1112 26982432 : tree var = SSA_NAME_VAR (repr);
1113 26982432 : if (part_var[i])
1114 7859178 : var = expand_leader_merge (var, part_var[i]);
1115 26982432 : if (!var)
1116 19123254 : continue;
1117 : /* Only partitions that will live in memory can end up with a
1118 : misleading shared MEM_EXPR. Mirror the decision that
1119 : expand_one_ssa_partition will make. */
1120 7859178 : if (use_register_for_decl (repr))
1121 6602406 : continue;
1122 : /* One partition of VAR keeps the user decl, the rest are split.
1123 : A default definition cannot change its variable, so if VAR has a
1124 : partitioned default definition, its partition is the one that
1125 : keeps the user decl. Otherwise the first partition seen does. */
1126 1256772 : tree ddef = ssa_default_def (cfun, var);
1127 1256772 : int keep = ddef ? var_to_partition (map, ddef) : NO_PARTITION;
1128 809231 : if (keep == NO_PARTITION)
1129 : {
1130 448626 : if (!seen.add (var))
1131 448584 : continue;
1132 : }
1133 808146 : else if (keep >= 0 && (unsigned) keep == i)
1134 807936 : continue;
1135 :
1136 252 : tree nvar = create_tmp_var_raw (TREE_TYPE (var));
1137 : /* Avoid a register-only NVAR when the partition already has a MEM,
1138 : since set_rtl cannot assign that MEM to NVAR. */
1139 252 : if (use_register_for_decl (nvar))
1140 180 : DECL_IGNORED_P (nvar) = DECL_IGNORED_P (var);
1141 252 : gcc_checking_assert (!use_register_for_decl (nvar));
1142 252 : DECL_CONTEXT (nvar) = DECL_CONTEXT (var);
1143 252 : DECL_SOURCE_LOCATION (nvar) = DECL_SOURCE_LOCATION (var);
1144 252 : SET_DECL_ALIGN (nvar, DECL_ALIGN (var));
1145 252 : if (!DECL_ARTIFICIAL (var) && DECL_NAME (var))
1146 : {
1147 230 : SET_DECL_DEBUG_EXPR (nvar, var);
1148 230 : DECL_HAS_DEBUG_EXPR_P (nvar) = 1;
1149 : }
1150 252 : copy_warning (nvar, var);
1151 252 : add_local_decl (cfun, nvar);
1152 252 : new_decl[i] = nvar;
1153 252 : any = true;
1154 : }
1155 :
1156 1531890 : if (!any)
1157 1531706 : return;
1158 :
1159 10449 : FOR_EACH_SSA_NAME (ver, name, cfun)
1160 : {
1161 8603 : if (SSA_NAME_IS_DEFAULT_DEF (name))
1162 938 : continue;
1163 7665 : int p = var_to_partition (map, name);
1164 7665 : if (p != NO_PARTITION && new_decl[p])
1165 642 : SET_SSA_NAME_VAR_OR_IDENTIFIER (name, new_decl[p]);
1166 : }
1167 1531890 : }
1168 :
1169 : /* Remove the ssa-names in the current function and translate them into normal
1170 : compiler variables. PERFORM_TER is true if Temporary Expression Replacement
1171 : should also be used. */
1172 :
1173 : static void
1174 1531890 : remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
1175 : {
1176 1531890 : bitmap values = NULL;
1177 1531890 : var_map map;
1178 :
1179 1531890 : for_all_parms (create_default_def, NULL);
1180 3063780 : map = init_var_map (num_ssa_names);
1181 1531890 : coalesce_ssa_name (map);
1182 :
1183 : /* Return to viewing the variable list as just all reference variables after
1184 : coalescing has been performed. */
1185 1531890 : partition_view_normal (map);
1186 :
1187 1531890 : if (dump_file && (dump_flags & TDF_DETAILS))
1188 : {
1189 108 : fprintf (dump_file, "After Coalescing:\n");
1190 108 : dump_var_map (dump_file, map);
1191 : }
1192 :
1193 1531890 : if (perform_ter)
1194 : {
1195 1070524 : values = find_replaceable_exprs (map);
1196 1070524 : if (values && dump_file && (dump_flags & TDF_DETAILS))
1197 12 : dump_replaceable_exprs (dump_file, values);
1198 : }
1199 :
1200 : /* Distinct partitions of one decl must not share a MEM_EXPR once they are
1201 : spilled to separate stack slots. Done after TER so reassigning
1202 : SSA_NAME_VAR does not perturb find_replaceable_exprs. */
1203 1531890 : split_overlapping_partition_decls (map);
1204 :
1205 1531890 : rewrite_trees (map);
1206 :
1207 1531890 : sa->map = map;
1208 1531890 : sa->values = values;
1209 1531890 : sa->partitions_for_parm_default_defs = get_parm_default_def_partitions (map);
1210 1531890 : sa->partitions_for_undefined_values = get_undefined_value_partitions (map);
1211 1531890 : }
1212 :
1213 :
1214 : /* If not already done so for basic block BB, assign increasing uids
1215 : to each of its instructions. */
1216 :
1217 : static void
1218 1011803 : maybe_renumber_stmts_bb (basic_block bb)
1219 : {
1220 1011803 : unsigned i = 0;
1221 1011803 : gimple_stmt_iterator gsi;
1222 :
1223 1011803 : if (!bb->aux)
1224 1011803 : return;
1225 288699 : bb->aux = NULL;
1226 4185604 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1227 : {
1228 3608206 : gimple *stmt = gsi_stmt (gsi);
1229 3608206 : gimple_set_uid (stmt, i);
1230 3608206 : i++;
1231 : }
1232 : }
1233 :
1234 :
1235 : /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1236 : of a PHI node) and ARG (one of its arguments) conflict. Return false
1237 : otherwise, also when we simply aren't sure. */
1238 :
1239 : static bool
1240 1317173 : trivially_conflicts_p (basic_block bb, tree result, tree arg)
1241 : {
1242 1317173 : use_operand_p use;
1243 1317173 : imm_use_iterator imm_iter;
1244 1317173 : gimple *defa = SSA_NAME_DEF_STMT (arg);
1245 :
1246 : /* If ARG isn't defined in the same block it's too complicated for
1247 : our little mind. */
1248 1317173 : if (gimple_bb (defa) != bb)
1249 : return false;
1250 :
1251 1487113 : FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1252 : {
1253 1050925 : gimple *use_stmt = USE_STMT (use);
1254 1050925 : if (is_gimple_debug (use_stmt))
1255 131772 : continue;
1256 : /* Now, if there's a use of RESULT that lies outside this basic block,
1257 : then there surely is a conflict with ARG. */
1258 919153 : if (gimple_bb (use_stmt) != bb)
1259 : return true;
1260 867240 : if (gimple_code (use_stmt) == GIMPLE_PHI)
1261 844 : continue;
1262 : /* The use now is in a real stmt of BB, so if ARG was defined
1263 : in a PHI node (like RESULT) both conflict. */
1264 866396 : if (gimple_code (defa) == GIMPLE_PHI)
1265 : return true;
1266 863095 : maybe_renumber_stmts_bb (bb);
1267 : /* If the use of RESULT occurs after the definition of ARG,
1268 : the two conflict too. */
1269 863095 : if (gimple_uid (defa) < gimple_uid (use_stmt))
1270 : return true;
1271 71683 : }
1272 :
1273 436188 : return false;
1274 : }
1275 :
1276 :
1277 : /* Search every PHI node for arguments associated with backedges which
1278 : we can trivially determine will need a copy (the argument is either
1279 : not an SSA_NAME or the argument has a different underlying variable
1280 : than the PHI result).
1281 :
1282 : Insert a copy from the PHI argument to a new destination at the
1283 : end of the block with the backedge to the top of the loop. Update
1284 : the PHI argument to reference this new destination. */
1285 :
1286 : static void
1287 1531890 : insert_backedge_copies (void)
1288 : {
1289 1531890 : basic_block bb;
1290 1531890 : gphi_iterator gsi;
1291 :
1292 1531890 : mark_dfs_back_edges ();
1293 :
1294 14554838 : FOR_EACH_BB_FN (bb, cfun)
1295 : {
1296 : /* Mark block as possibly needing calculation of UIDs. */
1297 13022948 : bb->aux = &bb->aux;
1298 :
1299 18715206 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1300 : {
1301 5692258 : gphi *phi = gsi.phi ();
1302 5692258 : tree result = gimple_phi_result (phi);
1303 5692258 : size_t i;
1304 :
1305 11384516 : if (virtual_operand_p (result))
1306 2678397 : continue;
1307 :
1308 10367780 : for (i = 0; i < gimple_phi_num_args (phi); i++)
1309 : {
1310 7353919 : tree arg = gimple_phi_arg_def (phi, i);
1311 7353919 : edge e = gimple_phi_arg_edge (phi, i);
1312 : /* We are only interested in copies emitted on critical
1313 : backedges. */
1314 13535570 : if (!(e->flags & EDGE_DFS_BACK)
1315 7353919 : || !EDGE_CRITICAL_P (e))
1316 6181651 : continue;
1317 :
1318 : /* If the argument is not an SSA_NAME, then we will need a
1319 : constant initialization. If the argument is an SSA_NAME then
1320 : a copy statement may be needed. First handle the case
1321 : where we cannot insert before the argument definition. */
1322 1172268 : if (TREE_CODE (arg) != SSA_NAME
1323 1172268 : || (gimple_code (SSA_NAME_DEF_STMT (arg)) == GIMPLE_PHI
1324 196027 : && trivially_conflicts_p (bb, result, arg)))
1325 : {
1326 51122 : tree name;
1327 51122 : gassign *stmt;
1328 51122 : gimple *last = NULL;
1329 51122 : gimple_stmt_iterator gsi2;
1330 :
1331 51122 : gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1332 51122 : if (!gsi_end_p (gsi2))
1333 51122 : last = gsi_stmt (gsi2);
1334 :
1335 : /* In theory the only way we ought to get back to the
1336 : start of a loop should be with a COND_EXPR or GOTO_EXPR.
1337 : However, better safe than sorry.
1338 : If the block ends with a control statement or
1339 : something that might throw, then we have to
1340 : insert this assignment before the last
1341 : statement. Else insert it after the last statement. */
1342 51122 : if (last && stmt_ends_bb_p (last))
1343 : {
1344 : /* If the last statement in the block is the definition
1345 : site of the PHI argument, then we can't insert
1346 : anything after it. */
1347 51122 : if (TREE_CODE (arg) == SSA_NAME
1348 51122 : && SSA_NAME_DEF_STMT (arg) == last)
1349 0 : continue;
1350 : }
1351 :
1352 : /* Create a new instance of the underlying variable of the
1353 : PHI result. */
1354 51122 : name = copy_ssa_name (result);
1355 51122 : stmt = gimple_build_assign (name,
1356 : gimple_phi_arg_def (phi, i));
1357 :
1358 : /* copy location if present. */
1359 51122 : if (gimple_phi_arg_has_location (phi, i))
1360 5153 : gimple_set_location (stmt,
1361 : gimple_phi_arg_location (phi, i));
1362 :
1363 : /* Insert the new statement into the block and update
1364 : the PHI node. */
1365 51122 : if (last && stmt_ends_bb_p (last))
1366 51122 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1367 : else
1368 0 : gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1369 51122 : SET_PHI_ARG_DEF (phi, i, name);
1370 : }
1371 : /* Insert a copy before the definition of the backedge value
1372 : and adjust all conflicting uses. */
1373 1121146 : else if (trivially_conflicts_p (bb, result, arg))
1374 : {
1375 34397 : gimple *def = SSA_NAME_DEF_STMT (arg);
1376 34397 : if (gimple_nop_p (def)
1377 34397 : || gimple_code (def) == GIMPLE_PHI)
1378 0 : continue;
1379 34397 : imm_use_iterator imm_iter;
1380 34397 : gimple *use_stmt;
1381 34397 : auto_vec<use_operand_p, 8> uses;
1382 34397 : int idx = -1;
1383 : /* The following matches trivially_conflicts_p. */
1384 233595 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, result)
1385 : {
1386 199198 : if (gimple_bb (use_stmt) != bb
1387 199198 : || (gimple_code (use_stmt) != GIMPLE_PHI
1388 148708 : && (maybe_renumber_stmts_bb (bb), true)
1389 148708 : && gimple_uid (use_stmt) > gimple_uid (def)))
1390 : {
1391 116647 : use_operand_p use;
1392 233758 : FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1393 : {
1394 116879 : uses.safe_push (use);
1395 116879 : if (!is_gimple_debug (use_stmt))
1396 : {
1397 55610 : if (idx == -1)
1398 68794 : idx = uses.length () - 1;
1399 : else
1400 : idx = -2;
1401 : }
1402 : }
1403 : }
1404 34397 : }
1405 : /* When there is just a conflicting statement try to
1406 : adjust that to refer to the new definition.
1407 : In particular for now handle a conflict with the
1408 : use in a (exit) condition with a NE compare,
1409 : replacing a pre-IV-increment compare with a
1410 : post-IV-increment one. */
1411 34397 : auto assign_def = dyn_cast<gassign *> (def);
1412 34397 : if (idx >= 0
1413 25181 : && is_a <gcond *> (USE_STMT (uses[idx]))
1414 8054 : && (gimple_cond_code (USE_STMT (uses[idx])) == NE_EXPR
1415 2146 : || gimple_cond_code (USE_STMT (uses[idx])) == EQ_EXPR)
1416 7440 : && assign_def
1417 7386 : && gimple_assign_rhs1 (assign_def) == result
1418 4419 : && (gimple_assign_rhs_code (assign_def) == PLUS_EXPR
1419 28 : || gimple_assign_rhs_code (assign_def) == MINUS_EXPR
1420 28 : || (gimple_assign_rhs_code (assign_def)
1421 : == POINTER_PLUS_EXPR))
1422 38793 : && TREE_CODE (gimple_assign_rhs2 (assign_def))
1423 : == INTEGER_CST)
1424 : {
1425 4396 : gcond *cond = as_a <gcond *> (USE_STMT (uses[idx]));
1426 4396 : tree *adj;
1427 4396 : if (gimple_cond_lhs (cond) == result)
1428 2482 : adj = gimple_cond_rhs_ptr (cond);
1429 : else
1430 1914 : adj = gimple_cond_lhs_ptr (cond);
1431 4396 : gimple_stmt_iterator gsi = gsi_for_stmt (cond);
1432 4396 : tree newval
1433 4396 : = gimple_build (&gsi, true, GSI_SAME_STMT,
1434 : UNKNOWN_LOCATION,
1435 : gimple_assign_rhs_code (assign_def),
1436 4396 : TREE_TYPE (*adj),
1437 : *adj, gimple_assign_rhs2 (assign_def));
1438 4396 : *adj = newval;
1439 4396 : SET_USE (uses[idx], arg);
1440 4396 : update_stmt (cond);
1441 : }
1442 : else
1443 : {
1444 30001 : tree name = copy_ssa_name (result);
1445 30001 : gimple *stmt = gimple_build_assign (name, result);
1446 30001 : gimple_stmt_iterator gsi = gsi_for_stmt (def);
1447 30001 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1448 202447 : for (auto use : uses)
1449 112444 : SET_USE (use, name);
1450 : }
1451 34397 : }
1452 : }
1453 : }
1454 :
1455 : /* Unmark this block again. */
1456 13022948 : bb->aux = NULL;
1457 : }
1458 1531890 : }
1459 :
1460 : /* Remove indirect clobbers. */
1461 :
1462 : static void
1463 1531890 : remove_indirect_clobbers (void)
1464 : {
1465 1531890 : basic_block bb;
1466 :
1467 14554838 : FOR_EACH_BB_FN (bb, cfun)
1468 127381727 : for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1469 : {
1470 101335831 : gimple *stmt = gsi_stmt (gsi);
1471 101335831 : if (gimple_clobber_p (stmt))
1472 : {
1473 1541427 : tree lhs = gimple_assign_lhs (stmt);
1474 1657847 : if (TREE_CODE (lhs) == MEM_REF
1475 1541427 : && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
1476 : {
1477 116420 : unlink_stmt_vdef (stmt);
1478 116420 : gsi_remove (&gsi, true);
1479 116420 : release_defs (stmt);
1480 116420 : continue;
1481 : }
1482 : }
1483 101219411 : gsi_next (&gsi);
1484 : }
1485 1531890 : }
1486 :
1487 : /* Free all memory associated with going out of SSA form. SA is
1488 : the outof-SSA info object. */
1489 :
1490 : void
1491 1531888 : finish_out_of_ssa (struct ssaexpand *sa)
1492 : {
1493 1531888 : free (sa->partition_to_pseudo);
1494 1531888 : if (sa->values)
1495 680859 : BITMAP_FREE (sa->values);
1496 1531888 : if (sa->ccmp_no_comparison)
1497 0 : BITMAP_FREE (sa->ccmp_no_comparison);
1498 1531888 : delete_var_map (sa->map);
1499 1531888 : BITMAP_FREE (sa->partitions_for_parm_default_defs);
1500 1531888 : BITMAP_FREE (sa->partitions_for_undefined_values);
1501 1531888 : memset (sa, 0, sizeof *sa);
1502 1531888 : }
1503 :
1504 : /* Take the current function out of SSA form, translating PHIs as described in
1505 : R. Morgan, ``Building an Optimizing Compiler'',
1506 : Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1507 :
1508 : unsigned int
1509 1531890 : rewrite_out_of_ssa (struct ssaexpand *sa)
1510 : {
1511 : /* Remove remaining indirect clobbers as we do not need those anymore.
1512 : Those might extend SSA lifetime and restrict coalescing. */
1513 1531890 : remove_indirect_clobbers ();
1514 :
1515 : /* If elimination of a PHI requires inserting a copy on a backedge,
1516 : then we will have to split the backedge which has numerous
1517 : undesirable performance effects.
1518 :
1519 : A significant number of such cases can be handled here by inserting
1520 : copies into the loop itself. */
1521 1531890 : insert_backedge_copies ();
1522 :
1523 : /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1524 1531890 : eliminate_useless_phis ();
1525 :
1526 1531890 : if (dump_file && (dump_flags & TDF_DETAILS))
1527 108 : gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1528 :
1529 1531890 : remove_ssa_form (flag_tree_ter, sa);
1530 :
1531 1531890 : return 0;
1532 : }
|