Branch data Line data Source code
1 : : /* SCC value numbering for trees
2 : : Copyright (C) 2006-2025 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 "gimplify.h"
46 : : #include "flags.h"
47 : : #include "dojump.h"
48 : : #include "explow.h"
49 : : #include "calls.h"
50 : : #include "varasm.h"
51 : : #include "stmt.h"
52 : : #include "expr.h"
53 : : #include "tree-dfa.h"
54 : : #include "tree-ssa.h"
55 : : #include "dumpfile.h"
56 : : #include "cfgloop.h"
57 : : #include "tree-ssa-propagate.h"
58 : : #include "tree-cfg.h"
59 : : #include "domwalk.h"
60 : : #include "gimple-match.h"
61 : : #include "stringpool.h"
62 : : #include "attribs.h"
63 : : #include "tree-pass.h"
64 : : #include "statistics.h"
65 : : #include "langhooks.h"
66 : : #include "ipa-utils.h"
67 : : #include "dbgcnt.h"
68 : : #include "tree-cfgcleanup.h"
69 : : #include "tree-ssa-loop.h"
70 : : #include "tree-scalar-evolution.h"
71 : : #include "tree-ssa-loop-niter.h"
72 : : #include "builtins.h"
73 : : #include "fold-const-call.h"
74 : : #include "ipa-modref-tree.h"
75 : : #include "ipa-modref.h"
76 : : #include "tree-ssa-sccvn.h"
77 : : #include "alloc-pool.h"
78 : : #include "symbol-summary.h"
79 : : #include "sreal.h"
80 : : #include "ipa-cp.h"
81 : : #include "ipa-prop.h"
82 : : #include "target.h"
83 : :
84 : : /* This algorithm is based on the SCC algorithm presented by Keith
85 : : Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
86 : : (http://citeseer.ist.psu.edu/41805.html). In
87 : : straight line code, it is equivalent to a regular hash based value
88 : : numbering that is performed in reverse postorder.
89 : :
90 : : For code with cycles, there are two alternatives, both of which
91 : : require keeping the hashtables separate from the actual list of
92 : : value numbers for SSA names.
93 : :
94 : : 1. Iterate value numbering in an RPO walk of the blocks, removing
95 : : all the entries from the hashtable after each iteration (but
96 : : keeping the SSA name->value number mapping between iterations).
97 : : Iterate until it does not change.
98 : :
99 : : 2. Perform value numbering as part of an SCC walk on the SSA graph,
100 : : iterating only the cycles in the SSA graph until they do not change
101 : : (using a separate, optimistic hashtable for value numbering the SCC
102 : : operands).
103 : :
104 : : The second is not just faster in practice (because most SSA graph
105 : : cycles do not involve all the variables in the graph), it also has
106 : : some nice properties.
107 : :
108 : : One of these nice properties is that when we pop an SCC off the
109 : : stack, we are guaranteed to have processed all the operands coming from
110 : : *outside of that SCC*, so we do not need to do anything special to
111 : : ensure they have value numbers.
112 : :
113 : : Another nice property is that the SCC walk is done as part of a DFS
114 : : of the SSA graph, which makes it easy to perform combining and
115 : : simplifying operations at the same time.
116 : :
117 : : The code below is deliberately written in a way that makes it easy
118 : : to separate the SCC walk from the other work it does.
119 : :
120 : : In order to propagate constants through the code, we track which
121 : : expressions contain constants, and use those while folding. In
122 : : theory, we could also track expressions whose value numbers are
123 : : replaced, in case we end up folding based on expression
124 : : identities.
125 : :
126 : : In order to value number memory, we assign value numbers to vuses.
127 : : This enables us to note that, for example, stores to the same
128 : : address of the same value from the same starting memory states are
129 : : equivalent.
130 : : TODO:
131 : :
132 : : 1. We can iterate only the changing portions of the SCC's, but
133 : : I have not seen an SCC big enough for this to be a win.
134 : : 2. If you differentiate between phi nodes for loops and phi nodes
135 : : for if-then-else, you can properly consider phi nodes in different
136 : : blocks for equivalence.
137 : : 3. We could value number vuses in more cases, particularly, whole
138 : : structure copies.
139 : : */
140 : :
141 : : /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
142 : : #define BB_EXECUTABLE BB_VISITED
143 : :
144 : : static vn_lookup_kind default_vn_walk_kind;
145 : :
146 : : /* vn_nary_op hashtable helpers. */
147 : :
148 : : struct vn_nary_op_hasher : nofree_ptr_hash <vn_nary_op_s>
149 : : {
150 : : typedef vn_nary_op_s *compare_type;
151 : : static inline hashval_t hash (const vn_nary_op_s *);
152 : : static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
153 : : };
154 : :
155 : : /* Return the computed hashcode for nary operation P1. */
156 : :
157 : : inline hashval_t
158 : 777302724 : vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
159 : : {
160 : 777302724 : return vno1->hashcode;
161 : : }
162 : :
163 : : /* Compare nary operations P1 and P2 and return true if they are
164 : : equivalent. */
165 : :
166 : : inline bool
167 : 984250481 : vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
168 : : {
169 : 984250481 : return vno1 == vno2 || vn_nary_op_eq (vno1, vno2);
170 : : }
171 : :
172 : : typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
173 : : typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
174 : :
175 : :
176 : : /* vn_phi hashtable helpers. */
177 : :
178 : : static int
179 : : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
180 : :
181 : : struct vn_phi_hasher : nofree_ptr_hash <vn_phi_s>
182 : : {
183 : : static inline hashval_t hash (const vn_phi_s *);
184 : : static inline bool equal (const vn_phi_s *, const vn_phi_s *);
185 : : };
186 : :
187 : : /* Return the computed hashcode for phi operation P1. */
188 : :
189 : : inline hashval_t
190 : 26267477 : vn_phi_hasher::hash (const vn_phi_s *vp1)
191 : : {
192 : 26267477 : return vp1->hashcode;
193 : : }
194 : :
195 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
196 : :
197 : : inline bool
198 : 47337035 : vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
199 : : {
200 : 47337035 : return vp1 == vp2 || vn_phi_eq (vp1, vp2);
201 : : }
202 : :
203 : : typedef hash_table<vn_phi_hasher> vn_phi_table_type;
204 : : typedef vn_phi_table_type::iterator vn_phi_iterator_type;
205 : :
206 : :
207 : : /* Compare two reference operands P1 and P2 for equality. Return true if
208 : : they are equal, and false otherwise. */
209 : :
210 : : static int
211 : 23782880 : vn_reference_op_eq (const void *p1, const void *p2)
212 : : {
213 : 23782880 : const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
214 : 23782880 : const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
215 : :
216 : 23782880 : return (vro1->opcode == vro2->opcode
217 : : /* We do not care for differences in type qualification. */
218 : 23781008 : && (vro1->type == vro2->type
219 : 1123482 : || (vro1->type && vro2->type
220 : 1123482 : && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
221 : 1123482 : TYPE_MAIN_VARIANT (vro2->type))))
222 : 22802924 : && expressions_equal_p (vro1->op0, vro2->op0)
223 : 22786263 : && expressions_equal_p (vro1->op1, vro2->op1)
224 : 22786263 : && expressions_equal_p (vro1->op2, vro2->op2)
225 : 46569143 : && (vro1->opcode != CALL_EXPR || vro1->clique == vro2->clique));
226 : : }
227 : :
228 : : /* Free a reference operation structure VP. */
229 : :
230 : : static inline void
231 : 0 : free_reference (vn_reference_s *vr)
232 : : {
233 : 0 : vr->operands.release ();
234 : : }
235 : :
236 : :
237 : : /* vn_reference hashtable helpers. */
238 : :
239 : : struct vn_reference_hasher : nofree_ptr_hash <vn_reference_s>
240 : : {
241 : : static inline hashval_t hash (const vn_reference_s *);
242 : : static inline bool equal (const vn_reference_s *, const vn_reference_s *);
243 : : };
244 : :
245 : : /* Return the hashcode for a given reference operation P1. */
246 : :
247 : : inline hashval_t
248 : 3624206113 : vn_reference_hasher::hash (const vn_reference_s *vr1)
249 : : {
250 : 3624206113 : return vr1->hashcode;
251 : : }
252 : :
253 : : inline bool
254 : 4341058398 : vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
255 : : {
256 : 4341058398 : return v == c || vn_reference_eq (v, c);
257 : : }
258 : :
259 : : typedef hash_table<vn_reference_hasher> vn_reference_table_type;
260 : : typedef vn_reference_table_type::iterator vn_reference_iterator_type;
261 : :
262 : : /* Pretty-print OPS to OUTFILE. */
263 : :
264 : : void
265 : 317 : print_vn_reference_ops (FILE *outfile, const vec<vn_reference_op_s> ops)
266 : : {
267 : 317 : vn_reference_op_t vro;
268 : 317 : unsigned int i;
269 : 317 : fprintf (outfile, "{");
270 : 1394 : for (i = 0; ops.iterate (i, &vro); i++)
271 : : {
272 : 1077 : bool closebrace = false;
273 : 1077 : if (vro->opcode != SSA_NAME
274 : 863 : && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
275 : : {
276 : 863 : fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
277 : 863 : if (vro->op0 || vro->opcode == CALL_EXPR)
278 : : {
279 : 863 : fprintf (outfile, "<");
280 : 863 : closebrace = true;
281 : : }
282 : : }
283 : 1077 : if (vro->op0 || vro->opcode == CALL_EXPR)
284 : : {
285 : 1077 : if (!vro->op0)
286 : 0 : fprintf (outfile, internal_fn_name ((internal_fn)vro->clique));
287 : : else
288 : 1077 : print_generic_expr (outfile, vro->op0);
289 : 1077 : if (vro->op1)
290 : : {
291 : 185 : fprintf (outfile, ",");
292 : 185 : print_generic_expr (outfile, vro->op1);
293 : : }
294 : 1077 : if (vro->op2)
295 : : {
296 : 185 : fprintf (outfile, ",");
297 : 185 : print_generic_expr (outfile, vro->op2);
298 : : }
299 : : }
300 : 1077 : if (closebrace)
301 : 863 : fprintf (outfile, ">");
302 : 1077 : if (i != ops.length () - 1)
303 : 760 : fprintf (outfile, ",");
304 : : }
305 : 317 : fprintf (outfile, "}");
306 : 317 : }
307 : :
308 : : DEBUG_FUNCTION void
309 : 0 : debug_vn_reference_ops (const vec<vn_reference_op_s> ops)
310 : : {
311 : 0 : print_vn_reference_ops (stderr, ops);
312 : 0 : fputc ('\n', stderr);
313 : 0 : }
314 : :
315 : : /* The set of VN hashtables. */
316 : :
317 : : typedef struct vn_tables_s
318 : : {
319 : : vn_nary_op_table_type *nary;
320 : : vn_phi_table_type *phis;
321 : : vn_reference_table_type *references;
322 : : } *vn_tables_t;
323 : :
324 : :
325 : : /* vn_constant hashtable helpers. */
326 : :
327 : : struct vn_constant_hasher : free_ptr_hash <vn_constant_s>
328 : : {
329 : : static inline hashval_t hash (const vn_constant_s *);
330 : : static inline bool equal (const vn_constant_s *, const vn_constant_s *);
331 : : };
332 : :
333 : : /* Hash table hash function for vn_constant_t. */
334 : :
335 : : inline hashval_t
336 : 12205829 : vn_constant_hasher::hash (const vn_constant_s *vc1)
337 : : {
338 : 12205829 : return vc1->hashcode;
339 : : }
340 : :
341 : : /* Hash table equality function for vn_constant_t. */
342 : :
343 : : inline bool
344 : 14745595 : vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
345 : : {
346 : 14745595 : if (vc1->hashcode != vc2->hashcode)
347 : : return false;
348 : :
349 : 2232621 : return vn_constant_eq_with_type (vc1->constant, vc2->constant);
350 : : }
351 : :
352 : : static hash_table<vn_constant_hasher> *constant_to_value_id;
353 : :
354 : :
355 : : /* Obstack we allocate the vn-tables elements from. */
356 : : static obstack vn_tables_obstack;
357 : : /* Special obstack we never unwind. */
358 : : static obstack vn_tables_insert_obstack;
359 : :
360 : : static vn_reference_t last_inserted_ref;
361 : : static vn_phi_t last_inserted_phi;
362 : : static vn_nary_op_t last_inserted_nary;
363 : : static vn_ssa_aux_t last_pushed_avail;
364 : :
365 : : /* Valid hashtables storing information we have proven to be
366 : : correct. */
367 : : static vn_tables_t valid_info;
368 : :
369 : : /* Global RPO state for access from hooks. */
370 : : static class eliminate_dom_walker *rpo_avail;
371 : : basic_block vn_context_bb;
372 : :
373 : :
374 : : /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
375 : : an SSA name, otherwise just return it. */
376 : : tree (*vn_valueize) (tree);
377 : : static tree
378 : 83456 : vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
379 : : {
380 : 83456 : basic_block saved_vn_context_bb = vn_context_bb;
381 : : /* Look for sth available at the definition block of the argument.
382 : : This avoids inconsistencies between availability there which
383 : : decides if the stmt can be removed and availability at the
384 : : use site. The SSA property ensures that things available
385 : : at the definition are also available at uses. */
386 : 83456 : if (!SSA_NAME_IS_DEFAULT_DEF (t))
387 : 79606 : vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
388 : 83456 : tree res = vn_valueize (t);
389 : 83456 : vn_context_bb = saved_vn_context_bb;
390 : 83456 : return res;
391 : : }
392 : :
393 : :
394 : : /* This represents the top of the VN lattice, which is the universal
395 : : value. */
396 : :
397 : : tree VN_TOP;
398 : :
399 : : /* Unique counter for our value ids. */
400 : :
401 : : static unsigned int next_value_id;
402 : : static int next_constant_value_id;
403 : :
404 : :
405 : : /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
406 : : are allocated on an obstack for locality reasons, and to free them
407 : : without looping over the vec. */
408 : :
409 : : struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
410 : : {
411 : : typedef vn_ssa_aux_t value_type;
412 : : typedef tree compare_type;
413 : : static inline hashval_t hash (const value_type &);
414 : : static inline bool equal (const value_type &, const compare_type &);
415 : : static inline void mark_deleted (value_type &) {}
416 : : static const bool empty_zero_p = true;
417 : 0 : static inline void mark_empty (value_type &e) { e = NULL; }
418 : : static inline bool is_deleted (value_type &) { return false; }
419 : >13158*10^7 : static inline bool is_empty (value_type &e) { return e == NULL; }
420 : : };
421 : :
422 : : hashval_t
423 : 43309589139 : vn_ssa_aux_hasher::hash (const value_type &entry)
424 : : {
425 : 43309589139 : return SSA_NAME_VERSION (entry->name);
426 : : }
427 : :
428 : : bool
429 : 49535184901 : vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
430 : : {
431 : 49535184901 : return name == entry->name;
432 : : }
433 : :
434 : : static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
435 : : typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
436 : : static struct obstack vn_ssa_aux_obstack;
437 : :
438 : : static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
439 : : static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
440 : : vn_nary_op_table_type *);
441 : : static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
442 : : enum tree_code, tree, tree *);
443 : : static tree vn_lookup_simplify_result (gimple_match_op *);
444 : : static vn_reference_t vn_reference_lookup_or_insert_for_pieces
445 : : (tree, alias_set_type, alias_set_type, poly_int64, poly_int64, tree,
446 : : vec<vn_reference_op_s, va_heap>, tree);
447 : :
448 : : /* Return whether there is value numbering information for a given SSA name. */
449 : :
450 : : bool
451 : 5248429 : has_VN_INFO (tree name)
452 : : {
453 : 5248429 : return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
454 : : }
455 : :
456 : : vn_ssa_aux_t
457 : 3716785323 : VN_INFO (tree name)
458 : : {
459 : 3716785323 : vn_ssa_aux_t *res
460 : 3716785323 : = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
461 : : INSERT);
462 : 3716785323 : if (*res != NULL)
463 : : return *res;
464 : :
465 : 173355924 : vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
466 : 173355924 : memset (newinfo, 0, sizeof (struct vn_ssa_aux));
467 : 173355924 : newinfo->name = name;
468 : 173355924 : newinfo->valnum = VN_TOP;
469 : : /* We are using the visited flag to handle uses with defs not within the
470 : : region being value-numbered. */
471 : 173355924 : newinfo->visited = false;
472 : :
473 : : /* Given we create the VN_INFOs on-demand now we have to do initialization
474 : : different than VN_TOP here. */
475 : 173355924 : if (SSA_NAME_IS_DEFAULT_DEF (name))
476 : 9401482 : switch (TREE_CODE (SSA_NAME_VAR (name)))
477 : : {
478 : 1683034 : case VAR_DECL:
479 : : /* All undefined vars are VARYING. */
480 : 1683034 : newinfo->valnum = name;
481 : 1683034 : newinfo->visited = true;
482 : 1683034 : break;
483 : :
484 : 7656382 : case PARM_DECL:
485 : : /* Parameters are VARYING but we can record a condition
486 : : if we know it is a non-NULL pointer. */
487 : 7656382 : newinfo->visited = true;
488 : 7656382 : newinfo->valnum = name;
489 : 11742576 : if (POINTER_TYPE_P (TREE_TYPE (name))
490 : 8820327 : && nonnull_arg_p (SSA_NAME_VAR (name)))
491 : : {
492 : 2425680 : tree ops[2];
493 : 2425680 : ops[0] = name;
494 : 2425680 : ops[1] = build_int_cst (TREE_TYPE (name), 0);
495 : 2425680 : vn_nary_op_t nary;
496 : : /* Allocate from non-unwinding stack. */
497 : 2425680 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
498 : 2425680 : init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
499 : : boolean_type_node, ops);
500 : 2425680 : nary->predicated_values = 0;
501 : 2425680 : nary->u.result = boolean_true_node;
502 : 2425680 : vn_nary_op_insert_into (nary, valid_info->nary);
503 : 2425680 : gcc_assert (nary->unwind_to == NULL);
504 : : /* Also do not link it into the undo chain. */
505 : 2425680 : last_inserted_nary = nary->next;
506 : 2425680 : nary->next = (vn_nary_op_t)(void *)-1;
507 : 2425680 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
508 : 2425680 : init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
509 : : boolean_type_node, ops);
510 : 2425680 : nary->predicated_values = 0;
511 : 2425680 : nary->u.result = boolean_false_node;
512 : 2425680 : vn_nary_op_insert_into (nary, valid_info->nary);
513 : 2425680 : gcc_assert (nary->unwind_to == NULL);
514 : 2425680 : last_inserted_nary = nary->next;
515 : 2425680 : nary->next = (vn_nary_op_t)(void *)-1;
516 : 2425680 : if (dump_file && (dump_flags & TDF_DETAILS))
517 : : {
518 : 38 : fprintf (dump_file, "Recording ");
519 : 38 : print_generic_expr (dump_file, name, TDF_SLIM);
520 : 38 : fprintf (dump_file, " != 0\n");
521 : : }
522 : : }
523 : : break;
524 : :
525 : 62066 : case RESULT_DECL:
526 : : /* If the result is passed by invisible reference the default
527 : : def is initialized, otherwise it's uninitialized. Still
528 : : undefined is varying. */
529 : 62066 : newinfo->visited = true;
530 : 62066 : newinfo->valnum = name;
531 : 62066 : break;
532 : :
533 : 0 : default:
534 : 0 : gcc_unreachable ();
535 : : }
536 : : return newinfo;
537 : : }
538 : :
539 : : /* Return the SSA value of X. */
540 : :
541 : : inline tree
542 : 3408288895 : SSA_VAL (tree x, bool *visited = NULL)
543 : : {
544 : 3408288895 : vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
545 : 3408288895 : if (visited)
546 : 1373821276 : *visited = tem && tem->visited;
547 : 3408288895 : return tem && tem->visited ? tem->valnum : x;
548 : : }
549 : :
550 : : /* Return the SSA value of the VUSE x, supporting released VDEFs
551 : : during elimination which will value-number the VDEF to the
552 : : associated VUSE (but not substitute in the whole lattice). */
553 : :
554 : : static inline tree
555 : 1256228237 : vuse_ssa_val (tree x)
556 : : {
557 : 1256228237 : if (!x)
558 : : return NULL_TREE;
559 : :
560 : 1252820316 : do
561 : : {
562 : 1252820316 : x = SSA_VAL (x);
563 : 1252820316 : gcc_assert (x != VN_TOP);
564 : : }
565 : 1252820316 : while (SSA_NAME_IN_FREE_LIST (x));
566 : :
567 : : return x;
568 : : }
569 : :
570 : : /* Similar to the above but used as callback for walk_non_aliased_vuses
571 : : and thus should stop at unvisited VUSE to not walk across region
572 : : boundaries. */
573 : :
574 : : static tree
575 : 1055935996 : vuse_valueize (tree vuse)
576 : : {
577 : 1055935996 : do
578 : : {
579 : 1055935996 : bool visited;
580 : 1055935996 : vuse = SSA_VAL (vuse, &visited);
581 : 1055935996 : if (!visited)
582 : 16051054 : return NULL_TREE;
583 : 1039884942 : gcc_assert (vuse != VN_TOP);
584 : : }
585 : 1039884942 : while (SSA_NAME_IN_FREE_LIST (vuse));
586 : : return vuse;
587 : : }
588 : :
589 : :
590 : : /* Return the vn_kind the expression computed by the stmt should be
591 : : associated with. */
592 : :
593 : : enum vn_kind
594 : 103463693 : vn_get_stmt_kind (gimple *stmt)
595 : : {
596 : 103463693 : switch (gimple_code (stmt))
597 : : {
598 : : case GIMPLE_CALL:
599 : : return VN_REFERENCE;
600 : : case GIMPLE_PHI:
601 : : return VN_PHI;
602 : 103463693 : case GIMPLE_ASSIGN:
603 : 103463693 : {
604 : 103463693 : enum tree_code code = gimple_assign_rhs_code (stmt);
605 : 103463693 : tree rhs1 = gimple_assign_rhs1 (stmt);
606 : 103463693 : switch (get_gimple_rhs_class (code))
607 : : {
608 : : case GIMPLE_UNARY_RHS:
609 : : case GIMPLE_BINARY_RHS:
610 : : case GIMPLE_TERNARY_RHS:
611 : : return VN_NARY;
612 : 48685569 : case GIMPLE_SINGLE_RHS:
613 : 48685569 : switch (TREE_CODE_CLASS (code))
614 : : {
615 : 36674898 : case tcc_reference:
616 : : /* VOP-less references can go through unary case. */
617 : 36674898 : if ((code == REALPART_EXPR
618 : : || code == IMAGPART_EXPR
619 : 36674898 : || code == VIEW_CONVERT_EXPR
620 : 36674898 : || code == BIT_FIELD_REF)
621 : 36674898 : && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
622 : 736442 : || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
623 : 1973746 : return VN_NARY;
624 : :
625 : : /* Fallthrough. */
626 : : case tcc_declaration:
627 : : return VN_REFERENCE;
628 : :
629 : : case tcc_constant:
630 : : return VN_CONSTANT;
631 : :
632 : 5976497 : default:
633 : 5976497 : if (code == ADDR_EXPR)
634 : 3230490 : return (is_gimple_min_invariant (rhs1)
635 : 3230490 : ? VN_CONSTANT : VN_REFERENCE);
636 : 2746007 : else if (code == CONSTRUCTOR)
637 : : return VN_NARY;
638 : : return VN_NONE;
639 : : }
640 : : default:
641 : : return VN_NONE;
642 : : }
643 : : }
644 : : default:
645 : : return VN_NONE;
646 : : }
647 : : }
648 : :
649 : : /* Lookup a value id for CONSTANT and return it. If it does not
650 : : exist returns 0. */
651 : :
652 : : unsigned int
653 : 0 : get_constant_value_id (tree constant)
654 : : {
655 : 0 : vn_constant_s **slot;
656 : 0 : struct vn_constant_s vc;
657 : :
658 : 0 : vc.hashcode = vn_hash_constant_with_type (constant);
659 : 0 : vc.constant = constant;
660 : 0 : slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
661 : 0 : if (slot)
662 : 0 : return (*slot)->value_id;
663 : : return 0;
664 : : }
665 : :
666 : : /* Lookup a value id for CONSTANT, and if it does not exist, create a
667 : : new one and return it. If it does exist, return it. */
668 : :
669 : : unsigned int
670 : 28807950 : get_or_alloc_constant_value_id (tree constant)
671 : : {
672 : 28807950 : vn_constant_s **slot;
673 : 28807950 : struct vn_constant_s vc;
674 : 28807950 : vn_constant_t vcp;
675 : :
676 : : /* If the hashtable isn't initialized we're not running from PRE and thus
677 : : do not need value-ids. */
678 : 28807950 : if (!constant_to_value_id)
679 : : return 0;
680 : :
681 : 4782430 : vc.hashcode = vn_hash_constant_with_type (constant);
682 : 4782430 : vc.constant = constant;
683 : 4782430 : slot = constant_to_value_id->find_slot (&vc, INSERT);
684 : 4782430 : if (*slot)
685 : 2215339 : return (*slot)->value_id;
686 : :
687 : 2567091 : vcp = XNEW (struct vn_constant_s);
688 : 2567091 : vcp->hashcode = vc.hashcode;
689 : 2567091 : vcp->constant = constant;
690 : 2567091 : vcp->value_id = get_next_constant_value_id ();
691 : 2567091 : *slot = vcp;
692 : 2567091 : return vcp->value_id;
693 : : }
694 : :
695 : : /* Compute the hash for a reference operand VRO1. */
696 : :
697 : : static void
698 : 133960879 : vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
699 : : {
700 : 133960879 : hstate.add_int (vro1->opcode);
701 : 133960879 : if (vro1->opcode == CALL_EXPR && !vro1->op0)
702 : 554925 : hstate.add_int (vro1->clique);
703 : 133960879 : if (vro1->op0)
704 : 127719303 : inchash::add_expr (vro1->op0, hstate);
705 : 133960879 : if (vro1->op1)
706 : 11434365 : inchash::add_expr (vro1->op1, hstate);
707 : 133960879 : if (vro1->op2)
708 : 13234777 : inchash::add_expr (vro1->op2, hstate);
709 : 133960879 : }
710 : :
711 : : /* Compute a hash for the reference operation VR1 and return it. */
712 : :
713 : : static hashval_t
714 : 200325553 : vn_reference_compute_hash (const vn_reference_t vr1)
715 : : {
716 : 200325553 : inchash::hash hstate;
717 : 200325553 : hashval_t result;
718 : 200325553 : int i;
719 : 200325553 : vn_reference_op_t vro;
720 : 200325553 : poly_offset_int off = -1;
721 : 200325553 : bool deref = false;
722 : :
723 : 815222137 : FOR_EACH_VEC_ELT (vr1->operands, i, vro)
724 : : {
725 : 614896584 : if (vro->opcode == MEM_REF)
726 : : deref = true;
727 : 425190622 : else if (vro->opcode != ADDR_EXPR)
728 : 297176203 : deref = false;
729 : 614896584 : if (maybe_ne (vro->off, -1))
730 : : {
731 : 361016012 : if (known_eq (off, -1))
732 : 191895388 : off = 0;
733 : 614896584 : off += vro->off;
734 : : }
735 : : else
736 : : {
737 : 253880572 : if (maybe_ne (off, -1)
738 : 253880572 : && maybe_ne (off, 0))
739 : 101597987 : hstate.add_poly_hwi (off.force_shwi ());
740 : 253880572 : off = -1;
741 : 253880572 : if (deref
742 : 120157747 : && vro->opcode == ADDR_EXPR)
743 : : {
744 : 119919693 : if (vro->op0)
745 : : {
746 : 119919693 : tree op = TREE_OPERAND (vro->op0, 0);
747 : 119919693 : hstate.add_int (TREE_CODE (op));
748 : 119919693 : inchash::add_expr (op, hstate);
749 : : }
750 : : }
751 : : else
752 : 133960879 : vn_reference_op_compute_hash (vro, hstate);
753 : : }
754 : : }
755 : : /* Do not hash vr1->offset or vr1->max_size, we want to get collisions
756 : : to be able to identify compatible results. */
757 : 200325553 : result = hstate.end ();
758 : : /* ??? We would ICE later if we hash instead of adding that in. */
759 : 200325553 : if (vr1->vuse)
760 : 195493928 : result += SSA_NAME_VERSION (vr1->vuse);
761 : :
762 : 200325553 : return result;
763 : : }
764 : :
765 : : /* Return true if reference operations VR1 and VR2 are equivalent. This
766 : : means they have the same set of operands and vuses. */
767 : :
768 : : bool
769 : 4336160156 : vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
770 : : {
771 : 4336160156 : unsigned i, j;
772 : :
773 : : /* Early out if this is not a hash collision. */
774 : 4336160156 : if (vr1->hashcode != vr2->hashcode)
775 : : return false;
776 : :
777 : : /* The VOP needs to be the same. */
778 : 18379375 : if (vr1->vuse != vr2->vuse)
779 : : return false;
780 : :
781 : : /* The offset/max_size used for the ao_ref during lookup has to be
782 : : the same. */
783 : 18378915 : if (maybe_ne (vr1->offset, vr2->offset)
784 : 18378915 : || maybe_ne (vr1->max_size, vr2->max_size))
785 : : {
786 : : /* But nothing known in the prevailing entry is OK to be used. */
787 : 6929909 : if (maybe_ne (vr1->offset, 0) || known_size_p (vr1->max_size))
788 : : return false;
789 : : }
790 : :
791 : : /* If the operands are the same we are done. */
792 : 36673996 : if (vr1->operands == vr2->operands)
793 : : return true;
794 : :
795 : 17478541 : if (!vr1->type || !vr2->type)
796 : : {
797 : 548344 : if (vr1->type != vr2->type)
798 : : return false;
799 : : }
800 : 16930197 : else if (vr1->type == vr2->type)
801 : : ;
802 : 2237281 : else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
803 : 2237281 : || (COMPLETE_TYPE_P (vr1->type)
804 : 2237281 : && !expressions_equal_p (TYPE_SIZE (vr1->type),
805 : 2237281 : TYPE_SIZE (vr2->type))))
806 : 797730 : return false;
807 : 1439551 : else if (vr1->operands[0].opcode == CALL_EXPR
808 : 1439551 : && !types_compatible_p (vr1->type, vr2->type))
809 : : return false;
810 : 1439551 : else if (INTEGRAL_TYPE_P (vr1->type)
811 : 607086 : && INTEGRAL_TYPE_P (vr2->type))
812 : : {
813 : 565721 : if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
814 : : return false;
815 : : }
816 : 873830 : else if (INTEGRAL_TYPE_P (vr1->type)
817 : 873830 : && (TYPE_PRECISION (vr1->type)
818 : 41365 : != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
819 : : return false;
820 : 873696 : else if (INTEGRAL_TYPE_P (vr2->type)
821 : 873696 : && (TYPE_PRECISION (vr2->type)
822 : 9819 : != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
823 : : return false;
824 : 8201 : else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
825 : 873102 : && VECTOR_BOOLEAN_TYPE_P (vr2->type))
826 : : {
827 : : /* Vector boolean types can have padding, verify we are dealing with
828 : : the same number of elements, aka the precision of the types.
829 : : For example, In most architecture the precision_size of vbool*_t
830 : : types are caculated like below:
831 : : precision_size = type_size * 8
832 : :
833 : : Unfortunately, the RISC-V will adjust the precision_size for the
834 : : vbool*_t in order to align the ISA as below:
835 : : type_size = [1, 1, 1, 1, 2, 4, 8]
836 : : precision_size = [1, 2, 4, 8, 16, 32, 64]
837 : :
838 : : Then the precision_size of RISC-V vbool*_t will not be the multiple
839 : : of the type_size. We take care of this case consolidated here. */
840 : 0 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1->type),
841 : 0 : TYPE_VECTOR_SUBPARTS (vr2->type)))
842 : : return false;
843 : : }
844 : 873102 : else if (TYPE_MODE (vr1->type) != TYPE_MODE (vr2->type)
845 : 873102 : && (!mode_can_transfer_bits (TYPE_MODE (vr1->type))
846 : 44589 : || !mode_can_transfer_bits (TYPE_MODE (vr2->type))))
847 : 1056 : return false;
848 : :
849 : : i = 0;
850 : : j = 0;
851 : 20033643 : do
852 : : {
853 : 20033643 : poly_offset_int off1 = 0, off2 = 0;
854 : 20033643 : vn_reference_op_t vro1, vro2;
855 : 20033643 : vn_reference_op_s tem1, tem2;
856 : 20033643 : bool deref1 = false, deref2 = false;
857 : 20033643 : bool reverse1 = false, reverse2 = false;
858 : 68283891 : for (; vr1->operands.iterate (i, &vro1); i++)
859 : : {
860 : 48250248 : if (vro1->opcode == MEM_REF)
861 : : deref1 = true;
862 : : /* Do not look through a storage order barrier. */
863 : 32989837 : else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
864 : 5992 : return false;
865 : 48250248 : reverse1 |= vro1->reverse;
866 : 48250248 : if (known_eq (vro1->off, -1))
867 : : break;
868 : 28216605 : off1 += vro1->off;
869 : : }
870 : 48432412 : for (; vr2->operands.iterate (j, &vro2); j++)
871 : : {
872 : 48432412 : if (vro2->opcode == MEM_REF)
873 : : deref2 = true;
874 : : /* Do not look through a storage order barrier. */
875 : 33171991 : else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
876 : : return false;
877 : 48432412 : reverse2 |= vro2->reverse;
878 : 48432412 : if (known_eq (vro2->off, -1))
879 : : break;
880 : 28398769 : off2 += vro2->off;
881 : : }
882 : 20033643 : if (maybe_ne (off1, off2) || reverse1 != reverse2)
883 : : return false;
884 : 20033597 : if (deref1 && vro1->opcode == ADDR_EXPR)
885 : : {
886 : 8475417 : memset (&tem1, 0, sizeof (tem1));
887 : 8475417 : tem1.op0 = TREE_OPERAND (vro1->op0, 0);
888 : 8475417 : tem1.type = TREE_TYPE (tem1.op0);
889 : 8475417 : tem1.opcode = TREE_CODE (tem1.op0);
890 : 8475417 : vro1 = &tem1;
891 : 8475417 : deref1 = false;
892 : : }
893 : 20033597 : if (deref2 && vro2->opcode == ADDR_EXPR)
894 : : {
895 : 8475427 : memset (&tem2, 0, sizeof (tem2));
896 : 8475427 : tem2.op0 = TREE_OPERAND (vro2->op0, 0);
897 : 8475427 : tem2.type = TREE_TYPE (tem2.op0);
898 : 8475427 : tem2.opcode = TREE_CODE (tem2.op0);
899 : 8475427 : vro2 = &tem2;
900 : 8475427 : deref2 = false;
901 : : }
902 : 20033597 : if (deref1 != deref2)
903 : : return false;
904 : 20033597 : if (!vn_reference_op_eq (vro1, vro2))
905 : : return false;
906 : 20027651 : ++j;
907 : 20027651 : ++i;
908 : : }
909 : 40055302 : while (vr1->operands.length () != i
910 : 60082953 : || vr2->operands.length () != j);
911 : :
912 : : return true;
913 : : }
914 : :
915 : : /* Copy the operations present in load/store REF into RESULT, a vector of
916 : : vn_reference_op_s's. */
917 : :
918 : : static void
919 : 220526686 : copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
920 : : {
921 : : /* For non-calls, store the information that makes up the address. */
922 : 220526686 : tree orig = ref;
923 : 763520917 : while (ref)
924 : : {
925 : 542994231 : vn_reference_op_s temp;
926 : :
927 : 542994231 : memset (&temp, 0, sizeof (temp));
928 : 542994231 : temp.type = TREE_TYPE (ref);
929 : 542994231 : temp.opcode = TREE_CODE (ref);
930 : 542994231 : temp.off = -1;
931 : :
932 : 542994231 : switch (temp.opcode)
933 : : {
934 : 15074932 : case MODIFY_EXPR:
935 : 15074932 : temp.op0 = TREE_OPERAND (ref, 1);
936 : 15074932 : break;
937 : 137 : case WITH_SIZE_EXPR:
938 : 137 : temp.op0 = TREE_OPERAND (ref, 1);
939 : 137 : temp.off = 0;
940 : 137 : break;
941 : 115035468 : case MEM_REF:
942 : : /* The base address gets its own vn_reference_op_s structure. */
943 : 115035468 : temp.op0 = TREE_OPERAND (ref, 1);
944 : 115035468 : if (!mem_ref_offset (ref).to_shwi (&temp.off))
945 : 0 : temp.off = -1;
946 : 115035468 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
947 : 115035468 : temp.base = MR_DEPENDENCE_BASE (ref);
948 : 115035468 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
949 : 115035468 : break;
950 : 2510014 : case TARGET_MEM_REF:
951 : : /* The base address gets its own vn_reference_op_s structure. */
952 : 2510014 : temp.op0 = TMR_INDEX (ref);
953 : 2510014 : temp.op1 = TMR_STEP (ref);
954 : 2510014 : temp.op2 = TMR_OFFSET (ref);
955 : 2510014 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
956 : 2510014 : temp.base = MR_DEPENDENCE_BASE (ref);
957 : 2510014 : result->safe_push (temp);
958 : 2510014 : memset (&temp, 0, sizeof (temp));
959 : 2510014 : temp.type = NULL_TREE;
960 : 2510014 : temp.opcode = ERROR_MARK;
961 : 2510014 : temp.op0 = TMR_INDEX2 (ref);
962 : 2510014 : temp.off = -1;
963 : 2510014 : break;
964 : 869457 : case BIT_FIELD_REF:
965 : : /* Record bits, position and storage order. */
966 : 869457 : temp.op0 = TREE_OPERAND (ref, 1);
967 : 869457 : temp.op1 = TREE_OPERAND (ref, 2);
968 : 1738204 : if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
969 : 710 : temp.off = -1;
970 : 869457 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
971 : 869457 : break;
972 : 144836090 : case COMPONENT_REF:
973 : : /* The field decl is enough to unambiguously specify the field,
974 : : so use its type here. */
975 : 144836090 : temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
976 : 144836090 : temp.op0 = TREE_OPERAND (ref, 1);
977 : 144836090 : temp.op1 = TREE_OPERAND (ref, 2);
978 : 289669829 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
979 : 289669546 : && TYPE_REVERSE_STORAGE_ORDER
980 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
981 : 144836090 : {
982 : 144836090 : tree this_offset = component_ref_field_offset (ref);
983 : 144836090 : if (this_offset
984 : 144836090 : && poly_int_tree_p (this_offset))
985 : : {
986 : 144833988 : tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
987 : 144833988 : if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
988 : : {
989 : 144171131 : poly_offset_int off
990 : 144171131 : = (wi::to_poly_offset (this_offset)
991 : 144171131 : + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
992 : : /* Prohibit value-numbering zero offset components
993 : : of addresses the same before the pass folding
994 : : __builtin_object_size had a chance to run. Likewise
995 : : for components of zero size at arbitrary offset. */
996 : 144171131 : if (TREE_CODE (orig) != ADDR_EXPR
997 : 4799117 : || (TYPE_SIZE (temp.type)
998 : 4786645 : && integer_nonzerop (TYPE_SIZE (temp.type))
999 : 6249702 : && maybe_ne (off, 0))
1000 : 147131550 : || (cfun->curr_properties & PROP_objsz))
1001 : 142705943 : off.to_shwi (&temp.off);
1002 : : }
1003 : : }
1004 : : }
1005 : : break;
1006 : 37617215 : case ARRAY_RANGE_REF:
1007 : 37617215 : case ARRAY_REF:
1008 : 37617215 : {
1009 : 37617215 : tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
1010 : : /* Record index as operand. */
1011 : 37617215 : temp.op0 = TREE_OPERAND (ref, 1);
1012 : : /* Always record lower bounds and element size. */
1013 : 37617215 : temp.op1 = array_ref_low_bound (ref);
1014 : : /* But record element size in units of the type alignment. */
1015 : 37617215 : temp.op2 = TREE_OPERAND (ref, 3);
1016 : 37617215 : temp.align = eltype->type_common.align;
1017 : 37617215 : if (! temp.op2)
1018 : 37408744 : temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
1019 : : size_int (TYPE_ALIGN_UNIT (eltype)));
1020 : : /* Prohibit value-numbering addresses of one-after-the-last
1021 : : element ARRAY_REFs the same as addresses of other components
1022 : : before the pass folding __builtin_object_size had a chance
1023 : : to run. */
1024 : 37617215 : bool avoid_oob = true;
1025 : 37617215 : if (TREE_CODE (orig) != ADDR_EXPR
1026 : 452943 : || cfun->curr_properties & PROP_objsz)
1027 : : avoid_oob = false;
1028 : 217681 : else if (poly_int_tree_p (temp.op0))
1029 : : {
1030 : 73781 : tree ub = array_ref_up_bound (ref);
1031 : 73781 : if (ub
1032 : 72155 : && poly_int_tree_p (ub)
1033 : : /* ??? The C frontend for T[0] uses [0:] and the
1034 : : C++ frontend [0:-1U]. See layout_type for how
1035 : : awkward this is. */
1036 : 63978 : && !integer_minus_onep (ub)
1037 : 145936 : && known_le (wi::to_poly_offset (temp.op0),
1038 : : wi::to_poly_offset (ub)))
1039 : 63131 : avoid_oob = false;
1040 : : }
1041 : 37617215 : if (poly_int_tree_p (temp.op0)
1042 : 21772736 : && poly_int_tree_p (temp.op1)
1043 : 21772712 : && TREE_CODE (temp.op2) == INTEGER_CST
1044 : 59329743 : && !avoid_oob)
1045 : : {
1046 : 43405466 : poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1047 : 65108199 : - wi::to_poly_offset (temp.op1))
1048 : 43405466 : * wi::to_offset (temp.op2)
1049 : 21702733 : * vn_ref_op_align_unit (&temp));
1050 : 21702733 : off.to_shwi (&temp.off);
1051 : : }
1052 : 37617215 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1053 : 37617215 : && TYPE_REVERSE_STORAGE_ORDER
1054 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1055 : : }
1056 : 37617215 : break;
1057 : 82310339 : case VAR_DECL:
1058 : 82310339 : if (DECL_HARD_REGISTER (ref))
1059 : : {
1060 : 20201 : temp.op0 = ref;
1061 : 20201 : break;
1062 : : }
1063 : : /* Fallthru. */
1064 : 85721280 : case PARM_DECL:
1065 : 85721280 : case CONST_DECL:
1066 : 85721280 : case RESULT_DECL:
1067 : : /* Canonicalize decls to MEM[&decl] which is what we end up with
1068 : : when valueizing MEM[ptr] with ptr = &decl. */
1069 : 85721280 : temp.opcode = MEM_REF;
1070 : 85721280 : temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1071 : 85721280 : temp.off = 0;
1072 : 85721280 : result->safe_push (temp);
1073 : 85721280 : temp.opcode = ADDR_EXPR;
1074 : 85721280 : temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1075 : 85721280 : temp.type = TREE_TYPE (temp.op0);
1076 : 85721280 : temp.off = -1;
1077 : 85721280 : break;
1078 : 94231857 : case STRING_CST:
1079 : 94231857 : case INTEGER_CST:
1080 : 94231857 : case POLY_INT_CST:
1081 : 94231857 : case COMPLEX_CST:
1082 : 94231857 : case VECTOR_CST:
1083 : 94231857 : case REAL_CST:
1084 : 94231857 : case FIXED_CST:
1085 : 94231857 : case CONSTRUCTOR:
1086 : 94231857 : case SSA_NAME:
1087 : 94231857 : temp.op0 = ref;
1088 : 94231857 : break;
1089 : 44641748 : case ADDR_EXPR:
1090 : 44641748 : if (is_gimple_min_invariant (ref))
1091 : : {
1092 : 40553348 : temp.op0 = ref;
1093 : 40553348 : break;
1094 : : }
1095 : : break;
1096 : : /* These are only interesting for their operands, their
1097 : : existence, and their type. They will never be the last
1098 : : ref in the chain of references (IE they require an
1099 : : operand), so we don't have to put anything
1100 : : for op* as it will be handled by the iteration */
1101 : 471038 : case REALPART_EXPR:
1102 : 471038 : temp.off = 0;
1103 : 471038 : break;
1104 : 1488159 : case VIEW_CONVERT_EXPR:
1105 : 1488159 : temp.off = 0;
1106 : 1488159 : temp.reverse = storage_order_barrier_p (ref);
1107 : 1488159 : break;
1108 : 476635 : case IMAGPART_EXPR:
1109 : : /* This is only interesting for its constant offset. */
1110 : 476635 : temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1111 : 476635 : break;
1112 : 0 : default:
1113 : 0 : gcc_unreachable ();
1114 : : }
1115 : 542994231 : result->safe_push (temp);
1116 : :
1117 : 542994231 : if (REFERENCE_CLASS_P (ref)
1118 : 239690155 : || TREE_CODE (ref) == MODIFY_EXPR
1119 : 224615223 : || TREE_CODE (ref) == WITH_SIZE_EXPR
1120 : 767609317 : || (TREE_CODE (ref) == ADDR_EXPR
1121 : 44641748 : && !is_gimple_min_invariant (ref)))
1122 : 322467545 : ref = TREE_OPERAND (ref, 0);
1123 : : else
1124 : : ref = NULL_TREE;
1125 : : }
1126 : 220526686 : }
1127 : :
1128 : : /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1129 : : operands in *OPS, the reference alias set SET and the reference type TYPE.
1130 : : Return true if something useful was produced. */
1131 : :
1132 : : bool
1133 : 14382593 : ao_ref_init_from_vn_reference (ao_ref *ref,
1134 : : alias_set_type set, alias_set_type base_set,
1135 : : tree type, const vec<vn_reference_op_s> &ops)
1136 : : {
1137 : 14382593 : unsigned i;
1138 : 14382593 : tree base = NULL_TREE;
1139 : 14382593 : tree *op0_p = &base;
1140 : 14382593 : poly_offset_int offset = 0;
1141 : 14382593 : poly_offset_int max_size;
1142 : 14382593 : poly_offset_int size = -1;
1143 : 14382593 : tree size_tree = NULL_TREE;
1144 : :
1145 : : /* We don't handle calls. */
1146 : 14382593 : if (!type)
1147 : : return false;
1148 : :
1149 : 14382593 : machine_mode mode = TYPE_MODE (type);
1150 : 14382593 : if (mode == BLKmode)
1151 : 254682 : size_tree = TYPE_SIZE (type);
1152 : : else
1153 : 28255822 : size = GET_MODE_BITSIZE (mode);
1154 : 14127911 : if (size_tree != NULL_TREE
1155 : 254682 : && poly_int_tree_p (size_tree))
1156 : 254682 : size = wi::to_poly_offset (size_tree);
1157 : :
1158 : : /* Lower the final access size from the outermost expression. */
1159 : 14382593 : const_vn_reference_op_t cst_op = &ops[0];
1160 : : /* Cast away constness for the sake of the const-unsafe
1161 : : FOR_EACH_VEC_ELT(). */
1162 : 14382593 : vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1163 : 14382593 : size_tree = NULL_TREE;
1164 : 14382593 : if (op->opcode == COMPONENT_REF)
1165 : 5011745 : size_tree = DECL_SIZE (op->op0);
1166 : 9370848 : else if (op->opcode == BIT_FIELD_REF)
1167 : 88775 : size_tree = op->op0;
1168 : 5100520 : if (size_tree != NULL_TREE
1169 : 5100520 : && poly_int_tree_p (size_tree)
1170 : 10201040 : && (!known_size_p (size)
1171 : 14382593 : || known_lt (wi::to_poly_offset (size_tree), size)))
1172 : 62713 : size = wi::to_poly_offset (size_tree);
1173 : :
1174 : : /* Initially, maxsize is the same as the accessed element size.
1175 : : In the following it will only grow (or become -1). */
1176 : 14382593 : max_size = size;
1177 : :
1178 : : /* Compute cumulative bit-offset for nested component-refs and array-refs,
1179 : : and find the ultimate containing object. */
1180 : 55318919 : FOR_EACH_VEC_ELT (ops, i, op)
1181 : : {
1182 : 41083116 : switch (op->opcode)
1183 : : {
1184 : : case CALL_EXPR:
1185 : : return false;
1186 : :
1187 : : /* Record the base objects. */
1188 : 13938987 : case MEM_REF:
1189 : 13938987 : *op0_p = build2 (MEM_REF, op->type,
1190 : : NULL_TREE, op->op0);
1191 : 13938987 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1192 : 13938987 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1193 : 13938987 : op0_p = &TREE_OPERAND (*op0_p, 0);
1194 : 13938987 : break;
1195 : :
1196 : 296210 : case TARGET_MEM_REF:
1197 : 888630 : *op0_p = build5 (TARGET_MEM_REF, op->type,
1198 : : NULL_TREE, op->op2, op->op0,
1199 : 296210 : op->op1, ops[i+1].op0);
1200 : 296210 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1201 : 296210 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1202 : 296210 : op0_p = &TREE_OPERAND (*op0_p, 0);
1203 : 296210 : ++i;
1204 : 296210 : break;
1205 : :
1206 : : /* Unwrap some of the wrapped decls. */
1207 : 6755082 : case ADDR_EXPR:
1208 : : /* Apart from ADDR_EXPR arguments to MEM_REF. */
1209 : 6755082 : if (base != NULL_TREE
1210 : 6755082 : && TREE_CODE (base) == MEM_REF
1211 : 6720499 : && op->op0
1212 : 13475581 : && DECL_P (TREE_OPERAND (op->op0, 0)))
1213 : : {
1214 : 6717482 : const_vn_reference_op_t pop = &ops[i-1];
1215 : 6717482 : base = TREE_OPERAND (op->op0, 0);
1216 : 6717482 : if (known_eq (pop->off, -1))
1217 : : {
1218 : 56 : max_size = -1;
1219 : 56 : offset = 0;
1220 : : }
1221 : : else
1222 : 20152278 : offset += poly_offset_int (pop->off) * BITS_PER_UNIT;
1223 : : op0_p = NULL;
1224 : : break;
1225 : : }
1226 : : /* Fallthru. */
1227 : 7518321 : case PARM_DECL:
1228 : 7518321 : case CONST_DECL:
1229 : 7518321 : case RESULT_DECL:
1230 : : /* ??? We shouldn't see these, but un-canonicalize what
1231 : : copy_reference_ops_from_ref does when visiting MEM_REF. */
1232 : 7518321 : case VAR_DECL:
1233 : : /* ??? And for this only have DECL_HARD_REGISTER. */
1234 : 7518321 : case STRING_CST:
1235 : : /* This can show up in ARRAY_REF bases. */
1236 : 7518321 : case INTEGER_CST:
1237 : 7518321 : case SSA_NAME:
1238 : 7518321 : *op0_p = op->op0;
1239 : 7518321 : op0_p = NULL;
1240 : 7518321 : break;
1241 : :
1242 : : /* And now the usual component-reference style ops. */
1243 : 88775 : case BIT_FIELD_REF:
1244 : 88775 : offset += wi::to_poly_offset (op->op1);
1245 : 88775 : break;
1246 : :
1247 : 8175512 : case COMPONENT_REF:
1248 : 8175512 : {
1249 : 8175512 : tree field = op->op0;
1250 : : /* We do not have a complete COMPONENT_REF tree here so we
1251 : : cannot use component_ref_field_offset. Do the interesting
1252 : : parts manually. */
1253 : 8175512 : tree this_offset = DECL_FIELD_OFFSET (field);
1254 : :
1255 : 8175512 : if (op->op1 || !poly_int_tree_p (this_offset))
1256 : 236 : max_size = -1;
1257 : : else
1258 : : {
1259 : 8175276 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1260 : 8175276 : << LOG2_BITS_PER_UNIT);
1261 : 8175276 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1262 : 8175276 : offset += woffset;
1263 : : }
1264 : : break;
1265 : : }
1266 : :
1267 : 2911700 : case ARRAY_RANGE_REF:
1268 : 2911700 : case ARRAY_REF:
1269 : : /* Use the recorded constant offset. */
1270 : 2911700 : if (maybe_eq (op->off, -1))
1271 : 1011806 : max_size = -1;
1272 : : else
1273 : 5699682 : offset += poly_offset_int (op->off) * BITS_PER_UNIT;
1274 : : break;
1275 : :
1276 : : case REALPART_EXPR:
1277 : : break;
1278 : :
1279 : : case IMAGPART_EXPR:
1280 : 40936326 : offset += size;
1281 : : break;
1282 : :
1283 : : case VIEW_CONVERT_EXPR:
1284 : : break;
1285 : :
1286 : : case POLY_INT_CST:
1287 : : case COMPLEX_CST:
1288 : : case VECTOR_CST:
1289 : : case REAL_CST:
1290 : : case FIXED_CST:
1291 : : case CONSTRUCTOR:
1292 : : return false;
1293 : :
1294 : : default:
1295 : : return false;
1296 : : }
1297 : : }
1298 : :
1299 : 14235803 : if (base == NULL_TREE)
1300 : : return false;
1301 : :
1302 : 14235803 : ref->ref = NULL_TREE;
1303 : 14235803 : ref->base = base;
1304 : 14235803 : ref->ref_alias_set = set;
1305 : 14235803 : ref->base_alias_set = base_set;
1306 : : /* We discount volatiles from value-numbering elsewhere. */
1307 : 14235803 : ref->volatile_p = false;
1308 : :
1309 : 14235803 : if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1310 : : {
1311 : 0 : ref->offset = 0;
1312 : 0 : ref->size = -1;
1313 : 0 : ref->max_size = -1;
1314 : 0 : return true;
1315 : : }
1316 : :
1317 : 14235803 : if (!offset.to_shwi (&ref->offset))
1318 : : {
1319 : 26 : ref->offset = 0;
1320 : 26 : ref->max_size = -1;
1321 : 26 : return true;
1322 : : }
1323 : :
1324 : 14235777 : if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1325 : 868482 : ref->max_size = -1;
1326 : :
1327 : : return true;
1328 : : }
1329 : :
1330 : : /* Copy the operations present in load/store/call REF into RESULT, a vector of
1331 : : vn_reference_op_s's. */
1332 : :
1333 : : static void
1334 : 9246063 : copy_reference_ops_from_call (gcall *call,
1335 : : vec<vn_reference_op_s> *result)
1336 : : {
1337 : 9246063 : vn_reference_op_s temp;
1338 : 9246063 : unsigned i;
1339 : 9246063 : tree lhs = gimple_call_lhs (call);
1340 : 9246063 : int lr;
1341 : :
1342 : : /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1343 : : different. By adding the lhs here in the vector, we ensure that the
1344 : : hashcode is different, guaranteeing a different value number. */
1345 : 9246063 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
1346 : : {
1347 : 472622 : memset (&temp, 0, sizeof (temp));
1348 : 472622 : temp.opcode = MODIFY_EXPR;
1349 : 472622 : temp.type = TREE_TYPE (lhs);
1350 : 472622 : temp.op0 = lhs;
1351 : 472622 : temp.off = -1;
1352 : 472622 : result->safe_push (temp);
1353 : : }
1354 : :
1355 : : /* Copy the type, opcode, function, static chain and EH region, if any. */
1356 : 9246063 : memset (&temp, 0, sizeof (temp));
1357 : 9246063 : temp.type = gimple_call_fntype (call);
1358 : 9246063 : temp.opcode = CALL_EXPR;
1359 : 9246063 : temp.op0 = gimple_call_fn (call);
1360 : 9246063 : if (gimple_call_internal_p (call))
1361 : 540894 : temp.clique = gimple_call_internal_fn (call);
1362 : 9246063 : temp.op1 = gimple_call_chain (call);
1363 : 9246063 : if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1364 : 720958 : temp.op2 = size_int (lr);
1365 : 9246063 : temp.off = -1;
1366 : 9246063 : result->safe_push (temp);
1367 : :
1368 : : /* Copy the call arguments. As they can be references as well,
1369 : : just chain them together. */
1370 : 27304628 : for (i = 0; i < gimple_call_num_args (call); ++i)
1371 : : {
1372 : 18058565 : tree callarg = gimple_call_arg (call, i);
1373 : 18058565 : copy_reference_ops_from_ref (callarg, result);
1374 : : }
1375 : 9246063 : }
1376 : :
1377 : : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1378 : : *I_P to point to the last element of the replacement. */
1379 : : static bool
1380 : 129908065 : vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1381 : : unsigned int *i_p)
1382 : : {
1383 : 129908065 : unsigned int i = *i_p;
1384 : 129908065 : vn_reference_op_t op = &(*ops)[i];
1385 : 129908065 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1386 : 129908065 : tree addr_base;
1387 : 129908065 : poly_int64 addr_offset = 0;
1388 : :
1389 : : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1390 : : from .foo.bar to the preceding MEM_REF offset and replace the
1391 : : address with &OBJ. */
1392 : 129908065 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1393 : : &addr_offset, vn_valueize);
1394 : 129908065 : gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1395 : 129908065 : if (addr_base != TREE_OPERAND (op->op0, 0))
1396 : : {
1397 : 658626 : poly_offset_int off
1398 : 658626 : = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1399 : : SIGNED)
1400 : 658626 : + addr_offset);
1401 : 658626 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1402 : 658626 : op->op0 = build_fold_addr_expr (addr_base);
1403 : 658626 : if (tree_fits_shwi_p (mem_op->op0))
1404 : 658557 : mem_op->off = tree_to_shwi (mem_op->op0);
1405 : : else
1406 : 69 : mem_op->off = -1;
1407 : 658626 : return true;
1408 : : }
1409 : : return false;
1410 : : }
1411 : :
1412 : : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1413 : : *I_P to point to the last element of the replacement. */
1414 : : static bool
1415 : 86453886 : vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1416 : : unsigned int *i_p)
1417 : : {
1418 : 86453886 : bool changed = false;
1419 : 93176757 : vn_reference_op_t op;
1420 : :
1421 : 93176757 : do
1422 : : {
1423 : 93176757 : unsigned int i = *i_p;
1424 : 93176757 : op = &(*ops)[i];
1425 : 93176757 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1426 : 93176757 : gimple *def_stmt;
1427 : 93176757 : enum tree_code code;
1428 : 93176757 : poly_offset_int off;
1429 : :
1430 : 93176757 : def_stmt = SSA_NAME_DEF_STMT (op->op0);
1431 : 93176757 : if (!is_gimple_assign (def_stmt))
1432 : 86451935 : return changed;
1433 : :
1434 : 37421025 : code = gimple_assign_rhs_code (def_stmt);
1435 : 37421025 : if (code != ADDR_EXPR
1436 : 37421025 : && code != POINTER_PLUS_EXPR)
1437 : : return changed;
1438 : :
1439 : 19054744 : off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1440 : :
1441 : : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1442 : : from .foo.bar to the preceding MEM_REF offset and replace the
1443 : : address with &OBJ. */
1444 : 19054744 : if (code == ADDR_EXPR)
1445 : : {
1446 : 924659 : tree addr, addr_base;
1447 : 924659 : poly_int64 addr_offset;
1448 : :
1449 : 924659 : addr = gimple_assign_rhs1 (def_stmt);
1450 : 924659 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1451 : : &addr_offset,
1452 : : vn_valueize);
1453 : : /* If that didn't work because the address isn't invariant propagate
1454 : : the reference tree from the address operation in case the current
1455 : : dereference isn't offsetted. */
1456 : 924659 : if (!addr_base
1457 : 274924 : && *i_p == ops->length () - 1
1458 : 137462 : && known_eq (off, 0)
1459 : : /* This makes us disable this transform for PRE where the
1460 : : reference ops might be also used for code insertion which
1461 : : is invalid. */
1462 : 1009401 : && default_vn_walk_kind == VN_WALKREWRITE)
1463 : : {
1464 : 84644 : auto_vec<vn_reference_op_s, 32> tem;
1465 : 84644 : copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1466 : : /* Make sure to preserve TBAA info. The only objects not
1467 : : wrapped in MEM_REFs that can have their address taken are
1468 : : STRING_CSTs. */
1469 : 84644 : if (tem.length () >= 2
1470 : 84644 : && tem[tem.length () - 2].opcode == MEM_REF)
1471 : : {
1472 : 84629 : vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1473 : 84629 : new_mem_op->op0
1474 : 84629 : = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1475 : 169258 : wi::to_poly_wide (new_mem_op->op0));
1476 : : }
1477 : : else
1478 : 15 : gcc_assert (tem.last ().opcode == STRING_CST);
1479 : 84644 : ops->pop ();
1480 : 84644 : ops->pop ();
1481 : 84644 : ops->safe_splice (tem);
1482 : 84644 : --*i_p;
1483 : 84644 : return true;
1484 : 84644 : }
1485 : 840015 : if (!addr_base
1486 : 787197 : || TREE_CODE (addr_base) != MEM_REF
1487 : 1625345 : || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1488 : 783469 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1489 : : 0))))
1490 : : return changed;
1491 : :
1492 : 785330 : off += addr_offset;
1493 : 785330 : off += mem_ref_offset (addr_base);
1494 : 785330 : op->op0 = TREE_OPERAND (addr_base, 0);
1495 : : }
1496 : : else
1497 : : {
1498 : 18130085 : tree ptr, ptroff;
1499 : 18130085 : ptr = gimple_assign_rhs1 (def_stmt);
1500 : 18130085 : ptroff = gimple_assign_rhs2 (def_stmt);
1501 : 18130085 : if (TREE_CODE (ptr) != SSA_NAME
1502 : 16430033 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1503 : : /* Make sure to not endlessly recurse.
1504 : : See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1505 : : happen when we value-number a PHI to its backedge value. */
1506 : 16428619 : || SSA_VAL (ptr) == op->op0
1507 : 34558704 : || !poly_int_tree_p (ptroff))
1508 : 12190593 : return changed;
1509 : :
1510 : 5939492 : off += wi::to_poly_offset (ptroff);
1511 : 5939492 : op->op0 = ptr;
1512 : : }
1513 : :
1514 : 6724822 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1515 : 6724822 : if (tree_fits_shwi_p (mem_op->op0))
1516 : 6460808 : mem_op->off = tree_to_shwi (mem_op->op0);
1517 : : else
1518 : 264014 : mem_op->off = -1;
1519 : : /* ??? Can end up with endless recursion here!?
1520 : : gcc.c-torture/execute/strcmp-1.c */
1521 : 6724822 : if (TREE_CODE (op->op0) == SSA_NAME)
1522 : 6722961 : op->op0 = SSA_VAL (op->op0);
1523 : 6724822 : if (TREE_CODE (op->op0) != SSA_NAME)
1524 : 1951 : op->opcode = TREE_CODE (op->op0);
1525 : :
1526 : 6724822 : changed = true;
1527 : : }
1528 : : /* Tail-recurse. */
1529 : 6724822 : while (TREE_CODE (op->op0) == SSA_NAME);
1530 : :
1531 : : /* Fold a remaining *&. */
1532 : 1951 : if (TREE_CODE (op->op0) == ADDR_EXPR)
1533 : 261 : vn_reference_fold_indirect (ops, i_p);
1534 : :
1535 : : return changed;
1536 : : }
1537 : :
1538 : : /* Optimize the reference REF to a constant if possible or return
1539 : : NULL_TREE if not. */
1540 : :
1541 : : tree
1542 : 110611774 : fully_constant_vn_reference_p (vn_reference_t ref)
1543 : : {
1544 : 110611774 : vec<vn_reference_op_s> operands = ref->operands;
1545 : 110611774 : vn_reference_op_t op;
1546 : :
1547 : : /* Try to simplify the translated expression if it is
1548 : : a call to a builtin function with at most two arguments. */
1549 : 110611774 : op = &operands[0];
1550 : 110611774 : if (op->opcode == CALL_EXPR
1551 : 89513 : && (!op->op0
1552 : 82401 : || (TREE_CODE (op->op0) == ADDR_EXPR
1553 : 82401 : && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1554 : 82401 : && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1555 : : BUILT_IN_NORMAL)))
1556 : 73090 : && operands.length () >= 2
1557 : 110684820 : && operands.length () <= 3)
1558 : : {
1559 : 36742 : vn_reference_op_t arg0, arg1 = NULL;
1560 : 36742 : bool anyconst = false;
1561 : 36742 : arg0 = &operands[1];
1562 : 36742 : if (operands.length () > 2)
1563 : 5435 : arg1 = &operands[2];
1564 : 36742 : if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1565 : 36742 : || (arg0->opcode == ADDR_EXPR
1566 : 13459 : && is_gimple_min_invariant (arg0->op0)))
1567 : : anyconst = true;
1568 : 36742 : if (arg1
1569 : 36742 : && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1570 : 3956 : || (arg1->opcode == ADDR_EXPR
1571 : 579 : && is_gimple_min_invariant (arg1->op0))))
1572 : : anyconst = true;
1573 : 34684 : if (anyconst)
1574 : : {
1575 : 21571 : combined_fn fn;
1576 : 21571 : if (op->op0)
1577 : 20591 : fn = as_combined_fn (DECL_FUNCTION_CODE
1578 : 20591 : (TREE_OPERAND (op->op0, 0)));
1579 : : else
1580 : 980 : fn = as_combined_fn ((internal_fn) op->clique);
1581 : 21571 : tree folded;
1582 : 21571 : if (arg1)
1583 : 2648 : folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1584 : : else
1585 : 18923 : folded = fold_const_call (fn, ref->type, arg0->op0);
1586 : 21571 : if (folded
1587 : 21571 : && is_gimple_min_invariant (folded))
1588 : : return folded;
1589 : : }
1590 : : }
1591 : :
1592 : : /* Simplify reads from constants or constant initializers. */
1593 : 110575032 : else if (BITS_PER_UNIT == 8
1594 : 110575032 : && ref->type
1595 : 110575032 : && COMPLETE_TYPE_P (ref->type)
1596 : 221150022 : && is_gimple_reg_type (ref->type))
1597 : : {
1598 : 105788557 : poly_int64 off = 0;
1599 : 105788557 : HOST_WIDE_INT size;
1600 : 105788557 : if (INTEGRAL_TYPE_P (ref->type))
1601 : 53645845 : size = TYPE_PRECISION (ref->type);
1602 : 52142712 : else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1603 : 52142712 : size = tree_to_shwi (TYPE_SIZE (ref->type));
1604 : : else
1605 : 110611774 : return NULL_TREE;
1606 : 105788557 : if (size % BITS_PER_UNIT != 0
1607 : 103739223 : || size > MAX_BITSIZE_MODE_ANY_MODE)
1608 : : return NULL_TREE;
1609 : 103737910 : size /= BITS_PER_UNIT;
1610 : 103737910 : unsigned i;
1611 : 191741652 : for (i = 0; i < operands.length (); ++i)
1612 : : {
1613 : 191741652 : if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1614 : : {
1615 : 309 : ++i;
1616 : 309 : break;
1617 : : }
1618 : 191741343 : if (operands[i].reverse)
1619 : : return NULL_TREE;
1620 : 191732997 : if (known_eq (operands[i].off, -1))
1621 : : return NULL_TREE;
1622 : 177958125 : off += operands[i].off;
1623 : 177958125 : if (operands[i].opcode == MEM_REF)
1624 : : {
1625 : 89954383 : ++i;
1626 : 89954383 : break;
1627 : : }
1628 : : }
1629 : 89954692 : vn_reference_op_t base = &operands[--i];
1630 : 89954692 : tree ctor = error_mark_node;
1631 : 89954692 : tree decl = NULL_TREE;
1632 : 89954692 : if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1633 : 309 : ctor = base->op0;
1634 : 89954383 : else if (base->opcode == MEM_REF
1635 : 89954383 : && base[1].opcode == ADDR_EXPR
1636 : 147901388 : && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1637 : 3617689 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1638 : 3617629 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1639 : : {
1640 : 54334305 : decl = TREE_OPERAND (base[1].op0, 0);
1641 : 54334305 : if (TREE_CODE (decl) == STRING_CST)
1642 : : ctor = decl;
1643 : : else
1644 : 54329376 : ctor = ctor_for_folding (decl);
1645 : : }
1646 : 89949763 : if (ctor == NULL_TREE)
1647 : 379 : return build_zero_cst (ref->type);
1648 : 89954313 : else if (ctor != error_mark_node)
1649 : : {
1650 : 100174 : HOST_WIDE_INT const_off;
1651 : 100174 : if (decl)
1652 : : {
1653 : 199730 : tree res = fold_ctor_reference (ref->type, ctor,
1654 : 99865 : off * BITS_PER_UNIT,
1655 : 99865 : size * BITS_PER_UNIT, decl);
1656 : 99865 : if (res)
1657 : : {
1658 : 54569 : STRIP_USELESS_TYPE_CONVERSION (res);
1659 : 54569 : if (is_gimple_min_invariant (res))
1660 : 110611774 : return res;
1661 : : }
1662 : : }
1663 : 309 : else if (off.is_constant (&const_off))
1664 : : {
1665 : 309 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1666 : 309 : int len = native_encode_expr (ctor, buf, size, const_off);
1667 : 309 : if (len > 0)
1668 : 139 : return native_interpret_expr (ref->type, buf, len);
1669 : : }
1670 : : }
1671 : : }
1672 : :
1673 : : return NULL_TREE;
1674 : : }
1675 : :
1676 : : /* Return true if OPS contain a storage order barrier. */
1677 : :
1678 : : static bool
1679 : 59958767 : contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1680 : : {
1681 : 59958767 : vn_reference_op_t op;
1682 : 59958767 : unsigned i;
1683 : :
1684 : 235285321 : FOR_EACH_VEC_ELT (ops, i, op)
1685 : 175326554 : if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1686 : : return true;
1687 : :
1688 : : return false;
1689 : : }
1690 : :
1691 : : /* Return true if OPS represent an access with reverse storage order. */
1692 : :
1693 : : static bool
1694 : 59966948 : reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
1695 : : {
1696 : 59966948 : unsigned i = 0;
1697 : 59966948 : if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1698 : : ++i;
1699 : 59966948 : switch (ops[i].opcode)
1700 : : {
1701 : 57921117 : case ARRAY_REF:
1702 : 57921117 : case COMPONENT_REF:
1703 : 57921117 : case BIT_FIELD_REF:
1704 : 57921117 : case MEM_REF:
1705 : 57921117 : return ops[i].reverse;
1706 : : default:
1707 : : return false;
1708 : : }
1709 : : }
1710 : :
1711 : : /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1712 : : structures into their value numbers. This is done in-place, and
1713 : : the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1714 : : whether any operands were valueized. */
1715 : :
1716 : : static void
1717 : 223924499 : valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1718 : : bool with_avail = false)
1719 : : {
1720 : 223924499 : *valueized_anything = false;
1721 : :
1722 : 902458961 : for (unsigned i = 0; i < orig->length (); ++i)
1723 : : {
1724 : 678534462 : re_valueize:
1725 : 682137645 : vn_reference_op_t vro = &(*orig)[i];
1726 : 682137645 : if (vro->opcode == SSA_NAME
1727 : 583743107 : || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1728 : : {
1729 : 122950233 : tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1730 : 122950233 : if (tem != vro->op0)
1731 : : {
1732 : 17715098 : *valueized_anything = true;
1733 : 17715098 : vro->op0 = tem;
1734 : : }
1735 : : /* If it transforms from an SSA_NAME to a constant, update
1736 : : the opcode. */
1737 : 122950233 : if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1738 : 2104769 : vro->opcode = TREE_CODE (vro->op0);
1739 : : }
1740 : 682137645 : if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1741 : : {
1742 : 26327 : tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1743 : 26327 : if (tem != vro->op1)
1744 : : {
1745 : 584 : *valueized_anything = true;
1746 : 584 : vro->op1 = tem;
1747 : : }
1748 : : }
1749 : 682137645 : if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1750 : : {
1751 : 205418 : tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1752 : 205418 : if (tem != vro->op2)
1753 : : {
1754 : 119505 : *valueized_anything = true;
1755 : 119505 : vro->op2 = tem;
1756 : : }
1757 : : }
1758 : : /* If it transforms from an SSA_NAME to an address, fold with
1759 : : a preceding indirect reference. */
1760 : 682137645 : if (i > 0
1761 : 458134491 : && vro->op0
1762 : 454632783 : && TREE_CODE (vro->op0) == ADDR_EXPR
1763 : 817992475 : && (*orig)[i - 1].opcode == MEM_REF)
1764 : : {
1765 : 129907804 : if (vn_reference_fold_indirect (orig, &i))
1766 : 658626 : *valueized_anything = true;
1767 : : }
1768 : 552229841 : else if (i > 0
1769 : 328226687 : && vro->opcode == SSA_NAME
1770 : 648519610 : && (*orig)[i - 1].opcode == MEM_REF)
1771 : : {
1772 : 86453886 : if (vn_reference_maybe_forwprop_address (orig, &i))
1773 : : {
1774 : 3603183 : *valueized_anything = true;
1775 : : /* Re-valueize the current operand. */
1776 : 3603183 : goto re_valueize;
1777 : : }
1778 : : }
1779 : : /* If it transforms a non-constant ARRAY_REF into a constant
1780 : : one, adjust the constant offset. */
1781 : 465775955 : else if ((vro->opcode == ARRAY_REF
1782 : 465775955 : || vro->opcode == ARRAY_RANGE_REF)
1783 : 39814772 : && known_eq (vro->off, -1)
1784 : 17302651 : && poly_int_tree_p (vro->op0)
1785 : 4628891 : && poly_int_tree_p (vro->op1)
1786 : 470404846 : && TREE_CODE (vro->op2) == INTEGER_CST)
1787 : : {
1788 : : /* Prohibit value-numbering addresses of one-after-the-last
1789 : : element ARRAY_REFs the same as addresses of other components
1790 : : before the pass folding __builtin_object_size had a chance
1791 : : to run. */
1792 : 4495193 : if (!(cfun->curr_properties & PROP_objsz)
1793 : 5714730 : && (*orig)[0].opcode == ADDR_EXPR)
1794 : : {
1795 : 34597 : tree dom = TYPE_DOMAIN ((*orig)[i + 1].type);
1796 : 52704 : if (!dom
1797 : 34447 : || !TYPE_MAX_VALUE (dom)
1798 : 24810 : || !poly_int_tree_p (TYPE_MAX_VALUE (dom))
1799 : 51177 : || integer_minus_onep (TYPE_MAX_VALUE (dom)))
1800 : 18914 : continue;
1801 : 16490 : if (!known_le (wi::to_poly_offset (vro->op0),
1802 : : wi::to_poly_offset (TYPE_MAX_VALUE (dom))))
1803 : 807 : continue;
1804 : : }
1805 : :
1806 : 8952558 : poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1807 : 13428837 : - wi::to_poly_offset (vro->op1))
1808 : 8952558 : * wi::to_offset (vro->op2)
1809 : 4476279 : * vn_ref_op_align_unit (vro));
1810 : 4476279 : off.to_shwi (&vro->off);
1811 : : }
1812 : : }
1813 : 223924499 : }
1814 : :
1815 : : static void
1816 : 14928964 : valueize_refs (vec<vn_reference_op_s> *orig)
1817 : : {
1818 : 14928964 : bool tem;
1819 : 0 : valueize_refs_1 (orig, &tem);
1820 : 0 : }
1821 : :
1822 : : static vec<vn_reference_op_s> shared_lookup_references;
1823 : :
1824 : : /* Create a vector of vn_reference_op_s structures from REF, a
1825 : : REFERENCE_CLASS_P tree. The vector is shared among all callers of
1826 : : this function. *VALUEIZED_ANYTHING will specify whether any
1827 : : operands were valueized. */
1828 : :
1829 : : static vec<vn_reference_op_s>
1830 : 182098919 : valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1831 : : {
1832 : 182098919 : if (!ref)
1833 : 0 : return vNULL;
1834 : 182098919 : shared_lookup_references.truncate (0);
1835 : 182098919 : copy_reference_ops_from_ref (ref, &shared_lookup_references);
1836 : 182098919 : valueize_refs_1 (&shared_lookup_references, valueized_anything);
1837 : 182098919 : return shared_lookup_references;
1838 : : }
1839 : :
1840 : : /* Create a vector of vn_reference_op_s structures from CALL, a
1841 : : call statement. The vector is shared among all callers of
1842 : : this function. */
1843 : :
1844 : : static vec<vn_reference_op_s>
1845 : 9246063 : valueize_shared_reference_ops_from_call (gcall *call)
1846 : : {
1847 : 9246063 : if (!call)
1848 : 0 : return vNULL;
1849 : 9246063 : shared_lookup_references.truncate (0);
1850 : 9246063 : copy_reference_ops_from_call (call, &shared_lookup_references);
1851 : 9246063 : valueize_refs (&shared_lookup_references);
1852 : 9246063 : return shared_lookup_references;
1853 : : }
1854 : :
1855 : : /* Lookup a SCCVN reference operation VR in the current hash table.
1856 : : Returns the resulting value number if it exists in the hash table,
1857 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
1858 : : vn_reference_t stored in the hashtable if something is found. */
1859 : :
1860 : : static tree
1861 : 65932904 : vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1862 : : {
1863 : 65932904 : vn_reference_s **slot;
1864 : 65932904 : hashval_t hash;
1865 : :
1866 : 65932904 : hash = vr->hashcode;
1867 : 65932904 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1868 : 65932904 : if (slot)
1869 : : {
1870 : 8559555 : if (vnresult)
1871 : 8559555 : *vnresult = (vn_reference_t)*slot;
1872 : 8559555 : return ((vn_reference_t)*slot)->result;
1873 : : }
1874 : :
1875 : : return NULL_TREE;
1876 : : }
1877 : :
1878 : :
1879 : : /* Partial definition tracking support. */
1880 : :
1881 : : struct pd_range
1882 : : {
1883 : : HOST_WIDE_INT offset;
1884 : : HOST_WIDE_INT size;
1885 : : pd_range *m_children[2];
1886 : : };
1887 : :
1888 : : struct pd_data
1889 : : {
1890 : : tree rhs;
1891 : : HOST_WIDE_INT rhs_off;
1892 : : HOST_WIDE_INT offset;
1893 : : HOST_WIDE_INT size;
1894 : : };
1895 : :
1896 : : /* Context for alias walking. */
1897 : :
1898 : : struct vn_walk_cb_data
1899 : : {
1900 : 61889284 : vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1901 : : vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1902 : : bool redundant_store_removal_p_)
1903 : 61889284 : : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1904 : 61889284 : mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1905 : 61889284 : vn_walk_kind (vn_walk_kind_),
1906 : 61889284 : tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
1907 : 123778568 : saved_operands (vNULL), first_range (), first_set (-2),
1908 : 123778568 : first_base_set (-2)
1909 : : {
1910 : 61889284 : if (!last_vuse_ptr)
1911 : 28586156 : last_vuse_ptr = &last_vuse;
1912 : 61889284 : ao_ref_init (&orig_ref, orig_ref_);
1913 : 61889284 : if (mask)
1914 : : {
1915 : 369242 : wide_int w = wi::to_wide (mask);
1916 : 369242 : unsigned int pos = 0, prec = w.get_precision ();
1917 : 369242 : pd_data pd;
1918 : 369242 : pd.rhs = build_constructor (NULL_TREE, NULL);
1919 : 369242 : pd.rhs_off = 0;
1920 : : /* When bitwise and with a constant is done on a memory load,
1921 : : we don't really need all the bits to be defined or defined
1922 : : to constants, we don't really care what is in the position
1923 : : corresponding to 0 bits in the mask.
1924 : : So, push the ranges of those 0 bits in the mask as artificial
1925 : : zero stores and let the partial def handling code do the
1926 : : rest. */
1927 : 786477 : while (pos < prec)
1928 : : {
1929 : 765389 : int tz = wi::ctz (w);
1930 : 765389 : if (pos + tz > prec)
1931 : 348154 : tz = prec - pos;
1932 : 765389 : if (tz)
1933 : : {
1934 : 590883 : if (BYTES_BIG_ENDIAN)
1935 : : pd.offset = prec - pos - tz;
1936 : : else
1937 : 590883 : pd.offset = pos;
1938 : 590883 : pd.size = tz;
1939 : 590883 : void *r = push_partial_def (pd, 0, 0, 0, prec);
1940 : 590883 : gcc_assert (r == NULL_TREE);
1941 : : }
1942 : 765389 : pos += tz;
1943 : 765389 : if (pos == prec)
1944 : : break;
1945 : 417235 : w = wi::lrshift (w, tz);
1946 : 417235 : tz = wi::ctz (wi::bit_not (w));
1947 : 417235 : if (pos + tz > prec)
1948 : 0 : tz = prec - pos;
1949 : 417235 : pos += tz;
1950 : 417235 : w = wi::lrshift (w, tz);
1951 : : }
1952 : 369242 : }
1953 : 61889284 : }
1954 : : ~vn_walk_cb_data ();
1955 : : void *finish (alias_set_type, alias_set_type, tree);
1956 : : void *push_partial_def (pd_data pd,
1957 : : alias_set_type, alias_set_type, HOST_WIDE_INT,
1958 : : HOST_WIDE_INT);
1959 : :
1960 : : vn_reference_t vr;
1961 : : ao_ref orig_ref;
1962 : : tree *last_vuse_ptr;
1963 : : tree last_vuse;
1964 : : tree mask;
1965 : : tree masked_result;
1966 : : tree same_val;
1967 : : vn_lookup_kind vn_walk_kind;
1968 : : bool tbaa_p;
1969 : : bool redundant_store_removal_p;
1970 : : vec<vn_reference_op_s> saved_operands;
1971 : :
1972 : : /* The VDEFs of partial defs we come along. */
1973 : : auto_vec<pd_data, 2> partial_defs;
1974 : : /* The first defs range to avoid splay tree setup in most cases. */
1975 : : pd_range first_range;
1976 : : alias_set_type first_set;
1977 : : alias_set_type first_base_set;
1978 : : default_splay_tree<pd_range *> known_ranges;
1979 : : obstack ranges_obstack;
1980 : : static constexpr HOST_WIDE_INT bufsize = 64;
1981 : : };
1982 : :
1983 : 61889284 : vn_walk_cb_data::~vn_walk_cb_data ()
1984 : : {
1985 : 61889284 : if (known_ranges)
1986 : 212381 : obstack_free (&ranges_obstack, NULL);
1987 : 61889284 : saved_operands.release ();
1988 : 61889284 : }
1989 : :
1990 : : void *
1991 : 1476996 : vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
1992 : : {
1993 : 1476996 : if (first_set != -2)
1994 : : {
1995 : 360933 : set = first_set;
1996 : 360933 : base_set = first_base_set;
1997 : : }
1998 : 1476996 : if (mask)
1999 : : {
2000 : 694 : masked_result = val;
2001 : 694 : return (void *) -1;
2002 : : }
2003 : 1476302 : if (same_val && !operand_equal_p (val, same_val))
2004 : : return (void *) -1;
2005 : 1472663 : vec<vn_reference_op_s> &operands
2006 : 1472663 : = saved_operands.exists () ? saved_operands : vr->operands;
2007 : 1472663 : return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
2008 : : vr->offset, vr->max_size,
2009 : 1472663 : vr->type, operands, val);
2010 : : }
2011 : :
2012 : : /* Push PD to the vector of partial definitions returning a
2013 : : value when we are ready to combine things with VUSE, SET and MAXSIZEI,
2014 : : NULL when we want to continue looking for partial defs or -1
2015 : : on failure. */
2016 : :
2017 : : void *
2018 : 665678 : vn_walk_cb_data::push_partial_def (pd_data pd,
2019 : : alias_set_type set, alias_set_type base_set,
2020 : : HOST_WIDE_INT offseti,
2021 : : HOST_WIDE_INT maxsizei)
2022 : : {
2023 : : /* We're using a fixed buffer for encoding so fail early if the object
2024 : : we want to interpret is bigger. */
2025 : 665678 : if (maxsizei > bufsize * BITS_PER_UNIT
2026 : : || CHAR_BIT != 8
2027 : : || BITS_PER_UNIT != 8
2028 : : /* Not prepared to handle PDP endian. */
2029 : : || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2030 : : return (void *)-1;
2031 : :
2032 : : /* Turn too large constant stores into non-constant stores. */
2033 : 665607 : if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
2034 : 0 : pd.rhs = error_mark_node;
2035 : :
2036 : : /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
2037 : : most a partial byte before and/or after the region. */
2038 : 665607 : if (!CONSTANT_CLASS_P (pd.rhs))
2039 : : {
2040 : 630330 : if (pd.offset < offseti)
2041 : : {
2042 : 6257 : HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2043 : 6257 : gcc_assert (pd.size > o);
2044 : 6257 : pd.size -= o;
2045 : 6257 : pd.offset += o;
2046 : : }
2047 : 630330 : if (pd.size > maxsizei)
2048 : 5473 : pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
2049 : : }
2050 : :
2051 : 665607 : pd.offset -= offseti;
2052 : :
2053 : 1331214 : bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2054 : 665607 : || CONSTANT_CLASS_P (pd.rhs));
2055 : 665607 : pd_range *r;
2056 : 665607 : if (partial_defs.is_empty ())
2057 : : {
2058 : : /* If we get a clobber upfront, fail. */
2059 : 419996 : if (TREE_CLOBBER_P (pd.rhs))
2060 : : return (void *)-1;
2061 : 419632 : if (!pd_constant_p)
2062 : : return (void *)-1;
2063 : 389618 : partial_defs.safe_push (pd);
2064 : 389618 : first_range.offset = pd.offset;
2065 : 389618 : first_range.size = pd.size;
2066 : 389618 : first_set = set;
2067 : 389618 : first_base_set = base_set;
2068 : 389618 : last_vuse_ptr = NULL;
2069 : 389618 : r = &first_range;
2070 : : /* Go check if the first partial definition was a full one in case
2071 : : the caller didn't optimize for this. */
2072 : : }
2073 : : else
2074 : : {
2075 : 245611 : if (!known_ranges)
2076 : : {
2077 : : /* ??? Optimize the case where the 2nd partial def completes
2078 : : things. */
2079 : 212381 : gcc_obstack_init (&ranges_obstack);
2080 : 212381 : known_ranges.insert_max_node (&first_range);
2081 : : }
2082 : : /* Lookup the offset and see if we need to merge. */
2083 : 245611 : int comparison = known_ranges.lookup_le
2084 : 495851 : ([&] (pd_range *r) { return pd.offset < r->offset; },
2085 : 225853 : [&] (pd_range *r) { return pd.offset > r->offset; });
2086 : 245611 : r = known_ranges.root ();
2087 : 245611 : if (comparison >= 0
2088 : 245611 : && ranges_known_overlap_p (r->offset, r->size + 1,
2089 : : pd.offset, pd.size))
2090 : : {
2091 : : /* Ignore partial defs already covered. Here we also drop shadowed
2092 : : clobbers arriving here at the floor. */
2093 : 3884 : if (known_subrange_p (pd.offset, pd.size, r->offset, r->size))
2094 : : return NULL;
2095 : 3210 : r->size = MAX (r->offset + r->size, pd.offset + pd.size) - r->offset;
2096 : : }
2097 : : else
2098 : : {
2099 : : /* pd.offset wasn't covered yet, insert the range. */
2100 : 241727 : void *addr = XOBNEW (&ranges_obstack, pd_range);
2101 : 241727 : r = new (addr) pd_range { pd.offset, pd.size, {} };
2102 : 241727 : known_ranges.insert_relative (comparison, r);
2103 : : }
2104 : : /* Merge r which now contains pd's range and is a member of the splay
2105 : : tree with adjacent overlapping ranges. */
2106 : 244937 : if (known_ranges.splay_next_node ())
2107 : 21834 : do
2108 : : {
2109 : 21834 : pd_range *rafter = known_ranges.root ();
2110 : 21834 : if (!ranges_known_overlap_p (r->offset, r->size + 1,
2111 : 21834 : rafter->offset, rafter->size))
2112 : : break;
2113 : 21440 : r->size = MAX (r->offset + r->size,
2114 : 21440 : rafter->offset + rafter->size) - r->offset;
2115 : : }
2116 : 21440 : while (known_ranges.remove_root_and_splay_next ());
2117 : : /* If we get a clobber, fail. */
2118 : 244937 : if (TREE_CLOBBER_P (pd.rhs))
2119 : : return (void *)-1;
2120 : : /* Non-constants are OK as long as they are shadowed by a constant. */
2121 : 244502 : if (!pd_constant_p)
2122 : : return (void *)-1;
2123 : 237711 : partial_defs.safe_push (pd);
2124 : : }
2125 : :
2126 : : /* Now we have merged pd's range into the range tree. When we have covered
2127 : : [offseti, sizei] then the tree will contain exactly one node which has
2128 : : the desired properties and it will be 'r'. */
2129 : 627329 : if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2130 : : /* Continue looking for partial defs. */
2131 : : return NULL;
2132 : :
2133 : : /* Now simply native encode all partial defs in reverse order. */
2134 : 8402 : unsigned ndefs = partial_defs.length ();
2135 : : /* We support up to 512-bit values (for V8DFmode). */
2136 : 8402 : unsigned char buffer[bufsize + 1];
2137 : 8402 : unsigned char this_buffer[bufsize + 1];
2138 : 8402 : int len;
2139 : :
2140 : 8402 : memset (buffer, 0, bufsize + 1);
2141 : 8402 : unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2142 : 40869 : while (!partial_defs.is_empty ())
2143 : : {
2144 : 24065 : pd_data pd = partial_defs.pop ();
2145 : 24065 : unsigned int amnt;
2146 : 24065 : if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2147 : : {
2148 : : /* Empty CONSTRUCTOR. */
2149 : 2241 : if (pd.size >= needed_len * BITS_PER_UNIT)
2150 : 2241 : len = needed_len;
2151 : : else
2152 : 2027 : len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2153 : 2241 : memset (this_buffer, 0, len);
2154 : : }
2155 : 21824 : else if (pd.rhs_off >= 0)
2156 : : {
2157 : 43648 : len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2158 : 21824 : (MAX (0, -pd.offset)
2159 : 21824 : + pd.rhs_off) / BITS_PER_UNIT);
2160 : 21824 : if (len <= 0
2161 : 21824 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2162 : 21824 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2163 : : {
2164 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2165 : 0 : fprintf (dump_file, "Failed to encode %u "
2166 : : "partial definitions\n", ndefs);
2167 : 0 : return (void *)-1;
2168 : : }
2169 : : }
2170 : : else /* negative pd.rhs_off indicates we want to chop off first bits */
2171 : : {
2172 : 0 : if (-pd.rhs_off >= bufsize)
2173 : : return (void *)-1;
2174 : 0 : len = native_encode_expr (pd.rhs,
2175 : 0 : this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2176 : 0 : bufsize - -pd.rhs_off / BITS_PER_UNIT,
2177 : 0 : MAX (0, -pd.offset) / BITS_PER_UNIT);
2178 : 0 : if (len <= 0
2179 : 0 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2180 : 0 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2181 : : {
2182 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2183 : 0 : fprintf (dump_file, "Failed to encode %u "
2184 : : "partial definitions\n", ndefs);
2185 : 0 : return (void *)-1;
2186 : : }
2187 : : }
2188 : :
2189 : 24065 : unsigned char *p = buffer;
2190 : 24065 : HOST_WIDE_INT size = pd.size;
2191 : 24065 : if (pd.offset < 0)
2192 : 230 : size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2193 : 24065 : this_buffer[len] = 0;
2194 : 24065 : if (BYTES_BIG_ENDIAN)
2195 : : {
2196 : : /* LSB of this_buffer[len - 1] byte should be at
2197 : : pd.offset + pd.size - 1 bits in buffer. */
2198 : : amnt = ((unsigned HOST_WIDE_INT) pd.offset
2199 : : + pd.size) % BITS_PER_UNIT;
2200 : : if (amnt)
2201 : : shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2202 : : unsigned char *q = this_buffer;
2203 : : unsigned int off = 0;
2204 : : if (pd.offset >= 0)
2205 : : {
2206 : : unsigned int msk;
2207 : : off = pd.offset / BITS_PER_UNIT;
2208 : : gcc_assert (off < needed_len);
2209 : : p = buffer + off;
2210 : : if (size <= amnt)
2211 : : {
2212 : : msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2213 : : *p = (*p & ~msk) | (this_buffer[len] & msk);
2214 : : size = 0;
2215 : : }
2216 : : else
2217 : : {
2218 : : if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2219 : : q = (this_buffer + len
2220 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2221 : : / BITS_PER_UNIT));
2222 : : if (pd.offset % BITS_PER_UNIT)
2223 : : {
2224 : : msk = -1U << (BITS_PER_UNIT
2225 : : - (pd.offset % BITS_PER_UNIT));
2226 : : *p = (*p & msk) | (*q & ~msk);
2227 : : p++;
2228 : : q++;
2229 : : off++;
2230 : : size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2231 : : gcc_assert (size >= 0);
2232 : : }
2233 : : }
2234 : : }
2235 : : else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2236 : : {
2237 : : q = (this_buffer + len
2238 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2239 : : / BITS_PER_UNIT));
2240 : : if (pd.offset % BITS_PER_UNIT)
2241 : : {
2242 : : q++;
2243 : : size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2244 : : % BITS_PER_UNIT);
2245 : : gcc_assert (size >= 0);
2246 : : }
2247 : : }
2248 : : if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2249 : : > needed_len)
2250 : : size = (needed_len - off) * BITS_PER_UNIT;
2251 : : memcpy (p, q, size / BITS_PER_UNIT);
2252 : : if (size % BITS_PER_UNIT)
2253 : : {
2254 : : unsigned int msk
2255 : : = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2256 : : p += size / BITS_PER_UNIT;
2257 : : q += size / BITS_PER_UNIT;
2258 : : *p = (*q & msk) | (*p & ~msk);
2259 : : }
2260 : : }
2261 : : else
2262 : : {
2263 : 24065 : if (pd.offset >= 0)
2264 : : {
2265 : : /* LSB of this_buffer[0] byte should be at pd.offset bits
2266 : : in buffer. */
2267 : 23835 : unsigned int msk;
2268 : 23835 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2269 : 23835 : amnt = pd.offset % BITS_PER_UNIT;
2270 : 23835 : if (amnt)
2271 : 1796 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2272 : 23835 : unsigned int off = pd.offset / BITS_PER_UNIT;
2273 : 23835 : gcc_assert (off < needed_len);
2274 : 23835 : size = MIN (size,
2275 : : (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2276 : 23835 : p = buffer + off;
2277 : 23835 : if (amnt + size < BITS_PER_UNIT)
2278 : : {
2279 : : /* Low amnt bits come from *p, then size bits
2280 : : from this_buffer[0] and the remaining again from
2281 : : *p. */
2282 : 1378 : msk = ((1 << size) - 1) << amnt;
2283 : 1378 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2284 : 1378 : size = 0;
2285 : : }
2286 : 22457 : else if (amnt)
2287 : : {
2288 : 1369 : msk = -1U << amnt;
2289 : 1369 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2290 : 1369 : p++;
2291 : 1369 : size -= (BITS_PER_UNIT - amnt);
2292 : : }
2293 : : }
2294 : : else
2295 : : {
2296 : 230 : amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2297 : 230 : if (amnt)
2298 : 16 : size -= BITS_PER_UNIT - amnt;
2299 : 230 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2300 : 230 : if (amnt)
2301 : 16 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2302 : : }
2303 : 24065 : memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2304 : 24065 : p += size / BITS_PER_UNIT;
2305 : 24065 : if (size % BITS_PER_UNIT)
2306 : : {
2307 : 621 : unsigned int msk = -1U << (size % BITS_PER_UNIT);
2308 : 621 : *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2309 : 621 : & ~msk) | (*p & msk);
2310 : : }
2311 : : }
2312 : : }
2313 : :
2314 : 8402 : tree type = vr->type;
2315 : : /* Make sure to interpret in a type that has a range covering the whole
2316 : : access size. */
2317 : 8402 : if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2318 : 13 : type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2319 : 8402 : tree val;
2320 : 8402 : if (BYTES_BIG_ENDIAN)
2321 : : {
2322 : : unsigned sz = needed_len;
2323 : : if (maxsizei % BITS_PER_UNIT)
2324 : : shift_bytes_in_array_right (buffer, needed_len,
2325 : : BITS_PER_UNIT
2326 : : - (maxsizei % BITS_PER_UNIT));
2327 : : if (INTEGRAL_TYPE_P (type))
2328 : : {
2329 : : if (TYPE_MODE (type) != BLKmode)
2330 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2331 : : else
2332 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2333 : : }
2334 : : if (sz > needed_len)
2335 : : {
2336 : : memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2337 : : val = native_interpret_expr (type, this_buffer, sz);
2338 : : }
2339 : : else
2340 : : val = native_interpret_expr (type, buffer, needed_len);
2341 : : }
2342 : : else
2343 : 8402 : val = native_interpret_expr (type, buffer, bufsize);
2344 : : /* If we chop off bits because the types precision doesn't match the memory
2345 : : access size this is ok when optimizing reads but not when called from
2346 : : the DSE code during elimination. */
2347 : 8402 : if (val && type != vr->type)
2348 : : {
2349 : 13 : if (! int_fits_type_p (val, vr->type))
2350 : : val = NULL_TREE;
2351 : : else
2352 : 13 : val = fold_convert (vr->type, val);
2353 : : }
2354 : :
2355 : 8398 : if (val)
2356 : : {
2357 : 8398 : if (dump_file && (dump_flags & TDF_DETAILS))
2358 : 0 : fprintf (dump_file,
2359 : : "Successfully combined %u partial definitions\n", ndefs);
2360 : : /* We are using the alias-set of the first store we encounter which
2361 : : should be appropriate here. */
2362 : 8398 : return finish (first_set, first_base_set, val);
2363 : : }
2364 : : else
2365 : : {
2366 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
2367 : 0 : fprintf (dump_file,
2368 : : "Failed to interpret %u encoded partial definitions\n", ndefs);
2369 : 4 : return (void *)-1;
2370 : : }
2371 : : }
2372 : :
2373 : : /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2374 : : with the current VUSE and performs the expression lookup. */
2375 : :
2376 : : static void *
2377 : 1063892596 : vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2378 : : {
2379 : 1063892596 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2380 : 1063892596 : vn_reference_t vr = data->vr;
2381 : 1063892596 : vn_reference_s **slot;
2382 : 1063892596 : hashval_t hash;
2383 : :
2384 : : /* If we have partial definitions recorded we have to go through
2385 : : vn_reference_lookup_3. */
2386 : 2119828592 : if (!data->partial_defs.is_empty ())
2387 : : return NULL;
2388 : :
2389 : 1062969557 : if (data->last_vuse_ptr)
2390 : : {
2391 : 1041208807 : *data->last_vuse_ptr = vuse;
2392 : 1041208807 : data->last_vuse = vuse;
2393 : : }
2394 : :
2395 : : /* Fixup vuse and hash. */
2396 : 1062969557 : if (vr->vuse)
2397 : 1062969557 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2398 : 1062969557 : vr->vuse = vuse_ssa_val (vuse);
2399 : 1062969557 : if (vr->vuse)
2400 : 1062969557 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2401 : :
2402 : 1062969557 : hash = vr->hashcode;
2403 : 1062969557 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2404 : 1062969557 : if (slot)
2405 : : {
2406 : 7955360 : if ((*slot)->result && data->saved_operands.exists ())
2407 : 347234 : return data->finish (vr->set, vr->base_set, (*slot)->result);
2408 : : return *slot;
2409 : : }
2410 : :
2411 : 1055014197 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2412 : : {
2413 : 18025190 : HOST_WIDE_INT op_offset, op_size;
2414 : 18025190 : tree v = NULL_TREE;
2415 : 18025190 : tree base = ao_ref_base (op);
2416 : :
2417 : 18025190 : if (base
2418 : 18025190 : && op->offset.is_constant (&op_offset)
2419 : 18025190 : && op->size.is_constant (&op_size)
2420 : 18025190 : && op->max_size_known_p ()
2421 : 35597408 : && known_eq (op->size, op->max_size))
2422 : : {
2423 : 17275692 : if (TREE_CODE (base) == PARM_DECL)
2424 : 705259 : v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2425 : : op_size);
2426 : 16570433 : else if (TREE_CODE (base) == MEM_REF
2427 : 6858985 : && integer_zerop (TREE_OPERAND (base, 1))
2428 : 5552726 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2429 : 5546764 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2430 : 20293932 : && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2431 : : == PARM_DECL))
2432 : 3669706 : v = ipcp_get_aggregate_const (cfun,
2433 : 3669706 : SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2434 : : true, op_offset, op_size);
2435 : : }
2436 : 4374965 : if (v)
2437 : 1240 : return data->finish (vr->set, vr->base_set, v);
2438 : : }
2439 : :
2440 : : return NULL;
2441 : : }
2442 : :
2443 : : /* Lookup an existing or insert a new vn_reference entry into the
2444 : : value table for the VUSE, SET, TYPE, OPERANDS reference which
2445 : : has the value VALUE which is either a constant or an SSA name. */
2446 : :
2447 : : static vn_reference_t
2448 : 1472663 : vn_reference_lookup_or_insert_for_pieces (tree vuse,
2449 : : alias_set_type set,
2450 : : alias_set_type base_set,
2451 : : poly_int64 offset,
2452 : : poly_int64 max_size,
2453 : : tree type,
2454 : : vec<vn_reference_op_s,
2455 : : va_heap> operands,
2456 : : tree value)
2457 : : {
2458 : 1472663 : vn_reference_s vr1;
2459 : 1472663 : vn_reference_t result;
2460 : 1472663 : unsigned value_id;
2461 : 1472663 : vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2462 : 1472663 : vr1.operands = operands;
2463 : 1472663 : vr1.type = type;
2464 : 1472663 : vr1.set = set;
2465 : 1472663 : vr1.base_set = base_set;
2466 : 1472663 : vr1.offset = offset;
2467 : 1472663 : vr1.max_size = max_size;
2468 : 1472663 : vr1.hashcode = vn_reference_compute_hash (&vr1);
2469 : 1472663 : if (vn_reference_lookup_1 (&vr1, &result))
2470 : 5259 : return result;
2471 : :
2472 : 1467404 : if (TREE_CODE (value) == SSA_NAME)
2473 : 285684 : value_id = VN_INFO (value)->value_id;
2474 : : else
2475 : 1181720 : value_id = get_or_alloc_constant_value_id (value);
2476 : 1467404 : return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
2477 : 1467404 : type, operands.copy (), value, value_id);
2478 : : }
2479 : :
2480 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2481 : : value-number for the possibly simplified result or by inserting the
2482 : : operation if INSERT is true. If SIMPLIFY is false, return a value
2483 : : number for the unsimplified expression. */
2484 : :
2485 : : static tree
2486 : 18421899 : vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2487 : : bool simplify)
2488 : : {
2489 : 18421899 : tree result = NULL_TREE;
2490 : : /* We will be creating a value number for
2491 : : RCODE (OPS...).
2492 : : So first simplify and lookup this expression to see if it
2493 : : is already available. */
2494 : : /* For simplification valueize. */
2495 : 18421899 : unsigned i = 0;
2496 : 18421899 : if (simplify)
2497 : 42793910 : for (i = 0; i < res_op->num_ops; ++i)
2498 : 24378466 : if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2499 : : {
2500 : 15598742 : tree tem = vn_valueize (res_op->ops[i]);
2501 : 15598742 : if (!tem)
2502 : : break;
2503 : 15598742 : res_op->ops[i] = tem;
2504 : : }
2505 : : /* If valueization of an operand fails (it is not available), skip
2506 : : simplification. */
2507 : 18421899 : bool res = false;
2508 : 18421899 : if (i == res_op->num_ops)
2509 : : {
2510 : : /* Do not leak not available operands into the simplified expression
2511 : : when called from PRE context. */
2512 : 18415444 : if (rpo_avail)
2513 : 10954840 : mprts_hook = vn_lookup_simplify_result;
2514 : 18415444 : res = res_op->resimplify (NULL, vn_valueize);
2515 : 18415444 : mprts_hook = NULL;
2516 : : }
2517 : 31696560 : gimple *new_stmt = NULL;
2518 : 18415444 : if (res
2519 : 18415444 : && gimple_simplified_result_is_gimple_val (res_op))
2520 : : {
2521 : : /* The expression is already available. */
2522 : 5140783 : result = res_op->ops[0];
2523 : : /* Valueize it, simplification returns sth in AVAIL only. */
2524 : 5140783 : if (TREE_CODE (result) == SSA_NAME)
2525 : 290625 : result = SSA_VAL (result);
2526 : : }
2527 : : else
2528 : : {
2529 : 13281116 : tree val = vn_lookup_simplify_result (res_op);
2530 : : /* ??? In weird cases we can end up with internal-fn calls,
2531 : : but this isn't expected so throw the result away. See
2532 : : PR123040 for an example. */
2533 : 13281116 : if (!val && insert && res_op->code.is_tree_code ())
2534 : : {
2535 : 140269 : gimple_seq stmts = NULL;
2536 : 140269 : result = maybe_push_res_to_seq (res_op, &stmts);
2537 : 140269 : if (result)
2538 : : {
2539 : 140263 : gcc_assert (gimple_seq_singleton_p (stmts));
2540 : 140263 : new_stmt = gimple_seq_first_stmt (stmts);
2541 : : }
2542 : : }
2543 : : else
2544 : : /* The expression is already available. */
2545 : : result = val;
2546 : : }
2547 : 290631 : if (new_stmt)
2548 : : {
2549 : : /* The expression is not yet available, value-number lhs to
2550 : : the new SSA_NAME we created. */
2551 : : /* Initialize value-number information properly. */
2552 : 140263 : vn_ssa_aux_t result_info = VN_INFO (result);
2553 : 140263 : result_info->valnum = result;
2554 : 140263 : result_info->value_id = get_next_value_id ();
2555 : 140263 : result_info->visited = 1;
2556 : 140263 : gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2557 : : new_stmt);
2558 : 140263 : result_info->needs_insertion = true;
2559 : : /* ??? PRE phi-translation inserts NARYs without corresponding
2560 : : SSA name result. Re-use those but set their result according
2561 : : to the stmt we just built. */
2562 : 140263 : vn_nary_op_t nary = NULL;
2563 : 140263 : vn_nary_op_lookup_stmt (new_stmt, &nary);
2564 : 140263 : if (nary)
2565 : : {
2566 : 0 : gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2567 : 0 : nary->u.result = gimple_assign_lhs (new_stmt);
2568 : : }
2569 : : /* As all "inserted" statements are singleton SCCs, insert
2570 : : to the valid table. This is strictly needed to
2571 : : avoid re-generating new value SSA_NAMEs for the same
2572 : : expression during SCC iteration over and over (the
2573 : : optimistic table gets cleared after each iteration).
2574 : : We do not need to insert into the optimistic table, as
2575 : : lookups there will fall back to the valid table. */
2576 : : else
2577 : : {
2578 : 140263 : unsigned int length = vn_nary_length_from_stmt (new_stmt);
2579 : 140263 : vn_nary_op_t vno1
2580 : 140263 : = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2581 : 140263 : vno1->value_id = result_info->value_id;
2582 : 140263 : vno1->length = length;
2583 : 140263 : vno1->predicated_values = 0;
2584 : 140263 : vno1->u.result = result;
2585 : 140263 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2586 : 140263 : vn_nary_op_insert_into (vno1, valid_info->nary);
2587 : : /* Also do not link it into the undo chain. */
2588 : 140263 : last_inserted_nary = vno1->next;
2589 : 140263 : vno1->next = (vn_nary_op_t)(void *)-1;
2590 : : }
2591 : 140263 : if (dump_file && (dump_flags & TDF_DETAILS))
2592 : : {
2593 : 590 : fprintf (dump_file, "Inserting name ");
2594 : 590 : print_generic_expr (dump_file, result);
2595 : 590 : fprintf (dump_file, " for expression ");
2596 : 590 : print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2597 : 590 : fprintf (dump_file, "\n");
2598 : : }
2599 : : }
2600 : 18421899 : return result;
2601 : : }
2602 : :
2603 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2604 : : value-number for the simplified result or by inserting the operation. */
2605 : :
2606 : : static tree
2607 : 183672 : vn_nary_build_or_lookup (gimple_match_op *res_op)
2608 : : {
2609 : 0 : return vn_nary_build_or_lookup_1 (res_op, true, true);
2610 : : }
2611 : :
2612 : : /* Try to simplify the expression RCODE OPS... of type TYPE and return
2613 : : its value if present. Update NARY with a simplified expression if
2614 : : it fits. */
2615 : :
2616 : : tree
2617 : 7455096 : vn_nary_simplify (vn_nary_op_t nary)
2618 : : {
2619 : 7455096 : if (nary->length > gimple_match_op::MAX_NUM_OPS
2620 : : /* For CONSTRUCTOR the vn_nary_op_t and gimple_match_op representation
2621 : : does not match. */
2622 : 7454920 : || nary->opcode == CONSTRUCTOR)
2623 : : return NULL_TREE;
2624 : 7454506 : gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2625 : 7454506 : nary->type, nary->length);
2626 : 7454506 : memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2627 : 7454506 : tree res = vn_nary_build_or_lookup_1 (&op, false, true);
2628 : : /* Do not update *NARY with a simplified result that contains abnormals.
2629 : : This matches what maybe_push_res_to_seq does when requesting insertion. */
2630 : 19543717 : for (unsigned i = 0; i < op.num_ops; ++i)
2631 : 12089290 : if (TREE_CODE (op.ops[i]) == SSA_NAME
2632 : 12089290 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op.ops[i]))
2633 : : return res;
2634 : 7454427 : if (op.code.is_tree_code ()
2635 : 7454427 : && op.num_ops <= nary->length
2636 : 14908142 : && (tree_code) op.code != CONSTRUCTOR)
2637 : : {
2638 : 7453714 : nary->opcode = (tree_code) op.code;
2639 : 7453714 : nary->length = op.num_ops;
2640 : 19541496 : for (unsigned i = 0; i < op.num_ops; ++i)
2641 : 12087782 : nary->op[i] = op.ops[i];
2642 : : }
2643 : : return res;
2644 : : }
2645 : :
2646 : : /* Elimination engine. */
2647 : :
2648 : : class eliminate_dom_walker : public dom_walker
2649 : : {
2650 : : public:
2651 : : eliminate_dom_walker (cdi_direction, bitmap);
2652 : : ~eliminate_dom_walker ();
2653 : :
2654 : : edge before_dom_children (basic_block) final override;
2655 : : void after_dom_children (basic_block) final override;
2656 : :
2657 : : virtual tree eliminate_avail (basic_block, tree op);
2658 : : virtual void eliminate_push_avail (basic_block, tree op);
2659 : : tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2660 : :
2661 : : void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2662 : :
2663 : : unsigned eliminate_cleanup (bool region_p = false);
2664 : :
2665 : : bool do_pre;
2666 : : unsigned int el_todo;
2667 : : unsigned int eliminations;
2668 : : unsigned int insertions;
2669 : :
2670 : : /* SSA names that had their defs inserted by PRE if do_pre. */
2671 : : bitmap inserted_exprs;
2672 : :
2673 : : /* Blocks with statements that have had their EH properties changed. */
2674 : : bitmap need_eh_cleanup;
2675 : :
2676 : : /* Blocks with statements that have had their AB properties changed. */
2677 : : bitmap need_ab_cleanup;
2678 : :
2679 : : /* Local state for the eliminate domwalk. */
2680 : : auto_vec<gimple *> to_remove;
2681 : : auto_vec<gimple *> to_fixup;
2682 : : auto_vec<tree> avail;
2683 : : auto_vec<tree> avail_stack;
2684 : : };
2685 : :
2686 : : /* Adaptor to the elimination engine using RPO availability. */
2687 : :
2688 : 12467250 : class rpo_elim : public eliminate_dom_walker
2689 : : {
2690 : : public:
2691 : 6233625 : rpo_elim(basic_block entry_)
2692 : 12467250 : : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2693 : 12467250 : m_avail_freelist (NULL) {}
2694 : :
2695 : : tree eliminate_avail (basic_block, tree op) final override;
2696 : :
2697 : : void eliminate_push_avail (basic_block, tree) final override;
2698 : :
2699 : : basic_block entry;
2700 : : /* Freelist of avail entries which are allocated from the vn_ssa_aux
2701 : : obstack. */
2702 : : vn_avail *m_avail_freelist;
2703 : : };
2704 : :
2705 : : /* Return true if BASE1 and BASE2 can be adjusted so they have the
2706 : : same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2707 : : Otherwise return false. */
2708 : :
2709 : : static bool
2710 : 6807709 : adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2711 : : tree base2, poly_int64 *offset2)
2712 : : {
2713 : 6807709 : poly_int64 soff;
2714 : 6807709 : if (TREE_CODE (base1) == MEM_REF
2715 : 3086059 : && TREE_CODE (base2) == MEM_REF)
2716 : : {
2717 : 2462147 : if (mem_ref_offset (base1).to_shwi (&soff))
2718 : : {
2719 : 2462147 : base1 = TREE_OPERAND (base1, 0);
2720 : 2462147 : *offset1 += soff * BITS_PER_UNIT;
2721 : : }
2722 : 2462147 : if (mem_ref_offset (base2).to_shwi (&soff))
2723 : : {
2724 : 2462147 : base2 = TREE_OPERAND (base2, 0);
2725 : 2462147 : *offset2 += soff * BITS_PER_UNIT;
2726 : : }
2727 : 2462147 : return operand_equal_p (base1, base2, 0);
2728 : : }
2729 : 4345562 : return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2730 : : }
2731 : :
2732 : : /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2733 : : from the statement defining VUSE and if not successful tries to
2734 : : translate *REFP and VR_ through an aggregate copy at the definition
2735 : : of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2736 : : of *REF and *VR. If only disambiguation was performed then
2737 : : *DISAMBIGUATE_ONLY is set to true. */
2738 : :
2739 : : static void *
2740 : 42777134 : vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2741 : : translate_flags *disambiguate_only)
2742 : : {
2743 : 42777134 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2744 : 42777134 : vn_reference_t vr = data->vr;
2745 : 42777134 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2746 : 42777134 : tree base = ao_ref_base (ref);
2747 : 42777134 : HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2748 : 42777134 : static vec<vn_reference_op_s> lhs_ops;
2749 : 42777134 : ao_ref lhs_ref;
2750 : 42777134 : bool lhs_ref_ok = false;
2751 : 42777134 : poly_int64 copy_size;
2752 : :
2753 : : /* First try to disambiguate after value-replacing in the definitions LHS. */
2754 : 42777134 : if (is_gimple_assign (def_stmt))
2755 : : {
2756 : 20749075 : tree lhs = gimple_assign_lhs (def_stmt);
2757 : 20749075 : bool valueized_anything = false;
2758 : : /* Avoid re-allocation overhead. */
2759 : 20749075 : lhs_ops.truncate (0);
2760 : 20749075 : basic_block saved_rpo_bb = vn_context_bb;
2761 : 20749075 : vn_context_bb = gimple_bb (def_stmt);
2762 : 20749075 : if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2763 : : {
2764 : 13910250 : copy_reference_ops_from_ref (lhs, &lhs_ops);
2765 : 13910250 : valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2766 : : }
2767 : 20749075 : vn_context_bb = saved_rpo_bb;
2768 : 20749075 : ao_ref_init (&lhs_ref, lhs);
2769 : 20749075 : lhs_ref_ok = true;
2770 : 20749075 : if (valueized_anything
2771 : 1805811 : && ao_ref_init_from_vn_reference
2772 : 1805811 : (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2773 : 1805811 : ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2774 : 22554886 : && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2775 : : {
2776 : 1516765 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2777 : 7845133 : return NULL;
2778 : : }
2779 : :
2780 : : /* When the def is a CLOBBER we can optimistically disambiguate
2781 : : against it since any overlap it would be undefined behavior.
2782 : : Avoid this for obvious must aliases to save compile-time though.
2783 : : We also may not do this when the query is used for redundant
2784 : : store removal. */
2785 : 19232310 : if (!data->redundant_store_removal_p
2786 : 10646214 : && gimple_clobber_p (def_stmt)
2787 : 19684841 : && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2788 : : {
2789 : 425973 : *disambiguate_only = TR_DISAMBIGUATE;
2790 : 425973 : return NULL;
2791 : : }
2792 : :
2793 : : /* Besides valueizing the LHS we can also use access-path based
2794 : : disambiguation on the original non-valueized ref. */
2795 : 18806337 : if (!ref->ref
2796 : : && lhs_ref_ok
2797 : 2690265 : && data->orig_ref.ref)
2798 : : {
2799 : : /* We want to use the non-valueized LHS for this, but avoid redundant
2800 : : work. */
2801 : 1807431 : ao_ref *lref = &lhs_ref;
2802 : 1807431 : ao_ref lref_alt;
2803 : 1807431 : if (valueized_anything)
2804 : : {
2805 : 117769 : ao_ref_init (&lref_alt, lhs);
2806 : 117769 : lref = &lref_alt;
2807 : : }
2808 : 1807431 : if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2809 : : {
2810 : 252618 : *disambiguate_only = (valueized_anything
2811 : 126309 : ? TR_VALUEIZE_AND_DISAMBIGUATE
2812 : : : TR_DISAMBIGUATE);
2813 : 126309 : return NULL;
2814 : : }
2815 : : }
2816 : :
2817 : : /* If we reach a clobbering statement try to skip it and see if
2818 : : we find a VN result with exactly the same value as the
2819 : : possible clobber. In this case we can ignore the clobber
2820 : : and return the found value. */
2821 : 18680028 : if (!gimple_has_volatile_ops (def_stmt)
2822 : 17391791 : && ((is_gimple_reg_type (TREE_TYPE (lhs))
2823 : 12771505 : && types_compatible_p (TREE_TYPE (lhs), vr->type)
2824 : 9805592 : && !storage_order_barrier_p (lhs)
2825 : 9805592 : && !reverse_storage_order_for_component_p (lhs))
2826 : 7586201 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == CONSTRUCTOR)
2827 : 10870338 : && (ref->ref || data->orig_ref.ref)
2828 : 10447728 : && !data->mask
2829 : 10426133 : && data->partial_defs.is_empty ()
2830 : 10424004 : && multiple_p (get_object_alignment
2831 : : (ref->ref ? ref->ref : data->orig_ref.ref),
2832 : : ref->size)
2833 : 41832868 : && multiple_p (get_object_alignment (lhs), ref->size))
2834 : : {
2835 : 10020370 : HOST_WIDE_INT offset2i, size2i;
2836 : 10020370 : poly_int64 offset = ref->offset;
2837 : 10020370 : poly_int64 maxsize = ref->max_size;
2838 : :
2839 : 10020370 : gcc_assert (lhs_ref_ok);
2840 : 10020370 : tree base2 = ao_ref_base (&lhs_ref);
2841 : 10020370 : poly_int64 offset2 = lhs_ref.offset;
2842 : 10020370 : poly_int64 size2 = lhs_ref.size;
2843 : 10020370 : poly_int64 maxsize2 = lhs_ref.max_size;
2844 : :
2845 : 10020370 : tree rhs = gimple_assign_rhs1 (def_stmt);
2846 : 10020370 : if (TREE_CODE (rhs) == CONSTRUCTOR)
2847 : 1036519 : rhs = integer_zero_node;
2848 : : /* ??? We may not compare to ahead values which might be from
2849 : : a different loop iteration but only to loop invariants. Use
2850 : : CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2851 : : The one-hop lookup below doesn't have this issue since there's
2852 : : a virtual PHI before we ever reach a backedge to cross.
2853 : : We can skip multiple defs as long as they are from the same
2854 : : value though. */
2855 : 10020370 : if (data->same_val
2856 : 10020370 : && !operand_equal_p (data->same_val, rhs))
2857 : : ;
2858 : : /* When this is a (partial) must-def, leave it to handling
2859 : : below in case we are interested in the value. */
2860 : 9731160 : else if (!(*disambiguate_only > TR_TRANSLATE)
2861 : 3343769 : && base2
2862 : 3343769 : && known_eq (maxsize2, size2)
2863 : 2353049 : && adjust_offsets_for_equal_base_address (base, &offset,
2864 : : base2, &offset2)
2865 : 1145499 : && offset2.is_constant (&offset2i)
2866 : 1145499 : && size2.is_constant (&size2i)
2867 : 1145499 : && maxsize.is_constant (&maxsizei)
2868 : 1145499 : && offset.is_constant (&offseti)
2869 : 10876659 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
2870 : : size2i))
2871 : : ;
2872 : 8671615 : else if (CONSTANT_CLASS_P (rhs))
2873 : : {
2874 : 4195954 : if (dump_file && (dump_flags & TDF_DETAILS))
2875 : : {
2876 : 1908 : fprintf (dump_file,
2877 : : "Skipping possible redundant definition ");
2878 : 1908 : print_gimple_stmt (dump_file, def_stmt, 0);
2879 : : }
2880 : : /* Delay the actual compare of the values to the end of the walk
2881 : : but do not update last_vuse from here. */
2882 : 4195954 : data->last_vuse_ptr = NULL;
2883 : 4195954 : data->same_val = rhs;
2884 : 4259321 : return NULL;
2885 : : }
2886 : : else
2887 : : {
2888 : 4475661 : tree saved_vuse = vr->vuse;
2889 : 4475661 : hashval_t saved_hashcode = vr->hashcode;
2890 : 4475661 : if (vr->vuse)
2891 : 4475661 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2892 : 8951322 : vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
2893 : 4475661 : if (vr->vuse)
2894 : 4475661 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2895 : 4475661 : vn_reference_t vnresult = NULL;
2896 : : /* Do not use vn_reference_lookup_2 since that might perform
2897 : : expression hashtable insertion but this lookup crosses
2898 : : a possible may-alias making such insertion conditionally
2899 : : invalid. */
2900 : 4475661 : vn_reference_lookup_1 (vr, &vnresult);
2901 : : /* Need to restore vr->vuse and vr->hashcode. */
2902 : 4475661 : vr->vuse = saved_vuse;
2903 : 4475661 : vr->hashcode = saved_hashcode;
2904 : 4475661 : if (vnresult)
2905 : : {
2906 : 250763 : if (TREE_CODE (rhs) == SSA_NAME)
2907 : 249231 : rhs = SSA_VAL (rhs);
2908 : 250763 : if (vnresult->result
2909 : 250763 : && operand_equal_p (vnresult->result, rhs, 0))
2910 : 63367 : return vnresult;
2911 : : }
2912 : : }
2913 : : }
2914 : : }
2915 : 22028059 : else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2916 : 20597562 : && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2917 : 24156103 : && gimple_call_num_args (def_stmt) <= 4)
2918 : : {
2919 : : /* For builtin calls valueize its arguments and call the
2920 : : alias oracle again. Valueization may improve points-to
2921 : : info of pointers and constify size and position arguments.
2922 : : Originally this was motivated by PR61034 which has
2923 : : conditional calls to free falsely clobbering ref because
2924 : : of imprecise points-to info of the argument. */
2925 : : tree oldargs[4];
2926 : : bool valueized_anything = false;
2927 : 5208611 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2928 : : {
2929 : 3605610 : oldargs[i] = gimple_call_arg (def_stmt, i);
2930 : 3605610 : tree val = vn_valueize (oldargs[i]);
2931 : 3605610 : if (val != oldargs[i])
2932 : : {
2933 : 131638 : gimple_call_set_arg (def_stmt, i, val);
2934 : 131638 : valueized_anything = true;
2935 : : }
2936 : : }
2937 : 1603001 : if (valueized_anything)
2938 : : {
2939 : 192266 : bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2940 : 96133 : ref, data->tbaa_p);
2941 : 352057 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2942 : 255924 : gimple_call_set_arg (def_stmt, i, oldargs[i]);
2943 : 96133 : if (!res)
2944 : : {
2945 : 24258 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2946 : 24258 : return NULL;
2947 : : }
2948 : : }
2949 : : }
2950 : :
2951 : 36424508 : if (*disambiguate_only > TR_TRANSLATE)
2952 : : return (void *)-1;
2953 : :
2954 : : /* If we cannot constrain the size of the reference we cannot
2955 : : test if anything kills it. */
2956 : 24417307 : if (!ref->max_size_known_p ())
2957 : : return (void *)-1;
2958 : :
2959 : 23998239 : poly_int64 offset = ref->offset;
2960 : 23998239 : poly_int64 maxsize = ref->max_size;
2961 : :
2962 : : /* def_stmt may-defs *ref. See if we can derive a value for *ref
2963 : : from that definition.
2964 : : 1) Memset. */
2965 : 23998239 : if (is_gimple_reg_type (vr->type)
2966 : 23675884 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2967 : 23587767 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
2968 : 88659 : && (integer_zerop (gimple_call_arg (def_stmt, 1))
2969 : 32687 : || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2970 : 8199 : || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2971 : : && CHAR_BIT == 8
2972 : : && BITS_PER_UNIT == 8
2973 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2974 : 31855 : && offset.is_constant (&offseti)
2975 : 31855 : && ref->size.is_constant (&sizei)
2976 : 31855 : && (offseti % BITS_PER_UNIT == 0
2977 : 39 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
2978 : 87827 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2979 : 36394 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
2980 : 36394 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
2981 : 24050210 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2982 : 28582 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2983 : : {
2984 : 51930 : tree base2;
2985 : 51930 : poly_int64 offset2, size2, maxsize2;
2986 : 51930 : bool reverse;
2987 : 51930 : tree ref2 = gimple_call_arg (def_stmt, 0);
2988 : 51930 : if (TREE_CODE (ref2) == SSA_NAME)
2989 : : {
2990 : 28541 : ref2 = SSA_VAL (ref2);
2991 : 28541 : if (TREE_CODE (ref2) == SSA_NAME
2992 : 28541 : && (TREE_CODE (base) != MEM_REF
2993 : 18433 : || TREE_OPERAND (base, 0) != ref2))
2994 : : {
2995 : 22323 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2996 : 22323 : if (gimple_assign_single_p (def_stmt)
2997 : 22323 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2998 : 822 : ref2 = gimple_assign_rhs1 (def_stmt);
2999 : : }
3000 : : }
3001 : 51930 : if (TREE_CODE (ref2) == ADDR_EXPR)
3002 : : {
3003 : 27195 : ref2 = TREE_OPERAND (ref2, 0);
3004 : 27195 : base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
3005 : : &reverse);
3006 : 27195 : if (!known_size_p (maxsize2)
3007 : 27155 : || !known_eq (maxsize2, size2)
3008 : 54276 : || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
3009 : 55887 : return (void *)-1;
3010 : : }
3011 : 24735 : else if (TREE_CODE (ref2) == SSA_NAME)
3012 : : {
3013 : 24735 : poly_int64 soff;
3014 : 24735 : if (TREE_CODE (base) != MEM_REF
3015 : 42567 : || !(mem_ref_offset (base)
3016 : 35664 : << LOG2_BITS_PER_UNIT).to_shwi (&soff))
3017 : 20904 : return (void *)-1;
3018 : 17832 : offset += soff;
3019 : 17832 : offset2 = 0;
3020 : 17832 : if (TREE_OPERAND (base, 0) != ref2)
3021 : : {
3022 : 14598 : gimple *def = SSA_NAME_DEF_STMT (ref2);
3023 : 14598 : if (is_gimple_assign (def)
3024 : 13496 : && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
3025 : 11562 : && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
3026 : 15225 : && poly_int_tree_p (gimple_assign_rhs2 (def)))
3027 : : {
3028 : 597 : tree rhs2 = gimple_assign_rhs2 (def);
3029 : 597 : if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
3030 : : SIGNED)
3031 : 597 : << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
3032 : : return (void *)-1;
3033 : 597 : ref2 = gimple_assign_rhs1 (def);
3034 : 597 : if (TREE_CODE (ref2) == SSA_NAME)
3035 : 597 : ref2 = SSA_VAL (ref2);
3036 : : }
3037 : : else
3038 : : return (void *)-1;
3039 : : }
3040 : : }
3041 : : else
3042 : : return (void *)-1;
3043 : 26684 : tree len = gimple_call_arg (def_stmt, 2);
3044 : 26684 : HOST_WIDE_INT leni, offset2i;
3045 : 26684 : if (TREE_CODE (len) == SSA_NAME)
3046 : 254 : len = SSA_VAL (len);
3047 : : /* Sometimes the above trickery is smarter than alias analysis. Take
3048 : : advantage of that. */
3049 : 26684 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
3050 : 53368 : (wi::to_poly_offset (len)
3051 : 26684 : << LOG2_BITS_PER_UNIT)))
3052 : : return NULL;
3053 : 53325 : if (data->partial_defs.is_empty ()
3054 : 26641 : && known_subrange_p (offset, maxsize, offset2,
3055 : 26641 : wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
3056 : : {
3057 : 26146 : tree val;
3058 : 26146 : if (integer_zerop (gimple_call_arg (def_stmt, 1)))
3059 : 21018 : val = build_zero_cst (vr->type);
3060 : 5128 : else if (INTEGRAL_TYPE_P (vr->type)
3061 : 3984 : && known_eq (ref->size, 8)
3062 : 8344 : && offseti % BITS_PER_UNIT == 0)
3063 : : {
3064 : 3216 : gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
3065 : 3216 : vr->type, gimple_call_arg (def_stmt, 1));
3066 : 3216 : val = vn_nary_build_or_lookup (&res_op);
3067 : 3216 : if (!val
3068 : 3216 : || (TREE_CODE (val) == SSA_NAME
3069 : 626 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3070 : 0 : return (void *)-1;
3071 : : }
3072 : : else
3073 : : {
3074 : 1912 : unsigned buflen
3075 : 1912 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
3076 : 1912 : if (INTEGRAL_TYPE_P (vr->type)
3077 : 1912 : && TYPE_MODE (vr->type) != BLKmode)
3078 : 1534 : buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
3079 : 1912 : unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
3080 : 1912 : memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
3081 : : buflen);
3082 : 1912 : if (BYTES_BIG_ENDIAN)
3083 : : {
3084 : : unsigned int amnt
3085 : : = (((unsigned HOST_WIDE_INT) offseti + sizei)
3086 : : % BITS_PER_UNIT);
3087 : : if (amnt)
3088 : : {
3089 : : shift_bytes_in_array_right (buf, buflen,
3090 : : BITS_PER_UNIT - amnt);
3091 : : buf++;
3092 : : buflen--;
3093 : : }
3094 : : }
3095 : 1912 : else if (offseti % BITS_PER_UNIT != 0)
3096 : : {
3097 : 7 : unsigned int amnt
3098 : : = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3099 : 7 : % BITS_PER_UNIT);
3100 : 7 : shift_bytes_in_array_left (buf, buflen, amnt);
3101 : 7 : buf++;
3102 : 7 : buflen--;
3103 : : }
3104 : 1912 : val = native_interpret_expr (vr->type, buf, buflen);
3105 : 1912 : if (!val)
3106 : : return (void *)-1;
3107 : : }
3108 : 26146 : return data->finish (0, 0, val);
3109 : : }
3110 : : /* For now handle clearing memory with partial defs. */
3111 : 538 : else if (known_eq (ref->size, maxsize)
3112 : 468 : && integer_zerop (gimple_call_arg (def_stmt, 1))
3113 : 157 : && tree_fits_poly_int64_p (len)
3114 : 153 : && tree_to_poly_int64 (len).is_constant (&leni)
3115 : 153 : && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3116 : 153 : && offset.is_constant (&offseti)
3117 : 153 : && offset2.is_constant (&offset2i)
3118 : 153 : && maxsize.is_constant (&maxsizei)
3119 : 538 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3120 : 538 : leni << LOG2_BITS_PER_UNIT))
3121 : : {
3122 : 153 : pd_data pd;
3123 : 153 : pd.rhs = build_constructor (NULL_TREE, NULL);
3124 : 153 : pd.rhs_off = 0;
3125 : 153 : pd.offset = offset2i;
3126 : 153 : pd.size = leni << LOG2_BITS_PER_UNIT;
3127 : 153 : return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3128 : : }
3129 : : }
3130 : :
3131 : : /* 2) Assignment from an empty CONSTRUCTOR. */
3132 : 23946309 : else if (is_gimple_reg_type (vr->type)
3133 : 23623954 : && gimple_assign_single_p (def_stmt)
3134 : 7684926 : && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3135 : 1899647 : && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0
3136 : 25845956 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt)))
3137 : : {
3138 : 1899591 : tree base2;
3139 : 1899591 : poly_int64 offset2, size2, maxsize2;
3140 : 1899591 : HOST_WIDE_INT offset2i, size2i;
3141 : 1899591 : gcc_assert (lhs_ref_ok);
3142 : 1899591 : base2 = ao_ref_base (&lhs_ref);
3143 : 1899591 : offset2 = lhs_ref.offset;
3144 : 1899591 : size2 = lhs_ref.size;
3145 : 1899591 : maxsize2 = lhs_ref.max_size;
3146 : 1899591 : if (known_size_p (maxsize2)
3147 : 1899553 : && known_eq (maxsize2, size2)
3148 : 3799098 : && adjust_offsets_for_equal_base_address (base, &offset,
3149 : : base2, &offset2))
3150 : : {
3151 : 1871813 : if (data->partial_defs.is_empty ()
3152 : 1870132 : && known_subrange_p (offset, maxsize, offset2, size2))
3153 : : {
3154 : : /* While technically undefined behavior do not optimize
3155 : : a full read from a clobber. */
3156 : 1869342 : if (gimple_clobber_p (def_stmt))
3157 : 1871759 : return (void *)-1;
3158 : 986394 : tree val = build_zero_cst (vr->type);
3159 : 986394 : return data->finish (ao_ref_alias_set (&lhs_ref),
3160 : 986394 : ao_ref_base_alias_set (&lhs_ref), val);
3161 : : }
3162 : 2471 : else if (known_eq (ref->size, maxsize)
3163 : 2417 : && maxsize.is_constant (&maxsizei)
3164 : 2417 : && offset.is_constant (&offseti)
3165 : 2417 : && offset2.is_constant (&offset2i)
3166 : 2417 : && size2.is_constant (&size2i)
3167 : 2471 : && ranges_known_overlap_p (offseti, maxsizei,
3168 : : offset2i, size2i))
3169 : : {
3170 : : /* Let clobbers be consumed by the partial-def tracker
3171 : : which can choose to ignore them if they are shadowed
3172 : : by a later def. */
3173 : 2417 : pd_data pd;
3174 : 2417 : pd.rhs = gimple_assign_rhs1 (def_stmt);
3175 : 2417 : pd.rhs_off = 0;
3176 : 2417 : pd.offset = offset2i;
3177 : 2417 : pd.size = size2i;
3178 : 2417 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3179 : : ao_ref_base_alias_set (&lhs_ref),
3180 : : offseti, maxsizei);
3181 : : }
3182 : : }
3183 : : }
3184 : :
3185 : : /* 3) Assignment from a constant. We can use folds native encode/interpret
3186 : : routines to extract the assigned bits. */
3187 : 22046718 : else if (known_eq (ref->size, maxsize)
3188 : 21498728 : && is_gimple_reg_type (vr->type)
3189 : 21176373 : && !reverse_storage_order_for_component_p (vr->operands)
3190 : 21173646 : && !contains_storage_order_barrier_p (vr->operands)
3191 : 21173646 : && gimple_assign_single_p (def_stmt)
3192 : 5432523 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3193 : : && CHAR_BIT == 8
3194 : : && BITS_PER_UNIT == 8
3195 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3196 : : /* native_encode and native_decode operate on arrays of bytes
3197 : : and so fundamentally need a compile-time size and offset. */
3198 : 5429601 : && maxsize.is_constant (&maxsizei)
3199 : 5429601 : && offset.is_constant (&offseti)
3200 : 27476319 : && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3201 : 4591270 : || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3202 : 1870392 : && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3203 : : {
3204 : 853448 : tree lhs = gimple_assign_lhs (def_stmt);
3205 : 853448 : tree base2;
3206 : 853448 : poly_int64 offset2, size2, maxsize2;
3207 : 853448 : HOST_WIDE_INT offset2i, size2i;
3208 : 853448 : bool reverse;
3209 : 853448 : gcc_assert (lhs_ref_ok);
3210 : 853448 : base2 = ao_ref_base (&lhs_ref);
3211 : 853448 : offset2 = lhs_ref.offset;
3212 : 853448 : size2 = lhs_ref.size;
3213 : 853448 : maxsize2 = lhs_ref.max_size;
3214 : 853448 : reverse = reverse_storage_order_for_component_p (lhs);
3215 : 853448 : if (base2
3216 : 853448 : && !reverse
3217 : 852620 : && !storage_order_barrier_p (lhs)
3218 : 852620 : && known_eq (maxsize2, size2)
3219 : 825765 : && adjust_offsets_for_equal_base_address (base, &offset,
3220 : : base2, &offset2)
3221 : 78858 : && offset.is_constant (&offseti)
3222 : 78858 : && offset2.is_constant (&offset2i)
3223 : 853448 : && size2.is_constant (&size2i))
3224 : : {
3225 : 78858 : if (data->partial_defs.is_empty ()
3226 : 63384 : && known_subrange_p (offseti, maxsizei, offset2, size2))
3227 : : {
3228 : : /* We support up to 512-bit values (for V8DFmode). */
3229 : 43470 : unsigned char buffer[65];
3230 : 43470 : int len;
3231 : :
3232 : 43470 : tree rhs = gimple_assign_rhs1 (def_stmt);
3233 : 43470 : if (TREE_CODE (rhs) == SSA_NAME)
3234 : 1415 : rhs = SSA_VAL (rhs);
3235 : 86940 : len = native_encode_expr (rhs,
3236 : : buffer, sizeof (buffer) - 1,
3237 : 43470 : (offseti - offset2i) / BITS_PER_UNIT);
3238 : 43470 : if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
3239 : : {
3240 : 40448 : tree type = vr->type;
3241 : 40448 : unsigned char *buf = buffer;
3242 : 40448 : unsigned int amnt = 0;
3243 : : /* Make sure to interpret in a type that has a range
3244 : : covering the whole access size. */
3245 : 40448 : if (INTEGRAL_TYPE_P (vr->type)
3246 : 40448 : && maxsizei != TYPE_PRECISION (vr->type))
3247 : 1714 : type = build_nonstandard_integer_type (maxsizei,
3248 : 857 : TYPE_UNSIGNED (type));
3249 : 40448 : if (BYTES_BIG_ENDIAN)
3250 : : {
3251 : : /* For big-endian native_encode_expr stored the rhs
3252 : : such that the LSB of it is the LSB of buffer[len - 1].
3253 : : That bit is stored into memory at position
3254 : : offset2 + size2 - 1, i.e. in byte
3255 : : base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3256 : : E.g. for offset2 1 and size2 14, rhs -1 and memory
3257 : : previously cleared that is:
3258 : : 0 1
3259 : : 01111111|11111110
3260 : : Now, if we want to extract offset 2 and size 12 from
3261 : : it using native_interpret_expr (which actually works
3262 : : for integral bitfield types in terms of byte size of
3263 : : the mode), the native_encode_expr stored the value
3264 : : into buffer as
3265 : : XX111111|11111111
3266 : : and returned len 2 (the X bits are outside of
3267 : : precision).
3268 : : Let sz be maxsize / BITS_PER_UNIT if not extracting
3269 : : a bitfield, and GET_MODE_SIZE otherwise.
3270 : : We need to align the LSB of the value we want to
3271 : : extract as the LSB of buf[sz - 1].
3272 : : The LSB from memory we need to read is at position
3273 : : offset + maxsize - 1. */
3274 : : HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3275 : : if (INTEGRAL_TYPE_P (type))
3276 : : {
3277 : : if (TYPE_MODE (type) != BLKmode)
3278 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3279 : : else
3280 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3281 : : }
3282 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3283 : : - offseti - maxsizei) % BITS_PER_UNIT;
3284 : : if (amnt)
3285 : : shift_bytes_in_array_right (buffer, len, amnt);
3286 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3287 : : - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3288 : : if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3289 : : len = 0;
3290 : : else
3291 : : {
3292 : : buf = buffer + len - sz - amnt;
3293 : : len -= (buf - buffer);
3294 : : }
3295 : : }
3296 : : else
3297 : : {
3298 : 40448 : amnt = ((unsigned HOST_WIDE_INT) offset2i
3299 : 40448 : - offseti) % BITS_PER_UNIT;
3300 : 40448 : if (amnt)
3301 : : {
3302 : 305 : buffer[len] = 0;
3303 : 305 : shift_bytes_in_array_left (buffer, len + 1, amnt);
3304 : 305 : buf = buffer + 1;
3305 : : }
3306 : : }
3307 : 40448 : tree val = native_interpret_expr (type, buf, len);
3308 : : /* If we chop off bits because the types precision doesn't
3309 : : match the memory access size this is ok when optimizing
3310 : : reads but not when called from the DSE code during
3311 : : elimination. */
3312 : 40448 : if (val
3313 : 40446 : && type != vr->type)
3314 : : {
3315 : 857 : if (! int_fits_type_p (val, vr->type))
3316 : : val = NULL_TREE;
3317 : : else
3318 : 857 : val = fold_convert (vr->type, val);
3319 : : }
3320 : :
3321 : 40446 : if (val)
3322 : 40446 : return data->finish (ao_ref_alias_set (&lhs_ref),
3323 : 40446 : ao_ref_base_alias_set (&lhs_ref), val);
3324 : : }
3325 : : }
3326 : 35388 : else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3327 : : size2i))
3328 : : {
3329 : 35388 : pd_data pd;
3330 : 35388 : tree rhs = gimple_assign_rhs1 (def_stmt);
3331 : 35388 : if (TREE_CODE (rhs) == SSA_NAME)
3332 : 2539 : rhs = SSA_VAL (rhs);
3333 : 35388 : pd.rhs = rhs;
3334 : 35388 : pd.rhs_off = 0;
3335 : 35388 : pd.offset = offset2i;
3336 : 35388 : pd.size = size2i;
3337 : 35388 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3338 : : ao_ref_base_alias_set (&lhs_ref),
3339 : : offseti, maxsizei);
3340 : : }
3341 : : }
3342 : : }
3343 : :
3344 : : /* 4) Assignment from an SSA name which definition we may be able
3345 : : to access pieces from or we can combine to a larger entity. */
3346 : 21193270 : else if (known_eq (ref->size, maxsize)
3347 : 20645280 : && is_gimple_reg_type (vr->type)
3348 : 20322925 : && !reverse_storage_order_for_component_p (vr->operands)
3349 : 20320198 : && !contains_storage_order_barrier_p (vr->operands)
3350 : 20320198 : && gimple_assign_single_p (def_stmt)
3351 : 4579075 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3352 : 25769423 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3353 : : {
3354 : 1855275 : tree lhs = gimple_assign_lhs (def_stmt);
3355 : 1855275 : tree base2;
3356 : 1855275 : poly_int64 offset2, size2, maxsize2;
3357 : 1855275 : HOST_WIDE_INT offset2i, size2i, offseti;
3358 : 1855275 : bool reverse;
3359 : 1855275 : gcc_assert (lhs_ref_ok);
3360 : 1855275 : base2 = ao_ref_base (&lhs_ref);
3361 : 1855275 : offset2 = lhs_ref.offset;
3362 : 1855275 : size2 = lhs_ref.size;
3363 : 1855275 : maxsize2 = lhs_ref.max_size;
3364 : 1855275 : reverse = reverse_storage_order_for_component_p (lhs);
3365 : 1855275 : tree def_rhs = gimple_assign_rhs1 (def_stmt);
3366 : 1855275 : if (!reverse
3367 : 1855063 : && !storage_order_barrier_p (lhs)
3368 : 1855063 : && known_size_p (maxsize2)
3369 : 1831765 : && known_eq (maxsize2, size2)
3370 : 3584649 : && adjust_offsets_for_equal_base_address (base, &offset,
3371 : : base2, &offset2))
3372 : : {
3373 : 80547 : if (data->partial_defs.is_empty ()
3374 : 73766 : && known_subrange_p (offset, maxsize, offset2, size2)
3375 : : /* ??? We can't handle bitfield precision extracts without
3376 : : either using an alternate type for the BIT_FIELD_REF and
3377 : : then doing a conversion or possibly adjusting the offset
3378 : : according to endianness. */
3379 : 48757 : && (! INTEGRAL_TYPE_P (vr->type)
3380 : 35226 : || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3381 : 90954 : && multiple_p (ref->size, BITS_PER_UNIT))
3382 : : {
3383 : 43721 : tree val = NULL_TREE;
3384 : 87436 : if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3385 : 48146 : || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3386 : : {
3387 : 42589 : gimple_match_op op (gimple_match_cond::UNCOND,
3388 : 42589 : BIT_FIELD_REF, vr->type,
3389 : : SSA_VAL (def_rhs),
3390 : : bitsize_int (ref->size),
3391 : 42589 : bitsize_int (offset - offset2));
3392 : 42589 : val = vn_nary_build_or_lookup (&op);
3393 : : }
3394 : 1132 : else if (known_eq (ref->size, size2))
3395 : : {
3396 : 1110 : gimple_match_op op (gimple_match_cond::UNCOND,
3397 : 1110 : VIEW_CONVERT_EXPR, vr->type,
3398 : 1110 : SSA_VAL (def_rhs));
3399 : 1110 : val = vn_nary_build_or_lookup (&op);
3400 : : }
3401 : 43699 : if (val
3402 : 43699 : && (TREE_CODE (val) != SSA_NAME
3403 : 42899 : || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3404 : 43680 : return data->finish (ao_ref_alias_set (&lhs_ref),
3405 : 80506 : ao_ref_base_alias_set (&lhs_ref), val);
3406 : : }
3407 : 36826 : else if (maxsize.is_constant (&maxsizei)
3408 : 36826 : && offset.is_constant (&offseti)
3409 : 36826 : && offset2.is_constant (&offset2i)
3410 : 36826 : && size2.is_constant (&size2i)
3411 : 36826 : && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3412 : : {
3413 : 36826 : pd_data pd;
3414 : 36826 : pd.rhs = SSA_VAL (def_rhs);
3415 : 36826 : pd.rhs_off = 0;
3416 : 36826 : pd.offset = offset2i;
3417 : 36826 : pd.size = size2i;
3418 : 36826 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3419 : : ao_ref_base_alias_set (&lhs_ref),
3420 : : offseti, maxsizei);
3421 : : }
3422 : : }
3423 : : }
3424 : :
3425 : : /* 4b) Assignment done via one of the vectorizer internal store
3426 : : functions where we may be able to access pieces from or we can
3427 : : combine to a larger entity. */
3428 : 19337995 : else if (known_eq (ref->size, maxsize)
3429 : 18790005 : && is_gimple_reg_type (vr->type)
3430 : 18467650 : && !reverse_storage_order_for_component_p (vr->operands)
3431 : 18464923 : && !contains_storage_order_barrier_p (vr->operands)
3432 : 18464923 : && is_gimple_call (def_stmt)
3433 : 14985222 : && gimple_call_internal_p (def_stmt)
3434 : 19733436 : && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3435 : : {
3436 : 46 : gcall *call = as_a <gcall *> (def_stmt);
3437 : 46 : internal_fn fn = gimple_call_internal_fn (call);
3438 : :
3439 : 46 : tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3440 : 46 : switch (fn)
3441 : : {
3442 : 46 : case IFN_MASK_STORE:
3443 : 46 : mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3444 : 46 : mask = vn_valueize (mask);
3445 : 46 : if (TREE_CODE (mask) != VECTOR_CST)
3446 : 38 : return (void *)-1;
3447 : : break;
3448 : 0 : case IFN_LEN_STORE:
3449 : 0 : {
3450 : 0 : int len_index = internal_fn_len_index (fn);
3451 : 0 : len = gimple_call_arg (call, len_index);
3452 : 0 : bias = gimple_call_arg (call, len_index + 1);
3453 : 0 : if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3454 : : return (void *) -1;
3455 : : break;
3456 : : }
3457 : : default:
3458 : : return (void *)-1;
3459 : : }
3460 : 14 : tree def_rhs = gimple_call_arg (call,
3461 : 14 : internal_fn_stored_value_index (fn));
3462 : 14 : def_rhs = vn_valueize (def_rhs);
3463 : 14 : if (TREE_CODE (def_rhs) != VECTOR_CST)
3464 : : return (void *)-1;
3465 : :
3466 : 14 : ao_ref_init_from_ptr_and_size (&lhs_ref,
3467 : : vn_valueize (gimple_call_arg (call, 0)),
3468 : 14 : TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3469 : 14 : tree base2;
3470 : 14 : poly_int64 offset2, size2, maxsize2;
3471 : 14 : HOST_WIDE_INT offset2i, size2i, offseti;
3472 : 14 : base2 = ao_ref_base (&lhs_ref);
3473 : 14 : offset2 = lhs_ref.offset;
3474 : 14 : size2 = lhs_ref.size;
3475 : 14 : maxsize2 = lhs_ref.max_size;
3476 : 14 : if (known_size_p (maxsize2)
3477 : 14 : && known_eq (maxsize2, size2)
3478 : 14 : && adjust_offsets_for_equal_base_address (base, &offset,
3479 : : base2, &offset2)
3480 : 6 : && maxsize.is_constant (&maxsizei)
3481 : 6 : && offset.is_constant (&offseti)
3482 : 6 : && offset2.is_constant (&offset2i)
3483 : 14 : && size2.is_constant (&size2i))
3484 : : {
3485 : 6 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3486 : : /* Poor-mans disambiguation. */
3487 : : return NULL;
3488 : 6 : else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3489 : : {
3490 : 6 : pd_data pd;
3491 : 6 : pd.rhs = def_rhs;
3492 : 6 : tree aa = gimple_call_arg (call, 1);
3493 : 6 : alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3494 : 6 : tree vectype = TREE_TYPE (def_rhs);
3495 : 6 : unsigned HOST_WIDE_INT elsz
3496 : 6 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3497 : 6 : if (mask)
3498 : : {
3499 : : HOST_WIDE_INT start = 0, length = 0;
3500 : : unsigned mask_idx = 0;
3501 : 48 : do
3502 : : {
3503 : 48 : if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3504 : : {
3505 : 24 : if (length != 0)
3506 : : {
3507 : 18 : pd.rhs_off = start;
3508 : 18 : pd.offset = offset2i + start;
3509 : 18 : pd.size = length;
3510 : 18 : if (ranges_known_overlap_p
3511 : 18 : (offset, maxsize, pd.offset, pd.size))
3512 : : {
3513 : 0 : void *res = data->push_partial_def
3514 : 0 : (pd, set, set, offseti, maxsizei);
3515 : 0 : if (res != NULL)
3516 : 6 : return res;
3517 : : }
3518 : : }
3519 : 24 : start = (mask_idx + 1) * elsz;
3520 : 24 : length = 0;
3521 : : }
3522 : : else
3523 : 24 : length += elsz;
3524 : 48 : mask_idx++;
3525 : : }
3526 : 48 : while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3527 : 6 : if (length != 0)
3528 : : {
3529 : 6 : pd.rhs_off = start;
3530 : 6 : pd.offset = offset2i + start;
3531 : 6 : pd.size = length;
3532 : 6 : if (ranges_known_overlap_p (offset, maxsize,
3533 : : pd.offset, pd.size))
3534 : 2 : return data->push_partial_def (pd, set, set,
3535 : 2 : offseti, maxsizei);
3536 : : }
3537 : : }
3538 : 0 : else if (fn == IFN_LEN_STORE)
3539 : : {
3540 : 0 : pd.offset = offset2i;
3541 : 0 : pd.size = (tree_to_uhwi (len)
3542 : 0 : + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3543 : 0 : if (BYTES_BIG_ENDIAN)
3544 : : pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3545 : : else
3546 : 0 : pd.rhs_off = 0;
3547 : 0 : if (ranges_known_overlap_p (offset, maxsize,
3548 : : pd.offset, pd.size))
3549 : 0 : return data->push_partial_def (pd, set, set,
3550 : 0 : offseti, maxsizei);
3551 : : }
3552 : : else
3553 : 0 : gcc_unreachable ();
3554 : 4 : return NULL;
3555 : : }
3556 : : }
3557 : : }
3558 : :
3559 : : /* 5) For aggregate copies translate the reference through them if
3560 : : the copy kills ref. */
3561 : 19337949 : else if (data->vn_walk_kind == VN_WALKREWRITE
3562 : 15330792 : && gimple_assign_single_p (def_stmt)
3563 : 2517970 : && !gimple_has_volatile_ops (def_stmt)
3564 : 21853637 : && (DECL_P (gimple_assign_rhs1 (def_stmt))
3565 : 2009798 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3566 : 1601617 : || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3567 : : {
3568 : 2282197 : tree base2;
3569 : 2282197 : int i, j, k;
3570 : 2282197 : auto_vec<vn_reference_op_s> rhs;
3571 : 2282197 : vn_reference_op_t vro;
3572 : 2282197 : ao_ref r;
3573 : :
3574 : 2282197 : gcc_assert (lhs_ref_ok);
3575 : :
3576 : : /* See if the assignment kills REF. */
3577 : 2282197 : base2 = ao_ref_base (&lhs_ref);
3578 : 2282197 : if (!lhs_ref.max_size_known_p ()
3579 : 2281766 : || (base != base2
3580 : 99721 : && (TREE_CODE (base) != MEM_REF
3581 : 85649 : || TREE_CODE (base2) != MEM_REF
3582 : 70622 : || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3583 : 33012 : || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3584 : 33012 : TREE_OPERAND (base2, 1))))
3585 : 4495471 : || !stmt_kills_ref_p (def_stmt, ref))
3586 : 424276 : return (void *)-1;
3587 : :
3588 : : /* Find the common base of ref and the lhs. lhs_ops already
3589 : : contains valueized operands for the lhs. */
3590 : 1857921 : poly_int64 extra_off = 0;
3591 : 1857921 : i = vr->operands.length () - 1;
3592 : 1857921 : j = lhs_ops.length () - 1;
3593 : :
3594 : : /* The base should be always equal due to the above check. */
3595 : 1857921 : if (! vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3596 : : return (void *)-1;
3597 : 1857753 : i--, j--;
3598 : :
3599 : : /* The 2nd component should always exist and be a MEM_REF. */
3600 : 1857753 : if (!(i >= 0 && j >= 0))
3601 : : ;
3602 : 1857753 : else if (vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3603 : 869568 : i--, j--;
3604 : 988185 : else if (vr->operands[i].opcode == MEM_REF
3605 : 986637 : && lhs_ops[j].opcode == MEM_REF
3606 : 986637 : && known_ne (lhs_ops[j].off, -1)
3607 : 1974822 : && known_ne (vr->operands[i].off, -1))
3608 : : {
3609 : 986637 : bool found = false;
3610 : : /* When we ge a mismatch at a MEM_REF that is not the sole component
3611 : : try finding a match in one of the outer components and continue
3612 : : stripping there. This happens when addresses of components get
3613 : : forwarded into dereferences. */
3614 : 986637 : if (i > 0)
3615 : : {
3616 : 112024 : int temi = i - 1;
3617 : 112024 : extra_off = vr->operands[i].off;
3618 : 112024 : while (temi >= 0
3619 : 242326 : && known_ne (vr->operands[temi].off, -1))
3620 : : {
3621 : 131696 : if (vr->operands[temi].type
3622 : 131696 : && lhs_ops[j].type
3623 : 263392 : && (TYPE_MAIN_VARIANT (vr->operands[temi].type)
3624 : 131696 : == TYPE_MAIN_VARIANT (lhs_ops[j].type)))
3625 : : {
3626 : 1394 : i = temi;
3627 : : /* Strip the component that was type matched to
3628 : : the MEM_REF. */
3629 : 1394 : extra_off += vr->operands[i].off - lhs_ops[j].off;
3630 : 1394 : i--, j--;
3631 : : /* Strip further equal components. */
3632 : 1394 : found = true;
3633 : 1394 : break;
3634 : : }
3635 : 130302 : extra_off += vr->operands[temi].off;
3636 : 130302 : temi--;
3637 : : }
3638 : : }
3639 : 986637 : if (!found && j > 0)
3640 : : {
3641 : 28824 : int temj = j - 1;
3642 : 28824 : extra_off = -lhs_ops[j].off;
3643 : 28824 : while (temj >= 0
3644 : 55085 : && known_ne (lhs_ops[temj].off, -1))
3645 : : {
3646 : 30677 : if (vr->operands[i].type
3647 : 30677 : && lhs_ops[temj].type
3648 : 61354 : && (TYPE_MAIN_VARIANT (vr->operands[i].type)
3649 : 30677 : == TYPE_MAIN_VARIANT (lhs_ops[temj].type)))
3650 : : {
3651 : 4416 : j = temj;
3652 : : /* Strip the component that was type matched to
3653 : : the MEM_REF. */
3654 : 4416 : extra_off += vr->operands[i].off - lhs_ops[j].off;
3655 : 4416 : i--, j--;
3656 : : /* Strip further equal components. */
3657 : 4416 : found = true;
3658 : 4416 : break;
3659 : : }
3660 : 26261 : extra_off += -lhs_ops[temj].off;
3661 : 26261 : temj--;
3662 : : }
3663 : : }
3664 : : /* When the LHS is already at the outermost level simply
3665 : : adjust for any offset difference. Further lookups
3666 : : will fail when there's too gross of a type compatibility
3667 : : issue. */
3668 : 986637 : if (!found && j == 0)
3669 : : {
3670 : 956419 : extra_off = vr->operands[i].off - lhs_ops[j].off;
3671 : 956419 : i--, j--;
3672 : 956419 : found = true;
3673 : : }
3674 : : /* If we did find a match we'd eventually append a MEM_REF
3675 : : as component. Don't. */
3676 : 986637 : if (!found)
3677 : : return (void *)-1;
3678 : : }
3679 : : else
3680 : : return (void *)-1;
3681 : :
3682 : : /* Strip further common components, attempting to consume lhs_ops
3683 : : in full. */
3684 : 1863088 : while (j >= 0 && i >= 0
3685 : 1863088 : && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3686 : : {
3687 : 31291 : i--;
3688 : 31291 : j--;
3689 : : }
3690 : :
3691 : : /* When we still didn't manage to strip off all components from
3692 : : lhs_op, opportunistically continue for those we can handle
3693 : : via extra_off. Note this is an attempt to fixup secondary
3694 : : copies after we hit the !found && j == 0 case above. */
3695 : : while (j != -1
3696 : 1834879 : && known_ne (lhs_ops[j].off, -1U))
3697 : : {
3698 : 3082 : extra_off += -lhs_ops[j].off;
3699 : 3082 : j--;
3700 : : }
3701 : :
3702 : : /* i now points to the first additional op.
3703 : : ??? LHS may not be completely contained in VR, one or more
3704 : : VIEW_CONVERT_EXPRs could be in its way. We could at least
3705 : : try handling outermost VIEW_CONVERT_EXPRs. */
3706 : 1831797 : if (j != -1)
3707 : : return (void *)-1;
3708 : :
3709 : : /* Punt if the additional ops contain a storage order barrier. */
3710 : 2998510 : for (k = i; k >= 0; k--)
3711 : : {
3712 : 1166713 : vro = &vr->operands[k];
3713 : 1166713 : if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3714 : : return (void *)-1;
3715 : : }
3716 : :
3717 : : /* Now re-write REF to be based on the rhs of the assignment. */
3718 : 1831797 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
3719 : 1831797 : copy_reference_ops_from_ref (rhs1, &rhs);
3720 : :
3721 : : /* Apply an extra offset to the inner MEM_REF of the RHS. */
3722 : 1831797 : bool force_no_tbaa = false;
3723 : 1831797 : if (maybe_ne (extra_off, 0))
3724 : : {
3725 : 654651 : if (rhs.length () < 2)
3726 : : return (void *)-1;
3727 : 654651 : int ix = rhs.length () - 2;
3728 : 654651 : if (rhs[ix].opcode != MEM_REF
3729 : 654651 : || known_eq (rhs[ix].off, -1))
3730 : : return (void *)-1;
3731 : 654647 : rhs[ix].off += extra_off;
3732 : 654647 : rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3733 : 654647 : build_int_cst (TREE_TYPE (rhs[ix].op0),
3734 : : extra_off));
3735 : : /* When we have offsetted the RHS, reading only parts of it,
3736 : : we can no longer use the original TBAA type, force alias-set
3737 : : zero. */
3738 : 654647 : force_no_tbaa = true;
3739 : : }
3740 : :
3741 : : /* Save the operands since we need to use the original ones for
3742 : : the hash entry we use. */
3743 : 1831793 : if (!data->saved_operands.exists ())
3744 : 1750675 : data->saved_operands = vr->operands.copy ();
3745 : :
3746 : : /* We need to pre-pend vr->operands[0..i] to rhs. */
3747 : 1831793 : vec<vn_reference_op_s> old = vr->operands;
3748 : 5495379 : if (i + 1 + rhs.length () > vr->operands.length ())
3749 : 1145930 : vr->operands.safe_grow (i + 1 + rhs.length (), true);
3750 : : else
3751 : 685863 : vr->operands.truncate (i + 1 + rhs.length ());
3752 : 6678952 : FOR_EACH_VEC_ELT (rhs, j, vro)
3753 : 4847159 : vr->operands[i + 1 + j] = *vro;
3754 : 1831793 : valueize_refs (&vr->operands);
3755 : 3663586 : if (old == shared_lookup_references)
3756 : 1831793 : shared_lookup_references = vr->operands;
3757 : 1831793 : vr->hashcode = vn_reference_compute_hash (vr);
3758 : :
3759 : : /* Try folding the new reference to a constant. */
3760 : 1831793 : tree val = fully_constant_vn_reference_p (vr);
3761 : 1831793 : if (val)
3762 : : {
3763 : 21551 : if (data->partial_defs.is_empty ())
3764 : 21542 : return data->finish (ao_ref_alias_set (&lhs_ref),
3765 : 21542 : ao_ref_base_alias_set (&lhs_ref), val);
3766 : : /* This is the only interesting case for partial-def handling
3767 : : coming from targets that like to gimplify init-ctors as
3768 : : aggregate copies from constant data like aarch64 for
3769 : : PR83518. */
3770 : 9 : if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3771 : : {
3772 : 9 : pd_data pd;
3773 : 9 : pd.rhs = val;
3774 : 9 : pd.rhs_off = 0;
3775 : 9 : pd.offset = 0;
3776 : 9 : pd.size = maxsizei;
3777 : 9 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3778 : : ao_ref_base_alias_set (&lhs_ref),
3779 : : 0, maxsizei);
3780 : : }
3781 : : }
3782 : :
3783 : : /* Continuing with partial defs isn't easily possible here, we
3784 : : have to find a full def from further lookups from here. Probably
3785 : : not worth the special-casing everywhere. */
3786 : 2267844 : if (!data->partial_defs.is_empty ())
3787 : : return (void *)-1;
3788 : :
3789 : : /* Adjust *ref from the new operands. */
3790 : 1803044 : ao_ref rhs1_ref;
3791 : 1803044 : ao_ref_init (&rhs1_ref, rhs1);
3792 : 2955121 : if (!ao_ref_init_from_vn_reference (&r,
3793 : : force_no_tbaa ? 0
3794 : 1152077 : : ao_ref_alias_set (&rhs1_ref),
3795 : : force_no_tbaa ? 0
3796 : 1152077 : : ao_ref_base_alias_set (&rhs1_ref),
3797 : : vr->type, vr->operands))
3798 : : return (void *)-1;
3799 : : /* This can happen with bitfields. */
3800 : 1803044 : if (maybe_ne (ref->size, r.size))
3801 : : {
3802 : : /* If the access lacks some subsetting simply apply that by
3803 : : shortening it. That in the end can only be successful
3804 : : if we can pun the lookup result which in turn requires
3805 : : exact offsets. */
3806 : 0 : if (known_eq (r.size, r.max_size)
3807 : 0 : && known_lt (ref->size, r.size))
3808 : 0 : r.size = r.max_size = ref->size;
3809 : : else
3810 : : return (void *)-1;
3811 : : }
3812 : 1803044 : *ref = r;
3813 : 1803044 : vr->offset = r.offset;
3814 : 1803044 : vr->max_size = r.max_size;
3815 : :
3816 : : /* Do not update last seen VUSE after translating. */
3817 : 1803044 : data->last_vuse_ptr = NULL;
3818 : : /* Invalidate the original access path since it now contains
3819 : : the wrong base. */
3820 : 1803044 : data->orig_ref.ref = NULL_TREE;
3821 : : /* Use the alias-set of this LHS for recording an eventual result. */
3822 : 1803044 : if (data->first_set == -2)
3823 : : {
3824 : 1723151 : data->first_set = ao_ref_alias_set (&lhs_ref);
3825 : 1723151 : data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3826 : : }
3827 : :
3828 : : /* Keep looking for the adjusted *REF / VR pair. */
3829 : 1803044 : return NULL;
3830 : 2282197 : }
3831 : :
3832 : : /* 6) For memcpy copies translate the reference through them if the copy
3833 : : kills ref. But we cannot (easily) do this translation if the memcpy is
3834 : : a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3835 : : can modify the storage order of objects (see storage_order_barrier_p). */
3836 : 17055752 : else if (data->vn_walk_kind == VN_WALKREWRITE
3837 : 13048595 : && is_gimple_reg_type (vr->type)
3838 : : /* ??? Handle BCOPY as well. */
3839 : 13040714 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3840 : 12962032 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3841 : 12961609 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3842 : 12960423 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3843 : 12960181 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3844 : 12933164 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3845 : 107878 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3846 : 99016 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3847 : 107846 : && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3848 : 76357 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3849 : 107831 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), ©_size)
3850 : 63520 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3851 : 63520 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3852 : : ©_size)))
3853 : : /* Handling this is more complicated, give up for now. */
3854 : 17101783 : && data->partial_defs.is_empty ())
3855 : : {
3856 : 45727 : tree lhs, rhs;
3857 : 45727 : ao_ref r;
3858 : 45727 : poly_int64 rhs_offset, lhs_offset;
3859 : 45727 : vn_reference_op_s op;
3860 : 45727 : poly_uint64 mem_offset;
3861 : 45727 : poly_int64 at, byte_maxsize;
3862 : :
3863 : : /* Only handle non-variable, addressable refs. */
3864 : 45727 : if (maybe_ne (ref->size, maxsize)
3865 : 45270 : || !multiple_p (offset, BITS_PER_UNIT, &at)
3866 : 45727 : || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3867 : 457 : return (void *)-1;
3868 : :
3869 : : /* Extract a pointer base and an offset for the destination. */
3870 : 45270 : lhs = gimple_call_arg (def_stmt, 0);
3871 : 45270 : lhs_offset = 0;
3872 : 45270 : if (TREE_CODE (lhs) == SSA_NAME)
3873 : : {
3874 : 37609 : lhs = vn_valueize (lhs);
3875 : 37609 : if (TREE_CODE (lhs) == SSA_NAME)
3876 : : {
3877 : 37313 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3878 : 37313 : if (gimple_assign_single_p (def_stmt)
3879 : 37313 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3880 : 2303 : lhs = gimple_assign_rhs1 (def_stmt);
3881 : : }
3882 : : }
3883 : 45270 : if (TREE_CODE (lhs) == ADDR_EXPR)
3884 : : {
3885 : 14494 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3886 : 14197 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3887 : : return (void *)-1;
3888 : 10120 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3889 : : &lhs_offset);
3890 : 10120 : if (!tem)
3891 : : return (void *)-1;
3892 : 9506 : if (TREE_CODE (tem) == MEM_REF
3893 : 9506 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3894 : : {
3895 : 1689 : lhs = TREE_OPERAND (tem, 0);
3896 : 1689 : if (TREE_CODE (lhs) == SSA_NAME)
3897 : 1689 : lhs = vn_valueize (lhs);
3898 : 1689 : lhs_offset += mem_offset;
3899 : : }
3900 : 7817 : else if (DECL_P (tem))
3901 : 7817 : lhs = build_fold_addr_expr (tem);
3902 : : else
3903 : : return (void *)-1;
3904 : : }
3905 : 44516 : if (TREE_CODE (lhs) != SSA_NAME
3906 : 7818 : && TREE_CODE (lhs) != ADDR_EXPR)
3907 : : return (void *)-1;
3908 : :
3909 : : /* Extract a pointer base and an offset for the source. */
3910 : 44516 : rhs = gimple_call_arg (def_stmt, 1);
3911 : 44516 : rhs_offset = 0;
3912 : 44516 : if (TREE_CODE (rhs) == SSA_NAME)
3913 : 17317 : rhs = vn_valueize (rhs);
3914 : 44516 : if (TREE_CODE (rhs) == ADDR_EXPR)
3915 : : {
3916 : 39972 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3917 : 28527 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3918 : : return (void *)-1;
3919 : 28083 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3920 : : &rhs_offset);
3921 : 28083 : if (!tem)
3922 : : return (void *)-1;
3923 : 28083 : if (TREE_CODE (tem) == MEM_REF
3924 : 28083 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3925 : : {
3926 : 0 : rhs = TREE_OPERAND (tem, 0);
3927 : 0 : rhs_offset += mem_offset;
3928 : : }
3929 : 28083 : else if (DECL_P (tem)
3930 : 23789 : || TREE_CODE (tem) == STRING_CST)
3931 : 28083 : rhs = build_fold_addr_expr (tem);
3932 : : else
3933 : : return (void *)-1;
3934 : : }
3935 : 44516 : if (TREE_CODE (rhs) == SSA_NAME)
3936 : 16433 : rhs = SSA_VAL (rhs);
3937 : 28083 : else if (TREE_CODE (rhs) != ADDR_EXPR)
3938 : : return (void *)-1;
3939 : :
3940 : : /* The bases of the destination and the references have to agree. */
3941 : 44516 : if (TREE_CODE (base) == MEM_REF)
3942 : : {
3943 : 17642 : if (TREE_OPERAND (base, 0) != lhs
3944 : 17642 : || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
3945 : 14305 : return (void *) -1;
3946 : 10014 : at += mem_offset;
3947 : : }
3948 : 26874 : else if (!DECL_P (base)
3949 : 26038 : || TREE_CODE (lhs) != ADDR_EXPR
3950 : 33552 : || TREE_OPERAND (lhs, 0) != base)
3951 : : return (void *)-1;
3952 : :
3953 : : /* If the access is completely outside of the memcpy destination
3954 : : area there is no aliasing. */
3955 : 10014 : if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
3956 : : return NULL;
3957 : : /* And the access has to be contained within the memcpy destination. */
3958 : 9979 : if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
3959 : : return (void *)-1;
3960 : :
3961 : : /* Save the operands since we need to use the original ones for
3962 : : the hash entry we use. */
3963 : 9618 : if (!data->saved_operands.exists ())
3964 : 9271 : data->saved_operands = vr->operands.copy ();
3965 : :
3966 : : /* Make room for 2 operands in the new reference. */
3967 : 9618 : if (vr->operands.length () < 2)
3968 : : {
3969 : 0 : vec<vn_reference_op_s> old = vr->operands;
3970 : 0 : vr->operands.safe_grow_cleared (2, true);
3971 : 0 : if (old == shared_lookup_references)
3972 : 0 : shared_lookup_references = vr->operands;
3973 : : }
3974 : : else
3975 : 9618 : vr->operands.truncate (2);
3976 : :
3977 : : /* The looked-through reference is a simple MEM_REF. */
3978 : 9618 : memset (&op, 0, sizeof (op));
3979 : 9618 : op.type = vr->type;
3980 : 9618 : op.opcode = MEM_REF;
3981 : 9618 : op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3982 : 9618 : op.off = at - lhs_offset + rhs_offset;
3983 : 9618 : vr->operands[0] = op;
3984 : 9618 : op.type = TREE_TYPE (rhs);
3985 : 9618 : op.opcode = TREE_CODE (rhs);
3986 : 9618 : op.op0 = rhs;
3987 : 9618 : op.off = -1;
3988 : 9618 : vr->operands[1] = op;
3989 : 9618 : vr->hashcode = vn_reference_compute_hash (vr);
3990 : :
3991 : : /* Try folding the new reference to a constant. */
3992 : 9618 : tree val = fully_constant_vn_reference_p (vr);
3993 : 9618 : if (val)
3994 : 1916 : return data->finish (0, 0, val);
3995 : :
3996 : : /* Adjust *ref from the new operands. */
3997 : 7702 : if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
3998 : : return (void *)-1;
3999 : : /* This can happen with bitfields. */
4000 : 7702 : if (maybe_ne (ref->size, r.size))
4001 : : return (void *)-1;
4002 : 7702 : *ref = r;
4003 : 7702 : vr->offset = r.offset;
4004 : 7702 : vr->max_size = r.max_size;
4005 : :
4006 : : /* Do not update last seen VUSE after translating. */
4007 : 7702 : data->last_vuse_ptr = NULL;
4008 : : /* Invalidate the original access path since it now contains
4009 : : the wrong base. */
4010 : 7702 : data->orig_ref.ref = NULL_TREE;
4011 : : /* Use the alias-set of this stmt for recording an eventual result. */
4012 : 7702 : if (data->first_set == -2)
4013 : : {
4014 : 7395 : data->first_set = 0;
4015 : 7395 : data->first_base_set = 0;
4016 : : }
4017 : :
4018 : : /* Keep looking for the adjusted *REF / VR pair. */
4019 : 7702 : return NULL;
4020 : : }
4021 : :
4022 : : /* Bail out and stop walking. */
4023 : : return (void *)-1;
4024 : : }
4025 : :
4026 : : /* Return a reference op vector from OP that can be used for
4027 : : vn_reference_lookup_pieces. The caller is responsible for releasing
4028 : : the vector. */
4029 : :
4030 : : vec<vn_reference_op_s>
4031 : 5289333 : vn_reference_operands_for_lookup (tree op)
4032 : : {
4033 : 5289333 : bool valueized;
4034 : 5289333 : return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
4035 : : }
4036 : :
4037 : : /* Lookup a reference operation by it's parts, in the current hash table.
4038 : : Returns the resulting value number if it exists in the hash table,
4039 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
4040 : : vn_reference_t stored in the hashtable if something is found. */
4041 : :
4042 : : tree
4043 : 8352054 : vn_reference_lookup_pieces (tree vuse, alias_set_type set,
4044 : : alias_set_type base_set, tree type,
4045 : : vec<vn_reference_op_s> operands,
4046 : : vn_reference_t *vnresult, vn_lookup_kind kind)
4047 : : {
4048 : 8352054 : struct vn_reference_s vr1;
4049 : 8352054 : vn_reference_t tmp;
4050 : 8352054 : tree cst;
4051 : :
4052 : 8352054 : if (!vnresult)
4053 : 0 : vnresult = &tmp;
4054 : 8352054 : *vnresult = NULL;
4055 : :
4056 : 8352054 : vr1.vuse = vuse_ssa_val (vuse);
4057 : 8352054 : shared_lookup_references.truncate (0);
4058 : 16704108 : shared_lookup_references.safe_grow (operands.length (), true);
4059 : 8352054 : memcpy (shared_lookup_references.address (),
4060 : 8352054 : operands.address (),
4061 : : sizeof (vn_reference_op_s)
4062 : 8352054 : * operands.length ());
4063 : 8352054 : bool valueized_p;
4064 : 8352054 : valueize_refs_1 (&shared_lookup_references, &valueized_p);
4065 : 8352054 : vr1.operands = shared_lookup_references;
4066 : 8352054 : vr1.type = type;
4067 : 8352054 : vr1.set = set;
4068 : 8352054 : vr1.base_set = base_set;
4069 : : /* We can pretend there's no extra info fed in since the ao_refs offset
4070 : : and max_size are computed only from the VN reference ops. */
4071 : 8352054 : vr1.offset = 0;
4072 : 8352054 : vr1.max_size = -1;
4073 : 8352054 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4074 : 8352054 : if ((cst = fully_constant_vn_reference_p (&vr1)))
4075 : : return cst;
4076 : :
4077 : 8333151 : vn_reference_lookup_1 (&vr1, vnresult);
4078 : 8333151 : if (!*vnresult
4079 : 3225806 : && kind != VN_NOWALK
4080 : 3225806 : && vr1.vuse)
4081 : : {
4082 : 3199977 : ao_ref r;
4083 : 3199977 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4084 : 3199977 : vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
4085 : 3199977 : false);
4086 : 3199977 : vec<vn_reference_op_s> ops_for_ref;
4087 : 3199977 : if (!valueized_p)
4088 : 3108176 : ops_for_ref = vr1.operands;
4089 : : else
4090 : : {
4091 : : /* For ao_ref_from_mem we have to ensure only available SSA names
4092 : : end up in base and the only convenient way to make this work
4093 : : for PRE is to re-valueize with that in mind. */
4094 : 183602 : ops_for_ref.create (operands.length ());
4095 : 183602 : ops_for_ref.quick_grow (operands.length ());
4096 : 91801 : memcpy (ops_for_ref.address (),
4097 : 91801 : operands.address (),
4098 : : sizeof (vn_reference_op_s)
4099 : 91801 : * operands.length ());
4100 : 91801 : valueize_refs_1 (&ops_for_ref, &valueized_p, true);
4101 : : }
4102 : 3199977 : if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
4103 : : ops_for_ref))
4104 : 3129731 : *vnresult
4105 : 3129731 : = ((vn_reference_t)
4106 : 3129731 : walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
4107 : : vn_reference_lookup_3, vuse_valueize,
4108 : : limit, &data));
4109 : 6399954 : if (ops_for_ref != shared_lookup_references)
4110 : 91801 : ops_for_ref.release ();
4111 : 6399954 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4112 : 3199977 : if (*vnresult
4113 : 464788 : && data.same_val
4114 : 3199977 : && (!(*vnresult)->result
4115 : 0 : || !operand_equal_p ((*vnresult)->result, data.same_val)))
4116 : : {
4117 : 0 : *vnresult = NULL;
4118 : 0 : return NULL_TREE;
4119 : : }
4120 : 3199977 : }
4121 : :
4122 : 8333151 : if (*vnresult)
4123 : 5572133 : return (*vnresult)->result;
4124 : :
4125 : : return NULL_TREE;
4126 : : }
4127 : :
4128 : : /* When OPERANDS is an ADDR_EXPR that can be possibly expressed as a
4129 : : POINTER_PLUS_EXPR return true and fill in its operands in OPS. */
4130 : :
4131 : : bool
4132 : 2213896 : vn_pp_nary_for_addr (const vec<vn_reference_op_s>& operands, tree ops[2])
4133 : : {
4134 : 4427792 : gcc_assert (operands[0].opcode == ADDR_EXPR
4135 : : && operands.last ().opcode == SSA_NAME);
4136 : : poly_int64 off = 0;
4137 : : vn_reference_op_t vro;
4138 : : unsigned i;
4139 : 7158529 : for (i = 1; operands.iterate (i, &vro); ++i)
4140 : : {
4141 : 7158529 : if (vro->opcode == SSA_NAME)
4142 : : break;
4143 : 4991436 : else if (known_eq (vro->off, -1))
4144 : : break;
4145 : 4944633 : off += vro->off;
4146 : : }
4147 : 2213896 : if (i == operands.length () - 1
4148 : 2167093 : && maybe_ne (off, 0)
4149 : : /* Make sure we the offset we accumulated in a 64bit int
4150 : : fits the address computation carried out in target
4151 : : offset precision. */
4152 : 3640424 : && (off.coeffs[0]
4153 : 1426528 : == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4154 : : {
4155 : 1425804 : gcc_assert (operands[i-1].opcode == MEM_REF);
4156 : 1425804 : ops[0] = operands[i].op0;
4157 : 1425804 : ops[1] = wide_int_to_tree (sizetype, off);
4158 : 1425804 : return true;
4159 : : }
4160 : : return false;
4161 : : }
4162 : :
4163 : : /* Lookup OP in the current hash table, and return the resulting value
4164 : : number if it exists in the hash table. Return NULL_TREE if it does
4165 : : not exist in the hash table or if the result field of the structure
4166 : : was NULL.. VNRESULT will be filled in with the vn_reference_t
4167 : : stored in the hashtable if one exists. When TBAA_P is false assume
4168 : : we are looking up a store and treat it as having alias-set zero.
4169 : : *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
4170 : : MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
4171 : : load is bitwise anded with MASK and so we are only interested in a subset
4172 : : of the bits and can ignore if the other bits are uninitialized or
4173 : : not initialized with constants. When doing redundant store removal
4174 : : the caller has to set REDUNDANT_STORE_REMOVAL_P. */
4175 : :
4176 : : tree
4177 : 101478591 : vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
4178 : : vn_reference_t *vnresult, bool tbaa_p,
4179 : : tree *last_vuse_ptr, tree mask,
4180 : : bool redundant_store_removal_p)
4181 : : {
4182 : 101478591 : vec<vn_reference_op_s> operands;
4183 : 101478591 : struct vn_reference_s vr1;
4184 : 101478591 : bool valueized_anything;
4185 : :
4186 : 101478591 : if (vnresult)
4187 : 101032043 : *vnresult = NULL;
4188 : :
4189 : 101478591 : vr1.vuse = vuse_ssa_val (vuse);
4190 : 202957182 : vr1.operands = operands
4191 : 101478591 : = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
4192 : :
4193 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
4194 : : this before the pass folding __builtin_object_size had a chance to run. */
4195 : 101478591 : if ((cfun->curr_properties & PROP_objsz)
4196 : 73653657 : && operands[0].opcode == ADDR_EXPR
4197 : 102583877 : && operands.last ().opcode == SSA_NAME)
4198 : : {
4199 : 1071588 : tree ops[2];
4200 : 1071588 : if (vn_pp_nary_for_addr (operands, ops))
4201 : : {
4202 : 691040 : tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4203 : 691040 : TREE_TYPE (op), ops, NULL);
4204 : 691040 : if (res)
4205 : 691040 : return res;
4206 : 691040 : return NULL_TREE;
4207 : : }
4208 : : }
4209 : :
4210 : 100787551 : vr1.type = TREE_TYPE (op);
4211 : 100787551 : ao_ref op_ref;
4212 : 100787551 : ao_ref_init (&op_ref, op);
4213 : 100787551 : vr1.set = ao_ref_alias_set (&op_ref);
4214 : 100787551 : vr1.base_set = ao_ref_base_alias_set (&op_ref);
4215 : 100787551 : vr1.offset = 0;
4216 : 100787551 : vr1.max_size = -1;
4217 : 100787551 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4218 : 100787551 : if (mask == NULL_TREE)
4219 : 100418309 : if (tree cst = fully_constant_vn_reference_p (&vr1))
4220 : : return cst;
4221 : :
4222 : 100774409 : if (kind != VN_NOWALK && vr1.vuse)
4223 : : {
4224 : 58689307 : vn_reference_t wvnresult;
4225 : 58689307 : ao_ref r;
4226 : 58689307 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4227 : 58689307 : auto_vec<vn_reference_op_s> ops_for_ref;
4228 : 58689307 : if (valueized_anything)
4229 : : {
4230 : 4542511 : copy_reference_ops_from_ref (op, &ops_for_ref);
4231 : 4542511 : bool tem;
4232 : 4542511 : valueize_refs_1 (&ops_for_ref, &tem, true);
4233 : : }
4234 : : /* Make sure to use a valueized reference if we valueized anything.
4235 : : Otherwise preserve the full reference for advanced TBAA. */
4236 : 58689307 : if (!valueized_anything
4237 : 58689307 : || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4238 : : vr1.type, ops_for_ref))
4239 : : {
4240 : 54146796 : ao_ref_init (&r, op);
4241 : : /* Record the extra info we're getting from the full ref. */
4242 : 54146796 : ao_ref_base (&r);
4243 : 54146796 : vr1.offset = r.offset;
4244 : 54146796 : vr1.max_size = r.max_size;
4245 : : }
4246 : 58689307 : vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4247 : : last_vuse_ptr, kind, tbaa_p, mask,
4248 : 112836103 : redundant_store_removal_p);
4249 : :
4250 : 58689307 : wvnresult
4251 : : = ((vn_reference_t)
4252 : 58689307 : walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4253 : : vn_reference_lookup_3, vuse_valueize, limit,
4254 : : &data));
4255 : 117378614 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4256 : 58689307 : if (wvnresult)
4257 : : {
4258 : 8675126 : gcc_assert (mask == NULL_TREE);
4259 : 8675126 : if (data.same_val
4260 : 8675126 : && (!wvnresult->result
4261 : 69222 : || !operand_equal_p (wvnresult->result, data.same_val)))
4262 : 48373 : return NULL_TREE;
4263 : 8626753 : if (vnresult)
4264 : 8625637 : *vnresult = wvnresult;
4265 : 8626753 : return wvnresult->result;
4266 : : }
4267 : 50014181 : else if (mask)
4268 : 369242 : return data.masked_result;
4269 : :
4270 : : return NULL_TREE;
4271 : 58689307 : }
4272 : :
4273 : 42085102 : if (last_vuse_ptr)
4274 : 1442485 : *last_vuse_ptr = vr1.vuse;
4275 : 42085102 : if (mask)
4276 : : return NULL_TREE;
4277 : 42085102 : return vn_reference_lookup_1 (&vr1, vnresult);
4278 : : }
4279 : :
4280 : : /* Lookup CALL in the current hash table and return the entry in
4281 : : *VNRESULT if found. Populates *VR for the hashtable lookup. */
4282 : :
4283 : : void
4284 : 9246063 : vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4285 : : vn_reference_t vr)
4286 : : {
4287 : 9246063 : if (vnresult)
4288 : 9246063 : *vnresult = NULL;
4289 : :
4290 : 9246063 : tree vuse = gimple_vuse (call);
4291 : :
4292 : 9246063 : vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4293 : 9246063 : vr->operands = valueize_shared_reference_ops_from_call (call);
4294 : 9246063 : tree lhs = gimple_call_lhs (call);
4295 : : /* For non-SSA return values the referece ops contain the LHS. */
4296 : 5064324 : vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4297 : 13837765 : ? TREE_TYPE (lhs) : NULL_TREE);
4298 : 9246063 : vr->punned = false;
4299 : 9246063 : vr->set = 0;
4300 : 9246063 : vr->base_set = 0;
4301 : 9246063 : vr->offset = 0;
4302 : 9246063 : vr->max_size = -1;
4303 : 9246063 : vr->hashcode = vn_reference_compute_hash (vr);
4304 : 9246063 : vn_reference_lookup_1 (vr, vnresult);
4305 : 9246063 : }
4306 : :
4307 : : /* Insert OP into the current hash table with a value number of RESULT. */
4308 : :
4309 : : static void
4310 : 75330995 : vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4311 : : {
4312 : 75330995 : vn_reference_s **slot;
4313 : 75330995 : vn_reference_t vr1;
4314 : 75330995 : bool tem;
4315 : :
4316 : 75330995 : vec<vn_reference_op_s> operands
4317 : 75330995 : = valueize_shared_reference_ops_from_ref (op, &tem);
4318 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4319 : : before the pass folding __builtin_object_size had a chance to run. */
4320 : 75330995 : if ((cfun->curr_properties & PROP_objsz)
4321 : 56562081 : && operands[0].opcode == ADDR_EXPR
4322 : 76238202 : && operands.last ().opcode == SSA_NAME)
4323 : : {
4324 : 876195 : tree ops[2];
4325 : 876195 : if (vn_pp_nary_for_addr (operands, ops))
4326 : : {
4327 : 556292 : vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4328 : 556292 : TREE_TYPE (op), ops, result,
4329 : 556292 : VN_INFO (result)->value_id);
4330 : 556292 : return;
4331 : : }
4332 : : }
4333 : :
4334 : 74774703 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4335 : 74774703 : if (TREE_CODE (result) == SSA_NAME)
4336 : 51766666 : vr1->value_id = VN_INFO (result)->value_id;
4337 : : else
4338 : 23008037 : vr1->value_id = get_or_alloc_constant_value_id (result);
4339 : 74774703 : vr1->vuse = vuse_ssa_val (vuse);
4340 : 74774703 : vr1->operands = operands.copy ();
4341 : 74774703 : vr1->type = TREE_TYPE (op);
4342 : 74774703 : vr1->punned = false;
4343 : 74774703 : ao_ref op_ref;
4344 : 74774703 : ao_ref_init (&op_ref, op);
4345 : 74774703 : vr1->set = ao_ref_alias_set (&op_ref);
4346 : 74774703 : vr1->base_set = ao_ref_base_alias_set (&op_ref);
4347 : : /* Specifically use an unknown extent here, we're not doing any lookup
4348 : : and assume the caller didn't either (or it went VARYING). */
4349 : 74774703 : vr1->offset = 0;
4350 : 74774703 : vr1->max_size = -1;
4351 : 74774703 : vr1->hashcode = vn_reference_compute_hash (vr1);
4352 : 74774703 : vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4353 : 74774703 : vr1->result_vdef = vdef;
4354 : :
4355 : 74774703 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4356 : : INSERT);
4357 : :
4358 : : /* Because IL walking on reference lookup can end up visiting
4359 : : a def that is only to be visited later in iteration order
4360 : : when we are about to make an irreducible region reducible
4361 : : the def can be effectively processed and its ref being inserted
4362 : : by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4363 : : but save a lookup if we deal with already inserted refs here. */
4364 : 74774703 : if (*slot)
4365 : : {
4366 : : /* We cannot assert that we have the same value either because
4367 : : when disentangling an irreducible region we may end up visiting
4368 : : a use before the corresponding def. That's a missed optimization
4369 : : only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4370 : 0 : if (dump_file && (dump_flags & TDF_DETAILS)
4371 : 0 : && !operand_equal_p ((*slot)->result, vr1->result, 0))
4372 : : {
4373 : 0 : fprintf (dump_file, "Keeping old value ");
4374 : 0 : print_generic_expr (dump_file, (*slot)->result);
4375 : 0 : fprintf (dump_file, " because of collision\n");
4376 : : }
4377 : 0 : free_reference (vr1);
4378 : 0 : obstack_free (&vn_tables_obstack, vr1);
4379 : 0 : return;
4380 : : }
4381 : :
4382 : 74774703 : *slot = vr1;
4383 : 74774703 : vr1->next = last_inserted_ref;
4384 : 74774703 : last_inserted_ref = vr1;
4385 : : }
4386 : :
4387 : : /* Insert a reference by it's pieces into the current hash table with
4388 : : a value number of RESULT. Return the resulting reference
4389 : : structure we created. */
4390 : :
4391 : : vn_reference_t
4392 : 3851108 : vn_reference_insert_pieces (tree vuse, alias_set_type set,
4393 : : alias_set_type base_set,
4394 : : poly_int64 offset, poly_int64 max_size, tree type,
4395 : : vec<vn_reference_op_s> operands,
4396 : : tree result, unsigned int value_id)
4397 : :
4398 : : {
4399 : 3851108 : vn_reference_s **slot;
4400 : 3851108 : vn_reference_t vr1;
4401 : :
4402 : 3851108 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4403 : 3851108 : vr1->value_id = value_id;
4404 : 3851108 : vr1->vuse = vuse_ssa_val (vuse);
4405 : 3851108 : vr1->operands = operands;
4406 : 3851108 : valueize_refs (&vr1->operands);
4407 : 3851108 : vr1->type = type;
4408 : 3851108 : vr1->punned = false;
4409 : 3851108 : vr1->set = set;
4410 : 3851108 : vr1->base_set = base_set;
4411 : 3851108 : vr1->offset = offset;
4412 : 3851108 : vr1->max_size = max_size;
4413 : 3851108 : vr1->hashcode = vn_reference_compute_hash (vr1);
4414 : 3851108 : if (result && TREE_CODE (result) == SSA_NAME)
4415 : 285684 : result = SSA_VAL (result);
4416 : 3851108 : vr1->result = result;
4417 : 3851108 : vr1->result_vdef = NULL_TREE;
4418 : :
4419 : 3851108 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4420 : : INSERT);
4421 : :
4422 : : /* At this point we should have all the things inserted that we have
4423 : : seen before, and we should never try inserting something that
4424 : : already exists. */
4425 : 3851108 : gcc_assert (!*slot);
4426 : :
4427 : 3851108 : *slot = vr1;
4428 : 3851108 : vr1->next = last_inserted_ref;
4429 : 3851108 : last_inserted_ref = vr1;
4430 : 3851108 : return vr1;
4431 : : }
4432 : :
4433 : : /* Compute and return the hash value for nary operation VBO1. */
4434 : :
4435 : : hashval_t
4436 : 305252522 : vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4437 : : {
4438 : 305252522 : inchash::hash hstate;
4439 : 305252522 : unsigned i;
4440 : :
4441 : 305252522 : if (((vno1->length == 2
4442 : 257394418 : && commutative_tree_code (vno1->opcode))
4443 : 139754908 : || (vno1->length == 3
4444 : 1398331 : && commutative_ternary_tree_code (vno1->opcode)))
4445 : 470752121 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4446 : 2398243 : std::swap (vno1->op[0], vno1->op[1]);
4447 : 302854279 : else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4448 : 302854279 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4449 : : {
4450 : 478284 : std::swap (vno1->op[0], vno1->op[1]);
4451 : 478284 : vno1->opcode = swap_tree_comparison (vno1->opcode);
4452 : : }
4453 : :
4454 : 305252522 : hstate.add_int (vno1->opcode);
4455 : 871385757 : for (i = 0; i < vno1->length; ++i)
4456 : 566133235 : inchash::add_expr (vno1->op[i], hstate);
4457 : :
4458 : 305252522 : return hstate.end ();
4459 : : }
4460 : :
4461 : : /* Compare nary operations VNO1 and VNO2 and return true if they are
4462 : : equivalent. */
4463 : :
4464 : : bool
4465 : 972948161 : vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4466 : : {
4467 : 972948161 : unsigned i;
4468 : :
4469 : 972948161 : if (vno1->hashcode != vno2->hashcode)
4470 : : return false;
4471 : :
4472 : 50262693 : if (vno1->length != vno2->length)
4473 : : return false;
4474 : :
4475 : 50262693 : if (vno1->opcode != vno2->opcode
4476 : 50262693 : || !types_compatible_p (vno1->type, vno2->type))
4477 : 1205503 : return false;
4478 : :
4479 : 141722531 : for (i = 0; i < vno1->length; ++i)
4480 : 92760165 : if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4481 : : return false;
4482 : :
4483 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
4484 : : of op1. Need to check to make sure they are the same. */
4485 : 48962366 : if (vno1->opcode == BIT_INSERT_EXPR
4486 : 528 : && TREE_CODE (vno1->op[1]) == INTEGER_CST
4487 : 48962473 : && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4488 : 107 : != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4489 : : return false;
4490 : :
4491 : : return true;
4492 : : }
4493 : :
4494 : : /* Initialize VNO from the pieces provided. */
4495 : :
4496 : : static void
4497 : 189491425 : init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4498 : : enum tree_code code, tree type, tree *ops)
4499 : : {
4500 : 189491425 : vno->opcode = code;
4501 : 189491425 : vno->length = length;
4502 : 189491425 : vno->type = type;
4503 : 4851360 : memcpy (&vno->op[0], ops, sizeof (tree) * length);
4504 : 0 : }
4505 : :
4506 : : /* Return the number of operands for a vn_nary ops structure from STMT. */
4507 : :
4508 : : unsigned int
4509 : 109702212 : vn_nary_length_from_stmt (gimple *stmt)
4510 : : {
4511 : 109702212 : switch (gimple_assign_rhs_code (stmt))
4512 : : {
4513 : : case REALPART_EXPR:
4514 : : case IMAGPART_EXPR:
4515 : : case VIEW_CONVERT_EXPR:
4516 : : return 1;
4517 : :
4518 : 528338 : case BIT_FIELD_REF:
4519 : 528338 : return 3;
4520 : :
4521 : 501514 : case CONSTRUCTOR:
4522 : 501514 : return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4523 : :
4524 : 105346206 : default:
4525 : 105346206 : return gimple_num_ops (stmt) - 1;
4526 : : }
4527 : : }
4528 : :
4529 : : /* Initialize VNO from STMT. */
4530 : :
4531 : : void
4532 : 109702212 : init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4533 : : {
4534 : 109702212 : unsigned i;
4535 : :
4536 : 109702212 : vno->opcode = gimple_assign_rhs_code (stmt);
4537 : 109702212 : vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4538 : 109702212 : switch (vno->opcode)
4539 : : {
4540 : 3326154 : case REALPART_EXPR:
4541 : 3326154 : case IMAGPART_EXPR:
4542 : 3326154 : case VIEW_CONVERT_EXPR:
4543 : 3326154 : vno->length = 1;
4544 : 3326154 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4545 : 3326154 : break;
4546 : :
4547 : 528338 : case BIT_FIELD_REF:
4548 : 528338 : vno->length = 3;
4549 : 528338 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4550 : 528338 : vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4551 : 528338 : vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4552 : 528338 : break;
4553 : :
4554 : 501514 : case CONSTRUCTOR:
4555 : 501514 : vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4556 : 1960281 : for (i = 0; i < vno->length; ++i)
4557 : 1458767 : vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4558 : : break;
4559 : :
4560 : 105346206 : default:
4561 : 105346206 : gcc_checking_assert (!gimple_assign_single_p (stmt));
4562 : 105346206 : vno->length = gimple_num_ops (stmt) - 1;
4563 : 288408732 : for (i = 0; i < vno->length; ++i)
4564 : 183062526 : vno->op[i] = gimple_op (stmt, i + 1);
4565 : : }
4566 : 109702212 : }
4567 : :
4568 : : /* Compute the hashcode for VNO and look for it in the hash table;
4569 : : return the resulting value number if it exists in the hash table.
4570 : : Return NULL_TREE if it does not exist in the hash table or if the
4571 : : result field of the operation is NULL. VNRESULT will contain the
4572 : : vn_nary_op_t from the hashtable if it exists. */
4573 : :
4574 : : static tree
4575 : 131899460 : vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4576 : : {
4577 : 131899460 : vn_nary_op_s **slot;
4578 : :
4579 : 131899460 : if (vnresult)
4580 : 124748186 : *vnresult = NULL;
4581 : :
4582 : 366721583 : for (unsigned i = 0; i < vno->length; ++i)
4583 : 234822123 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4584 : 166237150 : vno->op[i] = SSA_VAL (vno->op[i]);
4585 : :
4586 : 131899460 : vno->hashcode = vn_nary_op_compute_hash (vno);
4587 : 131899460 : slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4588 : 131899460 : if (!slot)
4589 : : return NULL_TREE;
4590 : 17580456 : if (vnresult)
4591 : 17139975 : *vnresult = *slot;
4592 : 17580456 : return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4593 : : }
4594 : :
4595 : : /* Lookup a n-ary operation by its pieces and return the resulting value
4596 : : number if it exists in the hash table. Return NULL_TREE if it does
4597 : : not exist in the hash table or if the result field of the operation
4598 : : is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4599 : : if it exists. */
4600 : :
4601 : : tree
4602 : 75148287 : vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4603 : : tree type, tree *ops, vn_nary_op_t *vnresult)
4604 : : {
4605 : 75148287 : vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4606 : : sizeof_vn_nary_op (length));
4607 : 75148287 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4608 : 75148287 : return vn_nary_op_lookup_1 (vno1, vnresult);
4609 : : }
4610 : :
4611 : : /* Lookup the rhs of STMT in the current hash table, and return the resulting
4612 : : value number if it exists in the hash table. Return NULL_TREE if
4613 : : it does not exist in the hash table. VNRESULT will contain the
4614 : : vn_nary_op_t from the hashtable if it exists. */
4615 : :
4616 : : tree
4617 : 56751173 : vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4618 : : {
4619 : 56751173 : vn_nary_op_t vno1
4620 : 56751173 : = XALLOCAVAR (struct vn_nary_op_s,
4621 : : sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4622 : 56751173 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4623 : 56751173 : return vn_nary_op_lookup_1 (vno1, vnresult);
4624 : : }
4625 : :
4626 : : /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4627 : :
4628 : : vn_nary_op_t
4629 : 172361151 : alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4630 : : {
4631 : 172361151 : return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4632 : : }
4633 : :
4634 : : /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4635 : : obstack. */
4636 : :
4637 : : static vn_nary_op_t
4638 : 154764312 : alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4639 : : {
4640 : 0 : vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4641 : :
4642 : 154764312 : vno1->value_id = value_id;
4643 : 154764312 : vno1->length = length;
4644 : 154764312 : vno1->predicated_values = 0;
4645 : 154764312 : vno1->u.result = result;
4646 : :
4647 : 154764312 : return vno1;
4648 : : }
4649 : :
4650 : : /* Insert VNO into TABLE. */
4651 : :
4652 : : static vn_nary_op_t
4653 : 159755935 : vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4654 : : {
4655 : 159755935 : vn_nary_op_s **slot;
4656 : :
4657 : 159755935 : gcc_assert (! vno->predicated_values
4658 : : || (! vno->u.values->next
4659 : : && vno->u.values->n == 1));
4660 : :
4661 : 467429840 : for (unsigned i = 0; i < vno->length; ++i)
4662 : 307673905 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4663 : 200296697 : vno->op[i] = SSA_VAL (vno->op[i]);
4664 : :
4665 : 159755935 : vno->hashcode = vn_nary_op_compute_hash (vno);
4666 : 159755935 : slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4667 : 159755935 : vno->unwind_to = *slot;
4668 : 159755935 : if (*slot)
4669 : : {
4670 : : /* Prefer non-predicated values.
4671 : : ??? Only if those are constant, otherwise, with constant predicated
4672 : : value, turn them into predicated values with entry-block validity
4673 : : (??? but we always find the first valid result currently). */
4674 : 30389999 : if ((*slot)->predicated_values
4675 : 29635698 : && ! vno->predicated_values)
4676 : : {
4677 : : /* ??? We cannot remove *slot from the unwind stack list.
4678 : : For the moment we deal with this by skipping not found
4679 : : entries but this isn't ideal ... */
4680 : 83193 : *slot = vno;
4681 : : /* ??? Maintain a stack of states we can unwind in
4682 : : vn_nary_op_s? But how far do we unwind? In reality
4683 : : we need to push change records somewhere... Or not
4684 : : unwind vn_nary_op_s and linking them but instead
4685 : : unwind the results "list", linking that, which also
4686 : : doesn't move on hashtable resize. */
4687 : : /* We can also have a ->unwind_to recording *slot there.
4688 : : That way we can make u.values a fixed size array with
4689 : : recording the number of entries but of course we then
4690 : : have always N copies for each unwind_to-state. Or we
4691 : : make sure to only ever append and each unwinding will
4692 : : pop off one entry (but how to deal with predicated
4693 : : replaced with non-predicated here?) */
4694 : 83193 : vno->next = last_inserted_nary;
4695 : 83193 : last_inserted_nary = vno;
4696 : 83193 : return vno;
4697 : : }
4698 : 30306806 : else if (vno->predicated_values
4699 : 30306450 : && ! (*slot)->predicated_values)
4700 : : return *slot;
4701 : 29552861 : else if (vno->predicated_values
4702 : 29552505 : && (*slot)->predicated_values)
4703 : : {
4704 : : /* ??? Factor this all into a insert_single_predicated_value
4705 : : routine. */
4706 : 29552505 : gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4707 : 29552505 : basic_block vno_bb
4708 : 29552505 : = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4709 : 29552505 : vn_pval *nval = vno->u.values;
4710 : 29552505 : vn_pval **next = &vno->u.values;
4711 : 29552505 : vn_pval *ins = NULL;
4712 : 29552505 : vn_pval *ins_at = NULL;
4713 : : /* Find an existing value to append to. */
4714 : 55786569 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4715 : : {
4716 : 30492289 : if (expressions_equal_p (val->result, nval->result))
4717 : : {
4718 : : /* Limit the number of places we register a predicate
4719 : : as valid. */
4720 : 4258225 : if (val->n > 8)
4721 : 119730 : return *slot;
4722 : 10454487 : for (unsigned i = 0; i < val->n; ++i)
4723 : : {
4724 : 6550596 : basic_block val_bb
4725 : 6550596 : = BASIC_BLOCK_FOR_FN (cfun,
4726 : : val->valid_dominated_by_p[i]);
4727 : 6550596 : if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4728 : : /* Value registered with more generic predicate. */
4729 : 234604 : return *slot;
4730 : 6315992 : else if (flag_checking)
4731 : : /* Shouldn't happen, we insert in RPO order. */
4732 : 6315992 : gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4733 : : val_bb, vno_bb));
4734 : : }
4735 : : /* Append the location. */
4736 : 3903891 : ins_at = val;
4737 : 3903891 : ins = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4738 : : sizeof (vn_pval)
4739 : : + val->n * sizeof (int));
4740 : 3903891 : ins->next = NULL;
4741 : 3903891 : ins->result = val->result;
4742 : 3903891 : ins->n = val->n + 1;
4743 : 3903891 : memcpy (ins->valid_dominated_by_p,
4744 : 3903891 : val->valid_dominated_by_p,
4745 : 3903891 : val->n * sizeof (int));
4746 : 3903891 : ins->valid_dominated_by_p[val->n] = vno_bb->index;
4747 : 3903891 : if (dump_file && (dump_flags & TDF_DETAILS))
4748 : 4 : fprintf (dump_file, "Appending predicate to value.\n");
4749 : : break;
4750 : : }
4751 : : }
4752 : : /* Copy the rest of the value chain. */
4753 : 60162722 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4754 : : {
4755 : 30964551 : if (val == ins_at)
4756 : : /* Replace the node we appended to. */
4757 : 3903891 : *next = ins;
4758 : : else
4759 : : {
4760 : : /* Copy other predicated values. */
4761 : 27060660 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4762 : : sizeof (vn_pval)
4763 : : + ((val->n-1)
4764 : : * sizeof (int)));
4765 : 27060660 : memcpy (*next, val,
4766 : 27060660 : sizeof (vn_pval) + (val->n-1) * sizeof (int));
4767 : 27060660 : (*next)->next = NULL;
4768 : : }
4769 : 30964551 : next = &(*next)->next;
4770 : : }
4771 : : /* Append the value if we didn't find it. */
4772 : 29198171 : if (!ins_at)
4773 : 25294280 : *next = nval;
4774 : 29198171 : *slot = vno;
4775 : 29198171 : vno->next = last_inserted_nary;
4776 : 29198171 : last_inserted_nary = vno;
4777 : 29198171 : return vno;
4778 : : }
4779 : :
4780 : : /* While we do not want to insert things twice it's awkward to
4781 : : avoid it in the case where visit_nary_op pattern-matches stuff
4782 : : and ends up simplifying the replacement to itself. We then
4783 : : get two inserts, one from visit_nary_op and one from
4784 : : vn_nary_build_or_lookup.
4785 : : So allow inserts with the same value number. */
4786 : 356 : if ((*slot)->u.result == vno->u.result)
4787 : : return *slot;
4788 : : }
4789 : :
4790 : : /* ??? There's also optimistic vs. previous commited state merging
4791 : : that is problematic for the case of unwinding. */
4792 : :
4793 : : /* ??? We should return NULL if we do not use 'vno' and have the
4794 : : caller release it. */
4795 : 129365936 : gcc_assert (!*slot);
4796 : :
4797 : 129365936 : *slot = vno;
4798 : 129365936 : vno->next = last_inserted_nary;
4799 : 129365936 : last_inserted_nary = vno;
4800 : 129365936 : return vno;
4801 : : }
4802 : :
4803 : : /* Insert a n-ary operation into the current hash table using it's
4804 : : pieces. Return the vn_nary_op_t structure we created and put in
4805 : : the hashtable. */
4806 : :
4807 : : vn_nary_op_t
4808 : 556292 : vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4809 : : tree type, tree *ops,
4810 : : tree result, unsigned int value_id)
4811 : : {
4812 : 556292 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4813 : 556292 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4814 : 556292 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4815 : : }
4816 : :
4817 : : /* Return whether we can track a predicate valid when PRED_E is executed. */
4818 : :
4819 : : static bool
4820 : 153669078 : can_track_predicate_on_edge (edge pred_e)
4821 : : {
4822 : : /* ??? As we are currently recording the destination basic-block index in
4823 : : vn_pval.valid_dominated_by_p and using dominance for the
4824 : : validity check we cannot track predicates on all edges. */
4825 : 153669078 : if (single_pred_p (pred_e->dest))
4826 : : return true;
4827 : : /* Never record for backedges. */
4828 : 12112138 : if (pred_e->flags & EDGE_DFS_BACK)
4829 : : return false;
4830 : : /* When there's more than one predecessor we cannot track
4831 : : predicate validity based on the destination block. The
4832 : : exception is when all other incoming edges sources are
4833 : : dominated by the destination block. */
4834 : 11420200 : edge_iterator ei;
4835 : 11420200 : edge e;
4836 : 19596052 : FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4837 : 17739063 : if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4838 : : return false;
4839 : : return true;
4840 : : }
4841 : :
4842 : : static vn_nary_op_t
4843 : 108935486 : vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4844 : : tree type, tree *ops,
4845 : : tree result, unsigned int value_id,
4846 : : edge pred_e)
4847 : : {
4848 : 108935486 : if (flag_checking)
4849 : 108934650 : gcc_assert (can_track_predicate_on_edge (pred_e));
4850 : :
4851 : 71745 : if (dump_file && (dump_flags & TDF_DETAILS)
4852 : : /* ??? Fix dumping, but currently we only get comparisons. */
4853 : 109003201 : && TREE_CODE_CLASS (code) == tcc_comparison)
4854 : : {
4855 : 67715 : fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4856 : 67715 : pred_e->dest->index);
4857 : 67715 : print_generic_expr (dump_file, ops[0], TDF_SLIM);
4858 : 67715 : fprintf (dump_file, " %s ", get_tree_code_name (code));
4859 : 67715 : print_generic_expr (dump_file, ops[1], TDF_SLIM);
4860 : 101249 : fprintf (dump_file, " == %s\n",
4861 : 67715 : integer_zerop (result) ? "false" : "true");
4862 : : }
4863 : 108935486 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4864 : 108935486 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4865 : 108935486 : vno1->predicated_values = 1;
4866 : 108935486 : vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4867 : : sizeof (vn_pval));
4868 : 108935486 : vno1->u.values->next = NULL;
4869 : 108935486 : vno1->u.values->result = result;
4870 : 108935486 : vno1->u.values->n = 1;
4871 : 108935486 : vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4872 : 108935486 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4873 : : }
4874 : :
4875 : : static bool
4876 : : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4877 : :
4878 : : static tree
4879 : 1658687 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
4880 : : edge e = NULL)
4881 : : {
4882 : 1658687 : if (! vno->predicated_values)
4883 : 0 : return vno->u.result;
4884 : 3425595 : for (vn_pval *val = vno->u.values; val; val = val->next)
4885 : 5167982 : for (unsigned i = 0; i < val->n; ++i)
4886 : : {
4887 : 3401074 : basic_block cand
4888 : 3401074 : = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
4889 : : /* Do not handle backedge executability optimistically since
4890 : : when figuring out whether to iterate we do not consider
4891 : : changed predication.
4892 : : When asking for predicated values on an edge avoid looking
4893 : : at edge executability for edges forward in our iteration
4894 : : as well. */
4895 : 3401074 : if (e && (e->flags & EDGE_DFS_BACK))
4896 : : {
4897 : 24096 : if (dominated_by_p (CDI_DOMINATORS, bb, cand))
4898 : 8676 : return val->result;
4899 : : }
4900 : 3376978 : else if (dominated_by_p_w_unex (bb, cand, false))
4901 : 509515 : return val->result;
4902 : : }
4903 : : return NULL_TREE;
4904 : : }
4905 : :
4906 : : static tree
4907 : 209432 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
4908 : : {
4909 : 0 : return vn_nary_op_get_predicated_value (vno, e->src, e);
4910 : : }
4911 : :
4912 : : /* Insert the rhs of STMT into the current hash table with a value number of
4913 : : RESULT. */
4914 : :
4915 : : static vn_nary_op_t
4916 : 45272534 : vn_nary_op_insert_stmt (gimple *stmt, tree result)
4917 : : {
4918 : 45272534 : vn_nary_op_t vno1
4919 : 45272534 : = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
4920 : 45272534 : result, VN_INFO (result)->value_id);
4921 : 45272534 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4922 : 45272534 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4923 : : }
4924 : :
4925 : : /* Compute a hashcode for PHI operation VP1 and return it. */
4926 : :
4927 : : static inline hashval_t
4928 : 51136179 : vn_phi_compute_hash (vn_phi_t vp1)
4929 : : {
4930 : 51136179 : inchash::hash hstate;
4931 : 51136179 : tree phi1op;
4932 : 51136179 : tree type;
4933 : 51136179 : edge e;
4934 : 51136179 : edge_iterator ei;
4935 : :
4936 : 102272358 : hstate.add_int (EDGE_COUNT (vp1->block->preds));
4937 : 51136179 : switch (EDGE_COUNT (vp1->block->preds))
4938 : : {
4939 : : case 1:
4940 : : break;
4941 : 43612645 : case 2:
4942 : : /* When this is a PHI node subject to CSE for different blocks
4943 : : avoid hashing the block index. */
4944 : 43612645 : if (vp1->cclhs)
4945 : : break;
4946 : : /* Fallthru. */
4947 : 34644199 : default:
4948 : 34644199 : hstate.add_int (vp1->block->index);
4949 : : }
4950 : :
4951 : : /* If all PHI arguments are constants we need to distinguish
4952 : : the PHI node via its type. */
4953 : 51136179 : type = vp1->type;
4954 : 51136179 : hstate.merge_hash (vn_hash_type (type));
4955 : :
4956 : 178755059 : FOR_EACH_EDGE (e, ei, vp1->block->preds)
4957 : : {
4958 : : /* Don't hash backedge values they need to be handled as VN_TOP
4959 : : for optimistic value-numbering. */
4960 : 127618880 : if (e->flags & EDGE_DFS_BACK)
4961 : 28606177 : continue;
4962 : :
4963 : 99012703 : phi1op = vp1->phiargs[e->dest_idx];
4964 : 99012703 : if (phi1op == VN_TOP)
4965 : 247407 : continue;
4966 : 98765296 : inchash::add_expr (phi1op, hstate);
4967 : : }
4968 : :
4969 : 51136179 : return hstate.end ();
4970 : : }
4971 : :
4972 : :
4973 : : /* Return true if COND1 and COND2 represent the same condition, set
4974 : : *INVERTED_P if one needs to be inverted to make it the same as
4975 : : the other. */
4976 : :
4977 : : static bool
4978 : 3783776 : cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
4979 : : gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
4980 : : {
4981 : 3783776 : enum tree_code code1 = gimple_cond_code (cond1);
4982 : 3783776 : enum tree_code code2 = gimple_cond_code (cond2);
4983 : :
4984 : 3783776 : *inverted_p = false;
4985 : 3783776 : if (code1 == code2)
4986 : : ;
4987 : 301294 : else if (code1 == swap_tree_comparison (code2))
4988 : : std::swap (lhs2, rhs2);
4989 : 265346 : else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
4990 : 133809 : *inverted_p = true;
4991 : 131537 : else if (code1 == invert_tree_comparison
4992 : 131537 : (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
4993 : : {
4994 : 10392 : std::swap (lhs2, rhs2);
4995 : 10392 : *inverted_p = true;
4996 : : }
4997 : : else
4998 : : return false;
4999 : :
5000 : 3662631 : return ((expressions_equal_p (lhs1, lhs2)
5001 : 105327 : && expressions_equal_p (rhs1, rhs2))
5002 : 3687958 : || (commutative_tree_code (code1)
5003 : 1811622 : && expressions_equal_p (lhs1, rhs2)
5004 : 2469 : && expressions_equal_p (rhs1, lhs2)));
5005 : : }
5006 : :
5007 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
5008 : :
5009 : : static int
5010 : 41699845 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
5011 : : {
5012 : 41699845 : if (vp1->hashcode != vp2->hashcode)
5013 : : return false;
5014 : :
5015 : 12840131 : if (vp1->block != vp2->block)
5016 : : {
5017 : 11374731 : if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
5018 : : return false;
5019 : :
5020 : 37411099 : switch (EDGE_COUNT (vp1->block->preds))
5021 : : {
5022 : : case 1:
5023 : : /* Single-arg PHIs are just copies. */
5024 : : break;
5025 : :
5026 : 3791577 : case 2:
5027 : 3791577 : {
5028 : : /* Make sure both PHIs are classified as CSEable. */
5029 : 3791577 : if (! vp1->cclhs || ! vp2->cclhs)
5030 : : return false;
5031 : :
5032 : : /* Rule out backedges into the PHI. */
5033 : 3791577 : gcc_checking_assert
5034 : : (vp1->block->loop_father->header != vp1->block
5035 : : && vp2->block->loop_father->header != vp2->block);
5036 : :
5037 : : /* If the PHI nodes do not have compatible types
5038 : : they are not the same. */
5039 : 3791577 : if (!types_compatible_p (vp1->type, vp2->type))
5040 : : return false;
5041 : :
5042 : : /* If the immediate dominator end in switch stmts multiple
5043 : : values may end up in the same PHI arg via intermediate
5044 : : CFG merges. */
5045 : 3783776 : basic_block idom1
5046 : 3783776 : = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5047 : 3783776 : basic_block idom2
5048 : 3783776 : = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
5049 : 3783776 : gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
5050 : : && EDGE_COUNT (idom2->succs) == 2);
5051 : :
5052 : : /* Verify the controlling stmt is the same. */
5053 : 7567552 : gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
5054 : 7567552 : gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
5055 : 3783776 : bool inverted_p;
5056 : 3783776 : if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
5057 : 3783776 : last2, vp2->cclhs, vp2->ccrhs,
5058 : : &inverted_p))
5059 : : return false;
5060 : :
5061 : : /* Get at true/false controlled edges into the PHI. */
5062 : 80088 : edge te1, te2, fe1, fe2;
5063 : 80088 : if (! extract_true_false_controlled_edges (idom1, vp1->block,
5064 : : &te1, &fe1)
5065 : 80088 : || ! extract_true_false_controlled_edges (idom2, vp2->block,
5066 : : &te2, &fe2))
5067 : 34481 : return false;
5068 : :
5069 : : /* Swap edges if the second condition is the inverted of the
5070 : : first. */
5071 : 45607 : if (inverted_p)
5072 : 2076 : std::swap (te2, fe2);
5073 : :
5074 : : /* Since we do not know which edge will be executed we have
5075 : : to be careful when matching VN_TOP. Be conservative and
5076 : : only match VN_TOP == VN_TOP for now, we could allow
5077 : : VN_TOP on the not prevailing PHI though. See for example
5078 : : PR102920. */
5079 : 45607 : if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
5080 : 45607 : vp2->phiargs[te2->dest_idx], false)
5081 : 89401 : || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
5082 : 43794 : vp2->phiargs[fe2->dest_idx], false))
5083 : 1813 : return false;
5084 : :
5085 : : return true;
5086 : : }
5087 : :
5088 : : default:
5089 : : return false;
5090 : : }
5091 : : }
5092 : :
5093 : : /* If the PHI nodes do not have compatible types
5094 : : they are not the same. */
5095 : 9048554 : if (!types_compatible_p (vp1->type, vp2->type))
5096 : : return false;
5097 : :
5098 : : /* Any phi in the same block will have it's arguments in the
5099 : : same edge order, because of how we store phi nodes. */
5100 : 9047408 : unsigned nargs = EDGE_COUNT (vp1->block->preds);
5101 : 21208631 : for (unsigned i = 0; i < nargs; ++i)
5102 : : {
5103 : 16919885 : tree phi1op = vp1->phiargs[i];
5104 : 16919885 : tree phi2op = vp2->phiargs[i];
5105 : 16919885 : if (phi1op == phi2op)
5106 : 12066972 : continue;
5107 : 4852913 : if (!expressions_equal_p (phi1op, phi2op, false))
5108 : : return false;
5109 : : }
5110 : :
5111 : : return true;
5112 : : }
5113 : :
5114 : : /* Lookup PHI in the current hash table, and return the resulting
5115 : : value number if it exists in the hash table. Return NULL_TREE if
5116 : : it does not exist in the hash table. */
5117 : :
5118 : : static tree
5119 : 28059094 : vn_phi_lookup (gimple *phi, bool backedges_varying_p)
5120 : : {
5121 : 28059094 : vn_phi_s **slot;
5122 : 28059094 : struct vn_phi_s *vp1;
5123 : 28059094 : edge e;
5124 : 28059094 : edge_iterator ei;
5125 : :
5126 : 28059094 : vp1 = XALLOCAVAR (struct vn_phi_s,
5127 : : sizeof (struct vn_phi_s)
5128 : : + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
5129 : :
5130 : : /* Canonicalize the SSA_NAME's to their value number. */
5131 : 97373290 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5132 : : {
5133 : 69314196 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5134 : 69314196 : if (TREE_CODE (def) == SSA_NAME
5135 : 57760908 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5136 : : {
5137 : 55149095 : if (!virtual_operand_p (def)
5138 : 55149095 : && ssa_undefined_value_p (def, false))
5139 : 138090 : def = VN_TOP;
5140 : : else
5141 : 55011005 : def = SSA_VAL (def);
5142 : : }
5143 : 69314196 : vp1->phiargs[e->dest_idx] = def;
5144 : : }
5145 : 28059094 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5146 : 28059094 : vp1->block = gimple_bb (phi);
5147 : : /* Extract values of the controlling condition. */
5148 : 28059094 : vp1->cclhs = NULL_TREE;
5149 : 28059094 : vp1->ccrhs = NULL_TREE;
5150 : 28059094 : if (EDGE_COUNT (vp1->block->preds) == 2
5151 : 28059094 : && vp1->block->loop_father->header != vp1->block)
5152 : : {
5153 : 8730023 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5154 : 8730023 : if (EDGE_COUNT (idom1->succs) == 2)
5155 : 17339002 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5156 : : {
5157 : : /* ??? We want to use SSA_VAL here. But possibly not
5158 : : allow VN_TOP. */
5159 : 8435624 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5160 : 8435624 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5161 : : }
5162 : : }
5163 : 28059094 : vp1->hashcode = vn_phi_compute_hash (vp1);
5164 : 28059094 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
5165 : 28059094 : if (!slot)
5166 : : return NULL_TREE;
5167 : 4332540 : return (*slot)->result;
5168 : : }
5169 : :
5170 : : /* Insert PHI into the current hash table with a value number of
5171 : : RESULT. */
5172 : :
5173 : : static vn_phi_t
5174 : 23077085 : vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
5175 : : {
5176 : 23077085 : vn_phi_s **slot;
5177 : 23077085 : vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
5178 : : sizeof (vn_phi_s)
5179 : : + ((gimple_phi_num_args (phi) - 1)
5180 : : * sizeof (tree)));
5181 : 23077085 : edge e;
5182 : 23077085 : edge_iterator ei;
5183 : :
5184 : : /* Canonicalize the SSA_NAME's to their value number. */
5185 : 81381769 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5186 : : {
5187 : 58304684 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5188 : 58304684 : if (TREE_CODE (def) == SSA_NAME
5189 : 47885392 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5190 : : {
5191 : 45274015 : if (!virtual_operand_p (def)
5192 : 45274015 : && ssa_undefined_value_p (def, false))
5193 : 109561 : def = VN_TOP;
5194 : : else
5195 : 45164454 : def = SSA_VAL (def);
5196 : : }
5197 : 58304684 : vp1->phiargs[e->dest_idx] = def;
5198 : : }
5199 : 23077085 : vp1->value_id = VN_INFO (result)->value_id;
5200 : 23077085 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5201 : 23077085 : vp1->block = gimple_bb (phi);
5202 : : /* Extract values of the controlling condition. */
5203 : 23077085 : vp1->cclhs = NULL_TREE;
5204 : 23077085 : vp1->ccrhs = NULL_TREE;
5205 : 23077085 : if (EDGE_COUNT (vp1->block->preds) == 2
5206 : 23077085 : && vp1->block->loop_father->header != vp1->block)
5207 : : {
5208 : 8346771 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5209 : 8346771 : if (EDGE_COUNT (idom1->succs) == 2)
5210 : 16576204 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5211 : : {
5212 : : /* ??? We want to use SSA_VAL here. But possibly not
5213 : : allow VN_TOP. */
5214 : 8056356 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5215 : 8056356 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5216 : : }
5217 : : }
5218 : 23077085 : vp1->result = result;
5219 : 23077085 : vp1->hashcode = vn_phi_compute_hash (vp1);
5220 : :
5221 : 23077085 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5222 : 23077085 : gcc_assert (!*slot);
5223 : :
5224 : 23077085 : *slot = vp1;
5225 : 23077085 : vp1->next = last_inserted_phi;
5226 : 23077085 : last_inserted_phi = vp1;
5227 : 23077085 : return vp1;
5228 : : }
5229 : :
5230 : :
5231 : : /* Return true if BB1 is dominated by BB2 taking into account edges
5232 : : that are not executable. When ALLOW_BACK is false consider not
5233 : : executable backedges as executable. */
5234 : :
5235 : : static bool
5236 : 69837772 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
5237 : : {
5238 : 69837772 : edge_iterator ei;
5239 : 69837772 : edge e;
5240 : :
5241 : 69837772 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5242 : : return true;
5243 : :
5244 : : /* Before iterating we'd like to know if there exists a
5245 : : (executable) path from bb2 to bb1 at all, if not we can
5246 : : directly return false. For now simply iterate once. */
5247 : :
5248 : : /* Iterate to the single executable bb1 predecessor. */
5249 : 21226411 : if (EDGE_COUNT (bb1->preds) > 1)
5250 : : {
5251 : 2999312 : edge prede = NULL;
5252 : 6480583 : FOR_EACH_EDGE (e, ei, bb1->preds)
5253 : 6072926 : if ((e->flags & EDGE_EXECUTABLE)
5254 : 560785 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5255 : : {
5256 : 5590967 : if (prede)
5257 : : {
5258 : : prede = NULL;
5259 : : break;
5260 : : }
5261 : : prede = e;
5262 : : }
5263 : 2999312 : if (prede)
5264 : : {
5265 : 407657 : bb1 = prede->src;
5266 : :
5267 : : /* Re-do the dominance check with changed bb1. */
5268 : 407657 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5269 : : return true;
5270 : : }
5271 : : }
5272 : :
5273 : : /* Iterate to the single executable bb2 successor. */
5274 : 20989222 : if (EDGE_COUNT (bb2->succs) > 1)
5275 : : {
5276 : 6115052 : edge succe = NULL;
5277 : 12377533 : FOR_EACH_EDGE (e, ei, bb2->succs)
5278 : 12230368 : if ((e->flags & EDGE_EXECUTABLE)
5279 : 181463 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5280 : : {
5281 : 12048986 : if (succe)
5282 : : {
5283 : : succe = NULL;
5284 : : break;
5285 : : }
5286 : : succe = e;
5287 : : }
5288 : 6115052 : if (succe
5289 : : /* Limit the number of edges we check, we should bring in
5290 : : context from the iteration and compute the single
5291 : : executable incoming edge when visiting a block. */
5292 : 6115052 : && EDGE_COUNT (succe->dest->preds) < 8)
5293 : : {
5294 : : /* Verify the reached block is only reached through succe.
5295 : : If there is only one edge we can spare us the dominator
5296 : : check and iterate directly. */
5297 : 112677 : if (EDGE_COUNT (succe->dest->preds) > 1)
5298 : : {
5299 : 55451 : FOR_EACH_EDGE (e, ei, succe->dest->preds)
5300 : 42325 : if (e != succe
5301 : 27178 : && ((e->flags & EDGE_EXECUTABLE)
5302 : 18422 : || (!allow_back && (e->flags & EDGE_DFS_BACK))))
5303 : : {
5304 : : succe = NULL;
5305 : : break;
5306 : : }
5307 : : }
5308 : 112677 : if (succe)
5309 : : {
5310 : 103912 : bb2 = succe->dest;
5311 : :
5312 : : /* Re-do the dominance check with changed bb2. */
5313 : 103912 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5314 : : return true;
5315 : : }
5316 : : }
5317 : : }
5318 : : /* Iterate to the single successor of bb2 with only a single executable
5319 : : incoming edge. */
5320 : 14874170 : else if (EDGE_COUNT (bb2->succs) == 1
5321 : 14376312 : && EDGE_COUNT (single_succ (bb2)->preds) > 1
5322 : : /* Limit the number of edges we check, we should bring in
5323 : : context from the iteration and compute the single
5324 : : executable incoming edge when visiting a block. */
5325 : 28995200 : && EDGE_COUNT (single_succ (bb2)->preds) < 8)
5326 : : {
5327 : 5006218 : edge prede = NULL;
5328 : 11333926 : FOR_EACH_EDGE (e, ei, single_succ (bb2)->preds)
5329 : 10783991 : if ((e->flags & EDGE_EXECUTABLE)
5330 : 1372944 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5331 : : {
5332 : 9415119 : if (prede)
5333 : : {
5334 : : prede = NULL;
5335 : : break;
5336 : : }
5337 : : prede = e;
5338 : : }
5339 : : /* We might actually get to a query with BB2 not visited yet when
5340 : : we're querying for a predicated value. */
5341 : 5006218 : if (prede && prede->src == bb2)
5342 : : {
5343 : 489578 : bb2 = prede->dest;
5344 : :
5345 : : /* Re-do the dominance check with changed bb2. */
5346 : 489578 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5347 : : return true;
5348 : : }
5349 : : }
5350 : :
5351 : : /* We could now iterate updating bb1 / bb2. */
5352 : : return false;
5353 : : }
5354 : :
5355 : : /* Set the value number of FROM to TO, return true if it has changed
5356 : : as a result. */
5357 : :
5358 : : static inline bool
5359 : 206334793 : set_ssa_val_to (tree from, tree to)
5360 : : {
5361 : 206334793 : vn_ssa_aux_t from_info = VN_INFO (from);
5362 : 206334793 : tree currval = from_info->valnum; // SSA_VAL (from)
5363 : 206334793 : poly_int64 toff, coff;
5364 : 206334793 : bool curr_undefined = false;
5365 : 206334793 : bool curr_invariant = false;
5366 : :
5367 : : /* The only thing we allow as value numbers are ssa_names
5368 : : and invariants. So assert that here. We don't allow VN_TOP
5369 : : as visiting a stmt should produce a value-number other than
5370 : : that.
5371 : : ??? Still VN_TOP can happen for unreachable code, so force
5372 : : it to varying in that case. Not all code is prepared to
5373 : : get VN_TOP on valueization. */
5374 : 206334793 : if (to == VN_TOP)
5375 : : {
5376 : : /* ??? When iterating and visiting PHI <undef, backedge-value>
5377 : : for the first time we rightfully get VN_TOP and we need to
5378 : : preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5379 : : With SCCVN we were simply lucky we iterated the other PHI
5380 : : cycles first and thus visited the backedge-value DEF. */
5381 : 0 : if (currval == VN_TOP)
5382 : 0 : goto set_and_exit;
5383 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5384 : 0 : fprintf (dump_file, "Forcing value number to varying on "
5385 : : "receiving VN_TOP\n");
5386 : : to = from;
5387 : : }
5388 : :
5389 : 206334793 : gcc_checking_assert (to != NULL_TREE
5390 : : && ((TREE_CODE (to) == SSA_NAME
5391 : : && (to == from || SSA_VAL (to) == to))
5392 : : || is_gimple_min_invariant (to)));
5393 : :
5394 : 206334793 : if (from != to)
5395 : : {
5396 : 32544381 : if (currval == from)
5397 : : {
5398 : 14284 : if (dump_file && (dump_flags & TDF_DETAILS))
5399 : : {
5400 : 0 : fprintf (dump_file, "Not changing value number of ");
5401 : 0 : print_generic_expr (dump_file, from);
5402 : 0 : fprintf (dump_file, " from VARYING to ");
5403 : 0 : print_generic_expr (dump_file, to);
5404 : 0 : fprintf (dump_file, "\n");
5405 : : }
5406 : 14284 : return false;
5407 : : }
5408 : 32530097 : curr_invariant = is_gimple_min_invariant (currval);
5409 : 65060194 : curr_undefined = (TREE_CODE (currval) == SSA_NAME
5410 : 3905082 : && !virtual_operand_p (currval)
5411 : 36183196 : && ssa_undefined_value_p (currval, false));
5412 : 32530097 : if (currval != VN_TOP
5413 : : && !curr_invariant
5414 : 5400071 : && !curr_undefined
5415 : 36420901 : && is_gimple_min_invariant (to))
5416 : : {
5417 : 218 : if (dump_file && (dump_flags & TDF_DETAILS))
5418 : : {
5419 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5420 : : "value number of ");
5421 : 0 : print_generic_expr (dump_file, from);
5422 : 0 : fprintf (dump_file, " from ");
5423 : 0 : print_generic_expr (dump_file, currval);
5424 : 0 : fprintf (dump_file, " (non-constant) to ");
5425 : 0 : print_generic_expr (dump_file, to);
5426 : 0 : fprintf (dump_file, " (constant)\n");
5427 : : }
5428 : : to = from;
5429 : : }
5430 : 32529879 : else if (currval != VN_TOP
5431 : 5399853 : && !curr_undefined
5432 : 5385575 : && TREE_CODE (to) == SSA_NAME
5433 : 4544949 : && !virtual_operand_p (to)
5434 : 36822845 : && ssa_undefined_value_p (to, false))
5435 : : {
5436 : 6 : if (dump_file && (dump_flags & TDF_DETAILS))
5437 : : {
5438 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5439 : : "value number of ");
5440 : 0 : print_generic_expr (dump_file, from);
5441 : 0 : fprintf (dump_file, " from ");
5442 : 0 : print_generic_expr (dump_file, currval);
5443 : 0 : fprintf (dump_file, " (non-undefined) to ");
5444 : 0 : print_generic_expr (dump_file, to);
5445 : 0 : fprintf (dump_file, " (undefined)\n");
5446 : : }
5447 : : to = from;
5448 : : }
5449 : 32529873 : else if (TREE_CODE (to) == SSA_NAME
5450 : 32529873 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5451 : : to = from;
5452 : : }
5453 : :
5454 : 173790412 : set_and_exit:
5455 : 206320509 : if (dump_file && (dump_flags & TDF_DETAILS))
5456 : : {
5457 : 378711 : fprintf (dump_file, "Setting value number of ");
5458 : 378711 : print_generic_expr (dump_file, from);
5459 : 378711 : fprintf (dump_file, " to ");
5460 : 378711 : print_generic_expr (dump_file, to);
5461 : : }
5462 : :
5463 : 206320509 : if (currval != to
5464 : 168002998 : && !operand_equal_p (currval, to, 0)
5465 : : /* Different undefined SSA names are not actually different. See
5466 : : PR82320 for a testcase were we'd otherwise not terminate iteration. */
5467 : 167933670 : && !(curr_undefined
5468 : 3391 : && TREE_CODE (to) == SSA_NAME
5469 : 580 : && !virtual_operand_p (to)
5470 : 580 : && ssa_undefined_value_p (to, false))
5471 : : /* ??? For addresses involving volatile objects or types operand_equal_p
5472 : : does not reliably detect ADDR_EXPRs as equal. We know we are only
5473 : : getting invariant gimple addresses here, so can use
5474 : : get_addr_base_and_unit_offset to do this comparison. */
5475 : 374253566 : && !(TREE_CODE (currval) == ADDR_EXPR
5476 : 472830 : && TREE_CODE (to) == ADDR_EXPR
5477 : 12 : && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5478 : 6 : == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5479 : 6 : && known_eq (coff, toff)))
5480 : : {
5481 : 167933051 : if (to != from
5482 : 28130651 : && currval != VN_TOP
5483 : 1004007 : && !curr_undefined
5484 : : /* We do not want to allow lattice transitions from one value
5485 : : to another since that may lead to not terminating iteration
5486 : : (see PR95049). Since there's no convenient way to check
5487 : : for the allowed transition of VAL -> PHI (loop entry value,
5488 : : same on two PHIs, to same PHI result) we restrict the check
5489 : : to invariants. */
5490 : 1004007 : && curr_invariant
5491 : 168587408 : && is_gimple_min_invariant (to))
5492 : : {
5493 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5494 : 0 : fprintf (dump_file, " forced VARYING");
5495 : : to = from;
5496 : : }
5497 : 167933051 : if (dump_file && (dump_flags & TDF_DETAILS))
5498 : 378395 : fprintf (dump_file, " (changed)\n");
5499 : 167933051 : from_info->valnum = to;
5500 : 167933051 : return true;
5501 : : }
5502 : 38387458 : if (dump_file && (dump_flags & TDF_DETAILS))
5503 : 316 : fprintf (dump_file, "\n");
5504 : : return false;
5505 : : }
5506 : :
5507 : : /* Set all definitions in STMT to value number to themselves.
5508 : : Return true if a value number changed. */
5509 : :
5510 : : static bool
5511 : 288460001 : defs_to_varying (gimple *stmt)
5512 : : {
5513 : 288460001 : bool changed = false;
5514 : 288460001 : ssa_op_iter iter;
5515 : 288460001 : def_operand_p defp;
5516 : :
5517 : 318155063 : FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5518 : : {
5519 : 29695062 : tree def = DEF_FROM_PTR (defp);
5520 : 29695062 : changed |= set_ssa_val_to (def, def);
5521 : : }
5522 : 288460001 : return changed;
5523 : : }
5524 : :
5525 : : /* Visit a copy between LHS and RHS, return true if the value number
5526 : : changed. */
5527 : :
5528 : : static bool
5529 : 7965286 : visit_copy (tree lhs, tree rhs)
5530 : : {
5531 : : /* Valueize. */
5532 : 7965286 : rhs = SSA_VAL (rhs);
5533 : :
5534 : 7965286 : return set_ssa_val_to (lhs, rhs);
5535 : : }
5536 : :
5537 : : /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5538 : : is the same. */
5539 : :
5540 : : static tree
5541 : 2456097 : valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5542 : : {
5543 : 2456097 : if (TREE_CODE (op) == SSA_NAME)
5544 : 2142578 : op = vn_valueize (op);
5545 : :
5546 : : /* Either we have the op widened available. */
5547 : 2456097 : tree ops[3] = {};
5548 : 2456097 : ops[0] = op;
5549 : 2456097 : tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5550 : : wide_type, ops, NULL);
5551 : 2456097 : if (tem)
5552 : : return tem;
5553 : :
5554 : : /* Or the op is truncated from some existing value. */
5555 : 2166718 : if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5556 : : {
5557 : 547157 : gimple *def = SSA_NAME_DEF_STMT (op);
5558 : 547157 : if (is_gimple_assign (def)
5559 : 547157 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5560 : : {
5561 : 286496 : tem = gimple_assign_rhs1 (def);
5562 : 286496 : if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5563 : : {
5564 : 175630 : if (TREE_CODE (tem) == SSA_NAME)
5565 : 175630 : tem = vn_valueize (tem);
5566 : 175630 : return tem;
5567 : : }
5568 : : }
5569 : : }
5570 : :
5571 : : /* For constants simply extend it. */
5572 : 1991088 : if (TREE_CODE (op) == INTEGER_CST)
5573 : 346255 : return wide_int_to_tree (wide_type, wi::to_widest (op));
5574 : :
5575 : : return NULL_TREE;
5576 : : }
5577 : :
5578 : : /* Visit a nary operator RHS, value number it, and return true if the
5579 : : value number of LHS has changed as a result. */
5580 : :
5581 : : static bool
5582 : 48954836 : visit_nary_op (tree lhs, gassign *stmt)
5583 : : {
5584 : 48954836 : vn_nary_op_t vnresult;
5585 : 48954836 : tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5586 : 48954836 : if (! result && vnresult)
5587 : 154286 : result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5588 : 45345088 : if (result)
5589 : 3680841 : return set_ssa_val_to (lhs, result);
5590 : :
5591 : : /* Do some special pattern matching for redundancies of operations
5592 : : in different types. */
5593 : 45273995 : enum tree_code code = gimple_assign_rhs_code (stmt);
5594 : 45273995 : tree type = TREE_TYPE (lhs);
5595 : 45273995 : tree rhs1 = gimple_assign_rhs1 (stmt);
5596 : 45273995 : switch (code)
5597 : : {
5598 : 10160683 : CASE_CONVERT:
5599 : : /* Match arithmetic done in a different type where we can easily
5600 : : substitute the result from some earlier sign-changed or widened
5601 : : operation. */
5602 : 10160683 : if (INTEGRAL_TYPE_P (type)
5603 : 9120179 : && TREE_CODE (rhs1) == SSA_NAME
5604 : : /* We only handle sign-changes, zero-extension -> & mask or
5605 : : sign-extension if we know the inner operation doesn't
5606 : : overflow. */
5607 : 19043952 : && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5608 : 5323702 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5609 : 5322511 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5610 : 8155772 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5611 : 5982635 : || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5612 : : {
5613 : 7752717 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5614 : 5589196 : if (def
5615 : 5589196 : && (gimple_assign_rhs_code (def) == PLUS_EXPR
5616 : 4332302 : || gimple_assign_rhs_code (def) == MINUS_EXPR
5617 : 4196265 : || gimple_assign_rhs_code (def) == MULT_EXPR))
5618 : : {
5619 : 1983014 : tree ops[3] = {};
5620 : : /* When requiring a sign-extension we cannot model a
5621 : : previous truncation with a single op so don't bother. */
5622 : 1983014 : bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5623 : : /* Either we have the op widened available. */
5624 : 1983014 : ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5625 : : allow_truncate);
5626 : 1983014 : if (ops[0])
5627 : 946166 : ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5628 : : allow_truncate);
5629 : 1983014 : if (ops[0] && ops[1])
5630 : : {
5631 : 338181 : ops[0] = vn_nary_op_lookup_pieces
5632 : 338181 : (2, gimple_assign_rhs_code (def), type, ops, NULL);
5633 : : /* We have wider operation available. */
5634 : 338181 : if (ops[0]
5635 : : /* If the leader is a wrapping operation we can
5636 : : insert it for code hoisting w/o introducing
5637 : : undefined overflow. If it is not it has to
5638 : : be available. See PR86554. */
5639 : 338181 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
5640 : 2049 : || (rpo_avail && vn_context_bb
5641 : 2049 : && rpo_avail->eliminate_avail (vn_context_bb,
5642 : : ops[0]))))
5643 : : {
5644 : 9303 : unsigned lhs_prec = TYPE_PRECISION (type);
5645 : 9303 : unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5646 : 9303 : if (lhs_prec == rhs_prec
5647 : 9303 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5648 : 1709 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5649 : : {
5650 : 8713 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5651 : 8713 : NOP_EXPR, type, ops[0]);
5652 : 8713 : result = vn_nary_build_or_lookup (&match_op);
5653 : 8713 : if (result)
5654 : : {
5655 : 8713 : bool changed = set_ssa_val_to (lhs, result);
5656 : 8713 : if (TREE_CODE (result) == SSA_NAME)
5657 : 8713 : vn_nary_op_insert_stmt (stmt, result);
5658 : 8713 : return changed;
5659 : : }
5660 : : }
5661 : : else
5662 : : {
5663 : 590 : tree mask = wide_int_to_tree
5664 : 590 : (type, wi::mask (rhs_prec, false, lhs_prec));
5665 : 590 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5666 : 590 : BIT_AND_EXPR,
5667 : 590 : TREE_TYPE (lhs),
5668 : 590 : ops[0], mask);
5669 : 590 : result = vn_nary_build_or_lookup (&match_op);
5670 : 590 : if (result)
5671 : : {
5672 : 590 : bool changed = set_ssa_val_to (lhs, result);
5673 : 590 : if (TREE_CODE (result) == SSA_NAME)
5674 : 590 : vn_nary_op_insert_stmt (stmt, result);
5675 : 590 : return changed;
5676 : : }
5677 : : }
5678 : : }
5679 : : }
5680 : : }
5681 : : }
5682 : : break;
5683 : 1612039 : case BIT_AND_EXPR:
5684 : 1612039 : if (INTEGRAL_TYPE_P (type)
5685 : 1576154 : && TREE_CODE (rhs1) == SSA_NAME
5686 : 1576154 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5687 : 994327 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5688 : 994209 : && default_vn_walk_kind != VN_NOWALK
5689 : : && CHAR_BIT == 8
5690 : : && BITS_PER_UNIT == 8
5691 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5692 : 994001 : && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5693 : 993999 : && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5694 : 2606038 : && !integer_zerop (gimple_assign_rhs2 (stmt)))
5695 : : {
5696 : 993999 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5697 : 738965 : if (ass
5698 : 738965 : && !gimple_has_volatile_ops (ass)
5699 : 737519 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5700 : : {
5701 : 369242 : tree last_vuse = gimple_vuse (ass);
5702 : 369242 : tree op = gimple_assign_rhs1 (ass);
5703 : 1107726 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5704 : : default_vn_walk_kind,
5705 : : NULL, true, &last_vuse,
5706 : : gimple_assign_rhs2 (stmt));
5707 : 369242 : if (result
5708 : 369936 : && useless_type_conversion_p (TREE_TYPE (result),
5709 : 694 : TREE_TYPE (op)))
5710 : 694 : return set_ssa_val_to (lhs, result);
5711 : : }
5712 : : }
5713 : : break;
5714 : 218812 : case BIT_FIELD_REF:
5715 : 218812 : if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
5716 : : {
5717 : 218792 : tree op0 = TREE_OPERAND (rhs1, 0);
5718 : 218792 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0));
5719 : 178151 : if (ass
5720 : 178151 : && !gimple_has_volatile_ops (ass)
5721 : 178068 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5722 : : {
5723 : 84442 : tree last_vuse = gimple_vuse (ass);
5724 : 84442 : tree op = gimple_assign_rhs1 (ass);
5725 : : /* Avoid building invalid and unexpected refs. */
5726 : 84442 : if (TREE_CODE (op) != TARGET_MEM_REF
5727 : : && TREE_CODE (op) != BIT_FIELD_REF
5728 : : && TREE_CODE (op) != REALPART_EXPR
5729 : : && TREE_CODE (op) != IMAGPART_EXPR)
5730 : : {
5731 : 77306 : tree op = build3 (BIT_FIELD_REF, TREE_TYPE (rhs1),
5732 : : gimple_assign_rhs1 (ass),
5733 : 77306 : TREE_OPERAND (rhs1, 1),
5734 : 77306 : TREE_OPERAND (rhs1, 2));
5735 : 154612 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5736 : : default_vn_walk_kind,
5737 : : NULL, true, &last_vuse);
5738 : 77306 : if (result
5739 : 77306 : && useless_type_conversion_p (type, TREE_TYPE (result)))
5740 : 1116 : return set_ssa_val_to (lhs, result);
5741 : 76552 : else if (result
5742 : 362 : && TYPE_SIZE (type)
5743 : 362 : && TYPE_SIZE (TREE_TYPE (result))
5744 : 76914 : && operand_equal_p (TYPE_SIZE (type),
5745 : 362 : TYPE_SIZE (TREE_TYPE (result))))
5746 : : {
5747 : 362 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5748 : 362 : VIEW_CONVERT_EXPR,
5749 : 362 : type, result);
5750 : 362 : result = vn_nary_build_or_lookup (&match_op);
5751 : 362 : if (result)
5752 : : {
5753 : 362 : bool changed = set_ssa_val_to (lhs, result);
5754 : 362 : if (TREE_CODE (result) == SSA_NAME)
5755 : 350 : vn_nary_op_insert_stmt (stmt, result);
5756 : 362 : return changed;
5757 : : }
5758 : : }
5759 : : }
5760 : : }
5761 : : }
5762 : : break;
5763 : 340421 : case TRUNC_DIV_EXPR:
5764 : 340421 : if (TYPE_UNSIGNED (type))
5765 : : break;
5766 : : /* Fallthru. */
5767 : 5390120 : case RDIV_EXPR:
5768 : 5390120 : case MULT_EXPR:
5769 : : /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5770 : 5390120 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5771 : : {
5772 : 5389210 : tree rhs[2];
5773 : 5389210 : rhs[0] = rhs1;
5774 : 5389210 : rhs[1] = gimple_assign_rhs2 (stmt);
5775 : 16160021 : for (unsigned i = 0; i <= 1; ++i)
5776 : : {
5777 : 10777266 : unsigned j = i == 0 ? 1 : 0;
5778 : 10777266 : tree ops[2];
5779 : 10777266 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5780 : 10777266 : NEGATE_EXPR, type, rhs[i]);
5781 : 10777266 : ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5782 : 10777266 : ops[j] = rhs[j];
5783 : 10777266 : if (ops[i]
5784 : 10777266 : && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5785 : : type, ops, NULL)))
5786 : : {
5787 : 6455 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5788 : 6455 : NEGATE_EXPR, type, ops[0]);
5789 : 6455 : result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5790 : 6455 : if (result)
5791 : : {
5792 : 6455 : bool changed = set_ssa_val_to (lhs, result);
5793 : 6455 : if (TREE_CODE (result) == SSA_NAME)
5794 : 6455 : vn_nary_op_insert_stmt (stmt, result);
5795 : 6455 : return changed;
5796 : : }
5797 : : }
5798 : : }
5799 : : }
5800 : : break;
5801 : 360785 : case LSHIFT_EXPR:
5802 : : /* For X << C, use the value number of X * (1 << C). */
5803 : 360785 : if (INTEGRAL_TYPE_P (type)
5804 : 348828 : && TYPE_OVERFLOW_WRAPS (type)
5805 : 549283 : && !TYPE_SATURATING (type))
5806 : : {
5807 : 188498 : tree rhs2 = gimple_assign_rhs2 (stmt);
5808 : 188498 : if (TREE_CODE (rhs2) == INTEGER_CST
5809 : 110348 : && tree_fits_uhwi_p (rhs2)
5810 : 298846 : && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
5811 : : {
5812 : 110348 : wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
5813 : 110348 : TYPE_PRECISION (type));
5814 : 220696 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5815 : 110348 : MULT_EXPR, type, rhs1,
5816 : 110348 : wide_int_to_tree (type, w));
5817 : 110348 : result = vn_nary_build_or_lookup (&match_op);
5818 : 110348 : if (result)
5819 : : {
5820 : 110348 : bool changed = set_ssa_val_to (lhs, result);
5821 : 110348 : if (TREE_CODE (result) == SSA_NAME)
5822 : 110347 : vn_nary_op_insert_stmt (stmt, result);
5823 : 110348 : return changed;
5824 : : }
5825 : 110348 : }
5826 : : }
5827 : : break;
5828 : : default:
5829 : : break;
5830 : : }
5831 : :
5832 : 45146079 : bool changed = set_ssa_val_to (lhs, lhs);
5833 : 45146079 : vn_nary_op_insert_stmt (stmt, lhs);
5834 : 45146079 : return changed;
5835 : : }
5836 : :
5837 : : /* Visit a call STMT storing into LHS. Return true if the value number
5838 : : of the LHS has changed as a result. */
5839 : :
5840 : : static bool
5841 : 8671341 : visit_reference_op_call (tree lhs, gcall *stmt)
5842 : : {
5843 : 8671341 : bool changed = false;
5844 : 8671341 : struct vn_reference_s vr1;
5845 : 8671341 : vn_reference_t vnresult = NULL;
5846 : 8671341 : tree vdef = gimple_vdef (stmt);
5847 : 8671341 : modref_summary *summary;
5848 : :
5849 : : /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5850 : 8671341 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
5851 : 4617111 : lhs = NULL_TREE;
5852 : :
5853 : 8671341 : vn_reference_lookup_call (stmt, &vnresult, &vr1);
5854 : :
5855 : : /* If the lookup did not succeed for pure functions try to use
5856 : : modref info to find a candidate to CSE to. */
5857 : 8671341 : const unsigned accesses_limit = 8;
5858 : 8671341 : if (!vnresult
5859 : 8031908 : && !vdef
5860 : 8031908 : && lhs
5861 : 2817932 : && gimple_vuse (stmt)
5862 : 10252177 : && (((summary = get_modref_function_summary (stmt, NULL))
5863 : 220427 : && !summary->global_memory_read
5864 : 81759 : && summary->load_accesses < accesses_limit)
5865 : 1499218 : || gimple_call_flags (stmt) & ECF_CONST))
5866 : : {
5867 : : /* First search if we can do someting useful and build a
5868 : : vector of all loads we have to check. */
5869 : 82362 : bool unknown_memory_access = false;
5870 : 82362 : auto_vec<ao_ref, accesses_limit> accesses;
5871 : 82362 : unsigned load_accesses = summary ? summary->load_accesses : 0;
5872 : 82362 : if (!unknown_memory_access)
5873 : : /* Add loads done as part of setting up the call arguments.
5874 : : That's also necessary for CONST functions which will
5875 : : not have a modref summary. */
5876 : 248512 : for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
5877 : : {
5878 : 166158 : tree arg = gimple_call_arg (stmt, i);
5879 : 166158 : if (TREE_CODE (arg) != SSA_NAME
5880 : 166158 : && !is_gimple_min_invariant (arg))
5881 : : {
5882 : 62072 : if (accesses.length () >= accesses_limit - load_accesses)
5883 : : {
5884 : : unknown_memory_access = true;
5885 : : break;
5886 : : }
5887 : 31028 : accesses.quick_grow (accesses.length () + 1);
5888 : 31028 : ao_ref_init (&accesses.last (), arg);
5889 : : }
5890 : : }
5891 : 82362 : if (summary && !unknown_memory_access)
5892 : : {
5893 : : /* Add loads as analyzed by IPA modref. */
5894 : 284885 : for (auto base_node : summary->loads->bases)
5895 : 71801 : if (unknown_memory_access)
5896 : : break;
5897 : 292618 : else for (auto ref_node : base_node->refs)
5898 : 78022 : if (unknown_memory_access)
5899 : : break;
5900 : 317608 : else for (auto access_node : ref_node->accesses)
5901 : : {
5902 : 206110 : accesses.quick_grow (accesses.length () + 1);
5903 : 103055 : ao_ref *r = &accesses.last ();
5904 : 103055 : if (!access_node.get_ao_ref (stmt, r))
5905 : : {
5906 : : /* Initialize a ref based on the argument and
5907 : : unknown offset if possible. */
5908 : 19477 : tree arg = access_node.get_call_arg (stmt);
5909 : 19477 : if (arg && TREE_CODE (arg) == SSA_NAME)
5910 : 5773 : arg = SSA_VAL (arg);
5911 : 5773 : if (arg
5912 : 19467 : && TREE_CODE (arg) == ADDR_EXPR
5913 : 13845 : && (arg = get_base_address (arg))
5914 : 19618 : && DECL_P (arg))
5915 : : {
5916 : 0 : ao_ref_init (r, arg);
5917 : 0 : r->ref = NULL_TREE;
5918 : 0 : r->base = arg;
5919 : : }
5920 : : else
5921 : : {
5922 : : unknown_memory_access = true;
5923 : : break;
5924 : : }
5925 : : }
5926 : 83578 : r->base_alias_set = base_node->base;
5927 : 83578 : r->ref_alias_set = ref_node->ref;
5928 : : }
5929 : : }
5930 : :
5931 : : /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5932 : : for the call in the hashtable. */
5933 : 82362 : unsigned limit = (unknown_memory_access
5934 : 82362 : ? 0
5935 : 62877 : : (param_sccvn_max_alias_queries_per_access
5936 : 62877 : / (accesses.length () + 1)));
5937 : 82362 : tree saved_vuse = vr1.vuse;
5938 : 82362 : hashval_t saved_hashcode = vr1.hashcode;
5939 : 402626 : while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
5940 : : {
5941 : 341216 : vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
5942 : 341216 : gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
5943 : : /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5944 : : do not bother for now. */
5945 : 341216 : if (is_a <gphi *> (def))
5946 : : break;
5947 : 640528 : vr1.vuse = vuse_ssa_val (gimple_vuse (def));
5948 : 320264 : vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
5949 : 320264 : vn_reference_lookup_1 (&vr1, &vnresult);
5950 : 320264 : limit--;
5951 : : }
5952 : :
5953 : : /* If we found a candidate to CSE to verify it is valid. */
5954 : 82362 : if (vnresult && !accesses.is_empty ())
5955 : : {
5956 : 1904 : tree vuse = vuse_ssa_val (gimple_vuse (stmt));
5957 : 7128 : while (vnresult && vuse != vr1.vuse)
5958 : : {
5959 : 3320 : gimple *def = SSA_NAME_DEF_STMT (vuse);
5960 : 17608 : for (auto &ref : accesses)
5961 : : {
5962 : : /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5963 : : analysis overhead that we might be able to cache. */
5964 : 9384 : if (stmt_may_clobber_ref_p_1 (def, &ref, true))
5965 : : {
5966 : 1736 : vnresult = NULL;
5967 : 1736 : break;
5968 : : }
5969 : : }
5970 : 6640 : vuse = vuse_ssa_val (gimple_vuse (def));
5971 : : }
5972 : : }
5973 : 82362 : vr1.vuse = saved_vuse;
5974 : 82362 : vr1.hashcode = saved_hashcode;
5975 : 82362 : }
5976 : :
5977 : 8671341 : if (vnresult)
5978 : : {
5979 : 639629 : if (vdef)
5980 : : {
5981 : 175709 : if (vnresult->result_vdef)
5982 : 175709 : changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
5983 : 0 : else if (!lhs && gimple_call_lhs (stmt))
5984 : : /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5985 : : as the call still acts as a lhs store. */
5986 : 0 : changed |= set_ssa_val_to (vdef, vdef);
5987 : : else
5988 : : /* If the call was discovered to be pure or const reflect
5989 : : that as far as possible. */
5990 : 0 : changed |= set_ssa_val_to (vdef,
5991 : : vuse_ssa_val (gimple_vuse (stmt)));
5992 : : }
5993 : :
5994 : 639629 : if (!vnresult->result && lhs)
5995 : 0 : vnresult->result = lhs;
5996 : :
5997 : 639629 : if (vnresult->result && lhs)
5998 : 100340 : changed |= set_ssa_val_to (lhs, vnresult->result);
5999 : : }
6000 : : else
6001 : : {
6002 : 8031712 : vn_reference_t vr2;
6003 : 8031712 : vn_reference_s **slot;
6004 : 8031712 : tree vdef_val = vdef;
6005 : 8031712 : if (vdef)
6006 : : {
6007 : : /* If we value numbered an indirect functions function to
6008 : : one not clobbering memory value number its VDEF to its
6009 : : VUSE. */
6010 : 4883836 : tree fn = gimple_call_fn (stmt);
6011 : 4883836 : if (fn && TREE_CODE (fn) == SSA_NAME)
6012 : : {
6013 : 130440 : fn = SSA_VAL (fn);
6014 : 130440 : if (TREE_CODE (fn) == ADDR_EXPR
6015 : 1733 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6016 : 1733 : && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
6017 : 1733 : & (ECF_CONST | ECF_PURE))
6018 : : /* If stmt has non-SSA_NAME lhs, value number the
6019 : : vdef to itself, as the call still acts as a lhs
6020 : : store. */
6021 : 131582 : && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
6022 : 2150 : vdef_val = vuse_ssa_val (gimple_vuse (stmt));
6023 : : }
6024 : 4883836 : changed |= set_ssa_val_to (vdef, vdef_val);
6025 : : }
6026 : 8031712 : if (lhs)
6027 : 3953890 : changed |= set_ssa_val_to (lhs, lhs);
6028 : 8031712 : vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
6029 : 8031712 : vr2->vuse = vr1.vuse;
6030 : : /* As we are not walking the virtual operand chain we know the
6031 : : shared_lookup_references are still original so we can re-use
6032 : : them here. */
6033 : 8031712 : vr2->operands = vr1.operands.copy ();
6034 : 8031712 : vr2->type = vr1.type;
6035 : 8031712 : vr2->punned = vr1.punned;
6036 : 8031712 : vr2->set = vr1.set;
6037 : 8031712 : vr2->offset = vr1.offset;
6038 : 8031712 : vr2->max_size = vr1.max_size;
6039 : 8031712 : vr2->base_set = vr1.base_set;
6040 : 8031712 : vr2->hashcode = vr1.hashcode;
6041 : 8031712 : vr2->result = lhs;
6042 : 8031712 : vr2->result_vdef = vdef_val;
6043 : 8031712 : vr2->value_id = 0;
6044 : 8031712 : slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
6045 : : INSERT);
6046 : 8031712 : gcc_assert (!*slot);
6047 : 8031712 : *slot = vr2;
6048 : 8031712 : vr2->next = last_inserted_ref;
6049 : 8031712 : last_inserted_ref = vr2;
6050 : : }
6051 : :
6052 : 8671341 : return changed;
6053 : : }
6054 : :
6055 : : /* Visit a load from a reference operator RHS, part of STMT, value number it,
6056 : : and return true if the value number of the LHS has changed as a result. */
6057 : :
6058 : : static bool
6059 : 35002922 : visit_reference_op_load (tree lhs, tree op, gimple *stmt)
6060 : : {
6061 : 35002922 : bool changed = false;
6062 : 35002922 : tree result;
6063 : 35002922 : vn_reference_t res;
6064 : :
6065 : 35002922 : tree vuse = gimple_vuse (stmt);
6066 : 35002922 : tree last_vuse = vuse;
6067 : 35002922 : result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
6068 : :
6069 : : /* We handle type-punning through unions by value-numbering based
6070 : : on offset and size of the access. Be prepared to handle a
6071 : : type-mismatch here via creating a VIEW_CONVERT_EXPR. */
6072 : 35002922 : if (result
6073 : 35002922 : && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
6074 : : {
6075 : 21008 : if (CONSTANT_CLASS_P (result))
6076 : 4264 : result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6077 : : else
6078 : : {
6079 : : /* We will be setting the value number of lhs to the value number
6080 : : of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
6081 : : So first simplify and lookup this expression to see if it
6082 : : is already available. */
6083 : 16744 : gimple_match_op res_op (gimple_match_cond::UNCOND,
6084 : 16744 : VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6085 : 16744 : result = vn_nary_build_or_lookup (&res_op);
6086 : 16744 : if (result
6087 : 16738 : && TREE_CODE (result) == SSA_NAME
6088 : 31804 : && VN_INFO (result)->needs_insertion)
6089 : : /* Track whether this is the canonical expression for different
6090 : : typed loads. We use that as a stopgap measure for code
6091 : : hoisting when dealing with floating point loads. */
6092 : 14229 : res->punned = true;
6093 : : }
6094 : :
6095 : : /* When building the conversion fails avoid inserting the reference
6096 : : again. */
6097 : 21008 : if (!result)
6098 : 6 : return set_ssa_val_to (lhs, lhs);
6099 : : }
6100 : :
6101 : 34981914 : if (result)
6102 : 5576697 : changed = set_ssa_val_to (lhs, result);
6103 : : else
6104 : : {
6105 : 29426219 : changed = set_ssa_val_to (lhs, lhs);
6106 : 29426219 : vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
6107 : 29426219 : if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
6108 : : {
6109 : 9005866 : if (dump_file && (dump_flags & TDF_DETAILS))
6110 : : {
6111 : 14421 : fprintf (dump_file, "Using extra use virtual operand ");
6112 : 14421 : print_generic_expr (dump_file, last_vuse);
6113 : 14421 : fprintf (dump_file, "\n");
6114 : : }
6115 : 9005866 : vn_reference_insert (op, lhs, vuse, NULL_TREE);
6116 : : }
6117 : : }
6118 : :
6119 : : return changed;
6120 : : }
6121 : :
6122 : :
6123 : : /* Visit a store to a reference operator LHS, part of STMT, value number it,
6124 : : and return true if the value number of the LHS has changed as a result. */
6125 : :
6126 : : static bool
6127 : 33080803 : visit_reference_op_store (tree lhs, tree op, gimple *stmt)
6128 : : {
6129 : 33080803 : bool changed = false;
6130 : 33080803 : vn_reference_t vnresult = NULL;
6131 : 33080803 : tree assign;
6132 : 33080803 : bool resultsame = false;
6133 : 33080803 : tree vuse = gimple_vuse (stmt);
6134 : 33080803 : tree vdef = gimple_vdef (stmt);
6135 : :
6136 : 33080803 : if (TREE_CODE (op) == SSA_NAME)
6137 : 14973453 : op = SSA_VAL (op);
6138 : :
6139 : : /* First we want to lookup using the *vuses* from the store and see
6140 : : if there the last store to this location with the same address
6141 : : had the same value.
6142 : :
6143 : : The vuses represent the memory state before the store. If the
6144 : : memory state, address, and value of the store is the same as the
6145 : : last store to this location, then this store will produce the
6146 : : same memory state as that store.
6147 : :
6148 : : In this case the vdef versions for this store are value numbered to those
6149 : : vuse versions, since they represent the same memory state after
6150 : : this store.
6151 : :
6152 : : Otherwise, the vdefs for the store are used when inserting into
6153 : : the table, since the store generates a new memory state. */
6154 : :
6155 : 33080803 : vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
6156 : 33080803 : if (vnresult
6157 : 1737906 : && vnresult->result)
6158 : : {
6159 : 1737906 : tree result = vnresult->result;
6160 : 1737906 : gcc_checking_assert (TREE_CODE (result) != SSA_NAME
6161 : : || result == SSA_VAL (result));
6162 : 1737906 : resultsame = expressions_equal_p (result, op);
6163 : 1737906 : if (resultsame)
6164 : : {
6165 : : /* If the TBAA state isn't compatible for downstream reads
6166 : : we cannot value-number the VDEFs the same. */
6167 : 51822 : ao_ref lhs_ref;
6168 : 51822 : ao_ref_init (&lhs_ref, lhs);
6169 : 51822 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
6170 : 51822 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
6171 : 51822 : if ((vnresult->set != set
6172 : 702 : && ! alias_set_subset_of (set, vnresult->set))
6173 : 52333 : || (vnresult->base_set != base_set
6174 : 6352 : && ! alias_set_subset_of (base_set, vnresult->base_set)))
6175 : 749 : resultsame = false;
6176 : : }
6177 : : }
6178 : :
6179 : 749 : if (!resultsame)
6180 : : {
6181 : 33029730 : if (dump_file && (dump_flags & TDF_DETAILS))
6182 : : {
6183 : 19228 : fprintf (dump_file, "No store match\n");
6184 : 19228 : fprintf (dump_file, "Value numbering store ");
6185 : 19228 : print_generic_expr (dump_file, lhs);
6186 : 19228 : fprintf (dump_file, " to ");
6187 : 19228 : print_generic_expr (dump_file, op);
6188 : 19228 : fprintf (dump_file, "\n");
6189 : : }
6190 : : /* Have to set value numbers before insert, since insert is
6191 : : going to valueize the references in-place. */
6192 : 33029730 : if (vdef)
6193 : 33029730 : changed |= set_ssa_val_to (vdef, vdef);
6194 : :
6195 : : /* Do not insert structure copies into the tables. */
6196 : 33029730 : if (is_gimple_min_invariant (op)
6197 : 33029730 : || is_gimple_reg (op))
6198 : 29385965 : vn_reference_insert (lhs, op, vdef, NULL);
6199 : :
6200 : : /* Only perform the following when being called from PRE
6201 : : which embeds tail merging. */
6202 : 33029730 : if (default_vn_walk_kind == VN_WALK)
6203 : : {
6204 : 7561987 : assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
6205 : 7561987 : vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
6206 : 7561987 : if (!vnresult)
6207 : 7512945 : vn_reference_insert (assign, lhs, vuse, vdef);
6208 : : }
6209 : : }
6210 : : else
6211 : : {
6212 : : /* We had a match, so value number the vdef to have the value
6213 : : number of the vuse it came from. */
6214 : :
6215 : 51073 : if (dump_file && (dump_flags & TDF_DETAILS))
6216 : 9 : fprintf (dump_file, "Store matched earlier value, "
6217 : : "value numbering store vdefs to matching vuses.\n");
6218 : :
6219 : 51073 : changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
6220 : : }
6221 : :
6222 : 33080803 : return changed;
6223 : : }
6224 : :
6225 : : /* Visit and value number PHI, return true if the value number
6226 : : changed. When BACKEDGES_VARYING_P is true then assume all
6227 : : backedge values are varying. When INSERTED is not NULL then
6228 : : this is just a ahead query for a possible iteration, set INSERTED
6229 : : to true if we'd insert into the hashtable. */
6230 : :
6231 : : static bool
6232 : 35029779 : visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
6233 : : {
6234 : 35029779 : tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
6235 : 35029779 : bool seen_undef_visited = false;
6236 : 35029779 : tree backedge_val = NULL_TREE;
6237 : 35029779 : bool seen_non_backedge = false;
6238 : 35029779 : tree sameval_base = NULL_TREE;
6239 : 35029779 : poly_int64 soff, doff;
6240 : 35029779 : unsigned n_executable = 0;
6241 : 35029779 : edge sameval_e = NULL;
6242 : :
6243 : : /* TODO: We could check for this in initialization, and replace this
6244 : : with a gcc_assert. */
6245 : 35029779 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
6246 : 28197 : return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
6247 : :
6248 : : /* We track whether a PHI was CSEd to avoid excessive iterations
6249 : : that would be necessary only because the PHI changed arguments
6250 : : but not value. */
6251 : 35001582 : if (!inserted)
6252 : 27266824 : gimple_set_plf (phi, GF_PLF_1, false);
6253 : :
6254 : 35001582 : basic_block bb = gimple_bb (phi);
6255 : :
6256 : : /* For the equivalence handling below make sure to first process an
6257 : : edge with a non-constant. */
6258 : 35001582 : auto_vec<edge, 2> preds;
6259 : 70003164 : preds.reserve_exact (EDGE_COUNT (bb->preds));
6260 : 35001582 : bool seen_nonconstant = false;
6261 : 116301546 : for (unsigned i = 0; i < EDGE_COUNT (bb->preds); ++i)
6262 : : {
6263 : 81299964 : edge e = EDGE_PRED (bb, i);
6264 : 81299964 : preds.quick_push (e);
6265 : 81299964 : if (!seen_nonconstant)
6266 : : {
6267 : 42700989 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6268 : 42700989 : if (TREE_CODE (def) == SSA_NAME)
6269 : : {
6270 : 33253624 : seen_nonconstant = true;
6271 : 33253624 : if (i != 0)
6272 : 5659794 : std::swap (preds[0], preds[i]);
6273 : : }
6274 : : }
6275 : : }
6276 : :
6277 : : /* See if all non-TOP arguments have the same value. TOP is
6278 : : equivalent to everything, so we can ignore it. */
6279 : 147519586 : for (edge e : preds)
6280 : 69783363 : if (e->flags & EDGE_EXECUTABLE)
6281 : : {
6282 : 64631768 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6283 : :
6284 : 64631768 : if (def == PHI_RESULT (phi))
6285 : 381084 : continue;
6286 : 64272533 : ++n_executable;
6287 : 64272533 : bool visited = true;
6288 : 64272533 : if (TREE_CODE (def) == SSA_NAME)
6289 : : {
6290 : 51955548 : tree val = SSA_VAL (def, &visited);
6291 : 51955548 : if (SSA_NAME_IS_DEFAULT_DEF (def))
6292 : 2714754 : visited = true;
6293 : 51955548 : if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6294 : 49397653 : def = val;
6295 : 51955548 : if (e->flags & EDGE_DFS_BACK)
6296 : 15674978 : backedge_val = def;
6297 : : }
6298 : 64272533 : if (!(e->flags & EDGE_DFS_BACK))
6299 : 48397473 : seen_non_backedge = true;
6300 : 64272533 : if (def == VN_TOP)
6301 : : ;
6302 : : /* Ignore undefined defs for sameval but record one. */
6303 : 64272533 : else if (TREE_CODE (def) == SSA_NAME
6304 : 48505962 : && ! virtual_operand_p (def)
6305 : 89100022 : && ssa_undefined_value_p (def, false))
6306 : : {
6307 : 229386 : if (!seen_undef
6308 : : /* Avoid having not visited undefined defs if we also have
6309 : : a visited one. */
6310 : 28300 : || (!seen_undef_visited && visited))
6311 : : {
6312 : 201087 : seen_undef = def;
6313 : 201087 : seen_undef_visited = visited;
6314 : : }
6315 : : }
6316 : 64043147 : else if (sameval == VN_TOP)
6317 : : {
6318 : : sameval = def;
6319 : : sameval_e = e;
6320 : : }
6321 : 29089934 : else if (expressions_equal_p (def, sameval))
6322 : : sameval_e = NULL;
6323 : 45649065 : else if (virtual_operand_p (def))
6324 : : {
6325 : : sameval = NULL_TREE;
6326 : 27268523 : break;
6327 : : }
6328 : : else
6329 : : {
6330 : : /* We know we're arriving only with invariant addresses here,
6331 : : try harder comparing them. We can do some caching here
6332 : : which we cannot do in expressions_equal_p. */
6333 : 17076924 : if (TREE_CODE (def) == ADDR_EXPR
6334 : 428175 : && TREE_CODE (sameval) == ADDR_EXPR
6335 : 121737 : && sameval_base != (void *)-1)
6336 : : {
6337 : 121737 : if (!sameval_base)
6338 : 121735 : sameval_base = get_addr_base_and_unit_offset
6339 : 121735 : (TREE_OPERAND (sameval, 0), &soff);
6340 : 121735 : if (!sameval_base)
6341 : : sameval_base = (tree)(void *)-1;
6342 : 121742 : else if ((get_addr_base_and_unit_offset
6343 : 121737 : (TREE_OPERAND (def, 0), &doff) == sameval_base)
6344 : 121737 : && known_eq (soff, doff))
6345 : 5 : continue;
6346 : : }
6347 : : /* There's also the possibility to use equivalences. */
6348 : 33063972 : if (!FLOAT_TYPE_P (TREE_TYPE (def))
6349 : : /* But only do this if we didn't force any of sameval or
6350 : : val to VARYING because of backedge processing rules. */
6351 : 15883662 : && (TREE_CODE (sameval) != SSA_NAME
6352 : 12535311 : || SSA_VAL (sameval) == sameval)
6353 : 32960517 : && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6354 : : {
6355 : 15883585 : vn_nary_op_t vnresult;
6356 : 15883585 : tree ops[2];
6357 : 15883585 : ops[0] = def;
6358 : 15883585 : ops[1] = sameval;
6359 : : /* Canonicalize the operands order for eq below. */
6360 : 15883585 : if (tree_swap_operands_p (ops[0], ops[1]))
6361 : 9517421 : std::swap (ops[0], ops[1]);
6362 : 15883585 : tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6363 : : boolean_type_node,
6364 : : ops, &vnresult);
6365 : 15883585 : if (! val && vnresult && vnresult->predicated_values)
6366 : : {
6367 : 209432 : val = vn_nary_op_get_predicated_value (vnresult, e);
6368 : 118783 : if (val && integer_truep (val)
6369 : 231408 : && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6370 : : {
6371 : 21844 : if (dump_file && (dump_flags & TDF_DETAILS))
6372 : : {
6373 : 2 : fprintf (dump_file, "Predication says ");
6374 : 2 : print_generic_expr (dump_file, def, TDF_NONE);
6375 : 2 : fprintf (dump_file, " and ");
6376 : 2 : print_generic_expr (dump_file, sameval, TDF_NONE);
6377 : 2 : fprintf (dump_file, " are equal on edge %d -> %d\n",
6378 : 2 : e->src->index, e->dest->index);
6379 : : }
6380 : 21844 : continue;
6381 : : }
6382 : : }
6383 : : }
6384 : : sameval = NULL_TREE;
6385 : : break;
6386 : : }
6387 : : }
6388 : :
6389 : : /* If the value we want to use is flowing over the backedge and we
6390 : : should take it as VARYING but it has a non-VARYING value drop to
6391 : : VARYING.
6392 : : If we value-number a virtual operand never value-number to the
6393 : : value from the backedge as that confuses the alias-walking code.
6394 : : See gcc.dg/torture/pr87176.c. If the value is the same on a
6395 : : non-backedge everything is OK though. */
6396 : 35001582 : bool visited_p;
6397 : 35001582 : if ((backedge_val
6398 : 35001582 : && !seen_non_backedge
6399 : 2565 : && TREE_CODE (backedge_val) == SSA_NAME
6400 : 2298 : && sameval == backedge_val
6401 : 317 : && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6402 : 44 : || SSA_VAL (backedge_val) != backedge_val))
6403 : : /* Do not value-number a virtual operand to sth not visited though
6404 : : given that allows us to escape a region in alias walking. */
6405 : 35003874 : || (sameval
6406 : 7732786 : && TREE_CODE (sameval) == SSA_NAME
6407 : 4626637 : && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6408 : 3920973 : && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6409 : 1953477 : && (SSA_VAL (sameval, &visited_p), !visited_p)))
6410 : : /* Note this just drops to VARYING without inserting the PHI into
6411 : : the hashes. */
6412 : 294880 : result = PHI_RESULT (phi);
6413 : : /* If none of the edges was executable keep the value-number at VN_TOP,
6414 : : if only a single edge is exectuable use its value. */
6415 : 34706702 : else if (n_executable <= 1)
6416 : 6642280 : result = seen_undef ? seen_undef : sameval;
6417 : : /* If we saw only undefined values and VN_TOP use one of the
6418 : : undefined values. */
6419 : 28064422 : else if (sameval == VN_TOP)
6420 : 7254036 : result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6421 : : /* First see if it is equivalent to a phi node in this block. We prefer
6422 : : this as it allows IV elimination - see PRs 66502 and 67167. */
6423 : 28059094 : else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6424 : : {
6425 : 4332540 : if (!inserted
6426 : 67111 : && TREE_CODE (result) == SSA_NAME
6427 : 4399651 : && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6428 : : {
6429 : 67111 : gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6430 : 67111 : if (dump_file && (dump_flags & TDF_DETAILS))
6431 : : {
6432 : 6 : fprintf (dump_file, "Marking CSEd to PHI node ");
6433 : 6 : print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6434 : : 0, TDF_SLIM);
6435 : 6 : fprintf (dump_file, "\n");
6436 : : }
6437 : : }
6438 : : }
6439 : : /* If all values are the same use that, unless we've seen undefined
6440 : : values as well and the value isn't constant.
6441 : : CCP/copyprop have the same restriction to not remove uninit warnings. */
6442 : 23726554 : else if (sameval
6443 : 23726554 : && (! seen_undef || is_gimple_min_invariant (sameval)))
6444 : : result = sameval;
6445 : : else
6446 : : {
6447 : 23077085 : result = PHI_RESULT (phi);
6448 : : /* Only insert PHIs that are varying, for constant value numbers
6449 : : we mess up equivalences otherwise as we are only comparing
6450 : : the immediate controlling predicates. */
6451 : 23077085 : vn_phi_insert (phi, result, backedges_varying_p);
6452 : 23077085 : if (inserted)
6453 : 3304139 : *inserted = true;
6454 : : }
6455 : :
6456 : 35001582 : return set_ssa_val_to (PHI_RESULT (phi), result);
6457 : 35001582 : }
6458 : :
6459 : : /* Try to simplify RHS using equivalences and constant folding. */
6460 : :
6461 : : static tree
6462 : 127450049 : try_to_simplify (gassign *stmt)
6463 : : {
6464 : 127450049 : enum tree_code code = gimple_assign_rhs_code (stmt);
6465 : 127450049 : tree tem;
6466 : :
6467 : : /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6468 : : in this case, there is no point in doing extra work. */
6469 : 127450049 : if (code == SSA_NAME)
6470 : : return NULL_TREE;
6471 : :
6472 : : /* First try constant folding based on our current lattice. */
6473 : 112476343 : mprts_hook = vn_lookup_simplify_result;
6474 : 112476343 : tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6475 : 112476343 : mprts_hook = NULL;
6476 : 112476343 : if (tem
6477 : 112476343 : && (TREE_CODE (tem) == SSA_NAME
6478 : 24795109 : || is_gimple_min_invariant (tem)))
6479 : 24771427 : return tem;
6480 : :
6481 : : return NULL_TREE;
6482 : : }
6483 : :
6484 : : /* Visit and value number STMT, return true if the value number
6485 : : changed. */
6486 : :
6487 : : static bool
6488 : 456913101 : visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6489 : : {
6490 : 456913101 : bool changed = false;
6491 : :
6492 : 456913101 : if (dump_file && (dump_flags & TDF_DETAILS))
6493 : : {
6494 : 390522 : fprintf (dump_file, "Value numbering stmt = ");
6495 : 390522 : print_gimple_stmt (dump_file, stmt, 0);
6496 : : }
6497 : :
6498 : 456913101 : if (gimple_code (stmt) == GIMPLE_PHI)
6499 : 27286351 : changed = visit_phi (stmt, NULL, backedges_varying_p);
6500 : 601771511 : else if (gimple_has_volatile_ops (stmt))
6501 : 8886332 : changed = defs_to_varying (stmt);
6502 : 420740418 : else if (gassign *ass = dyn_cast <gassign *> (stmt))
6503 : : {
6504 : 132544530 : enum tree_code code = gimple_assign_rhs_code (ass);
6505 : 132544530 : tree lhs = gimple_assign_lhs (ass);
6506 : 132544530 : tree rhs1 = gimple_assign_rhs1 (ass);
6507 : 132544530 : tree simplified;
6508 : :
6509 : : /* Shortcut for copies. Simplifying copies is pointless,
6510 : : since we copy the expression and value they represent. */
6511 : 132544530 : if (code == SSA_NAME
6512 : 20068187 : && TREE_CODE (lhs) == SSA_NAME)
6513 : : {
6514 : 5094481 : changed = visit_copy (lhs, rhs1);
6515 : 5094481 : goto done;
6516 : : }
6517 : 127450049 : simplified = try_to_simplify (ass);
6518 : 127450049 : if (simplified)
6519 : : {
6520 : 24771427 : if (dump_file && (dump_flags & TDF_DETAILS))
6521 : : {
6522 : 13792 : fprintf (dump_file, "RHS ");
6523 : 13792 : print_gimple_expr (dump_file, ass, 0);
6524 : 13792 : fprintf (dump_file, " simplified to ");
6525 : 13792 : print_generic_expr (dump_file, simplified);
6526 : 13792 : fprintf (dump_file, "\n");
6527 : : }
6528 : : }
6529 : : /* Setting value numbers to constants will occasionally
6530 : : screw up phi congruence because constants are not
6531 : : uniquely associated with a single ssa name that can be
6532 : : looked up. */
6533 : 24771427 : if (simplified
6534 : 24771427 : && is_gimple_min_invariant (simplified)
6535 : 21900923 : && TREE_CODE (lhs) == SSA_NAME)
6536 : : {
6537 : 7437318 : changed = set_ssa_val_to (lhs, simplified);
6538 : 7437318 : goto done;
6539 : : }
6540 : 120012731 : else if (simplified
6541 : 17334109 : && TREE_CODE (simplified) == SSA_NAME
6542 : 2870504 : && TREE_CODE (lhs) == SSA_NAME)
6543 : : {
6544 : 2870504 : changed = visit_copy (lhs, simplified);
6545 : 2870504 : goto done;
6546 : : }
6547 : :
6548 : 117142227 : if ((TREE_CODE (lhs) == SSA_NAME
6549 : : /* We can substitute SSA_NAMEs that are live over
6550 : : abnormal edges with their constant value. */
6551 : 84061197 : && !(gimple_assign_copy_p (ass)
6552 : 26 : && is_gimple_min_invariant (rhs1))
6553 : 84061171 : && !(simplified
6554 : 0 : && is_gimple_min_invariant (simplified))
6555 : 84061171 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6556 : : /* Stores or copies from SSA_NAMEs that are live over
6557 : : abnormal edges are a problem. */
6558 : 201202114 : || (code == SSA_NAME
6559 : 14973706 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6560 : 1537 : changed = defs_to_varying (ass);
6561 : 117140690 : else if (REFERENCE_CLASS_P (lhs)
6562 : 117140690 : || DECL_P (lhs))
6563 : 33080803 : changed = visit_reference_op_store (lhs, rhs1, ass);
6564 : 84059887 : else if (TREE_CODE (lhs) == SSA_NAME)
6565 : : {
6566 : 84059887 : if ((gimple_assign_copy_p (ass)
6567 : 26 : && is_gimple_min_invariant (rhs1))
6568 : 84059913 : || (simplified
6569 : 0 : && is_gimple_min_invariant (simplified)))
6570 : : {
6571 : 0 : if (simplified)
6572 : 0 : changed = set_ssa_val_to (lhs, simplified);
6573 : : else
6574 : 0 : changed = set_ssa_val_to (lhs, rhs1);
6575 : : }
6576 : : else
6577 : : {
6578 : : /* Visit the original statement. */
6579 : 84059887 : switch (vn_get_stmt_kind (ass))
6580 : : {
6581 : 48954836 : case VN_NARY:
6582 : 48954836 : changed = visit_nary_op (lhs, ass);
6583 : 48954836 : break;
6584 : 35002922 : case VN_REFERENCE:
6585 : 35002922 : changed = visit_reference_op_load (lhs, rhs1, ass);
6586 : 35002922 : break;
6587 : 102129 : default:
6588 : 102129 : changed = defs_to_varying (ass);
6589 : 102129 : break;
6590 : : }
6591 : : }
6592 : : }
6593 : : else
6594 : 0 : changed = defs_to_varying (ass);
6595 : : }
6596 : 288195888 : else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6597 : : {
6598 : 24946054 : tree lhs = gimple_call_lhs (call_stmt);
6599 : 24946054 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6600 : : {
6601 : : /* Try constant folding based on our current lattice. */
6602 : 8395242 : tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6603 : : vn_valueize);
6604 : 8395242 : if (simplified)
6605 : : {
6606 : 60901 : if (dump_file && (dump_flags & TDF_DETAILS))
6607 : : {
6608 : 1 : fprintf (dump_file, "call ");
6609 : 1 : print_gimple_expr (dump_file, call_stmt, 0);
6610 : 1 : fprintf (dump_file, " simplified to ");
6611 : 1 : print_generic_expr (dump_file, simplified);
6612 : 1 : fprintf (dump_file, "\n");
6613 : : }
6614 : : }
6615 : : /* Setting value numbers to constants will occasionally
6616 : : screw up phi congruence because constants are not
6617 : : uniquely associated with a single ssa name that can be
6618 : : looked up. */
6619 : 60901 : if (simplified
6620 : 60901 : && is_gimple_min_invariant (simplified))
6621 : : {
6622 : 54243 : changed = set_ssa_val_to (lhs, simplified);
6623 : 108486 : if (gimple_vdef (call_stmt))
6624 : 769 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6625 : : SSA_VAL (gimple_vuse (call_stmt)));
6626 : 54243 : goto done;
6627 : : }
6628 : 8340999 : else if (simplified
6629 : 6658 : && TREE_CODE (simplified) == SSA_NAME)
6630 : : {
6631 : 301 : changed = visit_copy (lhs, simplified);
6632 : 602 : if (gimple_vdef (call_stmt))
6633 : 0 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6634 : : SSA_VAL (gimple_vuse (call_stmt)));
6635 : 301 : goto done;
6636 : : }
6637 : 8340698 : else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6638 : : {
6639 : 381 : changed = defs_to_varying (call_stmt);
6640 : 381 : goto done;
6641 : : }
6642 : : }
6643 : :
6644 : : /* Pick up flags from a devirtualization target. */
6645 : 24891129 : tree fn = gimple_call_fn (stmt);
6646 : 24891129 : int extra_fnflags = 0;
6647 : 24891129 : if (fn && TREE_CODE (fn) == SSA_NAME)
6648 : : {
6649 : 537211 : fn = SSA_VAL (fn);
6650 : 537211 : if (TREE_CODE (fn) == ADDR_EXPR
6651 : 537211 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6652 : 4840 : extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6653 : : }
6654 : 24891129 : if ((/* Calls to the same function with the same vuse
6655 : : and the same operands do not necessarily return the same
6656 : : value, unless they're pure or const. */
6657 : 24891129 : ((gimple_call_flags (call_stmt) | extra_fnflags)
6658 : 24891129 : & (ECF_PURE | ECF_CONST))
6659 : : /* If calls have a vdef, subsequent calls won't have
6660 : : the same incoming vuse. So, if 2 calls with vdef have the
6661 : : same vuse, we know they're not subsequent.
6662 : : We can value number 2 calls to the same function with the
6663 : : same vuse and the same operands which are not subsequent
6664 : : the same, because there is no code in the program that can
6665 : : compare the 2 values... */
6666 : 20870355 : || (gimple_vdef (call_stmt)
6667 : : /* ... unless the call returns a pointer which does
6668 : : not alias with anything else. In which case the
6669 : : information that the values are distinct are encoded
6670 : : in the IL. */
6671 : 20835495 : && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6672 : : /* Only perform the following when being called from PRE
6673 : : which embeds tail merging. */
6674 : 20361203 : && default_vn_walk_kind == VN_WALK))
6675 : : /* Do not process .DEFERRED_INIT since that confuses uninit
6676 : : analysis. */
6677 : 29886171 : && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6678 : 8671341 : changed = visit_reference_op_call (lhs, call_stmt);
6679 : : else
6680 : 16219788 : changed = defs_to_varying (call_stmt);
6681 : : }
6682 : : else
6683 : 263249834 : changed = defs_to_varying (stmt);
6684 : 456913101 : done:
6685 : 456913101 : return changed;
6686 : : }
6687 : :
6688 : :
6689 : : /* Allocate a value number table. */
6690 : :
6691 : : static void
6692 : 6233625 : allocate_vn_table (vn_tables_t table, unsigned size)
6693 : : {
6694 : 6233625 : table->phis = new vn_phi_table_type (size);
6695 : 6233625 : table->nary = new vn_nary_op_table_type (size);
6696 : 6233625 : table->references = new vn_reference_table_type (size);
6697 : 6233625 : }
6698 : :
6699 : : /* Free a value number table. */
6700 : :
6701 : : static void
6702 : 6233625 : free_vn_table (vn_tables_t table)
6703 : : {
6704 : : /* Walk over elements and release vectors. */
6705 : 6233625 : vn_reference_iterator_type hir;
6706 : 6233625 : vn_reference_t vr;
6707 : 152677007 : FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
6708 : 73221691 : vr->operands.release ();
6709 : 6233625 : delete table->phis;
6710 : 6233625 : table->phis = NULL;
6711 : 6233625 : delete table->nary;
6712 : 6233625 : table->nary = NULL;
6713 : 6233625 : delete table->references;
6714 : 6233625 : table->references = NULL;
6715 : 6233625 : }
6716 : :
6717 : : /* Set *ID according to RESULT. */
6718 : :
6719 : : static void
6720 : 35120293 : set_value_id_for_result (tree result, unsigned int *id)
6721 : : {
6722 : 35120293 : if (result && TREE_CODE (result) == SSA_NAME)
6723 : 21961545 : *id = VN_INFO (result)->value_id;
6724 : 9866618 : else if (result && is_gimple_min_invariant (result))
6725 : 3729193 : *id = get_or_alloc_constant_value_id (result);
6726 : : else
6727 : 9429555 : *id = get_next_value_id ();
6728 : 35120293 : }
6729 : :
6730 : : /* Set the value ids in the valid hash tables. */
6731 : :
6732 : : static void
6733 : 970003 : set_hashtable_value_ids (void)
6734 : : {
6735 : 970003 : vn_nary_op_iterator_type hin;
6736 : 970003 : vn_phi_iterator_type hip;
6737 : 970003 : vn_reference_iterator_type hir;
6738 : 970003 : vn_nary_op_t vno;
6739 : 970003 : vn_reference_t vr;
6740 : 970003 : vn_phi_t vp;
6741 : :
6742 : : /* Now set the value ids of the things we had put in the hash
6743 : : table. */
6744 : :
6745 : 49845823 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
6746 : 24437910 : if (! vno->predicated_values)
6747 : 7903277 : set_value_id_for_result (vno->u.result, &vno->value_id);
6748 : :
6749 : 9245325 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
6750 : 4137661 : set_value_id_for_result (vp->result, &vp->value_id);
6751 : :
6752 : 47128713 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
6753 : : hir)
6754 : 23079355 : set_value_id_for_result (vr->result, &vr->value_id);
6755 : 970003 : }
6756 : :
6757 : : /* Return the maximum value id we have ever seen. */
6758 : :
6759 : : unsigned int
6760 : 1940006 : get_max_value_id (void)
6761 : : {
6762 : 1940006 : return next_value_id;
6763 : : }
6764 : :
6765 : : /* Return the maximum constant value id we have ever seen. */
6766 : :
6767 : : unsigned int
6768 : 1940006 : get_max_constant_value_id (void)
6769 : : {
6770 : 1940006 : return -next_constant_value_id;
6771 : : }
6772 : :
6773 : : /* Return the next unique value id. */
6774 : :
6775 : : unsigned int
6776 : 49683524 : get_next_value_id (void)
6777 : : {
6778 : 49683524 : gcc_checking_assert ((int)next_value_id > 0);
6779 : 49683524 : return next_value_id++;
6780 : : }
6781 : :
6782 : : /* Return the next unique value id for constants. */
6783 : :
6784 : : unsigned int
6785 : 2567091 : get_next_constant_value_id (void)
6786 : : {
6787 : 2567091 : gcc_checking_assert (next_constant_value_id < 0);
6788 : 2567091 : return next_constant_value_id--;
6789 : : }
6790 : :
6791 : :
6792 : : /* Compare two expressions E1 and E2 and return true if they are equal.
6793 : : If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6794 : : otherwise VN_TOP only matches VN_TOP. */
6795 : :
6796 : : bool
6797 : 242103777 : expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
6798 : : {
6799 : : /* The obvious case. */
6800 : 242103777 : if (e1 == e2)
6801 : : return true;
6802 : :
6803 : : /* If either one is VN_TOP consider them equal. */
6804 : 71154738 : if (match_vn_top_optimistically
6805 : 66298115 : && (e1 == VN_TOP || e2 == VN_TOP))
6806 : : return true;
6807 : :
6808 : : /* If only one of them is null, they cannot be equal. While in general
6809 : : this should not happen for operations like TARGET_MEM_REF some
6810 : : operands are optional and an identity value we could substitute
6811 : : has differing semantics. */
6812 : 71154738 : if (!e1 || !e2)
6813 : : return false;
6814 : :
6815 : : /* SSA_NAME compare pointer equal. */
6816 : 71154738 : if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
6817 : : return false;
6818 : :
6819 : : /* Now perform the actual comparison. */
6820 : 35062395 : if (TREE_CODE (e1) == TREE_CODE (e2)
6821 : 35062395 : && operand_equal_p (e1, e2, OEP_PURE_SAME))
6822 : : return true;
6823 : :
6824 : : return false;
6825 : : }
6826 : :
6827 : :
6828 : : /* Return true if the nary operation NARY may trap. This is a copy
6829 : : of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6830 : :
6831 : : bool
6832 : 5574390 : vn_nary_may_trap (vn_nary_op_t nary)
6833 : : {
6834 : 5574390 : tree type;
6835 : 5574390 : tree rhs2 = NULL_TREE;
6836 : 5574390 : bool honor_nans = false;
6837 : 5574390 : bool honor_snans = false;
6838 : 5574390 : bool fp_operation = false;
6839 : 5574390 : bool honor_trapv = false;
6840 : 5574390 : bool handled, ret;
6841 : 5574390 : unsigned i;
6842 : :
6843 : 5574390 : if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
6844 : : || TREE_CODE_CLASS (nary->opcode) == tcc_unary
6845 : 5574390 : || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
6846 : : {
6847 : 5486206 : type = nary->type;
6848 : 5486206 : fp_operation = FLOAT_TYPE_P (type);
6849 : 5486206 : if (fp_operation)
6850 : : {
6851 : 118868 : honor_nans = flag_trapping_math && !flag_finite_math_only;
6852 : 118868 : honor_snans = flag_signaling_nans != 0;
6853 : : }
6854 : 5367338 : else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
6855 : : honor_trapv = true;
6856 : : }
6857 : 5574390 : if (nary->length >= 2)
6858 : 2201299 : rhs2 = nary->op[1];
6859 : 5574390 : ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
6860 : : honor_trapv, honor_nans, honor_snans,
6861 : : rhs2, &handled);
6862 : 5574390 : if (handled && ret)
6863 : : return true;
6864 : :
6865 : 13027256 : for (i = 0; i < nary->length; ++i)
6866 : 7570880 : if (tree_could_trap_p (nary->op[i]))
6867 : : return true;
6868 : :
6869 : : return false;
6870 : : }
6871 : :
6872 : : /* Return true if the reference operation REF may trap. */
6873 : :
6874 : : bool
6875 : 1873072 : vn_reference_may_trap (vn_reference_t ref)
6876 : : {
6877 : 1873072 : switch (ref->operands[0].opcode)
6878 : : {
6879 : : case MODIFY_EXPR:
6880 : : case CALL_EXPR:
6881 : : /* We do not handle calls. */
6882 : : return true;
6883 : : case ADDR_EXPR:
6884 : : /* And toplevel address computations never trap. */
6885 : : return false;
6886 : : default:;
6887 : : }
6888 : :
6889 : : vn_reference_op_t op;
6890 : : unsigned i;
6891 : 4995064 : FOR_EACH_VEC_ELT (ref->operands, i, op)
6892 : : {
6893 : 4994860 : switch (op->opcode)
6894 : : {
6895 : : case WITH_SIZE_EXPR:
6896 : : case TARGET_MEM_REF:
6897 : : /* Always variable. */
6898 : : return true;
6899 : 1243110 : case COMPONENT_REF:
6900 : 1243110 : if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
6901 : : return true;
6902 : : break;
6903 : 0 : case ARRAY_RANGE_REF:
6904 : 0 : if (TREE_CODE (op->op0) == SSA_NAME)
6905 : : return true;
6906 : : break;
6907 : 241945 : case ARRAY_REF:
6908 : 241945 : {
6909 : 241945 : if (TREE_CODE (op->op0) != INTEGER_CST)
6910 : : return true;
6911 : :
6912 : : /* !in_array_bounds */
6913 : 214616 : tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
6914 : 214616 : if (!domain_type)
6915 : : return true;
6916 : :
6917 : 214483 : tree min = op->op1;
6918 : 214483 : tree max = TYPE_MAX_VALUE (domain_type);
6919 : 214483 : if (!min
6920 : 214483 : || !max
6921 : 201207 : || TREE_CODE (min) != INTEGER_CST
6922 : 201207 : || TREE_CODE (max) != INTEGER_CST)
6923 : : return true;
6924 : :
6925 : 198539 : if (tree_int_cst_lt (op->op0, min)
6926 : 198539 : || tree_int_cst_lt (max, op->op0))
6927 : 642 : return true;
6928 : :
6929 : : break;
6930 : : }
6931 : : case MEM_REF:
6932 : : /* Nothing interesting in itself, the base is separate. */
6933 : : break;
6934 : : /* The following are the address bases. */
6935 : : case SSA_NAME:
6936 : : return true;
6937 : 1233750 : case ADDR_EXPR:
6938 : 1233750 : if (op->op0)
6939 : 1233750 : return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
6940 : : return false;
6941 : 3212299 : default:;
6942 : : }
6943 : : }
6944 : : return false;
6945 : : }
6946 : :
6947 : 10583378 : eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
6948 : 10583378 : bitmap inserted_exprs_)
6949 : 10583378 : : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
6950 : 10583378 : el_todo (0), eliminations (0), insertions (0),
6951 : 10583378 : inserted_exprs (inserted_exprs_)
6952 : : {
6953 : 10583378 : need_eh_cleanup = BITMAP_ALLOC (NULL);
6954 : 10583378 : need_ab_cleanup = BITMAP_ALLOC (NULL);
6955 : 10583378 : }
6956 : :
6957 : 10583378 : eliminate_dom_walker::~eliminate_dom_walker ()
6958 : : {
6959 : 10583378 : BITMAP_FREE (need_eh_cleanup);
6960 : 10583378 : BITMAP_FREE (need_ab_cleanup);
6961 : 10583378 : }
6962 : :
6963 : : /* Return a leader for OP that is available at the current point of the
6964 : : eliminate domwalk. */
6965 : :
6966 : : tree
6967 : 182948917 : eliminate_dom_walker::eliminate_avail (basic_block, tree op)
6968 : : {
6969 : 182948917 : tree valnum = VN_INFO (op)->valnum;
6970 : 182948917 : if (TREE_CODE (valnum) == SSA_NAME)
6971 : : {
6972 : 177973834 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6973 : : return valnum;
6974 : 309831163 : if (avail.length () > SSA_NAME_VERSION (valnum))
6975 : : {
6976 : 139600509 : tree av = avail[SSA_NAME_VERSION (valnum)];
6977 : : /* When PRE discovers a new redundancy there's no way to unite
6978 : : the value classes so it instead inserts a copy old-val = new-val.
6979 : : Look through such copies here, providing one more level of
6980 : : simplification at elimination time. */
6981 : 139600509 : gassign *ass;
6982 : 245003338 : if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
6983 : 74549497 : if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
6984 : : {
6985 : 39720790 : tree rhs1 = gimple_assign_rhs1 (ass);
6986 : 39720790 : if (CONSTANT_CLASS_P (rhs1)
6987 : 39720790 : || (TREE_CODE (rhs1) == SSA_NAME
6988 : 8909 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6989 : : av = rhs1;
6990 : : }
6991 : 139600509 : return av;
6992 : : }
6993 : : }
6994 : 4975083 : else if (is_gimple_min_invariant (valnum))
6995 : : return valnum;
6996 : : return NULL_TREE;
6997 : : }
6998 : :
6999 : : /* At the current point of the eliminate domwalk make OP available. */
7000 : :
7001 : : void
7002 : 50862302 : eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
7003 : : {
7004 : 50862302 : tree valnum = VN_INFO (op)->valnum;
7005 : 50862302 : if (TREE_CODE (valnum) == SSA_NAME)
7006 : : {
7007 : 98324874 : if (avail.length () <= SSA_NAME_VERSION (valnum))
7008 : 17015905 : avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
7009 : 50862302 : tree pushop = op;
7010 : 50862302 : if (avail[SSA_NAME_VERSION (valnum)])
7011 : 43334 : pushop = avail[SSA_NAME_VERSION (valnum)];
7012 : 50862302 : avail_stack.safe_push (pushop);
7013 : 50862302 : avail[SSA_NAME_VERSION (valnum)] = op;
7014 : : }
7015 : 50862302 : }
7016 : :
7017 : : /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
7018 : : the leader for the expression if insertion was successful. */
7019 : :
7020 : : tree
7021 : 129393 : eliminate_dom_walker::eliminate_insert (basic_block bb,
7022 : : gimple_stmt_iterator *gsi, tree val)
7023 : : {
7024 : : /* We can insert a sequence with a single assignment only. */
7025 : 129393 : gimple_seq stmts = VN_INFO (val)->expr;
7026 : 129393 : if (!gimple_seq_singleton_p (stmts))
7027 : : return NULL_TREE;
7028 : 236044 : gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
7029 : 129393 : if (!stmt
7030 : 129393 : || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7031 : : && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
7032 : : && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
7033 : : && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
7034 : : && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
7035 : 75 : || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
7036 : : return NULL_TREE;
7037 : :
7038 : 35834 : tree op = gimple_assign_rhs1 (stmt);
7039 : 35834 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
7040 : 35834 : || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7041 : 19202 : op = TREE_OPERAND (op, 0);
7042 : 35834 : tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
7043 : 35788 : if (!leader)
7044 : : return NULL_TREE;
7045 : :
7046 : 22746 : tree res;
7047 : 22746 : stmts = NULL;
7048 : 41049 : if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7049 : 32706 : res = gimple_build (&stmts, BIT_FIELD_REF,
7050 : 16353 : TREE_TYPE (val), leader,
7051 : 16353 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
7052 : 16353 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
7053 : 6393 : else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
7054 : 150 : res = gimple_build (&stmts, BIT_AND_EXPR,
7055 : 75 : TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
7056 : : else
7057 : 6318 : res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
7058 : 6318 : TREE_TYPE (val), leader);
7059 : 22746 : if (TREE_CODE (res) != SSA_NAME
7060 : 22745 : || SSA_NAME_IS_DEFAULT_DEF (res)
7061 : 45491 : || gimple_bb (SSA_NAME_DEF_STMT (res)))
7062 : : {
7063 : 4 : gimple_seq_discard (stmts);
7064 : :
7065 : : /* During propagation we have to treat SSA info conservatively
7066 : : and thus we can end up simplifying the inserted expression
7067 : : at elimination time to sth not defined in stmts. */
7068 : : /* But then this is a redundancy we failed to detect. Which means
7069 : : res now has two values. That doesn't play well with how
7070 : : we track availability here, so give up. */
7071 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
7072 : : {
7073 : 0 : if (TREE_CODE (res) == SSA_NAME)
7074 : 0 : res = eliminate_avail (bb, res);
7075 : 0 : if (res)
7076 : : {
7077 : 0 : fprintf (dump_file, "Failed to insert expression for value ");
7078 : 0 : print_generic_expr (dump_file, val);
7079 : 0 : fprintf (dump_file, " which is really fully redundant to ");
7080 : 0 : print_generic_expr (dump_file, res);
7081 : 0 : fprintf (dump_file, "\n");
7082 : : }
7083 : : }
7084 : :
7085 : 4 : return NULL_TREE;
7086 : : }
7087 : : else
7088 : : {
7089 : 22742 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
7090 : 22742 : vn_ssa_aux_t vn_info = VN_INFO (res);
7091 : 22742 : vn_info->valnum = val;
7092 : 22742 : vn_info->visited = true;
7093 : : }
7094 : :
7095 : 22742 : insertions++;
7096 : 22742 : if (dump_file && (dump_flags & TDF_DETAILS))
7097 : : {
7098 : 501 : fprintf (dump_file, "Inserted ");
7099 : 501 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
7100 : : }
7101 : :
7102 : : return res;
7103 : : }
7104 : :
7105 : : void
7106 : 353039850 : eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
7107 : : {
7108 : 353039850 : tree sprime = NULL_TREE;
7109 : 353039850 : gimple *stmt = gsi_stmt (*gsi);
7110 : 353039850 : tree lhs = gimple_get_lhs (stmt);
7111 : 120859786 : if (lhs && TREE_CODE (lhs) == SSA_NAME
7112 : 166884856 : && !gimple_has_volatile_ops (stmt)
7113 : : /* See PR43491. Do not replace a global register variable when
7114 : : it is a the RHS of an assignment. Do replace local register
7115 : : variables since gcc does not guarantee a local variable will
7116 : : be allocated in register.
7117 : : ??? The fix isn't effective here. This should instead
7118 : : be ensured by not value-numbering them the same but treating
7119 : : them like volatiles? */
7120 : 435422457 : && !(gimple_assign_single_p (stmt)
7121 : 35770410 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
7122 : 2534350 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
7123 : 4172 : && is_global_var (gimple_assign_rhs1 (stmt)))))
7124 : : {
7125 : 82382363 : sprime = eliminate_avail (b, lhs);
7126 : 82382363 : if (!sprime)
7127 : : {
7128 : : /* If there is no existing usable leader but SCCVN thinks
7129 : : it has an expression it wants to use as replacement,
7130 : : insert that. */
7131 : 69543284 : tree val = VN_INFO (lhs)->valnum;
7132 : 69543284 : vn_ssa_aux_t vn_info;
7133 : 69543284 : if (val != VN_TOP
7134 : 69543284 : && TREE_CODE (val) == SSA_NAME
7135 : 69543284 : && (vn_info = VN_INFO (val), true)
7136 : 69543284 : && vn_info->needs_insertion
7137 : 328430 : && vn_info->expr != NULL
7138 : 69672677 : && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
7139 : 22742 : eliminate_push_avail (b, sprime);
7140 : : }
7141 : :
7142 : : /* If this now constitutes a copy duplicate points-to
7143 : : and range info appropriately. This is especially
7144 : : important for inserted code. */
7145 : 69543284 : if (sprime
7146 : 12861821 : && TREE_CODE (sprime) == SSA_NAME)
7147 : 8885744 : maybe_duplicate_ssa_info_at_copy (lhs, sprime);
7148 : :
7149 : : /* Inhibit the use of an inserted PHI on a loop header when
7150 : : the address of the memory reference is a simple induction
7151 : : variable. In other cases the vectorizer won't do anything
7152 : : anyway (either it's loop invariant or a complicated
7153 : : expression). */
7154 : 8885744 : if (sprime
7155 : 12861821 : && TREE_CODE (sprime) == SSA_NAME
7156 : 8885744 : && do_pre
7157 : 958905 : && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
7158 : 940574 : && loop_outer (b->loop_father)
7159 : 380688 : && has_zero_uses (sprime)
7160 : 184540 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
7161 : 184385 : && gimple_assign_load_p (stmt))
7162 : : {
7163 : 100220 : gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
7164 : 100220 : basic_block def_bb = gimple_bb (def_stmt);
7165 : 100220 : if (gimple_code (def_stmt) == GIMPLE_PHI
7166 : 100220 : && def_bb->loop_father->header == def_bb)
7167 : : {
7168 : 62380 : loop_p loop = def_bb->loop_father;
7169 : 62380 : ssa_op_iter iter;
7170 : 62380 : tree op;
7171 : 62380 : bool found = false;
7172 : 78359 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
7173 : : {
7174 : 59271 : affine_iv iv;
7175 : 59271 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
7176 : 59271 : if (def_bb
7177 : 54126 : && flow_bb_inside_loop_p (loop, def_bb)
7178 : 108517 : && simple_iv (loop, loop, op, &iv, true))
7179 : : {
7180 : 43292 : found = true;
7181 : 43292 : break;
7182 : : }
7183 : : }
7184 : 19088 : if (found)
7185 : : {
7186 : 43292 : if (dump_file && (dump_flags & TDF_DETAILS))
7187 : : {
7188 : 3 : fprintf (dump_file, "Not replacing ");
7189 : 3 : print_gimple_expr (dump_file, stmt, 0);
7190 : 3 : fprintf (dump_file, " with ");
7191 : 3 : print_generic_expr (dump_file, sprime);
7192 : 3 : fprintf (dump_file, " which would add a loop"
7193 : : " carried dependence to loop %d\n",
7194 : : loop->num);
7195 : : }
7196 : : /* Don't keep sprime available. */
7197 : 43292 : sprime = NULL_TREE;
7198 : : }
7199 : : }
7200 : : }
7201 : :
7202 : 82382363 : if (sprime)
7203 : : {
7204 : : /* If we can propagate the value computed for LHS into
7205 : : all uses don't bother doing anything with this stmt. */
7206 : 12818529 : if (may_propagate_copy (lhs, sprime))
7207 : : {
7208 : : /* Mark it for removal. */
7209 : 12816638 : to_remove.safe_push (stmt);
7210 : :
7211 : : /* ??? Don't count copy/constant propagations. */
7212 : 12816638 : if (gimple_assign_single_p (stmt)
7213 : 12816638 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7214 : 4505327 : || gimple_assign_rhs1 (stmt) == sprime))
7215 : 13602304 : return;
7216 : :
7217 : 7680242 : if (dump_file && (dump_flags & TDF_DETAILS))
7218 : : {
7219 : 17923 : fprintf (dump_file, "Replaced ");
7220 : 17923 : print_gimple_expr (dump_file, stmt, 0);
7221 : 17923 : fprintf (dump_file, " with ");
7222 : 17923 : print_generic_expr (dump_file, sprime);
7223 : 17923 : fprintf (dump_file, " in all uses of ");
7224 : 17923 : print_gimple_stmt (dump_file, stmt, 0);
7225 : : }
7226 : :
7227 : 7680242 : eliminations++;
7228 : 7680242 : return;
7229 : : }
7230 : :
7231 : : /* If this is an assignment from our leader (which
7232 : : happens in the case the value-number is a constant)
7233 : : then there is nothing to do. Likewise if we run into
7234 : : inserted code that needed a conversion because of
7235 : : our type-agnostic value-numbering of loads. */
7236 : 1891 : if ((gimple_assign_single_p (stmt)
7237 : 1 : || (is_gimple_assign (stmt)
7238 : 1 : && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7239 : 0 : || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
7240 : 1892 : && sprime == gimple_assign_rhs1 (stmt))
7241 : : return;
7242 : :
7243 : : /* Else replace its RHS. */
7244 : 718 : if (dump_file && (dump_flags & TDF_DETAILS))
7245 : : {
7246 : 0 : fprintf (dump_file, "Replaced ");
7247 : 0 : print_gimple_expr (dump_file, stmt, 0);
7248 : 0 : fprintf (dump_file, " with ");
7249 : 0 : print_generic_expr (dump_file, sprime);
7250 : 0 : fprintf (dump_file, " in ");
7251 : 0 : print_gimple_stmt (dump_file, stmt, 0);
7252 : : }
7253 : 718 : eliminations++;
7254 : :
7255 : 718 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
7256 : 718 : && stmt_can_make_abnormal_goto (stmt));
7257 : 718 : gimple *orig_stmt = stmt;
7258 : 718 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
7259 : 718 : TREE_TYPE (sprime)))
7260 : : {
7261 : : /* We preserve conversions to but not from function or method
7262 : : types. This asymmetry makes it necessary to re-instantiate
7263 : : conversions here. */
7264 : 716 : if (POINTER_TYPE_P (TREE_TYPE (lhs))
7265 : 716 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7266 : 716 : sprime = fold_convert (TREE_TYPE (lhs), sprime);
7267 : : else
7268 : 0 : gcc_unreachable ();
7269 : : }
7270 : 718 : tree vdef = gimple_vdef (stmt);
7271 : 718 : tree vuse = gimple_vuse (stmt);
7272 : 718 : propagate_tree_value_into_stmt (gsi, sprime);
7273 : 718 : stmt = gsi_stmt (*gsi);
7274 : 718 : update_stmt (stmt);
7275 : : /* In case the VDEF on the original stmt was released, value-number
7276 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7277 : : released virtual operands. */
7278 : 1436 : if (vdef != gimple_vdef (stmt))
7279 : : {
7280 : 0 : gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7281 : 0 : VN_INFO (vdef)->valnum = vuse;
7282 : : }
7283 : :
7284 : : /* If we removed EH side-effects from the statement, clean
7285 : : its EH information. */
7286 : 718 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7287 : : {
7288 : 0 : bitmap_set_bit (need_eh_cleanup,
7289 : 0 : gimple_bb (stmt)->index);
7290 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7291 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7292 : : }
7293 : :
7294 : : /* Likewise for AB side-effects. */
7295 : 718 : if (can_make_abnormal_goto
7296 : 718 : && !stmt_can_make_abnormal_goto (stmt))
7297 : : {
7298 : 0 : bitmap_set_bit (need_ab_cleanup,
7299 : 0 : gimple_bb (stmt)->index);
7300 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7301 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7302 : : }
7303 : :
7304 : 718 : return;
7305 : : }
7306 : : }
7307 : :
7308 : : /* If the statement is a scalar store, see if the expression
7309 : : has the same value number as its rhs. If so, the store is
7310 : : dead. */
7311 : 340221321 : if (gimple_assign_single_p (stmt)
7312 : 128169916 : && !gimple_has_volatile_ops (stmt)
7313 : 55810871 : && !is_gimple_reg (gimple_assign_lhs (stmt))
7314 : 28646089 : && (TREE_CODE (gimple_assign_lhs (stmt)) != VAR_DECL
7315 : 2840417 : || !DECL_HARD_REGISTER (gimple_assign_lhs (stmt)))
7316 : 368863401 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7317 : 16410235 : || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7318 : : {
7319 : 25522454 : tree rhs = gimple_assign_rhs1 (stmt);
7320 : 25522454 : vn_reference_t vnresult;
7321 : : /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7322 : : typed load of a byte known to be 0x11 as 1 so a store of
7323 : : a boolean 1 is detected as redundant. Because of this we
7324 : : have to make sure to lookup with a ref where its size
7325 : : matches the precision. */
7326 : 25522454 : tree lookup_lhs = lhs;
7327 : 50772291 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7328 : 13309250 : && (TREE_CODE (lhs) != COMPONENT_REF
7329 : 8090505 : || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7330 : 38552285 : && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7331 : : {
7332 : 435886 : if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
7333 : 444921 : && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7334 : : lookup_lhs = NULL_TREE;
7335 : 429041 : else if (TREE_CODE (lhs) == COMPONENT_REF
7336 : 429041 : || TREE_CODE (lhs) == MEM_REF)
7337 : : {
7338 : 299763 : tree ltype = build_nonstandard_integer_type
7339 : 299763 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7340 : 299763 : TYPE_UNSIGNED (TREE_TYPE (lhs)));
7341 : 299763 : if (TREE_CODE (lhs) == COMPONENT_REF)
7342 : : {
7343 : 226583 : tree foff = component_ref_field_offset (lhs);
7344 : 226583 : tree f = TREE_OPERAND (lhs, 1);
7345 : 226583 : if (!poly_int_tree_p (foff))
7346 : : lookup_lhs = NULL_TREE;
7347 : : else
7348 : 453166 : lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7349 : 226583 : TREE_OPERAND (lhs, 0),
7350 : 226583 : TYPE_SIZE (TREE_TYPE (lhs)),
7351 : : bit_from_pos
7352 : 226583 : (foff, DECL_FIELD_BIT_OFFSET (f)));
7353 : : }
7354 : : else
7355 : 73180 : lookup_lhs = build2 (MEM_REF, ltype,
7356 : 73180 : TREE_OPERAND (lhs, 0),
7357 : 73180 : TREE_OPERAND (lhs, 1));
7358 : : }
7359 : : else
7360 : : lookup_lhs = NULL_TREE;
7361 : : }
7362 : 25386331 : tree val = NULL_TREE, tem;
7363 : 25386331 : if (lookup_lhs)
7364 : 50772662 : val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7365 : : VN_WALKREWRITE, &vnresult, false,
7366 : : NULL, NULL_TREE, true);
7367 : 25522454 : if (TREE_CODE (rhs) == SSA_NAME)
7368 : 12231845 : rhs = VN_INFO (rhs)->valnum;
7369 : 25522454 : gassign *ass;
7370 : 25522454 : if (val
7371 : 25522454 : && (operand_equal_p (val, rhs, 0)
7372 : : /* Due to the bitfield lookups above we can get bit
7373 : : interpretations of the same RHS as values here. Those
7374 : : are redundant as well. */
7375 : 3173168 : || (TREE_CODE (val) == SSA_NAME
7376 : 1956924 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7377 : 1772573 : && (tem = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7378 : 1772573 : && TREE_CODE (tem) == VIEW_CONVERT_EXPR
7379 : 3795 : && TREE_OPERAND (tem, 0) == rhs)
7380 : 3173158 : || (TREE_CODE (rhs) == SSA_NAME
7381 : 26008707 : && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs)))
7382 : 1543127 : && gimple_assign_rhs1 (ass) == val
7383 : 714523 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass))
7384 : 9 : && tree_nop_conversion_p (TREE_TYPE (rhs), TREE_TYPE (val)))))
7385 : : {
7386 : : /* We can only remove the later store if the former aliases
7387 : : at least all accesses the later one does or if the store
7388 : : was to readonly memory storing the same value. */
7389 : 237052 : ao_ref lhs_ref;
7390 : 237052 : ao_ref_init (&lhs_ref, lhs);
7391 : 237052 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
7392 : 237052 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7393 : 237052 : if (! vnresult
7394 : 237052 : || ((vnresult->set == set
7395 : 43258 : || alias_set_subset_of (set, vnresult->set))
7396 : 231098 : && (vnresult->base_set == base_set
7397 : 22919 : || alias_set_subset_of (base_set, vnresult->base_set))))
7398 : : {
7399 : 228270 : if (dump_file && (dump_flags & TDF_DETAILS))
7400 : : {
7401 : 17 : fprintf (dump_file, "Deleted redundant store ");
7402 : 17 : print_gimple_stmt (dump_file, stmt, 0);
7403 : : }
7404 : :
7405 : : /* Queue stmt for removal. */
7406 : 228270 : to_remove.safe_push (stmt);
7407 : 228270 : return;
7408 : : }
7409 : : }
7410 : : }
7411 : :
7412 : : /* If this is a control statement value numbering left edges
7413 : : unexecuted on force the condition in a way consistent with
7414 : : that. */
7415 : 339993051 : if (gcond *cond = dyn_cast <gcond *> (stmt))
7416 : : {
7417 : 19130845 : if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7418 : 19130845 : ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7419 : : {
7420 : 555505 : if (dump_file && (dump_flags & TDF_DETAILS))
7421 : : {
7422 : 15 : fprintf (dump_file, "Removing unexecutable edge from ");
7423 : 15 : print_gimple_stmt (dump_file, stmt, 0);
7424 : : }
7425 : 555505 : if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7426 : 555505 : == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7427 : 223421 : gimple_cond_make_true (cond);
7428 : : else
7429 : 332084 : gimple_cond_make_false (cond);
7430 : 555505 : update_stmt (cond);
7431 : 555505 : el_todo |= TODO_cleanup_cfg;
7432 : 555505 : return;
7433 : : }
7434 : : }
7435 : :
7436 : 339437546 : bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7437 : 339437546 : bool was_noreturn = (is_gimple_call (stmt)
7438 : 339437546 : && gimple_call_noreturn_p (stmt));
7439 : 339437546 : tree vdef = gimple_vdef (stmt);
7440 : 339437546 : tree vuse = gimple_vuse (stmt);
7441 : :
7442 : : /* If we didn't replace the whole stmt (or propagate the result
7443 : : into all uses), replace all uses on this stmt with their
7444 : : leaders. */
7445 : 339437546 : bool modified = false;
7446 : 339437546 : use_operand_p use_p;
7447 : 339437546 : ssa_op_iter iter;
7448 : 504026637 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7449 : : {
7450 : 164589091 : tree use = USE_FROM_PTR (use_p);
7451 : : /* ??? The call code above leaves stmt operands un-updated. */
7452 : 164589091 : if (TREE_CODE (use) != SSA_NAME)
7453 : 0 : continue;
7454 : 164589091 : tree sprime;
7455 : 164589091 : if (SSA_NAME_IS_DEFAULT_DEF (use))
7456 : : /* ??? For default defs BB shouldn't matter, but we have to
7457 : : solve the inconsistency between rpo eliminate and
7458 : : dom eliminate avail valueization first. */
7459 : 26677214 : sprime = eliminate_avail (b, use);
7460 : : else
7461 : : /* Look for sth available at the definition block of the argument.
7462 : : This avoids inconsistencies between availability there which
7463 : : decides if the stmt can be removed and availability at the
7464 : : use site. The SSA property ensures that things available
7465 : : at the definition are also available at uses. */
7466 : 137911877 : sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7467 : 164589091 : if (sprime && sprime != use
7468 : 12299217 : && may_propagate_copy (use, sprime, true)
7469 : : /* We substitute into debug stmts to avoid excessive
7470 : : debug temporaries created by removed stmts, but we need
7471 : : to avoid doing so for inserted sprimes as we never want
7472 : : to create debug temporaries for them. */
7473 : 176887592 : && (!inserted_exprs
7474 : 1212484 : || TREE_CODE (sprime) != SSA_NAME
7475 : 1187514 : || !is_gimple_debug (stmt)
7476 : 352200 : || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7477 : : {
7478 : 11978920 : propagate_value (use_p, sprime);
7479 : 11978920 : modified = true;
7480 : : }
7481 : : }
7482 : :
7483 : : /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7484 : : into which is a requirement for the IPA devirt machinery. */
7485 : 339437546 : gimple *old_stmt = stmt;
7486 : 339437546 : if (modified)
7487 : : {
7488 : : /* If a formerly non-invariant ADDR_EXPR is turned into an
7489 : : invariant one it was on a separate stmt. */
7490 : 11114955 : if (gimple_assign_single_p (stmt)
7491 : 11114955 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7492 : 246723 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7493 : 11114955 : gimple_stmt_iterator prev = *gsi;
7494 : 11114955 : gsi_prev (&prev);
7495 : 11114955 : if (fold_stmt (gsi, follow_all_ssa_edges))
7496 : : {
7497 : : /* fold_stmt may have created new stmts inbetween
7498 : : the previous stmt and the folded stmt. Mark
7499 : : all defs created there as varying to not confuse
7500 : : the SCCVN machinery as we're using that even during
7501 : : elimination. */
7502 : 971059 : if (gsi_end_p (prev))
7503 : 221188 : prev = gsi_start_bb (b);
7504 : : else
7505 : 860465 : gsi_next (&prev);
7506 : 971059 : if (gsi_stmt (prev) != gsi_stmt (*gsi))
7507 : 85337 : do
7508 : : {
7509 : 53084 : tree def;
7510 : 53084 : ssa_op_iter dit;
7511 : 102392 : FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7512 : : dit, SSA_OP_ALL_DEFS)
7513 : : /* As existing DEFs may move between stmts
7514 : : only process new ones. */
7515 : 49308 : if (! has_VN_INFO (def))
7516 : : {
7517 : 32151 : vn_ssa_aux_t vn_info = VN_INFO (def);
7518 : 32151 : vn_info->valnum = def;
7519 : 32151 : vn_info->visited = true;
7520 : : }
7521 : 53084 : if (gsi_stmt (prev) == gsi_stmt (*gsi))
7522 : : break;
7523 : 32253 : gsi_next (&prev);
7524 : 32253 : }
7525 : : while (1);
7526 : : }
7527 : 11114955 : stmt = gsi_stmt (*gsi);
7528 : : /* In case we folded the stmt away schedule the NOP for removal. */
7529 : 11114955 : if (gimple_nop_p (stmt))
7530 : 809 : to_remove.safe_push (stmt);
7531 : : }
7532 : :
7533 : : /* Visit indirect calls and turn them into direct calls if
7534 : : possible using the devirtualization machinery. Do this before
7535 : : checking for required EH/abnormal/noreturn cleanup as devird
7536 : : may expose more of those. */
7537 : 339437546 : if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7538 : : {
7539 : 22456347 : tree fn = gimple_call_fn (call_stmt);
7540 : 22456347 : if (fn
7541 : 21591865 : && flag_devirtualize
7542 : 43314835 : && virtual_method_call_p (fn))
7543 : : {
7544 : 215675 : tree otr_type = obj_type_ref_class (fn);
7545 : 215675 : unsigned HOST_WIDE_INT otr_tok
7546 : 215675 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7547 : 215675 : tree instance;
7548 : 215675 : ipa_polymorphic_call_context context (current_function_decl,
7549 : 215675 : fn, stmt, &instance);
7550 : 215675 : context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7551 : : otr_type, stmt, NULL);
7552 : 215675 : bool final;
7553 : 215675 : vec <cgraph_node *> targets
7554 : 215675 : = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7555 : : otr_tok, context, &final);
7556 : 215675 : if (dump_file)
7557 : 22 : dump_possible_polymorphic_call_targets (dump_file,
7558 : : obj_type_ref_class (fn),
7559 : : otr_tok, context);
7560 : 215939 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
7561 : : {
7562 : 63 : tree fn;
7563 : 63 : if (targets.length () == 1)
7564 : 63 : fn = targets[0]->decl;
7565 : : else
7566 : 0 : fn = builtin_decl_unreachable ();
7567 : 63 : if (dump_enabled_p ())
7568 : : {
7569 : 9 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7570 : : "converting indirect call to "
7571 : : "function %s\n",
7572 : 9 : lang_hooks.decl_printable_name (fn, 2));
7573 : : }
7574 : 63 : gimple_call_set_fndecl (call_stmt, fn);
7575 : : /* If changing the call to __builtin_unreachable
7576 : : or similar noreturn function, adjust gimple_call_fntype
7577 : : too. */
7578 : 63 : if (gimple_call_noreturn_p (call_stmt)
7579 : 0 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7580 : 0 : && TYPE_ARG_TYPES (TREE_TYPE (fn))
7581 : 63 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7582 : 0 : == void_type_node))
7583 : 0 : gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7584 : 63 : maybe_remove_unused_call_args (cfun, call_stmt);
7585 : 63 : modified = true;
7586 : : }
7587 : : }
7588 : : }
7589 : :
7590 : 339437546 : if (modified)
7591 : : {
7592 : : /* When changing a call into a noreturn call, cfg cleanup
7593 : : is needed to fix up the noreturn call. */
7594 : 11114976 : if (!was_noreturn
7595 : 11114976 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7596 : 56 : to_fixup.safe_push (stmt);
7597 : : /* When changing a condition or switch into one we know what
7598 : : edge will be executed, schedule a cfg cleanup. */
7599 : 11114976 : if ((gimple_code (stmt) == GIMPLE_COND
7600 : 1497953 : && (gimple_cond_true_p (as_a <gcond *> (stmt))
7601 : 1490987 : || gimple_cond_false_p (as_a <gcond *> (stmt))))
7602 : 12604008 : || (gimple_code (stmt) == GIMPLE_SWITCH
7603 : 8866 : && TREE_CODE (gimple_switch_index
7604 : : (as_a <gswitch *> (stmt))) == INTEGER_CST))
7605 : 10780 : el_todo |= TODO_cleanup_cfg;
7606 : : /* If we removed EH side-effects from the statement, clean
7607 : : its EH information. */
7608 : 11114976 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7609 : : {
7610 : 1605 : bitmap_set_bit (need_eh_cleanup,
7611 : 1605 : gimple_bb (stmt)->index);
7612 : 1605 : if (dump_file && (dump_flags & TDF_DETAILS))
7613 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7614 : : }
7615 : : /* Likewise for AB side-effects. */
7616 : 11114976 : if (can_make_abnormal_goto
7617 : 11114976 : && !stmt_can_make_abnormal_goto (stmt))
7618 : : {
7619 : 0 : bitmap_set_bit (need_ab_cleanup,
7620 : 0 : gimple_bb (stmt)->index);
7621 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7622 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7623 : : }
7624 : 11114976 : update_stmt (stmt);
7625 : : /* In case the VDEF on the original stmt was released, value-number
7626 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7627 : : released virtual operands. */
7628 : 14263526 : if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7629 : 1835 : VN_INFO (vdef)->valnum = vuse;
7630 : : }
7631 : :
7632 : : /* Make new values available - for fully redundant LHS we
7633 : : continue with the next stmt above and skip this.
7634 : : But avoid picking up dead defs. */
7635 : 339437546 : tree def;
7636 : 410313056 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7637 : 70875510 : if (! has_zero_uses (def)
7638 : 70875510 : || (inserted_exprs
7639 : 215902 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7640 : 69465462 : eliminate_push_avail (b, def);
7641 : : }
7642 : :
7643 : : /* Perform elimination for the basic-block B during the domwalk. */
7644 : :
7645 : : edge
7646 : 41980400 : eliminate_dom_walker::before_dom_children (basic_block b)
7647 : : {
7648 : : /* Mark new bb. */
7649 : 41980400 : avail_stack.safe_push (NULL_TREE);
7650 : :
7651 : : /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7652 : 41980400 : if (!(b->flags & BB_EXECUTABLE))
7653 : : return NULL;
7654 : :
7655 : 37179284 : vn_context_bb = b;
7656 : :
7657 : 48979072 : for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7658 : : {
7659 : 11799788 : gphi *phi = gsi.phi ();
7660 : 11799788 : tree res = PHI_RESULT (phi);
7661 : :
7662 : 23599576 : if (virtual_operand_p (res))
7663 : : {
7664 : 5423852 : gsi_next (&gsi);
7665 : 5423852 : continue;
7666 : : }
7667 : :
7668 : 6375936 : tree sprime = eliminate_avail (b, res);
7669 : 6375936 : if (sprime
7670 : 6375936 : && sprime != res)
7671 : : {
7672 : 436067 : if (dump_file && (dump_flags & TDF_DETAILS))
7673 : : {
7674 : 22 : fprintf (dump_file, "Replaced redundant PHI node defining ");
7675 : 22 : print_generic_expr (dump_file, res);
7676 : 22 : fprintf (dump_file, " with ");
7677 : 22 : print_generic_expr (dump_file, sprime);
7678 : 22 : fprintf (dump_file, "\n");
7679 : : }
7680 : :
7681 : : /* If we inserted this PHI node ourself, it's not an elimination. */
7682 : 436067 : if (! inserted_exprs
7683 : 567999 : || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
7684 : 406436 : eliminations++;
7685 : :
7686 : : /* If we will propagate into all uses don't bother to do
7687 : : anything. */
7688 : 436067 : if (may_propagate_copy (res, sprime))
7689 : : {
7690 : : /* Mark the PHI for removal. */
7691 : 436067 : to_remove.safe_push (phi);
7692 : 436067 : gsi_next (&gsi);
7693 : 436067 : continue;
7694 : : }
7695 : :
7696 : 0 : remove_phi_node (&gsi, false);
7697 : :
7698 : 0 : if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
7699 : 0 : sprime = fold_convert (TREE_TYPE (res), sprime);
7700 : 0 : gimple *stmt = gimple_build_assign (res, sprime);
7701 : 0 : gimple_stmt_iterator gsi2 = gsi_after_labels (b);
7702 : 0 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
7703 : 0 : continue;
7704 : 0 : }
7705 : :
7706 : 5939869 : eliminate_push_avail (b, res);
7707 : 5939869 : gsi_next (&gsi);
7708 : : }
7709 : :
7710 : 74358568 : for (gimple_stmt_iterator gsi = gsi_start_bb (b);
7711 : 283996117 : !gsi_end_p (gsi);
7712 : 246816833 : gsi_next (&gsi))
7713 : 246816833 : eliminate_stmt (b, &gsi);
7714 : :
7715 : : /* Replace destination PHI arguments. */
7716 : 37179284 : edge_iterator ei;
7717 : 37179284 : edge e;
7718 : 87830502 : FOR_EACH_EDGE (e, ei, b->succs)
7719 : 50651218 : if (e->flags & EDGE_EXECUTABLE)
7720 : 50159038 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
7721 : 80729817 : !gsi_end_p (gsi);
7722 : 30570779 : gsi_next (&gsi))
7723 : : {
7724 : 30570779 : gphi *phi = gsi.phi ();
7725 : 30570779 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7726 : 30570779 : tree arg = USE_FROM_PTR (use_p);
7727 : 50520356 : if (TREE_CODE (arg) != SSA_NAME
7728 : 30570779 : || virtual_operand_p (arg))
7729 : 19949577 : continue;
7730 : 10621202 : tree sprime = eliminate_avail (b, arg);
7731 : 21242404 : if (sprime && may_propagate_copy (arg, sprime,
7732 : 10621202 : !(e->flags & EDGE_ABNORMAL)))
7733 : 10610235 : propagate_value (use_p, sprime);
7734 : : }
7735 : :
7736 : 37179284 : vn_context_bb = NULL;
7737 : :
7738 : 37179284 : return NULL;
7739 : : }
7740 : :
7741 : : /* Make no longer available leaders no longer available. */
7742 : :
7743 : : void
7744 : 41980400 : eliminate_dom_walker::after_dom_children (basic_block)
7745 : : {
7746 : 41980400 : tree entry;
7747 : 92842702 : while ((entry = avail_stack.pop ()) != NULL_TREE)
7748 : : {
7749 : 50862302 : tree valnum = VN_INFO (entry)->valnum;
7750 : 50862302 : tree old = avail[SSA_NAME_VERSION (valnum)];
7751 : 50862302 : if (old == entry)
7752 : 50818968 : avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
7753 : : else
7754 : 43334 : avail[SSA_NAME_VERSION (valnum)] = entry;
7755 : : }
7756 : 41980400 : }
7757 : :
7758 : : /* Remove queued stmts and perform delayed cleanups. */
7759 : :
7760 : : unsigned
7761 : 6214244 : eliminate_dom_walker::eliminate_cleanup (bool region_p)
7762 : : {
7763 : 6214244 : statistics_counter_event (cfun, "Eliminated", eliminations);
7764 : 6214244 : statistics_counter_event (cfun, "Insertions", insertions);
7765 : :
7766 : : /* We cannot remove stmts during BB walk, especially not release SSA
7767 : : names there as this confuses the VN machinery. The stmts ending
7768 : : up in to_remove are either stores or simple copies.
7769 : : Remove stmts in reverse order to make debug stmt creation possible. */
7770 : 33392148 : while (!to_remove.is_empty ())
7771 : : {
7772 : 14749360 : bool do_release_defs = true;
7773 : 14749360 : gimple *stmt = to_remove.pop ();
7774 : :
7775 : : /* When we are value-numbering a region we do not require exit PHIs to
7776 : : be present so we have to make sure to deal with uses outside of the
7777 : : region of stmts that we thought are eliminated.
7778 : : ??? Note we may be confused by uses in dead regions we didn't run
7779 : : elimination on. Rather than checking individual uses we accept
7780 : : dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7781 : : contains such example). */
7782 : 14749360 : if (region_p)
7783 : : {
7784 : 1691450 : if (gphi *phi = dyn_cast <gphi *> (stmt))
7785 : : {
7786 : 1098368 : tree lhs = gimple_phi_result (phi);
7787 : 1098368 : if (!has_zero_uses (lhs))
7788 : : {
7789 : 21935 : if (dump_file && (dump_flags & TDF_DETAILS))
7790 : 3 : fprintf (dump_file, "Keeping eliminated stmt live "
7791 : : "as copy because of out-of-region uses\n");
7792 : 21935 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7793 : 21935 : gimple *copy = gimple_build_assign (lhs, sprime);
7794 : 21935 : gimple_stmt_iterator gsi
7795 : 21935 : = gsi_after_labels (gimple_bb (stmt));
7796 : 21935 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7797 : 21935 : do_release_defs = false;
7798 : : }
7799 : : }
7800 : 593082 : else if (tree lhs = gimple_get_lhs (stmt))
7801 : 593082 : if (TREE_CODE (lhs) == SSA_NAME
7802 : 593082 : && !has_zero_uses (lhs))
7803 : : {
7804 : 1574 : if (dump_file && (dump_flags & TDF_DETAILS))
7805 : 0 : fprintf (dump_file, "Keeping eliminated stmt live "
7806 : : "as copy because of out-of-region uses\n");
7807 : 1574 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7808 : 1574 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7809 : 1574 : if (is_gimple_assign (stmt))
7810 : : {
7811 : 1574 : gimple_assign_set_rhs_from_tree (&gsi, sprime);
7812 : 1574 : stmt = gsi_stmt (gsi);
7813 : 1574 : update_stmt (stmt);
7814 : 1574 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
7815 : 0 : bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
7816 : 1574 : continue;
7817 : : }
7818 : : else
7819 : : {
7820 : 0 : gimple *copy = gimple_build_assign (lhs, sprime);
7821 : 0 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7822 : 0 : do_release_defs = false;
7823 : : }
7824 : : }
7825 : : }
7826 : :
7827 : 14747786 : if (dump_file && (dump_flags & TDF_DETAILS))
7828 : : {
7829 : 20886 : fprintf (dump_file, "Removing dead stmt ");
7830 : 20886 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
7831 : : }
7832 : :
7833 : 14747786 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7834 : 14747786 : if (gimple_code (stmt) == GIMPLE_PHI)
7835 : 1703643 : remove_phi_node (&gsi, do_release_defs);
7836 : : else
7837 : : {
7838 : 13044143 : basic_block bb = gimple_bb (stmt);
7839 : 13044143 : unlink_stmt_vdef (stmt);
7840 : 13044143 : if (gsi_remove (&gsi, true))
7841 : 26680 : bitmap_set_bit (need_eh_cleanup, bb->index);
7842 : 13044143 : if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
7843 : 2 : bitmap_set_bit (need_ab_cleanup, bb->index);
7844 : 13044143 : if (do_release_defs)
7845 : 13044143 : release_defs (stmt);
7846 : : }
7847 : :
7848 : : /* Removing a stmt may expose a forwarder block. */
7849 : 14747786 : el_todo |= TODO_cleanup_cfg;
7850 : : }
7851 : :
7852 : : /* Fixup stmts that became noreturn calls. This may require splitting
7853 : : blocks and thus isn't possible during the dominator walk. Do this
7854 : : in reverse order so we don't inadvertedly remove a stmt we want to
7855 : : fixup by visiting a dominating now noreturn call first. */
7856 : 6214300 : while (!to_fixup.is_empty ())
7857 : : {
7858 : 56 : gimple *stmt = to_fixup.pop ();
7859 : :
7860 : 56 : if (dump_file && (dump_flags & TDF_DETAILS))
7861 : : {
7862 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
7863 : 0 : print_gimple_stmt (dump_file, stmt, 0);
7864 : : }
7865 : :
7866 : 56 : if (fixup_noreturn_call (stmt))
7867 : 56 : el_todo |= TODO_cleanup_cfg;
7868 : : }
7869 : :
7870 : 6214244 : bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
7871 : 6214244 : bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
7872 : :
7873 : 6214244 : if (do_eh_cleanup)
7874 : 10668 : gimple_purge_all_dead_eh_edges (need_eh_cleanup);
7875 : :
7876 : 6214244 : if (do_ab_cleanup)
7877 : 2 : gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
7878 : :
7879 : 6214244 : if (do_eh_cleanup || do_ab_cleanup)
7880 : 10670 : el_todo |= TODO_cleanup_cfg;
7881 : :
7882 : 6214244 : return el_todo;
7883 : : }
7884 : :
7885 : : /* Eliminate fully redundant computations. */
7886 : :
7887 : : unsigned
7888 : 4349753 : eliminate_with_rpo_vn (bitmap inserted_exprs)
7889 : : {
7890 : 4349753 : eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
7891 : :
7892 : 4349753 : eliminate_dom_walker *saved_rpo_avail = rpo_avail;
7893 : 4349753 : rpo_avail = &walker;
7894 : 4349753 : walker.walk (cfun->cfg->x_entry_block_ptr);
7895 : 4349753 : rpo_avail = saved_rpo_avail;
7896 : :
7897 : 4349753 : return walker.eliminate_cleanup ();
7898 : 4349753 : }
7899 : :
7900 : : static unsigned
7901 : : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
7902 : : bool iterate, bool eliminate, bool skip_entry_phis,
7903 : : vn_lookup_kind kind);
7904 : :
7905 : : void
7906 : 970003 : run_rpo_vn (vn_lookup_kind kind)
7907 : : {
7908 : 970003 : do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
7909 : :
7910 : : /* ??? Prune requirement of these. */
7911 : 970003 : constant_to_value_id = new hash_table<vn_constant_hasher> (23);
7912 : :
7913 : : /* Initialize the value ids and prune out remaining VN_TOPs
7914 : : from dead code. */
7915 : 970003 : tree name;
7916 : 970003 : unsigned i;
7917 : 47657990 : FOR_EACH_SSA_NAME (i, name, cfun)
7918 : : {
7919 : 34263752 : vn_ssa_aux_t info = VN_INFO (name);
7920 : 34263752 : if (!info->visited
7921 : 34168486 : || info->valnum == VN_TOP)
7922 : 95266 : info->valnum = name;
7923 : 34263752 : if (info->valnum == name)
7924 : 33024531 : info->value_id = get_next_value_id ();
7925 : 1239221 : else if (is_gimple_min_invariant (info->valnum))
7926 : 50553 : info->value_id = get_or_alloc_constant_value_id (info->valnum);
7927 : : }
7928 : :
7929 : : /* Propagate. */
7930 : 47657990 : FOR_EACH_SSA_NAME (i, name, cfun)
7931 : : {
7932 : 34263752 : vn_ssa_aux_t info = VN_INFO (name);
7933 : 34263752 : if (TREE_CODE (info->valnum) == SSA_NAME
7934 : 34213199 : && info->valnum != name
7935 : 35452420 : && info->value_id != VN_INFO (info->valnum)->value_id)
7936 : 1188668 : info->value_id = VN_INFO (info->valnum)->value_id;
7937 : : }
7938 : :
7939 : 970003 : set_hashtable_value_ids ();
7940 : :
7941 : 970003 : if (dump_file && (dump_flags & TDF_DETAILS))
7942 : : {
7943 : 14 : fprintf (dump_file, "Value numbers:\n");
7944 : 406 : FOR_EACH_SSA_NAME (i, name, cfun)
7945 : : {
7946 : 307 : if (VN_INFO (name)->visited
7947 : 307 : && SSA_VAL (name) != name)
7948 : : {
7949 : 33 : print_generic_expr (dump_file, name);
7950 : 33 : fprintf (dump_file, " = ");
7951 : 33 : print_generic_expr (dump_file, SSA_VAL (name));
7952 : 33 : fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
7953 : : }
7954 : : }
7955 : : }
7956 : 970003 : }
7957 : :
7958 : : /* Free VN associated data structures. */
7959 : :
7960 : : void
7961 : 6233625 : free_rpo_vn (void)
7962 : : {
7963 : 6233625 : free_vn_table (valid_info);
7964 : 6233625 : XDELETE (valid_info);
7965 : 6233625 : obstack_free (&vn_tables_obstack, NULL);
7966 : 6233625 : obstack_free (&vn_tables_insert_obstack, NULL);
7967 : :
7968 : 6233625 : vn_ssa_aux_iterator_type it;
7969 : 6233625 : vn_ssa_aux_t info;
7970 : 352945473 : FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
7971 : 173355924 : if (info->needs_insertion)
7972 : 4092530 : release_ssa_name (info->name);
7973 : 6233625 : obstack_free (&vn_ssa_aux_obstack, NULL);
7974 : 6233625 : delete vn_ssa_aux_hash;
7975 : :
7976 : 6233625 : delete constant_to_value_id;
7977 : 6233625 : constant_to_value_id = NULL;
7978 : 6233625 : }
7979 : :
7980 : : /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7981 : :
7982 : : static tree
7983 : 22860340 : vn_lookup_simplify_result (gimple_match_op *res_op)
7984 : : {
7985 : 22860340 : if (!res_op->code.is_tree_code ())
7986 : : return NULL_TREE;
7987 : 22856803 : tree *ops = res_op->ops;
7988 : 22856803 : unsigned int length = res_op->num_ops;
7989 : 22856803 : if (res_op->code == CONSTRUCTOR
7990 : : /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7991 : : and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7992 : 22856803 : && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
7993 : : {
7994 : 1217 : length = CONSTRUCTOR_NELTS (res_op->ops[0]);
7995 : 1217 : ops = XALLOCAVEC (tree, length);
7996 : 5891 : for (unsigned i = 0; i < length; ++i)
7997 : 4674 : ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
7998 : : }
7999 : 22856803 : vn_nary_op_t vnresult = NULL;
8000 : 22856803 : tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
8001 : : res_op->type, ops, &vnresult);
8002 : : /* If this is used from expression simplification make sure to
8003 : : return an available expression. */
8004 : 22856803 : if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
8005 : 2162302 : res = rpo_avail->eliminate_avail (vn_context_bb, res);
8006 : : return res;
8007 : : }
8008 : :
8009 : : /* Return a leader for OPs value that is valid at BB. */
8010 : :
8011 : : tree
8012 : 263976255 : rpo_elim::eliminate_avail (basic_block bb, tree op)
8013 : : {
8014 : 263976255 : bool visited;
8015 : 263976255 : tree valnum = SSA_VAL (op, &visited);
8016 : : /* If we didn't visit OP then it must be defined outside of the
8017 : : region we process and also dominate it. So it is available. */
8018 : 263976255 : if (!visited)
8019 : : return op;
8020 : 261810344 : if (TREE_CODE (valnum) == SSA_NAME)
8021 : : {
8022 : 247928617 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
8023 : : return valnum;
8024 : 241096350 : vn_ssa_aux_t valnum_info = VN_INFO (valnum);
8025 : 241096350 : vn_avail *av = valnum_info->avail;
8026 : 241096350 : if (!av)
8027 : : {
8028 : : /* See above. But when there's availability info prefer
8029 : : what we recorded there for example to preserve LC SSA. */
8030 : 84323740 : if (!valnum_info->visited)
8031 : : return valnum;
8032 : : return NULL_TREE;
8033 : : }
8034 : 156772610 : if (av->location == bb->index)
8035 : : /* On tramp3d 90% of the cases are here. */
8036 : 104347237 : return ssa_name (av->leader);
8037 : 66460794 : do
8038 : : {
8039 : 66460794 : basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
8040 : : /* ??? During elimination we have to use availability at the
8041 : : definition site of a use we try to replace. This
8042 : : is required to not run into inconsistencies because
8043 : : of dominated_by_p_w_unex behavior and removing a definition
8044 : : while not replacing all uses.
8045 : : ??? We could try to consistently walk dominators
8046 : : ignoring non-executable regions. The nearest common
8047 : : dominator of bb and abb is where we can stop walking. We
8048 : : may also be able to "pre-compute" (bits of) the next immediate
8049 : : (non-)dominator during the RPO walk when marking edges as
8050 : : executable. */
8051 : 66460794 : if (dominated_by_p_w_unex (bb, abb, true))
8052 : : {
8053 : 48510317 : tree leader = ssa_name (av->leader);
8054 : : /* Prevent eliminations that break loop-closed SSA. */
8055 : 48510317 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
8056 : 3090554 : && ! SSA_NAME_IS_DEFAULT_DEF (leader)
8057 : 51600871 : && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
8058 : 3090554 : (leader))->loop_father,
8059 : : bb))
8060 : : return NULL_TREE;
8061 : 48429613 : if (dump_file && (dump_flags & TDF_DETAILS))
8062 : : {
8063 : 3547 : print_generic_expr (dump_file, leader);
8064 : 3547 : fprintf (dump_file, " is available for ");
8065 : 3547 : print_generic_expr (dump_file, valnum);
8066 : 3547 : fprintf (dump_file, "\n");
8067 : : }
8068 : : /* On tramp3d 99% of the _remaining_ cases succeed at
8069 : : the first enty. */
8070 : 48429613 : return leader;
8071 : : }
8072 : : /* ??? Can we somehow skip to the immediate dominator
8073 : : RPO index (bb_to_rpo)? Again, maybe not worth, on
8074 : : tramp3d the worst number of elements in the vector is 9. */
8075 : 17950477 : av = av->next;
8076 : : }
8077 : 17950477 : while (av);
8078 : : /* While we prefer avail we have to fallback to using the value
8079 : : directly if defined outside of the region when none of the
8080 : : available defs suit. */
8081 : 3915056 : if (!valnum_info->visited)
8082 : : return valnum;
8083 : : }
8084 : 13881727 : else if (valnum != VN_TOP)
8085 : : /* valnum is is_gimple_min_invariant. */
8086 : : return valnum;
8087 : : return NULL_TREE;
8088 : : }
8089 : :
8090 : : /* Make LEADER a leader for its value at BB. */
8091 : :
8092 : : void
8093 : 98046608 : rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
8094 : : {
8095 : 98046608 : tree valnum = VN_INFO (leader)->valnum;
8096 : 98046608 : if (valnum == VN_TOP
8097 : 98046608 : || is_gimple_min_invariant (valnum))
8098 : 0 : return;
8099 : 98046608 : if (dump_file && (dump_flags & TDF_DETAILS))
8100 : : {
8101 : 308806 : fprintf (dump_file, "Making available beyond BB%d ", bb->index);
8102 : 308806 : print_generic_expr (dump_file, leader);
8103 : 308806 : fprintf (dump_file, " for value ");
8104 : 308806 : print_generic_expr (dump_file, valnum);
8105 : 308806 : fprintf (dump_file, "\n");
8106 : : }
8107 : 98046608 : vn_ssa_aux_t value = VN_INFO (valnum);
8108 : 98046608 : vn_avail *av;
8109 : 98046608 : if (m_avail_freelist)
8110 : : {
8111 : 19049085 : av = m_avail_freelist;
8112 : 19049085 : m_avail_freelist = m_avail_freelist->next;
8113 : : }
8114 : : else
8115 : 78997523 : av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
8116 : 98046608 : av->location = bb->index;
8117 : 98046608 : av->leader = SSA_NAME_VERSION (leader);
8118 : 98046608 : av->next = value->avail;
8119 : 98046608 : av->next_undo = last_pushed_avail;
8120 : 98046608 : last_pushed_avail = value;
8121 : 98046608 : value->avail = av;
8122 : : }
8123 : :
8124 : : /* Valueization hook for RPO VN plus required state. */
8125 : :
8126 : : tree
8127 : 1998550761 : rpo_vn_valueize (tree name)
8128 : : {
8129 : 1998550761 : if (TREE_CODE (name) == SSA_NAME)
8130 : : {
8131 : 1952092190 : vn_ssa_aux_t val = VN_INFO (name);
8132 : 1952092190 : if (val)
8133 : : {
8134 : 1952092190 : tree tem = val->valnum;
8135 : 1952092190 : if (tem != VN_TOP && tem != name)
8136 : : {
8137 : 104121380 : if (TREE_CODE (tem) != SSA_NAME)
8138 : : return tem;
8139 : : /* For all values we only valueize to an available leader
8140 : : which means we can use SSA name info without restriction. */
8141 : 87407757 : tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
8142 : 87407757 : if (tem)
8143 : : return tem;
8144 : : }
8145 : : }
8146 : : }
8147 : : return name;
8148 : : }
8149 : :
8150 : : /* Insert on PRED_E predicates derived from CODE OPS being true besides the
8151 : : inverted condition. */
8152 : :
8153 : : static void
8154 : 27791897 : insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
8155 : : {
8156 : 27791897 : switch (code)
8157 : : {
8158 : 1367233 : case LT_EXPR:
8159 : : /* a < b -> a {!,<}= b */
8160 : 1367233 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8161 : : ops, boolean_true_node, 0, pred_e);
8162 : 1367233 : vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
8163 : : ops, boolean_true_node, 0, pred_e);
8164 : : /* a < b -> ! a {>,=} b */
8165 : 1367233 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8166 : : ops, boolean_false_node, 0, pred_e);
8167 : 1367233 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8168 : : ops, boolean_false_node, 0, pred_e);
8169 : 1367233 : break;
8170 : 3479073 : case GT_EXPR:
8171 : : /* a > b -> a {!,>}= b */
8172 : 3479073 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8173 : : ops, boolean_true_node, 0, pred_e);
8174 : 3479073 : vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
8175 : : ops, boolean_true_node, 0, pred_e);
8176 : : /* a > b -> ! a {<,=} b */
8177 : 3479073 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8178 : : ops, boolean_false_node, 0, pred_e);
8179 : 3479073 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8180 : : ops, boolean_false_node, 0, pred_e);
8181 : 3479073 : break;
8182 : 9521402 : case EQ_EXPR:
8183 : : /* a == b -> ! a {<,>} b */
8184 : 9521402 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8185 : : ops, boolean_false_node, 0, pred_e);
8186 : 9521402 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8187 : : ops, boolean_false_node, 0, pred_e);
8188 : 9521402 : break;
8189 : : case LE_EXPR:
8190 : : case GE_EXPR:
8191 : : case NE_EXPR:
8192 : : /* Nothing besides inverted condition. */
8193 : : break;
8194 : 27791897 : default:;
8195 : : }
8196 : 27791897 : }
8197 : :
8198 : : /* Insert on the TRUE_E true and FALSE_E false predicates
8199 : : derived from LHS CODE RHS. */
8200 : :
8201 : : static void
8202 : 23686931 : insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
8203 : : edge true_e, edge false_e)
8204 : : {
8205 : : /* If both edges are null, then there is nothing to be done. */
8206 : 23686931 : if (!true_e && !false_e)
8207 : 1331778 : return;
8208 : :
8209 : : /* Canonicalize the comparison if needed, putting
8210 : : the constant in the rhs. */
8211 : 22358535 : if (tree_swap_operands_p (lhs, rhs))
8212 : : {
8213 : 16865 : std::swap (lhs, rhs);
8214 : 16865 : code = swap_tree_comparison (code);
8215 : : }
8216 : :
8217 : : /* If the lhs is not a ssa name, don't record anything. */
8218 : 22358535 : if (TREE_CODE (lhs) != SSA_NAME)
8219 : : return;
8220 : :
8221 : 22355153 : tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs));
8222 : 22355153 : tree ops[2];
8223 : 22355153 : ops[0] = lhs;
8224 : 22355153 : ops[1] = rhs;
8225 : 22355153 : if (true_e)
8226 : 18255250 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8227 : : boolean_true_node, 0, true_e);
8228 : 22355153 : if (false_e)
8229 : 17177631 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8230 : : boolean_false_node, 0, false_e);
8231 : 22355153 : if (icode != ERROR_MARK)
8232 : : {
8233 : 22103850 : if (true_e)
8234 : 18100995 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8235 : : boolean_false_node, 0, true_e);
8236 : 22103850 : if (false_e)
8237 : 16973582 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8238 : : boolean_true_node, 0, false_e);
8239 : : }
8240 : : /* Relax for non-integers, inverted condition handled
8241 : : above. */
8242 : 22355153 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8243 : : {
8244 : 17513612 : if (true_e)
8245 : 14375615 : insert_related_predicates_on_edge (code, ops, true_e);
8246 : 17513612 : if (false_e)
8247 : 13416282 : insert_related_predicates_on_edge (icode, ops, false_e);
8248 : : }
8249 : 22355153 : if (integer_zerop (rhs)
8250 : 22355153 : && (code == NE_EXPR || code == EQ_EXPR))
8251 : : {
8252 : 9306298 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
8253 : : /* (A CMP B) != 0 is the same as (A CMP B).
8254 : : (A CMP B) == 0 is just (A CMP B) with the edges swapped. */
8255 : 9306298 : if (is_gimple_assign (def_stmt)
8256 : 9306298 : && TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_comparison)
8257 : : {
8258 : 455837 : tree_code nc = gimple_assign_rhs_code (def_stmt);
8259 : 455837 : tree nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8260 : 455837 : tree nrhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8261 : 455837 : edge nt = true_e;
8262 : 455837 : edge nf = false_e;
8263 : 455837 : if (code == EQ_EXPR)
8264 : 325249 : std::swap (nt, nf);
8265 : 455837 : if (lhs != nlhs)
8266 : 455837 : insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
8267 : : }
8268 : : /* (a | b) == 0 ->
8269 : : on true edge assert: a == 0 & b == 0. */
8270 : : /* (a | b) != 0 ->
8271 : : on false edge assert: a == 0 & b == 0. */
8272 : 9306298 : if (is_gimple_assign (def_stmt)
8273 : 9306298 : && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
8274 : : {
8275 : 267467 : edge e = code == EQ_EXPR ? true_e : false_e;
8276 : 267467 : tree nlhs;
8277 : :
8278 : 267467 : nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8279 : : /* A valueization of the `a` might return the old lhs
8280 : : which is already handled above. */
8281 : 267467 : if (nlhs != lhs)
8282 : 267467 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8283 : :
8284 : : /* A valueization of the `b` might return the old lhs
8285 : : which is already handled above. */
8286 : 267467 : nlhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8287 : 267467 : if (nlhs != lhs)
8288 : 267467 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8289 : : }
8290 : : }
8291 : : }
8292 : :
8293 : : /* Main stmt worker for RPO VN, process BB. */
8294 : :
8295 : : static unsigned
8296 : 62651386 : process_bb (rpo_elim &avail, basic_block bb,
8297 : : bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
8298 : : bool do_region, bitmap exit_bbs, bool skip_phis)
8299 : : {
8300 : 62651386 : unsigned todo = 0;
8301 : 62651386 : edge_iterator ei;
8302 : 62651386 : edge e;
8303 : :
8304 : 62651386 : vn_context_bb = bb;
8305 : :
8306 : : /* If we are in loop-closed SSA preserve this state. This is
8307 : : relevant when called on regions from outside of FRE/PRE. */
8308 : 62651386 : bool lc_phi_nodes = false;
8309 : 62651386 : if (!skip_phis
8310 : 62651386 : && loops_state_satisfies_p (LOOP_CLOSED_SSA))
8311 : 3506911 : FOR_EACH_EDGE (e, ei, bb->preds)
8312 : 2120925 : if (e->src->loop_father != e->dest->loop_father
8313 : 2120925 : && flow_loop_nested_p (e->dest->loop_father,
8314 : : e->src->loop_father))
8315 : : {
8316 : : lc_phi_nodes = true;
8317 : : break;
8318 : : }
8319 : :
8320 : : /* When we visit a loop header substitute into loop info. */
8321 : 62651386 : if (!iterate && eliminate && bb->loop_father->header == bb)
8322 : : {
8323 : : /* Keep fields in sync with substitute_in_loop_info. */
8324 : 954990 : if (bb->loop_father->nb_iterations)
8325 : 153366 : bb->loop_father->nb_iterations
8326 : 153366 : = simplify_replace_tree (bb->loop_father->nb_iterations,
8327 : : NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
8328 : : }
8329 : :
8330 : : /* Value-number all defs in the basic-block. */
8331 : 62651386 : if (!skip_phis)
8332 : 89910901 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8333 : 27286351 : gsi_next (&gsi))
8334 : : {
8335 : 27286351 : gphi *phi = gsi.phi ();
8336 : 27286351 : tree res = PHI_RESULT (phi);
8337 : 27286351 : vn_ssa_aux_t res_info = VN_INFO (res);
8338 : 27286351 : if (!bb_visited)
8339 : : {
8340 : 19269250 : gcc_assert (!res_info->visited);
8341 : 19269250 : res_info->valnum = VN_TOP;
8342 : 19269250 : res_info->visited = true;
8343 : : }
8344 : :
8345 : : /* When not iterating force backedge values to varying. */
8346 : 27286351 : visit_stmt (phi, !iterate_phis);
8347 : 54572702 : if (virtual_operand_p (res))
8348 : 10800118 : continue;
8349 : :
8350 : : /* Eliminate */
8351 : : /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
8352 : : how we handle backedges and availability.
8353 : : And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
8354 : 16486233 : tree val = res_info->valnum;
8355 : 16486233 : if (res != val && !iterate && eliminate)
8356 : : {
8357 : 1391397 : if (tree leader = avail.eliminate_avail (bb, res))
8358 : : {
8359 : 1268423 : if (leader != res
8360 : : /* Preserve loop-closed SSA form. */
8361 : 1268423 : && (! lc_phi_nodes
8362 : 6779 : || is_gimple_min_invariant (leader)))
8363 : : {
8364 : 1267576 : if (dump_file && (dump_flags & TDF_DETAILS))
8365 : : {
8366 : 202 : fprintf (dump_file, "Replaced redundant PHI node "
8367 : : "defining ");
8368 : 202 : print_generic_expr (dump_file, res);
8369 : 202 : fprintf (dump_file, " with ");
8370 : 202 : print_generic_expr (dump_file, leader);
8371 : 202 : fprintf (dump_file, "\n");
8372 : : }
8373 : 1267576 : avail.eliminations++;
8374 : :
8375 : 1267576 : if (may_propagate_copy (res, leader))
8376 : : {
8377 : : /* Schedule for removal. */
8378 : 1267576 : avail.to_remove.safe_push (phi);
8379 : 1267576 : continue;
8380 : : }
8381 : : /* ??? Else generate a copy stmt. */
8382 : : }
8383 : : }
8384 : : }
8385 : : /* Only make defs available that not already are. But make
8386 : : sure loop-closed SSA PHI node defs are picked up for
8387 : : downstream uses. */
8388 : 15218657 : if (lc_phi_nodes
8389 : 15218657 : || res == val
8390 : 15218657 : || ! avail.eliminate_avail (bb, res))
8391 : 11637258 : avail.eliminate_push_avail (bb, res);
8392 : : }
8393 : :
8394 : : /* For empty BBs mark outgoing edges executable. For non-empty BBs
8395 : : we do this when processing the last stmt as we have to do this
8396 : : before elimination which otherwise forces GIMPLE_CONDs to
8397 : : if (1 != 0) style when seeing non-executable edges. */
8398 : 125302772 : if (gsi_end_p (gsi_start_bb (bb)))
8399 : : {
8400 : 14386734 : FOR_EACH_EDGE (e, ei, bb->succs)
8401 : : {
8402 : 7193367 : if (!(e->flags & EDGE_EXECUTABLE))
8403 : : {
8404 : 4898541 : if (dump_file && (dump_flags & TDF_DETAILS))
8405 : 5871 : fprintf (dump_file,
8406 : : "marking outgoing edge %d -> %d executable\n",
8407 : 5871 : e->src->index, e->dest->index);
8408 : 4898541 : e->flags |= EDGE_EXECUTABLE;
8409 : 4898541 : e->dest->flags |= BB_EXECUTABLE;
8410 : : }
8411 : 2294826 : else if (!(e->dest->flags & BB_EXECUTABLE))
8412 : : {
8413 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8414 : 0 : fprintf (dump_file,
8415 : : "marking destination block %d reachable\n",
8416 : : e->dest->index);
8417 : 0 : e->dest->flags |= BB_EXECUTABLE;
8418 : : }
8419 : : }
8420 : : }
8421 : 125302772 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8422 : 492278136 : !gsi_end_p (gsi); gsi_next (&gsi))
8423 : : {
8424 : 429626750 : ssa_op_iter i;
8425 : 429626750 : tree op;
8426 : 429626750 : if (!bb_visited)
8427 : : {
8428 : 491005988 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8429 : : {
8430 : 138324725 : vn_ssa_aux_t op_info = VN_INFO (op);
8431 : 138324725 : gcc_assert (!op_info->visited);
8432 : 138324725 : op_info->valnum = VN_TOP;
8433 : 138324725 : op_info->visited = true;
8434 : : }
8435 : :
8436 : : /* We somehow have to deal with uses that are not defined
8437 : : in the processed region. Forcing unvisited uses to
8438 : : varying here doesn't play well with def-use following during
8439 : : expression simplification, so we deal with this by checking
8440 : : the visited flag in SSA_VAL. */
8441 : : }
8442 : :
8443 : 429626750 : visit_stmt (gsi_stmt (gsi));
8444 : :
8445 : 429626750 : gimple *last = gsi_stmt (gsi);
8446 : 429626750 : e = NULL;
8447 : 429626750 : switch (gimple_code (last))
8448 : : {
8449 : 124016 : case GIMPLE_SWITCH:
8450 : 124016 : e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8451 : 124016 : (as_a <gswitch *> (last))));
8452 : 124016 : break;
8453 : 24848629 : case GIMPLE_COND:
8454 : 24848629 : {
8455 : 24848629 : tree lhs = vn_valueize (gimple_cond_lhs (last));
8456 : 24848629 : tree rhs = vn_valueize (gimple_cond_rhs (last));
8457 : 24848629 : tree_code cmpcode = gimple_cond_code (last);
8458 : : /* Canonicalize the comparison if needed, putting
8459 : : the constant in the rhs. */
8460 : 24848629 : if (tree_swap_operands_p (lhs, rhs))
8461 : : {
8462 : 850965 : std::swap (lhs, rhs);
8463 : 850965 : cmpcode = swap_tree_comparison (cmpcode);
8464 : : }
8465 : 24848629 : tree val = gimple_simplify (cmpcode,
8466 : : boolean_type_node, lhs, rhs,
8467 : : NULL, vn_valueize);
8468 : : /* If the condition didn't simplfy see if we have recorded
8469 : : an expression from sofar taken edges. */
8470 : 24848629 : if (! val || TREE_CODE (val) != INTEGER_CST)
8471 : : {
8472 : 23024491 : vn_nary_op_t vnresult;
8473 : 23024491 : tree ops[2];
8474 : 23024491 : ops[0] = lhs;
8475 : 23024491 : ops[1] = rhs;
8476 : 23024491 : val = vn_nary_op_lookup_pieces (2, cmpcode,
8477 : : boolean_type_node, ops,
8478 : : &vnresult);
8479 : : /* Got back a ssa name, then try looking up `val != 0`
8480 : : as it might have been recorded that way. */
8481 : 23024491 : if (val && TREE_CODE (val) == SSA_NAME)
8482 : : {
8483 : 144593 : ops[0] = val;
8484 : 144593 : ops[1] = build_zero_cst (TREE_TYPE (val));
8485 : 144593 : val = vn_nary_op_lookup_pieces (2, NE_EXPR,
8486 : : boolean_type_node, ops,
8487 : : &vnresult);
8488 : : }
8489 : : /* Did we get a predicated value? */
8490 : 23024475 : if (! val && vnresult && vnresult->predicated_values)
8491 : : {
8492 : 1294969 : val = vn_nary_op_get_predicated_value (vnresult, bb);
8493 : 1294969 : if (val && dump_file && (dump_flags & TDF_DETAILS))
8494 : : {
8495 : 2 : fprintf (dump_file, "Got predicated value ");
8496 : 2 : print_generic_expr (dump_file, val, TDF_NONE);
8497 : 2 : fprintf (dump_file, " for ");
8498 : 2 : print_gimple_stmt (dump_file, last, TDF_SLIM);
8499 : : }
8500 : : }
8501 : : }
8502 : 23024491 : if (val)
8503 : 2152469 : e = find_taken_edge (bb, val);
8504 : 24848629 : if (! e)
8505 : : {
8506 : : /* If we didn't manage to compute the taken edge then
8507 : : push predicated expressions for the condition itself
8508 : : and related conditions to the hashtables. This allows
8509 : : simplification of redundant conditions which is
8510 : : important as early cleanup. */
8511 : 22696160 : edge true_e, false_e;
8512 : 22696160 : extract_true_false_edges_from_block (bb, &true_e, &false_e);
8513 : 541021 : if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8514 : 22921999 : || !can_track_predicate_on_edge (true_e))
8515 : 4977565 : true_e = NULL;
8516 : 541021 : if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8517 : 22894471 : || !can_track_predicate_on_edge (false_e))
8518 : 5935476 : false_e = NULL;
8519 : 22696160 : insert_predicates_for_cond (cmpcode, lhs, rhs, true_e, false_e);
8520 : : }
8521 : : break;
8522 : : }
8523 : 1367 : case GIMPLE_GOTO:
8524 : 1367 : e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8525 : 1367 : break;
8526 : : default:
8527 : : e = NULL;
8528 : : }
8529 : 429626750 : if (e)
8530 : : {
8531 : 2156216 : todo = TODO_cleanup_cfg;
8532 : 2156216 : if (!(e->flags & EDGE_EXECUTABLE))
8533 : : {
8534 : 1697681 : if (dump_file && (dump_flags & TDF_DETAILS))
8535 : 35 : fprintf (dump_file,
8536 : : "marking known outgoing %sedge %d -> %d executable\n",
8537 : 35 : e->flags & EDGE_DFS_BACK ? "back-" : "",
8538 : 35 : e->src->index, e->dest->index);
8539 : 1697681 : e->flags |= EDGE_EXECUTABLE;
8540 : 1697681 : e->dest->flags |= BB_EXECUTABLE;
8541 : : }
8542 : 458535 : else if (!(e->dest->flags & BB_EXECUTABLE))
8543 : : {
8544 : 27189 : if (dump_file && (dump_flags & TDF_DETAILS))
8545 : 1 : fprintf (dump_file,
8546 : : "marking destination block %d reachable\n",
8547 : : e->dest->index);
8548 : 27189 : e->dest->flags |= BB_EXECUTABLE;
8549 : : }
8550 : : }
8551 : 854941068 : else if (gsi_one_before_end_p (gsi))
8552 : : {
8553 : 131028129 : FOR_EACH_EDGE (e, ei, bb->succs)
8554 : : {
8555 : 77726326 : if (!(e->flags & EDGE_EXECUTABLE))
8556 : : {
8557 : 57044317 : if (dump_file && (dump_flags & TDF_DETAILS))
8558 : 17505 : fprintf (dump_file,
8559 : : "marking outgoing edge %d -> %d executable\n",
8560 : 17505 : e->src->index, e->dest->index);
8561 : 57044317 : e->flags |= EDGE_EXECUTABLE;
8562 : 57044317 : e->dest->flags |= BB_EXECUTABLE;
8563 : : }
8564 : 20682009 : else if (!(e->dest->flags & BB_EXECUTABLE))
8565 : : {
8566 : 2602582 : if (dump_file && (dump_flags & TDF_DETAILS))
8567 : 5854 : fprintf (dump_file,
8568 : : "marking destination block %d reachable\n",
8569 : : e->dest->index);
8570 : 2602582 : e->dest->flags |= BB_EXECUTABLE;
8571 : : }
8572 : : }
8573 : : }
8574 : :
8575 : : /* Eliminate. That also pushes to avail. */
8576 : 429626750 : if (eliminate && ! iterate)
8577 : 106223017 : avail.eliminate_stmt (bb, &gsi);
8578 : : else
8579 : : /* If not eliminating, make all not already available defs
8580 : : available. But avoid picking up dead defs. */
8581 : 404301738 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8582 : 80898005 : if (! has_zero_uses (op)
8583 : 80898005 : && ! avail.eliminate_avail (bb, op))
8584 : 61843579 : avail.eliminate_push_avail (bb, op);
8585 : : }
8586 : :
8587 : : /* Eliminate in destination PHI arguments. Always substitute in dest
8588 : : PHIs, even for non-executable edges. This handles region
8589 : : exits PHIs. */
8590 : 62651386 : if (!iterate && eliminate)
8591 : 33586040 : FOR_EACH_EDGE (e, ei, bb->succs)
8592 : 20014268 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
8593 : 38673370 : !gsi_end_p (gsi); gsi_next (&gsi))
8594 : : {
8595 : 18659102 : gphi *phi = gsi.phi ();
8596 : 18659102 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8597 : 18659102 : tree arg = USE_FROM_PTR (use_p);
8598 : 28373170 : if (TREE_CODE (arg) != SSA_NAME
8599 : 18659102 : || virtual_operand_p (arg))
8600 : 9714068 : continue;
8601 : 8945034 : tree sprime;
8602 : 8945034 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
8603 : : {
8604 : 121758 : sprime = SSA_VAL (arg);
8605 : 121758 : gcc_assert (TREE_CODE (sprime) != SSA_NAME
8606 : : || SSA_NAME_IS_DEFAULT_DEF (sprime));
8607 : : }
8608 : : else
8609 : : /* Look for sth available at the definition block of the argument.
8610 : : This avoids inconsistencies between availability there which
8611 : : decides if the stmt can be removed and availability at the
8612 : : use site. The SSA property ensures that things available
8613 : : at the definition are also available at uses. */
8614 : 8823276 : sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8615 : : arg);
8616 : 8945034 : if (sprime
8617 : 8945034 : && sprime != arg
8618 : 8945034 : && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8619 : 1506437 : propagate_value (use_p, sprime);
8620 : : }
8621 : :
8622 : 62651386 : vn_context_bb = NULL;
8623 : 62651386 : return todo;
8624 : : }
8625 : :
8626 : : /* Unwind state per basic-block. */
8627 : :
8628 : : struct unwind_state
8629 : : {
8630 : : /* Times this block has been visited. */
8631 : : unsigned visited;
8632 : : /* Whether to handle this as iteration point or whether to treat
8633 : : incoming backedge PHI values as varying. */
8634 : : bool iterate;
8635 : : /* Maximum RPO index this block is reachable from. */
8636 : : int max_rpo;
8637 : : /* Unwind state. */
8638 : : void *ob_top;
8639 : : vn_reference_t ref_top;
8640 : : vn_phi_t phi_top;
8641 : : vn_nary_op_t nary_top;
8642 : : vn_avail *avail_top;
8643 : : };
8644 : :
8645 : : /* Unwind the RPO VN state for iteration. */
8646 : :
8647 : : static void
8648 : 1930048 : do_unwind (unwind_state *to, rpo_elim &avail)
8649 : : {
8650 : 1930048 : gcc_assert (to->iterate);
8651 : 35448621 : for (; last_inserted_nary != to->nary_top;
8652 : 33518573 : last_inserted_nary = last_inserted_nary->next)
8653 : : {
8654 : 33518573 : vn_nary_op_t *slot;
8655 : 33518573 : slot = valid_info->nary->find_slot_with_hash
8656 : 33518573 : (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
8657 : : /* Predication causes the need to restore previous state. */
8658 : 33518573 : if ((*slot)->unwind_to)
8659 : 6717712 : *slot = (*slot)->unwind_to;
8660 : : else
8661 : 26800861 : valid_info->nary->clear_slot (slot);
8662 : : }
8663 : 7567238 : for (; last_inserted_phi != to->phi_top;
8664 : 5637190 : last_inserted_phi = last_inserted_phi->next)
8665 : : {
8666 : 5637190 : vn_phi_t *slot;
8667 : 5637190 : slot = valid_info->phis->find_slot_with_hash
8668 : 5637190 : (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
8669 : 5637190 : valid_info->phis->clear_slot (slot);
8670 : : }
8671 : 15365880 : for (; last_inserted_ref != to->ref_top;
8672 : 13435832 : last_inserted_ref = last_inserted_ref->next)
8673 : : {
8674 : 13435832 : vn_reference_t *slot;
8675 : 13435832 : slot = valid_info->references->find_slot_with_hash
8676 : 13435832 : (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
8677 : 13435832 : (*slot)->operands.release ();
8678 : 13435832 : valid_info->references->clear_slot (slot);
8679 : : }
8680 : 1930048 : obstack_free (&vn_tables_obstack, to->ob_top);
8681 : :
8682 : : /* Prune [rpo_idx, ] from avail. */
8683 : 20979133 : for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
8684 : : {
8685 : 19049085 : vn_ssa_aux_t val = last_pushed_avail;
8686 : 19049085 : vn_avail *av = val->avail;
8687 : 19049085 : val->avail = av->next;
8688 : 19049085 : last_pushed_avail = av->next_undo;
8689 : 19049085 : av->next = avail.m_avail_freelist;
8690 : 19049085 : avail.m_avail_freelist = av;
8691 : : }
8692 : 1930048 : }
8693 : :
8694 : : /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8695 : : If ITERATE is true then treat backedges optimistically as not
8696 : : executed and iterate. If ELIMINATE is true then perform
8697 : : elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8698 : : is true then force PHI nodes in ENTRY->dest to VARYING. */
8699 : :
8700 : : static unsigned
8701 : 6233625 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8702 : : bool iterate, bool eliminate, bool skip_entry_phis,
8703 : : vn_lookup_kind kind)
8704 : : {
8705 : 6233625 : unsigned todo = 0;
8706 : 6233625 : default_vn_walk_kind = kind;
8707 : :
8708 : : /* We currently do not support region-based iteration when
8709 : : elimination is requested. */
8710 : 6233625 : gcc_assert (!entry || !iterate || !eliminate);
8711 : : /* When iterating we need loop info up-to-date. */
8712 : 6233625 : gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
8713 : :
8714 : 6233625 : bool do_region = entry != NULL;
8715 : 6233625 : if (!do_region)
8716 : : {
8717 : 5551937 : entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
8718 : 5551937 : exit_bbs = BITMAP_ALLOC (NULL);
8719 : 5551937 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
8720 : : }
8721 : :
8722 : : /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8723 : : re-mark those that are contained in the region. */
8724 : 6233625 : edge_iterator ei;
8725 : 6233625 : edge e;
8726 : 12523666 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8727 : 6290041 : e->flags &= ~EDGE_DFS_BACK;
8728 : :
8729 : 6233625 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
8730 : 6233625 : auto_vec<std::pair<int, int> > toplevel_scc_extents;
8731 : 6233625 : int n = rev_post_order_and_mark_dfs_back_seme
8732 : 8117497 : (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
8733 : :
8734 : 6233625 : if (!do_region)
8735 : 5551937 : BITMAP_FREE (exit_bbs);
8736 : :
8737 : : /* If there are any non-DFS_BACK edges into entry->dest skip
8738 : : processing PHI nodes for that block. This supports
8739 : : value-numbering loop bodies w/o the actual loop. */
8740 : 12523665 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8741 : 6290041 : if (e != entry
8742 : 56416 : && !(e->flags & EDGE_DFS_BACK))
8743 : : break;
8744 : 6233625 : if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
8745 : 0 : fprintf (dump_file, "Region does not contain all edges into "
8746 : : "the entry block, skipping its PHIs.\n");
8747 : 6233625 : skip_entry_phis |= e != NULL;
8748 : :
8749 : 6233625 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
8750 : 57595218 : for (int i = 0; i < n; ++i)
8751 : 51361593 : bb_to_rpo[rpo[i]] = i;
8752 : :
8753 : 6233625 : unwind_state *rpo_state = XNEWVEC (unwind_state, n);
8754 : :
8755 : 6233625 : rpo_elim avail (entry->dest);
8756 : 6233625 : rpo_avail = &avail;
8757 : :
8758 : : /* Verify we have no extra entries into the region. */
8759 : 6233625 : if (flag_checking && do_region)
8760 : : {
8761 : 681682 : auto_bb_flag bb_in_region (fn);
8762 : 2056393 : for (int i = 0; i < n; ++i)
8763 : : {
8764 : 1374711 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8765 : 1374711 : bb->flags |= bb_in_region;
8766 : : }
8767 : : /* We can't merge the first two loops because we cannot rely
8768 : : on EDGE_DFS_BACK for edges not within the region. But if
8769 : : we decide to always have the bb_in_region flag we can
8770 : : do the checking during the RPO walk itself (but then it's
8771 : : also easy to handle MEME conservatively). */
8772 : 2056393 : for (int i = 0; i < n; ++i)
8773 : : {
8774 : 1374711 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8775 : 1374711 : edge e;
8776 : 1374711 : edge_iterator ei;
8777 : 3003268 : FOR_EACH_EDGE (e, ei, bb->preds)
8778 : 1628557 : gcc_assert (e == entry
8779 : : || (skip_entry_phis && bb == entry->dest)
8780 : : || (e->src->flags & bb_in_region));
8781 : : }
8782 : 2056393 : for (int i = 0; i < n; ++i)
8783 : : {
8784 : 1374711 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8785 : 1374711 : bb->flags &= ~bb_in_region;
8786 : : }
8787 : 681682 : }
8788 : :
8789 : : /* Create the VN state. For the initial size of the various hashtables
8790 : : use a heuristic based on region size and number of SSA names. */
8791 : 6233625 : unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
8792 : 6233625 : / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
8793 : 6233625 : VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
8794 : 6233625 : next_value_id = 1;
8795 : 6233625 : next_constant_value_id = -1;
8796 : :
8797 : 6233625 : vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
8798 : 6233625 : gcc_obstack_init (&vn_ssa_aux_obstack);
8799 : :
8800 : 6233625 : gcc_obstack_init (&vn_tables_obstack);
8801 : 6233625 : gcc_obstack_init (&vn_tables_insert_obstack);
8802 : 6233625 : valid_info = XCNEW (struct vn_tables_s);
8803 : 6233625 : allocate_vn_table (valid_info, region_size);
8804 : 6233625 : last_inserted_ref = NULL;
8805 : 6233625 : last_inserted_phi = NULL;
8806 : 6233625 : last_inserted_nary = NULL;
8807 : 6233625 : last_pushed_avail = NULL;
8808 : :
8809 : 6233625 : vn_valueize = rpo_vn_valueize;
8810 : :
8811 : : /* Initialize the unwind state and edge/BB executable state. */
8812 : 6233625 : unsigned curr_scc = 0;
8813 : 57595218 : for (int i = 0; i < n; ++i)
8814 : : {
8815 : 51361593 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8816 : 51361593 : rpo_state[i].visited = 0;
8817 : 51361593 : rpo_state[i].max_rpo = i;
8818 : 59936562 : if (!iterate && curr_scc < toplevel_scc_extents.length ())
8819 : : {
8820 : 7093711 : if (i >= toplevel_scc_extents[curr_scc].first
8821 : 7093711 : && i <= toplevel_scc_extents[curr_scc].second)
8822 : 3882057 : rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
8823 : 7093711 : if (i == toplevel_scc_extents[curr_scc].second)
8824 : 729038 : curr_scc++;
8825 : : }
8826 : 51361593 : bb->flags &= ~BB_EXECUTABLE;
8827 : 51361593 : bool has_backedges = false;
8828 : 51361593 : edge e;
8829 : 51361593 : edge_iterator ei;
8830 : 122014413 : FOR_EACH_EDGE (e, ei, bb->preds)
8831 : : {
8832 : 70652820 : if (e->flags & EDGE_DFS_BACK)
8833 : 2904218 : has_backedges = true;
8834 : 70652820 : e->flags &= ~EDGE_EXECUTABLE;
8835 : 70652820 : if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
8836 : 70652820 : continue;
8837 : : }
8838 : 51361593 : rpo_state[i].iterate = iterate && has_backedges;
8839 : : }
8840 : 6233625 : entry->flags |= EDGE_EXECUTABLE;
8841 : 6233625 : entry->dest->flags |= BB_EXECUTABLE;
8842 : :
8843 : : /* As heuristic to improve compile-time we handle only the N innermost
8844 : : loops and the outermost one optimistically. */
8845 : 6233625 : if (iterate)
8846 : : {
8847 : 4349753 : unsigned max_depth = param_rpo_vn_max_loop_depth;
8848 : 14609521 : for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
8849 : 1562560 : if (loop_depth (loop) > max_depth)
8850 : 1949 : for (unsigned i = 2;
8851 : 8494 : i < loop_depth (loop) - max_depth; ++i)
8852 : : {
8853 : 1949 : basic_block header = superloop_at_depth (loop, i)->header;
8854 : 1949 : bool non_latch_backedge = false;
8855 : 1949 : edge e;
8856 : 1949 : edge_iterator ei;
8857 : 5878 : FOR_EACH_EDGE (e, ei, header->preds)
8858 : 3929 : if (e->flags & EDGE_DFS_BACK)
8859 : : {
8860 : : /* There can be a non-latch backedge into the header
8861 : : which is part of an outer irreducible region. We
8862 : : cannot avoid iterating this block then. */
8863 : 1980 : if (!dominated_by_p (CDI_DOMINATORS,
8864 : 1980 : e->src, e->dest))
8865 : : {
8866 : 12 : if (dump_file && (dump_flags & TDF_DETAILS))
8867 : 0 : fprintf (dump_file, "non-latch backedge %d -> %d "
8868 : : "forces iteration of loop %d\n",
8869 : 0 : e->src->index, e->dest->index, loop->num);
8870 : : non_latch_backedge = true;
8871 : : }
8872 : : else
8873 : 1968 : e->flags |= EDGE_EXECUTABLE;
8874 : : }
8875 : 1949 : rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
8876 : 4349753 : }
8877 : : }
8878 : :
8879 : 6233625 : uint64_t nblk = 0;
8880 : 6233625 : int idx = 0;
8881 : 4349753 : if (iterate)
8882 : : /* Go and process all blocks, iterating as necessary. */
8883 : 49865961 : do
8884 : : {
8885 : 49865961 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8886 : :
8887 : : /* If the block has incoming backedges remember unwind state. This
8888 : : is required even for non-executable blocks since in irreducible
8889 : : regions we might reach them via the backedge and re-start iterating
8890 : : from there.
8891 : : Note we can individually mark blocks with incoming backedges to
8892 : : not iterate where we then handle PHIs conservatively. We do that
8893 : : heuristically to reduce compile-time for degenerate cases. */
8894 : 49865961 : if (rpo_state[idx].iterate)
8895 : : {
8896 : 4456220 : rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
8897 : 4456220 : rpo_state[idx].ref_top = last_inserted_ref;
8898 : 4456220 : rpo_state[idx].phi_top = last_inserted_phi;
8899 : 4456220 : rpo_state[idx].nary_top = last_inserted_nary;
8900 : 4456220 : rpo_state[idx].avail_top
8901 : 4456220 : = last_pushed_avail ? last_pushed_avail->avail : NULL;
8902 : : }
8903 : :
8904 : 49865961 : if (!(bb->flags & BB_EXECUTABLE))
8905 : : {
8906 : 892047 : if (dump_file && (dump_flags & TDF_DETAILS))
8907 : 2 : fprintf (dump_file, "Block %d: BB%d found not executable\n",
8908 : : idx, bb->index);
8909 : 892047 : idx++;
8910 : 2822095 : continue;
8911 : : }
8912 : :
8913 : 48973914 : if (dump_file && (dump_flags & TDF_DETAILS))
8914 : 334 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8915 : 48973914 : nblk++;
8916 : 97947828 : todo |= process_bb (avail, bb,
8917 : 48973914 : rpo_state[idx].visited != 0,
8918 : : rpo_state[idx].iterate,
8919 : : iterate, eliminate, do_region, exit_bbs, false);
8920 : 48973914 : rpo_state[idx].visited++;
8921 : :
8922 : : /* Verify if changed values flow over executable outgoing backedges
8923 : : and those change destination PHI values (that's the thing we
8924 : : can easily verify). Reduce over all such edges to the farthest
8925 : : away PHI. */
8926 : 48973914 : int iterate_to = -1;
8927 : 48973914 : edge_iterator ei;
8928 : 48973914 : edge e;
8929 : 118077097 : FOR_EACH_EDGE (e, ei, bb->succs)
8930 : 69103183 : if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
8931 : : == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
8932 : 4479171 : && rpo_state[bb_to_rpo[e->dest->index]].iterate)
8933 : : {
8934 : 4476503 : int destidx = bb_to_rpo[e->dest->index];
8935 : 4476503 : if (!rpo_state[destidx].visited)
8936 : : {
8937 : 135 : if (dump_file && (dump_flags & TDF_DETAILS))
8938 : 0 : fprintf (dump_file, "Unvisited destination %d\n",
8939 : : e->dest->index);
8940 : 135 : if (iterate_to == -1 || destidx < iterate_to)
8941 : 135 : iterate_to = destidx;
8942 : 135 : continue;
8943 : : }
8944 : 4476368 : if (dump_file && (dump_flags & TDF_DETAILS))
8945 : 53 : fprintf (dump_file, "Looking for changed values of backedge"
8946 : : " %d->%d destination PHIs\n",
8947 : 53 : e->src->index, e->dest->index);
8948 : 4476368 : vn_context_bb = e->dest;
8949 : 4476368 : gphi_iterator gsi;
8950 : 4476368 : for (gsi = gsi_start_phis (e->dest);
8951 : 10289591 : !gsi_end_p (gsi); gsi_next (&gsi))
8952 : : {
8953 : 7743428 : bool inserted = false;
8954 : : /* While we'd ideally just iterate on value changes
8955 : : we CSE PHIs and do that even across basic-block
8956 : : boundaries. So even hashtable state changes can
8957 : : be important (which is roughly equivalent to
8958 : : PHI argument value changes). To not excessively
8959 : : iterate because of that we track whether a PHI
8960 : : was CSEd to with GF_PLF_1. */
8961 : 7743428 : bool phival_changed;
8962 : 7743428 : if ((phival_changed = visit_phi (gsi.phi (),
8963 : : &inserted, false))
8964 : 9121880 : || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
8965 : : {
8966 : 1930205 : if (!phival_changed
8967 : 1930205 : && dump_file && (dump_flags & TDF_DETAILS))
8968 : 0 : fprintf (dump_file, "PHI was CSEd and hashtable "
8969 : : "state (changed)\n");
8970 : 1930205 : if (iterate_to == -1 || destidx < iterate_to)
8971 : 1930120 : iterate_to = destidx;
8972 : 1930205 : break;
8973 : : }
8974 : : }
8975 : 4476368 : vn_context_bb = NULL;
8976 : : }
8977 : 48973914 : if (iterate_to != -1)
8978 : : {
8979 : 1930048 : do_unwind (&rpo_state[iterate_to], avail);
8980 : 1930048 : idx = iterate_to;
8981 : 1930048 : if (dump_file && (dump_flags & TDF_DETAILS))
8982 : 20 : fprintf (dump_file, "Iterating to %d BB%d\n",
8983 : 20 : iterate_to, rpo[iterate_to]);
8984 : 1930048 : continue;
8985 : : }
8986 : :
8987 : 47043866 : idx++;
8988 : : }
8989 : 49865961 : while (idx < n);
8990 : :
8991 : : else /* !iterate */
8992 : : {
8993 : : /* Process all blocks greedily with a worklist that enforces RPO
8994 : : processing of reachable blocks. */
8995 : 1883872 : auto_bitmap worklist;
8996 : 1883872 : bitmap_set_bit (worklist, 0);
8997 : 17445216 : while (!bitmap_empty_p (worklist))
8998 : : {
8999 : 13677472 : int idx = bitmap_clear_first_set_bit (worklist);
9000 : 13677472 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
9001 : 13677472 : gcc_assert ((bb->flags & BB_EXECUTABLE)
9002 : : && !rpo_state[idx].visited);
9003 : :
9004 : 13677472 : if (dump_file && (dump_flags & TDF_DETAILS))
9005 : 33350 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
9006 : :
9007 : : /* When we run into predecessor edges where we cannot trust its
9008 : : executable state mark them executable so PHI processing will
9009 : : be conservative.
9010 : : ??? Do we need to force arguments flowing over that edge
9011 : : to be varying or will they even always be? */
9012 : 13677472 : edge_iterator ei;
9013 : 13677472 : edge e;
9014 : 33187143 : FOR_EACH_EDGE (e, ei, bb->preds)
9015 : 19509671 : if (!(e->flags & EDGE_EXECUTABLE)
9016 : 1045006 : && (bb == entry->dest
9017 : 991664 : || (!rpo_state[bb_to_rpo[e->src->index]].visited
9018 : 956251 : && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
9019 : : >= (int)idx))))
9020 : : {
9021 : 986395 : if (dump_file && (dump_flags & TDF_DETAILS))
9022 : 10711 : fprintf (dump_file, "Cannot trust state of predecessor "
9023 : : "edge %d -> %d, marking executable\n",
9024 : 10711 : e->src->index, e->dest->index);
9025 : 986395 : e->flags |= EDGE_EXECUTABLE;
9026 : : }
9027 : :
9028 : 13677472 : nblk++;
9029 : 13677472 : todo |= process_bb (avail, bb, false, false, false, eliminate,
9030 : : do_region, exit_bbs,
9031 : 13677472 : skip_entry_phis && bb == entry->dest);
9032 : 13677472 : rpo_state[idx].visited++;
9033 : :
9034 : 33820835 : FOR_EACH_EDGE (e, ei, bb->succs)
9035 : 20143363 : if ((e->flags & EDGE_EXECUTABLE)
9036 : 20067948 : && e->dest->index != EXIT_BLOCK
9037 : 18895990 : && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
9038 : 37705374 : && !rpo_state[bb_to_rpo[e->dest->index]].visited)
9039 : 16580793 : bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
9040 : : }
9041 : 1883872 : }
9042 : :
9043 : : /* If statistics or dump file active. */
9044 : 6233625 : int nex = 0;
9045 : 6233625 : unsigned max_visited = 1;
9046 : 57595218 : for (int i = 0; i < n; ++i)
9047 : : {
9048 : 51361593 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9049 : 51361593 : if (bb->flags & BB_EXECUTABLE)
9050 : 50845208 : nex++;
9051 : 51361593 : statistics_histogram_event (cfun, "RPO block visited times",
9052 : 51361593 : rpo_state[i].visited);
9053 : 51361593 : if (rpo_state[i].visited > max_visited)
9054 : : max_visited = rpo_state[i].visited;
9055 : : }
9056 : 6233625 : unsigned nvalues = 0, navail = 0;
9057 : 170787950 : for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
9058 : 335342275 : i != vn_ssa_aux_hash->end (); ++i)
9059 : : {
9060 : 164554325 : nvalues++;
9061 : 164554325 : vn_avail *av = (*i)->avail;
9062 : 243551848 : while (av)
9063 : : {
9064 : 78997523 : navail++;
9065 : 78997523 : av = av->next;
9066 : : }
9067 : : }
9068 : 6233625 : statistics_counter_event (cfun, "RPO blocks", n);
9069 : 6233625 : statistics_counter_event (cfun, "RPO blocks visited", nblk);
9070 : 6233625 : statistics_counter_event (cfun, "RPO blocks executable", nex);
9071 : 6233625 : statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
9072 : 6233625 : statistics_histogram_event (cfun, "RPO num values", nvalues);
9073 : 6233625 : statistics_histogram_event (cfun, "RPO num avail", navail);
9074 : 6233625 : statistics_histogram_event (cfun, "RPO num lattice",
9075 : 6233625 : vn_ssa_aux_hash->elements ());
9076 : 6233625 : if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
9077 : : {
9078 : 10674 : fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
9079 : : " blocks in total discovering %d executable blocks iterating "
9080 : : "%d.%d times, a block was visited max. %u times\n",
9081 : : n, nblk, nex,
9082 : 10674 : (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
9083 : : max_visited);
9084 : 10674 : fprintf (dump_file, "RPO tracked %d values available at %d locations "
9085 : : "and %" PRIu64 " lattice elements\n",
9086 : 10674 : nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
9087 : : }
9088 : :
9089 : 6233625 : if (eliminate)
9090 : : {
9091 : : /* When !iterate we already performed elimination during the RPO
9092 : : walk. */
9093 : 5244241 : if (iterate)
9094 : : {
9095 : : /* Elimination for region-based VN needs to be done within the
9096 : : RPO walk. */
9097 : 3379750 : gcc_assert (! do_region);
9098 : : /* Note we can't use avail.walk here because that gets confused
9099 : : by the existing availability and it will be less efficient
9100 : : as well. */
9101 : 3379750 : todo |= eliminate_with_rpo_vn (NULL);
9102 : : }
9103 : : else
9104 : 1864491 : todo |= avail.eliminate_cleanup (do_region);
9105 : : }
9106 : :
9107 : 6233625 : vn_valueize = NULL;
9108 : 6233625 : rpo_avail = NULL;
9109 : :
9110 : 6233625 : XDELETEVEC (bb_to_rpo);
9111 : 6233625 : XDELETEVEC (rpo);
9112 : 6233625 : XDELETEVEC (rpo_state);
9113 : :
9114 : 6233625 : return todo;
9115 : 6233625 : }
9116 : :
9117 : : /* Region-based entry for RPO VN. Performs value-numbering and elimination
9118 : : on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
9119 : : the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
9120 : : are not considered.
9121 : : If ITERATE is true then treat backedges optimistically as not
9122 : : executed and iterate. If ELIMINATE is true then perform
9123 : : elimination, otherwise leave that to the caller.
9124 : : If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
9125 : : KIND specifies the amount of work done for handling memory operations. */
9126 : :
9127 : : unsigned
9128 : 701069 : do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
9129 : : bool iterate, bool eliminate, bool skip_entry_phis,
9130 : : vn_lookup_kind kind)
9131 : : {
9132 : 701069 : auto_timevar tv (TV_TREE_RPO_VN);
9133 : 701069 : unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
9134 : : skip_entry_phis, kind);
9135 : 701069 : free_rpo_vn ();
9136 : 1402138 : return todo;
9137 : 701069 : }
9138 : :
9139 : :
9140 : : namespace {
9141 : :
9142 : : const pass_data pass_data_fre =
9143 : : {
9144 : : GIMPLE_PASS, /* type */
9145 : : "fre", /* name */
9146 : : OPTGROUP_NONE, /* optinfo_flags */
9147 : : TV_TREE_FRE, /* tv_id */
9148 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
9149 : : 0, /* properties_provided */
9150 : : 0, /* properties_destroyed */
9151 : : 0, /* todo_flags_start */
9152 : : 0, /* todo_flags_finish */
9153 : : };
9154 : :
9155 : : class pass_fre : public gimple_opt_pass
9156 : : {
9157 : : public:
9158 : 1452085 : pass_fre (gcc::context *ctxt)
9159 : 2904170 : : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
9160 : : {}
9161 : :
9162 : : /* opt_pass methods: */
9163 : 1161668 : opt_pass * clone () final override { return new pass_fre (m_ctxt); }
9164 : 1452085 : void set_pass_param (unsigned int n, bool param) final override
9165 : : {
9166 : 1452085 : gcc_assert (n == 0);
9167 : 1452085 : may_iterate = param;
9168 : 1452085 : }
9169 : 4640830 : bool gate (function *) final override
9170 : : {
9171 : 4640830 : return flag_tree_fre != 0 && (may_iterate || optimize > 1);
9172 : : }
9173 : : unsigned int execute (function *) final override;
9174 : :
9175 : : private:
9176 : : bool may_iterate;
9177 : : }; // class pass_fre
9178 : :
9179 : : unsigned int
9180 : 4562553 : pass_fre::execute (function *fun)
9181 : : {
9182 : 4562553 : unsigned todo = 0;
9183 : :
9184 : : /* At -O[1g] use the cheap non-iterating mode. */
9185 : 4562553 : bool iterate_p = may_iterate && (optimize > 1);
9186 : 4562553 : calculate_dominance_info (CDI_DOMINATORS);
9187 : 4562553 : if (iterate_p)
9188 : 3379750 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
9189 : :
9190 : 4562553 : todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
9191 : 4562553 : free_rpo_vn ();
9192 : :
9193 : 4562553 : if (iterate_p)
9194 : 3379750 : loop_optimizer_finalize ();
9195 : :
9196 : 4562553 : if (scev_initialized_p ())
9197 : 31109 : scev_reset_htab ();
9198 : :
9199 : : /* For late FRE after IVOPTs and unrolling, see if we can
9200 : : remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
9201 : 4562553 : if (!may_iterate)
9202 : 1000889 : todo |= TODO_update_address_taken;
9203 : :
9204 : 4562553 : return todo;
9205 : : }
9206 : :
9207 : : } // anon namespace
9208 : :
9209 : : gimple_opt_pass *
9210 : 290417 : make_pass_fre (gcc::context *ctxt)
9211 : : {
9212 : 290417 : return new pass_fre (ctxt);
9213 : : }
9214 : :
9215 : : #undef BB_EXECUTABLE
|