Line data Source code
1 : /* Miscellaneous SSA utility functions.
2 : Copyright (C) 2001-2026 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-iterator.h"
34 : #include "gimple-fold.h"
35 : #include "gimplify.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
55 12403006 : redirect_edge_var_map_add (edge e, tree result, tree def, location_t locus)
56 : {
57 12403006 : edge_var_map new_node;
58 :
59 12403006 : if (edge_var_maps == NULL)
60 70620 : edge_var_maps = new hash_map<edge, auto_vec<edge_var_map> >;
61 :
62 12403006 : auto_vec<edge_var_map> &slot = edge_var_maps->get_or_insert (e);
63 12403006 : new_node.def = def;
64 12403006 : new_node.result = result;
65 12403006 : new_node.locus = locus;
66 :
67 12403006 : slot.safe_push (new_node);
68 12403006 : }
69 :
70 :
71 : /* Clear the var mappings in edge E. */
72 :
73 : void
74 145793095 : redirect_edge_var_map_clear (edge e)
75 : {
76 145793095 : if (!edge_var_maps)
77 : return;
78 :
79 129905444 : auto_vec<edge_var_map> *head = edge_var_maps->get (e);
80 :
81 129905444 : if (head)
82 10909313 : 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
93 447184 : redirect_edge_var_map_dup (edge newe, edge olde)
94 : {
95 447184 : if (!edge_var_maps)
96 : return;
97 :
98 416256 : auto_vec<edge_var_map> *new_head = &edge_var_maps->get_or_insert (newe);
99 416256 : auto_vec<edge_var_map> *old_head = edge_var_maps->get (olde);
100 416256 : if (!old_head)
101 : return;
102 :
103 7883 : 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> *
111 8589600 : redirect_edge_var_map_vector (edge e)
112 : {
113 : /* Hey, what kind of idiot would... you'd be surprised. */
114 8589600 : if (!edge_var_maps)
115 : return NULL;
116 :
117 8563504 : auto_vec<edge_var_map> *slot = edge_var_maps->get (e);
118 8563504 : if (!slot)
119 : return NULL;
120 :
121 : return slot;
122 : }
123 :
124 : /* Clear the edge variable mappings. */
125 :
126 : void
127 952377868 : redirect_edge_var_map_empty (void)
128 : {
129 952377868 : if (edge_var_maps)
130 255942721 : edge_var_maps->empty ();
131 952377868 : }
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
140 65476498 : ssa_redirect_edge (edge e, basic_block dest)
141 : {
142 65476498 : gphi_iterator gsi;
143 65476498 : gphi *phi;
144 :
145 65476498 : 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 65476498 : if (! (e->dest->flags & BB_DUPLICATED))
151 76660299 : for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
152 : {
153 13166571 : tree def;
154 13166571 : location_t locus;
155 :
156 13166571 : phi = gsi.phi ();
157 13166571 : def = gimple_phi_arg_def (phi, e->dest_idx);
158 13166571 : locus = gimple_phi_arg_location (phi, e->dest_idx);
159 :
160 13166571 : if (def == NULL_TREE)
161 1701087 : continue;
162 :
163 11465484 : redirect_edge_var_map_add (e, gimple_phi_result (phi), def, locus);
164 : }
165 :
166 65476498 : e = redirect_edge_succ_nodup (e, dest);
167 :
168 65476498 : 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
176 3465723 : flush_pending_stmts (edge e)
177 : {
178 3465723 : gphi *phi;
179 3465723 : edge_var_map *vm;
180 3465723 : int i;
181 3465723 : gphi_iterator gsi;
182 :
183 3465723 : vec<edge_var_map> *v = redirect_edge_var_map_vector (e);
184 3465723 : if (!v)
185 363189 : return;
186 :
187 3102534 : for (gsi = gsi_start_phis (e->dest), i = 0;
188 8521257 : !gsi_end_p (gsi) && v->iterate (i, &vm);
189 5418723 : gsi_next (&gsi), i++)
190 : {
191 5418723 : tree def;
192 :
193 5418723 : phi = gsi.phi ();
194 5418723 : def = redirect_edge_var_map_def (vm);
195 5418723 : add_phi_arg (phi, def, e, redirect_edge_var_map_location (vm));
196 : }
197 :
198 3102534 : 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
221 51 : gimple_replace_ssa_lhs (gimple *stmt, tree nlhs)
222 : {
223 51 : if (MAY_HAVE_DEBUG_BIND_STMTS)
224 : {
225 0 : tree lhs = gimple_get_lhs (stmt);
226 :
227 0 : gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
228 :
229 0 : insert_debug_temp_for_var_def (NULL, lhs);
230 : }
231 :
232 51 : gimple_set_lhs (stmt, nlhs);
233 51 : }
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
243 390446039 : target_for_debug_bind (tree var)
244 : {
245 390448301 : if (!MAY_HAVE_DEBUG_BIND_STMTS)
246 : return NULL_TREE;
247 :
248 368637462 : if (TREE_CODE (var) == SSA_NAME)
249 : {
250 17115892 : var = SSA_NAME_VAR (var);
251 : if (var == NULL_TREE)
252 : return NULL_TREE;
253 : }
254 :
255 314107278 : if ((!VAR_P (var) || VAR_DECL_IS_VIRTUAL_OPERAND (var))
256 377720271 : && TREE_CODE (var) != PARM_DECL)
257 : return NULL_TREE;
258 :
259 311921260 : if (DECL_HAS_VALUE_EXPR_P (var))
260 2262 : return target_for_debug_bind (DECL_VALUE_EXPR (var));
261 :
262 311918998 : if (DECL_IGNORED_P (var))
263 : return NULL_TREE;
264 :
265 : /* var-tracking only tracks registers. */
266 288802088 : if (!is_gimple_reg_type (TREE_TYPE (var)))
267 9316746 : 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
276 10072340 : find_released_ssa_name (tree *tp, int *walk_subtrees, void *data_)
277 : {
278 10072340 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data_;
279 :
280 10072340 : if (wi && wi->is_lhs)
281 : return NULL_TREE;
282 :
283 10024692 : if (TREE_CODE (*tp) == SSA_NAME)
284 : {
285 1641431 : if (SSA_NAME_IN_FREE_LIST (*tp))
286 : return *tp;
287 :
288 1621681 : *walk_subtrees = 0;
289 : }
290 8383261 : else if (IS_TYPE_OR_DECL_P (*tp))
291 38283 : *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
301 94071865 : insert_debug_temp_for_var_def (gimple_stmt_iterator *gsi, tree var)
302 : {
303 94071865 : imm_use_iterator imm_iter;
304 94071865 : use_operand_p use_p;
305 94071865 : gimple *stmt;
306 94071865 : gimple *def_stmt = NULL;
307 94071865 : int usecount = 0;
308 94071865 : tree value = NULL;
309 :
310 94071865 : if (!MAY_HAVE_DEBUG_BIND_STMTS)
311 90492673 : 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 94071865 : 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 104220642 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, var)
321 : {
322 11864029 : stmt = USE_STMT (use_p);
323 :
324 11864029 : if (!gimple_debug_bind_p (stmt))
325 7785229 : continue;
326 :
327 4078800 : if (usecount++)
328 : break;
329 :
330 3579192 : 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 755209 : usecount++;
336 755209 : break;
337 : }
338 93611430 : }
339 :
340 93611430 : if (!usecount)
341 : return;
342 :
343 3579192 : if (gsi)
344 3113821 : def_stmt = gsi_stmt (*gsi);
345 : else
346 465371 : 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 3579192 : if (gimple_code (def_stmt) == GIMPLE_PHI)
352 : {
353 175128 : value = degenerate_phi_result (as_a <gphi *> (def_stmt));
354 175128 : if (value && walk_tree (&value, find_released_ssa_name, NULL, NULL))
355 63 : value = NULL;
356 : /* error_mark_node is what fixup_noreturn_call changes PHI arguments
357 : to. */
358 175065 : else if (value == error_mark_node)
359 0 : value = NULL;
360 : }
361 3404064 : 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 1297710 : value = NULL;
366 2106354 : else if (is_gimple_assign (def_stmt))
367 : {
368 2018319 : bool no_value = false;
369 :
370 2018319 : if (!dom_info_available_p (CDI_DOMINATORS))
371 : {
372 67335 : struct walk_stmt_info wi;
373 :
374 67335 : 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 67308 : if ((!gsi && !gimple_bb (def_stmt))
403 134643 : || walk_gimple_op (def_stmt, find_released_ssa_name, &wi))
404 19687 : no_value = true;
405 : }
406 :
407 67335 : if (!no_value)
408 1998632 : value = gimple_assign_rhs_to_tree (def_stmt);
409 : }
410 :
411 3579192 : 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), then we can propagate VALUE into this single use,
416 : avoiding the temp.
417 :
418 : We can also avoid using a temp if VALUE can be shared and
419 : propagated into all uses, without generating expressions that
420 : wouldn't be valid gimple RHSs.
421 :
422 : Other cases that would require unsharing or non-gimple RHSs
423 : are deferred to a debug temp, although we could avoid temps
424 : at the expense of duplication of expressions. */
425 :
426 2025923 : if (usecount == 1
427 1128055 : || gimple_code (def_stmt) == GIMPLE_PHI
428 1118534 : || CONSTANT_CLASS_P (value)
429 3141786 : || is_gimple_reg (value))
430 : ;
431 : else
432 : {
433 1113854 : gdebug *def_temp;
434 1113854 : tree vexpr = build_debug_expr_decl (TREE_TYPE (value));
435 :
436 1113854 : def_temp = gimple_build_debug_bind (vexpr,
437 : unshare_expr (value),
438 : def_stmt);
439 :
440 : /* FIXME: Is setting the mode really necessary? */
441 1113854 : if (DECL_P (value))
442 2831 : SET_DECL_MODE (vexpr, DECL_MODE (value));
443 : else
444 1111023 : SET_DECL_MODE (vexpr, TYPE_MODE (TREE_TYPE (value)));
445 :
446 1113854 : if (gsi)
447 880990 : gsi_insert_before (gsi, def_temp, GSI_SAME_STMT);
448 : else
449 : {
450 232864 : gimple_stmt_iterator ngsi = gsi_for_stmt (def_stmt);
451 232864 : gsi_insert_before (&ngsi, def_temp, GSI_SAME_STMT);
452 : }
453 :
454 1113854 : value = vexpr;
455 : }
456 : }
457 :
458 10352040 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, var)
459 : {
460 6772848 : if (!gimple_debug_bind_p (stmt))
461 932563 : continue;
462 :
463 5840285 : if (value)
464 : {
465 8227482 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
466 4113741 : SET_USE (use_p, unshare_expr (value));
467 : /* If we didn't replace uses with a debug decl fold the
468 : resulting expression. Otherwise we end up with invalid IL. */
469 4112394 : if (TREE_CODE (value) != DEBUG_EXPR_DECL)
470 : {
471 939844 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
472 939844 : fold_stmt_inplace (&gsi);
473 : }
474 : }
475 : else
476 1727891 : gimple_debug_bind_reset_value (stmt);
477 :
478 5840285 : update_stmt (stmt);
479 3579192 : }
480 : }
481 :
482 :
483 : /* Insert a DEBUG BIND stmt before STMT for each DEF referenced by
484 : other DEBUG stmts, and replace uses of the DEF with the
485 : newly-created debug temp. */
486 :
487 : void
488 174505640 : insert_debug_temps_for_defs (gimple_stmt_iterator *gsi)
489 : {
490 174505640 : gimple *stmt;
491 174505640 : ssa_op_iter op_iter;
492 174505640 : def_operand_p def_p;
493 :
494 174505640 : if (!MAY_HAVE_DEBUG_BIND_STMTS)
495 45910515 : return;
496 :
497 128595125 : stmt = gsi_stmt (*gsi);
498 :
499 293861507 : FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
500 : {
501 36671257 : tree var = DEF_FROM_PTR (def_p);
502 :
503 36671257 : if (TREE_CODE (var) != SSA_NAME)
504 153410 : continue;
505 :
506 36517847 : insert_debug_temp_for_var_def (gsi, var);
507 : }
508 : }
509 :
510 : /* Reset all debug stmts that use SSA_NAME(s) defined in STMT. */
511 :
512 : void
513 7735 : reset_debug_uses (gimple *stmt)
514 : {
515 7735 : ssa_op_iter op_iter;
516 7735 : def_operand_p def_p;
517 7735 : imm_use_iterator imm_iter;
518 7735 : gimple *use_stmt;
519 :
520 7735 : if (!MAY_HAVE_DEBUG_BIND_STMTS)
521 257 : return;
522 :
523 19998 : FOR_EACH_PHI_OR_STMT_DEF (def_p, stmt, op_iter, SSA_OP_DEF)
524 : {
525 5042 : tree var = DEF_FROM_PTR (def_p);
526 :
527 5042 : if (TREE_CODE (var) != SSA_NAME)
528 0 : continue;
529 :
530 11372 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, var)
531 : {
532 6330 : if (!gimple_debug_bind_p (use_stmt))
533 6246 : continue;
534 :
535 84 : gimple_debug_bind_reset_value (use_stmt);
536 84 : update_stmt (use_stmt);
537 5042 : }
538 : }
539 : }
540 :
541 : /* Delete SSA DEFs for SSA versions in the TOREMOVE bitmap, removing
542 : dominated stmts before their dominators, so that release_ssa_defs
543 : stands a chance of propagating DEFs into debug bind stmts. */
544 :
545 : void
546 247663 : release_defs_bitset (bitmap toremove)
547 : {
548 247663 : unsigned j;
549 247663 : bitmap_iterator bi;
550 :
551 : /* Performing a topological sort is probably overkill, this will
552 : most likely run in slightly superlinear time, rather than the
553 : pathological quadratic worst case.
554 : But iterate from max SSA name version to min one because
555 : that mimics allocation order during code generation behavior best.
556 : Use an array for this which we compact on-the-fly with a NULL
557 : marker moving towards the end of the vector. */
558 247663 : auto_vec<tree, 16> names;
559 247663 : names.reserve (bitmap_count_bits (toremove) + 1);
560 247663 : names.quick_push (NULL_TREE);
561 2756306 : EXECUTE_IF_SET_IN_BITMAP (toremove, 0, j, bi)
562 2508643 : names.quick_push (ssa_name (j));
563 :
564 247663 : bitmap_tree_view (toremove);
565 761464 : while (!bitmap_empty_p (toremove))
566 : {
567 266138 : j = names.length () - 1;
568 3562237 : for (unsigned i = names.length () - 1; names[i];)
569 : {
570 3296099 : bool remove_now = true;
571 3296099 : tree var = names[i];
572 3296099 : gimple *stmt;
573 3296099 : imm_use_iterator uit;
574 :
575 3992576 : FOR_EACH_IMM_USE_STMT (stmt, uit, var)
576 : {
577 1483933 : ssa_op_iter dit;
578 1483933 : def_operand_p def_p;
579 :
580 : /* We can't propagate PHI nodes into debug stmts. */
581 1483933 : if (gimple_code (stmt) == GIMPLE_PHI
582 1483933 : || is_gimple_debug (stmt))
583 696477 : continue;
584 :
585 : /* If we find another definition to remove that uses
586 : the one we're looking at, defer the removal of this
587 : one, so that it can be propagated into debug stmts
588 : after the other is. */
589 787456 : FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, dit, SSA_OP_DEF)
590 : {
591 787456 : tree odef = DEF_FROM_PTR (def_p);
592 :
593 787456 : if (bitmap_bit_p (toremove, SSA_NAME_VERSION (odef)))
594 : {
595 : remove_now = false;
596 : break;
597 : }
598 : }
599 :
600 787456 : if (!remove_now)
601 : break;
602 3296099 : }
603 :
604 3296099 : if (remove_now)
605 : {
606 2508643 : gimple *def = SSA_NAME_DEF_STMT (var);
607 2508643 : gimple_stmt_iterator gsi = gsi_for_stmt (def);
608 :
609 2508643 : if (gimple_code (def) == GIMPLE_PHI)
610 638572 : remove_phi_node (&gsi, true);
611 : else
612 : {
613 1870071 : gsi_remove (&gsi, true);
614 1870071 : release_defs (def);
615 : }
616 2508643 : bitmap_clear_bit (toremove, SSA_NAME_VERSION (var));
617 : }
618 : else
619 787456 : --i;
620 3296099 : if (--j != i)
621 3103956 : names[i] = names[j];
622 : }
623 : }
624 247663 : bitmap_list_view (toremove);
625 247663 : }
626 :
627 : /* Disable warnings about missing quoting in GCC diagnostics for
628 : the verification errors. Their format strings don't follow GCC
629 : diagnostic conventions and the calls are ultimately followed by
630 : one to internal_error. */
631 : #if __GNUC__ >= 10
632 : # pragma GCC diagnostic push
633 : # pragma GCC diagnostic ignored "-Wformat-diag"
634 : #endif
635 :
636 : /* Verify virtual SSA form. */
637 :
638 : bool
639 230348418 : verify_vssa (basic_block bb_start, tree current_vdef, sbitmap visited)
640 : {
641 230348418 : bool err = false;
642 :
643 230348418 : struct state_t {
644 : basic_block bb;
645 : tree vdef;
646 : } state;
647 :
648 230348418 : auto_vec<state_t, 3> worklist;
649 230348418 : state.bb = bb_start;
650 230348418 : state.vdef = current_vdef;
651 230348418 : worklist.safe_push (state);
652 :
653 2912431616 : while (!worklist.is_empty ())
654 : {
655 2451734780 : const auto &state = worklist.pop ();
656 2451734780 : const auto &bb = state.bb;
657 2451734780 : current_vdef = state.vdef;
658 :
659 2451734780 : if (!bitmap_set_bit (visited, bb->index))
660 113848680 : continue;
661 :
662 : /* Pick up the single virtual PHI def. */
663 2337886100 : gphi *phi = NULL;
664 3005399138 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
665 667513038 : gsi_next (&si))
666 : {
667 667513038 : tree res = gimple_phi_result (si.phi ());
668 1649262895 : if (virtual_operand_p (res))
669 : {
670 314236819 : if (phi)
671 : {
672 0 : error ("multiple virtual PHI nodes in BB %d", bb->index);
673 0 : print_gimple_stmt (stderr, phi, 0);
674 0 : print_gimple_stmt (stderr, si.phi (), 0);
675 0 : err = true;
676 : }
677 : else
678 : phi = si.phi ();
679 : }
680 : }
681 2337886100 : if (phi)
682 : {
683 314236819 : current_vdef = gimple_phi_result (phi);
684 314236819 : if (TREE_CODE (current_vdef) != SSA_NAME)
685 : {
686 0 : error ("virtual definition is not an SSA name");
687 0 : print_gimple_stmt (stderr, phi, 0);
688 0 : err = true;
689 : }
690 : }
691 :
692 : /* Verify stmts. */
693 17899821910 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
694 13224049710 : gsi_next (&gsi))
695 : {
696 13224049710 : gimple *stmt = gsi_stmt (gsi);
697 18637074696 : tree vuse = gimple_vuse (stmt);
698 5346388831 : if (vuse)
699 : {
700 3580774657 : if (vuse != current_vdef)
701 : {
702 0 : error ("stmt with wrong VUSE");
703 0 : print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
704 0 : fprintf (stderr, "expected ");
705 0 : print_generic_expr (stderr, current_vdef);
706 0 : fprintf (stderr, "\n");
707 0 : err = true;
708 : }
709 16804824367 : tree vdef = gimple_vdef (stmt);
710 3580774657 : if (vdef)
711 : {
712 2240136985 : current_vdef = vdef;
713 2240136985 : if (TREE_CODE (current_vdef) != SSA_NAME)
714 : {
715 0 : error ("virtual definition is not an SSA name");
716 0 : print_gimple_stmt (stderr, phi, 0);
717 0 : err = true;
718 : }
719 : }
720 : }
721 : }
722 :
723 : /* Verify destination PHI uses and add successors to worklist. */
724 2337886100 : edge_iterator ei;
725 2337886100 : edge e;
726 5171818293 : FOR_EACH_EDGE (e, ei, bb->succs)
727 : {
728 2833932193 : gphi *phi = get_virtual_phi (e->dest);
729 2833932193 : if (phi
730 2833932193 : && PHI_ARG_DEF_FROM_EDGE (phi, e) != current_vdef)
731 : {
732 0 : error ("PHI node with wrong VUSE on edge from BB %d",
733 0 : e->src->index);
734 0 : print_gimple_stmt (stderr, phi, 0, TDF_VOPS);
735 0 : fprintf (stderr, "expected ");
736 0 : print_generic_expr (stderr, current_vdef);
737 0 : fprintf (stderr, "\n");
738 0 : err = true;
739 : }
740 :
741 : /* Add successor BB along with current vdef to worklist. */
742 2833932193 : if (!bitmap_bit_p (visited, e->dest->index))
743 : {
744 2221386362 : state_t new_state;
745 2221386362 : new_state.bb = e->dest;
746 2221386362 : new_state.vdef = current_vdef;
747 :
748 2221386362 : worklist.safe_push (new_state);
749 : }
750 : }
751 : }
752 230348418 : return err;
753 230348418 : }
754 :
755 : /* Return true if SSA_NAME is malformed and mark it visited.
756 :
757 : IS_VIRTUAL is true if this SSA_NAME was found inside a virtual
758 : operand. */
759 :
760 : static bool
761 14059226480 : verify_ssa_name (tree ssa_name, bool is_virtual)
762 : {
763 14059226480 : if (TREE_CODE (ssa_name) != SSA_NAME)
764 : {
765 0 : error ("expected an SSA_NAME object");
766 0 : return true;
767 : }
768 :
769 14059226480 : if (SSA_NAME_IN_FREE_LIST (ssa_name))
770 : {
771 0 : error ("found an SSA_NAME that had been released into the free pool");
772 0 : return true;
773 : }
774 :
775 14059226480 : if (SSA_NAME_VAR (ssa_name) != NULL_TREE
776 8404896589 : && TREE_TYPE (ssa_name) != TREE_TYPE (SSA_NAME_VAR (ssa_name)))
777 : {
778 0 : error ("type mismatch between an SSA_NAME and its symbol");
779 0 : return true;
780 : }
781 :
782 14059226480 : if (is_virtual && !virtual_operand_p (ssa_name))
783 : {
784 0 : error ("found a virtual definition for a GIMPLE register");
785 0 : return true;
786 : }
787 :
788 14059226480 : if (is_virtual && SSA_NAME_VAR (ssa_name) != gimple_vop (cfun))
789 : {
790 0 : error ("virtual SSA name for non-VOP decl");
791 0 : return true;
792 : }
793 :
794 14059226480 : if (!is_virtual && virtual_operand_p (ssa_name))
795 : {
796 0 : error ("found a real definition for a non-register");
797 0 : return true;
798 : }
799 :
800 14059226480 : if (SSA_NAME_IS_DEFAULT_DEF (ssa_name)
801 14059226480 : && !gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name)))
802 : {
803 0 : error ("found a default name with a non-empty defining statement");
804 0 : return true;
805 : }
806 :
807 : return false;
808 : }
809 :
810 :
811 : /* Return true if the definition of SSA_NAME at block BB is malformed.
812 :
813 : STMT is the statement where SSA_NAME is created.
814 :
815 : DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
816 : version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
817 : it means that the block in that array slot contains the
818 : definition of SSA_NAME.
819 :
820 : IS_VIRTUAL is true if SSA_NAME is created by a VDEF. */
821 :
822 : static bool
823 5977329577 : verify_def (basic_block bb, basic_block *definition_block, tree ssa_name,
824 : gimple *stmt, bool is_virtual)
825 : {
826 5977329577 : if (verify_ssa_name (ssa_name, is_virtual))
827 0 : goto err;
828 :
829 5977329577 : if (SSA_NAME_VAR (ssa_name)
830 3279819806 : && TREE_CODE (SSA_NAME_VAR (ssa_name)) == RESULT_DECL
831 0 : && DECL_BY_REFERENCE (SSA_NAME_VAR (ssa_name)))
832 : {
833 0 : error ("RESULT_DECL should be read only when DECL_BY_REFERENCE is set");
834 0 : goto err;
835 : }
836 :
837 5977329577 : if (definition_block[SSA_NAME_VERSION (ssa_name)])
838 : {
839 0 : error ("SSA_NAME created in two different blocks %i and %i",
840 0 : definition_block[SSA_NAME_VERSION (ssa_name)]->index, bb->index);
841 0 : goto err;
842 : }
843 :
844 5977329577 : definition_block[SSA_NAME_VERSION (ssa_name)] = bb;
845 :
846 5977329577 : if (SSA_NAME_DEF_STMT (ssa_name) != stmt)
847 : {
848 0 : error ("SSA_NAME_DEF_STMT is wrong");
849 0 : fprintf (stderr, "Expected definition statement:\n");
850 0 : print_gimple_stmt (stderr, SSA_NAME_DEF_STMT (ssa_name), 4, TDF_VOPS);
851 0 : fprintf (stderr, "\nActual definition statement:\n");
852 0 : print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
853 0 : goto err;
854 : }
855 :
856 : return false;
857 :
858 0 : err:
859 0 : fprintf (stderr, "while verifying SSA_NAME ");
860 0 : print_generic_expr (stderr, ssa_name);
861 0 : fprintf (stderr, " in statement\n");
862 0 : print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
863 :
864 0 : return true;
865 : }
866 :
867 :
868 : /* Return true if the use of SSA_NAME at statement STMT in block BB is
869 : malformed.
870 :
871 : DEF_BB is the block where SSA_NAME was found to be created.
872 :
873 : IDOM contains immediate dominator information for the flowgraph.
874 :
875 : CHECK_ABNORMAL is true if the caller wants to check whether this use
876 : is flowing through an abnormal edge (only used when checking PHI
877 : arguments).
878 :
879 : If NAMES_DEFINED_IN_BB is not NULL, it contains a bitmap of ssa names
880 : that are defined before STMT in basic block BB. */
881 :
882 : static bool
883 11480535755 : verify_use (basic_block bb, basic_block def_bb, use_operand_p use_p,
884 : gimple *stmt, bool check_abnormal, bitmap names_defined_in_bb)
885 : {
886 11480535755 : bool err = false;
887 11480535755 : tree ssa_name = USE_FROM_PTR (use_p);
888 :
889 11480535755 : if (!TREE_VISITED (ssa_name))
890 6374339177 : if (verify_imm_links (stderr, ssa_name))
891 11480535755 : err = true;
892 :
893 11480535755 : TREE_VISITED (ssa_name) = 1;
894 :
895 11480535755 : if (gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name))
896 11480535755 : && SSA_NAME_IS_DEFAULT_DEF (ssa_name))
897 : ; /* Default definitions have empty statements. Nothing to do. */
898 9859094516 : else if (!def_bb)
899 : {
900 0 : error ("missing definition");
901 0 : err = true;
902 : }
903 9859094516 : else if (bb != def_bb
904 9859094516 : && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
905 : {
906 0 : error ("definition in block %i does not dominate use in block %i",
907 : def_bb->index, bb->index);
908 0 : err = true;
909 : }
910 9859094516 : else if (bb == def_bb
911 9859094516 : && names_defined_in_bb != NULL
912 15744371768 : && !bitmap_bit_p (names_defined_in_bb, SSA_NAME_VERSION (ssa_name)))
913 : {
914 0 : error ("definition in block %i follows the use", def_bb->index);
915 0 : err = true;
916 : }
917 :
918 11480535755 : if (check_abnormal
919 11488272319 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name))
920 : {
921 0 : error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set");
922 0 : err = true;
923 : }
924 :
925 : /* Make sure the use is in an appropriate list by checking the previous
926 : element to make sure it's the same. */
927 11480535755 : if (use_p->prev == NULL)
928 : {
929 0 : error ("no immediate_use list");
930 0 : err = true;
931 : }
932 : else
933 : {
934 11480535755 : tree listvar;
935 11480535755 : if (use_p->prev->use == NULL)
936 6374339177 : listvar = use_p->prev->loc.ssa_name;
937 : else
938 5106196578 : listvar = USE_FROM_PTR (use_p->prev);
939 11480535755 : if (listvar != ssa_name)
940 : {
941 0 : error ("wrong immediate use list");
942 0 : err = true;
943 : }
944 : }
945 :
946 11480535755 : if (err)
947 : {
948 0 : fprintf (stderr, "for SSA_NAME: ");
949 0 : print_generic_expr (stderr, ssa_name, TDF_VOPS);
950 0 : fprintf (stderr, " in statement:\n");
951 0 : print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
952 : }
953 :
954 11480535755 : return err;
955 : }
956 :
957 :
958 : /* Return true if any of the arguments for PHI node PHI at block BB is
959 : malformed.
960 :
961 : DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
962 : version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
963 : it means that the block in that array slot contains the
964 : definition of SSA_NAME. */
965 :
966 : static bool
967 667524751 : verify_phi_args (gphi *phi, basic_block bb, basic_block *definition_block)
968 : {
969 667524751 : edge e;
970 667524751 : bool err = false;
971 667524751 : size_t i, phi_num_args = gimple_phi_num_args (phi);
972 :
973 1335049502 : if (EDGE_COUNT (bb->preds) != phi_num_args)
974 : {
975 0 : error ("incoming edge count does not match number of PHI arguments");
976 0 : err = true;
977 0 : goto error;
978 : }
979 :
980 2372390387 : for (i = 0; i < phi_num_args; i++)
981 : {
982 1704865636 : use_operand_p op_p = gimple_phi_arg_imm_use_ptr (phi, i);
983 1704865636 : tree op = USE_FROM_PTR (op_p);
984 :
985 1704865636 : e = EDGE_PRED (bb, i);
986 :
987 1704865636 : if (op == NULL_TREE)
988 : {
989 0 : error ("PHI argument is missing for edge %d->%d",
990 0 : e->src->index,
991 0 : e->dest->index);
992 0 : err = true;
993 0 : goto error;
994 : }
995 :
996 1704865636 : if (TREE_CODE (op) != SSA_NAME && !is_gimple_min_invariant (op))
997 : {
998 0 : error ("PHI argument is not SSA_NAME, or invariant");
999 0 : err = true;
1000 : }
1001 :
1002 1704865636 : if ((e->flags & EDGE_ABNORMAL) && TREE_CODE (op) != SSA_NAME)
1003 : {
1004 0 : error ("PHI argument on abnormal edge is not SSA_NAME");
1005 0 : err = true;
1006 : }
1007 :
1008 1704865636 : if (TREE_CODE (op) == SSA_NAME)
1009 : {
1010 2945960310 : err = verify_ssa_name (op, virtual_operand_p (gimple_phi_result (phi)));
1011 1472980155 : err |= verify_use (e->src, definition_block[SSA_NAME_VERSION (op)],
1012 1472980155 : op_p, phi, e->flags & EDGE_ABNORMAL, NULL);
1013 : }
1014 :
1015 1704865636 : if (TREE_CODE (op) == ADDR_EXPR)
1016 : {
1017 12064718 : tree base = TREE_OPERAND (op, 0);
1018 16438924 : while (handled_component_p (base))
1019 4374206 : base = TREE_OPERAND (base, 0);
1020 12064718 : if ((VAR_P (base)
1021 : || TREE_CODE (base) == PARM_DECL
1022 : || TREE_CODE (base) == RESULT_DECL)
1023 5628420 : && !TREE_ADDRESSABLE (base))
1024 : {
1025 0 : error ("address taken, but ADDRESSABLE bit not set");
1026 0 : err = true;
1027 : }
1028 : }
1029 :
1030 1704865636 : if (e->dest != bb)
1031 : {
1032 0 : error ("wrong edge %d->%d for PHI argument",
1033 0 : e->src->index, e->dest->index);
1034 0 : err = true;
1035 : }
1036 :
1037 1704865636 : if (err)
1038 : {
1039 0 : fprintf (stderr, "PHI argument\n");
1040 0 : print_generic_stmt (stderr, op, TDF_VOPS);
1041 0 : goto error;
1042 : }
1043 : }
1044 :
1045 667524751 : error:
1046 667524751 : if (err)
1047 : {
1048 0 : fprintf (stderr, "for PHI node\n");
1049 0 : print_gimple_stmt (stderr, phi, 0, TDF_VOPS|TDF_MEMSYMS);
1050 : }
1051 :
1052 :
1053 667524751 : return err;
1054 : }
1055 :
1056 :
1057 : /* Verify common invariants in the SSA web.
1058 : TODO: verify the variable annotations. */
1059 :
1060 : DEBUG_FUNCTION void
1061 230454343 : verify_ssa (bool check_modified_stmt, bool check_ssa_operands)
1062 : {
1063 230454343 : basic_block bb;
1064 460908686 : basic_block *definition_block = XCNEWVEC (basic_block, num_ssa_names);
1065 230454343 : ssa_op_iter iter;
1066 230454343 : tree op;
1067 230454343 : enum dom_state orig_dom_state = dom_info_state (CDI_DOMINATORS);
1068 230454343 : auto_bitmap names_defined_in_bb;
1069 :
1070 230454343 : gcc_assert (!need_ssa_update_p (cfun));
1071 :
1072 230454343 : timevar_push (TV_TREE_SSA_VERIFY);
1073 :
1074 230454343 : {
1075 : /* Keep track of SSA names present in the IL. */
1076 230454343 : size_t i;
1077 230454343 : tree name;
1078 230454343 : hash_map <void *, tree> ssa_info;
1079 :
1080 9002711158 : FOR_EACH_SSA_NAME (i, name, cfun)
1081 : {
1082 6608916748 : gimple *stmt;
1083 6608916748 : TREE_VISITED (name) = 0;
1084 :
1085 13217833496 : verify_ssa_name (name, virtual_operand_p (name));
1086 :
1087 6608916748 : stmt = SSA_NAME_DEF_STMT (name);
1088 6608916748 : if (!gimple_nop_p (stmt))
1089 : {
1090 5977329577 : basic_block bb = gimple_bb (stmt);
1091 5977329577 : if (verify_def (bb, definition_block,
1092 : name, stmt, virtual_operand_p (name)))
1093 0 : goto err;
1094 : }
1095 :
1096 6608916748 : void *info = NULL;
1097 6608916748 : if (POINTER_TYPE_P (TREE_TYPE (name)))
1098 1181537030 : info = SSA_NAME_PTR_INFO (name);
1099 5427379718 : else if (INTEGRAL_TYPE_P (TREE_TYPE (name)))
1100 2273332325 : info = SSA_NAME_RANGE_INFO (name);
1101 6608916748 : if (info)
1102 : {
1103 1386727617 : bool existed;
1104 1386727617 : tree &val = ssa_info.get_or_insert (info, &existed);
1105 1386727617 : if (existed)
1106 : {
1107 0 : error ("shared SSA name info");
1108 0 : print_generic_expr (stderr, val);
1109 0 : fprintf (stderr, " and ");
1110 0 : print_generic_expr (stderr, name);
1111 0 : fprintf (stderr, "\n");
1112 0 : goto err;
1113 : }
1114 : else
1115 1386727617 : val = name;
1116 : }
1117 : }
1118 0 : }
1119 :
1120 230454343 : calculate_dominance_info (CDI_DOMINATORS);
1121 :
1122 : /* Now verify all the uses and make sure they agree with the definitions
1123 : found in the previous pass. */
1124 2112123954 : FOR_EACH_BB_FN (bb, cfun)
1125 : {
1126 1881669611 : edge e;
1127 1881669611 : edge_iterator ei;
1128 :
1129 : /* Make sure that all edges have a clear 'aux' field. */
1130 4489582490 : FOR_EACH_EDGE (e, ei, bb->preds)
1131 : {
1132 2607912879 : if (e->aux)
1133 : {
1134 0 : error ("AUX pointer initialized for edge %d->%d", e->src->index,
1135 0 : e->dest->index);
1136 0 : goto err;
1137 : }
1138 : }
1139 :
1140 : /* Verify the arguments for every PHI node in the block. */
1141 2549194362 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1142 : {
1143 667524751 : gphi *phi = gsi.phi ();
1144 667524751 : if (verify_phi_args (phi, bb, definition_block))
1145 0 : goto err;
1146 :
1147 667524751 : bitmap_set_bit (names_defined_in_bb,
1148 667524751 : SSA_NAME_VERSION (gimple_phi_result (phi)));
1149 : }
1150 :
1151 : /* Now verify all the uses and vuses in every statement of the block. */
1152 16987541703 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1153 13224202481 : gsi_next (&gsi))
1154 : {
1155 13224202481 : gimple *stmt = gsi_stmt (gsi);
1156 13224202481 : use_operand_p use_p;
1157 :
1158 26373271618 : if (check_modified_stmt && gimple_modified_p (stmt))
1159 : {
1160 0 : error ("stmt (%p) marked modified after optimization pass: ",
1161 : (void *)stmt);
1162 0 : print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
1163 0 : goto err;
1164 : }
1165 :
1166 13224202481 : if (check_ssa_operands && verify_ssa_operands (cfun, stmt))
1167 : {
1168 0 : print_gimple_stmt (stderr, stmt, 0, TDF_VOPS);
1169 0 : goto err;
1170 : }
1171 :
1172 13224202481 : if (gimple_debug_bind_p (stmt)
1173 6965475480 : && !gimple_debug_bind_has_value_p (stmt))
1174 2322384010 : continue;
1175 :
1176 20909374071 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE|SSA_OP_VUSE)
1177 : {
1178 10007555600 : op = USE_FROM_PTR (use_p);
1179 10007555600 : if (verify_use (bb, definition_block[SSA_NAME_VERSION (op)],
1180 : use_p, stmt, false, names_defined_in_bb))
1181 0 : goto err;
1182 : }
1183 :
1184 16199742166 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_ALL_DEFS)
1185 : {
1186 5297923695 : if (SSA_NAME_DEF_STMT (op) != stmt)
1187 : {
1188 0 : error ("SSA_NAME_DEF_STMT is wrong");
1189 0 : fprintf (stderr, "Expected definition statement:\n");
1190 0 : print_gimple_stmt (stderr, stmt, 4, TDF_VOPS);
1191 0 : fprintf (stderr, "\nActual definition statement:\n");
1192 0 : print_gimple_stmt (stderr, SSA_NAME_DEF_STMT (op),
1193 : 4, TDF_VOPS);
1194 0 : goto err;
1195 : }
1196 5297923695 : bitmap_set_bit (names_defined_in_bb, SSA_NAME_VERSION (op));
1197 : }
1198 : }
1199 :
1200 1881669611 : bitmap_clear (names_defined_in_bb);
1201 : }
1202 :
1203 230454343 : free (definition_block);
1204 :
1205 230454343 : if (gimple_vop (cfun)
1206 230454343 : && ssa_default_def (cfun, gimple_vop (cfun)))
1207 : {
1208 230348418 : auto_sbitmap visited (last_basic_block_for_fn (cfun) + 1);
1209 230348418 : bitmap_clear (visited);
1210 230348418 : if (verify_vssa (ENTRY_BLOCK_PTR_FOR_FN (cfun),
1211 : ssa_default_def (cfun, gimple_vop (cfun)), visited))
1212 0 : goto err;
1213 230348418 : }
1214 :
1215 : /* Restore the dominance information to its prior known state, so
1216 : that we do not perturb the compiler's subsequent behavior. */
1217 230454343 : if (orig_dom_state == DOM_NONE)
1218 74878247 : free_dominance_info (CDI_DOMINATORS);
1219 : else
1220 155576096 : set_dom_info_availability (CDI_DOMINATORS, orig_dom_state);
1221 :
1222 230454343 : timevar_pop (TV_TREE_SSA_VERIFY);
1223 230454343 : return;
1224 :
1225 0 : err:
1226 0 : internal_error ("verify_ssa failed");
1227 230454343 : }
1228 :
1229 : #if __GNUC__ >= 10
1230 : # pragma GCC diagnostic pop
1231 : #endif
1232 :
1233 : /* Initialize global DFA and SSA structures.
1234 : If SIZE is non-zero allocated ssa names array of a given size. */
1235 :
1236 : void
1237 3467190 : init_tree_ssa (struct function *fn, int size)
1238 : {
1239 3467190 : fn->gimple_df = ggc_cleared_alloc<gimple_df> ();
1240 3467190 : fn->gimple_df->default_defs = hash_table<ssa_name_hasher>::create_ggc (20);
1241 3467190 : pt_solution_reset (&fn->gimple_df->escaped);
1242 3467190 : pt_solution_reset (&fn->gimple_df->escaped_return);
1243 3467190 : init_ssanames (fn, size);
1244 3467190 : }
1245 :
1246 : /* Deallocate memory associated with SSA data structures for FNDECL. */
1247 :
1248 : void
1249 3357910 : delete_tree_ssa (struct function *fn)
1250 : {
1251 3357910 : fini_ssanames (fn);
1252 :
1253 : /* We no longer maintain the SSA operand cache at this point. */
1254 3357910 : if (ssa_operands_active (fn))
1255 3333392 : fini_ssa_operands (fn);
1256 :
1257 3357910 : fn->gimple_df->default_defs->empty ();
1258 3357910 : fn->gimple_df->default_defs = NULL;
1259 3357910 : pt_solution_reset (&fn->gimple_df->escaped);
1260 3357910 : pt_solution_reset (&fn->gimple_df->escaped_return);
1261 3357910 : if (fn->gimple_df->decls_to_pointers != NULL)
1262 43443 : delete fn->gimple_df->decls_to_pointers;
1263 3357910 : fn->gimple_df->decls_to_pointers = NULL;
1264 3357910 : fn->gimple_df = NULL;
1265 :
1266 : /* We no longer need the edge variable maps. */
1267 3357910 : redirect_edge_var_map_empty ();
1268 3357910 : }
1269 :
1270 : /* Return true if EXPR is a useless type conversion, otherwise return
1271 : false. */
1272 :
1273 : bool
1274 1008289411 : tree_ssa_useless_type_conversion (tree expr)
1275 : {
1276 1008289411 : tree outer_type, inner_type;
1277 :
1278 : /* If we have an assignment that merely uses a NOP_EXPR to change
1279 : the top of the RHS to the type of the LHS and the type conversion
1280 : is "safe", then strip away the type conversion so that we can
1281 : enter LHS = RHS into the const_and_copies table. */
1282 1008289411 : if (!CONVERT_EXPR_P (expr)
1283 931586642 : && TREE_CODE (expr) != VIEW_CONVERT_EXPR
1284 930524444 : && TREE_CODE (expr) != NON_LVALUE_EXPR)
1285 : return false;
1286 :
1287 78340140 : outer_type = TREE_TYPE (expr);
1288 78340140 : inner_type = TREE_TYPE (TREE_OPERAND (expr, 0));
1289 :
1290 78340140 : if (inner_type == error_mark_node)
1291 : return false;
1292 :
1293 78340097 : return useless_type_conversion_p (outer_type, inner_type);
1294 : }
1295 :
1296 : /* Strip conversions from EXP according to
1297 : tree_ssa_useless_type_conversion and return the resulting
1298 : expression. */
1299 :
1300 : tree
1301 972637752 : tree_ssa_strip_useless_type_conversions (tree exp)
1302 : {
1303 995021056 : while (tree_ssa_useless_type_conversion (exp))
1304 22383304 : exp = TREE_OPERAND (exp, 0);
1305 972637752 : return exp;
1306 : }
1307 :
1308 : /* Return true if T, as SSA_NAME, has an implicit default defined value. */
1309 :
1310 : bool
1311 394828936 : ssa_defined_default_def_p (tree t)
1312 : {
1313 394828936 : tree var = SSA_NAME_VAR (t);
1314 :
1315 153077602 : if (!var)
1316 : ;
1317 : /* Parameters get their initial value from the function entry. */
1318 153077602 : else if (TREE_CODE (var) == PARM_DECL)
1319 : return true;
1320 : /* When returning by reference the return address is actually a hidden
1321 : parameter. */
1322 118990477 : else if (TREE_CODE (var) == RESULT_DECL && DECL_BY_REFERENCE (var))
1323 : return true;
1324 : /* Hard register variables get their initial value from the ether. */
1325 118767856 : else if (VAR_P (var) && DECL_HARD_REGISTER (var))
1326 0 : return true;
1327 :
1328 : return false;
1329 : }
1330 :
1331 :
1332 : /* Return true if T, an SSA_NAME, has an undefined value. PARTIAL is what
1333 : should be returned if the value is only partially undefined. */
1334 :
1335 : bool
1336 392393946 : ssa_undefined_value_p (tree t, bool partial)
1337 : {
1338 392393946 : gimple *def_stmt;
1339 :
1340 784787892 : gcc_checking_assert (!virtual_operand_p (t));
1341 :
1342 392393946 : if (ssa_defined_default_def_p (t))
1343 : return false;
1344 :
1345 : /* The value is undefined iff its definition statement is empty. */
1346 360246102 : def_stmt = SSA_NAME_DEF_STMT (t);
1347 360246102 : if (gimple_nop_p (def_stmt))
1348 : return true;
1349 :
1350 : /* The value is undefined if the definition statement is a call
1351 : to .DEFERRED_INIT function. Except for AUTO_INIT_CXX26 which is
1352 : undefined but needs to have the same value in all uses. */
1353 349951504 : if (gimple_call_internal_p (def_stmt, IFN_DEFERRED_INIT))
1354 : {
1355 40240 : if (tree_to_uhwi (gimple_call_arg (def_stmt, 1)) & AUTO_INIT_CXX26)
1356 : return false;
1357 : else
1358 : return true;
1359 : }
1360 :
1361 : /* The value is partially undefined if the definition statement is
1362 : a REALPART_EXPR or IMAGPART_EXPR and its operand is defined by
1363 : the call to .DEFERRED_INIT function. This is for handling the
1364 : following case:
1365 :
1366 : 1 typedef _Complex float C;
1367 : 2 C foo (int cond)
1368 : 3 {
1369 : 4 C f;
1370 : 5 __imag__ f = 0;
1371 : 6 if (cond)
1372 : 7 {
1373 : 8 __real__ f = 1;
1374 : 9 return f;
1375 : 10 }
1376 : 11 return f;
1377 : 12 }
1378 :
1379 : with -ftrivial-auto-var-init, compiler will insert the following
1380 : artificial initialization:
1381 : f = .DEFERRED_INIT (f, 2);
1382 : _1 = REALPART_EXPR <f>;
1383 :
1384 : we should treat the definition _1 = REALPART_EXPR <f> as undefined. */
1385 53618199 : if (partial && is_gimple_assign (def_stmt)
1386 390934760 : && (gimple_assign_rhs_code (def_stmt) == REALPART_EXPR
1387 40707382 : || gimple_assign_rhs_code (def_stmt) == IMAGPART_EXPR))
1388 : {
1389 674870 : tree real_imag_part = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
1390 674870 : if (TREE_CODE (real_imag_part) == SSA_NAME
1391 674870 : && gimple_call_internal_p (SSA_NAME_DEF_STMT (real_imag_part),
1392 : IFN_DEFERRED_INIT))
1393 : return true;
1394 : }
1395 :
1396 : /* Check if the complex was not only partially defined. */
1397 53618054 : if (partial && is_gimple_assign (def_stmt)
1398 390934470 : && gimple_assign_rhs_code (def_stmt) == COMPLEX_EXPR)
1399 : {
1400 18393 : tree rhs1, rhs2;
1401 :
1402 18393 : rhs1 = gimple_assign_rhs1 (def_stmt);
1403 18393 : rhs2 = gimple_assign_rhs2 (def_stmt);
1404 18055 : return (TREE_CODE (rhs1) == SSA_NAME && ssa_undefined_value_p (rhs1))
1405 36407 : || (TREE_CODE (rhs2) == SSA_NAME && ssa_undefined_value_p (rhs2));
1406 : }
1407 : return false;
1408 : }
1409 :
1410 :
1411 : /* Return TRUE iff there are any non-PHI uses of VAR that dominate the
1412 : end of BB. If we return TRUE and BB is a loop header, then VAR we
1413 : be assumed to be defined within the loop, even if it is marked as
1414 : maybe-undefined. */
1415 :
1416 : bool
1417 330550 : ssa_name_any_use_dominates_bb_p (tree var, basic_block bb)
1418 : {
1419 330550 : imm_use_iterator iter;
1420 330550 : use_operand_p use_p;
1421 1081334 : FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1422 : {
1423 774592 : if (is_a <gphi *> (USE_STMT (use_p))
1424 774592 : || is_gimple_debug (USE_STMT (use_p)))
1425 703960 : continue;
1426 70632 : basic_block dombb = gimple_bb (USE_STMT (use_p));
1427 70632 : if (dominated_by_p (CDI_DOMINATORS, bb, dombb))
1428 23808 : return true;
1429 23808 : }
1430 :
1431 306742 : return false;
1432 : }
1433 :
1434 : /* Mark as maybe_undef any SSA_NAMEs that are unsuitable as ivopts
1435 : candidates for potentially involving undefined behavior. */
1436 :
1437 : void
1438 12524859 : mark_ssa_maybe_undefs (void)
1439 : {
1440 12524859 : auto_vec<tree> queue;
1441 :
1442 : /* Scan all SSA_NAMEs, marking the definitely-undefined ones as
1443 : maybe-undefined and queuing them for propagation, while clearing
1444 : the mark on others. */
1445 12524859 : unsigned int i;
1446 12524859 : tree var;
1447 617521744 : FOR_EACH_SSA_NAME (i, var, cfun)
1448 : {
1449 407881223 : if (SSA_NAME_IS_VIRTUAL_OPERAND (var)
1450 407881223 : || !ssa_undefined_value_p (var, false))
1451 399711671 : ssa_name_set_maybe_undef (var, false);
1452 : else
1453 : {
1454 8169552 : ssa_name_set_maybe_undef (var);
1455 8169552 : queue.safe_push (var);
1456 8169552 : if (dump_file && (dump_flags & TDF_DETAILS))
1457 138 : fprintf (dump_file, "marking _%i as maybe-undef\n",
1458 69 : SSA_NAME_VERSION (var));
1459 : }
1460 : }
1461 :
1462 : /* Now propagate maybe-undefined from a DEF to any other PHI that
1463 : uses it, as long as there isn't any intervening use of DEF. */
1464 22203308 : while (!queue.is_empty ())
1465 : {
1466 8473118 : var = queue.pop ();
1467 8473118 : imm_use_iterator iter;
1468 8473118 : use_operand_p use_p;
1469 9997384 : FOR_EACH_IMM_USE_FAST (use_p, iter, var)
1470 : {
1471 : /* Any uses of VAR that aren't PHI args imply VAR must be
1472 : defined, otherwise undefined behavior would have been
1473 : definitely invoked. Only PHI args may hold
1474 : maybe-undefined values without invoking undefined
1475 : behavior for that reason alone. */
1476 1524266 : if (!is_a <gphi *> (USE_STMT (use_p)))
1477 1220700 : continue;
1478 544701 : gphi *phi = as_a <gphi *> (USE_STMT (use_p));
1479 :
1480 544701 : tree def = gimple_phi_result (phi);
1481 544701 : if (ssa_name_maybe_undef_p (def))
1482 222980 : continue;
1483 :
1484 : /* Look for any uses of the maybe-unused SSA_NAME that
1485 : dominates the block that reaches the incoming block
1486 : corresponding to the PHI arg in which it is mentioned.
1487 : That means we can assume the SSA_NAME is defined in that
1488 : path, so we only mark a PHI result as maybe-undef if we
1489 : find an unused reaching SSA_NAME. */
1490 321721 : int idx = phi_arg_index_from_use (use_p);
1491 321721 : basic_block bb = gimple_phi_arg_edge (phi, idx)->src;
1492 321721 : if (ssa_name_any_use_dominates_bb_p (var, bb))
1493 18155 : continue;
1494 :
1495 303566 : ssa_name_set_maybe_undef (def);
1496 303566 : queue.safe_push (def);
1497 303566 : if (dump_file && (dump_flags & TDF_DETAILS))
1498 0 : fprintf (dump_file, "marking _%i as maybe-undef because of _%i\n",
1499 0 : SSA_NAME_VERSION (def), SSA_NAME_VERSION (var));
1500 8473118 : }
1501 : }
1502 12524859 : }
1503 :
1504 :
1505 : /* If necessary, rewrite the base of the reference tree *TP from
1506 : a MEM_REF to a plain or converted symbol. */
1507 :
1508 : static void
1509 24522828 : maybe_rewrite_mem_ref_base (tree *tp, bitmap suitable_for_renaming)
1510 : {
1511 24522828 : tree sym;
1512 :
1513 27958723 : while (handled_component_p (*tp))
1514 3435895 : tp = &TREE_OPERAND (*tp, 0);
1515 24522828 : if (TREE_CODE (*tp) == MEM_REF
1516 1762375 : && TREE_CODE (TREE_OPERAND (*tp, 0)) == ADDR_EXPR
1517 316219 : && (sym = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0))
1518 316219 : && DECL_P (sym)
1519 315774 : && !TREE_ADDRESSABLE (sym)
1520 183402 : && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym))
1521 58744 : && is_gimple_reg_type (TREE_TYPE (*tp))
1522 24581562 : && ! VOID_TYPE_P (TREE_TYPE (*tp)))
1523 : {
1524 58734 : if (VECTOR_TYPE_P (TREE_TYPE (sym))
1525 1438 : && useless_type_conversion_p (TREE_TYPE (*tp),
1526 1438 : TREE_TYPE (TREE_TYPE (sym)))
1527 59114 : && multiple_p (mem_ref_offset (*tp),
1528 380 : wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (*tp)))))
1529 : {
1530 758 : *tp = build3 (BIT_FIELD_REF, TREE_TYPE (*tp), sym,
1531 379 : TYPE_SIZE (TREE_TYPE (*tp)),
1532 : int_const_binop (MULT_EXPR,
1533 758 : bitsize_int (BITS_PER_UNIT),
1534 379 : TREE_OPERAND (*tp, 1)));
1535 : }
1536 58355 : else if (TREE_CODE (TREE_TYPE (sym)) == COMPLEX_TYPE
1537 1056 : && useless_type_conversion_p (TREE_TYPE (*tp),
1538 1056 : TREE_TYPE (TREE_TYPE (sym)))
1539 58360 : && (integer_zerop (TREE_OPERAND (*tp, 1))
1540 3 : || tree_int_cst_equal (TREE_OPERAND (*tp, 1),
1541 3 : TYPE_SIZE_UNIT (TREE_TYPE (*tp)))))
1542 : {
1543 6 : *tp = build1 (integer_zerop (TREE_OPERAND (*tp, 1))
1544 : ? REALPART_EXPR : IMAGPART_EXPR,
1545 4 : TREE_TYPE (*tp), sym);
1546 : }
1547 58351 : else if (integer_zerop (TREE_OPERAND (*tp, 1))
1548 58351 : && DECL_SIZE (sym) == TYPE_SIZE (TREE_TYPE (*tp)))
1549 : {
1550 57987 : if (!useless_type_conversion_p (TREE_TYPE (*tp),
1551 57987 : TREE_TYPE (sym)))
1552 12450 : *tp = build1 (VIEW_CONVERT_EXPR,
1553 12450 : TREE_TYPE (*tp), sym);
1554 : else
1555 45537 : *tp = sym;
1556 : }
1557 364 : else if (DECL_SIZE (sym)
1558 364 : && TREE_CODE (DECL_SIZE (sym)) == INTEGER_CST
1559 364 : && (known_subrange_p
1560 364 : (mem_ref_offset (*tp),
1561 364 : wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (*tp))),
1562 364 : 0, wi::to_offset (DECL_SIZE_UNIT (sym))))
1563 364 : && (! INTEGRAL_TYPE_P (TREE_TYPE (*tp))
1564 347 : || (wi::to_offset (TYPE_SIZE (TREE_TYPE (*tp)))
1565 694 : == TYPE_PRECISION (TREE_TYPE (*tp))))
1566 364 : && (! INTEGRAL_TYPE_P (TREE_TYPE (sym))
1567 169 : || type_has_mode_precision_p (TREE_TYPE (sym)))
1568 728 : && wi::umod_trunc (wi::to_offset (TYPE_SIZE (TREE_TYPE (*tp))),
1569 728 : BITS_PER_UNIT) == 0)
1570 : {
1571 728 : *tp = build3 (BIT_FIELD_REF, TREE_TYPE (*tp), sym,
1572 364 : TYPE_SIZE (TREE_TYPE (*tp)),
1573 : wide_int_to_tree (bitsizetype,
1574 364 : mem_ref_offset (*tp)
1575 728 : << LOG2_BITS_PER_UNIT));
1576 : }
1577 : }
1578 24522828 : }
1579 :
1580 : /* For a tree REF return its base if it is the base of a MEM_REF
1581 : that cannot be rewritten into SSA form. Otherwise return NULL_TREE. */
1582 :
1583 : static tree
1584 309512074 : non_rewritable_mem_ref_base (tree ref)
1585 : {
1586 309512074 : tree base;
1587 :
1588 : /* A plain decl does not need it set. */
1589 309512074 : if (DECL_P (ref))
1590 : return NULL_TREE;
1591 :
1592 278575871 : switch (TREE_CODE (ref))
1593 : {
1594 2931784 : case REALPART_EXPR:
1595 2931784 : case IMAGPART_EXPR:
1596 2931784 : case BIT_FIELD_REF:
1597 2931784 : if (DECL_P (TREE_OPERAND (ref, 0)))
1598 : return NULL_TREE;
1599 : break;
1600 2500052 : case VIEW_CONVERT_EXPR:
1601 2500052 : if (DECL_P (TREE_OPERAND (ref, 0)))
1602 : {
1603 1087559 : if (TYPE_SIZE (TREE_TYPE (ref))
1604 1087559 : != TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 0))))
1605 676 : return TREE_OPERAND (ref, 0);
1606 : return NULL_TREE;
1607 : }
1608 : break;
1609 : /* We would need to rewrite ARRAY_REFs or COMPONENT_REFs and even
1610 : more so multiple levels of handled components. */
1611 276994904 : default:;
1612 : }
1613 :
1614 276994904 : base = ref;
1615 :
1616 : /* But watch out for MEM_REFs we cannot lower to a
1617 : VIEW_CONVERT_EXPR or a BIT_FIELD_REF. */
1618 276994904 : if (TREE_CODE (base) == MEM_REF
1619 276994904 : && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
1620 : {
1621 6522868 : tree decl = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
1622 6522868 : if (! DECL_P (decl))
1623 : return NULL_TREE;
1624 6476212 : if (! is_gimple_reg_type (TREE_TYPE (base))
1625 6293780 : || VOID_TYPE_P (TREE_TYPE (base))
1626 12769992 : || TREE_THIS_VOLATILE (decl) != TREE_THIS_VOLATILE (base))
1627 : return decl;
1628 6292549 : if ((VECTOR_TYPE_P (TREE_TYPE (decl))
1629 6290114 : || TREE_CODE (TREE_TYPE (decl)) == COMPLEX_TYPE)
1630 3901 : && useless_type_conversion_p (TREE_TYPE (base),
1631 3901 : TREE_TYPE (TREE_TYPE (decl)))
1632 571 : && known_ge (mem_ref_offset (base), 0)
1633 524 : && known_gt (wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (decl))),
1634 : mem_ref_offset (base))
1635 6293067 : && multiple_p (mem_ref_offset (base),
1636 518 : wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (base)))))
1637 516 : return NULL_TREE;
1638 : /* For same sizes and zero offset we can use a VIEW_CONVERT_EXPR. */
1639 6292033 : if (integer_zerop (TREE_OPERAND (base, 1))
1640 6292033 : && DECL_SIZE (decl) == TYPE_SIZE (TREE_TYPE (base)))
1641 : return NULL_TREE;
1642 : /* For integral typed extracts we can use a BIT_FIELD_REF. */
1643 5618586 : if (DECL_SIZE (decl)
1644 5614223 : && TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST
1645 5614223 : && (known_subrange_p
1646 5614223 : (mem_ref_offset (base),
1647 5614223 : wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (base))),
1648 5614223 : 0, wi::to_poly_offset (DECL_SIZE_UNIT (decl))))
1649 : /* ??? We can't handle bitfield precision extracts without
1650 : either using an alternate type for the BIT_FIELD_REF and
1651 : then doing a conversion or possibly adjusting the offset
1652 : according to endianness. */
1653 5611751 : && (! INTEGRAL_TYPE_P (TREE_TYPE (base))
1654 2998099 : || (wi::to_offset (TYPE_SIZE (TREE_TYPE (base)))
1655 5996198 : == TYPE_PRECISION (TREE_TYPE (base))))
1656 : /* ??? Likewise for extracts from bitfields, we'd have
1657 : to pun the base object to a size precision mode first. */
1658 5604708 : && (! INTEGRAL_TYPE_P (TREE_TYPE (decl))
1659 42201 : || type_has_mode_precision_p (TREE_TYPE (decl)))
1660 11187541 : && wi::umod_trunc (wi::to_offset (TYPE_SIZE (TREE_TYPE (base))),
1661 11137910 : BITS_PER_UNIT) == 0)
1662 5568955 : return NULL_TREE;
1663 49631 : return decl;
1664 : }
1665 :
1666 : /* We cannot rewrite a decl in the base. */
1667 270472036 : base = get_base_address (ref);
1668 270472036 : if (DECL_P (base))
1669 : return base;
1670 :
1671 : /* We cannot rewrite TARGET_MEM_REFs. */
1672 249947758 : else if (TREE_CODE (base) == TARGET_MEM_REF
1673 249947758 : && TREE_CODE (TREE_OPERAND (base, 0)) == ADDR_EXPR)
1674 : {
1675 0 : tree decl = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
1676 0 : if (! DECL_P (decl))
1677 : return NULL_TREE;
1678 0 : return decl;
1679 : }
1680 :
1681 : return NULL_TREE;
1682 : }
1683 :
1684 : /* For an lvalue tree LHS return true if it cannot be rewritten into SSA form.
1685 : Otherwise return true. */
1686 :
1687 : static bool
1688 108549811 : non_rewritable_lvalue_p (tree lhs)
1689 : {
1690 : /* A plain decl is always rewritable. */
1691 108549811 : if (DECL_P (lhs))
1692 : return false;
1693 :
1694 : /* We can re-write REALPART_EXPR and IMAGPART_EXPR sets in
1695 : a reasonably efficient manner... */
1696 62942238 : if ((TREE_CODE (lhs) == REALPART_EXPR
1697 62942238 : || TREE_CODE (lhs) == IMAGPART_EXPR)
1698 62942238 : && DECL_P (TREE_OPERAND (lhs, 0)))
1699 : return false;
1700 :
1701 : /* ??? The following could be relaxed allowing component
1702 : references that do not change the access size. */
1703 62910276 : if (TREE_CODE (lhs) == MEM_REF
1704 62910276 : && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR)
1705 : {
1706 7547576 : tree decl = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0);
1707 :
1708 : /* A decl that is wrapped inside a MEM-REF that covers
1709 : it full is also rewritable. */
1710 7547576 : if (integer_zerop (TREE_OPERAND (lhs, 1))
1711 2197355 : && DECL_P (decl)
1712 2197295 : && DECL_SIZE (decl) == TYPE_SIZE (TREE_TYPE (lhs))
1713 : /* If the dynamic type of the decl has larger precision than
1714 : the decl itself we can't use the decls type for SSA rewriting. */
1715 572900 : && ((! INTEGRAL_TYPE_P (TREE_TYPE (decl))
1716 14119 : || compare_tree_int (DECL_SIZE (decl),
1717 14119 : TYPE_PRECISION (TREE_TYPE (decl))) == 0)
1718 817 : || (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
1719 796 : && (TYPE_PRECISION (TREE_TYPE (decl))
1720 796 : >= TYPE_PRECISION (TREE_TYPE (lhs)))))
1721 : /* Make sure we are not re-writing non-float copying into float
1722 : copying as that can incur normalization. */
1723 572744 : && (! FLOAT_TYPE_P (TREE_TYPE (decl))
1724 4856 : || types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (decl)))
1725 8116055 : && (TREE_THIS_VOLATILE (decl) == TREE_THIS_VOLATILE (lhs)))
1726 : return false;
1727 :
1728 : /* A vector-insert using a MEM_REF or ARRAY_REF is rewritable
1729 : using a BIT_INSERT_EXPR. */
1730 6979583 : if (DECL_P (decl)
1731 6979517 : && VECTOR_TYPE_P (TREE_TYPE (decl))
1732 2102 : && TYPE_MODE (TREE_TYPE (decl)) != BLKmode
1733 284 : && known_ge (mem_ref_offset (lhs), 0)
1734 249 : && known_gt (wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (decl))),
1735 : mem_ref_offset (lhs))
1736 249 : && multiple_p (mem_ref_offset (lhs),
1737 249 : wi::to_poly_offset (TYPE_SIZE_UNIT (TREE_TYPE (lhs))))
1738 13959100 : && known_ge (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (decl))),
1739 : wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (lhs)))))
1740 : {
1741 248 : poly_uint64 lhs_bits, nelts;
1742 248 : if (poly_int_tree_p (TYPE_SIZE (TREE_TYPE (lhs)), &lhs_bits)
1743 250 : && multiple_p (lhs_bits,
1744 : tree_to_uhwi
1745 248 : (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))),
1746 : &nelts)
1747 268 : && valid_vector_subparts_p (nelts))
1748 : {
1749 228 : if (known_eq (nelts, 1u))
1750 228 : return false;
1751 : /* For sub-vector inserts the insert vector mode has to be
1752 : supported. */
1753 175 : tree vtype = build_vector_type (TREE_TYPE (TREE_TYPE (decl)),
1754 175 : nelts);
1755 175 : if (TYPE_MODE (vtype) != BLKmode)
1756 : return false;
1757 : }
1758 : }
1759 : }
1760 :
1761 : /* A vector-insert using a BIT_FIELD_REF is rewritable using
1762 : BIT_INSERT_EXPR. */
1763 62342055 : if (TREE_CODE (lhs) == BIT_FIELD_REF
1764 19552 : && DECL_P (TREE_OPERAND (lhs, 0))
1765 13911 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (lhs, 0)))
1766 13857 : && TYPE_MODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) != BLKmode
1767 1730 : && operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (lhs)),
1768 1730 : TYPE_SIZE_UNIT
1769 : (TREE_TYPE (TREE_TYPE (TREE_OPERAND (lhs, 0)))), 0)
1770 62343784 : && (tree_to_uhwi (TREE_OPERAND (lhs, 2))
1771 1729 : % tree_to_uhwi (TYPE_SIZE (TREE_TYPE (lhs)))) == 0)
1772 1729 : return false;
1773 :
1774 : return true;
1775 : }
1776 :
1777 : /* When possible, clear TREE_ADDRESSABLE bit, set or clear DECL_NOT_GIMPLE_REG_P
1778 : and mark the variable VAR for conversion into SSA. Return true when updating
1779 : stmts is required. */
1780 :
1781 : static void
1782 110999761 : maybe_optimize_var (tree var, bitmap addresses_taken, bitmap not_reg_needs,
1783 : bitmap suitable_for_renaming)
1784 : {
1785 : /* Global Variables, result decls cannot be changed. */
1786 110999761 : if (is_global_var (var)
1787 109359601 : || TREE_CODE (var) == RESULT_DECL
1788 220359362 : || bitmap_bit_p (addresses_taken, DECL_UID (var)))
1789 : return;
1790 :
1791 99905876 : bool maybe_reg = false;
1792 99905876 : if (TREE_ADDRESSABLE (var))
1793 : {
1794 1590131 : TREE_ADDRESSABLE (var) = 0;
1795 1590131 : maybe_reg = true;
1796 1590131 : if (dump_file)
1797 : {
1798 71 : fprintf (dump_file, "No longer having address taken: ");
1799 71 : print_generic_expr (dump_file, var);
1800 71 : fprintf (dump_file, "\n");
1801 : }
1802 : }
1803 :
1804 : /* For register type decls if we do not have any partial defs
1805 : we cannot express in SSA form mark them as DECL_NOT_GIMPLE_REG_P
1806 : as to avoid SSA rewrite. For the others go ahead and mark
1807 : them for renaming. */
1808 99905876 : if (is_gimple_reg_type (TREE_TYPE (var)))
1809 : {
1810 83302884 : if (bitmap_bit_p (not_reg_needs, DECL_UID (var)))
1811 : {
1812 33420 : DECL_NOT_GIMPLE_REG_P (var) = 1;
1813 33420 : if (dump_file)
1814 : {
1815 4 : fprintf (dump_file, "Has partial defs: ");
1816 4 : print_generic_expr (dump_file, var);
1817 4 : fprintf (dump_file, "\n");
1818 : }
1819 : }
1820 166495773 : else if (BITINT_TYPE_P (TREE_TYPE (var))
1821 43173 : && (cfun->curr_properties & PROP_gimple_lbitint) != 0
1822 83293348 : && TYPE_PRECISION (TREE_TYPE (var)) > MAX_FIXED_MODE_SIZE)
1823 : {
1824 : /* Don't rewrite large/huge _BitInt vars after _BitInt lowering
1825 : into SSA form. */
1826 2660 : DECL_NOT_GIMPLE_REG_P (var) = 1;
1827 2660 : if (dump_file)
1828 : {
1829 0 : fprintf (dump_file, "_BitInt var after its lowering: ");
1830 0 : print_generic_expr (dump_file, var);
1831 0 : fprintf (dump_file, "\n");
1832 : }
1833 : }
1834 83266804 : else if (DECL_NOT_GIMPLE_REG_P (var))
1835 : {
1836 8947 : maybe_reg = true;
1837 8947 : DECL_NOT_GIMPLE_REG_P (var) = 0;
1838 : }
1839 83302884 : if (maybe_reg)
1840 : {
1841 540386 : if (is_gimple_reg (var))
1842 : {
1843 532886 : if (dump_file)
1844 : {
1845 19 : fprintf (dump_file, "Now a gimple register: ");
1846 19 : print_generic_expr (dump_file, var);
1847 19 : fprintf (dump_file, "\n");
1848 : }
1849 532886 : bitmap_set_bit (suitable_for_renaming, DECL_UID (var));
1850 : }
1851 : else
1852 7500 : DECL_NOT_GIMPLE_REG_P (var) = 1;
1853 : }
1854 : }
1855 : }
1856 :
1857 : /* Return true when STMT is ASAN mark where second argument is an address
1858 : of a local variable. */
1859 :
1860 : static bool
1861 61905352 : is_asan_mark_p (gimple *stmt)
1862 : {
1863 61905352 : if (!gimple_call_internal_p (stmt, IFN_ASAN_MARK))
1864 : return false;
1865 :
1866 57787 : tree addr = get_base_address (gimple_call_arg (stmt, 1));
1867 57787 : if (TREE_CODE (addr) == ADDR_EXPR
1868 57787 : && VAR_P (TREE_OPERAND (addr, 0)))
1869 : {
1870 57583 : tree var = TREE_OPERAND (addr, 0);
1871 57583 : if (lookup_attribute (ASAN_USE_AFTER_SCOPE_ATTRIBUTE,
1872 57583 : DECL_ATTRIBUTES (var)))
1873 : return false;
1874 :
1875 52564 : unsigned addressable = TREE_ADDRESSABLE (var);
1876 52564 : TREE_ADDRESSABLE (var) = 0;
1877 52564 : bool r = is_gimple_reg (var);
1878 52564 : TREE_ADDRESSABLE (var) = addressable;
1879 52564 : return r;
1880 : }
1881 :
1882 : return false;
1883 : }
1884 :
1885 : /* Helper for set_addresses_taken. If the base of ADDR is a DECL,
1886 : set the bit for its uid in the bitmap passed as DATA and return
1887 : true; otherwise return false. */
1888 :
1889 : static bool
1890 56728529 : set_addresses_taken_1 (gimple *, tree addr, tree, void *data)
1891 : {
1892 56728529 : bitmap addresses_taken = (bitmap)data;
1893 56728529 : addr = get_base_address (addr);
1894 56728529 : if (addr && DECL_P (addr))
1895 : {
1896 36143914 : bitmap_set_bit (addresses_taken, DECL_UID (addr));
1897 36143914 : return true;
1898 : }
1899 : return false;
1900 : }
1901 :
1902 : /* Set the bit for the uid of all DECLs that have their address taken in
1903 : STMT in the ADDRESSES_TAKEN bitmap. Return true if there are any. */
1904 :
1905 : static bool
1906 909048430 : set_addresses_taken (gimple *stmt, bitmap addresses_taken)
1907 : {
1908 909048430 : return walk_stmt_load_store_addr_ops (stmt, addresses_taken, NULL, NULL,
1909 909048430 : set_addresses_taken_1);
1910 : }
1911 :
1912 : /* Compute TREE_ADDRESSABLE and whether we have unhandled partial defs
1913 : for local variables. */
1914 :
1915 : void
1916 13038123 : execute_update_addresses_taken (void)
1917 : {
1918 13038123 : basic_block bb;
1919 13038123 : auto_bitmap addresses_taken;
1920 13038123 : auto_bitmap not_reg_needs;
1921 13038123 : auto_bitmap suitable_for_renaming;
1922 13038123 : bool optimistic_not_addressable = false;
1923 13038123 : tree var;
1924 13038123 : unsigned i;
1925 :
1926 13038123 : timevar_push (TV_ADDRESS_TAKEN);
1927 :
1928 : /* Collect into ADDRESSES_TAKEN all variables whose address is taken within
1929 : the function body. */
1930 129311622 : FOR_EACH_BB_FN (bb, cfun)
1931 : {
1932 1141616496 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1933 909069498 : gsi_next (&gsi))
1934 : {
1935 909069498 : gimple *stmt = gsi_stmt (gsi);
1936 909069498 : enum gimple_code code = gimple_code (stmt);
1937 909069498 : tree decl;
1938 :
1939 909069498 : if (code == GIMPLE_CALL)
1940 : {
1941 59430157 : if (optimize_atomic_compare_exchange_p (stmt))
1942 : {
1943 : /* For __atomic_compare_exchange_N if the second argument
1944 : is &var, don't mark var addressable;
1945 : if it becomes non-addressable, we'll rewrite it into
1946 : ATOMIC_COMPARE_EXCHANGE call. */
1947 5812 : tree arg = gimple_call_arg (stmt, 1);
1948 5812 : gimple_call_set_arg (stmt, 1, null_pointer_node);
1949 5812 : set_addresses_taken (stmt, addresses_taken);
1950 5812 : gimple_call_set_arg (stmt, 1, arg);
1951 : /* Remember we have to check again below. */
1952 5812 : optimistic_not_addressable = true;
1953 : }
1954 59424345 : else if (is_asan_mark_p (stmt)
1955 59424345 : || gimple_call_internal_p (stmt, IFN_GOMP_SIMT_ENTER))
1956 : ;
1957 59404270 : else if (gimple_call_internal_p (stmt, IFN_ASSUME))
1958 : {
1959 : /* An address-taken arguments are not relevant for aliasing
1960 : and we never expand the call (and need the address).
1961 : But we can't use a register. */
1962 2220 : for (unsigned i = 1; i < gimple_call_num_args (stmt); ++i)
1963 : {
1964 1227 : tree arg = gimple_call_arg (stmt, i);
1965 1227 : if (TREE_CODE (arg) == ADDR_EXPR)
1966 64 : bitmap_set_bit (not_reg_needs,
1967 32 : DECL_UID (get_base_address
1968 : (TREE_OPERAND (arg, 0))));
1969 : }
1970 : }
1971 : else
1972 59403277 : set_addresses_taken (stmt, addresses_taken);
1973 : }
1974 : else
1975 : /* Note all addresses taken by the stmt. */
1976 849639341 : set_addresses_taken (stmt, addresses_taken);
1977 :
1978 : /* If we have a call or an assignment, see if the lhs contains
1979 : a local decl that requires not to be a gimple register. */
1980 909069498 : if (code == GIMPLE_ASSIGN || code == GIMPLE_CALL)
1981 : {
1982 332225609 : tree lhs = gimple_get_lhs (stmt);
1983 332225609 : if (lhs
1984 296382855 : && TREE_CODE (lhs) != SSA_NAME
1985 440784046 : && ((code == GIMPLE_CALL && ! DECL_P (lhs))
1986 108443084 : || non_rewritable_lvalue_p (lhs)))
1987 : {
1988 62445477 : decl = get_base_address (lhs);
1989 62445477 : if (DECL_P (decl))
1990 43256327 : bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1991 : }
1992 : }
1993 :
1994 909069498 : if (gimple_assign_single_p (stmt))
1995 : {
1996 184737381 : tree rhs = gimple_assign_rhs1 (stmt);
1997 184737381 : if ((decl = non_rewritable_mem_ref_base (rhs)))
1998 20551007 : bitmap_set_bit (not_reg_needs, DECL_UID (decl));
1999 : }
2000 :
2001 724332117 : else if (code == GIMPLE_CALL)
2002 : {
2003 176290929 : for (i = 0; i < gimple_call_num_args (stmt); ++i)
2004 : {
2005 116860772 : tree arg = gimple_call_arg (stmt, i);
2006 116860772 : if ((decl = non_rewritable_mem_ref_base (arg)))
2007 166039 : bitmap_set_bit (not_reg_needs, DECL_UID (decl));
2008 : }
2009 : }
2010 :
2011 664901960 : else if (code == GIMPLE_ASM)
2012 : {
2013 685002 : gasm *asm_stmt = as_a <gasm *> (stmt);
2014 2056933 : for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
2015 : {
2016 686929 : tree link = gimple_asm_output_op (asm_stmt, i);
2017 686929 : tree lhs = TREE_VALUE (link);
2018 686929 : if (TREE_CODE (lhs) != SSA_NAME)
2019 : {
2020 180090 : decl = get_base_address (lhs);
2021 180090 : if (DECL_P (decl)
2022 180090 : && (non_rewritable_lvalue_p (lhs)
2023 : /* We cannot move required conversions from
2024 : the lhs to the rhs in asm statements, so
2025 : require we do not need any. */
2026 96525 : || !useless_type_conversion_p
2027 96525 : (TREE_TYPE (lhs), TREE_TYPE (decl))))
2028 10812 : bitmap_set_bit (not_reg_needs, DECL_UID (decl));
2029 : }
2030 : }
2031 1140110 : for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
2032 : {
2033 455108 : tree link = gimple_asm_input_op (asm_stmt, i);
2034 455108 : if ((decl = non_rewritable_mem_ref_base (TREE_VALUE (link))))
2035 1323 : bitmap_set_bit (not_reg_needs, DECL_UID (decl));
2036 : }
2037 : }
2038 : }
2039 :
2040 155080508 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2041 38807009 : gsi_next (&gsi))
2042 : {
2043 38807009 : gphi *phi = gsi.phi ();
2044 :
2045 178824458 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2046 : {
2047 101210440 : tree op = PHI_ARG_DEF (phi, i);
2048 101210440 : if (TREE_CODE (op) == ADDR_EXPR)
2049 799650 : set_addresses_taken_1 (phi, TREE_OPERAND (op, 0), op,
2050 799650 : (void *)addresses_taken);
2051 : }
2052 : }
2053 : }
2054 :
2055 : /* We cannot iterate over all referenced vars because that can contain
2056 : unused vars from BLOCK trees, which causes code generation differences
2057 : for -g vs. -g0. */
2058 40276331 : for (var = DECL_ARGUMENTS (cfun->decl); var; var = DECL_CHAIN (var))
2059 27238208 : maybe_optimize_var (var, addresses_taken, not_reg_needs,
2060 : suitable_for_renaming);
2061 :
2062 96799676 : FOR_EACH_VEC_SAFE_ELT (cfun->local_decls, i, var)
2063 83761553 : maybe_optimize_var (var, addresses_taken, not_reg_needs,
2064 : suitable_for_renaming);
2065 :
2066 : /* Operand caches need to be recomputed for operands referencing the updated
2067 : variables and operands need to be rewritten to expose bare symbols. */
2068 13038123 : if (!bitmap_empty_p (suitable_for_renaming)
2069 13038123 : || optimistic_not_addressable)
2070 : {
2071 5751093 : FOR_EACH_BB_FN (bb, cfun)
2072 49308094 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
2073 : {
2074 38253164 : gimple *stmt = gsi_stmt (gsi);
2075 :
2076 : /* Re-write TARGET_MEM_REFs of symbols we want to
2077 : rewrite into SSA form. */
2078 38253164 : if (gimple_assign_single_p (stmt))
2079 : {
2080 12557915 : tree lhs = gimple_assign_lhs (stmt);
2081 12557915 : tree rhs, *rhsp = gimple_assign_rhs1_ptr (stmt);
2082 12557915 : tree sym;
2083 :
2084 : /* Rewrite LHS IMAG/REALPART_EXPR similar to
2085 : gimplify_modify_expr_complex_part. */
2086 12557915 : if ((TREE_CODE (lhs) == IMAGPART_EXPR
2087 12557915 : || TREE_CODE (lhs) == REALPART_EXPR)
2088 3631 : && DECL_P (TREE_OPERAND (lhs, 0))
2089 12560505 : && bitmap_bit_p (suitable_for_renaming,
2090 2590 : DECL_UID (TREE_OPERAND (lhs, 0))))
2091 : {
2092 2512 : tree other = make_ssa_name (TREE_TYPE (lhs));
2093 3777 : tree lrhs = build1 (TREE_CODE (lhs) == IMAGPART_EXPR
2094 : ? REALPART_EXPR : IMAGPART_EXPR,
2095 2512 : TREE_TYPE (other),
2096 2512 : TREE_OPERAND (lhs, 0));
2097 2512 : suppress_warning (lrhs);
2098 2512 : gimple *load = gimple_build_assign (other, lrhs);
2099 2512 : location_t loc = gimple_location (stmt);
2100 2512 : gimple_set_location (load, loc);
2101 5024 : gimple_set_vuse (load, gimple_vuse (stmt));
2102 2512 : gsi_insert_before (&gsi, load, GSI_SAME_STMT);
2103 2512 : gimple_assign_set_lhs (stmt, TREE_OPERAND (lhs, 0));
2104 2512 : gimple_assign_set_rhs_with_ops
2105 3777 : (&gsi, COMPLEX_EXPR,
2106 : TREE_CODE (lhs) == IMAGPART_EXPR
2107 1265 : ? other : gimple_assign_rhs1 (stmt),
2108 2512 : TREE_CODE (lhs) == IMAGPART_EXPR
2109 1247 : ? gimple_assign_rhs1 (stmt) : other, NULL_TREE);
2110 2512 : stmt = gsi_stmt (gsi);
2111 2512 : unlink_stmt_vdef (stmt);
2112 2512 : update_stmt (stmt);
2113 2512 : continue;
2114 2512 : }
2115 :
2116 : /* Rewrite a vector insert via a BIT_FIELD_REF on the LHS
2117 : into a BIT_INSERT_EXPR. */
2118 12555403 : if (TREE_CODE (lhs) == BIT_FIELD_REF
2119 903 : && DECL_P (TREE_OPERAND (lhs, 0))
2120 845 : && bitmap_bit_p (suitable_for_renaming,
2121 845 : DECL_UID (TREE_OPERAND (lhs, 0)))
2122 702 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (lhs, 0)))
2123 702 : && TYPE_MODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) != BLKmode
2124 702 : && operand_equal_p (TYPE_SIZE_UNIT (TREE_TYPE (lhs)),
2125 702 : TYPE_SIZE_UNIT (TREE_TYPE
2126 : (TREE_TYPE (TREE_OPERAND (lhs, 0)))),
2127 : 0)
2128 12556105 : && (tree_to_uhwi (TREE_OPERAND (lhs, 2))
2129 702 : % tree_to_uhwi (TYPE_SIZE (TREE_TYPE (lhs))) == 0))
2130 : {
2131 702 : tree var = TREE_OPERAND (lhs, 0);
2132 702 : tree val = gimple_assign_rhs1 (stmt);
2133 702 : if (! types_compatible_p (TREE_TYPE (TREE_TYPE (var)),
2134 702 : TREE_TYPE (val)))
2135 : {
2136 0 : tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (var)));
2137 0 : gimple *pun
2138 0 : = gimple_build_assign (tem,
2139 : build1 (VIEW_CONVERT_EXPR,
2140 0 : TREE_TYPE (tem), val));
2141 0 : gsi_insert_before (&gsi, pun, GSI_SAME_STMT);
2142 0 : val = tem;
2143 : }
2144 702 : tree bitpos = TREE_OPERAND (lhs, 2);
2145 702 : gimple_assign_set_lhs (stmt, var);
2146 702 : gimple_assign_set_rhs_with_ops
2147 702 : (&gsi, BIT_INSERT_EXPR, var, val, bitpos);
2148 702 : stmt = gsi_stmt (gsi);
2149 702 : unlink_stmt_vdef (stmt);
2150 702 : update_stmt (stmt);
2151 702 : continue;
2152 702 : }
2153 :
2154 : /* Rewrite a vector insert using a MEM_REF on the LHS
2155 : into a BIT_INSERT_EXPR. */
2156 12554701 : if (TREE_CODE (lhs) == MEM_REF
2157 480628 : && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR
2158 115387 : && (sym = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0))
2159 115387 : && DECL_P (sym)
2160 115387 : && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym))
2161 8329 : && VECTOR_TYPE_P (TREE_TYPE (sym))
2162 183 : && TYPE_MODE (TREE_TYPE (sym)) != BLKmode
2163 : /* If it is a full replacement we can do better below. */
2164 177 : && maybe_ne (wi::to_poly_offset
2165 177 : (TYPE_SIZE_UNIT (TREE_TYPE (lhs))),
2166 : wi::to_poly_offset
2167 177 : (TYPE_SIZE_UNIT (TREE_TYPE (sym))))
2168 79 : && known_ge (mem_ref_offset (lhs), 0)
2169 79 : && known_gt (wi::to_poly_offset
2170 : (TYPE_SIZE_UNIT (TREE_TYPE (sym))),
2171 : mem_ref_offset (lhs))
2172 12554780 : && multiple_p (mem_ref_offset (lhs),
2173 : wi::to_poly_offset
2174 12554701 : (TYPE_SIZE_UNIT (TREE_TYPE (lhs)))))
2175 : {
2176 79 : tree val = gimple_assign_rhs1 (stmt);
2177 79 : if (! types_compatible_p (TREE_TYPE (val),
2178 79 : TREE_TYPE (TREE_TYPE (sym))))
2179 : {
2180 58 : poly_uint64 lhs_bits, nelts;
2181 58 : tree temtype = TREE_TYPE (TREE_TYPE (sym));
2182 58 : if (poly_int_tree_p (TYPE_SIZE (TREE_TYPE (lhs)),
2183 : &lhs_bits)
2184 174 : && multiple_p (lhs_bits,
2185 : tree_to_uhwi
2186 58 : (TYPE_SIZE (TREE_TYPE
2187 : (TREE_TYPE (sym)))),
2188 : &nelts)
2189 58 : && maybe_ne (nelts, 1u)
2190 148 : && valid_vector_subparts_p (nelts))
2191 32 : temtype = build_vector_type (temtype, nelts);
2192 58 : tree tem = make_ssa_name (temtype);
2193 58 : gimple *pun
2194 58 : = gimple_build_assign (tem,
2195 : build1 (VIEW_CONVERT_EXPR,
2196 58 : TREE_TYPE (tem), val));
2197 58 : gsi_insert_before (&gsi, pun, GSI_SAME_STMT);
2198 58 : val = tem;
2199 : }
2200 79 : tree bitpos
2201 79 : = wide_int_to_tree (bitsizetype,
2202 79 : mem_ref_offset (lhs) * BITS_PER_UNIT);
2203 79 : gimple_assign_set_lhs (stmt, sym);
2204 79 : gimple_assign_set_rhs_with_ops
2205 79 : (&gsi, BIT_INSERT_EXPR, sym, val, bitpos);
2206 79 : stmt = gsi_stmt (gsi);
2207 79 : unlink_stmt_vdef (stmt);
2208 79 : update_stmt (stmt);
2209 79 : continue;
2210 79 : }
2211 :
2212 : /* We shouldn't have any fancy wrapping of
2213 : component-refs on the LHS, but look through
2214 : VIEW_CONVERT_EXPRs as that is easy. */
2215 12554627 : while (TREE_CODE (lhs) == VIEW_CONVERT_EXPR)
2216 5 : lhs = TREE_OPERAND (lhs, 0);
2217 12554622 : if (TREE_CODE (lhs) == MEM_REF
2218 480554 : && TREE_CODE (TREE_OPERAND (lhs, 0)) == ADDR_EXPR
2219 115308 : && integer_zerop (TREE_OPERAND (lhs, 1))
2220 49721 : && (sym = TREE_OPERAND (TREE_OPERAND (lhs, 0), 0))
2221 49721 : && DECL_P (sym)
2222 49721 : && !TREE_ADDRESSABLE (sym)
2223 12584586 : && bitmap_bit_p (suitable_for_renaming, DECL_UID (sym)))
2224 : lhs = sym;
2225 : else
2226 12546372 : lhs = gimple_assign_lhs (stmt);
2227 :
2228 : /* Rewrite the RHS and make sure the resulting assignment
2229 : is validly typed. */
2230 12554622 : maybe_rewrite_mem_ref_base (rhsp, suitable_for_renaming);
2231 12554622 : rhs = gimple_assign_rhs1 (stmt);
2232 12554622 : if (gimple_assign_lhs (stmt) != lhs
2233 12562872 : && !useless_type_conversion_p (TREE_TYPE (lhs),
2234 8250 : TREE_TYPE (rhs)))
2235 : {
2236 4044 : if (gimple_clobber_p (stmt))
2237 : {
2238 11 : rhs = build_constructor (TREE_TYPE (lhs), NULL);
2239 11 : TREE_THIS_VOLATILE (rhs) = 1;
2240 : }
2241 : else
2242 4033 : rhs = fold_build1 (VIEW_CONVERT_EXPR,
2243 : TREE_TYPE (lhs), rhs);
2244 : }
2245 12554622 : if (gimple_assign_lhs (stmt) != lhs)
2246 8250 : gimple_assign_set_lhs (stmt, lhs);
2247 :
2248 12554622 : if (gimple_assign_rhs1 (stmt) != rhs)
2249 : {
2250 4044 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2251 4044 : gimple_assign_set_rhs_from_tree (&gsi, rhs);
2252 : }
2253 : }
2254 :
2255 25695249 : else if (gimple_code (stmt) == GIMPLE_CALL)
2256 : {
2257 2486819 : unsigned i;
2258 2486819 : if (optimize_atomic_compare_exchange_p (stmt))
2259 : {
2260 5812 : tree expected = gimple_call_arg (stmt, 1);
2261 5812 : tree decl = TREE_OPERAND (expected, 0);
2262 5812 : if (bitmap_bit_p (suitable_for_renaming, DECL_UID (decl)))
2263 : {
2264 5737 : fold_builtin_atomic_compare_exchange (&gsi);
2265 5737 : continue;
2266 : }
2267 75 : else if (!TREE_ADDRESSABLE (decl))
2268 : /* If there are partial defs of the decl we may
2269 : have cleared the addressable bit but set
2270 : DECL_NOT_GIMPLE_REG_P. We have to restore
2271 : TREE_ADDRESSABLE here. */
2272 27 : TREE_ADDRESSABLE (decl) = 1;
2273 : }
2274 2481007 : else if (is_asan_mark_p (stmt))
2275 : {
2276 392 : tree var = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
2277 392 : if (bitmap_bit_p (suitable_for_renaming, DECL_UID (var)))
2278 : {
2279 344 : unlink_stmt_vdef (stmt);
2280 344 : if (asan_mark_p (stmt, ASAN_MARK_POISON))
2281 : {
2282 177 : gcall *call
2283 177 : = gimple_build_call_internal (IFN_ASAN_POISON, 0);
2284 177 : gimple_call_set_lhs (call, var);
2285 177 : gsi_replace (&gsi, call, true);
2286 : }
2287 : else
2288 : {
2289 : /* In ASAN_MARK (UNPOISON, &b, ...) the variable
2290 : is uninitialized. Avoid dependencies on
2291 : previous out of scope value. */
2292 167 : tree clobber = build_clobber (TREE_TYPE (var));
2293 167 : gimple *g = gimple_build_assign (var, clobber);
2294 167 : gsi_replace (&gsi, g, true);
2295 : }
2296 344 : continue;
2297 344 : }
2298 : }
2299 2480615 : else if (gimple_call_internal_p (stmt, IFN_GOMP_SIMT_ENTER))
2300 0 : for (i = 1; i < gimple_call_num_args (stmt); i++)
2301 : {
2302 0 : tree *argp = gimple_call_arg_ptr (stmt, i);
2303 0 : if (*argp == null_pointer_node)
2304 0 : continue;
2305 0 : gcc_assert (TREE_CODE (*argp) == ADDR_EXPR
2306 : && VAR_P (TREE_OPERAND (*argp, 0)));
2307 0 : tree var = TREE_OPERAND (*argp, 0);
2308 0 : if (bitmap_bit_p (suitable_for_renaming, DECL_UID (var)))
2309 0 : *argp = null_pointer_node;
2310 : }
2311 6936175 : for (i = 0; i < gimple_call_num_args (stmt); ++i)
2312 : {
2313 4455437 : tree *argp = gimple_call_arg_ptr (stmt, i);
2314 4455437 : maybe_rewrite_mem_ref_base (argp, suitable_for_renaming);
2315 : }
2316 : }
2317 :
2318 23208430 : else if (gimple_code (stmt) == GIMPLE_ASM)
2319 : {
2320 11698 : gasm *asm_stmt = as_a <gasm *> (stmt);
2321 11698 : unsigned i;
2322 62085 : for (i = 0; i < gimple_asm_noutputs (asm_stmt); ++i)
2323 : {
2324 38689 : tree link = gimple_asm_output_op (asm_stmt, i);
2325 38689 : maybe_rewrite_mem_ref_base (&TREE_VALUE (link),
2326 : suitable_for_renaming);
2327 : }
2328 26965 : for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
2329 : {
2330 15267 : tree link = gimple_asm_input_op (asm_stmt, i);
2331 15267 : maybe_rewrite_mem_ref_base (&TREE_VALUE (link),
2332 : suitable_for_renaming);
2333 : }
2334 : }
2335 :
2336 23196732 : else if (gimple_debug_bind_p (stmt)
2337 17366842 : && gimple_debug_bind_has_value_p (stmt))
2338 : {
2339 7458813 : tree *valuep = gimple_debug_bind_get_value_ptr (stmt);
2340 7458813 : tree decl;
2341 7458813 : maybe_rewrite_mem_ref_base (valuep, suitable_for_renaming);
2342 7458813 : decl = non_rewritable_mem_ref_base (*valuep);
2343 7458813 : if (decl
2344 7458813 : && bitmap_bit_p (suitable_for_renaming, DECL_UID (decl)))
2345 10 : gimple_debug_bind_reset_value (stmt);
2346 : }
2347 :
2348 51918534 : if (gimple_references_memory_p (stmt)
2349 33215638 : || is_gimple_debug (stmt))
2350 22394994 : update_stmt (stmt);
2351 :
2352 38243790 : gsi_next (&gsi);
2353 : }
2354 :
2355 : /* Update SSA form here, we are called as non-pass as well. */
2356 223628 : if (number_of_loops (cfun) > 1
2357 223628 : && loops_state_satisfies_p (LOOP_CLOSED_SSA))
2358 133 : rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
2359 : else
2360 223495 : update_ssa (TODO_update_ssa);
2361 : }
2362 :
2363 13038123 : timevar_pop (TV_ADDRESS_TAKEN);
2364 13038123 : }
2365 :
2366 : namespace {
2367 :
2368 : const pass_data pass_data_update_address_taken =
2369 : {
2370 : GIMPLE_PASS, /* type */
2371 : "addressables", /* name */
2372 : OPTGROUP_NONE, /* optinfo_flags */
2373 : TV_ADDRESS_TAKEN, /* tv_id */
2374 : PROP_ssa, /* properties_required */
2375 : 0, /* properties_provided */
2376 : 0, /* properties_destroyed */
2377 : 0, /* todo_flags_start */
2378 : TODO_update_address_taken, /* todo_flags_finish */
2379 : };
2380 :
2381 : class pass_update_address_taken : public gimple_opt_pass
2382 : {
2383 : public:
2384 0 : pass_update_address_taken (gcc::context *ctxt)
2385 0 : : gimple_opt_pass (pass_data_update_address_taken, ctxt)
2386 : {}
2387 :
2388 : /* opt_pass methods: */
2389 :
2390 : }; // class pass_update_address_taken
2391 :
2392 : } // anon namespace
2393 :
2394 : gimple_opt_pass *
2395 0 : make_pass_update_address_taken (gcc::context *ctxt)
2396 : {
2397 0 : return new pass_update_address_taken (ctxt);
2398 : }
|