Daily bump.
[official-gcc.git] / gcc / tree-ssa-sccvn.cc
blob2587eb1c505279ef49514ec99ffe030ea32554c8
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, 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 result = hstate.end ();
752 /* ??? We would ICE later if we hash instead of adding that in. */
753 if (vr1->vuse)
754 result += SSA_NAME_VERSION (vr1->vuse);
756 return result;
759 /* Return true if reference operations VR1 and VR2 are equivalent. This
760 means they have the same set of operands and vuses. */
762 bool
763 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
765 unsigned i, j;
767 /* Early out if this is not a hash collision. */
768 if (vr1->hashcode != vr2->hashcode)
769 return false;
771 /* The VOP needs to be the same. */
772 if (vr1->vuse != vr2->vuse)
773 return false;
775 /* If the operands are the same we are done. */
776 if (vr1->operands == vr2->operands)
777 return true;
779 if (!vr1->type || !vr2->type)
781 if (vr1->type != vr2->type)
782 return false;
784 else if (vr1->type == vr2->type)
786 else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
787 || (COMPLETE_TYPE_P (vr1->type)
788 && !expressions_equal_p (TYPE_SIZE (vr1->type),
789 TYPE_SIZE (vr2->type))))
790 return false;
791 else if (vr1->operands[0].opcode == CALL_EXPR
792 && !types_compatible_p (vr1->type, vr2->type))
793 return false;
794 else if (INTEGRAL_TYPE_P (vr1->type)
795 && INTEGRAL_TYPE_P (vr2->type))
797 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
798 return false;
800 else if (INTEGRAL_TYPE_P (vr1->type)
801 && (TYPE_PRECISION (vr1->type)
802 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
803 return false;
804 else if (INTEGRAL_TYPE_P (vr2->type)
805 && (TYPE_PRECISION (vr2->type)
806 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
807 return false;
808 else if (VECTOR_BOOLEAN_TYPE_P (vr1->type)
809 && VECTOR_BOOLEAN_TYPE_P (vr2->type))
811 /* Vector boolean types can have padding, verify we are dealing with
812 the same number of elements, aka the precision of the types.
813 For example, In most architecture the precision_size of vbool*_t
814 types are caculated like below:
815 precision_size = type_size * 8
817 Unfortunately, the RISC-V will adjust the precision_size for the
818 vbool*_t in order to align the ISA as below:
819 type_size = [1, 1, 1, 1, 2, 4, 8]
820 precision_size = [1, 2, 4, 8, 16, 32, 64]
822 Then the precision_size of RISC-V vbool*_t will not be the multiple
823 of the type_size. We take care of this case consolidated here. */
824 if (maybe_ne (TYPE_VECTOR_SUBPARTS (vr1->type),
825 TYPE_VECTOR_SUBPARTS (vr2->type)))
826 return false;
829 i = 0;
830 j = 0;
833 poly_int64 off1 = 0, off2 = 0;
834 vn_reference_op_t vro1, vro2;
835 vn_reference_op_s tem1, tem2;
836 bool deref1 = false, deref2 = false;
837 bool reverse1 = false, reverse2 = false;
838 for (; vr1->operands.iterate (i, &vro1); i++)
840 if (vro1->opcode == MEM_REF)
841 deref1 = true;
842 /* Do not look through a storage order barrier. */
843 else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
844 return false;
845 reverse1 |= vro1->reverse;
846 if (known_eq (vro1->off, -1))
847 break;
848 off1 += vro1->off;
850 for (; vr2->operands.iterate (j, &vro2); j++)
852 if (vro2->opcode == MEM_REF)
853 deref2 = true;
854 /* Do not look through a storage order barrier. */
855 else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
856 return false;
857 reverse2 |= vro2->reverse;
858 if (known_eq (vro2->off, -1))
859 break;
860 off2 += vro2->off;
862 if (maybe_ne (off1, off2) || reverse1 != reverse2)
863 return false;
864 if (deref1 && vro1->opcode == ADDR_EXPR)
866 memset (&tem1, 0, sizeof (tem1));
867 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
868 tem1.type = TREE_TYPE (tem1.op0);
869 tem1.opcode = TREE_CODE (tem1.op0);
870 vro1 = &tem1;
871 deref1 = false;
873 if (deref2 && vro2->opcode == ADDR_EXPR)
875 memset (&tem2, 0, sizeof (tem2));
876 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
877 tem2.type = TREE_TYPE (tem2.op0);
878 tem2.opcode = TREE_CODE (tem2.op0);
879 vro2 = &tem2;
880 deref2 = false;
882 if (deref1 != deref2)
883 return false;
884 if (!vn_reference_op_eq (vro1, vro2))
885 return false;
886 ++j;
887 ++i;
889 while (vr1->operands.length () != i
890 || vr2->operands.length () != j);
892 return true;
895 /* Copy the operations present in load/store REF into RESULT, a vector of
896 vn_reference_op_s's. */
898 static void
899 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
901 /* For non-calls, store the information that makes up the address. */
902 tree orig = ref;
903 unsigned start = result->length ();
904 bool seen_variable_array_ref = false;
905 while (ref)
907 vn_reference_op_s temp;
909 memset (&temp, 0, sizeof (temp));
910 temp.type = TREE_TYPE (ref);
911 temp.opcode = TREE_CODE (ref);
912 temp.off = -1;
914 switch (temp.opcode)
916 case MODIFY_EXPR:
917 temp.op0 = TREE_OPERAND (ref, 1);
918 break;
919 case WITH_SIZE_EXPR:
920 temp.op0 = TREE_OPERAND (ref, 1);
921 temp.off = 0;
922 break;
923 case MEM_REF:
924 /* The base address gets its own vn_reference_op_s structure. */
925 temp.op0 = TREE_OPERAND (ref, 1);
926 if (!mem_ref_offset (ref).to_shwi (&temp.off))
927 temp.off = -1;
928 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
929 temp.base = MR_DEPENDENCE_BASE (ref);
930 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
931 break;
932 case TARGET_MEM_REF:
933 /* The base address gets its own vn_reference_op_s structure. */
934 temp.op0 = TMR_INDEX (ref);
935 temp.op1 = TMR_STEP (ref);
936 temp.op2 = TMR_OFFSET (ref);
937 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
938 temp.base = MR_DEPENDENCE_BASE (ref);
939 result->safe_push (temp);
940 memset (&temp, 0, sizeof (temp));
941 temp.type = NULL_TREE;
942 temp.opcode = ERROR_MARK;
943 temp.op0 = TMR_INDEX2 (ref);
944 temp.off = -1;
945 break;
946 case BIT_FIELD_REF:
947 /* Record bits, position and storage order. */
948 temp.op0 = TREE_OPERAND (ref, 1);
949 temp.op1 = TREE_OPERAND (ref, 2);
950 if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
951 temp.off = -1;
952 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
953 break;
954 case COMPONENT_REF:
955 /* The field decl is enough to unambiguously specify the field,
956 so use its type here. */
957 temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
958 temp.op0 = TREE_OPERAND (ref, 1);
959 temp.op1 = TREE_OPERAND (ref, 2);
960 temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
961 && TYPE_REVERSE_STORAGE_ORDER
962 (TREE_TYPE (TREE_OPERAND (ref, 0))));
964 tree this_offset = component_ref_field_offset (ref);
965 if (this_offset
966 && poly_int_tree_p (this_offset))
968 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
969 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
971 poly_offset_int off
972 = (wi::to_poly_offset (this_offset)
973 + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
974 /* Probibit value-numbering zero offset components
975 of addresses the same before the pass folding
976 __builtin_object_size had a chance to run. */
977 if (TREE_CODE (orig) != ADDR_EXPR
978 || maybe_ne (off, 0)
979 || (cfun->curr_properties & PROP_objsz))
980 off.to_shwi (&temp.off);
984 break;
985 case ARRAY_RANGE_REF:
986 case ARRAY_REF:
988 tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
989 /* Record index as operand. */
990 temp.op0 = TREE_OPERAND (ref, 1);
991 /* When the index is not constant we have to apply the same
992 logic as get_ref_base_and_extent which eventually uses
993 global ranges to refine the overall ref extent. Record
994 we've seen such a case, fixup below. */
995 if (TREE_CODE (temp.op0) == SSA_NAME)
996 seen_variable_array_ref = true;
997 /* Always record lower bounds and element size. */
998 temp.op1 = array_ref_low_bound (ref);
999 /* But record element size in units of the type alignment. */
1000 temp.op2 = TREE_OPERAND (ref, 3);
1001 temp.align = eltype->type_common.align;
1002 if (! temp.op2)
1003 temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
1004 size_int (TYPE_ALIGN_UNIT (eltype)));
1005 if (poly_int_tree_p (temp.op0)
1006 && poly_int_tree_p (temp.op1)
1007 && TREE_CODE (temp.op2) == INTEGER_CST)
1009 poly_offset_int off = ((wi::to_poly_offset (temp.op0)
1010 - wi::to_poly_offset (temp.op1))
1011 * wi::to_offset (temp.op2)
1012 * vn_ref_op_align_unit (&temp));
1013 off.to_shwi (&temp.off);
1015 temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
1016 && TYPE_REVERSE_STORAGE_ORDER
1017 (TREE_TYPE (TREE_OPERAND (ref, 0))));
1019 break;
1020 case VAR_DECL:
1021 if (DECL_HARD_REGISTER (ref))
1023 temp.op0 = ref;
1024 break;
1026 /* Fallthru. */
1027 case PARM_DECL:
1028 case CONST_DECL:
1029 case RESULT_DECL:
1030 /* Canonicalize decls to MEM[&decl] which is what we end up with
1031 when valueizing MEM[ptr] with ptr = &decl. */
1032 temp.opcode = MEM_REF;
1033 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
1034 temp.off = 0;
1035 result->safe_push (temp);
1036 temp.opcode = ADDR_EXPR;
1037 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
1038 temp.type = TREE_TYPE (temp.op0);
1039 temp.off = -1;
1040 break;
1041 case STRING_CST:
1042 case INTEGER_CST:
1043 case POLY_INT_CST:
1044 case COMPLEX_CST:
1045 case VECTOR_CST:
1046 case REAL_CST:
1047 case FIXED_CST:
1048 case CONSTRUCTOR:
1049 case SSA_NAME:
1050 temp.op0 = ref;
1051 break;
1052 case ADDR_EXPR:
1053 if (is_gimple_min_invariant (ref))
1055 temp.op0 = ref;
1056 break;
1058 break;
1059 /* These are only interesting for their operands, their
1060 existence, and their type. They will never be the last
1061 ref in the chain of references (IE they require an
1062 operand), so we don't have to put anything
1063 for op* as it will be handled by the iteration */
1064 case REALPART_EXPR:
1065 temp.off = 0;
1066 break;
1067 case VIEW_CONVERT_EXPR:
1068 temp.off = 0;
1069 temp.reverse = storage_order_barrier_p (ref);
1070 break;
1071 case IMAGPART_EXPR:
1072 /* This is only interesting for its constant offset. */
1073 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1074 break;
1075 default:
1076 gcc_unreachable ();
1078 result->safe_push (temp);
1080 if (REFERENCE_CLASS_P (ref)
1081 || TREE_CODE (ref) == MODIFY_EXPR
1082 || TREE_CODE (ref) == WITH_SIZE_EXPR
1083 || (TREE_CODE (ref) == ADDR_EXPR
1084 && !is_gimple_min_invariant (ref)))
1085 ref = TREE_OPERAND (ref, 0);
1086 else
1087 ref = NULL_TREE;
1089 poly_int64 offset, size, max_size;
1090 tree base;
1091 bool rev;
1092 if (seen_variable_array_ref
1093 && handled_component_p (orig)
1094 && (base = get_ref_base_and_extent (orig,
1095 &offset, &size, &max_size, &rev))
1096 && known_size_p (max_size)
1097 && known_eq (size, max_size))
1099 poly_int64 orig_offset = offset;
1100 poly_int64 tem;
1101 if (TREE_CODE (base) == MEM_REF
1102 && mem_ref_offset (base).to_shwi (&tem))
1103 offset += tem * BITS_PER_UNIT;
1104 HOST_WIDE_INT coffset = offset.to_constant ();
1105 /* When get_ref_base_and_extent computes an offset constrained to
1106 a constant position we have to fixup variable array indexes in
1107 the ref to avoid the situation where based on context we'd have
1108 to value-number the same vn_reference ops differently. Make
1109 the vn_reference ops differ by adjusting those indexes to
1110 appropriate constants. */
1111 poly_int64 off = 0;
1112 bool oob_index = false;
1113 for (unsigned i = result->length (); i > start; --i)
1115 auto &op = (*result)[i-1];
1116 if (flag_checking
1117 && op.opcode == ARRAY_REF
1118 && TREE_CODE (op.op0) == INTEGER_CST)
1120 /* The verifier below chokes on inconsistencies of handling
1121 out-of-bound accesses so disable it in that case. */
1122 tree atype = (*result)[i].type;
1123 if (TREE_CODE (atype) == ARRAY_TYPE)
1124 if (tree dom = TYPE_DOMAIN (atype))
1125 if ((TYPE_MIN_VALUE (dom)
1126 && TREE_CODE (TYPE_MIN_VALUE (dom)) == INTEGER_CST
1127 && (wi::to_widest (op.op0)
1128 < wi::to_widest (TYPE_MIN_VALUE (dom))))
1129 || (TYPE_MAX_VALUE (dom)
1130 && TREE_CODE (TYPE_MAX_VALUE (dom)) == INTEGER_CST
1131 && (wi::to_widest (op.op0)
1132 > wi::to_widest (TYPE_MAX_VALUE (dom)))))
1133 oob_index = true;
1135 if ((op.opcode == ARRAY_REF
1136 || op.opcode == ARRAY_RANGE_REF)
1137 && TREE_CODE (op.op0) == SSA_NAME)
1139 /* There's a single constant index that get's 'off' closer
1140 to 'offset'. */
1141 unsigned HOST_WIDE_INT elsz
1142 = tree_to_uhwi (op.op2) * vn_ref_op_align_unit (&op);
1143 unsigned HOST_WIDE_INT idx
1144 = (coffset - off.to_constant ()) / BITS_PER_UNIT / elsz;
1145 if (idx == 0)
1146 op.op0 = op.op1;
1147 else
1148 op.op0 = wide_int_to_tree (TREE_TYPE (op.op0),
1149 wi::to_poly_wide (op.op1) + idx);
1150 op.off = idx * elsz;
1151 off += op.off * BITS_PER_UNIT;
1153 else
1155 if (op.opcode == ERROR_MARK)
1156 /* two-ops codes have the offset in the first op. */
1158 else if (op.opcode == ADDR_EXPR
1159 || op.opcode == SSA_NAME
1160 || op.opcode == CONSTRUCTOR
1161 || TREE_CODE_CLASS (op.opcode) == tcc_declaration
1162 || TREE_CODE_CLASS (op.opcode) == tcc_constant)
1163 /* end-of ref. */
1164 gcc_assert (i == result->length ());
1165 else if (op.opcode == COMPONENT_REF)
1167 /* op.off is tracked in bytes, re-do it manually
1168 because of bitfields. */
1169 tree field = op.op0;
1170 /* We do not have a complete COMPONENT_REF tree here so we
1171 cannot use component_ref_field_offset. Do the interesting
1172 parts manually. */
1173 tree this_offset = DECL_FIELD_OFFSET (field);
1174 if (op.op1 || !poly_int_tree_p (this_offset))
1175 gcc_unreachable ();
1176 else
1178 poly_offset_int woffset
1179 = (wi::to_poly_offset (this_offset)
1180 << LOG2_BITS_PER_UNIT);
1181 woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1182 off += woffset.force_shwi ();
1185 else
1187 gcc_assert (known_ne (op.off, -1)
1188 /* The constant offset can be -1. */
1189 || op.opcode == MEM_REF
1190 /* Out-of-bound indices can compute to
1191 a known -1 offset. */
1192 || ((op.opcode == ARRAY_REF
1193 || op.opcode == ARRAY_RANGE_REF)
1194 && poly_int_tree_p (op.op0)
1195 && poly_int_tree_p (op.op1)
1196 && TREE_CODE (op.op2) == INTEGER_CST));
1197 off += op.off * BITS_PER_UNIT;
1201 if (flag_checking && !oob_index)
1203 ao_ref r;
1204 if (start != 0)
1206 else if (ao_ref_init_from_vn_reference (&r, 0, 0, TREE_TYPE (orig),
1207 *result))
1208 gcc_assert (known_eq (r.offset, orig_offset)
1209 && known_eq (r.size, size)
1210 && known_eq (r.max_size, max_size));
1211 else
1212 gcc_unreachable ();
1217 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1218 operands in *OPS, the reference alias set SET and the reference type TYPE.
1219 Return true if something useful was produced. */
1221 bool
1222 ao_ref_init_from_vn_reference (ao_ref *ref,
1223 alias_set_type set, alias_set_type base_set,
1224 tree type, const vec<vn_reference_op_s> &ops)
1226 unsigned i;
1227 tree base = NULL_TREE;
1228 tree *op0_p = &base;
1229 poly_offset_int offset = 0;
1230 poly_offset_int max_size;
1231 poly_offset_int size = -1;
1232 tree size_tree = NULL_TREE;
1234 /* We don't handle calls. */
1235 if (!type)
1236 return false;
1238 machine_mode mode = TYPE_MODE (type);
1239 if (mode == BLKmode)
1240 size_tree = TYPE_SIZE (type);
1241 else
1242 size = GET_MODE_BITSIZE (mode);
1243 if (size_tree != NULL_TREE
1244 && poly_int_tree_p (size_tree))
1245 size = wi::to_poly_offset (size_tree);
1247 /* Lower the final access size from the outermost expression. */
1248 const_vn_reference_op_t cst_op = &ops[0];
1249 /* Cast away constness for the sake of the const-unsafe
1250 FOR_EACH_VEC_ELT(). */
1251 vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1252 size_tree = NULL_TREE;
1253 if (op->opcode == COMPONENT_REF)
1254 size_tree = DECL_SIZE (op->op0);
1255 else if (op->opcode == BIT_FIELD_REF)
1256 size_tree = op->op0;
1257 if (size_tree != NULL_TREE
1258 && poly_int_tree_p (size_tree)
1259 && (!known_size_p (size)
1260 || known_lt (wi::to_poly_offset (size_tree), size)))
1261 size = wi::to_poly_offset (size_tree);
1263 /* Initially, maxsize is the same as the accessed element size.
1264 In the following it will only grow (or become -1). */
1265 max_size = size;
1267 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1268 and find the ultimate containing object. */
1269 FOR_EACH_VEC_ELT (ops, i, op)
1271 switch (op->opcode)
1273 /* These may be in the reference ops, but we cannot do anything
1274 sensible with them here. */
1275 case ADDR_EXPR:
1276 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1277 if (base != NULL_TREE
1278 && TREE_CODE (base) == MEM_REF
1279 && op->op0
1280 && DECL_P (TREE_OPERAND (op->op0, 0)))
1282 const_vn_reference_op_t pop = &ops[i-1];
1283 base = TREE_OPERAND (op->op0, 0);
1284 if (known_eq (pop->off, -1))
1286 max_size = -1;
1287 offset = 0;
1289 else
1290 offset += pop->off * BITS_PER_UNIT;
1291 op0_p = NULL;
1292 break;
1294 /* Fallthru. */
1295 case CALL_EXPR:
1296 return false;
1298 /* Record the base objects. */
1299 case MEM_REF:
1300 *op0_p = build2 (MEM_REF, op->type,
1301 NULL_TREE, op->op0);
1302 MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1303 MR_DEPENDENCE_BASE (*op0_p) = op->base;
1304 op0_p = &TREE_OPERAND (*op0_p, 0);
1305 break;
1307 case VAR_DECL:
1308 case PARM_DECL:
1309 case RESULT_DECL:
1310 case SSA_NAME:
1311 *op0_p = op->op0;
1312 op0_p = NULL;
1313 break;
1315 /* And now the usual component-reference style ops. */
1316 case BIT_FIELD_REF:
1317 offset += wi::to_poly_offset (op->op1);
1318 break;
1320 case COMPONENT_REF:
1322 tree field = op->op0;
1323 /* We do not have a complete COMPONENT_REF tree here so we
1324 cannot use component_ref_field_offset. Do the interesting
1325 parts manually. */
1326 tree this_offset = DECL_FIELD_OFFSET (field);
1328 if (op->op1 || !poly_int_tree_p (this_offset))
1329 max_size = -1;
1330 else
1332 poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1333 << LOG2_BITS_PER_UNIT);
1334 woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1335 offset += woffset;
1337 break;
1340 case ARRAY_RANGE_REF:
1341 case ARRAY_REF:
1342 /* Use the recorded constant offset. */
1343 if (maybe_eq (op->off, -1))
1344 max_size = -1;
1345 else
1346 offset += op->off << LOG2_BITS_PER_UNIT;
1347 break;
1349 case REALPART_EXPR:
1350 break;
1352 case IMAGPART_EXPR:
1353 offset += size;
1354 break;
1356 case VIEW_CONVERT_EXPR:
1357 break;
1359 case STRING_CST:
1360 case INTEGER_CST:
1361 case COMPLEX_CST:
1362 case VECTOR_CST:
1363 case REAL_CST:
1364 case CONSTRUCTOR:
1365 case CONST_DECL:
1366 return false;
1368 default:
1369 return false;
1373 if (base == NULL_TREE)
1374 return false;
1376 ref->ref = NULL_TREE;
1377 ref->base = base;
1378 ref->ref_alias_set = set;
1379 ref->base_alias_set = base_set;
1380 /* We discount volatiles from value-numbering elsewhere. */
1381 ref->volatile_p = false;
1383 if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1385 ref->offset = 0;
1386 ref->size = -1;
1387 ref->max_size = -1;
1388 return true;
1391 if (!offset.to_shwi (&ref->offset))
1393 ref->offset = 0;
1394 ref->max_size = -1;
1395 return true;
1398 if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1399 ref->max_size = -1;
1401 return true;
1404 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1405 vn_reference_op_s's. */
1407 static void
1408 copy_reference_ops_from_call (gcall *call,
1409 vec<vn_reference_op_s> *result)
1411 vn_reference_op_s temp;
1412 unsigned i;
1413 tree lhs = gimple_call_lhs (call);
1414 int lr;
1416 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1417 different. By adding the lhs here in the vector, we ensure that the
1418 hashcode is different, guaranteeing a different value number. */
1419 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1421 memset (&temp, 0, sizeof (temp));
1422 temp.opcode = MODIFY_EXPR;
1423 temp.type = TREE_TYPE (lhs);
1424 temp.op0 = lhs;
1425 temp.off = -1;
1426 result->safe_push (temp);
1429 /* Copy the type, opcode, function, static chain and EH region, if any. */
1430 memset (&temp, 0, sizeof (temp));
1431 temp.type = gimple_call_fntype (call);
1432 temp.opcode = CALL_EXPR;
1433 temp.op0 = gimple_call_fn (call);
1434 if (gimple_call_internal_p (call))
1435 temp.clique = gimple_call_internal_fn (call);
1436 temp.op1 = gimple_call_chain (call);
1437 if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1438 temp.op2 = size_int (lr);
1439 temp.off = -1;
1440 result->safe_push (temp);
1442 /* Copy the call arguments. As they can be references as well,
1443 just chain them together. */
1444 for (i = 0; i < gimple_call_num_args (call); ++i)
1446 tree callarg = gimple_call_arg (call, i);
1447 copy_reference_ops_from_ref (callarg, result);
1451 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1452 *I_P to point to the last element of the replacement. */
1453 static bool
1454 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1455 unsigned int *i_p)
1457 unsigned int i = *i_p;
1458 vn_reference_op_t op = &(*ops)[i];
1459 vn_reference_op_t mem_op = &(*ops)[i - 1];
1460 tree addr_base;
1461 poly_int64 addr_offset = 0;
1463 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1464 from .foo.bar to the preceding MEM_REF offset and replace the
1465 address with &OBJ. */
1466 addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1467 &addr_offset, vn_valueize);
1468 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1469 if (addr_base != TREE_OPERAND (op->op0, 0))
1471 poly_offset_int off
1472 = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1473 SIGNED)
1474 + addr_offset);
1475 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1476 op->op0 = build_fold_addr_expr (addr_base);
1477 if (tree_fits_shwi_p (mem_op->op0))
1478 mem_op->off = tree_to_shwi (mem_op->op0);
1479 else
1480 mem_op->off = -1;
1481 return true;
1483 return false;
1486 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1487 *I_P to point to the last element of the replacement. */
1488 static bool
1489 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1490 unsigned int *i_p)
1492 bool changed = false;
1493 vn_reference_op_t op;
1497 unsigned int i = *i_p;
1498 op = &(*ops)[i];
1499 vn_reference_op_t mem_op = &(*ops)[i - 1];
1500 gimple *def_stmt;
1501 enum tree_code code;
1502 poly_offset_int off;
1504 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1505 if (!is_gimple_assign (def_stmt))
1506 return changed;
1508 code = gimple_assign_rhs_code (def_stmt);
1509 if (code != ADDR_EXPR
1510 && code != POINTER_PLUS_EXPR)
1511 return changed;
1513 off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1515 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1516 from .foo.bar to the preceding MEM_REF offset and replace the
1517 address with &OBJ. */
1518 if (code == ADDR_EXPR)
1520 tree addr, addr_base;
1521 poly_int64 addr_offset;
1523 addr = gimple_assign_rhs1 (def_stmt);
1524 addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1525 &addr_offset,
1526 vn_valueize);
1527 /* If that didn't work because the address isn't invariant propagate
1528 the reference tree from the address operation in case the current
1529 dereference isn't offsetted. */
1530 if (!addr_base
1531 && *i_p == ops->length () - 1
1532 && known_eq (off, 0)
1533 /* This makes us disable this transform for PRE where the
1534 reference ops might be also used for code insertion which
1535 is invalid. */
1536 && default_vn_walk_kind == VN_WALKREWRITE)
1538 auto_vec<vn_reference_op_s, 32> tem;
1539 copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1540 /* Make sure to preserve TBAA info. The only objects not
1541 wrapped in MEM_REFs that can have their address taken are
1542 STRING_CSTs. */
1543 if (tem.length () >= 2
1544 && tem[tem.length () - 2].opcode == MEM_REF)
1546 vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1547 new_mem_op->op0
1548 = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1549 wi::to_poly_wide (new_mem_op->op0));
1551 else
1552 gcc_assert (tem.last ().opcode == STRING_CST);
1553 ops->pop ();
1554 ops->pop ();
1555 ops->safe_splice (tem);
1556 --*i_p;
1557 return true;
1559 if (!addr_base
1560 || TREE_CODE (addr_base) != MEM_REF
1561 || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1562 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1563 0))))
1564 return changed;
1566 off += addr_offset;
1567 off += mem_ref_offset (addr_base);
1568 op->op0 = TREE_OPERAND (addr_base, 0);
1570 else
1572 tree ptr, ptroff;
1573 ptr = gimple_assign_rhs1 (def_stmt);
1574 ptroff = gimple_assign_rhs2 (def_stmt);
1575 if (TREE_CODE (ptr) != SSA_NAME
1576 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1577 /* Make sure to not endlessly recurse.
1578 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1579 happen when we value-number a PHI to its backedge value. */
1580 || SSA_VAL (ptr) == op->op0
1581 || !poly_int_tree_p (ptroff))
1582 return changed;
1584 off += wi::to_poly_offset (ptroff);
1585 op->op0 = ptr;
1588 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1589 if (tree_fits_shwi_p (mem_op->op0))
1590 mem_op->off = tree_to_shwi (mem_op->op0);
1591 else
1592 mem_op->off = -1;
1593 /* ??? Can end up with endless recursion here!?
1594 gcc.c-torture/execute/strcmp-1.c */
1595 if (TREE_CODE (op->op0) == SSA_NAME)
1596 op->op0 = SSA_VAL (op->op0);
1597 if (TREE_CODE (op->op0) != SSA_NAME)
1598 op->opcode = TREE_CODE (op->op0);
1600 changed = true;
1602 /* Tail-recurse. */
1603 while (TREE_CODE (op->op0) == SSA_NAME);
1605 /* Fold a remaining *&. */
1606 if (TREE_CODE (op->op0) == ADDR_EXPR)
1607 vn_reference_fold_indirect (ops, i_p);
1609 return changed;
1612 /* Optimize the reference REF to a constant if possible or return
1613 NULL_TREE if not. */
1615 tree
1616 fully_constant_vn_reference_p (vn_reference_t ref)
1618 vec<vn_reference_op_s> operands = ref->operands;
1619 vn_reference_op_t op;
1621 /* Try to simplify the translated expression if it is
1622 a call to a builtin function with at most two arguments. */
1623 op = &operands[0];
1624 if (op->opcode == CALL_EXPR
1625 && (!op->op0
1626 || (TREE_CODE (op->op0) == ADDR_EXPR
1627 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1628 && fndecl_built_in_p (TREE_OPERAND (op->op0, 0),
1629 BUILT_IN_NORMAL)))
1630 && operands.length () >= 2
1631 && operands.length () <= 3)
1633 vn_reference_op_t arg0, arg1 = NULL;
1634 bool anyconst = false;
1635 arg0 = &operands[1];
1636 if (operands.length () > 2)
1637 arg1 = &operands[2];
1638 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1639 || (arg0->opcode == ADDR_EXPR
1640 && is_gimple_min_invariant (arg0->op0)))
1641 anyconst = true;
1642 if (arg1
1643 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1644 || (arg1->opcode == ADDR_EXPR
1645 && is_gimple_min_invariant (arg1->op0))))
1646 anyconst = true;
1647 if (anyconst)
1649 combined_fn fn;
1650 if (op->op0)
1651 fn = as_combined_fn (DECL_FUNCTION_CODE
1652 (TREE_OPERAND (op->op0, 0)));
1653 else
1654 fn = as_combined_fn ((internal_fn) op->clique);
1655 tree folded;
1656 if (arg1)
1657 folded = fold_const_call (fn, ref->type, arg0->op0, arg1->op0);
1658 else
1659 folded = fold_const_call (fn, ref->type, arg0->op0);
1660 if (folded
1661 && is_gimple_min_invariant (folded))
1662 return folded;
1666 /* Simplify reads from constants or constant initializers. */
1667 else if (BITS_PER_UNIT == 8
1668 && ref->type
1669 && COMPLETE_TYPE_P (ref->type)
1670 && is_gimple_reg_type (ref->type))
1672 poly_int64 off = 0;
1673 HOST_WIDE_INT size;
1674 if (INTEGRAL_TYPE_P (ref->type))
1675 size = TYPE_PRECISION (ref->type);
1676 else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1677 size = tree_to_shwi (TYPE_SIZE (ref->type));
1678 else
1679 return NULL_TREE;
1680 if (size % BITS_PER_UNIT != 0
1681 || size > MAX_BITSIZE_MODE_ANY_MODE)
1682 return NULL_TREE;
1683 size /= BITS_PER_UNIT;
1684 unsigned i;
1685 for (i = 0; i < operands.length (); ++i)
1687 if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1689 ++i;
1690 break;
1692 if (known_eq (operands[i].off, -1))
1693 return NULL_TREE;
1694 off += operands[i].off;
1695 if (operands[i].opcode == MEM_REF)
1697 ++i;
1698 break;
1701 vn_reference_op_t base = &operands[--i];
1702 tree ctor = error_mark_node;
1703 tree decl = NULL_TREE;
1704 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1705 ctor = base->op0;
1706 else if (base->opcode == MEM_REF
1707 && base[1].opcode == ADDR_EXPR
1708 && (VAR_P (TREE_OPERAND (base[1].op0, 0))
1709 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1710 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1712 decl = TREE_OPERAND (base[1].op0, 0);
1713 if (TREE_CODE (decl) == STRING_CST)
1714 ctor = decl;
1715 else
1716 ctor = ctor_for_folding (decl);
1718 if (ctor == NULL_TREE)
1719 return build_zero_cst (ref->type);
1720 else if (ctor != error_mark_node)
1722 HOST_WIDE_INT const_off;
1723 if (decl)
1725 tree res = fold_ctor_reference (ref->type, ctor,
1726 off * BITS_PER_UNIT,
1727 size * BITS_PER_UNIT, decl);
1728 if (res)
1730 STRIP_USELESS_TYPE_CONVERSION (res);
1731 if (is_gimple_min_invariant (res))
1732 return res;
1735 else if (off.is_constant (&const_off))
1737 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1738 int len = native_encode_expr (ctor, buf, size, const_off);
1739 if (len > 0)
1740 return native_interpret_expr (ref->type, buf, len);
1745 return NULL_TREE;
1748 /* Return true if OPS contain a storage order barrier. */
1750 static bool
1751 contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1753 vn_reference_op_t op;
1754 unsigned i;
1756 FOR_EACH_VEC_ELT (ops, i, op)
1757 if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1758 return true;
1760 return false;
1763 /* Return true if OPS represent an access with reverse storage order. */
1765 static bool
1766 reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
1768 unsigned i = 0;
1769 if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1770 ++i;
1771 switch (ops[i].opcode)
1773 case ARRAY_REF:
1774 case COMPONENT_REF:
1775 case BIT_FIELD_REF:
1776 case MEM_REF:
1777 return ops[i].reverse;
1778 default:
1779 return false;
1783 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1784 structures into their value numbers. This is done in-place, and
1785 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1786 whether any operands were valueized. */
1788 static void
1789 valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1790 bool with_avail = false)
1792 *valueized_anything = false;
1794 for (unsigned i = 0; i < orig->length (); ++i)
1796 re_valueize:
1797 vn_reference_op_t vro = &(*orig)[i];
1798 if (vro->opcode == SSA_NAME
1799 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1801 tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1802 if (tem != vro->op0)
1804 *valueized_anything = true;
1805 vro->op0 = tem;
1807 /* If it transforms from an SSA_NAME to a constant, update
1808 the opcode. */
1809 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1810 vro->opcode = TREE_CODE (vro->op0);
1812 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1814 tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1815 if (tem != vro->op1)
1817 *valueized_anything = true;
1818 vro->op1 = tem;
1821 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1823 tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1824 if (tem != vro->op2)
1826 *valueized_anything = true;
1827 vro->op2 = tem;
1830 /* If it transforms from an SSA_NAME to an address, fold with
1831 a preceding indirect reference. */
1832 if (i > 0
1833 && vro->op0
1834 && TREE_CODE (vro->op0) == ADDR_EXPR
1835 && (*orig)[i - 1].opcode == MEM_REF)
1837 if (vn_reference_fold_indirect (orig, &i))
1838 *valueized_anything = true;
1840 else if (i > 0
1841 && vro->opcode == SSA_NAME
1842 && (*orig)[i - 1].opcode == MEM_REF)
1844 if (vn_reference_maybe_forwprop_address (orig, &i))
1846 *valueized_anything = true;
1847 /* Re-valueize the current operand. */
1848 goto re_valueize;
1851 /* If it transforms a non-constant ARRAY_REF into a constant
1852 one, adjust the constant offset. */
1853 else if ((vro->opcode == ARRAY_REF
1854 || vro->opcode == ARRAY_RANGE_REF)
1855 && known_eq (vro->off, -1)
1856 && poly_int_tree_p (vro->op0)
1857 && poly_int_tree_p (vro->op1)
1858 && TREE_CODE (vro->op2) == INTEGER_CST)
1860 poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1861 - wi::to_poly_offset (vro->op1))
1862 * wi::to_offset (vro->op2)
1863 * vn_ref_op_align_unit (vro));
1864 off.to_shwi (&vro->off);
1869 static void
1870 valueize_refs (vec<vn_reference_op_s> *orig)
1872 bool tem;
1873 valueize_refs_1 (orig, &tem);
1876 static vec<vn_reference_op_s> shared_lookup_references;
1878 /* Create a vector of vn_reference_op_s structures from REF, a
1879 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1880 this function. *VALUEIZED_ANYTHING will specify whether any
1881 operands were valueized. */
1883 static vec<vn_reference_op_s>
1884 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1886 if (!ref)
1887 return vNULL;
1888 shared_lookup_references.truncate (0);
1889 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1890 valueize_refs_1 (&shared_lookup_references, valueized_anything);
1891 return shared_lookup_references;
1894 /* Create a vector of vn_reference_op_s structures from CALL, a
1895 call statement. The vector is shared among all callers of
1896 this function. */
1898 static vec<vn_reference_op_s>
1899 valueize_shared_reference_ops_from_call (gcall *call)
1901 if (!call)
1902 return vNULL;
1903 shared_lookup_references.truncate (0);
1904 copy_reference_ops_from_call (call, &shared_lookup_references);
1905 valueize_refs (&shared_lookup_references);
1906 return shared_lookup_references;
1909 /* Lookup a SCCVN reference operation VR in the current hash table.
1910 Returns the resulting value number if it exists in the hash table,
1911 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1912 vn_reference_t stored in the hashtable if something is found. */
1914 static tree
1915 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1917 vn_reference_s **slot;
1918 hashval_t hash;
1920 hash = vr->hashcode;
1921 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1922 if (slot)
1924 if (vnresult)
1925 *vnresult = (vn_reference_t)*slot;
1926 return ((vn_reference_t)*slot)->result;
1929 return NULL_TREE;
1933 /* Partial definition tracking support. */
1935 struct pd_range
1937 HOST_WIDE_INT offset;
1938 HOST_WIDE_INT size;
1941 struct pd_data
1943 tree rhs;
1944 HOST_WIDE_INT rhs_off;
1945 HOST_WIDE_INT offset;
1946 HOST_WIDE_INT size;
1949 /* Context for alias walking. */
1951 struct vn_walk_cb_data
1953 vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1954 vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_,
1955 bool redundant_store_removal_p_)
1956 : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1957 mask (mask_), masked_result (NULL_TREE), same_val (NULL_TREE),
1958 vn_walk_kind (vn_walk_kind_),
1959 tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
1960 saved_operands (vNULL), first_set (-2), first_base_set (-2),
1961 known_ranges (NULL)
1963 if (!last_vuse_ptr)
1964 last_vuse_ptr = &last_vuse;
1965 ao_ref_init (&orig_ref, orig_ref_);
1966 if (mask)
1968 wide_int w = wi::to_wide (mask);
1969 unsigned int pos = 0, prec = w.get_precision ();
1970 pd_data pd;
1971 pd.rhs = build_constructor (NULL_TREE, NULL);
1972 pd.rhs_off = 0;
1973 /* When bitwise and with a constant is done on a memory load,
1974 we don't really need all the bits to be defined or defined
1975 to constants, we don't really care what is in the position
1976 corresponding to 0 bits in the mask.
1977 So, push the ranges of those 0 bits in the mask as artificial
1978 zero stores and let the partial def handling code do the
1979 rest. */
1980 while (pos < prec)
1982 int tz = wi::ctz (w);
1983 if (pos + tz > prec)
1984 tz = prec - pos;
1985 if (tz)
1987 if (BYTES_BIG_ENDIAN)
1988 pd.offset = prec - pos - tz;
1989 else
1990 pd.offset = pos;
1991 pd.size = tz;
1992 void *r = push_partial_def (pd, 0, 0, 0, prec);
1993 gcc_assert (r == NULL_TREE);
1995 pos += tz;
1996 if (pos == prec)
1997 break;
1998 w = wi::lrshift (w, tz);
1999 tz = wi::ctz (wi::bit_not (w));
2000 if (pos + tz > prec)
2001 tz = prec - pos;
2002 pos += tz;
2003 w = wi::lrshift (w, tz);
2007 ~vn_walk_cb_data ();
2008 void *finish (alias_set_type, alias_set_type, tree);
2009 void *push_partial_def (pd_data pd,
2010 alias_set_type, alias_set_type, HOST_WIDE_INT,
2011 HOST_WIDE_INT);
2013 vn_reference_t vr;
2014 ao_ref orig_ref;
2015 tree *last_vuse_ptr;
2016 tree last_vuse;
2017 tree mask;
2018 tree masked_result;
2019 tree same_val;
2020 vn_lookup_kind vn_walk_kind;
2021 bool tbaa_p;
2022 bool redundant_store_removal_p;
2023 vec<vn_reference_op_s> saved_operands;
2025 /* The VDEFs of partial defs we come along. */
2026 auto_vec<pd_data, 2> partial_defs;
2027 /* The first defs range to avoid splay tree setup in most cases. */
2028 pd_range first_range;
2029 alias_set_type first_set;
2030 alias_set_type first_base_set;
2031 splay_tree known_ranges;
2032 obstack ranges_obstack;
2033 static constexpr HOST_WIDE_INT bufsize = 64;
2036 vn_walk_cb_data::~vn_walk_cb_data ()
2038 if (known_ranges)
2040 splay_tree_delete (known_ranges);
2041 obstack_free (&ranges_obstack, NULL);
2043 saved_operands.release ();
2046 void *
2047 vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
2049 if (first_set != -2)
2051 set = first_set;
2052 base_set = first_base_set;
2054 if (mask)
2056 masked_result = val;
2057 return (void *) -1;
2059 if (same_val && !operand_equal_p (val, same_val))
2060 return (void *) -1;
2061 vec<vn_reference_op_s> &operands
2062 = saved_operands.exists () ? saved_operands : vr->operands;
2063 return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
2064 vr->type, operands, val);
2067 /* pd_range splay-tree helpers. */
2069 static int
2070 pd_range_compare (splay_tree_key offset1p, splay_tree_key offset2p)
2072 HOST_WIDE_INT offset1 = *(HOST_WIDE_INT *)offset1p;
2073 HOST_WIDE_INT offset2 = *(HOST_WIDE_INT *)offset2p;
2074 if (offset1 < offset2)
2075 return -1;
2076 else if (offset1 > offset2)
2077 return 1;
2078 return 0;
2081 static void *
2082 pd_tree_alloc (int size, void *data_)
2084 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2085 return obstack_alloc (&data->ranges_obstack, size);
2088 static void
2089 pd_tree_dealloc (void *, void *)
2093 /* Push PD to the vector of partial definitions returning a
2094 value when we are ready to combine things with VUSE, SET and MAXSIZEI,
2095 NULL when we want to continue looking for partial defs or -1
2096 on failure. */
2098 void *
2099 vn_walk_cb_data::push_partial_def (pd_data pd,
2100 alias_set_type set, alias_set_type base_set,
2101 HOST_WIDE_INT offseti,
2102 HOST_WIDE_INT maxsizei)
2104 /* We're using a fixed buffer for encoding so fail early if the object
2105 we want to interpret is bigger. */
2106 if (maxsizei > bufsize * BITS_PER_UNIT
2107 || CHAR_BIT != 8
2108 || BITS_PER_UNIT != 8
2109 /* Not prepared to handle PDP endian. */
2110 || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2111 return (void *)-1;
2113 /* Turn too large constant stores into non-constant stores. */
2114 if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
2115 pd.rhs = error_mark_node;
2117 /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
2118 most a partial byte before and/or after the region. */
2119 if (!CONSTANT_CLASS_P (pd.rhs))
2121 if (pd.offset < offseti)
2123 HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
2124 gcc_assert (pd.size > o);
2125 pd.size -= o;
2126 pd.offset += o;
2128 if (pd.size > maxsizei)
2129 pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
2132 pd.offset -= offseti;
2134 bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
2135 || CONSTANT_CLASS_P (pd.rhs));
2136 pd_range *r;
2137 if (partial_defs.is_empty ())
2139 /* If we get a clobber upfront, fail. */
2140 if (TREE_CLOBBER_P (pd.rhs))
2141 return (void *)-1;
2142 if (!pd_constant_p)
2143 return (void *)-1;
2144 partial_defs.safe_push (pd);
2145 first_range.offset = pd.offset;
2146 first_range.size = pd.size;
2147 first_set = set;
2148 first_base_set = base_set;
2149 last_vuse_ptr = NULL;
2150 r = &first_range;
2151 /* Go check if the first partial definition was a full one in case
2152 the caller didn't optimize for this. */
2154 else
2156 if (!known_ranges)
2158 /* ??? Optimize the case where the 2nd partial def completes
2159 things. */
2160 gcc_obstack_init (&ranges_obstack);
2161 known_ranges = splay_tree_new_with_allocator (pd_range_compare, 0, 0,
2162 pd_tree_alloc,
2163 pd_tree_dealloc, this);
2164 splay_tree_insert (known_ranges,
2165 (splay_tree_key)&first_range.offset,
2166 (splay_tree_value)&first_range);
2169 pd_range newr = { pd.offset, pd.size };
2170 splay_tree_node n;
2171 /* Lookup the predecessor of offset + 1 and see if we need to merge. */
2172 HOST_WIDE_INT loffset = newr.offset + 1;
2173 if ((n = splay_tree_predecessor (known_ranges, (splay_tree_key)&loffset))
2174 && ((r = (pd_range *)n->value), true)
2175 && ranges_known_overlap_p (r->offset, r->size + 1,
2176 newr.offset, newr.size))
2178 /* Ignore partial defs already covered. Here we also drop shadowed
2179 clobbers arriving here at the floor. */
2180 if (known_subrange_p (newr.offset, newr.size, r->offset, r->size))
2181 return NULL;
2182 r->size
2183 = MAX (r->offset + r->size, newr.offset + newr.size) - r->offset;
2185 else
2187 /* newr.offset wasn't covered yet, insert the range. */
2188 r = XOBNEW (&ranges_obstack, pd_range);
2189 *r = newr;
2190 splay_tree_insert (known_ranges, (splay_tree_key)&r->offset,
2191 (splay_tree_value)r);
2193 /* Merge r which now contains newr and is a member of the splay tree with
2194 adjacent overlapping ranges. */
2195 pd_range *rafter;
2196 while ((n = splay_tree_successor (known_ranges,
2197 (splay_tree_key)&r->offset))
2198 && ((rafter = (pd_range *)n->value), true)
2199 && ranges_known_overlap_p (r->offset, r->size + 1,
2200 rafter->offset, rafter->size))
2202 r->size = MAX (r->offset + r->size,
2203 rafter->offset + rafter->size) - r->offset;
2204 splay_tree_remove (known_ranges, (splay_tree_key)&rafter->offset);
2206 /* If we get a clobber, fail. */
2207 if (TREE_CLOBBER_P (pd.rhs))
2208 return (void *)-1;
2209 /* Non-constants are OK as long as they are shadowed by a constant. */
2210 if (!pd_constant_p)
2211 return (void *)-1;
2212 partial_defs.safe_push (pd);
2215 /* Now we have merged newr into the range tree. When we have covered
2216 [offseti, sizei] then the tree will contain exactly one node which has
2217 the desired properties and it will be 'r'. */
2218 if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2219 /* Continue looking for partial defs. */
2220 return NULL;
2222 /* Now simply native encode all partial defs in reverse order. */
2223 unsigned ndefs = partial_defs.length ();
2224 /* We support up to 512-bit values (for V8DFmode). */
2225 unsigned char buffer[bufsize + 1];
2226 unsigned char this_buffer[bufsize + 1];
2227 int len;
2229 memset (buffer, 0, bufsize + 1);
2230 unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2231 while (!partial_defs.is_empty ())
2233 pd_data pd = partial_defs.pop ();
2234 unsigned int amnt;
2235 if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2237 /* Empty CONSTRUCTOR. */
2238 if (pd.size >= needed_len * BITS_PER_UNIT)
2239 len = needed_len;
2240 else
2241 len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2242 memset (this_buffer, 0, len);
2244 else if (pd.rhs_off >= 0)
2246 len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2247 (MAX (0, -pd.offset)
2248 + pd.rhs_off) / BITS_PER_UNIT);
2249 if (len <= 0
2250 || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2251 - MAX (0, -pd.offset) / BITS_PER_UNIT))
2253 if (dump_file && (dump_flags & TDF_DETAILS))
2254 fprintf (dump_file, "Failed to encode %u "
2255 "partial definitions\n", ndefs);
2256 return (void *)-1;
2259 else /* negative pd.rhs_off indicates we want to chop off first bits */
2261 if (-pd.rhs_off >= bufsize)
2262 return (void *)-1;
2263 len = native_encode_expr (pd.rhs,
2264 this_buffer + -pd.rhs_off / BITS_PER_UNIT,
2265 bufsize - -pd.rhs_off / BITS_PER_UNIT,
2266 MAX (0, -pd.offset) / BITS_PER_UNIT);
2267 if (len <= 0
2268 || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2269 - MAX (0, -pd.offset) / BITS_PER_UNIT))
2271 if (dump_file && (dump_flags & TDF_DETAILS))
2272 fprintf (dump_file, "Failed to encode %u "
2273 "partial definitions\n", ndefs);
2274 return (void *)-1;
2278 unsigned char *p = buffer;
2279 HOST_WIDE_INT size = pd.size;
2280 if (pd.offset < 0)
2281 size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2282 this_buffer[len] = 0;
2283 if (BYTES_BIG_ENDIAN)
2285 /* LSB of this_buffer[len - 1] byte should be at
2286 pd.offset + pd.size - 1 bits in buffer. */
2287 amnt = ((unsigned HOST_WIDE_INT) pd.offset
2288 + pd.size) % BITS_PER_UNIT;
2289 if (amnt)
2290 shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2291 unsigned char *q = this_buffer;
2292 unsigned int off = 0;
2293 if (pd.offset >= 0)
2295 unsigned int msk;
2296 off = pd.offset / BITS_PER_UNIT;
2297 gcc_assert (off < needed_len);
2298 p = buffer + off;
2299 if (size <= amnt)
2301 msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2302 *p = (*p & ~msk) | (this_buffer[len] & msk);
2303 size = 0;
2305 else
2307 if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2308 q = (this_buffer + len
2309 - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2310 / BITS_PER_UNIT));
2311 if (pd.offset % BITS_PER_UNIT)
2313 msk = -1U << (BITS_PER_UNIT
2314 - (pd.offset % BITS_PER_UNIT));
2315 *p = (*p & msk) | (*q & ~msk);
2316 p++;
2317 q++;
2318 off++;
2319 size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2320 gcc_assert (size >= 0);
2324 else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2326 q = (this_buffer + len
2327 - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2328 / BITS_PER_UNIT));
2329 if (pd.offset % BITS_PER_UNIT)
2331 q++;
2332 size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2333 % BITS_PER_UNIT);
2334 gcc_assert (size >= 0);
2337 if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2338 > needed_len)
2339 size = (needed_len - off) * BITS_PER_UNIT;
2340 memcpy (p, q, size / BITS_PER_UNIT);
2341 if (size % BITS_PER_UNIT)
2343 unsigned int msk
2344 = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2345 p += size / BITS_PER_UNIT;
2346 q += size / BITS_PER_UNIT;
2347 *p = (*q & msk) | (*p & ~msk);
2350 else
2352 if (pd.offset >= 0)
2354 /* LSB of this_buffer[0] byte should be at pd.offset bits
2355 in buffer. */
2356 unsigned int msk;
2357 size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2358 amnt = pd.offset % BITS_PER_UNIT;
2359 if (amnt)
2360 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2361 unsigned int off = pd.offset / BITS_PER_UNIT;
2362 gcc_assert (off < needed_len);
2363 size = MIN (size,
2364 (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2365 p = buffer + off;
2366 if (amnt + size < BITS_PER_UNIT)
2368 /* Low amnt bits come from *p, then size bits
2369 from this_buffer[0] and the remaining again from
2370 *p. */
2371 msk = ((1 << size) - 1) << amnt;
2372 *p = (*p & ~msk) | (this_buffer[0] & msk);
2373 size = 0;
2375 else if (amnt)
2377 msk = -1U << amnt;
2378 *p = (*p & ~msk) | (this_buffer[0] & msk);
2379 p++;
2380 size -= (BITS_PER_UNIT - amnt);
2383 else
2385 amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2386 if (amnt)
2387 size -= BITS_PER_UNIT - amnt;
2388 size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2389 if (amnt)
2390 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2392 memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2393 p += size / BITS_PER_UNIT;
2394 if (size % BITS_PER_UNIT)
2396 unsigned int msk = -1U << (size % BITS_PER_UNIT);
2397 *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2398 & ~msk) | (*p & msk);
2403 tree type = vr->type;
2404 /* Make sure to interpret in a type that has a range covering the whole
2405 access size. */
2406 if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2407 type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2408 tree val;
2409 if (BYTES_BIG_ENDIAN)
2411 unsigned sz = needed_len;
2412 if (maxsizei % BITS_PER_UNIT)
2413 shift_bytes_in_array_right (buffer, needed_len,
2414 BITS_PER_UNIT
2415 - (maxsizei % BITS_PER_UNIT));
2416 if (INTEGRAL_TYPE_P (type))
2418 if (TYPE_MODE (type) != BLKmode)
2419 sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2420 else
2421 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
2423 if (sz > needed_len)
2425 memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2426 val = native_interpret_expr (type, this_buffer, sz);
2428 else
2429 val = native_interpret_expr (type, buffer, needed_len);
2431 else
2432 val = native_interpret_expr (type, buffer, bufsize);
2433 /* If we chop off bits because the types precision doesn't match the memory
2434 access size this is ok when optimizing reads but not when called from
2435 the DSE code during elimination. */
2436 if (val && type != vr->type)
2438 if (! int_fits_type_p (val, vr->type))
2439 val = NULL_TREE;
2440 else
2441 val = fold_convert (vr->type, val);
2444 if (val)
2446 if (dump_file && (dump_flags & TDF_DETAILS))
2447 fprintf (dump_file,
2448 "Successfully combined %u partial definitions\n", ndefs);
2449 /* We are using the alias-set of the first store we encounter which
2450 should be appropriate here. */
2451 return finish (first_set, first_base_set, val);
2453 else
2455 if (dump_file && (dump_flags & TDF_DETAILS))
2456 fprintf (dump_file,
2457 "Failed to interpret %u encoded partial definitions\n", ndefs);
2458 return (void *)-1;
2462 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2463 with the current VUSE and performs the expression lookup. */
2465 static void *
2466 vn_reference_lookup_2 (ao_ref *op, tree vuse, void *data_)
2468 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2469 vn_reference_t vr = data->vr;
2470 vn_reference_s **slot;
2471 hashval_t hash;
2473 /* If we have partial definitions recorded we have to go through
2474 vn_reference_lookup_3. */
2475 if (!data->partial_defs.is_empty ())
2476 return NULL;
2478 if (data->last_vuse_ptr)
2480 *data->last_vuse_ptr = vuse;
2481 data->last_vuse = vuse;
2484 /* Fixup vuse and hash. */
2485 if (vr->vuse)
2486 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2487 vr->vuse = vuse_ssa_val (vuse);
2488 if (vr->vuse)
2489 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2491 hash = vr->hashcode;
2492 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2493 if (slot)
2495 if ((*slot)->result && data->saved_operands.exists ())
2496 return data->finish (vr->set, vr->base_set, (*slot)->result);
2497 return *slot;
2500 if (SSA_NAME_IS_DEFAULT_DEF (vuse))
2502 HOST_WIDE_INT op_offset, op_size;
2503 tree v = NULL_TREE;
2504 tree base = ao_ref_base (op);
2506 if (base
2507 && op->offset.is_constant (&op_offset)
2508 && op->size.is_constant (&op_size)
2509 && op->max_size_known_p ()
2510 && known_eq (op->size, op->max_size))
2512 if (TREE_CODE (base) == PARM_DECL)
2513 v = ipcp_get_aggregate_const (cfun, base, false, op_offset,
2514 op_size);
2515 else if (TREE_CODE (base) == MEM_REF
2516 && integer_zerop (TREE_OPERAND (base, 1))
2517 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2518 && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0))
2519 && (TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (base, 0)))
2520 == PARM_DECL))
2521 v = ipcp_get_aggregate_const (cfun,
2522 SSA_NAME_VAR (TREE_OPERAND (base, 0)),
2523 true, op_offset, op_size);
2525 if (v)
2526 return data->finish (vr->set, vr->base_set, v);
2529 return NULL;
2532 /* Lookup an existing or insert a new vn_reference entry into the
2533 value table for the VUSE, SET, TYPE, OPERANDS reference which
2534 has the value VALUE which is either a constant or an SSA name. */
2536 static vn_reference_t
2537 vn_reference_lookup_or_insert_for_pieces (tree vuse,
2538 alias_set_type set,
2539 alias_set_type base_set,
2540 tree type,
2541 vec<vn_reference_op_s,
2542 va_heap> operands,
2543 tree value)
2545 vn_reference_s vr1;
2546 vn_reference_t result;
2547 unsigned value_id;
2548 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2549 vr1.operands = operands;
2550 vr1.type = type;
2551 vr1.set = set;
2552 vr1.base_set = base_set;
2553 vr1.hashcode = vn_reference_compute_hash (&vr1);
2554 if (vn_reference_lookup_1 (&vr1, &result))
2555 return result;
2556 if (TREE_CODE (value) == SSA_NAME)
2557 value_id = VN_INFO (value)->value_id;
2558 else
2559 value_id = get_or_alloc_constant_value_id (value);
2560 return vn_reference_insert_pieces (vuse, set, base_set, type,
2561 operands.copy (), value, value_id);
2564 /* Return a value-number for RCODE OPS... either by looking up an existing
2565 value-number for the possibly simplified result or by inserting the
2566 operation if INSERT is true. If SIMPLIFY is false, return a value
2567 number for the unsimplified expression. */
2569 static tree
2570 vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2571 bool simplify)
2573 tree result = NULL_TREE;
2574 /* We will be creating a value number for
2575 RCODE (OPS...).
2576 So first simplify and lookup this expression to see if it
2577 is already available. */
2578 /* For simplification valueize. */
2579 unsigned i = 0;
2580 if (simplify)
2581 for (i = 0; i < res_op->num_ops; ++i)
2582 if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2584 tree tem = vn_valueize (res_op->ops[i]);
2585 if (!tem)
2586 break;
2587 res_op->ops[i] = tem;
2589 /* If valueization of an operand fails (it is not available), skip
2590 simplification. */
2591 bool res = false;
2592 if (i == res_op->num_ops)
2594 mprts_hook = vn_lookup_simplify_result;
2595 res = res_op->resimplify (NULL, vn_valueize);
2596 mprts_hook = NULL;
2598 gimple *new_stmt = NULL;
2599 if (res
2600 && gimple_simplified_result_is_gimple_val (res_op))
2602 /* The expression is already available. */
2603 result = res_op->ops[0];
2604 /* Valueize it, simplification returns sth in AVAIL only. */
2605 if (TREE_CODE (result) == SSA_NAME)
2606 result = SSA_VAL (result);
2608 else
2610 tree val = vn_lookup_simplify_result (res_op);
2611 if (!val && insert)
2613 gimple_seq stmts = NULL;
2614 result = maybe_push_res_to_seq (res_op, &stmts);
2615 if (result)
2617 gcc_assert (gimple_seq_singleton_p (stmts));
2618 new_stmt = gimple_seq_first_stmt (stmts);
2621 else
2622 /* The expression is already available. */
2623 result = val;
2625 if (new_stmt)
2627 /* The expression is not yet available, value-number lhs to
2628 the new SSA_NAME we created. */
2629 /* Initialize value-number information properly. */
2630 vn_ssa_aux_t result_info = VN_INFO (result);
2631 result_info->valnum = result;
2632 result_info->value_id = get_next_value_id ();
2633 result_info->visited = 1;
2634 gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2635 new_stmt);
2636 result_info->needs_insertion = true;
2637 /* ??? PRE phi-translation inserts NARYs without corresponding
2638 SSA name result. Re-use those but set their result according
2639 to the stmt we just built. */
2640 vn_nary_op_t nary = NULL;
2641 vn_nary_op_lookup_stmt (new_stmt, &nary);
2642 if (nary)
2644 gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2645 nary->u.result = gimple_assign_lhs (new_stmt);
2647 /* As all "inserted" statements are singleton SCCs, insert
2648 to the valid table. This is strictly needed to
2649 avoid re-generating new value SSA_NAMEs for the same
2650 expression during SCC iteration over and over (the
2651 optimistic table gets cleared after each iteration).
2652 We do not need to insert into the optimistic table, as
2653 lookups there will fall back to the valid table. */
2654 else
2656 unsigned int length = vn_nary_length_from_stmt (new_stmt);
2657 vn_nary_op_t vno1
2658 = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2659 vno1->value_id = result_info->value_id;
2660 vno1->length = length;
2661 vno1->predicated_values = 0;
2662 vno1->u.result = result;
2663 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2664 vn_nary_op_insert_into (vno1, valid_info->nary);
2665 /* Also do not link it into the undo chain. */
2666 last_inserted_nary = vno1->next;
2667 vno1->next = (vn_nary_op_t)(void *)-1;
2669 if (dump_file && (dump_flags & TDF_DETAILS))
2671 fprintf (dump_file, "Inserting name ");
2672 print_generic_expr (dump_file, result);
2673 fprintf (dump_file, " for expression ");
2674 print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2675 fprintf (dump_file, "\n");
2678 return result;
2681 /* Return a value-number for RCODE OPS... either by looking up an existing
2682 value-number for the simplified result or by inserting the operation. */
2684 static tree
2685 vn_nary_build_or_lookup (gimple_match_op *res_op)
2687 return vn_nary_build_or_lookup_1 (res_op, true, true);
2690 /* Try to simplify the expression RCODE OPS... of type TYPE and return
2691 its value if present. */
2693 tree
2694 vn_nary_simplify (vn_nary_op_t nary)
2696 if (nary->length > gimple_match_op::MAX_NUM_OPS)
2697 return NULL_TREE;
2698 gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2699 nary->type, nary->length);
2700 memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2701 return vn_nary_build_or_lookup_1 (&op, false, true);
2704 /* Elimination engine. */
2706 class eliminate_dom_walker : public dom_walker
2708 public:
2709 eliminate_dom_walker (cdi_direction, bitmap);
2710 ~eliminate_dom_walker ();
2712 edge before_dom_children (basic_block) final override;
2713 void after_dom_children (basic_block) final override;
2715 virtual tree eliminate_avail (basic_block, tree op);
2716 virtual void eliminate_push_avail (basic_block, tree op);
2717 tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2719 void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2721 unsigned eliminate_cleanup (bool region_p = false);
2723 bool do_pre;
2724 unsigned int el_todo;
2725 unsigned int eliminations;
2726 unsigned int insertions;
2728 /* SSA names that had their defs inserted by PRE if do_pre. */
2729 bitmap inserted_exprs;
2731 /* Blocks with statements that have had their EH properties changed. */
2732 bitmap need_eh_cleanup;
2734 /* Blocks with statements that have had their AB properties changed. */
2735 bitmap need_ab_cleanup;
2737 /* Local state for the eliminate domwalk. */
2738 auto_vec<gimple *> to_remove;
2739 auto_vec<gimple *> to_fixup;
2740 auto_vec<tree> avail;
2741 auto_vec<tree> avail_stack;
2744 /* Adaptor to the elimination engine using RPO availability. */
2746 class rpo_elim : public eliminate_dom_walker
2748 public:
2749 rpo_elim(basic_block entry_)
2750 : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2751 m_avail_freelist (NULL) {}
2753 tree eliminate_avail (basic_block, tree op) final override;
2755 void eliminate_push_avail (basic_block, tree) final override;
2757 basic_block entry;
2758 /* Freelist of avail entries which are allocated from the vn_ssa_aux
2759 obstack. */
2760 vn_avail *m_avail_freelist;
2763 /* Global RPO state for access from hooks. */
2764 static eliminate_dom_walker *rpo_avail;
2765 basic_block vn_context_bb;
2767 /* Return true if BASE1 and BASE2 can be adjusted so they have the
2768 same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2769 Otherwise return false. */
2771 static bool
2772 adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2773 tree base2, poly_int64 *offset2)
2775 poly_int64 soff;
2776 if (TREE_CODE (base1) == MEM_REF
2777 && TREE_CODE (base2) == MEM_REF)
2779 if (mem_ref_offset (base1).to_shwi (&soff))
2781 base1 = TREE_OPERAND (base1, 0);
2782 *offset1 += soff * BITS_PER_UNIT;
2784 if (mem_ref_offset (base2).to_shwi (&soff))
2786 base2 = TREE_OPERAND (base2, 0);
2787 *offset2 += soff * BITS_PER_UNIT;
2789 return operand_equal_p (base1, base2, 0);
2791 return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2794 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2795 from the statement defining VUSE and if not successful tries to
2796 translate *REFP and VR_ through an aggregate copy at the definition
2797 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2798 of *REF and *VR. If only disambiguation was performed then
2799 *DISAMBIGUATE_ONLY is set to true. */
2801 static void *
2802 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2803 translate_flags *disambiguate_only)
2805 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2806 vn_reference_t vr = data->vr;
2807 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2808 tree base = ao_ref_base (ref);
2809 HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2810 static vec<vn_reference_op_s> lhs_ops;
2811 ao_ref lhs_ref;
2812 bool lhs_ref_ok = false;
2813 poly_int64 copy_size;
2815 /* First try to disambiguate after value-replacing in the definitions LHS. */
2816 if (is_gimple_assign (def_stmt))
2818 tree lhs = gimple_assign_lhs (def_stmt);
2819 bool valueized_anything = false;
2820 /* Avoid re-allocation overhead. */
2821 lhs_ops.truncate (0);
2822 basic_block saved_rpo_bb = vn_context_bb;
2823 vn_context_bb = gimple_bb (def_stmt);
2824 if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2826 copy_reference_ops_from_ref (lhs, &lhs_ops);
2827 valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2829 vn_context_bb = saved_rpo_bb;
2830 ao_ref_init (&lhs_ref, lhs);
2831 lhs_ref_ok = true;
2832 if (valueized_anything
2833 && ao_ref_init_from_vn_reference
2834 (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2835 ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2836 && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2838 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2839 return NULL;
2842 /* When the def is a CLOBBER we can optimistically disambiguate
2843 against it since any overlap it would be undefined behavior.
2844 Avoid this for obvious must aliases to save compile-time though.
2845 We also may not do this when the query is used for redundant
2846 store removal. */
2847 if (!data->redundant_store_removal_p
2848 && gimple_clobber_p (def_stmt)
2849 && !operand_equal_p (ao_ref_base (&lhs_ref), base, OEP_ADDRESS_OF))
2851 *disambiguate_only = TR_DISAMBIGUATE;
2852 return NULL;
2855 /* Besides valueizing the LHS we can also use access-path based
2856 disambiguation on the original non-valueized ref. */
2857 if (!ref->ref
2858 && lhs_ref_ok
2859 && data->orig_ref.ref)
2861 /* We want to use the non-valueized LHS for this, but avoid redundant
2862 work. */
2863 ao_ref *lref = &lhs_ref;
2864 ao_ref lref_alt;
2865 if (valueized_anything)
2867 ao_ref_init (&lref_alt, lhs);
2868 lref = &lref_alt;
2870 if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2872 *disambiguate_only = (valueized_anything
2873 ? TR_VALUEIZE_AND_DISAMBIGUATE
2874 : TR_DISAMBIGUATE);
2875 return NULL;
2879 /* If we reach a clobbering statement try to skip it and see if
2880 we find a VN result with exactly the same value as the
2881 possible clobber. In this case we can ignore the clobber
2882 and return the found value. */
2883 if (is_gimple_reg_type (TREE_TYPE (lhs))
2884 && types_compatible_p (TREE_TYPE (lhs), vr->type)
2885 && (ref->ref || data->orig_ref.ref)
2886 && !data->mask
2887 && data->partial_defs.is_empty ()
2888 && multiple_p (get_object_alignment
2889 (ref->ref ? ref->ref : data->orig_ref.ref),
2890 ref->size)
2891 && multiple_p (get_object_alignment (lhs), ref->size))
2893 tree rhs = gimple_assign_rhs1 (def_stmt);
2894 /* ??? We may not compare to ahead values which might be from
2895 a different loop iteration but only to loop invariants. Use
2896 CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
2897 The one-hop lookup below doesn't have this issue since there's
2898 a virtual PHI before we ever reach a backedge to cross.
2899 We can skip multiple defs as long as they are from the same
2900 value though. */
2901 if (data->same_val
2902 && !operand_equal_p (data->same_val, rhs))
2904 else if (CONSTANT_CLASS_P (rhs))
2906 if (dump_file && (dump_flags & TDF_DETAILS))
2908 fprintf (dump_file,
2909 "Skipping possible redundant definition ");
2910 print_gimple_stmt (dump_file, def_stmt, 0);
2912 /* Delay the actual compare of the values to the end of the walk
2913 but do not update last_vuse from here. */
2914 data->last_vuse_ptr = NULL;
2915 data->same_val = rhs;
2916 return NULL;
2918 else
2920 tree saved_vuse = vr->vuse;
2921 hashval_t saved_hashcode = vr->hashcode;
2922 if (vr->vuse)
2923 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2924 vr->vuse = vuse_ssa_val (gimple_vuse (def_stmt));
2925 if (vr->vuse)
2926 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2927 vn_reference_t vnresult = NULL;
2928 /* Do not use vn_reference_lookup_2 since that might perform
2929 expression hashtable insertion but this lookup crosses
2930 a possible may-alias making such insertion conditionally
2931 invalid. */
2932 vn_reference_lookup_1 (vr, &vnresult);
2933 /* Need to restore vr->vuse and vr->hashcode. */
2934 vr->vuse = saved_vuse;
2935 vr->hashcode = saved_hashcode;
2936 if (vnresult)
2938 if (TREE_CODE (rhs) == SSA_NAME)
2939 rhs = SSA_VAL (rhs);
2940 if (vnresult->result
2941 && operand_equal_p (vnresult->result, rhs, 0))
2942 return vnresult;
2947 else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2948 && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2949 && gimple_call_num_args (def_stmt) <= 4)
2951 /* For builtin calls valueize its arguments and call the
2952 alias oracle again. Valueization may improve points-to
2953 info of pointers and constify size and position arguments.
2954 Originally this was motivated by PR61034 which has
2955 conditional calls to free falsely clobbering ref because
2956 of imprecise points-to info of the argument. */
2957 tree oldargs[4];
2958 bool valueized_anything = false;
2959 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2961 oldargs[i] = gimple_call_arg (def_stmt, i);
2962 tree val = vn_valueize (oldargs[i]);
2963 if (val != oldargs[i])
2965 gimple_call_set_arg (def_stmt, i, val);
2966 valueized_anything = true;
2969 if (valueized_anything)
2971 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2972 ref, data->tbaa_p);
2973 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2974 gimple_call_set_arg (def_stmt, i, oldargs[i]);
2975 if (!res)
2977 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2978 return NULL;
2983 if (*disambiguate_only > TR_TRANSLATE)
2984 return (void *)-1;
2986 /* If we cannot constrain the size of the reference we cannot
2987 test if anything kills it. */
2988 if (!ref->max_size_known_p ())
2989 return (void *)-1;
2991 poly_int64 offset = ref->offset;
2992 poly_int64 maxsize = ref->max_size;
2994 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2995 from that definition.
2996 1) Memset. */
2997 if (is_gimple_reg_type (vr->type)
2998 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2999 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
3000 && (integer_zerop (gimple_call_arg (def_stmt, 1))
3001 || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
3002 || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
3003 && CHAR_BIT == 8
3004 && BITS_PER_UNIT == 8
3005 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3006 && offset.is_constant (&offseti)
3007 && ref->size.is_constant (&sizei)
3008 && (offseti % BITS_PER_UNIT == 0
3009 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
3010 && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
3011 || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3012 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
3013 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3014 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
3016 tree base2;
3017 poly_int64 offset2, size2, maxsize2;
3018 bool reverse;
3019 tree ref2 = gimple_call_arg (def_stmt, 0);
3020 if (TREE_CODE (ref2) == SSA_NAME)
3022 ref2 = SSA_VAL (ref2);
3023 if (TREE_CODE (ref2) == SSA_NAME
3024 && (TREE_CODE (base) != MEM_REF
3025 || TREE_OPERAND (base, 0) != ref2))
3027 gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
3028 if (gimple_assign_single_p (def_stmt)
3029 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3030 ref2 = gimple_assign_rhs1 (def_stmt);
3033 if (TREE_CODE (ref2) == ADDR_EXPR)
3035 ref2 = TREE_OPERAND (ref2, 0);
3036 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
3037 &reverse);
3038 if (!known_size_p (maxsize2)
3039 || !known_eq (maxsize2, size2)
3040 || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
3041 return (void *)-1;
3043 else if (TREE_CODE (ref2) == SSA_NAME)
3045 poly_int64 soff;
3046 if (TREE_CODE (base) != MEM_REF
3047 || !(mem_ref_offset (base)
3048 << LOG2_BITS_PER_UNIT).to_shwi (&soff))
3049 return (void *)-1;
3050 offset += soff;
3051 offset2 = 0;
3052 if (TREE_OPERAND (base, 0) != ref2)
3054 gimple *def = SSA_NAME_DEF_STMT (ref2);
3055 if (is_gimple_assign (def)
3056 && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
3057 && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
3058 && poly_int_tree_p (gimple_assign_rhs2 (def)))
3060 tree rhs2 = gimple_assign_rhs2 (def);
3061 if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
3062 SIGNED)
3063 << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
3064 return (void *)-1;
3065 ref2 = gimple_assign_rhs1 (def);
3066 if (TREE_CODE (ref2) == SSA_NAME)
3067 ref2 = SSA_VAL (ref2);
3069 else
3070 return (void *)-1;
3073 else
3074 return (void *)-1;
3075 tree len = gimple_call_arg (def_stmt, 2);
3076 HOST_WIDE_INT leni, offset2i;
3077 if (TREE_CODE (len) == SSA_NAME)
3078 len = SSA_VAL (len);
3079 /* Sometimes the above trickery is smarter than alias analysis. Take
3080 advantage of that. */
3081 if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
3082 (wi::to_poly_offset (len)
3083 << LOG2_BITS_PER_UNIT)))
3084 return NULL;
3085 if (data->partial_defs.is_empty ()
3086 && known_subrange_p (offset, maxsize, offset2,
3087 wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
3089 tree val;
3090 if (integer_zerop (gimple_call_arg (def_stmt, 1)))
3091 val = build_zero_cst (vr->type);
3092 else if (INTEGRAL_TYPE_P (vr->type)
3093 && known_eq (ref->size, 8)
3094 && offseti % BITS_PER_UNIT == 0)
3096 gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
3097 vr->type, gimple_call_arg (def_stmt, 1));
3098 val = vn_nary_build_or_lookup (&res_op);
3099 if (!val
3100 || (TREE_CODE (val) == SSA_NAME
3101 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3102 return (void *)-1;
3104 else
3106 unsigned buflen
3107 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
3108 if (INTEGRAL_TYPE_P (vr->type)
3109 && TYPE_MODE (vr->type) != BLKmode)
3110 buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
3111 unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
3112 memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
3113 buflen);
3114 if (BYTES_BIG_ENDIAN)
3116 unsigned int amnt
3117 = (((unsigned HOST_WIDE_INT) offseti + sizei)
3118 % BITS_PER_UNIT);
3119 if (amnt)
3121 shift_bytes_in_array_right (buf, buflen,
3122 BITS_PER_UNIT - amnt);
3123 buf++;
3124 buflen--;
3127 else if (offseti % BITS_PER_UNIT != 0)
3129 unsigned int amnt
3130 = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
3131 % BITS_PER_UNIT);
3132 shift_bytes_in_array_left (buf, buflen, amnt);
3133 buf++;
3134 buflen--;
3136 val = native_interpret_expr (vr->type, buf, buflen);
3137 if (!val)
3138 return (void *)-1;
3140 return data->finish (0, 0, val);
3142 /* For now handle clearing memory with partial defs. */
3143 else if (known_eq (ref->size, maxsize)
3144 && integer_zerop (gimple_call_arg (def_stmt, 1))
3145 && tree_fits_poly_int64_p (len)
3146 && tree_to_poly_int64 (len).is_constant (&leni)
3147 && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
3148 && offset.is_constant (&offseti)
3149 && offset2.is_constant (&offset2i)
3150 && maxsize.is_constant (&maxsizei)
3151 && ranges_known_overlap_p (offseti, maxsizei, offset2i,
3152 leni << LOG2_BITS_PER_UNIT))
3154 pd_data pd;
3155 pd.rhs = build_constructor (NULL_TREE, NULL);
3156 pd.rhs_off = 0;
3157 pd.offset = offset2i;
3158 pd.size = leni << LOG2_BITS_PER_UNIT;
3159 return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
3163 /* 2) Assignment from an empty CONSTRUCTOR. */
3164 else if (is_gimple_reg_type (vr->type)
3165 && gimple_assign_single_p (def_stmt)
3166 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
3167 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
3169 tree base2;
3170 poly_int64 offset2, size2, maxsize2;
3171 HOST_WIDE_INT offset2i, size2i;
3172 gcc_assert (lhs_ref_ok);
3173 base2 = ao_ref_base (&lhs_ref);
3174 offset2 = lhs_ref.offset;
3175 size2 = lhs_ref.size;
3176 maxsize2 = lhs_ref.max_size;
3177 if (known_size_p (maxsize2)
3178 && known_eq (maxsize2, size2)
3179 && adjust_offsets_for_equal_base_address (base, &offset,
3180 base2, &offset2))
3182 if (data->partial_defs.is_empty ()
3183 && known_subrange_p (offset, maxsize, offset2, size2))
3185 /* While technically undefined behavior do not optimize
3186 a full read from a clobber. */
3187 if (gimple_clobber_p (def_stmt))
3188 return (void *)-1;
3189 tree val = build_zero_cst (vr->type);
3190 return data->finish (ao_ref_alias_set (&lhs_ref),
3191 ao_ref_base_alias_set (&lhs_ref), val);
3193 else if (known_eq (ref->size, maxsize)
3194 && maxsize.is_constant (&maxsizei)
3195 && offset.is_constant (&offseti)
3196 && offset2.is_constant (&offset2i)
3197 && size2.is_constant (&size2i)
3198 && ranges_known_overlap_p (offseti, maxsizei,
3199 offset2i, size2i))
3201 /* Let clobbers be consumed by the partial-def tracker
3202 which can choose to ignore them if they are shadowed
3203 by a later def. */
3204 pd_data pd;
3205 pd.rhs = gimple_assign_rhs1 (def_stmt);
3206 pd.rhs_off = 0;
3207 pd.offset = offset2i;
3208 pd.size = size2i;
3209 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3210 ao_ref_base_alias_set (&lhs_ref),
3211 offseti, maxsizei);
3216 /* 3) Assignment from a constant. We can use folds native encode/interpret
3217 routines to extract the assigned bits. */
3218 else if (known_eq (ref->size, maxsize)
3219 && is_gimple_reg_type (vr->type)
3220 && !reverse_storage_order_for_component_p (vr->operands)
3221 && !contains_storage_order_barrier_p (vr->operands)
3222 && gimple_assign_single_p (def_stmt)
3223 && CHAR_BIT == 8
3224 && BITS_PER_UNIT == 8
3225 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
3226 /* native_encode and native_decode operate on arrays of bytes
3227 and so fundamentally need a compile-time size and offset. */
3228 && maxsize.is_constant (&maxsizei)
3229 && offset.is_constant (&offseti)
3230 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
3231 || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
3232 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
3234 tree lhs = gimple_assign_lhs (def_stmt);
3235 tree base2;
3236 poly_int64 offset2, size2, maxsize2;
3237 HOST_WIDE_INT offset2i, size2i;
3238 bool reverse;
3239 gcc_assert (lhs_ref_ok);
3240 base2 = ao_ref_base (&lhs_ref);
3241 offset2 = lhs_ref.offset;
3242 size2 = lhs_ref.size;
3243 maxsize2 = lhs_ref.max_size;
3244 reverse = reverse_storage_order_for_component_p (lhs);
3245 if (base2
3246 && !reverse
3247 && !storage_order_barrier_p (lhs)
3248 && known_eq (maxsize2, size2)
3249 && adjust_offsets_for_equal_base_address (base, &offset,
3250 base2, &offset2)
3251 && offset.is_constant (&offseti)
3252 && offset2.is_constant (&offset2i)
3253 && size2.is_constant (&size2i))
3255 if (data->partial_defs.is_empty ()
3256 && known_subrange_p (offseti, maxsizei, offset2, size2))
3258 /* We support up to 512-bit values (for V8DFmode). */
3259 unsigned char buffer[65];
3260 int len;
3262 tree rhs = gimple_assign_rhs1 (def_stmt);
3263 if (TREE_CODE (rhs) == SSA_NAME)
3264 rhs = SSA_VAL (rhs);
3265 len = native_encode_expr (rhs,
3266 buffer, sizeof (buffer) - 1,
3267 (offseti - offset2i) / BITS_PER_UNIT);
3268 if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
3270 tree type = vr->type;
3271 unsigned char *buf = buffer;
3272 unsigned int amnt = 0;
3273 /* Make sure to interpret in a type that has a range
3274 covering the whole access size. */
3275 if (INTEGRAL_TYPE_P (vr->type)
3276 && maxsizei != TYPE_PRECISION (vr->type))
3277 type = build_nonstandard_integer_type (maxsizei,
3278 TYPE_UNSIGNED (type));
3279 if (BYTES_BIG_ENDIAN)
3281 /* For big-endian native_encode_expr stored the rhs
3282 such that the LSB of it is the LSB of buffer[len - 1].
3283 That bit is stored into memory at position
3284 offset2 + size2 - 1, i.e. in byte
3285 base + (offset2 + size2 - 1) / BITS_PER_UNIT.
3286 E.g. for offset2 1 and size2 14, rhs -1 and memory
3287 previously cleared that is:
3289 01111111|11111110
3290 Now, if we want to extract offset 2 and size 12 from
3291 it using native_interpret_expr (which actually works
3292 for integral bitfield types in terms of byte size of
3293 the mode), the native_encode_expr stored the value
3294 into buffer as
3295 XX111111|11111111
3296 and returned len 2 (the X bits are outside of
3297 precision).
3298 Let sz be maxsize / BITS_PER_UNIT if not extracting
3299 a bitfield, and GET_MODE_SIZE otherwise.
3300 We need to align the LSB of the value we want to
3301 extract as the LSB of buf[sz - 1].
3302 The LSB from memory we need to read is at position
3303 offset + maxsize - 1. */
3304 HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3305 if (INTEGRAL_TYPE_P (type))
3307 if (TYPE_MODE (type) != BLKmode)
3308 sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3309 else
3310 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type));
3312 amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3313 - offseti - maxsizei) % BITS_PER_UNIT;
3314 if (amnt)
3315 shift_bytes_in_array_right (buffer, len, amnt);
3316 amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3317 - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3318 if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3319 len = 0;
3320 else
3322 buf = buffer + len - sz - amnt;
3323 len -= (buf - buffer);
3326 else
3328 amnt = ((unsigned HOST_WIDE_INT) offset2i
3329 - offseti) % BITS_PER_UNIT;
3330 if (amnt)
3332 buffer[len] = 0;
3333 shift_bytes_in_array_left (buffer, len + 1, amnt);
3334 buf = buffer + 1;
3337 tree val = native_interpret_expr (type, buf, len);
3338 /* If we chop off bits because the types precision doesn't
3339 match the memory access size this is ok when optimizing
3340 reads but not when called from the DSE code during
3341 elimination. */
3342 if (val
3343 && type != vr->type)
3345 if (! int_fits_type_p (val, vr->type))
3346 val = NULL_TREE;
3347 else
3348 val = fold_convert (vr->type, val);
3351 if (val)
3352 return data->finish (ao_ref_alias_set (&lhs_ref),
3353 ao_ref_base_alias_set (&lhs_ref), val);
3356 else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3357 size2i))
3359 pd_data pd;
3360 tree rhs = gimple_assign_rhs1 (def_stmt);
3361 if (TREE_CODE (rhs) == SSA_NAME)
3362 rhs = SSA_VAL (rhs);
3363 pd.rhs = rhs;
3364 pd.rhs_off = 0;
3365 pd.offset = offset2i;
3366 pd.size = size2i;
3367 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3368 ao_ref_base_alias_set (&lhs_ref),
3369 offseti, maxsizei);
3374 /* 4) Assignment from an SSA name which definition we may be able
3375 to access pieces from or we can combine to a larger entity. */
3376 else if (known_eq (ref->size, maxsize)
3377 && is_gimple_reg_type (vr->type)
3378 && !reverse_storage_order_for_component_p (vr->operands)
3379 && !contains_storage_order_barrier_p (vr->operands)
3380 && gimple_assign_single_p (def_stmt)
3381 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3383 tree lhs = gimple_assign_lhs (def_stmt);
3384 tree base2;
3385 poly_int64 offset2, size2, maxsize2;
3386 HOST_WIDE_INT offset2i, size2i, offseti;
3387 bool reverse;
3388 gcc_assert (lhs_ref_ok);
3389 base2 = ao_ref_base (&lhs_ref);
3390 offset2 = lhs_ref.offset;
3391 size2 = lhs_ref.size;
3392 maxsize2 = lhs_ref.max_size;
3393 reverse = reverse_storage_order_for_component_p (lhs);
3394 tree def_rhs = gimple_assign_rhs1 (def_stmt);
3395 if (!reverse
3396 && !storage_order_barrier_p (lhs)
3397 && known_size_p (maxsize2)
3398 && known_eq (maxsize2, size2)
3399 && adjust_offsets_for_equal_base_address (base, &offset,
3400 base2, &offset2))
3402 if (data->partial_defs.is_empty ()
3403 && known_subrange_p (offset, maxsize, offset2, size2)
3404 /* ??? We can't handle bitfield precision extracts without
3405 either using an alternate type for the BIT_FIELD_REF and
3406 then doing a conversion or possibly adjusting the offset
3407 according to endianness. */
3408 && (! INTEGRAL_TYPE_P (vr->type)
3409 || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3410 && multiple_p (ref->size, BITS_PER_UNIT))
3412 tree val = NULL_TREE;
3413 if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3414 || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3416 gimple_match_op op (gimple_match_cond::UNCOND,
3417 BIT_FIELD_REF, vr->type,
3418 SSA_VAL (def_rhs),
3419 bitsize_int (ref->size),
3420 bitsize_int (offset - offset2));
3421 val = vn_nary_build_or_lookup (&op);
3423 else if (known_eq (ref->size, size2))
3425 gimple_match_op op (gimple_match_cond::UNCOND,
3426 VIEW_CONVERT_EXPR, vr->type,
3427 SSA_VAL (def_rhs));
3428 val = vn_nary_build_or_lookup (&op);
3430 if (val
3431 && (TREE_CODE (val) != SSA_NAME
3432 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3433 return data->finish (ao_ref_alias_set (&lhs_ref),
3434 ao_ref_base_alias_set (&lhs_ref), val);
3436 else if (maxsize.is_constant (&maxsizei)
3437 && offset.is_constant (&offseti)
3438 && offset2.is_constant (&offset2i)
3439 && size2.is_constant (&size2i)
3440 && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3442 pd_data pd;
3443 pd.rhs = SSA_VAL (def_rhs);
3444 pd.rhs_off = 0;
3445 pd.offset = offset2i;
3446 pd.size = size2i;
3447 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3448 ao_ref_base_alias_set (&lhs_ref),
3449 offseti, maxsizei);
3454 /* 4b) Assignment done via one of the vectorizer internal store
3455 functions where we may be able to access pieces from or we can
3456 combine to a larger entity. */
3457 else if (known_eq (ref->size, maxsize)
3458 && is_gimple_reg_type (vr->type)
3459 && !reverse_storage_order_for_component_p (vr->operands)
3460 && !contains_storage_order_barrier_p (vr->operands)
3461 && is_gimple_call (def_stmt)
3462 && gimple_call_internal_p (def_stmt)
3463 && internal_store_fn_p (gimple_call_internal_fn (def_stmt)))
3465 gcall *call = as_a <gcall *> (def_stmt);
3466 internal_fn fn = gimple_call_internal_fn (call);
3468 tree mask = NULL_TREE, len = NULL_TREE, bias = NULL_TREE;
3469 switch (fn)
3471 case IFN_MASK_STORE:
3472 mask = gimple_call_arg (call, internal_fn_mask_index (fn));
3473 mask = vn_valueize (mask);
3474 if (TREE_CODE (mask) != VECTOR_CST)
3475 return (void *)-1;
3476 break;
3477 case IFN_LEN_STORE:
3479 int len_index = internal_fn_len_index (fn);
3480 len = gimple_call_arg (call, len_index);
3481 bias = gimple_call_arg (call, len_index + 1);
3482 if (!tree_fits_uhwi_p (len) || !tree_fits_shwi_p (bias))
3483 return (void *) -1;
3484 break;
3486 default:
3487 return (void *)-1;
3489 tree def_rhs = gimple_call_arg (call,
3490 internal_fn_stored_value_index (fn));
3491 def_rhs = vn_valueize (def_rhs);
3492 if (TREE_CODE (def_rhs) != VECTOR_CST)
3493 return (void *)-1;
3495 ao_ref_init_from_ptr_and_size (&lhs_ref,
3496 vn_valueize (gimple_call_arg (call, 0)),
3497 TYPE_SIZE_UNIT (TREE_TYPE (def_rhs)));
3498 tree base2;
3499 poly_int64 offset2, size2, maxsize2;
3500 HOST_WIDE_INT offset2i, size2i, offseti;
3501 base2 = ao_ref_base (&lhs_ref);
3502 offset2 = lhs_ref.offset;
3503 size2 = lhs_ref.size;
3504 maxsize2 = lhs_ref.max_size;
3505 if (known_size_p (maxsize2)
3506 && known_eq (maxsize2, size2)
3507 && adjust_offsets_for_equal_base_address (base, &offset,
3508 base2, &offset2)
3509 && maxsize.is_constant (&maxsizei)
3510 && offset.is_constant (&offseti)
3511 && offset2.is_constant (&offset2i)
3512 && size2.is_constant (&size2i))
3514 if (!ranges_maybe_overlap_p (offset, maxsize, offset2, size2))
3515 /* Poor-mans disambiguation. */
3516 return NULL;
3517 else if (ranges_known_overlap_p (offset, maxsize, offset2, size2))
3519 pd_data pd;
3520 pd.rhs = def_rhs;
3521 tree aa = gimple_call_arg (call, 1);
3522 alias_set_type set = get_deref_alias_set (TREE_TYPE (aa));
3523 tree vectype = TREE_TYPE (def_rhs);
3524 unsigned HOST_WIDE_INT elsz
3525 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (vectype)));
3526 if (mask)
3528 HOST_WIDE_INT start = 0, length = 0;
3529 unsigned mask_idx = 0;
3532 if (integer_zerop (VECTOR_CST_ELT (mask, mask_idx)))
3534 if (length != 0)
3536 pd.rhs_off = start;
3537 pd.offset = offset2i + start;
3538 pd.size = length;
3539 if (ranges_known_overlap_p
3540 (offset, maxsize, pd.offset, pd.size))
3542 void *res = data->push_partial_def
3543 (pd, set, set, offseti, maxsizei);
3544 if (res != NULL)
3545 return res;
3548 start = (mask_idx + 1) * elsz;
3549 length = 0;
3551 else
3552 length += elsz;
3553 mask_idx++;
3555 while (known_lt (mask_idx, TYPE_VECTOR_SUBPARTS (vectype)));
3556 if (length != 0)
3558 pd.rhs_off = start;
3559 pd.offset = offset2i + start;
3560 pd.size = length;
3561 if (ranges_known_overlap_p (offset, maxsize,
3562 pd.offset, pd.size))
3563 return data->push_partial_def (pd, set, set,
3564 offseti, maxsizei);
3567 else if (fn == IFN_LEN_STORE)
3569 pd.offset = offset2i;
3570 pd.size = (tree_to_uhwi (len)
3571 + -tree_to_shwi (bias)) * BITS_PER_UNIT;
3572 if (BYTES_BIG_ENDIAN)
3573 pd.rhs_off = pd.size - tree_to_uhwi (TYPE_SIZE (vectype));
3574 else
3575 pd.rhs_off = 0;
3576 if (ranges_known_overlap_p (offset, maxsize,
3577 pd.offset, pd.size))
3578 return data->push_partial_def (pd, set, set,
3579 offseti, maxsizei);
3581 else
3582 gcc_unreachable ();
3583 return NULL;
3588 /* 5) For aggregate copies translate the reference through them if
3589 the copy kills ref. */
3590 else if (data->vn_walk_kind == VN_WALKREWRITE
3591 && gimple_assign_single_p (def_stmt)
3592 && (DECL_P (gimple_assign_rhs1 (def_stmt))
3593 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3594 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3596 tree base2;
3597 int i, j, k;
3598 auto_vec<vn_reference_op_s> rhs;
3599 vn_reference_op_t vro;
3600 ao_ref r;
3602 gcc_assert (lhs_ref_ok);
3604 /* See if the assignment kills REF. */
3605 base2 = ao_ref_base (&lhs_ref);
3606 if (!lhs_ref.max_size_known_p ()
3607 || (base != base2
3608 && (TREE_CODE (base) != MEM_REF
3609 || TREE_CODE (base2) != MEM_REF
3610 || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3611 || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3612 TREE_OPERAND (base2, 1))))
3613 || !stmt_kills_ref_p (def_stmt, ref))
3614 return (void *)-1;
3616 /* Find the common base of ref and the lhs. lhs_ops already
3617 contains valueized operands for the lhs. */
3618 i = vr->operands.length () - 1;
3619 j = lhs_ops.length () - 1;
3620 while (j >= 0 && i >= 0
3621 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3623 i--;
3624 j--;
3627 /* ??? The innermost op should always be a MEM_REF and we already
3628 checked that the assignment to the lhs kills vr. Thus for
3629 aggregate copies using char[] types the vn_reference_op_eq
3630 may fail when comparing types for compatibility. But we really
3631 don't care here - further lookups with the rewritten operands
3632 will simply fail if we messed up types too badly. */
3633 poly_int64 extra_off = 0;
3634 if (j == 0 && i >= 0
3635 && lhs_ops[0].opcode == MEM_REF
3636 && maybe_ne (lhs_ops[0].off, -1))
3638 if (known_eq (lhs_ops[0].off, vr->operands[i].off))
3639 i--, j--;
3640 else if (vr->operands[i].opcode == MEM_REF
3641 && maybe_ne (vr->operands[i].off, -1))
3643 extra_off = vr->operands[i].off - lhs_ops[0].off;
3644 i--, j--;
3648 /* i now points to the first additional op.
3649 ??? LHS may not be completely contained in VR, one or more
3650 VIEW_CONVERT_EXPRs could be in its way. We could at least
3651 try handling outermost VIEW_CONVERT_EXPRs. */
3652 if (j != -1)
3653 return (void *)-1;
3655 /* Punt if the additional ops contain a storage order barrier. */
3656 for (k = i; k >= 0; k--)
3658 vro = &vr->operands[k];
3659 if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3660 return (void *)-1;
3663 /* Now re-write REF to be based on the rhs of the assignment. */
3664 tree rhs1 = gimple_assign_rhs1 (def_stmt);
3665 copy_reference_ops_from_ref (rhs1, &rhs);
3667 /* Apply an extra offset to the inner MEM_REF of the RHS. */
3668 bool force_no_tbaa = false;
3669 if (maybe_ne (extra_off, 0))
3671 if (rhs.length () < 2)
3672 return (void *)-1;
3673 int ix = rhs.length () - 2;
3674 if (rhs[ix].opcode != MEM_REF
3675 || known_eq (rhs[ix].off, -1))
3676 return (void *)-1;
3677 rhs[ix].off += extra_off;
3678 rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3679 build_int_cst (TREE_TYPE (rhs[ix].op0),
3680 extra_off));
3681 /* When we have offsetted the RHS, reading only parts of it,
3682 we can no longer use the original TBAA type, force alias-set
3683 zero. */
3684 force_no_tbaa = true;
3687 /* Save the operands since we need to use the original ones for
3688 the hash entry we use. */
3689 if (!data->saved_operands.exists ())
3690 data->saved_operands = vr->operands.copy ();
3692 /* We need to pre-pend vr->operands[0..i] to rhs. */
3693 vec<vn_reference_op_s> old = vr->operands;
3694 if (i + 1 + rhs.length () > vr->operands.length ())
3695 vr->operands.safe_grow (i + 1 + rhs.length (), true);
3696 else
3697 vr->operands.truncate (i + 1 + rhs.length ());
3698 FOR_EACH_VEC_ELT (rhs, j, vro)
3699 vr->operands[i + 1 + j] = *vro;
3700 valueize_refs (&vr->operands);
3701 if (old == shared_lookup_references)
3702 shared_lookup_references = vr->operands;
3703 vr->hashcode = vn_reference_compute_hash (vr);
3705 /* Try folding the new reference to a constant. */
3706 tree val = fully_constant_vn_reference_p (vr);
3707 if (val)
3709 if (data->partial_defs.is_empty ())
3710 return data->finish (ao_ref_alias_set (&lhs_ref),
3711 ao_ref_base_alias_set (&lhs_ref), val);
3712 /* This is the only interesting case for partial-def handling
3713 coming from targets that like to gimplify init-ctors as
3714 aggregate copies from constant data like aarch64 for
3715 PR83518. */
3716 if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3718 pd_data pd;
3719 pd.rhs = val;
3720 pd.rhs_off = 0;
3721 pd.offset = 0;
3722 pd.size = maxsizei;
3723 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3724 ao_ref_base_alias_set (&lhs_ref),
3725 0, maxsizei);
3729 /* Continuing with partial defs isn't easily possible here, we
3730 have to find a full def from further lookups from here. Probably
3731 not worth the special-casing everywhere. */
3732 if (!data->partial_defs.is_empty ())
3733 return (void *)-1;
3735 /* Adjust *ref from the new operands. */
3736 ao_ref rhs1_ref;
3737 ao_ref_init (&rhs1_ref, rhs1);
3738 if (!ao_ref_init_from_vn_reference (&r,
3739 force_no_tbaa ? 0
3740 : ao_ref_alias_set (&rhs1_ref),
3741 force_no_tbaa ? 0
3742 : ao_ref_base_alias_set (&rhs1_ref),
3743 vr->type, vr->operands))
3744 return (void *)-1;
3745 /* This can happen with bitfields. */
3746 if (maybe_ne (ref->size, r.size))
3748 /* If the access lacks some subsetting simply apply that by
3749 shortening it. That in the end can only be successful
3750 if we can pun the lookup result which in turn requires
3751 exact offsets. */
3752 if (known_eq (r.size, r.max_size)
3753 && known_lt (ref->size, r.size))
3754 r.size = r.max_size = ref->size;
3755 else
3756 return (void *)-1;
3758 *ref = r;
3760 /* Do not update last seen VUSE after translating. */
3761 data->last_vuse_ptr = NULL;
3762 /* Invalidate the original access path since it now contains
3763 the wrong base. */
3764 data->orig_ref.ref = NULL_TREE;
3765 /* Use the alias-set of this LHS for recording an eventual result. */
3766 if (data->first_set == -2)
3768 data->first_set = ao_ref_alias_set (&lhs_ref);
3769 data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3772 /* Keep looking for the adjusted *REF / VR pair. */
3773 return NULL;
3776 /* 6) For memcpy copies translate the reference through them if the copy
3777 kills ref. But we cannot (easily) do this translation if the memcpy is
3778 a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3779 can modify the storage order of objects (see storage_order_barrier_p). */
3780 else if (data->vn_walk_kind == VN_WALKREWRITE
3781 && is_gimple_reg_type (vr->type)
3782 /* ??? Handle BCOPY as well. */
3783 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3784 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3785 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3786 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3787 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3788 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3789 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3790 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3791 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3792 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3793 && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), &copy_size)
3794 || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3795 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3796 &copy_size)))
3797 /* Handling this is more complicated, give up for now. */
3798 && data->partial_defs.is_empty ())
3800 tree lhs, rhs;
3801 ao_ref r;
3802 poly_int64 rhs_offset, lhs_offset;
3803 vn_reference_op_s op;
3804 poly_uint64 mem_offset;
3805 poly_int64 at, byte_maxsize;
3807 /* Only handle non-variable, addressable refs. */
3808 if (maybe_ne (ref->size, maxsize)
3809 || !multiple_p (offset, BITS_PER_UNIT, &at)
3810 || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3811 return (void *)-1;
3813 /* Extract a pointer base and an offset for the destination. */
3814 lhs = gimple_call_arg (def_stmt, 0);
3815 lhs_offset = 0;
3816 if (TREE_CODE (lhs) == SSA_NAME)
3818 lhs = vn_valueize (lhs);
3819 if (TREE_CODE (lhs) == SSA_NAME)
3821 gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3822 if (gimple_assign_single_p (def_stmt)
3823 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3824 lhs = gimple_assign_rhs1 (def_stmt);
3827 if (TREE_CODE (lhs) == ADDR_EXPR)
3829 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3830 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3831 return (void *)-1;
3832 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3833 &lhs_offset);
3834 if (!tem)
3835 return (void *)-1;
3836 if (TREE_CODE (tem) == MEM_REF
3837 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3839 lhs = TREE_OPERAND (tem, 0);
3840 if (TREE_CODE (lhs) == SSA_NAME)
3841 lhs = vn_valueize (lhs);
3842 lhs_offset += mem_offset;
3844 else if (DECL_P (tem))
3845 lhs = build_fold_addr_expr (tem);
3846 else
3847 return (void *)-1;
3849 if (TREE_CODE (lhs) != SSA_NAME
3850 && TREE_CODE (lhs) != ADDR_EXPR)
3851 return (void *)-1;
3853 /* Extract a pointer base and an offset for the source. */
3854 rhs = gimple_call_arg (def_stmt, 1);
3855 rhs_offset = 0;
3856 if (TREE_CODE (rhs) == SSA_NAME)
3857 rhs = vn_valueize (rhs);
3858 if (TREE_CODE (rhs) == ADDR_EXPR)
3860 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3861 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3862 return (void *)-1;
3863 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3864 &rhs_offset);
3865 if (!tem)
3866 return (void *)-1;
3867 if (TREE_CODE (tem) == MEM_REF
3868 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3870 rhs = TREE_OPERAND (tem, 0);
3871 rhs_offset += mem_offset;
3873 else if (DECL_P (tem)
3874 || TREE_CODE (tem) == STRING_CST)
3875 rhs = build_fold_addr_expr (tem);
3876 else
3877 return (void *)-1;
3879 if (TREE_CODE (rhs) == SSA_NAME)
3880 rhs = SSA_VAL (rhs);
3881 else if (TREE_CODE (rhs) != ADDR_EXPR)
3882 return (void *)-1;
3884 /* The bases of the destination and the references have to agree. */
3885 if (TREE_CODE (base) == MEM_REF)
3887 if (TREE_OPERAND (base, 0) != lhs
3888 || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
3889 return (void *) -1;
3890 at += mem_offset;
3892 else if (!DECL_P (base)
3893 || TREE_CODE (lhs) != ADDR_EXPR
3894 || TREE_OPERAND (lhs, 0) != base)
3895 return (void *)-1;
3897 /* If the access is completely outside of the memcpy destination
3898 area there is no aliasing. */
3899 if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
3900 return NULL;
3901 /* And the access has to be contained within the memcpy destination. */
3902 if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
3903 return (void *)-1;
3905 /* Save the operands since we need to use the original ones for
3906 the hash entry we use. */
3907 if (!data->saved_operands.exists ())
3908 data->saved_operands = vr->operands.copy ();
3910 /* Make room for 2 operands in the new reference. */
3911 if (vr->operands.length () < 2)
3913 vec<vn_reference_op_s> old = vr->operands;
3914 vr->operands.safe_grow_cleared (2, true);
3915 if (old == shared_lookup_references)
3916 shared_lookup_references = vr->operands;
3918 else
3919 vr->operands.truncate (2);
3921 /* The looked-through reference is a simple MEM_REF. */
3922 memset (&op, 0, sizeof (op));
3923 op.type = vr->type;
3924 op.opcode = MEM_REF;
3925 op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3926 op.off = at - lhs_offset + rhs_offset;
3927 vr->operands[0] = op;
3928 op.type = TREE_TYPE (rhs);
3929 op.opcode = TREE_CODE (rhs);
3930 op.op0 = rhs;
3931 op.off = -1;
3932 vr->operands[1] = op;
3933 vr->hashcode = vn_reference_compute_hash (vr);
3935 /* Try folding the new reference to a constant. */
3936 tree val = fully_constant_vn_reference_p (vr);
3937 if (val)
3938 return data->finish (0, 0, val);
3940 /* Adjust *ref from the new operands. */
3941 if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
3942 return (void *)-1;
3943 /* This can happen with bitfields. */
3944 if (maybe_ne (ref->size, r.size))
3945 return (void *)-1;
3946 *ref = r;
3948 /* Do not update last seen VUSE after translating. */
3949 data->last_vuse_ptr = NULL;
3950 /* Invalidate the original access path since it now contains
3951 the wrong base. */
3952 data->orig_ref.ref = NULL_TREE;
3953 /* Use the alias-set of this stmt for recording an eventual result. */
3954 if (data->first_set == -2)
3956 data->first_set = 0;
3957 data->first_base_set = 0;
3960 /* Keep looking for the adjusted *REF / VR pair. */
3961 return NULL;
3964 /* Bail out and stop walking. */
3965 return (void *)-1;
3968 /* Return a reference op vector from OP that can be used for
3969 vn_reference_lookup_pieces. The caller is responsible for releasing
3970 the vector. */
3972 vec<vn_reference_op_s>
3973 vn_reference_operands_for_lookup (tree op)
3975 bool valueized;
3976 return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
3979 /* Lookup a reference operation by it's parts, in the current hash table.
3980 Returns the resulting value number if it exists in the hash table,
3981 NULL_TREE otherwise. VNRESULT will be filled in with the actual
3982 vn_reference_t stored in the hashtable if something is found. */
3984 tree
3985 vn_reference_lookup_pieces (tree vuse, alias_set_type set,
3986 alias_set_type base_set, tree type,
3987 vec<vn_reference_op_s> operands,
3988 vn_reference_t *vnresult, vn_lookup_kind kind)
3990 struct vn_reference_s vr1;
3991 vn_reference_t tmp;
3992 tree cst;
3994 if (!vnresult)
3995 vnresult = &tmp;
3996 *vnresult = NULL;
3998 vr1.vuse = vuse_ssa_val (vuse);
3999 shared_lookup_references.truncate (0);
4000 shared_lookup_references.safe_grow (operands.length (), true);
4001 memcpy (shared_lookup_references.address (),
4002 operands.address (),
4003 sizeof (vn_reference_op_s)
4004 * operands.length ());
4005 bool valueized_p;
4006 valueize_refs_1 (&shared_lookup_references, &valueized_p);
4007 vr1.operands = shared_lookup_references;
4008 vr1.type = type;
4009 vr1.set = set;
4010 vr1.base_set = base_set;
4011 vr1.hashcode = vn_reference_compute_hash (&vr1);
4012 if ((cst = fully_constant_vn_reference_p (&vr1)))
4013 return cst;
4015 vn_reference_lookup_1 (&vr1, vnresult);
4016 if (!*vnresult
4017 && kind != VN_NOWALK
4018 && vr1.vuse)
4020 ao_ref r;
4021 unsigned limit = param_sccvn_max_alias_queries_per_access;
4022 vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE,
4023 false);
4024 vec<vn_reference_op_s> ops_for_ref;
4025 if (!valueized_p)
4026 ops_for_ref = vr1.operands;
4027 else
4029 /* For ao_ref_from_mem we have to ensure only available SSA names
4030 end up in base and the only convenient way to make this work
4031 for PRE is to re-valueize with that in mind. */
4032 ops_for_ref.create (operands.length ());
4033 ops_for_ref.quick_grow (operands.length ());
4034 memcpy (ops_for_ref.address (),
4035 operands.address (),
4036 sizeof (vn_reference_op_s)
4037 * operands.length ());
4038 valueize_refs_1 (&ops_for_ref, &valueized_p, true);
4040 if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
4041 ops_for_ref))
4042 *vnresult
4043 = ((vn_reference_t)
4044 walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
4045 vn_reference_lookup_3, vuse_valueize,
4046 limit, &data));
4047 if (ops_for_ref != shared_lookup_references)
4048 ops_for_ref.release ();
4049 gcc_checking_assert (vr1.operands == shared_lookup_references);
4050 if (*vnresult
4051 && data.same_val
4052 && (!(*vnresult)->result
4053 || !operand_equal_p ((*vnresult)->result, data.same_val)))
4055 *vnresult = NULL;
4056 return NULL_TREE;
4060 if (*vnresult)
4061 return (*vnresult)->result;
4063 return NULL_TREE;
4066 /* Lookup OP in the current hash table, and return the resulting value
4067 number if it exists in the hash table. Return NULL_TREE if it does
4068 not exist in the hash table or if the result field of the structure
4069 was NULL.. VNRESULT will be filled in with the vn_reference_t
4070 stored in the hashtable if one exists. When TBAA_P is false assume
4071 we are looking up a store and treat it as having alias-set zero.
4072 *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
4073 MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
4074 load is bitwise anded with MASK and so we are only interested in a subset
4075 of the bits and can ignore if the other bits are uninitialized or
4076 not initialized with constants. When doing redundant store removal
4077 the caller has to set REDUNDANT_STORE_REMOVAL_P. */
4079 tree
4080 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
4081 vn_reference_t *vnresult, bool tbaa_p,
4082 tree *last_vuse_ptr, tree mask,
4083 bool redundant_store_removal_p)
4085 vec<vn_reference_op_s> operands;
4086 struct vn_reference_s vr1;
4087 bool valueized_anything;
4089 if (vnresult)
4090 *vnresult = NULL;
4092 vr1.vuse = vuse_ssa_val (vuse);
4093 vr1.operands = operands
4094 = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
4096 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing
4097 this before the pass folding __builtin_object_size had a chance to run. */
4098 if ((cfun->curr_properties & PROP_objsz)
4099 && operands[0].opcode == ADDR_EXPR
4100 && operands.last ().opcode == SSA_NAME)
4102 poly_int64 off = 0;
4103 vn_reference_op_t vro;
4104 unsigned i;
4105 for (i = 1; operands.iterate (i, &vro); ++i)
4107 if (vro->opcode == SSA_NAME)
4108 break;
4109 else if (known_eq (vro->off, -1))
4110 break;
4111 off += vro->off;
4113 if (i == operands.length () - 1
4114 /* Make sure we the offset we accumulated in a 64bit int
4115 fits the address computation carried out in target
4116 offset precision. */
4117 && (off.coeffs[0]
4118 == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4120 gcc_assert (operands[i-1].opcode == MEM_REF);
4121 tree ops[2];
4122 ops[0] = operands[i].op0;
4123 ops[1] = wide_int_to_tree (sizetype, off);
4124 tree res = vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4125 TREE_TYPE (op), ops, NULL);
4126 if (res)
4127 return res;
4128 return NULL_TREE;
4132 vr1.type = TREE_TYPE (op);
4133 ao_ref op_ref;
4134 ao_ref_init (&op_ref, op);
4135 vr1.set = ao_ref_alias_set (&op_ref);
4136 vr1.base_set = ao_ref_base_alias_set (&op_ref);
4137 vr1.hashcode = vn_reference_compute_hash (&vr1);
4138 if (mask == NULL_TREE)
4139 if (tree cst = fully_constant_vn_reference_p (&vr1))
4140 return cst;
4142 if (kind != VN_NOWALK && vr1.vuse)
4144 vn_reference_t wvnresult;
4145 ao_ref r;
4146 unsigned limit = param_sccvn_max_alias_queries_per_access;
4147 auto_vec<vn_reference_op_s> ops_for_ref;
4148 if (valueized_anything)
4150 copy_reference_ops_from_ref (op, &ops_for_ref);
4151 bool tem;
4152 valueize_refs_1 (&ops_for_ref, &tem, true);
4154 /* Make sure to use a valueized reference if we valueized anything.
4155 Otherwise preserve the full reference for advanced TBAA. */
4156 if (!valueized_anything
4157 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
4158 vr1.type, ops_for_ref))
4159 ao_ref_init (&r, op);
4160 vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
4161 last_vuse_ptr, kind, tbaa_p, mask,
4162 redundant_store_removal_p);
4164 wvnresult
4165 = ((vn_reference_t)
4166 walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
4167 vn_reference_lookup_3, vuse_valueize, limit,
4168 &data));
4169 gcc_checking_assert (vr1.operands == shared_lookup_references);
4170 if (wvnresult)
4172 gcc_assert (mask == NULL_TREE);
4173 if (data.same_val
4174 && (!wvnresult->result
4175 || !operand_equal_p (wvnresult->result, data.same_val)))
4176 return NULL_TREE;
4177 if (vnresult)
4178 *vnresult = wvnresult;
4179 return wvnresult->result;
4181 else if (mask)
4182 return data.masked_result;
4184 return NULL_TREE;
4187 if (last_vuse_ptr)
4188 *last_vuse_ptr = vr1.vuse;
4189 if (mask)
4190 return NULL_TREE;
4191 return vn_reference_lookup_1 (&vr1, vnresult);
4194 /* Lookup CALL in the current hash table and return the entry in
4195 *VNRESULT if found. Populates *VR for the hashtable lookup. */
4197 void
4198 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
4199 vn_reference_t vr)
4201 if (vnresult)
4202 *vnresult = NULL;
4204 tree vuse = gimple_vuse (call);
4206 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
4207 vr->operands = valueize_shared_reference_ops_from_call (call);
4208 tree lhs = gimple_call_lhs (call);
4209 /* For non-SSA return values the referece ops contain the LHS. */
4210 vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
4211 ? TREE_TYPE (lhs) : NULL_TREE);
4212 vr->punned = false;
4213 vr->set = 0;
4214 vr->base_set = 0;
4215 vr->hashcode = vn_reference_compute_hash (vr);
4216 vn_reference_lookup_1 (vr, vnresult);
4219 /* Insert OP into the current hash table with a value number of RESULT. */
4221 static void
4222 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
4224 vn_reference_s **slot;
4225 vn_reference_t vr1;
4226 bool tem;
4228 vec<vn_reference_op_s> operands
4229 = valueize_shared_reference_ops_from_ref (op, &tem);
4230 /* Handle &MEM[ptr + 5].b[1].c as POINTER_PLUS_EXPR. Avoid doing this
4231 before the pass folding __builtin_object_size had a chance to run. */
4232 if ((cfun->curr_properties & PROP_objsz)
4233 && operands[0].opcode == ADDR_EXPR
4234 && operands.last ().opcode == SSA_NAME)
4236 poly_int64 off = 0;
4237 vn_reference_op_t vro;
4238 unsigned i;
4239 for (i = 1; operands.iterate (i, &vro); ++i)
4241 if (vro->opcode == SSA_NAME)
4242 break;
4243 else if (known_eq (vro->off, -1))
4244 break;
4245 off += vro->off;
4247 if (i == operands.length () - 1
4248 /* Make sure we the offset we accumulated in a 64bit int
4249 fits the address computation carried out in target
4250 offset precision. */
4251 && (off.coeffs[0]
4252 == sext_hwi (off.coeffs[0], TYPE_PRECISION (sizetype))))
4254 gcc_assert (operands[i-1].opcode == MEM_REF);
4255 tree ops[2];
4256 ops[0] = operands[i].op0;
4257 ops[1] = wide_int_to_tree (sizetype, off);
4258 vn_nary_op_insert_pieces (2, POINTER_PLUS_EXPR,
4259 TREE_TYPE (op), ops, result,
4260 VN_INFO (result)->value_id);
4261 return;
4265 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4266 if (TREE_CODE (result) == SSA_NAME)
4267 vr1->value_id = VN_INFO (result)->value_id;
4268 else
4269 vr1->value_id = get_or_alloc_constant_value_id (result);
4270 vr1->vuse = vuse_ssa_val (vuse);
4271 vr1->operands = operands.copy ();
4272 vr1->type = TREE_TYPE (op);
4273 vr1->punned = false;
4274 ao_ref op_ref;
4275 ao_ref_init (&op_ref, op);
4276 vr1->set = ao_ref_alias_set (&op_ref);
4277 vr1->base_set = ao_ref_base_alias_set (&op_ref);
4278 vr1->hashcode = vn_reference_compute_hash (vr1);
4279 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4280 vr1->result_vdef = vdef;
4282 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4283 INSERT);
4285 /* Because IL walking on reference lookup can end up visiting
4286 a def that is only to be visited later in iteration order
4287 when we are about to make an irreducible region reducible
4288 the def can be effectively processed and its ref being inserted
4289 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
4290 but save a lookup if we deal with already inserted refs here. */
4291 if (*slot)
4293 /* We cannot assert that we have the same value either because
4294 when disentangling an irreducible region we may end up visiting
4295 a use before the corresponding def. That's a missed optimization
4296 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
4297 if (dump_file && (dump_flags & TDF_DETAILS)
4298 && !operand_equal_p ((*slot)->result, vr1->result, 0))
4300 fprintf (dump_file, "Keeping old value ");
4301 print_generic_expr (dump_file, (*slot)->result);
4302 fprintf (dump_file, " because of collision\n");
4304 free_reference (vr1);
4305 obstack_free (&vn_tables_obstack, vr1);
4306 return;
4309 *slot = vr1;
4310 vr1->next = last_inserted_ref;
4311 last_inserted_ref = vr1;
4314 /* Insert a reference by it's pieces into the current hash table with
4315 a value number of RESULT. Return the resulting reference
4316 structure we created. */
4318 vn_reference_t
4319 vn_reference_insert_pieces (tree vuse, alias_set_type set,
4320 alias_set_type base_set, tree type,
4321 vec<vn_reference_op_s> operands,
4322 tree result, unsigned int value_id)
4325 vn_reference_s **slot;
4326 vn_reference_t vr1;
4328 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4329 vr1->value_id = value_id;
4330 vr1->vuse = vuse_ssa_val (vuse);
4331 vr1->operands = operands;
4332 valueize_refs (&vr1->operands);
4333 vr1->type = type;
4334 vr1->punned = false;
4335 vr1->set = set;
4336 vr1->base_set = base_set;
4337 vr1->hashcode = vn_reference_compute_hash (vr1);
4338 if (result && TREE_CODE (result) == SSA_NAME)
4339 result = SSA_VAL (result);
4340 vr1->result = result;
4341 vr1->result_vdef = NULL_TREE;
4343 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
4344 INSERT);
4346 /* At this point we should have all the things inserted that we have
4347 seen before, and we should never try inserting something that
4348 already exists. */
4349 gcc_assert (!*slot);
4351 *slot = vr1;
4352 vr1->next = last_inserted_ref;
4353 last_inserted_ref = vr1;
4354 return vr1;
4357 /* Compute and return the hash value for nary operation VBO1. */
4359 hashval_t
4360 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
4362 inchash::hash hstate;
4363 unsigned i;
4365 if (((vno1->length == 2
4366 && commutative_tree_code (vno1->opcode))
4367 || (vno1->length == 3
4368 && commutative_ternary_tree_code (vno1->opcode)))
4369 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4370 std::swap (vno1->op[0], vno1->op[1]);
4371 else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
4372 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
4374 std::swap (vno1->op[0], vno1->op[1]);
4375 vno1->opcode = swap_tree_comparison (vno1->opcode);
4378 hstate.add_int (vno1->opcode);
4379 for (i = 0; i < vno1->length; ++i)
4380 inchash::add_expr (vno1->op[i], hstate);
4382 return hstate.end ();
4385 /* Compare nary operations VNO1 and VNO2 and return true if they are
4386 equivalent. */
4388 bool
4389 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
4391 unsigned i;
4393 if (vno1->hashcode != vno2->hashcode)
4394 return false;
4396 if (vno1->length != vno2->length)
4397 return false;
4399 if (vno1->opcode != vno2->opcode
4400 || !types_compatible_p (vno1->type, vno2->type))
4401 return false;
4403 for (i = 0; i < vno1->length; ++i)
4404 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
4405 return false;
4407 /* BIT_INSERT_EXPR has an implict operand as the type precision
4408 of op1. Need to check to make sure they are the same. */
4409 if (vno1->opcode == BIT_INSERT_EXPR
4410 && TREE_CODE (vno1->op[1]) == INTEGER_CST
4411 && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
4412 != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
4413 return false;
4415 return true;
4418 /* Initialize VNO from the pieces provided. */
4420 static void
4421 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
4422 enum tree_code code, tree type, tree *ops)
4424 vno->opcode = code;
4425 vno->length = length;
4426 vno->type = type;
4427 memcpy (&vno->op[0], ops, sizeof (tree) * length);
4430 /* Return the number of operands for a vn_nary ops structure from STMT. */
4432 unsigned int
4433 vn_nary_length_from_stmt (gimple *stmt)
4435 switch (gimple_assign_rhs_code (stmt))
4437 case REALPART_EXPR:
4438 case IMAGPART_EXPR:
4439 case VIEW_CONVERT_EXPR:
4440 return 1;
4442 case BIT_FIELD_REF:
4443 return 3;
4445 case CONSTRUCTOR:
4446 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4448 default:
4449 return gimple_num_ops (stmt) - 1;
4453 /* Initialize VNO from STMT. */
4455 void
4456 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
4458 unsigned i;
4460 vno->opcode = gimple_assign_rhs_code (stmt);
4461 vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
4462 switch (vno->opcode)
4464 case REALPART_EXPR:
4465 case IMAGPART_EXPR:
4466 case VIEW_CONVERT_EXPR:
4467 vno->length = 1;
4468 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4469 break;
4471 case BIT_FIELD_REF:
4472 vno->length = 3;
4473 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
4474 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
4475 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
4476 break;
4478 case CONSTRUCTOR:
4479 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
4480 for (i = 0; i < vno->length; ++i)
4481 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
4482 break;
4484 default:
4485 gcc_checking_assert (!gimple_assign_single_p (stmt));
4486 vno->length = gimple_num_ops (stmt) - 1;
4487 for (i = 0; i < vno->length; ++i)
4488 vno->op[i] = gimple_op (stmt, i + 1);
4492 /* Compute the hashcode for VNO and look for it in the hash table;
4493 return the resulting value number if it exists in the hash table.
4494 Return NULL_TREE if it does not exist in the hash table or if the
4495 result field of the operation is NULL. VNRESULT will contain the
4496 vn_nary_op_t from the hashtable if it exists. */
4498 static tree
4499 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
4501 vn_nary_op_s **slot;
4503 if (vnresult)
4504 *vnresult = NULL;
4506 for (unsigned i = 0; i < vno->length; ++i)
4507 if (TREE_CODE (vno->op[i]) == SSA_NAME)
4508 vno->op[i] = SSA_VAL (vno->op[i]);
4510 vno->hashcode = vn_nary_op_compute_hash (vno);
4511 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
4512 if (!slot)
4513 return NULL_TREE;
4514 if (vnresult)
4515 *vnresult = *slot;
4516 return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
4519 /* Lookup a n-ary operation by its pieces and return the resulting value
4520 number if it exists in the hash table. Return NULL_TREE if it does
4521 not exist in the hash table or if the result field of the operation
4522 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
4523 if it exists. */
4525 tree
4526 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4527 tree type, tree *ops, vn_nary_op_t *vnresult)
4529 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4530 sizeof_vn_nary_op (length));
4531 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4532 return vn_nary_op_lookup_1 (vno1, vnresult);
4535 /* Lookup the rhs of STMT in the current hash table, and return the resulting
4536 value number if it exists in the hash table. Return NULL_TREE if
4537 it does not exist in the hash table. VNRESULT will contain the
4538 vn_nary_op_t from the hashtable if it exists. */
4540 tree
4541 vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4543 vn_nary_op_t vno1
4544 = XALLOCAVAR (struct vn_nary_op_s,
4545 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4546 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4547 return vn_nary_op_lookup_1 (vno1, vnresult);
4550 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4552 vn_nary_op_t
4553 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4555 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4558 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4559 obstack. */
4561 static vn_nary_op_t
4562 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4564 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4566 vno1->value_id = value_id;
4567 vno1->length = length;
4568 vno1->predicated_values = 0;
4569 vno1->u.result = result;
4571 return vno1;
4574 /* Insert VNO into TABLE. */
4576 static vn_nary_op_t
4577 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table)
4579 vn_nary_op_s **slot;
4581 gcc_assert (! vno->predicated_values
4582 || (! vno->u.values->next
4583 && vno->u.values->n == 1));
4585 for (unsigned i = 0; i < vno->length; ++i)
4586 if (TREE_CODE (vno->op[i]) == SSA_NAME)
4587 vno->op[i] = SSA_VAL (vno->op[i]);
4589 vno->hashcode = vn_nary_op_compute_hash (vno);
4590 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4591 vno->unwind_to = *slot;
4592 if (*slot)
4594 /* Prefer non-predicated values.
4595 ??? Only if those are constant, otherwise, with constant predicated
4596 value, turn them into predicated values with entry-block validity
4597 (??? but we always find the first valid result currently). */
4598 if ((*slot)->predicated_values
4599 && ! vno->predicated_values)
4601 /* ??? We cannot remove *slot from the unwind stack list.
4602 For the moment we deal with this by skipping not found
4603 entries but this isn't ideal ... */
4604 *slot = vno;
4605 /* ??? Maintain a stack of states we can unwind in
4606 vn_nary_op_s? But how far do we unwind? In reality
4607 we need to push change records somewhere... Or not
4608 unwind vn_nary_op_s and linking them but instead
4609 unwind the results "list", linking that, which also
4610 doesn't move on hashtable resize. */
4611 /* We can also have a ->unwind_to recording *slot there.
4612 That way we can make u.values a fixed size array with
4613 recording the number of entries but of course we then
4614 have always N copies for each unwind_to-state. Or we
4615 make sure to only ever append and each unwinding will
4616 pop off one entry (but how to deal with predicated
4617 replaced with non-predicated here?) */
4618 vno->next = last_inserted_nary;
4619 last_inserted_nary = vno;
4620 return vno;
4622 else if (vno->predicated_values
4623 && ! (*slot)->predicated_values)
4624 return *slot;
4625 else if (vno->predicated_values
4626 && (*slot)->predicated_values)
4628 /* ??? Factor this all into a insert_single_predicated_value
4629 routine. */
4630 gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4631 basic_block vno_bb
4632 = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4633 vn_pval *nval = vno->u.values;
4634 vn_pval **next = &vno->u.values;
4635 bool found = false;
4636 for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4638 if (expressions_equal_p (val->result, nval->result))
4640 found = true;
4641 for (unsigned i = 0; i < val->n; ++i)
4643 basic_block val_bb
4644 = BASIC_BLOCK_FOR_FN (cfun,
4645 val->valid_dominated_by_p[i]);
4646 if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4647 /* Value registered with more generic predicate. */
4648 return *slot;
4649 else if (flag_checking)
4650 /* Shouldn't happen, we insert in RPO order. */
4651 gcc_assert (!dominated_by_p (CDI_DOMINATORS,
4652 val_bb, vno_bb));
4654 /* Append value. */
4655 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4656 sizeof (vn_pval)
4657 + val->n * sizeof (int));
4658 (*next)->next = NULL;
4659 (*next)->result = val->result;
4660 (*next)->n = val->n + 1;
4661 memcpy ((*next)->valid_dominated_by_p,
4662 val->valid_dominated_by_p,
4663 val->n * sizeof (int));
4664 (*next)->valid_dominated_by_p[val->n] = vno_bb->index;
4665 next = &(*next)->next;
4666 if (dump_file && (dump_flags & TDF_DETAILS))
4667 fprintf (dump_file, "Appending predicate to value.\n");
4668 continue;
4670 /* Copy other predicated values. */
4671 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4672 sizeof (vn_pval)
4673 + (val->n-1) * sizeof (int));
4674 memcpy (*next, val, sizeof (vn_pval) + (val->n-1) * sizeof (int));
4675 (*next)->next = NULL;
4676 next = &(*next)->next;
4678 if (!found)
4679 *next = nval;
4681 *slot = vno;
4682 vno->next = last_inserted_nary;
4683 last_inserted_nary = vno;
4684 return vno;
4687 /* While we do not want to insert things twice it's awkward to
4688 avoid it in the case where visit_nary_op pattern-matches stuff
4689 and ends up simplifying the replacement to itself. We then
4690 get two inserts, one from visit_nary_op and one from
4691 vn_nary_build_or_lookup.
4692 So allow inserts with the same value number. */
4693 if ((*slot)->u.result == vno->u.result)
4694 return *slot;
4697 /* ??? There's also optimistic vs. previous commited state merging
4698 that is problematic for the case of unwinding. */
4700 /* ??? We should return NULL if we do not use 'vno' and have the
4701 caller release it. */
4702 gcc_assert (!*slot);
4704 *slot = vno;
4705 vno->next = last_inserted_nary;
4706 last_inserted_nary = vno;
4707 return vno;
4710 /* Insert a n-ary operation into the current hash table using it's
4711 pieces. Return the vn_nary_op_t structure we created and put in
4712 the hashtable. */
4714 vn_nary_op_t
4715 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4716 tree type, tree *ops,
4717 tree result, unsigned int value_id)
4719 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4720 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4721 return vn_nary_op_insert_into (vno1, valid_info->nary);
4724 /* Return whether we can track a predicate valid when PRED_E is executed. */
4726 static bool
4727 can_track_predicate_on_edge (edge pred_e)
4729 /* ??? As we are currently recording the destination basic-block index in
4730 vn_pval.valid_dominated_by_p and using dominance for the
4731 validity check we cannot track predicates on all edges. */
4732 if (single_pred_p (pred_e->dest))
4733 return true;
4734 /* Never record for backedges. */
4735 if (pred_e->flags & EDGE_DFS_BACK)
4736 return false;
4737 /* When there's more than one predecessor we cannot track
4738 predicate validity based on the destination block. The
4739 exception is when all other incoming edges sources are
4740 dominated by the destination block. */
4741 edge_iterator ei;
4742 edge e;
4743 FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4744 if (e != pred_e && ! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4745 return false;
4746 return true;
4749 static vn_nary_op_t
4750 vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4751 tree type, tree *ops,
4752 tree result, unsigned int value_id,
4753 edge pred_e)
4755 gcc_assert (can_track_predicate_on_edge (pred_e));
4757 if (dump_file && (dump_flags & TDF_DETAILS)
4758 /* ??? Fix dumping, but currently we only get comparisons. */
4759 && TREE_CODE_CLASS (code) == tcc_comparison)
4761 fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4762 pred_e->dest->index);
4763 print_generic_expr (dump_file, ops[0], TDF_SLIM);
4764 fprintf (dump_file, " %s ", get_tree_code_name (code));
4765 print_generic_expr (dump_file, ops[1], TDF_SLIM);
4766 fprintf (dump_file, " == %s\n",
4767 integer_zerop (result) ? "false" : "true");
4769 vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4770 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4771 vno1->predicated_values = 1;
4772 vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4773 sizeof (vn_pval));
4774 vno1->u.values->next = NULL;
4775 vno1->u.values->result = result;
4776 vno1->u.values->n = 1;
4777 vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4778 return vn_nary_op_insert_into (vno1, valid_info->nary);
4781 static bool
4782 dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4784 static tree
4785 vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb,
4786 edge e = NULL)
4788 if (! vno->predicated_values)
4789 return vno->u.result;
4790 for (vn_pval *val = vno->u.values; val; val = val->next)
4791 for (unsigned i = 0; i < val->n; ++i)
4793 basic_block cand
4794 = BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]);
4795 /* Do not handle backedge executability optimistically since
4796 when figuring out whether to iterate we do not consider
4797 changed predication.
4798 When asking for predicated values on an edge avoid looking
4799 at edge executability for edges forward in our iteration
4800 as well. */
4801 if (e && (e->flags & EDGE_DFS_BACK))
4803 if (dominated_by_p (CDI_DOMINATORS, bb, cand))
4804 return val->result;
4806 else if (dominated_by_p_w_unex (bb, cand, false))
4807 return val->result;
4809 return NULL_TREE;
4812 static tree
4813 vn_nary_op_get_predicated_value (vn_nary_op_t vno, edge e)
4815 return vn_nary_op_get_predicated_value (vno, e->src, e);
4818 /* Insert the rhs of STMT into the current hash table with a value number of
4819 RESULT. */
4821 static vn_nary_op_t
4822 vn_nary_op_insert_stmt (gimple *stmt, tree result)
4824 vn_nary_op_t vno1
4825 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
4826 result, VN_INFO (result)->value_id);
4827 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4828 return vn_nary_op_insert_into (vno1, valid_info->nary);
4831 /* Compute a hashcode for PHI operation VP1 and return it. */
4833 static inline hashval_t
4834 vn_phi_compute_hash (vn_phi_t vp1)
4836 inchash::hash hstate;
4837 tree phi1op;
4838 tree type;
4839 edge e;
4840 edge_iterator ei;
4842 hstate.add_int (EDGE_COUNT (vp1->block->preds));
4843 switch (EDGE_COUNT (vp1->block->preds))
4845 case 1:
4846 break;
4847 case 2:
4848 /* When this is a PHI node subject to CSE for different blocks
4849 avoid hashing the block index. */
4850 if (vp1->cclhs)
4851 break;
4852 /* Fallthru. */
4853 default:
4854 hstate.add_int (vp1->block->index);
4857 /* If all PHI arguments are constants we need to distinguish
4858 the PHI node via its type. */
4859 type = vp1->type;
4860 hstate.merge_hash (vn_hash_type (type));
4862 FOR_EACH_EDGE (e, ei, vp1->block->preds)
4864 /* Don't hash backedge values they need to be handled as VN_TOP
4865 for optimistic value-numbering. */
4866 if (e->flags & EDGE_DFS_BACK)
4867 continue;
4869 phi1op = vp1->phiargs[e->dest_idx];
4870 if (phi1op == VN_TOP)
4871 continue;
4872 inchash::add_expr (phi1op, hstate);
4875 return hstate.end ();
4879 /* Return true if COND1 and COND2 represent the same condition, set
4880 *INVERTED_P if one needs to be inverted to make it the same as
4881 the other. */
4883 static bool
4884 cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
4885 gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
4887 enum tree_code code1 = gimple_cond_code (cond1);
4888 enum tree_code code2 = gimple_cond_code (cond2);
4890 *inverted_p = false;
4891 if (code1 == code2)
4893 else if (code1 == swap_tree_comparison (code2))
4894 std::swap (lhs2, rhs2);
4895 else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
4896 *inverted_p = true;
4897 else if (code1 == invert_tree_comparison
4898 (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
4900 std::swap (lhs2, rhs2);
4901 *inverted_p = true;
4903 else
4904 return false;
4906 return ((expressions_equal_p (lhs1, lhs2)
4907 && expressions_equal_p (rhs1, rhs2))
4908 || (commutative_tree_code (code1)
4909 && expressions_equal_p (lhs1, rhs2)
4910 && expressions_equal_p (rhs1, lhs2)));
4913 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4915 static int
4916 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
4918 if (vp1->hashcode != vp2->hashcode)
4919 return false;
4921 if (vp1->block != vp2->block)
4923 if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
4924 return false;
4926 switch (EDGE_COUNT (vp1->block->preds))
4928 case 1:
4929 /* Single-arg PHIs are just copies. */
4930 break;
4932 case 2:
4934 /* Make sure both PHIs are classified as CSEable. */
4935 if (! vp1->cclhs || ! vp2->cclhs)
4936 return false;
4938 /* Rule out backedges into the PHI. */
4939 gcc_checking_assert
4940 (vp1->block->loop_father->header != vp1->block
4941 && vp2->block->loop_father->header != vp2->block);
4943 /* If the PHI nodes do not have compatible types
4944 they are not the same. */
4945 if (!types_compatible_p (vp1->type, vp2->type))
4946 return false;
4948 /* If the immediate dominator end in switch stmts multiple
4949 values may end up in the same PHI arg via intermediate
4950 CFG merges. */
4951 basic_block idom1
4952 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4953 basic_block idom2
4954 = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
4955 gcc_checking_assert (EDGE_COUNT (idom1->succs) == 2
4956 && EDGE_COUNT (idom2->succs) == 2);
4958 /* Verify the controlling stmt is the same. */
4959 gcond *last1 = as_a <gcond *> (*gsi_last_bb (idom1));
4960 gcond *last2 = as_a <gcond *> (*gsi_last_bb (idom2));
4961 bool inverted_p;
4962 if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
4963 last2, vp2->cclhs, vp2->ccrhs,
4964 &inverted_p))
4965 return false;
4967 /* Get at true/false controlled edges into the PHI. */
4968 edge te1, te2, fe1, fe2;
4969 if (! extract_true_false_controlled_edges (idom1, vp1->block,
4970 &te1, &fe1)
4971 || ! extract_true_false_controlled_edges (idom2, vp2->block,
4972 &te2, &fe2))
4973 return false;
4975 /* Swap edges if the second condition is the inverted of the
4976 first. */
4977 if (inverted_p)
4978 std::swap (te2, fe2);
4980 /* Since we do not know which edge will be executed we have
4981 to be careful when matching VN_TOP. Be conservative and
4982 only match VN_TOP == VN_TOP for now, we could allow
4983 VN_TOP on the not prevailing PHI though. See for example
4984 PR102920. */
4985 if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
4986 vp2->phiargs[te2->dest_idx], false)
4987 || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
4988 vp2->phiargs[fe2->dest_idx], false))
4989 return false;
4991 return true;
4994 default:
4995 return false;
4999 /* If the PHI nodes do not have compatible types
5000 they are not the same. */
5001 if (!types_compatible_p (vp1->type, vp2->type))
5002 return false;
5004 /* Any phi in the same block will have it's arguments in the
5005 same edge order, because of how we store phi nodes. */
5006 unsigned nargs = EDGE_COUNT (vp1->block->preds);
5007 for (unsigned i = 0; i < nargs; ++i)
5009 tree phi1op = vp1->phiargs[i];
5010 tree phi2op = vp2->phiargs[i];
5011 if (phi1op == phi2op)
5012 continue;
5013 if (!expressions_equal_p (phi1op, phi2op, false))
5014 return false;
5017 return true;
5020 /* Lookup PHI in the current hash table, and return the resulting
5021 value number if it exists in the hash table. Return NULL_TREE if
5022 it does not exist in the hash table. */
5024 static tree
5025 vn_phi_lookup (gimple *phi, bool backedges_varying_p)
5027 vn_phi_s **slot;
5028 struct vn_phi_s *vp1;
5029 edge e;
5030 edge_iterator ei;
5032 vp1 = XALLOCAVAR (struct vn_phi_s,
5033 sizeof (struct vn_phi_s)
5034 + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
5036 /* Canonicalize the SSA_NAME's to their value number. */
5037 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5039 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5040 if (TREE_CODE (def) == SSA_NAME
5041 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5043 if (!virtual_operand_p (def)
5044 && ssa_undefined_value_p (def, false))
5045 def = VN_TOP;
5046 else
5047 def = SSA_VAL (def);
5049 vp1->phiargs[e->dest_idx] = def;
5051 vp1->type = TREE_TYPE (gimple_phi_result (phi));
5052 vp1->block = gimple_bb (phi);
5053 /* Extract values of the controlling condition. */
5054 vp1->cclhs = NULL_TREE;
5055 vp1->ccrhs = NULL_TREE;
5056 if (EDGE_COUNT (vp1->block->preds) == 2
5057 && vp1->block->loop_father->header != vp1->block)
5059 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5060 if (EDGE_COUNT (idom1->succs) == 2)
5061 if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5063 /* ??? We want to use SSA_VAL here. But possibly not
5064 allow VN_TOP. */
5065 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5066 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5069 vp1->hashcode = vn_phi_compute_hash (vp1);
5070 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
5071 if (!slot)
5072 return NULL_TREE;
5073 return (*slot)->result;
5076 /* Insert PHI into the current hash table with a value number of
5077 RESULT. */
5079 static vn_phi_t
5080 vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
5082 vn_phi_s **slot;
5083 vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
5084 sizeof (vn_phi_s)
5085 + ((gimple_phi_num_args (phi) - 1)
5086 * sizeof (tree)));
5087 edge e;
5088 edge_iterator ei;
5090 /* Canonicalize the SSA_NAME's to their value number. */
5091 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5093 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5094 if (TREE_CODE (def) == SSA_NAME
5095 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
5097 if (!virtual_operand_p (def)
5098 && ssa_undefined_value_p (def, false))
5099 def = VN_TOP;
5100 else
5101 def = SSA_VAL (def);
5103 vp1->phiargs[e->dest_idx] = def;
5105 vp1->value_id = VN_INFO (result)->value_id;
5106 vp1->type = TREE_TYPE (gimple_phi_result (phi));
5107 vp1->block = gimple_bb (phi);
5108 /* Extract values of the controlling condition. */
5109 vp1->cclhs = NULL_TREE;
5110 vp1->ccrhs = NULL_TREE;
5111 if (EDGE_COUNT (vp1->block->preds) == 2
5112 && vp1->block->loop_father->header != vp1->block)
5114 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
5115 if (EDGE_COUNT (idom1->succs) == 2)
5116 if (gcond *last1 = safe_dyn_cast <gcond *> (*gsi_last_bb (idom1)))
5118 /* ??? We want to use SSA_VAL here. But possibly not
5119 allow VN_TOP. */
5120 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
5121 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
5124 vp1->result = result;
5125 vp1->hashcode = vn_phi_compute_hash (vp1);
5127 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
5128 gcc_assert (!*slot);
5130 *slot = vp1;
5131 vp1->next = last_inserted_phi;
5132 last_inserted_phi = vp1;
5133 return vp1;
5137 /* Return true if BB1 is dominated by BB2 taking into account edges
5138 that are not executable. When ALLOW_BACK is false consider not
5139 executable backedges as executable. */
5141 static bool
5142 dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
5144 edge_iterator ei;
5145 edge e;
5147 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5148 return true;
5150 /* Before iterating we'd like to know if there exists a
5151 (executable) path from bb2 to bb1 at all, if not we can
5152 directly return false. For now simply iterate once. */
5154 /* Iterate to the single executable bb1 predecessor. */
5155 if (EDGE_COUNT (bb1->preds) > 1)
5157 edge prede = NULL;
5158 FOR_EACH_EDGE (e, ei, bb1->preds)
5159 if ((e->flags & EDGE_EXECUTABLE)
5160 || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5162 if (prede)
5164 prede = NULL;
5165 break;
5167 prede = e;
5169 if (prede)
5171 bb1 = prede->src;
5173 /* Re-do the dominance check with changed bb1. */
5174 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5175 return true;
5179 /* Iterate to the single executable bb2 successor. */
5180 if (EDGE_COUNT (bb2->succs) > 1)
5182 edge succe = NULL;
5183 FOR_EACH_EDGE (e, ei, bb2->succs)
5184 if ((e->flags & EDGE_EXECUTABLE)
5185 || (!allow_back && (e->flags & EDGE_DFS_BACK)))
5187 if (succe)
5189 succe = NULL;
5190 break;
5192 succe = e;
5194 if (succe)
5196 /* Verify the reached block is only reached through succe.
5197 If there is only one edge we can spare us the dominator
5198 check and iterate directly. */
5199 if (EDGE_COUNT (succe->dest->preds) > 1)
5201 FOR_EACH_EDGE (e, ei, succe->dest->preds)
5202 if (e != succe
5203 && ((e->flags & EDGE_EXECUTABLE)
5204 || (!allow_back && (e->flags & EDGE_DFS_BACK))))
5206 succe = NULL;
5207 break;
5210 if (succe)
5212 bb2 = succe->dest;
5214 /* Re-do the dominance check with changed bb2. */
5215 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
5216 return true;
5221 /* We could now iterate updating bb1 / bb2. */
5222 return false;
5225 /* Set the value number of FROM to TO, return true if it has changed
5226 as a result. */
5228 static inline bool
5229 set_ssa_val_to (tree from, tree to)
5231 vn_ssa_aux_t from_info = VN_INFO (from);
5232 tree currval = from_info->valnum; // SSA_VAL (from)
5233 poly_int64 toff, coff;
5234 bool curr_undefined = false;
5235 bool curr_invariant = false;
5237 /* The only thing we allow as value numbers are ssa_names
5238 and invariants. So assert that here. We don't allow VN_TOP
5239 as visiting a stmt should produce a value-number other than
5240 that.
5241 ??? Still VN_TOP can happen for unreachable code, so force
5242 it to varying in that case. Not all code is prepared to
5243 get VN_TOP on valueization. */
5244 if (to == VN_TOP)
5246 /* ??? When iterating and visiting PHI <undef, backedge-value>
5247 for the first time we rightfully get VN_TOP and we need to
5248 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
5249 With SCCVN we were simply lucky we iterated the other PHI
5250 cycles first and thus visited the backedge-value DEF. */
5251 if (currval == VN_TOP)
5252 goto set_and_exit;
5253 if (dump_file && (dump_flags & TDF_DETAILS))
5254 fprintf (dump_file, "Forcing value number to varying on "
5255 "receiving VN_TOP\n");
5256 to = from;
5259 gcc_checking_assert (to != NULL_TREE
5260 && ((TREE_CODE (to) == SSA_NAME
5261 && (to == from || SSA_VAL (to) == to))
5262 || is_gimple_min_invariant (to)));
5264 if (from != to)
5266 if (currval == from)
5268 if (dump_file && (dump_flags & TDF_DETAILS))
5270 fprintf (dump_file, "Not changing value number of ");
5271 print_generic_expr (dump_file, from);
5272 fprintf (dump_file, " from VARYING to ");
5273 print_generic_expr (dump_file, to);
5274 fprintf (dump_file, "\n");
5276 return false;
5278 curr_invariant = is_gimple_min_invariant (currval);
5279 curr_undefined = (TREE_CODE (currval) == SSA_NAME
5280 && !virtual_operand_p (currval)
5281 && ssa_undefined_value_p (currval, false));
5282 if (currval != VN_TOP
5283 && !curr_invariant
5284 && !curr_undefined
5285 && is_gimple_min_invariant (to))
5287 if (dump_file && (dump_flags & TDF_DETAILS))
5289 fprintf (dump_file, "Forcing VARYING instead of changing "
5290 "value number of ");
5291 print_generic_expr (dump_file, from);
5292 fprintf (dump_file, " from ");
5293 print_generic_expr (dump_file, currval);
5294 fprintf (dump_file, " (non-constant) to ");
5295 print_generic_expr (dump_file, to);
5296 fprintf (dump_file, " (constant)\n");
5298 to = from;
5300 else if (currval != VN_TOP
5301 && !curr_undefined
5302 && TREE_CODE (to) == SSA_NAME
5303 && !virtual_operand_p (to)
5304 && ssa_undefined_value_p (to, false))
5306 if (dump_file && (dump_flags & TDF_DETAILS))
5308 fprintf (dump_file, "Forcing VARYING instead of changing "
5309 "value number of ");
5310 print_generic_expr (dump_file, from);
5311 fprintf (dump_file, " from ");
5312 print_generic_expr (dump_file, currval);
5313 fprintf (dump_file, " (non-undefined) to ");
5314 print_generic_expr (dump_file, to);
5315 fprintf (dump_file, " (undefined)\n");
5317 to = from;
5319 else if (TREE_CODE (to) == SSA_NAME
5320 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
5321 to = from;
5324 set_and_exit:
5325 if (dump_file && (dump_flags & TDF_DETAILS))
5327 fprintf (dump_file, "Setting value number of ");
5328 print_generic_expr (dump_file, from);
5329 fprintf (dump_file, " to ");
5330 print_generic_expr (dump_file, to);
5333 if (currval != to
5334 && !operand_equal_p (currval, to, 0)
5335 /* Different undefined SSA names are not actually different. See
5336 PR82320 for a testcase were we'd otherwise not terminate iteration. */
5337 && !(curr_undefined
5338 && TREE_CODE (to) == SSA_NAME
5339 && !virtual_operand_p (to)
5340 && ssa_undefined_value_p (to, false))
5341 /* ??? For addresses involving volatile objects or types operand_equal_p
5342 does not reliably detect ADDR_EXPRs as equal. We know we are only
5343 getting invariant gimple addresses here, so can use
5344 get_addr_base_and_unit_offset to do this comparison. */
5345 && !(TREE_CODE (currval) == ADDR_EXPR
5346 && TREE_CODE (to) == ADDR_EXPR
5347 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
5348 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
5349 && known_eq (coff, toff)))
5351 if (to != from
5352 && currval != VN_TOP
5353 && !curr_undefined
5354 /* We do not want to allow lattice transitions from one value
5355 to another since that may lead to not terminating iteration
5356 (see PR95049). Since there's no convenient way to check
5357 for the allowed transition of VAL -> PHI (loop entry value,
5358 same on two PHIs, to same PHI result) we restrict the check
5359 to invariants. */
5360 && curr_invariant
5361 && is_gimple_min_invariant (to))
5363 if (dump_file && (dump_flags & TDF_DETAILS))
5364 fprintf (dump_file, " forced VARYING");
5365 to = from;
5367 if (dump_file && (dump_flags & TDF_DETAILS))
5368 fprintf (dump_file, " (changed)\n");
5369 from_info->valnum = to;
5370 return true;
5372 if (dump_file && (dump_flags & TDF_DETAILS))
5373 fprintf (dump_file, "\n");
5374 return false;
5377 /* Set all definitions in STMT to value number to themselves.
5378 Return true if a value number changed. */
5380 static bool
5381 defs_to_varying (gimple *stmt)
5383 bool changed = false;
5384 ssa_op_iter iter;
5385 def_operand_p defp;
5387 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
5389 tree def = DEF_FROM_PTR (defp);
5390 changed |= set_ssa_val_to (def, def);
5392 return changed;
5395 /* Visit a copy between LHS and RHS, return true if the value number
5396 changed. */
5398 static bool
5399 visit_copy (tree lhs, tree rhs)
5401 /* Valueize. */
5402 rhs = SSA_VAL (rhs);
5404 return set_ssa_val_to (lhs, rhs);
5407 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
5408 is the same. */
5410 static tree
5411 valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
5413 if (TREE_CODE (op) == SSA_NAME)
5414 op = vn_valueize (op);
5416 /* Either we have the op widened available. */
5417 tree ops[3] = {};
5418 ops[0] = op;
5419 tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
5420 wide_type, ops, NULL);
5421 if (tem)
5422 return tem;
5424 /* Or the op is truncated from some existing value. */
5425 if (allow_truncate && TREE_CODE (op) == SSA_NAME)
5427 gimple *def = SSA_NAME_DEF_STMT (op);
5428 if (is_gimple_assign (def)
5429 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
5431 tem = gimple_assign_rhs1 (def);
5432 if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
5434 if (TREE_CODE (tem) == SSA_NAME)
5435 tem = vn_valueize (tem);
5436 return tem;
5441 /* For constants simply extend it. */
5442 if (TREE_CODE (op) == INTEGER_CST)
5443 return wide_int_to_tree (wide_type, wi::to_widest (op));
5445 return NULL_TREE;
5448 /* Visit a nary operator RHS, value number it, and return true if the
5449 value number of LHS has changed as a result. */
5451 static bool
5452 visit_nary_op (tree lhs, gassign *stmt)
5454 vn_nary_op_t vnresult;
5455 tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
5456 if (! result && vnresult)
5457 result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
5458 if (result)
5459 return set_ssa_val_to (lhs, result);
5461 /* Do some special pattern matching for redundancies of operations
5462 in different types. */
5463 enum tree_code code = gimple_assign_rhs_code (stmt);
5464 tree type = TREE_TYPE (lhs);
5465 tree rhs1 = gimple_assign_rhs1 (stmt);
5466 switch (code)
5468 CASE_CONVERT:
5469 /* Match arithmetic done in a different type where we can easily
5470 substitute the result from some earlier sign-changed or widened
5471 operation. */
5472 if (INTEGRAL_TYPE_P (type)
5473 && TREE_CODE (rhs1) == SSA_NAME
5474 /* We only handle sign-changes, zero-extension -> & mask or
5475 sign-extension if we know the inner operation doesn't
5476 overflow. */
5477 && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
5478 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5479 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5480 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
5481 || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
5483 gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5484 if (def
5485 && (gimple_assign_rhs_code (def) == PLUS_EXPR
5486 || gimple_assign_rhs_code (def) == MINUS_EXPR
5487 || gimple_assign_rhs_code (def) == MULT_EXPR))
5489 tree ops[3] = {};
5490 /* When requiring a sign-extension we cannot model a
5491 previous truncation with a single op so don't bother. */
5492 bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
5493 /* Either we have the op widened available. */
5494 ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
5495 allow_truncate);
5496 if (ops[0])
5497 ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
5498 allow_truncate);
5499 if (ops[0] && ops[1])
5501 ops[0] = vn_nary_op_lookup_pieces
5502 (2, gimple_assign_rhs_code (def), type, ops, NULL);
5503 /* We have wider operation available. */
5504 if (ops[0]
5505 /* If the leader is a wrapping operation we can
5506 insert it for code hoisting w/o introducing
5507 undefined overflow. If it is not it has to
5508 be available. See PR86554. */
5509 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
5510 || (rpo_avail && vn_context_bb
5511 && rpo_avail->eliminate_avail (vn_context_bb,
5512 ops[0]))))
5514 unsigned lhs_prec = TYPE_PRECISION (type);
5515 unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
5516 if (lhs_prec == rhs_prec
5517 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5518 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
5520 gimple_match_op match_op (gimple_match_cond::UNCOND,
5521 NOP_EXPR, type, ops[0]);
5522 result = vn_nary_build_or_lookup (&match_op);
5523 if (result)
5525 bool changed = set_ssa_val_to (lhs, result);
5526 vn_nary_op_insert_stmt (stmt, result);
5527 return changed;
5530 else
5532 tree mask = wide_int_to_tree
5533 (type, wi::mask (rhs_prec, false, lhs_prec));
5534 gimple_match_op match_op (gimple_match_cond::UNCOND,
5535 BIT_AND_EXPR,
5536 TREE_TYPE (lhs),
5537 ops[0], mask);
5538 result = vn_nary_build_or_lookup (&match_op);
5539 if (result)
5541 bool changed = set_ssa_val_to (lhs, result);
5542 vn_nary_op_insert_stmt (stmt, result);
5543 return changed;
5550 break;
5551 case BIT_AND_EXPR:
5552 if (INTEGRAL_TYPE_P (type)
5553 && TREE_CODE (rhs1) == SSA_NAME
5554 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
5555 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
5556 && default_vn_walk_kind != VN_NOWALK
5557 && CHAR_BIT == 8
5558 && BITS_PER_UNIT == 8
5559 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
5560 && TYPE_PRECISION (type) <= vn_walk_cb_data::bufsize * BITS_PER_UNIT
5561 && !integer_all_onesp (gimple_assign_rhs2 (stmt))
5562 && !integer_zerop (gimple_assign_rhs2 (stmt)))
5564 gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
5565 if (ass
5566 && !gimple_has_volatile_ops (ass)
5567 && vn_get_stmt_kind (ass) == VN_REFERENCE)
5569 tree last_vuse = gimple_vuse (ass);
5570 tree op = gimple_assign_rhs1 (ass);
5571 tree result = vn_reference_lookup (op, gimple_vuse (ass),
5572 default_vn_walk_kind,
5573 NULL, true, &last_vuse,
5574 gimple_assign_rhs2 (stmt));
5575 if (result
5576 && useless_type_conversion_p (TREE_TYPE (result),
5577 TREE_TYPE (op)))
5578 return set_ssa_val_to (lhs, result);
5581 break;
5582 case TRUNC_DIV_EXPR:
5583 if (TYPE_UNSIGNED (type))
5584 break;
5585 /* Fallthru. */
5586 case RDIV_EXPR:
5587 case MULT_EXPR:
5588 /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5589 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5591 tree rhs[2];
5592 rhs[0] = rhs1;
5593 rhs[1] = gimple_assign_rhs2 (stmt);
5594 for (unsigned i = 0; i <= 1; ++i)
5596 unsigned j = i == 0 ? 1 : 0;
5597 tree ops[2];
5598 gimple_match_op match_op (gimple_match_cond::UNCOND,
5599 NEGATE_EXPR, type, rhs[i]);
5600 ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5601 ops[j] = rhs[j];
5602 if (ops[i]
5603 && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5604 type, ops, NULL)))
5606 gimple_match_op match_op (gimple_match_cond::UNCOND,
5607 NEGATE_EXPR, type, ops[0]);
5608 result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5609 if (result)
5611 bool changed = set_ssa_val_to (lhs, result);
5612 vn_nary_op_insert_stmt (stmt, result);
5613 return changed;
5618 break;
5619 case LSHIFT_EXPR:
5620 /* For X << C, use the value number of X * (1 << C). */
5621 if (INTEGRAL_TYPE_P (type)
5622 && TYPE_OVERFLOW_WRAPS (type)
5623 && !TYPE_SATURATING (type))
5625 tree rhs2 = gimple_assign_rhs2 (stmt);
5626 if (TREE_CODE (rhs2) == INTEGER_CST
5627 && tree_fits_uhwi_p (rhs2)
5628 && tree_to_uhwi (rhs2) < TYPE_PRECISION (type))
5630 wide_int w = wi::set_bit_in_zero (tree_to_uhwi (rhs2),
5631 TYPE_PRECISION (type));
5632 gimple_match_op match_op (gimple_match_cond::UNCOND,
5633 MULT_EXPR, type, rhs1,
5634 wide_int_to_tree (type, w));
5635 result = vn_nary_build_or_lookup (&match_op);
5636 if (result)
5638 bool changed = set_ssa_val_to (lhs, result);
5639 if (TREE_CODE (result) == SSA_NAME)
5640 vn_nary_op_insert_stmt (stmt, result);
5641 return changed;
5645 break;
5646 default:
5647 break;
5650 bool changed = set_ssa_val_to (lhs, lhs);
5651 vn_nary_op_insert_stmt (stmt, lhs);
5652 return changed;
5655 /* Visit a call STMT storing into LHS. Return true if the value number
5656 of the LHS has changed as a result. */
5658 static bool
5659 visit_reference_op_call (tree lhs, gcall *stmt)
5661 bool changed = false;
5662 struct vn_reference_s vr1;
5663 vn_reference_t vnresult = NULL;
5664 tree vdef = gimple_vdef (stmt);
5665 modref_summary *summary;
5667 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5668 if (lhs && TREE_CODE (lhs) != SSA_NAME)
5669 lhs = NULL_TREE;
5671 vn_reference_lookup_call (stmt, &vnresult, &vr1);
5673 /* If the lookup did not succeed for pure functions try to use
5674 modref info to find a candidate to CSE to. */
5675 const unsigned accesses_limit = 8;
5676 if (!vnresult
5677 && !vdef
5678 && lhs
5679 && gimple_vuse (stmt)
5680 && (((summary = get_modref_function_summary (stmt, NULL))
5681 && !summary->global_memory_read
5682 && summary->load_accesses < accesses_limit)
5683 || gimple_call_flags (stmt) & ECF_CONST))
5685 /* First search if we can do someting useful and build a
5686 vector of all loads we have to check. */
5687 bool unknown_memory_access = false;
5688 auto_vec<ao_ref, accesses_limit> accesses;
5689 unsigned load_accesses = summary ? summary->load_accesses : 0;
5690 if (!unknown_memory_access)
5691 /* Add loads done as part of setting up the call arguments.
5692 That's also necessary for CONST functions which will
5693 not have a modref summary. */
5694 for (unsigned i = 0; i < gimple_call_num_args (stmt); ++i)
5696 tree arg = gimple_call_arg (stmt, i);
5697 if (TREE_CODE (arg) != SSA_NAME
5698 && !is_gimple_min_invariant (arg))
5700 if (accesses.length () >= accesses_limit - load_accesses)
5702 unknown_memory_access = true;
5703 break;
5705 accesses.quick_grow (accesses.length () + 1);
5706 ao_ref_init (&accesses.last (), arg);
5709 if (summary && !unknown_memory_access)
5711 /* Add loads as analyzed by IPA modref. */
5712 for (auto base_node : summary->loads->bases)
5713 if (unknown_memory_access)
5714 break;
5715 else for (auto ref_node : base_node->refs)
5716 if (unknown_memory_access)
5717 break;
5718 else for (auto access_node : ref_node->accesses)
5720 accesses.quick_grow (accesses.length () + 1);
5721 ao_ref *r = &accesses.last ();
5722 if (!access_node.get_ao_ref (stmt, r))
5724 /* Initialize a ref based on the argument and
5725 unknown offset if possible. */
5726 tree arg = access_node.get_call_arg (stmt);
5727 if (arg && TREE_CODE (arg) == SSA_NAME)
5728 arg = SSA_VAL (arg);
5729 if (arg
5730 && TREE_CODE (arg) == ADDR_EXPR
5731 && (arg = get_base_address (arg))
5732 && DECL_P (arg))
5734 ao_ref_init (r, arg);
5735 r->ref = NULL_TREE;
5736 r->base = arg;
5738 else
5740 unknown_memory_access = true;
5741 break;
5744 r->base_alias_set = base_node->base;
5745 r->ref_alias_set = ref_node->ref;
5749 /* Walk the VUSE->VDEF chain optimistically trying to find an entry
5750 for the call in the hashtable. */
5751 unsigned limit = (unknown_memory_access
5753 : (param_sccvn_max_alias_queries_per_access
5754 / (accesses.length () + 1)));
5755 tree saved_vuse = vr1.vuse;
5756 hashval_t saved_hashcode = vr1.hashcode;
5757 while (limit > 0 && !vnresult && !SSA_NAME_IS_DEFAULT_DEF (vr1.vuse))
5759 vr1.hashcode = vr1.hashcode - SSA_NAME_VERSION (vr1.vuse);
5760 gimple *def = SSA_NAME_DEF_STMT (vr1.vuse);
5761 /* ??? We could use fancy stuff like in walk_non_aliased_vuses, but
5762 do not bother for now. */
5763 if (is_a <gphi *> (def))
5764 break;
5765 vr1.vuse = vuse_ssa_val (gimple_vuse (def));
5766 vr1.hashcode = vr1.hashcode + SSA_NAME_VERSION (vr1.vuse);
5767 vn_reference_lookup_1 (&vr1, &vnresult);
5768 limit--;
5771 /* If we found a candidate to CSE to verify it is valid. */
5772 if (vnresult && !accesses.is_empty ())
5774 tree vuse = vuse_ssa_val (gimple_vuse (stmt));
5775 while (vnresult && vuse != vr1.vuse)
5777 gimple *def = SSA_NAME_DEF_STMT (vuse);
5778 for (auto &ref : accesses)
5780 /* ??? stmt_may_clobber_ref_p_1 does per stmt constant
5781 analysis overhead that we might be able to cache. */
5782 if (stmt_may_clobber_ref_p_1 (def, &ref, true))
5784 vnresult = NULL;
5785 break;
5788 vuse = vuse_ssa_val (gimple_vuse (def));
5791 vr1.vuse = saved_vuse;
5792 vr1.hashcode = saved_hashcode;
5795 if (vnresult)
5797 if (vdef)
5799 if (vnresult->result_vdef)
5800 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
5801 else if (!lhs && gimple_call_lhs (stmt))
5802 /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
5803 as the call still acts as a lhs store. */
5804 changed |= set_ssa_val_to (vdef, vdef);
5805 else
5806 /* If the call was discovered to be pure or const reflect
5807 that as far as possible. */
5808 changed |= set_ssa_val_to (vdef,
5809 vuse_ssa_val (gimple_vuse (stmt)));
5812 if (!vnresult->result && lhs)
5813 vnresult->result = lhs;
5815 if (vnresult->result && lhs)
5816 changed |= set_ssa_val_to (lhs, vnresult->result);
5818 else
5820 vn_reference_t vr2;
5821 vn_reference_s **slot;
5822 tree vdef_val = vdef;
5823 if (vdef)
5825 /* If we value numbered an indirect functions function to
5826 one not clobbering memory value number its VDEF to its
5827 VUSE. */
5828 tree fn = gimple_call_fn (stmt);
5829 if (fn && TREE_CODE (fn) == SSA_NAME)
5831 fn = SSA_VAL (fn);
5832 if (TREE_CODE (fn) == ADDR_EXPR
5833 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5834 && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
5835 & (ECF_CONST | ECF_PURE))
5836 /* If stmt has non-SSA_NAME lhs, value number the
5837 vdef to itself, as the call still acts as a lhs
5838 store. */
5839 && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
5840 vdef_val = vuse_ssa_val (gimple_vuse (stmt));
5842 changed |= set_ssa_val_to (vdef, vdef_val);
5844 if (lhs)
5845 changed |= set_ssa_val_to (lhs, lhs);
5846 vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
5847 vr2->vuse = vr1.vuse;
5848 /* As we are not walking the virtual operand chain we know the
5849 shared_lookup_references are still original so we can re-use
5850 them here. */
5851 vr2->operands = vr1.operands.copy ();
5852 vr2->type = vr1.type;
5853 vr2->punned = vr1.punned;
5854 vr2->set = vr1.set;
5855 vr2->base_set = vr1.base_set;
5856 vr2->hashcode = vr1.hashcode;
5857 vr2->result = lhs;
5858 vr2->result_vdef = vdef_val;
5859 vr2->value_id = 0;
5860 slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
5861 INSERT);
5862 gcc_assert (!*slot);
5863 *slot = vr2;
5864 vr2->next = last_inserted_ref;
5865 last_inserted_ref = vr2;
5868 return changed;
5871 /* Visit a load from a reference operator RHS, part of STMT, value number it,
5872 and return true if the value number of the LHS has changed as a result. */
5874 static bool
5875 visit_reference_op_load (tree lhs, tree op, gimple *stmt)
5877 bool changed = false;
5878 tree result;
5879 vn_reference_t res;
5881 tree vuse = gimple_vuse (stmt);
5882 tree last_vuse = vuse;
5883 result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
5885 /* We handle type-punning through unions by value-numbering based
5886 on offset and size of the access. Be prepared to handle a
5887 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5888 if (result
5889 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
5891 /* Avoid the type punning in case the result mode has padding where
5892 the op we lookup has not. */
5893 if (TYPE_MODE (TREE_TYPE (result)) != BLKmode
5894 && maybe_lt (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (result))),
5895 GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op)))))
5896 result = NULL_TREE;
5897 else if (CONSTANT_CLASS_P (result))
5898 result = const_unop (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5899 else
5901 /* We will be setting the value number of lhs to the value number
5902 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5903 So first simplify and lookup this expression to see if it
5904 is already available. */
5905 gimple_match_op res_op (gimple_match_cond::UNCOND,
5906 VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5907 result = vn_nary_build_or_lookup (&res_op);
5908 if (result
5909 && TREE_CODE (result) == SSA_NAME
5910 && VN_INFO (result)->needs_insertion)
5911 /* Track whether this is the canonical expression for different
5912 typed loads. We use that as a stopgap measure for code
5913 hoisting when dealing with floating point loads. */
5914 res->punned = true;
5917 /* When building the conversion fails avoid inserting the reference
5918 again. */
5919 if (!result)
5920 return set_ssa_val_to (lhs, lhs);
5923 if (result)
5924 changed = set_ssa_val_to (lhs, result);
5925 else
5927 changed = set_ssa_val_to (lhs, lhs);
5928 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
5929 if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
5931 if (dump_file && (dump_flags & TDF_DETAILS))
5933 fprintf (dump_file, "Using extra use virtual operand ");
5934 print_generic_expr (dump_file, last_vuse);
5935 fprintf (dump_file, "\n");
5937 vn_reference_insert (op, lhs, vuse, NULL_TREE);
5941 return changed;
5945 /* Visit a store to a reference operator LHS, part of STMT, value number it,
5946 and return true if the value number of the LHS has changed as a result. */
5948 static bool
5949 visit_reference_op_store (tree lhs, tree op, gimple *stmt)
5951 bool changed = false;
5952 vn_reference_t vnresult = NULL;
5953 tree assign;
5954 bool resultsame = false;
5955 tree vuse = gimple_vuse (stmt);
5956 tree vdef = gimple_vdef (stmt);
5958 if (TREE_CODE (op) == SSA_NAME)
5959 op = SSA_VAL (op);
5961 /* First we want to lookup using the *vuses* from the store and see
5962 if there the last store to this location with the same address
5963 had the same value.
5965 The vuses represent the memory state before the store. If the
5966 memory state, address, and value of the store is the same as the
5967 last store to this location, then this store will produce the
5968 same memory state as that store.
5970 In this case the vdef versions for this store are value numbered to those
5971 vuse versions, since they represent the same memory state after
5972 this store.
5974 Otherwise, the vdefs for the store are used when inserting into
5975 the table, since the store generates a new memory state. */
5977 vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
5978 if (vnresult
5979 && vnresult->result)
5981 tree result = vnresult->result;
5982 gcc_checking_assert (TREE_CODE (result) != SSA_NAME
5983 || result == SSA_VAL (result));
5984 resultsame = expressions_equal_p (result, op);
5985 if (resultsame)
5987 /* If the TBAA state isn't compatible for downstream reads
5988 we cannot value-number the VDEFs the same. */
5989 ao_ref lhs_ref;
5990 ao_ref_init (&lhs_ref, lhs);
5991 alias_set_type set = ao_ref_alias_set (&lhs_ref);
5992 alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
5993 if ((vnresult->set != set
5994 && ! alias_set_subset_of (set, vnresult->set))
5995 || (vnresult->base_set != base_set
5996 && ! alias_set_subset_of (base_set, vnresult->base_set)))
5997 resultsame = false;
6001 if (!resultsame)
6003 if (dump_file && (dump_flags & TDF_DETAILS))
6005 fprintf (dump_file, "No store match\n");
6006 fprintf (dump_file, "Value numbering store ");
6007 print_generic_expr (dump_file, lhs);
6008 fprintf (dump_file, " to ");
6009 print_generic_expr (dump_file, op);
6010 fprintf (dump_file, "\n");
6012 /* Have to set value numbers before insert, since insert is
6013 going to valueize the references in-place. */
6014 if (vdef)
6015 changed |= set_ssa_val_to (vdef, vdef);
6017 /* Do not insert structure copies into the tables. */
6018 if (is_gimple_min_invariant (op)
6019 || is_gimple_reg (op))
6020 vn_reference_insert (lhs, op, vdef, NULL);
6022 /* Only perform the following when being called from PRE
6023 which embeds tail merging. */
6024 if (default_vn_walk_kind == VN_WALK)
6026 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
6027 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
6028 if (!vnresult)
6029 vn_reference_insert (assign, lhs, vuse, vdef);
6032 else
6034 /* We had a match, so value number the vdef to have the value
6035 number of the vuse it came from. */
6037 if (dump_file && (dump_flags & TDF_DETAILS))
6038 fprintf (dump_file, "Store matched earlier value, "
6039 "value numbering store vdefs to matching vuses.\n");
6041 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
6044 return changed;
6047 /* Visit and value number PHI, return true if the value number
6048 changed. When BACKEDGES_VARYING_P is true then assume all
6049 backedge values are varying. When INSERTED is not NULL then
6050 this is just a ahead query for a possible iteration, set INSERTED
6051 to true if we'd insert into the hashtable. */
6053 static bool
6054 visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
6056 tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
6057 bool seen_undef_visited = false;
6058 tree backedge_val = NULL_TREE;
6059 bool seen_non_backedge = false;
6060 tree sameval_base = NULL_TREE;
6061 poly_int64 soff, doff;
6062 unsigned n_executable = 0;
6063 edge_iterator ei;
6064 edge e, sameval_e = NULL;
6066 /* TODO: We could check for this in initialization, and replace this
6067 with a gcc_assert. */
6068 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
6069 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
6071 /* We track whether a PHI was CSEd to to avoid excessive iterations
6072 that would be necessary only because the PHI changed arguments
6073 but not value. */
6074 if (!inserted)
6075 gimple_set_plf (phi, GF_PLF_1, false);
6077 /* See if all non-TOP arguments have the same value. TOP is
6078 equivalent to everything, so we can ignore it. */
6079 basic_block bb = gimple_bb (phi);
6080 FOR_EACH_EDGE (e, ei, bb->preds)
6081 if (e->flags & EDGE_EXECUTABLE)
6083 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
6085 if (def == PHI_RESULT (phi))
6086 continue;
6087 ++n_executable;
6088 bool visited = true;
6089 if (TREE_CODE (def) == SSA_NAME)
6091 tree val = SSA_VAL (def, &visited);
6092 if (SSA_NAME_IS_DEFAULT_DEF (def))
6093 visited = true;
6094 if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
6095 def = val;
6096 if (e->flags & EDGE_DFS_BACK)
6097 backedge_val = def;
6099 if (!(e->flags & EDGE_DFS_BACK))
6100 seen_non_backedge = true;
6101 if (def == VN_TOP)
6103 /* Ignore undefined defs for sameval but record one. */
6104 else if (TREE_CODE (def) == SSA_NAME
6105 && ! virtual_operand_p (def)
6106 && ssa_undefined_value_p (def, false))
6108 if (!seen_undef
6109 /* Avoid having not visited undefined defs if we also have
6110 a visited one. */
6111 || (!seen_undef_visited && visited))
6113 seen_undef = def;
6114 seen_undef_visited = visited;
6117 else if (sameval == VN_TOP)
6119 sameval = def;
6120 sameval_e = e;
6122 else if (expressions_equal_p (def, sameval))
6123 sameval_e = NULL;
6124 else if (virtual_operand_p (def))
6126 sameval = NULL_TREE;
6127 break;
6129 else
6131 /* We know we're arriving only with invariant addresses here,
6132 try harder comparing them. We can do some caching here
6133 which we cannot do in expressions_equal_p. */
6134 if (TREE_CODE (def) == ADDR_EXPR
6135 && TREE_CODE (sameval) == ADDR_EXPR
6136 && sameval_base != (void *)-1)
6138 if (!sameval_base)
6139 sameval_base = get_addr_base_and_unit_offset
6140 (TREE_OPERAND (sameval, 0), &soff);
6141 if (!sameval_base)
6142 sameval_base = (tree)(void *)-1;
6143 else if ((get_addr_base_and_unit_offset
6144 (TREE_OPERAND (def, 0), &doff) == sameval_base)
6145 && known_eq (soff, doff))
6146 continue;
6148 /* There's also the possibility to use equivalences. */
6149 if (!FLOAT_TYPE_P (TREE_TYPE (def))
6150 /* But only do this if we didn't force any of sameval or
6151 val to VARYING because of backedge processing rules. */
6152 && (TREE_CODE (sameval) != SSA_NAME
6153 || SSA_VAL (sameval) == sameval)
6154 && (TREE_CODE (def) != SSA_NAME || SSA_VAL (def) == def))
6156 vn_nary_op_t vnresult;
6157 tree ops[2];
6158 ops[0] = def;
6159 ops[1] = sameval;
6160 tree val = vn_nary_op_lookup_pieces (2, EQ_EXPR,
6161 boolean_type_node,
6162 ops, &vnresult);
6163 if (! val && vnresult && vnresult->predicated_values)
6165 val = vn_nary_op_get_predicated_value (vnresult, e);
6166 if (val && integer_truep (val)
6167 && !(sameval_e && (sameval_e->flags & EDGE_DFS_BACK)))
6169 if (dump_file && (dump_flags & TDF_DETAILS))
6171 fprintf (dump_file, "Predication says ");
6172 print_generic_expr (dump_file, def, TDF_NONE);
6173 fprintf (dump_file, " and ");
6174 print_generic_expr (dump_file, sameval, TDF_NONE);
6175 fprintf (dump_file, " are equal on edge %d -> %d\n",
6176 e->src->index, e->dest->index);
6178 continue;
6180 /* If on all previous edges the value was equal to def
6181 we can change sameval to def. */
6182 if (EDGE_COUNT (bb->preds) == 2
6183 && (val = vn_nary_op_get_predicated_value
6184 (vnresult, EDGE_PRED (bb, 0)))
6185 && integer_truep (val)
6186 && !(e->flags & EDGE_DFS_BACK))
6188 if (dump_file && (dump_flags & TDF_DETAILS))
6190 fprintf (dump_file, "Predication says ");
6191 print_generic_expr (dump_file, def, TDF_NONE);
6192 fprintf (dump_file, " and ");
6193 print_generic_expr (dump_file, sameval, TDF_NONE);
6194 fprintf (dump_file, " are equal on edge %d -> %d\n",
6195 EDGE_PRED (bb, 0)->src->index,
6196 EDGE_PRED (bb, 0)->dest->index);
6198 sameval = def;
6199 continue;
6203 sameval = NULL_TREE;
6204 break;
6208 /* If the value we want to use is flowing over the backedge and we
6209 should take it as VARYING but it has a non-VARYING value drop to
6210 VARYING.
6211 If we value-number a virtual operand never value-number to the
6212 value from the backedge as that confuses the alias-walking code.
6213 See gcc.dg/torture/pr87176.c. If the value is the same on a
6214 non-backedge everything is OK though. */
6215 bool visited_p;
6216 if ((backedge_val
6217 && !seen_non_backedge
6218 && TREE_CODE (backedge_val) == SSA_NAME
6219 && sameval == backedge_val
6220 && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
6221 || SSA_VAL (backedge_val) != backedge_val))
6222 /* Do not value-number a virtual operand to sth not visited though
6223 given that allows us to escape a region in alias walking. */
6224 || (sameval
6225 && TREE_CODE (sameval) == SSA_NAME
6226 && !SSA_NAME_IS_DEFAULT_DEF (sameval)
6227 && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
6228 && (SSA_VAL (sameval, &visited_p), !visited_p)))
6229 /* Note this just drops to VARYING without inserting the PHI into
6230 the hashes. */
6231 result = PHI_RESULT (phi);
6232 /* If none of the edges was executable keep the value-number at VN_TOP,
6233 if only a single edge is exectuable use its value. */
6234 else if (n_executable <= 1)
6235 result = seen_undef ? seen_undef : sameval;
6236 /* If we saw only undefined values and VN_TOP use one of the
6237 undefined values. */
6238 else if (sameval == VN_TOP)
6239 result = (seen_undef && seen_undef_visited) ? seen_undef : sameval;
6240 /* First see if it is equivalent to a phi node in this block. We prefer
6241 this as it allows IV elimination - see PRs 66502 and 67167. */
6242 else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
6244 if (!inserted
6245 && TREE_CODE (result) == SSA_NAME
6246 && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
6248 gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
6249 if (dump_file && (dump_flags & TDF_DETAILS))
6251 fprintf (dump_file, "Marking CSEd to PHI node ");
6252 print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
6253 0, TDF_SLIM);
6254 fprintf (dump_file, "\n");
6258 /* If all values are the same use that, unless we've seen undefined
6259 values as well and the value isn't constant.
6260 CCP/copyprop have the same restriction to not remove uninit warnings. */
6261 else if (sameval
6262 && (! seen_undef || is_gimple_min_invariant (sameval)))
6263 result = sameval;
6264 else
6266 result = PHI_RESULT (phi);
6267 /* Only insert PHIs that are varying, for constant value numbers
6268 we mess up equivalences otherwise as we are only comparing
6269 the immediate controlling predicates. */
6270 vn_phi_insert (phi, result, backedges_varying_p);
6271 if (inserted)
6272 *inserted = true;
6275 return set_ssa_val_to (PHI_RESULT (phi), result);
6278 /* Try to simplify RHS using equivalences and constant folding. */
6280 static tree
6281 try_to_simplify (gassign *stmt)
6283 enum tree_code code = gimple_assign_rhs_code (stmt);
6284 tree tem;
6286 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
6287 in this case, there is no point in doing extra work. */
6288 if (code == SSA_NAME)
6289 return NULL_TREE;
6291 /* First try constant folding based on our current lattice. */
6292 mprts_hook = vn_lookup_simplify_result;
6293 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
6294 mprts_hook = NULL;
6295 if (tem
6296 && (TREE_CODE (tem) == SSA_NAME
6297 || is_gimple_min_invariant (tem)))
6298 return tem;
6300 return NULL_TREE;
6303 /* Visit and value number STMT, return true if the value number
6304 changed. */
6306 static bool
6307 visit_stmt (gimple *stmt, bool backedges_varying_p = false)
6309 bool changed = false;
6311 if (dump_file && (dump_flags & TDF_DETAILS))
6313 fprintf (dump_file, "Value numbering stmt = ");
6314 print_gimple_stmt (dump_file, stmt, 0);
6317 if (gimple_code (stmt) == GIMPLE_PHI)
6318 changed = visit_phi (stmt, NULL, backedges_varying_p);
6319 else if (gimple_has_volatile_ops (stmt))
6320 changed = defs_to_varying (stmt);
6321 else if (gassign *ass = dyn_cast <gassign *> (stmt))
6323 enum tree_code code = gimple_assign_rhs_code (ass);
6324 tree lhs = gimple_assign_lhs (ass);
6325 tree rhs1 = gimple_assign_rhs1 (ass);
6326 tree simplified;
6328 /* Shortcut for copies. Simplifying copies is pointless,
6329 since we copy the expression and value they represent. */
6330 if (code == SSA_NAME
6331 && TREE_CODE (lhs) == SSA_NAME)
6333 changed = visit_copy (lhs, rhs1);
6334 goto done;
6336 simplified = try_to_simplify (ass);
6337 if (simplified)
6339 if (dump_file && (dump_flags & TDF_DETAILS))
6341 fprintf (dump_file, "RHS ");
6342 print_gimple_expr (dump_file, ass, 0);
6343 fprintf (dump_file, " simplified to ");
6344 print_generic_expr (dump_file, simplified);
6345 fprintf (dump_file, "\n");
6348 /* Setting value numbers to constants will occasionally
6349 screw up phi congruence because constants are not
6350 uniquely associated with a single ssa name that can be
6351 looked up. */
6352 if (simplified
6353 && is_gimple_min_invariant (simplified)
6354 && TREE_CODE (lhs) == SSA_NAME)
6356 changed = set_ssa_val_to (lhs, simplified);
6357 goto done;
6359 else if (simplified
6360 && TREE_CODE (simplified) == SSA_NAME
6361 && TREE_CODE (lhs) == SSA_NAME)
6363 changed = visit_copy (lhs, simplified);
6364 goto done;
6367 if ((TREE_CODE (lhs) == SSA_NAME
6368 /* We can substitute SSA_NAMEs that are live over
6369 abnormal edges with their constant value. */
6370 && !(gimple_assign_copy_p (ass)
6371 && is_gimple_min_invariant (rhs1))
6372 && !(simplified
6373 && is_gimple_min_invariant (simplified))
6374 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6375 /* Stores or copies from SSA_NAMEs that are live over
6376 abnormal edges are a problem. */
6377 || (code == SSA_NAME
6378 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6379 changed = defs_to_varying (ass);
6380 else if (REFERENCE_CLASS_P (lhs)
6381 || DECL_P (lhs))
6382 changed = visit_reference_op_store (lhs, rhs1, ass);
6383 else if (TREE_CODE (lhs) == SSA_NAME)
6385 if ((gimple_assign_copy_p (ass)
6386 && is_gimple_min_invariant (rhs1))
6387 || (simplified
6388 && is_gimple_min_invariant (simplified)))
6390 if (simplified)
6391 changed = set_ssa_val_to (lhs, simplified);
6392 else
6393 changed = set_ssa_val_to (lhs, rhs1);
6395 else
6397 /* Visit the original statement. */
6398 switch (vn_get_stmt_kind (ass))
6400 case VN_NARY:
6401 changed = visit_nary_op (lhs, ass);
6402 break;
6403 case VN_REFERENCE:
6404 changed = visit_reference_op_load (lhs, rhs1, ass);
6405 break;
6406 default:
6407 changed = defs_to_varying (ass);
6408 break;
6412 else
6413 changed = defs_to_varying (ass);
6415 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6417 tree lhs = gimple_call_lhs (call_stmt);
6418 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6420 /* Try constant folding based on our current lattice. */
6421 tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
6422 vn_valueize);
6423 if (simplified)
6425 if (dump_file && (dump_flags & TDF_DETAILS))
6427 fprintf (dump_file, "call ");
6428 print_gimple_expr (dump_file, call_stmt, 0);
6429 fprintf (dump_file, " simplified to ");
6430 print_generic_expr (dump_file, simplified);
6431 fprintf (dump_file, "\n");
6434 /* Setting value numbers to constants will occasionally
6435 screw up phi congruence because constants are not
6436 uniquely associated with a single ssa name that can be
6437 looked up. */
6438 if (simplified
6439 && is_gimple_min_invariant (simplified))
6441 changed = set_ssa_val_to (lhs, simplified);
6442 if (gimple_vdef (call_stmt))
6443 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6444 SSA_VAL (gimple_vuse (call_stmt)));
6445 goto done;
6447 else if (simplified
6448 && TREE_CODE (simplified) == SSA_NAME)
6450 changed = visit_copy (lhs, simplified);
6451 if (gimple_vdef (call_stmt))
6452 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
6453 SSA_VAL (gimple_vuse (call_stmt)));
6454 goto done;
6456 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
6458 changed = defs_to_varying (call_stmt);
6459 goto done;
6463 /* Pick up flags from a devirtualization target. */
6464 tree fn = gimple_call_fn (stmt);
6465 int extra_fnflags = 0;
6466 if (fn && TREE_CODE (fn) == SSA_NAME)
6468 fn = SSA_VAL (fn);
6469 if (TREE_CODE (fn) == ADDR_EXPR
6470 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
6471 extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
6473 if ((/* Calls to the same function with the same vuse
6474 and the same operands do not necessarily return the same
6475 value, unless they're pure or const. */
6476 ((gimple_call_flags (call_stmt) | extra_fnflags)
6477 & (ECF_PURE | ECF_CONST))
6478 /* If calls have a vdef, subsequent calls won't have
6479 the same incoming vuse. So, if 2 calls with vdef have the
6480 same vuse, we know they're not subsequent.
6481 We can value number 2 calls to the same function with the
6482 same vuse and the same operands which are not subsequent
6483 the same, because there is no code in the program that can
6484 compare the 2 values... */
6485 || (gimple_vdef (call_stmt)
6486 /* ... unless the call returns a pointer which does
6487 not alias with anything else. In which case the
6488 information that the values are distinct are encoded
6489 in the IL. */
6490 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
6491 /* Only perform the following when being called from PRE
6492 which embeds tail merging. */
6493 && default_vn_walk_kind == VN_WALK))
6494 /* Do not process .DEFERRED_INIT since that confuses uninit
6495 analysis. */
6496 && !gimple_call_internal_p (call_stmt, IFN_DEFERRED_INIT))
6497 changed = visit_reference_op_call (lhs, call_stmt);
6498 else
6499 changed = defs_to_varying (call_stmt);
6501 else
6502 changed = defs_to_varying (stmt);
6503 done:
6504 return changed;
6508 /* Allocate a value number table. */
6510 static void
6511 allocate_vn_table (vn_tables_t table, unsigned size)
6513 table->phis = new vn_phi_table_type (size);
6514 table->nary = new vn_nary_op_table_type (size);
6515 table->references = new vn_reference_table_type (size);
6518 /* Free a value number table. */
6520 static void
6521 free_vn_table (vn_tables_t table)
6523 /* Walk over elements and release vectors. */
6524 vn_reference_iterator_type hir;
6525 vn_reference_t vr;
6526 FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
6527 vr->operands.release ();
6528 delete table->phis;
6529 table->phis = NULL;
6530 delete table->nary;
6531 table->nary = NULL;
6532 delete table->references;
6533 table->references = NULL;
6536 /* Set *ID according to RESULT. */
6538 static void
6539 set_value_id_for_result (tree result, unsigned int *id)
6541 if (result && TREE_CODE (result) == SSA_NAME)
6542 *id = VN_INFO (result)->value_id;
6543 else if (result && is_gimple_min_invariant (result))
6544 *id = get_or_alloc_constant_value_id (result);
6545 else
6546 *id = get_next_value_id ();
6549 /* Set the value ids in the valid hash tables. */
6551 static void
6552 set_hashtable_value_ids (void)
6554 vn_nary_op_iterator_type hin;
6555 vn_phi_iterator_type hip;
6556 vn_reference_iterator_type hir;
6557 vn_nary_op_t vno;
6558 vn_reference_t vr;
6559 vn_phi_t vp;
6561 /* Now set the value ids of the things we had put in the hash
6562 table. */
6564 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
6565 if (! vno->predicated_values)
6566 set_value_id_for_result (vno->u.result, &vno->value_id);
6568 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
6569 set_value_id_for_result (vp->result, &vp->value_id);
6571 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
6572 hir)
6573 set_value_id_for_result (vr->result, &vr->value_id);
6576 /* Return the maximum value id we have ever seen. */
6578 unsigned int
6579 get_max_value_id (void)
6581 return next_value_id;
6584 /* Return the maximum constant value id we have ever seen. */
6586 unsigned int
6587 get_max_constant_value_id (void)
6589 return -next_constant_value_id;
6592 /* Return the next unique value id. */
6594 unsigned int
6595 get_next_value_id (void)
6597 gcc_checking_assert ((int)next_value_id > 0);
6598 return next_value_id++;
6601 /* Return the next unique value id for constants. */
6603 unsigned int
6604 get_next_constant_value_id (void)
6606 gcc_checking_assert (next_constant_value_id < 0);
6607 return next_constant_value_id--;
6611 /* Compare two expressions E1 and E2 and return true if they are equal.
6612 If match_vn_top_optimistically is true then VN_TOP is equal to anything,
6613 otherwise VN_TOP only matches VN_TOP. */
6615 bool
6616 expressions_equal_p (tree e1, tree e2, bool match_vn_top_optimistically)
6618 /* The obvious case. */
6619 if (e1 == e2)
6620 return true;
6622 /* If either one is VN_TOP consider them equal. */
6623 if (match_vn_top_optimistically
6624 && (e1 == VN_TOP || e2 == VN_TOP))
6625 return true;
6627 /* If only one of them is null, they cannot be equal. While in general
6628 this should not happen for operations like TARGET_MEM_REF some
6629 operands are optional and an identity value we could substitute
6630 has differing semantics. */
6631 if (!e1 || !e2)
6632 return false;
6634 /* SSA_NAME compare pointer equal. */
6635 if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
6636 return false;
6638 /* Now perform the actual comparison. */
6639 if (TREE_CODE (e1) == TREE_CODE (e2)
6640 && operand_equal_p (e1, e2, OEP_PURE_SAME))
6641 return true;
6643 return false;
6647 /* Return true if the nary operation NARY may trap. This is a copy
6648 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
6650 bool
6651 vn_nary_may_trap (vn_nary_op_t nary)
6653 tree type;
6654 tree rhs2 = NULL_TREE;
6655 bool honor_nans = false;
6656 bool honor_snans = false;
6657 bool fp_operation = false;
6658 bool honor_trapv = false;
6659 bool handled, ret;
6660 unsigned i;
6662 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
6663 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
6664 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
6666 type = nary->type;
6667 fp_operation = FLOAT_TYPE_P (type);
6668 if (fp_operation)
6670 honor_nans = flag_trapping_math && !flag_finite_math_only;
6671 honor_snans = flag_signaling_nans != 0;
6673 else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
6674 honor_trapv = true;
6676 if (nary->length >= 2)
6677 rhs2 = nary->op[1];
6678 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
6679 honor_trapv, honor_nans, honor_snans,
6680 rhs2, &handled);
6681 if (handled && ret)
6682 return true;
6684 for (i = 0; i < nary->length; ++i)
6685 if (tree_could_trap_p (nary->op[i]))
6686 return true;
6688 return false;
6691 /* Return true if the reference operation REF may trap. */
6693 bool
6694 vn_reference_may_trap (vn_reference_t ref)
6696 switch (ref->operands[0].opcode)
6698 case MODIFY_EXPR:
6699 case CALL_EXPR:
6700 /* We do not handle calls. */
6701 return true;
6702 case ADDR_EXPR:
6703 /* And toplevel address computations never trap. */
6704 return false;
6705 default:;
6708 vn_reference_op_t op;
6709 unsigned i;
6710 FOR_EACH_VEC_ELT (ref->operands, i, op)
6712 switch (op->opcode)
6714 case WITH_SIZE_EXPR:
6715 case TARGET_MEM_REF:
6716 /* Always variable. */
6717 return true;
6718 case COMPONENT_REF:
6719 if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
6720 return true;
6721 break;
6722 case ARRAY_RANGE_REF:
6723 if (TREE_CODE (op->op0) == SSA_NAME)
6724 return true;
6725 break;
6726 case ARRAY_REF:
6728 if (TREE_CODE (op->op0) != INTEGER_CST)
6729 return true;
6731 /* !in_array_bounds */
6732 tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
6733 if (!domain_type)
6734 return true;
6736 tree min = op->op1;
6737 tree max = TYPE_MAX_VALUE (domain_type);
6738 if (!min
6739 || !max
6740 || TREE_CODE (min) != INTEGER_CST
6741 || TREE_CODE (max) != INTEGER_CST)
6742 return true;
6744 if (tree_int_cst_lt (op->op0, min)
6745 || tree_int_cst_lt (max, op->op0))
6746 return true;
6748 break;
6750 case MEM_REF:
6751 /* Nothing interesting in itself, the base is separate. */
6752 break;
6753 /* The following are the address bases. */
6754 case SSA_NAME:
6755 return true;
6756 case ADDR_EXPR:
6757 if (op->op0)
6758 return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
6759 return false;
6760 default:;
6763 return false;
6766 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
6767 bitmap inserted_exprs_)
6768 : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
6769 el_todo (0), eliminations (0), insertions (0),
6770 inserted_exprs (inserted_exprs_)
6772 need_eh_cleanup = BITMAP_ALLOC (NULL);
6773 need_ab_cleanup = BITMAP_ALLOC (NULL);
6776 eliminate_dom_walker::~eliminate_dom_walker ()
6778 BITMAP_FREE (need_eh_cleanup);
6779 BITMAP_FREE (need_ab_cleanup);
6782 /* Return a leader for OP that is available at the current point of the
6783 eliminate domwalk. */
6785 tree
6786 eliminate_dom_walker::eliminate_avail (basic_block, tree op)
6788 tree valnum = VN_INFO (op)->valnum;
6789 if (TREE_CODE (valnum) == SSA_NAME)
6791 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6792 return valnum;
6793 if (avail.length () > SSA_NAME_VERSION (valnum))
6795 tree av = avail[SSA_NAME_VERSION (valnum)];
6796 /* When PRE discovers a new redundancy there's no way to unite
6797 the value classes so it instead inserts a copy old-val = new-val.
6798 Look through such copies here, providing one more level of
6799 simplification at elimination time. */
6800 gassign *ass;
6801 if (av && (ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (av))))
6802 if (gimple_assign_rhs_class (ass) == GIMPLE_SINGLE_RHS)
6804 tree rhs1 = gimple_assign_rhs1 (ass);
6805 if (CONSTANT_CLASS_P (rhs1)
6806 || (TREE_CODE (rhs1) == SSA_NAME
6807 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6808 av = rhs1;
6810 return av;
6813 else if (is_gimple_min_invariant (valnum))
6814 return valnum;
6815 return NULL_TREE;
6818 /* At the current point of the eliminate domwalk make OP available. */
6820 void
6821 eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
6823 tree valnum = VN_INFO (op)->valnum;
6824 if (TREE_CODE (valnum) == SSA_NAME)
6826 if (avail.length () <= SSA_NAME_VERSION (valnum))
6827 avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
6828 tree pushop = op;
6829 if (avail[SSA_NAME_VERSION (valnum)])
6830 pushop = avail[SSA_NAME_VERSION (valnum)];
6831 avail_stack.safe_push (pushop);
6832 avail[SSA_NAME_VERSION (valnum)] = op;
6836 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
6837 the leader for the expression if insertion was successful. */
6839 tree
6840 eliminate_dom_walker::eliminate_insert (basic_block bb,
6841 gimple_stmt_iterator *gsi, tree val)
6843 /* We can insert a sequence with a single assignment only. */
6844 gimple_seq stmts = VN_INFO (val)->expr;
6845 if (!gimple_seq_singleton_p (stmts))
6846 return NULL_TREE;
6847 gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
6848 if (!stmt
6849 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
6850 && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
6851 && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
6852 && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
6853 && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6854 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
6855 return NULL_TREE;
6857 tree op = gimple_assign_rhs1 (stmt);
6858 if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
6859 || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6860 op = TREE_OPERAND (op, 0);
6861 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
6862 if (!leader)
6863 return NULL_TREE;
6865 tree res;
6866 stmts = NULL;
6867 if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6868 res = gimple_build (&stmts, BIT_FIELD_REF,
6869 TREE_TYPE (val), leader,
6870 TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
6871 TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
6872 else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
6873 res = gimple_build (&stmts, BIT_AND_EXPR,
6874 TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
6875 else
6876 res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
6877 TREE_TYPE (val), leader);
6878 if (TREE_CODE (res) != SSA_NAME
6879 || SSA_NAME_IS_DEFAULT_DEF (res)
6880 || gimple_bb (SSA_NAME_DEF_STMT (res)))
6882 gimple_seq_discard (stmts);
6884 /* During propagation we have to treat SSA info conservatively
6885 and thus we can end up simplifying the inserted expression
6886 at elimination time to sth not defined in stmts. */
6887 /* But then this is a redundancy we failed to detect. Which means
6888 res now has two values. That doesn't play well with how
6889 we track availability here, so give up. */
6890 if (dump_file && (dump_flags & TDF_DETAILS))
6892 if (TREE_CODE (res) == SSA_NAME)
6893 res = eliminate_avail (bb, res);
6894 if (res)
6896 fprintf (dump_file, "Failed to insert expression for value ");
6897 print_generic_expr (dump_file, val);
6898 fprintf (dump_file, " which is really fully redundant to ");
6899 print_generic_expr (dump_file, res);
6900 fprintf (dump_file, "\n");
6904 return NULL_TREE;
6906 else
6908 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6909 vn_ssa_aux_t vn_info = VN_INFO (res);
6910 vn_info->valnum = val;
6911 vn_info->visited = true;
6914 insertions++;
6915 if (dump_file && (dump_flags & TDF_DETAILS))
6917 fprintf (dump_file, "Inserted ");
6918 print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
6921 return res;
6924 void
6925 eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
6927 tree sprime = NULL_TREE;
6928 gimple *stmt = gsi_stmt (*gsi);
6929 tree lhs = gimple_get_lhs (stmt);
6930 if (lhs && TREE_CODE (lhs) == SSA_NAME
6931 && !gimple_has_volatile_ops (stmt)
6932 /* See PR43491. Do not replace a global register variable when
6933 it is a the RHS of an assignment. Do replace local register
6934 variables since gcc does not guarantee a local variable will
6935 be allocated in register.
6936 ??? The fix isn't effective here. This should instead
6937 be ensured by not value-numbering them the same but treating
6938 them like volatiles? */
6939 && !(gimple_assign_single_p (stmt)
6940 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
6941 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
6942 && is_global_var (gimple_assign_rhs1 (stmt)))))
6944 sprime = eliminate_avail (b, lhs);
6945 if (!sprime)
6947 /* If there is no existing usable leader but SCCVN thinks
6948 it has an expression it wants to use as replacement,
6949 insert that. */
6950 tree val = VN_INFO (lhs)->valnum;
6951 vn_ssa_aux_t vn_info;
6952 if (val != VN_TOP
6953 && TREE_CODE (val) == SSA_NAME
6954 && (vn_info = VN_INFO (val), true)
6955 && vn_info->needs_insertion
6956 && vn_info->expr != NULL
6957 && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
6958 eliminate_push_avail (b, sprime);
6961 /* If this now constitutes a copy duplicate points-to
6962 and range info appropriately. This is especially
6963 important for inserted code. See tree-ssa-copy.cc
6964 for similar code. */
6965 if (sprime
6966 && TREE_CODE (sprime) == SSA_NAME)
6968 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
6969 if (POINTER_TYPE_P (TREE_TYPE (lhs))
6970 && SSA_NAME_PTR_INFO (lhs)
6971 && ! SSA_NAME_PTR_INFO (sprime))
6973 duplicate_ssa_name_ptr_info (sprime,
6974 SSA_NAME_PTR_INFO (lhs));
6975 if (b != sprime_b)
6976 reset_flow_sensitive_info (sprime);
6978 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6979 && SSA_NAME_RANGE_INFO (lhs)
6980 && ! SSA_NAME_RANGE_INFO (sprime)
6981 && b == sprime_b)
6982 duplicate_ssa_name_range_info (sprime, lhs);
6985 /* Inhibit the use of an inserted PHI on a loop header when
6986 the address of the memory reference is a simple induction
6987 variable. In other cases the vectorizer won't do anything
6988 anyway (either it's loop invariant or a complicated
6989 expression). */
6990 if (sprime
6991 && TREE_CODE (sprime) == SSA_NAME
6992 && do_pre
6993 && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
6994 && loop_outer (b->loop_father)
6995 && has_zero_uses (sprime)
6996 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
6997 && gimple_assign_load_p (stmt))
6999 gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
7000 basic_block def_bb = gimple_bb (def_stmt);
7001 if (gimple_code (def_stmt) == GIMPLE_PHI
7002 && def_bb->loop_father->header == def_bb)
7004 loop_p loop = def_bb->loop_father;
7005 ssa_op_iter iter;
7006 tree op;
7007 bool found = false;
7008 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
7010 affine_iv iv;
7011 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
7012 if (def_bb
7013 && flow_bb_inside_loop_p (loop, def_bb)
7014 && simple_iv (loop, loop, op, &iv, true))
7016 found = true;
7017 break;
7020 if (found)
7022 if (dump_file && (dump_flags & TDF_DETAILS))
7024 fprintf (dump_file, "Not replacing ");
7025 print_gimple_expr (dump_file, stmt, 0);
7026 fprintf (dump_file, " with ");
7027 print_generic_expr (dump_file, sprime);
7028 fprintf (dump_file, " which would add a loop"
7029 " carried dependence to loop %d\n",
7030 loop->num);
7032 /* Don't keep sprime available. */
7033 sprime = NULL_TREE;
7038 if (sprime)
7040 /* If we can propagate the value computed for LHS into
7041 all uses don't bother doing anything with this stmt. */
7042 if (may_propagate_copy (lhs, sprime))
7044 /* Mark it for removal. */
7045 to_remove.safe_push (stmt);
7047 /* ??? Don't count copy/constant propagations. */
7048 if (gimple_assign_single_p (stmt)
7049 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7050 || gimple_assign_rhs1 (stmt) == sprime))
7051 return;
7053 if (dump_file && (dump_flags & TDF_DETAILS))
7055 fprintf (dump_file, "Replaced ");
7056 print_gimple_expr (dump_file, stmt, 0);
7057 fprintf (dump_file, " with ");
7058 print_generic_expr (dump_file, sprime);
7059 fprintf (dump_file, " in all uses of ");
7060 print_gimple_stmt (dump_file, stmt, 0);
7063 eliminations++;
7064 return;
7067 /* If this is an assignment from our leader (which
7068 happens in the case the value-number is a constant)
7069 then there is nothing to do. Likewise if we run into
7070 inserted code that needed a conversion because of
7071 our type-agnostic value-numbering of loads. */
7072 if ((gimple_assign_single_p (stmt)
7073 || (is_gimple_assign (stmt)
7074 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
7075 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
7076 && sprime == gimple_assign_rhs1 (stmt))
7077 return;
7079 /* Else replace its RHS. */
7080 if (dump_file && (dump_flags & TDF_DETAILS))
7082 fprintf (dump_file, "Replaced ");
7083 print_gimple_expr (dump_file, stmt, 0);
7084 fprintf (dump_file, " with ");
7085 print_generic_expr (dump_file, sprime);
7086 fprintf (dump_file, " in ");
7087 print_gimple_stmt (dump_file, stmt, 0);
7089 eliminations++;
7091 bool can_make_abnormal_goto = (is_gimple_call (stmt)
7092 && stmt_can_make_abnormal_goto (stmt));
7093 gimple *orig_stmt = stmt;
7094 if (!useless_type_conversion_p (TREE_TYPE (lhs),
7095 TREE_TYPE (sprime)))
7097 /* We preserve conversions to but not from function or method
7098 types. This asymmetry makes it necessary to re-instantiate
7099 conversions here. */
7100 if (POINTER_TYPE_P (TREE_TYPE (lhs))
7101 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
7102 sprime = fold_convert (TREE_TYPE (lhs), sprime);
7103 else
7104 gcc_unreachable ();
7106 tree vdef = gimple_vdef (stmt);
7107 tree vuse = gimple_vuse (stmt);
7108 propagate_tree_value_into_stmt (gsi, sprime);
7109 stmt = gsi_stmt (*gsi);
7110 update_stmt (stmt);
7111 /* In case the VDEF on the original stmt was released, value-number
7112 it to the VUSE. This is to make vuse_ssa_val able to skip
7113 released virtual operands. */
7114 if (vdef != gimple_vdef (stmt))
7116 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
7117 VN_INFO (vdef)->valnum = vuse;
7120 /* If we removed EH side-effects from the statement, clean
7121 its EH information. */
7122 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
7124 bitmap_set_bit (need_eh_cleanup,
7125 gimple_bb (stmt)->index);
7126 if (dump_file && (dump_flags & TDF_DETAILS))
7127 fprintf (dump_file, " Removed EH side-effects.\n");
7130 /* Likewise for AB side-effects. */
7131 if (can_make_abnormal_goto
7132 && !stmt_can_make_abnormal_goto (stmt))
7134 bitmap_set_bit (need_ab_cleanup,
7135 gimple_bb (stmt)->index);
7136 if (dump_file && (dump_flags & TDF_DETAILS))
7137 fprintf (dump_file, " Removed AB side-effects.\n");
7140 return;
7144 /* If the statement is a scalar store, see if the expression
7145 has the same value number as its rhs. If so, the store is
7146 dead. */
7147 if (gimple_assign_single_p (stmt)
7148 && !gimple_has_volatile_ops (stmt)
7149 && !is_gimple_reg (gimple_assign_lhs (stmt))
7150 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
7151 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
7153 tree rhs = gimple_assign_rhs1 (stmt);
7154 vn_reference_t vnresult;
7155 /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
7156 typed load of a byte known to be 0x11 as 1 so a store of
7157 a boolean 1 is detected as redundant. Because of this we
7158 have to make sure to lookup with a ref where its size
7159 matches the precision. */
7160 tree lookup_lhs = lhs;
7161 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7162 && (TREE_CODE (lhs) != COMPONENT_REF
7163 || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
7164 && !type_has_mode_precision_p (TREE_TYPE (lhs)))
7166 if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
7167 && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
7168 lookup_lhs = NULL_TREE;
7169 else if (TREE_CODE (lhs) == COMPONENT_REF
7170 || TREE_CODE (lhs) == MEM_REF)
7172 tree ltype = build_nonstandard_integer_type
7173 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
7174 TYPE_UNSIGNED (TREE_TYPE (lhs)));
7175 if (TREE_CODE (lhs) == COMPONENT_REF)
7177 tree foff = component_ref_field_offset (lhs);
7178 tree f = TREE_OPERAND (lhs, 1);
7179 if (!poly_int_tree_p (foff))
7180 lookup_lhs = NULL_TREE;
7181 else
7182 lookup_lhs = build3 (BIT_FIELD_REF, ltype,
7183 TREE_OPERAND (lhs, 0),
7184 TYPE_SIZE (TREE_TYPE (lhs)),
7185 bit_from_pos
7186 (foff, DECL_FIELD_BIT_OFFSET (f)));
7188 else
7189 lookup_lhs = build2 (MEM_REF, ltype,
7190 TREE_OPERAND (lhs, 0),
7191 TREE_OPERAND (lhs, 1));
7193 else
7194 lookup_lhs = NULL_TREE;
7196 tree val = NULL_TREE;
7197 if (lookup_lhs)
7198 val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
7199 VN_WALKREWRITE, &vnresult, false,
7200 NULL, NULL_TREE, true);
7201 if (TREE_CODE (rhs) == SSA_NAME)
7202 rhs = VN_INFO (rhs)->valnum;
7203 if (val
7204 && (operand_equal_p (val, rhs, 0)
7205 /* Due to the bitfield lookups above we can get bit
7206 interpretations of the same RHS as values here. Those
7207 are redundant as well. */
7208 || (TREE_CODE (val) == SSA_NAME
7209 && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
7210 && (val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
7211 && TREE_CODE (val) == VIEW_CONVERT_EXPR
7212 && TREE_OPERAND (val, 0) == rhs)))
7214 /* We can only remove the later store if the former aliases
7215 at least all accesses the later one does or if the store
7216 was to readonly memory storing the same value. */
7217 ao_ref lhs_ref;
7218 ao_ref_init (&lhs_ref, lhs);
7219 alias_set_type set = ao_ref_alias_set (&lhs_ref);
7220 alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
7221 if (! vnresult
7222 || ((vnresult->set == set
7223 || alias_set_subset_of (set, vnresult->set))
7224 && (vnresult->base_set == base_set
7225 || alias_set_subset_of (base_set, vnresult->base_set))))
7227 if (dump_file && (dump_flags & TDF_DETAILS))
7229 fprintf (dump_file, "Deleted redundant store ");
7230 print_gimple_stmt (dump_file, stmt, 0);
7233 /* Queue stmt for removal. */
7234 to_remove.safe_push (stmt);
7235 return;
7240 /* If this is a control statement value numbering left edges
7241 unexecuted on force the condition in a way consistent with
7242 that. */
7243 if (gcond *cond = dyn_cast <gcond *> (stmt))
7245 if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
7246 ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
7248 if (dump_file && (dump_flags & TDF_DETAILS))
7250 fprintf (dump_file, "Removing unexecutable edge from ");
7251 print_gimple_stmt (dump_file, stmt, 0);
7253 if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
7254 == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
7255 gimple_cond_make_true (cond);
7256 else
7257 gimple_cond_make_false (cond);
7258 update_stmt (cond);
7259 el_todo |= TODO_cleanup_cfg;
7260 return;
7264 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
7265 bool was_noreturn = (is_gimple_call (stmt)
7266 && gimple_call_noreturn_p (stmt));
7267 tree vdef = gimple_vdef (stmt);
7268 tree vuse = gimple_vuse (stmt);
7270 /* If we didn't replace the whole stmt (or propagate the result
7271 into all uses), replace all uses on this stmt with their
7272 leaders. */
7273 bool modified = false;
7274 use_operand_p use_p;
7275 ssa_op_iter iter;
7276 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
7278 tree use = USE_FROM_PTR (use_p);
7279 /* ??? The call code above leaves stmt operands un-updated. */
7280 if (TREE_CODE (use) != SSA_NAME)
7281 continue;
7282 tree sprime;
7283 if (SSA_NAME_IS_DEFAULT_DEF (use))
7284 /* ??? For default defs BB shouldn't matter, but we have to
7285 solve the inconsistency between rpo eliminate and
7286 dom eliminate avail valueization first. */
7287 sprime = eliminate_avail (b, use);
7288 else
7289 /* Look for sth available at the definition block of the argument.
7290 This avoids inconsistencies between availability there which
7291 decides if the stmt can be removed and availability at the
7292 use site. The SSA property ensures that things available
7293 at the definition are also available at uses. */
7294 sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
7295 if (sprime && sprime != use
7296 && may_propagate_copy (use, sprime, true)
7297 /* We substitute into debug stmts to avoid excessive
7298 debug temporaries created by removed stmts, but we need
7299 to avoid doing so for inserted sprimes as we never want
7300 to create debug temporaries for them. */
7301 && (!inserted_exprs
7302 || TREE_CODE (sprime) != SSA_NAME
7303 || !is_gimple_debug (stmt)
7304 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
7306 propagate_value (use_p, sprime);
7307 modified = true;
7311 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
7312 into which is a requirement for the IPA devirt machinery. */
7313 gimple *old_stmt = stmt;
7314 if (modified)
7316 /* If a formerly non-invariant ADDR_EXPR is turned into an
7317 invariant one it was on a separate stmt. */
7318 if (gimple_assign_single_p (stmt)
7319 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
7320 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
7321 gimple_stmt_iterator prev = *gsi;
7322 gsi_prev (&prev);
7323 if (fold_stmt (gsi, follow_all_ssa_edges))
7325 /* fold_stmt may have created new stmts inbetween
7326 the previous stmt and the folded stmt. Mark
7327 all defs created there as varying to not confuse
7328 the SCCVN machinery as we're using that even during
7329 elimination. */
7330 if (gsi_end_p (prev))
7331 prev = gsi_start_bb (b);
7332 else
7333 gsi_next (&prev);
7334 if (gsi_stmt (prev) != gsi_stmt (*gsi))
7337 tree def;
7338 ssa_op_iter dit;
7339 FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
7340 dit, SSA_OP_ALL_DEFS)
7341 /* As existing DEFs may move between stmts
7342 only process new ones. */
7343 if (! has_VN_INFO (def))
7345 vn_ssa_aux_t vn_info = VN_INFO (def);
7346 vn_info->valnum = def;
7347 vn_info->visited = true;
7349 if (gsi_stmt (prev) == gsi_stmt (*gsi))
7350 break;
7351 gsi_next (&prev);
7353 while (1);
7355 stmt = gsi_stmt (*gsi);
7356 /* In case we folded the stmt away schedule the NOP for removal. */
7357 if (gimple_nop_p (stmt))
7358 to_remove.safe_push (stmt);
7361 /* Visit indirect calls and turn them into direct calls if
7362 possible using the devirtualization machinery. Do this before
7363 checking for required EH/abnormal/noreturn cleanup as devird
7364 may expose more of those. */
7365 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
7367 tree fn = gimple_call_fn (call_stmt);
7368 if (fn
7369 && flag_devirtualize
7370 && virtual_method_call_p (fn))
7372 tree otr_type = obj_type_ref_class (fn);
7373 unsigned HOST_WIDE_INT otr_tok
7374 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
7375 tree instance;
7376 ipa_polymorphic_call_context context (current_function_decl,
7377 fn, stmt, &instance);
7378 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
7379 otr_type, stmt, NULL);
7380 bool final;
7381 vec <cgraph_node *> targets
7382 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
7383 otr_tok, context, &final);
7384 if (dump_file)
7385 dump_possible_polymorphic_call_targets (dump_file,
7386 obj_type_ref_class (fn),
7387 otr_tok, context);
7388 if (final && targets.length () <= 1 && dbg_cnt (devirt))
7390 tree fn;
7391 if (targets.length () == 1)
7392 fn = targets[0]->decl;
7393 else
7394 fn = builtin_decl_unreachable ();
7395 if (dump_enabled_p ())
7397 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
7398 "converting indirect call to "
7399 "function %s\n",
7400 lang_hooks.decl_printable_name (fn, 2));
7402 gimple_call_set_fndecl (call_stmt, fn);
7403 /* If changing the call to __builtin_unreachable
7404 or similar noreturn function, adjust gimple_call_fntype
7405 too. */
7406 if (gimple_call_noreturn_p (call_stmt)
7407 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
7408 && TYPE_ARG_TYPES (TREE_TYPE (fn))
7409 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
7410 == void_type_node))
7411 gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
7412 maybe_remove_unused_call_args (cfun, call_stmt);
7413 modified = true;
7418 if (modified)
7420 /* When changing a call into a noreturn call, cfg cleanup
7421 is needed to fix up the noreturn call. */
7422 if (!was_noreturn
7423 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
7424 to_fixup.safe_push (stmt);
7425 /* When changing a condition or switch into one we know what
7426 edge will be executed, schedule a cfg cleanup. */
7427 if ((gimple_code (stmt) == GIMPLE_COND
7428 && (gimple_cond_true_p (as_a <gcond *> (stmt))
7429 || gimple_cond_false_p (as_a <gcond *> (stmt))))
7430 || (gimple_code (stmt) == GIMPLE_SWITCH
7431 && TREE_CODE (gimple_switch_index
7432 (as_a <gswitch *> (stmt))) == INTEGER_CST))
7433 el_todo |= TODO_cleanup_cfg;
7434 /* If we removed EH side-effects from the statement, clean
7435 its EH information. */
7436 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
7438 bitmap_set_bit (need_eh_cleanup,
7439 gimple_bb (stmt)->index);
7440 if (dump_file && (dump_flags & TDF_DETAILS))
7441 fprintf (dump_file, " Removed EH side-effects.\n");
7443 /* Likewise for AB side-effects. */
7444 if (can_make_abnormal_goto
7445 && !stmt_can_make_abnormal_goto (stmt))
7447 bitmap_set_bit (need_ab_cleanup,
7448 gimple_bb (stmt)->index);
7449 if (dump_file && (dump_flags & TDF_DETAILS))
7450 fprintf (dump_file, " Removed AB side-effects.\n");
7452 update_stmt (stmt);
7453 /* In case the VDEF on the original stmt was released, value-number
7454 it to the VUSE. This is to make vuse_ssa_val able to skip
7455 released virtual operands. */
7456 if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
7457 VN_INFO (vdef)->valnum = vuse;
7460 /* Make new values available - for fully redundant LHS we
7461 continue with the next stmt above and skip this.
7462 But avoid picking up dead defs. */
7463 tree def;
7464 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7465 if (! has_zero_uses (def)
7466 || (inserted_exprs
7467 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (def))))
7468 eliminate_push_avail (b, def);
7471 /* Perform elimination for the basic-block B during the domwalk. */
7473 edge
7474 eliminate_dom_walker::before_dom_children (basic_block b)
7476 /* Mark new bb. */
7477 avail_stack.safe_push (NULL_TREE);
7479 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
7480 if (!(b->flags & BB_EXECUTABLE))
7481 return NULL;
7483 vn_context_bb = b;
7485 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
7487 gphi *phi = gsi.phi ();
7488 tree res = PHI_RESULT (phi);
7490 if (virtual_operand_p (res))
7492 gsi_next (&gsi);
7493 continue;
7496 tree sprime = eliminate_avail (b, res);
7497 if (sprime
7498 && sprime != res)
7500 if (dump_file && (dump_flags & TDF_DETAILS))
7502 fprintf (dump_file, "Replaced redundant PHI node defining ");
7503 print_generic_expr (dump_file, res);
7504 fprintf (dump_file, " with ");
7505 print_generic_expr (dump_file, sprime);
7506 fprintf (dump_file, "\n");
7509 /* If we inserted this PHI node ourself, it's not an elimination. */
7510 if (! inserted_exprs
7511 || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
7512 eliminations++;
7514 /* If we will propagate into all uses don't bother to do
7515 anything. */
7516 if (may_propagate_copy (res, sprime))
7518 /* Mark the PHI for removal. */
7519 to_remove.safe_push (phi);
7520 gsi_next (&gsi);
7521 continue;
7524 remove_phi_node (&gsi, false);
7526 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
7527 sprime = fold_convert (TREE_TYPE (res), sprime);
7528 gimple *stmt = gimple_build_assign (res, sprime);
7529 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
7530 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
7531 continue;
7534 eliminate_push_avail (b, res);
7535 gsi_next (&gsi);
7538 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
7539 !gsi_end_p (gsi);
7540 gsi_next (&gsi))
7541 eliminate_stmt (b, &gsi);
7543 /* Replace destination PHI arguments. */
7544 edge_iterator ei;
7545 edge e;
7546 FOR_EACH_EDGE (e, ei, b->succs)
7547 if (e->flags & EDGE_EXECUTABLE)
7548 for (gphi_iterator gsi = gsi_start_phis (e->dest);
7549 !gsi_end_p (gsi);
7550 gsi_next (&gsi))
7552 gphi *phi = gsi.phi ();
7553 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7554 tree arg = USE_FROM_PTR (use_p);
7555 if (TREE_CODE (arg) != SSA_NAME
7556 || virtual_operand_p (arg))
7557 continue;
7558 tree sprime = eliminate_avail (b, arg);
7559 if (sprime && may_propagate_copy (arg, sprime,
7560 !(e->flags & EDGE_ABNORMAL)))
7561 propagate_value (use_p, sprime);
7564 vn_context_bb = NULL;
7566 return NULL;
7569 /* Make no longer available leaders no longer available. */
7571 void
7572 eliminate_dom_walker::after_dom_children (basic_block)
7574 tree entry;
7575 while ((entry = avail_stack.pop ()) != NULL_TREE)
7577 tree valnum = VN_INFO (entry)->valnum;
7578 tree old = avail[SSA_NAME_VERSION (valnum)];
7579 if (old == entry)
7580 avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
7581 else
7582 avail[SSA_NAME_VERSION (valnum)] = entry;
7586 /* Remove queued stmts and perform delayed cleanups. */
7588 unsigned
7589 eliminate_dom_walker::eliminate_cleanup (bool region_p)
7591 statistics_counter_event (cfun, "Eliminated", eliminations);
7592 statistics_counter_event (cfun, "Insertions", insertions);
7594 /* We cannot remove stmts during BB walk, especially not release SSA
7595 names there as this confuses the VN machinery. The stmts ending
7596 up in to_remove are either stores or simple copies.
7597 Remove stmts in reverse order to make debug stmt creation possible. */
7598 while (!to_remove.is_empty ())
7600 bool do_release_defs = true;
7601 gimple *stmt = to_remove.pop ();
7603 /* When we are value-numbering a region we do not require exit PHIs to
7604 be present so we have to make sure to deal with uses outside of the
7605 region of stmts that we thought are eliminated.
7606 ??? Note we may be confused by uses in dead regions we didn't run
7607 elimination on. Rather than checking individual uses we accept
7608 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
7609 contains such example). */
7610 if (region_p)
7612 if (gphi *phi = dyn_cast <gphi *> (stmt))
7614 tree lhs = gimple_phi_result (phi);
7615 if (!has_zero_uses (lhs))
7617 if (dump_file && (dump_flags & TDF_DETAILS))
7618 fprintf (dump_file, "Keeping eliminated stmt live "
7619 "as copy because of out-of-region uses\n");
7620 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7621 gimple *copy = gimple_build_assign (lhs, sprime);
7622 gimple_stmt_iterator gsi
7623 = gsi_after_labels (gimple_bb (stmt));
7624 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7625 do_release_defs = false;
7628 else if (tree lhs = gimple_get_lhs (stmt))
7629 if (TREE_CODE (lhs) == SSA_NAME
7630 && !has_zero_uses (lhs))
7632 if (dump_file && (dump_flags & TDF_DETAILS))
7633 fprintf (dump_file, "Keeping eliminated stmt live "
7634 "as copy because of out-of-region uses\n");
7635 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
7636 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7637 if (is_gimple_assign (stmt))
7639 gimple_assign_set_rhs_from_tree (&gsi, sprime);
7640 stmt = gsi_stmt (gsi);
7641 update_stmt (stmt);
7642 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
7643 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
7644 continue;
7646 else
7648 gimple *copy = gimple_build_assign (lhs, sprime);
7649 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
7650 do_release_defs = false;
7655 if (dump_file && (dump_flags & TDF_DETAILS))
7657 fprintf (dump_file, "Removing dead stmt ");
7658 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
7661 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
7662 if (gimple_code (stmt) == GIMPLE_PHI)
7663 remove_phi_node (&gsi, do_release_defs);
7664 else
7666 basic_block bb = gimple_bb (stmt);
7667 unlink_stmt_vdef (stmt);
7668 if (gsi_remove (&gsi, true))
7669 bitmap_set_bit (need_eh_cleanup, bb->index);
7670 if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
7671 bitmap_set_bit (need_ab_cleanup, bb->index);
7672 if (do_release_defs)
7673 release_defs (stmt);
7676 /* Removing a stmt may expose a forwarder block. */
7677 el_todo |= TODO_cleanup_cfg;
7680 /* Fixup stmts that became noreturn calls. This may require splitting
7681 blocks and thus isn't possible during the dominator walk. Do this
7682 in reverse order so we don't inadvertedly remove a stmt we want to
7683 fixup by visiting a dominating now noreturn call first. */
7684 while (!to_fixup.is_empty ())
7686 gimple *stmt = to_fixup.pop ();
7688 if (dump_file && (dump_flags & TDF_DETAILS))
7690 fprintf (dump_file, "Fixing up noreturn call ");
7691 print_gimple_stmt (dump_file, stmt, 0);
7694 if (fixup_noreturn_call (stmt))
7695 el_todo |= TODO_cleanup_cfg;
7698 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
7699 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
7701 if (do_eh_cleanup)
7702 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
7704 if (do_ab_cleanup)
7705 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
7707 if (do_eh_cleanup || do_ab_cleanup)
7708 el_todo |= TODO_cleanup_cfg;
7710 return el_todo;
7713 /* Eliminate fully redundant computations. */
7715 unsigned
7716 eliminate_with_rpo_vn (bitmap inserted_exprs)
7718 eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
7720 eliminate_dom_walker *saved_rpo_avail = rpo_avail;
7721 rpo_avail = &walker;
7722 walker.walk (cfun->cfg->x_entry_block_ptr);
7723 rpo_avail = saved_rpo_avail;
7725 return walker.eliminate_cleanup ();
7728 static unsigned
7729 do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
7730 bool iterate, bool eliminate, bool skip_entry_phis,
7731 vn_lookup_kind kind);
7733 void
7734 run_rpo_vn (vn_lookup_kind kind)
7736 do_rpo_vn_1 (cfun, NULL, NULL, true, false, false, kind);
7738 /* ??? Prune requirement of these. */
7739 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
7741 /* Initialize the value ids and prune out remaining VN_TOPs
7742 from dead code. */
7743 tree name;
7744 unsigned i;
7745 FOR_EACH_SSA_NAME (i, name, cfun)
7747 vn_ssa_aux_t info = VN_INFO (name);
7748 if (!info->visited
7749 || info->valnum == VN_TOP)
7750 info->valnum = name;
7751 if (info->valnum == name)
7752 info->value_id = get_next_value_id ();
7753 else if (is_gimple_min_invariant (info->valnum))
7754 info->value_id = get_or_alloc_constant_value_id (info->valnum);
7757 /* Propagate. */
7758 FOR_EACH_SSA_NAME (i, name, cfun)
7760 vn_ssa_aux_t info = VN_INFO (name);
7761 if (TREE_CODE (info->valnum) == SSA_NAME
7762 && info->valnum != name
7763 && info->value_id != VN_INFO (info->valnum)->value_id)
7764 info->value_id = VN_INFO (info->valnum)->value_id;
7767 set_hashtable_value_ids ();
7769 if (dump_file && (dump_flags & TDF_DETAILS))
7771 fprintf (dump_file, "Value numbers:\n");
7772 FOR_EACH_SSA_NAME (i, name, cfun)
7774 if (VN_INFO (name)->visited
7775 && SSA_VAL (name) != name)
7777 print_generic_expr (dump_file, name);
7778 fprintf (dump_file, " = ");
7779 print_generic_expr (dump_file, SSA_VAL (name));
7780 fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
7786 /* Free VN associated data structures. */
7788 void
7789 free_rpo_vn (void)
7791 free_vn_table (valid_info);
7792 XDELETE (valid_info);
7793 obstack_free (&vn_tables_obstack, NULL);
7794 obstack_free (&vn_tables_insert_obstack, NULL);
7796 vn_ssa_aux_iterator_type it;
7797 vn_ssa_aux_t info;
7798 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
7799 if (info->needs_insertion)
7800 release_ssa_name (info->name);
7801 obstack_free (&vn_ssa_aux_obstack, NULL);
7802 delete vn_ssa_aux_hash;
7804 delete constant_to_value_id;
7805 constant_to_value_id = NULL;
7808 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
7810 static tree
7811 vn_lookup_simplify_result (gimple_match_op *res_op)
7813 if (!res_op->code.is_tree_code ())
7814 return NULL_TREE;
7815 tree *ops = res_op->ops;
7816 unsigned int length = res_op->num_ops;
7817 if (res_op->code == CONSTRUCTOR
7818 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
7819 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
7820 && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
7822 length = CONSTRUCTOR_NELTS (res_op->ops[0]);
7823 ops = XALLOCAVEC (tree, length);
7824 for (unsigned i = 0; i < length; ++i)
7825 ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
7827 vn_nary_op_t vnresult = NULL;
7828 tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
7829 res_op->type, ops, &vnresult);
7830 /* If this is used from expression simplification make sure to
7831 return an available expression. */
7832 if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
7833 res = rpo_avail->eliminate_avail (vn_context_bb, res);
7834 return res;
7837 /* Return a leader for OPs value that is valid at BB. */
7839 tree
7840 rpo_elim::eliminate_avail (basic_block bb, tree op)
7842 bool visited;
7843 tree valnum = SSA_VAL (op, &visited);
7844 /* If we didn't visit OP then it must be defined outside of the
7845 region we process and also dominate it. So it is available. */
7846 if (!visited)
7847 return op;
7848 if (TREE_CODE (valnum) == SSA_NAME)
7850 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
7851 return valnum;
7852 vn_ssa_aux_t valnum_info = VN_INFO (valnum);
7853 vn_avail *av = valnum_info->avail;
7854 if (!av)
7856 /* See above. But when there's availability info prefer
7857 what we recorded there for example to preserve LC SSA. */
7858 if (!valnum_info->visited)
7859 return valnum;
7860 return NULL_TREE;
7862 if (av->location == bb->index)
7863 /* On tramp3d 90% of the cases are here. */
7864 return ssa_name (av->leader);
7867 basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
7868 /* ??? During elimination we have to use availability at the
7869 definition site of a use we try to replace. This
7870 is required to not run into inconsistencies because
7871 of dominated_by_p_w_unex behavior and removing a definition
7872 while not replacing all uses.
7873 ??? We could try to consistently walk dominators
7874 ignoring non-executable regions. The nearest common
7875 dominator of bb and abb is where we can stop walking. We
7876 may also be able to "pre-compute" (bits of) the next immediate
7877 (non-)dominator during the RPO walk when marking edges as
7878 executable. */
7879 if (dominated_by_p_w_unex (bb, abb, true))
7881 tree leader = ssa_name (av->leader);
7882 /* Prevent eliminations that break loop-closed SSA. */
7883 if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
7884 && ! SSA_NAME_IS_DEFAULT_DEF (leader)
7885 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7886 (leader))->loop_father,
7887 bb))
7888 return NULL_TREE;
7889 if (dump_file && (dump_flags & TDF_DETAILS))
7891 print_generic_expr (dump_file, leader);
7892 fprintf (dump_file, " is available for ");
7893 print_generic_expr (dump_file, valnum);
7894 fprintf (dump_file, "\n");
7896 /* On tramp3d 99% of the _remaining_ cases succeed at
7897 the first enty. */
7898 return leader;
7900 /* ??? Can we somehow skip to the immediate dominator
7901 RPO index (bb_to_rpo)? Again, maybe not worth, on
7902 tramp3d the worst number of elements in the vector is 9. */
7903 av = av->next;
7905 while (av);
7906 /* While we prefer avail we have to fallback to using the value
7907 directly if defined outside of the region when none of the
7908 available defs suit. */
7909 if (!valnum_info->visited)
7910 return valnum;
7912 else if (valnum != VN_TOP)
7913 /* valnum is is_gimple_min_invariant. */
7914 return valnum;
7915 return NULL_TREE;
7918 /* Make LEADER a leader for its value at BB. */
7920 void
7921 rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
7923 tree valnum = VN_INFO (leader)->valnum;
7924 if (valnum == VN_TOP
7925 || is_gimple_min_invariant (valnum))
7926 return;
7927 if (dump_file && (dump_flags & TDF_DETAILS))
7929 fprintf (dump_file, "Making available beyond BB%d ", bb->index);
7930 print_generic_expr (dump_file, leader);
7931 fprintf (dump_file, " for value ");
7932 print_generic_expr (dump_file, valnum);
7933 fprintf (dump_file, "\n");
7935 vn_ssa_aux_t value = VN_INFO (valnum);
7936 vn_avail *av;
7937 if (m_avail_freelist)
7939 av = m_avail_freelist;
7940 m_avail_freelist = m_avail_freelist->next;
7942 else
7943 av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
7944 av->location = bb->index;
7945 av->leader = SSA_NAME_VERSION (leader);
7946 av->next = value->avail;
7947 av->next_undo = last_pushed_avail;
7948 last_pushed_avail = value;
7949 value->avail = av;
7952 /* Valueization hook for RPO VN plus required state. */
7954 tree
7955 rpo_vn_valueize (tree name)
7957 if (TREE_CODE (name) == SSA_NAME)
7959 vn_ssa_aux_t val = VN_INFO (name);
7960 if (val)
7962 tree tem = val->valnum;
7963 if (tem != VN_TOP && tem != name)
7965 if (TREE_CODE (tem) != SSA_NAME)
7966 return tem;
7967 /* For all values we only valueize to an available leader
7968 which means we can use SSA name info without restriction. */
7969 tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
7970 if (tem)
7971 return tem;
7975 return name;
7978 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7979 inverted condition. */
7981 static void
7982 insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
7984 switch (code)
7986 case LT_EXPR:
7987 /* a < b -> a {!,<}= b */
7988 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7989 ops, boolean_true_node, 0, pred_e);
7990 vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
7991 ops, boolean_true_node, 0, pred_e);
7992 /* a < b -> ! a {>,=} b */
7993 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7994 ops, boolean_false_node, 0, pred_e);
7995 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7996 ops, boolean_false_node, 0, pred_e);
7997 break;
7998 case GT_EXPR:
7999 /* a > b -> a {!,>}= b */
8000 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
8001 ops, boolean_true_node, 0, pred_e);
8002 vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
8003 ops, boolean_true_node, 0, pred_e);
8004 /* a > b -> ! a {<,=} b */
8005 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8006 ops, boolean_false_node, 0, pred_e);
8007 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
8008 ops, boolean_false_node, 0, pred_e);
8009 break;
8010 case EQ_EXPR:
8011 /* a == b -> ! a {<,>} b */
8012 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
8013 ops, boolean_false_node, 0, pred_e);
8014 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
8015 ops, boolean_false_node, 0, pred_e);
8016 break;
8017 case LE_EXPR:
8018 case GE_EXPR:
8019 case NE_EXPR:
8020 /* Nothing besides inverted condition. */
8021 break;
8022 default:;
8026 /* Main stmt worker for RPO VN, process BB. */
8028 static unsigned
8029 process_bb (rpo_elim &avail, basic_block bb,
8030 bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
8031 bool do_region, bitmap exit_bbs, bool skip_phis)
8033 unsigned todo = 0;
8034 edge_iterator ei;
8035 edge e;
8037 vn_context_bb = bb;
8039 /* If we are in loop-closed SSA preserve this state. This is
8040 relevant when called on regions from outside of FRE/PRE. */
8041 bool lc_phi_nodes = false;
8042 if (!skip_phis
8043 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
8044 FOR_EACH_EDGE (e, ei, bb->preds)
8045 if (e->src->loop_father != e->dest->loop_father
8046 && flow_loop_nested_p (e->dest->loop_father,
8047 e->src->loop_father))
8049 lc_phi_nodes = true;
8050 break;
8053 /* When we visit a loop header substitute into loop info. */
8054 if (!iterate && eliminate && bb->loop_father->header == bb)
8056 /* Keep fields in sync with substitute_in_loop_info. */
8057 if (bb->loop_father->nb_iterations)
8058 bb->loop_father->nb_iterations
8059 = simplify_replace_tree (bb->loop_father->nb_iterations,
8060 NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
8063 /* Value-number all defs in the basic-block. */
8064 if (!skip_phis)
8065 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8066 gsi_next (&gsi))
8068 gphi *phi = gsi.phi ();
8069 tree res = PHI_RESULT (phi);
8070 vn_ssa_aux_t res_info = VN_INFO (res);
8071 if (!bb_visited)
8073 gcc_assert (!res_info->visited);
8074 res_info->valnum = VN_TOP;
8075 res_info->visited = true;
8078 /* When not iterating force backedge values to varying. */
8079 visit_stmt (phi, !iterate_phis);
8080 if (virtual_operand_p (res))
8081 continue;
8083 /* Eliminate */
8084 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
8085 how we handle backedges and availability.
8086 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
8087 tree val = res_info->valnum;
8088 if (res != val && !iterate && eliminate)
8090 if (tree leader = avail.eliminate_avail (bb, res))
8092 if (leader != res
8093 /* Preserve loop-closed SSA form. */
8094 && (! lc_phi_nodes
8095 || is_gimple_min_invariant (leader)))
8097 if (dump_file && (dump_flags & TDF_DETAILS))
8099 fprintf (dump_file, "Replaced redundant PHI node "
8100 "defining ");
8101 print_generic_expr (dump_file, res);
8102 fprintf (dump_file, " with ");
8103 print_generic_expr (dump_file, leader);
8104 fprintf (dump_file, "\n");
8106 avail.eliminations++;
8108 if (may_propagate_copy (res, leader))
8110 /* Schedule for removal. */
8111 avail.to_remove.safe_push (phi);
8112 continue;
8114 /* ??? Else generate a copy stmt. */
8118 /* Only make defs available that not already are. But make
8119 sure loop-closed SSA PHI node defs are picked up for
8120 downstream uses. */
8121 if (lc_phi_nodes
8122 || res == val
8123 || ! avail.eliminate_avail (bb, res))
8124 avail.eliminate_push_avail (bb, res);
8127 /* For empty BBs mark outgoing edges executable. For non-empty BBs
8128 we do this when processing the last stmt as we have to do this
8129 before elimination which otherwise forces GIMPLE_CONDs to
8130 if (1 != 0) style when seeing non-executable edges. */
8131 if (gsi_end_p (gsi_start_bb (bb)))
8133 FOR_EACH_EDGE (e, ei, bb->succs)
8135 if (!(e->flags & EDGE_EXECUTABLE))
8137 if (dump_file && (dump_flags & TDF_DETAILS))
8138 fprintf (dump_file,
8139 "marking outgoing edge %d -> %d executable\n",
8140 e->src->index, e->dest->index);
8141 e->flags |= EDGE_EXECUTABLE;
8142 e->dest->flags |= BB_EXECUTABLE;
8144 else if (!(e->dest->flags & BB_EXECUTABLE))
8146 if (dump_file && (dump_flags & TDF_DETAILS))
8147 fprintf (dump_file,
8148 "marking destination block %d reachable\n",
8149 e->dest->index);
8150 e->dest->flags |= BB_EXECUTABLE;
8154 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
8155 !gsi_end_p (gsi); gsi_next (&gsi))
8157 ssa_op_iter i;
8158 tree op;
8159 if (!bb_visited)
8161 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
8163 vn_ssa_aux_t op_info = VN_INFO (op);
8164 gcc_assert (!op_info->visited);
8165 op_info->valnum = VN_TOP;
8166 op_info->visited = true;
8169 /* We somehow have to deal with uses that are not defined
8170 in the processed region. Forcing unvisited uses to
8171 varying here doesn't play well with def-use following during
8172 expression simplification, so we deal with this by checking
8173 the visited flag in SSA_VAL. */
8176 visit_stmt (gsi_stmt (gsi));
8178 gimple *last = gsi_stmt (gsi);
8179 e = NULL;
8180 switch (gimple_code (last))
8182 case GIMPLE_SWITCH:
8183 e = find_taken_edge (bb, vn_valueize (gimple_switch_index
8184 (as_a <gswitch *> (last))));
8185 break;
8186 case GIMPLE_COND:
8188 tree lhs = vn_valueize (gimple_cond_lhs (last));
8189 tree rhs = vn_valueize (gimple_cond_rhs (last));
8190 tree val = gimple_simplify (gimple_cond_code (last),
8191 boolean_type_node, lhs, rhs,
8192 NULL, vn_valueize);
8193 /* If the condition didn't simplfy see if we have recorded
8194 an expression from sofar taken edges. */
8195 if (! val || TREE_CODE (val) != INTEGER_CST)
8197 vn_nary_op_t vnresult;
8198 tree ops[2];
8199 ops[0] = lhs;
8200 ops[1] = rhs;
8201 val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
8202 boolean_type_node, ops,
8203 &vnresult);
8204 /* Did we get a predicated value? */
8205 if (! val && vnresult && vnresult->predicated_values)
8207 val = vn_nary_op_get_predicated_value (vnresult, bb);
8208 if (val && dump_file && (dump_flags & TDF_DETAILS))
8210 fprintf (dump_file, "Got predicated value ");
8211 print_generic_expr (dump_file, val, TDF_NONE);
8212 fprintf (dump_file, " for ");
8213 print_gimple_stmt (dump_file, last, TDF_SLIM);
8217 if (val)
8218 e = find_taken_edge (bb, val);
8219 if (! e)
8221 /* If we didn't manage to compute the taken edge then
8222 push predicated expressions for the condition itself
8223 and related conditions to the hashtables. This allows
8224 simplification of redundant conditions which is
8225 important as early cleanup. */
8226 edge true_e, false_e;
8227 extract_true_false_edges_from_block (bb, &true_e, &false_e);
8228 enum tree_code code = gimple_cond_code (last);
8229 enum tree_code icode
8230 = invert_tree_comparison (code, HONOR_NANS (lhs));
8231 tree ops[2];
8232 ops[0] = lhs;
8233 ops[1] = rhs;
8234 if ((do_region && bitmap_bit_p (exit_bbs, true_e->dest->index))
8235 || !can_track_predicate_on_edge (true_e))
8236 true_e = NULL;
8237 if ((do_region && bitmap_bit_p (exit_bbs, false_e->dest->index))
8238 || !can_track_predicate_on_edge (false_e))
8239 false_e = NULL;
8240 if (true_e)
8241 vn_nary_op_insert_pieces_predicated
8242 (2, code, boolean_type_node, ops,
8243 boolean_true_node, 0, true_e);
8244 if (false_e)
8245 vn_nary_op_insert_pieces_predicated
8246 (2, code, boolean_type_node, ops,
8247 boolean_false_node, 0, false_e);
8248 if (icode != ERROR_MARK)
8250 if (true_e)
8251 vn_nary_op_insert_pieces_predicated
8252 (2, icode, boolean_type_node, ops,
8253 boolean_false_node, 0, true_e);
8254 if (false_e)
8255 vn_nary_op_insert_pieces_predicated
8256 (2, icode, boolean_type_node, ops,
8257 boolean_true_node, 0, false_e);
8259 /* Relax for non-integers, inverted condition handled
8260 above. */
8261 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
8263 if (true_e)
8264 insert_related_predicates_on_edge (code, ops, true_e);
8265 if (false_e)
8266 insert_related_predicates_on_edge (icode, ops, false_e);
8269 break;
8271 case GIMPLE_GOTO:
8272 e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
8273 break;
8274 default:
8275 e = NULL;
8277 if (e)
8279 todo = TODO_cleanup_cfg;
8280 if (!(e->flags & EDGE_EXECUTABLE))
8282 if (dump_file && (dump_flags & TDF_DETAILS))
8283 fprintf (dump_file,
8284 "marking known outgoing %sedge %d -> %d executable\n",
8285 e->flags & EDGE_DFS_BACK ? "back-" : "",
8286 e->src->index, e->dest->index);
8287 e->flags |= EDGE_EXECUTABLE;
8288 e->dest->flags |= BB_EXECUTABLE;
8290 else if (!(e->dest->flags & BB_EXECUTABLE))
8292 if (dump_file && (dump_flags & TDF_DETAILS))
8293 fprintf (dump_file,
8294 "marking destination block %d reachable\n",
8295 e->dest->index);
8296 e->dest->flags |= BB_EXECUTABLE;
8299 else if (gsi_one_before_end_p (gsi))
8301 FOR_EACH_EDGE (e, ei, bb->succs)
8303 if (!(e->flags & EDGE_EXECUTABLE))
8305 if (dump_file && (dump_flags & TDF_DETAILS))
8306 fprintf (dump_file,
8307 "marking outgoing edge %d -> %d executable\n",
8308 e->src->index, e->dest->index);
8309 e->flags |= EDGE_EXECUTABLE;
8310 e->dest->flags |= BB_EXECUTABLE;
8312 else if (!(e->dest->flags & BB_EXECUTABLE))
8314 if (dump_file && (dump_flags & TDF_DETAILS))
8315 fprintf (dump_file,
8316 "marking destination block %d reachable\n",
8317 e->dest->index);
8318 e->dest->flags |= BB_EXECUTABLE;
8323 /* Eliminate. That also pushes to avail. */
8324 if (eliminate && ! iterate)
8325 avail.eliminate_stmt (bb, &gsi);
8326 else
8327 /* If not eliminating, make all not already available defs
8328 available. But avoid picking up dead defs. */
8329 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
8330 if (! has_zero_uses (op)
8331 && ! avail.eliminate_avail (bb, op))
8332 avail.eliminate_push_avail (bb, op);
8335 /* Eliminate in destination PHI arguments. Always substitute in dest
8336 PHIs, even for non-executable edges. This handles region
8337 exits PHIs. */
8338 if (!iterate && eliminate)
8339 FOR_EACH_EDGE (e, ei, bb->succs)
8340 for (gphi_iterator gsi = gsi_start_phis (e->dest);
8341 !gsi_end_p (gsi); gsi_next (&gsi))
8343 gphi *phi = gsi.phi ();
8344 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
8345 tree arg = USE_FROM_PTR (use_p);
8346 if (TREE_CODE (arg) != SSA_NAME
8347 || virtual_operand_p (arg))
8348 continue;
8349 tree sprime;
8350 if (SSA_NAME_IS_DEFAULT_DEF (arg))
8352 sprime = SSA_VAL (arg);
8353 gcc_assert (TREE_CODE (sprime) != SSA_NAME
8354 || SSA_NAME_IS_DEFAULT_DEF (sprime));
8356 else
8357 /* Look for sth available at the definition block of the argument.
8358 This avoids inconsistencies between availability there which
8359 decides if the stmt can be removed and availability at the
8360 use site. The SSA property ensures that things available
8361 at the definition are also available at uses. */
8362 sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
8363 arg);
8364 if (sprime
8365 && sprime != arg
8366 && may_propagate_copy (arg, sprime, !(e->flags & EDGE_ABNORMAL)))
8367 propagate_value (use_p, sprime);
8370 vn_context_bb = NULL;
8371 return todo;
8374 /* Unwind state per basic-block. */
8376 struct unwind_state
8378 /* Times this block has been visited. */
8379 unsigned visited;
8380 /* Whether to handle this as iteration point or whether to treat
8381 incoming backedge PHI values as varying. */
8382 bool iterate;
8383 /* Maximum RPO index this block is reachable from. */
8384 int max_rpo;
8385 /* Unwind state. */
8386 void *ob_top;
8387 vn_reference_t ref_top;
8388 vn_phi_t phi_top;
8389 vn_nary_op_t nary_top;
8390 vn_avail *avail_top;
8393 /* Unwind the RPO VN state for iteration. */
8395 static void
8396 do_unwind (unwind_state *to, rpo_elim &avail)
8398 gcc_assert (to->iterate);
8399 for (; last_inserted_nary != to->nary_top;
8400 last_inserted_nary = last_inserted_nary->next)
8402 vn_nary_op_t *slot;
8403 slot = valid_info->nary->find_slot_with_hash
8404 (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
8405 /* Predication causes the need to restore previous state. */
8406 if ((*slot)->unwind_to)
8407 *slot = (*slot)->unwind_to;
8408 else
8409 valid_info->nary->clear_slot (slot);
8411 for (; last_inserted_phi != to->phi_top;
8412 last_inserted_phi = last_inserted_phi->next)
8414 vn_phi_t *slot;
8415 slot = valid_info->phis->find_slot_with_hash
8416 (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
8417 valid_info->phis->clear_slot (slot);
8419 for (; last_inserted_ref != to->ref_top;
8420 last_inserted_ref = last_inserted_ref->next)
8422 vn_reference_t *slot;
8423 slot = valid_info->references->find_slot_with_hash
8424 (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
8425 (*slot)->operands.release ();
8426 valid_info->references->clear_slot (slot);
8428 obstack_free (&vn_tables_obstack, to->ob_top);
8430 /* Prune [rpo_idx, ] from avail. */
8431 for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
8433 vn_ssa_aux_t val = last_pushed_avail;
8434 vn_avail *av = val->avail;
8435 val->avail = av->next;
8436 last_pushed_avail = av->next_undo;
8437 av->next = avail.m_avail_freelist;
8438 avail.m_avail_freelist = av;
8442 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
8443 If ITERATE is true then treat backedges optimistically as not
8444 executed and iterate. If ELIMINATE is true then perform
8445 elimination, otherwise leave that to the caller. If SKIP_ENTRY_PHIS
8446 is true then force PHI nodes in ENTRY->dest to VARYING. */
8448 static unsigned
8449 do_rpo_vn_1 (function *fn, edge entry, bitmap exit_bbs,
8450 bool iterate, bool eliminate, bool skip_entry_phis,
8451 vn_lookup_kind kind)
8453 unsigned todo = 0;
8454 default_vn_walk_kind = kind;
8456 /* We currently do not support region-based iteration when
8457 elimination is requested. */
8458 gcc_assert (!entry || !iterate || !eliminate);
8459 /* When iterating we need loop info up-to-date. */
8460 gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
8462 bool do_region = entry != NULL;
8463 if (!do_region)
8465 entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
8466 exit_bbs = BITMAP_ALLOC (NULL);
8467 bitmap_set_bit (exit_bbs, EXIT_BLOCK);
8470 /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
8471 re-mark those that are contained in the region. */
8472 edge_iterator ei;
8473 edge e;
8474 FOR_EACH_EDGE (e, ei, entry->dest->preds)
8475 e->flags &= ~EDGE_DFS_BACK;
8477 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
8478 auto_vec<std::pair<int, int> > toplevel_scc_extents;
8479 int n = rev_post_order_and_mark_dfs_back_seme
8480 (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
8482 if (!do_region)
8483 BITMAP_FREE (exit_bbs);
8485 /* If there are any non-DFS_BACK edges into entry->dest skip
8486 processing PHI nodes for that block. This supports
8487 value-numbering loop bodies w/o the actual loop. */
8488 FOR_EACH_EDGE (e, ei, entry->dest->preds)
8489 if (e != entry
8490 && !(e->flags & EDGE_DFS_BACK))
8491 break;
8492 if (e != NULL && dump_file && (dump_flags & TDF_DETAILS))
8493 fprintf (dump_file, "Region does not contain all edges into "
8494 "the entry block, skipping its PHIs.\n");
8495 skip_entry_phis |= e != NULL;
8497 int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
8498 for (int i = 0; i < n; ++i)
8499 bb_to_rpo[rpo[i]] = i;
8501 unwind_state *rpo_state = XNEWVEC (unwind_state, n);
8503 rpo_elim avail (entry->dest);
8504 rpo_avail = &avail;
8506 /* Verify we have no extra entries into the region. */
8507 if (flag_checking && do_region)
8509 auto_bb_flag bb_in_region (fn);
8510 for (int i = 0; i < n; ++i)
8512 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8513 bb->flags |= bb_in_region;
8515 /* We can't merge the first two loops because we cannot rely
8516 on EDGE_DFS_BACK for edges not within the region. But if
8517 we decide to always have the bb_in_region flag we can
8518 do the checking during the RPO walk itself (but then it's
8519 also easy to handle MEME conservatively). */
8520 for (int i = 0; i < n; ++i)
8522 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8523 edge e;
8524 edge_iterator ei;
8525 FOR_EACH_EDGE (e, ei, bb->preds)
8526 gcc_assert (e == entry
8527 || (skip_entry_phis && bb == entry->dest)
8528 || (e->src->flags & bb_in_region));
8530 for (int i = 0; i < n; ++i)
8532 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8533 bb->flags &= ~bb_in_region;
8537 /* Create the VN state. For the initial size of the various hashtables
8538 use a heuristic based on region size and number of SSA names. */
8539 unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
8540 / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
8541 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
8542 next_value_id = 1;
8543 next_constant_value_id = -1;
8545 vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
8546 gcc_obstack_init (&vn_ssa_aux_obstack);
8548 gcc_obstack_init (&vn_tables_obstack);
8549 gcc_obstack_init (&vn_tables_insert_obstack);
8550 valid_info = XCNEW (struct vn_tables_s);
8551 allocate_vn_table (valid_info, region_size);
8552 last_inserted_ref = NULL;
8553 last_inserted_phi = NULL;
8554 last_inserted_nary = NULL;
8555 last_pushed_avail = NULL;
8557 vn_valueize = rpo_vn_valueize;
8559 /* Initialize the unwind state and edge/BB executable state. */
8560 unsigned curr_scc = 0;
8561 for (int i = 0; i < n; ++i)
8563 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8564 rpo_state[i].visited = 0;
8565 rpo_state[i].max_rpo = i;
8566 if (!iterate && curr_scc < toplevel_scc_extents.length ())
8568 if (i >= toplevel_scc_extents[curr_scc].first
8569 && i <= toplevel_scc_extents[curr_scc].second)
8570 rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
8571 if (i == toplevel_scc_extents[curr_scc].second)
8572 curr_scc++;
8574 bb->flags &= ~BB_EXECUTABLE;
8575 bool has_backedges = false;
8576 edge e;
8577 edge_iterator ei;
8578 FOR_EACH_EDGE (e, ei, bb->preds)
8580 if (e->flags & EDGE_DFS_BACK)
8581 has_backedges = true;
8582 e->flags &= ~EDGE_EXECUTABLE;
8583 if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
8584 continue;
8586 rpo_state[i].iterate = iterate && has_backedges;
8588 entry->flags |= EDGE_EXECUTABLE;
8589 entry->dest->flags |= BB_EXECUTABLE;
8591 /* As heuristic to improve compile-time we handle only the N innermost
8592 loops and the outermost one optimistically. */
8593 if (iterate)
8595 unsigned max_depth = param_rpo_vn_max_loop_depth;
8596 for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
8597 if (loop_depth (loop) > max_depth)
8598 for (unsigned i = 2;
8599 i < loop_depth (loop) - max_depth; ++i)
8601 basic_block header = superloop_at_depth (loop, i)->header;
8602 bool non_latch_backedge = false;
8603 edge e;
8604 edge_iterator ei;
8605 FOR_EACH_EDGE (e, ei, header->preds)
8606 if (e->flags & EDGE_DFS_BACK)
8608 /* There can be a non-latch backedge into the header
8609 which is part of an outer irreducible region. We
8610 cannot avoid iterating this block then. */
8611 if (!dominated_by_p (CDI_DOMINATORS,
8612 e->src, e->dest))
8614 if (dump_file && (dump_flags & TDF_DETAILS))
8615 fprintf (dump_file, "non-latch backedge %d -> %d "
8616 "forces iteration of loop %d\n",
8617 e->src->index, e->dest->index, loop->num);
8618 non_latch_backedge = true;
8620 else
8621 e->flags |= EDGE_EXECUTABLE;
8623 rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
8627 uint64_t nblk = 0;
8628 int idx = 0;
8629 if (iterate)
8630 /* Go and process all blocks, iterating as necessary. */
8633 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8635 /* If the block has incoming backedges remember unwind state. This
8636 is required even for non-executable blocks since in irreducible
8637 regions we might reach them via the backedge and re-start iterating
8638 from there.
8639 Note we can individually mark blocks with incoming backedges to
8640 not iterate where we then handle PHIs conservatively. We do that
8641 heuristically to reduce compile-time for degenerate cases. */
8642 if (rpo_state[idx].iterate)
8644 rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
8645 rpo_state[idx].ref_top = last_inserted_ref;
8646 rpo_state[idx].phi_top = last_inserted_phi;
8647 rpo_state[idx].nary_top = last_inserted_nary;
8648 rpo_state[idx].avail_top
8649 = last_pushed_avail ? last_pushed_avail->avail : NULL;
8652 if (!(bb->flags & BB_EXECUTABLE))
8654 if (dump_file && (dump_flags & TDF_DETAILS))
8655 fprintf (dump_file, "Block %d: BB%d found not executable\n",
8656 idx, bb->index);
8657 idx++;
8658 continue;
8661 if (dump_file && (dump_flags & TDF_DETAILS))
8662 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8663 nblk++;
8664 todo |= process_bb (avail, bb,
8665 rpo_state[idx].visited != 0,
8666 rpo_state[idx].iterate,
8667 iterate, eliminate, do_region, exit_bbs, false);
8668 rpo_state[idx].visited++;
8670 /* Verify if changed values flow over executable outgoing backedges
8671 and those change destination PHI values (that's the thing we
8672 can easily verify). Reduce over all such edges to the farthest
8673 away PHI. */
8674 int iterate_to = -1;
8675 edge_iterator ei;
8676 edge e;
8677 FOR_EACH_EDGE (e, ei, bb->succs)
8678 if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
8679 == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
8680 && rpo_state[bb_to_rpo[e->dest->index]].iterate)
8682 int destidx = bb_to_rpo[e->dest->index];
8683 if (!rpo_state[destidx].visited)
8685 if (dump_file && (dump_flags & TDF_DETAILS))
8686 fprintf (dump_file, "Unvisited destination %d\n",
8687 e->dest->index);
8688 if (iterate_to == -1 || destidx < iterate_to)
8689 iterate_to = destidx;
8690 continue;
8692 if (dump_file && (dump_flags & TDF_DETAILS))
8693 fprintf (dump_file, "Looking for changed values of backedge"
8694 " %d->%d destination PHIs\n",
8695 e->src->index, e->dest->index);
8696 vn_context_bb = e->dest;
8697 gphi_iterator gsi;
8698 for (gsi = gsi_start_phis (e->dest);
8699 !gsi_end_p (gsi); gsi_next (&gsi))
8701 bool inserted = false;
8702 /* While we'd ideally just iterate on value changes
8703 we CSE PHIs and do that even across basic-block
8704 boundaries. So even hashtable state changes can
8705 be important (which is roughly equivalent to
8706 PHI argument value changes). To not excessively
8707 iterate because of that we track whether a PHI
8708 was CSEd to with GF_PLF_1. */
8709 bool phival_changed;
8710 if ((phival_changed = visit_phi (gsi.phi (),
8711 &inserted, false))
8712 || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
8714 if (!phival_changed
8715 && dump_file && (dump_flags & TDF_DETAILS))
8716 fprintf (dump_file, "PHI was CSEd and hashtable "
8717 "state (changed)\n");
8718 if (iterate_to == -1 || destidx < iterate_to)
8719 iterate_to = destidx;
8720 break;
8723 vn_context_bb = NULL;
8725 if (iterate_to != -1)
8727 do_unwind (&rpo_state[iterate_to], avail);
8728 idx = iterate_to;
8729 if (dump_file && (dump_flags & TDF_DETAILS))
8730 fprintf (dump_file, "Iterating to %d BB%d\n",
8731 iterate_to, rpo[iterate_to]);
8732 continue;
8735 idx++;
8737 while (idx < n);
8739 else /* !iterate */
8741 /* Process all blocks greedily with a worklist that enforces RPO
8742 processing of reachable blocks. */
8743 auto_bitmap worklist;
8744 bitmap_set_bit (worklist, 0);
8745 while (!bitmap_empty_p (worklist))
8747 int idx = bitmap_clear_first_set_bit (worklist);
8748 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
8749 gcc_assert ((bb->flags & BB_EXECUTABLE)
8750 && !rpo_state[idx].visited);
8752 if (dump_file && (dump_flags & TDF_DETAILS))
8753 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
8755 /* When we run into predecessor edges where we cannot trust its
8756 executable state mark them executable so PHI processing will
8757 be conservative.
8758 ??? Do we need to force arguments flowing over that edge
8759 to be varying or will they even always be? */
8760 edge_iterator ei;
8761 edge e;
8762 FOR_EACH_EDGE (e, ei, bb->preds)
8763 if (!(e->flags & EDGE_EXECUTABLE)
8764 && (bb == entry->dest
8765 || (!rpo_state[bb_to_rpo[e->src->index]].visited
8766 && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
8767 >= (int)idx))))
8769 if (dump_file && (dump_flags & TDF_DETAILS))
8770 fprintf (dump_file, "Cannot trust state of predecessor "
8771 "edge %d -> %d, marking executable\n",
8772 e->src->index, e->dest->index);
8773 e->flags |= EDGE_EXECUTABLE;
8776 nblk++;
8777 todo |= process_bb (avail, bb, false, false, false, eliminate,
8778 do_region, exit_bbs,
8779 skip_entry_phis && bb == entry->dest);
8780 rpo_state[idx].visited++;
8782 FOR_EACH_EDGE (e, ei, bb->succs)
8783 if ((e->flags & EDGE_EXECUTABLE)
8784 && e->dest->index != EXIT_BLOCK
8785 && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
8786 && !rpo_state[bb_to_rpo[e->dest->index]].visited)
8787 bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
8791 /* If statistics or dump file active. */
8792 int nex = 0;
8793 unsigned max_visited = 1;
8794 for (int i = 0; i < n; ++i)
8796 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
8797 if (bb->flags & BB_EXECUTABLE)
8798 nex++;
8799 statistics_histogram_event (cfun, "RPO block visited times",
8800 rpo_state[i].visited);
8801 if (rpo_state[i].visited > max_visited)
8802 max_visited = rpo_state[i].visited;
8804 unsigned nvalues = 0, navail = 0;
8805 for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
8806 i != vn_ssa_aux_hash->end (); ++i)
8808 nvalues++;
8809 vn_avail *av = (*i)->avail;
8810 while (av)
8812 navail++;
8813 av = av->next;
8816 statistics_counter_event (cfun, "RPO blocks", n);
8817 statistics_counter_event (cfun, "RPO blocks visited", nblk);
8818 statistics_counter_event (cfun, "RPO blocks executable", nex);
8819 statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
8820 statistics_histogram_event (cfun, "RPO num values", nvalues);
8821 statistics_histogram_event (cfun, "RPO num avail", navail);
8822 statistics_histogram_event (cfun, "RPO num lattice",
8823 vn_ssa_aux_hash->elements ());
8824 if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
8826 fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
8827 " blocks in total discovering %d executable blocks iterating "
8828 "%d.%d times, a block was visited max. %u times\n",
8829 n, nblk, nex,
8830 (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
8831 max_visited);
8832 fprintf (dump_file, "RPO tracked %d values available at %d locations "
8833 "and %" PRIu64 " lattice elements\n",
8834 nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
8837 if (eliminate)
8839 /* When !iterate we already performed elimination during the RPO
8840 walk. */
8841 if (iterate)
8843 /* Elimination for region-based VN needs to be done within the
8844 RPO walk. */
8845 gcc_assert (! do_region);
8846 /* Note we can't use avail.walk here because that gets confused
8847 by the existing availability and it will be less efficient
8848 as well. */
8849 todo |= eliminate_with_rpo_vn (NULL);
8851 else
8852 todo |= avail.eliminate_cleanup (do_region);
8855 vn_valueize = NULL;
8856 rpo_avail = NULL;
8858 XDELETEVEC (bb_to_rpo);
8859 XDELETEVEC (rpo);
8860 XDELETEVEC (rpo_state);
8862 return todo;
8865 /* Region-based entry for RPO VN. Performs value-numbering and elimination
8866 on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
8867 the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
8868 are not considered.
8869 If ITERATE is true then treat backedges optimistically as not
8870 executed and iterate. If ELIMINATE is true then perform
8871 elimination, otherwise leave that to the caller.
8872 If SKIP_ENTRY_PHIS is true then force PHI nodes in ENTRY->dest to VARYING.
8873 KIND specifies the amount of work done for handling memory operations. */
8875 unsigned
8876 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
8877 bool iterate, bool eliminate, bool skip_entry_phis,
8878 vn_lookup_kind kind)
8880 auto_timevar tv (TV_TREE_RPO_VN);
8881 unsigned todo = do_rpo_vn_1 (fn, entry, exit_bbs, iterate, eliminate,
8882 skip_entry_phis, kind);
8883 free_rpo_vn ();
8884 return todo;
8888 namespace {
8890 const pass_data pass_data_fre =
8892 GIMPLE_PASS, /* type */
8893 "fre", /* name */
8894 OPTGROUP_NONE, /* optinfo_flags */
8895 TV_TREE_FRE, /* tv_id */
8896 ( PROP_cfg | PROP_ssa ), /* properties_required */
8897 0, /* properties_provided */
8898 0, /* properties_destroyed */
8899 0, /* todo_flags_start */
8900 0, /* todo_flags_finish */
8903 class pass_fre : public gimple_opt_pass
8905 public:
8906 pass_fre (gcc::context *ctxt)
8907 : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
8910 /* opt_pass methods: */
8911 opt_pass * clone () final override { return new pass_fre (m_ctxt); }
8912 void set_pass_param (unsigned int n, bool param) final override
8914 gcc_assert (n == 0);
8915 may_iterate = param;
8917 bool gate (function *) final override
8919 return flag_tree_fre != 0 && (may_iterate || optimize > 1);
8921 unsigned int execute (function *) final override;
8923 private:
8924 bool may_iterate;
8925 }; // class pass_fre
8927 unsigned int
8928 pass_fre::execute (function *fun)
8930 unsigned todo = 0;
8932 /* At -O[1g] use the cheap non-iterating mode. */
8933 bool iterate_p = may_iterate && (optimize > 1);
8934 calculate_dominance_info (CDI_DOMINATORS);
8935 if (iterate_p)
8936 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
8938 todo = do_rpo_vn_1 (fun, NULL, NULL, iterate_p, true, false, VN_WALKREWRITE);
8939 free_rpo_vn ();
8941 if (iterate_p)
8942 loop_optimizer_finalize ();
8944 if (scev_initialized_p ())
8945 scev_reset_htab ();
8947 /* For late FRE after IVOPTs and unrolling, see if we can
8948 remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8949 if (!may_iterate)
8950 todo |= TODO_update_address_taken;
8952 return todo;
8955 } // anon namespace
8957 gimple_opt_pass *
8958 make_pass_fre (gcc::context *ctxt)
8960 return new pass_fre (ctxt);
8963 #undef BB_EXECUTABLE