Branch data Line data Source code
1 : : /* SCC value numbering for trees
2 : : Copyright (C) 2006-2025 Free Software Foundation, Inc.
3 : : Contributed by Daniel Berlin <dan@dberlin.org>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify
8 : : it under the terms of the GNU General Public License as published by
9 : : the Free Software Foundation; either version 3, or (at your option)
10 : : any later version.
11 : :
12 : : GCC is distributed in the hope that it will be useful,
13 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : : GNU General Public License for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "rtl.h"
26 : : #include "tree.h"
27 : : #include "gimple.h"
28 : : #include "ssa.h"
29 : : #include "expmed.h"
30 : : #include "insn-config.h"
31 : : #include "memmodel.h"
32 : : #include "emit-rtl.h"
33 : : #include "cgraph.h"
34 : : #include "gimple-pretty-print.h"
35 : : #include "splay-tree-utils.h"
36 : : #include "alias.h"
37 : : #include "fold-const.h"
38 : : #include "stor-layout.h"
39 : : #include "cfganal.h"
40 : : #include "tree-inline.h"
41 : : #include "internal-fn.h"
42 : : #include "gimple-iterator.h"
43 : : #include "gimple-fold.h"
44 : : #include "tree-eh.h"
45 : : #include "gimplify.h"
46 : : #include "flags.h"
47 : : #include "dojump.h"
48 : : #include "explow.h"
49 : : #include "calls.h"
50 : : #include "varasm.h"
51 : : #include "stmt.h"
52 : : #include "expr.h"
53 : : #include "tree-dfa.h"
54 : : #include "tree-ssa.h"
55 : : #include "dumpfile.h"
56 : : #include "cfgloop.h"
57 : : #include "tree-ssa-propagate.h"
58 : : #include "tree-cfg.h"
59 : : #include "domwalk.h"
60 : : #include "gimple-match.h"
61 : : #include "stringpool.h"
62 : : #include "attribs.h"
63 : : #include "tree-pass.h"
64 : : #include "statistics.h"
65 : : #include "langhooks.h"
66 : : #include "ipa-utils.h"
67 : : #include "dbgcnt.h"
68 : : #include "tree-cfgcleanup.h"
69 : : #include "tree-ssa-loop.h"
70 : : #include "tree-scalar-evolution.h"
71 : : #include "tree-ssa-loop-niter.h"
72 : : #include "builtins.h"
73 : : #include "fold-const-call.h"
74 : : #include "ipa-modref-tree.h"
75 : : #include "ipa-modref.h"
76 : : #include "tree-ssa-sccvn.h"
77 : : #include "alloc-pool.h"
78 : : #include "symbol-summary.h"
79 : : #include "sreal.h"
80 : : #include "ipa-cp.h"
81 : : #include "ipa-prop.h"
82 : : #include "target.h"
83 : :
84 : : /* This algorithm is based on the SCC algorithm presented by Keith
85 : : Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
86 : : (http://citeseer.ist.psu.edu/41805.html). In
87 : : straight line code, it is equivalent to a regular hash based value
88 : : numbering that is performed in reverse postorder.
89 : :
90 : : For code with cycles, there are two alternatives, both of which
91 : : require keeping the hashtables separate from the actual list of
92 : : value numbers for SSA names.
93 : :
94 : : 1. Iterate value numbering in an RPO walk of the blocks, removing
95 : : all the entries from the hashtable after each iteration (but
96 : : keeping the SSA name->value number mapping between iterations).
97 : : Iterate until it does not change.
98 : :
99 : : 2. Perform value numbering as part of an SCC walk on the SSA graph,
100 : : iterating only the cycles in the SSA graph until they do not change
101 : : (using a separate, optimistic hashtable for value numbering the SCC
102 : : operands).
103 : :
104 : : The second is not just faster in practice (because most SSA graph
105 : : cycles do not involve all the variables in the graph), it also has
106 : : some nice properties.
107 : :
108 : : One of these nice properties is that when we pop an SCC off the
109 : : stack, we are guaranteed to have processed all the operands coming from
110 : : *outside of that SCC*, so we do not need to do anything special to
111 : : ensure they have value numbers.
112 : :
113 : : Another nice property is that the SCC walk is done as part of a DFS
114 : : of the SSA graph, which makes it easy to perform combining and
115 : : simplifying operations at the same time.
116 : :
117 : : The code below is deliberately written in a way that makes it easy
118 : : to separate the SCC walk from the other work it does.
119 : :
120 : : In order to propagate constants through the code, we track which
121 : : expressions contain constants, and use those while folding. In
122 : : theory, we could also track expressions whose value numbers are
123 : : replaced, in case we end up folding based on expression
124 : : identities.
125 : :
126 : : In order to value number memory, we assign value numbers to vuses.
127 : : This enables us to note that, for example, stores to the same
128 : : address of the same value from the same starting memory states are
129 : : equivalent.
130 : : TODO:
131 : :
132 : : 1. We can iterate only the changing portions of the SCC's, but
133 : : I have not seen an SCC big enough for this to be a win.
134 : : 2. If you differentiate between phi nodes for loops and phi nodes
135 : : for if-then-else, you can properly consider phi nodes in different
136 : : blocks for equivalence.
137 : : 3. We could value number vuses in more cases, particularly, whole
138 : : structure copies.
139 : : */
140 : :
141 : : /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
142 : : #define BB_EXECUTABLE BB_VISITED
143 : :
144 : : static vn_lookup_kind default_vn_walk_kind;
145 : :
146 : : /* vn_nary_op hashtable helpers. */
147 : :
148 : : struct vn_nary_op_hasher : nofree_ptr_hash <vn_nary_op_s>
149 : : {
150 : : typedef vn_nary_op_s *compare_type;
151 : : static inline hashval_t hash (const vn_nary_op_s *);
152 : : static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
153 : : };
154 : :
155 : : /* Return the computed hashcode for nary operation P1. */
156 : :
157 : : inline hashval_t
158 : 759977786 : vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
159 : : {
160 : 759977786 : 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 : 963883592 : vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
168 : : {
169 : 963883592 : 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 : 26756851 : vn_phi_hasher::hash (const vn_phi_s *vp1)
191 : : {
192 : 26756851 : return vp1->hashcode;
193 : : }
194 : :
195 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
196 : :
197 : : inline bool
198 : 47910231 : vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
199 : : {
200 : 47910231 : 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 : 23055822 : vn_reference_op_eq (const void *p1, const void *p2)
212 : : {
213 : 23055822 : const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
214 : 23055822 : const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
215 : :
216 : 23055822 : return (vro1->opcode == vro2->opcode
217 : : /* We do not care for differences in type qualification. */
218 : 23053873 : && (vro1->type == vro2->type
219 : 1040296 : || (vro1->type && vro2->type
220 : 1040296 : && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
221 : 1040296 : TYPE_MAIN_VARIANT (vro2->type))))
222 : 22147966 : && expressions_equal_p (vro1->op0, vro2->op0)
223 : 22131011 : && expressions_equal_p (vro1->op1, vro2->op1)
224 : 22131011 : && expressions_equal_p (vro1->op2, vro2->op2)
225 : 45186833 : && (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 : 3708143222 : vn_reference_hasher::hash (const vn_reference_s *vr1)
249 : : {
250 : 3708143222 : return vr1->hashcode;
251 : : }
252 : :
253 : : inline bool
254 : 4403205441 : vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
255 : : {
256 : 4403205441 : 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 : 305 : print_vn_reference_ops (FILE *outfile, const vec<vn_reference_op_s> ops)
266 : : {
267 : 305 : vn_reference_op_t vro;
268 : 305 : unsigned int i;
269 : 305 : fprintf (outfile, "{");
270 : 1346 : for (i = 0; ops.iterate (i, &vro); i++)
271 : : {
272 : 1041 : bool closebrace = false;
273 : 1041 : if (vro->opcode != SSA_NAME
274 : 839 : && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
275 : : {
276 : 839 : fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
277 : 839 : if (vro->op0 || vro->opcode == CALL_EXPR)
278 : : {
279 : 839 : fprintf (outfile, "<");
280 : 839 : closebrace = true;
281 : : }
282 : : }
283 : 1041 : if (vro->op0 || vro->opcode == CALL_EXPR)
284 : : {
285 : 1041 : if (!vro->op0)
286 : 0 : fprintf (outfile, internal_fn_name ((internal_fn)vro->clique));
287 : : else
288 : 1041 : print_generic_expr (outfile, vro->op0);
289 : 1041 : if (vro->op1)
290 : : {
291 : 173 : fprintf (outfile, ",");
292 : 173 : print_generic_expr (outfile, vro->op1);
293 : : }
294 : 1041 : if (vro->op2)
295 : : {
296 : 173 : fprintf (outfile, ",");
297 : 173 : print_generic_expr (outfile, vro->op2);
298 : : }
299 : : }
300 : 1041 : if (closebrace)
301 : 839 : fprintf (outfile, ">");
302 : 1041 : if (i != ops.length () - 1)
303 : 736 : fprintf (outfile, ",");
304 : : }
305 : 305 : fprintf (outfile, "}");
306 : 305 : }
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 : 12015738 : vn_constant_hasher::hash (const vn_constant_s *vc1)
337 : : {
338 : 12015738 : return vc1->hashcode;
339 : : }
340 : :
341 : : /* Hash table equality function for vn_constant_t. */
342 : :
343 : : inline bool
344 : 14513804 : vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
345 : : {
346 : 14513804 : if (vc1->hashcode != vc2->hashcode)
347 : : return false;
348 : :
349 : 2194536 : return vn_constant_eq_with_type (vc1->constant, vc2->constant);
350 : : }
351 : :
352 : : static hash_table<vn_constant_hasher> *constant_to_value_id;
353 : :
354 : :
355 : : /* Obstack we allocate the vn-tables elements from. */
356 : : static obstack vn_tables_obstack;
357 : : /* Special obstack we never unwind. */
358 : : static obstack vn_tables_insert_obstack;
359 : :
360 : : static vn_reference_t last_inserted_ref;
361 : : static vn_phi_t last_inserted_phi;
362 : : static vn_nary_op_t last_inserted_nary;
363 : : static vn_ssa_aux_t last_pushed_avail;
364 : :
365 : : /* Valid hashtables storing information we have proven to be
366 : : correct. */
367 : : static vn_tables_t valid_info;
368 : :
369 : : /* Global RPO state for access from hooks. */
370 : : static class eliminate_dom_walker *rpo_avail;
371 : : basic_block vn_context_bb;
372 : :
373 : :
374 : : /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
375 : : an SSA name, otherwise just return it. */
376 : : tree (*vn_valueize) (tree);
377 : : static tree
378 : 77344 : vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
379 : : {
380 : 77344 : basic_block saved_vn_context_bb = vn_context_bb;
381 : : /* Look for sth available at the definition block of the argument.
382 : : This avoids inconsistencies between availability there which
383 : : decides if the stmt can be removed and availability at the
384 : : use site. The SSA property ensures that things available
385 : : at the definition are also available at uses. */
386 : 77344 : if (!SSA_NAME_IS_DEFAULT_DEF (t))
387 : 74687 : vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
388 : 77344 : tree res = vn_valueize (t);
389 : 77344 : vn_context_bb = saved_vn_context_bb;
390 : 77344 : return res;
391 : : }
392 : :
393 : :
394 : : /* This represents the top of the VN lattice, which is the universal
395 : : value. */
396 : :
397 : : tree VN_TOP;
398 : :
399 : : /* Unique counter for our value ids. */
400 : :
401 : : static unsigned int next_value_id;
402 : : static int next_constant_value_id;
403 : :
404 : :
405 : : /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
406 : : are allocated on an obstack for locality reasons, and to free them
407 : : without looping over the vec. */
408 : :
409 : : struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
410 : : {
411 : : typedef vn_ssa_aux_t value_type;
412 : : typedef tree compare_type;
413 : : static inline hashval_t hash (const value_type &);
414 : : static inline bool equal (const value_type &, const compare_type &);
415 : : static inline void mark_deleted (value_type &) {}
416 : : static const bool empty_zero_p = true;
417 : 0 : static inline void mark_empty (value_type &e) { e = NULL; }
418 : : static inline bool is_deleted (value_type &) { return false; }
419 : >12688*10^7 : static inline bool is_empty (value_type &e) { return e == NULL; }
420 : : };
421 : :
422 : : hashval_t
423 : 41545127984 : vn_ssa_aux_hasher::hash (const value_type &entry)
424 : : {
425 : 41545127984 : return SSA_NAME_VERSION (entry->name);
426 : : }
427 : :
428 : : bool
429 : 47561173499 : vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
430 : : {
431 : 47561173499 : return name == entry->name;
432 : : }
433 : :
434 : : static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
435 : : typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
436 : : static struct obstack vn_ssa_aux_obstack;
437 : :
438 : : static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
439 : : static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
440 : : vn_nary_op_table_type *);
441 : : static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
442 : : enum tree_code, tree, tree *);
443 : : static tree vn_lookup_simplify_result (gimple_match_op *);
444 : : static vn_reference_t vn_reference_lookup_or_insert_for_pieces
445 : : (tree, alias_set_type, alias_set_type, poly_int64, poly_int64, tree,
446 : : vec<vn_reference_op_s, va_heap>, tree);
447 : :
448 : : /* Return whether there is value numbering information for a given SSA name. */
449 : :
450 : : bool
451 : 5133401 : has_VN_INFO (tree name)
452 : : {
453 : 5133401 : return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
454 : : }
455 : :
456 : : vn_ssa_aux_t
457 : 3563304114 : VN_INFO (tree name)
458 : : {
459 : 3563304114 : vn_ssa_aux_t *res
460 : 3563304114 : = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
461 : : INSERT);
462 : 3563304114 : if (*res != NULL)
463 : : return *res;
464 : :
465 : 172088380 : vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
466 : 172088380 : memset (newinfo, 0, sizeof (struct vn_ssa_aux));
467 : 172088380 : newinfo->name = name;
468 : 172088380 : newinfo->valnum = VN_TOP;
469 : : /* We are using the visited flag to handle uses with defs not within the
470 : : region being value-numbered. */
471 : 172088380 : newinfo->visited = false;
472 : :
473 : : /* Given we create the VN_INFOs on-demand now we have to do initialization
474 : : different than VN_TOP here. */
475 : 172088380 : if (SSA_NAME_IS_DEFAULT_DEF (name))
476 : 9232657 : switch (TREE_CODE (SSA_NAME_VAR (name)))
477 : : {
478 : 1686015 : case VAR_DECL:
479 : : /* All undefined vars are VARYING. */
480 : 1686015 : newinfo->valnum = name;
481 : 1686015 : newinfo->visited = true;
482 : 1686015 : break;
483 : :
484 : 7486302 : case PARM_DECL:
485 : : /* Parameters are VARYING but we can record a condition
486 : : if we know it is a non-NULL pointer. */
487 : 7486302 : newinfo->visited = true;
488 : 7486302 : newinfo->valnum = name;
489 : 11482848 : if (POINTER_TYPE_P (TREE_TYPE (name))
490 : 8623295 : && nonnull_arg_p (SSA_NAME_VAR (name)))
491 : : {
492 : 2362542 : tree ops[2];
493 : 2362542 : ops[0] = name;
494 : 2362542 : ops[1] = build_int_cst (TREE_TYPE (name), 0);
495 : 2362542 : vn_nary_op_t nary;
496 : : /* Allocate from non-unwinding stack. */
497 : 2362542 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
498 : 2362542 : init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
499 : : boolean_type_node, ops);
500 : 2362542 : nary->predicated_values = 0;
501 : 2362542 : nary->u.result = boolean_true_node;
502 : 2362542 : vn_nary_op_insert_into (nary, valid_info->nary);
503 : 2362542 : gcc_assert (nary->unwind_to == NULL);
504 : : /* Also do not link it into the undo chain. */
505 : 2362542 : last_inserted_nary = nary->next;
506 : 2362542 : nary->next = (vn_nary_op_t)(void *)-1;
507 : 2362542 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
508 : 2362542 : init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
509 : : boolean_type_node, ops);
510 : 2362542 : nary->predicated_values = 0;
511 : 2362542 : nary->u.result = boolean_false_node;
512 : 2362542 : vn_nary_op_insert_into (nary, valid_info->nary);
513 : 2362542 : gcc_assert (nary->unwind_to == NULL);
514 : 2362542 : last_inserted_nary = nary->next;
515 : 2362542 : nary->next = (vn_nary_op_t)(void *)-1;
516 : 2362542 : if (dump_file && (dump_flags & TDF_DETAILS))
517 : : {
518 : 35 : fprintf (dump_file, "Recording ");
519 : 35 : print_generic_expr (dump_file, name, TDF_SLIM);
520 : 35 : fprintf (dump_file, " != 0\n");
521 : : }
522 : : }
523 : : break;
524 : :
525 : 60340 : case RESULT_DECL:
526 : : /* If the result is passed by invisible reference the default
527 : : def is initialized, otherwise it's uninitialized. Still
528 : : undefined is varying. */
529 : 60340 : newinfo->visited = true;
530 : 60340 : newinfo->valnum = name;
531 : 60340 : break;
532 : :
533 : 0 : default:
534 : 0 : gcc_unreachable ();
535 : : }
536 : : return newinfo;
537 : : }
538 : :
539 : : /* Return the SSA value of X. */
540 : :
541 : : inline tree
542 : 3319810125 : SSA_VAL (tree x, bool *visited = NULL)
543 : : {
544 : 3319810125 : vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
545 : 3319810125 : if (visited)
546 : 1335952411 : *visited = tem && tem->visited;
547 : 3319810125 : return tem && tem->visited ? tem->valnum : x;
548 : : }
549 : :
550 : : /* Return the SSA value of the VUSE x, supporting released VDEFs
551 : : during elimination which will value-number the VDEF to the
552 : : associated VUSE (but not substitute in the whole lattice). */
553 : :
554 : : static inline tree
555 : 1220654094 : vuse_ssa_val (tree x)
556 : : {
557 : 1220654094 : if (!x)
558 : : return NULL_TREE;
559 : :
560 : 1217298854 : do
561 : : {
562 : 1217298854 : x = SSA_VAL (x);
563 : 1217298854 : gcc_assert (x != VN_TOP);
564 : : }
565 : 1217298854 : while (SSA_NAME_IN_FREE_LIST (x));
566 : :
567 : : return x;
568 : : }
569 : :
570 : : /* Similar to the above but used as callback for walk_non_aliased_vuses
571 : : and thus should stop at unvisited VUSE to not walk across region
572 : : boundaries. */
573 : :
574 : : static tree
575 : 1023780147 : vuse_valueize (tree vuse)
576 : : {
577 : 1023780147 : do
578 : : {
579 : 1023780147 : bool visited;
580 : 1023780147 : vuse = SSA_VAL (vuse, &visited);
581 : 1023780147 : if (!visited)
582 : 15017329 : return NULL_TREE;
583 : 1008762818 : gcc_assert (vuse != VN_TOP);
584 : : }
585 : 1008762818 : while (SSA_NAME_IN_FREE_LIST (vuse));
586 : : return vuse;
587 : : }
588 : :
589 : :
590 : : /* Return the vn_kind the expression computed by the stmt should be
591 : : associated with. */
592 : :
593 : : enum vn_kind
594 : 101398180 : vn_get_stmt_kind (gimple *stmt)
595 : : {
596 : 101398180 : switch (gimple_code (stmt))
597 : : {
598 : : case GIMPLE_CALL:
599 : : return VN_REFERENCE;
600 : : case GIMPLE_PHI:
601 : : return VN_PHI;
602 : 101398180 : case GIMPLE_ASSIGN:
603 : 101398180 : {
604 : 101398180 : enum tree_code code = gimple_assign_rhs_code (stmt);
605 : 101398180 : tree rhs1 = gimple_assign_rhs1 (stmt);
606 : 101398180 : switch (get_gimple_rhs_class (code))
607 : : {
608 : : case GIMPLE_UNARY_RHS:
609 : : case GIMPLE_BINARY_RHS:
610 : : case GIMPLE_TERNARY_RHS:
611 : : return VN_NARY;
612 : 47460618 : case GIMPLE_SINGLE_RHS:
613 : 47460618 : switch (TREE_CODE_CLASS (code))
614 : : {
615 : 35685233 : case tcc_reference:
616 : : /* VOP-less references can go through unary case. */
617 : 35685233 : if ((code == REALPART_EXPR
618 : : || code == IMAGPART_EXPR
619 : 35685233 : || code == VIEW_CONVERT_EXPR
620 : 35685233 : || code == BIT_FIELD_REF)
621 : 35685233 : && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
622 : 756222 : || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
623 : 1893192 : return VN_NARY;
624 : :
625 : : /* Fallthrough. */
626 : : case tcc_declaration:
627 : : return VN_REFERENCE;
628 : :
629 : : case tcc_constant:
630 : : return VN_CONSTANT;
631 : :
632 : 5840582 : default:
633 : 5840582 : if (code == ADDR_EXPR)
634 : 3213216 : return (is_gimple_min_invariant (rhs1)
635 : 3213216 : ? VN_CONSTANT : VN_REFERENCE);
636 : 2627366 : else if (code == CONSTRUCTOR)
637 : : return VN_NARY;
638 : : return VN_NONE;
639 : : }
640 : : default:
641 : : return VN_NONE;
642 : : }
643 : : }
644 : : default:
645 : : return VN_NONE;
646 : : }
647 : : }
648 : :
649 : : /* Lookup a value id for CONSTANT and return it. If it does not
650 : : exist returns 0. */
651 : :
652 : : unsigned int
653 : 0 : get_constant_value_id (tree constant)
654 : : {
655 : 0 : vn_constant_s **slot;
656 : 0 : struct vn_constant_s vc;
657 : :
658 : 0 : vc.hashcode = vn_hash_constant_with_type (constant);
659 : 0 : vc.constant = constant;
660 : 0 : slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
661 : 0 : if (slot)
662 : 0 : return (*slot)->value_id;
663 : : return 0;
664 : : }
665 : :
666 : : /* Lookup a value id for CONSTANT, and if it does not exist, create a
667 : : new one and return it. If it does exist, return it. */
668 : :
669 : : unsigned int
670 : 28434176 : get_or_alloc_constant_value_id (tree constant)
671 : : {
672 : 28434176 : vn_constant_s **slot;
673 : 28434176 : struct vn_constant_s vc;
674 : 28434176 : vn_constant_t vcp;
675 : :
676 : : /* If the hashtable isn't initialized we're not running from PRE and thus
677 : : do not need value-ids. */
678 : 28434176 : if (!constant_to_value_id)
679 : : return 0;
680 : :
681 : 4698107 : vc.hashcode = vn_hash_constant_with_type (constant);
682 : 4698107 : vc.constant = constant;
683 : 4698107 : slot = constant_to_value_id->find_slot (&vc, INSERT);
684 : 4698107 : if (*slot)
685 : 2178496 : return (*slot)->value_id;
686 : :
687 : 2519611 : vcp = XNEW (struct vn_constant_s);
688 : 2519611 : vcp->hashcode = vc.hashcode;
689 : 2519611 : vcp->constant = constant;
690 : 2519611 : vcp->value_id = get_next_constant_value_id ();
691 : 2519611 : *slot = vcp;
692 : 2519611 : return vcp->value_id;
693 : : }
694 : :
695 : : /* Compute the hash for a reference operand VRO1. */
696 : :
697 : : static void
698 : 131806188 : vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
699 : : {
700 : 131806188 : hstate.add_int (vro1->opcode);
701 : 131806188 : if (vro1->opcode == CALL_EXPR && !vro1->op0)
702 : 512684 : hstate.add_int (vro1->clique);
703 : 131806188 : if (vro1->op0)
704 : 125731897 : inchash::add_expr (vro1->op0, hstate);
705 : 131806188 : if (vro1->op1)
706 : 11245439 : inchash::add_expr (vro1->op1, hstate);
707 : 131806188 : if (vro1->op2)
708 : 13003209 : inchash::add_expr (vro1->op2, hstate);
709 : 131806188 : }
710 : :
711 : : /* Compute a hash for the reference operation VR1 and return it. */
712 : :
713 : : static hashval_t
714 : 197212002 : vn_reference_compute_hash (const vn_reference_t vr1)
715 : : {
716 : 197212002 : inchash::hash hstate;
717 : 197212002 : hashval_t result;
718 : 197212002 : int i;
719 : 197212002 : vn_reference_op_t vro;
720 : 197212002 : poly_int64 off = -1;
721 : 197212002 : bool deref = false;
722 : :
723 : 802439750 : FOR_EACH_VEC_ELT (vr1->operands, i, vro)
724 : : {
725 : 605227748 : if (vro->opcode == MEM_REF)
726 : : deref = true;
727 : 418488715 : else if (vro->opcode != ADDR_EXPR)
728 : 292005072 : deref = false;
729 : 605227748 : if (maybe_ne (vro->off, -1))
730 : : {
731 : 354990426 : if (known_eq (off, -1))
732 : 188884281 : off = 0;
733 : 605227748 : off += vro->off;
734 : : }
735 : : else
736 : : {
737 : 250237322 : if (maybe_ne (off, -1)
738 : 250237322 : && maybe_ne (off, 0))
739 : 99544779 : hstate.add_poly_int (off);
740 : 250237322 : off = -1;
741 : 250237322 : if (deref
742 : 118674871 : && vro->opcode == ADDR_EXPR)
743 : : {
744 : 118431134 : if (vro->op0)
745 : : {
746 : 118431134 : tree op = TREE_OPERAND (vro->op0, 0);
747 : 118431134 : hstate.add_int (TREE_CODE (op));
748 : 118431134 : inchash::add_expr (op, hstate);
749 : : }
750 : : }
751 : : else
752 : 131806188 : vn_reference_op_compute_hash (vro, hstate);
753 : : }
754 : : }
755 : : /* Do not hash vr1->offset or vr1->max_size, we want to get collisions
756 : : to be able to identify compatible results. */
757 : 197212002 : result = hstate.end ();
758 : : /* ??? We would ICE later if we hash instead of adding that in. */
759 : 197212002 : if (vr1->vuse)
760 : 192505198 : result += SSA_NAME_VERSION (vr1->vuse);
761 : :
762 : 197212002 : return result;
763 : : }
764 : :
765 : : /* Return true if reference operations VR1 and VR2 are equivalent. This
766 : : means they have the same set of operands and vuses. */
767 : :
768 : : bool
769 : 4398617892 : vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
770 : : {
771 : 4398617892 : unsigned i, j;
772 : :
773 : : /* Early out if this is not a hash collision. */
774 : 4398617892 : if (vr1->hashcode != vr2->hashcode)
775 : : return false;
776 : :
777 : : /* The VOP needs to be the same. */
778 : 17712393 : if (vr1->vuse != vr2->vuse)
779 : : return false;
780 : :
781 : : /* The offset/max_size used for the ao_ref during lookup has to be
782 : : the same. */
783 : 17711943 : if (maybe_ne (vr1->offset, vr2->offset)
784 : 17711943 : || maybe_ne (vr1->max_size, vr2->max_size))
785 : : {
786 : : /* But nothing known in the prevailing entry is OK to be used. */
787 : 6632234 : if (maybe_ne (vr1->offset, 0) || known_size_p (vr1->max_size))
788 : : return false;
789 : : }
790 : :
791 : : /* If the operands are the same we are done. */
792 : 35349754 : if (vr1->operands == vr2->operands)
793 : : return true;
794 : :
795 : 16858436 : if (!vr1->type || !vr2->type)
796 : : {
797 : 548139 : if (vr1->type != vr2->type)
798 : : return false;
799 : : }
800 : 16310297 : else if (vr1->type == vr2->type)
801 : : ;
802 : 2059512 : else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
803 : 2059512 : || (COMPLETE_TYPE_P (vr1->type)
804 : 2059512 : && !expressions_equal_p (TYPE_SIZE (vr1->type),
805 : 2059512 : TYPE_SIZE (vr2->type))))
806 : 742734 : return false;
807 : 1316778 : else if (vr1->operands[0].opcode == CALL_EXPR
808 : 1316778 : && !types_compatible_p (vr1->type, vr2->type))
809 : : return false;
810 : 1316778 : else if (INTEGRAL_TYPE_P (vr1->type)
811 : 538212 : && INTEGRAL_TYPE_P (vr2->type))
812 : : {
813 : 530471 : if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
814 : : return false;
815 : : }
816 : 786307 : else if (INTEGRAL_TYPE_P (vr1->type)
817 : 786307 : && (TYPE_PRECISION (vr1->type)
818 : 7741 : != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
819 : : return false;
820 : 786185 : else if (INTEGRAL_TYPE_P (vr2->type)
821 : 786185 : && (TYPE_PRECISION (vr2->type)
822 : 7565 : != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
823 : : return false;
824 : 7435 : else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
825 : 785591 : && VECTOR_BOOLEAN_TYPE_P (vr2->type))
826 : : {
827 : : /* Vector boolean types can have padding, verify we are dealing with
828 : : the same number of elements, aka the precision of the types.
829 : : For example, In most architecture the precision_size of vbool*_t
830 : : types are caculated like below:
831 : : precision_size = type_size * 8
832 : :
833 : : Unfortunately, the RISC-V will adjust the precision_size for the
834 : : vbool*_t in order to align the ISA as below:
835 : : type_size = [1, 1, 1, 1, 2, 4, 8]
836 : : precision_size = [1, 2, 4, 8, 16, 32, 64]
837 : :
838 : : Then the precision_size of RISC-V vbool*_t will not be the multiple
839 : : of the type_size. We take care of this case consolidated here. */
840 : 0 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1->type),
841 : 0 : TYPE_VECTOR_SUBPARTS (vr2->type)))
842 : : return false;
843 : : }
844 : 785591 : else if (TYPE_MODE (vr1->type) != TYPE_MODE (vr2->type)
845 : 785591 : && (!mode_can_transfer_bits (TYPE_MODE (vr1->type))
846 : 9530 : || !mode_can_transfer_bits (TYPE_MODE (vr2->type))))
847 : 1096 : return false;
848 : :
849 : : i = 0;
850 : : j = 0;
851 : 19461297 : do
852 : : {
853 : 19461297 : poly_int64 off1 = 0, off2 = 0;
854 : 19461297 : vn_reference_op_t vro1, vro2;
855 : 19461297 : vn_reference_op_s tem1, tem2;
856 : 19461297 : bool deref1 = false, deref2 = false;
857 : 19461297 : bool reverse1 = false, reverse2 = false;
858 : 66094175 : for (; vr1->operands.iterate (i, &vro1); i++)
859 : : {
860 : 46632878 : if (vro1->opcode == MEM_REF)
861 : : deref1 = true;
862 : : /* Do not look through a storage order barrier. */
863 : 31918004 : else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
864 : 6533 : return false;
865 : 46632878 : reverse1 |= vro1->reverse;
866 : 46632878 : if (known_eq (vro1->off, -1))
867 : : break;
868 : 27171581 : off1 += vro1->off;
869 : : }
870 : 46744954 : for (; vr2->operands.iterate (j, &vro2); j++)
871 : : {
872 : 46744954 : if (vro2->opcode == MEM_REF)
873 : : deref2 = true;
874 : : /* Do not look through a storage order barrier. */
875 : 32030070 : else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
876 : : return false;
877 : 46744954 : reverse2 |= vro2->reverse;
878 : 46744954 : if (known_eq (vro2->off, -1))
879 : : break;
880 : 27283657 : off2 += vro2->off;
881 : : }
882 : 19461297 : if (maybe_ne (off1, off2) || reverse1 != reverse2)
883 : : return false;
884 : 19461107 : if (deref1 && vro1->opcode == ADDR_EXPR)
885 : : {
886 : 8182172 : memset (&tem1, 0, sizeof (tem1));
887 : 8182172 : tem1.op0 = TREE_OPERAND (vro1->op0, 0);
888 : 8182172 : tem1.type = TREE_TYPE (tem1.op0);
889 : 8182172 : tem1.opcode = TREE_CODE (tem1.op0);
890 : 8182172 : vro1 = &tem1;
891 : 8182172 : deref1 = false;
892 : : }
893 : 19461107 : if (deref2 && vro2->opcode == ADDR_EXPR)
894 : : {
895 : 8182182 : memset (&tem2, 0, sizeof (tem2));
896 : 8182182 : tem2.op0 = TREE_OPERAND (vro2->op0, 0);
897 : 8182182 : tem2.type = TREE_TYPE (tem2.op0);
898 : 8182182 : tem2.opcode = TREE_CODE (tem2.op0);
899 : 8182182 : vro2 = &tem2;
900 : 8182182 : deref2 = false;
901 : : }
902 : 19461107 : if (deref1 != deref2)
903 : : return false;
904 : 19461107 : if (!vn_reference_op_eq (vro1, vro2))
905 : : return false;
906 : 19454764 : ++j;
907 : 19454764 : ++i;
908 : : }
909 : 38909528 : while (vr1->operands.length () != i
910 : 58364292 : || vr2->operands.length () != j);
911 : :
912 : : return true;
913 : : }
914 : :
915 : : /* Copy the operations present in load/store REF into RESULT, a vector of
916 : : vn_reference_op_s's. */
917 : :
918 : : static void
919 : 218469984 : copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
920 : : {
921 : : /* For non-calls, store the information that makes up the address. */
922 : 218469984 : tree orig = ref;
923 : 755526403 : while (ref)
924 : : {
925 : 537056419 : vn_reference_op_s temp;
926 : :
927 : 537056419 : memset (&temp, 0, sizeof (temp));
928 : 537056419 : temp.type = TREE_TYPE (ref);
929 : 537056419 : temp.opcode = TREE_CODE (ref);
930 : 537056419 : temp.off = -1;
931 : :
932 : 537056419 : switch (temp.opcode)
933 : : {
934 : 15107295 : case MODIFY_EXPR:
935 : 15107295 : temp.op0 = TREE_OPERAND (ref, 1);
936 : 15107295 : break;
937 : 137 : case WITH_SIZE_EXPR:
938 : 137 : temp.op0 = TREE_OPERAND (ref, 1);
939 : 137 : temp.off = 0;
940 : 137 : break;
941 : 114345237 : case MEM_REF:
942 : : /* The base address gets its own vn_reference_op_s structure. */
943 : 114345237 : temp.op0 = TREE_OPERAND (ref, 1);
944 : 114345237 : if (!mem_ref_offset (ref).to_shwi (&temp.off))
945 : 0 : temp.off = -1;
946 : 114345237 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
947 : 114345237 : temp.base = MR_DEPENDENCE_BASE (ref);
948 : 114345237 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
949 : 114345237 : break;
950 : 2438803 : case TARGET_MEM_REF:
951 : : /* The base address gets its own vn_reference_op_s structure. */
952 : 2438803 : temp.op0 = TMR_INDEX (ref);
953 : 2438803 : temp.op1 = TMR_STEP (ref);
954 : 2438803 : temp.op2 = TMR_OFFSET (ref);
955 : 2438803 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
956 : 2438803 : temp.base = MR_DEPENDENCE_BASE (ref);
957 : 2438803 : result->safe_push (temp);
958 : 2438803 : memset (&temp, 0, sizeof (temp));
959 : 2438803 : temp.type = NULL_TREE;
960 : 2438803 : temp.opcode = ERROR_MARK;
961 : 2438803 : temp.op0 = TMR_INDEX2 (ref);
962 : 2438803 : temp.off = -1;
963 : 2438803 : break;
964 : 814767 : case BIT_FIELD_REF:
965 : : /* Record bits, position and storage order. */
966 : 814767 : temp.op0 = TREE_OPERAND (ref, 1);
967 : 814767 : temp.op1 = TREE_OPERAND (ref, 2);
968 : 1628829 : if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
969 : 705 : temp.off = -1;
970 : 814767 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
971 : 814767 : break;
972 : 142339791 : case COMPONENT_REF:
973 : : /* The field decl is enough to unambiguously specify the field,
974 : : so use its type here. */
975 : 142339791 : temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
976 : 142339791 : temp.op0 = TREE_OPERAND (ref, 1);
977 : 142339791 : temp.op1 = TREE_OPERAND (ref, 2);
978 : 284677238 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
979 : 284675422 : && TYPE_REVERSE_STORAGE_ORDER
980 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
981 : 142339791 : {
982 : 142339791 : tree this_offset = component_ref_field_offset (ref);
983 : 142339791 : if (this_offset
984 : 142339791 : && poly_int_tree_p (this_offset))
985 : : {
986 : 142337709 : tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
987 : 142337709 : if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
988 : : {
989 : 141728030 : poly_offset_int off
990 : 141728030 : = (wi::to_poly_offset (this_offset)
991 : 141728030 : + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
992 : : /* Prohibit value-numbering zero offset components
993 : : of addresses the same before the pass folding
994 : : __builtin_object_size had a chance to run. Likewise
995 : : for components of zero size at arbitrary offset. */
996 : 141728030 : if (TREE_CODE (orig) != ADDR_EXPR
997 : 4778829 : || (TYPE_SIZE (temp.type)
998 : 4766374 : && integer_nonzerop (TYPE_SIZE (temp.type))
999 : 6209776 : && maybe_ne (off, 0))
1000 : 144648515 : || (cfun->curr_properties & PROP_objsz))
1001 : 140282521 : off.to_shwi (&temp.off);
1002 : : }
1003 : : }
1004 : : }
1005 : : break;
1006 : 37068567 : case ARRAY_RANGE_REF:
1007 : 37068567 : case ARRAY_REF:
1008 : 37068567 : {
1009 : 37068567 : tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
1010 : : /* Record index as operand. */
1011 : 37068567 : temp.op0 = TREE_OPERAND (ref, 1);
1012 : : /* Always record lower bounds and element size. */
1013 : 37068567 : temp.op1 = array_ref_low_bound (ref);
1014 : : /* But record element size in units of the type alignment. */
1015 : 37068567 : temp.op2 = TREE_OPERAND (ref, 3);
1016 : 37068567 : temp.align = eltype->type_common.align;
1017 : 37068567 : if (! temp.op2)
1018 : 36860187 : temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
1019 : : size_int (TYPE_ALIGN_UNIT (eltype)));
1020 : : /* Prohibit value-numbering addresses of one-after-the-last
1021 : : element ARRAY_REFs the same as addresses of other components
1022 : : before the pass folding __builtin_object_size had a chance
1023 : : to run. */
1024 : 37068567 : bool avoid_oob = true;
1025 : 37068567 : if (TREE_CODE (orig) != ADDR_EXPR
1026 : 409681 : || cfun->curr_properties & PROP_objsz)
1027 : : avoid_oob = false;
1028 : 193269 : else if (poly_int_tree_p (temp.op0))
1029 : : {
1030 : 66793 : tree ub = array_ref_up_bound (ref);
1031 : 66793 : if (ub
1032 : 65167 : && poly_int_tree_p (ub)
1033 : : /* ??? The C frontend for T[0] uses [0:] and the
1034 : : C++ frontend [0:-1U]. See layout_type for how
1035 : : awkward this is. */
1036 : 57288 : && !integer_minus_onep (ub)
1037 : 131960 : && known_le (wi::to_poly_offset (temp.op0),
1038 : : wi::to_poly_offset (ub)))
1039 : 56441 : avoid_oob = false;
1040 : : }
1041 : 37068567 : if (poly_int_tree_p (temp.op0)
1042 : 21412139 : && poly_int_tree_p (temp.op1)
1043 : 21412139 : && TREE_CODE (temp.op2) == INTEGER_CST
1044 : 58420535 : && !avoid_oob)
1045 : : {
1046 : 42684942 : poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1047 : 64027413 : - wi::to_poly_offset (temp.op1))
1048 : 42684942 : * wi::to_offset (temp.op2)
1049 : 21342471 : * vn_ref_op_align_unit (&temp));
1050 : 21342471 : off.to_shwi (&temp.off);
1051 : : }
1052 : 37068567 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1053 : 37068567 : && TYPE_REVERSE_STORAGE_ORDER
1054 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1055 : : }
1056 : 37068567 : break;
1057 : 81231650 : case VAR_DECL:
1058 : 81231650 : if (DECL_HARD_REGISTER (ref))
1059 : : {
1060 : 20153 : temp.op0 = ref;
1061 : 20153 : break;
1062 : : }
1063 : : /* Fallthru. */
1064 : 84603703 : case PARM_DECL:
1065 : 84603703 : case CONST_DECL:
1066 : 84603703 : case RESULT_DECL:
1067 : : /* Canonicalize decls to MEM[&decl] which is what we end up with
1068 : : when valueizing MEM[ptr] with ptr = &decl. */
1069 : 84603703 : temp.opcode = MEM_REF;
1070 : 84603703 : temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1071 : 84603703 : temp.off = 0;
1072 : 84603703 : result->safe_push (temp);
1073 : 84603703 : temp.opcode = ADDR_EXPR;
1074 : 84603703 : temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1075 : 84603703 : temp.type = TREE_TYPE (temp.op0);
1076 : 84603703 : temp.off = -1;
1077 : 84603703 : break;
1078 : 92679410 : case STRING_CST:
1079 : 92679410 : case INTEGER_CST:
1080 : 92679410 : case POLY_INT_CST:
1081 : 92679410 : case COMPLEX_CST:
1082 : 92679410 : case VECTOR_CST:
1083 : 92679410 : case REAL_CST:
1084 : 92679410 : case FIXED_CST:
1085 : 92679410 : case CONSTRUCTOR:
1086 : 92679410 : case SSA_NAME:
1087 : 92679410 : temp.op0 = ref;
1088 : 92679410 : break;
1089 : 45219432 : case ADDR_EXPR:
1090 : 45219432 : if (is_gimple_min_invariant (ref))
1091 : : {
1092 : 41166718 : temp.op0 = ref;
1093 : 41166718 : break;
1094 : : }
1095 : : break;
1096 : : /* These are only interesting for their operands, their
1097 : : existence, and their type. They will never be the last
1098 : : ref in the chain of references (IE they require an
1099 : : operand), so we don't have to put anything
1100 : : for op* as it will be handled by the iteration */
1101 : 466627 : case REALPART_EXPR:
1102 : 466627 : temp.off = 0;
1103 : 466627 : break;
1104 : 1477366 : case VIEW_CONVERT_EXPR:
1105 : 1477366 : temp.off = 0;
1106 : 1477366 : temp.reverse = storage_order_barrier_p (ref);
1107 : 1477366 : break;
1108 : 475131 : case IMAGPART_EXPR:
1109 : : /* This is only interesting for its constant offset. */
1110 : 475131 : temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1111 : 475131 : break;
1112 : 0 : default:
1113 : 0 : gcc_unreachable ();
1114 : : }
1115 : 537056419 : result->safe_push (temp);
1116 : :
1117 : 537056419 : if (REFERENCE_CLASS_P (ref)
1118 : 237630130 : || TREE_CODE (ref) == MODIFY_EXPR
1119 : 222522835 : || TREE_CODE (ref) == WITH_SIZE_EXPR
1120 : 759579117 : || (TREE_CODE (ref) == ADDR_EXPR
1121 : 45219432 : && !is_gimple_min_invariant (ref)))
1122 : 318586435 : ref = TREE_OPERAND (ref, 0);
1123 : : else
1124 : : ref = NULL_TREE;
1125 : : }
1126 : 218469984 : }
1127 : :
1128 : : /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1129 : : operands in *OPS, the reference alias set SET and the reference type TYPE.
1130 : : Return true if something useful was produced. */
1131 : :
1132 : : bool
1133 : 13819142 : ao_ref_init_from_vn_reference (ao_ref *ref,
1134 : : alias_set_type set, alias_set_type base_set,
1135 : : tree type, const vec<vn_reference_op_s> &ops)
1136 : : {
1137 : 13819142 : unsigned i;
1138 : 13819142 : tree base = NULL_TREE;
1139 : 13819142 : tree *op0_p = &base;
1140 : 13819142 : poly_offset_int offset = 0;
1141 : 13819142 : poly_offset_int max_size;
1142 : 13819142 : poly_offset_int size = -1;
1143 : 13819142 : tree size_tree = NULL_TREE;
1144 : :
1145 : : /* We don't handle calls. */
1146 : 13819142 : if (!type)
1147 : : return false;
1148 : :
1149 : 13819142 : machine_mode mode = TYPE_MODE (type);
1150 : 13819142 : if (mode == BLKmode)
1151 : 241686 : size_tree = TYPE_SIZE (type);
1152 : : else
1153 : 27154912 : size = GET_MODE_BITSIZE (mode);
1154 : 13577456 : if (size_tree != NULL_TREE
1155 : 241686 : && poly_int_tree_p (size_tree))
1156 : 241686 : size = wi::to_poly_offset (size_tree);
1157 : :
1158 : : /* Lower the final access size from the outermost expression. */
1159 : 13819142 : const_vn_reference_op_t cst_op = &ops[0];
1160 : : /* Cast away constness for the sake of the const-unsafe
1161 : : FOR_EACH_VEC_ELT(). */
1162 : 13819142 : vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1163 : 13819142 : size_tree = NULL_TREE;
1164 : 13819142 : if (op->opcode == COMPONENT_REF)
1165 : 4737456 : size_tree = DECL_SIZE (op->op0);
1166 : 9081686 : else if (op->opcode == BIT_FIELD_REF)
1167 : 73353 : size_tree = op->op0;
1168 : 4810809 : if (size_tree != NULL_TREE
1169 : 4810809 : && poly_int_tree_p (size_tree)
1170 : 9621618 : && (!known_size_p (size)
1171 : 13819142 : || known_lt (wi::to_poly_offset (size_tree), size)))
1172 : 48182 : size = wi::to_poly_offset (size_tree);
1173 : :
1174 : : /* Initially, maxsize is the same as the accessed element size.
1175 : : In the following it will only grow (or become -1). */
1176 : 13819142 : max_size = size;
1177 : :
1178 : : /* Compute cumulative bit-offset for nested component-refs and array-refs,
1179 : : and find the ultimate containing object. */
1180 : 53057944 : FOR_EACH_VEC_ELT (ops, i, op)
1181 : : {
1182 : 39386989 : switch (op->opcode)
1183 : : {
1184 : : case CALL_EXPR:
1185 : : return false;
1186 : :
1187 : : /* Record the base objects. */
1188 : 13386378 : case MEM_REF:
1189 : 13386378 : *op0_p = build2 (MEM_REF, op->type,
1190 : : NULL_TREE, op->op0);
1191 : 13386378 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1192 : 13386378 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1193 : 13386378 : op0_p = &TREE_OPERAND (*op0_p, 0);
1194 : 13386378 : break;
1195 : :
1196 : 283827 : case TARGET_MEM_REF:
1197 : 851481 : *op0_p = build5 (TARGET_MEM_REF, op->type,
1198 : : NULL_TREE, op->op2, op->op0,
1199 : 283827 : op->op1, ops[i+1].op0);
1200 : 283827 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1201 : 283827 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1202 : 283827 : op0_p = &TREE_OPERAND (*op0_p, 0);
1203 : 283827 : ++i;
1204 : 283827 : break;
1205 : :
1206 : : /* Unwrap some of the wrapped decls. */
1207 : 6662369 : case ADDR_EXPR:
1208 : : /* Apart from ADDR_EXPR arguments to MEM_REF. */
1209 : 6662369 : if (base != NULL_TREE
1210 : 6662369 : && TREE_CODE (base) == MEM_REF
1211 : 6629315 : && op->op0
1212 : 13291684 : && DECL_P (TREE_OPERAND (op->op0, 0)))
1213 : : {
1214 : 6626259 : const_vn_reference_op_t pop = &ops[i-1];
1215 : 6626259 : base = TREE_OPERAND (op->op0, 0);
1216 : 6626259 : if (known_eq (pop->off, -1))
1217 : : {
1218 : 51 : max_size = -1;
1219 : 51 : offset = 0;
1220 : : }
1221 : : else
1222 : 6626208 : offset += pop->off * BITS_PER_UNIT;
1223 : : op0_p = NULL;
1224 : : break;
1225 : : }
1226 : : /* Fallthru. */
1227 : 7044696 : case PARM_DECL:
1228 : 7044696 : case CONST_DECL:
1229 : 7044696 : case RESULT_DECL:
1230 : : /* ??? We shouldn't see these, but un-canonicalize what
1231 : : copy_reference_ops_from_ref does when visiting MEM_REF. */
1232 : 7044696 : case VAR_DECL:
1233 : : /* ??? And for this only have DECL_HARD_REGISTER. */
1234 : 7044696 : case STRING_CST:
1235 : : /* This can show up in ARRAY_REF bases. */
1236 : 7044696 : case INTEGER_CST:
1237 : 7044696 : case SSA_NAME:
1238 : 7044696 : *op0_p = op->op0;
1239 : 7044696 : op0_p = NULL;
1240 : 7044696 : break;
1241 : :
1242 : : /* And now the usual component-reference style ops. */
1243 : 73353 : case BIT_FIELD_REF:
1244 : 73353 : offset += wi::to_poly_offset (op->op1);
1245 : 73353 : break;
1246 : :
1247 : 7690928 : case COMPONENT_REF:
1248 : 7690928 : {
1249 : 7690928 : tree field = op->op0;
1250 : : /* We do not have a complete COMPONENT_REF tree here so we
1251 : : cannot use component_ref_field_offset. Do the interesting
1252 : : parts manually. */
1253 : 7690928 : tree this_offset = DECL_FIELD_OFFSET (field);
1254 : :
1255 : 7690928 : if (op->op1 || !poly_int_tree_p (this_offset))
1256 : 236 : max_size = -1;
1257 : : else
1258 : : {
1259 : 7690692 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1260 : 7690692 : << LOG2_BITS_PER_UNIT);
1261 : 7690692 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1262 : 7690692 : offset += woffset;
1263 : : }
1264 : : break;
1265 : : }
1266 : :
1267 : 2854951 : case ARRAY_RANGE_REF:
1268 : 2854951 : case ARRAY_REF:
1269 : : /* Use the recorded constant offset. */
1270 : 2854951 : if (maybe_eq (op->off, -1))
1271 : 976699 : max_size = -1;
1272 : : else
1273 : 1878252 : offset += op->off * BITS_PER_UNIT;
1274 : : break;
1275 : :
1276 : : case REALPART_EXPR:
1277 : : break;
1278 : :
1279 : : case IMAGPART_EXPR:
1280 : 39238802 : offset += size;
1281 : : break;
1282 : :
1283 : : case VIEW_CONVERT_EXPR:
1284 : : break;
1285 : :
1286 : : case POLY_INT_CST:
1287 : : case COMPLEX_CST:
1288 : : case VECTOR_CST:
1289 : : case REAL_CST:
1290 : : case FIXED_CST:
1291 : : case CONSTRUCTOR:
1292 : : return false;
1293 : :
1294 : : default:
1295 : : return false;
1296 : : }
1297 : : }
1298 : :
1299 : 13670955 : if (base == NULL_TREE)
1300 : : return false;
1301 : :
1302 : 13670955 : ref->ref = NULL_TREE;
1303 : 13670955 : ref->base = base;
1304 : 13670955 : ref->ref_alias_set = set;
1305 : 13670955 : ref->base_alias_set = base_set;
1306 : : /* We discount volatiles from value-numbering elsewhere. */
1307 : 13670955 : ref->volatile_p = false;
1308 : :
1309 : 13670955 : if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1310 : : {
1311 : 0 : ref->offset = 0;
1312 : 0 : ref->size = -1;
1313 : 0 : ref->max_size = -1;
1314 : 0 : return true;
1315 : : }
1316 : :
1317 : 13670955 : if (!offset.to_shwi (&ref->offset))
1318 : : {
1319 : 0 : ref->offset = 0;
1320 : 0 : ref->max_size = -1;
1321 : 0 : return true;
1322 : : }
1323 : :
1324 : 13670955 : if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1325 : 833727 : ref->max_size = -1;
1326 : :
1327 : : return true;
1328 : : }
1329 : :
1330 : : /* Copy the operations present in load/store/call REF into RESULT, a vector of
1331 : : vn_reference_op_s's. */
1332 : :
1333 : : static void
1334 : 9148560 : copy_reference_ops_from_call (gcall *call,
1335 : : vec<vn_reference_op_s> *result)
1336 : : {
1337 : 9148560 : vn_reference_op_s temp;
1338 : 9148560 : unsigned i;
1339 : 9148560 : tree lhs = gimple_call_lhs (call);
1340 : 9148560 : int lr;
1341 : :
1342 : : /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1343 : : different. By adding the lhs here in the vector, we ensure that the
1344 : : hashcode is different, guaranteeing a different value number. */
1345 : 9148560 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
1346 : : {
1347 : 487258 : memset (&temp, 0, sizeof (temp));
1348 : 487258 : temp.opcode = MODIFY_EXPR;
1349 : 487258 : temp.type = TREE_TYPE (lhs);
1350 : 487258 : temp.op0 = lhs;
1351 : 487258 : temp.off = -1;
1352 : 487258 : result->safe_push (temp);
1353 : : }
1354 : :
1355 : : /* Copy the type, opcode, function, static chain and EH region, if any. */
1356 : 9148560 : memset (&temp, 0, sizeof (temp));
1357 : 9148560 : temp.type = gimple_call_fntype (call);
1358 : 9148560 : temp.opcode = CALL_EXPR;
1359 : 9148560 : temp.op0 = gimple_call_fn (call);
1360 : 9148560 : if (gimple_call_internal_p (call))
1361 : 500284 : temp.clique = gimple_call_internal_fn (call);
1362 : 9148560 : temp.op1 = gimple_call_chain (call);
1363 : 9148560 : if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1364 : 715837 : temp.op2 = size_int (lr);
1365 : 9148560 : temp.off = -1;
1366 : 9148560 : result->safe_push (temp);
1367 : :
1368 : : /* Copy the call arguments. As they can be references as well,
1369 : : just chain them together. */
1370 : 27042000 : for (i = 0; i < gimple_call_num_args (call); ++i)
1371 : : {
1372 : 17893440 : tree callarg = gimple_call_arg (call, i);
1373 : 17893440 : copy_reference_ops_from_ref (callarg, result);
1374 : : }
1375 : 9148560 : }
1376 : :
1377 : : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1378 : : *I_P to point to the last element of the replacement. */
1379 : : static bool
1380 : 129207225 : vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1381 : : unsigned int *i_p)
1382 : : {
1383 : 129207225 : unsigned int i = *i_p;
1384 : 129207225 : vn_reference_op_t op = &(*ops)[i];
1385 : 129207225 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1386 : 129207225 : tree addr_base;
1387 : 129207225 : poly_int64 addr_offset = 0;
1388 : :
1389 : : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1390 : : from .foo.bar to the preceding MEM_REF offset and replace the
1391 : : address with &OBJ. */
1392 : 129207225 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1393 : : &addr_offset, vn_valueize);
1394 : 129207225 : gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1395 : 129207225 : if (addr_base != TREE_OPERAND (op->op0, 0))
1396 : : {
1397 : 586171 : poly_offset_int off
1398 : 586171 : = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1399 : : SIGNED)
1400 : 586171 : + addr_offset);
1401 : 586171 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1402 : 586171 : op->op0 = build_fold_addr_expr (addr_base);
1403 : 586171 : if (tree_fits_shwi_p (mem_op->op0))
1404 : 586102 : mem_op->off = tree_to_shwi (mem_op->op0);
1405 : : else
1406 : 69 : mem_op->off = -1;
1407 : 586171 : return true;
1408 : : }
1409 : : return false;
1410 : : }
1411 : :
1412 : : /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1413 : : *I_P to point to the last element of the replacement. */
1414 : : static bool
1415 : 84839418 : vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1416 : : unsigned int *i_p)
1417 : : {
1418 : 84839418 : bool changed = false;
1419 : 91188885 : vn_reference_op_t op;
1420 : :
1421 : 91188885 : do
1422 : : {
1423 : 91188885 : unsigned int i = *i_p;
1424 : 91188885 : op = &(*ops)[i];
1425 : 91188885 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1426 : 91188885 : gimple *def_stmt;
1427 : 91188885 : enum tree_code code;
1428 : 91188885 : poly_offset_int off;
1429 : :
1430 : 91188885 : def_stmt = SSA_NAME_DEF_STMT (op->op0);
1431 : 91188885 : if (!is_gimple_assign (def_stmt))
1432 : 84837467 : return changed;
1433 : :
1434 : 36306528 : code = gimple_assign_rhs_code (def_stmt);
1435 : 36306528 : if (code != ADDR_EXPR
1436 : 36306528 : && code != POINTER_PLUS_EXPR)
1437 : : return changed;
1438 : :
1439 : 18425971 : off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1440 : :
1441 : : /* The only thing we have to do is from &OBJ.foo.bar add the offset
1442 : : from .foo.bar to the preceding MEM_REF offset and replace the
1443 : : address with &OBJ. */
1444 : 18425971 : if (code == ADDR_EXPR)
1445 : : {
1446 : 841182 : tree addr, addr_base;
1447 : 841182 : poly_int64 addr_offset;
1448 : :
1449 : 841182 : addr = gimple_assign_rhs1 (def_stmt);
1450 : 841182 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1451 : : &addr_offset,
1452 : : vn_valueize);
1453 : : /* If that didn't work because the address isn't invariant propagate
1454 : : the reference tree from the address operation in case the current
1455 : : dereference isn't offsetted. */
1456 : 841182 : if (!addr_base
1457 : 174282 : && *i_p == ops->length () - 1
1458 : 87141 : && known_eq (off, 0)
1459 : : /* This makes us disable this transform for PRE where the
1460 : : reference ops might be also used for code insertion which
1461 : : is invalid. */
1462 : 887281 : && default_vn_walk_kind == VN_WALKREWRITE)
1463 : : {
1464 : 46001 : auto_vec<vn_reference_op_s, 32> tem;
1465 : 46001 : copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1466 : : /* Make sure to preserve TBAA info. The only objects not
1467 : : wrapped in MEM_REFs that can have their address taken are
1468 : : STRING_CSTs. */
1469 : 46001 : if (tem.length () >= 2
1470 : 46001 : && tem[tem.length () - 2].opcode == MEM_REF)
1471 : : {
1472 : 45986 : vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1473 : 45986 : new_mem_op->op0
1474 : 45986 : = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1475 : 91972 : wi::to_poly_wide (new_mem_op->op0));
1476 : : }
1477 : : else
1478 : 15 : gcc_assert (tem.last ().opcode == STRING_CST);
1479 : 46001 : ops->pop ();
1480 : 46001 : ops->pop ();
1481 : 46001 : ops->safe_splice (tem);
1482 : 46001 : --*i_p;
1483 : 46001 : return true;
1484 : 46001 : }
1485 : 795181 : if (!addr_base
1486 : 754041 : || TREE_CODE (addr_base) != MEM_REF
1487 : 1548344 : || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1488 : 751302 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1489 : : 0))))
1490 : : return changed;
1491 : :
1492 : 753163 : off += addr_offset;
1493 : 753163 : off += mem_ref_offset (addr_base);
1494 : 753163 : op->op0 = TREE_OPERAND (addr_base, 0);
1495 : : }
1496 : : else
1497 : : {
1498 : 17584789 : tree ptr, ptroff;
1499 : 17584789 : ptr = gimple_assign_rhs1 (def_stmt);
1500 : 17584789 : ptroff = gimple_assign_rhs2 (def_stmt);
1501 : 17584789 : if (TREE_CODE (ptr) != SSA_NAME
1502 : 15839472 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1503 : : /* Make sure to not endlessly recurse.
1504 : : See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1505 : : happen when we value-number a PHI to its backedge value. */
1506 : 15838058 : || SSA_VAL (ptr) == op->op0
1507 : 33422847 : || !poly_int_tree_p (ptroff))
1508 : 11986534 : return changed;
1509 : :
1510 : 5598255 : off += wi::to_poly_offset (ptroff);
1511 : 5598255 : op->op0 = ptr;
1512 : : }
1513 : :
1514 : 6351418 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1515 : 6351418 : if (tree_fits_shwi_p (mem_op->op0))
1516 : 6084259 : mem_op->off = tree_to_shwi (mem_op->op0);
1517 : : else
1518 : 267159 : mem_op->off = -1;
1519 : : /* ??? Can end up with endless recursion here!?
1520 : : gcc.c-torture/execute/strcmp-1.c */
1521 : 6351418 : if (TREE_CODE (op->op0) == SSA_NAME)
1522 : 6349557 : op->op0 = SSA_VAL (op->op0);
1523 : 6351418 : if (TREE_CODE (op->op0) != SSA_NAME)
1524 : 1951 : op->opcode = TREE_CODE (op->op0);
1525 : :
1526 : 6351418 : changed = true;
1527 : : }
1528 : : /* Tail-recurse. */
1529 : 6351418 : while (TREE_CODE (op->op0) == SSA_NAME);
1530 : :
1531 : : /* Fold a remaining *&. */
1532 : 1951 : if (TREE_CODE (op->op0) == ADDR_EXPR)
1533 : 261 : vn_reference_fold_indirect (ops, i_p);
1534 : :
1535 : : return changed;
1536 : : }
1537 : :
1538 : : /* Optimize the reference REF to a constant if possible or return
1539 : : NULL_TREE if not. */
1540 : :
1541 : : tree
1542 : 108717450 : fully_constant_vn_reference_p (vn_reference_t ref)
1543 : : {
1544 : 108717450 : vec<vn_reference_op_s> operands = ref->operands;
1545 : 108717450 : vn_reference_op_t op;
1546 : :
1547 : : /* Try to simplify the translated expression if it is
1548 : : a call to a builtin function with at most two arguments. */
1549 : 108717450 : op = &operands[0];
1550 : 108717450 : if (op->opcode == CALL_EXPR
1551 : 86508 : && (!op->op0
1552 : 80210 : || (TREE_CODE (op->op0) == ADDR_EXPR
1553 : 80210 : && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1554 : 80210 : && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1555 : : BUILT_IN_NORMAL)))
1556 : 71134 : && operands.length () >= 2
1557 : 108788548 : && operands.length () <= 3)
1558 : : {
1559 : 35422 : vn_reference_op_t arg0, arg1 = NULL;
1560 : 35422 : bool anyconst = false;
1561 : 35422 : arg0 = &operands[1];
1562 : 35422 : if (operands.length () > 2)
1563 : 4676 : arg1 = &operands[2];
1564 : 35422 : if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1565 : 35422 : || (arg0->opcode == ADDR_EXPR
1566 : 13296 : && is_gimple_min_invariant (arg0->op0)))
1567 : : anyconst = true;
1568 : 35422 : if (arg1
1569 : 35422 : && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1570 : 3855 : || (arg1->opcode == ADDR_EXPR
1571 : 579 : && is_gimple_min_invariant (arg1->op0))))
1572 : : anyconst = true;
1573 : 34022 : if (anyconst)
1574 : : {
1575 : 20792 : combined_fn fn;
1576 : 20792 : if (op->op0)
1577 : 20469 : fn = as_combined_fn (DECL_FUNCTION_CODE
1578 : 20469 : (TREE_OPERAND (op->op0, 0)));
1579 : : else
1580 : 323 : fn = as_combined_fn ((internal_fn) op->clique);
1581 : 20792 : tree folded;
1582 : 20792 : if (arg1)
1583 : 1993 : folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1584 : : else
1585 : 18799 : folded = fold_const_call (fn, ref->type, arg0->op0);
1586 : 20792 : if (folded
1587 : 20792 : && is_gimple_min_invariant (folded))
1588 : : return folded;
1589 : : }
1590 : : }
1591 : :
1592 : : /* Simplify reads from constants or constant initializers. */
1593 : 108682028 : else if (BITS_PER_UNIT == 8
1594 : 108682028 : && ref->type
1595 : 108682028 : && COMPLETE_TYPE_P (ref->type)
1596 : 217364014 : && is_gimple_reg_type (ref->type))
1597 : : {
1598 : 103359131 : poly_int64 off = 0;
1599 : 103359131 : HOST_WIDE_INT size;
1600 : 103359131 : if (INTEGRAL_TYPE_P (ref->type))
1601 : 52263091 : size = TYPE_PRECISION (ref->type);
1602 : 51096040 : else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1603 : 51096040 : size = tree_to_shwi (TYPE_SIZE (ref->type));
1604 : : else
1605 : 108717450 : return NULL_TREE;
1606 : 103359131 : if (size % BITS_PER_UNIT != 0
1607 : 101391398 : || size > MAX_BITSIZE_MODE_ANY_MODE)
1608 : : return NULL_TREE;
1609 : 101390251 : size /= BITS_PER_UNIT;
1610 : 101390251 : unsigned i;
1611 : 187496033 : for (i = 0; i < operands.length (); ++i)
1612 : : {
1613 : 187496033 : if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1614 : : {
1615 : 308 : ++i;
1616 : 308 : break;
1617 : : }
1618 : 187495725 : if (known_eq (operands[i].off, -1))
1619 : : return NULL_TREE;
1620 : 173977579 : off += operands[i].off;
1621 : 173977579 : if (operands[i].opcode == MEM_REF)
1622 : : {
1623 : 87871797 : ++i;
1624 : 87871797 : break;
1625 : : }
1626 : : }
1627 : 87872105 : vn_reference_op_t base = &operands[--i];
1628 : 87872105 : tree ctor = error_mark_node;
1629 : 87872105 : tree decl = NULL_TREE;
1630 : 87872105 : if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1631 : 308 : ctor = base->op0;
1632 : 87871797 : else if (base->opcode == MEM_REF
1633 : 87871797 : && base[1].opcode == ADDR_EXPR
1634 : 144635614 : && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1635 : 3556595 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1636 : 3556535 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1637 : : {
1638 : 53211635 : decl = TREE_OPERAND (base[1].op0, 0);
1639 : 53211635 : if (TREE_CODE (decl) == STRING_CST)
1640 : : ctor = decl;
1641 : : else
1642 : 53207282 : ctor = ctor_for_folding (decl);
1643 : : }
1644 : 87867752 : if (ctor == NULL_TREE)
1645 : 379 : return build_zero_cst (ref->type);
1646 : 87871726 : else if (ctor != error_mark_node)
1647 : : {
1648 : 111686 : HOST_WIDE_INT const_off;
1649 : 111686 : if (decl)
1650 : : {
1651 : 222756 : tree res = fold_ctor_reference (ref->type, ctor,
1652 : 111378 : off * BITS_PER_UNIT,
1653 : 111378 : size * BITS_PER_UNIT, decl);
1654 : 111378 : if (res)
1655 : : {
1656 : 53324 : STRIP_USELESS_TYPE_CONVERSION (res);
1657 : 53324 : if (is_gimple_min_invariant (res))
1658 : 108717450 : return res;
1659 : : }
1660 : : }
1661 : 308 : else if (off.is_constant (&const_off))
1662 : : {
1663 : 308 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1664 : 308 : int len = native_encode_expr (ctor, buf, size, const_off);
1665 : 308 : if (len > 0)
1666 : 138 : return native_interpret_expr (ref->type, buf, len);
1667 : : }
1668 : : }
1669 : : }
1670 : :
1671 : : return NULL_TREE;
1672 : : }
1673 : :
1674 : : /* Return true if OPS contain a storage order barrier. */
1675 : :
1676 : : static bool
1677 : 57068724 : contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1678 : : {
1679 : 57068724 : vn_reference_op_t op;
1680 : 57068724 : unsigned i;
1681 : :
1682 : 224286949 : FOR_EACH_VEC_ELT (ops, i, op)
1683 : 167218225 : if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1684 : : return true;
1685 : :
1686 : : return false;
1687 : : }
1688 : :
1689 : : /* Return true if OPS represent an access with reverse storage order. */
1690 : :
1691 : : static bool
1692 : 57076899 : reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
1693 : : {
1694 : 57076899 : unsigned i = 0;
1695 : 57076899 : if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1696 : : ++i;
1697 : 57076899 : switch (ops[i].opcode)
1698 : : {
1699 : 54946441 : case ARRAY_REF:
1700 : 54946441 : case COMPONENT_REF:
1701 : 54946441 : case BIT_FIELD_REF:
1702 : 54946441 : case MEM_REF:
1703 : 54946441 : return ops[i].reverse;
1704 : : default:
1705 : : return false;
1706 : : }
1707 : : }
1708 : :
1709 : : /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1710 : : structures into their value numbers. This is done in-place, and
1711 : : the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1712 : : whether any operands were valueized. */
1713 : :
1714 : : static void
1715 : 221666354 : valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1716 : : bool with_avail = false)
1717 : : {
1718 : 221666354 : *valueized_anything = false;
1719 : :
1720 : 891937452 : for (unsigned i = 0; i < orig->length (); ++i)
1721 : : {
1722 : 670271098 : re_valueize:
1723 : 673630854 : vn_reference_op_t vro = &(*orig)[i];
1724 : 673630854 : if (vro->opcode == SSA_NAME
1725 : 577167460 : || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1726 : : {
1727 : 120705970 : tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1728 : 120705970 : if (tem != vro->op0)
1729 : : {
1730 : 17234912 : *valueized_anything = true;
1731 : 17234912 : vro->op0 = tem;
1732 : : }
1733 : : /* If it transforms from an SSA_NAME to a constant, update
1734 : : the opcode. */
1735 : 120705970 : if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1736 : 1985635 : vro->opcode = TREE_CODE (vro->op0);
1737 : : }
1738 : 673630854 : if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1739 : : {
1740 : 26181 : tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1741 : 26181 : if (tem != vro->op1)
1742 : : {
1743 : 543 : *valueized_anything = true;
1744 : 543 : vro->op1 = tem;
1745 : : }
1746 : : }
1747 : 673630854 : if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1748 : : {
1749 : 205318 : tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1750 : 205318 : if (tem != vro->op2)
1751 : : {
1752 : 119490 : *valueized_anything = true;
1753 : 119490 : vro->op2 = tem;
1754 : : }
1755 : : }
1756 : : /* If it transforms from an SSA_NAME to an address, fold with
1757 : : a preceding indirect reference. */
1758 : 673630854 : if (i > 0
1759 : 451923896 : && vro->op0
1760 : 448557125 : && TREE_CODE (vro->op0) == ADDR_EXPR
1761 : 808859319 : && (*orig)[i - 1].opcode == MEM_REF)
1762 : : {
1763 : 129206964 : if (vn_reference_fold_indirect (orig, &i))
1764 : 586171 : *valueized_anything = true;
1765 : : }
1766 : 544423890 : else if (i > 0
1767 : 322716932 : && vro->opcode == SSA_NAME
1768 : 638901649 : && (*orig)[i - 1].opcode == MEM_REF)
1769 : : {
1770 : 84839418 : if (vn_reference_maybe_forwprop_address (orig, &i))
1771 : : {
1772 : 3359756 : *valueized_anything = true;
1773 : : /* Re-valueize the current operand. */
1774 : 3359756 : goto re_valueize;
1775 : : }
1776 : : }
1777 : : /* If it transforms a non-constant ARRAY_REF into a constant
1778 : : one, adjust the constant offset. */
1779 : 459584472 : else if ((vro->opcode == ARRAY_REF
1780 : 459584472 : || vro->opcode == ARRAY_RANGE_REF)
1781 : 39231860 : && known_eq (vro->off, -1)
1782 : 17095592 : && poly_int_tree_p (vro->op0)
1783 : 4623802 : && poly_int_tree_p (vro->op1)
1784 : 464208274 : && TREE_CODE (vro->op2) == INTEGER_CST)
1785 : : {
1786 : : /* Prohibit value-numbering addresses of one-after-the-last
1787 : : element ARRAY_REFs the same as addresses of other components
1788 : : before the pass folding __builtin_object_size had a chance
1789 : : to run. */
1790 : 4490117 : if (!(cfun->curr_properties & PROP_objsz)
1791 : 5710967 : && (*orig)[0].opcode == ADDR_EXPR)
1792 : : {
1793 : 27325 : tree dom = TYPE_DOMAIN ((*orig)[i + 1].type);
1794 : 44979 : if (!dom
1795 : 27175 : || !TYPE_MAX_VALUE (dom)
1796 : 17573 : || !poly_int_tree_p (TYPE_MAX_VALUE (dom))
1797 : 37086 : || integer_minus_onep (TYPE_MAX_VALUE (dom)))
1798 : 18461 : continue;
1799 : 9671 : if (!known_le (wi::to_poly_offset (vro->op0),
1800 : : wi::to_poly_offset (TYPE_MAX_VALUE (dom))))
1801 : 807 : continue;
1802 : : }
1803 : :
1804 : 8943312 : poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1805 : 13414968 : - wi::to_poly_offset (vro->op1))
1806 : 8943312 : * wi::to_offset (vro->op2)
1807 : 4471656 : * vn_ref_op_align_unit (vro));
1808 : 4471656 : off.to_shwi (&vro->off);
1809 : : }
1810 : : }
1811 : 221666354 : }
1812 : :
1813 : : static void
1814 : 14646563 : valueize_refs (vec<vn_reference_op_s> *orig)
1815 : : {
1816 : 14646563 : bool tem;
1817 : 0 : valueize_refs_1 (orig, &tem);
1818 : 0 : }
1819 : :
1820 : : static vec<vn_reference_op_s> shared_lookup_references;
1821 : :
1822 : : /* Create a vector of vn_reference_op_s structures from REF, a
1823 : : REFERENCE_CLASS_P tree. The vector is shared among all callers of
1824 : : this function. *VALUEIZED_ANYTHING will specify whether any
1825 : : operands were valueized. */
1826 : :
1827 : : static vec<vn_reference_op_s>
1828 : 179439588 : valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1829 : : {
1830 : 179439588 : if (!ref)
1831 : 0 : return vNULL;
1832 : 179439588 : shared_lookup_references.truncate (0);
1833 : 179439588 : copy_reference_ops_from_ref (ref, &shared_lookup_references);
1834 : 179439588 : valueize_refs_1 (&shared_lookup_references, valueized_anything);
1835 : 179439588 : return shared_lookup_references;
1836 : : }
1837 : :
1838 : : /* Create a vector of vn_reference_op_s structures from CALL, a
1839 : : call statement. The vector is shared among all callers of
1840 : : this function. */
1841 : :
1842 : : static vec<vn_reference_op_s>
1843 : 9148560 : valueize_shared_reference_ops_from_call (gcall *call)
1844 : : {
1845 : 9148560 : if (!call)
1846 : 0 : return vNULL;
1847 : 9148560 : shared_lookup_references.truncate (0);
1848 : 9148560 : copy_reference_ops_from_call (call, &shared_lookup_references);
1849 : 9148560 : valueize_refs (&shared_lookup_references);
1850 : 9148560 : return shared_lookup_references;
1851 : : }
1852 : :
1853 : : /* Lookup a SCCVN reference operation VR in the current hash table.
1854 : : Returns the resulting value number if it exists in the hash table,
1855 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
1856 : : vn_reference_t stored in the hashtable if something is found. */
1857 : :
1858 : : static tree
1859 : 65118801 : vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1860 : : {
1861 : 65118801 : vn_reference_s **slot;
1862 : 65118801 : hashval_t hash;
1863 : :
1864 : 65118801 : hash = vr->hashcode;
1865 : 65118801 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1866 : 65118801 : if (slot)
1867 : : {
1868 : 8356338 : if (vnresult)
1869 : 8356338 : *vnresult = (vn_reference_t)*slot;
1870 : 8356338 : return ((vn_reference_t)*slot)->result;
1871 : : }
1872 : :
1873 : : return NULL_TREE;
1874 : : }
1875 : :
1876 : :
1877 : : /* Partial definition tracking support. */
1878 : :
1879 : : struct pd_range
1880 : : {
1881 : : HOST_WIDE_INT offset;
1882 : : HOST_WIDE_INT size;
1883 : : pd_range *m_children[2];
1884 : : };
1885 : :
1886 : : struct pd_data
1887 : : {
1888 : : tree rhs;
1889 : : HOST_WIDE_INT rhs_off;
1890 : : HOST_WIDE_INT offset;
1891 : : HOST_WIDE_INT size;
1892 : : };
1893 : :
1894 : : /* Context for alias walking. */
1895 : :
1896 : : struct vn_walk_cb_data
1897 : : {
1898 : 60419567 : vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1899 : : vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1900 : : bool redundant_store_removal_p_)
1901 : 60419567 : : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1902 : 60419567 : mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1903 : 60419567 : vn_walk_kind (vn_walk_kind_),
1904 : 60419567 : tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
1905 : 120839134 : saved_operands (vNULL), first_range (), first_set (-2),
1906 : 120839134 : first_base_set (-2)
1907 : : {
1908 : 60419567 : if (!last_vuse_ptr)
1909 : 28002151 : last_vuse_ptr = &last_vuse;
1910 : 60419567 : ao_ref_init (&orig_ref, orig_ref_);
1911 : 60419567 : if (mask)
1912 : : {
1913 : 370840 : wide_int w = wi::to_wide (mask);
1914 : 370840 : unsigned int pos = 0, prec = w.get_precision ();
1915 : 370840 : pd_data pd;
1916 : 370840 : pd.rhs = build_constructor (NULL_TREE, NULL);
1917 : 370840 : pd.rhs_off = 0;
1918 : : /* When bitwise and with a constant is done on a memory load,
1919 : : we don't really need all the bits to be defined or defined
1920 : : to constants, we don't really care what is in the position
1921 : : corresponding to 0 bits in the mask.
1922 : : So, push the ranges of those 0 bits in the mask as artificial
1923 : : zero stores and let the partial def handling code do the
1924 : : rest. */
1925 : 783672 : while (pos < prec)
1926 : : {
1927 : 765407 : int tz = wi::ctz (w);
1928 : 765407 : if (pos + tz > prec)
1929 : 352575 : tz = prec - pos;
1930 : 765407 : if (tz)
1931 : : {
1932 : 588600 : if (BYTES_BIG_ENDIAN)
1933 : : pd.offset = prec - pos - tz;
1934 : : else
1935 : 588600 : pd.offset = pos;
1936 : 588600 : pd.size = tz;
1937 : 588600 : void *r = push_partial_def (pd, 0, 0, 0, prec);
1938 : 588600 : gcc_assert (r == NULL_TREE);
1939 : : }
1940 : 765407 : pos += tz;
1941 : 765407 : if (pos == prec)
1942 : : break;
1943 : 412832 : w = wi::lrshift (w, tz);
1944 : 412832 : tz = wi::ctz (wi::bit_not (w));
1945 : 412832 : if (pos + tz > prec)
1946 : 0 : tz = prec - pos;
1947 : 412832 : pos += tz;
1948 : 412832 : w = wi::lrshift (w, tz);
1949 : : }
1950 : 370840 : }
1951 : 60419567 : }
1952 : : ~vn_walk_cb_data ();
1953 : : void *finish (alias_set_type, alias_set_type, tree);
1954 : : void *push_partial_def (pd_data pd,
1955 : : alias_set_type, alias_set_type, HOST_WIDE_INT,
1956 : : HOST_WIDE_INT);
1957 : :
1958 : : vn_reference_t vr;
1959 : : ao_ref orig_ref;
1960 : : tree *last_vuse_ptr;
1961 : : tree last_vuse;
1962 : : tree mask;
1963 : : tree masked_result;
1964 : : tree same_val;
1965 : : vn_lookup_kind vn_walk_kind;
1966 : : bool tbaa_p;
1967 : : bool redundant_store_removal_p;
1968 : : vec<vn_reference_op_s> saved_operands;
1969 : :
1970 : : /* The VDEFs of partial defs we come along. */
1971 : : auto_vec<pd_data, 2> partial_defs;
1972 : : /* The first defs range to avoid splay tree setup in most cases. */
1973 : : pd_range first_range;
1974 : : alias_set_type first_set;
1975 : : alias_set_type first_base_set;
1976 : : default_splay_tree<pd_range *> known_ranges;
1977 : : obstack ranges_obstack;
1978 : : static constexpr HOST_WIDE_INT bufsize = 64;
1979 : : };
1980 : :
1981 : 60419567 : vn_walk_cb_data::~vn_walk_cb_data ()
1982 : : {
1983 : 60419567 : if (known_ranges)
1984 : 216225 : obstack_free (&ranges_obstack, NULL);
1985 : 60419567 : saved_operands.release ();
1986 : 60419567 : }
1987 : :
1988 : : void *
1989 : 1456668 : vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
1990 : : {
1991 : 1456668 : if (first_set != -2)
1992 : : {
1993 : 344901 : set = first_set;
1994 : 344901 : base_set = first_base_set;
1995 : : }
1996 : 1456668 : if (mask)
1997 : : {
1998 : 534 : masked_result = val;
1999 : 534 : return (void *) -1;
2000 : : }
2001 : 1456134 : if (same_val && !operand_equal_p (val, same_val))
2002 : : return (void *) -1;
2003 : 1451044 : vec<vn_reference_op_s> &operands
2004 : 1451044 : = saved_operands.exists () ? saved_operands : vr->operands;
2005 : 1451044 : return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
2006 : : vr->offset, vr->max_size,
2007 : 1451044 : vr->type, operands, val);
2008 : : }
2009 : :
2010 : : /* Push PD to the vector of partial definitions returning a
2011 : : value when we are ready to combine things with VUSE, SET and MAXSIZEI,
2012 : : NULL when we want to continue looking for partial defs or -1
2013 : : on failure. */
2014 : :
2015 : : void *
2016 : 670486 : vn_walk_cb_data::push_partial_def (pd_data pd,
2017 : : alias_set_type set, alias_set_type base_set,
2018 : : HOST_WIDE_INT offseti,
2019 : : HOST_WIDE_INT maxsizei)
2020 : : {
2021 : : /* We're using a fixed buffer for encoding so fail early if the object
2022 : : we want to interpret is bigger. */
2023 : 670486 : if (maxsizei > bufsize * BITS_PER_UNIT
2024 : : || CHAR_BIT != 8
2025 : : || BITS_PER_UNIT != 8
2026 : : /* Not prepared to handle PDP endian. */
2027 : : || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2028 : : return (void *)-1;
2029 : :
2030 : : /* Turn too large constant stores into non-constant stores. */
2031 : 670415 : if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
2032 : 0 : pd.rhs = error_mark_node;
2033 : :
2034 : : /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
2035 : : most a partial byte before and/or after the region. */
2036 : 670415 : if (!CONSTANT_CLASS_P (pd.rhs))
2037 : : {
2038 : 640566 : if (pd.offset < offseti)
2039 : : {
2040 : 9765 : HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2041 : 9765 : gcc_assert (pd.size > o);
2042 : 9765 : pd.size -= o;
2043 : 9765 : pd.offset += o;
2044 : : }
2045 : 640566 : if (pd.size > maxsizei)
2046 : 11566 : pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
2047 : : }
2048 : :
2049 : 670415 : pd.offset -= offseti;
2050 : :
2051 : 1340830 : bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2052 : 670415 : || CONSTANT_CLASS_P (pd.rhs));
2053 : 670415 : pd_range *r;
2054 : 670415 : if (partial_defs.is_empty ())
2055 : : {
2056 : : /* If we get a clobber upfront, fail. */
2057 : 427285 : if (TREE_CLOBBER_P (pd.rhs))
2058 : : return (void *)-1;
2059 : 415443 : if (!pd_constant_p)
2060 : : return (void *)-1;
2061 : 387201 : partial_defs.safe_push (pd);
2062 : 387201 : first_range.offset = pd.offset;
2063 : 387201 : first_range.size = pd.size;
2064 : 387201 : first_set = set;
2065 : 387201 : first_base_set = base_set;
2066 : 387201 : last_vuse_ptr = NULL;
2067 : 387201 : r = &first_range;
2068 : : /* Go check if the first partial definition was a full one in case
2069 : : the caller didn't optimize for this. */
2070 : : }
2071 : : else
2072 : : {
2073 : 243130 : if (!known_ranges)
2074 : : {
2075 : : /* ??? Optimize the case where the 2nd partial def completes
2076 : : things. */
2077 : 216225 : gcc_obstack_init (&ranges_obstack);
2078 : 216225 : known_ranges.insert_max_node (&first_range);
2079 : : }
2080 : : /* Lookup the offset and see if we need to merge. */
2081 : 243130 : int comparison = known_ranges.lookup_le
2082 : 489466 : ([&] (pd_range *r) { return pd.offset < r->offset; },
2083 : 223394 : [&] (pd_range *r) { return pd.offset > r->offset; });
2084 : 243130 : r = known_ranges.root ();
2085 : 243130 : if (comparison >= 0
2086 : 243130 : && ranges_known_overlap_p (r->offset, r->size + 1,
2087 : : pd.offset, pd.size))
2088 : : {
2089 : : /* Ignore partial defs already covered. Here we also drop shadowed
2090 : : clobbers arriving here at the floor. */
2091 : 5468 : if (known_subrange_p (pd.offset, pd.size, r->offset, r->size))
2092 : : return NULL;
2093 : 5012 : r->size = MAX (r->offset + r->size, pd.offset + pd.size) - r->offset;
2094 : : }
2095 : : else
2096 : : {
2097 : : /* pd.offset wasn't covered yet, insert the range. */
2098 : 237662 : void *addr = XOBNEW (&ranges_obstack, pd_range);
2099 : 237662 : r = new (addr) pd_range { pd.offset, pd.size, {} };
2100 : 237662 : known_ranges.insert_relative (comparison, r);
2101 : : }
2102 : : /* Merge r which now contains pd's range and is a member of the splay
2103 : : tree with adjacent overlapping ranges. */
2104 : 242674 : if (known_ranges.splay_next_node ())
2105 : 21443 : do
2106 : : {
2107 : 21443 : pd_range *rafter = known_ranges.root ();
2108 : 21443 : if (!ranges_known_overlap_p (r->offset, r->size + 1,
2109 : 21443 : rafter->offset, rafter->size))
2110 : : break;
2111 : 21094 : r->size = MAX (r->offset + r->size,
2112 : 21094 : rafter->offset + rafter->size) - r->offset;
2113 : : }
2114 : 21094 : while (known_ranges.remove_root_and_splay_next ());
2115 : : /* If we get a clobber, fail. */
2116 : 242674 : if (TREE_CLOBBER_P (pd.rhs))
2117 : : return (void *)-1;
2118 : : /* Non-constants are OK as long as they are shadowed by a constant. */
2119 : 240205 : if (!pd_constant_p)
2120 : : return (void *)-1;
2121 : 232603 : partial_defs.safe_push (pd);
2122 : : }
2123 : :
2124 : : /* Now we have merged pd's range into the range tree. When we have covered
2125 : : [offseti, sizei] then the tree will contain exactly one node which has
2126 : : the desired properties and it will be 'r'. */
2127 : 619804 : if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2128 : : /* Continue looking for partial defs. */
2129 : : return NULL;
2130 : :
2131 : : /* Now simply native encode all partial defs in reverse order. */
2132 : 7757 : unsigned ndefs = partial_defs.length ();
2133 : : /* We support up to 512-bit values (for V8DFmode). */
2134 : 7757 : unsigned char buffer[bufsize + 1];
2135 : 7757 : unsigned char this_buffer[bufsize + 1];
2136 : 7757 : int len;
2137 : :
2138 : 7757 : memset (buffer, 0, bufsize + 1);
2139 : 7757 : unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2140 : 37615 : while (!partial_defs.is_empty ())
2141 : : {
2142 : 22101 : pd_data pd = partial_defs.pop ();
2143 : 22101 : unsigned int amnt;
2144 : 22101 : if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2145 : : {
2146 : : /* Empty CONSTRUCTOR. */
2147 : 1908 : if (pd.size >= needed_len * BITS_PER_UNIT)
2148 : 1908 : len = needed_len;
2149 : : else
2150 : 1744 : len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2151 : 1908 : memset (this_buffer, 0, len);
2152 : : }
2153 : 20193 : else if (pd.rhs_off >= 0)
2154 : : {
2155 : 40386 : len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2156 : 20193 : (MAX (0, -pd.offset)
2157 : 20193 : + pd.rhs_off) / BITS_PER_UNIT);
2158 : 20193 : if (len <= 0
2159 : 20193 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2160 : 20193 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2161 : : {
2162 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2163 : 0 : fprintf (dump_file, "Failed to encode %u "
2164 : : "partial definitions\n", ndefs);
2165 : 0 : return (void *)-1;
2166 : : }
2167 : : }
2168 : : else /* negative pd.rhs_off indicates we want to chop off first bits */
2169 : : {
2170 : 0 : if (-pd.rhs_off >= bufsize)
2171 : : return (void *)-1;
2172 : 0 : len = native_encode_expr (pd.rhs,
2173 : 0 : this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2174 : 0 : bufsize - -pd.rhs_off / BITS_PER_UNIT,
2175 : 0 : MAX (0, -pd.offset) / BITS_PER_UNIT);
2176 : 0 : if (len <= 0
2177 : 0 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2178 : 0 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2179 : : {
2180 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2181 : 0 : fprintf (dump_file, "Failed to encode %u "
2182 : : "partial definitions\n", ndefs);
2183 : 0 : return (void *)-1;
2184 : : }
2185 : : }
2186 : :
2187 : 22101 : unsigned char *p = buffer;
2188 : 22101 : HOST_WIDE_INT size = pd.size;
2189 : 22101 : if (pd.offset < 0)
2190 : 231 : size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2191 : 22101 : this_buffer[len] = 0;
2192 : 22101 : if (BYTES_BIG_ENDIAN)
2193 : : {
2194 : : /* LSB of this_buffer[len - 1] byte should be at
2195 : : pd.offset + pd.size - 1 bits in buffer. */
2196 : : amnt = ((unsigned HOST_WIDE_INT) pd.offset
2197 : : + pd.size) % BITS_PER_UNIT;
2198 : : if (amnt)
2199 : : shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2200 : : unsigned char *q = this_buffer;
2201 : : unsigned int off = 0;
2202 : : if (pd.offset >= 0)
2203 : : {
2204 : : unsigned int msk;
2205 : : off = pd.offset / BITS_PER_UNIT;
2206 : : gcc_assert (off < needed_len);
2207 : : p = buffer + off;
2208 : : if (size <= amnt)
2209 : : {
2210 : : msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2211 : : *p = (*p & ~msk) | (this_buffer[len] & msk);
2212 : : size = 0;
2213 : : }
2214 : : else
2215 : : {
2216 : : if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2217 : : q = (this_buffer + len
2218 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2219 : : / BITS_PER_UNIT));
2220 : : if (pd.offset % BITS_PER_UNIT)
2221 : : {
2222 : : msk = -1U << (BITS_PER_UNIT
2223 : : - (pd.offset % BITS_PER_UNIT));
2224 : : *p = (*p & msk) | (*q & ~msk);
2225 : : p++;
2226 : : q++;
2227 : : off++;
2228 : : size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2229 : : gcc_assert (size >= 0);
2230 : : }
2231 : : }
2232 : : }
2233 : : else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2234 : : {
2235 : : q = (this_buffer + len
2236 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2237 : : / BITS_PER_UNIT));
2238 : : if (pd.offset % BITS_PER_UNIT)
2239 : : {
2240 : : q++;
2241 : : size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2242 : : % BITS_PER_UNIT);
2243 : : gcc_assert (size >= 0);
2244 : : }
2245 : : }
2246 : : if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2247 : : > needed_len)
2248 : : size = (needed_len - off) * BITS_PER_UNIT;
2249 : : memcpy (p, q, size / BITS_PER_UNIT);
2250 : : if (size % BITS_PER_UNIT)
2251 : : {
2252 : : unsigned int msk
2253 : : = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2254 : : p += size / BITS_PER_UNIT;
2255 : : q += size / BITS_PER_UNIT;
2256 : : *p = (*q & msk) | (*p & ~msk);
2257 : : }
2258 : : }
2259 : : else
2260 : : {
2261 : 22101 : if (pd.offset >= 0)
2262 : : {
2263 : : /* LSB of this_buffer[0] byte should be at pd.offset bits
2264 : : in buffer. */
2265 : 21870 : unsigned int msk;
2266 : 21870 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2267 : 21870 : amnt = pd.offset % BITS_PER_UNIT;
2268 : 21870 : if (amnt)
2269 : 1380 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2270 : 21870 : unsigned int off = pd.offset / BITS_PER_UNIT;
2271 : 21870 : gcc_assert (off < needed_len);
2272 : 21870 : size = MIN (size,
2273 : : (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2274 : 21870 : p = buffer + off;
2275 : 21870 : if (amnt + size < BITS_PER_UNIT)
2276 : : {
2277 : : /* Low amnt bits come from *p, then size bits
2278 : : from this_buffer[0] and the remaining again from
2279 : : *p. */
2280 : 963 : msk = ((1 << size) - 1) << amnt;
2281 : 963 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2282 : 963 : size = 0;
2283 : : }
2284 : 20907 : else if (amnt)
2285 : : {
2286 : 1165 : msk = -1U << amnt;
2287 : 1165 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2288 : 1165 : p++;
2289 : 1165 : size -= (BITS_PER_UNIT - amnt);
2290 : : }
2291 : : }
2292 : : else
2293 : : {
2294 : 231 : amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2295 : 231 : if (amnt)
2296 : 16 : size -= BITS_PER_UNIT - amnt;
2297 : 231 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2298 : 231 : if (amnt)
2299 : 16 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2300 : : }
2301 : 22101 : memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2302 : 22101 : p += size / BITS_PER_UNIT;
2303 : 22101 : if (size % BITS_PER_UNIT)
2304 : : {
2305 : 618 : unsigned int msk = -1U << (size % BITS_PER_UNIT);
2306 : 618 : *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2307 : 618 : & ~msk) | (*p & msk);
2308 : : }
2309 : : }
2310 : : }
2311 : :
2312 : 7757 : tree type = vr->type;
2313 : : /* Make sure to interpret in a type that has a range covering the whole
2314 : : access size. */
2315 : 7757 : if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2316 : 13 : type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2317 : 7757 : tree val;
2318 : 7757 : if (BYTES_BIG_ENDIAN)
2319 : : {
2320 : : unsigned sz = needed_len;
2321 : : if (maxsizei % BITS_PER_UNIT)
2322 : : shift_bytes_in_array_right (buffer, needed_len,
2323 : : BITS_PER_UNIT
2324 : : - (maxsizei % BITS_PER_UNIT));
2325 : : if (INTEGRAL_TYPE_P (type))
2326 : : {
2327 : : if (TYPE_MODE (type) != BLKmode)
2328 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2329 : : else
2330 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2331 : : }
2332 : : if (sz > needed_len)
2333 : : {
2334 : : memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2335 : : val = native_interpret_expr (type, this_buffer, sz);
2336 : : }
2337 : : else
2338 : : val = native_interpret_expr (type, buffer, needed_len);
2339 : : }
2340 : : else
2341 : 7757 : val = native_interpret_expr (type, buffer, bufsize);
2342 : : /* If we chop off bits because the types precision doesn't match the memory
2343 : : access size this is ok when optimizing reads but not when called from
2344 : : the DSE code during elimination. */
2345 : 7757 : if (val && type != vr->type)
2346 : : {
2347 : 13 : if (! int_fits_type_p (val, vr->type))
2348 : : val = NULL_TREE;
2349 : : else
2350 : 13 : val = fold_convert (vr->type, val);
2351 : : }
2352 : :
2353 : 7753 : if (val)
2354 : : {
2355 : 7753 : if (dump_file && (dump_flags & TDF_DETAILS))
2356 : 0 : fprintf (dump_file,
2357 : : "Successfully combined %u partial definitions\n", ndefs);
2358 : : /* We are using the alias-set of the first store we encounter which
2359 : : should be appropriate here. */
2360 : 7753 : return finish (first_set, first_base_set, val);
2361 : : }
2362 : : else
2363 : : {
2364 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
2365 : 0 : fprintf (dump_file,
2366 : : "Failed to interpret %u encoded partial definitions\n", ndefs);
2367 : 4 : return (void *)-1;
2368 : : }
2369 : : }
2370 : :
2371 : : /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2372 : : with the current VUSE and performs the expression lookup. */
2373 : :
2374 : : static void *
2375 : 1031391495 : vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2376 : : {
2377 : 1031391495 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2378 : 1031391495 : vn_reference_t vr = data->vr;
2379 : 1031391495 : vn_reference_s **slot;
2380 : 1031391495 : hashval_t hash;
2381 : :
2382 : : /* If we have partial definitions recorded we have to go through
2383 : : vn_reference_lookup_3. */
2384 : 2055171642 : if (!data->partial_defs.is_empty ())
2385 : : return NULL;
2386 : :
2387 : 1030540111 : if (data->last_vuse_ptr)
2388 : : {
2389 : 1010832599 : *data->last_vuse_ptr = vuse;
2390 : 1010832599 : data->last_vuse = vuse;
2391 : : }
2392 : :
2393 : : /* Fixup vuse and hash. */
2394 : 1030540111 : if (vr->vuse)
2395 : 1030540111 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2396 : 1030540111 : vr->vuse = vuse_ssa_val (vuse);
2397 : 1030540111 : if (vr->vuse)
2398 : 1030540111 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2399 : :
2400 : 1030540111 : hash = vr->hashcode;
2401 : 1030540111 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2402 : 1030540111 : if (slot)
2403 : : {
2404 : 7610249 : if ((*slot)->result && data->saved_operands.exists ())
2405 : 331993 : return data->finish (vr->set, vr->base_set, (*slot)->result);
2406 : : return *slot;
2407 : : }
2408 : :
2409 : 1022929862 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2410 : : {
2411 : 16887913 : HOST_WIDE_INT op_offset, op_size;
2412 : 16887913 : tree v = NULL_TREE;
2413 : 16887913 : tree base = ao_ref_base (op);
2414 : :
2415 : 16887913 : if (base
2416 : 16887913 : && op->offset.is_constant (&op_offset)
2417 : 16887913 : && op->size.is_constant (&op_size)
2418 : 16887913 : && op->max_size_known_p ()
2419 : 33329551 : && known_eq (op->size, op->max_size))
2420 : : {
2421 : 16137363 : if (TREE_CODE (base) == PARM_DECL)
2422 : 679851 : v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2423 : : op_size);
2424 : 15457512 : else if (TREE_CODE (base) == MEM_REF
2425 : 6190895 : && integer_zerop (TREE_OPERAND (base, 1))
2426 : 5052840 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2427 : 5046880 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2428 : 18729673 : && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2429 : : == PARM_DECL))
2430 : 3220987 : v = ipcp_get_aggregate_const (cfun,
2431 : 3220987 : SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2432 : : true, op_offset, op_size);
2433 : : }
2434 : 3900838 : if (v)
2435 : 1099 : return data->finish (vr->set, vr->base_set, v);
2436 : : }
2437 : :
2438 : : return NULL;
2439 : : }
2440 : :
2441 : : /* Lookup an existing or insert a new vn_reference entry into the
2442 : : value table for the VUSE, SET, TYPE, OPERANDS reference which
2443 : : has the value VALUE which is either a constant or an SSA name. */
2444 : :
2445 : : static vn_reference_t
2446 : 1451044 : vn_reference_lookup_or_insert_for_pieces (tree vuse,
2447 : : alias_set_type set,
2448 : : alias_set_type base_set,
2449 : : poly_int64 offset,
2450 : : poly_int64 max_size,
2451 : : tree type,
2452 : : vec<vn_reference_op_s,
2453 : : va_heap> operands,
2454 : : tree value)
2455 : : {
2456 : 1451044 : vn_reference_s vr1;
2457 : 1451044 : vn_reference_t result;
2458 : 1451044 : unsigned value_id;
2459 : 1451044 : vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2460 : 1451044 : vr1.operands = operands;
2461 : 1451044 : vr1.type = type;
2462 : 1451044 : vr1.set = set;
2463 : 1451044 : vr1.base_set = base_set;
2464 : 1451044 : vr1.offset = offset;
2465 : 1451044 : vr1.max_size = max_size;
2466 : 1451044 : vr1.hashcode = vn_reference_compute_hash (&vr1);
2467 : 1451044 : if (vn_reference_lookup_1 (&vr1, &result))
2468 : 3371 : return result;
2469 : :
2470 : 1447673 : if (TREE_CODE (value) == SSA_NAME)
2471 : 271524 : value_id = VN_INFO (value)->value_id;
2472 : : else
2473 : 1176149 : value_id = get_or_alloc_constant_value_id (value);
2474 : 1447673 : return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
2475 : 1447673 : type, operands.copy (), value, value_id);
2476 : : }
2477 : :
2478 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2479 : : value-number for the possibly simplified result or by inserting the
2480 : : operation if INSERT is true. If SIMPLIFY is false, return a value
2481 : : number for the unsimplified expression. */
2482 : :
2483 : : static tree
2484 : 18262488 : vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2485 : : bool simplify)
2486 : : {
2487 : 18262488 : tree result = NULL_TREE;
2488 : : /* We will be creating a value number for
2489 : : RCODE (OPS...).
2490 : : So first simplify and lookup this expression to see if it
2491 : : is already available. */
2492 : : /* For simplification valueize. */
2493 : 18262488 : unsigned i = 0;
2494 : 18262488 : if (simplify)
2495 : 42443440 : for (i = 0; i < res_op->num_ops; ++i)
2496 : 24186444 : if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2497 : : {
2498 : 15424447 : tree tem = vn_valueize (res_op->ops[i]);
2499 : 15424447 : if (!tem)
2500 : : break;
2501 : 15424447 : res_op->ops[i] = tem;
2502 : : }
2503 : : /* If valueization of an operand fails (it is not available), skip
2504 : : simplification. */
2505 : 18262488 : bool res = false;
2506 : 18262488 : if (i == res_op->num_ops)
2507 : : {
2508 : : /* Do not leak not available operands into the simplified expression
2509 : : when called from PRE context. */
2510 : 18256996 : if (rpo_avail)
2511 : 10844408 : mprts_hook = vn_lookup_simplify_result;
2512 : 18256996 : res = res_op->resimplify (NULL, vn_valueize);
2513 : 18256996 : mprts_hook = NULL;
2514 : : }
2515 : 31443848 : gimple *new_stmt = NULL;
2516 : 18256996 : if (res
2517 : 18256996 : && gimple_simplified_result_is_gimple_val (res_op))
2518 : : {
2519 : : /* The expression is already available. */
2520 : 5075636 : result = res_op->ops[0];
2521 : : /* Valueize it, simplification returns sth in AVAIL only. */
2522 : 5075636 : if (TREE_CODE (result) == SSA_NAME)
2523 : 277852 : result = SSA_VAL (result);
2524 : : }
2525 : : else
2526 : : {
2527 : 13186852 : tree val = vn_lookup_simplify_result (res_op);
2528 : 13186852 : if (!val && insert)
2529 : : {
2530 : 170296 : gimple_seq stmts = NULL;
2531 : 170296 : result = maybe_push_res_to_seq (res_op, &stmts);
2532 : 170296 : if (result)
2533 : : {
2534 : 170296 : gcc_assert (gimple_seq_singleton_p (stmts));
2535 : 170296 : new_stmt = gimple_seq_first_stmt (stmts);
2536 : : }
2537 : : }
2538 : : else
2539 : : /* The expression is already available. */
2540 : : result = val;
2541 : : }
2542 : 277852 : if (new_stmt)
2543 : : {
2544 : : /* The expression is not yet available, value-number lhs to
2545 : : the new SSA_NAME we created. */
2546 : : /* Initialize value-number information properly. */
2547 : 170296 : vn_ssa_aux_t result_info = VN_INFO (result);
2548 : 170296 : result_info->valnum = result;
2549 : 170296 : result_info->value_id = get_next_value_id ();
2550 : 170296 : result_info->visited = 1;
2551 : 170296 : gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2552 : : new_stmt);
2553 : 170296 : result_info->needs_insertion = true;
2554 : : /* ??? PRE phi-translation inserts NARYs without corresponding
2555 : : SSA name result. Re-use those but set their result according
2556 : : to the stmt we just built. */
2557 : 170296 : vn_nary_op_t nary = NULL;
2558 : 170296 : vn_nary_op_lookup_stmt (new_stmt, &nary);
2559 : 170296 : if (nary)
2560 : : {
2561 : 0 : gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2562 : 0 : nary->u.result = gimple_assign_lhs (new_stmt);
2563 : : }
2564 : : /* As all "inserted" statements are singleton SCCs, insert
2565 : : to the valid table. This is strictly needed to
2566 : : avoid re-generating new value SSA_NAMEs for the same
2567 : : expression during SCC iteration over and over (the
2568 : : optimistic table gets cleared after each iteration).
2569 : : We do not need to insert into the optimistic table, as
2570 : : lookups there will fall back to the valid table. */
2571 : : else
2572 : : {
2573 : 170296 : unsigned int length = vn_nary_length_from_stmt (new_stmt);
2574 : 170296 : vn_nary_op_t vno1
2575 : 170296 : = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2576 : 170296 : vno1->value_id = result_info->value_id;
2577 : 170296 : vno1->length = length;
2578 : 170296 : vno1->predicated_values = 0;
2579 : 170296 : vno1->u.result = result;
2580 : 170296 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2581 : 170296 : vn_nary_op_insert_into (vno1, valid_info->nary);
2582 : : /* Also do not link it into the undo chain. */
2583 : 170296 : last_inserted_nary = vno1->next;
2584 : 170296 : vno1->next = (vn_nary_op_t)(void *)-1;
2585 : : }
2586 : 170296 : if (dump_file && (dump_flags & TDF_DETAILS))
2587 : : {
2588 : 588 : fprintf (dump_file, "Inserting name ");
2589 : 588 : print_generic_expr (dump_file, result);
2590 : 588 : fprintf (dump_file, " for expression ");
2591 : 588 : print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2592 : 588 : fprintf (dump_file, "\n");
2593 : : }
2594 : : }
2595 : 18262488 : return result;
2596 : : }
2597 : :
2598 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2599 : : value-number for the simplified result or by inserting the operation. */
2600 : :
2601 : : static tree
2602 : 215826 : vn_nary_build_or_lookup (gimple_match_op *res_op)
2603 : : {
2604 : 0 : return vn_nary_build_or_lookup_1 (res_op, true, true);
2605 : : }
2606 : :
2607 : : /* Try to simplify the expression RCODE OPS... of type TYPE and return
2608 : : its value if present. Update NARY with a simplified expression if
2609 : : it fits. */
2610 : :
2611 : : tree
2612 : 7407330 : vn_nary_simplify (vn_nary_op_t nary)
2613 : : {
2614 : 7407330 : if (nary->length > gimple_match_op::MAX_NUM_OPS
2615 : : /* For CONSTRUCTOR the vn_nary_op_t and gimple_match_op representation
2616 : : does not match. */
2617 : 7407154 : || nary->opcode == CONSTRUCTOR)
2618 : : return NULL_TREE;
2619 : 7406761 : gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2620 : 7406761 : nary->type, nary->length);
2621 : 7406761 : memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2622 : 7406761 : tree res = vn_nary_build_or_lookup_1 (&op, false, true);
2623 : 7406761 : if (op.code.is_tree_code ()
2624 : 7406761 : && op.num_ops <= nary->length
2625 : 14812611 : && (tree_code) op.code != CONSTRUCTOR)
2626 : : {
2627 : 7405849 : nary->opcode = (tree_code) op.code;
2628 : 7405849 : nary->length = op.num_ops;
2629 : 19405293 : for (unsigned i = 0; i < op.num_ops; ++i)
2630 : 11999444 : nary->op[i] = op.ops[i];
2631 : : }
2632 : : return res;
2633 : : }
2634 : :
2635 : : /* Elimination engine. */
2636 : :
2637 : : class eliminate_dom_walker : public dom_walker
2638 : : {
2639 : : public:
2640 : : eliminate_dom_walker (cdi_direction, bitmap);
2641 : : ~eliminate_dom_walker ();
2642 : :
2643 : : edge before_dom_children (basic_block) final override;
2644 : : void after_dom_children (basic_block) final override;
2645 : :
2646 : : virtual tree eliminate_avail (basic_block, tree op);
2647 : : virtual void eliminate_push_avail (basic_block, tree op);
2648 : : tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2649 : :
2650 : : void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2651 : :
2652 : : unsigned eliminate_cleanup (bool region_p = false);
2653 : :
2654 : : bool do_pre;
2655 : : unsigned int el_todo;
2656 : : unsigned int eliminations;
2657 : : unsigned int insertions;
2658 : :
2659 : : /* SSA names that had their defs inserted by PRE if do_pre. */
2660 : : bitmap inserted_exprs;
2661 : :
2662 : : /* Blocks with statements that have had their EH properties changed. */
2663 : : bitmap need_eh_cleanup;
2664 : :
2665 : : /* Blocks with statements that have had their AB properties changed. */
2666 : : bitmap need_ab_cleanup;
2667 : :
2668 : : /* Local state for the eliminate domwalk. */
2669 : : auto_vec<gimple *> to_remove;
2670 : : auto_vec<gimple *> to_fixup;
2671 : : auto_vec<tree> avail;
2672 : : auto_vec<tree> avail_stack;
2673 : : };
2674 : :
2675 : : /* Adaptor to the elimination engine using RPO availability. */
2676 : :
2677 : 12131666 : class rpo_elim : public eliminate_dom_walker
2678 : : {
2679 : : public:
2680 : 6065833 : rpo_elim(basic_block entry_)
2681 : 12131666 : : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2682 : 12131666 : m_avail_freelist (NULL) {}
2683 : :
2684 : : tree eliminate_avail (basic_block, tree op) final override;
2685 : :
2686 : : void eliminate_push_avail (basic_block, tree) final override;
2687 : :
2688 : : basic_block entry;
2689 : : /* Freelist of avail entries which are allocated from the vn_ssa_aux
2690 : : obstack. */
2691 : : vn_avail *m_avail_freelist;
2692 : : };
2693 : :
2694 : : /* Return true if BASE1 and BASE2 can be adjusted so they have the
2695 : : same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2696 : : Otherwise return false. */
2697 : :
2698 : : static bool
2699 : 5963634 : adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2700 : : tree base2, poly_int64 *offset2)
2701 : : {
2702 : 5963634 : poly_int64 soff;
2703 : 5963634 : if (TREE_CODE (base1) == MEM_REF
2704 : 2421520 : && TREE_CODE (base2) == MEM_REF)
2705 : : {
2706 : 1972186 : if (mem_ref_offset (base1).to_shwi (&soff))
2707 : : {
2708 : 1972186 : base1 = TREE_OPERAND (base1, 0);
2709 : 1972186 : *offset1 += soff * BITS_PER_UNIT;
2710 : : }
2711 : 1972186 : if (mem_ref_offset (base2).to_shwi (&soff))
2712 : : {
2713 : 1972186 : base2 = TREE_OPERAND (base2, 0);
2714 : 1972186 : *offset2 += soff * BITS_PER_UNIT;
2715 : : }
2716 : 1972186 : return operand_equal_p (base1, base2, 0);
2717 : : }
2718 : 3991448 : return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2719 : : }
2720 : :
2721 : : /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2722 : : from the statement defining VUSE and if not successful tries to
2723 : : translate *REFP and VR_ through an aggregate copy at the definition
2724 : : of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2725 : : of *REF and *VR. If only disambiguation was performed then
2726 : : *DISAMBIGUATE_ONLY is set to true. */
2727 : :
2728 : : static void *
2729 : 42588493 : vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2730 : : translate_flags *disambiguate_only)
2731 : : {
2732 : 42588493 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2733 : 42588493 : vn_reference_t vr = data->vr;
2734 : 42588493 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2735 : 42588493 : tree base = ao_ref_base (ref);
2736 : 42588493 : HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2737 : 42588493 : static vec<vn_reference_op_s> lhs_ops;
2738 : 42588493 : ao_ref lhs_ref;
2739 : 42588493 : bool lhs_ref_ok = false;
2740 : 42588493 : poly_int64 copy_size;
2741 : :
2742 : : /* First try to disambiguate after value-replacing in the definitions LHS. */
2743 : 42588493 : if (is_gimple_assign (def_stmt))
2744 : : {
2745 : 21645051 : tree lhs = gimple_assign_lhs (def_stmt);
2746 : 21645051 : bool valueized_anything = false;
2747 : : /* Avoid re-allocation overhead. */
2748 : 21645051 : lhs_ops.truncate (0);
2749 : 21645051 : basic_block saved_rpo_bb = vn_context_bb;
2750 : 21645051 : vn_context_bb = gimple_bb (def_stmt);
2751 : 21645051 : if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2752 : : {
2753 : 14969696 : copy_reference_ops_from_ref (lhs, &lhs_ops);
2754 : 14969696 : valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2755 : : }
2756 : 21645051 : vn_context_bb = saved_rpo_bb;
2757 : 21645051 : ao_ref_init (&lhs_ref, lhs);
2758 : 21645051 : lhs_ref_ok = true;
2759 : 21645051 : if (valueized_anything
2760 : 1696869 : && ao_ref_init_from_vn_reference
2761 : 1696869 : (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2762 : 1696869 : ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2763 : 23341920 : && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2764 : : {
2765 : 1437871 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2766 : 7705863 : return NULL;
2767 : : }
2768 : :
2769 : : /* When the def is a CLOBBER we can optimistically disambiguate
2770 : : against it since any overlap it would be undefined behavior.
2771 : : Avoid this for obvious must aliases to save compile-time though.
2772 : : We also may not do this when the query is used for redundant
2773 : : store removal. */
2774 : 20207180 : if (!data->redundant_store_removal_p
2775 : 10415532 : && gimple_clobber_p (def_stmt)
2776 : 20655482 : && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2777 : : {
2778 : 420612 : *disambiguate_only = TR_DISAMBIGUATE;
2779 : 420612 : return NULL;
2780 : : }
2781 : :
2782 : : /* Besides valueizing the LHS we can also use access-path based
2783 : : disambiguation on the original non-valueized ref. */
2784 : 19786568 : if (!ref->ref
2785 : : && lhs_ref_ok
2786 : 2639937 : && data->orig_ref.ref)
2787 : : {
2788 : : /* We want to use the non-valueized LHS for this, but avoid redundant
2789 : : work. */
2790 : 1786754 : ao_ref *lref = &lhs_ref;
2791 : 1786754 : ao_ref lref_alt;
2792 : 1786754 : if (valueized_anything)
2793 : : {
2794 : 110470 : ao_ref_init (&lref_alt, lhs);
2795 : 110470 : lref = &lref_alt;
2796 : : }
2797 : 1786754 : if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2798 : : {
2799 : 240324 : *disambiguate_only = (valueized_anything
2800 : 120162 : ? TR_VALUEIZE_AND_DISAMBIGUATE
2801 : : : TR_DISAMBIGUATE);
2802 : 120162 : return NULL;
2803 : : }
2804 : : }
2805 : :
2806 : : /* If we reach a clobbering statement try to skip it and see if
2807 : : we find a VN result with exactly the same value as the
2808 : : possible clobber. In this case we can ignore the clobber
2809 : : and return the found value. */
2810 : 19666406 : if (is_gimple_reg_type (TREE_TYPE (lhs))
2811 : 12470536 : && types_compatible_p (TREE_TYPE (lhs), vr->type)
2812 : 9622490 : && (ref->ref || data->orig_ref.ref)
2813 : 9214511 : && !data->mask
2814 : 9191825 : && data->partial_defs.is_empty ()
2815 : 9190958 : && multiple_p (get_object_alignment
2816 : : (ref->ref ? ref->ref : data->orig_ref.ref),
2817 : : ref->size)
2818 : 36678943 : && multiple_p (get_object_alignment (lhs), ref->size))
2819 : : {
2820 : 8831348 : tree rhs = gimple_assign_rhs1 (def_stmt);
2821 : : /* ??? We may not compare to ahead values which might be from
2822 : : a different loop iteration but only to loop invariants. Use
2823 : : CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2824 : : The one-hop lookup below doesn't have this issue since there's
2825 : : a virtual PHI before we ever reach a backedge to cross.
2826 : : We can skip multiple defs as long as they are from the same
2827 : : value though. */
2828 : 8831348 : if (data->same_val
2829 : 8831348 : && !operand_equal_p (data->same_val, rhs))
2830 : : ;
2831 : 8528820 : else if (CONSTANT_CLASS_P (rhs))
2832 : : {
2833 : 4226925 : if (dump_file && (dump_flags & TDF_DETAILS))
2834 : : {
2835 : 1895 : fprintf (dump_file,
2836 : : "Skipping possible redundant definition ");
2837 : 1895 : print_gimple_stmt (dump_file, def_stmt, 0);
2838 : : }
2839 : : /* Delay the actual compare of the values to the end of the walk
2840 : : but do not update last_vuse from here. */
2841 : 4226925 : data->last_vuse_ptr = NULL;
2842 : 4226925 : data->same_val = rhs;
2843 : 4226925 : return NULL;
2844 : : }
2845 : : else
2846 : : {
2847 : 4301895 : tree saved_vuse = vr->vuse;
2848 : 4301895 : hashval_t saved_hashcode = vr->hashcode;
2849 : 4301895 : if (vr->vuse)
2850 : 4301895 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2851 : 8603790 : vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
2852 : 4301895 : if (vr->vuse)
2853 : 4301895 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2854 : 4301895 : vn_reference_t vnresult = NULL;
2855 : : /* Do not use vn_reference_lookup_2 since that might perform
2856 : : expression hashtable insertion but this lookup crosses
2857 : : a possible may-alias making such insertion conditionally
2858 : : invalid. */
2859 : 4301895 : vn_reference_lookup_1 (vr, &vnresult);
2860 : : /* Need to restore vr->vuse and vr->hashcode. */
2861 : 4301895 : vr->vuse = saved_vuse;
2862 : 4301895 : vr->hashcode = saved_hashcode;
2863 : 4301895 : if (vnresult)
2864 : : {
2865 : 244433 : if (TREE_CODE (rhs) == SSA_NAME)
2866 : 239462 : rhs = SSA_VAL (rhs);
2867 : 244433 : if (vnresult->result
2868 : 244433 : && operand_equal_p (vnresult->result, rhs, 0))
2869 : 62422 : return vnresult;
2870 : : }
2871 : : }
2872 : : }
2873 : : }
2874 : 20943442 : else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2875 : 19547376 : && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2876 : 22993091 : && gimple_call_num_args (def_stmt) <= 4)
2877 : : {
2878 : : /* For builtin calls valueize its arguments and call the
2879 : : alias oracle again. Valueization may improve points-to
2880 : : info of pointers and constify size and position arguments.
2881 : : Originally this was motivated by PR61034 which has
2882 : : conditional calls to free falsely clobbering ref because
2883 : : of imprecise points-to info of the argument. */
2884 : : tree oldargs[4];
2885 : : bool valueized_anything = false;
2886 : 4972699 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2887 : : {
2888 : 3441137 : oldargs[i] = gimple_call_arg (def_stmt, i);
2889 : 3441137 : tree val = vn_valueize (oldargs[i]);
2890 : 3441137 : if (val != oldargs[i])
2891 : : {
2892 : 128431 : gimple_call_set_arg (def_stmt, i, val);
2893 : 128431 : valueized_anything = true;
2894 : : }
2895 : : }
2896 : 1531562 : if (valueized_anything)
2897 : : {
2898 : 191178 : bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2899 : 95589 : ref, data->tbaa_p);
2900 : 352232 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2901 : 256643 : gimple_call_set_arg (def_stmt, i, oldargs[i]);
2902 : 95589 : if (!res)
2903 : : {
2904 : 23907 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2905 : 23907 : return NULL;
2906 : : }
2907 : : }
2908 : : }
2909 : :
2910 : 36296594 : if (*disambiguate_only > TR_TRANSLATE)
2911 : : return (void *)-1;
2912 : :
2913 : : /* If we cannot constrain the size of the reference we cannot
2914 : : test if anything kills it. */
2915 : 24908016 : if (!ref->max_size_known_p ())
2916 : : return (void *)-1;
2917 : :
2918 : 24494566 : poly_int64 offset = ref->offset;
2919 : 24494566 : poly_int64 maxsize = ref->max_size;
2920 : :
2921 : : /* def_stmt may-defs *ref. See if we can derive a value for *ref
2922 : : from that definition.
2923 : : 1) Memset. */
2924 : 24494566 : if (is_gimple_reg_type (vr->type)
2925 : 24168757 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2926 : 24083776 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
2927 : 85523 : && (integer_zerop (gimple_call_arg (def_stmt, 1))
2928 : 30246 : || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2929 : 8246 : || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2930 : : && CHAR_BIT == 8
2931 : : && BITS_PER_UNIT == 8
2932 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2933 : 29326 : && offset.is_constant (&offseti)
2934 : 29326 : && ref->size.is_constant (&sizei)
2935 : 29326 : && (offseti % BITS_PER_UNIT == 0
2936 : 39 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
2937 : 84603 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2938 : 35835 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
2939 : 35835 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
2940 : 24543876 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2941 : 27552 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2942 : : {
2943 : 49269 : tree base2;
2944 : 49269 : poly_int64 offset2, size2, maxsize2;
2945 : 49269 : bool reverse;
2946 : 49269 : tree ref2 = gimple_call_arg (def_stmt, 0);
2947 : 49269 : if (TREE_CODE (ref2) == SSA_NAME)
2948 : : {
2949 : 27511 : ref2 = SSA_VAL (ref2);
2950 : 27511 : if (TREE_CODE (ref2) == SSA_NAME
2951 : 27511 : && (TREE_CODE (base) != MEM_REF
2952 : 17758 : || TREE_OPERAND (base, 0) != ref2))
2953 : : {
2954 : 21613 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2955 : 21613 : if (gimple_assign_single_p (def_stmt)
2956 : 21613 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2957 : 697 : ref2 = gimple_assign_rhs1 (def_stmt);
2958 : : }
2959 : : }
2960 : 49269 : if (TREE_CODE (ref2) == ADDR_EXPR)
2961 : : {
2962 : 25431 : ref2 = TREE_OPERAND (ref2, 0);
2963 : 25431 : base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
2964 : : &reverse);
2965 : 25431 : if (!known_size_p (maxsize2)
2966 : 25391 : || !known_eq (maxsize2, size2)
2967 : 50748 : || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
2968 : 52988 : return (void *)-1;
2969 : : }
2970 : 23838 : else if (TREE_CODE (ref2) == SSA_NAME)
2971 : : {
2972 : 23838 : poly_int64 soff;
2973 : 23838 : if (TREE_CODE (base) != MEM_REF
2974 : 41111 : || !(mem_ref_offset (base)
2975 : 34546 : << LOG2_BITS_PER_UNIT).to_shwi (&soff))
2976 : 20425 : return (void *)-1;
2977 : 17273 : offset += soff;
2978 : 17273 : offset2 = 0;
2979 : 17273 : if (TREE_OPERAND (base, 0) != ref2)
2980 : : {
2981 : 14351 : gimple *def = SSA_NAME_DEF_STMT (ref2);
2982 : 14351 : if (is_gimple_assign (def)
2983 : 13318 : && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
2984 : 11361 : && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
2985 : 14872 : && poly_int_tree_p (gimple_assign_rhs2 (def)))
2986 : : {
2987 : 491 : tree rhs2 = gimple_assign_rhs2 (def);
2988 : 491 : if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
2989 : : SIGNED)
2990 : 491 : << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
2991 : : return (void *)-1;
2992 : 491 : ref2 = gimple_assign_rhs1 (def);
2993 : 491 : if (TREE_CODE (ref2) == SSA_NAME)
2994 : 491 : ref2 = SSA_VAL (ref2);
2995 : : }
2996 : : else
2997 : : return (void *)-1;
2998 : : }
2999 : : }
3000 : : else
3001 : : return (void *)-1;
3002 : 24773 : tree len = gimple_call_arg (def_stmt, 2);
3003 : 24773 : HOST_WIDE_INT leni, offset2i;
3004 : 24773 : if (TREE_CODE (len) == SSA_NAME)
3005 : 254 : len = SSA_VAL (len);
3006 : : /* Sometimes the above trickery is smarter than alias analysis. Take
3007 : : advantage of that. */
3008 : 24773 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
3009 : 49546 : (wi::to_poly_offset (len)
3010 : 24773 : << LOG2_BITS_PER_UNIT)))
3011 : : return NULL;
3012 : 49512 : if (data->partial_defs.is_empty ()
3013 : 24739 : && known_subrange_p (offset, maxsize, offset2,
3014 : 24739 : wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
3015 : : {
3016 : 24315 : tree val;
3017 : 24315 : if (integer_zerop (gimple_call_arg (def_stmt, 1)))
3018 : 20797 : val = build_zero_cst (vr->type);
3019 : 3518 : else if (INTEGRAL_TYPE_P (vr->type)
3020 : 3189 : && known_eq (ref->size, 8)
3021 : 5938 : && offseti % BITS_PER_UNIT == 0)
3022 : : {
3023 : 2420 : gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
3024 : 2420 : vr->type, gimple_call_arg (def_stmt, 1));
3025 : 2420 : val = vn_nary_build_or_lookup (&res_op);
3026 : 2420 : if (!val
3027 : 2420 : || (TREE_CODE (val) == SSA_NAME
3028 : 626 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3029 : 0 : return (void *)-1;
3030 : : }
3031 : : else
3032 : : {
3033 : 1098 : unsigned buflen
3034 : 1098 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
3035 : 1098 : if (INTEGRAL_TYPE_P (vr->type)
3036 : 1098 : && TYPE_MODE (vr->type) != BLKmode)
3037 : 1536 : buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
3038 : 1098 : unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
3039 : 1098 : memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
3040 : : buflen);
3041 : 1098 : if (BYTES_BIG_ENDIAN)
3042 : : {
3043 : : unsigned int amnt
3044 : : = (((unsigned HOST_WIDE_INT) offseti + sizei)
3045 : : % BITS_PER_UNIT);
3046 : : if (amnt)
3047 : : {
3048 : : shift_bytes_in_array_right (buf, buflen,
3049 : : BITS_PER_UNIT - amnt);
3050 : : buf++;
3051 : : buflen--;
3052 : : }
3053 : : }
3054 : 1098 : else if (offseti % BITS_PER_UNIT != 0)
3055 : : {
3056 : 7 : unsigned int amnt
3057 : : = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3058 : 7 : % BITS_PER_UNIT);
3059 : 7 : shift_bytes_in_array_left (buf, buflen, amnt);
3060 : 7 : buf++;
3061 : 7 : buflen--;
3062 : : }
3063 : 1098 : val = native_interpret_expr (vr->type, buf, buflen);
3064 : 1098 : if (!val)
3065 : : return (void *)-1;
3066 : : }
3067 : 24315 : return data->finish (0, 0, val);
3068 : : }
3069 : : /* For now handle clearing memory with partial defs. */
3070 : 458 : else if (known_eq (ref->size, maxsize)
3071 : 392 : && integer_zerop (gimple_call_arg (def_stmt, 1))
3072 : 110 : && tree_fits_poly_int64_p (len)
3073 : 106 : && tree_to_poly_int64 (len).is_constant (&leni)
3074 : 106 : && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3075 : 106 : && offset.is_constant (&offseti)
3076 : 106 : && offset2.is_constant (&offset2i)
3077 : 106 : && maxsize.is_constant (&maxsizei)
3078 : 458 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3079 : 458 : leni << LOG2_BITS_PER_UNIT))
3080 : : {
3081 : 106 : pd_data pd;
3082 : 106 : pd.rhs = build_constructor (NULL_TREE, NULL);
3083 : 106 : pd.rhs_off = 0;
3084 : 106 : pd.offset = offset2i;
3085 : 106 : pd.size = leni << LOG2_BITS_PER_UNIT;
3086 : 106 : return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3087 : : }
3088 : : }
3089 : :
3090 : : /* 2) Assignment from an empty CONSTRUCTOR. */
3091 : 24445297 : else if (is_gimple_reg_type (vr->type)
3092 : 24119488 : && gimple_assign_single_p (def_stmt)
3093 : 8893896 : && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3094 : 27906501 : && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
3095 : : {
3096 : 3461204 : tree base2;
3097 : 3461204 : poly_int64 offset2, size2, maxsize2;
3098 : 3461204 : HOST_WIDE_INT offset2i, size2i;
3099 : 3461204 : gcc_assert (lhs_ref_ok);
3100 : 3461204 : base2 = ao_ref_base (&lhs_ref);
3101 : 3461204 : offset2 = lhs_ref.offset;
3102 : 3461204 : size2 = lhs_ref.size;
3103 : 3461204 : maxsize2 = lhs_ref.max_size;
3104 : 3461204 : if (known_size_p (maxsize2)
3105 : 3460813 : && known_eq (maxsize2, size2)
3106 : 6921058 : && adjust_offsets_for_equal_base_address (base, &offset,
3107 : : base2, &offset2))
3108 : : {
3109 : 3399055 : if (data->partial_defs.is_empty ()
3110 : 3395315 : && known_subrange_p (offset, maxsize, offset2, size2))
3111 : : {
3112 : : /* While technically undefined behavior do not optimize
3113 : : a full read from a clobber. */
3114 : 3382814 : if (gimple_clobber_p (def_stmt))
3115 : 3398738 : return (void *)-1;
3116 : 987165 : tree val = build_zero_cst (vr->type);
3117 : 987165 : return data->finish (ao_ref_alias_set (&lhs_ref),
3118 : 987165 : ao_ref_base_alias_set (&lhs_ref), val);
3119 : : }
3120 : 16241 : else if (known_eq (ref->size, maxsize)
3121 : 15924 : && maxsize.is_constant (&maxsizei)
3122 : 15924 : && offset.is_constant (&offseti)
3123 : 15924 : && offset2.is_constant (&offset2i)
3124 : 15924 : && size2.is_constant (&size2i)
3125 : 16241 : && ranges_known_overlap_p (offseti, maxsizei,
3126 : : offset2i, size2i))
3127 : : {
3128 : : /* Let clobbers be consumed by the partial-def tracker
3129 : : which can choose to ignore them if they are shadowed
3130 : : by a later def. */
3131 : 15924 : pd_data pd;
3132 : 15924 : pd.rhs = gimple_assign_rhs1 (def_stmt);
3133 : 15924 : pd.rhs_off = 0;
3134 : 15924 : pd.offset = offset2i;
3135 : 15924 : pd.size = size2i;
3136 : 15924 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3137 : : ao_ref_base_alias_set (&lhs_ref),
3138 : : offseti, maxsizei);
3139 : : }
3140 : : }
3141 : : }
3142 : :
3143 : : /* 3) Assignment from a constant. We can use folds native encode/interpret
3144 : : routines to extract the assigned bits. */
3145 : 20984093 : else if (known_eq (ref->size, maxsize)
3146 : 20480644 : && is_gimple_reg_type (vr->type)
3147 : 20154835 : && !reverse_storage_order_for_component_p (vr->operands)
3148 : 20152110 : && !contains_storage_order_barrier_p (vr->operands)
3149 : 20152110 : && gimple_assign_single_p (def_stmt)
3150 : : && CHAR_BIT == 8
3151 : : && BITS_PER_UNIT == 8
3152 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3153 : : /* native_encode and native_decode operate on arrays of bytes
3154 : : and so fundamentally need a compile-time size and offset. */
3155 : 5126896 : && maxsize.is_constant (&maxsizei)
3156 : 5126896 : && offset.is_constant (&offseti)
3157 : 26110989 : && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3158 : 4394126 : || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3159 : 1906389 : && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3160 : : {
3161 : 748447 : tree lhs = gimple_assign_lhs (def_stmt);
3162 : 748447 : tree base2;
3163 : 748447 : poly_int64 offset2, size2, maxsize2;
3164 : 748447 : HOST_WIDE_INT offset2i, size2i;
3165 : 748447 : bool reverse;
3166 : 748447 : gcc_assert (lhs_ref_ok);
3167 : 748447 : base2 = ao_ref_base (&lhs_ref);
3168 : 748447 : offset2 = lhs_ref.offset;
3169 : 748447 : size2 = lhs_ref.size;
3170 : 748447 : maxsize2 = lhs_ref.max_size;
3171 : 748447 : reverse = reverse_storage_order_for_component_p (lhs);
3172 : 748447 : if (base2
3173 : 748447 : && !reverse
3174 : 747619 : && !storage_order_barrier_p (lhs)
3175 : 747619 : && known_eq (maxsize2, size2)
3176 : 729211 : && adjust_offsets_for_equal_base_address (base, &offset,
3177 : : base2, &offset2)
3178 : 71848 : && offset.is_constant (&offseti)
3179 : 71848 : && offset2.is_constant (&offset2i)
3180 : 748447 : && size2.is_constant (&size2i))
3181 : : {
3182 : 71848 : if (data->partial_defs.is_empty ()
3183 : 57827 : && known_subrange_p (offseti, maxsizei, offset2, size2))
3184 : : {
3185 : : /* We support up to 512-bit values (for V8DFmode). */
3186 : 41860 : unsigned char buffer[65];
3187 : 41860 : int len;
3188 : :
3189 : 41860 : tree rhs = gimple_assign_rhs1 (def_stmt);
3190 : 41860 : if (TREE_CODE (rhs) == SSA_NAME)
3191 : 1325 : rhs = SSA_VAL (rhs);
3192 : 83720 : len = native_encode_expr (rhs,
3193 : : buffer, sizeof (buffer) - 1,
3194 : 41860 : (offseti - offset2i) / BITS_PER_UNIT);
3195 : 41860 : if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
3196 : : {
3197 : 38839 : tree type = vr->type;
3198 : 38839 : unsigned char *buf = buffer;
3199 : 38839 : unsigned int amnt = 0;
3200 : : /* Make sure to interpret in a type that has a range
3201 : : covering the whole access size. */
3202 : 38839 : if (INTEGRAL_TYPE_P (vr->type)
3203 : 38839 : && maxsizei != TYPE_PRECISION (vr->type))
3204 : 1772 : type = build_nonstandard_integer_type (maxsizei,
3205 : 886 : TYPE_UNSIGNED (type));
3206 : 38839 : if (BYTES_BIG_ENDIAN)
3207 : : {
3208 : : /* For big-endian native_encode_expr stored the rhs
3209 : : such that the LSB of it is the LSB of buffer[len - 1].
3210 : : That bit is stored into memory at position
3211 : : offset2 + size2 - 1, i.e. in byte
3212 : : base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3213 : : E.g. for offset2 1 and size2 14, rhs -1 and memory
3214 : : previously cleared that is:
3215 : : 0 1
3216 : : 01111111|11111110
3217 : : Now, if we want to extract offset 2 and size 12 from
3218 : : it using native_interpret_expr (which actually works
3219 : : for integral bitfield types in terms of byte size of
3220 : : the mode), the native_encode_expr stored the value
3221 : : into buffer as
3222 : : XX111111|11111111
3223 : : and returned len 2 (the X bits are outside of
3224 : : precision).
3225 : : Let sz be maxsize / BITS_PER_UNIT if not extracting
3226 : : a bitfield, and GET_MODE_SIZE otherwise.
3227 : : We need to align the LSB of the value we want to
3228 : : extract as the LSB of buf[sz - 1].
3229 : : The LSB from memory we need to read is at position
3230 : : offset + maxsize - 1. */
3231 : : HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3232 : : if (INTEGRAL_TYPE_P (type))
3233 : : {
3234 : : if (TYPE_MODE (type) != BLKmode)
3235 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3236 : : else
3237 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3238 : : }
3239 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3240 : : - offseti - maxsizei) % BITS_PER_UNIT;
3241 : : if (amnt)
3242 : : shift_bytes_in_array_right (buffer, len, amnt);
3243 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3244 : : - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3245 : : if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3246 : : len = 0;
3247 : : else
3248 : : {
3249 : : buf = buffer + len - sz - amnt;
3250 : : len -= (buf - buffer);
3251 : : }
3252 : : }
3253 : : else
3254 : : {
3255 : 38839 : amnt = ((unsigned HOST_WIDE_INT) offset2i
3256 : 38839 : - offseti) % BITS_PER_UNIT;
3257 : 38839 : if (amnt)
3258 : : {
3259 : 305 : buffer[len] = 0;
3260 : 305 : shift_bytes_in_array_left (buffer, len + 1, amnt);
3261 : 305 : buf = buffer + 1;
3262 : : }
3263 : : }
3264 : 38839 : tree val = native_interpret_expr (type, buf, len);
3265 : : /* If we chop off bits because the types precision doesn't
3266 : : match the memory access size this is ok when optimizing
3267 : : reads but not when called from the DSE code during
3268 : : elimination. */
3269 : 38839 : if (val
3270 : 38837 : && type != vr->type)
3271 : : {
3272 : 886 : if (! int_fits_type_p (val, vr->type))
3273 : : val = NULL_TREE;
3274 : : else
3275 : 886 : val = fold_convert (vr->type, val);
3276 : : }
3277 : :
3278 : 38837 : if (val)
3279 : 38837 : return data->finish (ao_ref_alias_set (&lhs_ref),
3280 : 38837 : ao_ref_base_alias_set (&lhs_ref), val);
3281 : : }
3282 : : }
3283 : 29988 : else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3284 : : size2i))
3285 : : {
3286 : 29988 : pd_data pd;
3287 : 29988 : tree rhs = gimple_assign_rhs1 (def_stmt);
3288 : 29988 : if (TREE_CODE (rhs) == SSA_NAME)
3289 : 2588 : rhs = SSA_VAL (rhs);
3290 : 29988 : pd.rhs = rhs;
3291 : 29988 : pd.rhs_off = 0;
3292 : 29988 : pd.offset = offset2i;
3293 : 29988 : pd.size = size2i;
3294 : 29988 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3295 : : ao_ref_base_alias_set (&lhs_ref),
3296 : : offseti, maxsizei);
3297 : : }
3298 : : }
3299 : : }
3300 : :
3301 : : /* 4) Assignment from an SSA name which definition we may be able
3302 : : to access pieces from or we can combine to a larger entity. */
3303 : 20235646 : else if (known_eq (ref->size, maxsize)
3304 : 19732197 : && is_gimple_reg_type (vr->type)
3305 : 19406388 : && !reverse_storage_order_for_component_p (vr->operands)
3306 : 19403663 : && !contains_storage_order_barrier_p (vr->operands)
3307 : 19403663 : && gimple_assign_single_p (def_stmt)
3308 : 24614095 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3309 : : {
3310 : 1890712 : tree lhs = gimple_assign_lhs (def_stmt);
3311 : 1890712 : tree base2;
3312 : 1890712 : poly_int64 offset2, size2, maxsize2;
3313 : 1890712 : HOST_WIDE_INT offset2i, size2i, offseti;
3314 : 1890712 : bool reverse;
3315 : 1890712 : gcc_assert (lhs_ref_ok);
3316 : 1890712 : base2 = ao_ref_base (&lhs_ref);
3317 : 1890712 : offset2 = lhs_ref.offset;
3318 : 1890712 : size2 = lhs_ref.size;
3319 : 1890712 : maxsize2 = lhs_ref.max_size;
3320 : 1890712 : reverse = reverse_storage_order_for_component_p (lhs);
3321 : 1890712 : tree def_rhs = gimple_assign_rhs1 (def_stmt);
3322 : 1890712 : if (!reverse
3323 : 1890500 : && !storage_order_barrier_p (lhs)
3324 : 1890500 : && known_size_p (maxsize2)
3325 : 1868023 : && known_eq (maxsize2, size2)
3326 : 3665267 : && adjust_offsets_for_equal_base_address (base, &offset,
3327 : : base2, &offset2))
3328 : : {
3329 : 79060 : if (data->partial_defs.is_empty ()
3330 : 71472 : && known_subrange_p (offset, maxsize, offset2, size2)
3331 : : /* ??? We can't handle bitfield precision extracts without
3332 : : either using an alternate type for the BIT_FIELD_REF and
3333 : : then doing a conversion or possibly adjusting the offset
3334 : : according to endianness. */
3335 : 48003 : && (! INTEGRAL_TYPE_P (vr->type)
3336 : 34702 : || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3337 : 89764 : && multiple_p (ref->size, BITS_PER_UNIT))
3338 : : {
3339 : 43199 : tree val = NULL_TREE;
3340 : 86392 : if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3341 : 47574 : || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3342 : : {
3343 : 42087 : gimple_match_op op (gimple_match_cond::UNCOND,
3344 : 42087 : BIT_FIELD_REF, vr->type,
3345 : : SSA_VAL (def_rhs),
3346 : : bitsize_int (ref->size),
3347 : 42087 : bitsize_int (offset - offset2));
3348 : 42087 : val = vn_nary_build_or_lookup (&op);
3349 : : }
3350 : 1112 : else if (known_eq (ref->size, size2))
3351 : : {
3352 : 1090 : gimple_match_op op (gimple_match_cond::UNCOND,
3353 : 1090 : VIEW_CONVERT_EXPR, vr->type,
3354 : 1090 : SSA_VAL (def_rhs));
3355 : 1090 : val = vn_nary_build_or_lookup (&op);
3356 : : }
3357 : 43177 : if (val
3358 : 43177 : && (TREE_CODE (val) != SSA_NAME
3359 : 42165 : || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3360 : 43158 : return data->finish (ao_ref_alias_set (&lhs_ref),
3361 : 79019 : ao_ref_base_alias_set (&lhs_ref), val);
3362 : : }
3363 : 35861 : else if (maxsize.is_constant (&maxsizei)
3364 : 35861 : && offset.is_constant (&offseti)
3365 : 35861 : && offset2.is_constant (&offset2i)
3366 : 35861 : && size2.is_constant (&size2i)
3367 : 35861 : && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3368 : : {
3369 : 35861 : pd_data pd;
3370 : 35861 : pd.rhs = SSA_VAL (def_rhs);
3371 : 35861 : pd.rhs_off = 0;
3372 : 35861 : pd.offset = offset2i;
3373 : 35861 : pd.size = size2i;
3374 : 35861 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3375 : : ao_ref_base_alias_set (&lhs_ref),
3376 : : offseti, maxsizei);
3377 : : }
3378 : : }
3379 : : }
3380 : :
3381 : : /* 4b) Assignment done via one of the vectorizer internal store
3382 : : functions where we may be able to access pieces from or we can
3383 : : combine to a larger entity. */
3384 : 18344934 : else if (known_eq (ref->size, maxsize)
3385 : 17841485 : && is_gimple_reg_type (vr->type)
3386 : 17515676 : && !reverse_storage_order_for_component_p (vr->operands)
3387 : 17512951 : && !contains_storage_order_barrier_p (vr->operands)
3388 : 17512951 : && is_gimple_call (def_stmt)
3389 : 14299952 : && gimple_call_internal_p (def_stmt)
3390 : 18396703 : && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3391 : : {
3392 : 46 : gcall *call = as_a <gcall *> (def_stmt);
3393 : 46 : internal_fn fn = gimple_call_internal_fn (call);
3394 : :
3395 : 46 : tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3396 : 46 : switch (fn)
3397 : : {
3398 : 46 : case IFN_MASK_STORE:
3399 : 46 : mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3400 : 46 : mask = vn_valueize (mask);
3401 : 46 : if (TREE_CODE (mask) != VECTOR_CST)
3402 : 38 : return (void *)-1;
3403 : : break;
3404 : 0 : case IFN_LEN_STORE:
3405 : 0 : {
3406 : 0 : int len_index = internal_fn_len_index (fn);
3407 : 0 : len = gimple_call_arg (call, len_index);
3408 : 0 : bias = gimple_call_arg (call, len_index + 1);
3409 : 0 : if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3410 : : return (void *) -1;
3411 : : break;
3412 : : }
3413 : : default:
3414 : : return (void *)-1;
3415 : : }
3416 : 14 : tree def_rhs = gimple_call_arg (call,
3417 : 14 : internal_fn_stored_value_index (fn));
3418 : 14 : def_rhs = vn_valueize (def_rhs);
3419 : 14 : if (TREE_CODE (def_rhs) != VECTOR_CST)
3420 : : return (void *)-1;
3421 : :
3422 : 14 : ao_ref_init_from_ptr_and_size (&lhs_ref,
3423 : : vn_valueize (gimple_call_arg (call, 0)),
3424 : 14 : TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3425 : 14 : tree base2;
3426 : 14 : poly_int64 offset2, size2, maxsize2;
3427 : 14 : HOST_WIDE_INT offset2i, size2i, offseti;
3428 : 14 : base2 = ao_ref_base (&lhs_ref);
3429 : 14 : offset2 = lhs_ref.offset;
3430 : 14 : size2 = lhs_ref.size;
3431 : 14 : maxsize2 = lhs_ref.max_size;
3432 : 14 : if (known_size_p (maxsize2)
3433 : 14 : && known_eq (maxsize2, size2)
3434 : 14 : && adjust_offsets_for_equal_base_address (base, &offset,
3435 : : base2, &offset2)
3436 : 6 : && maxsize.is_constant (&maxsizei)
3437 : 6 : && offset.is_constant (&offseti)
3438 : 6 : && offset2.is_constant (&offset2i)
3439 : 14 : && size2.is_constant (&size2i))
3440 : : {
3441 : 6 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3442 : : /* Poor-mans disambiguation. */
3443 : : return NULL;
3444 : 6 : else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3445 : : {
3446 : 6 : pd_data pd;
3447 : 6 : pd.rhs = def_rhs;
3448 : 6 : tree aa = gimple_call_arg (call, 1);
3449 : 6 : alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3450 : 6 : tree vectype = TREE_TYPE (def_rhs);
3451 : 6 : unsigned HOST_WIDE_INT elsz
3452 : 6 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3453 : 6 : if (mask)
3454 : : {
3455 : : HOST_WIDE_INT start = 0, length = 0;
3456 : : unsigned mask_idx = 0;
3457 : 48 : do
3458 : : {
3459 : 48 : if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3460 : : {
3461 : 24 : if (length != 0)
3462 : : {
3463 : 18 : pd.rhs_off = start;
3464 : 18 : pd.offset = offset2i + start;
3465 : 18 : pd.size = length;
3466 : 18 : if (ranges_known_overlap_p
3467 : 18 : (offset, maxsize, pd.offset, pd.size))
3468 : : {
3469 : 0 : void *res = data->push_partial_def
3470 : 0 : (pd, set, set, offseti, maxsizei);
3471 : 0 : if (res != NULL)
3472 : 6 : return res;
3473 : : }
3474 : : }
3475 : 24 : start = (mask_idx + 1) * elsz;
3476 : 24 : length = 0;
3477 : : }
3478 : : else
3479 : 24 : length += elsz;
3480 : 48 : mask_idx++;
3481 : : }
3482 : 48 : while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3483 : 6 : if (length != 0)
3484 : : {
3485 : 6 : pd.rhs_off = start;
3486 : 6 : pd.offset = offset2i + start;
3487 : 6 : pd.size = length;
3488 : 6 : if (ranges_known_overlap_p (offset, maxsize,
3489 : : pd.offset, pd.size))
3490 : 2 : return data->push_partial_def (pd, set, set,
3491 : 2 : offseti, maxsizei);
3492 : : }
3493 : : }
3494 : 0 : else if (fn == IFN_LEN_STORE)
3495 : : {
3496 : 0 : pd.offset = offset2i;
3497 : 0 : pd.size = (tree_to_uhwi (len)
3498 : 0 : + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3499 : 0 : if (BYTES_BIG_ENDIAN)
3500 : : pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3501 : : else
3502 : 0 : pd.rhs_off = 0;
3503 : 0 : if (ranges_known_overlap_p (offset, maxsize,
3504 : : pd.offset, pd.size))
3505 : 0 : return data->push_partial_def (pd, set, set,
3506 : 0 : offseti, maxsizei);
3507 : : }
3508 : : else
3509 : 0 : gcc_unreachable ();
3510 : 4 : return NULL;
3511 : : }
3512 : : }
3513 : : }
3514 : :
3515 : : /* 5) For aggregate copies translate the reference through them if
3516 : : the copy kills ref. */
3517 : 18344888 : else if (data->vn_walk_kind == VN_WALKREWRITE
3518 : 14374697 : && gimple_assign_single_p (def_stmt)
3519 : 20609533 : && (DECL_P (gimple_assign_rhs1 (def_stmt))
3520 : 1751691 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3521 : 1499569 : || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3522 : : {
3523 : 2071908 : tree base2;
3524 : 2071908 : int i, j, k;
3525 : 2071908 : auto_vec<vn_reference_op_s> rhs;
3526 : 2071908 : vn_reference_op_t vro;
3527 : 2071908 : ao_ref r;
3528 : :
3529 : 2071908 : gcc_assert (lhs_ref_ok);
3530 : :
3531 : : /* See if the assignment kills REF. */
3532 : 2071908 : base2 = ao_ref_base (&lhs_ref);
3533 : 2071908 : if (!lhs_ref.max_size_known_p ()
3534 : 2071728 : || (base != base2
3535 : 98123 : && (TREE_CODE (base) != MEM_REF
3536 : 83969 : || TREE_CODE (base2) != MEM_REF
3537 : 69846 : || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3538 : 32343 : || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3539 : 32343 : TREE_OPERAND (base2, 1))))
3540 : 4076399 : || !stmt_kills_ref_p (def_stmt, ref))
3541 : 291279 : return (void *)-1;
3542 : :
3543 : : /* Find the common base of ref and the lhs. lhs_ops already
3544 : : contains valueized operands for the lhs. */
3545 : 1780629 : i = vr->operands.length () - 1;
3546 : 1780629 : j = lhs_ops.length () - 1;
3547 : 1780629 : while (j >= 0 && i >= 0
3548 : 4456876 : && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3549 : : {
3550 : 2676247 : i--;
3551 : 2676247 : j--;
3552 : : }
3553 : :
3554 : : /* ??? The innermost op should always be a MEM_REF and we already
3555 : : checked that the assignment to the lhs kills vr. Thus for
3556 : : aggregate copies using char[] types the vn_reference_op_eq
3557 : : may fail when comparing types for compatibility. But we really
3558 : : don't care here - further lookups with the rewritten operands
3559 : : will simply fail if we messed up types too badly. */
3560 : 1780629 : poly_int64 extra_off = 0;
3561 : 1780629 : if (j == 0 && i >= 0
3562 : 895084 : && lhs_ops[0].opcode == MEM_REF
3563 : 2673981 : && maybe_ne (lhs_ops[0].off, -1))
3564 : : {
3565 : 893352 : if (known_eq (lhs_ops[0].off, vr->operands[i].off))
3566 : 271024 : i--, j--;
3567 : 622328 : else if (vr->operands[i].opcode == MEM_REF
3568 : 622328 : && maybe_ne (vr->operands[i].off, -1))
3569 : : {
3570 : 620899 : extra_off = vr->operands[i].off - lhs_ops[0].off;
3571 : 620899 : i--, j--;
3572 : : }
3573 : : }
3574 : :
3575 : : /* i now points to the first additional op.
3576 : : ??? LHS may not be completely contained in VR, one or more
3577 : : VIEW_CONVERT_EXPRs could be in its way. We could at least
3578 : : try handling outermost VIEW_CONVERT_EXPRs. */
3579 : 1780629 : if (j != -1)
3580 : : return (void *)-1;
3581 : :
3582 : : /* Punt if the additional ops contain a storage order barrier. */
3583 : 2874494 : for (k = i; k >= 0; k--)
3584 : : {
3585 : 1120451 : vro = &vr->operands[k];
3586 : 1120451 : if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3587 : : return (void *)-1;
3588 : : }
3589 : :
3590 : : /* Now re-write REF to be based on the rhs of the assignment. */
3591 : 1754043 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
3592 : 1754043 : copy_reference_ops_from_ref (rhs1, &rhs);
3593 : :
3594 : : /* Apply an extra offset to the inner MEM_REF of the RHS. */
3595 : 1754043 : bool force_no_tbaa = false;
3596 : 1754043 : if (maybe_ne (extra_off, 0))
3597 : : {
3598 : 620899 : if (rhs.length () < 2)
3599 : : return (void *)-1;
3600 : 620899 : int ix = rhs.length () - 2;
3601 : 620899 : if (rhs[ix].opcode != MEM_REF
3602 : 620899 : || known_eq (rhs[ix].off, -1))
3603 : : return (void *)-1;
3604 : 620895 : rhs[ix].off += extra_off;
3605 : 620895 : rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3606 : 620895 : build_int_cst (TREE_TYPE (rhs[ix].op0),
3607 : : extra_off));
3608 : : /* When we have offsetted the RHS, reading only parts of it,
3609 : : we can no longer use the original TBAA type, force alias-set
3610 : : zero. */
3611 : 620895 : force_no_tbaa = true;
3612 : : }
3613 : :
3614 : : /* Save the operands since we need to use the original ones for
3615 : : the hash entry we use. */
3616 : 1754039 : if (!data->saved_operands.exists ())
3617 : 1672554 : data->saved_operands = vr->operands.copy ();
3618 : :
3619 : : /* We need to pre-pend vr->operands[0..i] to rhs. */
3620 : 1754039 : vec<vn_reference_op_s> old = vr->operands;
3621 : 5262117 : if (i + 1 + rhs.length () > vr->operands.length ())
3622 : 1114868 : vr->operands.safe_grow (i + 1 + rhs.length (), true);
3623 : : else
3624 : 639171 : vr->operands.truncate (i + 1 + rhs.length ());
3625 : 6400345 : FOR_EACH_VEC_ELT (rhs, j, vro)
3626 : 4646306 : vr->operands[i + 1 + j] = *vro;
3627 : 1754039 : valueize_refs (&vr->operands);
3628 : 3508078 : if (old == shared_lookup_references)
3629 : 1754039 : shared_lookup_references = vr->operands;
3630 : 1754039 : vr->hashcode = vn_reference_compute_hash (vr);
3631 : :
3632 : : /* Try folding the new reference to a constant. */
3633 : 1754039 : tree val = fully_constant_vn_reference_p (vr);
3634 : 1754039 : if (val)
3635 : : {
3636 : 20489 : if (data->partial_defs.is_empty ())
3637 : 20484 : return data->finish (ao_ref_alias_set (&lhs_ref),
3638 : 20484 : ao_ref_base_alias_set (&lhs_ref), val);
3639 : : /* This is the only interesting case for partial-def handling
3640 : : coming from targets that like to gimplify init-ctors as
3641 : : aggregate copies from constant data like aarch64 for
3642 : : PR83518. */
3643 : 5 : if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3644 : : {
3645 : 5 : pd_data pd;
3646 : 5 : pd.rhs = val;
3647 : 5 : pd.rhs_off = 0;
3648 : 5 : pd.offset = 0;
3649 : 5 : pd.size = maxsizei;
3650 : 5 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3651 : : ao_ref_base_alias_set (&lhs_ref),
3652 : : 0, maxsizei);
3653 : : }
3654 : : }
3655 : :
3656 : : /* Continuing with partial defs isn't easily possible here, we
3657 : : have to find a full def from further lookups from here. Probably
3658 : : not worth the special-casing everywhere. */
3659 : 2058477 : if (!data->partial_defs.is_empty ())
3660 : : return (void *)-1;
3661 : :
3662 : : /* Adjust *ref from the new operands. */
3663 : 1726492 : ao_ref rhs1_ref;
3664 : 1726492 : ao_ref_init (&rhs1_ref, rhs1);
3665 : 2835571 : if (!ao_ref_init_from_vn_reference (&r,
3666 : : force_no_tbaa ? 0
3667 : 1109079 : : ao_ref_alias_set (&rhs1_ref),
3668 : : force_no_tbaa ? 0
3669 : 1109079 : : ao_ref_base_alias_set (&rhs1_ref),
3670 : : vr->type, vr->operands))
3671 : : return (void *)-1;
3672 : : /* This can happen with bitfields. */
3673 : 1726492 : if (maybe_ne (ref->size, r.size))
3674 : : {
3675 : : /* If the access lacks some subsetting simply apply that by
3676 : : shortening it. That in the end can only be successful
3677 : : if we can pun the lookup result which in turn requires
3678 : : exact offsets. */
3679 : 0 : if (known_eq (r.size, r.max_size)
3680 : 0 : && known_lt (ref->size, r.size))
3681 : 0 : r.size = r.max_size = ref->size;
3682 : : else
3683 : : return (void *)-1;
3684 : : }
3685 : 1726492 : *ref = r;
3686 : 1726492 : vr->offset = r.offset;
3687 : 1726492 : vr->max_size = r.max_size;
3688 : :
3689 : : /* Do not update last seen VUSE after translating. */
3690 : 1726492 : data->last_vuse_ptr = NULL;
3691 : : /* Invalidate the original access path since it now contains
3692 : : the wrong base. */
3693 : 1726492 : data->orig_ref.ref = NULL_TREE;
3694 : : /* Use the alias-set of this LHS for recording an eventual result. */
3695 : 1726492 : if (data->first_set == -2)
3696 : : {
3697 : 1646044 : data->first_set = ao_ref_alias_set (&lhs_ref);
3698 : 1646044 : data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3699 : : }
3700 : :
3701 : : /* Keep looking for the adjusted *REF / VR pair. */
3702 : 1726492 : return NULL;
3703 : 2071908 : }
3704 : :
3705 : : /* 6) For memcpy copies translate the reference through them if the copy
3706 : : kills ref. But we cannot (easily) do this translation if the memcpy is
3707 : : a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3708 : : can modify the storage order of objects (see storage_order_barrier_p). */
3709 : 16272980 : else if (data->vn_walk_kind == VN_WALKREWRITE
3710 : 12302789 : && is_gimple_reg_type (vr->type)
3711 : : /* ??? Handle BCOPY as well. */
3712 : 12295337 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3713 : 12218404 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3714 : 12218028 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3715 : 12216972 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3716 : 12216729 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3717 : 12189779 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3718 : 105863 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3719 : 97468 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3720 : 105827 : && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3721 : 70067 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3722 : 105812 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), ©_size)
3723 : 57184 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3724 : 57184 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3725 : : ©_size)))
3726 : : /* Handling this is more complicated, give up for now. */
3727 : 16323253 : && data->partial_defs.is_empty ())
3728 : : {
3729 : 49969 : tree lhs, rhs;
3730 : 49969 : ao_ref r;
3731 : 49969 : poly_int64 rhs_offset, lhs_offset;
3732 : 49969 : vn_reference_op_s op;
3733 : 49969 : poly_uint64 mem_offset;
3734 : 49969 : poly_int64 at, byte_maxsize;
3735 : :
3736 : : /* Only handle non-variable, addressable refs. */
3737 : 49969 : if (maybe_ne (ref->size, maxsize)
3738 : 49507 : || !multiple_p (offset, BITS_PER_UNIT, &at)
3739 : 49969 : || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3740 : 462 : return (void *)-1;
3741 : :
3742 : : /* Extract a pointer base and an offset for the destination. */
3743 : 49507 : lhs = gimple_call_arg (def_stmt, 0);
3744 : 49507 : lhs_offset = 0;
3745 : 49507 : if (TREE_CODE (lhs) == SSA_NAME)
3746 : : {
3747 : 42103 : lhs = vn_valueize (lhs);
3748 : 42103 : if (TREE_CODE (lhs) == SSA_NAME)
3749 : : {
3750 : 41818 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3751 : 41818 : if (gimple_assign_single_p (def_stmt)
3752 : 41818 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3753 : 2178 : lhs = gimple_assign_rhs1 (def_stmt);
3754 : : }
3755 : : }
3756 : 49507 : if (TREE_CODE (lhs) == ADDR_EXPR)
3757 : : {
3758 : 14027 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3759 : 13742 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3760 : : return (void *)-1;
3761 : 9775 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3762 : : &lhs_offset);
3763 : 9775 : if (!tem)
3764 : : return (void *)-1;
3765 : 9281 : if (TREE_CODE (tem) == MEM_REF
3766 : 9281 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3767 : : {
3768 : 1684 : lhs = TREE_OPERAND (tem, 0);
3769 : 1684 : if (TREE_CODE (lhs) == SSA_NAME)
3770 : 1684 : lhs = vn_valueize (lhs);
3771 : 1684 : lhs_offset += mem_offset;
3772 : : }
3773 : 7597 : else if (DECL_P (tem))
3774 : 7597 : lhs = build_fold_addr_expr (tem);
3775 : : else
3776 : : return (void *)-1;
3777 : : }
3778 : 48921 : if (TREE_CODE (lhs) != SSA_NAME
3779 : 7598 : && TREE_CODE (lhs) != ADDR_EXPR)
3780 : : return (void *)-1;
3781 : :
3782 : : /* Extract a pointer base and an offset for the source. */
3783 : 48921 : rhs = gimple_call_arg (def_stmt, 1);
3784 : 48921 : rhs_offset = 0;
3785 : 48921 : if (TREE_CODE (rhs) == SSA_NAME)
3786 : 16956 : rhs = vn_valueize (rhs);
3787 : 48921 : if (TREE_CODE (rhs) == ADDR_EXPR)
3788 : : {
3789 : 44621 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3790 : 33303 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3791 : : return (void *)-1;
3792 : 32860 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3793 : : &rhs_offset);
3794 : 32860 : if (!tem)
3795 : : return (void *)-1;
3796 : 32860 : if (TREE_CODE (tem) == MEM_REF
3797 : 32860 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3798 : : {
3799 : 0 : rhs = TREE_OPERAND (tem, 0);
3800 : 0 : rhs_offset += mem_offset;
3801 : : }
3802 : 32860 : else if (DECL_P (tem)
3803 : 27867 : || TREE_CODE (tem) == STRING_CST)
3804 : 32860 : rhs = build_fold_addr_expr (tem);
3805 : : else
3806 : : return (void *)-1;
3807 : : }
3808 : 48921 : if (TREE_CODE (rhs) == SSA_NAME)
3809 : 16061 : rhs = SSA_VAL (rhs);
3810 : 32860 : else if (TREE_CODE (rhs) != ADDR_EXPR)
3811 : : return (void *)-1;
3812 : :
3813 : : /* The bases of the destination and the references have to agree. */
3814 : 48921 : if (TREE_CODE (base) == MEM_REF)
3815 : : {
3816 : 13546 : if (TREE_OPERAND (base, 0) != lhs
3817 : 13546 : || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
3818 : 10402 : return (void *) -1;
3819 : 9603 : at += mem_offset;
3820 : : }
3821 : 35375 : else if (!DECL_P (base)
3822 : 34546 : || TREE_CODE (lhs) != ADDR_EXPR
3823 : 41835 : || TREE_OPERAND (lhs, 0) != base)
3824 : : return (void *)-1;
3825 : :
3826 : : /* If the access is completely outside of the memcpy destination
3827 : : area there is no aliasing. */
3828 : 9603 : if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
3829 : : return NULL;
3830 : : /* And the access has to be contained within the memcpy destination. */
3831 : 9566 : if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
3832 : : return (void *)-1;
3833 : :
3834 : : /* Save the operands since we need to use the original ones for
3835 : : the hash entry we use. */
3836 : 9315 : if (!data->saved_operands.exists ())
3837 : 8973 : data->saved_operands = vr->operands.copy ();
3838 : :
3839 : : /* Make room for 2 operands in the new reference. */
3840 : 9315 : if (vr->operands.length () < 2)
3841 : : {
3842 : 0 : vec<vn_reference_op_s> old = vr->operands;
3843 : 0 : vr->operands.safe_grow_cleared (2, true);
3844 : 0 : if (old == shared_lookup_references)
3845 : 0 : shared_lookup_references = vr->operands;
3846 : : }
3847 : : else
3848 : 9315 : vr->operands.truncate (2);
3849 : :
3850 : : /* The looked-through reference is a simple MEM_REF. */
3851 : 9315 : memset (&op, 0, sizeof (op));
3852 : 9315 : op.type = vr->type;
3853 : 9315 : op.opcode = MEM_REF;
3854 : 9315 : op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3855 : 9315 : op.off = at - lhs_offset + rhs_offset;
3856 : 9315 : vr->operands[0] = op;
3857 : 9315 : op.type = TREE_TYPE (rhs);
3858 : 9315 : op.opcode = TREE_CODE (rhs);
3859 : 9315 : op.op0 = rhs;
3860 : 9315 : op.off = -1;
3861 : 9315 : vr->operands[1] = op;
3862 : 9315 : vr->hashcode = vn_reference_compute_hash (vr);
3863 : :
3864 : : /* Try folding the new reference to a constant. */
3865 : 9315 : tree val = fully_constant_vn_reference_p (vr);
3866 : 9315 : if (val)
3867 : 1864 : return data->finish (0, 0, val);
3868 : :
3869 : : /* Adjust *ref from the new operands. */
3870 : 7451 : if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
3871 : : return (void *)-1;
3872 : : /* This can happen with bitfields. */
3873 : 7451 : if (maybe_ne (ref->size, r.size))
3874 : : return (void *)-1;
3875 : 7451 : *ref = r;
3876 : 7451 : vr->offset = r.offset;
3877 : 7451 : vr->max_size = r.max_size;
3878 : :
3879 : : /* Do not update last seen VUSE after translating. */
3880 : 7451 : data->last_vuse_ptr = NULL;
3881 : : /* Invalidate the original access path since it now contains
3882 : : the wrong base. */
3883 : 7451 : data->orig_ref.ref = NULL_TREE;
3884 : : /* Use the alias-set of this stmt for recording an eventual result. */
3885 : 7451 : if (data->first_set == -2)
3886 : : {
3887 : 7143 : data->first_set = 0;
3888 : 7143 : data->first_base_set = 0;
3889 : : }
3890 : :
3891 : : /* Keep looking for the adjusted *REF / VR pair. */
3892 : 7451 : return NULL;
3893 : : }
3894 : :
3895 : : /* Bail out and stop walking. */
3896 : : return (void *)-1;
3897 : : }
3898 : :
3899 : : /* Return a reference op vector from OP that can be used for
3900 : : vn_reference_lookup_pieces. The caller is responsible for releasing
3901 : : the vector. */
3902 : :
3903 : : vec<vn_reference_op_s>
3904 : 5216424 : vn_reference_operands_for_lookup (tree op)
3905 : : {
3906 : 5216424 : bool valueized;
3907 : 5216424 : return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
3908 : : }
3909 : :
3910 : : /* Lookup a reference operation by it's parts, in the current hash table.
3911 : : Returns the resulting value number if it exists in the hash table,
3912 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
3913 : : vn_reference_t stored in the hashtable if something is found. */
3914 : :
3915 : : tree
3916 : 8155771 : vn_reference_lookup_pieces (tree vuse, alias_set_type set,
3917 : : alias_set_type base_set, tree type,
3918 : : vec<vn_reference_op_s> operands,
3919 : : vn_reference_t *vnresult, vn_lookup_kind kind)
3920 : : {
3921 : 8155771 : struct vn_reference_s vr1;
3922 : 8155771 : vn_reference_t tmp;
3923 : 8155771 : tree cst;
3924 : :
3925 : 8155771 : if (!vnresult)
3926 : 0 : vnresult = &tmp;
3927 : 8155771 : *vnresult = NULL;
3928 : :
3929 : 8155771 : vr1.vuse = vuse_ssa_val (vuse);
3930 : 8155771 : shared_lookup_references.truncate (0);
3931 : 16311542 : shared_lookup_references.safe_grow (operands.length (), true);
3932 : 8155771 : memcpy (shared_lookup_references.address (),
3933 : 8155771 : operands.address (),
3934 : : sizeof (vn_reference_op_s)
3935 : 8155771 : * operands.length ());
3936 : 8155771 : bool valueized_p;
3937 : 8155771 : valueize_refs_1 (&shared_lookup_references, &valueized_p);
3938 : 8155771 : vr1.operands = shared_lookup_references;
3939 : 8155771 : vr1.type = type;
3940 : 8155771 : vr1.set = set;
3941 : 8155771 : vr1.base_set = base_set;
3942 : : /* We can pretend there's no extra info fed in since the ao_refs offset
3943 : : and max_size are computed only from the VN reference ops. */
3944 : 8155771 : vr1.offset = 0;
3945 : 8155771 : vr1.max_size = -1;
3946 : 8155771 : vr1.hashcode = vn_reference_compute_hash (&vr1);
3947 : 8155771 : if ((cst = fully_constant_vn_reference_p (&vr1)))
3948 : : return cst;
3949 : :
3950 : 8136961 : vn_reference_lookup_1 (&vr1, vnresult);
3951 : 8136961 : if (!*vnresult
3952 : 3121723 : && kind != VN_NOWALK
3953 : 3121723 : && vr1.vuse)
3954 : : {
3955 : 3099027 : ao_ref r;
3956 : 3099027 : unsigned limit = param_sccvn_max_alias_queries_per_access;
3957 : 3099027 : vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
3958 : 3099027 : false);
3959 : 3099027 : vec<vn_reference_op_s> ops_for_ref;
3960 : 3099027 : if (!valueized_p)
3961 : 3011507 : ops_for_ref = vr1.operands;
3962 : : else
3963 : : {
3964 : : /* For ao_ref_from_mem we have to ensure only available SSA names
3965 : : end up in base and the only convenient way to make this work
3966 : : for PRE is to re-valueize with that in mind. */
3967 : 175040 : ops_for_ref.create (operands.length ());
3968 : 175040 : ops_for_ref.quick_grow (operands.length ());
3969 : 87520 : memcpy (ops_for_ref.address (),
3970 : 87520 : operands.address (),
3971 : : sizeof (vn_reference_op_s)
3972 : 87520 : * operands.length ());
3973 : 87520 : valueize_refs_1 (&ops_for_ref, &valueized_p, true);
3974 : : }
3975 : 3099027 : if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
3976 : : ops_for_ref))
3977 : 3029627 : *vnresult
3978 : 3029627 : = ((vn_reference_t)
3979 : 3029627 : walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
3980 : : vn_reference_lookup_3, vuse_valueize,
3981 : : limit, &data));
3982 : 6198054 : if (ops_for_ref != shared_lookup_references)
3983 : 87520 : ops_for_ref.release ();
3984 : 6198054 : gcc_checking_assert (vr1.operands == shared_lookup_references);
3985 : 3099027 : if (*vnresult
3986 : 442328 : && data.same_val
3987 : 3099027 : && (!(*vnresult)->result
3988 : 0 : || !operand_equal_p ((*vnresult)->result, data.same_val)))
3989 : : {
3990 : 0 : *vnresult = NULL;
3991 : 0 : return NULL_TREE;
3992 : : }
3993 : 3099027 : }
3994 : :
3995 : 8136961 : if (*vnresult)
3996 : 5457566 : return (*vnresult)->result;
3997 : :
3998 : : return NULL_TREE;
3999 : : }
4000 : :
4001 : : /* When OPERANDS is an ADDR_EXPR that can be possibly expressed as a
4002 : : POINTER_PLUS_EXPR return true and fill in its operands in OPS. */
4003 : :
4004 : : bool
4005 : 2192452 : vn_pp_nary_for_addr (const vec<vn_reference_op_s>& operands, tree ops[2])
4006 : : {
4007 : 4384904 : gcc_assert (operands[0].opcode == ADDR_EXPR
4008 : : && operands.last ().opcode == SSA_NAME);
4009 : : poly_int64 off = 0;
4010 : : vn_reference_op_t vro;
4011 : : unsigned i;
4012 : 7119982 : for (i = 1; operands.iterate (i, &vro); ++i)
4013 : : {
4014 : 7119982 : if (vro->opcode == SSA_NAME)
4015 : : break;
4016 : 4962250 : else if (known_eq (vro->off, -1))
4017 : : break;
4018 : 4927530 : off += vro->off;
4019 : : }
4020 : 2192452 : if (i == operands.length () - 1
4021 : 2157732 : && maybe_ne (off, 0)
4022 : : /* Make sure we the offset we accumulated in a 64bit int
4023 : : fits the address computation carried out in target
4024 : : offset precision. */
4025 : 3650393 : && (off.coeffs[0]
4026 : 1457941 : == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4027 : : {
4028 : 1457332 : gcc_assert (operands[i-1].opcode == MEM_REF);
4029 : 1457332 : ops[0] = operands[i].op0;
4030 : 1457332 : ops[1] = wide_int_to_tree (sizetype, off);
4031 : 1457332 : return true;
4032 : : }
4033 : : return false;
4034 : : }
4035 : :
4036 : : /* Lookup OP in the current hash table, and return the resulting value
4037 : : number if it exists in the hash table. Return NULL_TREE if it does
4038 : : not exist in the hash table or if the result field of the structure
4039 : : was NULL.. VNRESULT will be filled in with the vn_reference_t
4040 : : stored in the hashtable if one exists. When TBAA_P is false assume
4041 : : we are looking up a store and treat it as having alias-set zero.
4042 : : *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
4043 : : MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
4044 : : load is bitwise anded with MASK and so we are only interested in a subset
4045 : : of the bits and can ignore if the other bits are uninitialized or
4046 : : not initialized with constants. When doing redundant store removal
4047 : : the caller has to set REDUNDANT_STORE_REMOVAL_P. */
4048 : :
4049 : : tree
4050 : 99881647 : vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
4051 : : vn_reference_t *vnresult, bool tbaa_p,
4052 : : tree *last_vuse_ptr, tree mask,
4053 : : bool redundant_store_removal_p)
4054 : : {
4055 : 99881647 : vec<vn_reference_op_s> operands;
4056 : 99881647 : struct vn_reference_s vr1;
4057 : 99881647 : bool valueized_anything;
4058 : :
4059 : 99881647 : if (vnresult)
4060 : 99510807 : *vnresult = NULL;
4061 : :
4062 : 99881647 : vr1.vuse = vuse_ssa_val (vuse);
4063 : 199763294 : vr1.operands = operands
4064 : 99881647 : = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
4065 : :
4066 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
4067 : : this before the pass folding __builtin_object_size had a chance to run. */
4068 : 99881647 : if ((cfun->curr_properties & PROP_objsz)
4069 : 72556080 : && operands[0].opcode == ADDR_EXPR
4070 : 100980941 : && operands.last ().opcode == SSA_NAME)
4071 : : {
4072 : 1065699 : tree ops[2];
4073 : 1065699 : if (vn_pp_nary_for_addr (operands, ops))
4074 : : {
4075 : 712482 : tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4076 : 712482 : TREE_TYPE (op), ops, NULL);
4077 : 712482 : if (res)
4078 : 712482 : return res;
4079 : 712482 : return NULL_TREE;
4080 : : }
4081 : : }
4082 : :
4083 : 99169165 : vr1.type = TREE_TYPE (op);
4084 : 99169165 : ao_ref op_ref;
4085 : 99169165 : ao_ref_init (&op_ref, op);
4086 : 99169165 : vr1.set = ao_ref_alias_set (&op_ref);
4087 : 99169165 : vr1.base_set = ao_ref_base_alias_set (&op_ref);
4088 : 99169165 : vr1.offset = 0;
4089 : 99169165 : vr1.max_size = -1;
4090 : 99169165 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4091 : 99169165 : if (mask == NULL_TREE)
4092 : 98798325 : if (tree cst = fully_constant_vn_reference_p (&vr1))
4093 : : return cst;
4094 : :
4095 : 99156229 : if (kind != VN_NOWALK && vr1.vuse)
4096 : : {
4097 : 57320540 : vn_reference_t wvnresult;
4098 : 57320540 : ao_ref r;
4099 : 57320540 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4100 : 57320540 : auto_vec<vn_reference_op_s> ops_for_ref;
4101 : 57320540 : if (valueized_anything)
4102 : : {
4103 : 4367216 : copy_reference_ops_from_ref (op, &ops_for_ref);
4104 : 4367216 : bool tem;
4105 : 4367216 : valueize_refs_1 (&ops_for_ref, &tem, true);
4106 : : }
4107 : : /* Make sure to use a valueized reference if we valueized anything.
4108 : : Otherwise preserve the full reference for advanced TBAA. */
4109 : 57320540 : if (!valueized_anything
4110 : 57320540 : || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4111 : : vr1.type, ops_for_ref))
4112 : : {
4113 : 52953324 : ao_ref_init (&r, op);
4114 : : /* Record the extra info we're getting from the full ref. */
4115 : 52953324 : ao_ref_base (&r);
4116 : 52953324 : vr1.offset = r.offset;
4117 : 52953324 : vr1.max_size = r.max_size;
4118 : : }
4119 : 57320540 : vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4120 : : last_vuse_ptr, kind, tbaa_p, mask,
4121 : 110273864 : redundant_store_removal_p);
4122 : :
4123 : 57320540 : wvnresult
4124 : : = ((vn_reference_t)
4125 : 57320540 : walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4126 : : vn_reference_lookup_3, vuse_valueize, limit,
4127 : : &data));
4128 : 114641080 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4129 : 57320540 : if (wvnresult)
4130 : : {
4131 : 8345279 : gcc_assert (mask == NULL_TREE);
4132 : 8345279 : if (data.same_val
4133 : 8345279 : && (!wvnresult->result
4134 : 55128 : || !operand_equal_p (wvnresult->result, data.same_val)))
4135 : 45473 : return NULL_TREE;
4136 : 8299806 : if (vnresult)
4137 : 8299806 : *vnresult = wvnresult;
4138 : 8299806 : return wvnresult->result;
4139 : : }
4140 : 48975261 : else if (mask)
4141 : 370840 : return data.masked_result;
4142 : :
4143 : : return NULL_TREE;
4144 : 57320540 : }
4145 : :
4146 : 41835689 : if (last_vuse_ptr)
4147 : 1406839 : *last_vuse_ptr = vr1.vuse;
4148 : 41835689 : if (mask)
4149 : : return NULL_TREE;
4150 : 41835689 : return vn_reference_lookup_1 (&vr1, vnresult);
4151 : : }
4152 : :
4153 : : /* Lookup CALL in the current hash table and return the entry in
4154 : : *VNRESULT if found. Populates *VR for the hashtable lookup. */
4155 : :
4156 : : void
4157 : 9148560 : vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4158 : : vn_reference_t vr)
4159 : : {
4160 : 9148560 : if (vnresult)
4161 : 9148560 : *vnresult = NULL;
4162 : :
4163 : 9148560 : tree vuse = gimple_vuse (call);
4164 : :
4165 : 9148560 : vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4166 : 9148560 : vr->operands = valueize_shared_reference_ops_from_call (call);
4167 : 9148560 : tree lhs = gimple_call_lhs (call);
4168 : : /* For non-SSA return values the referece ops contain the LHS. */
4169 : 5043826 : vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4170 : 13705128 : ? TREE_TYPE (lhs) : NULL_TREE);
4171 : 9148560 : vr->punned = false;
4172 : 9148560 : vr->set = 0;
4173 : 9148560 : vr->base_set = 0;
4174 : 9148560 : vr->offset = 0;
4175 : 9148560 : vr->max_size = -1;
4176 : 9148560 : vr->hashcode = vn_reference_compute_hash (vr);
4177 : 9148560 : vn_reference_lookup_1 (vr, vnresult);
4178 : 9148560 : }
4179 : :
4180 : : /* Insert OP into the current hash table with a value number of RESULT. */
4181 : :
4182 : : static void
4183 : 74341517 : vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4184 : : {
4185 : 74341517 : vn_reference_s **slot;
4186 : 74341517 : vn_reference_t vr1;
4187 : 74341517 : bool tem;
4188 : :
4189 : 74341517 : vec<vn_reference_op_s> operands
4190 : 74341517 : = valueize_shared_reference_ops_from_ref (op, &tem);
4191 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4192 : : before the pass folding __builtin_object_size had a chance to run. */
4193 : 74341517 : if ((cfun->curr_properties & PROP_objsz)
4194 : 55919338 : && operands[0].opcode == ADDR_EXPR
4195 : 75232097 : && operands.last ().opcode == SSA_NAME)
4196 : : {
4197 : 859571 : tree ops[2];
4198 : 859571 : if (vn_pp_nary_for_addr (operands, ops))
4199 : : {
4200 : 561373 : vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4201 : 561373 : TREE_TYPE (op), ops, result,
4202 : 561373 : VN_INFO (result)->value_id);
4203 : 561373 : return;
4204 : : }
4205 : : }
4206 : :
4207 : 73780144 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4208 : 73780144 : if (TREE_CODE (result) == SSA_NAME)
4209 : 51059310 : vr1->value_id = VN_INFO (result)->value_id;
4210 : : else
4211 : 22720834 : vr1->value_id = get_or_alloc_constant_value_id (result);
4212 : 73780144 : vr1->vuse = vuse_ssa_val (vuse);
4213 : 73780144 : vr1->operands = operands.copy ();
4214 : 73780144 : vr1->type = TREE_TYPE (op);
4215 : 73780144 : vr1->punned = false;
4216 : 73780144 : ao_ref op_ref;
4217 : 73780144 : ao_ref_init (&op_ref, op);
4218 : 73780144 : vr1->set = ao_ref_alias_set (&op_ref);
4219 : 73780144 : vr1->base_set = ao_ref_base_alias_set (&op_ref);
4220 : : /* Specifically use an unknown extent here, we're not doing any lookup
4221 : : and assume the caller didn't either (or it went VARYING). */
4222 : 73780144 : vr1->offset = 0;
4223 : 73780144 : vr1->max_size = -1;
4224 : 73780144 : vr1->hashcode = vn_reference_compute_hash (vr1);
4225 : 73780144 : vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4226 : 73780144 : vr1->result_vdef = vdef;
4227 : :
4228 : 73780144 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4229 : : INSERT);
4230 : :
4231 : : /* Because IL walking on reference lookup can end up visiting
4232 : : a def that is only to be visited later in iteration order
4233 : : when we are about to make an irreducible region reducible
4234 : : the def can be effectively processed and its ref being inserted
4235 : : by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4236 : : but save a lookup if we deal with already inserted refs here. */
4237 : 73780144 : if (*slot)
4238 : : {
4239 : : /* We cannot assert that we have the same value either because
4240 : : when disentangling an irreducible region we may end up visiting
4241 : : a use before the corresponding def. That's a missed optimization
4242 : : only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4243 : 0 : if (dump_file && (dump_flags & TDF_DETAILS)
4244 : 0 : && !operand_equal_p ((*slot)->result, vr1->result, 0))
4245 : : {
4246 : 0 : fprintf (dump_file, "Keeping old value ");
4247 : 0 : print_generic_expr (dump_file, (*slot)->result);
4248 : 0 : fprintf (dump_file, " because of collision\n");
4249 : : }
4250 : 0 : free_reference (vr1);
4251 : 0 : obstack_free (&vn_tables_obstack, vr1);
4252 : 0 : return;
4253 : : }
4254 : :
4255 : 73780144 : *slot = vr1;
4256 : 73780144 : vr1->next = last_inserted_ref;
4257 : 73780144 : last_inserted_ref = vr1;
4258 : : }
4259 : :
4260 : : /* Insert a reference by it's pieces into the current hash table with
4261 : : a value number of RESULT. Return the resulting reference
4262 : : structure we created. */
4263 : :
4264 : : vn_reference_t
4265 : 3743964 : vn_reference_insert_pieces (tree vuse, alias_set_type set,
4266 : : alias_set_type base_set,
4267 : : poly_int64 offset, poly_int64 max_size, tree type,
4268 : : vec<vn_reference_op_s> operands,
4269 : : tree result, unsigned int value_id)
4270 : :
4271 : : {
4272 : 3743964 : vn_reference_s **slot;
4273 : 3743964 : vn_reference_t vr1;
4274 : :
4275 : 3743964 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4276 : 3743964 : vr1->value_id = value_id;
4277 : 3743964 : vr1->vuse = vuse_ssa_val (vuse);
4278 : 3743964 : vr1->operands = operands;
4279 : 3743964 : valueize_refs (&vr1->operands);
4280 : 3743964 : vr1->type = type;
4281 : 3743964 : vr1->punned = false;
4282 : 3743964 : vr1->set = set;
4283 : 3743964 : vr1->base_set = base_set;
4284 : 3743964 : vr1->offset = offset;
4285 : 3743964 : vr1->max_size = max_size;
4286 : 3743964 : vr1->hashcode = vn_reference_compute_hash (vr1);
4287 : 3743964 : if (result && TREE_CODE (result) == SSA_NAME)
4288 : 271524 : result = SSA_VAL (result);
4289 : 3743964 : vr1->result = result;
4290 : 3743964 : vr1->result_vdef = NULL_TREE;
4291 : :
4292 : 3743964 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4293 : : INSERT);
4294 : :
4295 : : /* At this point we should have all the things inserted that we have
4296 : : seen before, and we should never try inserting something that
4297 : : already exists. */
4298 : 3743964 : gcc_assert (!*slot);
4299 : :
4300 : 3743964 : *slot = vr1;
4301 : 3743964 : vr1->next = last_inserted_ref;
4302 : 3743964 : last_inserted_ref = vr1;
4303 : 3743964 : return vr1;
4304 : : }
4305 : :
4306 : : /* Compute and return the hash value for nary operation VBO1. */
4307 : :
4308 : : hashval_t
4309 : 300485607 : vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4310 : : {
4311 : 300485607 : inchash::hash hstate;
4312 : 300485607 : unsigned i;
4313 : :
4314 : 300485607 : if (((vno1->length == 2
4315 : 253309741 : && commutative_tree_code (vno1->opcode))
4316 : 137635995 : || (vno1->length == 3
4317 : 1346191 : && commutative_ternary_tree_code (vno1->opcode)))
4318 : 463336523 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4319 : 3043164 : std::swap (vno1->op[0], vno1->op[1]);
4320 : 297442443 : else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4321 : 297442443 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4322 : : {
4323 : 470010 : std::swap (vno1->op[0], vno1->op[1]);
4324 : 470010 : vno1->opcode = swap_tree_comparison (vno1->opcode);
4325 : : }
4326 : :
4327 : 300485607 : hstate.add_int (vno1->opcode);
4328 : 857598858 : for (i = 0; i < vno1->length; ++i)
4329 : 557113251 : inchash::add_expr (vno1->op[i], hstate);
4330 : :
4331 : 300485607 : return hstate.end ();
4332 : : }
4333 : :
4334 : : /* Compare nary operations VNO1 and VNO2 and return true if they are
4335 : : equivalent. */
4336 : :
4337 : : bool
4338 : 953461618 : vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4339 : : {
4340 : 953461618 : unsigned i;
4341 : :
4342 : 953461618 : if (vno1->hashcode != vno2->hashcode)
4343 : : return false;
4344 : :
4345 : 49839023 : if (vno1->length != vno2->length)
4346 : : return false;
4347 : :
4348 : 49839023 : if (vno1->opcode != vno2->opcode
4349 : 49839023 : || !types_compatible_p (vno1->type, vno2->type))
4350 : 1237586 : return false;
4351 : :
4352 : 140387354 : for (i = 0; i < vno1->length; ++i)
4353 : 91879625 : if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4354 : : return false;
4355 : :
4356 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
4357 : : of op1. Need to check to make sure they are the same. */
4358 : 48507729 : if (vno1->opcode == BIT_INSERT_EXPR
4359 : 506 : && TREE_CODE (vno1->op[1]) == INTEGER_CST
4360 : 48507814 : && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4361 : 85 : != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4362 : : return false;
4363 : :
4364 : : return true;
4365 : : }
4366 : :
4367 : : /* Initialize VNO from the pieces provided. */
4368 : :
4369 : : static void
4370 : 186316647 : init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4371 : : enum tree_code code, tree type, tree *ops)
4372 : : {
4373 : 186316647 : vno->opcode = code;
4374 : 186316647 : vno->length = length;
4375 : 186316647 : vno->type = type;
4376 : 4725084 : memcpy (&vno->op[0], ops, sizeof (tree) * length);
4377 : 0 : }
4378 : :
4379 : : /* Return the number of operands for a vn_nary ops structure from STMT. */
4380 : :
4381 : : unsigned int
4382 : 108123508 : vn_nary_length_from_stmt (gimple *stmt)
4383 : : {
4384 : 108123508 : switch (gimple_assign_rhs_code (stmt))
4385 : : {
4386 : : case REALPART_EXPR:
4387 : : case IMAGPART_EXPR:
4388 : : case VIEW_CONVERT_EXPR:
4389 : : return 1;
4390 : :
4391 : 527090 : case BIT_FIELD_REF:
4392 : 527090 : return 3;
4393 : :
4394 : 489979 : case CONSTRUCTOR:
4395 : 489979 : return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4396 : :
4397 : 103878261 : default:
4398 : 103878261 : return gimple_num_ops (stmt) - 1;
4399 : : }
4400 : : }
4401 : :
4402 : : /* Initialize VNO from STMT. */
4403 : :
4404 : : void
4405 : 108123508 : init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4406 : : {
4407 : 108123508 : unsigned i;
4408 : :
4409 : 108123508 : vno->opcode = gimple_assign_rhs_code (stmt);
4410 : 108123508 : vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4411 : 108123508 : switch (vno->opcode)
4412 : : {
4413 : 3228178 : case REALPART_EXPR:
4414 : 3228178 : case IMAGPART_EXPR:
4415 : 3228178 : case VIEW_CONVERT_EXPR:
4416 : 3228178 : vno->length = 1;
4417 : 3228178 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4418 : 3228178 : break;
4419 : :
4420 : 527090 : case BIT_FIELD_REF:
4421 : 527090 : vno->length = 3;
4422 : 527090 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4423 : 527090 : vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4424 : 527090 : vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4425 : 527090 : break;
4426 : :
4427 : 489979 : case CONSTRUCTOR:
4428 : 489979 : vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4429 : 1871983 : for (i = 0; i < vno->length; ++i)
4430 : 1382004 : vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4431 : : break;
4432 : :
4433 : 103878261 : default:
4434 : 103878261 : gcc_checking_assert (!gimple_assign_single_p (stmt));
4435 : 103878261 : vno->length = gimple_num_ops (stmt) - 1;
4436 : 284374620 : for (i = 0; i < vno->length; ++i)
4437 : 180496359 : vno->op[i] = gimple_op (stmt, i + 1);
4438 : : }
4439 : 108123508 : }
4440 : :
4441 : : /* Compute the hashcode for VNO and look for it in the hash table;
4442 : : return the resulting value number if it exists in the hash table.
4443 : : Return NULL_TREE if it does not exist in the hash table or if the
4444 : : result field of the operation is NULL. VNRESULT will contain the
4445 : : vn_nary_op_t from the hashtable if it exists. */
4446 : :
4447 : : static tree
4448 : 130185570 : vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4449 : : {
4450 : 130185570 : vn_nary_op_s **slot;
4451 : :
4452 : 130185570 : if (vnresult)
4453 : 123201418 : *vnresult = NULL;
4454 : :
4455 : 361928726 : for (unsigned i = 0; i < vno->length; ++i)
4456 : 231743156 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4457 : 164028999 : vno->op[i] = SSA_VAL (vno->op[i]);
4458 : :
4459 : 130185570 : vno->hashcode = vn_nary_op_compute_hash (vno);
4460 : 130185570 : slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4461 : 130185570 : if (!slot)
4462 : : return NULL_TREE;
4463 : 17497797 : if (vnresult)
4464 : 17053492 : *vnresult = *slot;
4465 : 17497797 : return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4466 : : }
4467 : :
4468 : : /* Lookup a n-ary operation by its pieces and return the resulting value
4469 : : number if it exists in the hash table. Return NULL_TREE if it does
4470 : : not exist in the hash table or if the result field of the operation
4471 : : is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4472 : : if it exists. */
4473 : :
4474 : : tree
4475 : 74233819 : vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4476 : : tree type, tree *ops, vn_nary_op_t *vnresult)
4477 : : {
4478 : 74233819 : vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4479 : : sizeof_vn_nary_op (length));
4480 : 74233819 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4481 : 74233819 : return vn_nary_op_lookup_1 (vno1, vnresult);
4482 : : }
4483 : :
4484 : : /* Lookup the rhs of STMT in the current hash table, and return the resulting
4485 : : value number if it exists in the hash table. Return NULL_TREE if
4486 : : it does not exist in the hash table. VNRESULT will contain the
4487 : : vn_nary_op_t from the hashtable if it exists. */
4488 : :
4489 : : tree
4490 : 55951751 : vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4491 : : {
4492 : 55951751 : vn_nary_op_t vno1
4493 : 55951751 : = XALLOCAVAR (struct vn_nary_op_s,
4494 : : sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4495 : 55951751 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4496 : 55951751 : return vn_nary_op_lookup_1 (vno1, vnresult);
4497 : : }
4498 : :
4499 : : /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4500 : :
4501 : : vn_nary_op_t
4502 : 169303915 : alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4503 : : {
4504 : 169303915 : return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4505 : : }
4506 : :
4507 : : /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4508 : : obstack. */
4509 : :
4510 : : static vn_nary_op_t
4511 : 151907702 : alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4512 : : {
4513 : 0 : vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4514 : :
4515 : 151907702 : vno1->value_id = value_id;
4516 : 151907702 : vno1->length = length;
4517 : 151907702 : vno1->predicated_values = 0;
4518 : 151907702 : vno1->u.result = result;
4519 : :
4520 : 151907702 : return vno1;
4521 : : }
4522 : :
4523 : : /* Insert VNO into TABLE. */
4524 : :
4525 : : static vn_nary_op_t
4526 : 156803082 : vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4527 : : {
4528 : 156803082 : vn_nary_op_s **slot;
4529 : :
4530 : 156803082 : gcc_assert (! vno->predicated_values
4531 : : || (! vno->u.values->next
4532 : : && vno->u.values->n == 1));
4533 : :
4534 : 458731514 : for (unsigned i = 0; i < vno->length; ++i)
4535 : 301928432 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4536 : 196258336 : vno->op[i] = SSA_VAL (vno->op[i]);
4537 : :
4538 : 156803082 : vno->hashcode = vn_nary_op_compute_hash (vno);
4539 : 156803082 : slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4540 : 156803082 : vno->unwind_to = *slot;
4541 : 156803082 : if (*slot)
4542 : : {
4543 : : /* Prefer non-predicated values.
4544 : : ??? Only if those are constant, otherwise, with constant predicated
4545 : : value, turn them into predicated values with entry-block validity
4546 : : (??? but we always find the first valid result currently). */
4547 : 30013810 : if ((*slot)->predicated_values
4548 : 29222317 : && ! vno->predicated_values)
4549 : : {
4550 : : /* ??? We cannot remove *slot from the unwind stack list.
4551 : : For the moment we deal with this by skipping not found
4552 : : entries but this isn't ideal ... */
4553 : 81872 : *slot = vno;
4554 : : /* ??? Maintain a stack of states we can unwind in
4555 : : vn_nary_op_s? But how far do we unwind? In reality
4556 : : we need to push change records somewhere... Or not
4557 : : unwind vn_nary_op_s and linking them but instead
4558 : : unwind the results "list", linking that, which also
4559 : : doesn't move on hashtable resize. */
4560 : : /* We can also have a ->unwind_to recording *slot there.
4561 : : That way we can make u.values a fixed size array with
4562 : : recording the number of entries but of course we then
4563 : : have always N copies for each unwind_to-state. Or we
4564 : : make sure to only ever append and each unwinding will
4565 : : pop off one entry (but how to deal with predicated
4566 : : replaced with non-predicated here?) */
4567 : 81872 : vno->next = last_inserted_nary;
4568 : 81872 : last_inserted_nary = vno;
4569 : 81872 : return vno;
4570 : : }
4571 : 29931938 : else if (vno->predicated_values
4572 : 29927896 : && ! (*slot)->predicated_values)
4573 : : return *slot;
4574 : 29144487 : else if (vno->predicated_values
4575 : 29140445 : && (*slot)->predicated_values)
4576 : : {
4577 : : /* ??? Factor this all into a insert_single_predicated_value
4578 : : routine. */
4579 : 29140445 : gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4580 : 29140445 : basic_block vno_bb
4581 : 29140445 : = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4582 : 29140445 : vn_pval *nval = vno->u.values;
4583 : 29140445 : vn_pval **next = &vno->u.values;
4584 : 29140445 : bool found = false;
4585 : 60000310 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4586 : : {
4587 : 31096604 : if (expressions_equal_p (val->result, nval->result))
4588 : : {
4589 : 13270530 : found = true;
4590 : 13270530 : for (unsigned i = 0; i < val->n; ++i)
4591 : : {
4592 : 9248576 : basic_block val_bb
4593 : 9248576 : = BASIC_BLOCK_FOR_FN (cfun,
4594 : : val->valid_dominated_by_p[i]);
4595 : 9248576 : if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4596 : : /* Value registered with more generic predicate. */
4597 : 236739 : return *slot;
4598 : 9011837 : else if (flag_checking)
4599 : : /* Shouldn't happen, we insert in RPO order. */
4600 : 9011837 : gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4601 : : val_bb, vno_bb));
4602 : : }
4603 : : /* Append value. */
4604 : 4021954 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4605 : : sizeof (vn_pval)
4606 : : + val->n * sizeof (int));
4607 : 4021954 : (*next)->next = NULL;
4608 : 4021954 : (*next)->result = val->result;
4609 : 4021954 : (*next)->n = val->n + 1;
4610 : 4021954 : memcpy ((*next)->valid_dominated_by_p,
4611 : 4021954 : val->valid_dominated_by_p,
4612 : 4021954 : val->n * sizeof (int));
4613 : 4021954 : (*next)->valid_dominated_by_p[val->n] = vno_bb->index;
4614 : 4021954 : next = &(*next)->next;
4615 : 4021954 : if (dump_file && (dump_flags & TDF_DETAILS))
4616 : 4 : fprintf (dump_file, "Appending predicate to value.\n");
4617 : 4021954 : continue;
4618 : 4021954 : }
4619 : : /* Copy other predicated values. */
4620 : 26837911 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4621 : : sizeof (vn_pval)
4622 : : + (val->n-1) * sizeof (int));
4623 : 26837911 : memcpy (*next, val, sizeof (vn_pval) + (val->n-1) * sizeof (int));
4624 : 26837911 : (*next)->next = NULL;
4625 : 26837911 : next = &(*next)->next;
4626 : : }
4627 : 28903706 : if (!found)
4628 : 24881752 : *next = nval;
4629 : :
4630 : 28903706 : *slot = vno;
4631 : 28903706 : vno->next = last_inserted_nary;
4632 : 28903706 : last_inserted_nary = vno;
4633 : 28903706 : return vno;
4634 : : }
4635 : :
4636 : : /* While we do not want to insert things twice it's awkward to
4637 : : avoid it in the case where visit_nary_op pattern-matches stuff
4638 : : and ends up simplifying the replacement to itself. We then
4639 : : get two inserts, one from visit_nary_op and one from
4640 : : vn_nary_build_or_lookup.
4641 : : So allow inserts with the same value number. */
4642 : 4042 : if ((*slot)->u.result == vno->u.result)
4643 : : return *slot;
4644 : : }
4645 : :
4646 : : /* ??? There's also optimistic vs. previous commited state merging
4647 : : that is problematic for the case of unwinding. */
4648 : :
4649 : : /* ??? We should return NULL if we do not use 'vno' and have the
4650 : : caller release it. */
4651 : 126789272 : gcc_assert (!*slot);
4652 : :
4653 : 126789272 : *slot = vno;
4654 : 126789272 : vno->next = last_inserted_nary;
4655 : 126789272 : last_inserted_nary = vno;
4656 : 126789272 : return vno;
4657 : : }
4658 : :
4659 : : /* Insert a n-ary operation into the current hash table using it's
4660 : : pieces. Return the vn_nary_op_t structure we created and put in
4661 : : the hashtable. */
4662 : :
4663 : : vn_nary_op_t
4664 : 561373 : vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4665 : : tree type, tree *ops,
4666 : : tree result, unsigned int value_id)
4667 : : {
4668 : 561373 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4669 : 561373 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4670 : 561373 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4671 : : }
4672 : :
4673 : : /* Return whether we can track a predicate valid when PRED_E is executed. */
4674 : :
4675 : : static bool
4676 : 150334570 : can_track_predicate_on_edge (edge pred_e)
4677 : : {
4678 : : /* ??? As we are currently recording the destination basic-block index in
4679 : : vn_pval.valid_dominated_by_p and using dominance for the
4680 : : validity check we cannot track predicates on all edges. */
4681 : 150334570 : if (single_pred_p (pred_e->dest))
4682 : : return true;
4683 : : /* Never record for backedges. */
4684 : 11662176 : if (pred_e->flags & EDGE_DFS_BACK)
4685 : : return false;
4686 : : /* When there's more than one predecessor we cannot track
4687 : : predicate validity based on the destination block. The
4688 : : exception is when all other incoming edges sources are
4689 : : dominated by the destination block. */
4690 : 11010083 : edge_iterator ei;
4691 : 11010083 : edge e;
4692 : 19153162 : FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4693 : 17328007 : if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4694 : : return false;
4695 : : return true;
4696 : : }
4697 : :
4698 : : static vn_nary_op_t
4699 : 106796371 : vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4700 : : tree type, tree *ops,
4701 : : tree result, unsigned int value_id,
4702 : : edge pred_e)
4703 : : {
4704 : 106796371 : gcc_assert (can_track_predicate_on_edge (pred_e));
4705 : :
4706 : 68489 : if (dump_file && (dump_flags & TDF_DETAILS)
4707 : : /* ??? Fix dumping, but currently we only get comparisons. */
4708 : 106860966 : && TREE_CODE_CLASS (code) == tcc_comparison)
4709 : : {
4710 : 64595 : fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4711 : 64595 : pred_e->dest->index);
4712 : 64595 : print_generic_expr (dump_file, ops[0], TDF_SLIM);
4713 : 64595 : fprintf (dump_file, " %s ", get_tree_code_name (code));
4714 : 64595 : print_generic_expr (dump_file, ops[1], TDF_SLIM);
4715 : 96590 : fprintf (dump_file, " == %s\n",
4716 : 64595 : integer_zerop (result) ? "false" : "true");
4717 : : }
4718 : 106796371 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4719 : 106796371 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4720 : 106796371 : vno1->predicated_values = 1;
4721 : 106796371 : vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4722 : : sizeof (vn_pval));
4723 : 106796371 : vno1->u.values->next = NULL;
4724 : 106796371 : vno1->u.values->result = result;
4725 : 106796371 : vno1->u.values->n = 1;
4726 : 106796371 : vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4727 : 106796371 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4728 : : }
4729 : :
4730 : : static bool
4731 : : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4732 : :
4733 : : static tree
4734 : 1611775 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
4735 : : edge e = NULL)
4736 : : {
4737 : 1611775 : if (! vno->predicated_values)
4738 : 0 : return vno->u.result;
4739 : 3360126 : for (vn_pval *val = vno->u.values; val; val = val->next)
4740 : 5620869 : for (unsigned i = 0; i < val->n; ++i)
4741 : : {
4742 : 3872518 : basic_block cand
4743 : 3872518 : = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
4744 : : /* Do not handle backedge executability optimistically since
4745 : : when figuring out whether to iterate we do not consider
4746 : : changed predication.
4747 : : When asking for predicated values on an edge avoid looking
4748 : : at edge executability for edges forward in our iteration
4749 : : as well. */
4750 : 3872518 : if (e && (e->flags & EDGE_DFS_BACK))
4751 : : {
4752 : 21179 : if (dominated_by_p (CDI_DOMINATORS, bb, cand))
4753 : 7733 : return val->result;
4754 : : }
4755 : 3851339 : else if (dominated_by_p_w_unex (bb, cand, false))
4756 : 478937 : return val->result;
4757 : : }
4758 : : return NULL_TREE;
4759 : : }
4760 : :
4761 : : static tree
4762 : 202939 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
4763 : : {
4764 : 0 : return vn_nary_op_get_predicated_value (vno, e->src, e);
4765 : : }
4766 : :
4767 : : /* Insert the rhs of STMT into the current hash table with a value number of
4768 : : RESULT. */
4769 : :
4770 : : static vn_nary_op_t
4771 : 44549958 : vn_nary_op_insert_stmt (gimple *stmt, tree result)
4772 : : {
4773 : 44549958 : vn_nary_op_t vno1
4774 : 44549958 : = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
4775 : 44549958 : result, VN_INFO (result)->value_id);
4776 : 44549958 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4777 : 44549958 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4778 : : }
4779 : :
4780 : : /* Compute a hashcode for PHI operation VP1 and return it. */
4781 : :
4782 : : static inline hashval_t
4783 : 51072958 : vn_phi_compute_hash (vn_phi_t vp1)
4784 : : {
4785 : 51072958 : inchash::hash hstate;
4786 : 51072958 : tree phi1op;
4787 : 51072958 : tree type;
4788 : 51072958 : edge e;
4789 : 51072958 : edge_iterator ei;
4790 : :
4791 : 102145916 : hstate.add_int (EDGE_COUNT (vp1->block->preds));
4792 : 51072958 : switch (EDGE_COUNT (vp1->block->preds))
4793 : : {
4794 : : case 1:
4795 : : break;
4796 : 44597219 : case 2:
4797 : : /* When this is a PHI node subject to CSE for different blocks
4798 : : avoid hashing the block index. */
4799 : 44597219 : if (vp1->cclhs)
4800 : : break;
4801 : : /* Fallthru. */
4802 : 33171243 : default:
4803 : 33171243 : hstate.add_int (vp1->block->index);
4804 : : }
4805 : :
4806 : : /* If all PHI arguments are constants we need to distinguish
4807 : : the PHI node via its type. */
4808 : 51072958 : type = vp1->type;
4809 : 51072958 : hstate.merge_hash (vn_hash_type (type));
4810 : :
4811 : 174667511 : FOR_EACH_EDGE (e, ei, vp1->block->preds)
4812 : : {
4813 : : /* Don't hash backedge values they need to be handled as VN_TOP
4814 : : for optimistic value-numbering. */
4815 : 123594553 : if (e->flags & EDGE_DFS_BACK)
4816 : 27531231 : continue;
4817 : :
4818 : 96063322 : phi1op = vp1->phiargs[e->dest_idx];
4819 : 96063322 : if (phi1op == VN_TOP)
4820 : 242457 : continue;
4821 : 95820865 : inchash::add_expr (phi1op, hstate);
4822 : : }
4823 : :
4824 : 51072958 : return hstate.end ();
4825 : : }
4826 : :
4827 : :
4828 : : /* Return true if COND1 and COND2 represent the same condition, set
4829 : : *INVERTED_P if one needs to be inverted to make it the same as
4830 : : the other. */
4831 : :
4832 : : static bool
4833 : 4101266 : cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
4834 : : gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
4835 : : {
4836 : 4101266 : enum tree_code code1 = gimple_cond_code (cond1);
4837 : 4101266 : enum tree_code code2 = gimple_cond_code (cond2);
4838 : :
4839 : 4101266 : *inverted_p = false;
4840 : 4101266 : if (code1 == code2)
4841 : : ;
4842 : 303736 : else if (code1 == swap_tree_comparison (code2))
4843 : : std::swap (lhs2, rhs2);
4844 : 267071 : else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
4845 : 133409 : *inverted_p = true;
4846 : 133662 : else if (code1 == invert_tree_comparison
4847 : 133662 : (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
4848 : : {
4849 : 10800 : std::swap (lhs2, rhs2);
4850 : 10800 : *inverted_p = true;
4851 : : }
4852 : : else
4853 : : return false;
4854 : :
4855 : 3978404 : return ((expressions_equal_p (lhs1, lhs2)
4856 : 130483 : && expressions_equal_p (rhs1, rhs2))
4857 : 4007558 : || (commutative_tree_code (code1)
4858 : 1872013 : && expressions_equal_p (lhs1, rhs2)
4859 : 3669 : && expressions_equal_p (rhs1, lhs2)));
4860 : : }
4861 : :
4862 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4863 : :
4864 : : static int
4865 : 42331854 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
4866 : : {
4867 : 42331854 : if (vp1->hashcode != vp2->hashcode)
4868 : : return false;
4869 : :
4870 : 12917226 : if (vp1->block != vp2->block)
4871 : : {
4872 : 12325020 : if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
4873 : : return false;
4874 : :
4875 : 38186352 : switch (EDGE_COUNT (vp1->block->preds))
4876 : : {
4877 : : case 1:
4878 : : /* Single-arg PHIs are just copies. */
4879 : : break;
4880 : :
4881 : 4108340 : case 2:
4882 : 4108340 : {
4883 : : /* Make sure both PHIs are classified as CSEable. */
4884 : 4108340 : if (! vp1->cclhs || ! vp2->cclhs)
4885 : : return false;
4886 : :
4887 : : /* Rule out backedges into the PHI. */
4888 : 4108340 : gcc_checking_assert
4889 : : (vp1->block->loop_father->header != vp1->block
4890 : : && vp2->block->loop_father->header != vp2->block);
4891 : :
4892 : : /* If the PHI nodes do not have compatible types
4893 : : they are not the same. */
4894 : 4108340 : if (!types_compatible_p (vp1->type, vp2->type))
4895 : : return false;
4896 : :
4897 : : /* If the immediate dominator end in switch stmts multiple
4898 : : values may end up in the same PHI arg via intermediate
4899 : : CFG merges. */
4900 : 4101266 : basic_block idom1
4901 : 4101266 : = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4902 : 4101266 : basic_block idom2
4903 : 4101266 : = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
4904 : 4101266 : gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
4905 : : && EDGE_COUNT (idom2->succs) == 2);
4906 : :
4907 : : /* Verify the controlling stmt is the same. */
4908 : 8202532 : gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
4909 : 8202532 : gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
4910 : 4101266 : bool inverted_p;
4911 : 4101266 : if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
4912 : 4101266 : last2, vp2->cclhs, vp2->ccrhs,
4913 : : &inverted_p))
4914 : : return false;
4915 : :
4916 : : /* Get at true/false controlled edges into the PHI. */
4917 : 101513 : edge te1, te2, fe1, fe2;
4918 : 101513 : if (! extract_true_false_controlled_edges (idom1, vp1->block,
4919 : : &te1, &fe1)
4920 : 101513 : || ! extract_true_false_controlled_edges (idom2, vp2->block,
4921 : : &te2, &fe2))
4922 : 48169 : return false;
4923 : :
4924 : : /* Swap edges if the second condition is the inverted of the
4925 : : first. */
4926 : 53344 : if (inverted_p)
4927 : 1999 : std::swap (te2, fe2);
4928 : :
4929 : : /* Since we do not know which edge will be executed we have
4930 : : to be careful when matching VN_TOP. Be conservative and
4931 : : only match VN_TOP == VN_TOP for now, we could allow
4932 : : VN_TOP on the not prevailing PHI though. See for example
4933 : : PR102920. */
4934 : 53344 : if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
4935 : 53344 : vp2->phiargs[te2->dest_idx], false)
4936 : 104950 : || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
4937 : 51606 : vp2->phiargs[fe2->dest_idx], false))
4938 : 1738 : return false;
4939 : :
4940 : : return true;
4941 : : }
4942 : :
4943 : : default:
4944 : : return false;
4945 : : }
4946 : : }
4947 : :
4948 : : /* If the PHI nodes do not have compatible types
4949 : : they are not the same. */
4950 : 8808886 : if (!types_compatible_p (vp1->type, vp2->type))
4951 : : return false;
4952 : :
4953 : : /* Any phi in the same block will have it's arguments in the
4954 : : same edge order, because of how we store phi nodes. */
4955 : 8807866 : unsigned nargs = EDGE_COUNT (vp1->block->preds);
4956 : 20519509 : for (unsigned i = 0; i < nargs; ++i)
4957 : : {
4958 : 16374007 : tree phi1op = vp1->phiargs[i];
4959 : 16374007 : tree phi2op = vp2->phiargs[i];
4960 : 16374007 : if (phi1op == phi2op)
4961 : 11618026 : continue;
4962 : 4755981 : if (!expressions_equal_p (phi1op, phi2op, false))
4963 : : return false;
4964 : : }
4965 : :
4966 : : return true;
4967 : : }
4968 : :
4969 : : /* Lookup PHI in the current hash table, and return the resulting
4970 : : value number if it exists in the hash table. Return NULL_TREE if
4971 : : it does not exist in the hash table. */
4972 : :
4973 : : static tree
4974 : 27965127 : vn_phi_lookup (gimple *phi, bool backedges_varying_p)
4975 : : {
4976 : 27965127 : vn_phi_s **slot;
4977 : 27965127 : struct vn_phi_s *vp1;
4978 : 27965127 : edge e;
4979 : 27965127 : edge_iterator ei;
4980 : :
4981 : 27965127 : vp1 = XALLOCAVAR (struct vn_phi_s,
4982 : : sizeof (struct vn_phi_s)
4983 : : + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
4984 : :
4985 : : /* Canonicalize the SSA_NAME's to their value number. */
4986 : 95010755 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
4987 : : {
4988 : 67045628 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4989 : 67045628 : if (TREE_CODE (def) == SSA_NAME
4990 : 56146200 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
4991 : : {
4992 : 53666215 : if (!virtual_operand_p (def)
4993 : 53666215 : && ssa_undefined_value_p (def, false))
4994 : 134789 : def = VN_TOP;
4995 : : else
4996 : 53531426 : def = SSA_VAL (def);
4997 : : }
4998 : 67045628 : vp1->phiargs[e->dest_idx] = def;
4999 : : }
5000 : 27965127 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5001 : 27965127 : vp1->block = gimple_bb (phi);
5002 : : /* Extract values of the controlling condition. */
5003 : 27965127 : vp1->cclhs = NULL_TREE;
5004 : 27965127 : vp1->ccrhs = NULL_TREE;
5005 : 27965127 : if (EDGE_COUNT (vp1->block->preds) == 2
5006 : 27965127 : && vp1->block->loop_father->header != vp1->block)
5007 : : {
5008 : 9521189 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5009 : 9521189 : if (EDGE_COUNT (idom1->succs) == 2)
5010 : 18835452 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5011 : : {
5012 : : /* ??? We want to use SSA_VAL here. But possibly not
5013 : : allow VN_TOP. */
5014 : 9157729 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5015 : 9157729 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5016 : : }
5017 : : }
5018 : 27965127 : vp1->hashcode = vn_phi_compute_hash (vp1);
5019 : 27965127 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
5020 : 27965127 : if (!slot)
5021 : : return NULL_TREE;
5022 : 4197108 : return (*slot)->result;
5023 : : }
5024 : :
5025 : : /* Insert PHI into the current hash table with a value number of
5026 : : RESULT. */
5027 : :
5028 : : static vn_phi_t
5029 : 23107831 : vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
5030 : : {
5031 : 23107831 : vn_phi_s **slot;
5032 : 23107831 : vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
5033 : : sizeof (vn_phi_s)
5034 : : + ((gimple_phi_num_args (phi) - 1)
5035 : : * sizeof (tree)));
5036 : 23107831 : edge e;
5037 : 23107831 : edge_iterator ei;
5038 : :
5039 : : /* Canonicalize the SSA_NAME's to their value number. */
5040 : 79656756 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5041 : : {
5042 : 56548925 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5043 : 56548925 : if (TREE_CODE (def) == SSA_NAME
5044 : 46709220 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5045 : : {
5046 : 44229613 : if (!virtual_operand_p (def)
5047 : 44229613 : && ssa_undefined_value_p (def, false))
5048 : 107904 : def = VN_TOP;
5049 : : else
5050 : 44121709 : def = SSA_VAL (def);
5051 : : }
5052 : 56548925 : vp1->phiargs[e->dest_idx] = def;
5053 : : }
5054 : 23107831 : vp1->value_id = VN_INFO (result)->value_id;
5055 : 23107831 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5056 : 23107831 : vp1->block = gimple_bb (phi);
5057 : : /* Extract values of the controlling condition. */
5058 : 23107831 : vp1->cclhs = NULL_TREE;
5059 : 23107831 : vp1->ccrhs = NULL_TREE;
5060 : 23107831 : if (EDGE_COUNT (vp1->block->preds) == 2
5061 : 23107831 : && vp1->block->loop_father->header != vp1->block)
5062 : : {
5063 : 9090316 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5064 : 9090316 : if (EDGE_COUNT (idom1->succs) == 2)
5065 : 17980928 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5066 : : {
5067 : : /* ??? We want to use SSA_VAL here. But possibly not
5068 : : allow VN_TOP. */
5069 : 8743986 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5070 : 8743986 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5071 : : }
5072 : : }
5073 : 23107831 : vp1->result = result;
5074 : 23107831 : vp1->hashcode = vn_phi_compute_hash (vp1);
5075 : :
5076 : 23107831 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5077 : 23107831 : gcc_assert (!*slot);
5078 : :
5079 : 23107831 : *slot = vp1;
5080 : 23107831 : vp1->next = last_inserted_phi;
5081 : 23107831 : last_inserted_phi = vp1;
5082 : 23107831 : return vp1;
5083 : : }
5084 : :
5085 : :
5086 : : /* Return true if BB1 is dominated by BB2 taking into account edges
5087 : : that are not executable. When ALLOW_BACK is false consider not
5088 : : executable backedges as executable. */
5089 : :
5090 : : static bool
5091 : 68333568 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
5092 : : {
5093 : 68333568 : edge_iterator ei;
5094 : 68333568 : edge e;
5095 : :
5096 : 68333568 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5097 : : return true;
5098 : :
5099 : : /* Before iterating we'd like to know if there exists a
5100 : : (executable) path from bb2 to bb1 at all, if not we can
5101 : : directly return false. For now simply iterate once. */
5102 : :
5103 : : /* Iterate to the single executable bb1 predecessor. */
5104 : 21591052 : if (EDGE_COUNT (bb1->preds) > 1)
5105 : : {
5106 : 3133832 : edge prede = NULL;
5107 : 6743000 : FOR_EACH_EDGE (e, ei, bb1->preds)
5108 : 6332617 : if ((e->flags & EDGE_EXECUTABLE)
5109 : 553797 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5110 : : {
5111 : 5857281 : if (prede)
5112 : : {
5113 : : prede = NULL;
5114 : : break;
5115 : : }
5116 : : prede = e;
5117 : : }
5118 : 3133832 : if (prede)
5119 : : {
5120 : 410383 : bb1 = prede->src;
5121 : :
5122 : : /* Re-do the dominance check with changed bb1. */
5123 : 410383 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5124 : : return true;
5125 : : }
5126 : : }
5127 : :
5128 : : /* Iterate to the single executable bb2 successor. */
5129 : 21353031 : if (EDGE_COUNT (bb2->succs) > 1)
5130 : : {
5131 : 6149217 : edge succe = NULL;
5132 : 12454223 : FOR_EACH_EDGE (e, ei, bb2->succs)
5133 : 12298758 : if ((e->flags & EDGE_EXECUTABLE)
5134 : 193824 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5135 : : {
5136 : 12104984 : if (succe)
5137 : : {
5138 : : succe = NULL;
5139 : : break;
5140 : : }
5141 : : succe = e;
5142 : : }
5143 : 6149217 : if (succe)
5144 : : {
5145 : : /* Verify the reached block is only reached through succe.
5146 : : If there is only one edge we can spare us the dominator
5147 : : check and iterate directly. */
5148 : 117480 : if (EDGE_COUNT (succe->dest->preds) > 1)
5149 : : {
5150 : 77098 : FOR_EACH_EDGE (e, ei, succe->dest->preds)
5151 : 59047 : if (e != succe
5152 : 38799 : && ((e->flags & EDGE_EXECUTABLE)
5153 : 28855 : || (!allow_back && (e->flags & EDGE_DFS_BACK))))
5154 : : {
5155 : : succe = NULL;
5156 : : break;
5157 : : }
5158 : : }
5159 : 117480 : if (succe)
5160 : : {
5161 : 107527 : bb2 = succe->dest;
5162 : :
5163 : : /* Re-do the dominance check with changed bb2. */
5164 : 107527 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5165 : : return true;
5166 : : }
5167 : : }
5168 : : }
5169 : : /* Iterate to the single successor of bb2 with only a single executable
5170 : : incoming edge. */
5171 : 15203814 : else if (EDGE_COUNT (bb2->succs) == 1
5172 : 14725780 : && EDGE_COUNT (single_succ (bb2)->preds) > 1
5173 : : /* Limit the number of edges we check, we should bring in
5174 : : context from the iteration and compute the single
5175 : : executable incoming edge when visiting a block. */
5176 : 29675421 : && EDGE_COUNT (single_succ (bb2)->preds) < 8)
5177 : : {
5178 : 5544042 : edge prede = NULL;
5179 : 12434020 : FOR_EACH_EDGE (e, ei, single_succ (bb2)->preds)
5180 : 11855784 : if ((e->flags & EDGE_EXECUTABLE)
5181 : 1399681 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5182 : : {
5183 : 10460054 : if (prede)
5184 : : {
5185 : : prede = NULL;
5186 : : break;
5187 : : }
5188 : : prede = e;
5189 : : }
5190 : : /* We might actually get to a query with BB2 not visited yet when
5191 : : we're querying for a predicated value. */
5192 : 5544042 : if (prede && prede->src == bb2)
5193 : : {
5194 : 515132 : bb2 = prede->dest;
5195 : :
5196 : : /* Re-do the dominance check with changed bb2. */
5197 : 515132 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5198 : : return true;
5199 : : }
5200 : : }
5201 : :
5202 : : /* We could now iterate updating bb1 / bb2. */
5203 : : return false;
5204 : : }
5205 : :
5206 : : /* Set the value number of FROM to TO, return true if it has changed
5207 : : as a result. */
5208 : :
5209 : : static inline bool
5210 : 204610057 : set_ssa_val_to (tree from, tree to)
5211 : : {
5212 : 204610057 : vn_ssa_aux_t from_info = VN_INFO (from);
5213 : 204610057 : tree currval = from_info->valnum; // SSA_VAL (from)
5214 : 204610057 : poly_int64 toff, coff;
5215 : 204610057 : bool curr_undefined = false;
5216 : 204610057 : bool curr_invariant = false;
5217 : :
5218 : : /* The only thing we allow as value numbers are ssa_names
5219 : : and invariants. So assert that here. We don't allow VN_TOP
5220 : : as visiting a stmt should produce a value-number other than
5221 : : that.
5222 : : ??? Still VN_TOP can happen for unreachable code, so force
5223 : : it to varying in that case. Not all code is prepared to
5224 : : get VN_TOP on valueization. */
5225 : 204610057 : if (to == VN_TOP)
5226 : : {
5227 : : /* ??? When iterating and visiting PHI <undef, backedge-value>
5228 : : for the first time we rightfully get VN_TOP and we need to
5229 : : preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5230 : : With SCCVN we were simply lucky we iterated the other PHI
5231 : : cycles first and thus visited the backedge-value DEF. */
5232 : 0 : if (currval == VN_TOP)
5233 : 0 : goto set_and_exit;
5234 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5235 : 0 : fprintf (dump_file, "Forcing value number to varying on "
5236 : : "receiving VN_TOP\n");
5237 : : to = from;
5238 : : }
5239 : :
5240 : 204610057 : gcc_checking_assert (to != NULL_TREE
5241 : : && ((TREE_CODE (to) == SSA_NAME
5242 : : && (to == from || SSA_VAL (to) == to))
5243 : : || is_gimple_min_invariant (to)));
5244 : :
5245 : 204610057 : if (from != to)
5246 : : {
5247 : 32143721 : if (currval == from)
5248 : : {
5249 : 15914 : if (dump_file && (dump_flags & TDF_DETAILS))
5250 : : {
5251 : 0 : fprintf (dump_file, "Not changing value number of ");
5252 : 0 : print_generic_expr (dump_file, from);
5253 : 0 : fprintf (dump_file, " from VARYING to ");
5254 : 0 : print_generic_expr (dump_file, to);
5255 : 0 : fprintf (dump_file, "\n");
5256 : : }
5257 : 15914 : return false;
5258 : : }
5259 : 32127807 : curr_invariant = is_gimple_min_invariant (currval);
5260 : 64255614 : curr_undefined = (TREE_CODE (currval) == SSA_NAME
5261 : 3798739 : && !virtual_operand_p (currval)
5262 : 35688449 : && ssa_undefined_value_p (currval, false));
5263 : 32127807 : if (currval != VN_TOP
5264 : : && !curr_invariant
5265 : 5279837 : && !curr_undefined
5266 : 35913425 : && is_gimple_min_invariant (to))
5267 : : {
5268 : 230 : if (dump_file && (dump_flags & TDF_DETAILS))
5269 : : {
5270 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5271 : : "value number of ");
5272 : 0 : print_generic_expr (dump_file, from);
5273 : 0 : fprintf (dump_file, " from ");
5274 : 0 : print_generic_expr (dump_file, currval);
5275 : 0 : fprintf (dump_file, " (non-constant) to ");
5276 : 0 : print_generic_expr (dump_file, to);
5277 : 0 : fprintf (dump_file, " (constant)\n");
5278 : : }
5279 : : to = from;
5280 : : }
5281 : 32127577 : else if (currval != VN_TOP
5282 : 5279607 : && !curr_undefined
5283 : 5266486 : && TREE_CODE (to) == SSA_NAME
5284 : 4429328 : && !virtual_operand_p (to)
5285 : 36318808 : && ssa_undefined_value_p (to, false))
5286 : : {
5287 : 6 : if (dump_file && (dump_flags & TDF_DETAILS))
5288 : : {
5289 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5290 : : "value number of ");
5291 : 0 : print_generic_expr (dump_file, from);
5292 : 0 : fprintf (dump_file, " from ");
5293 : 0 : print_generic_expr (dump_file, currval);
5294 : 0 : fprintf (dump_file, " (non-undefined) to ");
5295 : 0 : print_generic_expr (dump_file, to);
5296 : 0 : fprintf (dump_file, " (undefined)\n");
5297 : : }
5298 : : to = from;
5299 : : }
5300 : 32127571 : else if (TREE_CODE (to) == SSA_NAME
5301 : 32127571 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5302 : : to = from;
5303 : : }
5304 : :
5305 : 172466336 : set_and_exit:
5306 : 204594143 : if (dump_file && (dump_flags & TDF_DETAILS))
5307 : : {
5308 : 359088 : fprintf (dump_file, "Setting value number of ");
5309 : 359088 : print_generic_expr (dump_file, from);
5310 : 359088 : fprintf (dump_file, " to ");
5311 : 359088 : print_generic_expr (dump_file, to);
5312 : : }
5313 : :
5314 : 204594143 : if (currval != to
5315 : 166773413 : && !operand_equal_p (currval, to, 0)
5316 : : /* Different undefined SSA names are not actually different. See
5317 : : PR82320 for a testcase were we'd otherwise not terminate iteration. */
5318 : 166704783 : && !(curr_undefined
5319 : 3613 : && TREE_CODE (to) == SSA_NAME
5320 : 874 : && !virtual_operand_p (to)
5321 : 874 : && ssa_undefined_value_p (to, false))
5322 : : /* ??? For addresses involving volatile objects or types operand_equal_p
5323 : : does not reliably detect ADDR_EXPRs as equal. We know we are only
5324 : : getting invariant gimple addresses here, so can use
5325 : : get_addr_base_and_unit_offset to do this comparison. */
5326 : 371298020 : && !(TREE_CODE (currval) == ADDR_EXPR
5327 : 467159 : && TREE_CODE (to) == ADDR_EXPR
5328 : 12 : && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5329 : 6 : == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5330 : 6 : && known_eq (coff, toff)))
5331 : : {
5332 : 166703871 : if (to != from
5333 : 27830183 : && currval != VN_TOP
5334 : 985642 : && !curr_undefined
5335 : : /* We do not want to allow lattice transitions from one value
5336 : : to another since that may lead to not terminating iteration
5337 : : (see PR95049). Since there's no convenient way to check
5338 : : for the allowed transition of VAL -> PHI (loop entry value,
5339 : : same on two PHIs, to same PHI result) we restrict the check
5340 : : to invariants. */
5341 : 985642 : && curr_invariant
5342 : 167347805 : && is_gimple_min_invariant (to))
5343 : : {
5344 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5345 : 0 : fprintf (dump_file, " forced VARYING");
5346 : : to = from;
5347 : : }
5348 : 166703871 : if (dump_file && (dump_flags & TDF_DETAILS))
5349 : 358770 : fprintf (dump_file, " (changed)\n");
5350 : 166703871 : from_info->valnum = to;
5351 : 166703871 : return true;
5352 : : }
5353 : 37890272 : if (dump_file && (dump_flags & TDF_DETAILS))
5354 : 318 : fprintf (dump_file, "\n");
5355 : : return false;
5356 : : }
5357 : :
5358 : : /* Set all definitions in STMT to value number to themselves.
5359 : : Return true if a value number changed. */
5360 : :
5361 : : static bool
5362 : 281025860 : defs_to_varying (gimple *stmt)
5363 : : {
5364 : 281025860 : bool changed = false;
5365 : 281025860 : ssa_op_iter iter;
5366 : 281025860 : def_operand_p defp;
5367 : :
5368 : 311215565 : FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5369 : : {
5370 : 30189705 : tree def = DEF_FROM_PTR (defp);
5371 : 30189705 : changed |= set_ssa_val_to (def, def);
5372 : : }
5373 : 281025860 : return changed;
5374 : : }
5375 : :
5376 : : /* Visit a copy between LHS and RHS, return true if the value number
5377 : : changed. */
5378 : :
5379 : : static bool
5380 : 7783934 : visit_copy (tree lhs, tree rhs)
5381 : : {
5382 : : /* Valueize. */
5383 : 7783934 : rhs = SSA_VAL (rhs);
5384 : :
5385 : 7783934 : return set_ssa_val_to (lhs, rhs);
5386 : : }
5387 : :
5388 : : /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5389 : : is the same. */
5390 : :
5391 : : static tree
5392 : 2342124 : valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5393 : : {
5394 : 2342124 : if (TREE_CODE (op) == SSA_NAME)
5395 : 2062522 : op = vn_valueize (op);
5396 : :
5397 : : /* Either we have the op widened available. */
5398 : 2342124 : tree ops[3] = {};
5399 : 2342124 : ops[0] = op;
5400 : 2342124 : tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5401 : : wide_type, ops, NULL);
5402 : 2342124 : if (tem)
5403 : : return tem;
5404 : :
5405 : : /* Or the op is truncated from some existing value. */
5406 : 2063821 : if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5407 : : {
5408 : 477893 : gimple *def = SSA_NAME_DEF_STMT (op);
5409 : 477893 : if (is_gimple_assign (def)
5410 : 477893 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5411 : : {
5412 : 244969 : tem = gimple_assign_rhs1 (def);
5413 : 244969 : if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5414 : : {
5415 : 135566 : if (TREE_CODE (tem) == SSA_NAME)
5416 : 135566 : tem = vn_valueize (tem);
5417 : 135566 : return tem;
5418 : : }
5419 : : }
5420 : : }
5421 : :
5422 : : /* For constants simply extend it. */
5423 : 1928255 : if (TREE_CODE (op) == INTEGER_CST)
5424 : 311695 : return wide_int_to_tree (wide_type, wi::to_widest (op));
5425 : :
5426 : : return NULL_TREE;
5427 : : }
5428 : :
5429 : : /* Visit a nary operator RHS, value number it, and return true if the
5430 : : value number of LHS has changed as a result. */
5431 : :
5432 : : static bool
5433 : 48220802 : visit_nary_op (tree lhs, gassign *stmt)
5434 : : {
5435 : 48220802 : vn_nary_op_t vnresult;
5436 : 48220802 : tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5437 : 48220802 : if (! result && vnresult)
5438 : 152172 : result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5439 : 44620793 : if (result)
5440 : 3670309 : return set_ssa_val_to (lhs, result);
5441 : :
5442 : : /* Do some special pattern matching for redundancies of operations
5443 : : in different types. */
5444 : 44550493 : enum tree_code code = gimple_assign_rhs_code (stmt);
5445 : 44550493 : tree type = TREE_TYPE (lhs);
5446 : 44550493 : tree rhs1 = gimple_assign_rhs1 (stmt);
5447 : 44550493 : switch (code)
5448 : : {
5449 : 10019934 : CASE_CONVERT:
5450 : : /* Match arithmetic done in a different type where we can easily
5451 : : substitute the result from some earlier sign-changed or widened
5452 : : operation. */
5453 : 10019934 : if (INTEGRAL_TYPE_P (type)
5454 : 8994393 : && TREE_CODE (rhs1) == SSA_NAME
5455 : : /* We only handle sign-changes, zero-extension -> & mask or
5456 : : sign-extension if we know the inner operation doesn't
5457 : : overflow. */
5458 : 18779097 : && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5459 : 5250622 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5460 : 5249437 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5461 : 8034825 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5462 : 5860328 : || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5463 : : {
5464 : 7584019 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5465 : 5472056 : if (def
5466 : 5472056 : && (gimple_assign_rhs_code (def) == PLUS_EXPR
5467 : 4263312 : || gimple_assign_rhs_code (def) == MINUS_EXPR
5468 : 4128340 : || gimple_assign_rhs_code (def) == MULT_EXPR))
5469 : : {
5470 : 1912773 : tree ops[3] = {};
5471 : : /* When requiring a sign-extension we cannot model a
5472 : : previous truncation with a single op so don't bother. */
5473 : 1912773 : bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5474 : : /* Either we have the op widened available. */
5475 : 1912773 : ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5476 : : allow_truncate);
5477 : 1912773 : if (ops[0])
5478 : 858702 : ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5479 : : allow_truncate);
5480 : 1912773 : if (ops[0] && ops[1])
5481 : : {
5482 : 296213 : ops[0] = vn_nary_op_lookup_pieces
5483 : 296213 : (2, gimple_assign_rhs_code (def), type, ops, NULL);
5484 : : /* We have wider operation available. */
5485 : 296213 : if (ops[0]
5486 : : /* If the leader is a wrapping operation we can
5487 : : insert it for code hoisting w/o introducing
5488 : : undefined overflow. If it is not it has to
5489 : : be available. See PR86554. */
5490 : 296213 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
5491 : 1880 : || (rpo_avail && vn_context_bb
5492 : 1880 : && rpo_avail->eliminate_avail (vn_context_bb,
5493 : : ops[0]))))
5494 : : {
5495 : 8949 : unsigned lhs_prec = TYPE_PRECISION (type);
5496 : 8949 : unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5497 : 8949 : if (lhs_prec == rhs_prec
5498 : 8949 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5499 : 1622 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5500 : : {
5501 : 8404 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5502 : 8404 : NOP_EXPR, type, ops[0]);
5503 : 8404 : result = vn_nary_build_or_lookup (&match_op);
5504 : 8404 : if (result)
5505 : : {
5506 : 8404 : bool changed = set_ssa_val_to (lhs, result);
5507 : 8404 : vn_nary_op_insert_stmt (stmt, result);
5508 : 8404 : return changed;
5509 : : }
5510 : : }
5511 : : else
5512 : : {
5513 : 545 : tree mask = wide_int_to_tree
5514 : 545 : (type, wi::mask (rhs_prec, false, lhs_prec));
5515 : 545 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5516 : 545 : BIT_AND_EXPR,
5517 : 545 : TREE_TYPE (lhs),
5518 : 545 : ops[0], mask);
5519 : 545 : result = vn_nary_build_or_lookup (&match_op);
5520 : 545 : if (result)
5521 : : {
5522 : 545 : bool changed = set_ssa_val_to (lhs, result);
5523 : 545 : vn_nary_op_insert_stmt (stmt, result);
5524 : 545 : return changed;
5525 : : }
5526 : : }
5527 : : }
5528 : : }
5529 : : }
5530 : : }
5531 : : break;
5532 : 1570186 : case BIT_AND_EXPR:
5533 : 1570186 : if (INTEGRAL_TYPE_P (type)
5534 : 1542887 : && TREE_CODE (rhs1) == SSA_NAME
5535 : 1542887 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5536 : 980190 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5537 : 980072 : && default_vn_walk_kind != VN_NOWALK
5538 : : && CHAR_BIT == 8
5539 : : && BITS_PER_UNIT == 8
5540 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5541 : 979864 : && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5542 : 979862 : && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5543 : 2550048 : && !integer_zerop (gimple_assign_rhs2 (stmt)))
5544 : : {
5545 : 979862 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5546 : 734946 : if (ass
5547 : 734946 : && !gimple_has_volatile_ops (ass)
5548 : 733488 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5549 : : {
5550 : 370840 : tree last_vuse = gimple_vuse (ass);
5551 : 370840 : tree op = gimple_assign_rhs1 (ass);
5552 : 1112520 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5553 : : default_vn_walk_kind,
5554 : : NULL, true, &last_vuse,
5555 : : gimple_assign_rhs2 (stmt));
5556 : 370840 : if (result
5557 : 371374 : && useless_type_conversion_p (TREE_TYPE (result),
5558 : 534 : TREE_TYPE (op)))
5559 : 534 : return set_ssa_val_to (lhs, result);
5560 : : }
5561 : : }
5562 : : break;
5563 : 368096 : case TRUNC_DIV_EXPR:
5564 : 368096 : if (TYPE_UNSIGNED (type))
5565 : : break;
5566 : : /* Fallthru. */
5567 : 5318668 : case RDIV_EXPR:
5568 : 5318668 : case MULT_EXPR:
5569 : : /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5570 : 5318668 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5571 : : {
5572 : 5317758 : tree rhs[2];
5573 : 5317758 : rhs[0] = rhs1;
5574 : 5317758 : rhs[1] = gimple_assign_rhs2 (stmt);
5575 : 15946675 : for (unsigned i = 0; i <= 1; ++i)
5576 : : {
5577 : 10634409 : unsigned j = i == 0 ? 1 : 0;
5578 : 10634409 : tree ops[2];
5579 : 10634409 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5580 : 10634409 : NEGATE_EXPR, type, rhs[i]);
5581 : 10634409 : ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5582 : 10634409 : ops[j] = rhs[j];
5583 : 10634409 : if (ops[i]
5584 : 10634409 : && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5585 : : type, ops, NULL)))
5586 : : {
5587 : 5492 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5588 : 5492 : NEGATE_EXPR, type, ops[0]);
5589 : 5492 : result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5590 : 5492 : if (result)
5591 : : {
5592 : 5492 : bool changed = set_ssa_val_to (lhs, result);
5593 : 5492 : vn_nary_op_insert_stmt (stmt, result);
5594 : 5492 : return changed;
5595 : : }
5596 : : }
5597 : : }
5598 : : }
5599 : : break;
5600 : 386371 : case LSHIFT_EXPR:
5601 : : /* For X << C, use the value number of X * (1 << C). */
5602 : 386371 : if (INTEGRAL_TYPE_P (type)
5603 : 379071 : && TYPE_OVERFLOW_WRAPS (type)
5604 : 608867 : && !TYPE_SATURATING (type))
5605 : : {
5606 : 222496 : tree rhs2 = gimple_assign_rhs2 (stmt);
5607 : 222496 : if (TREE_CODE (rhs2) == INTEGER_CST
5608 : 145718 : && tree_fits_uhwi_p (rhs2)
5609 : 368214 : && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
5610 : : {
5611 : 145718 : wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
5612 : 145718 : TYPE_PRECISION (type));
5613 : 291436 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5614 : 145718 : MULT_EXPR, type, rhs1,
5615 : 145718 : wide_int_to_tree (type, w));
5616 : 145718 : result = vn_nary_build_or_lookup (&match_op);
5617 : 145718 : if (result)
5618 : : {
5619 : 145718 : bool changed = set_ssa_val_to (lhs, result);
5620 : 145718 : if (TREE_CODE (result) == SSA_NAME)
5621 : 145717 : vn_nary_op_insert_stmt (stmt, result);
5622 : 145718 : return changed;
5623 : : }
5624 : 145718 : }
5625 : : }
5626 : : break;
5627 : : default:
5628 : : break;
5629 : : }
5630 : :
5631 : 44389800 : bool changed = set_ssa_val_to (lhs, lhs);
5632 : 44389800 : vn_nary_op_insert_stmt (stmt, lhs);
5633 : 44389800 : return changed;
5634 : : }
5635 : :
5636 : : /* Visit a call STMT storing into LHS. Return true if the value number
5637 : : of the LHS has changed as a result. */
5638 : :
5639 : : static bool
5640 : 8604167 : visit_reference_op_call (tree lhs, gcall *stmt)
5641 : : {
5642 : 8604167 : bool changed = false;
5643 : 8604167 : struct vn_reference_s vr1;
5644 : 8604167 : vn_reference_t vnresult = NULL;
5645 : 8604167 : tree vdef = gimple_vdef (stmt);
5646 : 8604167 : modref_summary *summary;
5647 : :
5648 : : /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5649 : 8604167 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
5650 : 4582271 : lhs = NULL_TREE;
5651 : :
5652 : 8604167 : vn_reference_lookup_call (stmt, &vnresult, &vr1);
5653 : :
5654 : : /* If the lookup did not succeed for pure functions try to use
5655 : : modref info to find a candidate to CSE to. */
5656 : 8604167 : const unsigned accesses_limit = 8;
5657 : 8604167 : if (!vnresult
5658 : 7970552 : && !vdef
5659 : 7970552 : && lhs
5660 : 2803451 : && gimple_vuse (stmt)
5661 : 10195199 : && (((summary = get_modref_function_summary (stmt, NULL))
5662 : 196448 : && !summary->global_memory_read
5663 : 70983 : && summary->load_accesses < accesses_limit)
5664 : 1520191 : || gimple_call_flags (stmt) & ECF_CONST))
5665 : : {
5666 : : /* First search if we can do someting useful and build a
5667 : : vector of all loads we have to check. */
5668 : 71576 : bool unknown_memory_access = false;
5669 : 71576 : auto_vec<ao_ref, accesses_limit> accesses;
5670 : 71576 : unsigned load_accesses = summary ? summary->load_accesses : 0;
5671 : 71576 : if (!unknown_memory_access)
5672 : : /* Add loads done as part of setting up the call arguments.
5673 : : That's also necessary for CONST functions which will
5674 : : not have a modref summary. */
5675 : 213813 : for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
5676 : : {
5677 : 142245 : tree arg = gimple_call_arg (stmt, i);
5678 : 142245 : if (TREE_CODE (arg) != SSA_NAME
5679 : 142245 : && !is_gimple_min_invariant (arg))
5680 : : {
5681 : 49028 : if (accesses.length () >= accesses_limit - load_accesses)
5682 : : {
5683 : : unknown_memory_access = true;
5684 : : break;
5685 : : }
5686 : 24506 : accesses.quick_grow (accesses.length () + 1);
5687 : 24506 : ao_ref_init (&accesses.last (), arg);
5688 : : }
5689 : : }
5690 : 71576 : if (summary && !unknown_memory_access)
5691 : : {
5692 : : /* Add loads as analyzed by IPA modref. */
5693 : 250913 : for (auto base_node : summary->loads->bases)
5694 : 63822 : if (unknown_memory_access)
5695 : : break;
5696 : 260333 : else for (auto ref_node : base_node->refs)
5697 : 69718 : if (unknown_memory_access)
5698 : : break;
5699 : 283017 : else for (auto access_node : ref_node->accesses)
5700 : : {
5701 : 186864 : accesses.quick_grow (accesses.length () + 1);
5702 : 93432 : ao_ref *r = &accesses.last ();
5703 : 93432 : if (!access_node.get_ao_ref (stmt, r))
5704 : : {
5705 : : /* Initialize a ref based on the argument and
5706 : : unknown offset if possible. */
5707 : 19545 : tree arg = access_node.get_call_arg (stmt);
5708 : 19545 : if (arg && TREE_CODE (arg) == SSA_NAME)
5709 : 6426 : arg = SSA_VAL (arg);
5710 : 6426 : if (arg
5711 : 19535 : && TREE_CODE (arg) == ADDR_EXPR
5712 : 13238 : && (arg = get_base_address (arg))
5713 : 19664 : && DECL_P (arg))
5714 : : {
5715 : 0 : ao_ref_init (r, arg);
5716 : 0 : r->ref = NULL_TREE;
5717 : 0 : r->base = arg;
5718 : : }
5719 : : else
5720 : : {
5721 : : unknown_memory_access = true;
5722 : : break;
5723 : : }
5724 : : }
5725 : 73887 : r->base_alias_set = base_node->base;
5726 : 73887 : r->ref_alias_set = ref_node->ref;
5727 : : }
5728 : : }
5729 : :
5730 : : /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5731 : : for the call in the hashtable. */
5732 : 71576 : unsigned limit = (unknown_memory_access
5733 : 71576 : ? 0
5734 : 52023 : : (param_sccvn_max_alias_queries_per_access
5735 : 52023 : / (accesses.length () + 1)));
5736 : 71576 : tree saved_vuse = vr1.vuse;
5737 : 71576 : hashval_t saved_hashcode = vr1.hashcode;
5738 : 316228 : while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
5739 : : {
5740 : 259561 : vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
5741 : 259561 : gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
5742 : : /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5743 : : do not bother for now. */
5744 : 259561 : if (is_a <gphi *> (def))
5745 : : break;
5746 : 489304 : vr1.vuse = vuse_ssa_val (gimple_vuse (def));
5747 : 244652 : vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
5748 : 244652 : vn_reference_lookup_1 (&vr1, &vnresult);
5749 : 244652 : limit--;
5750 : : }
5751 : :
5752 : : /* If we found a candidate to CSE to verify it is valid. */
5753 : 71576 : if (vnresult && !accesses.is_empty ())
5754 : : {
5755 : 1769 : tree vuse = vuse_ssa_val (gimple_vuse (stmt));
5756 : 6695 : while (vnresult && vuse != vr1.vuse)
5757 : : {
5758 : 3157 : gimple *def = SSA_NAME_DEF_STMT (vuse);
5759 : 16957 : for (auto &ref : accesses)
5760 : : {
5761 : : /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5762 : : analysis overhead that we might be able to cache. */
5763 : 9075 : if (stmt_may_clobber_ref_p_1 (def, &ref, true))
5764 : : {
5765 : 1589 : vnresult = NULL;
5766 : 1589 : break;
5767 : : }
5768 : : }
5769 : 6314 : vuse = vuse_ssa_val (gimple_vuse (def));
5770 : : }
5771 : : }
5772 : 71576 : vr1.vuse = saved_vuse;
5773 : 71576 : vr1.hashcode = saved_hashcode;
5774 : 71576 : }
5775 : :
5776 : 8604167 : if (vnresult)
5777 : : {
5778 : 633819 : if (vdef)
5779 : : {
5780 : 184944 : if (vnresult->result_vdef)
5781 : 184944 : changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
5782 : 0 : else if (!lhs && gimple_call_lhs (stmt))
5783 : : /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5784 : : as the call still acts as a lhs store. */
5785 : 0 : changed |= set_ssa_val_to (vdef, vdef);
5786 : : else
5787 : : /* If the call was discovered to be pure or const reflect
5788 : : that as far as possible. */
5789 : 0 : changed |= set_ssa_val_to (vdef,
5790 : : vuse_ssa_val (gimple_vuse (stmt)));
5791 : : }
5792 : :
5793 : 633819 : if (!vnresult->result && lhs)
5794 : 0 : vnresult->result = lhs;
5795 : :
5796 : 633819 : if (vnresult->result && lhs)
5797 : 96431 : changed |= set_ssa_val_to (lhs, vnresult->result);
5798 : : }
5799 : : else
5800 : : {
5801 : 7970348 : vn_reference_t vr2;
5802 : 7970348 : vn_reference_s **slot;
5803 : 7970348 : tree vdef_val = vdef;
5804 : 7970348 : if (vdef)
5805 : : {
5806 : : /* If we value numbered an indirect functions function to
5807 : : one not clobbering memory value number its VDEF to its
5808 : : VUSE. */
5809 : 4846074 : tree fn = gimple_call_fn (stmt);
5810 : 4846074 : if (fn && TREE_CODE (fn) == SSA_NAME)
5811 : : {
5812 : 128466 : fn = SSA_VAL (fn);
5813 : 128466 : if (TREE_CODE (fn) == ADDR_EXPR
5814 : 1655 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5815 : 1655 : && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
5816 : 1655 : & (ECF_CONST | ECF_PURE))
5817 : : /* If stmt has non-SSA_NAME lhs, value number the
5818 : : vdef to itself, as the call still acts as a lhs
5819 : : store. */
5820 : 129525 : && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
5821 : 1968 : vdef_val = vuse_ssa_val (gimple_vuse (stmt));
5822 : : }
5823 : 4846074 : changed |= set_ssa_val_to (vdef, vdef_val);
5824 : : }
5825 : 7970348 : if (lhs)
5826 : 3925465 : changed |= set_ssa_val_to (lhs, lhs);
5827 : 7970348 : vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
5828 : 7970348 : vr2->vuse = vr1.vuse;
5829 : : /* As we are not walking the virtual operand chain we know the
5830 : : shared_lookup_references are still original so we can re-use
5831 : : them here. */
5832 : 7970348 : vr2->operands = vr1.operands.copy ();
5833 : 7970348 : vr2->type = vr1.type;
5834 : 7970348 : vr2->punned = vr1.punned;
5835 : 7970348 : vr2->set = vr1.set;
5836 : 7970348 : vr2->offset = vr1.offset;
5837 : 7970348 : vr2->max_size = vr1.max_size;
5838 : 7970348 : vr2->base_set = vr1.base_set;
5839 : 7970348 : vr2->hashcode = vr1.hashcode;
5840 : 7970348 : vr2->result = lhs;
5841 : 7970348 : vr2->result_vdef = vdef_val;
5842 : 7970348 : vr2->value_id = 0;
5843 : 7970348 : slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
5844 : : INSERT);
5845 : 7970348 : gcc_assert (!*slot);
5846 : 7970348 : *slot = vr2;
5847 : 7970348 : vr2->next = last_inserted_ref;
5848 : 7970348 : last_inserted_ref = vr2;
5849 : : }
5850 : :
5851 : 8604167 : return changed;
5852 : : }
5853 : :
5854 : : /* Visit a load from a reference operator RHS, part of STMT, value number it,
5855 : : and return true if the value number of the LHS has changed as a result. */
5856 : :
5857 : : static bool
5858 : 34178508 : visit_reference_op_load (tree lhs, tree op, gimple *stmt)
5859 : : {
5860 : 34178508 : bool changed = false;
5861 : 34178508 : tree result;
5862 : 34178508 : vn_reference_t res;
5863 : :
5864 : 34178508 : tree vuse = gimple_vuse (stmt);
5865 : 34178508 : tree last_vuse = vuse;
5866 : 34178508 : result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
5867 : :
5868 : : /* We handle type-punning through unions by value-numbering based
5869 : : on offset and size of the access. Be prepared to handle a
5870 : : type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5871 : 34178508 : if (result
5872 : 34178508 : && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
5873 : : {
5874 : 18389 : if (CONSTANT_CLASS_P (result))
5875 : 2827 : result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5876 : : else
5877 : : {
5878 : : /* We will be setting the value number of lhs to the value number
5879 : : of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5880 : : So first simplify and lookup this expression to see if it
5881 : : is already available. */
5882 : 15562 : gimple_match_op res_op (gimple_match_cond::UNCOND,
5883 : 15562 : VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5884 : 15562 : result = vn_nary_build_or_lookup (&res_op);
5885 : 15562 : if (result
5886 : 15562 : && TREE_CODE (result) == SSA_NAME
5887 : 29457 : && VN_INFO (result)->needs_insertion)
5888 : : /* Track whether this is the canonical expression for different
5889 : : typed loads. We use that as a stopgap measure for code
5890 : : hoisting when dealing with floating point loads. */
5891 : 13345 : res->punned = true;
5892 : : }
5893 : :
5894 : : /* When building the conversion fails avoid inserting the reference
5895 : : again. */
5896 : 18389 : if (!result)
5897 : 0 : return set_ssa_val_to (lhs, lhs);
5898 : : }
5899 : :
5900 : 34160119 : if (result)
5901 : 5394722 : changed = set_ssa_val_to (lhs, result);
5902 : : else
5903 : : {
5904 : 28783786 : changed = set_ssa_val_to (lhs, lhs);
5905 : 28783786 : vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
5906 : 28783786 : if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
5907 : : {
5908 : 9292111 : if (dump_file && (dump_flags & TDF_DETAILS))
5909 : : {
5910 : 13357 : fprintf (dump_file, "Using extra use virtual operand ");
5911 : 13357 : print_generic_expr (dump_file, last_vuse);
5912 : 13357 : fprintf (dump_file, "\n");
5913 : : }
5914 : 9292111 : vn_reference_insert (op, lhs, vuse, NULL_TREE);
5915 : : }
5916 : : }
5917 : :
5918 : : return changed;
5919 : : }
5920 : :
5921 : :
5922 : : /* Visit a store to a reference operator LHS, part of STMT, value number it,
5923 : : and return true if the value number of the LHS has changed as a result. */
5924 : :
5925 : : static bool
5926 : 32852020 : visit_reference_op_store (tree lhs, tree op, gimple *stmt)
5927 : : {
5928 : 32852020 : bool changed = false;
5929 : 32852020 : vn_reference_t vnresult = NULL;
5930 : 32852020 : tree assign;
5931 : 32852020 : bool resultsame = false;
5932 : 32852020 : tree vuse = gimple_vuse (stmt);
5933 : 32852020 : tree vdef = gimple_vdef (stmt);
5934 : :
5935 : 32852020 : if (TREE_CODE (op) == SSA_NAME)
5936 : 14603349 : op = SSA_VAL (op);
5937 : :
5938 : : /* First we want to lookup using the *vuses* from the store and see
5939 : : if there the last store to this location with the same address
5940 : : had the same value.
5941 : :
5942 : : The vuses represent the memory state before the store. If the
5943 : : memory state, address, and value of the store is the same as the
5944 : : last store to this location, then this store will produce the
5945 : : same memory state as that store.
5946 : :
5947 : : In this case the vdef versions for this store are value numbered to those
5948 : : vuse versions, since they represent the same memory state after
5949 : : this store.
5950 : :
5951 : : Otherwise, the vdefs for the store are used when inserting into
5952 : : the table, since the store generates a new memory state. */
5953 : :
5954 : 32852020 : vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
5955 : 32852020 : if (vnresult
5956 : 1649808 : && vnresult->result)
5957 : : {
5958 : 1649808 : tree result = vnresult->result;
5959 : 1649808 : gcc_checking_assert (TREE_CODE (result) != SSA_NAME
5960 : : || result == SSA_VAL (result));
5961 : 1649808 : resultsame = expressions_equal_p (result, op);
5962 : 1649808 : if (resultsame)
5963 : : {
5964 : : /* If the TBAA state isn't compatible for downstream reads
5965 : : we cannot value-number the VDEFs the same. */
5966 : 52100 : ao_ref lhs_ref;
5967 : 52100 : ao_ref_init (&lhs_ref, lhs);
5968 : 52100 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
5969 : 52100 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
5970 : 52100 : if ((vnresult->set != set
5971 : 1639 : && ! alias_set_subset_of (set, vnresult->set))
5972 : 53471 : || (vnresult->base_set != base_set
5973 : 6358 : && ! alias_set_subset_of (base_set, vnresult->base_set)))
5974 : 844 : resultsame = false;
5975 : : }
5976 : : }
5977 : :
5978 : 844 : if (!resultsame)
5979 : : {
5980 : 32800764 : if (dump_file && (dump_flags & TDF_DETAILS))
5981 : : {
5982 : 18579 : fprintf (dump_file, "No store match\n");
5983 : 18579 : fprintf (dump_file, "Value numbering store ");
5984 : 18579 : print_generic_expr (dump_file, lhs);
5985 : 18579 : fprintf (dump_file, " to ");
5986 : 18579 : print_generic_expr (dump_file, op);
5987 : 18579 : fprintf (dump_file, "\n");
5988 : : }
5989 : : /* Have to set value numbers before insert, since insert is
5990 : : going to valueize the references in-place. */
5991 : 32800764 : if (vdef)
5992 : 32800764 : changed |= set_ssa_val_to (vdef, vdef);
5993 : :
5994 : : /* Do not insert structure copies into the tables. */
5995 : 32800764 : if (is_gimple_min_invariant (op)
5996 : 32800764 : || is_gimple_reg (op))
5997 : 28735328 : vn_reference_insert (lhs, op, vdef, NULL);
5998 : :
5999 : : /* Only perform the following when being called from PRE
6000 : : which embeds tail merging. */
6001 : 32800764 : if (default_vn_walk_kind == VN_WALK)
6002 : : {
6003 : 7577003 : assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
6004 : 7577003 : vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
6005 : 7577003 : if (!vnresult)
6006 : 7530292 : vn_reference_insert (assign, lhs, vuse, vdef);
6007 : : }
6008 : : }
6009 : : else
6010 : : {
6011 : : /* We had a match, so value number the vdef to have the value
6012 : : number of the vuse it came from. */
6013 : :
6014 : 51256 : if (dump_file && (dump_flags & TDF_DETAILS))
6015 : 9 : fprintf (dump_file, "Store matched earlier value, "
6016 : : "value numbering store vdefs to matching vuses.\n");
6017 : :
6018 : 51256 : changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
6019 : : }
6020 : :
6021 : 32852020 : return changed;
6022 : : }
6023 : :
6024 : : /* Visit and value number PHI, return true if the value number
6025 : : changed. When BACKEDGES_VARYING_P is true then assume all
6026 : : backedge values are varying. When INSERTED is not NULL then
6027 : : this is just a ahead query for a possible iteration, set INSERTED
6028 : : to true if we'd insert into the hashtable. */
6029 : :
6030 : : static bool
6031 : 34979310 : visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
6032 : : {
6033 : 34979310 : tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
6034 : 34979310 : bool seen_undef_visited = false;
6035 : 34979310 : tree backedge_val = NULL_TREE;
6036 : 34979310 : bool seen_non_backedge = false;
6037 : 34979310 : tree sameval_base = NULL_TREE;
6038 : 34979310 : poly_int64 soff, doff;
6039 : 34979310 : unsigned n_executable = 0;
6040 : 34979310 : edge sameval_e = NULL;
6041 : :
6042 : : /* TODO: We could check for this in initialization, and replace this
6043 : : with a gcc_assert. */
6044 : 34979310 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
6045 : 27826 : return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
6046 : :
6047 : : /* We track whether a PHI was CSEd to avoid excessive iterations
6048 : : that would be necessary only because the PHI changed arguments
6049 : : but not value. */
6050 : 34951484 : if (!inserted)
6051 : 27447310 : gimple_set_plf (phi, GF_PLF_1, false);
6052 : :
6053 : 34951484 : basic_block bb = gimple_bb (phi);
6054 : :
6055 : : /* For the equivalence handling below make sure to first process an
6056 : : edge with a non-constant. */
6057 : 34951484 : auto_vec<edge, 2> preds;
6058 : 69902968 : preds.reserve_exact (EDGE_COUNT (bb->preds));
6059 : 34951484 : bool seen_nonconstant = false;
6060 : 113897451 : for (unsigned i = 0; i < EDGE_COUNT (bb->preds); ++i)
6061 : : {
6062 : 78945967 : edge e = EDGE_PRED (bb, i);
6063 : 78945967 : preds.quick_push (e);
6064 : 78945967 : if (!seen_nonconstant)
6065 : : {
6066 : 42378544 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6067 : 42378544 : if (TREE_CODE (def) == SSA_NAME)
6068 : : {
6069 : 33159806 : seen_nonconstant = true;
6070 : 33159806 : if (i != 0)
6071 : 5612404 : std::swap (preds[0], preds[i]);
6072 : : }
6073 : : }
6074 : : }
6075 : :
6076 : : /* See if all non-TOP arguments have the same value. TOP is
6077 : : equivalent to everything, so we can ignore it. */
6078 : 146933650 : for (edge e : preds)
6079 : 69244961 : if (e->flags & EDGE_EXECUTABLE)
6080 : : {
6081 : 64263292 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6082 : :
6083 : 64263292 : if (def == PHI_RESULT (phi))
6084 : 332304 : continue;
6085 : 63951056 : ++n_executable;
6086 : 63951056 : bool visited = true;
6087 : 63951056 : if (TREE_CODE (def) == SSA_NAME)
6088 : : {
6089 : 51850749 : tree val = SSA_VAL (def, &visited);
6090 : 51850749 : if (SSA_NAME_IS_DEFAULT_DEF (def))
6091 : 2630408 : visited = true;
6092 : 51850749 : if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6093 : 49375355 : def = val;
6094 : 51850749 : if (e->flags & EDGE_DFS_BACK)
6095 : 15242264 : backedge_val = def;
6096 : : }
6097 : 63951056 : if (!(e->flags & EDGE_DFS_BACK))
6098 : 48522727 : seen_non_backedge = true;
6099 : 63951056 : if (def == VN_TOP)
6100 : : ;
6101 : : /* Ignore undefined defs for sameval but record one. */
6102 : 63951056 : else if (TREE_CODE (def) == SSA_NAME
6103 : 48439168 : && ! virtual_operand_p (def)
6104 : 88482188 : && ssa_undefined_value_p (def, false))
6105 : : {
6106 : 233656 : if (!seen_undef
6107 : : /* Avoid having not visited undefined defs if we also have
6108 : : a visited one. */
6109 : 30962 : || (!seen_undef_visited && visited))
6110 : : {
6111 : 202697 : seen_undef = def;
6112 : 202697 : seen_undef_visited = visited;
6113 : : }
6114 : : }
6115 : 63717400 : else if (sameval == VN_TOP)
6116 : : {
6117 : : sameval = def;
6118 : : sameval_e = e;
6119 : : }
6120 : 28819116 : else if (expressions_equal_p (def, sameval))
6121 : : sameval_e = NULL;
6122 : 45604559 : else if (virtual_operand_p (def))
6123 : : {
6124 : : sameval = NULL_TREE;
6125 : 27165763 : break;
6126 : : }
6127 : : else
6128 : : {
6129 : : /* We know we're arriving only with invariant addresses here,
6130 : : try harder comparing them. We can do some caching here
6131 : : which we cannot do in expressions_equal_p. */
6132 : 16840585 : if (TREE_CODE (def) == ADDR_EXPR
6133 : 426986 : && TREE_CODE (sameval) == ADDR_EXPR
6134 : 119699 : && sameval_base != (void *)-1)
6135 : : {
6136 : 119699 : if (!sameval_base)
6137 : 119697 : sameval_base = get_addr_base_and_unit_offset
6138 : 119697 : (TREE_OPERAND (sameval, 0), &soff);
6139 : 119697 : if (!sameval_base)
6140 : : sameval_base = (tree)(void *)-1;
6141 : 119704 : else if ((get_addr_base_and_unit_offset
6142 : 119699 : (TREE_OPERAND (def, 0), &doff) == sameval_base)
6143 : 119699 : && known_eq (soff, doff))
6144 : 5 : continue;
6145 : : }
6146 : : /* There's also the possibility to use equivalences. */
6147 : 32572249 : if (!FLOAT_TYPE_P (TREE_TYPE (def))
6148 : : /* But only do this if we didn't force any of sameval or
6149 : : val to VARYING because of backedge processing rules. */
6150 : 15632384 : && (TREE_CODE (sameval) != SSA_NAME
6151 : 12342355 : || SSA_VAL (sameval) == sameval)
6152 : 32472769 : && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6153 : : {
6154 : 15632181 : vn_nary_op_t vnresult;
6155 : 15632181 : tree ops[2];
6156 : 15632181 : ops[0] = def;
6157 : 15632181 : ops[1] = sameval;
6158 : : /* Canonicalize the operands order for eq below. */
6159 : 15632181 : if (tree_swap_operands_p (ops[0], ops[1]))
6160 : 9346942 : std::swap (ops[0], ops[1]);
6161 : 15632181 : tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6162 : : boolean_type_node,
6163 : : ops, &vnresult);
6164 : 15632181 : if (! val && vnresult && vnresult->predicated_values)
6165 : : {
6166 : 202939 : val = vn_nary_op_get_predicated_value (vnresult, e);
6167 : 113898 : if (val && integer_truep (val)
6168 : 223108 : && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6169 : : {
6170 : 20063 : if (dump_file && (dump_flags & TDF_DETAILS))
6171 : : {
6172 : 2 : fprintf (dump_file, "Predication says ");
6173 : 2 : print_generic_expr (dump_file, def, TDF_NONE);
6174 : 2 : fprintf (dump_file, " and ");
6175 : 2 : print_generic_expr (dump_file, sameval, TDF_NONE);
6176 : 2 : fprintf (dump_file, " are equal on edge %d -> %d\n",
6177 : 2 : e->src->index, e->dest->index);
6178 : : }
6179 : 20063 : continue;
6180 : : }
6181 : : }
6182 : : }
6183 : : sameval = NULL_TREE;
6184 : : break;
6185 : : }
6186 : : }
6187 : :
6188 : : /* If the value we want to use is flowing over the backedge and we
6189 : : should take it as VARYING but it has a non-VARYING value drop to
6190 : : VARYING.
6191 : : If we value-number a virtual operand never value-number to the
6192 : : value from the backedge as that confuses the alias-walking code.
6193 : : See gcc.dg/torture/pr87176.c. If the value is the same on a
6194 : : non-backedge everything is OK though. */
6195 : 34951484 : bool visited_p;
6196 : 34951484 : if ((backedge_val
6197 : 34951484 : && !seen_non_backedge
6198 : 1818 : && TREE_CODE (backedge_val) == SSA_NAME
6199 : 1551 : && sameval == backedge_val
6200 : 356 : && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6201 : 57 : || SSA_VAL (backedge_val) != backedge_val))
6202 : : /* Do not value-number a virtual operand to sth not visited though
6203 : : given that allows us to escape a region in alias walking. */
6204 : 34953002 : || (sameval
6205 : 7785421 : && TREE_CODE (sameval) == SSA_NAME
6206 : 4656802 : && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6207 : 3947303 : && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6208 : 1994089 : && (SSA_VAL (sameval, &visited_p), !visited_p)))
6209 : : /* Note this just drops to VARYING without inserting the PHI into
6210 : : the hashes. */
6211 : 283263 : result = PHI_RESULT (phi);
6212 : : /* If none of the edges was executable keep the value-number at VN_TOP,
6213 : : if only a single edge is exectuable use its value. */
6214 : 34668221 : else if (n_executable <= 1)
6215 : 6697788 : result = seen_undef ? seen_undef : sameval;
6216 : : /* If we saw only undefined values and VN_TOP use one of the
6217 : : undefined values. */
6218 : 27970433 : else if (sameval == VN_TOP)
6219 : 7315388 : result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6220 : : /* First see if it is equivalent to a phi node in this block. We prefer
6221 : : this as it allows IV elimination - see PRs 66502 and 67167. */
6222 : 27965127 : else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6223 : : {
6224 : 4197108 : if (!inserted
6225 : 75857 : && TREE_CODE (result) == SSA_NAME
6226 : 4272965 : && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6227 : : {
6228 : 75857 : gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6229 : 75857 : if (dump_file && (dump_flags & TDF_DETAILS))
6230 : : {
6231 : 2 : fprintf (dump_file, "Marking CSEd to PHI node ");
6232 : 2 : print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6233 : : 0, TDF_SLIM);
6234 : 2 : fprintf (dump_file, "\n");
6235 : : }
6236 : : }
6237 : : }
6238 : : /* If all values are the same use that, unless we've seen undefined
6239 : : values as well and the value isn't constant.
6240 : : CCP/copyprop have the same restriction to not remove uninit warnings. */
6241 : 23768019 : else if (sameval
6242 : 23768019 : && (! seen_undef || is_gimple_min_invariant (sameval)))
6243 : : result = sameval;
6244 : : else
6245 : : {
6246 : 23107831 : result = PHI_RESULT (phi);
6247 : : /* Only insert PHIs that are varying, for constant value numbers
6248 : : we mess up equivalences otherwise as we are only comparing
6249 : : the immediate controlling predicates. */
6250 : 23107831 : vn_phi_insert (phi, result, backedges_varying_p);
6251 : 23107831 : if (inserted)
6252 : 3229199 : *inserted = true;
6253 : : }
6254 : :
6255 : 34951484 : return set_ssa_val_to (PHI_RESULT (phi), result);
6256 : 34951484 : }
6257 : :
6258 : : /* Try to simplify RHS using equivalences and constant folding. */
6259 : :
6260 : : static tree
6261 : 125396918 : try_to_simplify (gassign *stmt)
6262 : : {
6263 : 125396918 : enum tree_code code = gimple_assign_rhs_code (stmt);
6264 : 125396918 : tree tem;
6265 : :
6266 : : /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6267 : : in this case, there is no point in doing extra work. */
6268 : 125396918 : if (code == SSA_NAME)
6269 : : return NULL_TREE;
6270 : :
6271 : : /* First try constant folding based on our current lattice. */
6272 : 110793312 : mprts_hook = vn_lookup_simplify_result;
6273 : 110793312 : tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6274 : 110793312 : mprts_hook = NULL;
6275 : 110793312 : if (tem
6276 : 110793312 : && (TREE_CODE (tem) == SSA_NAME
6277 : 24290559 : || is_gimple_min_invariant (tem)))
6278 : 24285360 : return tem;
6279 : :
6280 : : return NULL_TREE;
6281 : : }
6282 : :
6283 : : /* Visit and value number STMT, return true if the value number
6284 : : changed. */
6285 : :
6286 : : static bool
6287 : 447484011 : visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6288 : : {
6289 : 447484011 : bool changed = false;
6290 : :
6291 : 447484011 : if (dump_file && (dump_flags & TDF_DETAILS))
6292 : : {
6293 : 370247 : fprintf (dump_file, "Value numbering stmt = ");
6294 : 370247 : print_gimple_stmt (dump_file, stmt, 0);
6295 : : }
6296 : :
6297 : 447484011 : if (gimple_code (stmt) == GIMPLE_PHI)
6298 : 27466581 : changed = visit_phi (stmt, NULL, backedges_varying_p);
6299 : 590414289 : else if (gimple_has_volatile_ops (stmt))
6300 : 10096864 : changed = defs_to_varying (stmt);
6301 : 409920566 : else if (gassign *ass = dyn_cast <gassign *> (stmt))
6302 : : {
6303 : 130376976 : enum tree_code code = gimple_assign_rhs_code (ass);
6304 : 130376976 : tree lhs = gimple_assign_lhs (ass);
6305 : 130376976 : tree rhs1 = gimple_assign_rhs1 (ass);
6306 : 130376976 : tree simplified;
6307 : :
6308 : : /* Shortcut for copies. Simplifying copies is pointless,
6309 : : since we copy the expression and value they represent. */
6310 : 130376976 : if (code == SSA_NAME
6311 : 19583664 : && TREE_CODE (lhs) == SSA_NAME)
6312 : : {
6313 : 4980058 : changed = visit_copy (lhs, rhs1);
6314 : 4980058 : goto done;
6315 : : }
6316 : 125396918 : simplified = try_to_simplify (ass);
6317 : 125396918 : if (simplified)
6318 : : {
6319 : 24285360 : if (dump_file && (dump_flags & TDF_DETAILS))
6320 : : {
6321 : 13419 : fprintf (dump_file, "RHS ");
6322 : 13419 : print_gimple_expr (dump_file, ass, 0);
6323 : 13419 : fprintf (dump_file, " simplified to ");
6324 : 13419 : print_generic_expr (dump_file, simplified);
6325 : 13419 : fprintf (dump_file, "\n");
6326 : : }
6327 : : }
6328 : : /* Setting value numbers to constants will occasionally
6329 : : screw up phi congruence because constants are not
6330 : : uniquely associated with a single ssa name that can be
6331 : : looked up. */
6332 : 24285360 : if (simplified
6333 : 24285360 : && is_gimple_min_invariant (simplified)
6334 : 21481783 : && TREE_CODE (lhs) == SSA_NAME)
6335 : : {
6336 : 7298530 : changed = set_ssa_val_to (lhs, simplified);
6337 : 7298530 : goto done;
6338 : : }
6339 : 118098388 : else if (simplified
6340 : 16986830 : && TREE_CODE (simplified) == SSA_NAME
6341 : 2803577 : && TREE_CODE (lhs) == SSA_NAME)
6342 : : {
6343 : 2803577 : changed = visit_copy (lhs, simplified);
6344 : 2803577 : goto done;
6345 : : }
6346 : :
6347 : 115294811 : if ((TREE_CODE (lhs) == SSA_NAME
6348 : : /* We can substitute SSA_NAMEs that are live over
6349 : : abnormal edges with their constant value. */
6350 : 82442560 : && !(gimple_assign_copy_p (ass)
6351 : 26 : && is_gimple_min_invariant (rhs1))
6352 : 82442534 : && !(simplified
6353 : 0 : && is_gimple_min_invariant (simplified))
6354 : 82442534 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6355 : : /* Stores or copies from SSA_NAMEs that are live over
6356 : : abnormal edges are a problem. */
6357 : 197736098 : || (code == SSA_NAME
6358 : 14603606 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6359 : 1504 : changed = defs_to_varying (ass);
6360 : 115293307 : else if (REFERENCE_CLASS_P (lhs)
6361 : 115293307 : || DECL_P (lhs))
6362 : 32852020 : changed = visit_reference_op_store (lhs, rhs1, ass);
6363 : 82441287 : else if (TREE_CODE (lhs) == SSA_NAME)
6364 : : {
6365 : 82441287 : if ((gimple_assign_copy_p (ass)
6366 : 26 : && is_gimple_min_invariant (rhs1))
6367 : 82441313 : || (simplified
6368 : 0 : && is_gimple_min_invariant (simplified)))
6369 : : {
6370 : 0 : if (simplified)
6371 : 0 : changed = set_ssa_val_to (lhs, simplified);
6372 : : else
6373 : 0 : changed = set_ssa_val_to (lhs, rhs1);
6374 : : }
6375 : : else
6376 : : {
6377 : : /* Visit the original statement. */
6378 : 82441287 : switch (vn_get_stmt_kind (ass))
6379 : : {
6380 : 48220802 : case VN_NARY:
6381 : 48220802 : changed = visit_nary_op (lhs, ass);
6382 : 48220802 : break;
6383 : 34178508 : case VN_REFERENCE:
6384 : 34178508 : changed = visit_reference_op_load (lhs, rhs1, ass);
6385 : 34178508 : break;
6386 : 41977 : default:
6387 : 41977 : changed = defs_to_varying (ass);
6388 : 41977 : break;
6389 : : }
6390 : : }
6391 : : }
6392 : : else
6393 : 0 : changed = defs_to_varying (ass);
6394 : : }
6395 : 279543590 : else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6396 : : {
6397 : 24312978 : tree lhs = gimple_call_lhs (call_stmt);
6398 : 24312978 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6399 : : {
6400 : : /* Try constant folding based on our current lattice. */
6401 : 8136322 : tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6402 : : vn_valueize);
6403 : 8136322 : if (simplified)
6404 : : {
6405 : 62006 : if (dump_file && (dump_flags & TDF_DETAILS))
6406 : : {
6407 : 1 : fprintf (dump_file, "call ");
6408 : 1 : print_gimple_expr (dump_file, call_stmt, 0);
6409 : 1 : fprintf (dump_file, " simplified to ");
6410 : 1 : print_generic_expr (dump_file, simplified);
6411 : 1 : fprintf (dump_file, "\n");
6412 : : }
6413 : : }
6414 : : /* Setting value numbers to constants will occasionally
6415 : : screw up phi congruence because constants are not
6416 : : uniquely associated with a single ssa name that can be
6417 : : looked up. */
6418 : 62006 : if (simplified
6419 : 62006 : && is_gimple_min_invariant (simplified))
6420 : : {
6421 : 53609 : changed = set_ssa_val_to (lhs, simplified);
6422 : 107218 : if (gimple_vdef (call_stmt))
6423 : 725 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6424 : : SSA_VAL (gimple_vuse (call_stmt)));
6425 : 53609 : goto done;
6426 : : }
6427 : 8082713 : else if (simplified
6428 : 8397 : && TREE_CODE (simplified) == SSA_NAME)
6429 : : {
6430 : 299 : changed = visit_copy (lhs, simplified);
6431 : 598 : if (gimple_vdef (call_stmt))
6432 : 0 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6433 : : SSA_VAL (gimple_vuse (call_stmt)));
6434 : 299 : goto done;
6435 : : }
6436 : 8082414 : else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6437 : : {
6438 : 381 : changed = defs_to_varying (call_stmt);
6439 : 381 : goto done;
6440 : : }
6441 : : }
6442 : :
6443 : : /* Pick up flags from a devirtualization target. */
6444 : 24258689 : tree fn = gimple_call_fn (stmt);
6445 : 24258689 : int extra_fnflags = 0;
6446 : 24258689 : if (fn && TREE_CODE (fn) == SSA_NAME)
6447 : : {
6448 : 530528 : fn = SSA_VAL (fn);
6449 : 530528 : if (TREE_CODE (fn) == ADDR_EXPR
6450 : 530528 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6451 : 4765 : extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6452 : : }
6453 : 24258689 : if ((/* Calls to the same function with the same vuse
6454 : : and the same operands do not necessarily return the same
6455 : : value, unless they're pure or const. */
6456 : 24258689 : ((gimple_call_flags (call_stmt) | extra_fnflags)
6457 : 24258689 : & (ECF_PURE | ECF_CONST))
6458 : : /* If calls have a vdef, subsequent calls won't have
6459 : : the same incoming vuse. So, if 2 calls with vdef have the
6460 : : same vuse, we know they're not subsequent.
6461 : : We can value number 2 calls to the same function with the
6462 : : same vuse and the same operands which are not subsequent
6463 : : the same, because there is no code in the program that can
6464 : : compare the 2 values... */
6465 : 20608057 : || (gimple_vdef (call_stmt)
6466 : : /* ... unless the call returns a pointer which does
6467 : : not alias with anything else. In which case the
6468 : : information that the values are distinct are encoded
6469 : : in the IL. */
6470 : 20573418 : && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6471 : : /* Only perform the following when being called from PRE
6472 : : which embeds tail merging. */
6473 : 20141557 : && default_vn_walk_kind == VN_WALK))
6474 : : /* Do not process .DEFERRED_INIT since that confuses uninit
6475 : : analysis. */
6476 : 29212920 : && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6477 : 8604167 : changed = visit_reference_op_call (lhs, call_stmt);
6478 : : else
6479 : 15654522 : changed = defs_to_varying (call_stmt);
6480 : : }
6481 : : else
6482 : 255230612 : changed = defs_to_varying (stmt);
6483 : 447484011 : done:
6484 : 447484011 : return changed;
6485 : : }
6486 : :
6487 : :
6488 : : /* Allocate a value number table. */
6489 : :
6490 : : static void
6491 : 6065833 : allocate_vn_table (vn_tables_t table, unsigned size)
6492 : : {
6493 : 6065833 : table->phis = new vn_phi_table_type (size);
6494 : 6065833 : table->nary = new vn_nary_op_table_type (size);
6495 : 6065833 : table->references = new vn_reference_table_type (size);
6496 : 6065833 : }
6497 : :
6498 : : /* Free a value number table. */
6499 : :
6500 : : static void
6501 : 6065833 : free_vn_table (vn_tables_t table)
6502 : : {
6503 : : /* Walk over elements and release vectors. */
6504 : 6065833 : vn_reference_iterator_type hir;
6505 : 6065833 : vn_reference_t vr;
6506 : 150566135 : FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
6507 : 72250151 : vr->operands.release ();
6508 : 6065833 : delete table->phis;
6509 : 6065833 : table->phis = NULL;
6510 : 6065833 : delete table->nary;
6511 : 6065833 : table->nary = NULL;
6512 : 6065833 : delete table->references;
6513 : 6065833 : table->references = NULL;
6514 : 6065833 : }
6515 : :
6516 : : /* Set *ID according to RESULT. */
6517 : :
6518 : : static void
6519 : 34810274 : set_value_id_for_result (tree result, unsigned int *id)
6520 : : {
6521 : 34810274 : if (result && TREE_CODE (result) == SSA_NAME)
6522 : 21747868 : *id = VN_INFO (result)->value_id;
6523 : 9806755 : else if (result && is_gimple_min_invariant (result))
6524 : 3677097 : *id = get_or_alloc_constant_value_id (result);
6525 : : else
6526 : 9385309 : *id = get_next_value_id ();
6527 : 34810274 : }
6528 : :
6529 : : /* Set the value ids in the valid hash tables. */
6530 : :
6531 : : static void
6532 : 948158 : set_hashtable_value_ids (void)
6533 : : {
6534 : 948158 : vn_nary_op_iterator_type hin;
6535 : 948158 : vn_phi_iterator_type hip;
6536 : 948158 : vn_reference_iterator_type hir;
6537 : 948158 : vn_nary_op_t vno;
6538 : 948158 : vn_reference_t vr;
6539 : 948158 : vn_phi_t vp;
6540 : :
6541 : : /* Now set the value ids of the things we had put in the hash
6542 : : table. */
6543 : :
6544 : 48837974 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
6545 : 23944908 : if (! vno->predicated_values)
6546 : 7804013 : set_value_id_for_result (vno->u.result, &vno->value_id);
6547 : :
6548 : 9077948 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
6549 : 4064895 : set_value_id_for_result (vp->result, &vp->value_id);
6550 : :
6551 : 46830890 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
6552 : : hir)
6553 : 22941366 : set_value_id_for_result (vr->result, &vr->value_id);
6554 : 948158 : }
6555 : :
6556 : : /* Return the maximum value id we have ever seen. */
6557 : :
6558 : : unsigned int
6559 : 1896316 : get_max_value_id (void)
6560 : : {
6561 : 1896316 : return next_value_id;
6562 : : }
6563 : :
6564 : : /* Return the maximum constant value id we have ever seen. */
6565 : :
6566 : : unsigned int
6567 : 1896316 : get_max_constant_value_id (void)
6568 : : {
6569 : 1896316 : return -next_constant_value_id;
6570 : : }
6571 : :
6572 : : /* Return the next unique value id. */
6573 : :
6574 : : unsigned int
6575 : 49377337 : get_next_value_id (void)
6576 : : {
6577 : 49377337 : gcc_checking_assert ((int)next_value_id > 0);
6578 : 49377337 : return next_value_id++;
6579 : : }
6580 : :
6581 : : /* Return the next unique value id for constants. */
6582 : :
6583 : : unsigned int
6584 : 2519611 : get_next_constant_value_id (void)
6585 : : {
6586 : 2519611 : gcc_checking_assert (next_constant_value_id < 0);
6587 : 2519611 : return next_constant_value_id--;
6588 : : }
6589 : :
6590 : :
6591 : : /* Compare two expressions E1 and E2 and return true if they are equal.
6592 : : If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6593 : : otherwise VN_TOP only matches VN_TOP. */
6594 : :
6595 : : bool
6596 : 239438821 : expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
6597 : : {
6598 : : /* The obvious case. */
6599 : 239438821 : if (e1 == e2)
6600 : : return true;
6601 : :
6602 : : /* If either one is VN_TOP consider them equal. */
6603 : 71626736 : if (match_vn_top_optimistically
6604 : 66867216 : && (e1 == VN_TOP || e2 == VN_TOP))
6605 : : return true;
6606 : :
6607 : : /* If only one of them is null, they cannot be equal. While in general
6608 : : this should not happen for operations like TARGET_MEM_REF some
6609 : : operands are optional and an identity value we could substitute
6610 : : has differing semantics. */
6611 : 71626736 : if (!e1 || !e2)
6612 : : return false;
6613 : :
6614 : : /* SSA_NAME compare pointer equal. */
6615 : 71626736 : if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
6616 : : return false;
6617 : :
6618 : : /* Now perform the actual comparison. */
6619 : 35416816 : if (TREE_CODE (e1) == TREE_CODE (e2)
6620 : 35416816 : && operand_equal_p (e1, e2, OEP_PURE_SAME))
6621 : : return true;
6622 : :
6623 : : return false;
6624 : : }
6625 : :
6626 : :
6627 : : /* Return true if the nary operation NARY may trap. This is a copy
6628 : : of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6629 : :
6630 : : bool
6631 : 5402024 : vn_nary_may_trap (vn_nary_op_t nary)
6632 : : {
6633 : 5402024 : tree type;
6634 : 5402024 : tree rhs2 = NULL_TREE;
6635 : 5402024 : bool honor_nans = false;
6636 : 5402024 : bool honor_snans = false;
6637 : 5402024 : bool fp_operation = false;
6638 : 5402024 : bool honor_trapv = false;
6639 : 5402024 : bool handled, ret;
6640 : 5402024 : unsigned i;
6641 : :
6642 : 5402024 : if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
6643 : : || TREE_CODE_CLASS (nary->opcode) == tcc_unary
6644 : 5402024 : || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
6645 : : {
6646 : 5317582 : type = nary->type;
6647 : 5317582 : fp_operation = FLOAT_TYPE_P (type);
6648 : 5317582 : if (fp_operation)
6649 : : {
6650 : 118081 : honor_nans = flag_trapping_math && !flag_finite_math_only;
6651 : 118081 : honor_snans = flag_signaling_nans != 0;
6652 : : }
6653 : 5199501 : else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
6654 : : honor_trapv = true;
6655 : : }
6656 : 5402024 : if (nary->length >= 2)
6657 : 2191102 : rhs2 = nary->op[1];
6658 : 5402024 : ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
6659 : : honor_trapv, honor_nans, honor_snans,
6660 : : rhs2, &handled);
6661 : 5402024 : if (handled && ret)
6662 : : return true;
6663 : :
6664 : 12676072 : for (i = 0; i < nary->length; ++i)
6665 : 7390582 : if (tree_could_trap_p (nary->op[i]))
6666 : : return true;
6667 : :
6668 : : return false;
6669 : : }
6670 : :
6671 : : /* Return true if the reference operation REF may trap. */
6672 : :
6673 : : bool
6674 : 1856130 : vn_reference_may_trap (vn_reference_t ref)
6675 : : {
6676 : 1856130 : switch (ref->operands[0].opcode)
6677 : : {
6678 : : case MODIFY_EXPR:
6679 : : case CALL_EXPR:
6680 : : /* We do not handle calls. */
6681 : : return true;
6682 : : case ADDR_EXPR:
6683 : : /* And toplevel address computations never trap. */
6684 : : return false;
6685 : : default:;
6686 : : }
6687 : :
6688 : : vn_reference_op_t op;
6689 : : unsigned i;
6690 : 4920384 : FOR_EACH_VEC_ELT (ref->operands, i, op)
6691 : : {
6692 : 4920184 : switch (op->opcode)
6693 : : {
6694 : : case WITH_SIZE_EXPR:
6695 : : case TARGET_MEM_REF:
6696 : : /* Always variable. */
6697 : : return true;
6698 : 1201053 : case COMPONENT_REF:
6699 : 1201053 : if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
6700 : : return true;
6701 : : break;
6702 : 0 : case ARRAY_RANGE_REF:
6703 : 0 : if (TREE_CODE (op->op0) == SSA_NAME)
6704 : : return true;
6705 : : break;
6706 : 240449 : case ARRAY_REF:
6707 : 240449 : {
6708 : 240449 : if (TREE_CODE (op->op0) != INTEGER_CST)
6709 : : return true;
6710 : :
6711 : : /* !in_array_bounds */
6712 : 210519 : tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
6713 : 210519 : if (!domain_type)
6714 : : return true;
6715 : :
6716 : 210388 : tree min = op->op1;
6717 : 210388 : tree max = TYPE_MAX_VALUE (domain_type);
6718 : 210388 : if (!min
6719 : 210388 : || !max
6720 : 197361 : || TREE_CODE (min) != INTEGER_CST
6721 : 197361 : || TREE_CODE (max) != INTEGER_CST)
6722 : : return true;
6723 : :
6724 : 194895 : if (tree_int_cst_lt (op->op0, min)
6725 : 194895 : || tree_int_cst_lt (max, op->op0))
6726 : 597 : return true;
6727 : :
6728 : : break;
6729 : : }
6730 : : case MEM_REF:
6731 : : /* Nothing interesting in itself, the base is separate. */
6732 : : break;
6733 : : /* The following are the address bases. */
6734 : : case SSA_NAME:
6735 : : return true;
6736 : 1240643 : case ADDR_EXPR:
6737 : 1240643 : if (op->op0)
6738 : 1240643 : return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
6739 : : return false;
6740 : 3151923 : default:;
6741 : : }
6742 : : }
6743 : : return false;
6744 : : }
6745 : :
6746 : 10302703 : eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
6747 : 10302703 : bitmap inserted_exprs_)
6748 : 10302703 : : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
6749 : 10302703 : el_todo (0), eliminations (0), insertions (0),
6750 : 10302703 : inserted_exprs (inserted_exprs_)
6751 : : {
6752 : 10302703 : need_eh_cleanup = BITMAP_ALLOC (NULL);
6753 : 10302703 : need_ab_cleanup = BITMAP_ALLOC (NULL);
6754 : 10302703 : }
6755 : :
6756 : 10302703 : eliminate_dom_walker::~eliminate_dom_walker ()
6757 : : {
6758 : 10302703 : BITMAP_FREE (need_eh_cleanup);
6759 : 10302703 : BITMAP_FREE (need_ab_cleanup);
6760 : 10302703 : }
6761 : :
6762 : : /* Return a leader for OP that is available at the current point of the
6763 : : eliminate domwalk. */
6764 : :
6765 : : tree
6766 : 180137738 : eliminate_dom_walker::eliminate_avail (basic_block, tree op)
6767 : : {
6768 : 180137738 : tree valnum = VN_INFO (op)->valnum;
6769 : 180137738 : if (TREE_CODE (valnum) == SSA_NAME)
6770 : : {
6771 : 175102082 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6772 : : return valnum;
6773 : 304234746 : if (avail.length () > SSA_NAME_VERSION (valnum))
6774 : : {
6775 : 137028038 : tree av = avail[SSA_NAME_VERSION (valnum)];
6776 : : /* When PRE discovers a new redundancy there's no way to unite
6777 : : the value classes so it instead inserts a copy old-val = new-val.
6778 : : Look through such copies here, providing one more level of
6779 : : simplification at elimination time. */
6780 : 137028038 : gassign *ass;
6781 : 240428599 : if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
6782 : 73240191 : if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
6783 : : {
6784 : 38616444 : tree rhs1 = gimple_assign_rhs1 (ass);
6785 : 38616444 : if (CONSTANT_CLASS_P (rhs1)
6786 : 38616444 : || (TREE_CODE (rhs1) == SSA_NAME
6787 : 9037 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6788 : : av = rhs1;
6789 : : }
6790 : 137028038 : return av;
6791 : : }
6792 : : }
6793 : 5035656 : else if (is_gimple_min_invariant (valnum))
6794 : : return valnum;
6795 : : return NULL_TREE;
6796 : : }
6797 : :
6798 : : /* At the current point of the eliminate domwalk make OP available. */
6799 : :
6800 : : void
6801 : 50098245 : eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
6802 : : {
6803 : 50098245 : tree valnum = VN_INFO (op)->valnum;
6804 : 50098245 : if (TREE_CODE (valnum) == SSA_NAME)
6805 : : {
6806 : 96884411 : if (avail.length () <= SSA_NAME_VERSION (valnum))
6807 : 16800886 : avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
6808 : 50098245 : tree pushop = op;
6809 : 50098245 : if (avail[SSA_NAME_VERSION (valnum)])
6810 : 42966 : pushop = avail[SSA_NAME_VERSION (valnum)];
6811 : 50098245 : avail_stack.safe_push (pushop);
6812 : 50098245 : avail[SSA_NAME_VERSION (valnum)] = op;
6813 : : }
6814 : 50098245 : }
6815 : :
6816 : : /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
6817 : : the leader for the expression if insertion was successful. */
6818 : :
6819 : : tree
6820 : 163069 : eliminate_dom_walker::eliminate_insert (basic_block bb,
6821 : : gimple_stmt_iterator *gsi, tree val)
6822 : : {
6823 : : /* We can insert a sequence with a single assignment only. */
6824 : 163069 : gimple_seq stmts = VN_INFO (val)->expr;
6825 : 163069 : if (!gimple_seq_singleton_p (stmts))
6826 : : return NULL_TREE;
6827 : 305564 : gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
6828 : 163069 : if (!stmt
6829 : 163069 : || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
6830 : : && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
6831 : : && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
6832 : : && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
6833 : : && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6834 : 21 : || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
6835 : : return NULL_TREE;
6836 : :
6837 : 33510 : tree op = gimple_assign_rhs1 (stmt);
6838 : 33510 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
6839 : 33510 : || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6840 : 18335 : op = TREE_OPERAND (op, 0);
6841 : 33510 : tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
6842 : 33464 : if (!leader)
6843 : : return NULL_TREE;
6844 : :
6845 : 20578 : tree res;
6846 : 20578 : stmts = NULL;
6847 : 38014 : if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6848 : 32098 : res = gimple_build (&stmts, BIT_FIELD_REF,
6849 : 16049 : TREE_TYPE (val), leader,
6850 : 16049 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
6851 : 16049 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
6852 : 4529 : else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
6853 : 42 : res = gimple_build (&stmts, BIT_AND_EXPR,
6854 : 21 : TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
6855 : : else
6856 : 4508 : res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
6857 : 4508 : TREE_TYPE (val), leader);
6858 : 20578 : if (TREE_CODE (res) != SSA_NAME
6859 : 20577 : || SSA_NAME_IS_DEFAULT_DEF (res)
6860 : 41155 : || gimple_bb (SSA_NAME_DEF_STMT (res)))
6861 : : {
6862 : 4 : gimple_seq_discard (stmts);
6863 : :
6864 : : /* During propagation we have to treat SSA info conservatively
6865 : : and thus we can end up simplifying the inserted expression
6866 : : at elimination time to sth not defined in stmts. */
6867 : : /* But then this is a redundancy we failed to detect. Which means
6868 : : res now has two values. That doesn't play well with how
6869 : : we track availability here, so give up. */
6870 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
6871 : : {
6872 : 0 : if (TREE_CODE (res) == SSA_NAME)
6873 : 0 : res = eliminate_avail (bb, res);
6874 : 0 : if (res)
6875 : : {
6876 : 0 : fprintf (dump_file, "Failed to insert expression for value ");
6877 : 0 : print_generic_expr (dump_file, val);
6878 : 0 : fprintf (dump_file, " which is really fully redundant to ");
6879 : 0 : print_generic_expr (dump_file, res);
6880 : 0 : fprintf (dump_file, "\n");
6881 : : }
6882 : : }
6883 : :
6884 : 4 : return NULL_TREE;
6885 : : }
6886 : : else
6887 : : {
6888 : 20574 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6889 : 20574 : vn_ssa_aux_t vn_info = VN_INFO (res);
6890 : 20574 : vn_info->valnum = val;
6891 : 20574 : vn_info->visited = true;
6892 : : }
6893 : :
6894 : 20574 : insertions++;
6895 : 20574 : if (dump_file && (dump_flags & TDF_DETAILS))
6896 : : {
6897 : 501 : fprintf (dump_file, "Inserted ");
6898 : 501 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
6899 : : }
6900 : :
6901 : : return res;
6902 : : }
6903 : :
6904 : : void
6905 : 345214357 : eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
6906 : : {
6907 : 345214357 : tree sprime = NULL_TREE;
6908 : 345214357 : gimple *stmt = gsi_stmt (*gsi);
6909 : 345214357 : tree lhs = gimple_get_lhs (stmt);
6910 : 119879939 : if (lhs && TREE_CODE (lhs) == SSA_NAME
6911 : 163251186 : && !gimple_has_volatile_ops (stmt)
6912 : : /* See PR43491. Do not replace a global register variable when
6913 : : it is a the RHS of an assignment. Do replace local register
6914 : : variables since gcc does not guarantee a local variable will
6915 : : be allocated in register.
6916 : : ??? The fix isn't effective here. This should instead
6917 : : be ensured by not value-numbering them the same but treating
6918 : : them like volatiles? */
6919 : 425783971 : && !(gimple_assign_single_p (stmt)
6920 : 34917022 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
6921 : 2498921 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
6922 : 4180 : && is_global_var (gimple_assign_rhs1 (stmt)))))
6923 : : {
6924 : 80569370 : sprime = eliminate_avail (b, lhs);
6925 : 80569370 : if (!sprime)
6926 : : {
6927 : : /* If there is no existing usable leader but SCCVN thinks
6928 : : it has an expression it wants to use as replacement,
6929 : : insert that. */
6930 : 68055786 : tree val = VN_INFO (lhs)->valnum;
6931 : 68055786 : vn_ssa_aux_t vn_info;
6932 : 68055786 : if (val != VN_TOP
6933 : 68055786 : && TREE_CODE (val) == SSA_NAME
6934 : 68055786 : && (vn_info = VN_INFO (val), true)
6935 : 68055786 : && vn_info->needs_insertion
6936 : 357469 : && vn_info->expr != NULL
6937 : 68218855 : && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
6938 : 20574 : eliminate_push_avail (b, sprime);
6939 : : }
6940 : :
6941 : : /* If this now constitutes a copy duplicate points-to
6942 : : and range info appropriately. This is especially
6943 : : important for inserted code. */
6944 : 68055786 : if (sprime
6945 : 12534158 : && TREE_CODE (sprime) == SSA_NAME)
6946 : 8638742 : maybe_duplicate_ssa_info_at_copy (lhs, sprime);
6947 : :
6948 : : /* Inhibit the use of an inserted PHI on a loop header when
6949 : : the address of the memory reference is a simple induction
6950 : : variable. In other cases the vectorizer won't do anything
6951 : : anyway (either it's loop invariant or a complicated
6952 : : expression). */
6953 : 8638742 : if (sprime
6954 : 12534158 : && TREE_CODE (sprime) == SSA_NAME
6955 : 8638742 : && do_pre
6956 : 924017 : && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
6957 : 906786 : && loop_outer (b->loop_father)
6958 : 370176 : && has_zero_uses (sprime)
6959 : 185033 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
6960 : 184880 : && gimple_assign_load_p (stmt))
6961 : : {
6962 : 98814 : gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
6963 : 98814 : basic_block def_bb = gimple_bb (def_stmt);
6964 : 98814 : if (gimple_code (def_stmt) == GIMPLE_PHI
6965 : 98814 : && def_bb->loop_father->header == def_bb)
6966 : : {
6967 : 62089 : loop_p loop = def_bb->loop_father;
6968 : 62089 : ssa_op_iter iter;
6969 : 62089 : tree op;
6970 : 62089 : bool found = false;
6971 : 77134 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6972 : : {
6973 : 57969 : affine_iv iv;
6974 : 57969 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
6975 : 57969 : if (def_bb
6976 : 53188 : && flow_bb_inside_loop_p (loop, def_bb)
6977 : 106515 : && simple_iv (loop, loop, op, &iv, true))
6978 : : {
6979 : 42924 : found = true;
6980 : 42924 : break;
6981 : : }
6982 : : }
6983 : 19165 : if (found)
6984 : : {
6985 : 42924 : if (dump_file && (dump_flags & TDF_DETAILS))
6986 : : {
6987 : 3 : fprintf (dump_file, "Not replacing ");
6988 : 3 : print_gimple_expr (dump_file, stmt, 0);
6989 : 3 : fprintf (dump_file, " with ");
6990 : 3 : print_generic_expr (dump_file, sprime);
6991 : 3 : fprintf (dump_file, " which would add a loop"
6992 : : " carried dependence to loop %d\n",
6993 : : loop->num);
6994 : : }
6995 : : /* Don't keep sprime available. */
6996 : 42924 : sprime = NULL_TREE;
6997 : : }
6998 : : }
6999 : : }
7000 : :
7001 : 80569370 : if (sprime)
7002 : : {
7003 : : /* If we can propagate the value computed for LHS into
7004 : : all uses don't bother doing anything with this stmt. */
7005 : 12491234 : if (may_propagate_copy (lhs, sprime))
7006 : : {
7007 : : /* Mark it for removal. */
7008 : 12489332 : to_remove.safe_push (stmt);
7009 : :
7010 : : /* ??? Don't count copy/constant propagations. */
7011 : 12489332 : if (gimple_assign_single_p (stmt)
7012 : 12489332 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7013 : 4378480 : || gimple_assign_rhs1 (stmt) == sprime))
7014 : 13212098 : return;
7015 : :
7016 : 7437834 : if (dump_file && (dump_flags & TDF_DETAILS))
7017 : : {
7018 : 17415 : fprintf (dump_file, "Replaced ");
7019 : 17415 : print_gimple_expr (dump_file, stmt, 0);
7020 : 17415 : fprintf (dump_file, " with ");
7021 : 17415 : print_generic_expr (dump_file, sprime);
7022 : 17415 : fprintf (dump_file, " in all uses of ");
7023 : 17415 : print_gimple_stmt (dump_file, stmt, 0);
7024 : : }
7025 : :
7026 : 7437834 : eliminations++;
7027 : 7437834 : return;
7028 : : }
7029 : :
7030 : : /* If this is an assignment from our leader (which
7031 : : happens in the case the value-number is a constant)
7032 : : then there is nothing to do. Likewise if we run into
7033 : : inserted code that needed a conversion because of
7034 : : our type-agnostic value-numbering of loads. */
7035 : 1902 : if ((gimple_assign_single_p (stmt)
7036 : 1 : || (is_gimple_assign (stmt)
7037 : 1 : && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7038 : 0 : || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
7039 : 1903 : && sprime == gimple_assign_rhs1 (stmt))
7040 : : return;
7041 : :
7042 : : /* Else replace its RHS. */
7043 : 711 : if (dump_file && (dump_flags & TDF_DETAILS))
7044 : : {
7045 : 0 : fprintf (dump_file, "Replaced ");
7046 : 0 : print_gimple_expr (dump_file, stmt, 0);
7047 : 0 : fprintf (dump_file, " with ");
7048 : 0 : print_generic_expr (dump_file, sprime);
7049 : 0 : fprintf (dump_file, " in ");
7050 : 0 : print_gimple_stmt (dump_file, stmt, 0);
7051 : : }
7052 : 711 : eliminations++;
7053 : :
7054 : 711 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
7055 : 711 : && stmt_can_make_abnormal_goto (stmt));
7056 : 711 : gimple *orig_stmt = stmt;
7057 : 711 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
7058 : 711 : TREE_TYPE (sprime)))
7059 : : {
7060 : : /* We preserve conversions to but not from function or method
7061 : : types. This asymmetry makes it necessary to re-instantiate
7062 : : conversions here. */
7063 : 709 : if (POINTER_TYPE_P (TREE_TYPE (lhs))
7064 : 709 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7065 : 709 : sprime = fold_convert (TREE_TYPE (lhs), sprime);
7066 : : else
7067 : 0 : gcc_unreachable ();
7068 : : }
7069 : 711 : tree vdef = gimple_vdef (stmt);
7070 : 711 : tree vuse = gimple_vuse (stmt);
7071 : 711 : propagate_tree_value_into_stmt (gsi, sprime);
7072 : 711 : stmt = gsi_stmt (*gsi);
7073 : 711 : update_stmt (stmt);
7074 : : /* In case the VDEF on the original stmt was released, value-number
7075 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7076 : : released virtual operands. */
7077 : 1422 : if (vdef != gimple_vdef (stmt))
7078 : : {
7079 : 0 : gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7080 : 0 : VN_INFO (vdef)->valnum = vuse;
7081 : : }
7082 : :
7083 : : /* If we removed EH side-effects from the statement, clean
7084 : : its EH information. */
7085 : 711 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7086 : : {
7087 : 0 : bitmap_set_bit (need_eh_cleanup,
7088 : 0 : gimple_bb (stmt)->index);
7089 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7090 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7091 : : }
7092 : :
7093 : : /* Likewise for AB side-effects. */
7094 : 711 : if (can_make_abnormal_goto
7095 : 711 : && !stmt_can_make_abnormal_goto (stmt))
7096 : : {
7097 : 0 : bitmap_set_bit (need_ab_cleanup,
7098 : 0 : gimple_bb (stmt)->index);
7099 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7100 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7101 : : }
7102 : :
7103 : 711 : return;
7104 : : }
7105 : : }
7106 : :
7107 : : /* If the statement is a scalar store, see if the expression
7108 : : has the same value number as its rhs. If so, the store is
7109 : : dead. */
7110 : 332723123 : if (gimple_assign_single_p (stmt)
7111 : 128701348 : && !gimple_has_volatile_ops (stmt)
7112 : 54962985 : && !is_gimple_reg (gimple_assign_lhs (stmt))
7113 : 28449659 : && (TREE_CODE (gimple_assign_lhs (stmt)) != VAR_DECL
7114 : 3120789 : || !DECL_HARD_REGISTER (gimple_assign_lhs (stmt)))
7115 : 361168773 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7116 : 16453405 : || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7117 : : {
7118 : 25037552 : tree rhs = gimple_assign_rhs1 (stmt);
7119 : 25037552 : vn_reference_t vnresult;
7120 : : /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7121 : : typed load of a byte known to be 0x11 as 1 so a store of
7122 : : a boolean 1 is detected as redundant. Because of this we
7123 : : have to make sure to lookup with a ref where its size
7124 : : matches the precision. */
7125 : 25037552 : tree lookup_lhs = lhs;
7126 : 49814359 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7127 : 12993115 : && (TREE_CODE (lhs) != COMPONENT_REF
7128 : 8043935 : || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7129 : 37780856 : && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7130 : : {
7131 : 434455 : if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
7132 : 443448 : && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7133 : : lookup_lhs = NULL_TREE;
7134 : 427628 : else if (TREE_CODE (lhs) == COMPONENT_REF
7135 : 427628 : || TREE_CODE (lhs) == MEM_REF)
7136 : : {
7137 : 300179 : tree ltype = build_nonstandard_integer_type
7138 : 300179 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7139 : 300179 : TYPE_UNSIGNED (TREE_TYPE (lhs)));
7140 : 300179 : if (TREE_CODE (lhs) == COMPONENT_REF)
7141 : : {
7142 : 227322 : tree foff = component_ref_field_offset (lhs);
7143 : 227322 : tree f = TREE_OPERAND (lhs, 1);
7144 : 227322 : if (!poly_int_tree_p (foff))
7145 : : lookup_lhs = NULL_TREE;
7146 : : else
7147 : 454644 : lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7148 : 227322 : TREE_OPERAND (lhs, 0),
7149 : 227322 : TYPE_SIZE (TREE_TYPE (lhs)),
7150 : : bit_from_pos
7151 : 227322 : (foff, DECL_FIELD_BIT_OFFSET (f)));
7152 : : }
7153 : : else
7154 : 72857 : lookup_lhs = build2 (MEM_REF, ltype,
7155 : 72857 : TREE_OPERAND (lhs, 0),
7156 : 72857 : TREE_OPERAND (lhs, 1));
7157 : : }
7158 : : else
7159 : : lookup_lhs = NULL_TREE;
7160 : : }
7161 : 24903276 : tree val = NULL_TREE;
7162 : 24903276 : if (lookup_lhs)
7163 : 49806552 : val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7164 : : VN_WALKREWRITE, &vnresult, false,
7165 : : NULL, NULL_TREE, true);
7166 : 25037552 : if (TREE_CODE (rhs) == SSA_NAME)
7167 : 11992245 : rhs = VN_INFO (rhs)->valnum;
7168 : 25037552 : if (val
7169 : 25037552 : && (operand_equal_p (val, rhs, 0)
7170 : : /* Due to the bitfield lookups above we can get bit
7171 : : interpretations of the same RHS as values here. Those
7172 : : are redundant as well. */
7173 : 3068524 : || (TREE_CODE (val) == SSA_NAME
7174 : 1856572 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7175 : 1676507 : && (val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7176 : 1676507 : && TREE_CODE (val) == VIEW_CONVERT_EXPR
7177 : 3381 : && TREE_OPERAND (val, 0) == rhs)))
7178 : : {
7179 : : /* We can only remove the later store if the former aliases
7180 : : at least all accesses the later one does or if the store
7181 : : was to readonly memory storing the same value. */
7182 : 208763 : ao_ref lhs_ref;
7183 : 208763 : ao_ref_init (&lhs_ref, lhs);
7184 : 208763 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
7185 : 208763 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7186 : 208763 : if (! vnresult
7187 : 208763 : || ((vnresult->set == set
7188 : 32857 : || alias_set_subset_of (set, vnresult->set))
7189 : 205694 : && (vnresult->base_set == base_set
7190 : 10296 : || alias_set_subset_of (base_set, vnresult->base_set))))
7191 : : {
7192 : 202874 : if (dump_file && (dump_flags & TDF_DETAILS))
7193 : : {
7194 : 16 : fprintf (dump_file, "Deleted redundant store ");
7195 : 16 : print_gimple_stmt (dump_file, stmt, 0);
7196 : : }
7197 : :
7198 : : /* Queue stmt for removal. */
7199 : 202874 : to_remove.safe_push (stmt);
7200 : 202874 : return;
7201 : : }
7202 : : }
7203 : : }
7204 : :
7205 : : /* If this is a control statement value numbering left edges
7206 : : unexecuted on force the condition in a way consistent with
7207 : : that. */
7208 : 332520249 : if (gcond *cond = dyn_cast <gcond *> (stmt))
7209 : : {
7210 : 18591590 : if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7211 : 18591590 : ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7212 : : {
7213 : 517990 : if (dump_file && (dump_flags & TDF_DETAILS))
7214 : : {
7215 : 15 : fprintf (dump_file, "Removing unexecutable edge from ");
7216 : 15 : print_gimple_stmt (dump_file, stmt, 0);
7217 : : }
7218 : 517990 : if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7219 : 517990 : == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7220 : 155368 : gimple_cond_make_true (cond);
7221 : : else
7222 : 362622 : gimple_cond_make_false (cond);
7223 : 517990 : update_stmt (cond);
7224 : 517990 : el_todo |= TODO_cleanup_cfg;
7225 : 517990 : return;
7226 : : }
7227 : : }
7228 : :
7229 : 332002259 : bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7230 : 332002259 : bool was_noreturn = (is_gimple_call (stmt)
7231 : 332002259 : && gimple_call_noreturn_p (stmt));
7232 : 332002259 : tree vdef = gimple_vdef (stmt);
7233 : 332002259 : tree vuse = gimple_vuse (stmt);
7234 : :
7235 : : /* If we didn't replace the whole stmt (or propagate the result
7236 : : into all uses), replace all uses on this stmt with their
7237 : : leaders. */
7238 : 332002259 : bool modified = false;
7239 : 332002259 : use_operand_p use_p;
7240 : 332002259 : ssa_op_iter iter;
7241 : 493542015 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7242 : : {
7243 : 161539756 : tree use = USE_FROM_PTR (use_p);
7244 : : /* ??? The call code above leaves stmt operands un-updated. */
7245 : 161539756 : if (TREE_CODE (use) != SSA_NAME)
7246 : 0 : continue;
7247 : 161539756 : tree sprime;
7248 : 161539756 : if (SSA_NAME_IS_DEFAULT_DEF (use))
7249 : : /* ??? For default defs BB shouldn't matter, but we have to
7250 : : solve the inconsistency between rpo eliminate and
7251 : : dom eliminate avail valueization first. */
7252 : 26554030 : sprime = eliminate_avail (b, use);
7253 : : else
7254 : : /* Look for sth available at the definition block of the argument.
7255 : : This avoids inconsistencies between availability there which
7256 : : decides if the stmt can be removed and availability at the
7257 : : use site. The SSA property ensures that things available
7258 : : at the definition are also available at uses. */
7259 : 134985726 : sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7260 : 161539756 : if (sprime && sprime != use
7261 : 11967361 : && may_propagate_copy (use, sprime, true)
7262 : : /* We substitute into debug stmts to avoid excessive
7263 : : debug temporaries created by removed stmts, but we need
7264 : : to avoid doing so for inserted sprimes as we never want
7265 : : to create debug temporaries for them. */
7266 : 173506408 : && (!inserted_exprs
7267 : 1174054 : || TREE_CODE (sprime) != SSA_NAME
7268 : 1155346 : || !is_gimple_debug (stmt)
7269 : 347716 : || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7270 : : {
7271 : 11645463 : propagate_value (use_p, sprime);
7272 : 11645463 : modified = true;
7273 : : }
7274 : : }
7275 : :
7276 : : /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7277 : : into which is a requirement for the IPA devirt machinery. */
7278 : 332002259 : gimple *old_stmt = stmt;
7279 : 332002259 : if (modified)
7280 : : {
7281 : : /* If a formerly non-invariant ADDR_EXPR is turned into an
7282 : : invariant one it was on a separate stmt. */
7283 : 10816253 : if (gimple_assign_single_p (stmt)
7284 : 10816253 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7285 : 233177 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7286 : 10816253 : gimple_stmt_iterator prev = *gsi;
7287 : 10816253 : gsi_prev (&prev);
7288 : 10816253 : if (fold_stmt (gsi, follow_all_ssa_edges))
7289 : : {
7290 : : /* fold_stmt may have created new stmts inbetween
7291 : : the previous stmt and the folded stmt. Mark
7292 : : all defs created there as varying to not confuse
7293 : : the SCCVN machinery as we're using that even during
7294 : : elimination. */
7295 : 933408 : if (gsi_end_p (prev))
7296 : 213060 : prev = gsi_start_bb (b);
7297 : : else
7298 : 826878 : gsi_next (&prev);
7299 : 933408 : if (gsi_stmt (prev) != gsi_stmt (*gsi))
7300 : 82661 : do
7301 : : {
7302 : 51326 : tree def;
7303 : 51326 : ssa_op_iter dit;
7304 : 99437 : FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7305 : : dit, SSA_OP_ALL_DEFS)
7306 : : /* As existing DEFs may move between stmts
7307 : : only process new ones. */
7308 : 48111 : if (! has_VN_INFO (def))
7309 : : {
7310 : 31233 : vn_ssa_aux_t vn_info = VN_INFO (def);
7311 : 31233 : vn_info->valnum = def;
7312 : 31233 : vn_info->visited = true;
7313 : : }
7314 : 51326 : if (gsi_stmt (prev) == gsi_stmt (*gsi))
7315 : : break;
7316 : 31335 : gsi_next (&prev);
7317 : 31335 : }
7318 : : while (1);
7319 : : }
7320 : 10816253 : stmt = gsi_stmt (*gsi);
7321 : : /* In case we folded the stmt away schedule the NOP for removal. */
7322 : 10816253 : if (gimple_nop_p (stmt))
7323 : 807 : to_remove.safe_push (stmt);
7324 : : }
7325 : :
7326 : : /* Visit indirect calls and turn them into direct calls if
7327 : : possible using the devirtualization machinery. Do this before
7328 : : checking for required EH/abnormal/noreturn cleanup as devird
7329 : : may expose more of those. */
7330 : 332002259 : if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7331 : : {
7332 : 21864709 : tree fn = gimple_call_fn (call_stmt);
7333 : 21864709 : if (fn
7334 : 21333018 : && flag_devirtualize
7335 : 42474734 : && virtual_method_call_p (fn))
7336 : : {
7337 : 198268 : tree otr_type = obj_type_ref_class (fn);
7338 : 198268 : unsigned HOST_WIDE_INT otr_tok
7339 : 198268 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7340 : 198268 : tree instance;
7341 : 198268 : ipa_polymorphic_call_context context (current_function_decl,
7342 : 198268 : fn, stmt, &instance);
7343 : 198268 : context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7344 : : otr_type, stmt, NULL);
7345 : 198268 : bool final;
7346 : 198268 : vec <cgraph_node *> targets
7347 : 198268 : = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7348 : : otr_tok, context, &final);
7349 : 198268 : if (dump_file)
7350 : 22 : dump_possible_polymorphic_call_targets (dump_file,
7351 : : obj_type_ref_class (fn),
7352 : : otr_tok, context);
7353 : 198582 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
7354 : : {
7355 : 113 : tree fn;
7356 : 113 : if (targets.length () == 1)
7357 : 113 : fn = targets[0]->decl;
7358 : : else
7359 : 0 : fn = builtin_decl_unreachable ();
7360 : 113 : if (dump_enabled_p ())
7361 : : {
7362 : 9 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7363 : : "converting indirect call to "
7364 : : "function %s\n",
7365 : 9 : lang_hooks.decl_printable_name (fn, 2));
7366 : : }
7367 : 113 : gimple_call_set_fndecl (call_stmt, fn);
7368 : : /* If changing the call to __builtin_unreachable
7369 : : or similar noreturn function, adjust gimple_call_fntype
7370 : : too. */
7371 : 113 : if (gimple_call_noreturn_p (call_stmt)
7372 : 0 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7373 : 0 : && TYPE_ARG_TYPES (TREE_TYPE (fn))
7374 : 113 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7375 : 0 : == void_type_node))
7376 : 0 : gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7377 : 113 : maybe_remove_unused_call_args (cfun, call_stmt);
7378 : 113 : modified = true;
7379 : : }
7380 : : }
7381 : : }
7382 : :
7383 : 332002259 : if (modified)
7384 : : {
7385 : : /* When changing a call into a noreturn call, cfg cleanup
7386 : : is needed to fix up the noreturn call. */
7387 : 10816274 : if (!was_noreturn
7388 : 10816274 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7389 : 56 : to_fixup.safe_push (stmt);
7390 : : /* When changing a condition or switch into one we know what
7391 : : edge will be executed, schedule a cfg cleanup. */
7392 : 10816274 : if ((gimple_code (stmt) == GIMPLE_COND
7393 : 1431396 : && (gimple_cond_true_p (as_a <gcond *> (stmt))
7394 : 1426146 : || gimple_cond_false_p (as_a <gcond *> (stmt))))
7395 : 12240540 : || (gimple_code (stmt) == GIMPLE_SWITCH
7396 : 8191 : && TREE_CODE (gimple_switch_index
7397 : : (as_a <gswitch *> (stmt))) == INTEGER_CST))
7398 : 8974 : el_todo |= TODO_cleanup_cfg;
7399 : : /* If we removed EH side-effects from the statement, clean
7400 : : its EH information. */
7401 : 10816274 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7402 : : {
7403 : 1379 : bitmap_set_bit (need_eh_cleanup,
7404 : 1379 : gimple_bb (stmt)->index);
7405 : 1379 : if (dump_file && (dump_flags & TDF_DETAILS))
7406 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7407 : : }
7408 : : /* Likewise for AB side-effects. */
7409 : 10816274 : if (can_make_abnormal_goto
7410 : 10816274 : && !stmt_can_make_abnormal_goto (stmt))
7411 : : {
7412 : 0 : bitmap_set_bit (need_ab_cleanup,
7413 : 0 : gimple_bb (stmt)->index);
7414 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7415 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7416 : : }
7417 : 10816274 : update_stmt (stmt);
7418 : : /* In case the VDEF on the original stmt was released, value-number
7419 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7420 : : released virtual operands. */
7421 : 13921448 : if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7422 : 1716 : VN_INFO (vdef)->valnum = vuse;
7423 : : }
7424 : :
7425 : : /* Make new values available - for fully redundant LHS we
7426 : : continue with the next stmt above and skip this.
7427 : : But avoid picking up dead defs. */
7428 : 332002259 : tree def;
7429 : 401377902 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7430 : 69375643 : if (! has_zero_uses (def)
7431 : 69375643 : || (inserted_exprs
7432 : 217369 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7433 : 68038432 : eliminate_push_avail (b, def);
7434 : : }
7435 : :
7436 : : /* Perform elimination for the basic-block B during the domwalk. */
7437 : :
7438 : : edge
7439 : 41313651 : eliminate_dom_walker::before_dom_children (basic_block b)
7440 : : {
7441 : : /* Mark new bb. */
7442 : 41313651 : avail_stack.safe_push (NULL_TREE);
7443 : :
7444 : : /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7445 : 41313651 : if (!(b->flags & BB_EXECUTABLE))
7446 : : return NULL;
7447 : :
7448 : 36651310 : vn_context_bb = b;
7449 : :
7450 : 48587764 : for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7451 : : {
7452 : 11936454 : gphi *phi = gsi.phi ();
7453 : 11936454 : tree res = PHI_RESULT (phi);
7454 : :
7455 : 23872908 : if (virtual_operand_p (res))
7456 : : {
7457 : 5533633 : gsi_next (&gsi);
7458 : 5533633 : continue;
7459 : : }
7460 : :
7461 : 6402821 : tree sprime = eliminate_avail (b, res);
7462 : 6402821 : if (sprime
7463 : 6402821 : && sprime != res)
7464 : : {
7465 : 518565 : if (dump_file && (dump_flags & TDF_DETAILS))
7466 : : {
7467 : 22 : fprintf (dump_file, "Replaced redundant PHI node defining ");
7468 : 22 : print_generic_expr (dump_file, res);
7469 : 22 : fprintf (dump_file, " with ");
7470 : 22 : print_generic_expr (dump_file, sprime);
7471 : 22 : fprintf (dump_file, "\n");
7472 : : }
7473 : :
7474 : : /* If we inserted this PHI node ourself, it's not an elimination. */
7475 : 518565 : if (! inserted_exprs
7476 : 637224 : || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
7477 : 488619 : eliminations++;
7478 : :
7479 : : /* If we will propagate into all uses don't bother to do
7480 : : anything. */
7481 : 518565 : if (may_propagate_copy (res, sprime))
7482 : : {
7483 : : /* Mark the PHI for removal. */
7484 : 518565 : to_remove.safe_push (phi);
7485 : 518565 : gsi_next (&gsi);
7486 : 518565 : continue;
7487 : : }
7488 : :
7489 : 0 : remove_phi_node (&gsi, false);
7490 : :
7491 : 0 : if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
7492 : 0 : sprime = fold_convert (TREE_TYPE (res), sprime);
7493 : 0 : gimple *stmt = gimple_build_assign (res, sprime);
7494 : 0 : gimple_stmt_iterator gsi2 = gsi_after_labels (b);
7495 : 0 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
7496 : 0 : continue;
7497 : 0 : }
7498 : :
7499 : 5884256 : eliminate_push_avail (b, res);
7500 : 5884256 : gsi_next (&gsi);
7501 : : }
7502 : :
7503 : 73302620 : for (gimple_stmt_iterator gsi = gsi_start_bb (b);
7504 : 279239819 : !gsi_end_p (gsi);
7505 : 242588509 : gsi_next (&gsi))
7506 : 242588509 : eliminate_stmt (b, &gsi);
7507 : :
7508 : : /* Replace destination PHI arguments. */
7509 : 36651310 : edge_iterator ei;
7510 : 36651310 : edge e;
7511 : 86480012 : FOR_EACH_EDGE (e, ei, b->succs)
7512 : 49828702 : if (e->flags & EDGE_EXECUTABLE)
7513 : 49372425 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
7514 : 79246277 : !gsi_end_p (gsi);
7515 : 29873852 : gsi_next (&gsi))
7516 : : {
7517 : 29873852 : gphi *phi = gsi.phi ();
7518 : 29873852 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7519 : 29873852 : tree arg = USE_FROM_PTR (use_p);
7520 : 49458298 : if (TREE_CODE (arg) != SSA_NAME
7521 : 29873852 : || virtual_operand_p (arg))
7522 : 19584446 : continue;
7523 : 10289406 : tree sprime = eliminate_avail (b, arg);
7524 : 20578812 : if (sprime && may_propagate_copy (arg, sprime,
7525 : 10289406 : !(e->flags & EDGE_ABNORMAL)))
7526 : 10278768 : propagate_value (use_p, sprime);
7527 : : }
7528 : :
7529 : 36651310 : vn_context_bb = NULL;
7530 : :
7531 : 36651310 : return NULL;
7532 : : }
7533 : :
7534 : : /* Make no longer available leaders no longer available. */
7535 : :
7536 : : void
7537 : 41313651 : eliminate_dom_walker::after_dom_children (basic_block)
7538 : : {
7539 : 41313651 : tree entry;
7540 : 91411896 : while ((entry = avail_stack.pop ()) != NULL_TREE)
7541 : : {
7542 : 50098245 : tree valnum = VN_INFO (entry)->valnum;
7543 : 50098245 : tree old = avail[SSA_NAME_VERSION (valnum)];
7544 : 50098245 : if (old == entry)
7545 : 50055279 : avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
7546 : : else
7547 : 42966 : avail[SSA_NAME_VERSION (valnum)] = entry;
7548 : : }
7549 : 41313651 : }
7550 : :
7551 : : /* Remove queued stmts and perform delayed cleanups. */
7552 : :
7553 : : unsigned
7554 : 6046551 : eliminate_dom_walker::eliminate_cleanup (bool region_p)
7555 : : {
7556 : 6046551 : statistics_counter_event (cfun, "Eliminated", eliminations);
7557 : 6046551 : statistics_counter_event (cfun, "Insertions", insertions);
7558 : :
7559 : : /* We cannot remove stmts during BB walk, especially not release SSA
7560 : : names there as this confuses the VN machinery. The stmts ending
7561 : : up in to_remove are either stores or simple copies.
7562 : : Remove stmts in reverse order to make debug stmt creation possible. */
7563 : 32596178 : while (!to_remove.is_empty ())
7564 : : {
7565 : 14456469 : bool do_release_defs = true;
7566 : 14456469 : gimple *stmt = to_remove.pop ();
7567 : :
7568 : : /* When we are value-numbering a region we do not require exit PHIs to
7569 : : be present so we have to make sure to deal with uses outside of the
7570 : : region of stmts that we thought are eliminated.
7571 : : ??? Note we may be confused by uses in dead regions we didn't run
7572 : : elimination on. Rather than checking individual uses we accept
7573 : : dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7574 : : contains such example). */
7575 : 14456469 : if (region_p)
7576 : : {
7577 : 1612530 : if (gphi *phi = dyn_cast <gphi *> (stmt))
7578 : : {
7579 : 1063835 : tree lhs = gimple_phi_result (phi);
7580 : 1063835 : if (!has_zero_uses (lhs))
7581 : : {
7582 : 20854 : if (dump_file && (dump_flags & TDF_DETAILS))
7583 : 3 : fprintf (dump_file, "Keeping eliminated stmt live "
7584 : : "as copy because of out-of-region uses\n");
7585 : 20854 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7586 : 20854 : gimple *copy = gimple_build_assign (lhs, sprime);
7587 : 20854 : gimple_stmt_iterator gsi
7588 : 20854 : = gsi_after_labels (gimple_bb (stmt));
7589 : 20854 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7590 : 20854 : do_release_defs = false;
7591 : : }
7592 : : }
7593 : 548695 : else if (tree lhs = gimple_get_lhs (stmt))
7594 : 548695 : if (TREE_CODE (lhs) == SSA_NAME
7595 : 548695 : && !has_zero_uses (lhs))
7596 : : {
7597 : 932 : if (dump_file && (dump_flags & TDF_DETAILS))
7598 : 0 : fprintf (dump_file, "Keeping eliminated stmt live "
7599 : : "as copy because of out-of-region uses\n");
7600 : 932 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7601 : 932 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7602 : 932 : if (is_gimple_assign (stmt))
7603 : : {
7604 : 932 : gimple_assign_set_rhs_from_tree (&gsi, sprime);
7605 : 932 : stmt = gsi_stmt (gsi);
7606 : 932 : update_stmt (stmt);
7607 : 932 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
7608 : 0 : bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
7609 : 932 : continue;
7610 : : }
7611 : : else
7612 : : {
7613 : 0 : gimple *copy = gimple_build_assign (lhs, sprime);
7614 : 0 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7615 : 0 : do_release_defs = false;
7616 : : }
7617 : : }
7618 : : }
7619 : :
7620 : 14455537 : if (dump_file && (dump_flags & TDF_DETAILS))
7621 : : {
7622 : 20327 : fprintf (dump_file, "Removing dead stmt ");
7623 : 20327 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
7624 : : }
7625 : :
7626 : 14455537 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7627 : 14455537 : if (gimple_code (stmt) == GIMPLE_PHI)
7628 : 1763456 : remove_phi_node (&gsi, do_release_defs);
7629 : : else
7630 : : {
7631 : 12692081 : basic_block bb = gimple_bb (stmt);
7632 : 12692081 : unlink_stmt_vdef (stmt);
7633 : 12692081 : if (gsi_remove (&gsi, true))
7634 : 26473 : bitmap_set_bit (need_eh_cleanup, bb->index);
7635 : 12692081 : if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
7636 : 2 : bitmap_set_bit (need_ab_cleanup, bb->index);
7637 : 12692081 : if (do_release_defs)
7638 : 12692081 : release_defs (stmt);
7639 : : }
7640 : :
7641 : : /* Removing a stmt may expose a forwarder block. */
7642 : 14455537 : el_todo |= TODO_cleanup_cfg;
7643 : : }
7644 : :
7645 : : /* Fixup stmts that became noreturn calls. This may require splitting
7646 : : blocks and thus isn't possible during the dominator walk. Do this
7647 : : in reverse order so we don't inadvertedly remove a stmt we want to
7648 : : fixup by visiting a dominating now noreturn call first. */
7649 : 6046607 : while (!to_fixup.is_empty ())
7650 : : {
7651 : 56 : gimple *stmt = to_fixup.pop ();
7652 : :
7653 : 56 : if (dump_file && (dump_flags & TDF_DETAILS))
7654 : : {
7655 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
7656 : 0 : print_gimple_stmt (dump_file, stmt, 0);
7657 : : }
7658 : :
7659 : 56 : if (fixup_noreturn_call (stmt))
7660 : 56 : el_todo |= TODO_cleanup_cfg;
7661 : : }
7662 : :
7663 : 6046551 : bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
7664 : 6046551 : bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
7665 : :
7666 : 6046551 : if (do_eh_cleanup)
7667 : 10655 : gimple_purge_all_dead_eh_edges (need_eh_cleanup);
7668 : :
7669 : 6046551 : if (do_ab_cleanup)
7670 : 2 : gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
7671 : :
7672 : 6046551 : if (do_eh_cleanup || do_ab_cleanup)
7673 : 10657 : el_todo |= TODO_cleanup_cfg;
7674 : :
7675 : 6046551 : return el_todo;
7676 : : }
7677 : :
7678 : : /* Eliminate fully redundant computations. */
7679 : :
7680 : : unsigned
7681 : 4236870 : eliminate_with_rpo_vn (bitmap inserted_exprs)
7682 : : {
7683 : 4236870 : eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
7684 : :
7685 : 4236870 : eliminate_dom_walker *saved_rpo_avail = rpo_avail;
7686 : 4236870 : rpo_avail = &walker;
7687 : 4236870 : walker.walk (cfun->cfg->x_entry_block_ptr);
7688 : 4236870 : rpo_avail = saved_rpo_avail;
7689 : :
7690 : 4236870 : return walker.eliminate_cleanup ();
7691 : 4236870 : }
7692 : :
7693 : : static unsigned
7694 : : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
7695 : : bool iterate, bool eliminate, bool skip_entry_phis,
7696 : : vn_lookup_kind kind);
7697 : :
7698 : : void
7699 : 948158 : run_rpo_vn (vn_lookup_kind kind)
7700 : : {
7701 : 948158 : do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
7702 : :
7703 : : /* ??? Prune requirement of these. */
7704 : 948158 : constant_to_value_id = new hash_table<vn_constant_hasher> (23);
7705 : :
7706 : : /* Initialize the value ids and prune out remaining VN_TOPs
7707 : : from dead code. */
7708 : 948158 : tree name;
7709 : 948158 : unsigned i;
7710 : 46977188 : FOR_EACH_SSA_NAME (i, name, cfun)
7711 : : {
7712 : 34069151 : vn_ssa_aux_t info = VN_INFO (name);
7713 : 34069151 : if (!info->visited
7714 : 33998544 : || info->valnum == VN_TOP)
7715 : 70607 : info->valnum = name;
7716 : 34069151 : if (info->valnum == name)
7717 : 32858637 : info->value_id = get_next_value_id ();
7718 : 1210514 : else if (is_gimple_min_invariant (info->valnum))
7719 : 45443 : info->value_id = get_or_alloc_constant_value_id (info->valnum);
7720 : : }
7721 : :
7722 : : /* Propagate. */
7723 : 46977188 : FOR_EACH_SSA_NAME (i, name, cfun)
7724 : : {
7725 : 34069151 : vn_ssa_aux_t info = VN_INFO (name);
7726 : 34069151 : if (TREE_CODE (info->valnum) == SSA_NAME
7727 : 34023708 : && info->valnum != name
7728 : 35234222 : && info->value_id != VN_INFO (info->valnum)->value_id)
7729 : 1165071 : info->value_id = VN_INFO (info->valnum)->value_id;
7730 : : }
7731 : :
7732 : 948158 : set_hashtable_value_ids ();
7733 : :
7734 : 948158 : if (dump_file && (dump_flags & TDF_DETAILS))
7735 : : {
7736 : 14 : fprintf (dump_file, "Value numbers:\n");
7737 : 407 : FOR_EACH_SSA_NAME (i, name, cfun)
7738 : : {
7739 : 308 : if (VN_INFO (name)->visited
7740 : 308 : && SSA_VAL (name) != name)
7741 : : {
7742 : 33 : print_generic_expr (dump_file, name);
7743 : 33 : fprintf (dump_file, " = ");
7744 : 33 : print_generic_expr (dump_file, SSA_VAL (name));
7745 : 33 : fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
7746 : : }
7747 : : }
7748 : : }
7749 : 948158 : }
7750 : :
7751 : : /* Free VN associated data structures. */
7752 : :
7753 : : void
7754 : 6065833 : free_rpo_vn (void)
7755 : : {
7756 : 6065833 : free_vn_table (valid_info);
7757 : 6065833 : XDELETE (valid_info);
7758 : 6065833 : obstack_free (&vn_tables_obstack, NULL);
7759 : 6065833 : obstack_free (&vn_tables_insert_obstack, NULL);
7760 : :
7761 : 6065833 : vn_ssa_aux_iterator_type it;
7762 : 6065833 : vn_ssa_aux_t info;
7763 : 350242593 : FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
7764 : 172088380 : if (info->needs_insertion)
7765 : 4119817 : release_ssa_name (info->name);
7766 : 6065833 : obstack_free (&vn_ssa_aux_obstack, NULL);
7767 : 6065833 : delete vn_ssa_aux_hash;
7768 : :
7769 : 6065833 : delete constant_to_value_id;
7770 : 6065833 : constant_to_value_id = NULL;
7771 : 6065833 : }
7772 : :
7773 : : /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7774 : :
7775 : : static tree
7776 : 23010658 : vn_lookup_simplify_result (gimple_match_op *res_op)
7777 : : {
7778 : 23010658 : if (!res_op->code.is_tree_code ())
7779 : : return NULL_TREE;
7780 : 23007189 : tree *ops = res_op->ops;
7781 : 23007189 : unsigned int length = res_op->num_ops;
7782 : 23007189 : if (res_op->code == CONSTRUCTOR
7783 : : /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7784 : : and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7785 : 23007189 : && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
7786 : : {
7787 : 1178 : length = CONSTRUCTOR_NELTS (res_op->ops[0]);
7788 : 1178 : ops = XALLOCAVEC (tree, length);
7789 : 5808 : for (unsigned i = 0; i < length; ++i)
7790 : 4630 : ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
7791 : : }
7792 : 23007189 : vn_nary_op_t vnresult = NULL;
7793 : 23007189 : tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
7794 : : res_op->type, ops, &vnresult);
7795 : : /* If this is used from expression simplification make sure to
7796 : : return an available expression. */
7797 : 23007189 : if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
7798 : 2140714 : res = rpo_avail->eliminate_avail (vn_context_bb, res);
7799 : : return res;
7800 : : }
7801 : :
7802 : : /* Return a leader for OPs value that is valid at BB. */
7803 : :
7804 : : tree
7805 : 258327426 : rpo_elim::eliminate_avail (basic_block bb, tree op)
7806 : : {
7807 : 258327426 : bool visited;
7808 : 258327426 : tree valnum = SSA_VAL (op, &visited);
7809 : : /* If we didn't visit OP then it must be defined outside of the
7810 : : region we process and also dominate it. So it is available. */
7811 : 258327426 : if (!visited)
7812 : : return op;
7813 : 256224300 : if (TREE_CODE (valnum) == SSA_NAME)
7814 : : {
7815 : 242562484 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
7816 : : return valnum;
7817 : 235915076 : vn_ssa_aux_t valnum_info = VN_INFO (valnum);
7818 : 235915076 : vn_avail *av = valnum_info->avail;
7819 : 235915076 : if (!av)
7820 : : {
7821 : : /* See above. But when there's availability info prefer
7822 : : what we recorded there for example to preserve LC SSA. */
7823 : 82696770 : if (!valnum_info->visited)
7824 : : return valnum;
7825 : : return NULL_TREE;
7826 : : }
7827 : 153218306 : if (av->location == bb->index)
7828 : : /* On tramp3d 90% of the cases are here. */
7829 : 102632546 : return ssa_name (av->leader);
7830 : 64482229 : do
7831 : : {
7832 : 64482229 : basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
7833 : : /* ??? During elimination we have to use availability at the
7834 : : definition site of a use we try to replace. This
7835 : : is required to not run into inconsistencies because
7836 : : of dominated_by_p_w_unex behavior and removing a definition
7837 : : while not replacing all uses.
7838 : : ??? We could try to consistently walk dominators
7839 : : ignoring non-executable regions. The nearest common
7840 : : dominator of bb and abb is where we can stop walking. We
7841 : : may also be able to "pre-compute" (bits of) the next immediate
7842 : : (non-)dominator during the RPO walk when marking edges as
7843 : : executable. */
7844 : 64482229 : if (dominated_by_p_w_unex (bb, abb, true))
7845 : : {
7846 : 46676614 : tree leader = ssa_name (av->leader);
7847 : : /* Prevent eliminations that break loop-closed SSA. */
7848 : 46676614 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
7849 : 2923903 : && ! SSA_NAME_IS_DEFAULT_DEF (leader)
7850 : 49600517 : && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7851 : 2923903 : (leader))->loop_father,
7852 : : bb))
7853 : : return NULL_TREE;
7854 : 46576107 : if (dump_file && (dump_flags & TDF_DETAILS))
7855 : : {
7856 : 3363 : print_generic_expr (dump_file, leader);
7857 : 3363 : fprintf (dump_file, " is available for ");
7858 : 3363 : print_generic_expr (dump_file, valnum);
7859 : 3363 : fprintf (dump_file, "\n");
7860 : : }
7861 : : /* On tramp3d 99% of the _remaining_ cases succeed at
7862 : : the first enty. */
7863 : 46576107 : return leader;
7864 : : }
7865 : : /* ??? Can we somehow skip to the immediate dominator
7866 : : RPO index (bb_to_rpo)? Again, maybe not worth, on
7867 : : tramp3d the worst number of elements in the vector is 9. */
7868 : 17805615 : av = av->next;
7869 : : }
7870 : 17805615 : while (av);
7871 : : /* While we prefer avail we have to fallback to using the value
7872 : : directly if defined outside of the region when none of the
7873 : : available defs suit. */
7874 : 3909146 : if (!valnum_info->visited)
7875 : : return valnum;
7876 : : }
7877 : 13661816 : else if (valnum != VN_TOP)
7878 : : /* valnum is is_gimple_min_invariant. */
7879 : : return valnum;
7880 : : return NULL_TREE;
7881 : : }
7882 : :
7883 : : /* Make LEADER a leader for its value at BB. */
7884 : :
7885 : : void
7886 : 96363388 : rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
7887 : : {
7888 : 96363388 : tree valnum = VN_INFO (leader)->valnum;
7889 : 96363388 : if (valnum == VN_TOP
7890 : 96363388 : || is_gimple_min_invariant (valnum))
7891 : 0 : return;
7892 : 96363388 : if (dump_file && (dump_flags & TDF_DETAILS))
7893 : : {
7894 : 290507 : fprintf (dump_file, "Making available beyond BB%d ", bb->index);
7895 : 290507 : print_generic_expr (dump_file, leader);
7896 : 290507 : fprintf (dump_file, " for value ");
7897 : 290507 : print_generic_expr (dump_file, valnum);
7898 : 290507 : fprintf (dump_file, "\n");
7899 : : }
7900 : 96363388 : vn_ssa_aux_t value = VN_INFO (valnum);
7901 : 96363388 : vn_avail *av;
7902 : 96363388 : if (m_avail_freelist)
7903 : : {
7904 : 18839939 : av = m_avail_freelist;
7905 : 18839939 : m_avail_freelist = m_avail_freelist->next;
7906 : : }
7907 : : else
7908 : 77523449 : av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
7909 : 96363388 : av->location = bb->index;
7910 : 96363388 : av->leader = SSA_NAME_VERSION (leader);
7911 : 96363388 : av->next = value->avail;
7912 : 96363388 : av->next_undo = last_pushed_avail;
7913 : 96363388 : last_pushed_avail = value;
7914 : 96363388 : value->avail = av;
7915 : : }
7916 : :
7917 : : /* Valueization hook for RPO VN plus required state. */
7918 : :
7919 : : tree
7920 : 1872436129 : rpo_vn_valueize (tree name)
7921 : : {
7922 : 1872436129 : if (TREE_CODE (name) == SSA_NAME)
7923 : : {
7924 : 1825431059 : vn_ssa_aux_t val = VN_INFO (name);
7925 : 1825431059 : if (val)
7926 : : {
7927 : 1825431059 : tree tem = val->valnum;
7928 : 1825431059 : if (tem != VN_TOP && tem != name)
7929 : : {
7930 : 102238056 : if (TREE_CODE (tem) != SSA_NAME)
7931 : : return tem;
7932 : : /* For all values we only valueize to an available leader
7933 : : which means we can use SSA name info without restriction. */
7934 : 85869671 : tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
7935 : 85869671 : if (tem)
7936 : : return tem;
7937 : : }
7938 : : }
7939 : : }
7940 : : return name;
7941 : : }
7942 : :
7943 : : /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7944 : : inverted condition. */
7945 : :
7946 : : static void
7947 : 27362685 : insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
7948 : : {
7949 : 27362685 : switch (code)
7950 : : {
7951 : 1343106 : case LT_EXPR:
7952 : : /* a < b -> a {!,<}= b */
7953 : 1343106 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7954 : : ops, boolean_true_node, 0, pred_e);
7955 : 1343106 : vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
7956 : : ops, boolean_true_node, 0, pred_e);
7957 : : /* a < b -> ! a {>,=} b */
7958 : 1343106 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7959 : : ops, boolean_false_node, 0, pred_e);
7960 : 1343106 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7961 : : ops, boolean_false_node, 0, pred_e);
7962 : 1343106 : break;
7963 : 3382735 : case GT_EXPR:
7964 : : /* a > b -> a {!,>}= b */
7965 : 3382735 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7966 : : ops, boolean_true_node, 0, pred_e);
7967 : 3382735 : vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
7968 : : ops, boolean_true_node, 0, pred_e);
7969 : : /* a > b -> ! a {<,=} b */
7970 : 3382735 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7971 : : ops, boolean_false_node, 0, pred_e);
7972 : 3382735 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7973 : : ops, boolean_false_node, 0, pred_e);
7974 : 3382735 : break;
7975 : 9432085 : case EQ_EXPR:
7976 : : /* a == b -> ! a {<,>} b */
7977 : 9432085 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7978 : : ops, boolean_false_node, 0, pred_e);
7979 : 9432085 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7980 : : ops, boolean_false_node, 0, pred_e);
7981 : 9432085 : break;
7982 : : case LE_EXPR:
7983 : : case GE_EXPR:
7984 : : case NE_EXPR:
7985 : : /* Nothing besides inverted condition. */
7986 : : break;
7987 : 27362685 : default:;
7988 : : }
7989 : 27362685 : }
7990 : :
7991 : : /* Insert on the TRUE_E true and FALSE_E false predicates
7992 : : derived from LHS CODE RHS. */
7993 : :
7994 : : static void
7995 : 23107361 : insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
7996 : : edge true_e, edge false_e)
7997 : : {
7998 : : /* If both edges are null, then there is nothing to be done. */
7999 : 23107361 : if (!true_e && !false_e)
8000 : 1283370 : return;
8001 : :
8002 : : /* Canonicalize the comparison if needed, putting
8003 : : the constant in the rhs. */
8004 : 21827377 : if (tree_swap_operands_p (lhs, rhs))
8005 : : {
8006 : 14616 : std::swap (lhs, rhs);
8007 : 14616 : code = swap_tree_comparison (code);
8008 : : }
8009 : :
8010 : : /* If the lhs is not a ssa name, don't record anything. */
8011 : 21827377 : if (TREE_CODE (lhs) != SSA_NAME)
8012 : : return;
8013 : :
8014 : 21823991 : tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs));
8015 : 21823991 : tree ops[2];
8016 : 21823991 : ops[0] = lhs;
8017 : 21823991 : ops[1] = rhs;
8018 : 21823991 : if (true_e)
8019 : 17758398 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8020 : : boolean_true_node, 0, true_e);
8021 : 21823991 : if (false_e)
8022 : 16934932 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8023 : : boolean_false_node, 0, false_e);
8024 : 21823991 : if (icode != ERROR_MARK)
8025 : : {
8026 : 21573170 : if (true_e)
8027 : 17605888 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8028 : : boolean_false_node, 0, true_e);
8029 : 21573170 : if (false_e)
8030 : 16729619 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8031 : : boolean_true_node, 0, false_e);
8032 : : }
8033 : : /* Relax for non-integers, inverted condition handled
8034 : : above. */
8035 : 21823991 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8036 : : {
8037 : 17179371 : if (true_e)
8038 : 14132751 : insert_related_predicates_on_edge (code, ops, true_e);
8039 : 17179371 : if (false_e)
8040 : 13229934 : insert_related_predicates_on_edge (icode, ops, false_e);
8041 : : }
8042 : 21823991 : if (integer_zerop (rhs)
8043 : 21823991 : && (code == NE_EXPR || code == EQ_EXPR))
8044 : : {
8045 : 9177388 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
8046 : : /* (A CMP B) != 0 is the same as (A CMP B).
8047 : : (A CMP B) == 0 is just (A CMP B) with the edges swapped. */
8048 : 9177388 : if (is_gimple_assign (def_stmt)
8049 : 9177388 : && TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_comparison)
8050 : : {
8051 : 472372 : tree_code nc = gimple_assign_rhs_code (def_stmt);
8052 : 472372 : tree nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8053 : 472372 : tree nrhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8054 : 472372 : edge nt = true_e;
8055 : 472372 : edge nf = false_e;
8056 : 472372 : if (code == EQ_EXPR)
8057 : 341173 : std::swap (nt, nf);
8058 : 472372 : if (lhs != nlhs)
8059 : 472372 : insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
8060 : : }
8061 : : /* (a | b) == 0 ->
8062 : : on true edge assert: a == 0 & b == 0. */
8063 : : /* (a | b) != 0 ->
8064 : : on false edge assert: a == 0 & b == 0. */
8065 : 9177388 : if (is_gimple_assign (def_stmt)
8066 : 9177388 : && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
8067 : : {
8068 : 277891 : edge e = code == EQ_EXPR ? true_e : false_e;
8069 : 277891 : tree nlhs;
8070 : :
8071 : 277891 : nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8072 : : /* A valueization of the `a` might return the old lhs
8073 : : which is already handled above. */
8074 : 277891 : if (nlhs != lhs)
8075 : 277891 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8076 : :
8077 : : /* A valueization of the `b` might return the old lhs
8078 : : which is already handled above. */
8079 : 277891 : nlhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8080 : 277891 : if (nlhs != lhs)
8081 : 277891 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8082 : : }
8083 : : }
8084 : : }
8085 : :
8086 : : /* Main stmt worker for RPO VN, process BB. */
8087 : :
8088 : : static unsigned
8089 : 61843601 : process_bb (rpo_elim &avail, basic_block bb,
8090 : : bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
8091 : : bool do_region, bitmap exit_bbs, bool skip_phis)
8092 : : {
8093 : 61843601 : unsigned todo = 0;
8094 : 61843601 : edge_iterator ei;
8095 : 61843601 : edge e;
8096 : :
8097 : 61843601 : vn_context_bb = bb;
8098 : :
8099 : : /* If we are in loop-closed SSA preserve this state. This is
8100 : : relevant when called on regions from outside of FRE/PRE. */
8101 : 61843601 : bool lc_phi_nodes = false;
8102 : 61843601 : if (!skip_phis
8103 : 61843601 : && loops_state_satisfies_p (LOOP_CLOSED_SSA))
8104 : 3514379 : FOR_EACH_EDGE (e, ei, bb->preds)
8105 : 2134965 : if (e->src->loop_father != e->dest->loop_father
8106 : 2134965 : && flow_loop_nested_p (e->dest->loop_father,
8107 : : e->src->loop_father))
8108 : : {
8109 : : lc_phi_nodes = true;
8110 : : break;
8111 : : }
8112 : :
8113 : : /* When we visit a loop header substitute into loop info. */
8114 : 61843601 : if (!iterate && eliminate && bb->loop_father->header == bb)
8115 : : {
8116 : : /* Keep fields in sync with substitute_in_loop_info. */
8117 : 923426 : if (bb->loop_father->nb_iterations)
8118 : 143749 : bb->loop_father->nb_iterations
8119 : 143749 : = simplify_replace_tree (bb->loop_father->nb_iterations,
8120 : : NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
8121 : : }
8122 : :
8123 : : /* Value-number all defs in the basic-block. */
8124 : 61843601 : if (!skip_phis)
8125 : 89283973 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8126 : 27466581 : gsi_next (&gsi))
8127 : : {
8128 : 27466581 : gphi *phi = gsi.phi ();
8129 : 27466581 : tree res = PHI_RESULT (phi);
8130 : 27466581 : vn_ssa_aux_t res_info = VN_INFO (res);
8131 : 27466581 : if (!bb_visited)
8132 : : {
8133 : 19546636 : gcc_assert (!res_info->visited);
8134 : 19546636 : res_info->valnum = VN_TOP;
8135 : 19546636 : res_info->visited = true;
8136 : : }
8137 : :
8138 : : /* When not iterating force backedge values to varying. */
8139 : 27466581 : visit_stmt (phi, !iterate_phis);
8140 : 54933162 : if (virtual_operand_p (res))
8141 : 11042257 : continue;
8142 : :
8143 : : /* Eliminate */
8144 : : /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
8145 : : how we handle backedges and availability.
8146 : : And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
8147 : 16424324 : tree val = res_info->valnum;
8148 : 16424324 : if (res != val && !iterate && eliminate)
8149 : : {
8150 : 1388862 : if (tree leader = avail.eliminate_avail (bb, res))
8151 : : {
8152 : 1245614 : if (leader != res
8153 : : /* Preserve loop-closed SSA form. */
8154 : 1245614 : && (! lc_phi_nodes
8155 : 3229 : || is_gimple_min_invariant (leader)))
8156 : : {
8157 : 1244891 : if (dump_file && (dump_flags & TDF_DETAILS))
8158 : : {
8159 : 197 : fprintf (dump_file, "Replaced redundant PHI node "
8160 : : "defining ");
8161 : 197 : print_generic_expr (dump_file, res);
8162 : 197 : fprintf (dump_file, " with ");
8163 : 197 : print_generic_expr (dump_file, leader);
8164 : 197 : fprintf (dump_file, "\n");
8165 : : }
8166 : 1244891 : avail.eliminations++;
8167 : :
8168 : 1244891 : if (may_propagate_copy (res, leader))
8169 : : {
8170 : : /* Schedule for removal. */
8171 : 1244891 : avail.to_remove.safe_push (phi);
8172 : 1244891 : continue;
8173 : : }
8174 : : /* ??? Else generate a copy stmt. */
8175 : : }
8176 : : }
8177 : : }
8178 : : /* Only make defs available that not already are. But make
8179 : : sure loop-closed SSA PHI node defs are picked up for
8180 : : downstream uses. */
8181 : 15179433 : if (lc_phi_nodes
8182 : 15179433 : || res == val
8183 : 15179433 : || ! avail.eliminate_avail (bb, res))
8184 : 11579304 : avail.eliminate_push_avail (bb, res);
8185 : : }
8186 : :
8187 : : /* For empty BBs mark outgoing edges executable. For non-empty BBs
8188 : : we do this when processing the last stmt as we have to do this
8189 : : before elimination which otherwise forces GIMPLE_CONDs to
8190 : : if (1 != 0) style when seeing non-executable edges. */
8191 : 123687202 : if (gsi_end_p (gsi_start_bb (bb)))
8192 : : {
8193 : 14761450 : FOR_EACH_EDGE (e, ei, bb->succs)
8194 : : {
8195 : 7380725 : if (!(e->flags & EDGE_EXECUTABLE))
8196 : : {
8197 : 5122614 : if (dump_file && (dump_flags & TDF_DETAILS))
8198 : 5635 : fprintf (dump_file,
8199 : : "marking outgoing edge %d -> %d executable\n",
8200 : 5635 : e->src->index, e->dest->index);
8201 : 5122614 : e->flags |= EDGE_EXECUTABLE;
8202 : 5122614 : e->dest->flags |= BB_EXECUTABLE;
8203 : : }
8204 : 2258111 : else if (!(e->dest->flags & BB_EXECUTABLE))
8205 : : {
8206 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8207 : 0 : fprintf (dump_file,
8208 : : "marking destination block %d reachable\n",
8209 : : e->dest->index);
8210 : 0 : e->dest->flags |= BB_EXECUTABLE;
8211 : : }
8212 : : }
8213 : : }
8214 : 123687202 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8215 : 481861031 : !gsi_end_p (gsi); gsi_next (&gsi))
8216 : : {
8217 : 420017430 : ssa_op_iter i;
8218 : 420017430 : tree op;
8219 : 420017430 : if (!bb_visited)
8220 : : {
8221 : 481881255 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8222 : : {
8223 : 137007353 : vn_ssa_aux_t op_info = VN_INFO (op);
8224 : 137007353 : gcc_assert (!op_info->visited);
8225 : 137007353 : op_info->valnum = VN_TOP;
8226 : 137007353 : op_info->visited = true;
8227 : : }
8228 : :
8229 : : /* We somehow have to deal with uses that are not defined
8230 : : in the processed region. Forcing unvisited uses to
8231 : : varying here doesn't play well with def-use following during
8232 : : expression simplification, so we deal with this by checking
8233 : : the visited flag in SSA_VAL. */
8234 : : }
8235 : :
8236 : 420017430 : visit_stmt (gsi_stmt (gsi));
8237 : :
8238 : 420017430 : gimple *last = gsi_stmt (gsi);
8239 : 420017430 : e = NULL;
8240 : 420017430 : switch (gimple_code (last))
8241 : : {
8242 : 122133 : case GIMPLE_SWITCH:
8243 : 122133 : e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8244 : 122133 : (as_a <gswitch *> (last))));
8245 : 122133 : break;
8246 : 24165055 : case GIMPLE_COND:
8247 : 24165055 : {
8248 : 24165055 : tree lhs = vn_valueize (gimple_cond_lhs (last));
8249 : 24165055 : tree rhs = vn_valueize (gimple_cond_rhs (last));
8250 : 24165055 : tree_code cmpcode = gimple_cond_code (last);
8251 : : /* Canonicalize the comparison if needed, putting
8252 : : the constant in the rhs. */
8253 : 24165055 : if (tree_swap_operands_p (lhs, rhs))
8254 : : {
8255 : 807194 : std::swap (lhs, rhs);
8256 : 807194 : cmpcode = swap_tree_comparison (cmpcode);
8257 : : }
8258 : 24165055 : tree val = gimple_simplify (cmpcode,
8259 : : boolean_type_node, lhs, rhs,
8260 : : NULL, vn_valueize);
8261 : : /* If the condition didn't simplfy see if we have recorded
8262 : : an expression from sofar taken edges. */
8263 : 24165055 : if (! val || TREE_CODE (val) != INTEGER_CST)
8264 : : {
8265 : 22381694 : vn_nary_op_t vnresult;
8266 : 22381694 : tree ops[2];
8267 : 22381694 : ops[0] = lhs;
8268 : 22381694 : ops[1] = rhs;
8269 : 22381694 : val = vn_nary_op_lookup_pieces (2, cmpcode,
8270 : : boolean_type_node, ops,
8271 : : &vnresult);
8272 : : /* Got back a ssa name, then try looking up `val != 0`
8273 : : as it might have been recorded that way. */
8274 : 22381694 : if (val && TREE_CODE (val) == SSA_NAME)
8275 : : {
8276 : 155051 : ops[0] = val;
8277 : 155051 : ops[1] = build_zero_cst (TREE_TYPE (val));
8278 : 155051 : val = vn_nary_op_lookup_pieces (2, NE_EXPR,
8279 : : boolean_type_node, ops,
8280 : : &vnresult);
8281 : : }
8282 : : /* Did we get a predicated value? */
8283 : 22381679 : if (! val && vnresult && vnresult->predicated_values)
8284 : : {
8285 : 1256664 : val = vn_nary_op_get_predicated_value (vnresult, bb);
8286 : 1256664 : if (val && dump_file && (dump_flags & TDF_DETAILS))
8287 : : {
8288 : 2 : fprintf (dump_file, "Got predicated value ");
8289 : 2 : print_generic_expr (dump_file, val, TDF_NONE);
8290 : 2 : fprintf (dump_file, " for ");
8291 : 2 : print_gimple_stmt (dump_file, last, TDF_SLIM);
8292 : : }
8293 : : }
8294 : : }
8295 : 22381694 : if (val)
8296 : 2085848 : e = find_taken_edge (bb, val);
8297 : 24165055 : if (! e)
8298 : : {
8299 : : /* If we didn't manage to compute the taken edge then
8300 : : push predicated expressions for the condition itself
8301 : : and related conditions to the hashtables. This allows
8302 : : simplification of redundant conditions which is
8303 : : important as early cleanup. */
8304 : 22079207 : edge true_e, false_e;
8305 : 22079207 : extract_true_false_edges_from_block (bb, &true_e, &false_e);
8306 : 506976 : if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8307 : 22286791 : || !can_track_predicate_on_edge (true_e))
8308 : 4878854 : true_e = NULL;
8309 : 506976 : if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8310 : 22265360 : || !can_track_predicate_on_edge (false_e))
8311 : 5578382 : false_e = NULL;
8312 : 22079207 : insert_predicates_for_cond (cmpcode, lhs, rhs, true_e, false_e);
8313 : : }
8314 : : break;
8315 : : }
8316 : 1359 : case GIMPLE_GOTO:
8317 : 1359 : e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8318 : 1359 : break;
8319 : : default:
8320 : : e = NULL;
8321 : : }
8322 : 420017430 : if (e)
8323 : : {
8324 : 2089575 : todo = TODO_cleanup_cfg;
8325 : 2089575 : if (!(e->flags & EDGE_EXECUTABLE))
8326 : : {
8327 : 1647998 : if (dump_file && (dump_flags & TDF_DETAILS))
8328 : 35 : fprintf (dump_file,
8329 : : "marking known outgoing %sedge %d -> %d executable\n",
8330 : 35 : e->flags & EDGE_DFS_BACK ? "back-" : "",
8331 : 35 : e->src->index, e->dest->index);
8332 : 1647998 : e->flags |= EDGE_EXECUTABLE;
8333 : 1647998 : e->dest->flags |= BB_EXECUTABLE;
8334 : : }
8335 : 441577 : else if (!(e->dest->flags & BB_EXECUTABLE))
8336 : : {
8337 : 11747 : if (dump_file && (dump_flags & TDF_DETAILS))
8338 : 0 : fprintf (dump_file,
8339 : : "marking destination block %d reachable\n",
8340 : : e->dest->index);
8341 : 11747 : e->dest->flags |= BB_EXECUTABLE;
8342 : : }
8343 : : }
8344 : 835855710 : else if (gsi_one_before_end_p (gsi))
8345 : : {
8346 : 128635393 : FOR_EACH_EDGE (e, ei, bb->succs)
8347 : : {
8348 : 76262092 : if (!(e->flags & EDGE_EXECUTABLE))
8349 : : {
8350 : 56002663 : if (dump_file && (dump_flags & TDF_DETAILS))
8351 : 16361 : fprintf (dump_file,
8352 : : "marking outgoing edge %d -> %d executable\n",
8353 : 16361 : e->src->index, e->dest->index);
8354 : 56002663 : e->flags |= EDGE_EXECUTABLE;
8355 : 56002663 : e->dest->flags |= BB_EXECUTABLE;
8356 : : }
8357 : 20259429 : else if (!(e->dest->flags & BB_EXECUTABLE))
8358 : : {
8359 : 2508918 : if (dump_file && (dump_flags & TDF_DETAILS))
8360 : 5486 : fprintf (dump_file,
8361 : : "marking destination block %d reachable\n",
8362 : : e->dest->index);
8363 : 2508918 : e->dest->flags |= BB_EXECUTABLE;
8364 : : }
8365 : : }
8366 : : }
8367 : :
8368 : : /* Eliminate. That also pushes to avail. */
8369 : 420017430 : if (eliminate && ! iterate)
8370 : 102625848 : avail.eliminate_stmt (bb, &gsi);
8371 : : else
8372 : : /* If not eliminating, make all not already available defs
8373 : : available. But avoid picking up dead defs. */
8374 : 396920759 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8375 : 79529177 : if (! has_zero_uses (op)
8376 : 79529177 : && ! avail.eliminate_avail (bb, op))
8377 : 60939067 : avail.eliminate_push_avail (bb, op);
8378 : : }
8379 : :
8380 : : /* Eliminate in destination PHI arguments. Always substitute in dest
8381 : : PHIs, even for non-executable edges. This handles region
8382 : : exits PHIs. */
8383 : 61843601 : if (!iterate && eliminate)
8384 : 33175844 : FOR_EACH_EDGE (e, ei, bb->succs)
8385 : 19708950 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
8386 : 37662254 : !gsi_end_p (gsi); gsi_next (&gsi))
8387 : : {
8388 : 17953304 : gphi *phi = gsi.phi ();
8389 : 17953304 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8390 : 17953304 : tree arg = USE_FROM_PTR (use_p);
8391 : 27445662 : if (TREE_CODE (arg) != SSA_NAME
8392 : 17953304 : || virtual_operand_p (arg))
8393 : 9492358 : continue;
8394 : 8460946 : tree sprime;
8395 : 8460946 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
8396 : : {
8397 : 113110 : sprime = SSA_VAL (arg);
8398 : 113110 : gcc_assert (TREE_CODE (sprime) != SSA_NAME
8399 : : || SSA_NAME_IS_DEFAULT_DEF (sprime));
8400 : : }
8401 : : else
8402 : : /* Look for sth available at the definition block of the argument.
8403 : : This avoids inconsistencies between availability there which
8404 : : decides if the stmt can be removed and availability at the
8405 : : use site. The SSA property ensures that things available
8406 : : at the definition are also available at uses. */
8407 : 8347836 : sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8408 : : arg);
8409 : 8460946 : if (sprime
8410 : 8460946 : && sprime != arg
8411 : 8460946 : && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8412 : 1488719 : propagate_value (use_p, sprime);
8413 : : }
8414 : :
8415 : 61843601 : vn_context_bb = NULL;
8416 : 61843601 : return todo;
8417 : : }
8418 : :
8419 : : /* Unwind state per basic-block. */
8420 : :
8421 : : struct unwind_state
8422 : : {
8423 : : /* Times this block has been visited. */
8424 : : unsigned visited;
8425 : : /* Whether to handle this as iteration point or whether to treat
8426 : : incoming backedge PHI values as varying. */
8427 : : bool iterate;
8428 : : /* Maximum RPO index this block is reachable from. */
8429 : : int max_rpo;
8430 : : /* Unwind state. */
8431 : : void *ob_top;
8432 : : vn_reference_t ref_top;
8433 : : vn_phi_t phi_top;
8434 : : vn_nary_op_t nary_top;
8435 : : vn_avail *avail_top;
8436 : : };
8437 : :
8438 : : /* Unwind the RPO VN state for iteration. */
8439 : :
8440 : : static void
8441 : 1880137 : do_unwind (unwind_state *to, rpo_elim &avail)
8442 : : {
8443 : 1880137 : gcc_assert (to->iterate);
8444 : 34824685 : for (; last_inserted_nary != to->nary_top;
8445 : 32944548 : last_inserted_nary = last_inserted_nary->next)
8446 : : {
8447 : 32944548 : vn_nary_op_t *slot;
8448 : 32944548 : slot = valid_info->nary->find_slot_with_hash
8449 : 32944548 : (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
8450 : : /* Predication causes the need to restore previous state. */
8451 : 32944548 : if ((*slot)->unwind_to)
8452 : 6614592 : *slot = (*slot)->unwind_to;
8453 : : else
8454 : 26329956 : valid_info->nary->clear_slot (slot);
8455 : : }
8456 : 7458514 : for (; last_inserted_phi != to->phi_top;
8457 : 5578377 : last_inserted_phi = last_inserted_phi->next)
8458 : : {
8459 : 5578377 : vn_phi_t *slot;
8460 : 5578377 : slot = valid_info->phis->find_slot_with_hash
8461 : 5578377 : (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
8462 : 5578377 : valid_info->phis->clear_slot (slot);
8463 : : }
8464 : 15124442 : for (; last_inserted_ref != to->ref_top;
8465 : 13244305 : last_inserted_ref = last_inserted_ref->next)
8466 : : {
8467 : 13244305 : vn_reference_t *slot;
8468 : 13244305 : slot = valid_info->references->find_slot_with_hash
8469 : 13244305 : (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
8470 : 13244305 : (*slot)->operands.release ();
8471 : 13244305 : valid_info->references->clear_slot (slot);
8472 : : }
8473 : 1880137 : obstack_free (&vn_tables_obstack, to->ob_top);
8474 : :
8475 : : /* Prune [rpo_idx, ] from avail. */
8476 : 20720076 : for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
8477 : : {
8478 : 18839939 : vn_ssa_aux_t val = last_pushed_avail;
8479 : 18839939 : vn_avail *av = val->avail;
8480 : 18839939 : val->avail = av->next;
8481 : 18839939 : last_pushed_avail = av->next_undo;
8482 : 18839939 : av->next = avail.m_avail_freelist;
8483 : 18839939 : avail.m_avail_freelist = av;
8484 : : }
8485 : 1880137 : }
8486 : :
8487 : : /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8488 : : If ITERATE is true then treat backedges optimistically as not
8489 : : executed and iterate. If ELIMINATE is true then perform
8490 : : elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8491 : : is true then force PHI nodes in ENTRY->dest to VARYING. */
8492 : :
8493 : : static unsigned
8494 : 6065833 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8495 : : bool iterate, bool eliminate, bool skip_entry_phis,
8496 : : vn_lookup_kind kind)
8497 : : {
8498 : 6065833 : unsigned todo = 0;
8499 : 6065833 : default_vn_walk_kind = kind;
8500 : :
8501 : : /* We currently do not support region-based iteration when
8502 : : elimination is requested. */
8503 : 6065833 : gcc_assert (!entry || !iterate || !eliminate);
8504 : : /* When iterating we need loop info up-to-date. */
8505 : 6065833 : gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
8506 : :
8507 : 6065833 : bool do_region = entry != NULL;
8508 : 6065833 : if (!do_region)
8509 : : {
8510 : 5406903 : entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
8511 : 5406903 : exit_bbs = BITMAP_ALLOC (NULL);
8512 : 5406903 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
8513 : : }
8514 : :
8515 : : /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8516 : : re-mark those that are contained in the region. */
8517 : 6065833 : edge_iterator ei;
8518 : 6065833 : edge e;
8519 : 12186424 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8520 : 6120591 : e->flags &= ~EDGE_DFS_BACK;
8521 : :
8522 : 6065833 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
8523 : 6065833 : auto_vec<std::pair<int, int> > toplevel_scc_extents;
8524 : 6065833 : int n = rev_post_order_and_mark_dfs_back_seme
8525 : 7894796 : (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
8526 : :
8527 : 6065833 : if (!do_region)
8528 : 5406903 : BITMAP_FREE (exit_bbs);
8529 : :
8530 : : /* If there are any non-DFS_BACK edges into entry->dest skip
8531 : : processing PHI nodes for that block. This supports
8532 : : value-numbering loop bodies w/o the actual loop. */
8533 : 12186423 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8534 : 6120591 : if (e != entry
8535 : 54758 : && !(e->flags & EDGE_DFS_BACK))
8536 : : break;
8537 : 6065833 : if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
8538 : 0 : fprintf (dump_file, "Region does not contain all edges into "
8539 : : "the entry block, skipping its PHIs.\n");
8540 : 6065833 : skip_entry_phis |= e != NULL;
8541 : :
8542 : 6065833 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
8543 : 56770879 : for (int i = 0; i < n; ++i)
8544 : 50705046 : bb_to_rpo[rpo[i]] = i;
8545 : :
8546 : 6065833 : unwind_state *rpo_state = XNEWVEC (unwind_state, n);
8547 : :
8548 : 6065833 : rpo_elim avail (entry->dest);
8549 : 6065833 : rpo_avail = &avail;
8550 : :
8551 : : /* Verify we have no extra entries into the region. */
8552 : 6065833 : if (flag_checking && do_region)
8553 : : {
8554 : 658924 : auto_bb_flag bb_in_region (fn);
8555 : 1976711 : for (int i = 0; i < n; ++i)
8556 : : {
8557 : 1317787 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8558 : 1317787 : bb->flags |= bb_in_region;
8559 : : }
8560 : : /* We can't merge the first two loops because we cannot rely
8561 : : on EDGE_DFS_BACK for edges not within the region. But if
8562 : : we decide to always have the bb_in_region flag we can
8563 : : do the checking during the RPO walk itself (but then it's
8564 : : also easy to handle MEME conservatively). */
8565 : 1976711 : for (int i = 0; i < n; ++i)
8566 : : {
8567 : 1317787 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8568 : 1317787 : edge e;
8569 : 1317787 : edge_iterator ei;
8570 : 2874345 : FOR_EACH_EDGE (e, ei, bb->preds)
8571 : 1556558 : gcc_assert (e == entry
8572 : : || (skip_entry_phis && bb == entry->dest)
8573 : : || (e->src->flags & bb_in_region));
8574 : : }
8575 : 1976711 : for (int i = 0; i < n; ++i)
8576 : : {
8577 : 1317787 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8578 : 1317787 : bb->flags &= ~bb_in_region;
8579 : : }
8580 : 658924 : }
8581 : :
8582 : : /* Create the VN state. For the initial size of the various hashtables
8583 : : use a heuristic based on region size and number of SSA names. */
8584 : 6065833 : unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
8585 : 6065833 : / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
8586 : 6065833 : VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
8587 : 6065833 : next_value_id = 1;
8588 : 6065833 : next_constant_value_id = -1;
8589 : :
8590 : 6065833 : vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
8591 : 6065833 : gcc_obstack_init (&vn_ssa_aux_obstack);
8592 : :
8593 : 6065833 : gcc_obstack_init (&vn_tables_obstack);
8594 : 6065833 : gcc_obstack_init (&vn_tables_insert_obstack);
8595 : 6065833 : valid_info = XCNEW (struct vn_tables_s);
8596 : 6065833 : allocate_vn_table (valid_info, region_size);
8597 : 6065833 : last_inserted_ref = NULL;
8598 : 6065833 : last_inserted_phi = NULL;
8599 : 6065833 : last_inserted_nary = NULL;
8600 : 6065833 : last_pushed_avail = NULL;
8601 : :
8602 : 6065833 : vn_valueize = rpo_vn_valueize;
8603 : :
8604 : : /* Initialize the unwind state and edge/BB executable state. */
8605 : 6065833 : unsigned curr_scc = 0;
8606 : 56770879 : for (int i = 0; i < n; ++i)
8607 : : {
8608 : 50705046 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8609 : 50705046 : rpo_state[i].visited = 0;
8610 : 50705046 : rpo_state[i].max_rpo = i;
8611 : 59261802 : if (!iterate && curr_scc < toplevel_scc_extents.length ())
8612 : : {
8613 : 7018741 : if (i >= toplevel_scc_extents[curr_scc].first
8614 : 7018741 : && i <= toplevel_scc_extents[curr_scc].second)
8615 : 3886680 : rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
8616 : 7018741 : if (i == toplevel_scc_extents[curr_scc].second)
8617 : 709944 : curr_scc++;
8618 : : }
8619 : 50705046 : bb->flags &= ~BB_EXECUTABLE;
8620 : 50705046 : bool has_backedges = false;
8621 : 50705046 : edge e;
8622 : 50705046 : edge_iterator ei;
8623 : 120249203 : FOR_EACH_EDGE (e, ei, bb->preds)
8624 : : {
8625 : 69544157 : if (e->flags & EDGE_DFS_BACK)
8626 : 2801121 : has_backedges = true;
8627 : 69544157 : e->flags &= ~EDGE_EXECUTABLE;
8628 : 69544157 : if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
8629 : 69544157 : continue;
8630 : : }
8631 : 50705046 : rpo_state[i].iterate = iterate && has_backedges;
8632 : : }
8633 : 6065833 : entry->flags |= EDGE_EXECUTABLE;
8634 : 6065833 : entry->dest->flags |= BB_EXECUTABLE;
8635 : :
8636 : : /* As heuristic to improve compile-time we handle only the N innermost
8637 : : loops and the outermost one optimistically. */
8638 : 6065833 : if (iterate)
8639 : : {
8640 : 4236870 : unsigned max_depth = param_rpo_vn_max_loop_depth;
8641 : 14233780 : for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
8642 : 1525418 : if (loop_depth (loop) > max_depth)
8643 : 1925 : for (unsigned i = 2;
8644 : 8346 : i < loop_depth (loop) - max_depth; ++i)
8645 : : {
8646 : 1925 : basic_block header = superloop_at_depth (loop, i)->header;
8647 : 1925 : bool non_latch_backedge = false;
8648 : 1925 : edge e;
8649 : 1925 : edge_iterator ei;
8650 : 5806 : FOR_EACH_EDGE (e, ei, header->preds)
8651 : 3881 : if (e->flags & EDGE_DFS_BACK)
8652 : : {
8653 : : /* There can be a non-latch backedge into the header
8654 : : which is part of an outer irreducible region. We
8655 : : cannot avoid iterating this block then. */
8656 : 1956 : if (!dominated_by_p (CDI_DOMINATORS,
8657 : 1956 : e->src, e->dest))
8658 : : {
8659 : 12 : if (dump_file && (dump_flags & TDF_DETAILS))
8660 : 0 : fprintf (dump_file, "non-latch backedge %d -> %d "
8661 : : "forces iteration of loop %d\n",
8662 : 0 : e->src->index, e->dest->index, loop->num);
8663 : : non_latch_backedge = true;
8664 : : }
8665 : : else
8666 : 1944 : e->flags |= EDGE_EXECUTABLE;
8667 : : }
8668 : 1925 : rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
8669 : 4236870 : }
8670 : : }
8671 : :
8672 : 6065833 : uint64_t nblk = 0;
8673 : 6065833 : int idx = 0;
8674 : 4236870 : if (iterate)
8675 : : /* Go and process all blocks, iterating as necessary. */
8676 : 49175567 : do
8677 : : {
8678 : 49175567 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8679 : :
8680 : : /* If the block has incoming backedges remember unwind state. This
8681 : : is required even for non-executable blocks since in irreducible
8682 : : regions we might reach them via the backedge and re-start iterating
8683 : : from there.
8684 : : Note we can individually mark blocks with incoming backedges to
8685 : : not iterate where we then handle PHIs conservatively. We do that
8686 : : heuristically to reduce compile-time for degenerate cases. */
8687 : 49175567 : if (rpo_state[idx].iterate)
8688 : : {
8689 : 4337156 : rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
8690 : 4337156 : rpo_state[idx].ref_top = last_inserted_ref;
8691 : 4337156 : rpo_state[idx].phi_top = last_inserted_phi;
8692 : 4337156 : rpo_state[idx].nary_top = last_inserted_nary;
8693 : 4337156 : rpo_state[idx].avail_top
8694 : 4337156 : = last_pushed_avail ? last_pushed_avail->avail : NULL;
8695 : : }
8696 : :
8697 : 49175567 : if (!(bb->flags & BB_EXECUTABLE))
8698 : : {
8699 : 905983 : if (dump_file && (dump_flags & TDF_DETAILS))
8700 : 2 : fprintf (dump_file, "Block %d: BB%d found not executable\n",
8701 : : idx, bb->index);
8702 : 905983 : idx++;
8703 : 2786120 : continue;
8704 : : }
8705 : :
8706 : 48269584 : if (dump_file && (dump_flags & TDF_DETAILS))
8707 : 336 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8708 : 48269584 : nblk++;
8709 : 96539168 : todo |= process_bb (avail, bb,
8710 : 48269584 : rpo_state[idx].visited != 0,
8711 : : rpo_state[idx].iterate,
8712 : : iterate, eliminate, do_region, exit_bbs, false);
8713 : 48269584 : rpo_state[idx].visited++;
8714 : :
8715 : : /* Verify if changed values flow over executable outgoing backedges
8716 : : and those change destination PHI values (that's the thing we
8717 : : can easily verify). Reduce over all such edges to the farthest
8718 : : away PHI. */
8719 : 48269584 : int iterate_to = -1;
8720 : 48269584 : edge_iterator ei;
8721 : 48269584 : edge e;
8722 : 116266760 : FOR_EACH_EDGE (e, ei, bb->succs)
8723 : 67997176 : if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
8724 : : == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
8725 : 4347850 : && rpo_state[bb_to_rpo[e->dest->index]].iterate)
8726 : : {
8727 : 4345198 : int destidx = bb_to_rpo[e->dest->index];
8728 : 4345198 : if (!rpo_state[destidx].visited)
8729 : : {
8730 : 147 : if (dump_file && (dump_flags & TDF_DETAILS))
8731 : 0 : fprintf (dump_file, "Unvisited destination %d\n",
8732 : : e->dest->index);
8733 : 147 : if (iterate_to == -1 || destidx < iterate_to)
8734 : 147 : iterate_to = destidx;
8735 : 147 : continue;
8736 : : }
8737 : 4345051 : if (dump_file && (dump_flags & TDF_DETAILS))
8738 : 53 : fprintf (dump_file, "Looking for changed values of backedge"
8739 : : " %d->%d destination PHIs\n",
8740 : 53 : e->src->index, e->dest->index);
8741 : 4345051 : vn_context_bb = e->dest;
8742 : 4345051 : gphi_iterator gsi;
8743 : 4345051 : for (gsi = gsi_start_phis (e->dest);
8744 : 9977499 : !gsi_end_p (gsi); gsi_next (&gsi))
8745 : : {
8746 : 7512729 : bool inserted = false;
8747 : : /* While we'd ideally just iterate on value changes
8748 : : we CSE PHIs and do that even across basic-block
8749 : : boundaries. So even hashtable state changes can
8750 : : be important (which is roughly equivalent to
8751 : : PHI argument value changes). To not excessively
8752 : : iterate because of that we track whether a PHI
8753 : : was CSEd to with GF_PLF_1. */
8754 : 7512729 : bool phival_changed;
8755 : 7512729 : if ((phival_changed = visit_phi (gsi.phi (),
8756 : : &inserted, false))
8757 : 8866040 : || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
8758 : : {
8759 : 1880281 : if (!phival_changed
8760 : 1880281 : && dump_file && (dump_flags & TDF_DETAILS))
8761 : 0 : fprintf (dump_file, "PHI was CSEd and hashtable "
8762 : : "state (changed)\n");
8763 : 1880281 : if (iterate_to == -1 || destidx < iterate_to)
8764 : 1880197 : iterate_to = destidx;
8765 : 1880281 : break;
8766 : : }
8767 : : }
8768 : 4345051 : vn_context_bb = NULL;
8769 : : }
8770 : 48269584 : if (iterate_to != -1)
8771 : : {
8772 : 1880137 : do_unwind (&rpo_state[iterate_to], avail);
8773 : 1880137 : idx = iterate_to;
8774 : 1880137 : if (dump_file && (dump_flags & TDF_DETAILS))
8775 : 20 : fprintf (dump_file, "Iterating to %d BB%d\n",
8776 : 20 : iterate_to, rpo[iterate_to]);
8777 : 1880137 : continue;
8778 : : }
8779 : :
8780 : 46389447 : idx++;
8781 : : }
8782 : 49175567 : while (idx < n);
8783 : :
8784 : : else /* !iterate */
8785 : : {
8786 : : /* Process all blocks greedily with a worklist that enforces RPO
8787 : : processing of reachable blocks. */
8788 : 1828963 : auto_bitmap worklist;
8789 : 1828963 : bitmap_set_bit (worklist, 0);
8790 : 17231943 : while (!bitmap_empty_p (worklist))
8791 : : {
8792 : 13574017 : int idx = bitmap_clear_first_set_bit (worklist);
8793 : 13574017 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8794 : 13574017 : gcc_assert ((bb->flags & BB_EXECUTABLE)
8795 : : && !rpo_state[idx].visited);
8796 : :
8797 : 13574017 : if (dump_file && (dump_flags & TDF_DETAILS))
8798 : 31652 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8799 : :
8800 : : /* When we run into predecessor edges where we cannot trust its
8801 : : executable state mark them executable so PHI processing will
8802 : : be conservative.
8803 : : ??? Do we need to force arguments flowing over that edge
8804 : : to be varying or will they even always be? */
8805 : 13574017 : edge_iterator ei;
8806 : 13574017 : edge e;
8807 : 32806573 : FOR_EACH_EDGE (e, ei, bb->preds)
8808 : 19232556 : if (!(e->flags & EDGE_EXECUTABLE)
8809 : 996614 : && (bb == entry->dest
8810 : 944837 : || (!rpo_state[bb_to_rpo[e->src->index]].visited
8811 : 910237 : && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
8812 : : >= (int)idx))))
8813 : : {
8814 : 938805 : if (dump_file && (dump_flags & TDF_DETAILS))
8815 : 10245 : fprintf (dump_file, "Cannot trust state of predecessor "
8816 : : "edge %d -> %d, marking executable\n",
8817 : 10245 : e->src->index, e->dest->index);
8818 : 938805 : e->flags |= EDGE_EXECUTABLE;
8819 : : }
8820 : :
8821 : 13574017 : nblk++;
8822 : 13574017 : todo |= process_bb (avail, bb, false, false, false, eliminate,
8823 : : do_region, exit_bbs,
8824 : 13574017 : skip_entry_phis && bb == entry->dest);
8825 : 13574017 : rpo_state[idx].visited++;
8826 : :
8827 : 33413234 : FOR_EACH_EDGE (e, ei, bb->succs)
8828 : 19839217 : if ((e->flags & EDGE_EXECUTABLE)
8829 : 19765462 : && e->dest->index != EXIT_BLOCK
8830 : 18624818 : && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
8831 : 37180373 : && !rpo_state[bb_to_rpo[e->dest->index]].visited)
8832 : 16406979 : bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
8833 : : }
8834 : 1828963 : }
8835 : :
8836 : : /* If statistics or dump file active. */
8837 : 6065833 : int nex = 0;
8838 : 6065833 : unsigned max_visited = 1;
8839 : 56770879 : for (int i = 0; i < n; ++i)
8840 : : {
8841 : 50705046 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8842 : 50705046 : if (bb->flags & BB_EXECUTABLE)
8843 : 50214103 : nex++;
8844 : 50705046 : statistics_histogram_event (cfun, "RPO block visited times",
8845 : 50705046 : rpo_state[i].visited);
8846 : 50705046 : if (rpo_state[i].visited > max_visited)
8847 : : max_visited = rpo_state[i].visited;
8848 : : }
8849 : 6065833 : unsigned nvalues = 0, navail = 0;
8850 : 169457220 : for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
8851 : 332848607 : i != vn_ssa_aux_hash->end (); ++i)
8852 : : {
8853 : 163391387 : nvalues++;
8854 : 163391387 : vn_avail *av = (*i)->avail;
8855 : 240914836 : while (av)
8856 : : {
8857 : 77523449 : navail++;
8858 : 77523449 : av = av->next;
8859 : : }
8860 : : }
8861 : 6065833 : statistics_counter_event (cfun, "RPO blocks", n);
8862 : 6065833 : statistics_counter_event (cfun, "RPO blocks visited", nblk);
8863 : 6065833 : statistics_counter_event (cfun, "RPO blocks executable", nex);
8864 : 6065833 : statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
8865 : 6065833 : statistics_histogram_event (cfun, "RPO num values", nvalues);
8866 : 6065833 : statistics_histogram_event (cfun, "RPO num avail", navail);
8867 : 6065833 : statistics_histogram_event (cfun, "RPO num lattice",
8868 : 6065833 : vn_ssa_aux_hash->elements ());
8869 : 6065833 : if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
8870 : : {
8871 : 10220 : fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
8872 : : " blocks in total discovering %d executable blocks iterating "
8873 : : "%d.%d times, a block was visited max. %u times\n",
8874 : : n, nblk, nex,
8875 : 10220 : (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
8876 : : max_visited);
8877 : 10220 : fprintf (dump_file, "RPO tracked %d values available at %d locations "
8878 : : "and %" PRIu64 " lattice elements\n",
8879 : 10220 : nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
8880 : : }
8881 : :
8882 : 6065833 : if (eliminate)
8883 : : {
8884 : : /* When !iterate we already performed elimination during the RPO
8885 : : walk. */
8886 : 5098393 : if (iterate)
8887 : : {
8888 : : /* Elimination for region-based VN needs to be done within the
8889 : : RPO walk. */
8890 : 3288712 : gcc_assert (! do_region);
8891 : : /* Note we can't use avail.walk here because that gets confused
8892 : : by the existing availability and it will be less efficient
8893 : : as well. */
8894 : 3288712 : todo |= eliminate_with_rpo_vn (NULL);
8895 : : }
8896 : : else
8897 : 1809681 : todo |= avail.eliminate_cleanup (do_region);
8898 : : }
8899 : :
8900 : 6065833 : vn_valueize = NULL;
8901 : 6065833 : rpo_avail = NULL;
8902 : :
8903 : 6065833 : XDELETEVEC (bb_to_rpo);
8904 : 6065833 : XDELETEVEC (rpo);
8905 : 6065833 : XDELETEVEC (rpo_state);
8906 : :
8907 : 6065833 : return todo;
8908 : 6065833 : }
8909 : :
8910 : : /* Region-based entry for RPO VN. Performs value-numbering and elimination
8911 : : on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
8912 : : the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
8913 : : are not considered.
8914 : : If ITERATE is true then treat backedges optimistically as not
8915 : : executed and iterate. If ELIMINATE is true then perform
8916 : : elimination, otherwise leave that to the caller.
8917 : : If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
8918 : : KIND specifies the amount of work done for handling memory operations. */
8919 : :
8920 : : unsigned
8921 : 678212 : do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
8922 : : bool iterate, bool eliminate, bool skip_entry_phis,
8923 : : vn_lookup_kind kind)
8924 : : {
8925 : 678212 : auto_timevar tv (TV_TREE_RPO_VN);
8926 : 678212 : unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
8927 : : skip_entry_phis, kind);
8928 : 678212 : free_rpo_vn ();
8929 : 1356424 : return todo;
8930 : 678212 : }
8931 : :
8932 : :
8933 : : namespace {
8934 : :
8935 : : const pass_data pass_data_fre =
8936 : : {
8937 : : GIMPLE_PASS, /* type */
8938 : : "fre", /* name */
8939 : : OPTGROUP_NONE, /* optinfo_flags */
8940 : : TV_TREE_FRE, /* tv_id */
8941 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
8942 : : 0, /* properties_provided */
8943 : : 0, /* properties_destroyed */
8944 : : 0, /* todo_flags_start */
8945 : : 0, /* todo_flags_finish */
8946 : : };
8947 : :
8948 : : class pass_fre : public gimple_opt_pass
8949 : : {
8950 : : public:
8951 : 1425405 : pass_fre (gcc::context *ctxt)
8952 : 2850810 : : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
8953 : : {}
8954 : :
8955 : : /* opt_pass methods: */
8956 : 1140324 : opt_pass * clone () final override { return new pass_fre (m_ctxt); }
8957 : 1425405 : void set_pass_param (unsigned int n, bool param) final override
8958 : : {
8959 : 1425405 : gcc_assert (n == 0);
8960 : 1425405 : may_iterate = param;
8961 : 1425405 : }
8962 : 4514228 : bool gate (function *) final override
8963 : : {
8964 : 4514228 : return flag_tree_fre != 0 && (may_iterate || optimize > 1);
8965 : : }
8966 : : unsigned int execute (function *) final override;
8967 : :
8968 : : private:
8969 : : bool may_iterate;
8970 : : }; // class pass_fre
8971 : :
8972 : : unsigned int
8973 : 4439463 : pass_fre::execute (function *fun)
8974 : : {
8975 : 4439463 : unsigned todo = 0;
8976 : :
8977 : : /* At -O[1g] use the cheap non-iterating mode. */
8978 : 4439463 : bool iterate_p = may_iterate && (optimize > 1);
8979 : 4439463 : calculate_dominance_info (CDI_DOMINATORS);
8980 : 4439463 : if (iterate_p)
8981 : 3288712 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
8982 : :
8983 : 4439463 : todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
8984 : 4439463 : free_rpo_vn ();
8985 : :
8986 : 4439463 : if (iterate_p)
8987 : 3288712 : loop_optimizer_finalize ();
8988 : :
8989 : 4439463 : if (scev_initialized_p ())
8990 : 28567 : scev_reset_htab ();
8991 : :
8992 : : /* For late FRE after IVOPTs and unrolling, see if we can
8993 : : remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8994 : 4439463 : if (!may_iterate)
8995 : 976506 : todo |= TODO_update_address_taken;
8996 : :
8997 : 4439463 : return todo;
8998 : : }
8999 : :
9000 : : } // anon namespace
9001 : :
9002 : : gimple_opt_pass *
9003 : 285081 : make_pass_fre (gcc::context *ctxt)
9004 : : {
9005 : 285081 : return new pass_fre (ctxt);
9006 : : }
9007 : :
9008 : : #undef BB_EXECUTABLE
|