1 /* Miscellaneous SSA utility functions.
2    Copyright (C) 2001-2022 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "cfghooks.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "gimple-fold.h"
34 #include "gimplify.h"
35 #include "gimple-iterator.h"
36 #include "gimple-walk.h"
37 #include "tree-ssa-loop-manip.h"
38 #include "tree-into-ssa.h"
39 #include "tree-ssa.h"
40 #include "cfgloop.h"
41 #include "cfgexpand.h"
42 #include "tree-cfg.h"
43 #include "tree-dfa.h"
44 #include "stringpool.h"
45 #include "attribs.h"
46 #include "asan.h"
47 
48 /* Pointer map of variable mappings, keyed by edge.  */
49 static hash_map<edge, auto_vec<edge_var_map> > *edge_var_maps;
50 
51 
52 /* Add a mapping with PHI RESULT and PHI DEF associated with edge E.  */
53 
54 void
redirect_edge_var_map_add(edge e,tree result,tree def,location_t locus)55 redirect_edge_var_map_add (edge e, tree result, tree def, location_t locus)
56 {
57   edge_var_map new_node;
58 
59   if (edge_var_maps == NULL)
60     edge_var_maps = new hash_map<edge, auto_vec<edge_var_map> >;
61 
62   auto_vec<edge_var_map> &slot = edge_var_maps->get_or_insert (e);
63   new_node.def = def;
64   new_node.result = result;
65   new_node.locus = locus;
66 
67   slot.safe_push (new_node);
68 }
69 
70 
71 /* Clear the var mappings in edge E.  */
72 
73 void
redirect_edge_var_map_clear(edge e)74 redirect_edge_var_map_clear (edge e)
75 {
76   if (!edge_var_maps)
77     return;
78 
79   auto_vec<edge_var_map> *head = edge_var_maps->get (e);
80 
81   if (head)
82     head->release ();
83 }
84 
85 
86 /* Duplicate the redirected var mappings in OLDE in NEWE.
87 
88    This assumes a hash_map can have multiple edges mapping to the same
89    var_map (many to one mapping), since we don't remove the previous mappings.
90    */
91 
92 void
redirect_edge_var_map_dup(edge newe,edge olde)93 redirect_edge_var_map_dup (edge newe, edge olde)
94 {
95   if (!edge_var_maps)
96     return;
97 
98   auto_vec<edge_var_map> *new_head = &edge_var_maps->get_or_insert (newe);
99   auto_vec<edge_var_map> *old_head = edge_var_maps->get (olde);
100   if (!old_head)
101     return;
102 
103   new_head->safe_splice (*old_head);
104 }
105 
106 
107 /* Return the variable mappings for a given edge.  If there is none, return
108    NULL.  */
109 
110 vec<edge_var_map> *
redirect_edge_var_map_vector(edge e)111 redirect_edge_var_map_vector (edge e)
112 {
113   /* Hey, what kind of idiot would... you'd be surprised.  */
114   if (!edge_var_maps)
115     return NULL;
116 
117   auto_vec<edge_var_map> *slot = edge_var_maps->get (e);
118   if (!slot)
119     return NULL;
120 
121   return slot;
122 }
123 
124 /* Clear the edge variable mappings.  */
125 
126 void
redirect_edge_var_map_empty(void)127 redirect_edge_var_map_empty (void)
128 {
129   if (edge_var_maps)
130     edge_var_maps->empty ();
131 }
132 
133 
134 /* Remove the corresponding arguments from the PHI nodes in E's
135    destination block and redirect it to DEST.  Return redirected edge.
136    The list of removed arguments is stored in a vector accessed
137    through edge_var_maps.  */
138 
139 edge
ssa_redirect_edge(edge e,basic_block dest)140 ssa_redirect_edge (edge e, basic_block dest)
141 {
142   gphi_iterator gsi;
143   gphi *phi;
144 
145   redirect_edge_var_map_clear (e);
146 
147   /* Remove the appropriate PHI arguments in E's destination block.
148      If we are redirecting a copied edge the destination has not
149      got PHI argument space reserved nor an interesting argument.  */
150   if (! (e->dest->flags & BB_DUPLICATED))
151     for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
152       {
153           tree def;
154           location_t locus;
155 
156           phi = gsi.phi ();
157           def = gimple_phi_arg_def (phi, e->dest_idx);
158           locus = gimple_phi_arg_location (phi, e->dest_idx);
159 
160           if (def == NULL_TREE)
161             continue;
162 
163           redirect_edge_var_map_add (e, gimple_phi_result (phi), def, locus);
164       }
165 
166   e = redirect_edge_succ_nodup (e, dest);
167 
168   return e;
169 }
170 
171 
172 /* Add PHI arguments queued in PENDING_STMT list on edge E to edge
173    E->dest.  */
174 
175 void
flush_pending_stmts(edge e)176 flush_pending_stmts (edge e)
177 {
178   gphi *phi;
179   edge_var_map *vm;
180   int i;
181   gphi_iterator gsi;
182 
183   vec<edge_var_map> *v = redirect_edge_var_map_vector (e);
184   if (!v)
185     return;
186 
187   for (gsi = gsi_start_phis (e->dest), i = 0;
188        !gsi_end_p (gsi) && v->iterate (i, &vm);
189        gsi_next (&gsi), i++)
190     {
191       tree def;
192 
193       phi = gsi.phi ();
194       def = redirect_edge_var_map_def (vm);
195       add_phi_arg (phi, def, e, redirect_edge_var_map_location (vm));
196     }
197 
198   redirect_edge_var_map_clear (e);
199 }
200 
201 /* Replace the LHS of STMT, an assignment, either a GIMPLE_ASSIGN or a
202    GIMPLE_CALL, with NLHS, in preparation for modifying the RHS to an
203    expression with a different value.
204 
205    This will update any annotations (say debug bind stmts) referring
206    to the original LHS, so that they use the RHS instead.  This is
207    done even if NLHS and LHS are the same, for it is understood that
208    the RHS will be modified afterwards, and NLHS will not be assigned
209    an equivalent value.
210 
211    Adjusting any non-annotation uses of the LHS, if needed, is a
212    responsibility of the caller.
213 
214    The effect of this call should be pretty much the same as that of
215    inserting a copy of STMT before STMT, and then removing the
216    original stmt, at which time gsi_remove() would have update
217    annotations, but using this function saves all the inserting,
218    copying and removing.  */
219 
220 void
gimple_replace_ssa_lhs(gimple * stmt,tree nlhs)221 gimple_replace_ssa_lhs (gimple *stmt, tree nlhs)
222 {
223   if (MAY_HAVE_DEBUG_BIND_STMTS)
224     {
225       tree lhs = gimple_get_lhs (stmt);
226 
227       gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
228 
229       insert_debug_temp_for_var_def (NULL, lhs);
230     }
231 
232   gimple_set_lhs (stmt, nlhs);
233 }
234 
235 
236 /* Given a tree for an expression for which we might want to emit
237    locations or values in debug information (generally a variable, but
238    we might deal with other kinds of trees in the future), return the
239    tree that should be used as the variable of a DEBUG_BIND STMT or
240    VAR_LOCATION INSN or NOTE.  Return NULL if VAR is not to be tracked.  */
241 
242 tree
target_for_debug_bind(tree var)243 target_for_debug_bind (tree var)
244 {
245   if (!MAY_HAVE_DEBUG_BIND_STMTS)
246     return NULL_TREE;
247 
248   if (TREE_CODE (var) == SSA_NAME)
249     {
250       var = SSA_NAME_VAR (var);
251       if (var == NULL_TREE)
252           return NULL_TREE;
253     }
254 
255   if ((!VAR_P (var) || VAR_DECL_IS_VIRTUAL_OPERAND (var))
256       && TREE_CODE (var) != PARM_DECL)
257     return NULL_TREE;
258 
259   if (DECL_HAS_VALUE_EXPR_P (var))
260     return target_for_debug_bind (DECL_VALUE_EXPR (var));
261 
262   if (DECL_IGNORED_P (var))
263     return NULL_TREE;
264 
265   /* var-tracking only tracks registers.  */
266   if (!is_gimple_reg_type (TREE_TYPE (var)))
267     return NULL_TREE;
268 
269   return var;
270 }
271 
272 /* Called via walk_tree, look for SSA_NAMEs that have already been
273    released.  */
274 
275 tree
find_released_ssa_name(tree * tp,int * walk_subtrees,void * data_)276 find_released_ssa_name (tree *tp, int *walk_subtrees, void *data_)
277 {
278   struct walk_stmt_info *wi = (struct walk_stmt_info *) data_;
279 
280   if (wi && wi->is_lhs)
281     return NULL_TREE;
282 
283   if (TREE_CODE (*tp) == SSA_NAME)
284     {
285       if (SSA_NAME_IN_FREE_LIST (*tp))
286           return *tp;
287 
288       *walk_subtrees = 0;
289     }
290   else if (IS_TYPE_OR_DECL_P (*tp))
291     *walk_subtrees = 0;
292 
293   return NULL_TREE;
294 }
295 
296 /* Insert a DEBUG BIND stmt before the DEF of VAR if VAR is referenced
297    by other DEBUG stmts, and replace uses of the DEF with the
298    newly-created debug temp.  */
299 
300 void
insert_debug_temp_for_var_def(gimple_stmt_iterator * gsi,tree var)301 insert_debug_temp_for_var_def (gimple_stmt_iterator *gsi, tree var)
302 {
303   imm_use_iterator imm_iter;
304   use_operand_p use_p;
305   gimple *stmt;
306   gimple *def_stmt = NULL;
307   int usecount = 0;
308   tree value = NULL;
309 
310   if (!MAY_HAVE_DEBUG_BIND_STMTS)
311     return;
312 
313   /* If this name has already been registered for replacement, do nothing
314      as anything that uses this name isn't in SSA form.  */
315   if (name_registered_for_update_p (var))
316     return;
317 
318   /* Check whether there are debug stmts that reference this variable and,
319      if there are, decide whether we should use a debug temp.  */
320   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
321     {
322       stmt = USE_STMT (use_p);
323 
324       if (!gimple_debug_bind_p (stmt))
325           continue;
326 
327       if (usecount++)
328           break;
329 
330       if (gimple_debug_bind_get_value (stmt) != var)
331           {
332             /* Count this as an additional use, so as to make sure we
333                use a temp unless VAR's definition has a SINGLE_RHS that
334                can be shared.  */
335             usecount++;
336             break;
337           }
338     }
339 
340   if (!usecount)
341     return;
342 
343   if (gsi)
344     def_stmt = gsi_stmt (*gsi);
345   else
346     def_stmt = SSA_NAME_DEF_STMT (var);
347 
348   /* If we didn't get an insertion point, and the stmt has already
349      been removed, we won't be able to insert the debug bind stmt, so
350      we'll have to drop debug information.  */
351   if (gimple_code (def_stmt) == GIMPLE_PHI)
352     {
353       value = degenerate_phi_result (as_a <gphi *> (def_stmt));
354       if (value && walk_tree (&value, find_released_ssa_name, NULL, NULL))
355           value = NULL;
356       /* error_mark_node is what fixup_noreturn_call changes PHI arguments
357            to.  */
358       else if (value == error_mark_node)
359           value = NULL;
360     }
361   else if (gimple_clobber_p (def_stmt))
362     /* We can end up here when rewriting a decl into SSA and coming
363        along a clobber for the original decl.  Turn that into
364        # DEBUG decl => NULL  */
365     value = NULL;
366   else if (is_gimple_assign (def_stmt))
367     {
368       bool no_value = false;
369 
370       if (!dom_info_available_p (CDI_DOMINATORS))
371           {
372             struct walk_stmt_info wi;
373 
374             memset (&wi, 0, sizeof (wi));
375 
376             /* When removing blocks without following reverse dominance
377                order, we may sometimes encounter SSA_NAMEs that have
378                already been released, referenced in other SSA_DEFs that
379                we're about to release.  Consider:
380 
381                <bb X>:
382                v_1 = foo;
383 
384                <bb Y>:
385                w_2 = v_1 + bar;
386                # DEBUG w => w_2
387 
388                If we deleted BB X first, propagating the value of w_2
389                won't do us any good.  It's too late to recover their
390                original definition of v_1: when it was deleted, it was
391                only referenced in other DEFs, it couldn't possibly know
392                it should have been retained, and propagating every
393                single DEF just in case it might have to be propagated
394                into a DEBUG STMT would probably be too wasteful.
395 
396                When dominator information is not readily available, we
397                check for and accept some loss of debug information.  But
398                if it is available, there's no excuse for us to remove
399                blocks in the wrong order, so we don't even check for
400                dead SSA NAMEs.  SSA verification shall catch any
401                errors.  */
402             if ((!gsi && !gimple_bb (def_stmt))
403                 || walk_gimple_op (def_stmt, find_released_ssa_name, &wi))
404               no_value = true;
405           }
406 
407       if (!no_value)
408           value = gimple_assign_rhs_to_tree (def_stmt);
409     }
410 
411   if (value)
412     {
413       /* If there's a single use of VAR, and VAR is the entire debug
414            expression (usecount would have been incremented again
415            otherwise), and the definition involves only constants and
416            SSA names, then we can propagate VALUE into this single use,
417            avoiding the temp.
418 
419            We can also avoid using a temp if VALUE can be shared and
420            propagated into all uses, without generating expressions that
421            wouldn't be valid gimple RHSs.
422 
423            Other cases that would require unsharing or non-gimple RHSs
424            are deferred to a debug temp, although we could avoid temps
425            at the expense of duplication of expressions.  */
426 
427       if (CONSTANT_CLASS_P (value)
428             || gimple_code (def_stmt) == GIMPLE_PHI
429             || (usecount == 1
430                 && (!gimple_assign_single_p (def_stmt)
431                       || is_gimple_min_invariant (value)))
432             || is_gimple_reg (value))
433           ;
434       else
435           {
436             gdebug *def_temp;
437             tree vexpr = build_debug_expr_decl (TREE_TYPE (value));
438 
439             def_temp = gimple_build_debug_bind (vexpr,
440                                                         unshare_expr (value),
441                                                         def_stmt);
442 
443             /* FIXME: Is setting the mode really necessary? */
444             if (DECL_P (value))
445               SET_DECL_MODE (vexpr, DECL_MODE (value));
446             else
447               SET_DECL_MODE (vexpr, TYPE_MODE (TREE_TYPE (value)));
448 
449             if (gsi)
450               gsi_insert_before (gsi, def_temp, GSI_SAME_STMT);
451             else
452               {
453                 gimple_stmt_iterator ngsi = gsi_for_stmt (def_stmt);
454                 gsi_insert_before (&ngsi, def_temp, GSI_SAME_STMT);
455               }
456 
457             value = vexpr;
458           }
459     }
460 
461   FOR_EACH_IMM_USE_STMT (stmt, imm_iter, var)
462     {
463       if (!gimple_debug_bind_p (stmt))
464           continue;
465 
466       if (value)
467           {
468             FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
469               /* unshare_expr is not needed here.  vexpr is either a
470                  SINGLE_RHS, that can be safely shared, some other RHS
471                  that was unshared when we found it had a single debug
472                  use, or a DEBUG_EXPR_DECL, that can be safely
473                  shared.  */
474               SET_USE (use_p, unshare_expr (value));
475             /* If we didn't replace uses with a debug decl fold the
476                resulting expression.  Otherwise we end up with invalid IL.  */
477             if (TREE_CODE (value) != DEBUG_EXPR_DECL)
478               {
479                 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
480                 fold_stmt_inplace (&gsi);
481               }
482           }
483       else
484           gimple_debug_bind_reset_value (stmt);
485 
486       update_stmt (stmt);
487     }
488 }
489 
490 
491 /* Insert a DEBUG BIND stmt before STMT for each DEF referenced by
492    other DEBUG stmts, and replace uses of the DEF with the
493    newly-created debug temp.  */
494 
495 void
insert_debug_temps_for_defs(gimple_stmt_iterator * gsi)496 insert_debug_temps_for_defs (gimple_stmt_iterator *gsi)
497 {
498   gimple *stmt;
499   ssa_op_iter op_iter;
500   def_operand_p def_p;
501 
502   if (!MAY_HAVE_DEBUG_BIND_STMTS)
503     return;
504 
505   stmt = gsi_stmt (*gsi);
506 
507   FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
508     {
509       tree var = DEF_FROM_PTR (def_p);
510 
511       if (TREE_CODE (var) != SSA_NAME)
512           continue;
513 
514       insert_debug_temp_for_var_def (gsi, var);
515     }
516 }
517 
518 /* Reset all debug stmts that use SSA_NAME(s) defined in STMT.  */
519 
520 void
reset_debug_uses(gimple * stmt)521 reset_debug_uses (gimple *stmt)
522 {
523   ssa_op_iter op_iter;
524   def_operand_p def_p;
525   imm_use_iterator imm_iter;
526   gimple *use_stmt;
527 
528   if (!MAY_HAVE_DEBUG_BIND_STMTS)
529     return;
530 
531   FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
532     {
533       tree var = DEF_FROM_PTR (def_p);
534 
535       if (TREE_CODE (var) != SSA_NAME)
536           continue;
537 
538       FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, var)
539           {
540             if (!gimple_debug_bind_p (use_stmt))
541               continue;
542 
543             gimple_debug_bind_reset_value (use_stmt);
544             update_stmt (use_stmt);
545           }
546     }
547 }
548 
549 /* Delete SSA DEFs for SSA versions in the TOREMOVE bitmap, removing
550    dominated stmts before their dominators, so that release_ssa_defs
551    stands a chance of propagating DEFs into debug bind stmts.  */
552 
553 void
release_defs_bitset(bitmap toremove)554 release_defs_bitset (bitmap toremove)
555 {
556   unsigned j;
557   bitmap_iterator bi;
558 
559   /* Performing a topological sort is probably overkill, this will
560      most likely run in slightly superlinear time, rather than the
561      pathological quadratic worst case.
562      But iterate from max SSA name version to min one because
563      that mimics allocation order during code generation behavior best.
564      Use an array for this which we compact on-the-fly with a NULL
565      marker moving towards the end of the vector.  */
566   auto_vec<tree, 16> names;
567   names.reserve (bitmap_count_bits (toremove) + 1);
568   names.quick_push (NULL_TREE);
569   EXECUTE_IF_SET_IN_BITMAP (toremove, 0, j, bi)
570     names.quick_push (ssa_name (j));
571 
572   bitmap_tree_view (toremove);
573   while (!bitmap_empty_p (toremove))
574     {
575       j = names.length () - 1;
576       for (unsigned i = names.length () - 1; names[i];)
577           {
578             bool remove_now = true;
579             tree var = names[i];
580             gimple *stmt;
581             imm_use_iterator uit;
582 
583             FOR_EACH_IMM_USE_STMT (stmt, uit, var)
584               {
585                 ssa_op_iter dit;
586                 def_operand_p def_p;
587 
588                 /* We can't propagate PHI nodes into debug stmts.  */
589                 if (gimple_code (stmt) == GIMPLE_PHI
590                       || is_gimple_debug (stmt))
591                     continue;
592 
593                 /* If we find another definition to remove that uses
594                      the one we're looking at, defer the removal of this
595                      one, so that it can be propagated into debug stmts
596                      after the other is.  */
597                 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, dit, SSA_OP_DEF)
598                     {
599                       tree odef = DEF_FROM_PTR (def_p);
600 
601                       if (bitmap_bit_p (toremove, SSA_NAME_VERSION (odef)))
602                         {
603                           remove_now = false;
604                           break;
605                         }
606                     }
607 
608                 if (!remove_now)
609                     break;
610               }
611 
612             if (remove_now)
613               {
614                 gimple *def = SSA_NAME_DEF_STMT (var);
615                 gimple_stmt_iterator gsi = gsi_for_stmt (def);
616 
617                 if (gimple_code (def) == GIMPLE_PHI)
618                     remove_phi_node (&gsi, true);
619                 else
620                     {
621                       gsi_remove (&gsi, true);
622                       release_defs (def);
623                     }
624                 bitmap_clear_bit (toremove, SSA_NAME_VERSION (var));
625               }
626             else
627               --i;
628             if (--j != i)
629               names[i] = names[j];
630           }
631     }
632   bitmap_list_view (toremove);
633 }
634 
635 /* Disable warnings about missing quoting in GCC diagnostics for
636    the verification errors.  Their format strings don't follow GCC
637    diagnostic conventions and the calls are ultimately followed by
638    one to internal_error.  */
639 #if __GNUC__ >= 10
640 #  pragma GCC diagnostic push
641 #  pragma GCC diagnostic ignored "-Wformat-diag"
642 #endif
643 
644 /* Verify virtual SSA form.  */
645 
646 bool
verify_vssa(basic_block bb,tree current_vdef,sbitmap visited)647 verify_vssa (basic_block bb, tree current_vdef, sbitmap visited)
648 {
649   bool err = false;
650 
651   if (!bitmap_set_bit (visited, bb->index))
652     return false;
653 
654   /* Pick up the single virtual PHI def.  */
655   gphi *phi = NULL;
656   for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
657        gsi_next (&si))
658     {
659       tree res = gimple_phi_result (si.phi ());
660       if (virtual_operand_p (res))
661           {
662             if (phi)
663               {
664                 error ("multiple virtual PHI nodes in BB %d", bb->index);
665                 print_gimple_stmt (stderr, phi, 0);
666                 print_gimple_stmt (stderr, si.phi (), 0);
667                 err = true;
668               }
669             else
670               phi = si.phi ();
671           }
672     }
673   if (phi)
674     {
675       current_vdef = gimple_phi_result (phi);
676       if (TREE_CODE (current_vdef) != SSA_NAME)
677           {
678             error ("virtual definition is not an SSA name");
679             print_gimple_stmt (stderr, phi, 0);
680             err = true;
681           }
682     }
683 
684   /* Verify stmts.  */
685   for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
686        gsi_next (&gsi))
687     {
688       gimple *stmt = gsi_stmt (gsi);
689       tree vuse = gimple_vuse (stmt);
690       if (vuse)
691           {
692             if (vuse != current_vdef)
693               {
694                 error ("stmt with wrong VUSE");
695                 print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
696                 fprintf (stderr, "expected ");
697                 print_generic_expr (stderr, current_vdef);
698                 fprintf (stderr, "\n");
699                 err = true;
700               }
701             tree vdef = gimple_vdef (stmt);
702             if (vdef)
703               {
704                 current_vdef = vdef;
705                 if (TREE_CODE (current_vdef) != SSA_NAME)
706                     {
707                       error ("virtual definition is not an SSA name");
708                       print_gimple_stmt (stderr, phi, 0);
709                       err = true;
710                     }
711               }
712           }
713     }
714 
715   /* Verify destination PHI uses and recurse.  */
716   edge_iterator ei;
717   edge e;
718   FOR_EACH_EDGE (e, ei, bb->succs)
719     {
720       gphi *phi = get_virtual_phi (e->dest);
721       if (phi
722             && PHI_ARG_DEF_FROM_EDGE (phi, e) != current_vdef)
723           {
724             error ("PHI node with wrong VUSE on edge from BB %d",
725                      e->src->index);
726             print_gimple_stmt (stderr, phi, 0, TDF_VOPS);
727             fprintf (stderr, "expected ");
728             print_generic_expr (stderr, current_vdef);
729             fprintf (stderr, "\n");
730             err = true;
731           }
732 
733       /* Recurse.  */
734       err |= verify_vssa (e->dest, current_vdef, visited);
735     }
736 
737   return err;
738 }
739 
740 /* Return true if SSA_NAME is malformed and mark it visited.
741 
742    IS_VIRTUAL is true if this SSA_NAME was found inside a virtual
743       operand.  */
744 
745 static bool
verify_ssa_name(tree ssa_name,bool is_virtual)746 verify_ssa_name (tree ssa_name, bool is_virtual)
747 {
748   if (TREE_CODE (ssa_name) != SSA_NAME)
749     {
750       error ("expected an SSA_NAME object");
751       return true;
752     }
753 
754   if (SSA_NAME_IN_FREE_LIST (ssa_name))
755     {
756       error ("found an SSA_NAME that had been released into the free pool");
757       return true;
758     }
759 
760   if (SSA_NAME_VAR (ssa_name) != NULL_TREE
761       && TREE_TYPE (ssa_name) != TREE_TYPE (SSA_NAME_VAR (ssa_name)))
762     {
763       error ("type mismatch between an SSA_NAME and its symbol");
764       return true;
765     }
766 
767   if (is_virtual && !virtual_operand_p (ssa_name))
768     {
769       error ("found a virtual definition for a GIMPLE register");
770       return true;
771     }
772 
773   if (is_virtual && SSA_NAME_VAR (ssa_name) != gimple_vop (cfun))
774     {
775       error ("virtual SSA name for non-VOP decl");
776       return true;
777     }
778 
779   if (!is_virtual && virtual_operand_p (ssa_name))
780     {
781       error ("found a real definition for a non-register");
782       return true;
783     }
784 
785   if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
786       && !gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name)))
787     {
788       error ("found a default name with a non-empty defining statement");
789       return true;
790     }
791 
792   return false;
793 }
794 
795 
796 /* Return true if the definition of SSA_NAME at block BB is malformed.
797 
798    STMT is the statement where SSA_NAME is created.
799 
800    DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
801       version numbers.  If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
802       it means that the block in that array slot contains the
803       definition of SSA_NAME.
804 
805    IS_VIRTUAL is true if SSA_NAME is created by a VDEF.  */
806 
807 static bool
verify_def(basic_block bb,basic_block * definition_block,tree ssa_name,gimple * stmt,bool is_virtual)808 verify_def (basic_block bb, basic_block *definition_block, tree ssa_name,
809               gimple *stmt, bool is_virtual)
810 {
811   if (verify_ssa_name (ssa_name, is_virtual))
812     goto err;
813 
814   if (SSA_NAME_VAR (ssa_name)
815       && TREE_CODE (SSA_NAME_VAR (ssa_name)) == RESULT_DECL
816       && DECL_BY_REFERENCE (SSA_NAME_VAR (ssa_name)))
817     {
818       error ("RESULT_DECL should be read only when DECL_BY_REFERENCE is set");
819       goto err;
820     }
821 
822   if (definition_block[SSA_NAME_VERSION (ssa_name)])
823     {
824       error ("SSA_NAME created in two different blocks %i and %i",
825                definition_block[SSA_NAME_VERSION (ssa_name)]->index, bb->index);
826       goto err;
827     }
828 
829   definition_block[SSA_NAME_VERSION (ssa_name)] = bb;
830 
831   if (SSA_NAME_DEF_STMT (ssa_name) != stmt)
832     {
833       error ("SSA_NAME_DEF_STMT is wrong");
834       fprintf (stderr, "Expected definition statement:\n");
835       print_gimple_stmt (stderr, SSA_NAME_DEF_STMT (ssa_name), 4, TDF_VOPS);
836       fprintf (stderr, "\nActual definition statement:\n");
837       print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
838       goto err;
839     }
840 
841   return false;
842 
843 err:
844   fprintf (stderr, "while verifying SSA_NAME ");
845   print_generic_expr (stderr, ssa_name);
846   fprintf (stderr, " in statement\n");
847   print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
848 
849   return true;
850 }
851 
852 
853 /* Return true if the use of SSA_NAME at statement STMT in block BB is
854    malformed.
855 
856    DEF_BB is the block where SSA_NAME was found to be created.
857 
858    IDOM contains immediate dominator information for the flowgraph.
859 
860    CHECK_ABNORMAL is true if the caller wants to check whether this use
861       is flowing through an abnormal edge (only used when checking PHI
862       arguments).
863 
864    If NAMES_DEFINED_IN_BB is not NULL, it contains a bitmap of ssa names
865      that are defined before STMT in basic block BB.  */
866 
867 static bool
verify_use(basic_block bb,basic_block def_bb,use_operand_p use_p,gimple * stmt,bool check_abnormal,bitmap names_defined_in_bb)868 verify_use (basic_block bb, basic_block def_bb, use_operand_p use_p,
869               gimple *stmt, bool check_abnormal, bitmap names_defined_in_bb)
870 {
871   bool err = false;
872   tree ssa_name = USE_FROM_PTR (use_p);
873 
874   if (!TREE_VISITED (ssa_name))
875     if (verify_imm_links (stderr, ssa_name))
876       err = true;
877 
878   TREE_VISITED (ssa_name) = 1;
879 
880   if (gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name))
881       && SSA_NAME_IS_DEFAULT_DEF (ssa_name))
882     ; /* Default definitions have empty statements.  Nothing to do.  */
883   else if (!def_bb)
884     {
885       error ("missing definition");
886       err = true;
887     }
888   else if (bb != def_bb
889              && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
890     {
891       error ("definition in block %i does not dominate use in block %i",
892                def_bb->index, bb->index);
893       err = true;
894     }
895   else if (bb == def_bb
896              && names_defined_in_bb != NULL
897              && !bitmap_bit_p (names_defined_in_bb, SSA_NAME_VERSION (ssa_name)))
898     {
899       error ("definition in block %i follows the use", def_bb->index);
900       err = true;
901     }
902 
903   if (check_abnormal
904       && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
905     {
906       error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set");
907       err = true;
908     }
909 
910   /* Make sure the use is in an appropriate list by checking the previous
911      element to make sure it's the same.  */
912   if (use_p->prev == NULL)
913     {
914       error ("no immediate_use list");
915       err = true;
916     }
917   else
918     {
919       tree listvar;
920       if (use_p->prev->use == NULL)
921           listvar = use_p->prev->loc.ssa_name;
922       else
923           listvar = USE_FROM_PTR (use_p->prev);
924       if (listvar != ssa_name)
925         {
926             error ("wrong immediate use list");
927             err = true;
928           }
929     }
930 
931   if (err)
932     {
933       fprintf (stderr, "for SSA_NAME: ");
934       print_generic_expr (stderr, ssa_name, TDF_VOPS);
935       fprintf (stderr, " in statement:\n");
936       print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
937     }
938 
939   return err;
940 }
941 
942 
943 /* Return true if any of the arguments for PHI node PHI at block BB is
944    malformed.
945 
946    DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
947       version numbers.  If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
948       it means that the block in that array slot contains the
949       definition of SSA_NAME.  */
950 
951 static bool
verify_phi_args(gphi * phi,basic_block bb,basic_block * definition_block)952 verify_phi_args (gphi *phi, basic_block bb, basic_block *definition_block)
953 {
954   edge e;
955   bool err = false;
956   size_t i, phi_num_args = gimple_phi_num_args (phi);
957 
958   if (EDGE_COUNT (bb->preds) != phi_num_args)
959     {
960       error ("incoming edge count does not match number of PHI arguments");
961       err = true;
962       goto error;
963     }
964 
965   for (i = 0; i < phi_num_args; i++)
966     {
967       use_operand_p op_p = gimple_phi_arg_imm_use_ptr (phi, i);
968       tree op = USE_FROM_PTR (op_p);
969 
970       e = EDGE_PRED (bb, i);
971 
972       if (op == NULL_TREE)
973           {
974             error ("PHI argument is missing for edge %d->%d",
975                    e->src->index,
976                      e->dest->index);
977             err = true;
978             goto error;
979           }
980 
981       if (TREE_CODE (op) != SSA_NAME && !is_gimple_min_invariant (op))
982           {
983             error ("PHI argument is not SSA_NAME, or invariant");
984             err = true;
985           }
986 
987       if ((e->flags & EDGE_ABNORMAL) && TREE_CODE (op) != SSA_NAME)
988           {
989             error ("PHI argument on abnormal edge is not SSA_NAME");
990             err = true;
991           }
992 
993       if (TREE_CODE (op) == SSA_NAME)
994           {
995             err = verify_ssa_name (op, virtual_operand_p (gimple_phi_result (phi)));
996             err |= verify_use (e->src, definition_block[SSA_NAME_VERSION (op)],
997                                    op_p, phi, e->flags & EDGE_ABNORMAL, NULL);
998           }
999 
1000       if (TREE_CODE (op) == ADDR_EXPR)
1001           {
1002             tree base = TREE_OPERAND (op, 0);
1003             while (handled_component_p (base))
1004               base = TREE_OPERAND (base, 0);
1005             if ((VAR_P (base)
1006                  || TREE_CODE (base) == PARM_DECL
1007                  || TREE_CODE (base) == RESULT_DECL)
1008                 && !TREE_ADDRESSABLE (base))
1009               {
1010                 error ("address taken, but ADDRESSABLE bit not set");
1011                 err = true;
1012               }
1013           }
1014 
1015       if (e->dest != bb)
1016           {
1017             error ("wrong edge %d->%d for PHI argument",
1018                    e->src->index, e->dest->index);
1019             err = true;
1020           }
1021 
1022       if (err)
1023           {
1024             fprintf (stderr, "PHI argument\n");
1025             print_generic_stmt (stderr, op, TDF_VOPS);
1026             goto error;
1027           }
1028     }
1029 
1030 error:
1031   if (err)
1032     {
1033       fprintf (stderr, "for PHI node\n");
1034       print_gimple_stmt (stderr, phi, 0, TDF_VOPS|TDF_MEMSYMS);
1035     }
1036 
1037 
1038   return err;
1039 }
1040 
1041 
1042 /* Verify common invariants in the SSA web.
1043    TODO: verify the variable annotations.  */
1044 
1045 DEBUG_FUNCTION void
verify_ssa(bool check_modified_stmt,bool check_ssa_operands)1046 verify_ssa (bool check_modified_stmt, bool check_ssa_operands)
1047 {
1048   basic_block bb;
1049   basic_block *definition_block = XCNEWVEC (basic_block, num_ssa_names);
1050   ssa_op_iter iter;
1051   tree op;
1052   enum dom_state orig_dom_state = dom_info_state (CDI_DOMINATORS);
1053   auto_bitmap names_defined_in_bb;
1054 
1055   gcc_assert (!need_ssa_update_p (cfun));
1056 
1057   timevar_push (TV_TREE_SSA_VERIFY);
1058 
1059     {
1060       /* Keep track of SSA names present in the IL.  */
1061       size_t i;
1062       tree name;
1063       hash_map <void *, tree> ssa_info;
1064 
1065       FOR_EACH_SSA_NAME (i, name, cfun)
1066           {
1067             gimple *stmt;
1068             TREE_VISITED (name) = 0;
1069 
1070             verify_ssa_name (name, virtual_operand_p (name));
1071 
1072             stmt = SSA_NAME_DEF_STMT (name);
1073             if (!gimple_nop_p (stmt))
1074               {
1075                 basic_block bb = gimple_bb (stmt);
1076                 if (verify_def (bb, definition_block,
1077                                     name, stmt, virtual_operand_p (name)))
1078                     goto err;
1079               }
1080 
1081             void *info = NULL;
1082             if (POINTER_TYPE_P (TREE_TYPE (name)))
1083               info = SSA_NAME_PTR_INFO (name);
1084             else if (INTEGRAL_TYPE_P (TREE_TYPE (name)))
1085               info = SSA_NAME_RANGE_INFO (name);
1086             if (info)
1087               {
1088                 bool existed;
1089                 tree &val = ssa_info.get_or_insert (info, &existed);
1090                 if (existed)
1091                     {
1092                       error ("shared SSA name info");
1093                       print_generic_expr (stderr, val);
1094                       fprintf (stderr, " and ");
1095                       print_generic_expr (stderr, name);
1096                       fprintf (stderr, "\n");
1097                       goto err;
1098                     }
1099                 else
1100                     val = name;
1101               }
1102           }
1103     }
1104 
1105   calculate_dominance_info (CDI_DOMINATORS);
1106 
1107   /* Now verify all the uses and make sure they agree with the definitions
1108      found in the previous pass.  */
1109   FOR_EACH_BB_FN (bb, cfun)
1110     {
1111       edge e;
1112       edge_iterator ei;
1113 
1114       /* Make sure that all edges have a clear 'aux' field.  */
1115       FOR_EACH_EDGE (e, ei, bb->preds)
1116           {
1117             if (e->aux)
1118               {
1119                 error ("AUX pointer initialized for edge %d->%d", e->src->index,
1120                           e->dest->index);
1121                 goto err;
1122               }
1123           }
1124 
1125       /* Verify the arguments for every PHI node in the block.  */
1126       for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1127           {
1128             gphi *phi = gsi.phi ();
1129             if (verify_phi_args (phi, bb, definition_block))
1130               goto err;
1131 
1132             bitmap_set_bit (names_defined_in_bb,
1133                                 SSA_NAME_VERSION (gimple_phi_result (phi)));
1134           }
1135 
1136       /* Now verify all the uses and vuses in every statement of the block.  */
1137       for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1138              gsi_next (&gsi))
1139           {
1140             gimple *stmt = gsi_stmt (gsi);
1141             use_operand_p use_p;
1142 
1143             if (check_modified_stmt && gimple_modified_p (stmt))
1144               {
1145                 error ("stmt (%p) marked modified after optimization pass: ",
1146                          (void *)stmt);
1147                 print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
1148                 goto err;
1149               }
1150 
1151             if (check_ssa_operands && verify_ssa_operands (cfun, stmt))
1152               {
1153                 print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
1154                 goto err;
1155               }
1156 
1157             if (gimple_debug_bind_p (stmt)
1158                 && !gimple_debug_bind_has_value_p (stmt))
1159               continue;
1160 
1161             FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE|SSA_OP_VUSE)
1162               {
1163                 op = USE_FROM_PTR (use_p);
1164                 if (verify_use (bb, definition_block[SSA_NAME_VERSION (op)],
1165                                     use_p, stmt, false, names_defined_in_bb))
1166                     goto err;
1167               }
1168 
1169             FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_ALL_DEFS)
1170               {
1171                 if (SSA_NAME_DEF_STMT (op) != stmt)
1172                     {
1173                       error ("SSA_NAME_DEF_STMT is wrong");
1174                       fprintf (stderr, "Expected definition statement:\n");
1175                       print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
1176                       fprintf (stderr, "\nActual definition statement:\n");
1177                       print_gimple_stmt (stderr, SSA_NAME_DEF_STMT (op),
1178                                              4, TDF_VOPS);
1179                       goto err;
1180                     }
1181                 bitmap_set_bit (names_defined_in_bb, SSA_NAME_VERSION (op));
1182               }
1183           }
1184 
1185       bitmap_clear (names_defined_in_bb);
1186     }
1187 
1188   free (definition_block);
1189 
1190   if (gimple_vop (cfun)
1191       && ssa_default_def (cfun, gimple_vop (cfun)))
1192     {
1193       auto_sbitmap visited (last_basic_block_for_fn (cfun) + 1);
1194       bitmap_clear (visited);
1195       if (verify_vssa (ENTRY_BLOCK_PTR_FOR_FN (cfun),
1196                            ssa_default_def (cfun, gimple_vop (cfun)), visited))
1197           goto err;
1198     }
1199 
1200   /* Restore the dominance information to its prior known state, so
1201      that we do not perturb the compiler's subsequent behavior.  */
1202   if (orig_dom_state == DOM_NONE)
1203     free_dominance_info (CDI_DOMINATORS);
1204   else
1205     set_dom_info_availability (CDI_DOMINATORS, orig_dom_state);
1206 
1207   timevar_pop (TV_TREE_SSA_VERIFY);
1208   return;
1209 
1210 err:
1211   internal_error ("verify_ssa failed");
1212 }
1213 
1214 #if __GNUC__ >= 10
1215 #  pragma GCC diagnostic pop
1216 #endif
1217 
1218 /* Initialize global DFA and SSA structures.
1219    If SIZE is non-zero allocated ssa names array of a given size.  */
1220 
1221 void
init_tree_ssa(struct function * fn,int size)1222 init_tree_ssa (struct function *fn, int size)
1223 {
1224   fn->gimple_df = ggc_cleared_alloc<gimple_df> ();
1225   fn->gimple_df->default_defs = hash_table<ssa_name_hasher>::create_ggc (20);
1226   pt_solution_reset (&fn->gimple_df->escaped);
1227   init_ssanames (fn, size);
1228 }
1229 
1230 /* Deallocate memory associated with SSA data structures for FNDECL.  */
1231 
1232 void
delete_tree_ssa(struct function * fn)1233 delete_tree_ssa (struct function *fn)
1234 {
1235   fini_ssanames (fn);
1236 
1237   /* We no longer maintain the SSA operand cache at this point.  */
1238   if (ssa_operands_active (fn))
1239     fini_ssa_operands (fn);
1240 
1241   fn->gimple_df->default_defs->empty ();
1242   fn->gimple_df->default_defs = NULL;
1243   pt_solution_reset (&fn->gimple_df->escaped);
1244   if (fn->gimple_df->decls_to_pointers != NULL)
1245     delete fn->gimple_df->decls_to_pointers;
1246   fn->gimple_df->decls_to_pointers = NULL;
1247   fn->gimple_df = NULL;
1248 
1249   /* We no longer need the edge variable maps.  */
1250   redirect_edge_var_map_empty ();
1251 }
1252 
1253 /* Return true if EXPR is a useless type conversion, otherwise return
1254    false.  */
1255 
1256 bool
tree_ssa_useless_type_conversion(tree expr)1257 tree_ssa_useless_type_conversion (tree expr)
1258 {
1259   tree outer_type, inner_type;
1260 
1261   /* If we have an assignment that merely uses a NOP_EXPR to change
1262      the top of the RHS to the type of the LHS and the type conversion
1263      is "safe", then strip away the type conversion so that we can
1264      enter LHS = RHS into the const_and_copies table.  */
1265   if (!CONVERT_EXPR_P (expr)
1266       && TREE_CODE (expr) != VIEW_CONVERT_EXPR
1267       && TREE_CODE (expr) != NON_LVALUE_EXPR)
1268     return false;
1269 
1270   outer_type = TREE_TYPE (expr);
1271   inner_type = TREE_TYPE (TREE_OPERAND (expr, 0));
1272 
1273   if (inner_type == error_mark_node)
1274     return false;
1275 
1276   return useless_type_conversion_p (outer_type, inner_type);
1277 }
1278 
1279 /* Strip conversions from EXP according to
1280    tree_ssa_useless_type_conversion and return the resulting
1281    expression.  */
1282 
1283 tree
tree_ssa_strip_useless_type_conversions(tree exp)1284 tree_ssa_strip_useless_type_conversions (tree exp)
1285 {
1286   while (tree_ssa_useless_type_conversion (exp))
1287     exp = TREE_OPERAND (exp, 0);
1288   return exp;
1289 }
1290 
1291 /* Return true if T, as SSA_NAME, has an implicit default defined value.  */
1292 
1293 bool
ssa_defined_default_def_p(tree t)1294 ssa_defined_default_def_p (tree t)
1295 {
1296   tree var = SSA_NAME_VAR (t);
1297 
1298   if (!var)
1299     ;
1300   /* Parameters get their initial value from the function entry.  */
1301   else if (TREE_CODE (var) == PARM_DECL)
1302     return true;
1303   /* When returning by reference the return address is actually a hidden
1304      parameter.  */
1305   else if (TREE_CODE (var) == RESULT_DECL && DECL_BY_REFERENCE (var))
1306     return true;
1307   /* Hard register variables get their initial value from the ether.  */
1308   else if (VAR_P (var) && DECL_HARD_REGISTER (var))
1309     return true;
1310 
1311   return false;
1312 }
1313 
1314 
1315 /* Return true if T, an SSA_NAME, has an undefined value.  PARTIAL is what
1316    should be returned if the value is only partially undefined.  */
1317 
1318 bool
ssa_undefined_value_p(tree t,bool partial)1319 ssa_undefined_value_p (tree t, bool partial)
1320 {
1321   gimple *def_stmt;
1322 
1323   if (ssa_defined_default_def_p (t))
1324     return false;
1325 
1326   /* The value is undefined iff its definition statement is empty.  */
1327   def_stmt = SSA_NAME_DEF_STMT (t);
1328   if (gimple_nop_p (def_stmt))
1329     return true;
1330 
1331   /* The value is undefined if the definition statement is a call
1332      to .DEFERRED_INIT function.  */
1333   if (gimple_call_internal_p (def_stmt, IFN_DEFERRED_INIT))
1334     return true;
1335 
1336   /* The value is partially undefined if the definition statement is
1337      a REALPART_EXPR or IMAGPART_EXPR and its operand is defined by
1338      the call to .DEFERRED_INIT function.  This is for handling the
1339      following case:
1340 
1341   1 typedef _Complex float C;
1342   2 C foo (int cond)
1343   3 {
1344   4   C f;
1345   5   __imag__ f = 0;
1346   6   if (cond)
1347   7     {
1348   8       __real__ f = 1;
1349   9       return f;
1350  10     }
1351  11   return f;
1352  12 }
1353 
1354     with -ftrivial-auto-var-init, compiler will insert the following
1355     artificial initialization:
1356   f = .DEFERRED_INIT (f, 2);
1357   _1 = REALPART_EXPR <f>;
1358 
1359     we should treat the definition _1 = REALPART_EXPR <f> as undefined.  */
1360   if (partial && is_gimple_assign (def_stmt)
1361       && (gimple_assign_rhs_code (def_stmt) == REALPART_EXPR
1362             || gimple_assign_rhs_code (def_stmt) == IMAGPART_EXPR))
1363     {
1364       tree real_imag_part = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
1365       if (TREE_CODE (real_imag_part) == SSA_NAME
1366            && gimple_call_internal_p (SSA_NAME_DEF_STMT (real_imag_part),
1367                                             IFN_DEFERRED_INIT))
1368           return true;
1369     }
1370 
1371   /* Check if the complex was not only partially defined.  */
1372   if (partial && is_gimple_assign (def_stmt)
1373       && gimple_assign_rhs_code (def_stmt) == COMPLEX_EXPR)
1374     {
1375       tree rhs1, rhs2;
1376 
1377       rhs1 = gimple_assign_rhs1 (def_stmt);
1378       rhs2 = gimple_assign_rhs2 (def_stmt);
1379       return (TREE_CODE (rhs1) == SSA_NAME && ssa_undefined_value_p (rhs1))
1380                || (TREE_CODE (rhs2) == SSA_NAME && ssa_undefined_value_p (rhs2));
1381     }
1382   return false;
1383 }
1384 
1385 
1386 /* Return TRUE iff STMT, a gimple statement, references an undefined
1387    SSA name.  */
1388 
1389 bool
gimple_uses_undefined_value_p(gimple * stmt)1390 gimple_uses_undefined_value_p (gimple *stmt)
1391 {
1392   ssa_op_iter iter;
1393   tree op;
1394 
1395   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
1396     if (ssa_undefined_value_p (op))
1397       return true;
1398 
1399   return false;
1400 }
1401 
1402 
1403 /* Return TRUE iff there are any non-PHI uses of VAR that dominate the
1404    end of BB.  If we return TRUE and BB is a loop header, then VAR we
1405    be assumed to be defined within the loop, even if it is marked as
1406    maybe-undefined.  */
1407 
1408 bool
ssa_name_any_use_dominates_bb_p(tree var,basic_block bb)1409 ssa_name_any_use_dominates_bb_p (tree var, basic_block bb)
1410 {
1411   imm_use_iterator iter;
1412   use_operand_p use_p;
1413   FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1414     {
1415       if (is_a <gphi *> (USE_STMT (use_p))
1416             || is_gimple_debug (USE_STMT (use_p)))
1417           continue;
1418       basic_block dombb = gimple_bb (USE_STMT (use_p));
1419       if (dominated_by_p (CDI_DOMINATORS, bb, dombb))
1420           return true;
1421     }
1422 
1423   return false;
1424 }
1425 
1426 /* Mark as maybe_undef any SSA_NAMEs that are unsuitable as ivopts
1427    candidates for potentially involving undefined behavior.  */
1428 
1429 void
mark_ssa_maybe_undefs(void)1430 mark_ssa_maybe_undefs (void)
1431 {
1432   auto_vec<tree> queue;
1433 
1434   /* Scan all SSA_NAMEs, marking the definitely-undefined ones as
1435      maybe-undefined and queuing them for propagation, while clearing
1436      the mark on others.  */
1437   unsigned int i;
1438   tree var;
1439   FOR_EACH_SSA_NAME (i, var, cfun)
1440     {
1441       if (SSA_NAME_IS_VIRTUAL_OPERAND (var)
1442             || !ssa_undefined_value_p (var, false))
1443           ssa_name_set_maybe_undef (var, false);
1444       else
1445           {
1446             ssa_name_set_maybe_undef (var);
1447             queue.safe_push (var);
1448             if (dump_file && (dump_flags & TDF_DETAILS))
1449               fprintf (dump_file, "marking _%i as maybe-undef\n",
1450                          SSA_NAME_VERSION (var));
1451           }
1452     }
1453 
1454   /* Now propagate maybe-undefined from a DEF to any other PHI that
1455      uses it, as long as there isn't any intervening use of DEF.  */
1456   while (!queue.is_empty ())
1457     {
1458       var = queue.pop ();
1459       imm_use_iterator iter;
1460       use_operand_p use_p;
1461       FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1462           {
1463             /* Any uses of VAR that aren't PHI args imply VAR must be
1464                defined, otherwise undefined behavior would have been
1465                definitely invoked.  Only PHI args may hold
1466                maybe-undefined values without invoking undefined
1467                behavior for that reason alone.  */
1468             if (!is_a <gphi *> (USE_STMT (use_p)))
1469               continue;
1470             gphi *phi = as_a <gphi *> (USE_STMT (use_p));
1471 
1472             tree def = gimple_phi_result (phi);
1473             if (ssa_name_maybe_undef_p (def))
1474               continue;
1475 
1476             /* Look for any uses of the maybe-unused SSA_NAME that
1477                dominates the block that reaches the incoming block
1478                corresponding to the PHI arg in which it is mentioned.
1479                That means we can assume the SSA_NAME is defined in that
1480                path, so we only mark a PHI result as maybe-undef if we
1481                find an unused reaching SSA_NAME.  */
1482             int idx = phi_arg_index_from_use (use_p);
1483             basic_block bb = gimple_phi_arg_edge (phi, idx)->src;
1484             if (ssa_name_any_use_dominates_bb_p (var, bb))
1485               continue;
1486 
1487             ssa_name_set_maybe_undef (def);
1488             queue.safe_push (def);
1489             if (dump_file && (dump_flags & TDF_DETAILS))
1490               fprintf (dump_file, "marking _%i as maybe-undef because of _%i\n",
1491                          SSA_NAME_VERSION (def), SSA_NAME_VERSION (var));
1492           }
1493     }
1494 }
1495 
1496 
1497 /* If necessary, rewrite the base of the reference tree *TP from
1498    a MEM_REF to a plain or converted symbol.  */
1499 
1500 static void
maybe_rewrite_mem_ref_base(tree * tp,bitmap suitable_for_renaming)1501 maybe_rewrite_mem_ref_base (tree *tp, bitmap suitable_for_renaming)
1502 {
1503   tree sym;
1504 
1505   while (handled_component_p (*tp))
1506     tp = &TREE_OPERAND (*tp, 0);
1507   if (TREE_CODE (*tp) == MEM_REF
1508       && TREE_CODE (TREE_OPERAND (*tp, 0)) == ADDR_EXPR
1509       && (sym = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0))
1510       && DECL_P (sym)
1511       && !TREE_ADDRESSABLE (sym)
1512       && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym))
1513       && is_gimple_reg_type (TREE_TYPE (*tp))
1514       && ! VOID_TYPE_P (TREE_TYPE (*tp)))
1515     {
1516       if (TREE_CODE (TREE_TYPE (sym)) == VECTOR_TYPE
1517             && useless_type_conversion_p (TREE_TYPE (*tp),
1518                                                   TREE_TYPE (TREE_TYPE (sym)))
1519             && multiple_p (mem_ref_offset (*tp),
1520                                wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (*tp)))))
1521           {
1522             *tp = build3 (BIT_FIELD_REF, TREE_TYPE (*tp), sym,
1523                               TYPE_SIZE (TREE_TYPE (*tp)),
1524                               int_const_binop (MULT_EXPR,
1525                                                    bitsize_int (BITS_PER_UNIT),
1526                                                    TREE_OPERAND (*tp, 1)));
1527           }
1528       else if (TREE_CODE (TREE_TYPE (sym)) == COMPLEX_TYPE
1529                  && useless_type_conversion_p (TREE_TYPE (*tp),
1530                                                        TREE_TYPE (TREE_TYPE (sym))))
1531           {
1532             *tp = build1 (integer_zerop (TREE_OPERAND (*tp, 1))
1533                               ? REALPART_EXPR : IMAGPART_EXPR,
1534                               TREE_TYPE (*tp), sym);
1535           }
1536       else if (integer_zerop (TREE_OPERAND (*tp, 1))
1537                  && DECL_SIZE (sym) == TYPE_SIZE (TREE_TYPE (*tp)))
1538           {
1539             if (!useless_type_conversion_p (TREE_TYPE (*tp),
1540                                                     TREE_TYPE (sym)))
1541               *tp = build1 (VIEW_CONVERT_EXPR,
1542                                 TREE_TYPE (*tp), sym);
1543             else
1544               *tp = sym;
1545           }
1546       else if (DECL_SIZE (sym)
1547                  && TREE_CODE (DECL_SIZE (sym)) == INTEGER_CST
1548                  && (known_subrange_p
1549                        (mem_ref_offset (*tp),
1550                         wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (*tp))),
1551                         0, wi::to_offset (DECL_SIZE_UNIT (sym))))
1552                  && (! INTEGRAL_TYPE_P (TREE_TYPE (*tp))
1553                        || (wi::to_offset (TYPE_SIZE (TREE_TYPE (*tp)))
1554                            == TYPE_PRECISION (TREE_TYPE (*tp))))
1555                  && (! INTEGRAL_TYPE_P (TREE_TYPE (sym))
1556                        || type_has_mode_precision_p (TREE_TYPE (sym)))
1557                  && wi::umod_trunc (wi::to_offset (TYPE_SIZE (TREE_TYPE (*tp))),
1558                                           BITS_PER_UNIT) == 0)
1559           {
1560             *tp = build3 (BIT_FIELD_REF, TREE_TYPE (*tp), sym,
1561                               TYPE_SIZE (TREE_TYPE (*tp)),
1562                               wide_int_to_tree (bitsizetype,
1563                                                     mem_ref_offset (*tp)
1564                                                     << LOG2_BITS_PER_UNIT));
1565           }
1566     }
1567 }
1568 
1569 /* For a tree REF return its base if it is the base of a MEM_REF
1570    that cannot be rewritten into SSA form.  Otherwise return NULL_TREE.  */
1571 
1572 static tree
non_rewritable_mem_ref_base(tree ref)1573 non_rewritable_mem_ref_base (tree ref)
1574 {
1575   tree base;
1576 
1577   /* A plain decl does not need it set.  */
1578   if (DECL_P (ref))
1579     return NULL_TREE;
1580 
1581   if (! (base = CONST_CAST_TREE (strip_invariant_refs (ref))))
1582     {
1583       base = get_base_address (ref);
1584       if (DECL_P (base))
1585           return base;
1586       return NULL_TREE;
1587     }
1588 
1589   /* But watch out for MEM_REFs we cannot lower to a
1590      VIEW_CONVERT_EXPR or a BIT_FIELD_REF.  */
1591   if (TREE_CODE (base) == MEM_REF
1592       && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
1593     {
1594       tree decl = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
1595       if (! DECL_P (decl))
1596           return NULL_TREE;
1597       if (! is_gimple_reg_type (TREE_TYPE (base))
1598             || VOID_TYPE_P (TREE_TYPE (base))
1599             || TREE_THIS_VOLATILE (decl) != TREE_THIS_VOLATILE (base))
1600           return decl;
1601       if ((TREE_CODE (TREE_TYPE (decl)) == VECTOR_TYPE
1602              || TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE)
1603             && useless_type_conversion_p (TREE_TYPE (base),
1604                                                   TREE_TYPE (TREE_TYPE (decl)))
1605             && known_ge (mem_ref_offset (base), 0)
1606             && known_gt (wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (decl))),
1607                            mem_ref_offset (base))
1608             && multiple_p (mem_ref_offset (base),
1609                                wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (base)))))
1610           return NULL_TREE;
1611       /* For same sizes and zero offset we can use a VIEW_CONVERT_EXPR.  */
1612       if (integer_zerop (TREE_OPERAND (base, 1))
1613             && DECL_SIZE (decl) == TYPE_SIZE (TREE_TYPE (base)))
1614           return NULL_TREE;
1615       /* For integral typed extracts we can use a BIT_FIELD_REF.  */
1616       if (DECL_SIZE (decl)
1617             && TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST
1618             && (known_subrange_p
1619                 (mem_ref_offset (base),
1620                  wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (base))),
1621                  0, wi::to_poly_offset (DECL_SIZE_UNIT (decl))))
1622             /* ???  We can't handle bitfield precision extracts without
1623                either using an alternate type for the BIT_FIELD_REF and
1624                then doing a conversion or possibly adjusting the offset
1625                according to endianness.  */
1626             && (! INTEGRAL_TYPE_P (TREE_TYPE (base))
1627                 || (wi::to_offset (TYPE_SIZE (TREE_TYPE (base)))
1628                       == TYPE_PRECISION (TREE_TYPE (base))))
1629             /* ???  Likewise for extracts from bitfields, we'd have
1630                to pun the base object to a size precision mode first.  */
1631             && (! INTEGRAL_TYPE_P (TREE_TYPE (decl))
1632                 || type_has_mode_precision_p (TREE_TYPE (decl)))
1633             && wi::umod_trunc (wi::to_offset (TYPE_SIZE (TREE_TYPE (base))),
1634                                    BITS_PER_UNIT) == 0)
1635           return NULL_TREE;
1636       return decl;
1637     }
1638 
1639   /* We cannot rewrite TARGET_MEM_REFs.  */
1640   if (TREE_CODE (base) == TARGET_MEM_REF
1641       && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
1642     {
1643       tree decl = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
1644       if (! DECL_P (decl))
1645           return NULL_TREE;
1646       return decl;
1647     }
1648 
1649   return NULL_TREE;
1650 }
1651 
1652 /* For an lvalue tree LHS return true if it cannot be rewritten into SSA form.
1653    Otherwise return true.  */
1654 
1655 static bool
non_rewritable_lvalue_p(tree lhs)1656 non_rewritable_lvalue_p (tree lhs)
1657 {
1658   /* A plain decl is always rewritable.  */
1659   if (DECL_P (lhs))
1660     return false;
1661 
1662   /* We can re-write REALPART_EXPR and IMAGPART_EXPR sets in
1663      a reasonably efficient manner... */
1664   if ((TREE_CODE (lhs) == REALPART_EXPR
1665        || TREE_CODE (lhs) == IMAGPART_EXPR)
1666       && DECL_P (TREE_OPERAND (lhs, 0)))
1667     return false;
1668 
1669   /* ???  The following could be relaxed allowing component
1670      references that do not change the access size.  */
1671   if (TREE_CODE (lhs) == MEM_REF
1672       && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR)
1673     {
1674       tree decl = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0);
1675 
1676       /* A decl that is wrapped inside a MEM-REF that covers
1677            it full is also rewritable.  */
1678       if (integer_zerop (TREE_OPERAND (lhs, 1))
1679             && DECL_P (decl)
1680             && DECL_SIZE (decl) == TYPE_SIZE (TREE_TYPE (lhs))
1681             /* If the dynamic type of the decl has larger precision than
1682                the decl itself we can't use the decls type for SSA rewriting.  */
1683             && ((! INTEGRAL_TYPE_P (TREE_TYPE (decl))
1684                  || compare_tree_int (DECL_SIZE (decl),
1685                                             TYPE_PRECISION (TREE_TYPE (decl))) == 0)
1686                 || (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
1687                       && (TYPE_PRECISION (TREE_TYPE (decl))
1688                           >= TYPE_PRECISION (TREE_TYPE (lhs)))))
1689             /* Make sure we are not re-writing non-float copying into float
1690                copying as that can incur normalization.  */
1691             && (! FLOAT_TYPE_P (TREE_TYPE (decl))
1692                 || types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (decl)))
1693             && (TREE_THIS_VOLATILE (decl) == TREE_THIS_VOLATILE (lhs)))
1694           return false;
1695 
1696       /* A vector-insert using a MEM_REF or ARRAY_REF is rewritable
1697            using a BIT_INSERT_EXPR.  */
1698       if (DECL_P (decl)
1699             && VECTOR_TYPE_P (TREE_TYPE (decl))
1700             && TYPE_MODE (TREE_TYPE (decl)) != BLKmode
1701             && known_ge (mem_ref_offset (lhs), 0)
1702             && known_gt (wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (decl))),
1703                            mem_ref_offset (lhs))
1704             && multiple_p (mem_ref_offset (lhs),
1705                                wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (lhs))))
1706             && known_ge (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (decl))),
1707                            wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (lhs)))))
1708           {
1709             poly_uint64 lhs_bits, nelts;
1710             if (poly_int_tree_p (TYPE_SIZE (TREE_TYPE (lhs)), &lhs_bits)
1711                 && multiple_p (lhs_bits,
1712                                    tree_to_uhwi
1713                                      (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))),
1714                                    &nelts)
1715                 && valid_vector_subparts_p (nelts))
1716               {
1717                 if (known_eq (nelts, 1u))
1718                     return false;
1719                 /* For sub-vector inserts the insert vector mode has to be
1720                      supported.  */
1721                 tree vtype = build_vector_type (TREE_TYPE (TREE_TYPE (decl)),
1722                                                         nelts);
1723                 if (TYPE_MODE (vtype) != BLKmode)
1724                     return false;
1725               }
1726           }
1727     }
1728 
1729   /* A vector-insert using a BIT_FIELD_REF is rewritable using
1730      BIT_INSERT_EXPR.  */
1731   if (TREE_CODE (lhs) == BIT_FIELD_REF
1732       && DECL_P (TREE_OPERAND (lhs, 0))
1733       && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (lhs, 0)))
1734       && TYPE_MODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) != BLKmode
1735       && operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (lhs)),
1736                                 TYPE_SIZE_UNIT
1737                                   (TREE_TYPE (TREE_TYPE (TREE_OPERAND (lhs, 0)))), 0)
1738       && (tree_to_uhwi (TREE_OPERAND (lhs, 2))
1739             % tree_to_uhwi (TYPE_SIZE (TREE_TYPE (lhs)))) == 0)
1740     return false;
1741 
1742   return true;
1743 }
1744 
1745 /* When possible, clear TREE_ADDRESSABLE bit, set or clear DECL_NOT_GIMPLE_REG_P
1746    and mark the variable VAR for conversion into SSA.  Return true when updating
1747    stmts is required.  */
1748 
1749 static void
maybe_optimize_var(tree var,bitmap addresses_taken,bitmap not_reg_needs,bitmap suitable_for_renaming)1750 maybe_optimize_var (tree var, bitmap addresses_taken, bitmap not_reg_needs,
1751                         bitmap suitable_for_renaming)
1752 {
1753   /* Global Variables, result decls cannot be changed.  */
1754   if (is_global_var (var)
1755       || TREE_CODE (var) == RESULT_DECL
1756       || bitmap_bit_p (addresses_taken, DECL_UID (var)))
1757     return;
1758 
1759   bool maybe_reg = false;
1760   if (TREE_ADDRESSABLE (var))
1761     {
1762       TREE_ADDRESSABLE (var) = 0;
1763       maybe_reg = true;
1764       if (dump_file)
1765           {
1766             fprintf (dump_file, "No longer having address taken: ");
1767             print_generic_expr (dump_file, var);
1768             fprintf (dump_file, "\n");
1769           }
1770     }
1771 
1772   /* For register type decls if we do not have any partial defs
1773      we cannot express in SSA form mark them as DECL_NOT_GIMPLE_REG_P
1774      as to avoid SSA rewrite.  For the others go ahead and mark
1775      them for renaming.  */
1776   if (is_gimple_reg_type (TREE_TYPE (var)))
1777     {
1778       if (bitmap_bit_p (not_reg_needs, DECL_UID (var)))
1779           {
1780             DECL_NOT_GIMPLE_REG_P (var) = 1;
1781             if (dump_file)
1782               {
1783                 fprintf (dump_file, "Has partial defs: ");
1784                 print_generic_expr (dump_file, var);
1785                 fprintf (dump_file, "\n");
1786               }
1787           }
1788       else if (DECL_NOT_GIMPLE_REG_P (var))
1789           {
1790             maybe_reg = true;
1791             DECL_NOT_GIMPLE_REG_P (var) = 0;
1792           }
1793       if (maybe_reg)
1794           {
1795             if (is_gimple_reg (var))
1796               {
1797                 if (dump_file)
1798                     {
1799                       fprintf (dump_file, "Now a gimple register: ");
1800                       print_generic_expr (dump_file, var);
1801                       fprintf (dump_file, "\n");
1802                     }
1803                 bitmap_set_bit (suitable_for_renaming, DECL_UID (var));
1804               }
1805             else
1806               DECL_NOT_GIMPLE_REG_P (var) = 1;
1807           }
1808     }
1809 }
1810 
1811 /* Return true when STMT is ASAN mark where second argument is an address
1812    of a local variable.  */
1813 
1814 static bool
is_asan_mark_p(gimple * stmt)1815 is_asan_mark_p (gimple *stmt)
1816 {
1817   if (!gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1818     return false;
1819 
1820   tree addr = get_base_address (gimple_call_arg (stmt, 1));
1821   if (TREE_CODE (addr) == ADDR_EXPR
1822       && VAR_P (TREE_OPERAND (addr, 0)))
1823     {
1824       tree var = TREE_OPERAND (addr, 0);
1825       if (lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1826                                   DECL_ATTRIBUTES (var)))
1827           return false;
1828 
1829       unsigned addressable = TREE_ADDRESSABLE (var);
1830       TREE_ADDRESSABLE (var) = 0;
1831       bool r = is_gimple_reg (var);
1832       TREE_ADDRESSABLE (var) = addressable;
1833       return r;
1834     }
1835 
1836   return false;
1837 }
1838 
1839 /* Compute TREE_ADDRESSABLE and whether we have unhandled partial defs
1840    for local variables.  */
1841 
1842 void
execute_update_addresses_taken(void)1843 execute_update_addresses_taken (void)
1844 {
1845   basic_block bb;
1846   auto_bitmap addresses_taken;
1847   auto_bitmap not_reg_needs;
1848   auto_bitmap suitable_for_renaming;
1849   bool optimistic_not_addressable = false;
1850   tree var;
1851   unsigned i;
1852 
1853   timevar_push (TV_ADDRESS_TAKEN);
1854 
1855   /* Collect into ADDRESSES_TAKEN all variables whose address is taken within
1856      the function body.  */
1857   FOR_EACH_BB_FN (bb, cfun)
1858     {
1859       for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1860              gsi_next (&gsi))
1861           {
1862             gimple *stmt = gsi_stmt (gsi);
1863             enum gimple_code code = gimple_code (stmt);
1864             tree decl;
1865 
1866             if (code == GIMPLE_CALL)
1867               {
1868                 if (optimize_atomic_compare_exchange_p (stmt))
1869                     {
1870                       /* For __atomic_compare_exchange_N if the second argument
1871                          is &var, don't mark var addressable;
1872                          if it becomes non-addressable, we'll rewrite it into
1873                          ATOMIC_COMPARE_EXCHANGE call.  */
1874                       tree arg = gimple_call_arg (stmt, 1);
1875                       gimple_call_set_arg (stmt, 1, null_pointer_node);
1876                       gimple_ior_addresses_taken (addresses_taken, stmt);
1877                       gimple_call_set_arg (stmt, 1, arg);
1878                       /* Remember we have to check again below.  */
1879                       optimistic_not_addressable = true;
1880                     }
1881                 else if (is_asan_mark_p (stmt)
1882                            || gimple_call_internal_p (stmt, IFN_GOMP_SIMT_ENTER))
1883                     ;
1884                 else
1885                     gimple_ior_addresses_taken (addresses_taken, stmt);
1886               }
1887             else
1888               /* Note all addresses taken by the stmt.  */
1889               gimple_ior_addresses_taken (addresses_taken, stmt);
1890 
1891             /* If we have a call or an assignment, see if the lhs contains
1892                a local decl that requires not to be a gimple register.  */
1893             if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
1894               {
1895               tree lhs = gimple_get_lhs (stmt);
1896               if (lhs
1897                       && TREE_CODE (lhs) != SSA_NAME
1898                       && ((code == GIMPLE_CALL && ! DECL_P (lhs))
1899                           || non_rewritable_lvalue_p (lhs)))
1900                     {
1901                       decl = get_base_address (lhs);
1902                       if (DECL_P (decl))
1903                         bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1904                 }
1905               }
1906 
1907             if (gimple_assign_single_p (stmt))
1908               {
1909                 tree rhs = gimple_assign_rhs1 (stmt);
1910                 if ((decl = non_rewritable_mem_ref_base (rhs)))
1911                     bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1912               }
1913 
1914             else if (code == GIMPLE_CALL)
1915               {
1916                 for (i = 0; i < gimple_call_num_args (stmt); ++i)
1917                     {
1918                       tree arg = gimple_call_arg (stmt, i);
1919                       if ((decl = non_rewritable_mem_ref_base (arg)))
1920                         bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1921                     }
1922               }
1923 
1924             else if (code == GIMPLE_ASM)
1925               {
1926                 gasm *asm_stmt = as_a <gasm *> (stmt);
1927                 for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
1928                     {
1929                       tree link = gimple_asm_output_op (asm_stmt, i);
1930                       tree lhs = TREE_VALUE (link);
1931                       if (TREE_CODE (lhs) != SSA_NAME)
1932                         {
1933                           decl = get_base_address (lhs);
1934                           if (DECL_P (decl)
1935                                 && (non_rewritable_lvalue_p (lhs)
1936                                     /* We cannot move required conversions from
1937                                          the lhs to the rhs in asm statements, so
1938                                          require we do not need any.  */
1939                                     || !useless_type_conversion_p
1940                                           (TREE_TYPE (lhs), TREE_TYPE (decl))))
1941                               bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1942                         }
1943                     }
1944                 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
1945                     {
1946                       tree link = gimple_asm_input_op (asm_stmt, i);
1947                       if ((decl = non_rewritable_mem_ref_base (TREE_VALUE (link))))
1948                         bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1949                     }
1950               }
1951           }
1952 
1953       for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1954              gsi_next (&gsi))
1955           {
1956             size_t i;
1957             gphi *phi = gsi.phi ();
1958 
1959             for (i = 0; i < gimple_phi_num_args (phi); i++)
1960               {
1961                 tree op = PHI_ARG_DEF (phi, i), var;
1962                 if (TREE_CODE (op) == ADDR_EXPR
1963                       && (var = get_base_address (TREE_OPERAND (op, 0))) != NULL
1964                       && DECL_P (var))
1965                     bitmap_set_bit (addresses_taken, DECL_UID (var));
1966               }
1967           }
1968     }
1969 
1970   /* We cannot iterate over all referenced vars because that can contain
1971      unused vars from BLOCK trees, which causes code generation differences
1972      for -g vs. -g0.  */
1973   for (var = DECL_ARGUMENTS (cfun->decl); var; var = DECL_CHAIN (var))
1974     maybe_optimize_var (var, addresses_taken, not_reg_needs,
1975                               suitable_for_renaming);
1976 
1977   FOR_EACH_VEC_SAFE_ELT (cfun->local_decls, i, var)
1978     maybe_optimize_var (var, addresses_taken, not_reg_needs,
1979                               suitable_for_renaming);
1980 
1981   /* Operand caches need to be recomputed for operands referencing the updated
1982      variables and operands need to be rewritten to expose bare symbols.  */
1983   if (!bitmap_empty_p (suitable_for_renaming)
1984       || optimistic_not_addressable)
1985     {
1986       FOR_EACH_BB_FN (bb, cfun)
1987           for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1988             {
1989               gimple *stmt = gsi_stmt (gsi);
1990 
1991               /* Re-write TARGET_MEM_REFs of symbols we want to
1992                  rewrite into SSA form.  */
1993               if (gimple_assign_single_p (stmt))
1994                 {
1995                     tree lhs = gimple_assign_lhs (stmt);
1996                     tree rhs, *rhsp = gimple_assign_rhs1_ptr (stmt);
1997                     tree sym;
1998 
1999                     /* Rewrite LHS IMAG/REALPART_EXPR similar to
2000                        gimplify_modify_expr_complex_part.  */
2001                     if ((TREE_CODE (lhs) == IMAGPART_EXPR
2002                          || TREE_CODE (lhs) == REALPART_EXPR)
2003                         && DECL_P (TREE_OPERAND (lhs, 0))
2004                         && bitmap_bit_p (suitable_for_renaming,
2005                                              DECL_UID (TREE_OPERAND (lhs, 0))))
2006                       {
2007                         tree other = make_ssa_name (TREE_TYPE (lhs));
2008                         tree lrhs = build1 (TREE_CODE (lhs) == IMAGPART_EXPR
2009                                                   ? REALPART_EXPR : IMAGPART_EXPR,
2010                                                   TREE_TYPE (other),
2011                                                   TREE_OPERAND (lhs, 0));
2012                         suppress_warning (lrhs);
2013                         gimple *load = gimple_build_assign (other, lrhs);
2014                         location_t loc = gimple_location (stmt);
2015                         gimple_set_location (load, loc);
2016                         gimple_set_vuse (load, gimple_vuse (stmt));
2017                         gsi_insert_before (&gsi, load, GSI_SAME_STMT);
2018                         gimple_assign_set_lhs (stmt, TREE_OPERAND (lhs, 0));
2019                         gimple_assign_set_rhs_with_ops
2020                           (&gsi, COMPLEX_EXPR,
2021                            TREE_CODE (lhs) == IMAGPART_EXPR
2022                            ? other : gimple_assign_rhs1 (stmt),
2023                            TREE_CODE (lhs) == IMAGPART_EXPR
2024                            ? gimple_assign_rhs1 (stmt) : other, NULL_TREE);
2025                         stmt = gsi_stmt (gsi);
2026                         unlink_stmt_vdef (stmt);
2027                         update_stmt (stmt);
2028                         continue;
2029                       }
2030 
2031                     /* Rewrite a vector insert via a BIT_FIELD_REF on the LHS
2032                        into a BIT_INSERT_EXPR.  */
2033                     if (TREE_CODE (lhs) == BIT_FIELD_REF
2034                         && DECL_P (TREE_OPERAND (lhs, 0))
2035                         && bitmap_bit_p (suitable_for_renaming,
2036                                              DECL_UID (TREE_OPERAND (lhs, 0)))
2037                         && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (lhs, 0)))
2038                         && TYPE_MODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) != BLKmode
2039                         && operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (lhs)),
2040                                                   TYPE_SIZE_UNIT (TREE_TYPE
2041                                                     (TREE_TYPE (TREE_OPERAND (lhs, 0)))),
2042                                                   0)
2043                         && (tree_to_uhwi (TREE_OPERAND (lhs, 2))
2044                               % tree_to_uhwi (TYPE_SIZE (TREE_TYPE (lhs))) == 0))
2045                       {
2046                         tree var = TREE_OPERAND (lhs, 0);
2047                         tree val = gimple_assign_rhs1 (stmt);
2048                         if (! types_compatible_p (TREE_TYPE (TREE_TYPE (var)),
2049                                                         TREE_TYPE (val)))
2050                           {
2051                               tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (var)));
2052                               gimple *pun
2053                                 = gimple_build_assign (tem,
2054                                                              build1 (VIEW_CONVERT_EXPR,
2055                                                                        TREE_TYPE (tem), val));
2056                               gsi_insert_before (&gsi, pun, GSI_SAME_STMT);
2057                               val = tem;
2058                           }
2059                         tree bitpos = TREE_OPERAND (lhs, 2);
2060                         gimple_assign_set_lhs (stmt, var);
2061                         gimple_assign_set_rhs_with_ops
2062                           (&gsi, BIT_INSERT_EXPR, var, val, bitpos);
2063                         stmt = gsi_stmt (gsi);
2064                         unlink_stmt_vdef (stmt);
2065                         update_stmt (stmt);
2066                         continue;
2067                       }
2068 
2069                     /* Rewrite a vector insert using a MEM_REF on the LHS
2070                        into a BIT_INSERT_EXPR.  */
2071                     if (TREE_CODE (lhs) == MEM_REF
2072                         && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR
2073                         && (sym = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0))
2074                         && DECL_P (sym)
2075                         && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym))
2076                         && VECTOR_TYPE_P (TREE_TYPE (sym))
2077                         && TYPE_MODE (TREE_TYPE (sym)) != BLKmode
2078                         /* If it is a full replacement we can do better below.  */
2079                         && maybe_ne (wi::to_poly_offset
2080                                            (TYPE_SIZE_UNIT (TREE_TYPE (lhs))),
2081                                          wi::to_poly_offset
2082                                    (TYPE_SIZE_UNIT (TREE_TYPE (sym))))
2083                         && known_ge (mem_ref_offset (lhs), 0)
2084                         && known_gt (wi::to_poly_offset
2085                                            (TYPE_SIZE_UNIT (TREE_TYPE (sym))),
2086                                          mem_ref_offset (lhs))
2087                         && multiple_p (mem_ref_offset (lhs),
2088                                            wi::to_poly_offset
2089                                              (TYPE_SIZE_UNIT (TREE_TYPE (lhs)))))
2090                       {
2091                         tree val = gimple_assign_rhs1 (stmt);
2092                         if (! types_compatible_p (TREE_TYPE (val),
2093                                                         TREE_TYPE (TREE_TYPE (sym))))
2094                           {
2095                               poly_uint64 lhs_bits, nelts;
2096                               tree temtype = TREE_TYPE (TREE_TYPE (sym));
2097                               if (poly_int_tree_p (TYPE_SIZE (TREE_TYPE (lhs)),
2098                                                        &lhs_bits)
2099                                   && multiple_p (lhs_bits,
2100                                                      tree_to_uhwi
2101                                                        (TYPE_SIZE (TREE_TYPE
2102                                                                          (TREE_TYPE (sym)))),
2103                                                      &nelts)
2104                                   && maybe_ne (nelts, 1u)
2105                                   && valid_vector_subparts_p (nelts))
2106                                 temtype = build_vector_type (temtype, nelts);
2107                               tree tem = make_ssa_name (temtype);
2108                               gimple *pun
2109                                 = gimple_build_assign (tem,
2110                                                              build1 (VIEW_CONVERT_EXPR,
2111                                                                        TREE_TYPE (tem), val));
2112                               gsi_insert_before (&gsi, pun, GSI_SAME_STMT);
2113                               val = tem;
2114                           }
2115                         tree bitpos
2116                           = wide_int_to_tree (bitsizetype,
2117                                                     mem_ref_offset (lhs) * BITS_PER_UNIT);
2118                         gimple_assign_set_lhs (stmt, sym);
2119                         gimple_assign_set_rhs_with_ops
2120                           (&gsi, BIT_INSERT_EXPR, sym, val, bitpos);
2121                         stmt = gsi_stmt (gsi);
2122                         unlink_stmt_vdef (stmt);
2123                         update_stmt (stmt);
2124                         continue;
2125                       }
2126 
2127                     /* We shouldn't have any fancy wrapping of
2128                        component-refs on the LHS, but look through
2129                        VIEW_CONVERT_EXPRs as that is easy.  */
2130                     while (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
2131                       lhs = TREE_OPERAND (lhs, 0);
2132                     if (TREE_CODE (lhs) == MEM_REF
2133                         && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR
2134                         && integer_zerop (TREE_OPERAND (lhs, 1))
2135                         && (sym = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0))
2136                         && DECL_P (sym)
2137                         && !TREE_ADDRESSABLE (sym)
2138                         && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym)))
2139                       lhs = sym;
2140                     else
2141                       lhs = gimple_assign_lhs (stmt);
2142 
2143                     /* Rewrite the RHS and make sure the resulting assignment
2144                        is validly typed.  */
2145                     maybe_rewrite_mem_ref_base (rhsp, suitable_for_renaming);
2146                     rhs = gimple_assign_rhs1 (stmt);
2147                     if (gimple_assign_lhs (stmt) != lhs
2148                         && !useless_type_conversion_p (TREE_TYPE (lhs),
2149                                                                TREE_TYPE (rhs)))
2150                       {
2151                         if (gimple_clobber_p (stmt))
2152                           {
2153                               rhs = build_constructor (TREE_TYPE (lhs), NULL);
2154                               TREE_THIS_VOLATILE (rhs) = 1;
2155                           }
2156                         else
2157                           rhs = fold_build1 (VIEW_CONVERT_EXPR,
2158                                                    TREE_TYPE (lhs), rhs);
2159                       }
2160                     if (gimple_assign_lhs (stmt) != lhs)
2161                       gimple_assign_set_lhs (stmt, lhs);
2162 
2163                     if (gimple_assign_rhs1 (stmt) != rhs)
2164                       {
2165                         gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2166                         gimple_assign_set_rhs_from_tree (&gsi, rhs);
2167                       }
2168                 }
2169 
2170               else if (gimple_code (stmt) == GIMPLE_CALL)
2171                 {
2172                     unsigned i;
2173                     if (optimize_atomic_compare_exchange_p (stmt))
2174                       {
2175                         tree expected = gimple_call_arg (stmt, 1);
2176                         tree decl = TREE_OPERAND (expected, 0);
2177                         if (bitmap_bit_p (suitable_for_renaming, DECL_UID (decl)))
2178                           {
2179                               fold_builtin_atomic_compare_exchange (&gsi);
2180                               continue;
2181                           }
2182                         else if (!TREE_ADDRESSABLE (decl))
2183                           /* If there are partial defs of the decl we may
2184                                have cleared the addressable bit but set
2185                                DECL_NOT_GIMPLE_REG_P.  We have to restore
2186                                TREE_ADDRESSABLE here.  */
2187                           TREE_ADDRESSABLE (decl) = 1;
2188                       }
2189                     else if (is_asan_mark_p (stmt))
2190                       {
2191                         tree var = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
2192                         if (bitmap_bit_p (suitable_for_renaming, DECL_UID (var)))
2193                           {
2194                               unlink_stmt_vdef (stmt);
2195                               if (asan_mark_p (stmt, ASAN_MARK_POISON))
2196                                 {
2197                                   gcall *call
2198                                     = gimple_build_call_internal (IFN_ASAN_POISON, 0);
2199                                   gimple_call_set_lhs (call, var);
2200                                   gsi_replace (&gsi, call, true);
2201                                 }
2202                               else
2203                                 {
2204                                   /* In ASAN_MARK (UNPOISON, &b, ...) the variable
2205                                      is uninitialized.  Avoid dependencies on
2206                                      previous out of scope value.  */
2207                                   tree clobber = build_clobber (TREE_TYPE (var));
2208                                   gimple *g = gimple_build_assign (var, clobber);
2209                                   gsi_replace (&gsi, g, true);
2210                                 }
2211                               continue;
2212                           }
2213                       }
2214                     else if (gimple_call_internal_p (stmt, IFN_GOMP_SIMT_ENTER))
2215                       for (i = 1; i < gimple_call_num_args (stmt); i++)
2216                         {
2217                           tree *argp = gimple_call_arg_ptr (stmt, i);
2218                           if (*argp == null_pointer_node)
2219                               continue;
2220                           gcc_assert (TREE_CODE (*argp) == ADDR_EXPR
2221                                           && VAR_P (TREE_OPERAND (*argp, 0)));
2222                           tree var = TREE_OPERAND (*argp, 0);
2223                           if (bitmap_bit_p (suitable_for_renaming, DECL_UID (var)))
2224                               *argp = null_pointer_node;
2225                         }
2226                     for (i = 0; i < gimple_call_num_args (stmt); ++i)
2227                       {
2228                         tree *argp = gimple_call_arg_ptr (stmt, i);
2229                         maybe_rewrite_mem_ref_base (argp, suitable_for_renaming);
2230                       }
2231                 }
2232 
2233               else if (gimple_code (stmt) == GIMPLE_ASM)
2234                 {
2235                     gasm *asm_stmt = as_a <gasm *> (stmt);
2236                     unsigned i;
2237                     for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
2238                       {
2239                         tree link = gimple_asm_output_op (asm_stmt, i);
2240                         maybe_rewrite_mem_ref_base (&TREE_VALUE (link),
2241                                                             suitable_for_renaming);
2242                       }
2243                     for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
2244                       {
2245                         tree link = gimple_asm_input_op (asm_stmt, i);
2246                         maybe_rewrite_mem_ref_base (&TREE_VALUE (link),
2247                                                             suitable_for_renaming);
2248                       }
2249                 }
2250 
2251               else if (gimple_debug_bind_p (stmt)
2252                          && gimple_debug_bind_has_value_p (stmt))
2253                 {
2254                     tree *valuep = gimple_debug_bind_get_value_ptr (stmt);
2255                     tree decl;
2256                     maybe_rewrite_mem_ref_base (valuep, suitable_for_renaming);
2257                     decl = non_rewritable_mem_ref_base (*valuep);
2258                     if (decl
2259                         && bitmap_bit_p (suitable_for_renaming, DECL_UID (decl)))
2260                       gimple_debug_bind_reset_value (stmt);
2261                 }
2262 
2263               if (gimple_references_memory_p (stmt)
2264                     || is_gimple_debug (stmt))
2265                 update_stmt (stmt);
2266 
2267               gsi_next (&gsi);
2268             }
2269 
2270       /* Update SSA form here, we are called as non-pass as well.  */
2271       if (number_of_loops (cfun) > 1
2272             && loops_state_satisfies_p (LOOP_CLOSED_SSA))
2273           rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
2274       else
2275           update_ssa (TODO_update_ssa);
2276     }
2277 
2278   timevar_pop (TV_ADDRESS_TAKEN);
2279 }
2280 
2281 namespace {
2282 
2283 const pass_data pass_data_update_address_taken =
2284 {
2285   GIMPLE_PASS, /* type */
2286   "addressables", /* name */
2287   OPTGROUP_NONE, /* optinfo_flags */
2288   TV_ADDRESS_TAKEN, /* tv_id */
2289   PROP_ssa, /* properties_required */
2290   0, /* properties_provided */
2291   0, /* properties_destroyed */
2292   0, /* todo_flags_start */
2293   TODO_update_address_taken, /* todo_flags_finish */
2294 };
2295 
2296 class pass_update_address_taken : public gimple_opt_pass
2297 {
2298 public:
pass_update_address_taken(gcc::context * ctxt)2299   pass_update_address_taken (gcc::context *ctxt)
2300     : gimple_opt_pass (pass_data_update_address_taken, ctxt)
2301   {}
2302 
2303   /* opt_pass methods: */
2304 
2305 }; // class pass_update_address_taken
2306 
2307 } // anon namespace
2308 
2309 gimple_opt_pass *
make_pass_update_address_taken(gcc::context * ctxt)2310 make_pass_update_address_taken (gcc::context *ctxt)
2311 {
2312   return new pass_update_address_taken (ctxt);
2313 }
2314