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 "cfghooks.h"
29 : #include "ssa.h"
30 : #include "tree-ssa.h"
31 : #include "memmodel.h"
32 : #include "emit-rtl.h"
33 : #include "gimple-pretty-print.h"
34 : #include "diagnostic-core.h"
35 : #include "tree-dfa.h"
36 : #include "stor-layout.h"
37 : #include "cfgrtl.h"
38 : #include "cfganal.h"
39 : #include "tree-eh.h"
40 : #include "gimple-iterator.h"
41 : #include "tree-cfg.h"
42 : #include "dumpfile.h"
43 : #include "tree-ssa-live.h"
44 : #include "tree-ssa-ter.h"
45 : #include "tree-ssa-coalesce.h"
46 : #include "tree-outof-ssa.h"
47 : #include "dojump.h"
48 : #include "internal-fn.h"
49 : #include "gimple-fold.h"
50 :
51 : /* FIXME: A lot of code here deals with expanding to RTL. All that code
52 : should be in cfgexpand.cc. */
53 : #include "explow.h"
54 : #include "expr.h"
55 :
56 : /* Return TRUE if expression STMT is suitable for replacement. */
57 :
58 : bool
59 42845976 : ssa_is_replaceable_p (gimple *stmt)
60 : {
61 42845976 : use_operand_p use_p;
62 42845976 : tree def;
63 42845976 : gimple *use_stmt;
64 :
65 : /* Only consider modify stmts and direct internal fn calls that are
66 : not also tail-calls. */
67 42845976 : gcall *call;
68 42845976 : if (!is_gimple_assign (stmt)
69 72129482 : && (!(call = dyn_cast <gcall *> (stmt))
70 5380867 : || gimple_call_tail_p (call)
71 5202750 : || !gimple_call_internal_p (call)
72 202336 : || !direct_internal_fn_p (gimple_call_internal_fn (call))))
73 : return false;
74 :
75 : /* If the statement may throw an exception, it cannot be replaced. */
76 31712476 : if (stmt_could_throw_p (cfun, stmt))
77 : return false;
78 :
79 : /* Punt if there is more than 1 def. */
80 30684337 : def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
81 30684337 : if (!def)
82 : return false;
83 :
84 : /* Only consider definitions which have a single use. */
85 24013024 : if (!single_imm_use (def, &use_p, &use_stmt))
86 : return false;
87 :
88 : /* Used in this block, but at the TOP of the block, not the end. */
89 20367181 : if (gimple_code (use_stmt) == GIMPLE_PHI)
90 : return false;
91 :
92 : /* There must be no VDEFs. */
93 38049918 : if (gimple_vdef (stmt))
94 : return false;
95 :
96 : /* Float expressions must go through memory if float-store is on. */
97 19024959 : if (flag_float_store
98 19024959 : && FLOAT_TYPE_P (TREE_TYPE (def)))
99 : return false;
100 :
101 : /* An assignment with a register variable on the RHS is not
102 : replaceable. */
103 19024516 : if (is_gimple_assign (stmt)
104 18943560 : && gimple_assign_rhs_code (stmt) == VAR_DECL
105 19991042 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt)))
106 : return false;
107 :
108 : /* Leave any stmt with volatile operands alone as well. */
109 38047356 : if (gimple_has_volatile_ops (stmt))
110 : return false;
111 :
112 : return true;
113 : }
114 :
115 :
116 : /* Used to hold all the components required to do SSA PHI elimination.
117 : The node and pred/succ list is a simple linear list of nodes and
118 : edges represented as pairs of nodes.
119 :
120 : The predecessor and successor list: Nodes are entered in pairs, where
121 : [0] ->PRED, [1]->SUCC. All the even indexes in the array represent
122 : predecessors, all the odd elements are successors.
123 :
124 : Rationale:
125 : When implemented as bitmaps, very large programs SSA->Normal times were
126 : being dominated by clearing the interference graph.
127 :
128 : Typically this list of edges is extremely small since it only includes
129 : PHI results and uses from a single edge which have not coalesced with
130 : each other. This means that no virtual PHI nodes are included, and
131 : empirical evidence suggests that the number of edges rarely exceed
132 : 3, and in a bootstrap of GCC, the maximum size encountered was 7.
133 : This also limits the number of possible nodes that are involved to
134 : rarely more than 6, and in the bootstrap of gcc, the maximum number
135 : of nodes encountered was 12. */
136 :
137 : class elim_graph
138 : {
139 : public:
140 : elim_graph (var_map map);
141 :
142 : /* Size of the elimination vectors. */
143 : int size;
144 :
145 : /* List of nodes in the elimination graph. */
146 : auto_vec<int> nodes;
147 :
148 : /* The predecessor and successor edge list. */
149 : auto_vec<int> edge_list;
150 :
151 : /* Source locus on each edge */
152 : auto_vec<location_t> edge_locus;
153 :
154 : /* Visited vector. */
155 : auto_sbitmap visited;
156 :
157 : /* Stack for visited nodes. */
158 : auto_vec<int> stack;
159 :
160 : /* The variable partition map. */
161 : var_map map;
162 :
163 : /* Edge being eliminated by this graph. */
164 : edge e;
165 :
166 : /* List of constant copies to emit. These are pushed on in pairs. */
167 : auto_vec<int> const_dests;
168 : auto_vec<tree> const_copies;
169 :
170 : /* Source locations for any constant copies. */
171 : auto_vec<location_t> copy_locus;
172 : };
173 :
174 :
175 : /* For an edge E find out a good source location to associate with
176 : instructions inserted on edge E. If E has an implicit goto set,
177 : use its location. Otherwise search instructions in predecessors
178 : of E for a location, and use that one. That makes sense because
179 : we insert on edges for PHI nodes, and effects of PHIs happen on
180 : the end of the predecessor conceptually. An exception is made
181 : for EH edges because we don't want to drag the source location
182 : of unrelated statements at the beginning of handlers; they would
183 : be further reused for various EH constructs, which would damage
184 : the coverage information. */
185 :
186 : static void
187 1779546 : set_location_for_edge (edge e)
188 : {
189 1779546 : if (e->goto_locus)
190 123427 : set_curr_insn_location (e->goto_locus);
191 1656119 : else if (e->flags & EDGE_EH)
192 : {
193 6117 : basic_block bb = e->dest;
194 6266 : gimple_stmt_iterator gsi;
195 :
196 6266 : do
197 : {
198 111346 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
199 : {
200 104922 : gimple *stmt = gsi_stmt (gsi);
201 104922 : if (is_gimple_debug (stmt))
202 77338 : continue;
203 27584 : if (gimple_has_location (stmt) || gimple_block (stmt))
204 : {
205 6108 : set_curr_insn_location (gimple_location (stmt));
206 6108 : return;
207 : }
208 : }
209 : /* Nothing found in this basic block. Make a half-assed attempt
210 : to continue with another block. */
211 158 : if (single_succ_p (bb))
212 149 : bb = single_succ (bb);
213 : else
214 9 : bb = e->dest;
215 : }
216 158 : while (bb != e->dest);
217 : }
218 : else
219 : {
220 1650002 : basic_block bb = e->src;
221 2006605 : gimple_stmt_iterator gsi;
222 :
223 2006605 : do
224 : {
225 10275290 : for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
226 : {
227 4529409 : gimple *stmt = gsi_stmt (gsi);
228 4529409 : if (is_gimple_debug (stmt))
229 1550173 : continue;
230 2979236 : if (gimple_has_location (stmt) || gimple_block (stmt))
231 : {
232 1398369 : set_curr_insn_location (gimple_location (stmt));
233 1398369 : return;
234 : }
235 : }
236 : /* Nothing found in this basic block. Make a half-assed attempt
237 : to continue with another block. */
238 608236 : if (single_pred_p (bb))
239 356603 : bb = single_pred (bb);
240 : else
241 251633 : bb = e->src;
242 : }
243 608236 : while (bb != e->src);
244 : }
245 : }
246 :
247 : /* Emit insns to copy SRC into DEST converting SRC if necessary. As
248 : SRC/DEST might be BLKmode memory locations SIZEEXP is a tree from
249 : which we deduce the size to copy in that case. */
250 :
251 : static inline rtx_insn *
252 489032 : emit_partition_copy (rtx dest, rtx src, int unsignedsrcp, tree sizeexp)
253 : {
254 489032 : start_sequence ();
255 :
256 489032 : if (GET_MODE (src) != VOIDmode && GET_MODE (src) != GET_MODE (dest))
257 0 : src = convert_to_mode (GET_MODE (dest), src, unsignedsrcp);
258 489032 : if (GET_MODE (src) == BLKmode)
259 : {
260 9 : gcc_assert (GET_MODE (dest) == BLKmode);
261 9 : emit_block_move (dest, src, expr_size (sizeexp), BLOCK_OP_NORMAL);
262 : }
263 : else
264 489023 : emit_move_insn (dest, src);
265 489032 : do_pending_stack_adjust ();
266 :
267 489032 : return end_sequence ();
268 : }
269 :
270 : /* Insert a copy instruction from partition SRC to DEST onto edge E. */
271 :
272 : static void
273 480644 : insert_partition_copy_on_edge (edge e, int dest, int src, location_t locus)
274 : {
275 480644 : tree var;
276 480644 : if (dump_file && (dump_flags & TDF_DETAILS))
277 : {
278 0 : fprintf (dump_file,
279 : "Inserting a partition copy on edge BB%d->BB%d : "
280 : "PART.%d = PART.%d",
281 0 : e->src->index,
282 0 : e->dest->index, dest, src);
283 0 : fprintf (dump_file, "\n");
284 : }
285 :
286 480644 : gcc_assert (SA.partition_to_pseudo[dest]);
287 480644 : gcc_assert (SA.partition_to_pseudo[src]);
288 :
289 480644 : set_location_for_edge (e);
290 : /* If a locus is provided, override the default. */
291 480644 : if (locus)
292 209935 : set_curr_insn_location (locus);
293 :
294 480644 : var = partition_to_var (SA.map, src);
295 480644 : rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
296 480644 : copy_rtx (SA.partition_to_pseudo[src]),
297 480644 : TYPE_UNSIGNED (TREE_TYPE (var)),
298 : var);
299 :
300 480644 : insert_insn_on_edge (seq, e);
301 480644 : }
302 :
303 : /* Insert a copy instruction from expression SRC to partition DEST
304 : onto edge E. */
305 :
306 : static void
307 1290514 : insert_value_copy_on_edge (edge e, int dest, tree src, location_t locus)
308 : {
309 1290514 : rtx dest_rtx, seq, x;
310 1290514 : machine_mode dest_mode, src_mode;
311 1290514 : int unsignedp;
312 :
313 1290514 : if (dump_file && (dump_flags & TDF_DETAILS))
314 : {
315 9 : fprintf (dump_file,
316 : "Inserting a value copy on edge BB%d->BB%d : PART.%d = ",
317 9 : e->src->index,
318 9 : e->dest->index, dest);
319 9 : print_generic_expr (dump_file, src, TDF_SLIM);
320 9 : fprintf (dump_file, "\n");
321 : }
322 :
323 1290514 : dest_rtx = copy_rtx (SA.partition_to_pseudo[dest]);
324 1290514 : gcc_assert (dest_rtx);
325 :
326 1290514 : set_location_for_edge (e);
327 : /* If a locus is provided, override the default. */
328 1290514 : if (locus)
329 695187 : set_curr_insn_location (locus);
330 :
331 1290514 : start_sequence ();
332 :
333 1290514 : tree name = partition_to_var (SA.map, dest);
334 1290514 : src_mode = TYPE_MODE (TREE_TYPE (src));
335 1290514 : dest_mode = GET_MODE (dest_rtx);
336 1290514 : gcc_assert (src_mode == TYPE_MODE (TREE_TYPE (name)));
337 1290514 : gcc_assert (!REG_P (dest_rtx)
338 : || dest_mode == promote_ssa_mode (name, &unsignedp));
339 :
340 1290514 : if (src_mode != dest_mode)
341 : {
342 0 : x = expand_expr (src, NULL, src_mode, EXPAND_NORMAL);
343 0 : x = convert_modes (dest_mode, src_mode, x, unsignedp);
344 : }
345 1290514 : else if (src_mode == BLKmode)
346 : {
347 6 : x = dest_rtx;
348 6 : store_expr (src, x, 0, false, false);
349 : }
350 : else
351 1290508 : x = expand_expr (src, dest_rtx, dest_mode, EXPAND_NORMAL);
352 :
353 1290514 : if (x != dest_rtx)
354 1245915 : emit_move_insn (dest_rtx, x);
355 1290514 : do_pending_stack_adjust ();
356 :
357 1290514 : seq = end_sequence ();
358 :
359 1290514 : insert_insn_on_edge (seq, e);
360 1290514 : }
361 :
362 : /* Insert a copy instruction from RTL expression SRC to partition DEST
363 : onto edge E. */
364 :
365 : static void
366 4194 : insert_rtx_to_part_on_edge (edge e, int dest, rtx src, int unsignedsrcp,
367 : location_t locus)
368 : {
369 4194 : if (dump_file && (dump_flags & TDF_DETAILS))
370 : {
371 0 : fprintf (dump_file,
372 : "Inserting a temp copy on edge BB%d->BB%d : PART.%d = ",
373 0 : e->src->index,
374 0 : e->dest->index, dest);
375 0 : print_simple_rtl (dump_file, src);
376 0 : fprintf (dump_file, "\n");
377 : }
378 :
379 4194 : gcc_assert (SA.partition_to_pseudo[dest]);
380 :
381 4194 : set_location_for_edge (e);
382 : /* If a locus is provided, override the default. */
383 4194 : if (locus)
384 1836 : set_curr_insn_location (locus);
385 :
386 : /* We give the destination as sizeexp in case src/dest are BLKmode
387 : mems. Usually we give the source. As we result from SSA names
388 : the left and right size should be the same (and no WITH_SIZE_EXPR
389 : involved), so it doesn't matter. */
390 4194 : rtx_insn *seq = emit_partition_copy (copy_rtx (SA.partition_to_pseudo[dest]),
391 : src, unsignedsrcp,
392 : partition_to_var (SA.map, dest));
393 :
394 4194 : insert_insn_on_edge (seq, e);
395 4194 : }
396 :
397 : /* Insert a copy instruction from partition SRC to RTL lvalue DEST
398 : onto edge E. */
399 :
400 : static void
401 4194 : insert_part_to_rtx_on_edge (edge e, rtx dest, int src, location_t locus)
402 : {
403 4194 : tree var;
404 4194 : if (dump_file && (dump_flags & TDF_DETAILS))
405 : {
406 0 : fprintf (dump_file,
407 : "Inserting a temp copy on edge BB%d->BB%d : ",
408 0 : e->src->index,
409 0 : e->dest->index);
410 0 : print_simple_rtl (dump_file, dest);
411 0 : fprintf (dump_file, "= PART.%d\n", src);
412 : }
413 :
414 4194 : gcc_assert (SA.partition_to_pseudo[src]);
415 :
416 4194 : set_location_for_edge (e);
417 : /* If a locus is provided, override the default. */
418 4194 : if (locus)
419 0 : set_curr_insn_location (locus);
420 :
421 4194 : var = partition_to_var (SA.map, src);
422 4194 : rtx_insn *seq = emit_partition_copy (dest,
423 4194 : copy_rtx (SA.partition_to_pseudo[src]),
424 4194 : TYPE_UNSIGNED (TREE_TYPE (var)),
425 : var);
426 :
427 4194 : insert_insn_on_edge (seq, e);
428 4194 : }
429 :
430 :
431 : /* Create an elimination graph for map. */
432 :
433 1472141 : elim_graph::elim_graph (var_map map) :
434 1472141 : nodes (30), edge_list (20), edge_locus (10), visited (map->num_partitions),
435 1472141 : stack (30), map (map), const_dests (20), const_copies (20), copy_locus (10)
436 : {
437 1472141 : }
438 :
439 :
440 : /* Empty elimination graph G. */
441 :
442 : static inline void
443 3684107 : clear_elim_graph (elim_graph *g)
444 : {
445 3684107 : g->nodes.truncate (0);
446 3684107 : g->edge_list.truncate (0);
447 3684107 : g->edge_locus.truncate (0);
448 3684107 : }
449 :
450 :
451 : /* Return the number of nodes in graph G. */
452 :
453 : static inline int
454 3684107 : elim_graph_size (elim_graph *g)
455 : {
456 12342835 : return g->nodes.length ();
457 : }
458 :
459 :
460 : /* Add NODE to graph G, if it doesn't exist already. */
461 :
462 : static inline void
463 969676 : elim_graph_add_node (elim_graph *g, int node)
464 : {
465 969676 : int x;
466 969676 : int t;
467 :
468 2209156 : FOR_EACH_VEC_ELT (g->nodes, x, t)
469 1277188 : if (t == node)
470 969676 : return;
471 931968 : g->nodes.safe_push (node);
472 : }
473 :
474 :
475 : /* Add the edge PRED->SUCC to graph G. */
476 :
477 : static inline void
478 484838 : elim_graph_add_edge (elim_graph *g, int pred, int succ, location_t locus)
479 : {
480 484838 : g->edge_list.safe_push (pred);
481 484838 : g->edge_list.safe_push (succ);
482 484838 : g->edge_locus.safe_push (locus);
483 484838 : }
484 :
485 :
486 : /* Remove an edge from graph G for which NODE is the predecessor, and
487 : return the successor node. -1 is returned if there is no such edge. */
488 :
489 : static inline int
490 923034 : elim_graph_remove_succ_edge (elim_graph *g, int node, location_t *locus)
491 : {
492 923034 : int y;
493 923034 : unsigned x;
494 1945979 : for (x = 0; x < g->edge_list.length (); x += 2)
495 1498849 : if (g->edge_list[x] == node)
496 : {
497 475904 : g->edge_list[x] = -1;
498 475904 : y = g->edge_list[x + 1];
499 475904 : g->edge_list[x + 1] = -1;
500 475904 : *locus = g->edge_locus[x / 2];
501 475904 : g->edge_locus[x / 2] = UNKNOWN_LOCATION;
502 475904 : return y;
503 : }
504 447130 : *locus = UNKNOWN_LOCATION;
505 447130 : return -1;
506 : }
507 :
508 :
509 : /* Find all the nodes in GRAPH which are successors to NODE in the
510 : edge list. VAR will hold the partition number found. CODE is the
511 : code fragment executed for every node found. */
512 :
513 : #define FOR_EACH_ELIM_GRAPH_SUCC(GRAPH, NODE, VAR, LOCUS, CODE) \
514 : do { \
515 : unsigned x_; \
516 : int y_; \
517 : for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
518 : { \
519 : y_ = (GRAPH)->edge_list[x_]; \
520 : if (y_ != (NODE)) \
521 : continue; \
522 : (void) ((VAR) = (GRAPH)->edge_list[x_ + 1]); \
523 : (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
524 : CODE; \
525 : } \
526 : } while (0)
527 :
528 :
529 : /* Find all the nodes which are predecessors of NODE in the edge list for
530 : GRAPH. VAR will hold the partition number found. CODE is the
531 : code fragment executed for every node found. */
532 :
533 : #define FOR_EACH_ELIM_GRAPH_PRED(GRAPH, NODE, VAR, LOCUS, CODE) \
534 : do { \
535 : unsigned x_; \
536 : int y_; \
537 : for (x_ = 0; x_ < (GRAPH)->edge_list.length (); x_ += 2) \
538 : { \
539 : y_ = (GRAPH)->edge_list[x_ + 1]; \
540 : if (y_ != (NODE)) \
541 : continue; \
542 : (void) ((VAR) = (GRAPH)->edge_list[x_]); \
543 : (void) ((LOCUS) = (GRAPH)->edge_locus[x_ / 2]); \
544 : CODE; \
545 : } \
546 : } while (0)
547 :
548 :
549 : /* Add T to elimination graph G. */
550 :
551 : static inline void
552 969676 : eliminate_name (elim_graph *g, int T)
553 : {
554 969676 : elim_graph_add_node (g, T);
555 : }
556 :
557 : /* Return true if this phi argument T should have a copy queued when using
558 : var_map MAP. PHI nodes should contain only ssa_names and invariants. A
559 : test for ssa_name is definitely simpler, but don't let invalid contents
560 : slip through in the meantime. */
561 :
562 : static inline bool
563 6932443 : queue_phi_copy_p (var_map map, tree t)
564 : {
565 6932443 : if (TREE_CODE (t) == SSA_NAME)
566 : {
567 5641929 : if (var_to_partition (map, t) == NO_PARTITION)
568 : return true;
569 : return false;
570 : }
571 1290514 : gcc_checking_assert (is_gimple_min_invariant (t));
572 : return true;
573 : }
574 :
575 : /* Build elimination graph G for basic block BB on incoming PHI edge
576 : G->e. */
577 :
578 : static void
579 3684107 : eliminate_build (elim_graph *g)
580 : {
581 3684107 : tree Ti;
582 3684107 : int p0, pi;
583 3684107 : gphi_iterator gsi;
584 :
585 3684107 : clear_elim_graph (g);
586 :
587 10616550 : for (gsi = gsi_start_phis (g->e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
588 : {
589 6932443 : gphi *phi = gsi.phi ();
590 6932443 : location_t locus;
591 :
592 6932443 : p0 = var_to_partition (g->map, gimple_phi_result (phi));
593 : /* Ignore results which are not in partitions. */
594 6932443 : if (p0 == NO_PARTITION)
595 0 : continue;
596 :
597 6932443 : Ti = PHI_ARG_DEF (phi, g->e->dest_idx);
598 : /* See set_location_for_edge for the rationale. */
599 6932443 : if (g->e->flags & EDGE_EH)
600 43167 : locus = UNKNOWN_LOCATION;
601 : else
602 6889276 : locus = gimple_phi_arg_location_from_edge (phi, g->e);
603 :
604 : /* If this argument is a constant, or a SSA_NAME which is being
605 : left in SSA form, just queue a copy to be emitted on this
606 : edge. */
607 6932443 : if (queue_phi_copy_p (g->map, Ti))
608 : {
609 : /* Save constant copies until all other copies have been emitted
610 : on this edge. */
611 1290514 : g->const_dests.safe_push (p0);
612 1290514 : g->const_copies.safe_push (Ti);
613 1290514 : g->copy_locus.safe_push (locus);
614 : }
615 : else
616 : {
617 5641929 : pi = var_to_partition (g->map, Ti);
618 5641929 : if (p0 != pi)
619 : {
620 484838 : eliminate_name (g, p0);
621 484838 : eliminate_name (g, pi);
622 484838 : elim_graph_add_edge (g, p0, pi, locus);
623 : }
624 : }
625 : }
626 3684107 : }
627 :
628 :
629 : /* Push successors of T onto the elimination stack for G. */
630 :
631 : static void
632 931968 : elim_forward (elim_graph *g, int T)
633 : {
634 931968 : int S;
635 931968 : location_t locus;
636 :
637 931968 : bitmap_set_bit (g->visited, T);
638 3626844 : FOR_EACH_ELIM_GRAPH_SUCC (g, T, S, locus,
639 : {
640 : if (!bitmap_bit_p (g->visited, S))
641 : elim_forward (g, S);
642 : });
643 931968 : g->stack.safe_push (T);
644 931968 : }
645 :
646 :
647 : /* Return 1 if there unvisited predecessors of T in graph G. */
648 :
649 : static int
650 927228 : elim_unvisited_predecessor (elim_graph *g, int T)
651 : {
652 927228 : int P;
653 927228 : location_t locus;
654 :
655 2659250 : FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
656 : {
657 : if (!bitmap_bit_p (g->visited, P))
658 : return 1;
659 : });
660 : return 0;
661 : }
662 :
663 : /* Process predecessors first, and insert a copy. */
664 :
665 : static void
666 8934 : elim_backward (elim_graph *g, int T)
667 : {
668 8934 : int P;
669 8934 : location_t locus;
670 :
671 8934 : bitmap_set_bit (g->visited, T);
672 60838 : FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
673 : {
674 : if (!bitmap_bit_p (g->visited, P))
675 : {
676 : elim_backward (g, P);
677 : insert_partition_copy_on_edge (g->e, P, T, locus);
678 : }
679 : });
680 8934 : }
681 :
682 : /* Allocate a new pseudo register usable for storing values sitting
683 : in NAME (a decl or SSA name), i.e. with matching mode and attributes. */
684 :
685 : static rtx
686 4194 : get_temp_reg (tree name)
687 : {
688 4194 : tree type = TREE_TYPE (name);
689 4194 : int unsignedp;
690 4194 : machine_mode reg_mode = promote_ssa_mode (name, &unsignedp);
691 4194 : if (reg_mode == BLKmode)
692 1 : return assign_temp (type, 0, 0);
693 4193 : rtx x = gen_reg_rtx (reg_mode);
694 4193 : if (POINTER_TYPE_P (type))
695 791 : mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
696 : return x;
697 : }
698 :
699 : /* Insert required copies for T in graph G. Check for a strongly connected
700 : region, and create a temporary to break the cycle if one is found. */
701 :
702 : static void
703 927228 : elim_create (elim_graph *g, int T)
704 : {
705 927228 : int P, S;
706 927228 : location_t locus;
707 :
708 927228 : if (elim_unvisited_predecessor (g, T))
709 : {
710 4194 : tree var = partition_to_var (g->map, T);
711 4194 : rtx U = get_temp_reg (var);
712 4194 : int unsignedsrcp = TYPE_UNSIGNED (TREE_TYPE (var));
713 :
714 4194 : insert_part_to_rtx_on_edge (g->e, U, T, UNKNOWN_LOCATION);
715 27602 : FOR_EACH_ELIM_GRAPH_PRED (g, T, P, locus,
716 : {
717 : if (!bitmap_bit_p (g->visited, P))
718 : {
719 : elim_backward (g, P);
720 : insert_rtx_to_part_on_edge (g->e, P, U, unsignedsrcp, locus);
721 : }
722 : });
723 : }
724 : else
725 : {
726 923034 : S = elim_graph_remove_succ_edge (g, T, &locus);
727 923034 : if (S != -1)
728 : {
729 475904 : bitmap_set_bit (g->visited, T);
730 475904 : insert_partition_copy_on_edge (g->e, T, S, locus);
731 : }
732 : }
733 927228 : }
734 :
735 :
736 : /* Eliminate all the phi nodes on edge E in graph G. */
737 :
738 : static void
739 3688526 : eliminate_phi (edge e, elim_graph *g)
740 : {
741 3688526 : int x;
742 :
743 3688526 : gcc_assert (g->const_copies.length () == 0);
744 3688526 : gcc_assert (g->copy_locus.length () == 0);
745 :
746 : /* Abnormal edges already have everything coalesced. */
747 3688526 : if (e->flags & EDGE_ABNORMAL)
748 : return;
749 :
750 3684107 : g->e = e;
751 :
752 3684107 : eliminate_build (g);
753 :
754 3684107 : if (elim_graph_size (g) != 0)
755 : {
756 378017 : int part;
757 :
758 378017 : bitmap_clear (g->visited);
759 378017 : g->stack.truncate (0);
760 :
761 1688002 : FOR_EACH_VEC_ELT (g->nodes, x, part)
762 : {
763 931968 : if (!bitmap_bit_p (g->visited, part))
764 474881 : elim_forward (g, part);
765 : }
766 :
767 378017 : bitmap_clear (g->visited);
768 1688002 : while (g->stack.length () > 0)
769 : {
770 931968 : x = g->stack.pop ();
771 931968 : if (!bitmap_bit_p (g->visited, x))
772 927228 : elim_create (g, x);
773 : }
774 : }
775 :
776 : /* If there are any pending constant copies, issue them now. */
777 4974621 : while (g->const_copies.length () > 0)
778 : {
779 1290514 : int dest;
780 1290514 : tree src;
781 1290514 : location_t locus;
782 :
783 1290514 : src = g->const_copies.pop ();
784 1290514 : dest = g->const_dests.pop ();
785 1290514 : locus = g->copy_locus.pop ();
786 1290514 : insert_value_copy_on_edge (e, dest, src, locus);
787 : }
788 : }
789 :
790 :
791 : /* Remove each argument from PHI. If an arg was the last use of an SSA_NAME,
792 : check to see if this allows another PHI node to be removed. */
793 :
794 : static void
795 242 : remove_gimple_phi_args (gphi *phi)
796 : {
797 242 : use_operand_p arg_p;
798 242 : ssa_op_iter iter;
799 :
800 242 : if (dump_file && (dump_flags & TDF_DETAILS))
801 : {
802 0 : fprintf (dump_file, "Removing Dead PHI definition: ");
803 0 : print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
804 : }
805 :
806 729 : FOR_EACH_PHI_ARG (arg_p, phi, iter, SSA_OP_USE)
807 : {
808 487 : tree arg = USE_FROM_PTR (arg_p);
809 487 : if (TREE_CODE (arg) == SSA_NAME)
810 : {
811 : /* Remove the reference to the existing argument. */
812 330 : SET_USE (arg_p, NULL_TREE);
813 330 : if (has_zero_uses (arg))
814 : {
815 222 : gimple *stmt;
816 222 : gimple_stmt_iterator gsi;
817 :
818 222 : stmt = SSA_NAME_DEF_STMT (arg);
819 :
820 : /* Also remove the def if it is a PHI node. */
821 222 : if (gimple_code (stmt) == GIMPLE_PHI)
822 : {
823 3 : remove_gimple_phi_args (as_a <gphi *> (stmt));
824 3 : gsi = gsi_for_stmt (stmt);
825 3 : remove_phi_node (&gsi, true);
826 : }
827 :
828 : }
829 : }
830 : }
831 242 : }
832 :
833 : /* Remove any PHI node which is a virtual PHI, or a PHI with no uses. */
834 :
835 : static void
836 1472141 : eliminate_useless_phis (void)
837 : {
838 1472141 : basic_block bb;
839 1472141 : gphi_iterator gsi;
840 1472141 : tree result;
841 :
842 14042323 : FOR_EACH_BB_FN (bb, cfun)
843 : {
844 18099567 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
845 : {
846 5529385 : gphi *phi = gsi.phi ();
847 5529385 : result = gimple_phi_result (phi);
848 11058770 : if (virtual_operand_p (result))
849 2622310 : remove_phi_node (&gsi, true);
850 : else
851 : {
852 : /* Also remove real PHIs with no uses. */
853 2907075 : if (has_zero_uses (result))
854 : {
855 239 : remove_gimple_phi_args (phi);
856 239 : remove_phi_node (&gsi, true);
857 : }
858 : else
859 2906836 : gsi_next (&gsi);
860 : }
861 : }
862 : }
863 1472141 : }
864 :
865 :
866 : /* This function will rewrite the current program using the variable mapping
867 : found in MAP. If the replacement vector VALUES is provided, any
868 : occurrences of partitions with non-null entries in the vector will be
869 : replaced with the expression in the vector instead of its mapped
870 : variable. */
871 :
872 : static void
873 1472141 : rewrite_trees (var_map map)
874 : {
875 1472141 : if (!flag_checking)
876 : return;
877 :
878 1472121 : basic_block bb;
879 : /* Search for PHIs where the destination has no partition, but one
880 : or more arguments has a partition. This should not happen and can
881 : create incorrect code. */
882 14042226 : FOR_EACH_BB_FN (bb, cfun)
883 : {
884 12570105 : gphi_iterator gsi;
885 15476917 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
886 : {
887 2906812 : gphi *phi = gsi.phi ();
888 2906812 : tree T0 = var_to_partition_to_var (map, gimple_phi_result (phi));
889 2906812 : if (T0 == NULL_TREE)
890 : {
891 : size_t i;
892 0 : for (i = 0; i < gimple_phi_num_args (phi); i++)
893 : {
894 0 : tree arg = PHI_ARG_DEF (phi, i);
895 :
896 0 : if (TREE_CODE (arg) == SSA_NAME
897 0 : && var_to_partition (map, arg) != NO_PARTITION)
898 : {
899 0 : fprintf (stderr, "Argument of PHI is in a partition :(");
900 0 : print_generic_expr (stderr, arg, TDF_SLIM);
901 0 : fprintf (stderr, "), but the result is not :");
902 0 : print_gimple_stmt (stderr, phi, 0, TDF_SLIM);
903 0 : internal_error ("SSA corruption");
904 : }
905 : }
906 : }
907 : }
908 : }
909 : }
910 :
911 : /* Create a default def for VAR. */
912 :
913 : static void
914 3908026 : create_default_def (tree var, void *arg ATTRIBUTE_UNUSED)
915 : {
916 3908026 : if (!is_gimple_reg (var))
917 : return;
918 :
919 3606621 : tree ssa = get_or_create_ssa_default_def (cfun, var);
920 3606621 : gcc_assert (ssa);
921 : }
922 :
923 : /* Call CALLBACK for all PARM_DECLs and RESULT_DECLs for which
924 : assign_parms may ask for a default partition. */
925 :
926 : static void
927 2944282 : for_all_parms (void (*callback)(tree var, void *arg), void *arg)
928 : {
929 9149964 : for (tree var = DECL_ARGUMENTS (current_function_decl); var;
930 6205682 : var = DECL_CHAIN (var))
931 6205682 : callback (var, arg);
932 2944282 : if (!VOID_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
933 1571734 : callback (DECL_RESULT (current_function_decl), arg);
934 2944282 : if (cfun->static_chain_decl)
935 38636 : callback (cfun->static_chain_decl, arg);
936 2944282 : }
937 :
938 : /* We need to pass two arguments to set_parm_default_def_partition,
939 : but for_all_parms only supports one. Use a pair. */
940 :
941 : typedef std::pair<var_map, bitmap> parm_default_def_partition_arg;
942 :
943 : /* Set in ARG's PARTS bitmap the bit corresponding to the partition in
944 : ARG's MAP containing VAR's default def. */
945 :
946 : static void
947 3908026 : set_parm_default_def_partition (tree var, void *arg_)
948 : {
949 3908026 : parm_default_def_partition_arg *arg = (parm_default_def_partition_arg *)arg_;
950 3908026 : var_map map = arg->first;
951 3908026 : bitmap parts = arg->second;
952 :
953 3908026 : if (!is_gimple_reg (var))
954 : return;
955 :
956 3606621 : tree ssa = ssa_default_def (cfun, var);
957 3606621 : gcc_assert (ssa);
958 :
959 3606621 : int version = var_to_partition (map, ssa);
960 3606621 : gcc_assert (version != NO_PARTITION);
961 :
962 3606621 : bool changed = bitmap_set_bit (parts, version);
963 3606621 : gcc_assert (changed);
964 : }
965 :
966 : /* Allocate and return a bitmap that has a bit set for each partition
967 : that contains a default def for a parameter. */
968 :
969 : static bitmap
970 1472141 : get_parm_default_def_partitions (var_map map)
971 : {
972 1472141 : bitmap parm_default_def_parts = BITMAP_ALLOC (NULL);
973 :
974 1472141 : parm_default_def_partition_arg
975 1472141 : arg = std::make_pair (map, parm_default_def_parts);
976 :
977 1472141 : for_all_parms (set_parm_default_def_partition, &arg);
978 :
979 1472141 : return parm_default_def_parts;
980 : }
981 :
982 : /* Allocate and return a bitmap that has a bit set for each partition
983 : that contains an undefined value. */
984 :
985 : static bitmap
986 1472141 : get_undefined_value_partitions (var_map map)
987 : {
988 1472141 : bitmap undefined_value_parts = BITMAP_ALLOC (NULL);
989 :
990 72560160 : for (unsigned int i = 1; i < num_ssa_names; i++)
991 : {
992 71088019 : tree var = ssa_name (i);
993 71088019 : if (var
994 48294378 : && !virtual_operand_p (var)
995 31400731 : && !has_zero_uses (var)
996 100249929 : && ssa_undefined_value_p (var))
997 : {
998 63776 : const int p = var_to_partition (map, var);
999 63776 : if (p != NO_PARTITION)
1000 63776 : bitmap_set_bit (undefined_value_parts, p);
1001 : }
1002 : }
1003 :
1004 1472141 : return undefined_value_parts;
1005 : }
1006 :
1007 : /* Given the out-of-ssa info object SA (with prepared partitions)
1008 : eliminate all phi nodes in all basic blocks. Afterwards there
1009 : are possibly some RTL instructions inserted on edges. */
1010 :
1011 : void
1012 1472141 : expand_phi_nodes (struct ssaexpand *sa)
1013 : {
1014 1472141 : basic_block bb;
1015 1472141 : elim_graph g (sa->map);
1016 :
1017 14042333 : FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb,
1018 : EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1019 12570192 : if (!gimple_seq_empty_p (phi_nodes (bb)))
1020 : {
1021 1579936 : edge e;
1022 1579936 : edge_iterator ei;
1023 5268462 : FOR_EACH_EDGE (e, ei, bb->preds)
1024 3688526 : eliminate_phi (e, &g);
1025 : /* We can't redirect EH edges in RTL land, so we need to do this
1026 : here. Redirection happens only when splitting is necessary,
1027 : which it is only for critical edges, normally. For EH edges
1028 : it might also be necessary when the successor has more than
1029 : one predecessor. In that case the edge is either required to
1030 : be fallthru (which EH edges aren't), or the predecessor needs
1031 : to end with a jump (which again, isn't the case with EH edges).
1032 : Hence, split all EH edges on which we inserted instructions
1033 : and whose successor has multiple predecessors. */
1034 5271230 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1035 : {
1036 1241417 : if (e->insns.r && (e->flags & EDGE_EH)
1037 3694062 : && !single_pred_p (e->dest))
1038 : {
1039 2768 : rtx_insn *insns = e->insns.r;
1040 2768 : basic_block bb;
1041 2768 : e->insns.r = NULL;
1042 2768 : bb = split_edge (e);
1043 2768 : single_pred_edge (bb)->insns.r = insns;
1044 : }
1045 : else
1046 3688526 : ei_next (&ei);
1047 : }
1048 : }
1049 1472141 : }
1050 :
1051 :
1052 : /* Remove the ssa-names in the current function and translate them into normal
1053 : compiler variables. PERFORM_TER is true if Temporary Expression Replacement
1054 : should also be used. */
1055 :
1056 : static void
1057 1472141 : remove_ssa_form (bool perform_ter, struct ssaexpand *sa)
1058 : {
1059 1472141 : bitmap values = NULL;
1060 1472141 : var_map map;
1061 :
1062 1472141 : for_all_parms (create_default_def, NULL);
1063 2944282 : map = init_var_map (num_ssa_names);
1064 1472141 : coalesce_ssa_name (map);
1065 :
1066 : /* Return to viewing the variable list as just all reference variables after
1067 : coalescing has been performed. */
1068 1472141 : partition_view_normal (map);
1069 :
1070 1472141 : if (dump_file && (dump_flags & TDF_DETAILS))
1071 : {
1072 115 : fprintf (dump_file, "After Coalescing:\n");
1073 115 : dump_var_map (dump_file, map);
1074 : }
1075 :
1076 1472141 : if (perform_ter)
1077 : {
1078 1043996 : values = find_replaceable_exprs (map);
1079 1043996 : if (values && dump_file && (dump_flags & TDF_DETAILS))
1080 12 : dump_replaceable_exprs (dump_file, values);
1081 : }
1082 :
1083 1472141 : rewrite_trees (map);
1084 :
1085 1472141 : sa->map = map;
1086 1472141 : sa->values = values;
1087 1472141 : sa->partitions_for_parm_default_defs = get_parm_default_def_partitions (map);
1088 1472141 : sa->partitions_for_undefined_values = get_undefined_value_partitions (map);
1089 1472141 : }
1090 :
1091 :
1092 : /* If not already done so for basic block BB, assign increasing uids
1093 : to each of its instructions. */
1094 :
1095 : static void
1096 974762 : maybe_renumber_stmts_bb (basic_block bb)
1097 : {
1098 974762 : unsigned i = 0;
1099 974762 : gimple_stmt_iterator gsi;
1100 :
1101 974762 : if (!bb->aux)
1102 974762 : return;
1103 276517 : bb->aux = NULL;
1104 3973413 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1105 : {
1106 3420379 : gimple *stmt = gsi_stmt (gsi);
1107 3420379 : gimple_set_uid (stmt, i);
1108 3420379 : i++;
1109 : }
1110 : }
1111 :
1112 :
1113 : /* Return true if we can determine that the SSA_NAMEs RESULT (a result
1114 : of a PHI node) and ARG (one of its arguments) conflict. Return false
1115 : otherwise, also when we simply aren't sure. */
1116 :
1117 : static bool
1118 1248293 : trivially_conflicts_p (basic_block bb, tree result, tree arg)
1119 : {
1120 1248293 : use_operand_p use;
1121 1248293 : imm_use_iterator imm_iter;
1122 1248293 : gimple *defa = SSA_NAME_DEF_STMT (arg);
1123 :
1124 : /* If ARG isn't defined in the same block it's too complicated for
1125 : our little mind. */
1126 1248293 : if (gimple_bb (defa) != bb)
1127 : return false;
1128 :
1129 1916023 : FOR_EACH_IMM_USE_FAST (use, imm_iter, result)
1130 : {
1131 1013671 : gimple *use_stmt = USE_STMT (use);
1132 1013671 : if (is_gimple_debug (use_stmt))
1133 130404 : continue;
1134 : /* Now, if there's a use of RESULT that lies outside this basic block,
1135 : then there surely is a conflict with ARG. */
1136 883267 : if (gimple_bb (use_stmt) != bb)
1137 : return true;
1138 833771 : if (gimple_code (use_stmt) == GIMPLE_PHI)
1139 835 : continue;
1140 : /* The use now is in a real stmt of BB, so if ARG was defined
1141 : in a PHI node (like RESULT) both conflict. */
1142 832936 : if (gimple_code (defa) == GIMPLE_PHI)
1143 : return true;
1144 831117 : maybe_renumber_stmts_bb (bb);
1145 : /* If the use of RESULT occurs after the definition of ARG,
1146 : the two conflict too. */
1147 831117 : if (gimple_uid (defa) < gimple_uid (use_stmt))
1148 : return true;
1149 66670 : }
1150 :
1151 417841 : return false;
1152 : }
1153 :
1154 :
1155 : /* Search every PHI node for arguments associated with backedges which
1156 : we can trivially determine will need a copy (the argument is either
1157 : not an SSA_NAME or the argument has a different underlying variable
1158 : than the PHI result).
1159 :
1160 : Insert a copy from the PHI argument to a new destination at the
1161 : end of the block with the backedge to the top of the loop. Update
1162 : the PHI argument to reference this new destination. */
1163 :
1164 : static void
1165 1472141 : insert_backedge_copies (void)
1166 : {
1167 1472141 : basic_block bb;
1168 1472141 : gphi_iterator gsi;
1169 :
1170 1472141 : mark_dfs_back_edges ();
1171 :
1172 14042323 : FOR_EACH_BB_FN (bb, cfun)
1173 : {
1174 : /* Mark block as possibly needing calculation of UIDs. */
1175 12570182 : bb->aux = &bb->aux;
1176 :
1177 18099568 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1178 : {
1179 5529386 : gphi *phi = gsi.phi ();
1180 5529386 : tree result = gimple_phi_result (phi);
1181 5529386 : size_t i;
1182 :
1183 11058772 : if (virtual_operand_p (result))
1184 2622310 : continue;
1185 :
1186 10034437 : for (i = 0; i < gimple_phi_num_args (phi); i++)
1187 : {
1188 7127361 : tree arg = gimple_phi_arg_def (phi, i);
1189 7127361 : edge e = gimple_phi_arg_edge (phi, i);
1190 : /* We are only interested in copies emitted on critical
1191 : backedges. */
1192 13150526 : if (!(e->flags & EDGE_DFS_BACK)
1193 7127361 : || !EDGE_CRITICAL_P (e))
1194 6023165 : continue;
1195 :
1196 : /* If the argument is not an SSA_NAME, then we will need a
1197 : constant initialization. If the argument is an SSA_NAME then
1198 : a copy statement may be needed. First handle the case
1199 : where we cannot insert before the argument definition. */
1200 1104196 : if (TREE_CODE (arg) != SSA_NAME
1201 1104196 : || (gimple_code (SSA_NAME_DEF_STMT (arg)) == GIMPLE_PHI
1202 190048 : && trivially_conflicts_p (bb, result, arg)))
1203 : {
1204 45951 : tree name;
1205 45951 : gassign *stmt;
1206 45951 : gimple *last = NULL;
1207 45951 : gimple_stmt_iterator gsi2;
1208 :
1209 45951 : gsi2 = gsi_last_bb (gimple_phi_arg_edge (phi, i)->src);
1210 45951 : if (!gsi_end_p (gsi2))
1211 45951 : last = gsi_stmt (gsi2);
1212 :
1213 : /* In theory the only way we ought to get back to the
1214 : start of a loop should be with a COND_EXPR or GOTO_EXPR.
1215 : However, better safe than sorry.
1216 : If the block ends with a control statement or
1217 : something that might throw, then we have to
1218 : insert this assignment before the last
1219 : statement. Else insert it after the last statement. */
1220 45951 : if (last && stmt_ends_bb_p (last))
1221 : {
1222 : /* If the last statement in the block is the definition
1223 : site of the PHI argument, then we can't insert
1224 : anything after it. */
1225 45951 : if (TREE_CODE (arg) == SSA_NAME
1226 45951 : && SSA_NAME_DEF_STMT (arg) == last)
1227 0 : continue;
1228 : }
1229 :
1230 : /* Create a new instance of the underlying variable of the
1231 : PHI result. */
1232 45951 : name = copy_ssa_name (result);
1233 45951 : stmt = gimple_build_assign (name,
1234 : gimple_phi_arg_def (phi, i));
1235 :
1236 : /* copy location if present. */
1237 45951 : if (gimple_phi_arg_has_location (phi, i))
1238 3764 : gimple_set_location (stmt,
1239 : gimple_phi_arg_location (phi, i));
1240 :
1241 : /* Insert the new statement into the block and update
1242 : the PHI node. */
1243 45951 : if (last && stmt_ends_bb_p (last))
1244 45951 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
1245 : else
1246 0 : gsi_insert_after (&gsi2, stmt, GSI_NEW_STMT);
1247 45951 : SET_PHI_ARG_DEF (phi, i, name);
1248 : }
1249 : /* Insert a copy before the definition of the backedge value
1250 : and adjust all conflicting uses. */
1251 1058245 : else if (trivially_conflicts_p (bb, result, arg))
1252 : {
1253 31339 : gimple *def = SSA_NAME_DEF_STMT (arg);
1254 31339 : if (gimple_nop_p (def)
1255 31339 : || gimple_code (def) == GIMPLE_PHI)
1256 0 : continue;
1257 31339 : imm_use_iterator imm_iter;
1258 31339 : gimple *use_stmt;
1259 31339 : auto_vec<use_operand_p, 8> uses;
1260 31339 : int idx = -1;
1261 : /* The following matches trivially_conflicts_p. */
1262 258405 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, result)
1263 : {
1264 195727 : if (gimple_bb (use_stmt) != bb
1265 195727 : || (gimple_code (use_stmt) != GIMPLE_PHI
1266 143645 : && (maybe_renumber_stmts_bb (bb), true)
1267 143645 : && gimple_uid (use_stmt) > gimple_uid (def)))
1268 : {
1269 116317 : use_operand_p use;
1270 233168 : FOR_EACH_IMM_USE_ON_STMT (use, imm_iter)
1271 : {
1272 116584 : uses.safe_push (use);
1273 116584 : if (!is_gimple_debug (use_stmt))
1274 : {
1275 53257 : if (idx == -1)
1276 62678 : idx = uses.length () - 1;
1277 : else
1278 : idx = -2;
1279 : }
1280 : }
1281 : }
1282 31339 : }
1283 : /* When there is just a conflicting statement try to
1284 : adjust that to refer to the new definition.
1285 : In particular for now handle a conflict with the
1286 : use in a (exit) condition with a NE compare,
1287 : replacing a pre-IV-increment compare with a
1288 : post-IV-increment one. */
1289 31339 : if (idx >= 0
1290 22692 : && is_a <gcond *> (USE_STMT (uses[idx]))
1291 7117 : && (gimple_cond_code (USE_STMT (uses[idx])) == NE_EXPR
1292 2568 : || gimple_cond_code (USE_STMT (uses[idx])) == EQ_EXPR)
1293 6065 : && is_gimple_assign (def)
1294 6011 : && gimple_assign_rhs1 (def) == result
1295 3344 : && (gimple_assign_rhs_code (def) == PLUS_EXPR
1296 34 : || gimple_assign_rhs_code (def) == MINUS_EXPR
1297 34 : || gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR)
1298 34654 : && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
1299 : {
1300 3315 : gcond *cond = as_a <gcond *> (USE_STMT (uses[idx]));
1301 3315 : tree *adj;
1302 3315 : if (gimple_cond_lhs (cond) == result)
1303 1204 : adj = gimple_cond_rhs_ptr (cond);
1304 : else
1305 2111 : adj = gimple_cond_lhs_ptr (cond);
1306 3315 : gimple_stmt_iterator gsi = gsi_for_stmt (cond);
1307 3315 : tree newval
1308 6630 : = gimple_build (&gsi, true, GSI_SAME_STMT,
1309 : UNKNOWN_LOCATION,
1310 : gimple_assign_rhs_code (def),
1311 3315 : TREE_TYPE (*adj),
1312 : *adj, gimple_assign_rhs2 (def));
1313 3315 : *adj = newval;
1314 3315 : SET_USE (uses[idx], arg);
1315 3315 : update_stmt (cond);
1316 : }
1317 : else
1318 : {
1319 28024 : tree name = copy_ssa_name (result);
1320 28024 : gimple *stmt = gimple_build_assign (name, result);
1321 28024 : gimple_stmt_iterator gsi = gsi_for_stmt (def);
1322 28024 : gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
1323 197313 : for (auto use : uses)
1324 113241 : SET_USE (use, name);
1325 : }
1326 31339 : }
1327 : }
1328 : }
1329 :
1330 : /* Unmark this block again. */
1331 12570182 : bb->aux = NULL;
1332 : }
1333 1472141 : }
1334 :
1335 : /* Remove indirect clobbers. */
1336 :
1337 : static void
1338 1472141 : remove_indirect_clobbers (void)
1339 : {
1340 1472141 : basic_block bb;
1341 :
1342 14042323 : FOR_EACH_BB_FN (bb, cfun)
1343 120626906 : for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1344 : {
1345 95486542 : gimple *stmt = gsi_stmt (gsi);
1346 95486542 : if (gimple_clobber_p (stmt))
1347 : {
1348 1404534 : tree lhs = gimple_assign_lhs (stmt);
1349 1518474 : if (TREE_CODE (lhs) == MEM_REF
1350 1404534 : && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
1351 : {
1352 113940 : unlink_stmt_vdef (stmt);
1353 113940 : gsi_remove (&gsi, true);
1354 113940 : release_defs (stmt);
1355 113940 : continue;
1356 : }
1357 : }
1358 95372602 : gsi_next (&gsi);
1359 : }
1360 1472141 : }
1361 :
1362 : /* Free all memory associated with going out of SSA form. SA is
1363 : the outof-SSA info object. */
1364 :
1365 : void
1366 1472140 : finish_out_of_ssa (struct ssaexpand *sa)
1367 : {
1368 1472140 : free (sa->partition_to_pseudo);
1369 1472140 : if (sa->values)
1370 660071 : BITMAP_FREE (sa->values);
1371 1472140 : delete_var_map (sa->map);
1372 1472140 : BITMAP_FREE (sa->partitions_for_parm_default_defs);
1373 1472140 : BITMAP_FREE (sa->partitions_for_undefined_values);
1374 1472140 : memset (sa, 0, sizeof *sa);
1375 1472140 : }
1376 :
1377 : /* Take the current function out of SSA form, translating PHIs as described in
1378 : R. Morgan, ``Building an Optimizing Compiler'',
1379 : Butterworth-Heinemann, Boston, MA, 1998. pp 176-186. */
1380 :
1381 : unsigned int
1382 1472141 : rewrite_out_of_ssa (struct ssaexpand *sa)
1383 : {
1384 : /* Remove remaining indirect clobbers as we do not need those anymore.
1385 : Those might extend SSA lifetime and restrict coalescing. */
1386 1472141 : remove_indirect_clobbers ();
1387 :
1388 : /* If elimination of a PHI requires inserting a copy on a backedge,
1389 : then we will have to split the backedge which has numerous
1390 : undesirable performance effects.
1391 :
1392 : A significant number of such cases can be handled here by inserting
1393 : copies into the loop itself. */
1394 1472141 : insert_backedge_copies ();
1395 :
1396 : /* Eliminate PHIs which are of no use, such as virtual or dead phis. */
1397 1472141 : eliminate_useless_phis ();
1398 :
1399 1472141 : if (dump_file && (dump_flags & TDF_DETAILS))
1400 115 : gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1401 :
1402 1472141 : remove_ssa_form (flag_tree_ter, sa);
1403 :
1404 1472141 : return 0;
1405 : }
|