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