aix: Fix building fat library for AIX
[official-gcc.git] / gcc / tree-ssa-sccvn.cc
blob726e9d88b8f4205a9edcc9c6c503e7bb932d42aa
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 "splay-tree.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "ssa.h"
30 #include "expmed.h"
31 #include "insn-config.h"
32 #include "memmodel.h"
33 #include "emit-rtl.h"
34 #include "cgraph.h"
35 #include "gimple-pretty-print.h"
36 #include "alias.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "cfganal.h"
40 #include "tree-inline.h"
41 #include "internal-fn.h"
42 #include "gimple-iterator.h"
43 #include "gimple-fold.h"
44 #include "tree-eh.h"
45 #include "gimplify.h"
46 #include "flags.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "calls.h"
50 #include "varasm.h"
51 #include "stmt.h"
52 #include "expr.h"
53 #include "tree-dfa.h"
54 #include "tree-ssa.h"
55 #include "dumpfile.h"
56 #include "cfgloop.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-cfg.h"
59 #include "domwalk.h"
60 #include "gimple-match.h"
61 #include "stringpool.h"
62 #include "attribs.h"
63 #include "tree-pass.h"
64 #include "statistics.h"
65 #include "langhooks.h"
66 #include "ipa-utils.h"
67 #include "dbgcnt.h"
68 #include "tree-cfgcleanup.h"
69 #include "tree-ssa-loop.h"
70 #include "tree-scalar-evolution.h"
71 #include "tree-ssa-loop-niter.h"
72 #include "builtins.h"
73 #include "fold-const-call.h"
74 #include "ipa-modref-tree.h"
75 #include "ipa-modref.h"
76 #include "tree-ssa-sccvn.h"
77 #include "alloc-pool.h"
78 #include "symbol-summary.h"
79 #include "sreal.h"
80 #include "ipa-cp.h"
81 #include "ipa-prop.h"
82 #include "target.h"
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;
841 i = 0;
842 j = 0;
845 poly_int64 off1 = 0, off2 = 0;
846 vn_reference_op_t vro1, vro2;
847 vn_reference_op_s tem1, tem2;
848 bool deref1 = false, deref2 = false;
849 bool reverse1 = false, reverse2 = false;
850 for (; vr1->operands.iterate (i, &vro1); i++)
852 if (vro1->opcode == MEM_REF)
853 deref1 = true;
854 /* Do not look through a storage order barrier. */
855 else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
856 return false;
857 reverse1 |= vro1->reverse;
858 if (known_eq (vro1->off, -1))
859 break;
860 off1 += vro1->off;
862 for (; vr2->operands.iterate (j, &vro2); j++)
864 if (vro2->opcode == MEM_REF)
865 deref2 = true;
866 /* Do not look through a storage order barrier. */
867 else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
868 return false;
869 reverse2 |= vro2->reverse;
870 if (known_eq (vro2->off, -1))
871 break;
872 off2 += vro2->off;
874 if (maybe_ne (off1, off2) || reverse1 != reverse2)
875 return false;
876 if (deref1 && vro1->opcode == ADDR_EXPR)
878 memset (&tem1, 0, sizeof (tem1));
879 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
880 tem1.type = TREE_TYPE (tem1.op0);
881 tem1.opcode = TREE_CODE (tem1.op0);
882 vro1 = &tem1;
883 deref1 = false;
885 if (deref2 && vro2->opcode == ADDR_EXPR)
887 memset (&tem2, 0, sizeof (tem2));
888 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
889 tem2.type = TREE_TYPE (tem2.op0);
890 tem2.opcode = TREE_CODE (tem2.op0);
891 vro2 = &tem2;
892 deref2 = false;
894 if (deref1 != deref2)
895 return false;
896 if (!vn_reference_op_eq (vro1, vro2))
897 return false;
898 ++j;
899 ++i;
901 while (vr1->operands.length () != i
902 || vr2->operands.length () != j);
904 return true;
907 /* Copy the operations present in load/store REF into RESULT, a vector of
908 vn_reference_op_s's. */
910 static void
911 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
913 /* For non-calls, store the information that makes up the address. */
914 tree orig = ref;
915 while (ref)
917 vn_reference_op_s temp;
919 memset (&temp, 0, sizeof (temp));
920 temp.type = TREE_TYPE (ref);
921 temp.opcode = TREE_CODE (ref);
922 temp.off = -1;
924 switch (temp.opcode)
926 case MODIFY_EXPR:
927 temp.op0 = TREE_OPERAND (ref, 1);
928 break;
929 case WITH_SIZE_EXPR:
930 temp.op0 = TREE_OPERAND (ref, 1);
931 temp.off = 0;
932 break;
933 case MEM_REF:
934 /* The base address gets its own vn_reference_op_s structure. */
935 temp.op0 = TREE_OPERAND (ref, 1);
936 if (!mem_ref_offset (ref).to_shwi (&temp.off))
937 temp.off = -1;
938 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
939 temp.base = MR_DEPENDENCE_BASE (ref);
940 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
941 break;
942 case TARGET_MEM_REF:
943 /* The base address gets its own vn_reference_op_s structure. */
944 temp.op0 = TMR_INDEX (ref);
945 temp.op1 = TMR_STEP (ref);
946 temp.op2 = TMR_OFFSET (ref);
947 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
948 temp.base = MR_DEPENDENCE_BASE (ref);
949 result->safe_push (temp);
950 memset (&temp, 0, sizeof (temp));
951 temp.type = NULL_TREE;
952 temp.opcode = ERROR_MARK;
953 temp.op0 = TMR_INDEX2 (ref);
954 temp.off = -1;
955 break;
956 case BIT_FIELD_REF:
957 /* Record bits, position and storage order. */
958 temp.op0 = TREE_OPERAND (ref, 1);
959 temp.op1 = TREE_OPERAND (ref, 2);
960 if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
961 temp.off = -1;
962 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
963 break;
964 case COMPONENT_REF:
965 /* The field decl is enough to unambiguously specify the field,
966 so use its type here. */
967 temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
968 temp.op0 = TREE_OPERAND (ref, 1);
969 temp.op1 = TREE_OPERAND (ref, 2);
970 temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
971 && TYPE_REVERSE_STORAGE_ORDER
972 (TREE_TYPE (TREE_OPERAND (ref, 0))));
974 tree this_offset = component_ref_field_offset (ref);
975 if (this_offset
976 && poly_int_tree_p (this_offset))
978 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
979 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
981 poly_offset_int off
982 = (wi::to_poly_offset (this_offset)
983 + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
984 /* Probibit value-numbering zero offset components
985 of addresses the same before the pass folding
986 __builtin_object_size had a chance to run. */
987 if (TREE_CODE (orig) != ADDR_EXPR
988 || maybe_ne (off, 0)
989 || (cfun->curr_properties & PROP_objsz))
990 off.to_shwi (&temp.off);
994 break;
995 case ARRAY_RANGE_REF:
996 case ARRAY_REF:
998 tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
999 /* Record index as operand. */
1000 temp.op0 = TREE_OPERAND (ref, 1);
1001 /* Always record lower bounds and element size. */
1002 temp.op1 = array_ref_low_bound (ref);
1003 /* But record element size in units of the type alignment. */
1004 temp.op2 = TREE_OPERAND (ref, 3);
1005 temp.align = eltype->type_common.align;
1006 if (! temp.op2)
1007 temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
1008 size_int (TYPE_ALIGN_UNIT (eltype)));
1009 if (poly_int_tree_p (temp.op0)
1010 && poly_int_tree_p (temp.op1)
1011 && TREE_CODE (temp.op2) == INTEGER_CST)
1013 poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1014 - wi::to_poly_offset (temp.op1))
1015 * wi::to_offset (temp.op2)
1016 * vn_ref_op_align_unit (&temp));
1017 off.to_shwi (&temp.off);
1019 temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1020 && TYPE_REVERSE_STORAGE_ORDER
1021 (TREE_TYPE (TREE_OPERAND (ref, 0))));
1023 break;
1024 case VAR_DECL:
1025 if (DECL_HARD_REGISTER (ref))
1027 temp.op0 = ref;
1028 break;
1030 /* Fallthru. */
1031 case PARM_DECL:
1032 case CONST_DECL:
1033 case RESULT_DECL:
1034 /* Canonicalize decls to MEM[&decl] which is what we end up with
1035 when valueizing MEM[ptr] with ptr = &decl. */
1036 temp.opcode = MEM_REF;
1037 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1038 temp.off = 0;
1039 result->safe_push (temp);
1040 temp.opcode = ADDR_EXPR;
1041 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1042 temp.type = TREE_TYPE (temp.op0);
1043 temp.off = -1;
1044 break;
1045 case STRING_CST:
1046 case INTEGER_CST:
1047 case POLY_INT_CST:
1048 case COMPLEX_CST:
1049 case VECTOR_CST:
1050 case REAL_CST:
1051 case FIXED_CST:
1052 case CONSTRUCTOR:
1053 case SSA_NAME:
1054 temp.op0 = ref;
1055 break;
1056 case ADDR_EXPR:
1057 if (is_gimple_min_invariant (ref))
1059 temp.op0 = ref;
1060 break;
1062 break;
1063 /* These are only interesting for their operands, their
1064 existence, and their type. They will never be the last
1065 ref in the chain of references (IE they require an
1066 operand), so we don't have to put anything
1067 for op* as it will be handled by the iteration */
1068 case REALPART_EXPR:
1069 temp.off = 0;
1070 break;
1071 case VIEW_CONVERT_EXPR:
1072 temp.off = 0;
1073 temp.reverse = storage_order_barrier_p (ref);
1074 break;
1075 case IMAGPART_EXPR:
1076 /* This is only interesting for its constant offset. */
1077 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1078 break;
1079 default:
1080 gcc_unreachable ();
1082 result->safe_push (temp);
1084 if (REFERENCE_CLASS_P (ref)
1085 || TREE_CODE (ref) == MODIFY_EXPR
1086 || TREE_CODE (ref) == WITH_SIZE_EXPR
1087 || (TREE_CODE (ref) == ADDR_EXPR
1088 && !is_gimple_min_invariant (ref)))
1089 ref = TREE_OPERAND (ref, 0);
1090 else
1091 ref = NULL_TREE;
1095 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1096 operands in *OPS, the reference alias set SET and the reference type TYPE.
1097 Return true if something useful was produced. */
1099 bool
1100 ao_ref_init_from_vn_reference (ao_ref *ref,
1101 alias_set_type set, alias_set_type base_set,
1102 tree type, const vec<vn_reference_op_s> &ops)
1104 unsigned i;
1105 tree base = NULL_TREE;
1106 tree *op0_p = &base;
1107 poly_offset_int offset = 0;
1108 poly_offset_int max_size;
1109 poly_offset_int size = -1;
1110 tree size_tree = NULL_TREE;
1112 /* We don't handle calls. */
1113 if (!type)
1114 return false;
1116 machine_mode mode = TYPE_MODE (type);
1117 if (mode == BLKmode)
1118 size_tree = TYPE_SIZE (type);
1119 else
1120 size = GET_MODE_BITSIZE (mode);
1121 if (size_tree != NULL_TREE
1122 && poly_int_tree_p (size_tree))
1123 size = wi::to_poly_offset (size_tree);
1125 /* Lower the final access size from the outermost expression. */
1126 const_vn_reference_op_t cst_op = &ops[0];
1127 /* Cast away constness for the sake of the const-unsafe
1128 FOR_EACH_VEC_ELT(). */
1129 vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1130 size_tree = NULL_TREE;
1131 if (op->opcode == COMPONENT_REF)
1132 size_tree = DECL_SIZE (op->op0);
1133 else if (op->opcode == BIT_FIELD_REF)
1134 size_tree = op->op0;
1135 if (size_tree != NULL_TREE
1136 && poly_int_tree_p (size_tree)
1137 && (!known_size_p (size)
1138 || known_lt (wi::to_poly_offset (size_tree), size)))
1139 size = wi::to_poly_offset (size_tree);
1141 /* Initially, maxsize is the same as the accessed element size.
1142 In the following it will only grow (or become -1). */
1143 max_size = size;
1145 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1146 and find the ultimate containing object. */
1147 FOR_EACH_VEC_ELT (ops, i, op)
1149 switch (op->opcode)
1151 case CALL_EXPR:
1152 return false;
1154 /* Record the base objects. */
1155 case MEM_REF:
1156 *op0_p = build2 (MEM_REF, op->type,
1157 NULL_TREE, op->op0);
1158 MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1159 MR_DEPENDENCE_BASE (*op0_p) = op->base;
1160 op0_p = &TREE_OPERAND (*op0_p, 0);
1161 break;
1163 case TARGET_MEM_REF:
1164 *op0_p = build5 (TARGET_MEM_REF, op->type,
1165 NULL_TREE, op->op2, op->op0,
1166 op->op1, ops[i+1].op0);
1167 MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1168 MR_DEPENDENCE_BASE (*op0_p) = op->base;
1169 op0_p = &TREE_OPERAND (*op0_p, 0);
1170 ++i;
1171 break;
1173 /* Unwrap some of the wrapped decls. */
1174 case ADDR_EXPR:
1175 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1176 if (base != NULL_TREE
1177 && TREE_CODE (base) == MEM_REF
1178 && op->op0
1179 && DECL_P (TREE_OPERAND (op->op0, 0)))
1181 const_vn_reference_op_t pop = &ops[i-1];
1182 base = TREE_OPERAND (op->op0, 0);
1183 if (known_eq (pop->off, -1))
1185 max_size = -1;
1186 offset = 0;
1188 else
1189 offset += pop->off * BITS_PER_UNIT;
1190 op0_p = NULL;
1191 break;
1193 /* Fallthru. */
1194 case PARM_DECL:
1195 case CONST_DECL:
1196 case RESULT_DECL:
1197 /* ??? We shouldn't see these, but un-canonicalize what
1198 copy_reference_ops_from_ref does when visiting MEM_REF. */
1199 case VAR_DECL:
1200 /* ??? And for this only have DECL_HARD_REGISTER. */
1201 case STRING_CST:
1202 /* This can show up in ARRAY_REF bases. */
1203 case INTEGER_CST:
1204 *op0_p = op->op0;
1205 op0_p = NULL;
1206 break;
1208 case SSA_NAME:
1209 /* SSA names we have to get at one available since it contains
1210 flow-sensitive info. */
1211 *op0_p = vn_valueize (op->op0);
1212 op0_p = NULL;
1213 break;
1215 /* And now the usual component-reference style ops. */
1216 case BIT_FIELD_REF:
1217 offset += wi::to_poly_offset (op->op1);
1218 break;
1220 case COMPONENT_REF:
1222 tree field = op->op0;
1223 /* We do not have a complete COMPONENT_REF tree here so we
1224 cannot use component_ref_field_offset. Do the interesting
1225 parts manually. */
1226 tree this_offset = DECL_FIELD_OFFSET (field);
1228 if (op->op1 || !poly_int_tree_p (this_offset))
1229 max_size = -1;
1230 else
1232 poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1233 << LOG2_BITS_PER_UNIT);
1234 woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1235 offset += woffset;
1237 break;
1240 case ARRAY_RANGE_REF:
1241 case ARRAY_REF:
1242 /* Use the recorded constant offset. */
1243 if (maybe_eq (op->off, -1))
1244 max_size = -1;
1245 else
1246 offset += op->off * BITS_PER_UNIT;
1247 break;
1249 case REALPART_EXPR:
1250 break;
1252 case IMAGPART_EXPR:
1253 offset += size;
1254 break;
1256 case VIEW_CONVERT_EXPR:
1257 break;
1259 case POLY_INT_CST:
1260 case COMPLEX_CST:
1261 case VECTOR_CST:
1262 case REAL_CST:
1263 case FIXED_CST:
1264 case CONSTRUCTOR:
1265 return false;
1267 default:
1268 return false;
1272 if (base == NULL_TREE)
1273 return false;
1275 ref->ref = NULL_TREE;
1276 ref->base = base;
1277 ref->ref_alias_set = set;
1278 ref->base_alias_set = base_set;
1279 /* We discount volatiles from value-numbering elsewhere. */
1280 ref->volatile_p = false;
1282 if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1284 ref->offset = 0;
1285 ref->size = -1;
1286 ref->max_size = -1;
1287 return true;
1290 if (!offset.to_shwi (&ref->offset))
1292 ref->offset = 0;
1293 ref->max_size = -1;
1294 return true;
1297 if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1298 ref->max_size = -1;
1300 return true;
1303 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1304 vn_reference_op_s's. */
1306 static void
1307 copy_reference_ops_from_call (gcall *call,
1308 vec<vn_reference_op_s> *result)
1310 vn_reference_op_s temp;
1311 unsigned i;
1312 tree lhs = gimple_call_lhs (call);
1313 int lr;
1315 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1316 different. By adding the lhs here in the vector, we ensure that the
1317 hashcode is different, guaranteeing a different value number. */
1318 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1320 memset (&temp, 0, sizeof (temp));
1321 temp.opcode = MODIFY_EXPR;
1322 temp.type = TREE_TYPE (lhs);
1323 temp.op0 = lhs;
1324 temp.off = -1;
1325 result->safe_push (temp);
1328 /* Copy the type, opcode, function, static chain and EH region, if any. */
1329 memset (&temp, 0, sizeof (temp));
1330 temp.type = gimple_call_fntype (call);
1331 temp.opcode = CALL_EXPR;
1332 temp.op0 = gimple_call_fn (call);
1333 if (gimple_call_internal_p (call))
1334 temp.clique = gimple_call_internal_fn (call);
1335 temp.op1 = gimple_call_chain (call);
1336 if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1337 temp.op2 = size_int (lr);
1338 temp.off = -1;
1339 result->safe_push (temp);
1341 /* Copy the call arguments. As they can be references as well,
1342 just chain them together. */
1343 for (i = 0; i < gimple_call_num_args (call); ++i)
1345 tree callarg = gimple_call_arg (call, i);
1346 copy_reference_ops_from_ref (callarg, result);
1350 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1351 *I_P to point to the last element of the replacement. */
1352 static bool
1353 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1354 unsigned int *i_p)
1356 unsigned int i = *i_p;
1357 vn_reference_op_t op = &(*ops)[i];
1358 vn_reference_op_t mem_op = &(*ops)[i - 1];
1359 tree addr_base;
1360 poly_int64 addr_offset = 0;
1362 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1363 from .foo.bar to the preceding MEM_REF offset and replace the
1364 address with &OBJ. */
1365 addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1366 &addr_offset, vn_valueize);
1367 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1368 if (addr_base != TREE_OPERAND (op->op0, 0))
1370 poly_offset_int off
1371 = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1372 SIGNED)
1373 + addr_offset);
1374 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1375 op->op0 = build_fold_addr_expr (addr_base);
1376 if (tree_fits_shwi_p (mem_op->op0))
1377 mem_op->off = tree_to_shwi (mem_op->op0);
1378 else
1379 mem_op->off = -1;
1380 return true;
1382 return false;
1385 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1386 *I_P to point to the last element of the replacement. */
1387 static bool
1388 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1389 unsigned int *i_p)
1391 bool changed = false;
1392 vn_reference_op_t op;
1396 unsigned int i = *i_p;
1397 op = &(*ops)[i];
1398 vn_reference_op_t mem_op = &(*ops)[i - 1];
1399 gimple *def_stmt;
1400 enum tree_code code;
1401 poly_offset_int off;
1403 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1404 if (!is_gimple_assign (def_stmt))
1405 return changed;
1407 code = gimple_assign_rhs_code (def_stmt);
1408 if (code != ADDR_EXPR
1409 && code != POINTER_PLUS_EXPR)
1410 return changed;
1412 off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1414 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1415 from .foo.bar to the preceding MEM_REF offset and replace the
1416 address with &OBJ. */
1417 if (code == ADDR_EXPR)
1419 tree addr, addr_base;
1420 poly_int64 addr_offset;
1422 addr = gimple_assign_rhs1 (def_stmt);
1423 addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1424 &addr_offset,
1425 vn_valueize);
1426 /* If that didn't work because the address isn't invariant propagate
1427 the reference tree from the address operation in case the current
1428 dereference isn't offsetted. */
1429 if (!addr_base
1430 && *i_p == ops->length () - 1
1431 && known_eq (off, 0)
1432 /* This makes us disable this transform for PRE where the
1433 reference ops might be also used for code insertion which
1434 is invalid. */
1435 && default_vn_walk_kind == VN_WALKREWRITE)
1437 auto_vec<vn_reference_op_s, 32> tem;
1438 copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1439 /* Make sure to preserve TBAA info. The only objects not
1440 wrapped in MEM_REFs that can have their address taken are
1441 STRING_CSTs. */
1442 if (tem.length () >= 2
1443 && tem[tem.length () - 2].opcode == MEM_REF)
1445 vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1446 new_mem_op->op0
1447 = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1448 wi::to_poly_wide (new_mem_op->op0));
1450 else
1451 gcc_assert (tem.last ().opcode == STRING_CST);
1452 ops->pop ();
1453 ops->pop ();
1454 ops->safe_splice (tem);
1455 --*i_p;
1456 return true;
1458 if (!addr_base
1459 || TREE_CODE (addr_base) != MEM_REF
1460 || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1461 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1462 0))))
1463 return changed;
1465 off += addr_offset;
1466 off += mem_ref_offset (addr_base);
1467 op->op0 = TREE_OPERAND (addr_base, 0);
1469 else
1471 tree ptr, ptroff;
1472 ptr = gimple_assign_rhs1 (def_stmt);
1473 ptroff = gimple_assign_rhs2 (def_stmt);
1474 if (TREE_CODE (ptr) != SSA_NAME
1475 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1476 /* Make sure to not endlessly recurse.
1477 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1478 happen when we value-number a PHI to its backedge value. */
1479 || SSA_VAL (ptr) == op->op0
1480 || !poly_int_tree_p (ptroff))
1481 return changed;
1483 off += wi::to_poly_offset (ptroff);
1484 op->op0 = ptr;
1487 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1488 if (tree_fits_shwi_p (mem_op->op0))
1489 mem_op->off = tree_to_shwi (mem_op->op0);
1490 else
1491 mem_op->off = -1;
1492 /* ??? Can end up with endless recursion here!?
1493 gcc.c-torture/execute/strcmp-1.c */
1494 if (TREE_CODE (op->op0) == SSA_NAME)
1495 op->op0 = SSA_VAL (op->op0);
1496 if (TREE_CODE (op->op0) != SSA_NAME)
1497 op->opcode = TREE_CODE (op->op0);
1499 changed = true;
1501 /* Tail-recurse. */
1502 while (TREE_CODE (op->op0) == SSA_NAME);
1504 /* Fold a remaining *&. */
1505 if (TREE_CODE (op->op0) == ADDR_EXPR)
1506 vn_reference_fold_indirect (ops, i_p);
1508 return changed;
1511 /* Optimize the reference REF to a constant if possible or return
1512 NULL_TREE if not. */
1514 tree
1515 fully_constant_vn_reference_p (vn_reference_t ref)
1517 vec<vn_reference_op_s> operands = ref->operands;
1518 vn_reference_op_t op;
1520 /* Try to simplify the translated expression if it is
1521 a call to a builtin function with at most two arguments. */
1522 op = &operands[0];
1523 if (op->opcode == CALL_EXPR
1524 && (!op->op0
1525 || (TREE_CODE (op->op0) == ADDR_EXPR
1526 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1527 && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1528 BUILT_IN_NORMAL)))
1529 && operands.length () >= 2
1530 && operands.length () <= 3)
1532 vn_reference_op_t arg0, arg1 = NULL;
1533 bool anyconst = false;
1534 arg0 = &operands[1];
1535 if (operands.length () > 2)
1536 arg1 = &operands[2];
1537 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1538 || (arg0->opcode == ADDR_EXPR
1539 && is_gimple_min_invariant (arg0->op0)))
1540 anyconst = true;
1541 if (arg1
1542 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1543 || (arg1->opcode == ADDR_EXPR
1544 && is_gimple_min_invariant (arg1->op0))))
1545 anyconst = true;
1546 if (anyconst)
1548 combined_fn fn;
1549 if (op->op0)
1550 fn = as_combined_fn (DECL_FUNCTION_CODE
1551 (TREE_OPERAND (op->op0, 0)));
1552 else
1553 fn = as_combined_fn ((internal_fn) op->clique);
1554 tree folded;
1555 if (arg1)
1556 folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1557 else
1558 folded = fold_const_call (fn, ref->type, arg0->op0);
1559 if (folded
1560 && is_gimple_min_invariant (folded))
1561 return folded;
1565 /* Simplify reads from constants or constant initializers. */
1566 else if (BITS_PER_UNIT == 8
1567 && ref->type
1568 && COMPLETE_TYPE_P (ref->type)
1569 && is_gimple_reg_type (ref->type))
1571 poly_int64 off = 0;
1572 HOST_WIDE_INT size;
1573 if (INTEGRAL_TYPE_P (ref->type))
1574 size = TYPE_PRECISION (ref->type);
1575 else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1576 size = tree_to_shwi (TYPE_SIZE (ref->type));
1577 else
1578 return NULL_TREE;
1579 if (size % BITS_PER_UNIT != 0
1580 || size > MAX_BITSIZE_MODE_ANY_MODE)
1581 return NULL_TREE;
1582 size /= BITS_PER_UNIT;
1583 unsigned i;
1584 for (i = 0; i < operands.length (); ++i)
1586 if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1588 ++i;
1589 break;
1591 if (known_eq (operands[i].off, -1))
1592 return NULL_TREE;
1593 off += operands[i].off;
1594 if (operands[i].opcode == MEM_REF)
1596 ++i;
1597 break;
1600 vn_reference_op_t base = &operands[--i];
1601 tree ctor = error_mark_node;
1602 tree decl = NULL_TREE;
1603 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1604 ctor = base->op0;
1605 else if (base->opcode == MEM_REF
1606 && base[1].opcode == ADDR_EXPR
1607 && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1608 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1609 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1611 decl = TREE_OPERAND (base[1].op0, 0);
1612 if (TREE_CODE (decl) == STRING_CST)
1613 ctor = decl;
1614 else
1615 ctor = ctor_for_folding (decl);
1617 if (ctor == NULL_TREE)
1618 return build_zero_cst (ref->type);
1619 else if (ctor != error_mark_node)
1621 HOST_WIDE_INT const_off;
1622 if (decl)
1624 tree res = fold_ctor_reference (ref->type, ctor,
1625 off * BITS_PER_UNIT,
1626 size * BITS_PER_UNIT, decl);
1627 if (res)
1629 STRIP_USELESS_TYPE_CONVERSION (res);
1630 if (is_gimple_min_invariant (res))
1631 return res;
1634 else if (off.is_constant (&const_off))
1636 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1637 int len = native_encode_expr (ctor, buf, size, const_off);
1638 if (len > 0)
1639 return native_interpret_expr (ref->type, buf, len);
1644 return NULL_TREE;
1647 /* Return true if OPS contain a storage order barrier. */
1649 static bool
1650 contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1652 vn_reference_op_t op;
1653 unsigned i;
1655 FOR_EACH_VEC_ELT (ops, i, op)
1656 if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1657 return true;
1659 return false;
1662 /* Return true if OPS represent an access with reverse storage order. */
1664 static bool
1665 reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
1667 unsigned i = 0;
1668 if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1669 ++i;
1670 switch (ops[i].opcode)
1672 case ARRAY_REF:
1673 case COMPONENT_REF:
1674 case BIT_FIELD_REF:
1675 case MEM_REF:
1676 return ops[i].reverse;
1677 default:
1678 return false;
1682 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1683 structures into their value numbers. This is done in-place, and
1684 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1685 whether any operands were valueized. */
1687 static void
1688 valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1689 bool with_avail = false)
1691 *valueized_anything = false;
1693 for (unsigned i = 0; i < orig->length (); ++i)
1695 re_valueize:
1696 vn_reference_op_t vro = &(*orig)[i];
1697 if (vro->opcode == SSA_NAME
1698 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1700 tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1701 if (tem != vro->op0)
1703 *valueized_anything = true;
1704 vro->op0 = tem;
1706 /* If it transforms from an SSA_NAME to a constant, update
1707 the opcode. */
1708 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1709 vro->opcode = TREE_CODE (vro->op0);
1711 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1713 tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1714 if (tem != vro->op1)
1716 *valueized_anything = true;
1717 vro->op1 = tem;
1720 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1722 tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1723 if (tem != vro->op2)
1725 *valueized_anything = true;
1726 vro->op2 = tem;
1729 /* If it transforms from an SSA_NAME to an address, fold with
1730 a preceding indirect reference. */
1731 if (i > 0
1732 && vro->op0
1733 && TREE_CODE (vro->op0) == ADDR_EXPR
1734 && (*orig)[i - 1].opcode == MEM_REF)
1736 if (vn_reference_fold_indirect (orig, &i))
1737 *valueized_anything = true;
1739 else if (i > 0
1740 && vro->opcode == SSA_NAME
1741 && (*orig)[i - 1].opcode == MEM_REF)
1743 if (vn_reference_maybe_forwprop_address (orig, &i))
1745 *valueized_anything = true;
1746 /* Re-valueize the current operand. */
1747 goto re_valueize;
1750 /* If it transforms a non-constant ARRAY_REF into a constant
1751 one, adjust the constant offset. */
1752 else if ((vro->opcode == ARRAY_REF
1753 || vro->opcode == ARRAY_RANGE_REF)
1754 && known_eq (vro->off, -1)
1755 && poly_int_tree_p (vro->op0)
1756 && poly_int_tree_p (vro->op1)
1757 && TREE_CODE (vro->op2) == INTEGER_CST)
1759 poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1760 - wi::to_poly_offset (vro->op1))
1761 * wi::to_offset (vro->op2)
1762 * vn_ref_op_align_unit (vro));
1763 off.to_shwi (&vro->off);
1768 static void
1769 valueize_refs (vec<vn_reference_op_s> *orig)
1771 bool tem;
1772 valueize_refs_1 (orig, &tem);
1775 static vec<vn_reference_op_s> shared_lookup_references;
1777 /* Create a vector of vn_reference_op_s structures from REF, a
1778 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1779 this function. *VALUEIZED_ANYTHING will specify whether any
1780 operands were valueized. */
1782 static vec<vn_reference_op_s>
1783 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1785 if (!ref)
1786 return vNULL;
1787 shared_lookup_references.truncate (0);
1788 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1789 valueize_refs_1 (&shared_lookup_references, valueized_anything);
1790 return shared_lookup_references;
1793 /* Create a vector of vn_reference_op_s structures from CALL, a
1794 call statement. The vector is shared among all callers of
1795 this function. */
1797 static vec<vn_reference_op_s>
1798 valueize_shared_reference_ops_from_call (gcall *call)
1800 if (!call)
1801 return vNULL;
1802 shared_lookup_references.truncate (0);
1803 copy_reference_ops_from_call (call, &shared_lookup_references);
1804 valueize_refs (&shared_lookup_references);
1805 return shared_lookup_references;
1808 /* Lookup a SCCVN reference operation VR in the current hash table.
1809 Returns the resulting value number if it exists in the hash table,
1810 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1811 vn_reference_t stored in the hashtable if something is found. */
1813 static tree
1814 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1816 vn_reference_s **slot;
1817 hashval_t hash;
1819 hash = vr->hashcode;
1820 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1821 if (slot)
1823 if (vnresult)
1824 *vnresult = (vn_reference_t)*slot;
1825 return ((vn_reference_t)*slot)->result;
1828 return NULL_TREE;
1832 /* Partial definition tracking support. */
1834 struct pd_range
1836 HOST_WIDE_INT offset;
1837 HOST_WIDE_INT size;
1840 struct pd_data
1842 tree rhs;
1843 HOST_WIDE_INT rhs_off;
1844 HOST_WIDE_INT offset;
1845 HOST_WIDE_INT size;
1848 /* Context for alias walking. */
1850 struct vn_walk_cb_data
1852 vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1853 vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1854 bool redundant_store_removal_p_)
1855 : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1856 mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1857 vn_walk_kind (vn_walk_kind_),
1858 tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
1859 saved_operands (vNULL), first_set (-2), first_base_set (-2),
1860 known_ranges (NULL)
1862 if (!last_vuse_ptr)
1863 last_vuse_ptr = &last_vuse;
1864 ao_ref_init (&orig_ref, orig_ref_);
1865 if (mask)
1867 wide_int w = wi::to_wide (mask);
1868 unsigned int pos = 0, prec = w.get_precision ();
1869 pd_data pd;
1870 pd.rhs = build_constructor (NULL_TREE, NULL);
1871 pd.rhs_off = 0;
1872 /* When bitwise and with a constant is done on a memory load,
1873 we don't really need all the bits to be defined or defined
1874 to constants, we don't really care what is in the position
1875 corresponding to 0 bits in the mask.
1876 So, push the ranges of those 0 bits in the mask as artificial
1877 zero stores and let the partial def handling code do the
1878 rest. */
1879 while (pos < prec)
1881 int tz = wi::ctz (w);
1882 if (pos + tz > prec)
1883 tz = prec - pos;
1884 if (tz)
1886 if (BYTES_BIG_ENDIAN)
1887 pd.offset = prec - pos - tz;
1888 else
1889 pd.offset = pos;
1890 pd.size = tz;
1891 void *r = push_partial_def (pd, 0, 0, 0, prec);
1892 gcc_assert (r == NULL_TREE);
1894 pos += tz;
1895 if (pos == prec)
1896 break;
1897 w = wi::lrshift (w, tz);
1898 tz = wi::ctz (wi::bit_not (w));
1899 if (pos + tz > prec)
1900 tz = prec - pos;
1901 pos += tz;
1902 w = wi::lrshift (w, tz);
1906 ~vn_walk_cb_data ();
1907 void *finish (alias_set_type, alias_set_type, tree);
1908 void *push_partial_def (pd_data pd,
1909 alias_set_type, alias_set_type, HOST_WIDE_INT,
1910 HOST_WIDE_INT);
1912 vn_reference_t vr;
1913 ao_ref orig_ref;
1914 tree *last_vuse_ptr;
1915 tree last_vuse;
1916 tree mask;
1917 tree masked_result;
1918 tree same_val;
1919 vn_lookup_kind vn_walk_kind;
1920 bool tbaa_p;
1921 bool redundant_store_removal_p;
1922 vec<vn_reference_op_s> saved_operands;
1924 /* The VDEFs of partial defs we come along. */
1925 auto_vec<pd_data, 2> partial_defs;
1926 /* The first defs range to avoid splay tree setup in most cases. */
1927 pd_range first_range;
1928 alias_set_type first_set;
1929 alias_set_type first_base_set;
1930 splay_tree known_ranges;
1931 obstack ranges_obstack;
1932 static constexpr HOST_WIDE_INT bufsize = 64;
1935 vn_walk_cb_data::~vn_walk_cb_data ()
1937 if (known_ranges)
1939 splay_tree_delete (known_ranges);
1940 obstack_free (&ranges_obstack, NULL);
1942 saved_operands.release ();
1945 void *
1946 vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
1948 if (first_set != -2)
1950 set = first_set;
1951 base_set = first_base_set;
1953 if (mask)
1955 masked_result = val;
1956 return (void *) -1;
1958 if (same_val && !operand_equal_p (val, same_val))
1959 return (void *) -1;
1960 vec<vn_reference_op_s> &operands
1961 = saved_operands.exists () ? saved_operands : vr->operands;
1962 return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
1963 vr->offset, vr->max_size,
1964 vr->type, operands, val);
1967 /* pd_range splay-tree helpers. */
1969 static int
1970 pd_range_compare (splay_tree_key offset1p, splay_tree_key offset2p)
1972 HOST_WIDE_INT offset1 = *(HOST_WIDE_INT *)offset1p;
1973 HOST_WIDE_INT offset2 = *(HOST_WIDE_INT *)offset2p;
1974 if (offset1 < offset2)
1975 return -1;
1976 else if (offset1 > offset2)
1977 return 1;
1978 return 0;
1981 static void *
1982 pd_tree_alloc (int size, void *data_)
1984 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
1985 return obstack_alloc (&data->ranges_obstack, size);
1988 static void
1989 pd_tree_dealloc (void *, void *)
1993 /* Push PD to the vector of partial definitions returning a
1994 value when we are ready to combine things with VUSE, SET and MAXSIZEI,
1995 NULL when we want to continue looking for partial defs or -1
1996 on failure. */
1998 void *
1999 vn_walk_cb_data::push_partial_def (pd_data pd,
2000 alias_set_type set, alias_set_type base_set,
2001 HOST_WIDE_INT offseti,
2002 HOST_WIDE_INT maxsizei)
2004 /* We're using a fixed buffer for encoding so fail early if the object
2005 we want to interpret is bigger. */
2006 if (maxsizei > bufsize * BITS_PER_UNIT
2007 || CHAR_BIT != 8
2008 || BITS_PER_UNIT != 8
2009 /* Not prepared to handle PDP endian. */
2010 || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2011 return (void *)-1;
2013 /* Turn too large constant stores into non-constant stores. */
2014 if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
2015 pd.rhs = error_mark_node;
2017 /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
2018 most a partial byte before and/or after the region. */
2019 if (!CONSTANT_CLASS_P (pd.rhs))
2021 if (pd.offset < offseti)
2023 HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2024 gcc_assert (pd.size > o);
2025 pd.size -= o;
2026 pd.offset += o;
2028 if (pd.size > maxsizei)
2029 pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
2032 pd.offset -= offseti;
2034 bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2035 || CONSTANT_CLASS_P (pd.rhs));
2036 pd_range *r;
2037 if (partial_defs.is_empty ())
2039 /* If we get a clobber upfront, fail. */
2040 if (TREE_CLOBBER_P (pd.rhs))
2041 return (void *)-1;
2042 if (!pd_constant_p)
2043 return (void *)-1;
2044 partial_defs.safe_push (pd);
2045 first_range.offset = pd.offset;
2046 first_range.size = pd.size;
2047 first_set = set;
2048 first_base_set = base_set;
2049 last_vuse_ptr = NULL;
2050 r = &first_range;
2051 /* Go check if the first partial definition was a full one in case
2052 the caller didn't optimize for this. */
2054 else
2056 if (!known_ranges)
2058 /* ??? Optimize the case where the 2nd partial def completes
2059 things. */
2060 gcc_obstack_init (&ranges_obstack);
2061 known_ranges = splay_tree_new_with_allocator (pd_range_compare, 0, 0,
2062 pd_tree_alloc,
2063 pd_tree_dealloc, this);
2064 splay_tree_insert (known_ranges,
2065 (splay_tree_key)&first_range.offset,
2066 (splay_tree_value)&first_range);
2069 pd_range newr = { pd.offset, pd.size };
2070 splay_tree_node n;
2071 /* Lookup the predecessor of offset + 1 and see if we need to merge. */
2072 HOST_WIDE_INT loffset = newr.offset + 1;
2073 if ((n = splay_tree_predecessor (known_ranges, (splay_tree_key)&loffset))
2074 && ((r = (pd_range *)n->value), true)
2075 && ranges_known_overlap_p (r->offset, r->size + 1,
2076 newr.offset, newr.size))
2078 /* Ignore partial defs already covered. Here we also drop shadowed
2079 clobbers arriving here at the floor. */
2080 if (known_subrange_p (newr.offset, newr.size, r->offset, r->size))
2081 return NULL;
2082 r->size
2083 = MAX (r->offset + r->size, newr.offset + newr.size) - r->offset;
2085 else
2087 /* newr.offset wasn't covered yet, insert the range. */
2088 r = XOBNEW (&ranges_obstack, pd_range);
2089 *r = newr;
2090 splay_tree_insert (known_ranges, (splay_tree_key)&r->offset,
2091 (splay_tree_value)r);
2093 /* Merge r which now contains newr and is a member of the splay tree with
2094 adjacent overlapping ranges. */
2095 pd_range *rafter;
2096 while ((n = splay_tree_successor (known_ranges,
2097 (splay_tree_key)&r->offset))
2098 && ((rafter = (pd_range *)n->value), true)
2099 && ranges_known_overlap_p (r->offset, r->size + 1,
2100 rafter->offset, rafter->size))
2102 r->size = MAX (r->offset + r->size,
2103 rafter->offset + rafter->size) - r->offset;
2104 splay_tree_remove (known_ranges, (splay_tree_key)&rafter->offset);
2106 /* If we get a clobber, fail. */
2107 if (TREE_CLOBBER_P (pd.rhs))
2108 return (void *)-1;
2109 /* Non-constants are OK as long as they are shadowed by a constant. */
2110 if (!pd_constant_p)
2111 return (void *)-1;
2112 partial_defs.safe_push (pd);
2115 /* Now we have merged newr into the range tree. When we have covered
2116 [offseti, sizei] then the tree will contain exactly one node which has
2117 the desired properties and it will be 'r'. */
2118 if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2119 /* Continue looking for partial defs. */
2120 return NULL;
2122 /* Now simply native encode all partial defs in reverse order. */
2123 unsigned ndefs = partial_defs.length ();
2124 /* We support up to 512-bit values (for V8DFmode). */
2125 unsigned char buffer[bufsize + 1];
2126 unsigned char this_buffer[bufsize + 1];
2127 int len;
2129 memset (buffer, 0, bufsize + 1);
2130 unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2131 while (!partial_defs.is_empty ())
2133 pd_data pd = partial_defs.pop ();
2134 unsigned int amnt;
2135 if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2137 /* Empty CONSTRUCTOR. */
2138 if (pd.size >= needed_len * BITS_PER_UNIT)
2139 len = needed_len;
2140 else
2141 len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2142 memset (this_buffer, 0, len);
2144 else if (pd.rhs_off >= 0)
2146 len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2147 (MAX (0, -pd.offset)
2148 + pd.rhs_off) / BITS_PER_UNIT);
2149 if (len <= 0
2150 || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2151 - MAX (0, -pd.offset) / BITS_PER_UNIT))
2153 if (dump_file && (dump_flags & TDF_DETAILS))
2154 fprintf (dump_file, "Failed to encode %u "
2155 "partial definitions\n", ndefs);
2156 return (void *)-1;
2159 else /* negative pd.rhs_off indicates we want to chop off first bits */
2161 if (-pd.rhs_off >= bufsize)
2162 return (void *)-1;
2163 len = native_encode_expr (pd.rhs,
2164 this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2165 bufsize - -pd.rhs_off / BITS_PER_UNIT,
2166 MAX (0, -pd.offset) / BITS_PER_UNIT);
2167 if (len <= 0
2168 || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2169 - MAX (0, -pd.offset) / BITS_PER_UNIT))
2171 if (dump_file && (dump_flags & TDF_DETAILS))
2172 fprintf (dump_file, "Failed to encode %u "
2173 "partial definitions\n", ndefs);
2174 return (void *)-1;
2178 unsigned char *p = buffer;
2179 HOST_WIDE_INT size = pd.size;
2180 if (pd.offset < 0)
2181 size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2182 this_buffer[len] = 0;
2183 if (BYTES_BIG_ENDIAN)
2185 /* LSB of this_buffer[len - 1] byte should be at
2186 pd.offset + pd.size - 1 bits in buffer. */
2187 amnt = ((unsigned HOST_WIDE_INT) pd.offset
2188 + pd.size) % BITS_PER_UNIT;
2189 if (amnt)
2190 shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2191 unsigned char *q = this_buffer;
2192 unsigned int off = 0;
2193 if (pd.offset >= 0)
2195 unsigned int msk;
2196 off = pd.offset / BITS_PER_UNIT;
2197 gcc_assert (off < needed_len);
2198 p = buffer + off;
2199 if (size <= amnt)
2201 msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2202 *p = (*p & ~msk) | (this_buffer[len] & msk);
2203 size = 0;
2205 else
2207 if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2208 q = (this_buffer + len
2209 - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2210 / BITS_PER_UNIT));
2211 if (pd.offset % BITS_PER_UNIT)
2213 msk = -1U << (BITS_PER_UNIT
2214 - (pd.offset % BITS_PER_UNIT));
2215 *p = (*p & msk) | (*q & ~msk);
2216 p++;
2217 q++;
2218 off++;
2219 size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2220 gcc_assert (size >= 0);
2224 else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2226 q = (this_buffer + len
2227 - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2228 / BITS_PER_UNIT));
2229 if (pd.offset % BITS_PER_UNIT)
2231 q++;
2232 size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2233 % BITS_PER_UNIT);
2234 gcc_assert (size >= 0);
2237 if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2238 > needed_len)
2239 size = (needed_len - off) * BITS_PER_UNIT;
2240 memcpy (p, q, size / BITS_PER_UNIT);
2241 if (size % BITS_PER_UNIT)
2243 unsigned int msk
2244 = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2245 p += size / BITS_PER_UNIT;
2246 q += size / BITS_PER_UNIT;
2247 *p = (*q & msk) | (*p & ~msk);
2250 else
2252 if (pd.offset >= 0)
2254 /* LSB of this_buffer[0] byte should be at pd.offset bits
2255 in buffer. */
2256 unsigned int msk;
2257 size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2258 amnt = pd.offset % BITS_PER_UNIT;
2259 if (amnt)
2260 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2261 unsigned int off = pd.offset / BITS_PER_UNIT;
2262 gcc_assert (off < needed_len);
2263 size = MIN (size,
2264 (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2265 p = buffer + off;
2266 if (amnt + size < BITS_PER_UNIT)
2268 /* Low amnt bits come from *p, then size bits
2269 from this_buffer[0] and the remaining again from
2270 *p. */
2271 msk = ((1 << size) - 1) << amnt;
2272 *p = (*p & ~msk) | (this_buffer[0] & msk);
2273 size = 0;
2275 else if (amnt)
2277 msk = -1U << amnt;
2278 *p = (*p & ~msk) | (this_buffer[0] & msk);
2279 p++;
2280 size -= (BITS_PER_UNIT - amnt);
2283 else
2285 amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2286 if (amnt)
2287 size -= BITS_PER_UNIT - amnt;
2288 size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2289 if (amnt)
2290 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2292 memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2293 p += size / BITS_PER_UNIT;
2294 if (size % BITS_PER_UNIT)
2296 unsigned int msk = -1U << (size % BITS_PER_UNIT);
2297 *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2298 & ~msk) | (*p & msk);
2303 tree type = vr->type;
2304 /* Make sure to interpret in a type that has a range covering the whole
2305 access size. */
2306 if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2307 type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2308 tree val;
2309 if (BYTES_BIG_ENDIAN)
2311 unsigned sz = needed_len;
2312 if (maxsizei % BITS_PER_UNIT)
2313 shift_bytes_in_array_right (buffer, needed_len,
2314 BITS_PER_UNIT
2315 - (maxsizei % BITS_PER_UNIT));
2316 if (INTEGRAL_TYPE_P (type))
2318 if (TYPE_MODE (type) != BLKmode)
2319 sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2320 else
2321 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2323 if (sz > needed_len)
2325 memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2326 val = native_interpret_expr (type, this_buffer, sz);
2328 else
2329 val = native_interpret_expr (type, buffer, needed_len);
2331 else
2332 val = native_interpret_expr (type, buffer, bufsize);
2333 /* If we chop off bits because the types precision doesn't match the memory
2334 access size this is ok when optimizing reads but not when called from
2335 the DSE code during elimination. */
2336 if (val && type != vr->type)
2338 if (! int_fits_type_p (val, vr->type))
2339 val = NULL_TREE;
2340 else
2341 val = fold_convert (vr->type, val);
2344 if (val)
2346 if (dump_file && (dump_flags & TDF_DETAILS))
2347 fprintf (dump_file,
2348 "Successfully combined %u partial definitions\n", ndefs);
2349 /* We are using the alias-set of the first store we encounter which
2350 should be appropriate here. */
2351 return finish (first_set, first_base_set, val);
2353 else
2355 if (dump_file && (dump_flags & TDF_DETAILS))
2356 fprintf (dump_file,
2357 "Failed to interpret %u encoded partial definitions\n", ndefs);
2358 return (void *)-1;
2362 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2363 with the current VUSE and performs the expression lookup. */
2365 static void *
2366 vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2368 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2369 vn_reference_t vr = data->vr;
2370 vn_reference_s **slot;
2371 hashval_t hash;
2373 /* If we have partial definitions recorded we have to go through
2374 vn_reference_lookup_3. */
2375 if (!data->partial_defs.is_empty ())
2376 return NULL;
2378 if (data->last_vuse_ptr)
2380 *data->last_vuse_ptr = vuse;
2381 data->last_vuse = vuse;
2384 /* Fixup vuse and hash. */
2385 if (vr->vuse)
2386 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2387 vr->vuse = vuse_ssa_val (vuse);
2388 if (vr->vuse)
2389 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2391 hash = vr->hashcode;
2392 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2393 if (slot)
2395 if ((*slot)->result && data->saved_operands.exists ())
2396 return data->finish (vr->set, vr->base_set, (*slot)->result);
2397 return *slot;
2400 if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2402 HOST_WIDE_INT op_offset, op_size;
2403 tree v = NULL_TREE;
2404 tree base = ao_ref_base (op);
2406 if (base
2407 && op->offset.is_constant (&op_offset)
2408 && op->size.is_constant (&op_size)
2409 && op->max_size_known_p ()
2410 && known_eq (op->size, op->max_size))
2412 if (TREE_CODE (base) == PARM_DECL)
2413 v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2414 op_size);
2415 else if (TREE_CODE (base) == MEM_REF
2416 && integer_zerop (TREE_OPERAND (base, 1))
2417 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2418 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2419 && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2420 == PARM_DECL))
2421 v = ipcp_get_aggregate_const (cfun,
2422 SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2423 true, op_offset, op_size);
2425 if (v)
2426 return data->finish (vr->set, vr->base_set, v);
2429 return NULL;
2432 /* Lookup an existing or insert a new vn_reference entry into the
2433 value table for the VUSE, SET, TYPE, OPERANDS reference which
2434 has the value VALUE which is either a constant or an SSA name. */
2436 static vn_reference_t
2437 vn_reference_lookup_or_insert_for_pieces (tree vuse,
2438 alias_set_type set,
2439 alias_set_type base_set,
2440 poly_int64 offset,
2441 poly_int64 max_size,
2442 tree type,
2443 vec<vn_reference_op_s,
2444 va_heap> operands,
2445 tree value)
2447 vn_reference_s vr1;
2448 vn_reference_t result;
2449 unsigned value_id;
2450 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2451 vr1.operands = operands;
2452 vr1.type = type;
2453 vr1.set = set;
2454 vr1.base_set = base_set;
2455 vr1.offset = offset;
2456 vr1.max_size = max_size;
2457 vr1.hashcode = vn_reference_compute_hash (&vr1);
2458 if (vn_reference_lookup_1 (&vr1, &result))
2459 return result;
2461 if (TREE_CODE (value) == SSA_NAME)
2462 value_id = VN_INFO (value)->value_id;
2463 else
2464 value_id = get_or_alloc_constant_value_id (value);
2465 return vn_reference_insert_pieces (vuse, set, base_set, offset, max_size,
2466 type, operands.copy (), value, value_id);
2469 /* Return a value-number for RCODE OPS... either by looking up an existing
2470 value-number for the possibly simplified result or by inserting the
2471 operation if INSERT is true. If SIMPLIFY is false, return a value
2472 number for the unsimplified expression. */
2474 static tree
2475 vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2476 bool simplify)
2478 tree result = NULL_TREE;
2479 /* We will be creating a value number for
2480 RCODE (OPS...).
2481 So first simplify and lookup this expression to see if it
2482 is already available. */
2483 /* For simplification valueize. */
2484 unsigned i = 0;
2485 if (simplify)
2486 for (i = 0; i < res_op->num_ops; ++i)
2487 if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2489 tree tem = vn_valueize (res_op->ops[i]);
2490 if (!tem)
2491 break;
2492 res_op->ops[i] = tem;
2494 /* If valueization of an operand fails (it is not available), skip
2495 simplification. */
2496 bool res = false;
2497 if (i == res_op->num_ops)
2499 mprts_hook = vn_lookup_simplify_result;
2500 res = res_op->resimplify (NULL, vn_valueize);
2501 mprts_hook = NULL;
2503 gimple *new_stmt = NULL;
2504 if (res
2505 && gimple_simplified_result_is_gimple_val (res_op))
2507 /* The expression is already available. */
2508 result = res_op->ops[0];
2509 /* Valueize it, simplification returns sth in AVAIL only. */
2510 if (TREE_CODE (result) == SSA_NAME)
2511 result = SSA_VAL (result);
2513 else
2515 tree val = vn_lookup_simplify_result (res_op);
2516 if (!val && insert)
2518 gimple_seq stmts = NULL;
2519 result = maybe_push_res_to_seq (res_op, &stmts);
2520 if (result)
2522 gcc_assert (gimple_seq_singleton_p (stmts));
2523 new_stmt = gimple_seq_first_stmt (stmts);
2526 else
2527 /* The expression is already available. */
2528 result = val;
2530 if (new_stmt)
2532 /* The expression is not yet available, value-number lhs to
2533 the new SSA_NAME we created. */
2534 /* Initialize value-number information properly. */
2535 vn_ssa_aux_t result_info = VN_INFO (result);
2536 result_info->valnum = result;
2537 result_info->value_id = get_next_value_id ();
2538 result_info->visited = 1;
2539 gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2540 new_stmt);
2541 result_info->needs_insertion = true;
2542 /* ??? PRE phi-translation inserts NARYs without corresponding
2543 SSA name result. Re-use those but set their result according
2544 to the stmt we just built. */
2545 vn_nary_op_t nary = NULL;
2546 vn_nary_op_lookup_stmt (new_stmt, &nary);
2547 if (nary)
2549 gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2550 nary->u.result = gimple_assign_lhs (new_stmt);
2552 /* As all "inserted" statements are singleton SCCs, insert
2553 to the valid table. This is strictly needed to
2554 avoid re-generating new value SSA_NAMEs for the same
2555 expression during SCC iteration over and over (the
2556 optimistic table gets cleared after each iteration).
2557 We do not need to insert into the optimistic table, as
2558 lookups there will fall back to the valid table. */
2559 else
2561 unsigned int length = vn_nary_length_from_stmt (new_stmt);
2562 vn_nary_op_t vno1
2563 = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2564 vno1->value_id = result_info->value_id;
2565 vno1->length = length;
2566 vno1->predicated_values = 0;
2567 vno1->u.result = result;
2568 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2569 vn_nary_op_insert_into (vno1, valid_info->nary);
2570 /* Also do not link it into the undo chain. */
2571 last_inserted_nary = vno1->next;
2572 vno1->next = (vn_nary_op_t)(void *)-1;
2574 if (dump_file && (dump_flags & TDF_DETAILS))
2576 fprintf (dump_file, "Inserting name ");
2577 print_generic_expr (dump_file, result);
2578 fprintf (dump_file, " for expression ");
2579 print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2580 fprintf (dump_file, "\n");
2583 return result;
2586 /* Return a value-number for RCODE OPS... either by looking up an existing
2587 value-number for the simplified result or by inserting the operation. */
2589 static tree
2590 vn_nary_build_or_lookup (gimple_match_op *res_op)
2592 return vn_nary_build_or_lookup_1 (res_op, true, true);
2595 /* Try to simplify the expression RCODE OPS... of type TYPE and return
2596 its value if present. */
2598 tree
2599 vn_nary_simplify (vn_nary_op_t nary)
2601 if (nary->length > gimple_match_op::MAX_NUM_OPS)
2602 return NULL_TREE;
2603 gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2604 nary->type, nary->length);
2605 memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2606 return vn_nary_build_or_lookup_1 (&op, false, true);
2609 /* Elimination engine. */
2611 class eliminate_dom_walker : public dom_walker
2613 public:
2614 eliminate_dom_walker (cdi_direction, bitmap);
2615 ~eliminate_dom_walker ();
2617 edge before_dom_children (basic_block) final override;
2618 void after_dom_children (basic_block) final override;
2620 virtual tree eliminate_avail (basic_block, tree op);
2621 virtual void eliminate_push_avail (basic_block, tree op);
2622 tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2624 void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2626 unsigned eliminate_cleanup (bool region_p = false);
2628 bool do_pre;
2629 unsigned int el_todo;
2630 unsigned int eliminations;
2631 unsigned int insertions;
2633 /* SSA names that had their defs inserted by PRE if do_pre. */
2634 bitmap inserted_exprs;
2636 /* Blocks with statements that have had their EH properties changed. */
2637 bitmap need_eh_cleanup;
2639 /* Blocks with statements that have had their AB properties changed. */
2640 bitmap need_ab_cleanup;
2642 /* Local state for the eliminate domwalk. */
2643 auto_vec<gimple *> to_remove;
2644 auto_vec<gimple *> to_fixup;
2645 auto_vec<tree> avail;
2646 auto_vec<tree> avail_stack;
2649 /* Adaptor to the elimination engine using RPO availability. */
2651 class rpo_elim : public eliminate_dom_walker
2653 public:
2654 rpo_elim(basic_block entry_)
2655 : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2656 m_avail_freelist (NULL) {}
2658 tree eliminate_avail (basic_block, tree op) final override;
2660 void eliminate_push_avail (basic_block, tree) final override;
2662 basic_block entry;
2663 /* Freelist of avail entries which are allocated from the vn_ssa_aux
2664 obstack. */
2665 vn_avail *m_avail_freelist;
2668 /* Global RPO state for access from hooks. */
2669 static eliminate_dom_walker *rpo_avail;
2670 basic_block vn_context_bb;
2672 /* Return true if BASE1 and BASE2 can be adjusted so they have the
2673 same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2674 Otherwise return false. */
2676 static bool
2677 adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2678 tree base2, poly_int64 *offset2)
2680 poly_int64 soff;
2681 if (TREE_CODE (base1) == MEM_REF
2682 && TREE_CODE (base2) == MEM_REF)
2684 if (mem_ref_offset (base1).to_shwi (&soff))
2686 base1 = TREE_OPERAND (base1, 0);
2687 *offset1 += soff * BITS_PER_UNIT;
2689 if (mem_ref_offset (base2).to_shwi (&soff))
2691 base2 = TREE_OPERAND (base2, 0);
2692 *offset2 += soff * BITS_PER_UNIT;
2694 return operand_equal_p (base1, base2, 0);
2696 return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2699 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2700 from the statement defining VUSE and if not successful tries to
2701 translate *REFP and VR_ through an aggregate copy at the definition
2702 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2703 of *REF and *VR. If only disambiguation was performed then
2704 *DISAMBIGUATE_ONLY is set to true. */
2706 static void *
2707 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2708 translate_flags *disambiguate_only)
2710 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2711 vn_reference_t vr = data->vr;
2712 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2713 tree base = ao_ref_base (ref);
2714 HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2715 static vec<vn_reference_op_s> lhs_ops;
2716 ao_ref lhs_ref;
2717 bool lhs_ref_ok = false;
2718 poly_int64 copy_size;
2720 /* First try to disambiguate after value-replacing in the definitions LHS. */
2721 if (is_gimple_assign (def_stmt))
2723 tree lhs = gimple_assign_lhs (def_stmt);
2724 bool valueized_anything = false;
2725 /* Avoid re-allocation overhead. */
2726 lhs_ops.truncate (0);
2727 basic_block saved_rpo_bb = vn_context_bb;
2728 vn_context_bb = gimple_bb (def_stmt);
2729 if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2731 copy_reference_ops_from_ref (lhs, &lhs_ops);
2732 valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2734 ao_ref_init (&lhs_ref, lhs);
2735 lhs_ref_ok = true;
2736 if (valueized_anything
2737 && ao_ref_init_from_vn_reference
2738 (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2739 ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2740 && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2742 vn_context_bb = saved_rpo_bb;
2743 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2744 return NULL;
2746 vn_context_bb = saved_rpo_bb;
2748 /* When the def is a CLOBBER we can optimistically disambiguate
2749 against it since any overlap it would be undefined behavior.
2750 Avoid this for obvious must aliases to save compile-time though.
2751 We also may not do this when the query is used for redundant
2752 store removal. */
2753 if (!data->redundant_store_removal_p
2754 && gimple_clobber_p (def_stmt)
2755 && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2757 *disambiguate_only = TR_DISAMBIGUATE;
2758 return NULL;
2761 /* Besides valueizing the LHS we can also use access-path based
2762 disambiguation on the original non-valueized ref. */
2763 if (!ref->ref
2764 && lhs_ref_ok
2765 && data->orig_ref.ref)
2767 /* We want to use the non-valueized LHS for this, but avoid redundant
2768 work. */
2769 ao_ref *lref = &lhs_ref;
2770 ao_ref lref_alt;
2771 if (valueized_anything)
2773 ao_ref_init (&lref_alt, lhs);
2774 lref = &lref_alt;
2776 if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2778 *disambiguate_only = (valueized_anything
2779 ? TR_VALUEIZE_AND_DISAMBIGUATE
2780 : TR_DISAMBIGUATE);
2781 return NULL;
2785 /* If we reach a clobbering statement try to skip it and see if
2786 we find a VN result with exactly the same value as the
2787 possible clobber. In this case we can ignore the clobber
2788 and return the found value. */
2789 if (is_gimple_reg_type (TREE_TYPE (lhs))
2790 && types_compatible_p (TREE_TYPE (lhs), vr->type)
2791 && (ref->ref || data->orig_ref.ref)
2792 && !data->mask
2793 && data->partial_defs.is_empty ()
2794 && multiple_p (get_object_alignment
2795 (ref->ref ? ref->ref : data->orig_ref.ref),
2796 ref->size)
2797 && multiple_p (get_object_alignment (lhs), ref->size))
2799 tree rhs = gimple_assign_rhs1 (def_stmt);
2800 /* ??? We may not compare to ahead values which might be from
2801 a different loop iteration but only to loop invariants. Use
2802 CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2803 The one-hop lookup below doesn't have this issue since there's
2804 a virtual PHI before we ever reach a backedge to cross.
2805 We can skip multiple defs as long as they are from the same
2806 value though. */
2807 if (data->same_val
2808 && !operand_equal_p (data->same_val, rhs))
2810 else if (CONSTANT_CLASS_P (rhs))
2812 if (dump_file && (dump_flags & TDF_DETAILS))
2814 fprintf (dump_file,
2815 "Skipping possible redundant definition ");
2816 print_gimple_stmt (dump_file, def_stmt, 0);
2818 /* Delay the actual compare of the values to the end of the walk
2819 but do not update last_vuse from here. */
2820 data->last_vuse_ptr = NULL;
2821 data->same_val = rhs;
2822 return NULL;
2824 else
2826 tree saved_vuse = vr->vuse;
2827 hashval_t saved_hashcode = vr->hashcode;
2828 if (vr->vuse)
2829 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2830 vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
2831 if (vr->vuse)
2832 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2833 vn_reference_t vnresult = NULL;
2834 /* Do not use vn_reference_lookup_2 since that might perform
2835 expression hashtable insertion but this lookup crosses
2836 a possible may-alias making such insertion conditionally
2837 invalid. */
2838 vn_reference_lookup_1 (vr, &vnresult);
2839 /* Need to restore vr->vuse and vr->hashcode. */
2840 vr->vuse = saved_vuse;
2841 vr->hashcode = saved_hashcode;
2842 if (vnresult)
2844 if (TREE_CODE (rhs) == SSA_NAME)
2845 rhs = SSA_VAL (rhs);
2846 if (vnresult->result
2847 && operand_equal_p (vnresult->result, rhs, 0))
2848 return vnresult;
2853 else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2854 && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2855 && gimple_call_num_args (def_stmt) <= 4)
2857 /* For builtin calls valueize its arguments and call the
2858 alias oracle again. Valueization may improve points-to
2859 info of pointers and constify size and position arguments.
2860 Originally this was motivated by PR61034 which has
2861 conditional calls to free falsely clobbering ref because
2862 of imprecise points-to info of the argument. */
2863 tree oldargs[4];
2864 bool valueized_anything = false;
2865 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2867 oldargs[i] = gimple_call_arg (def_stmt, i);
2868 tree val = vn_valueize (oldargs[i]);
2869 if (val != oldargs[i])
2871 gimple_call_set_arg (def_stmt, i, val);
2872 valueized_anything = true;
2875 if (valueized_anything)
2877 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2878 ref, data->tbaa_p);
2879 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2880 gimple_call_set_arg (def_stmt, i, oldargs[i]);
2881 if (!res)
2883 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2884 return NULL;
2889 if (*disambiguate_only > TR_TRANSLATE)
2890 return (void *)-1;
2892 /* If we cannot constrain the size of the reference we cannot
2893 test if anything kills it. */
2894 if (!ref->max_size_known_p ())
2895 return (void *)-1;
2897 poly_int64 offset = ref->offset;
2898 poly_int64 maxsize = ref->max_size;
2900 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2901 from that definition.
2902 1) Memset. */
2903 if (is_gimple_reg_type (vr->type)
2904 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2905 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
2906 && (integer_zerop (gimple_call_arg (def_stmt, 1))
2907 || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2908 || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2909 && CHAR_BIT == 8
2910 && BITS_PER_UNIT == 8
2911 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2912 && offset.is_constant (&offseti)
2913 && ref->size.is_constant (&sizei)
2914 && (offseti % BITS_PER_UNIT == 0
2915 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
2916 && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2917 || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
2918 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
2919 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2920 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2922 tree base2;
2923 poly_int64 offset2, size2, maxsize2;
2924 bool reverse;
2925 tree ref2 = gimple_call_arg (def_stmt, 0);
2926 if (TREE_CODE (ref2) == SSA_NAME)
2928 ref2 = SSA_VAL (ref2);
2929 if (TREE_CODE (ref2) == SSA_NAME
2930 && (TREE_CODE (base) != MEM_REF
2931 || TREE_OPERAND (base, 0) != ref2))
2933 gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2934 if (gimple_assign_single_p (def_stmt)
2935 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2936 ref2 = gimple_assign_rhs1 (def_stmt);
2939 if (TREE_CODE (ref2) == ADDR_EXPR)
2941 ref2 = TREE_OPERAND (ref2, 0);
2942 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
2943 &reverse);
2944 if (!known_size_p (maxsize2)
2945 || !known_eq (maxsize2, size2)
2946 || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
2947 return (void *)-1;
2949 else if (TREE_CODE (ref2) == SSA_NAME)
2951 poly_int64 soff;
2952 if (TREE_CODE (base) != MEM_REF
2953 || !(mem_ref_offset (base)
2954 << LOG2_BITS_PER_UNIT).to_shwi (&soff))
2955 return (void *)-1;
2956 offset += soff;
2957 offset2 = 0;
2958 if (TREE_OPERAND (base, 0) != ref2)
2960 gimple *def = SSA_NAME_DEF_STMT (ref2);
2961 if (is_gimple_assign (def)
2962 && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
2963 && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
2964 && poly_int_tree_p (gimple_assign_rhs2 (def)))
2966 tree rhs2 = gimple_assign_rhs2 (def);
2967 if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
2968 SIGNED)
2969 << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
2970 return (void *)-1;
2971 ref2 = gimple_assign_rhs1 (def);
2972 if (TREE_CODE (ref2) == SSA_NAME)
2973 ref2 = SSA_VAL (ref2);
2975 else
2976 return (void *)-1;
2979 else
2980 return (void *)-1;
2981 tree len = gimple_call_arg (def_stmt, 2);
2982 HOST_WIDE_INT leni, offset2i;
2983 if (TREE_CODE (len) == SSA_NAME)
2984 len = SSA_VAL (len);
2985 /* Sometimes the above trickery is smarter than alias analysis. Take
2986 advantage of that. */
2987 if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
2988 (wi::to_poly_offset (len)
2989 << LOG2_BITS_PER_UNIT)))
2990 return NULL;
2991 if (data->partial_defs.is_empty ()
2992 && known_subrange_p (offset, maxsize, offset2,
2993 wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
2995 tree val;
2996 if (integer_zerop (gimple_call_arg (def_stmt, 1)))
2997 val = build_zero_cst (vr->type);
2998 else if (INTEGRAL_TYPE_P (vr->type)
2999 && known_eq (ref->size, 8)
3000 && offseti % BITS_PER_UNIT == 0)
3002 gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
3003 vr->type, gimple_call_arg (def_stmt, 1));
3004 val = vn_nary_build_or_lookup (&res_op);
3005 if (!val
3006 || (TREE_CODE (val) == SSA_NAME
3007 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3008 return (void *)-1;
3010 else
3012 unsigned buflen
3013 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
3014 if (INTEGRAL_TYPE_P (vr->type)
3015 && TYPE_MODE (vr->type) != BLKmode)
3016 buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
3017 unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
3018 memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
3019 buflen);
3020 if (BYTES_BIG_ENDIAN)
3022 unsigned int amnt
3023 = (((unsigned HOST_WIDE_INT) offseti + sizei)
3024 % BITS_PER_UNIT);
3025 if (amnt)
3027 shift_bytes_in_array_right (buf, buflen,
3028 BITS_PER_UNIT - amnt);
3029 buf++;
3030 buflen--;
3033 else if (offseti % BITS_PER_UNIT != 0)
3035 unsigned int amnt
3036 = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3037 % BITS_PER_UNIT);
3038 shift_bytes_in_array_left (buf, buflen, amnt);
3039 buf++;
3040 buflen--;
3042 val = native_interpret_expr (vr->type, buf, buflen);
3043 if (!val)
3044 return (void *)-1;
3046 return data->finish (0, 0, val);
3048 /* For now handle clearing memory with partial defs. */
3049 else if (known_eq (ref->size, maxsize)
3050 && integer_zerop (gimple_call_arg (def_stmt, 1))
3051 && tree_fits_poly_int64_p (len)
3052 && tree_to_poly_int64 (len).is_constant (&leni)
3053 && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3054 && offset.is_constant (&offseti)
3055 && offset2.is_constant (&offset2i)
3056 && maxsize.is_constant (&maxsizei)
3057 && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3058 leni << LOG2_BITS_PER_UNIT))
3060 pd_data pd;
3061 pd.rhs = build_constructor (NULL_TREE, NULL);
3062 pd.rhs_off = 0;
3063 pd.offset = offset2i;
3064 pd.size = leni << LOG2_BITS_PER_UNIT;
3065 return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3069 /* 2) Assignment from an empty CONSTRUCTOR. */
3070 else if (is_gimple_reg_type (vr->type)
3071 && gimple_assign_single_p (def_stmt)
3072 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3073 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
3075 tree base2;
3076 poly_int64 offset2, size2, maxsize2;
3077 HOST_WIDE_INT offset2i, size2i;
3078 gcc_assert (lhs_ref_ok);
3079 base2 = ao_ref_base (&lhs_ref);
3080 offset2 = lhs_ref.offset;
3081 size2 = lhs_ref.size;
3082 maxsize2 = lhs_ref.max_size;
3083 if (known_size_p (maxsize2)
3084 && known_eq (maxsize2, size2)
3085 && adjust_offsets_for_equal_base_address (base, &offset,
3086 base2, &offset2))
3088 if (data->partial_defs.is_empty ()
3089 && known_subrange_p (offset, maxsize, offset2, size2))
3091 /* While technically undefined behavior do not optimize
3092 a full read from a clobber. */
3093 if (gimple_clobber_p (def_stmt))
3094 return (void *)-1;
3095 tree val = build_zero_cst (vr->type);
3096 return data->finish (ao_ref_alias_set (&lhs_ref),
3097 ao_ref_base_alias_set (&lhs_ref), val);
3099 else if (known_eq (ref->size, maxsize)
3100 && maxsize.is_constant (&maxsizei)
3101 && offset.is_constant (&offseti)
3102 && offset2.is_constant (&offset2i)
3103 && size2.is_constant (&size2i)
3104 && ranges_known_overlap_p (offseti, maxsizei,
3105 offset2i, size2i))
3107 /* Let clobbers be consumed by the partial-def tracker
3108 which can choose to ignore them if they are shadowed
3109 by a later def. */
3110 pd_data pd;
3111 pd.rhs = gimple_assign_rhs1 (def_stmt);
3112 pd.rhs_off = 0;
3113 pd.offset = offset2i;
3114 pd.size = size2i;
3115 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3116 ao_ref_base_alias_set (&lhs_ref),
3117 offseti, maxsizei);
3122 /* 3) Assignment from a constant. We can use folds native encode/interpret
3123 routines to extract the assigned bits. */
3124 else if (known_eq (ref->size, maxsize)
3125 && is_gimple_reg_type (vr->type)
3126 && !reverse_storage_order_for_component_p (vr->operands)
3127 && !contains_storage_order_barrier_p (vr->operands)
3128 && gimple_assign_single_p (def_stmt)
3129 && CHAR_BIT == 8
3130 && BITS_PER_UNIT == 8
3131 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3132 /* native_encode and native_decode operate on arrays of bytes
3133 and so fundamentally need a compile-time size and offset. */
3134 && maxsize.is_constant (&maxsizei)
3135 && offset.is_constant (&offseti)
3136 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3137 || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3138 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3140 tree lhs = gimple_assign_lhs (def_stmt);
3141 tree base2;
3142 poly_int64 offset2, size2, maxsize2;
3143 HOST_WIDE_INT offset2i, size2i;
3144 bool reverse;
3145 gcc_assert (lhs_ref_ok);
3146 base2 = ao_ref_base (&lhs_ref);
3147 offset2 = lhs_ref.offset;
3148 size2 = lhs_ref.size;
3149 maxsize2 = lhs_ref.max_size;
3150 reverse = reverse_storage_order_for_component_p (lhs);
3151 if (base2
3152 && !reverse
3153 && !storage_order_barrier_p (lhs)
3154 && known_eq (maxsize2, size2)
3155 && adjust_offsets_for_equal_base_address (base, &offset,
3156 base2, &offset2)
3157 && offset.is_constant (&offseti)
3158 && offset2.is_constant (&offset2i)
3159 && size2.is_constant (&size2i))
3161 if (data->partial_defs.is_empty ()
3162 && known_subrange_p (offseti, maxsizei, offset2, size2))
3164 /* We support up to 512-bit values (for V8DFmode). */
3165 unsigned char buffer[65];
3166 int len;
3168 tree rhs = gimple_assign_rhs1 (def_stmt);
3169 if (TREE_CODE (rhs) == SSA_NAME)
3170 rhs = SSA_VAL (rhs);
3171 len = native_encode_expr (rhs,
3172 buffer, sizeof (buffer) - 1,
3173 (offseti - offset2i) / BITS_PER_UNIT);
3174 if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
3176 tree type = vr->type;
3177 unsigned char *buf = buffer;
3178 unsigned int amnt = 0;
3179 /* Make sure to interpret in a type that has a range
3180 covering the whole access size. */
3181 if (INTEGRAL_TYPE_P (vr->type)
3182 && maxsizei != TYPE_PRECISION (vr->type))
3183 type = build_nonstandard_integer_type (maxsizei,
3184 TYPE_UNSIGNED (type));
3185 if (BYTES_BIG_ENDIAN)
3187 /* For big-endian native_encode_expr stored the rhs
3188 such that the LSB of it is the LSB of buffer[len - 1].
3189 That bit is stored into memory at position
3190 offset2 + size2 - 1, i.e. in byte
3191 base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3192 E.g. for offset2 1 and size2 14, rhs -1 and memory
3193 previously cleared that is:
3195 01111111|11111110
3196 Now, if we want to extract offset 2 and size 12 from
3197 it using native_interpret_expr (which actually works
3198 for integral bitfield types in terms of byte size of
3199 the mode), the native_encode_expr stored the value
3200 into buffer as
3201 XX111111|11111111
3202 and returned len 2 (the X bits are outside of
3203 precision).
3204 Let sz be maxsize / BITS_PER_UNIT if not extracting
3205 a bitfield, and GET_MODE_SIZE otherwise.
3206 We need to align the LSB of the value we want to
3207 extract as the LSB of buf[sz - 1].
3208 The LSB from memory we need to read is at position
3209 offset + maxsize - 1. */
3210 HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3211 if (INTEGRAL_TYPE_P (type))
3213 if (TYPE_MODE (type) != BLKmode)
3214 sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3215 else
3216 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3218 amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3219 - offseti - maxsizei) % BITS_PER_UNIT;
3220 if (amnt)
3221 shift_bytes_in_array_right (buffer, len, amnt);
3222 amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3223 - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3224 if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3225 len = 0;
3226 else
3228 buf = buffer + len - sz - amnt;
3229 len -= (buf - buffer);
3232 else
3234 amnt = ((unsigned HOST_WIDE_INT) offset2i
3235 - offseti) % BITS_PER_UNIT;
3236 if (amnt)
3238 buffer[len] = 0;
3239 shift_bytes_in_array_left (buffer, len + 1, amnt);
3240 buf = buffer + 1;
3243 tree val = native_interpret_expr (type, buf, len);
3244 /* If we chop off bits because the types precision doesn't
3245 match the memory access size this is ok when optimizing
3246 reads but not when called from the DSE code during
3247 elimination. */
3248 if (val
3249 && type != vr->type)
3251 if (! int_fits_type_p (val, vr->type))
3252 val = NULL_TREE;
3253 else
3254 val = fold_convert (vr->type, val);
3257 if (val)
3258 return data->finish (ao_ref_alias_set (&lhs_ref),
3259 ao_ref_base_alias_set (&lhs_ref), val);
3262 else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3263 size2i))
3265 pd_data pd;
3266 tree rhs = gimple_assign_rhs1 (def_stmt);
3267 if (TREE_CODE (rhs) == SSA_NAME)
3268 rhs = SSA_VAL (rhs);
3269 pd.rhs = rhs;
3270 pd.rhs_off = 0;
3271 pd.offset = offset2i;
3272 pd.size = size2i;
3273 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3274 ao_ref_base_alias_set (&lhs_ref),
3275 offseti, maxsizei);
3280 /* 4) Assignment from an SSA name which definition we may be able
3281 to access pieces from or we can combine to a larger entity. */
3282 else if (known_eq (ref->size, maxsize)
3283 && is_gimple_reg_type (vr->type)
3284 && !reverse_storage_order_for_component_p (vr->operands)
3285 && !contains_storage_order_barrier_p (vr->operands)
3286 && gimple_assign_single_p (def_stmt)
3287 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3289 tree lhs = gimple_assign_lhs (def_stmt);
3290 tree base2;
3291 poly_int64 offset2, size2, maxsize2;
3292 HOST_WIDE_INT offset2i, size2i, offseti;
3293 bool reverse;
3294 gcc_assert (lhs_ref_ok);
3295 base2 = ao_ref_base (&lhs_ref);
3296 offset2 = lhs_ref.offset;
3297 size2 = lhs_ref.size;
3298 maxsize2 = lhs_ref.max_size;
3299 reverse = reverse_storage_order_for_component_p (lhs);
3300 tree def_rhs = gimple_assign_rhs1 (def_stmt);
3301 if (!reverse
3302 && !storage_order_barrier_p (lhs)
3303 && known_size_p (maxsize2)
3304 && known_eq (maxsize2, size2)
3305 && adjust_offsets_for_equal_base_address (base, &offset,
3306 base2, &offset2))
3308 if (data->partial_defs.is_empty ()
3309 && known_subrange_p (offset, maxsize, offset2, size2)
3310 /* ??? We can't handle bitfield precision extracts without
3311 either using an alternate type for the BIT_FIELD_REF and
3312 then doing a conversion or possibly adjusting the offset
3313 according to endianness. */
3314 && (! INTEGRAL_TYPE_P (vr->type)
3315 || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3316 && multiple_p (ref->size, BITS_PER_UNIT))
3318 tree val = NULL_TREE;
3319 if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3320 || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3322 gimple_match_op op (gimple_match_cond::UNCOND,
3323 BIT_FIELD_REF, vr->type,
3324 SSA_VAL (def_rhs),
3325 bitsize_int (ref->size),
3326 bitsize_int (offset - offset2));
3327 val = vn_nary_build_or_lookup (&op);
3329 else if (known_eq (ref->size, size2))
3331 gimple_match_op op (gimple_match_cond::UNCOND,
3332 VIEW_CONVERT_EXPR, vr->type,
3333 SSA_VAL (def_rhs));
3334 val = vn_nary_build_or_lookup (&op);
3336 if (val
3337 && (TREE_CODE (val) != SSA_NAME
3338 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3339 return data->finish (ao_ref_alias_set (&lhs_ref),
3340 ao_ref_base_alias_set (&lhs_ref), val);
3342 else if (maxsize.is_constant (&maxsizei)
3343 && offset.is_constant (&offseti)
3344 && offset2.is_constant (&offset2i)
3345 && size2.is_constant (&size2i)
3346 && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3348 pd_data pd;
3349 pd.rhs = SSA_VAL (def_rhs);
3350 pd.rhs_off = 0;
3351 pd.offset = offset2i;
3352 pd.size = size2i;
3353 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3354 ao_ref_base_alias_set (&lhs_ref),
3355 offseti, maxsizei);
3360 /* 4b) Assignment done via one of the vectorizer internal store
3361 functions where we may be able to access pieces from or we can
3362 combine to a larger entity. */
3363 else if (known_eq (ref->size, maxsize)
3364 && is_gimple_reg_type (vr->type)
3365 && !reverse_storage_order_for_component_p (vr->operands)
3366 && !contains_storage_order_barrier_p (vr->operands)
3367 && is_gimple_call (def_stmt)
3368 && gimple_call_internal_p (def_stmt)
3369 && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3371 gcall *call = as_a <gcall *> (def_stmt);
3372 internal_fn fn = gimple_call_internal_fn (call);
3374 tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3375 switch (fn)
3377 case IFN_MASK_STORE:
3378 mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3379 mask = vn_valueize (mask);
3380 if (TREE_CODE (mask) != VECTOR_CST)
3381 return (void *)-1;
3382 break;
3383 case IFN_LEN_STORE:
3385 int len_index = internal_fn_len_index (fn);
3386 len = gimple_call_arg (call, len_index);
3387 bias = gimple_call_arg (call, len_index + 1);
3388 if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3389 return (void *) -1;
3390 break;
3392 default:
3393 return (void *)-1;
3395 tree def_rhs = gimple_call_arg (call,
3396 internal_fn_stored_value_index (fn));
3397 def_rhs = vn_valueize (def_rhs);
3398 if (TREE_CODE (def_rhs) != VECTOR_CST)
3399 return (void *)-1;
3401 ao_ref_init_from_ptr_and_size (&lhs_ref,
3402 vn_valueize (gimple_call_arg (call, 0)),
3403 TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3404 tree base2;
3405 poly_int64 offset2, size2, maxsize2;
3406 HOST_WIDE_INT offset2i, size2i, offseti;
3407 base2 = ao_ref_base (&lhs_ref);
3408 offset2 = lhs_ref.offset;
3409 size2 = lhs_ref.size;
3410 maxsize2 = lhs_ref.max_size;
3411 if (known_size_p (maxsize2)
3412 && known_eq (maxsize2, size2)
3413 && adjust_offsets_for_equal_base_address (base, &offset,
3414 base2, &offset2)
3415 && maxsize.is_constant (&maxsizei)
3416 && offset.is_constant (&offseti)
3417 && offset2.is_constant (&offset2i)
3418 && size2.is_constant (&size2i))
3420 if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3421 /* Poor-mans disambiguation. */
3422 return NULL;
3423 else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3425 pd_data pd;
3426 pd.rhs = def_rhs;
3427 tree aa = gimple_call_arg (call, 1);
3428 alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3429 tree vectype = TREE_TYPE (def_rhs);
3430 unsigned HOST_WIDE_INT elsz
3431 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3432 if (mask)
3434 HOST_WIDE_INT start = 0, length = 0;
3435 unsigned mask_idx = 0;
3438 if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3440 if (length != 0)
3442 pd.rhs_off = start;
3443 pd.offset = offset2i + start;
3444 pd.size = length;
3445 if (ranges_known_overlap_p
3446 (offset, maxsize, pd.offset, pd.size))
3448 void *res = data->push_partial_def
3449 (pd, set, set, offseti, maxsizei);
3450 if (res != NULL)
3451 return res;
3454 start = (mask_idx + 1) * elsz;
3455 length = 0;
3457 else
3458 length += elsz;
3459 mask_idx++;
3461 while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3462 if (length != 0)
3464 pd.rhs_off = start;
3465 pd.offset = offset2i + start;
3466 pd.size = length;
3467 if (ranges_known_overlap_p (offset, maxsize,
3468 pd.offset, pd.size))
3469 return data->push_partial_def (pd, set, set,
3470 offseti, maxsizei);
3473 else if (fn == IFN_LEN_STORE)
3475 pd.offset = offset2i;
3476 pd.size = (tree_to_uhwi (len)
3477 + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3478 if (BYTES_BIG_ENDIAN)
3479 pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3480 else
3481 pd.rhs_off = 0;
3482 if (ranges_known_overlap_p (offset, maxsize,
3483 pd.offset, pd.size))
3484 return data->push_partial_def (pd, set, set,
3485 offseti, maxsizei);
3487 else
3488 gcc_unreachable ();
3489 return NULL;
3494 /* 5) For aggregate copies translate the reference through them if
3495 the copy kills ref. */
3496 else if (data->vn_walk_kind == VN_WALKREWRITE
3497 && gimple_assign_single_p (def_stmt)
3498 && (DECL_P (gimple_assign_rhs1 (def_stmt))
3499 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3500 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3502 tree base2;
3503 int i, j, k;
3504 auto_vec<vn_reference_op_s> rhs;
3505 vn_reference_op_t vro;
3506 ao_ref r;
3508 gcc_assert (lhs_ref_ok);
3510 /* See if the assignment kills REF. */
3511 base2 = ao_ref_base (&lhs_ref);
3512 if (!lhs_ref.max_size_known_p ()
3513 || (base != base2
3514 && (TREE_CODE (base) != MEM_REF
3515 || TREE_CODE (base2) != MEM_REF
3516 || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3517 || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3518 TREE_OPERAND (base2, 1))))
3519 || !stmt_kills_ref_p (def_stmt, ref))
3520 return (void *)-1;
3522 /* Find the common base of ref and the lhs. lhs_ops already
3523 contains valueized operands for the lhs. */
3524 i = vr->operands.length () - 1;
3525 j = lhs_ops.length () - 1;
3526 while (j >= 0 && i >= 0
3527 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3529 i--;
3530 j--;
3533 /* ??? The innermost op should always be a MEM_REF and we already
3534 checked that the assignment to the lhs kills vr. Thus for
3535 aggregate copies using char[] types the vn_reference_op_eq
3536 may fail when comparing types for compatibility. But we really
3537 don't care here - further lookups with the rewritten operands
3538 will simply fail if we messed up types too badly. */
3539 poly_int64 extra_off = 0;
3540 if (j == 0 && i >= 0
3541 && lhs_ops[0].opcode == MEM_REF
3542 && maybe_ne (lhs_ops[0].off, -1))
3544 if (known_eq (lhs_ops[0].off, vr->operands[i].off))
3545 i--, j--;
3546 else if (vr->operands[i].opcode == MEM_REF
3547 && maybe_ne (vr->operands[i].off, -1))
3549 extra_off = vr->operands[i].off - lhs_ops[0].off;
3550 i--, j--;
3554 /* i now points to the first additional op.
3555 ??? LHS may not be completely contained in VR, one or more
3556 VIEW_CONVERT_EXPRs could be in its way. We could at least
3557 try handling outermost VIEW_CONVERT_EXPRs. */
3558 if (j != -1)
3559 return (void *)-1;
3561 /* Punt if the additional ops contain a storage order barrier. */
3562 for (k = i; k >= 0; k--)
3564 vro = &vr->operands[k];
3565 if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3566 return (void *)-1;
3569 /* Now re-write REF to be based on the rhs of the assignment. */
3570 tree rhs1 = gimple_assign_rhs1 (def_stmt);
3571 copy_reference_ops_from_ref (rhs1, &rhs);
3573 /* Apply an extra offset to the inner MEM_REF of the RHS. */
3574 bool force_no_tbaa = false;
3575 if (maybe_ne (extra_off, 0))
3577 if (rhs.length () < 2)
3578 return (void *)-1;
3579 int ix = rhs.length () - 2;
3580 if (rhs[ix].opcode != MEM_REF
3581 || known_eq (rhs[ix].off, -1))
3582 return (void *)-1;
3583 rhs[ix].off += extra_off;
3584 rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3585 build_int_cst (TREE_TYPE (rhs[ix].op0),
3586 extra_off));
3587 /* When we have offsetted the RHS, reading only parts of it,
3588 we can no longer use the original TBAA type, force alias-set
3589 zero. */
3590 force_no_tbaa = true;
3593 /* Save the operands since we need to use the original ones for
3594 the hash entry we use. */
3595 if (!data->saved_operands.exists ())
3596 data->saved_operands = vr->operands.copy ();
3598 /* We need to pre-pend vr->operands[0..i] to rhs. */
3599 vec<vn_reference_op_s> old = vr->operands;
3600 if (i + 1 + rhs.length () > vr->operands.length ())
3601 vr->operands.safe_grow (i + 1 + rhs.length (), true);
3602 else
3603 vr->operands.truncate (i + 1 + rhs.length ());
3604 FOR_EACH_VEC_ELT (rhs, j, vro)
3605 vr->operands[i + 1 + j] = *vro;
3606 valueize_refs (&vr->operands);
3607 if (old == shared_lookup_references)
3608 shared_lookup_references = vr->operands;
3609 vr->hashcode = vn_reference_compute_hash (vr);
3611 /* Try folding the new reference to a constant. */
3612 tree val = fully_constant_vn_reference_p (vr);
3613 if (val)
3615 if (data->partial_defs.is_empty ())
3616 return data->finish (ao_ref_alias_set (&lhs_ref),
3617 ao_ref_base_alias_set (&lhs_ref), val);
3618 /* This is the only interesting case for partial-def handling
3619 coming from targets that like to gimplify init-ctors as
3620 aggregate copies from constant data like aarch64 for
3621 PR83518. */
3622 if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3624 pd_data pd;
3625 pd.rhs = val;
3626 pd.rhs_off = 0;
3627 pd.offset = 0;
3628 pd.size = maxsizei;
3629 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3630 ao_ref_base_alias_set (&lhs_ref),
3631 0, maxsizei);
3635 /* Continuing with partial defs isn't easily possible here, we
3636 have to find a full def from further lookups from here. Probably
3637 not worth the special-casing everywhere. */
3638 if (!data->partial_defs.is_empty ())
3639 return (void *)-1;
3641 /* Adjust *ref from the new operands. */
3642 ao_ref rhs1_ref;
3643 ao_ref_init (&rhs1_ref, rhs1);
3644 basic_block saved_rpo_bb = vn_context_bb;
3645 vn_context_bb = gimple_bb (def_stmt);
3646 if (!ao_ref_init_from_vn_reference (&r,
3647 force_no_tbaa ? 0
3648 : ao_ref_alias_set (&rhs1_ref),
3649 force_no_tbaa ? 0
3650 : ao_ref_base_alias_set (&rhs1_ref),
3651 vr->type, vr->operands))
3653 vn_context_bb = saved_rpo_bb;
3654 return (void *)-1;
3656 vn_context_bb = saved_rpo_bb;
3657 /* This can happen with bitfields. */
3658 if (maybe_ne (ref->size, r.size))
3660 /* If the access lacks some subsetting simply apply that by
3661 shortening it. That in the end can only be successful
3662 if we can pun the lookup result which in turn requires
3663 exact offsets. */
3664 if (known_eq (r.size, r.max_size)
3665 && known_lt (ref->size, r.size))
3666 r.size = r.max_size = ref->size;
3667 else
3668 return (void *)-1;
3670 *ref = r;
3671 vr->offset = r.offset;
3672 vr->max_size = r.max_size;
3674 /* Do not update last seen VUSE after translating. */
3675 data->last_vuse_ptr = NULL;
3676 /* Invalidate the original access path since it now contains
3677 the wrong base. */
3678 data->orig_ref.ref = NULL_TREE;
3679 /* Use the alias-set of this LHS for recording an eventual result. */
3680 if (data->first_set == -2)
3682 data->first_set = ao_ref_alias_set (&lhs_ref);
3683 data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3686 /* Keep looking for the adjusted *REF / VR pair. */
3687 return NULL;
3690 /* 6) For memcpy copies translate the reference through them if the copy
3691 kills ref. But we cannot (easily) do this translation if the memcpy is
3692 a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3693 can modify the storage order of objects (see storage_order_barrier_p). */
3694 else if (data->vn_walk_kind == VN_WALKREWRITE
3695 && is_gimple_reg_type (vr->type)
3696 /* ??? Handle BCOPY as well. */
3697 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3698 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3699 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3700 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3701 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3702 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3703 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3704 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3705 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3706 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3707 && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), &copy_size)
3708 || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3709 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3710 &copy_size)))
3711 /* Handling this is more complicated, give up for now. */
3712 && data->partial_defs.is_empty ())
3714 tree lhs, rhs;
3715 ao_ref r;
3716 poly_int64 rhs_offset, lhs_offset;
3717 vn_reference_op_s op;
3718 poly_uint64 mem_offset;
3719 poly_int64 at, byte_maxsize;
3721 /* Only handle non-variable, addressable refs. */
3722 if (maybe_ne (ref->size, maxsize)
3723 || !multiple_p (offset, BITS_PER_UNIT, &at)
3724 || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3725 return (void *)-1;
3727 /* Extract a pointer base and an offset for the destination. */
3728 lhs = gimple_call_arg (def_stmt, 0);
3729 lhs_offset = 0;
3730 if (TREE_CODE (lhs) == SSA_NAME)
3732 lhs = vn_valueize (lhs);
3733 if (TREE_CODE (lhs) == SSA_NAME)
3735 gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3736 if (gimple_assign_single_p (def_stmt)
3737 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3738 lhs = gimple_assign_rhs1 (def_stmt);
3741 if (TREE_CODE (lhs) == ADDR_EXPR)
3743 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3744 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3745 return (void *)-1;
3746 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3747 &lhs_offset);
3748 if (!tem)
3749 return (void *)-1;
3750 if (TREE_CODE (tem) == MEM_REF
3751 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3753 lhs = TREE_OPERAND (tem, 0);
3754 if (TREE_CODE (lhs) == SSA_NAME)
3755 lhs = vn_valueize (lhs);
3756 lhs_offset += mem_offset;
3758 else if (DECL_P (tem))
3759 lhs = build_fold_addr_expr (tem);
3760 else
3761 return (void *)-1;
3763 if (TREE_CODE (lhs) != SSA_NAME
3764 && TREE_CODE (lhs) != ADDR_EXPR)
3765 return (void *)-1;
3767 /* Extract a pointer base and an offset for the source. */
3768 rhs = gimple_call_arg (def_stmt, 1);
3769 rhs_offset = 0;
3770 if (TREE_CODE (rhs) == SSA_NAME)
3771 rhs = vn_valueize (rhs);
3772 if (TREE_CODE (rhs) == ADDR_EXPR)
3774 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3775 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3776 return (void *)-1;
3777 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3778 &rhs_offset);
3779 if (!tem)
3780 return (void *)-1;
3781 if (TREE_CODE (tem) == MEM_REF
3782 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3784 rhs = TREE_OPERAND (tem, 0);
3785 rhs_offset += mem_offset;
3787 else if (DECL_P (tem)
3788 || TREE_CODE (tem) == STRING_CST)
3789 rhs = build_fold_addr_expr (tem);
3790 else
3791 return (void *)-1;
3793 if (TREE_CODE (rhs) == SSA_NAME)
3794 rhs = SSA_VAL (rhs);
3795 else if (TREE_CODE (rhs) != ADDR_EXPR)
3796 return (void *)-1;
3798 /* The bases of the destination and the references have to agree. */
3799 if (TREE_CODE (base) == MEM_REF)
3801 if (TREE_OPERAND (base, 0) != lhs
3802 || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
3803 return (void *) -1;
3804 at += mem_offset;
3806 else if (!DECL_P (base)
3807 || TREE_CODE (lhs) != ADDR_EXPR
3808 || TREE_OPERAND (lhs, 0) != base)
3809 return (void *)-1;
3811 /* If the access is completely outside of the memcpy destination
3812 area there is no aliasing. */
3813 if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
3814 return NULL;
3815 /* And the access has to be contained within the memcpy destination. */
3816 if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
3817 return (void *)-1;
3819 /* Save the operands since we need to use the original ones for
3820 the hash entry we use. */
3821 if (!data->saved_operands.exists ())
3822 data->saved_operands = vr->operands.copy ();
3824 /* Make room for 2 operands in the new reference. */
3825 if (vr->operands.length () < 2)
3827 vec<vn_reference_op_s> old = vr->operands;
3828 vr->operands.safe_grow_cleared (2, true);
3829 if (old == shared_lookup_references)
3830 shared_lookup_references = vr->operands;
3832 else
3833 vr->operands.truncate (2);
3835 /* The looked-through reference is a simple MEM_REF. */
3836 memset (&op, 0, sizeof (op));
3837 op.type = vr->type;
3838 op.opcode = MEM_REF;
3839 op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3840 op.off = at - lhs_offset + rhs_offset;
3841 vr->operands[0] = op;
3842 op.type = TREE_TYPE (rhs);
3843 op.opcode = TREE_CODE (rhs);
3844 op.op0 = rhs;
3845 op.off = -1;
3846 vr->operands[1] = op;
3847 vr->hashcode = vn_reference_compute_hash (vr);
3849 /* Try folding the new reference to a constant. */
3850 tree val = fully_constant_vn_reference_p (vr);
3851 if (val)
3852 return data->finish (0, 0, val);
3854 /* Adjust *ref from the new operands. */
3855 basic_block saved_rpo_bb = vn_context_bb;
3856 vn_context_bb = gimple_bb (def_stmt);
3857 if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
3859 vn_context_bb = saved_rpo_bb;
3860 return (void *)-1;
3862 vn_context_bb = saved_rpo_bb;
3863 /* This can happen with bitfields. */
3864 if (maybe_ne (ref->size, r.size))
3865 return (void *)-1;
3866 *ref = r;
3867 vr->offset = r.offset;
3868 vr->max_size = r.max_size;
3870 /* Do not update last seen VUSE after translating. */
3871 data->last_vuse_ptr = NULL;
3872 /* Invalidate the original access path since it now contains
3873 the wrong base. */
3874 data->orig_ref.ref = NULL_TREE;
3875 /* Use the alias-set of this stmt for recording an eventual result. */
3876 if (data->first_set == -2)
3878 data->first_set = 0;
3879 data->first_base_set = 0;
3882 /* Keep looking for the adjusted *REF / VR pair. */
3883 return NULL;
3886 /* Bail out and stop walking. */
3887 return (void *)-1;
3890 /* Return a reference op vector from OP that can be used for
3891 vn_reference_lookup_pieces. The caller is responsible for releasing
3892 the vector. */
3894 vec<vn_reference_op_s>
3895 vn_reference_operands_for_lookup (tree op)
3897 bool valueized;
3898 return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
3901 /* Lookup a reference operation by it's parts, in the current hash table.
3902 Returns the resulting value number if it exists in the hash table,
3903 NULL_TREE otherwise. VNRESULT will be filled in with the actual
3904 vn_reference_t stored in the hashtable if something is found. */
3906 tree
3907 vn_reference_lookup_pieces (tree vuse, alias_set_type set,
3908 alias_set_type base_set, tree type,
3909 vec<vn_reference_op_s> operands,
3910 vn_reference_t *vnresult, vn_lookup_kind kind)
3912 struct vn_reference_s vr1;
3913 vn_reference_t tmp;
3914 tree cst;
3916 if (!vnresult)
3917 vnresult = &tmp;
3918 *vnresult = NULL;
3920 vr1.vuse = vuse_ssa_val (vuse);
3921 shared_lookup_references.truncate (0);
3922 shared_lookup_references.safe_grow (operands.length (), true);
3923 memcpy (shared_lookup_references.address (),
3924 operands.address (),
3925 sizeof (vn_reference_op_s)
3926 * operands.length ());
3927 bool valueized_p;
3928 valueize_refs_1 (&shared_lookup_references, &valueized_p);
3929 vr1.operands = shared_lookup_references;
3930 vr1.type = type;
3931 vr1.set = set;
3932 vr1.base_set = base_set;
3933 /* We can pretend there's no extra info fed in since the ao_refs offset
3934 and max_size are computed only from the VN reference ops. */
3935 vr1.offset = 0;
3936 vr1.max_size = -1;
3937 vr1.hashcode = vn_reference_compute_hash (&vr1);
3938 if ((cst = fully_constant_vn_reference_p (&vr1)))
3939 return cst;
3941 vn_reference_lookup_1 (&vr1, vnresult);
3942 if (!*vnresult
3943 && kind != VN_NOWALK
3944 && vr1.vuse)
3946 ao_ref r;
3947 unsigned limit = param_sccvn_max_alias_queries_per_access;
3948 vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
3949 false);
3950 if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
3951 vr1.operands))
3952 *vnresult
3953 = ((vn_reference_t)
3954 walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
3955 vn_reference_lookup_3, vuse_valueize,
3956 limit, &data));
3957 gcc_checking_assert (vr1.operands == shared_lookup_references);
3958 if (*vnresult
3959 && data.same_val
3960 && (!(*vnresult)->result
3961 || !operand_equal_p ((*vnresult)->result, data.same_val)))
3963 *vnresult = NULL;
3964 return NULL_TREE;
3968 if (*vnresult)
3969 return (*vnresult)->result;
3971 return NULL_TREE;
3974 /* Lookup OP in the current hash table, and return the resulting value
3975 number if it exists in the hash table. Return NULL_TREE if it does
3976 not exist in the hash table or if the result field of the structure
3977 was NULL.. VNRESULT will be filled in with the vn_reference_t
3978 stored in the hashtable if one exists. When TBAA_P is false assume
3979 we are looking up a store and treat it as having alias-set zero.
3980 *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
3981 MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
3982 load is bitwise anded with MASK and so we are only interested in a subset
3983 of the bits and can ignore if the other bits are uninitialized or
3984 not initialized with constants. When doing redundant store removal
3985 the caller has to set REDUNDANT_STORE_REMOVAL_P. */
3987 tree
3988 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
3989 vn_reference_t *vnresult, bool tbaa_p,
3990 tree *last_vuse_ptr, tree mask,
3991 bool redundant_store_removal_p)
3993 vec<vn_reference_op_s> operands;
3994 struct vn_reference_s vr1;
3995 bool valueized_anything;
3997 if (vnresult)
3998 *vnresult = NULL;
4000 vr1.vuse = vuse_ssa_val (vuse);
4001 vr1.operands = operands
4002 = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
4004 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
4005 this before the pass folding __builtin_object_size had a chance to run. */
4006 if ((cfun->curr_properties & PROP_objsz)
4007 && operands[0].opcode == ADDR_EXPR
4008 && operands.last ().opcode == SSA_NAME)
4010 poly_int64 off = 0;
4011 vn_reference_op_t vro;
4012 unsigned i;
4013 for (i = 1; operands.iterate (i, &vro); ++i)
4015 if (vro->opcode == SSA_NAME)
4016 break;
4017 else if (known_eq (vro->off, -1))
4018 break;
4019 off += vro->off;
4021 if (i == operands.length () - 1
4022 /* Make sure we the offset we accumulated in a 64bit int
4023 fits the address computation carried out in target
4024 offset precision. */
4025 && (off.coeffs[0]
4026 == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4028 gcc_assert (operands[i-1].opcode == MEM_REF);
4029 tree ops[2];
4030 ops[0] = operands[i].op0;
4031 ops[1] = wide_int_to_tree (sizetype, off);
4032 tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4033 TREE_TYPE (op), ops, NULL);
4034 if (res)
4035 return res;
4036 return NULL_TREE;
4040 vr1.type = TREE_TYPE (op);
4041 ao_ref op_ref;
4042 ao_ref_init (&op_ref, op);
4043 vr1.set = ao_ref_alias_set (&op_ref);
4044 vr1.base_set = ao_ref_base_alias_set (&op_ref);
4045 vr1.offset = 0;
4046 vr1.max_size = -1;
4047 vr1.hashcode = vn_reference_compute_hash (&vr1);
4048 if (mask == NULL_TREE)
4049 if (tree cst = fully_constant_vn_reference_p (&vr1))
4050 return cst;
4052 if (kind != VN_NOWALK && vr1.vuse)
4054 vn_reference_t wvnresult;
4055 ao_ref r;
4056 unsigned limit = param_sccvn_max_alias_queries_per_access;
4057 if (!valueized_anything
4058 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4059 vr1.type, vr1.operands))
4061 ao_ref_init (&r, op);
4062 /* Record the extra info we're getting from the full ref. */
4063 ao_ref_base (&r);
4064 vr1.offset = r.offset;
4065 vr1.max_size = r.max_size;
4067 vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4068 last_vuse_ptr, kind, tbaa_p, mask,
4069 redundant_store_removal_p);
4071 wvnresult
4072 = ((vn_reference_t)
4073 walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4074 vn_reference_lookup_3, vuse_valueize, limit,
4075 &data));
4076 gcc_checking_assert (vr1.operands == shared_lookup_references);
4077 if (wvnresult)
4079 gcc_assert (mask == NULL_TREE);
4080 if (data.same_val
4081 && (!wvnresult->result
4082 || !operand_equal_p (wvnresult->result, data.same_val)))
4083 return NULL_TREE;
4084 if (vnresult)
4085 *vnresult = wvnresult;
4086 return wvnresult->result;
4088 else if (mask)
4089 return data.masked_result;
4091 return NULL_TREE;
4094 if (last_vuse_ptr)
4095 *last_vuse_ptr = vr1.vuse;
4096 if (mask)
4097 return NULL_TREE;
4098 return vn_reference_lookup_1 (&vr1, vnresult);
4101 /* Lookup CALL in the current hash table and return the entry in
4102 *VNRESULT if found. Populates *VR for the hashtable lookup. */
4104 void
4105 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4106 vn_reference_t vr)
4108 if (vnresult)
4109 *vnresult = NULL;
4111 tree vuse = gimple_vuse (call);
4113 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4114 vr->operands = valueize_shared_reference_ops_from_call (call);
4115 tree lhs = gimple_call_lhs (call);
4116 /* For non-SSA return values the referece ops contain the LHS. */
4117 vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4118 ? TREE_TYPE (lhs) : NULL_TREE);
4119 vr->punned = false;
4120 vr->set = 0;
4121 vr->base_set = 0;
4122 vr->offset = 0;
4123 vr->max_size = -1;
4124 vr->hashcode = vn_reference_compute_hash (vr);
4125 vn_reference_lookup_1 (vr, vnresult);
4128 /* Insert OP into the current hash table with a value number of RESULT. */
4130 static void
4131 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4133 vn_reference_s **slot;
4134 vn_reference_t vr1;
4135 bool tem;
4137 vec<vn_reference_op_s> operands
4138 = valueize_shared_reference_ops_from_ref (op, &tem);
4139 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4140 before the pass folding __builtin_object_size had a chance to run. */
4141 if ((cfun->curr_properties & PROP_objsz)
4142 && operands[0].opcode == ADDR_EXPR
4143 && operands.last ().opcode == SSA_NAME)
4145 poly_int64 off = 0;
4146 vn_reference_op_t vro;
4147 unsigned i;
4148 for (i = 1; operands.iterate (i, &vro); ++i)
4150 if (vro->opcode == SSA_NAME)
4151 break;
4152 else if (known_eq (vro->off, -1))
4153 break;
4154 off += vro->off;
4156 if (i == operands.length () - 1
4157 /* Make sure we the offset we accumulated in a 64bit int
4158 fits the address computation carried out in target
4159 offset precision. */
4160 && (off.coeffs[0]
4161 == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4163 gcc_assert (operands[i-1].opcode == MEM_REF);
4164 tree ops[2];
4165 ops[0] = operands[i].op0;
4166 ops[1] = wide_int_to_tree (sizetype, off);
4167 vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4168 TREE_TYPE (op), ops, result,
4169 VN_INFO (result)->value_id);
4170 return;
4174 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4175 if (TREE_CODE (result) == SSA_NAME)
4176 vr1->value_id = VN_INFO (result)->value_id;
4177 else
4178 vr1->value_id = get_or_alloc_constant_value_id (result);
4179 vr1->vuse = vuse_ssa_val (vuse);
4180 vr1->operands = operands.copy ();
4181 vr1->type = TREE_TYPE (op);
4182 vr1->punned = false;
4183 ao_ref op_ref;
4184 ao_ref_init (&op_ref, op);
4185 vr1->set = ao_ref_alias_set (&op_ref);
4186 vr1->base_set = ao_ref_base_alias_set (&op_ref);
4187 /* Specifically use an unknown extent here, we're not doing any lookup
4188 and assume the caller didn't either (or it went VARYING). */
4189 vr1->offset = 0;
4190 vr1->max_size = -1;
4191 vr1->hashcode = vn_reference_compute_hash (vr1);
4192 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4193 vr1->result_vdef = vdef;
4195 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4196 INSERT);
4198 /* Because IL walking on reference lookup can end up visiting
4199 a def that is only to be visited later in iteration order
4200 when we are about to make an irreducible region reducible
4201 the def can be effectively processed and its ref being inserted
4202 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4203 but save a lookup if we deal with already inserted refs here. */
4204 if (*slot)
4206 /* We cannot assert that we have the same value either because
4207 when disentangling an irreducible region we may end up visiting
4208 a use before the corresponding def. That's a missed optimization
4209 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4210 if (dump_file && (dump_flags & TDF_DETAILS)
4211 && !operand_equal_p ((*slot)->result, vr1->result, 0))
4213 fprintf (dump_file, "Keeping old value ");
4214 print_generic_expr (dump_file, (*slot)->result);
4215 fprintf (dump_file, " because of collision\n");
4217 free_reference (vr1);
4218 obstack_free (&vn_tables_obstack, vr1);
4219 return;
4222 *slot = vr1;
4223 vr1->next = last_inserted_ref;
4224 last_inserted_ref = vr1;
4227 /* Insert a reference by it's pieces into the current hash table with
4228 a value number of RESULT. Return the resulting reference
4229 structure we created. */
4231 vn_reference_t
4232 vn_reference_insert_pieces (tree vuse, alias_set_type set,
4233 alias_set_type base_set,
4234 poly_int64 offset, poly_int64 max_size, tree type,
4235 vec<vn_reference_op_s> operands,
4236 tree result, unsigned int value_id)
4239 vn_reference_s **slot;
4240 vn_reference_t vr1;
4242 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4243 vr1->value_id = value_id;
4244 vr1->vuse = vuse_ssa_val (vuse);
4245 vr1->operands = operands;
4246 valueize_refs (&vr1->operands);
4247 vr1->type = type;
4248 vr1->punned = false;
4249 vr1->set = set;
4250 vr1->base_set = base_set;
4251 vr1->offset = offset;
4252 vr1->max_size = max_size;
4253 vr1->hashcode = vn_reference_compute_hash (vr1);
4254 if (result && TREE_CODE (result) == SSA_NAME)
4255 result = SSA_VAL (result);
4256 vr1->result = result;
4257 vr1->result_vdef = NULL_TREE;
4259 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4260 INSERT);
4262 /* At this point we should have all the things inserted that we have
4263 seen before, and we should never try inserting something that
4264 already exists. */
4265 gcc_assert (!*slot);
4267 *slot = vr1;
4268 vr1->next = last_inserted_ref;
4269 last_inserted_ref = vr1;
4270 return vr1;
4273 /* Compute and return the hash value for nary operation VBO1. */
4275 hashval_t
4276 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4278 inchash::hash hstate;
4279 unsigned i;
4281 if (((vno1->length == 2
4282 && commutative_tree_code (vno1->opcode))
4283 || (vno1->length == 3
4284 && commutative_ternary_tree_code (vno1->opcode)))
4285 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4286 std::swap (vno1->op[0], vno1->op[1]);
4287 else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4288 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4290 std::swap (vno1->op[0], vno1->op[1]);
4291 vno1->opcode = swap_tree_comparison (vno1->opcode);
4294 hstate.add_int (vno1->opcode);
4295 for (i = 0; i < vno1->length; ++i)
4296 inchash::add_expr (vno1->op[i], hstate);
4298 return hstate.end ();
4301 /* Compare nary operations VNO1 and VNO2 and return true if they are
4302 equivalent. */
4304 bool
4305 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4307 unsigned i;
4309 if (vno1->hashcode != vno2->hashcode)
4310 return false;
4312 if (vno1->length != vno2->length)
4313 return false;
4315 if (vno1->opcode != vno2->opcode
4316 || !types_compatible_p (vno1->type, vno2->type))
4317 return false;
4319 for (i = 0; i < vno1->length; ++i)
4320 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4321 return false;
4323 /* BIT_INSERT_EXPR has an implict operand as the type precision
4324 of op1. Need to check to make sure they are the same. */
4325 if (vno1->opcode == BIT_INSERT_EXPR
4326 && TREE_CODE (vno1->op[1]) == INTEGER_CST
4327 && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4328 != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4329 return false;
4331 return true;
4334 /* Initialize VNO from the pieces provided. */
4336 static void
4337 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4338 enum tree_code code, tree type, tree *ops)
4340 vno->opcode = code;
4341 vno->length = length;
4342 vno->type = type;
4343 memcpy (&vno->op[0], ops, sizeof (tree) * length);
4346 /* Return the number of operands for a vn_nary ops structure from STMT. */
4348 unsigned int
4349 vn_nary_length_from_stmt (gimple *stmt)
4351 switch (gimple_assign_rhs_code (stmt))
4353 case REALPART_EXPR:
4354 case IMAGPART_EXPR:
4355 case VIEW_CONVERT_EXPR:
4356 return 1;
4358 case BIT_FIELD_REF:
4359 return 3;
4361 case CONSTRUCTOR:
4362 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4364 default:
4365 return gimple_num_ops (stmt) - 1;
4369 /* Initialize VNO from STMT. */
4371 void
4372 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4374 unsigned i;
4376 vno->opcode = gimple_assign_rhs_code (stmt);
4377 vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4378 switch (vno->opcode)
4380 case REALPART_EXPR:
4381 case IMAGPART_EXPR:
4382 case VIEW_CONVERT_EXPR:
4383 vno->length = 1;
4384 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4385 break;
4387 case BIT_FIELD_REF:
4388 vno->length = 3;
4389 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4390 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4391 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4392 break;
4394 case CONSTRUCTOR:
4395 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4396 for (i = 0; i < vno->length; ++i)
4397 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4398 break;
4400 default:
4401 gcc_checking_assert (!gimple_assign_single_p (stmt));
4402 vno->length = gimple_num_ops (stmt) - 1;
4403 for (i = 0; i < vno->length; ++i)
4404 vno->op[i] = gimple_op (stmt, i + 1);
4408 /* Compute the hashcode for VNO and look for it in the hash table;
4409 return the resulting value number if it exists in the hash table.
4410 Return NULL_TREE if it does not exist in the hash table or if the
4411 result field of the operation is NULL. VNRESULT will contain the
4412 vn_nary_op_t from the hashtable if it exists. */
4414 static tree
4415 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4417 vn_nary_op_s **slot;
4419 if (vnresult)
4420 *vnresult = NULL;
4422 for (unsigned i = 0; i < vno->length; ++i)
4423 if (TREE_CODE (vno->op[i]) == SSA_NAME)
4424 vno->op[i] = SSA_VAL (vno->op[i]);
4426 vno->hashcode = vn_nary_op_compute_hash (vno);
4427 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4428 if (!slot)
4429 return NULL_TREE;
4430 if (vnresult)
4431 *vnresult = *slot;
4432 return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4435 /* Lookup a n-ary operation by its pieces and return the resulting value
4436 number if it exists in the hash table. Return NULL_TREE if it does
4437 not exist in the hash table or if the result field of the operation
4438 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4439 if it exists. */
4441 tree
4442 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4443 tree type, tree *ops, vn_nary_op_t *vnresult)
4445 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4446 sizeof_vn_nary_op (length));
4447 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4448 return vn_nary_op_lookup_1 (vno1, vnresult);
4451 /* Lookup the rhs of STMT in the current hash table, and return the resulting
4452 value number if it exists in the hash table. Return NULL_TREE if
4453 it does not exist in the hash table. VNRESULT will contain the
4454 vn_nary_op_t from the hashtable if it exists. */
4456 tree
4457 vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4459 vn_nary_op_t vno1
4460 = XALLOCAVAR (struct vn_nary_op_s,
4461 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4462 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4463 return vn_nary_op_lookup_1 (vno1, vnresult);
4466 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4468 vn_nary_op_t
4469 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4471 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4474 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4475 obstack. */
4477 static vn_nary_op_t
4478 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4480 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4482 vno1->value_id = value_id;
4483 vno1->length = length;
4484 vno1->predicated_values = 0;
4485 vno1->u.result = result;
4487 return vno1;
4490 /* Insert VNO into TABLE. */
4492 static vn_nary_op_t
4493 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4495 vn_nary_op_s **slot;
4497 gcc_assert (! vno->predicated_values
4498 || (! vno->u.values->next
4499 && vno->u.values->n == 1));
4501 for (unsigned i = 0; i < vno->length; ++i)
4502 if (TREE_CODE (vno->op[i]) == SSA_NAME)
4503 vno->op[i] = SSA_VAL (vno->op[i]);
4505 vno->hashcode = vn_nary_op_compute_hash (vno);
4506 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4507 vno->unwind_to = *slot;
4508 if (*slot)
4510 /* Prefer non-predicated values.
4511 ??? Only if those are constant, otherwise, with constant predicated
4512 value, turn them into predicated values with entry-block validity
4513 (??? but we always find the first valid result currently). */
4514 if ((*slot)->predicated_values
4515 && ! vno->predicated_values)
4517 /* ??? We cannot remove *slot from the unwind stack list.
4518 For the moment we deal with this by skipping not found
4519 entries but this isn't ideal ... */
4520 *slot = vno;
4521 /* ??? Maintain a stack of states we can unwind in
4522 vn_nary_op_s? But how far do we unwind? In reality
4523 we need to push change records somewhere... Or not
4524 unwind vn_nary_op_s and linking them but instead
4525 unwind the results "list", linking that, which also
4526 doesn't move on hashtable resize. */
4527 /* We can also have a ->unwind_to recording *slot there.
4528 That way we can make u.values a fixed size array with
4529 recording the number of entries but of course we then
4530 have always N copies for each unwind_to-state. Or we
4531 make sure to only ever append and each unwinding will
4532 pop off one entry (but how to deal with predicated
4533 replaced with non-predicated here?) */
4534 vno->next = last_inserted_nary;
4535 last_inserted_nary = vno;
4536 return vno;
4538 else if (vno->predicated_values
4539 && ! (*slot)->predicated_values)
4540 return *slot;
4541 else if (vno->predicated_values
4542 && (*slot)->predicated_values)
4544 /* ??? Factor this all into a insert_single_predicated_value
4545 routine. */
4546 gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4547 basic_block vno_bb
4548 = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4549 vn_pval *nval = vno->u.values;
4550 vn_pval **next = &vno->u.values;
4551 bool found = false;
4552 for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4554 if (expressions_equal_p (val->result, nval->result))
4556 found = true;
4557 for (unsigned i = 0; i < val->n; ++i)
4559 basic_block val_bb
4560 = BASIC_BLOCK_FOR_FN (cfun,
4561 val->valid_dominated_by_p[i]);
4562 if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4563 /* Value registered with more generic predicate. */
4564 return *slot;
4565 else if (flag_checking)
4566 /* Shouldn't happen, we insert in RPO order. */
4567 gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4568 val_bb, vno_bb));
4570 /* Append value. */
4571 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4572 sizeof (vn_pval)
4573 + val->n * sizeof (int));
4574 (*next)->next = NULL;
4575 (*next)->result = val->result;
4576 (*next)->n = val->n + 1;
4577 memcpy ((*next)->valid_dominated_by_p,
4578 val->valid_dominated_by_p,
4579 val->n * sizeof (int));
4580 (*next)->valid_dominated_by_p[val->n] = vno_bb->index;
4581 next = &(*next)->next;
4582 if (dump_file && (dump_flags & TDF_DETAILS))
4583 fprintf (dump_file, "Appending predicate to value.\n");
4584 continue;
4586 /* Copy other predicated values. */
4587 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4588 sizeof (vn_pval)
4589 + (val->n-1) * sizeof (int));
4590 memcpy (*next, val, sizeof (vn_pval) + (val->n-1) * sizeof (int));
4591 (*next)->next = NULL;
4592 next = &(*next)->next;
4594 if (!found)
4595 *next = nval;
4597 *slot = vno;
4598 vno->next = last_inserted_nary;
4599 last_inserted_nary = vno;
4600 return vno;
4603 /* While we do not want to insert things twice it's awkward to
4604 avoid it in the case where visit_nary_op pattern-matches stuff
4605 and ends up simplifying the replacement to itself. We then
4606 get two inserts, one from visit_nary_op and one from
4607 vn_nary_build_or_lookup.
4608 So allow inserts with the same value number. */
4609 if ((*slot)->u.result == vno->u.result)
4610 return *slot;
4613 /* ??? There's also optimistic vs. previous commited state merging
4614 that is problematic for the case of unwinding. */
4616 /* ??? We should return NULL if we do not use 'vno' and have the
4617 caller release it. */
4618 gcc_assert (!*slot);
4620 *slot = vno;
4621 vno->next = last_inserted_nary;
4622 last_inserted_nary = vno;
4623 return vno;
4626 /* Insert a n-ary operation into the current hash table using it's
4627 pieces. Return the vn_nary_op_t structure we created and put in
4628 the hashtable. */
4630 vn_nary_op_t
4631 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4632 tree type, tree *ops,
4633 tree result, unsigned int value_id)
4635 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4636 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4637 return vn_nary_op_insert_into (vno1, valid_info->nary);
4640 /* Return whether we can track a predicate valid when PRED_E is executed. */
4642 static bool
4643 can_track_predicate_on_edge (edge pred_e)
4645 /* ??? As we are currently recording the destination basic-block index in
4646 vn_pval.valid_dominated_by_p and using dominance for the
4647 validity check we cannot track predicates on all edges. */
4648 if (single_pred_p (pred_e->dest))
4649 return true;
4650 /* Never record for backedges. */
4651 if (pred_e->flags & EDGE_DFS_BACK)
4652 return false;
4653 /* When there's more than one predecessor we cannot track
4654 predicate validity based on the destination block. The
4655 exception is when all other incoming edges sources are
4656 dominated by the destination block. */
4657 edge_iterator ei;
4658 edge e;
4659 FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4660 if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4661 return false;
4662 return true;
4665 static vn_nary_op_t
4666 vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4667 tree type, tree *ops,
4668 tree result, unsigned int value_id,
4669 edge pred_e)
4671 gcc_assert (can_track_predicate_on_edge (pred_e));
4673 if (dump_file && (dump_flags & TDF_DETAILS)
4674 /* ??? Fix dumping, but currently we only get comparisons. */
4675 && TREE_CODE_CLASS (code) == tcc_comparison)
4677 fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4678 pred_e->dest->index);
4679 print_generic_expr (dump_file, ops[0], TDF_SLIM);
4680 fprintf (dump_file, " %s ", get_tree_code_name (code));
4681 print_generic_expr (dump_file, ops[1], TDF_SLIM);
4682 fprintf (dump_file, " == %s\n",
4683 integer_zerop (result) ? "false" : "true");
4685 vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4686 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4687 vno1->predicated_values = 1;
4688 vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4689 sizeof (vn_pval));
4690 vno1->u.values->next = NULL;
4691 vno1->u.values->result = result;
4692 vno1->u.values->n = 1;
4693 vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4694 return vn_nary_op_insert_into (vno1, valid_info->nary);
4697 static bool
4698 dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4700 static tree
4701 vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
4702 edge e = NULL)
4704 if (! vno->predicated_values)
4705 return vno->u.result;
4706 for (vn_pval *val = vno->u.values; val; val = val->next)
4707 for (unsigned i = 0; i < val->n; ++i)
4709 basic_block cand
4710 = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
4711 /* Do not handle backedge executability optimistically since
4712 when figuring out whether to iterate we do not consider
4713 changed predication.
4714 When asking for predicated values on an edge avoid looking
4715 at edge executability for edges forward in our iteration
4716 as well. */
4717 if (e && (e->flags & EDGE_DFS_BACK))
4719 if (dominated_by_p (CDI_DOMINATORS, bb, cand))
4720 return val->result;
4722 else if (dominated_by_p_w_unex (bb, cand, false))
4723 return val->result;
4725 return NULL_TREE;
4728 static tree
4729 vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
4731 return vn_nary_op_get_predicated_value (vno, e->src, e);
4734 /* Insert the rhs of STMT into the current hash table with a value number of
4735 RESULT. */
4737 static vn_nary_op_t
4738 vn_nary_op_insert_stmt (gimple *stmt, tree result)
4740 vn_nary_op_t vno1
4741 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
4742 result, VN_INFO (result)->value_id);
4743 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4744 return vn_nary_op_insert_into (vno1, valid_info->nary);
4747 /* Compute a hashcode for PHI operation VP1 and return it. */
4749 static inline hashval_t
4750 vn_phi_compute_hash (vn_phi_t vp1)
4752 inchash::hash hstate;
4753 tree phi1op;
4754 tree type;
4755 edge e;
4756 edge_iterator ei;
4758 hstate.add_int (EDGE_COUNT (vp1->block->preds));
4759 switch (EDGE_COUNT (vp1->block->preds))
4761 case 1:
4762 break;
4763 case 2:
4764 /* When this is a PHI node subject to CSE for different blocks
4765 avoid hashing the block index. */
4766 if (vp1->cclhs)
4767 break;
4768 /* Fallthru. */
4769 default:
4770 hstate.add_int (vp1->block->index);
4773 /* If all PHI arguments are constants we need to distinguish
4774 the PHI node via its type. */
4775 type = vp1->type;
4776 hstate.merge_hash (vn_hash_type (type));
4778 FOR_EACH_EDGE (e, ei, vp1->block->preds)
4780 /* Don't hash backedge values they need to be handled as VN_TOP
4781 for optimistic value-numbering. */
4782 if (e->flags & EDGE_DFS_BACK)
4783 continue;
4785 phi1op = vp1->phiargs[e->dest_idx];
4786 if (phi1op == VN_TOP)
4787 continue;
4788 inchash::add_expr (phi1op, hstate);
4791 return hstate.end ();
4795 /* Return true if COND1 and COND2 represent the same condition, set
4796 *INVERTED_P if one needs to be inverted to make it the same as
4797 the other. */
4799 static bool
4800 cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
4801 gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
4803 enum tree_code code1 = gimple_cond_code (cond1);
4804 enum tree_code code2 = gimple_cond_code (cond2);
4806 *inverted_p = false;
4807 if (code1 == code2)
4809 else if (code1 == swap_tree_comparison (code2))
4810 std::swap (lhs2, rhs2);
4811 else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
4812 *inverted_p = true;
4813 else if (code1 == invert_tree_comparison
4814 (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
4816 std::swap (lhs2, rhs2);
4817 *inverted_p = true;
4819 else
4820 return false;
4822 return ((expressions_equal_p (lhs1, lhs2)
4823 && expressions_equal_p (rhs1, rhs2))
4824 || (commutative_tree_code (code1)
4825 && expressions_equal_p (lhs1, rhs2)
4826 && expressions_equal_p (rhs1, lhs2)));
4829 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4831 static int
4832 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
4834 if (vp1->hashcode != vp2->hashcode)
4835 return false;
4837 if (vp1->block != vp2->block)
4839 if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
4840 return false;
4842 switch (EDGE_COUNT (vp1->block->preds))
4844 case 1:
4845 /* Single-arg PHIs are just copies. */
4846 break;
4848 case 2:
4850 /* Make sure both PHIs are classified as CSEable. */
4851 if (! vp1->cclhs || ! vp2->cclhs)
4852 return false;
4854 /* Rule out backedges into the PHI. */
4855 gcc_checking_assert
4856 (vp1->block->loop_father->header != vp1->block
4857 && vp2->block->loop_father->header != vp2->block);
4859 /* If the PHI nodes do not have compatible types
4860 they are not the same. */
4861 if (!types_compatible_p (vp1->type, vp2->type))
4862 return false;
4864 /* If the immediate dominator end in switch stmts multiple
4865 values may end up in the same PHI arg via intermediate
4866 CFG merges. */
4867 basic_block idom1
4868 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4869 basic_block idom2
4870 = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
4871 gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
4872 && EDGE_COUNT (idom2->succs) == 2);
4874 /* Verify the controlling stmt is the same. */
4875 gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
4876 gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
4877 bool inverted_p;
4878 if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
4879 last2, vp2->cclhs, vp2->ccrhs,
4880 &inverted_p))
4881 return false;
4883 /* Get at true/false controlled edges into the PHI. */
4884 edge te1, te2, fe1, fe2;
4885 if (! extract_true_false_controlled_edges (idom1, vp1->block,
4886 &te1, &fe1)
4887 || ! extract_true_false_controlled_edges (idom2, vp2->block,
4888 &te2, &fe2))
4889 return false;
4891 /* Swap edges if the second condition is the inverted of the
4892 first. */
4893 if (inverted_p)
4894 std::swap (te2, fe2);
4896 /* Since we do not know which edge will be executed we have
4897 to be careful when matching VN_TOP. Be conservative and
4898 only match VN_TOP == VN_TOP for now, we could allow
4899 VN_TOP on the not prevailing PHI though. See for example
4900 PR102920. */
4901 if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
4902 vp2->phiargs[te2->dest_idx], false)
4903 || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
4904 vp2->phiargs[fe2->dest_idx], false))
4905 return false;
4907 return true;
4910 default:
4911 return false;
4915 /* If the PHI nodes do not have compatible types
4916 they are not the same. */
4917 if (!types_compatible_p (vp1->type, vp2->type))
4918 return false;
4920 /* Any phi in the same block will have it's arguments in the
4921 same edge order, because of how we store phi nodes. */
4922 unsigned nargs = EDGE_COUNT (vp1->block->preds);
4923 for (unsigned i = 0; i < nargs; ++i)
4925 tree phi1op = vp1->phiargs[i];
4926 tree phi2op = vp2->phiargs[i];
4927 if (phi1op == phi2op)
4928 continue;
4929 if (!expressions_equal_p (phi1op, phi2op, false))
4930 return false;
4933 return true;
4936 /* Lookup PHI in the current hash table, and return the resulting
4937 value number if it exists in the hash table. Return NULL_TREE if
4938 it does not exist in the hash table. */
4940 static tree
4941 vn_phi_lookup (gimple *phi, bool backedges_varying_p)
4943 vn_phi_s **slot;
4944 struct vn_phi_s *vp1;
4945 edge e;
4946 edge_iterator ei;
4948 vp1 = XALLOCAVAR (struct vn_phi_s,
4949 sizeof (struct vn_phi_s)
4950 + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
4952 /* Canonicalize the SSA_NAME's to their value number. */
4953 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
4955 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4956 if (TREE_CODE (def) == SSA_NAME
4957 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
4959 if (!virtual_operand_p (def)
4960 && ssa_undefined_value_p (def, false))
4961 def = VN_TOP;
4962 else
4963 def = SSA_VAL (def);
4965 vp1->phiargs[e->dest_idx] = def;
4967 vp1->type = TREE_TYPE (gimple_phi_result (phi));
4968 vp1->block = gimple_bb (phi);
4969 /* Extract values of the controlling condition. */
4970 vp1->cclhs = NULL_TREE;
4971 vp1->ccrhs = NULL_TREE;
4972 if (EDGE_COUNT (vp1->block->preds) == 2
4973 && vp1->block->loop_father->header != vp1->block)
4975 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4976 if (EDGE_COUNT (idom1->succs) == 2)
4977 if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
4979 /* ??? We want to use SSA_VAL here. But possibly not
4980 allow VN_TOP. */
4981 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
4982 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
4985 vp1->hashcode = vn_phi_compute_hash (vp1);
4986 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
4987 if (!slot)
4988 return NULL_TREE;
4989 return (*slot)->result;
4992 /* Insert PHI into the current hash table with a value number of
4993 RESULT. */
4995 static vn_phi_t
4996 vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
4998 vn_phi_s **slot;
4999 vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
5000 sizeof (vn_phi_s)
5001 + ((gimple_phi_num_args (phi) - 1)
5002 * sizeof (tree)));
5003 edge e;
5004 edge_iterator ei;
5006 /* Canonicalize the SSA_NAME's to their value number. */
5007 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5009 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5010 if (TREE_CODE (def) == SSA_NAME
5011 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5013 if (!virtual_operand_p (def)
5014 && ssa_undefined_value_p (def, false))
5015 def = VN_TOP;
5016 else
5017 def = SSA_VAL (def);
5019 vp1->phiargs[e->dest_idx] = def;
5021 vp1->value_id = VN_INFO (result)->value_id;
5022 vp1->type = TREE_TYPE (gimple_phi_result (phi));
5023 vp1->block = gimple_bb (phi);
5024 /* Extract values of the controlling condition. */
5025 vp1->cclhs = NULL_TREE;
5026 vp1->ccrhs = NULL_TREE;
5027 if (EDGE_COUNT (vp1->block->preds) == 2
5028 && vp1->block->loop_father->header != vp1->block)
5030 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5031 if (EDGE_COUNT (idom1->succs) == 2)
5032 if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5034 /* ??? We want to use SSA_VAL here. But possibly not
5035 allow VN_TOP. */
5036 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5037 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5040 vp1->result = result;
5041 vp1->hashcode = vn_phi_compute_hash (vp1);
5043 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5044 gcc_assert (!*slot);
5046 *slot = vp1;
5047 vp1->next = last_inserted_phi;
5048 last_inserted_phi = vp1;
5049 return vp1;
5053 /* Return true if BB1 is dominated by BB2 taking into account edges
5054 that are not executable. When ALLOW_BACK is false consider not
5055 executable backedges as executable. */
5057 static bool
5058 dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
5060 edge_iterator ei;
5061 edge e;
5063 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5064 return true;
5066 /* Before iterating we'd like to know if there exists a
5067 (executable) path from bb2 to bb1 at all, if not we can
5068 directly return false. For now simply iterate once. */
5070 /* Iterate to the single executable bb1 predecessor. */
5071 if (EDGE_COUNT (bb1->preds) > 1)
5073 edge prede = NULL;
5074 FOR_EACH_EDGE (e, ei, bb1->preds)
5075 if ((e->flags & EDGE_EXECUTABLE)
5076 || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5078 if (prede)
5080 prede = NULL;
5081 break;
5083 prede = e;
5085 if (prede)
5087 bb1 = prede->src;
5089 /* Re-do the dominance check with changed bb1. */
5090 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5091 return true;
5095 /* Iterate to the single executable bb2 successor. */
5096 if (EDGE_COUNT (bb2->succs) > 1)
5098 edge succe = NULL;
5099 FOR_EACH_EDGE (e, ei, bb2->succs)
5100 if ((e->flags & EDGE_EXECUTABLE)
5101 || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5103 if (succe)
5105 succe = NULL;
5106 break;
5108 succe = e;
5110 if (succe)
5112 /* Verify the reached block is only reached through succe.
5113 If there is only one edge we can spare us the dominator
5114 check and iterate directly. */
5115 if (EDGE_COUNT (succe->dest->preds) > 1)
5117 FOR_EACH_EDGE (e, ei, succe->dest->preds)
5118 if (e != succe
5119 && ((e->flags & EDGE_EXECUTABLE)
5120 || (!allow_back && (e->flags & EDGE_DFS_BACK))))
5122 succe = NULL;
5123 break;
5126 if (succe)
5128 bb2 = succe->dest;
5130 /* Re-do the dominance check with changed bb2. */
5131 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5132 return true;
5137 /* We could now iterate updating bb1 / bb2. */
5138 return false;
5141 /* Set the value number of FROM to TO, return true if it has changed
5142 as a result. */
5144 static inline bool
5145 set_ssa_val_to (tree from, tree to)
5147 vn_ssa_aux_t from_info = VN_INFO (from);
5148 tree currval = from_info->valnum; // SSA_VAL (from)
5149 poly_int64 toff, coff;
5150 bool curr_undefined = false;
5151 bool curr_invariant = false;
5153 /* The only thing we allow as value numbers are ssa_names
5154 and invariants. So assert that here. We don't allow VN_TOP
5155 as visiting a stmt should produce a value-number other than
5156 that.
5157 ??? Still VN_TOP can happen for unreachable code, so force
5158 it to varying in that case. Not all code is prepared to
5159 get VN_TOP on valueization. */
5160 if (to == VN_TOP)
5162 /* ??? When iterating and visiting PHI <undef, backedge-value>
5163 for the first time we rightfully get VN_TOP and we need to
5164 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5165 With SCCVN we were simply lucky we iterated the other PHI
5166 cycles first and thus visited the backedge-value DEF. */
5167 if (currval == VN_TOP)
5168 goto set_and_exit;
5169 if (dump_file && (dump_flags & TDF_DETAILS))
5170 fprintf (dump_file, "Forcing value number to varying on "
5171 "receiving VN_TOP\n");
5172 to = from;
5175 gcc_checking_assert (to != NULL_TREE
5176 && ((TREE_CODE (to) == SSA_NAME
5177 && (to == from || SSA_VAL (to) == to))
5178 || is_gimple_min_invariant (to)));
5180 if (from != to)
5182 if (currval == from)
5184 if (dump_file && (dump_flags & TDF_DETAILS))
5186 fprintf (dump_file, "Not changing value number of ");
5187 print_generic_expr (dump_file, from);
5188 fprintf (dump_file, " from VARYING to ");
5189 print_generic_expr (dump_file, to);
5190 fprintf (dump_file, "\n");
5192 return false;
5194 curr_invariant = is_gimple_min_invariant (currval);
5195 curr_undefined = (TREE_CODE (currval) == SSA_NAME
5196 && !virtual_operand_p (currval)
5197 && ssa_undefined_value_p (currval, false));
5198 if (currval != VN_TOP
5199 && !curr_invariant
5200 && !curr_undefined
5201 && is_gimple_min_invariant (to))
5203 if (dump_file && (dump_flags & TDF_DETAILS))
5205 fprintf (dump_file, "Forcing VARYING instead of changing "
5206 "value number of ");
5207 print_generic_expr (dump_file, from);
5208 fprintf (dump_file, " from ");
5209 print_generic_expr (dump_file, currval);
5210 fprintf (dump_file, " (non-constant) to ");
5211 print_generic_expr (dump_file, to);
5212 fprintf (dump_file, " (constant)\n");
5214 to = from;
5216 else if (currval != VN_TOP
5217 && !curr_undefined
5218 && TREE_CODE (to) == SSA_NAME
5219 && !virtual_operand_p (to)
5220 && ssa_undefined_value_p (to, false))
5222 if (dump_file && (dump_flags & TDF_DETAILS))
5224 fprintf (dump_file, "Forcing VARYING instead of changing "
5225 "value number of ");
5226 print_generic_expr (dump_file, from);
5227 fprintf (dump_file, " from ");
5228 print_generic_expr (dump_file, currval);
5229 fprintf (dump_file, " (non-undefined) to ");
5230 print_generic_expr (dump_file, to);
5231 fprintf (dump_file, " (undefined)\n");
5233 to = from;
5235 else if (TREE_CODE (to) == SSA_NAME
5236 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5237 to = from;
5240 set_and_exit:
5241 if (dump_file && (dump_flags & TDF_DETAILS))
5243 fprintf (dump_file, "Setting value number of ");
5244 print_generic_expr (dump_file, from);
5245 fprintf (dump_file, " to ");
5246 print_generic_expr (dump_file, to);
5249 if (currval != to
5250 && !operand_equal_p (currval, to, 0)
5251 /* Different undefined SSA names are not actually different. See
5252 PR82320 for a testcase were we'd otherwise not terminate iteration. */
5253 && !(curr_undefined
5254 && TREE_CODE (to) == SSA_NAME
5255 && !virtual_operand_p (to)
5256 && ssa_undefined_value_p (to, false))
5257 /* ??? For addresses involving volatile objects or types operand_equal_p
5258 does not reliably detect ADDR_EXPRs as equal. We know we are only
5259 getting invariant gimple addresses here, so can use
5260 get_addr_base_and_unit_offset to do this comparison. */
5261 && !(TREE_CODE (currval) == ADDR_EXPR
5262 && TREE_CODE (to) == ADDR_EXPR
5263 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5264 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5265 && known_eq (coff, toff)))
5267 if (to != from
5268 && currval != VN_TOP
5269 && !curr_undefined
5270 /* We do not want to allow lattice transitions from one value
5271 to another since that may lead to not terminating iteration
5272 (see PR95049). Since there's no convenient way to check
5273 for the allowed transition of VAL -> PHI (loop entry value,
5274 same on two PHIs, to same PHI result) we restrict the check
5275 to invariants. */
5276 && curr_invariant
5277 && is_gimple_min_invariant (to))
5279 if (dump_file && (dump_flags & TDF_DETAILS))
5280 fprintf (dump_file, " forced VARYING");
5281 to = from;
5283 if (dump_file && (dump_flags & TDF_DETAILS))
5284 fprintf (dump_file, " (changed)\n");
5285 from_info->valnum = to;
5286 return true;
5288 if (dump_file && (dump_flags & TDF_DETAILS))
5289 fprintf (dump_file, "\n");
5290 return false;
5293 /* Set all definitions in STMT to value number to themselves.
5294 Return true if a value number changed. */
5296 static bool
5297 defs_to_varying (gimple *stmt)
5299 bool changed = false;
5300 ssa_op_iter iter;
5301 def_operand_p defp;
5303 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5305 tree def = DEF_FROM_PTR (defp);
5306 changed |= set_ssa_val_to (def, def);
5308 return changed;
5311 /* Visit a copy between LHS and RHS, return true if the value number
5312 changed. */
5314 static bool
5315 visit_copy (tree lhs, tree rhs)
5317 /* Valueize. */
5318 rhs = SSA_VAL (rhs);
5320 return set_ssa_val_to (lhs, rhs);
5323 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5324 is the same. */
5326 static tree
5327 valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5329 if (TREE_CODE (op) == SSA_NAME)
5330 op = vn_valueize (op);
5332 /* Either we have the op widened available. */
5333 tree ops[3] = {};
5334 ops[0] = op;
5335 tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5336 wide_type, ops, NULL);
5337 if (tem)
5338 return tem;
5340 /* Or the op is truncated from some existing value. */
5341 if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5343 gimple *def = SSA_NAME_DEF_STMT (op);
5344 if (is_gimple_assign (def)
5345 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5347 tem = gimple_assign_rhs1 (def);
5348 if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5350 if (TREE_CODE (tem) == SSA_NAME)
5351 tem = vn_valueize (tem);
5352 return tem;
5357 /* For constants simply extend it. */
5358 if (TREE_CODE (op) == INTEGER_CST)
5359 return wide_int_to_tree (wide_type, wi::to_widest (op));
5361 return NULL_TREE;
5364 /* Visit a nary operator RHS, value number it, and return true if the
5365 value number of LHS has changed as a result. */
5367 static bool
5368 visit_nary_op (tree lhs, gassign *stmt)
5370 vn_nary_op_t vnresult;
5371 tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5372 if (! result && vnresult)
5373 result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5374 if (result)
5375 return set_ssa_val_to (lhs, result);
5377 /* Do some special pattern matching for redundancies of operations
5378 in different types. */
5379 enum tree_code code = gimple_assign_rhs_code (stmt);
5380 tree type = TREE_TYPE (lhs);
5381 tree rhs1 = gimple_assign_rhs1 (stmt);
5382 switch (code)
5384 CASE_CONVERT:
5385 /* Match arithmetic done in a different type where we can easily
5386 substitute the result from some earlier sign-changed or widened
5387 operation. */
5388 if (INTEGRAL_TYPE_P (type)
5389 && TREE_CODE (rhs1) == SSA_NAME
5390 /* We only handle sign-changes, zero-extension -> & mask or
5391 sign-extension if we know the inner operation doesn't
5392 overflow. */
5393 && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5394 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5395 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5396 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5397 || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5399 gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5400 if (def
5401 && (gimple_assign_rhs_code (def) == PLUS_EXPR
5402 || gimple_assign_rhs_code (def) == MINUS_EXPR
5403 || gimple_assign_rhs_code (def) == MULT_EXPR))
5405 tree ops[3] = {};
5406 /* When requiring a sign-extension we cannot model a
5407 previous truncation with a single op so don't bother. */
5408 bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5409 /* Either we have the op widened available. */
5410 ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5411 allow_truncate);
5412 if (ops[0])
5413 ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5414 allow_truncate);
5415 if (ops[0] && ops[1])
5417 ops[0] = vn_nary_op_lookup_pieces
5418 (2, gimple_assign_rhs_code (def), type, ops, NULL);
5419 /* We have wider operation available. */
5420 if (ops[0]
5421 /* If the leader is a wrapping operation we can
5422 insert it for code hoisting w/o introducing
5423 undefined overflow. If it is not it has to
5424 be available. See PR86554. */
5425 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
5426 || (rpo_avail && vn_context_bb
5427 && rpo_avail->eliminate_avail (vn_context_bb,
5428 ops[0]))))
5430 unsigned lhs_prec = TYPE_PRECISION (type);
5431 unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5432 if (lhs_prec == rhs_prec
5433 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5434 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5436 gimple_match_op match_op (gimple_match_cond::UNCOND,
5437 NOP_EXPR, type, ops[0]);
5438 result = vn_nary_build_or_lookup (&match_op);
5439 if (result)
5441 bool changed = set_ssa_val_to (lhs, result);
5442 vn_nary_op_insert_stmt (stmt, result);
5443 return changed;
5446 else
5448 tree mask = wide_int_to_tree
5449 (type, wi::mask (rhs_prec, false, lhs_prec));
5450 gimple_match_op match_op (gimple_match_cond::UNCOND,
5451 BIT_AND_EXPR,
5452 TREE_TYPE (lhs),
5453 ops[0], mask);
5454 result = vn_nary_build_or_lookup (&match_op);
5455 if (result)
5457 bool changed = set_ssa_val_to (lhs, result);
5458 vn_nary_op_insert_stmt (stmt, result);
5459 return changed;
5466 break;
5467 case BIT_AND_EXPR:
5468 if (INTEGRAL_TYPE_P (type)
5469 && TREE_CODE (rhs1) == SSA_NAME
5470 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5471 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5472 && default_vn_walk_kind != VN_NOWALK
5473 && CHAR_BIT == 8
5474 && BITS_PER_UNIT == 8
5475 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5476 && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5477 && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5478 && !integer_zerop (gimple_assign_rhs2 (stmt)))
5480 gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5481 if (ass
5482 && !gimple_has_volatile_ops (ass)
5483 && vn_get_stmt_kind (ass) == VN_REFERENCE)
5485 tree last_vuse = gimple_vuse (ass);
5486 tree op = gimple_assign_rhs1 (ass);
5487 tree result = vn_reference_lookup (op, gimple_vuse (ass),
5488 default_vn_walk_kind,
5489 NULL, true, &last_vuse,
5490 gimple_assign_rhs2 (stmt));
5491 if (result
5492 && useless_type_conversion_p (TREE_TYPE (result),
5493 TREE_TYPE (op)))
5494 return set_ssa_val_to (lhs, result);
5497 break;
5498 case TRUNC_DIV_EXPR:
5499 if (TYPE_UNSIGNED (type))
5500 break;
5501 /* Fallthru. */
5502 case RDIV_EXPR:
5503 case MULT_EXPR:
5504 /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5505 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5507 tree rhs[2];
5508 rhs[0] = rhs1;
5509 rhs[1] = gimple_assign_rhs2 (stmt);
5510 for (unsigned i = 0; i <= 1; ++i)
5512 unsigned j = i == 0 ? 1 : 0;
5513 tree ops[2];
5514 gimple_match_op match_op (gimple_match_cond::UNCOND,
5515 NEGATE_EXPR, type, rhs[i]);
5516 ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5517 ops[j] = rhs[j];
5518 if (ops[i]
5519 && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5520 type, ops, NULL)))
5522 gimple_match_op match_op (gimple_match_cond::UNCOND,
5523 NEGATE_EXPR, type, ops[0]);
5524 result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5525 if (result)
5527 bool changed = set_ssa_val_to (lhs, result);
5528 vn_nary_op_insert_stmt (stmt, result);
5529 return changed;
5534 break;
5535 case LSHIFT_EXPR:
5536 /* For X << C, use the value number of X * (1 << C). */
5537 if (INTEGRAL_TYPE_P (type)
5538 && TYPE_OVERFLOW_WRAPS (type)
5539 && !TYPE_SATURATING (type))
5541 tree rhs2 = gimple_assign_rhs2 (stmt);
5542 if (TREE_CODE (rhs2) == INTEGER_CST
5543 && tree_fits_uhwi_p (rhs2)
5544 && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
5546 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
5547 TYPE_PRECISION (type));
5548 gimple_match_op match_op (gimple_match_cond::UNCOND,
5549 MULT_EXPR, type, rhs1,
5550 wide_int_to_tree (type, w));
5551 result = vn_nary_build_or_lookup (&match_op);
5552 if (result)
5554 bool changed = set_ssa_val_to (lhs, result);
5555 if (TREE_CODE (result) == SSA_NAME)
5556 vn_nary_op_insert_stmt (stmt, result);
5557 return changed;
5561 break;
5562 default:
5563 break;
5566 bool changed = set_ssa_val_to (lhs, lhs);
5567 vn_nary_op_insert_stmt (stmt, lhs);
5568 return changed;
5571 /* Visit a call STMT storing into LHS. Return true if the value number
5572 of the LHS has changed as a result. */
5574 static bool
5575 visit_reference_op_call (tree lhs, gcall *stmt)
5577 bool changed = false;
5578 struct vn_reference_s vr1;
5579 vn_reference_t vnresult = NULL;
5580 tree vdef = gimple_vdef (stmt);
5581 modref_summary *summary;
5583 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5584 if (lhs && TREE_CODE (lhs) != SSA_NAME)
5585 lhs = NULL_TREE;
5587 vn_reference_lookup_call (stmt, &vnresult, &vr1);
5589 /* If the lookup did not succeed for pure functions try to use
5590 modref info to find a candidate to CSE to. */
5591 const unsigned accesses_limit = 8;
5592 if (!vnresult
5593 && !vdef
5594 && lhs
5595 && gimple_vuse (stmt)
5596 && (((summary = get_modref_function_summary (stmt, NULL))
5597 && !summary->global_memory_read
5598 && summary->load_accesses < accesses_limit)
5599 || gimple_call_flags (stmt) & ECF_CONST))
5601 /* First search if we can do someting useful and build a
5602 vector of all loads we have to check. */
5603 bool unknown_memory_access = false;
5604 auto_vec<ao_ref, accesses_limit> accesses;
5605 unsigned load_accesses = summary ? summary->load_accesses : 0;
5606 if (!unknown_memory_access)
5607 /* Add loads done as part of setting up the call arguments.
5608 That's also necessary for CONST functions which will
5609 not have a modref summary. */
5610 for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
5612 tree arg = gimple_call_arg (stmt, i);
5613 if (TREE_CODE (arg) != SSA_NAME
5614 && !is_gimple_min_invariant (arg))
5616 if (accesses.length () >= accesses_limit - load_accesses)
5618 unknown_memory_access = true;
5619 break;
5621 accesses.quick_grow (accesses.length () + 1);
5622 ao_ref_init (&accesses.last (), arg);
5625 if (summary && !unknown_memory_access)
5627 /* Add loads as analyzed by IPA modref. */
5628 for (auto base_node : summary->loads->bases)
5629 if (unknown_memory_access)
5630 break;
5631 else for (auto ref_node : base_node->refs)
5632 if (unknown_memory_access)
5633 break;
5634 else for (auto access_node : ref_node->accesses)
5636 accesses.quick_grow (accesses.length () + 1);
5637 ao_ref *r = &accesses.last ();
5638 if (!access_node.get_ao_ref (stmt, r))
5640 /* Initialize a ref based on the argument and
5641 unknown offset if possible. */
5642 tree arg = access_node.get_call_arg (stmt);
5643 if (arg && TREE_CODE (arg) == SSA_NAME)
5644 arg = SSA_VAL (arg);
5645 if (arg
5646 && TREE_CODE (arg) == ADDR_EXPR
5647 && (arg = get_base_address (arg))
5648 && DECL_P (arg))
5650 ao_ref_init (r, arg);
5651 r->ref = NULL_TREE;
5652 r->base = arg;
5654 else
5656 unknown_memory_access = true;
5657 break;
5660 r->base_alias_set = base_node->base;
5661 r->ref_alias_set = ref_node->ref;
5665 /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5666 for the call in the hashtable. */
5667 unsigned limit = (unknown_memory_access
5669 : (param_sccvn_max_alias_queries_per_access
5670 / (accesses.length () + 1)));
5671 tree saved_vuse = vr1.vuse;
5672 hashval_t saved_hashcode = vr1.hashcode;
5673 while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
5675 vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
5676 gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
5677 /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5678 do not bother for now. */
5679 if (is_a <gphi *> (def))
5680 break;
5681 vr1.vuse = vuse_ssa_val (gimple_vuse (def));
5682 vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
5683 vn_reference_lookup_1 (&vr1, &vnresult);
5684 limit--;
5687 /* If we found a candidate to CSE to verify it is valid. */
5688 if (vnresult && !accesses.is_empty ())
5690 tree vuse = vuse_ssa_val (gimple_vuse (stmt));
5691 while (vnresult && vuse != vr1.vuse)
5693 gimple *def = SSA_NAME_DEF_STMT (vuse);
5694 for (auto &ref : accesses)
5696 /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5697 analysis overhead that we might be able to cache. */
5698 if (stmt_may_clobber_ref_p_1 (def, &ref, true))
5700 vnresult = NULL;
5701 break;
5704 vuse = vuse_ssa_val (gimple_vuse (def));
5707 vr1.vuse = saved_vuse;
5708 vr1.hashcode = saved_hashcode;
5711 if (vnresult)
5713 if (vdef)
5715 if (vnresult->result_vdef)
5716 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
5717 else if (!lhs && gimple_call_lhs (stmt))
5718 /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5719 as the call still acts as a lhs store. */
5720 changed |= set_ssa_val_to (vdef, vdef);
5721 else
5722 /* If the call was discovered to be pure or const reflect
5723 that as far as possible. */
5724 changed |= set_ssa_val_to (vdef,
5725 vuse_ssa_val (gimple_vuse (stmt)));
5728 if (!vnresult->result && lhs)
5729 vnresult->result = lhs;
5731 if (vnresult->result && lhs)
5732 changed |= set_ssa_val_to (lhs, vnresult->result);
5734 else
5736 vn_reference_t vr2;
5737 vn_reference_s **slot;
5738 tree vdef_val = vdef;
5739 if (vdef)
5741 /* If we value numbered an indirect functions function to
5742 one not clobbering memory value number its VDEF to its
5743 VUSE. */
5744 tree fn = gimple_call_fn (stmt);
5745 if (fn && TREE_CODE (fn) == SSA_NAME)
5747 fn = SSA_VAL (fn);
5748 if (TREE_CODE (fn) == ADDR_EXPR
5749 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5750 && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
5751 & (ECF_CONST | ECF_PURE))
5752 /* If stmt has non-SSA_NAME lhs, value number the
5753 vdef to itself, as the call still acts as a lhs
5754 store. */
5755 && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
5756 vdef_val = vuse_ssa_val (gimple_vuse (stmt));
5758 changed |= set_ssa_val_to (vdef, vdef_val);
5760 if (lhs)
5761 changed |= set_ssa_val_to (lhs, lhs);
5762 vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
5763 vr2->vuse = vr1.vuse;
5764 /* As we are not walking the virtual operand chain we know the
5765 shared_lookup_references are still original so we can re-use
5766 them here. */
5767 vr2->operands = vr1.operands.copy ();
5768 vr2->type = vr1.type;
5769 vr2->punned = vr1.punned;
5770 vr2->set = vr1.set;
5771 vr2->offset = vr1.offset;
5772 vr2->max_size = vr1.max_size;
5773 vr2->base_set = vr1.base_set;
5774 vr2->hashcode = vr1.hashcode;
5775 vr2->result = lhs;
5776 vr2->result_vdef = vdef_val;
5777 vr2->value_id = 0;
5778 slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
5779 INSERT);
5780 gcc_assert (!*slot);
5781 *slot = vr2;
5782 vr2->next = last_inserted_ref;
5783 last_inserted_ref = vr2;
5786 return changed;
5789 /* Visit a load from a reference operator RHS, part of STMT, value number it,
5790 and return true if the value number of the LHS has changed as a result. */
5792 static bool
5793 visit_reference_op_load (tree lhs, tree op, gimple *stmt)
5795 bool changed = false;
5796 tree result;
5797 vn_reference_t res;
5799 tree vuse = gimple_vuse (stmt);
5800 tree last_vuse = vuse;
5801 result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
5803 /* We handle type-punning through unions by value-numbering based
5804 on offset and size of the access. Be prepared to handle a
5805 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5806 if (result
5807 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
5809 /* Avoid the type punning in case the result mode has padding where
5810 the op we lookup has not. */
5811 if (TYPE_MODE (TREE_TYPE (result)) != BLKmode
5812 && maybe_lt (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (result))),
5813 GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op)))))
5814 result = NULL_TREE;
5815 else if (CONSTANT_CLASS_P (result))
5816 result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5817 else
5819 /* We will be setting the value number of lhs to the value number
5820 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5821 So first simplify and lookup this expression to see if it
5822 is already available. */
5823 gimple_match_op res_op (gimple_match_cond::UNCOND,
5824 VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5825 result = vn_nary_build_or_lookup (&res_op);
5826 if (result
5827 && TREE_CODE (result) == SSA_NAME
5828 && VN_INFO (result)->needs_insertion)
5829 /* Track whether this is the canonical expression for different
5830 typed loads. We use that as a stopgap measure for code
5831 hoisting when dealing with floating point loads. */
5832 res->punned = true;
5835 /* When building the conversion fails avoid inserting the reference
5836 again. */
5837 if (!result)
5838 return set_ssa_val_to (lhs, lhs);
5841 if (result)
5842 changed = set_ssa_val_to (lhs, result);
5843 else
5845 changed = set_ssa_val_to (lhs, lhs);
5846 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
5847 if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
5849 if (dump_file && (dump_flags & TDF_DETAILS))
5851 fprintf (dump_file, "Using extra use virtual operand ");
5852 print_generic_expr (dump_file, last_vuse);
5853 fprintf (dump_file, "\n");
5855 vn_reference_insert (op, lhs, vuse, NULL_TREE);
5859 return changed;
5863 /* Visit a store to a reference operator LHS, part of STMT, value number it,
5864 and return true if the value number of the LHS has changed as a result. */
5866 static bool
5867 visit_reference_op_store (tree lhs, tree op, gimple *stmt)
5869 bool changed = false;
5870 vn_reference_t vnresult = NULL;
5871 tree assign;
5872 bool resultsame = false;
5873 tree vuse = gimple_vuse (stmt);
5874 tree vdef = gimple_vdef (stmt);
5876 if (TREE_CODE (op) == SSA_NAME)
5877 op = SSA_VAL (op);
5879 /* First we want to lookup using the *vuses* from the store and see
5880 if there the last store to this location with the same address
5881 had the same value.
5883 The vuses represent the memory state before the store. If the
5884 memory state, address, and value of the store is the same as the
5885 last store to this location, then this store will produce the
5886 same memory state as that store.
5888 In this case the vdef versions for this store are value numbered to those
5889 vuse versions, since they represent the same memory state after
5890 this store.
5892 Otherwise, the vdefs for the store are used when inserting into
5893 the table, since the store generates a new memory state. */
5895 vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
5896 if (vnresult
5897 && vnresult->result)
5899 tree result = vnresult->result;
5900 gcc_checking_assert (TREE_CODE (result) != SSA_NAME
5901 || result == SSA_VAL (result));
5902 resultsame = expressions_equal_p (result, op);
5903 if (resultsame)
5905 /* If the TBAA state isn't compatible for downstream reads
5906 we cannot value-number the VDEFs the same. */
5907 ao_ref lhs_ref;
5908 ao_ref_init (&lhs_ref, lhs);
5909 alias_set_type set = ao_ref_alias_set (&lhs_ref);
5910 alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
5911 if ((vnresult->set != set
5912 && ! alias_set_subset_of (set, vnresult->set))
5913 || (vnresult->base_set != base_set
5914 && ! alias_set_subset_of (base_set, vnresult->base_set)))
5915 resultsame = false;
5919 if (!resultsame)
5921 if (dump_file && (dump_flags & TDF_DETAILS))
5923 fprintf (dump_file, "No store match\n");
5924 fprintf (dump_file, "Value numbering store ");
5925 print_generic_expr (dump_file, lhs);
5926 fprintf (dump_file, " to ");
5927 print_generic_expr (dump_file, op);
5928 fprintf (dump_file, "\n");
5930 /* Have to set value numbers before insert, since insert is
5931 going to valueize the references in-place. */
5932 if (vdef)
5933 changed |= set_ssa_val_to (vdef, vdef);
5935 /* Do not insert structure copies into the tables. */
5936 if (is_gimple_min_invariant (op)
5937 || is_gimple_reg (op))
5938 vn_reference_insert (lhs, op, vdef, NULL);
5940 /* Only perform the following when being called from PRE
5941 which embeds tail merging. */
5942 if (default_vn_walk_kind == VN_WALK)
5944 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
5945 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
5946 if (!vnresult)
5947 vn_reference_insert (assign, lhs, vuse, vdef);
5950 else
5952 /* We had a match, so value number the vdef to have the value
5953 number of the vuse it came from. */
5955 if (dump_file && (dump_flags & TDF_DETAILS))
5956 fprintf (dump_file, "Store matched earlier value, "
5957 "value numbering store vdefs to matching vuses.\n");
5959 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
5962 return changed;
5965 /* Visit and value number PHI, return true if the value number
5966 changed. When BACKEDGES_VARYING_P is true then assume all
5967 backedge values are varying. When INSERTED is not NULL then
5968 this is just a ahead query for a possible iteration, set INSERTED
5969 to true if we'd insert into the hashtable. */
5971 static bool
5972 visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
5974 tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
5975 bool seen_undef_visited = false;
5976 tree backedge_val = NULL_TREE;
5977 bool seen_non_backedge = false;
5978 tree sameval_base = NULL_TREE;
5979 poly_int64 soff, doff;
5980 unsigned n_executable = 0;
5981 edge_iterator ei;
5982 edge e, sameval_e = NULL;
5984 /* TODO: We could check for this in initialization, and replace this
5985 with a gcc_assert. */
5986 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
5987 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
5989 /* We track whether a PHI was CSEd to avoid excessive iterations
5990 that would be necessary only because the PHI changed arguments
5991 but not value. */
5992 if (!inserted)
5993 gimple_set_plf (phi, GF_PLF_1, false);
5995 /* See if all non-TOP arguments have the same value. TOP is
5996 equivalent to everything, so we can ignore it. */
5997 basic_block bb = gimple_bb (phi);
5998 FOR_EACH_EDGE (e, ei, bb->preds)
5999 if (e->flags & EDGE_EXECUTABLE)
6001 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6003 if (def == PHI_RESULT (phi))
6004 continue;
6005 ++n_executable;
6006 bool visited = true;
6007 if (TREE_CODE (def) == SSA_NAME)
6009 tree val = SSA_VAL (def, &visited);
6010 if (SSA_NAME_IS_DEFAULT_DEF (def))
6011 visited = true;
6012 if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6013 def = val;
6014 if (e->flags & EDGE_DFS_BACK)
6015 backedge_val = def;
6017 if (!(e->flags & EDGE_DFS_BACK))
6018 seen_non_backedge = true;
6019 if (def == VN_TOP)
6021 /* Ignore undefined defs for sameval but record one. */
6022 else if (TREE_CODE (def) == SSA_NAME
6023 && ! virtual_operand_p (def)
6024 && ssa_undefined_value_p (def, false))
6026 if (!seen_undef
6027 /* Avoid having not visited undefined defs if we also have
6028 a visited one. */
6029 || (!seen_undef_visited && visited))
6031 seen_undef = def;
6032 seen_undef_visited = visited;
6035 else if (sameval == VN_TOP)
6037 sameval = def;
6038 sameval_e = e;
6040 else if (expressions_equal_p (def, sameval))
6041 sameval_e = NULL;
6042 else if (virtual_operand_p (def))
6044 sameval = NULL_TREE;
6045 break;
6047 else
6049 /* We know we're arriving only with invariant addresses here,
6050 try harder comparing them. We can do some caching here
6051 which we cannot do in expressions_equal_p. */
6052 if (TREE_CODE (def) == ADDR_EXPR
6053 && TREE_CODE (sameval) == ADDR_EXPR
6054 && sameval_base != (void *)-1)
6056 if (!sameval_base)
6057 sameval_base = get_addr_base_and_unit_offset
6058 (TREE_OPERAND (sameval, 0), &soff);
6059 if (!sameval_base)
6060 sameval_base = (tree)(void *)-1;
6061 else if ((get_addr_base_and_unit_offset
6062 (TREE_OPERAND (def, 0), &doff) == sameval_base)
6063 && known_eq (soff, doff))
6064 continue;
6066 /* There's also the possibility to use equivalences. */
6067 if (!FLOAT_TYPE_P (TREE_TYPE (def))
6068 /* But only do this if we didn't force any of sameval or
6069 val to VARYING because of backedge processing rules. */
6070 && (TREE_CODE (sameval) != SSA_NAME
6071 || SSA_VAL (sameval) == sameval)
6072 && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6074 vn_nary_op_t vnresult;
6075 tree ops[2];
6076 ops[0] = def;
6077 ops[1] = sameval;
6078 tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6079 boolean_type_node,
6080 ops, &vnresult);
6081 if (! val && vnresult && vnresult->predicated_values)
6083 val = vn_nary_op_get_predicated_value (vnresult, e);
6084 if (val && integer_truep (val)
6085 && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6087 if (dump_file && (dump_flags & TDF_DETAILS))
6089 fprintf (dump_file, "Predication says ");
6090 print_generic_expr (dump_file, def, TDF_NONE);
6091 fprintf (dump_file, " and ");
6092 print_generic_expr (dump_file, sameval, TDF_NONE);
6093 fprintf (dump_file, " are equal on edge %d -> %d\n",
6094 e->src->index, e->dest->index);
6096 continue;
6098 /* If on all previous edges the value was equal to def
6099 we can change sameval to def. */
6100 if (EDGE_COUNT (bb->preds) == 2
6101 && (val = vn_nary_op_get_predicated_value
6102 (vnresult, EDGE_PRED (bb, 0)))
6103 && integer_truep (val)
6104 && !(e->flags & EDGE_DFS_BACK))
6106 if (dump_file && (dump_flags & TDF_DETAILS))
6108 fprintf (dump_file, "Predication says ");
6109 print_generic_expr (dump_file, def, TDF_NONE);
6110 fprintf (dump_file, " and ");
6111 print_generic_expr (dump_file, sameval, TDF_NONE);
6112 fprintf (dump_file, " are equal on edge %d -> %d\n",
6113 EDGE_PRED (bb, 0)->src->index,
6114 EDGE_PRED (bb, 0)->dest->index);
6116 sameval = def;
6117 continue;
6121 sameval = NULL_TREE;
6122 break;
6126 /* If the value we want to use is flowing over the backedge and we
6127 should take it as VARYING but it has a non-VARYING value drop to
6128 VARYING.
6129 If we value-number a virtual operand never value-number to the
6130 value from the backedge as that confuses the alias-walking code.
6131 See gcc.dg/torture/pr87176.c. If the value is the same on a
6132 non-backedge everything is OK though. */
6133 bool visited_p;
6134 if ((backedge_val
6135 && !seen_non_backedge
6136 && TREE_CODE (backedge_val) == SSA_NAME
6137 && sameval == backedge_val
6138 && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6139 || SSA_VAL (backedge_val) != backedge_val))
6140 /* Do not value-number a virtual operand to sth not visited though
6141 given that allows us to escape a region in alias walking. */
6142 || (sameval
6143 && TREE_CODE (sameval) == SSA_NAME
6144 && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6145 && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6146 && (SSA_VAL (sameval, &visited_p), !visited_p)))
6147 /* Note this just drops to VARYING without inserting the PHI into
6148 the hashes. */
6149 result = PHI_RESULT (phi);
6150 /* If none of the edges was executable keep the value-number at VN_TOP,
6151 if only a single edge is exectuable use its value. */
6152 else if (n_executable <= 1)
6153 result = seen_undef ? seen_undef : sameval;
6154 /* If we saw only undefined values and VN_TOP use one of the
6155 undefined values. */
6156 else if (sameval == VN_TOP)
6157 result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6158 /* First see if it is equivalent to a phi node in this block. We prefer
6159 this as it allows IV elimination - see PRs 66502 and 67167. */
6160 else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6162 if (!inserted
6163 && TREE_CODE (result) == SSA_NAME
6164 && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6166 gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6167 if (dump_file && (dump_flags & TDF_DETAILS))
6169 fprintf (dump_file, "Marking CSEd to PHI node ");
6170 print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6171 0, TDF_SLIM);
6172 fprintf (dump_file, "\n");
6176 /* If all values are the same use that, unless we've seen undefined
6177 values as well and the value isn't constant.
6178 CCP/copyprop have the same restriction to not remove uninit warnings. */
6179 else if (sameval
6180 && (! seen_undef || is_gimple_min_invariant (sameval)))
6181 result = sameval;
6182 else
6184 result = PHI_RESULT (phi);
6185 /* Only insert PHIs that are varying, for constant value numbers
6186 we mess up equivalences otherwise as we are only comparing
6187 the immediate controlling predicates. */
6188 vn_phi_insert (phi, result, backedges_varying_p);
6189 if (inserted)
6190 *inserted = true;
6193 return set_ssa_val_to (PHI_RESULT (phi), result);
6196 /* Try to simplify RHS using equivalences and constant folding. */
6198 static tree
6199 try_to_simplify (gassign *stmt)
6201 enum tree_code code = gimple_assign_rhs_code (stmt);
6202 tree tem;
6204 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6205 in this case, there is no point in doing extra work. */
6206 if (code == SSA_NAME)
6207 return NULL_TREE;
6209 /* First try constant folding based on our current lattice. */
6210 mprts_hook = vn_lookup_simplify_result;
6211 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6212 mprts_hook = NULL;
6213 if (tem
6214 && (TREE_CODE (tem) == SSA_NAME
6215 || is_gimple_min_invariant (tem)))
6216 return tem;
6218 return NULL_TREE;
6221 /* Visit and value number STMT, return true if the value number
6222 changed. */
6224 static bool
6225 visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6227 bool changed = false;
6229 if (dump_file && (dump_flags & TDF_DETAILS))
6231 fprintf (dump_file, "Value numbering stmt = ");
6232 print_gimple_stmt (dump_file, stmt, 0);
6235 if (gimple_code (stmt) == GIMPLE_PHI)
6236 changed = visit_phi (stmt, NULL, backedges_varying_p);
6237 else if (gimple_has_volatile_ops (stmt))
6238 changed = defs_to_varying (stmt);
6239 else if (gassign *ass = dyn_cast <gassign *> (stmt))
6241 enum tree_code code = gimple_assign_rhs_code (ass);
6242 tree lhs = gimple_assign_lhs (ass);
6243 tree rhs1 = gimple_assign_rhs1 (ass);
6244 tree simplified;
6246 /* Shortcut for copies. Simplifying copies is pointless,
6247 since we copy the expression and value they represent. */
6248 if (code == SSA_NAME
6249 && TREE_CODE (lhs) == SSA_NAME)
6251 changed = visit_copy (lhs, rhs1);
6252 goto done;
6254 simplified = try_to_simplify (ass);
6255 if (simplified)
6257 if (dump_file && (dump_flags & TDF_DETAILS))
6259 fprintf (dump_file, "RHS ");
6260 print_gimple_expr (dump_file, ass, 0);
6261 fprintf (dump_file, " simplified to ");
6262 print_generic_expr (dump_file, simplified);
6263 fprintf (dump_file, "\n");
6266 /* Setting value numbers to constants will occasionally
6267 screw up phi congruence because constants are not
6268 uniquely associated with a single ssa name that can be
6269 looked up. */
6270 if (simplified
6271 && is_gimple_min_invariant (simplified)
6272 && TREE_CODE (lhs) == SSA_NAME)
6274 changed = set_ssa_val_to (lhs, simplified);
6275 goto done;
6277 else if (simplified
6278 && TREE_CODE (simplified) == SSA_NAME
6279 && TREE_CODE (lhs) == SSA_NAME)
6281 changed = visit_copy (lhs, simplified);
6282 goto done;
6285 if ((TREE_CODE (lhs) == SSA_NAME
6286 /* We can substitute SSA_NAMEs that are live over
6287 abnormal edges with their constant value. */
6288 && !(gimple_assign_copy_p (ass)
6289 && is_gimple_min_invariant (rhs1))
6290 && !(simplified
6291 && is_gimple_min_invariant (simplified))
6292 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6293 /* Stores or copies from SSA_NAMEs that are live over
6294 abnormal edges are a problem. */
6295 || (code == SSA_NAME
6296 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6297 changed = defs_to_varying (ass);
6298 else if (REFERENCE_CLASS_P (lhs)
6299 || DECL_P (lhs))
6300 changed = visit_reference_op_store (lhs, rhs1, ass);
6301 else if (TREE_CODE (lhs) == SSA_NAME)
6303 if ((gimple_assign_copy_p (ass)
6304 && is_gimple_min_invariant (rhs1))
6305 || (simplified
6306 && is_gimple_min_invariant (simplified)))
6308 if (simplified)
6309 changed = set_ssa_val_to (lhs, simplified);
6310 else
6311 changed = set_ssa_val_to (lhs, rhs1);
6313 else
6315 /* Visit the original statement. */
6316 switch (vn_get_stmt_kind (ass))
6318 case VN_NARY:
6319 changed = visit_nary_op (lhs, ass);
6320 break;
6321 case VN_REFERENCE:
6322 changed = visit_reference_op_load (lhs, rhs1, ass);
6323 break;
6324 default:
6325 changed = defs_to_varying (ass);
6326 break;
6330 else
6331 changed = defs_to_varying (ass);
6333 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6335 tree lhs = gimple_call_lhs (call_stmt);
6336 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6338 /* Try constant folding based on our current lattice. */
6339 tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6340 vn_valueize);
6341 if (simplified)
6343 if (dump_file && (dump_flags & TDF_DETAILS))
6345 fprintf (dump_file, "call ");
6346 print_gimple_expr (dump_file, call_stmt, 0);
6347 fprintf (dump_file, " simplified to ");
6348 print_generic_expr (dump_file, simplified);
6349 fprintf (dump_file, "\n");
6352 /* Setting value numbers to constants will occasionally
6353 screw up phi congruence because constants are not
6354 uniquely associated with a single ssa name that can be
6355 looked up. */
6356 if (simplified
6357 && is_gimple_min_invariant (simplified))
6359 changed = set_ssa_val_to (lhs, simplified);
6360 if (gimple_vdef (call_stmt))
6361 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6362 SSA_VAL (gimple_vuse (call_stmt)));
6363 goto done;
6365 else if (simplified
6366 && TREE_CODE (simplified) == SSA_NAME)
6368 changed = visit_copy (lhs, simplified);
6369 if (gimple_vdef (call_stmt))
6370 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6371 SSA_VAL (gimple_vuse (call_stmt)));
6372 goto done;
6374 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6376 changed = defs_to_varying (call_stmt);
6377 goto done;
6381 /* Pick up flags from a devirtualization target. */
6382 tree fn = gimple_call_fn (stmt);
6383 int extra_fnflags = 0;
6384 if (fn && TREE_CODE (fn) == SSA_NAME)
6386 fn = SSA_VAL (fn);
6387 if (TREE_CODE (fn) == ADDR_EXPR
6388 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6389 extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6391 if ((/* Calls to the same function with the same vuse
6392 and the same operands do not necessarily return the same
6393 value, unless they're pure or const. */
6394 ((gimple_call_flags (call_stmt) | extra_fnflags)
6395 & (ECF_PURE | ECF_CONST))
6396 /* If calls have a vdef, subsequent calls won't have
6397 the same incoming vuse. So, if 2 calls with vdef have the
6398 same vuse, we know they're not subsequent.
6399 We can value number 2 calls to the same function with the
6400 same vuse and the same operands which are not subsequent
6401 the same, because there is no code in the program that can
6402 compare the 2 values... */
6403 || (gimple_vdef (call_stmt)
6404 /* ... unless the call returns a pointer which does
6405 not alias with anything else. In which case the
6406 information that the values are distinct are encoded
6407 in the IL. */
6408 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6409 /* Only perform the following when being called from PRE
6410 which embeds tail merging. */
6411 && default_vn_walk_kind == VN_WALK))
6412 /* Do not process .DEFERRED_INIT since that confuses uninit
6413 analysis. */
6414 && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6415 changed = visit_reference_op_call (lhs, call_stmt);
6416 else
6417 changed = defs_to_varying (call_stmt);
6419 else
6420 changed = defs_to_varying (stmt);
6421 done:
6422 return changed;
6426 /* Allocate a value number table. */
6428 static void
6429 allocate_vn_table (vn_tables_t table, unsigned size)
6431 table->phis = new vn_phi_table_type (size);
6432 table->nary = new vn_nary_op_table_type (size);
6433 table->references = new vn_reference_table_type (size);
6436 /* Free a value number table. */
6438 static void
6439 free_vn_table (vn_tables_t table)
6441 /* Walk over elements and release vectors. */
6442 vn_reference_iterator_type hir;
6443 vn_reference_t vr;
6444 FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
6445 vr->operands.release ();
6446 delete table->phis;
6447 table->phis = NULL;
6448 delete table->nary;
6449 table->nary = NULL;
6450 delete table->references;
6451 table->references = NULL;
6454 /* Set *ID according to RESULT. */
6456 static void
6457 set_value_id_for_result (tree result, unsigned int *id)
6459 if (result && TREE_CODE (result) == SSA_NAME)
6460 *id = VN_INFO (result)->value_id;
6461 else if (result && is_gimple_min_invariant (result))
6462 *id = get_or_alloc_constant_value_id (result);
6463 else
6464 *id = get_next_value_id ();
6467 /* Set the value ids in the valid hash tables. */
6469 static void
6470 set_hashtable_value_ids (void)
6472 vn_nary_op_iterator_type hin;
6473 vn_phi_iterator_type hip;
6474 vn_reference_iterator_type hir;
6475 vn_nary_op_t vno;
6476 vn_reference_t vr;
6477 vn_phi_t vp;
6479 /* Now set the value ids of the things we had put in the hash
6480 table. */
6482 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
6483 if (! vno->predicated_values)
6484 set_value_id_for_result (vno->u.result, &vno->value_id);
6486 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
6487 set_value_id_for_result (vp->result, &vp->value_id);
6489 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
6490 hir)
6491 set_value_id_for_result (vr->result, &vr->value_id);
6494 /* Return the maximum value id we have ever seen. */
6496 unsigned int
6497 get_max_value_id (void)
6499 return next_value_id;
6502 /* Return the maximum constant value id we have ever seen. */
6504 unsigned int
6505 get_max_constant_value_id (void)
6507 return -next_constant_value_id;
6510 /* Return the next unique value id. */
6512 unsigned int
6513 get_next_value_id (void)
6515 gcc_checking_assert ((int)next_value_id > 0);
6516 return next_value_id++;
6519 /* Return the next unique value id for constants. */
6521 unsigned int
6522 get_next_constant_value_id (void)
6524 gcc_checking_assert (next_constant_value_id < 0);
6525 return next_constant_value_id--;
6529 /* Compare two expressions E1 and E2 and return true if they are equal.
6530 If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6531 otherwise VN_TOP only matches VN_TOP. */
6533 bool
6534 expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
6536 /* The obvious case. */
6537 if (e1 == e2)
6538 return true;
6540 /* If either one is VN_TOP consider them equal. */
6541 if (match_vn_top_optimistically
6542 && (e1 == VN_TOP || e2 == VN_TOP))
6543 return true;
6545 /* If only one of them is null, they cannot be equal. While in general
6546 this should not happen for operations like TARGET_MEM_REF some
6547 operands are optional and an identity value we could substitute
6548 has differing semantics. */
6549 if (!e1 || !e2)
6550 return false;
6552 /* SSA_NAME compare pointer equal. */
6553 if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
6554 return false;
6556 /* Now perform the actual comparison. */
6557 if (TREE_CODE (e1) == TREE_CODE (e2)
6558 && operand_equal_p (e1, e2, OEP_PURE_SAME))
6559 return true;
6561 return false;
6565 /* Return true if the nary operation NARY may trap. This is a copy
6566 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6568 bool
6569 vn_nary_may_trap (vn_nary_op_t nary)
6571 tree type;
6572 tree rhs2 = NULL_TREE;
6573 bool honor_nans = false;
6574 bool honor_snans = false;
6575 bool fp_operation = false;
6576 bool honor_trapv = false;
6577 bool handled, ret;
6578 unsigned i;
6580 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
6581 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
6582 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
6584 type = nary->type;
6585 fp_operation = FLOAT_TYPE_P (type);
6586 if (fp_operation)
6588 honor_nans = flag_trapping_math && !flag_finite_math_only;
6589 honor_snans = flag_signaling_nans != 0;
6591 else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
6592 honor_trapv = true;
6594 if (nary->length >= 2)
6595 rhs2 = nary->op[1];
6596 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
6597 honor_trapv, honor_nans, honor_snans,
6598 rhs2, &handled);
6599 if (handled && ret)
6600 return true;
6602 for (i = 0; i < nary->length; ++i)
6603 if (tree_could_trap_p (nary->op[i]))
6604 return true;
6606 return false;
6609 /* Return true if the reference operation REF may trap. */
6611 bool
6612 vn_reference_may_trap (vn_reference_t ref)
6614 switch (ref->operands[0].opcode)
6616 case MODIFY_EXPR:
6617 case CALL_EXPR:
6618 /* We do not handle calls. */
6619 return true;
6620 case ADDR_EXPR:
6621 /* And toplevel address computations never trap. */
6622 return false;
6623 default:;
6626 vn_reference_op_t op;
6627 unsigned i;
6628 FOR_EACH_VEC_ELT (ref->operands, i, op)
6630 switch (op->opcode)
6632 case WITH_SIZE_EXPR:
6633 case TARGET_MEM_REF:
6634 /* Always variable. */
6635 return true;
6636 case COMPONENT_REF:
6637 if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
6638 return true;
6639 break;
6640 case ARRAY_RANGE_REF:
6641 if (TREE_CODE (op->op0) == SSA_NAME)
6642 return true;
6643 break;
6644 case ARRAY_REF:
6646 if (TREE_CODE (op->op0) != INTEGER_CST)
6647 return true;
6649 /* !in_array_bounds */
6650 tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
6651 if (!domain_type)
6652 return true;
6654 tree min = op->op1;
6655 tree max = TYPE_MAX_VALUE (domain_type);
6656 if (!min
6657 || !max
6658 || TREE_CODE (min) != INTEGER_CST
6659 || TREE_CODE (max) != INTEGER_CST)
6660 return true;
6662 if (tree_int_cst_lt (op->op0, min)
6663 || tree_int_cst_lt (max, op->op0))
6664 return true;
6666 break;
6668 case MEM_REF:
6669 /* Nothing interesting in itself, the base is separate. */
6670 break;
6671 /* The following are the address bases. */
6672 case SSA_NAME:
6673 return true;
6674 case ADDR_EXPR:
6675 if (op->op0)
6676 return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
6677 return false;
6678 default:;
6681 return false;
6684 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
6685 bitmap inserted_exprs_)
6686 : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
6687 el_todo (0), eliminations (0), insertions (0),
6688 inserted_exprs (inserted_exprs_)
6690 need_eh_cleanup = BITMAP_ALLOC (NULL);
6691 need_ab_cleanup = BITMAP_ALLOC (NULL);
6694 eliminate_dom_walker::~eliminate_dom_walker ()
6696 BITMAP_FREE (need_eh_cleanup);
6697 BITMAP_FREE (need_ab_cleanup);
6700 /* Return a leader for OP that is available at the current point of the
6701 eliminate domwalk. */
6703 tree
6704 eliminate_dom_walker::eliminate_avail (basic_block, tree op)
6706 tree valnum = VN_INFO (op)->valnum;
6707 if (TREE_CODE (valnum) == SSA_NAME)
6709 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6710 return valnum;
6711 if (avail.length () > SSA_NAME_VERSION (valnum))
6713 tree av = avail[SSA_NAME_VERSION (valnum)];
6714 /* When PRE discovers a new redundancy there's no way to unite
6715 the value classes so it instead inserts a copy old-val = new-val.
6716 Look through such copies here, providing one more level of
6717 simplification at elimination time. */
6718 gassign *ass;
6719 if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
6720 if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
6722 tree rhs1 = gimple_assign_rhs1 (ass);
6723 if (CONSTANT_CLASS_P (rhs1)
6724 || (TREE_CODE (rhs1) == SSA_NAME
6725 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6726 av = rhs1;
6728 return av;
6731 else if (is_gimple_min_invariant (valnum))
6732 return valnum;
6733 return NULL_TREE;
6736 /* At the current point of the eliminate domwalk make OP available. */
6738 void
6739 eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
6741 tree valnum = VN_INFO (op)->valnum;
6742 if (TREE_CODE (valnum) == SSA_NAME)
6744 if (avail.length () <= SSA_NAME_VERSION (valnum))
6745 avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
6746 tree pushop = op;
6747 if (avail[SSA_NAME_VERSION (valnum)])
6748 pushop = avail[SSA_NAME_VERSION (valnum)];
6749 avail_stack.safe_push (pushop);
6750 avail[SSA_NAME_VERSION (valnum)] = op;
6754 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
6755 the leader for the expression if insertion was successful. */
6757 tree
6758 eliminate_dom_walker::eliminate_insert (basic_block bb,
6759 gimple_stmt_iterator *gsi, tree val)
6761 /* We can insert a sequence with a single assignment only. */
6762 gimple_seq stmts = VN_INFO (val)->expr;
6763 if (!gimple_seq_singleton_p (stmts))
6764 return NULL_TREE;
6765 gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
6766 if (!stmt
6767 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
6768 && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
6769 && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
6770 && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
6771 && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6772 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
6773 return NULL_TREE;
6775 tree op = gimple_assign_rhs1 (stmt);
6776 if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
6777 || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6778 op = TREE_OPERAND (op, 0);
6779 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
6780 if (!leader)
6781 return NULL_TREE;
6783 tree res;
6784 stmts = NULL;
6785 if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6786 res = gimple_build (&stmts, BIT_FIELD_REF,
6787 TREE_TYPE (val), leader,
6788 TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
6789 TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
6790 else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
6791 res = gimple_build (&stmts, BIT_AND_EXPR,
6792 TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
6793 else
6794 res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
6795 TREE_TYPE (val), leader);
6796 if (TREE_CODE (res) != SSA_NAME
6797 || SSA_NAME_IS_DEFAULT_DEF (res)
6798 || gimple_bb (SSA_NAME_DEF_STMT (res)))
6800 gimple_seq_discard (stmts);
6802 /* During propagation we have to treat SSA info conservatively
6803 and thus we can end up simplifying the inserted expression
6804 at elimination time to sth not defined in stmts. */
6805 /* But then this is a redundancy we failed to detect. Which means
6806 res now has two values. That doesn't play well with how
6807 we track availability here, so give up. */
6808 if (dump_file && (dump_flags & TDF_DETAILS))
6810 if (TREE_CODE (res) == SSA_NAME)
6811 res = eliminate_avail (bb, res);
6812 if (res)
6814 fprintf (dump_file, "Failed to insert expression for value ");
6815 print_generic_expr (dump_file, val);
6816 fprintf (dump_file, " which is really fully redundant to ");
6817 print_generic_expr (dump_file, res);
6818 fprintf (dump_file, "\n");
6822 return NULL_TREE;
6824 else
6826 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6827 vn_ssa_aux_t vn_info = VN_INFO (res);
6828 vn_info->valnum = val;
6829 vn_info->visited = true;
6832 insertions++;
6833 if (dump_file && (dump_flags & TDF_DETAILS))
6835 fprintf (dump_file, "Inserted ");
6836 print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
6839 return res;
6842 void
6843 eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
6845 tree sprime = NULL_TREE;
6846 gimple *stmt = gsi_stmt (*gsi);
6847 tree lhs = gimple_get_lhs (stmt);
6848 if (lhs && TREE_CODE (lhs) == SSA_NAME
6849 && !gimple_has_volatile_ops (stmt)
6850 /* See PR43491. Do not replace a global register variable when
6851 it is a the RHS of an assignment. Do replace local register
6852 variables since gcc does not guarantee a local variable will
6853 be allocated in register.
6854 ??? The fix isn't effective here. This should instead
6855 be ensured by not value-numbering them the same but treating
6856 them like volatiles? */
6857 && !(gimple_assign_single_p (stmt)
6858 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
6859 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
6860 && is_global_var (gimple_assign_rhs1 (stmt)))))
6862 sprime = eliminate_avail (b, lhs);
6863 if (!sprime)
6865 /* If there is no existing usable leader but SCCVN thinks
6866 it has an expression it wants to use as replacement,
6867 insert that. */
6868 tree val = VN_INFO (lhs)->valnum;
6869 vn_ssa_aux_t vn_info;
6870 if (val != VN_TOP
6871 && TREE_CODE (val) == SSA_NAME
6872 && (vn_info = VN_INFO (val), true)
6873 && vn_info->needs_insertion
6874 && vn_info->expr != NULL
6875 && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
6876 eliminate_push_avail (b, sprime);
6879 /* If this now constitutes a copy duplicate points-to
6880 and range info appropriately. This is especially
6881 important for inserted code. See tree-ssa-copy.cc
6882 for similar code. */
6883 if (sprime
6884 && TREE_CODE (sprime) == SSA_NAME)
6886 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
6887 if (POINTER_TYPE_P (TREE_TYPE (lhs))
6888 && SSA_NAME_PTR_INFO (lhs)
6889 && ! SSA_NAME_PTR_INFO (sprime))
6891 duplicate_ssa_name_ptr_info (sprime,
6892 SSA_NAME_PTR_INFO (lhs));
6893 if (b != sprime_b)
6894 reset_flow_sensitive_info (sprime);
6896 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6897 && SSA_NAME_RANGE_INFO (lhs)
6898 && ! SSA_NAME_RANGE_INFO (sprime)
6899 && b == sprime_b)
6900 duplicate_ssa_name_range_info (sprime, lhs);
6903 /* Inhibit the use of an inserted PHI on a loop header when
6904 the address of the memory reference is a simple induction
6905 variable. In other cases the vectorizer won't do anything
6906 anyway (either it's loop invariant or a complicated
6907 expression). */
6908 if (sprime
6909 && TREE_CODE (sprime) == SSA_NAME
6910 && do_pre
6911 && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
6912 && loop_outer (b->loop_father)
6913 && has_zero_uses (sprime)
6914 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
6915 && gimple_assign_load_p (stmt))
6917 gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
6918 basic_block def_bb = gimple_bb (def_stmt);
6919 if (gimple_code (def_stmt) == GIMPLE_PHI
6920 && def_bb->loop_father->header == def_bb)
6922 loop_p loop = def_bb->loop_father;
6923 ssa_op_iter iter;
6924 tree op;
6925 bool found = false;
6926 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6928 affine_iv iv;
6929 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
6930 if (def_bb
6931 && flow_bb_inside_loop_p (loop, def_bb)
6932 && simple_iv (loop, loop, op, &iv, true))
6934 found = true;
6935 break;
6938 if (found)
6940 if (dump_file && (dump_flags & TDF_DETAILS))
6942 fprintf (dump_file, "Not replacing ");
6943 print_gimple_expr (dump_file, stmt, 0);
6944 fprintf (dump_file, " with ");
6945 print_generic_expr (dump_file, sprime);
6946 fprintf (dump_file, " which would add a loop"
6947 " carried dependence to loop %d\n",
6948 loop->num);
6950 /* Don't keep sprime available. */
6951 sprime = NULL_TREE;
6956 if (sprime)
6958 /* If we can propagate the value computed for LHS into
6959 all uses don't bother doing anything with this stmt. */
6960 if (may_propagate_copy (lhs, sprime))
6962 /* Mark it for removal. */
6963 to_remove.safe_push (stmt);
6965 /* ??? Don't count copy/constant propagations. */
6966 if (gimple_assign_single_p (stmt)
6967 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
6968 || gimple_assign_rhs1 (stmt) == sprime))
6969 return;
6971 if (dump_file && (dump_flags & TDF_DETAILS))
6973 fprintf (dump_file, "Replaced ");
6974 print_gimple_expr (dump_file, stmt, 0);
6975 fprintf (dump_file, " with ");
6976 print_generic_expr (dump_file, sprime);
6977 fprintf (dump_file, " in all uses of ");
6978 print_gimple_stmt (dump_file, stmt, 0);
6981 eliminations++;
6982 return;
6985 /* If this is an assignment from our leader (which
6986 happens in the case the value-number is a constant)
6987 then there is nothing to do. Likewise if we run into
6988 inserted code that needed a conversion because of
6989 our type-agnostic value-numbering of loads. */
6990 if ((gimple_assign_single_p (stmt)
6991 || (is_gimple_assign (stmt)
6992 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
6993 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
6994 && sprime == gimple_assign_rhs1 (stmt))
6995 return;
6997 /* Else replace its RHS. */
6998 if (dump_file && (dump_flags & TDF_DETAILS))
7000 fprintf (dump_file, "Replaced ");
7001 print_gimple_expr (dump_file, stmt, 0);
7002 fprintf (dump_file, " with ");
7003 print_generic_expr (dump_file, sprime);
7004 fprintf (dump_file, " in ");
7005 print_gimple_stmt (dump_file, stmt, 0);
7007 eliminations++;
7009 bool can_make_abnormal_goto = (is_gimple_call (stmt)
7010 && stmt_can_make_abnormal_goto (stmt));
7011 gimple *orig_stmt = stmt;
7012 if (!useless_type_conversion_p (TREE_TYPE (lhs),
7013 TREE_TYPE (sprime)))
7015 /* We preserve conversions to but not from function or method
7016 types. This asymmetry makes it necessary to re-instantiate
7017 conversions here. */
7018 if (POINTER_TYPE_P (TREE_TYPE (lhs))
7019 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7020 sprime = fold_convert (TREE_TYPE (lhs), sprime);
7021 else
7022 gcc_unreachable ();
7024 tree vdef = gimple_vdef (stmt);
7025 tree vuse = gimple_vuse (stmt);
7026 propagate_tree_value_into_stmt (gsi, sprime);
7027 stmt = gsi_stmt (*gsi);
7028 update_stmt (stmt);
7029 /* In case the VDEF on the original stmt was released, value-number
7030 it to the VUSE. This is to make vuse_ssa_val able to skip
7031 released virtual operands. */
7032 if (vdef != gimple_vdef (stmt))
7034 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7035 VN_INFO (vdef)->valnum = vuse;
7038 /* If we removed EH side-effects from the statement, clean
7039 its EH information. */
7040 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7042 bitmap_set_bit (need_eh_cleanup,
7043 gimple_bb (stmt)->index);
7044 if (dump_file && (dump_flags & TDF_DETAILS))
7045 fprintf (dump_file, " Removed EH side-effects.\n");
7048 /* Likewise for AB side-effects. */
7049 if (can_make_abnormal_goto
7050 && !stmt_can_make_abnormal_goto (stmt))
7052 bitmap_set_bit (need_ab_cleanup,
7053 gimple_bb (stmt)->index);
7054 if (dump_file && (dump_flags & TDF_DETAILS))
7055 fprintf (dump_file, " Removed AB side-effects.\n");
7058 return;
7062 /* If the statement is a scalar store, see if the expression
7063 has the same value number as its rhs. If so, the store is
7064 dead. */
7065 if (gimple_assign_single_p (stmt)
7066 && !gimple_has_volatile_ops (stmt)
7067 && !is_gimple_reg (gimple_assign_lhs (stmt))
7068 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7069 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7071 tree rhs = gimple_assign_rhs1 (stmt);
7072 vn_reference_t vnresult;
7073 /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7074 typed load of a byte known to be 0x11 as 1 so a store of
7075 a boolean 1 is detected as redundant. Because of this we
7076 have to make sure to lookup with a ref where its size
7077 matches the precision. */
7078 tree lookup_lhs = lhs;
7079 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7080 && (TREE_CODE (lhs) != COMPONENT_REF
7081 || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7082 && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7084 if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
7085 && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7086 lookup_lhs = NULL_TREE;
7087 else if (TREE_CODE (lhs) == COMPONENT_REF
7088 || TREE_CODE (lhs) == MEM_REF)
7090 tree ltype = build_nonstandard_integer_type
7091 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7092 TYPE_UNSIGNED (TREE_TYPE (lhs)));
7093 if (TREE_CODE (lhs) == COMPONENT_REF)
7095 tree foff = component_ref_field_offset (lhs);
7096 tree f = TREE_OPERAND (lhs, 1);
7097 if (!poly_int_tree_p (foff))
7098 lookup_lhs = NULL_TREE;
7099 else
7100 lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7101 TREE_OPERAND (lhs, 0),
7102 TYPE_SIZE (TREE_TYPE (lhs)),
7103 bit_from_pos
7104 (foff, DECL_FIELD_BIT_OFFSET (f)));
7106 else
7107 lookup_lhs = build2 (MEM_REF, ltype,
7108 TREE_OPERAND (lhs, 0),
7109 TREE_OPERAND (lhs, 1));
7111 else
7112 lookup_lhs = NULL_TREE;
7114 tree val = NULL_TREE;
7115 if (lookup_lhs)
7116 val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7117 VN_WALKREWRITE, &vnresult, false,
7118 NULL, NULL_TREE, true);
7119 if (TREE_CODE (rhs) == SSA_NAME)
7120 rhs = VN_INFO (rhs)->valnum;
7121 if (val
7122 && (operand_equal_p (val, rhs, 0)
7123 /* Due to the bitfield lookups above we can get bit
7124 interpretations of the same RHS as values here. Those
7125 are redundant as well. */
7126 || (TREE_CODE (val) == SSA_NAME
7127 && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7128 && (val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7129 && TREE_CODE (val) == VIEW_CONVERT_EXPR
7130 && TREE_OPERAND (val, 0) == rhs)))
7132 /* We can only remove the later store if the former aliases
7133 at least all accesses the later one does or if the store
7134 was to readonly memory storing the same value. */
7135 ao_ref lhs_ref;
7136 ao_ref_init (&lhs_ref, lhs);
7137 alias_set_type set = ao_ref_alias_set (&lhs_ref);
7138 alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7139 if (! vnresult
7140 || ((vnresult->set == set
7141 || alias_set_subset_of (set, vnresult->set))
7142 && (vnresult->base_set == base_set
7143 || alias_set_subset_of (base_set, vnresult->base_set))))
7145 if (dump_file && (dump_flags & TDF_DETAILS))
7147 fprintf (dump_file, "Deleted redundant store ");
7148 print_gimple_stmt (dump_file, stmt, 0);
7151 /* Queue stmt for removal. */
7152 to_remove.safe_push (stmt);
7153 return;
7158 /* If this is a control statement value numbering left edges
7159 unexecuted on force the condition in a way consistent with
7160 that. */
7161 if (gcond *cond = dyn_cast <gcond *> (stmt))
7163 if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7164 ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7166 if (dump_file && (dump_flags & TDF_DETAILS))
7168 fprintf (dump_file, "Removing unexecutable edge from ");
7169 print_gimple_stmt (dump_file, stmt, 0);
7171 if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7172 == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7173 gimple_cond_make_true (cond);
7174 else
7175 gimple_cond_make_false (cond);
7176 update_stmt (cond);
7177 el_todo |= TODO_cleanup_cfg;
7178 return;
7182 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7183 bool was_noreturn = (is_gimple_call (stmt)
7184 && gimple_call_noreturn_p (stmt));
7185 tree vdef = gimple_vdef (stmt);
7186 tree vuse = gimple_vuse (stmt);
7188 /* If we didn't replace the whole stmt (or propagate the result
7189 into all uses), replace all uses on this stmt with their
7190 leaders. */
7191 bool modified = false;
7192 use_operand_p use_p;
7193 ssa_op_iter iter;
7194 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7196 tree use = USE_FROM_PTR (use_p);
7197 /* ??? The call code above leaves stmt operands un-updated. */
7198 if (TREE_CODE (use) != SSA_NAME)
7199 continue;
7200 tree sprime;
7201 if (SSA_NAME_IS_DEFAULT_DEF (use))
7202 /* ??? For default defs BB shouldn't matter, but we have to
7203 solve the inconsistency between rpo eliminate and
7204 dom eliminate avail valueization first. */
7205 sprime = eliminate_avail (b, use);
7206 else
7207 /* Look for sth available at the definition block of the argument.
7208 This avoids inconsistencies between availability there which
7209 decides if the stmt can be removed and availability at the
7210 use site. The SSA property ensures that things available
7211 at the definition are also available at uses. */
7212 sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7213 if (sprime && sprime != use
7214 && may_propagate_copy (use, sprime, true)
7215 /* We substitute into debug stmts to avoid excessive
7216 debug temporaries created by removed stmts, but we need
7217 to avoid doing so for inserted sprimes as we never want
7218 to create debug temporaries for them. */
7219 && (!inserted_exprs
7220 || TREE_CODE (sprime) != SSA_NAME
7221 || !is_gimple_debug (stmt)
7222 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7224 propagate_value (use_p, sprime);
7225 modified = true;
7229 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7230 into which is a requirement for the IPA devirt machinery. */
7231 gimple *old_stmt = stmt;
7232 if (modified)
7234 /* If a formerly non-invariant ADDR_EXPR is turned into an
7235 invariant one it was on a separate stmt. */
7236 if (gimple_assign_single_p (stmt)
7237 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7238 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7239 gimple_stmt_iterator prev = *gsi;
7240 gsi_prev (&prev);
7241 if (fold_stmt (gsi, follow_all_ssa_edges))
7243 /* fold_stmt may have created new stmts inbetween
7244 the previous stmt and the folded stmt. Mark
7245 all defs created there as varying to not confuse
7246 the SCCVN machinery as we're using that even during
7247 elimination. */
7248 if (gsi_end_p (prev))
7249 prev = gsi_start_bb (b);
7250 else
7251 gsi_next (&prev);
7252 if (gsi_stmt (prev) != gsi_stmt (*gsi))
7255 tree def;
7256 ssa_op_iter dit;
7257 FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7258 dit, SSA_OP_ALL_DEFS)
7259 /* As existing DEFs may move between stmts
7260 only process new ones. */
7261 if (! has_VN_INFO (def))
7263 vn_ssa_aux_t vn_info = VN_INFO (def);
7264 vn_info->valnum = def;
7265 vn_info->visited = true;
7267 if (gsi_stmt (prev) == gsi_stmt (*gsi))
7268 break;
7269 gsi_next (&prev);
7271 while (1);
7273 stmt = gsi_stmt (*gsi);
7274 /* In case we folded the stmt away schedule the NOP for removal. */
7275 if (gimple_nop_p (stmt))
7276 to_remove.safe_push (stmt);
7279 /* Visit indirect calls and turn them into direct calls if
7280 possible using the devirtualization machinery. Do this before
7281 checking for required EH/abnormal/noreturn cleanup as devird
7282 may expose more of those. */
7283 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7285 tree fn = gimple_call_fn (call_stmt);
7286 if (fn
7287 && flag_devirtualize
7288 && virtual_method_call_p (fn))
7290 tree otr_type = obj_type_ref_class (fn);
7291 unsigned HOST_WIDE_INT otr_tok
7292 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7293 tree instance;
7294 ipa_polymorphic_call_context context (current_function_decl,
7295 fn, stmt, &instance);
7296 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7297 otr_type, stmt, NULL);
7298 bool final;
7299 vec <cgraph_node *> targets
7300 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7301 otr_tok, context, &final);
7302 if (dump_file)
7303 dump_possible_polymorphic_call_targets (dump_file,
7304 obj_type_ref_class (fn),
7305 otr_tok, context);
7306 if (final && targets.length () <= 1 && dbg_cnt (devirt))
7308 tree fn;
7309 if (targets.length () == 1)
7310 fn = targets[0]->decl;
7311 else
7312 fn = builtin_decl_unreachable ();
7313 if (dump_enabled_p ())
7315 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7316 "converting indirect call to "
7317 "function %s\n",
7318 lang_hooks.decl_printable_name (fn, 2));
7320 gimple_call_set_fndecl (call_stmt, fn);
7321 /* If changing the call to __builtin_unreachable
7322 or similar noreturn function, adjust gimple_call_fntype
7323 too. */
7324 if (gimple_call_noreturn_p (call_stmt)
7325 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7326 && TYPE_ARG_TYPES (TREE_TYPE (fn))
7327 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7328 == void_type_node))
7329 gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7330 maybe_remove_unused_call_args (cfun, call_stmt);
7331 modified = true;
7336 if (modified)
7338 /* When changing a call into a noreturn call, cfg cleanup
7339 is needed to fix up the noreturn call. */
7340 if (!was_noreturn
7341 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7342 to_fixup.safe_push (stmt);
7343 /* When changing a condition or switch into one we know what
7344 edge will be executed, schedule a cfg cleanup. */
7345 if ((gimple_code (stmt) == GIMPLE_COND
7346 && (gimple_cond_true_p (as_a <gcond *> (stmt))
7347 || gimple_cond_false_p (as_a <gcond *> (stmt))))
7348 || (gimple_code (stmt) == GIMPLE_SWITCH
7349 && TREE_CODE (gimple_switch_index
7350 (as_a <gswitch *> (stmt))) == INTEGER_CST))
7351 el_todo |= TODO_cleanup_cfg;
7352 /* If we removed EH side-effects from the statement, clean
7353 its EH information. */
7354 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7356 bitmap_set_bit (need_eh_cleanup,
7357 gimple_bb (stmt)->index);
7358 if (dump_file && (dump_flags & TDF_DETAILS))
7359 fprintf (dump_file, " Removed EH side-effects.\n");
7361 /* Likewise for AB side-effects. */
7362 if (can_make_abnormal_goto
7363 && !stmt_can_make_abnormal_goto (stmt))
7365 bitmap_set_bit (need_ab_cleanup,
7366 gimple_bb (stmt)->index);
7367 if (dump_file && (dump_flags & TDF_DETAILS))
7368 fprintf (dump_file, " Removed AB side-effects.\n");
7370 update_stmt (stmt);
7371 /* In case the VDEF on the original stmt was released, value-number
7372 it to the VUSE. This is to make vuse_ssa_val able to skip
7373 released virtual operands. */
7374 if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7375 VN_INFO (vdef)->valnum = vuse;
7378 /* Make new values available - for fully redundant LHS we
7379 continue with the next stmt above and skip this.
7380 But avoid picking up dead defs. */
7381 tree def;
7382 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7383 if (! has_zero_uses (def)
7384 || (inserted_exprs
7385 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7386 eliminate_push_avail (b, def);
7389 /* Perform elimination for the basic-block B during the domwalk. */
7391 edge
7392 eliminate_dom_walker::before_dom_children (basic_block b)
7394 /* Mark new bb. */
7395 avail_stack.safe_push (NULL_TREE);
7397 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7398 if (!(b->flags & BB_EXECUTABLE))
7399 return NULL;
7401 vn_context_bb = b;
7403 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7405 gphi *phi = gsi.phi ();
7406 tree res = PHI_RESULT (phi);
7408 if (virtual_operand_p (res))
7410 gsi_next (&gsi);
7411 continue;
7414 tree sprime = eliminate_avail (b, res);
7415 if (sprime
7416 && sprime != res)
7418 if (dump_file && (dump_flags & TDF_DETAILS))
7420 fprintf (dump_file, "Replaced redundant PHI node defining ");
7421 print_generic_expr (dump_file, res);
7422 fprintf (dump_file, " with ");
7423 print_generic_expr (dump_file, sprime);
7424 fprintf (dump_file, "\n");
7427 /* If we inserted this PHI node ourself, it's not an elimination. */
7428 if (! inserted_exprs
7429 || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
7430 eliminations++;
7432 /* If we will propagate into all uses don't bother to do
7433 anything. */
7434 if (may_propagate_copy (res, sprime))
7436 /* Mark the PHI for removal. */
7437 to_remove.safe_push (phi);
7438 gsi_next (&gsi);
7439 continue;
7442 remove_phi_node (&gsi, false);
7444 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
7445 sprime = fold_convert (TREE_TYPE (res), sprime);
7446 gimple *stmt = gimple_build_assign (res, sprime);
7447 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
7448 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
7449 continue;
7452 eliminate_push_avail (b, res);
7453 gsi_next (&gsi);
7456 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
7457 !gsi_end_p (gsi);
7458 gsi_next (&gsi))
7459 eliminate_stmt (b, &gsi);
7461 /* Replace destination PHI arguments. */
7462 edge_iterator ei;
7463 edge e;
7464 FOR_EACH_EDGE (e, ei, b->succs)
7465 if (e->flags & EDGE_EXECUTABLE)
7466 for (gphi_iterator gsi = gsi_start_phis (e->dest);
7467 !gsi_end_p (gsi);
7468 gsi_next (&gsi))
7470 gphi *phi = gsi.phi ();
7471 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7472 tree arg = USE_FROM_PTR (use_p);
7473 if (TREE_CODE (arg) != SSA_NAME
7474 || virtual_operand_p (arg))
7475 continue;
7476 tree sprime = eliminate_avail (b, arg);
7477 if (sprime && may_propagate_copy (arg, sprime,
7478 !(e->flags & EDGE_ABNORMAL)))
7479 propagate_value (use_p, sprime);
7482 vn_context_bb = NULL;
7484 return NULL;
7487 /* Make no longer available leaders no longer available. */
7489 void
7490 eliminate_dom_walker::after_dom_children (basic_block)
7492 tree entry;
7493 while ((entry = avail_stack.pop ()) != NULL_TREE)
7495 tree valnum = VN_INFO (entry)->valnum;
7496 tree old = avail[SSA_NAME_VERSION (valnum)];
7497 if (old == entry)
7498 avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
7499 else
7500 avail[SSA_NAME_VERSION (valnum)] = entry;
7504 /* Remove queued stmts and perform delayed cleanups. */
7506 unsigned
7507 eliminate_dom_walker::eliminate_cleanup (bool region_p)
7509 statistics_counter_event (cfun, "Eliminated", eliminations);
7510 statistics_counter_event (cfun, "Insertions", insertions);
7512 /* We cannot remove stmts during BB walk, especially not release SSA
7513 names there as this confuses the VN machinery. The stmts ending
7514 up in to_remove are either stores or simple copies.
7515 Remove stmts in reverse order to make debug stmt creation possible. */
7516 while (!to_remove.is_empty ())
7518 bool do_release_defs = true;
7519 gimple *stmt = to_remove.pop ();
7521 /* When we are value-numbering a region we do not require exit PHIs to
7522 be present so we have to make sure to deal with uses outside of the
7523 region of stmts that we thought are eliminated.
7524 ??? Note we may be confused by uses in dead regions we didn't run
7525 elimination on. Rather than checking individual uses we accept
7526 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7527 contains such example). */
7528 if (region_p)
7530 if (gphi *phi = dyn_cast <gphi *> (stmt))
7532 tree lhs = gimple_phi_result (phi);
7533 if (!has_zero_uses (lhs))
7535 if (dump_file && (dump_flags & TDF_DETAILS))
7536 fprintf (dump_file, "Keeping eliminated stmt live "
7537 "as copy because of out-of-region uses\n");
7538 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7539 gimple *copy = gimple_build_assign (lhs, sprime);
7540 gimple_stmt_iterator gsi
7541 = gsi_after_labels (gimple_bb (stmt));
7542 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7543 do_release_defs = false;
7546 else if (tree lhs = gimple_get_lhs (stmt))
7547 if (TREE_CODE (lhs) == SSA_NAME
7548 && !has_zero_uses (lhs))
7550 if (dump_file && (dump_flags & TDF_DETAILS))
7551 fprintf (dump_file, "Keeping eliminated stmt live "
7552 "as copy because of out-of-region uses\n");
7553 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7554 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7555 if (is_gimple_assign (stmt))
7557 gimple_assign_set_rhs_from_tree (&gsi, sprime);
7558 stmt = gsi_stmt (gsi);
7559 update_stmt (stmt);
7560 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
7561 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
7562 continue;
7564 else
7566 gimple *copy = gimple_build_assign (lhs, sprime);
7567 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7568 do_release_defs = false;
7573 if (dump_file && (dump_flags & TDF_DETAILS))
7575 fprintf (dump_file, "Removing dead stmt ");
7576 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
7579 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7580 if (gimple_code (stmt) == GIMPLE_PHI)
7581 remove_phi_node (&gsi, do_release_defs);
7582 else
7584 basic_block bb = gimple_bb (stmt);
7585 unlink_stmt_vdef (stmt);
7586 if (gsi_remove (&gsi, true))
7587 bitmap_set_bit (need_eh_cleanup, bb->index);
7588 if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
7589 bitmap_set_bit (need_ab_cleanup, bb->index);
7590 if (do_release_defs)
7591 release_defs (stmt);
7594 /* Removing a stmt may expose a forwarder block. */
7595 el_todo |= TODO_cleanup_cfg;
7598 /* Fixup stmts that became noreturn calls. This may require splitting
7599 blocks and thus isn't possible during the dominator walk. Do this
7600 in reverse order so we don't inadvertedly remove a stmt we want to
7601 fixup by visiting a dominating now noreturn call first. */
7602 while (!to_fixup.is_empty ())
7604 gimple *stmt = to_fixup.pop ();
7606 if (dump_file && (dump_flags & TDF_DETAILS))
7608 fprintf (dump_file, "Fixing up noreturn call ");
7609 print_gimple_stmt (dump_file, stmt, 0);
7612 if (fixup_noreturn_call (stmt))
7613 el_todo |= TODO_cleanup_cfg;
7616 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
7617 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
7619 if (do_eh_cleanup)
7620 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
7622 if (do_ab_cleanup)
7623 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
7625 if (do_eh_cleanup || do_ab_cleanup)
7626 el_todo |= TODO_cleanup_cfg;
7628 return el_todo;
7631 /* Eliminate fully redundant computations. */
7633 unsigned
7634 eliminate_with_rpo_vn (bitmap inserted_exprs)
7636 eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
7638 eliminate_dom_walker *saved_rpo_avail = rpo_avail;
7639 rpo_avail = &walker;
7640 walker.walk (cfun->cfg->x_entry_block_ptr);
7641 rpo_avail = saved_rpo_avail;
7643 return walker.eliminate_cleanup ();
7646 static unsigned
7647 do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
7648 bool iterate, bool eliminate, bool skip_entry_phis,
7649 vn_lookup_kind kind);
7651 void
7652 run_rpo_vn (vn_lookup_kind kind)
7654 do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
7656 /* ??? Prune requirement of these. */
7657 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
7659 /* Initialize the value ids and prune out remaining VN_TOPs
7660 from dead code. */
7661 tree name;
7662 unsigned i;
7663 FOR_EACH_SSA_NAME (i, name, cfun)
7665 vn_ssa_aux_t info = VN_INFO (name);
7666 if (!info->visited
7667 || info->valnum == VN_TOP)
7668 info->valnum = name;
7669 if (info->valnum == name)
7670 info->value_id = get_next_value_id ();
7671 else if (is_gimple_min_invariant (info->valnum))
7672 info->value_id = get_or_alloc_constant_value_id (info->valnum);
7675 /* Propagate. */
7676 FOR_EACH_SSA_NAME (i, name, cfun)
7678 vn_ssa_aux_t info = VN_INFO (name);
7679 if (TREE_CODE (info->valnum) == SSA_NAME
7680 && info->valnum != name
7681 && info->value_id != VN_INFO (info->valnum)->value_id)
7682 info->value_id = VN_INFO (info->valnum)->value_id;
7685 set_hashtable_value_ids ();
7687 if (dump_file && (dump_flags & TDF_DETAILS))
7689 fprintf (dump_file, "Value numbers:\n");
7690 FOR_EACH_SSA_NAME (i, name, cfun)
7692 if (VN_INFO (name)->visited
7693 && SSA_VAL (name) != name)
7695 print_generic_expr (dump_file, name);
7696 fprintf (dump_file, " = ");
7697 print_generic_expr (dump_file, SSA_VAL (name));
7698 fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
7704 /* Free VN associated data structures. */
7706 void
7707 free_rpo_vn (void)
7709 free_vn_table (valid_info);
7710 XDELETE (valid_info);
7711 obstack_free (&vn_tables_obstack, NULL);
7712 obstack_free (&vn_tables_insert_obstack, NULL);
7714 vn_ssa_aux_iterator_type it;
7715 vn_ssa_aux_t info;
7716 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
7717 if (info->needs_insertion)
7718 release_ssa_name (info->name);
7719 obstack_free (&vn_ssa_aux_obstack, NULL);
7720 delete vn_ssa_aux_hash;
7722 delete constant_to_value_id;
7723 constant_to_value_id = NULL;
7726 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7728 static tree
7729 vn_lookup_simplify_result (gimple_match_op *res_op)
7731 if (!res_op->code.is_tree_code ())
7732 return NULL_TREE;
7733 tree *ops = res_op->ops;
7734 unsigned int length = res_op->num_ops;
7735 if (res_op->code == CONSTRUCTOR
7736 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7737 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7738 && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
7740 length = CONSTRUCTOR_NELTS (res_op->ops[0]);
7741 ops = XALLOCAVEC (tree, length);
7742 for (unsigned i = 0; i < length; ++i)
7743 ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
7745 vn_nary_op_t vnresult = NULL;
7746 tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
7747 res_op->type, ops, &vnresult);
7748 /* If this is used from expression simplification make sure to
7749 return an available expression. */
7750 if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
7751 res = rpo_avail->eliminate_avail (vn_context_bb, res);
7752 return res;
7755 /* Return a leader for OPs value that is valid at BB. */
7757 tree
7758 rpo_elim::eliminate_avail (basic_block bb, tree op)
7760 bool visited;
7761 tree valnum = SSA_VAL (op, &visited);
7762 /* If we didn't visit OP then it must be defined outside of the
7763 region we process and also dominate it. So it is available. */
7764 if (!visited)
7765 return op;
7766 if (TREE_CODE (valnum) == SSA_NAME)
7768 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
7769 return valnum;
7770 vn_ssa_aux_t valnum_info = VN_INFO (valnum);
7771 vn_avail *av = valnum_info->avail;
7772 if (!av)
7774 /* See above. But when there's availability info prefer
7775 what we recorded there for example to preserve LC SSA. */
7776 if (!valnum_info->visited)
7777 return valnum;
7778 return NULL_TREE;
7780 if (av->location == bb->index)
7781 /* On tramp3d 90% of the cases are here. */
7782 return ssa_name (av->leader);
7785 basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
7786 /* ??? During elimination we have to use availability at the
7787 definition site of a use we try to replace. This
7788 is required to not run into inconsistencies because
7789 of dominated_by_p_w_unex behavior and removing a definition
7790 while not replacing all uses.
7791 ??? We could try to consistently walk dominators
7792 ignoring non-executable regions. The nearest common
7793 dominator of bb and abb is where we can stop walking. We
7794 may also be able to "pre-compute" (bits of) the next immediate
7795 (non-)dominator during the RPO walk when marking edges as
7796 executable. */
7797 if (dominated_by_p_w_unex (bb, abb, true))
7799 tree leader = ssa_name (av->leader);
7800 /* Prevent eliminations that break loop-closed SSA. */
7801 if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
7802 && ! SSA_NAME_IS_DEFAULT_DEF (leader)
7803 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7804 (leader))->loop_father,
7805 bb))
7806 return NULL_TREE;
7807 if (dump_file && (dump_flags & TDF_DETAILS))
7809 print_generic_expr (dump_file, leader);
7810 fprintf (dump_file, " is available for ");
7811 print_generic_expr (dump_file, valnum);
7812 fprintf (dump_file, "\n");
7814 /* On tramp3d 99% of the _remaining_ cases succeed at
7815 the first enty. */
7816 return leader;
7818 /* ??? Can we somehow skip to the immediate dominator
7819 RPO index (bb_to_rpo)? Again, maybe not worth, on
7820 tramp3d the worst number of elements in the vector is 9. */
7821 av = av->next;
7823 while (av);
7824 /* While we prefer avail we have to fallback to using the value
7825 directly if defined outside of the region when none of the
7826 available defs suit. */
7827 if (!valnum_info->visited)
7828 return valnum;
7830 else if (valnum != VN_TOP)
7831 /* valnum is is_gimple_min_invariant. */
7832 return valnum;
7833 return NULL_TREE;
7836 /* Make LEADER a leader for its value at BB. */
7838 void
7839 rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
7841 tree valnum = VN_INFO (leader)->valnum;
7842 if (valnum == VN_TOP
7843 || is_gimple_min_invariant (valnum))
7844 return;
7845 if (dump_file && (dump_flags & TDF_DETAILS))
7847 fprintf (dump_file, "Making available beyond BB%d ", bb->index);
7848 print_generic_expr (dump_file, leader);
7849 fprintf (dump_file, " for value ");
7850 print_generic_expr (dump_file, valnum);
7851 fprintf (dump_file, "\n");
7853 vn_ssa_aux_t value = VN_INFO (valnum);
7854 vn_avail *av;
7855 if (m_avail_freelist)
7857 av = m_avail_freelist;
7858 m_avail_freelist = m_avail_freelist->next;
7860 else
7861 av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
7862 av->location = bb->index;
7863 av->leader = SSA_NAME_VERSION (leader);
7864 av->next = value->avail;
7865 av->next_undo = last_pushed_avail;
7866 last_pushed_avail = value;
7867 value->avail = av;
7870 /* Valueization hook for RPO VN plus required state. */
7872 tree
7873 rpo_vn_valueize (tree name)
7875 if (TREE_CODE (name) == SSA_NAME)
7877 vn_ssa_aux_t val = VN_INFO (name);
7878 if (val)
7880 tree tem = val->valnum;
7881 if (tem != VN_TOP && tem != name)
7883 if (TREE_CODE (tem) != SSA_NAME)
7884 return tem;
7885 /* For all values we only valueize to an available leader
7886 which means we can use SSA name info without restriction. */
7887 tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
7888 if (tem)
7889 return tem;
7893 return name;
7896 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7897 inverted condition. */
7899 static void
7900 insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
7902 switch (code)
7904 case LT_EXPR:
7905 /* a < b -> a {!,<}= b */
7906 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7907 ops, boolean_true_node, 0, pred_e);
7908 vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
7909 ops, boolean_true_node, 0, pred_e);
7910 /* a < b -> ! a {>,=} b */
7911 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7912 ops, boolean_false_node, 0, pred_e);
7913 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7914 ops, boolean_false_node, 0, pred_e);
7915 break;
7916 case GT_EXPR:
7917 /* a > b -> a {!,>}= b */
7918 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7919 ops, boolean_true_node, 0, pred_e);
7920 vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
7921 ops, boolean_true_node, 0, pred_e);
7922 /* a > b -> ! a {<,=} b */
7923 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7924 ops, boolean_false_node, 0, pred_e);
7925 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7926 ops, boolean_false_node, 0, pred_e);
7927 break;
7928 case EQ_EXPR:
7929 /* a == b -> ! a {<,>} b */
7930 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7931 ops, boolean_false_node, 0, pred_e);
7932 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7933 ops, boolean_false_node, 0, pred_e);
7934 break;
7935 case LE_EXPR:
7936 case GE_EXPR:
7937 case NE_EXPR:
7938 /* Nothing besides inverted condition. */
7939 break;
7940 default:;
7944 /* Main stmt worker for RPO VN, process BB. */
7946 static unsigned
7947 process_bb (rpo_elim &avail, basic_block bb,
7948 bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
7949 bool do_region, bitmap exit_bbs, bool skip_phis)
7951 unsigned todo = 0;
7952 edge_iterator ei;
7953 edge e;
7955 vn_context_bb = bb;
7957 /* If we are in loop-closed SSA preserve this state. This is
7958 relevant when called on regions from outside of FRE/PRE. */
7959 bool lc_phi_nodes = false;
7960 if (!skip_phis
7961 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
7962 FOR_EACH_EDGE (e, ei, bb->preds)
7963 if (e->src->loop_father != e->dest->loop_father
7964 && flow_loop_nested_p (e->dest->loop_father,
7965 e->src->loop_father))
7967 lc_phi_nodes = true;
7968 break;
7971 /* When we visit a loop header substitute into loop info. */
7972 if (!iterate && eliminate && bb->loop_father->header == bb)
7974 /* Keep fields in sync with substitute_in_loop_info. */
7975 if (bb->loop_father->nb_iterations)
7976 bb->loop_father->nb_iterations
7977 = simplify_replace_tree (bb->loop_father->nb_iterations,
7978 NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
7981 /* Value-number all defs in the basic-block. */
7982 if (!skip_phis)
7983 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7984 gsi_next (&gsi))
7986 gphi *phi = gsi.phi ();
7987 tree res = PHI_RESULT (phi);
7988 vn_ssa_aux_t res_info = VN_INFO (res);
7989 if (!bb_visited)
7991 gcc_assert (!res_info->visited);
7992 res_info->valnum = VN_TOP;
7993 res_info->visited = true;
7996 /* When not iterating force backedge values to varying. */
7997 visit_stmt (phi, !iterate_phis);
7998 if (virtual_operand_p (res))
7999 continue;
8001 /* Eliminate */
8002 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
8003 how we handle backedges and availability.
8004 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
8005 tree val = res_info->valnum;
8006 if (res != val && !iterate && eliminate)
8008 if (tree leader = avail.eliminate_avail (bb, res))
8010 if (leader != res
8011 /* Preserve loop-closed SSA form. */
8012 && (! lc_phi_nodes
8013 || is_gimple_min_invariant (leader)))
8015 if (dump_file && (dump_flags & TDF_DETAILS))
8017 fprintf (dump_file, "Replaced redundant PHI node "
8018 "defining ");
8019 print_generic_expr (dump_file, res);
8020 fprintf (dump_file, " with ");
8021 print_generic_expr (dump_file, leader);
8022 fprintf (dump_file, "\n");
8024 avail.eliminations++;
8026 if (may_propagate_copy (res, leader))
8028 /* Schedule for removal. */
8029 avail.to_remove.safe_push (phi);
8030 continue;
8032 /* ??? Else generate a copy stmt. */
8036 /* Only make defs available that not already are. But make
8037 sure loop-closed SSA PHI node defs are picked up for
8038 downstream uses. */
8039 if (lc_phi_nodes
8040 || res == val
8041 || ! avail.eliminate_avail (bb, res))
8042 avail.eliminate_push_avail (bb, res);
8045 /* For empty BBs mark outgoing edges executable. For non-empty BBs
8046 we do this when processing the last stmt as we have to do this
8047 before elimination which otherwise forces GIMPLE_CONDs to
8048 if (1 != 0) style when seeing non-executable edges. */
8049 if (gsi_end_p (gsi_start_bb (bb)))
8051 FOR_EACH_EDGE (e, ei, bb->succs)
8053 if (!(e->flags & EDGE_EXECUTABLE))
8055 if (dump_file && (dump_flags & TDF_DETAILS))
8056 fprintf (dump_file,
8057 "marking outgoing edge %d -> %d executable\n",
8058 e->src->index, e->dest->index);
8059 e->flags |= EDGE_EXECUTABLE;
8060 e->dest->flags |= BB_EXECUTABLE;
8062 else if (!(e->dest->flags & BB_EXECUTABLE))
8064 if (dump_file && (dump_flags & TDF_DETAILS))
8065 fprintf (dump_file,
8066 "marking destination block %d reachable\n",
8067 e->dest->index);
8068 e->dest->flags |= BB_EXECUTABLE;
8072 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8073 !gsi_end_p (gsi); gsi_next (&gsi))
8075 ssa_op_iter i;
8076 tree op;
8077 if (!bb_visited)
8079 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8081 vn_ssa_aux_t op_info = VN_INFO (op);
8082 gcc_assert (!op_info->visited);
8083 op_info->valnum = VN_TOP;
8084 op_info->visited = true;
8087 /* We somehow have to deal with uses that are not defined
8088 in the processed region. Forcing unvisited uses to
8089 varying here doesn't play well with def-use following during
8090 expression simplification, so we deal with this by checking
8091 the visited flag in SSA_VAL. */
8094 visit_stmt (gsi_stmt (gsi));
8096 gimple *last = gsi_stmt (gsi);
8097 e = NULL;
8098 switch (gimple_code (last))
8100 case GIMPLE_SWITCH:
8101 e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8102 (as_a <gswitch *> (last))));
8103 break;
8104 case GIMPLE_COND:
8106 tree lhs = vn_valueize (gimple_cond_lhs (last));
8107 tree rhs = vn_valueize (gimple_cond_rhs (last));
8108 tree val = gimple_simplify (gimple_cond_code (last),
8109 boolean_type_node, lhs, rhs,
8110 NULL, vn_valueize);
8111 /* If the condition didn't simplfy see if we have recorded
8112 an expression from sofar taken edges. */
8113 if (! val || TREE_CODE (val) != INTEGER_CST)
8115 vn_nary_op_t vnresult;
8116 tree ops[2];
8117 ops[0] = lhs;
8118 ops[1] = rhs;
8119 val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
8120 boolean_type_node, ops,
8121 &vnresult);
8122 /* Did we get a predicated value? */
8123 if (! val && vnresult && vnresult->predicated_values)
8125 val = vn_nary_op_get_predicated_value (vnresult, bb);
8126 if (val && dump_file && (dump_flags & TDF_DETAILS))
8128 fprintf (dump_file, "Got predicated value ");
8129 print_generic_expr (dump_file, val, TDF_NONE);
8130 fprintf (dump_file, " for ");
8131 print_gimple_stmt (dump_file, last, TDF_SLIM);
8135 if (val)
8136 e = find_taken_edge (bb, val);
8137 if (! e)
8139 /* If we didn't manage to compute the taken edge then
8140 push predicated expressions for the condition itself
8141 and related conditions to the hashtables. This allows
8142 simplification of redundant conditions which is
8143 important as early cleanup. */
8144 edge true_e, false_e;
8145 extract_true_false_edges_from_block (bb, &true_e, &false_e);
8146 enum tree_code code = gimple_cond_code (last);
8147 enum tree_code icode
8148 = invert_tree_comparison (code, HONOR_NANS (lhs));
8149 tree ops[2];
8150 ops[0] = lhs;
8151 ops[1] = rhs;
8152 if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8153 || !can_track_predicate_on_edge (true_e))
8154 true_e = NULL;
8155 if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8156 || !can_track_predicate_on_edge (false_e))
8157 false_e = NULL;
8158 if (true_e)
8159 vn_nary_op_insert_pieces_predicated
8160 (2, code, boolean_type_node, ops,
8161 boolean_true_node, 0, true_e);
8162 if (false_e)
8163 vn_nary_op_insert_pieces_predicated
8164 (2, code, boolean_type_node, ops,
8165 boolean_false_node, 0, false_e);
8166 if (icode != ERROR_MARK)
8168 if (true_e)
8169 vn_nary_op_insert_pieces_predicated
8170 (2, icode, boolean_type_node, ops,
8171 boolean_false_node, 0, true_e);
8172 if (false_e)
8173 vn_nary_op_insert_pieces_predicated
8174 (2, icode, boolean_type_node, ops,
8175 boolean_true_node, 0, false_e);
8177 /* Relax for non-integers, inverted condition handled
8178 above. */
8179 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8181 if (true_e)
8182 insert_related_predicates_on_edge (code, ops, true_e);
8183 if (false_e)
8184 insert_related_predicates_on_edge (icode, ops, false_e);
8187 break;
8189 case GIMPLE_GOTO:
8190 e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8191 break;
8192 default:
8193 e = NULL;
8195 if (e)
8197 todo = TODO_cleanup_cfg;
8198 if (!(e->flags & EDGE_EXECUTABLE))
8200 if (dump_file && (dump_flags & TDF_DETAILS))
8201 fprintf (dump_file,
8202 "marking known outgoing %sedge %d -> %d executable\n",
8203 e->flags & EDGE_DFS_BACK ? "back-" : "",
8204 e->src->index, e->dest->index);
8205 e->flags |= EDGE_EXECUTABLE;
8206 e->dest->flags |= BB_EXECUTABLE;
8208 else if (!(e->dest->flags & BB_EXECUTABLE))
8210 if (dump_file && (dump_flags & TDF_DETAILS))
8211 fprintf (dump_file,
8212 "marking destination block %d reachable\n",
8213 e->dest->index);
8214 e->dest->flags |= BB_EXECUTABLE;
8217 else if (gsi_one_before_end_p (gsi))
8219 FOR_EACH_EDGE (e, ei, bb->succs)
8221 if (!(e->flags & EDGE_EXECUTABLE))
8223 if (dump_file && (dump_flags & TDF_DETAILS))
8224 fprintf (dump_file,
8225 "marking outgoing edge %d -> %d executable\n",
8226 e->src->index, e->dest->index);
8227 e->flags |= EDGE_EXECUTABLE;
8228 e->dest->flags |= BB_EXECUTABLE;
8230 else if (!(e->dest->flags & BB_EXECUTABLE))
8232 if (dump_file && (dump_flags & TDF_DETAILS))
8233 fprintf (dump_file,
8234 "marking destination block %d reachable\n",
8235 e->dest->index);
8236 e->dest->flags |= BB_EXECUTABLE;
8241 /* Eliminate. That also pushes to avail. */
8242 if (eliminate && ! iterate)
8243 avail.eliminate_stmt (bb, &gsi);
8244 else
8245 /* If not eliminating, make all not already available defs
8246 available. But avoid picking up dead defs. */
8247 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8248 if (! has_zero_uses (op)
8249 && ! avail.eliminate_avail (bb, op))
8250 avail.eliminate_push_avail (bb, op);
8253 /* Eliminate in destination PHI arguments. Always substitute in dest
8254 PHIs, even for non-executable edges. This handles region
8255 exits PHIs. */
8256 if (!iterate && eliminate)
8257 FOR_EACH_EDGE (e, ei, bb->succs)
8258 for (gphi_iterator gsi = gsi_start_phis (e->dest);
8259 !gsi_end_p (gsi); gsi_next (&gsi))
8261 gphi *phi = gsi.phi ();
8262 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8263 tree arg = USE_FROM_PTR (use_p);
8264 if (TREE_CODE (arg) != SSA_NAME
8265 || virtual_operand_p (arg))
8266 continue;
8267 tree sprime;
8268 if (SSA_NAME_IS_DEFAULT_DEF (arg))
8270 sprime = SSA_VAL (arg);
8271 gcc_assert (TREE_CODE (sprime) != SSA_NAME
8272 || SSA_NAME_IS_DEFAULT_DEF (sprime));
8274 else
8275 /* Look for sth available at the definition block of the argument.
8276 This avoids inconsistencies between availability there which
8277 decides if the stmt can be removed and availability at the
8278 use site. The SSA property ensures that things available
8279 at the definition are also available at uses. */
8280 sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8281 arg);
8282 if (sprime
8283 && sprime != arg
8284 && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8285 propagate_value (use_p, sprime);
8288 vn_context_bb = NULL;
8289 return todo;
8292 /* Unwind state per basic-block. */
8294 struct unwind_state
8296 /* Times this block has been visited. */
8297 unsigned visited;
8298 /* Whether to handle this as iteration point or whether to treat
8299 incoming backedge PHI values as varying. */
8300 bool iterate;
8301 /* Maximum RPO index this block is reachable from. */
8302 int max_rpo;
8303 /* Unwind state. */
8304 void *ob_top;
8305 vn_reference_t ref_top;
8306 vn_phi_t phi_top;
8307 vn_nary_op_t nary_top;
8308 vn_avail *avail_top;
8311 /* Unwind the RPO VN state for iteration. */
8313 static void
8314 do_unwind (unwind_state *to, rpo_elim &avail)
8316 gcc_assert (to->iterate);
8317 for (; last_inserted_nary != to->nary_top;
8318 last_inserted_nary = last_inserted_nary->next)
8320 vn_nary_op_t *slot;
8321 slot = valid_info->nary->find_slot_with_hash
8322 (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
8323 /* Predication causes the need to restore previous state. */
8324 if ((*slot)->unwind_to)
8325 *slot = (*slot)->unwind_to;
8326 else
8327 valid_info->nary->clear_slot (slot);
8329 for (; last_inserted_phi != to->phi_top;
8330 last_inserted_phi = last_inserted_phi->next)
8332 vn_phi_t *slot;
8333 slot = valid_info->phis->find_slot_with_hash
8334 (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
8335 valid_info->phis->clear_slot (slot);
8337 for (; last_inserted_ref != to->ref_top;
8338 last_inserted_ref = last_inserted_ref->next)
8340 vn_reference_t *slot;
8341 slot = valid_info->references->find_slot_with_hash
8342 (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
8343 (*slot)->operands.release ();
8344 valid_info->references->clear_slot (slot);
8346 obstack_free (&vn_tables_obstack, to->ob_top);
8348 /* Prune [rpo_idx, ] from avail. */
8349 for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
8351 vn_ssa_aux_t val = last_pushed_avail;
8352 vn_avail *av = val->avail;
8353 val->avail = av->next;
8354 last_pushed_avail = av->next_undo;
8355 av->next = avail.m_avail_freelist;
8356 avail.m_avail_freelist = av;
8360 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8361 If ITERATE is true then treat backedges optimistically as not
8362 executed and iterate. If ELIMINATE is true then perform
8363 elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8364 is true then force PHI nodes in ENTRY->dest to VARYING. */
8366 static unsigned
8367 do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8368 bool iterate, bool eliminate, bool skip_entry_phis,
8369 vn_lookup_kind kind)
8371 unsigned todo = 0;
8372 default_vn_walk_kind = kind;
8374 /* We currently do not support region-based iteration when
8375 elimination is requested. */
8376 gcc_assert (!entry || !iterate || !eliminate);
8377 /* When iterating we need loop info up-to-date. */
8378 gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
8380 bool do_region = entry != NULL;
8381 if (!do_region)
8383 entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
8384 exit_bbs = BITMAP_ALLOC (NULL);
8385 bitmap_set_bit (exit_bbs, EXIT_BLOCK);
8388 /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8389 re-mark those that are contained in the region. */
8390 edge_iterator ei;
8391 edge e;
8392 FOR_EACH_EDGE (e, ei, entry->dest->preds)
8393 e->flags &= ~EDGE_DFS_BACK;
8395 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
8396 auto_vec<std::pair<int, int> > toplevel_scc_extents;
8397 int n = rev_post_order_and_mark_dfs_back_seme
8398 (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
8400 if (!do_region)
8401 BITMAP_FREE (exit_bbs);
8403 /* If there are any non-DFS_BACK edges into entry->dest skip
8404 processing PHI nodes for that block. This supports
8405 value-numbering loop bodies w/o the actual loop. */
8406 FOR_EACH_EDGE (e, ei, entry->dest->preds)
8407 if (e != entry
8408 && !(e->flags & EDGE_DFS_BACK))
8409 break;
8410 if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
8411 fprintf (dump_file, "Region does not contain all edges into "
8412 "the entry block, skipping its PHIs.\n");
8413 skip_entry_phis |= e != NULL;
8415 int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
8416 for (int i = 0; i < n; ++i)
8417 bb_to_rpo[rpo[i]] = i;
8419 unwind_state *rpo_state = XNEWVEC (unwind_state, n);
8421 rpo_elim avail (entry->dest);
8422 rpo_avail = &avail;
8424 /* Verify we have no extra entries into the region. */
8425 if (flag_checking && do_region)
8427 auto_bb_flag bb_in_region (fn);
8428 for (int i = 0; i < n; ++i)
8430 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8431 bb->flags |= bb_in_region;
8433 /* We can't merge the first two loops because we cannot rely
8434 on EDGE_DFS_BACK for edges not within the region. But if
8435 we decide to always have the bb_in_region flag we can
8436 do the checking during the RPO walk itself (but then it's
8437 also easy to handle MEME conservatively). */
8438 for (int i = 0; i < n; ++i)
8440 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8441 edge e;
8442 edge_iterator ei;
8443 FOR_EACH_EDGE (e, ei, bb->preds)
8444 gcc_assert (e == entry
8445 || (skip_entry_phis && bb == entry->dest)
8446 || (e->src->flags & bb_in_region));
8448 for (int i = 0; i < n; ++i)
8450 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8451 bb->flags &= ~bb_in_region;
8455 /* Create the VN state. For the initial size of the various hashtables
8456 use a heuristic based on region size and number of SSA names. */
8457 unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
8458 / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
8459 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
8460 next_value_id = 1;
8461 next_constant_value_id = -1;
8463 vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
8464 gcc_obstack_init (&vn_ssa_aux_obstack);
8466 gcc_obstack_init (&vn_tables_obstack);
8467 gcc_obstack_init (&vn_tables_insert_obstack);
8468 valid_info = XCNEW (struct vn_tables_s);
8469 allocate_vn_table (valid_info, region_size);
8470 last_inserted_ref = NULL;
8471 last_inserted_phi = NULL;
8472 last_inserted_nary = NULL;
8473 last_pushed_avail = NULL;
8475 vn_valueize = rpo_vn_valueize;
8477 /* Initialize the unwind state and edge/BB executable state. */
8478 unsigned curr_scc = 0;
8479 for (int i = 0; i < n; ++i)
8481 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8482 rpo_state[i].visited = 0;
8483 rpo_state[i].max_rpo = i;
8484 if (!iterate && curr_scc < toplevel_scc_extents.length ())
8486 if (i >= toplevel_scc_extents[curr_scc].first
8487 && i <= toplevel_scc_extents[curr_scc].second)
8488 rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
8489 if (i == toplevel_scc_extents[curr_scc].second)
8490 curr_scc++;
8492 bb->flags &= ~BB_EXECUTABLE;
8493 bool has_backedges = false;
8494 edge e;
8495 edge_iterator ei;
8496 FOR_EACH_EDGE (e, ei, bb->preds)
8498 if (e->flags & EDGE_DFS_BACK)
8499 has_backedges = true;
8500 e->flags &= ~EDGE_EXECUTABLE;
8501 if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
8502 continue;
8504 rpo_state[i].iterate = iterate && has_backedges;
8506 entry->flags |= EDGE_EXECUTABLE;
8507 entry->dest->flags |= BB_EXECUTABLE;
8509 /* As heuristic to improve compile-time we handle only the N innermost
8510 loops and the outermost one optimistically. */
8511 if (iterate)
8513 unsigned max_depth = param_rpo_vn_max_loop_depth;
8514 for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
8515 if (loop_depth (loop) > max_depth)
8516 for (unsigned i = 2;
8517 i < loop_depth (loop) - max_depth; ++i)
8519 basic_block header = superloop_at_depth (loop, i)->header;
8520 bool non_latch_backedge = false;
8521 edge e;
8522 edge_iterator ei;
8523 FOR_EACH_EDGE (e, ei, header->preds)
8524 if (e->flags & EDGE_DFS_BACK)
8526 /* There can be a non-latch backedge into the header
8527 which is part of an outer irreducible region. We
8528 cannot avoid iterating this block then. */
8529 if (!dominated_by_p (CDI_DOMINATORS,
8530 e->src, e->dest))
8532 if (dump_file && (dump_flags & TDF_DETAILS))
8533 fprintf (dump_file, "non-latch backedge %d -> %d "
8534 "forces iteration of loop %d\n",
8535 e->src->index, e->dest->index, loop->num);
8536 non_latch_backedge = true;
8538 else
8539 e->flags |= EDGE_EXECUTABLE;
8541 rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
8545 uint64_t nblk = 0;
8546 int idx = 0;
8547 if (iterate)
8548 /* Go and process all blocks, iterating as necessary. */
8551 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8553 /* If the block has incoming backedges remember unwind state. This
8554 is required even for non-executable blocks since in irreducible
8555 regions we might reach them via the backedge and re-start iterating
8556 from there.
8557 Note we can individually mark blocks with incoming backedges to
8558 not iterate where we then handle PHIs conservatively. We do that
8559 heuristically to reduce compile-time for degenerate cases. */
8560 if (rpo_state[idx].iterate)
8562 rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
8563 rpo_state[idx].ref_top = last_inserted_ref;
8564 rpo_state[idx].phi_top = last_inserted_phi;
8565 rpo_state[idx].nary_top = last_inserted_nary;
8566 rpo_state[idx].avail_top
8567 = last_pushed_avail ? last_pushed_avail->avail : NULL;
8570 if (!(bb->flags & BB_EXECUTABLE))
8572 if (dump_file && (dump_flags & TDF_DETAILS))
8573 fprintf (dump_file, "Block %d: BB%d found not executable\n",
8574 idx, bb->index);
8575 idx++;
8576 continue;
8579 if (dump_file && (dump_flags & TDF_DETAILS))
8580 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8581 nblk++;
8582 todo |= process_bb (avail, bb,
8583 rpo_state[idx].visited != 0,
8584 rpo_state[idx].iterate,
8585 iterate, eliminate, do_region, exit_bbs, false);
8586 rpo_state[idx].visited++;
8588 /* Verify if changed values flow over executable outgoing backedges
8589 and those change destination PHI values (that's the thing we
8590 can easily verify). Reduce over all such edges to the farthest
8591 away PHI. */
8592 int iterate_to = -1;
8593 edge_iterator ei;
8594 edge e;
8595 FOR_EACH_EDGE (e, ei, bb->succs)
8596 if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
8597 == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
8598 && rpo_state[bb_to_rpo[e->dest->index]].iterate)
8600 int destidx = bb_to_rpo[e->dest->index];
8601 if (!rpo_state[destidx].visited)
8603 if (dump_file && (dump_flags & TDF_DETAILS))
8604 fprintf (dump_file, "Unvisited destination %d\n",
8605 e->dest->index);
8606 if (iterate_to == -1 || destidx < iterate_to)
8607 iterate_to = destidx;
8608 continue;
8610 if (dump_file && (dump_flags & TDF_DETAILS))
8611 fprintf (dump_file, "Looking for changed values of backedge"
8612 " %d->%d destination PHIs\n",
8613 e->src->index, e->dest->index);
8614 vn_context_bb = e->dest;
8615 gphi_iterator gsi;
8616 for (gsi = gsi_start_phis (e->dest);
8617 !gsi_end_p (gsi); gsi_next (&gsi))
8619 bool inserted = false;
8620 /* While we'd ideally just iterate on value changes
8621 we CSE PHIs and do that even across basic-block
8622 boundaries. So even hashtable state changes can
8623 be important (which is roughly equivalent to
8624 PHI argument value changes). To not excessively
8625 iterate because of that we track whether a PHI
8626 was CSEd to with GF_PLF_1. */
8627 bool phival_changed;
8628 if ((phival_changed = visit_phi (gsi.phi (),
8629 &inserted, false))
8630 || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
8632 if (!phival_changed
8633 && dump_file && (dump_flags & TDF_DETAILS))
8634 fprintf (dump_file, "PHI was CSEd and hashtable "
8635 "state (changed)\n");
8636 if (iterate_to == -1 || destidx < iterate_to)
8637 iterate_to = destidx;
8638 break;
8641 vn_context_bb = NULL;
8643 if (iterate_to != -1)
8645 do_unwind (&rpo_state[iterate_to], avail);
8646 idx = iterate_to;
8647 if (dump_file && (dump_flags & TDF_DETAILS))
8648 fprintf (dump_file, "Iterating to %d BB%d\n",
8649 iterate_to, rpo[iterate_to]);
8650 continue;
8653 idx++;
8655 while (idx < n);
8657 else /* !iterate */
8659 /* Process all blocks greedily with a worklist that enforces RPO
8660 processing of reachable blocks. */
8661 auto_bitmap worklist;
8662 bitmap_set_bit (worklist, 0);
8663 while (!bitmap_empty_p (worklist))
8665 int idx = bitmap_clear_first_set_bit (worklist);
8666 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8667 gcc_assert ((bb->flags & BB_EXECUTABLE)
8668 && !rpo_state[idx].visited);
8670 if (dump_file && (dump_flags & TDF_DETAILS))
8671 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8673 /* When we run into predecessor edges where we cannot trust its
8674 executable state mark them executable so PHI processing will
8675 be conservative.
8676 ??? Do we need to force arguments flowing over that edge
8677 to be varying or will they even always be? */
8678 edge_iterator ei;
8679 edge e;
8680 FOR_EACH_EDGE (e, ei, bb->preds)
8681 if (!(e->flags & EDGE_EXECUTABLE)
8682 && (bb == entry->dest
8683 || (!rpo_state[bb_to_rpo[e->src->index]].visited
8684 && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
8685 >= (int)idx))))
8687 if (dump_file && (dump_flags & TDF_DETAILS))
8688 fprintf (dump_file, "Cannot trust state of predecessor "
8689 "edge %d -> %d, marking executable\n",
8690 e->src->index, e->dest->index);
8691 e->flags |= EDGE_EXECUTABLE;
8694 nblk++;
8695 todo |= process_bb (avail, bb, false, false, false, eliminate,
8696 do_region, exit_bbs,
8697 skip_entry_phis && bb == entry->dest);
8698 rpo_state[idx].visited++;
8700 FOR_EACH_EDGE (e, ei, bb->succs)
8701 if ((e->flags & EDGE_EXECUTABLE)
8702 && e->dest->index != EXIT_BLOCK
8703 && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
8704 && !rpo_state[bb_to_rpo[e->dest->index]].visited)
8705 bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
8709 /* If statistics or dump file active. */
8710 int nex = 0;
8711 unsigned max_visited = 1;
8712 for (int i = 0; i < n; ++i)
8714 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8715 if (bb->flags & BB_EXECUTABLE)
8716 nex++;
8717 statistics_histogram_event (cfun, "RPO block visited times",
8718 rpo_state[i].visited);
8719 if (rpo_state[i].visited > max_visited)
8720 max_visited = rpo_state[i].visited;
8722 unsigned nvalues = 0, navail = 0;
8723 for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
8724 i != vn_ssa_aux_hash->end (); ++i)
8726 nvalues++;
8727 vn_avail *av = (*i)->avail;
8728 while (av)
8730 navail++;
8731 av = av->next;
8734 statistics_counter_event (cfun, "RPO blocks", n);
8735 statistics_counter_event (cfun, "RPO blocks visited", nblk);
8736 statistics_counter_event (cfun, "RPO blocks executable", nex);
8737 statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
8738 statistics_histogram_event (cfun, "RPO num values", nvalues);
8739 statistics_histogram_event (cfun, "RPO num avail", navail);
8740 statistics_histogram_event (cfun, "RPO num lattice",
8741 vn_ssa_aux_hash->elements ());
8742 if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
8744 fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
8745 " blocks in total discovering %d executable blocks iterating "
8746 "%d.%d times, a block was visited max. %u times\n",
8747 n, nblk, nex,
8748 (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
8749 max_visited);
8750 fprintf (dump_file, "RPO tracked %d values available at %d locations "
8751 "and %" PRIu64 " lattice elements\n",
8752 nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
8755 if (eliminate)
8757 /* When !iterate we already performed elimination during the RPO
8758 walk. */
8759 if (iterate)
8761 /* Elimination for region-based VN needs to be done within the
8762 RPO walk. */
8763 gcc_assert (! do_region);
8764 /* Note we can't use avail.walk here because that gets confused
8765 by the existing availability and it will be less efficient
8766 as well. */
8767 todo |= eliminate_with_rpo_vn (NULL);
8769 else
8770 todo |= avail.eliminate_cleanup (do_region);
8773 vn_valueize = NULL;
8774 rpo_avail = NULL;
8776 XDELETEVEC (bb_to_rpo);
8777 XDELETEVEC (rpo);
8778 XDELETEVEC (rpo_state);
8780 return todo;
8783 /* Region-based entry for RPO VN. Performs value-numbering and elimination
8784 on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
8785 the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
8786 are not considered.
8787 If ITERATE is true then treat backedges optimistically as not
8788 executed and iterate. If ELIMINATE is true then perform
8789 elimination, otherwise leave that to the caller.
8790 If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
8791 KIND specifies the amount of work done for handling memory operations. */
8793 unsigned
8794 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
8795 bool iterate, bool eliminate, bool skip_entry_phis,
8796 vn_lookup_kind kind)
8798 auto_timevar tv (TV_TREE_RPO_VN);
8799 unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
8800 skip_entry_phis, kind);
8801 free_rpo_vn ();
8802 return todo;
8806 namespace {
8808 const pass_data pass_data_fre =
8810 GIMPLE_PASS, /* type */
8811 "fre", /* name */
8812 OPTGROUP_NONE, /* optinfo_flags */
8813 TV_TREE_FRE, /* tv_id */
8814 ( PROP_cfg | PROP_ssa ), /* properties_required */
8815 0, /* properties_provided */
8816 0, /* properties_destroyed */
8817 0, /* todo_flags_start */
8818 0, /* todo_flags_finish */
8821 class pass_fre : public gimple_opt_pass
8823 public:
8824 pass_fre (gcc::context *ctxt)
8825 : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
8828 /* opt_pass methods: */
8829 opt_pass * clone () final override { return new pass_fre (m_ctxt); }
8830 void set_pass_param (unsigned int n, bool param) final override
8832 gcc_assert (n == 0);
8833 may_iterate = param;
8835 bool gate (function *) final override
8837 return flag_tree_fre != 0 && (may_iterate || optimize > 1);
8839 unsigned int execute (function *) final override;
8841 private:
8842 bool may_iterate;
8843 }; // class pass_fre
8845 unsigned int
8846 pass_fre::execute (function *fun)
8848 unsigned todo = 0;
8850 /* At -O[1g] use the cheap non-iterating mode. */
8851 bool iterate_p = may_iterate && (optimize > 1);
8852 calculate_dominance_info (CDI_DOMINATORS);
8853 if (iterate_p)
8854 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
8856 todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
8857 free_rpo_vn ();
8859 if (iterate_p)
8860 loop_optimizer_finalize ();
8862 if (scev_initialized_p ())
8863 scev_reset_htab ();
8865 /* For late FRE after IVOPTs and unrolling, see if we can
8866 remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8867 if (!may_iterate)
8868 todo |= TODO_update_address_taken;
8870 return todo;
8873 } // anon namespace
8875 gimple_opt_pass *
8876 make_pass_fre (gcc::context *ctxt)
8878 return new pass_fre (ctxt);
8881 #undef BB_EXECUTABLE