Debian Codegraph @

gcc/tree-outof-ssa.cc

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