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 : 774232186 : vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
159 : : {
160 : 774232186 : 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 : 979934750 : vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
168 : : {
169 : 979934750 : 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 : 26203009 : vn_phi_hasher::hash (const vn_phi_s *vp1)
191 : : {
192 : 26203009 : return vp1->hashcode;
193 : : }
194 : :
195 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
196 : :
197 : : inline bool
198 : 47238560 : vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
199 : : {
200 : 47238560 : 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 : 23547154 : vn_reference_op_eq (const void *p1, const void *p2)
212 : : {
213 : 23547154 : const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
214 : 23547154 : const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
215 : :
216 : 23547154 : return (vro1->opcode == vro2->opcode
217 : : /* We do not care for differences in type qualification. */
218 : 23545311 : && (vro1->type == vro2->type
219 : 1101913 : || (vro1->type && vro2->type
220 : 1101913 : && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
221 : 1101913 : TYPE_MAIN_VARIANT (vro2->type))))
222 : 22583741 : && expressions_equal_p (vro1->op0, vro2->op0)
223 : 22569552 : && expressions_equal_p (vro1->op1, vro2->op1)
224 : 22569552 : && expressions_equal_p (vro1->op2, vro2->op2)
225 : 46116706 : && (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 : 3633810458 : vn_reference_hasher::hash (const vn_reference_s *vr1)
249 : : {
250 : 3633810458 : return vr1->hashcode;
251 : : }
252 : :
253 : : inline bool
254 : 4351284370 : vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
255 : : {
256 : 4351284370 : return v == c || vn_reference_eq (v, c);
257 : : }
258 : :
259 : : typedef hash_table<vn_reference_hasher> vn_reference_table_type;
260 : : typedef vn_reference_table_type::iterator vn_reference_iterator_type;
261 : :
262 : : /* Pretty-print OPS to OUTFILE. */
263 : :
264 : : void
265 : 317 : print_vn_reference_ops (FILE *outfile, const vec<vn_reference_op_s> ops)
266 : : {
267 : 317 : vn_reference_op_t vro;
268 : 317 : unsigned int i;
269 : 317 : fprintf (outfile, "{");
270 : 1394 : for (i = 0; ops.iterate (i, &vro); i++)
271 : : {
272 : 1077 : bool closebrace = false;
273 : 1077 : if (vro->opcode != SSA_NAME
274 : 863 : && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
275 : : {
276 : 863 : fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
277 : 863 : if (vro->op0 || vro->opcode == CALL_EXPR)
278 : : {
279 : 863 : fprintf (outfile, "<");
280 : 863 : closebrace = true;
281 : : }
282 : : }
283 : 1077 : if (vro->op0 || vro->opcode == CALL_EXPR)
284 : : {
285 : 1077 : if (!vro->op0)
286 : 0 : fprintf (outfile, internal_fn_name ((internal_fn)vro->clique));
287 : : else
288 : 1077 : print_generic_expr (outfile, vro->op0);
289 : 1077 : if (vro->op1)
290 : : {
291 : 185 : fprintf (outfile, ",");
292 : 185 : print_generic_expr (outfile, vro->op1);
293 : : }
294 : 1077 : if (vro->op2)
295 : : {
296 : 185 : fprintf (outfile, ",");
297 : 185 : print_generic_expr (outfile, vro->op2);
298 : : }
299 : : }
300 : 1077 : if (closebrace)
301 : 863 : fprintf (outfile, ">");
302 : 1077 : if (i != ops.length () - 1)
303 : 760 : fprintf (outfile, ",");
304 : : }
305 : 317 : fprintf (outfile, "}");
306 : 317 : }
307 : :
308 : : DEBUG_FUNCTION void
309 : 0 : debug_vn_reference_ops (const vec<vn_reference_op_s> ops)
310 : : {
311 : 0 : print_vn_reference_ops (stderr, ops);
312 : 0 : fputc ('\n', stderr);
313 : 0 : }
314 : :
315 : : /* The set of VN hashtables. */
316 : :
317 : : typedef struct vn_tables_s
318 : : {
319 : : vn_nary_op_table_type *nary;
320 : : vn_phi_table_type *phis;
321 : : vn_reference_table_type *references;
322 : : } *vn_tables_t;
323 : :
324 : :
325 : : /* vn_constant hashtable helpers. */
326 : :
327 : : struct vn_constant_hasher : free_ptr_hash <vn_constant_s>
328 : : {
329 : : static inline hashval_t hash (const vn_constant_s *);
330 : : static inline bool equal (const vn_constant_s *, const vn_constant_s *);
331 : : };
332 : :
333 : : /* Hash table hash function for vn_constant_t. */
334 : :
335 : : inline hashval_t
336 : 12307244 : vn_constant_hasher::hash (const vn_constant_s *vc1)
337 : : {
338 : 12307244 : return vc1->hashcode;
339 : : }
340 : :
341 : : /* Hash table equality function for vn_constant_t. */
342 : :
343 : : inline bool
344 : 14849382 : vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
345 : : {
346 : 14849382 : if (vc1->hashcode != vc2->hashcode)
347 : : return false;
348 : :
349 : 2230663 : 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 : 83292 : vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
379 : : {
380 : 83292 : 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 : 83292 : if (!SSA_NAME_IS_DEFAULT_DEF (t))
387 : 79465 : vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
388 : 83292 : tree res = vn_valueize (t);
389 : 83292 : vn_context_bb = saved_vn_context_bb;
390 : 83292 : 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 : >13090*10^7 : static inline bool is_empty (value_type &e) { return e == NULL; }
420 : : };
421 : :
422 : : hashval_t
423 : 43169779205 : vn_ssa_aux_hasher::hash (const value_type &entry)
424 : : {
425 : 43169779205 : return SSA_NAME_VERSION (entry->name);
426 : : }
427 : :
428 : : bool
429 : 49357722493 : vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
430 : : {
431 : 49357722493 : 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 : 5223071 : has_VN_INFO (tree name)
452 : : {
453 : 5223071 : return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
454 : : }
455 : :
456 : : vn_ssa_aux_t
457 : 3686780285 : VN_INFO (tree name)
458 : : {
459 : 3686780285 : vn_ssa_aux_t *res
460 : 3686780285 : = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
461 : : INSERT);
462 : 3686780285 : if (*res != NULL)
463 : : return *res;
464 : :
465 : 172307642 : vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
466 : 172307642 : memset (newinfo, 0, sizeof (struct vn_ssa_aux));
467 : 172307642 : newinfo->name = name;
468 : 172307642 : 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 : 172307642 : 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 : 172307642 : if (SSA_NAME_IS_DEFAULT_DEF (name))
476 : 9319089 : switch (TREE_CODE (SSA_NAME_VAR (name)))
477 : : {
478 : 1666321 : case VAR_DECL:
479 : : /* All undefined vars are VARYING. */
480 : 1666321 : newinfo->valnum = name;
481 : 1666321 : newinfo->visited = true;
482 : 1666321 : break;
483 : :
484 : 7591231 : case PARM_DECL:
485 : : /* Parameters are VARYING but we can record a condition
486 : : if we know it is a non-NULL pointer. */
487 : 7591231 : newinfo->visited = true;
488 : 7591231 : newinfo->valnum = name;
489 : 11645214 : if (POINTER_TYPE_P (TREE_TYPE (name))
490 : 8742942 : && nonnull_arg_p (SSA_NAME_VAR (name)))
491 : : {
492 : 2391712 : tree ops[2];
493 : 2391712 : ops[0] = name;
494 : 2391712 : ops[1] = build_int_cst (TREE_TYPE (name), 0);
495 : 2391712 : vn_nary_op_t nary;
496 : : /* Allocate from non-unwinding stack. */
497 : 2391712 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
498 : 2391712 : init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
499 : : boolean_type_node, ops);
500 : 2391712 : nary->predicated_values = 0;
501 : 2391712 : nary->u.result = boolean_true_node;
502 : 2391712 : vn_nary_op_insert_into (nary, valid_info->nary);
503 : 2391712 : gcc_assert (nary->unwind_to == NULL);
504 : : /* Also do not link it into the undo chain. */
505 : 2391712 : last_inserted_nary = nary->next;
506 : 2391712 : nary->next = (vn_nary_op_t)(void *)-1;
507 : 2391712 : nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
508 : 2391712 : init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
509 : : boolean_type_node, ops);
510 : 2391712 : nary->predicated_values = 0;
511 : 2391712 : nary->u.result = boolean_false_node;
512 : 2391712 : vn_nary_op_insert_into (nary, valid_info->nary);
513 : 2391712 : gcc_assert (nary->unwind_to == NULL);
514 : 2391712 : last_inserted_nary = nary->next;
515 : 2391712 : nary->next = (vn_nary_op_t)(void *)-1;
516 : 2391712 : if (dump_file && (dump_flags & TDF_DETAILS))
517 : : {
518 : 38 : fprintf (dump_file, "Recording ");
519 : 38 : print_generic_expr (dump_file, name, TDF_SLIM);
520 : 38 : fprintf (dump_file, " != 0\n");
521 : : }
522 : : }
523 : : break;
524 : :
525 : 61537 : 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 : 61537 : newinfo->visited = true;
530 : 61537 : newinfo->valnum = name;
531 : 61537 : 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 : 3394771404 : SSA_VAL (tree x, bool *visited = NULL)
543 : : {
544 : 3394771404 : vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
545 : 3394771404 : if (visited)
546 : 1368667112 : *visited = tem && tem->visited;
547 : 3394771404 : 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 : 1252599717 : vuse_ssa_val (tree x)
556 : : {
557 : 1252599717 : if (!x)
558 : : return NULL_TREE;
559 : :
560 : 1249189702 : do
561 : : {
562 : 1249189702 : x = SSA_VAL (x);
563 : 1249189702 : gcc_assert (x != VN_TOP);
564 : : }
565 : 1249189702 : 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 : 1053246045 : vuse_valueize (tree vuse)
576 : : {
577 : 1053246045 : do
578 : : {
579 : 1053246045 : bool visited;
580 : 1053246045 : vuse = SSA_VAL (vuse, &visited);
581 : 1053246045 : if (!visited)
582 : 15984597 : return NULL_TREE;
583 : 1037261448 : gcc_assert (vuse != VN_TOP);
584 : : }
585 : 1037261448 : 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 : 102918700 : vn_get_stmt_kind (gimple *stmt)
595 : : {
596 : 102918700 : switch (gimple_code (stmt))
597 : : {
598 : : case GIMPLE_CALL:
599 : : return VN_REFERENCE;
600 : : case GIMPLE_PHI:
601 : : return VN_PHI;
602 : 102918700 : case GIMPLE_ASSIGN:
603 : 102918700 : {
604 : 102918700 : enum tree_code code = gimple_assign_rhs_code (stmt);
605 : 102918700 : tree rhs1 = gimple_assign_rhs1 (stmt);
606 : 102918700 : 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 : 48522211 : case GIMPLE_SINGLE_RHS:
613 : 48522211 : switch (TREE_CODE_CLASS (code))
614 : : {
615 : 36479534 : case tcc_reference:
616 : : /* VOP-less references can go through unary case. */
617 : 36479534 : if ((code == REALPART_EXPR
618 : : || code == IMAGPART_EXPR
619 : 36479534 : || code == VIEW_CONVERT_EXPR
620 : 36479534 : || code == BIT_FIELD_REF)
621 : 36479534 : && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
622 : 735151 : || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
623 : 1970131 : 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 : 6002038 : default:
633 : 6002038 : if (code == ADDR_EXPR)
634 : 3258411 : return (is_gimple_min_invariant (rhs1)
635 : 3258411 : ? VN_CONSTANT : VN_REFERENCE);
636 : 2743627 : 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 : 28735134 : get_or_alloc_constant_value_id (tree constant)
671 : : {
672 : 28735134 : vn_constant_s **slot;
673 : 28735134 : struct vn_constant_s vc;
674 : 28735134 : 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 : 28735134 : if (!constant_to_value_id)
679 : : return 0;
680 : :
681 : 4773593 : vc.hashcode = vn_hash_constant_with_type (constant);
682 : 4773593 : vc.constant = constant;
683 : 4773593 : slot = constant_to_value_id->find_slot (&vc, INSERT);
684 : 4773593 : if (*slot)
685 : 2213597 : return (*slot)->value_id;
686 : :
687 : 2559996 : vcp = XNEW (struct vn_constant_s);
688 : 2559996 : vcp->hashcode = vc.hashcode;
689 : 2559996 : vcp->constant = constant;
690 : 2559996 : vcp->value_id = get_next_constant_value_id ();
691 : 2559996 : *slot = vcp;
692 : 2559996 : return vcp->value_id;
693 : : }
694 : :
695 : : /* Compute the hash for a reference operand VRO1. */
696 : :
697 : : static void
698 : 133299704 : vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
699 : : {
700 : 133299704 : hstate.add_int (vro1->opcode);
701 : 133299704 : if (vro1->opcode == CALL_EXPR && !vro1->op0)
702 : 551950 : hstate.add_int (vro1->clique);
703 : 133299704 : if (vro1->op0)
704 : 127086812 : inchash::add_expr (vro1->op0, hstate);
705 : 133299704 : if (vro1->op1)
706 : 11398599 : inchash::add_expr (vro1->op1, hstate);
707 : 133299704 : if (vro1->op2)
708 : 13207146 : inchash::add_expr (vro1->op2, hstate);
709 : 133299704 : }
710 : :
711 : : /* Compute a hash for the reference operation VR1 and return it. */
712 : :
713 : : static hashval_t
714 : 199456111 : vn_reference_compute_hash (const vn_reference_t vr1)
715 : : {
716 : 199456111 : inchash::hash hstate;
717 : 199456111 : hashval_t result;
718 : 199456111 : int i;
719 : 199456111 : vn_reference_op_t vro;
720 : 199456111 : poly_offset_int off = -1;
721 : 199456111 : bool deref = false;
722 : :
723 : 811256540 : FOR_EACH_VEC_ELT (vr1->operands, i, vro)
724 : : {
725 : 611800429 : if (vro->opcode == MEM_REF)
726 : : deref = true;
727 : 422912319 : else if (vro->opcode != ADDR_EXPR)
728 : 295396412 : deref = false;
729 : 611800429 : if (maybe_ne (vro->off, -1))
730 : : {
731 : 359024337 : if (known_eq (off, -1))
732 : 191064193 : off = 0;
733 : 611800429 : off += vro->off;
734 : : }
735 : : else
736 : : {
737 : 252776092 : if (maybe_ne (off, -1)
738 : 252776092 : && maybe_ne (off, 0))
739 : 101381477 : hstate.add_poly_hwi (off.force_shwi ());
740 : 252776092 : off = -1;
741 : 252776092 : if (deref
742 : 119712357 : && vro->opcode == ADDR_EXPR)
743 : : {
744 : 119476388 : if (vro->op0)
745 : : {
746 : 119476388 : tree op = TREE_OPERAND (vro->op0, 0);
747 : 119476388 : hstate.add_int (TREE_CODE (op));
748 : 119476388 : inchash::add_expr (op, hstate);
749 : : }
750 : : }
751 : : else
752 : 133299704 : 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 : 199456111 : result = hstate.end ();
758 : : /* ??? We would ICE later if we hash instead of adding that in. */
759 : 199456111 : if (vr1->vuse)
760 : 194665128 : result += SSA_NAME_VERSION (vr1->vuse);
761 : :
762 : 199456111 : 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 : 4346437498 : vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
770 : : {
771 : 4346437498 : unsigned i, j;
772 : :
773 : : /* Early out if this is not a hash collision. */
774 : 4346437498 : if (vr1->hashcode != vr2->hashcode)
775 : : return false;
776 : :
777 : : /* The VOP needs to be the same. */
778 : 18211274 : 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 : 18210817 : if (maybe_ne (vr1->offset, vr2->offset)
784 : 18210817 : || maybe_ne (vr1->max_size, vr2->max_size))
785 : : {
786 : : /* But nothing known in the prevailing entry is OK to be used. */
787 : 6832113 : 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 : 36338956 : if (vr1->operands == vr2->operands)
793 : : return true;
794 : :
795 : 17317733 : if (!vr1->type || !vr2->type)
796 : : {
797 : 548704 : if (vr1->type != vr2->type)
798 : : return false;
799 : : }
800 : 16769029 : else if (vr1->type == vr2->type)
801 : : ;
802 : 2205104 : else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
803 : 2205104 : || (COMPLETE_TYPE_P (vr1->type)
804 : 2205104 : && !expressions_equal_p (TYPE_SIZE (vr1->type),
805 : 2205104 : TYPE_SIZE (vr2->type))))
806 : 796824 : return false;
807 : 1408280 : else if (vr1->operands[0].opcode == CALL_EXPR
808 : 1408280 : && !types_compatible_p (vr1->type, vr2->type))
809 : : return false;
810 : 1408280 : else if (INTEGRAL_TYPE_P (vr1->type)
811 : 586894 : && INTEGRAL_TYPE_P (vr2->type))
812 : : {
813 : 545506 : if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
814 : : return false;
815 : : }
816 : 862774 : else if (INTEGRAL_TYPE_P (vr1->type)
817 : 862774 : && (TYPE_PRECISION (vr1->type)
818 : 41388 : != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
819 : : return false;
820 : 862640 : else if (INTEGRAL_TYPE_P (vr2->type)
821 : 862640 : && (TYPE_PRECISION (vr2->type)
822 : 9800 : != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
823 : : return false;
824 : 8318 : else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
825 : 862046 : && 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 : 862046 : else if (TYPE_MODE (vr1->type) != TYPE_MODE (vr2->type)
845 : 862046 : && (!mode_can_transfer_bits (TYPE_MODE (vr1->type))
846 : 44579 : || !mode_can_transfer_bits (TYPE_MODE (vr2->type))))
847 : 1056 : return false;
848 : :
849 : : i = 0;
850 : : j = 0;
851 : 19847829 : do
852 : : {
853 : 19847829 : poly_offset_int off1 = 0, off2 = 0;
854 : 19847829 : vn_reference_op_t vro1, vro2;
855 : 19847829 : vn_reference_op_s tem1, tem2;
856 : 19847829 : bool deref1 = false, deref2 = false;
857 : 19847829 : bool reverse1 = false, reverse2 = false;
858 : 67548453 : for (; vr1->operands.iterate (i, &vro1); i++)
859 : : {
860 : 47700624 : if (vro1->opcode == MEM_REF)
861 : : deref1 = true;
862 : : /* Do not look through a storage order barrier. */
863 : 32595413 : else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
864 : 5992 : return false;
865 : 47700624 : reverse1 |= vro1->reverse;
866 : 47700624 : if (known_eq (vro1->off, -1))
867 : : break;
868 : 27852795 : off1 += vro1->off;
869 : : }
870 : 47865681 : for (; vr2->operands.iterate (j, &vro2); j++)
871 : : {
872 : 47865681 : if (vro2->opcode == MEM_REF)
873 : : deref2 = true;
874 : : /* Do not look through a storage order barrier. */
875 : 32760460 : else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
876 : : return false;
877 : 47865681 : reverse2 |= vro2->reverse;
878 : 47865681 : if (known_eq (vro2->off, -1))
879 : : break;
880 : 28017852 : off2 += vro2->off;
881 : : }
882 : 19847829 : if (maybe_ne (off1, off2) || reverse1 != reverse2)
883 : : return false;
884 : 19847783 : if (deref1 && vro1->opcode == ADDR_EXPR)
885 : : {
886 : 8386124 : memset (&tem1, 0, sizeof (tem1));
887 : 8386124 : tem1.op0 = TREE_OPERAND (vro1->op0, 0);
888 : 8386124 : tem1.type = TREE_TYPE (tem1.op0);
889 : 8386124 : tem1.opcode = TREE_CODE (tem1.op0);
890 : 8386124 : vro1 = &tem1;
891 : 8386124 : deref1 = false;
892 : : }
893 : 19847783 : if (deref2 && vro2->opcode == ADDR_EXPR)
894 : : {
895 : 8386134 : memset (&tem2, 0, sizeof (tem2));
896 : 8386134 : tem2.op0 = TREE_OPERAND (vro2->op0, 0);
897 : 8386134 : tem2.type = TREE_TYPE (tem2.op0);
898 : 8386134 : tem2.opcode = TREE_CODE (tem2.op0);
899 : 8386134 : vro2 = &tem2;
900 : 8386134 : deref2 = false;
901 : : }
902 : 19847783 : if (deref1 != deref2)
903 : : return false;
904 : 19847783 : if (!vn_reference_op_eq (vro1, vro2))
905 : : return false;
906 : 19841837 : ++j;
907 : 19841837 : ++i;
908 : : }
909 : 39683674 : while (vr1->operands.length () != i
910 : 59525511 : || 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 : 219593547 : 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 : 219593547 : tree orig = ref;
923 : 760000821 : while (ref)
924 : : {
925 : 540407274 : vn_reference_op_s temp;
926 : :
927 : 540407274 : memset (&temp, 0, sizeof (temp));
928 : 540407274 : temp.type = TREE_TYPE (ref);
929 : 540407274 : temp.opcode = TREE_CODE (ref);
930 : 540407274 : temp.off = -1;
931 : :
932 : 540407274 : switch (temp.opcode)
933 : : {
934 : 15057690 : case MODIFY_EXPR:
935 : 15057690 : temp.op0 = TREE_OPERAND (ref, 1);
936 : 15057690 : break;
937 : 137 : case WITH_SIZE_EXPR:
938 : 137 : temp.op0 = TREE_OPERAND (ref, 1);
939 : 137 : temp.off = 0;
940 : 137 : break;
941 : 114528829 : case MEM_REF:
942 : : /* The base address gets its own vn_reference_op_s structure. */
943 : 114528829 : temp.op0 = TREE_OPERAND (ref, 1);
944 : 114528829 : if (!mem_ref_offset (ref).to_shwi (&temp.off))
945 : 0 : temp.off = -1;
946 : 114528829 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
947 : 114528829 : temp.base = MR_DEPENDENCE_BASE (ref);
948 : 114528829 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
949 : 114528829 : break;
950 : 2512010 : case TARGET_MEM_REF:
951 : : /* The base address gets its own vn_reference_op_s structure. */
952 : 2512010 : temp.op0 = TMR_INDEX (ref);
953 : 2512010 : temp.op1 = TMR_STEP (ref);
954 : 2512010 : temp.op2 = TMR_OFFSET (ref);
955 : 2512010 : temp.clique = MR_DEPENDENCE_CLIQUE (ref);
956 : 2512010 : temp.base = MR_DEPENDENCE_BASE (ref);
957 : 2512010 : result->safe_push (temp);
958 : 2512010 : memset (&temp, 0, sizeof (temp));
959 : 2512010 : temp.type = NULL_TREE;
960 : 2512010 : temp.opcode = ERROR_MARK;
961 : 2512010 : temp.op0 = TMR_INDEX2 (ref);
962 : 2512010 : temp.off = -1;
963 : 2512010 : break;
964 : 868313 : case BIT_FIELD_REF:
965 : : /* Record bits, position and storage order. */
966 : 868313 : temp.op0 = TREE_OPERAND (ref, 1);
967 : 868313 : temp.op1 = TREE_OPERAND (ref, 2);
968 : 1735916 : if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
969 : 710 : temp.off = -1;
970 : 868313 : temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
971 : 868313 : break;
972 : 143816497 : case COMPONENT_REF:
973 : : /* The field decl is enough to unambiguously specify the field,
974 : : so use its type here. */
975 : 143816497 : temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
976 : 143816497 : temp.op0 = TREE_OPERAND (ref, 1);
977 : 143816497 : temp.op1 = TREE_OPERAND (ref, 2);
978 : 287630643 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
979 : 287630360 : && TYPE_REVERSE_STORAGE_ORDER
980 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
981 : 143816497 : {
982 : 143816497 : tree this_offset = component_ref_field_offset (ref);
983 : 143816497 : if (this_offset
984 : 143816497 : && poly_int_tree_p (this_offset))
985 : : {
986 : 143814395 : tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
987 : 143814395 : if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
988 : : {
989 : 143150763 : poly_offset_int off
990 : 143150763 : = (wi::to_poly_offset (this_offset)
991 : 143150763 : + (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 : 143150763 : if (TREE_CODE (orig) != ADDR_EXPR
997 : 4851219 : || (TYPE_SIZE (temp.type)
998 : 4838747 : && integer_nonzerop (TYPE_SIZE (temp.type))
999 : 6282978 : && maybe_ne (off, 0))
1000 : 146109129 : || (cfun->curr_properties & PROP_objsz))
1001 : 141704401 : off.to_shwi (&temp.off);
1002 : : }
1003 : : }
1004 : : }
1005 : : break;
1006 : 37476016 : case ARRAY_RANGE_REF:
1007 : 37476016 : case ARRAY_REF:
1008 : 37476016 : {
1009 : 37476016 : tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
1010 : : /* Record index as operand. */
1011 : 37476016 : temp.op0 = TREE_OPERAND (ref, 1);
1012 : : /* Always record lower bounds and element size. */
1013 : 37476016 : temp.op1 = array_ref_low_bound (ref);
1014 : : /* But record element size in units of the type alignment. */
1015 : 37476016 : temp.op2 = TREE_OPERAND (ref, 3);
1016 : 37476016 : temp.align = eltype->type_common.align;
1017 : 37476016 : if (! temp.op2)
1018 : 37267540 : 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 : 37476016 : bool avoid_oob = true;
1025 : 37476016 : if (TREE_CODE (orig) != ADDR_EXPR
1026 : 449909 : || cfun->curr_properties & PROP_objsz)
1027 : : avoid_oob = false;
1028 : 216301 : else if (poly_int_tree_p (temp.op0))
1029 : : {
1030 : 73463 : tree ub = array_ref_up_bound (ref);
1031 : 73463 : if (ub
1032 : 71837 : && 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 : 63938 : && !integer_minus_onep (ub)
1037 : 145300 : && known_le (wi::to_poly_offset (temp.op0),
1038 : : wi::to_poly_offset (ub)))
1039 : 63091 : avoid_oob = false;
1040 : : }
1041 : 37476016 : if (poly_int_tree_p (temp.op0)
1042 : 21698315 : && poly_int_tree_p (temp.op1)
1043 : 21698291 : && TREE_CODE (temp.op2) == INTEGER_CST
1044 : 59114123 : && !avoid_oob)
1045 : : {
1046 : 43257180 : poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1047 : 64885770 : - wi::to_poly_offset (temp.op1))
1048 : 43257180 : * wi::to_offset (temp.op2)
1049 : 21628590 : * vn_ref_op_align_unit (&temp));
1050 : 21628590 : off.to_shwi (&temp.off);
1051 : : }
1052 : 37476016 : temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1053 : 37476016 : && TYPE_REVERSE_STORAGE_ORDER
1054 : : (TREE_TYPE (TREE_OPERAND (ref, 0))));
1055 : : }
1056 : 37476016 : break;
1057 : 81989594 : case VAR_DECL:
1058 : 81989594 : if (DECL_HARD_REGISTER (ref))
1059 : : {
1060 : 20201 : temp.op0 = ref;
1061 : 20201 : break;
1062 : : }
1063 : : /* Fallthru. */
1064 : 85401853 : case PARM_DECL:
1065 : 85401853 : case CONST_DECL:
1066 : 85401853 : 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 : 85401853 : temp.opcode = MEM_REF;
1070 : 85401853 : temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1071 : 85401853 : temp.off = 0;
1072 : 85401853 : result->safe_push (temp);
1073 : 85401853 : temp.opcode = ADDR_EXPR;
1074 : 85401853 : temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1075 : 85401853 : temp.type = TREE_TYPE (temp.op0);
1076 : 85401853 : temp.off = -1;
1077 : 85401853 : break;
1078 : 93773905 : case STRING_CST:
1079 : 93773905 : case INTEGER_CST:
1080 : 93773905 : case POLY_INT_CST:
1081 : 93773905 : case COMPLEX_CST:
1082 : 93773905 : case VECTOR_CST:
1083 : 93773905 : case REAL_CST:
1084 : 93773905 : case FIXED_CST:
1085 : 93773905 : case CONSTRUCTOR:
1086 : 93773905 : case SSA_NAME:
1087 : 93773905 : temp.op0 = ref;
1088 : 93773905 : break;
1089 : 44518154 : case ADDR_EXPR:
1090 : 44518154 : if (is_gimple_min_invariant (ref))
1091 : : {
1092 : 40397588 : temp.op0 = ref;
1093 : 40397588 : 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 : 470366 : case REALPART_EXPR:
1102 : 470366 : temp.off = 0;
1103 : 470366 : break;
1104 : 1487827 : case VIEW_CONVERT_EXPR:
1105 : 1487827 : temp.off = 0;
1106 : 1487827 : temp.reverse = storage_order_barrier_p (ref);
1107 : 1487827 : break;
1108 : 475476 : case IMAGPART_EXPR:
1109 : : /* This is only interesting for its constant offset. */
1110 : 475476 : temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1111 : 475476 : break;
1112 : 0 : default:
1113 : 0 : gcc_unreachable ();
1114 : : }
1115 : 540407274 : result->safe_push (temp);
1116 : :
1117 : 540407274 : if (REFERENCE_CLASS_P (ref)
1118 : 238771940 : || TREE_CODE (ref) == MODIFY_EXPR
1119 : 223714250 : || TREE_CODE (ref) == WITH_SIZE_EXPR
1120 : 764121387 : || (TREE_CODE (ref) == ADDR_EXPR
1121 : 44518154 : && !is_gimple_min_invariant (ref)))
1122 : 320813727 : ref = TREE_OPERAND (ref, 0);
1123 : : else
1124 : : ref = NULL_TREE;
1125 : : }
1126 : 219593547 : }
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 : 14264063 : 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 : 14264063 : unsigned i;
1138 : 14264063 : tree base = NULL_TREE;
1139 : 14264063 : tree *op0_p = &base;
1140 : 14264063 : poly_offset_int offset = 0;
1141 : 14264063 : poly_offset_int max_size;
1142 : 14264063 : poly_offset_int size = -1;
1143 : 14264063 : tree size_tree = NULL_TREE;
1144 : :
1145 : : /* We don't handle calls. */
1146 : 14264063 : if (!type)
1147 : : return false;
1148 : :
1149 : 14264063 : machine_mode mode = TYPE_MODE (type);
1150 : 14264063 : if (mode == BLKmode)
1151 : 253490 : size_tree = TYPE_SIZE (type);
1152 : : else
1153 : 28021146 : size = GET_MODE_BITSIZE (mode);
1154 : 14010573 : if (size_tree != NULL_TREE
1155 : 253490 : && poly_int_tree_p (size_tree))
1156 : 253490 : size = wi::to_poly_offset (size_tree);
1157 : :
1158 : : /* Lower the final access size from the outermost expression. */
1159 : 14264063 : 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 : 14264063 : vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1163 : 14264063 : size_tree = NULL_TREE;
1164 : 14264063 : if (op->opcode == COMPONENT_REF)
1165 : 4943260 : size_tree = DECL_SIZE (op->op0);
1166 : 9320803 : else if (op->opcode == BIT_FIELD_REF)
1167 : 88805 : size_tree = op->op0;
1168 : 5032065 : if (size_tree != NULL_TREE
1169 : 5032065 : && poly_int_tree_p (size_tree)
1170 : 10064130 : && (!known_size_p (size)
1171 : 14264063 : || known_lt (wi::to_poly_offset (size_tree), size)))
1172 : 62727 : 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 : 14264063 : max_size = size;
1177 : :
1178 : : /* Compute cumulative bit-offset for nested component-refs and array-refs,
1179 : : and find the ultimate containing object. */
1180 : 54806174 : FOR_EACH_VEC_ELT (ops, i, op)
1181 : : {
1182 : 40684755 : switch (op->opcode)
1183 : : {
1184 : : case CALL_EXPR:
1185 : : return false;
1186 : :
1187 : : /* Record the base objects. */
1188 : 13825904 : case MEM_REF:
1189 : 13825904 : *op0_p = build2 (MEM_REF, op->type,
1190 : : NULL_TREE, op->op0);
1191 : 13825904 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1192 : 13825904 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1193 : 13825904 : op0_p = &TREE_OPERAND (*op0_p, 0);
1194 : 13825904 : break;
1195 : :
1196 : 294929 : case TARGET_MEM_REF:
1197 : 884787 : *op0_p = build5 (TARGET_MEM_REF, op->type,
1198 : : NULL_TREE, op->op2, op->op0,
1199 : 294929 : op->op1, ops[i+1].op0);
1200 : 294929 : MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1201 : 294929 : MR_DEPENDENCE_BASE (*op0_p) = op->base;
1202 : 294929 : op0_p = &TREE_OPERAND (*op0_p, 0);
1203 : 294929 : ++i;
1204 : 294929 : break;
1205 : :
1206 : : /* Unwrap some of the wrapped decls. */
1207 : 6705649 : case ADDR_EXPR:
1208 : : /* Apart from ADDR_EXPR arguments to MEM_REF. */
1209 : 6705649 : if (base != NULL_TREE
1210 : 6705649 : && TREE_CODE (base) == MEM_REF
1211 : 6671075 : && op->op0
1212 : 13376724 : && DECL_P (TREE_OPERAND (op->op0, 0)))
1213 : : {
1214 : 6668062 : const_vn_reference_op_t pop = &ops[i-1];
1215 : 6668062 : base = TREE_OPERAND (op->op0, 0);
1216 : 6668062 : if (known_eq (pop->off, -1))
1217 : : {
1218 : 55 : max_size = -1;
1219 : 55 : offset = 0;
1220 : : }
1221 : : else
1222 : 20004021 : offset += poly_offset_int (pop->off) * BITS_PER_UNIT;
1223 : : op0_p = NULL;
1224 : : break;
1225 : : }
1226 : : /* Fallthru. */
1227 : 7453357 : case PARM_DECL:
1228 : 7453357 : case CONST_DECL:
1229 : 7453357 : 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 : 7453357 : case VAR_DECL:
1233 : : /* ??? And for this only have DECL_HARD_REGISTER. */
1234 : 7453357 : case STRING_CST:
1235 : : /* This can show up in ARRAY_REF bases. */
1236 : 7453357 : case INTEGER_CST:
1237 : 7453357 : case SSA_NAME:
1238 : 7453357 : *op0_p = op->op0;
1239 : 7453357 : op0_p = NULL;
1240 : 7453357 : break;
1241 : :
1242 : : /* And now the usual component-reference style ops. */
1243 : 88805 : case BIT_FIELD_REF:
1244 : 88805 : offset += wi::to_poly_offset (op->op1);
1245 : 88805 : break;
1246 : :
1247 : 8036826 : case COMPONENT_REF:
1248 : 8036826 : {
1249 : 8036826 : 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 : 8036826 : tree this_offset = DECL_FIELD_OFFSET (field);
1254 : :
1255 : 8036826 : if (op->op1 || !poly_int_tree_p (this_offset))
1256 : 236 : max_size = -1;
1257 : : else
1258 : : {
1259 : 8036590 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1260 : 8036590 : << LOG2_BITS_PER_UNIT);
1261 : 8036590 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1262 : 8036590 : offset += woffset;
1263 : : }
1264 : : break;
1265 : : }
1266 : :
1267 : 2884866 : case ARRAY_RANGE_REF:
1268 : 2884866 : case ARRAY_REF:
1269 : : /* Use the recorded constant offset. */
1270 : 2884866 : if (maybe_eq (op->off, -1))
1271 : 1005605 : max_size = -1;
1272 : : else
1273 : 5637783 : offset += poly_offset_int (op->off) * BITS_PER_UNIT;
1274 : : break;
1275 : :
1276 : : case REALPART_EXPR:
1277 : : break;
1278 : :
1279 : : case IMAGPART_EXPR:
1280 : 40542111 : 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 : 14121419 : if (base == NULL_TREE)
1300 : : return false;
1301 : :
1302 : 14121419 : ref->ref = NULL_TREE;
1303 : 14121419 : ref->base = base;
1304 : 14121419 : ref->ref_alias_set = set;
1305 : 14121419 : ref->base_alias_set = base_set;
1306 : : /* We discount volatiles from value-numbering elsewhere. */
1307 : 14121419 : ref->volatile_p = false;
1308 : :
1309 : 14121419 : 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 : 14121419 : if (!offset.to_shwi (&ref->offset))
1318 : : {
1319 : 26 : ref->offset = 0;
1320 : 26 : ref->max_size = -1;
1321 : 26 : return true;
1322 : : }
1323 : :
1324 : 14121393 : if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1325 : 862386 : 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 : 9194175 : copy_reference_ops_from_call (gcall *call,
1335 : : vec<vn_reference_op_s> *result)
1336 : : {
1337 : 9194175 : vn_reference_op_s temp;
1338 : 9194175 : unsigned i;
1339 : 9194175 : tree lhs = gimple_call_lhs (call);
1340 : 9194175 : 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 : 9194175 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
1346 : : {
1347 : 469026 : memset (&temp, 0, sizeof (temp));
1348 : 469026 : temp.opcode = MODIFY_EXPR;
1349 : 469026 : temp.type = TREE_TYPE (lhs);
1350 : 469026 : temp.op0 = lhs;
1351 : 469026 : temp.off = -1;
1352 : 469026 : result->safe_push (temp);
1353 : : }
1354 : :
1355 : : /* Copy the type, opcode, function, static chain and EH region, if any. */
1356 : 9194175 : memset (&temp, 0, sizeof (temp));
1357 : 9194175 : temp.type = gimple_call_fntype (call);
1358 : 9194175 : temp.opcode = CALL_EXPR;
1359 : 9194175 : temp.op0 = gimple_call_fn (call);
1360 : 9194175 : if (gimple_call_internal_p (call))
1361 : 537920 : temp.clique = gimple_call_internal_fn (call);
1362 : 9194175 : temp.op1 = gimple_call_chain (call);
1363 : 9194175 : if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1364 : 724619 : temp.op2 = size_int (lr);
1365 : 9194175 : temp.off = -1;
1366 : 9194175 : result->safe_push (temp);
1367 : :
1368 : : /* Copy the call arguments. As they can be references as well,
1369 : : just chain them together. */
1370 : 27139579 : for (i = 0; i < gimple_call_num_args (call); ++i)
1371 : : {
1372 : 17945404 : tree callarg = gimple_call_arg (call, i);
1373 : 17945404 : copy_reference_ops_from_ref (callarg, result);
1374 : : }
1375 : 9194175 : }
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 : 129414932 : vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1381 : : unsigned int *i_p)
1382 : : {
1383 : 129414932 : unsigned int i = *i_p;
1384 : 129414932 : vn_reference_op_t op = &(*ops)[i];
1385 : 129414932 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1386 : 129414932 : tree addr_base;
1387 : 129414932 : 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 : 129414932 : addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1393 : : &addr_offset, vn_valueize);
1394 : 129414932 : gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1395 : 129414932 : if (addr_base != TREE_OPERAND (op->op0, 0))
1396 : : {
1397 : 658652 : poly_offset_int off
1398 : 658652 : = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1399 : : SIGNED)
1400 : 658652 : + addr_offset);
1401 : 658652 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1402 : 658652 : op->op0 = build_fold_addr_expr (addr_base);
1403 : 658652 : if (tree_fits_shwi_p (mem_op->op0))
1404 : 658583 : mem_op->off = tree_to_shwi (mem_op->op0);
1405 : : else
1406 : 69 : mem_op->off = -1;
1407 : 658652 : 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 : 86027055 : vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1416 : : unsigned int *i_p)
1417 : : {
1418 : 86027055 : bool changed = false;
1419 : 92723694 : vn_reference_op_t op;
1420 : :
1421 : 92723694 : do
1422 : : {
1423 : 92723694 : unsigned int i = *i_p;
1424 : 92723694 : op = &(*ops)[i];
1425 : 92723694 : vn_reference_op_t mem_op = &(*ops)[i - 1];
1426 : 92723694 : gimple *def_stmt;
1427 : 92723694 : enum tree_code code;
1428 : 92723694 : poly_offset_int off;
1429 : :
1430 : 92723694 : def_stmt = SSA_NAME_DEF_STMT (op->op0);
1431 : 92723694 : if (!is_gimple_assign (def_stmt))
1432 : 86025104 : return changed;
1433 : :
1434 : 37215629 : code = gimple_assign_rhs_code (def_stmt);
1435 : 37215629 : if (code != ADDR_EXPR
1436 : 37215629 : && code != POINTER_PLUS_EXPR)
1437 : : return changed;
1438 : :
1439 : 18934561 : 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 : 18934561 : if (code == ADDR_EXPR)
1445 : : {
1446 : 924519 : tree addr, addr_base;
1447 : 924519 : poly_int64 addr_offset;
1448 : :
1449 : 924519 : addr = gimple_assign_rhs1 (def_stmt);
1450 : 924519 : 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 : 924519 : if (!addr_base
1457 : 267558 : && *i_p == ops->length () - 1
1458 : 133779 : && 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 : 1007200 : && default_vn_walk_kind == VN_WALKREWRITE)
1463 : : {
1464 : 82583 : auto_vec<vn_reference_op_s, 32> tem;
1465 : 82583 : 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 : 82583 : if (tem.length () >= 2
1470 : 82583 : && tem[tem.length () - 2].opcode == MEM_REF)
1471 : : {
1472 : 82568 : vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1473 : 82568 : new_mem_op->op0
1474 : 82568 : = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1475 : 165136 : wi::to_poly_wide (new_mem_op->op0));
1476 : : }
1477 : : else
1478 : 15 : gcc_assert (tem.last ().opcode == STRING_CST);
1479 : 82583 : ops->pop ();
1480 : 82583 : ops->pop ();
1481 : 82583 : ops->safe_splice (tem);
1482 : 82583 : --*i_p;
1483 : 82583 : return true;
1484 : 82583 : }
1485 : 841936 : if (!addr_base
1486 : 790740 : || TREE_CODE (addr_base) != MEM_REF
1487 : 1630817 : || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1488 : 787020 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1489 : : 0))))
1490 : : return changed;
1491 : :
1492 : 788881 : off += addr_offset;
1493 : 788881 : off += mem_ref_offset (addr_base);
1494 : 788881 : op->op0 = TREE_OPERAND (addr_base, 0);
1495 : : }
1496 : : else
1497 : : {
1498 : 18010042 : tree ptr, ptroff;
1499 : 18010042 : ptr = gimple_assign_rhs1 (def_stmt);
1500 : 18010042 : ptroff = gimple_assign_rhs2 (def_stmt);
1501 : 18010042 : if (TREE_CODE (ptr) != SSA_NAME
1502 : 16301368 : || 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 : 16299954 : || SSA_VAL (ptr) == op->op0
1507 : 34309996 : || !poly_int_tree_p (ptroff))
1508 : 12100333 : return changed;
1509 : :
1510 : 5909709 : off += wi::to_poly_offset (ptroff);
1511 : 5909709 : op->op0 = ptr;
1512 : : }
1513 : :
1514 : 6698590 : mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1515 : 6698590 : if (tree_fits_shwi_p (mem_op->op0))
1516 : 6437413 : mem_op->off = tree_to_shwi (mem_op->op0);
1517 : : else
1518 : 261177 : mem_op->off = -1;
1519 : : /* ??? Can end up with endless recursion here!?
1520 : : gcc.c-torture/execute/strcmp-1.c */
1521 : 6698590 : if (TREE_CODE (op->op0) == SSA_NAME)
1522 : 6696729 : op->op0 = SSA_VAL (op->op0);
1523 : 6698590 : if (TREE_CODE (op->op0) != SSA_NAME)
1524 : 1951 : op->opcode = TREE_CODE (op->op0);
1525 : :
1526 : 6698590 : changed = true;
1527 : : }
1528 : : /* Tail-recurse. */
1529 : 6698590 : 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 : 110107428 : fully_constant_vn_reference_p (vn_reference_t ref)
1543 : : {
1544 : 110107428 : vec<vn_reference_op_s> operands = ref->operands;
1545 : 110107428 : 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 : 110107428 : op = &operands[0];
1550 : 110107428 : if (op->opcode == CALL_EXPR
1551 : 85954 : && (!op->op0
1552 : 78841 : || (TREE_CODE (op->op0) == ADDR_EXPR
1553 : 78841 : && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1554 : 78841 : && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1555 : : BUILT_IN_NORMAL)))
1556 : 69796 : && operands.length () >= 2
1557 : 110177180 : && operands.length () <= 3)
1558 : : {
1559 : 36732 : vn_reference_op_t arg0, arg1 = NULL;
1560 : 36732 : bool anyconst = false;
1561 : 36732 : arg0 = &operands[1];
1562 : 36732 : if (operands.length () > 2)
1563 : 5440 : arg1 = &operands[2];
1564 : 36732 : if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1565 : 36732 : || (arg0->opcode == ADDR_EXPR
1566 : 13459 : && is_gimple_min_invariant (arg0->op0)))
1567 : : anyconst = true;
1568 : 36732 : if (arg1
1569 : 36732 : && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1570 : 3956 : || (arg1->opcode == ADDR_EXPR
1571 : 579 : && is_gimple_min_invariant (arg1->op0))))
1572 : : anyconst = true;
1573 : 34669 : if (anyconst)
1574 : : {
1575 : 21521 : combined_fn fn;
1576 : 21521 : if (op->op0)
1577 : 20536 : fn = as_combined_fn (DECL_FUNCTION_CODE
1578 : 20536 : (TREE_OPERAND (op->op0, 0)));
1579 : : else
1580 : 985 : fn = as_combined_fn ((internal_fn) op->clique);
1581 : 21521 : tree folded;
1582 : 21521 : if (arg1)
1583 : 2653 : folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1584 : : else
1585 : 18868 : folded = fold_const_call (fn, ref->type, arg0->op0);
1586 : 21521 : if (folded
1587 : 21521 : && is_gimple_min_invariant (folded))
1588 : : return folded;
1589 : : }
1590 : : }
1591 : :
1592 : : /* Simplify reads from constants or constant initializers. */
1593 : 110070696 : else if (BITS_PER_UNIT == 8
1594 : 110070696 : && ref->type
1595 : 110070696 : && COMPLETE_TYPE_P (ref->type)
1596 : 220141350 : && is_gimple_reg_type (ref->type))
1597 : : {
1598 : 105305749 : poly_int64 off = 0;
1599 : 105305749 : HOST_WIDE_INT size;
1600 : 105305749 : if (INTEGRAL_TYPE_P (ref->type))
1601 : 53446992 : size = TYPE_PRECISION (ref->type);
1602 : 51858757 : else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1603 : 51858757 : size = tree_to_shwi (TYPE_SIZE (ref->type));
1604 : : else
1605 : 110107428 : return NULL_TREE;
1606 : 105305749 : if (size % BITS_PER_UNIT != 0
1607 : 103258625 : || size > MAX_BITSIZE_MODE_ANY_MODE)
1608 : : return NULL_TREE;
1609 : 103257312 : size /= BITS_PER_UNIT;
1610 : 103257312 : unsigned i;
1611 : 190609610 : for (i = 0; i < operands.length (); ++i)
1612 : : {
1613 : 190609610 : if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1614 : : {
1615 : 309 : ++i;
1616 : 309 : break;
1617 : : }
1618 : 190609301 : if (operands[i].reverse)
1619 : : return NULL_TREE;
1620 : 190600955 : if (known_eq (operands[i].off, -1))
1621 : : return NULL_TREE;
1622 : 176875163 : off += operands[i].off;
1623 : 176875163 : if (operands[i].opcode == MEM_REF)
1624 : : {
1625 : 89522865 : ++i;
1626 : 89522865 : break;
1627 : : }
1628 : : }
1629 : 89523174 : vn_reference_op_t base = &operands[--i];
1630 : 89523174 : tree ctor = error_mark_node;
1631 : 89523174 : tree decl = NULL_TREE;
1632 : 89523174 : if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1633 : 309 : ctor = base->op0;
1634 : 89522865 : else if (base->opcode == MEM_REF
1635 : 89522865 : && base[1].opcode == ADDR_EXPR
1636 : 147215000 : && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1637 : 3612753 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1638 : 3612693 : || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1639 : : {
1640 : 54084349 : decl = TREE_OPERAND (base[1].op0, 0);
1641 : 54084349 : if (TREE_CODE (decl) == STRING_CST)
1642 : : ctor = decl;
1643 : : else
1644 : 54079442 : ctor = ctor_for_folding (decl);
1645 : : }
1646 : 89518267 : if (ctor == NULL_TREE)
1647 : 379 : return build_zero_cst (ref->type);
1648 : 89522795 : else if (ctor != error_mark_node)
1649 : : {
1650 : 99378 : HOST_WIDE_INT const_off;
1651 : 99378 : if (decl)
1652 : : {
1653 : 198138 : tree res = fold_ctor_reference (ref->type, ctor,
1654 : 99069 : off * BITS_PER_UNIT,
1655 : 99069 : size * BITS_PER_UNIT, decl);
1656 : 99069 : if (res)
1657 : : {
1658 : 54375 : STRIP_USELESS_TYPE_CONVERSION (res);
1659 : 54375 : if (is_gimple_min_invariant (res))
1660 : 110107428 : return res;
1661 : : }
1662 : : }
1663 : 309 : else if (off.is_constant (&const_off))
1664 : : {
1665 : 309 : unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1666 : 309 : int len = native_encode_expr (ctor, buf, size, const_off);
1667 : 309 : if (len > 0)
1668 : 139 : return native_interpret_expr (ref->type, buf, len);
1669 : : }
1670 : : }
1671 : : }
1672 : :
1673 : : return NULL_TREE;
1674 : : }
1675 : :
1676 : : /* Return true if OPS contain a storage order barrier. */
1677 : :
1678 : : static bool
1679 : 60012121 : contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1680 : : {
1681 : 60012121 : vn_reference_op_t op;
1682 : 60012121 : unsigned i;
1683 : :
1684 : 235354786 : FOR_EACH_VEC_ELT (ops, i, op)
1685 : 175342665 : if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1686 : : return true;
1687 : :
1688 : : return false;
1689 : : }
1690 : :
1691 : : /* Return true if OPS represent an access with reverse storage order. */
1692 : :
1693 : : static bool
1694 : 60020302 : reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
1695 : : {
1696 : 60020302 : unsigned i = 0;
1697 : 60020302 : if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1698 : : ++i;
1699 : 60020302 : switch (ops[i].opcode)
1700 : : {
1701 : 57965288 : case ARRAY_REF:
1702 : 57965288 : case COMPONENT_REF:
1703 : 57965288 : case BIT_FIELD_REF:
1704 : 57965288 : case MEM_REF:
1705 : 57965288 : return ops[i].reverse;
1706 : : default:
1707 : : return false;
1708 : : }
1709 : : }
1710 : :
1711 : : /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1712 : : structures into their value numbers. This is done in-place, and
1713 : : the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1714 : : whether any operands were valueized. */
1715 : :
1716 : : static void
1717 : 222981527 : valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1718 : : bool with_avail = false)
1719 : : {
1720 : 222981527 : *valueized_anything = false;
1721 : :
1722 : 898272548 : for (unsigned i = 0; i < orig->length (); ++i)
1723 : : {
1724 : 675291021 : re_valueize:
1725 : 678864298 : vn_reference_op_t vro = &(*orig)[i];
1726 : 678864298 : if (vro->opcode == SSA_NAME
1727 : 580990234 : || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1728 : : {
1729 : 122345197 : tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1730 : 122345197 : if (tem != vro->op0)
1731 : : {
1732 : 17654156 : *valueized_anything = true;
1733 : 17654156 : vro->op0 = tem;
1734 : : }
1735 : : /* If it transforms from an SSA_NAME to a constant, update
1736 : : the opcode. */
1737 : 122345197 : if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1738 : 2070970 : vro->opcode = TREE_CODE (vro->op0);
1739 : : }
1740 : 678864298 : if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1741 : : {
1742 : 26327 : tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1743 : 26327 : if (tem != vro->op1)
1744 : : {
1745 : 584 : *valueized_anything = true;
1746 : 584 : vro->op1 = tem;
1747 : : }
1748 : : }
1749 : 678864298 : if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1750 : : {
1751 : 205423 : tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1752 : 205423 : if (tem != vro->op2)
1753 : : {
1754 : 119505 : *valueized_anything = true;
1755 : 119505 : vro->op2 = tem;
1756 : : }
1757 : : }
1758 : : /* If it transforms from an SSA_NAME to an address, fold with
1759 : : a preceding indirect reference. */
1760 : 678864298 : if (i > 0
1761 : 455805745 : && vro->op0
1762 : 452303124 : && TREE_CODE (vro->op0) == ADDR_EXPR
1763 : 814201472 : && (*orig)[i - 1].opcode == MEM_REF)
1764 : : {
1765 : 129414671 : if (vn_reference_fold_indirect (orig, &i))
1766 : 658652 : *valueized_anything = true;
1767 : : }
1768 : 549449627 : else if (i > 0
1769 : 326391074 : && vro->opcode == SSA_NAME
1770 : 645252721 : && (*orig)[i - 1].opcode == MEM_REF)
1771 : : {
1772 : 86027055 : if (vn_reference_maybe_forwprop_address (orig, &i))
1773 : : {
1774 : 3573277 : *valueized_anything = true;
1775 : : /* Re-valueize the current operand. */
1776 : 3573277 : goto re_valueize;
1777 : : }
1778 : : }
1779 : : /* If it transforms a non-constant ARRAY_REF into a constant
1780 : : one, adjust the constant offset. */
1781 : 463422572 : else if ((vro->opcode == ARRAY_REF
1782 : 463422572 : || vro->opcode == ARRAY_RANGE_REF)
1783 : 39662344 : && known_eq (vro->off, -1)
1784 : 17233062 : && poly_int_tree_p (vro->op0)
1785 : 4603733 : && poly_int_tree_p (vro->op1)
1786 : 468026305 : && TREE_CODE (vro->op2) == INTEGER_CST)
1787 : : {
1788 : : /* Prohibit value-numbering addresses of one-after-the-last
1789 : : element ARRAY_REFs the same as addresses of other components
1790 : : before the pass folding __builtin_object_size had a chance
1791 : : to run. */
1792 : 4470035 : if (!(cfun->curr_properties & PROP_objsz)
1793 : 5688160 : && (*orig)[0].opcode == ADDR_EXPR)
1794 : : {
1795 : 34161 : tree dom = TYPE_DOMAIN ((*orig)[i + 1].type);
1796 : 51860 : if (!dom
1797 : 34011 : || !TYPE_MAX_VALUE (dom)
1798 : 24392 : || !poly_int_tree_p (TYPE_MAX_VALUE (dom))
1799 : 50713 : || integer_minus_onep (TYPE_MAX_VALUE (dom)))
1800 : 18506 : continue;
1801 : 16462 : if (!known_le (wi::to_poly_offset (vro->op0),
1802 : : wi::to_poly_offset (TYPE_MAX_VALUE (dom))))
1803 : 807 : continue;
1804 : : }
1805 : :
1806 : 8903058 : poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1807 : 13354587 : - wi::to_poly_offset (vro->op1))
1808 : 8903058 : * wi::to_offset (vro->op2)
1809 : 4451529 : * vn_ref_op_align_unit (vro));
1810 : 4451529 : off.to_shwi (&vro->off);
1811 : : }
1812 : : }
1813 : 222981527 : }
1814 : :
1815 : : static void
1816 : 14824071 : valueize_refs (vec<vn_reference_op_s> *orig)
1817 : : {
1818 : 14824071 : bool tem;
1819 : 0 : valueize_refs_1 (orig, &tem);
1820 : 0 : }
1821 : :
1822 : : static vec<vn_reference_op_s> shared_lookup_references;
1823 : :
1824 : : /* Create a vector of vn_reference_op_s structures from REF, a
1825 : : REFERENCE_CLASS_P tree. The vector is shared among all callers of
1826 : : this function. *VALUEIZED_ANYTHING will specify whether any
1827 : : operands were valueized. */
1828 : :
1829 : : static vec<vn_reference_op_s>
1830 : 181434943 : valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1831 : : {
1832 : 181434943 : if (!ref)
1833 : 0 : return vNULL;
1834 : 181434943 : shared_lookup_references.truncate (0);
1835 : 181434943 : copy_reference_ops_from_ref (ref, &shared_lookup_references);
1836 : 181434943 : valueize_refs_1 (&shared_lookup_references, valueized_anything);
1837 : 181434943 : return shared_lookup_references;
1838 : : }
1839 : :
1840 : : /* Create a vector of vn_reference_op_s structures from CALL, a
1841 : : call statement. The vector is shared among all callers of
1842 : : this function. */
1843 : :
1844 : : static vec<vn_reference_op_s>
1845 : 9194175 : valueize_shared_reference_ops_from_call (gcall *call)
1846 : : {
1847 : 9194175 : if (!call)
1848 : 0 : return vNULL;
1849 : 9194175 : shared_lookup_references.truncate (0);
1850 : 9194175 : copy_reference_ops_from_call (call, &shared_lookup_references);
1851 : 9194175 : valueize_refs (&shared_lookup_references);
1852 : 9194175 : return shared_lookup_references;
1853 : : }
1854 : :
1855 : : /* Lookup a SCCVN reference operation VR in the current hash table.
1856 : : Returns the resulting value number if it exists in the hash table,
1857 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
1858 : : vn_reference_t stored in the hashtable if something is found. */
1859 : :
1860 : : static tree
1861 : 65600451 : vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1862 : : {
1863 : 65600451 : vn_reference_s **slot;
1864 : 65600451 : hashval_t hash;
1865 : :
1866 : 65600451 : hash = vr->hashcode;
1867 : 65600451 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1868 : 65600451 : if (slot)
1869 : : {
1870 : 8523715 : if (vnresult)
1871 : 8523715 : *vnresult = (vn_reference_t)*slot;
1872 : 8523715 : return ((vn_reference_t)*slot)->result;
1873 : : }
1874 : :
1875 : : return NULL_TREE;
1876 : : }
1877 : :
1878 : :
1879 : : /* Partial definition tracking support. */
1880 : :
1881 : : struct pd_range
1882 : : {
1883 : : HOST_WIDE_INT offset;
1884 : : HOST_WIDE_INT size;
1885 : : pd_range *m_children[2];
1886 : : };
1887 : :
1888 : : struct pd_data
1889 : : {
1890 : : tree rhs;
1891 : : HOST_WIDE_INT rhs_off;
1892 : : HOST_WIDE_INT offset;
1893 : : HOST_WIDE_INT size;
1894 : : };
1895 : :
1896 : : /* Context for alias walking. */
1897 : :
1898 : : struct vn_walk_cb_data
1899 : : {
1900 : 61589797 : vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1901 : : vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1902 : : bool redundant_store_removal_p_)
1903 : 61589797 : : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1904 : 61589797 : mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1905 : 61589797 : vn_walk_kind (vn_walk_kind_),
1906 : 61589797 : tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
1907 : 123179594 : saved_operands (vNULL), first_range (), first_set (-2),
1908 : 123179594 : first_base_set (-2)
1909 : : {
1910 : 61589797 : if (!last_vuse_ptr)
1911 : 28462313 : last_vuse_ptr = &last_vuse;
1912 : 61589797 : ao_ref_init (&orig_ref, orig_ref_);
1913 : 61589797 : if (mask)
1914 : : {
1915 : 369053 : wide_int w = wi::to_wide (mask);
1916 : 369053 : unsigned int pos = 0, prec = w.get_precision ();
1917 : 369053 : pd_data pd;
1918 : 369053 : pd.rhs = build_constructor (NULL_TREE, NULL);
1919 : 369053 : pd.rhs_off = 0;
1920 : : /* When bitwise and with a constant is done on a memory load,
1921 : : we don't really need all the bits to be defined or defined
1922 : : to constants, we don't really care what is in the position
1923 : : corresponding to 0 bits in the mask.
1924 : : So, push the ranges of those 0 bits in the mask as artificial
1925 : : zero stores and let the partial def handling code do the
1926 : : rest. */
1927 : 785787 : while (pos < prec)
1928 : : {
1929 : 764780 : int tz = wi::ctz (w);
1930 : 764780 : if (pos + tz > prec)
1931 : 348046 : tz = prec - pos;
1932 : 764780 : if (tz)
1933 : : {
1934 : 590407 : if (BYTES_BIG_ENDIAN)
1935 : : pd.offset = prec - pos - tz;
1936 : : else
1937 : 590407 : pd.offset = pos;
1938 : 590407 : pd.size = tz;
1939 : 590407 : void *r = push_partial_def (pd, 0, 0, 0, prec);
1940 : 590407 : gcc_assert (r == NULL_TREE);
1941 : : }
1942 : 764780 : pos += tz;
1943 : 764780 : if (pos == prec)
1944 : : break;
1945 : 416734 : w = wi::lrshift (w, tz);
1946 : 416734 : tz = wi::ctz (wi::bit_not (w));
1947 : 416734 : if (pos + tz > prec)
1948 : 0 : tz = prec - pos;
1949 : 416734 : pos += tz;
1950 : 416734 : w = wi::lrshift (w, tz);
1951 : : }
1952 : 369053 : }
1953 : 61589797 : }
1954 : : ~vn_walk_cb_data ();
1955 : : void *finish (alias_set_type, alias_set_type, tree);
1956 : : void *push_partial_def (pd_data pd,
1957 : : alias_set_type, alias_set_type, HOST_WIDE_INT,
1958 : : HOST_WIDE_INT);
1959 : :
1960 : : vn_reference_t vr;
1961 : : ao_ref orig_ref;
1962 : : tree *last_vuse_ptr;
1963 : : tree last_vuse;
1964 : : tree mask;
1965 : : tree masked_result;
1966 : : tree same_val;
1967 : : vn_lookup_kind vn_walk_kind;
1968 : : bool tbaa_p;
1969 : : bool redundant_store_removal_p;
1970 : : vec<vn_reference_op_s> saved_operands;
1971 : :
1972 : : /* The VDEFs of partial defs we come along. */
1973 : : auto_vec<pd_data, 2> partial_defs;
1974 : : /* The first defs range to avoid splay tree setup in most cases. */
1975 : : pd_range first_range;
1976 : : alias_set_type first_set;
1977 : : alias_set_type first_base_set;
1978 : : default_splay_tree<pd_range *> known_ranges;
1979 : : obstack ranges_obstack;
1980 : : static constexpr HOST_WIDE_INT bufsize = 64;
1981 : : };
1982 : :
1983 : 61589797 : vn_walk_cb_data::~vn_walk_cb_data ()
1984 : : {
1985 : 61589797 : if (known_ranges)
1986 : 212284 : obstack_free (&ranges_obstack, NULL);
1987 : 61589797 : saved_operands.release ();
1988 : 61589797 : }
1989 : :
1990 : : void *
1991 : 1470373 : vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
1992 : : {
1993 : 1470373 : if (first_set != -2)
1994 : : {
1995 : 353712 : set = first_set;
1996 : 353712 : base_set = first_base_set;
1997 : : }
1998 : 1470373 : if (mask)
1999 : : {
2000 : 690 : masked_result = val;
2001 : 690 : return (void *) -1;
2002 : : }
2003 : 1469683 : if (same_val && !operand_equal_p (val, same_val))
2004 : : return (void *) -1;
2005 : 1466044 : vec<vn_reference_op_s> &operands
2006 : 1466044 : = saved_operands.exists () ? saved_operands : vr->operands;
2007 : 1466044 : return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
2008 : : vr->offset, vr->max_size,
2009 : 1466044 : vr->type, operands, val);
2010 : : }
2011 : :
2012 : : /* Push PD to the vector of partial definitions returning a
2013 : : value when we are ready to combine things with VUSE, SET and MAXSIZEI,
2014 : : NULL when we want to continue looking for partial defs or -1
2015 : : on failure. */
2016 : :
2017 : : void *
2018 : 664324 : vn_walk_cb_data::push_partial_def (pd_data pd,
2019 : : alias_set_type set, alias_set_type base_set,
2020 : : HOST_WIDE_INT offseti,
2021 : : HOST_WIDE_INT maxsizei)
2022 : : {
2023 : : /* We're using a fixed buffer for encoding so fail early if the object
2024 : : we want to interpret is bigger. */
2025 : 664324 : if (maxsizei > bufsize * BITS_PER_UNIT
2026 : : || CHAR_BIT != 8
2027 : : || BITS_PER_UNIT != 8
2028 : : /* Not prepared to handle PDP endian. */
2029 : : || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2030 : : return (void *)-1;
2031 : :
2032 : : /* Turn too large constant stores into non-constant stores. */
2033 : 664253 : if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
2034 : 0 : pd.rhs = error_mark_node;
2035 : :
2036 : : /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
2037 : : most a partial byte before and/or after the region. */
2038 : 664253 : if (!CONSTANT_CLASS_P (pd.rhs))
2039 : : {
2040 : 629806 : if (pd.offset < offseti)
2041 : : {
2042 : 6250 : HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2043 : 6250 : gcc_assert (pd.size > o);
2044 : 6250 : pd.size -= o;
2045 : 6250 : pd.offset += o;
2046 : : }
2047 : 629806 : if (pd.size > maxsizei)
2048 : 5452 : pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
2049 : : }
2050 : :
2051 : 664253 : pd.offset -= offseti;
2052 : :
2053 : 1328506 : bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2054 : 664253 : || CONSTANT_CLASS_P (pd.rhs));
2055 : 664253 : pd_range *r;
2056 : 664253 : if (partial_defs.is_empty ())
2057 : : {
2058 : : /* If we get a clobber upfront, fail. */
2059 : 418966 : if (TREE_CLOBBER_P (pd.rhs))
2060 : : return (void *)-1;
2061 : 418602 : if (!pd_constant_p)
2062 : : return (void *)-1;
2063 : 388599 : partial_defs.safe_push (pd);
2064 : 388599 : first_range.offset = pd.offset;
2065 : 388599 : first_range.size = pd.size;
2066 : 388599 : first_set = set;
2067 : 388599 : first_base_set = base_set;
2068 : 388599 : last_vuse_ptr = NULL;
2069 : 388599 : r = &first_range;
2070 : : /* Go check if the first partial definition was a full one in case
2071 : : the caller didn't optimize for this. */
2072 : : }
2073 : : else
2074 : : {
2075 : 245287 : if (!known_ranges)
2076 : : {
2077 : : /* ??? Optimize the case where the 2nd partial def completes
2078 : : things. */
2079 : 212284 : gcc_obstack_init (&ranges_obstack);
2080 : 212284 : known_ranges.insert_max_node (&first_range);
2081 : : }
2082 : : /* Lookup the offset and see if we need to merge. */
2083 : 245287 : int comparison = known_ranges.lookup_le
2084 : 495199 : ([&] (pd_range *r) { return pd.offset < r->offset; },
2085 : 225555 : [&] (pd_range *r) { return pd.offset > r->offset; });
2086 : 245287 : r = known_ranges.root ();
2087 : 245287 : if (comparison >= 0
2088 : 245287 : && ranges_known_overlap_p (r->offset, r->size + 1,
2089 : : pd.offset, pd.size))
2090 : : {
2091 : : /* Ignore partial defs already covered. Here we also drop shadowed
2092 : : clobbers arriving here at the floor. */
2093 : 3875 : if (known_subrange_p (pd.offset, pd.size, r->offset, r->size))
2094 : : return NULL;
2095 : 3203 : r->size = MAX (r->offset + r->size, pd.offset + pd.size) - r->offset;
2096 : : }
2097 : : else
2098 : : {
2099 : : /* pd.offset wasn't covered yet, insert the range. */
2100 : 241412 : void *addr = XOBNEW (&ranges_obstack, pd_range);
2101 : 241412 : r = new (addr) pd_range { pd.offset, pd.size, {} };
2102 : 241412 : known_ranges.insert_relative (comparison, r);
2103 : : }
2104 : : /* Merge r which now contains pd's range and is a member of the splay
2105 : : tree with adjacent overlapping ranges. */
2106 : 244615 : if (known_ranges.splay_next_node ())
2107 : 21804 : do
2108 : : {
2109 : 21804 : pd_range *rafter = known_ranges.root ();
2110 : 21804 : if (!ranges_known_overlap_p (r->offset, r->size + 1,
2111 : 21804 : rafter->offset, rafter->size))
2112 : : break;
2113 : 21410 : r->size = MAX (r->offset + r->size,
2114 : 21410 : rafter->offset + rafter->size) - r->offset;
2115 : : }
2116 : 21410 : while (known_ranges.remove_root_and_splay_next ());
2117 : : /* If we get a clobber, fail. */
2118 : 244615 : if (TREE_CLOBBER_P (pd.rhs))
2119 : : return (void *)-1;
2120 : : /* Non-constants are OK as long as they are shadowed by a constant. */
2121 : 244187 : if (!pd_constant_p)
2122 : : return (void *)-1;
2123 : 237382 : partial_defs.safe_push (pd);
2124 : : }
2125 : :
2126 : : /* Now we have merged pd's range into the range tree. When we have covered
2127 : : [offseti, sizei] then the tree will contain exactly one node which has
2128 : : the desired properties and it will be 'r'. */
2129 : 625981 : if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2130 : : /* Continue looking for partial defs. */
2131 : : return NULL;
2132 : :
2133 : : /* Now simply native encode all partial defs in reverse order. */
2134 : 8379 : unsigned ndefs = partial_defs.length ();
2135 : : /* We support up to 512-bit values (for V8DFmode). */
2136 : 8379 : unsigned char buffer[bufsize + 1];
2137 : 8379 : unsigned char this_buffer[bufsize + 1];
2138 : 8379 : int len;
2139 : :
2140 : 8379 : memset (buffer, 0, bufsize + 1);
2141 : 8379 : unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2142 : 40756 : while (!partial_defs.is_empty ())
2143 : : {
2144 : 23998 : pd_data pd = partial_defs.pop ();
2145 : 23998 : unsigned int amnt;
2146 : 23998 : if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2147 : : {
2148 : : /* Empty CONSTRUCTOR. */
2149 : 2195 : if (pd.size >= needed_len * BITS_PER_UNIT)
2150 : 2195 : len = needed_len;
2151 : : else
2152 : 2017 : len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2153 : 2195 : memset (this_buffer, 0, len);
2154 : : }
2155 : 21803 : else if (pd.rhs_off >= 0)
2156 : : {
2157 : 43606 : len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2158 : 21803 : (MAX (0, -pd.offset)
2159 : 21803 : + pd.rhs_off) / BITS_PER_UNIT);
2160 : 21803 : if (len <= 0
2161 : 21803 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2162 : 21803 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2163 : : {
2164 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2165 : 0 : fprintf (dump_file, "Failed to encode %u "
2166 : : "partial definitions\n", ndefs);
2167 : 0 : return (void *)-1;
2168 : : }
2169 : : }
2170 : : else /* negative pd.rhs_off indicates we want to chop off first bits */
2171 : : {
2172 : 0 : if (-pd.rhs_off >= bufsize)
2173 : : return (void *)-1;
2174 : 0 : len = native_encode_expr (pd.rhs,
2175 : 0 : this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2176 : 0 : bufsize - -pd.rhs_off / BITS_PER_UNIT,
2177 : 0 : MAX (0, -pd.offset) / BITS_PER_UNIT);
2178 : 0 : if (len <= 0
2179 : 0 : || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2180 : 0 : - MAX (0, -pd.offset) / BITS_PER_UNIT))
2181 : : {
2182 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2183 : 0 : fprintf (dump_file, "Failed to encode %u "
2184 : : "partial definitions\n", ndefs);
2185 : 0 : return (void *)-1;
2186 : : }
2187 : : }
2188 : :
2189 : 23998 : unsigned char *p = buffer;
2190 : 23998 : HOST_WIDE_INT size = pd.size;
2191 : 23998 : if (pd.offset < 0)
2192 : 230 : size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2193 : 23998 : this_buffer[len] = 0;
2194 : 23998 : if (BYTES_BIG_ENDIAN)
2195 : : {
2196 : : /* LSB of this_buffer[len - 1] byte should be at
2197 : : pd.offset + pd.size - 1 bits in buffer. */
2198 : : amnt = ((unsigned HOST_WIDE_INT) pd.offset
2199 : : + pd.size) % BITS_PER_UNIT;
2200 : : if (amnt)
2201 : : shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2202 : : unsigned char *q = this_buffer;
2203 : : unsigned int off = 0;
2204 : : if (pd.offset >= 0)
2205 : : {
2206 : : unsigned int msk;
2207 : : off = pd.offset / BITS_PER_UNIT;
2208 : : gcc_assert (off < needed_len);
2209 : : p = buffer + off;
2210 : : if (size <= amnt)
2211 : : {
2212 : : msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2213 : : *p = (*p & ~msk) | (this_buffer[len] & msk);
2214 : : size = 0;
2215 : : }
2216 : : else
2217 : : {
2218 : : if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2219 : : q = (this_buffer + len
2220 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2221 : : / BITS_PER_UNIT));
2222 : : if (pd.offset % BITS_PER_UNIT)
2223 : : {
2224 : : msk = -1U << (BITS_PER_UNIT
2225 : : - (pd.offset % BITS_PER_UNIT));
2226 : : *p = (*p & msk) | (*q & ~msk);
2227 : : p++;
2228 : : q++;
2229 : : off++;
2230 : : size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2231 : : gcc_assert (size >= 0);
2232 : : }
2233 : : }
2234 : : }
2235 : : else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2236 : : {
2237 : : q = (this_buffer + len
2238 : : - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2239 : : / BITS_PER_UNIT));
2240 : : if (pd.offset % BITS_PER_UNIT)
2241 : : {
2242 : : q++;
2243 : : size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2244 : : % BITS_PER_UNIT);
2245 : : gcc_assert (size >= 0);
2246 : : }
2247 : : }
2248 : : if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2249 : : > needed_len)
2250 : : size = (needed_len - off) * BITS_PER_UNIT;
2251 : : memcpy (p, q, size / BITS_PER_UNIT);
2252 : : if (size % BITS_PER_UNIT)
2253 : : {
2254 : : unsigned int msk
2255 : : = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2256 : : p += size / BITS_PER_UNIT;
2257 : : q += size / BITS_PER_UNIT;
2258 : : *p = (*q & msk) | (*p & ~msk);
2259 : : }
2260 : : }
2261 : : else
2262 : : {
2263 : 23998 : if (pd.offset >= 0)
2264 : : {
2265 : : /* LSB of this_buffer[0] byte should be at pd.offset bits
2266 : : in buffer. */
2267 : 23768 : unsigned int msk;
2268 : 23768 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2269 : 23768 : amnt = pd.offset % BITS_PER_UNIT;
2270 : 23768 : if (amnt)
2271 : 1794 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2272 : 23768 : unsigned int off = pd.offset / BITS_PER_UNIT;
2273 : 23768 : gcc_assert (off < needed_len);
2274 : 23768 : size = MIN (size,
2275 : : (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2276 : 23768 : p = buffer + off;
2277 : 23768 : if (amnt + size < BITS_PER_UNIT)
2278 : : {
2279 : : /* Low amnt bits come from *p, then size bits
2280 : : from this_buffer[0] and the remaining again from
2281 : : *p. */
2282 : 1376 : msk = ((1 << size) - 1) << amnt;
2283 : 1376 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2284 : 1376 : size = 0;
2285 : : }
2286 : 22392 : else if (amnt)
2287 : : {
2288 : 1365 : msk = -1U << amnt;
2289 : 1365 : *p = (*p & ~msk) | (this_buffer[0] & msk);
2290 : 1365 : p++;
2291 : 1365 : size -= (BITS_PER_UNIT - amnt);
2292 : : }
2293 : : }
2294 : : else
2295 : : {
2296 : 230 : amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2297 : 230 : if (amnt)
2298 : 16 : size -= BITS_PER_UNIT - amnt;
2299 : 230 : size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2300 : 230 : if (amnt)
2301 : 16 : shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2302 : : }
2303 : 23998 : memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2304 : 23998 : p += size / BITS_PER_UNIT;
2305 : 23998 : if (size % BITS_PER_UNIT)
2306 : : {
2307 : 621 : unsigned int msk = -1U << (size % BITS_PER_UNIT);
2308 : 621 : *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2309 : 621 : & ~msk) | (*p & msk);
2310 : : }
2311 : : }
2312 : : }
2313 : :
2314 : 8379 : tree type = vr->type;
2315 : : /* Make sure to interpret in a type that has a range covering the whole
2316 : : access size. */
2317 : 8379 : if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2318 : 13 : type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2319 : 8379 : tree val;
2320 : 8379 : if (BYTES_BIG_ENDIAN)
2321 : : {
2322 : : unsigned sz = needed_len;
2323 : : if (maxsizei % BITS_PER_UNIT)
2324 : : shift_bytes_in_array_right (buffer, needed_len,
2325 : : BITS_PER_UNIT
2326 : : - (maxsizei % BITS_PER_UNIT));
2327 : : if (INTEGRAL_TYPE_P (type))
2328 : : {
2329 : : if (TYPE_MODE (type) != BLKmode)
2330 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2331 : : else
2332 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2333 : : }
2334 : : if (sz > needed_len)
2335 : : {
2336 : : memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2337 : : val = native_interpret_expr (type, this_buffer, sz);
2338 : : }
2339 : : else
2340 : : val = native_interpret_expr (type, buffer, needed_len);
2341 : : }
2342 : : else
2343 : 8379 : val = native_interpret_expr (type, buffer, bufsize);
2344 : : /* If we chop off bits because the types precision doesn't match the memory
2345 : : access size this is ok when optimizing reads but not when called from
2346 : : the DSE code during elimination. */
2347 : 8379 : if (val && type != vr->type)
2348 : : {
2349 : 13 : if (! int_fits_type_p (val, vr->type))
2350 : : val = NULL_TREE;
2351 : : else
2352 : 13 : val = fold_convert (vr->type, val);
2353 : : }
2354 : :
2355 : 8375 : if (val)
2356 : : {
2357 : 8375 : if (dump_file && (dump_flags & TDF_DETAILS))
2358 : 0 : fprintf (dump_file,
2359 : : "Successfully combined %u partial definitions\n", ndefs);
2360 : : /* We are using the alias-set of the first store we encounter which
2361 : : should be appropriate here. */
2362 : 8375 : return finish (first_set, first_base_set, val);
2363 : : }
2364 : : else
2365 : : {
2366 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
2367 : 0 : fprintf (dump_file,
2368 : : "Failed to interpret %u encoded partial definitions\n", ndefs);
2369 : 4 : return (void *)-1;
2370 : : }
2371 : : }
2372 : :
2373 : : /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2374 : : with the current VUSE and performs the expression lookup. */
2375 : :
2376 : : static void *
2377 : 1061078516 : vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2378 : : {
2379 : 1061078516 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2380 : 1061078516 : vn_reference_t vr = data->vr;
2381 : 1061078516 : vn_reference_s **slot;
2382 : 1061078516 : hashval_t hash;
2383 : :
2384 : : /* If we have partial definitions recorded we have to go through
2385 : : vn_reference_lookup_3. */
2386 : 2114324561 : if (!data->partial_defs.is_empty ())
2387 : : return NULL;
2388 : :
2389 : 1060155922 : if (data->last_vuse_ptr)
2390 : : {
2391 : 1038585551 : *data->last_vuse_ptr = vuse;
2392 : 1038585551 : data->last_vuse = vuse;
2393 : : }
2394 : :
2395 : : /* Fixup vuse and hash. */
2396 : 1060155922 : if (vr->vuse)
2397 : 1060155922 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2398 : 1060155922 : vr->vuse = vuse_ssa_val (vuse);
2399 : 1060155922 : if (vr->vuse)
2400 : 1060155922 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2401 : :
2402 : 1060155922 : hash = vr->hashcode;
2403 : 1060155922 : slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2404 : 1060155922 : if (slot)
2405 : : {
2406 : 7831231 : if ((*slot)->result && data->saved_operands.exists ())
2407 : 340232 : return data->finish (vr->set, vr->base_set, (*slot)->result);
2408 : : return *slot;
2409 : : }
2410 : :
2411 : 1052324691 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2412 : : {
2413 : 17963391 : HOST_WIDE_INT op_offset, op_size;
2414 : 17963391 : tree v = NULL_TREE;
2415 : 17963391 : tree base = ao_ref_base (op);
2416 : :
2417 : 17963391 : if (base
2418 : 17963391 : && op->offset.is_constant (&op_offset)
2419 : 17963391 : && op->size.is_constant (&op_size)
2420 : 17963391 : && op->max_size_known_p ()
2421 : 35479499 : && known_eq (op->size, op->max_size))
2422 : : {
2423 : 17219189 : if (TREE_CODE (base) == PARM_DECL)
2424 : 701891 : v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2425 : : op_size);
2426 : 16517298 : else if (TREE_CODE (base) == MEM_REF
2427 : 6803177 : && integer_zerop (TREE_OPERAND (base, 1))
2428 : 5502700 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2429 : 5496738 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2430 : 20205256 : && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2431 : : == PARM_DECL))
2432 : 3635150 : v = ipcp_get_aggregate_const (cfun,
2433 : 3635150 : SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2434 : : true, op_offset, op_size);
2435 : : }
2436 : 4337041 : if (v)
2437 : 1240 : return data->finish (vr->set, vr->base_set, v);
2438 : : }
2439 : :
2440 : : return NULL;
2441 : : }
2442 : :
2443 : : /* Lookup an existing or insert a new vn_reference entry into the
2444 : : value table for the VUSE, SET, TYPE, OPERANDS reference which
2445 : : has the value VALUE which is either a constant or an SSA name. */
2446 : :
2447 : : static vn_reference_t
2448 : 1466044 : vn_reference_lookup_or_insert_for_pieces (tree vuse,
2449 : : alias_set_type set,
2450 : : alias_set_type base_set,
2451 : : poly_int64 offset,
2452 : : poly_int64 max_size,
2453 : : tree type,
2454 : : vec<vn_reference_op_s,
2455 : : va_heap> operands,
2456 : : tree value)
2457 : : {
2458 : 1466044 : vn_reference_s vr1;
2459 : 1466044 : vn_reference_t result;
2460 : 1466044 : unsigned value_id;
2461 : 1466044 : vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2462 : 1466044 : vr1.operands = operands;
2463 : 1466044 : vr1.type = type;
2464 : 1466044 : vr1.set = set;
2465 : 1466044 : vr1.base_set = base_set;
2466 : 1466044 : vr1.offset = offset;
2467 : 1466044 : vr1.max_size = max_size;
2468 : 1466044 : vr1.hashcode = vn_reference_compute_hash (&vr1);
2469 : 1466044 : if (vn_reference_lookup_1 (&vr1, &result))
2470 : 4780 : return result;
2471 : :
2472 : 1461264 : if (TREE_CODE (value) == SSA_NAME)
2473 : 281084 : value_id = VN_INFO (value)->value_id;
2474 : : else
2475 : 1180180 : value_id = get_or_alloc_constant_value_id (value);
2476 : 1461264 : return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
2477 : 1461264 : type, operands.copy (), value, value_id);
2478 : : }
2479 : :
2480 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2481 : : value-number for the possibly simplified result or by inserting the
2482 : : operation if INSERT is true. If SIMPLIFY is false, return a value
2483 : : number for the unsimplified expression. */
2484 : :
2485 : : static tree
2486 : 18279015 : vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2487 : : bool simplify)
2488 : : {
2489 : 18279015 : tree result = NULL_TREE;
2490 : : /* We will be creating a value number for
2491 : : RCODE (OPS...).
2492 : : So first simplify and lookup this expression to see if it
2493 : : is already available. */
2494 : : /* For simplification valueize. */
2495 : 18279015 : unsigned i = 0;
2496 : 18279015 : if (simplify)
2497 : 42464137 : for (i = 0; i < res_op->num_ops; ++i)
2498 : 24191548 : if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2499 : : {
2500 : 15477794 : tree tem = vn_valueize (res_op->ops[i]);
2501 : 15477794 : if (!tem)
2502 : : break;
2503 : 15477794 : res_op->ops[i] = tem;
2504 : : }
2505 : : /* If valueization of an operand fails (it is not available), skip
2506 : : simplification. */
2507 : 18279015 : bool res = false;
2508 : 18279015 : if (i == res_op->num_ops)
2509 : : {
2510 : : /* Do not leak not available operands into the simplified expression
2511 : : when called from PRE context. */
2512 : 18272589 : if (rpo_avail)
2513 : 10859357 : mprts_hook = vn_lookup_simplify_result;
2514 : 18272589 : res = res_op->resimplify (NULL, vn_valueize);
2515 : 18272589 : mprts_hook = NULL;
2516 : : }
2517 : 31451427 : gimple *new_stmt = NULL;
2518 : 18272589 : if (res
2519 : 18272589 : && gimple_simplified_result_is_gimple_val (res_op))
2520 : : {
2521 : : /* The expression is already available. */
2522 : 5100177 : result = res_op->ops[0];
2523 : : /* Valueize it, simplification returns sth in AVAIL only. */
2524 : 5100177 : if (TREE_CODE (result) == SSA_NAME)
2525 : 288447 : result = SSA_VAL (result);
2526 : : }
2527 : : else
2528 : : {
2529 : 13178838 : tree val = vn_lookup_simplify_result (res_op);
2530 : 13178838 : if (!val && insert)
2531 : : {
2532 : 139834 : gimple_seq stmts = NULL;
2533 : 139834 : result = maybe_push_res_to_seq (res_op, &stmts);
2534 : 139834 : if (result)
2535 : : {
2536 : 139828 : gcc_assert (gimple_seq_singleton_p (stmts));
2537 : 139828 : new_stmt = gimple_seq_first_stmt (stmts);
2538 : : }
2539 : : }
2540 : : else
2541 : : /* The expression is already available. */
2542 : : result = val;
2543 : : }
2544 : 288453 : if (new_stmt)
2545 : : {
2546 : : /* The expression is not yet available, value-number lhs to
2547 : : the new SSA_NAME we created. */
2548 : : /* Initialize value-number information properly. */
2549 : 139828 : vn_ssa_aux_t result_info = VN_INFO (result);
2550 : 139828 : result_info->valnum = result;
2551 : 139828 : result_info->value_id = get_next_value_id ();
2552 : 139828 : result_info->visited = 1;
2553 : 139828 : gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2554 : : new_stmt);
2555 : 139828 : result_info->needs_insertion = true;
2556 : : /* ??? PRE phi-translation inserts NARYs without corresponding
2557 : : SSA name result. Re-use those but set their result according
2558 : : to the stmt we just built. */
2559 : 139828 : vn_nary_op_t nary = NULL;
2560 : 139828 : vn_nary_op_lookup_stmt (new_stmt, &nary);
2561 : 139828 : if (nary)
2562 : : {
2563 : 0 : gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2564 : 0 : nary->u.result = gimple_assign_lhs (new_stmt);
2565 : : }
2566 : : /* As all "inserted" statements are singleton SCCs, insert
2567 : : to the valid table. This is strictly needed to
2568 : : avoid re-generating new value SSA_NAMEs for the same
2569 : : expression during SCC iteration over and over (the
2570 : : optimistic table gets cleared after each iteration).
2571 : : We do not need to insert into the optimistic table, as
2572 : : lookups there will fall back to the valid table. */
2573 : : else
2574 : : {
2575 : 139828 : unsigned int length = vn_nary_length_from_stmt (new_stmt);
2576 : 139828 : vn_nary_op_t vno1
2577 : 139828 : = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2578 : 139828 : vno1->value_id = result_info->value_id;
2579 : 139828 : vno1->length = length;
2580 : 139828 : vno1->predicated_values = 0;
2581 : 139828 : vno1->u.result = result;
2582 : 139828 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2583 : 139828 : vn_nary_op_insert_into (vno1, valid_info->nary);
2584 : : /* Also do not link it into the undo chain. */
2585 : 139828 : last_inserted_nary = vno1->next;
2586 : 139828 : vno1->next = (vn_nary_op_t)(void *)-1;
2587 : : }
2588 : 139828 : if (dump_file && (dump_flags & TDF_DETAILS))
2589 : : {
2590 : 588 : fprintf (dump_file, "Inserting name ");
2591 : 588 : print_generic_expr (dump_file, result);
2592 : 588 : fprintf (dump_file, " for expression ");
2593 : 588 : print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2594 : 588 : fprintf (dump_file, "\n");
2595 : : }
2596 : : }
2597 : 18279015 : return result;
2598 : : }
2599 : :
2600 : : /* Return a value-number for RCODE OPS... either by looking up an existing
2601 : : value-number for the simplified result or by inserting the operation. */
2602 : :
2603 : : static tree
2604 : 182368 : vn_nary_build_or_lookup (gimple_match_op *res_op)
2605 : : {
2606 : 0 : return vn_nary_build_or_lookup_1 (res_op, true, true);
2607 : : }
2608 : :
2609 : : /* Try to simplify the expression RCODE OPS... of type TYPE and return
2610 : : its value if present. Update NARY with a simplified expression if
2611 : : it fits. */
2612 : :
2613 : : tree
2614 : 7407729 : vn_nary_simplify (vn_nary_op_t nary)
2615 : : {
2616 : 7407729 : if (nary->length > gimple_match_op::MAX_NUM_OPS
2617 : : /* For CONSTRUCTOR the vn_nary_op_t and gimple_match_op representation
2618 : : does not match. */
2619 : 7407553 : || nary->opcode == CONSTRUCTOR)
2620 : : return NULL_TREE;
2621 : 7407139 : gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2622 : 7407139 : nary->type, nary->length);
2623 : 7407139 : memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2624 : 7407139 : tree res = vn_nary_build_or_lookup_1 (&op, false, true);
2625 : : /* Do not update *NARY with a simplified result that contains abnormals.
2626 : : This matches what maybe_push_res_to_seq does when requesting insertion. */
2627 : 19409884 : for (unsigned i = 0; i < op.num_ops; ++i)
2628 : 12002824 : if (TREE_CODE (op.ops[i]) == SSA_NAME
2629 : 12002824 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op.ops[i]))
2630 : : return res;
2631 : 7407060 : if (op.code.is_tree_code ()
2632 : 7407060 : && op.num_ops <= nary->length
2633 : 14813188 : && (tree_code) op.code != CONSTRUCTOR)
2634 : : {
2635 : 7406127 : nary->opcode = (tree_code) op.code;
2636 : 7406127 : nary->length = op.num_ops;
2637 : 19406785 : for (unsigned i = 0; i < op.num_ops; ++i)
2638 : 12000658 : nary->op[i] = op.ops[i];
2639 : : }
2640 : : return res;
2641 : : }
2642 : :
2643 : : /* Elimination engine. */
2644 : :
2645 : : class eliminate_dom_walker : public dom_walker
2646 : : {
2647 : : public:
2648 : : eliminate_dom_walker (cdi_direction, bitmap);
2649 : : ~eliminate_dom_walker ();
2650 : :
2651 : : edge before_dom_children (basic_block) final override;
2652 : : void after_dom_children (basic_block) final override;
2653 : :
2654 : : virtual tree eliminate_avail (basic_block, tree op);
2655 : : virtual void eliminate_push_avail (basic_block, tree op);
2656 : : tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2657 : :
2658 : : void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2659 : :
2660 : : unsigned eliminate_cleanup (bool region_p = false);
2661 : :
2662 : : bool do_pre;
2663 : : unsigned int el_todo;
2664 : : unsigned int eliminations;
2665 : : unsigned int insertions;
2666 : :
2667 : : /* SSA names that had their defs inserted by PRE if do_pre. */
2668 : : bitmap inserted_exprs;
2669 : :
2670 : : /* Blocks with statements that have had their EH properties changed. */
2671 : : bitmap need_eh_cleanup;
2672 : :
2673 : : /* Blocks with statements that have had their AB properties changed. */
2674 : : bitmap need_ab_cleanup;
2675 : :
2676 : : /* Local state for the eliminate domwalk. */
2677 : : auto_vec<gimple *> to_remove;
2678 : : auto_vec<gimple *> to_fixup;
2679 : : auto_vec<tree> avail;
2680 : : auto_vec<tree> avail_stack;
2681 : : };
2682 : :
2683 : : /* Adaptor to the elimination engine using RPO availability. */
2684 : :
2685 : 12361900 : class rpo_elim : public eliminate_dom_walker
2686 : : {
2687 : : public:
2688 : 6180950 : rpo_elim(basic_block entry_)
2689 : 12361900 : : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2690 : 12361900 : m_avail_freelist (NULL) {}
2691 : :
2692 : : tree eliminate_avail (basic_block, tree op) final override;
2693 : :
2694 : : void eliminate_push_avail (basic_block, tree) final override;
2695 : :
2696 : : basic_block entry;
2697 : : /* Freelist of avail entries which are allocated from the vn_ssa_aux
2698 : : obstack. */
2699 : : vn_avail *m_avail_freelist;
2700 : : };
2701 : :
2702 : : /* Return true if BASE1 and BASE2 can be adjusted so they have the
2703 : : same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2704 : : Otherwise return false. */
2705 : :
2706 : : static bool
2707 : 6837299 : adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2708 : : tree base2, poly_int64 *offset2)
2709 : : {
2710 : 6837299 : poly_int64 soff;
2711 : 6837299 : if (TREE_CODE (base1) == MEM_REF
2712 : 3096826 : && TREE_CODE (base2) == MEM_REF)
2713 : : {
2714 : 2449944 : if (mem_ref_offset (base1).to_shwi (&soff))
2715 : : {
2716 : 2449944 : base1 = TREE_OPERAND (base1, 0);
2717 : 2449944 : *offset1 += soff * BITS_PER_UNIT;
2718 : : }
2719 : 2449944 : if (mem_ref_offset (base2).to_shwi (&soff))
2720 : : {
2721 : 2449944 : base2 = TREE_OPERAND (base2, 0);
2722 : 2449944 : *offset2 += soff * BITS_PER_UNIT;
2723 : : }
2724 : 2449944 : return operand_equal_p (base1, base2, 0);
2725 : : }
2726 : 4387355 : return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2727 : : }
2728 : :
2729 : : /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2730 : : from the statement defining VUSE and if not successful tries to
2731 : : translate *REFP and VR_ through an aggregate copy at the definition
2732 : : of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2733 : : of *REF and *VR. If only disambiguation was performed then
2734 : : *DISAMBIGUATE_ONLY is set to true. */
2735 : :
2736 : : static void *
2737 : 42629979 : vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2738 : : translate_flags *disambiguate_only)
2739 : : {
2740 : 42629979 : vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2741 : 42629979 : vn_reference_t vr = data->vr;
2742 : 42629979 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2743 : 42629979 : tree base = ao_ref_base (ref);
2744 : 42629979 : HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2745 : 42629979 : static vec<vn_reference_op_s> lhs_ops;
2746 : 42629979 : ao_ref lhs_ref;
2747 : 42629979 : bool lhs_ref_ok = false;
2748 : 42629979 : poly_int64 copy_size;
2749 : :
2750 : : /* First try to disambiguate after value-replacing in the definitions LHS. */
2751 : 42629979 : if (is_gimple_assign (def_stmt))
2752 : : {
2753 : 20594613 : tree lhs = gimple_assign_lhs (def_stmt);
2754 : 20594613 : bool valueized_anything = false;
2755 : : /* Avoid re-allocation overhead. */
2756 : 20594613 : lhs_ops.truncate (0);
2757 : 20594613 : basic_block saved_rpo_bb = vn_context_bb;
2758 : 20594613 : vn_context_bb = gimple_bb (def_stmt);
2759 : 20594613 : if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2760 : : {
2761 : 13813106 : copy_reference_ops_from_ref (lhs, &lhs_ops);
2762 : 13813106 : valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2763 : : }
2764 : 20594613 : vn_context_bb = saved_rpo_bb;
2765 : 20594613 : ao_ref_init (&lhs_ref, lhs);
2766 : 20594613 : lhs_ref_ok = true;
2767 : 20594613 : if (valueized_anything
2768 : 1796567 : && ao_ref_init_from_vn_reference
2769 : 1796567 : (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2770 : 1796567 : ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2771 : 22391180 : && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2772 : : {
2773 : 1502911 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2774 : 7806080 : return NULL;
2775 : : }
2776 : :
2777 : : /* When the def is a CLOBBER we can optimistically disambiguate
2778 : : against it since any overlap it would be undefined behavior.
2779 : : Avoid this for obvious must aliases to save compile-time though.
2780 : : We also may not do this when the query is used for redundant
2781 : : store removal. */
2782 : 19091702 : if (!data->redundant_store_removal_p
2783 : 10576511 : && gimple_clobber_p (def_stmt)
2784 : 19546206 : && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2785 : : {
2786 : 428970 : *disambiguate_only = TR_DISAMBIGUATE;
2787 : 428970 : return NULL;
2788 : : }
2789 : :
2790 : : /* Besides valueizing the LHS we can also use access-path based
2791 : : disambiguation on the original non-valueized ref. */
2792 : 18662732 : if (!ref->ref
2793 : : && lhs_ref_ok
2794 : 2661493 : && data->orig_ref.ref)
2795 : : {
2796 : : /* We want to use the non-valueized LHS for this, but avoid redundant
2797 : : work. */
2798 : 1794168 : ao_ref *lref = &lhs_ref;
2799 : 1794168 : ao_ref lref_alt;
2800 : 1794168 : if (valueized_anything)
2801 : : {
2802 : 118984 : ao_ref_init (&lref_alt, lhs);
2803 : 118984 : lref = &lref_alt;
2804 : : }
2805 : 1794168 : if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2806 : : {
2807 : 250382 : *disambiguate_only = (valueized_anything
2808 : 125191 : ? TR_VALUEIZE_AND_DISAMBIGUATE
2809 : : : TR_DISAMBIGUATE);
2810 : 125191 : return NULL;
2811 : : }
2812 : : }
2813 : :
2814 : : /* If we reach a clobbering statement try to skip it and see if
2815 : : we find a VN result with exactly the same value as the
2816 : : possible clobber. In this case we can ignore the clobber
2817 : : and return the found value. */
2818 : 18537541 : if (!gimple_has_volatile_ops (def_stmt)
2819 : 17273881 : && ((is_gimple_reg_type (TREE_TYPE (lhs))
2820 : 12683614 : && types_compatible_p (TREE_TYPE (lhs), vr->type)
2821 : 9769672 : && !storage_order_barrier_p (lhs)
2822 : 9769672 : && !reverse_storage_order_for_component_p (lhs))
2823 : 7504211 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == CONSTRUCTOR)
2824 : 10835800 : && (ref->ref || data->orig_ref.ref)
2825 : 10417244 : && !data->mask
2826 : 10395810 : && data->partial_defs.is_empty ()
2827 : 10393682 : && multiple_p (get_object_alignment
2828 : : (ref->ref ? ref->ref : data->orig_ref.ref),
2829 : : ref->size)
2830 : 41556204 : && multiple_p (get_object_alignment (lhs), ref->size))
2831 : : {
2832 : 9990879 : HOST_WIDE_INT offset2i, size2i;
2833 : 9990879 : poly_int64 offset = ref->offset;
2834 : 9990879 : poly_int64 maxsize = ref->max_size;
2835 : :
2836 : 9990879 : gcc_assert (lhs_ref_ok);
2837 : 9990879 : tree base2 = ao_ref_base (&lhs_ref);
2838 : 9990879 : poly_int64 offset2 = lhs_ref.offset;
2839 : 9990879 : poly_int64 size2 = lhs_ref.size;
2840 : 9990879 : poly_int64 maxsize2 = lhs_ref.max_size;
2841 : :
2842 : 9990879 : tree rhs = gimple_assign_rhs1 (def_stmt);
2843 : 9990879 : if (TREE_CODE (rhs) == CONSTRUCTOR)
2844 : 1038500 : rhs = integer_zero_node;
2845 : : /* ??? We may not compare to ahead values which might be from
2846 : : a different loop iteration but only to loop invariants. Use
2847 : : CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2848 : : The one-hop lookup below doesn't have this issue since there's
2849 : : a virtual PHI before we ever reach a backedge to cross.
2850 : : We can skip multiple defs as long as they are from the same
2851 : : value though. */
2852 : 9990879 : if (data->same_val
2853 : 9990879 : && !operand_equal_p (data->same_val, rhs))
2854 : : ;
2855 : : /* When this is a (partial) must-def, leave it to handling
2856 : : below in case we are interested in the value. */
2857 : 9706329 : else if (!(*disambiguate_only > TR_TRANSLATE)
2858 : 3365069 : && base2
2859 : 3365069 : && known_eq (maxsize2, size2)
2860 : 2379218 : && adjust_offsets_for_equal_base_address (base, &offset,
2861 : : base2, &offset2)
2862 : 1147037 : && offset2.is_constant (&offset2i)
2863 : 1147037 : && size2.is_constant (&size2i)
2864 : 1147037 : && maxsize.is_constant (&maxsizei)
2865 : 1147037 : && offset.is_constant (&offseti)
2866 : 10853366 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
2867 : : size2i))
2868 : : ;
2869 : 8644903 : else if (CONSTANT_CLASS_P (rhs))
2870 : : {
2871 : 4183328 : if (dump_file && (dump_flags & TDF_DETAILS))
2872 : : {
2873 : 1908 : fprintf (dump_file,
2874 : : "Skipping possible redundant definition ");
2875 : 1908 : print_gimple_stmt (dump_file, def_stmt, 0);
2876 : : }
2877 : : /* Delay the actual compare of the values to the end of the walk
2878 : : but do not update last_vuse from here. */
2879 : 4183328 : data->last_vuse_ptr = NULL;
2880 : 4183328 : data->same_val = rhs;
2881 : 4246097 : return NULL;
2882 : : }
2883 : : else
2884 : : {
2885 : 4461575 : tree saved_vuse = vr->vuse;
2886 : 4461575 : hashval_t saved_hashcode = vr->hashcode;
2887 : 4461575 : if (vr->vuse)
2888 : 4461575 : vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2889 : 8923150 : vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
2890 : 4461575 : if (vr->vuse)
2891 : 4461575 : vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2892 : 4461575 : vn_reference_t vnresult = NULL;
2893 : : /* Do not use vn_reference_lookup_2 since that might perform
2894 : : expression hashtable insertion but this lookup crosses
2895 : : a possible may-alias making such insertion conditionally
2896 : : invalid. */
2897 : 4461575 : vn_reference_lookup_1 (vr, &vnresult);
2898 : : /* Need to restore vr->vuse and vr->hashcode. */
2899 : 4461575 : vr->vuse = saved_vuse;
2900 : 4461575 : vr->hashcode = saved_hashcode;
2901 : 4461575 : if (vnresult)
2902 : : {
2903 : 248040 : if (TREE_CODE (rhs) == SSA_NAME)
2904 : 246508 : rhs = SSA_VAL (rhs);
2905 : 248040 : if (vnresult->result
2906 : 248040 : && operand_equal_p (vnresult->result, rhs, 0))
2907 : 62769 : return vnresult;
2908 : : }
2909 : : }
2910 : : }
2911 : : }
2912 : 22035366 : else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2913 : 20611169 : && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2914 : 24145815 : && gimple_call_num_args (def_stmt) <= 4)
2915 : : {
2916 : : /* For builtin calls valueize its arguments and call the
2917 : : alias oracle again. Valueization may improve points-to
2918 : : info of pointers and constify size and position arguments.
2919 : : Originally this was motivated by PR61034 which has
2920 : : conditional calls to free falsely clobbering ref because
2921 : : of imprecise points-to info of the argument. */
2922 : : tree oldargs[4];
2923 : : bool valueized_anything = false;
2924 : 5182639 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2925 : : {
2926 : 3591281 : oldargs[i] = gimple_call_arg (def_stmt, i);
2927 : 3591281 : tree val = vn_valueize (oldargs[i]);
2928 : 3591281 : if (val != oldargs[i])
2929 : : {
2930 : 133504 : gimple_call_set_arg (def_stmt, i, val);
2931 : 133504 : valueized_anything = true;
2932 : : }
2933 : : }
2934 : 1591358 : if (valueized_anything)
2935 : : {
2936 : 196100 : bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2937 : 98050 : ref, data->tbaa_p);
2938 : 360079 : for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2939 : 262029 : gimple_call_set_arg (def_stmt, i, oldargs[i]);
2940 : 98050 : if (!res)
2941 : : {
2942 : 25094 : *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2943 : 25094 : return NULL;
2944 : : }
2945 : : }
2946 : : }
2947 : :
2948 : 36301716 : if (*disambiguate_only > TR_TRANSLATE)
2949 : : return (void *)-1;
2950 : :
2951 : : /* If we cannot constrain the size of the reference we cannot
2952 : : test if anything kills it. */
2953 : 24430608 : if (!ref->max_size_known_p ())
2954 : : return (void *)-1;
2955 : :
2956 : 24014313 : poly_int64 offset = ref->offset;
2957 : 24014313 : poly_int64 maxsize = ref->max_size;
2958 : :
2959 : : /* def_stmt may-defs *ref. See if we can derive a value for *ref
2960 : : from that definition.
2961 : : 1) Memset. */
2962 : 24014313 : if (is_gimple_reg_type (vr->type)
2963 : 23692383 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2964 : 23604501 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
2965 : 88424 : && (integer_zerop (gimple_call_arg (def_stmt, 1))
2966 : 32689 : || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2967 : 8210 : || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2968 : : && CHAR_BIT == 8
2969 : : && BITS_PER_UNIT == 8
2970 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2971 : 31846 : && offset.is_constant (&offseti)
2972 : 31846 : && ref->size.is_constant (&sizei)
2973 : 31846 : && (offseti % BITS_PER_UNIT == 0
2974 : 39 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
2975 : 87581 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2976 : 36385 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
2977 : 36385 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
2978 : 24066047 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2979 : 28349 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2980 : : {
2981 : 51693 : tree base2;
2982 : 51693 : poly_int64 offset2, size2, maxsize2;
2983 : 51693 : bool reverse;
2984 : 51693 : tree ref2 = gimple_call_arg (def_stmt, 0);
2985 : 51693 : if (TREE_CODE (ref2) == SSA_NAME)
2986 : : {
2987 : 28308 : ref2 = SSA_VAL (ref2);
2988 : 28308 : if (TREE_CODE (ref2) == SSA_NAME
2989 : 28308 : && (TREE_CODE (base) != MEM_REF
2990 : 18208 : || TREE_OPERAND (base, 0) != ref2))
2991 : : {
2992 : 22100 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2993 : 22100 : if (gimple_assign_single_p (def_stmt)
2994 : 22100 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2995 : 822 : ref2 = gimple_assign_rhs1 (def_stmt);
2996 : : }
2997 : : }
2998 : 51693 : if (TREE_CODE (ref2) == ADDR_EXPR)
2999 : : {
3000 : 27183 : ref2 = TREE_OPERAND (ref2, 0);
3001 : 27183 : base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
3002 : : &reverse);
3003 : 27183 : if (!known_size_p (maxsize2)
3004 : 27143 : || !known_eq (maxsize2, size2)
3005 : 54252 : || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
3006 : 55658 : return (void *)-1;
3007 : : }
3008 : 24510 : else if (TREE_CODE (ref2) == SSA_NAME)
3009 : : {
3010 : 24510 : poly_int64 soff;
3011 : 24510 : if (TREE_CODE (base) != MEM_REF
3012 : 42117 : || !(mem_ref_offset (base)
3013 : 35214 : << LOG2_BITS_PER_UNIT).to_shwi (&soff))
3014 : 20816 : return (void *)-1;
3015 : 17607 : offset += soff;
3016 : 17607 : offset2 = 0;
3017 : 17607 : if (TREE_OPERAND (base, 0) != ref2)
3018 : : {
3019 : 14375 : gimple *def = SSA_NAME_DEF_STMT (ref2);
3020 : 14375 : if (is_gimple_assign (def)
3021 : 13273 : && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
3022 : 11349 : && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
3023 : 14867 : && poly_int_tree_p (gimple_assign_rhs2 (def)))
3024 : : {
3025 : 462 : tree rhs2 = gimple_assign_rhs2 (def);
3026 : 462 : if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
3027 : : SIGNED)
3028 : 462 : << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
3029 : : return (void *)-1;
3030 : 462 : ref2 = gimple_assign_rhs1 (def);
3031 : 462 : if (TREE_CODE (ref2) == SSA_NAME)
3032 : 462 : ref2 = SSA_VAL (ref2);
3033 : : }
3034 : : else
3035 : : return (void *)-1;
3036 : : }
3037 : : }
3038 : : else
3039 : : return (void *)-1;
3040 : 26535 : tree len = gimple_call_arg (def_stmt, 2);
3041 : 26535 : HOST_WIDE_INT leni, offset2i;
3042 : 26535 : if (TREE_CODE (len) == SSA_NAME)
3043 : 254 : len = SSA_VAL (len);
3044 : : /* Sometimes the above trickery is smarter than alias analysis. Take
3045 : : advantage of that. */
3046 : 26535 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
3047 : 53070 : (wi::to_poly_offset (len)
3048 : 26535 : << LOG2_BITS_PER_UNIT)))
3049 : : return NULL;
3050 : 53027 : if (data->partial_defs.is_empty ()
3051 : 26492 : && known_subrange_p (offset, maxsize, offset2,
3052 : 26492 : wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
3053 : : {
3054 : 26047 : tree val;
3055 : 26047 : if (integer_zerop (gimple_call_arg (def_stmt, 1)))
3056 : 20919 : val = build_zero_cst (vr->type);
3057 : 5128 : else if (INTEGRAL_TYPE_P (vr->type)
3058 : 3984 : && known_eq (ref->size, 8)
3059 : 8344 : && offseti % BITS_PER_UNIT == 0)
3060 : : {
3061 : 3216 : gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
3062 : 3216 : vr->type, gimple_call_arg (def_stmt, 1));
3063 : 3216 : val = vn_nary_build_or_lookup (&res_op);
3064 : 3216 : if (!val
3065 : 3216 : || (TREE_CODE (val) == SSA_NAME
3066 : 626 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3067 : 0 : return (void *)-1;
3068 : : }
3069 : : else
3070 : : {
3071 : 1912 : unsigned buflen
3072 : 1912 : = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
3073 : 1912 : if (INTEGRAL_TYPE_P (vr->type)
3074 : 1912 : && TYPE_MODE (vr->type) != BLKmode)
3075 : 1534 : buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
3076 : 1912 : unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
3077 : 1912 : memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
3078 : : buflen);
3079 : 1912 : if (BYTES_BIG_ENDIAN)
3080 : : {
3081 : : unsigned int amnt
3082 : : = (((unsigned HOST_WIDE_INT) offseti + sizei)
3083 : : % BITS_PER_UNIT);
3084 : : if (amnt)
3085 : : {
3086 : : shift_bytes_in_array_right (buf, buflen,
3087 : : BITS_PER_UNIT - amnt);
3088 : : buf++;
3089 : : buflen--;
3090 : : }
3091 : : }
3092 : 1912 : else if (offseti % BITS_PER_UNIT != 0)
3093 : : {
3094 : 7 : unsigned int amnt
3095 : : = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3096 : 7 : % BITS_PER_UNIT);
3097 : 7 : shift_bytes_in_array_left (buf, buflen, amnt);
3098 : 7 : buf++;
3099 : 7 : buflen--;
3100 : : }
3101 : 1912 : val = native_interpret_expr (vr->type, buf, buflen);
3102 : 1912 : if (!val)
3103 : : return (void *)-1;
3104 : : }
3105 : 26047 : return data->finish (0, 0, val);
3106 : : }
3107 : : /* For now handle clearing memory with partial defs. */
3108 : 488 : else if (known_eq (ref->size, maxsize)
3109 : 418 : && integer_zerop (gimple_call_arg (def_stmt, 1))
3110 : 115 : && tree_fits_poly_int64_p (len)
3111 : 111 : && tree_to_poly_int64 (len).is_constant (&leni)
3112 : 111 : && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3113 : 111 : && offset.is_constant (&offseti)
3114 : 111 : && offset2.is_constant (&offset2i)
3115 : 111 : && maxsize.is_constant (&maxsizei)
3116 : 488 : && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3117 : 488 : leni << LOG2_BITS_PER_UNIT))
3118 : : {
3119 : 111 : pd_data pd;
3120 : 111 : pd.rhs = build_constructor (NULL_TREE, NULL);
3121 : 111 : pd.rhs_off = 0;
3122 : 111 : pd.offset = offset2i;
3123 : 111 : pd.size = leni << LOG2_BITS_PER_UNIT;
3124 : 111 : return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3125 : : }
3126 : : }
3127 : :
3128 : : /* 2) Assignment from an empty CONSTRUCTOR. */
3129 : 23962620 : else if (is_gimple_reg_type (vr->type)
3130 : 23640690 : && gimple_assign_single_p (def_stmt)
3131 : 7654601 : && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3132 : 1898375 : && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0
3133 : 25860995 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt)))
3134 : : {
3135 : 1898319 : tree base2;
3136 : 1898319 : poly_int64 offset2, size2, maxsize2;
3137 : 1898319 : HOST_WIDE_INT offset2i, size2i;
3138 : 1898319 : gcc_assert (lhs_ref_ok);
3139 : 1898319 : base2 = ao_ref_base (&lhs_ref);
3140 : 1898319 : offset2 = lhs_ref.offset;
3141 : 1898319 : size2 = lhs_ref.size;
3142 : 1898319 : maxsize2 = lhs_ref.max_size;
3143 : 1898319 : if (known_size_p (maxsize2)
3144 : 1898281 : && known_eq (maxsize2, size2)
3145 : 3796554 : && adjust_offsets_for_equal_base_address (base, &offset,
3146 : : base2, &offset2))
3147 : : {
3148 : 1870698 : if (data->partial_defs.is_empty ()
3149 : 1869026 : && known_subrange_p (offset, maxsize, offset2, size2))
3150 : : {
3151 : : /* While technically undefined behavior do not optimize
3152 : : a full read from a clobber. */
3153 : 1868236 : if (gimple_clobber_p (def_stmt))
3154 : 1870644 : return (void *)-1;
3155 : 987863 : tree val = build_zero_cst (vr->type);
3156 : 987863 : return data->finish (ao_ref_alias_set (&lhs_ref),
3157 : 987863 : ao_ref_base_alias_set (&lhs_ref), val);
3158 : : }
3159 : 2462 : else if (known_eq (ref->size, maxsize)
3160 : 2408 : && maxsize.is_constant (&maxsizei)
3161 : 2408 : && offset.is_constant (&offseti)
3162 : 2408 : && offset2.is_constant (&offset2i)
3163 : 2408 : && size2.is_constant (&size2i)
3164 : 2462 : && ranges_known_overlap_p (offseti, maxsizei,
3165 : : offset2i, size2i))
3166 : : {
3167 : : /* Let clobbers be consumed by the partial-def tracker
3168 : : which can choose to ignore them if they are shadowed
3169 : : by a later def. */
3170 : 2408 : pd_data pd;
3171 : 2408 : pd.rhs = gimple_assign_rhs1 (def_stmt);
3172 : 2408 : pd.rhs_off = 0;
3173 : 2408 : pd.offset = offset2i;
3174 : 2408 : pd.size = size2i;
3175 : 2408 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3176 : : ao_ref_base_alias_set (&lhs_ref),
3177 : : offseti, maxsizei);
3178 : : }
3179 : : }
3180 : : }
3181 : :
3182 : : /* 3) Assignment from a constant. We can use folds native encode/interpret
3183 : : routines to extract the assigned bits. */
3184 : 22064301 : else if (known_eq (ref->size, maxsize)
3185 : 21516318 : && is_gimple_reg_type (vr->type)
3186 : 21194388 : && !reverse_storage_order_for_component_p (vr->operands)
3187 : 21191661 : && !contains_storage_order_barrier_p (vr->operands)
3188 : 21191661 : && gimple_assign_single_p (def_stmt)
3189 : 5409277 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3190 : : && CHAR_BIT == 8
3191 : : && BITS_PER_UNIT == 8
3192 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3193 : : /* native_encode and native_decode operate on arrays of bytes
3194 : : and so fundamentally need a compile-time size and offset. */
3195 : 5406355 : && maxsize.is_constant (&maxsizei)
3196 : 5406355 : && offset.is_constant (&offseti)
3197 : 27470656 : && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3198 : 4568748 : || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3199 : 1872585 : && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3200 : : {
3201 : 852670 : tree lhs = gimple_assign_lhs (def_stmt);
3202 : 852670 : tree base2;
3203 : 852670 : poly_int64 offset2, size2, maxsize2;
3204 : 852670 : HOST_WIDE_INT offset2i, size2i;
3205 : 852670 : bool reverse;
3206 : 852670 : gcc_assert (lhs_ref_ok);
3207 : 852670 : base2 = ao_ref_base (&lhs_ref);
3208 : 852670 : offset2 = lhs_ref.offset;
3209 : 852670 : size2 = lhs_ref.size;
3210 : 852670 : maxsize2 = lhs_ref.max_size;
3211 : 852670 : reverse = reverse_storage_order_for_component_p (lhs);
3212 : 852670 : if (base2
3213 : 852670 : && !reverse
3214 : 851842 : && !storage_order_barrier_p (lhs)
3215 : 851842 : && known_eq (maxsize2, size2)
3216 : 825108 : && adjust_offsets_for_equal_base_address (base, &offset,
3217 : : base2, &offset2)
3218 : 77977 : && offset.is_constant (&offseti)
3219 : 77977 : && offset2.is_constant (&offset2i)
3220 : 852670 : && size2.is_constant (&size2i))
3221 : : {
3222 : 77977 : if (data->partial_defs.is_empty ()
3223 : 62545 : && known_subrange_p (offseti, maxsizei, offset2, size2))
3224 : : {
3225 : : /* We support up to 512-bit values (for V8DFmode). */
3226 : 43419 : unsigned char buffer[65];
3227 : 43419 : int len;
3228 : :
3229 : 43419 : tree rhs = gimple_assign_rhs1 (def_stmt);
3230 : 43419 : if (TREE_CODE (rhs) == SSA_NAME)
3231 : 1415 : rhs = SSA_VAL (rhs);
3232 : 86838 : len = native_encode_expr (rhs,
3233 : : buffer, sizeof (buffer) - 1,
3234 : 43419 : (offseti - offset2i) / BITS_PER_UNIT);
3235 : 43419 : if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
3236 : : {
3237 : 40397 : tree type = vr->type;
3238 : 40397 : unsigned char *buf = buffer;
3239 : 40397 : unsigned int amnt = 0;
3240 : : /* Make sure to interpret in a type that has a range
3241 : : covering the whole access size. */
3242 : 40397 : if (INTEGRAL_TYPE_P (vr->type)
3243 : 40397 : && maxsizei != TYPE_PRECISION (vr->type))
3244 : 1682 : type = build_nonstandard_integer_type (maxsizei,
3245 : 841 : TYPE_UNSIGNED (type));
3246 : 40397 : if (BYTES_BIG_ENDIAN)
3247 : : {
3248 : : /* For big-endian native_encode_expr stored the rhs
3249 : : such that the LSB of it is the LSB of buffer[len - 1].
3250 : : That bit is stored into memory at position
3251 : : offset2 + size2 - 1, i.e. in byte
3252 : : base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3253 : : E.g. for offset2 1 and size2 14, rhs -1 and memory
3254 : : previously cleared that is:
3255 : : 0 1
3256 : : 01111111|11111110
3257 : : Now, if we want to extract offset 2 and size 12 from
3258 : : it using native_interpret_expr (which actually works
3259 : : for integral bitfield types in terms of byte size of
3260 : : the mode), the native_encode_expr stored the value
3261 : : into buffer as
3262 : : XX111111|11111111
3263 : : and returned len 2 (the X bits are outside of
3264 : : precision).
3265 : : Let sz be maxsize / BITS_PER_UNIT if not extracting
3266 : : a bitfield, and GET_MODE_SIZE otherwise.
3267 : : We need to align the LSB of the value we want to
3268 : : extract as the LSB of buf[sz - 1].
3269 : : The LSB from memory we need to read is at position
3270 : : offset + maxsize - 1. */
3271 : : HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3272 : : if (INTEGRAL_TYPE_P (type))
3273 : : {
3274 : : if (TYPE_MODE (type) != BLKmode)
3275 : : sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3276 : : else
3277 : : sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3278 : : }
3279 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3280 : : - offseti - maxsizei) % BITS_PER_UNIT;
3281 : : if (amnt)
3282 : : shift_bytes_in_array_right (buffer, len, amnt);
3283 : : amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3284 : : - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3285 : : if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3286 : : len = 0;
3287 : : else
3288 : : {
3289 : : buf = buffer + len - sz - amnt;
3290 : : len -= (buf - buffer);
3291 : : }
3292 : : }
3293 : : else
3294 : : {
3295 : 40397 : amnt = ((unsigned HOST_WIDE_INT) offset2i
3296 : 40397 : - offseti) % BITS_PER_UNIT;
3297 : 40397 : if (amnt)
3298 : : {
3299 : 305 : buffer[len] = 0;
3300 : 305 : shift_bytes_in_array_left (buffer, len + 1, amnt);
3301 : 305 : buf = buffer + 1;
3302 : : }
3303 : : }
3304 : 40397 : tree val = native_interpret_expr (type, buf, len);
3305 : : /* If we chop off bits because the types precision doesn't
3306 : : match the memory access size this is ok when optimizing
3307 : : reads but not when called from the DSE code during
3308 : : elimination. */
3309 : 40397 : if (val
3310 : 40395 : && type != vr->type)
3311 : : {
3312 : 841 : if (! int_fits_type_p (val, vr->type))
3313 : : val = NULL_TREE;
3314 : : else
3315 : 841 : val = fold_convert (vr->type, val);
3316 : : }
3317 : :
3318 : 40395 : if (val)
3319 : 40395 : return data->finish (ao_ref_alias_set (&lhs_ref),
3320 : 40395 : ao_ref_base_alias_set (&lhs_ref), val);
3321 : : }
3322 : : }
3323 : 34558 : else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3324 : : size2i))
3325 : : {
3326 : 34558 : pd_data pd;
3327 : 34558 : tree rhs = gimple_assign_rhs1 (def_stmt);
3328 : 34558 : if (TREE_CODE (rhs) == SSA_NAME)
3329 : 2533 : rhs = SSA_VAL (rhs);
3330 : 34558 : pd.rhs = rhs;
3331 : 34558 : pd.rhs_off = 0;
3332 : 34558 : pd.offset = offset2i;
3333 : 34558 : pd.size = size2i;
3334 : 34558 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3335 : : ao_ref_base_alias_set (&lhs_ref),
3336 : : offseti, maxsizei);
3337 : : }
3338 : : }
3339 : : }
3340 : :
3341 : : /* 4) Assignment from an SSA name which definition we may be able
3342 : : to access pieces from or we can combine to a larger entity. */
3343 : 21211631 : else if (known_eq (ref->size, maxsize)
3344 : 20663648 : && is_gimple_reg_type (vr->type)
3345 : 20341718 : && !reverse_storage_order_for_component_p (vr->operands)
3346 : 20338991 : && !contains_storage_order_barrier_p (vr->operands)
3347 : 20338991 : && gimple_assign_single_p (def_stmt)
3348 : 4556607 : && !TREE_THIS_VOLATILE (gimple_assign_lhs (def_stmt))
3349 : 25765316 : && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3350 : : {
3351 : 1857522 : tree lhs = gimple_assign_lhs (def_stmt);
3352 : 1857522 : tree base2;
3353 : 1857522 : poly_int64 offset2, size2, maxsize2;
3354 : 1857522 : HOST_WIDE_INT offset2i, size2i, offseti;
3355 : 1857522 : bool reverse;
3356 : 1857522 : gcc_assert (lhs_ref_ok);
3357 : 1857522 : base2 = ao_ref_base (&lhs_ref);
3358 : 1857522 : offset2 = lhs_ref.offset;
3359 : 1857522 : size2 = lhs_ref.size;
3360 : 1857522 : maxsize2 = lhs_ref.max_size;
3361 : 1857522 : reverse = reverse_storage_order_for_component_p (lhs);
3362 : 1857522 : tree def_rhs = gimple_assign_rhs1 (def_stmt);
3363 : 1857522 : if (!reverse
3364 : 1857310 : && !storage_order_barrier_p (lhs)
3365 : 1857310 : && known_size_p (maxsize2)
3366 : 1834227 : && known_eq (maxsize2, size2)
3367 : 3592246 : && adjust_offsets_for_equal_base_address (base, &offset,
3368 : : base2, &offset2))
3369 : : {
3370 : 80559 : if (data->partial_defs.is_empty ()
3371 : 73764 : && known_subrange_p (offset, maxsize, offset2, size2)
3372 : : /* ??? We can't handle bitfield precision extracts without
3373 : : either using an alternate type for the BIT_FIELD_REF and
3374 : : then doing a conversion or possibly adjusting the offset
3375 : : according to endianness. */
3376 : 48770 : && (! INTEGRAL_TYPE_P (vr->type)
3377 : 35181 : || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3378 : 90976 : && multiple_p (ref->size, BITS_PER_UNIT))
3379 : : {
3380 : 43730 : tree val = NULL_TREE;
3381 : 87454 : if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3382 : 48119 : || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3383 : : {
3384 : 42598 : gimple_match_op op (gimple_match_cond::UNCOND,
3385 : 42598 : BIT_FIELD_REF, vr->type,
3386 : : SSA_VAL (def_rhs),
3387 : : bitsize_int (ref->size),
3388 : 42598 : bitsize_int (offset - offset2));
3389 : 42598 : val = vn_nary_build_or_lookup (&op);
3390 : : }
3391 : 1132 : else if (known_eq (ref->size, size2))
3392 : : {
3393 : 1110 : gimple_match_op op (gimple_match_cond::UNCOND,
3394 : 1110 : VIEW_CONVERT_EXPR, vr->type,
3395 : 1110 : SSA_VAL (def_rhs));
3396 : 1110 : val = vn_nary_build_or_lookup (&op);
3397 : : }
3398 : 43708 : if (val
3399 : 43708 : && (TREE_CODE (val) != SSA_NAME
3400 : 42908 : || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3401 : 43689 : return data->finish (ao_ref_alias_set (&lhs_ref),
3402 : 80518 : ao_ref_base_alias_set (&lhs_ref), val);
3403 : : }
3404 : 36829 : else if (maxsize.is_constant (&maxsizei)
3405 : 36829 : && offset.is_constant (&offseti)
3406 : 36829 : && offset2.is_constant (&offset2i)
3407 : 36829 : && size2.is_constant (&size2i)
3408 : 36829 : && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3409 : : {
3410 : 36829 : pd_data pd;
3411 : 36829 : pd.rhs = SSA_VAL (def_rhs);
3412 : 36829 : pd.rhs_off = 0;
3413 : 36829 : pd.offset = offset2i;
3414 : 36829 : pd.size = size2i;
3415 : 36829 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3416 : : ao_ref_base_alias_set (&lhs_ref),
3417 : : offseti, maxsizei);
3418 : : }
3419 : : }
3420 : : }
3421 : :
3422 : : /* 4b) Assignment done via one of the vectorizer internal store
3423 : : functions where we may be able to access pieces from or we can
3424 : : combine to a larger entity. */
3425 : 19354109 : else if (known_eq (ref->size, maxsize)
3426 : 18806126 : && is_gimple_reg_type (vr->type)
3427 : 18484196 : && !reverse_storage_order_for_component_p (vr->operands)
3428 : 18481469 : && !contains_storage_order_barrier_p (vr->operands)
3429 : 18481469 : && is_gimple_call (def_stmt)
3430 : 15026796 : && gimple_call_internal_p (def_stmt)
3431 : 19718994 : && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3432 : : {
3433 : 46 : gcall *call = as_a <gcall *> (def_stmt);
3434 : 46 : internal_fn fn = gimple_call_internal_fn (call);
3435 : :
3436 : 46 : tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3437 : 46 : switch (fn)
3438 : : {
3439 : 46 : case IFN_MASK_STORE:
3440 : 46 : mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3441 : 46 : mask = vn_valueize (mask);
3442 : 46 : if (TREE_CODE (mask) != VECTOR_CST)
3443 : 38 : return (void *)-1;
3444 : : break;
3445 : 0 : case IFN_LEN_STORE:
3446 : 0 : {
3447 : 0 : int len_index = internal_fn_len_index (fn);
3448 : 0 : len = gimple_call_arg (call, len_index);
3449 : 0 : bias = gimple_call_arg (call, len_index + 1);
3450 : 0 : if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3451 : : return (void *) -1;
3452 : : break;
3453 : : }
3454 : : default:
3455 : : return (void *)-1;
3456 : : }
3457 : 14 : tree def_rhs = gimple_call_arg (call,
3458 : 14 : internal_fn_stored_value_index (fn));
3459 : 14 : def_rhs = vn_valueize (def_rhs);
3460 : 14 : if (TREE_CODE (def_rhs) != VECTOR_CST)
3461 : : return (void *)-1;
3462 : :
3463 : 14 : ao_ref_init_from_ptr_and_size (&lhs_ref,
3464 : : vn_valueize (gimple_call_arg (call, 0)),
3465 : 14 : TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3466 : 14 : tree base2;
3467 : 14 : poly_int64 offset2, size2, maxsize2;
3468 : 14 : HOST_WIDE_INT offset2i, size2i, offseti;
3469 : 14 : base2 = ao_ref_base (&lhs_ref);
3470 : 14 : offset2 = lhs_ref.offset;
3471 : 14 : size2 = lhs_ref.size;
3472 : 14 : maxsize2 = lhs_ref.max_size;
3473 : 14 : if (known_size_p (maxsize2)
3474 : 14 : && known_eq (maxsize2, size2)
3475 : 14 : && adjust_offsets_for_equal_base_address (base, &offset,
3476 : : base2, &offset2)
3477 : 6 : && maxsize.is_constant (&maxsizei)
3478 : 6 : && offset.is_constant (&offseti)
3479 : 6 : && offset2.is_constant (&offset2i)
3480 : 14 : && size2.is_constant (&size2i))
3481 : : {
3482 : 6 : if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3483 : : /* Poor-mans disambiguation. */
3484 : : return NULL;
3485 : 6 : else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3486 : : {
3487 : 6 : pd_data pd;
3488 : 6 : pd.rhs = def_rhs;
3489 : 6 : tree aa = gimple_call_arg (call, 1);
3490 : 6 : alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3491 : 6 : tree vectype = TREE_TYPE (def_rhs);
3492 : 6 : unsigned HOST_WIDE_INT elsz
3493 : 6 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3494 : 6 : if (mask)
3495 : : {
3496 : : HOST_WIDE_INT start = 0, length = 0;
3497 : : unsigned mask_idx = 0;
3498 : 48 : do
3499 : : {
3500 : 48 : if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3501 : : {
3502 : 24 : if (length != 0)
3503 : : {
3504 : 18 : pd.rhs_off = start;
3505 : 18 : pd.offset = offset2i + start;
3506 : 18 : pd.size = length;
3507 : 18 : if (ranges_known_overlap_p
3508 : 18 : (offset, maxsize, pd.offset, pd.size))
3509 : : {
3510 : 0 : void *res = data->push_partial_def
3511 : 0 : (pd, set, set, offseti, maxsizei);
3512 : 0 : if (res != NULL)
3513 : 6 : return res;
3514 : : }
3515 : : }
3516 : 24 : start = (mask_idx + 1) * elsz;
3517 : 24 : length = 0;
3518 : : }
3519 : : else
3520 : 24 : length += elsz;
3521 : 48 : mask_idx++;
3522 : : }
3523 : 48 : while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3524 : 6 : if (length != 0)
3525 : : {
3526 : 6 : pd.rhs_off = start;
3527 : 6 : pd.offset = offset2i + start;
3528 : 6 : pd.size = length;
3529 : 6 : if (ranges_known_overlap_p (offset, maxsize,
3530 : : pd.offset, pd.size))
3531 : 2 : return data->push_partial_def (pd, set, set,
3532 : 2 : offseti, maxsizei);
3533 : : }
3534 : : }
3535 : 0 : else if (fn == IFN_LEN_STORE)
3536 : : {
3537 : 0 : pd.offset = offset2i;
3538 : 0 : pd.size = (tree_to_uhwi (len)
3539 : 0 : + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3540 : 0 : if (BYTES_BIG_ENDIAN)
3541 : : pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3542 : : else
3543 : 0 : pd.rhs_off = 0;
3544 : 0 : if (ranges_known_overlap_p (offset, maxsize,
3545 : : pd.offset, pd.size))
3546 : 0 : return data->push_partial_def (pd, set, set,
3547 : 0 : offseti, maxsizei);
3548 : : }
3549 : : else
3550 : 0 : gcc_unreachable ();
3551 : 4 : return NULL;
3552 : : }
3553 : : }
3554 : : }
3555 : :
3556 : : /* 5) For aggregate copies translate the reference through them if
3557 : : the copy kills ref. */
3558 : 19354063 : else if (data->vn_walk_kind == VN_WALKREWRITE
3559 : 15343467 : && gimple_assign_single_p (def_stmt)
3560 : 2490019 : && !gimple_has_volatile_ops (def_stmt)
3561 : 21841800 : && (DECL_P (gimple_assign_rhs1 (def_stmt))
3562 : 1995174 : || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3563 : 1596585 : || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3564 : : {
3565 : 2255122 : tree base2;
3566 : 2255122 : int i, j, k;
3567 : 2255122 : auto_vec<vn_reference_op_s> rhs;
3568 : 2255122 : vn_reference_op_t vro;
3569 : 2255122 : ao_ref r;
3570 : :
3571 : 2255122 : gcc_assert (lhs_ref_ok);
3572 : :
3573 : : /* See if the assignment kills REF. */
3574 : 2255122 : base2 = ao_ref_base (&lhs_ref);
3575 : 2255122 : if (!lhs_ref.max_size_known_p ()
3576 : 2254691 : || (base != base2
3577 : 97210 : && (TREE_CODE (base) != MEM_REF
3578 : 83752 : || TREE_CODE (base2) != MEM_REF
3579 : 70179 : || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3580 : 32654 : || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3581 : 32654 : TREE_OPERAND (base2, 1))))
3582 : 4443546 : || !stmt_kills_ref_p (def_stmt, ref))
3583 : 421989 : return (void *)-1;
3584 : :
3585 : : /* Find the common base of ref and the lhs. lhs_ops already
3586 : : contains valueized operands for the lhs. */
3587 : 1833133 : poly_int64 extra_off = 0;
3588 : 1833133 : i = vr->operands.length () - 1;
3589 : 1833133 : j = lhs_ops.length () - 1;
3590 : :
3591 : : /* The base should be always equal due to the above check. */
3592 : 1833133 : if (! vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3593 : : return (void *)-1;
3594 : 1832965 : i--, j--;
3595 : :
3596 : : /* The 2nd component should always exist and be a MEM_REF. */
3597 : 1832965 : if (!(i >= 0 && j >= 0))
3598 : : ;
3599 : 1832965 : else if (vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3600 : 863582 : i--, j--;
3601 : 969383 : else if (vr->operands[i].opcode == MEM_REF
3602 : 967864 : && lhs_ops[j].opcode == MEM_REF
3603 : 967864 : && known_ne (lhs_ops[j].off, -1)
3604 : 1937247 : && known_ne (vr->operands[i].off, -1))
3605 : : {
3606 : 967864 : bool found = false;
3607 : : /* When we ge a mismatch at a MEM_REF that is not the sole component
3608 : : try finding a match in one of the outer components and continue
3609 : : stripping there. This happens when addresses of components get
3610 : : forwarded into dereferences. */
3611 : 967864 : if (i > 0)
3612 : : {
3613 : 102542 : int temi = i - 1;
3614 : 102542 : extra_off = vr->operands[i].off;
3615 : 102542 : while (temi >= 0
3616 : 222459 : && known_ne (vr->operands[temi].off, -1))
3617 : : {
3618 : 121311 : if (vr->operands[temi].type
3619 : 121311 : && lhs_ops[j].type
3620 : 242622 : && (TYPE_MAIN_VARIANT (vr->operands[temi].type)
3621 : 121311 : == TYPE_MAIN_VARIANT (lhs_ops[j].type)))
3622 : : {
3623 : 1394 : i = temi;
3624 : : /* Strip the component that was type matched to
3625 : : the MEM_REF. */
3626 : 1394 : extra_off += vr->operands[i].off - lhs_ops[j].off;
3627 : 1394 : i--, j--;
3628 : : /* Strip further equal components. */
3629 : 1394 : found = true;
3630 : 1394 : break;
3631 : : }
3632 : 119917 : extra_off += vr->operands[temi].off;
3633 : 119917 : temi--;
3634 : : }
3635 : : }
3636 : 967864 : if (!found && j > 0)
3637 : : {
3638 : 26347 : int temj = j - 1;
3639 : 26347 : extra_off = -lhs_ops[j].off;
3640 : 26347 : while (temj >= 0
3641 : 50942 : && known_ne (lhs_ops[temj].off, -1))
3642 : : {
3643 : 28212 : if (vr->operands[i].type
3644 : 28212 : && lhs_ops[temj].type
3645 : 56424 : && (TYPE_MAIN_VARIANT (vr->operands[i].type)
3646 : 28212 : == TYPE_MAIN_VARIANT (lhs_ops[temj].type)))
3647 : : {
3648 : 3617 : j = temj;
3649 : : /* Strip the component that was type matched to
3650 : : the MEM_REF. */
3651 : 3617 : extra_off += vr->operands[i].off - lhs_ops[j].off;
3652 : 3617 : i--, j--;
3653 : : /* Strip further equal components. */
3654 : 3617 : found = true;
3655 : 3617 : break;
3656 : : }
3657 : 24595 : extra_off += -lhs_ops[temj].off;
3658 : 24595 : temj--;
3659 : : }
3660 : : }
3661 : : /* When the LHS is already at the outermost level simply
3662 : : adjust for any offset difference. Further lookups
3663 : : will fail when there's too gross of a type compatibility
3664 : : issue. */
3665 : 967864 : if (!found && j == 0)
3666 : : {
3667 : 940123 : extra_off = vr->operands[i].off - lhs_ops[j].off;
3668 : 940123 : i--, j--;
3669 : 940123 : found = true;
3670 : : }
3671 : : /* If we did find a match we'd eventually append a MEM_REF
3672 : : as component. Don't. */
3673 : 967864 : if (!found)
3674 : : return (void *)-1;
3675 : : }
3676 : : else
3677 : : return (void *)-1;
3678 : :
3679 : : /* Strip further common components, attempting to consume lhs_ops
3680 : : in full. */
3681 : 1839884 : while (j >= 0 && i >= 0
3682 : 1839884 : && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3683 : : {
3684 : 31168 : i--;
3685 : 31168 : j--;
3686 : : }
3687 : :
3688 : : /* When we still didn't manage to strip off all components from
3689 : : lhs_op, opportunistically continue for those we can handle
3690 : : via extra_off. Note this is an attempt to fixup secondary
3691 : : copies after we hit the !found && j == 0 case above. */
3692 : : while (j != -1
3693 : 1811573 : && known_ne (lhs_ops[j].off, -1U))
3694 : : {
3695 : 2857 : extra_off += -lhs_ops[j].off;
3696 : 2857 : j--;
3697 : : }
3698 : :
3699 : : /* i now points to the first additional op.
3700 : : ??? LHS may not be completely contained in VR, one or more
3701 : : VIEW_CONVERT_EXPRs could be in its way. We could at least
3702 : : try handling outermost VIEW_CONVERT_EXPRs. */
3703 : 1808716 : if (j != -1)
3704 : : return (void *)-1;
3705 : :
3706 : : /* Punt if the additional ops contain a storage order barrier. */
3707 : 2956575 : for (k = i; k >= 0; k--)
3708 : : {
3709 : 1147859 : vro = &vr->operands[k];
3710 : 1147859 : if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3711 : : return (void *)-1;
3712 : : }
3713 : :
3714 : : /* Now re-write REF to be based on the rhs of the assignment. */
3715 : 1808716 : tree rhs1 = gimple_assign_rhs1 (def_stmt);
3716 : 1808716 : copy_reference_ops_from_ref (rhs1, &rhs);
3717 : :
3718 : : /* Apply an extra offset to the inner MEM_REF of the RHS. */
3719 : 1808716 : bool force_no_tbaa = false;
3720 : 1808716 : if (maybe_ne (extra_off, 0))
3721 : : {
3722 : 648028 : if (rhs.length () < 2)
3723 : : return (void *)-1;
3724 : 648028 : int ix = rhs.length () - 2;
3725 : 648028 : if (rhs[ix].opcode != MEM_REF
3726 : 648028 : || known_eq (rhs[ix].off, -1))
3727 : : return (void *)-1;
3728 : 648024 : rhs[ix].off += extra_off;
3729 : 648024 : rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3730 : 648024 : build_int_cst (TREE_TYPE (rhs[ix].op0),
3731 : : extra_off));
3732 : : /* When we have offsetted the RHS, reading only parts of it,
3733 : : we can no longer use the original TBAA type, force alias-set
3734 : : zero. */
3735 : 648024 : force_no_tbaa = true;
3736 : : }
3737 : :
3738 : : /* Save the operands since we need to use the original ones for
3739 : : the hash entry we use. */
3740 : 1808712 : if (!data->saved_operands.exists ())
3741 : 1732976 : data->saved_operands = vr->operands.copy ();
3742 : :
3743 : : /* We need to pre-pend vr->operands[0..i] to rhs. */
3744 : 1808712 : vec<vn_reference_op_s> old = vr->operands;
3745 : 5426136 : if (i + 1 + rhs.length () > vr->operands.length ())
3746 : 1142905 : vr->operands.safe_grow (i + 1 + rhs.length (), true);
3747 : : else
3748 : 665807 : vr->operands.truncate (i + 1 + rhs.length ());
3749 : 6606260 : FOR_EACH_VEC_ELT (rhs, j, vro)
3750 : 4797548 : vr->operands[i + 1 + j] = *vro;
3751 : 1808712 : valueize_refs (&vr->operands);
3752 : 3617424 : if (old == shared_lookup_references)
3753 : 1808712 : shared_lookup_references = vr->operands;
3754 : 1808712 : vr->hashcode = vn_reference_compute_hash (vr);
3755 : :
3756 : : /* Try folding the new reference to a constant. */
3757 : 1808712 : tree val = fully_constant_vn_reference_p (vr);
3758 : 1808712 : if (val)
3759 : : {
3760 : 20643 : if (data->partial_defs.is_empty ())
3761 : 20634 : return data->finish (ao_ref_alias_set (&lhs_ref),
3762 : 20634 : ao_ref_base_alias_set (&lhs_ref), val);
3763 : : /* This is the only interesting case for partial-def handling
3764 : : coming from targets that like to gimplify init-ctors as
3765 : : aggregate copies from constant data like aarch64 for
3766 : : PR83518. */
3767 : 9 : if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3768 : : {
3769 : 9 : pd_data pd;
3770 : 9 : pd.rhs = val;
3771 : 9 : pd.rhs_off = 0;
3772 : 9 : pd.offset = 0;
3773 : 9 : pd.size = maxsizei;
3774 : 9 : return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3775 : : ao_ref_base_alias_set (&lhs_ref),
3776 : : 0, maxsizei);
3777 : : }
3778 : : }
3779 : :
3780 : : /* Continuing with partial defs isn't easily possible here, we
3781 : : have to find a full def from further lookups from here. Probably
3782 : : not worth the special-casing everywhere. */
3783 : 2241687 : if (!data->partial_defs.is_empty ())
3784 : : return (void *)-1;
3785 : :
3786 : : /* Adjust *ref from the new operands. */
3787 : 1780861 : ao_ref rhs1_ref;
3788 : 1780861 : ao_ref_init (&rhs1_ref, rhs1);
3789 : 2917153 : if (!ao_ref_init_from_vn_reference (&r,
3790 : : force_no_tbaa ? 0
3791 : 1136292 : : ao_ref_alias_set (&rhs1_ref),
3792 : : force_no_tbaa ? 0
3793 : 1136292 : : ao_ref_base_alias_set (&rhs1_ref),
3794 : : vr->type, vr->operands))
3795 : : return (void *)-1;
3796 : : /* This can happen with bitfields. */
3797 : 1780861 : if (maybe_ne (ref->size, r.size))
3798 : : {
3799 : : /* If the access lacks some subsetting simply apply that by
3800 : : shortening it. That in the end can only be successful
3801 : : if we can pun the lookup result which in turn requires
3802 : : exact offsets. */
3803 : 0 : if (known_eq (r.size, r.max_size)
3804 : 0 : && known_lt (ref->size, r.size))
3805 : 0 : r.size = r.max_size = ref->size;
3806 : : else
3807 : : return (void *)-1;
3808 : : }
3809 : 1780861 : *ref = r;
3810 : 1780861 : vr->offset = r.offset;
3811 : 1780861 : vr->max_size = r.max_size;
3812 : :
3813 : : /* Do not update last seen VUSE after translating. */
3814 : 1780861 : data->last_vuse_ptr = NULL;
3815 : : /* Invalidate the original access path since it now contains
3816 : : the wrong base. */
3817 : 1780861 : data->orig_ref.ref = NULL_TREE;
3818 : : /* Use the alias-set of this LHS for recording an eventual result. */
3819 : 1780861 : if (data->first_set == -2)
3820 : : {
3821 : 1706350 : data->first_set = ao_ref_alias_set (&lhs_ref);
3822 : 1706350 : data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3823 : : }
3824 : :
3825 : : /* Keep looking for the adjusted *REF / VR pair. */
3826 : 1780861 : return NULL;
3827 : 2255122 : }
3828 : :
3829 : : /* 6) For memcpy copies translate the reference through them if the copy
3830 : : kills ref. But we cannot (easily) do this translation if the memcpy is
3831 : : a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3832 : : can modify the storage order of objects (see storage_order_barrier_p). */
3833 : 17098941 : else if (data->vn_walk_kind == VN_WALKREWRITE
3834 : 13088345 : && is_gimple_reg_type (vr->type)
3835 : : /* ??? Handle BCOPY as well. */
3836 : 13080475 : && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3837 : 12996624 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3838 : 12996201 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3839 : 12995015 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3840 : 12994773 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3841 : 12967798 : || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3842 : 113005 : && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3843 : 104187 : || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3844 : 112967 : && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3845 : 75928 : || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3846 : 112952 : && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), ©_size)
3847 : 63110 : || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3848 : 63110 : && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3849 : : ©_size)))
3850 : : /* Handling this is more complicated, give up for now. */
3851 : 17150503 : && data->partial_defs.is_empty ())
3852 : : {
3853 : 51258 : tree lhs, rhs;
3854 : 51258 : ao_ref r;
3855 : 51258 : poly_int64 rhs_offset, lhs_offset;
3856 : 51258 : vn_reference_op_s op;
3857 : 51258 : poly_uint64 mem_offset;
3858 : 51258 : poly_int64 at, byte_maxsize;
3859 : :
3860 : : /* Only handle non-variable, addressable refs. */
3861 : 51258 : if (maybe_ne (ref->size, maxsize)
3862 : 50801 : || !multiple_p (offset, BITS_PER_UNIT, &at)
3863 : 51258 : || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3864 : 457 : return (void *)-1;
3865 : :
3866 : : /* Extract a pointer base and an offset for the destination. */
3867 : 50801 : lhs = gimple_call_arg (def_stmt, 0);
3868 : 50801 : lhs_offset = 0;
3869 : 50801 : if (TREE_CODE (lhs) == SSA_NAME)
3870 : : {
3871 : 43183 : lhs = vn_valueize (lhs);
3872 : 43183 : if (TREE_CODE (lhs) == SSA_NAME)
3873 : : {
3874 : 42887 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3875 : 42887 : if (gimple_assign_single_p (def_stmt)
3876 : 42887 : && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3877 : 2303 : lhs = gimple_assign_rhs1 (def_stmt);
3878 : : }
3879 : : }
3880 : 50801 : if (TREE_CODE (lhs) == ADDR_EXPR)
3881 : : {
3882 : 14419 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3883 : 14130 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3884 : : return (void *)-1;
3885 : 10077 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3886 : : &lhs_offset);
3887 : 10077 : if (!tem)
3888 : : return (void *)-1;
3889 : 9463 : if (TREE_CODE (tem) == MEM_REF
3890 : 9463 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3891 : : {
3892 : 1689 : lhs = TREE_OPERAND (tem, 0);
3893 : 1689 : if (TREE_CODE (lhs) == SSA_NAME)
3894 : 1689 : lhs = vn_valueize (lhs);
3895 : 1689 : lhs_offset += mem_offset;
3896 : : }
3897 : 7774 : else if (DECL_P (tem))
3898 : 7774 : lhs = build_fold_addr_expr (tem);
3899 : : else
3900 : : return (void *)-1;
3901 : : }
3902 : 50047 : if (TREE_CODE (lhs) != SSA_NAME
3903 : 7775 : && TREE_CODE (lhs) != ADDR_EXPR)
3904 : : return (void *)-1;
3905 : :
3906 : : /* Extract a pointer base and an offset for the source. */
3907 : 50047 : rhs = gimple_call_arg (def_stmt, 1);
3908 : 50047 : rhs_offset = 0;
3909 : 50047 : if (TREE_CODE (rhs) == SSA_NAME)
3910 : 17279 : rhs = vn_valueize (rhs);
3911 : 50047 : if (TREE_CODE (rhs) == ADDR_EXPR)
3912 : : {
3913 : 45525 : if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3914 : 34096 : && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3915 : : return (void *)-1;
3916 : 33652 : tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3917 : : &rhs_offset);
3918 : 33652 : if (!tem)
3919 : : return (void *)-1;
3920 : 33652 : if (TREE_CODE (tem) == MEM_REF
3921 : 33652 : && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3922 : : {
3923 : 0 : rhs = TREE_OPERAND (tem, 0);
3924 : 0 : rhs_offset += mem_offset;
3925 : : }
3926 : 33652 : else if (DECL_P (tem)
3927 : 28838 : || TREE_CODE (tem) == STRING_CST)
3928 : 33652 : rhs = build_fold_addr_expr (tem);
3929 : : else
3930 : : return (void *)-1;
3931 : : }
3932 : 50047 : if (TREE_CODE (rhs) == SSA_NAME)
3933 : 16395 : rhs = SSA_VAL (rhs);
3934 : 33652 : else if (TREE_CODE (rhs) != ADDR_EXPR)
3935 : : return (void *)-1;
3936 : :
3937 : : /* The bases of the destination and the references have to agree. */
3938 : 50047 : if (TREE_CODE (base) == MEM_REF)
3939 : : {
3940 : 19785 : if (TREE_OPERAND (base, 0) != lhs
3941 : 19785 : || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
3942 : 16467 : return (void *) -1;
3943 : 9952 : at += mem_offset;
3944 : : }
3945 : 30262 : else if (!DECL_P (base)
3946 : 29426 : || TREE_CODE (lhs) != ADDR_EXPR
3947 : 36897 : || TREE_OPERAND (lhs, 0) != base)
3948 : : return (void *)-1;
3949 : :
3950 : : /* If the access is completely outside of the memcpy destination
3951 : : area there is no aliasing. */
3952 : 9952 : if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
3953 : : return NULL;
3954 : : /* And the access has to be contained within the memcpy destination. */
3955 : 9917 : if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
3956 : : return (void *)-1;
3957 : :
3958 : : /* Save the operands since we need to use the original ones for
3959 : : the hash entry we use. */
3960 : 9556 : if (!data->saved_operands.exists ())
3961 : 9209 : data->saved_operands = vr->operands.copy ();
3962 : :
3963 : : /* Make room for 2 operands in the new reference. */
3964 : 9556 : if (vr->operands.length () < 2)
3965 : : {
3966 : 0 : vec<vn_reference_op_s> old = vr->operands;
3967 : 0 : vr->operands.safe_grow_cleared (2, true);
3968 : 0 : if (old == shared_lookup_references)
3969 : 0 : shared_lookup_references = vr->operands;
3970 : : }
3971 : : else
3972 : 9556 : vr->operands.truncate (2);
3973 : :
3974 : : /* The looked-through reference is a simple MEM_REF. */
3975 : 9556 : memset (&op, 0, sizeof (op));
3976 : 9556 : op.type = vr->type;
3977 : 9556 : op.opcode = MEM_REF;
3978 : 9556 : op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3979 : 9556 : op.off = at - lhs_offset + rhs_offset;
3980 : 9556 : vr->operands[0] = op;
3981 : 9556 : op.type = TREE_TYPE (rhs);
3982 : 9556 : op.opcode = TREE_CODE (rhs);
3983 : 9556 : op.op0 = rhs;
3984 : 9556 : op.off = -1;
3985 : 9556 : vr->operands[1] = op;
3986 : 9556 : vr->hashcode = vn_reference_compute_hash (vr);
3987 : :
3988 : : /* Try folding the new reference to a constant. */
3989 : 9556 : tree val = fully_constant_vn_reference_p (vr);
3990 : 9556 : if (val)
3991 : 1898 : return data->finish (0, 0, val);
3992 : :
3993 : : /* Adjust *ref from the new operands. */
3994 : 7658 : if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
3995 : : return (void *)-1;
3996 : : /* This can happen with bitfields. */
3997 : 7658 : if (maybe_ne (ref->size, r.size))
3998 : : return (void *)-1;
3999 : 7658 : *ref = r;
4000 : 7658 : vr->offset = r.offset;
4001 : 7658 : vr->max_size = r.max_size;
4002 : :
4003 : : /* Do not update last seen VUSE after translating. */
4004 : 7658 : data->last_vuse_ptr = NULL;
4005 : : /* Invalidate the original access path since it now contains
4006 : : the wrong base. */
4007 : 7658 : data->orig_ref.ref = NULL_TREE;
4008 : : /* Use the alias-set of this stmt for recording an eventual result. */
4009 : 7658 : if (data->first_set == -2)
4010 : : {
4011 : 7351 : data->first_set = 0;
4012 : 7351 : data->first_base_set = 0;
4013 : : }
4014 : :
4015 : : /* Keep looking for the adjusted *REF / VR pair. */
4016 : 7658 : return NULL;
4017 : : }
4018 : :
4019 : : /* Bail out and stop walking. */
4020 : : return (void *)-1;
4021 : : }
4022 : :
4023 : : /* Return a reference op vector from OP that can be used for
4024 : : vn_reference_lookup_pieces. The caller is responsible for releasing
4025 : : the vector. */
4026 : :
4027 : : vec<vn_reference_op_s>
4028 : 5288356 : vn_reference_operands_for_lookup (tree op)
4029 : : {
4030 : 5288356 : bool valueized;
4031 : 5288356 : return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
4032 : : }
4033 : :
4034 : : /* Lookup a reference operation by it's parts, in the current hash table.
4035 : : Returns the resulting value number if it exists in the hash table,
4036 : : NULL_TREE otherwise. VNRESULT will be filled in with the actual
4037 : : vn_reference_t stored in the hashtable if something is found. */
4038 : :
4039 : : tree
4040 : 8311804 : vn_reference_lookup_pieces (tree vuse, alias_set_type set,
4041 : : alias_set_type base_set, tree type,
4042 : : vec<vn_reference_op_s> operands,
4043 : : vn_reference_t *vnresult, vn_lookup_kind kind)
4044 : : {
4045 : 8311804 : struct vn_reference_s vr1;
4046 : 8311804 : vn_reference_t tmp;
4047 : 8311804 : tree cst;
4048 : :
4049 : 8311804 : if (!vnresult)
4050 : 0 : vnresult = &tmp;
4051 : 8311804 : *vnresult = NULL;
4052 : :
4053 : 8311804 : vr1.vuse = vuse_ssa_val (vuse);
4054 : 8311804 : shared_lookup_references.truncate (0);
4055 : 16623608 : shared_lookup_references.safe_grow (operands.length (), true);
4056 : 8311804 : memcpy (shared_lookup_references.address (),
4057 : 8311804 : operands.address (),
4058 : : sizeof (vn_reference_op_s)
4059 : 8311804 : * operands.length ());
4060 : 8311804 : bool valueized_p;
4061 : 8311804 : valueize_refs_1 (&shared_lookup_references, &valueized_p);
4062 : 8311804 : vr1.operands = shared_lookup_references;
4063 : 8311804 : vr1.type = type;
4064 : 8311804 : vr1.set = set;
4065 : 8311804 : vr1.base_set = base_set;
4066 : : /* We can pretend there's no extra info fed in since the ao_refs offset
4067 : : and max_size are computed only from the VN reference ops. */
4068 : 8311804 : vr1.offset = 0;
4069 : 8311804 : vr1.max_size = -1;
4070 : 8311804 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4071 : 8311804 : if ((cst = fully_constant_vn_reference_p (&vr1)))
4072 : : return cst;
4073 : :
4074 : 8292156 : vn_reference_lookup_1 (&vr1, vnresult);
4075 : 8292156 : if (!*vnresult
4076 : 3195540 : && kind != VN_NOWALK
4077 : 3195540 : && vr1.vuse)
4078 : : {
4079 : 3170954 : ao_ref r;
4080 : 3170954 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4081 : 3170954 : vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
4082 : 3170954 : false);
4083 : 3170954 : vec<vn_reference_op_s> ops_for_ref;
4084 : 3170954 : if (!valueized_p)
4085 : 3082146 : ops_for_ref = vr1.operands;
4086 : : else
4087 : : {
4088 : : /* For ao_ref_from_mem we have to ensure only available SSA names
4089 : : end up in base and the only convenient way to make this work
4090 : : for PRE is to re-valueize with that in mind. */
4091 : 177616 : ops_for_ref.create (operands.length ());
4092 : 177616 : ops_for_ref.quick_grow (operands.length ());
4093 : 88808 : memcpy (ops_for_ref.address (),
4094 : 88808 : operands.address (),
4095 : : sizeof (vn_reference_op_s)
4096 : 88808 : * operands.length ());
4097 : 88808 : valueize_refs_1 (&ops_for_ref, &valueized_p, true);
4098 : : }
4099 : 3170954 : if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
4100 : : ops_for_ref))
4101 : 3103866 : *vnresult
4102 : 3103866 : = ((vn_reference_t)
4103 : 3103866 : walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
4104 : : vn_reference_lookup_3, vuse_valueize,
4105 : : limit, &data));
4106 : 6341908 : if (ops_for_ref != shared_lookup_references)
4107 : 88808 : ops_for_ref.release ();
4108 : 6341908 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4109 : 3170954 : if (*vnresult
4110 : 459256 : && data.same_val
4111 : 3170954 : && (!(*vnresult)->result
4112 : 0 : || !operand_equal_p ((*vnresult)->result, data.same_val)))
4113 : : {
4114 : 0 : *vnresult = NULL;
4115 : 0 : return NULL_TREE;
4116 : : }
4117 : 3170954 : }
4118 : :
4119 : 8292156 : if (*vnresult)
4120 : 5555872 : return (*vnresult)->result;
4121 : :
4122 : : return NULL_TREE;
4123 : : }
4124 : :
4125 : : /* When OPERANDS is an ADDR_EXPR that can be possibly expressed as a
4126 : : POINTER_PLUS_EXPR return true and fill in its operands in OPS. */
4127 : :
4128 : : bool
4129 : 2269268 : vn_pp_nary_for_addr (const vec<vn_reference_op_s>& operands, tree ops[2])
4130 : : {
4131 : 4538536 : gcc_assert (operands[0].opcode == ADDR_EXPR
4132 : : && operands.last ().opcode == SSA_NAME);
4133 : : poly_int64 off = 0;
4134 : : vn_reference_op_t vro;
4135 : : unsigned i;
4136 : 7350669 : for (i = 1; operands.iterate (i, &vro); ++i)
4137 : : {
4138 : 7350669 : if (vro->opcode == SSA_NAME)
4139 : : break;
4140 : 5127579 : else if (known_eq (vro->off, -1))
4141 : : break;
4142 : 5081401 : off += vro->off;
4143 : : }
4144 : 2269268 : if (i == operands.length () - 1
4145 : 2223090 : && maybe_ne (off, 0)
4146 : : /* Make sure we the offset we accumulated in a 64bit int
4147 : : fits the address computation carried out in target
4148 : : offset precision. */
4149 : 3758120 : && (off.coeffs[0]
4150 : 1488852 : == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4151 : : {
4152 : 1488128 : gcc_assert (operands[i-1].opcode == MEM_REF);
4153 : 1488128 : ops[0] = operands[i].op0;
4154 : 1488128 : ops[1] = wide_int_to_tree (sizetype, off);
4155 : 1488128 : return true;
4156 : : }
4157 : : return false;
4158 : : }
4159 : :
4160 : : /* Lookup OP in the current hash table, and return the resulting value
4161 : : number if it exists in the hash table. Return NULL_TREE if it does
4162 : : not exist in the hash table or if the result field of the structure
4163 : : was NULL.. VNRESULT will be filled in with the vn_reference_t
4164 : : stored in the hashtable if one exists. When TBAA_P is false assume
4165 : : we are looking up a store and treat it as having alias-set zero.
4166 : : *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
4167 : : MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
4168 : : load is bitwise anded with MASK and so we are only interested in a subset
4169 : : of the bits and can ignore if the other bits are uninitialized or
4170 : : not initialized with constants. When doing redundant store removal
4171 : : the caller has to set REDUNDANT_STORE_REMOVAL_P. */
4172 : :
4173 : : tree
4174 : 101072636 : vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
4175 : : vn_reference_t *vnresult, bool tbaa_p,
4176 : : tree *last_vuse_ptr, tree mask,
4177 : : bool redundant_store_removal_p)
4178 : : {
4179 : 101072636 : vec<vn_reference_op_s> operands;
4180 : 101072636 : struct vn_reference_s vr1;
4181 : 101072636 : bool valueized_anything;
4182 : :
4183 : 101072636 : if (vnresult)
4184 : 100626803 : *vnresult = NULL;
4185 : :
4186 : 101072636 : vr1.vuse = vuse_ssa_val (vuse);
4187 : 202145272 : vr1.operands = operands
4188 : 101072636 : = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
4189 : :
4190 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
4191 : : this before the pass folding __builtin_object_size had a chance to run. */
4192 : 101072636 : if ((cfun->curr_properties & PROP_objsz)
4193 : 73412946 : && operands[0].opcode == ADDR_EXPR
4194 : 102209119 : && operands.last ().opcode == SSA_NAME)
4195 : : {
4196 : 1102811 : tree ops[2];
4197 : 1102811 : if (vn_pp_nary_for_addr (operands, ops))
4198 : : {
4199 : 726227 : tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4200 : 726227 : TREE_TYPE (op), ops, NULL);
4201 : 726227 : if (res)
4202 : 726227 : return res;
4203 : 726227 : return NULL_TREE;
4204 : : }
4205 : : }
4206 : :
4207 : 100346409 : vr1.type = TREE_TYPE (op);
4208 : 100346409 : ao_ref op_ref;
4209 : 100346409 : ao_ref_init (&op_ref, op);
4210 : 100346409 : vr1.set = ao_ref_alias_set (&op_ref);
4211 : 100346409 : vr1.base_set = ao_ref_base_alias_set (&op_ref);
4212 : 100346409 : vr1.offset = 0;
4213 : 100346409 : vr1.max_size = -1;
4214 : 100346409 : vr1.hashcode = vn_reference_compute_hash (&vr1);
4215 : 100346409 : if (mask == NULL_TREE)
4216 : 99977356 : if (tree cst = fully_constant_vn_reference_p (&vr1))
4217 : : return cst;
4218 : :
4219 : 100333277 : if (kind != VN_NOWALK && vr1.vuse)
4220 : : {
4221 : 58418843 : vn_reference_t wvnresult;
4222 : 58418843 : ao_ref r;
4223 : 58418843 : unsigned limit = param_sccvn_max_alias_queries_per_access;
4224 : 58418843 : auto_vec<vn_reference_op_s> ops_for_ref;
4225 : 58418843 : if (valueized_anything)
4226 : : {
4227 : 4508795 : copy_reference_ops_from_ref (op, &ops_for_ref);
4228 : 4508795 : bool tem;
4229 : 4508795 : valueize_refs_1 (&ops_for_ref, &tem, true);
4230 : : }
4231 : : /* Make sure to use a valueized reference if we valueized anything.
4232 : : Otherwise preserve the full reference for advanced TBAA. */
4233 : 58418843 : if (!valueized_anything
4234 : 58418843 : || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4235 : : vr1.type, ops_for_ref))
4236 : : {
4237 : 53910048 : ao_ref_init (&r, op);
4238 : : /* Record the extra info we're getting from the full ref. */
4239 : 53910048 : ao_ref_base (&r);
4240 : 53910048 : vr1.offset = r.offset;
4241 : 53910048 : vr1.max_size = r.max_size;
4242 : : }
4243 : 58418843 : vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4244 : : last_vuse_ptr, kind, tbaa_p, mask,
4245 : 112328891 : redundant_store_removal_p);
4246 : :
4247 : 58418843 : wvnresult
4248 : : = ((vn_reference_t)
4249 : 58418843 : walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4250 : : vn_reference_lookup_3, vuse_valueize, limit,
4251 : : &data));
4252 : 116837686 : gcc_checking_assert (vr1.operands == shared_lookup_references);
4253 : 58418843 : if (wvnresult)
4254 : : {
4255 : 8556308 : gcc_assert (mask == NULL_TREE);
4256 : 8556308 : if (data.same_val
4257 : 8556308 : && (!wvnresult->result
4258 : 67705 : || !operand_equal_p (wvnresult->result, data.same_val)))
4259 : 47027 : return NULL_TREE;
4260 : 8509281 : if (vnresult)
4261 : 8508165 : *vnresult = wvnresult;
4262 : 8509281 : return wvnresult->result;
4263 : : }
4264 : 49862535 : else if (mask)
4265 : 369053 : return data.masked_result;
4266 : :
4267 : : return NULL_TREE;
4268 : 58418843 : }
4269 : :
4270 : 41914434 : if (last_vuse_ptr)
4271 : 1426144 : *last_vuse_ptr = vr1.vuse;
4272 : 41914434 : if (mask)
4273 : : return NULL_TREE;
4274 : 41914434 : return vn_reference_lookup_1 (&vr1, vnresult);
4275 : : }
4276 : :
4277 : : /* Lookup CALL in the current hash table and return the entry in
4278 : : *VNRESULT if found. Populates *VR for the hashtable lookup. */
4279 : :
4280 : : void
4281 : 9194175 : vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4282 : : vn_reference_t vr)
4283 : : {
4284 : 9194175 : if (vnresult)
4285 : 9194175 : *vnresult = NULL;
4286 : :
4287 : 9194175 : tree vuse = gimple_vuse (call);
4288 : :
4289 : 9194175 : vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4290 : 9194175 : vr->operands = valueize_shared_reference_ops_from_call (call);
4291 : 9194175 : tree lhs = gimple_call_lhs (call);
4292 : : /* For non-SSA return values the referece ops contain the LHS. */
4293 : 5022944 : vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4294 : 13748093 : ? TREE_TYPE (lhs) : NULL_TREE);
4295 : 9194175 : vr->punned = false;
4296 : 9194175 : vr->set = 0;
4297 : 9194175 : vr->base_set = 0;
4298 : 9194175 : vr->offset = 0;
4299 : 9194175 : vr->max_size = -1;
4300 : 9194175 : vr->hashcode = vn_reference_compute_hash (vr);
4301 : 9194175 : vn_reference_lookup_1 (vr, vnresult);
4302 : 9194175 : }
4303 : :
4304 : : /* Insert OP into the current hash table with a value number of RESULT. */
4305 : :
4306 : : static void
4307 : 75073951 : vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4308 : : {
4309 : 75073951 : vn_reference_s **slot;
4310 : 75073951 : vn_reference_t vr1;
4311 : 75073951 : bool tem;
4312 : :
4313 : 75073951 : vec<vn_reference_op_s> operands
4314 : 75073951 : = valueize_shared_reference_ops_from_ref (op, &tem);
4315 : : /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4316 : : before the pass folding __builtin_object_size had a chance to run. */
4317 : 75073951 : if ((cfun->curr_properties & PROP_objsz)
4318 : 56426822 : && operands[0].opcode == ADDR_EXPR
4319 : 75998139 : && operands.last ().opcode == SSA_NAME)
4320 : : {
4321 : 893281 : tree ops[2];
4322 : 893281 : if (vn_pp_nary_for_addr (operands, ops))
4323 : : {
4324 : 575724 : vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4325 : 575724 : TREE_TYPE (op), ops, result,
4326 : 575724 : VN_INFO (result)->value_id);
4327 : 575724 : return;
4328 : : }
4329 : : }
4330 : :
4331 : 74498227 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4332 : 74498227 : if (TREE_CODE (result) == SSA_NAME)
4333 : 51551834 : vr1->value_id = VN_INFO (result)->value_id;
4334 : : else
4335 : 22946393 : vr1->value_id = get_or_alloc_constant_value_id (result);
4336 : 74498227 : vr1->vuse = vuse_ssa_val (vuse);
4337 : 74498227 : vr1->operands = operands.copy ();
4338 : 74498227 : vr1->type = TREE_TYPE (op);
4339 : 74498227 : vr1->punned = false;
4340 : 74498227 : ao_ref op_ref;
4341 : 74498227 : ao_ref_init (&op_ref, op);
4342 : 74498227 : vr1->set = ao_ref_alias_set (&op_ref);
4343 : 74498227 : vr1->base_set = ao_ref_base_alias_set (&op_ref);
4344 : : /* Specifically use an unknown extent here, we're not doing any lookup
4345 : : and assume the caller didn't either (or it went VARYING). */
4346 : 74498227 : vr1->offset = 0;
4347 : 74498227 : vr1->max_size = -1;
4348 : 74498227 : vr1->hashcode = vn_reference_compute_hash (vr1);
4349 : 74498227 : vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4350 : 74498227 : vr1->result_vdef = vdef;
4351 : :
4352 : 74498227 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4353 : : INSERT);
4354 : :
4355 : : /* Because IL walking on reference lookup can end up visiting
4356 : : a def that is only to be visited later in iteration order
4357 : : when we are about to make an irreducible region reducible
4358 : : the def can be effectively processed and its ref being inserted
4359 : : by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4360 : : but save a lookup if we deal with already inserted refs here. */
4361 : 74498227 : if (*slot)
4362 : : {
4363 : : /* We cannot assert that we have the same value either because
4364 : : when disentangling an irreducible region we may end up visiting
4365 : : a use before the corresponding def. That's a missed optimization
4366 : : only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4367 : 0 : if (dump_file && (dump_flags & TDF_DETAILS)
4368 : 0 : && !operand_equal_p ((*slot)->result, vr1->result, 0))
4369 : : {
4370 : 0 : fprintf (dump_file, "Keeping old value ");
4371 : 0 : print_generic_expr (dump_file, (*slot)->result);
4372 : 0 : fprintf (dump_file, " because of collision\n");
4373 : : }
4374 : 0 : free_reference (vr1);
4375 : 0 : obstack_free (&vn_tables_obstack, vr1);
4376 : 0 : return;
4377 : : }
4378 : :
4379 : 74498227 : *slot = vr1;
4380 : 74498227 : vr1->next = last_inserted_ref;
4381 : 74498227 : last_inserted_ref = vr1;
4382 : : }
4383 : :
4384 : : /* Insert a reference by it's pieces into the current hash table with
4385 : : a value number of RESULT. Return the resulting reference
4386 : : structure we created. */
4387 : :
4388 : : vn_reference_t
4389 : 3821184 : vn_reference_insert_pieces (tree vuse, alias_set_type set,
4390 : : alias_set_type base_set,
4391 : : poly_int64 offset, poly_int64 max_size, tree type,
4392 : : vec<vn_reference_op_s> operands,
4393 : : tree result, unsigned int value_id)
4394 : :
4395 : : {
4396 : 3821184 : vn_reference_s **slot;
4397 : 3821184 : vn_reference_t vr1;
4398 : :
4399 : 3821184 : vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4400 : 3821184 : vr1->value_id = value_id;
4401 : 3821184 : vr1->vuse = vuse_ssa_val (vuse);
4402 : 3821184 : vr1->operands = operands;
4403 : 3821184 : valueize_refs (&vr1->operands);
4404 : 3821184 : vr1->type = type;
4405 : 3821184 : vr1->punned = false;
4406 : 3821184 : vr1->set = set;
4407 : 3821184 : vr1->base_set = base_set;
4408 : 3821184 : vr1->offset = offset;
4409 : 3821184 : vr1->max_size = max_size;
4410 : 3821184 : vr1->hashcode = vn_reference_compute_hash (vr1);
4411 : 3821184 : if (result && TREE_CODE (result) == SSA_NAME)
4412 : 281084 : result = SSA_VAL (result);
4413 : 3821184 : vr1->result = result;
4414 : 3821184 : vr1->result_vdef = NULL_TREE;
4415 : :
4416 : 3821184 : slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4417 : : INSERT);
4418 : :
4419 : : /* At this point we should have all the things inserted that we have
4420 : : seen before, and we should never try inserting something that
4421 : : already exists. */
4422 : 3821184 : gcc_assert (!*slot);
4423 : :
4424 : 3821184 : *slot = vr1;
4425 : 3821184 : vr1->next = last_inserted_ref;
4426 : 3821184 : last_inserted_ref = vr1;
4427 : 3821184 : return vr1;
4428 : : }
4429 : :
4430 : : /* Compute and return the hash value for nary operation VBO1. */
4431 : :
4432 : : hashval_t
4433 : 303358559 : vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4434 : : {
4435 : 303358559 : inchash::hash hstate;
4436 : 303358559 : unsigned i;
4437 : :
4438 : 303358559 : if (((vno1->length == 2
4439 : 255771351 : && commutative_tree_code (vno1->opcode))
4440 : 138801892 : || (vno1->length == 3
4441 : 1401235 : && commutative_ternary_tree_code (vno1->opcode)))
4442 : 467917253 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4443 : 3112651 : std::swap (vno1->op[0], vno1->op[1]);
4444 : 300245908 : else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4445 : 300245908 : && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4446 : : {
4447 : 480277 : std::swap (vno1->op[0], vno1->op[1]);
4448 : 480277 : vno1->opcode = swap_tree_comparison (vno1->opcode);
4449 : : }
4450 : :
4451 : 303358559 : hstate.add_int (vno1->opcode);
4452 : 865982988 : for (i = 0; i < vno1->length; ++i)
4453 : 562624429 : inchash::add_expr (vno1->op[i], hstate);
4454 : :
4455 : 303358559 : return hstate.end ();
4456 : : }
4457 : :
4458 : : /* Compare nary operations VNO1 and VNO2 and return true if they are
4459 : : equivalent. */
4460 : :
4461 : : bool
4462 : 968835325 : vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4463 : : {
4464 : 968835325 : unsigned i;
4465 : :
4466 : 968835325 : if (vno1->hashcode != vno2->hashcode)
4467 : : return false;
4468 : :
4469 : 49888358 : if (vno1->length != vno2->length)
4470 : : return false;
4471 : :
4472 : 49888358 : if (vno1->opcode != vno2->opcode
4473 : 49888358 : || !types_compatible_p (vno1->type, vno2->type))
4474 : 1206041 : return false;
4475 : :
4476 : 140610589 : for (i = 0; i < vno1->length; ++i)
4477 : 92023142 : if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4478 : : return false;
4479 : :
4480 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
4481 : : of op1. Need to check to make sure they are the same. */
4482 : 48587447 : if (vno1->opcode == BIT_INSERT_EXPR
4483 : 528 : && TREE_CODE (vno1->op[1]) == INTEGER_CST
4484 : 48587554 : && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4485 : 107 : != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4486 : : return false;
4487 : :
4488 : : return true;
4489 : : }
4490 : :
4491 : : /* Initialize VNO from the pieces provided. */
4492 : :
4493 : : static void
4494 : 188385617 : init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4495 : : enum tree_code code, tree type, tree *ops)
4496 : : {
4497 : 188385617 : vno->opcode = code;
4498 : 188385617 : vno->length = length;
4499 : 188385617 : vno->type = type;
4500 : 4783424 : memcpy (&vno->op[0], ops, sizeof (tree) * length);
4501 : 0 : }
4502 : :
4503 : : /* Return the number of operands for a vn_nary ops structure from STMT. */
4504 : :
4505 : : unsigned int
4506 : 108949256 : vn_nary_length_from_stmt (gimple *stmt)
4507 : : {
4508 : 108949256 : switch (gimple_assign_rhs_code (stmt))
4509 : : {
4510 : : case REALPART_EXPR:
4511 : : case IMAGPART_EXPR:
4512 : : case VIEW_CONVERT_EXPR:
4513 : : return 1;
4514 : :
4515 : 532524 : case BIT_FIELD_REF:
4516 : 532524 : return 3;
4517 : :
4518 : 503042 : case CONSTRUCTOR:
4519 : 503042 : return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4520 : :
4521 : 104597294 : default:
4522 : 104597294 : return gimple_num_ops (stmt) - 1;
4523 : : }
4524 : : }
4525 : :
4526 : : /* Initialize VNO from STMT. */
4527 : :
4528 : : void
4529 : 108949256 : init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4530 : : {
4531 : 108949256 : unsigned i;
4532 : :
4533 : 108949256 : vno->opcode = gimple_assign_rhs_code (stmt);
4534 : 108949256 : vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4535 : 108949256 : switch (vno->opcode)
4536 : : {
4537 : 3316396 : case REALPART_EXPR:
4538 : 3316396 : case IMAGPART_EXPR:
4539 : 3316396 : case VIEW_CONVERT_EXPR:
4540 : 3316396 : vno->length = 1;
4541 : 3316396 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4542 : 3316396 : break;
4543 : :
4544 : 532524 : case BIT_FIELD_REF:
4545 : 532524 : vno->length = 3;
4546 : 532524 : vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4547 : 532524 : vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4548 : 532524 : vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4549 : 532524 : break;
4550 : :
4551 : 503042 : case CONSTRUCTOR:
4552 : 503042 : vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4553 : 1966381 : for (i = 0; i < vno->length; ++i)
4554 : 1463339 : vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4555 : : break;
4556 : :
4557 : 104597294 : default:
4558 : 104597294 : gcc_checking_assert (!gimple_assign_single_p (stmt));
4559 : 104597294 : vno->length = gimple_num_ops (stmt) - 1;
4560 : 286328980 : for (i = 0; i < vno->length; ++i)
4561 : 181731686 : vno->op[i] = gimple_op (stmt, i + 1);
4562 : : }
4563 : 108949256 : }
4564 : :
4565 : : /* Compute the hashcode for VNO and look for it in the hash table;
4566 : : return the resulting value number if it exists in the hash table.
4567 : : Return NULL_TREE if it does not exist in the hash table or if the
4568 : : result field of the operation is NULL. VNRESULT will contain the
4569 : : vn_nary_op_t from the hashtable if it exists. */
4570 : :
4571 : : static tree
4572 : 131374420 : vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4573 : : {
4574 : 131374420 : vn_nary_op_s **slot;
4575 : :
4576 : 131374420 : if (vnresult)
4577 : 124251386 : *vnresult = NULL;
4578 : :
4579 : 365331131 : for (unsigned i = 0; i < vno->length; ++i)
4580 : 233956711 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4581 : 165792497 : vno->op[i] = SSA_VAL (vno->op[i]);
4582 : :
4583 : 131374420 : vno->hashcode = vn_nary_op_compute_hash (vno);
4584 : 131374420 : slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4585 : 131374420 : if (!slot)
4586 : : return NULL_TREE;
4587 : 17490779 : if (vnresult)
4588 : 17036959 : *vnresult = *slot;
4589 : 17490779 : return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4590 : : }
4591 : :
4592 : : /* Lookup a n-ary operation by its pieces and return the resulting value
4593 : : number if it exists in the hash table. Return NULL_TREE if it does
4594 : : not exist in the hash table or if the result field of the operation
4595 : : is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4596 : : if it exists. */
4597 : :
4598 : : tree
4599 : 75005958 : vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4600 : : tree type, tree *ops, vn_nary_op_t *vnresult)
4601 : : {
4602 : 75005958 : vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4603 : : sizeof_vn_nary_op (length));
4604 : 75005958 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4605 : 75005958 : return vn_nary_op_lookup_1 (vno1, vnresult);
4606 : : }
4607 : :
4608 : : /* Lookup the rhs of STMT in the current hash table, and return the resulting
4609 : : value number if it exists in the hash table. Return NULL_TREE if
4610 : : it does not exist in the hash table. VNRESULT will contain the
4611 : : vn_nary_op_t from the hashtable if it exists. */
4612 : :
4613 : : tree
4614 : 56368462 : vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4615 : : {
4616 : 56368462 : vn_nary_op_t vno1
4617 : 56368462 : = XALLOCAVAR (struct vn_nary_op_s,
4618 : : sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4619 : 56368462 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4620 : 56368462 : return vn_nary_op_lookup_1 (vno1, vnresult);
4621 : : }
4622 : :
4623 : : /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4624 : :
4625 : : vn_nary_op_t
4626 : 170998769 : alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4627 : : {
4628 : 170998769 : return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4629 : : }
4630 : :
4631 : : /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4632 : : obstack. */
4633 : :
4634 : : static vn_nary_op_t
4635 : 153550720 : alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4636 : : {
4637 : 0 : vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4638 : :
4639 : 153550720 : vno1->value_id = value_id;
4640 : 153550720 : vno1->length = length;
4641 : 153550720 : vno1->predicated_values = 0;
4642 : 153550720 : vno1->u.result = result;
4643 : :
4644 : 153550720 : return vno1;
4645 : : }
4646 : :
4647 : : /* Insert VNO into TABLE. */
4648 : :
4649 : : static vn_nary_op_t
4650 : 158473972 : vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4651 : : {
4652 : 158473972 : vn_nary_op_s **slot;
4653 : :
4654 : 158473972 : gcc_assert (! vno->predicated_values
4655 : : || (! vno->u.values->next
4656 : : && vno->u.values->n == 1));
4657 : :
4658 : 463664560 : for (unsigned i = 0; i < vno->length; ++i)
4659 : 305190588 : if (TREE_CODE (vno->op[i]) == SSA_NAME)
4660 : 198642671 : vno->op[i] = SSA_VAL (vno->op[i]);
4661 : :
4662 : 158473972 : vno->hashcode = vn_nary_op_compute_hash (vno);
4663 : 158473972 : slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4664 : 158473972 : vno->unwind_to = *slot;
4665 : 158473972 : if (*slot)
4666 : : {
4667 : : /* Prefer non-predicated values.
4668 : : ??? Only if those are constant, otherwise, with constant predicated
4669 : : value, turn them into predicated values with entry-block validity
4670 : : (??? but we always find the first valid result currently). */
4671 : 30111298 : if ((*slot)->predicated_values
4672 : 29356507 : && ! vno->predicated_values)
4673 : : {
4674 : : /* ??? We cannot remove *slot from the unwind stack list.
4675 : : For the moment we deal with this by skipping not found
4676 : : entries but this isn't ideal ... */
4677 : 82048 : *slot = vno;
4678 : : /* ??? Maintain a stack of states we can unwind in
4679 : : vn_nary_op_s? But how far do we unwind? In reality
4680 : : we need to push change records somewhere... Or not
4681 : : unwind vn_nary_op_s and linking them but instead
4682 : : unwind the results "list", linking that, which also
4683 : : doesn't move on hashtable resize. */
4684 : : /* We can also have a ->unwind_to recording *slot there.
4685 : : That way we can make u.values a fixed size array with
4686 : : recording the number of entries but of course we then
4687 : : have always N copies for each unwind_to-state. Or we
4688 : : make sure to only ever append and each unwinding will
4689 : : pop off one entry (but how to deal with predicated
4690 : : replaced with non-predicated here?) */
4691 : 82048 : vno->next = last_inserted_nary;
4692 : 82048 : last_inserted_nary = vno;
4693 : 82048 : return vno;
4694 : : }
4695 : 30029250 : else if (vno->predicated_values
4696 : 30025190 : && ! (*slot)->predicated_values)
4697 : : return *slot;
4698 : 29278519 : else if (vno->predicated_values
4699 : 29274459 : && (*slot)->predicated_values)
4700 : : {
4701 : : /* ??? Factor this all into a insert_single_predicated_value
4702 : : routine. */
4703 : 29274459 : gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4704 : 29274459 : basic_block vno_bb
4705 : 29274459 : = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4706 : 29274459 : vn_pval *nval = vno->u.values;
4707 : 29274459 : vn_pval **next = &vno->u.values;
4708 : 29274459 : vn_pval *ins = NULL;
4709 : 29274459 : vn_pval *ins_at = NULL;
4710 : : /* Find an existing value to append to. */
4711 : 55295775 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4712 : : {
4713 : 30190888 : if (expressions_equal_p (val->result, nval->result))
4714 : : {
4715 : : /* Limit the number of places we register a predicate
4716 : : as valid. */
4717 : 4169572 : if (val->n > 8)
4718 : 119094 : return *slot;
4719 : 10265172 : for (unsigned i = 0; i < val->n; ++i)
4720 : : {
4721 : 6448518 : basic_block val_bb
4722 : 6448518 : = BASIC_BLOCK_FOR_FN (cfun,
4723 : : val->valid_dominated_by_p[i]);
4724 : 6448518 : if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4725 : : /* Value registered with more generic predicate. */
4726 : 233824 : return *slot;
4727 : 6214694 : else if (flag_checking)
4728 : : /* Shouldn't happen, we insert in RPO order. */
4729 : 6214694 : gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4730 : : val_bb, vno_bb));
4731 : : }
4732 : : /* Append the location. */
4733 : 3816654 : ins_at = val;
4734 : 3816654 : ins = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4735 : : sizeof (vn_pval)
4736 : : + val->n * sizeof (int));
4737 : 3816654 : ins->next = NULL;
4738 : 3816654 : ins->result = val->result;
4739 : 3816654 : ins->n = val->n + 1;
4740 : 3816654 : memcpy (ins->valid_dominated_by_p,
4741 : 3816654 : val->valid_dominated_by_p,
4742 : 3816654 : val->n * sizeof (int));
4743 : 3816654 : ins->valid_dominated_by_p[val->n] = vno_bb->index;
4744 : 3816654 : if (dump_file && (dump_flags & TDF_DETAILS))
4745 : 4 : fprintf (dump_file, "Appending predicate to value.\n");
4746 : : break;
4747 : : }
4748 : : }
4749 : : /* Copy the rest of the value chain. */
4750 : 59564026 : for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4751 : : {
4752 : 30642485 : if (val == ins_at)
4753 : : /* Replace the node we appended to. */
4754 : 3816654 : *next = ins;
4755 : : else
4756 : : {
4757 : : /* Copy other predicated values. */
4758 : 26825831 : *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4759 : : sizeof (vn_pval)
4760 : : + ((val->n-1)
4761 : : * sizeof (int)));
4762 : 26825831 : memcpy (*next, val,
4763 : 26825831 : sizeof (vn_pval) + (val->n-1) * sizeof (int));
4764 : 26825831 : (*next)->next = NULL;
4765 : : }
4766 : 30642485 : next = &(*next)->next;
4767 : : }
4768 : : /* Append the value if we didn't find it. */
4769 : 28921541 : if (!ins_at)
4770 : 25104887 : *next = nval;
4771 : 28921541 : *slot = vno;
4772 : 28921541 : vno->next = last_inserted_nary;
4773 : 28921541 : last_inserted_nary = vno;
4774 : 28921541 : return vno;
4775 : : }
4776 : :
4777 : : /* While we do not want to insert things twice it's awkward to
4778 : : avoid it in the case where visit_nary_op pattern-matches stuff
4779 : : and ends up simplifying the replacement to itself. We then
4780 : : get two inserts, one from visit_nary_op and one from
4781 : : vn_nary_build_or_lookup.
4782 : : So allow inserts with the same value number. */
4783 : 4060 : if ((*slot)->u.result == vno->u.result)
4784 : : return *slot;
4785 : : }
4786 : :
4787 : : /* ??? There's also optimistic vs. previous commited state merging
4788 : : that is problematic for the case of unwinding. */
4789 : :
4790 : : /* ??? We should return NULL if we do not use 'vno' and have the
4791 : : caller release it. */
4792 : 128362674 : gcc_assert (!*slot);
4793 : :
4794 : 128362674 : *slot = vno;
4795 : 128362674 : vno->next = last_inserted_nary;
4796 : 128362674 : last_inserted_nary = vno;
4797 : 128362674 : return vno;
4798 : : }
4799 : :
4800 : : /* Insert a n-ary operation into the current hash table using it's
4801 : : pieces. Return the vn_nary_op_t structure we created and put in
4802 : : the hashtable. */
4803 : :
4804 : : vn_nary_op_t
4805 : 575724 : vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4806 : : tree type, tree *ops,
4807 : : tree result, unsigned int value_id)
4808 : : {
4809 : 575724 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4810 : 575724 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4811 : 575724 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4812 : : }
4813 : :
4814 : : /* Return whether we can track a predicate valid when PRED_E is executed. */
4815 : :
4816 : : static bool
4817 : 152354571 : can_track_predicate_on_edge (edge pred_e)
4818 : : {
4819 : : /* ??? As we are currently recording the destination basic-block index in
4820 : : vn_pval.valid_dominated_by_p and using dominance for the
4821 : : validity check we cannot track predicates on all edges. */
4822 : 152354571 : if (single_pred_p (pred_e->dest))
4823 : : return true;
4824 : : /* Never record for backedges. */
4825 : 11989940 : if (pred_e->flags & EDGE_DFS_BACK)
4826 : : return false;
4827 : : /* When there's more than one predecessor we cannot track
4828 : : predicate validity based on the destination block. The
4829 : : exception is when all other incoming edges sources are
4830 : : dominated by the destination block. */
4831 : 11306007 : edge_iterator ei;
4832 : 11306007 : edge e;
4833 : 19395924 : FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4834 : 17556107 : if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4835 : : return false;
4836 : : return true;
4837 : : }
4838 : :
4839 : : static vn_nary_op_t
4840 : 108020511 : vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4841 : : tree type, tree *ops,
4842 : : tree result, unsigned int value_id,
4843 : : edge pred_e)
4844 : : {
4845 : 108020511 : if (flag_checking)
4846 : 108019675 : gcc_assert (can_track_predicate_on_edge (pred_e));
4847 : :
4848 : 71493 : if (dump_file && (dump_flags & TDF_DETAILS)
4849 : : /* ??? Fix dumping, but currently we only get comparisons. */
4850 : 108087974 : && TREE_CODE_CLASS (code) == tcc_comparison)
4851 : : {
4852 : 67463 : fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4853 : 67463 : pred_e->dest->index);
4854 : 67463 : print_generic_expr (dump_file, ops[0], TDF_SLIM);
4855 : 67463 : fprintf (dump_file, " %s ", get_tree_code_name (code));
4856 : 67463 : print_generic_expr (dump_file, ops[1], TDF_SLIM);
4857 : 100872 : fprintf (dump_file, " == %s\n",
4858 : 67463 : integer_zerop (result) ? "false" : "true");
4859 : : }
4860 : 108020511 : vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4861 : 108020511 : init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4862 : 108020511 : vno1->predicated_values = 1;
4863 : 108020511 : vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4864 : : sizeof (vn_pval));
4865 : 108020511 : vno1->u.values->next = NULL;
4866 : 108020511 : vno1->u.values->result = result;
4867 : 108020511 : vno1->u.values->n = 1;
4868 : 108020511 : vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4869 : 108020511 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4870 : : }
4871 : :
4872 : : static bool
4873 : : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4874 : :
4875 : : static tree
4876 : 1625944 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
4877 : : edge e = NULL)
4878 : : {
4879 : 1625944 : if (! vno->predicated_values)
4880 : 0 : return vno->u.result;
4881 : 3354514 : for (vn_pval *val = vno->u.values; val; val = val->next)
4882 : 5071352 : for (unsigned i = 0; i < val->n; ++i)
4883 : : {
4884 : 3342782 : basic_block cand
4885 : 3342782 : = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
4886 : : /* Do not handle backedge executability optimistically since
4887 : : when figuring out whether to iterate we do not consider
4888 : : changed predication.
4889 : : When asking for predicated values on an edge avoid looking
4890 : : at edge executability for edges forward in our iteration
4891 : : as well. */
4892 : 3342782 : if (e && (e->flags & EDGE_DFS_BACK))
4893 : : {
4894 : 24146 : if (dominated_by_p (CDI_DOMINATORS, bb, cand))
4895 : 8446 : return val->result;
4896 : : }
4897 : 3318636 : else if (dominated_by_p_w_unex (bb, cand, false))
4898 : 497119 : return val->result;
4899 : : }
4900 : : return NULL_TREE;
4901 : : }
4902 : :
4903 : : static tree
4904 : 206874 : vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
4905 : : {
4906 : 0 : return vn_nary_op_get_predicated_value (vno, e->src, e);
4907 : : }
4908 : :
4909 : : /* Insert the rhs of STMT into the current hash table with a value number of
4910 : : RESULT. */
4911 : :
4912 : : static vn_nary_op_t
4913 : 44954485 : vn_nary_op_insert_stmt (gimple *stmt, tree result)
4914 : : {
4915 : 44954485 : vn_nary_op_t vno1
4916 : 44954485 : = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
4917 : 44954485 : result, VN_INFO (result)->value_id);
4918 : 44954485 : init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4919 : 44954485 : return vn_nary_op_insert_into (vno1, valid_info->nary);
4920 : : }
4921 : :
4922 : : /* Compute a hashcode for PHI operation VP1 and return it. */
4923 : :
4924 : : static inline hashval_t
4925 : 50795319 : vn_phi_compute_hash (vn_phi_t vp1)
4926 : : {
4927 : 50795319 : inchash::hash hstate;
4928 : 50795319 : tree phi1op;
4929 : 50795319 : tree type;
4930 : 50795319 : edge e;
4931 : 50795319 : edge_iterator ei;
4932 : :
4933 : 101590638 : hstate.add_int (EDGE_COUNT (vp1->block->preds));
4934 : 50795319 : switch (EDGE_COUNT (vp1->block->preds))
4935 : : {
4936 : : case 1:
4937 : : break;
4938 : 43356998 : case 2:
4939 : : /* When this is a PHI node subject to CSE for different blocks
4940 : : avoid hashing the block index. */
4941 : 43356998 : if (vp1->cclhs)
4942 : : break;
4943 : : /* Fallthru. */
4944 : 34459599 : default:
4945 : 34459599 : hstate.add_int (vp1->block->index);
4946 : : }
4947 : :
4948 : : /* If all PHI arguments are constants we need to distinguish
4949 : : the PHI node via its type. */
4950 : 50795319 : type = vp1->type;
4951 : 50795319 : hstate.merge_hash (vn_hash_type (type));
4952 : :
4953 : 177152672 : FOR_EACH_EDGE (e, ei, vp1->block->preds)
4954 : : {
4955 : : /* Don't hash backedge values they need to be handled as VN_TOP
4956 : : for optimistic value-numbering. */
4957 : 126357353 : if (e->flags & EDGE_DFS_BACK)
4958 : 28472031 : continue;
4959 : :
4960 : 97885322 : phi1op = vp1->phiargs[e->dest_idx];
4961 : 97885322 : if (phi1op == VN_TOP)
4962 : 247317 : continue;
4963 : 97638005 : inchash::add_expr (phi1op, hstate);
4964 : : }
4965 : :
4966 : 50795319 : return hstate.end ();
4967 : : }
4968 : :
4969 : :
4970 : : /* Return true if COND1 and COND2 represent the same condition, set
4971 : : *INVERTED_P if one needs to be inverted to make it the same as
4972 : : the other. */
4973 : :
4974 : : static bool
4975 : 3844296 : cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
4976 : : gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
4977 : : {
4978 : 3844296 : enum tree_code code1 = gimple_cond_code (cond1);
4979 : 3844296 : enum tree_code code2 = gimple_cond_code (cond2);
4980 : :
4981 : 3844296 : *inverted_p = false;
4982 : 3844296 : if (code1 == code2)
4983 : : ;
4984 : 302221 : else if (code1 == swap_tree_comparison (code2))
4985 : : std::swap (lhs2, rhs2);
4986 : 266260 : else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
4987 : 134849 : *inverted_p = true;
4988 : 131411 : else if (code1 == invert_tree_comparison
4989 : 131411 : (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
4990 : : {
4991 : 10392 : std::swap (lhs2, rhs2);
4992 : 10392 : *inverted_p = true;
4993 : : }
4994 : : else
4995 : : return false;
4996 : :
4997 : 3723277 : return ((expressions_equal_p (lhs1, lhs2)
4998 : 104563 : && expressions_equal_p (rhs1, rhs2))
4999 : 3748528 : || (commutative_tree_code (code1)
5000 : 1852152 : && expressions_equal_p (lhs1, rhs2)
5001 : 2453 : && expressions_equal_p (rhs1, lhs2)));
5002 : : }
5003 : :
5004 : : /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
5005 : :
5006 : : static int
5007 : 41644047 : vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
5008 : : {
5009 : 41644047 : if (vp1->hashcode != vp2->hashcode)
5010 : : return false;
5011 : :
5012 : 12848165 : if (vp1->block != vp2->block)
5013 : : {
5014 : 11556267 : if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
5015 : : return false;
5016 : :
5017 : 37381837 : switch (EDGE_COUNT (vp1->block->preds))
5018 : : {
5019 : : case 1:
5020 : : /* Single-arg PHIs are just copies. */
5021 : : break;
5022 : :
5023 : 3852089 : case 2:
5024 : 3852089 : {
5025 : : /* Make sure both PHIs are classified as CSEable. */
5026 : 3852089 : if (! vp1->cclhs || ! vp2->cclhs)
5027 : : return false;
5028 : :
5029 : : /* Rule out backedges into the PHI. */
5030 : 3852089 : gcc_checking_assert
5031 : : (vp1->block->loop_father->header != vp1->block
5032 : : && vp2->block->loop_father->header != vp2->block);
5033 : :
5034 : : /* If the PHI nodes do not have compatible types
5035 : : they are not the same. */
5036 : 3852089 : if (!types_compatible_p (vp1->type, vp2->type))
5037 : : return false;
5038 : :
5039 : : /* If the immediate dominator end in switch stmts multiple
5040 : : values may end up in the same PHI arg via intermediate
5041 : : CFG merges. */
5042 : 3844296 : basic_block idom1
5043 : 3844296 : = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5044 : 3844296 : basic_block idom2
5045 : 3844296 : = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
5046 : 3844296 : gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
5047 : : && EDGE_COUNT (idom2->succs) == 2);
5048 : :
5049 : : /* Verify the controlling stmt is the same. */
5050 : 7688592 : gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
5051 : 7688592 : gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
5052 : 3844296 : bool inverted_p;
5053 : 3844296 : if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
5054 : 3844296 : last2, vp2->cclhs, vp2->ccrhs,
5055 : : &inverted_p))
5056 : : return false;
5057 : :
5058 : : /* Get at true/false controlled edges into the PHI. */
5059 : 79400 : edge te1, te2, fe1, fe2;
5060 : 79400 : if (! extract_true_false_controlled_edges (idom1, vp1->block,
5061 : : &te1, &fe1)
5062 : 79400 : || ! extract_true_false_controlled_edges (idom2, vp2->block,
5063 : : &te2, &fe2))
5064 : 33643 : return false;
5065 : :
5066 : : /* Swap edges if the second condition is the inverted of the
5067 : : first. */
5068 : 45757 : if (inverted_p)
5069 : 2076 : std::swap (te2, fe2);
5070 : :
5071 : : /* Since we do not know which edge will be executed we have
5072 : : to be careful when matching VN_TOP. Be conservative and
5073 : : only match VN_TOP == VN_TOP for now, we could allow
5074 : : VN_TOP on the not prevailing PHI though. See for example
5075 : : PR102920. */
5076 : 45757 : if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
5077 : 45757 : vp2->phiargs[te2->dest_idx], false)
5078 : 89701 : || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
5079 : 43944 : vp2->phiargs[fe2->dest_idx], false))
5080 : 1813 : return false;
5081 : :
5082 : : return true;
5083 : : }
5084 : :
5085 : : default:
5086 : : return false;
5087 : : }
5088 : : }
5089 : :
5090 : : /* If the PHI nodes do not have compatible types
5091 : : they are not the same. */
5092 : 8996076 : if (!types_compatible_p (vp1->type, vp2->type))
5093 : : return false;
5094 : :
5095 : : /* Any phi in the same block will have it's arguments in the
5096 : : same edge order, because of how we store phi nodes. */
5097 : 8994912 : unsigned nargs = EDGE_COUNT (vp1->block->preds);
5098 : 21087465 : for (unsigned i = 0; i < nargs; ++i)
5099 : : {
5100 : 16825255 : tree phi1op = vp1->phiargs[i];
5101 : 16825255 : tree phi2op = vp2->phiargs[i];
5102 : 16825255 : if (phi1op == phi2op)
5103 : 11998249 : continue;
5104 : 4827006 : if (!expressions_equal_p (phi1op, phi2op, false))
5105 : : return false;
5106 : : }
5107 : :
5108 : : return true;
5109 : : }
5110 : :
5111 : : /* Lookup PHI in the current hash table, and return the resulting
5112 : : value number if it exists in the hash table. Return NULL_TREE if
5113 : : it does not exist in the hash table. */
5114 : :
5115 : : static tree
5116 : 27877437 : vn_phi_lookup (gimple *phi, bool backedges_varying_p)
5117 : : {
5118 : 27877437 : vn_phi_s **slot;
5119 : 27877437 : struct vn_phi_s *vp1;
5120 : 27877437 : edge e;
5121 : 27877437 : edge_iterator ei;
5122 : :
5123 : 27877437 : vp1 = XALLOCAVAR (struct vn_phi_s,
5124 : : sizeof (struct vn_phi_s)
5125 : : + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
5126 : :
5127 : : /* Canonicalize the SSA_NAME's to their value number. */
5128 : 96535508 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5129 : : {
5130 : 68658071 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5131 : 68658071 : if (TREE_CODE (def) == SSA_NAME
5132 : 57169753 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5133 : : {
5134 : 54568380 : if (!virtual_operand_p (def)
5135 : 54568380 : && ssa_undefined_value_p (def, false))
5136 : 138043 : def = VN_TOP;
5137 : : else
5138 : 54430337 : def = SSA_VAL (def);
5139 : : }
5140 : 68658071 : vp1->phiargs[e->dest_idx] = def;
5141 : : }
5142 : 27877437 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5143 : 27877437 : vp1->block = gimple_bb (phi);
5144 : : /* Extract values of the controlling condition. */
5145 : 27877437 : vp1->cclhs = NULL_TREE;
5146 : 27877437 : vp1->ccrhs = NULL_TREE;
5147 : 27877437 : if (EDGE_COUNT (vp1->block->preds) == 2
5148 : 27877437 : && vp1->block->loop_father->header != vp1->block)
5149 : : {
5150 : 8669513 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5151 : 8669513 : if (EDGE_COUNT (idom1->succs) == 2)
5152 : 17218234 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5153 : : {
5154 : : /* ??? We want to use SSA_VAL here. But possibly not
5155 : : allow VN_TOP. */
5156 : 8354935 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5157 : 8354935 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5158 : : }
5159 : : }
5160 : 27877437 : vp1->hashcode = vn_phi_compute_hash (vp1);
5161 : 27877437 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
5162 : 27877437 : if (!slot)
5163 : : return NULL_TREE;
5164 : 4306154 : return (*slot)->result;
5165 : : }
5166 : :
5167 : : /* Insert PHI into the current hash table with a value number of
5168 : : RESULT. */
5169 : :
5170 : : static vn_phi_t
5171 : 22917882 : vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
5172 : : {
5173 : 22917882 : vn_phi_s **slot;
5174 : 22917882 : vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
5175 : : sizeof (vn_phi_s)
5176 : : + ((gimple_phi_num_args (phi) - 1)
5177 : : * sizeof (tree)));
5178 : 22917882 : edge e;
5179 : 22917882 : edge_iterator ei;
5180 : :
5181 : : /* Canonicalize the SSA_NAME's to their value number. */
5182 : 80617164 : FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5183 : : {
5184 : 57699282 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5185 : 57699282 : if (TREE_CODE (def) == SSA_NAME
5186 : 47334046 : && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5187 : : {
5188 : 44733112 : if (!virtual_operand_p (def)
5189 : 44733112 : && ssa_undefined_value_p (def, false))
5190 : 109518 : def = VN_TOP;
5191 : : else
5192 : 44623594 : def = SSA_VAL (def);
5193 : : }
5194 : 57699282 : vp1->phiargs[e->dest_idx] = def;
5195 : : }
5196 : 22917882 : vp1->value_id = VN_INFO (result)->value_id;
5197 : 22917882 : vp1->type = TREE_TYPE (gimple_phi_result (phi));
5198 : 22917882 : vp1->block = gimple_bb (phi);
5199 : : /* Extract values of the controlling condition. */
5200 : 22917882 : vp1->cclhs = NULL_TREE;
5201 : 22917882 : vp1->ccrhs = NULL_TREE;
5202 : 22917882 : if (EDGE_COUNT (vp1->block->preds) == 2
5203 : 22917882 : && vp1->block->loop_father->header != vp1->block)
5204 : : {
5205 : 8277164 : basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5206 : 8277164 : if (EDGE_COUNT (idom1->succs) == 2)
5207 : 16437242 : if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5208 : : {
5209 : : /* ??? We want to use SSA_VAL here. But possibly not
5210 : : allow VN_TOP. */
5211 : 7980785 : vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5212 : 7980785 : vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5213 : : }
5214 : : }
5215 : 22917882 : vp1->result = result;
5216 : 22917882 : vp1->hashcode = vn_phi_compute_hash (vp1);
5217 : :
5218 : 22917882 : slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5219 : 22917882 : gcc_assert (!*slot);
5220 : :
5221 : 22917882 : *slot = vp1;
5222 : 22917882 : vp1->next = last_inserted_phi;
5223 : 22917882 : last_inserted_phi = vp1;
5224 : 22917882 : return vp1;
5225 : : }
5226 : :
5227 : :
5228 : : /* Return true if BB1 is dominated by BB2 taking into account edges
5229 : : that are not executable. When ALLOW_BACK is false consider not
5230 : : executable backedges as executable. */
5231 : :
5232 : : static bool
5233 : 69297200 : dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
5234 : : {
5235 : 69297200 : edge_iterator ei;
5236 : 69297200 : edge e;
5237 : :
5238 : 69297200 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5239 : : return true;
5240 : :
5241 : : /* Before iterating we'd like to know if there exists a
5242 : : (executable) path from bb2 to bb1 at all, if not we can
5243 : : directly return false. For now simply iterate once. */
5244 : :
5245 : : /* Iterate to the single executable bb1 predecessor. */
5246 : 21166707 : if (EDGE_COUNT (bb1->preds) > 1)
5247 : : {
5248 : 2958586 : edge prede = NULL;
5249 : 6394027 : FOR_EACH_EDGE (e, ei, bb1->preds)
5250 : 5991826 : if ((e->flags & EDGE_EXECUTABLE)
5251 : 555403 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5252 : : {
5253 : 5514971 : if (prede)
5254 : : {
5255 : : prede = NULL;
5256 : : break;
5257 : : }
5258 : : prede = e;
5259 : : }
5260 : 2958586 : if (prede)
5261 : : {
5262 : 402201 : bb1 = prede->src;
5263 : :
5264 : : /* Re-do the dominance check with changed bb1. */
5265 : 402201 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5266 : : return true;
5267 : : }
5268 : : }
5269 : :
5270 : : /* Iterate to the single executable bb2 successor. */
5271 : 20934279 : if (EDGE_COUNT (bb2->succs) > 1)
5272 : : {
5273 : 6097692 : edge succe = NULL;
5274 : 12340615 : FOR_EACH_EDGE (e, ei, bb2->succs)
5275 : 12195648 : if ((e->flags & EDGE_EXECUTABLE)
5276 : 178907 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5277 : : {
5278 : 12016822 : if (succe)
5279 : : {
5280 : : succe = NULL;
5281 : : break;
5282 : : }
5283 : : succe = e;
5284 : : }
5285 : 6097692 : if (succe
5286 : : /* Limit the number of edges we check, we should bring in
5287 : : context from the iteration and compute the single
5288 : : executable incoming edge when visiting a block. */
5289 : 6097692 : && EDGE_COUNT (succe->dest->preds) < 8)
5290 : : {
5291 : : /* Verify the reached block is only reached through succe.
5292 : : If there is only one edge we can spare us the dominator
5293 : : check and iterate directly. */
5294 : 110839 : if (EDGE_COUNT (succe->dest->preds) > 1)
5295 : : {
5296 : 55165 : FOR_EACH_EDGE (e, ei, succe->dest->preds)
5297 : 42158 : if (e != succe
5298 : 27092 : && ((e->flags & EDGE_EXECUTABLE)
5299 : 18265 : || (!allow_back && (e->flags & EDGE_DFS_BACK))))
5300 : : {
5301 : : succe = NULL;
5302 : : break;
5303 : : }
5304 : : }
5305 : 110839 : if (succe)
5306 : : {
5307 : 102003 : bb2 = succe->dest;
5308 : :
5309 : : /* Re-do the dominance check with changed bb2. */
5310 : 102003 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5311 : : return true;
5312 : : }
5313 : : }
5314 : : }
5315 : : /* Iterate to the single successor of bb2 with only a single executable
5316 : : incoming edge. */
5317 : 14836587 : else if (EDGE_COUNT (bb2->succs) == 1
5318 : 14347378 : && EDGE_COUNT (single_succ (bb2)->preds) > 1
5319 : : /* Limit the number of edges we check, we should bring in
5320 : : context from the iteration and compute the single
5321 : : executable incoming edge when visiting a block. */
5322 : 28927678 : && EDGE_COUNT (single_succ (bb2)->preds) < 8)
5323 : : {
5324 : 4977132 : edge prede = NULL;
5325 : 11270904 : FOR_EACH_EDGE (e, ei, single_succ (bb2)->preds)
5326 : 10724664 : if ((e->flags & EDGE_EXECUTABLE)
5327 : 1367474 : || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5328 : : {
5329 : 9361212 : if (prede)
5330 : : {
5331 : : prede = NULL;
5332 : : break;
5333 : : }
5334 : : prede = e;
5335 : : }
5336 : : /* We might actually get to a query with BB2 not visited yet when
5337 : : we're querying for a predicated value. */
5338 : 4977132 : if (prede && prede->src == bb2)
5339 : : {
5340 : 486704 : bb2 = prede->dest;
5341 : :
5342 : : /* Re-do the dominance check with changed bb2. */
5343 : 486704 : if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5344 : : return true;
5345 : : }
5346 : : }
5347 : :
5348 : : /* We could now iterate updating bb1 / bb2. */
5349 : : return false;
5350 : : }
5351 : :
5352 : : /* Set the value number of FROM to TO, return true if it has changed
5353 : : as a result. */
5354 : :
5355 : : static inline bool
5356 : 205118344 : set_ssa_val_to (tree from, tree to)
5357 : : {
5358 : 205118344 : vn_ssa_aux_t from_info = VN_INFO (from);
5359 : 205118344 : tree currval = from_info->valnum; // SSA_VAL (from)
5360 : 205118344 : poly_int64 toff, coff;
5361 : 205118344 : bool curr_undefined = false;
5362 : 205118344 : bool curr_invariant = false;
5363 : :
5364 : : /* The only thing we allow as value numbers are ssa_names
5365 : : and invariants. So assert that here. We don't allow VN_TOP
5366 : : as visiting a stmt should produce a value-number other than
5367 : : that.
5368 : : ??? Still VN_TOP can happen for unreachable code, so force
5369 : : it to varying in that case. Not all code is prepared to
5370 : : get VN_TOP on valueization. */
5371 : 205118344 : if (to == VN_TOP)
5372 : : {
5373 : : /* ??? When iterating and visiting PHI <undef, backedge-value>
5374 : : for the first time we rightfully get VN_TOP and we need to
5375 : : preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5376 : : With SCCVN we were simply lucky we iterated the other PHI
5377 : : cycles first and thus visited the backedge-value DEF. */
5378 : 0 : if (currval == VN_TOP)
5379 : 0 : goto set_and_exit;
5380 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5381 : 0 : fprintf (dump_file, "Forcing value number to varying on "
5382 : : "receiving VN_TOP\n");
5383 : : to = from;
5384 : : }
5385 : :
5386 : 205118344 : gcc_checking_assert (to != NULL_TREE
5387 : : && ((TREE_CODE (to) == SSA_NAME
5388 : : && (to == from || SSA_VAL (to) == to))
5389 : : || is_gimple_min_invariant (to)));
5390 : :
5391 : 205118344 : if (from != to)
5392 : : {
5393 : 32363248 : if (currval == from)
5394 : : {
5395 : 14897 : if (dump_file && (dump_flags & TDF_DETAILS))
5396 : : {
5397 : 0 : fprintf (dump_file, "Not changing value number of ");
5398 : 0 : print_generic_expr (dump_file, from);
5399 : 0 : fprintf (dump_file, " from VARYING to ");
5400 : 0 : print_generic_expr (dump_file, to);
5401 : 0 : fprintf (dump_file, "\n");
5402 : : }
5403 : 14897 : return false;
5404 : : }
5405 : 32348351 : curr_invariant = is_gimple_min_invariant (currval);
5406 : 64696702 : curr_undefined = (TREE_CODE (currval) == SSA_NAME
5407 : 3887051 : && !virtual_operand_p (currval)
5408 : 35988468 : && ssa_undefined_value_p (currval, false));
5409 : 32348351 : if (currval != VN_TOP
5410 : : && !curr_invariant
5411 : 5363246 : && !curr_undefined
5412 : 36221200 : && is_gimple_min_invariant (to))
5413 : : {
5414 : 218 : if (dump_file && (dump_flags & TDF_DETAILS))
5415 : : {
5416 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5417 : : "value number of ");
5418 : 0 : print_generic_expr (dump_file, from);
5419 : 0 : fprintf (dump_file, " from ");
5420 : 0 : print_generic_expr (dump_file, currval);
5421 : 0 : fprintf (dump_file, " (non-constant) to ");
5422 : 0 : print_generic_expr (dump_file, to);
5423 : 0 : fprintf (dump_file, " (constant)\n");
5424 : : }
5425 : : to = from;
5426 : : }
5427 : 32348133 : else if (currval != VN_TOP
5428 : 5363028 : && !curr_undefined
5429 : 5348826 : && TREE_CODE (to) == SSA_NAME
5430 : 4521697 : && !virtual_operand_p (to)
5431 : 36622896 : && ssa_undefined_value_p (to, false))
5432 : : {
5433 : 6 : if (dump_file && (dump_flags & TDF_DETAILS))
5434 : : {
5435 : 0 : fprintf (dump_file, "Forcing VARYING instead of changing "
5436 : : "value number of ");
5437 : 0 : print_generic_expr (dump_file, from);
5438 : 0 : fprintf (dump_file, " from ");
5439 : 0 : print_generic_expr (dump_file, currval);
5440 : 0 : fprintf (dump_file, " (non-undefined) to ");
5441 : 0 : print_generic_expr (dump_file, to);
5442 : 0 : fprintf (dump_file, " (undefined)\n");
5443 : : }
5444 : : to = from;
5445 : : }
5446 : 32348127 : else if (TREE_CODE (to) == SSA_NAME
5447 : 32348127 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5448 : : to = from;
5449 : : }
5450 : :
5451 : 172755096 : set_and_exit:
5452 : 205103447 : if (dump_file && (dump_flags & TDF_DETAILS))
5453 : : {
5454 : 378778 : fprintf (dump_file, "Setting value number of ");
5455 : 378778 : print_generic_expr (dump_file, from);
5456 : 378778 : fprintf (dump_file, " to ");
5457 : 378778 : print_generic_expr (dump_file, to);
5458 : : }
5459 : :
5460 : 205103447 : if (currval != to
5461 : 167057687 : && !operand_equal_p (currval, to, 0)
5462 : : /* Different undefined SSA names are not actually different. See
5463 : : PR82320 for a testcase were we'd otherwise not terminate iteration. */
5464 : 166988794 : && !(curr_undefined
5465 : 3327 : && TREE_CODE (to) == SSA_NAME
5466 : 580 : && !virtual_operand_p (to)
5467 : 580 : && ssa_undefined_value_p (to, false))
5468 : : /* ??? For addresses involving volatile objects or types operand_equal_p
5469 : : does not reliably detect ADDR_EXPRs as equal. We know we are only
5470 : : getting invariant gimple addresses here, so can use
5471 : : get_addr_base_and_unit_offset to do this comparison. */
5472 : 372091628 : && !(TREE_CODE (currval) == ADDR_EXPR
5473 : 472447 : && TREE_CODE (to) == ADDR_EXPR
5474 : 12 : && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5475 : 6 : == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5476 : 6 : && known_eq (coff, toff)))
5477 : : {
5478 : 166988175 : if (to != from
5479 : 27981227 : && currval != VN_TOP
5480 : 999504 : && !curr_undefined
5481 : : /* We do not want to allow lattice transitions from one value
5482 : : to another since that may lead to not terminating iteration
5483 : : (see PR95049). Since there's no convenient way to check
5484 : : for the allowed transition of VAL -> PHI (loop entry value,
5485 : : same on two PHIs, to same PHI result) we restrict the check
5486 : : to invariants. */
5487 : 999504 : && curr_invariant
5488 : 167637235 : && is_gimple_min_invariant (to))
5489 : : {
5490 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
5491 : 0 : fprintf (dump_file, " forced VARYING");
5492 : : to = from;
5493 : : }
5494 : 166988175 : if (dump_file && (dump_flags & TDF_DETAILS))
5495 : 378462 : fprintf (dump_file, " (changed)\n");
5496 : 166988175 : from_info->valnum = to;
5497 : 166988175 : return true;
5498 : : }
5499 : 38115272 : if (dump_file && (dump_flags & TDF_DETAILS))
5500 : 316 : fprintf (dump_file, "\n");
5501 : : return false;
5502 : : }
5503 : :
5504 : : /* Set all definitions in STMT to value number to themselves.
5505 : : Return true if a value number changed. */
5506 : :
5507 : : static bool
5508 : 283546183 : defs_to_varying (gimple *stmt)
5509 : : {
5510 : 283546183 : bool changed = false;
5511 : 283546183 : ssa_op_iter iter;
5512 : 283546183 : def_operand_p defp;
5513 : :
5514 : 312989996 : FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5515 : : {
5516 : 29443813 : tree def = DEF_FROM_PTR (defp);
5517 : 29443813 : changed |= set_ssa_val_to (def, def);
5518 : : }
5519 : 283546183 : return changed;
5520 : : }
5521 : :
5522 : : /* Visit a copy between LHS and RHS, return true if the value number
5523 : : changed. */
5524 : :
5525 : : static bool
5526 : 7927442 : visit_copy (tree lhs, tree rhs)
5527 : : {
5528 : : /* Valueize. */
5529 : 7927442 : rhs = SSA_VAL (rhs);
5530 : :
5531 : 7927442 : return set_ssa_val_to (lhs, rhs);
5532 : : }
5533 : :
5534 : : /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5535 : : is the same. */
5536 : :
5537 : : static tree
5538 : 2432058 : valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5539 : : {
5540 : 2432058 : if (TREE_CODE (op) == SSA_NAME)
5541 : 2121242 : op = vn_valueize (op);
5542 : :
5543 : : /* Either we have the op widened available. */
5544 : 2432058 : tree ops[3] = {};
5545 : 2432058 : ops[0] = op;
5546 : 2432058 : tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5547 : : wide_type, ops, NULL);
5548 : 2432058 : if (tem)
5549 : : return tem;
5550 : :
5551 : : /* Or the op is truncated from some existing value. */
5552 : 2144657 : if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5553 : : {
5554 : 541782 : gimple *def = SSA_NAME_DEF_STMT (op);
5555 : 541782 : if (is_gimple_assign (def)
5556 : 541782 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5557 : : {
5558 : 283148 : tem = gimple_assign_rhs1 (def);
5559 : 283148 : if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5560 : : {
5561 : 176087 : if (TREE_CODE (tem) == SSA_NAME)
5562 : 176087 : tem = vn_valueize (tem);
5563 : 176087 : return tem;
5564 : : }
5565 : : }
5566 : : }
5567 : :
5568 : : /* For constants simply extend it. */
5569 : 1968570 : if (TREE_CODE (op) == INTEGER_CST)
5570 : 343325 : return wide_int_to_tree (wide_type, wi::to_widest (op));
5571 : :
5572 : : return NULL_TREE;
5573 : : }
5574 : :
5575 : : /* Visit a nary operator RHS, value number it, and return true if the
5576 : : value number of LHS has changed as a result. */
5577 : :
5578 : : static bool
5579 : 48624130 : visit_nary_op (tree lhs, gassign *stmt)
5580 : : {
5581 : 48624130 : vn_nary_op_t vnresult;
5582 : 48624130 : tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5583 : 48624130 : if (! result && vnresult)
5584 : 153039 : result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5585 : 45026931 : if (result)
5586 : 3668190 : return set_ssa_val_to (lhs, result);
5587 : :
5588 : : /* Do some special pattern matching for redundancies of operations
5589 : : in different types. */
5590 : 44955940 : enum tree_code code = gimple_assign_rhs_code (stmt);
5591 : 44955940 : tree type = TREE_TYPE (lhs);
5592 : 44955940 : tree rhs1 = gimple_assign_rhs1 (stmt);
5593 : 44955940 : switch (code)
5594 : : {
5595 : 10086813 : CASE_CONVERT:
5596 : : /* Match arithmetic done in a different type where we can easily
5597 : : substitute the result from some earlier sign-changed or widened
5598 : : operation. */
5599 : 10086813 : if (INTEGRAL_TYPE_P (type)
5600 : 9049754 : && TREE_CODE (rhs1) == SSA_NAME
5601 : : /* We only handle sign-changes, zero-extension -> & mask or
5602 : : sign-extension if we know the inner operation doesn't
5603 : : overflow. */
5604 : 18900814 : && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5605 : 5286861 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5606 : 5285682 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5607 : 8085521 : && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5608 : 5937507 : || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5609 : : {
5610 : 7689680 : gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5611 : 5536731 : if (def
5612 : 5536731 : && (gimple_assign_rhs_code (def) == PLUS_EXPR
5613 : 4300531 : || gimple_assign_rhs_code (def) == MINUS_EXPR
5614 : 4165640 : || gimple_assign_rhs_code (def) == MULT_EXPR))
5615 : : {
5616 : 1960967 : tree ops[3] = {};
5617 : : /* When requiring a sign-extension we cannot model a
5618 : : previous truncation with a single op so don't bother. */
5619 : 1960967 : bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5620 : : /* Either we have the op widened available. */
5621 : 1960967 : ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5622 : : allow_truncate);
5623 : 1960967 : if (ops[0])
5624 : 942182 : ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5625 : : allow_truncate);
5626 : 1960967 : if (ops[0] && ops[1])
5627 : : {
5628 : 335722 : ops[0] = vn_nary_op_lookup_pieces
5629 : 335722 : (2, gimple_assign_rhs_code (def), type, ops, NULL);
5630 : : /* We have wider operation available. */
5631 : 335722 : if (ops[0]
5632 : : /* If the leader is a wrapping operation we can
5633 : : insert it for code hoisting w/o introducing
5634 : : undefined overflow. If it is not it has to
5635 : : be available. See PR86554. */
5636 : 335722 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
5637 : 1816 : || (rpo_avail && vn_context_bb
5638 : 1816 : && rpo_avail->eliminate_avail (vn_context_bb,
5639 : : ops[0]))))
5640 : : {
5641 : 9110 : unsigned lhs_prec = TYPE_PRECISION (type);
5642 : 9110 : unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5643 : 9110 : if (lhs_prec == rhs_prec
5644 : 9110 : || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5645 : 1678 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5646 : : {
5647 : 8520 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5648 : 8520 : NOP_EXPR, type, ops[0]);
5649 : 8520 : result = vn_nary_build_or_lookup (&match_op);
5650 : 8520 : if (result)
5651 : : {
5652 : 8520 : bool changed = set_ssa_val_to (lhs, result);
5653 : 8520 : if (TREE_CODE (result) == SSA_NAME)
5654 : 8520 : vn_nary_op_insert_stmt (stmt, result);
5655 : 8520 : return changed;
5656 : : }
5657 : : }
5658 : : else
5659 : : {
5660 : 590 : tree mask = wide_int_to_tree
5661 : 590 : (type, wi::mask (rhs_prec, false, lhs_prec));
5662 : 590 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5663 : 590 : BIT_AND_EXPR,
5664 : 590 : TREE_TYPE (lhs),
5665 : 590 : ops[0], mask);
5666 : 590 : result = vn_nary_build_or_lookup (&match_op);
5667 : 590 : if (result)
5668 : : {
5669 : 590 : bool changed = set_ssa_val_to (lhs, result);
5670 : 590 : if (TREE_CODE (result) == SSA_NAME)
5671 : 590 : vn_nary_op_insert_stmt (stmt, result);
5672 : 590 : return changed;
5673 : : }
5674 : : }
5675 : : }
5676 : : }
5677 : : }
5678 : : }
5679 : : break;
5680 : 1605215 : case BIT_AND_EXPR:
5681 : 1605215 : if (INTEGRAL_TYPE_P (type)
5682 : 1569365 : && TREE_CODE (rhs1) == SSA_NAME
5683 : 1569365 : && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5684 : 991451 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5685 : 991333 : && default_vn_walk_kind != VN_NOWALK
5686 : : && CHAR_BIT == 8
5687 : : && BITS_PER_UNIT == 8
5688 : : && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5689 : 991125 : && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5690 : 991123 : && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5691 : 2596338 : && !integer_zerop (gimple_assign_rhs2 (stmt)))
5692 : : {
5693 : 991123 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5694 : 737067 : if (ass
5695 : 737067 : && !gimple_has_volatile_ops (ass)
5696 : 735621 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5697 : : {
5698 : 369053 : tree last_vuse = gimple_vuse (ass);
5699 : 369053 : tree op = gimple_assign_rhs1 (ass);
5700 : 1107159 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5701 : : default_vn_walk_kind,
5702 : : NULL, true, &last_vuse,
5703 : : gimple_assign_rhs2 (stmt));
5704 : 369053 : if (result
5705 : 369743 : && useless_type_conversion_p (TREE_TYPE (result),
5706 : 690 : TREE_TYPE (op)))
5707 : 690 : return set_ssa_val_to (lhs, result);
5708 : : }
5709 : : }
5710 : : break;
5711 : 220843 : case BIT_FIELD_REF:
5712 : 220843 : if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
5713 : : {
5714 : 220823 : tree op0 = TREE_OPERAND (rhs1, 0);
5715 : 220823 : gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op0));
5716 : 177083 : if (ass
5717 : 177083 : && !gimple_has_volatile_ops (ass)
5718 : 177000 : && vn_get_stmt_kind (ass) == VN_REFERENCE)
5719 : : {
5720 : 83892 : tree last_vuse = gimple_vuse (ass);
5721 : 83892 : tree op = gimple_assign_rhs1 (ass);
5722 : : /* Avoid building invalid and unexpected refs. */
5723 : 83892 : if (TREE_CODE (op) != TARGET_MEM_REF
5724 : : && TREE_CODE (op) != BIT_FIELD_REF
5725 : : && TREE_CODE (op) != REALPART_EXPR
5726 : : && TREE_CODE (op) != IMAGPART_EXPR)
5727 : : {
5728 : 76780 : tree op = build3 (BIT_FIELD_REF, TREE_TYPE (rhs1),
5729 : : gimple_assign_rhs1 (ass),
5730 : 76780 : TREE_OPERAND (rhs1, 1),
5731 : 76780 : TREE_OPERAND (rhs1, 2));
5732 : 153560 : tree result = vn_reference_lookup (op, gimple_vuse (ass),
5733 : : default_vn_walk_kind,
5734 : : NULL, true, &last_vuse);
5735 : 76780 : if (result
5736 : 76780 : && useless_type_conversion_p (type, TREE_TYPE (result)))
5737 : 1116 : return set_ssa_val_to (lhs, result);
5738 : 76028 : else if (result
5739 : 364 : && TYPE_SIZE (type)
5740 : 364 : && TYPE_SIZE (TREE_TYPE (result))
5741 : 76392 : && operand_equal_p (TYPE_SIZE (type),
5742 : 364 : TYPE_SIZE (TREE_TYPE (result))))
5743 : : {
5744 : 364 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5745 : 364 : VIEW_CONVERT_EXPR,
5746 : 364 : type, result);
5747 : 364 : result = vn_nary_build_or_lookup (&match_op);
5748 : 364 : if (result)
5749 : : {
5750 : 364 : bool changed = set_ssa_val_to (lhs, result);
5751 : 364 : if (TREE_CODE (result) == SSA_NAME)
5752 : 352 : vn_nary_op_insert_stmt (stmt, result);
5753 : 364 : return changed;
5754 : : }
5755 : : }
5756 : : }
5757 : : }
5758 : : }
5759 : : break;
5760 : 338915 : case TRUNC_DIV_EXPR:
5761 : 338915 : if (TYPE_UNSIGNED (type))
5762 : : break;
5763 : : /* Fallthru. */
5764 : 5343007 : case RDIV_EXPR:
5765 : 5343007 : case MULT_EXPR:
5766 : : /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5767 : 5343007 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5768 : : {
5769 : 5342097 : tree rhs[2];
5770 : 5342097 : rhs[0] = rhs1;
5771 : 5342097 : rhs[1] = gimple_assign_rhs2 (stmt);
5772 : 16018753 : for (unsigned i = 0; i <= 1; ++i)
5773 : : {
5774 : 10683082 : unsigned j = i == 0 ? 1 : 0;
5775 : 10683082 : tree ops[2];
5776 : 10683082 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5777 : 10683082 : NEGATE_EXPR, type, rhs[i]);
5778 : 10683082 : ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5779 : 10683082 : ops[j] = rhs[j];
5780 : 10683082 : if (ops[i]
5781 : 10683082 : && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5782 : : type, ops, NULL)))
5783 : : {
5784 : 6426 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5785 : 6426 : NEGATE_EXPR, type, ops[0]);
5786 : 6426 : result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5787 : 6426 : if (result)
5788 : : {
5789 : 6426 : bool changed = set_ssa_val_to (lhs, result);
5790 : 6426 : if (TREE_CODE (result) == SSA_NAME)
5791 : 6426 : vn_nary_op_insert_stmt (stmt, result);
5792 : 6426 : return changed;
5793 : : }
5794 : : }
5795 : : }
5796 : : }
5797 : : break;
5798 : 359242 : case LSHIFT_EXPR:
5799 : : /* For X << C, use the value number of X * (1 << C). */
5800 : 359242 : if (INTEGRAL_TYPE_P (type)
5801 : 347273 : && TYPE_OVERFLOW_WRAPS (type)
5802 : 547037 : && !TYPE_SATURATING (type))
5803 : : {
5804 : 187795 : tree rhs2 = gimple_assign_rhs2 (stmt);
5805 : 187795 : if (TREE_CODE (rhs2) == INTEGER_CST
5806 : 109297 : && tree_fits_uhwi_p (rhs2)
5807 : 297092 : && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
5808 : : {
5809 : 109297 : wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
5810 : 109297 : TYPE_PRECISION (type));
5811 : 218594 : gimple_match_op match_op (gimple_match_cond::UNCOND,
5812 : 109297 : MULT_EXPR, type, rhs1,
5813 : 109297 : wide_int_to_tree (type, w));
5814 : 109297 : result = vn_nary_build_or_lookup (&match_op);
5815 : 109297 : if (result)
5816 : : {
5817 : 109297 : bool changed = set_ssa_val_to (lhs, result);
5818 : 109297 : if (TREE_CODE (result) == SSA_NAME)
5819 : 109296 : vn_nary_op_insert_stmt (stmt, result);
5820 : 109297 : return changed;
5821 : : }
5822 : 109297 : }
5823 : : }
5824 : : break;
5825 : : default:
5826 : : break;
5827 : : }
5828 : :
5829 : 44829301 : bool changed = set_ssa_val_to (lhs, lhs);
5830 : 44829301 : vn_nary_op_insert_stmt (stmt, lhs);
5831 : 44829301 : return changed;
5832 : : }
5833 : :
5834 : : /* Visit a call STMT storing into LHS. Return true if the value number
5835 : : of the LHS has changed as a result. */
5836 : :
5837 : : static bool
5838 : 8625282 : visit_reference_op_call (tree lhs, gcall *stmt)
5839 : : {
5840 : 8625282 : bool changed = false;
5841 : 8625282 : struct vn_reference_s vr1;
5842 : 8625282 : vn_reference_t vnresult = NULL;
5843 : 8625282 : tree vdef = gimple_vdef (stmt);
5844 : 8625282 : modref_summary *summary;
5845 : :
5846 : : /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5847 : 8625282 : if (lhs && TREE_CODE (lhs) != SSA_NAME)
5848 : 4604337 : lhs = NULL_TREE;
5849 : :
5850 : 8625282 : vn_reference_lookup_call (stmt, &vnresult, &vr1);
5851 : :
5852 : : /* If the lookup did not succeed for pure functions try to use
5853 : : modref info to find a candidate to CSE to. */
5854 : 8625282 : const unsigned accesses_limit = 8;
5855 : 8625282 : if (!vnresult
5856 : 7985718 : && !vdef
5857 : 7985718 : && lhs
5858 : 2786845 : && gimple_vuse (stmt)
5859 : 10178862 : && (((summary = get_modref_function_summary (stmt, NULL))
5860 : 212566 : && !summary->global_memory_read
5861 : 77526 : && summary->load_accesses < accesses_limit)
5862 : 1476198 : || gimple_call_flags (stmt) & ECF_CONST))
5863 : : {
5864 : : /* First search if we can do someting useful and build a
5865 : : vector of all loads we have to check. */
5866 : 78126 : bool unknown_memory_access = false;
5867 : 78126 : auto_vec<ao_ref, accesses_limit> accesses;
5868 : 78126 : unsigned load_accesses = summary ? summary->load_accesses : 0;
5869 : 78126 : if (!unknown_memory_access)
5870 : : /* Add loads done as part of setting up the call arguments.
5871 : : That's also necessary for CONST functions which will
5872 : : not have a modref summary. */
5873 : 230368 : for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
5874 : : {
5875 : 152250 : tree arg = gimple_call_arg (stmt, i);
5876 : 152250 : if (TREE_CODE (arg) != SSA_NAME
5877 : 152250 : && !is_gimple_min_invariant (arg))
5878 : : {
5879 : 57982 : if (accesses.length () >= accesses_limit - load_accesses)
5880 : : {
5881 : : unknown_memory_access = true;
5882 : : break;
5883 : : }
5884 : 28983 : accesses.quick_grow (accesses.length () + 1);
5885 : 28983 : ao_ref_init (&accesses.last (), arg);
5886 : : }
5887 : : }
5888 : 78126 : if (summary && !unknown_memory_access)
5889 : : {
5890 : : /* Add loads as analyzed by IPA modref. */
5891 : 271666 : for (auto base_node : summary->loads->bases)
5892 : 68359 : if (unknown_memory_access)
5893 : : break;
5894 : 278891 : else for (auto ref_node : base_node->refs)
5895 : 74522 : if (unknown_memory_access)
5896 : : break;
5897 : 303573 : else for (auto access_node : ref_node->accesses)
5898 : : {
5899 : 198692 : accesses.quick_grow (accesses.length () + 1);
5900 : 99346 : ao_ref *r = &accesses.last ();
5901 : 99346 : if (!access_node.get_ao_ref (stmt, r))
5902 : : {
5903 : : /* Initialize a ref based on the argument and
5904 : : unknown offset if possible. */
5905 : 19303 : tree arg = access_node.get_call_arg (stmt);
5906 : 19303 : if (arg && TREE_CODE (arg) == SSA_NAME)
5907 : 5623 : arg = SSA_VAL (arg);
5908 : 5623 : if (arg
5909 : 19293 : && TREE_CODE (arg) == ADDR_EXPR
5910 : 13814 : && (arg = get_base_address (arg))
5911 : 19437 : && DECL_P (arg))
5912 : : {
5913 : 0 : ao_ref_init (r, arg);
5914 : 0 : r->ref = NULL_TREE;
5915 : 0 : r->base = arg;
5916 : : }
5917 : : else
5918 : : {
5919 : : unknown_memory_access = true;
5920 : : break;
5921 : : }
5922 : : }
5923 : 80043 : r->base_alias_set = base_node->base;
5924 : 80043 : r->ref_alias_set = ref_node->ref;
5925 : : }
5926 : : }
5927 : :
5928 : : /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5929 : : for the call in the hashtable. */
5930 : 78126 : unsigned limit = (unknown_memory_access
5931 : 78126 : ? 0
5932 : 58815 : : (param_sccvn_max_alias_queries_per_access
5933 : 58815 : / (accesses.length () + 1)));
5934 : 78126 : tree saved_vuse = vr1.vuse;
5935 : 78126 : hashval_t saved_hashcode = vr1.hashcode;
5936 : 350193 : while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
5937 : : {
5938 : 290185 : vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
5939 : 290185 : gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
5940 : : /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5941 : : do not bother for now. */
5942 : 290185 : if (is_a <gphi *> (def))
5943 : : break;
5944 : 544134 : vr1.vuse = vuse_ssa_val (gimple_vuse (def));
5945 : 272067 : vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
5946 : 272067 : vn_reference_lookup_1 (&vr1, &vnresult);
5947 : 272067 : limit--;
5948 : : }
5949 : :
5950 : : /* If we found a candidate to CSE to verify it is valid. */
5951 : 78126 : if (vnresult && !accesses.is_empty ())
5952 : : {
5953 : 1904 : tree vuse = vuse_ssa_val (gimple_vuse (stmt));
5954 : 7131 : while (vnresult && vuse != vr1.vuse)
5955 : : {
5956 : 3323 : gimple *def = SSA_NAME_DEF_STMT (vuse);
5957 : 17642 : for (auto &ref : accesses)
5958 : : {
5959 : : /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5960 : : analysis overhead that we might be able to cache. */
5961 : 9407 : if (stmt_may_clobber_ref_p_1 (def, &ref, true))
5962 : : {
5963 : 1734 : vnresult = NULL;
5964 : 1734 : break;
5965 : : }
5966 : : }
5967 : 6646 : vuse = vuse_ssa_val (gimple_vuse (def));
5968 : : }
5969 : : }
5970 : 78126 : vr1.vuse = saved_vuse;
5971 : 78126 : vr1.hashcode = saved_hashcode;
5972 : 78126 : }
5973 : :
5974 : 8625282 : if (vnresult)
5975 : : {
5976 : 639758 : if (vdef)
5977 : : {
5978 : 175520 : if (vnresult->result_vdef)
5979 : 175520 : changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
5980 : 0 : else if (!lhs && gimple_call_lhs (stmt))
5981 : : /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5982 : : as the call still acts as a lhs store. */
5983 : 0 : changed |= set_ssa_val_to (vdef, vdef);
5984 : : else
5985 : : /* If the call was discovered to be pure or const reflect
5986 : : that as far as possible. */
5987 : 0 : changed |= set_ssa_val_to (vdef,
5988 : : vuse_ssa_val (gimple_vuse (stmt)));
5989 : : }
5990 : :
5991 : 639758 : if (!vnresult->result && lhs)
5992 : 0 : vnresult->result = lhs;
5993 : :
5994 : 639758 : if (vnresult->result && lhs)
5995 : 99977 : changed |= set_ssa_val_to (lhs, vnresult->result);
5996 : : }
5997 : : else
5998 : : {
5999 : 7985524 : vn_reference_t vr2;
6000 : 7985524 : vn_reference_s **slot;
6001 : 7985524 : tree vdef_val = vdef;
6002 : 7985524 : if (vdef)
6003 : : {
6004 : : /* If we value numbered an indirect functions function to
6005 : : one not clobbering memory value number its VDEF to its
6006 : : VUSE. */
6007 : 4872996 : tree fn = gimple_call_fn (stmt);
6008 : 4872996 : if (fn && TREE_CODE (fn) == SSA_NAME)
6009 : : {
6010 : 130144 : fn = SSA_VAL (fn);
6011 : 130144 : if (TREE_CODE (fn) == ADDR_EXPR
6012 : 1733 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
6013 : 1733 : && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
6014 : 1733 : & (ECF_CONST | ECF_PURE))
6015 : : /* If stmt has non-SSA_NAME lhs, value number the
6016 : : vdef to itself, as the call still acts as a lhs
6017 : : store. */
6018 : 131286 : && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
6019 : 2150 : vdef_val = vuse_ssa_val (gimple_vuse (stmt));
6020 : : }
6021 : 4872996 : changed |= set_ssa_val_to (vdef, vdef_val);
6022 : : }
6023 : 7985524 : if (lhs)
6024 : 3920968 : changed |= set_ssa_val_to (lhs, lhs);
6025 : 7985524 : vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
6026 : 7985524 : vr2->vuse = vr1.vuse;
6027 : : /* As we are not walking the virtual operand chain we know the
6028 : : shared_lookup_references are still original so we can re-use
6029 : : them here. */
6030 : 7985524 : vr2->operands = vr1.operands.copy ();
6031 : 7985524 : vr2->type = vr1.type;
6032 : 7985524 : vr2->punned = vr1.punned;
6033 : 7985524 : vr2->set = vr1.set;
6034 : 7985524 : vr2->offset = vr1.offset;
6035 : 7985524 : vr2->max_size = vr1.max_size;
6036 : 7985524 : vr2->base_set = vr1.base_set;
6037 : 7985524 : vr2->hashcode = vr1.hashcode;
6038 : 7985524 : vr2->result = lhs;
6039 : 7985524 : vr2->result_vdef = vdef_val;
6040 : 7985524 : vr2->value_id = 0;
6041 : 7985524 : slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
6042 : : INSERT);
6043 : 7985524 : gcc_assert (!*slot);
6044 : 7985524 : *slot = vr2;
6045 : 7985524 : vr2->next = last_inserted_ref;
6046 : 7985524 : last_inserted_ref = vr2;
6047 : : }
6048 : :
6049 : 8625282 : return changed;
6050 : : }
6051 : :
6052 : : /* Visit a load from a reference operator RHS, part of STMT, value number it,
6053 : : and return true if the value number of the LHS has changed as a result. */
6054 : :
6055 : : static bool
6056 : 34846829 : visit_reference_op_load (tree lhs, tree op, gimple *stmt)
6057 : : {
6058 : 34846829 : bool changed = false;
6059 : 34846829 : tree result;
6060 : 34846829 : vn_reference_t res;
6061 : :
6062 : 34846829 : tree vuse = gimple_vuse (stmt);
6063 : 34846829 : tree last_vuse = vuse;
6064 : 34846829 : result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
6065 : :
6066 : : /* We handle type-punning through unions by value-numbering based
6067 : : on offset and size of the access. Be prepared to handle a
6068 : : type-mismatch here via creating a VIEW_CONVERT_EXPR. */
6069 : 34846829 : if (result
6070 : 34846829 : && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
6071 : : {
6072 : 20891 : if (CONSTANT_CLASS_P (result))
6073 : 4218 : result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6074 : : else
6075 : : {
6076 : : /* We will be setting the value number of lhs to the value number
6077 : : of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
6078 : : So first simplify and lookup this expression to see if it
6079 : : is already available. */
6080 : 16673 : gimple_match_op res_op (gimple_match_cond::UNCOND,
6081 : 16673 : VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
6082 : 16673 : result = vn_nary_build_or_lookup (&res_op);
6083 : 16673 : if (result
6084 : 16667 : && TREE_CODE (result) == SSA_NAME
6085 : 31666 : && VN_INFO (result)->needs_insertion)
6086 : : /* Track whether this is the canonical expression for different
6087 : : typed loads. We use that as a stopgap measure for code
6088 : : hoisting when dealing with floating point loads. */
6089 : 14166 : res->punned = true;
6090 : : }
6091 : :
6092 : : /* When building the conversion fails avoid inserting the reference
6093 : : again. */
6094 : 20891 : if (!result)
6095 : 6 : return set_ssa_val_to (lhs, lhs);
6096 : : }
6097 : :
6098 : 34825938 : if (result)
6099 : 5508880 : changed = set_ssa_val_to (lhs, result);
6100 : : else
6101 : : {
6102 : 29337943 : changed = set_ssa_val_to (lhs, lhs);
6103 : 29337943 : vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
6104 : 29337943 : if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
6105 : : {
6106 : 8970637 : if (dump_file && (dump_flags & TDF_DETAILS))
6107 : : {
6108 : 14406 : fprintf (dump_file, "Using extra use virtual operand ");
6109 : 14406 : print_generic_expr (dump_file, last_vuse);
6110 : 14406 : fprintf (dump_file, "\n");
6111 : : }
6112 : 8970637 : vn_reference_insert (op, lhs, vuse, NULL_TREE);
6113 : : }
6114 : : }
6115 : :
6116 : : return changed;
6117 : : }
6118 : :
6119 : :
6120 : : /* Visit a store to a reference operator LHS, part of STMT, value number it,
6121 : : and return true if the value number of the LHS has changed as a result. */
6122 : :
6123 : : static bool
6124 : 32935328 : visit_reference_op_store (tree lhs, tree op, gimple *stmt)
6125 : : {
6126 : 32935328 : bool changed = false;
6127 : 32935328 : vn_reference_t vnresult = NULL;
6128 : 32935328 : tree assign;
6129 : 32935328 : bool resultsame = false;
6130 : 32935328 : tree vuse = gimple_vuse (stmt);
6131 : 32935328 : tree vdef = gimple_vdef (stmt);
6132 : :
6133 : 32935328 : if (TREE_CODE (op) == SSA_NAME)
6134 : 14891508 : op = SSA_VAL (op);
6135 : :
6136 : : /* First we want to lookup using the *vuses* from the store and see
6137 : : if there the last store to this location with the same address
6138 : : had the same value.
6139 : :
6140 : : The vuses represent the memory state before the store. If the
6141 : : memory state, address, and value of the store is the same as the
6142 : : last store to this location, then this store will produce the
6143 : : same memory state as that store.
6144 : :
6145 : : In this case the vdef versions for this store are value numbered to those
6146 : : vuse versions, since they represent the same memory state after
6147 : : this store.
6148 : :
6149 : : Otherwise, the vdefs for the store are used when inserting into
6150 : : the table, since the store generates a new memory state. */
6151 : :
6152 : 32935328 : vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
6153 : 32935328 : if (vnresult
6154 : 1723908 : && vnresult->result)
6155 : : {
6156 : 1723908 : tree result = vnresult->result;
6157 : 1723908 : gcc_checking_assert (TREE_CODE (result) != SSA_NAME
6158 : : || result == SSA_VAL (result));
6159 : 1723908 : resultsame = expressions_equal_p (result, op);
6160 : 1723908 : if (resultsame)
6161 : : {
6162 : : /* If the TBAA state isn't compatible for downstream reads
6163 : : we cannot value-number the VDEFs the same. */
6164 : 51732 : ao_ref lhs_ref;
6165 : 51732 : ao_ref_init (&lhs_ref, lhs);
6166 : 51732 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
6167 : 51732 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
6168 : 51732 : if ((vnresult->set != set
6169 : 735 : && ! alias_set_subset_of (set, vnresult->set))
6170 : 52243 : || (vnresult->base_set != base_set
6171 : 6351 : && ! alias_set_subset_of (base_set, vnresult->base_set)))
6172 : 780 : resultsame = false;
6173 : : }
6174 : : }
6175 : :
6176 : 780 : if (!resultsame)
6177 : : {
6178 : 32884376 : if (dump_file && (dump_flags & TDF_DETAILS))
6179 : : {
6180 : 19192 : fprintf (dump_file, "No store match\n");
6181 : 19192 : fprintf (dump_file, "Value numbering store ");
6182 : 19192 : print_generic_expr (dump_file, lhs);
6183 : 19192 : fprintf (dump_file, " to ");
6184 : 19192 : print_generic_expr (dump_file, op);
6185 : 19192 : fprintf (dump_file, "\n");
6186 : : }
6187 : : /* Have to set value numbers before insert, since insert is
6188 : : going to valueize the references in-place. */
6189 : 32884376 : if (vdef)
6190 : 32884376 : changed |= set_ssa_val_to (vdef, vdef);
6191 : :
6192 : : /* Do not insert structure copies into the tables. */
6193 : 32884376 : if (is_gimple_min_invariant (op)
6194 : 32884376 : || is_gimple_reg (op))
6195 : 29260816 : vn_reference_insert (lhs, op, vdef, NULL);
6196 : :
6197 : : /* Only perform the following when being called from PRE
6198 : : which embeds tail merging. */
6199 : 32884376 : if (default_vn_walk_kind == VN_WALK)
6200 : : {
6201 : 7553135 : assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
6202 : 7553135 : vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
6203 : 7553135 : if (!vnresult)
6204 : 7504555 : vn_reference_insert (assign, lhs, vuse, vdef);
6205 : : }
6206 : : }
6207 : : else
6208 : : {
6209 : : /* We had a match, so value number the vdef to have the value
6210 : : number of the vuse it came from. */
6211 : :
6212 : 50952 : if (dump_file && (dump_flags & TDF_DETAILS))
6213 : 9 : fprintf (dump_file, "Store matched earlier value, "
6214 : : "value numbering store vdefs to matching vuses.\n");
6215 : :
6216 : 50952 : changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
6217 : : }
6218 : :
6219 : 32935328 : return changed;
6220 : : }
6221 : :
6222 : : /* Visit and value number PHI, return true if the value number
6223 : : changed. When BACKEDGES_VARYING_P is true then assume all
6224 : : backedge values are varying. When INSERTED is not NULL then
6225 : : this is just a ahead query for a possible iteration, set INSERTED
6226 : : to true if we'd insert into the hashtable. */
6227 : :
6228 : : static bool
6229 : 34796540 : visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
6230 : : {
6231 : 34796540 : tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
6232 : 34796540 : bool seen_undef_visited = false;
6233 : 34796540 : tree backedge_val = NULL_TREE;
6234 : 34796540 : bool seen_non_backedge = false;
6235 : 34796540 : tree sameval_base = NULL_TREE;
6236 : 34796540 : poly_int64 soff, doff;
6237 : 34796540 : unsigned n_executable = 0;
6238 : 34796540 : edge sameval_e = NULL;
6239 : :
6240 : : /* TODO: We could check for this in initialization, and replace this
6241 : : with a gcc_assert. */
6242 : 34796540 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
6243 : 28197 : return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
6244 : :
6245 : : /* We track whether a PHI was CSEd to avoid excessive iterations
6246 : : that would be necessary only because the PHI changed arguments
6247 : : but not value. */
6248 : 34768343 : if (!inserted)
6249 : 27073911 : gimple_set_plf (phi, GF_PLF_1, false);
6250 : :
6251 : 34768343 : basic_block bb = gimple_bb (phi);
6252 : :
6253 : : /* For the equivalence handling below make sure to first process an
6254 : : edge with a non-constant. */
6255 : 34768343 : auto_vec<edge, 2> preds;
6256 : 69536686 : preds.reserve_exact (EDGE_COUNT (bb->preds));
6257 : 34768343 : bool seen_nonconstant = false;
6258 : 115334882 : for (unsigned i = 0; i < EDGE_COUNT (bb->preds); ++i)
6259 : : {
6260 : 80566539 : edge e = EDGE_PRED (bb, i);
6261 : 80566539 : preds.quick_push (e);
6262 : 80566539 : if (!seen_nonconstant)
6263 : : {
6264 : 42431120 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6265 : 42431120 : if (TREE_CODE (def) == SSA_NAME)
6266 : : {
6267 : 33031515 : seen_nonconstant = true;
6268 : 33031515 : if (i != 0)
6269 : 5632040 : std::swap (preds[0], preds[i]);
6270 : : }
6271 : : }
6272 : : }
6273 : :
6274 : : /* See if all non-TOP arguments have the same value. TOP is
6275 : : equivalent to everything, so we can ignore it. */
6276 : 146542499 : for (edge e : preds)
6277 : 69320125 : if (e->flags & EDGE_EXECUTABLE)
6278 : : {
6279 : 64196026 : tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6280 : :
6281 : 64196026 : if (def == PHI_RESULT (phi))
6282 : 377795 : continue;
6283 : 63839830 : ++n_executable;
6284 : 63839830 : bool visited = true;
6285 : 63839830 : if (TREE_CODE (def) == SSA_NAME)
6286 : : {
6287 : 51585273 : tree val = SSA_VAL (def, &visited);
6288 : 51585273 : if (SSA_NAME_IS_DEFAULT_DEF (def))
6289 : 2699691 : visited = true;
6290 : 51585273 : if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6291 : 49037648 : def = val;
6292 : 51585273 : if (e->flags & EDGE_DFS_BACK)
6293 : 15598370 : backedge_val = def;
6294 : : }
6295 : 63839830 : if (!(e->flags & EDGE_DFS_BACK))
6296 : 48043603 : seen_non_backedge = true;
6297 : 63839830 : if (def == VN_TOP)
6298 : : ;
6299 : : /* Ignore undefined defs for sameval but record one. */
6300 : 63839830 : else if (TREE_CODE (def) == SSA_NAME
6301 : 48156796 : && ! virtual_operand_p (def)
6302 : 88509698 : && ssa_undefined_value_p (def, false))
6303 : : {
6304 : 229296 : if (!seen_undef
6305 : : /* Avoid having not visited undefined defs if we also have
6306 : : a visited one. */
6307 : 28288 : || (!seen_undef_visited && visited))
6308 : : {
6309 : 201009 : seen_undef = def;
6310 : 201009 : seen_undef_visited = visited;
6311 : : }
6312 : : }
6313 : 63610534 : else if (sameval == VN_TOP)
6314 : : {
6315 : : sameval = def;
6316 : : sameval_e = e;
6317 : : }
6318 : 28890550 : else if (expressions_equal_p (def, sameval))
6319 : : sameval_e = NULL;
6320 : 45324718 : else if (virtual_operand_p (def))
6321 : : {
6322 : : sameval = NULL_TREE;
6323 : 27082655 : break;
6324 : : }
6325 : : else
6326 : : {
6327 : : /* We know we're arriving only with invariant addresses here,
6328 : : try harder comparing them. We can do some caching here
6329 : : which we cannot do in expressions_equal_p. */
6330 : 16969601 : if (TREE_CODE (def) == ADDR_EXPR
6331 : 425313 : && TREE_CODE (sameval) == ADDR_EXPR
6332 : 120978 : && sameval_base != (void *)-1)
6333 : : {
6334 : 120978 : if (!sameval_base)
6335 : 120976 : sameval_base = get_addr_base_and_unit_offset
6336 : 120976 : (TREE_OPERAND (sameval, 0), &soff);
6337 : 120976 : if (!sameval_base)
6338 : : sameval_base = (tree)(void *)-1;
6339 : 120983 : else if ((get_addr_base_and_unit_offset
6340 : 120978 : (TREE_OPERAND (def, 0), &doff) == sameval_base)
6341 : 120978 : && known_eq (soff, doff))
6342 : 5 : continue;
6343 : : }
6344 : : /* There's also the possibility to use equivalences. */
6345 : 32848997 : if (!FLOAT_TYPE_P (TREE_TYPE (def))
6346 : : /* But only do this if we didn't force any of sameval or
6347 : : val to VARYING because of backedge processing rules. */
6348 : 15775991 : && (TREE_CODE (sameval) != SSA_NAME
6349 : 12437609 : || SSA_VAL (sameval) == sameval)
6350 : 32745363 : && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6351 : : {
6352 : 15775754 : vn_nary_op_t vnresult;
6353 : 15775754 : tree ops[2];
6354 : 15775754 : ops[0] = def;
6355 : 15775754 : ops[1] = sameval;
6356 : : /* Canonicalize the operands order for eq below. */
6357 : 15775754 : if (tree_swap_operands_p (ops[0], ops[1]))
6358 : 9448802 : std::swap (ops[0], ops[1]);
6359 : 15775754 : tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6360 : : boolean_type_node,
6361 : : ops, &vnresult);
6362 : 15775754 : if (! val && vnresult && vnresult->predicated_values)
6363 : : {
6364 : 206874 : val = vn_nary_op_get_predicated_value (vnresult, e);
6365 : 116533 : if (val && integer_truep (val)
6366 : 228589 : && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6367 : : {
6368 : 21594 : if (dump_file && (dump_flags & TDF_DETAILS))
6369 : : {
6370 : 2 : fprintf (dump_file, "Predication says ");
6371 : 2 : print_generic_expr (dump_file, def, TDF_NONE);
6372 : 2 : fprintf (dump_file, " and ");
6373 : 2 : print_generic_expr (dump_file, sameval, TDF_NONE);
6374 : 2 : fprintf (dump_file, " are equal on edge %d -> %d\n",
6375 : 2 : e->src->index, e->dest->index);
6376 : : }
6377 : 21594 : continue;
6378 : : }
6379 : : }
6380 : : }
6381 : : sameval = NULL_TREE;
6382 : : break;
6383 : : }
6384 : : }
6385 : :
6386 : : /* If the value we want to use is flowing over the backedge and we
6387 : : should take it as VARYING but it has a non-VARYING value drop to
6388 : : VARYING.
6389 : : If we value-number a virtual operand never value-number to the
6390 : : value from the backedge as that confuses the alias-walking code.
6391 : : See gcc.dg/torture/pr87176.c. If the value is the same on a
6392 : : non-backedge everything is OK though. */
6393 : 34768343 : bool visited_p;
6394 : 34768343 : if ((backedge_val
6395 : 34768343 : && !seen_non_backedge
6396 : 2565 : && TREE_CODE (backedge_val) == SSA_NAME
6397 : 2298 : && sameval == backedge_val
6398 : 317 : && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6399 : 44 : || SSA_VAL (backedge_val) != backedge_val))
6400 : : /* Do not value-number a virtual operand to sth not visited though
6401 : : given that allows us to escape a region in alias walking. */
6402 : 34770635 : || (sameval
6403 : 7685415 : && TREE_CODE (sameval) == SSA_NAME
6404 : 4598558 : && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6405 : 3897530 : && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6406 : 1932354 : && (SSA_VAL (sameval, &visited_p), !visited_p)))
6407 : : /* Note this just drops to VARYING without inserting the PHI into
6408 : : the hashes. */
6409 : 290918 : result = PHI_RESULT (phi);
6410 : : /* If none of the edges was executable keep the value-number at VN_TOP,
6411 : : if only a single edge is exectuable use its value. */
6412 : 34477425 : else if (n_executable <= 1)
6413 : 6594663 : result = seen_undef ? seen_undef : sameval;
6414 : : /* If we saw only undefined values and VN_TOP use one of the
6415 : : undefined values. */
6416 : 27882762 : else if (sameval == VN_TOP)
6417 : 7210355 : result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6418 : : /* First see if it is equivalent to a phi node in this block. We prefer
6419 : : this as it allows IV elimination - see PRs 66502 and 67167. */
6420 : 27877437 : else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6421 : : {
6422 : 4306154 : if (!inserted
6423 : 67633 : && TREE_CODE (result) == SSA_NAME
6424 : 4373787 : && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6425 : : {
6426 : 67633 : gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6427 : 67633 : if (dump_file && (dump_flags & TDF_DETAILS))
6428 : : {
6429 : 2 : fprintf (dump_file, "Marking CSEd to PHI node ");
6430 : 2 : print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6431 : : 0, TDF_SLIM);
6432 : 2 : fprintf (dump_file, "\n");
6433 : : }
6434 : : }
6435 : : }
6436 : : /* If all values are the same use that, unless we've seen undefined
6437 : : values as well and the value isn't constant.
6438 : : CCP/copyprop have the same restriction to not remove uninit warnings. */
6439 : 23571283 : else if (sameval
6440 : 23571283 : && (! seen_undef || is_gimple_min_invariant (sameval)))
6441 : : result = sameval;
6442 : : else
6443 : : {
6444 : 22917882 : result = PHI_RESULT (phi);
6445 : : /* Only insert PHIs that are varying, for constant value numbers
6446 : : we mess up equivalences otherwise as we are only comparing
6447 : : the immediate controlling predicates. */
6448 : 22917882 : vn_phi_insert (phi, result, backedges_varying_p);
6449 : 22917882 : if (inserted)
6450 : 3292614 : *inserted = true;
6451 : : }
6452 : :
6453 : 34768343 : return set_ssa_val_to (PHI_RESULT (phi), result);
6454 : 34768343 : }
6455 : :
6456 : : /* Try to simplify RHS using equivalences and constant folding. */
6457 : :
6458 : : static tree
6459 : 126781106 : try_to_simplify (gassign *stmt)
6460 : : {
6461 : 126781106 : enum tree_code code = gimple_assign_rhs_code (stmt);
6462 : 126781106 : tree tem;
6463 : :
6464 : : /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6465 : : in this case, there is no point in doing extra work. */
6466 : 126781106 : if (code == SSA_NAME)
6467 : : return NULL_TREE;
6468 : :
6469 : : /* First try constant folding based on our current lattice. */
6470 : 111889345 : mprts_hook = vn_lookup_simplify_result;
6471 : 111889345 : tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6472 : 111889345 : mprts_hook = NULL;
6473 : 111889345 : if (tem
6474 : 111889345 : && (TREE_CODE (tem) == SSA_NAME
6475 : 24750664 : || is_gimple_min_invariant (tem)))
6476 : 24692401 : return tem;
6477 : :
6478 : : return NULL_TREE;
6479 : : }
6480 : :
6481 : : /* Visit and value number STMT, return true if the value number
6482 : : changed. */
6483 : :
6484 : : static bool
6485 : 451072664 : visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6486 : : {
6487 : 451072664 : bool changed = false;
6488 : :
6489 : 451072664 : if (dump_file && (dump_flags & TDF_DETAILS))
6490 : : {
6491 : 390526 : fprintf (dump_file, "Value numbering stmt = ");
6492 : 390526 : print_gimple_stmt (dump_file, stmt, 0);
6493 : : }
6494 : :
6495 : 451072664 : if (gimple_code (stmt) == GIMPLE_PHI)
6496 : 27093438 : changed = visit_phi (stmt, NULL, backedges_varying_p);
6497 : 595135704 : else if (gimple_has_volatile_ops (stmt))
6498 : 8794871 : changed = defs_to_varying (stmt);
6499 : 415184355 : else if (gassign *ass = dyn_cast <gassign *> (stmt))
6500 : : {
6501 : 131856429 : enum tree_code code = gimple_assign_rhs_code (ass);
6502 : 131856429 : tree lhs = gimple_assign_lhs (ass);
6503 : 131856429 : tree rhs1 = gimple_assign_rhs1 (ass);
6504 : 131856429 : tree simplified;
6505 : :
6506 : : /* Shortcut for copies. Simplifying copies is pointless,
6507 : : since we copy the expression and value they represent. */
6508 : 131856429 : if (code == SSA_NAME
6509 : 19967084 : && TREE_CODE (lhs) == SSA_NAME)
6510 : : {
6511 : 5075323 : changed = visit_copy (lhs, rhs1);
6512 : 5075323 : goto done;
6513 : : }
6514 : 126781106 : simplified = try_to_simplify (ass);
6515 : 126781106 : if (simplified)
6516 : : {
6517 : 24692401 : if (dump_file && (dump_flags & TDF_DETAILS))
6518 : : {
6519 : 13761 : fprintf (dump_file, "RHS ");
6520 : 13761 : print_gimple_expr (dump_file, ass, 0);
6521 : 13761 : fprintf (dump_file, " simplified to ");
6522 : 13761 : print_generic_expr (dump_file, simplified);
6523 : 13761 : fprintf (dump_file, "\n");
6524 : : }
6525 : : }
6526 : : /* Setting value numbers to constants will occasionally
6527 : : screw up phi congruence because constants are not
6528 : : uniquely associated with a single ssa name that can be
6529 : : looked up. */
6530 : 24692401 : if (simplified
6531 : 24692401 : && is_gimple_min_invariant (simplified)
6532 : 21840583 : && TREE_CODE (lhs) == SSA_NAME)
6533 : : {
6534 : 7420303 : changed = set_ssa_val_to (lhs, simplified);
6535 : 7420303 : goto done;
6536 : : }
6537 : 119360803 : else if (simplified
6538 : 17272098 : && TREE_CODE (simplified) == SSA_NAME
6539 : 2851818 : && TREE_CODE (lhs) == SSA_NAME)
6540 : : {
6541 : 2851818 : changed = visit_copy (lhs, simplified);
6542 : 2851818 : goto done;
6543 : : }
6544 : :
6545 : 116508985 : if ((TREE_CODE (lhs) == SSA_NAME
6546 : : /* We can substitute SSA_NAMEs that are live over
6547 : : abnormal edges with their constant value. */
6548 : 83573430 : && !(gimple_assign_copy_p (ass)
6549 : 26 : && is_gimple_min_invariant (rhs1))
6550 : 83573404 : && !(simplified
6551 : 0 : && is_gimple_min_invariant (simplified))
6552 : 83573404 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6553 : : /* Stores or copies from SSA_NAMEs that are live over
6554 : : abnormal edges are a problem. */
6555 : 200081105 : || (code == SSA_NAME
6556 : 14891761 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6557 : 1537 : changed = defs_to_varying (ass);
6558 : 116507448 : else if (REFERENCE_CLASS_P (lhs)
6559 : 116507448 : || DECL_P (lhs))
6560 : 32935328 : changed = visit_reference_op_store (lhs, rhs1, ass);
6561 : 83572120 : else if (TREE_CODE (lhs) == SSA_NAME)
6562 : : {
6563 : 83572120 : if ((gimple_assign_copy_p (ass)
6564 : 26 : && is_gimple_min_invariant (rhs1))
6565 : 83572146 : || (simplified
6566 : 0 : && is_gimple_min_invariant (simplified)))
6567 : : {
6568 : 0 : if (simplified)
6569 : 0 : changed = set_ssa_val_to (lhs, simplified);
6570 : : else
6571 : 0 : changed = set_ssa_val_to (lhs, rhs1);
6572 : : }
6573 : : else
6574 : : {
6575 : : /* Visit the original statement. */
6576 : 83572120 : switch (vn_get_stmt_kind (ass))
6577 : : {
6578 : 48624130 : case VN_NARY:
6579 : 48624130 : changed = visit_nary_op (lhs, ass);
6580 : 48624130 : break;
6581 : 34846829 : case VN_REFERENCE:
6582 : 34846829 : changed = visit_reference_op_load (lhs, rhs1, ass);
6583 : 34846829 : break;
6584 : 101161 : default:
6585 : 101161 : changed = defs_to_varying (ass);
6586 : 101161 : break;
6587 : : }
6588 : : }
6589 : : }
6590 : : else
6591 : 0 : changed = defs_to_varying (ass);
6592 : : }
6593 : 283327926 : else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6594 : : {
6595 : 24781848 : tree lhs = gimple_call_lhs (call_stmt);
6596 : 24781848 : if (lhs && TREE_CODE (lhs) == SSA_NAME)
6597 : : {
6598 : : /* Try constant folding based on our current lattice. */
6599 : 8310838 : tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6600 : : vn_valueize);
6601 : 8310838 : if (simplified)
6602 : : {
6603 : 60387 : if (dump_file && (dump_flags & TDF_DETAILS))
6604 : : {
6605 : 1 : fprintf (dump_file, "call ");
6606 : 1 : print_gimple_expr (dump_file, call_stmt, 0);
6607 : 1 : fprintf (dump_file, " simplified to ");
6608 : 1 : print_generic_expr (dump_file, simplified);
6609 : 1 : fprintf (dump_file, "\n");
6610 : : }
6611 : : }
6612 : : /* Setting value numbers to constants will occasionally
6613 : : screw up phi congruence because constants are not
6614 : : uniquely associated with a single ssa name that can be
6615 : : looked up. */
6616 : 60387 : if (simplified
6617 : 60387 : && is_gimple_min_invariant (simplified))
6618 : : {
6619 : 53729 : changed = set_ssa_val_to (lhs, simplified);
6620 : 107458 : if (gimple_vdef (call_stmt))
6621 : 769 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6622 : : SSA_VAL (gimple_vuse (call_stmt)));
6623 : 53729 : goto done;
6624 : : }
6625 : 8257109 : else if (simplified
6626 : 6658 : && TREE_CODE (simplified) == SSA_NAME)
6627 : : {
6628 : 301 : changed = visit_copy (lhs, simplified);
6629 : 602 : if (gimple_vdef (call_stmt))
6630 : 0 : changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6631 : : SSA_VAL (gimple_vuse (call_stmt)));
6632 : 301 : goto done;
6633 : : }
6634 : 8256808 : else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6635 : : {
6636 : 381 : changed = defs_to_varying (call_stmt);
6637 : 381 : goto done;
6638 : : }
6639 : : }
6640 : :
6641 : : /* Pick up flags from a devirtualization target. */
6642 : 24727437 : tree fn = gimple_call_fn (stmt);
6643 : 24727437 : int extra_fnflags = 0;
6644 : 24727437 : if (fn && TREE_CODE (fn) == SSA_NAME)
6645 : : {
6646 : 535809 : fn = SSA_VAL (fn);
6647 : 535809 : if (TREE_CODE (fn) == ADDR_EXPR
6648 : 535809 : && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6649 : 4840 : extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6650 : : }
6651 : 24727437 : if ((/* Calls to the same function with the same vuse
6652 : : and the same operands do not necessarily return the same
6653 : : value, unless they're pure or const. */
6654 : 24727437 : ((gimple_call_flags (call_stmt) | extra_fnflags)
6655 : 24727437 : & (ECF_PURE | ECF_CONST))
6656 : : /* If calls have a vdef, subsequent calls won't have
6657 : : the same incoming vuse. So, if 2 calls with vdef have the
6658 : : same vuse, we know they're not subsequent.
6659 : : We can value number 2 calls to the same function with the
6660 : : same vuse and the same operands which are not subsequent
6661 : : the same, because there is no code in the program that can
6662 : : compare the 2 values... */
6663 : 20768130 : || (gimple_vdef (call_stmt)
6664 : : /* ... unless the call returns a pointer which does
6665 : : not alias with anything else. In which case the
6666 : : information that the values are distinct are encoded
6667 : : in the IL. */
6668 : 20733415 : && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6669 : : /* Only perform the following when being called from PRE
6670 : : which embeds tail merging. */
6671 : 20287840 : && default_vn_walk_kind == VN_WALK))
6672 : : /* Do not process .DEFERRED_INIT since that confuses uninit
6673 : : analysis. */
6674 : 29712458 : && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6675 : 8625282 : changed = visit_reference_op_call (lhs, call_stmt);
6676 : : else
6677 : 16102155 : changed = defs_to_varying (call_stmt);
6678 : : }
6679 : : else
6680 : 258546078 : changed = defs_to_varying (stmt);
6681 : 451072664 : done:
6682 : 451072664 : return changed;
6683 : : }
6684 : :
6685 : :
6686 : : /* Allocate a value number table. */
6687 : :
6688 : : static void
6689 : 6180950 : allocate_vn_table (vn_tables_t table, unsigned size)
6690 : : {
6691 : 6180950 : table->phis = new vn_phi_table_type (size);
6692 : 6180950 : table->nary = new vn_nary_op_table_type (size);
6693 : 6180950 : table->references = new vn_reference_table_type (size);
6694 : 6180950 : }
6695 : :
6696 : : /* Free a value number table. */
6697 : :
6698 : : static void
6699 : 6180950 : free_vn_table (vn_tables_t table)
6700 : : {
6701 : : /* Walk over elements and release vectors. */
6702 : 6180950 : vn_reference_iterator_type hir;
6703 : 6180950 : vn_reference_t vr;
6704 : 152045538 : FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
6705 : 72932294 : vr->operands.release ();
6706 : 6180950 : delete table->phis;
6707 : 6180950 : table->phis = NULL;
6708 : 6180950 : delete table->nary;
6709 : 6180950 : table->nary = NULL;
6710 : 6180950 : delete table->references;
6711 : 6180950 : table->references = NULL;
6712 : 6180950 : }
6713 : :
6714 : : /* Set *ID according to RESULT. */
6715 : :
6716 : : static void
6717 : 35014372 : set_value_id_for_result (tree result, unsigned int *id)
6718 : : {
6719 : 35014372 : if (result && TREE_CODE (result) == SSA_NAME)
6720 : 21871676 : *id = VN_INFO (result)->value_id;
6721 : 9858503 : else if (result && is_gimple_min_invariant (result))
6722 : 3722838 : *id = get_or_alloc_constant_value_id (result);
6723 : : else
6724 : 9419858 : *id = get_next_value_id ();
6725 : 35014372 : }
6726 : :
6727 : : /* Set the value ids in the valid hash tables. */
6728 : :
6729 : : static void
6730 : 964692 : set_hashtable_value_ids (void)
6731 : : {
6732 : 964692 : vn_nary_op_iterator_type hin;
6733 : 964692 : vn_phi_iterator_type hip;
6734 : 964692 : vn_reference_iterator_type hir;
6735 : 964692 : vn_nary_op_t vno;
6736 : 964692 : vn_reference_t vr;
6737 : 964692 : vn_phi_t vp;
6738 : :
6739 : : /* Now set the value ids of the things we had put in the hash
6740 : : table. */
6741 : :
6742 : 49466444 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
6743 : 24250876 : if (! vno->predicated_values)
6744 : 7851241 : set_value_id_for_result (vno->u.result, &vno->value_id);
6745 : :
6746 : 9170372 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
6747 : 4102840 : set_value_id_for_result (vp->result, &vp->value_id);
6748 : :
6749 : 47085274 : FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
6750 : : hir)
6751 : 23060291 : set_value_id_for_result (vr->result, &vr->value_id);
6752 : 964692 : }
6753 : :
6754 : : /* Return the maximum value id we have ever seen. */
6755 : :
6756 : : unsigned int
6757 : 1929384 : get_max_value_id (void)
6758 : : {
6759 : 1929384 : return next_value_id;
6760 : : }
6761 : :
6762 : : /* Return the maximum constant value id we have ever seen. */
6763 : :
6764 : : unsigned int
6765 : 1929384 : get_max_constant_value_id (void)
6766 : : {
6767 : 1929384 : return -next_constant_value_id;
6768 : : }
6769 : :
6770 : : /* Return the next unique value id. */
6771 : :
6772 : : unsigned int
6773 : 49476429 : get_next_value_id (void)
6774 : : {
6775 : 49476429 : gcc_checking_assert ((int)next_value_id > 0);
6776 : 49476429 : return next_value_id++;
6777 : : }
6778 : :
6779 : : /* Return the next unique value id for constants. */
6780 : :
6781 : : unsigned int
6782 : 2559996 : get_next_constant_value_id (void)
6783 : : {
6784 : 2559996 : gcc_checking_assert (next_constant_value_id < 0);
6785 : 2559996 : return next_constant_value_id--;
6786 : : }
6787 : :
6788 : :
6789 : : /* Compare two expressions E1 and E2 and return true if they are equal.
6790 : : If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6791 : : otherwise VN_TOP only matches VN_TOP. */
6792 : :
6793 : : bool
6794 : 240237713 : expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
6795 : : {
6796 : : /* The obvious case. */
6797 : 240237713 : if (e1 == e2)
6798 : : return true;
6799 : :
6800 : : /* If either one is VN_TOP consider them equal. */
6801 : 70761966 : if (match_vn_top_optimistically
6802 : 65931170 : && (e1 == VN_TOP || e2 == VN_TOP))
6803 : : return true;
6804 : :
6805 : : /* If only one of them is null, they cannot be equal. While in general
6806 : : this should not happen for operations like TARGET_MEM_REF some
6807 : : operands are optional and an identity value we could substitute
6808 : : has differing semantics. */
6809 : 70761966 : if (!e1 || !e2)
6810 : : return false;
6811 : :
6812 : : /* SSA_NAME compare pointer equal. */
6813 : 70761966 : if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
6814 : : return false;
6815 : :
6816 : : /* Now perform the actual comparison. */
6817 : 34786293 : if (TREE_CODE (e1) == TREE_CODE (e2)
6818 : 34786293 : && operand_equal_p (e1, e2, OEP_PURE_SAME))
6819 : : return true;
6820 : :
6821 : : return false;
6822 : : }
6823 : :
6824 : :
6825 : : /* Return true if the nary operation NARY may trap. This is a copy
6826 : : of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6827 : :
6828 : : bool
6829 : 5378827 : vn_nary_may_trap (vn_nary_op_t nary)
6830 : : {
6831 : 5378827 : tree type;
6832 : 5378827 : tree rhs2 = NULL_TREE;
6833 : 5378827 : bool honor_nans = false;
6834 : 5378827 : bool honor_snans = false;
6835 : 5378827 : bool fp_operation = false;
6836 : 5378827 : bool honor_trapv = false;
6837 : 5378827 : bool handled, ret;
6838 : 5378827 : unsigned i;
6839 : :
6840 : 5378827 : if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
6841 : : || TREE_CODE_CLASS (nary->opcode) == tcc_unary
6842 : 5378827 : || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
6843 : : {
6844 : 5291075 : type = nary->type;
6845 : 5291075 : fp_operation = FLOAT_TYPE_P (type);
6846 : 5291075 : if (fp_operation)
6847 : : {
6848 : 118780 : honor_nans = flag_trapping_math && !flag_finite_math_only;
6849 : 118780 : honor_snans = flag_signaling_nans != 0;
6850 : : }
6851 : 5172295 : else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
6852 : : honor_trapv = true;
6853 : : }
6854 : 5378827 : if (nary->length >= 2)
6855 : 2187486 : rhs2 = nary->op[1];
6856 : 5378827 : ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
6857 : : honor_trapv, honor_nans, honor_snans,
6858 : : rhs2, &handled);
6859 : 5378827 : if (handled && ret)
6860 : : return true;
6861 : :
6862 : 12623695 : for (i = 0; i < nary->length; ++i)
6863 : 7362418 : if (tree_could_trap_p (nary->op[i]))
6864 : : return true;
6865 : :
6866 : : return false;
6867 : : }
6868 : :
6869 : : /* Return true if the reference operation REF may trap. */
6870 : :
6871 : : bool
6872 : 1868228 : vn_reference_may_trap (vn_reference_t ref)
6873 : : {
6874 : 1868228 : switch (ref->operands[0].opcode)
6875 : : {
6876 : : case MODIFY_EXPR:
6877 : : case CALL_EXPR:
6878 : : /* We do not handle calls. */
6879 : : return true;
6880 : : case ADDR_EXPR:
6881 : : /* And toplevel address computations never trap. */
6882 : : return false;
6883 : : default:;
6884 : : }
6885 : :
6886 : : vn_reference_op_t op;
6887 : : unsigned i;
6888 : 4981826 : FOR_EACH_VEC_ELT (ref->operands, i, op)
6889 : : {
6890 : 4981622 : switch (op->opcode)
6891 : : {
6892 : : case WITH_SIZE_EXPR:
6893 : : case TARGET_MEM_REF:
6894 : : /* Always variable. */
6895 : : return true;
6896 : 1237095 : case COMPONENT_REF:
6897 : 1237095 : if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
6898 : : return true;
6899 : : break;
6900 : 0 : case ARRAY_RANGE_REF:
6901 : 0 : if (TREE_CODE (op->op0) == SSA_NAME)
6902 : : return true;
6903 : : break;
6904 : 242063 : case ARRAY_REF:
6905 : 242063 : {
6906 : 242063 : if (TREE_CODE (op->op0) != INTEGER_CST)
6907 : : return true;
6908 : :
6909 : : /* !in_array_bounds */
6910 : 214914 : tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
6911 : 214914 : if (!domain_type)
6912 : : return true;
6913 : :
6914 : 214781 : tree min = op->op1;
6915 : 214781 : tree max = TYPE_MAX_VALUE (domain_type);
6916 : 214781 : if (!min
6917 : 214781 : || !max
6918 : 201495 : || TREE_CODE (min) != INTEGER_CST
6919 : 201495 : || TREE_CODE (max) != INTEGER_CST)
6920 : : return true;
6921 : :
6922 : 198869 : if (tree_int_cst_lt (op->op0, min)
6923 : 198869 : || tree_int_cst_lt (max, op->op0))
6924 : 638 : return true;
6925 : :
6926 : : break;
6927 : : }
6928 : : case MEM_REF:
6929 : : /* Nothing interesting in itself, the base is separate. */
6930 : : break;
6931 : : /* The following are the address bases. */
6932 : : case SSA_NAME:
6933 : : return true;
6934 : 1234874 : case ADDR_EXPR:
6935 : 1234874 : if (op->op0)
6936 : 1234874 : return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
6937 : : return false;
6938 : 3202955 : default:;
6939 : : }
6940 : : }
6941 : : return false;
6942 : : }
6943 : :
6944 : 10490834 : eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
6945 : 10490834 : bitmap inserted_exprs_)
6946 : 10490834 : : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
6947 : 10490834 : el_todo (0), eliminations (0), insertions (0),
6948 : 10490834 : inserted_exprs (inserted_exprs_)
6949 : : {
6950 : 10490834 : need_eh_cleanup = BITMAP_ALLOC (NULL);
6951 : 10490834 : need_ab_cleanup = BITMAP_ALLOC (NULL);
6952 : 10490834 : }
6953 : :
6954 : 10490834 : eliminate_dom_walker::~eliminate_dom_walker ()
6955 : : {
6956 : 10490834 : BITMAP_FREE (need_eh_cleanup);
6957 : 10490834 : BITMAP_FREE (need_ab_cleanup);
6958 : 10490834 : }
6959 : :
6960 : : /* Return a leader for OP that is available at the current point of the
6961 : : eliminate domwalk. */
6962 : :
6963 : : tree
6964 : 181275764 : eliminate_dom_walker::eliminate_avail (basic_block, tree op)
6965 : : {
6966 : 181275764 : tree valnum = VN_INFO (op)->valnum;
6967 : 181275764 : if (TREE_CODE (valnum) == SSA_NAME)
6968 : : {
6969 : 176333704 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6970 : : return valnum;
6971 : 307135988 : if (avail.length () > SSA_NAME_VERSION (valnum))
6972 : : {
6973 : 138327492 : tree av = avail[SSA_NAME_VERSION (valnum)];
6974 : : /* When PRE discovers a new redundancy there's no way to unite
6975 : : the value classes so it instead inserts a copy old-val = new-val.
6976 : : Look through such copies here, providing one more level of
6977 : : simplification at elimination time. */
6978 : 138327492 : gassign *ass;
6979 : 242681256 : if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
6980 : 73882814 : if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
6981 : : {
6982 : 39453605 : tree rhs1 = gimple_assign_rhs1 (ass);
6983 : 39453605 : if (CONSTANT_CLASS_P (rhs1)
6984 : 39453605 : || (TREE_CODE (rhs1) == SSA_NAME
6985 : 9136 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6986 : : av = rhs1;
6987 : : }
6988 : 138327492 : return av;
6989 : : }
6990 : : }
6991 : 4942060 : else if (is_gimple_min_invariant (valnum))
6992 : : return valnum;
6993 : : return NULL_TREE;
6994 : : }
6995 : :
6996 : : /* At the current point of the eliminate domwalk make OP available. */
6997 : :
6998 : : void
6999 : 50569776 : eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
7000 : : {
7001 : 50569776 : tree valnum = VN_INFO (op)->valnum;
7002 : 50569776 : if (TREE_CODE (valnum) == SSA_NAME)
7003 : : {
7004 : 97767977 : if (avail.length () <= SSA_NAME_VERSION (valnum))
7005 : 16936952 : avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
7006 : 50569776 : tree pushop = op;
7007 : 50569776 : if (avail[SSA_NAME_VERSION (valnum)])
7008 : 43529 : pushop = avail[SSA_NAME_VERSION (valnum)];
7009 : 50569776 : avail_stack.safe_push (pushop);
7010 : 50569776 : avail[SSA_NAME_VERSION (valnum)] = op;
7011 : : }
7012 : 50569776 : }
7013 : :
7014 : : /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
7015 : : the leader for the expression if insertion was successful. */
7016 : :
7017 : : tree
7018 : 128963 : eliminate_dom_walker::eliminate_insert (basic_block bb,
7019 : : gimple_stmt_iterator *gsi, tree val)
7020 : : {
7021 : : /* We can insert a sequence with a single assignment only. */
7022 : 128963 : gimple_seq stmts = VN_INFO (val)->expr;
7023 : 128963 : if (!gimple_seq_singleton_p (stmts))
7024 : : return NULL_TREE;
7025 : 235199 : gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
7026 : 128963 : if (!stmt
7027 : 128963 : || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7028 : : && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
7029 : : && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
7030 : : && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
7031 : : && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
7032 : 75 : || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
7033 : : return NULL_TREE;
7034 : :
7035 : 35812 : tree op = gimple_assign_rhs1 (stmt);
7036 : 35812 : if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
7037 : 35812 : || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7038 : 19258 : op = TREE_OPERAND (op, 0);
7039 : 35812 : tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
7040 : 35766 : if (!leader)
7041 : : return NULL_TREE;
7042 : :
7043 : 22731 : tree res;
7044 : 22731 : stmts = NULL;
7045 : 41090 : if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
7046 : 32826 : res = gimple_build (&stmts, BIT_FIELD_REF,
7047 : 16413 : TREE_TYPE (val), leader,
7048 : 16413 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
7049 : 16413 : TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
7050 : 6318 : else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
7051 : 150 : res = gimple_build (&stmts, BIT_AND_EXPR,
7052 : 75 : TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
7053 : : else
7054 : 6243 : res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
7055 : 6243 : TREE_TYPE (val), leader);
7056 : 22731 : if (TREE_CODE (res) != SSA_NAME
7057 : 22730 : || SSA_NAME_IS_DEFAULT_DEF (res)
7058 : 45461 : || gimple_bb (SSA_NAME_DEF_STMT (res)))
7059 : : {
7060 : 4 : gimple_seq_discard (stmts);
7061 : :
7062 : : /* During propagation we have to treat SSA info conservatively
7063 : : and thus we can end up simplifying the inserted expression
7064 : : at elimination time to sth not defined in stmts. */
7065 : : /* But then this is a redundancy we failed to detect. Which means
7066 : : res now has two values. That doesn't play well with how
7067 : : we track availability here, so give up. */
7068 : 4 : if (dump_file && (dump_flags & TDF_DETAILS))
7069 : : {
7070 : 0 : if (TREE_CODE (res) == SSA_NAME)
7071 : 0 : res = eliminate_avail (bb, res);
7072 : 0 : if (res)
7073 : : {
7074 : 0 : fprintf (dump_file, "Failed to insert expression for value ");
7075 : 0 : print_generic_expr (dump_file, val);
7076 : 0 : fprintf (dump_file, " which is really fully redundant to ");
7077 : 0 : print_generic_expr (dump_file, res);
7078 : 0 : fprintf (dump_file, "\n");
7079 : : }
7080 : : }
7081 : :
7082 : 4 : return NULL_TREE;
7083 : : }
7084 : : else
7085 : : {
7086 : 22727 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
7087 : 22727 : vn_ssa_aux_t vn_info = VN_INFO (res);
7088 : 22727 : vn_info->valnum = val;
7089 : 22727 : vn_info->visited = true;
7090 : : }
7091 : :
7092 : 22727 : insertions++;
7093 : 22727 : if (dump_file && (dump_flags & TDF_DETAILS))
7094 : : {
7095 : 501 : fprintf (dump_file, "Inserted ");
7096 : 501 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
7097 : : }
7098 : :
7099 : : return res;
7100 : : }
7101 : :
7102 : : void
7103 : 349047323 : eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
7104 : : {
7105 : 349047323 : tree sprime = NULL_TREE;
7106 : 349047323 : gimple *stmt = gsi_stmt (*gsi);
7107 : 349047323 : tree lhs = gimple_get_lhs (stmt);
7108 : 120174445 : if (lhs && TREE_CODE (lhs) == SSA_NAME
7109 : 165916210 : && !gimple_has_volatile_ops (stmt)
7110 : : /* See PR43491. Do not replace a global register variable when
7111 : : it is a the RHS of an assignment. Do replace local register
7112 : : variables since gcc does not guarantee a local variable will
7113 : : be allocated in register.
7114 : : ??? The fix isn't effective here. This should instead
7115 : : be ensured by not value-numbering them the same but treating
7116 : : them like volatiles? */
7117 : 430945770 : && !(gimple_assign_single_p (stmt)
7118 : 35614923 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
7119 : 2539355 : && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
7120 : 4172 : && is_global_var (gimple_assign_rhs1 (stmt)))))
7121 : : {
7122 : 81898203 : sprime = eliminate_avail (b, lhs);
7123 : 81898203 : if (!sprime)
7124 : : {
7125 : : /* If there is no existing usable leader but SCCVN thinks
7126 : : it has an expression it wants to use as replacement,
7127 : : insert that. */
7128 : 69156456 : tree val = VN_INFO (lhs)->valnum;
7129 : 69156456 : vn_ssa_aux_t vn_info;
7130 : 69156456 : if (val != VN_TOP
7131 : 69156456 : && TREE_CODE (val) == SSA_NAME
7132 : 69156456 : && (vn_info = VN_INFO (val), true)
7133 : 69156456 : && vn_info->needs_insertion
7134 : 326478 : && vn_info->expr != NULL
7135 : 69285419 : && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
7136 : 22727 : eliminate_push_avail (b, sprime);
7137 : : }
7138 : :
7139 : : /* If this now constitutes a copy duplicate points-to
7140 : : and range info appropriately. This is especially
7141 : : important for inserted code. */
7142 : 69156456 : if (sprime
7143 : 12764474 : && TREE_CODE (sprime) == SSA_NAME)
7144 : 8805090 : maybe_duplicate_ssa_info_at_copy (lhs, sprime);
7145 : :
7146 : : /* Inhibit the use of an inserted PHI on a loop header when
7147 : : the address of the memory reference is a simple induction
7148 : : variable. In other cases the vectorizer won't do anything
7149 : : anyway (either it's loop invariant or a complicated
7150 : : expression). */
7151 : 8805090 : if (sprime
7152 : 12764474 : && TREE_CODE (sprime) == SSA_NAME
7153 : 8805090 : && do_pre
7154 : 948957 : && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
7155 : 930671 : && loop_outer (b->loop_father)
7156 : 375905 : && has_zero_uses (sprime)
7157 : 182090 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
7158 : 181935 : && gimple_assign_load_p (stmt))
7159 : : {
7160 : 99756 : gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
7161 : 99756 : basic_block def_bb = gimple_bb (def_stmt);
7162 : 99756 : if (gimple_code (def_stmt) == GIMPLE_PHI
7163 : 99756 : && def_bb->loop_father->header == def_bb)
7164 : : {
7165 : 62499 : loop_p loop = def_bb->loop_father;
7166 : 62499 : ssa_op_iter iter;
7167 : 62499 : tree op;
7168 : 62499 : bool found = false;
7169 : 78434 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
7170 : : {
7171 : 59422 : affine_iv iv;
7172 : 59422 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
7173 : 59422 : if (def_bb
7174 : 54300 : && flow_bb_inside_loop_p (loop, def_bb)
7175 : 108960 : && simple_iv (loop, loop, op, &iv, true))
7176 : : {
7177 : 43487 : found = true;
7178 : 43487 : break;
7179 : : }
7180 : : }
7181 : 19012 : if (found)
7182 : : {
7183 : 43487 : if (dump_file && (dump_flags & TDF_DETAILS))
7184 : : {
7185 : 3 : fprintf (dump_file, "Not replacing ");
7186 : 3 : print_gimple_expr (dump_file, stmt, 0);
7187 : 3 : fprintf (dump_file, " with ");
7188 : 3 : print_generic_expr (dump_file, sprime);
7189 : 3 : fprintf (dump_file, " which would add a loop"
7190 : : " carried dependence to loop %d\n",
7191 : : loop->num);
7192 : : }
7193 : : /* Don't keep sprime available. */
7194 : 43487 : sprime = NULL_TREE;
7195 : : }
7196 : : }
7197 : : }
7198 : :
7199 : 81898203 : if (sprime)
7200 : : {
7201 : : /* If we can propagate the value computed for LHS into
7202 : : all uses don't bother doing anything with this stmt. */
7203 : 12720987 : if (may_propagate_copy (lhs, sprime))
7204 : : {
7205 : : /* Mark it for removal. */
7206 : 12719096 : to_remove.safe_push (stmt);
7207 : :
7208 : : /* ??? Don't count copy/constant propagations. */
7209 : 12719096 : if (gimple_assign_single_p (stmt)
7210 : 12719096 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7211 : 4452821 : || gimple_assign_rhs1 (stmt) == sprime))
7212 : 13491391 : return;
7213 : :
7214 : 7601611 : if (dump_file && (dump_flags & TDF_DETAILS))
7215 : : {
7216 : 17871 : fprintf (dump_file, "Replaced ");
7217 : 17871 : print_gimple_expr (dump_file, stmt, 0);
7218 : 17871 : fprintf (dump_file, " with ");
7219 : 17871 : print_generic_expr (dump_file, sprime);
7220 : 17871 : fprintf (dump_file, " in all uses of ");
7221 : 17871 : print_gimple_stmt (dump_file, stmt, 0);
7222 : : }
7223 : :
7224 : 7601611 : eliminations++;
7225 : 7601611 : return;
7226 : : }
7227 : :
7228 : : /* If this is an assignment from our leader (which
7229 : : happens in the case the value-number is a constant)
7230 : : then there is nothing to do. Likewise if we run into
7231 : : inserted code that needed a conversion because of
7232 : : our type-agnostic value-numbering of loads. */
7233 : 1891 : if ((gimple_assign_single_p (stmt)
7234 : 1 : || (is_gimple_assign (stmt)
7235 : 1 : && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7236 : 0 : || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
7237 : 1892 : && sprime == gimple_assign_rhs1 (stmt))
7238 : : return;
7239 : :
7240 : : /* Else replace its RHS. */
7241 : 718 : if (dump_file && (dump_flags & TDF_DETAILS))
7242 : : {
7243 : 0 : fprintf (dump_file, "Replaced ");
7244 : 0 : print_gimple_expr (dump_file, stmt, 0);
7245 : 0 : fprintf (dump_file, " with ");
7246 : 0 : print_generic_expr (dump_file, sprime);
7247 : 0 : fprintf (dump_file, " in ");
7248 : 0 : print_gimple_stmt (dump_file, stmt, 0);
7249 : : }
7250 : 718 : eliminations++;
7251 : :
7252 : 718 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
7253 : 718 : && stmt_can_make_abnormal_goto (stmt));
7254 : 718 : gimple *orig_stmt = stmt;
7255 : 718 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
7256 : 718 : TREE_TYPE (sprime)))
7257 : : {
7258 : : /* We preserve conversions to but not from function or method
7259 : : types. This asymmetry makes it necessary to re-instantiate
7260 : : conversions here. */
7261 : 716 : if (POINTER_TYPE_P (TREE_TYPE (lhs))
7262 : 716 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7263 : 716 : sprime = fold_convert (TREE_TYPE (lhs), sprime);
7264 : : else
7265 : 0 : gcc_unreachable ();
7266 : : }
7267 : 718 : tree vdef = gimple_vdef (stmt);
7268 : 718 : tree vuse = gimple_vuse (stmt);
7269 : 718 : propagate_tree_value_into_stmt (gsi, sprime);
7270 : 718 : stmt = gsi_stmt (*gsi);
7271 : 718 : update_stmt (stmt);
7272 : : /* In case the VDEF on the original stmt was released, value-number
7273 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7274 : : released virtual operands. */
7275 : 1436 : if (vdef != gimple_vdef (stmt))
7276 : : {
7277 : 0 : gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7278 : 0 : VN_INFO (vdef)->valnum = vuse;
7279 : : }
7280 : :
7281 : : /* If we removed EH side-effects from the statement, clean
7282 : : its EH information. */
7283 : 718 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7284 : : {
7285 : 0 : bitmap_set_bit (need_eh_cleanup,
7286 : 0 : gimple_bb (stmt)->index);
7287 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7288 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7289 : : }
7290 : :
7291 : : /* Likewise for AB side-effects. */
7292 : 718 : if (can_make_abnormal_goto
7293 : 718 : && !stmt_can_make_abnormal_goto (stmt))
7294 : : {
7295 : 0 : bitmap_set_bit (need_ab_cleanup,
7296 : 0 : gimple_bb (stmt)->index);
7297 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7298 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7299 : : }
7300 : :
7301 : 718 : return;
7302 : : }
7303 : : }
7304 : :
7305 : : /* If the statement is a scalar store, see if the expression
7306 : : has the same value number as its rhs. If so, the store is
7307 : : dead. */
7308 : 336326336 : if (gimple_assign_single_p (stmt)
7309 : 127630304 : && !gimple_has_volatile_ops (stmt)
7310 : 55613816 : && !is_gimple_reg (gimple_assign_lhs (stmt))
7311 : 28534714 : && (TREE_CODE (gimple_assign_lhs (stmt)) != VAR_DECL
7312 : 2825117 : || !DECL_HARD_REGISTER (gimple_assign_lhs (stmt)))
7313 : 364857041 : && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7314 : 16364477 : || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7315 : : {
7316 : 25427689 : tree rhs = gimple_assign_rhs1 (stmt);
7317 : 25427689 : vn_reference_t vnresult;
7318 : : /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7319 : : typed load of a byte known to be 0x11 as 1 so a store of
7320 : : a boolean 1 is detected as redundant. Because of this we
7321 : : have to make sure to lookup with a ref where its size
7322 : : matches the precision. */
7323 : 25427689 : tree lookup_lhs = lhs;
7324 : 50571729 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7325 : 13267174 : && (TREE_CODE (lhs) != COMPONENT_REF
7326 : 8101926 : || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7327 : 38415107 : && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7328 : : {
7329 : 435334 : if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
7330 : 444369 : && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7331 : : lookup_lhs = NULL_TREE;
7332 : 428489 : else if (TREE_CODE (lhs) == COMPONENT_REF
7333 : 428489 : || TREE_CODE (lhs) == MEM_REF)
7334 : : {
7335 : 299156 : tree ltype = build_nonstandard_integer_type
7336 : 299156 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7337 : 299156 : TYPE_UNSIGNED (TREE_TYPE (lhs)));
7338 : 299156 : if (TREE_CODE (lhs) == COMPONENT_REF)
7339 : : {
7340 : 225937 : tree foff = component_ref_field_offset (lhs);
7341 : 225937 : tree f = TREE_OPERAND (lhs, 1);
7342 : 225937 : if (!poly_int_tree_p (foff))
7343 : : lookup_lhs = NULL_TREE;
7344 : : else
7345 : 451874 : lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7346 : 225937 : TREE_OPERAND (lhs, 0),
7347 : 225937 : TYPE_SIZE (TREE_TYPE (lhs)),
7348 : : bit_from_pos
7349 : 225937 : (foff, DECL_FIELD_BIT_OFFSET (f)));
7350 : : }
7351 : : else
7352 : 73219 : lookup_lhs = build2 (MEM_REF, ltype,
7353 : 73219 : TREE_OPERAND (lhs, 0),
7354 : 73219 : TREE_OPERAND (lhs, 1));
7355 : : }
7356 : : else
7357 : : lookup_lhs = NULL_TREE;
7358 : : }
7359 : 25291511 : tree val = NULL_TREE, tem;
7360 : 25291511 : if (lookup_lhs)
7361 : 50583022 : val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7362 : : VN_WALKREWRITE, &vnresult, false,
7363 : : NULL, NULL_TREE, true);
7364 : 25427689 : if (TREE_CODE (rhs) == SSA_NAME)
7365 : 12166228 : rhs = VN_INFO (rhs)->valnum;
7366 : 25427689 : gassign *ass;
7367 : 25427689 : if (val
7368 : 25427689 : && (operand_equal_p (val, rhs, 0)
7369 : : /* Due to the bitfield lookups above we can get bit
7370 : : interpretations of the same RHS as values here. Those
7371 : : are redundant as well. */
7372 : 3137236 : || (TREE_CODE (val) == SSA_NAME
7373 : 1929867 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7374 : 1750041 : && (tem = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7375 : 1750041 : && TREE_CODE (tem) == VIEW_CONVERT_EXPR
7376 : 3795 : && TREE_OPERAND (tem, 0) == rhs)
7377 : 3137226 : || (TREE_CODE (rhs) == SSA_NAME
7378 : 25906850 : && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs)))
7379 : 1528093 : && gimple_assign_rhs1 (ass) == val
7380 : 706502 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass))
7381 : 9 : && tree_nop_conversion_p (TREE_TYPE (rhs), TREE_TYPE (val)))))
7382 : : {
7383 : : /* We can only remove the later store if the former aliases
7384 : : at least all accesses the later one does or if the store
7385 : : was to readonly memory storing the same value. */
7386 : 236128 : ao_ref lhs_ref;
7387 : 236128 : ao_ref_init (&lhs_ref, lhs);
7388 : 236128 : alias_set_type set = ao_ref_alias_set (&lhs_ref);
7389 : 236128 : alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7390 : 236128 : if (! vnresult
7391 : 236128 : || ((vnresult->set == set
7392 : 43091 : || alias_set_subset_of (set, vnresult->set))
7393 : 230155 : && (vnresult->base_set == base_set
7394 : 22828 : || alias_set_subset_of (base_set, vnresult->base_set))))
7395 : : {
7396 : 227341 : if (dump_file && (dump_flags & TDF_DETAILS))
7397 : : {
7398 : 17 : fprintf (dump_file, "Deleted redundant store ");
7399 : 17 : print_gimple_stmt (dump_file, stmt, 0);
7400 : : }
7401 : :
7402 : : /* Queue stmt for removal. */
7403 : 227341 : to_remove.safe_push (stmt);
7404 : 227341 : return;
7405 : : }
7406 : : }
7407 : : }
7408 : :
7409 : : /* If this is a control statement value numbering left edges
7410 : : unexecuted on force the condition in a way consistent with
7411 : : that. */
7412 : 336098995 : if (gcond *cond = dyn_cast <gcond *> (stmt))
7413 : : {
7414 : 18950257 : if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7415 : 18950257 : ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7416 : : {
7417 : 543063 : if (dump_file && (dump_flags & TDF_DETAILS))
7418 : : {
7419 : 15 : fprintf (dump_file, "Removing unexecutable edge from ");
7420 : 15 : print_gimple_stmt (dump_file, stmt, 0);
7421 : : }
7422 : 543063 : if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7423 : 543063 : == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7424 : 220705 : gimple_cond_make_true (cond);
7425 : : else
7426 : 322358 : gimple_cond_make_false (cond);
7427 : 543063 : update_stmt (cond);
7428 : 543063 : el_todo |= TODO_cleanup_cfg;
7429 : 543063 : return;
7430 : : }
7431 : : }
7432 : :
7433 : 335555932 : bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7434 : 335555932 : bool was_noreturn = (is_gimple_call (stmt)
7435 : 335555932 : && gimple_call_noreturn_p (stmt));
7436 : 335555932 : tree vdef = gimple_vdef (stmt);
7437 : 335555932 : tree vuse = gimple_vuse (stmt);
7438 : :
7439 : : /* If we didn't replace the whole stmt (or propagate the result
7440 : : into all uses), replace all uses on this stmt with their
7441 : : leaders. */
7442 : 335555932 : bool modified = false;
7443 : 335555932 : use_operand_p use_p;
7444 : 335555932 : ssa_op_iter iter;
7445 : 498493120 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7446 : : {
7447 : 162937188 : tree use = USE_FROM_PTR (use_p);
7448 : : /* ??? The call code above leaves stmt operands un-updated. */
7449 : 162937188 : if (TREE_CODE (use) != SSA_NAME)
7450 : 0 : continue;
7451 : 162937188 : tree sprime;
7452 : 162937188 : if (SSA_NAME_IS_DEFAULT_DEF (use))
7453 : : /* ??? For default defs BB shouldn't matter, but we have to
7454 : : solve the inconsistency between rpo eliminate and
7455 : : dom eliminate avail valueization first. */
7456 : 26351775 : sprime = eliminate_avail (b, use);
7457 : : else
7458 : : /* Look for sth available at the definition block of the argument.
7459 : : This avoids inconsistencies between availability there which
7460 : : decides if the stmt can be removed and availability at the
7461 : : use site. The SSA property ensures that things available
7462 : : at the definition are also available at uses. */
7463 : 136585413 : sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7464 : 162937188 : if (sprime && sprime != use
7465 : 12108972 : && may_propagate_copy (use, sprime, true)
7466 : : /* We substitute into debug stmts to avoid excessive
7467 : : debug temporaries created by removed stmts, but we need
7468 : : to avoid doing so for inserted sprimes as we never want
7469 : : to create debug temporaries for them. */
7470 : 175045444 : && (!inserted_exprs
7471 : 1196000 : || TREE_CODE (sprime) != SSA_NAME
7472 : 1170951 : || !is_gimple_debug (stmt)
7473 : 341630 : || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7474 : : {
7475 : 11799668 : propagate_value (use_p, sprime);
7476 : 11799668 : modified = true;
7477 : : }
7478 : : }
7479 : :
7480 : : /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7481 : : into which is a requirement for the IPA devirt machinery. */
7482 : 335555932 : gimple *old_stmt = stmt;
7483 : 335555932 : if (modified)
7484 : : {
7485 : : /* If a formerly non-invariant ADDR_EXPR is turned into an
7486 : : invariant one it was on a separate stmt. */
7487 : 10943536 : if (gimple_assign_single_p (stmt)
7488 : 10943536 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7489 : 245563 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7490 : 10943536 : gimple_stmt_iterator prev = *gsi;
7491 : 10943536 : gsi_prev (&prev);
7492 : 10943536 : if (fold_stmt (gsi, follow_all_ssa_edges))
7493 : : {
7494 : : /* fold_stmt may have created new stmts inbetween
7495 : : the previous stmt and the folded stmt. Mark
7496 : : all defs created there as varying to not confuse
7497 : : the SCCVN machinery as we're using that even during
7498 : : elimination. */
7499 : 959676 : if (gsi_end_p (prev))
7500 : 220142 : prev = gsi_start_bb (b);
7501 : : else
7502 : 849605 : gsi_next (&prev);
7503 : 959676 : if (gsi_stmt (prev) != gsi_stmt (*gsi))
7504 : 84954 : do
7505 : : {
7506 : 52817 : tree def;
7507 : 52817 : ssa_op_iter dit;
7508 : 102020 : FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7509 : : dit, SSA_OP_ALL_DEFS)
7510 : : /* As existing DEFs may move between stmts
7511 : : only process new ones. */
7512 : 49203 : if (! has_VN_INFO (def))
7513 : : {
7514 : 32035 : vn_ssa_aux_t vn_info = VN_INFO (def);
7515 : 32035 : vn_info->valnum = def;
7516 : 32035 : vn_info->visited = true;
7517 : : }
7518 : 52817 : if (gsi_stmt (prev) == gsi_stmt (*gsi))
7519 : : break;
7520 : 32137 : gsi_next (&prev);
7521 : 32137 : }
7522 : : while (1);
7523 : : }
7524 : 10943536 : stmt = gsi_stmt (*gsi);
7525 : : /* In case we folded the stmt away schedule the NOP for removal. */
7526 : 10943536 : if (gimple_nop_p (stmt))
7527 : 809 : to_remove.safe_push (stmt);
7528 : : }
7529 : :
7530 : : /* Visit indirect calls and turn them into direct calls if
7531 : : possible using the devirtualization machinery. Do this before
7532 : : checking for required EH/abnormal/noreturn cleanup as devird
7533 : : may expose more of those. */
7534 : 335555932 : if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7535 : : {
7536 : 22311301 : tree fn = gimple_call_fn (call_stmt);
7537 : 22311301 : if (fn
7538 : 21472003 : && flag_devirtualize
7539 : 43051611 : && virtual_method_call_p (fn))
7540 : : {
7541 : 214680 : tree otr_type = obj_type_ref_class (fn);
7542 : 214680 : unsigned HOST_WIDE_INT otr_tok
7543 : 214680 : = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7544 : 214680 : tree instance;
7545 : 214680 : ipa_polymorphic_call_context context (current_function_decl,
7546 : 214680 : fn, stmt, &instance);
7547 : 214680 : context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7548 : : otr_type, stmt, NULL);
7549 : 214680 : bool final;
7550 : 214680 : vec <cgraph_node *> targets
7551 : 214680 : = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7552 : : otr_tok, context, &final);
7553 : 214680 : if (dump_file)
7554 : 22 : dump_possible_polymorphic_call_targets (dump_file,
7555 : : obj_type_ref_class (fn),
7556 : : otr_tok, context);
7557 : 214944 : if (final && targets.length () <= 1 && dbg_cnt (devirt))
7558 : : {
7559 : 63 : tree fn;
7560 : 63 : if (targets.length () == 1)
7561 : 63 : fn = targets[0]->decl;
7562 : : else
7563 : 0 : fn = builtin_decl_unreachable ();
7564 : 63 : if (dump_enabled_p ())
7565 : : {
7566 : 9 : dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7567 : : "converting indirect call to "
7568 : : "function %s\n",
7569 : 9 : lang_hooks.decl_printable_name (fn, 2));
7570 : : }
7571 : 63 : gimple_call_set_fndecl (call_stmt, fn);
7572 : : /* If changing the call to __builtin_unreachable
7573 : : or similar noreturn function, adjust gimple_call_fntype
7574 : : too. */
7575 : 63 : if (gimple_call_noreturn_p (call_stmt)
7576 : 0 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7577 : 0 : && TYPE_ARG_TYPES (TREE_TYPE (fn))
7578 : 63 : && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7579 : 0 : == void_type_node))
7580 : 0 : gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7581 : 63 : maybe_remove_unused_call_args (cfun, call_stmt);
7582 : 63 : modified = true;
7583 : : }
7584 : : }
7585 : : }
7586 : :
7587 : 335555932 : if (modified)
7588 : : {
7589 : : /* When changing a call into a noreturn call, cfg cleanup
7590 : : is needed to fix up the noreturn call. */
7591 : 10943557 : if (!was_noreturn
7592 : 10943557 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7593 : 56 : to_fixup.safe_push (stmt);
7594 : : /* When changing a condition or switch into one we know what
7595 : : edge will be executed, schedule a cfg cleanup. */
7596 : 10943557 : if ((gimple_code (stmt) == GIMPLE_COND
7597 : 1475673 : && (gimple_cond_true_p (as_a <gcond *> (stmt))
7598 : 1468894 : || gimple_cond_false_p (as_a <gcond *> (stmt))))
7599 : 12411210 : || (gimple_code (stmt) == GIMPLE_SWITCH
7600 : 8854 : && TREE_CODE (gimple_switch_index
7601 : : (as_a <gswitch *> (stmt))) == INTEGER_CST))
7602 : 9871 : el_todo |= TODO_cleanup_cfg;
7603 : : /* If we removed EH side-effects from the statement, clean
7604 : : its EH information. */
7605 : 10943557 : if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7606 : : {
7607 : 1599 : bitmap_set_bit (need_eh_cleanup,
7608 : 1599 : gimple_bb (stmt)->index);
7609 : 1599 : if (dump_file && (dump_flags & TDF_DETAILS))
7610 : 0 : fprintf (dump_file, " Removed EH side-effects.\n");
7611 : : }
7612 : : /* Likewise for AB side-effects. */
7613 : 10943557 : if (can_make_abnormal_goto
7614 : 10943557 : && !stmt_can_make_abnormal_goto (stmt))
7615 : : {
7616 : 0 : bitmap_set_bit (need_ab_cleanup,
7617 : 0 : gimple_bb (stmt)->index);
7618 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7619 : 0 : fprintf (dump_file, " Removed AB side-effects.\n");
7620 : : }
7621 : 10943557 : update_stmt (stmt);
7622 : : /* In case the VDEF on the original stmt was released, value-number
7623 : : it to the VUSE. This is to make vuse_ssa_val able to skip
7624 : : released virtual operands. */
7625 : 14067531 : if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7626 : 1835 : VN_INFO (vdef)->valnum = vuse;
7627 : : }
7628 : :
7629 : : /* Make new values available - for fully redundant LHS we
7630 : : continue with the next stmt above and skip this.
7631 : : But avoid picking up dead defs. */
7632 : 335555932 : tree def;
7633 : 406043726 : FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7634 : 70487794 : if (! has_zero_uses (def)
7635 : 70487794 : || (inserted_exprs
7636 : 213337 : && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7637 : 69086894 : eliminate_push_avail (b, def);
7638 : : }
7639 : :
7640 : : /* Perform elimination for the basic-block B during the domwalk. */
7641 : :
7642 : : edge
7643 : 41628958 : eliminate_dom_walker::before_dom_children (basic_block b)
7644 : : {
7645 : : /* Mark new bb. */
7646 : 41628958 : avail_stack.safe_push (NULL_TREE);
7647 : :
7648 : : /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7649 : 41628958 : if (!(b->flags & BB_EXECUTABLE))
7650 : : return NULL;
7651 : :
7652 : 36879359 : vn_context_bb = b;
7653 : :
7654 : 48585575 : for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7655 : : {
7656 : 11706216 : gphi *phi = gsi.phi ();
7657 : 11706216 : tree res = PHI_RESULT (phi);
7658 : :
7659 : 23412432 : if (virtual_operand_p (res))
7660 : : {
7661 : 5379408 : gsi_next (&gsi);
7662 : 5379408 : continue;
7663 : : }
7664 : :
7665 : 6326808 : tree sprime = eliminate_avail (b, res);
7666 : 6326808 : if (sprime
7667 : 6326808 : && sprime != res)
7668 : : {
7669 : 429843 : if (dump_file && (dump_flags & TDF_DETAILS))
7670 : : {
7671 : 22 : fprintf (dump_file, "Replaced redundant PHI node defining ");
7672 : 22 : print_generic_expr (dump_file, res);
7673 : 22 : fprintf (dump_file, " with ");
7674 : 22 : print_generic_expr (dump_file, sprime);
7675 : 22 : fprintf (dump_file, "\n");
7676 : : }
7677 : :
7678 : : /* If we inserted this PHI node ourself, it's not an elimination. */
7679 : 429843 : if (! inserted_exprs
7680 : 560690 : || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
7681 : 400391 : eliminations++;
7682 : :
7683 : : /* If we will propagate into all uses don't bother to do
7684 : : anything. */
7685 : 429843 : if (may_propagate_copy (res, sprime))
7686 : : {
7687 : : /* Mark the PHI for removal. */
7688 : 429843 : to_remove.safe_push (phi);
7689 : 429843 : gsi_next (&gsi);
7690 : 429843 : continue;
7691 : : }
7692 : :
7693 : 0 : remove_phi_node (&gsi, false);
7694 : :
7695 : 0 : if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
7696 : 0 : sprime = fold_convert (TREE_TYPE (res), sprime);
7697 : 0 : gimple *stmt = gimple_build_assign (res, sprime);
7698 : 0 : gimple_stmt_iterator gsi2 = gsi_after_labels (b);
7699 : 0 : gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
7700 : 0 : continue;
7701 : 0 : }
7702 : :
7703 : 5896965 : eliminate_push_avail (b, res);
7704 : 5896965 : gsi_next (&gsi);
7705 : : }
7706 : :
7707 : 73758718 : for (gimple_stmt_iterator gsi = gsi_start_bb (b);
7708 : 281344878 : !gsi_end_p (gsi);
7709 : 244465519 : gsi_next (&gsi))
7710 : 244465519 : eliminate_stmt (b, &gsi);
7711 : :
7712 : : /* Replace destination PHI arguments. */
7713 : 36879359 : edge_iterator ei;
7714 : 36879359 : edge e;
7715 : 87121674 : FOR_EACH_EDGE (e, ei, b->succs)
7716 : 50242315 : if (e->flags & EDGE_EXECUTABLE)
7717 : 49762039 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
7718 : 80001334 : !gsi_end_p (gsi);
7719 : 30239295 : gsi_next (&gsi))
7720 : : {
7721 : 30239295 : gphi *phi = gsi.phi ();
7722 : 30239295 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7723 : 30239295 : tree arg = USE_FROM_PTR (use_p);
7724 : 50045833 : if (TREE_CODE (arg) != SSA_NAME
7725 : 30239295 : || virtual_operand_p (arg))
7726 : 19806538 : continue;
7727 : 10432757 : tree sprime = eliminate_avail (b, arg);
7728 : 20865514 : if (sprime && may_propagate_copy (arg, sprime,
7729 : 10432757 : !(e->flags & EDGE_ABNORMAL)))
7730 : 10421790 : propagate_value (use_p, sprime);
7731 : : }
7732 : :
7733 : 36879359 : vn_context_bb = NULL;
7734 : :
7735 : 36879359 : return NULL;
7736 : : }
7737 : :
7738 : : /* Make no longer available leaders no longer available. */
7739 : :
7740 : : void
7741 : 41628958 : eliminate_dom_walker::after_dom_children (basic_block)
7742 : : {
7743 : 41628958 : tree entry;
7744 : 92198734 : while ((entry = avail_stack.pop ()) != NULL_TREE)
7745 : : {
7746 : 50569776 : tree valnum = VN_INFO (entry)->valnum;
7747 : 50569776 : tree old = avail[SSA_NAME_VERSION (valnum)];
7748 : 50569776 : if (old == entry)
7749 : 50526247 : avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
7750 : : else
7751 : 43529 : avail[SSA_NAME_VERSION (valnum)] = entry;
7752 : : }
7753 : 41628958 : }
7754 : :
7755 : : /* Remove queued stmts and perform delayed cleanups. */
7756 : :
7757 : : unsigned
7758 : 6161486 : eliminate_dom_walker::eliminate_cleanup (bool region_p)
7759 : : {
7760 : 6161486 : statistics_counter_event (cfun, "Eliminated", eliminations);
7761 : 6161486 : statistics_counter_event (cfun, "Insertions", insertions);
7762 : :
7763 : : /* We cannot remove stmts during BB walk, especially not release SSA
7764 : : names there as this confuses the VN machinery. The stmts ending
7765 : : up in to_remove are either stores or simple copies.
7766 : : Remove stmts in reverse order to make debug stmt creation possible. */
7767 : 33131143 : while (!to_remove.is_empty ())
7768 : : {
7769 : 14646629 : bool do_release_defs = true;
7770 : 14646629 : gimple *stmt = to_remove.pop ();
7771 : :
7772 : : /* When we are value-numbering a region we do not require exit PHIs to
7773 : : be present so we have to make sure to deal with uses outside of the
7774 : : region of stmts that we thought are eliminated.
7775 : : ??? Note we may be confused by uses in dead regions we didn't run
7776 : : elimination on. Rather than checking individual uses we accept
7777 : : dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7778 : : contains such example). */
7779 : 14646629 : if (region_p)
7780 : : {
7781 : 1650786 : if (gphi *phi = dyn_cast <gphi *> (stmt))
7782 : : {
7783 : 1085474 : tree lhs = gimple_phi_result (phi);
7784 : 1085474 : if (!has_zero_uses (lhs))
7785 : : {
7786 : 20907 : if (dump_file && (dump_flags & TDF_DETAILS))
7787 : 3 : fprintf (dump_file, "Keeping eliminated stmt live "
7788 : : "as copy because of out-of-region uses\n");
7789 : 20907 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7790 : 20907 : gimple *copy = gimple_build_assign (lhs, sprime);
7791 : 20907 : gimple_stmt_iterator gsi
7792 : 20907 : = gsi_after_labels (gimple_bb (stmt));
7793 : 20907 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7794 : 20907 : do_release_defs = false;
7795 : : }
7796 : : }
7797 : 565312 : else if (tree lhs = gimple_get_lhs (stmt))
7798 : 565312 : if (TREE_CODE (lhs) == SSA_NAME
7799 : 565312 : && !has_zero_uses (lhs))
7800 : : {
7801 : 1085 : if (dump_file && (dump_flags & TDF_DETAILS))
7802 : 0 : fprintf (dump_file, "Keeping eliminated stmt live "
7803 : : "as copy because of out-of-region uses\n");
7804 : 1085 : tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7805 : 1085 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7806 : 1085 : if (is_gimple_assign (stmt))
7807 : : {
7808 : 1085 : gimple_assign_set_rhs_from_tree (&gsi, sprime);
7809 : 1085 : stmt = gsi_stmt (gsi);
7810 : 1085 : update_stmt (stmt);
7811 : 1085 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
7812 : 0 : bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
7813 : 1085 : continue;
7814 : : }
7815 : : else
7816 : : {
7817 : 0 : gimple *copy = gimple_build_assign (lhs, sprime);
7818 : 0 : gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7819 : 0 : do_release_defs = false;
7820 : : }
7821 : : }
7822 : : }
7823 : :
7824 : 14645544 : if (dump_file && (dump_flags & TDF_DETAILS))
7825 : : {
7826 : 20824 : fprintf (dump_file, "Removing dead stmt ");
7827 : 20824 : print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
7828 : : }
7829 : :
7830 : 14645544 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7831 : 14645544 : if (gimple_code (stmt) == GIMPLE_PHI)
7832 : 1699383 : remove_phi_node (&gsi, do_release_defs);
7833 : : else
7834 : : {
7835 : 12946161 : basic_block bb = gimple_bb (stmt);
7836 : 12946161 : unlink_stmt_vdef (stmt);
7837 : 12946161 : if (gsi_remove (&gsi, true))
7838 : 26680 : bitmap_set_bit (need_eh_cleanup, bb->index);
7839 : 12946161 : if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
7840 : 2 : bitmap_set_bit (need_ab_cleanup, bb->index);
7841 : 12946161 : if (do_release_defs)
7842 : 12946161 : release_defs (stmt);
7843 : : }
7844 : :
7845 : : /* Removing a stmt may expose a forwarder block. */
7846 : 14645544 : el_todo |= TODO_cleanup_cfg;
7847 : : }
7848 : :
7849 : : /* Fixup stmts that became noreturn calls. This may require splitting
7850 : : blocks and thus isn't possible during the dominator walk. Do this
7851 : : in reverse order so we don't inadvertedly remove a stmt we want to
7852 : : fixup by visiting a dominating now noreturn call first. */
7853 : 6161542 : while (!to_fixup.is_empty ())
7854 : : {
7855 : 56 : gimple *stmt = to_fixup.pop ();
7856 : :
7857 : 56 : if (dump_file && (dump_flags & TDF_DETAILS))
7858 : : {
7859 : 0 : fprintf (dump_file, "Fixing up noreturn call ");
7860 : 0 : print_gimple_stmt (dump_file, stmt, 0);
7861 : : }
7862 : :
7863 : 56 : if (fixup_noreturn_call (stmt))
7864 : 56 : el_todo |= TODO_cleanup_cfg;
7865 : : }
7866 : :
7867 : 6161486 : bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
7868 : 6161486 : bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
7869 : :
7870 : 6161486 : if (do_eh_cleanup)
7871 : 10662 : gimple_purge_all_dead_eh_edges (need_eh_cleanup);
7872 : :
7873 : 6161486 : if (do_ab_cleanup)
7874 : 2 : gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
7875 : :
7876 : 6161486 : if (do_eh_cleanup || do_ab_cleanup)
7877 : 10664 : el_todo |= TODO_cleanup_cfg;
7878 : :
7879 : 6161486 : return el_todo;
7880 : : }
7881 : :
7882 : : /* Eliminate fully redundant computations. */
7883 : :
7884 : : unsigned
7885 : 4309884 : eliminate_with_rpo_vn (bitmap inserted_exprs)
7886 : : {
7887 : 4309884 : eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
7888 : :
7889 : 4309884 : eliminate_dom_walker *saved_rpo_avail = rpo_avail;
7890 : 4309884 : rpo_avail = &walker;
7891 : 4309884 : walker.walk (cfun->cfg->x_entry_block_ptr);
7892 : 4309884 : rpo_avail = saved_rpo_avail;
7893 : :
7894 : 4309884 : return walker.eliminate_cleanup ();
7895 : 4309884 : }
7896 : :
7897 : : static unsigned
7898 : : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
7899 : : bool iterate, bool eliminate, bool skip_entry_phis,
7900 : : vn_lookup_kind kind);
7901 : :
7902 : : void
7903 : 964692 : run_rpo_vn (vn_lookup_kind kind)
7904 : : {
7905 : 964692 : do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
7906 : :
7907 : : /* ??? Prune requirement of these. */
7908 : 964692 : constant_to_value_id = new hash_table<vn_constant_hasher> (23);
7909 : :
7910 : : /* Initialize the value ids and prune out remaining VN_TOPs
7911 : : from dead code. */
7912 : 964692 : tree name;
7913 : 964692 : unsigned i;
7914 : 47240724 : FOR_EACH_SSA_NAME (i, name, cfun)
7915 : : {
7916 : 34117736 : vn_ssa_aux_t info = VN_INFO (name);
7917 : 34117736 : if (!info->visited
7918 : 34024867 : || info->valnum == VN_TOP)
7919 : 92869 : info->valnum = name;
7920 : 34117736 : if (info->valnum == name)
7921 : 32883883 : info->value_id = get_next_value_id ();
7922 : 1233853 : else if (is_gimple_min_invariant (info->valnum))
7923 : 50985 : info->value_id = get_or_alloc_constant_value_id (info->valnum);
7924 : : }
7925 : :
7926 : : /* Propagate. */
7927 : 47240724 : FOR_EACH_SSA_NAME (i, name, cfun)
7928 : : {
7929 : 34117736 : vn_ssa_aux_t info = VN_INFO (name);
7930 : 34117736 : if (TREE_CODE (info->valnum) == SSA_NAME
7931 : 34066751 : && info->valnum != name
7932 : 35300604 : && info->value_id != VN_INFO (info->valnum)->value_id)
7933 : 1182868 : info->value_id = VN_INFO (info->valnum)->value_id;
7934 : : }
7935 : :
7936 : 964692 : set_hashtable_value_ids ();
7937 : :
7938 : 964692 : if (dump_file && (dump_flags & TDF_DETAILS))
7939 : : {
7940 : 14 : fprintf (dump_file, "Value numbers:\n");
7941 : 406 : FOR_EACH_SSA_NAME (i, name, cfun)
7942 : : {
7943 : 307 : if (VN_INFO (name)->visited
7944 : 307 : && SSA_VAL (name) != name)
7945 : : {
7946 : 33 : print_generic_expr (dump_file, name);
7947 : 33 : fprintf (dump_file, " = ");
7948 : 33 : print_generic_expr (dump_file, SSA_VAL (name));
7949 : 33 : fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
7950 : : }
7951 : : }
7952 : : }
7953 : 964692 : }
7954 : :
7955 : : /* Free VN associated data structures. */
7956 : :
7957 : : void
7958 : 6180950 : free_rpo_vn (void)
7959 : : {
7960 : 6180950 : free_vn_table (valid_info);
7961 : 6180950 : XDELETE (valid_info);
7962 : 6180950 : obstack_free (&vn_tables_obstack, NULL);
7963 : 6180950 : obstack_free (&vn_tables_insert_obstack, NULL);
7964 : :
7965 : 6180950 : vn_ssa_aux_iterator_type it;
7966 : 6180950 : vn_ssa_aux_t info;
7967 : 350796234 : FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
7968 : 172307642 : if (info->needs_insertion)
7969 : 4066800 : release_ssa_name (info->name);
7970 : 6180950 : obstack_free (&vn_ssa_aux_obstack, NULL);
7971 : 6180950 : delete vn_ssa_aux_hash;
7972 : :
7973 : 6180950 : delete constant_to_value_id;
7974 : 6180950 : constant_to_value_id = NULL;
7975 : 6180950 : }
7976 : :
7977 : : /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7978 : :
7979 : : static tree
7980 : 23110266 : vn_lookup_simplify_result (gimple_match_op *res_op)
7981 : : {
7982 : 23110266 : if (!res_op->code.is_tree_code ())
7983 : : return NULL_TREE;
7984 : 23106729 : tree *ops = res_op->ops;
7985 : 23106729 : unsigned int length = res_op->num_ops;
7986 : 23106729 : if (res_op->code == CONSTRUCTOR
7987 : : /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7988 : : and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7989 : 23106729 : && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
7990 : : {
7991 : 1219 : length = CONSTRUCTOR_NELTS (res_op->ops[0]);
7992 : 1219 : ops = XALLOCAVEC (tree, length);
7993 : 5897 : for (unsigned i = 0; i < length; ++i)
7994 : 4678 : ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
7995 : : }
7996 : 23106729 : vn_nary_op_t vnresult = NULL;
7997 : 23106729 : tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
7998 : : res_op->type, ops, &vnresult);
7999 : : /* If this is used from expression simplification make sure to
8000 : : return an available expression. */
8001 : 23106729 : if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
8002 : 2165683 : res = rpo_avail->eliminate_avail (vn_context_bb, res);
8003 : : return res;
8004 : : }
8005 : :
8006 : : /* Return a leader for OPs value that is valid at BB. */
8007 : :
8008 : : tree
8009 : 261903440 : rpo_elim::eliminate_avail (basic_block bb, tree op)
8010 : : {
8011 : 261903440 : bool visited;
8012 : 261903440 : tree valnum = SSA_VAL (op, &visited);
8013 : : /* If we didn't visit OP then it must be defined outside of the
8014 : : region we process and also dominate it. So it is available. */
8015 : 261903440 : if (!visited)
8016 : : return op;
8017 : 259766683 : if (TREE_CODE (valnum) == SSA_NAME)
8018 : : {
8019 : 245975953 : if (SSA_NAME_IS_DEFAULT_DEF (valnum))
8020 : : return valnum;
8021 : 239202267 : vn_ssa_aux_t valnum_info = VN_INFO (valnum);
8022 : 239202267 : vn_avail *av = valnum_info->avail;
8023 : 239202267 : if (!av)
8024 : : {
8025 : : /* See above. But when there's availability info prefer
8026 : : what we recorded there for example to preserve LC SSA. */
8027 : 83815327 : if (!valnum_info->visited)
8028 : : return valnum;
8029 : : return NULL_TREE;
8030 : : }
8031 : 155386940 : if (av->location == bb->index)
8032 : : /* On tramp3d 90% of the cases are here. */
8033 : 103438008 : return ssa_name (av->leader);
8034 : 65978564 : do
8035 : : {
8036 : 65978564 : basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
8037 : : /* ??? During elimination we have to use availability at the
8038 : : definition site of a use we try to replace. This
8039 : : is required to not run into inconsistencies because
8040 : : of dominated_by_p_w_unex behavior and removing a definition
8041 : : while not replacing all uses.
8042 : : ??? We could try to consistently walk dominators
8043 : : ignoring non-executable regions. The nearest common
8044 : : dominator of bb and abb is where we can stop walking. We
8045 : : may also be able to "pre-compute" (bits of) the next immediate
8046 : : (non-)dominator during the RPO walk when marking edges as
8047 : : executable. */
8048 : 65978564 : if (dominated_by_p_w_unex (bb, abb, true))
8049 : : {
8050 : 48035215 : tree leader = ssa_name (av->leader);
8051 : : /* Prevent eliminations that break loop-closed SSA. */
8052 : 48035215 : if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
8053 : 3057955 : && ! SSA_NAME_IS_DEFAULT_DEF (leader)
8054 : 51093170 : && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
8055 : 3057955 : (leader))->loop_father,
8056 : : bb))
8057 : : return NULL_TREE;
8058 : 47953907 : if (dump_file && (dump_flags & TDF_DETAILS))
8059 : : {
8060 : 3408 : print_generic_expr (dump_file, leader);
8061 : 3408 : fprintf (dump_file, " is available for ");
8062 : 3408 : print_generic_expr (dump_file, valnum);
8063 : 3408 : fprintf (dump_file, "\n");
8064 : : }
8065 : : /* On tramp3d 99% of the _remaining_ cases succeed at
8066 : : the first enty. */
8067 : 47953907 : return leader;
8068 : : }
8069 : : /* ??? Can we somehow skip to the immediate dominator
8070 : : RPO index (bb_to_rpo)? Again, maybe not worth, on
8071 : : tramp3d the worst number of elements in the vector is 9. */
8072 : 17943349 : av = av->next;
8073 : : }
8074 : 17943349 : while (av);
8075 : : /* While we prefer avail we have to fallback to using the value
8076 : : directly if defined outside of the region when none of the
8077 : : available defs suit. */
8078 : 3913717 : if (!valnum_info->visited)
8079 : : return valnum;
8080 : : }
8081 : 13790730 : else if (valnum != VN_TOP)
8082 : : /* valnum is is_gimple_min_invariant. */
8083 : : return valnum;
8084 : : return NULL_TREE;
8085 : : }
8086 : :
8087 : : /* Make LEADER a leader for its value at BB. */
8088 : :
8089 : : void
8090 : 97484378 : rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
8091 : : {
8092 : 97484378 : tree valnum = VN_INFO (leader)->valnum;
8093 : 97484378 : if (valnum == VN_TOP
8094 : 97484378 : || is_gimple_min_invariant (valnum))
8095 : 0 : return;
8096 : 97484378 : if (dump_file && (dump_flags & TDF_DETAILS))
8097 : : {
8098 : 308222 : fprintf (dump_file, "Making available beyond BB%d ", bb->index);
8099 : 308222 : print_generic_expr (dump_file, leader);
8100 : 308222 : fprintf (dump_file, " for value ");
8101 : 308222 : print_generic_expr (dump_file, valnum);
8102 : 308222 : fprintf (dump_file, "\n");
8103 : : }
8104 : 97484378 : vn_ssa_aux_t value = VN_INFO (valnum);
8105 : 97484378 : vn_avail *av;
8106 : 97484378 : if (m_avail_freelist)
8107 : : {
8108 : 18928014 : av = m_avail_freelist;
8109 : 18928014 : m_avail_freelist = m_avail_freelist->next;
8110 : : }
8111 : : else
8112 : 78556364 : av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
8113 : 97484378 : av->location = bb->index;
8114 : 97484378 : av->leader = SSA_NAME_VERSION (leader);
8115 : 97484378 : av->next = value->avail;
8116 : 97484378 : av->next_undo = last_pushed_avail;
8117 : 97484378 : last_pushed_avail = value;
8118 : 97484378 : value->avail = av;
8119 : : }
8120 : :
8121 : : /* Valueization hook for RPO VN plus required state. */
8122 : :
8123 : : tree
8124 : 1979661058 : rpo_vn_valueize (tree name)
8125 : : {
8126 : 1979661058 : if (TREE_CODE (name) == SSA_NAME)
8127 : : {
8128 : 1933561217 : vn_ssa_aux_t val = VN_INFO (name);
8129 : 1933561217 : if (val)
8130 : : {
8131 : 1933561217 : tree tem = val->valnum;
8132 : 1933561217 : if (tem != VN_TOP && tem != name)
8133 : : {
8134 : 103274356 : if (TREE_CODE (tem) != SSA_NAME)
8135 : : return tem;
8136 : : /* For all values we only valueize to an available leader
8137 : : which means we can use SSA name info without restriction. */
8138 : 86653386 : tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
8139 : 86653386 : if (tem)
8140 : : return tem;
8141 : : }
8142 : : }
8143 : : }
8144 : : return name;
8145 : : }
8146 : :
8147 : : /* Insert on PRED_E predicates derived from CODE OPS being true besides the
8148 : : inverted condition. */
8149 : :
8150 : : static void
8151 : 27579003 : insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
8152 : : {
8153 : 27579003 : switch (code)
8154 : : {
8155 : 1352654 : case LT_EXPR:
8156 : : /* a < b -> a {!,<}= b */
8157 : 1352654 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8158 : : ops, boolean_true_node, 0, pred_e);
8159 : 1352654 : vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
8160 : : ops, boolean_true_node, 0, pred_e);
8161 : : /* a < b -> ! a {>,=} b */
8162 : 1352654 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8163 : : ops, boolean_false_node, 0, pred_e);
8164 : 1352654 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8165 : : ops, boolean_false_node, 0, pred_e);
8166 : 1352654 : break;
8167 : 3447844 : case GT_EXPR:
8168 : : /* a > b -> a {!,>}= b */
8169 : 3447844 : vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8170 : : ops, boolean_true_node, 0, pred_e);
8171 : 3447844 : vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
8172 : : ops, boolean_true_node, 0, pred_e);
8173 : : /* a > b -> ! a {<,=} b */
8174 : 3447844 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8175 : : ops, boolean_false_node, 0, pred_e);
8176 : 3447844 : vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8177 : : ops, boolean_false_node, 0, pred_e);
8178 : 3447844 : break;
8179 : 9454438 : case EQ_EXPR:
8180 : : /* a == b -> ! a {<,>} b */
8181 : 9454438 : vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8182 : : ops, boolean_false_node, 0, pred_e);
8183 : 9454438 : vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8184 : : ops, boolean_false_node, 0, pred_e);
8185 : 9454438 : break;
8186 : : case LE_EXPR:
8187 : : case GE_EXPR:
8188 : : case NE_EXPR:
8189 : : /* Nothing besides inverted condition. */
8190 : : break;
8191 : 27579003 : default:;
8192 : : }
8193 : 27579003 : }
8194 : :
8195 : : /* Insert on the TRUE_E true and FALSE_E false predicates
8196 : : derived from LHS CODE RHS. */
8197 : :
8198 : : static void
8199 : 23474184 : insert_predicates_for_cond (tree_code code, tree lhs, tree rhs,
8200 : : edge true_e, edge false_e)
8201 : : {
8202 : : /* If both edges are null, then there is nothing to be done. */
8203 : 23474184 : if (!true_e && !false_e)
8204 : 1311313 : return;
8205 : :
8206 : : /* Canonicalize the comparison if needed, putting
8207 : : the constant in the rhs. */
8208 : 22166286 : if (tree_swap_operands_p (lhs, rhs))
8209 : : {
8210 : 16895 : std::swap (lhs, rhs);
8211 : 16895 : code = swap_tree_comparison (code);
8212 : : }
8213 : :
8214 : : /* If the lhs is not a ssa name, don't record anything. */
8215 : 22166286 : if (TREE_CODE (lhs) != SSA_NAME)
8216 : : return;
8217 : :
8218 : 22162871 : tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs));
8219 : 22162871 : tree ops[2];
8220 : 22162871 : ops[0] = lhs;
8221 : 22162871 : ops[1] = rhs;
8222 : 22162871 : if (true_e)
8223 : 18090316 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8224 : : boolean_true_node, 0, true_e);
8225 : 22162871 : if (false_e)
8226 : 17044616 : vn_nary_op_insert_pieces_predicated (2, code, boolean_type_node, ops,
8227 : : boolean_false_node, 0, false_e);
8228 : 22162871 : if (icode != ERROR_MARK)
8229 : : {
8230 : 21909520 : if (true_e)
8231 : 17938180 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8232 : : boolean_false_node, 0, true_e);
8233 : 21909520 : if (false_e)
8234 : 16836531 : vn_nary_op_insert_pieces_predicated (2, icode, boolean_type_node, ops,
8235 : : boolean_true_node, 0, false_e);
8236 : : }
8237 : : /* Relax for non-integers, inverted condition handled
8238 : : above. */
8239 : 22162871 : if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8240 : : {
8241 : 17373626 : if (true_e)
8242 : 14259232 : insert_related_predicates_on_edge (code, ops, true_e);
8243 : 17373626 : if (false_e)
8244 : 13319771 : insert_related_predicates_on_edge (icode, ops, false_e);
8245 : : }
8246 : 22162871 : if (integer_zerop (rhs)
8247 : 22162871 : && (code == NE_EXPR || code == EQ_EXPR))
8248 : : {
8249 : 9227829 : gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
8250 : : /* (A CMP B) != 0 is the same as (A CMP B).
8251 : : (A CMP B) == 0 is just (A CMP B) with the edges swapped. */
8252 : 9227829 : if (is_gimple_assign (def_stmt)
8253 : 9227829 : && TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_comparison)
8254 : : {
8255 : 453649 : tree_code nc = gimple_assign_rhs_code (def_stmt);
8256 : 453649 : tree nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8257 : 453649 : tree nrhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8258 : 453649 : edge nt = true_e;
8259 : 453649 : edge nf = false_e;
8260 : 453649 : if (code == EQ_EXPR)
8261 : 324636 : std::swap (nt, nf);
8262 : 453649 : if (lhs != nlhs)
8263 : 453649 : insert_predicates_for_cond (nc, nlhs, nrhs, nt, nf);
8264 : : }
8265 : : /* (a | b) == 0 ->
8266 : : on true edge assert: a == 0 & b == 0. */
8267 : : /* (a | b) != 0 ->
8268 : : on false edge assert: a == 0 & b == 0. */
8269 : 9227829 : if (is_gimple_assign (def_stmt)
8270 : 9227829 : && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR)
8271 : : {
8272 : 267074 : edge e = code == EQ_EXPR ? true_e : false_e;
8273 : 267074 : tree nlhs;
8274 : :
8275 : 267074 : nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt));
8276 : : /* A valueization of the `a` might return the old lhs
8277 : : which is already handled above. */
8278 : 267074 : if (nlhs != lhs)
8279 : 267074 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8280 : :
8281 : : /* A valueization of the `b` might return the old lhs
8282 : : which is already handled above. */
8283 : 267074 : nlhs = vn_valueize (gimple_assign_rhs2 (def_stmt));
8284 : 267074 : if (nlhs != lhs)
8285 : 267074 : insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr);
8286 : : }
8287 : : }
8288 : : }
8289 : :
8290 : : /* Main stmt worker for RPO VN, process BB. */
8291 : :
8292 : : static unsigned
8293 : 62164115 : process_bb (rpo_elim &avail, basic_block bb,
8294 : : bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
8295 : : bool do_region, bitmap exit_bbs, bool skip_phis)
8296 : : {
8297 : 62164115 : unsigned todo = 0;
8298 : 62164115 : edge_iterator ei;
8299 : 62164115 : edge e;
8300 : :
8301 : 62164115 : vn_context_bb = bb;
8302 : :
8303 : : /* If we are in loop-closed SSA preserve this state. This is
8304 : : relevant when called on regions from outside of FRE/PRE. */
8305 : 62164115 : bool lc_phi_nodes = false;
8306 : 62164115 : if (!skip_phis
8307 : 62164115 : && loops_state_satisfies_p (LOOP_CLOSED_SSA))
8308 : 3501515 : FOR_EACH_EDGE (e, ei, bb->preds)
8309 : 2117596 : if (e->src->loop_father != e->dest->loop_father
8310 : 2117596 : && flow_loop_nested_p (e->dest->loop_father,
8311 : : e->src->loop_father))
8312 : : {
8313 : : lc_phi_nodes = true;
8314 : : break;
8315 : : }
8316 : :
8317 : : /* When we visit a loop header substitute into loop info. */
8318 : 62164115 : if (!iterate && eliminate && bb->loop_father->header == bb)
8319 : : {
8320 : : /* Keep fields in sync with substitute_in_loop_info. */
8321 : 948565 : if (bb->loop_father->nb_iterations)
8322 : 152658 : bb->loop_father->nb_iterations
8323 : 152658 : = simplify_replace_tree (bb->loop_father->nb_iterations,
8324 : : NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
8325 : : }
8326 : :
8327 : : /* Value-number all defs in the basic-block. */
8328 : 62164115 : if (!skip_phis)
8329 : 89230872 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8330 : 27093438 : gsi_next (&gsi))
8331 : : {
8332 : 27093438 : gphi *phi = gsi.phi ();
8333 : 27093438 : tree res = PHI_RESULT (phi);
8334 : 27093438 : vn_ssa_aux_t res_info = VN_INFO (res);
8335 : 27093438 : if (!bb_visited)
8336 : : {
8337 : 19131850 : gcc_assert (!res_info->visited);
8338 : 19131850 : res_info->valnum = VN_TOP;
8339 : 19131850 : res_info->visited = true;
8340 : : }
8341 : :
8342 : : /* When not iterating force backedge values to varying. */
8343 : 27093438 : visit_stmt (phi, !iterate_phis);
8344 : 54186876 : if (virtual_operand_p (res))
8345 : 10707052 : continue;
8346 : :
8347 : : /* Eliminate */
8348 : : /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
8349 : : how we handle backedges and availability.
8350 : : And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
8351 : 16386386 : tree val = res_info->valnum;
8352 : 16386386 : if (res != val && !iterate && eliminate)
8353 : : {
8354 : 1393144 : if (tree leader = avail.eliminate_avail (bb, res))
8355 : : {
8356 : 1270382 : if (leader != res
8357 : : /* Preserve loop-closed SSA form. */
8358 : 1270382 : && (! lc_phi_nodes
8359 : 6788 : || is_gimple_min_invariant (leader)))
8360 : : {
8361 : 1269540 : if (dump_file && (dump_flags & TDF_DETAILS))
8362 : : {
8363 : 195 : fprintf (dump_file, "Replaced redundant PHI node "
8364 : : "defining ");
8365 : 195 : print_generic_expr (dump_file, res);
8366 : 195 : fprintf (dump_file, " with ");
8367 : 195 : print_generic_expr (dump_file, leader);
8368 : 195 : fprintf (dump_file, "\n");
8369 : : }
8370 : 1269540 : avail.eliminations++;
8371 : :
8372 : 1269540 : if (may_propagate_copy (res, leader))
8373 : : {
8374 : : /* Schedule for removal. */
8375 : 1269540 : avail.to_remove.safe_push (phi);
8376 : 1269540 : continue;
8377 : : }
8378 : : /* ??? Else generate a copy stmt. */
8379 : : }
8380 : : }
8381 : : }
8382 : : /* Only make defs available that not already are. But make
8383 : : sure loop-closed SSA PHI node defs are picked up for
8384 : : downstream uses. */
8385 : 15116846 : if (lc_phi_nodes
8386 : 15116846 : || res == val
8387 : 15116846 : || ! avail.eliminate_avail (bb, res))
8388 : 11556577 : avail.eliminate_push_avail (bb, res);
8389 : : }
8390 : :
8391 : : /* For empty BBs mark outgoing edges executable. For non-empty BBs
8392 : : we do this when processing the last stmt as we have to do this
8393 : : before elimination which otherwise forces GIMPLE_CONDs to
8394 : : if (1 != 0) style when seeing non-executable edges. */
8395 : 124328230 : if (gsi_end_p (gsi_start_bb (bb)))
8396 : : {
8397 : 14222884 : FOR_EACH_EDGE (e, ei, bb->succs)
8398 : : {
8399 : 7111442 : if (!(e->flags & EDGE_EXECUTABLE))
8400 : : {
8401 : 4841478 : if (dump_file && (dump_flags & TDF_DETAILS))
8402 : 5859 : fprintf (dump_file,
8403 : : "marking outgoing edge %d -> %d executable\n",
8404 : 5859 : e->src->index, e->dest->index);
8405 : 4841478 : e->flags |= EDGE_EXECUTABLE;
8406 : 4841478 : e->dest->flags |= BB_EXECUTABLE;
8407 : : }
8408 : 2269964 : else if (!(e->dest->flags & BB_EXECUTABLE))
8409 : : {
8410 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8411 : 0 : fprintf (dump_file,
8412 : : "marking destination block %d reachable\n",
8413 : : e->dest->index);
8414 : 0 : e->dest->flags |= BB_EXECUTABLE;
8415 : : }
8416 : : }
8417 : : }
8418 : 124328230 : for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8419 : 486143341 : !gsi_end_p (gsi); gsi_next (&gsi))
8420 : : {
8421 : 423979226 : ssa_op_iter i;
8422 : 423979226 : tree op;
8423 : 423979226 : if (!bb_visited)
8424 : : {
8425 : 486251789 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8426 : : {
8427 : 137554211 : vn_ssa_aux_t op_info = VN_INFO (op);
8428 : 137554211 : gcc_assert (!op_info->visited);
8429 : 137554211 : op_info->valnum = VN_TOP;
8430 : 137554211 : op_info->visited = true;
8431 : : }
8432 : :
8433 : : /* We somehow have to deal with uses that are not defined
8434 : : in the processed region. Forcing unvisited uses to
8435 : : varying here doesn't play well with def-use following during
8436 : : expression simplification, so we deal with this by checking
8437 : : the visited flag in SSA_VAL. */
8438 : : }
8439 : :
8440 : 423979226 : visit_stmt (gsi_stmt (gsi));
8441 : :
8442 : 423979226 : gimple *last = gsi_stmt (gsi);
8443 : 423979226 : e = NULL;
8444 : 423979226 : switch (gimple_code (last))
8445 : : {
8446 : 123696 : case GIMPLE_SWITCH:
8447 : 123696 : e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8448 : 123696 : (as_a <gswitch *> (last))));
8449 : 123696 : break;
8450 : 24618084 : case GIMPLE_COND:
8451 : 24618084 : {
8452 : 24618084 : tree lhs = vn_valueize (gimple_cond_lhs (last));
8453 : 24618084 : tree rhs = vn_valueize (gimple_cond_rhs (last));
8454 : 24618084 : tree_code cmpcode = gimple_cond_code (last);
8455 : : /* Canonicalize the comparison if needed, putting
8456 : : the constant in the rhs. */
8457 : 24618084 : if (tree_swap_operands_p (lhs, rhs))
8458 : : {
8459 : 841260 : std::swap (lhs, rhs);
8460 : 841260 : cmpcode = swap_tree_comparison (cmpcode);
8461 : : }
8462 : 24618084 : tree val = gimple_simplify (cmpcode,
8463 : : boolean_type_node, lhs, rhs,
8464 : : NULL, vn_valueize);
8465 : : /* If the condition didn't simplfy see if we have recorded
8466 : : an expression from sofar taken edges. */
8467 : 24618084 : if (! val || TREE_CODE (val) != INTEGER_CST)
8468 : : {
8469 : 22804444 : vn_nary_op_t vnresult;
8470 : 22804444 : tree ops[2];
8471 : 22804444 : ops[0] = lhs;
8472 : 22804444 : ops[1] = rhs;
8473 : 22804444 : val = vn_nary_op_lookup_pieces (2, cmpcode,
8474 : : boolean_type_node, ops,
8475 : : &vnresult);
8476 : : /* Got back a ssa name, then try looking up `val != 0`
8477 : : as it might have been recorded that way. */
8478 : 22804444 : if (val && TREE_CODE (val) == SSA_NAME)
8479 : : {
8480 : 143989 : ops[0] = val;
8481 : 143989 : ops[1] = build_zero_cst (TREE_TYPE (val));
8482 : 143989 : val = vn_nary_op_lookup_pieces (2, NE_EXPR,
8483 : : boolean_type_node, ops,
8484 : : &vnresult);
8485 : : }
8486 : : /* Did we get a predicated value? */
8487 : 22804428 : if (! val && vnresult && vnresult->predicated_values)
8488 : : {
8489 : 1266031 : val = vn_nary_op_get_predicated_value (vnresult, bb);
8490 : 1266031 : if (val && dump_file && (dump_flags & TDF_DETAILS))
8491 : : {
8492 : 2 : fprintf (dump_file, "Got predicated value ");
8493 : 2 : print_generic_expr (dump_file, val, TDF_NONE);
8494 : 2 : fprintf (dump_file, " for ");
8495 : 2 : print_gimple_stmt (dump_file, last, TDF_SLIM);
8496 : : }
8497 : : }
8498 : : }
8499 : 22804444 : if (val)
8500 : 2131697 : e = find_taken_edge (bb, val);
8501 : 24618084 : if (! e)
8502 : : {
8503 : : /* If we didn't manage to compute the taken edge then
8504 : : push predicated expressions for the condition itself
8505 : : and related conditions to the hashtables. This allows
8506 : : simplification of redundant conditions which is
8507 : : important as early cleanup. */
8508 : 22486387 : edge true_e, false_e;
8509 : 22486387 : extract_true_false_edges_from_block (bb, &true_e, &false_e);
8510 : 523050 : if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8511 : 22704559 : || !can_track_predicate_on_edge (true_e))
8512 : 4931309 : true_e = NULL;
8513 : 523050 : if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8514 : 22676437 : || !can_track_predicate_on_edge (false_e))
8515 : 5856692 : false_e = NULL;
8516 : 22486387 : insert_predicates_for_cond (cmpcode, lhs, rhs, true_e, false_e);
8517 : : }
8518 : : break;
8519 : : }
8520 : 1367 : case GIMPLE_GOTO:
8521 : 1367 : e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8522 : 1367 : break;
8523 : : default:
8524 : : e = NULL;
8525 : : }
8526 : 423979226 : if (e)
8527 : : {
8528 : 2135436 : todo = TODO_cleanup_cfg;
8529 : 2135436 : if (!(e->flags & EDGE_EXECUTABLE))
8530 : : {
8531 : 1681532 : if (dump_file && (dump_flags & TDF_DETAILS))
8532 : 35 : fprintf (dump_file,
8533 : : "marking known outgoing %sedge %d -> %d executable\n",
8534 : 35 : e->flags & EDGE_DFS_BACK ? "back-" : "",
8535 : 35 : e->src->index, e->dest->index);
8536 : 1681532 : e->flags |= EDGE_EXECUTABLE;
8537 : 1681532 : e->dest->flags |= BB_EXECUTABLE;
8538 : : }
8539 : 453904 : else if (!(e->dest->flags & BB_EXECUTABLE))
8540 : : {
8541 : 26966 : if (dump_file && (dump_flags & TDF_DETAILS))
8542 : 1 : fprintf (dump_file,
8543 : : "marking destination block %d reachable\n",
8544 : : e->dest->index);
8545 : 26966 : e->dest->flags |= BB_EXECUTABLE;
8546 : : }
8547 : : }
8548 : 843687580 : else if (gsi_one_before_end_p (gsi))
8549 : : {
8550 : 130063335 : FOR_EACH_EDGE (e, ei, bb->succs)
8551 : : {
8552 : 77146098 : if (!(e->flags & EDGE_EXECUTABLE))
8553 : : {
8554 : 56634116 : if (dump_file && (dump_flags & TDF_DETAILS))
8555 : 17419 : fprintf (dump_file,
8556 : : "marking outgoing edge %d -> %d executable\n",
8557 : 17419 : e->src->index, e->dest->index);
8558 : 56634116 : e->flags |= EDGE_EXECUTABLE;
8559 : 56634116 : e->dest->flags |= BB_EXECUTABLE;
8560 : : }
8561 : 20511982 : else if (!(e->dest->flags & BB_EXECUTABLE))
8562 : : {
8563 : 2573010 : if (dump_file && (dump_flags & TDF_DETAILS))
8564 : 5811 : fprintf (dump_file,
8565 : : "marking destination block %d reachable\n",
8566 : : e->dest->index);
8567 : 2573010 : e->dest->flags |= BB_EXECUTABLE;
8568 : : }
8569 : : }
8570 : : }
8571 : :
8572 : : /* Eliminate. That also pushes to avail. */
8573 : 423979226 : if (eliminate && ! iterate)
8574 : 104581804 : avail.eliminate_stmt (bb, &gsi);
8575 : : else
8576 : : /* If not eliminating, make all not already available defs
8577 : : available. But avoid picking up dead defs. */
8578 : 399813581 : FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8579 : 80416159 : if (! has_zero_uses (op)
8580 : 80416159 : && ! avail.eliminate_avail (bb, op))
8581 : 61490991 : avail.eliminate_push_avail (bb, op);
8582 : : }
8583 : :
8584 : : /* Eliminate in destination PHI arguments. Always substitute in dest
8585 : : PHIs, even for non-executable edges. This handles region
8586 : : exits PHIs. */
8587 : 62164115 : if (!iterate && eliminate)
8588 : 33337461 : FOR_EACH_EDGE (e, ei, bb->succs)
8589 : 19860128 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
8590 : 38302258 : !gsi_end_p (gsi); gsi_next (&gsi))
8591 : : {
8592 : 18442130 : gphi *phi = gsi.phi ();
8593 : 18442130 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8594 : 18442130 : tree arg = USE_FROM_PTR (use_p);
8595 : 28073228 : if (TREE_CODE (arg) != SSA_NAME
8596 : 18442130 : || virtual_operand_p (arg))
8597 : 9631098 : continue;
8598 : 8811032 : tree sprime;
8599 : 8811032 : if (SSA_NAME_IS_DEFAULT_DEF (arg))
8600 : : {
8601 : 121591 : sprime = SSA_VAL (arg);
8602 : 121591 : gcc_assert (TREE_CODE (sprime) != SSA_NAME
8603 : : || SSA_NAME_IS_DEFAULT_DEF (sprime));
8604 : : }
8605 : : else
8606 : : /* Look for sth available at the definition block of the argument.
8607 : : This avoids inconsistencies between availability there which
8608 : : decides if the stmt can be removed and availability at the
8609 : : use site. The SSA property ensures that things available
8610 : : at the definition are also available at uses. */
8611 : 8689441 : sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8612 : : arg);
8613 : 8811032 : if (sprime
8614 : 8811032 : && sprime != arg
8615 : 8811032 : && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8616 : 1508899 : propagate_value (use_p, sprime);
8617 : : }
8618 : :
8619 : 62164115 : vn_context_bb = NULL;
8620 : 62164115 : return todo;
8621 : : }
8622 : :
8623 : : /* Unwind state per basic-block. */
8624 : :
8625 : : struct unwind_state
8626 : : {
8627 : : /* Times this block has been visited. */
8628 : : unsigned visited;
8629 : : /* Whether to handle this as iteration point or whether to treat
8630 : : incoming backedge PHI values as varying. */
8631 : : bool iterate;
8632 : : /* Maximum RPO index this block is reachable from. */
8633 : : int max_rpo;
8634 : : /* Unwind state. */
8635 : : void *ob_top;
8636 : : vn_reference_t ref_top;
8637 : : vn_phi_t phi_top;
8638 : : vn_nary_op_t nary_top;
8639 : : vn_avail *avail_top;
8640 : : };
8641 : :
8642 : : /* Unwind the RPO VN state for iteration. */
8643 : :
8644 : : static void
8645 : 1920553 : do_unwind (unwind_state *to, rpo_elim &avail)
8646 : : {
8647 : 1920553 : gcc_assert (to->iterate);
8648 : 35146198 : for (; last_inserted_nary != to->nary_top;
8649 : 33225645 : last_inserted_nary = last_inserted_nary->next)
8650 : : {
8651 : 33225645 : vn_nary_op_t *slot;
8652 : 33225645 : slot = valid_info->nary->find_slot_with_hash
8653 : 33225645 : (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
8654 : : /* Predication causes the need to restore previous state. */
8655 : 33225645 : if ((*slot)->unwind_to)
8656 : 6653582 : *slot = (*slot)->unwind_to;
8657 : : else
8658 : 26572063 : valid_info->nary->clear_slot (slot);
8659 : : }
8660 : 7515066 : for (; last_inserted_phi != to->phi_top;
8661 : 5594513 : last_inserted_phi = last_inserted_phi->next)
8662 : : {
8663 : 5594513 : vn_phi_t *slot;
8664 : 5594513 : slot = valid_info->phis->find_slot_with_hash
8665 : 5594513 : (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
8666 : 5594513 : valid_info->phis->clear_slot (slot);
8667 : : }
8668 : 15293194 : for (; last_inserted_ref != to->ref_top;
8669 : 13372641 : last_inserted_ref = last_inserted_ref->next)
8670 : : {
8671 : 13372641 : vn_reference_t *slot;
8672 : 13372641 : slot = valid_info->references->find_slot_with_hash
8673 : 13372641 : (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
8674 : 13372641 : (*slot)->operands.release ();
8675 : 13372641 : valid_info->references->clear_slot (slot);
8676 : : }
8677 : 1920553 : obstack_free (&vn_tables_obstack, to->ob_top);
8678 : :
8679 : : /* Prune [rpo_idx, ] from avail. */
8680 : 20848567 : for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
8681 : : {
8682 : 18928014 : vn_ssa_aux_t val = last_pushed_avail;
8683 : 18928014 : vn_avail *av = val->avail;
8684 : 18928014 : val->avail = av->next;
8685 : 18928014 : last_pushed_avail = av->next_undo;
8686 : 18928014 : av->next = avail.m_avail_freelist;
8687 : 18928014 : avail.m_avail_freelist = av;
8688 : : }
8689 : 1920553 : }
8690 : :
8691 : : /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8692 : : If ITERATE is true then treat backedges optimistically as not
8693 : : executed and iterate. If ELIMINATE is true then perform
8694 : : elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8695 : : is true then force PHI nodes in ENTRY->dest to VARYING. */
8696 : :
8697 : : static unsigned
8698 : 6180950 : do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8699 : : bool iterate, bool eliminate, bool skip_entry_phis,
8700 : : vn_lookup_kind kind)
8701 : : {
8702 : 6180950 : unsigned todo = 0;
8703 : 6180950 : default_vn_walk_kind = kind;
8704 : :
8705 : : /* We currently do not support region-based iteration when
8706 : : elimination is requested. */
8707 : 6180950 : gcc_assert (!entry || !iterate || !eliminate);
8708 : : /* When iterating we need loop info up-to-date. */
8709 : 6180950 : gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
8710 : :
8711 : 6180950 : bool do_region = entry != NULL;
8712 : 6180950 : if (!do_region)
8713 : : {
8714 : 5506526 : entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
8715 : 5506526 : exit_bbs = BITMAP_ALLOC (NULL);
8716 : 5506526 : bitmap_set_bit (exit_bbs, EXIT_BLOCK);
8717 : : }
8718 : :
8719 : : /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8720 : : re-mark those that are contained in the region. */
8721 : 6180950 : edge_iterator ei;
8722 : 6180950 : edge e;
8723 : 12417612 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8724 : 6236662 : e->flags &= ~EDGE_DFS_BACK;
8725 : :
8726 : 6180950 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
8727 : 6180950 : auto_vec<std::pair<int, int> > toplevel_scc_extents;
8728 : 6180950 : int n = rev_post_order_and_mark_dfs_back_seme
8729 : 8052016 : (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
8730 : :
8731 : 6180950 : if (!do_region)
8732 : 5506526 : BITMAP_FREE (exit_bbs);
8733 : :
8734 : : /* If there are any non-DFS_BACK edges into entry->dest skip
8735 : : processing PHI nodes for that block. This supports
8736 : : value-numbering loop bodies w/o the actual loop. */
8737 : 12417611 : FOR_EACH_EDGE (e, ei, entry->dest->preds)
8738 : 6236662 : if (e != entry
8739 : 55712 : && !(e->flags & EDGE_DFS_BACK))
8740 : : break;
8741 : 6180950 : if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
8742 : 0 : fprintf (dump_file, "Region does not contain all edges into "
8743 : : "the entry block, skipping its PHIs.\n");
8744 : 6180950 : skip_entry_phis |= e != NULL;
8745 : :
8746 : 6180950 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
8747 : 57135581 : for (int i = 0; i < n; ++i)
8748 : 50954631 : bb_to_rpo[rpo[i]] = i;
8749 : :
8750 : 6180950 : unwind_state *rpo_state = XNEWVEC (unwind_state, n);
8751 : :
8752 : 6180950 : rpo_elim avail (entry->dest);
8753 : 6180950 : rpo_avail = &avail;
8754 : :
8755 : : /* Verify we have no extra entries into the region. */
8756 : 6180950 : if (flag_checking && do_region)
8757 : : {
8758 : 674418 : auto_bb_flag bb_in_region (fn);
8759 : 2015721 : for (int i = 0; i < n; ++i)
8760 : : {
8761 : 1341303 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8762 : 1341303 : bb->flags |= bb_in_region;
8763 : : }
8764 : : /* We can't merge the first two loops because we cannot rely
8765 : : on EDGE_DFS_BACK for edges not within the region. But if
8766 : : we decide to always have the bb_in_region flag we can
8767 : : do the checking during the RPO walk itself (but then it's
8768 : : also easy to handle MEME conservatively). */
8769 : 2015721 : for (int i = 0; i < n; ++i)
8770 : : {
8771 : 1341303 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8772 : 1341303 : edge e;
8773 : 1341303 : edge_iterator ei;
8774 : 2930708 : FOR_EACH_EDGE (e, ei, bb->preds)
8775 : 1589405 : gcc_assert (e == entry
8776 : : || (skip_entry_phis && bb == entry->dest)
8777 : : || (e->src->flags & bb_in_region));
8778 : : }
8779 : 2015721 : for (int i = 0; i < n; ++i)
8780 : : {
8781 : 1341303 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8782 : 1341303 : bb->flags &= ~bb_in_region;
8783 : : }
8784 : 674418 : }
8785 : :
8786 : : /* Create the VN state. For the initial size of the various hashtables
8787 : : use a heuristic based on region size and number of SSA names. */
8788 : 6180950 : unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
8789 : 6180950 : / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
8790 : 6180950 : VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
8791 : 6180950 : next_value_id = 1;
8792 : 6180950 : next_constant_value_id = -1;
8793 : :
8794 : 6180950 : vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
8795 : 6180950 : gcc_obstack_init (&vn_ssa_aux_obstack);
8796 : :
8797 : 6180950 : gcc_obstack_init (&vn_tables_obstack);
8798 : 6180950 : gcc_obstack_init (&vn_tables_insert_obstack);
8799 : 6180950 : valid_info = XCNEW (struct vn_tables_s);
8800 : 6180950 : allocate_vn_table (valid_info, region_size);
8801 : 6180950 : last_inserted_ref = NULL;
8802 : 6180950 : last_inserted_phi = NULL;
8803 : 6180950 : last_inserted_nary = NULL;
8804 : 6180950 : last_pushed_avail = NULL;
8805 : :
8806 : 6180950 : vn_valueize = rpo_vn_valueize;
8807 : :
8808 : : /* Initialize the unwind state and edge/BB executable state. */
8809 : 6180950 : unsigned curr_scc = 0;
8810 : 57135581 : for (int i = 0; i < n; ++i)
8811 : : {
8812 : 50954631 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8813 : 50954631 : rpo_state[i].visited = 0;
8814 : 50954631 : rpo_state[i].max_rpo = i;
8815 : 59456252 : if (!iterate && curr_scc < toplevel_scc_extents.length ())
8816 : : {
8817 : 7033190 : if (i >= toplevel_scc_extents[curr_scc].first
8818 : 7033190 : && i <= toplevel_scc_extents[curr_scc].second)
8819 : 3843311 : rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
8820 : 7033190 : if (i == toplevel_scc_extents[curr_scc].second)
8821 : 726325 : curr_scc++;
8822 : : }
8823 : 50954631 : bb->flags &= ~BB_EXECUTABLE;
8824 : 50954631 : bool has_backedges = false;
8825 : 50954631 : edge e;
8826 : 50954631 : edge_iterator ei;
8827 : 121048912 : FOR_EACH_EDGE (e, ei, bb->preds)
8828 : : {
8829 : 70094281 : if (e->flags & EDGE_DFS_BACK)
8830 : 2888057 : has_backedges = true;
8831 : 70094281 : e->flags &= ~EDGE_EXECUTABLE;
8832 : 70094281 : if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
8833 : 70094281 : continue;
8834 : : }
8835 : 50954631 : rpo_state[i].iterate = iterate && has_backedges;
8836 : : }
8837 : 6180950 : entry->flags |= EDGE_EXECUTABLE;
8838 : 6180950 : entry->dest->flags |= BB_EXECUTABLE;
8839 : :
8840 : : /* As heuristic to improve compile-time we handle only the N innermost
8841 : : loops and the outermost one optimistically. */
8842 : 6180950 : if (iterate)
8843 : : {
8844 : 4309884 : unsigned max_depth = param_rpo_vn_max_loop_depth;
8845 : 14482942 : for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
8846 : 1555600 : if (loop_depth (loop) > max_depth)
8847 : 1949 : for (unsigned i = 2;
8848 : 8518 : i < loop_depth (loop) - max_depth; ++i)
8849 : : {
8850 : 1949 : basic_block header = superloop_at_depth (loop, i)->header;
8851 : 1949 : bool non_latch_backedge = false;
8852 : 1949 : edge e;
8853 : 1949 : edge_iterator ei;
8854 : 5878 : FOR_EACH_EDGE (e, ei, header->preds)
8855 : 3929 : if (e->flags & EDGE_DFS_BACK)
8856 : : {
8857 : : /* There can be a non-latch backedge into the header
8858 : : which is part of an outer irreducible region. We
8859 : : cannot avoid iterating this block then. */
8860 : 1980 : if (!dominated_by_p (CDI_DOMINATORS,
8861 : 1980 : e->src, e->dest))
8862 : : {
8863 : 12 : if (dump_file && (dump_flags & TDF_DETAILS))
8864 : 0 : fprintf (dump_file, "non-latch backedge %d -> %d "
8865 : : "forces iteration of loop %d\n",
8866 : 0 : e->src->index, e->dest->index, loop->num);
8867 : : non_latch_backedge = true;
8868 : : }
8869 : : else
8870 : 1968 : e->flags |= EDGE_EXECUTABLE;
8871 : : }
8872 : 1949 : rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
8873 : 4309884 : }
8874 : : }
8875 : :
8876 : 6180950 : uint64_t nblk = 0;
8877 : 6180950 : int idx = 0;
8878 : 4309884 : if (iterate)
8879 : : /* Go and process all blocks, iterating as necessary. */
8880 : 49462982 : do
8881 : : {
8882 : 49462982 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8883 : :
8884 : : /* If the block has incoming backedges remember unwind state. This
8885 : : is required even for non-executable blocks since in irreducible
8886 : : regions we might reach them via the backedge and re-start iterating
8887 : : from there.
8888 : : Note we can individually mark blocks with incoming backedges to
8889 : : not iterate where we then handle PHIs conservatively. We do that
8890 : : heuristically to reduce compile-time for degenerate cases. */
8891 : 49462982 : if (rpo_state[idx].iterate)
8892 : : {
8893 : 4431546 : rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
8894 : 4431546 : rpo_state[idx].ref_top = last_inserted_ref;
8895 : 4431546 : rpo_state[idx].phi_top = last_inserted_phi;
8896 : 4431546 : rpo_state[idx].nary_top = last_inserted_nary;
8897 : 4431546 : rpo_state[idx].avail_top
8898 : 4431546 : = last_pushed_avail ? last_pushed_avail->avail : NULL;
8899 : : }
8900 : :
8901 : 49462982 : if (!(bb->flags & BB_EXECUTABLE))
8902 : : {
8903 : 881955 : if (dump_file && (dump_flags & TDF_DETAILS))
8904 : 2 : fprintf (dump_file, "Block %d: BB%d found not executable\n",
8905 : : idx, bb->index);
8906 : 881955 : idx++;
8907 : 2802508 : continue;
8908 : : }
8909 : :
8910 : 48581027 : if (dump_file && (dump_flags & TDF_DETAILS))
8911 : 334 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8912 : 48581027 : nblk++;
8913 : 97162054 : todo |= process_bb (avail, bb,
8914 : 48581027 : rpo_state[idx].visited != 0,
8915 : : rpo_state[idx].iterate,
8916 : : iterate, eliminate, do_region, exit_bbs, false);
8917 : 48581027 : rpo_state[idx].visited++;
8918 : :
8919 : : /* Verify if changed values flow over executable outgoing backedges
8920 : : and those change destination PHI values (that's the thing we
8921 : : can easily verify). Reduce over all such edges to the farthest
8922 : : away PHI. */
8923 : 48581027 : int iterate_to = -1;
8924 : 48581027 : edge_iterator ei;
8925 : 48581027 : edge e;
8926 : 117134571 : FOR_EACH_EDGE (e, ei, bb->succs)
8927 : 68553544 : if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
8928 : : == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
8929 : 4454802 : && rpo_state[bb_to_rpo[e->dest->index]].iterate)
8930 : : {
8931 : 4452134 : int destidx = bb_to_rpo[e->dest->index];
8932 : 4452134 : if (!rpo_state[destidx].visited)
8933 : : {
8934 : 135 : if (dump_file && (dump_flags & TDF_DETAILS))
8935 : 0 : fprintf (dump_file, "Unvisited destination %d\n",
8936 : : e->dest->index);
8937 : 135 : if (iterate_to == -1 || destidx < iterate_to)
8938 : 135 : iterate_to = destidx;
8939 : 135 : continue;
8940 : : }
8941 : 4451999 : if (dump_file && (dump_flags & TDF_DETAILS))
8942 : 53 : fprintf (dump_file, "Looking for changed values of backedge"
8943 : : " %d->%d destination PHIs\n",
8944 : 53 : e->src->index, e->dest->index);
8945 : 4451999 : vn_context_bb = e->dest;
8946 : 4451999 : gphi_iterator gsi;
8947 : 4451999 : for (gsi = gsi_start_phis (e->dest);
8948 : 10234391 : !gsi_end_p (gsi); gsi_next (&gsi))
8949 : : {
8950 : 7703102 : bool inserted = false;
8951 : : /* While we'd ideally just iterate on value changes
8952 : : we CSE PHIs and do that even across basic-block
8953 : : boundaries. So even hashtable state changes can
8954 : : be important (which is roughly equivalent to
8955 : : PHI argument value changes). To not excessively
8956 : : iterate because of that we track whether a PHI
8957 : : was CSEd to with GF_PLF_1. */
8958 : 7703102 : bool phival_changed;
8959 : 7703102 : if ((phival_changed = visit_phi (gsi.phi (),
8960 : : &inserted, false))
8961 : 9079617 : || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
8962 : : {
8963 : 1920710 : if (!phival_changed
8964 : 1920710 : && dump_file && (dump_flags & TDF_DETAILS))
8965 : 0 : fprintf (dump_file, "PHI was CSEd and hashtable "
8966 : : "state (changed)\n");
8967 : 1920710 : if (iterate_to == -1 || destidx < iterate_to)
8968 : 1920625 : iterate_to = destidx;
8969 : 1920710 : break;
8970 : : }
8971 : : }
8972 : 4451999 : vn_context_bb = NULL;
8973 : : }
8974 : 48581027 : if (iterate_to != -1)
8975 : : {
8976 : 1920553 : do_unwind (&rpo_state[iterate_to], avail);
8977 : 1920553 : idx = iterate_to;
8978 : 1920553 : if (dump_file && (dump_flags & TDF_DETAILS))
8979 : 20 : fprintf (dump_file, "Iterating to %d BB%d\n",
8980 : 20 : iterate_to, rpo[iterate_to]);
8981 : 1920553 : continue;
8982 : : }
8983 : :
8984 : 46660474 : idx++;
8985 : : }
8986 : 49462982 : while (idx < n);
8987 : :
8988 : : else /* !iterate */
8989 : : {
8990 : : /* Process all blocks greedily with a worklist that enforces RPO
8991 : : processing of reachable blocks. */
8992 : 1871066 : auto_bitmap worklist;
8993 : 1871066 : bitmap_set_bit (worklist, 0);
8994 : 17325220 : while (!bitmap_empty_p (worklist))
8995 : : {
8996 : 13583088 : int idx = bitmap_clear_first_set_bit (worklist);
8997 : 13583088 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8998 : 13583088 : gcc_assert ((bb->flags & BB_EXECUTABLE)
8999 : : && !rpo_state[idx].visited);
9000 : :
9001 : 13583088 : if (dump_file && (dump_flags & TDF_DETAILS))
9002 : 33217 : fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
9003 : :
9004 : : /* When we run into predecessor edges where we cannot trust its
9005 : : executable state mark them executable so PHI processing will
9006 : : be conservative.
9007 : : ??? Do we need to force arguments flowing over that edge
9008 : : to be varying or will they even always be? */
9009 : 13583088 : edge_iterator ei;
9010 : 13583088 : edge e;
9011 : 32954067 : FOR_EACH_EDGE (e, ei, bb->preds)
9012 : 19370979 : if (!(e->flags & EDGE_EXECUTABLE)
9013 : 1038174 : && (bb == entry->dest
9014 : 985529 : || (!rpo_state[bb_to_rpo[e->src->index]].visited
9015 : 949996 : && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
9016 : : >= (int)idx))))
9017 : : {
9018 : 979735 : if (dump_file && (dump_flags & TDF_DETAILS))
9019 : 10677 : fprintf (dump_file, "Cannot trust state of predecessor "
9020 : : "edge %d -> %d, marking executable\n",
9021 : 10677 : e->src->index, e->dest->index);
9022 : 979735 : e->flags |= EDGE_EXECUTABLE;
9023 : : }
9024 : :
9025 : 13583088 : nblk++;
9026 : 13583088 : todo |= process_bb (avail, bb, false, false, false, eliminate,
9027 : : do_region, exit_bbs,
9028 : 13583088 : skip_entry_phis && bb == entry->dest);
9029 : 13583088 : rpo_state[idx].visited++;
9030 : :
9031 : 33572369 : FOR_EACH_EDGE (e, ei, bb->succs)
9032 : 19989281 : if ((e->flags & EDGE_EXECUTABLE)
9033 : 19914420 : && e->dest->index != EXIT_BLOCK
9034 : 18747876 : && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
9035 : 37425793 : && !rpo_state[bb_to_rpo[e->dest->index]].visited)
9036 : 16461739 : bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
9037 : : }
9038 : 1871066 : }
9039 : :
9040 : : /* If statistics or dump file active. */
9041 : 6180950 : int nex = 0;
9042 : 6180950 : unsigned max_visited = 1;
9043 : 57135581 : for (int i = 0; i < n; ++i)
9044 : : {
9045 : 50954631 : basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
9046 : 50954631 : if (bb->flags & BB_EXECUTABLE)
9047 : 50451243 : nex++;
9048 : 50954631 : statistics_histogram_event (cfun, "RPO block visited times",
9049 : 50954631 : rpo_state[i].visited);
9050 : 50954631 : if (rpo_state[i].visited > max_visited)
9051 : : max_visited = rpo_state[i].visited;
9052 : : }
9053 : 6180950 : unsigned nvalues = 0, navail = 0;
9054 : 169762727 : for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
9055 : 333344504 : i != vn_ssa_aux_hash->end (); ++i)
9056 : : {
9057 : 163581777 : nvalues++;
9058 : 163581777 : vn_avail *av = (*i)->avail;
9059 : 242138141 : while (av)
9060 : : {
9061 : 78556364 : navail++;
9062 : 78556364 : av = av->next;
9063 : : }
9064 : : }
9065 : 6180950 : statistics_counter_event (cfun, "RPO blocks", n);
9066 : 6180950 : statistics_counter_event (cfun, "RPO blocks visited", nblk);
9067 : 6180950 : statistics_counter_event (cfun, "RPO blocks executable", nex);
9068 : 6180950 : statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
9069 : 6180950 : statistics_histogram_event (cfun, "RPO num values", nvalues);
9070 : 6180950 : statistics_histogram_event (cfun, "RPO num avail", navail);
9071 : 6180950 : statistics_histogram_event (cfun, "RPO num lattice",
9072 : 6180950 : vn_ssa_aux_hash->elements ());
9073 : 6180950 : if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
9074 : : {
9075 : 10637 : fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
9076 : : " blocks in total discovering %d executable blocks iterating "
9077 : : "%d.%d times, a block was visited max. %u times\n",
9078 : : n, nblk, nex,
9079 : 10637 : (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
9080 : : max_visited);
9081 : 10637 : fprintf (dump_file, "RPO tracked %d values available at %d locations "
9082 : : "and %" PRIu64 " lattice elements\n",
9083 : 10637 : nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
9084 : : }
9085 : :
9086 : 6180950 : if (eliminate)
9087 : : {
9088 : : /* When !iterate we already performed elimination during the RPO
9089 : : walk. */
9090 : 5196794 : if (iterate)
9091 : : {
9092 : : /* Elimination for region-based VN needs to be done within the
9093 : : RPO walk. */
9094 : 3345192 : gcc_assert (! do_region);
9095 : : /* Note we can't use avail.walk here because that gets confused
9096 : : by the existing availability and it will be less efficient
9097 : : as well. */
9098 : 3345192 : todo |= eliminate_with_rpo_vn (NULL);
9099 : : }
9100 : : else
9101 : 1851602 : todo |= avail.eliminate_cleanup (do_region);
9102 : : }
9103 : :
9104 : 6180950 : vn_valueize = NULL;
9105 : 6180950 : rpo_avail = NULL;
9106 : :
9107 : 6180950 : XDELETEVEC (bb_to_rpo);
9108 : 6180950 : XDELETEVEC (rpo);
9109 : 6180950 : XDELETEVEC (rpo_state);
9110 : :
9111 : 6180950 : return todo;
9112 : 6180950 : }
9113 : :
9114 : : /* Region-based entry for RPO VN. Performs value-numbering and elimination
9115 : : on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
9116 : : the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
9117 : : are not considered.
9118 : : If ITERATE is true then treat backedges optimistically as not
9119 : : executed and iterate. If ELIMINATE is true then perform
9120 : : elimination, otherwise leave that to the caller.
9121 : : If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
9122 : : KIND specifies the amount of work done for handling memory operations. */
9123 : :
9124 : : unsigned
9125 : 693888 : do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
9126 : : bool iterate, bool eliminate, bool skip_entry_phis,
9127 : : vn_lookup_kind kind)
9128 : : {
9129 : 693888 : auto_timevar tv (TV_TREE_RPO_VN);
9130 : 693888 : unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
9131 : : skip_entry_phis, kind);
9132 : 693888 : free_rpo_vn ();
9133 : 1387776 : return todo;
9134 : 693888 : }
9135 : :
9136 : :
9137 : : namespace {
9138 : :
9139 : : const pass_data pass_data_fre =
9140 : : {
9141 : : GIMPLE_PASS, /* type */
9142 : : "fre", /* name */
9143 : : OPTGROUP_NONE, /* optinfo_flags */
9144 : : TV_TREE_FRE, /* tv_id */
9145 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
9146 : : 0, /* properties_provided */
9147 : : 0, /* properties_destroyed */
9148 : : 0, /* todo_flags_start */
9149 : : 0, /* todo_flags_finish */
9150 : : };
9151 : :
9152 : : class pass_fre : public gimple_opt_pass
9153 : : {
9154 : : public:
9155 : 1446510 : pass_fre (gcc::context *ctxt)
9156 : 2893020 : : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
9157 : : {}
9158 : :
9159 : : /* opt_pass methods: */
9160 : 1157208 : opt_pass * clone () final override { return new pass_fre (m_ctxt); }
9161 : 1446510 : void set_pass_param (unsigned int n, bool param) final override
9162 : : {
9163 : 1446510 : gcc_assert (n == 0);
9164 : 1446510 : may_iterate = param;
9165 : 1446510 : }
9166 : 4600494 : bool gate (function *) final override
9167 : : {
9168 : 4600494 : return flag_tree_fre != 0 && (may_iterate || optimize > 1);
9169 : : }
9170 : : unsigned int execute (function *) final override;
9171 : :
9172 : : private:
9173 : : bool may_iterate;
9174 : : }; // class pass_fre
9175 : :
9176 : : unsigned int
9177 : 4522370 : pass_fre::execute (function *fun)
9178 : : {
9179 : 4522370 : unsigned todo = 0;
9180 : :
9181 : : /* At -O[1g] use the cheap non-iterating mode. */
9182 : 4522370 : bool iterate_p = may_iterate && (optimize > 1);
9183 : 4522370 : calculate_dominance_info (CDI_DOMINATORS);
9184 : 4522370 : if (iterate_p)
9185 : 3345192 : loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
9186 : :
9187 : 4522370 : todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
9188 : 4522370 : free_rpo_vn ();
9189 : :
9190 : 4522370 : if (iterate_p)
9191 : 3345192 : loop_optimizer_finalize ();
9192 : :
9193 : 4522370 : if (scev_initialized_p ())
9194 : 31202 : scev_reset_htab ();
9195 : :
9196 : : /* For late FRE after IVOPTs and unrolling, see if we can
9197 : : remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
9198 : 4522370 : if (!may_iterate)
9199 : 995669 : todo |= TODO_update_address_taken;
9200 : :
9201 : 4522370 : return todo;
9202 : : }
9203 : :
9204 : : } // anon namespace
9205 : :
9206 : : gimple_opt_pass *
9207 : 289302 : make_pass_fre (gcc::context *ctxt)
9208 : : {
9209 : 289302 : return new pass_fre (ctxt);
9210 : : }
9211 : :
9212 : : #undef BB_EXECUTABLE
|