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 776374000 : vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
158 : {
159 776374000 : 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 984632051 : vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
167 : {
168 984632051 : 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 25674854 : vn_phi_hasher::hash (const vn_phi_s *vp1)
190 : {
191 25674854 : return vp1->hashcode;
192 : }
193 :
194 : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
195 :
196 : inline bool
197 46220377 : vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
198 : {
199 46220377 : 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 26174467 : vn_reference_op_eq (const void *p1, const void *p2)
211 : {
212 26174467 : const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
213 26174467 : const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
214 :
215 26174467 : return (vro1->opcode == vro2->opcode
216 : /* We do not care for differences in type qualification. */
217 26172669 : && (vro1->type == vro2->type
218 1197933 : || (vro1->type && vro2->type
219 1197933 : && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
220 1197933 : TYPE_MAIN_VARIANT (vro2->type))))
221 25165488 : && expressions_equal_p (vro1->op0, vro2->op0)
222 25123619 : && expressions_equal_p (vro1->op1, vro2->op1)
223 25123619 : && expressions_equal_p (vro1->op2, vro2->op2)
224 51298086 : && (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 3778968001 : vn_reference_hasher::hash (const vn_reference_s *vr1)
248 : {
249 3778968001 : return vr1->hashcode;
250 : }
251 :
252 : inline bool
253 4498661416 : vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
254 : {
255 4498661416 : 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 : } *vn_tables_t;
332 :
333 :
334 : /* vn_constant hashtable helpers. */
335 :
336 : struct vn_constant_hasher : free_ptr_hash <vn_constant_s>
337 : {
338 : static inline hashval_t hash (const vn_constant_s *);
339 : static inline bool equal (const vn_constant_s *, const vn_constant_s *);
340 : };
341 :
342 : /* Hash table hash function for vn_constant_t. */
343 :
344 : inline hashval_t
345 12438157 : vn_constant_hasher::hash (const vn_constant_s *vc1)
346 : {
347 12438157 : return vc1->hashcode;
348 : }
349 :
350 : /* Hash table equality function for vn_constant_t. */
351 :
352 : inline bool
353 15002354 : vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
354 : {
355 15002354 : if (vc1->hashcode != vc2->hashcode)
356 : return false;
357 :
358 2262835 : return vn_constant_eq_with_type (vc1->constant, vc2->constant);
359 : }
360 :
361 : static hash_table<vn_constant_hasher> *constant_to_value_id;
362 :
363 :
364 : /* Obstack we allocate the vn-tables elements from. */
365 : static obstack vn_tables_obstack;
366 : /* Special obstack we never unwind. */
367 : static obstack vn_tables_insert_obstack;
368 :
369 : static vn_reference_t last_inserted_ref;
370 : static vn_phi_t last_inserted_phi;
371 : static vn_nary_op_t last_inserted_nary;
372 : static vn_ssa_aux_t last_pushed_avail;
373 :
374 : /* Valid hashtables storing information we have proven to be
375 : correct. */
376 : static vn_tables_t valid_info;
377 :
378 : /* Global RPO state for access from hooks. */
379 : static class eliminate_dom_walker *rpo_avail;
380 : basic_block vn_context_bb;
381 : int *vn_bb_to_rpo;
382 :
383 :
384 : /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
385 : an SSA name, otherwise just return it. */
386 : tree (*vn_valueize) (tree);
387 : static tree
388 82284 : vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
389 : {
390 82284 : basic_block saved_vn_context_bb = vn_context_bb;
391 : /* Look for sth available at the definition block of the argument.
392 : This avoids inconsistencies between availability there which
393 : decides if the stmt can be removed and availability at the
394 : use site. The SSA property ensures that things available
395 : at the definition are also available at uses. */
396 82284 : if (!SSA_NAME_IS_DEFAULT_DEF (t))
397 78419 : vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
398 82284 : tree res = vn_valueize (t);
399 82284 : vn_context_bb = saved_vn_context_bb;
400 82284 : return res;
401 : }
402 :
403 :
404 : /* This represents the top of the VN lattice, which is the universal
405 : value. */
406 :
407 : tree VN_TOP;
408 :
409 : /* Unique counter for our value ids. */
410 :
411 : static unsigned int next_value_id;
412 : static int next_constant_value_id;
413 :
414 :
415 : /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
416 : are allocated on an obstack for locality reasons, and to free them
417 : without looping over the vec. */
418 :
419 : struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
420 : {
421 : typedef vn_ssa_aux_t value_type;
422 : typedef tree compare_type;
423 : static inline hashval_t hash (const value_type &);
424 : static inline bool equal (const value_type &, const compare_type &);
425 : static inline void mark_deleted (value_type &) {}
426 : static const bool empty_zero_p = true;
427 0 : static inline void mark_empty (value_type &e) { e = NULL; }
428 : static inline bool is_deleted (value_type &) { return false; }
429 >14195*10^7 : static inline bool is_empty (value_type &e) { return e == NULL; }
430 : };
431 :
432 : hashval_t
433 46721062677 : vn_ssa_aux_hasher::hash (const value_type &entry)
434 : {
435 46721062677 : return SSA_NAME_VERSION (entry->name);
436 : }
437 :
438 : bool
439 53462289929 : vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
440 : {
441 53462289929 : return name == entry->name;
442 : }
443 :
444 : static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
445 : typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
446 : static struct obstack vn_ssa_aux_obstack;
447 :
448 : static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
449 : static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
450 : vn_nary_op_table_type *);
451 : static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
452 : enum tree_code, tree, tree *);
453 : static tree vn_lookup_simplify_result (gimple_match_op *);
454 : static vn_reference_t vn_reference_lookup_or_insert_for_pieces
455 : (tree, alias_set_type, alias_set_type, poly_int64, poly_int64, tree,
456 : vec<vn_reference_op_s, va_heap>, tree);
457 :
458 : /* Return whether there is value numbering information for a given SSA name. */
459 :
460 : bool
461 5265887 : has_VN_INFO (tree name)
462 : {
463 5265887 : return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
464 : }
465 :
466 : vn_ssa_aux_t
467 4189242578 : VN_INFO (tree name)
468 : {
469 4189242578 : vn_ssa_aux_t *res
470 4189242578 : = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
471 : INSERT);
472 4189242578 : if (*res != NULL)
473 : return *res;
474 :
475 177056638 : vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
476 177056638 : memset (newinfo, 0, sizeof (struct vn_ssa_aux));
477 177056638 : newinfo->name = name;
478 177056638 : newinfo->valnum = VN_TOP;
479 : /* We are using the visited flag to handle uses with defs not within the
480 : region being value-numbered. */
481 177056638 : newinfo->visited = false;
482 :
483 : /* Given we create the VN_INFOs on-demand now we have to do initialization
484 : different than VN_TOP here. */
485 177056638 : if (SSA_NAME_IS_DEFAULT_DEF (name))
486 9545392 : switch (TREE_CODE (SSA_NAME_VAR (name)))
487 : {
488 1715212 : case VAR_DECL:
489 : /* All undefined vars are VARYING. */
490 1715212 : newinfo->valnum = name;
491 1715212 : newinfo->visited = true;
492 1715212 : break;
493 :
494 7769061 : case PARM_DECL:
495 : /* Parameters are VARYING but we can record a condition
496 : if we know it is a non-NULL pointer. */
497 7769061 : newinfo->visited = true;
498 7769061 : newinfo->valnum = name;
499 11953896 : if (POINTER_TYPE_P (TREE_TYPE (name))
500 8948121 : && nonnull_arg_p (SSA_NAME_VAR (name)))
501 : {
502 2417637 : tree ops[2];
503 2417637 : ops[0] = name;
504 2417637 : ops[1] = build_int_cst (TREE_TYPE (name), 0);
505 2417637 : vn_nary_op_t nary;
506 : /* Allocate from non-unwinding stack. */
507 2417637 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
508 2417637 : init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
509 : boolean_type_node, ops);
510 2417637 : nary->predicated_values = 0;
511 2417637 : nary->u.result = boolean_true_node;
512 2417637 : vn_nary_op_insert_into (nary, valid_info->nary);
513 2417637 : gcc_assert (nary->unwind_to == NULL);
514 : /* Also do not link it into the undo chain. */
515 2417637 : last_inserted_nary = nary->next;
516 2417637 : nary->next = (vn_nary_op_t)(void *)-1;
517 2417637 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
518 2417637 : init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
519 : boolean_type_node, ops);
520 2417637 : nary->predicated_values = 0;
521 2417637 : nary->u.result = boolean_false_node;
522 2417637 : vn_nary_op_insert_into (nary, valid_info->nary);
523 2417637 : gcc_assert (nary->unwind_to == NULL);
524 2417637 : last_inserted_nary = nary->next;
525 2417637 : nary->next = (vn_nary_op_t)(void *)-1;
526 2417637 : if (dump_file && (dump_flags & TDF_DETAILS))
527 : {
528 38 : fprintf (dump_file, "Recording ");
529 38 : print_generic_expr (dump_file, name, TDF_SLIM);
530 38 : fprintf (dump_file, " != 0\n");
531 : }
532 : }
533 : break;
534 :
535 61119 : case RESULT_DECL:
536 : /* If the result is passed by invisible reference the default
537 : def is initialized, otherwise it's uninitialized. Still
538 : undefined is varying. */
539 61119 : newinfo->visited = true;
540 61119 : newinfo->valnum = name;
541 61119 : break;
542 :
543 0 : default:
544 0 : gcc_unreachable ();
545 : }
546 : return newinfo;
547 : }
548 :
549 : /* Return the SSA value of X. */
550 :
551 : inline tree
552 3509598574 : SSA_VAL (tree x, bool *visited = NULL)
553 : {
554 3509598574 : vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
555 3509598574 : if (visited)
556 1429657764 : *visited = tem && tem->visited;
557 3509598574 : return tem && tem->visited ? tem->valnum : x;
558 : }
559 :
560 : /* Return the SSA value of the VUSE x, supporting released VDEFs
561 : during elimination which will value-number the VDEF to the
562 : associated VUSE (but not substitute in the whole lattice). */
563 :
564 : static inline tree
565 1292764408 : vuse_ssa_val (tree x)
566 : {
567 1292764408 : if (!x)
568 : return NULL_TREE;
569 :
570 1289292130 : do
571 : {
572 1289292130 : x = SSA_VAL (x);
573 1289292130 : gcc_assert (x != VN_TOP);
574 : }
575 1289292130 : while (SSA_NAME_IN_FREE_LIST (x));
576 :
577 : return x;
578 : }
579 :
580 : /* Similar to the above but used as callback for walk_non_aliased_vuses
581 : and thus should stop at unvisited VUSE to not walk across region
582 : boundaries. */
583 :
584 : static tree
585 1091964879 : vuse_valueize (tree vuse)
586 : {
587 1091964879 : do
588 : {
589 1091964879 : bool visited;
590 1091964879 : vuse = SSA_VAL (vuse, &visited);
591 1091964879 : if (!visited)
592 16455342 : return NULL_TREE;
593 1075509537 : gcc_assert (vuse != VN_TOP);
594 : }
595 1075509537 : while (SSA_NAME_IN_FREE_LIST (vuse));
596 : return vuse;
597 : }
598 :
599 :
600 : /* Return the vn_kind the expression computed by the stmt should be
601 : associated with. */
602 :
603 : enum vn_kind
604 104886929 : vn_get_stmt_kind (gimple *stmt)
605 : {
606 104886929 : switch (gimple_code (stmt))
607 : {
608 : case GIMPLE_CALL:
609 : return VN_REFERENCE;
610 : case GIMPLE_PHI:
611 : return VN_PHI;
612 104886929 : case GIMPLE_ASSIGN:
613 104886929 : {
614 104886929 : enum tree_code code = gimple_assign_rhs_code (stmt);
615 104886929 : tree rhs1 = gimple_assign_rhs1 (stmt);
616 104886929 : switch (get_gimple_rhs_class (code))
617 : {
618 : case GIMPLE_UNARY_RHS:
619 : case GIMPLE_BINARY_RHS:
620 : case GIMPLE_TERNARY_RHS:
621 : return VN_NARY;
622 49344030 : case GIMPLE_SINGLE_RHS:
623 49344030 : switch (TREE_CODE_CLASS (code))
624 : {
625 37220680 : case tcc_reference:
626 : /* VOP-less references can go through unary case. */
627 37220680 : if ((code == REALPART_EXPR
628 : || code == IMAGPART_EXPR
629 37220680 : || code == VIEW_CONVERT_EXPR
630 37220680 : || code == BIT_FIELD_REF)
631 37220680 : && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
632 673865 : || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
633 : return VN_NARY;
634 :
635 : /* Fallthrough. */
636 : case tcc_declaration:
637 : return VN_REFERENCE;
638 :
639 : case tcc_constant:
640 : return VN_CONSTANT;
641 :
642 6096619 : default:
643 6096619 : if (code == ADDR_EXPR)
644 3305956 : return (is_gimple_min_invariant (rhs1)
645 3305956 : ? VN_CONSTANT : VN_REFERENCE);
646 2790663 : else if (code == CONSTRUCTOR)
647 : return VN_NARY;
648 : return VN_NONE;
649 : }
650 : default:
651 : return VN_NONE;
652 : }
653 : }
654 : default:
655 : return VN_NONE;
656 : }
657 : }
658 :
659 : /* Lookup a value id for CONSTANT and return it. If it does not
660 : exist returns 0. */
661 :
662 : unsigned int
663 0 : get_constant_value_id (tree constant)
664 : {
665 0 : vn_constant_s **slot;
666 0 : struct vn_constant_s vc;
667 :
668 0 : vc.hashcode = vn_hash_constant_with_type (constant);
669 0 : vc.constant = constant;
670 0 : slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
671 0 : if (slot)
672 0 : return (*slot)->value_id;
673 : return 0;
674 : }
675 :
676 : /* Lookup a value id for CONSTANT, and if it does not exist, create a
677 : new one and return it. If it does exist, return it. */
678 :
679 : unsigned int
680 29308539 : get_or_alloc_constant_value_id (tree constant)
681 : {
682 29308539 : vn_constant_s **slot;
683 29308539 : struct vn_constant_s vc;
684 29308539 : vn_constant_t vcp;
685 :
686 : /* If the hashtable isn't initialized we're not running from PRE and thus
687 : do not need value-ids. */
688 29308539 : if (!constant_to_value_id)
689 : return 0;
690 :
691 4825598 : vc.hashcode = vn_hash_constant_with_type (constant);
692 4825598 : vc.constant = constant;
693 4825598 : slot = constant_to_value_id->find_slot (&vc, INSERT);
694 4825598 : if (*slot)
695 2245144 : return (*slot)->value_id;
696 :
697 2580454 : vcp = XNEW (struct vn_constant_s);
698 2580454 : vcp->hashcode = vc.hashcode;
699 2580454 : vcp->constant = constant;
700 2580454 : vcp->value_id = get_next_constant_value_id ();
701 2580454 : *slot = vcp;
702 2580454 : return vcp->value_id;
703 : }
704 :
705 : /* Compute the hash for a reference operand VRO1. */
706 :
707 : static void
708 139105761 : vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
709 : {
710 139105761 : hstate.add_int (vro1->opcode);
711 139105761 : if (vro1->opcode == CALL_EXPR && !vro1->op0)
712 559756 : hstate.add_int (vro1->clique);
713 139105761 : if (vro1->op0)
714 132596919 : inchash::add_expr (vro1->op0, hstate);
715 139105761 : if (vro1->op1)
716 12002312 : inchash::add_expr (vro1->op1, hstate);
717 139105761 : if (vro1->op2)
718 13767678 : inchash::add_expr (vro1->op2, hstate);
719 139105761 : }
720 :
721 : /* Compute a hash for the reference operation VR1 and return it. */
722 :
723 : hashval_t
724 207505557 : vn_reference_compute_hash (const vn_reference_t vr1)
725 : {
726 207505557 : inchash::hash hstate;
727 207505557 : hashval_t result;
728 207505557 : int i;
729 207505557 : vn_reference_op_t vro;
730 207505557 : poly_offset_int off = -1;
731 207505557 : bool deref = false;
732 :
733 844808893 : FOR_EACH_VEC_ELT (vr1->operands, i, vro)
734 : {
735 637303336 : if (vro->opcode == MEM_REF)
736 : deref = true;
737 440617032 : else if (vro->opcode != ADDR_EXPR)
738 309214083 : deref = false;
739 637303336 : if (maybe_ne (vro->off, -1))
740 : {
741 375125068 : if (known_eq (off, -1))
742 199075302 : off = 0;
743 637303336 : off += vro->off;
744 : }
745 : else
746 : {
747 262178268 : if (maybe_ne (off, -1)
748 262178268 : && maybe_ne (off, 0))
749 105822646 : hstate.add_poly_hwi (off.force_shwi ());
750 262178268 : off = -1;
751 262178268 : if (deref
752 123293932 : && vro->opcode == ADDR_EXPR)
753 : {
754 123072507 : if (vro->op0)
755 : {
756 123072507 : tree op = TREE_OPERAND (vro->op0, 0);
757 123072507 : hstate.add_int (TREE_CODE (op));
758 123072507 : inchash::add_expr (op, hstate);
759 : }
760 : }
761 : else
762 139105761 : vn_reference_op_compute_hash (vro, hstate);
763 : }
764 : }
765 : /* Do not hash vr1->offset or vr1->max_size, we want to get collisions
766 : to be able to identify compatible results. */
767 207505557 : result = hstate.end ();
768 : /* ??? We would ICE later if we hash instead of adding that in. */
769 207505557 : if (vr1->vuse)
770 202457175 : result += SSA_NAME_VERSION (vr1->vuse);
771 :
772 207505557 : return result;
773 : }
774 :
775 : /* Return true if reference operations VR1 and VR2 are equivalent. This
776 : means they have the same set of operands and vuses. If LEXICAL
777 : is true then the full access path has to be the same. */
778 :
779 : bool
780 4493776029 : vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2,
781 : bool lexical)
782 : {
783 4493776029 : unsigned i, j;
784 :
785 : /* Early out if this is not a hash collision. */
786 4493776029 : if (vr1->hashcode != vr2->hashcode)
787 : return false;
788 :
789 : /* The VOP needs to be the same. */
790 18187445 : if (vr1->vuse != vr2->vuse)
791 : return false;
792 :
793 : /* The offset/max_size used for the ao_ref during lookup has to be
794 : the same. */
795 18186980 : if (maybe_ne (vr1->offset, vr2->offset)
796 18186980 : || maybe_ne (vr1->max_size, vr2->max_size))
797 : {
798 : /* But nothing known in the prevailing entry is OK to be used. */
799 7066922 : if (maybe_ne (vr1->offset, 0) || known_size_p (vr1->max_size))
800 : return false;
801 : }
802 :
803 : /* If the operands are the same we are done. */
804 36281112 : if (vr1->operands == vr2->operands)
805 : return true;
806 :
807 18140556 : if (!vr1->type || !vr2->type)
808 : {
809 575168 : if (vr1->type != vr2->type)
810 : return false;
811 : }
812 17565388 : else if (vr1->type == vr2->type)
813 : ;
814 2273386 : else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
815 2273386 : || (COMPLETE_TYPE_P (vr1->type)
816 2273386 : && !expressions_equal_p (TYPE_SIZE (vr1->type),
817 2273386 : TYPE_SIZE (vr2->type))))
818 : return false;
819 1467586 : else if (vr1->operands[0].opcode == CALL_EXPR
820 1467586 : && !types_compatible_p (vr1->type, vr2->type))
821 : return false;
822 1467586 : else if (INTEGRAL_TYPE_P (vr1->type)
823 587488 : && INTEGRAL_TYPE_P (vr2->type))
824 : {
825 547344 : if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
826 : return false;
827 : }
828 920242 : else if (INTEGRAL_TYPE_P (vr1->type)
829 920242 : && (TYPE_PRECISION (vr1->type)
830 40144 : != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
831 : return false;
832 920196 : else if (INTEGRAL_TYPE_P (vr2->type)
833 920196 : && (TYPE_PRECISION (vr2->type)
834 9472 : != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
835 : return false;
836 19688 : else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
837 919601 : && VECTOR_BOOLEAN_TYPE_P (vr2->type))
838 : {
839 : /* Vector boolean types can have padding, verify we are dealing with
840 : the same number of elements, aka the precision of the types.
841 : For example, In most architecture the precision_size of vbool*_t
842 : types are calculated like below:
843 : precision_size = type_size * 8
844 :
845 : Unfortunately, the RISC-V will adjust the precision_size for the
846 : vbool*_t in order to align the ISA as below:
847 : type_size = [1, 1, 1, 1, 2, 4, 8]
848 : precision_size = [1, 2, 4, 8, 16, 32, 64]
849 :
850 : Then the precision_size of RISC-V vbool*_t will not be the multiple
851 : of the type_size. We take care of this case consolidated here. */
852 0 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1->type),
853 0 : TYPE_VECTOR_SUBPARTS (vr2->type)))
854 : return false;
855 : }
856 919601 : else if (TYPE_MODE (vr1->type) != TYPE_MODE (vr2->type)
857 919601 : && (!mode_can_transfer_bits (TYPE_MODE (vr1->type))
858 45252 : || !mode_can_transfer_bits (TYPE_MODE (vr2->type))))
859 : return false;
860 :
861 17221363 : i = 0;
862 17221363 : j = 0;
863 22266037 : do
864 : {
865 22266037 : poly_offset_int off1 = 0, off2 = 0;
866 22266037 : vn_reference_op_t vro1, vro2;
867 22266037 : vn_reference_op_s tem1, tem2;
868 22266037 : bool deref1 = false, deref2 = false;
869 22266037 : bool reverse1 = false, reverse2 = false;
870 72571053 : for (; vr1->operands.iterate (i, &vro1); i++)
871 : {
872 50305016 : if (vro1->opcode == MEM_REF)
873 : deref1 = true;
874 : /* Do not look through a storage order barrier. */
875 34457737 : else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
876 75712 : return false;
877 50305016 : reverse1 |= vro1->reverse;
878 50305016 : if (lexical || known_eq (vro1->off, -1))
879 : break;
880 28038979 : off1 += vro1->off;
881 : }
882 50471865 : for (; vr2->operands.iterate (j, &vro2); j++)
883 : {
884 50471865 : if (vro2->opcode == MEM_REF)
885 : deref2 = true;
886 : /* Do not look through a storage order barrier. */
887 34599255 : else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
888 : return false;
889 50471865 : reverse2 |= vro2->reverse;
890 50471865 : if (lexical || known_eq (vro2->off, -1))
891 : break;
892 28205828 : off2 += vro2->off;
893 : }
894 22266037 : if (maybe_ne (off1, off2) || reverse1 != reverse2)
895 : return false;
896 22265932 : if (deref1 && vro1->opcode == ADDR_EXPR)
897 : {
898 8385386 : memset (&tem1, 0, sizeof (tem1));
899 8385386 : tem1.op0 = TREE_OPERAND (vro1->op0, 0);
900 8385386 : tem1.type = TREE_TYPE (tem1.op0);
901 8385386 : tem1.opcode = TREE_CODE (tem1.op0);
902 8385386 : vro1 = &tem1;
903 8385386 : deref1 = false;
904 : }
905 22265932 : if (deref2 && vro2->opcode == ADDR_EXPR)
906 : {
907 8385392 : memset (&tem2, 0, sizeof (tem2));
908 8385392 : tem2.op0 = TREE_OPERAND (vro2->op0, 0);
909 8385392 : tem2.type = TREE_TYPE (tem2.op0);
910 8385392 : tem2.opcode = TREE_CODE (tem2.op0);
911 8385392 : vro2 = &tem2;
912 8385392 : deref2 = false;
913 : }
914 22265932 : if (deref1 != deref2)
915 : return false;
916 22206349 : if (!vn_reference_op_eq (vro1, vro2))
917 : return false;
918 : /* Both alignment and alias set are not relevant for the produced
919 : value but need to be included when doing lexical comparison.
920 : We also need to make sure that the access path ends in an
921 : access of the same size as otherwise we might assume an access
922 : may not trap while in fact it might. */
923 22194111 : if (lexical
924 2280518 : && (vro1->opcode == MEM_REF
925 2280518 : || vro1->opcode == TARGET_MEM_REF)
926 22939970 : && (TYPE_ALIGN (vro1->type) != TYPE_ALIGN (vro2->type)
927 745652 : || (TYPE_SIZE (vro1->type) != TYPE_SIZE (vro2->type)
928 6 : && (! TYPE_SIZE (vro1->type)
929 6 : || ! TYPE_SIZE (vro2->type)
930 6 : || ! operand_equal_p (TYPE_SIZE (vro1->type),
931 6 : TYPE_SIZE (vro2->type))))
932 2236938 : || (get_deref_alias_set (vro1->opcode == MEM_REF
933 745646 : ? TREE_TYPE (vro1->op0)
934 0 : : TREE_TYPE (vro1->op2))
935 1491292 : != get_deref_alias_set (vro2->opcode == MEM_REF
936 745646 : ? TREE_TYPE (vro2->op0)
937 0 : : TREE_TYPE (vro2->op2)))))
938 : return false;
939 22190325 : ++j;
940 22190325 : ++i;
941 : }
942 44380650 : while (vr1->operands.length () != i
943 66570975 : || vr2->operands.length () != j);
944 :
945 : return true;
946 : }
947 :
948 : /* Copy the operations present in load/store REF into RESULT, a vector of
949 : vn_reference_op_s's. */
950 :
951 : void
952 227055874 : copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
953 : {
954 : /* For non-calls, store the information that makes up the address. */
955 227055874 : tree orig = ref;
956 789231554 : while (ref)
957 : {
958 562175680 : vn_reference_op_s temp;
959 :
960 562175680 : memset (&temp, 0, sizeof (temp));
961 562175680 : temp.type = TREE_TYPE (ref);
962 562175680 : temp.opcode = TREE_CODE (ref);
963 562175680 : temp.off = -1;
964 :
965 562175680 : switch (temp.opcode)
966 : {
967 15227736 : case MODIFY_EXPR:
968 15227736 : temp.op0 = TREE_OPERAND (ref, 1);
969 15227736 : break;
970 137 : case WITH_SIZE_EXPR:
971 137 : temp.op0 = TREE_OPERAND (ref, 1);
972 137 : temp.off = 0;
973 137 : break;
974 120253308 : case MEM_REF:
975 : /* The base address gets its own vn_reference_op_s structure. */
976 120253308 : temp.op0 = TREE_OPERAND (ref, 1);
977 120253308 : if (!mem_ref_offset (ref).to_shwi (&temp.off))
978 0 : temp.off = -1;
979 120253308 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
980 120253308 : temp.base = MR_DEPENDENCE_BASE (ref);
981 120253308 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
982 120253308 : break;
983 2526619 : case TARGET_MEM_REF:
984 : /* The base address gets its own vn_reference_op_s structure. */
985 2526619 : temp.op0 = TMR_INDEX (ref);
986 2526619 : temp.op1 = TMR_STEP (ref);
987 2526619 : temp.op2 = TMR_OFFSET (ref);
988 2526619 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
989 2526619 : temp.base = MR_DEPENDENCE_BASE (ref);
990 2526619 : result->safe_push (temp);
991 2526619 : memset (&temp, 0, sizeof (temp));
992 2526619 : temp.type = NULL_TREE;
993 2526619 : temp.opcode = ERROR_MARK;
994 2526619 : temp.op0 = TMR_INDEX2 (ref);
995 2526619 : temp.off = -1;
996 2526619 : break;
997 818891 : case BIT_FIELD_REF:
998 : /* Record bits, position and storage order. */
999 818891 : temp.op0 = TREE_OPERAND (ref, 1);
1000 818891 : temp.op1 = TREE_OPERAND (ref, 2);
1001 1637084 : if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
1002 698 : temp.off = -1;
1003 818891 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
1004 818891 : break;
1005 150599039 : case COMPONENT_REF:
1006 : /* The field decl is enough to unambiguously specify the field,
1007 : so use its type here. */
1008 150599039 : temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
1009 150599039 : temp.op0 = TREE_OPERAND (ref, 1);
1010 150599039 : temp.op1 = TREE_OPERAND (ref, 2);
1011 301195646 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1012 301195381 : && TYPE_REVERSE_STORAGE_ORDER
1013 : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1014 150599039 : {
1015 150599039 : tree this_offset = component_ref_field_offset (ref);
1016 150599039 : if (this_offset
1017 150599039 : && poly_int_tree_p (this_offset))
1018 : {
1019 150596909 : tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
1020 150596909 : if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
1021 : {
1022 150110982 : poly_offset_int off
1023 150110982 : = (wi::to_poly_offset (this_offset)
1024 150110982 : + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
1025 : /* Prohibit value-numbering zero offset components
1026 : of addresses the same before the pass folding
1027 : __builtin_object_size had a chance to run. Likewise
1028 : for components of zero size at arbitrary offset. */
1029 150110982 : if (TREE_CODE (orig) != ADDR_EXPR
1030 5016581 : || (TYPE_SIZE (temp.type)
1031 5003557 : && integer_nonzerop (TYPE_SIZE (temp.type))
1032 153186390 : && maybe_ne (off, 0))
1033 153201564 : || (cfun->curr_properties & PROP_objsz))
1034 148646930 : off.to_shwi (&temp.off);
1035 : }
1036 : }
1037 : }
1038 : break;
1039 38968827 : case ARRAY_RANGE_REF:
1040 38968827 : case ARRAY_REF:
1041 38968827 : {
1042 38968827 : tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
1043 : /* Record index as operand. */
1044 38968827 : temp.op0 = TREE_OPERAND (ref, 1);
1045 : /* Always record lower bounds and element size. */
1046 38968827 : temp.op1 = array_ref_low_bound (ref);
1047 : /* But record element size in units of the type alignment. */
1048 38968827 : temp.op2 = TREE_OPERAND (ref, 3);
1049 38968827 : temp.align = eltype->type_common.align;
1050 38968827 : if (! temp.op2)
1051 38758360 : temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
1052 : size_int (TYPE_ALIGN_UNIT (eltype)));
1053 : /* Prohibit value-numbering addresses of one-after-the-last
1054 : element ARRAY_REFs the same as addresses of other components
1055 : before the pass folding __builtin_object_size had a chance
1056 : to run. */
1057 38968827 : bool avoid_oob = true;
1058 38968827 : if (TREE_CODE (orig) != ADDR_EXPR
1059 481064 : || cfun->curr_properties & PROP_objsz)
1060 : avoid_oob = false;
1061 225625 : else if (poly_int_tree_p (temp.op0))
1062 : {
1063 75839 : tree ub = array_ref_up_bound (ref);
1064 75839 : if (ub
1065 74201 : && poly_int_tree_p (ub)
1066 : /* ??? The C frontend for T[0] uses [0:] and the
1067 : C++ frontend [0:-1U]. See layout_type for how
1068 : awkward this is. */
1069 65456 : && !integer_minus_onep (ub)
1070 150040 : && known_le (wi::to_poly_offset (temp.op0),
1071 : wi::to_poly_offset (ub)))
1072 64619 : avoid_oob = false;
1073 : }
1074 38968827 : if (poly_int_tree_p (temp.op0)
1075 22468325 : && poly_int_tree_p (temp.op1)
1076 22468301 : && TREE_CODE (temp.op2) == INTEGER_CST
1077 61376011 : && !avoid_oob)
1078 : {
1079 44794154 : poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1080 67191231 : - wi::to_poly_offset (temp.op1))
1081 44794154 : * wi::to_offset (temp.op2)
1082 22397077 : * vn_ref_op_align_unit (&temp));
1083 22397077 : off.to_shwi (&temp.off);
1084 : }
1085 38968827 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1086 38968827 : && TYPE_REVERSE_STORAGE_ORDER
1087 : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1088 : }
1089 38968827 : break;
1090 83276006 : case VAR_DECL:
1091 83276006 : if (DECL_HARD_REGISTER (ref))
1092 : {
1093 20291 : temp.op0 = ref;
1094 20291 : break;
1095 : }
1096 : /* Fallthru. */
1097 86674682 : case PARM_DECL:
1098 86674682 : case CONST_DECL:
1099 86674682 : case RESULT_DECL:
1100 : /* Canonicalize decls to MEM[&decl] which is what we end up with
1101 : when valueizing MEM[ptr] with ptr = &decl. */
1102 86674682 : temp.opcode = MEM_REF;
1103 86674682 : temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1104 86674682 : temp.off = 0;
1105 86674682 : result->safe_push (temp);
1106 86674682 : temp.opcode = ADDR_EXPR;
1107 86674682 : temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1108 86674682 : temp.type = TREE_TYPE (temp.op0);
1109 86674682 : temp.off = -1;
1110 86674682 : break;
1111 98395242 : case STRING_CST:
1112 98395242 : case INTEGER_CST:
1113 98395242 : case POLY_INT_CST:
1114 98395242 : case COMPLEX_CST:
1115 98395242 : case VECTOR_CST:
1116 98395242 : case REAL_CST:
1117 98395242 : case FIXED_CST:
1118 98395242 : case CONSTRUCTOR:
1119 98395242 : case SSA_NAME:
1120 98395242 : temp.op0 = ref;
1121 98395242 : break;
1122 46250309 : case ADDR_EXPR:
1123 46250309 : if (is_gimple_min_invariant (ref))
1124 : {
1125 41965659 : temp.op0 = ref;
1126 41965659 : break;
1127 : }
1128 : break;
1129 : /* These are only interesting for their operands, their
1130 : existence, and their type. They will never be the last
1131 : ref in the chain of references (IE they require an
1132 : operand), so we don't have to put anything
1133 : for op* as it will be handled by the iteration */
1134 495026 : case REALPART_EXPR:
1135 495026 : temp.off = 0;
1136 495026 : break;
1137 1445831 : case VIEW_CONVERT_EXPR:
1138 1445831 : temp.off = 0;
1139 1445831 : temp.reverse = storage_order_barrier_p (ref);
1140 1445831 : break;
1141 499742 : case IMAGPART_EXPR:
1142 : /* This is only interesting for its constant offset. */
1143 499742 : temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1144 499742 : break;
1145 0 : default:
1146 0 : gcc_unreachable ();
1147 : }
1148 562175680 : result->safe_push (temp);
1149 :
1150 562175680 : if (REFERENCE_CLASS_P (ref)
1151 246568397 : || TREE_CODE (ref) == MODIFY_EXPR
1152 231340661 : || TREE_CODE (ref) == WITH_SIZE_EXPR
1153 793516204 : || (TREE_CODE (ref) == ADDR_EXPR
1154 46250309 : && !is_gimple_min_invariant (ref)))
1155 335119806 : ref = TREE_OPERAND (ref, 0);
1156 : else
1157 : ref = NULL_TREE;
1158 : }
1159 227055874 : }
1160 :
1161 : /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1162 : operands in *OPS, the reference alias set SET and the reference type TYPE.
1163 : Return true if something useful was produced. */
1164 :
1165 : bool
1166 14840502 : ao_ref_init_from_vn_reference (ao_ref *ref,
1167 : alias_set_type set, alias_set_type base_set,
1168 : tree type, const vec<vn_reference_op_s> &ops)
1169 : {
1170 14840502 : unsigned i;
1171 14840502 : tree base = NULL_TREE;
1172 14840502 : tree *op0_p = &base;
1173 14840502 : poly_offset_int offset = 0;
1174 14840502 : poly_offset_int max_size;
1175 14840502 : poly_offset_int size = -1;
1176 14840502 : tree size_tree = NULL_TREE;
1177 :
1178 : /* We don't handle calls. */
1179 14840502 : if (!type)
1180 : return false;
1181 :
1182 14840502 : machine_mode mode = TYPE_MODE (type);
1183 14840502 : if (mode == BLKmode)
1184 66869 : size_tree = TYPE_SIZE (type);
1185 : else
1186 29547266 : size = GET_MODE_BITSIZE (mode);
1187 14773633 : if (size_tree != NULL_TREE
1188 66869 : && poly_int_tree_p (size_tree))
1189 66869 : size = wi::to_poly_offset (size_tree);
1190 :
1191 : /* Lower the final access size from the outermost expression. */
1192 14840502 : const_vn_reference_op_t cst_op = &ops[0];
1193 : /* Cast away constness for the sake of the const-unsafe
1194 : FOR_EACH_VEC_ELT(). */
1195 14840502 : vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1196 14840502 : size_tree = NULL_TREE;
1197 14840502 : if (op->opcode == COMPONENT_REF)
1198 5141542 : size_tree = DECL_SIZE (op->op0);
1199 9698960 : else if (op->opcode == BIT_FIELD_REF)
1200 77717 : size_tree = op->op0;
1201 5219259 : if (size_tree != NULL_TREE
1202 5219259 : && poly_int_tree_p (size_tree)
1203 10438518 : && (!known_size_p (size)
1204 14840502 : || known_lt (wi::to_poly_offset (size_tree), size)))
1205 40765 : size = wi::to_poly_offset (size_tree);
1206 :
1207 : /* Initially, maxsize is the same as the accessed element size.
1208 : In the following it will only grow (or become -1). */
1209 14840502 : max_size = size;
1210 :
1211 : /* Compute cumulative bit-offset for nested component-refs and array-refs,
1212 : and find the ultimate containing object. */
1213 57031079 : FOR_EACH_VEC_ELT (ops, i, op)
1214 : {
1215 42340656 : switch (op->opcode)
1216 : {
1217 : case CALL_EXPR:
1218 : return false;
1219 :
1220 : /* Record the base objects. */
1221 14394191 : case MEM_REF:
1222 14394191 : *op0_p = build2 (MEM_REF, op->type,
1223 : NULL_TREE, op->op0);
1224 14394191 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1225 14394191 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1226 14394191 : op0_p = &TREE_OPERAND (*op0_p, 0);
1227 14394191 : break;
1228 :
1229 295704 : case TARGET_MEM_REF:
1230 887112 : *op0_p = build5 (TARGET_MEM_REF, op->type,
1231 : NULL_TREE, op->op2, op->op0,
1232 295704 : op->op1, ops[i+1].op0);
1233 295704 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1234 295704 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1235 295704 : op0_p = &TREE_OPERAND (*op0_p, 0);
1236 295704 : ++i;
1237 295704 : break;
1238 :
1239 : /* Unwrap some of the wrapped decls. */
1240 6688481 : case ADDR_EXPR:
1241 : /* Apart from ADDR_EXPR arguments to MEM_REF. */
1242 6688481 : if (base != NULL_TREE
1243 6688480 : && TREE_CODE (base) == MEM_REF
1244 6656582 : && op->op0
1245 13345063 : && DECL_P (TREE_OPERAND (op->op0, 0)))
1246 : {
1247 6649213 : const_vn_reference_op_t pop = &ops[i-1];
1248 6649213 : base = TREE_OPERAND (op->op0, 0);
1249 6649213 : if (known_eq (pop->off, -1))
1250 : {
1251 25 : max_size = -1;
1252 25 : offset = 0;
1253 : }
1254 : else
1255 19947564 : offset += poly_offset_int (pop->off) * BITS_PER_UNIT;
1256 : op0_p = NULL;
1257 : break;
1258 : }
1259 : /* Fallthru. */
1260 8041210 : case PARM_DECL:
1261 8041210 : case CONST_DECL:
1262 8041210 : case RESULT_DECL:
1263 : /* ??? We shouldn't see these, but un-canonicalize what
1264 : copy_reference_ops_from_ref does when visiting MEM_REF. */
1265 8041210 : case VAR_DECL:
1266 : /* ??? And for this only have DECL_HARD_REGISTER. */
1267 8041210 : case STRING_CST:
1268 : /* This can show up in ARRAY_REF bases. */
1269 8041210 : case INTEGER_CST:
1270 8041210 : case SSA_NAME:
1271 8041210 : *op0_p = op->op0;
1272 8041210 : op0_p = NULL;
1273 8041210 : break;
1274 :
1275 : /* And now the usual component-reference style ops. */
1276 77717 : case BIT_FIELD_REF:
1277 77717 : offset += wi::to_poly_offset (op->op1);
1278 77717 : break;
1279 :
1280 8407049 : case COMPONENT_REF:
1281 8407049 : {
1282 8407049 : tree field = op->op0;
1283 : /* We do not have a complete COMPONENT_REF tree here so we
1284 : cannot use component_ref_field_offset. Do the interesting
1285 : parts manually. */
1286 8407049 : tree this_offset = DECL_FIELD_OFFSET (field);
1287 :
1288 8407049 : if (op->op1 || !poly_int_tree_p (this_offset))
1289 233 : max_size = -1;
1290 : else
1291 : {
1292 8406816 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1293 8406816 : << LOG2_BITS_PER_UNIT);
1294 8406816 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1295 8406816 : offset += woffset;
1296 : }
1297 : break;
1298 : }
1299 :
1300 3123155 : case ARRAY_RANGE_REF:
1301 3123155 : case ARRAY_REF:
1302 : /* Use the recorded constant offset. */
1303 3123155 : if (maybe_eq (op->off, -1))
1304 1213438 : max_size = -1;
1305 : else
1306 5729151 : offset += poly_offset_int (op->off) * BITS_PER_UNIT;
1307 : break;
1308 :
1309 : case REALPART_EXPR:
1310 : break;
1311 :
1312 : case IMAGPART_EXPR:
1313 42190577 : offset += size;
1314 : break;
1315 :
1316 : case VIEW_CONVERT_EXPR:
1317 : break;
1318 :
1319 : case POLY_INT_CST:
1320 : case COMPLEX_CST:
1321 : case VECTOR_CST:
1322 : case REAL_CST:
1323 : case FIXED_CST:
1324 : case CONSTRUCTOR:
1325 : return false;
1326 :
1327 : default:
1328 : return false;
1329 : }
1330 : }
1331 :
1332 14690423 : if (base == NULL_TREE)
1333 : return false;
1334 :
1335 14690423 : ref->ref = NULL_TREE;
1336 14690423 : ref->base = base;
1337 14690423 : ref->ref_alias_set = set;
1338 14690423 : ref->base_alias_set = base_set;
1339 : /* We discount volatiles from value-numbering elsewhere. */
1340 14690423 : ref->volatile_p = false;
1341 :
1342 14690423 : if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1343 : {
1344 0 : ref->offset = 0;
1345 0 : ref->size = -1;
1346 0 : ref->max_size = -1;
1347 0 : return true;
1348 : }
1349 :
1350 14690423 : if (!offset.to_shwi (&ref->offset))
1351 : {
1352 26 : ref->offset = 0;
1353 26 : ref->max_size = -1;
1354 26 : return true;
1355 : }
1356 :
1357 14690397 : if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1358 1065386 : ref->max_size = -1;
1359 :
1360 : return true;
1361 : }
1362 :
1363 : /* Copy the operations present in load/store/call REF into RESULT, a vector of
1364 : vn_reference_op_s's. */
1365 :
1366 : static void
1367 9383261 : copy_reference_ops_from_call (gcall *call,
1368 : vec<vn_reference_op_s> *result)
1369 : {
1370 9383261 : vn_reference_op_s temp;
1371 9383261 : unsigned i;
1372 9383261 : tree lhs = gimple_call_lhs (call);
1373 9383261 : int lr;
1374 :
1375 : /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1376 : different. By adding the lhs here in the vector, we ensure that the
1377 : hashcode is different, guaranteeing a different value number. */
1378 9383261 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
1379 : {
1380 450739 : memset (&temp, 0, sizeof (temp));
1381 450739 : temp.opcode = MODIFY_EXPR;
1382 450739 : temp.type = TREE_TYPE (lhs);
1383 450739 : temp.op0 = lhs;
1384 450739 : temp.off = -1;
1385 450739 : result->safe_push (temp);
1386 : }
1387 :
1388 : /* Copy the type, opcode, function, static chain and EH region, if any. */
1389 9383261 : memset (&temp, 0, sizeof (temp));
1390 9383261 : temp.type = gimple_call_fntype (call);
1391 9383261 : temp.opcode = CALL_EXPR;
1392 9383261 : temp.op0 = gimple_call_fn (call);
1393 9383261 : if (gimple_call_internal_p (call))
1394 544638 : temp.clique = gimple_call_internal_fn (call);
1395 9383261 : temp.op1 = gimple_call_chain (call);
1396 9383261 : if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1397 623848 : temp.op2 = size_int (lr);
1398 9383261 : temp.off = -1;
1399 9383261 : result->safe_push (temp);
1400 :
1401 : /* Copy the call arguments. As they can be references as well,
1402 : just chain them together. */
1403 37156115 : for (i = 0; i < gimple_call_num_args (call); ++i)
1404 : {
1405 18389593 : tree callarg = gimple_call_arg (call, i);
1406 18389593 : copy_reference_ops_from_ref (callarg, result);
1407 : }
1408 9383261 : }
1409 :
1410 : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1411 : *I_P to point to the last element of the replacement. */
1412 : static bool
1413 129270868 : vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1414 : unsigned int *i_p)
1415 : {
1416 129270868 : unsigned int i = *i_p;
1417 129270868 : vn_reference_op_t op = &(*ops)[i];
1418 129270868 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1419 129270868 : tree addr_base;
1420 129270868 : poly_int64 addr_offset = 0;
1421 :
1422 : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1423 : from .foo.bar to the preceding MEM_REF offset and replace the
1424 : address with &OBJ. */
1425 129270868 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1426 : &addr_offset, vn_valueize);
1427 129270868 : gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1428 129270868 : if (addr_base != TREE_OPERAND (op->op0, 0))
1429 : {
1430 686565 : poly_offset_int off
1431 686565 : = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1432 : SIGNED)
1433 686565 : + addr_offset);
1434 686565 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1435 686565 : op->op0 = build_fold_addr_expr (addr_base);
1436 686565 : if (tree_fits_shwi_p (mem_op->op0))
1437 686498 : mem_op->off = tree_to_shwi (mem_op->op0);
1438 : else
1439 : mem_op->off = -1;
1440 686565 : return true;
1441 : }
1442 : return false;
1443 : }
1444 :
1445 : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1446 : *I_P to point to the last element of the replacement. */
1447 : static bool
1448 87561366 : vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1449 : unsigned int *i_p)
1450 : {
1451 87561366 : bool changed = false;
1452 95179560 : vn_reference_op_t op;
1453 :
1454 95179560 : do
1455 : {
1456 95179560 : unsigned int i = *i_p;
1457 95179560 : op = &(*ops)[i];
1458 95179560 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1459 95179560 : gimple *def_stmt;
1460 95179560 : enum tree_code code;
1461 95179560 : poly_offset_int off;
1462 :
1463 95179560 : def_stmt = SSA_NAME_DEF_STMT (op->op0);
1464 95179560 : if (!is_gimple_assign (def_stmt))
1465 87559415 : return changed;
1466 :
1467 38521200 : code = gimple_assign_rhs_code (def_stmt);
1468 38521200 : if (code != ADDR_EXPR
1469 38521200 : && code != POINTER_PLUS_EXPR)
1470 : return changed;
1471 :
1472 20469696 : off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1473 :
1474 : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1475 : from .foo.bar to the preceding MEM_REF offset and replace the
1476 : address with &OBJ. */
1477 20469696 : if (code == ADDR_EXPR)
1478 : {
1479 969110 : tree addr, addr_base;
1480 969110 : poly_int64 addr_offset;
1481 :
1482 969110 : addr = gimple_assign_rhs1 (def_stmt);
1483 969110 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1484 : &addr_offset,
1485 : vn_valueize);
1486 : /* If that didn't work because the address isn't invariant propagate
1487 : the reference tree from the address operation in case the current
1488 : dereference isn't offsetted. */
1489 969110 : if (!addr_base
1490 287194 : && *i_p == ops->length () - 1
1491 143597 : && known_eq (off, 0)
1492 : /* This makes us disable this transform for PRE where the
1493 : reference ops might be also used for code insertion which
1494 : is invalid. */
1495 1055607 : && default_vn_walk_kind == VN_WALKREWRITE)
1496 : {
1497 86407 : auto_vec<vn_reference_op_s, 32> tem;
1498 86407 : copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1499 : /* Make sure to preserve TBAA info. The only objects not
1500 : wrapped in MEM_REFs that can have their address taken are
1501 : STRING_CSTs. */
1502 86407 : if (tem.length () >= 2
1503 86407 : && tem[tem.length () - 2].opcode == MEM_REF)
1504 : {
1505 86392 : vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1506 86392 : new_mem_op->op0
1507 86392 : = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1508 172784 : wi::to_poly_wide (new_mem_op->op0));
1509 : }
1510 : /* Do not forward addresses of TARGET_MEM_REF. */
1511 15 : else if (tem[0].opcode == TARGET_MEM_REF)
1512 : return changed;
1513 : else
1514 15 : gcc_assert (tem.last ().opcode == STRING_CST);
1515 86407 : ops->pop ();
1516 86407 : ops->pop ();
1517 86407 : ops->safe_splice (tem);
1518 86407 : --*i_p;
1519 86407 : return true;
1520 86407 : }
1521 882703 : if (!addr_base
1522 825513 : || TREE_CODE (addr_base) != MEM_REF
1523 1706384 : || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1524 821820 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1525 : 0))))
1526 : return changed;
1527 :
1528 823681 : off += addr_offset;
1529 823681 : off += mem_ref_offset (addr_base);
1530 823681 : op->op0 = TREE_OPERAND (addr_base, 0);
1531 : }
1532 : else
1533 : {
1534 19500586 : tree ptr, ptroff;
1535 19500586 : ptr = gimple_assign_rhs1 (def_stmt);
1536 19500586 : ptroff = gimple_assign_rhs2 (def_stmt);
1537 19500586 : if (TREE_CODE (ptr) != SSA_NAME
1538 17771680 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1539 : /* Make sure to not endlessly recurse.
1540 : See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1541 : happen when we value-number a PHI to its backedge value. */
1542 17770295 : || SSA_VAL (ptr) == op->op0
1543 37270881 : || !poly_int_tree_p (ptroff))
1544 : return changed;
1545 :
1546 6796464 : off += wi::to_poly_offset (ptroff);
1547 6796464 : op->op0 = ptr;
1548 : }
1549 :
1550 7620145 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1551 7620145 : if (tree_fits_shwi_p (mem_op->op0))
1552 7311208 : mem_op->off = tree_to_shwi (mem_op->op0);
1553 : else
1554 : mem_op->off = -1;
1555 : /* ??? Can end up with endless recursion here!?
1556 : gcc.c-torture/execute/strcmp-1.c */
1557 7620145 : if (TREE_CODE (op->op0) == SSA_NAME)
1558 7618284 : op->op0 = SSA_VAL (op->op0);
1559 7620145 : if (TREE_CODE (op->op0) != SSA_NAME)
1560 1951 : op->opcode = TREE_CODE (op->op0);
1561 :
1562 7620145 : changed = true;
1563 : }
1564 : /* Tail-recurse. */
1565 7620145 : while (TREE_CODE (op->op0) == SSA_NAME);
1566 :
1567 : /* Fold a remaining *&. */
1568 1951 : if (TREE_CODE (op->op0) == ADDR_EXPR)
1569 261 : vn_reference_fold_indirect (ops, i_p);
1570 :
1571 : return changed;
1572 : }
1573 :
1574 : /* Optimize the reference REF to a constant if possible or return
1575 : NULL_TREE if not. */
1576 :
1577 : tree
1578 112002198 : fully_constant_vn_reference_p (vn_reference_t ref)
1579 : {
1580 112002198 : vec<vn_reference_op_s> operands = ref->operands;
1581 112002198 : vn_reference_op_t op;
1582 :
1583 : /* Try to simplify the translated expression if it is
1584 : a call to a builtin function with at most two arguments. */
1585 112002198 : op = &operands[0];
1586 112002198 : if (op->opcode == CALL_EXPR
1587 90940 : && (!op->op0
1588 83355 : || (TREE_CODE (op->op0) == ADDR_EXPR
1589 83355 : && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1590 83355 : && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1591 : BUILT_IN_NORMAL)))
1592 73231 : && operands.length () >= 2
1593 112075397 : && operands.length () <= 3)
1594 : {
1595 34259 : vn_reference_op_t arg0, arg1 = NULL;
1596 34259 : bool anyconst = false;
1597 34259 : arg0 = &operands[1];
1598 34259 : if (operands.length () > 2)
1599 5618 : arg1 = &operands[2];
1600 34259 : if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1601 34259 : || (arg0->opcode == ADDR_EXPR
1602 13836 : && is_gimple_min_invariant (arg0->op0)))
1603 : anyconst = true;
1604 34259 : if (arg1
1605 34259 : && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1606 4127 : || (arg1->opcode == ADDR_EXPR
1607 587 : && is_gimple_min_invariant (arg1->op0))))
1608 : anyconst = true;
1609 32181 : if (anyconst)
1610 : {
1611 22445 : combined_fn fn;
1612 22445 : if (op->op0)
1613 21490 : fn = as_combined_fn (DECL_FUNCTION_CODE
1614 21490 : (TREE_OPERAND (op->op0, 0)));
1615 : else
1616 955 : fn = as_combined_fn ((internal_fn) op->clique);
1617 22445 : tree folded;
1618 22445 : if (arg1)
1619 2722 : folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1620 : else
1621 19723 : folded = fold_const_call (fn, ref->type, arg0->op0);
1622 22445 : if (folded
1623 22445 : && is_gimple_min_invariant (folded))
1624 1058 : return folded;
1625 : }
1626 : }
1627 :
1628 : /* Simplify reads from constants or constant initializers. */
1629 111967939 : else if (BITS_PER_UNIT == 8
1630 111967939 : && ref->type
1631 111967939 : && COMPLETE_TYPE_P (ref->type)
1632 223935836 : && is_gimple_reg_type (ref->type))
1633 : {
1634 107569408 : poly_int64 off = 0;
1635 107569408 : HOST_WIDE_INT size;
1636 107569408 : if (INTEGRAL_TYPE_P (ref->type))
1637 54643245 : size = TYPE_PRECISION (ref->type);
1638 52926163 : else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1639 52926163 : size = tree_to_shwi (TYPE_SIZE (ref->type));
1640 : else
1641 : return NULL_TREE;
1642 107569408 : if (size % BITS_PER_UNIT != 0
1643 105746097 : || size > MAX_BITSIZE_MODE_ANY_MODE)
1644 : return NULL_TREE;
1645 105744770 : size /= BITS_PER_UNIT;
1646 105744770 : unsigned i;
1647 196049600 : for (i = 0; i < operands.length (); ++i)
1648 : {
1649 196049600 : if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1650 : {
1651 307 : ++i;
1652 307 : break;
1653 : }
1654 196049293 : if (operands[i].reverse)
1655 : return NULL_TREE;
1656 196040811 : if (known_eq (operands[i].off, -1))
1657 : return NULL_TREE;
1658 182125093 : off += operands[i].off;
1659 182125093 : if (operands[i].opcode == MEM_REF)
1660 : {
1661 91820263 : ++i;
1662 91820263 : break;
1663 : }
1664 : }
1665 91820570 : vn_reference_op_t base = &operands[--i];
1666 91820570 : tree ctor = error_mark_node;
1667 91820570 : tree decl = NULL_TREE;
1668 91820570 : if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1669 307 : ctor = base->op0;
1670 91820263 : else if (base->opcode == MEM_REF
1671 91820263 : && base[1].opcode == ADDR_EXPR
1672 150810175 : && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1673 3590224 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1674 3590164 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1675 : {
1676 55405847 : decl = TREE_OPERAND (base[1].op0, 0);
1677 55405847 : if (TREE_CODE (decl) == STRING_CST)
1678 : ctor = decl;
1679 : else
1680 55399748 : ctor = ctor_for_folding (decl);
1681 : }
1682 91814471 : if (ctor == NULL_TREE)
1683 386 : return build_zero_cst (ref->type);
1684 91820184 : else if (ctor != error_mark_node)
1685 : {
1686 104602 : HOST_WIDE_INT const_off;
1687 104602 : if (decl)
1688 : {
1689 208590 : tree res = fold_ctor_reference (ref->type, ctor,
1690 104295 : off * BITS_PER_UNIT,
1691 104295 : size * BITS_PER_UNIT, decl);
1692 104295 : if (res)
1693 : {
1694 59999 : STRIP_USELESS_TYPE_CONVERSION (res);
1695 59999 : if (is_gimple_min_invariant (res))
1696 59867 : return res;
1697 : }
1698 : }
1699 307 : else if (off.is_constant (&const_off))
1700 : {
1701 307 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1702 307 : int len = native_encode_expr (ctor, buf, size, const_off);
1703 307 : if (len > 0)
1704 137 : return native_interpret_expr (ref->type, buf, len);
1705 : }
1706 : }
1707 : }
1708 :
1709 : return NULL_TREE;
1710 : }
1711 :
1712 : /* Return true if OPS contain a storage order barrier. */
1713 :
1714 : static bool
1715 60446271 : contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1716 : {
1717 60446271 : vn_reference_op_t op;
1718 60446271 : unsigned i;
1719 :
1720 236947929 : FOR_EACH_VEC_ELT (ops, i, op)
1721 176501658 : if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1722 : return true;
1723 :
1724 : return false;
1725 : }
1726 :
1727 : /* Return true if OPS represent an access with reverse storage order. */
1728 :
1729 : static bool
1730 62503011 : reverse_storage_order_for_component_p (const vec<vn_reference_op_s> &ops)
1731 : {
1732 62503011 : unsigned i = 0;
1733 62503011 : if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1734 : ++i;
1735 62503011 : switch (ops[i].opcode)
1736 : {
1737 59796432 : case ARRAY_REF:
1738 59796432 : case COMPONENT_REF:
1739 59796432 : case BIT_FIELD_REF:
1740 59796432 : case MEM_REF:
1741 59796432 : return ops[i].reverse;
1742 : default:
1743 : return false;
1744 : }
1745 : }
1746 :
1747 : /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1748 : structures into their value numbers. This is done in-place, and
1749 : the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1750 : whether any operands were valueized. */
1751 :
1752 : static void
1753 224045125 : valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1754 : bool with_avail = false)
1755 : {
1756 224045125 : *valueized_anything = false;
1757 :
1758 904646556 : for (unsigned i = 0; i < orig->length (); ++i)
1759 : {
1760 680601431 : re_valueize:
1761 684634324 : vn_reference_op_t vro = &(*orig)[i];
1762 684634324 : if (vro->opcode == SSA_NAME
1763 584904188 : || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1764 : {
1765 124296390 : tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1766 124296390 : if (tem != vro->op0)
1767 : {
1768 18501019 : *valueized_anything = true;
1769 18501019 : vro->op0 = tem;
1770 : }
1771 : /* If it transforms from an SSA_NAME to a constant, update
1772 : the opcode. */
1773 124296390 : if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1774 2178253 : vro->opcode = TREE_CODE (vro->op0);
1775 : }
1776 684634324 : if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1777 : {
1778 26275 : tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1779 26275 : if (tem != vro->op1)
1780 : {
1781 587 : *valueized_anything = true;
1782 587 : vro->op1 = tem;
1783 : }
1784 : }
1785 684634324 : if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1786 : {
1787 204909 : tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1788 204909 : if (tem != vro->op2)
1789 : {
1790 119894 : *valueized_anything = true;
1791 119894 : vro->op2 = tem;
1792 : }
1793 : }
1794 : /* If it transforms from an SSA_NAME to an address, fold with
1795 : a preceding indirect reference. */
1796 684634324 : if (i > 0
1797 460509316 : && vro->op0
1798 456997337 : && TREE_CODE (vro->op0) == ADDR_EXPR
1799 819856695 : && (*orig)[i - 1].opcode == MEM_REF)
1800 : {
1801 129270607 : if (vn_reference_fold_indirect (orig, &i))
1802 686565 : *valueized_anything = true;
1803 : }
1804 555363717 : else if (i > 0
1805 331238709 : && vro->opcode == SSA_NAME
1806 652915600 : && (*orig)[i - 1].opcode == MEM_REF)
1807 : {
1808 87561366 : if (vn_reference_maybe_forwprop_address (orig, &i))
1809 : {
1810 4032893 : *valueized_anything = true;
1811 : /* Re-valueize the current operand. */
1812 4032893 : goto re_valueize;
1813 : }
1814 : }
1815 : /* If it transforms a non-constant ARRAY_REF into a constant
1816 : one, adjust the constant offset. */
1817 467802351 : else if ((vro->opcode == ARRAY_REF
1818 467802351 : || vro->opcode == ARRAY_RANGE_REF)
1819 40081866 : && known_eq (vro->off, -1)
1820 17304457 : && poly_int_tree_p (vro->op0)
1821 5006263 : && poly_int_tree_p (vro->op1)
1822 472808614 : && TREE_CODE (vro->op2) == INTEGER_CST)
1823 : {
1824 : /* Prohibit value-numbering addresses of one-after-the-last
1825 : element ARRAY_REFs the same as addresses of other components
1826 : before the pass folding __builtin_object_size had a chance
1827 : to run. */
1828 4872015 : if (!(cfun->curr_properties & PROP_objsz)
1829 6114910 : && (*orig)[0].opcode == ADDR_EXPR)
1830 : {
1831 36030 : tree dom = TYPE_DOMAIN ((*orig)[i + 1].type);
1832 54923 : if (!dom
1833 35880 : || !TYPE_MAX_VALUE (dom)
1834 25753 : || !poly_int_tree_p (TYPE_MAX_VALUE (dom))
1835 53243 : || integer_minus_onep (TYPE_MAX_VALUE (dom)))
1836 19700 : continue;
1837 17137 : if (!known_le (wi::to_poly_offset (vro->op0),
1838 : wi::to_poly_offset (TYPE_MAX_VALUE (dom))))
1839 807 : continue;
1840 : }
1841 :
1842 9704630 : poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1843 14556945 : - wi::to_poly_offset (vro->op1))
1844 9704630 : * wi::to_offset (vro->op2)
1845 4852315 : * vn_ref_op_align_unit (vro));
1846 4852315 : off.to_shwi (&vro->off);
1847 : }
1848 : }
1849 224045125 : }
1850 :
1851 : static void
1852 12907788 : valueize_refs (vec<vn_reference_op_s> *orig)
1853 : {
1854 12907788 : bool tem;
1855 0 : valueize_refs_1 (orig, &tem);
1856 0 : }
1857 :
1858 : static vec<vn_reference_op_s> shared_lookup_references;
1859 :
1860 : /* Create a vector of vn_reference_op_s structures from REF, a
1861 : REFERENCE_CLASS_P tree. The vector is shared among all callers of
1862 : this function. *VALUEIZED_ANYTHING will specify whether any
1863 : operands were valueized. */
1864 :
1865 : static vec<vn_reference_op_s>
1866 184429081 : valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1867 : {
1868 184429081 : if (!ref)
1869 0 : return vNULL;
1870 184429081 : shared_lookup_references.truncate (0);
1871 184429081 : copy_reference_ops_from_ref (ref, &shared_lookup_references);
1872 184429081 : valueize_refs_1 (&shared_lookup_references, valueized_anything);
1873 184429081 : return shared_lookup_references;
1874 : }
1875 :
1876 : /* Create a vector of vn_reference_op_s structures from CALL, a
1877 : call statement. The vector is shared among all callers of
1878 : this function. */
1879 :
1880 : static vec<vn_reference_op_s>
1881 9383261 : valueize_shared_reference_ops_from_call (gcall *call)
1882 : {
1883 9383261 : if (!call)
1884 0 : return vNULL;
1885 9383261 : shared_lookup_references.truncate (0);
1886 9383261 : copy_reference_ops_from_call (call, &shared_lookup_references);
1887 9383261 : valueize_refs (&shared_lookup_references);
1888 9383261 : return shared_lookup_references;
1889 : }
1890 :
1891 : /* Lookup a SCCVN reference operation VR in the current hash table.
1892 : Returns the resulting value number if it exists in the hash table,
1893 : NULL_TREE otherwise. VNRESULT will be filled in with the actual
1894 : vn_reference_t stored in the hashtable if something is found. */
1895 :
1896 : static tree
1897 66767393 : vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1898 : {
1899 66767393 : vn_reference_s **slot;
1900 66767393 : hashval_t hash;
1901 :
1902 66767393 : hash = vr->hashcode;
1903 66767393 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1904 66767393 : if (slot)
1905 : {
1906 8330300 : if (vnresult)
1907 8330300 : *vnresult = (vn_reference_t)*slot;
1908 8330300 : return ((vn_reference_t)*slot)->result;
1909 : }
1910 :
1911 : return NULL_TREE;
1912 : }
1913 :
1914 :
1915 : /* Partial definition tracking support. */
1916 :
1917 : struct pd_range
1918 : {
1919 : HOST_WIDE_INT offset;
1920 : HOST_WIDE_INT size;
1921 : pd_range *m_children[2];
1922 : };
1923 :
1924 : struct pd_data
1925 : {
1926 : tree rhs;
1927 : HOST_WIDE_INT rhs_off;
1928 : HOST_WIDE_INT offset;
1929 : HOST_WIDE_INT size;
1930 : };
1931 :
1932 : /* Context for alias walking. */
1933 :
1934 : struct vn_walk_cb_data
1935 : {
1936 62560075 : vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1937 : vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1938 : bool redundant_store_removal_p_)
1939 62560075 : : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1940 62560075 : mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1941 62560075 : vn_walk_kind (vn_walk_kind_),
1942 62560075 : tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
1943 125120150 : saved_operands (vNULL), first_range (), first_set (-2),
1944 125120150 : first_base_set (-2)
1945 : {
1946 62560075 : if (!last_vuse_ptr)
1947 28986589 : last_vuse_ptr = &last_vuse;
1948 62560075 : ao_ref_init (&orig_ref, orig_ref_);
1949 62560075 : if (mask)
1950 : {
1951 308659 : wide_int w = wi::to_wide (mask);
1952 308659 : unsigned int pos = 0, prec = w.get_precision ();
1953 308659 : pd_data pd;
1954 308659 : pd.rhs = build_constructor (NULL_TREE, NULL);
1955 308659 : pd.rhs_off = 0;
1956 : /* When bitwise and with a constant is done on a memory load,
1957 : we don't really need all the bits to be defined or defined
1958 : to constants, we don't really care what is in the position
1959 : corresponding to 0 bits in the mask.
1960 : So, push the ranges of those 0 bits in the mask as artificial
1961 : zero stores and let the partial def handling code do the
1962 : rest. */
1963 661482 : while (pos < prec)
1964 : {
1965 640961 : int tz = wi::ctz (w);
1966 640961 : if (pos + tz > prec)
1967 288138 : tz = prec - pos;
1968 640961 : if (tz)
1969 : {
1970 488759 : if (BYTES_BIG_ENDIAN)
1971 : pd.offset = prec - pos - tz;
1972 : else
1973 488759 : pd.offset = pos;
1974 488759 : pd.size = tz;
1975 488759 : void *r = push_partial_def (pd, 0, 0, 0, prec);
1976 488759 : gcc_assert (r == NULL_TREE);
1977 : }
1978 640961 : pos += tz;
1979 640961 : if (pos == prec)
1980 : break;
1981 352823 : w = wi::lrshift (w, tz);
1982 352823 : tz = wi::ctz (wi::bit_not (w));
1983 352823 : if (pos + tz > prec)
1984 0 : tz = prec - pos;
1985 352823 : pos += tz;
1986 352823 : w = wi::lrshift (w, tz);
1987 : }
1988 308659 : }
1989 62560075 : }
1990 : ~vn_walk_cb_data ();
1991 : void *finish (alias_set_type, alias_set_type, tree);
1992 : void *push_partial_def (pd_data pd,
1993 : alias_set_type, alias_set_type, HOST_WIDE_INT,
1994 : HOST_WIDE_INT);
1995 :
1996 : vn_reference_t vr;
1997 : ao_ref orig_ref;
1998 : tree *last_vuse_ptr;
1999 : tree last_vuse;
2000 : tree mask;
2001 : tree masked_result;
2002 : tree same_val;
2003 : vn_lookup_kind vn_walk_kind;
2004 : bool tbaa_p;
2005 : bool redundant_store_removal_p;
2006 : vec<vn_reference_op_s> saved_operands;
2007 :
2008 : /* The VDEFs of partial defs we come along. */
2009 : auto_vec<pd_data, 2> partial_defs;
2010 : /* The first defs range to avoid splay tree setup in most cases. */
2011 : pd_range first_range;
2012 : alias_set_type first_set;
2013 : alias_set_type first_base_set;
2014 : default_splay_tree<pd_range *> known_ranges;
2015 : obstack ranges_obstack;
2016 : static constexpr HOST_WIDE_INT bufsize = 64;
2017 : };
2018 :
2019 62560075 : vn_walk_cb_data::~vn_walk_cb_data ()
2020 : {
2021 62560075 : if (known_ranges)
2022 175940 : obstack_free (&ranges_obstack, NULL);
2023 62560075 : saved_operands.release ();
2024 62560075 : }
2025 :
2026 : void *
2027 1598054 : vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
2028 : {
2029 1598054 : if (first_set != -2)
2030 : {
2031 455395 : set = first_set;
2032 455395 : base_set = first_base_set;
2033 : }
2034 1598054 : if (mask)
2035 : {
2036 457 : masked_result = val;
2037 457 : return (void *) -1;
2038 : }
2039 1597597 : if (same_val && !operand_equal_p (val, same_val))
2040 : return (void *) -1;
2041 1593752 : vec<vn_reference_op_s> &operands
2042 1593752 : = saved_operands.exists () ? saved_operands : vr->operands;
2043 1593752 : return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
2044 : vr->offset, vr->max_size,
2045 1593752 : vr->type, operands, val);
2046 : }
2047 :
2048 : /* Push PD to the vector of partial definitions returning a
2049 : value when we are ready to combine things with VUSE, SET and MAXSIZEI,
2050 : NULL when we want to continue looking for partial defs or -1
2051 : on failure. */
2052 :
2053 : void *
2054 574636 : vn_walk_cb_data::push_partial_def (pd_data pd,
2055 : alias_set_type set, alias_set_type base_set,
2056 : HOST_WIDE_INT offseti,
2057 : HOST_WIDE_INT maxsizei)
2058 : {
2059 : /* We're using a fixed buffer for encoding so fail early if the object
2060 : we want to interpret is bigger. */
2061 574636 : if (maxsizei > bufsize * BITS_PER_UNIT
2062 : || CHAR_BIT != 8
2063 : || BITS_PER_UNIT != 8
2064 : /* Not prepared to handle PDP endian. */
2065 : || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2066 : return (void *)-1;
2067 :
2068 : /* Turn too large constant stores into non-constant stores. */
2069 574561 : if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
2070 0 : pd.rhs = error_mark_node;
2071 :
2072 : /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
2073 : most a partial byte before and/or after the region. */
2074 574561 : if (!CONSTANT_CLASS_P (pd.rhs))
2075 : {
2076 532735 : if (pd.offset < offseti)
2077 : {
2078 8667 : HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2079 8667 : gcc_assert (pd.size > o);
2080 8667 : pd.size -= o;
2081 8667 : pd.offset += o;
2082 : }
2083 532735 : if (pd.size + pd.offset > offseti + maxsizei)
2084 7643 : pd.size = maxsizei + ((pd.size + pd.offset - offseti - maxsizei)
2085 7643 : % BITS_PER_UNIT);
2086 : }
2087 :
2088 574561 : pd.offset -= offseti;
2089 :
2090 1149122 : bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2091 574561 : || CONSTANT_CLASS_P (pd.rhs));
2092 574561 : pd_range *r;
2093 574561 : if (partial_defs.is_empty ())
2094 : {
2095 : /* If we get a clobber upfront, fail. */
2096 366860 : if (TREE_CLOBBER_P (pd.rhs))
2097 : return (void *)-1;
2098 366501 : if (!pd_constant_p)
2099 : return (void *)-1;
2100 333774 : partial_defs.safe_push (pd);
2101 333774 : first_range.offset = pd.offset;
2102 333774 : first_range.size = pd.size;
2103 333774 : first_set = set;
2104 333774 : first_base_set = base_set;
2105 333774 : last_vuse_ptr = NULL;
2106 333774 : r = &first_range;
2107 : /* Go check if the first partial definition was a full one in case
2108 : the caller didn't optimize for this. */
2109 : }
2110 : else
2111 : {
2112 207701 : if (!known_ranges)
2113 : {
2114 : /* ??? Optimize the case where the 2nd partial def completes
2115 : things. */
2116 175940 : gcc_obstack_init (&ranges_obstack);
2117 175940 : known_ranges.insert_max_node (&first_range);
2118 : }
2119 : /* Lookup the offset and see if we need to merge. */
2120 207701 : int comparison = known_ranges.lookup_le
2121 419700 : ([&] (pd_range *r) { return pd.offset < r->offset; },
2122 186451 : [&] (pd_range *r) { return pd.offset > r->offset; });
2123 207701 : r = known_ranges.root ();
2124 207701 : if (comparison >= 0
2125 207701 : && ranges_known_overlap_p (r->offset, r->size + 1,
2126 : pd.offset, pd.size))
2127 : {
2128 : /* Ignore partial defs already covered. Here we also drop shadowed
2129 : clobbers arriving here at the floor. */
2130 5931 : if (known_subrange_p (pd.offset, pd.size, r->offset, r->size))
2131 : return NULL;
2132 5036 : r->size = MAX (r->offset + r->size, pd.offset + pd.size) - r->offset;
2133 : }
2134 : else
2135 : {
2136 : /* pd.offset wasn't covered yet, insert the range. */
2137 201770 : void *addr = XOBNEW (&ranges_obstack, pd_range);
2138 201770 : r = new (addr) pd_range { pd.offset, pd.size, {} };
2139 201770 : known_ranges.insert_relative (comparison, r);
2140 : }
2141 : /* Merge r which now contains pd's range and is a member of the splay
2142 : tree with adjacent overlapping ranges. */
2143 206806 : if (known_ranges.splay_next_node ())
2144 23361 : do
2145 : {
2146 23361 : pd_range *rafter = known_ranges.root ();
2147 23361 : if (!ranges_known_overlap_p (r->offset, r->size + 1,
2148 23361 : rafter->offset, rafter->size))
2149 : break;
2150 23091 : r->size = MAX (r->offset + r->size,
2151 23091 : rafter->offset + rafter->size) - r->offset;
2152 : }
2153 23091 : while (known_ranges.remove_root_and_splay_next ());
2154 : /* If we get a clobber, fail. */
2155 206806 : if (TREE_CLOBBER_P (pd.rhs))
2156 : return (void *)-1;
2157 : /* Non-constants are OK as long as they are shadowed by a constant. */
2158 204603 : if (!pd_constant_p)
2159 : return (void *)-1;
2160 198099 : partial_defs.safe_push (pd);
2161 : }
2162 :
2163 : /* Now we have merged pd's range into the range tree. When we have covered
2164 : [offseti, sizei] then the tree will contain exactly one node which has
2165 : the desired properties and it will be 'r'. */
2166 531873 : if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2167 : /* Continue looking for partial defs. */
2168 : return NULL;
2169 :
2170 : /* Now simply native encode all partial defs in reverse order. */
2171 9186 : unsigned ndefs = partial_defs.length ();
2172 : /* We support up to 512-bit values (for V8DFmode). */
2173 9186 : unsigned char buffer[bufsize + 1];
2174 9186 : unsigned char this_buffer[bufsize + 1];
2175 9186 : int len;
2176 :
2177 9186 : memset (buffer, 0, bufsize + 1);
2178 9186 : unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2179 35901 : while (!partial_defs.is_empty ())
2180 : {
2181 26715 : pd_data pd = partial_defs.pop ();
2182 26715 : unsigned int amnt;
2183 26715 : if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2184 : {
2185 : /* Empty CONSTRUCTOR. */
2186 2202 : if (pd.size >= needed_len * BITS_PER_UNIT)
2187 2202 : len = needed_len;
2188 : else
2189 1842 : len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2190 2202 : memset (this_buffer, 0, len);
2191 : }
2192 24513 : else if (pd.rhs_off >= 0)
2193 : {
2194 49026 : len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2195 24513 : (MAX (0, -pd.offset)
2196 24513 : + pd.rhs_off) / BITS_PER_UNIT);
2197 24513 : if (len <= 0
2198 24513 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2199 24513 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2200 : {
2201 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2202 0 : fprintf (dump_file, "Failed to encode %u "
2203 : "partial definitions\n", ndefs);
2204 574636 : return (void *)-1;
2205 : }
2206 : }
2207 : else /* negative pd.rhs_off indicates we want to chop off first bits */
2208 : {
2209 0 : if (-pd.rhs_off >= bufsize)
2210 : return (void *)-1;
2211 0 : len = native_encode_expr (pd.rhs,
2212 0 : this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2213 0 : bufsize - -pd.rhs_off / BITS_PER_UNIT,
2214 0 : MAX (0, -pd.offset) / BITS_PER_UNIT);
2215 0 : if (len <= 0
2216 0 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2217 0 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2218 : {
2219 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2220 0 : fprintf (dump_file, "Failed to encode %u "
2221 : "partial definitions\n", ndefs);
2222 : return (void *)-1;
2223 : }
2224 : }
2225 :
2226 26715 : unsigned char *p = buffer;
2227 26715 : HOST_WIDE_INT size = pd.size;
2228 26715 : if (pd.offset < 0)
2229 369 : size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2230 26715 : this_buffer[len] = 0;
2231 26715 : if (BYTES_BIG_ENDIAN)
2232 : {
2233 : /* LSB of this_buffer[len - 1] byte should be at
2234 : pd.offset + pd.size - 1 bits in buffer. */
2235 : amnt = ((unsigned HOST_WIDE_INT) pd.offset
2236 : + pd.size) % BITS_PER_UNIT;
2237 : if (amnt)
2238 : shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2239 : unsigned char *q = this_buffer;
2240 : unsigned int off = 0;
2241 : if (pd.offset >= 0)
2242 : {
2243 : unsigned int msk;
2244 : off = pd.offset / BITS_PER_UNIT;
2245 : gcc_assert (off < needed_len);
2246 : p = buffer + off;
2247 : if (size <= amnt)
2248 : {
2249 : msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2250 : *p = (*p & ~msk) | (this_buffer[len] & msk);
2251 : size = 0;
2252 : }
2253 : else
2254 : {
2255 : if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2256 : q = (this_buffer + len
2257 : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2258 : / BITS_PER_UNIT));
2259 : if (pd.offset % BITS_PER_UNIT)
2260 : {
2261 : msk = -1U << (BITS_PER_UNIT
2262 : - (pd.offset % BITS_PER_UNIT));
2263 : *p = (*p & msk) | (*q & ~msk);
2264 : p++;
2265 : q++;
2266 : off++;
2267 : size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2268 : gcc_assert (size >= 0);
2269 : }
2270 : }
2271 : }
2272 : else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2273 : {
2274 : q = (this_buffer + len
2275 : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2276 : / BITS_PER_UNIT));
2277 : if (pd.offset % BITS_PER_UNIT)
2278 : {
2279 : q++;
2280 : size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2281 : % BITS_PER_UNIT);
2282 : gcc_assert (size >= 0);
2283 : }
2284 : }
2285 : if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2286 : > needed_len)
2287 : size = (needed_len - off) * BITS_PER_UNIT;
2288 : memcpy (p, q, size / BITS_PER_UNIT);
2289 : if (size % BITS_PER_UNIT)
2290 : {
2291 : unsigned int msk
2292 : = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2293 : p += size / BITS_PER_UNIT;
2294 : q += size / BITS_PER_UNIT;
2295 : *p = (*q & msk) | (*p & ~msk);
2296 : }
2297 : }
2298 : else
2299 : {
2300 26715 : if (pd.offset >= 0)
2301 : {
2302 : /* LSB of this_buffer[0] byte should be at pd.offset bits
2303 : in buffer. */
2304 26346 : unsigned int msk;
2305 26346 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2306 26346 : amnt = pd.offset % BITS_PER_UNIT;
2307 26346 : if (amnt)
2308 1528 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2309 26346 : unsigned int off = pd.offset / BITS_PER_UNIT;
2310 26346 : gcc_assert (off < needed_len);
2311 26346 : size = MIN (size,
2312 : (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2313 26346 : p = buffer + off;
2314 26346 : if (amnt + size < BITS_PER_UNIT)
2315 : {
2316 : /* Low amnt bits come from *p, then size bits
2317 : from this_buffer[0] and the remaining again from
2318 : *p. */
2319 1088 : msk = ((1 << size) - 1) << amnt;
2320 1088 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2321 1088 : size = 0;
2322 : }
2323 25258 : else if (amnt)
2324 : {
2325 1152 : msk = -1U << amnt;
2326 1152 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2327 1152 : p++;
2328 1152 : size -= (BITS_PER_UNIT - amnt);
2329 : }
2330 : }
2331 : else
2332 : {
2333 369 : amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2334 369 : if (amnt)
2335 44 : size -= BITS_PER_UNIT - amnt;
2336 369 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2337 369 : if (amnt)
2338 44 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2339 : }
2340 26715 : memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2341 26715 : p += size / BITS_PER_UNIT;
2342 26715 : if (size % BITS_PER_UNIT)
2343 : {
2344 647 : unsigned int msk = -1U << (size % BITS_PER_UNIT);
2345 647 : *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2346 647 : & ~msk) | (*p & msk);
2347 : }
2348 : }
2349 : }
2350 :
2351 9186 : tree type = vr->type;
2352 : /* Make sure to interpret in a type that has a range covering the whole
2353 : access size. */
2354 9186 : if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2355 : {
2356 0 : if (BITINT_TYPE_P (vr->type)
2357 26 : && maxsizei > MAX_FIXED_MODE_SIZE)
2358 13 : type = build_bitint_type (maxsizei, TYPE_UNSIGNED (type));
2359 : else
2360 0 : type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2361 : }
2362 9186 : tree val;
2363 9186 : if (BYTES_BIG_ENDIAN)
2364 : {
2365 : unsigned sz = needed_len;
2366 : if (maxsizei % BITS_PER_UNIT)
2367 : shift_bytes_in_array_right (buffer, needed_len,
2368 : BITS_PER_UNIT
2369 : - (maxsizei % BITS_PER_UNIT));
2370 : if (INTEGRAL_TYPE_P (type))
2371 : {
2372 : if (TYPE_MODE (type) != BLKmode)
2373 : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2374 : else
2375 : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2376 : }
2377 : if (sz > needed_len)
2378 : {
2379 : memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2380 : val = native_interpret_expr (type, this_buffer, sz);
2381 : }
2382 : else
2383 : val = native_interpret_expr (type, buffer, needed_len);
2384 : }
2385 : else
2386 9186 : val = native_interpret_expr (type, buffer, bufsize);
2387 : /* If we chop off bits because the types precision doesn't match the memory
2388 : access size this is ok when optimizing reads but not when called from
2389 : the DSE code during elimination. */
2390 9186 : if (val && type != vr->type)
2391 : {
2392 13 : if (! int_fits_type_p (val, vr->type))
2393 : val = NULL_TREE;
2394 : else
2395 13 : val = fold_convert (vr->type, val);
2396 : }
2397 :
2398 9182 : if (val)
2399 : {
2400 9182 : if (dump_file && (dump_flags & TDF_DETAILS))
2401 0 : fprintf (dump_file,
2402 : "Successfully combined %u partial definitions\n", ndefs);
2403 : /* We are using the alias-set of the first store we encounter which
2404 : should be appropriate here. */
2405 9182 : return finish (first_set, first_base_set, val);
2406 : }
2407 : else
2408 : {
2409 4 : if (dump_file && (dump_flags & TDF_DETAILS))
2410 0 : fprintf (dump_file,
2411 : "Failed to interpret %u encoded partial definitions\n", ndefs);
2412 : return (void *)-1;
2413 : }
2414 : }
2415 :
2416 : /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2417 : with the current VUSE and performs the expression lookup. */
2418 :
2419 : static void *
2420 1100024912 : vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2421 : {
2422 1100024912 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2423 1100024912 : vn_reference_t vr = data->vr;
2424 1100024912 : vn_reference_s **slot;
2425 1100024912 : hashval_t hash;
2426 :
2427 : /* If we have partial definitions recorded we have to go through
2428 : vn_reference_lookup_3. */
2429 1100024912 : if (!data->partial_defs.is_empty ())
2430 : return NULL;
2431 :
2432 1099229067 : if (data->last_vuse_ptr)
2433 : {
2434 1077886524 : *data->last_vuse_ptr = vuse;
2435 1077886524 : data->last_vuse = vuse;
2436 : }
2437 :
2438 : /* Fixup vuse and hash. */
2439 1099229067 : if (vr->vuse)
2440 1099229067 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2441 1099229067 : vr->vuse = vuse_ssa_val (vuse);
2442 1099229067 : if (vr->vuse)
2443 1099229067 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2444 :
2445 1099229067 : hash = vr->hashcode;
2446 1099229067 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2447 1099229067 : if (slot)
2448 : {
2449 8058857 : if ((*slot)->result && data->saved_operands.exists ())
2450 440332 : return data->finish (vr->set, vr->base_set, (*slot)->result);
2451 : return *slot;
2452 : }
2453 :
2454 1091170210 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2455 : {
2456 18517358 : HOST_WIDE_INT op_offset, op_size;
2457 18517358 : tree v = NULL_TREE;
2458 18517358 : tree base = ao_ref_base (op);
2459 :
2460 18517358 : if (base
2461 18517358 : && op->offset.is_constant (&op_offset)
2462 18517358 : && op->size.is_constant (&op_size)
2463 18517358 : && op->max_size_known_p ()
2464 36575916 : && known_eq (op->size, op->max_size))
2465 : {
2466 17755245 : if (TREE_CODE (base) == PARM_DECL)
2467 679439 : v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2468 : op_size);
2469 17075806 : else if (TREE_CODE (base) == MEM_REF
2470 7109735 : && integer_zerop (TREE_OPERAND (base, 1))
2471 5710837 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2472 5705634 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2473 20876387 : && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2474 : == PARM_DECL))
2475 3746563 : v = ipcp_get_aggregate_const (cfun,
2476 3746563 : SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2477 : true, op_offset, op_size);
2478 : }
2479 4426002 : if (v)
2480 1176 : return data->finish (vr->set, vr->base_set, v);
2481 : }
2482 :
2483 : return NULL;
2484 : }
2485 :
2486 : /* Lookup an existing or insert a new vn_reference entry into the
2487 : value table for the VUSE, SET, TYPE, OPERANDS reference which
2488 : has the value VALUE which is either a constant or an SSA name. */
2489 :
2490 : static vn_reference_t
2491 1593752 : vn_reference_lookup_or_insert_for_pieces (tree vuse,
2492 : alias_set_type set,
2493 : alias_set_type base_set,
2494 : poly_int64 offset,
2495 : poly_int64 max_size,
2496 : tree type,
2497 : vec<vn_reference_op_s,
2498 : va_heap> operands,
2499 : tree value)
2500 : {
2501 1593752 : vn_reference_s vr1;
2502 1593752 : vn_reference_t result;
2503 1593752 : unsigned value_id;
2504 1593752 : vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2505 1593752 : vr1.operands = operands;
2506 1593752 : vr1.type = type;
2507 1593752 : vr1.set = set;
2508 1593752 : vr1.base_set = base_set;
2509 1593752 : vr1.offset = offset;
2510 1593752 : vr1.max_size = max_size;
2511 1593752 : vr1.hashcode = vn_reference_compute_hash (&vr1);
2512 1593752 : if (vn_reference_lookup_1 (&vr1, &result))
2513 8319 : return result;
2514 :
2515 1585433 : if (TREE_CODE (value) == SSA_NAME)
2516 367186 : value_id = VN_INFO (value)->value_id;
2517 : else
2518 1218247 : value_id = get_or_alloc_constant_value_id (value);
2519 1585433 : return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
2520 1585433 : type, operands.copy (), value, value_id);
2521 : }
2522 :
2523 : /* Return a value-number for RCODE OPS... either by looking up an existing
2524 : value-number for the possibly simplified result or by inserting the
2525 : operation if INSERT is true. If SIMPLIFY is false, return a value
2526 : number for the unsimplified expression. */
2527 :
2528 : static tree
2529 18953005 : vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2530 : bool simplify)
2531 : {
2532 18953005 : tree result = NULL_TREE;
2533 : /* We will be creating a value number for
2534 : RCODE (OPS...).
2535 : So first simplify and lookup this expression to see if it
2536 : is already available. */
2537 : /* For simplification valueize. */
2538 18953005 : unsigned i = 0;
2539 18953005 : if (simplify)
2540 43976939 : for (i = 0; i < res_op->num_ops; ++i)
2541 25031321 : if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2542 : {
2543 16056196 : tree tem = vn_valueize (res_op->ops[i]);
2544 16056196 : if (!tem)
2545 : break;
2546 16056196 : res_op->ops[i] = tem;
2547 : }
2548 : /* If valueization of an operand fails (it is not available), skip
2549 : simplification. */
2550 18953005 : bool res = false;
2551 18953005 : if (i == res_op->num_ops)
2552 : {
2553 : /* Do not leak not available operands into the simplified expression
2554 : when called from PRE context. */
2555 18945618 : if (rpo_avail)
2556 11355412 : mprts_hook = vn_lookup_simplify_result;
2557 18945618 : res = res_op->resimplify (NULL, vn_valueize);
2558 18945618 : mprts_hook = NULL;
2559 : }
2560 32656629 : gimple *new_stmt = NULL;
2561 18945618 : if (res
2562 18945618 : && gimple_simplified_result_is_gimple_val (res_op))
2563 : {
2564 : /* The expression is already available. */
2565 5241994 : result = res_op->ops[0];
2566 : /* Valueize it, simplification returns sth in AVAIL only. */
2567 5241994 : if (TREE_CODE (result) == SSA_NAME)
2568 293866 : result = SSA_VAL (result);
2569 : }
2570 : else
2571 : {
2572 13711011 : tree val = vn_lookup_simplify_result (res_op);
2573 : /* ??? In weird cases we can end up with internal-fn calls,
2574 : but this isn't expected so throw the result away. See
2575 : PR123040 for an example. Likewise we can end up with
2576 : &MEM[ptr_1 + CST] which would be a vn_reference (PR127000). */
2577 13711011 : if (!val
2578 13711011 : && insert
2579 148881 : && res_op->code.is_tree_code ()
2580 13859892 : && (tree_code) res_op->code != ADDR_EXPR)
2581 : {
2582 148881 : gimple_seq stmts = NULL;
2583 148881 : result = maybe_push_res_to_seq (res_op, &stmts);
2584 148881 : if (result)
2585 : {
2586 148875 : gcc_assert (gimple_seq_singleton_p (stmts));
2587 148875 : new_stmt = gimple_seq_first_stmt (stmts);
2588 : }
2589 : }
2590 : else
2591 : /* The expression is already available. */
2592 : result = val;
2593 : }
2594 293872 : if (new_stmt)
2595 : {
2596 : /* The expression is not yet available, value-number lhs to
2597 : the new SSA_NAME we created. */
2598 : /* Initialize value-number information properly. */
2599 148875 : vn_ssa_aux_t result_info = VN_INFO (result);
2600 148875 : result_info->valnum = result;
2601 148875 : result_info->value_id = get_next_value_id ();
2602 148875 : result_info->visited = 1;
2603 148875 : gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2604 : new_stmt);
2605 148875 : result_info->needs_insertion = true;
2606 : /* ??? PRE phi-translation inserts NARYs without corresponding
2607 : SSA name result. Re-use those but set their result according
2608 : to the stmt we just built. */
2609 148875 : vn_nary_op_t nary = NULL;
2610 148875 : vn_nary_op_lookup_stmt (new_stmt, &nary);
2611 148875 : if (nary)
2612 : {
2613 0 : gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2614 0 : nary->u.result = gimple_assign_lhs (new_stmt);
2615 : }
2616 : /* As all "inserted" statements are singleton SCCs, insert
2617 : to the valid table. This is strictly needed to
2618 : avoid re-generating new value SSA_NAMEs for the same
2619 : expression during SCC iteration over and over (the
2620 : optimistic table gets cleared after each iteration).
2621 : We do not need to insert into the optimistic table, as
2622 : lookups there will fall back to the valid table. */
2623 : else
2624 : {
2625 148875 : unsigned int length = vn_nary_length_from_stmt (new_stmt);
2626 148875 : vn_nary_op_t vno1
2627 148875 : = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2628 148875 : vno1->value_id = result_info->value_id;
2629 148875 : vno1->length = length;
2630 148875 : vno1->predicated_values = 0;
2631 148875 : vno1->u.result = result;
2632 148875 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2633 148875 : vn_nary_op_insert_into (vno1, valid_info->nary);
2634 : /* Also do not link it into the undo chain. */
2635 148875 : last_inserted_nary = vno1->next;
2636 148875 : vno1->next = (vn_nary_op_t)(void *)-1;
2637 : }
2638 148875 : if (dump_file && (dump_flags & TDF_DETAILS))
2639 : {
2640 595 : fprintf (dump_file, "Inserting name ");
2641 595 : print_generic_expr (dump_file, result);
2642 595 : fprintf (dump_file, " for expression ");
2643 595 : print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2644 595 : fprintf (dump_file, "\n");
2645 : }
2646 : }
2647 18953005 : return result;
2648 : }
2649 :
2650 : /* Return a value-number for RCODE OPS... either by looking up an existing
2651 : value-number for the simplified result or by inserting the operation. */
2652 :
2653 : static tree
2654 194445 : vn_nary_build_or_lookup (gimple_match_op *res_op)
2655 : {
2656 0 : return vn_nary_build_or_lookup_1 (res_op, true, true);
2657 : }
2658 :
2659 : /* Try to simplify the expression RCODE OPS... of type TYPE and return
2660 : its value if present. Update NARY with a simplified expression if
2661 : it fits. */
2662 :
2663 : tree
2664 7586986 : vn_nary_simplify (vn_nary_op_t nary)
2665 : {
2666 7586986 : if (nary->length > gimple_match_op::MAX_NUM_OPS
2667 : /* For CONSTRUCTOR the vn_nary_op_t and gimple_match_op representation
2668 : does not match. */
2669 7586440 : || nary->opcode == CONSTRUCTOR)
2670 : return NULL_TREE;
2671 7583947 : gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2672 7583947 : nary->type, nary->length);
2673 7583947 : memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2674 7583947 : tree res = vn_nary_build_or_lookup_1 (&op, false, true);
2675 : /* Do not update *NARY with a simplified result that contains abnormals.
2676 : This matches what maybe_push_res_to_seq does when requesting insertion. */
2677 27479761 : for (unsigned i = 0; i < op.num_ops; ++i)
2678 12311949 : if (TREE_CODE (op.ops[i]) == SSA_NAME
2679 12311949 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op.ops[i]))
2680 : return res;
2681 7583865 : if (op.code.is_tree_code ()
2682 7583865 : && op.num_ops <= nary->length
2683 15166947 : && (tree_code) op.code != CONSTRUCTOR)
2684 : {
2685 7583081 : nary->opcode = (tree_code) op.code;
2686 7583081 : nary->length = op.num_ops;
2687 19893343 : for (unsigned i = 0; i < op.num_ops; ++i)
2688 12310262 : nary->op[i] = op.ops[i];
2689 : }
2690 : return res;
2691 : }
2692 :
2693 : /* Elimination engine. */
2694 :
2695 : class eliminate_dom_walker : public dom_walker
2696 : {
2697 : public:
2698 : eliminate_dom_walker (cdi_direction, bitmap);
2699 : ~eliminate_dom_walker ();
2700 :
2701 : edge before_dom_children (basic_block) final override;
2702 : void after_dom_children (basic_block) final override;
2703 :
2704 : virtual tree eliminate_avail (basic_block, tree op);
2705 : virtual void eliminate_push_avail (basic_block, tree op);
2706 : tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2707 :
2708 : void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2709 :
2710 : unsigned eliminate_cleanup (bool region_p = false);
2711 :
2712 : bool do_pre;
2713 : unsigned int el_todo;
2714 : unsigned int eliminations;
2715 : unsigned int insertions;
2716 :
2717 : /* SSA names that had their defs inserted by PRE if do_pre. */
2718 : bitmap inserted_exprs;
2719 :
2720 : /* Blocks with statements that have had their EH properties changed. */
2721 : bitmap need_eh_cleanup;
2722 :
2723 : /* Blocks with statements that have had their AB properties changed. */
2724 : bitmap need_ab_cleanup;
2725 :
2726 : /* Local state for the eliminate domwalk. */
2727 : auto_vec<gimple *> to_remove;
2728 : auto_vec<gimple *> to_fixup;
2729 : auto_vec<tree> avail;
2730 : auto_vec<tree> avail_stack;
2731 : };
2732 :
2733 : /* Adaptor to the elimination engine using RPO availability. */
2734 :
2735 12636810 : class rpo_elim : public eliminate_dom_walker
2736 : {
2737 : public:
2738 6318405 : rpo_elim(basic_block entry_)
2739 6318405 : : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2740 12636810 : m_avail_freelist (NULL) {}
2741 :
2742 : tree eliminate_avail (basic_block, tree op) final override;
2743 :
2744 : void eliminate_push_avail (basic_block, tree) final override;
2745 :
2746 : basic_block entry;
2747 : /* Freelist of avail entries which are allocated from the vn_ssa_aux
2748 : obstack. */
2749 : vn_avail *m_avail_freelist;
2750 : };
2751 :
2752 : /* Return true if BASE1 and BASE2 can be adjusted so they have the
2753 : same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2754 : Otherwise return false. */
2755 :
2756 : static bool
2757 7007058 : adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2758 : tree base2, poly_int64 *offset2)
2759 : {
2760 7007058 : poly_int64 soff;
2761 7007058 : if (TREE_CODE (base1) == MEM_REF
2762 3187683 : && TREE_CODE (base2) == MEM_REF)
2763 : {
2764 2557637 : if (mem_ref_offset (base1).to_shwi (&soff))
2765 : {
2766 2557637 : base1 = TREE_OPERAND (base1, 0);
2767 2557637 : *offset1 += soff * BITS_PER_UNIT;
2768 : }
2769 2557637 : if (mem_ref_offset (base2).to_shwi (&soff))
2770 : {
2771 2557637 : base2 = TREE_OPERAND (base2, 0);
2772 2557637 : *offset2 += soff * BITS_PER_UNIT;
2773 : }
2774 2557637 : return operand_equal_p (base1, base2, 0);
2775 : }
2776 4449421 : return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2777 : }
2778 :
2779 : /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2780 : from the statement defining VUSE and if not successful tries to
2781 : translate *REFP and VR_ through an aggregate copy at the definition
2782 : of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2783 : of *REF and *VR. If only disambiguation was performed then
2784 : *DISAMBIGUATE_ONLY is set to true. */
2785 :
2786 : static void *
2787 43348128 : vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2788 : translate_flags *disambiguate_only)
2789 : {
2790 43348128 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2791 43348128 : vn_reference_t vr = data->vr;
2792 43348128 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2793 43348128 : tree base = ao_ref_base (ref);
2794 43348128 : HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2795 43348128 : static vec<vn_reference_op_s> lhs_ops;
2796 43348128 : ao_ref lhs_ref;
2797 43348128 : bool lhs_ref_ok = false;
2798 43348128 : poly_int64 copy_size;
2799 :
2800 : /* First try to disambiguate after value-replacing in the definitions LHS. */
2801 43348128 : if (is_gimple_assign (def_stmt))
2802 : {
2803 21263048 : tree lhs = gimple_assign_lhs (def_stmt);
2804 21263048 : bool valueized_anything = false;
2805 : /* Avoid re-allocation overhead. */
2806 21263048 : lhs_ops.truncate (0);
2807 21263048 : basic_block saved_rpo_bb = vn_context_bb;
2808 21263048 : vn_context_bb = gimple_bb (def_stmt);
2809 21263048 : if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2810 : {
2811 13927165 : copy_reference_ops_from_ref (lhs, &lhs_ops);
2812 13927165 : valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2813 : }
2814 21263048 : vn_context_bb = saved_rpo_bb;
2815 21263048 : ao_ref_init (&lhs_ref, lhs);
2816 21263048 : lhs_ref_ok = true;
2817 21263048 : if (valueized_anything
2818 2078214 : && ao_ref_init_from_vn_reference
2819 2078214 : (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2820 2078214 : ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2821 23341262 : && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2822 : {
2823 1777415 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2824 6699107 : return NULL;
2825 : }
2826 :
2827 : /* When the def is a CLOBBER we can optimistically disambiguate
2828 : against it since any overlap it would be undefined behavior.
2829 : Avoid this for obvious must aliases to save compile-time though.
2830 : We also may not do this when the query is used for redundant
2831 : store removal. */
2832 19485633 : if (!data->redundant_store_removal_p
2833 10706450 : && gimple_clobber_p (def_stmt)
2834 20003504 : && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2835 : {
2836 491630 : *disambiguate_only = TR_DISAMBIGUATE;
2837 491630 : return NULL;
2838 : }
2839 :
2840 : /* Besides valueizing the LHS we can also use access-path based
2841 : disambiguation on the original non-valueized ref. */
2842 18994003 : if (!ref->ref
2843 : && lhs_ref_ok
2844 2731299 : && data->orig_ref.ref)
2845 : {
2846 : /* We want to use the non-valueized LHS for this, but avoid redundant
2847 : work. */
2848 1897501 : ao_ref *lref = &lhs_ref;
2849 1897501 : ao_ref lref_alt;
2850 1897501 : if (valueized_anything)
2851 : {
2852 119323 : ao_ref_init (&lref_alt, lhs);
2853 119323 : lref = &lref_alt;
2854 : }
2855 1897501 : if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2856 : {
2857 315190 : *disambiguate_only = (valueized_anything
2858 157595 : ? TR_VALUEIZE_AND_DISAMBIGUATE
2859 : : TR_DISAMBIGUATE);
2860 157595 : return NULL;
2861 : }
2862 : }
2863 :
2864 : /* If we reach a clobbering statement try to skip it and see if
2865 : we find a VN result with exactly the same value as the
2866 : possible clobber. In this case we can ignore the clobber
2867 : and return the found value. */
2868 18836408 : if (!gimple_has_volatile_ops (def_stmt)
2869 17419131 : && ((is_gimple_reg_type (TREE_TYPE (lhs))
2870 12790928 : && types_compatible_p (TREE_TYPE (lhs), vr->type)
2871 9927292 : && !storage_order_barrier_p (lhs)
2872 9927292 : && !reverse_storage_order_for_component_p (lhs))
2873 7491843 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == CONSTRUCTOR)
2874 11012625 : && (ref->ref || data->orig_ref.ref)
2875 10535215 : && !data->mask
2876 10512625 : && data->partial_defs.is_empty ()
2877 10510217 : && multiple_p (get_object_alignment
2878 : (ref->ref ? ref->ref : data->orig_ref.ref),
2879 : ref->size)
2880 42096789 : && multiple_p (get_object_alignment (lhs), ref->size))
2881 : {
2882 10113717 : HOST_WIDE_INT offset2i, size2i;
2883 10113717 : poly_int64 offset = ref->offset;
2884 10113717 : poly_int64 maxsize = ref->max_size;
2885 :
2886 10113717 : gcc_assert (lhs_ref_ok);
2887 10113717 : tree base2 = ao_ref_base (&lhs_ref);
2888 10113717 : poly_int64 offset2 = lhs_ref.offset;
2889 10113717 : poly_int64 size2 = lhs_ref.size;
2890 10113717 : poly_int64 maxsize2 = lhs_ref.max_size;
2891 :
2892 10113717 : tree rhs = gimple_assign_rhs1 (def_stmt);
2893 10113717 : if (TREE_CODE (rhs) == CONSTRUCTOR)
2894 1055600 : rhs = integer_zero_node;
2895 : /* ??? We may not compare to ahead values which might be from
2896 : a different loop iteration but only to loop invariants. Use
2897 : CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2898 : The one-hop lookup below doesn't have this issue since there's
2899 : a virtual PHI before we ever reach a backedge to cross.
2900 : We can skip multiple defs as long as they are from the same
2901 : value though. */
2902 10113717 : if (data->same_val
2903 10113717 : && !operand_equal_p (data->same_val, rhs))
2904 : ;
2905 : /* When this is a (partial) must-def, leave it to handling
2906 : below in case we are interested in the value. */
2907 9846348 : else if (!(*disambiguate_only > TR_TRANSLATE)
2908 3388893 : && base2
2909 3388893 : && known_eq (maxsize2, size2)
2910 2394616 : && adjust_offsets_for_equal_base_address (base, &offset,
2911 : base2, &offset2)
2912 1172461 : && offset2.is_constant (&offset2i)
2913 1172461 : && size2.is_constant (&size2i)
2914 1172461 : && maxsize.is_constant (&maxsizei)
2915 1172461 : && offset.is_constant (&offseti)
2916 11018809 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
2917 : size2i))
2918 : ;
2919 8769744 : else if (CONSTANT_CLASS_P (rhs))
2920 : {
2921 4206909 : if (dump_file && (dump_flags & TDF_DETAILS))
2922 : {
2923 2218 : fprintf (dump_file,
2924 : "Skipping possible redundant definition ");
2925 2218 : print_gimple_stmt (dump_file, def_stmt, 0);
2926 : }
2927 : /* Delay the actual compare of the values to the end of the walk
2928 : but do not update last_vuse from here. */
2929 4206909 : data->last_vuse_ptr = NULL;
2930 4206909 : data->same_val = rhs;
2931 4272467 : return NULL;
2932 : }
2933 : else
2934 : {
2935 4562835 : tree saved_vuse = vr->vuse;
2936 4562835 : hashval_t saved_hashcode = vr->hashcode;
2937 4562835 : if (vr->vuse)
2938 4562835 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2939 9125670 : vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
2940 4562835 : if (vr->vuse)
2941 4562835 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2942 4562835 : vn_reference_t vnresult = NULL;
2943 : /* Do not use vn_reference_lookup_2 since that might perform
2944 : expression hashtable insertion but this lookup crosses
2945 : a possible may-alias making such insertion conditionally
2946 : invalid. */
2947 4562835 : vn_reference_lookup_1 (vr, &vnresult);
2948 : /* Need to restore vr->vuse and vr->hashcode. */
2949 4562835 : vr->vuse = saved_vuse;
2950 4562835 : vr->hashcode = saved_hashcode;
2951 4562835 : if (vnresult)
2952 : {
2953 245874 : if (TREE_CODE (rhs) == SSA_NAME)
2954 244352 : rhs = SSA_VAL (rhs);
2955 245874 : if (vnresult->result
2956 245874 : && operand_equal_p (vnresult->result, rhs, 0))
2957 65558 : return vnresult;
2958 : }
2959 : }
2960 : }
2961 : }
2962 22085080 : else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2963 19849889 : && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2964 24205779 : && gimple_call_num_args (def_stmt) <= 4)
2965 : {
2966 : /* For builtin calls valueize its arguments and call the
2967 : alias oracle again. Valueization may improve points-to
2968 : info of pointers and constify size and position arguments.
2969 : Originally this was motivated by PR61034 which has
2970 : conditional calls to free falsely clobbering ref because
2971 : of imprecise points-to info of the argument. */
2972 : tree oldargs[4];
2973 : bool valueized_anything = false;
2974 5014521 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2975 : {
2976 3455184 : oldargs[i] = gimple_call_arg (def_stmt, i);
2977 3455184 : tree val = vn_valueize (oldargs[i]);
2978 3455184 : if (val != oldargs[i])
2979 : {
2980 129198 : gimple_call_set_arg (def_stmt, i, val);
2981 129198 : valueized_anything = true;
2982 : }
2983 : }
2984 1559337 : if (valueized_anything)
2985 : {
2986 100280 : bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2987 : ref, data->tbaa_p);
2988 465311 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2989 264751 : gimple_call_set_arg (def_stmt, i, oldargs[i]);
2990 100280 : if (!res)
2991 : {
2992 31332 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2993 31332 : return NULL;
2994 : }
2995 : }
2996 : }
2997 :
2998 36617689 : if (*disambiguate_only > TR_TRANSLATE)
2999 : return (void *)-1;
3000 :
3001 : /* If we cannot constrain the size of the reference we cannot
3002 : test if anything kills it. */
3003 24403579 : if (!ref->max_size_known_p ())
3004 : return (void *)-1;
3005 :
3006 23970614 : poly_int64 offset = ref->offset;
3007 23970614 : poly_int64 maxsize = ref->max_size;
3008 :
3009 : /* def_stmt may-defs *ref. See if we can derive a value for *ref
3010 : from that definition.
3011 : 1) Memset. */
3012 23970614 : if (is_gimple_reg_type (vr->type)
3013 23964801 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
3014 23874619 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
3015 90716 : && (integer_zerop (gimple_call_arg (def_stmt, 1))
3016 32610 : || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
3017 8936 : || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
3018 : && CHAR_BIT == 8
3019 : && BITS_PER_UNIT == 8
3020 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3021 31288 : && offset.is_constant (&offseti)
3022 31288 : && ref->size.is_constant (&sizei)
3023 31288 : && (offseti % BITS_PER_UNIT == 0
3024 39 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
3025 89394 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
3026 36578 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3027 36578 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
3028 24024001 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3029 29860 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
3030 : {
3031 53346 : tree base2;
3032 53346 : poly_int64 offset2, size2, maxsize2;
3033 53346 : bool reverse;
3034 53346 : tree ref2 = gimple_call_arg (def_stmt, 0);
3035 53346 : if (TREE_CODE (ref2) == SSA_NAME)
3036 : {
3037 29819 : ref2 = SSA_VAL (ref2);
3038 29819 : if (TREE_CODE (ref2) == SSA_NAME
3039 29819 : && (TREE_CODE (base) != MEM_REF
3040 19100 : || TREE_OPERAND (base, 0) != ref2))
3041 : {
3042 23497 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
3043 23497 : if (gimple_assign_single_p (def_stmt)
3044 23497 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3045 818 : ref2 = gimple_assign_rhs1 (def_stmt);
3046 : }
3047 : }
3048 53346 : if (TREE_CODE (ref2) == ADDR_EXPR)
3049 : {
3050 27270 : ref2 = TREE_OPERAND (ref2, 0);
3051 27270 : base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
3052 : &reverse);
3053 27270 : if (!known_size_p (maxsize2)
3054 27230 : || !known_eq (maxsize2, size2)
3055 54432 : || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
3056 52982 : return (void *)-1;
3057 : }
3058 26076 : else if (TREE_CODE (ref2) == SSA_NAME)
3059 : {
3060 26076 : poly_int64 soff;
3061 26076 : if (TREE_CODE (base) != MEM_REF
3062 44574 : || !(mem_ref_offset (base)
3063 44574 : << LOG2_BITS_PER_UNIT).to_shwi (&soff))
3064 22054 : return (void *)-1;
3065 18498 : offset += soff;
3066 18498 : offset2 = 0;
3067 18498 : if (TREE_OPERAND (base, 0) != ref2)
3068 : {
3069 15101 : gimple *def = SSA_NAME_DEF_STMT (ref2);
3070 15101 : if (is_gimple_assign (def)
3071 13653 : && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
3072 11803 : && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
3073 15756 : && poly_int_tree_p (gimple_assign_rhs2 (def)))
3074 : {
3075 625 : tree rhs2 = gimple_assign_rhs2 (def);
3076 625 : if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
3077 : SIGNED)
3078 625 : << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
3079 : return (void *)-1;
3080 625 : ref2 = gimple_assign_rhs1 (def);
3081 625 : if (TREE_CODE (ref2) == SSA_NAME)
3082 625 : ref2 = SSA_VAL (ref2);
3083 : }
3084 : else
3085 : return (void *)-1;
3086 : }
3087 : }
3088 : else
3089 : return (void *)-1;
3090 27313 : tree len = gimple_call_arg (def_stmt, 2);
3091 27313 : HOST_WIDE_INT leni, offset2i;
3092 27313 : if (TREE_CODE (len) == SSA_NAME)
3093 259 : len = SSA_VAL (len);
3094 : /* Sometimes the above trickery is smarter than alias analysis. Take
3095 : advantage of that. */
3096 27313 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
3097 54626 : (wi::to_poly_offset (len)
3098 27313 : << LOG2_BITS_PER_UNIT)))
3099 : return NULL;
3100 54568 : if (data->partial_defs.is_empty ()
3101 27255 : && known_subrange_p (offset, maxsize, offset2,
3102 27255 : wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
3103 : {
3104 26752 : tree val;
3105 26752 : if (integer_zerop (gimple_call_arg (def_stmt, 1)))
3106 21735 : val = build_zero_cst (vr->type);
3107 5017 : else if (INTEGRAL_TYPE_P (vr->type)
3108 3877 : && known_eq (ref->size, 8)
3109 8003 : && offseti % BITS_PER_UNIT == 0)
3110 : {
3111 2986 : gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
3112 2986 : vr->type, gimple_call_arg (def_stmt, 1));
3113 2986 : val = vn_nary_build_or_lookup (&res_op);
3114 2986 : if (!val
3115 2986 : || (TREE_CODE (val) == SSA_NAME
3116 616 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3117 0 : return (void *)-1;
3118 : }
3119 : else
3120 : {
3121 2031 : unsigned buflen
3122 2031 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
3123 2031 : if (INTEGRAL_TYPE_P (vr->type)
3124 2031 : && TYPE_MODE (vr->type) != BLKmode)
3125 1780 : buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
3126 2031 : unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
3127 2031 : memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
3128 : buflen);
3129 2031 : if (BYTES_BIG_ENDIAN)
3130 : {
3131 : unsigned int amnt
3132 : = (((unsigned HOST_WIDE_INT) offseti + sizei)
3133 : % BITS_PER_UNIT);
3134 : if (amnt)
3135 : {
3136 : shift_bytes_in_array_right (buf, buflen,
3137 : BITS_PER_UNIT - amnt);
3138 : buf++;
3139 : buflen--;
3140 : }
3141 : }
3142 2031 : else if (offseti % BITS_PER_UNIT != 0)
3143 : {
3144 7 : unsigned int amnt
3145 : = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3146 7 : % BITS_PER_UNIT);
3147 7 : shift_bytes_in_array_left (buf, buflen, amnt);
3148 7 : buf++;
3149 7 : buflen--;
3150 : }
3151 2031 : val = native_interpret_expr (vr->type, buf, buflen);
3152 2031 : if (!val)
3153 : return (void *)-1;
3154 : }
3155 26752 : return data->finish (0, 0, val);
3156 : }
3157 : /* For now handle clearing memory with partial defs. */
3158 561 : else if (known_eq (ref->size, maxsize)
3159 484 : && integer_zerop (gimple_call_arg (def_stmt, 1))
3160 201 : && tree_fits_poly_int64_p (len)
3161 197 : && tree_to_poly_int64 (len).is_constant (&leni)
3162 197 : && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3163 197 : && offset.is_constant (&offseti)
3164 197 : && offset2.is_constant (&offset2i)
3165 197 : && maxsize.is_constant (&maxsizei)
3166 561 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3167 561 : leni << LOG2_BITS_PER_UNIT))
3168 : {
3169 197 : pd_data pd;
3170 197 : pd.rhs = build_constructor (NULL_TREE, NULL);
3171 197 : pd.rhs_off = 0;
3172 197 : pd.offset = offset2i;
3173 197 : pd.size = leni << LOG2_BITS_PER_UNIT;
3174 197 : return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3175 : }
3176 : }
3177 :
3178 : /* 2) Assignment from an empty CONSTRUCTOR. */
3179 23917268 : else if (is_gimple_reg_type (vr->type)
3180 23911455 : && gimple_assign_single_p (def_stmt)
3181 7907779 : && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3182 2009210 : && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0
3183 25926478 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt)))
3184 : {
3185 2009178 : tree base2;
3186 2009178 : poly_int64 offset2, size2, maxsize2;
3187 2009178 : HOST_WIDE_INT offset2i, size2i;
3188 2009178 : gcc_assert (lhs_ref_ok);
3189 2009178 : base2 = ao_ref_base (&lhs_ref);
3190 2009178 : offset2 = lhs_ref.offset;
3191 2009178 : size2 = lhs_ref.size;
3192 2009178 : maxsize2 = lhs_ref.max_size;
3193 2009178 : if (known_size_p (maxsize2)
3194 2009140 : && known_eq (maxsize2, size2)
3195 4018272 : && adjust_offsets_for_equal_base_address (base, &offset,
3196 : base2, &offset2))
3197 : {
3198 1981020 : if (data->partial_defs.is_empty ()
3199 1977435 : && known_subrange_p (offset, maxsize, offset2, size2))
3200 : {
3201 : /* While technically undefined behavior do not optimize
3202 : a full read from a clobber. */
3203 1976517 : if (gimple_clobber_p (def_stmt))
3204 1980970 : return (void *)-1;
3205 1006395 : tree val = build_zero_cst (vr->type);
3206 1006395 : return data->finish (ao_ref_alias_set (&lhs_ref),
3207 1006395 : ao_ref_base_alias_set (&lhs_ref), val);
3208 : }
3209 4503 : else if (known_eq (ref->size, maxsize)
3210 4453 : && maxsize.is_constant (&maxsizei)
3211 4453 : && offset.is_constant (&offseti)
3212 4453 : && offset2.is_constant (&offset2i)
3213 4453 : && size2.is_constant (&size2i)
3214 4503 : && ranges_known_overlap_p (offseti, maxsizei,
3215 : offset2i, size2i))
3216 : {
3217 : /* Let clobbers be consumed by the partial-def tracker
3218 : which can choose to ignore them if they are shadowed
3219 : by a later def. */
3220 4453 : pd_data pd;
3221 4453 : pd.rhs = gimple_assign_rhs1 (def_stmt);
3222 4453 : pd.rhs_off = 0;
3223 4453 : pd.offset = offset2i;
3224 4453 : pd.size = size2i;
3225 4453 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3226 : ao_ref_base_alias_set (&lhs_ref),
3227 : offseti, maxsizei);
3228 : }
3229 : }
3230 : }
3231 :
3232 : /* 3) Assignment from a constant. We can use folds native encode/interpret
3233 : routines to extract the assigned bits. */
3234 21908090 : else if (known_eq (ref->size, maxsize)
3235 21382401 : && is_gimple_reg_type (vr->type)
3236 21376588 : && !reverse_storage_order_for_component_p (vr->operands)
3237 21373785 : && !contains_storage_order_barrier_p (vr->operands)
3238 21373785 : && gimple_assign_single_p (def_stmt)
3239 5574858 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3240 : && CHAR_BIT == 8
3241 : && BITS_PER_UNIT == 8
3242 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3243 : /* native_encode and native_decode operate on arrays of bytes
3244 : and so fundamentally need a compile-time size and offset. */
3245 5571897 : && maxsize.is_constant (&maxsizei)
3246 5571897 : && offset.is_constant (&offseti)
3247 27479987 : && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3248 4695827 : || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3249 1906112 : && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3250 : {
3251 892902 : tree lhs = gimple_assign_lhs (def_stmt);
3252 892902 : tree base2;
3253 892902 : poly_int64 offset2, size2, maxsize2;
3254 892902 : HOST_WIDE_INT offset2i, size2i;
3255 892902 : bool reverse;
3256 892902 : gcc_assert (lhs_ref_ok);
3257 892902 : base2 = ao_ref_base (&lhs_ref);
3258 892902 : offset2 = lhs_ref.offset;
3259 892902 : size2 = lhs_ref.size;
3260 892902 : maxsize2 = lhs_ref.max_size;
3261 892902 : reverse = reverse_storage_order_for_component_p (lhs);
3262 892902 : if (base2
3263 892902 : && !reverse
3264 892074 : && !storage_order_barrier_p (lhs)
3265 892074 : && known_eq (maxsize2, size2)
3266 859070 : && adjust_offsets_for_equal_base_address (base, &offset,
3267 : base2, &offset2)
3268 87050 : && offset.is_constant (&offseti)
3269 87050 : && offset2.is_constant (&offset2i)
3270 892902 : && size2.is_constant (&size2i))
3271 : {
3272 87050 : if (data->partial_defs.is_empty ()
3273 69599 : && known_subrange_p (offseti, maxsizei, offset2, size2))
3274 : {
3275 : /* We support up to 512-bit values (for V8DFmode). */
3276 45109 : unsigned char buffer[65];
3277 45109 : int len;
3278 :
3279 45109 : tree rhs = gimple_assign_rhs1 (def_stmt);
3280 45109 : if (TREE_CODE (rhs) == SSA_NAME)
3281 1786 : rhs = SSA_VAL (rhs);
3282 90218 : len = native_encode_expr (rhs,
3283 : buffer, sizeof (buffer) - 1,
3284 45109 : (offseti - offset2i) / BITS_PER_UNIT);
3285 45109 : if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
3286 : {
3287 42087 : tree type = vr->type;
3288 42087 : unsigned char *buf = buffer;
3289 42087 : unsigned int amnt = 0;
3290 : /* Make sure to interpret in a type that has a range
3291 : covering the whole access size. */
3292 42087 : if (INTEGRAL_TYPE_P (vr->type)
3293 42087 : && maxsizei != TYPE_PRECISION (vr->type))
3294 : {
3295 1013 : bool uns = TYPE_UNSIGNED (type);
3296 1012 : if (BITINT_TYPE_P (vr->type)
3297 1014 : && maxsizei > MAX_FIXED_MODE_SIZE)
3298 1 : type = build_bitint_type (maxsizei, uns);
3299 : else
3300 1012 : type = build_nonstandard_integer_type (maxsizei, uns);
3301 : }
3302 42087 : if (BYTES_BIG_ENDIAN)
3303 : {
3304 : /* For big-endian native_encode_expr stored the rhs
3305 : such that the LSB of it is the LSB of buffer[len - 1].
3306 : That bit is stored into memory at position
3307 : offset2 + size2 - 1, i.e. in byte
3308 : base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3309 : E.g. for offset2 1 and size2 14, rhs -1 and memory
3310 : previously cleared that is:
3311 : 0 1
3312 : 01111111|11111110
3313 : Now, if we want to extract offset 2 and size 12 from
3314 : it using native_interpret_expr (which actually works
3315 : for integral bitfield types in terms of byte size of
3316 : the mode), the native_encode_expr stored the value
3317 : into buffer as
3318 : XX111111|11111111
3319 : and returned len 2 (the X bits are outside of
3320 : precision).
3321 : Let sz be maxsize / BITS_PER_UNIT if not extracting
3322 : a bitfield, and GET_MODE_SIZE otherwise.
3323 : We need to align the LSB of the value we want to
3324 : extract as the LSB of buf[sz - 1].
3325 : The LSB from memory we need to read is at position
3326 : offset + maxsize - 1. */
3327 : HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3328 : if (INTEGRAL_TYPE_P (type))
3329 : {
3330 : if (TYPE_MODE (type) != BLKmode)
3331 : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3332 : else
3333 : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3334 : }
3335 : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3336 : - offseti - maxsizei) % BITS_PER_UNIT;
3337 : if (amnt)
3338 : shift_bytes_in_array_right (buffer, len, amnt);
3339 : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3340 : - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3341 : if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3342 : len = 0;
3343 : else
3344 : {
3345 : buf = buffer + len - sz - amnt;
3346 : len -= (buf - buffer);
3347 : }
3348 : }
3349 : else
3350 : {
3351 42087 : amnt = ((unsigned HOST_WIDE_INT) offset2i
3352 42087 : - offseti) % BITS_PER_UNIT;
3353 42087 : if (amnt)
3354 : {
3355 344 : buffer[len] = 0;
3356 344 : shift_bytes_in_array_left (buffer, len + 1, amnt);
3357 344 : buf = buffer + 1;
3358 : }
3359 : }
3360 42087 : tree val = native_interpret_expr (type, buf, len);
3361 : /* If we chop off bits because the types precision doesn't
3362 : match the memory access size this is ok when optimizing
3363 : reads but not when called from the DSE code during
3364 : elimination. */
3365 42087 : if (val
3366 42085 : && type != vr->type)
3367 : {
3368 1013 : if (! int_fits_type_p (val, vr->type))
3369 : val = NULL_TREE;
3370 : else
3371 1013 : val = fold_convert (vr->type, val);
3372 : }
3373 :
3374 42085 : if (val)
3375 42085 : return data->finish (ao_ref_alias_set (&lhs_ref),
3376 42085 : ao_ref_base_alias_set (&lhs_ref), val);
3377 : }
3378 : }
3379 41941 : else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3380 : size2i))
3381 : {
3382 41941 : pd_data pd;
3383 41941 : tree rhs = gimple_assign_rhs1 (def_stmt);
3384 41941 : if (TREE_CODE (rhs) == SSA_NAME)
3385 2270 : rhs = SSA_VAL (rhs);
3386 41941 : pd.rhs = rhs;
3387 41941 : pd.rhs_off = 0;
3388 41941 : pd.offset = offset2i;
3389 41941 : pd.size = size2i;
3390 41941 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3391 : ao_ref_base_alias_set (&lhs_ref),
3392 : offseti, maxsizei);
3393 : }
3394 : }
3395 : }
3396 :
3397 : /* 4) Assignment from an SSA name which definition we may be able
3398 : to access pieces from or we can combine to a larger entity. */
3399 21015188 : else if (known_eq (ref->size, maxsize)
3400 20489499 : && is_gimple_reg_type (vr->type)
3401 20483686 : && !reverse_storage_order_for_component_p (vr->operands)
3402 20480883 : && !contains_storage_order_barrier_p (vr->operands)
3403 20480883 : && gimple_assign_single_p (def_stmt)
3404 4681956 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3405 25694183 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3406 : {
3407 1889280 : tree lhs = gimple_assign_lhs (def_stmt);
3408 1889280 : tree base2;
3409 1889280 : poly_int64 offset2, size2, maxsize2;
3410 1889280 : HOST_WIDE_INT offset2i, size2i, offseti;
3411 1889280 : bool reverse;
3412 1889280 : gcc_assert (lhs_ref_ok);
3413 1889280 : base2 = ao_ref_base (&lhs_ref);
3414 1889280 : offset2 = lhs_ref.offset;
3415 1889280 : size2 = lhs_ref.size;
3416 1889280 : maxsize2 = lhs_ref.max_size;
3417 1889280 : reverse = reverse_storage_order_for_component_p (lhs);
3418 1889280 : tree def_rhs = gimple_assign_rhs1 (def_stmt);
3419 1889280 : if (!reverse
3420 1889068 : && !storage_order_barrier_p (lhs)
3421 1889068 : && known_size_p (maxsize2)
3422 1863060 : && known_eq (maxsize2, size2)
3423 3633544 : && adjust_offsets_for_equal_base_address (base, &offset,
3424 : base2, &offset2))
3425 : {
3426 86034 : if (data->partial_defs.is_empty ()
3427 79517 : && known_subrange_p (offset, maxsize, offset2, size2)
3428 : /* ??? We can't handle bitfield precision extracts without
3429 : either using an alternate type for the BIT_FIELD_REF and
3430 : then doing a conversion or possibly adjusting the offset
3431 : according to endianness. */
3432 55330 : && (! INTEGRAL_TYPE_P (vr->type)
3433 41118 : || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3434 96939 : && multiple_p (ref->size, BITS_PER_UNIT))
3435 : {
3436 46759 : tree val = NULL_TREE;
3437 93512 : if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3438 51453 : || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3439 : {
3440 91336 : gimple_match_op op (gimple_match_cond::UNCOND,
3441 45668 : BIT_FIELD_REF, vr->type,
3442 : SSA_VAL (def_rhs),
3443 : bitsize_int (ref->size),
3444 45668 : bitsize_int (offset - offset2));
3445 45668 : val = vn_nary_build_or_lookup (&op);
3446 : }
3447 1091 : else if (known_eq (ref->size, size2))
3448 : {
3449 1017 : gimple_match_op op (gimple_match_cond::UNCOND,
3450 1017 : VIEW_CONVERT_EXPR, vr->type,
3451 1017 : SSA_VAL (def_rhs));
3452 1017 : val = vn_nary_build_or_lookup (&op);
3453 : }
3454 46685 : if (val
3455 46685 : && (TREE_CODE (val) != SSA_NAME
3456 45882 : || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3457 46666 : return data->finish (ao_ref_alias_set (&lhs_ref),
3458 85941 : ao_ref_base_alias_set (&lhs_ref), val);
3459 : }
3460 39275 : else if (maxsize.is_constant (&maxsizei)
3461 39275 : && offset.is_constant (&offseti)
3462 39275 : && offset2.is_constant (&offset2i)
3463 39275 : && size2.is_constant (&size2i)
3464 39275 : && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3465 : {
3466 39275 : pd_data pd;
3467 39275 : pd.rhs = SSA_VAL (def_rhs);
3468 39275 : pd.rhs_off = 0;
3469 39275 : pd.offset = offset2i;
3470 39275 : pd.size = size2i;
3471 39275 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3472 : ao_ref_base_alias_set (&lhs_ref),
3473 : offseti, maxsizei);
3474 : }
3475 : }
3476 : }
3477 :
3478 : /* 4b) Assignment done via one of the vectorizer internal store
3479 : functions where we may be able to access pieces from or we can
3480 : combine to a larger entity. */
3481 19125908 : else if (known_eq (ref->size, maxsize)
3482 18600219 : && is_gimple_reg_type (vr->type)
3483 18594406 : && !reverse_storage_order_for_component_p (vr->operands)
3484 18591603 : && !contains_storage_order_barrier_p (vr->operands)
3485 18591603 : && is_gimple_call (def_stmt)
3486 14974935 : && gimple_call_internal_p (def_stmt)
3487 19439246 : && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3488 : {
3489 36 : gcall *call = as_a <gcall *> (def_stmt);
3490 36 : internal_fn fn = gimple_call_internal_fn (call);
3491 :
3492 36 : tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3493 36 : switch (fn)
3494 : {
3495 36 : case IFN_MASK_STORE:
3496 36 : mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3497 36 : mask = vn_valueize (mask);
3498 36 : if (TREE_CODE (mask) != VECTOR_CST)
3499 28 : return (void *)-1;
3500 : break;
3501 0 : case IFN_LEN_STORE:
3502 0 : {
3503 0 : int len_index = internal_fn_len_index (fn);
3504 0 : len = gimple_call_arg (call, len_index);
3505 0 : bias = gimple_call_arg (call, len_index + 1);
3506 0 : if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3507 : return (void *) -1;
3508 : break;
3509 : }
3510 : default:
3511 : return (void *)-1;
3512 : }
3513 14 : tree def_rhs = gimple_call_arg (call,
3514 14 : internal_fn_stored_value_index (fn));
3515 14 : def_rhs = vn_valueize (def_rhs);
3516 14 : if (TREE_CODE (def_rhs) != VECTOR_CST)
3517 : return (void *)-1;
3518 :
3519 14 : ao_ref_init_from_ptr_and_size (&lhs_ref,
3520 : vn_valueize (gimple_call_arg (call, 0)),
3521 14 : TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3522 14 : tree base2;
3523 14 : poly_int64 offset2, size2, maxsize2;
3524 14 : HOST_WIDE_INT offset2i, size2i, offseti;
3525 14 : base2 = ao_ref_base (&lhs_ref);
3526 14 : offset2 = lhs_ref.offset;
3527 14 : size2 = lhs_ref.size;
3528 14 : maxsize2 = lhs_ref.max_size;
3529 14 : if (known_size_p (maxsize2)
3530 14 : && known_eq (maxsize2, size2)
3531 14 : && adjust_offsets_for_equal_base_address (base, &offset,
3532 : base2, &offset2)
3533 6 : && maxsize.is_constant (&maxsizei)
3534 6 : && offset.is_constant (&offseti)
3535 6 : && offset2.is_constant (&offset2i)
3536 14 : && size2.is_constant (&size2i))
3537 : {
3538 6 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3539 : /* Poor-mans disambiguation. */
3540 : return NULL;
3541 6 : else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3542 : {
3543 6 : pd_data pd;
3544 6 : pd.rhs = def_rhs;
3545 6 : tree aa = gimple_call_arg (call, 1);
3546 6 : alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3547 6 : tree vectype = TREE_TYPE (def_rhs);
3548 6 : unsigned HOST_WIDE_INT elsz
3549 6 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3550 6 : if (mask)
3551 : {
3552 : HOST_WIDE_INT start = 0, length = 0;
3553 : unsigned mask_idx = 0;
3554 48 : do
3555 : {
3556 48 : if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3557 : {
3558 24 : if (length != 0)
3559 : {
3560 18 : pd.rhs_off = start;
3561 18 : pd.offset = offset2i + start;
3562 18 : pd.size = length;
3563 18 : if (ranges_known_overlap_p
3564 18 : (offset, maxsize, pd.offset, pd.size))
3565 : {
3566 0 : void *res = data->push_partial_def
3567 0 : (pd, set, set, offseti, maxsizei);
3568 0 : if (res != NULL)
3569 6 : return res;
3570 : }
3571 : }
3572 24 : start = (mask_idx + 1) * elsz;
3573 24 : length = 0;
3574 : }
3575 : else
3576 24 : length += elsz;
3577 48 : mask_idx++;
3578 : }
3579 48 : while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3580 6 : if (length != 0)
3581 : {
3582 6 : pd.rhs_off = start;
3583 6 : pd.offset = offset2i + start;
3584 6 : pd.size = length;
3585 6 : if (ranges_known_overlap_p (offset, maxsize,
3586 : pd.offset, pd.size))
3587 2 : return data->push_partial_def (pd, set, set,
3588 2 : offseti, maxsizei);
3589 : }
3590 : }
3591 0 : else if (fn == IFN_LEN_STORE)
3592 : {
3593 0 : pd.offset = offset2i;
3594 0 : pd.size = (tree_to_uhwi (len)
3595 0 : + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3596 0 : if (BYTES_BIG_ENDIAN)
3597 : pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3598 : else
3599 0 : pd.rhs_off = 0;
3600 0 : if (ranges_known_overlap_p (offset, maxsize,
3601 : pd.offset, pd.size))
3602 0 : return data->push_partial_def (pd, set, set,
3603 0 : offseti, maxsizei);
3604 : }
3605 : else
3606 0 : gcc_unreachable ();
3607 : return NULL;
3608 : }
3609 : }
3610 : }
3611 :
3612 : /* 5) For aggregate copies translate the reference through them if
3613 : the copy kills ref. */
3614 19125872 : else if (data->vn_walk_kind == VN_WALKREWRITE
3615 15446160 : && gimple_assign_single_p (def_stmt)
3616 2580135 : && !gimple_has_volatile_ops (def_stmt)
3617 21703709 : && (DECL_P (gimple_assign_rhs1 (def_stmt))
3618 1987358 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3619 1573426 : || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3620 : {
3621 2368392 : tree base2;
3622 2368392 : int i, j, k;
3623 2368392 : auto_vec<vn_reference_op_s> rhs;
3624 2368392 : vn_reference_op_t vro;
3625 2368392 : ao_ref r;
3626 :
3627 2368392 : gcc_assert (lhs_ref_ok);
3628 :
3629 : /* See if the assignment kills REF. */
3630 2368392 : base2 = ao_ref_base (&lhs_ref);
3631 2368392 : if (!lhs_ref.max_size_known_p ()
3632 2367758 : || (base != base2
3633 90062 : && (TREE_CODE (base) != MEM_REF
3634 74257 : || TREE_CODE (base2) != MEM_REF
3635 56941 : || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3636 19477 : || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3637 19477 : TREE_OPERAND (base2, 1))))
3638 4663899 : || !stmt_kills_ref_p (def_stmt, ref))
3639 : return (void *)-1;
3640 :
3641 : /* Find the common base of ref and the lhs. lhs_ops already
3642 : contains valueized operands for the lhs. */
3643 1970387 : poly_int64 extra_off = 0;
3644 1970387 : i = vr->operands.length () - 1;
3645 1970387 : j = lhs_ops.length () - 1;
3646 :
3647 : /* The base should be always equal due to the above check. */
3648 1970387 : if (! vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3649 : return (void *)-1;
3650 1970125 : i--, j--;
3651 :
3652 : /* The 2nd component should always exist and be a MEM_REF. */
3653 1970125 : if (!(i >= 0 && j >= 0))
3654 : ;
3655 1970125 : else if (vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3656 933719 : i--, j--;
3657 1036406 : else if (vr->operands[i].opcode == MEM_REF
3658 1034988 : && lhs_ops[j].opcode == MEM_REF
3659 1034988 : && known_ne (lhs_ops[j].off, -1)
3660 2071394 : && known_ne (vr->operands[i].off, -1))
3661 : {
3662 1034988 : bool found = false;
3663 : /* When we ge a mismatch at a MEM_REF that is not the sole component
3664 : try finding a match in one of the outer components and continue
3665 : stripping there. This happens when addresses of components get
3666 : forwarded into dereferences. */
3667 1034988 : if (i > 0)
3668 : {
3669 114998 : int temi = i - 1;
3670 114998 : poly_int64 tem_extra_off = extra_off + vr->operands[i].off;
3671 114998 : while (temi >= 0
3672 250318 : && known_ne (vr->operands[temi].off, -1))
3673 : {
3674 136813 : if (vr->operands[temi].type
3675 136813 : && lhs_ops[j].type
3676 273626 : && (TYPE_MAIN_VARIANT (vr->operands[temi].type)
3677 136813 : == TYPE_MAIN_VARIANT (lhs_ops[j].type)))
3678 : {
3679 1493 : i = temi;
3680 : /* Strip the component that was type matched to
3681 : the MEM_REF. */
3682 1493 : extra_off = (tem_extra_off
3683 1493 : + vr->operands[i].off - lhs_ops[j].off);
3684 1493 : i--, j--;
3685 : /* Strip further equal components. */
3686 1493 : found = true;
3687 1493 : break;
3688 : }
3689 135320 : tem_extra_off += vr->operands[temi].off;
3690 135320 : temi--;
3691 : }
3692 : }
3693 1034988 : if (!found && j > 0)
3694 : {
3695 33405 : int temj = j - 1;
3696 33405 : poly_int64 tem_extra_off = extra_off - lhs_ops[j].off;
3697 33405 : while (temj >= 0
3698 63870 : && known_ne (lhs_ops[temj].off, -1))
3699 : {
3700 35658 : if (vr->operands[i].type
3701 35658 : && lhs_ops[temj].type
3702 71316 : && (TYPE_MAIN_VARIANT (vr->operands[i].type)
3703 35658 : == TYPE_MAIN_VARIANT (lhs_ops[temj].type)))
3704 : {
3705 5193 : j = temj;
3706 : /* Strip the component that was type matched to
3707 : the MEM_REF. */
3708 5193 : extra_off = (tem_extra_off
3709 5193 : + vr->operands[i].off - lhs_ops[j].off);
3710 5193 : i--, j--;
3711 : /* Strip further equal components. */
3712 5193 : found = true;
3713 5193 : break;
3714 : }
3715 30465 : tem_extra_off += -lhs_ops[temj].off;
3716 30465 : temj--;
3717 : }
3718 : }
3719 : /* When we cannot find a common base to reconstruct the full
3720 : reference instead try to reduce the lookup to the new
3721 : base plus a constant offset. */
3722 1034988 : if (!found)
3723 : {
3724 : while (j >= 0
3725 2086683 : && known_ne (lhs_ops[j].off, -1))
3726 : {
3727 1058381 : extra_off += -lhs_ops[j].off;
3728 1058381 : j--;
3729 : }
3730 1028302 : if (j != -1)
3731 : return (void *)-1;
3732 : while (i >= 0
3733 2184508 : && known_ne (vr->operands[i].off, -1))
3734 : {
3735 : /* Punt if the additional ops contain a storage order
3736 : barrier. */
3737 1156206 : if (vr->operands[i].opcode == VIEW_CONVERT_EXPR
3738 1156206 : && vr->operands[i].reverse)
3739 : break;
3740 1156206 : extra_off += vr->operands[i].off;
3741 1156206 : i--;
3742 : }
3743 1028302 : if (i != -1)
3744 : return (void *)-1;
3745 : found = true;
3746 : }
3747 : /* If we did find a match we'd eventually append a MEM_REF
3748 : as component. Don't. */
3749 : if (!found)
3750 : return (void *)-1;
3751 : }
3752 : else
3753 : return (void *)-1;
3754 :
3755 : /* Strip further common components, attempting to consume lhs_ops
3756 : in full. */
3757 1967822 : while (j >= 0 && i >= 0
3758 1967822 : && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3759 : {
3760 25664 : i--;
3761 25664 : j--;
3762 : }
3763 :
3764 : /* i now points to the first additional op.
3765 : ??? LHS may not be completely contained in VR, one or more
3766 : VIEW_CONVERT_EXPRs could be in its way. We could at least
3767 : try handling outermost VIEW_CONVERT_EXPRs. */
3768 1942158 : if (j != -1)
3769 : return (void *)-1;
3770 :
3771 : /* Punt if the additional ops contain a storage order barrier. */
3772 3036529 : for (k = i; k >= 0; k--)
3773 : {
3774 1097326 : vro = &vr->operands[k];
3775 1097326 : if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3776 : return (void *)-1;
3777 : }
3778 :
3779 : /* Now re-write REF to be based on the rhs of the assignment. */
3780 1939203 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
3781 1939203 : copy_reference_ops_from_ref (rhs1, &rhs);
3782 :
3783 : /* When none of the original operands survives the storage order of
3784 : the translated reference is the one of the RHS of the copy. The
3785 : operands we folded into a constant offset above may well have
3786 : specified a reverse storage order, which is a property of the
3787 : component and not of its position, so it is not recoverable from
3788 : that offset. Punt unless both accesses are in natural order. */
3789 1939203 : if (i < 0
3790 1939203 : && (reverse_storage_order_for_component_p (vr->operands)
3791 1024120 : || reverse_storage_order_for_component_p (rhs)))
3792 : return (void *)-1;
3793 :
3794 : /* Apply an extra offset to the inner MEM_REF of the RHS. */
3795 1939112 : bool force_no_tbaa = false;
3796 1939112 : if (maybe_ne (extra_off, 0))
3797 : {
3798 740439 : if (rhs.length () < 2)
3799 : return (void *)-1;
3800 740439 : int ix = rhs.length () - 2;
3801 740439 : if (rhs[ix].opcode != MEM_REF
3802 740439 : || known_eq (rhs[ix].off, -1))
3803 : return (void *)-1;
3804 740421 : rhs[ix].off += extra_off;
3805 740421 : rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3806 740421 : build_int_cst (TREE_TYPE (rhs[ix].op0),
3807 : extra_off));
3808 : /* When we have offsetted the RHS, reading only parts of it,
3809 : we can no longer use the original TBAA type, force alias-set
3810 : zero. */
3811 740421 : force_no_tbaa = true;
3812 : }
3813 :
3814 : /* Save the operands since we need to use the original ones for
3815 : the hash entry we use. */
3816 1939094 : if (!data->saved_operands.exists ())
3817 1826019 : data->saved_operands = vr->operands.copy ();
3818 :
3819 : /* We need to pre-pend vr->operands[0..i] to rhs. */
3820 1939094 : vec<vn_reference_op_s> old = vr->operands;
3821 5817282 : if (i + 1 + rhs.length () > vr->operands.length ())
3822 1146978 : vr->operands.safe_grow (i + 1 + rhs.length (), true);
3823 : else
3824 792116 : vr->operands.truncate (i + 1 + rhs.length ());
3825 7014884 : FOR_EACH_VEC_ELT (rhs, j, vro)
3826 5075790 : vr->operands[i + 1 + j] = *vro;
3827 1939094 : valueize_refs (&vr->operands);
3828 3878188 : if (old == shared_lookup_references)
3829 1939094 : shared_lookup_references = vr->operands;
3830 1939094 : vr->hashcode = vn_reference_compute_hash (vr);
3831 :
3832 : /* Try folding the new reference to a constant. */
3833 1939094 : tree val = fully_constant_vn_reference_p (vr);
3834 1939094 : if (val)
3835 : {
3836 22266 : if (data->partial_defs.is_empty ())
3837 22257 : return data->finish (ao_ref_alias_set (&lhs_ref),
3838 22257 : ao_ref_base_alias_set (&lhs_ref), val);
3839 : /* This is the only interesting case for partial-def handling
3840 : coming from targets that like to gimplify init-ctors as
3841 : aggregate copies from constant data like aarch64 for
3842 : PR83518. */
3843 9 : if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3844 : {
3845 9 : pd_data pd;
3846 9 : pd.rhs = val;
3847 9 : pd.rhs_off = 0;
3848 9 : pd.offset = 0;
3849 9 : pd.size = maxsizei;
3850 9 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3851 : ao_ref_base_alias_set (&lhs_ref),
3852 : 0, maxsizei);
3853 : }
3854 : }
3855 :
3856 : /* Continuing with partial defs isn't easily possible here, we
3857 : have to find a full def from further lookups from here. Probably
3858 : not worth the special-casing everywhere. */
3859 4285220 : if (!data->partial_defs.is_empty ())
3860 : return (void *)-1;
3861 :
3862 : /* Adjust *ref from the new operands. */
3863 1910991 : ao_ref rhs1_ref;
3864 1910991 : ao_ref_init (&rhs1_ref, rhs1);
3865 3096248 : if (!ao_ref_init_from_vn_reference (&r,
3866 : force_no_tbaa ? 0
3867 1185257 : : ao_ref_alias_set (&rhs1_ref),
3868 : force_no_tbaa ? 0
3869 1185257 : : ao_ref_base_alias_set (&rhs1_ref),
3870 : vr->type, vr->operands))
3871 : return (void *)-1;
3872 : /* This can happen with bitfields. */
3873 1910991 : if (maybe_ne (ref->size, r.size))
3874 : {
3875 : /* If the access lacks some subsetting simply apply that by
3876 : shortening it. That in the end can only be successful
3877 : if we can pun the lookup result which in turn requires
3878 : exact offsets. */
3879 1571 : if (known_eq (r.size, r.max_size)
3880 1571 : && known_lt (ref->size, r.size))
3881 1571 : r.size = r.max_size = ref->size;
3882 : else
3883 : return (void *)-1;
3884 : }
3885 1910991 : *ref = r;
3886 1910991 : vr->offset = r.offset;
3887 1910991 : vr->max_size = r.max_size;
3888 :
3889 : /* Do not update last seen VUSE after translating. */
3890 1910991 : data->last_vuse_ptr = NULL;
3891 : /* Invalidate the original access path since it now contains
3892 : the wrong base. */
3893 1910991 : data->orig_ref.ref = NULL_TREE;
3894 : /* Use the alias-set of this LHS for recording an eventual result. */
3895 1910991 : if (data->first_set == -2)
3896 : {
3897 1799482 : data->first_set = ao_ref_alias_set (&lhs_ref);
3898 1799482 : data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3899 : }
3900 :
3901 : /* Keep looking for the adjusted *REF / VR pair. */
3902 : return NULL;
3903 2368392 : }
3904 :
3905 : /* 6) For memcpy copies translate the reference through them if the copy
3906 : kills ref. But we cannot (easily) do this translation if the memcpy is
3907 : a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3908 : can modify the storage order of objects (see storage_order_barrier_p). */
3909 16757480 : else if (data->vn_walk_kind == VN_WALKREWRITE
3910 13077768 : && is_gimple_reg_type (vr->type)
3911 : /* ??? Handle BCOPY as well. */
3912 13071955 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3913 13002024 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3914 13001601 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3915 13000425 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3916 13000183 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3917 12974245 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3918 98038 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3919 85856 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3920 98002 : && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3921 69472 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3922 97987 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), ©_size)
3923 55883 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3924 55883 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3925 : ©_size)))
3926 : /* Handling this is more complicated, give up for now. */
3927 16802203 : && data->partial_defs.is_empty ())
3928 : {
3929 44027 : tree lhs, rhs;
3930 44027 : ao_ref r;
3931 44027 : poly_int64 rhs_offset, lhs_offset;
3932 44027 : vn_reference_op_s op;
3933 44027 : poly_uint64 mem_offset;
3934 44027 : poly_int64 at, byte_maxsize;
3935 :
3936 : /* Only handle non-variable, addressable refs. */
3937 44027 : if (maybe_ne (ref->size, maxsize)
3938 43505 : || !multiple_p (offset, BITS_PER_UNIT, &at)
3939 87596 : || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3940 : return (void *)-1;
3941 :
3942 : /* Extract a pointer base and an offset for the destination. */
3943 43505 : lhs = gimple_call_arg (def_stmt, 0);
3944 43505 : lhs_offset = 0;
3945 43505 : if (TREE_CODE (lhs) == SSA_NAME)
3946 : {
3947 33045 : lhs = vn_valueize (lhs);
3948 33045 : if (TREE_CODE (lhs) == SSA_NAME)
3949 : {
3950 32716 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3951 32716 : if (gimple_assign_single_p (def_stmt)
3952 32716 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3953 2490 : lhs = gimple_assign_rhs1 (def_stmt);
3954 : }
3955 : }
3956 43505 : if (TREE_CODE (lhs) == ADDR_EXPR)
3957 : {
3958 18678 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3959 18381 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3960 : return (void *)-1;
3961 13139 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3962 : &lhs_offset);
3963 13139 : if (!tem)
3964 : return (void *)-1;
3965 12427 : if (TREE_CODE (tem) == MEM_REF
3966 12427 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3967 : {
3968 1778 : lhs = TREE_OPERAND (tem, 0);
3969 1778 : if (TREE_CODE (lhs) == SSA_NAME)
3970 1778 : lhs = vn_valueize (lhs);
3971 1778 : lhs_offset += mem_offset;
3972 : }
3973 10649 : else if (DECL_P (tem))
3974 10649 : lhs = build_fold_addr_expr (tem);
3975 : else
3976 : return (void *)-1;
3977 : }
3978 42653 : if (TREE_CODE (lhs) != SSA_NAME
3979 10650 : && TREE_CODE (lhs) != ADDR_EXPR)
3980 : return (void *)-1;
3981 :
3982 : /* Extract a pointer base and an offset for the source. */
3983 42653 : rhs = gimple_call_arg (def_stmt, 1);
3984 42653 : rhs_offset = 0;
3985 42653 : if (TREE_CODE (rhs) == SSA_NAME)
3986 20061 : rhs = vn_valueize (rhs);
3987 42653 : if (TREE_CODE (rhs) == ADDR_EXPR)
3988 : {
3989 35496 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3990 24847 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3991 : return (void *)-1;
3992 24219 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3993 : &rhs_offset);
3994 24219 : if (!tem)
3995 : return (void *)-1;
3996 24219 : if (TREE_CODE (tem) == MEM_REF
3997 24219 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3998 : {
3999 0 : rhs = TREE_OPERAND (tem, 0);
4000 0 : rhs_offset += mem_offset;
4001 : }
4002 24219 : else if (DECL_P (tem)
4003 17744 : || TREE_CODE (tem) == STRING_CST)
4004 24219 : rhs = build_fold_addr_expr (tem);
4005 : else
4006 : return (void *)-1;
4007 : }
4008 42653 : if (TREE_CODE (rhs) == SSA_NAME)
4009 18434 : rhs = SSA_VAL (rhs);
4010 24219 : else if (TREE_CODE (rhs) != ADDR_EXPR)
4011 : return (void *)-1;
4012 :
4013 : /* The bases of the destination and the references have to agree. */
4014 42653 : if (TREE_CODE (base) == MEM_REF)
4015 : {
4016 15903 : if (TREE_OPERAND (base, 0) != lhs
4017 15903 : || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
4018 : return (void *) -1;
4019 13155 : at += mem_offset;
4020 : }
4021 26750 : else if (!DECL_P (base)
4022 25737 : || TREE_CODE (lhs) != ADDR_EXPR
4023 36215 : || TREE_OPERAND (lhs, 0) != base)
4024 : return (void *)-1;
4025 :
4026 : /* If the access is completely outside of the memcpy destination
4027 : area there is no aliasing. */
4028 13155 : if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
4029 : return NULL;
4030 : /* And the access has to be contained within the memcpy destination. */
4031 13122 : if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
4032 : return (void *)-1;
4033 :
4034 : /* Save the operands since we need to use the original ones for
4035 : the hash entry we use. */
4036 12503 : if (!data->saved_operands.exists ())
4037 12062 : data->saved_operands = vr->operands.copy ();
4038 :
4039 : /* Make room for 2 operands in the new reference. */
4040 12503 : if (vr->operands.length () < 2)
4041 : {
4042 0 : vec<vn_reference_op_s> old = vr->operands;
4043 0 : vr->operands.safe_grow_cleared (2, true);
4044 0 : if (old == shared_lookup_references)
4045 0 : shared_lookup_references = vr->operands;
4046 : }
4047 : else
4048 12503 : vr->operands.truncate (2);
4049 :
4050 : /* The looked-through reference is a simple MEM_REF. */
4051 12503 : memset (&op, 0, sizeof (op));
4052 12503 : op.type = vr->type;
4053 12503 : op.opcode = MEM_REF;
4054 12503 : op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
4055 12503 : op.off = at - lhs_offset + rhs_offset;
4056 12503 : vr->operands[0] = op;
4057 12503 : op.type = TREE_TYPE (rhs);
4058 12503 : op.opcode = TREE_CODE (rhs);
4059 12503 : op.op0 = rhs;
4060 12503 : op.off = -1;
4061 12503 : vr->operands[1] = op;
4062 12503 : vr->hashcode = vn_reference_compute_hash (vr);
4063 :
4064 : /* Try folding the new reference to a constant. */
4065 12503 : tree val = fully_constant_vn_reference_p (vr);
4066 12503 : if (val)
4067 3209 : return data->finish (0, 0, val);
4068 :
4069 : /* Adjust *ref from the new operands. */
4070 9294 : if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
4071 : return (void *)-1;
4072 : /* This can happen with bitfields. */
4073 9294 : if (maybe_ne (ref->size, r.size))
4074 : return (void *)-1;
4075 9294 : *ref = r;
4076 9294 : vr->offset = r.offset;
4077 9294 : vr->max_size = r.max_size;
4078 :
4079 : /* Do not update last seen VUSE after translating. */
4080 9294 : data->last_vuse_ptr = NULL;
4081 : /* Invalidate the original access path since it now contains
4082 : the wrong base. */
4083 9294 : data->orig_ref.ref = NULL_TREE;
4084 : /* Use the alias-set of this stmt for recording an eventual result. */
4085 9294 : if (data->first_set == -2)
4086 : {
4087 8904 : data->first_set = 0;
4088 8904 : data->first_base_set = 0;
4089 : }
4090 :
4091 : /* Keep looking for the adjusted *REF / VR pair. */
4092 : return NULL;
4093 : }
4094 :
4095 : /* Bail out and stop walking. */
4096 : return (void *)-1;
4097 : }
4098 :
4099 : /* Return true if E is a backedge with respect to our CFG walk order. */
4100 :
4101 : static bool
4102 124331741 : vn_is_backedge (edge e, void *)
4103 : {
4104 : /* During PRE elimination we no longer have access to this info. */
4105 124331741 : return (!vn_bb_to_rpo
4106 124331741 : || vn_bb_to_rpo[e->dest->index] <= vn_bb_to_rpo[e->src->index]);
4107 : }
4108 :
4109 : /* Return a reference op vector from OP that can be used for
4110 : vn_reference_lookup_pieces. The caller is responsible for releasing
4111 : the vector. */
4112 :
4113 : vec<vn_reference_op_s>
4114 4852821 : vn_reference_operands_for_lookup (tree op)
4115 : {
4116 4852821 : bool valueized;
4117 4852821 : return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
4118 : }
4119 :
4120 : /* Lookup a reference operation by it's parts, in the current hash table.
4121 : Returns the resulting value number if it exists in the hash table,
4122 : NULL_TREE otherwise. VNRESULT will be filled in with the actual
4123 : vn_reference_t stored in the hashtable if something is found. */
4124 :
4125 : tree
4126 7956578 : vn_reference_lookup_pieces (tree vuse, alias_set_type set,
4127 : alias_set_type base_set, tree type,
4128 : vec<vn_reference_op_s> operands,
4129 : vn_reference_t *vnresult, vn_lookup_kind kind)
4130 : {
4131 7956578 : struct vn_reference_s vr1;
4132 7956578 : vn_reference_t tmp;
4133 7956578 : tree cst;
4134 :
4135 7956578 : if (!vnresult)
4136 0 : vnresult = &tmp;
4137 7956578 : *vnresult = NULL;
4138 :
4139 7956578 : vr1.vuse = vuse_ssa_val (vuse);
4140 7956578 : shared_lookup_references.truncate (0);
4141 15913156 : shared_lookup_references.safe_grow (operands.length (), true);
4142 7956578 : memcpy (shared_lookup_references.address (),
4143 7956578 : operands.address (),
4144 : sizeof (vn_reference_op_s)
4145 7956578 : * operands.length ());
4146 7956578 : bool valueized_p;
4147 7956578 : valueize_refs_1 (&shared_lookup_references, &valueized_p);
4148 7956578 : vr1.operands = shared_lookup_references;
4149 7956578 : vr1.type = type;
4150 7956578 : vr1.set = set;
4151 7956578 : vr1.base_set = base_set;
4152 : /* We can pretend there's no extra info fed in since the ao_refs offset
4153 : and max_size are computed only from the VN reference ops. */
4154 7956578 : vr1.offset = 0;
4155 7956578 : vr1.max_size = -1;
4156 7956578 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4157 7956578 : if ((cst = fully_constant_vn_reference_p (&vr1)))
4158 : return cst;
4159 :
4160 7936662 : vn_reference_lookup_1 (&vr1, vnresult);
4161 7936662 : if (!*vnresult
4162 3068641 : && kind != VN_NOWALK
4163 3068641 : && vr1.vuse)
4164 : {
4165 3038695 : ao_ref r;
4166 3038695 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4167 3038695 : vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
4168 3038695 : false);
4169 3038695 : vec<vn_reference_op_s> ops_for_ref;
4170 3038695 : if (!valueized_p)
4171 2943797 : ops_for_ref = vr1.operands;
4172 : else
4173 : {
4174 : /* For ao_ref_from_mem we have to ensure only available SSA names
4175 : end up in base and the only convenient way to make this work
4176 : for PRE is to re-valueize with that in mind. */
4177 189796 : ops_for_ref.create (operands.length ());
4178 189796 : ops_for_ref.quick_grow (operands.length ());
4179 94898 : memcpy (ops_for_ref.address (),
4180 94898 : operands.address (),
4181 : sizeof (vn_reference_op_s)
4182 94898 : * operands.length ());
4183 94898 : valueize_refs_1 (&ops_for_ref, &valueized_p, true);
4184 : }
4185 3038695 : if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
4186 : ops_for_ref))
4187 2967809 : *vnresult
4188 2967809 : = ((vn_reference_t)
4189 2967809 : walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
4190 : vn_reference_lookup_3, vn_is_backedge,
4191 : vuse_valueize, limit, &data));
4192 6077390 : if (ops_for_ref != shared_lookup_references)
4193 94898 : ops_for_ref.release ();
4194 6077390 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4195 3038695 : if (*vnresult
4196 438716 : && data.same_val
4197 3038695 : && (!(*vnresult)->result
4198 0 : || !operand_equal_p ((*vnresult)->result, data.same_val)))
4199 : {
4200 0 : *vnresult = NULL;
4201 0 : return NULL_TREE;
4202 : }
4203 3038695 : }
4204 :
4205 7936662 : if (*vnresult)
4206 5306737 : return (*vnresult)->result;
4207 :
4208 : return NULL_TREE;
4209 : }
4210 :
4211 : /* When OPERANDS is an ADDR_EXPR that can be possibly expressed as a
4212 : POINTER_PLUS_EXPR return true and fill in its operands in OPS. */
4213 :
4214 : bool
4215 2274365 : vn_pp_nary_for_addr (const vec<vn_reference_op_s>& operands, tree ops[2])
4216 : {
4217 4548730 : gcc_assert (operands[0].opcode == ADDR_EXPR
4218 : && operands.last ().opcode == SSA_NAME);
4219 : poly_int64 off = 0;
4220 : vn_reference_op_t vro;
4221 : unsigned i;
4222 7379829 : for (i = 1; operands.iterate (i, &vro); ++i)
4223 : {
4224 7379829 : if (vro->opcode == SSA_NAME)
4225 : break;
4226 5156470 : else if (known_eq (vro->off, -1))
4227 : break;
4228 5105464 : off += vro->off;
4229 : }
4230 2274365 : if (i == operands.length () - 1
4231 2223359 : && maybe_ne (off, 0)
4232 : /* Make sure we the offset we accumulated in a 64bit int
4233 : fits the address computation carried out in target
4234 : offset precision. */
4235 3749698 : && (off.coeffs[0]
4236 1475333 : == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4237 : {
4238 1474793 : gcc_assert (operands[i-1].opcode == MEM_REF);
4239 1474793 : ops[0] = operands[i].op0;
4240 1474793 : ops[1] = wide_int_to_tree (sizetype, off);
4241 1474793 : return true;
4242 : }
4243 : return false;
4244 : }
4245 :
4246 : /* Lookup OP in the current hash table, and return the resulting value
4247 : number if it exists in the hash table. Return NULL_TREE if it does
4248 : not exist in the hash table or if the result field of the structure
4249 : was NULL.. VNRESULT will be filled in with the vn_reference_t
4250 : stored in the hashtable if one exists. When TBAA_P is false assume
4251 : we are looking up a store and treat it as having alias-set zero.
4252 : *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
4253 : MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
4254 : load is bitwise anded with MASK and so we are only interested in a subset
4255 : of the bits and can ignore if the other bits are uninitialized or
4256 : not initialized with constants. When doing redundant store removal
4257 : the caller has to set REDUNDANT_STORE_REMOVAL_P. */
4258 :
4259 : tree
4260 103121794 : vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
4261 : vn_reference_t *vnresult, bool tbaa_p,
4262 : tree *last_vuse_ptr, tree mask,
4263 : bool redundant_store_removal_p)
4264 : {
4265 103121794 : vec<vn_reference_op_s> operands;
4266 103121794 : struct vn_reference_s vr1;
4267 103121794 : bool valueized_anything;
4268 :
4269 103121794 : if (vnresult)
4270 102714296 : *vnresult = NULL;
4271 :
4272 103121794 : vr1.vuse = vuse_ssa_val (vuse);
4273 206243588 : vr1.operands = operands
4274 103121794 : = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
4275 :
4276 : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
4277 : this before the pass folding __builtin_object_size had a chance to run. */
4278 103121794 : if ((cfun->curr_properties & PROP_objsz)
4279 74794478 : && operands[0].opcode == ADDR_EXPR
4280 104263897 : && operands.last ().opcode == SSA_NAME)
4281 : {
4282 1107511 : tree ops[2];
4283 1107511 : if (vn_pp_nary_for_addr (operands, ops))
4284 : {
4285 719112 : tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4286 719112 : TREE_TYPE (op), ops, NULL);
4287 719112 : if (res)
4288 719112 : return res;
4289 719112 : return NULL_TREE;
4290 : }
4291 : }
4292 :
4293 102402682 : vr1.type = TREE_TYPE (op);
4294 102402682 : ao_ref op_ref;
4295 102402682 : ao_ref_init (&op_ref, op);
4296 102402682 : vr1.set = ao_ref_alias_set (&op_ref);
4297 102402682 : vr1.base_set = ao_ref_base_alias_set (&op_ref);
4298 102402682 : vr1.offset = 0;
4299 102402682 : vr1.max_size = -1;
4300 102402682 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4301 102402682 : if (mask == NULL_TREE)
4302 102094023 : if (tree cst = fully_constant_vn_reference_p (&vr1))
4303 : return cst;
4304 :
4305 102386625 : if (kind != VN_NOWALK && vr1.vuse)
4306 : {
4307 59521380 : vn_reference_t wvnresult;
4308 59521380 : ao_ref r;
4309 59521380 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4310 59521380 : auto_vec<vn_reference_op_s> ops_for_ref;
4311 59521380 : if (valueized_anything)
4312 : {
4313 4729615 : copy_reference_ops_from_ref (op, &ops_for_ref);
4314 4729615 : bool tem;
4315 4729615 : valueize_refs_1 (&ops_for_ref, &tem, true);
4316 : }
4317 : /* Make sure to use a valueized reference if we valueized anything.
4318 : Otherwise preserve the full reference for advanced TBAA. */
4319 59521380 : if (!valueized_anything
4320 59521380 : || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4321 : vr1.type, ops_for_ref))
4322 : {
4323 54791765 : ao_ref_init (&r, op);
4324 : /* Record the extra info we're getting from the full ref. */
4325 54791765 : ao_ref_base (&r);
4326 54791765 : vr1.offset = r.offset;
4327 54791765 : vr1.max_size = r.max_size;
4328 : }
4329 59521380 : vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4330 : last_vuse_ptr, kind, tbaa_p, mask,
4331 114313145 : redundant_store_removal_p);
4332 :
4333 59521380 : wvnresult
4334 : = ((vn_reference_t)
4335 59521380 : walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4336 : vn_reference_lookup_3, vn_is_backedge,
4337 : vuse_valueize, limit, &data));
4338 119042760 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4339 59521380 : if (wvnresult)
4340 : {
4341 8834722 : gcc_assert (mask == NULL_TREE);
4342 8834722 : if (data.same_val
4343 8834722 : && (!wvnresult->result
4344 66159 : || !operand_equal_p (wvnresult->result, data.same_val)))
4345 : return NULL_TREE;
4346 8788645 : if (vnresult)
4347 8785974 : *vnresult = wvnresult;
4348 8788645 : return wvnresult->result;
4349 : }
4350 50686658 : else if (mask)
4351 308659 : return data.masked_result;
4352 :
4353 : return NULL_TREE;
4354 59521380 : }
4355 :
4356 42865245 : if (last_vuse_ptr)
4357 1481578 : *last_vuse_ptr = vr1.vuse;
4358 42865245 : if (mask)
4359 : return NULL_TREE;
4360 42865245 : return vn_reference_lookup_1 (&vr1, vnresult);
4361 : }
4362 :
4363 : /* Lookup CALL in the current hash table and return the entry in
4364 : *VNRESULT if found. Populates *VR for the hashtable lookup. */
4365 :
4366 : void
4367 9383261 : vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4368 : vn_reference_t vr)
4369 : {
4370 9383261 : if (vnresult)
4371 9383261 : *vnresult = NULL;
4372 :
4373 9383261 : tree vuse = gimple_vuse (call);
4374 :
4375 9383261 : vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4376 9383261 : vr->operands = valueize_shared_reference_ops_from_call (call);
4377 9383261 : tree lhs = gimple_call_lhs (call);
4378 : /* For non-SSA return values the reference ops contain the LHS. */
4379 5097560 : vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4380 14030082 : ? TREE_TYPE (lhs) : NULL_TREE);
4381 9383261 : vr->punned = false;
4382 9383261 : vr->set = 0;
4383 9383261 : vr->base_set = 0;
4384 9383261 : vr->offset = 0;
4385 9383261 : vr->max_size = -1;
4386 9383261 : vr->hashcode = vn_reference_compute_hash (vr);
4387 9383261 : vn_reference_lookup_1 (vr, vnresult);
4388 9383261 : }
4389 :
4390 : /* Insert OP into the current hash table with a value number of RESULT. */
4391 :
4392 : static void
4393 76454466 : vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4394 : {
4395 76454466 : vn_reference_s **slot;
4396 76454466 : vn_reference_t vr1;
4397 76454466 : bool tem;
4398 :
4399 76454466 : vec<vn_reference_op_s> operands
4400 76454466 : = valueize_shared_reference_ops_from_ref (op, &tem);
4401 : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4402 : before the pass folding __builtin_object_size had a chance to run. */
4403 76454466 : if ((cfun->curr_properties & PROP_objsz)
4404 57363788 : && operands[0].opcode == ADDR_EXPR
4405 77388118 : && operands.last ().opcode == SSA_NAME)
4406 : {
4407 901539 : tree ops[2];
4408 901539 : if (vn_pp_nary_for_addr (operands, ops))
4409 : {
4410 578213 : vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4411 578213 : TREE_TYPE (op), ops, result,
4412 578213 : VN_INFO (result)->value_id);
4413 578213 : return;
4414 : }
4415 : }
4416 :
4417 75876253 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4418 75876253 : if (TREE_CODE (result) == SSA_NAME)
4419 52448558 : vr1->value_id = VN_INFO (result)->value_id;
4420 : else
4421 23427695 : vr1->value_id = get_or_alloc_constant_value_id (result);
4422 75876253 : vr1->vuse = vuse_ssa_val (vuse);
4423 75876253 : vr1->operands = operands.copy ();
4424 75876253 : vr1->type = TREE_TYPE (op);
4425 75876253 : vr1->punned = false;
4426 75876253 : ao_ref op_ref;
4427 75876253 : ao_ref_init (&op_ref, op);
4428 75876253 : vr1->set = ao_ref_alias_set (&op_ref);
4429 75876253 : vr1->base_set = ao_ref_base_alias_set (&op_ref);
4430 : /* Specifically use an unknown extent here, we're not doing any lookup
4431 : and assume the caller didn't either (or it went VARYING). */
4432 75876253 : vr1->offset = 0;
4433 75876253 : vr1->max_size = -1;
4434 75876253 : vr1->hashcode = vn_reference_compute_hash (vr1);
4435 75876253 : vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4436 75876253 : vr1->result_vdef = vdef;
4437 :
4438 75876253 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4439 : INSERT);
4440 :
4441 : /* Because IL walking on reference lookup can end up visiting
4442 : a def that is only to be visited later in iteration order
4443 : when we are about to make an irreducible region reducible
4444 : the def can be effectively processed and its ref being inserted
4445 : by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4446 : but save a lookup if we deal with already inserted refs here. */
4447 75876253 : if (*slot)
4448 : {
4449 : /* We cannot assert that we have the same value either because
4450 : when disentangling an irreducible region we may end up visiting
4451 : a use before the corresponding def. That's a missed optimization
4452 : only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4453 0 : if (dump_file && (dump_flags & TDF_DETAILS)
4454 0 : && !operand_equal_p ((*slot)->result, vr1->result, 0))
4455 : {
4456 0 : fprintf (dump_file, "Keeping old value ");
4457 0 : print_generic_expr (dump_file, (*slot)->result);
4458 0 : fprintf (dump_file, " because of collision\n");
4459 : }
4460 0 : free_reference (vr1);
4461 0 : obstack_free (&vn_tables_obstack, vr1);
4462 : return;
4463 : }
4464 :
4465 75876253 : *slot = vr1;
4466 75876253 : vr1->next = last_inserted_ref;
4467 75876253 : last_inserted_ref = vr1;
4468 : }
4469 :
4470 : /* Insert a reference by it's pieces into the current hash table with
4471 : a value number of RESULT. Return the resulting reference
4472 : structure we created. */
4473 :
4474 : vn_reference_t
4475 1585433 : vn_reference_insert_pieces (tree vuse, alias_set_type set,
4476 : alias_set_type base_set,
4477 : poly_int64 offset, poly_int64 max_size, tree type,
4478 : vec<vn_reference_op_s> operands,
4479 : tree result, unsigned int value_id)
4480 :
4481 : {
4482 1585433 : vn_reference_s **slot;
4483 1585433 : vn_reference_t vr1;
4484 :
4485 1585433 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4486 1585433 : vr1->value_id = value_id;
4487 1585433 : vr1->vuse = vuse_ssa_val (vuse);
4488 1585433 : vr1->operands = operands;
4489 1585433 : valueize_refs (&vr1->operands);
4490 1585433 : vr1->type = type;
4491 1585433 : vr1->punned = false;
4492 1585433 : vr1->set = set;
4493 1585433 : vr1->base_set = base_set;
4494 1585433 : vr1->offset = offset;
4495 1585433 : vr1->max_size = max_size;
4496 1585433 : vr1->hashcode = vn_reference_compute_hash (vr1);
4497 1585433 : if (result && TREE_CODE (result) == SSA_NAME)
4498 367186 : result = SSA_VAL (result);
4499 1585433 : vr1->result = result;
4500 1585433 : vr1->result_vdef = NULL_TREE;
4501 :
4502 1585433 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4503 : INSERT);
4504 :
4505 : /* At this point we should have all the things inserted that we have
4506 : seen before, and we should never try inserting something that
4507 : already exists. */
4508 1585433 : gcc_assert (!*slot);
4509 :
4510 1585433 : *slot = vr1;
4511 1585433 : vr1->next = last_inserted_ref;
4512 1585433 : last_inserted_ref = vr1;
4513 1585433 : return vr1;
4514 : }
4515 :
4516 : /* Compute and return the hash value for nary operation VBO1. */
4517 :
4518 : hashval_t
4519 309751655 : vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4520 : {
4521 309751655 : inchash::hash hstate;
4522 309751655 : unsigned i;
4523 :
4524 309751655 : if (((vno1->length == 2
4525 260541043 : && commutative_tree_code (vno1->opcode))
4526 141844879 : || (vno1->length == 3
4527 1734467 : && commutative_ternary_tree_code (vno1->opcode)))
4528 477660694 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4529 2505238 : std::swap (vno1->op[0], vno1->op[1]);
4530 307246417 : else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4531 307246417 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4532 : {
4533 472972 : std::swap (vno1->op[0], vno1->op[1]);
4534 472972 : vno1->opcode = swap_tree_comparison (vno1->opcode);
4535 : }
4536 :
4537 309751655 : hstate.add_int (vno1->opcode);
4538 884313818 : for (i = 0; i < vno1->length; ++i)
4539 574562163 : inchash::add_expr (vno1->op[i], hstate);
4540 :
4541 309751655 : return hstate.end ();
4542 : }
4543 :
4544 : /* Compare nary operations VNO1 and VNO2 and return true if they are
4545 : equivalent. */
4546 :
4547 : bool
4548 974262682 : vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4549 : {
4550 974262682 : unsigned i;
4551 :
4552 974262682 : if (vno1->hashcode != vno2->hashcode)
4553 : return false;
4554 :
4555 51354486 : if (vno1->length != vno2->length)
4556 : return false;
4557 :
4558 51354486 : if (vno1->opcode != vno2->opcode
4559 51354486 : || !types_compatible_p (vno1->type, vno2->type))
4560 : return false;
4561 :
4562 145045272 : for (i = 0; i < vno1->length; ++i)
4563 94957518 : if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4564 : return false;
4565 :
4566 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
4567 : of op1. Need to check to make sure they are the same. */
4568 50087754 : if (vno1->opcode == BIT_INSERT_EXPR
4569 581 : && TREE_CODE (vno1->op[1]) == INTEGER_CST
4570 50087882 : && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4571 128 : != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4572 0 : return false;
4573 :
4574 : return true;
4575 : }
4576 :
4577 : /* Initialize VNO from the pieces provided. */
4578 :
4579 : static void
4580 191897647 : init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4581 : enum tree_code code, tree type, tree *ops)
4582 : {
4583 191897647 : vno->opcode = code;
4584 191897647 : vno->length = length;
4585 191897647 : vno->type = type;
4586 4835274 : memcpy (&vno->op[0], ops, sizeof (tree) * length);
4587 0 : }
4588 :
4589 : /* Return the number of operands for a vn_nary ops structure from STMT. */
4590 :
4591 : unsigned int
4592 111705681 : vn_nary_length_from_stmt (gimple *stmt)
4593 : {
4594 111705681 : switch (gimple_assign_rhs_code (stmt))
4595 : {
4596 : case REALPART_EXPR:
4597 : case IMAGPART_EXPR:
4598 : case VIEW_CONVERT_EXPR:
4599 : return 1;
4600 :
4601 711917 : case BIT_FIELD_REF:
4602 711917 : return 3;
4603 :
4604 553157 : case CONSTRUCTOR:
4605 553157 : return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4606 :
4607 106720806 : default:
4608 106720806 : return gimple_num_ops (stmt) - 1;
4609 : }
4610 : }
4611 :
4612 : /* Initialize VNO from STMT. */
4613 :
4614 : void
4615 111705681 : init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4616 : {
4617 111705681 : unsigned i;
4618 :
4619 111705681 : vno->opcode = gimple_assign_rhs_code (stmt);
4620 111705681 : vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4621 111705681 : switch (vno->opcode)
4622 : {
4623 3719801 : case REALPART_EXPR:
4624 3719801 : case IMAGPART_EXPR:
4625 3719801 : case VIEW_CONVERT_EXPR:
4626 3719801 : vno->length = 1;
4627 3719801 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4628 3719801 : break;
4629 :
4630 711917 : case BIT_FIELD_REF:
4631 711917 : vno->length = 3;
4632 711917 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4633 711917 : vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4634 711917 : vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4635 711917 : break;
4636 :
4637 553157 : case CONSTRUCTOR:
4638 553157 : vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4639 2189066 : for (i = 0; i < vno->length; ++i)
4640 1635909 : vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4641 : break;
4642 :
4643 106720806 : default:
4644 106720806 : gcc_checking_assert (!gimple_assign_single_p (stmt));
4645 106720806 : vno->length = gimple_num_ops (stmt) - 1;
4646 292435698 : for (i = 0; i < vno->length; ++i)
4647 185714892 : vno->op[i] = gimple_op (stmt, i + 1);
4648 : }
4649 111705681 : }
4650 :
4651 : /* Compute the hashcode for VNO and look for it in the hash table;
4652 : return the resulting value number if it exists in the hash table.
4653 : Return NULL_TREE if it does not exist in the hash table or if the
4654 : result field of the operation is NULL. VNRESULT will contain the
4655 : vn_nary_op_t from the hashtable if it exists. */
4656 :
4657 : static tree
4658 134595754 : vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4659 : {
4660 134595754 : vn_nary_op_s **slot;
4661 :
4662 134595754 : if (vnresult)
4663 126653948 : *vnresult = NULL;
4664 :
4665 374321469 : for (unsigned i = 0; i < vno->length; ++i)
4666 239725715 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4667 169528082 : vno->op[i] = SSA_VAL (vno->op[i]);
4668 :
4669 134595754 : vno->hashcode = vn_nary_op_compute_hash (vno);
4670 134595754 : slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4671 134595754 : if (!slot)
4672 : return NULL_TREE;
4673 18137349 : if (vnresult)
4674 17673232 : *vnresult = *slot;
4675 18137349 : return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4676 : }
4677 :
4678 : /* Lookup a n-ary operation by its pieces and return the resulting value
4679 : number if it exists in the hash table. Return NULL_TREE if it does
4680 : not exist in the hash table or if the result field of the operation
4681 : is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4682 : if it exists. */
4683 :
4684 : tree
4685 76770973 : vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4686 : tree type, tree *ops, vn_nary_op_t *vnresult)
4687 : {
4688 76770973 : vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4689 : sizeof_vn_nary_op (length));
4690 76770973 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4691 76770973 : return vn_nary_op_lookup_1 (vno1, vnresult);
4692 : }
4693 :
4694 : /* Lookup the rhs of STMT in the current hash table, and return the resulting
4695 : value number if it exists in the hash table. Return NULL_TREE if
4696 : it does not exist in the hash table. VNRESULT will contain the
4697 : vn_nary_op_t from the hashtable if it exists. */
4698 :
4699 : tree
4700 57824781 : vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4701 : {
4702 57824781 : vn_nary_op_t vno1
4703 57824781 : = XALLOCAVAR (struct vn_nary_op_s,
4704 : sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4705 57824781 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4706 57824781 : return vn_nary_op_lookup_1 (vno1, vnresult);
4707 : }
4708 :
4709 : /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4710 :
4711 : vn_nary_op_t
4712 174169781 : alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4713 : {
4714 174169781 : return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4715 : }
4716 :
4717 : /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4718 : obstack. */
4719 :
4720 : static vn_nary_op_t
4721 156443700 : alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4722 : {
4723 0 : vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4724 :
4725 156443700 : vno1->value_id = value_id;
4726 156443700 : vno1->length = length;
4727 156443700 : vno1->predicated_values = 0;
4728 156443700 : vno1->u.result = result;
4729 :
4730 156443700 : return vno1;
4731 : }
4732 :
4733 : /* Insert VNO into TABLE. */
4734 :
4735 : static vn_nary_op_t
4736 161427849 : vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4737 : {
4738 161427849 : vn_nary_op_s **slot;
4739 :
4740 161427849 : gcc_assert (! vno->predicated_values
4741 : || (! vno->u.values->next
4742 : && vno->u.values->n == 1));
4743 :
4744 472358134 : for (unsigned i = 0; i < vno->length; ++i)
4745 310930285 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4746 202507808 : vno->op[i] = SSA_VAL (vno->op[i]);
4747 :
4748 161427849 : vno->hashcode = vn_nary_op_compute_hash (vno);
4749 161427849 : slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4750 161427849 : vno->unwind_to = *slot;
4751 161427849 : if (*slot)
4752 : {
4753 : /* Prefer non-predicated values.
4754 : ??? Only if those are constant, otherwise, with constant predicated
4755 : value, turn them into predicated values with entry-block validity
4756 : (??? but we always find the first valid result currently). */
4757 30964285 : if ((*slot)->predicated_values
4758 30208952 : && ! vno->predicated_values)
4759 : {
4760 : /* ??? We cannot remove *slot from the unwind stack list.
4761 : For the moment we deal with this by skipping not found
4762 : entries but this isn't ideal ... */
4763 73705 : *slot = vno;
4764 : /* ??? Maintain a stack of states we can unwind in
4765 : vn_nary_op_s? But how far do we unwind? In reality
4766 : we need to push change records somewhere... Or not
4767 : unwind vn_nary_op_s and linking them but instead
4768 : unwind the results "list", linking that, which also
4769 : doesn't move on hashtable resize. */
4770 : /* We can also have a ->unwind_to recording *slot there.
4771 : That way we can make u.values a fixed size array with
4772 : recording the number of entries but of course we then
4773 : have always N copies for each unwind_to-state. Or we
4774 : make sure to only ever append and each unwinding will
4775 : pop off one entry (but how to deal with predicated
4776 : replaced with non-predicated here?) */
4777 73705 : vno->next = last_inserted_nary;
4778 73705 : last_inserted_nary = vno;
4779 73705 : return vno;
4780 : }
4781 30890580 : else if (vno->predicated_values
4782 30890228 : && ! (*slot)->predicated_values)
4783 : return *slot;
4784 30135599 : else if (vno->predicated_values
4785 30135247 : && (*slot)->predicated_values)
4786 : {
4787 : /* ??? Factor this all into a insert_single_predicated_value
4788 : routine. */
4789 30135247 : gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4790 30135247 : basic_block vno_bb
4791 30135247 : = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4792 30135247 : vn_pval *nval = vno->u.values;
4793 30135247 : vn_pval **next = &vno->u.values;
4794 30135247 : vn_pval *ins = NULL;
4795 30135247 : vn_pval *ins_at = NULL;
4796 : /* Find an existing value to append to. */
4797 56580824 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4798 : {
4799 31165567 : if (expressions_equal_p (val->result, nval->result))
4800 : {
4801 : /* Limit the number of places we register a predicate
4802 : as valid. */
4803 4719990 : if (val->n > 8)
4804 138632 : return *slot;
4805 11671436 : for (unsigned i = 0; i < val->n; ++i)
4806 : {
4807 7348240 : basic_block val_bb
4808 7348240 : = BASIC_BLOCK_FOR_FN (cfun,
4809 : val->valid_dominated_by_p[i]);
4810 7348240 : if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4811 : /* Value registered with more generic predicate. */
4812 258162 : return *slot;
4813 7090078 : else if (flag_checking)
4814 : /* Shouldn't happen, we insert in RPO order. */
4815 7090078 : gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4816 : val_bb, vno_bb));
4817 : }
4818 : /* Append the location. */
4819 4323196 : ins_at = val;
4820 4323196 : ins = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4821 : sizeof (vn_pval)
4822 : + val->n * sizeof (int));
4823 4323196 : ins->next = NULL;
4824 4323196 : ins->result = val->result;
4825 4323196 : ins->n = val->n + 1;
4826 4323196 : memcpy (ins->valid_dominated_by_p,
4827 4323196 : val->valid_dominated_by_p,
4828 4323196 : val->n * sizeof (int));
4829 4323196 : ins->valid_dominated_by_p[val->n] = vno_bb->index;
4830 4323196 : if (dump_file && (dump_flags & TDF_DETAILS))
4831 4 : fprintf (dump_file, "Appending predicate to value.\n");
4832 : break;
4833 : }
4834 : }
4835 : /* Copy the rest of the value chain. */
4836 61409614 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4837 : {
4838 31671161 : if (val == ins_at)
4839 : /* Replace the node we appended to. */
4840 4323196 : *next = ins;
4841 : else
4842 : {
4843 : /* Copy other predicated values. */
4844 27347965 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4845 : sizeof (vn_pval)
4846 : + ((val->n-1)
4847 : * sizeof (int)));
4848 27347965 : memcpy (*next, val,
4849 27347965 : sizeof (vn_pval) + (val->n-1) * sizeof (int));
4850 27347965 : (*next)->next = NULL;
4851 : }
4852 31671161 : next = &(*next)->next;
4853 : }
4854 : /* Append the value if we didn't find it. */
4855 29738453 : if (!ins_at)
4856 25415257 : *next = nval;
4857 29738453 : *slot = vno;
4858 29738453 : vno->next = last_inserted_nary;
4859 29738453 : last_inserted_nary = vno;
4860 29738453 : return vno;
4861 : }
4862 :
4863 : /* While we do not want to insert things twice it's awkward to
4864 : avoid it in the case where visit_nary_op pattern-matches stuff
4865 : and ends up simplifying the replacement to itself. We then
4866 : get two inserts, one from visit_nary_op and one from
4867 : vn_nary_build_or_lookup.
4868 : So allow inserts with the same value number. */
4869 352 : if ((*slot)->u.result == vno->u.result)
4870 : return *slot;
4871 : }
4872 :
4873 : /* ??? There's also optimistic vs. previous committed state merging
4874 : that is problematic for the case of unwinding. */
4875 :
4876 : /* ??? We should return NULL if we do not use 'vno' and have the
4877 : caller release it. */
4878 130463564 : gcc_assert (!*slot);
4879 :
4880 130463564 : *slot = vno;
4881 130463564 : vno->next = last_inserted_nary;
4882 130463564 : last_inserted_nary = vno;
4883 130463564 : return vno;
4884 : }
4885 :
4886 : /* Insert a n-ary operation into the current hash table using it's
4887 : pieces. Return the vn_nary_op_t structure we created and put in
4888 : the hashtable. */
4889 :
4890 : vn_nary_op_t
4891 578213 : vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4892 : tree type, tree *ops,
4893 : tree result, unsigned int value_id)
4894 : {
4895 578213 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4896 578213 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4897 578213 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4898 : }
4899 :
4900 : /* Return whether we can track a predicate valid when PRED_E is executed. */
4901 :
4902 : static bool
4903 154905686 : can_track_predicate_on_edge (edge pred_e)
4904 : {
4905 : /* ??? As we are currently recording the destination basic-block index in
4906 : vn_pval.valid_dominated_by_p and using dominance for the
4907 : validity check we cannot track predicates on all edges. */
4908 154905686 : if (single_pred_p (pred_e->dest))
4909 : return true;
4910 : /* Never record for backedges. */
4911 12332341 : if (pred_e->flags & EDGE_DFS_BACK)
4912 : return false;
4913 : /* When there's more than one predecessor we cannot track
4914 : predicate validity based on the destination block. The
4915 : exception is when all other incoming edges sources are
4916 : dominated by the destination block. */
4917 11636032 : edge_iterator ei;
4918 11636032 : edge e;
4919 19880580 : FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4920 17993203 : if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4921 : return false;
4922 : return true;
4923 : }
4924 :
4925 : static vn_nary_op_t
4926 109713187 : vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4927 : tree type, tree *ops,
4928 : tree result, unsigned int value_id,
4929 : edge pred_e)
4930 : {
4931 109713187 : if (flag_checking)
4932 109712367 : gcc_assert (can_track_predicate_on_edge (pred_e));
4933 :
4934 75928 : if (dump_file && (dump_flags & TDF_DETAILS)
4935 : /* ??? Fix dumping, but currently we only get comparisons. */
4936 109785013 : && TREE_CODE_CLASS (code) == tcc_comparison)
4937 : {
4938 71826 : fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4939 71826 : pred_e->dest->index);
4940 71826 : print_generic_expr (dump_file, ops[0], TDF_SLIM);
4941 71826 : fprintf (dump_file, " %s ", get_tree_code_name (code));
4942 71826 : print_generic_expr (dump_file, ops[1], TDF_SLIM);
4943 107368 : fprintf (dump_file, " == %s\n",
4944 71826 : integer_zerop (result) ? "false" : "true");
4945 : }
4946 109713187 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4947 109713187 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4948 109713187 : vno1->predicated_values = 1;
4949 109713187 : vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4950 : sizeof (vn_pval));
4951 109713187 : vno1->u.values->next = NULL;
4952 109713187 : vno1->u.values->result = result;
4953 109713187 : vno1->u.values->n = 1;
4954 109713187 : vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4955 109713187 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4956 : }
4957 :
4958 : static bool
4959 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4960 :
4961 : static tree
4962 1791718 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
4963 : edge e = NULL)
4964 : {
4965 1791718 : if (! vno->predicated_values)
4966 0 : return vno->u.result;
4967 3739961 : for (vn_pval *val = vno->u.values; val; val = val->next)
4968 5731804 : for (unsigned i = 0; i < val->n; ++i)
4969 : {
4970 3783561 : basic_block cand
4971 3783561 : = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
4972 : /* Do not handle backedge executability optimistically since
4973 : when figuring out whether to iterate we do not consider
4974 : changed predication.
4975 : When asking for predicated values on an edge avoid looking
4976 : at edge executability for edges forward in our iteration
4977 : as well. */
4978 3783561 : if (e && (e->flags & EDGE_DFS_BACK))
4979 : {
4980 22659 : if (dominated_by_p (CDI_DOMINATORS, bb, cand))
4981 7110 : return val->result;
4982 : }
4983 3760902 : else if (dominated_by_p_w_unex (bb, cand, false))
4984 549107 : return val->result;
4985 : }
4986 : return NULL_TREE;
4987 : }
4988 :
4989 : static tree
4990 202423 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
4991 : {
4992 0 : return vn_nary_op_get_predicated_value (vno, e->src, e);
4993 : }
4994 :
4995 : /* Insert the rhs of STMT into the current hash table with a value number of
4996 : RESULT. */
4997 :
4998 : static vn_nary_op_t
4999 46152300 : vn_nary_op_insert_stmt (gimple *stmt, tree result)
5000 : {
5001 46152300 : vn_nary_op_t vno1
5002 46152300 : = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
5003 46152300 : result, VN_INFO (result)->value_id);
5004 46152300 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
5005 46152300 : return vn_nary_op_insert_into (vno1, valid_info->nary);
5006 : }
5007 :
5008 : /* Compute a hashcode for PHI operation VP1 and return it. */
5009 :
5010 : static inline hashval_t
5011 50671218 : vn_phi_compute_hash (vn_phi_t vp1)
5012 : {
5013 50671218 : inchash::hash hstate;
5014 50671218 : tree phi1op;
5015 50671218 : tree type;
5016 50671218 : edge e;
5017 50671218 : edge_iterator ei;
5018 :
5019 101342436 : hstate.add_int (EDGE_COUNT (vp1->block->preds));
5020 50671218 : switch (EDGE_COUNT (vp1->block->preds))
5021 : {
5022 : case 1:
5023 : break;
5024 43572560 : case 2:
5025 : /* When this is a PHI node subject to CSE for different blocks
5026 : avoid hashing the block index. */
5027 43572560 : if (vp1->cclhs)
5028 : break;
5029 : /* Fallthru. */
5030 34152551 : default:
5031 34152551 : hstate.add_int (vp1->block->index);
5032 : }
5033 :
5034 : /* If all PHI arguments are constants we need to distinguish
5035 : the PHI node via its type. */
5036 50671218 : type = vp1->type;
5037 50671218 : hstate.merge_hash (vn_hash_type (type));
5038 :
5039 176600374 : FOR_EACH_EDGE (e, ei, vp1->block->preds)
5040 : {
5041 : /* Don't hash backedge values they need to be handled as VN_TOP
5042 : for optimistic value-numbering. */
5043 125929156 : if (e->flags & EDGE_DFS_BACK)
5044 27872784 : continue;
5045 :
5046 98056372 : phi1op = vp1->phiargs[e->dest_idx];
5047 98056372 : if (phi1op == VN_TOP)
5048 250118 : continue;
5049 97806254 : inchash::add_expr (phi1op, hstate);
5050 : }
5051 :
5052 50671218 : return hstate.end ();
5053 : }
5054 :
5055 :
5056 : /* Return true if COND1 and COND2 represent the same condition, set
5057 : *INVERTED_P if one needs to be inverted to make it the same as
5058 : the other. */
5059 :
5060 : static bool
5061 3552491 : cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
5062 : gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
5063 : {
5064 3552491 : enum tree_code code1 = gimple_cond_code (cond1);
5065 3552491 : enum tree_code code2 = gimple_cond_code (cond2);
5066 :
5067 3552491 : *inverted_p = false;
5068 3552491 : if (code1 == code2)
5069 : ;
5070 285264 : else if (code1 == swap_tree_comparison (code2))
5071 : std::swap (lhs2, rhs2);
5072 248335 : else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
5073 113714 : *inverted_p = true;
5074 134621 : else if (code1 == invert_tree_comparison
5075 134621 : (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
5076 : {
5077 10278 : std::swap (lhs2, rhs2);
5078 10278 : *inverted_p = true;
5079 : }
5080 : else
5081 : return false;
5082 :
5083 3428148 : return ((expressions_equal_p (lhs1, lhs2)
5084 108382 : && expressions_equal_p (rhs1, rhs2))
5085 3452845 : || (commutative_tree_code (code1)
5086 1771285 : && expressions_equal_p (lhs1, rhs2)
5087 2413 : && expressions_equal_p (rhs1, lhs2)));
5088 : }
5089 :
5090 : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
5091 :
5092 : static int
5093 40656291 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
5094 : {
5095 40656291 : if (vp1->hashcode != vp2->hashcode)
5096 : return false;
5097 :
5098 12515619 : if (vp1->block != vp2->block)
5099 : {
5100 10680546 : if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
5101 : return false;
5102 :
5103 3560182 : switch (EDGE_COUNT (vp1->block->preds))
5104 : {
5105 : case 1:
5106 : /* Single-arg PHIs are just copies. */
5107 : break;
5108 :
5109 3560182 : case 2:
5110 3560182 : {
5111 : /* Make sure both PHIs are classified as CSEable. */
5112 3560182 : if (! vp1->cclhs || ! vp2->cclhs)
5113 : return false;
5114 :
5115 : /* Rule out backedges into the PHI. */
5116 3560182 : gcc_checking_assert
5117 : (vp1->block->loop_father->header != vp1->block
5118 : && vp2->block->loop_father->header != vp2->block);
5119 :
5120 : /* If the PHI nodes do not have compatible types
5121 : they are not the same. */
5122 3560182 : if (!types_compatible_p (vp1->type, vp2->type))
5123 : return false;
5124 :
5125 : /* If the immediate dominator end in switch stmts multiple
5126 : values may end up in the same PHI arg via intermediate
5127 : CFG merges. */
5128 3552491 : basic_block idom1
5129 3552491 : = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5130 3552491 : basic_block idom2
5131 3552491 : = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
5132 3552491 : gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
5133 : && EDGE_COUNT (idom2->succs) == 2);
5134 :
5135 : /* Verify the controlling stmt is the same. */
5136 7104982 : gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
5137 7104982 : gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
5138 3552491 : bool inverted_p;
5139 3552491 : if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
5140 3552491 : last2, vp2->cclhs, vp2->ccrhs,
5141 : &inverted_p))
5142 : return false;
5143 :
5144 : /* Get at true/false controlled edges into the PHI. */
5145 83781 : edge te1, te2, fe1, fe2;
5146 83781 : if (! extract_true_false_controlled_edges (idom1, vp1->block,
5147 : &te1, &fe1)
5148 83781 : || ! extract_true_false_controlled_edges (idom2, vp2->block,
5149 : &te2, &fe2))
5150 : return false;
5151 :
5152 : /* Swap edges if the second condition is the inverted of the
5153 : first. */
5154 47555 : if (inverted_p)
5155 1867 : std::swap (te2, fe2);
5156 :
5157 : /* Since we do not know which edge will be executed we have
5158 : to be careful when matching VN_TOP. Be conservative and
5159 : only match VN_TOP == VN_TOP for now, we could allow
5160 : VN_TOP on the not prevailing PHI though. See for example
5161 : PR102920. */
5162 47555 : if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
5163 47555 : vp2->phiargs[te2->dest_idx], false)
5164 93370 : || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
5165 45815 : vp2->phiargs[fe2->dest_idx], false))
5166 1740 : return false;
5167 :
5168 : return true;
5169 : }
5170 :
5171 : default:
5172 : return false;
5173 : }
5174 : }
5175 :
5176 : /* If the PHI nodes do not have compatible types
5177 : they are not the same. */
5178 8955437 : if (!types_compatible_p (vp1->type, vp2->type))
5179 : return false;
5180 :
5181 : /* Any phi in the same block will have it's arguments in the
5182 : same edge order, because of how we store phi nodes. */
5183 8954309 : unsigned nargs = EDGE_COUNT (vp1->block->preds);
5184 20622733 : for (unsigned i = 0; i < nargs; ++i)
5185 : {
5186 16516634 : tree phi1op = vp1->phiargs[i];
5187 16516634 : tree phi2op = vp2->phiargs[i];
5188 16516634 : if (phi1op == phi2op)
5189 11571754 : continue;
5190 4944880 : if (!expressions_equal_p (phi1op, phi2op, false))
5191 : return false;
5192 : }
5193 :
5194 : return true;
5195 : }
5196 :
5197 : /* Lookup PHI in the current hash table, and return the resulting
5198 : value number if it exists in the hash table. Return NULL_TREE if
5199 : it does not exist in the hash table. */
5200 :
5201 : static tree
5202 27724911 : vn_phi_lookup (gimple *phi, bool backedges_varying_p)
5203 : {
5204 27724911 : vn_phi_s **slot;
5205 27724911 : struct vn_phi_s *vp1;
5206 27724911 : edge e;
5207 27724911 : edge_iterator ei;
5208 :
5209 27724911 : vp1 = XALLOCAVAR (struct vn_phi_s,
5210 : sizeof (struct vn_phi_s)
5211 : + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
5212 :
5213 : /* Canonicalize the SSA_NAME's to their value number. */
5214 95909032 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5215 : {
5216 68184121 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5217 68184121 : if (TREE_CODE (def) == SSA_NAME
5218 56804817 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5219 : {
5220 54226795 : if (!virtual_operand_p (def)
5221 54226795 : && ssa_undefined_value_p (def, false))
5222 138850 : def = VN_TOP;
5223 : else
5224 54087945 : def = SSA_VAL (def);
5225 : }
5226 68184121 : vp1->phiargs[e->dest_idx] = def;
5227 : }
5228 27724911 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5229 27724911 : vp1->block = gimple_bb (phi);
5230 : /* Extract values of the controlling condition. */
5231 27724911 : vp1->cclhs = NULL_TREE;
5232 27724911 : vp1->ccrhs = NULL_TREE;
5233 27724911 : if (EDGE_COUNT (vp1->block->preds) == 2
5234 27724911 : && vp1->block->loop_father->header != vp1->block)
5235 : {
5236 8729544 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5237 8729544 : if (EDGE_COUNT (idom1->succs) == 2)
5238 17360582 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5239 : {
5240 : /* ??? We want to use SSA_VAL here. But possibly not
5241 : allow VN_TOP. */
5242 8443926 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5243 8443926 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5244 : }
5245 : }
5246 27724911 : vp1->hashcode = vn_phi_compute_hash (vp1);
5247 27724911 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
5248 27724911 : if (!slot)
5249 : return NULL_TREE;
5250 4151914 : return (*slot)->result;
5251 : }
5252 :
5253 : /* Insert PHI into the current hash table with a value number of
5254 : RESULT. */
5255 :
5256 : static vn_phi_t
5257 22946307 : vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
5258 : {
5259 22946307 : vn_phi_s **slot;
5260 22946307 : vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
5261 : sizeof (vn_phi_s)
5262 : + ((gimple_phi_num_args (phi) - 1)
5263 : * sizeof (tree)));
5264 22946307 : edge e;
5265 22946307 : edge_iterator ei;
5266 :
5267 : /* Canonicalize the SSA_NAME's to their value number. */
5268 80691342 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5269 : {
5270 57745035 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5271 57745035 : if (TREE_CODE (def) == SSA_NAME
5272 47430189 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5273 : {
5274 44852599 : if (!virtual_operand_p (def)
5275 44852599 : && ssa_undefined_value_p (def, false))
5276 111512 : def = VN_TOP;
5277 : else
5278 44741087 : def = SSA_VAL (def);
5279 : }
5280 57745035 : vp1->phiargs[e->dest_idx] = def;
5281 : }
5282 22946307 : vp1->value_id = VN_INFO (result)->value_id;
5283 22946307 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5284 22946307 : vp1->block = gimple_bb (phi);
5285 : /* Extract values of the controlling condition. */
5286 22946307 : vp1->cclhs = NULL_TREE;
5287 22946307 : vp1->ccrhs = NULL_TREE;
5288 22946307 : if (EDGE_COUNT (vp1->block->preds) == 2
5289 22946307 : && vp1->block->loop_father->header != vp1->block)
5290 : {
5291 8355297 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5292 8355297 : if (EDGE_COUNT (idom1->succs) == 2)
5293 16616280 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5294 : {
5295 : /* ??? We want to use SSA_VAL here. But possibly not
5296 : allow VN_TOP. */
5297 8074741 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5298 8074741 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5299 : }
5300 : }
5301 22946307 : vp1->result = result;
5302 22946307 : vp1->hashcode = vn_phi_compute_hash (vp1);
5303 :
5304 22946307 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5305 22946307 : gcc_assert (!*slot);
5306 :
5307 22946307 : *slot = vp1;
5308 22946307 : vp1->next = last_inserted_phi;
5309 22946307 : last_inserted_phi = vp1;
5310 22946307 : return vp1;
5311 : }
5312 :
5313 :
5314 : /* Return true if BB1 is dominated by BB2 taking into account edges
5315 : that are not executable. When ALLOW_BACK is false consider not
5316 : executable backedges as executable. */
5317 :
5318 : static bool
5319 77554138 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
5320 : {
5321 77554138 : edge_iterator ei;
5322 77554138 : edge e;
5323 :
5324 77554138 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5325 : return true;
5326 :
5327 : /* Before iterating we'd like to know if there exists a
5328 : (executable) path from bb2 to bb1 at all, if not we can
5329 : directly return false. For now simply iterate once. */
5330 :
5331 : /* Iterate to the single executable bb1 predecessor. */
5332 21986899 : if (EDGE_COUNT (bb1->preds) > 1)
5333 : {
5334 3006852 : edge prede = NULL;
5335 6565252 : FOR_EACH_EDGE (e, ei, bb1->preds)
5336 6137091 : if ((e->flags & EDGE_EXECUTABLE)
5337 631871 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5338 : {
5339 5585543 : if (prede)
5340 : {
5341 : prede = NULL;
5342 : break;
5343 : }
5344 : prede = e;
5345 : }
5346 3006852 : if (prede)
5347 : {
5348 428161 : bb1 = prede->src;
5349 :
5350 : /* Re-do the dominance check with changed bb1. */
5351 428161 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5352 : return true;
5353 : }
5354 : }
5355 :
5356 : /* Iterate to the single executable bb2 successor. */
5357 21742682 : if (EDGE_COUNT (bb2->succs) > 1)
5358 : {
5359 6785411 : edge succe = NULL;
5360 13748086 : FOR_EACH_EDGE (e, ei, bb2->succs)
5361 13571071 : if ((e->flags & EDGE_EXECUTABLE)
5362 218701 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5363 : {
5364 13352422 : if (succe)
5365 : {
5366 : succe = NULL;
5367 : break;
5368 : }
5369 : succe = e;
5370 : }
5371 6785411 : if (succe
5372 : /* Limit the number of edges we check, we should bring in
5373 : context from the iteration and compute the single
5374 : executable incoming edge when visiting a block. */
5375 6785411 : && EDGE_COUNT (succe->dest->preds) < 8)
5376 : {
5377 : /* Verify the reached block is only reached through succe.
5378 : If there is only one edge we can spare us the dominator
5379 : check and iterate directly. */
5380 134770 : if (EDGE_COUNT (succe->dest->preds) > 1)
5381 : {
5382 60033 : FOR_EACH_EDGE (e, ei, succe->dest->preds)
5383 47154 : if (e != succe
5384 30631 : && ((e->flags & EDGE_EXECUTABLE)
5385 19099 : || (!allow_back && (e->flags & EDGE_DFS_BACK))))
5386 : {
5387 : succe = NULL;
5388 : break;
5389 : }
5390 : }
5391 134770 : if (succe)
5392 : {
5393 123229 : bb2 = succe->dest;
5394 :
5395 : /* Re-do the dominance check with changed bb2. */
5396 123229 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5397 : return true;
5398 : }
5399 : }
5400 : }
5401 : /* Iterate to the single successor of bb2 with only a single executable
5402 : incoming edge. */
5403 14957271 : else if (EDGE_COUNT (bb2->succs) == 1
5404 14411091 : && EDGE_COUNT (single_succ (bb2)->preds) > 1
5405 : /* Limit the number of edges we check, we should bring in
5406 : context from the iteration and compute the single
5407 : executable incoming edge when visiting a block. */
5408 29110718 : && EDGE_COUNT (single_succ (bb2)->preds) < 8)
5409 : {
5410 5177016 : edge prede = NULL;
5411 11670725 : FOR_EACH_EDGE (e, ei, single_succ (bb2)->preds)
5412 11114872 : if ((e->flags & EDGE_EXECUTABLE)
5413 1369365 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5414 : {
5415 9749981 : if (prede)
5416 : {
5417 : prede = NULL;
5418 : break;
5419 : }
5420 : prede = e;
5421 : }
5422 : /* We might actually get to a query with BB2 not visited yet when
5423 : we're querying for a predicated value. */
5424 5177016 : if (prede && prede->src == bb2)
5425 : {
5426 492846 : bb2 = prede->dest;
5427 :
5428 : /* Re-do the dominance check with changed bb2. */
5429 492846 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5430 : return true;
5431 : }
5432 : }
5433 :
5434 : /* We could now iterate updating bb1 / bb2. */
5435 : return false;
5436 : }
5437 :
5438 : /* Set the value number of FROM to TO, return true if it has changed
5439 : as a result. */
5440 :
5441 : static inline bool
5442 209706257 : set_ssa_val_to (tree from, tree to)
5443 : {
5444 209706257 : vn_ssa_aux_t from_info = VN_INFO (from);
5445 209706257 : tree currval = from_info->valnum; // SSA_VAL (from)
5446 209706257 : poly_int64 toff, coff;
5447 209706257 : bool curr_undefined = false;
5448 209706257 : bool curr_invariant = false;
5449 :
5450 : /* The only thing we allow as value numbers are ssa_names
5451 : and invariants. So assert that here. We don't allow VN_TOP
5452 : as visiting a stmt should produce a value-number other than
5453 : that.
5454 : ??? Still VN_TOP can happen for unreachable code, so force
5455 : it to varying in that case. Not all code is prepared to
5456 : get VN_TOP on valueization. */
5457 209706257 : if (to == VN_TOP)
5458 : {
5459 : /* ??? When iterating and visiting PHI <undef, backedge-value>
5460 : for the first time we rightfully get VN_TOP and we need to
5461 : preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5462 : With SCCVN we were simply lucky we iterated the other PHI
5463 : cycles first and thus visited the backedge-value DEF. */
5464 0 : if (currval == VN_TOP)
5465 0 : goto set_and_exit;
5466 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5467 0 : fprintf (dump_file, "Forcing value number to varying on "
5468 : "receiving VN_TOP\n");
5469 : to = from;
5470 : }
5471 :
5472 209706257 : gcc_checking_assert (to != NULL_TREE
5473 : && ((TREE_CODE (to) == SSA_NAME
5474 : && (to == from || SSA_VAL (to) == to))
5475 : || is_gimple_min_invariant (to)));
5476 :
5477 209706257 : if (from != to)
5478 : {
5479 33549302 : if (currval == from)
5480 : {
5481 14653 : if (dump_file && (dump_flags & TDF_DETAILS))
5482 : {
5483 0 : fprintf (dump_file, "Not changing value number of ");
5484 0 : print_generic_expr (dump_file, from);
5485 0 : fprintf (dump_file, " from VARYING to ");
5486 0 : print_generic_expr (dump_file, to);
5487 0 : fprintf (dump_file, "\n");
5488 : }
5489 : return false;
5490 : }
5491 33534649 : curr_invariant = is_gimple_min_invariant (currval);
5492 67069298 : curr_undefined = (TREE_CODE (currval) == SSA_NAME
5493 3920567 : && !virtual_operand_p (currval)
5494 37222888 : && ssa_undefined_value_p (currval, false));
5495 33534649 : if (currval != VN_TOP
5496 : && !curr_invariant
5497 5469580 : && !curr_undefined
5498 37441788 : && is_gimple_min_invariant (to))
5499 : {
5500 210 : if (dump_file && (dump_flags & TDF_DETAILS))
5501 : {
5502 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5503 : "value number of ");
5504 0 : print_generic_expr (dump_file, from);
5505 0 : fprintf (dump_file, " from ");
5506 0 : print_generic_expr (dump_file, currval);
5507 0 : fprintf (dump_file, " (non-constant) to ");
5508 0 : print_generic_expr (dump_file, to);
5509 0 : fprintf (dump_file, " (constant)\n");
5510 : }
5511 : to = from;
5512 : }
5513 33534439 : else if (currval != VN_TOP
5514 5469370 : && !curr_undefined
5515 5455942 : && TREE_CODE (to) == SSA_NAME
5516 4596080 : && !virtual_operand_p (to)
5517 37898191 : && ssa_undefined_value_p (to, false))
5518 : {
5519 6 : if (dump_file && (dump_flags & TDF_DETAILS))
5520 : {
5521 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5522 : "value number of ");
5523 0 : print_generic_expr (dump_file, from);
5524 0 : fprintf (dump_file, " from ");
5525 0 : print_generic_expr (dump_file, currval);
5526 0 : fprintf (dump_file, " (non-undefined) to ");
5527 0 : print_generic_expr (dump_file, to);
5528 0 : fprintf (dump_file, " (undefined)\n");
5529 : }
5530 : to = from;
5531 : }
5532 33534433 : else if (TREE_CODE (to) == SSA_NAME
5533 33534433 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5534 : to = from;
5535 : }
5536 :
5537 176156955 : set_and_exit:
5538 209691604 : if (dump_file && (dump_flags & TDF_DETAILS))
5539 : {
5540 402186 : fprintf (dump_file, "Setting value number of ");
5541 402186 : print_generic_expr (dump_file, from);
5542 402186 : fprintf (dump_file, " to ");
5543 402186 : print_generic_expr (dump_file, to);
5544 : }
5545 :
5546 209691604 : if (currval != to
5547 171599512 : && !operand_equal_p (currval, to, 0)
5548 : /* Different undefined SSA names are not actually different. See
5549 : PR82320 for a testcase were we'd otherwise not terminate iteration. */
5550 171529940 : && !(curr_undefined
5551 3468 : && TREE_CODE (to) == SSA_NAME
5552 613 : && !virtual_operand_p (to)
5553 613 : && ssa_undefined_value_p (to, false))
5554 : /* ??? For addresses involving volatile objects or types operand_equal_p
5555 : does not reliably detect ADDR_EXPRs as equal. We know we are only
5556 : getting invariant gimple addresses here, so can use
5557 : get_addr_base_and_unit_offset to do this comparison. */
5558 381220899 : && !(TREE_CODE (currval) == ADDR_EXPR
5559 469133 : && TREE_CODE (to) == ADDR_EXPR
5560 12 : && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5561 6 : == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5562 6 : && known_eq (coff, toff)))
5563 : {
5564 171529289 : if (to != from
5565 29097339 : && currval != VN_TOP
5566 1035886 : && !curr_undefined
5567 : /* We do not want to allow lattice transitions from one value
5568 : to another since that may lead to not terminating iteration
5569 : (see PR95049). Since there's no convenient way to check
5570 : for the allowed transition of VAL -> PHI (loop entry value,
5571 : same on two PHIs, to same PHI result) we restrict the check
5572 : to invariants. */
5573 1035886 : && curr_invariant
5574 172218434 : && is_gimple_min_invariant (to))
5575 : {
5576 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5577 0 : fprintf (dump_file, " forced VARYING");
5578 : to = from;
5579 : }
5580 171529289 : if (dump_file && (dump_flags & TDF_DETAILS))
5581 401870 : fprintf (dump_file, " (changed)\n");
5582 171529289 : from_info->valnum = to;
5583 171529289 : return true;
5584 : }
5585 38162315 : if (dump_file && (dump_flags & TDF_DETAILS))
5586 316 : fprintf (dump_file, "\n");
5587 : return false;
5588 : }
5589 :
5590 : /* Set all definitions in STMT to value number to themselves.
5591 : Return true if a value number changed. */
5592 :
5593 : static bool
5594 310316863 : defs_to_varying (gimple *stmt)
5595 : {
5596 310316863 : bool changed = false;
5597 310316863 : ssa_op_iter iter;
5598 310316863 : def_operand_p defp;
5599 :
5600 340829064 : FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5601 : {
5602 30512201 : tree def = DEF_FROM_PTR (defp);
5603 30512201 : changed |= set_ssa_val_to (def, def);
5604 : }
5605 310316863 : return changed;
5606 : }
5607 :
5608 : /* Visit a copy between LHS and RHS, return true if the value number
5609 : changed. */
5610 :
5611 : static bool
5612 8204201 : visit_copy (tree lhs, tree rhs)
5613 : {
5614 : /* Valueize. */
5615 8204201 : rhs = SSA_VAL (rhs);
5616 :
5617 8204201 : return set_ssa_val_to (lhs, rhs);
5618 : }
5619 :
5620 : /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5621 : is the same. */
5622 :
5623 : static tree
5624 2486651 : valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5625 : {
5626 2486651 : if (TREE_CODE (op) == SSA_NAME)
5627 2179901 : op = vn_valueize (op);
5628 :
5629 : /* Either we have the op widened available. */
5630 2486651 : tree ops[3] = {};
5631 2486651 : ops[0] = op;
5632 2486651 : tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5633 : wide_type, ops, NULL);
5634 2486651 : if (tem)
5635 : return tem;
5636 :
5637 : /* Or the op is truncated from some existing value. */
5638 2192783 : if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5639 : {
5640 551734 : gimple *def = SSA_NAME_DEF_STMT (op);
5641 551734 : if (is_gimple_assign (def)
5642 551734 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5643 : {
5644 298771 : tem = gimple_assign_rhs1 (def);
5645 298771 : if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5646 : {
5647 201182 : if (TREE_CODE (tem) == SSA_NAME)
5648 201182 : tem = vn_valueize (tem);
5649 : return tem;
5650 : }
5651 : }
5652 : }
5653 :
5654 : /* For constants simply extend it. */
5655 1991601 : if (TREE_CODE (op) == INTEGER_CST)
5656 340202 : return wide_int_to_tree (wide_type, wi::to_widest (op));
5657 :
5658 : return NULL_TREE;
5659 : }
5660 :
5661 : /* Return true if RESULT, the result of a value-number lookup, may be
5662 : used at the statement being visited. A result of wrapping type can
5663 : be inserted for code hoisting without introducing undefined
5664 : overflow; anything else has to be available. See PR86554. */
5665 :
5666 : static bool
5667 21963 : vn_nary_result_avail_or_insertable_p (tree result)
5668 : {
5669 21963 : return (TYPE_OVERFLOW_WRAPS (TREE_TYPE (result))
5670 13746 : || (rpo_avail && vn_context_bb
5671 13746 : && rpo_avail->eliminate_avail (vn_context_bb, result)));
5672 : }
5673 :
5674 : /* If OP is an SSA name defined by a conversion from an integral type,
5675 : return the valueized source of the conversion, otherwise return
5676 : NULL_TREE. */
5677 :
5678 : static tree
5679 14261588 : ssa_integral_conversion_op (tree op)
5680 : {
5681 14261588 : if (TREE_CODE (op) != SSA_NAME)
5682 : return NULL_TREE;
5683 13949287 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op));
5684 11704049 : if (!def || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5685 : return NULL_TREE;
5686 1540508 : const tree src = gimple_assign_rhs1 (def);
5687 1540508 : if (!INTEGRAL_TYPE_P (TREE_TYPE (src)))
5688 : return NULL_TREE;
5689 1285171 : return vn_valueize (src);
5690 : }
5691 :
5692 : /* Visit a nary operator RHS, value number it, and return true if the
5693 : value number of LHS has changed as a result. */
5694 :
5695 : static bool
5696 49984835 : visit_nary_op (tree lhs, gassign *stmt)
5697 : {
5698 49984835 : vn_nary_op_t vnresult;
5699 49984835 : tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5700 49984835 : if (! result && vnresult)
5701 143485 : result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5702 46224624 : if (result)
5703 3829991 : return set_ssa_val_to (lhs, result);
5704 :
5705 : /* Do some special pattern matching for redundancies of operations
5706 : in different types. */
5707 46154844 : enum tree_code code = gimple_assign_rhs_code (stmt);
5708 46154844 : tree type = TREE_TYPE (lhs);
5709 46154844 : tree rhs1 = gimple_assign_rhs1 (stmt);
5710 46154844 : switch (code)
5711 : {
5712 10236331 : CASE_CONVERT:
5713 : /* Match arithmetic done in a different type where we can easily
5714 : substitute the result from some earlier sign-changed or widened
5715 : operation. */
5716 10236331 : if (INTEGRAL_TYPE_P (type)
5717 9164774 : && TREE_CODE (rhs1) == SSA_NAME
5718 : /* We only handle sign-changes, zero-extension -> & mask or
5719 : sign-extension if we know the inner operation doesn't
5720 : overflow. */
5721 19160547 : && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5722 5380604 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5723 5379809 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5724 8195617 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5725 6050262 : || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5726 : {
5727 7794430 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5728 5676441 : if (def
5729 5676441 : && (gimple_assign_rhs_code (def) == PLUS_EXPR
5730 4440165 : || gimple_assign_rhs_code (def) == MINUS_EXPR
5731 4285621 : || gimple_assign_rhs_code (def) == MULT_EXPR))
5732 : {
5733 2003880 : tree ops[3] = {};
5734 : /* When requiring a sign-extension we cannot model a
5735 : previous truncation with a single op so don't bother. */
5736 2003880 : bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5737 : /* Either we have the op widened available. */
5738 2003880 : ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5739 : allow_truncate);
5740 2003880 : if (ops[0])
5741 965542 : ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5742 : allow_truncate);
5743 2003880 : if (ops[0] && ops[1])
5744 : {
5745 352481 : ops[0] = vn_nary_op_lookup_pieces
5746 352481 : (2, gimple_assign_rhs_code (def), type, ops, NULL);
5747 : /* We have wider operation available. */
5748 352481 : if (ops[0] && vn_nary_result_avail_or_insertable_p (ops[0]))
5749 : {
5750 8158 : unsigned lhs_prec = TYPE_PRECISION (type);
5751 8158 : unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5752 8158 : if (lhs_prec == rhs_prec
5753 8158 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5754 809 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5755 : {
5756 7521 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5757 7521 : NOP_EXPR, type, ops[0]);
5758 7521 : result = vn_nary_build_or_lookup (&match_op);
5759 7521 : if (result)
5760 : {
5761 7521 : bool changed = set_ssa_val_to (lhs, result);
5762 7521 : if (TREE_CODE (result) == SSA_NAME)
5763 7521 : vn_nary_op_insert_stmt (stmt, result);
5764 7521 : return changed;
5765 : }
5766 : }
5767 : else
5768 : {
5769 637 : tree mask = wide_int_to_tree
5770 637 : (type, wi::mask (rhs_prec, false, lhs_prec));
5771 637 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5772 637 : BIT_AND_EXPR,
5773 637 : TREE_TYPE (lhs),
5774 637 : ops[0], mask);
5775 637 : result = vn_nary_build_or_lookup (&match_op);
5776 637 : if (result)
5777 : {
5778 637 : bool changed = set_ssa_val_to (lhs, result);
5779 637 : if (TREE_CODE (result) == SSA_NAME)
5780 637 : vn_nary_op_insert_stmt (stmt, result);
5781 637 : return changed;
5782 : }
5783 : }
5784 : }
5785 : }
5786 : }
5787 : }
5788 : break;
5789 13890160 : case PLUS_EXPR:
5790 13890160 : case MINUS_EXPR:
5791 13890160 : {
5792 : /* Match (T)A +- B against an existing (T)(A +- B'), the inverse
5793 : of the conversion case above, so the redundancy is detected
5794 : regardless of the order the two forms appear in the IL.
5795 : See PR124545. The narrow operation is only ever looked up,
5796 : never created: assuming no overflow is only valid for
5797 : operations the program actually executes, so the narrow
5798 : leader has to be available. Creating the narrow operation
5799 : instead is wrong-code, see PR126415. */
5800 13890160 : const tree narrow1 = ssa_integral_conversion_op (vn_valueize (rhs1));
5801 13890160 : if (!INTEGRAL_TYPE_P (type) || !narrow1)
5802 : break;
5803 1106659 : const tree ntype = TREE_TYPE (narrow1);
5804 : /* A sign-change keeps the value bit-identical; a widening is
5805 : only handled when the narrow operation cannot wrap. */
5806 1106659 : const bool sign_change_p
5807 1106659 : = TYPE_PRECISION (ntype) == TYPE_PRECISION (type);
5808 1106659 : const bool nowrap_widening_p
5809 1106659 : = (TYPE_PRECISION (ntype) < TYPE_PRECISION (type)
5810 1106659 : && TYPE_OVERFLOW_UNDEFINED (ntype));
5811 1106659 : if (!sign_change_p && !nowrap_widening_p)
5812 : break;
5813 : /* Determine the narrow variant of the second operand: a
5814 : constant that narrows and extends back unchanged, or a
5815 : conversion from the same narrow type. */
5816 845972 : const tree rhs2 = gimple_assign_rhs2 (stmt);
5817 845972 : tree narrow2 = NULL_TREE;
5818 845972 : if (TREE_CODE (rhs2) == INTEGER_CST)
5819 : {
5820 474544 : const widest_int cst = wi::to_widest (rhs2);
5821 474544 : const widest_int narrowed
5822 474544 : = wi::ext (cst, TYPE_PRECISION (ntype), TYPE_SIGN (ntype));
5823 474544 : const widest_int extended
5824 474544 : = wi::ext (narrowed, TYPE_PRECISION (type), TYPE_SIGN (type));
5825 474544 : if (cst == extended)
5826 471779 : narrow2 = fold_convert (ntype, rhs2);
5827 474550 : }
5828 371428 : else if (TREE_CODE (rhs2) == SSA_NAME)
5829 : {
5830 371428 : const tree op = ssa_integral_conversion_op (vn_valueize (rhs2));
5831 371428 : if (op && types_compatible_p (TREE_TYPE (op), ntype))
5832 : narrow2 = op;
5833 : }
5834 641701 : if (!narrow2)
5835 : break;
5836 638936 : tree ops[3] = { narrow1, narrow2 };
5837 638936 : const tree narrow_val
5838 638936 : = vn_nary_op_lookup_pieces (2, code, ntype, ops, NULL);
5839 : /* We have a narrower or sign-changed operation available. */
5840 638936 : if (narrow_val && vn_nary_result_avail_or_insertable_p (narrow_val))
5841 : {
5842 11364 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5843 11364 : NOP_EXPR, type, narrow_val);
5844 11364 : result = vn_nary_build_or_lookup (&match_op);
5845 11364 : if (result)
5846 : {
5847 11364 : const bool changed = set_ssa_val_to (lhs, result);
5848 11364 : if (TREE_CODE (result) == SSA_NAME)
5849 11364 : vn_nary_op_insert_stmt (stmt, result);
5850 11364 : return changed;
5851 : }
5852 : }
5853 : }
5854 627572 : break;
5855 1538868 : case BIT_AND_EXPR:
5856 1538868 : if (INTEGRAL_TYPE_P (type)
5857 1497306 : && TREE_CODE (rhs1) == SSA_NAME
5858 1497306 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5859 913211 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5860 913091 : && default_vn_walk_kind != VN_NOWALK
5861 : && CHAR_BIT == 8
5862 : && BITS_PER_UNIT == 8
5863 : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5864 912882 : && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5865 912880 : && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5866 2451748 : && !integer_zerop (gimple_assign_rhs2 (stmt)))
5867 : {
5868 912880 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5869 667309 : if (ass
5870 667309 : && !gimple_has_volatile_ops (ass)
5871 665731 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5872 : {
5873 308659 : tree last_vuse = gimple_vuse (ass);
5874 308659 : tree op = gimple_assign_rhs1 (ass);
5875 925977 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5876 : default_vn_walk_kind,
5877 : NULL, true, &last_vuse,
5878 : gimple_assign_rhs2 (stmt));
5879 308659 : if (result
5880 309116 : && useless_type_conversion_p (TREE_TYPE (result),
5881 457 : TREE_TYPE (op)))
5882 457 : return set_ssa_val_to (lhs, result);
5883 : }
5884 : }
5885 : break;
5886 291777 : case BIT_FIELD_REF:
5887 291777 : if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
5888 : {
5889 291749 : tree op0 = vn_valueize (TREE_OPERAND (rhs1, 0));
5890 291749 : gassign *ass;
5891 291749 : if (TREE_CODE (op0) == SSA_NAME
5892 291749 : && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0)))
5893 246007 : && !gimple_has_volatile_ops (ass)
5894 537673 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5895 : {
5896 106720 : tree last_vuse = gimple_vuse (ass);
5897 106720 : tree op = gimple_assign_rhs1 (ass);
5898 : /* Avoid building invalid and unexpected refs. */
5899 106720 : if (TREE_CODE (op) != TARGET_MEM_REF
5900 : && TREE_CODE (op) != BIT_FIELD_REF
5901 : && TREE_CODE (op) != REALPART_EXPR
5902 : && TREE_CODE (op) != IMAGPART_EXPR)
5903 : {
5904 98839 : tree op = build3 (BIT_FIELD_REF, TREE_TYPE (rhs1),
5905 : gimple_assign_rhs1 (ass),
5906 98839 : TREE_OPERAND (rhs1, 1),
5907 98839 : TREE_OPERAND (rhs1, 2));
5908 197678 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5909 : default_vn_walk_kind,
5910 : NULL, true, &last_vuse);
5911 98839 : if (result
5912 98839 : && useless_type_conversion_p (type, TREE_TYPE (result)))
5913 2671 : return set_ssa_val_to (lhs, result);
5914 96765 : else if (result
5915 597 : && TYPE_SIZE (type)
5916 597 : && TYPE_SIZE (TREE_TYPE (result))
5917 97362 : && operand_equal_p (TYPE_SIZE (type),
5918 597 : TYPE_SIZE (TREE_TYPE (result))))
5919 : {
5920 597 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5921 597 : VIEW_CONVERT_EXPR,
5922 597 : type, result);
5923 597 : result = vn_nary_build_or_lookup (&match_op);
5924 597 : if (result)
5925 : {
5926 597 : bool changed = set_ssa_val_to (lhs, result);
5927 597 : if (TREE_CODE (result) == SSA_NAME)
5928 585 : vn_nary_op_insert_stmt (stmt, result);
5929 597 : return changed;
5930 : }
5931 : }
5932 : }
5933 : }
5934 : }
5935 : break;
5936 348649 : case TRUNC_DIV_EXPR:
5937 348649 : if (TYPE_UNSIGNED (type))
5938 : break;
5939 : /* Fallthru. */
5940 5585267 : case RDIV_EXPR:
5941 5585267 : case MULT_EXPR:
5942 : /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5943 5585267 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5944 : {
5945 5584353 : tree rhs[2];
5946 5584353 : rhs[0] = rhs1;
5947 5584353 : rhs[1] = gimple_assign_rhs2 (stmt);
5948 16744192 : for (unsigned i = 0; i <= 1; ++i)
5949 : {
5950 11167226 : unsigned j = i == 0 ? 1 : 0;
5951 11167226 : tree ops[2];
5952 11167226 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5953 11167226 : NEGATE_EXPR, type, rhs[i]);
5954 11167226 : ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5955 11167226 : ops[j] = rhs[j];
5956 11167226 : if (ops[i]
5957 11167226 : && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5958 : type, ops, NULL)))
5959 : {
5960 7387 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5961 7387 : NEGATE_EXPR, type, ops[0]);
5962 7387 : result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5963 7387 : if (result)
5964 : {
5965 7387 : bool changed = set_ssa_val_to (lhs, result);
5966 7387 : if (TREE_CODE (result) == SSA_NAME)
5967 7387 : vn_nary_op_insert_stmt (stmt, result);
5968 7387 : return changed;
5969 : }
5970 : }
5971 : }
5972 : }
5973 : break;
5974 371049 : case LSHIFT_EXPR:
5975 : /* For X << C, use the value number of X * (1 << C). */
5976 371049 : if (INTEGRAL_TYPE_P (type)
5977 355095 : && TYPE_OVERFLOW_WRAPS (type)
5978 560743 : && !TYPE_SATURATING (type))
5979 : {
5980 189694 : tree rhs2 = gimple_assign_rhs2 (stmt);
5981 189694 : if (TREE_CODE (rhs2) == INTEGER_CST
5982 110240 : && tree_fits_uhwi_p (rhs2)
5983 299934 : && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
5984 : {
5985 220480 : wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
5986 110240 : TYPE_PRECISION (type));
5987 220480 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5988 110240 : MULT_EXPR, type, rhs1,
5989 110240 : wide_int_to_tree (type, w));
5990 110240 : result = vn_nary_build_or_lookup (&match_op);
5991 110240 : if (result)
5992 : {
5993 110240 : bool changed = set_ssa_val_to (lhs, result);
5994 110240 : if (TREE_CODE (result) == SSA_NAME)
5995 110239 : vn_nary_op_insert_stmt (stmt, result);
5996 110240 : return changed;
5997 : }
5998 110240 : }
5999 : }
6000 : break;
6001 : default:
6002 : break;
6003 : }
6004 :
6005 46014567 : bool changed = set_ssa_val_to (lhs, lhs);
6006 46014567 : vn_nary_op_insert_stmt (stmt, lhs);
6007 46014567 : return changed;
6008 : }
6009 :
6010 : /* Visit a call STMT storing into LHS. Return true if the value number
6011 : of the LHS has changed as a result. */
6012 :
6013 : static bool
6014 8817270 : visit_reference_op_call (tree lhs, gcall *stmt)
6015 : {
6016 8817270 : bool changed = false;
6017 8817270 : struct vn_reference_s vr1;
6018 8817270 : vn_reference_t vnresult = NULL;
6019 8817270 : tree vdef = gimple_vdef (stmt);
6020 8817270 : modref_summary *summary;
6021 :
6022 : /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
6023 8817270 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
6024 4705922 : lhs = NULL_TREE;
6025 :
6026 8817270 : vn_reference_lookup_call (stmt, &vnresult, &vr1);
6027 :
6028 : /* If the lookup did not succeed for pure functions try to use
6029 : modref info to find a candidate to CSE to. */
6030 8817270 : const unsigned accesses_limit = 8;
6031 8817270 : if (!vnresult
6032 8127027 : && !vdef
6033 8127027 : && lhs
6034 2850147 : && gimple_vuse (stmt)
6035 10412569 : && (((summary = get_modref_function_summary (stmt, NULL))
6036 231245 : && !summary->global_memory_read
6037 95456 : && summary->load_accesses < accesses_limit)
6038 1500202 : || gimple_call_flags (stmt) & ECF_CONST))
6039 : {
6040 : /* First search if we can do something useful and build a
6041 : vector of all loads we have to check. */
6042 95847 : bool unknown_memory_access = false;
6043 95847 : auto_vec<ao_ref, accesses_limit> accesses;
6044 95847 : unsigned load_accesses = summary ? summary->load_accesses : 0;
6045 95847 : if (!unknown_memory_access)
6046 : /* Add loads done as part of setting up the call arguments.
6047 : That's also necessary for CONST functions which will
6048 : not have a modref summary. */
6049 283918 : for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
6050 : {
6051 188079 : tree arg = gimple_call_arg (stmt, i);
6052 188079 : if (TREE_CODE (arg) != SSA_NAME
6053 188079 : && !is_gimple_min_invariant (arg))
6054 : {
6055 64690 : if (accesses.length () >= accesses_limit - load_accesses)
6056 : {
6057 : unknown_memory_access = true;
6058 : break;
6059 : }
6060 32337 : accesses.quick_grow (accesses.length () + 1);
6061 32337 : ao_ref_init (&accesses.last (), arg);
6062 : }
6063 : }
6064 95847 : if (summary && !unknown_memory_access)
6065 : {
6066 : /* Add loads as analyzed by IPA modref. */
6067 329766 : for (auto base_node : summary->loads->bases)
6068 82952 : if (unknown_memory_access)
6069 : break;
6070 338196 : else for (auto ref_node : base_node->refs)
6071 90291 : if (unknown_memory_access)
6072 : break;
6073 379413 : else for (auto access_node : ref_node->accesses)
6074 : {
6075 252802 : accesses.quick_grow (accesses.length () + 1);
6076 126401 : ao_ref *r = &accesses.last ();
6077 126401 : if (!access_node.get_ao_ref (stmt, r))
6078 : {
6079 : /* Initialize a ref based on the argument and
6080 : unknown offset if possible. */
6081 17825 : tree arg = access_node.get_call_arg (stmt);
6082 17825 : if (arg && TREE_CODE (arg) == SSA_NAME)
6083 4358 : arg = SSA_VAL (arg);
6084 4358 : if (arg
6085 17815 : && TREE_CODE (arg) == ADDR_EXPR
6086 13463 : && (arg = get_base_address (arg))
6087 17821 : && DECL_P (arg))
6088 : {
6089 0 : ao_ref_init (r, arg);
6090 0 : r->ref = NULL_TREE;
6091 0 : r->base = arg;
6092 : }
6093 : else
6094 : {
6095 : unknown_memory_access = true;
6096 : break;
6097 : }
6098 : }
6099 108576 : r->base_alias_set = base_node->base;
6100 108576 : r->ref_alias_set = ref_node->ref;
6101 : }
6102 : }
6103 :
6104 : /* Walk the VUSE->VDEF chain optimistically trying to find an entry
6105 : for the call in the hashtable. */
6106 95847 : unsigned limit = (unknown_memory_access
6107 95847 : ? 0
6108 78014 : : (param_sccvn_max_alias_queries_per_access
6109 78014 : / (accesses.length () + 1)));
6110 95847 : tree saved_vuse = vr1.vuse;
6111 95847 : hashval_t saved_hashcode = vr1.hashcode;
6112 521485 : while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
6113 : {
6114 454754 : vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
6115 454754 : gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
6116 : /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
6117 : do not bother for now. */
6118 454754 : if (is_a <gphi *> (def))
6119 : break;
6120 851276 : vr1.vuse = vuse_ssa_val (gimple_vuse (def));
6121 425638 : vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
6122 425638 : vn_reference_lookup_1 (&vr1, &vnresult);
6123 425638 : limit--;
6124 : }
6125 :
6126 : /* If we found a candidate to CSE to verify it is valid. */
6127 95847 : if (vnresult && !accesses.is_empty ())
6128 : {
6129 1985 : tree vuse = vuse_ssa_val (gimple_vuse (stmt));
6130 7463 : while (vnresult && vuse != vr1.vuse)
6131 : {
6132 3493 : gimple *def = SSA_NAME_DEF_STMT (vuse);
6133 18765 : for (auto &ref : accesses)
6134 : {
6135 : /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
6136 : analysis overhead that we might be able to cache. */
6137 10051 : if (stmt_may_clobber_ref_p_1 (def, &ref, true))
6138 : {
6139 1765 : vnresult = NULL;
6140 1765 : break;
6141 : }
6142 : }
6143 6986 : vuse = vuse_ssa_val (gimple_vuse (def));
6144 : }
6145 : }
6146 95847 : vr1.vuse = saved_vuse;
6147 95847 : vr1.hashcode = saved_hashcode;
6148 95847 : }
6149 :
6150 8817270 : if (vnresult)
6151 : {
6152 690491 : if (vdef)
6153 : {
6154 175505 : if (vnresult->result_vdef)
6155 175505 : changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
6156 0 : else if (!lhs && gimple_call_lhs (stmt))
6157 : /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
6158 : as the call still acts as a lhs store. */
6159 0 : changed |= set_ssa_val_to (vdef, vdef);
6160 : else
6161 : /* If the call was discovered to be pure or const reflect
6162 : that as far as possible. */
6163 0 : changed |= set_ssa_val_to (vdef,
6164 : vuse_ssa_val (gimple_vuse (stmt)));
6165 : }
6166 :
6167 690491 : if (!vnresult->result && lhs)
6168 0 : vnresult->result = lhs;
6169 :
6170 690491 : if (vnresult->result && lhs)
6171 124871 : changed |= set_ssa_val_to (lhs, vnresult->result);
6172 : }
6173 : else
6174 : {
6175 8126779 : vn_reference_t vr2;
6176 8126779 : vn_reference_s **slot;
6177 8126779 : tree vdef_val = vdef;
6178 8126779 : if (vdef)
6179 : {
6180 : /* If we value numbered an indirect functions function to
6181 : one not clobbering memory value number its VDEF to its
6182 : VUSE. */
6183 4948404 : tree fn = gimple_call_fn (stmt);
6184 4948404 : if (fn && TREE_CODE (fn) == SSA_NAME)
6185 : {
6186 130286 : fn = SSA_VAL (fn);
6187 130286 : if (TREE_CODE (fn) == ADDR_EXPR
6188 2000 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6189 2000 : && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
6190 2000 : & (ECF_CONST | ECF_PURE))
6191 : /* If stmt has non-SSA_NAME lhs, value number the
6192 : vdef to itself, as the call still acts as a lhs
6193 : store. */
6194 131689 : && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
6195 2664 : vdef_val = vuse_ssa_val (gimple_vuse (stmt));
6196 : }
6197 4948404 : changed |= set_ssa_val_to (vdef, vdef_val);
6198 : }
6199 8126779 : if (lhs)
6200 3986477 : changed |= set_ssa_val_to (lhs, lhs);
6201 8126779 : vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
6202 8126779 : vr2->vuse = vr1.vuse;
6203 : /* As we are not walking the virtual operand chain we know the
6204 : shared_lookup_references are still original so we can re-use
6205 : them here. */
6206 8126779 : vr2->operands = vr1.operands.copy ();
6207 8126779 : vr2->type = vr1.type;
6208 8126779 : vr2->punned = vr1.punned;
6209 8126779 : vr2->set = vr1.set;
6210 8126779 : vr2->offset = vr1.offset;
6211 8126779 : vr2->max_size = vr1.max_size;
6212 8126779 : vr2->base_set = vr1.base_set;
6213 8126779 : vr2->hashcode = vr1.hashcode;
6214 8126779 : vr2->result = lhs;
6215 8126779 : vr2->result_vdef = vdef_val;
6216 8126779 : vr2->value_id = 0;
6217 8126779 : slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
6218 : INSERT);
6219 8126779 : gcc_assert (!*slot);
6220 8126779 : *slot = vr2;
6221 8126779 : vr2->next = last_inserted_ref;
6222 8126779 : last_inserted_ref = vr2;
6223 : }
6224 :
6225 8817270 : return changed;
6226 : }
6227 :
6228 : /* Visit a load from a reference operator RHS, part of STMT, value number it,
6229 : and return true if the value number of the LHS has changed as a result. */
6230 :
6231 : static bool
6232 35382410 : visit_reference_op_load (tree lhs, tree op, gimple *stmt)
6233 : {
6234 35382410 : bool changed = false;
6235 35382410 : tree result;
6236 35382410 : vn_reference_t res;
6237 :
6238 35382410 : tree vuse = gimple_vuse (stmt);
6239 35382410 : tree last_vuse = vuse;
6240 35382410 : result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
6241 :
6242 : /* We handle type-punning through unions by value-numbering based
6243 : on offset and size of the access. Be prepared to handle a
6244 : type-mismatch here via creating a VIEW_CONVERT_EXPR. */
6245 35382410 : if (result
6246 35382410 : && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
6247 : {
6248 18678 : if (CONSTANT_CLASS_P (result))
6249 4263 : result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6250 : else
6251 : {
6252 : /* We will be setting the value number of lhs to the value number
6253 : of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
6254 : So first simplify and lookup this expression to see if it
6255 : is already available. */
6256 14415 : gimple_match_op res_op (gimple_match_cond::UNCOND,
6257 14415 : VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6258 14415 : result = vn_nary_build_or_lookup (&res_op);
6259 14415 : if (result
6260 14409 : && TREE_CODE (result) == SSA_NAME
6261 27165 : && VN_INFO (result)->needs_insertion)
6262 : /* Track whether this is the canonical expression for different
6263 : typed loads. We use that as a stopgap measure for code
6264 : hoisting when dealing with floating point loads. */
6265 11480 : res->punned = true;
6266 : }
6267 :
6268 : /* When building the conversion fails avoid inserting the reference
6269 : again. */
6270 18678 : if (!result)
6271 6 : return set_ssa_val_to (lhs, lhs);
6272 : }
6273 :
6274 35363732 : if (result)
6275 5733618 : changed = set_ssa_val_to (lhs, result);
6276 : else
6277 : {
6278 29648786 : changed = set_ssa_val_to (lhs, lhs);
6279 29648786 : vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
6280 29648786 : if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
6281 : {
6282 9184343 : if (dump_file && (dump_flags & TDF_DETAILS))
6283 : {
6284 23265 : fprintf (dump_file, "Using extra use virtual operand ");
6285 23265 : print_generic_expr (dump_file, last_vuse);
6286 23265 : fprintf (dump_file, "\n");
6287 : }
6288 9184343 : vn_reference_insert (op, lhs, vuse, NULL_TREE);
6289 : }
6290 : }
6291 :
6292 : return changed;
6293 : }
6294 :
6295 :
6296 : /* Visit a store to a reference operator LHS, part of STMT, value number it,
6297 : and return true if the value number of the LHS has changed as a result. */
6298 :
6299 : static bool
6300 33749054 : visit_reference_op_store (tree lhs, tree op, gimple *stmt)
6301 : {
6302 33749054 : bool changed = false;
6303 33749054 : vn_reference_t vnresult = NULL;
6304 33749054 : tree assign;
6305 33749054 : bool resultsame = false;
6306 33749054 : tree vuse = gimple_vuse (stmt);
6307 33749054 : tree vdef = gimple_vdef (stmt);
6308 :
6309 33749054 : if (TREE_CODE (op) == SSA_NAME)
6310 15336217 : op = SSA_VAL (op);
6311 :
6312 : /* First we want to lookup using the *vuses* from the store and see
6313 : if there the last store to this location with the same address
6314 : had the same value.
6315 :
6316 : The vuses represent the memory state before the store. If the
6317 : memory state, address, and value of the store is the same as the
6318 : last store to this location, then this store will produce the
6319 : same memory state as that store.
6320 :
6321 : In this case the vdef versions for this store are value numbered to those
6322 : vuse versions, since they represent the same memory state after
6323 : this store.
6324 :
6325 : Otherwise, the vdefs for the store are used when inserting into
6326 : the table, since the store generates a new memory state. */
6327 :
6328 33749054 : vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
6329 33749054 : if (vnresult
6330 1691372 : && vnresult->result)
6331 : {
6332 1691372 : tree result = vnresult->result;
6333 1691372 : gcc_checking_assert (TREE_CODE (result) != SSA_NAME
6334 : || result == SSA_VAL (result));
6335 1691372 : resultsame = expressions_equal_p (result, op);
6336 1691372 : if (resultsame)
6337 : {
6338 : /* If the TBAA state isn't compatible for downstream reads
6339 : we cannot value-number the VDEFs the same. */
6340 53868 : ao_ref lhs_ref;
6341 53868 : ao_ref_init (&lhs_ref, lhs);
6342 53868 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
6343 53868 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
6344 53868 : if ((vnresult->set != set
6345 922 : && ! alias_set_subset_of (set, vnresult->set))
6346 54475 : || (vnresult->base_set != base_set
6347 8235 : && ! alias_set_subset_of (base_set, vnresult->base_set)))
6348 2725 : resultsame = false;
6349 : }
6350 : }
6351 :
6352 2725 : if (!resultsame)
6353 : {
6354 33697911 : if (dump_file && (dump_flags & TDF_DETAILS))
6355 : {
6356 20510 : fprintf (dump_file, "No store match\n");
6357 20510 : fprintf (dump_file, "Value numbering store ");
6358 20510 : print_generic_expr (dump_file, lhs);
6359 20510 : fprintf (dump_file, " to ");
6360 20510 : print_generic_expr (dump_file, op);
6361 20510 : fprintf (dump_file, "\n");
6362 : }
6363 : /* Have to set value numbers before insert, since insert is
6364 : going to valueize the references in-place. */
6365 33697911 : if (vdef)
6366 33697911 : changed |= set_ssa_val_to (vdef, vdef);
6367 :
6368 : /* Do not insert structure copies into the tables. */
6369 33697911 : if (is_gimple_min_invariant (op)
6370 33697911 : || is_gimple_reg (op))
6371 30028387 : vn_reference_insert (lhs, op, vdef, NULL);
6372 :
6373 : /* Only perform the following when being called from PRE
6374 : which embeds tail merging. */
6375 33697911 : if (default_vn_walk_kind == VN_WALK)
6376 : {
6377 7634786 : assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
6378 7634786 : vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
6379 7634786 : if (!vnresult)
6380 7592950 : vn_reference_insert (assign, lhs, vuse, vdef);
6381 : }
6382 : }
6383 : else
6384 : {
6385 : /* We had a match, so value number the vdef to have the value
6386 : number of the vuse it came from. */
6387 :
6388 51143 : if (dump_file && (dump_flags & TDF_DETAILS))
6389 9 : fprintf (dump_file, "Store matched earlier value, "
6390 : "value numbering store vdefs to matching vuses.\n");
6391 :
6392 51143 : changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
6393 : }
6394 :
6395 33749054 : return changed;
6396 : }
6397 :
6398 : /* Visit and value number PHI, return true if the value number
6399 : changed. When BACKEDGES_VARYING_P is true then assume all
6400 : backedge values are varying. When INSERTED is not NULL then
6401 : this is just a ahead query for a possible iteration, set INSERTED
6402 : to true if we'd insert into the hashtable. */
6403 :
6404 : static bool
6405 34766142 : visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
6406 : {
6407 34766142 : tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
6408 34766142 : bool seen_undef_visited = false;
6409 34766142 : tree backedge_val = NULL_TREE;
6410 34766142 : bool seen_non_backedge = false;
6411 34766142 : tree sameval_base = NULL_TREE;
6412 34766142 : poly_int64 soff, doff;
6413 34766142 : unsigned n_executable = 0;
6414 34766142 : edge sameval_e = NULL;
6415 :
6416 : /* TODO: We could check for this in initialization, and replace this
6417 : with a gcc_assert. */
6418 34766142 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
6419 30862 : return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
6420 :
6421 : /* We track whether a PHI was CSEd to avoid excessive iterations
6422 : that would be necessary only because the PHI changed arguments
6423 : but not value. */
6424 34735280 : if (!inserted)
6425 27162624 : gimple_set_plf (phi, GF_PLF_1, false);
6426 :
6427 34735280 : basic_block bb = gimple_bb (phi);
6428 :
6429 : /* For the equivalence handling below make sure to first process an
6430 : edge with a non-constant. */
6431 34735280 : auto_vec<edge, 2> preds;
6432 69470560 : preds.reserve_exact (EDGE_COUNT (bb->preds));
6433 34735280 : bool seen_nonconstant = false;
6434 149655824 : for (unsigned i = 0; i < EDGE_COUNT (bb->preds); ++i)
6435 : {
6436 80185264 : edge e = EDGE_PRED (bb, i);
6437 80185264 : preds.quick_push (e);
6438 80185264 : if (!seen_nonconstant)
6439 : {
6440 42521109 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6441 42521109 : if (TREE_CODE (def) == SSA_NAME)
6442 : {
6443 32979149 : seen_nonconstant = true;
6444 32979149 : if (i != 0)
6445 5773631 : std::swap (preds[0], preds[i]);
6446 : }
6447 : }
6448 : }
6449 :
6450 : /* See if all non-TOP arguments have the same value. TOP is
6451 : equivalent to everything, so we can ignore it. */
6452 146220235 : for (edge e : preds)
6453 68969332 : if (e->flags & EDGE_EXECUTABLE)
6454 : {
6455 63913642 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6456 :
6457 63913642 : if (def == PHI_RESULT (phi))
6458 336117 : continue;
6459 63602900 : ++n_executable;
6460 63602900 : bool visited = true;
6461 63602900 : if (TREE_CODE (def) == SSA_NAME)
6462 : {
6463 51337618 : tree val = SSA_VAL (def, &visited);
6464 51337618 : if (SSA_NAME_IS_DEFAULT_DEF (def))
6465 2718975 : visited = true;
6466 51337618 : if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6467 48766887 : def = val;
6468 51337618 : if (e->flags & EDGE_DFS_BACK)
6469 15483735 : backedge_val = def;
6470 : }
6471 63602900 : if (!(e->flags & EDGE_DFS_BACK))
6472 47976594 : seen_non_backedge = true;
6473 63602900 : if (def == VN_TOP)
6474 : ;
6475 : /* Ignore undefined defs for sameval but record one. */
6476 63602900 : else if (TREE_CODE (def) == SSA_NAME
6477 47890772 : && ! virtual_operand_p (def)
6478 87873060 : && ssa_undefined_value_p (def, false))
6479 : {
6480 236879 : if (!seen_undef
6481 : /* Avoid having not visited undefined defs if we also have
6482 : a visited one. */
6483 35484 : || (!seen_undef_visited && visited))
6484 : {
6485 201399 : seen_undef = def;
6486 201399 : seen_undef_visited = visited;
6487 : }
6488 : }
6489 63366021 : else if (sameval == VN_TOP)
6490 : {
6491 : sameval = def;
6492 : sameval_e = e;
6493 : }
6494 28678611 : else if (expressions_equal_p (def, sameval))
6495 : sameval_e = NULL;
6496 45091481 : else if (virtual_operand_p (def))
6497 : {
6498 : sameval = NULL_TREE;
6499 26954937 : break;
6500 : }
6501 : else
6502 : {
6503 : /* We know we're arriving only with invariant addresses here,
6504 : try harder comparing them. We can do some caching here
6505 : which we cannot do in expressions_equal_p. */
6506 16796488 : if (TREE_CODE (def) == ADDR_EXPR
6507 393091 : && TREE_CODE (sameval) == ADDR_EXPR
6508 108416 : && sameval_base != (void *)-1)
6509 : {
6510 108416 : if (!sameval_base)
6511 108414 : sameval_base = get_addr_base_and_unit_offset
6512 108414 : (TREE_OPERAND (sameval, 0), &soff);
6513 108414 : if (!sameval_base)
6514 : sameval_base = (tree)(void *)-1;
6515 108421 : else if ((get_addr_base_and_unit_offset
6516 108416 : (TREE_OPERAND (def, 0), &doff) == sameval_base)
6517 108416 : && known_eq (soff, doff))
6518 5 : continue;
6519 : }
6520 : /* There's also the possibility to use equivalences. */
6521 32499897 : if (!FLOAT_TYPE_P (TREE_TYPE (def))
6522 : /* But only do this if we didn't force any of sameval or
6523 : val to VARYING because of backedge processing rules. */
6524 15597616 : && (TREE_CODE (sameval) != SSA_NAME
6525 12270908 : || SSA_VAL (sameval) == sameval)
6526 32394038 : && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6527 : {
6528 15597543 : vn_nary_op_t vnresult;
6529 15597543 : tree ops[2];
6530 15597543 : ops[0] = def;
6531 15597543 : ops[1] = sameval;
6532 : /* Canonicalize the operands order for eq below. */
6533 15597543 : if (tree_swap_operands_p (ops[0], ops[1]))
6534 9334369 : std::swap (ops[0], ops[1]);
6535 15597543 : tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6536 : boolean_type_node,
6537 : ops, &vnresult);
6538 15597543 : if (! val && vnresult && vnresult->predicated_values)
6539 : {
6540 202423 : val = vn_nary_op_get_predicated_value (vnresult, e);
6541 110782 : if (val && integer_truep (val)
6542 227913 : && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6543 : {
6544 25370 : if (dump_file && (dump_flags & TDF_DETAILS))
6545 : {
6546 2 : fprintf (dump_file, "Predication says ");
6547 2 : print_generic_expr (dump_file, def, TDF_NONE);
6548 2 : fprintf (dump_file, " and ");
6549 2 : print_generic_expr (dump_file, sameval, TDF_NONE);
6550 2 : fprintf (dump_file, " are equal on edge %d -> %d\n",
6551 2 : e->src->index, e->dest->index);
6552 : }
6553 25370 : continue;
6554 : }
6555 : }
6556 : }
6557 : sameval = NULL_TREE;
6558 : break;
6559 : }
6560 : }
6561 :
6562 : /* If the value we want to use is flowing over the backedge and we
6563 : should take it as VARYING but it has a non-VARYING value drop to
6564 : VARYING.
6565 : If we value-number a virtual operand never value-number to the
6566 : value from the backedge as that confuses the alias-walking code.
6567 : See gcc.dg/torture/pr87176.c. If the value is the same on a
6568 : non-backedge everything is OK though. */
6569 34735280 : bool visited_p;
6570 34735280 : if ((backedge_val
6571 34735280 : && !seen_non_backedge
6572 1859 : && TREE_CODE (backedge_val) == SSA_NAME
6573 1592 : && sameval == backedge_val
6574 313 : && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6575 40 : || SSA_VAL (backedge_val) != backedge_val))
6576 : /* Do not value-number a virtual operand to sth not visited though
6577 : given that allows us to escape a region in alias walking. */
6578 34736866 : || (sameval
6579 7780070 : && TREE_CODE (sameval) == SSA_NAME
6580 4613872 : && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6581 3894787 : && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6582 1968240 : && (SSA_VAL (sameval, &visited_p), !visited_p)))
6583 : /* Note this just drops to VARYING without inserting the PHI into
6584 : the hashes. */
6585 301132 : result = PHI_RESULT (phi);
6586 : /* If none of the edges was executable keep the value-number at VN_TOP,
6587 : if only a single edge is executable use its value. */
6588 34434148 : else if (n_executable <= 1)
6589 6703594 : result = seen_undef ? seen_undef : sameval;
6590 : /* If we saw only undefined values and VN_TOP use one of the
6591 : undefined values. */
6592 27730554 : else if (sameval == VN_TOP)
6593 5643 : result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6594 : /* First see if it is equivalent to a phi node in this block. We prefer
6595 : this as it allows IV elimination - see PRs 66502 and 67167. */
6596 27724911 : else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6597 : {
6598 4151914 : if (!inserted
6599 70553 : && TREE_CODE (result) == SSA_NAME
6600 4222467 : && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6601 : {
6602 70553 : gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6603 70553 : if (dump_file && (dump_flags & TDF_DETAILS))
6604 : {
6605 6 : fprintf (dump_file, "Marking CSEd to PHI node ");
6606 6 : print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6607 : 0, TDF_SLIM);
6608 6 : fprintf (dump_file, "\n");
6609 : }
6610 : }
6611 : }
6612 : /* If all values are the same use that, unless we've seen undefined
6613 : values as well and the value isn't constant.
6614 : CCP/copyprop have the same restriction to not remove uninit warnings. */
6615 23572997 : else if (sameval
6616 23572997 : && (! seen_undef || is_gimple_min_invariant (sameval)))
6617 : result = sameval;
6618 : else
6619 : {
6620 22946307 : result = PHI_RESULT (phi);
6621 : /* Only insert PHIs that are varying, for constant value numbers
6622 : we mess up equivalences otherwise as we are only comparing
6623 : the immediate controlling predicates. */
6624 22946307 : vn_phi_insert (phi, result, backedges_varying_p);
6625 22946307 : if (inserted)
6626 3333753 : *inserted = true;
6627 : }
6628 :
6629 34735280 : return set_ssa_val_to (PHI_RESULT (phi), result);
6630 34735280 : }
6631 :
6632 : /* Try to simplify RHS using equivalences and constant folding. */
6633 :
6634 : static tree
6635 130075609 : try_to_simplify (gassign *stmt)
6636 : {
6637 130075609 : enum tree_code code = gimple_assign_rhs_code (stmt);
6638 130075609 : tree tem;
6639 :
6640 : /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6641 : in this case, there is no point in doing extra work. */
6642 130075609 : if (code == SSA_NAME)
6643 : return NULL_TREE;
6644 :
6645 : /* First try constant folding based on our current lattice. */
6646 114739064 : mprts_hook = vn_lookup_simplify_result;
6647 114739064 : tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6648 114739064 : mprts_hook = NULL;
6649 114739064 : if (tem
6650 114739064 : && (TREE_CODE (tem) == SSA_NAME
6651 25549959 : || is_gimple_min_invariant (tem)))
6652 25597953 : return tem;
6653 :
6654 : return NULL_TREE;
6655 : }
6656 :
6657 : /* Visit and value number STMT, return true if the value number
6658 : changed. */
6659 :
6660 : static bool
6661 481509808 : visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6662 : {
6663 481509808 : bool changed = false;
6664 :
6665 481509808 : if (dump_file && (dump_flags & TDF_DETAILS))
6666 : {
6667 414731 : fprintf (dump_file, "Value numbering stmt = ");
6668 414731 : print_gimple_stmt (dump_file, stmt, 0);
6669 : }
6670 :
6671 481509808 : if (gimple_code (stmt) == GIMPLE_PHI)
6672 27183769 : changed = visit_phi (stmt, NULL, backedges_varying_p);
6673 630048339 : else if (gimple_has_volatile_ops (stmt))
6674 9251507 : changed = defs_to_varying (stmt);
6675 445074532 : else if (gassign *ass = dyn_cast <gassign *> (stmt))
6676 : {
6677 135240520 : enum tree_code code = gimple_assign_rhs_code (ass);
6678 135240520 : tree lhs = gimple_assign_lhs (ass);
6679 135240520 : tree rhs1 = gimple_assign_rhs1 (ass);
6680 135240520 : tree simplified;
6681 :
6682 : /* Shortcut for copies. Simplifying copies is pointless,
6683 : since we copy the expression and value they represent. */
6684 135240520 : if (code == SSA_NAME
6685 20501456 : && TREE_CODE (lhs) == SSA_NAME)
6686 : {
6687 5164911 : changed = visit_copy (lhs, rhs1);
6688 5164911 : goto done;
6689 : }
6690 130075609 : simplified = try_to_simplify (ass);
6691 130075609 : if (simplified)
6692 : {
6693 25597953 : if (dump_file && (dump_flags & TDF_DETAILS))
6694 : {
6695 14816 : fprintf (dump_file, "RHS ");
6696 14816 : print_gimple_expr (dump_file, ass, 0);
6697 14816 : fprintf (dump_file, " simplified to ");
6698 14816 : print_generic_expr (dump_file, simplified);
6699 14816 : fprintf (dump_file, "\n");
6700 : }
6701 : }
6702 : /* Setting value numbers to constants will occasionally
6703 : screw up phi congruence because constants are not
6704 : uniquely associated with a single ssa name that can be
6705 : looked up. */
6706 25597953 : if (simplified
6707 25597953 : && is_gimple_min_invariant (simplified)
6708 22558972 : && TREE_CODE (lhs) == SSA_NAME)
6709 : {
6710 7809800 : changed = set_ssa_val_to (lhs, simplified);
6711 7809800 : goto done;
6712 : }
6713 122265809 : else if (simplified
6714 17788153 : && TREE_CODE (simplified) == SSA_NAME
6715 3038981 : && TREE_CODE (lhs) == SSA_NAME)
6716 : {
6717 3038981 : changed = visit_copy (lhs, simplified);
6718 3038981 : goto done;
6719 : }
6720 :
6721 119226828 : if ((TREE_CODE (lhs) == SSA_NAME
6722 : /* We can substitute SSA_NAMEs that are live over
6723 : abnormal edges with their constant value. */
6724 85477472 : && !(gimple_assign_copy_p (ass)
6725 26 : && is_gimple_min_invariant (rhs1))
6726 85477446 : && !(simplified
6727 0 : && is_gimple_min_invariant (simplified))
6728 85477446 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6729 : /* Stores or copies from SSA_NAMEs that are live over
6730 : abnormal edges are a problem. */
6731 204702961 : || (code == SSA_NAME
6732 15336545 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6733 1641 : changed = defs_to_varying (ass);
6734 119225187 : else if (REFERENCE_CLASS_P (lhs)
6735 119225187 : || DECL_P (lhs))
6736 33749054 : changed = visit_reference_op_store (lhs, rhs1, ass);
6737 85476133 : else if (TREE_CODE (lhs) == SSA_NAME)
6738 : {
6739 85476133 : if ((gimple_assign_copy_p (ass)
6740 26 : && is_gimple_min_invariant (rhs1))
6741 85476159 : || (simplified
6742 0 : && is_gimple_min_invariant (simplified)))
6743 : {
6744 0 : if (simplified)
6745 0 : changed = set_ssa_val_to (lhs, simplified);
6746 : else
6747 0 : changed = set_ssa_val_to (lhs, rhs1);
6748 : }
6749 : else
6750 : {
6751 : /* Visit the original statement. */
6752 85476133 : switch (vn_get_stmt_kind (ass))
6753 : {
6754 49984835 : case VN_NARY:
6755 49984835 : changed = visit_nary_op (lhs, ass);
6756 49984835 : break;
6757 35382410 : case VN_REFERENCE:
6758 35382410 : changed = visit_reference_op_load (lhs, rhs1, ass);
6759 35382410 : break;
6760 108888 : default:
6761 108888 : changed = defs_to_varying (ass);
6762 108888 : break;
6763 : }
6764 : }
6765 : }
6766 : else
6767 0 : changed = defs_to_varying (ass);
6768 : }
6769 309834012 : else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6770 : {
6771 25410598 : tree lhs = gimple_call_lhs (call_stmt);
6772 25410598 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6773 : {
6774 : /* Try constant folding based on our current lattice. */
6775 8531150 : tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6776 : vn_valueize);
6777 8531150 : if (simplified)
6778 : {
6779 68120 : if (dump_file && (dump_flags & TDF_DETAILS))
6780 : {
6781 1 : fprintf (dump_file, "call ");
6782 1 : print_gimple_expr (dump_file, call_stmt, 0);
6783 1 : fprintf (dump_file, " simplified to ");
6784 1 : print_generic_expr (dump_file, simplified);
6785 1 : fprintf (dump_file, "\n");
6786 : }
6787 : }
6788 : /* Setting value numbers to constants will occasionally
6789 : screw up phi congruence because constants are not
6790 : uniquely associated with a single ssa name that can be
6791 : looked up. */
6792 68120 : if (simplified
6793 68120 : && is_gimple_min_invariant (simplified))
6794 : {
6795 61606 : changed = set_ssa_val_to (lhs, simplified);
6796 123212 : if (gimple_vdef (call_stmt))
6797 751 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6798 : SSA_VAL (gimple_vuse (call_stmt)));
6799 61606 : goto done;
6800 : }
6801 8469544 : else if (simplified
6802 6514 : && TREE_CODE (simplified) == SSA_NAME)
6803 : {
6804 309 : changed = visit_copy (lhs, simplified);
6805 618 : if (gimple_vdef (call_stmt))
6806 0 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6807 : SSA_VAL (gimple_vuse (call_stmt)));
6808 309 : goto done;
6809 : }
6810 8469235 : else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6811 : {
6812 414 : changed = defs_to_varying (call_stmt);
6813 414 : goto done;
6814 : }
6815 : }
6816 :
6817 : /* Pick up flags from a devirtualization target. */
6818 25348269 : tree fn = gimple_call_fn (stmt);
6819 25348269 : int extra_fnflags = 0;
6820 25348269 : if (fn && TREE_CODE (fn) == SSA_NAME)
6821 : {
6822 540929 : fn = SSA_VAL (fn);
6823 540929 : if (TREE_CODE (fn) == ADDR_EXPR
6824 540929 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6825 5363 : extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6826 : }
6827 25348269 : if ((/* Calls to the same function with the same vuse
6828 : and the same operands do not necessarily return the same
6829 : value, unless they're pure or const. */
6830 25348269 : ((gimple_call_flags (call_stmt) | extra_fnflags)
6831 25348269 : & (ECF_PURE | ECF_CONST))
6832 : /* If calls have a vdef, subsequent calls won't have
6833 : the same incoming vuse. So, if 2 calls with vdef have the
6834 : same vuse, we know they're not subsequent.
6835 : We can value number 2 calls to the same function with the
6836 : same vuse and the same operands which are not subsequent
6837 : the same, because there is no code in the program that can
6838 : compare the 2 values... */
6839 21315914 : || (gimple_vdef (call_stmt)
6840 : /* ... unless the call returns a pointer which does
6841 : not alias with anything else. In which case the
6842 : information that the values are distinct are encoded
6843 : in the IL. */
6844 21281101 : && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6845 : /* Only perform the following when being called from PRE
6846 : which embeds tail merging. */
6847 20710173 : && default_vn_walk_kind == VN_WALK))
6848 : /* Do not process .DEFERRED_INIT since that confuses uninit
6849 : analysis. */
6850 30408074 : && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6851 8817270 : changed = visit_reference_op_call (lhs, call_stmt);
6852 : else
6853 16530999 : changed = defs_to_varying (call_stmt);
6854 : }
6855 : else
6856 284423414 : changed = defs_to_varying (stmt);
6857 481509808 : done:
6858 481509808 : return changed;
6859 : }
6860 :
6861 :
6862 : /* Allocate a value number table. */
6863 :
6864 : static void
6865 6318405 : allocate_vn_table (vn_tables_t table, unsigned size)
6866 : {
6867 6318405 : table->phis = new vn_phi_table_type (size);
6868 6318405 : table->nary = new vn_nary_op_table_type (size);
6869 6318405 : table->references = new vn_reference_table_type (size);
6870 6318405 : }
6871 :
6872 : /* Free a value number table. */
6873 :
6874 : static void
6875 6318405 : free_vn_table (vn_tables_t table)
6876 : {
6877 : /* Walk over elements and release vectors. */
6878 6318405 : vn_reference_iterator_type hir;
6879 6318405 : vn_reference_t vr;
6880 78390330 : FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
6881 72071925 : vr->operands.release ();
6882 6318405 : delete table->phis;
6883 6318405 : table->phis = NULL;
6884 6318405 : delete table->nary;
6885 6318405 : table->nary = NULL;
6886 6318405 : delete table->references;
6887 6318405 : table->references = NULL;
6888 6318405 : }
6889 :
6890 : /* Set *ID according to RESULT. */
6891 :
6892 : static void
6893 35278721 : set_value_id_for_result (tree result, unsigned int *id)
6894 : {
6895 35278721 : if (result && TREE_CODE (result) == SSA_NAME)
6896 21963934 : *id = VN_INFO (result)->value_id;
6897 9964603 : else if (result && is_gimple_min_invariant (result))
6898 3769307 : *id = get_or_alloc_constant_value_id (result);
6899 : else
6900 9545480 : *id = get_next_value_id ();
6901 35278721 : }
6902 :
6903 : /* Set the value ids in the valid hash tables. */
6904 :
6905 : static void
6906 983312 : set_hashtable_value_ids (void)
6907 : {
6908 983312 : vn_nary_op_iterator_type hin;
6909 983312 : vn_phi_iterator_type hip;
6910 983312 : vn_reference_iterator_type hir;
6911 983312 : vn_nary_op_t vno;
6912 983312 : vn_reference_t vr;
6913 983312 : vn_phi_t vp;
6914 :
6915 : /* Now set the value ids of the things we had put in the hash
6916 : table. */
6917 :
6918 25336723 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
6919 24353411 : if (! vno->predicated_values)
6920 7957095 : set_value_id_for_result (vno->u.result, &vno->value_id);
6921 :
6922 5071120 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
6923 4087808 : set_value_id_for_result (vp->result, &vp->value_id);
6924 :
6925 24217130 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
6926 : hir)
6927 23233818 : set_value_id_for_result (vr->result, &vr->value_id);
6928 983312 : }
6929 :
6930 : /* Return the maximum value id we have ever seen. */
6931 :
6932 : unsigned int
6933 1966624 : get_max_value_id (void)
6934 : {
6935 1966624 : return next_value_id;
6936 : }
6937 :
6938 : /* Return the maximum constant value id we have ever seen. */
6939 :
6940 : unsigned int
6941 1966624 : get_max_constant_value_id (void)
6942 : {
6943 1966624 : return -next_constant_value_id;
6944 : }
6945 :
6946 : /* Return the next unique value id. */
6947 :
6948 : unsigned int
6949 50191218 : get_next_value_id (void)
6950 : {
6951 50191218 : gcc_checking_assert ((int)next_value_id > 0);
6952 50191218 : return next_value_id++;
6953 : }
6954 :
6955 : /* Return the next unique value id for constants. */
6956 :
6957 : unsigned int
6958 2580454 : get_next_constant_value_id (void)
6959 : {
6960 2580454 : gcc_checking_assert (next_constant_value_id < 0);
6961 2580454 : return next_constant_value_id--;
6962 : }
6963 :
6964 :
6965 : /* Compare two expressions E1 and E2 and return true if they are equal.
6966 : If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6967 : otherwise VN_TOP only matches VN_TOP. */
6968 :
6969 : bool
6970 251550136 : expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
6971 : {
6972 : /* The obvious case. */
6973 251550136 : if (e1 == e2)
6974 : return true;
6975 :
6976 : /* If either one is VN_TOP consider them equal. */
6977 71447824 : if (match_vn_top_optimistically
6978 66499374 : && (e1 == VN_TOP || e2 == VN_TOP))
6979 : return true;
6980 :
6981 : /* If only one of them is null, they cannot be equal. While in general
6982 : this should not happen for operations like TARGET_MEM_REF some
6983 : operands are optional and an identity value we could substitute
6984 : has differing semantics. */
6985 71447824 : if (!e1 || !e2)
6986 : return false;
6987 :
6988 : /* SSA_NAME compare pointer equal. */
6989 71447824 : if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
6990 : return false;
6991 :
6992 : /* Now perform the actual comparison. */
6993 35892479 : if (TREE_CODE (e1) == TREE_CODE (e2)
6994 35892479 : && operand_equal_p (e1, e2, OEP_PURE_SAME))
6995 : return true;
6996 :
6997 : return false;
6998 : }
6999 :
7000 :
7001 : /* Return true if the nary operation NARY may trap. This is a copy
7002 : of stmt_could_throw_1_p adjusted to the SCCVN IL. */
7003 :
7004 : bool
7005 5724140 : vn_nary_may_trap (vn_nary_op_t nary)
7006 : {
7007 5724140 : tree type;
7008 5724140 : tree rhs2 = NULL_TREE;
7009 5724140 : bool honor_nans = false;
7010 5724140 : bool honor_snans = false;
7011 5724140 : bool fp_operation = false;
7012 5724140 : bool honor_trapv = false;
7013 5724140 : bool handled, ret;
7014 5724140 : unsigned i;
7015 :
7016 5724140 : if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
7017 : || TREE_CODE_CLASS (nary->opcode) == tcc_unary
7018 5724140 : || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
7019 : {
7020 5603403 : type = nary->type;
7021 5603403 : fp_operation = FLOAT_TYPE_P (type);
7022 5482496 : if (fp_operation)
7023 : {
7024 120907 : honor_nans = flag_trapping_math && !flag_finite_math_only;
7025 120907 : honor_snans = flag_signaling_nans != 0;
7026 : }
7027 5482496 : else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
7028 : honor_trapv = true;
7029 : }
7030 5724140 : if (nary->length >= 2)
7031 2306515 : rhs2 = nary->op[1];
7032 5724140 : ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
7033 : honor_trapv, honor_nans, honor_snans,
7034 : rhs2, &handled);
7035 5724140 : if (handled && ret)
7036 : return true;
7037 :
7038 13446624 : for (i = 0; i < nary->length; ++i)
7039 7842672 : if (tree_could_trap_p (nary->op[i]))
7040 : return true;
7041 :
7042 : return false;
7043 : }
7044 :
7045 : /* Return true if the reference operation REF may trap. */
7046 :
7047 : bool
7048 944830 : vn_reference_may_trap (vn_reference_t ref)
7049 : {
7050 944830 : switch (ref->operands[0].opcode)
7051 : {
7052 : case MODIFY_EXPR:
7053 : case CALL_EXPR:
7054 : /* We do not handle calls. */
7055 : return true;
7056 : case ADDR_EXPR:
7057 : /* And toplevel address computations never trap. */
7058 : return false;
7059 : default:;
7060 : }
7061 :
7062 : vn_reference_op_t op;
7063 : unsigned i;
7064 2604614 : FOR_EACH_VEC_ELT (ref->operands, i, op)
7065 : {
7066 2604359 : switch (op->opcode)
7067 : {
7068 : case WITH_SIZE_EXPR:
7069 : case TARGET_MEM_REF:
7070 : /* Always variable. */
7071 : return true;
7072 730037 : case COMPONENT_REF:
7073 730037 : if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
7074 : return true;
7075 : break;
7076 0 : case ARRAY_RANGE_REF:
7077 0 : if (TREE_CODE (op->op0) == SSA_NAME)
7078 : return true;
7079 : break;
7080 205936 : case ARRAY_REF:
7081 205936 : {
7082 205936 : if (TREE_CODE (op->op0) != INTEGER_CST)
7083 : return true;
7084 :
7085 : /* !in_array_bounds */
7086 185216 : tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
7087 185216 : if (!domain_type)
7088 : return true;
7089 :
7090 185170 : tree min = op->op1;
7091 185170 : tree max = TYPE_MAX_VALUE (domain_type);
7092 185170 : if (!min
7093 185170 : || !max
7094 172031 : || TREE_CODE (min) != INTEGER_CST
7095 172031 : || TREE_CODE (max) != INTEGER_CST)
7096 : return true;
7097 :
7098 169335 : if (tree_int_cst_lt (op->op0, min)
7099 169335 : || tree_int_cst_lt (max, op->op0))
7100 : return true;
7101 :
7102 : break;
7103 : }
7104 : case MEM_REF:
7105 : /* Nothing interesting in itself, the base is separate. */
7106 : break;
7107 : /* The following are the address bases. */
7108 : case SSA_NAME:
7109 : return true;
7110 535680 : case ADDR_EXPR:
7111 535680 : if (op->op0)
7112 535680 : return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
7113 : return false;
7114 1745936 : default:;
7115 : }
7116 : }
7117 : return false;
7118 : }
7119 :
7120 10721496 : eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
7121 : bitmap inserted_exprs_)
7122 10721496 : : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
7123 10721496 : el_todo (0), eliminations (0), insertions (0),
7124 10721496 : inserted_exprs (inserted_exprs_)
7125 : {
7126 10721496 : need_eh_cleanup = BITMAP_ALLOC (NULL);
7127 10721496 : need_ab_cleanup = BITMAP_ALLOC (NULL);
7128 10721496 : }
7129 :
7130 10721496 : eliminate_dom_walker::~eliminate_dom_walker ()
7131 : {
7132 10721496 : BITMAP_FREE (need_eh_cleanup);
7133 10721496 : BITMAP_FREE (need_ab_cleanup);
7134 10721496 : }
7135 :
7136 : /* Return a leader for OP that is available at the current point of the
7137 : eliminate domwalk. */
7138 :
7139 : tree
7140 186995667 : eliminate_dom_walker::eliminate_avail (basic_block, tree op)
7141 : {
7142 186995667 : tree valnum = VN_INFO (op)->valnum;
7143 186995667 : if (TREE_CODE (valnum) == SSA_NAME)
7144 : {
7145 181786829 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
7146 : return valnum;
7147 316341351 : if (avail.length () > SSA_NAME_VERSION (valnum))
7148 : {
7149 142549097 : tree av = avail[SSA_NAME_VERSION (valnum)];
7150 : /* When PRE discovers a new redundancy there's no way to unite
7151 : the value classes so it instead inserts a copy old-val = new-val.
7152 : Look through such copies here, providing one more level of
7153 : simplification at elimination time. */
7154 142549097 : gassign *ass;
7155 250759228 : if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
7156 76980649 : if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
7157 : {
7158 40823840 : tree rhs1 = gimple_assign_rhs1 (ass);
7159 40823840 : if (CONSTANT_CLASS_P (rhs1)
7160 40823840 : || (TREE_CODE (rhs1) == SSA_NAME
7161 34781 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
7162 : av = rhs1;
7163 : }
7164 : return av;
7165 : }
7166 : }
7167 5208838 : else if (is_gimple_min_invariant (valnum))
7168 5208838 : return valnum;
7169 : return NULL_TREE;
7170 : }
7171 :
7172 : /* At the current point of the eliminate domwalk make OP available. */
7173 :
7174 : void
7175 51341315 : eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
7176 : {
7177 51341315 : tree valnum = VN_INFO (op)->valnum;
7178 51341315 : if (TREE_CODE (valnum) == SSA_NAME)
7179 : {
7180 99208533 : if (avail.length () <= SSA_NAME_VERSION (valnum))
7181 17371561 : avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
7182 51341315 : tree pushop = op;
7183 51341315 : if (avail[SSA_NAME_VERSION (valnum)])
7184 45692 : pushop = avail[SSA_NAME_VERSION (valnum)];
7185 51341315 : avail_stack.safe_push (pushop);
7186 51341315 : avail[SSA_NAME_VERSION (valnum)] = op;
7187 : }
7188 51341315 : }
7189 :
7190 : /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
7191 : the leader for the expression if insertion was successful. */
7192 :
7193 : tree
7194 139267 : eliminate_dom_walker::eliminate_insert (basic_block bb,
7195 : gimple_stmt_iterator *gsi, tree val)
7196 : {
7197 : /* We can insert a sequence with a single assignment only. */
7198 139267 : gimple_seq stmts = VN_INFO (val)->expr;
7199 139267 : if (!gimple_seq_singleton_p (stmts))
7200 : return NULL_TREE;
7201 139267 : gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
7202 139267 : if (!stmt
7203 139267 : || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7204 : && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
7205 : && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
7206 : && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
7207 : && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
7208 106 : || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
7209 : return NULL_TREE;
7210 :
7211 46131 : tree op = gimple_assign_rhs1 (stmt);
7212 46131 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
7213 46131 : || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7214 20455 : op = TREE_OPERAND (op, 0);
7215 46131 : tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
7216 46085 : if (!leader)
7217 : return NULL_TREE;
7218 :
7219 35816 : tree res;
7220 35816 : stmts = NULL;
7221 55277 : if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7222 34486 : res = gimple_build (&stmts, BIT_FIELD_REF,
7223 17243 : TREE_TYPE (val), leader,
7224 17243 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
7225 17243 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
7226 18573 : else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
7227 204 : res = gimple_build (&stmts, BIT_AND_EXPR,
7228 102 : TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
7229 : else
7230 18471 : res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
7231 18471 : TREE_TYPE (val), leader);
7232 35816 : if (TREE_CODE (res) != SSA_NAME
7233 35815 : || SSA_NAME_IS_DEFAULT_DEF (res)
7234 71631 : || gimple_bb (SSA_NAME_DEF_STMT (res)))
7235 : {
7236 4 : gimple_seq_discard (stmts);
7237 :
7238 : /* During propagation we have to treat SSA info conservatively
7239 : and thus we can end up simplifying the inserted expression
7240 : at elimination time to sth not defined in stmts. */
7241 : /* But then this is a redundancy we failed to detect. Which means
7242 : res now has two values. That doesn't play well with how
7243 : we track availability here, so give up. */
7244 4 : if (dump_file && (dump_flags & TDF_DETAILS))
7245 : {
7246 0 : if (TREE_CODE (res) == SSA_NAME)
7247 0 : res = eliminate_avail (bb, res);
7248 0 : if (res)
7249 : {
7250 0 : fprintf (dump_file, "Failed to insert expression for value ");
7251 0 : print_generic_expr (dump_file, val);
7252 0 : fprintf (dump_file, " which is really fully redundant to ");
7253 0 : print_generic_expr (dump_file, res);
7254 0 : fprintf (dump_file, "\n");
7255 : }
7256 : }
7257 :
7258 : return NULL_TREE;
7259 : }
7260 : else
7261 : {
7262 35812 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
7263 35812 : vn_ssa_aux_t vn_info = VN_INFO (res);
7264 35812 : vn_info->valnum = val;
7265 35812 : vn_info->visited = true;
7266 : }
7267 :
7268 35812 : insertions++;
7269 35812 : if (dump_file && (dump_flags & TDF_DETAILS))
7270 : {
7271 501 : fprintf (dump_file, "Inserted ");
7272 501 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
7273 : }
7274 :
7275 : return res;
7276 : }
7277 :
7278 : void
7279 374539218 : eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
7280 : {
7281 374539218 : tree sprime = NULL_TREE;
7282 374539218 : gimple *stmt = gsi_stmt (*gsi);
7283 374539218 : tree lhs = gimple_get_lhs (stmt);
7284 123759241 : if (lhs && TREE_CODE (lhs) == SSA_NAME
7285 171047604 : && !gimple_has_volatile_ops (stmt)
7286 : /* See PR43491. Do not replace a global register variable when
7287 : it is a the RHS of an assignment. Do replace local register
7288 : variables since gcc does not guarantee a local variable will
7289 : be allocated in register.
7290 : ??? The fix isn't effective here. This should instead
7291 : be ensured by not value-numbering them the same but treating
7292 : them like volatiles? */
7293 458995282 : && !(gimple_assign_single_p (stmt)
7294 36539447 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
7295 2496896 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
7296 4179 : && is_global_var (gimple_assign_rhs1 (stmt)))))
7297 : {
7298 84455820 : sprime = eliminate_avail (b, lhs);
7299 84455820 : if (!sprime)
7300 : {
7301 : /* If there is no existing usable leader but SCCVN thinks
7302 : it has an expression it wants to use as replacement,
7303 : insert that. */
7304 70985536 : tree val = VN_INFO (lhs)->valnum;
7305 70985536 : vn_ssa_aux_t vn_info;
7306 70985536 : if (val != VN_TOP
7307 70985536 : && TREE_CODE (val) == SSA_NAME
7308 70985536 : && (vn_info = VN_INFO (val), true)
7309 70985536 : && vn_info->needs_insertion
7310 337789 : && vn_info->expr != NULL
7311 71124803 : && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
7312 35812 : eliminate_push_avail (b, sprime);
7313 : }
7314 :
7315 : /* If this now constitutes a copy duplicate points-to
7316 : and range info appropriately. This is especially
7317 : important for inserted code. */
7318 70985536 : if (sprime
7319 13506096 : && TREE_CODE (sprime) == SSA_NAME)
7320 9278571 : maybe_duplicate_ssa_info_at_copy (lhs, sprime);
7321 :
7322 : /* Inhibit the use of an inserted PHI on a loop header when
7323 : the address of the memory reference is a simple induction
7324 : variable. In other cases the vectorizer won't do anything
7325 : anyway (either it's loop invariant or a complicated
7326 : expression). */
7327 9278571 : if (sprime
7328 13506096 : && TREE_CODE (sprime) == SSA_NAME
7329 9278571 : && do_pre
7330 945698 : && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
7331 926492 : && loop_outer (b->loop_father)
7332 397514 : && has_zero_uses (sprime)
7333 197274 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
7334 197066 : && gimple_assign_load_p (stmt))
7335 : {
7336 107437 : gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
7337 107437 : basic_block def_bb = gimple_bb (def_stmt);
7338 107437 : if (gimple_code (def_stmt) == GIMPLE_PHI
7339 107437 : && def_bb->loop_father->header == def_bb)
7340 : {
7341 67624 : loop_p loop = def_bb->loop_father;
7342 67624 : ssa_op_iter iter;
7343 67624 : tree op;
7344 67624 : bool found = false;
7345 85938 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
7346 : {
7347 63962 : affine_iv iv;
7348 63962 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
7349 63962 : if (def_bb
7350 57740 : && flow_bb_inside_loop_p (loop, def_bb)
7351 116454 : && simple_iv (loop, loop, op, &iv, true))
7352 : {
7353 45648 : found = true;
7354 45648 : break;
7355 : }
7356 : }
7357 21976 : if (found)
7358 : {
7359 45648 : if (dump_file && (dump_flags & TDF_DETAILS))
7360 : {
7361 3 : fprintf (dump_file, "Not replacing ");
7362 3 : print_gimple_expr (dump_file, stmt, 0);
7363 3 : fprintf (dump_file, " with ");
7364 3 : print_generic_expr (dump_file, sprime);
7365 3 : fprintf (dump_file, " which would add a loop"
7366 : " carried dependence to loop %d\n",
7367 : loop->num);
7368 : }
7369 : /* Don't keep sprime available. */
7370 : sprime = NULL_TREE;
7371 : }
7372 : }
7373 : }
7374 :
7375 84455820 : if (sprime)
7376 : {
7377 : /* If we can propagate the value computed for LHS into
7378 : all uses don't bother doing anything with this stmt. */
7379 13460448 : if (may_propagate_copy (lhs, sprime))
7380 : {
7381 : /* Mark it for removal. */
7382 13458507 : to_remove.safe_push (stmt);
7383 :
7384 : /* ??? Don't count copy/constant propagations. */
7385 13458507 : if (gimple_assign_single_p (stmt)
7386 13458507 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7387 4716491 : || gimple_assign_rhs1 (stmt) == sprime))
7388 14323065 : return;
7389 :
7390 8199252 : if (dump_file && (dump_flags & TDF_DETAILS))
7391 : {
7392 19126 : fprintf (dump_file, "Replaced ");
7393 19126 : print_gimple_expr (dump_file, stmt, 0);
7394 19126 : fprintf (dump_file, " with ");
7395 19126 : print_generic_expr (dump_file, sprime);
7396 19126 : fprintf (dump_file, " in all uses of ");
7397 19126 : print_gimple_stmt (dump_file, stmt, 0);
7398 : }
7399 :
7400 8199252 : eliminations++;
7401 8199252 : return;
7402 : }
7403 :
7404 : /* If this is an assignment from our leader (which
7405 : happens in the case the value-number is a constant)
7406 : then there is nothing to do. Likewise if we run into
7407 : inserted code that needed a conversion because of
7408 : our type-agnostic value-numbering of loads. */
7409 1941 : if ((gimple_assign_single_p (stmt)
7410 1 : || (is_gimple_assign (stmt)
7411 1 : && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7412 0 : || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
7413 1942 : && sprime == gimple_assign_rhs1 (stmt))
7414 : return;
7415 :
7416 : /* Else replace its RHS. */
7417 719 : if (dump_file && (dump_flags & TDF_DETAILS))
7418 : {
7419 0 : fprintf (dump_file, "Replaced ");
7420 0 : print_gimple_expr (dump_file, stmt, 0);
7421 0 : fprintf (dump_file, " with ");
7422 0 : print_generic_expr (dump_file, sprime);
7423 0 : fprintf (dump_file, " in ");
7424 0 : print_gimple_stmt (dump_file, stmt, 0);
7425 : }
7426 719 : eliminations++;
7427 :
7428 719 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
7429 719 : && stmt_can_make_abnormal_goto (stmt));
7430 719 : gimple *orig_stmt = stmt;
7431 719 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
7432 719 : TREE_TYPE (sprime)))
7433 : {
7434 : /* We preserve conversions to but not from function or method
7435 : types. This asymmetry makes it necessary to re-instantiate
7436 : conversions here. */
7437 717 : if (POINTER_TYPE_P (TREE_TYPE (lhs))
7438 717 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7439 717 : sprime = fold_convert (TREE_TYPE (lhs), sprime);
7440 : else
7441 0 : gcc_unreachable ();
7442 : }
7443 719 : tree vdef = gimple_vdef (stmt);
7444 719 : tree vuse = gimple_vuse (stmt);
7445 719 : propagate_tree_value_into_stmt (gsi, sprime);
7446 719 : stmt = gsi_stmt (*gsi);
7447 719 : update_stmt (stmt);
7448 : /* In case the VDEF on the original stmt was released, value-number
7449 : it to the VUSE. This is to make vuse_ssa_val able to skip
7450 : released virtual operands. */
7451 1438 : if (vdef != gimple_vdef (stmt))
7452 : {
7453 0 : gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7454 0 : VN_INFO (vdef)->valnum = vuse;
7455 : }
7456 :
7457 : /* If we removed EH side-effects from the statement, clean
7458 : its EH information. */
7459 719 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7460 : {
7461 0 : bitmap_set_bit (need_eh_cleanup,
7462 0 : gimple_bb (stmt)->index);
7463 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7464 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7465 : }
7466 :
7467 : /* Likewise for AB side-effects. */
7468 719 : if (can_make_abnormal_goto
7469 719 : && !stmt_can_make_abnormal_goto (stmt))
7470 : {
7471 0 : bitmap_set_bit (need_ab_cleanup,
7472 0 : gimple_bb (stmt)->index);
7473 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7474 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7475 : }
7476 :
7477 : return;
7478 : }
7479 : }
7480 :
7481 : /* If the statement is a scalar store, see if the expression
7482 : has the same value number as its rhs. If so, the store is
7483 : dead. */
7484 361078770 : if (gimple_assign_single_p (stmt)
7485 130904408 : && !gimple_has_volatile_ops (stmt)
7486 56864950 : && !is_gimple_reg (gimple_assign_lhs (stmt))
7487 29225047 : && (TREE_CODE (gimple_assign_lhs (stmt)) != VAR_DECL
7488 2855246 : || !DECL_HARD_REGISTER (gimple_assign_lhs (stmt)))
7489 390299814 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7490 16676406 : || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7491 : {
7492 26081009 : tree rhs = gimple_assign_rhs1 (stmt);
7493 26081009 : vn_reference_t vnresult;
7494 : /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7495 : typed load of a byte known to be 0x11 as 1 so a store of
7496 : a boolean 1 is detected as redundant. Because of this we
7497 : have to make sure to lookup with a ref where its size
7498 : matches the precision. */
7499 26081009 : tree lookup_lhs = lhs;
7500 51893250 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7501 13605326 : && (TREE_CODE (lhs) != COMPONENT_REF
7502 8237754 : || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7503 39482800 : && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7504 : {
7505 846133 : if (BITINT_TYPE_P (TREE_TYPE (lhs))
7506 439916 : && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7507 : lookup_lhs = NULL_TREE;
7508 421386 : else if (TREE_CODE (lhs) == COMPONENT_REF
7509 421386 : || TREE_CODE (lhs) == MEM_REF)
7510 : {
7511 295716 : tree ltype = build_nonstandard_integer_type
7512 295716 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7513 295716 : TYPE_UNSIGNED (TREE_TYPE (lhs)));
7514 295716 : if (TREE_CODE (lhs) == COMPONENT_REF)
7515 : {
7516 227028 : tree foff = component_ref_field_offset (lhs);
7517 227028 : tree f = TREE_OPERAND (lhs, 1);
7518 227028 : if (!poly_int_tree_p (foff))
7519 : lookup_lhs = NULL_TREE;
7520 : else
7521 454056 : lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7522 227028 : TREE_OPERAND (lhs, 0),
7523 227028 : TYPE_SIZE (TREE_TYPE (lhs)),
7524 : bit_from_pos
7525 227028 : (foff, DECL_FIELD_BIT_OFFSET (f)));
7526 : }
7527 : else
7528 68688 : lookup_lhs = build2 (MEM_REF, ltype,
7529 68688 : TREE_OPERAND (lhs, 0),
7530 68688 : TREE_OPERAND (lhs, 1));
7531 : }
7532 : else
7533 : lookup_lhs = NULL_TREE;
7534 : }
7535 25948046 : tree val = NULL_TREE, tem;
7536 25948046 : if (lookup_lhs)
7537 51896092 : val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7538 : VN_WALKREWRITE, &vnresult, false,
7539 : NULL, NULL_TREE, true);
7540 26081009 : if (TREE_CODE (rhs) == SSA_NAME)
7541 12544638 : rhs = VN_INFO (rhs)->valnum;
7542 26081009 : gassign *ass;
7543 26081009 : if (val
7544 26081009 : && (operand_equal_p (val, rhs, 0)
7545 : /* Due to the bitfield lookups above we can get bit
7546 : interpretations of the same RHS as values here. Those
7547 : are redundant as well. */
7548 3197077 : || (TREE_CODE (val) == SSA_NAME
7549 1945960 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7550 1776983 : && (tem = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7551 1776983 : && TREE_CODE (tem) == VIEW_CONVERT_EXPR
7552 3523 : && TREE_OPERAND (tem, 0) == rhs)
7553 3197075 : || (TREE_CODE (rhs) == SSA_NAME
7554 26560887 : && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs)))
7555 1522753 : && gimple_assign_rhs1 (ass) == val
7556 708641 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass))
7557 9 : && tree_nop_conversion_p (TREE_TYPE (rhs), TREE_TYPE (val)))))
7558 : {
7559 : /* We can only remove the later store if the former aliases
7560 : at least all accesses the later one does or if the store
7561 : was to readonly memory storing the same value. */
7562 252831 : ao_ref lhs_ref;
7563 252831 : ao_ref_init (&lhs_ref, lhs);
7564 252831 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
7565 252831 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7566 252831 : if (! vnresult
7567 252831 : || ((vnresult->set == set
7568 54688 : || alias_set_subset_of (set, vnresult->set))
7569 233921 : && (vnresult->base_set == base_set
7570 26022 : || alias_set_subset_of (base_set, vnresult->base_set))))
7571 : {
7572 228763 : if (dump_file && (dump_flags & TDF_DETAILS))
7573 : {
7574 17 : fprintf (dump_file, "Deleted redundant store ");
7575 17 : print_gimple_stmt (dump_file, stmt, 0);
7576 : }
7577 :
7578 : /* Queue stmt for removal. */
7579 228763 : to_remove.safe_push (stmt);
7580 228763 : return;
7581 : }
7582 : }
7583 : }
7584 :
7585 : /* If this is a control statement value numbering left edges
7586 : unexecuted on force the condition in a way consistent with
7587 : that. */
7588 360850007 : if (gcond *cond = dyn_cast <gcond *> (stmt))
7589 : {
7590 19471641 : if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7591 19471641 : ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7592 : {
7593 633854 : if (dump_file && (dump_flags & TDF_DETAILS))
7594 : {
7595 15 : fprintf (dump_file, "Removing unexecutable edge from ");
7596 15 : print_gimple_stmt (dump_file, stmt, 0);
7597 : }
7598 633854 : if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7599 633854 : == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7600 247364 : gimple_cond_make_true (cond);
7601 : else
7602 386490 : gimple_cond_make_false (cond);
7603 633854 : update_stmt (cond);
7604 633854 : el_todo |= TODO_cleanup_cfg;
7605 633854 : return;
7606 : }
7607 : }
7608 :
7609 360216153 : bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7610 360216153 : bool was_noreturn = (is_gimple_call (stmt)
7611 360216153 : && gimple_call_noreturn_p (stmt));
7612 360216153 : tree vdef = gimple_vdef (stmt);
7613 360216153 : tree vuse = gimple_vuse (stmt);
7614 :
7615 : /* If we didn't replace the whole stmt (or propagate the result
7616 : into all uses), replace all uses on this stmt with their
7617 : leaders. */
7618 360216153 : bool modified = false;
7619 360216153 : use_operand_p use_p;
7620 360216153 : ssa_op_iter iter;
7621 530689500 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7622 : {
7623 170473347 : tree use = USE_FROM_PTR (use_p);
7624 : /* ??? The call code above leaves stmt operands un-updated. */
7625 170473347 : if (TREE_CODE (use) != SSA_NAME)
7626 0 : continue;
7627 170473347 : tree sprime;
7628 170473347 : if (SSA_NAME_IS_DEFAULT_DEF (use))
7629 : /* ??? For default defs BB shouldn't matter, but we have to
7630 : solve the inconsistency between rpo eliminate and
7631 : dom eliminate avail valueization first. */
7632 27341543 : sprime = eliminate_avail (b, use);
7633 : else
7634 : /* Look for sth available at the definition block of the argument.
7635 : This avoids inconsistencies between availability there which
7636 : decides if the stmt can be removed and availability at the
7637 : use site. The SSA property ensures that things available
7638 : at the definition are also available at uses. */
7639 143131804 : sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7640 170473347 : if (sprime && sprime != use
7641 13566319 : && may_propagate_copy (use, sprime, true)
7642 : /* We substitute into debug stmts to avoid excessive
7643 : debug temporaries created by removed stmts, but we need
7644 : to avoid doing so for inserted sprimes as we never want
7645 : to create debug temporaries for them. */
7646 184038949 : && (!inserted_exprs
7647 1221194 : || TREE_CODE (sprime) != SSA_NAME
7648 1203388 : || !is_gimple_debug (stmt)
7649 383098 : || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7650 : {
7651 13217023 : propagate_value (use_p, sprime);
7652 13217023 : modified = true;
7653 : }
7654 : }
7655 :
7656 : /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7657 : into which is a requirement for the IPA devirt machinery. */
7658 360216153 : gimple *old_stmt = stmt;
7659 360216153 : if (modified)
7660 : {
7661 : /* If a formerly non-invariant ADDR_EXPR is turned into an
7662 : invariant one it was on a separate stmt. */
7663 12296525 : if (gimple_assign_single_p (stmt)
7664 12296525 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7665 245422 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7666 12296525 : gimple_stmt_iterator prev = *gsi;
7667 12296525 : gsi_prev (&prev);
7668 12296525 : if (fold_stmt (gsi, follow_all_ssa_edges))
7669 : {
7670 : /* fold_stmt may have created new stmts in between
7671 : the previous stmt and the folded stmt. Mark
7672 : all defs created there as varying to not confuse
7673 : the SCCVN machinery as we're using that even during
7674 : elimination. */
7675 1043108 : if (gsi_end_p (prev))
7676 227068 : prev = gsi_start_bb (b);
7677 : else
7678 929574 : gsi_next (&prev);
7679 1043108 : if (gsi_stmt (prev) != gsi_stmt (*gsi))
7680 108593 : do
7681 : {
7682 68431 : tree def;
7683 68431 : ssa_op_iter dit;
7684 132613 : FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7685 : dit, SSA_OP_ALL_DEFS)
7686 : /* As existing DEFs may move between stmts
7687 : only process new ones. */
7688 64182 : if (! has_VN_INFO (def))
7689 : {
7690 40060 : vn_ssa_aux_t vn_info = VN_INFO (def);
7691 40060 : vn_info->valnum = def;
7692 40060 : vn_info->visited = true;
7693 : }
7694 68431 : if (gsi_stmt (prev) == gsi_stmt (*gsi))
7695 : break;
7696 40162 : gsi_next (&prev);
7697 40162 : }
7698 : while (1);
7699 : }
7700 12296525 : stmt = gsi_stmt (*gsi);
7701 : /* In case we folded the stmt away schedule the NOP for removal. */
7702 12296525 : if (gimple_nop_p (stmt))
7703 844 : to_remove.safe_push (stmt);
7704 : }
7705 :
7706 : /* Visit indirect calls and turn them into direct calls if
7707 : possible using the devirtualization machinery. Do this before
7708 : checking for required EH/abnormal/noreturn cleanup as devird
7709 : may expose more of those. */
7710 360216153 : if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7711 : {
7712 22907086 : tree fn = gimple_call_fn (call_stmt);
7713 22907086 : if (fn
7714 22085462 : && flag_devirtualize
7715 44244237 : && virtual_method_call_p (fn))
7716 : {
7717 186817 : tree otr_type = obj_type_ref_class (fn);
7718 186817 : unsigned HOST_WIDE_INT otr_tok
7719 186817 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7720 186817 : tree instance;
7721 186817 : ipa_polymorphic_call_context context (current_function_decl,
7722 186817 : fn, stmt, &instance);
7723 186817 : context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7724 : otr_type, stmt, NULL);
7725 186817 : bool final;
7726 186817 : vec <cgraph_node *> targets
7727 186817 : = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7728 : otr_tok, context, &final);
7729 186817 : if (dump_file)
7730 22 : dump_possible_polymorphic_call_targets (dump_file,
7731 : obj_type_ref_class (fn),
7732 : otr_tok, context);
7733 187112 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
7734 : {
7735 73 : tree fn;
7736 73 : if (targets.length () == 1)
7737 73 : fn = targets[0]->decl;
7738 : else
7739 0 : fn = builtin_decl_unreachable ();
7740 73 : if (dump_enabled_p ())
7741 : {
7742 9 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7743 : "converting indirect call to "
7744 : "function %s\n",
7745 9 : lang_hooks.decl_printable_name (fn, 2));
7746 : }
7747 73 : gimple_call_set_fndecl (call_stmt, fn);
7748 : /* If changing the call to __builtin_unreachable
7749 : or similar noreturn function, adjust gimple_call_fntype
7750 : too. */
7751 73 : if (gimple_call_noreturn_p (call_stmt)
7752 0 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7753 0 : && TYPE_ARG_TYPES (TREE_TYPE (fn))
7754 73 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7755 0 : == void_type_node))
7756 0 : gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7757 73 : maybe_remove_unused_call_args (cfun, call_stmt);
7758 73 : modified = true;
7759 : }
7760 : }
7761 : }
7762 :
7763 360216153 : if (modified)
7764 : {
7765 : /* When changing a call into a noreturn call, cfg cleanup
7766 : is needed to fix up the noreturn call. */
7767 12296546 : if (!was_noreturn
7768 12296546 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7769 56 : to_fixup.safe_push (stmt);
7770 : /* When changing a condition or switch into one we know what
7771 : edge will be executed, schedule a cfg cleanup. */
7772 12296546 : if ((gimple_code (stmt) == GIMPLE_COND
7773 1560868 : && (gimple_cond_true_p (as_a <gcond *> (stmt))
7774 1555129 : || gimple_cond_false_p (as_a <gcond *> (stmt))))
7775 13848778 : || (gimple_code (stmt) == GIMPLE_SWITCH
7776 7781 : && TREE_CODE (gimple_switch_index
7777 : (as_a <gswitch *> (stmt))) == INTEGER_CST))
7778 10489 : el_todo |= TODO_cleanup_cfg;
7779 : /* If we removed EH side-effects from the statement, clean
7780 : its EH information. */
7781 12296546 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7782 : {
7783 1963 : bitmap_set_bit (need_eh_cleanup,
7784 1963 : gimple_bb (stmt)->index);
7785 1963 : if (dump_file && (dump_flags & TDF_DETAILS))
7786 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7787 : }
7788 : /* Likewise for AB side-effects. */
7789 12296546 : if (can_make_abnormal_goto
7790 12296546 : && !stmt_can_make_abnormal_goto (stmt))
7791 : {
7792 0 : bitmap_set_bit (need_ab_cleanup,
7793 0 : gimple_bb (stmt)->index);
7794 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7795 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7796 : }
7797 12296546 : update_stmt (stmt);
7798 : /* In case the VDEF on the original stmt was released, value-number
7799 : it to the VUSE. This is to make vuse_ssa_val able to skip
7800 : released virtual operands. */
7801 15640614 : if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7802 2168 : VN_INFO (vdef)->valnum = vuse;
7803 : }
7804 :
7805 : /* Make new values available - for fully redundant LHS we
7806 : continue with the next stmt above and skip this.
7807 : But avoid picking up dead defs. */
7808 360216153 : tree def;
7809 432546898 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7810 72330745 : if (! has_zero_uses (def)
7811 72330745 : || (inserted_exprs
7812 217092 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7813 70871147 : eliminate_push_avail (b, def);
7814 : }
7815 :
7816 : /* Perform elimination for the basic-block B during the domwalk. */
7817 :
7818 : edge
7819 42292488 : eliminate_dom_walker::before_dom_children (basic_block b)
7820 : {
7821 : /* Mark new bb. */
7822 42292488 : avail_stack.safe_push (NULL_TREE);
7823 :
7824 : /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7825 42292488 : if (!(b->flags & BB_EXECUTABLE))
7826 : return NULL;
7827 :
7828 37336067 : vn_context_bb = b;
7829 :
7830 49013354 : for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7831 : {
7832 11677287 : gphi *phi = gsi.phi ();
7833 11677287 : tree res = PHI_RESULT (phi);
7834 :
7835 23354574 : if (virtual_operand_p (res))
7836 : {
7837 5402613 : gsi_next (&gsi);
7838 5402613 : continue;
7839 : }
7840 :
7841 6274674 : tree sprime = eliminate_avail (b, res);
7842 6274674 : if (sprime
7843 6274674 : && sprime != res)
7844 : {
7845 439663 : if (dump_file && (dump_flags & TDF_DETAILS))
7846 : {
7847 20 : fprintf (dump_file, "Replaced redundant PHI node defining ");
7848 20 : print_generic_expr (dump_file, res);
7849 20 : fprintf (dump_file, " with ");
7850 20 : print_generic_expr (dump_file, sprime);
7851 20 : fprintf (dump_file, "\n");
7852 : }
7853 :
7854 : /* If we inserted this PHI node ourself, it's not an elimination. */
7855 439663 : if (! inserted_exprs
7856 561285 : || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
7857 411502 : eliminations++;
7858 :
7859 : /* If we will propagate into all uses don't bother to do
7860 : anything. */
7861 439663 : if (may_propagate_copy (res, sprime))
7862 : {
7863 : /* Mark the PHI for removal. */
7864 439663 : to_remove.safe_push (phi);
7865 439663 : gsi_next (&gsi);
7866 439663 : continue;
7867 : }
7868 :
7869 0 : remove_phi_node (&gsi, false);
7870 :
7871 0 : if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
7872 0 : sprime = fold_convert (TREE_TYPE (res), sprime);
7873 0 : gimple *stmt = gimple_build_assign (res, sprime);
7874 0 : gimple_stmt_iterator gsi2 = gsi_after_labels (b);
7875 0 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
7876 0 : continue;
7877 0 : }
7878 :
7879 5835011 : eliminate_push_avail (b, res);
7880 5835011 : gsi_next (&gsi);
7881 : }
7882 :
7883 74672134 : for (gimple_stmt_iterator gsi = gsi_start_bb (b);
7884 297877096 : !gsi_end_p (gsi);
7885 260541029 : gsi_next (&gsi))
7886 260541029 : eliminate_stmt (b, &gsi);
7887 :
7888 : /* Replace destination PHI arguments. */
7889 37336067 : edge_iterator ei;
7890 37336067 : edge e;
7891 88135562 : FOR_EACH_EDGE (e, ei, b->succs)
7892 50799495 : if (e->flags & EDGE_EXECUTABLE)
7893 50233135 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
7894 80340453 : !gsi_end_p (gsi);
7895 30107318 : gsi_next (&gsi))
7896 : {
7897 30107318 : gphi *phi = gsi.phi ();
7898 30107318 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7899 30107318 : tree arg = USE_FROM_PTR (use_p);
7900 49818462 : if (TREE_CODE (arg) != SSA_NAME
7901 30107318 : || virtual_operand_p (arg))
7902 19711144 : continue;
7903 10396174 : tree sprime = eliminate_avail (b, arg);
7904 20792348 : if (sprime && may_propagate_copy (arg, sprime,
7905 10396174 : !(e->flags & EDGE_ABNORMAL)))
7906 10384086 : propagate_value (use_p, sprime);
7907 : }
7908 :
7909 37336067 : vn_context_bb = NULL;
7910 :
7911 37336067 : return NULL;
7912 : }
7913 :
7914 : /* Make no longer available leaders no longer available. */
7915 :
7916 : void
7917 42292488 : eliminate_dom_walker::after_dom_children (basic_block)
7918 : {
7919 42292488 : tree entry;
7920 93633803 : while ((entry = avail_stack.pop ()) != NULL_TREE)
7921 : {
7922 51341315 : tree valnum = VN_INFO (entry)->valnum;
7923 51341315 : tree old = avail[SSA_NAME_VERSION (valnum)];
7924 51341315 : if (old == entry)
7925 : avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
7926 : else
7927 45692 : avail[SSA_NAME_VERSION (valnum)] = entry;
7928 : }
7929 42292488 : }
7930 :
7931 : /* Remove queued stmts and perform delayed cleanups. */
7932 :
7933 : unsigned
7934 6298802 : eliminate_dom_walker::eliminate_cleanup (bool region_p)
7935 : {
7936 6298802 : statistics_counter_event (cfun, "Eliminated", eliminations);
7937 6298802 : statistics_counter_event (cfun, "Insertions", insertions);
7938 :
7939 : /* We cannot remove stmts during BB walk, especially not release SSA
7940 : names there as this confuses the VN machinery. The stmts ending
7941 : up in to_remove are either stores or simple copies.
7942 : Remove stmts in reverse order to make debug stmt creation possible. */
7943 34358351 : while (!to_remove.is_empty ())
7944 : {
7945 15461889 : bool do_release_defs = true;
7946 15461889 : gimple *stmt = to_remove.pop ();
7947 :
7948 : /* When we are value-numbering a region we do not require exit PHIs to
7949 : be present so we have to make sure to deal with uses outside of the
7950 : region of stmts that we thought are eliminated.
7951 : ??? Note we may be confused by uses in dead regions we didn't run
7952 : elimination on. Rather than checking individual uses we accept
7953 : dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7954 : contains such example). */
7955 15461889 : if (region_p)
7956 : {
7957 1816302 : if (gphi *phi = dyn_cast <gphi *> (stmt))
7958 : {
7959 1130655 : tree lhs = gimple_phi_result (phi);
7960 1130655 : if (!has_zero_uses (lhs))
7961 : {
7962 24089 : if (dump_file && (dump_flags & TDF_DETAILS))
7963 3 : fprintf (dump_file, "Keeping eliminated stmt live "
7964 : "as copy because of out-of-region uses\n");
7965 24089 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7966 24089 : gimple *copy = gimple_build_assign (lhs, sprime);
7967 24089 : gimple_stmt_iterator gsi
7968 24089 : = gsi_after_labels (gimple_bb (stmt));
7969 24089 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7970 24089 : do_release_defs = false;
7971 : }
7972 : }
7973 685647 : else if (tree lhs = gimple_get_lhs (stmt))
7974 685647 : if (TREE_CODE (lhs) == SSA_NAME
7975 685647 : && !has_zero_uses (lhs))
7976 : {
7977 2089 : if (dump_file && (dump_flags & TDF_DETAILS))
7978 0 : fprintf (dump_file, "Keeping eliminated stmt live "
7979 : "as copy because of out-of-region uses\n");
7980 2089 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7981 2089 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7982 2089 : if (is_gimple_assign (stmt))
7983 : {
7984 2089 : gimple_assign_set_rhs_from_tree (&gsi, sprime);
7985 2089 : stmt = gsi_stmt (gsi);
7986 2089 : update_stmt (stmt);
7987 2089 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
7988 0 : bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
7989 2089 : continue;
7990 : }
7991 : else
7992 : {
7993 0 : gimple *copy = gimple_build_assign (lhs, sprime);
7994 0 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7995 0 : do_release_defs = false;
7996 : }
7997 : }
7998 : }
7999 :
8000 15459800 : if (dump_file && (dump_flags & TDF_DETAILS))
8001 : {
8002 21744 : fprintf (dump_file, "Removing dead stmt ");
8003 21744 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
8004 : }
8005 :
8006 15459800 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
8007 15459800 : if (gimple_code (stmt) == GIMPLE_PHI)
8008 1773775 : remove_phi_node (&gsi, do_release_defs);
8009 : else
8010 : {
8011 13686025 : basic_block bb = gimple_bb (stmt);
8012 13686025 : unlink_stmt_vdef (stmt);
8013 13686025 : if (gsi_remove (&gsi, true))
8014 26747 : bitmap_set_bit (need_eh_cleanup, bb->index);
8015 13686025 : if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
8016 2 : bitmap_set_bit (need_ab_cleanup, bb->index);
8017 13686025 : if (do_release_defs)
8018 13686025 : release_defs (stmt);
8019 : }
8020 :
8021 : /* Removing a stmt may expose a forwarder block. */
8022 15459800 : el_todo |= TODO_cleanup_cfg;
8023 : }
8024 :
8025 : /* Fixup stmts that became noreturn calls. This may require splitting
8026 : blocks and thus isn't possible during the dominator walk. Do this
8027 : in reverse order so we don't inadvertently remove a stmt we want to
8028 : fixup by visiting a dominating now noreturn call first. */
8029 6298858 : while (!to_fixup.is_empty ())
8030 : {
8031 56 : gimple *stmt = to_fixup.pop ();
8032 :
8033 56 : if (dump_file && (dump_flags & TDF_DETAILS))
8034 : {
8035 0 : fprintf (dump_file, "Fixing up noreturn call ");
8036 0 : print_gimple_stmt (dump_file, stmt, 0);
8037 : }
8038 :
8039 56 : if (fixup_noreturn_call (stmt))
8040 56 : el_todo |= TODO_cleanup_cfg;
8041 : }
8042 :
8043 6298802 : bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
8044 6298802 : bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
8045 :
8046 6298802 : if (do_eh_cleanup)
8047 10809 : gimple_purge_all_dead_eh_edges (need_eh_cleanup);
8048 :
8049 6298802 : if (do_ab_cleanup)
8050 2 : gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
8051 :
8052 6298802 : if (do_eh_cleanup || do_ab_cleanup)
8053 10811 : el_todo |= TODO_cleanup_cfg;
8054 :
8055 6298802 : return el_todo;
8056 : }
8057 :
8058 : /* Eliminate fully redundant computations. */
8059 :
8060 : unsigned
8061 4403091 : eliminate_with_rpo_vn (bitmap inserted_exprs)
8062 : {
8063 4403091 : eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
8064 :
8065 4403091 : eliminate_dom_walker *saved_rpo_avail = rpo_avail;
8066 4403091 : rpo_avail = &walker;
8067 4403091 : walker.walk (cfun->cfg->x_entry_block_ptr);
8068 4403091 : rpo_avail = saved_rpo_avail;
8069 :
8070 4403091 : return walker.eliminate_cleanup ();
8071 4403091 : }
8072 :
8073 : static unsigned
8074 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8075 : bool iterate, bool eliminate, bool skip_entry_phis,
8076 : vn_lookup_kind kind);
8077 :
8078 : void
8079 983312 : run_rpo_vn (vn_lookup_kind kind)
8080 : {
8081 983312 : do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
8082 :
8083 : /* ??? Prune requirement of these. */
8084 983312 : constant_to_value_id = new hash_table<vn_constant_hasher> (23);
8085 :
8086 : /* Initialize the value ids and prune out remaining VN_TOPs
8087 : from dead code. */
8088 983312 : tree name;
8089 983312 : unsigned i;
8090 48319934 : FOR_EACH_SSA_NAME (i, name, cfun)
8091 : {
8092 34372746 : vn_ssa_aux_t info = VN_INFO (name);
8093 34372746 : if (!info->visited
8094 34296140 : || info->valnum == VN_TOP)
8095 76606 : info->valnum = name;
8096 34372746 : if (info->valnum == name)
8097 33202743 : info->value_id = get_next_value_id ();
8098 1170003 : else if (is_gimple_min_invariant (info->valnum))
8099 41429 : info->value_id = get_or_alloc_constant_value_id (info->valnum);
8100 : }
8101 :
8102 : /* Propagate. */
8103 48319934 : FOR_EACH_SSA_NAME (i, name, cfun)
8104 : {
8105 34372746 : vn_ssa_aux_t info = VN_INFO (name);
8106 34372746 : if (TREE_CODE (info->valnum) == SSA_NAME
8107 34331317 : && info->valnum != name
8108 35501320 : && info->value_id != VN_INFO (info->valnum)->value_id)
8109 1128574 : info->value_id = VN_INFO (info->valnum)->value_id;
8110 : }
8111 :
8112 983312 : set_hashtable_value_ids ();
8113 :
8114 983312 : if (dump_file && (dump_flags & TDF_DETAILS))
8115 : {
8116 14 : fprintf (dump_file, "Value numbers:\n");
8117 406 : FOR_EACH_SSA_NAME (i, name, cfun)
8118 : {
8119 307 : if (VN_INFO (name)->visited
8120 307 : && SSA_VAL (name) != name)
8121 : {
8122 33 : print_generic_expr (dump_file, name);
8123 33 : fprintf (dump_file, " = ");
8124 33 : print_generic_expr (dump_file, SSA_VAL (name));
8125 33 : fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
8126 : }
8127 : }
8128 : }
8129 983312 : }
8130 :
8131 : /* Free VN associated data structures. */
8132 :
8133 : void
8134 6318405 : free_rpo_vn (void)
8135 : {
8136 6318405 : free_vn_table (valid_info);
8137 6318405 : XDELETE (valid_info);
8138 6318405 : obstack_free (&vn_tables_obstack, NULL);
8139 6318405 : obstack_free (&vn_tables_insert_obstack, NULL);
8140 :
8141 6318405 : vn_ssa_aux_iterator_type it;
8142 6318405 : vn_ssa_aux_t info;
8143 183375043 : FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
8144 177056638 : if (info->needs_insertion)
8145 4215106 : release_ssa_name (info->name);
8146 6318405 : obstack_free (&vn_ssa_aux_obstack, NULL);
8147 6318405 : delete vn_ssa_aux_hash;
8148 :
8149 6318405 : delete constant_to_value_id;
8150 6318405 : constant_to_value_id = NULL;
8151 6318405 : }
8152 :
8153 : /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
8154 :
8155 : static tree
8156 23591663 : vn_lookup_simplify_result (gimple_match_op *res_op)
8157 : {
8158 23591663 : if (!res_op->code.is_tree_code ())
8159 : return NULL_TREE;
8160 23588490 : tree *ops = res_op->ops;
8161 23588490 : unsigned int length = res_op->num_ops;
8162 23588490 : if (res_op->code == CONSTRUCTOR
8163 : /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
8164 : and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
8165 23588490 : && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
8166 : {
8167 1048 : length = CONSTRUCTOR_NELTS (res_op->ops[0]);
8168 1048 : ops = XALLOCAVEC (tree, length);
8169 4724 : for (unsigned i = 0; i < length; ++i)
8170 3676 : ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
8171 : }
8172 23588490 : vn_nary_op_t vnresult = NULL;
8173 23588490 : tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
8174 : res_op->type, ops, &vnresult);
8175 : /* If this is used from expression simplification make sure to
8176 : return an available expression. */
8177 23588490 : if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
8178 2301410 : res = rpo_avail->eliminate_avail (vn_context_bb, res);
8179 : return res;
8180 : }
8181 :
8182 : /* Return a leader for OPs value that is valid at BB. */
8183 :
8184 : tree
8185 284387027 : rpo_elim::eliminate_avail (basic_block bb, tree op)
8186 : {
8187 284387027 : bool visited;
8188 284387027 : tree valnum = SSA_VAL (op, &visited);
8189 : /* If we didn't visit OP then it must be defined outside of the
8190 : region we process and also dominate it. So it is available. */
8191 284387027 : if (!visited)
8192 : return op;
8193 282189680 : if (TREE_CODE (valnum) == SSA_NAME)
8194 : {
8195 267591352 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
8196 : return valnum;
8197 260621422 : vn_ssa_aux_t valnum_info = VN_INFO (valnum);
8198 260621422 : vn_avail *av = valnum_info->avail;
8199 260621422 : if (!av)
8200 : {
8201 : /* See above. But when there's availability info prefer
8202 : what we recorded there for example to preserve LC SSA. */
8203 85583103 : if (!valnum_info->visited)
8204 : return valnum;
8205 84699334 : return NULL_TREE;
8206 : }
8207 175038319 : if (av->location == bb->index)
8208 : /* On tramp3d 90% of the cases are here. */
8209 115547989 : return ssa_name (av->leader);
8210 73793236 : do
8211 : {
8212 73793236 : basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
8213 : /* ??? During elimination we have to use availability at the
8214 : definition site of a use we try to replace. This
8215 : is required to not run into inconsistencies because
8216 : of dominated_by_p_w_unex behavior and removing a definition
8217 : while not replacing all uses.
8218 : ??? We could try to consistently walk dominators
8219 : ignoring non-executable regions. The nearest common
8220 : dominator of bb and abb is where we can stop walking. We
8221 : may also be able to "pre-compute" (bits of) the next immediate
8222 : (non-)dominator during the RPO walk when marking edges as
8223 : executable. */
8224 73793236 : if (dominated_by_p_w_unex (bb, abb, true))
8225 : {
8226 55450462 : tree leader = ssa_name (av->leader);
8227 : /* Prevent eliminations that break loop-closed SSA. */
8228 55450462 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
8229 3765724 : && ! SSA_NAME_IS_DEFAULT_DEF (leader)
8230 59216186 : && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
8231 3765724 : (leader))->loop_father,
8232 : bb))
8233 : return NULL_TREE;
8234 55372532 : if (dump_file && (dump_flags & TDF_DETAILS))
8235 : {
8236 3884 : print_generic_expr (dump_file, leader);
8237 3884 : fprintf (dump_file, " is available for ");
8238 3884 : print_generic_expr (dump_file, valnum);
8239 3884 : fprintf (dump_file, "\n");
8240 : }
8241 : /* On tramp3d 99% of the _remaining_ cases succeed at
8242 : the first enty. */
8243 : return leader;
8244 : }
8245 : /* ??? Can we somehow skip to the immediate dominator
8246 : RPO index (bb_to_rpo)? Again, maybe not worth, on
8247 : tramp3d the worst number of elements in the vector is 9. */
8248 18342774 : av = av->next;
8249 : }
8250 18342774 : while (av);
8251 : /* While we prefer avail we have to fallback to using the value
8252 : directly if defined outside of the region when none of the
8253 : available defs suit. */
8254 4039868 : if (!valnum_info->visited)
8255 2 : return valnum;
8256 : }
8257 14598328 : else if (valnum != VN_TOP)
8258 : /* valnum is is_gimple_min_invariant. */
8259 14598328 : return valnum;
8260 : return NULL_TREE;
8261 : }
8262 :
8263 : /* Make LEADER a leader for its value at BB. */
8264 :
8265 : void
8266 99142031 : rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
8267 : {
8268 99142031 : tree valnum = VN_INFO (leader)->valnum;
8269 99142031 : if (valnum == VN_TOP
8270 99142031 : || is_gimple_min_invariant (valnum))
8271 : return;
8272 99142031 : if (dump_file && (dump_flags & TDF_DETAILS))
8273 : {
8274 327921 : fprintf (dump_file, "Making available beyond BB%d ", bb->index);
8275 327921 : print_generic_expr (dump_file, leader);
8276 327921 : fprintf (dump_file, " for value ");
8277 327921 : print_generic_expr (dump_file, valnum);
8278 327921 : fprintf (dump_file, "\n");
8279 : }
8280 99142031 : vn_ssa_aux_t value = VN_INFO (valnum);
8281 99142031 : vn_avail *av;
8282 99142031 : if (m_avail_freelist)
8283 : {
8284 18807510 : av = m_avail_freelist;
8285 18807510 : m_avail_freelist = m_avail_freelist->next;
8286 : }
8287 : else
8288 80334521 : av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
8289 99142031 : av->location = bb->index;
8290 99142031 : av->leader = SSA_NAME_VERSION (leader);
8291 99142031 : av->next = value->avail;
8292 99142031 : av->next_undo = last_pushed_avail;
8293 99142031 : last_pushed_avail = value;
8294 99142031 : value->avail = av;
8295 : }
8296 :
8297 : /* Valueization hook for RPO VN plus required state. */
8298 :
8299 : tree
8300 2319935371 : rpo_vn_valueize (tree name)
8301 : {
8302 2319935371 : if (TREE_CODE (name) == SSA_NAME)
8303 : {
8304 2272863892 : vn_ssa_aux_t val = VN_INFO (name);
8305 2272863892 : if (val)
8306 : {
8307 2272863892 : tree tem = val->valnum;
8308 2272863892 : if (tem != VN_TOP && tem != name)
8309 : {
8310 120716450 : if (TREE_CODE (tem) != SSA_NAME)
8311 : return tem;
8312 : /* For all values we only valueize to an available leader
8313 : which means we can use SSA name info without restriction. */
8314 102924857 : tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
8315 102924857 : if (tem)
8316 102732137 : return tem;
8317 : }
8318 : }
8319 : }
8320 : return name;
8321 : }
8322 :
8323 : /* Insert on PRED_E predicates derived from CODE OPS being true besides the
8324 : inverted condition. */
8325 :
8326 : static void
8327 27881784 : insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
8328 : {
8329 27881784 : switch (code)
8330 : {
8331 1365692 : case LT_EXPR:
8332 : /* a < b -> a {!,<}= b */
8333 1365692 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8334 : ops, boolean_true_node, 0, pred_e);
8335 1365692 : vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
8336 : ops, boolean_true_node, 0, pred_e);
8337 : /* a < b -> ! a {>,=} b */
8338 1365692 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8339 : ops, boolean_false_node, 0, pred_e);
8340 1365692 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8341 : ops, boolean_false_node, 0, pred_e);
8342 1365692 : break;
8343 3532352 : case GT_EXPR:
8344 : /* a > b -> a {!,>}= b */
8345 3532352 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8346 : ops, boolean_true_node, 0, pred_e);
8347 3532352 : vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
8348 : ops, boolean_true_node, 0, pred_e);
8349 : /* a > b -> ! a {<,=} b */
8350 3532352 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8351 : ops, boolean_false_node, 0, pred_e);
8352 3532352 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8353 : ops, boolean_false_node, 0, pred_e);
8354 3532352 : break;
8355 9561423 : case EQ_EXPR:
8356 : /* a == b -> ! a {<,>} b */
8357 9561423 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8358 : ops, boolean_false_node, 0, pred_e);
8359 9561423 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8360 : ops, boolean_false_node, 0, pred_e);
8361 9561423 : break;
8362 : case LE_EXPR:
8363 : case GE_EXPR:
8364 : case NE_EXPR:
8365 : /* Nothing besides inverted condition. */
8366 : break;
8367 27881784 : default:;
8368 : }
8369 27881784 : }
8370 :
8371 : /* Insert on the TRUE_E true and FALSE_E false predicates
8372 : derived from LHS CODE RHS. */
8373 :
8374 : static void
8375 23899389 : insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
8376 : edge true_e, edge false_e)
8377 : {
8378 : /* If both edges are null, then there is nothing to be done. */
8379 23899389 : if (!true_e && !false_e)
8380 1398276 : return;
8381 :
8382 : /* Canonicalize the comparison if needed, putting
8383 : the constant in the rhs. */
8384 22504605 : if (tree_swap_operands_p (lhs, rhs))
8385 : {
8386 0 : std::swap (lhs, rhs);
8387 0 : code = swap_tree_comparison (code);
8388 : }
8389 :
8390 : /* If the lhs is not a ssa name, don't record anything. */
8391 22504605 : if (TREE_CODE (lhs) != SSA_NAME)
8392 : return;
8393 :
8394 22501113 : tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs));
8395 22501113 : tree ops[2];
8396 22501113 : ops[0] = lhs;
8397 22501113 : ops[1] = rhs;
8398 22501113 : if (true_e)
8399 18357243 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8400 : boolean_true_node, 0, true_e);
8401 22501113 : if (false_e)
8402 17324371 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8403 : boolean_false_node, 0, false_e);
8404 22501113 : if (icode != ERROR_MARK)
8405 : {
8406 22246122 : if (true_e)
8407 18198395 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8408 : boolean_false_node, 0, true_e);
8409 22246122 : if (false_e)
8410 17118156 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8411 : boolean_true_node, 0, false_e);
8412 : }
8413 : /* Relax for non-integers, inverted condition handled
8414 : above. */
8415 22501113 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8416 : {
8417 17581951 : if (true_e)
8418 14406421 : insert_related_predicates_on_edge (code, ops, true_e);
8419 17581951 : if (false_e)
8420 13475363 : insert_related_predicates_on_edge (icode, ops, false_e);
8421 : }
8422 22501113 : if (integer_zerop (rhs)
8423 22501113 : && (code == NE_EXPR || code == EQ_EXPR))
8424 : {
8425 9367755 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
8426 : /* (A CMP B) != 0 is the same as (A CMP B).
8427 : (A CMP B) == 0 is just (A CMP B) with the edges swapped. */
8428 9367755 : if (is_gimple_assign (def_stmt)
8429 9367755 : && TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_comparison)
8430 : {
8431 443488 : tree_code nc = gimple_assign_rhs_code (def_stmt);
8432 443488 : tree nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8433 443488 : tree nrhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8434 : // Canonicalize the comparison before the check below,
8435 : // it might be the case where nlhs is a constant now.
8436 443488 : if (tree_swap_operands_p (nlhs, nrhs))
8437 : {
8438 16786 : std::swap (nlhs, nrhs);
8439 16786 : nc = swap_tree_comparison (nc);
8440 : }
8441 443488 : edge nt = true_e;
8442 443488 : edge nf = false_e;
8443 443488 : if (code == EQ_EXPR)
8444 316572 : std::swap (nt, nf);
8445 443488 : if (lhs != nlhs)
8446 443483 : insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
8447 : }
8448 : /* (a | b) == 0 ->
8449 : on true edge assert: a == 0 & b == 0. */
8450 : /* (a | b) != 0 ->
8451 : on false edge assert: a == 0 & b == 0. */
8452 9367755 : if (is_gimple_assign (def_stmt)
8453 9367755 : && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
8454 : {
8455 265196 : edge e = code == EQ_EXPR ? true_e : false_e;
8456 265196 : tree nlhs;
8457 :
8458 265196 : nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8459 : /* A valueization of the `a` might return the old lhs
8460 : which is already handled above. */
8461 265196 : if (nlhs != lhs)
8462 265196 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8463 :
8464 : /* A valueization of the `b` might return the old lhs
8465 : which is already handled above. */
8466 265196 : nlhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8467 265196 : if (nlhs != lhs)
8468 265196 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8469 : }
8470 : }
8471 : }
8472 :
8473 : /* Main stmt worker for RPO VN, process BB. */
8474 :
8475 : static unsigned
8476 62737412 : process_bb (rpo_elim &avail, basic_block bb,
8477 : bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
8478 : bool do_region, bitmap exit_bbs, bool skip_phis)
8479 : {
8480 62737412 : unsigned todo = 0;
8481 62737412 : edge_iterator ei;
8482 62737412 : edge e;
8483 :
8484 62737412 : vn_context_bb = bb;
8485 :
8486 : /* If we are in loop-closed SSA preserve this state. This is
8487 : relevant when called on regions from outside of FRE/PRE. */
8488 62737412 : bool lc_phi_nodes = false;
8489 62737412 : if (!skip_phis
8490 62737412 : && loops_state_satisfies_p (LOOP_CLOSED_SSA))
8491 3803402 : FOR_EACH_EDGE (e, ei, bb->preds)
8492 2300561 : if (e->src->loop_father != e->dest->loop_father
8493 2300561 : && flow_loop_nested_p (e->dest->loop_father,
8494 : e->src->loop_father))
8495 : {
8496 : lc_phi_nodes = true;
8497 : break;
8498 : }
8499 :
8500 : /* When we visit a loop header substitute into loop info. */
8501 62737412 : if (!iterate && eliminate && bb->loop_father->header == bb)
8502 : {
8503 : /* Keep fields in sync with substitute_in_loop_info. */
8504 948805 : if (bb->loop_father->nb_iterations)
8505 154443 : bb->loop_father->nb_iterations
8506 154443 : = simplify_replace_tree (bb->loop_father->nb_iterations,
8507 : NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
8508 : }
8509 :
8510 : /* Value-number all defs in the basic-block. */
8511 62737412 : if (!skip_phis)
8512 89895689 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8513 27183769 : gsi_next (&gsi))
8514 : {
8515 27183769 : gphi *phi = gsi.phi ();
8516 27183769 : tree res = PHI_RESULT (phi);
8517 27183769 : vn_ssa_aux_t res_info = VN_INFO (res);
8518 27183769 : if (!bb_visited)
8519 : {
8520 19295523 : gcc_assert (!res_info->visited);
8521 19295523 : res_info->valnum = VN_TOP;
8522 19295523 : res_info->visited = true;
8523 : }
8524 :
8525 : /* When not iterating force backedge values to varying. */
8526 27183769 : visit_stmt (phi, !iterate_phis);
8527 54367538 : if (virtual_operand_p (res))
8528 10838433 : continue;
8529 :
8530 : /* Eliminate */
8531 : /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
8532 : how we handle backedges and availability.
8533 : And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
8534 16345336 : tree val = res_info->valnum;
8535 16345336 : if (res != val && !iterate && eliminate)
8536 : {
8537 1453383 : if (tree leader = avail.eliminate_avail (bb, res))
8538 : {
8539 1334633 : if (leader != res
8540 : /* Preserve loop-closed SSA form. */
8541 1334633 : && (! lc_phi_nodes
8542 5577 : || is_gimple_min_invariant (leader)))
8543 : {
8544 1334112 : if (dump_file && (dump_flags & TDF_DETAILS))
8545 : {
8546 231 : fprintf (dump_file, "Replaced redundant PHI node "
8547 : "defining ");
8548 231 : print_generic_expr (dump_file, res);
8549 231 : fprintf (dump_file, " with ");
8550 231 : print_generic_expr (dump_file, leader);
8551 231 : fprintf (dump_file, "\n");
8552 : }
8553 1334112 : avail.eliminations++;
8554 :
8555 1334112 : if (may_propagate_copy (res, leader))
8556 : {
8557 : /* Schedule for removal. */
8558 1334112 : avail.to_remove.safe_push (phi);
8559 1334112 : continue;
8560 : }
8561 : /* ??? Else generate a copy stmt. */
8562 : }
8563 : }
8564 : }
8565 : /* Only make defs available that not already are. But make
8566 : sure loop-closed SSA PHI node defs are picked up for
8567 : downstream uses. */
8568 15011224 : if (lc_phi_nodes
8569 15011224 : || res == val
8570 15011224 : || ! avail.eliminate_avail (bb, res))
8571 11471939 : avail.eliminate_push_avail (bb, res);
8572 : }
8573 :
8574 : /* For empty BBs mark outgoing edges executable. For non-empty BBs
8575 : we do this when processing the last stmt as we have to do this
8576 : before elimination which otherwise forces GIMPLE_CONDs to
8577 : if (1 != 0) style when seeing non-executable edges. */
8578 125474824 : if (gsi_end_p (gsi_start_bb (bb)))
8579 : {
8580 14050322 : FOR_EACH_EDGE (e, ei, bb->succs)
8581 : {
8582 7025161 : if (!(e->flags & EDGE_EXECUTABLE))
8583 : {
8584 4829464 : if (dump_file && (dump_flags & TDF_DETAILS))
8585 6267 : fprintf (dump_file,
8586 : "marking outgoing edge %d -> %d executable\n",
8587 6267 : e->src->index, e->dest->index);
8588 4829464 : e->flags |= EDGE_EXECUTABLE;
8589 4829464 : e->dest->flags |= BB_EXECUTABLE;
8590 : }
8591 2195697 : else if (!(e->dest->flags & BB_EXECUTABLE))
8592 : {
8593 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8594 0 : fprintf (dump_file,
8595 : "marking destination block %d reachable\n",
8596 : e->dest->index);
8597 0 : e->dest->flags |= BB_EXECUTABLE;
8598 : }
8599 : }
8600 : }
8601 125474824 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8602 517063451 : !gsi_end_p (gsi); gsi_next (&gsi))
8603 : {
8604 454326039 : ssa_op_iter i;
8605 454326039 : tree op;
8606 454326039 : if (!bb_visited)
8607 : {
8608 515896748 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8609 : {
8610 141714273 : vn_ssa_aux_t op_info = VN_INFO (op);
8611 141714273 : gcc_assert (!op_info->visited);
8612 141714273 : op_info->valnum = VN_TOP;
8613 141714273 : op_info->visited = true;
8614 : }
8615 :
8616 : /* We somehow have to deal with uses that are not defined
8617 : in the processed region. Forcing unvisited uses to
8618 : varying here doesn't play well with def-use following during
8619 : expression simplification, so we deal with this by checking
8620 : the visited flag in SSA_VAL. */
8621 : }
8622 :
8623 454326039 : visit_stmt (gsi_stmt (gsi));
8624 :
8625 454326039 : gimple *last = gsi_stmt (gsi);
8626 454326039 : e = NULL;
8627 454326039 : switch (gimple_code (last))
8628 : {
8629 116722 : case GIMPLE_SWITCH:
8630 116722 : e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8631 116722 : (as_a <gswitch *> (last))));
8632 116722 : break;
8633 25201208 : case GIMPLE_COND:
8634 25201208 : {
8635 25201208 : tree lhs = vn_valueize (gimple_cond_lhs (last));
8636 25201208 : tree rhs = vn_valueize (gimple_cond_rhs (last));
8637 25201208 : tree_code cmpcode = gimple_cond_code (last);
8638 : /* Canonicalize the comparison if needed, putting
8639 : the constant in the rhs. */
8640 25201208 : if (tree_swap_operands_p (lhs, rhs))
8641 : {
8642 850906 : std::swap (lhs, rhs);
8643 850906 : cmpcode = swap_tree_comparison (cmpcode);
8644 : }
8645 25201208 : tree val = gimple_simplify (cmpcode,
8646 : boolean_type_node, lhs, rhs,
8647 : NULL, vn_valueize);
8648 : /* If the condition didn't simplify see if we have recorded
8649 : an expression from sofar taken edges. */
8650 25201208 : if (! val || TREE_CODE (val) != INTEGER_CST)
8651 : {
8652 23301195 : vn_nary_op_t vnresult;
8653 23301195 : tree ops[2];
8654 23301195 : ops[0] = lhs;
8655 23301195 : ops[1] = rhs;
8656 23301195 : val = vn_nary_op_lookup_pieces (2, cmpcode,
8657 : boolean_type_node, ops,
8658 : &vnresult);
8659 : /* Got back a ssa name, then try looking up `val != 0`
8660 : as it might have been recorded that way. */
8661 23301195 : if (val && TREE_CODE (val) == SSA_NAME)
8662 : {
8663 163549 : ops[0] = val;
8664 163549 : ops[1] = build_zero_cst (TREE_TYPE (val));
8665 163549 : val = vn_nary_op_lookup_pieces (2, NE_EXPR,
8666 : boolean_type_node, ops,
8667 : &vnresult);
8668 : }
8669 : /* Did we get a predicated value? */
8670 23301169 : if (! val && vnresult && vnresult->predicated_values)
8671 : {
8672 1445810 : val = vn_nary_op_get_predicated_value (vnresult, bb);
8673 1445810 : if (val && dump_file && (dump_flags & TDF_DETAILS))
8674 : {
8675 2 : fprintf (dump_file, "Got predicated value ");
8676 2 : print_generic_expr (dump_file, val, TDF_NONE);
8677 2 : fprintf (dump_file, " for ");
8678 2 : print_gimple_stmt (dump_file, last, TDF_SLIM);
8679 : }
8680 : }
8681 : }
8682 23301195 : if (val)
8683 2275694 : e = find_taken_edge (bb, val);
8684 25201208 : if (! e)
8685 : {
8686 : /* If we didn't manage to compute the taken edge then
8687 : push predicated expressions for the condition itself
8688 : and related conditions to the hashtables. This allows
8689 : simplification of redundant conditions which is
8690 : important as early cleanup. */
8691 22925514 : edge true_e, false_e;
8692 22925514 : extract_true_false_edges_from_block (bb, &true_e, &false_e);
8693 552198 : if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8694 23159862 : || !can_track_predicate_on_edge (true_e))
8695 5095249 : true_e = NULL;
8696 552198 : if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8697 23137853 : || !can_track_predicate_on_edge (false_e))
8698 6007424 : false_e = NULL;
8699 22925514 : insert_predicates_for_cond (cmpcode, lhs, rhs, true_e, false_e);
8700 : }
8701 : break;
8702 : }
8703 1402 : case GIMPLE_GOTO:
8704 1402 : e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8705 1402 : break;
8706 : default:
8707 : e = NULL;
8708 : }
8709 454326039 : if (e)
8710 : {
8711 2279384 : todo = TODO_cleanup_cfg;
8712 2279384 : if (!(e->flags & EDGE_EXECUTABLE))
8713 : {
8714 1799328 : if (dump_file && (dump_flags & TDF_DETAILS))
8715 35 : fprintf (dump_file,
8716 : "marking known outgoing %sedge %d -> %d executable\n",
8717 35 : e->flags & EDGE_DFS_BACK ? "back-" : "",
8718 35 : e->src->index, e->dest->index);
8719 1799328 : e->flags |= EDGE_EXECUTABLE;
8720 1799328 : e->dest->flags |= BB_EXECUTABLE;
8721 : }
8722 480056 : else if (!(e->dest->flags & BB_EXECUTABLE))
8723 : {
8724 27486 : if (dump_file && (dump_flags & TDF_DETAILS))
8725 1 : fprintf (dump_file,
8726 : "marking destination block %d reachable\n",
8727 : e->dest->index);
8728 27486 : e->dest->flags |= BB_EXECUTABLE;
8729 : }
8730 : }
8731 452046655 : else if (gsi_one_before_end_p (gsi))
8732 : {
8733 131069583 : FOR_EACH_EDGE (e, ei, bb->succs)
8734 : {
8735 77636716 : if (!(e->flags & EDGE_EXECUTABLE))
8736 : {
8737 57048872 : if (dump_file && (dump_flags & TDF_DETAILS))
8738 18657 : fprintf (dump_file,
8739 : "marking outgoing edge %d -> %d executable\n",
8740 18657 : e->src->index, e->dest->index);
8741 57048872 : e->flags |= EDGE_EXECUTABLE;
8742 57048872 : e->dest->flags |= BB_EXECUTABLE;
8743 : }
8744 20587844 : else if (!(e->dest->flags & BB_EXECUTABLE))
8745 : {
8746 2636456 : if (dump_file && (dump_flags & TDF_DETAILS))
8747 6077 : fprintf (dump_file,
8748 : "marking destination block %d reachable\n",
8749 : e->dest->index);
8750 2636456 : e->dest->flags |= BB_EXECUTABLE;
8751 : }
8752 : }
8753 : }
8754 :
8755 : /* Eliminate. That also pushes to avail. */
8756 454326039 : if (eliminate && ! iterate)
8757 113998189 : avail.eliminate_stmt (bb, &gsi);
8758 : else
8759 : /* If not eliminating, make all not already available defs
8760 : available. But avoid picking up dead defs. */
8761 422194471 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8762 81866621 : if (! has_zero_uses (op)
8763 81866621 : && ! avail.eliminate_avail (bb, op))
8764 62269437 : avail.eliminate_push_avail (bb, op);
8765 : }
8766 :
8767 : /* Eliminate in destination PHI arguments. Always substitute in dest
8768 : PHIs, even for non-executable edges. This handles region
8769 : exits PHIs. */
8770 62737412 : if (!iterate && eliminate)
8771 33642813 : FOR_EACH_EDGE (e, ei, bb->succs)
8772 20043524 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
8773 38934932 : !gsi_end_p (gsi); gsi_next (&gsi))
8774 : {
8775 18891408 : gphi *phi = gsi.phi ();
8776 18891408 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8777 18891408 : tree arg = USE_FROM_PTR (use_p);
8778 28696487 : if (TREE_CODE (arg) != SSA_NAME
8779 18891408 : || virtual_operand_p (arg))
8780 9805079 : continue;
8781 9086329 : tree sprime;
8782 9086329 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
8783 : {
8784 119667 : sprime = SSA_VAL (arg);
8785 119667 : gcc_assert (TREE_CODE (sprime) != SSA_NAME
8786 : || SSA_NAME_IS_DEFAULT_DEF (sprime));
8787 : }
8788 : else
8789 : /* Look for sth available at the definition block of the argument.
8790 : This avoids inconsistencies between availability there which
8791 : decides if the stmt can be removed and availability at the
8792 : use site. The SSA property ensures that things available
8793 : at the definition are also available at uses. */
8794 8966662 : sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8795 : arg);
8796 9086329 : if (sprime
8797 9086329 : && sprime != arg
8798 9086329 : && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8799 1567969 : propagate_value (use_p, sprime);
8800 : }
8801 :
8802 62737412 : vn_context_bb = NULL;
8803 62737412 : return todo;
8804 : }
8805 :
8806 : /* Unwind state per basic-block. */
8807 :
8808 : struct unwind_state
8809 : {
8810 : /* Times this block has been visited. */
8811 : unsigned visited;
8812 : /* Whether to handle this as iteration point or whether to treat
8813 : incoming backedge PHI values as varying. */
8814 : bool iterate;
8815 : /* Maximum RPO index this block is reachable from. */
8816 : int max_rpo;
8817 : /* Unwind state. */
8818 : void *ob_top;
8819 : vn_reference_t ref_top;
8820 : vn_phi_t phi_top;
8821 : vn_nary_op_t nary_top;
8822 : vn_avail *avail_top;
8823 : };
8824 :
8825 : /* Unwind the RPO VN state for iteration. */
8826 :
8827 : static void
8828 1930976 : do_unwind (unwind_state *to, rpo_elim &avail)
8829 : {
8830 1930976 : gcc_assert (to->iterate);
8831 35098263 : for (; last_inserted_nary != to->nary_top;
8832 33167287 : last_inserted_nary = last_inserted_nary->next)
8833 : {
8834 33167287 : vn_nary_op_t *slot;
8835 33167287 : slot = valid_info->nary->find_slot_with_hash
8836 33167287 : (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
8837 : /* Predication causes the need to restore previous state. */
8838 33167287 : if ((*slot)->unwind_to)
8839 6762774 : *slot = (*slot)->unwind_to;
8840 : else
8841 26404513 : valid_info->nary->clear_slot (slot);
8842 : }
8843 7495062 : for (; last_inserted_phi != to->phi_top;
8844 5564086 : last_inserted_phi = last_inserted_phi->next)
8845 : {
8846 5564086 : vn_phi_t *slot;
8847 5564086 : slot = valid_info->phis->find_slot_with_hash
8848 5564086 : (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
8849 5564086 : valid_info->phis->clear_slot (slot);
8850 : }
8851 15447516 : for (; last_inserted_ref != to->ref_top;
8852 13516540 : last_inserted_ref = last_inserted_ref->next)
8853 : {
8854 13516540 : vn_reference_t *slot;
8855 13516540 : slot = valid_info->references->find_slot_with_hash
8856 13516540 : (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
8857 13516540 : (*slot)->operands.release ();
8858 13516540 : valid_info->references->clear_slot (slot);
8859 : }
8860 1930976 : obstack_free (&vn_tables_obstack, to->ob_top);
8861 :
8862 : /* Prune [rpo_idx, ] from avail. */
8863 20738486 : for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
8864 : {
8865 18807510 : vn_ssa_aux_t val = last_pushed_avail;
8866 18807510 : vn_avail *av = val->avail;
8867 18807510 : val->avail = av->next;
8868 18807510 : last_pushed_avail = av->next_undo;
8869 18807510 : av->next = avail.m_avail_freelist;
8870 18807510 : avail.m_avail_freelist = av;
8871 : }
8872 1930976 : }
8873 :
8874 : /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8875 : If ITERATE is true then treat backedges optimistically as not
8876 : executed and iterate. If ELIMINATE is true then perform
8877 : elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8878 : is true then force PHI nodes in ENTRY->dest to VARYING. */
8879 :
8880 : static unsigned
8881 6318405 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8882 : bool iterate, bool eliminate, bool skip_entry_phis,
8883 : vn_lookup_kind kind)
8884 : {
8885 6318405 : unsigned todo = 0;
8886 6318405 : default_vn_walk_kind = kind;
8887 :
8888 : /* We currently do not support region-based iteration when
8889 : elimination is requested. */
8890 6318405 : gcc_assert (!entry || !iterate || !eliminate);
8891 : /* When iterating we need loop info up-to-date. */
8892 6318405 : gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
8893 :
8894 6318405 : bool do_region = entry != NULL;
8895 6318405 : if (!do_region)
8896 : {
8897 5625569 : entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
8898 5625569 : exit_bbs = BITMAP_ALLOC (NULL);
8899 5625569 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
8900 : }
8901 :
8902 : /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8903 : re-mark those that are contained in the region. */
8904 6318405 : edge_iterator ei;
8905 6318405 : edge e;
8906 12693972 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8907 6375567 : e->flags &= ~EDGE_DFS_BACK;
8908 :
8909 6318405 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
8910 6318405 : auto_vec<std::pair<int, int> > toplevel_scc_extents;
8911 6318405 : int n = rev_post_order_and_mark_dfs_back_seme
8912 8233719 : (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
8913 :
8914 6318405 : if (!do_region)
8915 5625569 : BITMAP_FREE (exit_bbs);
8916 :
8917 : /* If there are any non-DFS_BACK edges into entry->dest skip
8918 : processing PHI nodes for that block. This supports
8919 : value-numbering loop bodies w/o the actual loop. */
8920 12693971 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8921 6375567 : if (e != entry
8922 57162 : && !(e->flags & EDGE_DFS_BACK))
8923 : break;
8924 6318405 : if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
8925 0 : fprintf (dump_file, "Region does not contain all edges into "
8926 : "the entry block, skipping its PHIs.\n");
8927 6318405 : skip_entry_phis |= e != NULL;
8928 :
8929 6318405 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
8930 64289602 : for (int i = 0; i < n; ++i)
8931 51652792 : bb_to_rpo[rpo[i]] = i;
8932 6318405 : vn_bb_to_rpo = bb_to_rpo;
8933 :
8934 6318405 : unwind_state *rpo_state = XNEWVEC (unwind_state, n);
8935 :
8936 6318405 : rpo_elim avail (entry->dest);
8937 6318405 : rpo_avail = &avail;
8938 :
8939 : /* Verify we have no extra entries into the region. */
8940 6318405 : if (flag_checking && do_region)
8941 : {
8942 692830 : auto_bb_flag bb_in_region (fn);
8943 2816373 : for (int i = 0; i < n; ++i)
8944 : {
8945 1430713 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8946 1430713 : bb->flags |= bb_in_region;
8947 : }
8948 : /* We can't merge the first two loops because we cannot rely
8949 : on EDGE_DFS_BACK for edges not within the region. But if
8950 : we decide to always have the bb_in_region flag we can
8951 : do the checking during the RPO walk itself (but then it's
8952 : also easy to handle MEME conservatively). */
8953 2123543 : for (int i = 0; i < n; ++i)
8954 : {
8955 1430713 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8956 1430713 : edge e;
8957 1430713 : edge_iterator ei;
8958 3128843 : FOR_EACH_EDGE (e, ei, bb->preds)
8959 1698130 : gcc_assert (e == entry
8960 : || (skip_entry_phis && bb == entry->dest)
8961 : || (e->src->flags & bb_in_region));
8962 : }
8963 2123543 : for (int i = 0; i < n; ++i)
8964 : {
8965 1430713 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8966 1430713 : bb->flags &= ~bb_in_region;
8967 : }
8968 692830 : }
8969 :
8970 : /* Create the VN state. For the initial size of the various hashtables
8971 : use a heuristic based on region size and number of SSA names. */
8972 6318405 : unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
8973 6318405 : / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
8974 6318405 : VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
8975 6318405 : next_value_id = 1;
8976 6318405 : next_constant_value_id = -1;
8977 :
8978 6318405 : vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
8979 6318405 : gcc_obstack_init (&vn_ssa_aux_obstack);
8980 :
8981 6318405 : gcc_obstack_init (&vn_tables_obstack);
8982 6318405 : gcc_obstack_init (&vn_tables_insert_obstack);
8983 6318405 : valid_info = XCNEW (struct vn_tables_s);
8984 6318405 : allocate_vn_table (valid_info, region_size);
8985 6318405 : last_inserted_ref = NULL;
8986 6318405 : last_inserted_phi = NULL;
8987 6318405 : last_inserted_nary = NULL;
8988 6318405 : last_pushed_avail = NULL;
8989 :
8990 6318405 : vn_valueize = rpo_vn_valueize;
8991 :
8992 : /* Initialize the unwind state and edge/BB executable state. */
8993 6318405 : unsigned curr_scc = 0;
8994 57971197 : for (int i = 0; i < n; ++i)
8995 : {
8996 51652792 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8997 51652792 : rpo_state[i].visited = 0;
8998 51652792 : rpo_state[i].max_rpo = i;
8999 60301164 : if (!iterate && curr_scc < toplevel_scc_extents.length ())
9000 : {
9001 7216202 : if (i >= toplevel_scc_extents[curr_scc].first
9002 7216202 : && i <= toplevel_scc_extents[curr_scc].second)
9003 3920336 : rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
9004 7216202 : if (i == toplevel_scc_extents[curr_scc].second)
9005 736100 : curr_scc++;
9006 : }
9007 51652792 : bb->flags &= ~BB_EXECUTABLE;
9008 51652792 : bool has_backedges = false;
9009 51652792 : edge e;
9010 51652792 : edge_iterator ei;
9011 122570350 : FOR_EACH_EDGE (e, ei, bb->preds)
9012 : {
9013 70917558 : if (e->flags & EDGE_DFS_BACK)
9014 2876320 : has_backedges = true;
9015 70917558 : e->flags &= ~EDGE_EXECUTABLE;
9016 70917558 : if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
9017 70917558 : continue;
9018 : }
9019 51652792 : rpo_state[i].iterate = iterate && has_backedges;
9020 : }
9021 6318405 : entry->flags |= EDGE_EXECUTABLE;
9022 6318405 : entry->dest->flags |= BB_EXECUTABLE;
9023 :
9024 : /* As heuristic to improve compile-time we handle only the N innermost
9025 : loops and the outermost one optimistically. */
9026 6318405 : if (iterate)
9027 : {
9028 4403091 : unsigned max_depth = param_rpo_vn_max_loop_depth;
9029 14784039 : for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
9030 1577184 : if (loop_depth (loop) > max_depth)
9031 2108 : for (unsigned i = 2;
9032 9052 : i < loop_depth (loop) - max_depth; ++i)
9033 : {
9034 2108 : basic_block header = superloop_at_depth (loop, i)->header;
9035 2108 : bool non_latch_backedge = false;
9036 2108 : edge e;
9037 2108 : edge_iterator ei;
9038 6355 : FOR_EACH_EDGE (e, ei, header->preds)
9039 4247 : if (e->flags & EDGE_DFS_BACK)
9040 : {
9041 : /* There can be a non-latch backedge into the header
9042 : which is part of an outer irreducible region. We
9043 : cannot avoid iterating this block then. */
9044 2139 : if (!dominated_by_p (CDI_DOMINATORS,
9045 2139 : e->src, e->dest))
9046 : {
9047 12 : if (dump_file && (dump_flags & TDF_DETAILS))
9048 0 : fprintf (dump_file, "non-latch backedge %d -> %d "
9049 : "forces iteration of loop %d\n",
9050 0 : e->src->index, e->dest->index, loop->num);
9051 : non_latch_backedge = true;
9052 : }
9053 : else
9054 2127 : e->flags |= EDGE_EXECUTABLE;
9055 : }
9056 2108 : rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
9057 4403091 : }
9058 : }
9059 :
9060 6318405 : uint64_t nblk = 0;
9061 6318405 : int idx = 0;
9062 4403091 : if (iterate)
9063 : /* Go and process all blocks, iterating as necessary. */
9064 50006545 : do
9065 : {
9066 50006545 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
9067 :
9068 : /* If the block has incoming backedges remember unwind state. This
9069 : is required even for non-executable blocks since in irreducible
9070 : regions we might reach them via the backedge and re-start iterating
9071 : from there.
9072 : Note we can individually mark blocks with incoming backedges to
9073 : not iterate where we then handle PHIs conservatively. We do that
9074 : heuristically to reduce compile-time for degenerate cases. */
9075 50006545 : if (rpo_state[idx].iterate)
9076 : {
9077 4434873 : rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
9078 4434873 : rpo_state[idx].ref_top = last_inserted_ref;
9079 4434873 : rpo_state[idx].phi_top = last_inserted_phi;
9080 4434873 : rpo_state[idx].nary_top = last_inserted_nary;
9081 4434873 : rpo_state[idx].avail_top
9082 4434873 : = last_pushed_avail ? last_pushed_avail->avail : NULL;
9083 : }
9084 :
9085 50006545 : if (!(bb->flags & BB_EXECUTABLE))
9086 : {
9087 977488 : if (dump_file && (dump_flags & TDF_DETAILS))
9088 2 : fprintf (dump_file, "Block %d: BB%d found not executable\n",
9089 : idx, bb->index);
9090 977488 : idx++;
9091 2908464 : continue;
9092 : }
9093 :
9094 49029057 : if (dump_file && (dump_flags & TDF_DETAILS))
9095 334 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
9096 49029057 : nblk++;
9097 98058114 : todo |= process_bb (avail, bb,
9098 49029057 : rpo_state[idx].visited != 0,
9099 : rpo_state[idx].iterate,
9100 : iterate, eliminate, do_region, exit_bbs, false);
9101 49029057 : rpo_state[idx].visited++;
9102 :
9103 : /* Verify if changed values flow over executable outgoing backedges
9104 : and those change destination PHI values (that's the thing we
9105 : can easily verify). Reduce over all such edges to the farthest
9106 : away PHI. */
9107 49029057 : int iterate_to = -1;
9108 49029057 : edge_iterator ei;
9109 49029057 : edge e;
9110 118088646 : FOR_EACH_EDGE (e, ei, bb->succs)
9111 69059589 : if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
9112 : == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
9113 4439449 : && rpo_state[bb_to_rpo[e->dest->index]].iterate)
9114 : {
9115 4436683 : int destidx = bb_to_rpo[e->dest->index];
9116 4436683 : if (!rpo_state[destidx].visited)
9117 : {
9118 134 : if (dump_file && (dump_flags & TDF_DETAILS))
9119 0 : fprintf (dump_file, "Unvisited destination %d\n",
9120 : e->dest->index);
9121 134 : if (iterate_to == -1 || destidx < iterate_to)
9122 134 : iterate_to = destidx;
9123 134 : continue;
9124 : }
9125 4436549 : if (dump_file && (dump_flags & TDF_DETAILS))
9126 53 : fprintf (dump_file, "Looking for changed values of backedge"
9127 : " %d->%d destination PHIs\n",
9128 53 : e->src->index, e->dest->index);
9129 4436549 : vn_context_bb = e->dest;
9130 4436549 : gphi_iterator gsi;
9131 4436549 : for (gsi = gsi_start_phis (e->dest);
9132 10087143 : !gsi_end_p (gsi); gsi_next (&gsi))
9133 : {
9134 7582373 : bool inserted = false;
9135 : /* While we'd ideally just iterate on value changes
9136 : we CSE PHIs and do that even across basic-block
9137 : boundaries. So even hashtable state changes can
9138 : be important (which is roughly equivalent to
9139 : PHI argument value changes). To not excessively
9140 : iterate because of that we track whether a PHI
9141 : was CSEd to with GF_PLF_1. */
9142 7582373 : bool phival_changed;
9143 7582373 : if ((phival_changed = visit_phi (gsi.phi (),
9144 : &inserted, false))
9145 8989051 : || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
9146 : {
9147 1931779 : if (!phival_changed
9148 1931779 : && dump_file && (dump_flags & TDF_DETAILS))
9149 0 : fprintf (dump_file, "PHI was CSEd and hashtable "
9150 : "state (changed)\n");
9151 1931779 : if (iterate_to == -1 || destidx < iterate_to)
9152 1931694 : iterate_to = destidx;
9153 1931779 : break;
9154 : }
9155 : }
9156 4436549 : vn_context_bb = NULL;
9157 : }
9158 49029057 : if (iterate_to != -1)
9159 : {
9160 1930976 : do_unwind (&rpo_state[iterate_to], avail);
9161 1930976 : idx = iterate_to;
9162 1930976 : if (dump_file && (dump_flags & TDF_DETAILS))
9163 20 : fprintf (dump_file, "Iterating to %d BB%d\n",
9164 20 : iterate_to, rpo[iterate_to]);
9165 1930976 : continue;
9166 : }
9167 :
9168 47098081 : idx++;
9169 : }
9170 50006545 : while (idx < n);
9171 :
9172 : else /* !iterate */
9173 : {
9174 : /* Process all blocks greedily with a worklist that enforces RPO
9175 : processing of reachable blocks. */
9176 1915314 : auto_bitmap worklist;
9177 1915314 : bitmap_set_bit (worklist, 0);
9178 17538983 : while (!bitmap_empty_p (worklist))
9179 : {
9180 13708355 : int idx = bitmap_clear_first_set_bit (worklist);
9181 13708355 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
9182 13708355 : gcc_assert ((bb->flags & BB_EXECUTABLE)
9183 : && !rpo_state[idx].visited);
9184 :
9185 13708355 : if (dump_file && (dump_flags & TDF_DETAILS))
9186 35522 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
9187 :
9188 : /* When we run into predecessor edges where we cannot trust its
9189 : executable state mark them executable so PHI processing will
9190 : be conservative.
9191 : ??? Do we need to force arguments flowing over that edge
9192 : to be varying or will they even always be? */
9193 13708355 : edge_iterator ei;
9194 13708355 : edge e;
9195 33236288 : FOR_EACH_EDGE (e, ei, bb->preds)
9196 19527933 : if (!(e->flags & EDGE_EXECUTABLE)
9197 1028748 : && (bb == entry->dest
9198 974932 : || (!rpo_state[bb_to_rpo[e->src->index]].visited
9199 936148 : && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
9200 : >= (int)idx))))
9201 : {
9202 966060 : if (dump_file && (dump_flags & TDF_DETAILS))
9203 11418 : fprintf (dump_file, "Cannot trust state of predecessor "
9204 : "edge %d -> %d, marking executable\n",
9205 11418 : e->src->index, e->dest->index);
9206 966060 : e->flags |= EDGE_EXECUTABLE;
9207 : }
9208 :
9209 13708355 : nblk++;
9210 27416710 : todo |= process_bb (avail, bb, false, false, false, eliminate,
9211 : do_region, exit_bbs,
9212 51594 : skip_entry_phis && bb == entry->dest);
9213 13708355 : rpo_state[idx].visited++;
9214 :
9215 33884541 : FOR_EACH_EDGE (e, ei, bb->succs)
9216 20176186 : if ((e->flags & EDGE_EXECUTABLE)
9217 20095653 : && e->dest->index != EXIT_BLOCK
9218 18903072 : && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
9219 37720639 : && !rpo_state[bb_to_rpo[e->dest->index]].visited)
9220 16583871 : bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
9221 : }
9222 1915314 : }
9223 :
9224 : /* If statistics or dump file active. */
9225 6318405 : int nex = 0;
9226 6318405 : unsigned max_visited = 1;
9227 57971197 : for (int i = 0; i < n; ++i)
9228 : {
9229 51652792 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9230 51652792 : if (bb->flags & BB_EXECUTABLE)
9231 51032870 : nex++;
9232 51652792 : statistics_histogram_event (cfun, "RPO block visited times",
9233 51652792 : rpo_state[i].visited);
9234 51652792 : if (rpo_state[i].visited > max_visited)
9235 : max_visited = rpo_state[i].visited;
9236 : }
9237 6318405 : unsigned nvalues = 0, navail = 0;
9238 174436871 : for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
9239 174436871 : i != vn_ssa_aux_hash->end (); ++i)
9240 : {
9241 168118466 : nvalues++;
9242 168118466 : vn_avail *av = (*i)->avail;
9243 248452987 : while (av)
9244 : {
9245 80334521 : navail++;
9246 80334521 : av = av->next;
9247 : }
9248 : }
9249 6318405 : statistics_counter_event (cfun, "RPO blocks", n);
9250 6318405 : statistics_counter_event (cfun, "RPO blocks visited", nblk);
9251 6318405 : statistics_counter_event (cfun, "RPO blocks executable", nex);
9252 6318405 : statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
9253 6318405 : statistics_histogram_event (cfun, "RPO num values", nvalues);
9254 6318405 : statistics_histogram_event (cfun, "RPO num avail", navail);
9255 6318405 : statistics_histogram_event (cfun, "RPO num lattice",
9256 6318405 : vn_ssa_aux_hash->elements ());
9257 6318405 : if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
9258 : {
9259 11323 : fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
9260 : " blocks in total discovering %d executable blocks iterating "
9261 : "%d.%d times, a block was visited max. %u times\n",
9262 : n, nblk, nex,
9263 11323 : (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
9264 : max_visited);
9265 11323 : fprintf (dump_file, "RPO tracked %d values available at %d locations "
9266 : "and %" PRIu64 " lattice elements\n",
9267 11323 : nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
9268 : }
9269 :
9270 6318405 : if (eliminate)
9271 : {
9272 : /* When !iterate we already performed elimination during the RPO
9273 : walk. */
9274 5315490 : if (iterate)
9275 : {
9276 : /* Elimination for region-based VN needs to be done within the
9277 : RPO walk. */
9278 3419779 : gcc_assert (! do_region);
9279 : /* Note we can't use avail.walk here because that gets confused
9280 : by the existing availability and it will be less efficient
9281 : as well. */
9282 3419779 : todo |= eliminate_with_rpo_vn (NULL);
9283 : }
9284 : else
9285 1895711 : todo |= avail.eliminate_cleanup (do_region);
9286 : }
9287 :
9288 6318405 : vn_valueize = NULL;
9289 6318405 : rpo_avail = NULL;
9290 6318405 : vn_bb_to_rpo = NULL;
9291 :
9292 6318405 : XDELETEVEC (bb_to_rpo);
9293 6318405 : XDELETEVEC (rpo);
9294 6318405 : XDELETEVEC (rpo_state);
9295 :
9296 6318405 : return todo;
9297 6318405 : }
9298 :
9299 : /* Region-based entry for RPO VN. Performs value-numbering and elimination
9300 : on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
9301 : the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
9302 : are not considered.
9303 : If ITERATE is true then treat backedges optimistically as not
9304 : executed and iterate. If ELIMINATE is true then perform
9305 : elimination, otherwise leave that to the caller.
9306 : If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
9307 : KIND specifies the amount of work done for handling memory operations. */
9308 :
9309 : unsigned
9310 712439 : do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
9311 : bool iterate, bool eliminate, bool skip_entry_phis,
9312 : vn_lookup_kind kind)
9313 : {
9314 712439 : auto_timevar tv (TV_TREE_RPO_VN);
9315 712439 : unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
9316 : skip_entry_phis, kind);
9317 712439 : free_rpo_vn ();
9318 1424878 : return todo;
9319 712439 : }
9320 :
9321 :
9322 : namespace {
9323 :
9324 : const pass_data pass_data_fre =
9325 : {
9326 : GIMPLE_PASS, /* type */
9327 : "fre", /* name */
9328 : OPTGROUP_NONE, /* optinfo_flags */
9329 : TV_TREE_FRE, /* tv_id */
9330 : ( PROP_cfg | PROP_ssa ), /* properties_required */
9331 : 0, /* properties_provided */
9332 : 0, /* properties_destroyed */
9333 : 0, /* todo_flags_start */
9334 : 0, /* todo_flags_finish */
9335 : };
9336 :
9337 : class pass_fre : public gimple_opt_pass
9338 : {
9339 : public:
9340 1472935 : pass_fre (gcc::context *ctxt)
9341 2945870 : : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
9342 : {}
9343 :
9344 : /* opt_pass methods: */
9345 1178348 : opt_pass * clone () final override { return new pass_fre (m_ctxt); }
9346 1472935 : void set_pass_param (unsigned int n, bool param) final override
9347 : {
9348 1472935 : gcc_assert (n == 0);
9349 1472935 : may_iterate = param;
9350 1472935 : }
9351 4703238 : bool gate (function *) final override
9352 : {
9353 4703238 : return flag_tree_fre != 0 && (may_iterate || optimize > 1);
9354 : }
9355 : unsigned int execute (function *) final override;
9356 :
9357 : private:
9358 : bool may_iterate;
9359 : }; // class pass_fre
9360 :
9361 : unsigned int
9362 4622654 : pass_fre::execute (function *fun)
9363 : {
9364 4622654 : unsigned todo = 0;
9365 :
9366 : /* At -O[1g] use the cheap non-iterating mode. */
9367 4622654 : bool iterate_p = may_iterate && (optimize > 1);
9368 4622654 : calculate_dominance_info (CDI_DOMINATORS);
9369 4622654 : if (iterate_p)
9370 3419779 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
9371 :
9372 4622654 : todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
9373 4622654 : free_rpo_vn ();
9374 :
9375 4622654 : if (iterate_p)
9376 3419779 : loop_optimizer_finalize ();
9377 :
9378 4622654 : if (scev_initialized_p ())
9379 32338 : scev_reset_htab ();
9380 :
9381 : /* For late FRE after IVOPTs and unrolling, see if we can
9382 : remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
9383 4622654 : if (!may_iterate)
9384 1015432 : todo |= TODO_update_address_taken;
9385 :
9386 4622654 : return todo;
9387 : }
9388 :
9389 : } // anon namespace
9390 :
9391 : gimple_opt_pass *
9392 294587 : make_pass_fre (gcc::context *ctxt)
9393 : {
9394 294587 : return new pass_fre (ctxt);
9395 : }
9396 :
9397 : #undef BB_EXECUTABLE
|