Branch data Line data Source code
1 : : /* SCC value numbering for trees
2 : : Copyright (C) 2006-2024 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 "splay-tree.h"
25 : : #include "backend.h"
26 : : #include "rtl.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "ssa.h"
30 : : #include "expmed.h"
31 : : #include "insn-config.h"
32 : : #include "memmodel.h"
33 : : #include "emit-rtl.h"
34 : : #include "cgraph.h"
35 : : #include "gimple-pretty-print.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 : 648705757 : vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
159 : : {
160 : 648705757 : 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 : 823081565 : vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
168 : : {
169 : 823081565 : 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 : 22487390 : vn_phi_hasher::hash (const vn_phi_s *vp1)
191 : : {
192 : 22487390 : return vp1->hashcode;
193 : : }
194 : :
195 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
196 : :
197 : : inline bool
198 : 39632923 : vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
199 : : {
200 : 39632923 : 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 : 19800670 : vn_reference_op_eq (const void *p1, const void *p2)
212 : : {
213 : 19800670 : const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
214 : 19800670 : const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
215 : :
216 : 19800670 : return (vro1->opcode == vro2->opcode
217 : : /* We do not care for differences in type qualification. */
218 : 19797621 : && (vro1->type == vro2->type
219 : 692403 : || (vro1->type && vro2->type
220 : 692403 : && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
221 : 692403 : TYPE_MAIN_VARIANT (vro2->type))))
222 : 19193935 : && expressions_equal_p (vro1->op0, vro2->op0)
223 : 19179079 : && expressions_equal_p (vro1->op1, vro2->op1)
224 : 19179079 : && expressions_equal_p (vro1->op2, vro2->op2)
225 : 38979749 : && (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 : 3206791065 : vn_reference_hasher::hash (const vn_reference_s *vr1)
249 : : {
250 : 3206791065 : return vr1->hashcode;
251 : : }
252 : :
253 : : inline bool
254 : 3839794837 : vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
255 : : {
256 : 3839794837 : 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 : 623 : print_vn_reference_ops (FILE *outfile, const vec<vn_reference_op_s> ops)
266 : : {
267 : 623 : vn_reference_op_t vro;
268 : 623 : unsigned int i;
269 : 623 : fprintf (outfile, "{");
270 : 2861 : for (i = 0; ops.iterate (i, &vro); i++)
271 : : {
272 : 2238 : bool closebrace = false;
273 : 2238 : if (vro->opcode != SSA_NAME
274 : 1741 : && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
275 : : {
276 : 1741 : fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
277 : 1741 : if (vro->op0 || vro->opcode == CALL_EXPR)
278 : : {
279 : 1741 : fprintf (outfile, "<");
280 : 1741 : closebrace = true;
281 : : }
282 : : }
283 : 2238 : if (vro->op0 || vro->opcode == CALL_EXPR)
284 : : {
285 : 2238 : if (!vro->op0)
286 : 0 : fprintf (outfile, internal_fn_name ((internal_fn)vro->clique));
287 : : else
288 : 2238 : print_generic_expr (outfile, vro->op0);
289 : 2238 : if (vro->op1)
290 : : {
291 : 416 : fprintf (outfile, ",");
292 : 416 : print_generic_expr (outfile, vro->op1);
293 : : }
294 : 2238 : if (vro->op2)
295 : : {
296 : 416 : fprintf (outfile, ",");
297 : 416 : print_generic_expr (outfile, vro->op2);
298 : : }
299 : : }
300 : 2238 : if (closebrace)
301 : 1741 : fprintf (outfile, ">");
302 : 2238 : if (i != ops.length () - 1)
303 : 1615 : fprintf (outfile, ",");
304 : : }
305 : 623 : fprintf (outfile, "}");
306 : 623 : }
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 : 10764464 : vn_constant_hasher::hash (const vn_constant_s *vc1)
337 : : {
338 : 10764464 : return vc1->hashcode;
339 : : }
340 : :
341 : : /* Hash table equality function for vn_constant_t. */
342 : :
343 : : inline bool
344 : 12945283 : vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
345 : : {
346 : 12945283 : if (vc1->hashcode != vc2->hashcode)
347 : : return false;
348 : :
349 : 1912884 : 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 : :
370 : : /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
371 : : an SSA name, otherwise just return it. */
372 : : tree (*vn_valueize) (tree);
373 : : static tree
374 : 27925 : vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
375 : : {
376 : 27925 : basic_block saved_vn_context_bb = vn_context_bb;
377 : : /* Look for sth available at the definition block of the argument.
378 : : This avoids inconsistencies between availability there which
379 : : decides if the stmt can be removed and availability at the
380 : : use site. The SSA property ensures that things available
381 : : at the definition are also available at uses. */
382 : 27925 : if (!SSA_NAME_IS_DEFAULT_DEF (t))
383 : 26788 : vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
384 : 27925 : tree res = vn_valueize (t);
385 : 27925 : vn_context_bb = saved_vn_context_bb;
386 : 27925 : return res;
387 : : }
388 : :
389 : :
390 : : /* This represents the top of the VN lattice, which is the universal
391 : : value. */
392 : :
393 : : tree VN_TOP;
394 : :
395 : : /* Unique counter for our value ids. */
396 : :
397 : : static unsigned int next_value_id;
398 : : static int next_constant_value_id;
399 : :
400 : :
401 : : /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
402 : : are allocated on an obstack for locality reasons, and to free them
403 : : without looping over the vec. */
404 : :
405 : : struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
406 : : {
407 : : typedef vn_ssa_aux_t value_type;
408 : : typedef tree compare_type;
409 : : static inline hashval_t hash (const value_type &);
410 : : static inline bool equal (const value_type &, const compare_type &);
411 : : static inline void mark_deleted (value_type &) {}
412 : : static const bool empty_zero_p = true;
413 : 0 : static inline void mark_empty (value_type &e) { e = NULL; }
414 : : static inline bool is_deleted (value_type &) { return false; }
415 : >11088*10^7 : static inline bool is_empty (value_type &e) { return e == NULL; }
416 : : };
417 : :
418 : : hashval_t
419 : 36779509162 : vn_ssa_aux_hasher::hash (const value_type &entry)
420 : : {
421 : 36779509162 : return SSA_NAME_VERSION (entry->name);
422 : : }
423 : :
424 : : bool
425 : 41984049059 : vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
426 : : {
427 : 41984049059 : return name == entry->name;
428 : : }
429 : :
430 : : static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
431 : : typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
432 : : static struct obstack vn_ssa_aux_obstack;
433 : :
434 : : static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
435 : : static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
436 : : vn_nary_op_table_type *);
437 : : static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
438 : : enum tree_code, tree, tree *);
439 : : static tree vn_lookup_simplify_result (gimple_match_op *);
440 : : static vn_reference_t vn_reference_lookup_or_insert_for_pieces
441 : : (tree, alias_set_type, alias_set_type, poly_int64, poly_int64, tree,
442 : : vec<vn_reference_op_s, va_heap>, tree);
443 : :
444 : : /* Return whether there is value numbering information for a given SSA name. */
445 : :
446 : : bool
447 : 4422062 : has_VN_INFO (tree name)
448 : : {
449 : 4422062 : return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
450 : : }
451 : :
452 : : vn_ssa_aux_t
453 : 3012534384 : VN_INFO (tree name)
454 : : {
455 : 3012534384 : vn_ssa_aux_t *res
456 : 3012534384 : = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
457 : : INSERT);
458 : 3012534384 : if (*res != NULL)
459 : : return *res;
460 : :
461 : 153444032 : vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
462 : 153444032 : memset (newinfo, 0, sizeof (struct vn_ssa_aux));
463 : 153444032 : newinfo->name = name;
464 : 153444032 : newinfo->valnum = VN_TOP;
465 : : /* We are using the visited flag to handle uses with defs not within the
466 : : region being value-numbered. */
467 : 153444032 : newinfo->visited = false;
468 : :
469 : : /* Given we create the VN_INFOs on-demand now we have to do initialization
470 : : different than VN_TOP here. */
471 : 153444032 : if (SSA_NAME_IS_DEFAULT_DEF (name))
472 : 8556545 : switch (TREE_CODE (SSA_NAME_VAR (name)))
473 : : {
474 : 1539301 : case VAR_DECL:
475 : : /* All undefined vars are VARYING. */
476 : 1539301 : newinfo->valnum = name;
477 : 1539301 : newinfo->visited = true;
478 : 1539301 : break;
479 : :
480 : 6968251 : case PARM_DECL:
481 : : /* Parameters are VARYING but we can record a condition
482 : : if we know it is a non-NULL pointer. */
483 : 6968251 : newinfo->visited = true;
484 : 6968251 : newinfo->valnum = name;
485 : 10600943 : if (POINTER_TYPE_P (TREE_TYPE (name))
486 : 7886520 : && nonnull_arg_p (SSA_NAME_VAR (name)))
487 : : {
488 : 2026414 : tree ops[2];
489 : 2026414 : ops[0] = name;
490 : 2026414 : ops[1] = build_int_cst (TREE_TYPE (name), 0);
491 : 2026414 : vn_nary_op_t nary;
492 : : /* Allocate from non-unwinding stack. */
493 : 2026414 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
494 : 2026414 : init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
495 : : boolean_type_node, ops);
496 : 2026414 : nary->predicated_values = 0;
497 : 2026414 : nary->u.result = boolean_true_node;
498 : 2026414 : vn_nary_op_insert_into (nary, valid_info->nary);
499 : 2026414 : gcc_assert (nary->unwind_to == NULL);
500 : : /* Also do not link it into the undo chain. */
501 : 2026414 : last_inserted_nary = nary->next;
502 : 2026414 : nary->next = (vn_nary_op_t)(void *)-1;
503 : 2026414 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
504 : 2026414 : init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
505 : : boolean_type_node, ops);
506 : 2026414 : nary->predicated_values = 0;
507 : 2026414 : nary->u.result = boolean_false_node;
508 : 2026414 : vn_nary_op_insert_into (nary, valid_info->nary);
509 : 2026414 : gcc_assert (nary->unwind_to == NULL);
510 : 2026414 : last_inserted_nary = nary->next;
511 : 2026414 : nary->next = (vn_nary_op_t)(void *)-1;
512 : 2026414 : if (dump_file && (dump_flags & TDF_DETAILS))
513 : : {
514 : 43 : fprintf (dump_file, "Recording ");
515 : 43 : print_generic_expr (dump_file, name, TDF_SLIM);
516 : 43 : fprintf (dump_file, " != 0\n");
517 : : }
518 : : }
519 : : break;
520 : :
521 : 48993 : case RESULT_DECL:
522 : : /* If the result is passed by invisible reference the default
523 : : def is initialized, otherwise it's uninitialized. Still
524 : : undefined is varying. */
525 : 48993 : newinfo->visited = true;
526 : 48993 : newinfo->valnum = name;
527 : 48993 : break;
528 : :
529 : 0 : default:
530 : 0 : gcc_unreachable ();
531 : : }
532 : : return newinfo;
533 : : }
534 : :
535 : : /* Return the SSA value of X. */
536 : :
537 : : inline tree
538 : 2962093947 : SSA_VAL (tree x, bool *visited = NULL)
539 : : {
540 : 2962093947 : vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
541 : 2962093947 : if (visited)
542 : 1187402191 : *visited = tem && tem->visited;
543 : 2962093947 : return tem && tem->visited ? tem->valnum : x;
544 : : }
545 : :
546 : : /* Return the SSA value of the VUSE x, supporting released VDEFs
547 : : during elimination which will value-number the VDEF to the
548 : : associated VUSE (but not substitute in the whole lattice). */
549 : :
550 : : static inline tree
551 : 1098626331 : vuse_ssa_val (tree x)
552 : : {
553 : 1098626331 : if (!x)
554 : : return NULL_TREE;
555 : :
556 : 1095578745 : do
557 : : {
558 : 1095578745 : x = SSA_VAL (x);
559 : 1095578745 : gcc_assert (x != VN_TOP);
560 : : }
561 : 1095578745 : while (SSA_NAME_IN_FREE_LIST (x));
562 : :
563 : : return x;
564 : : }
565 : :
566 : : /* Similar to the above but used as callback for walk_non_aliased_vuses
567 : : and thus should stop at unvisited VUSE to not walk across region
568 : : boundaries. */
569 : :
570 : : static tree
571 : 923030196 : vuse_valueize (tree vuse)
572 : : {
573 : 923030196 : do
574 : : {
575 : 923030196 : bool visited;
576 : 923030196 : vuse = SSA_VAL (vuse, &visited);
577 : 923030196 : if (!visited)
578 : 13204189 : return NULL_TREE;
579 : 909826007 : gcc_assert (vuse != VN_TOP);
580 : : }
581 : 909826007 : while (SSA_NAME_IN_FREE_LIST (vuse));
582 : : return vuse;
583 : : }
584 : :
585 : :
586 : : /* Return the vn_kind the expression computed by the stmt should be
587 : : associated with. */
588 : :
589 : : enum vn_kind
590 : 90393638 : vn_get_stmt_kind (gimple *stmt)
591 : : {
592 : 90393638 : switch (gimple_code (stmt))
593 : : {
594 : : case GIMPLE_CALL:
595 : : return VN_REFERENCE;
596 : : case GIMPLE_PHI:
597 : : return VN_PHI;
598 : 90393638 : case GIMPLE_ASSIGN:
599 : 90393638 : {
600 : 90393638 : enum tree_code code = gimple_assign_rhs_code (stmt);
601 : 90393638 : tree rhs1 = gimple_assign_rhs1 (stmt);
602 : 90393638 : switch (get_gimple_rhs_class (code))
603 : : {
604 : : case GIMPLE_UNARY_RHS:
605 : : case GIMPLE_BINARY_RHS:
606 : : case GIMPLE_TERNARY_RHS:
607 : : return VN_NARY;
608 : 42857766 : case GIMPLE_SINGLE_RHS:
609 : 42857766 : switch (TREE_CODE_CLASS (code))
610 : : {
611 : 32003617 : case tcc_reference:
612 : : /* VOP-less references can go through unary case. */
613 : 32003617 : if ((code == REALPART_EXPR
614 : : || code == IMAGPART_EXPR
615 : 32003617 : || code == VIEW_CONVERT_EXPR
616 : 32003617 : || code == BIT_FIELD_REF)
617 : 32003617 : && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
618 : 593908 : || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
619 : 1879457 : return VN_NARY;
620 : :
621 : : /* Fallthrough. */
622 : : case tcc_declaration:
623 : : return VN_REFERENCE;
624 : :
625 : : case tcc_constant:
626 : : return VN_CONSTANT;
627 : :
628 : 5491446 : default:
629 : 5491446 : if (code == ADDR_EXPR)
630 : 3014791 : return (is_gimple_min_invariant (rhs1)
631 : 3014791 : ? VN_CONSTANT : VN_REFERENCE);
632 : 2476655 : else if (code == CONSTRUCTOR)
633 : : return VN_NARY;
634 : : return VN_NONE;
635 : : }
636 : : default:
637 : : return VN_NONE;
638 : : }
639 : : }
640 : : default:
641 : : return VN_NONE;
642 : : }
643 : : }
644 : :
645 : : /* Lookup a value id for CONSTANT and return it. If it does not
646 : : exist returns 0. */
647 : :
648 : : unsigned int
649 : 0 : get_constant_value_id (tree constant)
650 : : {
651 : 0 : vn_constant_s **slot;
652 : 0 : struct vn_constant_s vc;
653 : :
654 : 0 : vc.hashcode = vn_hash_constant_with_type (constant);
655 : 0 : vc.constant = constant;
656 : 0 : slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
657 : 0 : if (slot)
658 : 0 : return (*slot)->value_id;
659 : : return 0;
660 : : }
661 : :
662 : : /* Lookup a value id for CONSTANT, and if it does not exist, create a
663 : : new one and return it. If it does exist, return it. */
664 : :
665 : : unsigned int
666 : 25443874 : get_or_alloc_constant_value_id (tree constant)
667 : : {
668 : 25443874 : vn_constant_s **slot;
669 : 25443874 : struct vn_constant_s vc;
670 : 25443874 : vn_constant_t vcp;
671 : :
672 : : /* If the hashtable isn't initialized we're not running from PRE and thus
673 : : do not need value-ids. */
674 : 25443874 : if (!constant_to_value_id)
675 : : return 0;
676 : :
677 : 4218821 : vc.hashcode = vn_hash_constant_with_type (constant);
678 : 4218821 : vc.constant = constant;
679 : 4218821 : slot = constant_to_value_id->find_slot (&vc, INSERT);
680 : 4218821 : if (*slot)
681 : 1898596 : return (*slot)->value_id;
682 : :
683 : 2320225 : vcp = XNEW (struct vn_constant_s);
684 : 2320225 : vcp->hashcode = vc.hashcode;
685 : 2320225 : vcp->constant = constant;
686 : 2320225 : vcp->value_id = get_next_constant_value_id ();
687 : 2320225 : *slot = vcp;
688 : 2320225 : return vcp->value_id;
689 : : }
690 : :
691 : : /* Compute the hash for a reference operand VRO1. */
692 : :
693 : : static void
694 : 117480721 : vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
695 : : {
696 : 117480721 : hstate.add_int (vro1->opcode);
697 : 117480721 : if (vro1->opcode == CALL_EXPR && !vro1->op0)
698 : 472057 : hstate.add_int (vro1->clique);
699 : 117480721 : if (vro1->op0)
700 : 112404423 : inchash::add_expr (vro1->op0, hstate);
701 : 117480721 : if (vro1->op1)
702 : 9845541 : inchash::add_expr (vro1->op1, hstate);
703 : 117480721 : if (vro1->op2)
704 : 11446693 : inchash::add_expr (vro1->op2, hstate);
705 : 117480721 : }
706 : :
707 : : /* Compute a hash for the reference operation VR1 and return it. */
708 : :
709 : : static hashval_t
710 : 176348739 : vn_reference_compute_hash (const vn_reference_t vr1)
711 : : {
712 : 176348739 : inchash::hash hstate;
713 : 176348739 : hashval_t result;
714 : 176348739 : int i;
715 : 176348739 : vn_reference_op_t vro;
716 : 176348739 : poly_int64 off = -1;
717 : 176348739 : bool deref = false;
718 : :
719 : 729556384 : FOR_EACH_VEC_ELT (vr1->operands, i, vro)
720 : : {
721 : 553207645 : if (vro->opcode == MEM_REF)
722 : : deref = true;
723 : 386406785 : else if (vro->opcode != ADDR_EXPR)
724 : 272654899 : deref = false;
725 : 553207645 : if (maybe_ne (vro->off, -1))
726 : : {
727 : 329074130 : if (known_eq (off, -1))
728 : 168846568 : off = 0;
729 : 553207645 : off += vro->off;
730 : : }
731 : : else
732 : : {
733 : 224133515 : if (maybe_ne (off, -1)
734 : 224133515 : && maybe_ne (off, 0))
735 : 89893455 : hstate.add_poly_int (off);
736 : 224133515 : off = -1;
737 : 224133515 : if (deref
738 : 106812149 : && vro->opcode == ADDR_EXPR)
739 : : {
740 : 106652794 : if (vro->op0)
741 : : {
742 : 106652794 : tree op = TREE_OPERAND (vro->op0, 0);
743 : 106652794 : hstate.add_int (TREE_CODE (op));
744 : 106652794 : inchash::add_expr (op, hstate);
745 : : }
746 : : }
747 : : else
748 : 117480721 : vn_reference_op_compute_hash (vro, hstate);
749 : : }
750 : : }
751 : : /* Do not hash vr1->offset or vr1->max_size, we want to get collisions
752 : : to be able to identify compatible results. */
753 : 176348739 : result = hstate.end ();
754 : : /* ??? We would ICE later if we hash instead of adding that in. */
755 : 176348739 : if (vr1->vuse)
756 : 172748291 : result += SSA_NAME_VERSION (vr1->vuse);
757 : :
758 : 176348739 : return result;
759 : : }
760 : :
761 : : /* Return true if reference operations VR1 and VR2 are equivalent. This
762 : : means they have the same set of operands and vuses. */
763 : :
764 : : bool
765 : 3836289812 : vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
766 : : {
767 : 3836289812 : unsigned i, j;
768 : :
769 : : /* Early out if this is not a hash collision. */
770 : 3836289812 : if (vr1->hashcode != vr2->hashcode)
771 : : return false;
772 : :
773 : : /* The VOP needs to be the same. */
774 : 14879428 : if (vr1->vuse != vr2->vuse)
775 : : return false;
776 : :
777 : : /* The offset/max_size used for the ao_ref during lookup has to be
778 : : the same. */
779 : 14878849 : if (maybe_ne (vr1->offset, vr2->offset)
780 : 14878849 : || maybe_ne (vr1->max_size, vr2->max_size))
781 : : {
782 : : /* But nothing known in the prevailing entry is OK to be used. */
783 : 5406013 : if (maybe_ne (vr1->offset, 0) || known_size_p (vr1->max_size))
784 : : return false;
785 : : }
786 : :
787 : : /* If the operands are the same we are done. */
788 : 29719280 : if (vr1->operands == vr2->operands)
789 : : return true;
790 : :
791 : 14161518 : if (!vr1->type || !vr2->type)
792 : : {
793 : 302454 : if (vr1->type != vr2->type)
794 : : return false;
795 : : }
796 : 13859064 : else if (vr1->type == vr2->type)
797 : : ;
798 : 1620224 : else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
799 : 1620224 : || (COMPLETE_TYPE_P (vr1->type)
800 : 1620224 : && !expressions_equal_p (TYPE_SIZE (vr1->type),
801 : 1620224 : TYPE_SIZE (vr2->type))))
802 : 745917 : return false;
803 : 874307 : else if (vr1->operands[0].opcode == CALL_EXPR
804 : 874307 : && !types_compatible_p (vr1->type, vr2->type))
805 : : return false;
806 : 874307 : else if (INTEGRAL_TYPE_P (vr1->type)
807 : 343754 : && INTEGRAL_TYPE_P (vr2->type))
808 : : {
809 : 336273 : if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
810 : : return false;
811 : : }
812 : 538034 : else if (INTEGRAL_TYPE_P (vr1->type)
813 : 538034 : && (TYPE_PRECISION (vr1->type)
814 : 7481 : != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
815 : : return false;
816 : 537902 : else if (INTEGRAL_TYPE_P (vr2->type)
817 : 537902 : && (TYPE_PRECISION (vr2->type)
818 : 6778 : != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
819 : : return false;
820 : 6125 : else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
821 : 537308 : && VECTOR_BOOLEAN_TYPE_P (vr2->type))
822 : : {
823 : : /* Vector boolean types can have padding, verify we are dealing with
824 : : the same number of elements, aka the precision of the types.
825 : : For example, In most architecture the precision_size of vbool*_t
826 : : types are caculated like below:
827 : : precision_size = type_size * 8
828 : :
829 : : Unfortunately, the RISC-V will adjust the precision_size for the
830 : : vbool*_t in order to align the ISA as below:
831 : : type_size = [1, 1, 1, 1, 2, 4, 8]
832 : : precision_size = [1, 2, 4, 8, 16, 32, 64]
833 : :
834 : : Then the precision_size of RISC-V vbool*_t will not be the multiple
835 : : of the type_size. We take care of this case consolidated here. */
836 : 0 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1->type),
837 : 0 : TYPE_VECTOR_SUBPARTS (vr2->type)))
838 : : return false;
839 : : }
840 : :
841 : : i = 0;
842 : : j = 0;
843 : 16383569 : do
844 : : {
845 : 16383569 : poly_int64 off1 = 0, off2 = 0;
846 : 16383569 : vn_reference_op_t vro1, vro2;
847 : 16383569 : vn_reference_op_s tem1, tem2;
848 : 16383569 : bool deref1 = false, deref2 = false;
849 : 16383569 : bool reverse1 = false, reverse2 = false;
850 : 55837912 : for (; vr1->operands.iterate (i, &vro1); i++)
851 : : {
852 : 39454343 : if (vro1->opcode == MEM_REF)
853 : : deref1 = true;
854 : : /* Do not look through a storage order barrier. */
855 : 27072669 : else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
856 : 4266 : return false;
857 : 39454343 : reverse1 |= vro1->reverse;
858 : 39454343 : if (known_eq (vro1->off, -1))
859 : : break;
860 : 23070774 : off1 += vro1->off;
861 : : }
862 : 39456423 : for (; vr2->operands.iterate (j, &vro2); j++)
863 : : {
864 : 39456423 : if (vro2->opcode == MEM_REF)
865 : : deref2 = true;
866 : : /* Do not look through a storage order barrier. */
867 : 27074739 : else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
868 : : return false;
869 : 39456423 : reverse2 |= vro2->reverse;
870 : 39456423 : if (known_eq (vro2->off, -1))
871 : : break;
872 : 23072854 : off2 += vro2->off;
873 : : }
874 : 16383569 : if (maybe_ne (off1, off2) || reverse1 != reverse2)
875 : : return false;
876 : 16383435 : if (deref1 && vro1->opcode == ADDR_EXPR)
877 : : {
878 : 6899270 : memset (&tem1, 0, sizeof (tem1));
879 : 6899270 : tem1.op0 = TREE_OPERAND (vro1->op0, 0);
880 : 6899270 : tem1.type = TREE_TYPE (tem1.op0);
881 : 6899270 : tem1.opcode = TREE_CODE (tem1.op0);
882 : 6899270 : vro1 = &tem1;
883 : 6899270 : deref1 = false;
884 : : }
885 : 16383435 : if (deref2 && vro2->opcode == ADDR_EXPR)
886 : : {
887 : 6899280 : memset (&tem2, 0, sizeof (tem2));
888 : 6899280 : tem2.op0 = TREE_OPERAND (vro2->op0, 0);
889 : 6899280 : tem2.type = TREE_TYPE (tem2.op0);
890 : 6899280 : tem2.opcode = TREE_CODE (tem2.op0);
891 : 6899280 : vro2 = &tem2;
892 : 6899280 : deref2 = false;
893 : : }
894 : 16383435 : if (deref1 != deref2)
895 : : return false;
896 : 16383435 : if (!vn_reference_op_eq (vro1, vro2))
897 : : return false;
898 : 16379303 : ++j;
899 : 16379303 : ++i;
900 : : }
901 : 32758606 : while (vr1->operands.length () != i
902 : 49137909 : || vr2->operands.length () != j);
903 : :
904 : : return true;
905 : : }
906 : :
907 : : /* Copy the operations present in load/store REF into RESULT, a vector of
908 : : vn_reference_op_s's. */
909 : :
910 : : static void
911 : 195976638 : copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
912 : : {
913 : : /* For non-calls, store the information that makes up the address. */
914 : 195976638 : tree orig = ref;
915 : 681158816 : while (ref)
916 : : {
917 : 485182178 : vn_reference_op_s temp;
918 : :
919 : 485182178 : memset (&temp, 0, sizeof (temp));
920 : 485182178 : temp.type = TREE_TYPE (ref);
921 : 485182178 : temp.opcode = TREE_CODE (ref);
922 : 485182178 : temp.off = -1;
923 : :
924 : 485182178 : switch (temp.opcode)
925 : : {
926 : 13876730 : case MODIFY_EXPR:
927 : 13876730 : temp.op0 = TREE_OPERAND (ref, 1);
928 : 13876730 : break;
929 : 137 : case WITH_SIZE_EXPR:
930 : 137 : temp.op0 = TREE_OPERAND (ref, 1);
931 : 137 : temp.off = 0;
932 : 137 : break;
933 : 95044319 : case MEM_REF:
934 : : /* The base address gets its own vn_reference_op_s structure. */
935 : 95044319 : temp.op0 = TREE_OPERAND (ref, 1);
936 : 95044319 : if (!mem_ref_offset (ref).to_shwi (&temp.off))
937 : 0 : temp.off = -1;
938 : 95044319 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
939 : 95044319 : temp.base = MR_DEPENDENCE_BASE (ref);
940 : 95044319 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
941 : 95044319 : break;
942 : 2220732 : case TARGET_MEM_REF:
943 : : /* The base address gets its own vn_reference_op_s structure. */
944 : 2220732 : temp.op0 = TMR_INDEX (ref);
945 : 2220732 : temp.op1 = TMR_STEP (ref);
946 : 2220732 : temp.op2 = TMR_OFFSET (ref);
947 : 2220732 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
948 : 2220732 : temp.base = MR_DEPENDENCE_BASE (ref);
949 : 2220732 : result->safe_push (temp);
950 : 2220732 : memset (&temp, 0, sizeof (temp));
951 : 2220732 : temp.type = NULL_TREE;
952 : 2220732 : temp.opcode = ERROR_MARK;
953 : 2220732 : temp.op0 = TMR_INDEX2 (ref);
954 : 2220732 : temp.off = -1;
955 : 2220732 : break;
956 : 592147 : case BIT_FIELD_REF:
957 : : /* Record bits, position and storage order. */
958 : 592147 : temp.op0 = TREE_OPERAND (ref, 1);
959 : 592147 : temp.op1 = TREE_OPERAND (ref, 2);
960 : 1183248 : if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
961 : 1046 : temp.off = -1;
962 : 592147 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
963 : 592147 : break;
964 : 137238020 : case COMPONENT_REF:
965 : : /* The field decl is enough to unambiguously specify the field,
966 : : so use its type here. */
967 : 137238020 : temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
968 : 137238020 : temp.op0 = TREE_OPERAND (ref, 1);
969 : 137238020 : temp.op1 = TREE_OPERAND (ref, 2);
970 : 274473690 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
971 : 274471865 : && TYPE_REVERSE_STORAGE_ORDER
972 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
973 : 137238020 : {
974 : 137238020 : tree this_offset = component_ref_field_offset (ref);
975 : 137238020 : if (this_offset
976 : 137238020 : && poly_int_tree_p (this_offset))
977 : : {
978 : 137235938 : tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
979 : 137235938 : if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
980 : : {
981 : 136819853 : poly_offset_int off
982 : 136819853 : = (wi::to_poly_offset (this_offset)
983 : 136819853 : + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
984 : : /* Probibit value-numbering zero offset components
985 : : of addresses the same before the pass folding
986 : : __builtin_object_size had a chance to run. */
987 : 136819853 : if (TREE_CODE (orig) != ADDR_EXPR
988 : 5713609 : || maybe_ne (off, 0)
989 : 139410056 : || (cfun->curr_properties & PROP_objsz))
990 : 135614004 : off.to_shwi (&temp.off);
991 : : }
992 : : }
993 : : }
994 : : break;
995 : 34007534 : case ARRAY_RANGE_REF:
996 : 34007534 : case ARRAY_REF:
997 : 34007534 : {
998 : 34007534 : tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
999 : : /* Record index as operand. */
1000 : 34007534 : temp.op0 = TREE_OPERAND (ref, 1);
1001 : : /* Always record lower bounds and element size. */
1002 : 34007534 : temp.op1 = array_ref_low_bound (ref);
1003 : : /* But record element size in units of the type alignment. */
1004 : 34007534 : temp.op2 = TREE_OPERAND (ref, 3);
1005 : 34007534 : temp.align = eltype->type_common.align;
1006 : 34007534 : if (! temp.op2)
1007 : 33760540 : temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
1008 : : size_int (TYPE_ALIGN_UNIT (eltype)));
1009 : 34007534 : if (poly_int_tree_p (temp.op0)
1010 : 20570659 : && poly_int_tree_p (temp.op1)
1011 : 54578193 : && TREE_CODE (temp.op2) == INTEGER_CST)
1012 : : {
1013 : 40993710 : poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1014 : 61490565 : - wi::to_poly_offset (temp.op1))
1015 : 40993710 : * wi::to_offset (temp.op2)
1016 : 20496855 : * vn_ref_op_align_unit (&temp));
1017 : 20496855 : off.to_shwi (&temp.off);
1018 : : }
1019 : 34007534 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1020 : 34007534 : && TYPE_REVERSE_STORAGE_ORDER
1021 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1022 : : }
1023 : 34007534 : break;
1024 : 78513635 : case VAR_DECL:
1025 : 78513635 : if (DECL_HARD_REGISTER (ref))
1026 : : {
1027 : 24459 : temp.op0 = ref;
1028 : 24459 : break;
1029 : : }
1030 : : /* Fallthru. */
1031 : 82659219 : case PARM_DECL:
1032 : 82659219 : case CONST_DECL:
1033 : 82659219 : case RESULT_DECL:
1034 : : /* Canonicalize decls to MEM[&decl] which is what we end up with
1035 : : when valueizing MEM[ptr] with ptr = &decl. */
1036 : 82659219 : temp.opcode = MEM_REF;
1037 : 82659219 : temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1038 : 82659219 : temp.off = 0;
1039 : 82659219 : result->safe_push (temp);
1040 : 82659219 : temp.opcode = ADDR_EXPR;
1041 : 82659219 : temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1042 : 82659219 : temp.type = TREE_TYPE (temp.op0);
1043 : 82659219 : temp.off = -1;
1044 : 82659219 : break;
1045 : 82876984 : case STRING_CST:
1046 : 82876984 : case INTEGER_CST:
1047 : 82876984 : case POLY_INT_CST:
1048 : 82876984 : case COMPLEX_CST:
1049 : 82876984 : case VECTOR_CST:
1050 : 82876984 : case REAL_CST:
1051 : 82876984 : case FIXED_CST:
1052 : 82876984 : case CONSTRUCTOR:
1053 : 82876984 : case SSA_NAME:
1054 : 82876984 : temp.op0 = ref;
1055 : 82876984 : break;
1056 : 34283464 : case ADDR_EXPR:
1057 : 34283464 : if (is_gimple_min_invariant (ref))
1058 : : {
1059 : 30415976 : temp.op0 = ref;
1060 : 30415976 : break;
1061 : : }
1062 : : break;
1063 : : /* These are only interesting for their operands, their
1064 : : existence, and their type. They will never be the last
1065 : : ref in the chain of references (IE they require an
1066 : : operand), so we don't have to put anything
1067 : : for op* as it will be handled by the iteration */
1068 : 441276 : case REALPART_EXPR:
1069 : 441276 : temp.off = 0;
1070 : 441276 : break;
1071 : 1471089 : case VIEW_CONVERT_EXPR:
1072 : 1471089 : temp.off = 0;
1073 : 1471089 : temp.reverse = storage_order_barrier_p (ref);
1074 : 1471089 : break;
1075 : 446068 : case IMAGPART_EXPR:
1076 : : /* This is only interesting for its constant offset. */
1077 : 446068 : temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1078 : 446068 : break;
1079 : 0 : default:
1080 : 0 : gcc_unreachable ();
1081 : : }
1082 : 485182178 : result->safe_push (temp);
1083 : :
1084 : 485182178 : if (REFERENCE_CLASS_P (ref)
1085 : 213720993 : || TREE_CODE (ref) == MODIFY_EXPR
1086 : 199844263 : || TREE_CODE (ref) == WITH_SIZE_EXPR
1087 : 685026304 : || (TREE_CODE (ref) == ADDR_EXPR
1088 : 34283464 : && !is_gimple_min_invariant (ref)))
1089 : 289205540 : ref = TREE_OPERAND (ref, 0);
1090 : : else
1091 : : ref = NULL_TREE;
1092 : : }
1093 : 195976638 : }
1094 : :
1095 : : /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1096 : : operands in *OPS, the reference alias set SET and the reference type TYPE.
1097 : : Return true if something useful was produced. */
1098 : :
1099 : : bool
1100 : 12667797 : ao_ref_init_from_vn_reference (ao_ref *ref,
1101 : : alias_set_type set, alias_set_type base_set,
1102 : : tree type, const vec<vn_reference_op_s> &ops)
1103 : : {
1104 : 12667797 : unsigned i;
1105 : 12667797 : tree base = NULL_TREE;
1106 : 12667797 : tree *op0_p = &base;
1107 : 12667797 : poly_offset_int offset = 0;
1108 : 12667797 : poly_offset_int max_size;
1109 : 12667797 : poly_offset_int size = -1;
1110 : 12667797 : tree size_tree = NULL_TREE;
1111 : :
1112 : : /* We don't handle calls. */
1113 : 12667797 : if (!type)
1114 : : return false;
1115 : :
1116 : 12667797 : machine_mode mode = TYPE_MODE (type);
1117 : 12667797 : if (mode == BLKmode)
1118 : 263663 : size_tree = TYPE_SIZE (type);
1119 : : else
1120 : 24808268 : size = GET_MODE_BITSIZE (mode);
1121 : 12404134 : if (size_tree != NULL_TREE
1122 : 263663 : && poly_int_tree_p (size_tree))
1123 : 263663 : size = wi::to_poly_offset (size_tree);
1124 : :
1125 : : /* Lower the final access size from the outermost expression. */
1126 : 12667797 : const_vn_reference_op_t cst_op = &ops[0];
1127 : : /* Cast away constness for the sake of the const-unsafe
1128 : : FOR_EACH_VEC_ELT(). */
1129 : 12667797 : vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1130 : 12667797 : size_tree = NULL_TREE;
1131 : 12667797 : if (op->opcode == COMPONENT_REF)
1132 : 4669841 : size_tree = DECL_SIZE (op->op0);
1133 : 7997956 : else if (op->opcode == BIT_FIELD_REF)
1134 : 48372 : size_tree = op->op0;
1135 : 4718213 : if (size_tree != NULL_TREE
1136 : 4718213 : && poly_int_tree_p (size_tree)
1137 : 9436426 : && (!known_size_p (size)
1138 : 12667797 : || known_lt (wi::to_poly_offset (size_tree), size)))
1139 : 28612 : size = wi::to_poly_offset (size_tree);
1140 : :
1141 : : /* Initially, maxsize is the same as the accessed element size.
1142 : : In the following it will only grow (or become -1). */
1143 : 12667797 : max_size = size;
1144 : :
1145 : : /* Compute cumulative bit-offset for nested component-refs and array-refs,
1146 : : and find the ultimate containing object. */
1147 : 49010651 : FOR_EACH_VEC_ELT (ops, i, op)
1148 : : {
1149 : 36700391 : switch (op->opcode)
1150 : : {
1151 : : /* These may be in the reference ops, but we cannot do anything
1152 : : sensible with them here. */
1153 : 6036955 : case ADDR_EXPR:
1154 : : /* Apart from ADDR_EXPR arguments to MEM_REF. */
1155 : 6036955 : if (base != NULL_TREE
1156 : 6036955 : && TREE_CODE (base) == MEM_REF
1157 : 6036955 : && op->op0
1158 : 12073910 : && DECL_P (TREE_OPERAND (op->op0, 0)))
1159 : : {
1160 : 6033673 : const_vn_reference_op_t pop = &ops[i-1];
1161 : 6033673 : base = TREE_OPERAND (op->op0, 0);
1162 : 6033673 : if (known_eq (pop->off, -1))
1163 : : {
1164 : 32 : max_size = -1;
1165 : 32 : offset = 0;
1166 : : }
1167 : : else
1168 : 6033641 : offset += pop->off * BITS_PER_UNIT;
1169 : : op0_p = NULL;
1170 : : break;
1171 : : }
1172 : : /* Fallthru. */
1173 : : case CALL_EXPR:
1174 : : return false;
1175 : :
1176 : : /* Record the base objects. */
1177 : 12314930 : case MEM_REF:
1178 : 12314930 : *op0_p = build2 (MEM_REF, op->type,
1179 : : NULL_TREE, op->op0);
1180 : 12314930 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1181 : 12314930 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1182 : 12314930 : op0_p = &TREE_OPERAND (*op0_p, 0);
1183 : 12314930 : break;
1184 : :
1185 : 6276587 : case VAR_DECL:
1186 : 6276587 : case PARM_DECL:
1187 : 6276587 : case RESULT_DECL:
1188 : 6276587 : case SSA_NAME:
1189 : 6276587 : *op0_p = op->op0;
1190 : 6276587 : op0_p = NULL;
1191 : 6276587 : break;
1192 : :
1193 : : /* And now the usual component-reference style ops. */
1194 : 48372 : case BIT_FIELD_REF:
1195 : 48372 : offset += wi::to_poly_offset (op->op1);
1196 : 48372 : break;
1197 : :
1198 : 7718849 : case COMPONENT_REF:
1199 : 7718849 : {
1200 : 7718849 : tree field = op->op0;
1201 : : /* We do not have a complete COMPONENT_REF tree here so we
1202 : : cannot use component_ref_field_offset. Do the interesting
1203 : : parts manually. */
1204 : 7718849 : tree this_offset = DECL_FIELD_OFFSET (field);
1205 : :
1206 : 7718849 : if (op->op1 || !poly_int_tree_p (this_offset))
1207 : 189 : max_size = -1;
1208 : : else
1209 : : {
1210 : 7718660 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1211 : 7718660 : << LOG2_BITS_PER_UNIT);
1212 : 7718660 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1213 : 7718660 : offset += woffset;
1214 : : }
1215 : : break;
1216 : : }
1217 : :
1218 : 2644529 : case ARRAY_RANGE_REF:
1219 : 2644529 : case ARRAY_REF:
1220 : : /* Use the recorded constant offset. */
1221 : 2644529 : if (maybe_eq (op->off, -1))
1222 : 892314 : max_size = -1;
1223 : : else
1224 : 1752215 : offset += op->off * BITS_PER_UNIT;
1225 : : break;
1226 : :
1227 : : case REALPART_EXPR:
1228 : : break;
1229 : :
1230 : : case IMAGPART_EXPR:
1231 : 36342854 : offset += size;
1232 : : break;
1233 : :
1234 : : case VIEW_CONVERT_EXPR:
1235 : : break;
1236 : :
1237 : : case STRING_CST:
1238 : : case INTEGER_CST:
1239 : : case COMPLEX_CST:
1240 : : case VECTOR_CST:
1241 : : case REAL_CST:
1242 : : case CONSTRUCTOR:
1243 : : case CONST_DECL:
1244 : : return false;
1245 : :
1246 : : default:
1247 : : return false;
1248 : : }
1249 : : }
1250 : :
1251 : 12310260 : if (base == NULL_TREE)
1252 : : return false;
1253 : :
1254 : 12310260 : ref->ref = NULL_TREE;
1255 : 12310260 : ref->base = base;
1256 : 12310260 : ref->ref_alias_set = set;
1257 : 12310260 : ref->base_alias_set = base_set;
1258 : : /* We discount volatiles from value-numbering elsewhere. */
1259 : 12310260 : ref->volatile_p = false;
1260 : :
1261 : 12310260 : if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1262 : : {
1263 : 0 : ref->offset = 0;
1264 : 0 : ref->size = -1;
1265 : 0 : ref->max_size = -1;
1266 : 0 : return true;
1267 : : }
1268 : :
1269 : 12310260 : if (!offset.to_shwi (&ref->offset))
1270 : : {
1271 : 0 : ref->offset = 0;
1272 : 0 : ref->max_size = -1;
1273 : 0 : return true;
1274 : : }
1275 : :
1276 : 12310260 : if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1277 : 733785 : ref->max_size = -1;
1278 : :
1279 : : return true;
1280 : : }
1281 : :
1282 : : /* Copy the operations present in load/store/call REF into RESULT, a vector of
1283 : : vn_reference_op_s's. */
1284 : :
1285 : : static void
1286 : 8292122 : copy_reference_ops_from_call (gcall *call,
1287 : : vec<vn_reference_op_s> *result)
1288 : : {
1289 : 8292122 : vn_reference_op_s temp;
1290 : 8292122 : unsigned i;
1291 : 8292122 : tree lhs = gimple_call_lhs (call);
1292 : 8292122 : int lr;
1293 : :
1294 : : /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1295 : : different. By adding the lhs here in the vector, we ensure that the
1296 : : hashcode is different, guaranteeing a different value number. */
1297 : 8292122 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
1298 : : {
1299 : 446007 : memset (&temp, 0, sizeof (temp));
1300 : 446007 : temp.opcode = MODIFY_EXPR;
1301 : 446007 : temp.type = TREE_TYPE (lhs);
1302 : 446007 : temp.op0 = lhs;
1303 : 446007 : temp.off = -1;
1304 : 446007 : result->safe_push (temp);
1305 : : }
1306 : :
1307 : : /* Copy the type, opcode, function, static chain and EH region, if any. */
1308 : 8292122 : memset (&temp, 0, sizeof (temp));
1309 : 8292122 : temp.type = gimple_call_fntype (call);
1310 : 8292122 : temp.opcode = CALL_EXPR;
1311 : 8292122 : temp.op0 = gimple_call_fn (call);
1312 : 8292122 : if (gimple_call_internal_p (call))
1313 : 454418 : temp.clique = gimple_call_internal_fn (call);
1314 : 8292122 : temp.op1 = gimple_call_chain (call);
1315 : 8292122 : if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1316 : 700147 : temp.op2 = size_int (lr);
1317 : 8292122 : temp.off = -1;
1318 : 8292122 : result->safe_push (temp);
1319 : :
1320 : : /* Copy the call arguments. As they can be references as well,
1321 : : just chain them together. */
1322 : 25060501 : for (i = 0; i < gimple_call_num_args (call); ++i)
1323 : : {
1324 : 16768379 : tree callarg = gimple_call_arg (call, i);
1325 : 16768379 : copy_reference_ops_from_ref (callarg, result);
1326 : : }
1327 : 8292122 : }
1328 : :
1329 : : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1330 : : *I_P to point to the last element of the replacement. */
1331 : : static bool
1332 : 115804231 : vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1333 : : unsigned int *i_p)
1334 : : {
1335 : 115804231 : unsigned int i = *i_p;
1336 : 115804231 : vn_reference_op_t op = &(*ops)[i];
1337 : 115804231 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1338 : 115804231 : tree addr_base;
1339 : 115804231 : poly_int64 addr_offset = 0;
1340 : :
1341 : : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1342 : : from .foo.bar to the preceding MEM_REF offset and replace the
1343 : : address with &OBJ. */
1344 : 115804231 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1345 : : &addr_offset, vn_valueize);
1346 : 115804231 : gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1347 : 115804231 : if (addr_base != TREE_OPERAND (op->op0, 0))
1348 : : {
1349 : 472579 : poly_offset_int off
1350 : 472579 : = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1351 : : SIGNED)
1352 : 472579 : + addr_offset);
1353 : 472579 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1354 : 472579 : op->op0 = build_fold_addr_expr (addr_base);
1355 : 472579 : if (tree_fits_shwi_p (mem_op->op0))
1356 : 472486 : mem_op->off = tree_to_shwi (mem_op->op0);
1357 : : else
1358 : 93 : mem_op->off = -1;
1359 : 472579 : return true;
1360 : : }
1361 : : return false;
1362 : : }
1363 : :
1364 : : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1365 : : *I_P to point to the last element of the replacement. */
1366 : : static bool
1367 : 75651972 : vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1368 : : unsigned int *i_p)
1369 : : {
1370 : 75651972 : bool changed = false;
1371 : 84735938 : vn_reference_op_t op;
1372 : :
1373 : 84735938 : do
1374 : : {
1375 : 84735938 : unsigned int i = *i_p;
1376 : 84735938 : op = &(*ops)[i];
1377 : 84735938 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1378 : 84735938 : gimple *def_stmt;
1379 : 84735938 : enum tree_code code;
1380 : 84735938 : poly_offset_int off;
1381 : :
1382 : 84735938 : def_stmt = SSA_NAME_DEF_STMT (op->op0);
1383 : 84735938 : if (!is_gimple_assign (def_stmt))
1384 : 75651654 : return changed;
1385 : :
1386 : 35795647 : code = gimple_assign_rhs_code (def_stmt);
1387 : 35795647 : if (code != ADDR_EXPR
1388 : 35795647 : && code != POINTER_PLUS_EXPR)
1389 : 16125345 : return changed;
1390 : :
1391 : 19670302 : off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1392 : :
1393 : : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1394 : : from .foo.bar to the preceding MEM_REF offset and replace the
1395 : : address with &OBJ. */
1396 : 19670302 : if (code == ADDR_EXPR)
1397 : : {
1398 : 793456 : tree addr, addr_base;
1399 : 793456 : poly_int64 addr_offset;
1400 : :
1401 : 793456 : addr = gimple_assign_rhs1 (def_stmt);
1402 : 793456 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1403 : : &addr_offset,
1404 : : vn_valueize);
1405 : : /* If that didn't work because the address isn't invariant propagate
1406 : : the reference tree from the address operation in case the current
1407 : : dereference isn't offsetted. */
1408 : 793456 : if (!addr_base
1409 : 199996 : && *i_p == ops->length () - 1
1410 : 99998 : && known_eq (off, 0)
1411 : : /* This makes us disable this transform for PRE where the
1412 : : reference ops might be also used for code insertion which
1413 : : is invalid. */
1414 : 872363 : && default_vn_walk_kind == VN_WALKREWRITE)
1415 : : {
1416 : 78809 : auto_vec<vn_reference_op_s, 32> tem;
1417 : 78809 : copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1418 : : /* Make sure to preserve TBAA info. The only objects not
1419 : : wrapped in MEM_REFs that can have their address taken are
1420 : : STRING_CSTs. */
1421 : 78809 : if (tem.length () >= 2
1422 : 78809 : && tem[tem.length () - 2].opcode == MEM_REF)
1423 : : {
1424 : 78794 : vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1425 : 78794 : new_mem_op->op0
1426 : 78794 : = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1427 : 157588 : wi::to_poly_wide (new_mem_op->op0));
1428 : : }
1429 : : else
1430 : 15 : gcc_assert (tem.last ().opcode == STRING_CST);
1431 : 78809 : ops->pop ();
1432 : 78809 : ops->pop ();
1433 : 78809 : ops->safe_splice (tem);
1434 : 78809 : --*i_p;
1435 : 78809 : return true;
1436 : 78809 : }
1437 : 714647 : if (!addr_base
1438 : 693458 : || TREE_CODE (addr_base) != MEM_REF
1439 : 1407546 : || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1440 : 692662 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1441 : : 0))))
1442 : 21748 : return changed;
1443 : :
1444 : 692899 : off += addr_offset;
1445 : 692899 : off += mem_ref_offset (addr_base);
1446 : 692899 : op->op0 = TREE_OPERAND (addr_base, 0);
1447 : : }
1448 : : else
1449 : : {
1450 : 18876846 : tree ptr, ptroff;
1451 : 18876846 : ptr = gimple_assign_rhs1 (def_stmt);
1452 : 18876846 : ptroff = gimple_assign_rhs2 (def_stmt);
1453 : 18876846 : if (TREE_CODE (ptr) != SSA_NAME
1454 : 17517910 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1455 : : /* Make sure to not endlessly recurse.
1456 : : See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1457 : : happen when we value-number a PHI to its backedge value. */
1458 : 17516496 : || SSA_VAL (ptr) == op->op0
1459 : 36393342 : || !poly_int_tree_p (ptroff))
1460 : 10485461 : return changed;
1461 : :
1462 : 8391385 : off += wi::to_poly_offset (ptroff);
1463 : 8391385 : op->op0 = ptr;
1464 : : }
1465 : :
1466 : 9084284 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1467 : 9084284 : if (tree_fits_shwi_p (mem_op->op0))
1468 : 8944621 : mem_op->off = tree_to_shwi (mem_op->op0);
1469 : : else
1470 : 139663 : mem_op->off = -1;
1471 : : /* ??? Can end up with endless recursion here!?
1472 : : gcc.c-torture/execute/strcmp-1.c */
1473 : 9084284 : if (TREE_CODE (op->op0) == SSA_NAME)
1474 : 9084047 : op->op0 = SSA_VAL (op->op0);
1475 : 9084284 : if (TREE_CODE (op->op0) != SSA_NAME)
1476 : 318 : op->opcode = TREE_CODE (op->op0);
1477 : :
1478 : 9084284 : changed = true;
1479 : : }
1480 : : /* Tail-recurse. */
1481 : 9084284 : while (TREE_CODE (op->op0) == SSA_NAME);
1482 : :
1483 : : /* Fold a remaining *&. */
1484 : 318 : if (TREE_CODE (op->op0) == ADDR_EXPR)
1485 : 237 : vn_reference_fold_indirect (ops, i_p);
1486 : :
1487 : : return changed;
1488 : : }
1489 : :
1490 : : /* Optimize the reference REF to a constant if possible or return
1491 : : NULL_TREE if not. */
1492 : :
1493 : : tree
1494 : 96915173 : fully_constant_vn_reference_p (vn_reference_t ref)
1495 : : {
1496 : 96915173 : vec<vn_reference_op_s> operands = ref->operands;
1497 : 96915173 : vn_reference_op_t op;
1498 : :
1499 : : /* Try to simplify the translated expression if it is
1500 : : a call to a builtin function with at most two arguments. */
1501 : 96915173 : op = &operands[0];
1502 : 96915173 : if (op->opcode == CALL_EXPR
1503 : 88656 : && (!op->op0
1504 : 79670 : || (TREE_CODE (op->op0) == ADDR_EXPR
1505 : 79670 : && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1506 : 79670 : && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1507 : : BUILT_IN_NORMAL)))
1508 : 75199 : && operands.length () >= 2
1509 : 96990316 : && operands.length () <= 3)
1510 : : {
1511 : 34861 : vn_reference_op_t arg0, arg1 = NULL;
1512 : 34861 : bool anyconst = false;
1513 : 34861 : arg0 = &operands[1];
1514 : 34861 : if (operands.length () > 2)
1515 : 4157 : arg1 = &operands[2];
1516 : 34861 : if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1517 : 34861 : || (arg0->opcode == ADDR_EXPR
1518 : 13388 : && is_gimple_min_invariant (arg0->op0)))
1519 : : anyconst = true;
1520 : 34861 : if (arg1
1521 : 34861 : && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1522 : 3317 : || (arg1->opcode == ADDR_EXPR
1523 : 513 : && is_gimple_min_invariant (arg1->op0))))
1524 : : anyconst = true;
1525 : 33508 : if (anyconst)
1526 : : {
1527 : 21671 : combined_fn fn;
1528 : 21671 : if (op->op0)
1529 : 21387 : fn = as_combined_fn (DECL_FUNCTION_CODE
1530 : 21387 : (TREE_OPERAND (op->op0, 0)));
1531 : : else
1532 : 284 : fn = as_combined_fn ((internal_fn) op->clique);
1533 : 21671 : tree folded;
1534 : 21671 : if (arg1)
1535 : 1971 : folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1536 : : else
1537 : 19700 : folded = fold_const_call (fn, ref->type, arg0->op0);
1538 : 21671 : if (folded
1539 : 21671 : && is_gimple_min_invariant (folded))
1540 : : return folded;
1541 : : }
1542 : : }
1543 : :
1544 : : /* Simplify reads from constants or constant initializers. */
1545 : 96880312 : else if (BITS_PER_UNIT == 8
1546 : 96880312 : && ref->type
1547 : 96880312 : && COMPLETE_TYPE_P (ref->type)
1548 : 193760624 : && is_gimple_reg_type (ref->type))
1549 : : {
1550 : 92147096 : poly_int64 off = 0;
1551 : 92147096 : HOST_WIDE_INT size;
1552 : 92147096 : if (INTEGRAL_TYPE_P (ref->type))
1553 : 46450963 : size = TYPE_PRECISION (ref->type);
1554 : 45696133 : else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1555 : 45696133 : size = tree_to_shwi (TYPE_SIZE (ref->type));
1556 : : else
1557 : 96915173 : return NULL_TREE;
1558 : 92147096 : if (size % BITS_PER_UNIT != 0
1559 : 90617977 : || size > MAX_BITSIZE_MODE_ANY_MODE)
1560 : : return NULL_TREE;
1561 : 90617025 : size /= BITS_PER_UNIT;
1562 : 90617025 : unsigned i;
1563 : 174033214 : for (i = 0; i < operands.length (); ++i)
1564 : : {
1565 : 174033214 : if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1566 : : {
1567 : 334 : ++i;
1568 : 334 : break;
1569 : : }
1570 : 174032880 : if (known_eq (operands[i].off, -1))
1571 : : return NULL_TREE;
1572 : 162000883 : off += operands[i].off;
1573 : 162000883 : if (operands[i].opcode == MEM_REF)
1574 : : {
1575 : 78584694 : ++i;
1576 : 78584694 : break;
1577 : : }
1578 : : }
1579 : 78585028 : vn_reference_op_t base = &operands[--i];
1580 : 78585028 : tree ctor = error_mark_node;
1581 : 78585028 : tree decl = NULL_TREE;
1582 : 78585028 : if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1583 : 334 : ctor = base->op0;
1584 : 78584694 : else if (base->opcode == MEM_REF
1585 : 78584694 : && base[1].opcode == ADDR_EXPR
1586 : 129767970 : && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1587 : 3520927 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1588 : 3520867 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1589 : : {
1590 : 47666730 : decl = TREE_OPERAND (base[1].op0, 0);
1591 : 47666730 : if (TREE_CODE (decl) == STRING_CST)
1592 : : ctor = decl;
1593 : : else
1594 : 47662409 : ctor = ctor_for_folding (decl);
1595 : : }
1596 : 78580707 : if (ctor == NULL_TREE)
1597 : 197 : return build_zero_cst (ref->type);
1598 : 78584831 : else if (ctor != error_mark_node)
1599 : : {
1600 : 93559 : HOST_WIDE_INT const_off;
1601 : 93559 : if (decl)
1602 : : {
1603 : 186450 : tree res = fold_ctor_reference (ref->type, ctor,
1604 : 93225 : off * BITS_PER_UNIT,
1605 : 93225 : size * BITS_PER_UNIT, decl);
1606 : 93225 : if (res)
1607 : : {
1608 : 48588 : STRIP_USELESS_TYPE_CONVERSION (res);
1609 : 48588 : if (is_gimple_min_invariant (res))
1610 : 96915173 : return res;
1611 : : }
1612 : : }
1613 : 334 : else if (off.is_constant (&const_off))
1614 : : {
1615 : 334 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1616 : 334 : int len = native_encode_expr (ctor, buf, size, const_off);
1617 : 334 : if (len > 0)
1618 : 139 : return native_interpret_expr (ref->type, buf, len);
1619 : : }
1620 : : }
1621 : : }
1622 : :
1623 : : return NULL_TREE;
1624 : : }
1625 : :
1626 : : /* Return true if OPS contain a storage order barrier. */
1627 : :
1628 : : static bool
1629 : 54894715 : contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1630 : : {
1631 : 54894715 : vn_reference_op_t op;
1632 : 54894715 : unsigned i;
1633 : :
1634 : 221995952 : FOR_EACH_VEC_ELT (ops, i, op)
1635 : 167101237 : if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1636 : : return true;
1637 : :
1638 : : return false;
1639 : : }
1640 : :
1641 : : /* Return true if OPS represent an access with reverse storage order. */
1642 : :
1643 : : static bool
1644 : 54902650 : reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
1645 : : {
1646 : 54902650 : unsigned i = 0;
1647 : 54902650 : if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1648 : : ++i;
1649 : 54902650 : switch (ops[i].opcode)
1650 : : {
1651 : 53469437 : case ARRAY_REF:
1652 : 53469437 : case COMPONENT_REF:
1653 : 53469437 : case BIT_FIELD_REF:
1654 : 53469437 : case MEM_REF:
1655 : 53469437 : return ops[i].reverse;
1656 : : default:
1657 : : return false;
1658 : : }
1659 : : }
1660 : :
1661 : : /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1662 : : structures into their value numbers. This is done in-place, and
1663 : : the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1664 : : whether any operands were valueized. */
1665 : :
1666 : : static void
1667 : 198400693 : valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1668 : : bool with_avail = false)
1669 : : {
1670 : 198400693 : *valueized_anything = false;
1671 : :
1672 : 1623345600 : for (unsigned i = 0; i < orig->length (); ++i)
1673 : : {
1674 : 613272107 : re_valueize:
1675 : 616323418 : vn_reference_op_t vro = &(*orig)[i];
1676 : 616323418 : if (vro->opcode == SSA_NAME
1677 : 529955202 : || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1678 : : {
1679 : 107783763 : tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1680 : 107783763 : if (tem != vro->op0)
1681 : : {
1682 : 14970543 : *valueized_anything = true;
1683 : 14970543 : vro->op0 = tem;
1684 : : }
1685 : : /* If it transforms from an SSA_NAME to a constant, update
1686 : : the opcode. */
1687 : 107783763 : if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1688 : 1637903 : vro->opcode = TREE_CODE (vro->op0);
1689 : : }
1690 : 616323418 : if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1691 : : {
1692 : 26122 : tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1693 : 26122 : if (tem != vro->op1)
1694 : : {
1695 : 582 : *valueized_anything = true;
1696 : 582 : vro->op1 = tem;
1697 : : }
1698 : : }
1699 : 616323418 : if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1700 : : {
1701 : 245163 : tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1702 : 245163 : if (tem != vro->op2)
1703 : : {
1704 : 147896 : *valueized_anything = true;
1705 : 147896 : vro->op2 = tem;
1706 : : }
1707 : : }
1708 : : /* If it transforms from an SSA_NAME to an address, fold with
1709 : : a preceding indirect reference. */
1710 : 616323418 : if (i > 0
1711 : 417850954 : && vro->op0
1712 : 414454115 : && TREE_CODE (vro->op0) == ADDR_EXPR
1713 : 737790040 : && (*orig)[i - 1].opcode == MEM_REF)
1714 : : {
1715 : 115803994 : if (vn_reference_fold_indirect (orig, &i))
1716 : 472579 : *valueized_anything = true;
1717 : : }
1718 : 500519424 : else if (i > 0
1719 : 302046960 : && vro->opcode == SSA_NAME
1720 : 585249737 : && (*orig)[i - 1].opcode == MEM_REF)
1721 : : {
1722 : 75651972 : if (vn_reference_maybe_forwprop_address (orig, &i))
1723 : : {
1724 : 3051311 : *valueized_anything = true;
1725 : : /* Re-valueize the current operand. */
1726 : 3051311 : goto re_valueize;
1727 : : }
1728 : : }
1729 : : /* If it transforms a non-constant ARRAY_REF into a constant
1730 : : one, adjust the constant offset. */
1731 : 424867452 : else if ((vro->opcode == ARRAY_REF
1732 : 424867452 : || vro->opcode == ARRAY_RANGE_REF)
1733 : 35979613 : && known_eq (vro->off, -1)
1734 : 14720544 : && poly_int_tree_p (vro->op0)
1735 : 4234138 : && poly_int_tree_p (vro->op1)
1736 : 429101590 : && TREE_CODE (vro->op2) == INTEGER_CST)
1737 : : {
1738 : 8143890 : poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1739 : 12215835 : - wi::to_poly_offset (vro->op1))
1740 : 8143890 : * wi::to_offset (vro->op2)
1741 : 4071945 : * vn_ref_op_align_unit (vro));
1742 : 4071945 : off.to_shwi (&vro->off);
1743 : : }
1744 : : }
1745 : 198400693 : }
1746 : :
1747 : : static void
1748 : 13240212 : valueize_refs (vec<vn_reference_op_s> *orig)
1749 : : {
1750 : 13240212 : bool tem;
1751 : 0 : valueize_refs_1 (orig, &tem);
1752 : 0 : }
1753 : :
1754 : : static vec<vn_reference_op_s> shared_lookup_references;
1755 : :
1756 : : /* Create a vector of vn_reference_op_s structures from REF, a
1757 : : REFERENCE_CLASS_P tree. The vector is shared among all callers of
1758 : : this function. *VALUEIZED_ANYTHING will specify whether any
1759 : : operands were valueized. */
1760 : :
1761 : : static vec<vn_reference_op_s>
1762 : 160979899 : valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1763 : : {
1764 : 160979899 : if (!ref)
1765 : 0 : return vNULL;
1766 : 160979899 : shared_lookup_references.truncate (0);
1767 : 160979899 : copy_reference_ops_from_ref (ref, &shared_lookup_references);
1768 : 160979899 : valueize_refs_1 (&shared_lookup_references, valueized_anything);
1769 : 160979899 : return shared_lookup_references;
1770 : : }
1771 : :
1772 : : /* Create a vector of vn_reference_op_s structures from CALL, a
1773 : : call statement. The vector is shared among all callers of
1774 : : this function. */
1775 : :
1776 : : static vec<vn_reference_op_s>
1777 : 8292122 : valueize_shared_reference_ops_from_call (gcall *call)
1778 : : {
1779 : 8292122 : if (!call)
1780 : 0 : return vNULL;
1781 : 8292122 : shared_lookup_references.truncate (0);
1782 : 8292122 : copy_reference_ops_from_call (call, &shared_lookup_references);
1783 : 8292122 : valueize_refs (&shared_lookup_references);
1784 : 8292122 : return shared_lookup_references;
1785 : : }
1786 : :
1787 : : /* Lookup a SCCVN reference operation VR in the current hash table.
1788 : : Returns the resulting value number if it exists in the hash table,
1789 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
1790 : : vn_reference_t stored in the hashtable if something is found. */
1791 : :
1792 : : static tree
1793 : 57887181 : vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1794 : : {
1795 : 57887181 : vn_reference_s **slot;
1796 : 57887181 : hashval_t hash;
1797 : :
1798 : 57887181 : hash = vr->hashcode;
1799 : 57887181 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1800 : 57887181 : if (slot)
1801 : : {
1802 : 7192288 : if (vnresult)
1803 : 7192288 : *vnresult = (vn_reference_t)*slot;
1804 : 7192288 : return ((vn_reference_t)*slot)->result;
1805 : : }
1806 : :
1807 : : return NULL_TREE;
1808 : : }
1809 : :
1810 : :
1811 : : /* Partial definition tracking support. */
1812 : :
1813 : : struct pd_range
1814 : : {
1815 : : HOST_WIDE_INT offset;
1816 : : HOST_WIDE_INT size;
1817 : : };
1818 : :
1819 : : struct pd_data
1820 : : {
1821 : : tree rhs;
1822 : : HOST_WIDE_INT rhs_off;
1823 : : HOST_WIDE_INT offset;
1824 : : HOST_WIDE_INT size;
1825 : : };
1826 : :
1827 : : /* Context for alias walking. */
1828 : :
1829 : : struct vn_walk_cb_data
1830 : : {
1831 : 53711648 : vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1832 : : vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1833 : : bool redundant_store_removal_p_)
1834 : 53711648 : : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1835 : 53711648 : mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1836 : 53711648 : vn_walk_kind (vn_walk_kind_),
1837 : 53711648 : tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
1838 : 53711648 : saved_operands (vNULL), first_set (-2), first_base_set (-2),
1839 : 53711648 : known_ranges (NULL)
1840 : : {
1841 : 53711648 : if (!last_vuse_ptr)
1842 : 24846980 : last_vuse_ptr = &last_vuse;
1843 : 53711648 : ao_ref_init (&orig_ref, orig_ref_);
1844 : 53711648 : if (mask)
1845 : : {
1846 : 290653 : wide_int w = wi::to_wide (mask);
1847 : 290653 : unsigned int pos = 0, prec = w.get_precision ();
1848 : 290653 : pd_data pd;
1849 : 290653 : pd.rhs = build_constructor (NULL_TREE, NULL);
1850 : 290653 : pd.rhs_off = 0;
1851 : : /* When bitwise and with a constant is done on a memory load,
1852 : : we don't really need all the bits to be defined or defined
1853 : : to constants, we don't really care what is in the position
1854 : : corresponding to 0 bits in the mask.
1855 : : So, push the ranges of those 0 bits in the mask as artificial
1856 : : zero stores and let the partial def handling code do the
1857 : : rest. */
1858 : 624969 : while (pos < prec)
1859 : : {
1860 : 606472 : int tz = wi::ctz (w);
1861 : 606472 : if (pos + tz > prec)
1862 : 272156 : tz = prec - pos;
1863 : 606472 : if (tz)
1864 : : {
1865 : 470326 : if (BYTES_BIG_ENDIAN)
1866 : : pd.offset = prec - pos - tz;
1867 : : else
1868 : 470326 : pd.offset = pos;
1869 : 470326 : pd.size = tz;
1870 : 470326 : void *r = push_partial_def (pd, 0, 0, 0, prec);
1871 : 470326 : gcc_assert (r == NULL_TREE);
1872 : : }
1873 : 606472 : pos += tz;
1874 : 606472 : if (pos == prec)
1875 : : break;
1876 : 334316 : w = wi::lrshift (w, tz);
1877 : 334316 : tz = wi::ctz (wi::bit_not (w));
1878 : 334316 : if (pos + tz > prec)
1879 : 0 : tz = prec - pos;
1880 : 334316 : pos += tz;
1881 : 334316 : w = wi::lrshift (w, tz);
1882 : : }
1883 : 290653 : }
1884 : 53711648 : }
1885 : : ~vn_walk_cb_data ();
1886 : : void *finish (alias_set_type, alias_set_type, tree);
1887 : : void *push_partial_def (pd_data pd,
1888 : : alias_set_type, alias_set_type, HOST_WIDE_INT,
1889 : : HOST_WIDE_INT);
1890 : :
1891 : : vn_reference_t vr;
1892 : : ao_ref orig_ref;
1893 : : tree *last_vuse_ptr;
1894 : : tree last_vuse;
1895 : : tree mask;
1896 : : tree masked_result;
1897 : : tree same_val;
1898 : : vn_lookup_kind vn_walk_kind;
1899 : : bool tbaa_p;
1900 : : bool redundant_store_removal_p;
1901 : : vec<vn_reference_op_s> saved_operands;
1902 : :
1903 : : /* The VDEFs of partial defs we come along. */
1904 : : auto_vec<pd_data, 2> partial_defs;
1905 : : /* The first defs range to avoid splay tree setup in most cases. */
1906 : : pd_range first_range;
1907 : : alias_set_type first_set;
1908 : : alias_set_type first_base_set;
1909 : : splay_tree known_ranges;
1910 : : obstack ranges_obstack;
1911 : : static constexpr HOST_WIDE_INT bufsize = 64;
1912 : : };
1913 : :
1914 : 53711648 : vn_walk_cb_data::~vn_walk_cb_data ()
1915 : : {
1916 : 53711648 : if (known_ranges)
1917 : : {
1918 : 173954 : splay_tree_delete (known_ranges);
1919 : 173954 : obstack_free (&ranges_obstack, NULL);
1920 : : }
1921 : 53711648 : saved_operands.release ();
1922 : 53711648 : }
1923 : :
1924 : : void *
1925 : 1136211 : vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
1926 : : {
1927 : 1136211 : if (first_set != -2)
1928 : : {
1929 : 272884 : set = first_set;
1930 : 272884 : base_set = first_base_set;
1931 : : }
1932 : 1136211 : if (mask)
1933 : : {
1934 : 494 : masked_result = val;
1935 : 494 : return (void *) -1;
1936 : : }
1937 : 1135717 : if (same_val && !operand_equal_p (val, same_val))
1938 : : return (void *) -1;
1939 : 1130988 : vec<vn_reference_op_s> &operands
1940 : 1130988 : = saved_operands.exists () ? saved_operands : vr->operands;
1941 : 1130988 : return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
1942 : : vr->offset, vr->max_size,
1943 : 1130988 : vr->type, operands, val);
1944 : : }
1945 : :
1946 : : /* pd_range splay-tree helpers. */
1947 : :
1948 : : static int
1949 : 1295211 : pd_range_compare (splay_tree_key offset1p, splay_tree_key offset2p)
1950 : : {
1951 : 1295211 : HOST_WIDE_INT offset1 = *(HOST_WIDE_INT *)offset1p;
1952 : 1295211 : HOST_WIDE_INT offset2 = *(HOST_WIDE_INT *)offset2p;
1953 : 1295211 : if (offset1 < offset2)
1954 : : return -1;
1955 : 895585 : else if (offset1 > offset2)
1956 : 416465 : return 1;
1957 : : return 0;
1958 : : }
1959 : :
1960 : : static void *
1961 : 544646 : pd_tree_alloc (int size, void *data_)
1962 : : {
1963 : 544646 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
1964 : 544646 : return obstack_alloc (&data->ranges_obstack, size);
1965 : : }
1966 : :
1967 : : static void
1968 : 544646 : pd_tree_dealloc (void *, void *)
1969 : : {
1970 : 544646 : }
1971 : :
1972 : : /* Push PD to the vector of partial definitions returning a
1973 : : value when we are ready to combine things with VUSE, SET and MAXSIZEI,
1974 : : NULL when we want to continue looking for partial defs or -1
1975 : : on failure. */
1976 : :
1977 : : void *
1978 : 541442 : vn_walk_cb_data::push_partial_def (pd_data pd,
1979 : : alias_set_type set, alias_set_type base_set,
1980 : : HOST_WIDE_INT offseti,
1981 : : HOST_WIDE_INT maxsizei)
1982 : : {
1983 : : /* We're using a fixed buffer for encoding so fail early if the object
1984 : : we want to interpret is bigger. */
1985 : 541442 : if (maxsizei > bufsize * BITS_PER_UNIT
1986 : : || CHAR_BIT != 8
1987 : : || BITS_PER_UNIT != 8
1988 : : /* Not prepared to handle PDP endian. */
1989 : : || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1990 : : return (void *)-1;
1991 : :
1992 : : /* Turn too large constant stores into non-constant stores. */
1993 : 541373 : if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
1994 : 0 : pd.rhs = error_mark_node;
1995 : :
1996 : : /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
1997 : : most a partial byte before and/or after the region. */
1998 : 541373 : if (!CONSTANT_CLASS_P (pd.rhs))
1999 : : {
2000 : 515506 : if (pd.offset < offseti)
2001 : : {
2002 : 6690 : HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2003 : 6690 : gcc_assert (pd.size > o);
2004 : 6690 : pd.size -= o;
2005 : 6690 : pd.offset += o;
2006 : : }
2007 : 515506 : if (pd.size > maxsizei)
2008 : 10612 : pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
2009 : : }
2010 : :
2011 : 541373 : pd.offset -= offseti;
2012 : :
2013 : 1082746 : bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2014 : 541373 : || CONSTANT_CLASS_P (pd.rhs));
2015 : 541373 : pd_range *r;
2016 : 541373 : if (partial_defs.is_empty ())
2017 : : {
2018 : : /* If we get a clobber upfront, fail. */
2019 : 339170 : if (TREE_CLOBBER_P (pd.rhs))
2020 : : return (void *)-1;
2021 : 330227 : if (!pd_constant_p)
2022 : : return (void *)-1;
2023 : 302594 : partial_defs.safe_push (pd);
2024 : 302594 : first_range.offset = pd.offset;
2025 : 302594 : first_range.size = pd.size;
2026 : 302594 : first_set = set;
2027 : 302594 : first_base_set = base_set;
2028 : 302594 : last_vuse_ptr = NULL;
2029 : 302594 : r = &first_range;
2030 : : /* Go check if the first partial definition was a full one in case
2031 : : the caller didn't optimize for this. */
2032 : : }
2033 : : else
2034 : : {
2035 : 202203 : if (!known_ranges)
2036 : : {
2037 : : /* ??? Optimize the case where the 2nd partial def completes
2038 : : things. */
2039 : 173954 : gcc_obstack_init (&ranges_obstack);
2040 : 173954 : known_ranges = splay_tree_new_with_allocator (pd_range_compare, 0, 0,
2041 : : pd_tree_alloc,
2042 : : pd_tree_dealloc, this);
2043 : 173954 : splay_tree_insert (known_ranges,
2044 : 173954 : (splay_tree_key)&first_range.offset,
2045 : 173954 : (splay_tree_value)&first_range);
2046 : : }
2047 : :
2048 : 202203 : pd_range newr = { pd.offset, pd.size };
2049 : 202203 : splay_tree_node n;
2050 : : /* Lookup the predecessor of offset + 1 and see if we need to merge. */
2051 : 202203 : HOST_WIDE_INT loffset = newr.offset + 1;
2052 : 202203 : if ((n = splay_tree_predecessor (known_ranges, (splay_tree_key)&loffset))
2053 : 185228 : && ((r = (pd_range *)n->value), true)
2054 : 202203 : && ranges_known_overlap_p (r->offset, r->size + 1,
2055 : : newr.offset, newr.size))
2056 : : {
2057 : : /* Ignore partial defs already covered. Here we also drop shadowed
2058 : : clobbers arriving here at the floor. */
2059 : 5465 : if (known_subrange_p (newr.offset, newr.size, r->offset, r->size))
2060 : 8711 : return NULL;
2061 : 4799 : r->size
2062 : 4799 : = MAX (r->offset + r->size, newr.offset + newr.size) - r->offset;
2063 : : }
2064 : : else
2065 : : {
2066 : : /* newr.offset wasn't covered yet, insert the range. */
2067 : 196738 : r = XOBNEW (&ranges_obstack, pd_range);
2068 : 196738 : *r = newr;
2069 : 196738 : splay_tree_insert (known_ranges, (splay_tree_key)&r->offset,
2070 : : (splay_tree_value)r);
2071 : : }
2072 : : /* Merge r which now contains newr and is a member of the splay tree with
2073 : : adjacent overlapping ranges. */
2074 : : pd_range *rafter;
2075 : 440010 : while ((n = splay_tree_successor (known_ranges,
2076 : 220005 : (splay_tree_key)&r->offset))
2077 : 18651 : && ((rafter = (pd_range *)n->value), true)
2078 : 220005 : && ranges_known_overlap_p (r->offset, r->size + 1,
2079 : 18651 : rafter->offset, rafter->size))
2080 : : {
2081 : 18468 : r->size = MAX (r->offset + r->size,
2082 : 18468 : rafter->offset + rafter->size) - r->offset;
2083 : 18468 : splay_tree_remove (known_ranges, (splay_tree_key)&rafter->offset);
2084 : : }
2085 : : /* If we get a clobber, fail. */
2086 : 201537 : if (TREE_CLOBBER_P (pd.rhs))
2087 : : return (void *)-1;
2088 : : /* Non-constants are OK as long as they are shadowed by a constant. */
2089 : 199394 : if (!pd_constant_p)
2090 : : return (void *)-1;
2091 : 193492 : partial_defs.safe_push (pd);
2092 : : }
2093 : :
2094 : : /* Now we have merged newr into the range tree. When we have covered
2095 : : [offseti, sizei] then the tree will contain exactly one node which has
2096 : : the desired properties and it will be 'r'. */
2097 : 496086 : if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2098 : : /* Continue looking for partial defs. */
2099 : : return NULL;
2100 : :
2101 : : /* Now simply native encode all partial defs in reverse order. */
2102 : 6864 : unsigned ndefs = partial_defs.length ();
2103 : : /* We support up to 512-bit values (for V8DFmode). */
2104 : 6864 : unsigned char buffer[bufsize + 1];
2105 : 6864 : unsigned char this_buffer[bufsize + 1];
2106 : 6864 : int len;
2107 : :
2108 : 6864 : memset (buffer, 0, bufsize + 1);
2109 : 6864 : unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2110 : 33923 : while (!partial_defs.is_empty ())
2111 : : {
2112 : 20195 : pd_data pd = partial_defs.pop ();
2113 : 20195 : unsigned int amnt;
2114 : 20195 : if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2115 : : {
2116 : : /* Empty CONSTRUCTOR. */
2117 : 790 : if (pd.size >= needed_len * BITS_PER_UNIT)
2118 : 97 : len = needed_len;
2119 : : else
2120 : 693 : len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2121 : 790 : memset (this_buffer, 0, len);
2122 : : }
2123 : 19405 : else if (pd.rhs_off >= 0)
2124 : : {
2125 : 38810 : len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2126 : 19405 : (MAX (0, -pd.offset)
2127 : 19405 : + pd.rhs_off) / BITS_PER_UNIT);
2128 : 19405 : if (len <= 0
2129 : 19405 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2130 : 19405 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2131 : : {
2132 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2133 : 0 : fprintf (dump_file, "Failed to encode %u "
2134 : : "partial definitions\n", ndefs);
2135 : 0 : return (void *)-1;
2136 : : }
2137 : : }
2138 : : else /* negative pd.rhs_off indicates we want to chop off first bits */
2139 : : {
2140 : 0 : if (-pd.rhs_off >= bufsize)
2141 : : return (void *)-1;
2142 : 0 : len = native_encode_expr (pd.rhs,
2143 : 0 : this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2144 : 0 : bufsize - -pd.rhs_off / BITS_PER_UNIT,
2145 : 0 : MAX (0, -pd.offset) / BITS_PER_UNIT);
2146 : 0 : if (len <= 0
2147 : 0 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2148 : 0 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2149 : : {
2150 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2151 : 0 : fprintf (dump_file, "Failed to encode %u "
2152 : : "partial definitions\n", ndefs);
2153 : 0 : return (void *)-1;
2154 : : }
2155 : : }
2156 : :
2157 : 20195 : unsigned char *p = buffer;
2158 : 20195 : HOST_WIDE_INT size = pd.size;
2159 : 20195 : if (pd.offset < 0)
2160 : 280 : size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2161 : 20195 : this_buffer[len] = 0;
2162 : 20195 : if (BYTES_BIG_ENDIAN)
2163 : : {
2164 : : /* LSB of this_buffer[len - 1] byte should be at
2165 : : pd.offset + pd.size - 1 bits in buffer. */
2166 : : amnt = ((unsigned HOST_WIDE_INT) pd.offset
2167 : : + pd.size) % BITS_PER_UNIT;
2168 : : if (amnt)
2169 : : shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2170 : : unsigned char *q = this_buffer;
2171 : : unsigned int off = 0;
2172 : : if (pd.offset >= 0)
2173 : : {
2174 : : unsigned int msk;
2175 : : off = pd.offset / BITS_PER_UNIT;
2176 : : gcc_assert (off < needed_len);
2177 : : p = buffer + off;
2178 : : if (size <= amnt)
2179 : : {
2180 : : msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2181 : : *p = (*p & ~msk) | (this_buffer[len] & msk);
2182 : : size = 0;
2183 : : }
2184 : : else
2185 : : {
2186 : : if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2187 : : q = (this_buffer + len
2188 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2189 : : / BITS_PER_UNIT));
2190 : : if (pd.offset % BITS_PER_UNIT)
2191 : : {
2192 : : msk = -1U << (BITS_PER_UNIT
2193 : : - (pd.offset % BITS_PER_UNIT));
2194 : : *p = (*p & msk) | (*q & ~msk);
2195 : : p++;
2196 : : q++;
2197 : : off++;
2198 : : size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2199 : : gcc_assert (size >= 0);
2200 : : }
2201 : : }
2202 : : }
2203 : : else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2204 : : {
2205 : : q = (this_buffer + len
2206 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2207 : : / BITS_PER_UNIT));
2208 : : if (pd.offset % BITS_PER_UNIT)
2209 : : {
2210 : : q++;
2211 : : size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2212 : : % BITS_PER_UNIT);
2213 : : gcc_assert (size >= 0);
2214 : : }
2215 : : }
2216 : : if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2217 : : > needed_len)
2218 : : size = (needed_len - off) * BITS_PER_UNIT;
2219 : : memcpy (p, q, size / BITS_PER_UNIT);
2220 : : if (size % BITS_PER_UNIT)
2221 : : {
2222 : : unsigned int msk
2223 : : = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2224 : : p += size / BITS_PER_UNIT;
2225 : : q += size / BITS_PER_UNIT;
2226 : : *p = (*q & msk) | (*p & ~msk);
2227 : : }
2228 : : }
2229 : : else
2230 : : {
2231 : 20195 : if (pd.offset >= 0)
2232 : : {
2233 : : /* LSB of this_buffer[0] byte should be at pd.offset bits
2234 : : in buffer. */
2235 : 19915 : unsigned int msk;
2236 : 19915 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2237 : 19915 : amnt = pd.offset % BITS_PER_UNIT;
2238 : 19915 : if (amnt)
2239 : 1362 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2240 : 19915 : unsigned int off = pd.offset / BITS_PER_UNIT;
2241 : 19915 : gcc_assert (off < needed_len);
2242 : 19915 : size = MIN (size,
2243 : : (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2244 : 19915 : p = buffer + off;
2245 : 19915 : if (amnt + size < BITS_PER_UNIT)
2246 : : {
2247 : : /* Low amnt bits come from *p, then size bits
2248 : : from this_buffer[0] and the remaining again from
2249 : : *p. */
2250 : 810 : msk = ((1 << size) - 1) << amnt;
2251 : 810 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2252 : 810 : size = 0;
2253 : : }
2254 : 19105 : else if (amnt)
2255 : : {
2256 : 1201 : msk = -1U << amnt;
2257 : 1201 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2258 : 1201 : p++;
2259 : 1201 : size -= (BITS_PER_UNIT - amnt);
2260 : : }
2261 : : }
2262 : : else
2263 : : {
2264 : 280 : amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2265 : 280 : if (amnt)
2266 : 30 : size -= BITS_PER_UNIT - amnt;
2267 : 280 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2268 : 280 : if (amnt)
2269 : 30 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2270 : : }
2271 : 20195 : memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2272 : 20195 : p += size / BITS_PER_UNIT;
2273 : 20195 : if (size % BITS_PER_UNIT)
2274 : : {
2275 : 691 : unsigned int msk = -1U << (size % BITS_PER_UNIT);
2276 : 691 : *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2277 : 691 : & ~msk) | (*p & msk);
2278 : : }
2279 : : }
2280 : : }
2281 : :
2282 : 6864 : tree type = vr->type;
2283 : : /* Make sure to interpret in a type that has a range covering the whole
2284 : : access size. */
2285 : 6864 : if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2286 : 13 : type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2287 : 6864 : tree val;
2288 : 6864 : if (BYTES_BIG_ENDIAN)
2289 : : {
2290 : : unsigned sz = needed_len;
2291 : : if (maxsizei % BITS_PER_UNIT)
2292 : : shift_bytes_in_array_right (buffer, needed_len,
2293 : : BITS_PER_UNIT
2294 : : - (maxsizei % BITS_PER_UNIT));
2295 : : if (INTEGRAL_TYPE_P (type))
2296 : : {
2297 : : if (TYPE_MODE (type) != BLKmode)
2298 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2299 : : else
2300 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2301 : : }
2302 : : if (sz > needed_len)
2303 : : {
2304 : : memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2305 : : val = native_interpret_expr (type, this_buffer, sz);
2306 : : }
2307 : : else
2308 : : val = native_interpret_expr (type, buffer, needed_len);
2309 : : }
2310 : : else
2311 : 6864 : val = native_interpret_expr (type, buffer, bufsize);
2312 : : /* If we chop off bits because the types precision doesn't match the memory
2313 : : access size this is ok when optimizing reads but not when called from
2314 : : the DSE code during elimination. */
2315 : 6864 : if (val && type != vr->type)
2316 : : {
2317 : 13 : if (! int_fits_type_p (val, vr->type))
2318 : : val = NULL_TREE;
2319 : : else
2320 : 13 : val = fold_convert (vr->type, val);
2321 : : }
2322 : :
2323 : 6860 : if (val)
2324 : : {
2325 : 6860 : if (dump_file && (dump_flags & TDF_DETAILS))
2326 : 0 : fprintf (dump_file,
2327 : : "Successfully combined %u partial definitions\n", ndefs);
2328 : : /* We are using the alias-set of the first store we encounter which
2329 : : should be appropriate here. */
2330 : 6860 : return finish (first_set, first_base_set, val);
2331 : : }
2332 : : else
2333 : : {
2334 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
2335 : 0 : fprintf (dump_file,
2336 : : "Failed to interpret %u encoded partial definitions\n", ndefs);
2337 : 4 : return (void *)-1;
2338 : : }
2339 : : }
2340 : :
2341 : : /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2342 : : with the current VUSE and performs the expression lookup. */
2343 : :
2344 : : static void *
2345 : 929166323 : vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2346 : : {
2347 : 929166323 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2348 : 929166323 : vn_reference_t vr = data->vr;
2349 : 929166323 : vn_reference_s **slot;
2350 : 929166323 : hashval_t hash;
2351 : :
2352 : : /* If we have partial definitions recorded we have to go through
2353 : : vn_reference_lookup_3. */
2354 : 1852196519 : if (!data->partial_defs.is_empty ())
2355 : : return NULL;
2356 : :
2357 : 928514304 : if (data->last_vuse_ptr)
2358 : : {
2359 : 910613312 : *data->last_vuse_ptr = vuse;
2360 : 910613312 : data->last_vuse = vuse;
2361 : : }
2362 : :
2363 : : /* Fixup vuse and hash. */
2364 : 928514304 : if (vr->vuse)
2365 : 928514304 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2366 : 928514304 : vr->vuse = vuse_ssa_val (vuse);
2367 : 928514304 : if (vr->vuse)
2368 : 928514304 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2369 : :
2370 : 928514304 : hash = vr->hashcode;
2371 : 928514304 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2372 : 928514304 : if (slot)
2373 : : {
2374 : 6135138 : if ((*slot)->result && data->saved_operands.exists ())
2375 : 260330 : return data->finish (vr->set, vr->base_set, (*slot)->result);
2376 : : return *slot;
2377 : : }
2378 : :
2379 : 922379166 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2380 : : {
2381 : 15063476 : HOST_WIDE_INT op_offset, op_size;
2382 : 15063476 : tree v = NULL_TREE;
2383 : 15063476 : tree base = ao_ref_base (op);
2384 : :
2385 : 15063476 : if (base
2386 : 15063476 : && op->offset.is_constant (&op_offset)
2387 : 15063476 : && op->size.is_constant (&op_size)
2388 : 15063476 : && op->max_size_known_p ()
2389 : 29727444 : && known_eq (op->size, op->max_size))
2390 : : {
2391 : 14396309 : if (TREE_CODE (base) == PARM_DECL)
2392 : 613226 : v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2393 : : op_size);
2394 : 13783083 : else if (TREE_CODE (base) == MEM_REF
2395 : 5426385 : && integer_zerop (TREE_OPERAND (base, 1))
2396 : 4443896 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2397 : 4439021 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2398 : 16648129 : && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2399 : : == PARM_DECL))
2400 : 2826909 : v = ipcp_get_aggregate_const (cfun,
2401 : 2826909 : SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2402 : : true, op_offset, op_size);
2403 : : }
2404 : 3440135 : if (v)
2405 : 989 : return data->finish (vr->set, vr->base_set, v);
2406 : : }
2407 : :
2408 : : return NULL;
2409 : : }
2410 : :
2411 : : /* Lookup an existing or insert a new vn_reference entry into the
2412 : : value table for the VUSE, SET, TYPE, OPERANDS reference which
2413 : : has the value VALUE which is either a constant or an SSA name. */
2414 : :
2415 : : static vn_reference_t
2416 : 1130988 : vn_reference_lookup_or_insert_for_pieces (tree vuse,
2417 : : alias_set_type set,
2418 : : alias_set_type base_set,
2419 : : poly_int64 offset,
2420 : : poly_int64 max_size,
2421 : : tree type,
2422 : : vec<vn_reference_op_s,
2423 : : va_heap> operands,
2424 : : tree value)
2425 : : {
2426 : 1130988 : vn_reference_s vr1;
2427 : 1130988 : vn_reference_t result;
2428 : 1130988 : unsigned value_id;
2429 : 1130988 : vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2430 : 1130988 : vr1.operands = operands;
2431 : 1130988 : vr1.type = type;
2432 : 1130988 : vr1.set = set;
2433 : 1130988 : vr1.base_set = base_set;
2434 : 1130988 : vr1.offset = offset;
2435 : 1130988 : vr1.max_size = max_size;
2436 : 1130988 : vr1.hashcode = vn_reference_compute_hash (&vr1);
2437 : 1130988 : if (vn_reference_lookup_1 (&vr1, &result))
2438 : 2740 : return result;
2439 : :
2440 : 1128248 : if (TREE_CODE (value) == SSA_NAME)
2441 : 235722 : value_id = VN_INFO (value)->value_id;
2442 : : else
2443 : 892526 : value_id = get_or_alloc_constant_value_id (value);
2444 : 1128248 : return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
2445 : 1128248 : type, operands.copy (), value, value_id);
2446 : : }
2447 : :
2448 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2449 : : value-number for the possibly simplified result or by inserting the
2450 : : operation if INSERT is true. If SIMPLIFY is false, return a value
2451 : : number for the unsimplified expression. */
2452 : :
2453 : : static tree
2454 : 15309962 : vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2455 : : bool simplify)
2456 : : {
2457 : 15309962 : tree result = NULL_TREE;
2458 : : /* We will be creating a value number for
2459 : : RCODE (OPS...).
2460 : : So first simplify and lookup this expression to see if it
2461 : : is already available. */
2462 : : /* For simplification valueize. */
2463 : 15309962 : unsigned i = 0;
2464 : 15309962 : if (simplify)
2465 : 35413302 : for (i = 0; i < res_op->num_ops; ++i)
2466 : 20109156 : if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2467 : : {
2468 : 12864600 : tree tem = vn_valueize (res_op->ops[i]);
2469 : 12864600 : if (!tem)
2470 : : break;
2471 : 12864600 : res_op->ops[i] = tem;
2472 : : }
2473 : : /* If valueization of an operand fails (it is not available), skip
2474 : : simplification. */
2475 : 15309962 : bool res = false;
2476 : 15309962 : if (i == res_op->num_ops)
2477 : : {
2478 : 15304146 : mprts_hook = vn_lookup_simplify_result;
2479 : 15304146 : res = res_op->resimplify (NULL, vn_valueize);
2480 : 15304146 : mprts_hook = NULL;
2481 : : }
2482 : 26299594 : gimple *new_stmt = NULL;
2483 : 15304146 : if (res
2484 : 15304146 : && gimple_simplified_result_is_gimple_val (res_op))
2485 : : {
2486 : : /* The expression is already available. */
2487 : 4314514 : result = res_op->ops[0];
2488 : : /* Valueize it, simplification returns sth in AVAIL only. */
2489 : 4314514 : if (TREE_CODE (result) == SSA_NAME)
2490 : 254003 : result = SSA_VAL (result);
2491 : : }
2492 : : else
2493 : : {
2494 : 10995448 : tree val = vn_lookup_simplify_result (res_op);
2495 : 10995448 : if (!val && insert)
2496 : : {
2497 : 152027 : gimple_seq stmts = NULL;
2498 : 152027 : result = maybe_push_res_to_seq (res_op, &stmts);
2499 : 152027 : if (result)
2500 : : {
2501 : 152027 : gcc_assert (gimple_seq_singleton_p (stmts));
2502 : 152027 : new_stmt = gimple_seq_first_stmt (stmts);
2503 : : }
2504 : : }
2505 : : else
2506 : : /* The expression is already available. */
2507 : : result = val;
2508 : : }
2509 : 254003 : if (new_stmt)
2510 : : {
2511 : : /* The expression is not yet available, value-number lhs to
2512 : : the new SSA_NAME we created. */
2513 : : /* Initialize value-number information properly. */
2514 : 152027 : vn_ssa_aux_t result_info = VN_INFO (result);
2515 : 152027 : result_info->valnum = result;
2516 : 152027 : result_info->value_id = get_next_value_id ();
2517 : 152027 : result_info->visited = 1;
2518 : 152027 : gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2519 : : new_stmt);
2520 : 152027 : result_info->needs_insertion = true;
2521 : : /* ??? PRE phi-translation inserts NARYs without corresponding
2522 : : SSA name result. Re-use those but set their result according
2523 : : to the stmt we just built. */
2524 : 152027 : vn_nary_op_t nary = NULL;
2525 : 152027 : vn_nary_op_lookup_stmt (new_stmt, &nary);
2526 : 152027 : if (nary)
2527 : : {
2528 : 0 : gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2529 : 0 : nary->u.result = gimple_assign_lhs (new_stmt);
2530 : : }
2531 : : /* As all "inserted" statements are singleton SCCs, insert
2532 : : to the valid table. This is strictly needed to
2533 : : avoid re-generating new value SSA_NAMEs for the same
2534 : : expression during SCC iteration over and over (the
2535 : : optimistic table gets cleared after each iteration).
2536 : : We do not need to insert into the optimistic table, as
2537 : : lookups there will fall back to the valid table. */
2538 : : else
2539 : : {
2540 : 152027 : unsigned int length = vn_nary_length_from_stmt (new_stmt);
2541 : 152027 : vn_nary_op_t vno1
2542 : 152027 : = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2543 : 152027 : vno1->value_id = result_info->value_id;
2544 : 152027 : vno1->length = length;
2545 : 152027 : vno1->predicated_values = 0;
2546 : 152027 : vno1->u.result = result;
2547 : 152027 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2548 : 152027 : vn_nary_op_insert_into (vno1, valid_info->nary);
2549 : : /* Also do not link it into the undo chain. */
2550 : 152027 : last_inserted_nary = vno1->next;
2551 : 152027 : vno1->next = (vn_nary_op_t)(void *)-1;
2552 : : }
2553 : 152027 : if (dump_file && (dump_flags & TDF_DETAILS))
2554 : : {
2555 : 622 : fprintf (dump_file, "Inserting name ");
2556 : 622 : print_generic_expr (dump_file, result);
2557 : 622 : fprintf (dump_file, " for expression ");
2558 : 622 : print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2559 : 622 : fprintf (dump_file, "\n");
2560 : : }
2561 : : }
2562 : 15309962 : return result;
2563 : : }
2564 : :
2565 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2566 : : value-number for the simplified result or by inserting the operation. */
2567 : :
2568 : : static tree
2569 : 194233 : vn_nary_build_or_lookup (gimple_match_op *res_op)
2570 : : {
2571 : 0 : return vn_nary_build_or_lookup_1 (res_op, true, true);
2572 : : }
2573 : :
2574 : : /* Try to simplify the expression RCODE OPS... of type TYPE and return
2575 : : its value if present. */
2576 : :
2577 : : tree
2578 : 5971448 : vn_nary_simplify (vn_nary_op_t nary)
2579 : : {
2580 : 5971448 : if (nary->length > gimple_match_op::MAX_NUM_OPS)
2581 : : return NULL_TREE;
2582 : 5971298 : gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2583 : 5971298 : nary->type, nary->length);
2584 : 5971298 : memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2585 : 5971298 : return vn_nary_build_or_lookup_1 (&op, false, true);
2586 : : }
2587 : :
2588 : : /* Elimination engine. */
2589 : :
2590 : : class eliminate_dom_walker : public dom_walker
2591 : : {
2592 : : public:
2593 : : eliminate_dom_walker (cdi_direction, bitmap);
2594 : : ~eliminate_dom_walker ();
2595 : :
2596 : : edge before_dom_children (basic_block) final override;
2597 : : void after_dom_children (basic_block) final override;
2598 : :
2599 : : virtual tree eliminate_avail (basic_block, tree op);
2600 : : virtual void eliminate_push_avail (basic_block, tree op);
2601 : : tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2602 : :
2603 : : void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2604 : :
2605 : : unsigned eliminate_cleanup (bool region_p = false);
2606 : :
2607 : : bool do_pre;
2608 : : unsigned int el_todo;
2609 : : unsigned int eliminations;
2610 : : unsigned int insertions;
2611 : :
2612 : : /* SSA names that had their defs inserted by PRE if do_pre. */
2613 : : bitmap inserted_exprs;
2614 : :
2615 : : /* Blocks with statements that have had their EH properties changed. */
2616 : : bitmap need_eh_cleanup;
2617 : :
2618 : : /* Blocks with statements that have had their AB properties changed. */
2619 : : bitmap need_ab_cleanup;
2620 : :
2621 : : /* Local state for the eliminate domwalk. */
2622 : : auto_vec<gimple *> to_remove;
2623 : : auto_vec<gimple *> to_fixup;
2624 : : auto_vec<tree> avail;
2625 : : auto_vec<tree> avail_stack;
2626 : : };
2627 : :
2628 : : /* Adaptor to the elimination engine using RPO availability. */
2629 : :
2630 : 11268004 : class rpo_elim : public eliminate_dom_walker
2631 : : {
2632 : : public:
2633 : 5634002 : rpo_elim(basic_block entry_)
2634 : 11268004 : : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2635 : 11268004 : m_avail_freelist (NULL) {}
2636 : :
2637 : : tree eliminate_avail (basic_block, tree op) final override;
2638 : :
2639 : : void eliminate_push_avail (basic_block, tree) final override;
2640 : :
2641 : : basic_block entry;
2642 : : /* Freelist of avail entries which are allocated from the vn_ssa_aux
2643 : : obstack. */
2644 : : vn_avail *m_avail_freelist;
2645 : : };
2646 : :
2647 : : /* Global RPO state for access from hooks. */
2648 : : static eliminate_dom_walker *rpo_avail;
2649 : : basic_block vn_context_bb;
2650 : :
2651 : : /* Return true if BASE1 and BASE2 can be adjusted so they have the
2652 : : same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2653 : : Otherwise return false. */
2654 : :
2655 : : static bool
2656 : 4799101 : adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2657 : : tree base2, poly_int64 *offset2)
2658 : : {
2659 : 4799101 : poly_int64 soff;
2660 : 4799101 : if (TREE_CODE (base1) == MEM_REF
2661 : 2047401 : && TREE_CODE (base2) == MEM_REF)
2662 : : {
2663 : 1755157 : if (mem_ref_offset (base1).to_shwi (&soff))
2664 : : {
2665 : 1755157 : base1 = TREE_OPERAND (base1, 0);
2666 : 1755157 : *offset1 += soff * BITS_PER_UNIT;
2667 : : }
2668 : 1755157 : if (mem_ref_offset (base2).to_shwi (&soff))
2669 : : {
2670 : 1755157 : base2 = TREE_OPERAND (base2, 0);
2671 : 1755157 : *offset2 += soff * BITS_PER_UNIT;
2672 : : }
2673 : 1755157 : return operand_equal_p (base1, base2, 0);
2674 : : }
2675 : 3043944 : return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2676 : : }
2677 : :
2678 : : /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2679 : : from the statement defining VUSE and if not successful tries to
2680 : : translate *REFP and VR_ through an aggregate copy at the definition
2681 : : of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2682 : : of *REF and *VR. If only disambiguation was performed then
2683 : : *DISAMBIGUATE_ONLY is set to true. */
2684 : :
2685 : : static void *
2686 : 37293535 : vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2687 : : translate_flags *disambiguate_only)
2688 : : {
2689 : 37293535 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2690 : 37293535 : vn_reference_t vr = data->vr;
2691 : 37293535 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2692 : 37293535 : tree base = ao_ref_base (ref);
2693 : 37293535 : HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2694 : 37293535 : static vec<vn_reference_op_s> lhs_ops;
2695 : 37293535 : ao_ref lhs_ref;
2696 : 37293535 : bool lhs_ref_ok = false;
2697 : 37293535 : poly_int64 copy_size;
2698 : :
2699 : : /* First try to disambiguate after value-replacing in the definitions LHS. */
2700 : 37293535 : if (is_gimple_assign (def_stmt))
2701 : : {
2702 : 17507541 : tree lhs = gimple_assign_lhs (def_stmt);
2703 : 17507541 : bool valueized_anything = false;
2704 : : /* Avoid re-allocation overhead. */
2705 : 17507541 : lhs_ops.truncate (0);
2706 : 17507541 : basic_block saved_rpo_bb = vn_context_bb;
2707 : 17507541 : vn_context_bb = gimple_bb (def_stmt);
2708 : 17507541 : if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2709 : : {
2710 : 12763148 : copy_reference_ops_from_ref (lhs, &lhs_ops);
2711 : 12763148 : valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2712 : : }
2713 : 17507541 : vn_context_bb = saved_rpo_bb;
2714 : 17507541 : ao_ref_init (&lhs_ref, lhs);
2715 : 17507541 : lhs_ref_ok = true;
2716 : 17507541 : if (valueized_anything
2717 : 1626070 : && ao_ref_init_from_vn_reference
2718 : 1626070 : (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2719 : 1626070 : ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2720 : 19133430 : && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2721 : : {
2722 : 1416913 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2723 : 5633755 : return NULL;
2724 : : }
2725 : :
2726 : : /* When the def is a CLOBBER we can optimistically disambiguate
2727 : : against it since any overlap it would be undefined behavior.
2728 : : Avoid this for obvious must aliases to save compile-time though.
2729 : : We also may not do this when the query is used for redundant
2730 : : store removal. */
2731 : 16090628 : if (!data->redundant_store_removal_p
2732 : 8132225 : && gimple_clobber_p (def_stmt)
2733 : 16511788 : && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2734 : : {
2735 : 395002 : *disambiguate_only = TR_DISAMBIGUATE;
2736 : 395002 : return NULL;
2737 : : }
2738 : :
2739 : : /* Besides valueizing the LHS we can also use access-path based
2740 : : disambiguation on the original non-valueized ref. */
2741 : 15695626 : if (!ref->ref
2742 : : && lhs_ref_ok
2743 : 1916315 : && data->orig_ref.ref)
2744 : : {
2745 : : /* We want to use the non-valueized LHS for this, but avoid redundant
2746 : : work. */
2747 : 1095985 : ao_ref *lref = &lhs_ref;
2748 : 1095985 : ao_ref lref_alt;
2749 : 1095985 : if (valueized_anything)
2750 : : {
2751 : 97419 : ao_ref_init (&lref_alt, lhs);
2752 : 97419 : lref = &lref_alt;
2753 : : }
2754 : 1095985 : if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2755 : : {
2756 : 241790 : *disambiguate_only = (valueized_anything
2757 : 120895 : ? TR_VALUEIZE_AND_DISAMBIGUATE
2758 : : : TR_DISAMBIGUATE);
2759 : 120895 : return NULL;
2760 : : }
2761 : : }
2762 : :
2763 : : /* If we reach a clobbering statement try to skip it and see if
2764 : : we find a VN result with exactly the same value as the
2765 : : possible clobber. In this case we can ignore the clobber
2766 : : and return the found value. */
2767 : 15574731 : if (is_gimple_reg_type (TREE_TYPE (lhs))
2768 : 9156535 : && types_compatible_p (TREE_TYPE (lhs), vr->type)
2769 : 6781822 : && (ref->ref || data->orig_ref.ref)
2770 : 6415105 : && !data->mask
2771 : 6396542 : && data->partial_defs.is_empty ()
2772 : 6395491 : && multiple_p (get_object_alignment
2773 : : (ref->ref ? ref->ref : data->orig_ref.ref),
2774 : 6395491 : ref->size)
2775 : 28519710 : && multiple_p (get_object_alignment (lhs), ref->size))
2776 : : {
2777 : 6072476 : tree rhs = gimple_assign_rhs1 (def_stmt);
2778 : : /* ??? We may not compare to ahead values which might be from
2779 : : a different loop iteration but only to loop invariants. Use
2780 : : CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2781 : : The one-hop lookup below doesn't have this issue since there's
2782 : : a virtual PHI before we ever reach a backedge to cross.
2783 : : We can skip multiple defs as long as they are from the same
2784 : : value though. */
2785 : 6072476 : if (data->same_val
2786 : 6072476 : && !operand_equal_p (data->same_val, rhs))
2787 : : ;
2788 : 5824139 : else if (CONSTANT_CLASS_P (rhs))
2789 : : {
2790 : 2231828 : if (dump_file && (dump_flags & TDF_DETAILS))
2791 : : {
2792 : 977 : fprintf (dump_file,
2793 : : "Skipping possible redundant definition ");
2794 : 977 : print_gimple_stmt (dump_file, def_stmt, 0);
2795 : : }
2796 : : /* Delay the actual compare of the values to the end of the walk
2797 : : but do not update last_vuse from here. */
2798 : 2231828 : data->last_vuse_ptr = NULL;
2799 : 2231828 : data->same_val = rhs;
2800 : 2231828 : return NULL;
2801 : : }
2802 : : else
2803 : : {
2804 : 3592311 : tree saved_vuse = vr->vuse;
2805 : 3592311 : hashval_t saved_hashcode = vr->hashcode;
2806 : 3592311 : if (vr->vuse)
2807 : 3592311 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2808 : 7184622 : vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
2809 : 3592311 : if (vr->vuse)
2810 : 3592311 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2811 : 3592311 : vn_reference_t vnresult = NULL;
2812 : : /* Do not use vn_reference_lookup_2 since that might perform
2813 : : expression hashtable insertion but this lookup crosses
2814 : : a possible may-alias making such insertion conditionally
2815 : : invalid. */
2816 : 3592311 : vn_reference_lookup_1 (vr, &vnresult);
2817 : : /* Need to restore vr->vuse and vr->hashcode. */
2818 : 3592311 : vr->vuse = saved_vuse;
2819 : 3592311 : vr->hashcode = saved_hashcode;
2820 : 3592311 : if (vnresult)
2821 : : {
2822 : 198481 : if (TREE_CODE (rhs) == SSA_NAME)
2823 : 194039 : rhs = SSA_VAL (rhs);
2824 : 198481 : if (vnresult->result
2825 : 198481 : && operand_equal_p (vnresult->result, rhs, 0))
2826 : 52204 : return vnresult;
2827 : : }
2828 : : }
2829 : : }
2830 : : }
2831 : 19785994 : else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2832 : 18551856 : && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2833 : 21622138 : && gimple_call_num_args (def_stmt) <= 4)
2834 : : {
2835 : : /* For builtin calls valueize its arguments and call the
2836 : : alias oracle again. Valueization may improve points-to
2837 : : info of pointers and constify size and position arguments.
2838 : : Originally this was motivated by PR61034 which has
2839 : : conditional calls to free falsely clobbering ref because
2840 : : of imprecise points-to info of the argument. */
2841 : : tree oldargs[4];
2842 : : bool valueized_anything = false;
2843 : 4324139 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2844 : : {
2845 : 2977550 : oldargs[i] = gimple_call_arg (def_stmt, i);
2846 : 2977550 : tree val = vn_valueize (oldargs[i]);
2847 : 2977550 : if (val != oldargs[i])
2848 : : {
2849 : 100440 : gimple_call_set_arg (def_stmt, i, val);
2850 : 100440 : valueized_anything = true;
2851 : : }
2852 : : }
2853 : 1346589 : if (valueized_anything)
2854 : : {
2855 : 161076 : bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2856 : 80538 : ref, data->tbaa_p);
2857 : 296875 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2858 : 216337 : gimple_call_set_arg (def_stmt, i, oldargs[i]);
2859 : 80538 : if (!res)
2860 : : {
2861 : 23728 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2862 : 23728 : return NULL;
2863 : : }
2864 : : }
2865 : : }
2866 : :
2867 : 33052965 : if (*disambiguate_only > TR_TRANSLATE)
2868 : : return (void *)-1;
2869 : :
2870 : : /* If we cannot constrain the size of the reference we cannot
2871 : : test if anything kills it. */
2872 : 23039970 : if (!ref->max_size_known_p ())
2873 : : return (void *)-1;
2874 : :
2875 : 22710287 : poly_int64 offset = ref->offset;
2876 : 22710287 : poly_int64 maxsize = ref->max_size;
2877 : :
2878 : : /* def_stmt may-defs *ref. See if we can derive a value for *ref
2879 : : from that definition.
2880 : : 1) Memset. */
2881 : 22710287 : if (is_gimple_reg_type (vr->type)
2882 : 22397982 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2883 : 22327209 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
2884 : 71076 : && (integer_zerop (gimple_call_arg (def_stmt, 1))
2885 : 27415 : || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2886 : 8025 : || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2887 : : && CHAR_BIT == 8
2888 : : && BITS_PER_UNIT == 8
2889 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2890 : 26726 : && offset.is_constant (&offseti)
2891 : 26726 : && ref->size.is_constant (&sizei)
2892 : 26726 : && (offseti % BITS_PER_UNIT == 0
2893 : 39 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
2894 : 70387 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2895 : 26885 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
2896 : 26885 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
2897 : 22754156 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2898 : 22813 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2899 : : {
2900 : 43869 : tree base2;
2901 : 43869 : poly_int64 offset2, size2, maxsize2;
2902 : 43869 : bool reverse;
2903 : 43869 : tree ref2 = gimple_call_arg (def_stmt, 0);
2904 : 43869 : if (TREE_CODE (ref2) == SSA_NAME)
2905 : : {
2906 : 22813 : ref2 = SSA_VAL (ref2);
2907 : 22813 : if (TREE_CODE (ref2) == SSA_NAME
2908 : 22813 : && (TREE_CODE (base) != MEM_REF
2909 : 15543 : || TREE_OPERAND (base, 0) != ref2))
2910 : : {
2911 : 18813 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2912 : 18813 : if (gimple_assign_single_p (def_stmt)
2913 : 18813 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2914 : 688 : ref2 = gimple_assign_rhs1 (def_stmt);
2915 : : }
2916 : : }
2917 : 43869 : if (TREE_CODE (ref2) == ADDR_EXPR)
2918 : : {
2919 : 24718 : ref2 = TREE_OPERAND (ref2, 0);
2920 : 24718 : base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
2921 : : &reverse);
2922 : 24718 : if (!known_size_p (maxsize2)
2923 : 24684 : || !known_eq (maxsize2, size2)
2924 : 49328 : || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
2925 : 47537 : return (void *)-1;
2926 : : }
2927 : 19151 : else if (TREE_CODE (ref2) == SSA_NAME)
2928 : : {
2929 : 19151 : poly_int64 soff;
2930 : 19151 : if (TREE_CODE (base) != MEM_REF
2931 : 34206 : || !(mem_ref_offset (base)
2932 : 30109 : << LOG2_BITS_PER_UNIT).to_shwi (&soff))
2933 : 17759 : return (void *)-1;
2934 : 15054 : offset += soff;
2935 : 15054 : offset2 = 0;
2936 : 15054 : if (TREE_OPERAND (base, 0) != ref2)
2937 : : {
2938 : 14028 : gimple *def = SSA_NAME_DEF_STMT (ref2);
2939 : 14028 : if (is_gimple_assign (def)
2940 : 13283 : && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
2941 : 11286 : && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
2942 : 14424 : && poly_int_tree_p (gimple_assign_rhs2 (def)))
2943 : : {
2944 : 366 : tree rhs2 = gimple_assign_rhs2 (def);
2945 : 366 : if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
2946 : : SIGNED)
2947 : 366 : << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
2948 : : return (void *)-1;
2949 : 366 : ref2 = gimple_assign_rhs1 (def);
2950 : 366 : if (TREE_CODE (ref2) == SSA_NAME)
2951 : 366 : ref2 = SSA_VAL (ref2);
2952 : : }
2953 : : else
2954 : : return (void *)-1;
2955 : : }
2956 : : }
2957 : : else
2958 : : return (void *)-1;
2959 : 22206 : tree len = gimple_call_arg (def_stmt, 2);
2960 : 22206 : HOST_WIDE_INT leni, offset2i;
2961 : 22206 : if (TREE_CODE (len) == SSA_NAME)
2962 : 129 : len = SSA_VAL (len);
2963 : : /* Sometimes the above trickery is smarter than alias analysis. Take
2964 : : advantage of that. */
2965 : 22206 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
2966 : 44412 : (wi::to_poly_offset (len)
2967 : 22206 : << LOG2_BITS_PER_UNIT)))
2968 : : return NULL;
2969 : 44380 : if (data->partial_defs.is_empty ()
2970 : 22174 : && known_subrange_p (offset, maxsize, offset2,
2971 : 22174 : wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
2972 : : {
2973 : 21886 : tree val;
2974 : 21886 : if (integer_zerop (gimple_call_arg (def_stmt, 1)))
2975 : 18449 : val = build_zero_cst (vr->type);
2976 : 3437 : else if (INTEGRAL_TYPE_P (vr->type)
2977 : 3151 : && known_eq (ref->size, 8)
2978 : 5905 : && offseti % BITS_PER_UNIT == 0)
2979 : : {
2980 : 2468 : gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
2981 : 2468 : vr->type, gimple_call_arg (def_stmt, 1));
2982 : 2468 : val = vn_nary_build_or_lookup (&res_op);
2983 : 2468 : if (!val
2984 : 2468 : || (TREE_CODE (val) == SSA_NAME
2985 : 626 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
2986 : 0 : return (void *)-1;
2987 : : }
2988 : : else
2989 : : {
2990 : 969 : unsigned buflen
2991 : 969 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
2992 : 969 : if (INTEGRAL_TYPE_P (vr->type)
2993 : 969 : && TYPE_MODE (vr->type) != BLKmode)
2994 : 1364 : buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
2995 : 969 : unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
2996 : 969 : memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
2997 : : buflen);
2998 : 969 : if (BYTES_BIG_ENDIAN)
2999 : : {
3000 : : unsigned int amnt
3001 : : = (((unsigned HOST_WIDE_INT) offseti + sizei)
3002 : : % BITS_PER_UNIT);
3003 : : if (amnt)
3004 : : {
3005 : : shift_bytes_in_array_right (buf, buflen,
3006 : : BITS_PER_UNIT - amnt);
3007 : : buf++;
3008 : : buflen--;
3009 : : }
3010 : : }
3011 : 969 : else if (offseti % BITS_PER_UNIT != 0)
3012 : : {
3013 : 7 : unsigned int amnt
3014 : : = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3015 : 7 : % BITS_PER_UNIT);
3016 : 7 : shift_bytes_in_array_left (buf, buflen, amnt);
3017 : 7 : buf++;
3018 : 7 : buflen--;
3019 : : }
3020 : 969 : val = native_interpret_expr (vr->type, buf, buflen);
3021 : 969 : if (!val)
3022 : : return (void *)-1;
3023 : : }
3024 : 21886 : return data->finish (0, 0, val);
3025 : : }
3026 : : /* For now handle clearing memory with partial defs. */
3027 : 320 : else if (known_eq (ref->size, maxsize)
3028 : 254 : && integer_zerop (gimple_call_arg (def_stmt, 1))
3029 : 88 : && tree_fits_poly_int64_p (len)
3030 : 84 : && tree_to_poly_int64 (len).is_constant (&leni)
3031 : 84 : && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3032 : 84 : && offset.is_constant (&offseti)
3033 : 84 : && offset2.is_constant (&offset2i)
3034 : 84 : && maxsize.is_constant (&maxsizei)
3035 : 320 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3036 : 320 : leni << LOG2_BITS_PER_UNIT))
3037 : : {
3038 : 84 : pd_data pd;
3039 : 84 : pd.rhs = build_constructor (NULL_TREE, NULL);
3040 : 84 : pd.rhs_off = 0;
3041 : 84 : pd.offset = offset2i;
3042 : 84 : pd.size = leni << LOG2_BITS_PER_UNIT;
3043 : 84 : return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3044 : : }
3045 : : }
3046 : :
3047 : : /* 2) Assignment from an empty CONSTRUCTOR. */
3048 : 22666418 : else if (is_gimple_reg_type (vr->type)
3049 : 22354113 : && gimple_assign_single_p (def_stmt)
3050 : 7738032 : && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3051 : 25300369 : && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
3052 : : {
3053 : 2633951 : tree base2;
3054 : 2633951 : poly_int64 offset2, size2, maxsize2;
3055 : 2633951 : HOST_WIDE_INT offset2i, size2i;
3056 : 2633951 : gcc_assert (lhs_ref_ok);
3057 : 2633951 : base2 = ao_ref_base (&lhs_ref);
3058 : 2633951 : offset2 = lhs_ref.offset;
3059 : 2633951 : size2 = lhs_ref.size;
3060 : 2633951 : maxsize2 = lhs_ref.max_size;
3061 : 2633951 : if (known_size_p (maxsize2)
3062 : 2633621 : && known_eq (maxsize2, size2)
3063 : 5266996 : && adjust_offsets_for_equal_base_address (base, &offset,
3064 : : base2, &offset2))
3065 : : {
3066 : 2577135 : if (data->partial_defs.is_empty ()
3067 : 2574753 : && known_subrange_p (offset, maxsize, offset2, size2))
3068 : : {
3069 : : /* While technically undefined behavior do not optimize
3070 : : a full read from a clobber. */
3071 : 2565019 : if (gimple_clobber_p (def_stmt))
3072 : 2576449 : return (void *)-1;
3073 : 750398 : tree val = build_zero_cst (vr->type);
3074 : 750398 : return data->finish (ao_ref_alias_set (&lhs_ref),
3075 : 750398 : ao_ref_base_alias_set (&lhs_ref), val);
3076 : : }
3077 : 12116 : else if (known_eq (ref->size, maxsize)
3078 : 11430 : && maxsize.is_constant (&maxsizei)
3079 : 11430 : && offset.is_constant (&offseti)
3080 : 11430 : && offset2.is_constant (&offset2i)
3081 : 11430 : && size2.is_constant (&size2i)
3082 : 12116 : && ranges_known_overlap_p (offseti, maxsizei,
3083 : : offset2i, size2i))
3084 : : {
3085 : : /* Let clobbers be consumed by the partial-def tracker
3086 : : which can choose to ignore them if they are shadowed
3087 : : by a later def. */
3088 : 11430 : pd_data pd;
3089 : 11430 : pd.rhs = gimple_assign_rhs1 (def_stmt);
3090 : 11430 : pd.rhs_off = 0;
3091 : 11430 : pd.offset = offset2i;
3092 : 11430 : pd.size = size2i;
3093 : 11430 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3094 : : ao_ref_base_alias_set (&lhs_ref),
3095 : : offseti, maxsizei);
3096 : : }
3097 : : }
3098 : : }
3099 : :
3100 : : /* 3) Assignment from a constant. We can use folds native encode/interpret
3101 : : routines to extract the assigned bits. */
3102 : 20032467 : else if (known_eq (ref->size, maxsize)
3103 : 19588837 : && is_gimple_reg_type (vr->type)
3104 : 19276532 : && !reverse_storage_order_for_component_p (vr->operands)
3105 : 19273887 : && !contains_storage_order_barrier_p (vr->operands)
3106 : 19273887 : && gimple_assign_single_p (def_stmt)
3107 : : && CHAR_BIT == 8
3108 : : && BITS_PER_UNIT == 8
3109 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3110 : : /* native_encode and native_decode operate on arrays of bytes
3111 : : and so fundamentally need a compile-time size and offset. */
3112 : 4845417 : && maxsize.is_constant (&maxsizei)
3113 : 4845417 : && offset.is_constant (&offseti)
3114 : 24877884 : && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3115 : 4216537 : || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3116 : 1654274 : && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3117 : : {
3118 : 643792 : tree lhs = gimple_assign_lhs (def_stmt);
3119 : 643792 : tree base2;
3120 : 643792 : poly_int64 offset2, size2, maxsize2;
3121 : 643792 : HOST_WIDE_INT offset2i, size2i;
3122 : 643792 : bool reverse;
3123 : 643792 : gcc_assert (lhs_ref_ok);
3124 : 643792 : base2 = ao_ref_base (&lhs_ref);
3125 : 643792 : offset2 = lhs_ref.offset;
3126 : 643792 : size2 = lhs_ref.size;
3127 : 643792 : maxsize2 = lhs_ref.max_size;
3128 : 643792 : reverse = reverse_storage_order_for_component_p (lhs);
3129 : 643792 : if (base2
3130 : 643792 : && !reverse
3131 : 643792 : && !storage_order_barrier_p (lhs)
3132 : 643792 : && known_eq (maxsize2, size2)
3133 : 627331 : && adjust_offsets_for_equal_base_address (base, &offset,
3134 : : base2, &offset2)
3135 : 60996 : && offset.is_constant (&offseti)
3136 : 60996 : && offset2.is_constant (&offset2i)
3137 : 643792 : && size2.is_constant (&size2i))
3138 : : {
3139 : 60996 : if (data->partial_defs.is_empty ()
3140 : 46797 : && known_subrange_p (offseti, maxsizei, offset2, size2))
3141 : : {
3142 : : /* We support up to 512-bit values (for V8DFmode). */
3143 : 34975 : unsigned char buffer[65];
3144 : 34975 : int len;
3145 : :
3146 : 34975 : tree rhs = gimple_assign_rhs1 (def_stmt);
3147 : 34975 : if (TREE_CODE (rhs) == SSA_NAME)
3148 : 1411 : rhs = SSA_VAL (rhs);
3149 : 69950 : len = native_encode_expr (rhs,
3150 : : buffer, sizeof (buffer) - 1,
3151 : 34975 : (offseti - offset2i) / BITS_PER_UNIT);
3152 : 34975 : if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
3153 : : {
3154 : 32002 : tree type = vr->type;
3155 : 32002 : unsigned char *buf = buffer;
3156 : 32002 : unsigned int amnt = 0;
3157 : : /* Make sure to interpret in a type that has a range
3158 : : covering the whole access size. */
3159 : 32002 : if (INTEGRAL_TYPE_P (vr->type)
3160 : 32002 : && maxsizei != TYPE_PRECISION (vr->type))
3161 : 1846 : type = build_nonstandard_integer_type (maxsizei,
3162 : 923 : TYPE_UNSIGNED (type));
3163 : 32002 : if (BYTES_BIG_ENDIAN)
3164 : : {
3165 : : /* For big-endian native_encode_expr stored the rhs
3166 : : such that the LSB of it is the LSB of buffer[len - 1].
3167 : : That bit is stored into memory at position
3168 : : offset2 + size2 - 1, i.e. in byte
3169 : : base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3170 : : E.g. for offset2 1 and size2 14, rhs -1 and memory
3171 : : previously cleared that is:
3172 : : 0 1
3173 : : 01111111|11111110
3174 : : Now, if we want to extract offset 2 and size 12 from
3175 : : it using native_interpret_expr (which actually works
3176 : : for integral bitfield types in terms of byte size of
3177 : : the mode), the native_encode_expr stored the value
3178 : : into buffer as
3179 : : XX111111|11111111
3180 : : and returned len 2 (the X bits are outside of
3181 : : precision).
3182 : : Let sz be maxsize / BITS_PER_UNIT if not extracting
3183 : : a bitfield, and GET_MODE_SIZE otherwise.
3184 : : We need to align the LSB of the value we want to
3185 : : extract as the LSB of buf[sz - 1].
3186 : : The LSB from memory we need to read is at position
3187 : : offset + maxsize - 1. */
3188 : : HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3189 : : if (INTEGRAL_TYPE_P (type))
3190 : : {
3191 : : if (TYPE_MODE (type) != BLKmode)
3192 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3193 : : else
3194 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3195 : : }
3196 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3197 : : - offseti - maxsizei) % BITS_PER_UNIT;
3198 : : if (amnt)
3199 : : shift_bytes_in_array_right (buffer, len, amnt);
3200 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3201 : : - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3202 : : if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3203 : : len = 0;
3204 : : else
3205 : : {
3206 : : buf = buffer + len - sz - amnt;
3207 : : len -= (buf - buffer);
3208 : : }
3209 : : }
3210 : : else
3211 : : {
3212 : 32002 : amnt = ((unsigned HOST_WIDE_INT) offset2i
3213 : 32002 : - offseti) % BITS_PER_UNIT;
3214 : 32002 : if (amnt)
3215 : : {
3216 : 324 : buffer[len] = 0;
3217 : 324 : shift_bytes_in_array_left (buffer, len + 1, amnt);
3218 : 324 : buf = buffer + 1;
3219 : : }
3220 : : }
3221 : 32002 : tree val = native_interpret_expr (type, buf, len);
3222 : : /* If we chop off bits because the types precision doesn't
3223 : : match the memory access size this is ok when optimizing
3224 : : reads but not when called from the DSE code during
3225 : : elimination. */
3226 : 32002 : if (val
3227 : 32002 : && type != vr->type)
3228 : : {
3229 : 923 : if (! int_fits_type_p (val, vr->type))
3230 : : val = NULL_TREE;
3231 : : else
3232 : 923 : val = fold_convert (vr->type, val);
3233 : : }
3234 : :
3235 : 32002 : if (val)
3236 : 32002 : return data->finish (ao_ref_alias_set (&lhs_ref),
3237 : 32002 : ao_ref_base_alias_set (&lhs_ref), val);
3238 : : }
3239 : : }
3240 : 26021 : else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3241 : : size2i))
3242 : : {
3243 : 26021 : pd_data pd;
3244 : 26021 : tree rhs = gimple_assign_rhs1 (def_stmt);
3245 : 26021 : if (TREE_CODE (rhs) == SSA_NAME)
3246 : 2800 : rhs = SSA_VAL (rhs);
3247 : 26021 : pd.rhs = rhs;
3248 : 26021 : pd.rhs_off = 0;
3249 : 26021 : pd.offset = offset2i;
3250 : 26021 : pd.size = size2i;
3251 : 26021 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3252 : : ao_ref_base_alias_set (&lhs_ref),
3253 : : offseti, maxsizei);
3254 : : }
3255 : : }
3256 : : }
3257 : :
3258 : : /* 4) Assignment from an SSA name which definition we may be able
3259 : : to access pieces from or we can combine to a larger entity. */
3260 : 19388675 : else if (known_eq (ref->size, maxsize)
3261 : 18945045 : && is_gimple_reg_type (vr->type)
3262 : 18632740 : && !reverse_storage_order_for_component_p (vr->operands)
3263 : 18630095 : && !contains_storage_order_barrier_p (vr->operands)
3264 : 18630095 : && gimple_assign_single_p (def_stmt)
3265 : 23590300 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3266 : : {
3267 : 1639362 : tree lhs = gimple_assign_lhs (def_stmt);
3268 : 1639362 : tree base2;
3269 : 1639362 : poly_int64 offset2, size2, maxsize2;
3270 : 1639362 : HOST_WIDE_INT offset2i, size2i, offseti;
3271 : 1639362 : bool reverse;
3272 : 1639362 : gcc_assert (lhs_ref_ok);
3273 : 1639362 : base2 = ao_ref_base (&lhs_ref);
3274 : 1639362 : offset2 = lhs_ref.offset;
3275 : 1639362 : size2 = lhs_ref.size;
3276 : 1639362 : maxsize2 = lhs_ref.max_size;
3277 : 1639362 : reverse = reverse_storage_order_for_component_p (lhs);
3278 : 1639362 : tree def_rhs = gimple_assign_rhs1 (def_stmt);
3279 : 1639362 : if (!reverse
3280 : 1639362 : && !storage_order_barrier_p (lhs)
3281 : 1639362 : && known_size_p (maxsize2)
3282 : 1623604 : && known_eq (maxsize2, size2)
3283 : 3178073 : && adjust_offsets_for_equal_base_address (base, &offset,
3284 : : base2, &offset2))
3285 : : {
3286 : 78404 : if (data->partial_defs.is_empty ()
3287 : 72474 : && known_subrange_p (offset, maxsize, offset2, size2)
3288 : : /* ??? We can't handle bitfield precision extracts without
3289 : : either using an alternate type for the BIT_FIELD_REF and
3290 : : then doing a conversion or possibly adjusting the offset
3291 : : according to endianness. */
3292 : 49240 : && (! INTEGRAL_TYPE_P (vr->type)
3293 : 33887 : || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3294 : 93040 : && multiple_p (ref->size, BITS_PER_UNIT))
3295 : : {
3296 : 44830 : tree val = NULL_TREE;
3297 : 89654 : if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3298 : 49005 : || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3299 : : {
3300 : : gimple_match_op op (gimple_match_cond::UNCOND,
3301 : : BIT_FIELD_REF, vr->type,
3302 : : SSA_VAL (def_rhs),
3303 : : bitsize_int (ref->size),
3304 : 43844 : bitsize_int (offset - offset2));
3305 : 43844 : val = vn_nary_build_or_lookup (&op);
3306 : : }
3307 : 986 : else if (known_eq (ref->size, size2))
3308 : : {
3309 : 964 : gimple_match_op op (gimple_match_cond::UNCOND,
3310 : : VIEW_CONVERT_EXPR, vr->type,
3311 : 964 : SSA_VAL (def_rhs));
3312 : 964 : val = vn_nary_build_or_lookup (&op);
3313 : : }
3314 : 44808 : if (val
3315 : 44808 : && (TREE_CODE (val) != SSA_NAME
3316 : 44268 : || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3317 : 44789 : return data->finish (ao_ref_alias_set (&lhs_ref),
3318 : 78363 : ao_ref_base_alias_set (&lhs_ref), val);
3319 : : }
3320 : 33574 : else if (maxsize.is_constant (&maxsizei)
3321 : 33574 : && offset.is_constant (&offseti)
3322 : 33574 : && offset2.is_constant (&offset2i)
3323 : 33574 : && size2.is_constant (&size2i)
3324 : 33574 : && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3325 : : {
3326 : 33574 : pd_data pd;
3327 : 33574 : pd.rhs = SSA_VAL (def_rhs);
3328 : 33574 : pd.rhs_off = 0;
3329 : 33574 : pd.offset = offset2i;
3330 : 33574 : pd.size = size2i;
3331 : 33574 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3332 : : ao_ref_base_alias_set (&lhs_ref),
3333 : : offseti, maxsizei);
3334 : : }
3335 : : }
3336 : : }
3337 : :
3338 : : /* 4b) Assignment done via one of the vectorizer internal store
3339 : : functions where we may be able to access pieces from or we can
3340 : : combine to a larger entity. */
3341 : 17749313 : else if (known_eq (ref->size, maxsize)
3342 : 17305683 : && is_gimple_reg_type (vr->type)
3343 : 16993378 : && !reverse_storage_order_for_component_p (vr->operands)
3344 : 16990733 : && !contains_storage_order_barrier_p (vr->operands)
3345 : 16990733 : && is_gimple_call (def_stmt)
3346 : 13677064 : && gimple_call_internal_p (def_stmt)
3347 : 17801072 : && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3348 : : {
3349 : 43 : gcall *call = as_a <gcall *> (def_stmt);
3350 : 43 : internal_fn fn = gimple_call_internal_fn (call);
3351 : :
3352 : 43 : tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3353 : 43 : switch (fn)
3354 : : {
3355 : 43 : case IFN_MASK_STORE:
3356 : 43 : mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3357 : 43 : mask = vn_valueize (mask);
3358 : 43 : if (TREE_CODE (mask) != VECTOR_CST)
3359 : 35 : return (void *)-1;
3360 : : break;
3361 : 0 : case IFN_LEN_STORE:
3362 : 0 : {
3363 : 0 : int len_index = internal_fn_len_index (fn);
3364 : 0 : len = gimple_call_arg (call, len_index);
3365 : 0 : bias = gimple_call_arg (call, len_index + 1);
3366 : 0 : if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3367 : : return (void *) -1;
3368 : : break;
3369 : : }
3370 : : default:
3371 : : return (void *)-1;
3372 : : }
3373 : 14 : tree def_rhs = gimple_call_arg (call,
3374 : 14 : internal_fn_stored_value_index (fn));
3375 : 14 : def_rhs = vn_valueize (def_rhs);
3376 : 14 : if (TREE_CODE (def_rhs) != VECTOR_CST)
3377 : : return (void *)-1;
3378 : :
3379 : 14 : ao_ref_init_from_ptr_and_size (&lhs_ref,
3380 : : vn_valueize (gimple_call_arg (call, 0)),
3381 : 14 : TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3382 : 14 : tree base2;
3383 : 14 : poly_int64 offset2, size2, maxsize2;
3384 : 14 : HOST_WIDE_INT offset2i, size2i, offseti;
3385 : 14 : base2 = ao_ref_base (&lhs_ref);
3386 : 14 : offset2 = lhs_ref.offset;
3387 : 14 : size2 = lhs_ref.size;
3388 : 14 : maxsize2 = lhs_ref.max_size;
3389 : 28 : if (known_size_p (maxsize2)
3390 : 14 : && known_eq (maxsize2, size2)
3391 : 14 : && adjust_offsets_for_equal_base_address (base, &offset,
3392 : : base2, &offset2)
3393 : 6 : && maxsize.is_constant (&maxsizei)
3394 : 6 : && offset.is_constant (&offseti)
3395 : 6 : && offset2.is_constant (&offset2i)
3396 : 20 : && size2.is_constant (&size2i))
3397 : : {
3398 : 6 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3399 : : /* Poor-mans disambiguation. */
3400 : : return NULL;
3401 : 6 : else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3402 : : {
3403 : 6 : pd_data pd;
3404 : 6 : pd.rhs = def_rhs;
3405 : 6 : tree aa = gimple_call_arg (call, 1);
3406 : 6 : alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3407 : 6 : tree vectype = TREE_TYPE (def_rhs);
3408 : 6 : unsigned HOST_WIDE_INT elsz
3409 : 6 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3410 : 6 : if (mask)
3411 : : {
3412 : : HOST_WIDE_INT start = 0, length = 0;
3413 : : unsigned mask_idx = 0;
3414 : 48 : do
3415 : : {
3416 : 48 : if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3417 : : {
3418 : 24 : if (length != 0)
3419 : : {
3420 : 18 : pd.rhs_off = start;
3421 : 18 : pd.offset = offset2i + start;
3422 : 18 : pd.size = length;
3423 : 18 : if (ranges_known_overlap_p
3424 : 18 : (offset, maxsize, pd.offset, pd.size))
3425 : : {
3426 : 0 : void *res = data->push_partial_def
3427 : 0 : (pd, set, set, offseti, maxsizei);
3428 : 0 : if (res != NULL)
3429 : 6 : return res;
3430 : : }
3431 : : }
3432 : 24 : start = (mask_idx + 1) * elsz;
3433 : 24 : length = 0;
3434 : : }
3435 : : else
3436 : 24 : length += elsz;
3437 : 48 : mask_idx++;
3438 : : }
3439 : 48 : while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3440 : 6 : if (length != 0)
3441 : : {
3442 : 6 : pd.rhs_off = start;
3443 : 6 : pd.offset = offset2i + start;
3444 : 6 : pd.size = length;
3445 : 6 : if (ranges_known_overlap_p (offset, maxsize,
3446 : : pd.offset, pd.size))
3447 : 2 : return data->push_partial_def (pd, set, set,
3448 : 2 : offseti, maxsizei);
3449 : : }
3450 : : }
3451 : 0 : else if (fn == IFN_LEN_STORE)
3452 : : {
3453 : 0 : pd.offset = offset2i;
3454 : 0 : pd.size = (tree_to_uhwi (len)
3455 : 0 : + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3456 : 0 : if (BYTES_BIG_ENDIAN)
3457 : : pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3458 : : else
3459 : 0 : pd.rhs_off = 0;
3460 : 0 : if (ranges_known_overlap_p (offset, maxsize,
3461 : : pd.offset, pd.size))
3462 : 0 : return data->push_partial_def (pd, set, set,
3463 : 0 : offseti, maxsizei);
3464 : : }
3465 : : else
3466 : 0 : gcc_unreachable ();
3467 : 4 : return NULL;
3468 : : }
3469 : : }
3470 : : }
3471 : :
3472 : : /* 5) For aggregate copies translate the reference through them if
3473 : : the copy kills ref. */
3474 : 17749270 : else if (data->vn_walk_kind == VN_WALKREWRITE
3475 : 13987426 : && gimple_assign_single_p (def_stmt)
3476 : 20045425 : && (DECL_P (gimple_assign_rhs1 (def_stmt))
3477 : 1894793 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3478 : 1512707 : || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3479 : : {
3480 : 2116076 : tree base2;
3481 : 2116076 : int i, j, k;
3482 : 2116076 : auto_vec<vn_reference_op_s> rhs;
3483 : 2116076 : vn_reference_op_t vro;
3484 : 2116076 : ao_ref r;
3485 : :
3486 : 2116076 : gcc_assert (lhs_ref_ok);
3487 : :
3488 : : /* See if the assignment kills REF. */
3489 : 2116076 : base2 = ao_ref_base (&lhs_ref);
3490 : 2116076 : if (!lhs_ref.max_size_known_p ()
3491 : 2115904 : || (base != base2
3492 : 70421 : && (TREE_CODE (base) != MEM_REF
3493 : 58368 : || TREE_CODE (base2) != MEM_REF
3494 : 48822 : || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3495 : 15719 : || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3496 : 15719 : TREE_OPERAND (base2, 1))))
3497 : 4176252 : || !stmt_kills_ref_p (def_stmt, ref))
3498 : 418378 : return (void *)-1;
3499 : :
3500 : : /* Find the common base of ref and the lhs. lhs_ops already
3501 : : contains valueized operands for the lhs. */
3502 : 1697698 : i = vr->operands.length () - 1;
3503 : 1697698 : j = lhs_ops.length () - 1;
3504 : 1697698 : while (j >= 0 && i >= 0
3505 : 4497474 : && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3506 : : {
3507 : 2799776 : i--;
3508 : 2799776 : j--;
3509 : : }
3510 : :
3511 : : /* ??? The innermost op should always be a MEM_REF and we already
3512 : : checked that the assignment to the lhs kills vr. Thus for
3513 : : aggregate copies using char[] types the vn_reference_op_eq
3514 : : may fail when comparing types for compatibility. But we really
3515 : : don't care here - further lookups with the rewritten operands
3516 : : will simply fail if we messed up types too badly. */
3517 : 1697698 : poly_int64 extra_off = 0;
3518 : 1697698 : if (j == 0 && i >= 0
3519 : 596963 : && lhs_ops[0].opcode == MEM_REF
3520 : 2293329 : && maybe_ne (lhs_ops[0].off, -1))
3521 : : {
3522 : 595631 : if (known_eq (lhs_ops[0].off, vr->operands[i].off))
3523 : 163379 : i--, j--;
3524 : 432252 : else if (vr->operands[i].opcode == MEM_REF
3525 : 432252 : && maybe_ne (vr->operands[i].off, -1))
3526 : : {
3527 : 429348 : extra_off = vr->operands[i].off - lhs_ops[0].off;
3528 : 429348 : i--, j--;
3529 : : }
3530 : : }
3531 : :
3532 : : /* i now points to the first additional op.
3533 : : ??? LHS may not be completely contained in VR, one or more
3534 : : VIEW_CONVERT_EXPRs could be in its way. We could at least
3535 : : try handling outermost VIEW_CONVERT_EXPRs. */
3536 : 1697698 : if (j != -1)
3537 : : return (void *)-1;
3538 : :
3539 : : /* Punt if the additional ops contain a storage order barrier. */
3540 : 3245990 : for (k = i; k >= 0; k--)
3541 : : {
3542 : 1573051 : vro = &vr->operands[k];
3543 : 1573051 : if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3544 : : return (void *)-1;
3545 : : }
3546 : :
3547 : : /* Now re-write REF to be based on the rhs of the assignment. */
3548 : 1672939 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
3549 : 1672939 : copy_reference_ops_from_ref (rhs1, &rhs);
3550 : :
3551 : : /* Apply an extra offset to the inner MEM_REF of the RHS. */
3552 : 1672939 : bool force_no_tbaa = false;
3553 : 1672939 : if (maybe_ne (extra_off, 0))
3554 : : {
3555 : 429348 : if (rhs.length () < 2)
3556 : : return (void *)-1;
3557 : 429348 : int ix = rhs.length () - 2;
3558 : 429348 : if (rhs[ix].opcode != MEM_REF
3559 : 429348 : || known_eq (rhs[ix].off, -1))
3560 : : return (void *)-1;
3561 : 429348 : rhs[ix].off += extra_off;
3562 : 429348 : rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3563 : 429348 : build_int_cst (TREE_TYPE (rhs[ix].op0),
3564 : : extra_off));
3565 : : /* When we have offsetted the RHS, reading only parts of it,
3566 : : we can no longer use the original TBAA type, force alias-set
3567 : : zero. */
3568 : 429348 : force_no_tbaa = true;
3569 : : }
3570 : :
3571 : : /* Save the operands since we need to use the original ones for
3572 : : the hash entry we use. */
3573 : 1672939 : if (!data->saved_operands.exists ())
3574 : 1581194 : data->saved_operands = vr->operands.copy ();
3575 : :
3576 : : /* We need to pre-pend vr->operands[0..i] to rhs. */
3577 : 1672939 : vec<vn_reference_op_s> old = vr->operands;
3578 : 5018817 : if (i + 1 + rhs.length () > vr->operands.length ())
3579 : 1119143 : vr->operands.safe_grow (i + 1 + rhs.length (), true);
3580 : : else
3581 : 553796 : vr->operands.truncate (i + 1 + rhs.length ());
3582 : 6158728 : FOR_EACH_VEC_ELT (rhs, j, vro)
3583 : 4485789 : vr->operands[i + 1 + j] = *vro;
3584 : 1672939 : valueize_refs (&vr->operands);
3585 : 3345878 : if (old == shared_lookup_references)
3586 : 1672939 : shared_lookup_references = vr->operands;
3587 : 1672939 : vr->hashcode = vn_reference_compute_hash (vr);
3588 : :
3589 : : /* Try folding the new reference to a constant. */
3590 : 1672939 : tree val = fully_constant_vn_reference_p (vr);
3591 : 1672939 : if (val)
3592 : : {
3593 : 17085 : if (data->partial_defs.is_empty ())
3594 : 17080 : return data->finish (ao_ref_alias_set (&lhs_ref),
3595 : 17080 : ao_ref_base_alias_set (&lhs_ref), val);
3596 : : /* This is the only interesting case for partial-def handling
3597 : : coming from targets that like to gimplify init-ctors as
3598 : : aggregate copies from constant data like aarch64 for
3599 : : PR83518. */
3600 : 5 : if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3601 : : {
3602 : 5 : pd_data pd;
3603 : 5 : pd.rhs = val;
3604 : 5 : pd.rhs_off = 0;
3605 : 5 : pd.offset = 0;
3606 : 5 : pd.size = maxsizei;
3607 : 5 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3608 : : ao_ref_base_alias_set (&lhs_ref),
3609 : : 0, maxsizei);
3610 : : }
3611 : : }
3612 : :
3613 : : /* Continuing with partial defs isn't easily possible here, we
3614 : : have to find a full def from further lookups from here. Probably
3615 : : not worth the special-casing everywhere. */
3616 : 2103063 : if (!data->partial_defs.is_empty ())
3617 : : return (void *)-1;
3618 : :
3619 : : /* Adjust *ref from the new operands. */
3620 : 1651888 : ao_ref rhs1_ref;
3621 : 1651888 : ao_ref_init (&rhs1_ref, rhs1);
3622 : 2877168 : if (!ao_ref_init_from_vn_reference (&r,
3623 : : force_no_tbaa ? 0
3624 : 1225280 : : ao_ref_alias_set (&rhs1_ref),
3625 : : force_no_tbaa ? 0
3626 : 1225280 : : ao_ref_base_alias_set (&rhs1_ref),
3627 : : vr->type, vr->operands))
3628 : : return (void *)-1;
3629 : : /* This can happen with bitfields. */
3630 : 1651782 : if (maybe_ne (ref->size, r.size))
3631 : : {
3632 : : /* If the access lacks some subsetting simply apply that by
3633 : : shortening it. That in the end can only be successful
3634 : : if we can pun the lookup result which in turn requires
3635 : : exact offsets. */
3636 : 0 : if (known_eq (r.size, r.max_size)
3637 : 0 : && known_lt (ref->size, r.size))
3638 : 0 : r.size = r.max_size = ref->size;
3639 : : else
3640 : : return (void *)-1;
3641 : : }
3642 : 1651782 : *ref = r;
3643 : 1651782 : vr->offset = r.offset;
3644 : 1651782 : vr->max_size = r.max_size;
3645 : :
3646 : : /* Do not update last seen VUSE after translating. */
3647 : 1651782 : data->last_vuse_ptr = NULL;
3648 : : /* Invalidate the original access path since it now contains
3649 : : the wrong base. */
3650 : 1651782 : data->orig_ref.ref = NULL_TREE;
3651 : : /* Use the alias-set of this LHS for recording an eventual result. */
3652 : 1651782 : if (data->first_set == -2)
3653 : : {
3654 : 1561331 : data->first_set = ao_ref_alias_set (&lhs_ref);
3655 : 1561331 : data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3656 : : }
3657 : :
3658 : : /* Keep looking for the adjusted *REF / VR pair. */
3659 : 1651782 : return NULL;
3660 : 2116076 : }
3661 : :
3662 : : /* 6) For memcpy copies translate the reference through them if the copy
3663 : : kills ref. But we cannot (easily) do this translation if the memcpy is
3664 : : a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3665 : : can modify the storage order of objects (see storage_order_barrier_p). */
3666 : 15633194 : else if (data->vn_walk_kind == VN_WALKREWRITE
3667 : 11871350 : && is_gimple_reg_type (vr->type)
3668 : : /* ??? Handle BCOPY as well. */
3669 : 11862897 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3670 : 11788389 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3671 : 11788025 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3672 : 11786944 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3673 : 11786726 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3674 : 11762770 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3675 : 100414 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3676 : 93262 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3677 : 100382 : && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3678 : 62213 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3679 : 100367 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), ©_size)
3680 : 57181 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3681 : 57181 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3682 : : ©_size)))
3683 : : /* Handling this is more complicated, give up for now. */
3684 : 15677676 : && data->partial_defs.is_empty ())
3685 : : {
3686 : 44213 : tree lhs, rhs;
3687 : 44213 : ao_ref r;
3688 : 44213 : poly_int64 rhs_offset, lhs_offset;
3689 : 44213 : vn_reference_op_s op;
3690 : 44213 : poly_uint64 mem_offset;
3691 : 44213 : poly_int64 at, byte_maxsize;
3692 : :
3693 : : /* Only handle non-variable, addressable refs. */
3694 : 44213 : if (maybe_ne (ref->size, maxsize)
3695 : 43604 : || !multiple_p (offset, BITS_PER_UNIT, &at)
3696 : 44213 : || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3697 : 609 : return (void *)-1;
3698 : :
3699 : : /* Extract a pointer base and an offset for the destination. */
3700 : 43604 : lhs = gimple_call_arg (def_stmt, 0);
3701 : 43604 : lhs_offset = 0;
3702 : 43604 : if (TREE_CODE (lhs) == SSA_NAME)
3703 : : {
3704 : 37551 : lhs = vn_valueize (lhs);
3705 : 37551 : if (TREE_CODE (lhs) == SSA_NAME)
3706 : : {
3707 : 37014 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3708 : 37014 : if (gimple_assign_single_p (def_stmt)
3709 : 37014 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3710 : 2903 : lhs = gimple_assign_rhs1 (def_stmt);
3711 : : }
3712 : : }
3713 : 43604 : if (TREE_CODE (lhs) == ADDR_EXPR)
3714 : : {
3715 : 12963 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3716 : 12740 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3717 : : return (void *)-1;
3718 : 9409 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3719 : : &lhs_offset);
3720 : 9409 : if (!tem)
3721 : : return (void *)-1;
3722 : 8922 : if (TREE_CODE (tem) == MEM_REF
3723 : 8922 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3724 : : {
3725 : 2416 : lhs = TREE_OPERAND (tem, 0);
3726 : 2416 : if (TREE_CODE (lhs) == SSA_NAME)
3727 : 2416 : lhs = vn_valueize (lhs);
3728 : 2416 : lhs_offset += mem_offset;
3729 : : }
3730 : 6506 : else if (DECL_P (tem))
3731 : 6506 : lhs = build_fold_addr_expr (tem);
3732 : : else
3733 : : return (void *)-1;
3734 : : }
3735 : 43033 : if (TREE_CODE (lhs) != SSA_NAME
3736 : 6506 : && TREE_CODE (lhs) != ADDR_EXPR)
3737 : : return (void *)-1;
3738 : :
3739 : : /* Extract a pointer base and an offset for the source. */
3740 : 43033 : rhs = gimple_call_arg (def_stmt, 1);
3741 : 43033 : rhs_offset = 0;
3742 : 43033 : if (TREE_CODE (rhs) == SSA_NAME)
3743 : 12170 : rhs = vn_valueize (rhs);
3744 : 43033 : if (TREE_CODE (rhs) == ADDR_EXPR)
3745 : : {
3746 : 41826 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3747 : 32061 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3748 : : return (void *)-1;
3749 : 31600 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3750 : : &rhs_offset);
3751 : 31600 : if (!tem)
3752 : : return (void *)-1;
3753 : 31600 : if (TREE_CODE (tem) == MEM_REF
3754 : 31600 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3755 : : {
3756 : 0 : rhs = TREE_OPERAND (tem, 0);
3757 : 0 : rhs_offset += mem_offset;
3758 : : }
3759 : 31600 : else if (DECL_P (tem)
3760 : 28279 : || TREE_CODE (tem) == STRING_CST)
3761 : 31600 : rhs = build_fold_addr_expr (tem);
3762 : : else
3763 : : return (void *)-1;
3764 : : }
3765 : 43033 : if (TREE_CODE (rhs) == SSA_NAME)
3766 : 11433 : rhs = SSA_VAL (rhs);
3767 : 31600 : else if (TREE_CODE (rhs) != ADDR_EXPR)
3768 : : return (void *)-1;
3769 : :
3770 : : /* The bases of the destination and the references have to agree. */
3771 : 43033 : if (TREE_CODE (base) == MEM_REF)
3772 : : {
3773 : 12331 : if (TREE_OPERAND (base, 0) != lhs
3774 : 12331 : || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
3775 : 9578 : return (void *) -1;
3776 : 8423 : at += mem_offset;
3777 : : }
3778 : 30702 : else if (!DECL_P (base)
3779 : 30216 : || TREE_CODE (lhs) != ADDR_EXPR
3780 : 36372 : || TREE_OPERAND (lhs, 0) != base)
3781 : : return (void *)-1;
3782 : :
3783 : : /* If the access is completely outside of the memcpy destination
3784 : : area there is no aliasing. */
3785 : 8423 : if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
3786 : : return NULL;
3787 : : /* And the access has to be contained within the memcpy destination. */
3788 : 8413 : if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
3789 : : return (void *)-1;
3790 : :
3791 : : /* Save the operands since we need to use the original ones for
3792 : : the hash entry we use. */
3793 : 8197 : if (!data->saved_operands.exists ())
3794 : 7853 : data->saved_operands = vr->operands.copy ();
3795 : :
3796 : : /* Make room for 2 operands in the new reference. */
3797 : 8197 : if (vr->operands.length () < 2)
3798 : : {
3799 : 0 : vec<vn_reference_op_s> old = vr->operands;
3800 : 0 : vr->operands.safe_grow_cleared (2, true);
3801 : 0 : if (old == shared_lookup_references)
3802 : 0 : shared_lookup_references = vr->operands;
3803 : : }
3804 : : else
3805 : 8197 : vr->operands.truncate (2);
3806 : :
3807 : : /* The looked-through reference is a simple MEM_REF. */
3808 : 8197 : memset (&op, 0, sizeof (op));
3809 : 8197 : op.type = vr->type;
3810 : 8197 : op.opcode = MEM_REF;
3811 : 8197 : op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3812 : 8197 : op.off = at - lhs_offset + rhs_offset;
3813 : 8197 : vr->operands[0] = op;
3814 : 8197 : op.type = TREE_TYPE (rhs);
3815 : 8197 : op.opcode = TREE_CODE (rhs);
3816 : 8197 : op.op0 = rhs;
3817 : 8197 : op.off = -1;
3818 : 8197 : vr->operands[1] = op;
3819 : 8197 : vr->hashcode = vn_reference_compute_hash (vr);
3820 : :
3821 : : /* Try folding the new reference to a constant. */
3822 : 8197 : tree val = fully_constant_vn_reference_p (vr);
3823 : 8197 : if (val)
3824 : 1877 : return data->finish (0, 0, val);
3825 : :
3826 : : /* Adjust *ref from the new operands. */
3827 : 6320 : if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
3828 : : return (void *)-1;
3829 : : /* This can happen with bitfields. */
3830 : 6320 : if (maybe_ne (ref->size, r.size))
3831 : : return (void *)-1;
3832 : 6320 : *ref = r;
3833 : 6320 : vr->offset = r.offset;
3834 : 6320 : vr->max_size = r.max_size;
3835 : :
3836 : : /* Do not update last seen VUSE after translating. */
3837 : 6320 : data->last_vuse_ptr = NULL;
3838 : : /* Invalidate the original access path since it now contains
3839 : : the wrong base. */
3840 : 6320 : data->orig_ref.ref = NULL_TREE;
3841 : : /* Use the alias-set of this stmt for recording an eventual result. */
3842 : 6320 : if (data->first_set == -2)
3843 : : {
3844 : 6015 : data->first_set = 0;
3845 : 6015 : data->first_base_set = 0;
3846 : : }
3847 : :
3848 : : /* Keep looking for the adjusted *REF / VR pair. */
3849 : 6320 : return NULL;
3850 : : }
3851 : :
3852 : : /* Bail out and stop walking. */
3853 : : return (void *)-1;
3854 : : }
3855 : :
3856 : : /* Return a reference op vector from OP that can be used for
3857 : : vn_reference_lookup_pieces. The caller is responsible for releasing
3858 : : the vector. */
3859 : :
3860 : : vec<vn_reference_op_s>
3861 : 4755897 : vn_reference_operands_for_lookup (tree op)
3862 : : {
3863 : 4755897 : bool valueized;
3864 : 4755897 : return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
3865 : : }
3866 : :
3867 : : /* Lookup a reference operation by it's parts, in the current hash table.
3868 : : Returns the resulting value number if it exists in the hash table,
3869 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
3870 : : vn_reference_t stored in the hashtable if something is found. */
3871 : :
3872 : : tree
3873 : 7640893 : vn_reference_lookup_pieces (tree vuse, alias_set_type set,
3874 : : alias_set_type base_set, tree type,
3875 : : vec<vn_reference_op_s> operands,
3876 : : vn_reference_t *vnresult, vn_lookup_kind kind)
3877 : : {
3878 : 7640893 : struct vn_reference_s vr1;
3879 : 7640893 : vn_reference_t tmp;
3880 : 7640893 : tree cst;
3881 : :
3882 : 7640893 : if (!vnresult)
3883 : 0 : vnresult = &tmp;
3884 : 7640893 : *vnresult = NULL;
3885 : :
3886 : 7640893 : vr1.vuse = vuse_ssa_val (vuse);
3887 : 7640893 : shared_lookup_references.truncate (0);
3888 : 15281786 : shared_lookup_references.safe_grow (operands.length (), true);
3889 : 7640893 : memcpy (shared_lookup_references.address (),
3890 : 7640893 : operands.address (),
3891 : : sizeof (vn_reference_op_s)
3892 : 7640893 : * operands.length ());
3893 : 7640893 : bool valueized_p;
3894 : 7640893 : valueize_refs_1 (&shared_lookup_references, &valueized_p);
3895 : 7640893 : vr1.operands = shared_lookup_references;
3896 : 7640893 : vr1.type = type;
3897 : 7640893 : vr1.set = set;
3898 : 7640893 : vr1.base_set = base_set;
3899 : : /* We can pretend there's no extra info fed in since the ao_refs offset
3900 : : and max_size are computed only from the VN reference ops. */
3901 : 7640893 : vr1.offset = 0;
3902 : 7640893 : vr1.max_size = -1;
3903 : 7640893 : vr1.hashcode = vn_reference_compute_hash (&vr1);
3904 : 7640893 : if ((cst = fully_constant_vn_reference_p (&vr1)))
3905 : : return cst;
3906 : :
3907 : 7623160 : vn_reference_lookup_1 (&vr1, vnresult);
3908 : 7623160 : if (!*vnresult
3909 : 3133437 : && kind != VN_NOWALK
3910 : 3133437 : && vr1.vuse)
3911 : : {
3912 : 2857168 : ao_ref r;
3913 : 2857168 : unsigned limit = param_sccvn_max_alias_queries_per_access;
3914 : 2857168 : vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
3915 : 2857168 : false);
3916 : 2857168 : vec<vn_reference_op_s> ops_for_ref;
3917 : 2857168 : if (!valueized_p)
3918 : 2794091 : ops_for_ref = vr1.operands;
3919 : : else
3920 : : {
3921 : : /* For ao_ref_from_mem we have to ensure only available SSA names
3922 : : end up in base and the only convenient way to make this work
3923 : : for PRE is to re-valueize with that in mind. */
3924 : 126154 : ops_for_ref.create (operands.length ());
3925 : 126154 : ops_for_ref.quick_grow (operands.length ());
3926 : 63077 : memcpy (ops_for_ref.address (),
3927 : 63077 : operands.address (),
3928 : : sizeof (vn_reference_op_s)
3929 : 63077 : * operands.length ());
3930 : 63077 : valueize_refs_1 (&ops_for_ref, &valueized_p, true);
3931 : : }
3932 : 2857168 : if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
3933 : : ops_for_ref))
3934 : 2785929 : *vnresult
3935 : 2785929 : = ((vn_reference_t)
3936 : 2785929 : walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
3937 : : vn_reference_lookup_3, vuse_valueize,
3938 : : limit, &data));
3939 : 5714336 : if (ops_for_ref != shared_lookup_references)
3940 : 63077 : ops_for_ref.release ();
3941 : 5714336 : gcc_checking_assert (vr1.operands == shared_lookup_references);
3942 : 2857168 : if (*vnresult
3943 : 370180 : && data.same_val
3944 : 2857168 : && (!(*vnresult)->result
3945 : 0 : || !operand_equal_p ((*vnresult)->result, data.same_val)))
3946 : : {
3947 : 0 : *vnresult = NULL;
3948 : 0 : return NULL_TREE;
3949 : : }
3950 : 2857168 : }
3951 : :
3952 : 7623160 : if (*vnresult)
3953 : 4859903 : return (*vnresult)->result;
3954 : :
3955 : : return NULL_TREE;
3956 : : }
3957 : :
3958 : : /* Lookup OP in the current hash table, and return the resulting value
3959 : : number if it exists in the hash table. Return NULL_TREE if it does
3960 : : not exist in the hash table or if the result field of the structure
3961 : : was NULL.. VNRESULT will be filled in with the vn_reference_t
3962 : : stored in the hashtable if one exists. When TBAA_P is false assume
3963 : : we are looking up a store and treat it as having alias-set zero.
3964 : : *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
3965 : : MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
3966 : : load is bitwise anded with MASK and so we are only interested in a subset
3967 : : of the bits and can ignore if the other bits are uninitialized or
3968 : : not initialized with constants. When doing redundant store removal
3969 : : the caller has to set REDUNDANT_STORE_REMOVAL_P. */
3970 : :
3971 : : tree
3972 : 88921931 : vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
3973 : : vn_reference_t *vnresult, bool tbaa_p,
3974 : : tree *last_vuse_ptr, tree mask,
3975 : : bool redundant_store_removal_p)
3976 : : {
3977 : 88921931 : vec<vn_reference_op_s> operands;
3978 : 88921931 : struct vn_reference_s vr1;
3979 : 88921931 : bool valueized_anything;
3980 : :
3981 : 88921931 : if (vnresult)
3982 : 88631278 : *vnresult = NULL;
3983 : :
3984 : 88921931 : vr1.vuse = vuse_ssa_val (vuse);
3985 : 177843862 : vr1.operands = operands
3986 : 88921931 : = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
3987 : :
3988 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
3989 : : this before the pass folding __builtin_object_size had a chance to run. */
3990 : 88921931 : if ((cfun->curr_properties & PROP_objsz)
3991 : 64944529 : && operands[0].opcode == ADDR_EXPR
3992 : 90013582 : && operands.last ().opcode == SSA_NAME)
3993 : : {
3994 : : poly_int64 off = 0;
3995 : : vn_reference_op_t vro;
3996 : : unsigned i;
3997 : 3441558 : for (i = 1; operands.iterate (i, &vro); ++i)
3998 : : {
3999 : 3441558 : if (vro->opcode == SSA_NAME)
4000 : : break;
4001 : 2403028 : else if (known_eq (vro->off, -1))
4002 : : break;
4003 : 2380918 : off += vro->off;
4004 : : }
4005 : 1060640 : if (i == operands.length () - 1
4006 : : /* Make sure we the offset we accumulated in a 64bit int
4007 : : fits the address computation carried out in target
4008 : : offset precision. */
4009 : 2099170 : && (off.coeffs[0]
4010 : 1038530 : == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4011 : : {
4012 : 1038134 : gcc_assert (operands[i-1].opcode == MEM_REF);
4013 : 1038134 : tree ops[2];
4014 : 1038134 : ops[0] = operands[i].op0;
4015 : 1038134 : ops[1] = wide_int_to_tree (sizetype, off);
4016 : 1038134 : tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4017 : 1038134 : TREE_TYPE (op), ops, NULL);
4018 : 1038134 : if (res)
4019 : : return res;
4020 : 1038134 : return NULL_TREE;
4021 : : }
4022 : : }
4023 : :
4024 : 87883797 : vr1.type = TREE_TYPE (op);
4025 : 87883797 : ao_ref op_ref;
4026 : 87883797 : ao_ref_init (&op_ref, op);
4027 : 87883797 : vr1.set = ao_ref_alias_set (&op_ref);
4028 : 87883797 : vr1.base_set = ao_ref_base_alias_set (&op_ref);
4029 : 87883797 : vr1.offset = 0;
4030 : 87883797 : vr1.max_size = -1;
4031 : 87883797 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4032 : 87883797 : if (mask == NULL_TREE)
4033 : 87593144 : if (tree cst = fully_constant_vn_reference_p (&vr1))
4034 : : return cst;
4035 : :
4036 : 87871208 : if (kind != VN_NOWALK && vr1.vuse)
4037 : : {
4038 : 50854480 : vn_reference_t wvnresult;
4039 : 50854480 : ao_ref r;
4040 : 50854480 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4041 : 50854480 : auto_vec<vn_reference_op_s> ops_for_ref;
4042 : 50854480 : if (valueized_anything)
4043 : : {
4044 : 3713464 : copy_reference_ops_from_ref (op, &ops_for_ref);
4045 : 3713464 : bool tem;
4046 : 3713464 : valueize_refs_1 (&ops_for_ref, &tem, true);
4047 : : }
4048 : : /* Make sure to use a valueized reference if we valueized anything.
4049 : : Otherwise preserve the full reference for advanced TBAA. */
4050 : 50854480 : if (!valueized_anything
4051 : 50854480 : || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4052 : : vr1.type, ops_for_ref))
4053 : : {
4054 : 47348528 : ao_ref_init (&r, op);
4055 : : /* Record the extra info we're getting from the full ref. */
4056 : 47348528 : ao_ref_base (&r);
4057 : 47348528 : vr1.offset = r.offset;
4058 : 47348528 : vr1.max_size = r.max_size;
4059 : : }
4060 : 50854480 : vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4061 : : last_vuse_ptr, kind, tbaa_p, mask,
4062 : 98203008 : redundant_store_removal_p);
4063 : :
4064 : 50854480 : wvnresult
4065 : : = ((vn_reference_t)
4066 : 50854480 : walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4067 : : vn_reference_lookup_3, vuse_valueize, limit,
4068 : : &data));
4069 : 101708960 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4070 : 50854480 : if (wvnresult)
4071 : : {
4072 : 6684075 : gcc_assert (mask == NULL_TREE);
4073 : 6684075 : if (data.same_val
4074 : 6684075 : && (!wvnresult->result
4075 : 46990 : || !operand_equal_p (wvnresult->result, data.same_val)))
4076 : 39203 : return NULL_TREE;
4077 : 6644872 : if (vnresult)
4078 : 6644872 : *vnresult = wvnresult;
4079 : 6644872 : return wvnresult->result;
4080 : : }
4081 : 44170405 : else if (mask)
4082 : 290653 : return data.masked_result;
4083 : :
4084 : : return NULL_TREE;
4085 : 50854480 : }
4086 : :
4087 : 37016728 : if (last_vuse_ptr)
4088 : 972134 : *last_vuse_ptr = vr1.vuse;
4089 : 37016728 : if (mask)
4090 : : return NULL_TREE;
4091 : 37016728 : return vn_reference_lookup_1 (&vr1, vnresult);
4092 : : }
4093 : :
4094 : : /* Lookup CALL in the current hash table and return the entry in
4095 : : *VNRESULT if found. Populates *VR for the hashtable lookup. */
4096 : :
4097 : : void
4098 : 8292122 : vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4099 : : vn_reference_t vr)
4100 : : {
4101 : 8292122 : if (vnresult)
4102 : 8292122 : *vnresult = NULL;
4103 : :
4104 : 8292122 : tree vuse = gimple_vuse (call);
4105 : :
4106 : 8292122 : vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4107 : 8292122 : vr->operands = valueize_shared_reference_ops_from_call (call);
4108 : 8292122 : tree lhs = gimple_call_lhs (call);
4109 : : /* For non-SSA return values the referece ops contain the LHS. */
4110 : 4720091 : vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4111 : 12566206 : ? TREE_TYPE (lhs) : NULL_TREE);
4112 : 8292122 : vr->punned = false;
4113 : 8292122 : vr->set = 0;
4114 : 8292122 : vr->base_set = 0;
4115 : 8292122 : vr->offset = 0;
4116 : 8292122 : vr->max_size = -1;
4117 : 8292122 : vr->hashcode = vn_reference_compute_hash (vr);
4118 : 8292122 : vn_reference_lookup_1 (vr, vnresult);
4119 : 8292122 : }
4120 : :
4121 : : /* Insert OP into the current hash table with a value number of RESULT. */
4122 : :
4123 : : static void
4124 : 67302071 : vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4125 : : {
4126 : 67302071 : vn_reference_s **slot;
4127 : 67302071 : vn_reference_t vr1;
4128 : 67302071 : bool tem;
4129 : :
4130 : 67302071 : vec<vn_reference_op_s> operands
4131 : 67302071 : = valueize_shared_reference_ops_from_ref (op, &tem);
4132 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4133 : : before the pass folding __builtin_object_size had a chance to run. */
4134 : 67302071 : if ((cfun->curr_properties & PROP_objsz)
4135 : 50752424 : && operands[0].opcode == ADDR_EXPR
4136 : 68209141 : && operands.last ().opcode == SSA_NAME)
4137 : : {
4138 : : poly_int64 off = 0;
4139 : : vn_reference_op_t vro;
4140 : : unsigned i;
4141 : 2846482 : for (i = 1; operands.iterate (i, &vro); ++i)
4142 : : {
4143 : 2846482 : if (vro->opcode == SSA_NAME)
4144 : : break;
4145 : 1988737 : else if (known_eq (vro->off, -1))
4146 : : break;
4147 : 1968347 : off += vro->off;
4148 : : }
4149 : 878135 : if (i == operands.length () - 1
4150 : : /* Make sure we the offset we accumulated in a 64bit int
4151 : : fits the address computation carried out in target
4152 : : offset precision. */
4153 : 1735880 : && (off.coeffs[0]
4154 : 857745 : == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4155 : : {
4156 : 857419 : gcc_assert (operands[i-1].opcode == MEM_REF);
4157 : 857419 : tree ops[2];
4158 : 857419 : ops[0] = operands[i].op0;
4159 : 857419 : ops[1] = wide_int_to_tree (sizetype, off);
4160 : 857419 : vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4161 : 857419 : TREE_TYPE (op), ops, result,
4162 : 857419 : VN_INFO (result)->value_id);
4163 : 857419 : return;
4164 : : }
4165 : : }
4166 : :
4167 : 66444652 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4168 : 66444652 : if (TREE_CODE (result) == SSA_NAME)
4169 : 45988203 : vr1->value_id = VN_INFO (result)->value_id;
4170 : : else
4171 : 20456449 : vr1->value_id = get_or_alloc_constant_value_id (result);
4172 : 66444652 : vr1->vuse = vuse_ssa_val (vuse);
4173 : 66444652 : vr1->operands = operands.copy ();
4174 : 66444652 : vr1->type = TREE_TYPE (op);
4175 : 66444652 : vr1->punned = false;
4176 : 66444652 : ao_ref op_ref;
4177 : 66444652 : ao_ref_init (&op_ref, op);
4178 : 66444652 : vr1->set = ao_ref_alias_set (&op_ref);
4179 : 66444652 : vr1->base_set = ao_ref_base_alias_set (&op_ref);
4180 : : /* Specifically use an unknown extent here, we're not doing any lookup
4181 : : and assume the caller didn't either (or it went VARYING). */
4182 : 66444652 : vr1->offset = 0;
4183 : 66444652 : vr1->max_size = -1;
4184 : 66444652 : vr1->hashcode = vn_reference_compute_hash (vr1);
4185 : 66444652 : vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4186 : 66444652 : vr1->result_vdef = vdef;
4187 : :
4188 : 66444652 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4189 : : INSERT);
4190 : :
4191 : : /* Because IL walking on reference lookup can end up visiting
4192 : : a def that is only to be visited later in iteration order
4193 : : when we are about to make an irreducible region reducible
4194 : : the def can be effectively processed and its ref being inserted
4195 : : by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4196 : : but save a lookup if we deal with already inserted refs here. */
4197 : 66444652 : if (*slot)
4198 : : {
4199 : : /* We cannot assert that we have the same value either because
4200 : : when disentangling an irreducible region we may end up visiting
4201 : : a use before the corresponding def. That's a missed optimization
4202 : : only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4203 : 0 : if (dump_file && (dump_flags & TDF_DETAILS)
4204 : 0 : && !operand_equal_p ((*slot)->result, vr1->result, 0))
4205 : : {
4206 : 0 : fprintf (dump_file, "Keeping old value ");
4207 : 0 : print_generic_expr (dump_file, (*slot)->result);
4208 : 0 : fprintf (dump_file, " because of collision\n");
4209 : : }
4210 : 0 : free_reference (vr1);
4211 : 0 : obstack_free (&vn_tables_obstack, vr1);
4212 : 0 : return;
4213 : : }
4214 : :
4215 : 66444652 : *slot = vr1;
4216 : 66444652 : vr1->next = last_inserted_ref;
4217 : 66444652 : last_inserted_ref = vr1;
4218 : : }
4219 : :
4220 : : /* Insert a reference by it's pieces into the current hash table with
4221 : : a value number of RESULT. Return the resulting reference
4222 : : structure we created. */
4223 : :
4224 : : vn_reference_t
4225 : 3275151 : vn_reference_insert_pieces (tree vuse, alias_set_type set,
4226 : : alias_set_type base_set,
4227 : : poly_int64 offset, poly_int64 max_size, tree type,
4228 : : vec<vn_reference_op_s> operands,
4229 : : tree result, unsigned int value_id)
4230 : :
4231 : : {
4232 : 3275151 : vn_reference_s **slot;
4233 : 3275151 : vn_reference_t vr1;
4234 : :
4235 : 3275151 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4236 : 3275151 : vr1->value_id = value_id;
4237 : 3275151 : vr1->vuse = vuse_ssa_val (vuse);
4238 : 3275151 : vr1->operands = operands;
4239 : 3275151 : valueize_refs (&vr1->operands);
4240 : 3275151 : vr1->type = type;
4241 : 3275151 : vr1->punned = false;
4242 : 3275151 : vr1->set = set;
4243 : 3275151 : vr1->base_set = base_set;
4244 : 3275151 : vr1->offset = offset;
4245 : 3275151 : vr1->max_size = max_size;
4246 : 3275151 : vr1->hashcode = vn_reference_compute_hash (vr1);
4247 : 3275151 : if (result && TREE_CODE (result) == SSA_NAME)
4248 : 235722 : result = SSA_VAL (result);
4249 : 3275151 : vr1->result = result;
4250 : 3275151 : vr1->result_vdef = NULL_TREE;
4251 : :
4252 : 3275151 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4253 : : INSERT);
4254 : :
4255 : : /* At this point we should have all the things inserted that we have
4256 : : seen before, and we should never try inserting something that
4257 : : already exists. */
4258 : 3275151 : gcc_assert (!*slot);
4259 : :
4260 : 3275151 : *slot = vr1;
4261 : 3275151 : vr1->next = last_inserted_ref;
4262 : 3275151 : last_inserted_ref = vr1;
4263 : 3275151 : return vr1;
4264 : : }
4265 : :
4266 : : /* Compute and return the hash value for nary operation VBO1. */
4267 : :
4268 : : hashval_t
4269 : 258507908 : vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4270 : : {
4271 : 258507908 : inchash::hash hstate;
4272 : 258507908 : unsigned i;
4273 : :
4274 : 258507908 : if (((vno1->length == 2
4275 : 215936607 : && commutative_tree_code (vno1->opcode))
4276 : 117579341 : || (vno1->length == 3
4277 : 1112122 : && commutative_ternary_tree_code (vno1->opcode)))
4278 : 399437331 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4279 : 9916581 : std::swap (vno1->op[0], vno1->op[1]);
4280 : 248591327 : else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4281 : 248591327 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4282 : : {
4283 : 2559334 : std::swap (vno1->op[0], vno1->op[1]);
4284 : 2559334 : vno1->opcode = swap_tree_comparison (vno1->opcode);
4285 : : }
4286 : :
4287 : 258507908 : hstate.add_int (vno1->opcode);
4288 : 735767304 : for (i = 0; i < vno1->length; ++i)
4289 : 477259396 : inchash::add_expr (vno1->op[i], hstate);
4290 : :
4291 : 258507908 : return hstate.end ();
4292 : : }
4293 : :
4294 : : /* Compare nary operations VNO1 and VNO2 and return true if they are
4295 : : equivalent. */
4296 : :
4297 : : bool
4298 : 811744920 : vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4299 : : {
4300 : 811744920 : unsigned i;
4301 : :
4302 : 811744920 : if (vno1->hashcode != vno2->hashcode)
4303 : : return false;
4304 : :
4305 : 41510994 : if (vno1->length != vno2->length)
4306 : : return false;
4307 : :
4308 : 41510994 : if (vno1->opcode != vno2->opcode
4309 : 41510994 : || !types_compatible_p (vno1->type, vno2->type))
4310 : 982545 : return false;
4311 : :
4312 : 116667111 : for (i = 0; i < vno1->length; ++i)
4313 : 76400689 : if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4314 : : return false;
4315 : :
4316 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
4317 : : of op1. Need to check to make sure they are the same. */
4318 : 40266422 : if (vno1->opcode == BIT_INSERT_EXPR
4319 : 456 : && TREE_CODE (vno1->op[1]) == INTEGER_CST
4320 : 40266523 : && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4321 : 101 : != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4322 : : return false;
4323 : :
4324 : : return true;
4325 : : }
4326 : :
4327 : : /* Initialize VNO from the pieces provided. */
4328 : :
4329 : : static void
4330 : 158038806 : init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4331 : : enum tree_code code, tree type, tree *ops)
4332 : : {
4333 : 158038806 : vno->opcode = code;
4334 : 158038806 : vno->length = length;
4335 : 158038806 : vno->type = type;
4336 : 4052828 : memcpy (&vno->op[0], ops, sizeof (tree) * length);
4337 : 0 : }
4338 : :
4339 : : /* Return the number of operands for a vn_nary ops structure from STMT. */
4340 : :
4341 : : unsigned int
4342 : 95830439 : vn_nary_length_from_stmt (gimple *stmt)
4343 : : {
4344 : 95830439 : switch (gimple_assign_rhs_code (stmt))
4345 : : {
4346 : : case REALPART_EXPR:
4347 : : case IMAGPART_EXPR:
4348 : : case VIEW_CONVERT_EXPR:
4349 : : return 1;
4350 : :
4351 : 505425 : case BIT_FIELD_REF:
4352 : 505425 : return 3;
4353 : :
4354 : 508449 : case CONSTRUCTOR:
4355 : 508449 : return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4356 : :
4357 : 91584411 : default:
4358 : 91584411 : return gimple_num_ops (stmt) - 1;
4359 : : }
4360 : : }
4361 : :
4362 : : /* Initialize VNO from STMT. */
4363 : :
4364 : : void
4365 : 95830439 : init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4366 : : {
4367 : 95830439 : unsigned i;
4368 : :
4369 : 95830439 : vno->opcode = gimple_assign_rhs_code (stmt);
4370 : 95830439 : vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4371 : 95830439 : switch (vno->opcode)
4372 : : {
4373 : 3232154 : case REALPART_EXPR:
4374 : 3232154 : case IMAGPART_EXPR:
4375 : 3232154 : case VIEW_CONVERT_EXPR:
4376 : 3232154 : vno->length = 1;
4377 : 3232154 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4378 : 3232154 : break;
4379 : :
4380 : 505425 : case BIT_FIELD_REF:
4381 : 505425 : vno->length = 3;
4382 : 505425 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4383 : 505425 : vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4384 : 505425 : vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4385 : 505425 : break;
4386 : :
4387 : 508449 : case CONSTRUCTOR:
4388 : 508449 : vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4389 : 1921120 : for (i = 0; i < vno->length; ++i)
4390 : 1412671 : vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4391 : : break;
4392 : :
4393 : 91584411 : default:
4394 : 91584411 : gcc_checking_assert (!gimple_assign_single_p (stmt));
4395 : 91584411 : vno->length = gimple_num_ops (stmt) - 1;
4396 : 249555471 : for (i = 0; i < vno->length; ++i)
4397 : 157971060 : vno->op[i] = gimple_op (stmt, i + 1);
4398 : : }
4399 : 95830439 : }
4400 : :
4401 : : /* Compute the hashcode for VNO and look for it in the hash table;
4402 : : return the resulting value number if it exists in the hash table.
4403 : : Return NULL_TREE if it does not exist in the hash table or if the
4404 : : result field of the operation is NULL. VNRESULT will contain the
4405 : : vn_nary_op_t from the hashtable if it exists. */
4406 : :
4407 : : static tree
4408 : 113074398 : vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4409 : : {
4410 : 113074398 : vn_nary_op_s **slot;
4411 : :
4412 : 113074398 : if (vnresult)
4413 : 106685644 : *vnresult = NULL;
4414 : :
4415 : 313483964 : for (unsigned i = 0; i < vno->length; ++i)
4416 : 200409566 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4417 : 142654589 : vno->op[i] = SSA_VAL (vno->op[i]);
4418 : :
4419 : 113074398 : vno->hashcode = vn_nary_op_compute_hash (vno);
4420 : 113074398 : slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4421 : 113074398 : if (!slot)
4422 : : return NULL_TREE;
4423 : 14264134 : if (vnresult)
4424 : 13825458 : *vnresult = *slot;
4425 : 14264134 : return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4426 : : }
4427 : :
4428 : : /* Lookup a n-ary operation by its pieces and return the resulting value
4429 : : number if it exists in the hash table. Return NULL_TREE if it does
4430 : : not exist in the hash table or if the result field of the operation
4431 : : is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4432 : : if it exists. */
4433 : :
4434 : : tree
4435 : 63524832 : vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4436 : : tree type, tree *ops, vn_nary_op_t *vnresult)
4437 : : {
4438 : 63524832 : vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4439 : : sizeof_vn_nary_op (length));
4440 : 63524832 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4441 : 63524832 : return vn_nary_op_lookup_1 (vno1, vnresult);
4442 : : }
4443 : :
4444 : : /* Lookup the rhs of STMT in the current hash table, and return the resulting
4445 : : value number if it exists in the hash table. Return NULL_TREE if
4446 : : it does not exist in the hash table. VNRESULT will contain the
4447 : : vn_nary_op_t from the hashtable if it exists. */
4448 : :
4449 : : tree
4450 : 49549566 : vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4451 : : {
4452 : 49549566 : vn_nary_op_t vno1
4453 : 49549566 : = XALLOCAVAR (struct vn_nary_op_s,
4454 : : sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4455 : 49549566 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4456 : 49549566 : return vn_nary_op_lookup_1 (vno1, vnresult);
4457 : : }
4458 : :
4459 : : /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4460 : :
4461 : : vn_nary_op_t
4462 : 144945563 : alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4463 : : {
4464 : 144945563 : return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4465 : : }
4466 : :
4467 : : /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4468 : : obstack. */
4469 : :
4470 : : static vn_nary_op_t
4471 : 129890255 : alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4472 : : {
4473 : 0 : vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4474 : :
4475 : 129890255 : vno1->value_id = value_id;
4476 : 129890255 : vno1->length = length;
4477 : 129890255 : vno1->predicated_values = 0;
4478 : 129890255 : vno1->u.result = result;
4479 : :
4480 : 129890255 : return vno1;
4481 : : }
4482 : :
4483 : : /* Insert VNO into TABLE. */
4484 : :
4485 : : static vn_nary_op_t
4486 : 134095110 : vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4487 : : {
4488 : 134095110 : vn_nary_op_s **slot;
4489 : :
4490 : 134095110 : gcc_assert (! vno->predicated_values
4491 : : || (! vno->u.values->next
4492 : : && vno->u.values->n == 1));
4493 : :
4494 : 391363749 : for (unsigned i = 0; i < vno->length; ++i)
4495 : 257268639 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4496 : 168011899 : vno->op[i] = SSA_VAL (vno->op[i]);
4497 : :
4498 : 134095110 : vno->hashcode = vn_nary_op_compute_hash (vno);
4499 : 134095110 : slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4500 : 134095110 : vno->unwind_to = *slot;
4501 : 134095110 : if (*slot)
4502 : : {
4503 : : /* Prefer non-predicated values.
4504 : : ??? Only if those are constant, otherwise, with constant predicated
4505 : : value, turn them into predicated values with entry-block validity
4506 : : (??? but we always find the first valid result currently). */
4507 : 25514341 : if ((*slot)->predicated_values
4508 : 25101427 : && ! vno->predicated_values)
4509 : : {
4510 : : /* ??? We cannot remove *slot from the unwind stack list.
4511 : : For the moment we deal with this by skipping not found
4512 : : entries but this isn't ideal ... */
4513 : 68511 : *slot = vno;
4514 : : /* ??? Maintain a stack of states we can unwind in
4515 : : vn_nary_op_s? But how far do we unwind? In reality
4516 : : we need to push change records somewhere... Or not
4517 : : unwind vn_nary_op_s and linking them but instead
4518 : : unwind the results "list", linking that, which also
4519 : : doesn't move on hashtable resize. */
4520 : : /* We can also have a ->unwind_to recording *slot there.
4521 : : That way we can make u.values a fixed size array with
4522 : : recording the number of entries but of course we then
4523 : : have always N copies for each unwind_to-state. Or we
4524 : : make sure to only ever append and each unwinding will
4525 : : pop off one entry (but how to deal with predicated
4526 : : replaced with non-predicated here?) */
4527 : 68511 : vno->next = last_inserted_nary;
4528 : 68511 : last_inserted_nary = vno;
4529 : 68511 : return vno;
4530 : : }
4531 : 25445830 : else if (vno->predicated_values
4532 : 25441836 : && ! (*slot)->predicated_values)
4533 : : return *slot;
4534 : 25036910 : else if (vno->predicated_values
4535 : 25032916 : && (*slot)->predicated_values)
4536 : : {
4537 : : /* ??? Factor this all into a insert_single_predicated_value
4538 : : routine. */
4539 : 25032916 : gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4540 : 25032916 : basic_block vno_bb
4541 : 25032916 : = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4542 : 25032916 : vn_pval *nval = vno->u.values;
4543 : 25032916 : vn_pval **next = &vno->u.values;
4544 : 25032916 : bool found = false;
4545 : 51482610 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4546 : : {
4547 : 26631606 : if (expressions_equal_p (val->result, nval->result))
4548 : : {
4549 : 10171695 : found = true;
4550 : 10171695 : for (unsigned i = 0; i < val->n; ++i)
4551 : : {
4552 : 7040915 : basic_block val_bb
4553 : 7040915 : = BASIC_BLOCK_FOR_FN (cfun,
4554 : : val->valid_dominated_by_p[i]);
4555 : 7040915 : if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4556 : : /* Value registered with more generic predicate. */
4557 : 181912 : return *slot;
4558 : 6859003 : else if (flag_checking)
4559 : : /* Shouldn't happen, we insert in RPO order. */
4560 : 6859003 : gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4561 : : val_bb, vno_bb));
4562 : : }
4563 : : /* Append value. */
4564 : 3130780 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4565 : : sizeof (vn_pval)
4566 : : + val->n * sizeof (int));
4567 : 3130780 : (*next)->next = NULL;
4568 : 3130780 : (*next)->result = val->result;
4569 : 3130780 : (*next)->n = val->n + 1;
4570 : 3130780 : memcpy ((*next)->valid_dominated_by_p,
4571 : 3130780 : val->valid_dominated_by_p,
4572 : 3130780 : val->n * sizeof (int));
4573 : 3130780 : (*next)->valid_dominated_by_p[val->n] = vno_bb->index;
4574 : 3130780 : next = &(*next)->next;
4575 : 3130780 : if (dump_file && (dump_flags & TDF_DETAILS))
4576 : 4 : fprintf (dump_file, "Appending predicate to value.\n");
4577 : 3130780 : continue;
4578 : 3130780 : }
4579 : : /* Copy other predicated values. */
4580 : 23318914 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4581 : : sizeof (vn_pval)
4582 : : + (val->n-1) * sizeof (int));
4583 : 23318914 : memcpy (*next, val, sizeof (vn_pval) + (val->n-1) * sizeof (int));
4584 : 23318914 : (*next)->next = NULL;
4585 : 23318914 : next = &(*next)->next;
4586 : : }
4587 : 24851004 : if (!found)
4588 : 21720224 : *next = nval;
4589 : :
4590 : 24851004 : *slot = vno;
4591 : 24851004 : vno->next = last_inserted_nary;
4592 : 24851004 : last_inserted_nary = vno;
4593 : 24851004 : return vno;
4594 : : }
4595 : :
4596 : : /* While we do not want to insert things twice it's awkward to
4597 : : avoid it in the case where visit_nary_op pattern-matches stuff
4598 : : and ends up simplifying the replacement to itself. We then
4599 : : get two inserts, one from visit_nary_op and one from
4600 : : vn_nary_build_or_lookup.
4601 : : So allow inserts with the same value number. */
4602 : 3994 : if ((*slot)->u.result == vno->u.result)
4603 : : return *slot;
4604 : : }
4605 : :
4606 : : /* ??? There's also optimistic vs. previous commited state merging
4607 : : that is problematic for the case of unwinding. */
4608 : :
4609 : : /* ??? We should return NULL if we do not use 'vno' and have the
4610 : : caller release it. */
4611 : 108580769 : gcc_assert (!*slot);
4612 : :
4613 : 108580769 : *slot = vno;
4614 : 108580769 : vno->next = last_inserted_nary;
4615 : 108580769 : last_inserted_nary = vno;
4616 : 108580769 : return vno;
4617 : : }
4618 : :
4619 : : /* Insert a n-ary operation into the current hash table using it's
4620 : : pieces. Return the vn_nary_op_t structure we created and put in
4621 : : the hashtable. */
4622 : :
4623 : : vn_nary_op_t
4624 : 857419 : vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4625 : : tree type, tree *ops,
4626 : : tree result, unsigned int value_id)
4627 : : {
4628 : 857419 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4629 : 857419 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4630 : 857419 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4631 : : }
4632 : :
4633 : : /* Return whether we can track a predicate valid when PRED_E is executed. */
4634 : :
4635 : : static bool
4636 : 127394468 : can_track_predicate_on_edge (edge pred_e)
4637 : : {
4638 : : /* ??? As we are currently recording the destination basic-block index in
4639 : : vn_pval.valid_dominated_by_p and using dominance for the
4640 : : validity check we cannot track predicates on all edges. */
4641 : 127394468 : if (single_pred_p (pred_e->dest))
4642 : : return true;
4643 : : /* Never record for backedges. */
4644 : 10110847 : if (pred_e->flags & EDGE_DFS_BACK)
4645 : : return false;
4646 : : /* When there's more than one predecessor we cannot track
4647 : : predicate validity based on the destination block. The
4648 : : exception is when all other incoming edges sources are
4649 : : dominated by the destination block. */
4650 : 9533634 : edge_iterator ei;
4651 : 9533634 : edge e;
4652 : 16793521 : FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4653 : 15091208 : if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4654 : : return false;
4655 : : return true;
4656 : : }
4657 : :
4658 : : static vn_nary_op_t
4659 : 89603727 : vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4660 : : tree type, tree *ops,
4661 : : tree result, unsigned int value_id,
4662 : : edge pred_e)
4663 : : {
4664 : 89603727 : gcc_assert (can_track_predicate_on_edge (pred_e));
4665 : :
4666 : 65116 : if (dump_file && (dump_flags & TDF_DETAILS)
4667 : : /* ??? Fix dumping, but currently we only get comparisons. */
4668 : 89665015 : && TREE_CODE_CLASS (code) == tcc_comparison)
4669 : : {
4670 : 61288 : fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4671 : 61288 : pred_e->dest->index);
4672 : 61288 : print_generic_expr (dump_file, ops[0], TDF_SLIM);
4673 : 61288 : fprintf (dump_file, " %s ", get_tree_code_name (code));
4674 : 61288 : print_generic_expr (dump_file, ops[1], TDF_SLIM);
4675 : 91614 : fprintf (dump_file, " == %s\n",
4676 : 61288 : integer_zerop (result) ? "false" : "true");
4677 : : }
4678 : 89603727 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4679 : 89603727 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4680 : 89603727 : vno1->predicated_values = 1;
4681 : 89603727 : vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4682 : : sizeof (vn_pval));
4683 : 89603727 : vno1->u.values->next = NULL;
4684 : 89603727 : vno1->u.values->result = result;
4685 : 89603727 : vno1->u.values->n = 1;
4686 : 89603727 : vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4687 : 89603727 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4688 : : }
4689 : :
4690 : : static bool
4691 : : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4692 : :
4693 : : static tree
4694 : 1344924 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
4695 : : edge e = NULL)
4696 : : {
4697 : 1344924 : if (! vno->predicated_values)
4698 : 0 : return vno->u.result;
4699 : 2795995 : for (vn_pval *val = vno->u.values; val; val = val->next)
4700 : 4678926 : for (unsigned i = 0; i < val->n; ++i)
4701 : : {
4702 : 3227855 : basic_block cand
4703 : 3227855 : = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
4704 : : /* Do not handle backedge executability optimistically since
4705 : : when figuring out whether to iterate we do not consider
4706 : : changed predication.
4707 : : When asking for predicated values on an edge avoid looking
4708 : : at edge executability for edges forward in our iteration
4709 : : as well. */
4710 : 3227855 : if (e && (e->flags & EDGE_DFS_BACK))
4711 : : {
4712 : 21293 : if (dominated_by_p (CDI_DOMINATORS, bb, cand))
4713 : 10111 : return val->result;
4714 : : }
4715 : 3206562 : else if (dominated_by_p_w_unex (bb, cand, false))
4716 : 389379 : return val->result;
4717 : : }
4718 : : return NULL_TREE;
4719 : : }
4720 : :
4721 : : static tree
4722 : 235891 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
4723 : : {
4724 : 0 : return vn_nary_op_get_predicated_value (vno, e->src, e);
4725 : : }
4726 : :
4727 : : /* Insert the rhs of STMT into the current hash table with a value number of
4728 : : RESULT. */
4729 : :
4730 : : static vn_nary_op_t
4731 : 39429109 : vn_nary_op_insert_stmt (gimple *stmt, tree result)
4732 : : {
4733 : 39429109 : vn_nary_op_t vno1
4734 : 39429109 : = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
4735 : 39429109 : result, VN_INFO (result)->value_id);
4736 : 39429109 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4737 : 39429109 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4738 : : }
4739 : :
4740 : : /* Compute a hashcode for PHI operation VP1 and return it. */
4741 : :
4742 : : static inline hashval_t
4743 : 43811435 : vn_phi_compute_hash (vn_phi_t vp1)
4744 : : {
4745 : 43811435 : inchash::hash hstate;
4746 : 43811435 : tree phi1op;
4747 : 43811435 : tree type;
4748 : 43811435 : edge e;
4749 : 43811435 : edge_iterator ei;
4750 : :
4751 : 87622870 : hstate.add_int (EDGE_COUNT (vp1->block->preds));
4752 : 43811435 : switch (EDGE_COUNT (vp1->block->preds))
4753 : : {
4754 : : case 1:
4755 : : break;
4756 : 38176054 : case 2:
4757 : : /* When this is a PHI node subject to CSE for different blocks
4758 : : avoid hashing the block index. */
4759 : 38176054 : if (vp1->cclhs)
4760 : : break;
4761 : : /* Fallthru. */
4762 : 28380836 : default:
4763 : 28380836 : hstate.add_int (vp1->block->index);
4764 : : }
4765 : :
4766 : : /* If all PHI arguments are constants we need to distinguish
4767 : : the PHI node via its type. */
4768 : 43811435 : type = vp1->type;
4769 : 43811435 : hstate.merge_hash (vn_hash_type (type));
4770 : :
4771 : 151299754 : FOR_EACH_EDGE (e, ei, vp1->block->preds)
4772 : : {
4773 : : /* Don't hash backedge values they need to be handled as VN_TOP
4774 : : for optimistic value-numbering. */
4775 : 107488319 : if (e->flags & EDGE_DFS_BACK)
4776 : 23593314 : continue;
4777 : :
4778 : 83895005 : phi1op = vp1->phiargs[e->dest_idx];
4779 : 83895005 : if (phi1op == VN_TOP)
4780 : 214066 : continue;
4781 : 83680939 : inchash::add_expr (phi1op, hstate);
4782 : : }
4783 : :
4784 : 43811435 : return hstate.end ();
4785 : : }
4786 : :
4787 : :
4788 : : /* Return true if COND1 and COND2 represent the same condition, set
4789 : : *INVERTED_P if one needs to be inverted to make it the same as
4790 : : the other. */
4791 : :
4792 : : static bool
4793 : 2692535 : cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
4794 : : gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
4795 : : {
4796 : 2692535 : enum tree_code code1 = gimple_cond_code (cond1);
4797 : 2692535 : enum tree_code code2 = gimple_cond_code (cond2);
4798 : :
4799 : 2692535 : *inverted_p = false;
4800 : 2692535 : if (code1 == code2)
4801 : : ;
4802 : 211976 : else if (code1 == swap_tree_comparison (code2))
4803 : : std::swap (lhs2, rhs2);
4804 : 181489 : else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
4805 : 54812 : *inverted_p = true;
4806 : 126677 : else if (code1 == invert_tree_comparison
4807 : 126677 : (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
4808 : : {
4809 : 11504 : std::swap (lhs2, rhs2);
4810 : 11504 : *inverted_p = true;
4811 : : }
4812 : : else
4813 : : return false;
4814 : :
4815 : 2577362 : return ((expressions_equal_p (lhs1, lhs2)
4816 : 112315 : && expressions_equal_p (rhs1, rhs2))
4817 : 2606560 : || (commutative_tree_code (code1)
4818 : 1442578 : && expressions_equal_p (lhs1, rhs2)
4819 : 4023 : && expressions_equal_p (rhs1, lhs2)));
4820 : : }
4821 : :
4822 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4823 : :
4824 : : static int
4825 : 34869471 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
4826 : : {
4827 : 34869471 : if (vp1->hashcode != vp2->hashcode)
4828 : : return false;
4829 : :
4830 : 10306207 : if (vp1->block != vp2->block)
4831 : : {
4832 : 8097813 : if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
4833 : : return false;
4834 : :
4835 : 31274085 : switch (EDGE_COUNT (vp1->block->preds))
4836 : : {
4837 : : case 1:
4838 : : /* Single-arg PHIs are just copies. */
4839 : : break;
4840 : :
4841 : 2699271 : case 2:
4842 : 2699271 : {
4843 : : /* Make sure both PHIs are classified as CSEable. */
4844 : 2699271 : if (! vp1->cclhs || ! vp2->cclhs)
4845 : : return false;
4846 : :
4847 : : /* Rule out backedges into the PHI. */
4848 : 2699271 : gcc_checking_assert
4849 : : (vp1->block->loop_father->header != vp1->block
4850 : : && vp2->block->loop_father->header != vp2->block);
4851 : :
4852 : : /* If the PHI nodes do not have compatible types
4853 : : they are not the same. */
4854 : 2699271 : if (!types_compatible_p (vp1->type, vp2->type))
4855 : : return false;
4856 : :
4857 : : /* If the immediate dominator end in switch stmts multiple
4858 : : values may end up in the same PHI arg via intermediate
4859 : : CFG merges. */
4860 : 2692535 : basic_block idom1
4861 : 2692535 : = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4862 : 2692535 : basic_block idom2
4863 : 2692535 : = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
4864 : 2692535 : gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
4865 : : && EDGE_COUNT (idom2->succs) == 2);
4866 : :
4867 : : /* Verify the controlling stmt is the same. */
4868 : 5385070 : gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
4869 : 5385070 : gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
4870 : 2692535 : bool inverted_p;
4871 : 2692535 : if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
4872 : 2692535 : last2, vp2->cclhs, vp2->ccrhs,
4873 : : &inverted_p))
4874 : : return false;
4875 : :
4876 : : /* Get at true/false controlled edges into the PHI. */
4877 : 83399 : edge te1, te2, fe1, fe2;
4878 : 83399 : if (! extract_true_false_controlled_edges (idom1, vp1->block,
4879 : : &te1, &fe1)
4880 : 83399 : || ! extract_true_false_controlled_edges (idom2, vp2->block,
4881 : : &te2, &fe2))
4882 : 37655 : return false;
4883 : :
4884 : : /* Swap edges if the second condition is the inverted of the
4885 : : first. */
4886 : 45744 : if (inverted_p)
4887 : 2220 : std::swap (te2, fe2);
4888 : :
4889 : : /* Since we do not know which edge will be executed we have
4890 : : to be careful when matching VN_TOP. Be conservative and
4891 : : only match VN_TOP == VN_TOP for now, we could allow
4892 : : VN_TOP on the not prevailing PHI though. See for example
4893 : : PR102920. */
4894 : 45744 : if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
4895 : 45744 : vp2->phiargs[te2->dest_idx], false)
4896 : 89541 : || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
4897 : 43797 : vp2->phiargs[fe2->dest_idx], false))
4898 : 1947 : return false;
4899 : :
4900 : : return true;
4901 : : }
4902 : :
4903 : : default:
4904 : : return false;
4905 : : }
4906 : : }
4907 : :
4908 : : /* If the PHI nodes do not have compatible types
4909 : : they are not the same. */
4910 : 7606936 : if (!types_compatible_p (vp1->type, vp2->type))
4911 : : return false;
4912 : :
4913 : : /* Any phi in the same block will have it's arguments in the
4914 : : same edge order, because of how we store phi nodes. */
4915 : 7606232 : unsigned nargs = EDGE_COUNT (vp1->block->preds);
4916 : 17830893 : for (unsigned i = 0; i < nargs; ++i)
4917 : : {
4918 : 14235507 : tree phi1op = vp1->phiargs[i];
4919 : 14235507 : tree phi2op = vp2->phiargs[i];
4920 : 14235507 : if (phi1op == phi2op)
4921 : 10140356 : continue;
4922 : 4095151 : if (!expressions_equal_p (phi1op, phi2op, false))
4923 : : return false;
4924 : : }
4925 : :
4926 : : return true;
4927 : : }
4928 : :
4929 : : /* Lookup PHI in the current hash table, and return the resulting
4930 : : value number if it exists in the hash table. Return NULL_TREE if
4931 : : it does not exist in the hash table. */
4932 : :
4933 : : static tree
4934 : 23980792 : vn_phi_lookup (gimple *phi, bool backedges_varying_p)
4935 : : {
4936 : 23980792 : vn_phi_s **slot;
4937 : 23980792 : struct vn_phi_s *vp1;
4938 : 23980792 : edge e;
4939 : 23980792 : edge_iterator ei;
4940 : :
4941 : 23980792 : vp1 = XALLOCAVAR (struct vn_phi_s,
4942 : : sizeof (struct vn_phi_s)
4943 : : + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
4944 : :
4945 : : /* Canonicalize the SSA_NAME's to their value number. */
4946 : 82237945 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
4947 : : {
4948 : 58257153 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4949 : 58257153 : if (TREE_CODE (def) == SSA_NAME
4950 : 48790249 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
4951 : : {
4952 : 46852162 : if (!virtual_operand_p (def)
4953 : 46852162 : && ssa_undefined_value_p (def, false))
4954 : 119588 : def = VN_TOP;
4955 : : else
4956 : 46732574 : def = SSA_VAL (def);
4957 : : }
4958 : 58257153 : vp1->phiargs[e->dest_idx] = def;
4959 : : }
4960 : 23980792 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
4961 : 23980792 : vp1->block = gimple_bb (phi);
4962 : : /* Extract values of the controlling condition. */
4963 : 23980792 : vp1->cclhs = NULL_TREE;
4964 : 23980792 : vp1->ccrhs = NULL_TREE;
4965 : 23980792 : if (EDGE_COUNT (vp1->block->preds) == 2
4966 : 23980792 : && vp1->block->loop_father->header != vp1->block)
4967 : : {
4968 : 8202553 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4969 : 8202553 : if (EDGE_COUNT (idom1->succs) == 2)
4970 : 16247956 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
4971 : : {
4972 : : /* ??? We want to use SSA_VAL here. But possibly not
4973 : : allow VN_TOP. */
4974 : 7886767 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
4975 : 7886767 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
4976 : : }
4977 : : }
4978 : 23980792 : vp1->hashcode = vn_phi_compute_hash (vp1);
4979 : 23980792 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
4980 : 23980792 : if (!slot)
4981 : : return NULL_TREE;
4982 : 3639183 : return (*slot)->result;
4983 : : }
4984 : :
4985 : : /* Insert PHI into the current hash table with a value number of
4986 : : RESULT. */
4987 : :
4988 : : static vn_phi_t
4989 : 19830643 : vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
4990 : : {
4991 : 19830643 : vn_phi_s **slot;
4992 : 19830643 : vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
4993 : : sizeof (vn_phi_s)
4994 : : + ((gimple_phi_num_args (phi) - 1)
4995 : : * sizeof (tree)));
4996 : 19830643 : edge e;
4997 : 19830643 : edge_iterator ei;
4998 : :
4999 : : /* Canonicalize the SSA_NAME's to their value number. */
5000 : 69061809 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5001 : : {
5002 : 49231166 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5003 : 49231166 : if (TREE_CODE (def) == SSA_NAME
5004 : 40690356 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5005 : : {
5006 : 38752651 : if (!virtual_operand_p (def)
5007 : 38752651 : && ssa_undefined_value_p (def, false))
5008 : 94738 : def = VN_TOP;
5009 : : else
5010 : 38657913 : def = SSA_VAL (def);
5011 : : }
5012 : 49231166 : vp1->phiargs[e->dest_idx] = def;
5013 : : }
5014 : 19830643 : vp1->value_id = VN_INFO (result)->value_id;
5015 : 19830643 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5016 : 19830643 : vp1->block = gimple_bb (phi);
5017 : : /* Extract values of the controlling condition. */
5018 : 19830643 : vp1->cclhs = NULL_TREE;
5019 : 19830643 : vp1->ccrhs = NULL_TREE;
5020 : 19830643 : if (EDGE_COUNT (vp1->block->preds) == 2
5021 : 19830643 : && vp1->block->loop_father->header != vp1->block)
5022 : : {
5023 : 7840290 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5024 : 7840290 : if (EDGE_COUNT (idom1->succs) == 2)
5025 : 15531220 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5026 : : {
5027 : : /* ??? We want to use SSA_VAL here. But possibly not
5028 : : allow VN_TOP. */
5029 : 7543832 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5030 : 7543832 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5031 : : }
5032 : : }
5033 : 19830643 : vp1->result = result;
5034 : 19830643 : vp1->hashcode = vn_phi_compute_hash (vp1);
5035 : :
5036 : 19830643 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5037 : 19830643 : gcc_assert (!*slot);
5038 : :
5039 : 19830643 : *slot = vp1;
5040 : 19830643 : vp1->next = last_inserted_phi;
5041 : 19830643 : last_inserted_phi = vp1;
5042 : 19830643 : return vp1;
5043 : : }
5044 : :
5045 : :
5046 : : /* Return true if BB1 is dominated by BB2 taking into account edges
5047 : : that are not executable. When ALLOW_BACK is false consider not
5048 : : executable backedges as executable. */
5049 : :
5050 : : static bool
5051 : 57705724 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
5052 : : {
5053 : 57705724 : edge_iterator ei;
5054 : 57705724 : edge e;
5055 : :
5056 : 57705724 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5057 : : return true;
5058 : :
5059 : : /* Before iterating we'd like to know if there exists a
5060 : : (executable) path from bb2 to bb1 at all, if not we can
5061 : : directly return false. For now simply iterate once. */
5062 : :
5063 : : /* Iterate to the single executable bb1 predecessor. */
5064 : 19832376 : if (EDGE_COUNT (bb1->preds) > 1)
5065 : : {
5066 : 2805490 : edge prede = NULL;
5067 : 6031051 : FOR_EACH_EDGE (e, ei, bb1->preds)
5068 : 5648682 : if ((e->flags & EDGE_EXECUTABLE)
5069 : 482293 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5070 : : {
5071 : 5228611 : if (prede)
5072 : : {
5073 : : prede = NULL;
5074 : : break;
5075 : : }
5076 : : prede = e;
5077 : : }
5078 : 2805490 : if (prede)
5079 : : {
5080 : 382369 : bb1 = prede->src;
5081 : :
5082 : : /* Re-do the dominance check with changed bb1. */
5083 : 382369 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5084 : : return true;
5085 : : }
5086 : : }
5087 : :
5088 : : /* Iterate to the single executable bb2 successor. */
5089 : 19643556 : if (EDGE_COUNT (bb2->succs) > 1)
5090 : : {
5091 : 5243151 : edge succe = NULL;
5092 : 10615932 : FOR_EACH_EDGE (e, ei, bb2->succs)
5093 : 10486548 : if ((e->flags & EDGE_EXECUTABLE)
5094 : 167075 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5095 : : {
5096 : 10320043 : if (succe)
5097 : : {
5098 : : succe = NULL;
5099 : : break;
5100 : : }
5101 : : succe = e;
5102 : : }
5103 : 5243151 : if (succe)
5104 : : {
5105 : : /* Verify the reached block is only reached through succe.
5106 : : If there is only one edge we can spare us the dominator
5107 : : check and iterate directly. */
5108 : 92509 : if (EDGE_COUNT (succe->dest->preds) > 1)
5109 : : {
5110 : 51989 : FOR_EACH_EDGE (e, ei, succe->dest->preds)
5111 : 41262 : if (e != succe
5112 : 27261 : && ((e->flags & EDGE_EXECUTABLE)
5113 : 16993 : || (!allow_back && (e->flags & EDGE_DFS_BACK))))
5114 : : {
5115 : : succe = NULL;
5116 : : break;
5117 : : }
5118 : : }
5119 : 92509 : if (succe)
5120 : : {
5121 : 82232 : bb2 = succe->dest;
5122 : :
5123 : : /* Re-do the dominance check with changed bb2. */
5124 : 82232 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5125 : : return true;
5126 : : }
5127 : : }
5128 : : }
5129 : :
5130 : : /* We could now iterate updating bb1 / bb2. */
5131 : : return false;
5132 : : }
5133 : :
5134 : : /* Set the value number of FROM to TO, return true if it has changed
5135 : : as a result. */
5136 : :
5137 : : static inline bool
5138 : 182438468 : set_ssa_val_to (tree from, tree to)
5139 : : {
5140 : 182438468 : vn_ssa_aux_t from_info = VN_INFO (from);
5141 : 182438468 : tree currval = from_info->valnum; // SSA_VAL (from)
5142 : 182438468 : poly_int64 toff, coff;
5143 : 182438468 : bool curr_undefined = false;
5144 : 182438468 : bool curr_invariant = false;
5145 : :
5146 : : /* The only thing we allow as value numbers are ssa_names
5147 : : and invariants. So assert that here. We don't allow VN_TOP
5148 : : as visiting a stmt should produce a value-number other than
5149 : : that.
5150 : : ??? Still VN_TOP can happen for unreachable code, so force
5151 : : it to varying in that case. Not all code is prepared to
5152 : : get VN_TOP on valueization. */
5153 : 182438468 : if (to == VN_TOP)
5154 : : {
5155 : : /* ??? When iterating and visiting PHI <undef, backedge-value>
5156 : : for the first time we rightfully get VN_TOP and we need to
5157 : : preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5158 : : With SCCVN we were simply lucky we iterated the other PHI
5159 : : cycles first and thus visited the backedge-value DEF. */
5160 : 0 : if (currval == VN_TOP)
5161 : 0 : goto set_and_exit;
5162 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5163 : 0 : fprintf (dump_file, "Forcing value number to varying on "
5164 : : "receiving VN_TOP\n");
5165 : : to = from;
5166 : : }
5167 : :
5168 : 182438468 : gcc_checking_assert (to != NULL_TREE
5169 : : && ((TREE_CODE (to) == SSA_NAME
5170 : : && (to == from || SSA_VAL (to) == to))
5171 : : || is_gimple_min_invariant (to)));
5172 : :
5173 : 182438468 : if (from != to)
5174 : : {
5175 : 28356173 : if (currval == from)
5176 : : {
5177 : 9938 : if (dump_file && (dump_flags & TDF_DETAILS))
5178 : : {
5179 : 0 : fprintf (dump_file, "Not changing value number of ");
5180 : 0 : print_generic_expr (dump_file, from);
5181 : 0 : fprintf (dump_file, " from VARYING to ");
5182 : 0 : print_generic_expr (dump_file, to);
5183 : 0 : fprintf (dump_file, "\n");
5184 : : }
5185 : 9938 : return false;
5186 : : }
5187 : 28346235 : curr_invariant = is_gimple_min_invariant (currval);
5188 : 56692470 : curr_undefined = (TREE_CODE (currval) == SSA_NAME
5189 : 3548707 : && !virtual_operand_p (currval)
5190 : 31700428 : && ssa_undefined_value_p (currval, false));
5191 : 28346235 : if (currval != VN_TOP
5192 : : && !curr_invariant
5193 : 4837337 : && !curr_undefined
5194 : 31883470 : && is_gimple_min_invariant (to))
5195 : : {
5196 : 218 : if (dump_file && (dump_flags & TDF_DETAILS))
5197 : : {
5198 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5199 : : "value number of ");
5200 : 0 : print_generic_expr (dump_file, from);
5201 : 0 : fprintf (dump_file, " from ");
5202 : 0 : print_generic_expr (dump_file, currval);
5203 : 0 : fprintf (dump_file, " (non-constant) to ");
5204 : 0 : print_generic_expr (dump_file, to);
5205 : 0 : fprintf (dump_file, " (constant)\n");
5206 : : }
5207 : : to = from;
5208 : : }
5209 : 28346017 : else if (currval != VN_TOP
5210 : 4837119 : && !curr_undefined
5211 : 4825647 : && TREE_CODE (to) == SSA_NAME
5212 : 4125784 : && !virtual_operand_p (to)
5213 : 32277287 : && ssa_undefined_value_p (to, false))
5214 : : {
5215 : 6 : if (dump_file && (dump_flags & TDF_DETAILS))
5216 : : {
5217 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5218 : : "value number of ");
5219 : 0 : print_generic_expr (dump_file, from);
5220 : 0 : fprintf (dump_file, " from ");
5221 : 0 : print_generic_expr (dump_file, currval);
5222 : 0 : fprintf (dump_file, " (non-undefined) to ");
5223 : 0 : print_generic_expr (dump_file, to);
5224 : 0 : fprintf (dump_file, " (undefined)\n");
5225 : : }
5226 : : to = from;
5227 : : }
5228 : 28346011 : else if (TREE_CODE (to) == SSA_NAME
5229 : 28346011 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5230 : : to = from;
5231 : : }
5232 : :
5233 : 154082295 : set_and_exit:
5234 : 182428530 : if (dump_file && (dump_flags & TDF_DETAILS))
5235 : : {
5236 : 365187 : fprintf (dump_file, "Setting value number of ");
5237 : 365187 : print_generic_expr (dump_file, from);
5238 : 365187 : fprintf (dump_file, " to ");
5239 : 365187 : print_generic_expr (dump_file, to);
5240 : : }
5241 : :
5242 : 182428530 : if (currval != to
5243 : 148458538 : && !operand_equal_p (currval, to, 0)
5244 : : /* Different undefined SSA names are not actually different. See
5245 : : PR82320 for a testcase were we'd otherwise not terminate iteration. */
5246 : 148388135 : && !(curr_undefined
5247 : 3211 : && TREE_CODE (to) == SSA_NAME
5248 : 804 : && !virtual_operand_p (to)
5249 : 804 : && ssa_undefined_value_p (to, false))
5250 : : /* ??? For addresses involving volatile objects or types operand_equal_p
5251 : : does not reliably detect ADDR_EXPRs as equal. We know we are only
5252 : : getting invariant gimple addresses here, so can use
5253 : : get_addr_base_and_unit_offset to do this comparison. */
5254 : 330815829 : && !(TREE_CODE (currval) == ADDR_EXPR
5255 : 396812 : && TREE_CODE (to) == ADDR_EXPR
5256 : 12 : && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5257 : 6 : == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5258 : 6 : && known_eq (coff, toff)))
5259 : : {
5260 : 148387293 : if (to != from
5261 : 24375541 : && currval != VN_TOP
5262 : 870018 : && !curr_undefined
5263 : : /* We do not want to allow lattice transitions from one value
5264 : : to another since that may lead to not terminating iteration
5265 : : (see PR95049). Since there's no convenient way to check
5266 : : for the allowed transition of VAL -> PHI (loop entry value,
5267 : : same on two PHIs, to same PHI result) we restrict the check
5268 : : to invariants. */
5269 : 870018 : && curr_invariant
5270 : 148976054 : && is_gimple_min_invariant (to))
5271 : : {
5272 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5273 : 0 : fprintf (dump_file, " forced VARYING");
5274 : : to = from;
5275 : : }
5276 : 148387293 : if (dump_file && (dump_flags & TDF_DETAILS))
5277 : 364801 : fprintf (dump_file, " (changed)\n");
5278 : 148387293 : from_info->valnum = to;
5279 : 148387293 : return true;
5280 : : }
5281 : 34041237 : if (dump_file && (dump_flags & TDF_DETAILS))
5282 : 386 : fprintf (dump_file, "\n");
5283 : : return false;
5284 : : }
5285 : :
5286 : : /* Set all definitions in STMT to value number to themselves.
5287 : : Return true if a value number changed. */
5288 : :
5289 : : static bool
5290 : 225451741 : defs_to_varying (gimple *stmt)
5291 : : {
5292 : 225451741 : bool changed = false;
5293 : 225451741 : ssa_op_iter iter;
5294 : 225451741 : def_operand_p defp;
5295 : :
5296 : 252985905 : FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5297 : : {
5298 : 27534164 : tree def = DEF_FROM_PTR (defp);
5299 : 27534164 : changed |= set_ssa_val_to (def, def);
5300 : : }
5301 : 225451741 : return changed;
5302 : : }
5303 : :
5304 : : /* Visit a copy between LHS and RHS, return true if the value number
5305 : : changed. */
5306 : :
5307 : : static bool
5308 : 7659063 : visit_copy (tree lhs, tree rhs)
5309 : : {
5310 : : /* Valueize. */
5311 : 7659063 : rhs = SSA_VAL (rhs);
5312 : :
5313 : 7659063 : return set_ssa_val_to (lhs, rhs);
5314 : : }
5315 : :
5316 : : /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5317 : : is the same. */
5318 : :
5319 : : static tree
5320 : 2034100 : valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5321 : : {
5322 : 2034100 : if (TREE_CODE (op) == SSA_NAME)
5323 : 1786477 : op = vn_valueize (op);
5324 : :
5325 : : /* Either we have the op widened available. */
5326 : 2034100 : tree ops[3] = {};
5327 : 2034100 : ops[0] = op;
5328 : 2034100 : tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5329 : : wide_type, ops, NULL);
5330 : 2034100 : if (tem)
5331 : : return tem;
5332 : :
5333 : : /* Or the op is truncated from some existing value. */
5334 : 1790878 : if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5335 : : {
5336 : 429088 : gimple *def = SSA_NAME_DEF_STMT (op);
5337 : 429088 : if (is_gimple_assign (def)
5338 : 429088 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5339 : : {
5340 : 222278 : tem = gimple_assign_rhs1 (def);
5341 : 222278 : if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5342 : : {
5343 : 130584 : if (TREE_CODE (tem) == SSA_NAME)
5344 : 130584 : tem = vn_valueize (tem);
5345 : 130584 : return tem;
5346 : : }
5347 : : }
5348 : : }
5349 : :
5350 : : /* For constants simply extend it. */
5351 : 1660294 : if (TREE_CODE (op) == INTEGER_CST)
5352 : 271387 : return wide_int_to_tree (wide_type, wi::to_widest (op));
5353 : :
5354 : : return NULL_TREE;
5355 : : }
5356 : :
5357 : : /* Visit a nary operator RHS, value number it, and return true if the
5358 : : value number of LHS has changed as a result. */
5359 : :
5360 : : static bool
5361 : 42610317 : visit_nary_op (tree lhs, gassign *stmt)
5362 : : {
5363 : 42610317 : vn_nary_op_t vnresult;
5364 : 42610317 : tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5365 : 42610317 : if (! result && vnresult)
5366 : 139230 : result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5367 : 39500323 : if (result)
5368 : 3180713 : return set_ssa_val_to (lhs, result);
5369 : :
5370 : : /* Do some special pattern matching for redundancies of operations
5371 : : in different types. */
5372 : 39429604 : enum tree_code code = gimple_assign_rhs_code (stmt);
5373 : 39429604 : tree type = TREE_TYPE (lhs);
5374 : 39429604 : tree rhs1 = gimple_assign_rhs1 (stmt);
5375 : 39429604 : switch (code)
5376 : : {
5377 : 9250159 : CASE_CONVERT:
5378 : : /* Match arithmetic done in a different type where we can easily
5379 : : substitute the result from some earlier sign-changed or widened
5380 : : operation. */
5381 : 9250159 : if (INTEGRAL_TYPE_P (type)
5382 : 8292737 : && TREE_CODE (rhs1) == SSA_NAME
5383 : : /* We only handle sign-changes, zero-extension -> & mask or
5384 : : sign-extension if we know the inner operation doesn't
5385 : : overflow. */
5386 : 17321976 : && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5387 : 4737894 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5388 : 4736657 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5389 : 7346660 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5390 : 5273151 : || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5391 : : {
5392 : 7086953 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5393 : 4997027 : if (def
5394 : 4997027 : && (gimple_assign_rhs_code (def) == PLUS_EXPR
5395 : 3909958 : || gimple_assign_rhs_code (def) == MINUS_EXPR
5396 : 3792846 : || gimple_assign_rhs_code (def) == MULT_EXPR))
5397 : : {
5398 : 1660302 : tree ops[3] = {};
5399 : : /* When requiring a sign-extension we cannot model a
5400 : : previous truncation with a single op so don't bother. */
5401 : 1660302 : bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5402 : : /* Either we have the op widened available. */
5403 : 1660302 : ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5404 : : allow_truncate);
5405 : 1660302 : if (ops[0])
5406 : 747596 : ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5407 : : allow_truncate);
5408 : 1660302 : if (ops[0] && ops[1])
5409 : : {
5410 : 271395 : ops[0] = vn_nary_op_lookup_pieces
5411 : 271395 : (2, gimple_assign_rhs_code (def), type, ops, NULL);
5412 : : /* We have wider operation available. */
5413 : 271395 : if (ops[0]
5414 : : /* If the leader is a wrapping operation we can
5415 : : insert it for code hoisting w/o introducing
5416 : : undefined overflow. If it is not it has to
5417 : : be available. See PR86554. */
5418 : 271395 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
5419 : 2558 : || (rpo_avail && vn_context_bb
5420 : 2558 : && rpo_avail->eliminate_avail (vn_context_bb,
5421 : : ops[0]))))
5422 : : {
5423 : 8490 : unsigned lhs_prec = TYPE_PRECISION (type);
5424 : 8490 : unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5425 : 8490 : if (lhs_prec == rhs_prec
5426 : 8490 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5427 : 1552 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5428 : : {
5429 : 7938 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5430 : 7938 : NOP_EXPR, type, ops[0]);
5431 : 7938 : result = vn_nary_build_or_lookup (&match_op);
5432 : 7938 : if (result)
5433 : : {
5434 : 7938 : bool changed = set_ssa_val_to (lhs, result);
5435 : 7938 : vn_nary_op_insert_stmt (stmt, result);
5436 : 7938 : return changed;
5437 : : }
5438 : : }
5439 : : else
5440 : : {
5441 : 552 : tree mask = wide_int_to_tree
5442 : 552 : (type, wi::mask (rhs_prec, false, lhs_prec));
5443 : 552 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5444 : : BIT_AND_EXPR,
5445 : 552 : TREE_TYPE (lhs),
5446 : 552 : ops[0], mask);
5447 : 552 : result = vn_nary_build_or_lookup (&match_op);
5448 : 552 : if (result)
5449 : : {
5450 : 552 : bool changed = set_ssa_val_to (lhs, result);
5451 : 552 : vn_nary_op_insert_stmt (stmt, result);
5452 : 552 : return changed;
5453 : : }
5454 : : }
5455 : : }
5456 : : }
5457 : : }
5458 : : }
5459 : : break;
5460 : 1400676 : case BIT_AND_EXPR:
5461 : 1400676 : if (INTEGRAL_TYPE_P (type)
5462 : 1380911 : && TREE_CODE (rhs1) == SSA_NAME
5463 : 1380911 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5464 : 862933 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5465 : 862820 : && default_vn_walk_kind != VN_NOWALK
5466 : : && CHAR_BIT == 8
5467 : : && BITS_PER_UNIT == 8
5468 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5469 : 862610 : && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5470 : 862608 : && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5471 : 2263284 : && !integer_zerop (gimple_assign_rhs2 (stmt)))
5472 : : {
5473 : 862608 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5474 : 619361 : if (ass
5475 : 619361 : && !gimple_has_volatile_ops (ass)
5476 : 616279 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5477 : : {
5478 : 290653 : tree last_vuse = gimple_vuse (ass);
5479 : 290653 : tree op = gimple_assign_rhs1 (ass);
5480 : 871959 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5481 : : default_vn_walk_kind,
5482 : : NULL, true, &last_vuse,
5483 : : gimple_assign_rhs2 (stmt));
5484 : 290653 : if (result
5485 : 291147 : && useless_type_conversion_p (TREE_TYPE (result),
5486 : 494 : TREE_TYPE (op)))
5487 : 494 : return set_ssa_val_to (lhs, result);
5488 : : }
5489 : : }
5490 : : break;
5491 : 314675 : case TRUNC_DIV_EXPR:
5492 : 314675 : if (TYPE_UNSIGNED (type))
5493 : : break;
5494 : : /* Fallthru. */
5495 : 4570890 : case RDIV_EXPR:
5496 : 4570890 : case MULT_EXPR:
5497 : : /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5498 : 4570890 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5499 : : {
5500 : 4569944 : tree rhs[2];
5501 : 4569944 : rhs[0] = rhs1;
5502 : 4569944 : rhs[1] = gimple_assign_rhs2 (stmt);
5503 : 13702743 : for (unsigned i = 0; i <= 1; ++i)
5504 : : {
5505 : 9138615 : unsigned j = i == 0 ? 1 : 0;
5506 : 9138615 : tree ops[2];
5507 : 9138615 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5508 : 9138615 : NEGATE_EXPR, type, rhs[i]);
5509 : 9138615 : ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5510 : 9138615 : ops[j] = rhs[j];
5511 : 9138615 : if (ops[i]
5512 : 9138615 : && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5513 : : type, ops, NULL)))
5514 : : {
5515 : 5816 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5516 : 5816 : NEGATE_EXPR, type, ops[0]);
5517 : 5816 : result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5518 : 5816 : if (result)
5519 : : {
5520 : 5816 : bool changed = set_ssa_val_to (lhs, result);
5521 : 5816 : vn_nary_op_insert_stmt (stmt, result);
5522 : 5816 : return changed;
5523 : : }
5524 : : }
5525 : : }
5526 : : }
5527 : : break;
5528 : 348067 : case LSHIFT_EXPR:
5529 : : /* For X << C, use the value number of X * (1 << C). */
5530 : 348067 : if (INTEGRAL_TYPE_P (type)
5531 : 341612 : && TYPE_OVERFLOW_WRAPS (type)
5532 : 540785 : && !TYPE_SATURATING (type))
5533 : : {
5534 : 192718 : tree rhs2 = gimple_assign_rhs2 (stmt);
5535 : 192718 : if (TREE_CODE (rhs2) == INTEGER_CST
5536 : 123158 : && tree_fits_uhwi_p (rhs2)
5537 : 315876 : && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
5538 : : {
5539 : 123158 : wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
5540 : 123158 : TYPE_PRECISION (type));
5541 : 123158 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5542 : : MULT_EXPR, type, rhs1,
5543 : 123158 : wide_int_to_tree (type, w));
5544 : 123158 : result = vn_nary_build_or_lookup (&match_op);
5545 : 123158 : if (result)
5546 : : {
5547 : 123158 : bool changed = set_ssa_val_to (lhs, result);
5548 : 123158 : if (TREE_CODE (result) == SSA_NAME)
5549 : 123157 : vn_nary_op_insert_stmt (stmt, result);
5550 : 123158 : return changed;
5551 : : }
5552 : 123158 : }
5553 : : }
5554 : : break;
5555 : : default:
5556 : : break;
5557 : : }
5558 : :
5559 : 39291646 : bool changed = set_ssa_val_to (lhs, lhs);
5560 : 39291646 : vn_nary_op_insert_stmt (stmt, lhs);
5561 : 39291646 : return changed;
5562 : : }
5563 : :
5564 : : /* Visit a call STMT storing into LHS. Return true if the value number
5565 : : of the LHS has changed as a result. */
5566 : :
5567 : : static bool
5568 : 7780424 : visit_reference_op_call (tree lhs, gcall *stmt)
5569 : : {
5570 : 7780424 : bool changed = false;
5571 : 7780424 : struct vn_reference_s vr1;
5572 : 7780424 : vn_reference_t vnresult = NULL;
5573 : 7780424 : tree vdef = gimple_vdef (stmt);
5574 : 7780424 : modref_summary *summary;
5575 : :
5576 : : /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5577 : 7780424 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
5578 : 4008115 : lhs = NULL_TREE;
5579 : :
5580 : 7780424 : vn_reference_lookup_call (stmt, &vnresult, &vr1);
5581 : :
5582 : : /* If the lookup did not succeed for pure functions try to use
5583 : : modref info to find a candidate to CSE to. */
5584 : 7780424 : const unsigned accesses_limit = 8;
5585 : 7780424 : if (!vnresult
5586 : 7409493 : && !vdef
5587 : 7409493 : && lhs
5588 : 2601452 : && gimple_vuse (stmt)
5589 : 9287613 : && (((summary = get_modref_function_summary (stmt, NULL))
5590 : 160136 : && !summary->global_memory_read
5591 : 67321 : && summary->load_accesses < accesses_limit)
5592 : 1439966 : || gimple_call_flags (stmt) & ECF_CONST))
5593 : : {
5594 : : /* First search if we can do someting useful and build a
5595 : : vector of all loads we have to check. */
5596 : 67933 : bool unknown_memory_access = false;
5597 : 67933 : auto_vec<ao_ref, accesses_limit> accesses;
5598 : 67933 : unsigned load_accesses = summary ? summary->load_accesses : 0;
5599 : 67933 : if (!unknown_memory_access)
5600 : : /* Add loads done as part of setting up the call arguments.
5601 : : That's also necessary for CONST functions which will
5602 : : not have a modref summary. */
5603 : 197382 : for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
5604 : : {
5605 : 129449 : tree arg = gimple_call_arg (stmt, i);
5606 : 129449 : if (TREE_CODE (arg) != SSA_NAME
5607 : 129449 : && !is_gimple_min_invariant (arg))
5608 : : {
5609 : 47842 : if (accesses.length () >= accesses_limit - load_accesses)
5610 : : {
5611 : : unknown_memory_access = true;
5612 : : break;
5613 : : }
5614 : 23921 : accesses.quick_grow (accesses.length () + 1);
5615 : 23921 : ao_ref_init (&accesses.last (), arg);
5616 : : }
5617 : : }
5618 : 67933 : if (summary && !unknown_memory_access)
5619 : : {
5620 : : /* Add loads as analyzed by IPA modref. */
5621 : 245541 : for (auto base_node : summary->loads->bases)
5622 : 62842 : if (unknown_memory_access)
5623 : : break;
5624 : 254295 : else for (auto ref_node : base_node->refs)
5625 : 66549 : if (unknown_memory_access)
5626 : : break;
5627 : 262170 : else for (auto access_node : ref_node->accesses)
5628 : : {
5629 : 160900 : accesses.quick_grow (accesses.length () + 1);
5630 : 80450 : ao_ref *r = &accesses.last ();
5631 : 80450 : if (!access_node.get_ao_ref (stmt, r))
5632 : : {
5633 : : /* Initialize a ref based on the argument and
5634 : : unknown offset if possible. */
5635 : 17891 : tree arg = access_node.get_call_arg (stmt);
5636 : 17891 : if (arg && TREE_CODE (arg) == SSA_NAME)
5637 : 4854 : arg = SSA_VAL (arg);
5638 : 4854 : if (arg
5639 : 17881 : && TREE_CODE (arg) == ADDR_EXPR
5640 : 13150 : && (arg = get_base_address (arg))
5641 : 18004 : && DECL_P (arg))
5642 : : {
5643 : 0 : ao_ref_init (r, arg);
5644 : 0 : r->ref = NULL_TREE;
5645 : 0 : r->base = arg;
5646 : : }
5647 : : else
5648 : : {
5649 : : unknown_memory_access = true;
5650 : : break;
5651 : : }
5652 : : }
5653 : 62559 : r->base_alias_set = base_node->base;
5654 : 62559 : r->ref_alias_set = ref_node->ref;
5655 : : }
5656 : : }
5657 : :
5658 : : /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5659 : : for the call in the hashtable. */
5660 : 67933 : unsigned limit = (unknown_memory_access
5661 : 67933 : ? 0
5662 : 50042 : : (param_sccvn_max_alias_queries_per_access
5663 : 50042 : / (accesses.length () + 1)));
5664 : 67933 : tree saved_vuse = vr1.vuse;
5665 : 67933 : hashval_t saved_hashcode = vr1.hashcode;
5666 : 299805 : while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
5667 : : {
5668 : 245645 : vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
5669 : 245645 : gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
5670 : : /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5671 : : do not bother for now. */
5672 : 245645 : if (is_a <gphi *> (def))
5673 : : break;
5674 : 463744 : vr1.vuse = vuse_ssa_val (gimple_vuse (def));
5675 : 231872 : vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
5676 : 231872 : vn_reference_lookup_1 (&vr1, &vnresult);
5677 : 231872 : limit--;
5678 : : }
5679 : :
5680 : : /* If we found a candidate to CSE to verify it is valid. */
5681 : 67933 : if (vnresult && !accesses.is_empty ())
5682 : : {
5683 : 1629 : tree vuse = vuse_ssa_val (gimple_vuse (stmt));
5684 : 6073 : while (vnresult && vuse != vr1.vuse)
5685 : : {
5686 : 2815 : gimple *def = SSA_NAME_DEF_STMT (vuse);
5687 : 14531 : for (auto &ref : accesses)
5688 : : {
5689 : : /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5690 : : analysis overhead that we might be able to cache. */
5691 : 7648 : if (stmt_may_clobber_ref_p_1 (def, &ref, true))
5692 : : {
5693 : 1562 : vnresult = NULL;
5694 : 1562 : break;
5695 : : }
5696 : : }
5697 : 5630 : vuse = vuse_ssa_val (gimple_vuse (def));
5698 : : }
5699 : : }
5700 : 67933 : vr1.vuse = saved_vuse;
5701 : 67933 : vr1.hashcode = saved_hashcode;
5702 : 67933 : }
5703 : :
5704 : 7780424 : if (vnresult)
5705 : : {
5706 : 371289 : if (vdef)
5707 : : {
5708 : 166139 : if (vnresult->result_vdef)
5709 : 166139 : changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
5710 : 0 : else if (!lhs && gimple_call_lhs (stmt))
5711 : : /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5712 : : as the call still acts as a lhs store. */
5713 : 0 : changed |= set_ssa_val_to (vdef, vdef);
5714 : : else
5715 : : /* If the call was discovered to be pure or const reflect
5716 : : that as far as possible. */
5717 : 0 : changed |= set_ssa_val_to (vdef,
5718 : : vuse_ssa_val (gimple_vuse (stmt)));
5719 : : }
5720 : :
5721 : 371289 : if (!vnresult->result && lhs)
5722 : 0 : vnresult->result = lhs;
5723 : :
5724 : 371289 : if (vnresult->result && lhs)
5725 : 79736 : changed |= set_ssa_val_to (lhs, vnresult->result);
5726 : : }
5727 : : else
5728 : : {
5729 : 7409135 : vn_reference_t vr2;
5730 : 7409135 : vn_reference_s **slot;
5731 : 7409135 : tree vdef_val = vdef;
5732 : 7409135 : if (vdef)
5733 : : {
5734 : : /* If we value numbered an indirect functions function to
5735 : : one not clobbering memory value number its VDEF to its
5736 : : VUSE. */
5737 : 4586440 : tree fn = gimple_call_fn (stmt);
5738 : 4586440 : if (fn && TREE_CODE (fn) == SSA_NAME)
5739 : : {
5740 : 126080 : fn = SSA_VAL (fn);
5741 : 126080 : if (TREE_CODE (fn) == ADDR_EXPR
5742 : 1425 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5743 : 1425 : && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
5744 : 1425 : & (ECF_CONST | ECF_PURE))
5745 : : /* If stmt has non-SSA_NAME lhs, value number the
5746 : : vdef to itself, as the call still acts as a lhs
5747 : : store. */
5748 : 126942 : && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
5749 : 1546 : vdef_val = vuse_ssa_val (gimple_vuse (stmt));
5750 : : }
5751 : 4586440 : changed |= set_ssa_val_to (vdef, vdef_val);
5752 : : }
5753 : 7409135 : if (lhs)
5754 : 3692573 : changed |= set_ssa_val_to (lhs, lhs);
5755 : 7409135 : vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
5756 : 7409135 : vr2->vuse = vr1.vuse;
5757 : : /* As we are not walking the virtual operand chain we know the
5758 : : shared_lookup_references are still original so we can re-use
5759 : : them here. */
5760 : 7409135 : vr2->operands = vr1.operands.copy ();
5761 : 7409135 : vr2->type = vr1.type;
5762 : 7409135 : vr2->punned = vr1.punned;
5763 : 7409135 : vr2->set = vr1.set;
5764 : 7409135 : vr2->offset = vr1.offset;
5765 : 7409135 : vr2->max_size = vr1.max_size;
5766 : 7409135 : vr2->base_set = vr1.base_set;
5767 : 7409135 : vr2->hashcode = vr1.hashcode;
5768 : 7409135 : vr2->result = lhs;
5769 : 7409135 : vr2->result_vdef = vdef_val;
5770 : 7409135 : vr2->value_id = 0;
5771 : 7409135 : slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
5772 : : INSERT);
5773 : 7409135 : gcc_assert (!*slot);
5774 : 7409135 : *slot = vr2;
5775 : 7409135 : vr2->next = last_inserted_ref;
5776 : 7409135 : last_inserted_ref = vr2;
5777 : : }
5778 : :
5779 : 7780424 : return changed;
5780 : : }
5781 : :
5782 : : /* Visit a load from a reference operator RHS, part of STMT, value number it,
5783 : : and return true if the value number of the LHS has changed as a result. */
5784 : :
5785 : : static bool
5786 : 30596666 : visit_reference_op_load (tree lhs, tree op, gimple *stmt)
5787 : : {
5788 : 30596666 : bool changed = false;
5789 : 30596666 : tree result;
5790 : 30596666 : vn_reference_t res;
5791 : :
5792 : 30596666 : tree vuse = gimple_vuse (stmt);
5793 : 30596666 : tree last_vuse = vuse;
5794 : 30596666 : result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
5795 : :
5796 : : /* We handle type-punning through unions by value-numbering based
5797 : : on offset and size of the access. Be prepared to handle a
5798 : : type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5799 : 30596666 : if (result
5800 : 30596666 : && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
5801 : : {
5802 : : /* Avoid the type punning in case the result mode has padding where
5803 : : the op we lookup has not. */
5804 : 17961 : if (TYPE_MODE (TREE_TYPE (result)) != BLKmode
5805 : 35870 : && maybe_lt (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (result))),
5806 : 17909 : GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op)))))
5807 : : result = NULL_TREE;
5808 : 17888 : else if (CONSTANT_CLASS_P (result))
5809 : 2579 : result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5810 : : else
5811 : : {
5812 : : /* We will be setting the value number of lhs to the value number
5813 : : of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5814 : : So first simplify and lookup this expression to see if it
5815 : : is already available. */
5816 : 15309 : gimple_match_op res_op (gimple_match_cond::UNCOND,
5817 : 15309 : VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5818 : 15309 : result = vn_nary_build_or_lookup (&res_op);
5819 : 15309 : if (result
5820 : 15309 : && TREE_CODE (result) == SSA_NAME
5821 : 29033 : && VN_INFO (result)->needs_insertion)
5822 : : /* Track whether this is the canonical expression for different
5823 : : typed loads. We use that as a stopgap measure for code
5824 : : hoisting when dealing with floating point loads. */
5825 : 12987 : res->punned = true;
5826 : : }
5827 : :
5828 : : /* When building the conversion fails avoid inserting the reference
5829 : : again. */
5830 : 17888 : if (!result)
5831 : 75 : return set_ssa_val_to (lhs, lhs);
5832 : : }
5833 : :
5834 : 30578705 : if (result)
5835 : 4321938 : changed = set_ssa_val_to (lhs, result);
5836 : : else
5837 : : {
5838 : 26274653 : changed = set_ssa_val_to (lhs, lhs);
5839 : 26274653 : vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
5840 : 26274653 : if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
5841 : : {
5842 : 8622265 : if (dump_file && (dump_flags & TDF_DETAILS))
5843 : : {
5844 : 12927 : fprintf (dump_file, "Using extra use virtual operand ");
5845 : 12927 : print_generic_expr (dump_file, last_vuse);
5846 : 12927 : fprintf (dump_file, "\n");
5847 : : }
5848 : 8622265 : vn_reference_insert (op, lhs, vuse, NULL_TREE);
5849 : : }
5850 : : }
5851 : :
5852 : : return changed;
5853 : : }
5854 : :
5855 : :
5856 : : /* Visit a store to a reference operator LHS, part of STMT, value number it,
5857 : : and return true if the value number of the LHS has changed as a result. */
5858 : :
5859 : : static bool
5860 : 29087246 : visit_reference_op_store (tree lhs, tree op, gimple *stmt)
5861 : : {
5862 : 29087246 : bool changed = false;
5863 : 29087246 : vn_reference_t vnresult = NULL;
5864 : 29087246 : tree assign;
5865 : 29087246 : bool resultsame = false;
5866 : 29087246 : tree vuse = gimple_vuse (stmt);
5867 : 29087246 : tree vdef = gimple_vdef (stmt);
5868 : :
5869 : 29087246 : if (TREE_CODE (op) == SSA_NAME)
5870 : 12870871 : op = SSA_VAL (op);
5871 : :
5872 : : /* First we want to lookup using the *vuses* from the store and see
5873 : : if there the last store to this location with the same address
5874 : : had the same value.
5875 : :
5876 : : The vuses represent the memory state before the store. If the
5877 : : memory state, address, and value of the store is the same as the
5878 : : last store to this location, then this store will produce the
5879 : : same memory state as that store.
5880 : :
5881 : : In this case the vdef versions for this store are value numbered to those
5882 : : vuse versions, since they represent the same memory state after
5883 : : this store.
5884 : :
5885 : : Otherwise, the vdefs for the store are used when inserting into
5886 : : the table, since the store generates a new memory state. */
5887 : :
5888 : 29087246 : vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
5889 : 29087246 : if (vnresult
5890 : 1431948 : && vnresult->result)
5891 : : {
5892 : 1431948 : tree result = vnresult->result;
5893 : 1431948 : gcc_checking_assert (TREE_CODE (result) != SSA_NAME
5894 : : || result == SSA_VAL (result));
5895 : 1431948 : resultsame = expressions_equal_p (result, op);
5896 : 1431948 : if (resultsame)
5897 : : {
5898 : : /* If the TBAA state isn't compatible for downstream reads
5899 : : we cannot value-number the VDEFs the same. */
5900 : 49465 : ao_ref lhs_ref;
5901 : 49465 : ao_ref_init (&lhs_ref, lhs);
5902 : 49465 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
5903 : 49465 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
5904 : 49465 : if ((vnresult->set != set
5905 : 2085 : && ! alias_set_subset_of (set, vnresult->set))
5906 : 51186 : || (vnresult->base_set != base_set
5907 : 5917 : && ! alias_set_subset_of (base_set, vnresult->base_set)))
5908 : 620 : resultsame = false;
5909 : : }
5910 : : }
5911 : :
5912 : 620 : if (!resultsame)
5913 : : {
5914 : 29038401 : if (dump_file && (dump_flags & TDF_DETAILS))
5915 : : {
5916 : 20473 : fprintf (dump_file, "No store match\n");
5917 : 20473 : fprintf (dump_file, "Value numbering store ");
5918 : 20473 : print_generic_expr (dump_file, lhs);
5919 : 20473 : fprintf (dump_file, " to ");
5920 : 20473 : print_generic_expr (dump_file, op);
5921 : 20473 : fprintf (dump_file, "\n");
5922 : : }
5923 : : /* Have to set value numbers before insert, since insert is
5924 : : going to valueize the references in-place. */
5925 : 29038401 : if (vdef)
5926 : 29038401 : changed |= set_ssa_val_to (vdef, vdef);
5927 : :
5928 : : /* Do not insert structure copies into the tables. */
5929 : 29038401 : if (is_gimple_min_invariant (op)
5930 : 29038401 : || is_gimple_reg (op))
5931 : 25485878 : vn_reference_insert (lhs, op, vdef, NULL);
5932 : :
5933 : : /* Only perform the following when being called from PRE
5934 : : which embeds tail merging. */
5935 : 29038401 : if (default_vn_walk_kind == VN_WALK)
5936 : : {
5937 : 6957455 : assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
5938 : 6957455 : vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
5939 : 6957455 : if (!vnresult)
5940 : 6919275 : vn_reference_insert (assign, lhs, vuse, vdef);
5941 : : }
5942 : : }
5943 : : else
5944 : : {
5945 : : /* We had a match, so value number the vdef to have the value
5946 : : number of the vuse it came from. */
5947 : :
5948 : 48845 : if (dump_file && (dump_flags & TDF_DETAILS))
5949 : 9 : fprintf (dump_file, "Store matched earlier value, "
5950 : : "value numbering store vdefs to matching vuses.\n");
5951 : :
5952 : 48845 : changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
5953 : : }
5954 : :
5955 : 29087246 : return changed;
5956 : : }
5957 : :
5958 : : /* Visit and value number PHI, return true if the value number
5959 : : changed. When BACKEDGES_VARYING_P is true then assume all
5960 : : backedge values are varying. When INSERTED is not NULL then
5961 : : this is just a ahead query for a possible iteration, set INSERTED
5962 : : to true if we'd insert into the hashtable. */
5963 : :
5964 : : static bool
5965 : 29910793 : visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
5966 : : {
5967 : 29910793 : tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
5968 : 29910793 : bool seen_undef_visited = false;
5969 : 29910793 : tree backedge_val = NULL_TREE;
5970 : 29910793 : bool seen_non_backedge = false;
5971 : 29910793 : tree sameval_base = NULL_TREE;
5972 : 29910793 : poly_int64 soff, doff;
5973 : 29910793 : unsigned n_executable = 0;
5974 : 29910793 : edge_iterator ei;
5975 : 29910793 : edge e, sameval_e = NULL;
5976 : :
5977 : : /* TODO: We could check for this in initialization, and replace this
5978 : : with a gcc_assert. */
5979 : 29910793 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
5980 : 26131 : return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
5981 : :
5982 : : /* We track whether a PHI was CSEd to avoid excessive iterations
5983 : : that would be necessary only because the PHI changed arguments
5984 : : but not value. */
5985 : 29884662 : if (!inserted)
5986 : 23342418 : gimple_set_plf (phi, GF_PLF_1, false);
5987 : :
5988 : : /* See if all non-TOP arguments have the same value. TOP is
5989 : : equivalent to everything, so we can ignore it. */
5990 : 29884662 : basic_block bb = gimple_bb (phi);
5991 : 65976882 : FOR_EACH_EDGE (e, ei, bb->preds)
5992 : 59437583 : if (e->flags & EDGE_EXECUTABLE)
5993 : : {
5994 : 55140653 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5995 : :
5996 : 55140653 : if (def == PHI_RESULT (phi))
5997 : 296079 : continue;
5998 : 54861974 : ++n_executable;
5999 : 54861974 : bool visited = true;
6000 : 54861974 : if (TREE_CODE (def) == SSA_NAME)
6001 : : {
6002 : 44236281 : tree val = SSA_VAL (def, &visited);
6003 : 44236281 : if (SSA_NAME_IS_DEFAULT_DEF (def))
6004 : 2424162 : visited = true;
6005 : 44236281 : if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6006 : 42302465 : def = val;
6007 : 44236281 : if (e->flags & EDGE_DFS_BACK)
6008 : 13008873 : backedge_val = def;
6009 : : }
6010 : 54861974 : if (!(e->flags & EDGE_DFS_BACK))
6011 : 41688832 : seen_non_backedge = true;
6012 : 54861974 : if (def == VN_TOP)
6013 : : ;
6014 : : /* Ignore undefined defs for sameval but record one. */
6015 : 54861974 : else if (TREE_CODE (def) == SSA_NAME
6016 : 41351914 : && ! virtual_operand_p (def)
6017 : 74922973 : && ssa_undefined_value_p (def, false))
6018 : : {
6019 : 212213 : if (!seen_undef
6020 : : /* Avoid having not visited undefined defs if we also have
6021 : : a visited one. */
6022 : 27514 : || (!seen_undef_visited && visited))
6023 : : {
6024 : 184702 : seen_undef = def;
6025 : 184702 : seen_undef_visited = visited;
6026 : : }
6027 : : }
6028 : 54649761 : else if (sameval == VN_TOP)
6029 : : {
6030 : : sameval = def;
6031 : : sameval_e = e;
6032 : : }
6033 : 24816292 : else if (expressions_equal_p (def, sameval))
6034 : : sameval_e = NULL;
6035 : 41777329 : else if (virtual_operand_p (def))
6036 : : {
6037 : : sameval = NULL_TREE;
6038 : 23345363 : break;
6039 : : }
6040 : : else
6041 : : {
6042 : : /* We know we're arriving only with invariant addresses here,
6043 : : try harder comparing them. We can do some caching here
6044 : : which we cannot do in expressions_equal_p. */
6045 : 14088775 : if (TREE_CODE (def) == ADDR_EXPR
6046 : 255992 : && TREE_CODE (sameval) == ADDR_EXPR
6047 : 102694 : && sameval_base != (void *)-1)
6048 : : {
6049 : 102694 : if (!sameval_base)
6050 : 102692 : sameval_base = get_addr_base_and_unit_offset
6051 : 102692 : (TREE_OPERAND (sameval, 0), &soff);
6052 : 102692 : if (!sameval_base)
6053 : : sameval_base = (tree)(void *)-1;
6054 : 102699 : else if ((get_addr_base_and_unit_offset
6055 : 102694 : (TREE_OPERAND (def, 0), &doff) == sameval_base)
6056 : 102694 : && known_eq (soff, doff))
6057 : 5 : continue;
6058 : : }
6059 : : /* There's also the possibility to use equivalences. */
6060 : 27145885 : if (!FLOAT_TYPE_P (TREE_TYPE (def))
6061 : : /* But only do this if we didn't force any of sameval or
6062 : : val to VARYING because of backedge processing rules. */
6063 : 12963934 : && (TREE_CODE (sameval) != SSA_NAME
6064 : 7773567 : || SSA_VAL (sameval) == sameval)
6065 : 27052648 : && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6066 : : {
6067 : 12963870 : vn_nary_op_t vnresult;
6068 : 12963870 : tree ops[2];
6069 : 12963870 : ops[0] = def;
6070 : 12963870 : ops[1] = sameval;
6071 : 12963870 : tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6072 : : boolean_type_node,
6073 : : ops, &vnresult);
6074 : 12963870 : if (! val && vnresult && vnresult->predicated_values)
6075 : : {
6076 : 145852 : val = vn_nary_op_get_predicated_value (vnresult, e);
6077 : 86335 : if (val && integer_truep (val)
6078 : 159918 : && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6079 : : {
6080 : 13976 : if (dump_file && (dump_flags & TDF_DETAILS))
6081 : : {
6082 : 2 : fprintf (dump_file, "Predication says ");
6083 : 2 : print_generic_expr (dump_file, def, TDF_NONE);
6084 : 2 : fprintf (dump_file, " and ");
6085 : 2 : print_generic_expr (dump_file, sameval, TDF_NONE);
6086 : 2 : fprintf (dump_file, " are equal on edge %d -> %d\n",
6087 : 2 : e->src->index, e->dest->index);
6088 : : }
6089 : 17395 : continue;
6090 : : }
6091 : : /* If on all previous edges the value was equal to def
6092 : : we can change sameval to def. */
6093 : 13078351 : if (EDGE_COUNT (bb->preds) == 2
6094 : 90039 : && (val = vn_nary_op_get_predicated_value
6095 : 90039 : (vnresult, EDGE_PRED (bb, 0)))
6096 : 23626 : && integer_truep (val)
6097 : 135295 : && !(e->flags & EDGE_DFS_BACK))
6098 : : {
6099 : 3419 : if (dump_file && (dump_flags & TDF_DETAILS))
6100 : : {
6101 : 0 : fprintf (dump_file, "Predication says ");
6102 : 0 : print_generic_expr (dump_file, def, TDF_NONE);
6103 : 0 : fprintf (dump_file, " and ");
6104 : 0 : print_generic_expr (dump_file, sameval, TDF_NONE);
6105 : 0 : fprintf (dump_file, " are equal on edge %d -> %d\n",
6106 : 0 : EDGE_PRED (bb, 0)->src->index,
6107 : 0 : EDGE_PRED (bb, 0)->dest->index);
6108 : : }
6109 : 3419 : sameval = def;
6110 : 3419 : continue;
6111 : : }
6112 : : }
6113 : : }
6114 : : sameval = NULL_TREE;
6115 : : break;
6116 : : }
6117 : : }
6118 : :
6119 : : /* If the value we want to use is flowing over the backedge and we
6120 : : should take it as VARYING but it has a non-VARYING value drop to
6121 : : VARYING.
6122 : : If we value-number a virtual operand never value-number to the
6123 : : value from the backedge as that confuses the alias-walking code.
6124 : : See gcc.dg/torture/pr87176.c. If the value is the same on a
6125 : : non-backedge everything is OK though. */
6126 : 29884662 : bool visited_p;
6127 : 29884662 : if ((backedge_val
6128 : 29884662 : && !seen_non_backedge
6129 : 1381 : && TREE_CODE (backedge_val) == SSA_NAME
6130 : 1185 : && sameval == backedge_val
6131 : 413 : && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6132 : 72 : || SSA_VAL (backedge_val) != backedge_val))
6133 : : /* Do not value-number a virtual operand to sth not visited though
6134 : : given that allows us to escape a region in alias walking. */
6135 : 29885701 : || (sameval
6136 : 6538957 : && TREE_CODE (sameval) == SSA_NAME
6137 : 3856795 : && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6138 : 3200439 : && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6139 : 1655866 : && (SSA_VAL (sameval, &visited_p), !visited_p)))
6140 : : /* Note this just drops to VARYING without inserting the PHI into
6141 : : the hashes. */
6142 : 239779 : result = PHI_RESULT (phi);
6143 : : /* If none of the edges was executable keep the value-number at VN_TOP,
6144 : : if only a single edge is exectuable use its value. */
6145 : 29644883 : else if (n_executable <= 1)
6146 : 5659601 : result = seen_undef ? seen_undef : sameval;
6147 : : /* If we saw only undefined values and VN_TOP use one of the
6148 : : undefined values. */
6149 : 23985282 : else if (sameval == VN_TOP)
6150 : 4490 : result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6151 : : /* First see if it is equivalent to a phi node in this block. We prefer
6152 : : this as it allows IV elimination - see PRs 66502 and 67167. */
6153 : 23980792 : else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6154 : : {
6155 : 3639183 : if (!inserted
6156 : 63875 : && TREE_CODE (result) == SSA_NAME
6157 : 3703058 : && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6158 : : {
6159 : 63875 : gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6160 : 63875 : if (dump_file && (dump_flags & TDF_DETAILS))
6161 : : {
6162 : 2 : fprintf (dump_file, "Marking CSEd to PHI node ");
6163 : 2 : print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6164 : : 0, TDF_SLIM);
6165 : 2 : fprintf (dump_file, "\n");
6166 : : }
6167 : : }
6168 : : }
6169 : : /* If all values are the same use that, unless we've seen undefined
6170 : : values as well and the value isn't constant.
6171 : : CCP/copyprop have the same restriction to not remove uninit warnings. */
6172 : 20341609 : else if (sameval
6173 : 20341609 : && (! seen_undef || is_gimple_min_invariant (sameval)))
6174 : : result = sameval;
6175 : : else
6176 : : {
6177 : 19830643 : result = PHI_RESULT (phi);
6178 : : /* Only insert PHIs that are varying, for constant value numbers
6179 : : we mess up equivalences otherwise as we are only comparing
6180 : : the immediate controlling predicates. */
6181 : 19830643 : vn_phi_insert (phi, result, backedges_varying_p);
6182 : 19830643 : if (inserted)
6183 : 2853682 : *inserted = true;
6184 : : }
6185 : :
6186 : 29884662 : return set_ssa_val_to (PHI_RESULT (phi), result);
6187 : : }
6188 : :
6189 : : /* Try to simplify RHS using equivalences and constant folding. */
6190 : :
6191 : : static tree
6192 : 111361450 : try_to_simplify (gassign *stmt)
6193 : : {
6194 : 111361450 : enum tree_code code = gimple_assign_rhs_code (stmt);
6195 : 111361450 : tree tem;
6196 : :
6197 : : /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6198 : : in this case, there is no point in doing extra work. */
6199 : 111361450 : if (code == SSA_NAME)
6200 : : return NULL_TREE;
6201 : :
6202 : : /* First try constant folding based on our current lattice. */
6203 : 98490347 : mprts_hook = vn_lookup_simplify_result;
6204 : 98490347 : tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6205 : 98490347 : mprts_hook = NULL;
6206 : 98490347 : if (tem
6207 : 98490347 : && (TREE_CODE (tem) == SSA_NAME
6208 : 21745574 : || is_gimple_min_invariant (tem)))
6209 : 21680414 : return tem;
6210 : :
6211 : : return NULL_TREE;
6212 : : }
6213 : :
6214 : : /* Visit and value number STMT, return true if the value number
6215 : : changed. */
6216 : :
6217 : : static bool
6218 : 373060654 : visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6219 : : {
6220 : 373060654 : bool changed = false;
6221 : :
6222 : 373060654 : if (dump_file && (dump_flags & TDF_DETAILS))
6223 : : {
6224 : 376172 : fprintf (dump_file, "Value numbering stmt = ");
6225 : 376172 : print_gimple_stmt (dump_file, stmt, 0);
6226 : : }
6227 : :
6228 : 373060654 : if (gimple_code (stmt) == GIMPLE_PHI)
6229 : 23360511 : changed = visit_phi (stmt, NULL, backedges_varying_p);
6230 : 502477427 : else if (gimple_has_volatile_ops (stmt))
6231 : 8450988 : changed = defs_to_varying (stmt);
6232 : 341249155 : else if (gassign *ass = dyn_cast <gassign *> (stmt))
6233 : : {
6234 : 116475097 : enum tree_code code = gimple_assign_rhs_code (ass);
6235 : 116475097 : tree lhs = gimple_assign_lhs (ass);
6236 : 116475097 : tree rhs1 = gimple_assign_rhs1 (ass);
6237 : 116475097 : tree simplified;
6238 : :
6239 : : /* Shortcut for copies. Simplifying copies is pointless,
6240 : : since we copy the expression and value they represent. */
6241 : 116475097 : if (code == SSA_NAME
6242 : 17984750 : && TREE_CODE (lhs) == SSA_NAME)
6243 : : {
6244 : 5113647 : changed = visit_copy (lhs, rhs1);
6245 : 5113647 : goto done;
6246 : : }
6247 : 111361450 : simplified = try_to_simplify (ass);
6248 : 111361450 : if (simplified)
6249 : : {
6250 : 21680414 : if (dump_file && (dump_flags & TDF_DETAILS))
6251 : : {
6252 : 24847 : fprintf (dump_file, "RHS ");
6253 : 24847 : print_gimple_expr (dump_file, ass, 0);
6254 : 24847 : fprintf (dump_file, " simplified to ");
6255 : 24847 : print_generic_expr (dump_file, simplified);
6256 : 24847 : fprintf (dump_file, "\n");
6257 : : }
6258 : : }
6259 : : /* Setting value numbers to constants will occasionally
6260 : : screw up phi congruence because constants are not
6261 : : uniquely associated with a single ssa name that can be
6262 : : looked up. */
6263 : 21680414 : if (simplified
6264 : 21680414 : && is_gimple_min_invariant (simplified)
6265 : 19135295 : && TREE_CODE (lhs) == SSA_NAME)
6266 : : {
6267 : 6471407 : changed = set_ssa_val_to (lhs, simplified);
6268 : 6471407 : goto done;
6269 : : }
6270 : 104890043 : else if (simplified
6271 : 15209007 : && TREE_CODE (simplified) == SSA_NAME
6272 : 2545119 : && TREE_CODE (lhs) == SSA_NAME)
6273 : : {
6274 : 2545119 : changed = visit_copy (lhs, simplified);
6275 : 2545119 : goto done;
6276 : : }
6277 : :
6278 : 102344924 : if ((TREE_CODE (lhs) == SSA_NAME
6279 : : /* We can substitute SSA_NAMEs that are live over
6280 : : abnormal edges with their constant value. */
6281 : 73257446 : && !(gimple_assign_copy_p (ass)
6282 : 0 : && is_gimple_min_invariant (rhs1))
6283 : 73257446 : && !(simplified
6284 : 0 : && is_gimple_min_invariant (simplified))
6285 : 73257446 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6286 : : /* Stores or copies from SSA_NAMEs that are live over
6287 : : abnormal edges are a problem. */
6288 : 175601131 : || (code == SSA_NAME
6289 : 12871103 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6290 : 1471 : changed = defs_to_varying (ass);
6291 : 102343453 : else if (REFERENCE_CLASS_P (lhs)
6292 : 102343453 : || DECL_P (lhs))
6293 : 29087246 : changed = visit_reference_op_store (lhs, rhs1, ass);
6294 : 73256207 : else if (TREE_CODE (lhs) == SSA_NAME)
6295 : : {
6296 : 73256207 : if ((gimple_assign_copy_p (ass)
6297 : 0 : && is_gimple_min_invariant (rhs1))
6298 : 73256207 : || (simplified
6299 : 0 : && is_gimple_min_invariant (simplified)))
6300 : : {
6301 : 0 : if (simplified)
6302 : 0 : changed = set_ssa_val_to (lhs, simplified);
6303 : : else
6304 : 0 : changed = set_ssa_val_to (lhs, rhs1);
6305 : : }
6306 : : else
6307 : : {
6308 : : /* Visit the original statement. */
6309 : 73256207 : switch (vn_get_stmt_kind (ass))
6310 : : {
6311 : 42610317 : case VN_NARY:
6312 : 42610317 : changed = visit_nary_op (lhs, ass);
6313 : 42610317 : break;
6314 : 30596666 : case VN_REFERENCE:
6315 : 30596666 : changed = visit_reference_op_load (lhs, rhs1, ass);
6316 : 30596666 : break;
6317 : 49224 : default:
6318 : 49224 : changed = defs_to_varying (ass);
6319 : 49224 : break;
6320 : : }
6321 : : }
6322 : : }
6323 : : else
6324 : 0 : changed = defs_to_varying (ass);
6325 : : }
6326 : 224774058 : else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6327 : : {
6328 : 22517940 : tree lhs = gimple_call_lhs (call_stmt);
6329 : 22517940 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6330 : : {
6331 : : /* Try constant folding based on our current lattice. */
6332 : 7715231 : tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6333 : : vn_valueize);
6334 : 7715231 : if (simplified)
6335 : : {
6336 : 49111 : if (dump_file && (dump_flags & TDF_DETAILS))
6337 : : {
6338 : 1 : fprintf (dump_file, "call ");
6339 : 1 : print_gimple_expr (dump_file, call_stmt, 0);
6340 : 1 : fprintf (dump_file, " simplified to ");
6341 : 1 : print_generic_expr (dump_file, simplified);
6342 : 1 : fprintf (dump_file, "\n");
6343 : : }
6344 : : }
6345 : : /* Setting value numbers to constants will occasionally
6346 : : screw up phi congruence because constants are not
6347 : : uniquely associated with a single ssa name that can be
6348 : : looked up. */
6349 : 49111 : if (simplified
6350 : 49111 : && is_gimple_min_invariant (simplified))
6351 : : {
6352 : 43279 : changed = set_ssa_val_to (lhs, simplified);
6353 : 86558 : if (gimple_vdef (call_stmt))
6354 : 645 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6355 : : SSA_VAL (gimple_vuse (call_stmt)));
6356 : 43279 : goto done;
6357 : : }
6358 : 7671952 : else if (simplified
6359 : 5832 : && TREE_CODE (simplified) == SSA_NAME)
6360 : : {
6361 : 297 : changed = visit_copy (lhs, simplified);
6362 : 594 : if (gimple_vdef (call_stmt))
6363 : 0 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6364 : : SSA_VAL (gimple_vuse (call_stmt)));
6365 : 297 : goto done;
6366 : : }
6367 : 7671655 : else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6368 : : {
6369 : 381 : changed = defs_to_varying (call_stmt);
6370 : 381 : goto done;
6371 : : }
6372 : : }
6373 : :
6374 : : /* Pick up flags from a devirtualization target. */
6375 : 22473983 : tree fn = gimple_call_fn (stmt);
6376 : 22473983 : int extra_fnflags = 0;
6377 : 22473983 : if (fn && TREE_CODE (fn) == SSA_NAME)
6378 : : {
6379 : 516145 : fn = SSA_VAL (fn);
6380 : 516145 : if (TREE_CODE (fn) == ADDR_EXPR
6381 : 516145 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6382 : 4048 : extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6383 : : }
6384 : 22473983 : if ((/* Calls to the same function with the same vuse
6385 : : and the same operands do not necessarily return the same
6386 : : value, unless they're pure or const. */
6387 : 22473983 : ((gimple_call_flags (call_stmt) | extra_fnflags)
6388 : 22473983 : & (ECF_PURE | ECF_CONST))
6389 : : /* If calls have a vdef, subsequent calls won't have
6390 : : the same incoming vuse. So, if 2 calls with vdef have the
6391 : : same vuse, we know they're not subsequent.
6392 : : We can value number 2 calls to the same function with the
6393 : : same vuse and the same operands which are not subsequent
6394 : : the same, because there is no code in the program that can
6395 : : compare the 2 values... */
6396 : 19376831 : || (gimple_vdef (call_stmt)
6397 : : /* ... unless the call returns a pointer which does
6398 : : not alias with anything else. In which case the
6399 : : information that the values are distinct are encoded
6400 : : in the IL. */
6401 : 19342215 : && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6402 : : /* Only perform the following when being called from PRE
6403 : : which embeds tail merging. */
6404 : 18963890 : && default_vn_walk_kind == VN_WALK))
6405 : : /* Do not process .DEFERRED_INIT since that confuses uninit
6406 : : analysis. */
6407 : 27157968 : && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6408 : 7780424 : changed = visit_reference_op_call (lhs, call_stmt);
6409 : : else
6410 : 14693559 : changed = defs_to_varying (call_stmt);
6411 : : }
6412 : : else
6413 : 202256118 : changed = defs_to_varying (stmt);
6414 : 373060654 : done:
6415 : 373060654 : return changed;
6416 : : }
6417 : :
6418 : :
6419 : : /* Allocate a value number table. */
6420 : :
6421 : : static void
6422 : 5634002 : allocate_vn_table (vn_tables_t table, unsigned size)
6423 : : {
6424 : 5634002 : table->phis = new vn_phi_table_type (size);
6425 : 5634002 : table->nary = new vn_nary_op_table_type (size);
6426 : 5634002 : table->references = new vn_reference_table_type (size);
6427 : 5634002 : }
6428 : :
6429 : : /* Free a value number table. */
6430 : :
6431 : : static void
6432 : 5634002 : free_vn_table (vn_tables_t table)
6433 : : {
6434 : : /* Walk over elements and release vectors. */
6435 : 5634002 : vn_reference_iterator_type hir;
6436 : 5634002 : vn_reference_t vr;
6437 : 135812800 : FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
6438 : 65089399 : vr->operands.release ();
6439 : 5634002 : delete table->phis;
6440 : 5634002 : table->phis = NULL;
6441 : 5634002 : delete table->nary;
6442 : 5634002 : table->nary = NULL;
6443 : 5634002 : delete table->references;
6444 : 5634002 : table->references = NULL;
6445 : 5634002 : }
6446 : :
6447 : : /* Set *ID according to RESULT. */
6448 : :
6449 : : static void
6450 : 31908542 : set_value_id_for_result (tree result, unsigned int *id)
6451 : : {
6452 : 31908542 : if (result && TREE_CODE (result) == SSA_NAME)
6453 : 19931665 : *id = VN_INFO (result)->value_id;
6454 : 8927003 : else if (result && is_gimple_min_invariant (result))
6455 : 3306192 : *id = get_or_alloc_constant_value_id (result);
6456 : : else
6457 : 8670685 : *id = get_next_value_id ();
6458 : 31908542 : }
6459 : :
6460 : : /* Set the value ids in the valid hash tables. */
6461 : :
6462 : : static void
6463 : 914618 : set_hashtable_value_ids (void)
6464 : : {
6465 : 914618 : vn_nary_op_iterator_type hin;
6466 : 914618 : vn_phi_iterator_type hip;
6467 : 914618 : vn_reference_iterator_type hir;
6468 : 914618 : vn_nary_op_t vno;
6469 : 914618 : vn_reference_t vr;
6470 : 914618 : vn_phi_t vp;
6471 : :
6472 : : /* Now set the value ids of the things we had put in the hash
6473 : : table. */
6474 : :
6475 : 42145152 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
6476 : 20615267 : if (! vno->predicated_values)
6477 : 7118526 : set_value_id_for_result (vno->u.result, &vno->value_id);
6478 : :
6479 : 7982506 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
6480 : 3533944 : set_value_id_for_result (vp->result, &vp->value_id);
6481 : :
6482 : 43426762 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
6483 : : hir)
6484 : 21256072 : set_value_id_for_result (vr->result, &vr->value_id);
6485 : 914618 : }
6486 : :
6487 : : /* Return the maximum value id we have ever seen. */
6488 : :
6489 : : unsigned int
6490 : 1829236 : get_max_value_id (void)
6491 : : {
6492 : 1829236 : return next_value_id;
6493 : : }
6494 : :
6495 : : /* Return the maximum constant value id we have ever seen. */
6496 : :
6497 : : unsigned int
6498 : 1829236 : get_max_constant_value_id (void)
6499 : : {
6500 : 1829236 : return -next_constant_value_id;
6501 : : }
6502 : :
6503 : : /* Return the next unique value id. */
6504 : :
6505 : : unsigned int
6506 : 45229416 : get_next_value_id (void)
6507 : : {
6508 : 45229416 : gcc_checking_assert ((int)next_value_id > 0);
6509 : 45229416 : return next_value_id++;
6510 : : }
6511 : :
6512 : : /* Return the next unique value id for constants. */
6513 : :
6514 : : unsigned int
6515 : 2320225 : get_next_constant_value_id (void)
6516 : : {
6517 : 2320225 : gcc_checking_assert (next_constant_value_id < 0);
6518 : 2320225 : return next_constant_value_id--;
6519 : : }
6520 : :
6521 : :
6522 : : /* Compare two expressions E1 and E2 and return true if they are equal.
6523 : : If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6524 : : otherwise VN_TOP only matches VN_TOP. */
6525 : :
6526 : : bool
6527 : 202860415 : expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
6528 : : {
6529 : : /* The obvious case. */
6530 : 202860415 : if (e1 == e2)
6531 : : return true;
6532 : :
6533 : : /* If either one is VN_TOP consider them equal. */
6534 : 61323123 : if (match_vn_top_optimistically
6535 : 57224412 : && (e1 == VN_TOP || e2 == VN_TOP))
6536 : : return true;
6537 : :
6538 : : /* If only one of them is null, they cannot be equal. While in general
6539 : : this should not happen for operations like TARGET_MEM_REF some
6540 : : operands are optional and an identity value we could substitute
6541 : : has differing semantics. */
6542 : 61323123 : if (!e1 || !e2)
6543 : : return false;
6544 : :
6545 : : /* SSA_NAME compare pointer equal. */
6546 : 61323123 : if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
6547 : : return false;
6548 : :
6549 : : /* Now perform the actual comparison. */
6550 : 31093406 : if (TREE_CODE (e1) == TREE_CODE (e2)
6551 : 31093406 : && operand_equal_p (e1, e2, OEP_PURE_SAME))
6552 : : return true;
6553 : :
6554 : : return false;
6555 : : }
6556 : :
6557 : :
6558 : : /* Return true if the nary operation NARY may trap. This is a copy
6559 : : of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6560 : :
6561 : : bool
6562 : 3606455 : vn_nary_may_trap (vn_nary_op_t nary)
6563 : : {
6564 : 3606455 : tree type;
6565 : 3606455 : tree rhs2 = NULL_TREE;
6566 : 3606455 : bool honor_nans = false;
6567 : 3606455 : bool honor_snans = false;
6568 : 3606455 : bool fp_operation = false;
6569 : 3606455 : bool honor_trapv = false;
6570 : 3606455 : bool handled, ret;
6571 : 3606455 : unsigned i;
6572 : :
6573 : 3606455 : if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
6574 : : || TREE_CODE_CLASS (nary->opcode) == tcc_unary
6575 : 3606455 : || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
6576 : : {
6577 : 3533706 : type = nary->type;
6578 : 3533706 : fp_operation = FLOAT_TYPE_P (type);
6579 : 3533706 : if (fp_operation)
6580 : : {
6581 : 81587 : honor_nans = flag_trapping_math && !flag_finite_math_only;
6582 : 81587 : honor_snans = flag_signaling_nans != 0;
6583 : : }
6584 : 3452119 : else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
6585 : : honor_trapv = true;
6586 : : }
6587 : 3606455 : if (nary->length >= 2)
6588 : 1399781 : rhs2 = nary->op[1];
6589 : 3606455 : ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
6590 : : honor_trapv, honor_nans, honor_snans,
6591 : : rhs2, &handled);
6592 : 3606455 : if (handled && ret)
6593 : : return true;
6594 : :
6595 : 8414160 : for (i = 0; i < nary->length; ++i)
6596 : 4879752 : if (tree_could_trap_p (nary->op[i]))
6597 : : return true;
6598 : :
6599 : : return false;
6600 : : }
6601 : :
6602 : : /* Return true if the reference operation REF may trap. */
6603 : :
6604 : : bool
6605 : 1514798 : vn_reference_may_trap (vn_reference_t ref)
6606 : : {
6607 : 1514798 : switch (ref->operands[0].opcode)
6608 : : {
6609 : : case MODIFY_EXPR:
6610 : : case CALL_EXPR:
6611 : : /* We do not handle calls. */
6612 : : return true;
6613 : : case ADDR_EXPR:
6614 : : /* And toplevel address computations never trap. */
6615 : : return false;
6616 : : default:;
6617 : : }
6618 : :
6619 : : vn_reference_op_t op;
6620 : : unsigned i;
6621 : 4189219 : FOR_EACH_VEC_ELT (ref->operands, i, op)
6622 : : {
6623 : 4189071 : switch (op->opcode)
6624 : : {
6625 : : case WITH_SIZE_EXPR:
6626 : : case TARGET_MEM_REF:
6627 : : /* Always variable. */
6628 : : return true;
6629 : 1142159 : case COMPONENT_REF:
6630 : 1142159 : if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
6631 : : return true;
6632 : : break;
6633 : 0 : case ARRAY_RANGE_REF:
6634 : 0 : if (TREE_CODE (op->op0) == SSA_NAME)
6635 : : return true;
6636 : : break;
6637 : 136296 : case ARRAY_REF:
6638 : 136296 : {
6639 : 136296 : if (TREE_CODE (op->op0) != INTEGER_CST)
6640 : : return true;
6641 : :
6642 : : /* !in_array_bounds */
6643 : 120299 : tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
6644 : 120299 : if (!domain_type)
6645 : : return true;
6646 : :
6647 : 120170 : tree min = op->op1;
6648 : 120170 : tree max = TYPE_MAX_VALUE (domain_type);
6649 : 120170 : if (!min
6650 : 120170 : || !max
6651 : 111869 : || TREE_CODE (min) != INTEGER_CST
6652 : 111869 : || TREE_CODE (max) != INTEGER_CST)
6653 : : return true;
6654 : :
6655 : 110049 : if (tree_int_cst_lt (op->op0, min)
6656 : 110049 : || tree_int_cst_lt (max, op->op0))
6657 : 562 : return true;
6658 : :
6659 : : break;
6660 : : }
6661 : : case MEM_REF:
6662 : : /* Nothing interesting in itself, the base is separate. */
6663 : : break;
6664 : : /* The following are the address bases. */
6665 : : case SSA_NAME:
6666 : : return true;
6667 : 1055993 : case ADDR_EXPR:
6668 : 1055993 : if (op->op0)
6669 : 1055993 : return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
6670 : : return false;
6671 : 2718040 : default:;
6672 : : }
6673 : : }
6674 : : return false;
6675 : : }
6676 : :
6677 : 9591992 : eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
6678 : 9591992 : bitmap inserted_exprs_)
6679 : 9591992 : : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
6680 : 9591992 : el_todo (0), eliminations (0), insertions (0),
6681 : 9591992 : inserted_exprs (inserted_exprs_)
6682 : : {
6683 : 9591992 : need_eh_cleanup = BITMAP_ALLOC (NULL);
6684 : 9591992 : need_ab_cleanup = BITMAP_ALLOC (NULL);
6685 : 9591992 : }
6686 : :
6687 : 9591992 : eliminate_dom_walker::~eliminate_dom_walker ()
6688 : : {
6689 : 9591992 : BITMAP_FREE (need_eh_cleanup);
6690 : 9591992 : BITMAP_FREE (need_ab_cleanup);
6691 : 9591992 : }
6692 : :
6693 : : /* Return a leader for OP that is available at the current point of the
6694 : : eliminate domwalk. */
6695 : :
6696 : : tree
6697 : 161933746 : eliminate_dom_walker::eliminate_avail (basic_block, tree op)
6698 : : {
6699 : 161933746 : tree valnum = VN_INFO (op)->valnum;
6700 : 161933746 : if (TREE_CODE (valnum) == SSA_NAME)
6701 : : {
6702 : 157603478 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6703 : : return valnum;
6704 : 273845505 : if (avail.length () > SSA_NAME_VERSION (valnum))
6705 : : {
6706 : 122867428 : tree av = avail[SSA_NAME_VERSION (valnum)];
6707 : : /* When PRE discovers a new redundancy there's no way to unite
6708 : : the value classes so it instead inserts a copy old-val = new-val.
6709 : : Look through such copies here, providing one more level of
6710 : : simplification at elimination time. */
6711 : 122867428 : gassign *ass;
6712 : 215295479 : if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
6713 : 65240729 : if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
6714 : : {
6715 : 34801159 : tree rhs1 = gimple_assign_rhs1 (ass);
6716 : 34801159 : if (CONSTANT_CLASS_P (rhs1)
6717 : 34801159 : || (TREE_CODE (rhs1) == SSA_NAME
6718 : 25402 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6719 : : av = rhs1;
6720 : : }
6721 : 122867428 : return av;
6722 : : }
6723 : : }
6724 : 4330268 : else if (is_gimple_min_invariant (valnum))
6725 : : return valnum;
6726 : : return NULL_TREE;
6727 : : }
6728 : :
6729 : : /* At the current point of the eliminate domwalk make OP available. */
6730 : :
6731 : : void
6732 : 45895833 : eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
6733 : : {
6734 : 45895833 : tree valnum = VN_INFO (op)->valnum;
6735 : 45895833 : if (TREE_CODE (valnum) == SSA_NAME)
6736 : : {
6737 : 88683777 : if (avail.length () <= SSA_NAME_VERSION (valnum))
6738 : 15726000 : avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
6739 : 45895833 : tree pushop = op;
6740 : 45895833 : if (avail[SSA_NAME_VERSION (valnum)])
6741 : 35798 : pushop = avail[SSA_NAME_VERSION (valnum)];
6742 : 45895833 : avail_stack.safe_push (pushop);
6743 : 45895833 : avail[SSA_NAME_VERSION (valnum)] = op;
6744 : : }
6745 : 45895833 : }
6746 : :
6747 : : /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
6748 : : the leader for the expression if insertion was successful. */
6749 : :
6750 : : tree
6751 : 143765 : eliminate_dom_walker::eliminate_insert (basic_block bb,
6752 : : gimple_stmt_iterator *gsi, tree val)
6753 : : {
6754 : : /* We can insert a sequence with a single assignment only. */
6755 : 143765 : gimple_seq stmts = VN_INFO (val)->expr;
6756 : 143765 : if (!gimple_seq_singleton_p (stmts))
6757 : : return NULL_TREE;
6758 : 267305 : gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
6759 : 143765 : if (!stmt
6760 : 143765 : || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
6761 : : && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
6762 : : && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
6763 : : && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
6764 : : && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6765 : 7 : || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
6766 : : return NULL_TREE;
6767 : :
6768 : 32297 : tree op = gimple_assign_rhs1 (stmt);
6769 : 32297 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
6770 : 32297 : || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6771 : 18236 : op = TREE_OPERAND (op, 0);
6772 : 32297 : tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
6773 : 32255 : if (!leader)
6774 : : return NULL_TREE;
6775 : :
6776 : 20228 : tree res;
6777 : 20228 : stmts = NULL;
6778 : 36915 : if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6779 : 30882 : res = gimple_build (&stmts, BIT_FIELD_REF,
6780 : 15441 : TREE_TYPE (val), leader,
6781 : 15441 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
6782 : 15441 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
6783 : 4787 : else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
6784 : 14 : res = gimple_build (&stmts, BIT_AND_EXPR,
6785 : 7 : TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
6786 : : else
6787 : 4780 : res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
6788 : 4780 : TREE_TYPE (val), leader);
6789 : 20228 : if (TREE_CODE (res) != SSA_NAME
6790 : 20228 : || SSA_NAME_IS_DEFAULT_DEF (res)
6791 : 40456 : || gimple_bb (SSA_NAME_DEF_STMT (res)))
6792 : : {
6793 : 3 : gimple_seq_discard (stmts);
6794 : :
6795 : : /* During propagation we have to treat SSA info conservatively
6796 : : and thus we can end up simplifying the inserted expression
6797 : : at elimination time to sth not defined in stmts. */
6798 : : /* But then this is a redundancy we failed to detect. Which means
6799 : : res now has two values. That doesn't play well with how
6800 : : we track availability here, so give up. */
6801 : 3 : if (dump_file && (dump_flags & TDF_DETAILS))
6802 : : {
6803 : 0 : if (TREE_CODE (res) == SSA_NAME)
6804 : 0 : res = eliminate_avail (bb, res);
6805 : 0 : if (res)
6806 : : {
6807 : 0 : fprintf (dump_file, "Failed to insert expression for value ");
6808 : 0 : print_generic_expr (dump_file, val);
6809 : 0 : fprintf (dump_file, " which is really fully redundant to ");
6810 : 0 : print_generic_expr (dump_file, res);
6811 : 0 : fprintf (dump_file, "\n");
6812 : : }
6813 : : }
6814 : :
6815 : 3 : return NULL_TREE;
6816 : : }
6817 : : else
6818 : : {
6819 : 20225 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6820 : 20225 : vn_ssa_aux_t vn_info = VN_INFO (res);
6821 : 20225 : vn_info->valnum = val;
6822 : 20225 : vn_info->visited = true;
6823 : : }
6824 : :
6825 : 20225 : insertions++;
6826 : 20225 : if (dump_file && (dump_flags & TDF_DETAILS))
6827 : : {
6828 : 537 : fprintf (dump_file, "Inserted ");
6829 : 537 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
6830 : : }
6831 : :
6832 : : return res;
6833 : : }
6834 : :
6835 : : void
6836 : 288335839 : eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
6837 : : {
6838 : 288335839 : tree sprime = NULL_TREE;
6839 : 288335839 : gimple *stmt = gsi_stmt (*gsi);
6840 : 288335839 : tree lhs = gimple_get_lhs (stmt);
6841 : 106602242 : if (lhs && TREE_CODE (lhs) == SSA_NAME
6842 : 146878862 : && !gimple_has_volatile_ops (stmt)
6843 : : /* See PR43491. Do not replace a global register variable when
6844 : : it is a the RHS of an assignment. Do replace local register
6845 : : variables since gcc does not guarantee a local variable will
6846 : : be allocated in register.
6847 : : ??? The fix isn't effective here. This should instead
6848 : : be ensured by not value-numbering them the same but treating
6849 : : them like volatiles? */
6850 : 360705071 : && !(gimple_assign_single_p (stmt)
6851 : 32075854 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
6852 : 2272236 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
6853 : 4169 : && is_global_var (gimple_assign_rhs1 (stmt)))))
6854 : : {
6855 : 72368958 : sprime = eliminate_avail (b, lhs);
6856 : 72368958 : if (!sprime)
6857 : : {
6858 : : /* If there is no existing usable leader but SCCVN thinks
6859 : : it has an expression it wants to use as replacement,
6860 : : insert that. */
6861 : 61076183 : tree val = VN_INFO (lhs)->valnum;
6862 : 61076183 : vn_ssa_aux_t vn_info;
6863 : 61076183 : if (val != VN_TOP
6864 : 61076183 : && TREE_CODE (val) == SSA_NAME
6865 : 61076183 : && (vn_info = VN_INFO (val), true)
6866 : 61076183 : && vn_info->needs_insertion
6867 : 326130 : && vn_info->expr != NULL
6868 : 61219948 : && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
6869 : 20225 : eliminate_push_avail (b, sprime);
6870 : : }
6871 : :
6872 : : /* If this now constitutes a copy duplicate points-to
6873 : : and range info appropriately. This is especially
6874 : : important for inserted code. See tree-ssa-copy.cc
6875 : : for similar code. */
6876 : 61076183 : if (sprime
6877 : 11313000 : && TREE_CODE (sprime) == SSA_NAME)
6878 : : {
6879 : 7944713 : basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
6880 : 12732612 : if (POINTER_TYPE_P (TREE_TYPE (lhs))
6881 : 3238621 : && SSA_NAME_PTR_INFO (lhs)
6882 : 11127303 : && ! SSA_NAME_PTR_INFO (sprime))
6883 : : {
6884 : 145727 : duplicate_ssa_name_ptr_info (sprime,
6885 : 145727 : SSA_NAME_PTR_INFO (lhs));
6886 : 145727 : if (b != sprime_b)
6887 : 56358 : reset_flow_sensitive_info (sprime);
6888 : : }
6889 : 15569318 : else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6890 : 4470075 : && SSA_NAME_RANGE_INFO (lhs)
6891 : 548028 : && ! SSA_NAME_RANGE_INFO (sprime)
6892 : 7977009 : && b == sprime_b)
6893 : 62530 : duplicate_ssa_name_range_info (sprime, lhs);
6894 : : }
6895 : :
6896 : : /* Inhibit the use of an inserted PHI on a loop header when
6897 : : the address of the memory reference is a simple induction
6898 : : variable. In other cases the vectorizer won't do anything
6899 : : anyway (either it's loop invariant or a complicated
6900 : : expression). */
6901 : : if (sprime
6902 : 11313000 : && TREE_CODE (sprime) == SSA_NAME
6903 : 7944713 : && do_pre
6904 : 813581 : && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
6905 : 796251 : && loop_outer (b->loop_father)
6906 : 328553 : && has_zero_uses (sprime)
6907 : 163901 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
6908 : 163750 : && gimple_assign_load_p (stmt))
6909 : : {
6910 : 84199 : gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
6911 : 84199 : basic_block def_bb = gimple_bb (def_stmt);
6912 : 84199 : if (gimple_code (def_stmt) == GIMPLE_PHI
6913 : 84199 : && def_bb->loop_father->header == def_bb)
6914 : : {
6915 : 51616 : loop_p loop = def_bb->loop_father;
6916 : 51616 : ssa_op_iter iter;
6917 : 51616 : tree op;
6918 : 51616 : bool found = false;
6919 : 63670 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6920 : : {
6921 : 47796 : affine_iv iv;
6922 : 47796 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
6923 : 47796 : if (def_bb
6924 : 43728 : && flow_bb_inside_loop_p (loop, def_bb)
6925 : 88350 : && simple_iv (loop, loop, op, &iv, true))
6926 : : {
6927 : 35742 : found = true;
6928 : 35742 : break;
6929 : : }
6930 : : }
6931 : 15874 : if (found)
6932 : : {
6933 : 35742 : if (dump_file && (dump_flags & TDF_DETAILS))
6934 : : {
6935 : 5 : fprintf (dump_file, "Not replacing ");
6936 : 5 : print_gimple_expr (dump_file, stmt, 0);
6937 : 5 : fprintf (dump_file, " with ");
6938 : 5 : print_generic_expr (dump_file, sprime);
6939 : 5 : fprintf (dump_file, " which would add a loop"
6940 : : " carried dependence to loop %d\n",
6941 : : loop->num);
6942 : : }
6943 : : /* Don't keep sprime available. */
6944 : 35742 : sprime = NULL_TREE;
6945 : : }
6946 : : }
6947 : : }
6948 : :
6949 : 72368958 : if (sprime)
6950 : : {
6951 : : /* If we can propagate the value computed for LHS into
6952 : : all uses don't bother doing anything with this stmt. */
6953 : 11277258 : if (may_propagate_copy (lhs, sprime))
6954 : : {
6955 : : /* Mark it for removal. */
6956 : 11275407 : to_remove.safe_push (stmt);
6957 : :
6958 : : /* ??? Don't count copy/constant propagations. */
6959 : 11275407 : if (gimple_assign_single_p (stmt)
6960 : 11275407 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
6961 : 3744756 : || gimple_assign_rhs1 (stmt) == sprime))
6962 : 11822332 : return;
6963 : :
6964 : 6153843 : if (dump_file && (dump_flags & TDF_DETAILS))
6965 : : {
6966 : 18234 : fprintf (dump_file, "Replaced ");
6967 : 18234 : print_gimple_expr (dump_file, stmt, 0);
6968 : 18234 : fprintf (dump_file, " with ");
6969 : 18234 : print_generic_expr (dump_file, sprime);
6970 : 18234 : fprintf (dump_file, " in all uses of ");
6971 : 18234 : print_gimple_stmt (dump_file, stmt, 0);
6972 : : }
6973 : :
6974 : 6153843 : eliminations++;
6975 : 6153843 : return;
6976 : : }
6977 : :
6978 : : /* If this is an assignment from our leader (which
6979 : : happens in the case the value-number is a constant)
6980 : : then there is nothing to do. Likewise if we run into
6981 : : inserted code that needed a conversion because of
6982 : : our type-agnostic value-numbering of loads. */
6983 : 1851 : if ((gimple_assign_single_p (stmt)
6984 : 1 : || (is_gimple_assign (stmt)
6985 : 1 : && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
6986 : 0 : || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
6987 : 1852 : && sprime == gimple_assign_rhs1 (stmt))
6988 : : return;
6989 : :
6990 : : /* Else replace its RHS. */
6991 : 691 : if (dump_file && (dump_flags & TDF_DETAILS))
6992 : : {
6993 : 0 : fprintf (dump_file, "Replaced ");
6994 : 0 : print_gimple_expr (dump_file, stmt, 0);
6995 : 0 : fprintf (dump_file, " with ");
6996 : 0 : print_generic_expr (dump_file, sprime);
6997 : 0 : fprintf (dump_file, " in ");
6998 : 0 : print_gimple_stmt (dump_file, stmt, 0);
6999 : : }
7000 : 691 : eliminations++;
7001 : :
7002 : 691 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
7003 : 691 : && stmt_can_make_abnormal_goto (stmt));
7004 : 691 : gimple *orig_stmt = stmt;
7005 : 691 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
7006 : 691 : TREE_TYPE (sprime)))
7007 : : {
7008 : : /* We preserve conversions to but not from function or method
7009 : : types. This asymmetry makes it necessary to re-instantiate
7010 : : conversions here. */
7011 : 689 : if (POINTER_TYPE_P (TREE_TYPE (lhs))
7012 : 689 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7013 : 689 : sprime = fold_convert (TREE_TYPE (lhs), sprime);
7014 : : else
7015 : 0 : gcc_unreachable ();
7016 : : }
7017 : 691 : tree vdef = gimple_vdef (stmt);
7018 : 691 : tree vuse = gimple_vuse (stmt);
7019 : 691 : propagate_tree_value_into_stmt (gsi, sprime);
7020 : 691 : stmt = gsi_stmt (*gsi);
7021 : 691 : update_stmt (stmt);
7022 : : /* In case the VDEF on the original stmt was released, value-number
7023 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7024 : : released virtual operands. */
7025 : 1382 : if (vdef != gimple_vdef (stmt))
7026 : : {
7027 : 0 : gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7028 : 0 : VN_INFO (vdef)->valnum = vuse;
7029 : : }
7030 : :
7031 : : /* If we removed EH side-effects from the statement, clean
7032 : : its EH information. */
7033 : 691 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7034 : : {
7035 : 0 : bitmap_set_bit (need_eh_cleanup,
7036 : 0 : gimple_bb (stmt)->index);
7037 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7038 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7039 : : }
7040 : :
7041 : : /* Likewise for AB side-effects. */
7042 : 691 : if (can_make_abnormal_goto
7043 : 691 : && !stmt_can_make_abnormal_goto (stmt))
7044 : : {
7045 : 0 : bitmap_set_bit (need_ab_cleanup,
7046 : 0 : gimple_bb (stmt)->index);
7047 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7048 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7049 : : }
7050 : :
7051 : 691 : return;
7052 : : }
7053 : : }
7054 : :
7055 : : /* If the statement is a scalar store, see if the expression
7056 : : has the same value number as its rhs. If so, the store is
7057 : : dead. */
7058 : 277058581 : if (gimple_assign_single_p (stmt)
7059 : 114335804 : && !gimple_has_volatile_ops (stmt)
7060 : 49349679 : && !is_gimple_reg (gimple_assign_lhs (stmt))
7061 : 302165039 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7062 : 14583787 : || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7063 : : {
7064 : 22110382 : tree rhs = gimple_assign_rhs1 (stmt);
7065 : 22110382 : vn_reference_t vnresult;
7066 : : /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7067 : : typed load of a byte known to be 0x11 as 1 so a store of
7068 : : a boolean 1 is detected as redundant. Because of this we
7069 : : have to make sure to lookup with a ref where its size
7070 : : matches the precision. */
7071 : 22110382 : tree lookup_lhs = lhs;
7072 : 43967016 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7073 : 11399626 : && (TREE_CODE (lhs) != COMPONENT_REF
7074 : 7385717 : || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7075 : 33362464 : && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7076 : : {
7077 : 403599 : if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
7078 : 412542 : && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7079 : : lookup_lhs = NULL_TREE;
7080 : 396806 : else if (TREE_CODE (lhs) == COMPONENT_REF
7081 : 396806 : || TREE_CODE (lhs) == MEM_REF)
7082 : : {
7083 : 283128 : tree ltype = build_nonstandard_integer_type
7084 : 283128 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7085 : 283128 : TYPE_UNSIGNED (TREE_TYPE (lhs)));
7086 : 283128 : if (TREE_CODE (lhs) == COMPONENT_REF)
7087 : : {
7088 : 217491 : tree foff = component_ref_field_offset (lhs);
7089 : 217491 : tree f = TREE_OPERAND (lhs, 1);
7090 : 217491 : if (!poly_int_tree_p (foff))
7091 : : lookup_lhs = NULL_TREE;
7092 : : else
7093 : 434982 : lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7094 : 217491 : TREE_OPERAND (lhs, 0),
7095 : 217491 : TYPE_SIZE (TREE_TYPE (lhs)),
7096 : : bit_from_pos
7097 : 217491 : (foff, DECL_FIELD_BIT_OFFSET (f)));
7098 : : }
7099 : : else
7100 : 65637 : lookup_lhs = build2 (MEM_REF, ltype,
7101 : 65637 : TREE_OPERAND (lhs, 0),
7102 : 65637 : TREE_OPERAND (lhs, 1));
7103 : : }
7104 : : else
7105 : : lookup_lhs = NULL_TREE;
7106 : : }
7107 : 21989911 : tree val = NULL_TREE;
7108 : 21989911 : if (lookup_lhs)
7109 : 43979822 : val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7110 : : VN_WALKREWRITE, &vnresult, false,
7111 : : NULL, NULL_TREE, true);
7112 : 22110382 : if (TREE_CODE (rhs) == SSA_NAME)
7113 : 10522671 : rhs = VN_INFO (rhs)->valnum;
7114 : 22110382 : if (val
7115 : 22110382 : && (operand_equal_p (val, rhs, 0)
7116 : : /* Due to the bitfield lookups above we can get bit
7117 : : interpretations of the same RHS as values here. Those
7118 : : are redundant as well. */
7119 : 2476832 : || (TREE_CODE (val) == SSA_NAME
7120 : 1549315 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7121 : 1401070 : && (val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7122 : 1401070 : && TREE_CODE (val) == VIEW_CONVERT_EXPR
7123 : 2896 : && TREE_OPERAND (val, 0) == rhs)))
7124 : : {
7125 : : /* We can only remove the later store if the former aliases
7126 : : at least all accesses the later one does or if the store
7127 : : was to readonly memory storing the same value. */
7128 : 179305 : ao_ref lhs_ref;
7129 : 179305 : ao_ref_init (&lhs_ref, lhs);
7130 : 179305 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
7131 : 179305 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7132 : 179305 : if (! vnresult
7133 : 179305 : || ((vnresult->set == set
7134 : 32538 : || alias_set_subset_of (set, vnresult->set))
7135 : 177475 : && (vnresult->base_set == base_set
7136 : 9303 : || alias_set_subset_of (base_set, vnresult->base_set))))
7137 : : {
7138 : 173884 : if (dump_file && (dump_flags & TDF_DETAILS))
7139 : : {
7140 : 16 : fprintf (dump_file, "Deleted redundant store ");
7141 : 16 : print_gimple_stmt (dump_file, stmt, 0);
7142 : : }
7143 : :
7144 : : /* Queue stmt for removal. */
7145 : 173884 : to_remove.safe_push (stmt);
7146 : 173884 : return;
7147 : : }
7148 : : }
7149 : : }
7150 : :
7151 : : /* If this is a control statement value numbering left edges
7152 : : unexecuted on force the condition in a way consistent with
7153 : : that. */
7154 : 276884697 : if (gcond *cond = dyn_cast <gcond *> (stmt))
7155 : : {
7156 : 16059631 : if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7157 : 16059631 : ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7158 : : {
7159 : 371190 : if (dump_file && (dump_flags & TDF_DETAILS))
7160 : : {
7161 : 18 : fprintf (dump_file, "Removing unexecutable edge from ");
7162 : 18 : print_gimple_stmt (dump_file, stmt, 0);
7163 : : }
7164 : 371190 : if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7165 : 371190 : == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7166 : 120245 : gimple_cond_make_true (cond);
7167 : : else
7168 : 250945 : gimple_cond_make_false (cond);
7169 : 371190 : update_stmt (cond);
7170 : 371190 : el_todo |= TODO_cleanup_cfg;
7171 : 371190 : return;
7172 : : }
7173 : : }
7174 : :
7175 : 276513507 : bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7176 : 276513507 : bool was_noreturn = (is_gimple_call (stmt)
7177 : 276513507 : && gimple_call_noreturn_p (stmt));
7178 : 276513507 : tree vdef = gimple_vdef (stmt);
7179 : 276513507 : tree vuse = gimple_vuse (stmt);
7180 : :
7181 : : /* If we didn't replace the whole stmt (or propagate the result
7182 : : into all uses), replace all uses on this stmt with their
7183 : : leaders. */
7184 : 276513507 : bool modified = false;
7185 : 276513507 : use_operand_p use_p;
7186 : 276513507 : ssa_op_iter iter;
7187 : 417899153 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7188 : : {
7189 : 141385646 : tree use = USE_FROM_PTR (use_p);
7190 : : /* ??? The call code above leaves stmt operands un-updated. */
7191 : 141385646 : if (TREE_CODE (use) != SSA_NAME)
7192 : 0 : continue;
7193 : 141385646 : tree sprime;
7194 : 141385646 : if (SSA_NAME_IS_DEFAULT_DEF (use))
7195 : : /* ??? For default defs BB shouldn't matter, but we have to
7196 : : solve the inconsistency between rpo eliminate and
7197 : : dom eliminate avail valueization first. */
7198 : 23770809 : sprime = eliminate_avail (b, use);
7199 : : else
7200 : : /* Look for sth available at the definition block of the argument.
7201 : : This avoids inconsistencies between availability there which
7202 : : decides if the stmt can be removed and availability at the
7203 : : use site. The SSA property ensures that things available
7204 : : at the definition are also available at uses. */
7205 : 117614837 : sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7206 : 141385646 : if (sprime && sprime != use
7207 : 10209818 : && may_propagate_copy (use, sprime, true)
7208 : : /* We substitute into debug stmts to avoid excessive
7209 : : debug temporaries created by removed stmts, but we need
7210 : : to avoid doing so for inserted sprimes as we never want
7211 : : to create debug temporaries for them. */
7212 : 151594775 : && (!inserted_exprs
7213 : 969782 : || TREE_CODE (sprime) != SSA_NAME
7214 : 958452 : || !is_gimple_debug (stmt)
7215 : 232021 : || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7216 : : {
7217 : 9990656 : propagate_value (use_p, sprime);
7218 : 9990656 : modified = true;
7219 : : }
7220 : : }
7221 : :
7222 : : /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7223 : : into which is a requirement for the IPA devirt machinery. */
7224 : 276513507 : gimple *old_stmt = stmt;
7225 : 276513507 : if (modified)
7226 : : {
7227 : : /* If a formerly non-invariant ADDR_EXPR is turned into an
7228 : : invariant one it was on a separate stmt. */
7229 : 9232390 : if (gimple_assign_single_p (stmt)
7230 : 9232390 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7231 : 202123 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7232 : 9232390 : gimple_stmt_iterator prev = *gsi;
7233 : 9232390 : gsi_prev (&prev);
7234 : 9232390 : if (fold_stmt (gsi, follow_all_ssa_edges))
7235 : : {
7236 : : /* fold_stmt may have created new stmts inbetween
7237 : : the previous stmt and the folded stmt. Mark
7238 : : all defs created there as varying to not confuse
7239 : : the SCCVN machinery as we're using that even during
7240 : : elimination. */
7241 : 813849 : if (gsi_end_p (prev))
7242 : 184074 : prev = gsi_start_bb (b);
7243 : : else
7244 : 721812 : gsi_next (&prev);
7245 : 813849 : if (gsi_stmt (prev) != gsi_stmt (*gsi))
7246 : 106362 : do
7247 : : {
7248 : 66437 : tree def;
7249 : 66437 : ssa_op_iter dit;
7250 : 129110 : FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7251 : : dit, SSA_OP_ALL_DEFS)
7252 : : /* As existing DEFs may move between stmts
7253 : : only process new ones. */
7254 : 62673 : if (! has_VN_INFO (def))
7255 : : {
7256 : 39823 : vn_ssa_aux_t vn_info = VN_INFO (def);
7257 : 39823 : vn_info->valnum = def;
7258 : 39823 : vn_info->visited = true;
7259 : : }
7260 : 66437 : if (gsi_stmt (prev) == gsi_stmt (*gsi))
7261 : : break;
7262 : 39925 : gsi_next (&prev);
7263 : 39925 : }
7264 : : while (1);
7265 : : }
7266 : 9232390 : stmt = gsi_stmt (*gsi);
7267 : : /* In case we folded the stmt away schedule the NOP for removal. */
7268 : 9232390 : if (gimple_nop_p (stmt))
7269 : 457 : to_remove.safe_push (stmt);
7270 : : }
7271 : :
7272 : : /* Visit indirect calls and turn them into direct calls if
7273 : : possible using the devirtualization machinery. Do this before
7274 : : checking for required EH/abnormal/noreturn cleanup as devird
7275 : : may expose more of those. */
7276 : 276513507 : if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7277 : : {
7278 : 20224088 : tree fn = gimple_call_fn (call_stmt);
7279 : 20224088 : if (fn
7280 : 19727159 : && flag_devirtualize
7281 : 39270695 : && virtual_method_call_p (fn))
7282 : : {
7283 : 179272 : tree otr_type = obj_type_ref_class (fn);
7284 : 179272 : unsigned HOST_WIDE_INT otr_tok
7285 : 179272 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7286 : 179272 : tree instance;
7287 : 179272 : ipa_polymorphic_call_context context (current_function_decl,
7288 : 179272 : fn, stmt, &instance);
7289 : 179272 : context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7290 : : otr_type, stmt, NULL);
7291 : 179272 : bool final;
7292 : 179272 : vec <cgraph_node *> targets
7293 : 179272 : = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7294 : : otr_tok, context, &final);
7295 : 179272 : if (dump_file)
7296 : 27 : dump_possible_polymorphic_call_targets (dump_file,
7297 : : obj_type_ref_class (fn),
7298 : : otr_tok, context);
7299 : 179551 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
7300 : : {
7301 : 59 : tree fn;
7302 : 59 : if (targets.length () == 1)
7303 : 59 : fn = targets[0]->decl;
7304 : : else
7305 : 0 : fn = builtin_decl_unreachable ();
7306 : 59 : if (dump_enabled_p ())
7307 : : {
7308 : 12 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7309 : : "converting indirect call to "
7310 : : "function %s\n",
7311 : 12 : lang_hooks.decl_printable_name (fn, 2));
7312 : : }
7313 : 59 : gimple_call_set_fndecl (call_stmt, fn);
7314 : : /* If changing the call to __builtin_unreachable
7315 : : or similar noreturn function, adjust gimple_call_fntype
7316 : : too. */
7317 : 59 : if (gimple_call_noreturn_p (call_stmt)
7318 : 0 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7319 : 0 : && TYPE_ARG_TYPES (TREE_TYPE (fn))
7320 : 59 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7321 : 0 : == void_type_node))
7322 : 0 : gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7323 : 59 : maybe_remove_unused_call_args (cfun, call_stmt);
7324 : 59 : modified = true;
7325 : : }
7326 : : }
7327 : : }
7328 : :
7329 : 276513507 : if (modified)
7330 : : {
7331 : : /* When changing a call into a noreturn call, cfg cleanup
7332 : : is needed to fix up the noreturn call. */
7333 : 9232414 : if (!was_noreturn
7334 : 9232414 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7335 : 64 : to_fixup.safe_push (stmt);
7336 : : /* When changing a condition or switch into one we know what
7337 : : edge will be executed, schedule a cfg cleanup. */
7338 : 9232414 : if ((gimple_code (stmt) == GIMPLE_COND
7339 : 1196233 : && (gimple_cond_true_p (as_a <gcond *> (stmt))
7340 : 1192429 : || gimple_cond_false_p (as_a <gcond *> (stmt))))
7341 : 10422985 : || (gimple_code (stmt) == GIMPLE_SWITCH
7342 : 7867 : && TREE_CODE (gimple_switch_index
7343 : : (as_a <gswitch *> (stmt))) == INTEGER_CST))
7344 : 7492 : el_todo |= TODO_cleanup_cfg;
7345 : : /* If we removed EH side-effects from the statement, clean
7346 : : its EH information. */
7347 : 9232414 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7348 : : {
7349 : 1761 : bitmap_set_bit (need_eh_cleanup,
7350 : 1761 : gimple_bb (stmt)->index);
7351 : 1761 : if (dump_file && (dump_flags & TDF_DETAILS))
7352 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7353 : : }
7354 : : /* Likewise for AB side-effects. */
7355 : 9232414 : if (can_make_abnormal_goto
7356 : 9232414 : && !stmt_can_make_abnormal_goto (stmt))
7357 : : {
7358 : 0 : bitmap_set_bit (need_ab_cleanup,
7359 : 0 : gimple_bb (stmt)->index);
7360 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7361 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7362 : : }
7363 : 9232414 : update_stmt (stmt);
7364 : : /* In case the VDEF on the original stmt was released, value-number
7365 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7366 : : released virtual operands. */
7367 : 11981864 : if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7368 : 1159 : VN_INFO (vdef)->valnum = vuse;
7369 : : }
7370 : :
7371 : : /* Make new values available - for fully redundant LHS we
7372 : : continue with the next stmt above and skip this.
7373 : : But avoid picking up dead defs. */
7374 : 276513507 : tree def;
7375 : 339011518 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7376 : 62498011 : if (! has_zero_uses (def)
7377 : 62498011 : || (inserted_exprs
7378 : 214194 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7379 : 61133790 : eliminate_push_avail (b, def);
7380 : : }
7381 : :
7382 : : /* Perform elimination for the basic-block B during the domwalk. */
7383 : :
7384 : : edge
7385 : 36985010 : eliminate_dom_walker::before_dom_children (basic_block b)
7386 : : {
7387 : : /* Mark new bb. */
7388 : 36985010 : avail_stack.safe_push (NULL_TREE);
7389 : :
7390 : : /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7391 : 36985010 : if (!(b->flags & BB_EXECUTABLE))
7392 : : return NULL;
7393 : :
7394 : 32727036 : vn_context_bb = b;
7395 : :
7396 : 43315575 : for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7397 : : {
7398 : 10588539 : gphi *phi = gsi.phi ();
7399 : 10588539 : tree res = PHI_RESULT (phi);
7400 : :
7401 : 21177078 : if (virtual_operand_p (res))
7402 : : {
7403 : 5021627 : gsi_next (&gsi);
7404 : 5021627 : continue;
7405 : : }
7406 : :
7407 : 5566912 : tree sprime = eliminate_avail (b, res);
7408 : 5566912 : if (sprime
7409 : 5566912 : && sprime != res)
7410 : : {
7411 : 445665 : if (dump_file && (dump_flags & TDF_DETAILS))
7412 : : {
7413 : 28 : fprintf (dump_file, "Replaced redundant PHI node defining ");
7414 : 28 : print_generic_expr (dump_file, res);
7415 : 28 : fprintf (dump_file, " with ");
7416 : 28 : print_generic_expr (dump_file, sprime);
7417 : 28 : fprintf (dump_file, "\n");
7418 : : }
7419 : :
7420 : : /* If we inserted this PHI node ourself, it's not an elimination. */
7421 : 445665 : if (! inserted_exprs
7422 : 545846 : || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
7423 : 421000 : eliminations++;
7424 : :
7425 : : /* If we will propagate into all uses don't bother to do
7426 : : anything. */
7427 : 445665 : if (may_propagate_copy (res, sprime))
7428 : : {
7429 : : /* Mark the PHI for removal. */
7430 : 445665 : to_remove.safe_push (phi);
7431 : 445665 : gsi_next (&gsi);
7432 : 445665 : continue;
7433 : : }
7434 : :
7435 : 0 : remove_phi_node (&gsi, false);
7436 : :
7437 : 0 : if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
7438 : 0 : sprime = fold_convert (TREE_TYPE (res), sprime);
7439 : 0 : gimple *stmt = gimple_build_assign (res, sprime);
7440 : 0 : gimple_stmt_iterator gsi2 = gsi_after_labels (b);
7441 : 0 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
7442 : 0 : continue;
7443 : 0 : }
7444 : :
7445 : 5121247 : eliminate_push_avail (b, res);
7446 : 5121247 : gsi_next (&gsi);
7447 : : }
7448 : :
7449 : 65454072 : for (gimple_stmt_iterator gsi = gsi_start_bb (b);
7450 : 237737037 : !gsi_end_p (gsi);
7451 : 205010001 : gsi_next (&gsi))
7452 : 205010001 : eliminate_stmt (b, &gsi);
7453 : :
7454 : : /* Replace destination PHI arguments. */
7455 : 32727036 : edge_iterator ei;
7456 : 32727036 : edge e;
7457 : 77416733 : FOR_EACH_EDGE (e, ei, b->succs)
7458 : 44689697 : if (e->flags & EDGE_EXECUTABLE)
7459 : 44359474 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
7460 : 71297383 : !gsi_end_p (gsi);
7461 : 26937909 : gsi_next (&gsi))
7462 : : {
7463 : 26937909 : gphi *phi = gsi.phi ();
7464 : 26937909 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7465 : 26937909 : tree arg = USE_FROM_PTR (use_p);
7466 : 44813260 : if (TREE_CODE (arg) != SSA_NAME
7467 : 26937909 : || virtual_operand_p (arg))
7468 : 17875351 : continue;
7469 : 9062558 : tree sprime = eliminate_avail (b, arg);
7470 : 18125116 : if (sprime && may_propagate_copy (arg, sprime,
7471 : 9062558 : !(e->flags & EDGE_ABNORMAL)))
7472 : 9053153 : propagate_value (use_p, sprime);
7473 : : }
7474 : :
7475 : 32727036 : vn_context_bb = NULL;
7476 : :
7477 : 32727036 : return NULL;
7478 : : }
7479 : :
7480 : : /* Make no longer available leaders no longer available. */
7481 : :
7482 : : void
7483 : 36985010 : eliminate_dom_walker::after_dom_children (basic_block)
7484 : : {
7485 : 36985010 : tree entry;
7486 : 82880843 : while ((entry = avail_stack.pop ()) != NULL_TREE)
7487 : : {
7488 : 45895833 : tree valnum = VN_INFO (entry)->valnum;
7489 : 45895833 : tree old = avail[SSA_NAME_VERSION (valnum)];
7490 : 45895833 : if (old == entry)
7491 : 45860035 : avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
7492 : : else
7493 : 35798 : avail[SSA_NAME_VERSION (valnum)] = entry;
7494 : : }
7495 : 36985010 : }
7496 : :
7497 : : /* Remove queued stmts and perform delayed cleanups. */
7498 : :
7499 : : unsigned
7500 : 5614721 : eliminate_dom_walker::eliminate_cleanup (bool region_p)
7501 : : {
7502 : 5614721 : statistics_counter_event (cfun, "Eliminated", eliminations);
7503 : 5614721 : statistics_counter_event (cfun, "Insertions", insertions);
7504 : :
7505 : : /* We cannot remove stmts during BB walk, especially not release SSA
7506 : : names there as this confuses the VN machinery. The stmts ending
7507 : : up in to_remove are either stores or simple copies.
7508 : : Remove stmts in reverse order to make debug stmt creation possible. */
7509 : 29744620 : while (!to_remove.is_empty ())
7510 : : {
7511 : 12900393 : bool do_release_defs = true;
7512 : 12900393 : gimple *stmt = to_remove.pop ();
7513 : :
7514 : : /* When we are value-numbering a region we do not require exit PHIs to
7515 : : be present so we have to make sure to deal with uses outside of the
7516 : : region of stmts that we thought are eliminated.
7517 : : ??? Note we may be confused by uses in dead regions we didn't run
7518 : : elimination on. Rather than checking individual uses we accept
7519 : : dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7520 : : contains such example). */
7521 : 12900393 : if (region_p)
7522 : : {
7523 : 1369249 : if (gphi *phi = dyn_cast <gphi *> (stmt))
7524 : : {
7525 : 871323 : tree lhs = gimple_phi_result (phi);
7526 : 871323 : if (!has_zero_uses (lhs))
7527 : : {
7528 : 14716 : if (dump_file && (dump_flags & TDF_DETAILS))
7529 : 3 : fprintf (dump_file, "Keeping eliminated stmt live "
7530 : : "as copy because of out-of-region uses\n");
7531 : 14716 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7532 : 14716 : gimple *copy = gimple_build_assign (lhs, sprime);
7533 : 14716 : gimple_stmt_iterator gsi
7534 : 14716 : = gsi_after_labels (gimple_bb (stmt));
7535 : 14716 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7536 : 14716 : do_release_defs = false;
7537 : : }
7538 : : }
7539 : 497926 : else if (tree lhs = gimple_get_lhs (stmt))
7540 : 497926 : if (TREE_CODE (lhs) == SSA_NAME
7541 : 497926 : && !has_zero_uses (lhs))
7542 : : {
7543 : 937 : if (dump_file && (dump_flags & TDF_DETAILS))
7544 : 0 : fprintf (dump_file, "Keeping eliminated stmt live "
7545 : : "as copy because of out-of-region uses\n");
7546 : 937 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7547 : 937 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7548 : 937 : if (is_gimple_assign (stmt))
7549 : : {
7550 : 937 : gimple_assign_set_rhs_from_tree (&gsi, sprime);
7551 : 937 : stmt = gsi_stmt (gsi);
7552 : 937 : update_stmt (stmt);
7553 : 937 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
7554 : 0 : bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
7555 : 937 : continue;
7556 : : }
7557 : : else
7558 : : {
7559 : 0 : gimple *copy = gimple_build_assign (lhs, sprime);
7560 : 0 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7561 : 0 : do_release_defs = false;
7562 : : }
7563 : : }
7564 : : }
7565 : :
7566 : 12899456 : if (dump_file && (dump_flags & TDF_DETAILS))
7567 : : {
7568 : 32416 : fprintf (dump_file, "Removing dead stmt ");
7569 : 32416 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
7570 : : }
7571 : :
7572 : 12899456 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7573 : 12899456 : if (gimple_code (stmt) == GIMPLE_PHI)
7574 : 1450645 : remove_phi_node (&gsi, do_release_defs);
7575 : : else
7576 : : {
7577 : 11448811 : basic_block bb = gimple_bb (stmt);
7578 : 11448811 : unlink_stmt_vdef (stmt);
7579 : 11448811 : if (gsi_remove (&gsi, true))
7580 : 26425 : bitmap_set_bit (need_eh_cleanup, bb->index);
7581 : 11448811 : if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
7582 : 2 : bitmap_set_bit (need_ab_cleanup, bb->index);
7583 : 11448811 : if (do_release_defs)
7584 : 11448811 : release_defs (stmt);
7585 : : }
7586 : :
7587 : : /* Removing a stmt may expose a forwarder block. */
7588 : 12899456 : el_todo |= TODO_cleanup_cfg;
7589 : : }
7590 : :
7591 : : /* Fixup stmts that became noreturn calls. This may require splitting
7592 : : blocks and thus isn't possible during the dominator walk. Do this
7593 : : in reverse order so we don't inadvertedly remove a stmt we want to
7594 : : fixup by visiting a dominating now noreturn call first. */
7595 : 5614785 : while (!to_fixup.is_empty ())
7596 : : {
7597 : 64 : gimple *stmt = to_fixup.pop ();
7598 : :
7599 : 64 : if (dump_file && (dump_flags & TDF_DETAILS))
7600 : : {
7601 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
7602 : 0 : print_gimple_stmt (dump_file, stmt, 0);
7603 : : }
7604 : :
7605 : 64 : if (fixup_noreturn_call (stmt))
7606 : 64 : el_todo |= TODO_cleanup_cfg;
7607 : : }
7608 : :
7609 : 5614721 : bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
7610 : 5614721 : bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
7611 : :
7612 : 5614721 : if (do_eh_cleanup)
7613 : 10866 : gimple_purge_all_dead_eh_edges (need_eh_cleanup);
7614 : :
7615 : 5614721 : if (do_ab_cleanup)
7616 : 2 : gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
7617 : :
7618 : 5614721 : if (do_eh_cleanup || do_ab_cleanup)
7619 : 10868 : el_todo |= TODO_cleanup_cfg;
7620 : :
7621 : 5614721 : return el_todo;
7622 : : }
7623 : :
7624 : : /* Eliminate fully redundant computations. */
7625 : :
7626 : : unsigned
7627 : 3957990 : eliminate_with_rpo_vn (bitmap inserted_exprs)
7628 : : {
7629 : 3957990 : eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
7630 : :
7631 : 3957990 : eliminate_dom_walker *saved_rpo_avail = rpo_avail;
7632 : 3957990 : rpo_avail = &walker;
7633 : 3957990 : walker.walk (cfun->cfg->x_entry_block_ptr);
7634 : 3957990 : rpo_avail = saved_rpo_avail;
7635 : :
7636 : 3957990 : return walker.eliminate_cleanup ();
7637 : 3957990 : }
7638 : :
7639 : : static unsigned
7640 : : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
7641 : : bool iterate, bool eliminate, bool skip_entry_phis,
7642 : : vn_lookup_kind kind);
7643 : :
7644 : : void
7645 : 914618 : run_rpo_vn (vn_lookup_kind kind)
7646 : : {
7647 : 914618 : do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
7648 : :
7649 : : /* ??? Prune requirement of these. */
7650 : 914618 : constant_to_value_id = new hash_table<vn_constant_hasher> (23);
7651 : :
7652 : : /* Initialize the value ids and prune out remaining VN_TOPs
7653 : : from dead code. */
7654 : 914618 : tree name;
7655 : 914618 : unsigned i;
7656 : 42122488 : FOR_EACH_SSA_NAME (i, name, cfun)
7657 : : {
7658 : 31120514 : vn_ssa_aux_t info = VN_INFO (name);
7659 : 31120514 : if (!info->visited
7660 : 31058209 : || info->valnum == VN_TOP)
7661 : 62305 : info->valnum = name;
7662 : 31120514 : if (info->valnum == name)
7663 : 30105057 : info->value_id = get_next_value_id ();
7664 : 1015457 : else if (is_gimple_min_invariant (info->valnum))
7665 : 39581 : info->value_id = get_or_alloc_constant_value_id (info->valnum);
7666 : : }
7667 : :
7668 : : /* Propagate. */
7669 : 42122488 : FOR_EACH_SSA_NAME (i, name, cfun)
7670 : : {
7671 : 31120514 : vn_ssa_aux_t info = VN_INFO (name);
7672 : 31120514 : if (TREE_CODE (info->valnum) == SSA_NAME
7673 : 31080933 : && info->valnum != name
7674 : 32096390 : && info->value_id != VN_INFO (info->valnum)->value_id)
7675 : 975876 : info->value_id = VN_INFO (info->valnum)->value_id;
7676 : : }
7677 : :
7678 : 914618 : set_hashtable_value_ids ();
7679 : :
7680 : 914618 : if (dump_file && (dump_flags & TDF_DETAILS))
7681 : : {
7682 : 16 : fprintf (dump_file, "Value numbers:\n");
7683 : 491 : FOR_EACH_SSA_NAME (i, name, cfun)
7684 : : {
7685 : 371 : if (VN_INFO (name)->visited
7686 : 371 : && SSA_VAL (name) != name)
7687 : : {
7688 : 41 : print_generic_expr (dump_file, name);
7689 : 41 : fprintf (dump_file, " = ");
7690 : 41 : print_generic_expr (dump_file, SSA_VAL (name));
7691 : 41 : fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
7692 : : }
7693 : : }
7694 : : }
7695 : 914618 : }
7696 : :
7697 : : /* Free VN associated data structures. */
7698 : :
7699 : : void
7700 : 5634002 : free_rpo_vn (void)
7701 : : {
7702 : 5634002 : free_vn_table (valid_info);
7703 : 5634002 : XDELETE (valid_info);
7704 : 5634002 : obstack_free (&vn_tables_obstack, NULL);
7705 : 5634002 : obstack_free (&vn_tables_insert_obstack, NULL);
7706 : :
7707 : 5634002 : vn_ssa_aux_iterator_type it;
7708 : 5634002 : vn_ssa_aux_t info;
7709 : 312522066 : FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
7710 : 153444032 : if (info->needs_insertion)
7711 : 3461023 : release_ssa_name (info->name);
7712 : 5634002 : obstack_free (&vn_ssa_aux_obstack, NULL);
7713 : 5634002 : delete vn_ssa_aux_hash;
7714 : :
7715 : 5634002 : delete constant_to_value_id;
7716 : 5634002 : constant_to_value_id = NULL;
7717 : 5634002 : }
7718 : :
7719 : : /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7720 : :
7721 : : static tree
7722 : 20134733 : vn_lookup_simplify_result (gimple_match_op *res_op)
7723 : : {
7724 : 20134733 : if (!res_op->code.is_tree_code ())
7725 : : return NULL_TREE;
7726 : 20132385 : tree *ops = res_op->ops;
7727 : 20132385 : unsigned int length = res_op->num_ops;
7728 : 20132385 : if (res_op->code == CONSTRUCTOR
7729 : : /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7730 : : and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7731 : 20132385 : && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
7732 : : {
7733 : 1130 : length = CONSTRUCTOR_NELTS (res_op->ops[0]);
7734 : 1130 : ops = XALLOCAVEC (tree, length);
7735 : 5838 : for (unsigned i = 0; i < length; ++i)
7736 : 4708 : ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
7737 : : }
7738 : 20132385 : vn_nary_op_t vnresult = NULL;
7739 : 20132385 : tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
7740 : : res_op->type, ops, &vnresult);
7741 : : /* If this is used from expression simplification make sure to
7742 : : return an available expression. */
7743 : 20132385 : if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
7744 : 1936076 : res = rpo_avail->eliminate_avail (vn_context_bb, res);
7745 : : return res;
7746 : : }
7747 : :
7748 : : /* Return a leader for OPs value that is valid at BB. */
7749 : :
7750 : : tree
7751 : 218479848 : rpo_elim::eliminate_avail (basic_block bb, tree op)
7752 : : {
7753 : 218479848 : bool visited;
7754 : 218479848 : tree valnum = SSA_VAL (op, &visited);
7755 : : /* If we didn't visit OP then it must be defined outside of the
7756 : : region we process and also dominate it. So it is available. */
7757 : 218479848 : if (!visited)
7758 : : return op;
7759 : 216644001 : if (TREE_CODE (valnum) == SSA_NAME)
7760 : : {
7761 : 204782210 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
7762 : : return valnum;
7763 : 198912106 : vn_ssa_aux_t valnum_info = VN_INFO (valnum);
7764 : 198912106 : vn_avail *av = valnum_info->avail;
7765 : 198912106 : if (!av)
7766 : : {
7767 : : /* See above. But when there's availability info prefer
7768 : : what we recorded there for example to preserve LC SSA. */
7769 : 74554571 : if (!valnum_info->visited)
7770 : : return valnum;
7771 : : return NULL_TREE;
7772 : : }
7773 : 124357535 : if (av->location == bb->index)
7774 : : /* On tramp3d 90% of the cases are here. */
7775 : 83170223 : return ssa_name (av->leader);
7776 : 54499162 : do
7777 : : {
7778 : 54499162 : basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
7779 : : /* ??? During elimination we have to use availability at the
7780 : : definition site of a use we try to replace. This
7781 : : is required to not run into inconsistencies because
7782 : : of dominated_by_p_w_unex behavior and removing a definition
7783 : : while not replacing all uses.
7784 : : ??? We could try to consistently walk dominators
7785 : : ignoring non-executable regions. The nearest common
7786 : : dominator of bb and abb is where we can stop walking. We
7787 : : may also be able to "pre-compute" (bits of) the next immediate
7788 : : (non-)dominator during the RPO walk when marking edges as
7789 : : executable. */
7790 : 54499162 : if (dominated_by_p_w_unex (bb, abb, true))
7791 : : {
7792 : 37674891 : tree leader = ssa_name (av->leader);
7793 : : /* Prevent eliminations that break loop-closed SSA. */
7794 : 37674891 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
7795 : 1835470 : && ! SSA_NAME_IS_DEFAULT_DEF (leader)
7796 : 39510361 : && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7797 : 1835470 : (leader))->loop_father,
7798 : : bb))
7799 : : return NULL_TREE;
7800 : 37640405 : if (dump_file && (dump_flags & TDF_DETAILS))
7801 : : {
7802 : 4584 : print_generic_expr (dump_file, leader);
7803 : 4584 : fprintf (dump_file, " is available for ");
7804 : 4584 : print_generic_expr (dump_file, valnum);
7805 : 4584 : fprintf (dump_file, "\n");
7806 : : }
7807 : : /* On tramp3d 99% of the _remaining_ cases succeed at
7808 : : the first enty. */
7809 : 37640405 : return leader;
7810 : : }
7811 : : /* ??? Can we somehow skip to the immediate dominator
7812 : : RPO index (bb_to_rpo)? Again, maybe not worth, on
7813 : : tramp3d the worst number of elements in the vector is 9. */
7814 : 16824271 : av = av->next;
7815 : : }
7816 : 16824271 : while (av);
7817 : : /* While we prefer avail we have to fallback to using the value
7818 : : directly if defined outside of the region when none of the
7819 : : available defs suit. */
7820 : 3512421 : if (!valnum_info->visited)
7821 : : return valnum;
7822 : : }
7823 : 11861791 : else if (valnum != VN_TOP)
7824 : : /* valnum is is_gimple_min_invariant. */
7825 : : return valnum;
7826 : : return NULL_TREE;
7827 : : }
7828 : :
7829 : : /* Make LEADER a leader for its value at BB. */
7830 : :
7831 : : void
7832 : 85741440 : rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
7833 : : {
7834 : 85741440 : tree valnum = VN_INFO (leader)->valnum;
7835 : 85741440 : if (valnum == VN_TOP
7836 : 85741440 : || is_gimple_min_invariant (valnum))
7837 : 0 : return;
7838 : 85741440 : if (dump_file && (dump_flags & TDF_DETAILS))
7839 : : {
7840 : 282089 : fprintf (dump_file, "Making available beyond BB%d ", bb->index);
7841 : 282089 : print_generic_expr (dump_file, leader);
7842 : 282089 : fprintf (dump_file, " for value ");
7843 : 282089 : print_generic_expr (dump_file, valnum);
7844 : 282089 : fprintf (dump_file, "\n");
7845 : : }
7846 : 85741440 : vn_ssa_aux_t value = VN_INFO (valnum);
7847 : 85741440 : vn_avail *av;
7848 : 85741440 : if (m_avail_freelist)
7849 : : {
7850 : 16831526 : av = m_avail_freelist;
7851 : 16831526 : m_avail_freelist = m_avail_freelist->next;
7852 : : }
7853 : : else
7854 : 68909914 : av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
7855 : 85741440 : av->location = bb->index;
7856 : 85741440 : av->leader = SSA_NAME_VERSION (leader);
7857 : 85741440 : av->next = value->avail;
7858 : 85741440 : av->next_undo = last_pushed_avail;
7859 : 85741440 : last_pushed_avail = value;
7860 : 85741440 : value->avail = av;
7861 : : }
7862 : :
7863 : : /* Valueization hook for RPO VN plus required state. */
7864 : :
7865 : : tree
7866 : 1526050564 : rpo_vn_valueize (tree name)
7867 : : {
7868 : 1526050564 : if (TREE_CODE (name) == SSA_NAME)
7869 : : {
7870 : 1484746980 : vn_ssa_aux_t val = VN_INFO (name);
7871 : 1484746980 : if (val)
7872 : : {
7873 : 1484746980 : tree tem = val->valnum;
7874 : 1484746980 : if (tem != VN_TOP && tem != name)
7875 : : {
7876 : 81998830 : if (TREE_CODE (tem) != SSA_NAME)
7877 : : return tem;
7878 : : /* For all values we only valueize to an available leader
7879 : : which means we can use SSA name info without restriction. */
7880 : 67755614 : tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
7881 : 67755614 : if (tem)
7882 : : return tem;
7883 : : }
7884 : : }
7885 : : }
7886 : : return name;
7887 : : }
7888 : :
7889 : : /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7890 : : inverted condition. */
7891 : :
7892 : : static void
7893 : 23031328 : insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
7894 : : {
7895 : 23031328 : switch (code)
7896 : : {
7897 : 1230590 : case LT_EXPR:
7898 : : /* a < b -> a {!,<}= b */
7899 : 1230590 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7900 : : ops, boolean_true_node, 0, pred_e);
7901 : 1230590 : vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
7902 : : ops, boolean_true_node, 0, pred_e);
7903 : : /* a < b -> ! a {>,=} b */
7904 : 1230590 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7905 : : ops, boolean_false_node, 0, pred_e);
7906 : 1230590 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7907 : : ops, boolean_false_node, 0, pred_e);
7908 : 1230590 : break;
7909 : 2538953 : case GT_EXPR:
7910 : : /* a > b -> a {!,>}= b */
7911 : 2538953 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7912 : : ops, boolean_true_node, 0, pred_e);
7913 : 2538953 : vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
7914 : : ops, boolean_true_node, 0, pred_e);
7915 : : /* a > b -> ! a {<,=} b */
7916 : 2538953 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7917 : : ops, boolean_false_node, 0, pred_e);
7918 : 2538953 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7919 : : ops, boolean_false_node, 0, pred_e);
7920 : 2538953 : break;
7921 : 8014379 : case EQ_EXPR:
7922 : : /* a == b -> ! a {<,>} b */
7923 : 8014379 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7924 : : ops, boolean_false_node, 0, pred_e);
7925 : 8014379 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7926 : : ops, boolean_false_node, 0, pred_e);
7927 : 8014379 : break;
7928 : : case LE_EXPR:
7929 : : case GE_EXPR:
7930 : : case NE_EXPR:
7931 : : /* Nothing besides inverted condition. */
7932 : : break;
7933 : 23031328 : default:;
7934 : : }
7935 : 23031328 : }
7936 : :
7937 : : /* Main stmt worker for RPO VN, process BB. */
7938 : :
7939 : : static unsigned
7940 : 54444423 : process_bb (rpo_elim &avail, basic_block bb,
7941 : : bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
7942 : : bool do_region, bitmap exit_bbs, bool skip_phis)
7943 : : {
7944 : 54444423 : unsigned todo = 0;
7945 : 54444423 : edge_iterator ei;
7946 : 54444423 : edge e;
7947 : :
7948 : 54444423 : vn_context_bb = bb;
7949 : :
7950 : : /* If we are in loop-closed SSA preserve this state. This is
7951 : : relevant when called on regions from outside of FRE/PRE. */
7952 : 54444423 : bool lc_phi_nodes = false;
7953 : 54444423 : if (!skip_phis
7954 : 54444423 : && loops_state_satisfies_p (LOOP_CLOSED_SSA))
7955 : 1847034 : FOR_EACH_EDGE (e, ei, bb->preds)
7956 : 1107919 : if (e->src->loop_father != e->dest->loop_father
7957 : 1107919 : && flow_loop_nested_p (e->dest->loop_father,
7958 : : e->src->loop_father))
7959 : : {
7960 : : lc_phi_nodes = true;
7961 : : break;
7962 : : }
7963 : :
7964 : : /* When we visit a loop header substitute into loop info. */
7965 : 54444423 : if (!iterate && eliminate && bb->loop_father->header == bb)
7966 : : {
7967 : : /* Keep fields in sync with substitute_in_loop_info. */
7968 : 738306 : if (bb->loop_father->nb_iterations)
7969 : 73431 : bb->loop_father->nb_iterations
7970 : 73431 : = simplify_replace_tree (bb->loop_father->nb_iterations,
7971 : : NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
7972 : : }
7973 : :
7974 : : /* Value-number all defs in the basic-block. */
7975 : 54444423 : if (!skip_phis)
7976 : 77784163 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7977 : 23360511 : gsi_next (&gsi))
7978 : : {
7979 : 23360511 : gphi *phi = gsi.phi ();
7980 : 23360511 : tree res = PHI_RESULT (phi);
7981 : 23360511 : vn_ssa_aux_t res_info = VN_INFO (res);
7982 : 23360511 : if (!bb_visited)
7983 : : {
7984 : 16598082 : gcc_assert (!res_info->visited);
7985 : 16598082 : res_info->valnum = VN_TOP;
7986 : 16598082 : res_info->visited = true;
7987 : : }
7988 : :
7989 : : /* When not iterating force backedge values to varying. */
7990 : 23360511 : visit_stmt (phi, !iterate_phis);
7991 : 46721022 : if (virtual_operand_p (res))
7992 : 9800856 : continue;
7993 : :
7994 : : /* Eliminate */
7995 : : /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
7996 : : how we handle backedges and availability.
7997 : : And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
7998 : 13559655 : tree val = res_info->valnum;
7999 : 13559655 : if (res != val && !iterate && eliminate)
8000 : : {
8001 : 1074772 : if (tree leader = avail.eliminate_avail (bb, res))
8002 : : {
8003 : 1005217 : if (leader != res
8004 : : /* Preserve loop-closed SSA form. */
8005 : 1005217 : && (! lc_phi_nodes
8006 : 1084 : || is_gimple_min_invariant (leader)))
8007 : : {
8008 : 1004980 : if (dump_file && (dump_flags & TDF_DETAILS))
8009 : : {
8010 : 114 : fprintf (dump_file, "Replaced redundant PHI node "
8011 : : "defining ");
8012 : 114 : print_generic_expr (dump_file, res);
8013 : 114 : fprintf (dump_file, " with ");
8014 : 114 : print_generic_expr (dump_file, leader);
8015 : 114 : fprintf (dump_file, "\n");
8016 : : }
8017 : 1004980 : avail.eliminations++;
8018 : :
8019 : 1004980 : if (may_propagate_copy (res, leader))
8020 : : {
8021 : : /* Schedule for removal. */
8022 : 1004980 : avail.to_remove.safe_push (phi);
8023 : 1004980 : continue;
8024 : : }
8025 : : /* ??? Else generate a copy stmt. */
8026 : : }
8027 : : }
8028 : : }
8029 : : /* Only make defs available that not already are. But make
8030 : : sure loop-closed SSA PHI node defs are picked up for
8031 : : downstream uses. */
8032 : 12554675 : if (lc_phi_nodes
8033 : 12554675 : || res == val
8034 : 12554675 : || ! avail.eliminate_avail (bb, res))
8035 : 9467679 : avail.eliminate_push_avail (bb, res);
8036 : : }
8037 : :
8038 : : /* For empty BBs mark outgoing edges executable. For non-empty BBs
8039 : : we do this when processing the last stmt as we have to do this
8040 : : before elimination which otherwise forces GIMPLE_CONDs to
8041 : : if (1 != 0) style when seeing non-executable edges. */
8042 : 108888846 : if (gsi_end_p (gsi_start_bb (bb)))
8043 : : {
8044 : 12719039 : FOR_EACH_EDGE (e, ei, bb->succs)
8045 : : {
8046 : 6359527 : if (!(e->flags & EDGE_EXECUTABLE))
8047 : : {
8048 : 4473950 : if (dump_file && (dump_flags & TDF_DETAILS))
8049 : 2790 : fprintf (dump_file,
8050 : : "marking outgoing edge %d -> %d executable\n",
8051 : 2790 : e->src->index, e->dest->index);
8052 : 4473950 : e->flags |= EDGE_EXECUTABLE;
8053 : 4473950 : e->dest->flags |= BB_EXECUTABLE;
8054 : : }
8055 : 1885577 : else if (!(e->dest->flags & BB_EXECUTABLE))
8056 : : {
8057 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8058 : 0 : fprintf (dump_file,
8059 : : "marking destination block %d reachable\n",
8060 : : e->dest->index);
8061 : 0 : e->dest->flags |= BB_EXECUTABLE;
8062 : : }
8063 : : }
8064 : : }
8065 : 108888846 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8066 : 404144566 : !gsi_end_p (gsi); gsi_next (&gsi))
8067 : : {
8068 : 349700143 : ssa_op_iter i;
8069 : 349700143 : tree op;
8070 : 349700143 : if (!bb_visited)
8071 : : {
8072 : 410913182 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8073 : : {
8074 : 122887079 : vn_ssa_aux_t op_info = VN_INFO (op);
8075 : 122887079 : gcc_assert (!op_info->visited);
8076 : 122887079 : op_info->valnum = VN_TOP;
8077 : 122887079 : op_info->visited = true;
8078 : : }
8079 : :
8080 : : /* We somehow have to deal with uses that are not defined
8081 : : in the processed region. Forcing unvisited uses to
8082 : : varying here doesn't play well with def-use following during
8083 : : expression simplification, so we deal with this by checking
8084 : : the visited flag in SSA_VAL. */
8085 : : }
8086 : :
8087 : 349700143 : visit_stmt (gsi_stmt (gsi));
8088 : :
8089 : 349700143 : gimple *last = gsi_stmt (gsi);
8090 : 349700143 : e = NULL;
8091 : 349700143 : switch (gimple_code (last))
8092 : : {
8093 : 104981 : case GIMPLE_SWITCH:
8094 : 104981 : e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8095 : 104981 : (as_a <gswitch *> (last))));
8096 : 104981 : break;
8097 : 20866946 : case GIMPLE_COND:
8098 : 20866946 : {
8099 : 20866946 : tree lhs = vn_valueize (gimple_cond_lhs (last));
8100 : 20866946 : tree rhs = vn_valueize (gimple_cond_rhs (last));
8101 : 20866946 : tree val = gimple_simplify (gimple_cond_code (last),
8102 : : boolean_type_node, lhs, rhs,
8103 : : NULL, vn_valueize);
8104 : : /* If the condition didn't simplfy see if we have recorded
8105 : : an expression from sofar taken edges. */
8106 : 20866946 : if (! val || TREE_CODE (val) != INTEGER_CST)
8107 : : {
8108 : 19375391 : vn_nary_op_t vnresult;
8109 : 19375391 : tree ops[2];
8110 : 19375391 : ops[0] = lhs;
8111 : 19375391 : ops[1] = rhs;
8112 : 19375391 : val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
8113 : : boolean_type_node, ops,
8114 : : &vnresult);
8115 : : /* Did we get a predicated value? */
8116 : 19375391 : if (! val && vnresult && vnresult->predicated_values)
8117 : : {
8118 : 969803 : val = vn_nary_op_get_predicated_value (vnresult, bb);
8119 : 969803 : if (val && dump_file && (dump_flags & TDF_DETAILS))
8120 : : {
8121 : 2 : fprintf (dump_file, "Got predicated value ");
8122 : 2 : print_generic_expr (dump_file, val, TDF_NONE);
8123 : 2 : fprintf (dump_file, " for ");
8124 : 2 : print_gimple_stmt (dump_file, last, TDF_SLIM);
8125 : : }
8126 : : }
8127 : : }
8128 : 19375391 : if (val)
8129 : 1945843 : e = find_taken_edge (bb, val);
8130 : 20866946 : if (! e)
8131 : : {
8132 : : /* If we didn't manage to compute the taken edge then
8133 : : push predicated expressions for the condition itself
8134 : : and related conditions to the hashtables. This allows
8135 : : simplification of redundant conditions which is
8136 : : important as early cleanup. */
8137 : 19155391 : edge true_e, false_e;
8138 : 19155391 : extract_true_false_edges_from_block (bb, &true_e, &false_e);
8139 : 19155391 : enum tree_code code = gimple_cond_code (last);
8140 : 19155391 : enum tree_code icode
8141 : 19155391 : = invert_tree_comparison (code, HONOR_NANS (lhs));
8142 : 19155391 : tree ops[2];
8143 : 19155391 : ops[0] = lhs;
8144 : 19155391 : ops[1] = rhs;
8145 : 403949 : if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8146 : 19311607 : || !can_track_predicate_on_edge (true_e))
8147 : 4143371 : true_e = NULL;
8148 : 403949 : if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8149 : 19287032 : || !can_track_predicate_on_edge (false_e))
8150 : 4785204 : false_e = NULL;
8151 : 19155391 : if (true_e)
8152 : 15012020 : vn_nary_op_insert_pieces_predicated
8153 : 15012020 : (2, code, boolean_type_node, ops,
8154 : : boolean_true_node, 0, true_e);
8155 : 19155391 : if (false_e)
8156 : 14370187 : vn_nary_op_insert_pieces_predicated
8157 : 14370187 : (2, code, boolean_type_node, ops,
8158 : : boolean_false_node, 0, false_e);
8159 : 19155391 : if (icode != ERROR_MARK)
8160 : : {
8161 : 18969033 : if (true_e)
8162 : 14886419 : vn_nary_op_insert_pieces_predicated
8163 : 14886419 : (2, icode, boolean_type_node, ops,
8164 : : boolean_false_node, 0, true_e);
8165 : 18969033 : if (false_e)
8166 : 14228171 : vn_nary_op_insert_pieces_predicated
8167 : 14228171 : (2, icode, boolean_type_node, ops,
8168 : : boolean_true_node, 0, false_e);
8169 : : }
8170 : : /* Relax for non-integers, inverted condition handled
8171 : : above. */
8172 : 19155391 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8173 : : {
8174 : 15061480 : if (true_e)
8175 : 11822618 : insert_related_predicates_on_edge (code, ops, true_e);
8176 : 15061480 : if (false_e)
8177 : 11208710 : insert_related_predicates_on_edge (icode, ops, false_e);
8178 : : }
8179 : : }
8180 : : break;
8181 : : }
8182 : 1363 : case GIMPLE_GOTO:
8183 : 1363 : e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8184 : 1363 : break;
8185 : : default:
8186 : : e = NULL;
8187 : : }
8188 : 349700143 : if (e)
8189 : : {
8190 : 1715127 : todo = TODO_cleanup_cfg;
8191 : 1715127 : if (!(e->flags & EDGE_EXECUTABLE))
8192 : : {
8193 : 1348450 : if (dump_file && (dump_flags & TDF_DETAILS))
8194 : 41 : fprintf (dump_file,
8195 : : "marking known outgoing %sedge %d -> %d executable\n",
8196 : 41 : e->flags & EDGE_DFS_BACK ? "back-" : "",
8197 : 41 : e->src->index, e->dest->index);
8198 : 1348450 : e->flags |= EDGE_EXECUTABLE;
8199 : 1348450 : e->dest->flags |= BB_EXECUTABLE;
8200 : : }
8201 : 366677 : else if (!(e->dest->flags & BB_EXECUTABLE))
8202 : : {
8203 : 8762 : if (dump_file && (dump_flags & TDF_DETAILS))
8204 : 0 : fprintf (dump_file,
8205 : : "marking destination block %d reachable\n",
8206 : : e->dest->index);
8207 : 8762 : e->dest->flags |= BB_EXECUTABLE;
8208 : : }
8209 : : }
8210 : 695970032 : else if (gsi_one_before_end_p (gsi))
8211 : : {
8212 : 113979062 : FOR_EACH_EDGE (e, ei, bb->succs)
8213 : : {
8214 : 67609278 : if (!(e->flags & EDGE_EXECUTABLE))
8215 : : {
8216 : 49589575 : if (dump_file && (dump_flags & TDF_DETAILS))
8217 : 18967 : fprintf (dump_file,
8218 : : "marking outgoing edge %d -> %d executable\n",
8219 : 18967 : e->src->index, e->dest->index);
8220 : 49589575 : e->flags |= EDGE_EXECUTABLE;
8221 : 49589575 : e->dest->flags |= BB_EXECUTABLE;
8222 : : }
8223 : 18019703 : else if (!(e->dest->flags & BB_EXECUTABLE))
8224 : : {
8225 : 2284284 : if (dump_file && (dump_flags & TDF_DETAILS))
8226 : 5305 : fprintf (dump_file,
8227 : : "marking destination block %d reachable\n",
8228 : : e->dest->index);
8229 : 2284284 : e->dest->flags |= BB_EXECUTABLE;
8230 : : }
8231 : : }
8232 : : }
8233 : :
8234 : : /* Eliminate. That also pushes to avail. */
8235 : 349700143 : if (eliminate && ! iterate)
8236 : 83325838 : avail.eliminate_stmt (bb, &gsi);
8237 : : else
8238 : : /* If not eliminating, make all not already available defs
8239 : : available. But avoid picking up dead defs. */
8240 : 339437670 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8241 : 73063365 : if (! has_zero_uses (op)
8242 : 73063365 : && ! avail.eliminate_avail (bb, op))
8243 : 55894332 : avail.eliminate_push_avail (bb, op);
8244 : : }
8245 : :
8246 : : /* Eliminate in destination PHI arguments. Always substitute in dest
8247 : : PHIs, even for non-executable edges. This handles region
8248 : : exits PHIs. */
8249 : 54444423 : if (!iterate && eliminate)
8250 : 28180201 : FOR_EACH_EDGE (e, ei, bb->succs)
8251 : 16700431 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
8252 : 31262352 : !gsi_end_p (gsi); gsi_next (&gsi))
8253 : : {
8254 : 14561921 : gphi *phi = gsi.phi ();
8255 : 14561921 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8256 : 14561921 : tree arg = USE_FROM_PTR (use_p);
8257 : 22627746 : if (TREE_CODE (arg) != SSA_NAME
8258 : 14561921 : || virtual_operand_p (arg))
8259 : 8065825 : continue;
8260 : 6496096 : tree sprime;
8261 : 6496096 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
8262 : : {
8263 : 94158 : sprime = SSA_VAL (arg);
8264 : 94158 : gcc_assert (TREE_CODE (sprime) != SSA_NAME
8265 : : || SSA_NAME_IS_DEFAULT_DEF (sprime));
8266 : : }
8267 : : else
8268 : : /* Look for sth available at the definition block of the argument.
8269 : : This avoids inconsistencies between availability there which
8270 : : decides if the stmt can be removed and availability at the
8271 : : use site. The SSA property ensures that things available
8272 : : at the definition are also available at uses. */
8273 : 6401938 : sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8274 : : arg);
8275 : 6496096 : if (sprime
8276 : 6496096 : && sprime != arg
8277 : 6496096 : && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8278 : 1191848 : propagate_value (use_p, sprime);
8279 : : }
8280 : :
8281 : 54444423 : vn_context_bb = NULL;
8282 : 54444423 : return todo;
8283 : : }
8284 : :
8285 : : /* Unwind state per basic-block. */
8286 : :
8287 : : struct unwind_state
8288 : : {
8289 : : /* Times this block has been visited. */
8290 : : unsigned visited;
8291 : : /* Whether to handle this as iteration point or whether to treat
8292 : : incoming backedge PHI values as varying. */
8293 : : bool iterate;
8294 : : /* Maximum RPO index this block is reachable from. */
8295 : : int max_rpo;
8296 : : /* Unwind state. */
8297 : : void *ob_top;
8298 : : vn_reference_t ref_top;
8299 : : vn_phi_t phi_top;
8300 : : vn_nary_op_t nary_top;
8301 : : vn_avail *avail_top;
8302 : : };
8303 : :
8304 : : /* Unwind the RPO VN state for iteration. */
8305 : :
8306 : : static void
8307 : 1651715 : do_unwind (unwind_state *to, rpo_elim &avail)
8308 : : {
8309 : 1651715 : gcc_assert (to->iterate);
8310 : 30261975 : for (; last_inserted_nary != to->nary_top;
8311 : 28610260 : last_inserted_nary = last_inserted_nary->next)
8312 : : {
8313 : 28610260 : vn_nary_op_t *slot;
8314 : 28610260 : slot = valid_info->nary->find_slot_with_hash
8315 : 28610260 : (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
8316 : : /* Predication causes the need to restore previous state. */
8317 : 28610260 : if ((*slot)->unwind_to)
8318 : 5691080 : *slot = (*slot)->unwind_to;
8319 : : else
8320 : 22919180 : valid_info->nary->clear_slot (slot);
8321 : : }
8322 : 6415167 : for (; last_inserted_phi != to->phi_top;
8323 : 4763452 : last_inserted_phi = last_inserted_phi->next)
8324 : : {
8325 : 4763452 : vn_phi_t *slot;
8326 : 4763452 : slot = valid_info->phis->find_slot_with_hash
8327 : 4763452 : (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
8328 : 4763452 : valid_info->phis->clear_slot (slot);
8329 : : }
8330 : 13691254 : for (; last_inserted_ref != to->ref_top;
8331 : 12039539 : last_inserted_ref = last_inserted_ref->next)
8332 : : {
8333 : 12039539 : vn_reference_t *slot;
8334 : 12039539 : slot = valid_info->references->find_slot_with_hash
8335 : 12039539 : (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
8336 : 12039539 : (*slot)->operands.release ();
8337 : 12039539 : valid_info->references->clear_slot (slot);
8338 : : }
8339 : 1651715 : obstack_free (&vn_tables_obstack, to->ob_top);
8340 : :
8341 : : /* Prune [rpo_idx, ] from avail. */
8342 : 18483241 : for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
8343 : : {
8344 : 16831526 : vn_ssa_aux_t val = last_pushed_avail;
8345 : 16831526 : vn_avail *av = val->avail;
8346 : 16831526 : val->avail = av->next;
8347 : 16831526 : last_pushed_avail = av->next_undo;
8348 : 16831526 : av->next = avail.m_avail_freelist;
8349 : 16831526 : avail.m_avail_freelist = av;
8350 : : }
8351 : 1651715 : }
8352 : :
8353 : : /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8354 : : If ITERATE is true then treat backedges optimistically as not
8355 : : executed and iterate. If ELIMINATE is true then perform
8356 : : elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8357 : : is true then force PHI nodes in ENTRY->dest to VARYING. */
8358 : :
8359 : : static unsigned
8360 : 5634002 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8361 : : bool iterate, bool eliminate, bool skip_entry_phis,
8362 : : vn_lookup_kind kind)
8363 : : {
8364 : 5634002 : unsigned todo = 0;
8365 : 5634002 : default_vn_walk_kind = kind;
8366 : :
8367 : : /* We currently do not support region-based iteration when
8368 : : elimination is requested. */
8369 : 5634002 : gcc_assert (!entry || !iterate || !eliminate);
8370 : : /* When iterating we need loop info up-to-date. */
8371 : 5634002 : gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
8372 : :
8373 : 5634002 : bool do_region = entry != NULL;
8374 : 5634002 : if (!do_region)
8375 : : {
8376 : 5077001 : entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
8377 : 5077001 : exit_bbs = BITMAP_ALLOC (NULL);
8378 : 5077001 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
8379 : : }
8380 : :
8381 : : /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8382 : : re-mark those that are contained in the region. */
8383 : 5634002 : edge_iterator ei;
8384 : 5634002 : edge e;
8385 : 11310880 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8386 : 5676878 : e->flags &= ~EDGE_DFS_BACK;
8387 : :
8388 : 5634002 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
8389 : 5634002 : auto_vec<std::pair<int, int> > toplevel_scc_extents;
8390 : 5634002 : int n = rev_post_order_and_mark_dfs_back_seme
8391 : 7310014 : (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
8392 : :
8393 : 5634002 : if (!do_region)
8394 : 5077001 : BITMAP_FREE (exit_bbs);
8395 : :
8396 : : /* If there are any non-DFS_BACK edges into entry->dest skip
8397 : : processing PHI nodes for that block. This supports
8398 : : value-numbering loop bodies w/o the actual loop. */
8399 : 11310879 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8400 : 5676878 : if (e != entry
8401 : 42876 : && !(e->flags & EDGE_DFS_BACK))
8402 : : break;
8403 : 5634002 : if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
8404 : 0 : fprintf (dump_file, "Region does not contain all edges into "
8405 : : "the entry block, skipping its PHIs.\n");
8406 : 5634002 : skip_entry_phis |= e != NULL;
8407 : :
8408 : 5634002 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
8409 : 50278973 : for (int i = 0; i < n; ++i)
8410 : 44644971 : bb_to_rpo[rpo[i]] = i;
8411 : :
8412 : 5634002 : unwind_state *rpo_state = XNEWVEC (unwind_state, n);
8413 : :
8414 : 5634002 : rpo_elim avail (entry->dest);
8415 : 5634002 : rpo_avail = &avail;
8416 : :
8417 : : /* Verify we have no extra entries into the region. */
8418 : 5634002 : if (flag_checking && do_region)
8419 : : {
8420 : 556995 : auto_bb_flag bb_in_region (fn);
8421 : 1604036 : for (int i = 0; i < n; ++i)
8422 : : {
8423 : 1047041 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8424 : 1047041 : bb->flags |= bb_in_region;
8425 : : }
8426 : : /* We can't merge the first two loops because we cannot rely
8427 : : on EDGE_DFS_BACK for edges not within the region. But if
8428 : : we decide to always have the bb_in_region flag we can
8429 : : do the checking during the RPO walk itself (but then it's
8430 : : also easy to handle MEME conservatively). */
8431 : 1604036 : for (int i = 0; i < n; ++i)
8432 : : {
8433 : 1047041 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8434 : 1047041 : edge e;
8435 : 1047041 : edge_iterator ei;
8436 : 2272312 : FOR_EACH_EDGE (e, ei, bb->preds)
8437 : 1225271 : gcc_assert (e == entry
8438 : : || (skip_entry_phis && bb == entry->dest)
8439 : : || (e->src->flags & bb_in_region));
8440 : : }
8441 : 1604036 : for (int i = 0; i < n; ++i)
8442 : : {
8443 : 1047041 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8444 : 1047041 : bb->flags &= ~bb_in_region;
8445 : : }
8446 : 556995 : }
8447 : :
8448 : : /* Create the VN state. For the initial size of the various hashtables
8449 : : use a heuristic based on region size and number of SSA names. */
8450 : 5634002 : unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
8451 : 5634002 : / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
8452 : 5634002 : VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
8453 : 5634002 : next_value_id = 1;
8454 : 5634002 : next_constant_value_id = -1;
8455 : :
8456 : 5634002 : vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
8457 : 5634002 : gcc_obstack_init (&vn_ssa_aux_obstack);
8458 : :
8459 : 5634002 : gcc_obstack_init (&vn_tables_obstack);
8460 : 5634002 : gcc_obstack_init (&vn_tables_insert_obstack);
8461 : 5634002 : valid_info = XCNEW (struct vn_tables_s);
8462 : 5634002 : allocate_vn_table (valid_info, region_size);
8463 : 5634002 : last_inserted_ref = NULL;
8464 : 5634002 : last_inserted_phi = NULL;
8465 : 5634002 : last_inserted_nary = NULL;
8466 : 5634002 : last_pushed_avail = NULL;
8467 : :
8468 : 5634002 : vn_valueize = rpo_vn_valueize;
8469 : :
8470 : : /* Initialize the unwind state and edge/BB executable state. */
8471 : 5634002 : unsigned curr_scc = 0;
8472 : 50278973 : for (int i = 0; i < n; ++i)
8473 : : {
8474 : 44644971 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8475 : 44644971 : rpo_state[i].visited = 0;
8476 : 44644971 : rpo_state[i].max_rpo = i;
8477 : 51591652 : if (!iterate && curr_scc < toplevel_scc_extents.length ())
8478 : : {
8479 : 5665380 : if (i >= toplevel_scc_extents[curr_scc].first
8480 : 5665380 : && i <= toplevel_scc_extents[curr_scc].second)
8481 : 3008996 : rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
8482 : 5665380 : if (i == toplevel_scc_extents[curr_scc].second)
8483 : 582967 : curr_scc++;
8484 : : }
8485 : 44644971 : bb->flags &= ~BB_EXECUTABLE;
8486 : 44644971 : bool has_backedges = false;
8487 : 44644971 : edge e;
8488 : 44644971 : edge_iterator ei;
8489 : 106003667 : FOR_EACH_EDGE (e, ei, bb->preds)
8490 : : {
8491 : 61358696 : if (e->flags & EDGE_DFS_BACK)
8492 : 2398162 : has_backedges = true;
8493 : 61358696 : e->flags &= ~EDGE_EXECUTABLE;
8494 : 61358696 : if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
8495 : 61358696 : continue;
8496 : : }
8497 : 44644971 : rpo_state[i].iterate = iterate && has_backedges;
8498 : : }
8499 : 5634002 : entry->flags |= EDGE_EXECUTABLE;
8500 : 5634002 : entry->dest->flags |= BB_EXECUTABLE;
8501 : :
8502 : : /* As heuristic to improve compile-time we handle only the N innermost
8503 : : loops and the outermost one optimistically. */
8504 : 5634002 : if (iterate)
8505 : : {
8506 : 3957990 : unsigned max_depth = param_rpo_vn_max_loop_depth;
8507 : 13231267 : for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
8508 : 2714594 : if (loop_depth (loop) > max_depth)
8509 : 1949 : for (unsigned i = 2;
8510 : 7740 : i < loop_depth (loop) - max_depth; ++i)
8511 : : {
8512 : 1949 : basic_block header = superloop_at_depth (loop, i)->header;
8513 : 1949 : bool non_latch_backedge = false;
8514 : 1949 : edge e;
8515 : 1949 : edge_iterator ei;
8516 : 5878 : FOR_EACH_EDGE (e, ei, header->preds)
8517 : 3929 : if (e->flags & EDGE_DFS_BACK)
8518 : : {
8519 : : /* There can be a non-latch backedge into the header
8520 : : which is part of an outer irreducible region. We
8521 : : cannot avoid iterating this block then. */
8522 : 1980 : if (!dominated_by_p (CDI_DOMINATORS,
8523 : 1980 : e->src, e->dest))
8524 : : {
8525 : 12 : if (dump_file && (dump_flags & TDF_DETAILS))
8526 : 0 : fprintf (dump_file, "non-latch backedge %d -> %d "
8527 : : "forces iteration of loop %d\n",
8528 : 0 : e->src->index, e->dest->index, loop->num);
8529 : : non_latch_backedge = true;
8530 : : }
8531 : : else
8532 : 1968 : e->flags |= EDGE_EXECUTABLE;
8533 : : }
8534 : 1949 : rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
8535 : 3957990 : }
8536 : : }
8537 : :
8538 : 5634002 : uint64_t nblk = 0;
8539 : 5634002 : int idx = 0;
8540 : 3957990 : if (iterate)
8541 : : /* Go and process all blocks, iterating as necessary. */
8542 : 43552608 : do
8543 : : {
8544 : 43552608 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8545 : :
8546 : : /* If the block has incoming backedges remember unwind state. This
8547 : : is required even for non-executable blocks since in irreducible
8548 : : regions we might reach them via the backedge and re-start iterating
8549 : : from there.
8550 : : Note we can individually mark blocks with incoming backedges to
8551 : : not iterate where we then handle PHIs conservatively. We do that
8552 : : heuristically to reduce compile-time for degenerate cases. */
8553 : 43552608 : if (rpo_state[idx].iterate)
8554 : : {
8555 : 3795701 : rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
8556 : 3795701 : rpo_state[idx].ref_top = last_inserted_ref;
8557 : 3795701 : rpo_state[idx].phi_top = last_inserted_phi;
8558 : 3795701 : rpo_state[idx].nary_top = last_inserted_nary;
8559 : 3795701 : rpo_state[idx].avail_top
8560 : 3795701 : = last_pushed_avail ? last_pushed_avail->avail : NULL;
8561 : : }
8562 : :
8563 : 43552608 : if (!(bb->flags & BB_EXECUTABLE))
8564 : : {
8565 : 686110 : if (dump_file && (dump_flags & TDF_DETAILS))
8566 : 2 : fprintf (dump_file, "Block %d: BB%d found not executable\n",
8567 : : idx, bb->index);
8568 : 686110 : idx++;
8569 : 2337825 : continue;
8570 : : }
8571 : :
8572 : 42866498 : if (dump_file && (dump_flags & TDF_DETAILS))
8573 : 396 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8574 : 42866498 : nblk++;
8575 : 85732996 : todo |= process_bb (avail, bb,
8576 : 42866498 : rpo_state[idx].visited != 0,
8577 : : rpo_state[idx].iterate,
8578 : : iterate, eliminate, do_region, exit_bbs, false);
8579 : 42866498 : rpo_state[idx].visited++;
8580 : :
8581 : : /* Verify if changed values flow over executable outgoing backedges
8582 : : and those change destination PHI values (that's the thing we
8583 : : can easily verify). Reduce over all such edges to the farthest
8584 : : away PHI. */
8585 : 42866498 : int iterate_to = -1;
8586 : 42866498 : edge_iterator ei;
8587 : 42866498 : edge e;
8588 : 103458975 : FOR_EACH_EDGE (e, ei, bb->succs)
8589 : 60592477 : if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
8590 : : == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
8591 : 3812798 : && rpo_state[bb_to_rpo[e->dest->index]].iterate)
8592 : : {
8593 : 3810062 : int destidx = bb_to_rpo[e->dest->index];
8594 : 3810062 : if (!rpo_state[destidx].visited)
8595 : : {
8596 : 163 : if (dump_file && (dump_flags & TDF_DETAILS))
8597 : 0 : fprintf (dump_file, "Unvisited destination %d\n",
8598 : : e->dest->index);
8599 : 163 : if (iterate_to == -1 || destidx < iterate_to)
8600 : 163 : iterate_to = destidx;
8601 : 163 : continue;
8602 : : }
8603 : 3809899 : if (dump_file && (dump_flags & TDF_DETAILS))
8604 : 64 : fprintf (dump_file, "Looking for changed values of backedge"
8605 : : " %d->%d destination PHIs\n",
8606 : 64 : e->src->index, e->dest->index);
8607 : 3809899 : vn_context_bb = e->dest;
8608 : 3809899 : gphi_iterator gsi;
8609 : 3809899 : for (gsi = gsi_start_phis (e->dest);
8610 : 8708351 : !gsi_end_p (gsi); gsi_next (&gsi))
8611 : : {
8612 : 6550282 : bool inserted = false;
8613 : : /* While we'd ideally just iterate on value changes
8614 : : we CSE PHIs and do that even across basic-block
8615 : : boundaries. So even hashtable state changes can
8616 : : be important (which is roughly equivalent to
8617 : : PHI argument value changes). To not excessively
8618 : : iterate because of that we track whether a PHI
8619 : : was CSEd to with GF_PLF_1. */
8620 : 6550282 : bool phival_changed;
8621 : 6550282 : if ((phival_changed = visit_phi (gsi.phi (),
8622 : : &inserted, false))
8623 : 7755970 : || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
8624 : : {
8625 : 1651830 : if (!phival_changed
8626 : 1651830 : && dump_file && (dump_flags & TDF_DETAILS))
8627 : 0 : fprintf (dump_file, "PHI was CSEd and hashtable "
8628 : : "state (changed)\n");
8629 : 1651830 : if (iterate_to == -1 || destidx < iterate_to)
8630 : 1651758 : iterate_to = destidx;
8631 : 1651830 : break;
8632 : : }
8633 : : }
8634 : 3809899 : vn_context_bb = NULL;
8635 : : }
8636 : 42866498 : if (iterate_to != -1)
8637 : : {
8638 : 1651715 : do_unwind (&rpo_state[iterate_to], avail);
8639 : 1651715 : idx = iterate_to;
8640 : 1651715 : if (dump_file && (dump_flags & TDF_DETAILS))
8641 : 24 : fprintf (dump_file, "Iterating to %d BB%d\n",
8642 : 24 : iterate_to, rpo[iterate_to]);
8643 : 1651715 : continue;
8644 : : }
8645 : :
8646 : 41214783 : idx++;
8647 : : }
8648 : 43552608 : while (idx < n);
8649 : :
8650 : : else /* !iterate */
8651 : : {
8652 : : /* Process all blocks greedily with a worklist that enforces RPO
8653 : : processing of reachable blocks. */
8654 : 1676012 : auto_bitmap worklist;
8655 : 1676012 : bitmap_set_bit (worklist, 0);
8656 : 14929949 : while (!bitmap_empty_p (worklist))
8657 : : {
8658 : 11577925 : int idx = bitmap_clear_first_set_bit (worklist);
8659 : 11577925 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8660 : 11577925 : gcc_assert ((bb->flags & BB_EXECUTABLE)
8661 : : && !rpo_state[idx].visited);
8662 : :
8663 : 11577925 : if (dump_file && (dump_flags & TDF_DETAILS))
8664 : 30880 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8665 : :
8666 : : /* When we run into predecessor edges where we cannot trust its
8667 : : executable state mark them executable so PHI processing will
8668 : : be conservative.
8669 : : ??? Do we need to force arguments flowing over that edge
8670 : : to be varying or will they even always be? */
8671 : 11577925 : edge_iterator ei;
8672 : 11577925 : edge e;
8673 : 27882203 : FOR_EACH_EDGE (e, ei, bb->preds)
8674 : 16304278 : if (!(e->flags & EDGE_EXECUTABLE)
8675 : 788334 : && (bb == entry->dest
8676 : 748312 : || (!rpo_state[bb_to_rpo[e->src->index]].visited
8677 : 726788 : && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
8678 : : >= (int)idx))))
8679 : : {
8680 : 752152 : if (dump_file && (dump_flags & TDF_DETAILS))
8681 : 9883 : fprintf (dump_file, "Cannot trust state of predecessor "
8682 : : "edge %d -> %d, marking executable\n",
8683 : 9883 : e->src->index, e->dest->index);
8684 : 752152 : e->flags |= EDGE_EXECUTABLE;
8685 : : }
8686 : :
8687 : 11577925 : nblk++;
8688 : 11577925 : todo |= process_bb (avail, bb, false, false, false, eliminate,
8689 : : do_region, exit_bbs,
8690 : 11577925 : skip_entry_phis && bb == entry->dest);
8691 : 11577925 : rpo_state[idx].visited++;
8692 : :
8693 : 28398183 : FOR_EACH_EDGE (e, ei, bb->succs)
8694 : 16820258 : if ((e->flags & EDGE_EXECUTABLE)
8695 : 16767705 : && e->dest->index != EXIT_BLOCK
8696 : 15675394 : && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
8697 : 31408306 : && !rpo_state[bb_to_rpo[e->dest->index]].visited)
8698 : 13839932 : bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
8699 : : }
8700 : 1676012 : }
8701 : :
8702 : : /* If statistics or dump file active. */
8703 : 5634002 : int nex = 0;
8704 : 5634002 : unsigned max_visited = 1;
8705 : 50278973 : for (int i = 0; i < n; ++i)
8706 : : {
8707 : 44644971 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8708 : 44644971 : if (bb->flags & BB_EXECUTABLE)
8709 : 44293691 : nex++;
8710 : 44644971 : statistics_histogram_event (cfun, "RPO block visited times",
8711 : 44644971 : rpo_state[i].visited);
8712 : 44644971 : if (rpo_state[i].visited > max_visited)
8713 : : max_visited = rpo_state[i].visited;
8714 : : }
8715 : 5634002 : unsigned nvalues = 0, navail = 0;
8716 : 151330087 : for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
8717 : 297026172 : i != vn_ssa_aux_hash->end (); ++i)
8718 : : {
8719 : 145696085 : nvalues++;
8720 : 145696085 : vn_avail *av = (*i)->avail;
8721 : 214605999 : while (av)
8722 : : {
8723 : 68909914 : navail++;
8724 : 68909914 : av = av->next;
8725 : : }
8726 : : }
8727 : 5634002 : statistics_counter_event (cfun, "RPO blocks", n);
8728 : 5634002 : statistics_counter_event (cfun, "RPO blocks visited", nblk);
8729 : 5634002 : statistics_counter_event (cfun, "RPO blocks executable", nex);
8730 : 5634002 : statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
8731 : 5634002 : statistics_histogram_event (cfun, "RPO num values", nvalues);
8732 : 5634002 : statistics_histogram_event (cfun, "RPO num avail", navail);
8733 : 5634002 : statistics_histogram_event (cfun, "RPO num lattice",
8734 : 5634002 : vn_ssa_aux_hash->elements ());
8735 : 5634002 : if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
8736 : : {
8737 : 9879 : fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
8738 : : " blocks in total discovering %d executable blocks iterating "
8739 : : "%d.%d times, a block was visited max. %u times\n",
8740 : : n, nblk, nex,
8741 : 9879 : (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
8742 : : max_visited);
8743 : 9879 : fprintf (dump_file, "RPO tracked %d values available at %d locations "
8744 : : "and %" PRIu64 " lattice elements\n",
8745 : 9879 : nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
8746 : : }
8747 : :
8748 : 5634002 : if (eliminate)
8749 : : {
8750 : : /* When !iterate we already performed elimination during the RPO
8751 : : walk. */
8752 : 4700103 : if (iterate)
8753 : : {
8754 : : /* Elimination for region-based VN needs to be done within the
8755 : : RPO walk. */
8756 : 3043372 : gcc_assert (! do_region);
8757 : : /* Note we can't use avail.walk here because that gets confused
8758 : : by the existing availability and it will be less efficient
8759 : : as well. */
8760 : 3043372 : todo |= eliminate_with_rpo_vn (NULL);
8761 : : }
8762 : : else
8763 : 1656731 : todo |= avail.eliminate_cleanup (do_region);
8764 : : }
8765 : :
8766 : 5634002 : vn_valueize = NULL;
8767 : 5634002 : rpo_avail = NULL;
8768 : :
8769 : 5634002 : XDELETEVEC (bb_to_rpo);
8770 : 5634002 : XDELETEVEC (rpo);
8771 : 5634002 : XDELETEVEC (rpo_state);
8772 : :
8773 : 5634002 : return todo;
8774 : 5634002 : }
8775 : :
8776 : : /* Region-based entry for RPO VN. Performs value-numbering and elimination
8777 : : on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
8778 : : the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
8779 : : are not considered.
8780 : : If ITERATE is true then treat backedges optimistically as not
8781 : : executed and iterate. If ELIMINATE is true then perform
8782 : : elimination, otherwise leave that to the caller.
8783 : : If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
8784 : : KIND specifies the amount of work done for handling memory operations. */
8785 : :
8786 : : unsigned
8787 : 576282 : do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
8788 : : bool iterate, bool eliminate, bool skip_entry_phis,
8789 : : vn_lookup_kind kind)
8790 : : {
8791 : 576282 : auto_timevar tv (TV_TREE_RPO_VN);
8792 : 576282 : unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
8793 : : skip_entry_phis, kind);
8794 : 576282 : free_rpo_vn ();
8795 : 1152564 : return todo;
8796 : 576282 : }
8797 : :
8798 : :
8799 : : namespace {
8800 : :
8801 : : const pass_data pass_data_fre =
8802 : : {
8803 : : GIMPLE_PASS, /* type */
8804 : : "fre", /* name */
8805 : : OPTGROUP_NONE, /* optinfo_flags */
8806 : : TV_TREE_FRE, /* tv_id */
8807 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
8808 : : 0, /* properties_provided */
8809 : : 0, /* properties_destroyed */
8810 : : 0, /* todo_flags_start */
8811 : : 0, /* todo_flags_finish */
8812 : : };
8813 : :
8814 : : class pass_fre : public gimple_opt_pass
8815 : : {
8816 : : public:
8817 : 1409570 : pass_fre (gcc::context *ctxt)
8818 : 2819140 : : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
8819 : : {}
8820 : :
8821 : : /* opt_pass methods: */
8822 : 1127656 : opt_pass * clone () final override { return new pass_fre (m_ctxt); }
8823 : 1409570 : void set_pass_param (unsigned int n, bool param) final override
8824 : : {
8825 : 1409570 : gcc_assert (n == 0);
8826 : 1409570 : may_iterate = param;
8827 : 1409570 : }
8828 : 4215056 : bool gate (function *) final override
8829 : : {
8830 : 4215056 : return flag_tree_fre != 0 && (may_iterate || optimize > 1);
8831 : : }
8832 : : unsigned int execute (function *) final override;
8833 : :
8834 : : private:
8835 : : bool may_iterate;
8836 : : }; // class pass_fre
8837 : :
8838 : : unsigned int
8839 : 4143102 : pass_fre::execute (function *fun)
8840 : : {
8841 : 4143102 : unsigned todo = 0;
8842 : :
8843 : : /* At -O[1g] use the cheap non-iterating mode. */
8844 : 4143102 : bool iterate_p = may_iterate && (optimize > 1);
8845 : 4143102 : calculate_dominance_info (CDI_DOMINATORS);
8846 : 4143102 : if (iterate_p)
8847 : 3043372 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
8848 : :
8849 : 4143102 : todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
8850 : 4143102 : free_rpo_vn ();
8851 : :
8852 : 4143102 : if (iterate_p)
8853 : 3043372 : loop_optimizer_finalize ();
8854 : :
8855 : 4143102 : if (scev_initialized_p ())
8856 : 17617 : scev_reset_htab ();
8857 : :
8858 : : /* For late FRE after IVOPTs and unrolling, see if we can
8859 : : remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8860 : 4143102 : if (!may_iterate)
8861 : 932052 : todo |= TODO_update_address_taken;
8862 : :
8863 : 4143102 : return todo;
8864 : : }
8865 : :
8866 : : } // anon namespace
8867 : :
8868 : : gimple_opt_pass *
8869 : 281914 : make_pass_fre (gcc::context *ctxt)
8870 : : {
8871 : 281914 : return new pass_fre (ctxt);
8872 : : }
8873 : :
8874 : : #undef BB_EXECUTABLE
|