Line data Source code
1 : /* SCC value numbering for trees
2 : Copyright (C) 2006-2026 Free Software Foundation, Inc.
3 : Contributed by Daniel Berlin <dan@dberlin.org>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "ssa.h"
29 : #include "expmed.h"
30 : #include "insn-config.h"
31 : #include "memmodel.h"
32 : #include "emit-rtl.h"
33 : #include "cgraph.h"
34 : #include "gimple-pretty-print.h"
35 : #include "splay-tree-utils.h"
36 : #include "alias.h"
37 : #include "fold-const.h"
38 : #include "stor-layout.h"
39 : #include "cfganal.h"
40 : #include "tree-inline.h"
41 : #include "internal-fn.h"
42 : #include "gimple-iterator.h"
43 : #include "gimple-fold.h"
44 : #include "tree-eh.h"
45 : #include "flags.h"
46 : #include "dojump.h"
47 : #include "explow.h"
48 : #include "calls.h"
49 : #include "varasm.h"
50 : #include "stmt.h"
51 : #include "expr.h"
52 : #include "tree-dfa.h"
53 : #include "tree-ssa.h"
54 : #include "dumpfile.h"
55 : #include "cfgloop.h"
56 : #include "tree-ssa-propagate.h"
57 : #include "tree-cfg.h"
58 : #include "domwalk.h"
59 : #include "gimple-match.h"
60 : #include "stringpool.h"
61 : #include "attribs.h"
62 : #include "tree-pass.h"
63 : #include "statistics.h"
64 : #include "langhooks.h"
65 : #include "ipa-utils.h"
66 : #include "dbgcnt.h"
67 : #include "tree-cfgcleanup.h"
68 : #include "tree-ssa-loop.h"
69 : #include "tree-scalar-evolution.h"
70 : #include "tree-ssa-loop-niter.h"
71 : #include "builtins.h"
72 : #include "fold-const-call.h"
73 : #include "ipa-modref-tree.h"
74 : #include "ipa-modref.h"
75 : #include "tree-ssa-sccvn.h"
76 : #include "alloc-pool.h"
77 : #include "symbol-summary.h"
78 : #include "sreal.h"
79 : #include "ipa-cp.h"
80 : #include "ipa-prop.h"
81 : #include "target.h"
82 :
83 : /* This algorithm is based on the SCC algorithm presented by Keith
84 : Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
85 : (http://citeseer.ist.psu.edu/41805.html). In
86 : straight line code, it is equivalent to a regular hash based value
87 : numbering that is performed in reverse postorder.
88 :
89 : For code with cycles, there are two alternatives, both of which
90 : require keeping the hashtables separate from the actual list of
91 : value numbers for SSA names.
92 :
93 : 1. Iterate value numbering in an RPO walk of the blocks, removing
94 : all the entries from the hashtable after each iteration (but
95 : keeping the SSA name->value number mapping between iterations).
96 : Iterate until it does not change.
97 :
98 : 2. Perform value numbering as part of an SCC walk on the SSA graph,
99 : iterating only the cycles in the SSA graph until they do not change
100 : (using a separate, optimistic hashtable for value numbering the SCC
101 : operands).
102 :
103 : The second is not just faster in practice (because most SSA graph
104 : cycles do not involve all the variables in the graph), it also has
105 : some nice properties.
106 :
107 : One of these nice properties is that when we pop an SCC off the
108 : stack, we are guaranteed to have processed all the operands coming from
109 : *outside of that SCC*, so we do not need to do anything special to
110 : ensure they have value numbers.
111 :
112 : Another nice property is that the SCC walk is done as part of a DFS
113 : of the SSA graph, which makes it easy to perform combining and
114 : simplifying operations at the same time.
115 :
116 : The code below is deliberately written in a way that makes it easy
117 : to separate the SCC walk from the other work it does.
118 :
119 : In order to propagate constants through the code, we track which
120 : expressions contain constants, and use those while folding. In
121 : theory, we could also track expressions whose value numbers are
122 : replaced, in case we end up folding based on expression
123 : identities.
124 :
125 : In order to value number memory, we assign value numbers to vuses.
126 : This enables us to note that, for example, stores to the same
127 : address of the same value from the same starting memory states are
128 : equivalent.
129 : TODO:
130 :
131 : 1. We can iterate only the changing portions of the SCC's, but
132 : I have not seen an SCC big enough for this to be a win.
133 : 2. If you differentiate between phi nodes for loops and phi nodes
134 : for if-then-else, you can properly consider phi nodes in different
135 : blocks for equivalence.
136 : 3. We could value number vuses in more cases, particularly, whole
137 : structure copies.
138 : */
139 :
140 : /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
141 : #define BB_EXECUTABLE BB_VISITED
142 :
143 : static vn_lookup_kind default_vn_walk_kind;
144 :
145 : /* vn_nary_op hashtable helpers. */
146 :
147 : struct vn_nary_op_hasher : nofree_ptr_hash <vn_nary_op_s>
148 : {
149 : typedef vn_nary_op_s *compare_type;
150 : static inline hashval_t hash (const vn_nary_op_s *);
151 : static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
152 : };
153 :
154 : /* Return the computed hashcode for nary operation P1. */
155 :
156 : inline hashval_t
157 794754208 : vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
158 : {
159 794754208 : return vno1->hashcode;
160 : }
161 :
162 : /* Compare nary operations P1 and P2 and return true if they are
163 : equivalent. */
164 :
165 : inline bool
166 1008387429 : vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
167 : {
168 1008387429 : return vno1 == vno2 || vn_nary_op_eq (vno1, vno2);
169 : }
170 :
171 : typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
172 : typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
173 :
174 :
175 : /* vn_phi hashtable helpers. */
176 :
177 : static int
178 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
179 :
180 : struct vn_phi_hasher : nofree_ptr_hash <vn_phi_s>
181 : {
182 : static inline hashval_t hash (const vn_phi_s *);
183 : static inline bool equal (const vn_phi_s *, const vn_phi_s *);
184 : };
185 :
186 : /* Return the computed hashcode for phi operation P1. */
187 :
188 : inline hashval_t
189 27090792 : vn_phi_hasher::hash (const vn_phi_s *vp1)
190 : {
191 27090792 : return vp1->hashcode;
192 : }
193 :
194 : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
195 :
196 : inline bool
197 48032865 : vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
198 : {
199 48032865 : return vp1 == vp2 || vn_phi_eq (vp1, vp2);
200 : }
201 :
202 : typedef hash_table<vn_phi_hasher> vn_phi_table_type;
203 : typedef vn_phi_table_type::iterator vn_phi_iterator_type;
204 :
205 :
206 : /* Compare two reference operands P1 and P2 for equality. Return true if
207 : they are equal, and false otherwise. */
208 :
209 : static int
210 26377700 : vn_reference_op_eq (const void *p1, const void *p2)
211 : {
212 26377700 : const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
213 26377700 : const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
214 :
215 26377700 : return (vro1->opcode == vro2->opcode
216 : /* We do not care for differences in type qualification. */
217 26375850 : && (vro1->type == vro2->type
218 1250762 : || (vro1->type && vro2->type
219 1250762 : && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
220 1250762 : TYPE_MAIN_VARIANT (vro2->type))))
221 25367336 : && expressions_equal_p (vro1->op0, vro2->op0)
222 25319155 : && expressions_equal_p (vro1->op1, vro2->op1)
223 25319155 : && expressions_equal_p (vro1->op2, vro2->op2)
224 51696855 : && (vro1->opcode != CALL_EXPR || vro1->clique == vro2->clique));
225 : }
226 :
227 : /* Free a reference operation structure VP. */
228 :
229 : static inline void
230 0 : free_reference (vn_reference_s *vr)
231 : {
232 0 : vr->operands.release ();
233 : }
234 :
235 :
236 : /* vn_reference hashtable helpers. */
237 :
238 : struct vn_reference_hasher : nofree_ptr_hash <vn_reference_s>
239 : {
240 : static inline hashval_t hash (const vn_reference_s *);
241 : static inline bool equal (const vn_reference_s *, const vn_reference_s *);
242 : };
243 :
244 : /* Return the hashcode for a given reference operation P1. */
245 :
246 : inline hashval_t
247 4003796785 : vn_reference_hasher::hash (const vn_reference_s *vr1)
248 : {
249 4003796785 : return vr1->hashcode;
250 : }
251 :
252 : inline bool
253 4771588593 : vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
254 : {
255 4771588593 : return v == c || vn_reference_eq (v, c);
256 : }
257 :
258 : typedef hash_table<vn_reference_hasher> vn_reference_table_type;
259 : typedef vn_reference_table_type::iterator vn_reference_iterator_type;
260 :
261 : /* Pretty-print OPS to OUTFILE. */
262 :
263 : void
264 287 : print_vn_reference_ops (FILE *outfile, const vec<vn_reference_op_s> ops)
265 : {
266 287 : vn_reference_op_t vro;
267 287 : unsigned int i;
268 287 : fprintf (outfile, "{");
269 1591 : for (i = 0; ops.iterate (i, &vro); i++)
270 : {
271 1017 : bool closebrace = false;
272 1017 : if (vro->opcode != SSA_NAME
273 803 : && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
274 : {
275 803 : fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
276 803 : if (vro->op0 || vro->opcode == CALL_EXPR)
277 : {
278 803 : fprintf (outfile, "<");
279 803 : closebrace = true;
280 : }
281 : }
282 1017 : if (vro->opcode == MEM_REF || vro->opcode == TARGET_MEM_REF)
283 275 : fprintf (outfile, "(A%d)", TYPE_ALIGN (vro->type));
284 1017 : if (vro->op0 || vro->opcode == CALL_EXPR)
285 : {
286 1017 : if (!vro->op0)
287 0 : fprintf (outfile, internal_fn_name ((internal_fn)vro->clique));
288 : else
289 : {
290 1017 : if (vro->opcode == MEM_REF || vro->opcode == TARGET_MEM_REF)
291 : {
292 275 : fprintf (outfile, "(");
293 275 : print_generic_expr (outfile, TREE_TYPE (vro->op0));
294 275 : fprintf (outfile, ")");
295 : }
296 1017 : print_generic_expr (outfile, vro->op0);
297 : }
298 1017 : if (vro->op1)
299 : {
300 185 : fprintf (outfile, ",");
301 185 : print_generic_expr (outfile, vro->op1);
302 : }
303 1017 : if (vro->op2)
304 : {
305 185 : fprintf (outfile, ",");
306 185 : print_generic_expr (outfile, vro->op2);
307 : }
308 : }
309 1017 : if (closebrace)
310 803 : fprintf (outfile, ">");
311 1017 : if (i != ops.length () - 1)
312 730 : fprintf (outfile, ",");
313 : }
314 287 : fprintf (outfile, "}");
315 287 : }
316 :
317 : DEBUG_FUNCTION void
318 0 : debug_vn_reference_ops (const vec<vn_reference_op_s> ops)
319 : {
320 0 : print_vn_reference_ops (stderr, ops);
321 0 : fputc ('\n', stderr);
322 0 : }
323 :
324 : /* The set of VN hashtables. */
325 :
326 : typedef struct vn_tables_s
327 : {
328 : vn_nary_op_table_type *nary;
329 : vn_phi_table_type *phis;
330 : vn_reference_table_type *references;
331 : /* Expressions inserted by vn_nary_build_or_lookup_1, keyed on the
332 : operands of the inserted statement rather than their value numbers.
333 : This is not unwound on iteration. Only allocated when iterating. */
334 : vn_nary_op_table_type *inserted_exprs;
335 : } *vn_tables_t;
336 :
337 :
338 : /* vn_constant hashtable helpers. */
339 :
340 : struct vn_constant_hasher : free_ptr_hash <vn_constant_s>
341 : {
342 : static inline hashval_t hash (const vn_constant_s *);
343 : static inline bool equal (const vn_constant_s *, const vn_constant_s *);
344 : };
345 :
346 : /* Hash table hash function for vn_constant_t. */
347 :
348 : inline hashval_t
349 12644399 : vn_constant_hasher::hash (const vn_constant_s *vc1)
350 : {
351 12644399 : return vc1->hashcode;
352 : }
353 :
354 : /* Hash table equality function for vn_constant_t. */
355 :
356 : inline bool
357 15235363 : vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
358 : {
359 15235363 : if (vc1->hashcode != vc2->hashcode)
360 : return false;
361 :
362 2292868 : return vn_constant_eq_with_type (vc1->constant, vc2->constant);
363 : }
364 :
365 : static hash_table<vn_constant_hasher> *constant_to_value_id;
366 :
367 :
368 : /* Obstack we allocate the vn-tables elements from. */
369 : static obstack vn_tables_obstack;
370 : /* Special obstack we never unwind. */
371 : static obstack vn_tables_insert_obstack;
372 :
373 : static vn_reference_t last_inserted_ref;
374 : static vn_phi_t last_inserted_phi;
375 : static vn_nary_op_t last_inserted_nary;
376 : static vn_ssa_aux_t last_pushed_avail;
377 :
378 : /* Valid hashtables storing information we have proven to be
379 : correct. */
380 : static vn_tables_t valid_info;
381 :
382 : /* Global RPO state for access from hooks. */
383 : static class eliminate_dom_walker *rpo_avail;
384 : basic_block vn_context_bb;
385 : int *vn_bb_to_rpo;
386 : static int *vn_exec_idom;
387 : static int vn_bb_in_region_flag;
388 : static edge vn_entry_edge;
389 :
390 :
391 : /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
392 : an SSA name, otherwise just return it. */
393 : tree (*vn_valueize) (tree);
394 : static tree
395 86650 : vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
396 : {
397 86650 : basic_block saved_vn_context_bb = vn_context_bb;
398 : /* Look for sth available at the definition block of the argument.
399 : This avoids inconsistencies between availability there which
400 : decides if the stmt can be removed and availability at the
401 : use site. The SSA property ensures that things available
402 : at the definition are also available at uses. */
403 86650 : if (!SSA_NAME_IS_DEFAULT_DEF (t))
404 82679 : vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
405 86650 : tree res = vn_valueize (t);
406 86650 : vn_context_bb = saved_vn_context_bb;
407 86650 : return res;
408 : }
409 :
410 :
411 : /* This represents the top of the VN lattice, which is the universal
412 : value. */
413 :
414 : tree VN_TOP;
415 :
416 : /* Unique counter for our value ids. */
417 :
418 : static unsigned int next_value_id;
419 : static int next_constant_value_id;
420 :
421 :
422 : /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
423 : are allocated on an obstack for locality reasons, and to free them
424 : without looping over the vec. */
425 :
426 : struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
427 : {
428 : typedef vn_ssa_aux_t value_type;
429 : typedef tree compare_type;
430 : static inline hashval_t hash (const value_type &);
431 : static inline bool equal (const value_type &, const compare_type &);
432 : static inline void mark_deleted (value_type &) {}
433 : static const bool empty_zero_p = true;
434 0 : static inline void mark_empty (value_type &e) { e = NULL; }
435 : static inline bool is_deleted (value_type &) { return false; }
436 >14536*10^7 : static inline bool is_empty (value_type &e) { return e == NULL; }
437 : };
438 :
439 : hashval_t
440 47987986900 : vn_ssa_aux_hasher::hash (const value_type &entry)
441 : {
442 47987986900 : return SSA_NAME_VERSION (entry->name);
443 : }
444 :
445 : bool
446 54887772572 : vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
447 : {
448 54887772572 : return name == entry->name;
449 : }
450 :
451 : static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
452 : typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
453 : static struct obstack vn_ssa_aux_obstack;
454 :
455 : static vn_nary_op_t vn_nary_op_insert_stmt (gassign *, tree);
456 : static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
457 : vn_nary_op_table_type *);
458 : static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
459 : enum tree_code, tree, tree *);
460 : static tree vn_lookup_simplify_result (gimple_match_op *);
461 : static vn_reference_t vn_reference_lookup_or_insert_for_pieces
462 : (tree, alias_set_type, alias_set_type, poly_int64, poly_int64, tree,
463 : vec<vn_reference_op_s, va_heap>, tree);
464 :
465 : /* Return whether there is value numbering information for a given SSA name. */
466 :
467 : bool
468 5285451 : has_VN_INFO (tree name)
469 : {
470 5285451 : return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
471 : }
472 :
473 : vn_ssa_aux_t
474 4241905454 : VN_INFO (tree name)
475 : {
476 4241905454 : vn_ssa_aux_t *res
477 4241905454 : = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
478 : INSERT);
479 4241905454 : if (*res != NULL)
480 : return *res;
481 :
482 179446362 : vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
483 179446362 : memset (newinfo, 0, sizeof (struct vn_ssa_aux));
484 179446362 : newinfo->name = name;
485 179446362 : newinfo->valnum = VN_TOP;
486 : /* We are using the visited flag to handle uses with defs not within the
487 : region being value-numbered. */
488 179446362 : newinfo->visited = false;
489 :
490 : /* Given we create the VN_INFOs on-demand now we have to do initialization
491 : different than VN_TOP here. */
492 179446362 : if (SSA_NAME_IS_DEFAULT_DEF (name))
493 9533901 : switch (TREE_CODE (SSA_NAME_VAR (name)))
494 : {
495 1723314 : case VAR_DECL:
496 : /* All undefined vars are VARYING. */
497 1723314 : newinfo->valnum = name;
498 1723314 : newinfo->visited = true;
499 1723314 : break;
500 :
501 7749640 : case PARM_DECL:
502 : /* Parameters are VARYING but we can record a condition
503 : if we know it is a non-NULL pointer. */
504 7749640 : newinfo->visited = true;
505 7749640 : newinfo->valnum = name;
506 11958872 : if (POINTER_TYPE_P (TREE_TYPE (name))
507 8937162 : && nonnull_arg_p (SSA_NAME_VAR (name)))
508 : {
509 2396014 : tree ops[2];
510 2396014 : ops[0] = name;
511 2396014 : ops[1] = build_int_cst (TREE_TYPE (name), 0);
512 2396014 : vn_nary_op_t nary;
513 : /* Allocate from non-unwinding stack. */
514 2396014 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
515 2396014 : init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
516 : boolean_type_node, ops);
517 2396014 : nary->predicated_values = 0;
518 2396014 : nary->u.result = boolean_true_node;
519 2396014 : vn_nary_op_insert_into (nary, valid_info->nary);
520 2396014 : gcc_assert (nary->unwind_to == NULL);
521 : /* Also do not link it into the undo chain. */
522 2396014 : last_inserted_nary = nary->next;
523 2396014 : nary->next = (vn_nary_op_t)(void *)-1;
524 2396014 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
525 2396014 : init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
526 : boolean_type_node, ops);
527 2396014 : nary->predicated_values = 0;
528 2396014 : nary->u.result = boolean_false_node;
529 2396014 : vn_nary_op_insert_into (nary, valid_info->nary);
530 2396014 : gcc_assert (nary->unwind_to == NULL);
531 2396014 : last_inserted_nary = nary->next;
532 2396014 : nary->next = (vn_nary_op_t)(void *)-1;
533 2396014 : if (dump_file && (dump_flags & TDF_DETAILS))
534 : {
535 38 : fprintf (dump_file, "Recording ");
536 38 : print_generic_expr (dump_file, name, TDF_SLIM);
537 38 : fprintf (dump_file, " != 0\n");
538 : }
539 : }
540 : break;
541 :
542 60947 : case RESULT_DECL:
543 : /* If the result is passed by invisible reference the default
544 : def is initialized, otherwise it's uninitialized. Still
545 : undefined is varying. */
546 60947 : newinfo->visited = true;
547 60947 : newinfo->valnum = name;
548 60947 : break;
549 :
550 0 : default:
551 0 : gcc_unreachable ();
552 : }
553 : return newinfo;
554 : }
555 :
556 : /* Return the SSA value of X. */
557 :
558 : inline tree
559 3632849621 : SSA_VAL (tree x, bool *visited = NULL)
560 : {
561 3632849621 : vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
562 3632849621 : if (visited)
563 1487687093 : *visited = tem && tem->visited;
564 3632849621 : return tem && tem->visited ? tem->valnum : x;
565 : }
566 :
567 : /* Return the SSA value of the VUSE x, supporting released VDEFs
568 : during elimination which will value-number the VDEF to the
569 : associated VUSE (but not substitute in the whole lattice). */
570 :
571 : static inline tree
572 1347130710 : vuse_ssa_val (tree x)
573 : {
574 1347130710 : if (!x)
575 : return NULL_TREE;
576 :
577 1343670839 : do
578 : {
579 1343670839 : x = SSA_VAL (x);
580 1343670839 : gcc_assert (x != VN_TOP);
581 : }
582 1343670839 : while (SSA_NAME_IN_FREE_LIST (x));
583 :
584 : return x;
585 : }
586 :
587 : /* Similar to the above but used as callback for walk_non_aliased_vuses
588 : and thus should stop at unvisited VUSE to not walk across region
589 : boundaries. */
590 :
591 : static tree
592 1145894989 : vuse_valueize (tree vuse)
593 : {
594 1145894997 : do
595 : {
596 1145894997 : bool visited;
597 1145894997 : vuse = SSA_VAL (vuse, &visited);
598 1145894997 : if (!visited)
599 17307712 : return NULL_TREE;
600 1128587285 : gcc_assert (vuse != VN_TOP);
601 : }
602 1128587285 : while (SSA_NAME_IN_FREE_LIST (vuse));
603 : return vuse;
604 : }
605 :
606 :
607 : /* Return the vn_kind the expression computed by the stmt should be
608 : associated with. */
609 :
610 : enum vn_kind
611 106381590 : vn_get_stmt_kind (gimple *stmt)
612 : {
613 106381590 : switch (gimple_code (stmt))
614 : {
615 : case GIMPLE_CALL:
616 : return VN_REFERENCE;
617 : case GIMPLE_PHI:
618 : return VN_PHI;
619 106381590 : case GIMPLE_ASSIGN:
620 106381590 : {
621 106381590 : enum tree_code code = gimple_assign_rhs_code (stmt);
622 106381590 : tree rhs1 = gimple_assign_rhs1 (stmt);
623 106381590 : switch (get_gimple_rhs_class (code))
624 : {
625 : case GIMPLE_UNARY_RHS:
626 : case GIMPLE_BINARY_RHS:
627 : case GIMPLE_TERNARY_RHS:
628 : return VN_NARY;
629 49722780 : case GIMPLE_SINGLE_RHS:
630 49722780 : switch (TREE_CODE_CLASS (code))
631 : {
632 37492528 : case tcc_reference:
633 : /* VOP-less references can go through unary case. */
634 37492528 : if ((code == REALPART_EXPR
635 : || code == IMAGPART_EXPR
636 37492528 : || code == VIEW_CONVERT_EXPR
637 37492528 : || code == BIT_FIELD_REF)
638 37492528 : && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
639 685112 : || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
640 : return VN_NARY;
641 :
642 : /* Fallthrough. */
643 : case tcc_declaration:
644 : return VN_REFERENCE;
645 :
646 : case tcc_constant:
647 : return VN_CONSTANT;
648 :
649 6100432 : default:
650 6100432 : if (code == ADDR_EXPR)
651 3290643 : return (is_gimple_min_invariant (rhs1)
652 3290643 : ? VN_CONSTANT : VN_REFERENCE);
653 2809789 : else if (code == CONSTRUCTOR)
654 : return VN_NARY;
655 : return VN_NONE;
656 : }
657 : default:
658 : return VN_NONE;
659 : }
660 : }
661 : default:
662 : return VN_NONE;
663 : }
664 : }
665 :
666 : /* Lookup a value id for CONSTANT and return it. If it does not
667 : exist returns 0. */
668 :
669 : unsigned int
670 0 : get_constant_value_id (tree constant)
671 : {
672 0 : vn_constant_s **slot;
673 0 : struct vn_constant_s vc;
674 :
675 0 : vc.hashcode = vn_hash_constant_with_type (constant);
676 0 : vc.constant = constant;
677 0 : slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
678 0 : if (slot)
679 0 : return (*slot)->value_id;
680 : return 0;
681 : }
682 :
683 : /* Lookup a value id for CONSTANT, and if it does not exist, create a
684 : new one and return it. If it does exist, return it. */
685 :
686 : unsigned int
687 29797337 : get_or_alloc_constant_value_id (tree constant)
688 : {
689 29797337 : vn_constant_s **slot;
690 29797337 : struct vn_constant_s vc;
691 29797337 : vn_constant_t vcp;
692 :
693 : /* If the hashtable isn't initialized we're not running from PRE and thus
694 : do not need value-ids. */
695 29797337 : if (!constant_to_value_id)
696 : return 0;
697 :
698 4887095 : vc.hashcode = vn_hash_constant_with_type (constant);
699 4887095 : vc.constant = constant;
700 4887095 : slot = constant_to_value_id->find_slot (&vc, INSERT);
701 4887095 : if (*slot)
702 2275292 : return (*slot)->value_id;
703 :
704 2611803 : vcp = XNEW (struct vn_constant_s);
705 2611803 : vcp->hashcode = vc.hashcode;
706 2611803 : vcp->constant = constant;
707 2611803 : vcp->value_id = get_next_constant_value_id ();
708 2611803 : *slot = vcp;
709 2611803 : return vcp->value_id;
710 : }
711 :
712 : /* Compute the hash for a reference operand VRO1. */
713 :
714 : static void
715 140746433 : vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
716 : {
717 140746433 : hstate.add_int (vro1->opcode);
718 140746433 : if (vro1->opcode == CALL_EXPR && !vro1->op0)
719 574940 : hstate.add_int (vro1->clique);
720 140746433 : if (vro1->op0)
721 134188715 : inchash::add_expr (vro1->op0, hstate);
722 140746433 : if (vro1->op1)
723 12588551 : inchash::add_expr (vro1->op1, hstate);
724 140746433 : if (vro1->op2)
725 14371779 : inchash::add_expr (vro1->op2, hstate);
726 140746433 : }
727 :
728 : /* Compute a hash for the reference operation VR1 and return it. */
729 :
730 : hashval_t
731 210008965 : vn_reference_compute_hash (const vn_reference_t vr1)
732 : {
733 210008965 : inchash::hash hstate;
734 210008965 : hashval_t result;
735 210008965 : int i;
736 210008965 : vn_reference_op_t vro;
737 210008965 : poly_offset_int off = -1;
738 210008965 : bool deref = false;
739 :
740 855293297 : FOR_EACH_VEC_ELT (vr1->operands, i, vro)
741 : {
742 645284332 : if (vro->opcode == MEM_REF)
743 : deref = true;
744 446137848 : else if (vro->opcode != ADDR_EXPR)
745 312935862 : deref = false;
746 645284332 : if (maybe_ne (vro->off, -1))
747 : {
748 379714503 : if (known_eq (off, -1))
749 201578736 : off = 0;
750 645284332 : off += vro->off;
751 : }
752 : else
753 : {
754 265569829 : if (maybe_ne (off, -1)
755 265569829 : && maybe_ne (off, 0))
756 107018518 : hstate.add_poly_hwi (off.force_shwi ());
757 265569829 : off = -1;
758 265569829 : if (deref
759 125035855 : && vro->opcode == ADDR_EXPR)
760 : {
761 124823396 : if (vro->op0)
762 : {
763 124823396 : tree op = TREE_OPERAND (vro->op0, 0);
764 124823396 : hstate.add_int (TREE_CODE (op));
765 124823396 : inchash::add_expr (op, hstate);
766 : }
767 : }
768 : else
769 140746433 : vn_reference_op_compute_hash (vro, hstate);
770 : }
771 : }
772 : /* Do not hash vr1->offset or vr1->max_size, we want to get collisions
773 : to be able to identify compatible results. */
774 210008965 : result = hstate.end ();
775 : /* ??? We would ICE later if we hash instead of adding that in. */
776 210008965 : if (vr1->vuse)
777 204959902 : result += SSA_NAME_VERSION (vr1->vuse);
778 :
779 210008965 : return result;
780 : }
781 :
782 : /* Return true if reference operations VR1 and VR2 are equivalent. This
783 : means they have the same set of operands and vuses. If LEXICAL
784 : is true then the full access path has to be the same. */
785 :
786 : bool
787 4766324194 : vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2,
788 : bool lexical)
789 : {
790 4766324194 : unsigned i, j;
791 :
792 : /* Early out if this is not a hash collision. */
793 4766324194 : if (vr1->hashcode != vr2->hashcode)
794 : return false;
795 :
796 : /* The VOP needs to be the same. */
797 18282271 : if (vr1->vuse != vr2->vuse)
798 : return false;
799 :
800 : /* The offset/max_size used for the ao_ref during lookup has to be
801 : the same. */
802 18281776 : if (maybe_ne (vr1->offset, vr2->offset)
803 18281776 : || maybe_ne (vr1->max_size, vr2->max_size))
804 : {
805 : /* But nothing known in the prevailing entry is OK to be used. */
806 7058653 : if (maybe_ne (vr1->offset, 0) || known_size_p (vr1->max_size))
807 : return false;
808 : }
809 :
810 : /* If the operands are the same we are done. */
811 36461720 : if (vr1->operands == vr2->operands)
812 : return true;
813 :
814 18230860 : if (!vr1->type || !vr2->type)
815 : {
816 551256 : if (vr1->type != vr2->type)
817 : return false;
818 : }
819 17679604 : else if (vr1->type == vr2->type)
820 : ;
821 2230487 : else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
822 2230487 : || (COMPLETE_TYPE_P (vr1->type)
823 2230487 : && !expressions_equal_p (TYPE_SIZE (vr1->type),
824 2230487 : TYPE_SIZE (vr2->type))))
825 : return false;
826 1472911 : else if (vr1->operands[0].opcode == CALL_EXPR
827 1472911 : && !types_compatible_p (vr1->type, vr2->type))
828 : return false;
829 1472911 : else if (INTEGRAL_TYPE_P (vr1->type)
830 585171 : && INTEGRAL_TYPE_P (vr2->type))
831 : {
832 544875 : if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
833 : return false;
834 : }
835 928036 : else if (INTEGRAL_TYPE_P (vr1->type)
836 928036 : && (TYPE_PRECISION (vr1->type)
837 40296 : != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
838 : return false;
839 927990 : else if (INTEGRAL_TYPE_P (vr2->type)
840 927990 : && (TYPE_PRECISION (vr2->type)
841 9420 : != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
842 : return false;
843 20460 : else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
844 927395 : && VECTOR_BOOLEAN_TYPE_P (vr2->type))
845 : {
846 : /* Vector boolean types can have padding, verify we are dealing with
847 : the same number of elements, aka the precision of the types.
848 : For example, In most architecture the precision_size of vbool*_t
849 : types are calculated like below:
850 : precision_size = type_size * 8
851 :
852 : Unfortunately, the RISC-V will adjust the precision_size for the
853 : vbool*_t in order to align the ISA as below:
854 : type_size = [1, 1, 1, 1, 2, 4, 8]
855 : precision_size = [1, 2, 4, 8, 16, 32, 64]
856 :
857 : Then the precision_size of RISC-V vbool*_t will not be the multiple
858 : of the type_size. We take care of this case consolidated here. */
859 0 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1->type),
860 0 : TYPE_VECTOR_SUBPARTS (vr2->type)))
861 : return false;
862 : }
863 927395 : else if (TYPE_MODE (vr1->type) != TYPE_MODE (vr2->type)
864 927395 : && (!mode_can_transfer_bits (TYPE_MODE (vr1->type))
865 45357 : || !mode_can_transfer_bits (TYPE_MODE (vr2->type))))
866 : return false;
867 :
868 17354388 : i = 0;
869 17354388 : j = 0;
870 22434250 : do
871 : {
872 22434250 : poly_offset_int off1 = 0, off2 = 0;
873 22434250 : vn_reference_op_t vro1, vro2;
874 22434250 : vn_reference_op_s tem1, tem2;
875 22434250 : bool deref1 = false, deref2 = false;
876 22434250 : bool reverse1 = false, reverse2 = false;
877 73056707 : for (; vr1->operands.iterate (i, &vro1); i++)
878 : {
879 50622457 : if (vro1->opcode == MEM_REF)
880 : deref1 = true;
881 : /* Do not look through a storage order barrier. */
882 34610898 : else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
883 141879 : return false;
884 50622457 : reverse1 |= vro1->reverse;
885 50622457 : if (lexical || known_eq (vro1->off, -1))
886 : break;
887 28188207 : off1 += vro1->off;
888 : }
889 50816527 : for (; vr2->operands.iterate (j, &vro2); j++)
890 : {
891 50816527 : if (vro2->opcode == MEM_REF)
892 : deref2 = true;
893 : /* Do not look through a storage order barrier. */
894 34780215 : else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
895 : return false;
896 50816527 : reverse2 |= vro2->reverse;
897 50816527 : if (lexical || known_eq (vro2->off, -1))
898 : break;
899 28382277 : off2 += vro2->off;
900 : }
901 22434250 : if (maybe_ne (off1, off2) || reverse1 != reverse2)
902 : return false;
903 22434131 : if (deref1 && vro1->opcode == ADDR_EXPR)
904 : {
905 8513201 : memset (&tem1, 0, sizeof (tem1));
906 8513201 : tem1.op0 = TREE_OPERAND (vro1->op0, 0);
907 8513201 : tem1.type = TREE_TYPE (tem1.op0);
908 8513201 : tem1.opcode = TREE_CODE (tem1.op0);
909 8513201 : vro1 = &tem1;
910 8513201 : deref1 = false;
911 : }
912 22434131 : if (deref2 && vro2->opcode == ADDR_EXPR)
913 : {
914 8513207 : memset (&tem2, 0, sizeof (tem2));
915 8513207 : tem2.op0 = TREE_OPERAND (vro2->op0, 0);
916 8513207 : tem2.type = TREE_TYPE (tem2.op0);
917 8513207 : tem2.opcode = TREE_CODE (tem2.op0);
918 8513207 : vro2 = &tem2;
919 8513207 : deref2 = false;
920 : }
921 22434131 : if (deref1 != deref2)
922 : return false;
923 22374026 : if (!vn_reference_op_eq (vro1, vro2))
924 : return false;
925 : /* Both alignment and alias set are not relevant for the produced
926 : value but need to be included when doing lexical comparison.
927 : We also need to make sure that the access path ends in an
928 : access of the same size as otherwise we might assume an access
929 : may not trap while in fact it might. */
930 22361302 : if (lexical
931 2344445 : && (vro1->opcode == MEM_REF
932 2344445 : || vro1->opcode == TARGET_MEM_REF)
933 23156138 : && (TYPE_ALIGN (vro1->type) != TYPE_ALIGN (vro2->type)
934 794629 : || (TYPE_SIZE (vro1->type) != TYPE_SIZE (vro2->type)
935 12 : && (! TYPE_SIZE (vro1->type)
936 12 : || ! TYPE_SIZE (vro2->type)
937 12 : || ! operand_equal_p (TYPE_SIZE (vro1->type),
938 12 : TYPE_SIZE (vro2->type))))
939 794617 : || vro1->clique != vro2->clique
940 728856 : || vro1->base != vro2->base
941 2186532 : || (get_deref_alias_set (vro1->opcode == MEM_REF
942 728844 : ? TREE_TYPE (vro1->op0)
943 0 : : TREE_TYPE (vro1->op2))
944 1457688 : != get_deref_alias_set (vro2->opcode == MEM_REF
945 728844 : ? TREE_TYPE (vro2->op0)
946 0 : : TREE_TYPE (vro2->op2)))))
947 : return false;
948 22292371 : ++j;
949 22292371 : ++i;
950 : }
951 44584742 : while (vr1->operands.length () != i
952 66877113 : || vr2->operands.length () != j);
953 :
954 : return true;
955 : }
956 :
957 : /* Copy the operations present in load/store REF into RESULT, a vector of
958 : vn_reference_op_s's. */
959 :
960 : void
961 228708477 : copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
962 : {
963 : /* For non-calls, store the information that makes up the address. */
964 228708477 : tree orig = ref;
965 794785923 : while (ref)
966 : {
967 566077446 : vn_reference_op_s temp;
968 :
969 566077446 : memset (&temp, 0, sizeof (temp));
970 566077446 : temp.type = TREE_TYPE (ref);
971 566077446 : temp.opcode = TREE_CODE (ref);
972 566077446 : temp.off = -1;
973 :
974 566077446 : switch (temp.opcode)
975 : {
976 15399335 : case MODIFY_EXPR:
977 15399335 : temp.op0 = TREE_OPERAND (ref, 1);
978 15399335 : break;
979 137 : case WITH_SIZE_EXPR:
980 137 : temp.op0 = TREE_OPERAND (ref, 1);
981 137 : temp.off = 0;
982 137 : break;
983 120428670 : case MEM_REF:
984 : /* The base address gets its own vn_reference_op_s structure. */
985 120428670 : temp.op0 = TREE_OPERAND (ref, 1);
986 120428670 : if (!mem_ref_offset (ref).to_shwi (&temp.off))
987 0 : temp.off = -1;
988 120428670 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
989 120428670 : temp.base = MR_DEPENDENCE_BASE (ref);
990 120428670 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
991 120428670 : break;
992 2561711 : case TARGET_MEM_REF:
993 : /* The base address gets its own vn_reference_op_s structure. */
994 2561711 : temp.op0 = TMR_INDEX (ref);
995 2561711 : temp.op1 = TMR_STEP (ref);
996 2561711 : temp.op2 = TMR_OFFSET (ref);
997 2561711 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
998 2561711 : temp.base = MR_DEPENDENCE_BASE (ref);
999 2561711 : result->safe_push (temp);
1000 2561711 : memset (&temp, 0, sizeof (temp));
1001 2561711 : temp.type = NULL_TREE;
1002 2561711 : temp.opcode = ERROR_MARK;
1003 2561711 : temp.op0 = TMR_INDEX2 (ref);
1004 2561711 : temp.off = -1;
1005 2561711 : break;
1006 820145 : case BIT_FIELD_REF:
1007 : /* Record bits, position and storage order. */
1008 820145 : temp.op0 = TREE_OPERAND (ref, 1);
1009 820145 : temp.op1 = TREE_OPERAND (ref, 2);
1010 1639449 : if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
1011 841 : temp.off = -1;
1012 820145 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
1013 820145 : break;
1014 151703941 : case COMPONENT_REF:
1015 151703941 : temp.op0 = TREE_OPERAND (ref, 1);
1016 151703941 : temp.op1 = TREE_OPERAND (ref, 2);
1017 303407299 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1018 303405185 : && TYPE_REVERSE_STORAGE_ORDER
1019 : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1020 151703941 : {
1021 151703941 : tree this_offset = component_ref_field_offset (ref);
1022 151703941 : if (this_offset
1023 151703941 : && poly_int_tree_p (this_offset))
1024 : {
1025 151701843 : tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
1026 151701843 : if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
1027 : {
1028 151209273 : poly_offset_int off
1029 151209273 : = (wi::to_poly_offset (this_offset)
1030 151209273 : + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
1031 : /* Prohibit value-numbering zero offset components
1032 : of addresses the same before the pass folding
1033 : __builtin_object_size had a chance to run. Likewise
1034 : for components of zero size at arbitrary offset. */
1035 151209273 : if (TREE_CODE (orig) != ADDR_EXPR
1036 4929757 : || (TYPE_SIZE (temp.type)
1037 4916259 : && integer_nonzerop (TYPE_SIZE (temp.type))
1038 154263070 : && maybe_ne (off, 0))
1039 154278734 : || (cfun->curr_properties & PROP_objsz))
1040 149733846 : off.to_shwi (&temp.off);
1041 : }
1042 : }
1043 : }
1044 : break;
1045 39722751 : case ARRAY_RANGE_REF:
1046 39722751 : case ARRAY_REF:
1047 39722751 : {
1048 39722751 : tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
1049 : /* Record index as operand. */
1050 39722751 : temp.op0 = TREE_OPERAND (ref, 1);
1051 : /* Always record lower bounds and element size. */
1052 39722751 : temp.op1 = array_ref_low_bound (ref);
1053 : /* But record element size in units of the type alignment. */
1054 39722751 : temp.op2 = TREE_OPERAND (ref, 3);
1055 39722751 : temp.align = eltype->type_common.align;
1056 39722751 : if (! temp.op2)
1057 39511666 : temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
1058 : size_int (TYPE_ALIGN_UNIT (eltype)));
1059 : /* Prohibit value-numbering addresses of one-after-the-last
1060 : element ARRAY_REFs the same as addresses of other components
1061 : before the pass folding __builtin_object_size had a chance
1062 : to run. */
1063 39722751 : bool avoid_oob = true;
1064 39722751 : if (TREE_CODE (orig) != ADDR_EXPR
1065 493146 : || cfun->curr_properties & PROP_objsz)
1066 : avoid_oob = false;
1067 231040 : else if (poly_int_tree_p (temp.op0))
1068 : {
1069 79864 : tree ub = array_ref_up_bound (ref);
1070 79864 : if (ub
1071 78071 : && poly_int_tree_p (ub)
1072 : /* ??? The C frontend for T[0] uses [0:] and the
1073 : C++ frontend [0:-1U]. See layout_type for how
1074 : awkward this is. */
1075 68928 : && !integer_minus_onep (ub)
1076 157935 : && known_le (wi::to_poly_offset (temp.op0),
1077 : wi::to_poly_offset (ub)))
1078 68091 : avoid_oob = false;
1079 : }
1080 39722751 : if (poly_int_tree_p (temp.op0)
1081 22898489 : && poly_int_tree_p (temp.op1)
1082 22898465 : && TREE_CODE (temp.op2) == INTEGER_CST
1083 62559776 : && !avoid_oob)
1084 : {
1085 45653030 : poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1086 68479545 : - wi::to_poly_offset (temp.op1))
1087 45653030 : * wi::to_offset (temp.op2)
1088 22826515 : * vn_ref_op_align_unit (&temp));
1089 22826515 : off.to_shwi (&temp.off);
1090 : }
1091 39722751 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1092 39722751 : && TYPE_REVERSE_STORAGE_ORDER
1093 : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1094 : }
1095 39722751 : break;
1096 84620637 : case VAR_DECL:
1097 84620637 : if (DECL_HARD_REGISTER (ref))
1098 : {
1099 20805 : temp.op0 = ref;
1100 20805 : break;
1101 : }
1102 : /* Fallthru. */
1103 88038298 : case PARM_DECL:
1104 88038298 : case CONST_DECL:
1105 88038298 : case RESULT_DECL:
1106 : /* Canonicalize decls to MEM[&decl] which is what we end up with
1107 : when valueizing MEM[ptr] with ptr = &decl. */
1108 88038298 : temp.opcode = MEM_REF;
1109 88038298 : temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1110 88038298 : temp.off = 0;
1111 88038298 : result->safe_push (temp);
1112 88038298 : temp.opcode = ADDR_EXPR;
1113 88038298 : temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1114 88038298 : temp.type = TREE_TYPE (temp.op0);
1115 88038298 : temp.off = -1;
1116 88038298 : break;
1117 98629629 : case STRING_CST:
1118 98629629 : case INTEGER_CST:
1119 98629629 : case POLY_INT_CST:
1120 98629629 : case COMPLEX_CST:
1121 98629629 : case VECTOR_CST:
1122 98629629 : case REAL_CST:
1123 98629629 : case FIXED_CST:
1124 98629629 : case CONSTRUCTOR:
1125 98629629 : case SSA_NAME:
1126 98629629 : temp.op0 = ref;
1127 98629629 : break;
1128 46266185 : case ADDR_EXPR:
1129 46266185 : if (is_gimple_min_invariant (ref))
1130 : {
1131 42019745 : temp.op0 = ref;
1132 42019745 : break;
1133 : }
1134 : break;
1135 : /* These are only interesting for their operands, their
1136 : existence, and their type. They will never be the last
1137 : ref in the chain of references (IE they require an
1138 : operand), so we don't have to put anything
1139 : for op* as it will be handled by the iteration */
1140 498226 : case REALPART_EXPR:
1141 498226 : temp.off = 0;
1142 498226 : break;
1143 1482465 : case VIEW_CONVERT_EXPR:
1144 1482465 : temp.off = 0;
1145 1482465 : temp.reverse = storage_order_barrier_p (ref);
1146 1482465 : break;
1147 505148 : case IMAGPART_EXPR:
1148 : /* This is only interesting for its constant offset. */
1149 505148 : temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1150 505148 : break;
1151 0 : default:
1152 0 : gcc_unreachable ();
1153 : }
1154 566077446 : result->safe_push (temp);
1155 :
1156 566077446 : if (REFERENCE_CLASS_P (ref)
1157 248354389 : || TREE_CODE (ref) == MODIFY_EXPR
1158 232955054 : || TREE_CODE (ref) == WITH_SIZE_EXPR
1159 799032363 : || (TREE_CODE (ref) == ADDR_EXPR
1160 46266185 : && !is_gimple_min_invariant (ref)))
1161 337368969 : ref = TREE_OPERAND (ref, 0);
1162 : else
1163 : ref = NULL_TREE;
1164 : }
1165 228708477 : }
1166 :
1167 : /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1168 : operands in *OPS, the reference alias set SET and the reference type TYPE.
1169 : Return true if something useful was produced. */
1170 :
1171 : bool
1172 15094387 : ao_ref_init_from_vn_reference (ao_ref *ref,
1173 : alias_set_type set, alias_set_type base_set,
1174 : tree type, const vec<vn_reference_op_s> &ops)
1175 : {
1176 15094387 : unsigned i;
1177 15094387 : tree base = NULL_TREE;
1178 15094387 : tree *op0_p = &base;
1179 15094387 : poly_offset_int offset = 0;
1180 15094387 : poly_offset_int max_size;
1181 15094387 : poly_offset_int size = -1;
1182 15094387 : tree size_tree = NULL_TREE;
1183 :
1184 : /* We don't handle calls. */
1185 15094387 : if (!type)
1186 : return false;
1187 :
1188 15094387 : machine_mode mode = TYPE_MODE (type);
1189 15094387 : if (mode == BLKmode)
1190 42147 : size_tree = TYPE_SIZE (type);
1191 : else
1192 30104480 : size = GET_MODE_BITSIZE (mode);
1193 15052240 : if (size_tree != NULL_TREE
1194 42147 : && poly_int_tree_p (size_tree))
1195 42147 : size = wi::to_poly_offset (size_tree);
1196 :
1197 : /* Lower the final access size from the outermost expression. */
1198 15094387 : const_vn_reference_op_t cst_op = &ops[0];
1199 : /* Cast away constness for the sake of the const-unsafe
1200 : FOR_EACH_VEC_ELT(). */
1201 15094387 : vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1202 15094387 : size_tree = NULL_TREE;
1203 15094387 : if (op->opcode == COMPONENT_REF)
1204 5159471 : size_tree = DECL_SIZE (op->op0);
1205 9934916 : else if (op->opcode == BIT_FIELD_REF)
1206 75584 : size_tree = op->op0;
1207 5235055 : if (size_tree != NULL_TREE
1208 5235055 : && poly_int_tree_p (size_tree)
1209 10470110 : && (!known_size_p (size)
1210 15094387 : || known_lt (wi::to_poly_offset (size_tree), size)))
1211 47127 : size = wi::to_poly_offset (size_tree);
1212 :
1213 : /* Initially, maxsize is the same as the accessed element size.
1214 : In the following it will only grow (or become -1). */
1215 15094387 : max_size = size;
1216 :
1217 : /* Compute cumulative bit-offset for nested component-refs and array-refs,
1218 : and find the ultimate containing object. */
1219 57856674 : FOR_EACH_VEC_ELT (ops, i, op)
1220 : {
1221 42917755 : switch (op->opcode)
1222 : {
1223 : case CALL_EXPR:
1224 : return false;
1225 :
1226 : /* Record the base objects. */
1227 14637238 : case MEM_REF:
1228 14637238 : *op0_p = build2 (MEM_REF, op->type,
1229 : NULL_TREE, op->op0);
1230 14637238 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1231 14637238 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1232 14637238 : op0_p = &TREE_OPERAND (*op0_p, 0);
1233 14637238 : break;
1234 :
1235 301123 : case TARGET_MEM_REF:
1236 903369 : *op0_p = build5 (TARGET_MEM_REF, op->type,
1237 : NULL_TREE, op->op2, op->op0,
1238 301123 : op->op1, ops[i+1].op0);
1239 301123 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1240 301123 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1241 301123 : op0_p = &TREE_OPERAND (*op0_p, 0);
1242 301123 : ++i;
1243 301123 : break;
1244 :
1245 : /* Unwrap some of the wrapped decls. */
1246 6751750 : case ADDR_EXPR:
1247 : /* Apart from ADDR_EXPR arguments to MEM_REF. */
1248 6751750 : if (base != NULL_TREE
1249 6751749 : && TREE_CODE (base) == MEM_REF
1250 6719979 : && op->op0
1251 13471729 : && DECL_P (TREE_OPERAND (op->op0, 0)))
1252 : {
1253 6711005 : const_vn_reference_op_t pop = &ops[i-1];
1254 6711005 : base = TREE_OPERAND (op->op0, 0);
1255 6711005 : if (known_eq (pop->off, -1))
1256 : {
1257 27 : max_size = -1;
1258 27 : offset = 0;
1259 : }
1260 : else
1261 20132934 : offset += poly_offset_int (pop->off) * BITS_PER_UNIT;
1262 : op0_p = NULL;
1263 : break;
1264 : }
1265 : /* Fallthru. */
1266 8227914 : case PARM_DECL:
1267 8227914 : case CONST_DECL:
1268 8227914 : case RESULT_DECL:
1269 : /* ??? We shouldn't see these, but un-canonicalize what
1270 : copy_reference_ops_from_ref does when visiting MEM_REF. */
1271 8227914 : case VAR_DECL:
1272 : /* ??? And for this only have DECL_HARD_REGISTER. */
1273 8227914 : case STRING_CST:
1274 : /* This can show up in ARRAY_REF bases. */
1275 8227914 : case INTEGER_CST:
1276 8227914 : case SSA_NAME:
1277 8227914 : *op0_p = op->op0;
1278 8227914 : op0_p = NULL;
1279 8227914 : break;
1280 :
1281 : /* And now the usual component-reference style ops. */
1282 75584 : case BIT_FIELD_REF:
1283 75584 : offset += wi::to_poly_offset (op->op1);
1284 75584 : break;
1285 :
1286 8395959 : case COMPONENT_REF:
1287 8395959 : {
1288 8395959 : tree field = op->op0;
1289 : /* We do not have a complete COMPONENT_REF tree here so we
1290 : cannot use component_ref_field_offset. Do the interesting
1291 : parts manually. */
1292 8395959 : tree this_offset = DECL_FIELD_OFFSET (field);
1293 :
1294 8395959 : if (op->op1 || !poly_int_tree_p (this_offset))
1295 233 : max_size = -1;
1296 : else
1297 : {
1298 8395726 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1299 8395726 : << LOG2_BITS_PER_UNIT);
1300 8395726 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1301 8395726 : offset += woffset;
1302 : }
1303 : break;
1304 : }
1305 :
1306 3207234 : case ARRAY_RANGE_REF:
1307 3207234 : case ARRAY_REF:
1308 : /* Use the recorded constant offset. */
1309 3207234 : if (maybe_eq (op->off, -1))
1310 1241442 : max_size = -1;
1311 : else
1312 5897376 : offset += poly_offset_int (op->off) * BITS_PER_UNIT;
1313 : break;
1314 :
1315 : case REALPART_EXPR:
1316 : break;
1317 :
1318 : case IMAGPART_EXPR:
1319 42762287 : offset += size;
1320 : break;
1321 :
1322 : case VIEW_CONVERT_EXPR:
1323 : break;
1324 :
1325 : case POLY_INT_CST:
1326 : case COMPLEX_CST:
1327 : case VECTOR_CST:
1328 : case REAL_CST:
1329 : case FIXED_CST:
1330 : case CONSTRUCTOR:
1331 : return false;
1332 :
1333 : default:
1334 : return false;
1335 : }
1336 : }
1337 :
1338 14938919 : if (base == NULL_TREE)
1339 : return false;
1340 :
1341 14938919 : ref->ref = NULL_TREE;
1342 14938919 : ref->base = base;
1343 14938919 : ref->ref_alias_set = set;
1344 14938919 : ref->base_alias_set = base_set;
1345 : /* We discount volatiles from value-numbering elsewhere. */
1346 14938919 : ref->volatile_p = false;
1347 :
1348 14938919 : if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1349 : {
1350 0 : ref->offset = 0;
1351 0 : ref->size = -1;
1352 0 : ref->max_size = -1;
1353 0 : return true;
1354 : }
1355 :
1356 14938919 : if (!offset.to_shwi (&ref->offset))
1357 : {
1358 26 : ref->offset = 0;
1359 26 : ref->max_size = -1;
1360 26 : return true;
1361 : }
1362 :
1363 14938893 : if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1364 1094886 : ref->max_size = -1;
1365 :
1366 : return true;
1367 : }
1368 :
1369 : /* Copy the operations present in load/store/call REF into RESULT, a vector of
1370 : vn_reference_op_s's. */
1371 :
1372 : static void
1373 9397296 : copy_reference_ops_from_call (gcall *call,
1374 : vec<vn_reference_op_s> *result)
1375 : {
1376 9397296 : vn_reference_op_s temp;
1377 9397296 : unsigned i;
1378 9397296 : tree lhs = gimple_call_lhs (call);
1379 9397296 : int lr;
1380 :
1381 : /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1382 : different. By adding the lhs here in the vector, we ensure that the
1383 : hashcode is different, guaranteeing a different value number. */
1384 9397296 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
1385 : {
1386 449831 : memset (&temp, 0, sizeof (temp));
1387 449831 : temp.opcode = MODIFY_EXPR;
1388 449831 : temp.type = TREE_TYPE (lhs);
1389 449831 : temp.op0 = lhs;
1390 449831 : temp.off = -1;
1391 449831 : result->safe_push (temp);
1392 : }
1393 :
1394 : /* Copy the type, opcode, function, static chain and EH region, if any. */
1395 9397296 : memset (&temp, 0, sizeof (temp));
1396 9397296 : temp.type = gimple_call_fntype (call);
1397 9397296 : temp.opcode = CALL_EXPR;
1398 9397296 : temp.op0 = gimple_call_fn (call);
1399 9397296 : if (gimple_call_internal_p (call))
1400 556972 : temp.clique = gimple_call_internal_fn (call);
1401 9397296 : temp.op1 = gimple_call_chain (call);
1402 9397296 : if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1403 635705 : temp.op2 = size_int (lr);
1404 9397296 : temp.off = -1;
1405 9397296 : result->safe_push (temp);
1406 :
1407 : /* Copy the call arguments. As they can be references as well,
1408 : just chain them together. */
1409 37263483 : for (i = 0; i < gimple_call_num_args (call); ++i)
1410 : {
1411 18468891 : tree callarg = gimple_call_arg (call, i);
1412 18468891 : copy_reference_ops_from_ref (callarg, result);
1413 : }
1414 9397296 : }
1415 :
1416 : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1417 : *I_P to point to the last element of the replacement. */
1418 : static bool
1419 130804195 : vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1420 : unsigned int *i_p)
1421 : {
1422 130804195 : unsigned int i = *i_p;
1423 130804195 : vn_reference_op_t op = &(*ops)[i];
1424 130804195 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1425 130804195 : tree addr_base;
1426 130804195 : poly_int64 addr_offset = 0;
1427 :
1428 : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1429 : from .foo.bar to the preceding MEM_REF offset and replace the
1430 : address with &OBJ. */
1431 130804195 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1432 : &addr_offset, vn_valueize);
1433 130804195 : gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1434 130804195 : if (addr_base != TREE_OPERAND (op->op0, 0))
1435 : {
1436 713456 : poly_offset_int off
1437 713456 : = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1438 : SIGNED)
1439 713456 : + addr_offset);
1440 713456 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1441 713456 : op->op0 = build_fold_addr_expr (addr_base);
1442 713456 : if (tree_fits_shwi_p (mem_op->op0))
1443 713379 : mem_op->off = tree_to_shwi (mem_op->op0);
1444 : else
1445 : mem_op->off = -1;
1446 713456 : return true;
1447 : }
1448 : return false;
1449 : }
1450 :
1451 : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1452 : *I_P to point to the last element of the replacement. */
1453 : static bool
1454 87885889 : vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1455 : unsigned int *i_p)
1456 : {
1457 87885889 : bool changed = false;
1458 95761850 : vn_reference_op_t op;
1459 :
1460 95761850 : do
1461 : {
1462 95761850 : unsigned int i = *i_p;
1463 95761850 : op = &(*ops)[i];
1464 95761850 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1465 95761850 : enum tree_code code;
1466 95761850 : poly_offset_int off;
1467 :
1468 95761850 : auto def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op->op0));
1469 38968219 : if (!def_stmt)
1470 87883934 : return changed;
1471 :
1472 38968219 : code = gimple_assign_rhs_code (def_stmt);
1473 38968219 : if (code != ADDR_EXPR
1474 38968219 : && code != POINTER_PLUS_EXPR)
1475 : return changed;
1476 :
1477 20808365 : off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1478 :
1479 : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1480 : from .foo.bar to the preceding MEM_REF offset and replace the
1481 : address with &OBJ. */
1482 20808365 : if (code == ADDR_EXPR)
1483 : {
1484 950235 : tree addr, addr_base;
1485 950235 : poly_int64 addr_offset;
1486 :
1487 950235 : addr = gimple_assign_rhs1 (def_stmt);
1488 950235 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1489 : &addr_offset,
1490 : vn_valueize);
1491 : /* If that didn't work because the address isn't invariant propagate
1492 : the reference tree from the address operation in case the current
1493 : dereference isn't offsetted. */
1494 950235 : if (!addr_base
1495 285380 : && *i_p == ops->length () - 1
1496 142690 : && known_eq (off, 0)
1497 : /* This makes us disable this transform for PRE where the
1498 : reference ops might be also used for code insertion which
1499 : is invalid. */
1500 1036685 : && default_vn_walk_kind == VN_WALKREWRITE)
1501 : {
1502 86360 : auto_vec<vn_reference_op_s, 32> tem;
1503 86360 : copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1504 : /* Make sure to preserve TBAA info. The only objects not
1505 : wrapped in MEM_REFs that can have their address taken are
1506 : STRING_CSTs. */
1507 86360 : if (tem.length () >= 2
1508 86360 : && tem[tem.length () - 2].opcode == MEM_REF)
1509 : {
1510 86345 : vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1511 86345 : new_mem_op->op0
1512 86345 : = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1513 172690 : wi::to_poly_wide (new_mem_op->op0));
1514 : }
1515 : /* Do not forward addresses of TARGET_MEM_REF. */
1516 15 : else if (tem[0].opcode == TARGET_MEM_REF)
1517 : return changed;
1518 : else
1519 15 : gcc_assert (tem.last ().opcode == STRING_CST);
1520 86360 : ops->pop ();
1521 86360 : ops->pop ();
1522 86360 : ops->safe_splice (tem);
1523 86360 : --*i_p;
1524 86360 : return true;
1525 86360 : }
1526 863875 : if (!addr_base
1527 807545 : || TREE_CODE (addr_base) != MEM_REF
1528 1669514 : || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1529 803774 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1530 : 0))))
1531 : return changed;
1532 :
1533 805639 : off += addr_offset;
1534 805639 : off += mem_ref_offset (addr_base);
1535 805639 : op->op0 = TREE_OPERAND (addr_base, 0);
1536 : }
1537 : else
1538 : {
1539 19858130 : tree ptr, ptroff;
1540 19858130 : ptr = gimple_assign_rhs1 (def_stmt);
1541 19858130 : ptroff = gimple_assign_rhs2 (def_stmt);
1542 19858130 : if (TREE_CODE (ptr) != SSA_NAME
1543 18116258 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1544 : /* Make sure to not endlessly recurse.
1545 : See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1546 : happen when we value-number a PHI to its backedge value. */
1547 18114833 : || SSA_VAL (ptr) == op->op0
1548 37972963 : || !poly_int_tree_p (ptroff))
1549 : return changed;
1550 :
1551 7072277 : off += wi::to_poly_offset (ptroff);
1552 7072277 : op->op0 = ptr;
1553 : }
1554 :
1555 7877916 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1556 7877916 : if (tree_fits_shwi_p (mem_op->op0))
1557 7592721 : mem_op->off = tree_to_shwi (mem_op->op0);
1558 : else
1559 : mem_op->off = -1;
1560 : /* ??? Can end up with endless recursion here!?
1561 : gcc.c-torture/execute/strcmp-1.c */
1562 7877916 : if (TREE_CODE (op->op0) == SSA_NAME)
1563 7876051 : op->op0 = SSA_VAL (op->op0);
1564 7877916 : if (TREE_CODE (op->op0) != SSA_NAME)
1565 1955 : op->opcode = TREE_CODE (op->op0);
1566 :
1567 7877916 : changed = true;
1568 : }
1569 : /* Tail-recurse. */
1570 7877916 : while (TREE_CODE (op->op0) == SSA_NAME);
1571 :
1572 : /* Fold a remaining *&. */
1573 1955 : if (TREE_CODE (op->op0) == ADDR_EXPR)
1574 265 : vn_reference_fold_indirect (ops, i_p);
1575 :
1576 : return changed;
1577 : }
1578 :
1579 : /* Optimize the reference REF to a constant if possible or return
1580 : NULL_TREE if not. */
1581 :
1582 : tree
1583 113258236 : fully_constant_vn_reference_p (vn_reference_t ref)
1584 : {
1585 113258236 : vec<vn_reference_op_s> operands = ref->operands;
1586 113258236 : vn_reference_op_t op;
1587 :
1588 : /* Try to simplify the translated expression if it is
1589 : a call to a builtin function with at most two arguments. */
1590 113258236 : op = &operands[0];
1591 113258236 : if (op->opcode == CALL_EXPR
1592 94258 : && (!op->op0
1593 85248 : || (TREE_CODE (op->op0) == ADDR_EXPR
1594 85248 : && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1595 85248 : && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1596 : BUILT_IN_NORMAL)))
1597 76553 : && operands.length () >= 2
1598 113334757 : && operands.length () <= 3)
1599 : {
1600 38399 : vn_reference_op_t arg0, arg1 = NULL;
1601 38399 : bool anyconst = false;
1602 38399 : arg0 = &operands[1];
1603 38399 : if (operands.length () > 2)
1604 5776 : arg1 = &operands[2];
1605 38399 : if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1606 38399 : || (arg0->opcode == ADDR_EXPR
1607 13838 : && is_gimple_min_invariant (arg0->op0)))
1608 : anyconst = true;
1609 38399 : if (arg1
1610 38399 : && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1611 4267 : || (arg1->opcode == ADDR_EXPR
1612 587 : && is_gimple_min_invariant (arg1->op0))))
1613 : anyconst = true;
1614 36303 : if (anyconst)
1615 : {
1616 26139 : combined_fn fn;
1617 26139 : if (op->op0)
1618 25176 : fn = as_combined_fn (DECL_FUNCTION_CODE
1619 25176 : (TREE_OPERAND (op->op0, 0)));
1620 : else
1621 963 : fn = as_combined_fn ((internal_fn) op->clique);
1622 26139 : tree folded;
1623 26139 : if (arg1)
1624 2744 : folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1625 : else
1626 23395 : folded = fold_const_call (fn, ref->type, arg0->op0);
1627 26139 : if (folded
1628 26139 : && is_gimple_min_invariant (folded))
1629 1048 : return folded;
1630 : }
1631 : }
1632 :
1633 : /* Simplify reads from constants or constant initializers. */
1634 113219837 : else if (BITS_PER_UNIT == 8
1635 113219837 : && ref->type
1636 113219837 : && COMPLETE_TYPE_P (ref->type)
1637 226439632 : && is_gimple_reg_type (ref->type))
1638 : {
1639 108772074 : poly_int64 off = 0;
1640 108772074 : HOST_WIDE_INT size;
1641 108772074 : if (INTEGRAL_TYPE_P (ref->type))
1642 55555414 : size = TYPE_PRECISION (ref->type);
1643 53216660 : else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1644 53216660 : size = tree_to_shwi (TYPE_SIZE (ref->type));
1645 : else
1646 : return NULL_TREE;
1647 108772074 : if (size % BITS_PER_UNIT != 0
1648 106905484 : || size > MAX_BITSIZE_MODE_ANY_MODE)
1649 : return NULL_TREE;
1650 106903588 : size /= BITS_PER_UNIT;
1651 106903588 : unsigned i;
1652 198106761 : for (i = 0; i < operands.length (); ++i)
1653 : {
1654 198106761 : if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1655 : {
1656 307 : ++i;
1657 307 : break;
1658 : }
1659 198106454 : if (operands[i].reverse)
1660 : return NULL_TREE;
1661 198097653 : if (known_eq (operands[i].off, -1))
1662 : return NULL_TREE;
1663 183850844 : off += operands[i].off;
1664 183850844 : if (operands[i].opcode == MEM_REF)
1665 : {
1666 92647671 : ++i;
1667 92647671 : break;
1668 : }
1669 : }
1670 92647978 : vn_reference_op_t base = &operands[--i];
1671 92647978 : tree ctor = error_mark_node;
1672 92647978 : tree decl = NULL_TREE;
1673 92647978 : if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1674 307 : ctor = base->op0;
1675 92647671 : else if (base->opcode == MEM_REF
1676 92647671 : && base[1].opcode == ADDR_EXPR
1677 152216767 : && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1678 3592239 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1679 3592179 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1680 : {
1681 55982947 : decl = TREE_OPERAND (base[1].op0, 0);
1682 55982947 : if (TREE_CODE (decl) == STRING_CST)
1683 : ctor = decl;
1684 : else
1685 55976917 : ctor = ctor_for_folding (decl);
1686 : }
1687 92641948 : if (ctor == NULL_TREE)
1688 411 : return build_zero_cst (ref->type);
1689 92647567 : else if (ctor != error_mark_node)
1690 : {
1691 104435 : HOST_WIDE_INT const_off;
1692 104435 : if (decl)
1693 : {
1694 208256 : tree res = fold_ctor_reference (ref->type, ctor,
1695 104128 : off * BITS_PER_UNIT,
1696 104128 : size * BITS_PER_UNIT, decl);
1697 104128 : if (res)
1698 : {
1699 61998 : STRIP_USELESS_TYPE_CONVERSION (res);
1700 61998 : if (is_gimple_min_invariant (res))
1701 61866 : return res;
1702 : }
1703 : }
1704 307 : else if (off.is_constant (&const_off))
1705 : {
1706 307 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1707 307 : int len = native_encode_expr (ctor, buf, size, const_off);
1708 307 : if (len > 0)
1709 137 : return native_interpret_expr (ref->type, buf, len);
1710 : }
1711 : }
1712 : }
1713 :
1714 : return NULL_TREE;
1715 : }
1716 :
1717 : /* Return true if OPS contain a storage order barrier. */
1718 :
1719 : static bool
1720 61023911 : contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1721 : {
1722 61023911 : vn_reference_op_t op;
1723 61023911 : unsigned i;
1724 :
1725 239124845 : FOR_EACH_VEC_ELT (ops, i, op)
1726 178100934 : if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1727 : return true;
1728 :
1729 : return false;
1730 : }
1731 :
1732 : /* Return true if OPS represent an access with reverse storage order. */
1733 :
1734 : static bool
1735 63095364 : reverse_storage_order_for_component_p (const vec<vn_reference_op_s> &ops)
1736 : {
1737 63095364 : unsigned i = 0;
1738 63095364 : if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1739 : ++i;
1740 63095364 : switch (ops[i].opcode)
1741 : {
1742 60378078 : case ARRAY_REF:
1743 60378078 : case COMPONENT_REF:
1744 60378078 : case BIT_FIELD_REF:
1745 60378078 : case MEM_REF:
1746 60378078 : return ops[i].reverse;
1747 : default:
1748 : return false;
1749 : }
1750 : }
1751 :
1752 : /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1753 : structures into their value numbers. This is done in-place, and
1754 : the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1755 : whether any operands were valueized. */
1756 :
1757 : static void
1758 225774628 : valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1759 : bool with_avail = false)
1760 : {
1761 225774628 : *valueized_anything = false;
1762 :
1763 912127330 : for (unsigned i = 0; i < orig->length (); ++i)
1764 : {
1765 686352702 : re_valueize:
1766 690565404 : vn_reference_op_t vro = &(*orig)[i];
1767 690565404 : if (vro->opcode == SSA_NAME
1768 590446090 : || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1769 : {
1770 125114008 : tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1771 125114008 : if (tem != vro->op0)
1772 : {
1773 18767066 : *valueized_anything = true;
1774 18767066 : vro->op0 = tem;
1775 : }
1776 : /* If it transforms from an SSA_NAME to a constant, update
1777 : the opcode. */
1778 125114008 : if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1779 2229756 : vro->opcode = TREE_CODE (vro->op0);
1780 : }
1781 690565404 : if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1782 : {
1783 26271 : tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1784 26271 : if (tem != vro->op1)
1785 : {
1786 586 : *valueized_anything = true;
1787 586 : vro->op1 = tem;
1788 : }
1789 : }
1790 690565404 : if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1791 : {
1792 205363 : tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1793 205363 : if (tem != vro->op2)
1794 : {
1795 120114 : *valueized_anything = true;
1796 120114 : vro->op2 = tem;
1797 : }
1798 : }
1799 : /* If it transforms from an SSA_NAME to an address, fold with
1800 : a preceding indirect reference. */
1801 690565404 : if (i > 0
1802 464711020 : && vro->op0
1803 461116705 : && TREE_CODE (vro->op0) == ADDR_EXPR
1804 827369933 : && (*orig)[i - 1].opcode == MEM_REF)
1805 : {
1806 130803930 : if (vn_reference_fold_indirect (orig, &i))
1807 713456 : *valueized_anything = true;
1808 : }
1809 559761474 : else if (i > 0
1810 333907090 : && vro->opcode == SSA_NAME
1811 657651032 : && (*orig)[i - 1].opcode == MEM_REF)
1812 : {
1813 87885889 : if (vn_reference_maybe_forwprop_address (orig, &i))
1814 : {
1815 4212702 : *valueized_anything = true;
1816 : /* Re-valueize the current operand. */
1817 4212702 : goto re_valueize;
1818 : }
1819 : }
1820 : /* If it transforms a non-constant ARRAY_REF into a constant
1821 : one, adjust the constant offset. */
1822 471875585 : else if ((vro->opcode == ARRAY_REF
1823 471875585 : || vro->opcode == ARRAY_RANGE_REF)
1824 40879174 : && known_eq (vro->off, -1)
1825 17657179 : && poly_int_tree_p (vro->op0)
1826 5172735 : && poly_int_tree_p (vro->op1)
1827 477048320 : && TREE_CODE (vro->op2) == INTEGER_CST)
1828 : {
1829 : /* Prohibit value-numbering addresses of one-after-the-last
1830 : element ARRAY_REFs the same as addresses of other components
1831 : before the pass folding __builtin_object_size had a chance
1832 : to run. */
1833 5038166 : if (!(cfun->curr_properties & PROP_objsz)
1834 6294933 : && (*orig)[0].opcode == ADDR_EXPR)
1835 : {
1836 36639 : tree dom = TYPE_DOMAIN ((*orig)[i + 1].type);
1837 56121 : if (!dom
1838 36489 : || !TYPE_MAX_VALUE (dom)
1839 26098 : || !poly_int_tree_p (TYPE_MAX_VALUE (dom))
1840 53872 : || integer_minus_onep (TYPE_MAX_VALUE (dom)))
1841 20289 : continue;
1842 17157 : if (!known_le (wi::to_poly_offset (vro->op0),
1843 : wi::to_poly_offset (TYPE_MAX_VALUE (dom))))
1844 807 : continue;
1845 : }
1846 :
1847 10035754 : poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1848 15053631 : - wi::to_poly_offset (vro->op1))
1849 10035754 : * wi::to_offset (vro->op2)
1850 5017877 : * vn_ref_op_align_unit (vro));
1851 5017877 : off.to_shwi (&vro->off);
1852 : }
1853 : }
1854 225774628 : }
1855 :
1856 : static void
1857 13042751 : valueize_refs (vec<vn_reference_op_s> *orig)
1858 : {
1859 13042751 : bool tem;
1860 0 : valueize_refs_1 (orig, &tem);
1861 0 : }
1862 :
1863 : static vec<vn_reference_op_s> shared_lookup_references;
1864 :
1865 : /* Create a vector of vn_reference_op_s structures from REF, a
1866 : REFERENCE_CLASS_P tree. The vector is shared among all callers of
1867 : this function. *VALUEIZED_ANYTHING will specify whether any
1868 : operands were valueized. */
1869 :
1870 : static vec<vn_reference_op_s>
1871 186574739 : valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1872 : {
1873 186574739 : if (!ref)
1874 0 : return vNULL;
1875 186574739 : shared_lookup_references.truncate (0);
1876 186574739 : copy_reference_ops_from_ref (ref, &shared_lookup_references);
1877 186574739 : valueize_refs_1 (&shared_lookup_references, valueized_anything);
1878 186574739 : return shared_lookup_references;
1879 : }
1880 :
1881 : /* Create a vector of vn_reference_op_s structures from CALL, a
1882 : call statement. The vector is shared among all callers of
1883 : this function. */
1884 :
1885 : static vec<vn_reference_op_s>
1886 9397296 : valueize_shared_reference_ops_from_call (gcall *call)
1887 : {
1888 9397296 : if (!call)
1889 0 : return vNULL;
1890 9397296 : shared_lookup_references.truncate (0);
1891 9397296 : copy_reference_ops_from_call (call, &shared_lookup_references);
1892 9397296 : valueize_refs (&shared_lookup_references);
1893 9397296 : return shared_lookup_references;
1894 : }
1895 :
1896 : /* Lookup a SCCVN reference operation VR in the current hash table.
1897 : Returns the resulting value number if it exists in the hash table,
1898 : NULL_TREE otherwise. VNRESULT will be filled in with the actual
1899 : vn_reference_t stored in the hashtable if something is found. */
1900 :
1901 : static tree
1902 65532680 : vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1903 : {
1904 65532680 : vn_reference_s **slot;
1905 65532680 : hashval_t hash;
1906 :
1907 65532680 : hash = vr->hashcode;
1908 65532680 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1909 65532680 : if (slot)
1910 : {
1911 8268182 : if (vnresult)
1912 8268182 : *vnresult = (vn_reference_t)*slot;
1913 8268182 : return ((vn_reference_t)*slot)->result;
1914 : }
1915 :
1916 : return NULL_TREE;
1917 : }
1918 :
1919 :
1920 : /* Partial definition tracking support. */
1921 :
1922 : struct pd_range
1923 : {
1924 : HOST_WIDE_INT offset;
1925 : HOST_WIDE_INT size;
1926 : pd_range *m_children[2];
1927 : };
1928 :
1929 : struct pd_data
1930 : {
1931 : tree rhs;
1932 : HOST_WIDE_INT rhs_off;
1933 : HOST_WIDE_INT offset;
1934 : HOST_WIDE_INT size;
1935 : };
1936 :
1937 : /* Context for alias walking. */
1938 :
1939 : struct vn_walk_cb_data
1940 : {
1941 63235271 : vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1942 : vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1943 : gimple *redundant_store_)
1944 63235271 : : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1945 63235271 : mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1946 63235271 : vn_walk_kind (vn_walk_kind_),
1947 63235271 : tbaa_p (tbaa_p_), redundant_store (redundant_store_),
1948 126470542 : saved_operands (vNULL), first_range (), first_set (-2),
1949 126470542 : first_base_set (-2), using_dummy_vr (false)
1950 : {
1951 63235271 : if (!last_vuse_ptr)
1952 29324656 : last_vuse_ptr = &last_vuse;
1953 63235271 : ao_ref_init (&orig_ref, orig_ref_);
1954 63235271 : if (mask)
1955 : {
1956 320210 : wide_int w = wi::to_wide (mask);
1957 320210 : unsigned int pos = 0, prec = w.get_precision ();
1958 320210 : pd_data pd;
1959 320210 : pd.rhs = build_constructor (NULL_TREE, NULL);
1960 320210 : pd.rhs_off = 0;
1961 : /* When bitwise and with a constant is done on a memory load,
1962 : we don't really need all the bits to be defined or defined
1963 : to constants, we don't really care what is in the position
1964 : corresponding to 0 bits in the mask.
1965 : So, push the ranges of those 0 bits in the mask as artificial
1966 : zero stores and let the partial def handling code do the
1967 : rest. */
1968 685733 : while (pos < prec)
1969 : {
1970 663960 : int tz = wi::ctz (w);
1971 663960 : if (pos + tz > prec)
1972 298437 : tz = prec - pos;
1973 663960 : if (tz)
1974 : {
1975 504410 : if (BYTES_BIG_ENDIAN)
1976 : pd.offset = prec - pos - tz;
1977 : else
1978 504410 : pd.offset = pos;
1979 504410 : pd.size = tz;
1980 504410 : void *r = push_partial_def (pd, 0, 0, 0, prec);
1981 504410 : gcc_assert (r == NULL_TREE);
1982 : }
1983 663960 : pos += tz;
1984 663960 : if (pos == prec)
1985 : break;
1986 365523 : w = wi::lrshift (w, tz);
1987 365523 : tz = wi::ctz (wi::bit_not (w));
1988 365523 : if (pos + tz > prec)
1989 0 : tz = prec - pos;
1990 365523 : pos += tz;
1991 365523 : w = wi::lrshift (w, tz);
1992 : }
1993 320210 : }
1994 63235271 : }
1995 : ~vn_walk_cb_data ();
1996 : void *finish (alias_set_type, alias_set_type, tree, bool insert = true);
1997 : void *push_partial_def (pd_data pd,
1998 : alias_set_type, alias_set_type, HOST_WIDE_INT,
1999 : HOST_WIDE_INT);
2000 :
2001 : vn_reference_t vr;
2002 : ao_ref orig_ref;
2003 : tree *last_vuse_ptr;
2004 : tree last_vuse;
2005 : tree mask;
2006 : tree masked_result;
2007 : tree same_val;
2008 : vn_lookup_kind vn_walk_kind;
2009 : bool tbaa_p;
2010 : gimple *redundant_store;
2011 : vec<vn_reference_op_s> saved_operands;
2012 :
2013 : /* The VDEFs of partial defs we come along. */
2014 : auto_vec<pd_data, 2> partial_defs;
2015 : /* The first defs range to avoid splay tree setup in most cases. */
2016 : pd_range first_range;
2017 : alias_set_type first_set;
2018 : alias_set_type first_base_set;
2019 : default_splay_tree<pd_range *> known_ranges;
2020 : obstack ranges_obstack;
2021 : static constexpr HOST_WIDE_INT bufsize = 64;
2022 :
2023 : bool using_dummy_vr;
2024 : vn_reference_s dummy_vr;
2025 : };
2026 :
2027 63235271 : vn_walk_cb_data::~vn_walk_cb_data ()
2028 : {
2029 63235271 : if (known_ranges)
2030 182124 : obstack_free (&ranges_obstack, NULL);
2031 63235271 : saved_operands.release ();
2032 63235271 : }
2033 :
2034 : void *
2035 1722620 : vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val,
2036 : bool insert)
2037 : {
2038 1722620 : if (first_set != -2)
2039 : {
2040 492388 : set = first_set;
2041 492388 : base_set = first_base_set;
2042 : }
2043 1722620 : if (mask)
2044 : {
2045 447 : masked_result = val;
2046 447 : return (void *) -1;
2047 : }
2048 1722173 : if (same_val && !operand_equal_p (val, same_val))
2049 : return (void *) -1;
2050 1718990 : if (!insert || same_val)
2051 : {
2052 12581 : memset (&dummy_vr, 0, sizeof (vn_reference_s));
2053 12581 : dummy_vr.result = val;
2054 12581 : using_dummy_vr = true;
2055 12581 : return &dummy_vr;
2056 : }
2057 1706409 : vec<vn_reference_op_s> &operands
2058 1706409 : = saved_operands.exists () ? saved_operands : vr->operands;
2059 1706409 : return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
2060 : vr->offset, vr->max_size,
2061 1706409 : vr->type, operands, val);
2062 : }
2063 :
2064 : /* Push PD to the vector of partial definitions returning a
2065 : value when we are ready to combine things with VUSE, SET and MAXSIZEI,
2066 : NULL when we want to continue looking for partial defs or -1
2067 : on failure. */
2068 :
2069 : void *
2070 603762 : vn_walk_cb_data::push_partial_def (pd_data pd,
2071 : alias_set_type set, alias_set_type base_set,
2072 : HOST_WIDE_INT offseti,
2073 : HOST_WIDE_INT maxsizei)
2074 : {
2075 : /* We're using a fixed buffer for encoding so fail early if the object
2076 : we want to interpret is bigger. */
2077 603762 : if (maxsizei > bufsize * BITS_PER_UNIT
2078 : || CHAR_BIT != 8
2079 : || BITS_PER_UNIT != 8
2080 : /* Not prepared to handle PDP endian. */
2081 : || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2082 : return (void *)-1;
2083 :
2084 : /* Turn too large constant stores into non-constant stores. */
2085 603687 : if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
2086 0 : pd.rhs = error_mark_node;
2087 :
2088 : /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
2089 : most a partial byte before and/or after the region. */
2090 603687 : if (!CONSTANT_CLASS_P (pd.rhs))
2091 : {
2092 548649 : if (pd.offset < offseti)
2093 : {
2094 8497 : HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2095 8497 : gcc_assert (pd.size > o);
2096 8497 : pd.size -= o;
2097 8497 : pd.offset += o;
2098 : }
2099 548649 : if (pd.size + pd.offset > offseti + maxsizei)
2100 7750 : pd.size = ((offseti - pd.offset + maxsizei)
2101 7750 : + ((pd.size + pd.offset - (offseti + maxsizei))
2102 7750 : % BITS_PER_UNIT));
2103 : }
2104 :
2105 603687 : pd.offset -= offseti;
2106 :
2107 1207374 : bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2108 603687 : || CONSTANT_CLASS_P (pd.rhs));
2109 603687 : pd_range *r;
2110 603687 : if (partial_defs.is_empty ())
2111 : {
2112 : /* If we get a clobber upfront, fail. */
2113 381004 : if (TREE_CLOBBER_P (pd.rhs))
2114 : return (void *)-1;
2115 380644 : if (!pd_constant_p)
2116 : return (void *)-1;
2117 347484 : partial_defs.safe_push (pd);
2118 347484 : first_range.offset = pd.offset;
2119 347484 : first_range.size = pd.size;
2120 347484 : first_set = set;
2121 347484 : first_base_set = base_set;
2122 347484 : last_vuse_ptr = NULL;
2123 347484 : r = &first_range;
2124 : /* Go check if the first partial definition was a full one in case
2125 : the caller didn't optimize for this. */
2126 : }
2127 : else
2128 : {
2129 222683 : if (!known_ranges)
2130 : {
2131 : /* ??? Optimize the case where the 2nd partial def completes
2132 : things. */
2133 182124 : gcc_obstack_init (&ranges_obstack);
2134 182124 : known_ranges.insert_max_node (&first_range);
2135 : }
2136 : /* Lookup the offset and see if we need to merge. */
2137 222683 : int comparison = known_ranges.lookup_le
2138 449592 : ([&] (pd_range *r) { return pd.offset < r->offset; },
2139 190450 : [&] (pd_range *r) { return pd.offset > r->offset; });
2140 222683 : r = known_ranges.root ();
2141 222683 : if (comparison >= 0
2142 222683 : && ranges_known_overlap_p (r->offset, r->size + 1,
2143 : pd.offset, pd.size))
2144 : {
2145 : /* Ignore partial defs already covered. Here we also drop shadowed
2146 : clobbers arriving here at the floor. */
2147 5878 : if (known_subrange_p (pd.offset, pd.size, r->offset, r->size))
2148 : return NULL;
2149 5049 : r->size = MAX (r->offset + r->size, pd.offset + pd.size) - r->offset;
2150 : }
2151 : else
2152 : {
2153 : /* pd.offset wasn't covered yet, insert the range. */
2154 216805 : void *addr = XOBNEW (&ranges_obstack, pd_range);
2155 216805 : r = new (addr) pd_range { pd.offset, pd.size, {} };
2156 216805 : known_ranges.insert_relative (comparison, r);
2157 : }
2158 : /* Merge r which now contains pd's range and is a member of the splay
2159 : tree with adjacent overlapping ranges. */
2160 221854 : if (known_ranges.splay_next_node ())
2161 34295 : do
2162 : {
2163 34295 : pd_range *rafter = known_ranges.root ();
2164 34295 : if (!ranges_known_overlap_p (r->offset, r->size + 1,
2165 34295 : rafter->offset, rafter->size))
2166 : break;
2167 34034 : r->size = MAX (r->offset + r->size,
2168 34034 : rafter->offset + rafter->size) - r->offset;
2169 : }
2170 34034 : while (known_ranges.remove_root_and_splay_next ());
2171 : /* If we get a clobber, fail. */
2172 221854 : if (TREE_CLOBBER_P (pd.rhs))
2173 : return (void *)-1;
2174 : /* Non-constants are OK as long as they are shadowed by a constant. */
2175 219793 : if (!pd_constant_p)
2176 : return (void *)-1;
2177 213335 : partial_defs.safe_push (pd);
2178 : }
2179 :
2180 : /* Now we have merged pd's range into the range tree. When we have covered
2181 : [offseti, sizei] then the tree will contain exactly one node which has
2182 : the desired properties and it will be 'r'. */
2183 560819 : if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2184 : /* Continue looking for partial defs. */
2185 : return NULL;
2186 :
2187 : /* Now simply native encode all partial defs in reverse order. */
2188 12317 : unsigned ndefs = partial_defs.length ();
2189 : /* We support up to 512-bit values (for V8DFmode). */
2190 12317 : unsigned char buffer[bufsize + 1];
2191 12317 : unsigned char this_buffer[bufsize + 1];
2192 12317 : int len;
2193 :
2194 12317 : memset (buffer, 0, bufsize + 1);
2195 12317 : unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2196 53275 : while (!partial_defs.is_empty ())
2197 : {
2198 40958 : pd_data pd = partial_defs.pop ();
2199 40958 : unsigned int amnt;
2200 40958 : if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2201 : {
2202 : /* Empty CONSTRUCTOR. */
2203 2327 : if (pd.size >= needed_len * BITS_PER_UNIT)
2204 2327 : len = needed_len;
2205 : else
2206 2017 : len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2207 2327 : memset (this_buffer, 0, len);
2208 : }
2209 38631 : else if (pd.rhs_off >= 0)
2210 : {
2211 77262 : len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2212 38631 : (MAX (0, -pd.offset)
2213 38631 : + pd.rhs_off) / BITS_PER_UNIT);
2214 38631 : if (len <= 0
2215 38631 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2216 38631 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2217 : {
2218 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2219 0 : fprintf (dump_file, "Failed to encode %u "
2220 : "partial definitions\n", ndefs);
2221 603762 : return (void *)-1;
2222 : }
2223 : }
2224 : else /* negative pd.rhs_off indicates we want to chop off first bits */
2225 : {
2226 0 : if (-pd.rhs_off >= bufsize)
2227 : return (void *)-1;
2228 0 : len = native_encode_expr (pd.rhs,
2229 0 : this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2230 0 : bufsize - -pd.rhs_off / BITS_PER_UNIT,
2231 0 : MAX (0, -pd.offset) / BITS_PER_UNIT);
2232 0 : if (len <= 0
2233 0 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2234 0 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2235 : {
2236 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2237 0 : fprintf (dump_file, "Failed to encode %u "
2238 : "partial definitions\n", ndefs);
2239 : return (void *)-1;
2240 : }
2241 : }
2242 :
2243 40958 : unsigned char *p = buffer;
2244 40958 : HOST_WIDE_INT size = pd.size;
2245 40958 : if (pd.offset < 0)
2246 347 : size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2247 40958 : this_buffer[len] = 0;
2248 40958 : if (BYTES_BIG_ENDIAN)
2249 : {
2250 : /* LSB of this_buffer[len - 1] byte should be at
2251 : pd.offset + pd.size - 1 bits in buffer. */
2252 : amnt = ((unsigned HOST_WIDE_INT) pd.offset
2253 : + pd.size) % BITS_PER_UNIT;
2254 : if (amnt)
2255 : shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2256 : unsigned char *q = this_buffer;
2257 : unsigned int off = 0;
2258 : if (pd.offset >= 0)
2259 : {
2260 : unsigned int msk;
2261 : off = pd.offset / BITS_PER_UNIT;
2262 : gcc_assert (off < needed_len);
2263 : p = buffer + off;
2264 : if (size <= amnt)
2265 : {
2266 : msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2267 : *p = (*p & ~msk) | (this_buffer[len] & msk);
2268 : size = 0;
2269 : }
2270 : else
2271 : {
2272 : if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2273 : q = (this_buffer + len
2274 : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2275 : / BITS_PER_UNIT));
2276 : if (pd.offset % BITS_PER_UNIT)
2277 : {
2278 : msk = -1U << (BITS_PER_UNIT
2279 : - (pd.offset % BITS_PER_UNIT));
2280 : *p = (*p & msk) | (*q & ~msk);
2281 : p++;
2282 : q++;
2283 : off++;
2284 : size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2285 : gcc_assert (size >= 0);
2286 : }
2287 : }
2288 : }
2289 : else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2290 : {
2291 : q = (this_buffer + len
2292 : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2293 : / BITS_PER_UNIT));
2294 : if (pd.offset % BITS_PER_UNIT)
2295 : {
2296 : q++;
2297 : size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2298 : % BITS_PER_UNIT);
2299 : gcc_assert (size >= 0);
2300 : }
2301 : }
2302 : if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2303 : > needed_len)
2304 : size = (needed_len - off) * BITS_PER_UNIT;
2305 : memcpy (p, q, size / BITS_PER_UNIT);
2306 : if (size % BITS_PER_UNIT)
2307 : {
2308 : unsigned int msk
2309 : = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2310 : p += size / BITS_PER_UNIT;
2311 : q += size / BITS_PER_UNIT;
2312 : *p = (*q & msk) | (*p & ~msk);
2313 : }
2314 : }
2315 : else
2316 : {
2317 40958 : if (pd.offset >= 0)
2318 : {
2319 : /* LSB of this_buffer[0] byte should be at pd.offset bits
2320 : in buffer. */
2321 40611 : unsigned int msk;
2322 40611 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2323 40611 : amnt = pd.offset % BITS_PER_UNIT;
2324 40611 : if (amnt)
2325 1532 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2326 40611 : unsigned int off = pd.offset / BITS_PER_UNIT;
2327 40611 : gcc_assert (off < needed_len);
2328 40611 : size = MIN (size,
2329 : (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2330 40611 : p = buffer + off;
2331 40611 : if (amnt + size < BITS_PER_UNIT)
2332 : {
2333 : /* Low amnt bits come from *p, then size bits
2334 : from this_buffer[0] and the remaining again from
2335 : *p. */
2336 1181 : msk = ((1 << size) - 1) << amnt;
2337 1181 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2338 1181 : size = 0;
2339 : }
2340 39430 : else if (amnt)
2341 : {
2342 1158 : msk = -1U << amnt;
2343 1158 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2344 1158 : p++;
2345 1158 : size -= (BITS_PER_UNIT - amnt);
2346 : }
2347 : }
2348 : else
2349 : {
2350 347 : amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2351 347 : if (amnt)
2352 45 : size -= BITS_PER_UNIT - amnt;
2353 347 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2354 347 : if (amnt)
2355 45 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2356 : }
2357 40958 : memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2358 40958 : p += size / BITS_PER_UNIT;
2359 40958 : if (size % BITS_PER_UNIT)
2360 : {
2361 667 : unsigned int msk = -1U << (size % BITS_PER_UNIT);
2362 667 : *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2363 667 : & ~msk) | (*p & msk);
2364 : }
2365 : }
2366 : }
2367 :
2368 12317 : tree type = vr->type;
2369 : /* Make sure to interpret in a type that has a range covering the whole
2370 : access size. */
2371 12317 : if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2372 : {
2373 0 : if (BITINT_TYPE_P (vr->type)
2374 26 : && maxsizei > MAX_FIXED_MODE_SIZE)
2375 13 : type = build_bitint_type (maxsizei, TYPE_UNSIGNED (type));
2376 : else
2377 0 : type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2378 : }
2379 12317 : tree val;
2380 12317 : if (BYTES_BIG_ENDIAN)
2381 : {
2382 : unsigned sz = needed_len;
2383 : if (maxsizei % BITS_PER_UNIT)
2384 : shift_bytes_in_array_right (buffer, needed_len,
2385 : BITS_PER_UNIT
2386 : - (maxsizei % BITS_PER_UNIT));
2387 : if (INTEGRAL_TYPE_P (type))
2388 : {
2389 : if (TYPE_MODE (type) != BLKmode)
2390 : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2391 : else
2392 : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2393 : }
2394 : if (sz > needed_len)
2395 : {
2396 : memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2397 : val = native_interpret_expr (type, this_buffer, sz);
2398 : }
2399 : else
2400 : val = native_interpret_expr (type, buffer, needed_len);
2401 : }
2402 : else
2403 12317 : val = native_interpret_expr (type, buffer, bufsize);
2404 : /* If we chop off bits because the types precision doesn't match the memory
2405 : access size this is ok when optimizing reads but not when called from
2406 : the DSE code during elimination. */
2407 12317 : if (val && type != vr->type)
2408 : {
2409 13 : if (! int_fits_type_p (val, vr->type))
2410 : val = NULL_TREE;
2411 : else
2412 13 : val = fold_convert (vr->type, val);
2413 : }
2414 :
2415 12313 : if (val)
2416 : {
2417 12313 : if (dump_file && (dump_flags & TDF_DETAILS))
2418 0 : fprintf (dump_file,
2419 : "Successfully combined %u partial definitions\n", ndefs);
2420 12313 : return finish (-2, -2, val, false);
2421 : }
2422 : else
2423 : {
2424 4 : if (dump_file && (dump_flags & TDF_DETAILS))
2425 0 : fprintf (dump_file,
2426 : "Failed to interpret %u encoded partial definitions\n", ndefs);
2427 : return (void *)-1;
2428 : }
2429 : }
2430 :
2431 : /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2432 : with the current VUSE and performs the expression lookup. */
2433 :
2434 : static void *
2435 1154100127 : vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2436 : {
2437 1154100127 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2438 1154100127 : vn_reference_t vr = data->vr;
2439 1154100127 : vn_reference_s **slot;
2440 1154100127 : hashval_t hash;
2441 :
2442 : /* If we have partial definitions recorded we have to go through
2443 : vn_reference_lookup_3. */
2444 1154100127 : if (!data->partial_defs.is_empty ())
2445 : return NULL;
2446 :
2447 1153250012 : if (data->last_vuse_ptr)
2448 : {
2449 1138994436 : *data->last_vuse_ptr = vuse;
2450 1138994436 : data->last_vuse = vuse;
2451 : }
2452 :
2453 : /* Fixup vuse and hash. */
2454 1153250012 : if (vr->vuse)
2455 1153250012 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2456 1153250012 : vr->vuse = vuse_ssa_val (vuse);
2457 1153250012 : if (vr->vuse)
2458 1153250012 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2459 :
2460 1153250012 : hash = vr->hashcode;
2461 1153250012 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2462 1153250012 : if (slot)
2463 : {
2464 8203905 : if ((*slot)->result && data->saved_operands.exists ())
2465 473617 : return data->finish (vr->set, vr->base_set, (*slot)->result);
2466 : return *slot;
2467 : }
2468 :
2469 1145046107 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2470 : {
2471 19486235 : HOST_WIDE_INT op_offset, op_size;
2472 19486235 : tree v = NULL_TREE;
2473 19486235 : tree base = ao_ref_base (op);
2474 :
2475 19486235 : if (base
2476 19486235 : && op->offset.is_constant (&op_offset)
2477 19486235 : && op->size.is_constant (&op_size)
2478 19486235 : && op->max_size_known_p ()
2479 38436380 : && known_eq (op->size, op->max_size))
2480 : {
2481 18495509 : if (TREE_CODE (base) == PARM_DECL)
2482 679276 : v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2483 : op_size);
2484 17816233 : else if (TREE_CODE (base) == MEM_REF
2485 7271177 : && integer_zerop (TREE_OPERAND (base, 1))
2486 5852335 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2487 5847122 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2488 21642372 : && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2489 : == PARM_DECL))
2490 3774567 : v = ipcp_get_aggregate_const (cfun,
2491 3774567 : SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2492 : true, op_offset, op_size);
2493 : }
2494 4453843 : if (v)
2495 1233 : return data->finish (vr->set, vr->base_set, v);
2496 : }
2497 :
2498 : return NULL;
2499 : }
2500 :
2501 : /* Lookup an existing or insert a new vn_reference entry into the
2502 : value table for the VUSE, SET, TYPE, OPERANDS reference which
2503 : has the value VALUE which is either a constant or an SSA name. */
2504 :
2505 : static vn_reference_t
2506 1706409 : vn_reference_lookup_or_insert_for_pieces (tree vuse,
2507 : alias_set_type set,
2508 : alias_set_type base_set,
2509 : poly_int64 offset,
2510 : poly_int64 max_size,
2511 : tree type,
2512 : vec<vn_reference_op_s,
2513 : va_heap> operands,
2514 : tree value)
2515 : {
2516 1706409 : vn_reference_s vr1;
2517 1706409 : vn_reference_t result;
2518 1706409 : unsigned value_id;
2519 1706409 : vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2520 1706409 : vr1.operands = operands;
2521 1706409 : vr1.type = type;
2522 1706409 : vr1.set = set;
2523 1706409 : vr1.base_set = base_set;
2524 1706409 : vr1.offset = offset;
2525 1706409 : vr1.max_size = max_size;
2526 1706409 : vr1.hashcode = vn_reference_compute_hash (&vr1);
2527 1706409 : if (vn_reference_lookup_1 (&vr1, &result))
2528 14904 : return result;
2529 :
2530 1691505 : if (TREE_CODE (value) == SSA_NAME)
2531 385305 : value_id = VN_INFO (value)->value_id;
2532 : else
2533 1306200 : value_id = get_or_alloc_constant_value_id (value);
2534 1691505 : return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
2535 1691505 : type, operands.copy (), value, value_id);
2536 : }
2537 :
2538 : /* Return true if RES_OP is a CONSTRUCTOR in GENERIC form. ??? We're
2539 : arriving here with SCCVNs view, decomposed CONSTRUCTOR and GIMPLEs /
2540 : match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
2541 :
2542 : static bool
2543 65153970 : match_op_generic_constructor_p (gimple_match_op *res_op)
2544 : {
2545 0 : return (res_op->code == CONSTRUCTOR
2546 65153970 : && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR);
2547 : }
2548 :
2549 : /* Return the number of operands for a vn_nary ops structure from RES_OP. */
2550 :
2551 : static unsigned int
2552 43435980 : vn_nary_length_from_match_op (gimple_match_op *res_op)
2553 : {
2554 43435980 : if (match_op_generic_constructor_p (res_op))
2555 2274 : return CONSTRUCTOR_NELTS (res_op->ops[0]);
2556 43433706 : return res_op->num_ops;
2557 : }
2558 :
2559 : /* Initialize VNO from RES_OP. The operands are taken as-is, they are
2560 : not valueized. */
2561 :
2562 : static void
2563 21717990 : init_vn_nary_op_from_match_op (vn_nary_op_t vno, gimple_match_op *res_op)
2564 : {
2565 21717990 : vno->opcode = (tree_code) res_op->code;
2566 21717990 : vno->length = vn_nary_length_from_match_op (res_op);
2567 21717990 : vno->type = res_op->type;
2568 21717990 : if (match_op_generic_constructor_p (res_op))
2569 5199 : for (unsigned i = 0; i < vno->length; ++i)
2570 4062 : vno->op[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
2571 : else
2572 21716853 : memcpy (&vno->op[0], res_op->ops, sizeof (tree) * vno->length);
2573 21717990 : vno->predicated_values = 0;
2574 21717990 : }
2575 :
2576 : /* Return a value-number for RCODE OPS... either by looking up an existing
2577 : value-number for the possibly simplified result or by inserting the
2578 : operation if INSERT is true. If SIMPLIFY is false, return a value
2579 : number for the unsimplified expression. */
2580 :
2581 : static tree
2582 16061023 : vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2583 : bool simplify)
2584 : {
2585 16061023 : tree result = NULL_TREE;
2586 : /* We will be creating a value number for
2587 : RCODE (OPS...).
2588 : So first simplify and lookup this expression to see if it
2589 : is already available. */
2590 : /* For simplification valueize. */
2591 16061023 : unsigned i = 0;
2592 16061023 : if (simplify)
2593 38342277 : for (i = 0; i < res_op->num_ops; ++i)
2594 22287101 : if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2595 : {
2596 13838954 : tree tem = vn_valueize (res_op->ops[i]);
2597 13838954 : if (!tem)
2598 : break;
2599 13838954 : res_op->ops[i] = tem;
2600 : }
2601 : /* If valueization of an operand fails (it is not available), skip
2602 : simplification. */
2603 16061023 : bool res = false;
2604 16061023 : if (i == res_op->num_ops)
2605 : {
2606 : /* Do not leak not available operands into the simplified expression
2607 : when called from PRE context. */
2608 16055176 : if (rpo_avail)
2609 8269911 : mprts_hook = vn_lookup_simplify_result;
2610 16055176 : res = res_op->resimplify (NULL, vn_valueize);
2611 16055176 : mprts_hook = NULL;
2612 : }
2613 27556301 : gimple *new_stmt = NULL;
2614 27556301 : bool reused = false;
2615 16055176 : if (res
2616 16055176 : && gimple_simplified_result_is_gimple_val (res_op))
2617 : {
2618 : /* The expression is already available. */
2619 4559898 : result = res_op->ops[0];
2620 : /* Valueize it, simplification returns sth in AVAIL only. */
2621 4559898 : if (TREE_CODE (result) == SSA_NAME)
2622 281762 : result = SSA_VAL (result);
2623 : }
2624 : else
2625 : {
2626 11501125 : tree val = vn_lookup_simplify_result (res_op);
2627 : /* ??? In weird cases we can end up with internal-fn calls,
2628 : but this isn't expected so throw the result away. See
2629 : PR123040 for an example. Likewise we can end up with
2630 : &MEM[ptr_1 + CST] which would be a vn_reference (PR127000). */
2631 11501125 : if (!val
2632 11501125 : && insert
2633 178664 : && res_op->code.is_tree_code ()
2634 11679789 : && (tree_code) res_op->code != ADDR_EXPR)
2635 : {
2636 : /* When iterating, re-use the SSA name we created for the same
2637 : expression in a previous iteration instead of creating a new
2638 : one each time. */
2639 178664 : vn_nary_op_t vno1 = NULL;
2640 178664 : vn_nary_op_s **slot = NULL;
2641 178664 : if (valid_info->inserted_exprs)
2642 : {
2643 127738 : vno1 = XALLOCAVAR (struct vn_nary_op_s,
2644 : sizeof_vn_nary_op
2645 : (vn_nary_length_from_match_op (res_op)));
2646 127738 : init_vn_nary_op_from_match_op (vno1, res_op);
2647 127738 : vno1->hashcode = vn_nary_op_compute_hash (vno1);
2648 127738 : slot = valid_info->inserted_exprs->find_slot_with_hash
2649 127738 : (vno1, vno1->hashcode, NO_INSERT);
2650 : }
2651 127738 : if (slot)
2652 : {
2653 27460 : result = (*slot)->u.result;
2654 27460 : new_stmt = gimple_seq_first_stmt (VN_INFO (result)->expr);
2655 27460 : reused = true;
2656 : }
2657 : else
2658 : {
2659 151204 : gimple_seq stmts = NULL;
2660 151204 : result = maybe_push_res_to_seq (res_op, &stmts);
2661 151204 : if (result)
2662 : {
2663 151198 : gcc_assert (gimple_seq_singleton_p (stmts));
2664 151198 : new_stmt = gimple_seq_first_stmt (stmts);
2665 151198 : if (vno1)
2666 : {
2667 100273 : slot = valid_info->inserted_exprs->find_slot_with_hash
2668 100273 : (vno1, vno1->hashcode, INSERT);
2669 100273 : vn_nary_op_t entry
2670 100273 : = alloc_vn_nary_op_noinit (vno1->length,
2671 : &vn_tables_insert_obstack);
2672 100273 : memcpy (entry, vno1, sizeof_vn_nary_op (vno1->length));
2673 100273 : entry->u.result = result;
2674 100273 : *slot = entry;
2675 : }
2676 : }
2677 : }
2678 : }
2679 : else
2680 : /* The expression is already available. */
2681 : result = val;
2682 : }
2683 460426 : if (new_stmt)
2684 : {
2685 : /* The expression is not yet available, value-number lhs to
2686 : the new SSA_NAME we created. */
2687 178658 : if (!reused)
2688 : {
2689 : /* Initialize value-number information properly. */
2690 151198 : vn_ssa_aux_t result_info = VN_INFO (result);
2691 151198 : result_info->valnum = result;
2692 151198 : result_info->value_id = get_next_value_id ();
2693 151198 : result_info->visited = 1;
2694 151198 : gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2695 : new_stmt);
2696 151198 : result_info->needs_insertion = true;
2697 : }
2698 : /* ??? PRE phi-translation inserts NARYs without corresponding
2699 : SSA name result. Re-use those but set their result according
2700 : to the stmt we just built. */
2701 178658 : vn_nary_op_t nary = NULL;
2702 178658 : vn_nary_op_lookup_stmt (new_stmt, &nary);
2703 178658 : if (nary)
2704 : {
2705 0 : gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2706 0 : nary->u.result = gimple_assign_lhs (new_stmt);
2707 : }
2708 : else
2709 : {
2710 : /* Record the expression in the hashtable. The entry is keyed
2711 : on the current, possibly optimistic, value numbers of the
2712 : operands, so unlike the inserted_exprs entry it has to be
2713 : unwound on iteration like any other entry. */
2714 178658 : vn_nary_op_insert_stmt (as_a<gassign *> (new_stmt), result);
2715 : }
2716 178658 : if (dump_file && (dump_flags & TDF_DETAILS))
2717 : {
2718 1190 : fprintf (dump_file, reused ? "Re-using name " : "Inserting name ");
2719 595 : print_generic_expr (dump_file, result);
2720 595 : fprintf (dump_file, " for expression ");
2721 595 : print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2722 595 : fprintf (dump_file, "\n");
2723 : }
2724 : }
2725 16061023 : return result;
2726 : }
2727 :
2728 : /* Return a value-number for RCODE OPS... either by looking up an existing
2729 : value-number for the simplified result or by inserting the operation. */
2730 :
2731 : static tree
2732 198436 : vn_nary_build_or_lookup (gimple_match_op *res_op)
2733 : {
2734 0 : return vn_nary_build_or_lookup_1 (res_op, true, true);
2735 : }
2736 :
2737 : /* Try to simplify the expression RCODE OPS... of type TYPE and return
2738 : its value if present. Update NARY with a simplified expression if
2739 : it fits. */
2740 :
2741 : tree
2742 7781949 : vn_nary_simplify (vn_nary_op_t nary)
2743 : {
2744 7781949 : if (nary->length > gimple_match_op::MAX_NUM_OPS
2745 : /* For CONSTRUCTOR the vn_nary_op_t and gimple_match_op representation
2746 : does not match. */
2747 7781401 : || nary->opcode == CONSTRUCTOR)
2748 : return NULL_TREE;
2749 7778858 : gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2750 7778858 : nary->type, nary->length);
2751 7778858 : memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2752 7778858 : tree res = vn_nary_build_or_lookup_1 (&op, false, true);
2753 : /* Do not update *NARY with a simplified result that contains abnormals.
2754 : This matches what maybe_push_res_to_seq does when requesting insertion. */
2755 28173245 : for (unsigned i = 0; i < op.num_ops; ++i)
2756 12615615 : if (TREE_CODE (op.ops[i]) == SSA_NAME
2757 12615615 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op.ops[i]))
2758 : return res;
2759 7778772 : if (op.code.is_tree_code ()
2760 7778772 : && op.num_ops <= nary->length
2761 15556692 : && (tree_code) op.code != CONSTRUCTOR)
2762 : {
2763 7777919 : nary->opcode = (tree_code) op.code;
2764 7777919 : nary->length = op.num_ops;
2765 20391675 : for (unsigned i = 0; i < op.num_ops; ++i)
2766 12613756 : nary->op[i] = op.ops[i];
2767 : }
2768 : return res;
2769 : }
2770 :
2771 : /* Elimination engine. */
2772 :
2773 : class eliminate_dom_walker : public dom_walker
2774 : {
2775 : public:
2776 : eliminate_dom_walker (cdi_direction, bitmap);
2777 : virtual ~eliminate_dom_walker ();
2778 :
2779 : edge before_dom_children (basic_block) final override;
2780 : void after_dom_children (basic_block) final override;
2781 :
2782 : virtual tree eliminate_avail (basic_block, tree op);
2783 : virtual void eliminate_push_avail (basic_block, tree op);
2784 : tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2785 :
2786 : void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2787 :
2788 : unsigned eliminate_cleanup (bool region_p = false);
2789 :
2790 : bool do_pre;
2791 : unsigned int el_todo;
2792 : unsigned int eliminations;
2793 : unsigned int insertions;
2794 :
2795 : /* SSA names that had their defs inserted by PRE if do_pre. */
2796 : bitmap inserted_exprs;
2797 :
2798 : /* Blocks with statements that have had their EH properties changed. */
2799 : bitmap need_eh_cleanup;
2800 :
2801 : /* Blocks with statements that have had their AB properties changed. */
2802 : bitmap need_ab_cleanup;
2803 :
2804 : /* Local state for the eliminate domwalk. */
2805 : auto_vec<gimple *> to_remove;
2806 : auto_vec<gimple *> to_fixup;
2807 : auto_vec<tree> avail;
2808 : auto_vec<tree> avail_stack;
2809 : };
2810 :
2811 : /* Adaptor to the elimination engine using RPO availability. */
2812 :
2813 : class rpo_elim : public eliminate_dom_walker
2814 : {
2815 : public:
2816 6335585 : rpo_elim(basic_block entry_)
2817 6335585 : : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2818 12671170 : m_avail_freelist (NULL) {}
2819 :
2820 : tree eliminate_avail (basic_block, tree op) final override;
2821 :
2822 : void eliminate_push_avail (basic_block, tree) final override;
2823 :
2824 : basic_block entry;
2825 : /* Freelist of avail entries which are allocated from the vn_ssa_aux
2826 : obstack. */
2827 : vn_avail *m_avail_freelist;
2828 : };
2829 :
2830 : /* Return true if BASE1 and BASE2 can be adjusted so they have the
2831 : same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2832 : Otherwise return false. */
2833 :
2834 : static bool
2835 5698325 : adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2836 : tree base2, poly_int64 *offset2)
2837 : {
2838 5698325 : poly_int64 soff;
2839 5698325 : if (TREE_CODE (base1) == MEM_REF
2840 2717254 : && TREE_CODE (base2) == MEM_REF)
2841 : {
2842 2177317 : if (mem_ref_offset (base1).to_shwi (&soff))
2843 : {
2844 2177317 : base1 = TREE_OPERAND (base1, 0);
2845 2177317 : *offset1 += soff * BITS_PER_UNIT;
2846 : }
2847 2177317 : if (mem_ref_offset (base2).to_shwi (&soff))
2848 : {
2849 2177317 : base2 = TREE_OPERAND (base2, 0);
2850 2177317 : *offset2 += soff * BITS_PER_UNIT;
2851 : }
2852 2177317 : return operand_equal_p (base1, base2, 0);
2853 : }
2854 3521008 : return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2855 : }
2856 :
2857 : /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2858 : from the statement defining VUSE and if not successful tries to
2859 : translate *REFP and VR_ through an aggregate copy at the definition
2860 : of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2861 : of *REF and *VR. If only disambiguation was performed then
2862 : *DISAMBIGUATE_ONLY is set to true. */
2863 :
2864 : static void *
2865 42467919 : vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2866 : translate_flags *disambiguate_only)
2867 : {
2868 42467919 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2869 42467919 : vn_reference_t vr = data->vr;
2870 42467919 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2871 42467919 : tree base = ao_ref_base (ref);
2872 42467919 : HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2873 42467919 : static vec<vn_reference_op_s> lhs_ops;
2874 42467919 : ao_ref lhs_ref;
2875 42467919 : bool lhs_ref_ok = false;
2876 42467919 : poly_int64 copy_size;
2877 :
2878 : /* If def_stmt is the redundant store itself, we can ignore it:
2879 : we can only reach it via a backedge, and if we eliminate it, it no longer
2880 : executes on any iteration. */
2881 42467919 : if (def_stmt == data->redundant_store)
2882 : {
2883 1663263 : *disambiguate_only = TR_DISAMBIGUATE;
2884 1663263 : return NULL;
2885 : }
2886 :
2887 : /* First try to disambiguate after value-replacing in the definitions LHS. */
2888 40804656 : if (auto a_stmt = dyn_cast<gassign *> (def_stmt))
2889 : {
2890 18008660 : tree lhs = gimple_assign_lhs (a_stmt);
2891 18008660 : bool valueized_anything = false;
2892 : /* Avoid re-allocation overhead. */
2893 18008660 : lhs_ops.truncate (0);
2894 18008660 : basic_block saved_rpo_bb = vn_context_bb;
2895 18008660 : vn_context_bb = gimple_bb (def_stmt);
2896 18008660 : if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2897 : {
2898 13250479 : copy_reference_ops_from_ref (lhs, &lhs_ops);
2899 13250479 : valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2900 : }
2901 18008660 : vn_context_bb = saved_rpo_bb;
2902 18008660 : ao_ref_init (&lhs_ref, lhs);
2903 18008660 : lhs_ref_ok = true;
2904 18008660 : if (valueized_anything
2905 2198255 : && ao_ref_init_from_vn_reference
2906 2198255 : (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2907 2198255 : ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2908 20206915 : && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2909 : {
2910 1916152 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2911 4547443 : return NULL;
2912 : }
2913 :
2914 : /* When the def is a CLOBBER we can optimistically disambiguate
2915 : against it since any overlap it would be undefined behavior.
2916 : Avoid this for obvious must aliases to save compile-time though.
2917 : We also may not do this when the query is used for redundant
2918 : store removal. */
2919 16092508 : if (!data->redundant_store
2920 10736092 : && gimple_clobber_p (def_stmt)
2921 16609230 : && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2922 : {
2923 489639 : *disambiguate_only = TR_DISAMBIGUATE;
2924 489639 : return NULL;
2925 : }
2926 :
2927 : /* Besides valueizing the LHS we can also use access-path based
2928 : disambiguation on the original non-valueized ref. */
2929 15602869 : if (!ref->ref
2930 : && lhs_ref_ok
2931 2515736 : && data->orig_ref.ref)
2932 : {
2933 : /* We want to use the non-valueized LHS for this, but avoid redundant
2934 : work. */
2935 1639311 : ao_ref *lref = &lhs_ref;
2936 1639311 : ao_ref lref_alt;
2937 1639311 : if (valueized_anything)
2938 : {
2939 110312 : ao_ref_init (&lref_alt, lhs);
2940 110312 : lref = &lref_alt;
2941 : }
2942 1639311 : if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2943 : {
2944 307066 : *disambiguate_only = (valueized_anything
2945 153533 : ? TR_VALUEIZE_AND_DISAMBIGUATE
2946 : : TR_DISAMBIGUATE);
2947 153533 : return NULL;
2948 : }
2949 : }
2950 :
2951 : /* If we reach a clobbering statement try to skip it and see if
2952 : we find a VN result with exactly the same value as the
2953 : possible clobber. In this case we can ignore the clobber
2954 : and return the found value. */
2955 15449336 : if (!gimple_has_volatile_ops (def_stmt)
2956 14022199 : && ((is_gimple_reg_type (TREE_TYPE (lhs))
2957 9775460 : && types_compatible_p (TREE_TYPE (lhs), vr->type)
2958 6948488 : && !storage_order_barrier_p (lhs)
2959 6948488 : && !reverse_storage_order_for_component_p (lhs))
2960 7073761 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == CONSTRUCTOR)
2961 8109347 : && (ref->ref || data->orig_ref.ref)
2962 7610992 : && !data->mask
2963 7589908 : && !data->redundant_store
2964 5143140 : && data->partial_defs.is_empty ()
2965 5142281 : && multiple_p (get_object_alignment
2966 : (ref->ref ? ref->ref : data->orig_ref.ref),
2967 : ref->size)
2968 32402835 : && multiple_p (get_object_alignment (lhs), ref->size))
2969 : {
2970 4919419 : HOST_WIDE_INT offset2i, size2i;
2971 4919419 : poly_int64 offset = ref->offset;
2972 4919419 : poly_int64 maxsize = ref->max_size;
2973 :
2974 4919419 : gcc_assert (lhs_ref_ok);
2975 4919419 : tree base2 = ao_ref_base (&lhs_ref);
2976 4919419 : poly_int64 offset2 = lhs_ref.offset;
2977 4919419 : poly_int64 size2 = lhs_ref.size;
2978 4919419 : poly_int64 maxsize2 = lhs_ref.max_size;
2979 :
2980 4919419 : tree rhs = gimple_assign_rhs1 (def_stmt);
2981 4919419 : if (TREE_CODE (rhs) == CONSTRUCTOR)
2982 107189 : rhs = integer_zero_node;
2983 : /* ??? We may not compare to ahead values which might be from
2984 : a different loop iteration but only to loop invariants. Use
2985 : CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2986 : The one-hop lookup below doesn't have this issue since there's
2987 : a virtual PHI before we ever reach a backedge to cross.
2988 : We can skip multiple defs as long as they are from the same
2989 : value though. */
2990 4919419 : if (data->same_val
2991 4919419 : && !operand_equal_p (data->same_val, rhs))
2992 : ;
2993 : /* When this is a (partial) must-def, leave it to handling
2994 : below in case we are interested in the value. */
2995 4735631 : else if (!(*disambiguate_only > TR_TRANSLATE)
2996 1382797 : && base2
2997 1382797 : && known_eq (maxsize2, size2)
2998 846860 : && adjust_offsets_for_equal_base_address (base, &offset,
2999 : base2, &offset2)
3000 241007 : && offset2.is_constant (&offset2i)
3001 241007 : && size2.is_constant (&size2i)
3002 241007 : && maxsize.is_constant (&maxsizei)
3003 241007 : && offset.is_constant (&offseti)
3004 4976638 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3005 : size2i))
3006 : ;
3007 4589136 : else if (CONSTANT_CLASS_P (rhs))
3008 : {
3009 1982149 : if (dump_file && (dump_flags & TDF_DETAILS))
3010 : {
3011 68 : fprintf (dump_file,
3012 : "Skipping possible redundant definition ");
3013 68 : print_gimple_stmt (dump_file, def_stmt, 0);
3014 : }
3015 : /* Delay the actual compare of the values to the end of the walk
3016 : but do not update last_vuse from here. */
3017 1982149 : data->last_vuse_ptr = NULL;
3018 1982149 : data->same_val = rhs;
3019 1988119 : return NULL;
3020 : }
3021 : else
3022 : {
3023 2606987 : tree saved_vuse = vr->vuse;
3024 2606987 : hashval_t saved_hashcode = vr->hashcode;
3025 2606987 : if (vr->vuse)
3026 2606987 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
3027 5213974 : vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
3028 2606987 : if (vr->vuse)
3029 2606987 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
3030 2606987 : vn_reference_t vnresult = NULL;
3031 : /* Do not use vn_reference_lookup_2 since that might perform
3032 : expression hashtable insertion but this lookup crosses
3033 : a possible may-alias making such insertion conditionally
3034 : invalid. */
3035 2606987 : vn_reference_lookup_1 (vr, &vnresult);
3036 : /* Need to restore vr->vuse and vr->hashcode. */
3037 2606987 : vr->vuse = saved_vuse;
3038 2606987 : vr->hashcode = saved_hashcode;
3039 2606987 : if (vnresult)
3040 : {
3041 126378 : if (TREE_CODE (rhs) == SSA_NAME)
3042 125226 : rhs = SSA_VAL (rhs);
3043 126378 : if (vnresult->result
3044 126378 : && operand_equal_p (vnresult->result, rhs, 0))
3045 5970 : return vnresult;
3046 : }
3047 : }
3048 : }
3049 : }
3050 22795996 : else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
3051 20438162 : && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
3052 25020847 : && gimple_call_num_args (def_stmt) <= 4)
3053 : {
3054 : /* For builtin calls valueize its arguments and call the
3055 : alias oracle again. Valueization may improve points-to
3056 : info of pointers and constify size and position arguments.
3057 : Originally this was motivated by PR61034 which has
3058 : conditional calls to free falsely clobbering ref because
3059 : of imprecise points-to info of the argument. */
3060 : tree oldargs[4];
3061 : bool valueized_anything = false;
3062 5244210 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
3063 : {
3064 3609545 : oldargs[i] = gimple_call_arg (def_stmt, i);
3065 3609545 : tree val = vn_valueize (oldargs[i]);
3066 3609545 : if (val != oldargs[i])
3067 : {
3068 142030 : gimple_call_set_arg (def_stmt, i, val);
3069 142030 : valueized_anything = true;
3070 : }
3071 : }
3072 1634665 : if (valueized_anything)
3073 : {
3074 110921 : bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
3075 : ref, data->tbaa_p);
3076 507354 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
3077 285512 : gimple_call_set_arg (def_stmt, i, oldargs[i]);
3078 110921 : if (!res)
3079 : {
3080 29973 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
3081 29973 : return NULL;
3082 : }
3083 : }
3084 : }
3085 :
3086 36227240 : if (*disambiguate_only > TR_TRANSLATE)
3087 : return (void *)-1;
3088 :
3089 : /* If we cannot constrain the size of the reference we cannot
3090 : test if anything kills it. */
3091 24869225 : if (!ref->max_size_known_p ())
3092 : return (void *)-1;
3093 :
3094 24400540 : poly_int64 offset = ref->offset;
3095 24400540 : poly_int64 maxsize = ref->max_size;
3096 :
3097 : /* def_stmt may-defs *ref. See if we can derive a value for *ref
3098 : from that definition.
3099 : 1) Memset. */
3100 24400540 : if (is_gimple_reg_type (vr->type)
3101 24391134 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
3102 24296498 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
3103 95170 : && (integer_zerop (gimple_call_arg (def_stmt, 1))
3104 33253 : || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
3105 8955 : || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
3106 : && CHAR_BIT == 8
3107 : && BITS_PER_UNIT == 8
3108 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3109 31916 : && offset.is_constant (&offseti)
3110 31916 : && ref->size.is_constant (&sizei)
3111 31916 : && (offseti % BITS_PER_UNIT == 0
3112 69 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
3113 93833 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
3114 37259 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3115 37259 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
3116 24457759 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3117 31055 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
3118 : {
3119 57178 : tree base2;
3120 57178 : poly_int64 offset2, size2, maxsize2;
3121 57178 : bool reverse;
3122 57178 : tree ref2 = gimple_call_arg (def_stmt, 0);
3123 57178 : if (TREE_CODE (ref2) == SSA_NAME)
3124 : {
3125 31014 : ref2 = SSA_VAL (ref2);
3126 31014 : if (TREE_CODE (ref2) == SSA_NAME
3127 31014 : && (TREE_CODE (base) != MEM_REF
3128 19464 : || TREE_OPERAND (base, 0) != ref2))
3129 : {
3130 24345 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
3131 24345 : if (gimple_assign_single_p (def_stmt)
3132 24345 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3133 869 : ref2 = gimple_assign_rhs1 (def_stmt);
3134 : }
3135 : }
3136 57178 : if (TREE_CODE (ref2) == ADDR_EXPR)
3137 : {
3138 30031 : ref2 = TREE_OPERAND (ref2, 0);
3139 30031 : base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
3140 : &reverse);
3141 30031 : if (!known_size_p (maxsize2)
3142 29987 : || !known_eq (maxsize2, size2)
3143 59938 : || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
3144 56752 : return (void *)-1;
3145 : }
3146 27147 : else if (TREE_CODE (ref2) == SSA_NAME)
3147 : {
3148 27147 : poly_int64 soff;
3149 27147 : if (TREE_CODE (base) != MEM_REF
3150 45968 : || !(mem_ref_offset (base)
3151 45968 : << LOG2_BITS_PER_UNIT).to_shwi (&soff))
3152 22851 : return (void *)-1;
3153 18821 : offset += soff;
3154 18821 : offset2 = 0;
3155 18821 : if (TREE_OPERAND (base, 0) != ref2)
3156 : {
3157 15150 : gimple *def = SSA_NAME_DEF_STMT (ref2);
3158 15150 : if (is_gimple_assign (def)
3159 13531 : && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
3160 11644 : && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
3161 15836 : && poly_int_tree_p (gimple_assign_rhs2 (def)))
3162 : {
3163 625 : tree rhs2 = gimple_assign_rhs2 (def);
3164 625 : if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
3165 : SIGNED)
3166 625 : << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
3167 : return (void *)-1;
3168 625 : ref2 = gimple_assign_rhs1 (def);
3169 625 : if (TREE_CODE (ref2) == SSA_NAME)
3170 625 : ref2 = SSA_VAL (ref2);
3171 : }
3172 : else
3173 : return (void *)-1;
3174 : }
3175 : }
3176 : else
3177 : return (void *)-1;
3178 29023 : tree len = gimple_call_arg (def_stmt, 2);
3179 29023 : HOST_WIDE_INT leni, offset2i;
3180 29023 : if (TREE_CODE (len) == SSA_NAME)
3181 367 : len = SSA_VAL (len);
3182 : /* Sometimes the above trickery is smarter than alias analysis. Take
3183 : advantage of that. */
3184 29023 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
3185 58046 : (wi::to_poly_offset (len)
3186 29023 : << LOG2_BITS_PER_UNIT)))
3187 : return NULL;
3188 57970 : if (data->partial_defs.is_empty ()
3189 28948 : && known_subrange_p (offset, maxsize, offset2,
3190 28948 : wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
3191 : {
3192 28344 : tree val;
3193 28344 : if (integer_zerop (gimple_call_arg (def_stmt, 1)))
3194 23553 : val = build_zero_cst (vr->type);
3195 4791 : else if (INTEGRAL_TYPE_P (vr->type)
3196 3747 : && known_eq (ref->size, 8)
3197 7798 : && offseti % BITS_PER_UNIT == 0)
3198 : {
3199 3007 : gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
3200 3007 : vr->type, gimple_call_arg (def_stmt, 1));
3201 3007 : val = vn_nary_build_or_lookup (&res_op);
3202 3007 : if (!val
3203 3007 : || (TREE_CODE (val) == SSA_NAME
3204 616 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3205 0 : return (void *)-1;
3206 : }
3207 : else
3208 : {
3209 1784 : unsigned buflen
3210 1784 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
3211 1784 : if (INTEGRAL_TYPE_P (vr->type)
3212 1784 : && TYPE_MODE (vr->type) != BLKmode)
3213 1478 : buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
3214 1784 : unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
3215 1784 : memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
3216 : buflen);
3217 1784 : if (BYTES_BIG_ENDIAN)
3218 : {
3219 : unsigned int amnt
3220 : = (((unsigned HOST_WIDE_INT) offseti + sizei)
3221 : % BITS_PER_UNIT);
3222 : if (amnt)
3223 : {
3224 : shift_bytes_in_array_right (buf, buflen,
3225 : BITS_PER_UNIT - amnt);
3226 : buf++;
3227 : buflen--;
3228 : }
3229 : }
3230 1784 : else if (offseti % BITS_PER_UNIT != 0)
3231 : {
3232 7 : unsigned int amnt
3233 : = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3234 7 : % BITS_PER_UNIT);
3235 7 : shift_bytes_in_array_left (buf, buflen, amnt);
3236 7 : buf++;
3237 7 : buflen--;
3238 : }
3239 1784 : val = native_interpret_expr (vr->type, buf, buflen);
3240 1784 : if (!val)
3241 : return (void *)-1;
3242 : }
3243 28344 : return data->finish (0, 0, val);
3244 : }
3245 : /* For now handle clearing memory with partial defs. */
3246 678 : else if (known_eq (ref->size, maxsize)
3247 548 : && integer_zerop (gimple_call_arg (def_stmt, 1))
3248 256 : && tree_fits_poly_int64_p (len)
3249 252 : && tree_to_poly_int64 (len).is_constant (&leni)
3250 252 : && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3251 252 : && offset.is_constant (&offseti)
3252 252 : && offset2.is_constant (&offset2i)
3253 252 : && maxsize.is_constant (&maxsizei)
3254 678 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3255 678 : leni << LOG2_BITS_PER_UNIT))
3256 : {
3257 252 : pd_data pd;
3258 252 : pd.rhs = build_constructor (NULL_TREE, NULL);
3259 252 : pd.rhs_off = 0;
3260 252 : pd.offset = offset2i;
3261 252 : pd.size = leni << LOG2_BITS_PER_UNIT;
3262 252 : return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3263 : }
3264 : }
3265 :
3266 : /* 2) Assignment from an empty CONSTRUCTOR. */
3267 24343362 : else if (is_gimple_reg_type (vr->type)
3268 24333956 : && gimple_assign_single_p (def_stmt)
3269 8220995 : && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3270 2082121 : && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0
3271 26425483 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt)))
3272 : {
3273 2082089 : tree base2;
3274 2082089 : poly_int64 offset2, size2, maxsize2;
3275 2082089 : HOST_WIDE_INT offset2i, size2i;
3276 2082089 : gcc_assert (lhs_ref_ok);
3277 2082089 : base2 = ao_ref_base (&lhs_ref);
3278 2082089 : offset2 = lhs_ref.offset;
3279 2082089 : size2 = lhs_ref.size;
3280 2082089 : maxsize2 = lhs_ref.max_size;
3281 2082089 : if (known_size_p (maxsize2)
3282 2081918 : && known_eq (maxsize2, size2)
3283 4162355 : && adjust_offsets_for_equal_base_address (base, &offset,
3284 : base2, &offset2))
3285 : {
3286 2046997 : if (data->partial_defs.is_empty ()
3287 2043470 : && known_subrange_p (offset, maxsize, offset2, size2))
3288 : {
3289 : /* While technically undefined behavior do not optimize
3290 : a full read from a clobber. */
3291 2042519 : if (gimple_clobber_p (def_stmt))
3292 2046793 : return (void *)-1;
3293 1089651 : tree val = build_zero_cst (vr->type);
3294 1089651 : return data->finish (ao_ref_alias_set (&lhs_ref),
3295 1089651 : ao_ref_base_alias_set (&lhs_ref), val);
3296 : }
3297 4478 : else if (known_eq (ref->size, maxsize)
3298 4274 : && maxsize.is_constant (&maxsizei)
3299 4274 : && offset.is_constant (&offseti)
3300 4274 : && offset2.is_constant (&offset2i)
3301 4274 : && size2.is_constant (&size2i)
3302 4478 : && ranges_known_overlap_p (offseti, maxsizei,
3303 : offset2i, size2i))
3304 : {
3305 : /* Let clobbers be consumed by the partial-def tracker
3306 : which can choose to ignore them if they are shadowed
3307 : by a later def. */
3308 4274 : pd_data pd;
3309 4274 : pd.rhs = gimple_assign_rhs1 (def_stmt);
3310 4274 : pd.rhs_off = 0;
3311 4274 : pd.offset = offset2i;
3312 4274 : pd.size = size2i;
3313 4274 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3314 : ao_ref_base_alias_set (&lhs_ref),
3315 : offseti, maxsizei);
3316 : }
3317 : }
3318 : }
3319 :
3320 : /* 3) Assignment from a constant. We can use folds native encode/interpret
3321 : routines to extract the assigned bits. */
3322 22261273 : else if (known_eq (ref->size, maxsize)
3323 21668859 : && is_gimple_reg_type (vr->type)
3324 21659453 : && !reverse_storage_order_for_component_p (vr->operands)
3325 21656615 : && !contains_storage_order_barrier_p (vr->operands)
3326 21656615 : && gimple_assign_single_p (def_stmt)
3327 5771591 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3328 : && CHAR_BIT == 8
3329 : && BITS_PER_UNIT == 8
3330 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3331 : /* native_encode and native_decode operate on arrays of bytes
3332 : and so fundamentally need a compile-time size and offset. */
3333 5767587 : && maxsize.is_constant (&maxsizei)
3334 5767587 : && offset.is_constant (&offseti)
3335 28028860 : && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3336 4795475 : || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3337 1984067 : && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3338 : {
3339 989755 : tree lhs = gimple_assign_lhs (def_stmt);
3340 989755 : tree base2;
3341 989755 : poly_int64 offset2, size2, maxsize2;
3342 989755 : HOST_WIDE_INT offset2i, size2i;
3343 989755 : bool reverse;
3344 989755 : gcc_assert (lhs_ref_ok);
3345 989755 : base2 = ao_ref_base (&lhs_ref);
3346 989755 : offset2 = lhs_ref.offset;
3347 989755 : size2 = lhs_ref.size;
3348 989755 : maxsize2 = lhs_ref.max_size;
3349 989755 : reverse = reverse_storage_order_for_component_p (lhs);
3350 989755 : if (base2
3351 989755 : && !reverse
3352 988927 : && !storage_order_barrier_p (lhs)
3353 988927 : && known_eq (maxsize2, size2)
3354 952067 : && adjust_offsets_for_equal_base_address (base, &offset,
3355 : base2, &offset2)
3356 101956 : && offset.is_constant (&offseti)
3357 101956 : && offset2.is_constant (&offset2i)
3358 989755 : && size2.is_constant (&size2i))
3359 : {
3360 101956 : if (data->partial_defs.is_empty ()
3361 73543 : && (maxsizei
3362 73543 : <= (HOST_WIDE_INT) vn_walk_cb_data::bufsize * BITS_PER_UNIT)
3363 73524 : && known_subrange_p (offseti, maxsizei, offset2, size2))
3364 : {
3365 : /* We support up to 512-bit values (for V8DFmode). An
3366 : unaligned access can need one more byte from the
3367 : encoding, plus one for the shift carry-over. */
3368 46811 : unsigned char buffer[vn_walk_cb_data::bufsize + 2];
3369 46811 : int len;
3370 :
3371 46811 : tree rhs = gimple_assign_rhs1 (def_stmt);
3372 46811 : if (TREE_CODE (rhs) == SSA_NAME)
3373 2006 : rhs = SSA_VAL (rhs);
3374 93622 : len = native_encode_expr (rhs,
3375 : buffer, sizeof (buffer) - 1,
3376 46811 : (offseti - offset2i) / BITS_PER_UNIT);
3377 46811 : if (len > 0
3378 43604 : && (len * BITS_PER_UNIT
3379 43604 : >= ((offseti - offset2i) % BITS_PER_UNIT) + maxsizei))
3380 : {
3381 43604 : tree type = vr->type;
3382 43604 : unsigned char *buf = buffer;
3383 43604 : unsigned int amnt = 0;
3384 : /* Make sure to interpret in a type that has a range
3385 : covering the whole access size. */
3386 43604 : if (INTEGRAL_TYPE_P (vr->type)
3387 43604 : && maxsizei != TYPE_PRECISION (vr->type))
3388 : {
3389 982 : bool uns = TYPE_UNSIGNED (type);
3390 981 : if (BITINT_TYPE_P (vr->type)
3391 983 : && maxsizei > MAX_FIXED_MODE_SIZE)
3392 1 : type = build_bitint_type (maxsizei, uns);
3393 : else
3394 981 : type = build_nonstandard_integer_type (maxsizei, uns);
3395 : }
3396 43604 : if (BYTES_BIG_ENDIAN)
3397 : {
3398 : /* For big-endian native_encode_expr stored the rhs
3399 : such that the LSB of it is the LSB of buffer[len - 1].
3400 : That bit is stored into memory at position
3401 : offset2 + size2 - 1, i.e. in byte
3402 : base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3403 : E.g. for offset2 1 and size2 14, rhs -1 and memory
3404 : previously cleared that is:
3405 : 0 1
3406 : 01111111|11111110
3407 : Now, if we want to extract offset 2 and size 12 from
3408 : it using native_interpret_expr (which actually works
3409 : for integral bitfield types in terms of byte size of
3410 : the mode), the native_encode_expr stored the value
3411 : into buffer as
3412 : XX111111|11111111
3413 : and returned len 2 (the X bits are outside of
3414 : precision).
3415 : Let sz be maxsize / BITS_PER_UNIT if not extracting
3416 : a bitfield, and GET_MODE_SIZE otherwise.
3417 : We need to align the LSB of the value we want to
3418 : extract as the LSB of buf[sz - 1].
3419 : The LSB from memory we need to read is at position
3420 : offset + maxsize - 1. */
3421 : HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3422 : if (INTEGRAL_TYPE_P (type))
3423 : {
3424 : if (TYPE_MODE (type) != BLKmode)
3425 : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3426 : else
3427 : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3428 : }
3429 : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3430 : - offseti - maxsizei) % BITS_PER_UNIT;
3431 : if (amnt)
3432 : shift_bytes_in_array_right (buffer, len, amnt);
3433 : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3434 : - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3435 : if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3436 : len = 0;
3437 : else
3438 : {
3439 : buf = buffer + len - sz - amnt;
3440 : len -= (buf - buffer);
3441 : }
3442 : }
3443 : else
3444 : {
3445 43604 : amnt = ((unsigned HOST_WIDE_INT) offset2i
3446 43604 : - offseti) % BITS_PER_UNIT;
3447 43604 : if (amnt)
3448 : {
3449 392 : buffer[len] = 0;
3450 392 : shift_bytes_in_array_left (buffer, len + 1, amnt);
3451 392 : buf = buffer + 1;
3452 : }
3453 : }
3454 43604 : tree val = native_interpret_expr (type, buf, len);
3455 : /* If we chop off bits because the types precision doesn't
3456 : match the memory access size this is ok when optimizing
3457 : reads but not when called from the DSE code during
3458 : elimination. */
3459 43604 : if (val
3460 43602 : && type != vr->type)
3461 : {
3462 982 : if (! int_fits_type_p (val, vr->type))
3463 : val = NULL_TREE;
3464 : else
3465 982 : val = fold_convert (vr->type, val);
3466 : }
3467 :
3468 43602 : if (val)
3469 43602 : return data->finish (ao_ref_alias_set (&lhs_ref),
3470 43602 : ao_ref_base_alias_set (&lhs_ref), val);
3471 : }
3472 : }
3473 55145 : else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3474 : size2i))
3475 : {
3476 55145 : pd_data pd;
3477 55145 : tree rhs = gimple_assign_rhs1 (def_stmt);
3478 55145 : if (TREE_CODE (rhs) == SSA_NAME)
3479 2612 : rhs = SSA_VAL (rhs);
3480 55145 : pd.rhs = rhs;
3481 55145 : pd.rhs_off = 0;
3482 55145 : pd.offset = offset2i;
3483 55145 : pd.size = size2i;
3484 55145 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3485 : ao_ref_base_alias_set (&lhs_ref),
3486 : offseti, maxsizei);
3487 : }
3488 : }
3489 : }
3490 :
3491 : /* 4) Assignment from an SSA name which definition we may be able
3492 : to access pieces from or we can combine to a larger entity. */
3493 21271518 : else if (known_eq (ref->size, maxsize)
3494 20679104 : && is_gimple_reg_type (vr->type)
3495 20669698 : && !reverse_storage_order_for_component_p (vr->operands)
3496 20666860 : && !contains_storage_order_barrier_p (vr->operands)
3497 20666860 : && gimple_assign_single_p (def_stmt)
3498 4781836 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3499 26049350 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3500 : {
3501 1966424 : tree lhs = gimple_assign_lhs (def_stmt);
3502 1966424 : tree base2;
3503 1966424 : poly_int64 offset2, size2, maxsize2;
3504 1966424 : HOST_WIDE_INT offset2i, size2i, offseti;
3505 1966424 : bool reverse;
3506 1966424 : gcc_assert (lhs_ref_ok);
3507 1966424 : base2 = ao_ref_base (&lhs_ref);
3508 1966424 : offset2 = lhs_ref.offset;
3509 1966424 : size2 = lhs_ref.size;
3510 1966424 : maxsize2 = lhs_ref.max_size;
3511 1966424 : reverse = reverse_storage_order_for_component_p (lhs);
3512 1966424 : tree def_rhs = gimple_assign_rhs1 (def_stmt);
3513 1966424 : if (!reverse
3514 1966212 : && !storage_order_barrier_p (lhs)
3515 1966212 : && known_size_p (maxsize2)
3516 1938967 : && known_eq (maxsize2, size2)
3517 3785542 : && adjust_offsets_for_equal_base_address (base, &offset,
3518 : base2, &offset2))
3519 : {
3520 87565 : if (data->partial_defs.is_empty ()
3521 81094 : && known_subrange_p (offset, maxsize, offset2, size2)
3522 : /* ??? We can't handle bitfield precision extracts without
3523 : either using an alternate type for the BIT_FIELD_REF and
3524 : then doing a conversion or possibly adjusting the offset
3525 : according to endianness. */
3526 56473 : && (! INTEGRAL_TYPE_P (vr->type)
3527 42244 : || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3528 99212 : && multiple_p (ref->size, BITS_PER_UNIT))
3529 : {
3530 47903 : tree val = NULL_TREE;
3531 95800 : if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3532 52975 : || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3533 : {
3534 93270 : gimple_match_op op (gimple_match_cond::UNCOND,
3535 46635 : BIT_FIELD_REF, vr->type,
3536 : SSA_VAL (def_rhs),
3537 : bitsize_int (ref->size),
3538 46635 : bitsize_int (offset - offset2));
3539 46635 : val = vn_nary_build_or_lookup (&op);
3540 : }
3541 1268 : else if (known_eq (ref->size, size2))
3542 : {
3543 1169 : gimple_match_op op (gimple_match_cond::UNCOND,
3544 1169 : VIEW_CONVERT_EXPR, vr->type,
3545 1169 : SSA_VAL (def_rhs));
3546 1169 : val = vn_nary_build_or_lookup (&op);
3547 : }
3548 47804 : if (val
3549 47804 : && (TREE_CODE (val) != SSA_NAME
3550 47101 : || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3551 47785 : return data->finish (ao_ref_alias_set (&lhs_ref),
3552 87447 : ao_ref_base_alias_set (&lhs_ref), val);
3553 : }
3554 39662 : else if (maxsize.is_constant (&maxsizei)
3555 39662 : && offset.is_constant (&offseti)
3556 39662 : && offset2.is_constant (&offset2i)
3557 39662 : && size2.is_constant (&size2i)
3558 39662 : && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3559 : {
3560 39662 : pd_data pd;
3561 39662 : pd.rhs = SSA_VAL (def_rhs);
3562 39662 : pd.rhs_off = 0;
3563 39662 : pd.offset = offset2i;
3564 39662 : pd.size = size2i;
3565 39662 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3566 : ao_ref_base_alias_set (&lhs_ref),
3567 : offseti, maxsizei);
3568 : }
3569 : }
3570 : }
3571 :
3572 : /* 4b) Assignment done via one of the vectorizer internal store
3573 : functions where we may be able to access pieces from or we can
3574 : combine to a larger entity. */
3575 19305094 : else if (known_eq (ref->size, maxsize)
3576 18712680 : && is_gimple_reg_type (vr->type)
3577 18703274 : && !reverse_storage_order_for_component_p (vr->operands)
3578 18700436 : && !contains_storage_order_barrier_p (vr->operands)
3579 18700436 : && is_gimple_call (def_stmt)
3580 15058559 : && gimple_call_internal_p (def_stmt)
3581 19620205 : && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3582 : {
3583 36 : gcall *call = as_a <gcall *> (def_stmt);
3584 36 : internal_fn fn = gimple_call_internal_fn (call);
3585 :
3586 36 : tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3587 36 : switch (fn)
3588 : {
3589 36 : case IFN_MASK_STORE:
3590 36 : mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3591 36 : mask = vn_valueize (mask);
3592 36 : if (TREE_CODE (mask) != VECTOR_CST)
3593 28 : return (void *)-1;
3594 : break;
3595 0 : case IFN_LEN_STORE:
3596 0 : {
3597 0 : int len_index = internal_fn_len_index (fn);
3598 0 : len = gimple_call_arg (call, len_index);
3599 0 : bias = gimple_call_arg (call, len_index + 1);
3600 0 : if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3601 : return (void *) -1;
3602 : break;
3603 : }
3604 : default:
3605 : return (void *)-1;
3606 : }
3607 14 : tree def_rhs = gimple_call_arg (call,
3608 14 : internal_fn_stored_value_index (fn));
3609 14 : def_rhs = vn_valueize (def_rhs);
3610 14 : if (TREE_CODE (def_rhs) != VECTOR_CST)
3611 : return (void *)-1;
3612 :
3613 14 : ao_ref_init_from_ptr_and_size (&lhs_ref,
3614 : vn_valueize (gimple_call_arg (call, 0)),
3615 14 : TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3616 14 : tree base2;
3617 14 : poly_int64 offset2, size2, maxsize2;
3618 14 : HOST_WIDE_INT offset2i, size2i, offseti;
3619 14 : base2 = ao_ref_base (&lhs_ref);
3620 14 : offset2 = lhs_ref.offset;
3621 14 : size2 = lhs_ref.size;
3622 14 : maxsize2 = lhs_ref.max_size;
3623 14 : if (known_size_p (maxsize2)
3624 14 : && known_eq (maxsize2, size2)
3625 14 : && adjust_offsets_for_equal_base_address (base, &offset,
3626 : base2, &offset2)
3627 6 : && maxsize.is_constant (&maxsizei)
3628 6 : && offset.is_constant (&offseti)
3629 6 : && offset2.is_constant (&offset2i)
3630 14 : && size2.is_constant (&size2i))
3631 : {
3632 6 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3633 : /* Poor-mans disambiguation. */
3634 : return NULL;
3635 6 : else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3636 : {
3637 6 : pd_data pd;
3638 6 : pd.rhs = def_rhs;
3639 6 : tree aa = gimple_call_arg (call, 1);
3640 6 : alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3641 6 : tree vectype = TREE_TYPE (def_rhs);
3642 6 : unsigned HOST_WIDE_INT elsz
3643 6 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3644 6 : if (mask)
3645 : {
3646 : HOST_WIDE_INT start = 0, length = 0;
3647 : unsigned mask_idx = 0;
3648 48 : do
3649 : {
3650 48 : if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3651 : {
3652 24 : if (length != 0)
3653 : {
3654 18 : pd.rhs_off = start;
3655 18 : pd.offset = offset2i + start;
3656 18 : pd.size = length;
3657 18 : if (ranges_known_overlap_p
3658 18 : (offset, maxsize, pd.offset, pd.size))
3659 : {
3660 0 : void *res = data->push_partial_def
3661 0 : (pd, set, set, offseti, maxsizei);
3662 0 : if (res != NULL)
3663 6 : return res;
3664 : }
3665 : }
3666 24 : start = (mask_idx + 1) * elsz;
3667 24 : length = 0;
3668 : }
3669 : else
3670 24 : length += elsz;
3671 48 : mask_idx++;
3672 : }
3673 48 : while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3674 6 : if (length != 0)
3675 : {
3676 6 : pd.rhs_off = start;
3677 6 : pd.offset = offset2i + start;
3678 6 : pd.size = length;
3679 6 : if (ranges_known_overlap_p (offset, maxsize,
3680 : pd.offset, pd.size))
3681 2 : return data->push_partial_def (pd, set, set,
3682 2 : offseti, maxsizei);
3683 : }
3684 : }
3685 0 : else if (fn == IFN_LEN_STORE)
3686 : {
3687 0 : pd.offset = offset2i;
3688 0 : pd.size = (tree_to_uhwi (len)
3689 0 : + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3690 0 : if (BYTES_BIG_ENDIAN)
3691 : pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3692 : else
3693 0 : pd.rhs_off = 0;
3694 0 : if (ranges_known_overlap_p (offset, maxsize,
3695 : pd.offset, pd.size))
3696 0 : return data->push_partial_def (pd, set, set,
3697 0 : offseti, maxsizei);
3698 : }
3699 : else
3700 0 : gcc_unreachable ();
3701 : return NULL;
3702 : }
3703 : }
3704 : }
3705 :
3706 : /* 5) For aggregate copies translate the reference through them if
3707 : the copy kills ref. */
3708 19305058 : else if (data->vn_walk_kind == VN_WALKREWRITE
3709 15600074 : && gimple_assign_single_p (def_stmt)
3710 2641325 : && !gimple_has_volatile_ops (def_stmt)
3711 21943761 : && (DECL_P (gimple_assign_rhs1 (def_stmt))
3712 2034867 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3713 1609077 : || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3714 : {
3715 2389757 : tree base2;
3716 2389757 : int i, j, k;
3717 2389757 : auto_vec<vn_reference_op_s> rhs;
3718 2389757 : vn_reference_op_t vro;
3719 2389757 : ao_ref r;
3720 :
3721 2389757 : gcc_assert (lhs_ref_ok);
3722 :
3723 : /* See if the assignment kills REF. */
3724 2389757 : base2 = ao_ref_base (&lhs_ref);
3725 2389757 : if (!lhs_ref.max_size_known_p ()
3726 2389159 : || (base != base2
3727 93363 : && (TREE_CODE (base) != MEM_REF
3728 77373 : || TREE_CODE (base2) != MEM_REF
3729 58169 : || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3730 20436 : || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3731 20436 : TREE_OPERAND (base2, 1))))
3732 4704044 : || !stmt_kills_ref_p (def_stmt, ref))
3733 : return (void *)-1;
3734 :
3735 : /* Find the common base of ref and the lhs. lhs_ops already
3736 : contains valueized operands for the lhs. */
3737 1987644 : poly_int64 extra_off = 0;
3738 1987644 : i = vr->operands.length () - 1;
3739 1987644 : j = lhs_ops.length () - 1;
3740 :
3741 : /* The base should be always equal due to the above check. */
3742 1987644 : if (! vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3743 : return (void *)-1;
3744 1987386 : i--, j--;
3745 :
3746 : /* The 2nd component should always exist and be a MEM_REF. */
3747 1987386 : if (!(i >= 0 && j >= 0))
3748 : ;
3749 1987386 : else if (vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3750 943779 : i--, j--;
3751 1043607 : else if (vr->operands[i].opcode == MEM_REF
3752 1042151 : && lhs_ops[j].opcode == MEM_REF
3753 1042151 : && known_ne (lhs_ops[j].off, -1)
3754 2085758 : && known_ne (vr->operands[i].off, -1))
3755 : {
3756 1042151 : bool found = false;
3757 : /* When we ge a mismatch at a MEM_REF that is not the sole component
3758 : try finding a match in one of the outer components and continue
3759 : stripping there. This happens when addresses of components get
3760 : forwarded into dereferences. */
3761 1042151 : if (i > 0)
3762 : {
3763 117651 : int temi = i - 1;
3764 117651 : poly_int64 tem_extra_off = extra_off + vr->operands[i].off;
3765 117651 : while (temi >= 0
3766 259980 : && known_ne (vr->operands[temi].off, -1))
3767 : {
3768 143973 : if (vr->operands[temi].type
3769 143973 : && lhs_ops[j].type
3770 287946 : && (TYPE_MAIN_VARIANT (vr->operands[temi].type)
3771 143973 : == TYPE_MAIN_VARIANT (lhs_ops[j].type)))
3772 : {
3773 1644 : i = temi;
3774 : /* Strip the component that was type matched to
3775 : the MEM_REF. */
3776 1644 : extra_off = (tem_extra_off
3777 1644 : + vr->operands[i].off - lhs_ops[j].off);
3778 1644 : i--, j--;
3779 : /* Strip further equal components. */
3780 1644 : found = true;
3781 1644 : break;
3782 : }
3783 142329 : tem_extra_off += vr->operands[temi].off;
3784 142329 : temi--;
3785 : }
3786 : }
3787 1042151 : if (!found && j > 0)
3788 : {
3789 34422 : int temj = j - 1;
3790 34422 : poly_int64 tem_extra_off = extra_off - lhs_ops[j].off;
3791 34422 : while (temj >= 0
3792 65665 : && known_ne (lhs_ops[temj].off, -1))
3793 : {
3794 36719 : if (vr->operands[i].type
3795 36719 : && lhs_ops[temj].type
3796 73438 : && (TYPE_MAIN_VARIANT (vr->operands[i].type)
3797 36719 : == TYPE_MAIN_VARIANT (lhs_ops[temj].type)))
3798 : {
3799 5476 : j = temj;
3800 : /* Strip the component that was type matched to
3801 : the MEM_REF. */
3802 5476 : extra_off = (tem_extra_off
3803 5476 : + vr->operands[i].off - lhs_ops[j].off);
3804 5476 : i--, j--;
3805 : /* Strip further equal components. */
3806 5476 : found = true;
3807 5476 : break;
3808 : }
3809 31243 : tem_extra_off += -lhs_ops[temj].off;
3810 31243 : temj--;
3811 : }
3812 : }
3813 : /* When we cannot find a common base to reconstruct the full
3814 : reference instead try to reduce the lookup to the new
3815 : base plus a constant offset. */
3816 1042151 : if (!found)
3817 : {
3818 : while (j >= 0
3819 2100833 : && known_ne (lhs_ops[j].off, -1))
3820 : {
3821 1065802 : extra_off += -lhs_ops[j].off;
3822 1065802 : j--;
3823 : }
3824 1035031 : if (j != -1)
3825 : return (void *)-1;
3826 : while (i >= 0
3827 2204591 : && known_ne (vr->operands[i].off, -1))
3828 : {
3829 : /* Punt if the additional ops contain a storage order
3830 : barrier. */
3831 1169560 : if (vr->operands[i].opcode == VIEW_CONVERT_EXPR
3832 1169560 : && vr->operands[i].reverse)
3833 : break;
3834 1169560 : extra_off += vr->operands[i].off;
3835 1169560 : i--;
3836 : }
3837 1035031 : if (i != -1)
3838 : return (void *)-1;
3839 : found = true;
3840 : }
3841 : /* If we did find a match we'd eventually append a MEM_REF
3842 : as component. Don't. */
3843 : if (!found)
3844 : return (void *)-1;
3845 : }
3846 : else
3847 : return (void *)-1;
3848 :
3849 : /* Strip further common components, attempting to consume lhs_ops
3850 : in full. */
3851 1983797 : while (j >= 0 && i >= 0
3852 1983797 : && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3853 : {
3854 26688 : i--;
3855 26688 : j--;
3856 : }
3857 :
3858 : /* i now points to the first additional op.
3859 : ??? LHS may not be completely contained in VR, one or more
3860 : VIEW_CONVERT_EXPRs could be in its way. We could at least
3861 : try handling outermost VIEW_CONVERT_EXPRs. */
3862 1957109 : if (j != -1)
3863 : return (void *)-1;
3864 :
3865 : /* Punt if the additional ops contain a storage order barrier. */
3866 3073040 : for (k = i; k >= 0; k--)
3867 : {
3868 1118980 : vro = &vr->operands[k];
3869 1118980 : if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3870 : return (void *)-1;
3871 : }
3872 :
3873 : /* Now re-write REF to be based on the rhs of the assignment. */
3874 1954060 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
3875 1954060 : copy_reference_ops_from_ref (rhs1, &rhs);
3876 :
3877 : /* When none of the original operands survives the storage order of
3878 : the translated reference is the one of the RHS of the copy. The
3879 : operands we folded into a constant offset above may well have
3880 : specified a reverse storage order, which is a property of the
3881 : component and not of its position, so it is not recoverable from
3882 : that offset. Punt unless both accesses are in natural order. */
3883 1954060 : if (i < 0
3884 1954060 : && (reverse_storage_order_for_component_p (vr->operands)
3885 1031424 : || reverse_storage_order_for_component_p (rhs)))
3886 : return (void *)-1;
3887 :
3888 : /* Apply an extra offset to the inner MEM_REF of the RHS. */
3889 1953969 : bool force_no_tbaa = false;
3890 1953969 : if (maybe_ne (extra_off, 0))
3891 : {
3892 746440 : if (rhs.length () < 2)
3893 : return (void *)-1;
3894 746440 : int ix = rhs.length () - 2;
3895 746440 : if (rhs[ix].opcode != MEM_REF
3896 746440 : || known_eq (rhs[ix].off, -1))
3897 : return (void *)-1;
3898 746421 : rhs[ix].off += extra_off;
3899 746421 : rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3900 746421 : build_int_cst (TREE_TYPE (rhs[ix].op0),
3901 : extra_off));
3902 : /* When we have offsetted the RHS, reading only parts of it,
3903 : we can no longer use the original TBAA type, force alias-set
3904 : zero. */
3905 746421 : force_no_tbaa = true;
3906 : }
3907 :
3908 : /* Save the operands since we need to use the original ones for
3909 : the hash entry we use. */
3910 1953950 : if (!data->saved_operands.exists ())
3911 1825828 : data->saved_operands = vr->operands.copy ();
3912 :
3913 : /* We need to pre-pend vr->operands[0..i] to rhs. */
3914 1953950 : vec<vn_reference_op_s> old = vr->operands;
3915 5861850 : if (i + 1 + rhs.length () > vr->operands.length ())
3916 1146074 : vr->operands.safe_grow (i + 1 + rhs.length (), true);
3917 : else
3918 807876 : vr->operands.truncate (i + 1 + rhs.length ());
3919 7053698 : FOR_EACH_VEC_ELT (rhs, j, vro)
3920 5099748 : vr->operands[i + 1 + j] = *vro;
3921 1953950 : valueize_refs (&vr->operands);
3922 3907900 : if (old == shared_lookup_references)
3923 1953950 : shared_lookup_references = vr->operands;
3924 1953950 : vr->hashcode = vn_reference_compute_hash (vr);
3925 :
3926 : /* Try folding the new reference to a constant. */
3927 1953950 : tree val = fully_constant_vn_reference_p (vr);
3928 1953950 : if (val)
3929 : {
3930 22939 : if (data->partial_defs.is_empty ())
3931 22922 : return data->finish (ao_ref_alias_set (&lhs_ref),
3932 22922 : ao_ref_base_alias_set (&lhs_ref), val);
3933 : /* This is the only interesting case for partial-def handling
3934 : coming from targets that like to gimplify init-ctors as
3935 : aggregate copies from constant data like aarch64 for
3936 : PR83518. */
3937 17 : if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3938 : {
3939 17 : pd_data pd;
3940 17 : pd.rhs = val;
3941 17 : pd.rhs_off = 0;
3942 17 : pd.offset = 0;
3943 17 : pd.size = maxsizei;
3944 17 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3945 : ao_ref_base_alias_set (&lhs_ref),
3946 : 0, maxsizei);
3947 : }
3948 : }
3949 :
3950 : /* Continuing with partial defs isn't easily possible here, we
3951 : have to find a full def from further lookups from here. Probably
3952 : not worth the special-casing everywhere. */
3953 4320768 : if (!data->partial_defs.is_empty ())
3954 : return (void *)-1;
3955 :
3956 : /* Adjust *ref from the new operands. */
3957 1924495 : ao_ref rhs1_ref;
3958 1924495 : ao_ref_init (&rhs1_ref, rhs1);
3959 3117730 : if (!ao_ref_init_from_vn_reference (&r,
3960 : force_no_tbaa ? 0
3961 1193235 : : ao_ref_alias_set (&rhs1_ref),
3962 : force_no_tbaa ? 0
3963 1193235 : : ao_ref_base_alias_set (&rhs1_ref),
3964 : vr->type, vr->operands))
3965 : return (void *)-1;
3966 : /* This can happen with bitfields. */
3967 1924495 : if (maybe_ne (ref->size, r.size))
3968 : {
3969 : /* If the access lacks some subsetting simply apply that by
3970 : shortening it. That in the end can only be successful
3971 : if we can pun the lookup result which in turn requires
3972 : exact offsets. */
3973 1515 : if (known_eq (r.size, r.max_size)
3974 1515 : && known_lt (ref->size, r.size))
3975 1515 : r.size = r.max_size = ref->size;
3976 : else
3977 : return (void *)-1;
3978 : }
3979 1924495 : *ref = r;
3980 1924495 : vr->offset = r.offset;
3981 1924495 : vr->max_size = r.max_size;
3982 :
3983 : /* Do not update last seen VUSE after translating. */
3984 1924495 : data->last_vuse_ptr = NULL;
3985 : /* Invalidate the original access path since it now contains
3986 : the wrong base. */
3987 1924495 : data->orig_ref.ref = NULL_TREE;
3988 : /* Use the alias-set of this LHS for recording an eventual result. */
3989 1924495 : if (data->first_set == -2)
3990 : {
3991 1797863 : data->first_set = ao_ref_alias_set (&lhs_ref);
3992 1797863 : data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3993 : }
3994 :
3995 : /* Keep looking for the adjusted *REF / VR pair. */
3996 : return NULL;
3997 2389757 : }
3998 :
3999 : /* 6) For memcpy copies translate the reference through them if the copy
4000 : kills ref. But we cannot (easily) do this translation if the memcpy is
4001 : a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
4002 : can modify the storage order of objects (see storage_order_barrier_p). */
4003 16915301 : else if (data->vn_walk_kind == VN_WALKREWRITE
4004 13210317 : && is_gimple_reg_type (vr->type)
4005 : /* ??? Handle BCOPY as well. */
4006 13200911 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
4007 13131456 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
4008 13131033 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
4009 13129857 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
4010 13129615 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
4011 13103372 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
4012 97867 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
4013 85788 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
4014 97827 : && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
4015 68723 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
4016 97812 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), ©_size)
4017 55904 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
4018 55904 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
4019 : ©_size)))
4020 : /* Handling this is more complicated, give up for now. */
4021 16959930 : && data->partial_defs.is_empty ())
4022 : {
4023 43938 : tree lhs, rhs;
4024 43938 : ao_ref r;
4025 43938 : poly_int64 rhs_offset, lhs_offset;
4026 43938 : vn_reference_op_s op;
4027 43938 : poly_uint64 mem_offset;
4028 43938 : poly_int64 at, byte_maxsize;
4029 :
4030 : /* Only handle non-variable, addressable refs. */
4031 43938 : if (maybe_ne (ref->size, maxsize)
4032 43310 : || !multiple_p (offset, BITS_PER_UNIT, &at)
4033 87312 : || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
4034 : return (void *)-1;
4035 :
4036 : /* Extract a pointer base and an offset for the destination. */
4037 43310 : lhs = gimple_call_arg (def_stmt, 0);
4038 43310 : lhs_offset = 0;
4039 43310 : if (TREE_CODE (lhs) == SSA_NAME)
4040 : {
4041 33073 : lhs = vn_valueize (lhs);
4042 33073 : if (TREE_CODE (lhs) == SSA_NAME)
4043 : {
4044 32721 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
4045 32721 : if (gimple_assign_single_p (def_stmt)
4046 32721 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
4047 2591 : lhs = gimple_assign_rhs1 (def_stmt);
4048 : }
4049 : }
4050 43310 : if (TREE_CODE (lhs) == ADDR_EXPR)
4051 : {
4052 18567 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
4053 18110 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
4054 : return (void *)-1;
4055 13040 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
4056 : &lhs_offset);
4057 13040 : if (!tem)
4058 : return (void *)-1;
4059 12287 : if (TREE_CODE (tem) == MEM_REF
4060 12287 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
4061 : {
4062 1838 : lhs = TREE_OPERAND (tem, 0);
4063 1838 : if (TREE_CODE (lhs) == SSA_NAME)
4064 1838 : lhs = vn_valueize (lhs);
4065 1838 : lhs_offset += mem_offset;
4066 : }
4067 10449 : else if (DECL_P (tem))
4068 10449 : lhs = build_fold_addr_expr (tem);
4069 : else
4070 : return (void *)-1;
4071 : }
4072 42417 : if (TREE_CODE (lhs) != SSA_NAME
4073 10451 : && TREE_CODE (lhs) != ADDR_EXPR)
4074 : return (void *)-1;
4075 :
4076 : /* Extract a pointer base and an offset for the source. */
4077 42417 : rhs = gimple_call_arg (def_stmt, 1);
4078 42417 : rhs_offset = 0;
4079 42417 : if (TREE_CODE (rhs) == SSA_NAME)
4080 19657 : rhs = vn_valueize (rhs);
4081 42417 : if (TREE_CODE (rhs) == ADDR_EXPR)
4082 : {
4083 35634 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
4084 25034 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
4085 : return (void *)-1;
4086 24426 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
4087 : &rhs_offset);
4088 24426 : if (!tem)
4089 : return (void *)-1;
4090 24426 : if (TREE_CODE (tem) == MEM_REF
4091 24426 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
4092 : {
4093 0 : rhs = TREE_OPERAND (tem, 0);
4094 0 : rhs_offset += mem_offset;
4095 : }
4096 24426 : else if (DECL_P (tem)
4097 17667 : || TREE_CODE (tem) == STRING_CST)
4098 24426 : rhs = build_fold_addr_expr (tem);
4099 : else
4100 : return (void *)-1;
4101 : }
4102 42417 : if (TREE_CODE (rhs) == SSA_NAME)
4103 17991 : rhs = SSA_VAL (rhs);
4104 24426 : else if (TREE_CODE (rhs) != ADDR_EXPR)
4105 : return (void *)-1;
4106 :
4107 : /* The bases of the destination and the references have to agree. */
4108 42417 : if (TREE_CODE (base) == MEM_REF)
4109 : {
4110 16401 : if (TREE_OPERAND (base, 0) != lhs
4111 16401 : || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
4112 : return (void *) -1;
4113 13304 : at += mem_offset;
4114 : }
4115 26016 : else if (!DECL_P (base)
4116 24742 : || TREE_CODE (lhs) != ADDR_EXPR
4117 35269 : || TREE_OPERAND (lhs, 0) != base)
4118 : return (void *)-1;
4119 :
4120 : /* If the access is completely outside of the memcpy destination
4121 : area there is no aliasing. */
4122 13304 : if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
4123 : return NULL;
4124 : /* And the access has to be contained within the memcpy destination. */
4125 13269 : if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
4126 : return (void *)-1;
4127 :
4128 : /* Save the operands since we need to use the original ones for
4129 : the hash entry we use. */
4130 12652 : if (!data->saved_operands.exists ())
4131 12186 : data->saved_operands = vr->operands.copy ();
4132 :
4133 : /* Make room for 2 operands in the new reference. */
4134 12652 : if (vr->operands.length () < 2)
4135 : {
4136 0 : vec<vn_reference_op_s> old = vr->operands;
4137 0 : vr->operands.safe_grow_cleared (2, true);
4138 0 : if (old == shared_lookup_references)
4139 0 : shared_lookup_references = vr->operands;
4140 : }
4141 : else
4142 12652 : vr->operands.truncate (2);
4143 :
4144 : /* The looked-through reference is a simple MEM_REF. */
4145 12652 : memset (&op, 0, sizeof (op));
4146 12652 : op.type = vr->type;
4147 12652 : op.opcode = MEM_REF;
4148 12652 : op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
4149 12652 : op.off = at - lhs_offset + rhs_offset;
4150 12652 : vr->operands[0] = op;
4151 12652 : op.type = TREE_TYPE (rhs);
4152 12652 : op.opcode = TREE_CODE (rhs);
4153 12652 : op.op0 = rhs;
4154 12652 : op.off = -1;
4155 12652 : vr->operands[1] = op;
4156 12652 : vr->hashcode = vn_reference_compute_hash (vr);
4157 :
4158 : /* Try folding the new reference to a constant. */
4159 12652 : tree val = fully_constant_vn_reference_p (vr);
4160 12652 : if (val)
4161 3153 : return data->finish (0, 0, val);
4162 :
4163 : /* Adjust *ref from the new operands. */
4164 9499 : if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
4165 : return (void *)-1;
4166 : /* This can happen with bitfields. */
4167 9499 : if (maybe_ne (ref->size, r.size))
4168 : return (void *)-1;
4169 9499 : *ref = r;
4170 9499 : vr->offset = r.offset;
4171 9499 : vr->max_size = r.max_size;
4172 :
4173 : /* Do not update last seen VUSE after translating. */
4174 9499 : data->last_vuse_ptr = NULL;
4175 : /* Invalidate the original access path since it now contains
4176 : the wrong base. */
4177 9499 : data->orig_ref.ref = NULL_TREE;
4178 : /* Use the alias-set of this stmt for recording an eventual result. */
4179 9499 : if (data->first_set == -2)
4180 : {
4181 9088 : data->first_set = 0;
4182 9088 : data->first_base_set = 0;
4183 : }
4184 :
4185 : /* Keep looking for the adjusted *REF / VR pair. */
4186 : return NULL;
4187 : }
4188 :
4189 : /* Bail out and stop walking. */
4190 : return (void *)-1;
4191 : }
4192 :
4193 : /* Return true if E is a backedge with respect to our CFG walk order. */
4194 :
4195 : static bool
4196 224828537 : vn_is_backedge (edge e, void *)
4197 : {
4198 : /* During PRE elimination and ANTIC compute we no longer have
4199 : vn_bb_to_rpo and the region is no longer marked. */
4200 224828537 : if (!vn_bb_to_rpo)
4201 : return true;
4202 :
4203 183772415 : gcc_assert (vn_bb_in_region_flag != 0);
4204 :
4205 : /* The entry edge is a forward edge. */
4206 183772415 : if (e == vn_entry_edge)
4207 : return false;
4208 : /* For out-of region edges conservatively return true. */
4209 183582590 : if (!(e->src->flags & vn_bb_in_region_flag))
4210 : return true;
4211 : /* We should never ask for region exit edges. */
4212 183582590 : gcc_checking_assert (e->dest->flags & vn_bb_in_region_flag);
4213 183582590 : return vn_bb_to_rpo[e->dest->index] <= vn_bb_to_rpo[e->src->index];
4214 : }
4215 :
4216 : /* Return a reference op vector from OP that can be used for
4217 : vn_reference_lookup_pieces. The caller is responsible for releasing
4218 : the vector. */
4219 :
4220 : vec<vn_reference_op_s>
4221 4859995 : vn_reference_operands_for_lookup (tree op)
4222 : {
4223 4859995 : bool valueized;
4224 4859995 : return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
4225 : }
4226 :
4227 : /* Lookup a reference operation by it's parts, in the current hash table.
4228 : Returns the resulting value number if it exists in the hash table,
4229 : NULL_TREE otherwise. VNRESULT will be filled in with the actual
4230 : vn_reference_t stored in the hashtable if something is found. */
4231 :
4232 : tree
4233 7999195 : vn_reference_lookup_pieces (tree vuse, alias_set_type set,
4234 : alias_set_type base_set, tree type,
4235 : vec<vn_reference_op_s> operands,
4236 : vn_reference_t *vnresult, vn_lookup_kind kind)
4237 : {
4238 7999195 : struct vn_reference_s vr1;
4239 7999195 : vn_reference_t tmp;
4240 7999195 : tree cst;
4241 :
4242 7999195 : if (!vnresult)
4243 0 : vnresult = &tmp;
4244 7999195 : *vnresult = NULL;
4245 :
4246 7999195 : vr1.vuse = vuse_ssa_val (vuse);
4247 7999195 : shared_lookup_references.truncate (0);
4248 15998390 : shared_lookup_references.safe_grow (operands.length (), true);
4249 7999195 : memcpy (shared_lookup_references.address (),
4250 7999195 : operands.address (),
4251 : sizeof (vn_reference_op_s)
4252 7999195 : * operands.length ());
4253 7999195 : bool valueized_p;
4254 7999195 : valueize_refs_1 (&shared_lookup_references, &valueized_p);
4255 7999195 : vr1.operands = shared_lookup_references;
4256 7999195 : vr1.type = type;
4257 7999195 : vr1.set = set;
4258 7999195 : vr1.base_set = base_set;
4259 : /* We can pretend there's no extra info fed in since the ao_refs offset
4260 : and max_size are computed only from the VN reference ops. */
4261 7999195 : vr1.offset = 0;
4262 7999195 : vr1.max_size = -1;
4263 7999195 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4264 7999195 : if ((cst = fully_constant_vn_reference_p (&vr1)))
4265 : return cst;
4266 :
4267 7978609 : vn_reference_lookup_1 (&vr1, vnresult);
4268 7978609 : if (!*vnresult
4269 3093121 : && kind != VN_NOWALK
4270 3093121 : && vr1.vuse)
4271 : {
4272 3061271 : ao_ref r;
4273 3061271 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4274 3061271 : vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
4275 3061271 : NULL);
4276 3061271 : vec<vn_reference_op_s> ops_for_ref;
4277 3061271 : if (!valueized_p)
4278 2967061 : ops_for_ref = vr1.operands;
4279 : else
4280 : {
4281 : /* For ao_ref_from_mem we have to ensure only available SSA names
4282 : end up in base and the only convenient way to make this work
4283 : for PRE is to re-valueize with that in mind. */
4284 188420 : ops_for_ref.create (operands.length ());
4285 188420 : ops_for_ref.quick_grow (operands.length ());
4286 94210 : memcpy (ops_for_ref.address (),
4287 94210 : operands.address (),
4288 : sizeof (vn_reference_op_s)
4289 94210 : * operands.length ());
4290 94210 : valueize_refs_1 (&ops_for_ref, &valueized_p, true);
4291 : }
4292 3061271 : if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
4293 : ops_for_ref))
4294 2988842 : *vnresult
4295 2988842 : = ((vn_reference_t)
4296 2988842 : walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
4297 : vn_reference_lookup_3, vn_is_backedge,
4298 : vuse_valueize, limit, &data));
4299 6122542 : if (ops_for_ref != shared_lookup_references)
4300 94210 : ops_for_ref.release ();
4301 6122542 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4302 3061271 : if (*vnresult
4303 433916 : && data.same_val
4304 3061271 : && (!(*vnresult)->result
4305 0 : || !operand_equal_p ((*vnresult)->result, data.same_val)))
4306 : {
4307 0 : *vnresult = NULL;
4308 0 : return NULL_TREE;
4309 : }
4310 3061271 : if (data.using_dummy_vr)
4311 : {
4312 378 : tree val = (*vnresult)->result;
4313 378 : *vnresult = NULL;
4314 378 : return val;
4315 : }
4316 3061271 : }
4317 :
4318 7978231 : if (*vnresult)
4319 5319026 : return (*vnresult)->result;
4320 :
4321 : return NULL_TREE;
4322 : }
4323 :
4324 : /* When OPERANDS is an ADDR_EXPR that can be possibly expressed as a
4325 : POINTER_PLUS_EXPR return true and fill in its operands in OPS. */
4326 :
4327 : bool
4328 2231548 : vn_pp_nary_for_addr (const vec<vn_reference_op_s>& operands, tree ops[2])
4329 : {
4330 4463096 : gcc_assert (operands[0].opcode == ADDR_EXPR
4331 : && operands.last ().opcode == SSA_NAME);
4332 : poly_int64 off = 0;
4333 : vn_reference_op_t vro;
4334 : unsigned i;
4335 7205124 : for (i = 1; operands.iterate (i, &vro); ++i)
4336 : {
4337 7205124 : if (vro->opcode == SSA_NAME)
4338 : break;
4339 5025863 : else if (known_eq (vro->off, -1))
4340 : break;
4341 4973576 : off += vro->off;
4342 : }
4343 2231548 : if (i == operands.length () - 1
4344 2179261 : && maybe_ne (off, 0)
4345 : /* Make sure we the offset we accumulated in a 64bit int
4346 : fits the address computation carried out in target
4347 : offset precision. */
4348 3663239 : && (off.coeffs[0]
4349 1431691 : == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4350 : {
4351 1431151 : gcc_assert (operands[i-1].opcode == MEM_REF);
4352 1431151 : ops[0] = operands[i].op0;
4353 1431151 : ops[1] = wide_int_to_tree (sizetype, off);
4354 1431151 : return true;
4355 : }
4356 : return false;
4357 : }
4358 :
4359 : /* Lookup OP in the current hash table, and return the resulting value
4360 : number if it exists in the hash table. Return NULL_TREE if it does
4361 : not exist in the hash table or if the result field of the structure
4362 : was NULL.. VNRESULT will be filled in with the vn_reference_t
4363 : stored in the hashtable if one exists. When TBAA_P is false assume
4364 : we are looking up a store and treat it as having alias-set zero.
4365 : *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
4366 : MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
4367 : load is bitwise anded with MASK and so we are only interested in a subset
4368 : of the bits and can ignore if the other bits are uninitialized or
4369 : not initialized with constants. When doing redundant store removal
4370 : the caller has to set REDUNDANT_STORE_REMOVAL_P. */
4371 :
4372 : tree
4373 104311156 : vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
4374 : vn_reference_t *vnresult, bool tbaa_p,
4375 : tree *last_vuse_ptr, tree mask,
4376 : gimple *redundant_store)
4377 : {
4378 104311156 : vec<vn_reference_op_s> operands;
4379 104311156 : struct vn_reference_s vr1;
4380 104311156 : bool valueized_anything;
4381 :
4382 104311156 : if (vnresult)
4383 103916051 : *vnresult = NULL;
4384 :
4385 104311156 : vr1.vuse = vuse_ssa_val (vuse);
4386 208622312 : vr1.operands = operands
4387 104311156 : = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
4388 :
4389 : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
4390 : this before the pass folding __builtin_object_size had a chance to run. */
4391 104311156 : if ((cfun->curr_properties & PROP_objsz)
4392 75525895 : && operands[0].opcode == ADDR_EXPR
4393 105433196 : && operands.last ().opcode == SSA_NAME)
4394 : {
4395 1086533 : tree ops[2];
4396 1086533 : if (vn_pp_nary_for_addr (operands, ops))
4397 : {
4398 698507 : tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4399 698507 : TREE_TYPE (op), ops, NULL);
4400 698507 : if (res)
4401 698507 : return res;
4402 698507 : return NULL_TREE;
4403 : }
4404 : }
4405 :
4406 103612649 : vr1.type = TREE_TYPE (op);
4407 103612649 : ao_ref op_ref;
4408 103612649 : ao_ref_init (&op_ref, op);
4409 103612649 : vr1.set = ao_ref_alias_set (&op_ref);
4410 103612649 : vr1.base_set = ao_ref_base_alias_set (&op_ref);
4411 103612649 : vr1.offset = 0;
4412 103612649 : vr1.max_size = -1;
4413 103612649 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4414 103612649 : if (mask == NULL_TREE)
4415 103292439 : if (tree cst = fully_constant_vn_reference_p (&vr1))
4416 : return cst;
4417 :
4418 103595865 : if (kind != VN_NOWALK && vr1.vuse)
4419 : {
4420 60174000 : vn_reference_t wvnresult;
4421 60174000 : ao_ref r;
4422 60174000 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4423 60174000 : auto_vec<vn_reference_op_s> ops_for_ref;
4424 60174000 : if (valueized_anything)
4425 : {
4426 4813254 : copy_reference_ops_from_ref (op, &ops_for_ref);
4427 4813254 : bool tem;
4428 4813254 : valueize_refs_1 (&ops_for_ref, &tem, true);
4429 : }
4430 : /* Make sure to use a valueized reference if we valueized anything.
4431 : Otherwise preserve the full reference for advanced TBAA. */
4432 60174000 : if (!valueized_anything
4433 60174000 : || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4434 : vr1.type, ops_for_ref))
4435 : {
4436 55360746 : ao_ref_init (&r, op);
4437 : /* Record the extra info we're getting from the full ref. */
4438 55360746 : ao_ref_base (&r);
4439 55360746 : vr1.offset = r.offset;
4440 55360746 : vr1.max_size = r.max_size;
4441 : }
4442 60174000 : vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4443 : last_vuse_ptr, kind, tbaa_p, mask,
4444 115534746 : redundant_store);
4445 :
4446 60174000 : wvnresult
4447 : = ((vn_reference_t)
4448 60174000 : walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4449 : vn_reference_lookup_3, vn_is_backedge,
4450 : vuse_valueize, limit, &data));
4451 120348000 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4452 60174000 : if (wvnresult)
4453 : {
4454 9018372 : gcc_assert (mask == NULL_TREE);
4455 9018372 : if (data.same_val
4456 9018372 : && (!wvnresult->result
4457 55001 : || !operand_equal_p (wvnresult->result, data.same_val)))
4458 : return NULL_TREE;
4459 8977404 : if (data.using_dummy_vr)
4460 12203 : return wvnresult->result;
4461 8965201 : if (vnresult)
4462 8963095 : *vnresult = wvnresult;
4463 8965201 : return wvnresult->result;
4464 : }
4465 51155628 : else if (mask)
4466 320210 : return data.masked_result;
4467 :
4468 : return NULL_TREE;
4469 60174000 : }
4470 :
4471 43421865 : if (last_vuse_ptr)
4472 1487198 : *last_vuse_ptr = vr1.vuse;
4473 43421865 : if (mask)
4474 : return NULL_TREE;
4475 43421865 : return vn_reference_lookup_1 (&vr1, vnresult);
4476 : }
4477 :
4478 : /* Lookup CALL in the current hash table and return the entry in
4479 : *VNRESULT if found. Populates *VR for the hashtable lookup. */
4480 :
4481 : void
4482 9397296 : vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4483 : vn_reference_t vr)
4484 : {
4485 9397296 : if (vnresult)
4486 9397296 : *vnresult = NULL;
4487 :
4488 9397296 : tree vuse = gimple_vuse (call);
4489 :
4490 9397296 : vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4491 9397296 : vr->operands = valueize_shared_reference_ops_from_call (call);
4492 9397296 : tree lhs = gimple_call_lhs (call);
4493 : /* For non-SSA return values the reference ops contain the LHS. */
4494 5120632 : vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4495 14068097 : ? TREE_TYPE (lhs) : NULL_TREE);
4496 9397296 : vr->punned = false;
4497 9397296 : vr->set = 0;
4498 9397296 : vr->base_set = 0;
4499 9397296 : vr->offset = 0;
4500 9397296 : vr->max_size = -1;
4501 9397296 : vr->hashcode = vn_reference_compute_hash (vr);
4502 9397296 : vn_reference_lookup_1 (vr, vnresult);
4503 9397296 : }
4504 :
4505 : /* Insert OP into the current hash table with a value number of RESULT. */
4506 :
4507 : static void
4508 77403588 : vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4509 : {
4510 77403588 : vn_reference_s **slot;
4511 77403588 : vn_reference_t vr1;
4512 77403588 : bool tem;
4513 :
4514 77403588 : vec<vn_reference_op_s> operands
4515 77403588 : = valueize_shared_reference_ops_from_ref (op, &tem);
4516 : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4517 : before the pass folding __builtin_object_size had a chance to run. */
4518 77403588 : if ((cfun->curr_properties & PROP_objsz)
4519 57979409 : && operands[0].opcode == ADDR_EXPR
4520 78321981 : && operands.last ().opcode == SSA_NAME)
4521 : {
4522 885426 : tree ops[2];
4523 885426 : if (vn_pp_nary_for_addr (operands, ops))
4524 : {
4525 560049 : vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4526 560049 : TREE_TYPE (op), ops, result,
4527 560049 : VN_INFO (result)->value_id);
4528 560049 : return;
4529 : }
4530 : }
4531 :
4532 76843539 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4533 76843539 : if (TREE_CODE (result) == SSA_NAME)
4534 53067350 : vr1->value_id = VN_INFO (result)->value_id;
4535 : else
4536 23776189 : vr1->value_id = get_or_alloc_constant_value_id (result);
4537 76843539 : vr1->vuse = vuse_ssa_val (vuse);
4538 76843539 : vr1->operands = operands.copy ();
4539 76843539 : vr1->type = TREE_TYPE (op);
4540 76843539 : vr1->punned = false;
4541 76843539 : ao_ref op_ref;
4542 76843539 : ao_ref_init (&op_ref, op);
4543 76843539 : vr1->set = ao_ref_alias_set (&op_ref);
4544 76843539 : vr1->base_set = ao_ref_base_alias_set (&op_ref);
4545 : /* Specifically use an unknown extent here, we're not doing any lookup
4546 : and assume the caller didn't either (or it went VARYING). */
4547 76843539 : vr1->offset = 0;
4548 76843539 : vr1->max_size = -1;
4549 76843539 : vr1->hashcode = vn_reference_compute_hash (vr1);
4550 76843539 : vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4551 76843539 : vr1->result_vdef = vdef;
4552 :
4553 76843539 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4554 : INSERT);
4555 :
4556 : /* Because IL walking on reference lookup can end up visiting
4557 : a def that is only to be visited later in iteration order
4558 : when we are about to make an irreducible region reducible
4559 : the def can be effectively processed and its ref being inserted
4560 : by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4561 : but save a lookup if we deal with already inserted refs here. */
4562 76843539 : if (*slot)
4563 : {
4564 : /* We cannot assert that we have the same value either because
4565 : when disentangling an irreducible region we may end up visiting
4566 : a use before the corresponding def. That's a missed optimization
4567 : only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4568 0 : if (dump_file && (dump_flags & TDF_DETAILS)
4569 0 : && !operand_equal_p ((*slot)->result, vr1->result, 0))
4570 : {
4571 0 : fprintf (dump_file, "Keeping old value ");
4572 0 : print_generic_expr (dump_file, (*slot)->result);
4573 0 : fprintf (dump_file, " because of collision\n");
4574 : }
4575 0 : free_reference (vr1);
4576 0 : obstack_free (&vn_tables_obstack, vr1);
4577 : return;
4578 : }
4579 :
4580 76843539 : *slot = vr1;
4581 76843539 : vr1->next = last_inserted_ref;
4582 76843539 : last_inserted_ref = vr1;
4583 : }
4584 :
4585 : /* Insert a reference by it's pieces into the current hash table with
4586 : a value number of RESULT. Return the resulting reference
4587 : structure we created. */
4588 :
4589 : vn_reference_t
4590 1691505 : vn_reference_insert_pieces (tree vuse, alias_set_type set,
4591 : alias_set_type base_set,
4592 : poly_int64 offset, poly_int64 max_size, tree type,
4593 : vec<vn_reference_op_s> operands,
4594 : tree result, unsigned int value_id)
4595 :
4596 : {
4597 1691505 : vn_reference_s **slot;
4598 1691505 : vn_reference_t vr1;
4599 :
4600 1691505 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4601 1691505 : vr1->value_id = value_id;
4602 1691505 : vr1->vuse = vuse_ssa_val (vuse);
4603 1691505 : vr1->operands = operands;
4604 1691505 : valueize_refs (&vr1->operands);
4605 1691505 : vr1->type = type;
4606 1691505 : vr1->punned = false;
4607 1691505 : vr1->set = set;
4608 1691505 : vr1->base_set = base_set;
4609 1691505 : vr1->offset = offset;
4610 1691505 : vr1->max_size = max_size;
4611 1691505 : vr1->hashcode = vn_reference_compute_hash (vr1);
4612 1691505 : if (result && TREE_CODE (result) == SSA_NAME)
4613 385305 : result = SSA_VAL (result);
4614 1691505 : vr1->result = result;
4615 1691505 : vr1->result_vdef = NULL_TREE;
4616 :
4617 1691505 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4618 : INSERT);
4619 :
4620 : /* At this point we should have all the things inserted that we have
4621 : seen before, and we should never try inserting something that
4622 : already exists. */
4623 1691505 : gcc_assert (!*slot);
4624 :
4625 1691505 : *slot = vr1;
4626 1691505 : vr1->next = last_inserted_ref;
4627 1691505 : last_inserted_ref = vr1;
4628 1691505 : return vr1;
4629 : }
4630 :
4631 : /* Compute and return the hash value for nary operation VBO1. */
4632 :
4633 : hashval_t
4634 314634929 : vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4635 : {
4636 314634929 : inchash::hash hstate;
4637 314634929 : unsigned i;
4638 :
4639 314634929 : if (((vno1->length == 2
4640 263321600 : && commutative_tree_code (vno1->opcode))
4641 144701633 : || (vno1->length == 3
4642 1715073 : && commutative_ternary_tree_code (vno1->opcode)))
4643 484570848 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4644 2512912 : std::swap (vno1->op[0], vno1->op[1]);
4645 312122017 : else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4646 312122017 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4647 : {
4648 467536 : std::swap (vno1->op[0], vno1->op[1]);
4649 467536 : vno1->opcode = swap_tree_comparison (vno1->opcode);
4650 : }
4651 :
4652 314634929 : hstate.add_int (vno1->opcode);
4653 896881261 : for (i = 0; i < vno1->length; ++i)
4654 582246332 : inchash::add_expr (vno1->op[i], hstate);
4655 :
4656 314634929 : return hstate.end ();
4657 : }
4658 :
4659 : /* Compare nary operations VNO1 and VNO2 and return true if they are
4660 : equivalent. */
4661 :
4662 : bool
4663 997729658 : vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4664 : {
4665 997729658 : unsigned i;
4666 :
4667 997729658 : if (vno1->hashcode != vno2->hashcode)
4668 : return false;
4669 :
4670 51870423 : if (vno1->length != vno2->length)
4671 : return false;
4672 :
4673 51870423 : if (vno1->opcode != vno2->opcode
4674 51870423 : || !types_compatible_p (vno1->type, vno2->type))
4675 : return false;
4676 :
4677 146350853 : for (i = 0; i < vno1->length; ++i)
4678 95792397 : if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4679 : return false;
4680 :
4681 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
4682 : of op1. Need to check to make sure they are the same. */
4683 50558456 : if (vno1->opcode == BIT_INSERT_EXPR
4684 454 : && TREE_CODE (vno1->op[1]) == INTEGER_CST
4685 50558514 : && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4686 58 : != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4687 0 : return false;
4688 :
4689 : return true;
4690 : }
4691 :
4692 : /* Initialize VNO from the pieces provided. */
4693 :
4694 : static void
4695 172698463 : init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4696 : enum tree_code code, tree type, tree *ops)
4697 : {
4698 172698463 : vno->opcode = code;
4699 172698463 : vno->length = length;
4700 172698463 : vno->type = type;
4701 4792028 : memcpy (&vno->op[0], ops, sizeof (tree) * length);
4702 0 : }
4703 :
4704 : /* Return the number of operands for a vn_nary ops structure from STMT. */
4705 :
4706 : unsigned int
4707 113920283 : vn_nary_length_from_stmt (gimple *stmt)
4708 : {
4709 113920283 : switch (gimple_assign_rhs_code (stmt))
4710 : {
4711 : case REALPART_EXPR:
4712 : case IMAGPART_EXPR:
4713 : case VIEW_CONVERT_EXPR:
4714 : return 1;
4715 :
4716 680416 : case BIT_FIELD_REF:
4717 680416 : return 3;
4718 :
4719 568971 : case CONSTRUCTOR:
4720 568971 : return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4721 :
4722 108894512 : default:
4723 108894512 : return gimple_num_ops (stmt) - 1;
4724 : }
4725 : }
4726 :
4727 : /* Initialize VNO from STMT. */
4728 :
4729 : void
4730 113920283 : init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4731 : {
4732 113920283 : unsigned i;
4733 :
4734 113920283 : vno->opcode = gimple_assign_rhs_code (stmt);
4735 113920283 : vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4736 113920283 : switch (vno->opcode)
4737 : {
4738 3776384 : case REALPART_EXPR:
4739 3776384 : case IMAGPART_EXPR:
4740 3776384 : case VIEW_CONVERT_EXPR:
4741 3776384 : vno->length = 1;
4742 3776384 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4743 3776384 : break;
4744 :
4745 680416 : case BIT_FIELD_REF:
4746 680416 : vno->length = 3;
4747 680416 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4748 680416 : vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4749 680416 : vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4750 680416 : break;
4751 :
4752 568971 : case CONSTRUCTOR:
4753 568971 : vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4754 2280160 : for (i = 0; i < vno->length; ++i)
4755 1711189 : vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4756 : break;
4757 :
4758 108894512 : default:
4759 108894512 : gcc_checking_assert (!gimple_assign_single_p (stmt));
4760 108894512 : vno->length = gimple_num_ops (stmt) - 1;
4761 298344608 : for (i = 0; i < vno->length; ++i)
4762 189450096 : vno->op[i] = gimple_op (stmt, i + 1);
4763 : }
4764 113920283 : }
4765 :
4766 : /* Compute the hashcode for VNO and look for it in the hash table;
4767 : return the resulting value number if it exists in the hash table.
4768 : Return NULL_TREE if it does not exist in the hash table or if the
4769 : result field of the operation is NULL. VNRESULT will contain the
4770 : vn_nary_op_t from the hashtable if it exists. */
4771 :
4772 : static tree
4773 137192129 : vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4774 : {
4775 137192129 : vn_nary_op_s **slot;
4776 :
4777 137192129 : if (vnresult)
4778 126506299 : *vnresult = NULL;
4779 :
4780 380367643 : for (unsigned i = 0; i < vno->length; ++i)
4781 243175514 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4782 172088839 : vno->op[i] = SSA_VAL (vno->op[i]);
4783 :
4784 137192129 : vno->hashcode = vn_nary_op_compute_hash (vno);
4785 137192129 : slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4786 137192129 : if (!slot)
4787 : return NULL_TREE;
4788 18320686 : if (vnresult)
4789 17834251 : *vnresult = *slot;
4790 18320686 : return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4791 : }
4792 :
4793 : /* Lookup a n-ary operation by its pieces and return the resulting value
4794 : number if it exists in the hash table. Return NULL_TREE if it does
4795 : not exist in the hash table or if the result field of the operation
4796 : is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4797 : if it exists. */
4798 :
4799 : tree
4800 56620253 : vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4801 : tree type, tree *ops, vn_nary_op_t *vnresult)
4802 : {
4803 56620253 : vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4804 : sizeof_vn_nary_op (length));
4805 56620253 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4806 56620253 : return vn_nary_op_lookup_1 (vno1, vnresult);
4807 : }
4808 :
4809 : /* Lookup the rhs of STMT in the current hash table, and return the resulting
4810 : value number if it exists in the hash table. Return NULL_TREE if
4811 : it does not exist in the hash table. VNRESULT will contain the
4812 : vn_nary_op_t from the hashtable if it exists. */
4813 :
4814 : tree
4815 58981624 : vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4816 : {
4817 58981624 : vn_nary_op_t vno1
4818 58981624 : = XALLOCAVAR (struct vn_nary_op_s,
4819 : sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4820 58981624 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4821 58981624 : return vn_nary_op_lookup_1 (vno1, vnresult);
4822 : }
4823 :
4824 : /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4825 :
4826 : vn_nary_op_t
4827 176409033 : alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4828 : {
4829 176409033 : return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4830 : }
4831 :
4832 : /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4833 : obstack. */
4834 :
4835 : static vn_nary_op_t
4836 158550814 : alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4837 : {
4838 0 : vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4839 :
4840 158550814 : vno1->value_id = value_id;
4841 158550814 : vno1->length = length;
4842 158550814 : vno1->predicated_values = 0;
4843 158550814 : vno1->u.result = result;
4844 :
4845 158550814 : return vno1;
4846 : }
4847 :
4848 : /* Insert VNO into TABLE. */
4849 :
4850 : static vn_nary_op_t
4851 163342842 : vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4852 : {
4853 163342842 : vn_nary_op_s **slot;
4854 :
4855 163342842 : gcc_assert (! vno->predicated_values
4856 : || (! vno->u.values->next
4857 : && vno->u.values->n == 1));
4858 :
4859 477827868 : for (unsigned i = 0; i < vno->length; ++i)
4860 314485026 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4861 205559362 : vno->op[i] = SSA_VAL (vno->op[i]);
4862 :
4863 163342842 : vno->hashcode = vn_nary_op_compute_hash (vno);
4864 163342842 : slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4865 163342842 : vno->unwind_to = *slot;
4866 163342842 : if (*slot)
4867 : {
4868 : /* Prefer non-predicated values.
4869 : ??? Only if those are constant, otherwise, with constant predicated
4870 : value, turn them into predicated values with entry-block validity
4871 : (??? but we always find the first valid result currently). */
4872 31204008 : if ((*slot)->predicated_values
4873 30457354 : && ! vno->predicated_values)
4874 : {
4875 : /* ??? We cannot remove *slot from the unwind stack list.
4876 : For the moment we deal with this by skipping not found
4877 : entries but this isn't ideal ... */
4878 73536 : *slot = vno;
4879 : /* ??? Maintain a stack of states we can unwind in
4880 : vn_nary_op_s? But how far do we unwind? In reality
4881 : we need to push change records somewhere... Or not
4882 : unwind vn_nary_op_s and linking them but instead
4883 : unwind the results "list", linking that, which also
4884 : doesn't move on hashtable resize. */
4885 : /* We can also have a ->unwind_to recording *slot there.
4886 : That way we can make u.values a fixed size array with
4887 : recording the number of entries but of course we then
4888 : have always N copies for each unwind_to-state. Or we
4889 : make sure to only ever append and each unwinding will
4890 : pop off one entry (but how to deal with predicated
4891 : replaced with non-predicated here?) */
4892 73536 : vno->next = last_inserted_nary;
4893 73536 : last_inserted_nary = vno;
4894 73536 : return vno;
4895 : }
4896 31130472 : else if (vno->predicated_values
4897 31130120 : && ! (*slot)->predicated_values)
4898 : return *slot;
4899 30384170 : else if (vno->predicated_values
4900 30383818 : && (*slot)->predicated_values)
4901 : {
4902 : /* ??? Factor this all into a insert_single_predicated_value
4903 : routine. */
4904 30383818 : gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4905 30383818 : basic_block vno_bb
4906 30383818 : = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4907 30383818 : vn_pval *nval = vno->u.values;
4908 30383818 : vn_pval **next = &vno->u.values;
4909 30383818 : vn_pval *ins = NULL;
4910 30383818 : vn_pval *ins_at = NULL;
4911 : /* Find an existing value to append to. */
4912 57109523 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4913 : {
4914 31400293 : if (expressions_equal_p (val->result, nval->result))
4915 : {
4916 : /* Limit the number of places we register a predicate
4917 : as valid. */
4918 4674588 : if (val->n > 8)
4919 129725 : return *slot;
4920 11537347 : for (unsigned i = 0; i < val->n; ++i)
4921 : {
4922 7243497 : basic_block val_bb
4923 7243497 : = BASIC_BLOCK_FOR_FN (cfun,
4924 : val->valid_dominated_by_p[i]);
4925 7243497 : if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4926 : /* Value registered with more generic predicate. */
4927 251013 : return *slot;
4928 6992484 : else if (flag_checking)
4929 : /* Shouldn't happen, we insert in RPO order. */
4930 6992484 : gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4931 : val_bb, vno_bb));
4932 : }
4933 : /* Append the location. */
4934 4293850 : ins_at = val;
4935 4293850 : ins = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4936 : sizeof (vn_pval)
4937 : + val->n * sizeof (int));
4938 4293850 : ins->next = NULL;
4939 4293850 : ins->result = val->result;
4940 4293850 : ins->n = val->n + 1;
4941 4293850 : memcpy (ins->valid_dominated_by_p,
4942 4293850 : val->valid_dominated_by_p,
4943 4293850 : val->n * sizeof (int));
4944 4293850 : ins->valid_dominated_by_p[val->n] = vno_bb->index;
4945 4293850 : if (dump_file && (dump_flags & TDF_DETAILS))
4946 4 : fprintf (dump_file, "Appending predicate to value.\n");
4947 : break;
4948 : }
4949 : }
4950 : /* Copy the rest of the value chain. */
4951 61930648 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4952 : {
4953 31927568 : if (val == ins_at)
4954 : /* Replace the node we appended to. */
4955 4293850 : *next = ins;
4956 : else
4957 : {
4958 : /* Copy other predicated values. */
4959 27633718 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4960 : sizeof (vn_pval)
4961 : + ((val->n-1)
4962 : * sizeof (int)));
4963 27633718 : memcpy (*next, val,
4964 27633718 : sizeof (vn_pval) + (val->n-1) * sizeof (int));
4965 27633718 : (*next)->next = NULL;
4966 : }
4967 31927568 : next = &(*next)->next;
4968 : }
4969 : /* Append the value if we didn't find it. */
4970 30003080 : if (!ins_at)
4971 25709230 : *next = nval;
4972 30003080 : *slot = vno;
4973 30003080 : vno->next = last_inserted_nary;
4974 30003080 : last_inserted_nary = vno;
4975 30003080 : return vno;
4976 : }
4977 :
4978 : /* While we do not want to insert things twice it's awkward to
4979 : avoid it in the case where visit_nary_op pattern-matches stuff
4980 : and ends up simplifying the replacement to itself. We then
4981 : get two inserts, one from visit_nary_op and one from
4982 : vn_nary_build_or_lookup.
4983 : So allow inserts with the same value number. */
4984 352 : if ((*slot)->u.result == vno->u.result)
4985 : return *slot;
4986 : }
4987 :
4988 : /* ??? There's also optimistic vs. previous committed state merging
4989 : that is problematic for the case of unwinding. */
4990 :
4991 : /* ??? We should return NULL if we do not use 'vno' and have the
4992 : caller release it. */
4993 132138834 : gcc_assert (!*slot);
4994 :
4995 132138834 : *slot = vno;
4996 132138834 : vno->next = last_inserted_nary;
4997 132138834 : last_inserted_nary = vno;
4998 132138834 : return vno;
4999 : }
5000 :
5001 : /* Insert a n-ary operation into the current hash table using it's
5002 : pieces. Return the vn_nary_op_t structure we created and put in
5003 : the hashtable. */
5004 :
5005 : vn_nary_op_t
5006 560049 : vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
5007 : tree type, tree *ops,
5008 : tree result, unsigned int value_id)
5009 : {
5010 560049 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
5011 560049 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
5012 560049 : return vn_nary_op_insert_into (vno1, valid_info->nary);
5013 : }
5014 :
5015 : /* Return whether we can track a predicate valid when PRED_E is executed. */
5016 :
5017 : static bool
5018 156097559 : can_track_predicate_on_edge (edge pred_e)
5019 : {
5020 : /* ??? As we are currently recording the destination basic-block index in
5021 : vn_pval.valid_dominated_by_p and using dominance for the
5022 : validity check we cannot track predicates on all edges. */
5023 156097559 : if (single_pred_p (pred_e->dest))
5024 : return true;
5025 : /* Never record for backedges. */
5026 12247173 : if (pred_e->flags & EDGE_DFS_BACK)
5027 : return false;
5028 : /* When there's more than one predecessor we cannot track
5029 : predicate validity based on the destination block. The
5030 : exception is when all other incoming edges sources are
5031 : dominated by the destination block. */
5032 11694948 : edge_iterator ei;
5033 11694948 : edge e;
5034 20003518 : FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
5035 18082048 : if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
5036 : return false;
5037 : return true;
5038 : }
5039 :
5040 : static vn_nary_op_t
5041 110726133 : vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
5042 : tree type, tree *ops,
5043 : tree result, unsigned int value_id,
5044 : edge pred_e)
5045 : {
5046 110726133 : if (flag_checking)
5047 110725223 : gcc_assert (can_track_predicate_on_edge (pred_e));
5048 :
5049 79804 : if (dump_file && (dump_flags & TDF_DETAILS)
5050 : /* ??? Fix dumping, but currently we only get comparisons. */
5051 110801683 : && TREE_CODE_CLASS (code) == tcc_comparison)
5052 : {
5053 75550 : fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
5054 75550 : pred_e->dest->index);
5055 75550 : print_generic_expr (dump_file, ops[0], TDF_SLIM);
5056 75550 : fprintf (dump_file, " %s ", get_tree_code_name (code));
5057 75550 : print_generic_expr (dump_file, ops[1], TDF_SLIM);
5058 112954 : fprintf (dump_file, " == %s\n",
5059 75550 : integer_zerop (result) ? "false" : "true");
5060 : }
5061 110726133 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
5062 110726133 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
5063 110726133 : vno1->predicated_values = 1;
5064 110726133 : vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
5065 : sizeof (vn_pval));
5066 110726133 : vno1->u.values->next = NULL;
5067 110726133 : vno1->u.values->result = result;
5068 110726133 : vno1->u.values->n = 1;
5069 110726133 : vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
5070 110726133 : return vn_nary_op_insert_into (vno1, valid_info->nary);
5071 : }
5072 :
5073 : static bool
5074 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2);
5075 :
5076 : static tree
5077 1775358 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
5078 : edge e = NULL)
5079 : {
5080 1775358 : if (! vno->predicated_values)
5081 0 : return vno->u.result;
5082 3705256 : for (vn_pval *val = vno->u.values; val; val = val->next)
5083 5643958 : for (unsigned i = 0; i < val->n; ++i)
5084 : {
5085 3714060 : basic_block cand
5086 3714060 : = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
5087 : /* When asking for predicated values on an edge avoid looking
5088 : at edge executability for edges forward in our iteration
5089 : as well. */
5090 3714060 : if (e && (e->flags & EDGE_DFS_BACK))
5091 : {
5092 22225 : if (dominated_by_p (CDI_DOMINATORS, bb, cand))
5093 9025 : return val->result;
5094 : }
5095 3691835 : else if (dominated_by_p_w_unex (bb, cand))
5096 540699 : return val->result;
5097 : }
5098 : return NULL_TREE;
5099 : }
5100 :
5101 : static tree
5102 207017 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
5103 : {
5104 0 : return vn_nary_op_get_predicated_value (vno, e->src, e);
5105 : }
5106 :
5107 : /* Insert the rhs of STMT into the current hash table with a value number of
5108 : RESULT. */
5109 :
5110 : static vn_nary_op_t
5111 47264632 : vn_nary_op_insert_stmt (gassign *stmt, tree result)
5112 : {
5113 47264632 : vn_nary_op_t vno1
5114 47264632 : = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
5115 47264632 : result, VN_INFO (result)->value_id);
5116 47264632 : init_vn_nary_op_from_stmt (vno1, stmt);
5117 47264632 : return vn_nary_op_insert_into (vno1, valid_info->nary);
5118 : }
5119 :
5120 : /* Compute a hashcode for PHI operation VP1 and return it. */
5121 :
5122 : static inline hashval_t
5123 51835201 : vn_phi_compute_hash (vn_phi_t vp1)
5124 : {
5125 51835201 : inchash::hash hstate;
5126 51835201 : tree phi1op;
5127 51835201 : tree type;
5128 51835201 : edge e;
5129 51835201 : edge_iterator ei;
5130 :
5131 103670402 : hstate.add_int (EDGE_COUNT (vp1->block->preds));
5132 51835201 : switch (EDGE_COUNT (vp1->block->preds))
5133 : {
5134 : case 1:
5135 : break;
5136 45031362 : case 2:
5137 : /* When this is a PHI node subject to CSE for different blocks
5138 : avoid hashing the block index. */
5139 45031362 : if (vp1->cclhs)
5140 : break;
5141 : /* Fallthru. */
5142 35173730 : default:
5143 35173730 : hstate.add_int (vp1->block->index);
5144 : }
5145 :
5146 : /* If all PHI arguments are constants we need to distinguish
5147 : the PHI node via its type. */
5148 51835201 : type = vp1->type;
5149 51835201 : hstate.merge_hash (vn_hash_type (type));
5150 :
5151 179032083 : FOR_EACH_EDGE (e, ei, vp1->block->preds)
5152 : {
5153 : /* Don't hash backedge values they need to be handled as VN_TOP
5154 : for optimistic value-numbering. */
5155 127196882 : if (e->flags & EDGE_DFS_BACK)
5156 27821217 : continue;
5157 :
5158 99375665 : phi1op = vp1->phiargs[e->dest_idx];
5159 99375665 : if (phi1op == VN_TOP)
5160 255517 : continue;
5161 99120148 : inchash::add_expr (phi1op, hstate);
5162 : }
5163 :
5164 51835201 : return hstate.end ();
5165 : }
5166 :
5167 :
5168 : /* Return true if COND1 and COND2 represent the same condition, set
5169 : *INVERTED_P if one needs to be inverted to make it the same as
5170 : the other. */
5171 :
5172 : static bool
5173 3511139 : cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
5174 : gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
5175 : {
5176 3511139 : enum tree_code code1 = gimple_cond_code (cond1);
5177 3511139 : enum tree_code code2 = gimple_cond_code (cond2);
5178 :
5179 3511139 : *inverted_p = false;
5180 3511139 : if (code1 == code2)
5181 : ;
5182 288564 : else if (code1 == swap_tree_comparison (code2))
5183 : std::swap (lhs2, rhs2);
5184 251441 : else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
5185 116120 : *inverted_p = true;
5186 135321 : else if (code1 == invert_tree_comparison
5187 135321 : (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
5188 : {
5189 10278 : std::swap (lhs2, rhs2);
5190 10278 : *inverted_p = true;
5191 : }
5192 : else
5193 : return false;
5194 :
5195 3386096 : return ((expressions_equal_p (lhs1, lhs2)
5196 108953 : && expressions_equal_p (rhs1, rhs2))
5197 3413828 : || (commutative_tree_code (code1)
5198 1726655 : && expressions_equal_p (lhs1, rhs2)
5199 3187 : && expressions_equal_p (rhs1, lhs2)));
5200 : }
5201 :
5202 : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
5203 :
5204 : static int
5205 42229040 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
5206 : {
5207 42229040 : if (vp1->hashcode != vp2->hashcode)
5208 : return false;
5209 :
5210 12563981 : if (vp1->block != vp2->block)
5211 : {
5212 10553226 : if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
5213 : return false;
5214 :
5215 3517742 : switch (EDGE_COUNT (vp1->block->preds))
5216 : {
5217 : case 1:
5218 : /* Single-arg PHIs are just copies. */
5219 : break;
5220 :
5221 3517742 : case 2:
5222 3517742 : {
5223 : /* Make sure both PHIs are classified as CSEable. */
5224 3517742 : if (! vp1->cclhs || ! vp2->cclhs)
5225 : return false;
5226 :
5227 : /* Rule out backedges into the PHI. */
5228 3517742 : gcc_checking_assert
5229 : (vp1->block->loop_father->header != vp1->block
5230 : && vp2->block->loop_father->header != vp2->block);
5231 :
5232 : /* If the PHI nodes do not have compatible types
5233 : they are not the same. */
5234 3517742 : if (!types_compatible_p (vp1->type, vp2->type))
5235 : return false;
5236 :
5237 : /* If the immediate dominator end in switch stmts multiple
5238 : values may end up in the same PHI arg via intermediate
5239 : CFG merges. */
5240 3511139 : basic_block idom1
5241 3511139 : = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5242 3511139 : basic_block idom2
5243 3511139 : = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
5244 3511139 : gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
5245 : && EDGE_COUNT (idom2->succs) == 2);
5246 :
5247 : /* Verify the controlling stmt is the same. */
5248 7022278 : gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
5249 7022278 : gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
5250 3511139 : bool inverted_p;
5251 3511139 : if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
5252 3511139 : last2, vp2->cclhs, vp2->ccrhs,
5253 : &inverted_p))
5254 : return false;
5255 :
5256 : /* Get at true/false controlled edges into the PHI. */
5257 81391 : edge te1, te2, fe1, fe2;
5258 81391 : if (! extract_true_false_controlled_edges (idom1, vp1->block,
5259 : &te1, &fe1)
5260 81391 : || ! extract_true_false_controlled_edges (idom2, vp2->block,
5261 : &te2, &fe2))
5262 : return false;
5263 :
5264 : /* Swap edges if the second condition is the inverted of the
5265 : first. */
5266 46654 : if (inverted_p)
5267 1856 : std::swap (te2, fe2);
5268 :
5269 : /* Since we do not know which edge will be executed we have
5270 : to be careful when matching VN_TOP. Be conservative and
5271 : only match VN_TOP == VN_TOP for now, we could allow
5272 : VN_TOP on the not prevailing PHI though. See for example
5273 : PR102920. */
5274 46654 : if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
5275 46654 : vp2->phiargs[te2->dest_idx], false)
5276 91581 : || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
5277 44927 : vp2->phiargs[fe2->dest_idx], false))
5278 1727 : return false;
5279 :
5280 : return true;
5281 : }
5282 :
5283 : default:
5284 : return false;
5285 : }
5286 : }
5287 :
5288 : /* If the PHI nodes do not have compatible types
5289 : they are not the same. */
5290 9046239 : if (!types_compatible_p (vp1->type, vp2->type))
5291 : return false;
5292 :
5293 : /* Any phi in the same block will have it's arguments in the
5294 : same edge order, because of how we store phi nodes. */
5295 9045345 : unsigned nargs = EDGE_COUNT (vp1->block->preds);
5296 20295659 : for (unsigned i = 0; i < nargs; ++i)
5297 : {
5298 16105691 : tree phi1op = vp1->phiargs[i];
5299 16105691 : tree phi2op = vp2->phiargs[i];
5300 16105691 : if (phi1op == phi2op)
5301 11153915 : continue;
5302 4951776 : if (!expressions_equal_p (phi1op, phi2op, false))
5303 : return false;
5304 : }
5305 :
5306 : return true;
5307 : }
5308 :
5309 : /* Lookup PHI in the current hash table, and return the resulting
5310 : value number if it exists in the hash table. Return NULL_TREE if
5311 : it does not exist in the hash table. */
5312 :
5313 : static tree
5314 28355792 : vn_phi_lookup (gimple *phi, bool backedges_varying_p)
5315 : {
5316 28355792 : vn_phi_s **slot;
5317 28355792 : struct vn_phi_s *vp1;
5318 28355792 : edge e;
5319 28355792 : edge_iterator ei;
5320 :
5321 28355792 : vp1 = XALLOCAVAR (struct vn_phi_s,
5322 : sizeof (struct vn_phi_s)
5323 : + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
5324 :
5325 : /* Canonicalize the SSA_NAME's to their value number. */
5326 97031939 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5327 : {
5328 68676147 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5329 68676147 : if (TREE_CODE (def) == SSA_NAME
5330 57052729 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5331 : {
5332 54389847 : if (!virtual_operand_p (def)
5333 54389847 : && ssa_undefined_value_p (def, false))
5334 141055 : def = VN_TOP;
5335 : else
5336 54248792 : def = SSA_VAL (def);
5337 : }
5338 68676147 : vp1->phiargs[e->dest_idx] = def;
5339 : }
5340 28355792 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5341 28355792 : vp1->block = gimple_bb (phi);
5342 : /* Extract values of the controlling condition. */
5343 28355792 : vp1->cclhs = NULL_TREE;
5344 28355792 : vp1->ccrhs = NULL_TREE;
5345 28355792 : if (EDGE_COUNT (vp1->block->preds) == 2
5346 28355792 : && vp1->block->loop_father->header != vp1->block)
5347 : {
5348 8822703 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5349 8822703 : if (EDGE_COUNT (idom1->succs) == 2)
5350 17530426 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5351 : {
5352 : /* ??? We want to use SSA_VAL here. But possibly not
5353 : allow VN_TOP. */
5354 8516558 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5355 8516558 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5356 : }
5357 : }
5358 28355792 : vp1->hashcode = vn_phi_compute_hash (vp1);
5359 28355792 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
5360 28355792 : if (!slot)
5361 : return NULL_TREE;
5362 4234895 : return (*slot)->result;
5363 : }
5364 :
5365 : /* Insert PHI into the current hash table with a value number of
5366 : RESULT. */
5367 :
5368 : static vn_phi_t
5369 23479409 : vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
5370 : {
5371 23479409 : vn_phi_s **slot;
5372 23479409 : vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
5373 : sizeof (vn_phi_s)
5374 : + ((gimple_phi_num_args (phi) - 1)
5375 : * sizeof (tree)));
5376 23479409 : edge e;
5377 23479409 : edge_iterator ei;
5378 :
5379 : /* Canonicalize the SSA_NAME's to their value number. */
5380 82000144 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5381 : {
5382 58520735 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5383 58520735 : if (TREE_CODE (def) == SSA_NAME
5384 47959866 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5385 : {
5386 45297492 : if (!virtual_operand_p (def)
5387 45297492 : && ssa_undefined_value_p (def, false))
5388 114698 : def = VN_TOP;
5389 : else
5390 45182794 : def = SSA_VAL (def);
5391 : }
5392 58520735 : vp1->phiargs[e->dest_idx] = def;
5393 : }
5394 23479409 : vp1->value_id = VN_INFO (result)->value_id;
5395 23479409 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5396 23479409 : vp1->block = gimple_bb (phi);
5397 : /* Extract values of the controlling condition. */
5398 23479409 : vp1->cclhs = NULL_TREE;
5399 23479409 : vp1->ccrhs = NULL_TREE;
5400 23479409 : if (EDGE_COUNT (vp1->block->preds) == 2
5401 23479409 : && vp1->block->loop_father->header != vp1->block)
5402 : {
5403 8445538 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5404 8445538 : if (EDGE_COUNT (idom1->succs) == 2)
5405 16781180 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5406 : {
5407 : /* ??? We want to use SSA_VAL here. But possibly not
5408 : allow VN_TOP. */
5409 8144913 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5410 8144913 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5411 : }
5412 : }
5413 23479409 : vp1->result = result;
5414 23479409 : vp1->hashcode = vn_phi_compute_hash (vp1);
5415 :
5416 23479409 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5417 23479409 : gcc_assert (!*slot);
5418 :
5419 23479409 : *slot = vp1;
5420 23479409 : vp1->next = last_inserted_phi;
5421 23479409 : last_inserted_phi = vp1;
5422 23479409 : return vp1;
5423 : }
5424 :
5425 :
5426 : /* Find the nearest common dominator of R1 and R2 in the IDOMS tree
5427 : using Reverse Postorder (RPO) numbers. Both walks terminate latest
5428 : at the root (RPO number 0, whose IDOMS entry is -1) because callers
5429 : maintain the invariant IDOMS[x] < x for every x they store into
5430 : IDOMS. */
5431 :
5432 : static int
5433 17899394 : exec_idom_intersect (int *idoms, int r1, int r2)
5434 : {
5435 473289469 : while (r1 != r2)
5436 : {
5437 455390075 : if (r1 > r2)
5438 13318723 : r1 = idoms[r1];
5439 : else
5440 442071352 : r2 = idoms[r2];
5441 : }
5442 0 : return r1;
5443 : }
5444 :
5445 : /* Update the immediate dominator of BB based on its executable predecessors.
5446 :
5447 : This performs one step of a Cooper-Harvey-Kennedy style dominator
5448 : computation, but unlike the textbook algorithm we do not have the
5449 : luxury of iterating over a fixed graph to a fixpoint: the executable
5450 : subgraph is discovered incrementally as the RPO walk proceeds (edges
5451 : only ever turn executable, never back), and BB can be visited before
5452 : every one of its predecessors has reached its final executable state.
5453 :
5454 : We can only trust an edges executable state if it is not a backedge
5455 : in the RPO order. Any other predecessor edge is folded in as BB's
5456 : immediate dominator in the static (whole-graph, CDI_DOMINATORS)
5457 : dominator tree. */
5458 :
5459 : static void
5460 63549132 : update_exec_idom (basic_block bb)
5461 : {
5462 63549132 : int rpo = vn_bb_to_rpo[bb->index];
5463 : /* Skip the entry block (RPO number zero) as its predecessors are
5464 : not covered by the region. */
5465 63549132 : if (rpo == 0)
5466 11809991 : return;
5467 :
5468 57211528 : edge_iterator ei;
5469 57211528 : edge e;
5470 57211528 : int idom = -1;
5471 130893935 : FOR_EACH_EDGE (e, ei, bb->preds)
5472 : {
5473 79154794 : int pred_rpo = vn_bb_to_rpo[e->src->index];
5474 : /* For backedges fall back to the static immediate dominator. */
5475 79154794 : if (pred_rpo >= rpo)
5476 : /* We initialize to the static immediate dominator and this is
5477 : never overwritten to another value since we either visit each
5478 : block exactly once or iterate in the same order. */
5479 : return;
5480 :
5481 73682407 : if (e->flags & EDGE_EXECUTABLE)
5482 : {
5483 72987119 : if (idom == -1)
5484 : idom = pred_rpo;
5485 : else
5486 91581801 : idom = exec_idom_intersect (vn_exec_idom, idom, pred_rpo);
5487 : }
5488 : }
5489 :
5490 51739141 : gcc_checking_assert (idom != -1);
5491 51739141 : vn_exec_idom[rpo] = idom;
5492 : }
5493 :
5494 : /* Return true if BB1 is dominated by BB2 taking into account edges
5495 : that are known executable, including those that are not executable
5496 : only because they are backedges not yet proven to be taken (see
5497 : the commentary on update_exec_idom for why folding those in
5498 : unconditionally is always sound, and why it used to be handled by
5499 : a second, separate optimistic/pessimistic pair of trees here).
5500 :
5501 : Note: It is assumed that both BB1 and BB2 are inside the region
5502 : currently being value-numbered. */
5503 :
5504 : static bool
5505 77877015 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2)
5506 : {
5507 77877015 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5508 : return true;
5509 :
5510 22029628 : if (!vn_exec_idom)
5511 : return false;
5512 :
5513 22029628 : int rpo2 = vn_bb_to_rpo[bb2->index];
5514 22029628 : int rpo1 = vn_bb_to_rpo[bb1->index];
5515 :
5516 22029628 : int curr = rpo1;
5517 22029628 : int limit = 32;
5518 124625239 : while (curr > rpo2 && limit-- > 0)
5519 102595611 : curr = vn_exec_idom[curr];
5520 :
5521 22029628 : return curr == rpo2;
5522 : }
5523 :
5524 : /* Set the value number of FROM to TO, return true if it has changed
5525 : as a result. */
5526 :
5527 : static inline bool
5528 213376082 : set_ssa_val_to (tree from, tree to)
5529 : {
5530 213376082 : vn_ssa_aux_t from_info = VN_INFO (from);
5531 213376082 : tree currval = from_info->valnum; // SSA_VAL (from)
5532 213376082 : poly_int64 toff, coff;
5533 213376082 : bool curr_undefined = false;
5534 213376082 : bool curr_invariant = false;
5535 :
5536 : /* The only thing we allow as value numbers are ssa_names
5537 : and invariants. So assert that here. We don't allow VN_TOP
5538 : as visiting a stmt should produce a value-number other than
5539 : that.
5540 : ??? Still VN_TOP can happen for unreachable code, so force
5541 : it to varying in that case. Not all code is prepared to
5542 : get VN_TOP on valueization. */
5543 213376082 : if (to == VN_TOP)
5544 : {
5545 : /* ??? When iterating and visiting PHI <undef, backedge-value>
5546 : for the first time we rightfully get VN_TOP and we need to
5547 : preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5548 : With SCCVN we were simply lucky we iterated the other PHI
5549 : cycles first and thus visited the backedge-value DEF. */
5550 0 : if (currval == VN_TOP)
5551 0 : goto set_and_exit;
5552 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5553 0 : fprintf (dump_file, "Forcing value number to varying on "
5554 : "receiving VN_TOP\n");
5555 : to = from;
5556 : }
5557 :
5558 213414334 : gcc_checking_assert (to != NULL_TREE
5559 : && ((TREE_CODE (to) == SSA_NAME
5560 : && (to == from || SSA_VAL (to) == to
5561 : /* PHIs in a block are value-numbered in
5562 : parallel, a PHI result in the same block
5563 : as FROM we CSE to still has the previous
5564 : iteration value number which might not be
5565 : VARYING. */
5566 : || (is_a <gphi *> (SSA_NAME_DEF_STMT (from))
5567 : && is_a <gphi *> (SSA_NAME_DEF_STMT (to))
5568 : && (gimple_bb (SSA_NAME_DEF_STMT (from))
5569 : == gimple_bb (SSA_NAME_DEF_STMT (to))))))
5570 : || is_gimple_min_invariant (to)));
5571 :
5572 213376082 : if (from != to)
5573 : {
5574 34219430 : if (currval == from)
5575 : {
5576 13346 : if (dump_file && (dump_flags & TDF_DETAILS))
5577 : {
5578 0 : fprintf (dump_file, "Not changing value number of ");
5579 0 : print_generic_expr (dump_file, from);
5580 0 : fprintf (dump_file, " from VARYING to ");
5581 0 : print_generic_expr (dump_file, to);
5582 0 : fprintf (dump_file, "\n");
5583 : }
5584 : return false;
5585 : }
5586 34206084 : curr_invariant = is_gimple_min_invariant (currval);
5587 68412168 : curr_undefined = (TREE_CODE (currval) == SSA_NAME
5588 3939337 : && !virtual_operand_p (currval)
5589 37915146 : && ssa_undefined_value_p (currval, false));
5590 34206084 : if (currval != VN_TOP
5591 : && !curr_invariant
5592 5514583 : && !curr_undefined
5593 38132724 : && is_gimple_min_invariant (to))
5594 : {
5595 202 : if (dump_file && (dump_flags & TDF_DETAILS))
5596 : {
5597 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5598 : "value number of ");
5599 0 : print_generic_expr (dump_file, from);
5600 0 : fprintf (dump_file, " from ");
5601 0 : print_generic_expr (dump_file, currval);
5602 0 : fprintf (dump_file, " (non-constant) to ");
5603 0 : print_generic_expr (dump_file, to);
5604 0 : fprintf (dump_file, " (constant)\n");
5605 : }
5606 : to = from;
5607 : }
5608 34205882 : else if (currval != VN_TOP
5609 5514381 : && !curr_undefined
5610 5501684 : && TREE_CODE (to) == SSA_NAME
5611 4643075 : && !virtual_operand_p (to)
5612 38618682 : && ssa_undefined_value_p (to, false))
5613 : {
5614 6 : if (dump_file && (dump_flags & TDF_DETAILS))
5615 : {
5616 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5617 : "value number of ");
5618 0 : print_generic_expr (dump_file, from);
5619 0 : fprintf (dump_file, " from ");
5620 0 : print_generic_expr (dump_file, currval);
5621 0 : fprintf (dump_file, " (non-undefined) to ");
5622 0 : print_generic_expr (dump_file, to);
5623 0 : fprintf (dump_file, " (undefined)\n");
5624 : }
5625 : to = from;
5626 : }
5627 34205876 : else if (TREE_CODE (to) == SSA_NAME
5628 34205876 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5629 : to = from;
5630 : }
5631 :
5632 179156652 : set_and_exit:
5633 213362736 : if (dump_file && (dump_flags & TDF_DETAILS))
5634 : {
5635 418696 : fprintf (dump_file, "Setting value number of ");
5636 418696 : print_generic_expr (dump_file, from);
5637 418696 : fprintf (dump_file, " to ");
5638 418696 : print_generic_expr (dump_file, to);
5639 : }
5640 :
5641 213362736 : if (currval != to
5642 176127162 : && !operand_equal_p (currval, to, 0)
5643 : /* Different undefined SSA names are not actually different. See
5644 : PR82320 for a testcase were we'd otherwise not terminate iteration. */
5645 176057126 : && !(curr_undefined
5646 3903 : && TREE_CODE (to) == SSA_NAME
5647 732 : && !virtual_operand_p (to)
5648 732 : && ssa_undefined_value_p (to, false))
5649 : /* ??? For addresses involving volatile objects or types operand_equal_p
5650 : does not reliably detect ADDR_EXPRs as equal. We know we are only
5651 : getting invariant gimple addresses here, so can use
5652 : get_addr_base_and_unit_offset to do this comparison. */
5653 389419061 : && !(TREE_CODE (currval) == ADDR_EXPR
5654 510157 : && TREE_CODE (to) == ADDR_EXPR
5655 12 : && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5656 6 : == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5657 6 : && known_eq (coff, toff)))
5658 : {
5659 176056319 : if (to != from
5660 29765543 : && currval != VN_TOP
5661 1077826 : && !curr_undefined
5662 : /* We do not want to allow lattice transitions from one value
5663 : to another since that may lead to not terminating iteration
5664 : (see PR95049). Since there's no convenient way to check
5665 : for the allowed transition of VAL -> PHI (loop entry value,
5666 : same on two PHIs, to same PHI result) we restrict the check
5667 : to invariants. */
5668 1077826 : && curr_invariant
5669 176772950 : && is_gimple_min_invariant (to))
5670 : {
5671 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5672 0 : fprintf (dump_file, " forced VARYING");
5673 : to = from;
5674 : }
5675 176056319 : if (dump_file && (dump_flags & TDF_DETAILS))
5676 418400 : fprintf (dump_file, " (changed)\n");
5677 176056319 : from_info->valnum = to;
5678 176056319 : return true;
5679 : }
5680 37306417 : if (dump_file && (dump_flags & TDF_DETAILS))
5681 296 : fprintf (dump_file, "\n");
5682 : return false;
5683 : }
5684 :
5685 : /* Set all definitions in STMT to value number to themselves.
5686 : Return true if a value number changed. */
5687 :
5688 : static bool
5689 307969192 : defs_to_varying (gimple *stmt)
5690 : {
5691 307969192 : bool changed = false;
5692 307969192 : ssa_op_iter iter;
5693 307969192 : def_operand_p defp;
5694 :
5695 339159698 : FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5696 : {
5697 31190506 : tree def = DEF_FROM_PTR (defp);
5698 31190506 : changed |= set_ssa_val_to (def, def);
5699 : }
5700 307969192 : return changed;
5701 : }
5702 :
5703 : /* Visit a copy between LHS and RHS, return true if the value number
5704 : changed. */
5705 :
5706 : static bool
5707 8300749 : visit_copy (tree lhs, tree rhs)
5708 : {
5709 : /* Valueize. */
5710 8300749 : rhs = SSA_VAL (rhs);
5711 :
5712 8300749 : return set_ssa_val_to (lhs, rhs);
5713 : }
5714 :
5715 : /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5716 : is the same. */
5717 :
5718 : static tree
5719 2585904 : valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5720 : {
5721 2585904 : if (TREE_CODE (op) == SSA_NAME)
5722 2270397 : op = vn_valueize (op);
5723 :
5724 : /* Either we have the op widened available. */
5725 2585904 : tree ops[3] = {};
5726 2585904 : ops[0] = op;
5727 2585904 : tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5728 : wide_type, ops, NULL);
5729 2585904 : if (tem)
5730 : return tem;
5731 :
5732 : /* Or the op is truncated from some existing value. */
5733 2286265 : if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5734 : {
5735 586002 : auto *def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op));
5736 559229 : if (def && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5737 : {
5738 318296 : tem = gimple_assign_rhs1 (def);
5739 318296 : if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5740 : {
5741 218029 : if (TREE_CODE (tem) == SSA_NAME)
5742 218029 : tem = vn_valueize (tem);
5743 : return tem;
5744 : }
5745 : }
5746 : }
5747 :
5748 : /* For constants simply extend it. */
5749 2068236 : if (TREE_CODE (op) == INTEGER_CST)
5750 349253 : return wide_int_to_tree (wide_type, wi::to_widest (op));
5751 :
5752 : return NULL_TREE;
5753 : }
5754 :
5755 : /* Return a value-number for RES_OP whose first operand is VAL, the
5756 : result of a value-number lookup, or NULL_TREE if VAL may not be used
5757 : at the statement being visited. When VAL has a leader available in
5758 : the current block simplify using that, otherwise VAL is a leader
5759 : that does not dominate the statement being visited and using SSA
5760 : name info such as range information of it is not valid. We can
5761 : still insert the unsimplified expression if VAL is of wrapping type
5762 : since that can be inserted for code hoisting without introducing
5763 : undefined overflow, see PR86554. See PR128884. */
5764 :
5765 : static tree
5766 22471 : vn_nary_build_or_lookup_avail (gimple_match_op *res_op, tree val)
5767 : {
5768 22471 : tree leader = val;
5769 22471 : if (TREE_CODE (val) == SSA_NAME)
5770 22471 : leader = (rpo_avail && vn_context_bb
5771 44942 : ? rpo_avail->eliminate_avail (vn_context_bb, val) : NULL_TREE);
5772 22471 : if (leader)
5773 : {
5774 19590 : res_op->ops[0] = leader;
5775 19590 : return vn_nary_build_or_lookup (res_op);
5776 : }
5777 2881 : if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val)))
5778 : {
5779 465 : res_op->ops[0] = val;
5780 465 : return vn_nary_build_or_lookup_1 (res_op, true, false);
5781 : }
5782 : return NULL_TREE;
5783 : }
5784 :
5785 : /* If OP is an SSA name defined by a conversion from an integral type,
5786 : return the valueized source of the conversion, otherwise return
5787 : NULL_TREE. */
5788 :
5789 : static tree
5790 14550601 : ssa_integral_conversion_op (tree op)
5791 : {
5792 14550601 : if (TREE_CODE (op) != SSA_NAME)
5793 : return NULL_TREE;
5794 14230998 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op));
5795 11885380 : if (!def || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5796 : return NULL_TREE;
5797 1538794 : const tree src = gimple_assign_rhs1 (def);
5798 1538794 : if (!INTEGRAL_TYPE_P (TREE_TYPE (src)))
5799 : return NULL_TREE;
5800 1280953 : return vn_valueize (src);
5801 : }
5802 :
5803 : /* Visit a nary operator RHS, value number it, and return true if the
5804 : value number of LHS has changed as a result. */
5805 :
5806 : static bool
5807 51013698 : visit_nary_op (tree lhs, gassign *stmt)
5808 : {
5809 51013698 : vn_nary_op_t vnresult;
5810 51013698 : tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5811 51013698 : if (! result && vnresult)
5812 146849 : result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5813 47180926 : if (result)
5814 3906085 : return set_ssa_val_to (lhs, result);
5815 :
5816 : /* Do some special pattern matching for redundancies of operations
5817 : in different types. */
5818 47107613 : enum tree_code code = gimple_assign_rhs_code (stmt);
5819 47107613 : tree type = TREE_TYPE (lhs);
5820 47107613 : tree rhs1 = gimple_assign_rhs1 (stmt);
5821 47107613 : switch (code)
5822 : {
5823 10449338 : CASE_CONVERT:
5824 : /* Match arithmetic done in a different type where we can easily
5825 : substitute the result from some earlier sign-changed or widened
5826 : operation. */
5827 10449338 : if (INTEGRAL_TYPE_P (type)
5828 9380033 : && TREE_CODE (rhs1) == SSA_NAME
5829 : /* We only handle sign-changes, zero-extension -> & mask or
5830 : sign-extension if we know the inner operation doesn't
5831 : overflow. */
5832 19586457 : && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5833 5508744 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5834 5507947 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5835 8407594 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5836 6204089 : || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5837 : {
5838 7979082 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5839 5827469 : if (def
5840 5827469 : && (gimple_assign_rhs_code (def) == PLUS_EXPR
5841 4548793 : || gimple_assign_rhs_code (def) == MINUS_EXPR
5842 4377747 : || gimple_assign_rhs_code (def) == MULT_EXPR))
5843 : {
5844 2079296 : tree ops[3] = {};
5845 : /* When requiring a sign-extension we cannot model a
5846 : previous truncation with a single op so don't bother. */
5847 2079296 : bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5848 : /* Either we have the op widened available. */
5849 2079296 : ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5850 : allow_truncate);
5851 2079296 : if (ops[0])
5852 1013216 : ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5853 : allow_truncate);
5854 2079296 : if (ops[0] && ops[1])
5855 : {
5856 360313 : ops[0] = vn_nary_op_lookup_pieces
5857 360313 : (2, gimple_assign_rhs_code (def), type, ops, NULL);
5858 360313 : if (ops[0])
5859 : {
5860 9490 : unsigned lhs_prec = TYPE_PRECISION (type);
5861 9490 : unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5862 9490 : if (lhs_prec == rhs_prec
5863 9490 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5864 873 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5865 : {
5866 : /* The wider operation computes the same value.
5867 : Use it if it is available or can be inserted
5868 : for code hoisting, see PR86554. */
5869 8824 : if (TREE_CODE (ops[0]) != SSA_NAME
5870 8824 : || TYPE_OVERFLOW_WRAPS (type)
5871 9639 : || (rpo_avail && vn_context_bb
5872 815 : && rpo_avail->eliminate_avail (vn_context_bb,
5873 : ops[0])))
5874 8387 : result = ops[0];
5875 : }
5876 : else
5877 : {
5878 666 : tree mask = wide_int_to_tree
5879 666 : (type, wi::mask (rhs_prec, false, lhs_prec));
5880 666 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5881 666 : BIT_AND_EXPR,
5882 666 : TREE_TYPE (lhs),
5883 666 : ops[0], mask);
5884 666 : result = vn_nary_build_or_lookup_avail (&match_op,
5885 : ops[0]);
5886 : }
5887 9490 : if (result)
5888 : {
5889 9053 : bool changed = set_ssa_val_to (lhs, result);
5890 9053 : if (TREE_CODE (result) == SSA_NAME)
5891 9053 : vn_nary_op_insert_stmt (stmt, result);
5892 9053 : return changed;
5893 : }
5894 : }
5895 : }
5896 : }
5897 : }
5898 : break;
5899 14184367 : case PLUS_EXPR:
5900 14184367 : case MINUS_EXPR:
5901 14184367 : {
5902 : /* Match (T)A +- B against an existing (T)(A +- B'), the inverse
5903 : of the conversion case above, so the redundancy is detected
5904 : regardless of the order the two forms appear in the IL.
5905 : See PR124545. The narrow operation is only ever looked up,
5906 : never created: assuming no overflow is only valid for
5907 : operations the program actually executes, so the narrow
5908 : leader has to be available. Creating the narrow operation
5909 : instead is wrong-code, see PR126415. */
5910 14184367 : const tree narrow1 = ssa_integral_conversion_op (vn_valueize (rhs1));
5911 14184367 : if (!INTEGRAL_TYPE_P (type) || !narrow1)
5912 : break;
5913 1120945 : const tree ntype = TREE_TYPE (narrow1);
5914 : /* A sign-change keeps the value bit-identical; a widening is
5915 : only handled when the narrow operation cannot wrap. */
5916 1120945 : const bool sign_change_p
5917 1120945 : = TYPE_PRECISION (ntype) == TYPE_PRECISION (type);
5918 1120945 : const bool nowrap_widening_p
5919 1120945 : = (TYPE_PRECISION (ntype) < TYPE_PRECISION (type)
5920 1120945 : && TYPE_OVERFLOW_UNDEFINED (ntype));
5921 1120945 : if (!sign_change_p && !nowrap_widening_p)
5922 : break;
5923 : /* Determine the narrow variant of the second operand: a
5924 : constant that narrows and extends back unchanged, or a
5925 : conversion from the same narrow type. */
5926 853001 : const tree rhs2 = gimple_assign_rhs2 (stmt);
5927 853001 : tree narrow2 = NULL_TREE;
5928 853001 : if (TREE_CODE (rhs2) == INTEGER_CST)
5929 : {
5930 486767 : const widest_int cst = wi::to_widest (rhs2);
5931 486767 : const widest_int narrowed
5932 486767 : = wi::ext (cst, TYPE_PRECISION (ntype), TYPE_SIGN (ntype));
5933 486767 : const widest_int extended
5934 486767 : = wi::ext (narrowed, TYPE_PRECISION (type), TYPE_SIGN (type));
5935 486767 : if (cst == extended)
5936 483929 : narrow2 = fold_convert (ntype, rhs2);
5937 486773 : }
5938 366234 : else if (TREE_CODE (rhs2) == SSA_NAME)
5939 : {
5940 366234 : const tree op = ssa_integral_conversion_op (vn_valueize (rhs2));
5941 366234 : if (op && types_compatible_p (TREE_TYPE (op), ntype))
5942 : narrow2 = op;
5943 : }
5944 635077 : if (!narrow2)
5945 : break;
5946 632239 : tree ops[3] = { narrow1, narrow2 };
5947 632239 : const tree narrow_val
5948 632239 : = vn_nary_op_lookup_pieces (2, code, ntype, ops, NULL);
5949 : /* We have a narrower or sign-changed operation available. */
5950 632239 : if (narrow_val)
5951 : {
5952 21805 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5953 21805 : NOP_EXPR, type, narrow_val);
5954 21805 : result = vn_nary_build_or_lookup_avail (&match_op, narrow_val);
5955 21805 : if (result)
5956 : /* Do not insert (stmt, result) into the VN tables: result is
5957 : derived from narrow_val which is only valid where narrow_val
5958 : is available (and may have undefined overflow). Inserting
5959 : stmt globally would leak narrow_val to blocks not dominated
5960 : by it and allow PRE to hoist narrow_val across guards. */
5961 19389 : return set_ssa_val_to (lhs, result);
5962 : }
5963 : }
5964 612850 : break;
5965 1575993 : case BIT_AND_EXPR:
5966 1575993 : if (INTEGRAL_TYPE_P (type)
5967 1529998 : && TREE_CODE (rhs1) == SSA_NAME
5968 1529998 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5969 934855 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5970 934735 : && default_vn_walk_kind != VN_NOWALK
5971 : && CHAR_BIT == 8
5972 : && BITS_PER_UNIT == 8
5973 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5974 934526 : && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5975 934524 : && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5976 2510517 : && !integer_zerop (gimple_assign_rhs2 (stmt)))
5977 : {
5978 934524 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5979 682554 : if (ass
5980 682554 : && !gimple_has_volatile_ops (ass)
5981 680960 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5982 : {
5983 320210 : tree last_vuse = gimple_vuse (ass);
5984 320210 : tree op = gimple_assign_rhs1 (ass);
5985 960630 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5986 : default_vn_walk_kind,
5987 : NULL, true, &last_vuse,
5988 : gimple_assign_rhs2 (stmt));
5989 320210 : if (result
5990 320657 : && useless_type_conversion_p (TREE_TYPE (result),
5991 447 : TREE_TYPE (op)))
5992 447 : return set_ssa_val_to (lhs, result);
5993 : }
5994 : }
5995 : break;
5996 265378 : case BIT_FIELD_REF:
5997 265378 : if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
5998 : {
5999 265350 : tree op0 = vn_valueize (TREE_OPERAND (rhs1, 0));
6000 265350 : gassign *ass;
6001 265350 : if (TREE_CODE (op0) == SSA_NAME
6002 265350 : && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
6003 214887 : && !gimple_has_volatile_ops (ass)
6004 480154 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
6005 : {
6006 81808 : tree last_vuse = gimple_vuse (ass);
6007 81808 : tree op = gimple_assign_rhs1 (ass);
6008 : /* Avoid building invalid and unexpected refs. */
6009 81808 : if (TREE_CODE (op) != TARGET_MEM_REF
6010 : && TREE_CODE (op) != BIT_FIELD_REF
6011 : && TREE_CODE (op) != REALPART_EXPR
6012 : && TREE_CODE (op) != IMAGPART_EXPR)
6013 : {
6014 74895 : tree op = build3 (BIT_FIELD_REF, TREE_TYPE (rhs1),
6015 : gimple_assign_rhs1 (ass),
6016 74895 : TREE_OPERAND (rhs1, 1),
6017 74895 : TREE_OPERAND (rhs1, 2));
6018 149790 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
6019 : default_vn_walk_kind,
6020 : NULL, true, &last_vuse);
6021 74895 : if (result
6022 74895 : && useless_type_conversion_p (type, TREE_TYPE (result)))
6023 2106 : return set_ssa_val_to (lhs, result);
6024 73126 : else if (result
6025 337 : && TYPE_SIZE (type)
6026 337 : && TYPE_SIZE (TREE_TYPE (result))
6027 73463 : && operand_equal_p (TYPE_SIZE (type),
6028 337 : TYPE_SIZE (TREE_TYPE (result))))
6029 : {
6030 337 : gimple_match_op match_op (gimple_match_cond::UNCOND,
6031 337 : VIEW_CONVERT_EXPR,
6032 337 : type, result);
6033 337 : result = vn_nary_build_or_lookup (&match_op);
6034 337 : if (result)
6035 : {
6036 337 : bool changed = set_ssa_val_to (lhs, result);
6037 337 : if (TREE_CODE (result) == SSA_NAME)
6038 325 : vn_nary_op_insert_stmt (stmt, result);
6039 337 : return changed;
6040 : }
6041 : }
6042 : }
6043 : }
6044 : }
6045 : break;
6046 368595 : case TRUNC_DIV_EXPR:
6047 : /* (-a) / b == -(a / b) does not hold for INT_MIN. */
6048 1584 : if (!ANY_INTEGRAL_TYPE_P (type)
6049 370179 : || !TYPE_OVERFLOW_UNDEFINED (type))
6050 : break;
6051 : /* Fallthru. */
6052 5758850 : case RDIV_EXPR:
6053 5758850 : case MULT_EXPR:
6054 : /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
6055 5758850 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)
6056 5758850 : && (! COMPLEX_FLOAT_TYPE_P (type)
6057 65888 : || ! HONOR_SIGNED_ZEROS (type)))
6058 : {
6059 5729292 : tree rhs[2];
6060 5729292 : rhs[0] = rhs1;
6061 5729292 : rhs[1] = gimple_assign_rhs2 (stmt);
6062 : /* With undefined overflow the identity relies on -rhs[i] not
6063 : overflowing, which is only known where the negation is
6064 : executed. */
6065 1026441 : const bool overflow_undef_p = (ANY_INTEGRAL_TYPE_P (type)
6066 5779692 : && !TYPE_OVERFLOW_WRAPS (type));
6067 17181963 : for (unsigned i = 0; i <= 1; ++i)
6068 : {
6069 11458053 : unsigned j = i == 0 ? 1 : 0;
6070 11458053 : tree ops[2];
6071 11458053 : if (!overflow_undef_p)
6072 : {
6073 8077882 : gimple_match_op match_op (gimple_match_cond::UNCOND,
6074 8077882 : NEGATE_EXPR, type, rhs[i]);
6075 8077882 : ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
6076 : }
6077 : else
6078 : {
6079 : /* Do not simplify as if -rhs[i] were in the IL, instead
6080 : see if such an operation is available. */
6081 3380171 : ops[i] = vn_nary_op_lookup_pieces (1, NEGATE_EXPR, type,
6082 : &rhs[i], NULL);
6083 3380171 : if (ops[i]
6084 3391832 : && !(rpo_avail && vn_context_bb
6085 11661 : && rpo_avail->eliminate_avail (vn_context_bb,
6086 : ops[i])))
6087 911 : ops[i] = NULL_TREE;
6088 : }
6089 11458053 : ops[j] = rhs[j];
6090 11458053 : if (ops[i]
6091 11458053 : && (ops[0] = vn_nary_op_lookup_pieces (2, code,
6092 : type, ops, NULL)))
6093 : {
6094 5382 : gimple_match_op match_op (gimple_match_cond::UNCOND,
6095 5382 : NEGATE_EXPR, type, ops[0]);
6096 5382 : result = vn_nary_build_or_lookup_1 (&match_op, true, false);
6097 5382 : if (result)
6098 : {
6099 5382 : bool changed = set_ssa_val_to (lhs, result);
6100 : /* When the equivalence is only valid where the
6101 : negation ops[i] is executed do not record it for
6102 : the expression of stmt, that would make it
6103 : available to computations not dominated by
6104 : ops[i]. */
6105 5382 : if (!overflow_undef_p && TREE_CODE (result) == SSA_NAME)
6106 5361 : vn_nary_op_insert_stmt (stmt, result);
6107 5382 : return changed;
6108 : }
6109 : }
6110 : }
6111 : }
6112 : break;
6113 374894 : case LSHIFT_EXPR:
6114 : /* For X << C, use the value number of X * (1 << C). */
6115 374894 : if (INTEGRAL_TYPE_P (type)
6116 357760 : && TYPE_OVERFLOW_WRAPS (type)
6117 567117 : && !TYPE_SATURATING (type))
6118 : {
6119 192223 : tree rhs2 = gimple_assign_rhs2 (stmt);
6120 192223 : if (TREE_CODE (rhs2) == INTEGER_CST
6121 112784 : && tree_fits_uhwi_p (rhs2)
6122 305007 : && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
6123 : {
6124 225568 : wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
6125 112784 : TYPE_PRECISION (type));
6126 225568 : gimple_match_op match_op (gimple_match_cond::UNCOND,
6127 112784 : MULT_EXPR, type, rhs1,
6128 112784 : wide_int_to_tree (type, w));
6129 112784 : result = vn_nary_build_or_lookup (&match_op);
6130 112784 : if (result)
6131 : {
6132 112784 : bool changed = set_ssa_val_to (lhs, result);
6133 112784 : if (TREE_CODE (result) == SSA_NAME)
6134 112783 : vn_nary_op_insert_stmt (stmt, result);
6135 112784 : return changed;
6136 : }
6137 112784 : }
6138 : }
6139 : break;
6140 : default:
6141 : break;
6142 : }
6143 :
6144 46958452 : bool changed = set_ssa_val_to (lhs, lhs);
6145 46958452 : vn_nary_op_insert_stmt (stmt, lhs);
6146 46958452 : return changed;
6147 : }
6148 :
6149 : /* Visit a call STMT storing into LHS. Return true if the value number
6150 : of the LHS has changed as a result. */
6151 :
6152 : static bool
6153 8830208 : visit_reference_op_call (tree lhs, gcall *stmt)
6154 : {
6155 8830208 : bool changed = false;
6156 8830208 : struct vn_reference_s vr1;
6157 8830208 : vn_reference_t vnresult = NULL;
6158 8830208 : tree vdef = gimple_vdef (stmt);
6159 8830208 : modref_summary *summary;
6160 :
6161 : /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
6162 8830208 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
6163 4696860 : lhs = NULL_TREE;
6164 :
6165 8830208 : vn_reference_lookup_call (stmt, &vnresult, &vr1);
6166 :
6167 : /* If the lookup did not succeed for pure functions try to use
6168 : modref info to find a candidate to CSE to. */
6169 8830208 : const unsigned accesses_limit = 8;
6170 8830208 : if (!vnresult
6171 8161968 : && !vdef
6172 8161968 : && lhs
6173 2852679 : && gimple_vuse (stmt)
6174 10411242 : && (((summary = get_modref_function_summary (stmt, NULL))
6175 216448 : && !summary->global_memory_read
6176 84075 : && summary->load_accesses < accesses_limit)
6177 1497320 : || gimple_call_flags (stmt) & ECF_CONST))
6178 : {
6179 : /* First search if we can do something useful and build a
6180 : vector of all loads we have to check. */
6181 84514 : bool unknown_memory_access = false;
6182 84514 : auto_vec<ao_ref, accesses_limit> accesses;
6183 84514 : unsigned load_accesses = summary ? summary->load_accesses : 0;
6184 84514 : if (!unknown_memory_access)
6185 : /* Add loads done as part of setting up the call arguments.
6186 : That's also necessary for CONST functions which will
6187 : not have a modref summary. */
6188 258898 : for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
6189 : {
6190 174392 : tree arg = gimple_call_arg (stmt, i);
6191 174392 : if (TREE_CODE (arg) != SSA_NAME
6192 174392 : && !is_gimple_min_invariant (arg))
6193 : {
6194 64856 : if (accesses.length () >= accesses_limit - load_accesses)
6195 : {
6196 : unknown_memory_access = true;
6197 : break;
6198 : }
6199 32420 : accesses.quick_grow (accesses.length () + 1);
6200 32420 : ao_ref_init (&accesses.last (), arg);
6201 : }
6202 : }
6203 84514 : if (summary && !unknown_memory_access)
6204 : {
6205 : /* Add loads as analyzed by IPA modref. */
6206 301949 : for (auto base_node : summary->loads->bases)
6207 76930 : if (unknown_memory_access)
6208 : break;
6209 313857 : else for (auto ref_node : base_node->refs)
6210 84018 : if (unknown_memory_access)
6211 : break;
6212 352621 : else for (auto access_node : ref_node->accesses)
6213 : {
6214 236636 : accesses.quick_grow (accesses.length () + 1);
6215 118318 : ao_ref *r = &accesses.last ();
6216 118318 : if (!access_node.get_ao_ref (stmt, r))
6217 : {
6218 : /* Initialize a ref based on the argument and
6219 : unknown offset if possible. */
6220 17715 : tree arg = access_node.get_call_arg (stmt);
6221 17715 : if (arg && TREE_CODE (arg) == SSA_NAME)
6222 4237 : arg = SSA_VAL (arg);
6223 4237 : if (arg
6224 17705 : && TREE_CODE (arg) == ADDR_EXPR
6225 13474 : && (arg = get_base_address (arg))
6226 17711 : && DECL_P (arg))
6227 : {
6228 0 : ao_ref_init (r, arg);
6229 0 : r->ref = NULL_TREE;
6230 0 : r->base = arg;
6231 : }
6232 : else
6233 : {
6234 : unknown_memory_access = true;
6235 : break;
6236 : }
6237 : }
6238 100603 : r->base_alias_set = base_node->base;
6239 100603 : r->ref_alias_set = ref_node->ref;
6240 : }
6241 : }
6242 :
6243 : /* Walk the VUSE->VDEF chain optimistically trying to find an entry
6244 : for the call in the hashtable. */
6245 84514 : unsigned limit = (unknown_memory_access
6246 84514 : ? 0
6247 66791 : : (param_sccvn_max_alias_queries_per_access
6248 66791 : / (accesses.length () + 1)));
6249 84514 : tree saved_vuse = vr1.vuse;
6250 84514 : hashval_t saved_hashcode = vr1.hashcode;
6251 506028 : while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
6252 : {
6253 449316 : vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
6254 449316 : gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
6255 : /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
6256 : do not bother for now. */
6257 449316 : if (is_a <gphi *> (def))
6258 : break;
6259 843028 : vr1.vuse = vuse_ssa_val (gimple_vuse (def));
6260 421514 : vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
6261 421514 : vn_reference_lookup_1 (&vr1, &vnresult);
6262 421514 : limit--;
6263 : }
6264 :
6265 : /* If we found a candidate to CSE to verify it is valid. */
6266 84514 : if (vnresult && !accesses.is_empty ())
6267 : {
6268 1978 : tree vuse = vuse_ssa_val (gimple_vuse (stmt));
6269 7434 : while (vnresult && vuse != vr1.vuse)
6270 : {
6271 3478 : gimple *def = SSA_NAME_DEF_STMT (vuse);
6272 18710 : for (auto &ref : accesses)
6273 : {
6274 : /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
6275 : analysis overhead that we might be able to cache. */
6276 10036 : if (stmt_may_clobber_ref_p_1 (def, &ref, true))
6277 : {
6278 1760 : vnresult = NULL;
6279 1760 : break;
6280 : }
6281 : }
6282 6956 : vuse = vuse_ssa_val (gimple_vuse (def));
6283 : }
6284 : }
6285 84514 : vr1.vuse = saved_vuse;
6286 84514 : vr1.hashcode = saved_hashcode;
6287 84514 : }
6288 :
6289 8830208 : if (vnresult)
6290 : {
6291 668486 : if (vdef)
6292 : {
6293 177410 : if (vnresult->result_vdef)
6294 177410 : changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
6295 0 : else if (!lhs && gimple_call_lhs (stmt))
6296 : /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
6297 : as the call still acts as a lhs store. */
6298 0 : changed |= set_ssa_val_to (vdef, vdef);
6299 : else
6300 : /* If the call was discovered to be pure or const reflect
6301 : that as far as possible. */
6302 0 : changed |= set_ssa_val_to (vdef,
6303 : vuse_ssa_val (gimple_vuse (stmt)));
6304 : }
6305 :
6306 668486 : if (!vnresult->result && lhs)
6307 0 : vnresult->result = lhs;
6308 :
6309 668486 : if (vnresult->result && lhs)
6310 126360 : changed |= set_ssa_val_to (lhs, vnresult->result);
6311 : }
6312 : else
6313 : {
6314 8161722 : vn_reference_t vr2;
6315 8161722 : vn_reference_s **slot;
6316 8161722 : tree vdef_val = vdef;
6317 8161722 : if (vdef)
6318 : {
6319 : /* If we value numbered an indirect functions function to
6320 : one not clobbering memory value number its VDEF to its
6321 : VUSE. */
6322 4985195 : tree fn = gimple_call_fn (stmt);
6323 4985195 : if (fn && TREE_CODE (fn) == SSA_NAME)
6324 : {
6325 131891 : fn = SSA_VAL (fn);
6326 131891 : if (TREE_CODE (fn) == ADDR_EXPR
6327 2189 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6328 2189 : && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
6329 2189 : & (ECF_CONST | ECF_PURE))
6330 : /* If stmt has non-SSA_NAME lhs, value number the
6331 : vdef to itself, as the call still acts as a lhs
6332 : store. */
6333 133308 : && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
6334 2692 : vdef_val = vuse_ssa_val (gimple_vuse (stmt));
6335 : }
6336 4985195 : changed |= set_ssa_val_to (vdef, vdef_val);
6337 : }
6338 8161722 : if (lhs)
6339 4006988 : changed |= set_ssa_val_to (lhs, lhs);
6340 8161722 : vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
6341 8161722 : vr2->vuse = vr1.vuse;
6342 : /* As we are not walking the virtual operand chain we know the
6343 : shared_lookup_references are still original so we can re-use
6344 : them here. */
6345 8161722 : vr2->operands = vr1.operands.copy ();
6346 8161722 : vr2->type = vr1.type;
6347 8161722 : vr2->punned = vr1.punned;
6348 8161722 : vr2->set = vr1.set;
6349 8161722 : vr2->offset = vr1.offset;
6350 8161722 : vr2->max_size = vr1.max_size;
6351 8161722 : vr2->base_set = vr1.base_set;
6352 8161722 : vr2->hashcode = vr1.hashcode;
6353 8161722 : vr2->result = lhs;
6354 8161722 : vr2->result_vdef = vdef_val;
6355 8161722 : vr2->value_id = 0;
6356 8161722 : slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
6357 : INSERT);
6358 8161722 : gcc_assert (!*slot);
6359 8161722 : *slot = vr2;
6360 8161722 : vr2->next = last_inserted_ref;
6361 8161722 : last_inserted_ref = vr2;
6362 : }
6363 :
6364 8830208 : return changed;
6365 : }
6366 :
6367 : /* Visit a load from a reference operator RHS, part of STMT, value number it,
6368 : and return true if the value number of the LHS has changed as a result. */
6369 :
6370 : static bool
6371 35717649 : visit_reference_op_load (tree lhs, tree op, gimple *stmt)
6372 : {
6373 35717649 : bool changed = false;
6374 35717649 : tree result;
6375 35717649 : vn_reference_t res;
6376 :
6377 35717649 : tree vuse = gimple_vuse (stmt);
6378 35717649 : tree last_vuse = vuse;
6379 35717649 : result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
6380 :
6381 : /* We handle type-punning through unions by value-numbering based
6382 : on offset and size of the access. Be prepared to handle a
6383 : type-mismatch here via creating a VIEW_CONVERT_EXPR. */
6384 35717649 : if (result
6385 35717649 : && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
6386 : {
6387 19122 : if (CONSTANT_CLASS_P (result))
6388 4208 : result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6389 : else
6390 : {
6391 : /* We will be setting the value number of lhs to the value number
6392 : of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
6393 : So first simplify and lookup this expression to see if it
6394 : is already available. */
6395 14914 : gimple_match_op res_op (gimple_match_cond::UNCOND,
6396 14914 : VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6397 14914 : result = vn_nary_build_or_lookup (&res_op);
6398 14914 : if (result
6399 14908 : && TREE_CODE (result) == SSA_NAME
6400 28162 : && VN_INFO (result)->needs_insertion)
6401 : /* Track whether this is the canonical expression for different
6402 : typed loads. We use that as a stopgap measure for code
6403 : hoisting when dealing with floating point loads. */
6404 11333 : res->punned = true;
6405 : }
6406 :
6407 : /* When building the conversion fails avoid inserting the reference
6408 : again. */
6409 19122 : if (!result)
6410 6 : return set_ssa_val_to (lhs, lhs);
6411 : }
6412 :
6413 35698527 : if (result)
6414 5835071 : changed = set_ssa_val_to (lhs, result);
6415 : else
6416 : {
6417 29882572 : changed = set_ssa_val_to (lhs, lhs);
6418 29882572 : vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
6419 29882572 : if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
6420 : {
6421 9384507 : if (dump_file && (dump_flags & TDF_DETAILS))
6422 : {
6423 23858 : fprintf (dump_file, "Using extra use virtual operand ");
6424 23858 : print_generic_expr (dump_file, last_vuse);
6425 23858 : fprintf (dump_file, "\n");
6426 : }
6427 9384507 : vn_reference_insert (op, lhs, vuse, NULL_TREE);
6428 : }
6429 : }
6430 :
6431 : return changed;
6432 : }
6433 :
6434 :
6435 : /* Visit a store to a reference operator LHS, part of STMT, value number it,
6436 : and return true if the value number of the LHS has changed as a result. */
6437 :
6438 : static bool
6439 34214318 : visit_reference_op_store (tree lhs, tree op, gimple *stmt)
6440 : {
6441 34214318 : bool changed = false;
6442 34214318 : vn_reference_t vnresult = NULL;
6443 34214318 : tree assign;
6444 34214318 : bool resultsame = false;
6445 34214318 : tree vuse = gimple_vuse (stmt);
6446 34214318 : tree vdef = gimple_vdef (stmt);
6447 :
6448 34214318 : if (TREE_CODE (op) == SSA_NAME)
6449 15537837 : op = SSA_VAL (op);
6450 :
6451 : /* First we want to lookup using the *vuses* from the store and see
6452 : if there the last store to this location with the same address
6453 : had the same value.
6454 :
6455 : The vuses represent the memory state before the store. If the
6456 : memory state, address, and value of the store is the same as the
6457 : last store to this location, then this store will produce the
6458 : same memory state as that store.
6459 :
6460 : In this case the vdef versions for this store are value numbered to those
6461 : vuse versions, since they represent the same memory state after
6462 : this store.
6463 :
6464 : Otherwise, the vdefs for the store are used when inserting into
6465 : the table, since the store generates a new memory state. */
6466 :
6467 34214318 : vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
6468 34214318 : if (vnresult
6469 1741346 : && vnresult->result)
6470 : {
6471 1741346 : tree result = vnresult->result;
6472 1741346 : gcc_checking_assert (TREE_CODE (result) != SSA_NAME
6473 : || result == SSA_VAL (result));
6474 1741346 : resultsame = expressions_equal_p (result, op);
6475 1741346 : if (resultsame)
6476 : {
6477 : /* If the TBAA state isn't compatible for downstream reads
6478 : we cannot value-number the VDEFs the same. */
6479 55768 : ao_ref lhs_ref;
6480 55768 : ao_ref_init (&lhs_ref, lhs);
6481 55768 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
6482 55768 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
6483 55768 : if ((vnresult->set != set
6484 958 : && ! alias_set_subset_of (set, vnresult->set))
6485 56440 : || (vnresult->base_set != base_set
6486 8430 : && ! alias_set_subset_of (base_set, vnresult->base_set)))
6487 2700 : resultsame = false;
6488 : }
6489 : }
6490 :
6491 2700 : if (!resultsame)
6492 : {
6493 34161250 : if (dump_file && (dump_flags & TDF_DETAILS))
6494 : {
6495 20402 : fprintf (dump_file, "No store match\n");
6496 20402 : fprintf (dump_file, "Value numbering store ");
6497 20402 : print_generic_expr (dump_file, lhs);
6498 20402 : fprintf (dump_file, " to ");
6499 20402 : print_generic_expr (dump_file, op);
6500 20402 : fprintf (dump_file, "\n");
6501 : }
6502 : /* Have to set value numbers before insert, since insert is
6503 : going to valueize the references in-place. */
6504 34161250 : if (vdef)
6505 34161250 : changed |= set_ssa_val_to (vdef, vdef);
6506 :
6507 : /* Do not insert structure copies into the tables. */
6508 34161250 : if (is_gimple_min_invariant (op)
6509 34161250 : || is_gimple_reg (op))
6510 30457709 : vn_reference_insert (lhs, op, vdef, NULL);
6511 :
6512 : /* Only perform the following when being called from PRE
6513 : which embeds tail merging. */
6514 34161250 : if (default_vn_walk_kind == VN_WALK)
6515 : {
6516 7720535 : assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
6517 7720535 : vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
6518 7720535 : if (!vnresult)
6519 7678800 : vn_reference_insert (assign, lhs, vuse, vdef);
6520 : }
6521 : }
6522 : else
6523 : {
6524 : /* We had a match, so value number the vdef to have the value
6525 : number of the vuse it came from. */
6526 :
6527 53068 : if (dump_file && (dump_flags & TDF_DETAILS))
6528 9 : fprintf (dump_file, "Store matched earlier value, "
6529 : "value numbering store vdefs to matching vuses.\n");
6530 :
6531 53068 : changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
6532 : }
6533 :
6534 34214318 : return changed;
6535 : }
6536 :
6537 : /* Visit and value number PHI, return true if the value number
6538 : changed. When BACKEDGES_VARYING_P is true then assume all
6539 : backedge values are varying. When INSERTED is not NULL then
6540 : this is just a ahead query for a possible iteration, set INSERTED
6541 : to true if we'd insert into the hashtable. */
6542 :
6543 : static bool
6544 35561018 : visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
6545 : {
6546 35561018 : tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
6547 35561018 : bool seen_undef_visited = false;
6548 35561018 : tree backedge_val = NULL_TREE;
6549 35561018 : bool seen_non_backedge = false;
6550 35561018 : tree sameval_base = NULL_TREE;
6551 35561018 : poly_int64 soff, doff;
6552 35561018 : unsigned n_executable = 0;
6553 35561018 : edge sameval_e = NULL;
6554 :
6555 : /* TODO: We could check for this in initialization, and replace this
6556 : with a gcc_assert. */
6557 35561018 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
6558 31794 : return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
6559 :
6560 : /* We track whether a PHI was CSEd to avoid excessive iterations
6561 : that would be necessary only because the PHI changed arguments
6562 : but not value. */
6563 35529224 : if (!inserted)
6564 27837869 : gimple_set_plf (phi, GF_PLF_1, false);
6565 :
6566 35529224 : basic_block bb = gimple_bb (phi);
6567 :
6568 : /* For the equivalence handling below make sure to first process an
6569 : edge with a non-constant. */
6570 35529224 : auto_vec<edge, 2> preds;
6571 71058448 : preds.reserve_exact (EDGE_COUNT (bb->preds));
6572 35529224 : bool seen_nonconstant = false;
6573 151797068 : for (unsigned i = 0; i < EDGE_COUNT (bb->preds); ++i)
6574 : {
6575 80738620 : edge e = EDGE_PRED (bb, i);
6576 80738620 : preds.quick_push (e);
6577 80738620 : if (!seen_nonconstant)
6578 : {
6579 43453530 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6580 43453530 : if (TREE_CODE (def) == SSA_NAME)
6581 : {
6582 33722441 : seen_nonconstant = true;
6583 33722441 : if (i != 0)
6584 5890577 : std::swap (preds[0], preds[i]);
6585 : }
6586 : }
6587 : }
6588 :
6589 : /* See if all non-TOP arguments have the same value. TOP is
6590 : equivalent to everything, so we can ignore it. */
6591 149071165 : for (edge e : preds)
6592 70051494 : if (e->flags & EDGE_EXECUTABLE)
6593 : {
6594 65164525 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6595 :
6596 65164525 : if (def == PHI_RESULT (phi))
6597 154840 : continue;
6598 65034972 : ++n_executable;
6599 65034972 : bool visited = true;
6600 65034972 : if (TREE_CODE (def) == SSA_NAME)
6601 : {
6602 52464260 : tree val = SSA_VAL (def, &visited);
6603 52464260 : if (SSA_NAME_IS_DEFAULT_DEF (def))
6604 2737964 : visited = true;
6605 52464260 : if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6606 49806425 : def = val;
6607 52464260 : if (e->flags & EDGE_DFS_BACK)
6608 15940586 : backedge_val = def;
6609 : }
6610 65034972 : if (!(e->flags & EDGE_DFS_BACK))
6611 48943211 : seen_non_backedge = true;
6612 65034972 : if (def == VN_TOP)
6613 : ;
6614 : /* Ignore undefined defs for sameval but record one. */
6615 65034972 : else if (TREE_CODE (def) == SSA_NAME
6616 48949385 : && ! virtual_operand_p (def)
6617 90006192 : && ssa_undefined_value_p (def, false))
6618 : {
6619 240615 : if (!seen_undef
6620 : /* Avoid having not visited undefined defs if we also have
6621 : a visited one. */
6622 37374 : || (!seen_undef_visited && visited))
6623 : {
6624 203245 : seen_undef = def;
6625 203245 : seen_undef_visited = visited;
6626 : }
6627 : }
6628 64794357 : else if (sameval == VN_TOP)
6629 : {
6630 : sameval = def;
6631 : sameval_e = e;
6632 : }
6633 29312097 : else if (expressions_equal_p (def, sameval))
6634 : sameval_e = NULL;
6635 46056701 : else if (virtual_operand_p (def))
6636 : {
6637 : sameval = NULL_TREE;
6638 27568001 : break;
6639 : }
6640 : else
6641 : {
6642 : /* We know we're arriving only with invariant addresses here,
6643 : try harder comparing them. We can do some caching here
6644 : which we cannot do in expressions_equal_p. */
6645 17274975 : if (TREE_CODE (def) == ADDR_EXPR
6646 392122 : && TREE_CODE (sameval) == ADDR_EXPR
6647 109678 : && sameval_base != (void *)-1)
6648 : {
6649 109678 : if (!sameval_base)
6650 109676 : sameval_base = get_addr_base_and_unit_offset
6651 109676 : (TREE_OPERAND (sameval, 0), &soff);
6652 109676 : if (!sameval_base)
6653 : sameval_base = (tree)(void *)-1;
6654 109683 : else if ((get_addr_base_and_unit_offset
6655 109678 : (TREE_OPERAND (def, 0), &doff) == sameval_base)
6656 109678 : && known_eq (soff, doff))
6657 5 : continue;
6658 : }
6659 : /* There's also the possibility to use equivalences. */
6660 33444111 : if (!FLOAT_TYPE_P (TREE_TYPE (def))
6661 : /* But only do this if we didn't force any of sameval or
6662 : val to VARYING because of backedge processing rules. */
6663 16062296 : && (TREE_CODE (sameval) != SSA_NAME
6664 12690237 : || SSA_VAL (sameval) == sameval)
6665 33331151 : && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6666 : {
6667 16054051 : vn_nary_op_t vnresult;
6668 16054051 : tree ops[2];
6669 16054051 : ops[0] = def;
6670 16054051 : ops[1] = sameval;
6671 : /* Canonicalize the operands order for eq below. */
6672 16054051 : if (tree_swap_operands_p (ops[0], ops[1]))
6673 9555864 : std::swap (ops[0], ops[1]);
6674 16054051 : tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6675 : boolean_type_node,
6676 : ops, &vnresult);
6677 16054051 : if (! val && vnresult && vnresult->predicated_values)
6678 : {
6679 207017 : val = vn_nary_op_get_predicated_value (vnresult, e);
6680 113595 : if (val && integer_truep (val)
6681 232419 : && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6682 : {
6683 25282 : if (dump_file && (dump_flags & TDF_DETAILS))
6684 : {
6685 2 : fprintf (dump_file, "Predication says ");
6686 2 : print_generic_expr (dump_file, def, TDF_NONE);
6687 2 : fprintf (dump_file, " and ");
6688 2 : print_generic_expr (dump_file, sameval, TDF_NONE);
6689 2 : fprintf (dump_file, " are equal on edge %d -> %d\n",
6690 2 : e->src->index, e->dest->index);
6691 : }
6692 25282 : continue;
6693 : }
6694 : }
6695 : }
6696 : sameval = NULL_TREE;
6697 : break;
6698 : }
6699 : }
6700 :
6701 : /* If the value we want to use is flowing over the backedge and we
6702 : should take it as VARYING but it has a non-VARYING value drop to
6703 : VARYING.
6704 : If we value-number a virtual operand never value-number to the
6705 : value from the backedge as that confuses the alias-walking code.
6706 : See gcc.dg/torture/pr87176.c. If the value is the same on a
6707 : non-backedge everything is OK though. */
6708 35529224 : bool visited_p;
6709 35529224 : if ((backedge_val
6710 35529224 : && !seen_non_backedge
6711 706 : && TREE_CODE (backedge_val) == SSA_NAME
6712 495 : && sameval == backedge_val
6713 328 : && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6714 46 : || SSA_VAL (backedge_val) != backedge_val))
6715 : /* Do not value-number a virtual operand to sth not visited though
6716 : given that allows us to escape a region in alias walking. */
6717 35529648 : || (sameval
6718 7960941 : && TREE_CODE (sameval) == SSA_NAME
6719 4731834 : && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6720 4008839 : && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6721 2029046 : && (SSA_VAL (sameval, &visited_p), !visited_p)))
6722 : /* Note this just drops to VARYING without inserting the PHI into
6723 : the hashes. */
6724 308666 : result = PHI_RESULT (phi);
6725 : /* If none of the edges was executable keep the value-number at VN_TOP,
6726 : if only a single edge is executable use its value. */
6727 35220558 : else if (n_executable <= 1)
6728 6859723 : result = seen_undef ? seen_undef : sameval;
6729 : /* If we saw only undefined values and VN_TOP use one of the
6730 : undefined values. */
6731 28360835 : else if (sameval == VN_TOP)
6732 5043 : result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6733 : /* First see if it is equivalent to a phi node in this block. We prefer
6734 : this as it allows IV elimination - see PRs 66502 and 67167. */
6735 28355792 : else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6736 : {
6737 4234895 : if (!inserted
6738 71284 : && TREE_CODE (result) == SSA_NAME
6739 4306179 : && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6740 : {
6741 71284 : gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6742 71284 : if (dump_file && (dump_flags & TDF_DETAILS))
6743 : {
6744 6 : fprintf (dump_file, "Marking CSEd to PHI node ");
6745 6 : print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6746 : 0, TDF_SLIM);
6747 6 : fprintf (dump_file, "\n");
6748 : }
6749 : }
6750 : }
6751 : /* If all values are the same use that, unless we've seen undefined
6752 : values as well and the value isn't constant.
6753 : CCP/copyprop have the same restriction to not remove uninit warnings. */
6754 24120897 : else if (sameval
6755 24120897 : && (! seen_undef || is_gimple_min_invariant (sameval)))
6756 : result = sameval;
6757 : else
6758 : {
6759 23479409 : result = PHI_RESULT (phi);
6760 : /* Only insert PHIs that are varying, for constant value numbers
6761 : we mess up equivalences otherwise as we are only comparing
6762 : the immediate controlling predicates. */
6763 23479409 : vn_phi_insert (phi, result, backedges_varying_p);
6764 23479409 : if (inserted)
6765 3395172 : *inserted = true;
6766 : }
6767 :
6768 35529224 : return set_ssa_val_to (PHI_RESULT (phi), result);
6769 35529224 : }
6770 :
6771 : /* Try to simplify RHS using equivalences and constant folding. */
6772 :
6773 : static tree
6774 132149234 : try_to_simplify (gassign *stmt)
6775 : {
6776 132149234 : enum tree_code code = gimple_assign_rhs_code (stmt);
6777 132149234 : tree tem;
6778 :
6779 : /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6780 : in this case, there is no point in doing extra work. */
6781 132149234 : if (code == SSA_NAME)
6782 : return NULL_TREE;
6783 :
6784 : /* First try constant folding based on our current lattice. */
6785 116611055 : mprts_hook = vn_lookup_simplify_result;
6786 116611055 : tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6787 116611055 : mprts_hook = NULL;
6788 116611055 : if (tem
6789 116611055 : && (TREE_CODE (tem) == SSA_NAME
6790 25986239 : || is_gimple_min_invariant (tem)))
6791 26077951 : return tem;
6792 :
6793 : return NULL_TREE;
6794 : }
6795 :
6796 : /* Visit and value number STMT, return true if the value number
6797 : changed. */
6798 :
6799 : static bool
6800 481987026 : visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6801 : {
6802 481987026 : bool changed = false;
6803 :
6804 481987026 : if (dump_file && (dump_flags & TDF_DETAILS))
6805 : {
6806 432011 : fprintf (dump_file, "Value numbering stmt = ");
6807 432011 : print_gimple_stmt (dump_file, stmt, 0);
6808 : }
6809 :
6810 481987026 : if (gimple_code (stmt) == GIMPLE_PHI)
6811 27859772 : changed = visit_phi (stmt, NULL, backedges_varying_p);
6812 632595714 : else if (gimple_has_volatile_ops (stmt))
6813 9682400 : changed = defs_to_varying (stmt);
6814 444444854 : else if (gassign *ass = dyn_cast <gassign *> (stmt))
6815 : {
6816 137369655 : enum tree_code code = gimple_assign_rhs_code (ass);
6817 137369655 : tree lhs = gimple_assign_lhs (ass);
6818 137369655 : tree rhs1 = gimple_assign_rhs1 (ass);
6819 137369655 : tree simplified;
6820 :
6821 : /* Shortcut for copies. Simplifying copies is pointless,
6822 : since we copy the expression and value they represent. */
6823 137369655 : if (code == SSA_NAME
6824 20758600 : && TREE_CODE (lhs) == SSA_NAME)
6825 : {
6826 5220421 : changed = visit_copy (lhs, rhs1);
6827 5220421 : goto done;
6828 : }
6829 132149234 : simplified = try_to_simplify (ass);
6830 132149234 : if (simplified)
6831 : {
6832 26077951 : if (dump_file && (dump_flags & TDF_DETAILS))
6833 : {
6834 14574 : fprintf (dump_file, "RHS ");
6835 14574 : print_gimple_expr (dump_file, ass, 0);
6836 14574 : fprintf (dump_file, " simplified to ");
6837 14574 : print_generic_expr (dump_file, simplified);
6838 14574 : fprintf (dump_file, "\n");
6839 : }
6840 : }
6841 : /* Setting value numbers to constants will occasionally
6842 : screw up phi congruence because constants are not
6843 : uniquely associated with a single ssa name that can be
6844 : looked up. */
6845 26077951 : if (simplified
6846 26077951 : && is_gimple_min_invariant (simplified)
6847 22997932 : && TREE_CODE (lhs) == SSA_NAME)
6848 : {
6849 8019091 : changed = set_ssa_val_to (lhs, simplified);
6850 8019091 : goto done;
6851 : }
6852 124130143 : else if (simplified
6853 18058860 : && TREE_CODE (simplified) == SSA_NAME
6854 3080019 : && TREE_CODE (lhs) == SSA_NAME)
6855 : {
6856 3080019 : changed = visit_copy (lhs, simplified);
6857 3080019 : goto done;
6858 : }
6859 :
6860 121050124 : if ((TREE_CODE (lhs) == SSA_NAME
6861 : /* We can substitute SSA_NAMEs that are live over
6862 : abnormal edges with their constant value. */
6863 86835498 : && !(gimple_assign_copy_p (ass)
6864 34 : && is_gimple_min_invariant (rhs1))
6865 86835464 : && !(simplified
6866 0 : && is_gimple_min_invariant (simplified))
6867 86835464 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6868 : /* Stores or copies from SSA_NAMEs that are live over
6869 : abnormal edges are a problem. */
6870 207884178 : || (code == SSA_NAME
6871 15538179 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6872 1752 : changed = defs_to_varying (ass);
6873 121048372 : else if (REFERENCE_CLASS_P (lhs)
6874 121048372 : || DECL_P (lhs))
6875 34214318 : changed = visit_reference_op_store (lhs, rhs1, ass);
6876 86834054 : else if (TREE_CODE (lhs) == SSA_NAME)
6877 : {
6878 86834054 : if ((gimple_assign_copy_p (ass)
6879 34 : && is_gimple_min_invariant (rhs1))
6880 86834088 : || (simplified
6881 0 : && is_gimple_min_invariant (simplified)))
6882 : {
6883 0 : if (simplified)
6884 0 : changed = set_ssa_val_to (lhs, simplified);
6885 : else
6886 0 : changed = set_ssa_val_to (lhs, rhs1);
6887 : }
6888 : else
6889 : {
6890 : /* Visit the original statement. */
6891 86834054 : switch (vn_get_stmt_kind (ass))
6892 : {
6893 51013698 : case VN_NARY:
6894 51013698 : changed = visit_nary_op (lhs, ass);
6895 51013698 : break;
6896 35717649 : case VN_REFERENCE:
6897 35717649 : changed = visit_reference_op_load (lhs, rhs1, ass);
6898 35717649 : break;
6899 102707 : default:
6900 102707 : changed = defs_to_varying (ass);
6901 102707 : break;
6902 : }
6903 : }
6904 : }
6905 : else
6906 0 : changed = defs_to_varying (ass);
6907 : }
6908 307075199 : else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6909 : {
6910 25595055 : tree lhs = gimple_call_lhs (call_stmt);
6911 25595055 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6912 : {
6913 : /* Try constant folding based on our current lattice. */
6914 8635651 : tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6915 : vn_valueize);
6916 8635651 : if (simplified)
6917 : {
6918 68922 : if (dump_file && (dump_flags & TDF_DETAILS))
6919 : {
6920 1 : fprintf (dump_file, "call ");
6921 1 : print_gimple_expr (dump_file, call_stmt, 0);
6922 1 : fprintf (dump_file, " simplified to ");
6923 1 : print_generic_expr (dump_file, simplified);
6924 1 : fprintf (dump_file, "\n");
6925 : }
6926 : }
6927 : /* Setting value numbers to constants will occasionally
6928 : screw up phi congruence because constants are not
6929 : uniquely associated with a single ssa name that can be
6930 : looked up. */
6931 68922 : if (simplified
6932 68922 : && is_gimple_min_invariant (simplified))
6933 : {
6934 62349 : changed = set_ssa_val_to (lhs, simplified);
6935 124698 : if (gimple_vdef (call_stmt))
6936 751 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6937 : SSA_VAL (gimple_vuse (call_stmt)));
6938 62349 : goto done;
6939 : }
6940 8573302 : else if (simplified
6941 6573 : && TREE_CODE (simplified) == SSA_NAME)
6942 : {
6943 309 : changed = visit_copy (lhs, simplified);
6944 618 : if (gimple_vdef (call_stmt))
6945 0 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6946 : SSA_VAL (gimple_vuse (call_stmt)));
6947 309 : goto done;
6948 : }
6949 8572993 : else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6950 : {
6951 414 : changed = defs_to_varying (call_stmt);
6952 414 : goto done;
6953 : }
6954 : }
6955 :
6956 : /* Pick up flags from a devirtualization target. */
6957 25531983 : tree fn = gimple_call_fn (stmt);
6958 25531983 : int extra_fnflags = 0;
6959 25531983 : if (fn && TREE_CODE (fn) == SSA_NAME)
6960 : {
6961 548214 : fn = SSA_VAL (fn);
6962 548214 : if (TREE_CODE (fn) == ADDR_EXPR
6963 548214 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6964 5905 : extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6965 : }
6966 25531983 : if ((/* Calls to the same function with the same vuse
6967 : and the same operands do not necessarily return the same
6968 : value, unless they're pure or const. */
6969 25531983 : ((gimple_call_flags (call_stmt) | extra_fnflags)
6970 25531983 : & (ECF_PURE | ECF_CONST))
6971 : /* If calls have a vdef, subsequent calls won't have
6972 : the same incoming vuse. So, if 2 calls with vdef have the
6973 : same vuse, we know they're not subsequent.
6974 : We can value number 2 calls to the same function with the
6975 : same vuse and the same operands which are not subsequent
6976 : the same, because there is no code in the program that can
6977 : compare the 2 values... */
6978 21525386 : || (gimple_vdef (call_stmt)
6979 : /* ... unless the call returns a pointer which does
6980 : not alias with anything else. In which case the
6981 : information that the values are distinct are encoded
6982 : in the IL. */
6983 21485305 : && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6984 : /* Only perform the following when being called from PRE
6985 : which embeds tail merging. */
6986 20900640 : && default_vn_walk_kind == VN_WALK))
6987 : /* Do not process .DEFERRED_INIT since that confuses uninit
6988 : analysis. */
6989 30632015 : && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6990 8830208 : changed = visit_reference_op_call (lhs, call_stmt);
6991 : else
6992 16701775 : changed = defs_to_varying (call_stmt);
6993 : }
6994 : else
6995 281480144 : changed = defs_to_varying (stmt);
6996 481987026 : done:
6997 481987026 : return changed;
6998 : }
6999 :
7000 :
7001 : /* Allocate a value number table. */
7002 :
7003 : static void
7004 6335585 : allocate_vn_table (vn_tables_t table, unsigned size)
7005 : {
7006 6335585 : table->phis = new vn_phi_table_type (size);
7007 6335585 : table->nary = new vn_nary_op_table_type (size);
7008 6335585 : table->references = new vn_reference_table_type (size);
7009 6335585 : table->inserted_exprs = NULL;
7010 6335585 : }
7011 :
7012 : /* Free a value number table. */
7013 :
7014 : static void
7015 6335585 : free_vn_table (vn_tables_t table)
7016 : {
7017 : /* Walk over elements and release vectors. */
7018 6335585 : vn_reference_iterator_type hir;
7019 6335585 : vn_reference_t vr;
7020 79145342 : FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
7021 72809757 : vr->operands.release ();
7022 6335585 : delete table->phis;
7023 6335585 : table->phis = NULL;
7024 6335585 : delete table->nary;
7025 6335585 : table->nary = NULL;
7026 6335585 : delete table->references;
7027 6335585 : table->references = NULL;
7028 6335585 : delete table->inserted_exprs;
7029 6335585 : table->inserted_exprs = NULL;
7030 6335585 : }
7031 :
7032 : /* Set *ID according to RESULT. */
7033 :
7034 : static void
7035 35564993 : set_value_id_for_result (tree result, unsigned int *id)
7036 : {
7037 35564993 : if (result && TREE_CODE (result) == SSA_NAME)
7038 22156843 : *id = VN_INFO (result)->value_id;
7039 10038951 : else if (result && is_gimple_min_invariant (result))
7040 3795396 : *id = get_or_alloc_constant_value_id (result);
7041 : else
7042 9612754 : *id = get_next_value_id ();
7043 35564993 : }
7044 :
7045 : /* Set the value ids in the valid hash tables. */
7046 :
7047 : static void
7048 987733 : set_hashtable_value_ids (void)
7049 : {
7050 987733 : vn_nary_op_iterator_type hin;
7051 987733 : vn_phi_iterator_type hip;
7052 987733 : vn_reference_iterator_type hir;
7053 987733 : vn_nary_op_t vno;
7054 987733 : vn_reference_t vr;
7055 987733 : vn_phi_t vp;
7056 :
7057 : /* Now set the value ids of the things we had put in the hash
7058 : table. */
7059 :
7060 25411668 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
7061 24423935 : if (! vno->predicated_values)
7062 8034966 : set_value_id_for_result (vno->u.result, &vno->value_id);
7063 :
7064 5125473 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
7065 4137740 : set_value_id_for_result (vp->result, &vp->value_id);
7066 :
7067 24380020 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
7068 : hir)
7069 23392287 : set_value_id_for_result (vr->result, &vr->value_id);
7070 987733 : }
7071 :
7072 : /* Return the maximum value id we have ever seen. */
7073 :
7074 : unsigned int
7075 1975466 : get_max_value_id (void)
7076 : {
7077 1975466 : return next_value_id;
7078 : }
7079 :
7080 : /* Return the maximum constant value id we have ever seen. */
7081 :
7082 : unsigned int
7083 1975466 : get_max_constant_value_id (void)
7084 : {
7085 1975466 : return -next_constant_value_id;
7086 : }
7087 :
7088 : /* Return the next unique value id. */
7089 :
7090 : unsigned int
7091 50730517 : get_next_value_id (void)
7092 : {
7093 50730517 : gcc_checking_assert ((int)next_value_id > 0);
7094 50730517 : return next_value_id++;
7095 : }
7096 :
7097 : /* Return the next unique value id for constants. */
7098 :
7099 : unsigned int
7100 2611803 : get_next_constant_value_id (void)
7101 : {
7102 2611803 : gcc_checking_assert (next_constant_value_id < 0);
7103 2611803 : return next_constant_value_id--;
7104 : }
7105 :
7106 :
7107 : /* Compare two expressions E1 and E2 and return true if they are equal.
7108 : If match_vn_top_optimistically is true then VN_TOP is equal to anything,
7109 : otherwise VN_TOP only matches VN_TOP. */
7110 :
7111 : bool
7112 253922258 : expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
7113 : {
7114 : /* The obvious case. */
7115 253922258 : if (e1 == e2)
7116 : return true;
7117 :
7118 : /* If either one is VN_TOP consider them equal. */
7119 72309473 : if (match_vn_top_optimistically
7120 67354171 : && (e1 == VN_TOP || e2 == VN_TOP))
7121 : return true;
7122 :
7123 : /* If only one of them is null, they cannot be equal. While in general
7124 : this should not happen for operations like TARGET_MEM_REF some
7125 : operands are optional and an identity value we could substitute
7126 : has differing semantics. */
7127 72309473 : if (!e1 || !e2)
7128 : return false;
7129 :
7130 : /* SSA_NAME compare pointer equal. */
7131 72309473 : if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
7132 : return false;
7133 :
7134 : /* Now perform the actual comparison. */
7135 36239109 : if (TREE_CODE (e1) == TREE_CODE (e2)
7136 36239109 : && operand_equal_p (e1, e2, OEP_PURE_SAME))
7137 : return true;
7138 :
7139 : return false;
7140 : }
7141 :
7142 :
7143 : /* Return true if the nary operation NARY may trap. This is a copy
7144 : of stmt_could_throw_1_p adjusted to the SCCVN IL. */
7145 :
7146 : bool
7147 5771783 : vn_nary_may_trap (vn_nary_op_t nary)
7148 : {
7149 5771783 : tree type;
7150 5771783 : tree rhs2 = NULL_TREE;
7151 5771783 : bool honor_nans = false;
7152 5771783 : bool honor_snans = false;
7153 5771783 : bool fp_operation = false;
7154 5771783 : bool honor_trapv = false;
7155 5771783 : bool handled, ret;
7156 5771783 : unsigned i;
7157 :
7158 5771783 : if (nary->opcode == COND_EXPR || nary->opcode == VEC_COND_EXPR)
7159 : return false;
7160 :
7161 5765951 : if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
7162 : || TREE_CODE_CLASS (nary->opcode) == tcc_unary
7163 5765951 : || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
7164 : {
7165 5650137 : if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
7166 5503061 : || nary->opcode == FIX_TRUNC_EXPR)
7167 156686 : type = TREE_TYPE (nary->op[0]);
7168 : else
7169 5493451 : type = nary->type;
7170 5650137 : fp_operation = FLOAT_TYPE_P (type);
7171 5499466 : if (fp_operation)
7172 : {
7173 150671 : honor_nans = flag_trapping_math && !flag_finite_math_only;
7174 150671 : honor_snans = flag_signaling_nans != 0;
7175 : }
7176 5499466 : else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
7177 : honor_trapv = true;
7178 : }
7179 5765951 : if (nary->length >= 2)
7180 2331872 : rhs2 = nary->op[1];
7181 5765951 : ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
7182 : honor_trapv, honor_nans, honor_snans,
7183 : rhs2, &handled);
7184 5765951 : if (handled && ret)
7185 : return true;
7186 :
7187 13538069 : for (i = 0; i < nary->length; ++i)
7188 7896097 : if (tree_could_trap_p (nary->op[i]))
7189 : return true;
7190 :
7191 : return false;
7192 : }
7193 :
7194 : /* Return true if the reference operation REF may trap. */
7195 :
7196 : bool
7197 954723 : vn_reference_may_trap (vn_reference_t ref)
7198 : {
7199 954723 : switch (ref->operands[0].opcode)
7200 : {
7201 : case MODIFY_EXPR:
7202 : case CALL_EXPR:
7203 : /* We do not handle calls. */
7204 : return true;
7205 : case ADDR_EXPR:
7206 : /* And toplevel address computations never trap. */
7207 : return false;
7208 : default:;
7209 : }
7210 :
7211 : vn_reference_op_t op;
7212 : unsigned i;
7213 2613632 : FOR_EACH_VEC_ELT (ref->operands, i, op)
7214 : {
7215 2613376 : switch (op->opcode)
7216 : {
7217 : case WITH_SIZE_EXPR:
7218 : case TARGET_MEM_REF:
7219 : /* Always variable. */
7220 : return true;
7221 714989 : case COMPONENT_REF:
7222 714989 : if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
7223 : return true;
7224 : break;
7225 0 : case ARRAY_RANGE_REF:
7226 0 : if (TREE_CODE (op->op0) == SSA_NAME)
7227 : return true;
7228 : break;
7229 213292 : case ARRAY_REF:
7230 213292 : {
7231 213292 : if (TREE_CODE (op->op0) != INTEGER_CST)
7232 : return true;
7233 :
7234 : /* !in_array_bounds */
7235 191493 : tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
7236 191493 : if (!domain_type)
7237 : return true;
7238 :
7239 191447 : tree min = op->op1;
7240 191447 : tree max = TYPE_MAX_VALUE (domain_type);
7241 191447 : if (!min
7242 191447 : || !max
7243 178144 : || TREE_CODE (min) != INTEGER_CST
7244 178144 : || TREE_CODE (max) != INTEGER_CST)
7245 : return true;
7246 :
7247 175168 : if (tree_int_cst_lt (op->op0, min)
7248 175168 : || tree_int_cst_lt (max, op->op0))
7249 : return true;
7250 :
7251 : break;
7252 : }
7253 : case MEM_REF:
7254 : /* Nothing interesting in itself, the base is separate. */
7255 : break;
7256 : /* The following are the address bases. */
7257 : case SSA_NAME:
7258 : return true;
7259 544095 : case ADDR_EXPR:
7260 544095 : if (op->op0)
7261 544095 : return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
7262 : return false;
7263 1745846 : default:;
7264 : }
7265 : }
7266 : return false;
7267 : }
7268 :
7269 10727937 : eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
7270 : bitmap inserted_exprs_)
7271 10727937 : : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
7272 10727937 : el_todo (0), eliminations (0), insertions (0),
7273 10727937 : inserted_exprs (inserted_exprs_)
7274 : {
7275 10727937 : need_eh_cleanup = BITMAP_ALLOC (NULL);
7276 10727937 : need_ab_cleanup = BITMAP_ALLOC (NULL);
7277 10727937 : }
7278 :
7279 14132556 : eliminate_dom_walker::~eliminate_dom_walker ()
7280 : {
7281 10727937 : BITMAP_FREE (need_eh_cleanup);
7282 10727937 : BITMAP_FREE (need_ab_cleanup);
7283 14132556 : }
7284 :
7285 : /* Return a leader for OP that is available at the current point of the
7286 : eliminate domwalk. */
7287 :
7288 : tree
7289 187836371 : eliminate_dom_walker::eliminate_avail (basic_block, tree op)
7290 : {
7291 187836371 : tree valnum = VN_INFO (op)->valnum;
7292 187836371 : if (TREE_CODE (valnum) == SSA_NAME)
7293 : {
7294 182560095 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
7295 : return valnum;
7296 317985488 : if (avail.length () > SSA_NAME_VERSION (valnum))
7297 : {
7298 143246584 : tree av = avail[SSA_NAME_VERSION (valnum)];
7299 : /* When PRE discovers a new redundancy there's no way to unite
7300 : the value classes so it instead inserts a copy old-val = new-val.
7301 : Look through such copies here, providing one more level of
7302 : simplification at elimination time. */
7303 143246584 : gassign *ass;
7304 251744897 : if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
7305 77195117 : if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
7306 : {
7307 40717004 : tree rhs1 = gimple_assign_rhs1 (ass);
7308 40717004 : if (CONSTANT_CLASS_P (rhs1)
7309 40717004 : || (TREE_CODE (rhs1) == SSA_NAME
7310 26566 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
7311 : av = rhs1;
7312 : }
7313 : return av;
7314 : }
7315 : }
7316 5276276 : else if (is_gimple_min_invariant (valnum))
7317 5276276 : return valnum;
7318 : return NULL_TREE;
7319 : }
7320 :
7321 : /* At the current point of the eliminate domwalk make OP available. */
7322 :
7323 : void
7324 51874782 : eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
7325 : {
7326 51874782 : tree valnum = VN_INFO (op)->valnum;
7327 51874782 : if (TREE_CODE (valnum) == SSA_NAME)
7328 : {
7329 100276829 : if (avail.length () <= SSA_NAME_VERSION (valnum))
7330 17494991 : avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
7331 51874782 : tree pushop = op;
7332 51874782 : if (avail[SSA_NAME_VERSION (valnum)])
7333 47840 : pushop = avail[SSA_NAME_VERSION (valnum)];
7334 51874782 : avail_stack.safe_push (pushop);
7335 51874782 : avail[SSA_NAME_VERSION (valnum)] = op;
7336 : }
7337 51874782 : }
7338 :
7339 : /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
7340 : the leader for the expression if insertion was successful. */
7341 :
7342 : tree
7343 141134 : eliminate_dom_walker::eliminate_insert (basic_block bb,
7344 : gimple_stmt_iterator *gsi, tree val)
7345 : {
7346 : /* We can insert a sequence with a single assignment only. */
7347 141134 : gimple_seq stmts = VN_INFO (val)->expr;
7348 141134 : if (!gimple_seq_singleton_p (stmts))
7349 : return NULL_TREE;
7350 141134 : gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
7351 141134 : if (!stmt
7352 141134 : || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7353 : && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
7354 : && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
7355 : && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
7356 : && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
7357 136 : || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
7358 : return NULL_TREE;
7359 :
7360 46079 : tree op = gimple_assign_rhs1 (stmt);
7361 46079 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
7362 46079 : || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7363 20800 : op = TREE_OPERAND (op, 0);
7364 46079 : tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
7365 46033 : if (!leader)
7366 : return NULL_TREE;
7367 :
7368 36287 : tree res;
7369 36287 : stmts = NULL;
7370 56034 : if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7371 34724 : res = gimple_build (&stmts, BIT_FIELD_REF,
7372 17362 : TREE_TYPE (val), leader,
7373 17362 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
7374 17362 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
7375 18925 : else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
7376 214 : res = gimple_build (&stmts, BIT_AND_EXPR,
7377 107 : TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
7378 : else
7379 18818 : res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
7380 18818 : TREE_TYPE (val), leader);
7381 36287 : if (TREE_CODE (res) != SSA_NAME
7382 36286 : || SSA_NAME_IS_DEFAULT_DEF (res)
7383 72573 : || gimple_bb (SSA_NAME_DEF_STMT (res)))
7384 : {
7385 1 : gimple_seq_discard (stmts);
7386 :
7387 : /* During propagation we have to treat SSA info conservatively
7388 : and thus we can end up simplifying the inserted expression
7389 : at elimination time to sth not defined in stmts. */
7390 : /* But then this is a redundancy we failed to detect. Which means
7391 : res now has two values. That doesn't play well with how
7392 : we track availability here, so give up. */
7393 1 : if (dump_file && (dump_flags & TDF_DETAILS))
7394 : {
7395 0 : if (TREE_CODE (res) == SSA_NAME)
7396 0 : res = eliminate_avail (bb, res);
7397 0 : if (res)
7398 : {
7399 0 : fprintf (dump_file, "Failed to insert expression for value ");
7400 0 : print_generic_expr (dump_file, val);
7401 0 : fprintf (dump_file, " which is really fully redundant to ");
7402 0 : print_generic_expr (dump_file, res);
7403 0 : fprintf (dump_file, "\n");
7404 : }
7405 : }
7406 :
7407 : return NULL_TREE;
7408 : }
7409 : else
7410 : {
7411 36286 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
7412 36286 : vn_ssa_aux_t vn_info = VN_INFO (res);
7413 36286 : vn_info->valnum = val;
7414 36286 : vn_info->visited = true;
7415 : }
7416 :
7417 36286 : insertions++;
7418 36286 : if (dump_file && (dump_flags & TDF_DETAILS))
7419 : {
7420 501 : fprintf (dump_file, "Inserted ");
7421 501 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
7422 : }
7423 :
7424 : return res;
7425 : }
7426 :
7427 : void
7428 373283667 : eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
7429 : {
7430 373283667 : tree sprime = NULL_TREE;
7431 373283667 : gimple *stmt = gsi_stmt (*gsi);
7432 373283667 : tree lhs = gimple_get_lhs (stmt);
7433 125454992 : if (lhs && TREE_CODE (lhs) == SSA_NAME
7434 173201640 : && !gimple_has_volatile_ops (stmt)
7435 : /* See PR43491. Do not replace a global register variable when
7436 : it is a the RHS of an assignment. Do replace local register
7437 : variables since gcc does not guarantee a local variable will
7438 : be allocated in register.
7439 : ??? The fix isn't effective here. This should instead
7440 : be ensured by not value-numbering them the same but treating
7441 : them like volatiles? */
7442 458807083 : && !(gimple_assign_single_p (stmt)
7443 36766520 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
7444 2548507 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
7445 4203 : && is_global_var (gimple_assign_rhs1 (stmt)))))
7446 : {
7447 85523168 : sprime = eliminate_avail (b, lhs);
7448 85523168 : if (!sprime)
7449 : {
7450 : /* If there is no existing usable leader but SCCVN thinks
7451 : it has an expression it wants to use as replacement,
7452 : insert that. */
7453 71826275 : tree val = VN_INFO (lhs)->valnum;
7454 71826275 : vn_ssa_aux_t vn_info;
7455 71826275 : if (val != VN_TOP
7456 71826275 : && TREE_CODE (val) == SSA_NAME
7457 71826275 : && (vn_info = VN_INFO (val), true)
7458 71826275 : && vn_info->needs_insertion
7459 346235 : && vn_info->expr != NULL
7460 71967409 : && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
7461 36286 : eliminate_push_avail (b, sprime);
7462 : }
7463 :
7464 : /* If this now constitutes a copy duplicate points-to
7465 : and range info appropriately. This is especially
7466 : important for inserted code. */
7467 71826275 : if (sprime
7468 13733179 : && TREE_CODE (sprime) == SSA_NAME)
7469 9384319 : maybe_duplicate_ssa_info_at_copy (lhs, sprime);
7470 :
7471 : /* Inhibit the use of an inserted PHI on a loop header when
7472 : the address of the memory reference is a simple induction
7473 : variable. In other cases the vectorizer won't do anything
7474 : anyway (either it's loop invariant or a complicated
7475 : expression). */
7476 9384319 : if (sprime
7477 13733179 : && TREE_CODE (sprime) == SSA_NAME
7478 9384319 : && do_pre
7479 939234 : && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
7480 920611 : && loop_outer (b->loop_father)
7481 396480 : && has_zero_uses (sprime)
7482 198044 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
7483 197836 : && gimple_assign_load_p (stmt))
7484 : {
7485 108126 : gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
7486 108126 : basic_block def_bb = gimple_bb (def_stmt);
7487 108126 : if (gimple_code (def_stmt) == GIMPLE_PHI
7488 108126 : && def_bb->loop_father->header == def_bb)
7489 : {
7490 69550 : loop_p loop = def_bb->loop_father;
7491 69550 : ssa_op_iter iter;
7492 69550 : tree op;
7493 69550 : bool found = false;
7494 87770 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
7495 : {
7496 66016 : affine_iv iv;
7497 66016 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
7498 66016 : if (def_bb
7499 59860 : && flow_bb_inside_loop_p (loop, def_bb)
7500 120116 : && simple_iv (loop, loop, op, &iv, true))
7501 : {
7502 47796 : found = true;
7503 47796 : break;
7504 : }
7505 : }
7506 21754 : if (found)
7507 : {
7508 47796 : if (dump_file && (dump_flags & TDF_DETAILS))
7509 : {
7510 3 : fprintf (dump_file, "Not replacing ");
7511 3 : print_gimple_expr (dump_file, stmt, 0);
7512 3 : fprintf (dump_file, " with ");
7513 3 : print_generic_expr (dump_file, sprime);
7514 3 : fprintf (dump_file, " which would add a loop"
7515 : " carried dependence to loop %d\n",
7516 : loop->num);
7517 : }
7518 : /* Don't keep sprime available. */
7519 : sprime = NULL_TREE;
7520 : }
7521 : }
7522 : }
7523 :
7524 85523168 : if (sprime)
7525 : {
7526 : /* If we can propagate the value computed for LHS into
7527 : all uses don't bother doing anything with this stmt. */
7528 13685383 : if (may_propagate_copy (lhs, sprime))
7529 : {
7530 : /* Mark it for removal. */
7531 13683296 : to_remove.safe_push (stmt);
7532 :
7533 : /* ??? Don't count copy/constant propagations. */
7534 13683296 : if (gimple_assign_single_p (stmt)
7535 13683296 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7536 4773550 : || gimple_assign_rhs1 (stmt) == sprime))
7537 14564303 : return;
7538 :
7539 8346265 : if (dump_file && (dump_flags & TDF_DETAILS))
7540 : {
7541 18742 : fprintf (dump_file, "Replaced ");
7542 18742 : print_gimple_expr (dump_file, stmt, 0);
7543 18742 : fprintf (dump_file, " with ");
7544 18742 : print_generic_expr (dump_file, sprime);
7545 18742 : fprintf (dump_file, " in all uses of ");
7546 18742 : print_gimple_stmt (dump_file, stmt, 0);
7547 : }
7548 :
7549 8346265 : eliminations++;
7550 8346265 : return;
7551 : }
7552 :
7553 : /* If this is an assignment from our leader (which
7554 : happens in the case the value-number is a constant)
7555 : then there is nothing to do. Likewise if we run into
7556 : inserted code that needed a conversion because of
7557 : our type-agnostic value-numbering of loads. */
7558 2087 : if ((gimple_assign_single_p (stmt)
7559 1 : || (is_gimple_assign (stmt)
7560 1 : && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7561 0 : || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
7562 2088 : && sprime == gimple_assign_rhs1 (stmt))
7563 : return;
7564 :
7565 : /* Else replace its RHS. */
7566 719 : if (dump_file && (dump_flags & TDF_DETAILS))
7567 : {
7568 0 : fprintf (dump_file, "Replaced ");
7569 0 : print_gimple_expr (dump_file, stmt, 0);
7570 0 : fprintf (dump_file, " with ");
7571 0 : print_generic_expr (dump_file, sprime);
7572 0 : fprintf (dump_file, " in ");
7573 0 : print_gimple_stmt (dump_file, stmt, 0);
7574 : }
7575 719 : eliminations++;
7576 :
7577 719 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
7578 719 : && stmt_can_make_abnormal_goto (stmt));
7579 719 : gimple *orig_stmt = stmt;
7580 719 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
7581 719 : TREE_TYPE (sprime)))
7582 : {
7583 : /* We preserve conversions to but not from function or method
7584 : types. This asymmetry makes it necessary to re-instantiate
7585 : conversions here. */
7586 717 : if (POINTER_TYPE_P (TREE_TYPE (lhs))
7587 717 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7588 717 : sprime = fold_convert (TREE_TYPE (lhs), sprime);
7589 : else
7590 0 : gcc_unreachable ();
7591 : }
7592 719 : tree vdef = gimple_vdef (stmt);
7593 719 : tree vuse = gimple_vuse (stmt);
7594 719 : propagate_tree_value_into_stmt (gsi, sprime);
7595 719 : stmt = gsi_stmt (*gsi);
7596 719 : update_stmt (stmt);
7597 : /* In case the VDEF on the original stmt was released, value-number
7598 : it to the VUSE. This is to make vuse_ssa_val able to skip
7599 : released virtual operands. */
7600 1438 : if (vdef != gimple_vdef (stmt))
7601 : {
7602 0 : gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7603 0 : VN_INFO (vdef)->valnum = vuse;
7604 : }
7605 :
7606 : /* If we removed EH side-effects from the statement, clean
7607 : its EH information. */
7608 719 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7609 : {
7610 0 : bitmap_set_bit (need_eh_cleanup,
7611 0 : gimple_bb (stmt)->index);
7612 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7613 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7614 : }
7615 :
7616 : /* Likewise for AB side-effects. */
7617 719 : if (can_make_abnormal_goto
7618 719 : && !stmt_can_make_abnormal_goto (stmt))
7619 : {
7620 0 : bitmap_set_bit (need_ab_cleanup,
7621 0 : gimple_bb (stmt)->index);
7622 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7623 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7624 : }
7625 :
7626 : return;
7627 : }
7628 : }
7629 :
7630 : /* If the statement is a scalar store, see if the expression
7631 : has the same value number as its rhs. If so, the store is
7632 : dead. */
7633 359598284 : if (gimple_assign_single_p (stmt)
7634 132387294 : && !gimple_has_volatile_ops (stmt)
7635 57330488 : && !is_gimple_reg (gimple_assign_lhs (stmt))
7636 29573772 : && (TREE_CODE (gimple_assign_lhs (stmt)) != VAR_DECL
7637 2898415 : || !DECL_HARD_REGISTER (gimple_assign_lhs (stmt)))
7638 389167965 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7639 16892877 : || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7640 : {
7641 26398553 : tree rhs = gimple_assign_rhs1 (stmt);
7642 26398553 : vn_reference_t vnresult;
7643 : /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7644 : typed load of a byte known to be 0x11 as 1 so a store of
7645 : a boolean 1 is detected as redundant. Because of this we
7646 : have to make sure to lookup with a ref where its size
7647 : matches the precision. */
7648 26398553 : tree lookup_lhs = lhs;
7649 52535282 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7650 13826296 : && (TREE_CODE (lhs) != COMPONENT_REF
7651 8351750 : || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7652 40018366 : && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7653 : {
7654 863843 : if (BITINT_TYPE_P (TREE_TYPE (lhs))
7655 453430 : && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7656 : lookup_lhs = NULL_TREE;
7657 431219 : else if (TREE_CODE (lhs) == COMPONENT_REF
7658 431219 : || TREE_CODE (lhs) == MEM_REF)
7659 : {
7660 304083 : tree ltype = build_nonstandard_integer_type
7661 304083 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7662 304083 : TYPE_UNSIGNED (TREE_TYPE (lhs)));
7663 304083 : if (TREE_CODE (lhs) == COMPONENT_REF)
7664 : {
7665 233975 : tree bpos, foff = component_ref_field_offset (lhs);
7666 233975 : tree f = TREE_OPERAND (lhs, 1);
7667 233975 : if (!poly_int_tree_p (foff)
7668 233975 : || !tree_fits_poly_uint64_p
7669 233975 : (bpos = bit_from_pos (foff,
7670 233975 : DECL_FIELD_BIT_OFFSET (f))))
7671 : lookup_lhs = NULL_TREE;
7672 : else
7673 233975 : lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7674 233975 : TREE_OPERAND (lhs, 0),
7675 233975 : TYPE_SIZE (TREE_TYPE (lhs)), bpos);
7676 : }
7677 : else
7678 70108 : lookup_lhs = build2 (MEM_REF, ltype,
7679 70108 : TREE_OPERAND (lhs, 0),
7680 70108 : TREE_OPERAND (lhs, 1));
7681 : }
7682 : else
7683 : lookup_lhs = NULL_TREE;
7684 : }
7685 26263549 : tree val = NULL_TREE, tem;
7686 26263549 : if (lookup_lhs)
7687 52527098 : val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7688 : VN_WALKREWRITE, &vnresult, false,
7689 : NULL, NULL_TREE, stmt);
7690 26398553 : if (TREE_CODE (rhs) == SSA_NAME)
7691 12676804 : rhs = VN_INFO (rhs)->valnum;
7692 26398553 : gassign *ass;
7693 26398553 : if (val
7694 26398553 : && (operand_equal_p (val, rhs, 0)
7695 : /* Due to the bitfield lookups above we can get bit
7696 : interpretations of the same RHS as values here. Those
7697 : are redundant as well. */
7698 3280401 : || (TREE_CODE (val) == SSA_NAME
7699 1942439 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7700 1769187 : && (tem = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7701 1769187 : && TREE_CODE (tem) == VIEW_CONVERT_EXPR
7702 3700 : && TREE_OPERAND (tem, 0) == rhs)
7703 3280398 : || (TREE_CODE (rhs) == SSA_NAME
7704 26888046 : && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs)))
7705 1563326 : && gimple_assign_rhs1 (ass) == val
7706 725332 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass))
7707 9 : && tree_nop_conversion_p (TREE_TYPE (rhs), TREE_TYPE (val)))))
7708 : {
7709 : /* We can only remove the later store if the former aliases
7710 : at least all accesses the later one does. */
7711 259578 : ao_ref lhs_ref;
7712 259578 : ao_ref_init (&lhs_ref, lhs);
7713 259578 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
7714 259578 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7715 259578 : if (vnresult
7716 259578 : && ((vnresult->set == set
7717 56605 : || alias_set_subset_of (set, vnresult->set))
7718 241186 : && (vnresult->base_set == base_set
7719 26671 : || alias_set_subset_of (base_set, vnresult->base_set))))
7720 : {
7721 235839 : if (dump_file && (dump_flags & TDF_DETAILS))
7722 : {
7723 17 : fprintf (dump_file, "Deleted redundant store ");
7724 17 : print_gimple_stmt (dump_file, stmt, 0);
7725 : }
7726 :
7727 : /* Queue stmt for removal. */
7728 235839 : to_remove.safe_push (stmt);
7729 235839 : return;
7730 : }
7731 : }
7732 : }
7733 :
7734 : /* If this is a control statement value numbering left edges
7735 : unexecuted on force the condition in a way consistent with
7736 : that. */
7737 359362445 : if (gcond *cond = dyn_cast <gcond *> (stmt))
7738 : {
7739 19514723 : if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7740 19514723 : ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7741 : {
7742 643081 : if (dump_file && (dump_flags & TDF_DETAILS))
7743 : {
7744 15 : fprintf (dump_file, "Removing unexecutable edge from ");
7745 15 : print_gimple_stmt (dump_file, stmt, 0);
7746 : }
7747 643081 : if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7748 643081 : == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7749 257155 : gimple_cond_make_true (cond);
7750 : else
7751 385926 : gimple_cond_make_false (cond);
7752 643081 : update_stmt (cond);
7753 643081 : el_todo |= TODO_cleanup_cfg;
7754 643081 : return;
7755 : }
7756 : }
7757 :
7758 358719364 : bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7759 358719364 : bool was_noreturn = (is_gimple_call (stmt)
7760 358719364 : && gimple_call_noreturn_p (stmt));
7761 358719364 : tree vdef = gimple_vdef (stmt);
7762 358719364 : tree vuse = gimple_vuse (stmt);
7763 :
7764 : /* If we didn't replace the whole stmt (or propagate the result
7765 : into all uses), replace all uses on this stmt with their
7766 : leaders. */
7767 358719364 : bool modified = false;
7768 358719364 : use_operand_p use_p;
7769 358719364 : ssa_op_iter iter;
7770 530045938 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7771 : {
7772 171326574 : tree use = USE_FROM_PTR (use_p);
7773 : /* ??? The call code above leaves stmt operands un-updated. */
7774 171326574 : if (TREE_CODE (use) != SSA_NAME)
7775 0 : continue;
7776 171326574 : tree sprime;
7777 171326574 : if (SSA_NAME_IS_DEFAULT_DEF (use))
7778 : /* ??? For default defs BB shouldn't matter, but we have to
7779 : solve the inconsistency between rpo eliminate and
7780 : dom eliminate avail valueization first. */
7781 27313296 : sprime = eliminate_avail (b, use);
7782 : else
7783 : /* Look for sth available at the definition block of the argument.
7784 : This avoids inconsistencies between availability there which
7785 : decides if the stmt can be removed and availability at the
7786 : use site. The SSA property ensures that things available
7787 : at the definition are also available at uses. */
7788 144013278 : sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7789 171326574 : if (sprime && sprime != use
7790 13656796 : && may_propagate_copy (use, sprime, true)
7791 : /* We substitute into debug stmts to avoid excessive
7792 : debug temporaries created by removed stmts, but we need
7793 : to avoid doing so for inserted sprimes as we never want
7794 : to create debug temporaries for them. */
7795 184982653 : && (!inserted_exprs
7796 1204054 : || TREE_CODE (sprime) != SSA_NAME
7797 1186783 : || !is_gimple_debug (stmt)
7798 370533 : || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7799 : {
7800 13323071 : propagate_value (use_p, sprime);
7801 13323071 : modified = true;
7802 : }
7803 : }
7804 :
7805 : /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7806 : into which is a requirement for the IPA devirt machinery. */
7807 358719364 : gimple *old_stmt = stmt;
7808 358719364 : if (modified)
7809 : {
7810 : /* If a formerly non-invariant ADDR_EXPR is turned into an
7811 : invariant one it was on a separate stmt. */
7812 12390046 : if (gimple_assign_single_p (stmt)
7813 12390046 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7814 249569 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7815 12390046 : gimple_stmt_iterator prev = *gsi;
7816 12390046 : gsi_prev (&prev);
7817 12390046 : if (fold_stmt (gsi, follow_all_ssa_edges))
7818 : {
7819 : /* fold_stmt may have created new stmts in between
7820 : the previous stmt and the folded stmt. Mark
7821 : all defs created there as varying to not confuse
7822 : the SCCVN machinery as we're using that even during
7823 : elimination. */
7824 1075769 : if (gsi_end_p (prev))
7825 236160 : prev = gsi_start_bb (b);
7826 : else
7827 957689 : gsi_next (&prev);
7828 1075769 : if (gsi_stmt (prev) != gsi_stmt (*gsi))
7829 108221 : do
7830 : {
7831 68100 : tree def;
7832 68100 : ssa_op_iter dit;
7833 132277 : FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7834 : dit, SSA_OP_ALL_DEFS)
7835 : /* As existing DEFs may move between stmts
7836 : only process new ones. */
7837 64177 : if (! has_VN_INFO (def))
7838 : {
7839 40019 : vn_ssa_aux_t vn_info = VN_INFO (def);
7840 40019 : vn_info->valnum = def;
7841 40019 : vn_info->visited = true;
7842 : }
7843 68100 : if (gsi_stmt (prev) == gsi_stmt (*gsi))
7844 : break;
7845 40121 : gsi_next (&prev);
7846 40121 : }
7847 : while (1);
7848 : }
7849 12390046 : stmt = gsi_stmt (*gsi);
7850 : /* In case we folded the stmt away schedule the NOP for removal. */
7851 12390046 : if (gimple_nop_p (stmt))
7852 847 : to_remove.safe_push (stmt);
7853 : }
7854 :
7855 : /* Visit indirect calls and turn them into direct calls if
7856 : possible using the devirtualization machinery. Do this before
7857 : checking for required EH/abnormal/noreturn cleanup as devird
7858 : may expose more of those. */
7859 358719364 : if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7860 : {
7861 23062657 : tree fn = gimple_call_fn (call_stmt);
7862 23062657 : if (fn
7863 22222822 : && flag_devirtualize
7864 44528679 : && virtual_method_call_p (fn))
7865 : {
7866 182980 : tree otr_type = obj_type_ref_class (fn);
7867 182980 : unsigned HOST_WIDE_INT otr_tok
7868 182980 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7869 182980 : tree instance;
7870 182980 : ipa_polymorphic_call_context context (current_function_decl,
7871 182980 : fn, stmt, &instance);
7872 182980 : context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7873 : otr_type, stmt, NULL);
7874 182980 : bool final;
7875 182980 : vec <cgraph_node *> targets
7876 182980 : = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7877 : otr_tok, context, &final);
7878 182980 : if (dump_file)
7879 22 : dump_possible_polymorphic_call_targets (dump_file,
7880 : obj_type_ref_class (fn),
7881 : otr_tok, context);
7882 183269 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
7883 : {
7884 67 : tree fn;
7885 67 : if (targets.length () == 1)
7886 67 : fn = targets[0]->decl;
7887 : else
7888 0 : fn = builtin_decl_unreachable ();
7889 67 : if (dump_enabled_p ())
7890 : {
7891 9 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7892 : "converting indirect call to "
7893 : "function %s\n",
7894 9 : lang_hooks.decl_printable_name (fn, 2));
7895 : }
7896 67 : gimple_call_set_fndecl (call_stmt, fn);
7897 : /* If changing the call to __builtin_unreachable
7898 : or similar noreturn function, adjust gimple_call_fntype
7899 : too. */
7900 67 : if (gimple_call_noreturn_p (call_stmt)
7901 0 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7902 0 : && TYPE_ARG_TYPES (TREE_TYPE (fn))
7903 67 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7904 0 : == void_type_node))
7905 0 : gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7906 67 : maybe_remove_unused_call_args (cfun, call_stmt);
7907 67 : modified = true;
7908 : }
7909 : }
7910 : }
7911 :
7912 358719364 : if (modified)
7913 : {
7914 : /* When changing a call into a noreturn call, cfg cleanup
7915 : is needed to fix up the noreturn call. */
7916 12390067 : if (!was_noreturn
7917 12390067 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7918 56 : to_fixup.safe_push (stmt);
7919 : /* When changing a condition or switch into one we know what
7920 : edge will be executed, schedule a cfg cleanup. */
7921 12390067 : if ((gimple_code (stmt) == GIMPLE_COND
7922 1562607 : && (gimple_cond_true_p (as_a <gcond *> (stmt))
7923 1556749 : || gimple_cond_false_p (as_a <gcond *> (stmt))))
7924 13943856 : || (gimple_code (stmt) == GIMPLE_SWITCH
7925 8086 : && TREE_CODE (gimple_switch_index
7926 : (as_a <gswitch *> (stmt))) == INTEGER_CST))
7927 10673 : el_todo |= TODO_cleanup_cfg;
7928 : /* If we removed EH side-effects from the statement, clean
7929 : its EH information. */
7930 12390067 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7931 : {
7932 1903 : bitmap_set_bit (need_eh_cleanup,
7933 1903 : gimple_bb (stmt)->index);
7934 1903 : if (dump_file && (dump_flags & TDF_DETAILS))
7935 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7936 : }
7937 : /* Likewise for AB side-effects. */
7938 12390067 : if (can_make_abnormal_goto
7939 12390067 : && !stmt_can_make_abnormal_goto (stmt))
7940 : {
7941 0 : bitmap_set_bit (need_ab_cleanup,
7942 0 : gimple_bb (stmt)->index);
7943 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7944 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7945 : }
7946 12390067 : update_stmt (stmt);
7947 : /* In case the VDEF on the original stmt was released, value-number
7948 : it to the VUSE. This is to make vuse_ssa_val able to skip
7949 : released virtual operands. */
7950 15772191 : if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7951 2181 : VN_INFO (vdef)->valnum = vuse;
7952 : }
7953 :
7954 : /* Make new values available - for fully redundant LHS we
7955 : continue with the next stmt above and skip this.
7956 : But avoid picking up dead defs. */
7957 358719364 : tree def;
7958 431908191 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7959 73188827 : if (! has_zero_uses (def)
7960 73188827 : || (inserted_exprs
7961 213285 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7962 71724149 : eliminate_push_avail (b, def);
7963 : }
7964 :
7965 : /* Perform elimination for the basic-block B during the domwalk. */
7966 :
7967 : edge
7968 42552064 : eliminate_dom_walker::before_dom_children (basic_block b)
7969 : {
7970 : /* Mark new bb. */
7971 42552064 : avail_stack.safe_push (NULL_TREE);
7972 :
7973 : /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7974 42552064 : if (!(b->flags & BB_EXECUTABLE))
7975 : return NULL;
7976 :
7977 37587534 : vn_context_bb = b;
7978 :
7979 49444089 : for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7980 : {
7981 11856555 : gphi *phi = gsi.phi ();
7982 11856555 : tree res = PHI_RESULT (phi);
7983 :
7984 23713110 : if (virtual_operand_p (res))
7985 : {
7986 5442694 : gsi_next (&gsi);
7987 5442694 : continue;
7988 : }
7989 :
7990 6413861 : tree sprime = eliminate_avail (b, res);
7991 6413861 : if (sprime
7992 6413861 : && sprime != res)
7993 : {
7994 440543 : if (dump_file && (dump_flags & TDF_DETAILS))
7995 : {
7996 20 : fprintf (dump_file, "Replaced redundant PHI node defining ");
7997 20 : print_generic_expr (dump_file, res);
7998 20 : fprintf (dump_file, " with ");
7999 20 : print_generic_expr (dump_file, sprime);
8000 20 : fprintf (dump_file, "\n");
8001 : }
8002 :
8003 : /* If we inserted this PHI node ourself, it's not an elimination. */
8004 440543 : if (! inserted_exprs
8005 562674 : || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
8006 414084 : eliminations++;
8007 :
8008 : /* If we will propagate into all uses don't bother to do
8009 : anything. */
8010 440543 : if (may_propagate_copy (res, sprime))
8011 : {
8012 : /* Mark the PHI for removal. */
8013 440543 : to_remove.safe_push (phi);
8014 440543 : gsi_next (&gsi);
8015 440543 : continue;
8016 : }
8017 :
8018 0 : remove_phi_node (&gsi, false);
8019 :
8020 0 : if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
8021 0 : sprime = fold_convert (TREE_TYPE (res), sprime);
8022 0 : gimple *stmt = gimple_build_assign (res, sprime);
8023 0 : gimple_stmt_iterator gsi2 = gsi_after_labels (b);
8024 0 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
8025 0 : continue;
8026 0 : }
8027 :
8028 5973318 : eliminate_push_avail (b, res);
8029 5973318 : gsi_next (&gsi);
8030 : }
8031 :
8032 75175068 : for (gimple_stmt_iterator gsi = gsi_start_bb (b);
8033 296748155 : !gsi_end_p (gsi);
8034 259160621 : gsi_next (&gsi))
8035 259160621 : eliminate_stmt (b, &gsi);
8036 :
8037 : /* Replace destination PHI arguments. */
8038 37587534 : edge_iterator ei;
8039 37587534 : edge e;
8040 88740853 : FOR_EACH_EDGE (e, ei, b->succs)
8041 51153319 : if (e->flags & EDGE_EXECUTABLE)
8042 50579645 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
8043 80931463 : !gsi_end_p (gsi);
8044 30351818 : gsi_next (&gsi))
8045 : {
8046 30351818 : gphi *phi = gsi.phi ();
8047 30351818 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8048 30351818 : tree arg = USE_FROM_PTR (use_p);
8049 50257684 : if (TREE_CODE (arg) != SSA_NAME
8050 30351818 : || virtual_operand_p (arg))
8051 19905866 : continue;
8052 10445952 : tree sprime = eliminate_avail (b, arg);
8053 20891904 : if (sprime && may_propagate_copy (arg, sprime,
8054 10445952 : !(e->flags & EDGE_ABNORMAL)))
8055 10433299 : propagate_value (use_p, sprime);
8056 : }
8057 :
8058 37587534 : vn_context_bb = NULL;
8059 :
8060 37587534 : return NULL;
8061 : }
8062 :
8063 : /* Make no longer available leaders no longer available. */
8064 :
8065 : void
8066 42552064 : eliminate_dom_walker::after_dom_children (basic_block)
8067 : {
8068 42552064 : tree entry;
8069 94426846 : while ((entry = avail_stack.pop ()) != NULL_TREE)
8070 : {
8071 51874782 : tree valnum = VN_INFO (entry)->valnum;
8072 51874782 : tree old = avail[SSA_NAME_VERSION (valnum)];
8073 51874782 : if (old == entry)
8074 : avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
8075 : else
8076 47840 : avail[SSA_NAME_VERSION (valnum)] = entry;
8077 : }
8078 42552064 : }
8079 :
8080 : /* Remove queued stmts and perform delayed cleanups. */
8081 :
8082 : unsigned
8083 6315895 : eliminate_dom_walker::eliminate_cleanup (bool region_p)
8084 : {
8085 6315895 : statistics_counter_event (cfun, "Eliminated", eliminations);
8086 6315895 : statistics_counter_event (cfun, "Insertions", insertions);
8087 :
8088 : /* We cannot remove stmts during BB walk, especially not release SSA
8089 : names there as this confuses the VN machinery. The stmts ending
8090 : up in to_remove are either stores or simple copies.
8091 : Remove stmts in reverse order to make debug stmt creation possible. */
8092 34686692 : while (!to_remove.is_empty ())
8093 : {
8094 15738951 : bool do_release_defs = true;
8095 15738951 : gimple *stmt = to_remove.pop ();
8096 :
8097 : /* When we are value-numbering a region we do not require exit PHIs to
8098 : be present so we have to make sure to deal with uses outside of the
8099 : region of stmts that we thought are eliminated.
8100 : ??? Note we may be confused by uses in dead regions we didn't run
8101 : elimination on. Rather than checking individual uses we accept
8102 : dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
8103 : contains such example). */
8104 15738951 : if (region_p)
8105 : {
8106 1873294 : if (gphi *phi = dyn_cast <gphi *> (stmt))
8107 : {
8108 1164448 : tree lhs = gimple_phi_result (phi);
8109 1164448 : if (!has_zero_uses (lhs))
8110 : {
8111 24540 : if (dump_file && (dump_flags & TDF_DETAILS))
8112 3 : fprintf (dump_file, "Keeping eliminated stmt live "
8113 : "as copy because of out-of-region uses\n");
8114 24540 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
8115 24540 : gimple *copy = gimple_build_assign (lhs, sprime);
8116 24540 : gimple_stmt_iterator gsi
8117 24540 : = gsi_after_labels (gimple_bb (stmt));
8118 24540 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
8119 24540 : do_release_defs = false;
8120 : }
8121 : }
8122 708846 : else if (tree lhs = gimple_get_lhs (stmt))
8123 708846 : if (TREE_CODE (lhs) == SSA_NAME
8124 708846 : && !has_zero_uses (lhs))
8125 : {
8126 2225 : if (dump_file && (dump_flags & TDF_DETAILS))
8127 0 : fprintf (dump_file, "Keeping eliminated stmt live "
8128 : "as copy because of out-of-region uses\n");
8129 2225 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
8130 2225 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
8131 2225 : if (is_gimple_assign (stmt))
8132 : {
8133 2225 : gimple_assign_set_rhs_from_tree (&gsi, sprime);
8134 2225 : stmt = gsi_stmt (gsi);
8135 2225 : update_stmt (stmt);
8136 2225 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
8137 0 : bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
8138 2225 : continue;
8139 : }
8140 : else
8141 : {
8142 0 : gimple *copy = gimple_build_assign (lhs, sprime);
8143 0 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
8144 0 : do_release_defs = false;
8145 : }
8146 : }
8147 : }
8148 :
8149 15736726 : if (dump_file && (dump_flags & TDF_DETAILS))
8150 : {
8151 21442 : fprintf (dump_file, "Removing dead stmt ");
8152 21442 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
8153 : }
8154 :
8155 15736726 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
8156 15736726 : if (gimple_code (stmt) == GIMPLE_PHI)
8157 1818969 : remove_phi_node (&gsi, do_release_defs);
8158 : else
8159 : {
8160 13917757 : basic_block bb = gimple_bb (stmt);
8161 13917757 : unlink_stmt_vdef (stmt);
8162 13917757 : if (gsi_remove (&gsi, true))
8163 26779 : bitmap_set_bit (need_eh_cleanup, bb->index);
8164 13917757 : if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
8165 2 : bitmap_set_bit (need_ab_cleanup, bb->index);
8166 13917757 : if (do_release_defs)
8167 13917757 : release_defs (stmt);
8168 : }
8169 :
8170 : /* Removing a stmt may expose a forwarder block. */
8171 15736726 : el_todo |= TODO_cleanup_cfg;
8172 : }
8173 :
8174 : /* Fixup stmts that became noreturn calls. This may require splitting
8175 : blocks and thus isn't possible during the dominator walk. Do this
8176 : in reverse order so we don't inadvertently remove a stmt we want to
8177 : fixup by visiting a dominating now noreturn call first. */
8178 6315951 : while (!to_fixup.is_empty ())
8179 : {
8180 56 : gimple *stmt = to_fixup.pop ();
8181 :
8182 56 : if (dump_file && (dump_flags & TDF_DETAILS))
8183 : {
8184 0 : fprintf (dump_file, "Fixing up noreturn call ");
8185 0 : print_gimple_stmt (dump_file, stmt, 0);
8186 : }
8187 :
8188 56 : if (fixup_noreturn_call (stmt))
8189 56 : el_todo |= TODO_cleanup_cfg;
8190 : }
8191 :
8192 6315895 : bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
8193 6315895 : bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
8194 :
8195 6315895 : if (do_eh_cleanup)
8196 10781 : gimple_purge_all_dead_eh_edges (need_eh_cleanup);
8197 :
8198 6315895 : if (do_ab_cleanup)
8199 2 : gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
8200 :
8201 6315895 : if (do_eh_cleanup || do_ab_cleanup)
8202 10783 : el_todo |= TODO_cleanup_cfg;
8203 :
8204 6315895 : return el_todo;
8205 : }
8206 :
8207 : /* Eliminate fully redundant computations. */
8208 :
8209 : static unsigned
8210 987733 : eliminate_with_rpo_vn (bitmap inserted_exprs)
8211 : {
8212 987733 : eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
8213 :
8214 987733 : eliminate_dom_walker *saved_rpo_avail = rpo_avail;
8215 987733 : rpo_avail = &walker;
8216 987733 : walker.walk (cfun->cfg->x_entry_block_ptr);
8217 987733 : rpo_avail = saved_rpo_avail;
8218 :
8219 987733 : return walker.eliminate_cleanup ();
8220 987733 : }
8221 :
8222 : /* Give every value a value id, for PRE. */
8223 :
8224 : static void
8225 987733 : assign_value_ids ()
8226 : {
8227 : /* ??? Prune requirement of these. */
8228 987733 : constant_to_value_id = new hash_table<vn_constant_hasher> (23);
8229 :
8230 : /* Initialize the value ids and prune out remaining VN_TOPs
8231 : from dead code. */
8232 987733 : tree name;
8233 987733 : unsigned i;
8234 48627352 : FOR_EACH_SSA_NAME (i, name, cfun)
8235 : {
8236 34697954 : vn_ssa_aux_t info = VN_INFO (name);
8237 34697954 : if (!info->visited
8238 34621741 : || info->valnum == VN_TOP)
8239 76213 : info->valnum = name;
8240 34697954 : if (info->valnum == name)
8241 33511343 : info->value_id = get_next_value_id ();
8242 1186611 : else if (is_gimple_min_invariant (info->valnum))
8243 42345 : info->value_id = get_or_alloc_constant_value_id (info->valnum);
8244 : }
8245 :
8246 : /* Propagate. */
8247 48627352 : FOR_EACH_SSA_NAME (i, name, cfun)
8248 : {
8249 34697954 : vn_ssa_aux_t info = VN_INFO (name);
8250 34697954 : if (TREE_CODE (info->valnum) == SSA_NAME
8251 34655609 : && info->valnum != name
8252 35842220 : && info->value_id != VN_INFO (info->valnum)->value_id)
8253 1144266 : info->value_id = VN_INFO (info->valnum)->value_id;
8254 : }
8255 :
8256 987733 : set_hashtable_value_ids ();
8257 :
8258 987733 : if (dump_file && (dump_flags & TDF_DETAILS))
8259 : {
8260 14 : fprintf (dump_file, "Value numbers:\n");
8261 406 : FOR_EACH_SSA_NAME (i, name, cfun)
8262 : {
8263 307 : if (VN_INFO (name)->visited
8264 307 : && SSA_VAL (name) != name)
8265 : {
8266 33 : print_generic_expr (dump_file, name);
8267 33 : fprintf (dump_file, " = ");
8268 33 : print_generic_expr (dump_file, SSA_VAL (name));
8269 33 : fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
8270 : }
8271 : }
8272 : }
8273 987733 : }
8274 :
8275 : /* Free VN associated data structures. */
8276 :
8277 : static void
8278 6335585 : free_rpo_vn (void)
8279 : {
8280 6335585 : free_vn_table (valid_info);
8281 6335585 : XDELETE (valid_info);
8282 6335585 : obstack_free (&vn_tables_obstack, NULL);
8283 6335585 : obstack_free (&vn_tables_insert_obstack, NULL);
8284 :
8285 6335585 : vn_ssa_aux_iterator_type it;
8286 6335585 : vn_ssa_aux_t info;
8287 185781947 : FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
8288 179446362 : if (info->needs_insertion)
8289 4326274 : release_ssa_name (info->name);
8290 6335585 : obstack_free (&vn_ssa_aux_obstack, NULL);
8291 6335585 : delete vn_ssa_aux_hash;
8292 :
8293 6335585 : delete constant_to_value_id;
8294 6335585 : constant_to_value_id = NULL;
8295 6335585 : }
8296 :
8297 : /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
8298 :
8299 : static tree
8300 21593519 : vn_lookup_simplify_result (gimple_match_op *res_op)
8301 : {
8302 21593519 : if (!res_op->code.is_tree_code ())
8303 : return NULL_TREE;
8304 21590252 : vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
8305 : sizeof_vn_nary_op
8306 : (vn_nary_length_from_match_op (res_op)));
8307 21590252 : init_vn_nary_op_from_match_op (vno1, res_op);
8308 21590252 : vn_nary_op_t vnresult = NULL;
8309 21590252 : tree res = vn_nary_op_lookup_1 (vno1, &vnresult);
8310 : /* If this is used from expression simplification make sure to
8311 : return an available expression. */
8312 21590252 : if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
8313 2337428 : res = rpo_avail->eliminate_avail (vn_context_bb, res);
8314 : return res;
8315 : }
8316 :
8317 : /* Return a leader for OPs value that is valid at BB. */
8318 :
8319 : tree
8320 287298790 : rpo_elim::eliminate_avail (basic_block bb, tree op)
8321 : {
8322 287298790 : bool visited;
8323 287298790 : tree valnum = SSA_VAL (op, &visited);
8324 : /* If we didn't visit OP then it must be defined outside of the
8325 : region we process and also dominate it. So it is available. */
8326 287298790 : if (!visited)
8327 : return op;
8328 285062701 : if (TREE_CODE (valnum) == SSA_NAME)
8329 : {
8330 270069232 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
8331 : return valnum;
8332 263054767 : vn_ssa_aux_t valnum_info = VN_INFO (valnum);
8333 263054767 : vn_avail *av = valnum_info->avail;
8334 263054767 : if (!av)
8335 : {
8336 : /* See above. But when there's availability info prefer
8337 : what we recorded there for example to preserve LC SSA. */
8338 86858093 : if (!valnum_info->visited)
8339 : return valnum;
8340 85957744 : return NULL_TREE;
8341 : }
8342 176196674 : if (av->location == bb->index)
8343 : /* On tramp3d 90% of the cases are here. */
8344 116321120 : return ssa_name (av->leader);
8345 74185180 : do
8346 : {
8347 74185180 : basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
8348 : /* ??? During elimination we have to use availability at the
8349 : definition site of a use we try to replace. This
8350 : is required to not run into inconsistencies because
8351 : of dominated_by_p_w_unex behavior and removing a definition
8352 : while not replacing all uses.
8353 : ??? We could try to consistently walk dominators
8354 : ignoring non-executable regions. The nearest common
8355 : dominator of bb and abb is where we can stop walking. We
8356 : may also be able to "pre-compute" (bits of) the next immediate
8357 : (non-)dominator during the RPO walk when marking edges as
8358 : executable. */
8359 74185180 : if (dominated_by_p_w_unex (bb, abb))
8360 : {
8361 55911979 : tree leader = ssa_name (av->leader);
8362 : /* Prevent eliminations that break loop-closed SSA. */
8363 55911979 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
8364 3884452 : && ! SSA_NAME_IS_DEFAULT_DEF (leader)
8365 59796431 : && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
8366 3884452 : (leader))->loop_father,
8367 : bb))
8368 : return NULL_TREE;
8369 55826718 : if (dump_file && (dump_flags & TDF_DETAILS))
8370 : {
8371 3978 : print_generic_expr (dump_file, leader);
8372 3978 : fprintf (dump_file, " is available for ");
8373 3978 : print_generic_expr (dump_file, valnum);
8374 3978 : fprintf (dump_file, "\n");
8375 : }
8376 : /* On tramp3d 99% of the _remaining_ cases succeed at
8377 : the first enty. */
8378 : return leader;
8379 : }
8380 : /* ??? Can we somehow skip to the immediate dominator
8381 : RPO index (bb_to_rpo)? Again, maybe not worth, on
8382 : tramp3d the worst number of elements in the vector is 9. */
8383 18273201 : av = av->next;
8384 : }
8385 18273201 : while (av);
8386 : /* While we prefer avail we have to fallback to using the value
8387 : directly if defined outside of the region when none of the
8388 : available defs suit. */
8389 3963575 : if (!valnum_info->visited)
8390 2 : return valnum;
8391 : }
8392 14993469 : else if (valnum != VN_TOP)
8393 : /* valnum is is_gimple_min_invariant. */
8394 14993469 : return valnum;
8395 : return NULL_TREE;
8396 : }
8397 :
8398 : /* Make LEADER a leader for its value at BB. */
8399 :
8400 : void
8401 100859717 : rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
8402 : {
8403 100859717 : tree valnum = VN_INFO (leader)->valnum;
8404 100859717 : if (valnum == VN_TOP
8405 100859717 : || is_gimple_min_invariant (valnum))
8406 : return;
8407 100859717 : if (dump_file && (dump_flags & TDF_DETAILS))
8408 : {
8409 343234 : fprintf (dump_file, "Making available beyond BB%d ", bb->index);
8410 343234 : print_generic_expr (dump_file, leader);
8411 343234 : fprintf (dump_file, " for value ");
8412 343234 : print_generic_expr (dump_file, valnum);
8413 343234 : fprintf (dump_file, "\n");
8414 : }
8415 100859717 : vn_ssa_aux_t value = VN_INFO (valnum);
8416 100859717 : vn_avail *av;
8417 100859717 : if (m_avail_freelist)
8418 : {
8419 19462330 : av = m_avail_freelist;
8420 19462330 : m_avail_freelist = m_avail_freelist->next;
8421 : }
8422 : else
8423 81397387 : av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
8424 100859717 : av->location = bb->index;
8425 100859717 : av->leader = SSA_NAME_VERSION (leader);
8426 100859717 : av->next = value->avail;
8427 100859717 : av->next_undo = last_pushed_avail;
8428 100859717 : last_pushed_avail = value;
8429 100859717 : value->avail = av;
8430 : }
8431 :
8432 : /* Valueization hook for RPO VN plus required state. */
8433 :
8434 : tree
8435 2319264848 : rpo_vn_valueize (tree name)
8436 : {
8437 2319264848 : if (TREE_CODE (name) == SSA_NAME)
8438 : {
8439 2271779774 : vn_ssa_aux_t val = VN_INFO (name);
8440 2271779774 : if (val)
8441 : {
8442 2271779774 : tree tem = val->valnum;
8443 2271779774 : if (tem != VN_TOP && tem != name)
8444 : {
8445 121179075 : if (TREE_CODE (tem) != SSA_NAME)
8446 : return tem;
8447 : /* For all values we only valueize to an available leader
8448 : which means we can use SSA name info without restriction. */
8449 103149397 : tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
8450 103149397 : if (tem)
8451 103094025 : return tem;
8452 : }
8453 : }
8454 : }
8455 : return name;
8456 : }
8457 :
8458 : /* Insert on PRED_E predicates derived from CODE OPS being true besides the
8459 : inverted condition. */
8460 :
8461 : static void
8462 28178654 : insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
8463 : {
8464 28178654 : switch (code)
8465 : {
8466 1410333 : case LT_EXPR:
8467 : /* a < b -> a {!,<}= b */
8468 1410333 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8469 : ops, boolean_true_node, 0, pred_e);
8470 1410333 : vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
8471 : ops, boolean_true_node, 0, pred_e);
8472 : /* a < b -> ! a {>,=} b */
8473 1410333 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8474 : ops, boolean_false_node, 0, pred_e);
8475 1410333 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8476 : ops, boolean_false_node, 0, pred_e);
8477 1410333 : break;
8478 3583754 : case GT_EXPR:
8479 : /* a > b -> a {!,>}= b */
8480 3583754 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8481 : ops, boolean_true_node, 0, pred_e);
8482 3583754 : vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
8483 : ops, boolean_true_node, 0, pred_e);
8484 : /* a > b -> ! a {<,=} b */
8485 3583754 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8486 : ops, boolean_false_node, 0, pred_e);
8487 3583754 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8488 : ops, boolean_false_node, 0, pred_e);
8489 3583754 : break;
8490 9633909 : case EQ_EXPR:
8491 : /* a == b -> ! a {<,>} b */
8492 9633909 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8493 : ops, boolean_false_node, 0, pred_e);
8494 9633909 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8495 : ops, boolean_false_node, 0, pred_e);
8496 9633909 : break;
8497 : case LE_EXPR:
8498 : case GE_EXPR:
8499 : case NE_EXPR:
8500 : /* Nothing besides inverted condition. */
8501 : break;
8502 28178654 : default:;
8503 : }
8504 28178654 : }
8505 :
8506 : /* Insert on the TRUE_E true and FALSE_E false predicates
8507 : derived from LHS CODE RHS. */
8508 :
8509 : static void
8510 23948526 : insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
8511 : edge true_e, edge false_e)
8512 : {
8513 : /* If both edges are null, then there is nothing to be done. */
8514 23948526 : if (!true_e && !false_e)
8515 1347888 : return;
8516 :
8517 : /* Canonicalize the comparison if needed, putting
8518 : the constant in the rhs. */
8519 22604288 : if (tree_swap_operands_p (lhs, rhs))
8520 : {
8521 0 : std::swap (lhs, rhs);
8522 0 : code = swap_tree_comparison (code);
8523 : }
8524 :
8525 : /* If the lhs is not a ssa name, don't record anything. */
8526 22604288 : if (TREE_CODE (lhs) != SSA_NAME)
8527 : return;
8528 :
8529 22600638 : tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs));
8530 22600638 : tree ops[2];
8531 22600638 : ops[0] = lhs;
8532 22600638 : ops[1] = rhs;
8533 22600638 : if (true_e)
8534 18488150 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8535 : boolean_true_node, 0, true_e);
8536 22600638 : if (false_e)
8537 17441430 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8538 : boolean_false_node, 0, false_e);
8539 22600638 : if (icode != ERROR_MARK)
8540 : {
8541 22338783 : if (true_e)
8542 18322453 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8543 : boolean_false_node, 0, true_e);
8544 22338783 : if (false_e)
8545 17229934 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8546 : boolean_true_node, 0, false_e);
8547 : }
8548 : /* Relax for non-integers, inverted condition handled
8549 : above. */
8550 22600638 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8551 : {
8552 17712397 : if (true_e)
8553 14565285 : insert_related_predicates_on_edge (code, ops, true_e);
8554 17712397 : if (false_e)
8555 13613369 : insert_related_predicates_on_edge (icode, ops, false_e);
8556 : }
8557 22600638 : if (integer_zerop (rhs)
8558 22600638 : && (code == NE_EXPR || code == EQ_EXPR))
8559 : {
8560 9384365 : auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (lhs));
8561 : /* (A CMP B) != 0 is the same as (A CMP B).
8562 : (A CMP B) == 0 is just (A CMP B) with the edges swapped. */
8563 5971975 : if (def_stmt
8564 5971975 : && TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_comparison)
8565 : {
8566 422451 : tree_code nc = gimple_assign_rhs_code (def_stmt);
8567 422451 : tree nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8568 844902 : tree nrhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8569 : // Canonicalize the comparison before the check below,
8570 : // it might be the case where nlhs is a constant now.
8571 422451 : if (tree_swap_operands_p (nlhs, nrhs))
8572 : {
8573 16882 : std::swap (nlhs, nrhs);
8574 16882 : nc = swap_tree_comparison (nc);
8575 : }
8576 422451 : edge nt = true_e;
8577 422451 : edge nf = false_e;
8578 422451 : if (code == EQ_EXPR)
8579 301688 : std::swap (nt, nf);
8580 422451 : if (lhs != nlhs)
8581 422451 : insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
8582 : }
8583 : /* (a | b) == 0 ->
8584 : on true edge assert: a == 0 & b == 0. */
8585 : /* (a | b) != 0 ->
8586 : on false edge assert: a == 0 & b == 0. */
8587 15356340 : if (def_stmt && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
8588 : {
8589 251289 : edge e = code == EQ_EXPR ? true_e : false_e;
8590 251289 : tree nlhs;
8591 :
8592 251289 : nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8593 : /* A valueization of the `a` might return the old lhs
8594 : which is already handled above. */
8595 251289 : if (nlhs != lhs)
8596 251289 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8597 :
8598 : /* A valueization of the `b` might return the old lhs
8599 : which is already handled above. */
8600 502578 : nlhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8601 251289 : if (nlhs != lhs)
8602 251289 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8603 : }
8604 : }
8605 : }
8606 :
8607 : /* Main stmt worker for RPO VN, process BB. */
8608 :
8609 : static unsigned
8610 63549132 : process_bb (rpo_elim &avail, basic_block bb,
8611 : bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
8612 : bool do_region, bitmap exit_bbs, bool skip_phis)
8613 : {
8614 63549132 : unsigned todo = 0;
8615 63549132 : edge_iterator ei;
8616 63549132 : edge e;
8617 :
8618 63549132 : vn_context_bb = bb;
8619 :
8620 63549132 : update_exec_idom (bb);
8621 :
8622 : /* If we are in loop-closed SSA preserve this state. This is
8623 : relevant when called on regions from outside of FRE/PRE. */
8624 63549132 : bool lc_phi_nodes = false;
8625 63549132 : if (!skip_phis
8626 63549132 : && loops_state_satisfies_p (LOOP_CLOSED_SSA))
8627 4057333 : FOR_EACH_EDGE (e, ei, bb->preds)
8628 2455254 : if (e->src->loop_father != e->dest->loop_father
8629 2455254 : && flow_loop_nested_p (e->dest->loop_father,
8630 : e->src->loop_father))
8631 : {
8632 : lc_phi_nodes = true;
8633 : break;
8634 : }
8635 :
8636 : /* When we visit a loop header substitute into loop info. */
8637 63549132 : if (!iterate && eliminate && bb->loop_father->header == bb)
8638 : {
8639 : /* Keep fields in sync with substitute_in_loop_info. */
8640 978547 : if (bb->loop_father->nb_iterations)
8641 164494 : bb->loop_father->nb_iterations
8642 164494 : = simplify_replace_tree (bb->loop_father->nb_iterations,
8643 : NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
8644 : }
8645 :
8646 : /* Value-number all defs in the basic-block. */
8647 63549132 : if (!skip_phis)
8648 : {
8649 : /* PHIs are executed in parallel so value-number all of them
8650 : based on the values of their arguments before committing any
8651 : new value. Otherwise a PHI argument flowing in over a backedge
8652 : that is the result of a PHI visited earlier in the same block
8653 : sees its new value while one that is the result of a PHI visited
8654 : later does not. That lets one PHI cycle advance a step ahead of
8655 : an equivalent one, depending on the order of the PHIs, and
8656 : iteration then fails to discover the equivalence or oscillates
8657 : between CSEing to different cycles. */
8658 63523112 : auto_vec<tree, 16> phi_vals;
8659 63523112 : if (!bb_visited)
8660 71102799 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8661 19667853 : gsi_next (&gsi))
8662 : {
8663 19667853 : gphi *phi = gsi.phi ();
8664 19667853 : vn_ssa_aux_t res_info = VN_INFO (PHI_RESULT (phi));
8665 19667853 : gcc_assert (!res_info->visited);
8666 19667853 : res_info->valnum = VN_TOP;
8667 19667853 : res_info->visited = true;
8668 : }
8669 91382884 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8670 27859772 : gsi_next (&gsi))
8671 : {
8672 27859772 : gphi *phi = gsi.phi ();
8673 27859772 : vn_ssa_aux_t res_info = VN_INFO (PHI_RESULT (phi));
8674 27859772 : tree oldval = res_info->valnum;
8675 : /* When not iterating force backedge values to varying. */
8676 27859772 : visit_stmt (phi, !iterate_phis);
8677 27859772 : phi_vals.safe_push (res_info->valnum);
8678 27859772 : res_info->valnum = oldval;
8679 : }
8680 :
8681 63523112 : unsigned phi_idx = 0;
8682 91382884 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8683 27859772 : gsi_next (&gsi))
8684 : {
8685 27859772 : gphi *phi = gsi.phi ();
8686 27859772 : tree res = PHI_RESULT (phi);
8687 27859772 : vn_ssa_aux_t res_info = VN_INFO (res);
8688 : /* Commit the new PHI value. */
8689 27859772 : res_info->valnum = phi_vals[phi_idx++];
8690 55719544 : if (virtual_operand_p (res))
8691 11015554 : continue;
8692 :
8693 : /* Eliminate */
8694 : /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
8695 : how we handle backedges and availability.
8696 : And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
8697 16844218 : tree val = res_info->valnum;
8698 16844218 : if (res != val && !iterate && eliminate)
8699 : {
8700 1504171 : if (tree leader = avail.eliminate_avail (bb, res))
8701 : {
8702 1378989 : if (leader != res
8703 : /* Preserve loop-closed SSA form. */
8704 1378989 : && (! lc_phi_nodes
8705 7465 : || is_gimple_min_invariant (leader)))
8706 : {
8707 1378426 : if (dump_file && (dump_flags & TDF_DETAILS))
8708 : {
8709 257 : fprintf (dump_file, "Replaced redundant PHI node "
8710 : "defining ");
8711 257 : print_generic_expr (dump_file, res);
8712 257 : fprintf (dump_file, " with ");
8713 257 : print_generic_expr (dump_file, leader);
8714 257 : fprintf (dump_file, "\n");
8715 : }
8716 1378426 : avail.eliminations++;
8717 :
8718 1378426 : if (may_propagate_copy (res, leader))
8719 : {
8720 : /* Schedule for removal. */
8721 1378426 : avail.to_remove.safe_push (phi);
8722 1378426 : continue;
8723 : }
8724 : /* ??? Else generate a copy stmt. */
8725 : }
8726 : }
8727 : }
8728 : /* Only make defs available that not already are. But make
8729 : sure loop-closed SSA PHI node defs are picked up for
8730 : downstream uses. */
8731 15465792 : if (lc_phi_nodes
8732 15465792 : || res == val
8733 15465792 : || ! avail.eliminate_avail (bb, res))
8734 11844011 : avail.eliminate_push_avail (bb, res);
8735 : }
8736 63523112 : }
8737 :
8738 : /* For empty BBs mark outgoing edges executable. For non-empty BBs
8739 : we do this when processing the last stmt as we have to do this
8740 : before elimination which otherwise forces GIMPLE_CONDs to
8741 : if (1 != 0) style when seeing non-executable edges. */
8742 127098264 : if (gsi_end_p (gsi_start_bb (bb)))
8743 : {
8744 14690128 : FOR_EACH_EDGE (e, ei, bb->succs)
8745 : {
8746 7345064 : if (!(e->flags & EDGE_EXECUTABLE))
8747 : {
8748 4956193 : if (dump_file && (dump_flags & TDF_DETAILS))
8749 6539 : fprintf (dump_file,
8750 : "marking outgoing edge %d -> %d executable\n",
8751 6539 : e->src->index, e->dest->index);
8752 4956193 : e->flags |= EDGE_EXECUTABLE;
8753 4956193 : e->dest->flags |= BB_EXECUTABLE;
8754 : }
8755 2388871 : else if (!(e->dest->flags & BB_EXECUTABLE))
8756 : {
8757 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8758 0 : fprintf (dump_file,
8759 : "marking destination block %d reachable\n",
8760 : e->dest->index);
8761 0 : e->dest->flags |= BB_EXECUTABLE;
8762 : }
8763 : }
8764 : }
8765 127098264 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8766 517676386 : !gsi_end_p (gsi); gsi_next (&gsi))
8767 : {
8768 454127254 : ssa_op_iter i;
8769 454127254 : tree op;
8770 454127254 : if (!bb_visited)
8771 : {
8772 516540411 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8773 : {
8774 143604120 : vn_ssa_aux_t op_info = VN_INFO (op);
8775 143604120 : gcc_assert (!op_info->visited);
8776 143604120 : op_info->valnum = VN_TOP;
8777 143604120 : op_info->visited = true;
8778 : }
8779 :
8780 : /* We somehow have to deal with uses that are not defined
8781 : in the processed region. Forcing unvisited uses to
8782 : varying here doesn't play well with def-use following during
8783 : expression simplification, so we deal with this by checking
8784 : the visited flag in SSA_VAL. */
8785 : }
8786 :
8787 454127254 : visit_stmt (gsi_stmt (gsi));
8788 :
8789 454127254 : gimple *last = gsi_stmt (gsi);
8790 454127254 : e = NULL;
8791 454127254 : switch (gimple_code (last))
8792 : {
8793 143217 : case GIMPLE_SWITCH:
8794 143217 : e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8795 143217 : (as_a <gswitch *> (last))));
8796 143217 : break;
8797 25349475 : case GIMPLE_COND:
8798 25349475 : {
8799 25349475 : tree lhs = vn_valueize (gimple_cond_lhs (last));
8800 25349475 : tree rhs = vn_valueize (gimple_cond_rhs (last));
8801 25349475 : tree_code cmpcode = gimple_cond_code (last);
8802 : /* Canonicalize the comparison if needed, putting
8803 : the constant in the rhs. */
8804 25349475 : if (tree_swap_operands_p (lhs, rhs))
8805 : {
8806 874238 : std::swap (lhs, rhs);
8807 874238 : cmpcode = swap_tree_comparison (cmpcode);
8808 : }
8809 25349475 : tree val = gimple_simplify (cmpcode,
8810 : boolean_type_node, lhs, rhs,
8811 : NULL, vn_valueize);
8812 : /* If the condition didn't simplify see if we have recorded
8813 : an expression from sofar taken edges. */
8814 25349475 : if (! val || TREE_CODE (val) != INTEGER_CST)
8815 : {
8816 23386339 : vn_nary_op_t vnresult;
8817 23386339 : tree ops[2];
8818 23386339 : ops[0] = lhs;
8819 23386339 : ops[1] = rhs;
8820 23386339 : val = vn_nary_op_lookup_pieces (2, cmpcode,
8821 : boolean_type_node, ops,
8822 : &vnresult);
8823 : /* Got back a ssa name, then try looking up `val != 0`
8824 : as it might have been recorded that way. */
8825 23386339 : if (val && TREE_CODE (val) == SSA_NAME)
8826 : {
8827 163412 : ops[0] = val;
8828 163412 : ops[1] = build_zero_cst (TREE_TYPE (val));
8829 163412 : val = vn_nary_op_lookup_pieces (2, NE_EXPR,
8830 : boolean_type_node, ops,
8831 : &vnresult);
8832 : }
8833 : /* Did we get a predicated value? */
8834 23386313 : if (! val && vnresult && vnresult->predicated_values)
8835 : {
8836 1421492 : val = vn_nary_op_get_predicated_value (vnresult, bb);
8837 1421492 : if (val && dump_file && (dump_flags & TDF_DETAILS))
8838 : {
8839 2 : fprintf (dump_file, "Got predicated value ");
8840 2 : print_generic_expr (dump_file, val, TDF_NONE);
8841 2 : fprintf (dump_file, " for ");
8842 2 : print_gimple_stmt (dump_file, last, TDF_SLIM);
8843 : }
8844 : }
8845 : }
8846 23386339 : if (val)
8847 2325978 : e = find_taken_edge (bb, val);
8848 25349475 : if (! e)
8849 : {
8850 : /* If we didn't manage to compute the taken edge then
8851 : push predicated expressions for the condition itself
8852 : and related conditions to the hashtables. This allows
8853 : simplification of redundant conditions which is
8854 : important as early cleanup. */
8855 23023497 : edge true_e, false_e;
8856 23023497 : extract_true_false_edges_from_block (bb, &true_e, &false_e);
8857 569527 : if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8858 23267523 : || !can_track_predicate_on_edge (true_e))
8859 5030942 : true_e = NULL;
8860 569527 : if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8861 23243867 : || !can_track_predicate_on_edge (false_e))
8862 5969419 : false_e = NULL;
8863 23023497 : insert_predicates_for_cond (cmpcode, lhs, rhs, true_e, false_e);
8864 : }
8865 : break;
8866 : }
8867 1547 : case GIMPLE_GOTO:
8868 1547 : e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8869 1547 : break;
8870 : default:
8871 : e = NULL;
8872 : }
8873 454127254 : if (e)
8874 : {
8875 2329837 : todo = TODO_cleanup_cfg;
8876 2329837 : if (!(e->flags & EDGE_EXECUTABLE))
8877 : {
8878 1836928 : if (dump_file && (dump_flags & TDF_DETAILS))
8879 102 : fprintf (dump_file,
8880 : "marking known outgoing %sedge %d -> %d executable\n",
8881 102 : e->flags & EDGE_DFS_BACK ? "back-" : "",
8882 102 : e->src->index, e->dest->index);
8883 1836928 : e->flags |= EDGE_EXECUTABLE;
8884 1836928 : e->dest->flags |= BB_EXECUTABLE;
8885 : }
8886 492909 : else if (!(e->dest->flags & BB_EXECUTABLE))
8887 : {
8888 28142 : if (dump_file && (dump_flags & TDF_DETAILS))
8889 1 : fprintf (dump_file,
8890 : "marking destination block %d reachable\n",
8891 : e->dest->index);
8892 28142 : e->dest->flags |= BB_EXECUTABLE;
8893 : }
8894 : }
8895 451797417 : else if (gsi_one_before_end_p (gsi))
8896 : {
8897 132211972 : FOR_EACH_EDGE (e, ei, bb->succs)
8898 : {
8899 78337741 : if (!(e->flags & EDGE_EXECUTABLE))
8900 : {
8901 57449728 : if (dump_file && (dump_flags & TDF_DETAILS))
8902 19752 : fprintf (dump_file,
8903 : "marking outgoing edge %d -> %d executable\n",
8904 19752 : e->src->index, e->dest->index);
8905 57449728 : e->flags |= EDGE_EXECUTABLE;
8906 57449728 : e->dest->flags |= BB_EXECUTABLE;
8907 : }
8908 20888013 : else if (!(e->dest->flags & BB_EXECUTABLE))
8909 : {
8910 2620765 : if (dump_file && (dump_flags & TDF_DETAILS))
8911 6544 : fprintf (dump_file,
8912 : "marking destination block %d reachable\n",
8913 : e->dest->index);
8914 2620765 : e->dest->flags |= BB_EXECUTABLE;
8915 : }
8916 : }
8917 : }
8918 :
8919 : /* Eliminate. That also pushes to avail. */
8920 454127254 : if (eliminate && ! iterate)
8921 114123046 : avail.eliminate_stmt (bb, &gsi);
8922 : else
8923 : /* If not eliminating, make all not already available defs
8924 : available. But avoid picking up dead defs. */
8925 423080145 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8926 83075937 : if (! has_zero_uses (op)
8927 83075937 : && ! avail.eliminate_avail (bb, op))
8928 63156735 : avail.eliminate_push_avail (bb, op);
8929 : }
8930 :
8931 : /* Eliminate in destination PHI arguments. Always substitute in dest
8932 : PHIs, even for non-executable edges. This handles region
8933 : exits PHIs. */
8934 63549132 : if (!iterate && eliminate)
8935 34090588 : FOR_EACH_EDGE (e, ei, bb->succs)
8936 20314955 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
8937 39462986 : !gsi_end_p (gsi); gsi_next (&gsi))
8938 : {
8939 19148031 : gphi *phi = gsi.phi ();
8940 19148031 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8941 19148031 : tree arg = USE_FROM_PTR (use_p);
8942 29160913 : if (TREE_CODE (arg) != SSA_NAME
8943 19148031 : || virtual_operand_p (arg))
8944 10012882 : continue;
8945 9135149 : tree sprime;
8946 9135149 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
8947 : {
8948 124252 : sprime = SSA_VAL (arg);
8949 124252 : gcc_assert (TREE_CODE (sprime) != SSA_NAME
8950 : || SSA_NAME_IS_DEFAULT_DEF (sprime));
8951 : }
8952 : else
8953 : /* Look for sth available at the definition block of the argument.
8954 : This avoids inconsistencies between availability there which
8955 : decides if the stmt can be removed and availability at the
8956 : use site. The SSA property ensures that things available
8957 : at the definition are also available at uses. */
8958 9010897 : sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8959 : arg);
8960 9135149 : if (sprime
8961 9135149 : && sprime != arg
8962 9135149 : && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8963 1611922 : propagate_value (use_p, sprime);
8964 : }
8965 :
8966 63549132 : vn_context_bb = NULL;
8967 63549132 : return todo;
8968 : }
8969 :
8970 : /* Unwind state per basic-block. */
8971 :
8972 : struct unwind_state
8973 : {
8974 : /* Times this block has been visited. */
8975 : unsigned visited;
8976 : /* Whether to handle this as iteration point or whether to treat
8977 : incoming backedge PHI values as varying. */
8978 : bool iterate;
8979 : /* Maximum RPO index this block is reachable from. */
8980 : int max_rpo;
8981 : /* Unwind state. */
8982 : void *ob_top;
8983 : vn_reference_t ref_top;
8984 : vn_phi_t phi_top;
8985 : vn_nary_op_t nary_top;
8986 : vn_avail *avail_top;
8987 : };
8988 :
8989 : /* Record in TO the RPO VN state do_unwind unwinds to. */
8990 :
8991 : static void
8992 4565797 : record_unwind_state (unwind_state *to)
8993 : {
8994 4565797 : to->ob_top = obstack_alloc (&vn_tables_obstack, 0);
8995 4565797 : to->ref_top = last_inserted_ref;
8996 4565797 : to->phi_top = last_inserted_phi;
8997 4565797 : to->nary_top = last_inserted_nary;
8998 4565797 : to->avail_top = last_pushed_avail ? last_pushed_avail->avail : NULL;
8999 4565797 : }
9000 :
9001 : /* Unwind the RPO VN state for iteration. */
9002 :
9003 : static void
9004 1973569 : do_unwind (unwind_state *to, rpo_elim &avail)
9005 : {
9006 1973569 : gcc_assert (to->iterate);
9007 36297385 : for (; last_inserted_nary != to->nary_top;
9008 34323816 : last_inserted_nary = last_inserted_nary->next)
9009 : {
9010 34323816 : vn_nary_op_t *slot;
9011 34323816 : slot = valid_info->nary->find_slot_with_hash
9012 34323816 : (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
9013 : /* Predication causes the need to restore previous state. */
9014 34323816 : if ((*slot)->unwind_to)
9015 7003711 : *slot = (*slot)->unwind_to;
9016 : else
9017 27320105 : valid_info->nary->clear_slot (slot);
9018 : }
9019 7777394 : for (; last_inserted_phi != to->phi_top;
9020 5803825 : last_inserted_phi = last_inserted_phi->next)
9021 : {
9022 5803825 : vn_phi_t *slot;
9023 5803825 : slot = valid_info->phis->find_slot_with_hash
9024 5803825 : (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
9025 5803825 : valid_info->phis->clear_slot (slot);
9026 : }
9027 15860578 : for (; last_inserted_ref != to->ref_top;
9028 13887009 : last_inserted_ref = last_inserted_ref->next)
9029 : {
9030 13887009 : vn_reference_t *slot;
9031 13887009 : slot = valid_info->references->find_slot_with_hash
9032 13887009 : (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
9033 13887009 : (*slot)->operands.release ();
9034 13887009 : valid_info->references->clear_slot (slot);
9035 : }
9036 1973569 : obstack_free (&vn_tables_obstack, to->ob_top);
9037 :
9038 : /* Prune [rpo_idx, ] from avail. */
9039 21435899 : for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
9040 : {
9041 19462330 : vn_ssa_aux_t val = last_pushed_avail;
9042 19462330 : vn_avail *av = val->avail;
9043 19462330 : val->avail = av->next;
9044 19462330 : last_pushed_avail = av->next_undo;
9045 19462330 : av->next = avail.m_avail_freelist;
9046 19462330 : avail.m_avail_freelist = av;
9047 : }
9048 1973569 : }
9049 :
9050 : /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
9051 : If ITERATE is true then treat backedges optimistically as not
9052 : executed and iterate. If ELIMINATE is true then perform
9053 : elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
9054 : is true then force PHI nodes in ENTRY->dest to VARYING. AVAIL,
9055 : BB_TO_RPO, EXEC_IDOM and ELIMINATOR belong to the caller, which can
9056 : keep them past the walk. Elimination stops short of eliminate_cleanup, which
9057 : is left to the caller. */
9058 :
9059 : static unsigned
9060 6335585 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
9061 : bool iterate, bool eliminate, bool skip_entry_phis,
9062 : vn_lookup_kind kind, rpo_elim &avail, int *bb_to_rpo,
9063 : int *exec_idom, eliminate_dom_walker *eliminator)
9064 : {
9065 6335585 : unsigned todo = 0;
9066 6335585 : default_vn_walk_kind = kind;
9067 :
9068 : /* We currently do not support region-based iteration when
9069 : elimination is requested. */
9070 6335585 : gcc_assert (!entry || !iterate || !eliminate);
9071 : /* When iterating we need loop info up-to-date. */
9072 6335585 : gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
9073 :
9074 6335585 : bool do_region = entry != NULL;
9075 6335585 : if (!do_region)
9076 : {
9077 5624577 : entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
9078 5624577 : exit_bbs = BITMAP_ALLOC (NULL);
9079 5624577 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
9080 : }
9081 :
9082 : /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
9083 : re-mark those that are contained in the region. */
9084 6335585 : edge_iterator ei;
9085 6335585 : edge e;
9086 12729354 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
9087 6393769 : e->flags &= ~EDGE_DFS_BACK;
9088 :
9089 6335585 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
9090 6335585 : auto_vec<std::pair<int, int> > toplevel_scc_extents;
9091 6335585 : int n = rev_post_order_and_mark_dfs_back_seme
9092 8278818 : (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
9093 :
9094 6335585 : if (!do_region)
9095 5624577 : BITMAP_FREE (exit_bbs);
9096 :
9097 : /* If there are any non-DFS_BACK edges into entry->dest skip
9098 : processing PHI nodes for that block. This supports
9099 : value-numbering loop bodies w/o the actual loop. */
9100 12729353 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
9101 6393769 : if (e != entry
9102 58184 : && !(e->flags & EDGE_DFS_BACK))
9103 : break;
9104 6335585 : if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
9105 0 : fprintf (dump_file, "Region does not contain all edges into "
9106 : "the entry block, skipping its PHIs.\n");
9107 6335585 : skip_entry_phis |= e != NULL;
9108 :
9109 58435900 : for (int i = 0; i < n; ++i)
9110 52100315 : bb_to_rpo[rpo[i]] = i;
9111 6335585 : vn_bb_to_rpo = bb_to_rpo;
9112 :
9113 : /* When not iterating we can end up with executable edges from unvisited
9114 : but backward reachable blocks, so we cannot rely on the idom being
9115 : up-to-date. Initialize it with a conservative default. */
9116 6335585 : vn_exec_idom = exec_idom;
9117 6335585 : vn_exec_idom[0] = -1;
9118 52100315 : for (int i = 1; i < n; ++i)
9119 : {
9120 45764730 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9121 91529460 : vn_exec_idom[i]
9122 45764730 : = bb_to_rpo[get_immediate_dominator (CDI_DOMINATORS, bb)->index];
9123 : }
9124 :
9125 6335585 : unwind_state *rpo_state = XNEWVEC (unwind_state, n);
9126 :
9127 6335585 : rpo_avail = &avail;
9128 :
9129 : /* Create the VN state. For the initial size of the various hashtables
9130 : use a heuristic based on region size and number of SSA names. */
9131 6335585 : unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
9132 6335585 : / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
9133 6335585 : VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
9134 6335585 : next_value_id = 1;
9135 6335585 : next_constant_value_id = -1;
9136 :
9137 6335585 : vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
9138 6335585 : gcc_obstack_init (&vn_ssa_aux_obstack);
9139 :
9140 6335585 : gcc_obstack_init (&vn_tables_obstack);
9141 6335585 : gcc_obstack_init (&vn_tables_insert_obstack);
9142 6335585 : valid_info = XCNEW (struct vn_tables_s);
9143 6335585 : allocate_vn_table (valid_info, region_size);
9144 : /* Without iteration nothing is unwound so there's no need to remember
9145 : inserted expressions for re-use. */
9146 6335585 : if (iterate)
9147 4392352 : valid_info->inserted_exprs = new vn_nary_op_table_type (16);
9148 6335585 : last_inserted_ref = NULL;
9149 6335585 : last_inserted_phi = NULL;
9150 6335585 : last_inserted_nary = NULL;
9151 6335585 : last_pushed_avail = NULL;
9152 :
9153 6335585 : vn_valueize = rpo_vn_valueize;
9154 :
9155 : /* The blocks in the value-numbered region are marked with bb_in_region. */
9156 6335585 : auto_bb_flag bb_in_region (fn);
9157 6335585 : vn_bb_in_region_flag = bb_in_region;
9158 6335585 : vn_entry_edge = entry;
9159 :
9160 : /* Initialize the unwind state and edge/BB executable state. */
9161 6335585 : unsigned curr_scc = 0;
9162 58435900 : for (int i = 0; i < n; ++i)
9163 : {
9164 52100315 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9165 52100315 : bb->flags |= bb_in_region;
9166 52100315 : rpo_state[i].visited = 0;
9167 52100315 : rpo_state[i].max_rpo = i;
9168 60916823 : if (!iterate && curr_scc < toplevel_scc_extents.length ())
9169 : {
9170 7306433 : if (i >= toplevel_scc_extents[curr_scc].first
9171 7306433 : && i <= toplevel_scc_extents[curr_scc].second)
9172 4023471 : rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
9173 7306433 : if (i == toplevel_scc_extents[curr_scc].second)
9174 753827 : curr_scc++;
9175 : }
9176 52100315 : bb->flags &= ~BB_EXECUTABLE;
9177 52100315 : bool has_backedges = false;
9178 52100315 : edge e;
9179 52100315 : edge_iterator ei;
9180 123665204 : FOR_EACH_EDGE (e, ei, bb->preds)
9181 : {
9182 71564889 : if (e->flags & EDGE_DFS_BACK)
9183 2935258 : has_backedges = true;
9184 71564889 : e->flags &= ~EDGE_EXECUTABLE;
9185 71564889 : if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
9186 71564889 : continue;
9187 : }
9188 52100315 : rpo_state[i].iterate = iterate && has_backedges;
9189 : }
9190 6335585 : entry->flags |= EDGE_EXECUTABLE;
9191 6335585 : entry->dest->flags |= BB_EXECUTABLE;
9192 :
9193 : /* Verify we have no extra entries into the region. */
9194 6335585 : if (flag_checking && do_region)
9195 : {
9196 : /* We can't merge the first two loops because we cannot rely
9197 : on EDGE_DFS_BACK for edges not within the region. */
9198 2189731 : for (int i = 0; i < n; ++i)
9199 : {
9200 1478729 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9201 1478729 : edge e;
9202 1478729 : edge_iterator ei;
9203 3236302 : FOR_EACH_EDGE (e, ei, bb->preds)
9204 1757573 : gcc_assert (e == entry
9205 : || (skip_entry_phis && bb == entry->dest)
9206 : || (e->src->flags & bb_in_region));
9207 : }
9208 : }
9209 :
9210 : /* As heuristic to improve compile-time we handle only the N innermost
9211 : loops and the outermost one optimistically. */
9212 6335585 : if (iterate)
9213 : {
9214 4392352 : unsigned max_depth = param_rpo_vn_max_loop_depth;
9215 14783164 : for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
9216 1608616 : if (loop_depth (loop) > max_depth)
9217 2136 : for (unsigned i = 2;
9218 9288 : i < loop_depth (loop) - max_depth; ++i)
9219 : {
9220 2136 : basic_block header = superloop_at_depth (loop, i)->header;
9221 2136 : bool non_latch_backedge = false;
9222 2136 : edge e;
9223 2136 : edge_iterator ei;
9224 6420 : FOR_EACH_EDGE (e, ei, header->preds)
9225 4284 : if (e->flags & EDGE_DFS_BACK)
9226 : {
9227 : /* There can be a non-latch backedge into the header
9228 : which is part of an outer irreducible region. We
9229 : cannot avoid iterating this block then. */
9230 2148 : if (!dominated_by_p (CDI_DOMINATORS,
9231 2148 : e->src, e->dest))
9232 : {
9233 12 : if (dump_file && (dump_flags & TDF_DETAILS))
9234 0 : fprintf (dump_file, "non-latch backedge %d -> %d "
9235 : "forces iteration of loop %d\n",
9236 0 : e->src->index, e->dest->index, loop->num);
9237 : non_latch_backedge = true;
9238 : }
9239 : else
9240 2136 : e->flags |= EDGE_EXECUTABLE;
9241 : }
9242 2136 : rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
9243 4392352 : }
9244 : }
9245 :
9246 6335585 : uint64_t nblk = 0;
9247 6335585 : int idx = 0;
9248 4392352 : if (iterate)
9249 : /* Go and process all blocks, iterating as necessary. */
9250 50707563 : do
9251 : {
9252 50707563 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
9253 :
9254 : /* If the block has incoming backedges remember unwind state. This
9255 : is required even for non-executable blocks since in irreducible
9256 : regions we might reach them via the backedge and re-start iterating
9257 : from there.
9258 : Note we can individually mark blocks with incoming backedges to
9259 : not iterate where we then handle PHIs conservatively. We do that
9260 : heuristically to reduce compile-time for degenerate cases. */
9261 50707563 : if (rpo_state[idx].iterate)
9262 4565797 : record_unwind_state (&rpo_state[idx]);
9263 :
9264 50707563 : if (!(bb->flags & BB_EXECUTABLE))
9265 : {
9266 1043423 : if (dump_file && (dump_flags & TDF_DETAILS))
9267 2 : fprintf (dump_file, "Block %d: BB%d found not executable\n",
9268 : idx, bb->index);
9269 1043423 : idx++;
9270 3016992 : continue;
9271 : }
9272 :
9273 49664140 : if (dump_file && (dump_flags & TDF_DETAILS))
9274 338 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
9275 49664140 : nblk++;
9276 99328280 : todo |= process_bb (avail, bb,
9277 49664140 : rpo_state[idx].visited != 0,
9278 : rpo_state[idx].iterate,
9279 : iterate, eliminate, do_region, exit_bbs, false);
9280 49664140 : rpo_state[idx].visited++;
9281 :
9282 : /* Verify if changed values flow over executable outgoing backedges
9283 : and those change destination PHI values (that's the thing we
9284 : can easily verify). Reduce over all such edges to the farthest
9285 : away PHI. */
9286 49664140 : int iterate_to = -1;
9287 49664140 : edge_iterator ei;
9288 49664140 : edge e;
9289 119574314 : FOR_EACH_EDGE (e, ei, bb->succs)
9290 69910174 : if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
9291 : == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
9292 4530444 : && rpo_state[bb_to_rpo[e->dest->index]].iterate)
9293 : {
9294 4527716 : int destidx = bb_to_rpo[e->dest->index];
9295 4527716 : if (!rpo_state[destidx].visited)
9296 : {
9297 138 : if (dump_file && (dump_flags & TDF_DETAILS))
9298 0 : fprintf (dump_file, "Unvisited destination %d\n",
9299 : e->dest->index);
9300 138 : if (iterate_to == -1 || destidx < iterate_to)
9301 138 : iterate_to = destidx;
9302 138 : continue;
9303 : }
9304 4527578 : if (dump_file && (dump_flags & TDF_DETAILS))
9305 53 : fprintf (dump_file, "Looking for changed values of backedge"
9306 : " %d->%d destination PHIs\n",
9307 53 : e->src->index, e->dest->index);
9308 4527578 : vn_context_bb = e->dest;
9309 4527578 : gphi_iterator gsi;
9310 4527578 : for (gsi = gsi_start_phis (e->dest);
9311 10255367 : !gsi_end_p (gsi); gsi_next (&gsi))
9312 : {
9313 7701246 : bool inserted = false;
9314 : /* While we'd ideally just iterate on value changes
9315 : we CSE PHIs and do that even across basic-block
9316 : boundaries. So even hashtable state changes can
9317 : be important (which is roughly equivalent to
9318 : PHI argument value changes). To not excessively
9319 : iterate because of that we track whether a PHI
9320 : was CSEd to with GF_PLF_1. */
9321 7701246 : bool phival_changed;
9322 7701246 : tree res = PHI_RESULT (gsi.phi ());
9323 7701246 : tree oldval = VN_INFO (res)->valnum;
9324 7701246 : phival_changed = visit_phi (gsi.phi (), &inserted, false);
9325 : /* Only detect the change, do not commit it. Committing
9326 : it would make PHIs earlier in the destination block see
9327 : the new value of this PHI when we re-visit them, letting
9328 : one PHI cycle advance a step ahead of equivalent ones
9329 : which then are never discovered equivalent. */
9330 7701246 : if (phival_changed)
9331 1968570 : VN_INFO (res)->valnum = oldval;
9332 1968570 : if (phival_changed
9333 7159571 : || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
9334 : {
9335 1973457 : if (!phival_changed
9336 1973457 : && dump_file && (dump_flags & TDF_DETAILS))
9337 0 : fprintf (dump_file, "PHI was CSEd and hashtable "
9338 : "state (changed)\n");
9339 1973457 : if (iterate_to == -1 || destidx < iterate_to)
9340 1973434 : iterate_to = destidx;
9341 1973457 : break;
9342 : }
9343 : }
9344 4527578 : vn_context_bb = NULL;
9345 : }
9346 49664140 : if (iterate_to != -1)
9347 : {
9348 1973569 : do_unwind (&rpo_state[iterate_to], avail);
9349 1973569 : idx = iterate_to;
9350 1973569 : if (dump_file && (dump_flags & TDF_DETAILS))
9351 20 : fprintf (dump_file, "Iterating to %d BB%d\n",
9352 20 : iterate_to, rpo[iterate_to]);
9353 1973569 : continue;
9354 : }
9355 :
9356 47690571 : idx++;
9357 : }
9358 50707563 : while (idx < n);
9359 :
9360 : else /* !iterate */
9361 : {
9362 : /* Process all blocks greedily with a worklist that enforces RPO
9363 : processing of reachable blocks. */
9364 1943233 : auto_bitmap worklist;
9365 1943233 : bitmap_set_bit (worklist, 0);
9366 17771458 : while (!bitmap_empty_p (worklist))
9367 : {
9368 13884992 : int idx = bitmap_clear_first_set_bit (worklist);
9369 13884992 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
9370 13884992 : gcc_assert ((bb->flags & BB_EXECUTABLE)
9371 : && !rpo_state[idx].visited);
9372 :
9373 13884992 : if (dump_file && (dump_flags & TDF_DETAILS))
9374 37450 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
9375 :
9376 : /* When we run into predecessor edges where we cannot trust its
9377 : executable state mark them executable so PHI processing will
9378 : be conservative.
9379 : ??? Do we need to force arguments flowing over that edge
9380 : to be varying or will they even always be? */
9381 13884992 : edge_iterator ei;
9382 13884992 : edge e;
9383 33668273 : FOR_EACH_EDGE (e, ei, bb->preds)
9384 19783281 : if (!(e->flags & EDGE_EXECUTABLE)
9385 1058777 : && (bb == entry->dest
9386 1003283 : || (!rpo_state[bb_to_rpo[e->src->index]].visited
9387 963661 : && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
9388 : >= (int)idx))))
9389 : {
9390 995068 : if (dump_file && (dump_flags & TDF_DETAILS))
9391 12016 : fprintf (dump_file, "Cannot trust state of predecessor "
9392 : "edge %d -> %d, marking executable\n",
9393 12016 : e->src->index, e->dest->index);
9394 995068 : e->flags |= EDGE_EXECUTABLE;
9395 : }
9396 :
9397 13884992 : nblk++;
9398 27769984 : todo |= process_bb (avail, bb, false, false, false, eliminate,
9399 : do_region, exit_bbs,
9400 52682 : skip_entry_phis && bb == entry->dest);
9401 13884992 : rpo_state[idx].visited++;
9402 :
9403 34332953 : FOR_EACH_EDGE (e, ei, bb->succs)
9404 20447961 : if ((e->flags & EDGE_EXECUTABLE)
9405 20365514 : && e->dest->index != EXIT_BLOCK
9406 19163228 : && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
9407 38218708 : && !rpo_state[bb_to_rpo[e->dest->index]].visited)
9408 16781271 : bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
9409 : }
9410 1943233 : }
9411 :
9412 : /* If statistics or dump file active. */
9413 6335585 : int nex = 0;
9414 6335585 : unsigned max_visited = 1;
9415 58435900 : for (int i = 0; i < n; ++i)
9416 : {
9417 52100315 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9418 52100315 : if (bb->flags & BB_EXECUTABLE)
9419 51460966 : nex++;
9420 52100315 : statistics_histogram_event (cfun, "RPO block visited times",
9421 52100315 : rpo_state[i].visited);
9422 52100315 : if (rpo_state[i].visited > max_visited)
9423 : max_visited = rpo_state[i].visited;
9424 : }
9425 6335585 : unsigned nvalues = 0, navail = 0;
9426 176756353 : for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
9427 176756353 : i != vn_ssa_aux_hash->end (); ++i)
9428 : {
9429 170420768 : nvalues++;
9430 170420768 : vn_avail *av = (*i)->avail;
9431 251818155 : while (av)
9432 : {
9433 81397387 : navail++;
9434 81397387 : av = av->next;
9435 : }
9436 : }
9437 6335585 : statistics_counter_event (cfun, "RPO blocks", n);
9438 6335585 : statistics_counter_event (cfun, "RPO blocks visited", nblk);
9439 6335585 : statistics_counter_event (cfun, "RPO blocks executable", nex);
9440 6335585 : statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
9441 6335585 : statistics_histogram_event (cfun, "RPO num values", nvalues);
9442 6335585 : statistics_histogram_event (cfun, "RPO num avail", navail);
9443 6335585 : statistics_histogram_event (cfun, "RPO num lattice",
9444 6335585 : vn_ssa_aux_hash->elements ());
9445 6335585 : if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
9446 : {
9447 11920 : fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
9448 : " blocks in total discovering %d executable blocks iterating "
9449 : "%d.%d times, a block was visited max. %u times\n",
9450 : n, nblk, nex,
9451 11920 : (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
9452 : max_visited);
9453 11920 : fprintf (dump_file, "RPO tracked %d values available at %d locations "
9454 : "and %" PRIu64 " lattice elements\n",
9455 11920 : nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
9456 : }
9457 :
9458 6335585 : if (eliminate)
9459 : {
9460 : /* When !iterate we already performed elimination during the RPO
9461 : walk. */
9462 5328162 : if (iterate)
9463 : {
9464 : /* Elimination for region-based VN needs to be done within the
9465 : RPO walk. */
9466 3404619 : gcc_assert (! do_region);
9467 : /* Note we can't use avail.walk here because that gets confused
9468 : by the existing availability and it will be less efficient
9469 : as well. */
9470 3404619 : rpo_avail = eliminator;
9471 3404619 : eliminator->walk (ENTRY_BLOCK_PTR_FOR_FN (fn));
9472 : }
9473 : }
9474 :
9475 : /* Clear the bb_in_region flag. */
9476 58435900 : for (int i = 0; i < n; ++i)
9477 : {
9478 52100315 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9479 52100315 : bb->flags &= ~bb_in_region;
9480 : }
9481 :
9482 6335585 : vn_valueize = NULL;
9483 6335585 : rpo_avail = NULL;
9484 6335585 : vn_bb_to_rpo = NULL;
9485 6335585 : vn_bb_in_region_flag = 0;
9486 6335585 : vn_entry_edge = NULL;
9487 :
9488 6335585 : vn_exec_idom = NULL;
9489 :
9490 6335585 : XDELETEVEC (rpo);
9491 6335585 : XDELETEVEC (rpo_state);
9492 :
9493 6335585 : return todo;
9494 6335585 : }
9495 :
9496 : /* Region-based entry for RPO VN. Performs value-numbering and elimination
9497 : on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
9498 : the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
9499 : are not considered.
9500 : If ITERATE is true then treat backedges optimistically as not
9501 : executed and iterate. If ELIMINATE is true then perform
9502 : elimination, otherwise leave that to the caller.
9503 : If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
9504 : KIND specifies the amount of work done for handling memory operations. */
9505 :
9506 : unsigned
9507 730698 : do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
9508 : bool iterate, bool eliminate, bool skip_entry_phis,
9509 : vn_lookup_kind kind)
9510 : {
9511 730698 : auto_timevar tv (TV_TREE_RPO_VN);
9512 :
9513 730698 : vn_driver vn (fn, entry, exit_bbs, iterate, eliminate,
9514 730698 : skip_entry_phis, kind);
9515 730698 : return vn.finalize ();
9516 730698 : }
9517 :
9518 5604887 : vn_driver::vn_driver (function *fn, bool iterate, bool eliminate,
9519 : bool want_value_ids, vn_lookup_kind kind)
9520 5604887 : : vn_driver (fn, NULL, NULL, iterate, eliminate, false, kind)
9521 : {
9522 5604887 : if (want_value_ids)
9523 987733 : assign_value_ids ();
9524 5604887 : }
9525 :
9526 : /* Value-number the region ENTRY leads into, which exits to EXIT_BBS, or all of
9527 : FN if ENTRY is NULL. */
9528 :
9529 6335585 : vn_driver::vn_driver (function *fn, edge entry, bitmap exit_bbs,
9530 : bool iterate, bool eliminate, bool skip_entry_phis,
9531 : vn_lookup_kind kind)
9532 6335585 : : m_eliminate (eliminate), m_region_p (entry != NULL)
9533 : {
9534 6335585 : basic_block entry_bb
9535 6335585 : = entry ? entry->dest : single_succ (ENTRY_BLOCK_PTR_FOR_FN (fn));
9536 6335585 : m_avail = new rpo_elim (entry_bb);
9537 6335585 : m_bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
9538 6335585 : m_exec_idom = XNEWVEC (int, n_basic_blocks_for_fn (fn));
9539 : /* When iterating, elimination is a walk of its own. */
9540 12671170 : m_eliminator = (iterate && eliminate
9541 6335585 : ? new eliminate_dom_walker (CDI_DOMINATORS, NULL) : m_avail);
9542 12671170 : m_todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
9543 6335585 : skip_entry_phis, kind, *m_avail, m_bb_to_rpo,
9544 : m_exec_idom, m_eliminator);
9545 6335585 : }
9546 :
9547 6335585 : vn_driver::~vn_driver ()
9548 : {
9549 6335585 : free_rpo_vn ();
9550 6335585 : XDELETEVEC (m_bb_to_rpo);
9551 6335585 : XDELETEVEC (m_exec_idom);
9552 6335585 : if (m_eliminator != m_avail)
9553 3404619 : delete m_eliminator;
9554 6335585 : delete m_avail;
9555 6335585 : }
9556 :
9557 : /* Finish elimination and return the TODO flags. */
9558 :
9559 : unsigned
9560 6335585 : vn_driver::finalize (bitmap inserted_exprs)
9561 : {
9562 : /* If ELIMINATE was passed to the constructor, finish the elimination. */
9563 6335585 : if (m_eliminate)
9564 : {
9565 5328162 : vn_bb_to_rpo = m_bb_to_rpo;
9566 5328162 : vn_exec_idom = m_exec_idom;
9567 5328162 : unsigned todo = m_todo | m_eliminator->eliminate_cleanup (m_region_p);
9568 5328162 : vn_bb_to_rpo = NULL;
9569 5328162 : vn_exec_idom = NULL;
9570 5328162 : return todo;
9571 : }
9572 :
9573 : /* PRE eliminates after its insertions, when the RPO map no longer
9574 : describes the IL. Return only its flags, as PRE always has, to avoid
9575 : an extra CFG cleanup. */
9576 1007423 : if (inserted_exprs)
9577 987733 : return eliminate_with_rpo_vn (inserted_exprs);
9578 :
9579 19690 : return m_todo;
9580 : }
9581 :
9582 :
9583 : namespace {
9584 :
9585 : const pass_data pass_data_fre =
9586 : {
9587 : GIMPLE_PASS, /* type */
9588 : "fre", /* name */
9589 : OPTGROUP_NONE, /* optinfo_flags */
9590 : TV_TREE_FRE, /* tv_id */
9591 : ( PROP_cfg | PROP_ssa ), /* properties_required */
9592 : 0, /* properties_provided */
9593 : 0, /* properties_destroyed */
9594 : 0, /* todo_flags_start */
9595 : 0, /* todo_flags_finish */
9596 : };
9597 :
9598 : class pass_fre : public gimple_opt_pass
9599 : {
9600 : public:
9601 1514925 : pass_fre (gcc::context *ctxt)
9602 3029850 : : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
9603 : {}
9604 :
9605 : /* opt_pass methods: */
9606 1211940 : opt_pass * clone () final override { return new pass_fre (m_ctxt); }
9607 1514925 : void set_pass_param (unsigned int n, bool param) final override
9608 : {
9609 1514925 : gcc_assert (n == 0);
9610 1514925 : may_iterate = param;
9611 1514925 : }
9612 4699318 : bool gate (function *) final override
9613 : {
9614 4699318 : return flag_tree_fre != 0 && (may_iterate || optimize > 1);
9615 : }
9616 : unsigned int execute (function *) final override;
9617 :
9618 : private:
9619 : bool may_iterate;
9620 : }; // class pass_fre
9621 :
9622 : unsigned int
9623 4617154 : pass_fre::execute (function *fun)
9624 : {
9625 4617154 : unsigned todo = 0;
9626 :
9627 : /* At -O[1g] use the cheap non-iterating mode. */
9628 4617154 : bool iterate_p = may_iterate && (optimize > 1);
9629 4617154 : calculate_dominance_info (CDI_DOMINATORS);
9630 4617154 : if (iterate_p)
9631 : /* When iterating make sure to minimize the number of backedges. */
9632 3404619 : loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES);
9633 :
9634 4617154 : {
9635 4617154 : vn_driver vn (fun, iterate_p, true);
9636 4617154 : todo = vn.finalize ();
9637 4617154 : }
9638 :
9639 4617154 : if (iterate_p)
9640 3404619 : loop_optimizer_finalize ();
9641 :
9642 4617154 : if (scev_initialized_p ())
9643 34290 : scev_reset_htab ();
9644 :
9645 : /* For late FRE after IVOPTs and unrolling, see if we can
9646 : remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
9647 4617154 : if (!may_iterate)
9648 1021812 : todo |= TODO_update_address_taken;
9649 :
9650 4617154 : return todo;
9651 : }
9652 :
9653 : } // anon namespace
9654 :
9655 : gimple_opt_pass *
9656 302985 : make_pass_fre (gcc::context *ctxt)
9657 : {
9658 302985 : return new pass_fre (ctxt);
9659 : }
9660 :
9661 : #undef BB_EXECUTABLE
|