re PR tree-optimization/92751 (VN partial def support confused about clobbers)
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob8fbdb5163d6f15a8d3c5e6b77908ed74504591f7
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2019 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-fold.h"
43 #include "tree-eh.h"
44 #include "gimplify.h"
45 #include "flags.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "varasm.h"
50 #include "stmt.h"
51 #include "expr.h"
52 #include "tree-dfa.h"
53 #include "tree-ssa.h"
54 #include "dumpfile.h"
55 #include "cfgloop.h"
56 #include "tree-ssa-propagate.h"
57 #include "tree-cfg.h"
58 #include "domwalk.h"
59 #include "gimple-iterator.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 "tree-ssa-sccvn.h"
75 /* This algorithm is based on the SCC algorithm presented by Keith
76 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
77 (http://citeseer.ist.psu.edu/41805.html). In
78 straight line code, it is equivalent to a regular hash based value
79 numbering that is performed in reverse postorder.
81 For code with cycles, there are two alternatives, both of which
82 require keeping the hashtables separate from the actual list of
83 value numbers for SSA names.
85 1. Iterate value numbering in an RPO walk of the blocks, removing
86 all the entries from the hashtable after each iteration (but
87 keeping the SSA name->value number mapping between iterations).
88 Iterate until it does not change.
90 2. Perform value numbering as part of an SCC walk on the SSA graph,
91 iterating only the cycles in the SSA graph until they do not change
92 (using a separate, optimistic hashtable for value numbering the SCC
93 operands).
95 The second is not just faster in practice (because most SSA graph
96 cycles do not involve all the variables in the graph), it also has
97 some nice properties.
99 One of these nice properties is that when we pop an SCC off the
100 stack, we are guaranteed to have processed all the operands coming from
101 *outside of that SCC*, so we do not need to do anything special to
102 ensure they have value numbers.
104 Another nice property is that the SCC walk is done as part of a DFS
105 of the SSA graph, which makes it easy to perform combining and
106 simplifying operations at the same time.
108 The code below is deliberately written in a way that makes it easy
109 to separate the SCC walk from the other work it does.
111 In order to propagate constants through the code, we track which
112 expressions contain constants, and use those while folding. In
113 theory, we could also track expressions whose value numbers are
114 replaced, in case we end up folding based on expression
115 identities.
117 In order to value number memory, we assign value numbers to vuses.
118 This enables us to note that, for example, stores to the same
119 address of the same value from the same starting memory states are
120 equivalent.
121 TODO:
123 1. We can iterate only the changing portions of the SCC's, but
124 I have not seen an SCC big enough for this to be a win.
125 2. If you differentiate between phi nodes for loops and phi nodes
126 for if-then-else, you can properly consider phi nodes in different
127 blocks for equivalence.
128 3. We could value number vuses in more cases, particularly, whole
129 structure copies.
132 /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
133 #define BB_EXECUTABLE BB_VISITED
135 static vn_lookup_kind default_vn_walk_kind;
137 /* vn_nary_op hashtable helpers. */
139 struct vn_nary_op_hasher : nofree_ptr_hash <vn_nary_op_s>
141 typedef vn_nary_op_s *compare_type;
142 static inline hashval_t hash (const vn_nary_op_s *);
143 static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
146 /* Return the computed hashcode for nary operation P1. */
148 inline hashval_t
149 vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
151 return vno1->hashcode;
154 /* Compare nary operations P1 and P2 and return true if they are
155 equivalent. */
157 inline bool
158 vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
160 return vno1 == vno2 || vn_nary_op_eq (vno1, vno2);
163 typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
164 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
167 /* vn_phi hashtable helpers. */
169 static int
170 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
172 struct vn_phi_hasher : nofree_ptr_hash <vn_phi_s>
174 static inline hashval_t hash (const vn_phi_s *);
175 static inline bool equal (const vn_phi_s *, const vn_phi_s *);
178 /* Return the computed hashcode for phi operation P1. */
180 inline hashval_t
181 vn_phi_hasher::hash (const vn_phi_s *vp1)
183 return vp1->hashcode;
186 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
188 inline bool
189 vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
191 return vp1 == vp2 || vn_phi_eq (vp1, vp2);
194 typedef hash_table<vn_phi_hasher> vn_phi_table_type;
195 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
198 /* Compare two reference operands P1 and P2 for equality. Return true if
199 they are equal, and false otherwise. */
201 static int
202 vn_reference_op_eq (const void *p1, const void *p2)
204 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
205 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
207 return (vro1->opcode == vro2->opcode
208 /* We do not care for differences in type qualification. */
209 && (vro1->type == vro2->type
210 || (vro1->type && vro2->type
211 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
212 TYPE_MAIN_VARIANT (vro2->type))))
213 && expressions_equal_p (vro1->op0, vro2->op0)
214 && expressions_equal_p (vro1->op1, vro2->op1)
215 && expressions_equal_p (vro1->op2, vro2->op2));
218 /* Free a reference operation structure VP. */
220 static inline void
221 free_reference (vn_reference_s *vr)
223 vr->operands.release ();
227 /* vn_reference hashtable helpers. */
229 struct vn_reference_hasher : nofree_ptr_hash <vn_reference_s>
231 static inline hashval_t hash (const vn_reference_s *);
232 static inline bool equal (const vn_reference_s *, const vn_reference_s *);
235 /* Return the hashcode for a given reference operation P1. */
237 inline hashval_t
238 vn_reference_hasher::hash (const vn_reference_s *vr1)
240 return vr1->hashcode;
243 inline bool
244 vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
246 return v == c || vn_reference_eq (v, c);
249 typedef hash_table<vn_reference_hasher> vn_reference_table_type;
250 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
253 /* The set of VN hashtables. */
255 typedef struct vn_tables_s
257 vn_nary_op_table_type *nary;
258 vn_phi_table_type *phis;
259 vn_reference_table_type *references;
260 } *vn_tables_t;
263 /* vn_constant hashtable helpers. */
265 struct vn_constant_hasher : free_ptr_hash <vn_constant_s>
267 static inline hashval_t hash (const vn_constant_s *);
268 static inline bool equal (const vn_constant_s *, const vn_constant_s *);
271 /* Hash table hash function for vn_constant_t. */
273 inline hashval_t
274 vn_constant_hasher::hash (const vn_constant_s *vc1)
276 return vc1->hashcode;
279 /* Hash table equality function for vn_constant_t. */
281 inline bool
282 vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
284 if (vc1->hashcode != vc2->hashcode)
285 return false;
287 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
290 static hash_table<vn_constant_hasher> *constant_to_value_id;
291 static bitmap constant_value_ids;
294 /* Obstack we allocate the vn-tables elements from. */
295 static obstack vn_tables_obstack;
296 /* Special obstack we never unwind. */
297 static obstack vn_tables_insert_obstack;
299 static vn_reference_t last_inserted_ref;
300 static vn_phi_t last_inserted_phi;
301 static vn_nary_op_t last_inserted_nary;
303 /* Valid hashtables storing information we have proven to be
304 correct. */
305 static vn_tables_t valid_info;
308 /* Valueization hook. Valueize NAME if it is an SSA name, otherwise
309 just return it. */
310 tree (*vn_valueize) (tree);
311 tree vn_valueize_wrapper (tree t, void* context ATTRIBUTE_UNUSED)
313 return vn_valueize (t);
317 /* This represents the top of the VN lattice, which is the universal
318 value. */
320 tree VN_TOP;
322 /* Unique counter for our value ids. */
324 static unsigned int next_value_id;
327 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
328 are allocated on an obstack for locality reasons, and to free them
329 without looping over the vec. */
331 struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
333 typedef vn_ssa_aux_t value_type;
334 typedef tree compare_type;
335 static inline hashval_t hash (const value_type &);
336 static inline bool equal (const value_type &, const compare_type &);
337 static inline void mark_deleted (value_type &) {}
338 static inline void mark_empty (value_type &e) { e = NULL; }
339 static inline bool is_deleted (value_type &) { return false; }
340 static inline bool is_empty (value_type &e) { return e == NULL; }
343 hashval_t
344 vn_ssa_aux_hasher::hash (const value_type &entry)
346 return SSA_NAME_VERSION (entry->name);
349 bool
350 vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
352 return name == entry->name;
355 static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
356 typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
357 static struct obstack vn_ssa_aux_obstack;
359 static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
360 static unsigned int vn_nary_length_from_stmt (gimple *);
361 static vn_nary_op_t alloc_vn_nary_op_noinit (unsigned int, obstack *);
362 static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
363 vn_nary_op_table_type *, bool);
364 static void init_vn_nary_op_from_stmt (vn_nary_op_t, gimple *);
365 static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
366 enum tree_code, tree, tree *);
367 static tree vn_lookup_simplify_result (gimple_match_op *);
368 static vn_reference_t vn_reference_lookup_or_insert_for_pieces
369 (tree, alias_set_type, tree, vec<vn_reference_op_s, va_heap>, tree);
371 /* Return whether there is value numbering information for a given SSA name. */
373 bool
374 has_VN_INFO (tree name)
376 return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
379 vn_ssa_aux_t
380 VN_INFO (tree name)
382 vn_ssa_aux_t *res
383 = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
384 INSERT);
385 if (*res != NULL)
386 return *res;
388 vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
389 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
390 newinfo->name = name;
391 newinfo->valnum = VN_TOP;
392 /* We are using the visited flag to handle uses with defs not within the
393 region being value-numbered. */
394 newinfo->visited = false;
396 /* Given we create the VN_INFOs on-demand now we have to do initialization
397 different than VN_TOP here. */
398 if (SSA_NAME_IS_DEFAULT_DEF (name))
399 switch (TREE_CODE (SSA_NAME_VAR (name)))
401 case VAR_DECL:
402 /* All undefined vars are VARYING. */
403 newinfo->valnum = name;
404 newinfo->visited = true;
405 break;
407 case PARM_DECL:
408 /* Parameters are VARYING but we can record a condition
409 if we know it is a non-NULL pointer. */
410 newinfo->visited = true;
411 newinfo->valnum = name;
412 if (POINTER_TYPE_P (TREE_TYPE (name))
413 && nonnull_arg_p (SSA_NAME_VAR (name)))
415 tree ops[2];
416 ops[0] = name;
417 ops[1] = build_int_cst (TREE_TYPE (name), 0);
418 vn_nary_op_t nary;
419 /* Allocate from non-unwinding stack. */
420 nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
421 init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
422 boolean_type_node, ops);
423 nary->predicated_values = 0;
424 nary->u.result = boolean_true_node;
425 vn_nary_op_insert_into (nary, valid_info->nary, true);
426 gcc_assert (nary->unwind_to == NULL);
427 /* Also do not link it into the undo chain. */
428 last_inserted_nary = nary->next;
429 nary->next = (vn_nary_op_t)(void *)-1;
430 nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
431 init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
432 boolean_type_node, ops);
433 nary->predicated_values = 0;
434 nary->u.result = boolean_false_node;
435 vn_nary_op_insert_into (nary, valid_info->nary, true);
436 gcc_assert (nary->unwind_to == NULL);
437 last_inserted_nary = nary->next;
438 nary->next = (vn_nary_op_t)(void *)-1;
439 if (dump_file && (dump_flags & TDF_DETAILS))
441 fprintf (dump_file, "Recording ");
442 print_generic_expr (dump_file, name, TDF_SLIM);
443 fprintf (dump_file, " != 0\n");
446 break;
448 case RESULT_DECL:
449 /* If the result is passed by invisible reference the default
450 def is initialized, otherwise it's uninitialized. Still
451 undefined is varying. */
452 newinfo->visited = true;
453 newinfo->valnum = name;
454 break;
456 default:
457 gcc_unreachable ();
459 return newinfo;
462 /* Return the SSA value of X. */
464 inline tree
465 SSA_VAL (tree x, bool *visited = NULL)
467 vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
468 if (visited)
469 *visited = tem && tem->visited;
470 return tem && tem->visited ? tem->valnum : x;
473 /* Return the SSA value of the VUSE x, supporting released VDEFs
474 during elimination which will value-number the VDEF to the
475 associated VUSE (but not substitute in the whole lattice). */
477 static inline tree
478 vuse_ssa_val (tree x)
480 if (!x)
481 return NULL_TREE;
485 x = SSA_VAL (x);
486 gcc_assert (x != VN_TOP);
488 while (SSA_NAME_IN_FREE_LIST (x));
490 return x;
493 /* Similar to the above but used as callback for walk_non_aliases_vuses
494 and thus should stop at unvisited VUSE to not walk across region
495 boundaries. */
497 static tree
498 vuse_valueize (tree vuse)
502 bool visited;
503 vuse = SSA_VAL (vuse, &visited);
504 if (!visited)
505 return NULL_TREE;
506 gcc_assert (vuse != VN_TOP);
508 while (SSA_NAME_IN_FREE_LIST (vuse));
509 return vuse;
513 /* Return the vn_kind the expression computed by the stmt should be
514 associated with. */
516 enum vn_kind
517 vn_get_stmt_kind (gimple *stmt)
519 switch (gimple_code (stmt))
521 case GIMPLE_CALL:
522 return VN_REFERENCE;
523 case GIMPLE_PHI:
524 return VN_PHI;
525 case GIMPLE_ASSIGN:
527 enum tree_code code = gimple_assign_rhs_code (stmt);
528 tree rhs1 = gimple_assign_rhs1 (stmt);
529 switch (get_gimple_rhs_class (code))
531 case GIMPLE_UNARY_RHS:
532 case GIMPLE_BINARY_RHS:
533 case GIMPLE_TERNARY_RHS:
534 return VN_NARY;
535 case GIMPLE_SINGLE_RHS:
536 switch (TREE_CODE_CLASS (code))
538 case tcc_reference:
539 /* VOP-less references can go through unary case. */
540 if ((code == REALPART_EXPR
541 || code == IMAGPART_EXPR
542 || code == VIEW_CONVERT_EXPR
543 || code == BIT_FIELD_REF)
544 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
545 return VN_NARY;
547 /* Fallthrough. */
548 case tcc_declaration:
549 return VN_REFERENCE;
551 case tcc_constant:
552 return VN_CONSTANT;
554 default:
555 if (code == ADDR_EXPR)
556 return (is_gimple_min_invariant (rhs1)
557 ? VN_CONSTANT : VN_REFERENCE);
558 else if (code == CONSTRUCTOR)
559 return VN_NARY;
560 return VN_NONE;
562 default:
563 return VN_NONE;
566 default:
567 return VN_NONE;
571 /* Lookup a value id for CONSTANT and return it. If it does not
572 exist returns 0. */
574 unsigned int
575 get_constant_value_id (tree constant)
577 vn_constant_s **slot;
578 struct vn_constant_s vc;
580 vc.hashcode = vn_hash_constant_with_type (constant);
581 vc.constant = constant;
582 slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
583 if (slot)
584 return (*slot)->value_id;
585 return 0;
588 /* Lookup a value id for CONSTANT, and if it does not exist, create a
589 new one and return it. If it does exist, return it. */
591 unsigned int
592 get_or_alloc_constant_value_id (tree constant)
594 vn_constant_s **slot;
595 struct vn_constant_s vc;
596 vn_constant_t vcp;
598 /* If the hashtable isn't initialized we're not running from PRE and thus
599 do not need value-ids. */
600 if (!constant_to_value_id)
601 return 0;
603 vc.hashcode = vn_hash_constant_with_type (constant);
604 vc.constant = constant;
605 slot = constant_to_value_id->find_slot (&vc, INSERT);
606 if (*slot)
607 return (*slot)->value_id;
609 vcp = XNEW (struct vn_constant_s);
610 vcp->hashcode = vc.hashcode;
611 vcp->constant = constant;
612 vcp->value_id = get_next_value_id ();
613 *slot = vcp;
614 bitmap_set_bit (constant_value_ids, vcp->value_id);
615 return vcp->value_id;
618 /* Return true if V is a value id for a constant. */
620 bool
621 value_id_constant_p (unsigned int v)
623 return bitmap_bit_p (constant_value_ids, v);
626 /* Compute the hash for a reference operand VRO1. */
628 static void
629 vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
631 hstate.add_int (vro1->opcode);
632 if (vro1->op0)
633 inchash::add_expr (vro1->op0, hstate);
634 if (vro1->op1)
635 inchash::add_expr (vro1->op1, hstate);
636 if (vro1->op2)
637 inchash::add_expr (vro1->op2, hstate);
640 /* Compute a hash for the reference operation VR1 and return it. */
642 static hashval_t
643 vn_reference_compute_hash (const vn_reference_t vr1)
645 inchash::hash hstate;
646 hashval_t result;
647 int i;
648 vn_reference_op_t vro;
649 poly_int64 off = -1;
650 bool deref = false;
652 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
654 if (vro->opcode == MEM_REF)
655 deref = true;
656 else if (vro->opcode != ADDR_EXPR)
657 deref = false;
658 if (maybe_ne (vro->off, -1))
660 if (known_eq (off, -1))
661 off = 0;
662 off += vro->off;
664 else
666 if (maybe_ne (off, -1)
667 && maybe_ne (off, 0))
668 hstate.add_poly_int (off);
669 off = -1;
670 if (deref
671 && vro->opcode == ADDR_EXPR)
673 if (vro->op0)
675 tree op = TREE_OPERAND (vro->op0, 0);
676 hstate.add_int (TREE_CODE (op));
677 inchash::add_expr (op, hstate);
680 else
681 vn_reference_op_compute_hash (vro, hstate);
684 result = hstate.end ();
685 /* ??? We would ICE later if we hash instead of adding that in. */
686 if (vr1->vuse)
687 result += SSA_NAME_VERSION (vr1->vuse);
689 return result;
692 /* Return true if reference operations VR1 and VR2 are equivalent. This
693 means they have the same set of operands and vuses. */
695 bool
696 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
698 unsigned i, j;
700 /* Early out if this is not a hash collision. */
701 if (vr1->hashcode != vr2->hashcode)
702 return false;
704 /* The VOP needs to be the same. */
705 if (vr1->vuse != vr2->vuse)
706 return false;
708 /* If the operands are the same we are done. */
709 if (vr1->operands == vr2->operands)
710 return true;
712 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
713 return false;
715 if (INTEGRAL_TYPE_P (vr1->type)
716 && INTEGRAL_TYPE_P (vr2->type))
718 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
719 return false;
721 else if (INTEGRAL_TYPE_P (vr1->type)
722 && (TYPE_PRECISION (vr1->type)
723 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
724 return false;
725 else if (INTEGRAL_TYPE_P (vr2->type)
726 && (TYPE_PRECISION (vr2->type)
727 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
728 return false;
730 i = 0;
731 j = 0;
734 poly_int64 off1 = 0, off2 = 0;
735 vn_reference_op_t vro1, vro2;
736 vn_reference_op_s tem1, tem2;
737 bool deref1 = false, deref2 = false;
738 for (; vr1->operands.iterate (i, &vro1); i++)
740 if (vro1->opcode == MEM_REF)
741 deref1 = true;
742 /* Do not look through a storage order barrier. */
743 else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
744 return false;
745 if (known_eq (vro1->off, -1))
746 break;
747 off1 += vro1->off;
749 for (; vr2->operands.iterate (j, &vro2); j++)
751 if (vro2->opcode == MEM_REF)
752 deref2 = true;
753 /* Do not look through a storage order barrier. */
754 else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
755 return false;
756 if (known_eq (vro2->off, -1))
757 break;
758 off2 += vro2->off;
760 if (maybe_ne (off1, off2))
761 return false;
762 if (deref1 && vro1->opcode == ADDR_EXPR)
764 memset (&tem1, 0, sizeof (tem1));
765 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
766 tem1.type = TREE_TYPE (tem1.op0);
767 tem1.opcode = TREE_CODE (tem1.op0);
768 vro1 = &tem1;
769 deref1 = false;
771 if (deref2 && vro2->opcode == ADDR_EXPR)
773 memset (&tem2, 0, sizeof (tem2));
774 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
775 tem2.type = TREE_TYPE (tem2.op0);
776 tem2.opcode = TREE_CODE (tem2.op0);
777 vro2 = &tem2;
778 deref2 = false;
780 if (deref1 != deref2)
781 return false;
782 if (!vn_reference_op_eq (vro1, vro2))
783 return false;
784 ++j;
785 ++i;
787 while (vr1->operands.length () != i
788 || vr2->operands.length () != j);
790 return true;
793 /* Copy the operations present in load/store REF into RESULT, a vector of
794 vn_reference_op_s's. */
796 static void
797 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
799 /* For non-calls, store the information that makes up the address. */
800 tree orig = ref;
801 while (ref)
803 vn_reference_op_s temp;
805 memset (&temp, 0, sizeof (temp));
806 temp.type = TREE_TYPE (ref);
807 temp.opcode = TREE_CODE (ref);
808 temp.off = -1;
810 switch (temp.opcode)
812 case MODIFY_EXPR:
813 temp.op0 = TREE_OPERAND (ref, 1);
814 break;
815 case WITH_SIZE_EXPR:
816 temp.op0 = TREE_OPERAND (ref, 1);
817 temp.off = 0;
818 break;
819 case MEM_REF:
820 /* The base address gets its own vn_reference_op_s structure. */
821 temp.op0 = TREE_OPERAND (ref, 1);
822 if (!mem_ref_offset (ref).to_shwi (&temp.off))
823 temp.off = -1;
824 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
825 temp.base = MR_DEPENDENCE_BASE (ref);
826 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
827 break;
828 case TARGET_MEM_REF:
829 /* The base address gets its own vn_reference_op_s structure. */
830 temp.op0 = TMR_INDEX (ref);
831 temp.op1 = TMR_STEP (ref);
832 temp.op2 = TMR_OFFSET (ref);
833 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
834 temp.base = MR_DEPENDENCE_BASE (ref);
835 result->safe_push (temp);
836 memset (&temp, 0, sizeof (temp));
837 temp.type = NULL_TREE;
838 temp.opcode = ERROR_MARK;
839 temp.op0 = TMR_INDEX2 (ref);
840 temp.off = -1;
841 break;
842 case BIT_FIELD_REF:
843 /* Record bits, position and storage order. */
844 temp.op0 = TREE_OPERAND (ref, 1);
845 temp.op1 = TREE_OPERAND (ref, 2);
846 if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
847 temp.off = -1;
848 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
849 break;
850 case COMPONENT_REF:
851 /* The field decl is enough to unambiguously specify the field,
852 a matching type is not necessary and a mismatching type
853 is always a spurious difference. */
854 temp.type = NULL_TREE;
855 temp.op0 = TREE_OPERAND (ref, 1);
856 temp.op1 = TREE_OPERAND (ref, 2);
858 tree this_offset = component_ref_field_offset (ref);
859 if (this_offset
860 && poly_int_tree_p (this_offset))
862 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
863 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
865 poly_offset_int off
866 = (wi::to_poly_offset (this_offset)
867 + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
868 /* Probibit value-numbering zero offset components
869 of addresses the same before the pass folding
870 __builtin_object_size had a chance to run
871 (checking cfun->after_inlining does the
872 trick here). */
873 if (TREE_CODE (orig) != ADDR_EXPR
874 || maybe_ne (off, 0)
875 || cfun->after_inlining)
876 off.to_shwi (&temp.off);
880 break;
881 case ARRAY_RANGE_REF:
882 case ARRAY_REF:
884 tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
885 /* Record index as operand. */
886 temp.op0 = TREE_OPERAND (ref, 1);
887 /* Always record lower bounds and element size. */
888 temp.op1 = array_ref_low_bound (ref);
889 /* But record element size in units of the type alignment. */
890 temp.op2 = TREE_OPERAND (ref, 3);
891 temp.align = eltype->type_common.align;
892 if (! temp.op2)
893 temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
894 size_int (TYPE_ALIGN_UNIT (eltype)));
895 if (poly_int_tree_p (temp.op0)
896 && poly_int_tree_p (temp.op1)
897 && TREE_CODE (temp.op2) == INTEGER_CST)
899 poly_offset_int off = ((wi::to_poly_offset (temp.op0)
900 - wi::to_poly_offset (temp.op1))
901 * wi::to_offset (temp.op2)
902 * vn_ref_op_align_unit (&temp));
903 off.to_shwi (&temp.off);
906 break;
907 case VAR_DECL:
908 if (DECL_HARD_REGISTER (ref))
910 temp.op0 = ref;
911 break;
913 /* Fallthru. */
914 case PARM_DECL:
915 case CONST_DECL:
916 case RESULT_DECL:
917 /* Canonicalize decls to MEM[&decl] which is what we end up with
918 when valueizing MEM[ptr] with ptr = &decl. */
919 temp.opcode = MEM_REF;
920 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
921 temp.off = 0;
922 result->safe_push (temp);
923 temp.opcode = ADDR_EXPR;
924 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
925 temp.type = TREE_TYPE (temp.op0);
926 temp.off = -1;
927 break;
928 case STRING_CST:
929 case INTEGER_CST:
930 case POLY_INT_CST:
931 case COMPLEX_CST:
932 case VECTOR_CST:
933 case REAL_CST:
934 case FIXED_CST:
935 case CONSTRUCTOR:
936 case SSA_NAME:
937 temp.op0 = ref;
938 break;
939 case ADDR_EXPR:
940 if (is_gimple_min_invariant (ref))
942 temp.op0 = ref;
943 break;
945 break;
946 /* These are only interesting for their operands, their
947 existence, and their type. They will never be the last
948 ref in the chain of references (IE they require an
949 operand), so we don't have to put anything
950 for op* as it will be handled by the iteration */
951 case REALPART_EXPR:
952 temp.off = 0;
953 break;
954 case VIEW_CONVERT_EXPR:
955 temp.off = 0;
956 temp.reverse = storage_order_barrier_p (ref);
957 break;
958 case IMAGPART_EXPR:
959 /* This is only interesting for its constant offset. */
960 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
961 break;
962 default:
963 gcc_unreachable ();
965 result->safe_push (temp);
967 if (REFERENCE_CLASS_P (ref)
968 || TREE_CODE (ref) == MODIFY_EXPR
969 || TREE_CODE (ref) == WITH_SIZE_EXPR
970 || (TREE_CODE (ref) == ADDR_EXPR
971 && !is_gimple_min_invariant (ref)))
972 ref = TREE_OPERAND (ref, 0);
973 else
974 ref = NULL_TREE;
978 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
979 operands in *OPS, the reference alias set SET and the reference type TYPE.
980 Return true if something useful was produced. */
982 bool
983 ao_ref_init_from_vn_reference (ao_ref *ref,
984 alias_set_type set, tree type,
985 vec<vn_reference_op_s> ops)
987 vn_reference_op_t op;
988 unsigned i;
989 tree base = NULL_TREE;
990 tree *op0_p = &base;
991 poly_offset_int offset = 0;
992 poly_offset_int max_size;
993 poly_offset_int size = -1;
994 tree size_tree = NULL_TREE;
995 alias_set_type base_alias_set = -1;
997 /* First get the final access size from just the outermost expression. */
998 op = &ops[0];
999 if (op->opcode == COMPONENT_REF)
1000 size_tree = DECL_SIZE (op->op0);
1001 else if (op->opcode == BIT_FIELD_REF)
1002 size_tree = op->op0;
1003 else
1005 machine_mode mode = TYPE_MODE (type);
1006 if (mode == BLKmode)
1007 size_tree = TYPE_SIZE (type);
1008 else
1009 size = GET_MODE_BITSIZE (mode);
1011 if (size_tree != NULL_TREE
1012 && poly_int_tree_p (size_tree))
1013 size = wi::to_poly_offset (size_tree);
1015 /* Initially, maxsize is the same as the accessed element size.
1016 In the following it will only grow (or become -1). */
1017 max_size = size;
1019 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1020 and find the ultimate containing object. */
1021 FOR_EACH_VEC_ELT (ops, i, op)
1023 switch (op->opcode)
1025 /* These may be in the reference ops, but we cannot do anything
1026 sensible with them here. */
1027 case ADDR_EXPR:
1028 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1029 if (base != NULL_TREE
1030 && TREE_CODE (base) == MEM_REF
1031 && op->op0
1032 && DECL_P (TREE_OPERAND (op->op0, 0)))
1034 vn_reference_op_t pop = &ops[i-1];
1035 base = TREE_OPERAND (op->op0, 0);
1036 if (known_eq (pop->off, -1))
1038 max_size = -1;
1039 offset = 0;
1041 else
1042 offset += pop->off * BITS_PER_UNIT;
1043 op0_p = NULL;
1044 break;
1046 /* Fallthru. */
1047 case CALL_EXPR:
1048 return false;
1050 /* Record the base objects. */
1051 case MEM_REF:
1052 base_alias_set = get_deref_alias_set (op->op0);
1053 *op0_p = build2 (MEM_REF, op->type,
1054 NULL_TREE, op->op0);
1055 MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1056 MR_DEPENDENCE_BASE (*op0_p) = op->base;
1057 op0_p = &TREE_OPERAND (*op0_p, 0);
1058 break;
1060 case VAR_DECL:
1061 case PARM_DECL:
1062 case RESULT_DECL:
1063 case SSA_NAME:
1064 *op0_p = op->op0;
1065 op0_p = NULL;
1066 break;
1068 /* And now the usual component-reference style ops. */
1069 case BIT_FIELD_REF:
1070 offset += wi::to_poly_offset (op->op1);
1071 break;
1073 case COMPONENT_REF:
1075 tree field = op->op0;
1076 /* We do not have a complete COMPONENT_REF tree here so we
1077 cannot use component_ref_field_offset. Do the interesting
1078 parts manually. */
1079 tree this_offset = DECL_FIELD_OFFSET (field);
1081 if (op->op1 || !poly_int_tree_p (this_offset))
1082 max_size = -1;
1083 else
1085 poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1086 << LOG2_BITS_PER_UNIT);
1087 woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1088 offset += woffset;
1090 break;
1093 case ARRAY_RANGE_REF:
1094 case ARRAY_REF:
1095 /* We recorded the lower bound and the element size. */
1096 if (!poly_int_tree_p (op->op0)
1097 || !poly_int_tree_p (op->op1)
1098 || TREE_CODE (op->op2) != INTEGER_CST)
1099 max_size = -1;
1100 else
1102 poly_offset_int woffset
1103 = wi::sext (wi::to_poly_offset (op->op0)
1104 - wi::to_poly_offset (op->op1),
1105 TYPE_PRECISION (TREE_TYPE (op->op0)));
1106 woffset *= wi::to_offset (op->op2) * vn_ref_op_align_unit (op);
1107 woffset <<= LOG2_BITS_PER_UNIT;
1108 offset += woffset;
1110 break;
1112 case REALPART_EXPR:
1113 break;
1115 case IMAGPART_EXPR:
1116 offset += size;
1117 break;
1119 case VIEW_CONVERT_EXPR:
1120 break;
1122 case STRING_CST:
1123 case INTEGER_CST:
1124 case COMPLEX_CST:
1125 case VECTOR_CST:
1126 case REAL_CST:
1127 case CONSTRUCTOR:
1128 case CONST_DECL:
1129 return false;
1131 default:
1132 return false;
1136 if (base == NULL_TREE)
1137 return false;
1139 ref->ref = NULL_TREE;
1140 ref->base = base;
1141 ref->ref_alias_set = set;
1142 if (base_alias_set != -1)
1143 ref->base_alias_set = base_alias_set;
1144 else
1145 ref->base_alias_set = get_alias_set (base);
1146 /* We discount volatiles from value-numbering elsewhere. */
1147 ref->volatile_p = false;
1149 if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1151 ref->offset = 0;
1152 ref->size = -1;
1153 ref->max_size = -1;
1154 return true;
1157 if (!offset.to_shwi (&ref->offset))
1159 ref->offset = 0;
1160 ref->max_size = -1;
1161 return true;
1164 if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1165 ref->max_size = -1;
1167 return true;
1170 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1171 vn_reference_op_s's. */
1173 static void
1174 copy_reference_ops_from_call (gcall *call,
1175 vec<vn_reference_op_s> *result)
1177 vn_reference_op_s temp;
1178 unsigned i;
1179 tree lhs = gimple_call_lhs (call);
1180 int lr;
1182 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1183 different. By adding the lhs here in the vector, we ensure that the
1184 hashcode is different, guaranteeing a different value number. */
1185 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1187 memset (&temp, 0, sizeof (temp));
1188 temp.opcode = MODIFY_EXPR;
1189 temp.type = TREE_TYPE (lhs);
1190 temp.op0 = lhs;
1191 temp.off = -1;
1192 result->safe_push (temp);
1195 /* Copy the type, opcode, function, static chain and EH region, if any. */
1196 memset (&temp, 0, sizeof (temp));
1197 temp.type = gimple_call_fntype (call);
1198 temp.opcode = CALL_EXPR;
1199 temp.op0 = gimple_call_fn (call);
1200 temp.op1 = gimple_call_chain (call);
1201 if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1202 temp.op2 = size_int (lr);
1203 temp.off = -1;
1204 result->safe_push (temp);
1206 /* Copy the call arguments. As they can be references as well,
1207 just chain them together. */
1208 for (i = 0; i < gimple_call_num_args (call); ++i)
1210 tree callarg = gimple_call_arg (call, i);
1211 copy_reference_ops_from_ref (callarg, result);
1215 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1216 *I_P to point to the last element of the replacement. */
1217 static bool
1218 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1219 unsigned int *i_p)
1221 unsigned int i = *i_p;
1222 vn_reference_op_t op = &(*ops)[i];
1223 vn_reference_op_t mem_op = &(*ops)[i - 1];
1224 tree addr_base;
1225 poly_int64 addr_offset = 0;
1227 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1228 from .foo.bar to the preceding MEM_REF offset and replace the
1229 address with &OBJ. */
1230 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1231 &addr_offset);
1232 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1233 if (addr_base != TREE_OPERAND (op->op0, 0))
1235 poly_offset_int off
1236 = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1237 SIGNED)
1238 + addr_offset);
1239 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1240 op->op0 = build_fold_addr_expr (addr_base);
1241 if (tree_fits_shwi_p (mem_op->op0))
1242 mem_op->off = tree_to_shwi (mem_op->op0);
1243 else
1244 mem_op->off = -1;
1245 return true;
1247 return false;
1250 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1251 *I_P to point to the last element of the replacement. */
1252 static bool
1253 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1254 unsigned int *i_p)
1256 bool changed = false;
1257 vn_reference_op_t op;
1261 unsigned int i = *i_p;
1262 op = &(*ops)[i];
1263 vn_reference_op_t mem_op = &(*ops)[i - 1];
1264 gimple *def_stmt;
1265 enum tree_code code;
1266 poly_offset_int off;
1268 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1269 if (!is_gimple_assign (def_stmt))
1270 return changed;
1272 code = gimple_assign_rhs_code (def_stmt);
1273 if (code != ADDR_EXPR
1274 && code != POINTER_PLUS_EXPR)
1275 return changed;
1277 off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1279 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1280 from .foo.bar to the preceding MEM_REF offset and replace the
1281 address with &OBJ. */
1282 if (code == ADDR_EXPR)
1284 tree addr, addr_base;
1285 poly_int64 addr_offset;
1287 addr = gimple_assign_rhs1 (def_stmt);
1288 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1289 &addr_offset);
1290 /* If that didn't work because the address isn't invariant propagate
1291 the reference tree from the address operation in case the current
1292 dereference isn't offsetted. */
1293 if (!addr_base
1294 && *i_p == ops->length () - 1
1295 && known_eq (off, 0)
1296 /* This makes us disable this transform for PRE where the
1297 reference ops might be also used for code insertion which
1298 is invalid. */
1299 && default_vn_walk_kind == VN_WALKREWRITE)
1301 auto_vec<vn_reference_op_s, 32> tem;
1302 copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1303 /* Make sure to preserve TBAA info. The only objects not
1304 wrapped in MEM_REFs that can have their address taken are
1305 STRING_CSTs. */
1306 if (tem.length () >= 2
1307 && tem[tem.length () - 2].opcode == MEM_REF)
1309 vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1310 new_mem_op->op0
1311 = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1312 wi::to_poly_wide (new_mem_op->op0));
1314 else
1315 gcc_assert (tem.last ().opcode == STRING_CST);
1316 ops->pop ();
1317 ops->pop ();
1318 ops->safe_splice (tem);
1319 --*i_p;
1320 return true;
1322 if (!addr_base
1323 || TREE_CODE (addr_base) != MEM_REF
1324 || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1325 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1326 0))))
1327 return changed;
1329 off += addr_offset;
1330 off += mem_ref_offset (addr_base);
1331 op->op0 = TREE_OPERAND (addr_base, 0);
1333 else
1335 tree ptr, ptroff;
1336 ptr = gimple_assign_rhs1 (def_stmt);
1337 ptroff = gimple_assign_rhs2 (def_stmt);
1338 if (TREE_CODE (ptr) != SSA_NAME
1339 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1340 /* Make sure to not endlessly recurse.
1341 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1342 happen when we value-number a PHI to its backedge value. */
1343 || SSA_VAL (ptr) == op->op0
1344 || !poly_int_tree_p (ptroff))
1345 return changed;
1347 off += wi::to_poly_offset (ptroff);
1348 op->op0 = ptr;
1351 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1352 if (tree_fits_shwi_p (mem_op->op0))
1353 mem_op->off = tree_to_shwi (mem_op->op0);
1354 else
1355 mem_op->off = -1;
1356 /* ??? Can end up with endless recursion here!?
1357 gcc.c-torture/execute/strcmp-1.c */
1358 if (TREE_CODE (op->op0) == SSA_NAME)
1359 op->op0 = SSA_VAL (op->op0);
1360 if (TREE_CODE (op->op0) != SSA_NAME)
1361 op->opcode = TREE_CODE (op->op0);
1363 changed = true;
1365 /* Tail-recurse. */
1366 while (TREE_CODE (op->op0) == SSA_NAME);
1368 /* Fold a remaining *&. */
1369 if (TREE_CODE (op->op0) == ADDR_EXPR)
1370 vn_reference_fold_indirect (ops, i_p);
1372 return changed;
1375 /* Optimize the reference REF to a constant if possible or return
1376 NULL_TREE if not. */
1378 tree
1379 fully_constant_vn_reference_p (vn_reference_t ref)
1381 vec<vn_reference_op_s> operands = ref->operands;
1382 vn_reference_op_t op;
1384 /* Try to simplify the translated expression if it is
1385 a call to a builtin function with at most two arguments. */
1386 op = &operands[0];
1387 if (op->opcode == CALL_EXPR
1388 && TREE_CODE (op->op0) == ADDR_EXPR
1389 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1390 && fndecl_built_in_p (TREE_OPERAND (op->op0, 0))
1391 && operands.length () >= 2
1392 && operands.length () <= 3)
1394 vn_reference_op_t arg0, arg1 = NULL;
1395 bool anyconst = false;
1396 arg0 = &operands[1];
1397 if (operands.length () > 2)
1398 arg1 = &operands[2];
1399 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1400 || (arg0->opcode == ADDR_EXPR
1401 && is_gimple_min_invariant (arg0->op0)))
1402 anyconst = true;
1403 if (arg1
1404 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1405 || (arg1->opcode == ADDR_EXPR
1406 && is_gimple_min_invariant (arg1->op0))))
1407 anyconst = true;
1408 if (anyconst)
1410 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1411 arg1 ? 2 : 1,
1412 arg0->op0,
1413 arg1 ? arg1->op0 : NULL);
1414 if (folded
1415 && TREE_CODE (folded) == NOP_EXPR)
1416 folded = TREE_OPERAND (folded, 0);
1417 if (folded
1418 && is_gimple_min_invariant (folded))
1419 return folded;
1423 /* Simplify reads from constants or constant initializers. */
1424 else if (BITS_PER_UNIT == 8
1425 && COMPLETE_TYPE_P (ref->type)
1426 && is_gimple_reg_type (ref->type))
1428 poly_int64 off = 0;
1429 HOST_WIDE_INT size;
1430 if (INTEGRAL_TYPE_P (ref->type))
1431 size = TYPE_PRECISION (ref->type);
1432 else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1433 size = tree_to_shwi (TYPE_SIZE (ref->type));
1434 else
1435 return NULL_TREE;
1436 if (size % BITS_PER_UNIT != 0
1437 || size > MAX_BITSIZE_MODE_ANY_MODE)
1438 return NULL_TREE;
1439 size /= BITS_PER_UNIT;
1440 unsigned i;
1441 for (i = 0; i < operands.length (); ++i)
1443 if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1445 ++i;
1446 break;
1448 if (known_eq (operands[i].off, -1))
1449 return NULL_TREE;
1450 off += operands[i].off;
1451 if (operands[i].opcode == MEM_REF)
1453 ++i;
1454 break;
1457 vn_reference_op_t base = &operands[--i];
1458 tree ctor = error_mark_node;
1459 tree decl = NULL_TREE;
1460 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1461 ctor = base->op0;
1462 else if (base->opcode == MEM_REF
1463 && base[1].opcode == ADDR_EXPR
1464 && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
1465 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1466 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1468 decl = TREE_OPERAND (base[1].op0, 0);
1469 if (TREE_CODE (decl) == STRING_CST)
1470 ctor = decl;
1471 else
1472 ctor = ctor_for_folding (decl);
1474 if (ctor == NULL_TREE)
1475 return build_zero_cst (ref->type);
1476 else if (ctor != error_mark_node)
1478 HOST_WIDE_INT const_off;
1479 if (decl)
1481 tree res = fold_ctor_reference (ref->type, ctor,
1482 off * BITS_PER_UNIT,
1483 size * BITS_PER_UNIT, decl);
1484 if (res)
1486 STRIP_USELESS_TYPE_CONVERSION (res);
1487 if (is_gimple_min_invariant (res))
1488 return res;
1491 else if (off.is_constant (&const_off))
1493 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1494 int len = native_encode_expr (ctor, buf, size, const_off);
1495 if (len > 0)
1496 return native_interpret_expr (ref->type, buf, len);
1501 return NULL_TREE;
1504 /* Return true if OPS contain a storage order barrier. */
1506 static bool
1507 contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1509 vn_reference_op_t op;
1510 unsigned i;
1512 FOR_EACH_VEC_ELT (ops, i, op)
1513 if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1514 return true;
1516 return false;
1519 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1520 structures into their value numbers. This is done in-place, and
1521 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1522 whether any operands were valueized. */
1524 static vec<vn_reference_op_s>
1525 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything,
1526 bool with_avail = false)
1528 vn_reference_op_t vro;
1529 unsigned int i;
1531 *valueized_anything = false;
1533 FOR_EACH_VEC_ELT (orig, i, vro)
1535 if (vro->opcode == SSA_NAME
1536 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1538 tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1539 if (tem != vro->op0)
1541 *valueized_anything = true;
1542 vro->op0 = tem;
1544 /* If it transforms from an SSA_NAME to a constant, update
1545 the opcode. */
1546 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1547 vro->opcode = TREE_CODE (vro->op0);
1549 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1551 tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1552 if (tem != vro->op1)
1554 *valueized_anything = true;
1555 vro->op1 = tem;
1558 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1560 tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1561 if (tem != vro->op2)
1563 *valueized_anything = true;
1564 vro->op2 = tem;
1567 /* If it transforms from an SSA_NAME to an address, fold with
1568 a preceding indirect reference. */
1569 if (i > 0
1570 && vro->op0
1571 && TREE_CODE (vro->op0) == ADDR_EXPR
1572 && orig[i - 1].opcode == MEM_REF)
1574 if (vn_reference_fold_indirect (&orig, &i))
1575 *valueized_anything = true;
1577 else if (i > 0
1578 && vro->opcode == SSA_NAME
1579 && orig[i - 1].opcode == MEM_REF)
1581 if (vn_reference_maybe_forwprop_address (&orig, &i))
1582 *valueized_anything = true;
1584 /* If it transforms a non-constant ARRAY_REF into a constant
1585 one, adjust the constant offset. */
1586 else if (vro->opcode == ARRAY_REF
1587 && known_eq (vro->off, -1)
1588 && poly_int_tree_p (vro->op0)
1589 && poly_int_tree_p (vro->op1)
1590 && TREE_CODE (vro->op2) == INTEGER_CST)
1592 poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1593 - wi::to_poly_offset (vro->op1))
1594 * wi::to_offset (vro->op2)
1595 * vn_ref_op_align_unit (vro));
1596 off.to_shwi (&vro->off);
1600 return orig;
1603 static vec<vn_reference_op_s>
1604 valueize_refs (vec<vn_reference_op_s> orig)
1606 bool tem;
1607 return valueize_refs_1 (orig, &tem);
1610 static vec<vn_reference_op_s> shared_lookup_references;
1612 /* Create a vector of vn_reference_op_s structures from REF, a
1613 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1614 this function. *VALUEIZED_ANYTHING will specify whether any
1615 operands were valueized. */
1617 static vec<vn_reference_op_s>
1618 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1620 if (!ref)
1621 return vNULL;
1622 shared_lookup_references.truncate (0);
1623 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1624 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1625 valueized_anything);
1626 return shared_lookup_references;
1629 /* Create a vector of vn_reference_op_s structures from CALL, a
1630 call statement. The vector is shared among all callers of
1631 this function. */
1633 static vec<vn_reference_op_s>
1634 valueize_shared_reference_ops_from_call (gcall *call)
1636 if (!call)
1637 return vNULL;
1638 shared_lookup_references.truncate (0);
1639 copy_reference_ops_from_call (call, &shared_lookup_references);
1640 shared_lookup_references = valueize_refs (shared_lookup_references);
1641 return shared_lookup_references;
1644 /* Lookup a SCCVN reference operation VR in the current hash table.
1645 Returns the resulting value number if it exists in the hash table,
1646 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1647 vn_reference_t stored in the hashtable if something is found. */
1649 static tree
1650 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1652 vn_reference_s **slot;
1653 hashval_t hash;
1655 hash = vr->hashcode;
1656 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1657 if (slot)
1659 if (vnresult)
1660 *vnresult = (vn_reference_t)*slot;
1661 return ((vn_reference_t)*slot)->result;
1664 return NULL_TREE;
1668 /* Partial definition tracking support. */
1670 struct pd_range
1672 HOST_WIDE_INT offset;
1673 HOST_WIDE_INT size;
1676 struct pd_data
1678 tree rhs;
1679 HOST_WIDE_INT offset;
1680 HOST_WIDE_INT size;
1683 /* Context for alias walking. */
1685 struct vn_walk_cb_data
1687 vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1688 vn_lookup_kind vn_walk_kind_, bool tbaa_p_)
1689 : vr (vr_), last_vuse_ptr (last_vuse_ptr_),
1690 vn_walk_kind (vn_walk_kind_), tbaa_p (tbaa_p_), known_ranges (NULL)
1692 ao_ref_init (&orig_ref, orig_ref_);
1694 ~vn_walk_cb_data ();
1695 void *push_partial_def (const pd_data& pd, tree, HOST_WIDE_INT);
1697 vn_reference_t vr;
1698 ao_ref orig_ref;
1699 tree *last_vuse_ptr;
1700 vn_lookup_kind vn_walk_kind;
1701 bool tbaa_p;
1703 /* The VDEFs of partial defs we come along. */
1704 auto_vec<pd_data, 2> partial_defs;
1705 /* The first defs range to avoid splay tree setup in most cases. */
1706 pd_range first_range;
1707 tree first_vuse;
1708 splay_tree known_ranges;
1709 obstack ranges_obstack;
1712 vn_walk_cb_data::~vn_walk_cb_data ()
1714 if (known_ranges)
1716 splay_tree_delete (known_ranges);
1717 obstack_free (&ranges_obstack, NULL);
1721 /* pd_range splay-tree helpers. */
1723 static int
1724 pd_range_compare (splay_tree_key offset1p, splay_tree_key offset2p)
1726 HOST_WIDE_INT offset1 = *(HOST_WIDE_INT *)offset1p;
1727 HOST_WIDE_INT offset2 = *(HOST_WIDE_INT *)offset2p;
1728 if (offset1 < offset2)
1729 return -1;
1730 else if (offset1 > offset2)
1731 return 1;
1732 return 0;
1735 static void *
1736 pd_tree_alloc (int size, void *data_)
1738 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
1739 return obstack_alloc (&data->ranges_obstack, size);
1742 static void
1743 pd_tree_dealloc (void *, void *)
1747 /* Push PD to the vector of partial definitions returning a
1748 value when we are ready to combine things with VUSE and MAXSIZEI,
1749 NULL when we want to continue looking for partial defs or -1
1750 on failure. */
1752 void *
1753 vn_walk_cb_data::push_partial_def (const pd_data &pd, tree vuse,
1754 HOST_WIDE_INT maxsizei)
1756 const HOST_WIDE_INT bufsize = 64;
1757 /* We're using a fixed buffer for encoding so fail early if the object
1758 we want to interpret is bigger. */
1759 if (maxsizei > bufsize * BITS_PER_UNIT)
1760 return (void *)-1;
1762 if (partial_defs.is_empty ())
1764 /* If we get a clobber upfront, fail. */
1765 if (TREE_CLOBBER_P (pd.rhs))
1766 return (void *)-1;
1767 partial_defs.safe_push (pd);
1768 first_range.offset = pd.offset;
1769 first_range.size = pd.size;
1770 first_vuse = vuse;
1771 last_vuse_ptr = NULL;
1772 /* Continue looking for partial defs. */
1773 return NULL;
1776 if (!known_ranges)
1778 /* ??? Optimize the case where the 2nd partial def completes things. */
1779 gcc_obstack_init (&ranges_obstack);
1780 known_ranges = splay_tree_new_with_allocator (pd_range_compare, 0, 0,
1781 pd_tree_alloc,
1782 pd_tree_dealloc, this);
1783 splay_tree_insert (known_ranges,
1784 (splay_tree_key)&first_range.offset,
1785 (splay_tree_value)&first_range);
1788 pd_range newr = { pd.offset, pd.size };
1789 splay_tree_node n;
1790 pd_range *r;
1791 /* Lookup the predecessor of offset + 1 and see if we need to merge. */
1792 HOST_WIDE_INT loffset = newr.offset + 1;
1793 if ((n = splay_tree_predecessor (known_ranges, (splay_tree_key)&loffset))
1794 && ((r = (pd_range *)n->value), true)
1795 && ranges_known_overlap_p (r->offset, r->size + 1,
1796 newr.offset, newr.size))
1798 /* Ignore partial defs already covered. Here we also drop shadowed
1799 clobbers arriving here at the floor. */
1800 if (known_subrange_p (newr.offset, newr.size, r->offset, r->size))
1801 return NULL;
1802 r->size = MAX (r->offset + r->size, newr.offset + newr.size) - r->offset;
1804 else
1806 /* newr.offset wasn't covered yet, insert the range. */
1807 r = XOBNEW (&ranges_obstack, pd_range);
1808 *r = newr;
1809 splay_tree_insert (known_ranges, (splay_tree_key)&r->offset,
1810 (splay_tree_value)r);
1812 /* Merge r which now contains newr and is a member of the splay tree with
1813 adjacent overlapping ranges. */
1814 pd_range *rafter;
1815 while ((n = splay_tree_successor (known_ranges, (splay_tree_key)&r->offset))
1816 && ((rafter = (pd_range *)n->value), true)
1817 && ranges_known_overlap_p (r->offset, r->size + 1,
1818 rafter->offset, rafter->size))
1820 r->size = MAX (r->offset + r->size,
1821 rafter->offset + rafter->size) - r->offset;
1822 splay_tree_remove (known_ranges, (splay_tree_key)&rafter->offset);
1824 /* If we get a clobber, fail. */
1825 if (TREE_CLOBBER_P (pd.rhs))
1826 return (void *)-1;
1827 partial_defs.safe_push (pd);
1829 /* Now we have merged newr into the range tree. When we have covered
1830 [offseti, sizei] then the tree will contain exactly one node which has
1831 the desired properties and it will be 'r'. */
1832 if (!known_subrange_p (0, maxsizei / BITS_PER_UNIT, r->offset, r->size))
1833 /* Continue looking for partial defs. */
1834 return NULL;
1836 /* Now simply native encode all partial defs in reverse order. */
1837 unsigned ndefs = partial_defs.length ();
1838 /* We support up to 512-bit values (for V8DFmode). */
1839 unsigned char buffer[bufsize];
1840 int len;
1842 while (!partial_defs.is_empty ())
1844 pd_data pd = partial_defs.pop ();
1845 gcc_checking_assert (pd.offset < bufsize);
1846 if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
1847 /* Empty CONSTRUCTOR. */
1848 memset (buffer + MAX (0, pd.offset),
1849 0, MIN (bufsize - MAX (0, pd.offset),
1850 pd.size + MIN (0, pd.offset)));
1851 else
1853 unsigned pad = 0;
1854 if (BYTES_BIG_ENDIAN
1855 && is_a <scalar_mode> (TYPE_MODE (TREE_TYPE (pd.rhs))))
1857 /* On big-endian the padding is at the 'front' so just skip
1858 the initial bytes. */
1859 fixed_size_mode mode
1860 = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (pd.rhs)));
1861 pad = GET_MODE_SIZE (mode) - pd.size;
1863 len = native_encode_expr (pd.rhs, buffer + MAX (0, pd.offset),
1864 bufsize - MAX (0, pd.offset),
1865 MAX (0, -pd.offset) + pad);
1866 if (len <= 0 || len < (pd.size - MAX (0, -pd.offset)))
1868 if (dump_file && (dump_flags & TDF_DETAILS))
1869 fprintf (dump_file, "Failed to encode %u "
1870 "partial definitions\n", ndefs);
1871 return (void *)-1;
1876 tree type = vr->type;
1877 /* Make sure to interpret in a type that has a range covering the whole
1878 access size. */
1879 if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
1880 type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
1881 tree val = native_interpret_expr (type, buffer, maxsizei / BITS_PER_UNIT);
1882 /* If we chop off bits because the types precision doesn't match the memory
1883 access size this is ok when optimizing reads but not when called from
1884 the DSE code during elimination. */
1885 if (val && type != vr->type)
1887 if (! int_fits_type_p (val, vr->type))
1888 val = NULL_TREE;
1889 else
1890 val = fold_convert (vr->type, val);
1893 if (val)
1895 if (dump_file && (dump_flags & TDF_DETAILS))
1896 fprintf (dump_file,
1897 "Successfully combined %u partial definitions\n", ndefs);
1898 /* ??? If we track partial defs alias-set we could use that if it
1899 is the same for all. Use zero for now. */
1900 return vn_reference_lookup_or_insert_for_pieces
1901 (first_vuse, 0, vr->type, vr->operands, val);
1903 else
1905 if (dump_file && (dump_flags & TDF_DETAILS))
1906 fprintf (dump_file,
1907 "Failed to interpret %u encoded partial definitions\n", ndefs);
1908 return (void *)-1;
1912 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1913 with the current VUSE and performs the expression lookup. */
1915 static void *
1916 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, void *data_)
1918 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
1919 vn_reference_t vr = data->vr;
1920 vn_reference_s **slot;
1921 hashval_t hash;
1923 /* If we have partial definitions recorded we have to go through
1924 vn_reference_lookup_3. */
1925 if (!data->partial_defs.is_empty ())
1926 return NULL;
1928 if (data->last_vuse_ptr)
1929 *data->last_vuse_ptr = vuse;
1931 /* Fixup vuse and hash. */
1932 if (vr->vuse)
1933 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1934 vr->vuse = vuse_ssa_val (vuse);
1935 if (vr->vuse)
1936 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1938 hash = vr->hashcode;
1939 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1940 if (slot)
1941 return *slot;
1943 return NULL;
1946 /* Lookup an existing or insert a new vn_reference entry into the
1947 value table for the VUSE, SET, TYPE, OPERANDS reference which
1948 has the value VALUE which is either a constant or an SSA name. */
1950 static vn_reference_t
1951 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1952 alias_set_type set,
1953 tree type,
1954 vec<vn_reference_op_s,
1955 va_heap> operands,
1956 tree value)
1958 vn_reference_s vr1;
1959 vn_reference_t result;
1960 unsigned value_id;
1961 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1962 vr1.operands = operands;
1963 vr1.type = type;
1964 vr1.set = set;
1965 vr1.hashcode = vn_reference_compute_hash (&vr1);
1966 if (vn_reference_lookup_1 (&vr1, &result))
1967 return result;
1968 if (TREE_CODE (value) == SSA_NAME)
1969 value_id = VN_INFO (value)->value_id;
1970 else
1971 value_id = get_or_alloc_constant_value_id (value);
1972 return vn_reference_insert_pieces (vuse, set, type,
1973 operands.copy (), value, value_id);
1976 /* Return a value-number for RCODE OPS... either by looking up an existing
1977 value-number for the simplified result or by inserting the operation if
1978 INSERT is true. */
1980 static tree
1981 vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert)
1983 tree result = NULL_TREE;
1984 /* We will be creating a value number for
1985 RCODE (OPS...).
1986 So first simplify and lookup this expression to see if it
1987 is already available. */
1988 /* For simplification valueize. */
1989 unsigned i;
1990 for (i = 0; i < res_op->num_ops; ++i)
1991 if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
1993 tree tem = vn_valueize (res_op->ops[i]);
1994 if (!tem)
1995 break;
1996 res_op->ops[i] = tem;
1998 /* If valueization of an operand fails (it is not available), skip
1999 simplification. */
2000 bool res = false;
2001 if (i == res_op->num_ops)
2003 mprts_hook = vn_lookup_simplify_result;
2004 res = res_op->resimplify (NULL, vn_valueize);
2005 mprts_hook = NULL;
2007 gimple *new_stmt = NULL;
2008 if (res
2009 && gimple_simplified_result_is_gimple_val (res_op))
2011 /* The expression is already available. */
2012 result = res_op->ops[0];
2013 /* Valueize it, simplification returns sth in AVAIL only. */
2014 if (TREE_CODE (result) == SSA_NAME)
2015 result = SSA_VAL (result);
2017 else
2019 tree val = vn_lookup_simplify_result (res_op);
2020 if (!val && insert)
2022 gimple_seq stmts = NULL;
2023 result = maybe_push_res_to_seq (res_op, &stmts);
2024 if (result)
2026 gcc_assert (gimple_seq_singleton_p (stmts));
2027 new_stmt = gimple_seq_first_stmt (stmts);
2030 else
2031 /* The expression is already available. */
2032 result = val;
2034 if (new_stmt)
2036 /* The expression is not yet available, value-number lhs to
2037 the new SSA_NAME we created. */
2038 /* Initialize value-number information properly. */
2039 vn_ssa_aux_t result_info = VN_INFO (result);
2040 result_info->valnum = result;
2041 result_info->value_id = get_next_value_id ();
2042 result_info->visited = 1;
2043 gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2044 new_stmt);
2045 result_info->needs_insertion = true;
2046 /* ??? PRE phi-translation inserts NARYs without corresponding
2047 SSA name result. Re-use those but set their result according
2048 to the stmt we just built. */
2049 vn_nary_op_t nary = NULL;
2050 vn_nary_op_lookup_stmt (new_stmt, &nary);
2051 if (nary)
2053 gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2054 nary->u.result = gimple_assign_lhs (new_stmt);
2056 /* As all "inserted" statements are singleton SCCs, insert
2057 to the valid table. This is strictly needed to
2058 avoid re-generating new value SSA_NAMEs for the same
2059 expression during SCC iteration over and over (the
2060 optimistic table gets cleared after each iteration).
2061 We do not need to insert into the optimistic table, as
2062 lookups there will fall back to the valid table. */
2063 else
2065 unsigned int length = vn_nary_length_from_stmt (new_stmt);
2066 vn_nary_op_t vno1
2067 = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2068 vno1->value_id = result_info->value_id;
2069 vno1->length = length;
2070 vno1->predicated_values = 0;
2071 vno1->u.result = result;
2072 init_vn_nary_op_from_stmt (vno1, new_stmt);
2073 vn_nary_op_insert_into (vno1, valid_info->nary, true);
2074 /* Also do not link it into the undo chain. */
2075 last_inserted_nary = vno1->next;
2076 vno1->next = (vn_nary_op_t)(void *)-1;
2078 if (dump_file && (dump_flags & TDF_DETAILS))
2080 fprintf (dump_file, "Inserting name ");
2081 print_generic_expr (dump_file, result);
2082 fprintf (dump_file, " for expression ");
2083 print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2084 fprintf (dump_file, "\n");
2087 return result;
2090 /* Return a value-number for RCODE OPS... either by looking up an existing
2091 value-number for the simplified result or by inserting the operation. */
2093 static tree
2094 vn_nary_build_or_lookup (gimple_match_op *res_op)
2096 return vn_nary_build_or_lookup_1 (res_op, true);
2099 /* Try to simplify the expression RCODE OPS... of type TYPE and return
2100 its value if present. */
2102 tree
2103 vn_nary_simplify (vn_nary_op_t nary)
2105 if (nary->length > gimple_match_op::MAX_NUM_OPS)
2106 return NULL_TREE;
2107 gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2108 nary->type, nary->length);
2109 memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2110 return vn_nary_build_or_lookup_1 (&op, false);
2113 /* Elimination engine. */
2115 class eliminate_dom_walker : public dom_walker
2117 public:
2118 eliminate_dom_walker (cdi_direction, bitmap);
2119 ~eliminate_dom_walker ();
2121 virtual edge before_dom_children (basic_block);
2122 virtual void after_dom_children (basic_block);
2124 virtual tree eliminate_avail (basic_block, tree op);
2125 virtual void eliminate_push_avail (basic_block, tree op);
2126 tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2128 void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2130 unsigned eliminate_cleanup (bool region_p = false);
2132 bool do_pre;
2133 unsigned int el_todo;
2134 unsigned int eliminations;
2135 unsigned int insertions;
2137 /* SSA names that had their defs inserted by PRE if do_pre. */
2138 bitmap inserted_exprs;
2140 /* Blocks with statements that have had their EH properties changed. */
2141 bitmap need_eh_cleanup;
2143 /* Blocks with statements that have had their AB properties changed. */
2144 bitmap need_ab_cleanup;
2146 /* Local state for the eliminate domwalk. */
2147 auto_vec<gimple *> to_remove;
2148 auto_vec<gimple *> to_fixup;
2149 auto_vec<tree> avail;
2150 auto_vec<tree> avail_stack;
2153 /* Adaptor to the elimination engine using RPO availability. */
2155 class rpo_elim : public eliminate_dom_walker
2157 public:
2158 rpo_elim(basic_block entry_)
2159 : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2160 m_avail_freelist (NULL) {}
2162 virtual tree eliminate_avail (basic_block, tree op);
2164 virtual void eliminate_push_avail (basic_block, tree);
2166 basic_block entry;
2167 /* Freelist of avail entries which are allocated from the vn_ssa_aux
2168 obstack. */
2169 vn_avail *m_avail_freelist;
2172 /* Global RPO state for access from hooks. */
2173 static rpo_elim *rpo_avail;
2174 basic_block vn_context_bb;
2176 /* Return true if BASE1 and BASE2 can be adjusted so they have the
2177 same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2178 Otherwise return false. */
2180 static bool
2181 adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2182 tree base2, poly_int64 *offset2)
2184 poly_int64 soff;
2185 if (TREE_CODE (base1) == MEM_REF
2186 && TREE_CODE (base2) == MEM_REF)
2188 if (mem_ref_offset (base1).to_shwi (&soff))
2190 base1 = TREE_OPERAND (base1, 0);
2191 *offset1 += soff * BITS_PER_UNIT;
2193 if (mem_ref_offset (base2).to_shwi (&soff))
2195 base2 = TREE_OPERAND (base2, 0);
2196 *offset2 += soff * BITS_PER_UNIT;
2198 return operand_equal_p (base1, base2, 0);
2200 return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2203 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2204 from the statement defining VUSE and if not successful tries to
2205 translate *REFP and VR_ through an aggregate copy at the definition
2206 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2207 of *REF and *VR. If only disambiguation was performed then
2208 *DISAMBIGUATE_ONLY is set to true. */
2210 static void *
2211 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2212 translate_flags *disambiguate_only)
2214 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2215 vn_reference_t vr = data->vr;
2216 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2217 tree base = ao_ref_base (ref);
2218 HOST_WIDE_INT offseti, maxsizei;
2219 static vec<vn_reference_op_s> lhs_ops;
2220 ao_ref lhs_ref;
2221 bool lhs_ref_ok = false;
2222 poly_int64 copy_size;
2224 /* First try to disambiguate after value-replacing in the definitions LHS. */
2225 if (is_gimple_assign (def_stmt))
2227 tree lhs = gimple_assign_lhs (def_stmt);
2228 bool valueized_anything = false;
2229 /* Avoid re-allocation overhead. */
2230 lhs_ops.truncate (0);
2231 basic_block saved_rpo_bb = vn_context_bb;
2232 vn_context_bb = gimple_bb (def_stmt);
2233 if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2235 copy_reference_ops_from_ref (lhs, &lhs_ops);
2236 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything, true);
2238 vn_context_bb = saved_rpo_bb;
2239 if (valueized_anything)
2241 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
2242 get_alias_set (lhs),
2243 TREE_TYPE (lhs), lhs_ops);
2244 if (lhs_ref_ok
2245 && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2247 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2248 return NULL;
2251 else
2253 ao_ref_init (&lhs_ref, lhs);
2254 lhs_ref_ok = true;
2257 /* Besides valueizing the LHS we can also use access-path based
2258 disambiguation on the original non-valueized ref. */
2259 if (!ref->ref
2260 && lhs_ref_ok
2261 && data->orig_ref.ref)
2263 /* We want to use the non-valueized LHS for this, but avoid redundant
2264 work. */
2265 ao_ref *lref = &lhs_ref;
2266 ao_ref lref_alt;
2267 if (valueized_anything)
2269 ao_ref_init (&lref_alt, lhs);
2270 lref = &lref_alt;
2272 if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2274 *disambiguate_only = (valueized_anything
2275 ? TR_VALUEIZE_AND_DISAMBIGUATE
2276 : TR_DISAMBIGUATE);
2277 return NULL;
2281 /* If we reach a clobbering statement try to skip it and see if
2282 we find a VN result with exactly the same value as the
2283 possible clobber. In this case we can ignore the clobber
2284 and return the found value. */
2285 if (is_gimple_reg_type (TREE_TYPE (lhs))
2286 && types_compatible_p (TREE_TYPE (lhs), vr->type)
2287 && ref->ref)
2289 tree *saved_last_vuse_ptr = data->last_vuse_ptr;
2290 /* Do not update last_vuse_ptr in vn_reference_lookup_2. */
2291 data->last_vuse_ptr = NULL;
2292 tree saved_vuse = vr->vuse;
2293 hashval_t saved_hashcode = vr->hashcode;
2294 void *res = vn_reference_lookup_2 (ref, gimple_vuse (def_stmt), data);
2295 /* Need to restore vr->vuse and vr->hashcode. */
2296 vr->vuse = saved_vuse;
2297 vr->hashcode = saved_hashcode;
2298 data->last_vuse_ptr = saved_last_vuse_ptr;
2299 if (res && res != (void *)-1)
2301 vn_reference_t vnresult = (vn_reference_t) res;
2302 tree rhs = gimple_assign_rhs1 (def_stmt);
2303 if (TREE_CODE (rhs) == SSA_NAME)
2304 rhs = SSA_VAL (rhs);
2305 if (vnresult->result
2306 && operand_equal_p (vnresult->result, rhs, 0)
2307 /* We have to honor our promise about union type punning
2308 and also support arbitrary overlaps with
2309 -fno-strict-aliasing. So simply resort to alignment to
2310 rule out overlaps. Do this check last because it is
2311 quite expensive compared to the hash-lookup above. */
2312 && multiple_p (get_object_alignment (ref->ref), ref->size)
2313 && multiple_p (get_object_alignment (lhs), ref->size))
2314 return res;
2318 else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2319 && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2320 && gimple_call_num_args (def_stmt) <= 4)
2322 /* For builtin calls valueize its arguments and call the
2323 alias oracle again. Valueization may improve points-to
2324 info of pointers and constify size and position arguments.
2325 Originally this was motivated by PR61034 which has
2326 conditional calls to free falsely clobbering ref because
2327 of imprecise points-to info of the argument. */
2328 tree oldargs[4];
2329 bool valueized_anything = false;
2330 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2332 oldargs[i] = gimple_call_arg (def_stmt, i);
2333 tree val = vn_valueize (oldargs[i]);
2334 if (val != oldargs[i])
2336 gimple_call_set_arg (def_stmt, i, val);
2337 valueized_anything = true;
2340 if (valueized_anything)
2342 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2343 ref);
2344 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2345 gimple_call_set_arg (def_stmt, i, oldargs[i]);
2346 if (!res)
2348 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2349 return NULL;
2354 if (*disambiguate_only > TR_TRANSLATE)
2355 return (void *)-1;
2357 /* If we cannot constrain the size of the reference we cannot
2358 test if anything kills it. */
2359 if (!ref->max_size_known_p ())
2360 return (void *)-1;
2362 poly_int64 offset = ref->offset;
2363 poly_int64 maxsize = ref->max_size;
2365 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2366 from that definition.
2367 1) Memset. */
2368 if (is_gimple_reg_type (vr->type)
2369 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2370 && (integer_zerop (gimple_call_arg (def_stmt, 1))
2371 || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2372 || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2373 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
2374 && offset.is_constant (&offseti)
2375 && offseti % BITS_PER_UNIT == 0))
2376 && poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2377 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2378 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2380 tree base2;
2381 poly_int64 offset2, size2, maxsize2;
2382 bool reverse;
2383 tree ref2 = gimple_call_arg (def_stmt, 0);
2384 if (TREE_CODE (ref2) == SSA_NAME)
2386 ref2 = SSA_VAL (ref2);
2387 if (TREE_CODE (ref2) == SSA_NAME
2388 && (TREE_CODE (base) != MEM_REF
2389 || TREE_OPERAND (base, 0) != ref2))
2391 gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2392 if (gimple_assign_single_p (def_stmt)
2393 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2394 ref2 = gimple_assign_rhs1 (def_stmt);
2397 if (TREE_CODE (ref2) == ADDR_EXPR)
2399 ref2 = TREE_OPERAND (ref2, 0);
2400 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
2401 &reverse);
2402 if (!known_size_p (maxsize2)
2403 || !known_eq (maxsize2, size2)
2404 || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
2405 return (void *)-1;
2407 else if (TREE_CODE (ref2) == SSA_NAME)
2409 poly_int64 soff;
2410 if (TREE_CODE (base) != MEM_REF
2411 || !(mem_ref_offset (base) << LOG2_BITS_PER_UNIT).to_shwi (&soff))
2412 return (void *)-1;
2413 offset += soff;
2414 offset2 = 0;
2415 if (TREE_OPERAND (base, 0) != ref2)
2417 gimple *def = SSA_NAME_DEF_STMT (ref2);
2418 if (is_gimple_assign (def)
2419 && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
2420 && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
2421 && poly_int_tree_p (gimple_assign_rhs2 (def))
2422 && (wi::to_poly_offset (gimple_assign_rhs2 (def))
2423 << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
2425 ref2 = gimple_assign_rhs1 (def);
2426 if (TREE_CODE (ref2) == SSA_NAME)
2427 ref2 = SSA_VAL (ref2);
2429 else
2430 return (void *)-1;
2433 else
2434 return (void *)-1;
2435 tree len = gimple_call_arg (def_stmt, 2);
2436 HOST_WIDE_INT leni, offset2i, offseti;
2437 if (data->partial_defs.is_empty ()
2438 && known_subrange_p (offset, maxsize, offset2,
2439 wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
2441 tree val;
2442 if (integer_zerop (gimple_call_arg (def_stmt, 1)))
2443 val = build_zero_cst (vr->type);
2444 else if (INTEGRAL_TYPE_P (vr->type)
2445 && known_eq (ref->size, 8))
2447 gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
2448 vr->type, gimple_call_arg (def_stmt, 1));
2449 val = vn_nary_build_or_lookup (&res_op);
2450 if (!val
2451 || (TREE_CODE (val) == SSA_NAME
2452 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
2453 return (void *)-1;
2455 else
2457 unsigned len = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type));
2458 unsigned char *buf = XALLOCAVEC (unsigned char, len);
2459 memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
2460 len);
2461 val = native_interpret_expr (vr->type, buf, len);
2462 if (!val)
2463 return (void *)-1;
2465 return vn_reference_lookup_or_insert_for_pieces
2466 (vuse, 0, vr->type, vr->operands, val);
2468 /* For now handle clearing memory with partial defs. */
2469 else if (known_eq (ref->size, maxsize)
2470 && integer_zerop (gimple_call_arg (def_stmt, 1))
2471 && tree_to_poly_int64 (len).is_constant (&leni)
2472 && offset.is_constant (&offseti)
2473 && offset2.is_constant (&offset2i)
2474 && maxsize.is_constant (&maxsizei))
2476 pd_data pd;
2477 pd.rhs = build_constructor (NULL_TREE, NULL);
2478 pd.offset = (offset2i - offseti) / BITS_PER_UNIT;
2479 pd.size = leni;
2480 return data->push_partial_def (pd, vuse, maxsizei);
2484 /* 2) Assignment from an empty CONSTRUCTOR. */
2485 else if (is_gimple_reg_type (vr->type)
2486 && gimple_assign_single_p (def_stmt)
2487 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
2488 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
2490 tree lhs = gimple_assign_lhs (def_stmt);
2491 tree base2;
2492 poly_int64 offset2, size2, maxsize2;
2493 HOST_WIDE_INT offset2i, size2i;
2494 bool reverse;
2495 if (lhs_ref_ok)
2497 base2 = ao_ref_base (&lhs_ref);
2498 offset2 = lhs_ref.offset;
2499 size2 = lhs_ref.size;
2500 maxsize2 = lhs_ref.max_size;
2501 reverse = reverse_storage_order_for_component_p (lhs);
2503 else
2504 base2 = get_ref_base_and_extent (lhs,
2505 &offset2, &size2, &maxsize2, &reverse);
2506 if (known_size_p (maxsize2)
2507 && known_eq (maxsize2, size2)
2508 && adjust_offsets_for_equal_base_address (base, &offset,
2509 base2, &offset2))
2511 if (data->partial_defs.is_empty ()
2512 && known_subrange_p (offset, maxsize, offset2, size2))
2514 /* While technically undefined behavior do not optimize
2515 a full read from a clobber. */
2516 if (gimple_clobber_p (def_stmt))
2517 return (void *)-1;
2518 tree val = build_zero_cst (vr->type);
2519 return vn_reference_lookup_or_insert_for_pieces
2520 (vuse, get_alias_set (lhs), vr->type, vr->operands, val);
2522 else if (known_eq (ref->size, maxsize)
2523 && maxsize.is_constant (&maxsizei)
2524 && maxsizei % BITS_PER_UNIT == 0
2525 && offset.is_constant (&offseti)
2526 && offseti % BITS_PER_UNIT == 0
2527 && offset2.is_constant (&offset2i)
2528 && offset2i % BITS_PER_UNIT == 0
2529 && size2.is_constant (&size2i)
2530 && size2i % BITS_PER_UNIT == 0)
2532 /* Let clobbers be consumed by the partial-def tracker
2533 which can choose to ignore them if they are shadowed
2534 by a later def. */
2535 pd_data pd;
2536 pd.rhs = gimple_assign_rhs1 (def_stmt);
2537 pd.offset = (offset2i - offseti) / BITS_PER_UNIT;
2538 pd.size = size2i / BITS_PER_UNIT;
2539 return data->push_partial_def (pd, vuse, maxsizei);
2544 /* 3) Assignment from a constant. We can use folds native encode/interpret
2545 routines to extract the assigned bits. */
2546 else if (known_eq (ref->size, maxsize)
2547 && is_gimple_reg_type (vr->type)
2548 && !contains_storage_order_barrier_p (vr->operands)
2549 && gimple_assign_single_p (def_stmt)
2550 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
2551 /* native_encode and native_decode operate on arrays of bytes
2552 and so fundamentally need a compile-time size and offset. */
2553 && maxsize.is_constant (&maxsizei)
2554 && maxsizei % BITS_PER_UNIT == 0
2555 && offset.is_constant (&offseti)
2556 && offseti % BITS_PER_UNIT == 0
2557 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
2558 || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
2559 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
2561 tree lhs = gimple_assign_lhs (def_stmt);
2562 tree base2;
2563 poly_int64 offset2, size2, maxsize2;
2564 HOST_WIDE_INT offset2i, size2i;
2565 bool reverse;
2566 if (lhs_ref_ok)
2568 base2 = ao_ref_base (&lhs_ref);
2569 offset2 = lhs_ref.offset;
2570 size2 = lhs_ref.size;
2571 maxsize2 = lhs_ref.max_size;
2572 reverse = reverse_storage_order_for_component_p (lhs);
2574 else
2575 base2 = get_ref_base_and_extent (lhs,
2576 &offset2, &size2, &maxsize2, &reverse);
2577 if (base2
2578 && !reverse
2579 && known_eq (maxsize2, size2)
2580 && multiple_p (size2, BITS_PER_UNIT)
2581 && multiple_p (offset2, BITS_PER_UNIT)
2582 && adjust_offsets_for_equal_base_address (base, &offset,
2583 base2, &offset2)
2584 && offset.is_constant (&offseti)
2585 && offset2.is_constant (&offset2i)
2586 && size2.is_constant (&size2i))
2588 if (data->partial_defs.is_empty ()
2589 && known_subrange_p (offseti, maxsizei, offset2, size2))
2591 /* We support up to 512-bit values (for V8DFmode). */
2592 unsigned char buffer[64];
2593 int len;
2595 tree rhs = gimple_assign_rhs1 (def_stmt);
2596 if (TREE_CODE (rhs) == SSA_NAME)
2597 rhs = SSA_VAL (rhs);
2598 unsigned pad = 0;
2599 if (BYTES_BIG_ENDIAN
2600 && is_a <scalar_mode> (TYPE_MODE (TREE_TYPE (rhs))))
2602 /* On big-endian the padding is at the 'front' so
2603 just skip the initial bytes. */
2604 fixed_size_mode mode
2605 = as_a <fixed_size_mode> (TYPE_MODE (TREE_TYPE (rhs)));
2606 pad = GET_MODE_SIZE (mode) - size2i / BITS_PER_UNIT;
2608 len = native_encode_expr (rhs,
2609 buffer, sizeof (buffer),
2610 ((offseti - offset2i) / BITS_PER_UNIT
2611 + pad));
2612 if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
2614 tree type = vr->type;
2615 /* Make sure to interpret in a type that has a range
2616 covering the whole access size. */
2617 if (INTEGRAL_TYPE_P (vr->type)
2618 && maxsizei != TYPE_PRECISION (vr->type))
2619 type = build_nonstandard_integer_type (maxsizei,
2620 TYPE_UNSIGNED (type));
2621 tree val = native_interpret_expr (type, buffer,
2622 maxsizei / BITS_PER_UNIT);
2623 /* If we chop off bits because the types precision doesn't
2624 match the memory access size this is ok when optimizing
2625 reads but not when called from the DSE code during
2626 elimination. */
2627 if (val
2628 && type != vr->type)
2630 if (! int_fits_type_p (val, vr->type))
2631 val = NULL_TREE;
2632 else
2633 val = fold_convert (vr->type, val);
2636 if (val)
2637 return vn_reference_lookup_or_insert_for_pieces
2638 (vuse, get_alias_set (lhs), vr->type, vr->operands, val);
2641 else if (ranges_known_overlap_p (offseti, maxsizei, offset2i, size2i))
2643 pd_data pd;
2644 tree rhs = gimple_assign_rhs1 (def_stmt);
2645 if (TREE_CODE (rhs) == SSA_NAME)
2646 rhs = SSA_VAL (rhs);
2647 pd.rhs = rhs;
2648 pd.offset = (offset2i - offseti) / BITS_PER_UNIT;
2649 pd.size = size2i / BITS_PER_UNIT;
2650 return data->push_partial_def (pd, vuse, maxsizei);
2655 /* 4) Assignment from an SSA name which definition we may be able
2656 to access pieces from. */
2657 else if (known_eq (ref->size, maxsize)
2658 && is_gimple_reg_type (vr->type)
2659 && !contains_storage_order_barrier_p (vr->operands)
2660 && gimple_assign_single_p (def_stmt)
2661 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
2662 /* A subset of partial defs from non-constants can be handled
2663 by for example inserting a CONSTRUCTOR, a COMPLEX_EXPR or
2664 even a (series of) BIT_INSERT_EXPR hoping for simplifications
2665 downstream, not so much for actually doing the insertion. */
2666 && data->partial_defs.is_empty ())
2668 tree lhs = gimple_assign_lhs (def_stmt);
2669 tree base2;
2670 poly_int64 offset2, size2, maxsize2;
2671 bool reverse;
2672 if (lhs_ref_ok)
2674 base2 = ao_ref_base (&lhs_ref);
2675 offset2 = lhs_ref.offset;
2676 size2 = lhs_ref.size;
2677 maxsize2 = lhs_ref.max_size;
2678 reverse = reverse_storage_order_for_component_p (lhs);
2680 else
2681 base2 = get_ref_base_and_extent (lhs,
2682 &offset2, &size2, &maxsize2, &reverse);
2683 tree def_rhs = gimple_assign_rhs1 (def_stmt);
2684 if (!reverse
2685 && known_size_p (maxsize2)
2686 && known_eq (maxsize2, size2)
2687 && adjust_offsets_for_equal_base_address (base, &offset,
2688 base2, &offset2)
2689 && known_subrange_p (offset, maxsize, offset2, size2)
2690 /* ??? We can't handle bitfield precision extracts without
2691 either using an alternate type for the BIT_FIELD_REF and
2692 then doing a conversion or possibly adjusting the offset
2693 according to endianness. */
2694 && (! INTEGRAL_TYPE_P (vr->type)
2695 || known_eq (ref->size, TYPE_PRECISION (vr->type)))
2696 && multiple_p (ref->size, BITS_PER_UNIT))
2698 if (known_eq (ref->size, size2))
2699 return vn_reference_lookup_or_insert_for_pieces
2700 (vuse, get_alias_set (lhs), vr->type, vr->operands,
2701 SSA_VAL (def_rhs));
2702 else if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
2703 || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
2705 gimple_match_op op (gimple_match_cond::UNCOND,
2706 BIT_FIELD_REF, vr->type,
2707 vn_valueize (def_rhs),
2708 bitsize_int (ref->size),
2709 bitsize_int (offset - offset2));
2710 tree val = vn_nary_build_or_lookup (&op);
2711 if (val
2712 && (TREE_CODE (val) != SSA_NAME
2713 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
2714 return vn_reference_lookup_or_insert_for_pieces
2715 (vuse, get_alias_set (lhs), vr->type, vr->operands, val);
2720 /* 5) For aggregate copies translate the reference through them if
2721 the copy kills ref. */
2722 else if (data->vn_walk_kind == VN_WALKREWRITE
2723 && gimple_assign_single_p (def_stmt)
2724 && (DECL_P (gimple_assign_rhs1 (def_stmt))
2725 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
2726 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
2728 tree base2;
2729 int i, j, k;
2730 auto_vec<vn_reference_op_s> rhs;
2731 vn_reference_op_t vro;
2732 ao_ref r;
2734 if (!lhs_ref_ok)
2735 return (void *)-1;
2737 /* See if the assignment kills REF. */
2738 base2 = ao_ref_base (&lhs_ref);
2739 if (!lhs_ref.max_size_known_p ()
2740 || (base != base2
2741 && (TREE_CODE (base) != MEM_REF
2742 || TREE_CODE (base2) != MEM_REF
2743 || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
2744 || !tree_int_cst_equal (TREE_OPERAND (base, 1),
2745 TREE_OPERAND (base2, 1))))
2746 || !stmt_kills_ref_p (def_stmt, ref))
2747 return (void *)-1;
2749 /* Find the common base of ref and the lhs. lhs_ops already
2750 contains valueized operands for the lhs. */
2751 i = vr->operands.length () - 1;
2752 j = lhs_ops.length () - 1;
2753 while (j >= 0 && i >= 0
2754 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
2756 i--;
2757 j--;
2760 /* ??? The innermost op should always be a MEM_REF and we already
2761 checked that the assignment to the lhs kills vr. Thus for
2762 aggregate copies using char[] types the vn_reference_op_eq
2763 may fail when comparing types for compatibility. But we really
2764 don't care here - further lookups with the rewritten operands
2765 will simply fail if we messed up types too badly. */
2766 poly_int64 extra_off = 0;
2767 if (j == 0 && i >= 0
2768 && lhs_ops[0].opcode == MEM_REF
2769 && maybe_ne (lhs_ops[0].off, -1))
2771 if (known_eq (lhs_ops[0].off, vr->operands[i].off))
2772 i--, j--;
2773 else if (vr->operands[i].opcode == MEM_REF
2774 && maybe_ne (vr->operands[i].off, -1))
2776 extra_off = vr->operands[i].off - lhs_ops[0].off;
2777 i--, j--;
2781 /* i now points to the first additional op.
2782 ??? LHS may not be completely contained in VR, one or more
2783 VIEW_CONVERT_EXPRs could be in its way. We could at least
2784 try handling outermost VIEW_CONVERT_EXPRs. */
2785 if (j != -1)
2786 return (void *)-1;
2788 /* Punt if the additional ops contain a storage order barrier. */
2789 for (k = i; k >= 0; k--)
2791 vro = &vr->operands[k];
2792 if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
2793 return (void *)-1;
2796 /* Now re-write REF to be based on the rhs of the assignment. */
2797 tree rhs1 = gimple_assign_rhs1 (def_stmt);
2798 copy_reference_ops_from_ref (rhs1, &rhs);
2800 /* Apply an extra offset to the inner MEM_REF of the RHS. */
2801 if (maybe_ne (extra_off, 0))
2803 if (rhs.length () < 2)
2804 return (void *)-1;
2805 int ix = rhs.length () - 2;
2806 if (rhs[ix].opcode != MEM_REF
2807 || known_eq (rhs[ix].off, -1))
2808 return (void *)-1;
2809 rhs[ix].off += extra_off;
2810 rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
2811 build_int_cst (TREE_TYPE (rhs[ix].op0),
2812 extra_off));
2815 /* We need to pre-pend vr->operands[0..i] to rhs. */
2816 vec<vn_reference_op_s> old = vr->operands;
2817 if (i + 1 + rhs.length () > vr->operands.length ())
2818 vr->operands.safe_grow (i + 1 + rhs.length ());
2819 else
2820 vr->operands.truncate (i + 1 + rhs.length ());
2821 FOR_EACH_VEC_ELT (rhs, j, vro)
2822 vr->operands[i + 1 + j] = *vro;
2823 vr->operands = valueize_refs (vr->operands);
2824 if (old == shared_lookup_references)
2825 shared_lookup_references = vr->operands;
2826 vr->hashcode = vn_reference_compute_hash (vr);
2828 /* Try folding the new reference to a constant. */
2829 tree val = fully_constant_vn_reference_p (vr);
2830 if (val)
2832 if (data->partial_defs.is_empty ())
2833 return vn_reference_lookup_or_insert_for_pieces
2834 (vuse, get_alias_set (rhs1), vr->type, vr->operands, val);
2835 /* This is the only interesting case for partial-def handling
2836 coming from targets that like to gimplify init-ctors as
2837 aggregate copies from constant data like aarch64 for
2838 PR83518. */
2839 if (maxsize.is_constant (&maxsizei)
2840 && known_eq (ref->size, maxsize))
2842 pd_data pd;
2843 pd.rhs = val;
2844 pd.offset = 0;
2845 pd.size = maxsizei / BITS_PER_UNIT;
2846 return data->push_partial_def (pd, vuse, maxsizei);
2850 /* Continuing with partial defs isn't easily possible here, we
2851 have to find a full def from further lookups from here. Probably
2852 not worth the special-casing everywhere. */
2853 if (!data->partial_defs.is_empty ())
2854 return (void *)-1;
2856 /* Adjust *ref from the new operands. */
2857 if (!ao_ref_init_from_vn_reference (&r, get_alias_set (rhs1),
2858 vr->type, vr->operands))
2859 return (void *)-1;
2860 /* This can happen with bitfields. */
2861 if (maybe_ne (ref->size, r.size))
2862 return (void *)-1;
2863 *ref = r;
2865 /* Do not update last seen VUSE after translating. */
2866 data->last_vuse_ptr = NULL;
2867 /* Invalidate the original access path since it now contains
2868 the wrong base. */
2869 data->orig_ref.ref = NULL_TREE;
2871 /* Keep looking for the adjusted *REF / VR pair. */
2872 return NULL;
2875 /* 6) For memcpy copies translate the reference through them if
2876 the copy kills ref. */
2877 else if (data->vn_walk_kind == VN_WALKREWRITE
2878 && is_gimple_reg_type (vr->type)
2879 /* ??? Handle BCOPY as well. */
2880 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
2881 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
2882 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
2883 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2884 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
2885 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
2886 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
2887 && poly_int_tree_p (gimple_call_arg (def_stmt, 2), &copy_size)
2888 /* Handling this is more complicated, give up for now. */
2889 && data->partial_defs.is_empty ())
2891 tree lhs, rhs;
2892 ao_ref r;
2893 poly_int64 rhs_offset, lhs_offset;
2894 vn_reference_op_s op;
2895 poly_uint64 mem_offset;
2896 poly_int64 at, byte_maxsize;
2898 /* Only handle non-variable, addressable refs. */
2899 if (maybe_ne (ref->size, maxsize)
2900 || !multiple_p (offset, BITS_PER_UNIT, &at)
2901 || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
2902 return (void *)-1;
2904 /* Extract a pointer base and an offset for the destination. */
2905 lhs = gimple_call_arg (def_stmt, 0);
2906 lhs_offset = 0;
2907 if (TREE_CODE (lhs) == SSA_NAME)
2909 lhs = vn_valueize (lhs);
2910 if (TREE_CODE (lhs) == SSA_NAME)
2912 gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
2913 if (gimple_assign_single_p (def_stmt)
2914 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2915 lhs = gimple_assign_rhs1 (def_stmt);
2918 if (TREE_CODE (lhs) == ADDR_EXPR)
2920 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
2921 &lhs_offset);
2922 if (!tem)
2923 return (void *)-1;
2924 if (TREE_CODE (tem) == MEM_REF
2925 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
2927 lhs = TREE_OPERAND (tem, 0);
2928 if (TREE_CODE (lhs) == SSA_NAME)
2929 lhs = vn_valueize (lhs);
2930 lhs_offset += mem_offset;
2932 else if (DECL_P (tem))
2933 lhs = build_fold_addr_expr (tem);
2934 else
2935 return (void *)-1;
2937 if (TREE_CODE (lhs) != SSA_NAME
2938 && TREE_CODE (lhs) != ADDR_EXPR)
2939 return (void *)-1;
2941 /* Extract a pointer base and an offset for the source. */
2942 rhs = gimple_call_arg (def_stmt, 1);
2943 rhs_offset = 0;
2944 if (TREE_CODE (rhs) == SSA_NAME)
2945 rhs = vn_valueize (rhs);
2946 if (TREE_CODE (rhs) == ADDR_EXPR)
2948 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
2949 &rhs_offset);
2950 if (!tem)
2951 return (void *)-1;
2952 if (TREE_CODE (tem) == MEM_REF
2953 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
2955 rhs = TREE_OPERAND (tem, 0);
2956 rhs_offset += mem_offset;
2958 else if (DECL_P (tem)
2959 || TREE_CODE (tem) == STRING_CST)
2960 rhs = build_fold_addr_expr (tem);
2961 else
2962 return (void *)-1;
2964 if (TREE_CODE (rhs) == SSA_NAME)
2965 rhs = SSA_VAL (rhs);
2966 else if (TREE_CODE (rhs) != ADDR_EXPR)
2967 return (void *)-1;
2969 /* The bases of the destination and the references have to agree. */
2970 if (TREE_CODE (base) == MEM_REF)
2972 if (TREE_OPERAND (base, 0) != lhs
2973 || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
2974 return (void *) -1;
2975 at += mem_offset;
2977 else if (!DECL_P (base)
2978 || TREE_CODE (lhs) != ADDR_EXPR
2979 || TREE_OPERAND (lhs, 0) != base)
2980 return (void *)-1;
2982 /* If the access is completely outside of the memcpy destination
2983 area there is no aliasing. */
2984 if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
2985 return NULL;
2986 /* And the access has to be contained within the memcpy destination. */
2987 if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
2988 return (void *)-1;
2990 /* Make room for 2 operands in the new reference. */
2991 if (vr->operands.length () < 2)
2993 vec<vn_reference_op_s> old = vr->operands;
2994 vr->operands.safe_grow_cleared (2);
2995 if (old == shared_lookup_references)
2996 shared_lookup_references = vr->operands;
2998 else
2999 vr->operands.truncate (2);
3001 /* The looked-through reference is a simple MEM_REF. */
3002 memset (&op, 0, sizeof (op));
3003 op.type = vr->type;
3004 op.opcode = MEM_REF;
3005 op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3006 op.off = at - lhs_offset + rhs_offset;
3007 vr->operands[0] = op;
3008 op.type = TREE_TYPE (rhs);
3009 op.opcode = TREE_CODE (rhs);
3010 op.op0 = rhs;
3011 op.off = -1;
3012 vr->operands[1] = op;
3013 vr->hashcode = vn_reference_compute_hash (vr);
3015 /* Try folding the new reference to a constant. */
3016 tree val = fully_constant_vn_reference_p (vr);
3017 if (val)
3018 return vn_reference_lookup_or_insert_for_pieces
3019 (vuse, 0, vr->type, vr->operands, val);
3021 /* Adjust *ref from the new operands. */
3022 if (!ao_ref_init_from_vn_reference (&r, 0, vr->type, vr->operands))
3023 return (void *)-1;
3024 /* This can happen with bitfields. */
3025 if (maybe_ne (ref->size, r.size))
3026 return (void *)-1;
3027 *ref = r;
3029 /* Do not update last seen VUSE after translating. */
3030 data->last_vuse_ptr = NULL;
3031 /* Invalidate the original access path since it now contains
3032 the wrong base. */
3033 data->orig_ref.ref = NULL_TREE;
3035 /* Keep looking for the adjusted *REF / VR pair. */
3036 return NULL;
3039 /* Bail out and stop walking. */
3040 return (void *)-1;
3043 /* Return a reference op vector from OP that can be used for
3044 vn_reference_lookup_pieces. The caller is responsible for releasing
3045 the vector. */
3047 vec<vn_reference_op_s>
3048 vn_reference_operands_for_lookup (tree op)
3050 bool valueized;
3051 return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
3054 /* Lookup a reference operation by it's parts, in the current hash table.
3055 Returns the resulting value number if it exists in the hash table,
3056 NULL_TREE otherwise. VNRESULT will be filled in with the actual
3057 vn_reference_t stored in the hashtable if something is found. */
3059 tree
3060 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
3061 vec<vn_reference_op_s> operands,
3062 vn_reference_t *vnresult, vn_lookup_kind kind)
3064 struct vn_reference_s vr1;
3065 vn_reference_t tmp;
3066 tree cst;
3068 if (!vnresult)
3069 vnresult = &tmp;
3070 *vnresult = NULL;
3072 vr1.vuse = vuse_ssa_val (vuse);
3073 shared_lookup_references.truncate (0);
3074 shared_lookup_references.safe_grow (operands.length ());
3075 memcpy (shared_lookup_references.address (),
3076 operands.address (),
3077 sizeof (vn_reference_op_s)
3078 * operands.length ());
3079 vr1.operands = operands = shared_lookup_references
3080 = valueize_refs (shared_lookup_references);
3081 vr1.type = type;
3082 vr1.set = set;
3083 vr1.hashcode = vn_reference_compute_hash (&vr1);
3084 if ((cst = fully_constant_vn_reference_p (&vr1)))
3085 return cst;
3087 vn_reference_lookup_1 (&vr1, vnresult);
3088 if (!*vnresult
3089 && kind != VN_NOWALK
3090 && vr1.vuse)
3092 ao_ref r;
3093 unsigned limit = param_sccvn_max_alias_queries_per_access;
3094 vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true);
3095 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
3096 *vnresult =
3097 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse, true,
3098 vn_reference_lookup_2,
3099 vn_reference_lookup_3,
3100 vuse_valueize, limit, &data);
3101 gcc_checking_assert (vr1.operands == shared_lookup_references);
3104 if (*vnresult)
3105 return (*vnresult)->result;
3107 return NULL_TREE;
3110 /* Lookup OP in the current hash table, and return the resulting value
3111 number if it exists in the hash table. Return NULL_TREE if it does
3112 not exist in the hash table or if the result field of the structure
3113 was NULL.. VNRESULT will be filled in with the vn_reference_t
3114 stored in the hashtable if one exists. When TBAA_P is false assume
3115 we are looking up a store and treat it as having alias-set zero.
3116 *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded. */
3118 tree
3119 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
3120 vn_reference_t *vnresult, bool tbaa_p, tree *last_vuse_ptr)
3122 vec<vn_reference_op_s> operands;
3123 struct vn_reference_s vr1;
3124 tree cst;
3125 bool valuezied_anything;
3127 if (vnresult)
3128 *vnresult = NULL;
3130 vr1.vuse = vuse_ssa_val (vuse);
3131 vr1.operands = operands
3132 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
3133 vr1.type = TREE_TYPE (op);
3134 vr1.set = get_alias_set (op);
3135 vr1.hashcode = vn_reference_compute_hash (&vr1);
3136 if ((cst = fully_constant_vn_reference_p (&vr1)))
3137 return cst;
3139 if (kind != VN_NOWALK
3140 && vr1.vuse)
3142 vn_reference_t wvnresult;
3143 ao_ref r;
3144 unsigned limit = param_sccvn_max_alias_queries_per_access;
3145 /* Make sure to use a valueized reference if we valueized anything.
3146 Otherwise preserve the full reference for advanced TBAA. */
3147 if (!valuezied_anything
3148 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
3149 vr1.operands))
3150 ao_ref_init (&r, op);
3151 vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
3152 last_vuse_ptr, kind, tbaa_p);
3153 wvnresult =
3154 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p,
3155 vn_reference_lookup_2,
3156 vn_reference_lookup_3,
3157 vuse_valueize, limit, &data);
3158 gcc_checking_assert (vr1.operands == shared_lookup_references);
3159 if (wvnresult)
3161 if (vnresult)
3162 *vnresult = wvnresult;
3163 return wvnresult->result;
3166 return NULL_TREE;
3169 return vn_reference_lookup_1 (&vr1, vnresult);
3172 /* Lookup CALL in the current hash table and return the entry in
3173 *VNRESULT if found. Populates *VR for the hashtable lookup. */
3175 void
3176 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
3177 vn_reference_t vr)
3179 if (vnresult)
3180 *vnresult = NULL;
3182 tree vuse = gimple_vuse (call);
3184 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
3185 vr->operands = valueize_shared_reference_ops_from_call (call);
3186 vr->type = gimple_expr_type (call);
3187 vr->set = 0;
3188 vr->hashcode = vn_reference_compute_hash (vr);
3189 vn_reference_lookup_1 (vr, vnresult);
3192 /* Insert OP into the current hash table with a value number of RESULT. */
3194 static void
3195 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
3197 vn_reference_s **slot;
3198 vn_reference_t vr1;
3199 bool tem;
3201 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
3202 if (TREE_CODE (result) == SSA_NAME)
3203 vr1->value_id = VN_INFO (result)->value_id;
3204 else
3205 vr1->value_id = get_or_alloc_constant_value_id (result);
3206 vr1->vuse = vuse_ssa_val (vuse);
3207 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
3208 vr1->type = TREE_TYPE (op);
3209 vr1->set = get_alias_set (op);
3210 vr1->hashcode = vn_reference_compute_hash (vr1);
3211 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
3212 vr1->result_vdef = vdef;
3214 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
3215 INSERT);
3217 /* Because IL walking on reference lookup can end up visiting
3218 a def that is only to be visited later in iteration order
3219 when we are about to make an irreducible region reducible
3220 the def can be effectively processed and its ref being inserted
3221 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
3222 but save a lookup if we deal with already inserted refs here. */
3223 if (*slot)
3225 /* We cannot assert that we have the same value either because
3226 when disentangling an irreducible region we may end up visiting
3227 a use before the corresponding def. That's a missed optimization
3228 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
3229 if (dump_file && (dump_flags & TDF_DETAILS)
3230 && !operand_equal_p ((*slot)->result, vr1->result, 0))
3232 fprintf (dump_file, "Keeping old value ");
3233 print_generic_expr (dump_file, (*slot)->result);
3234 fprintf (dump_file, " because of collision\n");
3236 free_reference (vr1);
3237 obstack_free (&vn_tables_obstack, vr1);
3238 return;
3241 *slot = vr1;
3242 vr1->next = last_inserted_ref;
3243 last_inserted_ref = vr1;
3246 /* Insert a reference by it's pieces into the current hash table with
3247 a value number of RESULT. Return the resulting reference
3248 structure we created. */
3250 vn_reference_t
3251 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
3252 vec<vn_reference_op_s> operands,
3253 tree result, unsigned int value_id)
3256 vn_reference_s **slot;
3257 vn_reference_t vr1;
3259 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
3260 vr1->value_id = value_id;
3261 vr1->vuse = vuse_ssa_val (vuse);
3262 vr1->operands = valueize_refs (operands);
3263 vr1->type = type;
3264 vr1->set = set;
3265 vr1->hashcode = vn_reference_compute_hash (vr1);
3266 if (result && TREE_CODE (result) == SSA_NAME)
3267 result = SSA_VAL (result);
3268 vr1->result = result;
3270 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
3271 INSERT);
3273 /* At this point we should have all the things inserted that we have
3274 seen before, and we should never try inserting something that
3275 already exists. */
3276 gcc_assert (!*slot);
3278 *slot = vr1;
3279 vr1->next = last_inserted_ref;
3280 last_inserted_ref = vr1;
3281 return vr1;
3284 /* Compute and return the hash value for nary operation VBO1. */
3286 static hashval_t
3287 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
3289 inchash::hash hstate;
3290 unsigned i;
3292 for (i = 0; i < vno1->length; ++i)
3293 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
3294 vno1->op[i] = SSA_VAL (vno1->op[i]);
3296 if (((vno1->length == 2
3297 && commutative_tree_code (vno1->opcode))
3298 || (vno1->length == 3
3299 && commutative_ternary_tree_code (vno1->opcode)))
3300 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
3301 std::swap (vno1->op[0], vno1->op[1]);
3302 else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
3303 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
3305 std::swap (vno1->op[0], vno1->op[1]);
3306 vno1->opcode = swap_tree_comparison (vno1->opcode);
3309 hstate.add_int (vno1->opcode);
3310 for (i = 0; i < vno1->length; ++i)
3311 inchash::add_expr (vno1->op[i], hstate);
3313 return hstate.end ();
3316 /* Compare nary operations VNO1 and VNO2 and return true if they are
3317 equivalent. */
3319 bool
3320 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
3322 unsigned i;
3324 if (vno1->hashcode != vno2->hashcode)
3325 return false;
3327 if (vno1->length != vno2->length)
3328 return false;
3330 if (vno1->opcode != vno2->opcode
3331 || !types_compatible_p (vno1->type, vno2->type))
3332 return false;
3334 for (i = 0; i < vno1->length; ++i)
3335 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
3336 return false;
3338 /* BIT_INSERT_EXPR has an implict operand as the type precision
3339 of op1. Need to check to make sure they are the same. */
3340 if (vno1->opcode == BIT_INSERT_EXPR
3341 && TREE_CODE (vno1->op[1]) == INTEGER_CST
3342 && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
3343 != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
3344 return false;
3346 return true;
3349 /* Initialize VNO from the pieces provided. */
3351 static void
3352 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
3353 enum tree_code code, tree type, tree *ops)
3355 vno->opcode = code;
3356 vno->length = length;
3357 vno->type = type;
3358 memcpy (&vno->op[0], ops, sizeof (tree) * length);
3361 /* Return the number of operands for a vn_nary ops structure from STMT. */
3363 static unsigned int
3364 vn_nary_length_from_stmt (gimple *stmt)
3366 switch (gimple_assign_rhs_code (stmt))
3368 case REALPART_EXPR:
3369 case IMAGPART_EXPR:
3370 case VIEW_CONVERT_EXPR:
3371 return 1;
3373 case BIT_FIELD_REF:
3374 return 3;
3376 case CONSTRUCTOR:
3377 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
3379 default:
3380 return gimple_num_ops (stmt) - 1;
3384 /* Initialize VNO from STMT. */
3386 static void
3387 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple *stmt)
3389 unsigned i;
3391 vno->opcode = gimple_assign_rhs_code (stmt);
3392 vno->type = gimple_expr_type (stmt);
3393 switch (vno->opcode)
3395 case REALPART_EXPR:
3396 case IMAGPART_EXPR:
3397 case VIEW_CONVERT_EXPR:
3398 vno->length = 1;
3399 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
3400 break;
3402 case BIT_FIELD_REF:
3403 vno->length = 3;
3404 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
3405 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
3406 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
3407 break;
3409 case CONSTRUCTOR:
3410 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
3411 for (i = 0; i < vno->length; ++i)
3412 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
3413 break;
3415 default:
3416 gcc_checking_assert (!gimple_assign_single_p (stmt));
3417 vno->length = gimple_num_ops (stmt) - 1;
3418 for (i = 0; i < vno->length; ++i)
3419 vno->op[i] = gimple_op (stmt, i + 1);
3423 /* Compute the hashcode for VNO and look for it in the hash table;
3424 return the resulting value number if it exists in the hash table.
3425 Return NULL_TREE if it does not exist in the hash table or if the
3426 result field of the operation is NULL. VNRESULT will contain the
3427 vn_nary_op_t from the hashtable if it exists. */
3429 static tree
3430 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
3432 vn_nary_op_s **slot;
3434 if (vnresult)
3435 *vnresult = NULL;
3437 vno->hashcode = vn_nary_op_compute_hash (vno);
3438 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
3439 if (!slot)
3440 return NULL_TREE;
3441 if (vnresult)
3442 *vnresult = *slot;
3443 return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
3446 /* Lookup a n-ary operation by its pieces and return the resulting value
3447 number if it exists in the hash table. Return NULL_TREE if it does
3448 not exist in the hash table or if the result field of the operation
3449 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
3450 if it exists. */
3452 tree
3453 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
3454 tree type, tree *ops, vn_nary_op_t *vnresult)
3456 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
3457 sizeof_vn_nary_op (length));
3458 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
3459 return vn_nary_op_lookup_1 (vno1, vnresult);
3462 /* Lookup the rhs of STMT in the current hash table, and return the resulting
3463 value number if it exists in the hash table. Return NULL_TREE if
3464 it does not exist in the hash table. VNRESULT will contain the
3465 vn_nary_op_t from the hashtable if it exists. */
3467 tree
3468 vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
3470 vn_nary_op_t vno1
3471 = XALLOCAVAR (struct vn_nary_op_s,
3472 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
3473 init_vn_nary_op_from_stmt (vno1, stmt);
3474 return vn_nary_op_lookup_1 (vno1, vnresult);
3477 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
3479 static vn_nary_op_t
3480 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
3482 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
3485 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
3486 obstack. */
3488 static vn_nary_op_t
3489 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
3491 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
3493 vno1->value_id = value_id;
3494 vno1->length = length;
3495 vno1->predicated_values = 0;
3496 vno1->u.result = result;
3498 return vno1;
3501 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
3502 VNO->HASHCODE first. */
3504 static vn_nary_op_t
3505 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
3506 bool compute_hash)
3508 vn_nary_op_s **slot;
3510 if (compute_hash)
3512 vno->hashcode = vn_nary_op_compute_hash (vno);
3513 gcc_assert (! vno->predicated_values
3514 || (! vno->u.values->next
3515 && vno->u.values->n == 1));
3518 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
3519 vno->unwind_to = *slot;
3520 if (*slot)
3522 /* Prefer non-predicated values.
3523 ??? Only if those are constant, otherwise, with constant predicated
3524 value, turn them into predicated values with entry-block validity
3525 (??? but we always find the first valid result currently). */
3526 if ((*slot)->predicated_values
3527 && ! vno->predicated_values)
3529 /* ??? We cannot remove *slot from the unwind stack list.
3530 For the moment we deal with this by skipping not found
3531 entries but this isn't ideal ... */
3532 *slot = vno;
3533 /* ??? Maintain a stack of states we can unwind in
3534 vn_nary_op_s? But how far do we unwind? In reality
3535 we need to push change records somewhere... Or not
3536 unwind vn_nary_op_s and linking them but instead
3537 unwind the results "list", linking that, which also
3538 doesn't move on hashtable resize. */
3539 /* We can also have a ->unwind_to recording *slot there.
3540 That way we can make u.values a fixed size array with
3541 recording the number of entries but of course we then
3542 have always N copies for each unwind_to-state. Or we
3543 make sure to only ever append and each unwinding will
3544 pop off one entry (but how to deal with predicated
3545 replaced with non-predicated here?) */
3546 vno->next = last_inserted_nary;
3547 last_inserted_nary = vno;
3548 return vno;
3550 else if (vno->predicated_values
3551 && ! (*slot)->predicated_values)
3552 return *slot;
3553 else if (vno->predicated_values
3554 && (*slot)->predicated_values)
3556 /* ??? Factor this all into a insert_single_predicated_value
3557 routine. */
3558 gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
3559 basic_block vno_bb
3560 = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
3561 vn_pval *nval = vno->u.values;
3562 vn_pval **next = &vno->u.values;
3563 bool found = false;
3564 for (vn_pval *val = (*slot)->u.values; val; val = val->next)
3566 if (expressions_equal_p (val->result, vno->u.values->result))
3568 found = true;
3569 for (unsigned i = 0; i < val->n; ++i)
3571 basic_block val_bb
3572 = BASIC_BLOCK_FOR_FN (cfun,
3573 val->valid_dominated_by_p[i]);
3574 if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
3575 /* Value registered with more generic predicate. */
3576 return *slot;
3577 else if (dominated_by_p (CDI_DOMINATORS, val_bb, vno_bb))
3578 /* Shouldn't happen, we insert in RPO order. */
3579 gcc_unreachable ();
3581 /* Append value. */
3582 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
3583 sizeof (vn_pval)
3584 + val->n * sizeof (int));
3585 (*next)->next = NULL;
3586 (*next)->result = val->result;
3587 (*next)->n = val->n + 1;
3588 memcpy ((*next)->valid_dominated_by_p,
3589 val->valid_dominated_by_p,
3590 val->n * sizeof (int));
3591 (*next)->valid_dominated_by_p[val->n] = vno_bb->index;
3592 next = &(*next)->next;
3593 if (dump_file && (dump_flags & TDF_DETAILS))
3594 fprintf (dump_file, "Appending predicate to value.\n");
3595 continue;
3597 /* Copy other predicated values. */
3598 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
3599 sizeof (vn_pval)
3600 + (val->n-1) * sizeof (int));
3601 memcpy (*next, val, sizeof (vn_pval) + (val->n-1) * sizeof (int));
3602 (*next)->next = NULL;
3603 next = &(*next)->next;
3605 if (!found)
3606 *next = nval;
3608 *slot = vno;
3609 vno->next = last_inserted_nary;
3610 last_inserted_nary = vno;
3611 return vno;
3614 /* While we do not want to insert things twice it's awkward to
3615 avoid it in the case where visit_nary_op pattern-matches stuff
3616 and ends up simplifying the replacement to itself. We then
3617 get two inserts, one from visit_nary_op and one from
3618 vn_nary_build_or_lookup.
3619 So allow inserts with the same value number. */
3620 if ((*slot)->u.result == vno->u.result)
3621 return *slot;
3624 /* ??? There's also optimistic vs. previous commited state merging
3625 that is problematic for the case of unwinding. */
3627 /* ??? We should return NULL if we do not use 'vno' and have the
3628 caller release it. */
3629 gcc_assert (!*slot);
3631 *slot = vno;
3632 vno->next = last_inserted_nary;
3633 last_inserted_nary = vno;
3634 return vno;
3637 /* Insert a n-ary operation into the current hash table using it's
3638 pieces. Return the vn_nary_op_t structure we created and put in
3639 the hashtable. */
3641 vn_nary_op_t
3642 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
3643 tree type, tree *ops,
3644 tree result, unsigned int value_id)
3646 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
3647 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
3648 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
3651 static vn_nary_op_t
3652 vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
3653 tree type, tree *ops,
3654 tree result, unsigned int value_id,
3655 edge pred_e)
3657 /* ??? Currently tracking BBs. */
3658 if (! single_pred_p (pred_e->dest))
3660 /* Never record for backedges. */
3661 if (pred_e->flags & EDGE_DFS_BACK)
3662 return NULL;
3663 edge_iterator ei;
3664 edge e;
3665 int cnt = 0;
3666 /* Ignore backedges. */
3667 FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
3668 if (! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
3669 cnt++;
3670 if (cnt != 1)
3671 return NULL;
3673 if (dump_file && (dump_flags & TDF_DETAILS)
3674 /* ??? Fix dumping, but currently we only get comparisons. */
3675 && TREE_CODE_CLASS (code) == tcc_comparison)
3677 fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
3678 pred_e->dest->index);
3679 print_generic_expr (dump_file, ops[0], TDF_SLIM);
3680 fprintf (dump_file, " %s ", get_tree_code_name (code));
3681 print_generic_expr (dump_file, ops[1], TDF_SLIM);
3682 fprintf (dump_file, " == %s\n",
3683 integer_zerop (result) ? "false" : "true");
3685 vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
3686 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
3687 vno1->predicated_values = 1;
3688 vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
3689 sizeof (vn_pval));
3690 vno1->u.values->next = NULL;
3691 vno1->u.values->result = result;
3692 vno1->u.values->n = 1;
3693 vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
3694 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
3697 static bool
3698 dominated_by_p_w_unex (basic_block bb1, basic_block bb2);
3700 static tree
3701 vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb)
3703 if (! vno->predicated_values)
3704 return vno->u.result;
3705 for (vn_pval *val = vno->u.values; val; val = val->next)
3706 for (unsigned i = 0; i < val->n; ++i)
3707 if (dominated_by_p_w_unex (bb,
3708 BASIC_BLOCK_FOR_FN
3709 (cfun, val->valid_dominated_by_p[i])))
3710 return val->result;
3711 return NULL_TREE;
3714 /* Insert the rhs of STMT into the current hash table with a value number of
3715 RESULT. */
3717 static vn_nary_op_t
3718 vn_nary_op_insert_stmt (gimple *stmt, tree result)
3720 vn_nary_op_t vno1
3721 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
3722 result, VN_INFO (result)->value_id);
3723 init_vn_nary_op_from_stmt (vno1, stmt);
3724 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
3727 /* Compute a hashcode for PHI operation VP1 and return it. */
3729 static inline hashval_t
3730 vn_phi_compute_hash (vn_phi_t vp1)
3732 inchash::hash hstate (EDGE_COUNT (vp1->block->preds) > 2
3733 ? vp1->block->index : EDGE_COUNT (vp1->block->preds));
3734 tree phi1op;
3735 tree type;
3736 edge e;
3737 edge_iterator ei;
3739 /* If all PHI arguments are constants we need to distinguish
3740 the PHI node via its type. */
3741 type = vp1->type;
3742 hstate.merge_hash (vn_hash_type (type));
3744 FOR_EACH_EDGE (e, ei, vp1->block->preds)
3746 /* Don't hash backedge values they need to be handled as VN_TOP
3747 for optimistic value-numbering. */
3748 if (e->flags & EDGE_DFS_BACK)
3749 continue;
3751 phi1op = vp1->phiargs[e->dest_idx];
3752 if (phi1op == VN_TOP)
3753 continue;
3754 inchash::add_expr (phi1op, hstate);
3757 return hstate.end ();
3761 /* Return true if COND1 and COND2 represent the same condition, set
3762 *INVERTED_P if one needs to be inverted to make it the same as
3763 the other. */
3765 static bool
3766 cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
3767 gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
3769 enum tree_code code1 = gimple_cond_code (cond1);
3770 enum tree_code code2 = gimple_cond_code (cond2);
3772 *inverted_p = false;
3773 if (code1 == code2)
3775 else if (code1 == swap_tree_comparison (code2))
3776 std::swap (lhs2, rhs2);
3777 else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
3778 *inverted_p = true;
3779 else if (code1 == invert_tree_comparison
3780 (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
3782 std::swap (lhs2, rhs2);
3783 *inverted_p = true;
3785 else
3786 return false;
3788 return ((expressions_equal_p (lhs1, lhs2)
3789 && expressions_equal_p (rhs1, rhs2))
3790 || (commutative_tree_code (code1)
3791 && expressions_equal_p (lhs1, rhs2)
3792 && expressions_equal_p (rhs1, lhs2)));
3795 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
3797 static int
3798 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
3800 if (vp1->hashcode != vp2->hashcode)
3801 return false;
3803 if (vp1->block != vp2->block)
3805 if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
3806 return false;
3808 switch (EDGE_COUNT (vp1->block->preds))
3810 case 1:
3811 /* Single-arg PHIs are just copies. */
3812 break;
3814 case 2:
3816 /* Rule out backedges into the PHI. */
3817 if (vp1->block->loop_father->header == vp1->block
3818 || vp2->block->loop_father->header == vp2->block)
3819 return false;
3821 /* If the PHI nodes do not have compatible types
3822 they are not the same. */
3823 if (!types_compatible_p (vp1->type, vp2->type))
3824 return false;
3826 basic_block idom1
3827 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
3828 basic_block idom2
3829 = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
3830 /* If the immediate dominator end in switch stmts multiple
3831 values may end up in the same PHI arg via intermediate
3832 CFG merges. */
3833 if (EDGE_COUNT (idom1->succs) != 2
3834 || EDGE_COUNT (idom2->succs) != 2)
3835 return false;
3837 /* Verify the controlling stmt is the same. */
3838 gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1));
3839 gcond *last2 = safe_dyn_cast <gcond *> (last_stmt (idom2));
3840 if (! last1 || ! last2)
3841 return false;
3842 bool inverted_p;
3843 if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
3844 last2, vp2->cclhs, vp2->ccrhs,
3845 &inverted_p))
3846 return false;
3848 /* Get at true/false controlled edges into the PHI. */
3849 edge te1, te2, fe1, fe2;
3850 if (! extract_true_false_controlled_edges (idom1, vp1->block,
3851 &te1, &fe1)
3852 || ! extract_true_false_controlled_edges (idom2, vp2->block,
3853 &te2, &fe2))
3854 return false;
3856 /* Swap edges if the second condition is the inverted of the
3857 first. */
3858 if (inverted_p)
3859 std::swap (te2, fe2);
3861 /* ??? Handle VN_TOP specially. */
3862 if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
3863 vp2->phiargs[te2->dest_idx])
3864 || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
3865 vp2->phiargs[fe2->dest_idx]))
3866 return false;
3868 return true;
3871 default:
3872 return false;
3876 /* If the PHI nodes do not have compatible types
3877 they are not the same. */
3878 if (!types_compatible_p (vp1->type, vp2->type))
3879 return false;
3881 /* Any phi in the same block will have it's arguments in the
3882 same edge order, because of how we store phi nodes. */
3883 for (unsigned i = 0; i < EDGE_COUNT (vp1->block->preds); ++i)
3885 tree phi1op = vp1->phiargs[i];
3886 tree phi2op = vp2->phiargs[i];
3887 if (phi1op == VN_TOP || phi2op == VN_TOP)
3888 continue;
3889 if (!expressions_equal_p (phi1op, phi2op))
3890 return false;
3893 return true;
3896 /* Lookup PHI in the current hash table, and return the resulting
3897 value number if it exists in the hash table. Return NULL_TREE if
3898 it does not exist in the hash table. */
3900 static tree
3901 vn_phi_lookup (gimple *phi, bool backedges_varying_p)
3903 vn_phi_s **slot;
3904 struct vn_phi_s *vp1;
3905 edge e;
3906 edge_iterator ei;
3908 vp1 = XALLOCAVAR (struct vn_phi_s,
3909 sizeof (struct vn_phi_s)
3910 + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
3912 /* Canonicalize the SSA_NAME's to their value number. */
3913 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3915 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3916 if (TREE_CODE (def) == SSA_NAME
3917 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
3918 def = SSA_VAL (def);
3919 vp1->phiargs[e->dest_idx] = def;
3921 vp1->type = TREE_TYPE (gimple_phi_result (phi));
3922 vp1->block = gimple_bb (phi);
3923 /* Extract values of the controlling condition. */
3924 vp1->cclhs = NULL_TREE;
3925 vp1->ccrhs = NULL_TREE;
3926 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
3927 if (EDGE_COUNT (idom1->succs) == 2)
3928 if (gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1)))
3930 /* ??? We want to use SSA_VAL here. But possibly not
3931 allow VN_TOP. */
3932 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
3933 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
3935 vp1->hashcode = vn_phi_compute_hash (vp1);
3936 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
3937 if (!slot)
3938 return NULL_TREE;
3939 return (*slot)->result;
3942 /* Insert PHI into the current hash table with a value number of
3943 RESULT. */
3945 static vn_phi_t
3946 vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
3948 vn_phi_s **slot;
3949 vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
3950 sizeof (vn_phi_s)
3951 + ((gimple_phi_num_args (phi) - 1)
3952 * sizeof (tree)));
3953 edge e;
3954 edge_iterator ei;
3956 /* Canonicalize the SSA_NAME's to their value number. */
3957 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3959 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3960 if (TREE_CODE (def) == SSA_NAME
3961 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
3962 def = SSA_VAL (def);
3963 vp1->phiargs[e->dest_idx] = def;
3965 vp1->value_id = VN_INFO (result)->value_id;
3966 vp1->type = TREE_TYPE (gimple_phi_result (phi));
3967 vp1->block = gimple_bb (phi);
3968 /* Extract values of the controlling condition. */
3969 vp1->cclhs = NULL_TREE;
3970 vp1->ccrhs = NULL_TREE;
3971 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
3972 if (EDGE_COUNT (idom1->succs) == 2)
3973 if (gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1)))
3975 /* ??? We want to use SSA_VAL here. But possibly not
3976 allow VN_TOP. */
3977 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
3978 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
3980 vp1->result = result;
3981 vp1->hashcode = vn_phi_compute_hash (vp1);
3983 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
3984 gcc_assert (!*slot);
3986 *slot = vp1;
3987 vp1->next = last_inserted_phi;
3988 last_inserted_phi = vp1;
3989 return vp1;
3993 /* Return true if BB1 is dominated by BB2 taking into account edges
3994 that are not executable. */
3996 static bool
3997 dominated_by_p_w_unex (basic_block bb1, basic_block bb2)
3999 edge_iterator ei;
4000 edge e;
4002 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
4003 return true;
4005 /* Before iterating we'd like to know if there exists a
4006 (executable) path from bb2 to bb1 at all, if not we can
4007 directly return false. For now simply iterate once. */
4009 /* Iterate to the single executable bb1 predecessor. */
4010 if (EDGE_COUNT (bb1->preds) > 1)
4012 edge prede = NULL;
4013 FOR_EACH_EDGE (e, ei, bb1->preds)
4014 if (e->flags & EDGE_EXECUTABLE)
4016 if (prede)
4018 prede = NULL;
4019 break;
4021 prede = e;
4023 if (prede)
4025 bb1 = prede->src;
4027 /* Re-do the dominance check with changed bb1. */
4028 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
4029 return true;
4033 /* Iterate to the single executable bb2 successor. */
4034 edge succe = NULL;
4035 FOR_EACH_EDGE (e, ei, bb2->succs)
4036 if (e->flags & EDGE_EXECUTABLE)
4038 if (succe)
4040 succe = NULL;
4041 break;
4043 succe = e;
4045 if (succe)
4047 /* Verify the reached block is only reached through succe.
4048 If there is only one edge we can spare us the dominator
4049 check and iterate directly. */
4050 if (EDGE_COUNT (succe->dest->preds) > 1)
4052 FOR_EACH_EDGE (e, ei, succe->dest->preds)
4053 if (e != succe
4054 && (e->flags & EDGE_EXECUTABLE))
4056 succe = NULL;
4057 break;
4060 if (succe)
4062 bb2 = succe->dest;
4064 /* Re-do the dominance check with changed bb2. */
4065 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
4066 return true;
4070 /* We could now iterate updating bb1 / bb2. */
4071 return false;
4074 /* Set the value number of FROM to TO, return true if it has changed
4075 as a result. */
4077 static inline bool
4078 set_ssa_val_to (tree from, tree to)
4080 vn_ssa_aux_t from_info = VN_INFO (from);
4081 tree currval = from_info->valnum; // SSA_VAL (from)
4082 poly_int64 toff, coff;
4084 /* The only thing we allow as value numbers are ssa_names
4085 and invariants. So assert that here. We don't allow VN_TOP
4086 as visiting a stmt should produce a value-number other than
4087 that.
4088 ??? Still VN_TOP can happen for unreachable code, so force
4089 it to varying in that case. Not all code is prepared to
4090 get VN_TOP on valueization. */
4091 if (to == VN_TOP)
4093 /* ??? When iterating and visiting PHI <undef, backedge-value>
4094 for the first time we rightfully get VN_TOP and we need to
4095 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
4096 With SCCVN we were simply lucky we iterated the other PHI
4097 cycles first and thus visited the backedge-value DEF. */
4098 if (currval == VN_TOP)
4099 goto set_and_exit;
4100 if (dump_file && (dump_flags & TDF_DETAILS))
4101 fprintf (dump_file, "Forcing value number to varying on "
4102 "receiving VN_TOP\n");
4103 to = from;
4106 gcc_checking_assert (to != NULL_TREE
4107 && ((TREE_CODE (to) == SSA_NAME
4108 && (to == from || SSA_VAL (to) == to))
4109 || is_gimple_min_invariant (to)));
4111 if (from != to)
4113 if (currval == from)
4115 if (dump_file && (dump_flags & TDF_DETAILS))
4117 fprintf (dump_file, "Not changing value number of ");
4118 print_generic_expr (dump_file, from);
4119 fprintf (dump_file, " from VARYING to ");
4120 print_generic_expr (dump_file, to);
4121 fprintf (dump_file, "\n");
4123 return false;
4125 bool curr_invariant = is_gimple_min_invariant (currval);
4126 bool curr_undefined = (TREE_CODE (currval) == SSA_NAME
4127 && ssa_undefined_value_p (currval, false));
4128 if (currval != VN_TOP
4129 && !curr_invariant
4130 && !curr_undefined
4131 && is_gimple_min_invariant (to))
4133 if (dump_file && (dump_flags & TDF_DETAILS))
4135 fprintf (dump_file, "Forcing VARYING instead of changing "
4136 "value number of ");
4137 print_generic_expr (dump_file, from);
4138 fprintf (dump_file, " from ");
4139 print_generic_expr (dump_file, currval);
4140 fprintf (dump_file, " (non-constant) to ");
4141 print_generic_expr (dump_file, to);
4142 fprintf (dump_file, " (constant)\n");
4144 to = from;
4146 else if (currval != VN_TOP
4147 && !curr_undefined
4148 && TREE_CODE (to) == SSA_NAME
4149 && ssa_undefined_value_p (to, false))
4151 if (dump_file && (dump_flags & TDF_DETAILS))
4153 fprintf (dump_file, "Forcing VARYING instead of changing "
4154 "value number of ");
4155 print_generic_expr (dump_file, from);
4156 fprintf (dump_file, " from ");
4157 print_generic_expr (dump_file, currval);
4158 fprintf (dump_file, " (non-undefined) to ");
4159 print_generic_expr (dump_file, to);
4160 fprintf (dump_file, " (undefined)\n");
4162 to = from;
4164 else if (TREE_CODE (to) == SSA_NAME
4165 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
4166 to = from;
4169 set_and_exit:
4170 if (dump_file && (dump_flags & TDF_DETAILS))
4172 fprintf (dump_file, "Setting value number of ");
4173 print_generic_expr (dump_file, from);
4174 fprintf (dump_file, " to ");
4175 print_generic_expr (dump_file, to);
4178 if (currval != to
4179 && !operand_equal_p (currval, to, 0)
4180 /* Different undefined SSA names are not actually different. See
4181 PR82320 for a testcase were we'd otherwise not terminate iteration. */
4182 && !(TREE_CODE (currval) == SSA_NAME
4183 && TREE_CODE (to) == SSA_NAME
4184 && ssa_undefined_value_p (currval, false)
4185 && ssa_undefined_value_p (to, false))
4186 /* ??? For addresses involving volatile objects or types operand_equal_p
4187 does not reliably detect ADDR_EXPRs as equal. We know we are only
4188 getting invariant gimple addresses here, so can use
4189 get_addr_base_and_unit_offset to do this comparison. */
4190 && !(TREE_CODE (currval) == ADDR_EXPR
4191 && TREE_CODE (to) == ADDR_EXPR
4192 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
4193 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
4194 && known_eq (coff, toff)))
4196 if (dump_file && (dump_flags & TDF_DETAILS))
4197 fprintf (dump_file, " (changed)\n");
4198 from_info->valnum = to;
4199 return true;
4201 if (dump_file && (dump_flags & TDF_DETAILS))
4202 fprintf (dump_file, "\n");
4203 return false;
4206 /* Set all definitions in STMT to value number to themselves.
4207 Return true if a value number changed. */
4209 static bool
4210 defs_to_varying (gimple *stmt)
4212 bool changed = false;
4213 ssa_op_iter iter;
4214 def_operand_p defp;
4216 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
4218 tree def = DEF_FROM_PTR (defp);
4219 changed |= set_ssa_val_to (def, def);
4221 return changed;
4224 /* Visit a copy between LHS and RHS, return true if the value number
4225 changed. */
4227 static bool
4228 visit_copy (tree lhs, tree rhs)
4230 /* Valueize. */
4231 rhs = SSA_VAL (rhs);
4233 return set_ssa_val_to (lhs, rhs);
4236 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
4237 is the same. */
4239 static tree
4240 valueized_wider_op (tree wide_type, tree op)
4242 if (TREE_CODE (op) == SSA_NAME)
4243 op = vn_valueize (op);
4245 /* Either we have the op widened available. */
4246 tree ops[3] = {};
4247 ops[0] = op;
4248 tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
4249 wide_type, ops, NULL);
4250 if (tem)
4251 return tem;
4253 /* Or the op is truncated from some existing value. */
4254 if (TREE_CODE (op) == SSA_NAME)
4256 gimple *def = SSA_NAME_DEF_STMT (op);
4257 if (is_gimple_assign (def)
4258 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
4260 tem = gimple_assign_rhs1 (def);
4261 if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
4263 if (TREE_CODE (tem) == SSA_NAME)
4264 tem = vn_valueize (tem);
4265 return tem;
4270 /* For constants simply extend it. */
4271 if (TREE_CODE (op) == INTEGER_CST)
4272 return wide_int_to_tree (wide_type, wi::to_wide (op));
4274 return NULL_TREE;
4277 /* Visit a nary operator RHS, value number it, and return true if the
4278 value number of LHS has changed as a result. */
4280 static bool
4281 visit_nary_op (tree lhs, gassign *stmt)
4283 vn_nary_op_t vnresult;
4284 tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
4285 if (! result && vnresult)
4286 result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
4287 if (result)
4288 return set_ssa_val_to (lhs, result);
4290 /* Do some special pattern matching for redundancies of operations
4291 in different types. */
4292 enum tree_code code = gimple_assign_rhs_code (stmt);
4293 tree type = TREE_TYPE (lhs);
4294 tree rhs1 = gimple_assign_rhs1 (stmt);
4295 switch (code)
4297 CASE_CONVERT:
4298 /* Match arithmetic done in a different type where we can easily
4299 substitute the result from some earlier sign-changed or widened
4300 operation. */
4301 if (INTEGRAL_TYPE_P (type)
4302 && TREE_CODE (rhs1) == SSA_NAME
4303 /* We only handle sign-changes, zero-extension -> & mask or
4304 sign-extension if we know the inner operation doesn't
4305 overflow. */
4306 && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
4307 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4308 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
4309 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
4310 || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
4312 gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
4313 if (def
4314 && (gimple_assign_rhs_code (def) == PLUS_EXPR
4315 || gimple_assign_rhs_code (def) == MINUS_EXPR
4316 || gimple_assign_rhs_code (def) == MULT_EXPR))
4318 tree ops[3] = {};
4319 /* Either we have the op widened available. */
4320 ops[0] = valueized_wider_op (type,
4321 gimple_assign_rhs1 (def));
4322 if (ops[0])
4323 ops[1] = valueized_wider_op (type,
4324 gimple_assign_rhs2 (def));
4325 if (ops[0] && ops[1])
4327 ops[0] = vn_nary_op_lookup_pieces
4328 (2, gimple_assign_rhs_code (def), type, ops, NULL);
4329 /* We have wider operation available. */
4330 if (ops[0]
4331 /* If the leader is a wrapping operation we can
4332 insert it for code hoisting w/o introducing
4333 undefined overflow. If it is not it has to
4334 be available. See PR86554. */
4335 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
4336 || (rpo_avail && vn_context_bb
4337 && rpo_avail->eliminate_avail (vn_context_bb,
4338 ops[0]))))
4340 unsigned lhs_prec = TYPE_PRECISION (type);
4341 unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
4342 if (lhs_prec == rhs_prec
4343 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4344 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
4346 gimple_match_op match_op (gimple_match_cond::UNCOND,
4347 NOP_EXPR, type, ops[0]);
4348 result = vn_nary_build_or_lookup (&match_op);
4349 if (result)
4351 bool changed = set_ssa_val_to (lhs, result);
4352 vn_nary_op_insert_stmt (stmt, result);
4353 return changed;
4356 else
4358 tree mask = wide_int_to_tree
4359 (type, wi::mask (rhs_prec, false, lhs_prec));
4360 gimple_match_op match_op (gimple_match_cond::UNCOND,
4361 BIT_AND_EXPR,
4362 TREE_TYPE (lhs),
4363 ops[0], mask);
4364 result = vn_nary_build_or_lookup (&match_op);
4365 if (result)
4367 bool changed = set_ssa_val_to (lhs, result);
4368 vn_nary_op_insert_stmt (stmt, result);
4369 return changed;
4376 default:;
4379 bool changed = set_ssa_val_to (lhs, lhs);
4380 vn_nary_op_insert_stmt (stmt, lhs);
4381 return changed;
4384 /* Visit a call STMT storing into LHS. Return true if the value number
4385 of the LHS has changed as a result. */
4387 static bool
4388 visit_reference_op_call (tree lhs, gcall *stmt)
4390 bool changed = false;
4391 struct vn_reference_s vr1;
4392 vn_reference_t vnresult = NULL;
4393 tree vdef = gimple_vdef (stmt);
4395 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
4396 if (lhs && TREE_CODE (lhs) != SSA_NAME)
4397 lhs = NULL_TREE;
4399 vn_reference_lookup_call (stmt, &vnresult, &vr1);
4400 if (vnresult)
4402 if (vnresult->result_vdef && vdef)
4403 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
4404 else if (vdef)
4405 /* If the call was discovered to be pure or const reflect
4406 that as far as possible. */
4407 changed |= set_ssa_val_to (vdef, vuse_ssa_val (gimple_vuse (stmt)));
4409 if (!vnresult->result && lhs)
4410 vnresult->result = lhs;
4412 if (vnresult->result && lhs)
4413 changed |= set_ssa_val_to (lhs, vnresult->result);
4415 else
4417 vn_reference_t vr2;
4418 vn_reference_s **slot;
4419 tree vdef_val = vdef;
4420 if (vdef)
4422 /* If we value numbered an indirect functions function to
4423 one not clobbering memory value number its VDEF to its
4424 VUSE. */
4425 tree fn = gimple_call_fn (stmt);
4426 if (fn && TREE_CODE (fn) == SSA_NAME)
4428 fn = SSA_VAL (fn);
4429 if (TREE_CODE (fn) == ADDR_EXPR
4430 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
4431 && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
4432 & (ECF_CONST | ECF_PURE)))
4433 vdef_val = vuse_ssa_val (gimple_vuse (stmt));
4435 changed |= set_ssa_val_to (vdef, vdef_val);
4437 if (lhs)
4438 changed |= set_ssa_val_to (lhs, lhs);
4439 vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
4440 vr2->vuse = vr1.vuse;
4441 /* As we are not walking the virtual operand chain we know the
4442 shared_lookup_references are still original so we can re-use
4443 them here. */
4444 vr2->operands = vr1.operands.copy ();
4445 vr2->type = vr1.type;
4446 vr2->set = vr1.set;
4447 vr2->hashcode = vr1.hashcode;
4448 vr2->result = lhs;
4449 vr2->result_vdef = vdef_val;
4450 vr2->value_id = 0;
4451 slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
4452 INSERT);
4453 gcc_assert (!*slot);
4454 *slot = vr2;
4455 vr2->next = last_inserted_ref;
4456 last_inserted_ref = vr2;
4459 return changed;
4462 /* Visit a load from a reference operator RHS, part of STMT, value number it,
4463 and return true if the value number of the LHS has changed as a result. */
4465 static bool
4466 visit_reference_op_load (tree lhs, tree op, gimple *stmt)
4468 bool changed = false;
4469 tree last_vuse;
4470 tree result;
4472 last_vuse = gimple_vuse (stmt);
4473 result = vn_reference_lookup (op, gimple_vuse (stmt),
4474 default_vn_walk_kind, NULL, true, &last_vuse);
4476 /* We handle type-punning through unions by value-numbering based
4477 on offset and size of the access. Be prepared to handle a
4478 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
4479 if (result
4480 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
4482 /* We will be setting the value number of lhs to the value number
4483 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
4484 So first simplify and lookup this expression to see if it
4485 is already available. */
4486 gimple_match_op res_op (gimple_match_cond::UNCOND,
4487 VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
4488 result = vn_nary_build_or_lookup (&res_op);
4489 /* When building the conversion fails avoid inserting the reference
4490 again. */
4491 if (!result)
4492 return set_ssa_val_to (lhs, lhs);
4495 if (result)
4496 changed = set_ssa_val_to (lhs, result);
4497 else
4499 changed = set_ssa_val_to (lhs, lhs);
4500 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
4503 return changed;
4507 /* Visit a store to a reference operator LHS, part of STMT, value number it,
4508 and return true if the value number of the LHS has changed as a result. */
4510 static bool
4511 visit_reference_op_store (tree lhs, tree op, gimple *stmt)
4513 bool changed = false;
4514 vn_reference_t vnresult = NULL;
4515 tree assign;
4516 bool resultsame = false;
4517 tree vuse = gimple_vuse (stmt);
4518 tree vdef = gimple_vdef (stmt);
4520 if (TREE_CODE (op) == SSA_NAME)
4521 op = SSA_VAL (op);
4523 /* First we want to lookup using the *vuses* from the store and see
4524 if there the last store to this location with the same address
4525 had the same value.
4527 The vuses represent the memory state before the store. If the
4528 memory state, address, and value of the store is the same as the
4529 last store to this location, then this store will produce the
4530 same memory state as that store.
4532 In this case the vdef versions for this store are value numbered to those
4533 vuse versions, since they represent the same memory state after
4534 this store.
4536 Otherwise, the vdefs for the store are used when inserting into
4537 the table, since the store generates a new memory state. */
4539 vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
4540 if (vnresult
4541 && vnresult->result)
4543 tree result = vnresult->result;
4544 gcc_checking_assert (TREE_CODE (result) != SSA_NAME
4545 || result == SSA_VAL (result));
4546 resultsame = expressions_equal_p (result, op);
4547 if (resultsame)
4549 /* If the TBAA state isn't compatible for downstream reads
4550 we cannot value-number the VDEFs the same. */
4551 alias_set_type set = get_alias_set (lhs);
4552 if (vnresult->set != set
4553 && ! alias_set_subset_of (set, vnresult->set))
4554 resultsame = false;
4558 if (!resultsame)
4560 /* Only perform the following when being called from PRE
4561 which embeds tail merging. */
4562 if (default_vn_walk_kind == VN_WALK)
4564 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
4565 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
4566 if (vnresult)
4568 VN_INFO (vdef)->visited = true;
4569 return set_ssa_val_to (vdef, vnresult->result_vdef);
4573 if (dump_file && (dump_flags & TDF_DETAILS))
4575 fprintf (dump_file, "No store match\n");
4576 fprintf (dump_file, "Value numbering store ");
4577 print_generic_expr (dump_file, lhs);
4578 fprintf (dump_file, " to ");
4579 print_generic_expr (dump_file, op);
4580 fprintf (dump_file, "\n");
4582 /* Have to set value numbers before insert, since insert is
4583 going to valueize the references in-place. */
4584 if (vdef)
4585 changed |= set_ssa_val_to (vdef, vdef);
4587 /* Do not insert structure copies into the tables. */
4588 if (is_gimple_min_invariant (op)
4589 || is_gimple_reg (op))
4590 vn_reference_insert (lhs, op, vdef, NULL);
4592 /* Only perform the following when being called from PRE
4593 which embeds tail merging. */
4594 if (default_vn_walk_kind == VN_WALK)
4596 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
4597 vn_reference_insert (assign, lhs, vuse, vdef);
4600 else
4602 /* We had a match, so value number the vdef to have the value
4603 number of the vuse it came from. */
4605 if (dump_file && (dump_flags & TDF_DETAILS))
4606 fprintf (dump_file, "Store matched earlier value, "
4607 "value numbering store vdefs to matching vuses.\n");
4609 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
4612 return changed;
4615 /* Visit and value number PHI, return true if the value number
4616 changed. When BACKEDGES_VARYING_P is true then assume all
4617 backedge values are varying. When INSERTED is not NULL then
4618 this is just a ahead query for a possible iteration, set INSERTED
4619 to true if we'd insert into the hashtable. */
4621 static bool
4622 visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
4624 tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
4625 tree backedge_val = NULL_TREE;
4626 bool seen_non_backedge = false;
4627 tree sameval_base = NULL_TREE;
4628 poly_int64 soff, doff;
4629 unsigned n_executable = 0;
4630 edge_iterator ei;
4631 edge e;
4633 /* TODO: We could check for this in initialization, and replace this
4634 with a gcc_assert. */
4635 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
4636 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
4638 /* We track whether a PHI was CSEd to to avoid excessive iterations
4639 that would be necessary only because the PHI changed arguments
4640 but not value. */
4641 if (!inserted)
4642 gimple_set_plf (phi, GF_PLF_1, false);
4644 /* See if all non-TOP arguments have the same value. TOP is
4645 equivalent to everything, so we can ignore it. */
4646 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
4647 if (e->flags & EDGE_EXECUTABLE)
4649 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4651 ++n_executable;
4652 if (TREE_CODE (def) == SSA_NAME)
4654 if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
4655 def = SSA_VAL (def);
4656 if (e->flags & EDGE_DFS_BACK)
4657 backedge_val = def;
4659 if (!(e->flags & EDGE_DFS_BACK))
4660 seen_non_backedge = true;
4661 if (def == VN_TOP)
4663 /* Ignore undefined defs for sameval but record one. */
4664 else if (TREE_CODE (def) == SSA_NAME
4665 && ! virtual_operand_p (def)
4666 && ssa_undefined_value_p (def, false))
4667 seen_undef = def;
4668 else if (sameval == VN_TOP)
4669 sameval = def;
4670 else if (!expressions_equal_p (def, sameval))
4672 /* We know we're arriving only with invariant addresses here,
4673 try harder comparing them. We can do some caching here
4674 which we cannot do in expressions_equal_p. */
4675 if (TREE_CODE (def) == ADDR_EXPR
4676 && TREE_CODE (sameval) == ADDR_EXPR
4677 && sameval_base != (void *)-1)
4679 if (!sameval_base)
4680 sameval_base = get_addr_base_and_unit_offset
4681 (TREE_OPERAND (sameval, 0), &soff);
4682 if (!sameval_base)
4683 sameval_base = (tree)(void *)-1;
4684 else if ((get_addr_base_and_unit_offset
4685 (TREE_OPERAND (def, 0), &doff) == sameval_base)
4686 && known_eq (soff, doff))
4687 continue;
4689 sameval = NULL_TREE;
4690 break;
4694 /* If the value we want to use is flowing over the backedge and we
4695 should take it as VARYING but it has a non-VARYING value drop to
4696 VARYING.
4697 If we value-number a virtual operand never value-number to the
4698 value from the backedge as that confuses the alias-walking code.
4699 See gcc.dg/torture/pr87176.c. If the value is the same on a
4700 non-backedge everything is OK though. */
4701 bool visited_p;
4702 if ((backedge_val
4703 && !seen_non_backedge
4704 && TREE_CODE (backedge_val) == SSA_NAME
4705 && sameval == backedge_val
4706 && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
4707 || SSA_VAL (backedge_val) != backedge_val))
4708 /* Do not value-number a virtual operand to sth not visited though
4709 given that allows us to escape a region in alias walking. */
4710 || (sameval
4711 && TREE_CODE (sameval) == SSA_NAME
4712 && !SSA_NAME_IS_DEFAULT_DEF (sameval)
4713 && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
4714 && (SSA_VAL (sameval, &visited_p), !visited_p)))
4715 /* Note this just drops to VARYING without inserting the PHI into
4716 the hashes. */
4717 result = PHI_RESULT (phi);
4718 /* If none of the edges was executable keep the value-number at VN_TOP,
4719 if only a single edge is exectuable use its value. */
4720 else if (n_executable <= 1)
4721 result = seen_undef ? seen_undef : sameval;
4722 /* If we saw only undefined values and VN_TOP use one of the
4723 undefined values. */
4724 else if (sameval == VN_TOP)
4725 result = seen_undef ? seen_undef : sameval;
4726 /* First see if it is equivalent to a phi node in this block. We prefer
4727 this as it allows IV elimination - see PRs 66502 and 67167. */
4728 else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
4730 if (!inserted
4731 && TREE_CODE (result) == SSA_NAME
4732 && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
4734 gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
4735 if (dump_file && (dump_flags & TDF_DETAILS))
4737 fprintf (dump_file, "Marking CSEd to PHI node ");
4738 print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
4739 0, TDF_SLIM);
4740 fprintf (dump_file, "\n");
4744 /* If all values are the same use that, unless we've seen undefined
4745 values as well and the value isn't constant.
4746 CCP/copyprop have the same restriction to not remove uninit warnings. */
4747 else if (sameval
4748 && (! seen_undef || is_gimple_min_invariant (sameval)))
4749 result = sameval;
4750 else
4752 result = PHI_RESULT (phi);
4753 /* Only insert PHIs that are varying, for constant value numbers
4754 we mess up equivalences otherwise as we are only comparing
4755 the immediate controlling predicates. */
4756 vn_phi_insert (phi, result, backedges_varying_p);
4757 if (inserted)
4758 *inserted = true;
4761 return set_ssa_val_to (PHI_RESULT (phi), result);
4764 /* Try to simplify RHS using equivalences and constant folding. */
4766 static tree
4767 try_to_simplify (gassign *stmt)
4769 enum tree_code code = gimple_assign_rhs_code (stmt);
4770 tree tem;
4772 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
4773 in this case, there is no point in doing extra work. */
4774 if (code == SSA_NAME)
4775 return NULL_TREE;
4777 /* First try constant folding based on our current lattice. */
4778 mprts_hook = vn_lookup_simplify_result;
4779 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
4780 mprts_hook = NULL;
4781 if (tem
4782 && (TREE_CODE (tem) == SSA_NAME
4783 || is_gimple_min_invariant (tem)))
4784 return tem;
4786 return NULL_TREE;
4789 /* Visit and value number STMT, return true if the value number
4790 changed. */
4792 static bool
4793 visit_stmt (gimple *stmt, bool backedges_varying_p = false)
4795 bool changed = false;
4797 if (dump_file && (dump_flags & TDF_DETAILS))
4799 fprintf (dump_file, "Value numbering stmt = ");
4800 print_gimple_stmt (dump_file, stmt, 0);
4803 if (gimple_code (stmt) == GIMPLE_PHI)
4804 changed = visit_phi (stmt, NULL, backedges_varying_p);
4805 else if (gimple_has_volatile_ops (stmt))
4806 changed = defs_to_varying (stmt);
4807 else if (gassign *ass = dyn_cast <gassign *> (stmt))
4809 enum tree_code code = gimple_assign_rhs_code (ass);
4810 tree lhs = gimple_assign_lhs (ass);
4811 tree rhs1 = gimple_assign_rhs1 (ass);
4812 tree simplified;
4814 /* Shortcut for copies. Simplifying copies is pointless,
4815 since we copy the expression and value they represent. */
4816 if (code == SSA_NAME
4817 && TREE_CODE (lhs) == SSA_NAME)
4819 changed = visit_copy (lhs, rhs1);
4820 goto done;
4822 simplified = try_to_simplify (ass);
4823 if (simplified)
4825 if (dump_file && (dump_flags & TDF_DETAILS))
4827 fprintf (dump_file, "RHS ");
4828 print_gimple_expr (dump_file, ass, 0);
4829 fprintf (dump_file, " simplified to ");
4830 print_generic_expr (dump_file, simplified);
4831 fprintf (dump_file, "\n");
4834 /* Setting value numbers to constants will occasionally
4835 screw up phi congruence because constants are not
4836 uniquely associated with a single ssa name that can be
4837 looked up. */
4838 if (simplified
4839 && is_gimple_min_invariant (simplified)
4840 && TREE_CODE (lhs) == SSA_NAME)
4842 changed = set_ssa_val_to (lhs, simplified);
4843 goto done;
4845 else if (simplified
4846 && TREE_CODE (simplified) == SSA_NAME
4847 && TREE_CODE (lhs) == SSA_NAME)
4849 changed = visit_copy (lhs, simplified);
4850 goto done;
4853 if ((TREE_CODE (lhs) == SSA_NAME
4854 /* We can substitute SSA_NAMEs that are live over
4855 abnormal edges with their constant value. */
4856 && !(gimple_assign_copy_p (ass)
4857 && is_gimple_min_invariant (rhs1))
4858 && !(simplified
4859 && is_gimple_min_invariant (simplified))
4860 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
4861 /* Stores or copies from SSA_NAMEs that are live over
4862 abnormal edges are a problem. */
4863 || (code == SSA_NAME
4864 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
4865 changed = defs_to_varying (ass);
4866 else if (REFERENCE_CLASS_P (lhs)
4867 || DECL_P (lhs))
4868 changed = visit_reference_op_store (lhs, rhs1, ass);
4869 else if (TREE_CODE (lhs) == SSA_NAME)
4871 if ((gimple_assign_copy_p (ass)
4872 && is_gimple_min_invariant (rhs1))
4873 || (simplified
4874 && is_gimple_min_invariant (simplified)))
4876 if (simplified)
4877 changed = set_ssa_val_to (lhs, simplified);
4878 else
4879 changed = set_ssa_val_to (lhs, rhs1);
4881 else
4883 /* Visit the original statement. */
4884 switch (vn_get_stmt_kind (ass))
4886 case VN_NARY:
4887 changed = visit_nary_op (lhs, ass);
4888 break;
4889 case VN_REFERENCE:
4890 changed = visit_reference_op_load (lhs, rhs1, ass);
4891 break;
4892 default:
4893 changed = defs_to_varying (ass);
4894 break;
4898 else
4899 changed = defs_to_varying (ass);
4901 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
4903 tree lhs = gimple_call_lhs (call_stmt);
4904 if (lhs && TREE_CODE (lhs) == SSA_NAME)
4906 /* Try constant folding based on our current lattice. */
4907 tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
4908 vn_valueize);
4909 if (simplified)
4911 if (dump_file && (dump_flags & TDF_DETAILS))
4913 fprintf (dump_file, "call ");
4914 print_gimple_expr (dump_file, call_stmt, 0);
4915 fprintf (dump_file, " simplified to ");
4916 print_generic_expr (dump_file, simplified);
4917 fprintf (dump_file, "\n");
4920 /* Setting value numbers to constants will occasionally
4921 screw up phi congruence because constants are not
4922 uniquely associated with a single ssa name that can be
4923 looked up. */
4924 if (simplified
4925 && is_gimple_min_invariant (simplified))
4927 changed = set_ssa_val_to (lhs, simplified);
4928 if (gimple_vdef (call_stmt))
4929 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
4930 SSA_VAL (gimple_vuse (call_stmt)));
4931 goto done;
4933 else if (simplified
4934 && TREE_CODE (simplified) == SSA_NAME)
4936 changed = visit_copy (lhs, simplified);
4937 if (gimple_vdef (call_stmt))
4938 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
4939 SSA_VAL (gimple_vuse (call_stmt)));
4940 goto done;
4942 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
4944 changed = defs_to_varying (call_stmt);
4945 goto done;
4949 /* Pick up flags from a devirtualization target. */
4950 tree fn = gimple_call_fn (stmt);
4951 int extra_fnflags = 0;
4952 if (fn && TREE_CODE (fn) == SSA_NAME)
4954 fn = SSA_VAL (fn);
4955 if (TREE_CODE (fn) == ADDR_EXPR
4956 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
4957 extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
4959 if (!gimple_call_internal_p (call_stmt)
4960 && (/* Calls to the same function with the same vuse
4961 and the same operands do not necessarily return the same
4962 value, unless they're pure or const. */
4963 ((gimple_call_flags (call_stmt) | extra_fnflags)
4964 & (ECF_PURE | ECF_CONST))
4965 /* If calls have a vdef, subsequent calls won't have
4966 the same incoming vuse. So, if 2 calls with vdef have the
4967 same vuse, we know they're not subsequent.
4968 We can value number 2 calls to the same function with the
4969 same vuse and the same operands which are not subsequent
4970 the same, because there is no code in the program that can
4971 compare the 2 values... */
4972 || (gimple_vdef (call_stmt)
4973 /* ... unless the call returns a pointer which does
4974 not alias with anything else. In which case the
4975 information that the values are distinct are encoded
4976 in the IL. */
4977 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
4978 /* Only perform the following when being called from PRE
4979 which embeds tail merging. */
4980 && default_vn_walk_kind == VN_WALK)))
4981 changed = visit_reference_op_call (lhs, call_stmt);
4982 else
4983 changed = defs_to_varying (call_stmt);
4985 else
4986 changed = defs_to_varying (stmt);
4987 done:
4988 return changed;
4992 /* Allocate a value number table. */
4994 static void
4995 allocate_vn_table (vn_tables_t table, unsigned size)
4997 table->phis = new vn_phi_table_type (size);
4998 table->nary = new vn_nary_op_table_type (size);
4999 table->references = new vn_reference_table_type (size);
5002 /* Free a value number table. */
5004 static void
5005 free_vn_table (vn_tables_t table)
5007 /* Walk over elements and release vectors. */
5008 vn_reference_iterator_type hir;
5009 vn_reference_t vr;
5010 FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
5011 vr->operands.release ();
5012 delete table->phis;
5013 table->phis = NULL;
5014 delete table->nary;
5015 table->nary = NULL;
5016 delete table->references;
5017 table->references = NULL;
5020 /* Set *ID according to RESULT. */
5022 static void
5023 set_value_id_for_result (tree result, unsigned int *id)
5025 if (result && TREE_CODE (result) == SSA_NAME)
5026 *id = VN_INFO (result)->value_id;
5027 else if (result && is_gimple_min_invariant (result))
5028 *id = get_or_alloc_constant_value_id (result);
5029 else
5030 *id = get_next_value_id ();
5033 /* Set the value ids in the valid hash tables. */
5035 static void
5036 set_hashtable_value_ids (void)
5038 vn_nary_op_iterator_type hin;
5039 vn_phi_iterator_type hip;
5040 vn_reference_iterator_type hir;
5041 vn_nary_op_t vno;
5042 vn_reference_t vr;
5043 vn_phi_t vp;
5045 /* Now set the value ids of the things we had put in the hash
5046 table. */
5048 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
5049 if (! vno->predicated_values)
5050 set_value_id_for_result (vno->u.result, &vno->value_id);
5052 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
5053 set_value_id_for_result (vp->result, &vp->value_id);
5055 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
5056 hir)
5057 set_value_id_for_result (vr->result, &vr->value_id);
5060 /* Return the maximum value id we have ever seen. */
5062 unsigned int
5063 get_max_value_id (void)
5065 return next_value_id;
5068 /* Return the next unique value id. */
5070 unsigned int
5071 get_next_value_id (void)
5073 return next_value_id++;
5077 /* Compare two expressions E1 and E2 and return true if they are equal. */
5079 bool
5080 expressions_equal_p (tree e1, tree e2)
5082 /* The obvious case. */
5083 if (e1 == e2)
5084 return true;
5086 /* If either one is VN_TOP consider them equal. */
5087 if (e1 == VN_TOP || e2 == VN_TOP)
5088 return true;
5090 /* If only one of them is null, they cannot be equal. */
5091 if (!e1 || !e2)
5092 return false;
5094 /* Now perform the actual comparison. */
5095 if (TREE_CODE (e1) == TREE_CODE (e2)
5096 && operand_equal_p (e1, e2, OEP_PURE_SAME))
5097 return true;
5099 return false;
5103 /* Return true if the nary operation NARY may trap. This is a copy
5104 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
5106 bool
5107 vn_nary_may_trap (vn_nary_op_t nary)
5109 tree type;
5110 tree rhs2 = NULL_TREE;
5111 bool honor_nans = false;
5112 bool honor_snans = false;
5113 bool fp_operation = false;
5114 bool honor_trapv = false;
5115 bool handled, ret;
5116 unsigned i;
5118 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
5119 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
5120 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
5122 type = nary->type;
5123 fp_operation = FLOAT_TYPE_P (type);
5124 if (fp_operation)
5126 honor_nans = flag_trapping_math && !flag_finite_math_only;
5127 honor_snans = flag_signaling_nans != 0;
5129 else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
5130 honor_trapv = true;
5132 if (nary->length >= 2)
5133 rhs2 = nary->op[1];
5134 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
5135 honor_trapv, honor_nans, honor_snans,
5136 rhs2, &handled);
5137 if (handled && ret)
5138 return true;
5140 for (i = 0; i < nary->length; ++i)
5141 if (tree_could_trap_p (nary->op[i]))
5142 return true;
5144 return false;
5147 /* Return true if the reference operation REF may trap. */
5149 bool
5150 vn_reference_may_trap (vn_reference_t ref)
5152 switch (ref->operands[0].opcode)
5154 case MODIFY_EXPR:
5155 case CALL_EXPR:
5156 /* We do not handle calls. */
5157 case ADDR_EXPR:
5158 /* And toplevel address computations never trap. */
5159 return false;
5160 default:;
5163 vn_reference_op_t op;
5164 unsigned i;
5165 FOR_EACH_VEC_ELT (ref->operands, i, op)
5167 switch (op->opcode)
5169 case WITH_SIZE_EXPR:
5170 case TARGET_MEM_REF:
5171 /* Always variable. */
5172 return true;
5173 case COMPONENT_REF:
5174 if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
5175 return true;
5176 break;
5177 case ARRAY_RANGE_REF:
5178 case ARRAY_REF:
5179 if (TREE_CODE (op->op0) == SSA_NAME)
5180 return true;
5181 break;
5182 case MEM_REF:
5183 /* Nothing interesting in itself, the base is separate. */
5184 break;
5185 /* The following are the address bases. */
5186 case SSA_NAME:
5187 return true;
5188 case ADDR_EXPR:
5189 if (op->op0)
5190 return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
5191 return false;
5192 default:;
5195 return false;
5198 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
5199 bitmap inserted_exprs_)
5200 : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
5201 el_todo (0), eliminations (0), insertions (0),
5202 inserted_exprs (inserted_exprs_)
5204 need_eh_cleanup = BITMAP_ALLOC (NULL);
5205 need_ab_cleanup = BITMAP_ALLOC (NULL);
5208 eliminate_dom_walker::~eliminate_dom_walker ()
5210 BITMAP_FREE (need_eh_cleanup);
5211 BITMAP_FREE (need_ab_cleanup);
5214 /* Return a leader for OP that is available at the current point of the
5215 eliminate domwalk. */
5217 tree
5218 eliminate_dom_walker::eliminate_avail (basic_block, tree op)
5220 tree valnum = VN_INFO (op)->valnum;
5221 if (TREE_CODE (valnum) == SSA_NAME)
5223 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
5224 return valnum;
5225 if (avail.length () > SSA_NAME_VERSION (valnum))
5226 return avail[SSA_NAME_VERSION (valnum)];
5228 else if (is_gimple_min_invariant (valnum))
5229 return valnum;
5230 return NULL_TREE;
5233 /* At the current point of the eliminate domwalk make OP available. */
5235 void
5236 eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
5238 tree valnum = VN_INFO (op)->valnum;
5239 if (TREE_CODE (valnum) == SSA_NAME)
5241 if (avail.length () <= SSA_NAME_VERSION (valnum))
5242 avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
5243 tree pushop = op;
5244 if (avail[SSA_NAME_VERSION (valnum)])
5245 pushop = avail[SSA_NAME_VERSION (valnum)];
5246 avail_stack.safe_push (pushop);
5247 avail[SSA_NAME_VERSION (valnum)] = op;
5251 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
5252 the leader for the expression if insertion was successful. */
5254 tree
5255 eliminate_dom_walker::eliminate_insert (basic_block bb,
5256 gimple_stmt_iterator *gsi, tree val)
5258 /* We can insert a sequence with a single assignment only. */
5259 gimple_seq stmts = VN_INFO (val)->expr;
5260 if (!gimple_seq_singleton_p (stmts))
5261 return NULL_TREE;
5262 gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
5263 if (!stmt
5264 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
5265 && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
5266 && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
5267 && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
5268 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
5269 return NULL_TREE;
5271 tree op = gimple_assign_rhs1 (stmt);
5272 if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
5273 || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
5274 op = TREE_OPERAND (op, 0);
5275 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
5276 if (!leader)
5277 return NULL_TREE;
5279 tree res;
5280 stmts = NULL;
5281 if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
5282 res = gimple_build (&stmts, BIT_FIELD_REF,
5283 TREE_TYPE (val), leader,
5284 TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
5285 TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
5286 else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
5287 res = gimple_build (&stmts, BIT_AND_EXPR,
5288 TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
5289 else
5290 res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
5291 TREE_TYPE (val), leader);
5292 if (TREE_CODE (res) != SSA_NAME
5293 || SSA_NAME_IS_DEFAULT_DEF (res)
5294 || gimple_bb (SSA_NAME_DEF_STMT (res)))
5296 gimple_seq_discard (stmts);
5298 /* During propagation we have to treat SSA info conservatively
5299 and thus we can end up simplifying the inserted expression
5300 at elimination time to sth not defined in stmts. */
5301 /* But then this is a redundancy we failed to detect. Which means
5302 res now has two values. That doesn't play well with how
5303 we track availability here, so give up. */
5304 if (dump_file && (dump_flags & TDF_DETAILS))
5306 if (TREE_CODE (res) == SSA_NAME)
5307 res = eliminate_avail (bb, res);
5308 if (res)
5310 fprintf (dump_file, "Failed to insert expression for value ");
5311 print_generic_expr (dump_file, val);
5312 fprintf (dump_file, " which is really fully redundant to ");
5313 print_generic_expr (dump_file, res);
5314 fprintf (dump_file, "\n");
5318 return NULL_TREE;
5320 else
5322 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
5323 VN_INFO (res)->valnum = val;
5324 VN_INFO (res)->visited = true;
5327 insertions++;
5328 if (dump_file && (dump_flags & TDF_DETAILS))
5330 fprintf (dump_file, "Inserted ");
5331 print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
5334 return res;
5337 void
5338 eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
5340 tree sprime = NULL_TREE;
5341 gimple *stmt = gsi_stmt (*gsi);
5342 tree lhs = gimple_get_lhs (stmt);
5343 if (lhs && TREE_CODE (lhs) == SSA_NAME
5344 && !gimple_has_volatile_ops (stmt)
5345 /* See PR43491. Do not replace a global register variable when
5346 it is a the RHS of an assignment. Do replace local register
5347 variables since gcc does not guarantee a local variable will
5348 be allocated in register.
5349 ??? The fix isn't effective here. This should instead
5350 be ensured by not value-numbering them the same but treating
5351 them like volatiles? */
5352 && !(gimple_assign_single_p (stmt)
5353 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
5354 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
5355 && is_global_var (gimple_assign_rhs1 (stmt)))))
5357 sprime = eliminate_avail (b, lhs);
5358 if (!sprime)
5360 /* If there is no existing usable leader but SCCVN thinks
5361 it has an expression it wants to use as replacement,
5362 insert that. */
5363 tree val = VN_INFO (lhs)->valnum;
5364 if (val != VN_TOP
5365 && TREE_CODE (val) == SSA_NAME
5366 && VN_INFO (val)->needs_insertion
5367 && VN_INFO (val)->expr != NULL
5368 && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
5369 eliminate_push_avail (b, sprime);
5372 /* If this now constitutes a copy duplicate points-to
5373 and range info appropriately. This is especially
5374 important for inserted code. See tree-ssa-copy.c
5375 for similar code. */
5376 if (sprime
5377 && TREE_CODE (sprime) == SSA_NAME)
5379 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
5380 if (POINTER_TYPE_P (TREE_TYPE (lhs))
5381 && SSA_NAME_PTR_INFO (lhs)
5382 && ! SSA_NAME_PTR_INFO (sprime))
5384 duplicate_ssa_name_ptr_info (sprime,
5385 SSA_NAME_PTR_INFO (lhs));
5386 if (b != sprime_b)
5387 mark_ptr_info_alignment_unknown
5388 (SSA_NAME_PTR_INFO (sprime));
5390 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
5391 && SSA_NAME_RANGE_INFO (lhs)
5392 && ! SSA_NAME_RANGE_INFO (sprime)
5393 && b == sprime_b)
5394 duplicate_ssa_name_range_info (sprime,
5395 SSA_NAME_RANGE_TYPE (lhs),
5396 SSA_NAME_RANGE_INFO (lhs));
5399 /* Inhibit the use of an inserted PHI on a loop header when
5400 the address of the memory reference is a simple induction
5401 variable. In other cases the vectorizer won't do anything
5402 anyway (either it's loop invariant or a complicated
5403 expression). */
5404 if (sprime
5405 && TREE_CODE (sprime) == SSA_NAME
5406 && do_pre
5407 && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
5408 && loop_outer (b->loop_father)
5409 && has_zero_uses (sprime)
5410 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
5411 && gimple_assign_load_p (stmt))
5413 gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
5414 basic_block def_bb = gimple_bb (def_stmt);
5415 if (gimple_code (def_stmt) == GIMPLE_PHI
5416 && def_bb->loop_father->header == def_bb)
5418 loop_p loop = def_bb->loop_father;
5419 ssa_op_iter iter;
5420 tree op;
5421 bool found = false;
5422 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
5424 affine_iv iv;
5425 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
5426 if (def_bb
5427 && flow_bb_inside_loop_p (loop, def_bb)
5428 && simple_iv (loop, loop, op, &iv, true))
5430 found = true;
5431 break;
5434 if (found)
5436 if (dump_file && (dump_flags & TDF_DETAILS))
5438 fprintf (dump_file, "Not replacing ");
5439 print_gimple_expr (dump_file, stmt, 0);
5440 fprintf (dump_file, " with ");
5441 print_generic_expr (dump_file, sprime);
5442 fprintf (dump_file, " which would add a loop"
5443 " carried dependence to loop %d\n",
5444 loop->num);
5446 /* Don't keep sprime available. */
5447 sprime = NULL_TREE;
5452 if (sprime)
5454 /* If we can propagate the value computed for LHS into
5455 all uses don't bother doing anything with this stmt. */
5456 if (may_propagate_copy (lhs, sprime))
5458 /* Mark it for removal. */
5459 to_remove.safe_push (stmt);
5461 /* ??? Don't count copy/constant propagations. */
5462 if (gimple_assign_single_p (stmt)
5463 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
5464 || gimple_assign_rhs1 (stmt) == sprime))
5465 return;
5467 if (dump_file && (dump_flags & TDF_DETAILS))
5469 fprintf (dump_file, "Replaced ");
5470 print_gimple_expr (dump_file, stmt, 0);
5471 fprintf (dump_file, " with ");
5472 print_generic_expr (dump_file, sprime);
5473 fprintf (dump_file, " in all uses of ");
5474 print_gimple_stmt (dump_file, stmt, 0);
5477 eliminations++;
5478 return;
5481 /* If this is an assignment from our leader (which
5482 happens in the case the value-number is a constant)
5483 then there is nothing to do. Likewise if we run into
5484 inserted code that needed a conversion because of
5485 our type-agnostic value-numbering of loads. */
5486 if ((gimple_assign_single_p (stmt)
5487 || (is_gimple_assign (stmt)
5488 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
5489 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
5490 && sprime == gimple_assign_rhs1 (stmt))
5491 return;
5493 /* Else replace its RHS. */
5494 if (dump_file && (dump_flags & TDF_DETAILS))
5496 fprintf (dump_file, "Replaced ");
5497 print_gimple_expr (dump_file, stmt, 0);
5498 fprintf (dump_file, " with ");
5499 print_generic_expr (dump_file, sprime);
5500 fprintf (dump_file, " in ");
5501 print_gimple_stmt (dump_file, stmt, 0);
5503 eliminations++;
5505 bool can_make_abnormal_goto = (is_gimple_call (stmt)
5506 && stmt_can_make_abnormal_goto (stmt));
5507 gimple *orig_stmt = stmt;
5508 if (!useless_type_conversion_p (TREE_TYPE (lhs),
5509 TREE_TYPE (sprime)))
5511 /* We preserve conversions to but not from function or method
5512 types. This asymmetry makes it necessary to re-instantiate
5513 conversions here. */
5514 if (POINTER_TYPE_P (TREE_TYPE (lhs))
5515 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
5516 sprime = fold_convert (TREE_TYPE (lhs), sprime);
5517 else
5518 gcc_unreachable ();
5520 tree vdef = gimple_vdef (stmt);
5521 tree vuse = gimple_vuse (stmt);
5522 propagate_tree_value_into_stmt (gsi, sprime);
5523 stmt = gsi_stmt (*gsi);
5524 update_stmt (stmt);
5525 /* In case the VDEF on the original stmt was released, value-number
5526 it to the VUSE. This is to make vuse_ssa_val able to skip
5527 released virtual operands. */
5528 if (vdef != gimple_vdef (stmt))
5530 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
5531 VN_INFO (vdef)->valnum = vuse;
5534 /* If we removed EH side-effects from the statement, clean
5535 its EH information. */
5536 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
5538 bitmap_set_bit (need_eh_cleanup,
5539 gimple_bb (stmt)->index);
5540 if (dump_file && (dump_flags & TDF_DETAILS))
5541 fprintf (dump_file, " Removed EH side-effects.\n");
5544 /* Likewise for AB side-effects. */
5545 if (can_make_abnormal_goto
5546 && !stmt_can_make_abnormal_goto (stmt))
5548 bitmap_set_bit (need_ab_cleanup,
5549 gimple_bb (stmt)->index);
5550 if (dump_file && (dump_flags & TDF_DETAILS))
5551 fprintf (dump_file, " Removed AB side-effects.\n");
5554 return;
5558 /* If the statement is a scalar store, see if the expression
5559 has the same value number as its rhs. If so, the store is
5560 dead. */
5561 if (gimple_assign_single_p (stmt)
5562 && !gimple_has_volatile_ops (stmt)
5563 && !is_gimple_reg (gimple_assign_lhs (stmt))
5564 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
5565 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
5567 tree val;
5568 tree rhs = gimple_assign_rhs1 (stmt);
5569 vn_reference_t vnresult;
5570 /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
5571 typed load of a byte known to be 0x11 as 1 so a store of
5572 a boolean 1 is detected as redundant. Because of this we
5573 have to make sure to lookup with a ref where its size
5574 matches the precision. */
5575 tree lookup_lhs = lhs;
5576 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
5577 && (TREE_CODE (lhs) != COMPONENT_REF
5578 || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
5579 && !type_has_mode_precision_p (TREE_TYPE (lhs)))
5581 if (TREE_CODE (lhs) == COMPONENT_REF
5582 || TREE_CODE (lhs) == MEM_REF)
5584 tree ltype = build_nonstandard_integer_type
5585 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
5586 TYPE_UNSIGNED (TREE_TYPE (lhs)));
5587 if (TREE_CODE (lhs) == COMPONENT_REF)
5589 tree foff = component_ref_field_offset (lhs);
5590 tree f = TREE_OPERAND (lhs, 1);
5591 if (!poly_int_tree_p (foff))
5592 lookup_lhs = NULL_TREE;
5593 else
5594 lookup_lhs = build3 (BIT_FIELD_REF, ltype,
5595 TREE_OPERAND (lhs, 0),
5596 TYPE_SIZE (TREE_TYPE (lhs)),
5597 bit_from_pos
5598 (foff, DECL_FIELD_BIT_OFFSET (f)));
5600 else
5601 lookup_lhs = build2 (MEM_REF, ltype,
5602 TREE_OPERAND (lhs, 0),
5603 TREE_OPERAND (lhs, 1));
5605 else
5606 lookup_lhs = NULL_TREE;
5608 val = NULL_TREE;
5609 if (lookup_lhs)
5610 val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt), VN_WALK,
5611 &vnresult, false);
5612 if (TREE_CODE (rhs) == SSA_NAME)
5613 rhs = VN_INFO (rhs)->valnum;
5614 if (val
5615 && operand_equal_p (val, rhs, 0))
5617 /* We can only remove the later store if the former aliases
5618 at least all accesses the later one does or if the store
5619 was to readonly memory storing the same value. */
5620 alias_set_type set = get_alias_set (lhs);
5621 if (! vnresult
5622 || vnresult->set == set
5623 || alias_set_subset_of (set, vnresult->set))
5625 if (dump_file && (dump_flags & TDF_DETAILS))
5627 fprintf (dump_file, "Deleted redundant store ");
5628 print_gimple_stmt (dump_file, stmt, 0);
5631 /* Queue stmt for removal. */
5632 to_remove.safe_push (stmt);
5633 return;
5638 /* If this is a control statement value numbering left edges
5639 unexecuted on force the condition in a way consistent with
5640 that. */
5641 if (gcond *cond = dyn_cast <gcond *> (stmt))
5643 if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
5644 ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
5646 if (dump_file && (dump_flags & TDF_DETAILS))
5648 fprintf (dump_file, "Removing unexecutable edge from ");
5649 print_gimple_stmt (dump_file, stmt, 0);
5651 if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
5652 == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
5653 gimple_cond_make_true (cond);
5654 else
5655 gimple_cond_make_false (cond);
5656 update_stmt (cond);
5657 el_todo |= TODO_cleanup_cfg;
5658 return;
5662 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
5663 bool was_noreturn = (is_gimple_call (stmt)
5664 && gimple_call_noreturn_p (stmt));
5665 tree vdef = gimple_vdef (stmt);
5666 tree vuse = gimple_vuse (stmt);
5668 /* If we didn't replace the whole stmt (or propagate the result
5669 into all uses), replace all uses on this stmt with their
5670 leaders. */
5671 bool modified = false;
5672 use_operand_p use_p;
5673 ssa_op_iter iter;
5674 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5676 tree use = USE_FROM_PTR (use_p);
5677 /* ??? The call code above leaves stmt operands un-updated. */
5678 if (TREE_CODE (use) != SSA_NAME)
5679 continue;
5680 tree sprime;
5681 if (SSA_NAME_IS_DEFAULT_DEF (use))
5682 /* ??? For default defs BB shouldn't matter, but we have to
5683 solve the inconsistency between rpo eliminate and
5684 dom eliminate avail valueization first. */
5685 sprime = eliminate_avail (b, use);
5686 else
5687 /* Look for sth available at the definition block of the argument.
5688 This avoids inconsistencies between availability there which
5689 decides if the stmt can be removed and availability at the
5690 use site. The SSA property ensures that things available
5691 at the definition are also available at uses. */
5692 sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
5693 if (sprime && sprime != use
5694 && may_propagate_copy (use, sprime)
5695 /* We substitute into debug stmts to avoid excessive
5696 debug temporaries created by removed stmts, but we need
5697 to avoid doing so for inserted sprimes as we never want
5698 to create debug temporaries for them. */
5699 && (!inserted_exprs
5700 || TREE_CODE (sprime) != SSA_NAME
5701 || !is_gimple_debug (stmt)
5702 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
5704 propagate_value (use_p, sprime);
5705 modified = true;
5709 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
5710 into which is a requirement for the IPA devirt machinery. */
5711 gimple *old_stmt = stmt;
5712 if (modified)
5714 /* If a formerly non-invariant ADDR_EXPR is turned into an
5715 invariant one it was on a separate stmt. */
5716 if (gimple_assign_single_p (stmt)
5717 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
5718 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
5719 gimple_stmt_iterator prev = *gsi;
5720 gsi_prev (&prev);
5721 if (fold_stmt (gsi))
5723 /* fold_stmt may have created new stmts inbetween
5724 the previous stmt and the folded stmt. Mark
5725 all defs created there as varying to not confuse
5726 the SCCVN machinery as we're using that even during
5727 elimination. */
5728 if (gsi_end_p (prev))
5729 prev = gsi_start_bb (b);
5730 else
5731 gsi_next (&prev);
5732 if (gsi_stmt (prev) != gsi_stmt (*gsi))
5735 tree def;
5736 ssa_op_iter dit;
5737 FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
5738 dit, SSA_OP_ALL_DEFS)
5739 /* As existing DEFs may move between stmts
5740 only process new ones. */
5741 if (! has_VN_INFO (def))
5743 VN_INFO (def)->valnum = def;
5744 VN_INFO (def)->visited = true;
5746 if (gsi_stmt (prev) == gsi_stmt (*gsi))
5747 break;
5748 gsi_next (&prev);
5750 while (1);
5752 stmt = gsi_stmt (*gsi);
5753 /* In case we folded the stmt away schedule the NOP for removal. */
5754 if (gimple_nop_p (stmt))
5755 to_remove.safe_push (stmt);
5758 /* Visit indirect calls and turn them into direct calls if
5759 possible using the devirtualization machinery. Do this before
5760 checking for required EH/abnormal/noreturn cleanup as devird
5761 may expose more of those. */
5762 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
5764 tree fn = gimple_call_fn (call_stmt);
5765 if (fn
5766 && flag_devirtualize
5767 && virtual_method_call_p (fn))
5769 tree otr_type = obj_type_ref_class (fn);
5770 unsigned HOST_WIDE_INT otr_tok
5771 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
5772 tree instance;
5773 ipa_polymorphic_call_context context (current_function_decl,
5774 fn, stmt, &instance);
5775 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
5776 otr_type, stmt, NULL);
5777 bool final;
5778 vec <cgraph_node *> targets
5779 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
5780 otr_tok, context, &final);
5781 if (dump_file)
5782 dump_possible_polymorphic_call_targets (dump_file,
5783 obj_type_ref_class (fn),
5784 otr_tok, context);
5785 if (final && targets.length () <= 1 && dbg_cnt (devirt))
5787 tree fn;
5788 if (targets.length () == 1)
5789 fn = targets[0]->decl;
5790 else
5791 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
5792 if (dump_enabled_p ())
5794 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
5795 "converting indirect call to "
5796 "function %s\n",
5797 lang_hooks.decl_printable_name (fn, 2));
5799 gimple_call_set_fndecl (call_stmt, fn);
5800 /* If changing the call to __builtin_unreachable
5801 or similar noreturn function, adjust gimple_call_fntype
5802 too. */
5803 if (gimple_call_noreturn_p (call_stmt)
5804 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
5805 && TYPE_ARG_TYPES (TREE_TYPE (fn))
5806 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
5807 == void_type_node))
5808 gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
5809 maybe_remove_unused_call_args (cfun, call_stmt);
5810 modified = true;
5815 if (modified)
5817 /* When changing a call into a noreturn call, cfg cleanup
5818 is needed to fix up the noreturn call. */
5819 if (!was_noreturn
5820 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
5821 to_fixup.safe_push (stmt);
5822 /* When changing a condition or switch into one we know what
5823 edge will be executed, schedule a cfg cleanup. */
5824 if ((gimple_code (stmt) == GIMPLE_COND
5825 && (gimple_cond_true_p (as_a <gcond *> (stmt))
5826 || gimple_cond_false_p (as_a <gcond *> (stmt))))
5827 || (gimple_code (stmt) == GIMPLE_SWITCH
5828 && TREE_CODE (gimple_switch_index
5829 (as_a <gswitch *> (stmt))) == INTEGER_CST))
5830 el_todo |= TODO_cleanup_cfg;
5831 /* If we removed EH side-effects from the statement, clean
5832 its EH information. */
5833 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
5835 bitmap_set_bit (need_eh_cleanup,
5836 gimple_bb (stmt)->index);
5837 if (dump_file && (dump_flags & TDF_DETAILS))
5838 fprintf (dump_file, " Removed EH side-effects.\n");
5840 /* Likewise for AB side-effects. */
5841 if (can_make_abnormal_goto
5842 && !stmt_can_make_abnormal_goto (stmt))
5844 bitmap_set_bit (need_ab_cleanup,
5845 gimple_bb (stmt)->index);
5846 if (dump_file && (dump_flags & TDF_DETAILS))
5847 fprintf (dump_file, " Removed AB side-effects.\n");
5849 update_stmt (stmt);
5850 /* In case the VDEF on the original stmt was released, value-number
5851 it to the VUSE. This is to make vuse_ssa_val able to skip
5852 released virtual operands. */
5853 if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
5854 VN_INFO (vdef)->valnum = vuse;
5857 /* Make new values available - for fully redundant LHS we
5858 continue with the next stmt above and skip this. */
5859 def_operand_p defp;
5860 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
5861 eliminate_push_avail (b, DEF_FROM_PTR (defp));
5864 /* Perform elimination for the basic-block B during the domwalk. */
5866 edge
5867 eliminate_dom_walker::before_dom_children (basic_block b)
5869 /* Mark new bb. */
5870 avail_stack.safe_push (NULL_TREE);
5872 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
5873 if (!(b->flags & BB_EXECUTABLE))
5874 return NULL;
5876 vn_context_bb = b;
5878 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
5880 gphi *phi = gsi.phi ();
5881 tree res = PHI_RESULT (phi);
5883 if (virtual_operand_p (res))
5885 gsi_next (&gsi);
5886 continue;
5889 tree sprime = eliminate_avail (b, res);
5890 if (sprime
5891 && sprime != res)
5893 if (dump_file && (dump_flags & TDF_DETAILS))
5895 fprintf (dump_file, "Replaced redundant PHI node defining ");
5896 print_generic_expr (dump_file, res);
5897 fprintf (dump_file, " with ");
5898 print_generic_expr (dump_file, sprime);
5899 fprintf (dump_file, "\n");
5902 /* If we inserted this PHI node ourself, it's not an elimination. */
5903 if (! inserted_exprs
5904 || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
5905 eliminations++;
5907 /* If we will propagate into all uses don't bother to do
5908 anything. */
5909 if (may_propagate_copy (res, sprime))
5911 /* Mark the PHI for removal. */
5912 to_remove.safe_push (phi);
5913 gsi_next (&gsi);
5914 continue;
5917 remove_phi_node (&gsi, false);
5919 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
5920 sprime = fold_convert (TREE_TYPE (res), sprime);
5921 gimple *stmt = gimple_build_assign (res, sprime);
5922 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
5923 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
5924 continue;
5927 eliminate_push_avail (b, res);
5928 gsi_next (&gsi);
5931 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
5932 !gsi_end_p (gsi);
5933 gsi_next (&gsi))
5934 eliminate_stmt (b, &gsi);
5936 /* Replace destination PHI arguments. */
5937 edge_iterator ei;
5938 edge e;
5939 FOR_EACH_EDGE (e, ei, b->succs)
5940 if (e->flags & EDGE_EXECUTABLE)
5941 for (gphi_iterator gsi = gsi_start_phis (e->dest);
5942 !gsi_end_p (gsi);
5943 gsi_next (&gsi))
5945 gphi *phi = gsi.phi ();
5946 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
5947 tree arg = USE_FROM_PTR (use_p);
5948 if (TREE_CODE (arg) != SSA_NAME
5949 || virtual_operand_p (arg))
5950 continue;
5951 tree sprime = eliminate_avail (b, arg);
5952 if (sprime && may_propagate_copy (arg, sprime))
5953 propagate_value (use_p, sprime);
5956 vn_context_bb = NULL;
5958 return NULL;
5961 /* Make no longer available leaders no longer available. */
5963 void
5964 eliminate_dom_walker::after_dom_children (basic_block)
5966 tree entry;
5967 while ((entry = avail_stack.pop ()) != NULL_TREE)
5969 tree valnum = VN_INFO (entry)->valnum;
5970 tree old = avail[SSA_NAME_VERSION (valnum)];
5971 if (old == entry)
5972 avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
5973 else
5974 avail[SSA_NAME_VERSION (valnum)] = entry;
5978 /* Remove queued stmts and perform delayed cleanups. */
5980 unsigned
5981 eliminate_dom_walker::eliminate_cleanup (bool region_p)
5983 statistics_counter_event (cfun, "Eliminated", eliminations);
5984 statistics_counter_event (cfun, "Insertions", insertions);
5986 /* We cannot remove stmts during BB walk, especially not release SSA
5987 names there as this confuses the VN machinery. The stmts ending
5988 up in to_remove are either stores or simple copies.
5989 Remove stmts in reverse order to make debug stmt creation possible. */
5990 while (!to_remove.is_empty ())
5992 bool do_release_defs = true;
5993 gimple *stmt = to_remove.pop ();
5995 /* When we are value-numbering a region we do not require exit PHIs to
5996 be present so we have to make sure to deal with uses outside of the
5997 region of stmts that we thought are eliminated.
5998 ??? Note we may be confused by uses in dead regions we didn't run
5999 elimination on. Rather than checking individual uses we accept
6000 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
6001 contains such example). */
6002 if (region_p)
6004 if (gphi *phi = dyn_cast <gphi *> (stmt))
6006 tree lhs = gimple_phi_result (phi);
6007 if (!has_zero_uses (lhs))
6009 if (dump_file && (dump_flags & TDF_DETAILS))
6010 fprintf (dump_file, "Keeping eliminated stmt live "
6011 "as copy because of out-of-region uses\n");
6012 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
6013 gimple *copy = gimple_build_assign (lhs, sprime);
6014 gimple_stmt_iterator gsi
6015 = gsi_after_labels (gimple_bb (stmt));
6016 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
6017 do_release_defs = false;
6020 else if (tree lhs = gimple_get_lhs (stmt))
6021 if (TREE_CODE (lhs) == SSA_NAME
6022 && !has_zero_uses (lhs))
6024 if (dump_file && (dump_flags & TDF_DETAILS))
6025 fprintf (dump_file, "Keeping eliminated stmt live "
6026 "as copy because of out-of-region uses\n");
6027 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
6028 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
6029 if (is_gimple_assign (stmt))
6031 gimple_assign_set_rhs_from_tree (&gsi, sprime);
6032 stmt = gsi_stmt (gsi);
6033 update_stmt (stmt);
6034 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
6035 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
6036 continue;
6038 else
6040 gimple *copy = gimple_build_assign (lhs, sprime);
6041 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
6042 do_release_defs = false;
6047 if (dump_file && (dump_flags & TDF_DETAILS))
6049 fprintf (dump_file, "Removing dead stmt ");
6050 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
6053 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
6054 if (gimple_code (stmt) == GIMPLE_PHI)
6055 remove_phi_node (&gsi, do_release_defs);
6056 else
6058 basic_block bb = gimple_bb (stmt);
6059 unlink_stmt_vdef (stmt);
6060 if (gsi_remove (&gsi, true))
6061 bitmap_set_bit (need_eh_cleanup, bb->index);
6062 if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
6063 bitmap_set_bit (need_ab_cleanup, bb->index);
6064 if (do_release_defs)
6065 release_defs (stmt);
6068 /* Removing a stmt may expose a forwarder block. */
6069 el_todo |= TODO_cleanup_cfg;
6072 /* Fixup stmts that became noreturn calls. This may require splitting
6073 blocks and thus isn't possible during the dominator walk. Do this
6074 in reverse order so we don't inadvertedly remove a stmt we want to
6075 fixup by visiting a dominating now noreturn call first. */
6076 while (!to_fixup.is_empty ())
6078 gimple *stmt = to_fixup.pop ();
6080 if (dump_file && (dump_flags & TDF_DETAILS))
6082 fprintf (dump_file, "Fixing up noreturn call ");
6083 print_gimple_stmt (dump_file, stmt, 0);
6086 if (fixup_noreturn_call (stmt))
6087 el_todo |= TODO_cleanup_cfg;
6090 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
6091 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
6093 if (do_eh_cleanup)
6094 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
6096 if (do_ab_cleanup)
6097 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
6099 if (do_eh_cleanup || do_ab_cleanup)
6100 el_todo |= TODO_cleanup_cfg;
6102 return el_todo;
6105 /* Eliminate fully redundant computations. */
6107 unsigned
6108 eliminate_with_rpo_vn (bitmap inserted_exprs)
6110 eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
6112 walker.walk (cfun->cfg->x_entry_block_ptr);
6113 return walker.eliminate_cleanup ();
6116 static unsigned
6117 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
6118 bool iterate, bool eliminate);
6120 void
6121 run_rpo_vn (vn_lookup_kind kind)
6123 default_vn_walk_kind = kind;
6124 do_rpo_vn (cfun, NULL, NULL, true, false);
6126 /* ??? Prune requirement of these. */
6127 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
6128 constant_value_ids = BITMAP_ALLOC (NULL);
6130 /* Initialize the value ids and prune out remaining VN_TOPs
6131 from dead code. */
6132 tree name;
6133 unsigned i;
6134 FOR_EACH_SSA_NAME (i, name, cfun)
6136 vn_ssa_aux_t info = VN_INFO (name);
6137 if (!info->visited
6138 || info->valnum == VN_TOP)
6139 info->valnum = name;
6140 if (info->valnum == name)
6141 info->value_id = get_next_value_id ();
6142 else if (is_gimple_min_invariant (info->valnum))
6143 info->value_id = get_or_alloc_constant_value_id (info->valnum);
6146 /* Propagate. */
6147 FOR_EACH_SSA_NAME (i, name, cfun)
6149 vn_ssa_aux_t info = VN_INFO (name);
6150 if (TREE_CODE (info->valnum) == SSA_NAME
6151 && info->valnum != name
6152 && info->value_id != VN_INFO (info->valnum)->value_id)
6153 info->value_id = VN_INFO (info->valnum)->value_id;
6156 set_hashtable_value_ids ();
6158 if (dump_file && (dump_flags & TDF_DETAILS))
6160 fprintf (dump_file, "Value numbers:\n");
6161 FOR_EACH_SSA_NAME (i, name, cfun)
6163 if (VN_INFO (name)->visited
6164 && SSA_VAL (name) != name)
6166 print_generic_expr (dump_file, name);
6167 fprintf (dump_file, " = ");
6168 print_generic_expr (dump_file, SSA_VAL (name));
6169 fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
6175 /* Free VN associated data structures. */
6177 void
6178 free_rpo_vn (void)
6180 free_vn_table (valid_info);
6181 XDELETE (valid_info);
6182 obstack_free (&vn_tables_obstack, NULL);
6183 obstack_free (&vn_tables_insert_obstack, NULL);
6185 vn_ssa_aux_iterator_type it;
6186 vn_ssa_aux_t info;
6187 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
6188 if (info->needs_insertion)
6189 release_ssa_name (info->name);
6190 obstack_free (&vn_ssa_aux_obstack, NULL);
6191 delete vn_ssa_aux_hash;
6193 delete constant_to_value_id;
6194 constant_to_value_id = NULL;
6195 BITMAP_FREE (constant_value_ids);
6198 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
6200 static tree
6201 vn_lookup_simplify_result (gimple_match_op *res_op)
6203 if (!res_op->code.is_tree_code ())
6204 return NULL_TREE;
6205 tree *ops = res_op->ops;
6206 unsigned int length = res_op->num_ops;
6207 if (res_op->code == CONSTRUCTOR
6208 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
6209 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
6210 && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
6212 length = CONSTRUCTOR_NELTS (res_op->ops[0]);
6213 ops = XALLOCAVEC (tree, length);
6214 for (unsigned i = 0; i < length; ++i)
6215 ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
6217 vn_nary_op_t vnresult = NULL;
6218 tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
6219 res_op->type, ops, &vnresult);
6220 /* If this is used from expression simplification make sure to
6221 return an available expression. */
6222 if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
6223 res = rpo_avail->eliminate_avail (vn_context_bb, res);
6224 return res;
6227 /* Return a leader for OPs value that is valid at BB. */
6229 tree
6230 rpo_elim::eliminate_avail (basic_block bb, tree op)
6232 bool visited;
6233 tree valnum = SSA_VAL (op, &visited);
6234 /* If we didn't visit OP then it must be defined outside of the
6235 region we process and also dominate it. So it is available. */
6236 if (!visited)
6237 return op;
6238 if (TREE_CODE (valnum) == SSA_NAME)
6240 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6241 return valnum;
6242 vn_avail *av = VN_INFO (valnum)->avail;
6243 if (!av)
6244 return NULL_TREE;
6245 if (av->location == bb->index)
6246 /* On tramp3d 90% of the cases are here. */
6247 return ssa_name (av->leader);
6250 basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
6251 /* ??? During elimination we have to use availability at the
6252 definition site of a use we try to replace. This
6253 is required to not run into inconsistencies because
6254 of dominated_by_p_w_unex behavior and removing a definition
6255 while not replacing all uses.
6256 ??? We could try to consistently walk dominators
6257 ignoring non-executable regions. The nearest common
6258 dominator of bb and abb is where we can stop walking. We
6259 may also be able to "pre-compute" (bits of) the next immediate
6260 (non-)dominator during the RPO walk when marking edges as
6261 executable. */
6262 if (dominated_by_p_w_unex (bb, abb))
6264 tree leader = ssa_name (av->leader);
6265 /* Prevent eliminations that break loop-closed SSA. */
6266 if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
6267 && ! SSA_NAME_IS_DEFAULT_DEF (leader)
6268 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
6269 (leader))->loop_father,
6270 bb))
6271 return NULL_TREE;
6272 if (dump_file && (dump_flags & TDF_DETAILS))
6274 print_generic_expr (dump_file, leader);
6275 fprintf (dump_file, " is available for ");
6276 print_generic_expr (dump_file, valnum);
6277 fprintf (dump_file, "\n");
6279 /* On tramp3d 99% of the _remaining_ cases succeed at
6280 the first enty. */
6281 return leader;
6283 /* ??? Can we somehow skip to the immediate dominator
6284 RPO index (bb_to_rpo)? Again, maybe not worth, on
6285 tramp3d the worst number of elements in the vector is 9. */
6286 av = av->next;
6288 while (av);
6290 else if (valnum != VN_TOP)
6291 /* valnum is is_gimple_min_invariant. */
6292 return valnum;
6293 return NULL_TREE;
6296 /* Make LEADER a leader for its value at BB. */
6298 void
6299 rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
6301 tree valnum = VN_INFO (leader)->valnum;
6302 if (valnum == VN_TOP
6303 || is_gimple_min_invariant (valnum))
6304 return;
6305 if (dump_file && (dump_flags & TDF_DETAILS))
6307 fprintf (dump_file, "Making available beyond BB%d ", bb->index);
6308 print_generic_expr (dump_file, leader);
6309 fprintf (dump_file, " for value ");
6310 print_generic_expr (dump_file, valnum);
6311 fprintf (dump_file, "\n");
6313 vn_ssa_aux_t value = VN_INFO (valnum);
6314 vn_avail *av;
6315 if (m_avail_freelist)
6317 av = m_avail_freelist;
6318 m_avail_freelist = m_avail_freelist->next;
6320 else
6321 av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
6322 av->location = bb->index;
6323 av->leader = SSA_NAME_VERSION (leader);
6324 av->next = value->avail;
6325 value->avail = av;
6328 /* Valueization hook for RPO VN plus required state. */
6330 tree
6331 rpo_vn_valueize (tree name)
6333 if (TREE_CODE (name) == SSA_NAME)
6335 vn_ssa_aux_t val = VN_INFO (name);
6336 if (val)
6338 tree tem = val->valnum;
6339 if (tem != VN_TOP && tem != name)
6341 if (TREE_CODE (tem) != SSA_NAME)
6342 return tem;
6343 /* For all values we only valueize to an available leader
6344 which means we can use SSA name info without restriction. */
6345 tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
6346 if (tem)
6347 return tem;
6351 return name;
6354 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
6355 inverted condition. */
6357 static void
6358 insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
6360 switch (code)
6362 case LT_EXPR:
6363 /* a < b -> a {!,<}= b */
6364 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
6365 ops, boolean_true_node, 0, pred_e);
6366 vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
6367 ops, boolean_true_node, 0, pred_e);
6368 /* a < b -> ! a {>,=} b */
6369 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
6370 ops, boolean_false_node, 0, pred_e);
6371 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
6372 ops, boolean_false_node, 0, pred_e);
6373 break;
6374 case GT_EXPR:
6375 /* a > b -> a {!,>}= b */
6376 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
6377 ops, boolean_true_node, 0, pred_e);
6378 vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
6379 ops, boolean_true_node, 0, pred_e);
6380 /* a > b -> ! a {<,=} b */
6381 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
6382 ops, boolean_false_node, 0, pred_e);
6383 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
6384 ops, boolean_false_node, 0, pred_e);
6385 break;
6386 case EQ_EXPR:
6387 /* a == b -> ! a {<,>} b */
6388 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
6389 ops, boolean_false_node, 0, pred_e);
6390 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
6391 ops, boolean_false_node, 0, pred_e);
6392 break;
6393 case LE_EXPR:
6394 case GE_EXPR:
6395 case NE_EXPR:
6396 /* Nothing besides inverted condition. */
6397 break;
6398 default:;
6402 /* Main stmt worker for RPO VN, process BB. */
6404 static unsigned
6405 process_bb (rpo_elim &avail, basic_block bb,
6406 bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
6407 bool do_region, bitmap exit_bbs, bool skip_phis)
6409 unsigned todo = 0;
6410 edge_iterator ei;
6411 edge e;
6413 vn_context_bb = bb;
6415 /* If we are in loop-closed SSA preserve this state. This is
6416 relevant when called on regions from outside of FRE/PRE. */
6417 bool lc_phi_nodes = false;
6418 if (!skip_phis
6419 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
6420 FOR_EACH_EDGE (e, ei, bb->preds)
6421 if (e->src->loop_father != e->dest->loop_father
6422 && flow_loop_nested_p (e->dest->loop_father,
6423 e->src->loop_father))
6425 lc_phi_nodes = true;
6426 break;
6429 /* When we visit a loop header substitute into loop info. */
6430 if (!iterate && eliminate && bb->loop_father->header == bb)
6432 /* Keep fields in sync with substitute_in_loop_info. */
6433 if (bb->loop_father->nb_iterations)
6434 bb->loop_father->nb_iterations
6435 = simplify_replace_tree (bb->loop_father->nb_iterations,
6436 NULL_TREE, NULL_TREE, &vn_valueize_wrapper);
6439 /* Value-number all defs in the basic-block. */
6440 if (!skip_phis)
6441 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
6442 gsi_next (&gsi))
6444 gphi *phi = gsi.phi ();
6445 tree res = PHI_RESULT (phi);
6446 vn_ssa_aux_t res_info = VN_INFO (res);
6447 if (!bb_visited)
6449 gcc_assert (!res_info->visited);
6450 res_info->valnum = VN_TOP;
6451 res_info->visited = true;
6454 /* When not iterating force backedge values to varying. */
6455 visit_stmt (phi, !iterate_phis);
6456 if (virtual_operand_p (res))
6457 continue;
6459 /* Eliminate */
6460 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
6461 how we handle backedges and availability.
6462 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
6463 tree val = res_info->valnum;
6464 if (res != val && !iterate && eliminate)
6466 if (tree leader = avail.eliminate_avail (bb, res))
6468 if (leader != res
6469 /* Preserve loop-closed SSA form. */
6470 && (! lc_phi_nodes
6471 || is_gimple_min_invariant (leader)))
6473 if (dump_file && (dump_flags & TDF_DETAILS))
6475 fprintf (dump_file, "Replaced redundant PHI node "
6476 "defining ");
6477 print_generic_expr (dump_file, res);
6478 fprintf (dump_file, " with ");
6479 print_generic_expr (dump_file, leader);
6480 fprintf (dump_file, "\n");
6482 avail.eliminations++;
6484 if (may_propagate_copy (res, leader))
6486 /* Schedule for removal. */
6487 avail.to_remove.safe_push (phi);
6488 continue;
6490 /* ??? Else generate a copy stmt. */
6494 /* Only make defs available that not already are. But make
6495 sure loop-closed SSA PHI node defs are picked up for
6496 downstream uses. */
6497 if (lc_phi_nodes
6498 || res == val
6499 || ! avail.eliminate_avail (bb, res))
6500 avail.eliminate_push_avail (bb, res);
6503 /* For empty BBs mark outgoing edges executable. For non-empty BBs
6504 we do this when processing the last stmt as we have to do this
6505 before elimination which otherwise forces GIMPLE_CONDs to
6506 if (1 != 0) style when seeing non-executable edges. */
6507 if (gsi_end_p (gsi_start_bb (bb)))
6509 FOR_EACH_EDGE (e, ei, bb->succs)
6511 if (!(e->flags & EDGE_EXECUTABLE))
6513 if (dump_file && (dump_flags & TDF_DETAILS))
6514 fprintf (dump_file,
6515 "marking outgoing edge %d -> %d executable\n",
6516 e->src->index, e->dest->index);
6517 e->flags |= EDGE_EXECUTABLE;
6518 e->dest->flags |= BB_EXECUTABLE;
6520 else if (!(e->dest->flags & BB_EXECUTABLE))
6522 if (dump_file && (dump_flags & TDF_DETAILS))
6523 fprintf (dump_file,
6524 "marking destination block %d reachable\n",
6525 e->dest->index);
6526 e->dest->flags |= BB_EXECUTABLE;
6530 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
6531 !gsi_end_p (gsi); gsi_next (&gsi))
6533 ssa_op_iter i;
6534 tree op;
6535 if (!bb_visited)
6537 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
6539 vn_ssa_aux_t op_info = VN_INFO (op);
6540 gcc_assert (!op_info->visited);
6541 op_info->valnum = VN_TOP;
6542 op_info->visited = true;
6545 /* We somehow have to deal with uses that are not defined
6546 in the processed region. Forcing unvisited uses to
6547 varying here doesn't play well with def-use following during
6548 expression simplification, so we deal with this by checking
6549 the visited flag in SSA_VAL. */
6552 visit_stmt (gsi_stmt (gsi));
6554 gimple *last = gsi_stmt (gsi);
6555 e = NULL;
6556 switch (gimple_code (last))
6558 case GIMPLE_SWITCH:
6559 e = find_taken_edge (bb, vn_valueize (gimple_switch_index
6560 (as_a <gswitch *> (last))));
6561 break;
6562 case GIMPLE_COND:
6564 tree lhs = vn_valueize (gimple_cond_lhs (last));
6565 tree rhs = vn_valueize (gimple_cond_rhs (last));
6566 tree val = gimple_simplify (gimple_cond_code (last),
6567 boolean_type_node, lhs, rhs,
6568 NULL, vn_valueize);
6569 /* If the condition didn't simplfy see if we have recorded
6570 an expression from sofar taken edges. */
6571 if (! val || TREE_CODE (val) != INTEGER_CST)
6573 vn_nary_op_t vnresult;
6574 tree ops[2];
6575 ops[0] = lhs;
6576 ops[1] = rhs;
6577 val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
6578 boolean_type_node, ops,
6579 &vnresult);
6580 /* Did we get a predicated value? */
6581 if (! val && vnresult && vnresult->predicated_values)
6583 val = vn_nary_op_get_predicated_value (vnresult, bb);
6584 if (val && dump_file && (dump_flags & TDF_DETAILS))
6586 fprintf (dump_file, "Got predicated value ");
6587 print_generic_expr (dump_file, val, TDF_NONE);
6588 fprintf (dump_file, " for ");
6589 print_gimple_stmt (dump_file, last, TDF_SLIM);
6593 if (val)
6594 e = find_taken_edge (bb, val);
6595 if (! e)
6597 /* If we didn't manage to compute the taken edge then
6598 push predicated expressions for the condition itself
6599 and related conditions to the hashtables. This allows
6600 simplification of redundant conditions which is
6601 important as early cleanup. */
6602 edge true_e, false_e;
6603 extract_true_false_edges_from_block (bb, &true_e, &false_e);
6604 enum tree_code code = gimple_cond_code (last);
6605 enum tree_code icode
6606 = invert_tree_comparison (code, HONOR_NANS (lhs));
6607 tree ops[2];
6608 ops[0] = lhs;
6609 ops[1] = rhs;
6610 if (do_region
6611 && bitmap_bit_p (exit_bbs, true_e->dest->index))
6612 true_e = NULL;
6613 if (do_region
6614 && bitmap_bit_p (exit_bbs, false_e->dest->index))
6615 false_e = NULL;
6616 if (true_e)
6617 vn_nary_op_insert_pieces_predicated
6618 (2, code, boolean_type_node, ops,
6619 boolean_true_node, 0, true_e);
6620 if (false_e)
6621 vn_nary_op_insert_pieces_predicated
6622 (2, code, boolean_type_node, ops,
6623 boolean_false_node, 0, false_e);
6624 if (icode != ERROR_MARK)
6626 if (true_e)
6627 vn_nary_op_insert_pieces_predicated
6628 (2, icode, boolean_type_node, ops,
6629 boolean_false_node, 0, true_e);
6630 if (false_e)
6631 vn_nary_op_insert_pieces_predicated
6632 (2, icode, boolean_type_node, ops,
6633 boolean_true_node, 0, false_e);
6635 /* Relax for non-integers, inverted condition handled
6636 above. */
6637 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
6639 if (true_e)
6640 insert_related_predicates_on_edge (code, ops, true_e);
6641 if (false_e)
6642 insert_related_predicates_on_edge (icode, ops, false_e);
6645 break;
6647 case GIMPLE_GOTO:
6648 e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
6649 break;
6650 default:
6651 e = NULL;
6653 if (e)
6655 todo = TODO_cleanup_cfg;
6656 if (!(e->flags & EDGE_EXECUTABLE))
6658 if (dump_file && (dump_flags & TDF_DETAILS))
6659 fprintf (dump_file,
6660 "marking known outgoing %sedge %d -> %d executable\n",
6661 e->flags & EDGE_DFS_BACK ? "back-" : "",
6662 e->src->index, e->dest->index);
6663 e->flags |= EDGE_EXECUTABLE;
6664 e->dest->flags |= BB_EXECUTABLE;
6666 else if (!(e->dest->flags & BB_EXECUTABLE))
6668 if (dump_file && (dump_flags & TDF_DETAILS))
6669 fprintf (dump_file,
6670 "marking destination block %d reachable\n",
6671 e->dest->index);
6672 e->dest->flags |= BB_EXECUTABLE;
6675 else if (gsi_one_before_end_p (gsi))
6677 FOR_EACH_EDGE (e, ei, bb->succs)
6679 if (!(e->flags & EDGE_EXECUTABLE))
6681 if (dump_file && (dump_flags & TDF_DETAILS))
6682 fprintf (dump_file,
6683 "marking outgoing edge %d -> %d executable\n",
6684 e->src->index, e->dest->index);
6685 e->flags |= EDGE_EXECUTABLE;
6686 e->dest->flags |= BB_EXECUTABLE;
6688 else if (!(e->dest->flags & BB_EXECUTABLE))
6690 if (dump_file && (dump_flags & TDF_DETAILS))
6691 fprintf (dump_file,
6692 "marking destination block %d reachable\n",
6693 e->dest->index);
6694 e->dest->flags |= BB_EXECUTABLE;
6699 /* Eliminate. That also pushes to avail. */
6700 if (eliminate && ! iterate)
6701 avail.eliminate_stmt (bb, &gsi);
6702 else
6703 /* If not eliminating, make all not already available defs
6704 available. */
6705 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
6706 if (! avail.eliminate_avail (bb, op))
6707 avail.eliminate_push_avail (bb, op);
6710 /* Eliminate in destination PHI arguments. Always substitute in dest
6711 PHIs, even for non-executable edges. This handles region
6712 exits PHIs. */
6713 if (!iterate && eliminate)
6714 FOR_EACH_EDGE (e, ei, bb->succs)
6715 for (gphi_iterator gsi = gsi_start_phis (e->dest);
6716 !gsi_end_p (gsi); gsi_next (&gsi))
6718 gphi *phi = gsi.phi ();
6719 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
6720 tree arg = USE_FROM_PTR (use_p);
6721 if (TREE_CODE (arg) != SSA_NAME
6722 || virtual_operand_p (arg))
6723 continue;
6724 tree sprime;
6725 if (SSA_NAME_IS_DEFAULT_DEF (arg))
6727 sprime = SSA_VAL (arg);
6728 gcc_assert (TREE_CODE (sprime) != SSA_NAME
6729 || SSA_NAME_IS_DEFAULT_DEF (sprime));
6731 else
6732 /* Look for sth available at the definition block of the argument.
6733 This avoids inconsistencies between availability there which
6734 decides if the stmt can be removed and availability at the
6735 use site. The SSA property ensures that things available
6736 at the definition are also available at uses. */
6737 sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
6738 arg);
6739 if (sprime
6740 && sprime != arg
6741 && may_propagate_copy (arg, sprime))
6742 propagate_value (use_p, sprime);
6745 vn_context_bb = NULL;
6746 return todo;
6749 /* Unwind state per basic-block. */
6751 struct unwind_state
6753 /* Times this block has been visited. */
6754 unsigned visited;
6755 /* Whether to handle this as iteration point or whether to treat
6756 incoming backedge PHI values as varying. */
6757 bool iterate;
6758 /* Maximum RPO index this block is reachable from. */
6759 int max_rpo;
6760 /* Unwind state. */
6761 void *ob_top;
6762 vn_reference_t ref_top;
6763 vn_phi_t phi_top;
6764 vn_nary_op_t nary_top;
6767 /* Unwind the RPO VN state for iteration. */
6769 static void
6770 do_unwind (unwind_state *to, int rpo_idx, rpo_elim &avail, int *bb_to_rpo)
6772 gcc_assert (to->iterate);
6773 for (; last_inserted_nary != to->nary_top;
6774 last_inserted_nary = last_inserted_nary->next)
6776 vn_nary_op_t *slot;
6777 slot = valid_info->nary->find_slot_with_hash
6778 (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
6779 /* Predication causes the need to restore previous state. */
6780 if ((*slot)->unwind_to)
6781 *slot = (*slot)->unwind_to;
6782 else
6783 valid_info->nary->clear_slot (slot);
6785 for (; last_inserted_phi != to->phi_top;
6786 last_inserted_phi = last_inserted_phi->next)
6788 vn_phi_t *slot;
6789 slot = valid_info->phis->find_slot_with_hash
6790 (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
6791 valid_info->phis->clear_slot (slot);
6793 for (; last_inserted_ref != to->ref_top;
6794 last_inserted_ref = last_inserted_ref->next)
6796 vn_reference_t *slot;
6797 slot = valid_info->references->find_slot_with_hash
6798 (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
6799 (*slot)->operands.release ();
6800 valid_info->references->clear_slot (slot);
6802 obstack_free (&vn_tables_obstack, to->ob_top);
6804 /* Prune [rpo_idx, ] from avail. */
6805 /* ??? This is O(number-of-values-in-region) which is
6806 O(region-size) rather than O(iteration-piece). */
6807 for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
6808 i != vn_ssa_aux_hash->end (); ++i)
6810 while ((*i)->avail)
6812 if (bb_to_rpo[(*i)->avail->location] < rpo_idx)
6813 break;
6814 vn_avail *av = (*i)->avail;
6815 (*i)->avail = (*i)->avail->next;
6816 av->next = avail.m_avail_freelist;
6817 avail.m_avail_freelist = av;
6822 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
6823 If ITERATE is true then treat backedges optimistically as not
6824 executed and iterate. If ELIMINATE is true then perform
6825 elimination, otherwise leave that to the caller. */
6827 static unsigned
6828 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
6829 bool iterate, bool eliminate)
6831 unsigned todo = 0;
6833 /* We currently do not support region-based iteration when
6834 elimination is requested. */
6835 gcc_assert (!entry || !iterate || !eliminate);
6836 /* When iterating we need loop info up-to-date. */
6837 gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
6839 bool do_region = entry != NULL;
6840 if (!do_region)
6842 entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
6843 exit_bbs = BITMAP_ALLOC (NULL);
6844 bitmap_set_bit (exit_bbs, EXIT_BLOCK);
6847 /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
6848 re-mark those that are contained in the region. */
6849 edge_iterator ei;
6850 edge e;
6851 FOR_EACH_EDGE (e, ei, entry->dest->preds)
6852 e->flags &= ~EDGE_DFS_BACK;
6854 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
6855 int n = rev_post_order_and_mark_dfs_back_seme
6856 (fn, entry, exit_bbs, !loops_state_satisfies_p (LOOPS_NEED_FIXUP), rpo);
6857 /* rev_post_order_and_mark_dfs_back_seme fills RPO in reverse order. */
6858 for (int i = 0; i < n / 2; ++i)
6859 std::swap (rpo[i], rpo[n-i-1]);
6861 if (!do_region)
6862 BITMAP_FREE (exit_bbs);
6864 /* If there are any non-DFS_BACK edges into entry->dest skip
6865 processing PHI nodes for that block. This supports
6866 value-numbering loop bodies w/o the actual loop. */
6867 FOR_EACH_EDGE (e, ei, entry->dest->preds)
6868 if (e != entry
6869 && !(e->flags & EDGE_DFS_BACK))
6870 break;
6871 bool skip_entry_phis = e != NULL;
6872 if (skip_entry_phis && dump_file && (dump_flags & TDF_DETAILS))
6873 fprintf (dump_file, "Region does not contain all edges into "
6874 "the entry block, skipping its PHIs.\n");
6876 int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
6877 for (int i = 0; i < n; ++i)
6878 bb_to_rpo[rpo[i]] = i;
6880 unwind_state *rpo_state = XNEWVEC (unwind_state, n);
6882 rpo_elim avail (entry->dest);
6883 rpo_avail = &avail;
6885 /* Verify we have no extra entries into the region. */
6886 if (flag_checking && do_region)
6888 auto_bb_flag bb_in_region (fn);
6889 for (int i = 0; i < n; ++i)
6891 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6892 bb->flags |= bb_in_region;
6894 /* We can't merge the first two loops because we cannot rely
6895 on EDGE_DFS_BACK for edges not within the region. But if
6896 we decide to always have the bb_in_region flag we can
6897 do the checking during the RPO walk itself (but then it's
6898 also easy to handle MEME conservatively). */
6899 for (int i = 0; i < n; ++i)
6901 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6902 edge e;
6903 edge_iterator ei;
6904 FOR_EACH_EDGE (e, ei, bb->preds)
6905 gcc_assert (e == entry
6906 || (skip_entry_phis && bb == entry->dest)
6907 || (e->src->flags & bb_in_region));
6909 for (int i = 0; i < n; ++i)
6911 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6912 bb->flags &= ~bb_in_region;
6916 /* Create the VN state. For the initial size of the various hashtables
6917 use a heuristic based on region size and number of SSA names. */
6918 unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
6919 / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
6920 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
6921 next_value_id = 1;
6923 vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
6924 gcc_obstack_init (&vn_ssa_aux_obstack);
6926 gcc_obstack_init (&vn_tables_obstack);
6927 gcc_obstack_init (&vn_tables_insert_obstack);
6928 valid_info = XCNEW (struct vn_tables_s);
6929 allocate_vn_table (valid_info, region_size);
6930 last_inserted_ref = NULL;
6931 last_inserted_phi = NULL;
6932 last_inserted_nary = NULL;
6934 vn_valueize = rpo_vn_valueize;
6936 /* Initialize the unwind state and edge/BB executable state. */
6937 bool need_max_rpo_iterate = false;
6938 for (int i = 0; i < n; ++i)
6940 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6941 rpo_state[i].visited = 0;
6942 rpo_state[i].max_rpo = i;
6943 bb->flags &= ~BB_EXECUTABLE;
6944 bool has_backedges = false;
6945 edge e;
6946 edge_iterator ei;
6947 FOR_EACH_EDGE (e, ei, bb->preds)
6949 if (e->flags & EDGE_DFS_BACK)
6950 has_backedges = true;
6951 e->flags &= ~EDGE_EXECUTABLE;
6952 if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
6953 continue;
6954 if (bb_to_rpo[e->src->index] > i)
6956 rpo_state[i].max_rpo = MAX (rpo_state[i].max_rpo,
6957 bb_to_rpo[e->src->index]);
6958 need_max_rpo_iterate = true;
6960 else
6961 rpo_state[i].max_rpo
6962 = MAX (rpo_state[i].max_rpo,
6963 rpo_state[bb_to_rpo[e->src->index]].max_rpo);
6965 rpo_state[i].iterate = iterate && has_backedges;
6967 entry->flags |= EDGE_EXECUTABLE;
6968 entry->dest->flags |= BB_EXECUTABLE;
6970 /* When there are irreducible regions the simplistic max_rpo computation
6971 above for the case of backedges doesn't work and we need to iterate
6972 until there are no more changes. */
6973 unsigned nit = 0;
6974 while (need_max_rpo_iterate)
6976 nit++;
6977 need_max_rpo_iterate = false;
6978 for (int i = 0; i < n; ++i)
6980 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6981 edge e;
6982 edge_iterator ei;
6983 FOR_EACH_EDGE (e, ei, bb->preds)
6985 if (e == entry || (skip_entry_phis && bb == entry->dest))
6986 continue;
6987 int max_rpo = MAX (rpo_state[i].max_rpo,
6988 rpo_state[bb_to_rpo[e->src->index]].max_rpo);
6989 if (rpo_state[i].max_rpo != max_rpo)
6991 rpo_state[i].max_rpo = max_rpo;
6992 need_max_rpo_iterate = true;
6997 statistics_histogram_event (cfun, "RPO max_rpo iterations", nit);
6999 /* As heuristic to improve compile-time we handle only the N innermost
7000 loops and the outermost one optimistically. */
7001 if (iterate)
7003 loop_p loop;
7004 unsigned max_depth = param_rpo_vn_max_loop_depth;
7005 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
7006 if (loop_depth (loop) > max_depth)
7007 for (unsigned i = 2;
7008 i < loop_depth (loop) - max_depth; ++i)
7010 basic_block header = superloop_at_depth (loop, i)->header;
7011 bool non_latch_backedge = false;
7012 edge e;
7013 edge_iterator ei;
7014 FOR_EACH_EDGE (e, ei, header->preds)
7015 if (e->flags & EDGE_DFS_BACK)
7017 /* There can be a non-latch backedge into the header
7018 which is part of an outer irreducible region. We
7019 cannot avoid iterating this block then. */
7020 if (!dominated_by_p (CDI_DOMINATORS,
7021 e->src, e->dest))
7023 if (dump_file && (dump_flags & TDF_DETAILS))
7024 fprintf (dump_file, "non-latch backedge %d -> %d "
7025 "forces iteration of loop %d\n",
7026 e->src->index, e->dest->index, loop->num);
7027 non_latch_backedge = true;
7029 else
7030 e->flags |= EDGE_EXECUTABLE;
7032 rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
7036 uint64_t nblk = 0;
7037 int idx = 0;
7038 if (iterate)
7039 /* Go and process all blocks, iterating as necessary. */
7042 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
7044 /* If the block has incoming backedges remember unwind state. This
7045 is required even for non-executable blocks since in irreducible
7046 regions we might reach them via the backedge and re-start iterating
7047 from there.
7048 Note we can individually mark blocks with incoming backedges to
7049 not iterate where we then handle PHIs conservatively. We do that
7050 heuristically to reduce compile-time for degenerate cases. */
7051 if (rpo_state[idx].iterate)
7053 rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
7054 rpo_state[idx].ref_top = last_inserted_ref;
7055 rpo_state[idx].phi_top = last_inserted_phi;
7056 rpo_state[idx].nary_top = last_inserted_nary;
7059 if (!(bb->flags & BB_EXECUTABLE))
7061 if (dump_file && (dump_flags & TDF_DETAILS))
7062 fprintf (dump_file, "Block %d: BB%d found not executable\n",
7063 idx, bb->index);
7064 idx++;
7065 continue;
7068 if (dump_file && (dump_flags & TDF_DETAILS))
7069 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
7070 nblk++;
7071 todo |= process_bb (avail, bb,
7072 rpo_state[idx].visited != 0,
7073 rpo_state[idx].iterate,
7074 iterate, eliminate, do_region, exit_bbs, false);
7075 rpo_state[idx].visited++;
7077 /* Verify if changed values flow over executable outgoing backedges
7078 and those change destination PHI values (that's the thing we
7079 can easily verify). Reduce over all such edges to the farthest
7080 away PHI. */
7081 int iterate_to = -1;
7082 edge_iterator ei;
7083 edge e;
7084 FOR_EACH_EDGE (e, ei, bb->succs)
7085 if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
7086 == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
7087 && rpo_state[bb_to_rpo[e->dest->index]].iterate)
7089 int destidx = bb_to_rpo[e->dest->index];
7090 if (!rpo_state[destidx].visited)
7092 if (dump_file && (dump_flags & TDF_DETAILS))
7093 fprintf (dump_file, "Unvisited destination %d\n",
7094 e->dest->index);
7095 if (iterate_to == -1 || destidx < iterate_to)
7096 iterate_to = destidx;
7097 continue;
7099 if (dump_file && (dump_flags & TDF_DETAILS))
7100 fprintf (dump_file, "Looking for changed values of backedge"
7101 " %d->%d destination PHIs\n",
7102 e->src->index, e->dest->index);
7103 vn_context_bb = e->dest;
7104 gphi_iterator gsi;
7105 for (gsi = gsi_start_phis (e->dest);
7106 !gsi_end_p (gsi); gsi_next (&gsi))
7108 bool inserted = false;
7109 /* While we'd ideally just iterate on value changes
7110 we CSE PHIs and do that even across basic-block
7111 boundaries. So even hashtable state changes can
7112 be important (which is roughly equivalent to
7113 PHI argument value changes). To not excessively
7114 iterate because of that we track whether a PHI
7115 was CSEd to with GF_PLF_1. */
7116 bool phival_changed;
7117 if ((phival_changed = visit_phi (gsi.phi (),
7118 &inserted, false))
7119 || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
7121 if (!phival_changed
7122 && dump_file && (dump_flags & TDF_DETAILS))
7123 fprintf (dump_file, "PHI was CSEd and hashtable "
7124 "state (changed)\n");
7125 if (iterate_to == -1 || destidx < iterate_to)
7126 iterate_to = destidx;
7127 break;
7130 vn_context_bb = NULL;
7132 if (iterate_to != -1)
7134 do_unwind (&rpo_state[iterate_to], iterate_to, avail, bb_to_rpo);
7135 idx = iterate_to;
7136 if (dump_file && (dump_flags & TDF_DETAILS))
7137 fprintf (dump_file, "Iterating to %d BB%d\n",
7138 iterate_to, rpo[iterate_to]);
7139 continue;
7142 idx++;
7144 while (idx < n);
7146 else /* !iterate */
7148 /* Process all blocks greedily with a worklist that enforces RPO
7149 processing of reachable blocks. */
7150 auto_bitmap worklist;
7151 bitmap_set_bit (worklist, 0);
7152 while (!bitmap_empty_p (worklist))
7154 int idx = bitmap_first_set_bit (worklist);
7155 bitmap_clear_bit (worklist, idx);
7156 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
7157 gcc_assert ((bb->flags & BB_EXECUTABLE)
7158 && !rpo_state[idx].visited);
7160 if (dump_file && (dump_flags & TDF_DETAILS))
7161 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
7163 /* When we run into predecessor edges where we cannot trust its
7164 executable state mark them executable so PHI processing will
7165 be conservative.
7166 ??? Do we need to force arguments flowing over that edge
7167 to be varying or will they even always be? */
7168 edge_iterator ei;
7169 edge e;
7170 FOR_EACH_EDGE (e, ei, bb->preds)
7171 if (!(e->flags & EDGE_EXECUTABLE)
7172 && (bb == entry->dest
7173 || (!rpo_state[bb_to_rpo[e->src->index]].visited
7174 && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
7175 >= (int)idx))))
7177 if (dump_file && (dump_flags & TDF_DETAILS))
7178 fprintf (dump_file, "Cannot trust state of predecessor "
7179 "edge %d -> %d, marking executable\n",
7180 e->src->index, e->dest->index);
7181 e->flags |= EDGE_EXECUTABLE;
7184 nblk++;
7185 todo |= process_bb (avail, bb, false, false, false, eliminate,
7186 do_region, exit_bbs,
7187 skip_entry_phis && bb == entry->dest);
7188 rpo_state[idx].visited++;
7190 FOR_EACH_EDGE (e, ei, bb->succs)
7191 if ((e->flags & EDGE_EXECUTABLE)
7192 && e->dest->index != EXIT_BLOCK
7193 && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
7194 && !rpo_state[bb_to_rpo[e->dest->index]].visited)
7195 bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
7199 /* If statistics or dump file active. */
7200 int nex = 0;
7201 unsigned max_visited = 1;
7202 for (int i = 0; i < n; ++i)
7204 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
7205 if (bb->flags & BB_EXECUTABLE)
7206 nex++;
7207 statistics_histogram_event (cfun, "RPO block visited times",
7208 rpo_state[i].visited);
7209 if (rpo_state[i].visited > max_visited)
7210 max_visited = rpo_state[i].visited;
7212 unsigned nvalues = 0, navail = 0;
7213 for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
7214 i != vn_ssa_aux_hash->end (); ++i)
7216 nvalues++;
7217 vn_avail *av = (*i)->avail;
7218 while (av)
7220 navail++;
7221 av = av->next;
7224 statistics_counter_event (cfun, "RPO blocks", n);
7225 statistics_counter_event (cfun, "RPO blocks visited", nblk);
7226 statistics_counter_event (cfun, "RPO blocks executable", nex);
7227 statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
7228 statistics_histogram_event (cfun, "RPO num values", nvalues);
7229 statistics_histogram_event (cfun, "RPO num avail", navail);
7230 statistics_histogram_event (cfun, "RPO num lattice",
7231 vn_ssa_aux_hash->elements ());
7232 if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
7234 fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
7235 " blocks in total discovering %d executable blocks iterating "
7236 "%d.%d times, a block was visited max. %u times\n",
7237 n, nblk, nex,
7238 (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
7239 max_visited);
7240 fprintf (dump_file, "RPO tracked %d values available at %d locations "
7241 "and %" PRIu64 " lattice elements\n",
7242 nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
7245 if (eliminate)
7247 /* When !iterate we already performed elimination during the RPO
7248 walk. */
7249 if (iterate)
7251 /* Elimination for region-based VN needs to be done within the
7252 RPO walk. */
7253 gcc_assert (! do_region);
7254 /* Note we can't use avail.walk here because that gets confused
7255 by the existing availability and it will be less efficient
7256 as well. */
7257 todo |= eliminate_with_rpo_vn (NULL);
7259 else
7260 todo |= avail.eliminate_cleanup (do_region);
7263 vn_valueize = NULL;
7264 rpo_avail = NULL;
7266 XDELETEVEC (bb_to_rpo);
7267 XDELETEVEC (rpo);
7268 XDELETEVEC (rpo_state);
7270 return todo;
7273 /* Region-based entry for RPO VN. Performs value-numbering and elimination
7274 on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
7275 the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
7276 are not considered. */
7278 unsigned
7279 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs)
7281 default_vn_walk_kind = VN_WALKREWRITE;
7282 unsigned todo = do_rpo_vn (fn, entry, exit_bbs, false, true);
7283 free_rpo_vn ();
7284 return todo;
7288 namespace {
7290 const pass_data pass_data_fre =
7292 GIMPLE_PASS, /* type */
7293 "fre", /* name */
7294 OPTGROUP_NONE, /* optinfo_flags */
7295 TV_TREE_FRE, /* tv_id */
7296 ( PROP_cfg | PROP_ssa ), /* properties_required */
7297 0, /* properties_provided */
7298 0, /* properties_destroyed */
7299 0, /* todo_flags_start */
7300 0, /* todo_flags_finish */
7303 class pass_fre : public gimple_opt_pass
7305 public:
7306 pass_fre (gcc::context *ctxt)
7307 : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
7310 /* opt_pass methods: */
7311 opt_pass * clone () { return new pass_fre (m_ctxt); }
7312 void set_pass_param (unsigned int n, bool param)
7314 gcc_assert (n == 0);
7315 may_iterate = param;
7317 virtual bool gate (function *)
7319 return flag_tree_fre != 0 && (may_iterate || optimize > 1);
7321 virtual unsigned int execute (function *);
7323 private:
7324 bool may_iterate;
7325 }; // class pass_fre
7327 unsigned int
7328 pass_fre::execute (function *fun)
7330 unsigned todo = 0;
7332 /* At -O[1g] use the cheap non-iterating mode. */
7333 bool iterate_p = may_iterate && (optimize > 1);
7334 calculate_dominance_info (CDI_DOMINATORS);
7335 if (iterate_p)
7336 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
7338 default_vn_walk_kind = VN_WALKREWRITE;
7339 todo = do_rpo_vn (fun, NULL, NULL, iterate_p, true);
7340 free_rpo_vn ();
7342 if (iterate_p)
7343 loop_optimizer_finalize ();
7345 /* For late FRE after IVOPTs and unrolling, see if we can
7346 remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
7347 if (!may_iterate)
7348 todo |= TODO_update_address_taken;
7350 return todo;
7353 } // anon namespace
7355 gimple_opt_pass *
7356 make_pass_fre (gcc::context *ctxt)
7358 return new pass_fre (ctxt);
7361 #undef BB_EXECUTABLE