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