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