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

source code of gcc/tree-ssa-sccvn.cc