2018-09-03 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob1f00335885136f54c7151134b35cd95599275559
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2018 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 "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "ssa.h"
29 #include "expmed.h"
30 #include "insn-config.h"
31 #include "memmodel.h"
32 #include "emit-rtl.h"
33 #include "cgraph.h"
34 #include "gimple-pretty-print.h"
35 #include "alias.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "cfganal.h"
39 #include "tree-inline.h"
40 #include "internal-fn.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimplify.h"
44 #include "flags.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "varasm.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "tree-dfa.h"
52 #include "tree-ssa.h"
53 #include "dumpfile.h"
54 #include "cfgloop.h"
55 #include "params.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-sccvn.h"
73 /* This algorithm is based on the SCC algorithm presented by Keith
74 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
75 (http://citeseer.ist.psu.edu/41805.html). In
76 straight line code, it is equivalent to a regular hash based value
77 numbering that is performed in reverse postorder.
79 For code with cycles, there are two alternatives, both of which
80 require keeping the hashtables separate from the actual list of
81 value numbers for SSA names.
83 1. Iterate value numbering in an RPO walk of the blocks, removing
84 all the entries from the hashtable after each iteration (but
85 keeping the SSA name->value number mapping between iterations).
86 Iterate until it does not change.
88 2. Perform value numbering as part of an SCC walk on the SSA graph,
89 iterating only the cycles in the SSA graph until they do not change
90 (using a separate, optimistic hashtable for value numbering the SCC
91 operands).
93 The second is not just faster in practice (because most SSA graph
94 cycles do not involve all the variables in the graph), it also has
95 some nice properties.
97 One of these nice properties is that when we pop an SCC off the
98 stack, we are guaranteed to have processed all the operands coming from
99 *outside of that SCC*, so we do not need to do anything special to
100 ensure they have value numbers.
102 Another nice property is that the SCC walk is done as part of a DFS
103 of the SSA graph, which makes it easy to perform combining and
104 simplifying operations at the same time.
106 The code below is deliberately written in a way that makes it easy
107 to separate the SCC walk from the other work it does.
109 In order to propagate constants through the code, we track which
110 expressions contain constants, and use those while folding. In
111 theory, we could also track expressions whose value numbers are
112 replaced, in case we end up folding based on expression
113 identities.
115 In order to value number memory, we assign value numbers to vuses.
116 This enables us to note that, for example, stores to the same
117 address of the same value from the same starting memory states are
118 equivalent.
119 TODO:
121 1. We can iterate only the changing portions of the SCC's, but
122 I have not seen an SCC big enough for this to be a win.
123 2. If you differentiate between phi nodes for loops and phi nodes
124 for if-then-else, you can properly consider phi nodes in different
125 blocks for equivalence.
126 3. We could value number vuses in more cases, particularly, whole
127 structure copies.
130 /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
131 #define BB_EXECUTABLE BB_VISITED
133 static tree *last_vuse_ptr;
134 static vn_lookup_kind vn_walk_kind;
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);
313 /* This represents the top of the VN lattice, which is the universal
314 value. */
316 tree VN_TOP;
318 /* Unique counter for our value ids. */
320 static unsigned int next_value_id;
323 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
324 are allocated on an obstack for locality reasons, and to free them
325 without looping over the vec. */
327 struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
329 typedef vn_ssa_aux_t value_type;
330 typedef tree compare_type;
331 static inline hashval_t hash (const value_type &);
332 static inline bool equal (const value_type &, const compare_type &);
333 static inline void mark_deleted (value_type &) {}
334 static inline void mark_empty (value_type &e) { e = NULL; }
335 static inline bool is_deleted (value_type &) { return false; }
336 static inline bool is_empty (value_type &e) { return e == NULL; }
339 hashval_t
340 vn_ssa_aux_hasher::hash (const value_type &entry)
342 return SSA_NAME_VERSION (entry->name);
345 bool
346 vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
348 return name == entry->name;
351 static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
352 typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
353 static struct obstack vn_ssa_aux_obstack;
355 static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
356 static unsigned int vn_nary_length_from_stmt (gimple *);
357 static vn_nary_op_t alloc_vn_nary_op_noinit (unsigned int, obstack *);
358 static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
359 vn_nary_op_table_type *, bool);
360 static void init_vn_nary_op_from_stmt (vn_nary_op_t, gimple *);
361 static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
362 enum tree_code, tree, tree *);
363 static tree vn_lookup_simplify_result (gimple_match_op *);
365 /* Return whether there is value numbering information for a given SSA name. */
367 bool
368 has_VN_INFO (tree name)
370 return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
373 vn_ssa_aux_t
374 VN_INFO (tree name)
376 vn_ssa_aux_t *res
377 = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
378 INSERT);
379 if (*res != NULL)
380 return *res;
382 vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
383 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
384 newinfo->name = name;
385 newinfo->valnum = VN_TOP;
386 /* We are using the visited flag to handle uses with defs not within the
387 region being value-numbered. */
388 newinfo->visited = false;
390 /* Given we create the VN_INFOs on-demand now we have to do initialization
391 different than VN_TOP here. */
392 if (SSA_NAME_IS_DEFAULT_DEF (name))
393 switch (TREE_CODE (SSA_NAME_VAR (name)))
395 case VAR_DECL:
396 /* All undefined vars are VARYING. */
397 newinfo->valnum = name;
398 newinfo->visited = true;
399 break;
401 case PARM_DECL:
402 /* Parameters are VARYING but we can record a condition
403 if we know it is a non-NULL pointer. */
404 newinfo->visited = true;
405 newinfo->valnum = name;
406 if (POINTER_TYPE_P (TREE_TYPE (name))
407 && nonnull_arg_p (SSA_NAME_VAR (name)))
409 tree ops[2];
410 ops[0] = name;
411 ops[1] = build_int_cst (TREE_TYPE (name), 0);
412 vn_nary_op_t nary;
413 /* Allocate from non-unwinding stack. */
414 nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
415 init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
416 boolean_type_node, ops);
417 nary->predicated_values = 0;
418 nary->u.result = boolean_true_node;
419 vn_nary_op_insert_into (nary, valid_info->nary, true);
420 gcc_assert (nary->unwind_to == NULL);
421 /* Also do not link it into the undo chain. */
422 last_inserted_nary = nary->next;
423 nary->next = (vn_nary_op_t)(void *)-1;
424 nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
425 init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
426 boolean_type_node, ops);
427 nary->predicated_values = 0;
428 nary->u.result = boolean_false_node;
429 vn_nary_op_insert_into (nary, valid_info->nary, true);
430 gcc_assert (nary->unwind_to == NULL);
431 last_inserted_nary = nary->next;
432 nary->next = (vn_nary_op_t)(void *)-1;
433 if (dump_file && (dump_flags & TDF_DETAILS))
435 fprintf (dump_file, "Recording ");
436 print_generic_expr (dump_file, name, TDF_SLIM);
437 fprintf (dump_file, " != 0\n");
440 break;
442 case RESULT_DECL:
443 /* If the result is passed by invisible reference the default
444 def is initialized, otherwise it's uninitialized. Still
445 undefined is varying. */
446 newinfo->visited = true;
447 newinfo->valnum = name;
448 break;
450 default:
451 gcc_unreachable ();
453 return newinfo;
456 /* Return the SSA value of X. */
458 inline tree
459 SSA_VAL (tree x, bool *visited = NULL)
461 vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
462 if (visited)
463 *visited = tem && tem->visited;
464 return tem && tem->visited ? tem->valnum : x;
467 /* Return whether X was visited. */
469 inline bool
470 SSA_VISITED (tree x)
472 vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
473 return tem && tem->visited;
476 /* Return the SSA value of the VUSE x, supporting released VDEFs
477 during elimination which will value-number the VDEF to the
478 associated VUSE (but not substitute in the whole lattice). */
480 static inline tree
481 vuse_ssa_val (tree x)
483 if (!x)
484 return NULL_TREE;
488 if (SSA_NAME_IS_DEFAULT_DEF (x))
489 return x;
490 vn_ssa_aux_t tem
491 = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
492 /* For region-based VN this makes walk_non_aliased_vuses stop walking
493 when we are about to look at a def outside of the region. */
494 if (!tem || !tem->visited)
495 return NULL_TREE;
496 gcc_assert (tem->valnum != VN_TOP);
497 x = tem->valnum;
499 while (SSA_NAME_IN_FREE_LIST (x));
501 return x;
505 /* Return the vn_kind the expression computed by the stmt should be
506 associated with. */
508 enum vn_kind
509 vn_get_stmt_kind (gimple *stmt)
511 switch (gimple_code (stmt))
513 case GIMPLE_CALL:
514 return VN_REFERENCE;
515 case GIMPLE_PHI:
516 return VN_PHI;
517 case GIMPLE_ASSIGN:
519 enum tree_code code = gimple_assign_rhs_code (stmt);
520 tree rhs1 = gimple_assign_rhs1 (stmt);
521 switch (get_gimple_rhs_class (code))
523 case GIMPLE_UNARY_RHS:
524 case GIMPLE_BINARY_RHS:
525 case GIMPLE_TERNARY_RHS:
526 return VN_NARY;
527 case GIMPLE_SINGLE_RHS:
528 switch (TREE_CODE_CLASS (code))
530 case tcc_reference:
531 /* VOP-less references can go through unary case. */
532 if ((code == REALPART_EXPR
533 || code == IMAGPART_EXPR
534 || code == VIEW_CONVERT_EXPR
535 || code == BIT_FIELD_REF)
536 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
537 return VN_NARY;
539 /* Fallthrough. */
540 case tcc_declaration:
541 return VN_REFERENCE;
543 case tcc_constant:
544 return VN_CONSTANT;
546 default:
547 if (code == ADDR_EXPR)
548 return (is_gimple_min_invariant (rhs1)
549 ? VN_CONSTANT : VN_REFERENCE);
550 else if (code == CONSTRUCTOR)
551 return VN_NARY;
552 return VN_NONE;
554 default:
555 return VN_NONE;
558 default:
559 return VN_NONE;
563 /* Lookup a value id for CONSTANT and return it. If it does not
564 exist returns 0. */
566 unsigned int
567 get_constant_value_id (tree constant)
569 vn_constant_s **slot;
570 struct vn_constant_s vc;
572 vc.hashcode = vn_hash_constant_with_type (constant);
573 vc.constant = constant;
574 slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
575 if (slot)
576 return (*slot)->value_id;
577 return 0;
580 /* Lookup a value id for CONSTANT, and if it does not exist, create a
581 new one and return it. If it does exist, return it. */
583 unsigned int
584 get_or_alloc_constant_value_id (tree constant)
586 vn_constant_s **slot;
587 struct vn_constant_s vc;
588 vn_constant_t vcp;
590 /* If the hashtable isn't initialized we're not running from PRE and thus
591 do not need value-ids. */
592 if (!constant_to_value_id)
593 return 0;
595 vc.hashcode = vn_hash_constant_with_type (constant);
596 vc.constant = constant;
597 slot = constant_to_value_id->find_slot (&vc, INSERT);
598 if (*slot)
599 return (*slot)->value_id;
601 vcp = XNEW (struct vn_constant_s);
602 vcp->hashcode = vc.hashcode;
603 vcp->constant = constant;
604 vcp->value_id = get_next_value_id ();
605 *slot = vcp;
606 bitmap_set_bit (constant_value_ids, vcp->value_id);
607 return vcp->value_id;
610 /* Return true if V is a value id for a constant. */
612 bool
613 value_id_constant_p (unsigned int v)
615 return bitmap_bit_p (constant_value_ids, v);
618 /* Compute the hash for a reference operand VRO1. */
620 static void
621 vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
623 hstate.add_int (vro1->opcode);
624 if (vro1->op0)
625 inchash::add_expr (vro1->op0, hstate);
626 if (vro1->op1)
627 inchash::add_expr (vro1->op1, hstate);
628 if (vro1->op2)
629 inchash::add_expr (vro1->op2, hstate);
632 /* Compute a hash for the reference operation VR1 and return it. */
634 static hashval_t
635 vn_reference_compute_hash (const vn_reference_t vr1)
637 inchash::hash hstate;
638 hashval_t result;
639 int i;
640 vn_reference_op_t vro;
641 poly_int64 off = -1;
642 bool deref = false;
644 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
646 if (vro->opcode == MEM_REF)
647 deref = true;
648 else if (vro->opcode != ADDR_EXPR)
649 deref = false;
650 if (maybe_ne (vro->off, -1))
652 if (known_eq (off, -1))
653 off = 0;
654 off += vro->off;
656 else
658 if (maybe_ne (off, -1)
659 && maybe_ne (off, 0))
660 hstate.add_poly_int (off);
661 off = -1;
662 if (deref
663 && vro->opcode == ADDR_EXPR)
665 if (vro->op0)
667 tree op = TREE_OPERAND (vro->op0, 0);
668 hstate.add_int (TREE_CODE (op));
669 inchash::add_expr (op, hstate);
672 else
673 vn_reference_op_compute_hash (vro, hstate);
676 result = hstate.end ();
677 /* ??? We would ICE later if we hash instead of adding that in. */
678 if (vr1->vuse)
679 result += SSA_NAME_VERSION (vr1->vuse);
681 return result;
684 /* Return true if reference operations VR1 and VR2 are equivalent. This
685 means they have the same set of operands and vuses. */
687 bool
688 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
690 unsigned i, j;
692 /* Early out if this is not a hash collision. */
693 if (vr1->hashcode != vr2->hashcode)
694 return false;
696 /* The VOP needs to be the same. */
697 if (vr1->vuse != vr2->vuse)
698 return false;
700 /* If the operands are the same we are done. */
701 if (vr1->operands == vr2->operands)
702 return true;
704 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
705 return false;
707 if (INTEGRAL_TYPE_P (vr1->type)
708 && INTEGRAL_TYPE_P (vr2->type))
710 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
711 return false;
713 else if (INTEGRAL_TYPE_P (vr1->type)
714 && (TYPE_PRECISION (vr1->type)
715 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
716 return false;
717 else if (INTEGRAL_TYPE_P (vr2->type)
718 && (TYPE_PRECISION (vr2->type)
719 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
720 return false;
722 i = 0;
723 j = 0;
726 poly_int64 off1 = 0, off2 = 0;
727 vn_reference_op_t vro1, vro2;
728 vn_reference_op_s tem1, tem2;
729 bool deref1 = false, deref2 = false;
730 for (; vr1->operands.iterate (i, &vro1); i++)
732 if (vro1->opcode == MEM_REF)
733 deref1 = true;
734 /* Do not look through a storage order barrier. */
735 else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
736 return false;
737 if (known_eq (vro1->off, -1))
738 break;
739 off1 += vro1->off;
741 for (; vr2->operands.iterate (j, &vro2); j++)
743 if (vro2->opcode == MEM_REF)
744 deref2 = true;
745 /* Do not look through a storage order barrier. */
746 else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
747 return false;
748 if (known_eq (vro2->off, -1))
749 break;
750 off2 += vro2->off;
752 if (maybe_ne (off1, off2))
753 return false;
754 if (deref1 && vro1->opcode == ADDR_EXPR)
756 memset (&tem1, 0, sizeof (tem1));
757 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
758 tem1.type = TREE_TYPE (tem1.op0);
759 tem1.opcode = TREE_CODE (tem1.op0);
760 vro1 = &tem1;
761 deref1 = false;
763 if (deref2 && vro2->opcode == ADDR_EXPR)
765 memset (&tem2, 0, sizeof (tem2));
766 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
767 tem2.type = TREE_TYPE (tem2.op0);
768 tem2.opcode = TREE_CODE (tem2.op0);
769 vro2 = &tem2;
770 deref2 = false;
772 if (deref1 != deref2)
773 return false;
774 if (!vn_reference_op_eq (vro1, vro2))
775 return false;
776 ++j;
777 ++i;
779 while (vr1->operands.length () != i
780 || vr2->operands.length () != j);
782 return true;
785 /* Copy the operations present in load/store REF into RESULT, a vector of
786 vn_reference_op_s's. */
788 static void
789 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
791 if (TREE_CODE (ref) == TARGET_MEM_REF)
793 vn_reference_op_s temp;
795 result->reserve (3);
797 memset (&temp, 0, sizeof (temp));
798 temp.type = TREE_TYPE (ref);
799 temp.opcode = TREE_CODE (ref);
800 temp.op0 = TMR_INDEX (ref);
801 temp.op1 = TMR_STEP (ref);
802 temp.op2 = TMR_OFFSET (ref);
803 temp.off = -1;
804 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
805 temp.base = MR_DEPENDENCE_BASE (ref);
806 result->quick_push (temp);
808 memset (&temp, 0, sizeof (temp));
809 temp.type = NULL_TREE;
810 temp.opcode = ERROR_MARK;
811 temp.op0 = TMR_INDEX2 (ref);
812 temp.off = -1;
813 result->quick_push (temp);
815 memset (&temp, 0, sizeof (temp));
816 temp.type = NULL_TREE;
817 temp.opcode = TREE_CODE (TMR_BASE (ref));
818 temp.op0 = TMR_BASE (ref);
819 temp.off = -1;
820 result->quick_push (temp);
821 return;
824 /* For non-calls, store the information that makes up the address. */
825 tree orig = ref;
826 while (ref)
828 vn_reference_op_s temp;
830 memset (&temp, 0, sizeof (temp));
831 temp.type = TREE_TYPE (ref);
832 temp.opcode = TREE_CODE (ref);
833 temp.off = -1;
835 switch (temp.opcode)
837 case MODIFY_EXPR:
838 temp.op0 = TREE_OPERAND (ref, 1);
839 break;
840 case WITH_SIZE_EXPR:
841 temp.op0 = TREE_OPERAND (ref, 1);
842 temp.off = 0;
843 break;
844 case MEM_REF:
845 /* The base address gets its own vn_reference_op_s structure. */
846 temp.op0 = TREE_OPERAND (ref, 1);
847 if (!mem_ref_offset (ref).to_shwi (&temp.off))
848 temp.off = -1;
849 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
850 temp.base = MR_DEPENDENCE_BASE (ref);
851 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
852 break;
853 case BIT_FIELD_REF:
854 /* Record bits, position and storage order. */
855 temp.op0 = TREE_OPERAND (ref, 1);
856 temp.op1 = TREE_OPERAND (ref, 2);
857 if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
858 temp.off = -1;
859 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
860 break;
861 case COMPONENT_REF:
862 /* The field decl is enough to unambiguously specify the field,
863 a matching type is not necessary and a mismatching type
864 is always a spurious difference. */
865 temp.type = NULL_TREE;
866 temp.op0 = TREE_OPERAND (ref, 1);
867 temp.op1 = TREE_OPERAND (ref, 2);
869 tree this_offset = component_ref_field_offset (ref);
870 if (this_offset
871 && poly_int_tree_p (this_offset))
873 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
874 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
876 poly_offset_int off
877 = (wi::to_poly_offset (this_offset)
878 + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
879 /* Probibit value-numbering zero offset components
880 of addresses the same before the pass folding
881 __builtin_object_size had a chance to run
882 (checking cfun->after_inlining does the
883 trick here). */
884 if (TREE_CODE (orig) != ADDR_EXPR
885 || maybe_ne (off, 0)
886 || cfun->after_inlining)
887 off.to_shwi (&temp.off);
891 break;
892 case ARRAY_RANGE_REF:
893 case ARRAY_REF:
895 tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
896 /* Record index as operand. */
897 temp.op0 = TREE_OPERAND (ref, 1);
898 /* Always record lower bounds and element size. */
899 temp.op1 = array_ref_low_bound (ref);
900 /* But record element size in units of the type alignment. */
901 temp.op2 = TREE_OPERAND (ref, 3);
902 temp.align = eltype->type_common.align;
903 if (! temp.op2)
904 temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
905 size_int (TYPE_ALIGN_UNIT (eltype)));
906 if (poly_int_tree_p (temp.op0)
907 && poly_int_tree_p (temp.op1)
908 && TREE_CODE (temp.op2) == INTEGER_CST)
910 poly_offset_int off = ((wi::to_poly_offset (temp.op0)
911 - wi::to_poly_offset (temp.op1))
912 * wi::to_offset (temp.op2)
913 * vn_ref_op_align_unit (&temp));
914 off.to_shwi (&temp.off);
917 break;
918 case VAR_DECL:
919 if (DECL_HARD_REGISTER (ref))
921 temp.op0 = ref;
922 break;
924 /* Fallthru. */
925 case PARM_DECL:
926 case CONST_DECL:
927 case RESULT_DECL:
928 /* Canonicalize decls to MEM[&decl] which is what we end up with
929 when valueizing MEM[ptr] with ptr = &decl. */
930 temp.opcode = MEM_REF;
931 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
932 temp.off = 0;
933 result->safe_push (temp);
934 temp.opcode = ADDR_EXPR;
935 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
936 temp.type = TREE_TYPE (temp.op0);
937 temp.off = -1;
938 break;
939 case STRING_CST:
940 case INTEGER_CST:
941 case COMPLEX_CST:
942 case VECTOR_CST:
943 case REAL_CST:
944 case FIXED_CST:
945 case CONSTRUCTOR:
946 case SSA_NAME:
947 temp.op0 = ref;
948 break;
949 case ADDR_EXPR:
950 if (is_gimple_min_invariant (ref))
952 temp.op0 = ref;
953 break;
955 break;
956 /* These are only interesting for their operands, their
957 existence, and their type. They will never be the last
958 ref in the chain of references (IE they require an
959 operand), so we don't have to put anything
960 for op* as it will be handled by the iteration */
961 case REALPART_EXPR:
962 temp.off = 0;
963 break;
964 case VIEW_CONVERT_EXPR:
965 temp.off = 0;
966 temp.reverse = storage_order_barrier_p (ref);
967 break;
968 case IMAGPART_EXPR:
969 /* This is only interesting for its constant offset. */
970 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
971 break;
972 default:
973 gcc_unreachable ();
975 result->safe_push (temp);
977 if (REFERENCE_CLASS_P (ref)
978 || TREE_CODE (ref) == MODIFY_EXPR
979 || TREE_CODE (ref) == WITH_SIZE_EXPR
980 || (TREE_CODE (ref) == ADDR_EXPR
981 && !is_gimple_min_invariant (ref)))
982 ref = TREE_OPERAND (ref, 0);
983 else
984 ref = NULL_TREE;
988 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
989 operands in *OPS, the reference alias set SET and the reference type TYPE.
990 Return true if something useful was produced. */
992 bool
993 ao_ref_init_from_vn_reference (ao_ref *ref,
994 alias_set_type set, tree type,
995 vec<vn_reference_op_s> ops)
997 vn_reference_op_t op;
998 unsigned i;
999 tree base = NULL_TREE;
1000 tree *op0_p = &base;
1001 poly_offset_int offset = 0;
1002 poly_offset_int max_size;
1003 poly_offset_int size = -1;
1004 tree size_tree = NULL_TREE;
1005 alias_set_type base_alias_set = -1;
1007 /* First get the final access size from just the outermost expression. */
1008 op = &ops[0];
1009 if (op->opcode == COMPONENT_REF)
1010 size_tree = DECL_SIZE (op->op0);
1011 else if (op->opcode == BIT_FIELD_REF)
1012 size_tree = op->op0;
1013 else
1015 machine_mode mode = TYPE_MODE (type);
1016 if (mode == BLKmode)
1017 size_tree = TYPE_SIZE (type);
1018 else
1019 size = GET_MODE_BITSIZE (mode);
1021 if (size_tree != NULL_TREE
1022 && poly_int_tree_p (size_tree))
1023 size = wi::to_poly_offset (size_tree);
1025 /* Initially, maxsize is the same as the accessed element size.
1026 In the following it will only grow (or become -1). */
1027 max_size = size;
1029 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1030 and find the ultimate containing object. */
1031 FOR_EACH_VEC_ELT (ops, i, op)
1033 switch (op->opcode)
1035 /* These may be in the reference ops, but we cannot do anything
1036 sensible with them here. */
1037 case ADDR_EXPR:
1038 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1039 if (base != NULL_TREE
1040 && TREE_CODE (base) == MEM_REF
1041 && op->op0
1042 && DECL_P (TREE_OPERAND (op->op0, 0)))
1044 vn_reference_op_t pop = &ops[i-1];
1045 base = TREE_OPERAND (op->op0, 0);
1046 if (known_eq (pop->off, -1))
1048 max_size = -1;
1049 offset = 0;
1051 else
1052 offset += pop->off * BITS_PER_UNIT;
1053 op0_p = NULL;
1054 break;
1056 /* Fallthru. */
1057 case CALL_EXPR:
1058 return false;
1060 /* Record the base objects. */
1061 case MEM_REF:
1062 base_alias_set = get_deref_alias_set (op->op0);
1063 *op0_p = build2 (MEM_REF, op->type,
1064 NULL_TREE, op->op0);
1065 MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1066 MR_DEPENDENCE_BASE (*op0_p) = op->base;
1067 op0_p = &TREE_OPERAND (*op0_p, 0);
1068 break;
1070 case VAR_DECL:
1071 case PARM_DECL:
1072 case RESULT_DECL:
1073 case SSA_NAME:
1074 *op0_p = op->op0;
1075 op0_p = NULL;
1076 break;
1078 /* And now the usual component-reference style ops. */
1079 case BIT_FIELD_REF:
1080 offset += wi::to_poly_offset (op->op1);
1081 break;
1083 case COMPONENT_REF:
1085 tree field = op->op0;
1086 /* We do not have a complete COMPONENT_REF tree here so we
1087 cannot use component_ref_field_offset. Do the interesting
1088 parts manually. */
1089 tree this_offset = DECL_FIELD_OFFSET (field);
1091 if (op->op1 || !poly_int_tree_p (this_offset))
1092 max_size = -1;
1093 else
1095 poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1096 << LOG2_BITS_PER_UNIT);
1097 woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1098 offset += woffset;
1100 break;
1103 case ARRAY_RANGE_REF:
1104 case ARRAY_REF:
1105 /* We recorded the lower bound and the element size. */
1106 if (!poly_int_tree_p (op->op0)
1107 || !poly_int_tree_p (op->op1)
1108 || TREE_CODE (op->op2) != INTEGER_CST)
1109 max_size = -1;
1110 else
1112 poly_offset_int woffset
1113 = wi::sext (wi::to_poly_offset (op->op0)
1114 - wi::to_poly_offset (op->op1),
1115 TYPE_PRECISION (TREE_TYPE (op->op0)));
1116 woffset *= wi::to_offset (op->op2) * vn_ref_op_align_unit (op);
1117 woffset <<= LOG2_BITS_PER_UNIT;
1118 offset += woffset;
1120 break;
1122 case REALPART_EXPR:
1123 break;
1125 case IMAGPART_EXPR:
1126 offset += size;
1127 break;
1129 case VIEW_CONVERT_EXPR:
1130 break;
1132 case STRING_CST:
1133 case INTEGER_CST:
1134 case COMPLEX_CST:
1135 case VECTOR_CST:
1136 case REAL_CST:
1137 case CONSTRUCTOR:
1138 case CONST_DECL:
1139 return false;
1141 default:
1142 return false;
1146 if (base == NULL_TREE)
1147 return false;
1149 ref->ref = NULL_TREE;
1150 ref->base = base;
1151 ref->ref_alias_set = set;
1152 if (base_alias_set != -1)
1153 ref->base_alias_set = base_alias_set;
1154 else
1155 ref->base_alias_set = get_alias_set (base);
1156 /* We discount volatiles from value-numbering elsewhere. */
1157 ref->volatile_p = false;
1159 if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1161 ref->offset = 0;
1162 ref->size = -1;
1163 ref->max_size = -1;
1164 return true;
1167 if (!offset.to_shwi (&ref->offset))
1169 ref->offset = 0;
1170 ref->max_size = -1;
1171 return true;
1174 if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1175 ref->max_size = -1;
1177 return true;
1180 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1181 vn_reference_op_s's. */
1183 static void
1184 copy_reference_ops_from_call (gcall *call,
1185 vec<vn_reference_op_s> *result)
1187 vn_reference_op_s temp;
1188 unsigned i;
1189 tree lhs = gimple_call_lhs (call);
1190 int lr;
1192 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1193 different. By adding the lhs here in the vector, we ensure that the
1194 hashcode is different, guaranteeing a different value number. */
1195 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1197 memset (&temp, 0, sizeof (temp));
1198 temp.opcode = MODIFY_EXPR;
1199 temp.type = TREE_TYPE (lhs);
1200 temp.op0 = lhs;
1201 temp.off = -1;
1202 result->safe_push (temp);
1205 /* Copy the type, opcode, function, static chain and EH region, if any. */
1206 memset (&temp, 0, sizeof (temp));
1207 temp.type = gimple_call_return_type (call);
1208 temp.opcode = CALL_EXPR;
1209 temp.op0 = gimple_call_fn (call);
1210 temp.op1 = gimple_call_chain (call);
1211 if (stmt_could_throw_p (call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1212 temp.op2 = size_int (lr);
1213 temp.off = -1;
1214 result->safe_push (temp);
1216 /* Copy the call arguments. As they can be references as well,
1217 just chain them together. */
1218 for (i = 0; i < gimple_call_num_args (call); ++i)
1220 tree callarg = gimple_call_arg (call, i);
1221 copy_reference_ops_from_ref (callarg, result);
1225 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1226 *I_P to point to the last element of the replacement. */
1227 static bool
1228 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1229 unsigned int *i_p)
1231 unsigned int i = *i_p;
1232 vn_reference_op_t op = &(*ops)[i];
1233 vn_reference_op_t mem_op = &(*ops)[i - 1];
1234 tree addr_base;
1235 poly_int64 addr_offset = 0;
1237 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1238 from .foo.bar to the preceding MEM_REF offset and replace the
1239 address with &OBJ. */
1240 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1241 &addr_offset);
1242 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1243 if (addr_base != TREE_OPERAND (op->op0, 0))
1245 poly_offset_int off
1246 = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1247 SIGNED)
1248 + addr_offset);
1249 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1250 op->op0 = build_fold_addr_expr (addr_base);
1251 if (tree_fits_shwi_p (mem_op->op0))
1252 mem_op->off = tree_to_shwi (mem_op->op0);
1253 else
1254 mem_op->off = -1;
1255 return true;
1257 return false;
1260 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1261 *I_P to point to the last element of the replacement. */
1262 static bool
1263 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1264 unsigned int *i_p)
1266 unsigned int i = *i_p;
1267 vn_reference_op_t op = &(*ops)[i];
1268 vn_reference_op_t mem_op = &(*ops)[i - 1];
1269 gimple *def_stmt;
1270 enum tree_code code;
1271 poly_offset_int off;
1273 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1274 if (!is_gimple_assign (def_stmt))
1275 return false;
1277 code = gimple_assign_rhs_code (def_stmt);
1278 if (code != ADDR_EXPR
1279 && code != POINTER_PLUS_EXPR)
1280 return false;
1282 off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1284 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1285 from .foo.bar to the preceding MEM_REF offset and replace the
1286 address with &OBJ. */
1287 if (code == ADDR_EXPR)
1289 tree addr, addr_base;
1290 poly_int64 addr_offset;
1292 addr = gimple_assign_rhs1 (def_stmt);
1293 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1294 &addr_offset);
1295 /* If that didn't work because the address isn't invariant propagate
1296 the reference tree from the address operation in case the current
1297 dereference isn't offsetted. */
1298 if (!addr_base
1299 && *i_p == ops->length () - 1
1300 && known_eq (off, 0)
1301 /* This makes us disable this transform for PRE where the
1302 reference ops might be also used for code insertion which
1303 is invalid. */
1304 && default_vn_walk_kind == VN_WALKREWRITE)
1306 auto_vec<vn_reference_op_s, 32> tem;
1307 copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1308 /* Make sure to preserve TBAA info. The only objects not
1309 wrapped in MEM_REFs that can have their address taken are
1310 STRING_CSTs. */
1311 if (tem.length () >= 2
1312 && tem[tem.length () - 2].opcode == MEM_REF)
1314 vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1315 new_mem_op->op0
1316 = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1317 wi::to_poly_wide (new_mem_op->op0));
1319 else
1320 gcc_assert (tem.last ().opcode == STRING_CST);
1321 ops->pop ();
1322 ops->pop ();
1323 ops->safe_splice (tem);
1324 --*i_p;
1325 return true;
1327 if (!addr_base
1328 || TREE_CODE (addr_base) != MEM_REF
1329 || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1330 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base, 0))))
1331 return false;
1333 off += addr_offset;
1334 off += mem_ref_offset (addr_base);
1335 op->op0 = TREE_OPERAND (addr_base, 0);
1337 else
1339 tree ptr, ptroff;
1340 ptr = gimple_assign_rhs1 (def_stmt);
1341 ptroff = gimple_assign_rhs2 (def_stmt);
1342 if (TREE_CODE (ptr) != SSA_NAME
1343 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1344 /* Make sure to not endlessly recurse.
1345 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1346 happen when we value-number a PHI to its backedge value. */
1347 || SSA_VAL (ptr) == op->op0
1348 || !poly_int_tree_p (ptroff))
1349 return false;
1351 off += wi::to_poly_offset (ptroff);
1352 op->op0 = ptr;
1355 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1356 if (tree_fits_shwi_p (mem_op->op0))
1357 mem_op->off = tree_to_shwi (mem_op->op0);
1358 else
1359 mem_op->off = -1;
1360 /* ??? Can end up with endless recursion here!?
1361 gcc.c-torture/execute/strcmp-1.c */
1362 if (TREE_CODE (op->op0) == SSA_NAME)
1363 op->op0 = SSA_VAL (op->op0);
1364 if (TREE_CODE (op->op0) != SSA_NAME)
1365 op->opcode = TREE_CODE (op->op0);
1367 /* And recurse. */
1368 if (TREE_CODE (op->op0) == SSA_NAME)
1369 vn_reference_maybe_forwprop_address (ops, i_p);
1370 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1371 vn_reference_fold_indirect (ops, i_p);
1372 return true;
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;
1667 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1668 with the current VUSE and performs the expression lookup. */
1670 static void *
1671 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1672 unsigned int cnt, void *vr_)
1674 vn_reference_t vr = (vn_reference_t)vr_;
1675 vn_reference_s **slot;
1676 hashval_t hash;
1678 /* This bounds the stmt walks we perform on reference lookups
1679 to O(1) instead of O(N) where N is the number of dominating
1680 stores. */
1681 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1682 return (void *)-1;
1684 if (last_vuse_ptr)
1685 *last_vuse_ptr = vuse;
1687 /* Fixup vuse and hash. */
1688 if (vr->vuse)
1689 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1690 vr->vuse = vuse_ssa_val (vuse);
1691 if (vr->vuse)
1692 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1694 hash = vr->hashcode;
1695 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1696 if (slot)
1697 return *slot;
1699 return NULL;
1702 /* Lookup an existing or insert a new vn_reference entry into the
1703 value table for the VUSE, SET, TYPE, OPERANDS reference which
1704 has the value VALUE which is either a constant or an SSA name. */
1706 static vn_reference_t
1707 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1708 alias_set_type set,
1709 tree type,
1710 vec<vn_reference_op_s,
1711 va_heap> operands,
1712 tree value)
1714 vn_reference_s vr1;
1715 vn_reference_t result;
1716 unsigned value_id;
1717 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1718 vr1.operands = operands;
1719 vr1.type = type;
1720 vr1.set = set;
1721 vr1.hashcode = vn_reference_compute_hash (&vr1);
1722 if (vn_reference_lookup_1 (&vr1, &result))
1723 return result;
1724 if (TREE_CODE (value) == SSA_NAME)
1725 value_id = VN_INFO (value)->value_id;
1726 else
1727 value_id = get_or_alloc_constant_value_id (value);
1728 return vn_reference_insert_pieces (vuse, set, type,
1729 operands.copy (), value, value_id);
1732 /* Return a value-number for RCODE OPS... either by looking up an existing
1733 value-number for the simplified result or by inserting the operation if
1734 INSERT is true. */
1736 static tree
1737 vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert)
1739 tree result = NULL_TREE;
1740 /* We will be creating a value number for
1741 RCODE (OPS...).
1742 So first simplify and lookup this expression to see if it
1743 is already available. */
1744 mprts_hook = vn_lookup_simplify_result;
1745 bool res = false;
1746 switch (TREE_CODE_LENGTH ((tree_code) res_op->code))
1748 case 1:
1749 res = gimple_resimplify1 (NULL, res_op, vn_valueize);
1750 break;
1751 case 2:
1752 res = gimple_resimplify2 (NULL, res_op, vn_valueize);
1753 break;
1754 case 3:
1755 res = gimple_resimplify3 (NULL, res_op, vn_valueize);
1756 break;
1758 mprts_hook = NULL;
1759 gimple *new_stmt = NULL;
1760 if (res
1761 && gimple_simplified_result_is_gimple_val (res_op))
1763 /* The expression is already available. */
1764 result = res_op->ops[0];
1765 /* Valueize it, simplification returns sth in AVAIL only. */
1766 if (TREE_CODE (result) == SSA_NAME)
1767 result = SSA_VAL (result);
1769 else
1771 tree val = vn_lookup_simplify_result (res_op);
1772 if (!val && insert)
1774 gimple_seq stmts = NULL;
1775 result = maybe_push_res_to_seq (res_op, &stmts);
1776 if (result)
1778 gcc_assert (gimple_seq_singleton_p (stmts));
1779 new_stmt = gimple_seq_first_stmt (stmts);
1782 else
1783 /* The expression is already available. */
1784 result = val;
1786 if (new_stmt)
1788 /* The expression is not yet available, value-number lhs to
1789 the new SSA_NAME we created. */
1790 /* Initialize value-number information properly. */
1791 vn_ssa_aux_t result_info = VN_INFO (result);
1792 result_info->valnum = result;
1793 result_info->value_id = get_next_value_id ();
1794 result_info->visited = 1;
1795 gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
1796 new_stmt);
1797 result_info->needs_insertion = true;
1798 /* ??? PRE phi-translation inserts NARYs without corresponding
1799 SSA name result. Re-use those but set their result according
1800 to the stmt we just built. */
1801 vn_nary_op_t nary = NULL;
1802 vn_nary_op_lookup_stmt (new_stmt, &nary);
1803 if (nary)
1805 gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
1806 nary->u.result = gimple_assign_lhs (new_stmt);
1808 /* As all "inserted" statements are singleton SCCs, insert
1809 to the valid table. This is strictly needed to
1810 avoid re-generating new value SSA_NAMEs for the same
1811 expression during SCC iteration over and over (the
1812 optimistic table gets cleared after each iteration).
1813 We do not need to insert into the optimistic table, as
1814 lookups there will fall back to the valid table. */
1815 else
1817 unsigned int length = vn_nary_length_from_stmt (new_stmt);
1818 vn_nary_op_t vno1
1819 = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
1820 vno1->value_id = result_info->value_id;
1821 vno1->length = length;
1822 vno1->predicated_values = 0;
1823 vno1->u.result = result;
1824 init_vn_nary_op_from_stmt (vno1, new_stmt);
1825 vn_nary_op_insert_into (vno1, valid_info->nary, true);
1826 /* Also do not link it into the undo chain. */
1827 last_inserted_nary = vno1->next;
1828 vno1->next = (vn_nary_op_t)(void *)-1;
1830 if (dump_file && (dump_flags & TDF_DETAILS))
1832 fprintf (dump_file, "Inserting name ");
1833 print_generic_expr (dump_file, result);
1834 fprintf (dump_file, " for expression ");
1835 print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
1836 fprintf (dump_file, "\n");
1839 return result;
1842 /* Return a value-number for RCODE OPS... either by looking up an existing
1843 value-number for the simplified result or by inserting the operation. */
1845 static tree
1846 vn_nary_build_or_lookup (gimple_match_op *res_op)
1848 return vn_nary_build_or_lookup_1 (res_op, true);
1851 /* Try to simplify the expression RCODE OPS... of type TYPE and return
1852 its value if present. */
1854 tree
1855 vn_nary_simplify (vn_nary_op_t nary)
1857 if (nary->length > gimple_match_op::MAX_NUM_OPS)
1858 return NULL_TREE;
1859 gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
1860 nary->type, nary->length);
1861 memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
1862 return vn_nary_build_or_lookup_1 (&op, false);
1865 basic_block vn_context_bb;
1867 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1868 from the statement defining VUSE and if not successful tries to
1869 translate *REFP and VR_ through an aggregate copy at the definition
1870 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
1871 of *REF and *VR. If only disambiguation was performed then
1872 *DISAMBIGUATE_ONLY is set to true. */
1874 static void *
1875 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1876 bool *disambiguate_only)
1878 vn_reference_t vr = (vn_reference_t)vr_;
1879 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
1880 tree base = ao_ref_base (ref);
1881 HOST_WIDE_INT offseti, maxsizei;
1882 static vec<vn_reference_op_s> lhs_ops;
1883 ao_ref lhs_ref;
1884 bool lhs_ref_ok = false;
1885 poly_int64 copy_size;
1887 /* First try to disambiguate after value-replacing in the definitions LHS. */
1888 if (is_gimple_assign (def_stmt))
1890 tree lhs = gimple_assign_lhs (def_stmt);
1891 bool valueized_anything = false;
1892 /* Avoid re-allocation overhead. */
1893 lhs_ops.truncate (0);
1894 basic_block saved_rpo_bb = vn_context_bb;
1895 vn_context_bb = gimple_bb (def_stmt);
1896 copy_reference_ops_from_ref (lhs, &lhs_ops);
1897 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything, true);
1898 vn_context_bb = saved_rpo_bb;
1899 if (valueized_anything)
1901 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1902 get_alias_set (lhs),
1903 TREE_TYPE (lhs), lhs_ops);
1904 if (lhs_ref_ok
1905 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1907 *disambiguate_only = true;
1908 return NULL;
1911 else
1913 ao_ref_init (&lhs_ref, lhs);
1914 lhs_ref_ok = true;
1917 /* If we reach a clobbering statement try to skip it and see if
1918 we find a VN result with exactly the same value as the
1919 possible clobber. In this case we can ignore the clobber
1920 and return the found value.
1921 Note that we don't need to worry about partial overlapping
1922 accesses as we then can use TBAA to disambiguate against the
1923 clobbering statement when looking up a load (thus the
1924 VN_WALKREWRITE guard). */
1925 if (vn_walk_kind == VN_WALKREWRITE
1926 && is_gimple_reg_type (TREE_TYPE (lhs))
1927 && types_compatible_p (TREE_TYPE (lhs), vr->type))
1929 tree *saved_last_vuse_ptr = last_vuse_ptr;
1930 /* Do not update last_vuse_ptr in vn_reference_lookup_2. */
1931 last_vuse_ptr = NULL;
1932 tree saved_vuse = vr->vuse;
1933 hashval_t saved_hashcode = vr->hashcode;
1934 void *res = vn_reference_lookup_2 (ref,
1935 gimple_vuse (def_stmt), 0, vr);
1936 /* Need to restore vr->vuse and vr->hashcode. */
1937 vr->vuse = saved_vuse;
1938 vr->hashcode = saved_hashcode;
1939 last_vuse_ptr = saved_last_vuse_ptr;
1940 if (res && res != (void *)-1)
1942 vn_reference_t vnresult = (vn_reference_t) res;
1943 if (vnresult->result
1944 && operand_equal_p (vnresult->result,
1945 gimple_assign_rhs1 (def_stmt), 0))
1946 return res;
1950 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1951 && gimple_call_num_args (def_stmt) <= 4)
1953 /* For builtin calls valueize its arguments and call the
1954 alias oracle again. Valueization may improve points-to
1955 info of pointers and constify size and position arguments.
1956 Originally this was motivated by PR61034 which has
1957 conditional calls to free falsely clobbering ref because
1958 of imprecise points-to info of the argument. */
1959 tree oldargs[4];
1960 bool valueized_anything = false;
1961 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1963 oldargs[i] = gimple_call_arg (def_stmt, i);
1964 tree val = vn_valueize (oldargs[i]);
1965 if (val != oldargs[i])
1967 gimple_call_set_arg (def_stmt, i, val);
1968 valueized_anything = true;
1971 if (valueized_anything)
1973 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
1974 ref);
1975 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1976 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1977 if (!res)
1979 *disambiguate_only = true;
1980 return NULL;
1985 if (*disambiguate_only)
1986 return (void *)-1;
1988 /* If we cannot constrain the size of the reference we cannot
1989 test if anything kills it. */
1990 if (!ref->max_size_known_p ())
1991 return (void *)-1;
1993 poly_int64 offset = ref->offset;
1994 poly_int64 maxsize = ref->max_size;
1996 /* We can't deduce anything useful from clobbers. */
1997 if (gimple_clobber_p (def_stmt))
1998 return (void *)-1;
2000 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2001 from that definition.
2002 1) Memset. */
2003 if (is_gimple_reg_type (vr->type)
2004 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2005 && (integer_zerop (gimple_call_arg (def_stmt, 1))
2006 || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2007 || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2008 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
2009 && offset.is_constant (&offseti)
2010 && offseti % BITS_PER_UNIT == 0))
2011 && poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2012 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2013 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2015 tree base2;
2016 poly_int64 offset2, size2, maxsize2;
2017 bool reverse;
2018 tree ref2 = gimple_call_arg (def_stmt, 0);
2019 if (TREE_CODE (ref2) == SSA_NAME)
2021 ref2 = SSA_VAL (ref2);
2022 if (TREE_CODE (ref2) == SSA_NAME
2023 && (TREE_CODE (base) != MEM_REF
2024 || TREE_OPERAND (base, 0) != ref2))
2026 gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2027 if (gimple_assign_single_p (def_stmt)
2028 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2029 ref2 = gimple_assign_rhs1 (def_stmt);
2032 if (TREE_CODE (ref2) == ADDR_EXPR)
2034 ref2 = TREE_OPERAND (ref2, 0);
2035 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
2036 &reverse);
2037 if (!known_size_p (maxsize2)
2038 || !known_eq (maxsize2, size2)
2039 || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
2040 return (void *)-1;
2042 else if (TREE_CODE (ref2) == SSA_NAME)
2044 poly_int64 soff;
2045 if (TREE_CODE (base) != MEM_REF
2046 || !(mem_ref_offset (base) << LOG2_BITS_PER_UNIT).to_shwi (&soff))
2047 return (void *)-1;
2048 offset += soff;
2049 offset2 = 0;
2050 if (TREE_OPERAND (base, 0) != ref2)
2052 gimple *def = SSA_NAME_DEF_STMT (ref2);
2053 if (is_gimple_assign (def)
2054 && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
2055 && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
2056 && poly_int_tree_p (gimple_assign_rhs2 (def))
2057 && (wi::to_poly_offset (gimple_assign_rhs2 (def))
2058 << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
2060 ref2 = gimple_assign_rhs1 (def);
2061 if (TREE_CODE (ref2) == SSA_NAME)
2062 ref2 = SSA_VAL (ref2);
2064 else
2065 return (void *)-1;
2068 else
2069 return (void *)-1;
2070 tree len = gimple_call_arg (def_stmt, 2);
2071 if (known_subrange_p (offset, maxsize, offset2,
2072 wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
2074 tree val;
2075 if (integer_zerop (gimple_call_arg (def_stmt, 1)))
2076 val = build_zero_cst (vr->type);
2077 else if (INTEGRAL_TYPE_P (vr->type)
2078 && known_eq (ref->size, 8))
2080 gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
2081 vr->type, gimple_call_arg (def_stmt, 1));
2082 val = vn_nary_build_or_lookup (&res_op);
2083 if (!val
2084 || (TREE_CODE (val) == SSA_NAME
2085 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
2086 return (void *)-1;
2088 else
2090 unsigned len = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type));
2091 unsigned char *buf = XALLOCAVEC (unsigned char, len);
2092 memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
2093 len);
2094 val = native_interpret_expr (vr->type, buf, len);
2095 if (!val)
2096 return (void *)-1;
2098 return vn_reference_lookup_or_insert_for_pieces
2099 (vuse, vr->set, vr->type, vr->operands, val);
2103 /* 2) Assignment from an empty CONSTRUCTOR. */
2104 else if (is_gimple_reg_type (vr->type)
2105 && gimple_assign_single_p (def_stmt)
2106 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
2107 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
2109 tree base2;
2110 poly_int64 offset2, size2, maxsize2;
2111 bool reverse;
2112 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
2113 &offset2, &size2, &maxsize2, &reverse);
2114 if (known_size_p (maxsize2)
2115 && operand_equal_p (base, base2, 0)
2116 && known_subrange_p (offset, maxsize, offset2, size2))
2118 tree val = build_zero_cst (vr->type);
2119 return vn_reference_lookup_or_insert_for_pieces
2120 (vuse, vr->set, vr->type, vr->operands, val);
2124 /* 3) Assignment from a constant. We can use folds native encode/interpret
2125 routines to extract the assigned bits. */
2126 else if (known_eq (ref->size, maxsize)
2127 && is_gimple_reg_type (vr->type)
2128 && !contains_storage_order_barrier_p (vr->operands)
2129 && gimple_assign_single_p (def_stmt)
2130 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
2131 /* native_encode and native_decode operate on arrays of bytes
2132 and so fundamentally need a compile-time size and offset. */
2133 && maxsize.is_constant (&maxsizei)
2134 && maxsizei % BITS_PER_UNIT == 0
2135 && offset.is_constant (&offseti)
2136 && offseti % BITS_PER_UNIT == 0
2137 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
2138 || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
2139 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
2141 tree base2;
2142 HOST_WIDE_INT offset2, size2;
2143 bool reverse;
2144 base2 = get_ref_base_and_extent_hwi (gimple_assign_lhs (def_stmt),
2145 &offset2, &size2, &reverse);
2146 if (base2
2147 && !reverse
2148 && size2 % BITS_PER_UNIT == 0
2149 && offset2 % BITS_PER_UNIT == 0
2150 && operand_equal_p (base, base2, 0)
2151 && known_subrange_p (offseti, maxsizei, offset2, size2))
2153 /* We support up to 512-bit values (for V8DFmode). */
2154 unsigned char buffer[64];
2155 int len;
2157 tree rhs = gimple_assign_rhs1 (def_stmt);
2158 if (TREE_CODE (rhs) == SSA_NAME)
2159 rhs = SSA_VAL (rhs);
2160 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
2161 buffer, sizeof (buffer),
2162 (offseti - offset2) / BITS_PER_UNIT);
2163 if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
2165 tree type = vr->type;
2166 /* Make sure to interpret in a type that has a range
2167 covering the whole access size. */
2168 if (INTEGRAL_TYPE_P (vr->type)
2169 && maxsizei != TYPE_PRECISION (vr->type))
2170 type = build_nonstandard_integer_type (maxsizei,
2171 TYPE_UNSIGNED (type));
2172 tree val = native_interpret_expr (type, buffer,
2173 maxsizei / BITS_PER_UNIT);
2174 /* If we chop off bits because the types precision doesn't
2175 match the memory access size this is ok when optimizing
2176 reads but not when called from the DSE code during
2177 elimination. */
2178 if (val
2179 && type != vr->type)
2181 if (! int_fits_type_p (val, vr->type))
2182 val = NULL_TREE;
2183 else
2184 val = fold_convert (vr->type, val);
2187 if (val)
2188 return vn_reference_lookup_or_insert_for_pieces
2189 (vuse, vr->set, vr->type, vr->operands, val);
2194 /* 4) Assignment from an SSA name which definition we may be able
2195 to access pieces from. */
2196 else if (known_eq (ref->size, maxsize)
2197 && is_gimple_reg_type (vr->type)
2198 && !contains_storage_order_barrier_p (vr->operands)
2199 && gimple_assign_single_p (def_stmt)
2200 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
2202 tree base2;
2203 poly_int64 offset2, size2, maxsize2;
2204 bool reverse;
2205 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
2206 &offset2, &size2, &maxsize2,
2207 &reverse);
2208 if (!reverse
2209 && known_size_p (maxsize2)
2210 && known_eq (maxsize2, size2)
2211 && operand_equal_p (base, base2, 0)
2212 && known_subrange_p (offset, maxsize, offset2, size2)
2213 /* ??? We can't handle bitfield precision extracts without
2214 either using an alternate type for the BIT_FIELD_REF and
2215 then doing a conversion or possibly adjusting the offset
2216 according to endianness. */
2217 && (! INTEGRAL_TYPE_P (vr->type)
2218 || known_eq (ref->size, TYPE_PRECISION (vr->type)))
2219 && multiple_p (ref->size, BITS_PER_UNIT))
2221 gimple_match_op op (gimple_match_cond::UNCOND,
2222 BIT_FIELD_REF, vr->type,
2223 vn_valueize (gimple_assign_rhs1 (def_stmt)),
2224 bitsize_int (ref->size),
2225 bitsize_int (offset - offset2));
2226 tree val = vn_nary_build_or_lookup (&op);
2227 if (val
2228 && (TREE_CODE (val) != SSA_NAME
2229 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
2231 vn_reference_t res = vn_reference_lookup_or_insert_for_pieces
2232 (vuse, vr->set, vr->type, vr->operands, val);
2233 return res;
2238 /* 5) For aggregate copies translate the reference through them if
2239 the copy kills ref. */
2240 else if (vn_walk_kind == VN_WALKREWRITE
2241 && gimple_assign_single_p (def_stmt)
2242 && (DECL_P (gimple_assign_rhs1 (def_stmt))
2243 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
2244 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
2246 tree base2;
2247 int i, j, k;
2248 auto_vec<vn_reference_op_s> rhs;
2249 vn_reference_op_t vro;
2250 ao_ref r;
2252 if (!lhs_ref_ok)
2253 return (void *)-1;
2255 /* See if the assignment kills REF. */
2256 base2 = ao_ref_base (&lhs_ref);
2257 if (!lhs_ref.max_size_known_p ()
2258 || (base != base2
2259 && (TREE_CODE (base) != MEM_REF
2260 || TREE_CODE (base2) != MEM_REF
2261 || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
2262 || !tree_int_cst_equal (TREE_OPERAND (base, 1),
2263 TREE_OPERAND (base2, 1))))
2264 || !stmt_kills_ref_p (def_stmt, ref))
2265 return (void *)-1;
2267 /* Find the common base of ref and the lhs. lhs_ops already
2268 contains valueized operands for the lhs. */
2269 i = vr->operands.length () - 1;
2270 j = lhs_ops.length () - 1;
2271 while (j >= 0 && i >= 0
2272 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
2274 i--;
2275 j--;
2278 /* ??? The innermost op should always be a MEM_REF and we already
2279 checked that the assignment to the lhs kills vr. Thus for
2280 aggregate copies using char[] types the vn_reference_op_eq
2281 may fail when comparing types for compatibility. But we really
2282 don't care here - further lookups with the rewritten operands
2283 will simply fail if we messed up types too badly. */
2284 poly_int64 extra_off = 0;
2285 if (j == 0 && i >= 0
2286 && lhs_ops[0].opcode == MEM_REF
2287 && maybe_ne (lhs_ops[0].off, -1))
2289 if (known_eq (lhs_ops[0].off, vr->operands[i].off))
2290 i--, j--;
2291 else if (vr->operands[i].opcode == MEM_REF
2292 && maybe_ne (vr->operands[i].off, -1))
2294 extra_off = vr->operands[i].off - lhs_ops[0].off;
2295 i--, j--;
2299 /* i now points to the first additional op.
2300 ??? LHS may not be completely contained in VR, one or more
2301 VIEW_CONVERT_EXPRs could be in its way. We could at least
2302 try handling outermost VIEW_CONVERT_EXPRs. */
2303 if (j != -1)
2304 return (void *)-1;
2306 /* Punt if the additional ops contain a storage order barrier. */
2307 for (k = i; k >= 0; k--)
2309 vro = &vr->operands[k];
2310 if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
2311 return (void *)-1;
2314 /* Now re-write REF to be based on the rhs of the assignment. */
2315 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
2317 /* Apply an extra offset to the inner MEM_REF of the RHS. */
2318 if (maybe_ne (extra_off, 0))
2320 if (rhs.length () < 2)
2321 return (void *)-1;
2322 int ix = rhs.length () - 2;
2323 if (rhs[ix].opcode != MEM_REF
2324 || known_eq (rhs[ix].off, -1))
2325 return (void *)-1;
2326 rhs[ix].off += extra_off;
2327 rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
2328 build_int_cst (TREE_TYPE (rhs[ix].op0),
2329 extra_off));
2332 /* We need to pre-pend vr->operands[0..i] to rhs. */
2333 vec<vn_reference_op_s> old = vr->operands;
2334 if (i + 1 + rhs.length () > vr->operands.length ())
2335 vr->operands.safe_grow (i + 1 + rhs.length ());
2336 else
2337 vr->operands.truncate (i + 1 + rhs.length ());
2338 FOR_EACH_VEC_ELT (rhs, j, vro)
2339 vr->operands[i + 1 + j] = *vro;
2340 vr->operands = valueize_refs (vr->operands);
2341 if (old == shared_lookup_references)
2342 shared_lookup_references = vr->operands;
2343 vr->hashcode = vn_reference_compute_hash (vr);
2345 /* Try folding the new reference to a constant. */
2346 tree val = fully_constant_vn_reference_p (vr);
2347 if (val)
2348 return vn_reference_lookup_or_insert_for_pieces
2349 (vuse, vr->set, vr->type, vr->operands, val);
2351 /* Adjust *ref from the new operands. */
2352 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2353 return (void *)-1;
2354 /* This can happen with bitfields. */
2355 if (maybe_ne (ref->size, r.size))
2356 return (void *)-1;
2357 *ref = r;
2359 /* Do not update last seen VUSE after translating. */
2360 last_vuse_ptr = NULL;
2362 /* Keep looking for the adjusted *REF / VR pair. */
2363 return NULL;
2366 /* 6) For memcpy copies translate the reference through them if
2367 the copy kills ref. */
2368 else if (vn_walk_kind == VN_WALKREWRITE
2369 && is_gimple_reg_type (vr->type)
2370 /* ??? Handle BCOPY as well. */
2371 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
2372 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
2373 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
2374 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2375 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
2376 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
2377 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
2378 && poly_int_tree_p (gimple_call_arg (def_stmt, 2), &copy_size))
2380 tree lhs, rhs;
2381 ao_ref r;
2382 poly_int64 rhs_offset, lhs_offset;
2383 vn_reference_op_s op;
2384 poly_uint64 mem_offset;
2385 poly_int64 at, byte_maxsize;
2387 /* Only handle non-variable, addressable refs. */
2388 if (maybe_ne (ref->size, maxsize)
2389 || !multiple_p (offset, BITS_PER_UNIT, &at)
2390 || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
2391 return (void *)-1;
2393 /* Extract a pointer base and an offset for the destination. */
2394 lhs = gimple_call_arg (def_stmt, 0);
2395 lhs_offset = 0;
2396 if (TREE_CODE (lhs) == SSA_NAME)
2398 lhs = vn_valueize (lhs);
2399 if (TREE_CODE (lhs) == SSA_NAME)
2401 gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
2402 if (gimple_assign_single_p (def_stmt)
2403 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2404 lhs = gimple_assign_rhs1 (def_stmt);
2407 if (TREE_CODE (lhs) == ADDR_EXPR)
2409 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
2410 &lhs_offset);
2411 if (!tem)
2412 return (void *)-1;
2413 if (TREE_CODE (tem) == MEM_REF
2414 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
2416 lhs = TREE_OPERAND (tem, 0);
2417 if (TREE_CODE (lhs) == SSA_NAME)
2418 lhs = vn_valueize (lhs);
2419 lhs_offset += mem_offset;
2421 else if (DECL_P (tem))
2422 lhs = build_fold_addr_expr (tem);
2423 else
2424 return (void *)-1;
2426 if (TREE_CODE (lhs) != SSA_NAME
2427 && TREE_CODE (lhs) != ADDR_EXPR)
2428 return (void *)-1;
2430 /* Extract a pointer base and an offset for the source. */
2431 rhs = gimple_call_arg (def_stmt, 1);
2432 rhs_offset = 0;
2433 if (TREE_CODE (rhs) == SSA_NAME)
2434 rhs = vn_valueize (rhs);
2435 if (TREE_CODE (rhs) == ADDR_EXPR)
2437 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
2438 &rhs_offset);
2439 if (!tem)
2440 return (void *)-1;
2441 if (TREE_CODE (tem) == MEM_REF
2442 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
2444 rhs = TREE_OPERAND (tem, 0);
2445 rhs_offset += mem_offset;
2447 else if (DECL_P (tem)
2448 || TREE_CODE (tem) == STRING_CST)
2449 rhs = build_fold_addr_expr (tem);
2450 else
2451 return (void *)-1;
2453 if (TREE_CODE (rhs) != SSA_NAME
2454 && TREE_CODE (rhs) != ADDR_EXPR)
2455 return (void *)-1;
2457 /* The bases of the destination and the references have to agree. */
2458 if (TREE_CODE (base) == MEM_REF)
2460 if (TREE_OPERAND (base, 0) != lhs
2461 || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
2462 return (void *) -1;
2463 at += mem_offset;
2465 else if (!DECL_P (base)
2466 || TREE_CODE (lhs) != ADDR_EXPR
2467 || TREE_OPERAND (lhs, 0) != base)
2468 return (void *)-1;
2470 /* If the access is completely outside of the memcpy destination
2471 area there is no aliasing. */
2472 if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
2473 return NULL;
2474 /* And the access has to be contained within the memcpy destination. */
2475 if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
2476 return (void *)-1;
2478 /* Make room for 2 operands in the new reference. */
2479 if (vr->operands.length () < 2)
2481 vec<vn_reference_op_s> old = vr->operands;
2482 vr->operands.safe_grow_cleared (2);
2483 if (old == shared_lookup_references)
2484 shared_lookup_references = vr->operands;
2486 else
2487 vr->operands.truncate (2);
2489 /* The looked-through reference is a simple MEM_REF. */
2490 memset (&op, 0, sizeof (op));
2491 op.type = vr->type;
2492 op.opcode = MEM_REF;
2493 op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
2494 op.off = at - lhs_offset + rhs_offset;
2495 vr->operands[0] = op;
2496 op.type = TREE_TYPE (rhs);
2497 op.opcode = TREE_CODE (rhs);
2498 op.op0 = rhs;
2499 op.off = -1;
2500 vr->operands[1] = op;
2501 vr->hashcode = vn_reference_compute_hash (vr);
2503 /* Try folding the new reference to a constant. */
2504 tree val = fully_constant_vn_reference_p (vr);
2505 if (val)
2506 return vn_reference_lookup_or_insert_for_pieces
2507 (vuse, vr->set, vr->type, vr->operands, val);
2509 /* Adjust *ref from the new operands. */
2510 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2511 return (void *)-1;
2512 /* This can happen with bitfields. */
2513 if (maybe_ne (ref->size, r.size))
2514 return (void *)-1;
2515 *ref = r;
2517 /* Do not update last seen VUSE after translating. */
2518 last_vuse_ptr = NULL;
2520 /* Keep looking for the adjusted *REF / VR pair. */
2521 return NULL;
2524 /* Bail out and stop walking. */
2525 return (void *)-1;
2528 /* Return a reference op vector from OP that can be used for
2529 vn_reference_lookup_pieces. The caller is responsible for releasing
2530 the vector. */
2532 vec<vn_reference_op_s>
2533 vn_reference_operands_for_lookup (tree op)
2535 bool valueized;
2536 return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
2539 /* Lookup a reference operation by it's parts, in the current hash table.
2540 Returns the resulting value number if it exists in the hash table,
2541 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2542 vn_reference_t stored in the hashtable if something is found. */
2544 tree
2545 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2546 vec<vn_reference_op_s> operands,
2547 vn_reference_t *vnresult, vn_lookup_kind kind)
2549 struct vn_reference_s vr1;
2550 vn_reference_t tmp;
2551 tree cst;
2553 if (!vnresult)
2554 vnresult = &tmp;
2555 *vnresult = NULL;
2557 vr1.vuse = vuse_ssa_val (vuse);
2558 shared_lookup_references.truncate (0);
2559 shared_lookup_references.safe_grow (operands.length ());
2560 memcpy (shared_lookup_references.address (),
2561 operands.address (),
2562 sizeof (vn_reference_op_s)
2563 * operands.length ());
2564 vr1.operands = operands = shared_lookup_references
2565 = valueize_refs (shared_lookup_references);
2566 vr1.type = type;
2567 vr1.set = set;
2568 vr1.hashcode = vn_reference_compute_hash (&vr1);
2569 if ((cst = fully_constant_vn_reference_p (&vr1)))
2570 return cst;
2572 vn_reference_lookup_1 (&vr1, vnresult);
2573 if (!*vnresult
2574 && kind != VN_NOWALK
2575 && vr1.vuse)
2577 ao_ref r;
2578 vn_walk_kind = kind;
2579 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2580 *vnresult =
2581 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2582 vn_reference_lookup_2,
2583 vn_reference_lookup_3,
2584 vuse_ssa_val, &vr1);
2585 gcc_checking_assert (vr1.operands == shared_lookup_references);
2588 if (*vnresult)
2589 return (*vnresult)->result;
2591 return NULL_TREE;
2594 /* Lookup OP in the current hash table, and return the resulting value
2595 number if it exists in the hash table. Return NULL_TREE if it does
2596 not exist in the hash table or if the result field of the structure
2597 was NULL.. VNRESULT will be filled in with the vn_reference_t
2598 stored in the hashtable if one exists. When TBAA_P is false assume
2599 we are looking up a store and treat it as having alias-set zero. */
2601 tree
2602 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2603 vn_reference_t *vnresult, bool tbaa_p)
2605 vec<vn_reference_op_s> operands;
2606 struct vn_reference_s vr1;
2607 tree cst;
2608 bool valuezied_anything;
2610 if (vnresult)
2611 *vnresult = NULL;
2613 vr1.vuse = vuse_ssa_val (vuse);
2614 vr1.operands = operands
2615 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2616 vr1.type = TREE_TYPE (op);
2617 vr1.set = tbaa_p ? get_alias_set (op) : 0;
2618 vr1.hashcode = vn_reference_compute_hash (&vr1);
2619 if ((cst = fully_constant_vn_reference_p (&vr1)))
2620 return cst;
2622 if (kind != VN_NOWALK
2623 && vr1.vuse)
2625 vn_reference_t wvnresult;
2626 ao_ref r;
2627 /* Make sure to use a valueized reference if we valueized anything.
2628 Otherwise preserve the full reference for advanced TBAA. */
2629 if (!valuezied_anything
2630 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2631 vr1.operands))
2632 ao_ref_init (&r, op);
2633 if (! tbaa_p)
2634 r.ref_alias_set = r.base_alias_set = 0;
2635 vn_walk_kind = kind;
2636 wvnresult =
2637 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2638 vn_reference_lookup_2,
2639 vn_reference_lookup_3,
2640 vuse_ssa_val, &vr1);
2641 gcc_checking_assert (vr1.operands == shared_lookup_references);
2642 if (wvnresult)
2644 if (vnresult)
2645 *vnresult = wvnresult;
2646 return wvnresult->result;
2649 return NULL_TREE;
2652 return vn_reference_lookup_1 (&vr1, vnresult);
2655 /* Lookup CALL in the current hash table and return the entry in
2656 *VNRESULT if found. Populates *VR for the hashtable lookup. */
2658 void
2659 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
2660 vn_reference_t vr)
2662 if (vnresult)
2663 *vnresult = NULL;
2665 tree vuse = gimple_vuse (call);
2667 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2668 vr->operands = valueize_shared_reference_ops_from_call (call);
2669 vr->type = gimple_expr_type (call);
2670 vr->set = 0;
2671 vr->hashcode = vn_reference_compute_hash (vr);
2672 vn_reference_lookup_1 (vr, vnresult);
2675 /* Insert OP into the current hash table with a value number of RESULT. */
2677 static void
2678 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2680 vn_reference_s **slot;
2681 vn_reference_t vr1;
2682 bool tem;
2684 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
2685 if (TREE_CODE (result) == SSA_NAME)
2686 vr1->value_id = VN_INFO (result)->value_id;
2687 else
2688 vr1->value_id = get_or_alloc_constant_value_id (result);
2689 vr1->vuse = vuse_ssa_val (vuse);
2690 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2691 vr1->type = TREE_TYPE (op);
2692 vr1->set = get_alias_set (op);
2693 vr1->hashcode = vn_reference_compute_hash (vr1);
2694 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2695 vr1->result_vdef = vdef;
2697 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2698 INSERT);
2700 /* Because IL walking on reference lookup can end up visiting
2701 a def that is only to be visited later in iteration order
2702 when we are about to make an irreducible region reducible
2703 the def can be effectively processed and its ref being inserted
2704 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
2705 but save a lookup if we deal with already inserted refs here. */
2706 if (*slot)
2708 /* We cannot assert that we have the same value either because
2709 when disentangling an irreducible region we may end up visiting
2710 a use before the corresponding def. That's a missed optimization
2711 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
2712 if (dump_file && (dump_flags & TDF_DETAILS)
2713 && !operand_equal_p ((*slot)->result, vr1->result, 0))
2715 fprintf (dump_file, "Keeping old value ");
2716 print_generic_expr (dump_file, (*slot)->result);
2717 fprintf (dump_file, " because of collision\n");
2719 free_reference (vr1);
2720 obstack_free (&vn_tables_obstack, vr1);
2721 return;
2724 *slot = vr1;
2725 vr1->next = last_inserted_ref;
2726 last_inserted_ref = vr1;
2729 /* Insert a reference by it's pieces into the current hash table with
2730 a value number of RESULT. Return the resulting reference
2731 structure we created. */
2733 vn_reference_t
2734 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2735 vec<vn_reference_op_s> operands,
2736 tree result, unsigned int value_id)
2739 vn_reference_s **slot;
2740 vn_reference_t vr1;
2742 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
2743 vr1->value_id = value_id;
2744 vr1->vuse = vuse_ssa_val (vuse);
2745 vr1->operands = valueize_refs (operands);
2746 vr1->type = type;
2747 vr1->set = set;
2748 vr1->hashcode = vn_reference_compute_hash (vr1);
2749 if (result && TREE_CODE (result) == SSA_NAME)
2750 result = SSA_VAL (result);
2751 vr1->result = result;
2753 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2754 INSERT);
2756 /* At this point we should have all the things inserted that we have
2757 seen before, and we should never try inserting something that
2758 already exists. */
2759 gcc_assert (!*slot);
2761 *slot = vr1;
2762 vr1->next = last_inserted_ref;
2763 last_inserted_ref = vr1;
2764 return vr1;
2767 /* Compute and return the hash value for nary operation VBO1. */
2769 static hashval_t
2770 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2772 inchash::hash hstate;
2773 unsigned i;
2775 for (i = 0; i < vno1->length; ++i)
2776 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2777 vno1->op[i] = SSA_VAL (vno1->op[i]);
2779 if (((vno1->length == 2
2780 && commutative_tree_code (vno1->opcode))
2781 || (vno1->length == 3
2782 && commutative_ternary_tree_code (vno1->opcode)))
2783 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
2784 std::swap (vno1->op[0], vno1->op[1]);
2785 else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
2786 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
2788 std::swap (vno1->op[0], vno1->op[1]);
2789 vno1->opcode = swap_tree_comparison (vno1->opcode);
2792 hstate.add_int (vno1->opcode);
2793 for (i = 0; i < vno1->length; ++i)
2794 inchash::add_expr (vno1->op[i], hstate);
2796 return hstate.end ();
2799 /* Compare nary operations VNO1 and VNO2 and return true if they are
2800 equivalent. */
2802 bool
2803 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2805 unsigned i;
2807 if (vno1->hashcode != vno2->hashcode)
2808 return false;
2810 if (vno1->length != vno2->length)
2811 return false;
2813 if (vno1->opcode != vno2->opcode
2814 || !types_compatible_p (vno1->type, vno2->type))
2815 return false;
2817 for (i = 0; i < vno1->length; ++i)
2818 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2819 return false;
2821 /* BIT_INSERT_EXPR has an implict operand as the type precision
2822 of op1. Need to check to make sure they are the same. */
2823 if (vno1->opcode == BIT_INSERT_EXPR
2824 && TREE_CODE (vno1->op[1]) == INTEGER_CST
2825 && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
2826 != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
2827 return false;
2829 return true;
2832 /* Initialize VNO from the pieces provided. */
2834 static void
2835 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2836 enum tree_code code, tree type, tree *ops)
2838 vno->opcode = code;
2839 vno->length = length;
2840 vno->type = type;
2841 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2844 /* Initialize VNO from OP. */
2846 static void
2847 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2849 unsigned i;
2851 vno->opcode = TREE_CODE (op);
2852 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2853 vno->type = TREE_TYPE (op);
2854 for (i = 0; i < vno->length; ++i)
2855 vno->op[i] = TREE_OPERAND (op, i);
2858 /* Return the number of operands for a vn_nary ops structure from STMT. */
2860 static unsigned int
2861 vn_nary_length_from_stmt (gimple *stmt)
2863 switch (gimple_assign_rhs_code (stmt))
2865 case REALPART_EXPR:
2866 case IMAGPART_EXPR:
2867 case VIEW_CONVERT_EXPR:
2868 return 1;
2870 case BIT_FIELD_REF:
2871 return 3;
2873 case CONSTRUCTOR:
2874 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2876 default:
2877 return gimple_num_ops (stmt) - 1;
2881 /* Initialize VNO from STMT. */
2883 static void
2884 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple *stmt)
2886 unsigned i;
2888 vno->opcode = gimple_assign_rhs_code (stmt);
2889 vno->type = gimple_expr_type (stmt);
2890 switch (vno->opcode)
2892 case REALPART_EXPR:
2893 case IMAGPART_EXPR:
2894 case VIEW_CONVERT_EXPR:
2895 vno->length = 1;
2896 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2897 break;
2899 case BIT_FIELD_REF:
2900 vno->length = 3;
2901 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2902 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2903 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2904 break;
2906 case CONSTRUCTOR:
2907 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2908 for (i = 0; i < vno->length; ++i)
2909 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2910 break;
2912 default:
2913 gcc_checking_assert (!gimple_assign_single_p (stmt));
2914 vno->length = gimple_num_ops (stmt) - 1;
2915 for (i = 0; i < vno->length; ++i)
2916 vno->op[i] = gimple_op (stmt, i + 1);
2920 /* Compute the hashcode for VNO and look for it in the hash table;
2921 return the resulting value number if it exists in the hash table.
2922 Return NULL_TREE if it does not exist in the hash table or if the
2923 result field of the operation is NULL. VNRESULT will contain the
2924 vn_nary_op_t from the hashtable if it exists. */
2926 static tree
2927 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2929 vn_nary_op_s **slot;
2931 if (vnresult)
2932 *vnresult = NULL;
2934 vno->hashcode = vn_nary_op_compute_hash (vno);
2935 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
2936 if (!slot)
2937 return NULL_TREE;
2938 if (vnresult)
2939 *vnresult = *slot;
2940 return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
2943 /* Lookup a n-ary operation by its pieces and return the resulting value
2944 number if it exists in the hash table. Return NULL_TREE if it does
2945 not exist in the hash table or if the result field of the operation
2946 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2947 if it exists. */
2949 tree
2950 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2951 tree type, tree *ops, vn_nary_op_t *vnresult)
2953 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2954 sizeof_vn_nary_op (length));
2955 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2956 return vn_nary_op_lookup_1 (vno1, vnresult);
2959 /* Lookup OP in the current hash table, and return the resulting value
2960 number if it exists in the hash table. Return NULL_TREE if it does
2961 not exist in the hash table or if the result field of the operation
2962 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2963 if it exists. */
2965 tree
2966 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2968 vn_nary_op_t vno1
2969 = XALLOCAVAR (struct vn_nary_op_s,
2970 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2971 init_vn_nary_op_from_op (vno1, op);
2972 return vn_nary_op_lookup_1 (vno1, vnresult);
2975 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2976 value number if it exists in the hash table. Return NULL_TREE if
2977 it does not exist in the hash table. VNRESULT will contain the
2978 vn_nary_op_t from the hashtable if it exists. */
2980 tree
2981 vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
2983 vn_nary_op_t vno1
2984 = XALLOCAVAR (struct vn_nary_op_s,
2985 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2986 init_vn_nary_op_from_stmt (vno1, stmt);
2987 return vn_nary_op_lookup_1 (vno1, vnresult);
2990 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2992 static vn_nary_op_t
2993 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2995 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2998 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2999 obstack. */
3001 static vn_nary_op_t
3002 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
3004 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
3006 vno1->value_id = value_id;
3007 vno1->length = length;
3008 vno1->predicated_values = 0;
3009 vno1->u.result = result;
3011 return vno1;
3014 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
3015 VNO->HASHCODE first. */
3017 static vn_nary_op_t
3018 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
3019 bool compute_hash)
3021 vn_nary_op_s **slot;
3023 if (compute_hash)
3025 vno->hashcode = vn_nary_op_compute_hash (vno);
3026 gcc_assert (! vno->predicated_values
3027 || (! vno->u.values->next
3028 && vno->u.values->valid_dominated_by_p[0] != EXIT_BLOCK
3029 && vno->u.values->valid_dominated_by_p[1] == EXIT_BLOCK));
3032 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
3033 vno->unwind_to = *slot;
3034 if (*slot)
3036 /* Prefer non-predicated values.
3037 ??? Only if those are constant, otherwise, with constant predicated
3038 value, turn them into predicated values with entry-block validity
3039 (??? but we always find the first valid result currently). */
3040 if ((*slot)->predicated_values
3041 && ! vno->predicated_values)
3043 /* ??? We cannot remove *slot from the unwind stack list.
3044 For the moment we deal with this by skipping not found
3045 entries but this isn't ideal ... */
3046 *slot = vno;
3047 /* ??? Maintain a stack of states we can unwind in
3048 vn_nary_op_s? But how far do we unwind? In reality
3049 we need to push change records somewhere... Or not
3050 unwind vn_nary_op_s and linking them but instead
3051 unwind the results "list", linking that, which also
3052 doesn't move on hashtable resize. */
3053 /* We can also have a ->unwind_to recording *slot there.
3054 That way we can make u.values a fixed size array with
3055 recording the number of entries but of course we then
3056 have always N copies for each unwind_to-state. Or we
3057 make sure to only ever append and each unwinding will
3058 pop off one entry (but how to deal with predicated
3059 replaced with non-predicated here?) */
3060 vno->next = last_inserted_nary;
3061 last_inserted_nary = vno;
3062 return vno;
3064 else if (vno->predicated_values
3065 && ! (*slot)->predicated_values)
3066 return *slot;
3067 else if (vno->predicated_values
3068 && (*slot)->predicated_values)
3070 /* ??? Factor this all into a insert_single_predicated_value
3071 routine. */
3072 gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
3073 basic_block vno_bb
3074 = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
3075 vn_pval *nval = vno->u.values;
3076 vn_pval **next = &vno->u.values;
3077 bool found = false;
3078 for (vn_pval *val = (*slot)->u.values; val; val = val->next)
3080 if (expressions_equal_p (val->result, vno->u.values->result))
3082 found = true;
3083 for (unsigned i = 0; i < val->n; ++i)
3085 basic_block val_bb
3086 = BASIC_BLOCK_FOR_FN (cfun,
3087 val->valid_dominated_by_p[i]);
3088 if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
3089 /* Value registered with more generic predicate. */
3090 return *slot;
3091 else if (dominated_by_p (CDI_DOMINATORS, val_bb, vno_bb))
3092 /* Shouldn't happen, we insert in RPO order. */
3093 gcc_unreachable ();
3095 /* Append value. */
3096 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
3097 sizeof (vn_pval)
3098 + val->n * sizeof (int));
3099 (*next)->next = NULL;
3100 (*next)->result = val->result;
3101 (*next)->n = val->n + 1;
3102 memcpy ((*next)->valid_dominated_by_p,
3103 val->valid_dominated_by_p,
3104 val->n * sizeof (int));
3105 (*next)->valid_dominated_by_p[val->n] = vno_bb->index;
3106 next = &(*next)->next;
3107 if (dump_file && (dump_flags & TDF_DETAILS))
3108 fprintf (dump_file, "Appending predicate to value.\n");
3109 continue;
3111 /* Copy other predicated values. */
3112 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
3113 sizeof (vn_pval)
3114 + (val->n-1) * sizeof (int));
3115 memcpy (*next, val, sizeof (vn_pval) + (val->n-1) * sizeof (int));
3116 (*next)->next = NULL;
3117 next = &(*next)->next;
3119 if (!found)
3120 *next = nval;
3122 *slot = vno;
3123 vno->next = last_inserted_nary;
3124 last_inserted_nary = vno;
3125 return vno;
3128 /* While we do not want to insert things twice it's awkward to
3129 avoid it in the case where visit_nary_op pattern-matches stuff
3130 and ends up simplifying the replacement to itself. We then
3131 get two inserts, one from visit_nary_op and one from
3132 vn_nary_build_or_lookup.
3133 So allow inserts with the same value number. */
3134 if ((*slot)->u.result == vno->u.result)
3135 return *slot;
3138 /* ??? There's also optimistic vs. previous commited state merging
3139 that is problematic for the case of unwinding. */
3141 /* ??? We should return NULL if we do not use 'vno' and have the
3142 caller release it. */
3143 gcc_assert (!*slot);
3145 *slot = vno;
3146 vno->next = last_inserted_nary;
3147 last_inserted_nary = vno;
3148 return vno;
3151 /* Insert a n-ary operation into the current hash table using it's
3152 pieces. Return the vn_nary_op_t structure we created and put in
3153 the hashtable. */
3155 vn_nary_op_t
3156 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
3157 tree type, tree *ops,
3158 tree result, unsigned int value_id)
3160 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
3161 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
3162 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
3165 static vn_nary_op_t
3166 vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
3167 tree type, tree *ops,
3168 tree result, unsigned int value_id,
3169 edge pred_e)
3171 /* ??? Currently tracking BBs. */
3172 if (! single_pred_p (pred_e->dest))
3174 /* Never record for backedges. */
3175 if (pred_e->flags & EDGE_DFS_BACK)
3176 return NULL;
3177 edge_iterator ei;
3178 edge e;
3179 int cnt = 0;
3180 /* Ignore backedges. */
3181 FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
3182 if (! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
3183 cnt++;
3184 if (cnt != 1)
3185 return NULL;
3187 if (dump_file && (dump_flags & TDF_DETAILS)
3188 /* ??? Fix dumping, but currently we only get comparisons. */
3189 && TREE_CODE_CLASS (code) == tcc_comparison)
3191 fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
3192 pred_e->dest->index);
3193 print_generic_expr (dump_file, ops[0], TDF_SLIM);
3194 fprintf (dump_file, " %s ", get_tree_code_name (code));
3195 print_generic_expr (dump_file, ops[1], TDF_SLIM);
3196 fprintf (dump_file, " == %s\n",
3197 integer_zerop (result) ? "false" : "true");
3199 vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
3200 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
3201 vno1->predicated_values = 1;
3202 vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
3203 sizeof (vn_pval));
3204 vno1->u.values->next = NULL;
3205 vno1->u.values->result = result;
3206 vno1->u.values->n = 1;
3207 vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
3208 vno1->u.values->valid_dominated_by_p[1] = EXIT_BLOCK;
3209 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
3212 static bool
3213 dominated_by_p_w_unex (basic_block bb1, basic_block bb2);
3215 static tree
3216 vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb)
3218 if (! vno->predicated_values)
3219 return vno->u.result;
3220 for (vn_pval *val = vno->u.values; val; val = val->next)
3221 for (unsigned i = 0; i < val->n; ++i)
3222 if (dominated_by_p_w_unex (bb,
3223 BASIC_BLOCK_FOR_FN
3224 (cfun, val->valid_dominated_by_p[i])))
3225 return val->result;
3226 return NULL_TREE;
3229 /* Insert OP into the current hash table with a value number of
3230 RESULT. Return the vn_nary_op_t structure we created and put in
3231 the hashtable. */
3233 vn_nary_op_t
3234 vn_nary_op_insert (tree op, tree result)
3236 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
3237 vn_nary_op_t vno1;
3239 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
3240 init_vn_nary_op_from_op (vno1, op);
3241 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
3244 /* Insert the rhs of STMT into the current hash table with a value number of
3245 RESULT. */
3247 static vn_nary_op_t
3248 vn_nary_op_insert_stmt (gimple *stmt, tree result)
3250 vn_nary_op_t vno1
3251 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
3252 result, VN_INFO (result)->value_id);
3253 init_vn_nary_op_from_stmt (vno1, stmt);
3254 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
3257 /* Compute a hashcode for PHI operation VP1 and return it. */
3259 static inline hashval_t
3260 vn_phi_compute_hash (vn_phi_t vp1)
3262 inchash::hash hstate (EDGE_COUNT (vp1->block->preds) > 2
3263 ? vp1->block->index : EDGE_COUNT (vp1->block->preds));
3264 tree phi1op;
3265 tree type;
3266 edge e;
3267 edge_iterator ei;
3269 /* If all PHI arguments are constants we need to distinguish
3270 the PHI node via its type. */
3271 type = vp1->type;
3272 hstate.merge_hash (vn_hash_type (type));
3274 FOR_EACH_EDGE (e, ei, vp1->block->preds)
3276 /* Don't hash backedge values they need to be handled as VN_TOP
3277 for optimistic value-numbering. */
3278 if (e->flags & EDGE_DFS_BACK)
3279 continue;
3281 phi1op = vp1->phiargs[e->dest_idx];
3282 if (phi1op == VN_TOP)
3283 continue;
3284 inchash::add_expr (phi1op, hstate);
3287 return hstate.end ();
3291 /* Return true if COND1 and COND2 represent the same condition, set
3292 *INVERTED_P if one needs to be inverted to make it the same as
3293 the other. */
3295 static bool
3296 cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
3297 gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
3299 enum tree_code code1 = gimple_cond_code (cond1);
3300 enum tree_code code2 = gimple_cond_code (cond2);
3302 *inverted_p = false;
3303 if (code1 == code2)
3305 else if (code1 == swap_tree_comparison (code2))
3306 std::swap (lhs2, rhs2);
3307 else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
3308 *inverted_p = true;
3309 else if (code1 == invert_tree_comparison
3310 (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
3312 std::swap (lhs2, rhs2);
3313 *inverted_p = true;
3315 else
3316 return false;
3318 return ((expressions_equal_p (lhs1, lhs2)
3319 && expressions_equal_p (rhs1, rhs2))
3320 || (commutative_tree_code (code1)
3321 && expressions_equal_p (lhs1, rhs2)
3322 && expressions_equal_p (rhs1, lhs2)));
3325 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
3327 static int
3328 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
3330 if (vp1->hashcode != vp2->hashcode)
3331 return false;
3333 if (vp1->block != vp2->block)
3335 if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
3336 return false;
3338 switch (EDGE_COUNT (vp1->block->preds))
3340 case 1:
3341 /* Single-arg PHIs are just copies. */
3342 break;
3344 case 2:
3346 /* Rule out backedges into the PHI. */
3347 if (vp1->block->loop_father->header == vp1->block
3348 || vp2->block->loop_father->header == vp2->block)
3349 return false;
3351 /* If the PHI nodes do not have compatible types
3352 they are not the same. */
3353 if (!types_compatible_p (vp1->type, vp2->type))
3354 return false;
3356 basic_block idom1
3357 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
3358 basic_block idom2
3359 = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
3360 /* If the immediate dominator end in switch stmts multiple
3361 values may end up in the same PHI arg via intermediate
3362 CFG merges. */
3363 if (EDGE_COUNT (idom1->succs) != 2
3364 || EDGE_COUNT (idom2->succs) != 2)
3365 return false;
3367 /* Verify the controlling stmt is the same. */
3368 gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1));
3369 gcond *last2 = safe_dyn_cast <gcond *> (last_stmt (idom2));
3370 if (! last1 || ! last2)
3371 return false;
3372 bool inverted_p;
3373 if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
3374 last2, vp2->cclhs, vp2->ccrhs,
3375 &inverted_p))
3376 return false;
3378 /* Get at true/false controlled edges into the PHI. */
3379 edge te1, te2, fe1, fe2;
3380 if (! extract_true_false_controlled_edges (idom1, vp1->block,
3381 &te1, &fe1)
3382 || ! extract_true_false_controlled_edges (idom2, vp2->block,
3383 &te2, &fe2))
3384 return false;
3386 /* Swap edges if the second condition is the inverted of the
3387 first. */
3388 if (inverted_p)
3389 std::swap (te2, fe2);
3391 /* ??? Handle VN_TOP specially. */
3392 if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
3393 vp2->phiargs[te2->dest_idx])
3394 || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
3395 vp2->phiargs[fe2->dest_idx]))
3396 return false;
3398 return true;
3401 default:
3402 return false;
3406 /* If the PHI nodes do not have compatible types
3407 they are not the same. */
3408 if (!types_compatible_p (vp1->type, vp2->type))
3409 return false;
3411 /* Any phi in the same block will have it's arguments in the
3412 same edge order, because of how we store phi nodes. */
3413 for (unsigned i = 0; i < EDGE_COUNT (vp1->block->preds); ++i)
3415 tree phi1op = vp1->phiargs[i];
3416 tree phi2op = vp2->phiargs[i];
3417 if (phi1op == VN_TOP || phi2op == VN_TOP)
3418 continue;
3419 if (!expressions_equal_p (phi1op, phi2op))
3420 return false;
3423 return true;
3426 /* Lookup PHI in the current hash table, and return the resulting
3427 value number if it exists in the hash table. Return NULL_TREE if
3428 it does not exist in the hash table. */
3430 static tree
3431 vn_phi_lookup (gimple *phi, bool backedges_varying_p)
3433 vn_phi_s **slot;
3434 struct vn_phi_s *vp1;
3435 edge e;
3436 edge_iterator ei;
3438 vp1 = XALLOCAVAR (struct vn_phi_s,
3439 sizeof (struct vn_phi_s)
3440 + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
3442 /* Canonicalize the SSA_NAME's to their value number. */
3443 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3445 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3446 if (TREE_CODE (def) == SSA_NAME
3447 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
3448 def = SSA_VAL (def);
3449 vp1->phiargs[e->dest_idx] = def;
3451 vp1->type = TREE_TYPE (gimple_phi_result (phi));
3452 vp1->block = gimple_bb (phi);
3453 /* Extract values of the controlling condition. */
3454 vp1->cclhs = NULL_TREE;
3455 vp1->ccrhs = NULL_TREE;
3456 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
3457 if (EDGE_COUNT (idom1->succs) == 2)
3458 if (gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1)))
3460 /* ??? We want to use SSA_VAL here. But possibly not
3461 allow VN_TOP. */
3462 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
3463 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
3465 vp1->hashcode = vn_phi_compute_hash (vp1);
3466 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
3467 if (!slot)
3468 return NULL_TREE;
3469 return (*slot)->result;
3472 /* Insert PHI into the current hash table with a value number of
3473 RESULT. */
3475 static vn_phi_t
3476 vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
3478 vn_phi_s **slot;
3479 vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
3480 sizeof (vn_phi_s)
3481 + ((gimple_phi_num_args (phi) - 1)
3482 * sizeof (tree)));
3483 edge e;
3484 edge_iterator ei;
3486 /* Canonicalize the SSA_NAME's to their value number. */
3487 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3489 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3490 if (TREE_CODE (def) == SSA_NAME
3491 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
3492 def = SSA_VAL (def);
3493 vp1->phiargs[e->dest_idx] = def;
3495 vp1->value_id = VN_INFO (result)->value_id;
3496 vp1->type = TREE_TYPE (gimple_phi_result (phi));
3497 vp1->block = gimple_bb (phi);
3498 /* Extract values of the controlling condition. */
3499 vp1->cclhs = NULL_TREE;
3500 vp1->ccrhs = NULL_TREE;
3501 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
3502 if (EDGE_COUNT (idom1->succs) == 2)
3503 if (gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1)))
3505 /* ??? We want to use SSA_VAL here. But possibly not
3506 allow VN_TOP. */
3507 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
3508 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
3510 vp1->result = result;
3511 vp1->hashcode = vn_phi_compute_hash (vp1);
3513 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
3514 gcc_assert (!*slot);
3516 *slot = vp1;
3517 vp1->next = last_inserted_phi;
3518 last_inserted_phi = vp1;
3519 return vp1;
3523 /* Return true if BB1 is dominated by BB2 taking into account edges
3524 that are not executable. */
3526 static bool
3527 dominated_by_p_w_unex (basic_block bb1, basic_block bb2)
3529 edge_iterator ei;
3530 edge e;
3532 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
3533 return true;
3535 /* Before iterating we'd like to know if there exists a
3536 (executable) path from bb2 to bb1 at all, if not we can
3537 directly return false. For now simply iterate once. */
3539 /* Iterate to the single executable bb1 predecessor. */
3540 if (EDGE_COUNT (bb1->preds) > 1)
3542 edge prede = NULL;
3543 FOR_EACH_EDGE (e, ei, bb1->preds)
3544 if (e->flags & EDGE_EXECUTABLE)
3546 if (prede)
3548 prede = NULL;
3549 break;
3551 prede = e;
3553 if (prede)
3555 bb1 = prede->src;
3557 /* Re-do the dominance check with changed bb1. */
3558 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
3559 return true;
3563 /* Iterate to the single executable bb2 successor. */
3564 edge succe = NULL;
3565 FOR_EACH_EDGE (e, ei, bb2->succs)
3566 if (e->flags & EDGE_EXECUTABLE)
3568 if (succe)
3570 succe = NULL;
3571 break;
3573 succe = e;
3575 if (succe)
3577 /* Verify the reached block is only reached through succe.
3578 If there is only one edge we can spare us the dominator
3579 check and iterate directly. */
3580 if (EDGE_COUNT (succe->dest->preds) > 1)
3582 FOR_EACH_EDGE (e, ei, succe->dest->preds)
3583 if (e != succe
3584 && (e->flags & EDGE_EXECUTABLE))
3586 succe = NULL;
3587 break;
3590 if (succe)
3592 bb2 = succe->dest;
3594 /* Re-do the dominance check with changed bb2. */
3595 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
3596 return true;
3600 /* We could now iterate updating bb1 / bb2. */
3601 return false;
3604 /* Set the value number of FROM to TO, return true if it has changed
3605 as a result. */
3607 static inline bool
3608 set_ssa_val_to (tree from, tree to)
3610 vn_ssa_aux_t from_info = VN_INFO (from);
3611 tree currval = from_info->valnum; // SSA_VAL (from)
3612 poly_int64 toff, coff;
3614 /* The only thing we allow as value numbers are ssa_names
3615 and invariants. So assert that here. We don't allow VN_TOP
3616 as visiting a stmt should produce a value-number other than
3617 that.
3618 ??? Still VN_TOP can happen for unreachable code, so force
3619 it to varying in that case. Not all code is prepared to
3620 get VN_TOP on valueization. */
3621 if (to == VN_TOP)
3623 /* ??? When iterating and visiting PHI <undef, backedge-value>
3624 for the first time we rightfully get VN_TOP and we need to
3625 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
3626 With SCCVN we were simply lucky we iterated the other PHI
3627 cycles first and thus visited the backedge-value DEF. */
3628 if (currval == VN_TOP)
3629 goto set_and_exit;
3630 if (dump_file && (dump_flags & TDF_DETAILS))
3631 fprintf (dump_file, "Forcing value number to varying on "
3632 "receiving VN_TOP\n");
3633 to = from;
3636 gcc_checking_assert (to != NULL_TREE
3637 && ((TREE_CODE (to) == SSA_NAME
3638 && (to == from || SSA_VAL (to) == to))
3639 || is_gimple_min_invariant (to)));
3641 if (from != to)
3643 if (currval == from)
3645 if (dump_file && (dump_flags & TDF_DETAILS))
3647 fprintf (dump_file, "Not changing value number of ");
3648 print_generic_expr (dump_file, from);
3649 fprintf (dump_file, " from VARYING to ");
3650 print_generic_expr (dump_file, to);
3651 fprintf (dump_file, "\n");
3653 return false;
3655 else if (currval != VN_TOP
3656 && ! is_gimple_min_invariant (currval)
3657 && ! ssa_undefined_value_p (currval, false)
3658 && is_gimple_min_invariant (to))
3660 if (dump_file && (dump_flags & TDF_DETAILS))
3662 fprintf (dump_file, "Forcing VARYING instead of changing "
3663 "value number of ");
3664 print_generic_expr (dump_file, from);
3665 fprintf (dump_file, " from ");
3666 print_generic_expr (dump_file, currval);
3667 fprintf (dump_file, " (non-constant) to ");
3668 print_generic_expr (dump_file, to);
3669 fprintf (dump_file, " (constant)\n");
3671 to = from;
3673 else if (TREE_CODE (to) == SSA_NAME
3674 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
3675 to = from;
3678 set_and_exit:
3679 if (dump_file && (dump_flags & TDF_DETAILS))
3681 fprintf (dump_file, "Setting value number of ");
3682 print_generic_expr (dump_file, from);
3683 fprintf (dump_file, " to ");
3684 print_generic_expr (dump_file, to);
3687 if (currval != to
3688 && !operand_equal_p (currval, to, 0)
3689 /* Different undefined SSA names are not actually different. See
3690 PR82320 for a testcase were we'd otherwise not terminate iteration. */
3691 && !(TREE_CODE (currval) == SSA_NAME
3692 && TREE_CODE (to) == SSA_NAME
3693 && ssa_undefined_value_p (currval, false)
3694 && ssa_undefined_value_p (to, false))
3695 /* ??? For addresses involving volatile objects or types operand_equal_p
3696 does not reliably detect ADDR_EXPRs as equal. We know we are only
3697 getting invariant gimple addresses here, so can use
3698 get_addr_base_and_unit_offset to do this comparison. */
3699 && !(TREE_CODE (currval) == ADDR_EXPR
3700 && TREE_CODE (to) == ADDR_EXPR
3701 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
3702 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
3703 && known_eq (coff, toff)))
3705 if (dump_file && (dump_flags & TDF_DETAILS))
3706 fprintf (dump_file, " (changed)\n");
3707 from_info->valnum = to;
3708 return true;
3710 if (dump_file && (dump_flags & TDF_DETAILS))
3711 fprintf (dump_file, "\n");
3712 return false;
3715 /* Set all definitions in STMT to value number to themselves.
3716 Return true if a value number changed. */
3718 static bool
3719 defs_to_varying (gimple *stmt)
3721 bool changed = false;
3722 ssa_op_iter iter;
3723 def_operand_p defp;
3725 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
3727 tree def = DEF_FROM_PTR (defp);
3728 changed |= set_ssa_val_to (def, def);
3730 return changed;
3733 /* Visit a copy between LHS and RHS, return true if the value number
3734 changed. */
3736 static bool
3737 visit_copy (tree lhs, tree rhs)
3739 /* Valueize. */
3740 rhs = SSA_VAL (rhs);
3742 return set_ssa_val_to (lhs, rhs);
3745 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
3746 is the same. */
3748 static tree
3749 valueized_wider_op (tree wide_type, tree op)
3751 if (TREE_CODE (op) == SSA_NAME)
3752 op = vn_valueize (op);
3754 /* Either we have the op widened available. */
3755 tree ops[3] = {};
3756 ops[0] = op;
3757 tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
3758 wide_type, ops, NULL);
3759 if (tem)
3760 return tem;
3762 /* Or the op is truncated from some existing value. */
3763 if (TREE_CODE (op) == SSA_NAME)
3765 gimple *def = SSA_NAME_DEF_STMT (op);
3766 if (is_gimple_assign (def)
3767 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
3769 tem = gimple_assign_rhs1 (def);
3770 if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
3772 if (TREE_CODE (tem) == SSA_NAME)
3773 tem = vn_valueize (tem);
3774 return tem;
3779 /* For constants simply extend it. */
3780 if (TREE_CODE (op) == INTEGER_CST)
3781 return wide_int_to_tree (wide_type, wi::to_wide (op));
3783 return NULL_TREE;
3786 /* Visit a nary operator RHS, value number it, and return true if the
3787 value number of LHS has changed as a result. */
3789 static bool
3790 visit_nary_op (tree lhs, gassign *stmt)
3792 vn_nary_op_t vnresult;
3793 tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
3794 if (! result && vnresult)
3795 result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
3796 if (result)
3797 return set_ssa_val_to (lhs, result);
3799 /* Do some special pattern matching for redundancies of operations
3800 in different types. */
3801 enum tree_code code = gimple_assign_rhs_code (stmt);
3802 tree type = TREE_TYPE (lhs);
3803 tree rhs1 = gimple_assign_rhs1 (stmt);
3804 switch (code)
3806 CASE_CONVERT:
3807 /* Match arithmetic done in a different type where we can easily
3808 substitute the result from some earlier sign-changed or widened
3809 operation. */
3810 if (INTEGRAL_TYPE_P (type)
3811 && TREE_CODE (rhs1) == SSA_NAME
3812 /* We only handle sign-changes or zero-extension -> & mask. */
3813 && ((TYPE_UNSIGNED (TREE_TYPE (rhs1))
3814 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
3815 || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
3817 gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
3818 if (def
3819 && (gimple_assign_rhs_code (def) == PLUS_EXPR
3820 || gimple_assign_rhs_code (def) == MINUS_EXPR
3821 || gimple_assign_rhs_code (def) == MULT_EXPR))
3823 tree ops[3] = {};
3824 /* Either we have the op widened available. */
3825 ops[0] = valueized_wider_op (type,
3826 gimple_assign_rhs1 (def));
3827 if (ops[0])
3828 ops[1] = valueized_wider_op (type,
3829 gimple_assign_rhs2 (def));
3830 if (ops[0] && ops[1])
3832 ops[0] = vn_nary_op_lookup_pieces
3833 (2, gimple_assign_rhs_code (def), type, ops, NULL);
3834 /* We have wider operation available. */
3835 if (ops[0])
3837 unsigned lhs_prec = TYPE_PRECISION (type);
3838 unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
3839 if (lhs_prec == rhs_prec)
3841 gimple_match_op match_op (gimple_match_cond::UNCOND,
3842 NOP_EXPR, type, ops[0]);
3843 result = vn_nary_build_or_lookup (&match_op);
3844 if (result)
3846 bool changed = set_ssa_val_to (lhs, result);
3847 vn_nary_op_insert_stmt (stmt, result);
3848 return changed;
3851 else
3853 tree mask = wide_int_to_tree
3854 (type, wi::mask (rhs_prec, false, lhs_prec));
3855 gimple_match_op match_op (gimple_match_cond::UNCOND,
3856 BIT_AND_EXPR,
3857 TREE_TYPE (lhs),
3858 ops[0], mask);
3859 result = vn_nary_build_or_lookup (&match_op);
3860 if (result)
3862 bool changed = set_ssa_val_to (lhs, result);
3863 vn_nary_op_insert_stmt (stmt, result);
3864 return changed;
3871 default:;
3874 bool changed = set_ssa_val_to (lhs, lhs);
3875 vn_nary_op_insert_stmt (stmt, lhs);
3876 return changed;
3879 /* Visit a call STMT storing into LHS. Return true if the value number
3880 of the LHS has changed as a result. */
3882 static bool
3883 visit_reference_op_call (tree lhs, gcall *stmt)
3885 bool changed = false;
3886 struct vn_reference_s vr1;
3887 vn_reference_t vnresult = NULL;
3888 tree vdef = gimple_vdef (stmt);
3890 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
3891 if (lhs && TREE_CODE (lhs) != SSA_NAME)
3892 lhs = NULL_TREE;
3894 vn_reference_lookup_call (stmt, &vnresult, &vr1);
3895 if (vnresult)
3897 if (vnresult->result_vdef && vdef)
3898 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
3899 else if (vdef)
3900 /* If the call was discovered to be pure or const reflect
3901 that as far as possible. */
3902 changed |= set_ssa_val_to (vdef, vuse_ssa_val (gimple_vuse (stmt)));
3904 if (!vnresult->result && lhs)
3905 vnresult->result = lhs;
3907 if (vnresult->result && lhs)
3908 changed |= set_ssa_val_to (lhs, vnresult->result);
3910 else
3912 vn_reference_t vr2;
3913 vn_reference_s **slot;
3914 tree vdef_val = vdef;
3915 if (vdef)
3917 /* If we value numbered an indirect functions function to
3918 one not clobbering memory value number its VDEF to its
3919 VUSE. */
3920 tree fn = gimple_call_fn (stmt);
3921 if (fn && TREE_CODE (fn) == SSA_NAME)
3923 fn = SSA_VAL (fn);
3924 if (TREE_CODE (fn) == ADDR_EXPR
3925 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
3926 && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
3927 & (ECF_CONST | ECF_PURE)))
3928 vdef_val = vuse_ssa_val (gimple_vuse (stmt));
3930 changed |= set_ssa_val_to (vdef, vdef_val);
3932 if (lhs)
3933 changed |= set_ssa_val_to (lhs, lhs);
3934 vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
3935 vr2->vuse = vr1.vuse;
3936 /* As we are not walking the virtual operand chain we know the
3937 shared_lookup_references are still original so we can re-use
3938 them here. */
3939 vr2->operands = vr1.operands.copy ();
3940 vr2->type = vr1.type;
3941 vr2->set = vr1.set;
3942 vr2->hashcode = vr1.hashcode;
3943 vr2->result = lhs;
3944 vr2->result_vdef = vdef_val;
3945 slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
3946 INSERT);
3947 gcc_assert (!*slot);
3948 *slot = vr2;
3949 vr2->next = last_inserted_ref;
3950 last_inserted_ref = vr2;
3953 return changed;
3956 /* Visit a load from a reference operator RHS, part of STMT, value number it,
3957 and return true if the value number of the LHS has changed as a result. */
3959 static bool
3960 visit_reference_op_load (tree lhs, tree op, gimple *stmt)
3962 bool changed = false;
3963 tree last_vuse;
3964 tree result;
3966 last_vuse = gimple_vuse (stmt);
3967 last_vuse_ptr = &last_vuse;
3968 result = vn_reference_lookup (op, gimple_vuse (stmt),
3969 default_vn_walk_kind, NULL, true);
3970 last_vuse_ptr = NULL;
3972 /* We handle type-punning through unions by value-numbering based
3973 on offset and size of the access. Be prepared to handle a
3974 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
3975 if (result
3976 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
3978 /* We will be setting the value number of lhs to the value number
3979 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
3980 So first simplify and lookup this expression to see if it
3981 is already available. */
3982 gimple_match_op res_op (gimple_match_cond::UNCOND,
3983 VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
3984 result = vn_nary_build_or_lookup (&res_op);
3985 /* When building the conversion fails avoid inserting the reference
3986 again. */
3987 if (!result)
3988 return set_ssa_val_to (lhs, lhs);
3991 if (result)
3992 changed = set_ssa_val_to (lhs, result);
3993 else
3995 changed = set_ssa_val_to (lhs, lhs);
3996 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3999 return changed;
4003 /* Visit a store to a reference operator LHS, part of STMT, value number it,
4004 and return true if the value number of the LHS has changed as a result. */
4006 static bool
4007 visit_reference_op_store (tree lhs, tree op, gimple *stmt)
4009 bool changed = false;
4010 vn_reference_t vnresult = NULL;
4011 tree assign;
4012 bool resultsame = false;
4013 tree vuse = gimple_vuse (stmt);
4014 tree vdef = gimple_vdef (stmt);
4016 if (TREE_CODE (op) == SSA_NAME)
4017 op = SSA_VAL (op);
4019 /* First we want to lookup using the *vuses* from the store and see
4020 if there the last store to this location with the same address
4021 had the same value.
4023 The vuses represent the memory state before the store. If the
4024 memory state, address, and value of the store is the same as the
4025 last store to this location, then this store will produce the
4026 same memory state as that store.
4028 In this case the vdef versions for this store are value numbered to those
4029 vuse versions, since they represent the same memory state after
4030 this store.
4032 Otherwise, the vdefs for the store are used when inserting into
4033 the table, since the store generates a new memory state. */
4035 vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
4036 if (vnresult
4037 && vnresult->result)
4039 tree result = vnresult->result;
4040 gcc_checking_assert (TREE_CODE (result) != SSA_NAME
4041 || result == SSA_VAL (result));
4042 resultsame = expressions_equal_p (result, op);
4043 if (resultsame)
4045 /* If the TBAA state isn't compatible for downstream reads
4046 we cannot value-number the VDEFs the same. */
4047 alias_set_type set = get_alias_set (lhs);
4048 if (vnresult->set != set
4049 && ! alias_set_subset_of (set, vnresult->set))
4050 resultsame = false;
4054 if (!resultsame)
4056 /* Only perform the following when being called from PRE
4057 which embeds tail merging. */
4058 if (default_vn_walk_kind == VN_WALK)
4060 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
4061 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
4062 if (vnresult)
4064 VN_INFO (vdef)->visited = true;
4065 return set_ssa_val_to (vdef, vnresult->result_vdef);
4069 if (dump_file && (dump_flags & TDF_DETAILS))
4071 fprintf (dump_file, "No store match\n");
4072 fprintf (dump_file, "Value numbering store ");
4073 print_generic_expr (dump_file, lhs);
4074 fprintf (dump_file, " to ");
4075 print_generic_expr (dump_file, op);
4076 fprintf (dump_file, "\n");
4078 /* Have to set value numbers before insert, since insert is
4079 going to valueize the references in-place. */
4080 if (vdef)
4081 changed |= set_ssa_val_to (vdef, vdef);
4083 /* Do not insert structure copies into the tables. */
4084 if (is_gimple_min_invariant (op)
4085 || is_gimple_reg (op))
4086 vn_reference_insert (lhs, op, vdef, NULL);
4088 /* Only perform the following when being called from PRE
4089 which embeds tail merging. */
4090 if (default_vn_walk_kind == VN_WALK)
4092 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
4093 vn_reference_insert (assign, lhs, vuse, vdef);
4096 else
4098 /* We had a match, so value number the vdef to have the value
4099 number of the vuse it came from. */
4101 if (dump_file && (dump_flags & TDF_DETAILS))
4102 fprintf (dump_file, "Store matched earlier value, "
4103 "value numbering store vdefs to matching vuses.\n");
4105 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
4108 return changed;
4111 /* Visit and value number PHI, return true if the value number
4112 changed. When BACKEDGES_VARYING_P is true then assume all
4113 backedge values are varying. When INSERTED is not NULL then
4114 this is just a ahead query for a possible iteration, set INSERTED
4115 to true if we'd insert into the hashtable. */
4117 static bool
4118 visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
4120 tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
4121 tree backedge_name = NULL_TREE;
4122 tree sameval_base = NULL_TREE;
4123 poly_int64 soff, doff;
4124 unsigned n_executable = 0;
4125 bool allsame = true;
4126 edge_iterator ei;
4127 edge e;
4129 /* TODO: We could check for this in initialization, and replace this
4130 with a gcc_assert. */
4131 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
4132 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
4134 /* We track whether a PHI was CSEd to to avoid excessive iterations
4135 that would be necessary only because the PHI changed arguments
4136 but not value. */
4137 if (!inserted)
4138 gimple_set_plf (phi, GF_PLF_1, false);
4140 /* See if all non-TOP arguments have the same value. TOP is
4141 equivalent to everything, so we can ignore it. */
4142 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
4143 if (e->flags & EDGE_EXECUTABLE)
4145 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4147 ++n_executable;
4148 if (TREE_CODE (def) == SSA_NAME)
4150 if (e->flags & EDGE_DFS_BACK)
4151 backedge_name = def;
4152 if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
4153 def = SSA_VAL (def);
4155 if (def == VN_TOP)
4157 /* Ignore undefined defs for sameval but record one. */
4158 else if (TREE_CODE (def) == SSA_NAME
4159 && ! virtual_operand_p (def)
4160 && ssa_undefined_value_p (def, false))
4161 seen_undef = def;
4162 else if (sameval == VN_TOP)
4163 sameval = def;
4164 else if (!expressions_equal_p (def, sameval))
4166 /* We know we're arriving only with invariant addresses here,
4167 try harder comparing them. We can do some caching here
4168 which we cannot do in expressions_equal_p. */
4169 if (TREE_CODE (def) == ADDR_EXPR
4170 && TREE_CODE (sameval) == ADDR_EXPR
4171 && sameval_base != (void *)-1)
4173 if (!sameval_base)
4174 sameval_base = get_addr_base_and_unit_offset
4175 (TREE_OPERAND (sameval, 0), &soff);
4176 if (!sameval_base)
4177 sameval_base = (tree)(void *)-1;
4178 else if ((get_addr_base_and_unit_offset
4179 (TREE_OPERAND (def, 0), &doff) == sameval_base)
4180 && known_eq (soff, doff))
4181 continue;
4183 allsame = false;
4184 break;
4188 /* If the value we want to use is the backedge and that wasn't visited
4189 yet drop to VARYING. */
4190 if (backedge_name
4191 && sameval == backedge_name
4192 && !SSA_VISITED (backedge_name))
4193 result = PHI_RESULT (phi);
4194 /* If none of the edges was executable keep the value-number at VN_TOP,
4195 if only a single edge is exectuable use its value. */
4196 else if (n_executable <= 1)
4197 result = seen_undef ? seen_undef : sameval;
4198 /* If we saw only undefined values and VN_TOP use one of the
4199 undefined values. */
4200 else if (sameval == VN_TOP)
4201 result = seen_undef ? seen_undef : sameval;
4202 /* First see if it is equivalent to a phi node in this block. We prefer
4203 this as it allows IV elimination - see PRs 66502 and 67167. */
4204 else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
4206 if (!inserted
4207 && TREE_CODE (result) == SSA_NAME
4208 && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
4210 gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
4211 if (dump_file && (dump_flags & TDF_DETAILS))
4213 fprintf (dump_file, "Marking CSEd to PHI node ");
4214 print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
4215 0, TDF_SLIM);
4216 fprintf (dump_file, "\n");
4220 /* If all values are the same use that, unless we've seen undefined
4221 values as well and the value isn't constant.
4222 CCP/copyprop have the same restriction to not remove uninit warnings. */
4223 else if (allsame
4224 && (! seen_undef || is_gimple_min_invariant (sameval)))
4225 result = sameval;
4226 else
4228 result = PHI_RESULT (phi);
4229 /* Only insert PHIs that are varying, for constant value numbers
4230 we mess up equivalences otherwise as we are only comparing
4231 the immediate controlling predicates. */
4232 vn_phi_insert (phi, result, backedges_varying_p);
4233 if (inserted)
4234 *inserted = true;
4237 return set_ssa_val_to (PHI_RESULT (phi), result);
4240 /* Try to simplify RHS using equivalences and constant folding. */
4242 static tree
4243 try_to_simplify (gassign *stmt)
4245 enum tree_code code = gimple_assign_rhs_code (stmt);
4246 tree tem;
4248 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
4249 in this case, there is no point in doing extra work. */
4250 if (code == SSA_NAME)
4251 return NULL_TREE;
4253 /* First try constant folding based on our current lattice. */
4254 mprts_hook = vn_lookup_simplify_result;
4255 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
4256 mprts_hook = NULL;
4257 if (tem
4258 && (TREE_CODE (tem) == SSA_NAME
4259 || is_gimple_min_invariant (tem)))
4260 return tem;
4262 return NULL_TREE;
4265 /* Visit and value number STMT, return true if the value number
4266 changed. */
4268 static bool
4269 visit_stmt (gimple *stmt, bool backedges_varying_p = false)
4271 bool changed = false;
4273 if (dump_file && (dump_flags & TDF_DETAILS))
4275 fprintf (dump_file, "Value numbering stmt = ");
4276 print_gimple_stmt (dump_file, stmt, 0);
4279 if (gimple_code (stmt) == GIMPLE_PHI)
4280 changed = visit_phi (stmt, NULL, backedges_varying_p);
4281 else if (gimple_has_volatile_ops (stmt))
4282 changed = defs_to_varying (stmt);
4283 else if (gassign *ass = dyn_cast <gassign *> (stmt))
4285 enum tree_code code = gimple_assign_rhs_code (ass);
4286 tree lhs = gimple_assign_lhs (ass);
4287 tree rhs1 = gimple_assign_rhs1 (ass);
4288 tree simplified;
4290 /* Shortcut for copies. Simplifying copies is pointless,
4291 since we copy the expression and value they represent. */
4292 if (code == SSA_NAME
4293 && TREE_CODE (lhs) == SSA_NAME)
4295 changed = visit_copy (lhs, rhs1);
4296 goto done;
4298 simplified = try_to_simplify (ass);
4299 if (simplified)
4301 if (dump_file && (dump_flags & TDF_DETAILS))
4303 fprintf (dump_file, "RHS ");
4304 print_gimple_expr (dump_file, ass, 0);
4305 fprintf (dump_file, " simplified to ");
4306 print_generic_expr (dump_file, simplified);
4307 fprintf (dump_file, "\n");
4310 /* Setting value numbers to constants will occasionally
4311 screw up phi congruence because constants are not
4312 uniquely associated with a single ssa name that can be
4313 looked up. */
4314 if (simplified
4315 && is_gimple_min_invariant (simplified)
4316 && TREE_CODE (lhs) == SSA_NAME)
4318 changed = set_ssa_val_to (lhs, simplified);
4319 goto done;
4321 else if (simplified
4322 && TREE_CODE (simplified) == SSA_NAME
4323 && TREE_CODE (lhs) == SSA_NAME)
4325 changed = visit_copy (lhs, simplified);
4326 goto done;
4329 if ((TREE_CODE (lhs) == SSA_NAME
4330 /* We can substitute SSA_NAMEs that are live over
4331 abnormal edges with their constant value. */
4332 && !(gimple_assign_copy_p (ass)
4333 && is_gimple_min_invariant (rhs1))
4334 && !(simplified
4335 && is_gimple_min_invariant (simplified))
4336 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
4337 /* Stores or copies from SSA_NAMEs that are live over
4338 abnormal edges are a problem. */
4339 || (code == SSA_NAME
4340 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
4341 changed = defs_to_varying (ass);
4342 else if (REFERENCE_CLASS_P (lhs)
4343 || DECL_P (lhs))
4344 changed = visit_reference_op_store (lhs, rhs1, ass);
4345 else if (TREE_CODE (lhs) == SSA_NAME)
4347 if ((gimple_assign_copy_p (ass)
4348 && is_gimple_min_invariant (rhs1))
4349 || (simplified
4350 && is_gimple_min_invariant (simplified)))
4352 if (simplified)
4353 changed = set_ssa_val_to (lhs, simplified);
4354 else
4355 changed = set_ssa_val_to (lhs, rhs1);
4357 else
4359 /* Visit the original statement. */
4360 switch (vn_get_stmt_kind (ass))
4362 case VN_NARY:
4363 changed = visit_nary_op (lhs, ass);
4364 break;
4365 case VN_REFERENCE:
4366 changed = visit_reference_op_load (lhs, rhs1, ass);
4367 break;
4368 default:
4369 changed = defs_to_varying (ass);
4370 break;
4374 else
4375 changed = defs_to_varying (ass);
4377 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
4379 tree lhs = gimple_call_lhs (call_stmt);
4380 if (lhs && TREE_CODE (lhs) == SSA_NAME)
4382 /* Try constant folding based on our current lattice. */
4383 tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
4384 vn_valueize);
4385 if (simplified)
4387 if (dump_file && (dump_flags & TDF_DETAILS))
4389 fprintf (dump_file, "call ");
4390 print_gimple_expr (dump_file, call_stmt, 0);
4391 fprintf (dump_file, " simplified to ");
4392 print_generic_expr (dump_file, simplified);
4393 fprintf (dump_file, "\n");
4396 /* Setting value numbers to constants will occasionally
4397 screw up phi congruence because constants are not
4398 uniquely associated with a single ssa name that can be
4399 looked up. */
4400 if (simplified
4401 && is_gimple_min_invariant (simplified))
4403 changed = set_ssa_val_to (lhs, simplified);
4404 if (gimple_vdef (call_stmt))
4405 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
4406 SSA_VAL (gimple_vuse (call_stmt)));
4407 goto done;
4409 else if (simplified
4410 && TREE_CODE (simplified) == SSA_NAME)
4412 changed = visit_copy (lhs, simplified);
4413 if (gimple_vdef (call_stmt))
4414 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
4415 SSA_VAL (gimple_vuse (call_stmt)));
4416 goto done;
4418 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
4420 changed = defs_to_varying (call_stmt);
4421 goto done;
4425 /* Pick up flags from a devirtualization target. */
4426 tree fn = gimple_call_fn (stmt);
4427 int extra_fnflags = 0;
4428 if (fn && TREE_CODE (fn) == SSA_NAME)
4430 fn = SSA_VAL (fn);
4431 if (TREE_CODE (fn) == ADDR_EXPR
4432 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
4433 extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
4435 if (!gimple_call_internal_p (call_stmt)
4436 && (/* Calls to the same function with the same vuse
4437 and the same operands do not necessarily return the same
4438 value, unless they're pure or const. */
4439 ((gimple_call_flags (call_stmt) | extra_fnflags)
4440 & (ECF_PURE | ECF_CONST))
4441 /* If calls have a vdef, subsequent calls won't have
4442 the same incoming vuse. So, if 2 calls with vdef have the
4443 same vuse, we know they're not subsequent.
4444 We can value number 2 calls to the same function with the
4445 same vuse and the same operands which are not subsequent
4446 the same, because there is no code in the program that can
4447 compare the 2 values... */
4448 || (gimple_vdef (call_stmt)
4449 /* ... unless the call returns a pointer which does
4450 not alias with anything else. In which case the
4451 information that the values are distinct are encoded
4452 in the IL. */
4453 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
4454 /* Only perform the following when being called from PRE
4455 which embeds tail merging. */
4456 && default_vn_walk_kind == VN_WALK)))
4457 changed = visit_reference_op_call (lhs, call_stmt);
4458 else
4459 changed = defs_to_varying (call_stmt);
4461 else
4462 changed = defs_to_varying (stmt);
4463 done:
4464 return changed;
4468 /* Allocate a value number table. */
4470 static void
4471 allocate_vn_table (vn_tables_t table, unsigned size)
4473 table->phis = new vn_phi_table_type (size);
4474 table->nary = new vn_nary_op_table_type (size);
4475 table->references = new vn_reference_table_type (size);
4478 /* Free a value number table. */
4480 static void
4481 free_vn_table (vn_tables_t table)
4483 /* Walk over elements and release vectors. */
4484 vn_reference_iterator_type hir;
4485 vn_reference_t vr;
4486 FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
4487 vr->operands.release ();
4488 delete table->phis;
4489 table->phis = NULL;
4490 delete table->nary;
4491 table->nary = NULL;
4492 delete table->references;
4493 table->references = NULL;
4496 /* Set *ID according to RESULT. */
4498 static void
4499 set_value_id_for_result (tree result, unsigned int *id)
4501 if (result && TREE_CODE (result) == SSA_NAME)
4502 *id = VN_INFO (result)->value_id;
4503 else if (result && is_gimple_min_invariant (result))
4504 *id = get_or_alloc_constant_value_id (result);
4505 else
4506 *id = get_next_value_id ();
4509 /* Set the value ids in the valid hash tables. */
4511 static void
4512 set_hashtable_value_ids (void)
4514 vn_nary_op_iterator_type hin;
4515 vn_phi_iterator_type hip;
4516 vn_reference_iterator_type hir;
4517 vn_nary_op_t vno;
4518 vn_reference_t vr;
4519 vn_phi_t vp;
4521 /* Now set the value ids of the things we had put in the hash
4522 table. */
4524 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4525 if (! vno->predicated_values)
4526 set_value_id_for_result (vno->u.result, &vno->value_id);
4528 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4529 set_value_id_for_result (vp->result, &vp->value_id);
4531 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4532 hir)
4533 set_value_id_for_result (vr->result, &vr->value_id);
4536 /* Return the maximum value id we have ever seen. */
4538 unsigned int
4539 get_max_value_id (void)
4541 return next_value_id;
4544 /* Return the next unique value id. */
4546 unsigned int
4547 get_next_value_id (void)
4549 return next_value_id++;
4553 /* Compare two expressions E1 and E2 and return true if they are equal. */
4555 bool
4556 expressions_equal_p (tree e1, tree e2)
4558 /* The obvious case. */
4559 if (e1 == e2)
4560 return true;
4562 /* If either one is VN_TOP consider them equal. */
4563 if (e1 == VN_TOP || e2 == VN_TOP)
4564 return true;
4566 /* If only one of them is null, they cannot be equal. */
4567 if (!e1 || !e2)
4568 return false;
4570 /* Now perform the actual comparison. */
4571 if (TREE_CODE (e1) == TREE_CODE (e2)
4572 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4573 return true;
4575 return false;
4579 /* Return true if the nary operation NARY may trap. This is a copy
4580 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4582 bool
4583 vn_nary_may_trap (vn_nary_op_t nary)
4585 tree type;
4586 tree rhs2 = NULL_TREE;
4587 bool honor_nans = false;
4588 bool honor_snans = false;
4589 bool fp_operation = false;
4590 bool honor_trapv = false;
4591 bool handled, ret;
4592 unsigned i;
4594 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4595 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4596 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4598 type = nary->type;
4599 fp_operation = FLOAT_TYPE_P (type);
4600 if (fp_operation)
4602 honor_nans = flag_trapping_math && !flag_finite_math_only;
4603 honor_snans = flag_signaling_nans != 0;
4605 else if (INTEGRAL_TYPE_P (type)
4606 && TYPE_OVERFLOW_TRAPS (type))
4607 honor_trapv = true;
4609 if (nary->length >= 2)
4610 rhs2 = nary->op[1];
4611 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4612 honor_trapv,
4613 honor_nans, honor_snans, rhs2,
4614 &handled);
4615 if (handled
4616 && ret)
4617 return true;
4619 for (i = 0; i < nary->length; ++i)
4620 if (tree_could_trap_p (nary->op[i]))
4621 return true;
4623 return false;
4627 class eliminate_dom_walker : public dom_walker
4629 public:
4630 eliminate_dom_walker (cdi_direction, bitmap);
4631 ~eliminate_dom_walker ();
4633 virtual edge before_dom_children (basic_block);
4634 virtual void after_dom_children (basic_block);
4636 virtual tree eliminate_avail (basic_block, tree op);
4637 virtual void eliminate_push_avail (basic_block, tree op);
4638 tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
4640 void eliminate_stmt (basic_block, gimple_stmt_iterator *);
4642 unsigned eliminate_cleanup (bool region_p = false);
4644 bool do_pre;
4645 unsigned int el_todo;
4646 unsigned int eliminations;
4647 unsigned int insertions;
4649 /* SSA names that had their defs inserted by PRE if do_pre. */
4650 bitmap inserted_exprs;
4652 /* Blocks with statements that have had their EH properties changed. */
4653 bitmap need_eh_cleanup;
4655 /* Blocks with statements that have had their AB properties changed. */
4656 bitmap need_ab_cleanup;
4658 /* Local state for the eliminate domwalk. */
4659 auto_vec<gimple *> to_remove;
4660 auto_vec<gimple *> to_fixup;
4661 auto_vec<tree> avail;
4662 auto_vec<tree> avail_stack;
4665 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
4666 bitmap inserted_exprs_)
4667 : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
4668 el_todo (0), eliminations (0), insertions (0),
4669 inserted_exprs (inserted_exprs_)
4671 need_eh_cleanup = BITMAP_ALLOC (NULL);
4672 need_ab_cleanup = BITMAP_ALLOC (NULL);
4675 eliminate_dom_walker::~eliminate_dom_walker ()
4677 BITMAP_FREE (need_eh_cleanup);
4678 BITMAP_FREE (need_ab_cleanup);
4681 /* Return a leader for OP that is available at the current point of the
4682 eliminate domwalk. */
4684 tree
4685 eliminate_dom_walker::eliminate_avail (basic_block, tree op)
4687 tree valnum = VN_INFO (op)->valnum;
4688 if (TREE_CODE (valnum) == SSA_NAME)
4690 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
4691 return valnum;
4692 if (avail.length () > SSA_NAME_VERSION (valnum))
4693 return avail[SSA_NAME_VERSION (valnum)];
4695 else if (is_gimple_min_invariant (valnum))
4696 return valnum;
4697 return NULL_TREE;
4700 /* At the current point of the eliminate domwalk make OP available. */
4702 void
4703 eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
4705 tree valnum = VN_INFO (op)->valnum;
4706 if (TREE_CODE (valnum) == SSA_NAME)
4708 if (avail.length () <= SSA_NAME_VERSION (valnum))
4709 avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
4710 tree pushop = op;
4711 if (avail[SSA_NAME_VERSION (valnum)])
4712 pushop = avail[SSA_NAME_VERSION (valnum)];
4713 avail_stack.safe_push (pushop);
4714 avail[SSA_NAME_VERSION (valnum)] = op;
4718 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
4719 the leader for the expression if insertion was successful. */
4721 tree
4722 eliminate_dom_walker::eliminate_insert (basic_block bb,
4723 gimple_stmt_iterator *gsi, tree val)
4725 /* We can insert a sequence with a single assignment only. */
4726 gimple_seq stmts = VN_INFO (val)->expr;
4727 if (!gimple_seq_singleton_p (stmts))
4728 return NULL_TREE;
4729 gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
4730 if (!stmt
4731 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
4732 && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
4733 && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
4734 && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
4735 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
4736 return NULL_TREE;
4738 tree op = gimple_assign_rhs1 (stmt);
4739 if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
4740 || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
4741 op = TREE_OPERAND (op, 0);
4742 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
4743 if (!leader)
4744 return NULL_TREE;
4746 tree res;
4747 stmts = NULL;
4748 if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
4749 res = gimple_build (&stmts, BIT_FIELD_REF,
4750 TREE_TYPE (val), leader,
4751 TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
4752 TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
4753 else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
4754 res = gimple_build (&stmts, BIT_AND_EXPR,
4755 TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
4756 else
4757 res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
4758 TREE_TYPE (val), leader);
4759 if (TREE_CODE (res) != SSA_NAME
4760 || SSA_NAME_IS_DEFAULT_DEF (res)
4761 || gimple_bb (SSA_NAME_DEF_STMT (res)))
4763 gimple_seq_discard (stmts);
4765 /* During propagation we have to treat SSA info conservatively
4766 and thus we can end up simplifying the inserted expression
4767 at elimination time to sth not defined in stmts. */
4768 /* But then this is a redundancy we failed to detect. Which means
4769 res now has two values. That doesn't play well with how
4770 we track availability here, so give up. */
4771 if (dump_file && (dump_flags & TDF_DETAILS))
4773 if (TREE_CODE (res) == SSA_NAME)
4774 res = eliminate_avail (bb, res);
4775 if (res)
4777 fprintf (dump_file, "Failed to insert expression for value ");
4778 print_generic_expr (dump_file, val);
4779 fprintf (dump_file, " which is really fully redundant to ");
4780 print_generic_expr (dump_file, res);
4781 fprintf (dump_file, "\n");
4785 return NULL_TREE;
4787 else
4789 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4790 VN_INFO (res)->valnum = val;
4791 VN_INFO (res)->visited = true;
4794 insertions++;
4795 if (dump_file && (dump_flags & TDF_DETAILS))
4797 fprintf (dump_file, "Inserted ");
4798 print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
4801 return res;
4804 void
4805 eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
4807 tree sprime = NULL_TREE;
4808 gimple *stmt = gsi_stmt (*gsi);
4809 tree lhs = gimple_get_lhs (stmt);
4810 if (lhs && TREE_CODE (lhs) == SSA_NAME
4811 && !gimple_has_volatile_ops (stmt)
4812 /* See PR43491. Do not replace a global register variable when
4813 it is a the RHS of an assignment. Do replace local register
4814 variables since gcc does not guarantee a local variable will
4815 be allocated in register.
4816 ??? The fix isn't effective here. This should instead
4817 be ensured by not value-numbering them the same but treating
4818 them like volatiles? */
4819 && !(gimple_assign_single_p (stmt)
4820 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
4821 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
4822 && is_global_var (gimple_assign_rhs1 (stmt)))))
4824 sprime = eliminate_avail (b, lhs);
4825 if (!sprime)
4827 /* If there is no existing usable leader but SCCVN thinks
4828 it has an expression it wants to use as replacement,
4829 insert that. */
4830 tree val = VN_INFO (lhs)->valnum;
4831 if (val != VN_TOP
4832 && TREE_CODE (val) == SSA_NAME
4833 && VN_INFO (val)->needs_insertion
4834 && VN_INFO (val)->expr != NULL
4835 && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
4836 eliminate_push_avail (b, sprime);
4839 /* If this now constitutes a copy duplicate points-to
4840 and range info appropriately. This is especially
4841 important for inserted code. See tree-ssa-copy.c
4842 for similar code. */
4843 if (sprime
4844 && TREE_CODE (sprime) == SSA_NAME)
4846 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
4847 if (POINTER_TYPE_P (TREE_TYPE (lhs))
4848 && SSA_NAME_PTR_INFO (lhs)
4849 && ! SSA_NAME_PTR_INFO (sprime))
4851 duplicate_ssa_name_ptr_info (sprime,
4852 SSA_NAME_PTR_INFO (lhs));
4853 if (b != sprime_b)
4854 mark_ptr_info_alignment_unknown
4855 (SSA_NAME_PTR_INFO (sprime));
4857 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4858 && SSA_NAME_RANGE_INFO (lhs)
4859 && ! SSA_NAME_RANGE_INFO (sprime)
4860 && b == sprime_b)
4861 duplicate_ssa_name_range_info (sprime,
4862 SSA_NAME_RANGE_TYPE (lhs),
4863 SSA_NAME_RANGE_INFO (lhs));
4866 /* Inhibit the use of an inserted PHI on a loop header when
4867 the address of the memory reference is a simple induction
4868 variable. In other cases the vectorizer won't do anything
4869 anyway (either it's loop invariant or a complicated
4870 expression). */
4871 if (sprime
4872 && TREE_CODE (sprime) == SSA_NAME
4873 && do_pre
4874 && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
4875 && loop_outer (b->loop_father)
4876 && has_zero_uses (sprime)
4877 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
4878 && gimple_assign_load_p (stmt))
4880 gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
4881 basic_block def_bb = gimple_bb (def_stmt);
4882 if (gimple_code (def_stmt) == GIMPLE_PHI
4883 && def_bb->loop_father->header == def_bb)
4885 loop_p loop = def_bb->loop_father;
4886 ssa_op_iter iter;
4887 tree op;
4888 bool found = false;
4889 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4891 affine_iv iv;
4892 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
4893 if (def_bb
4894 && flow_bb_inside_loop_p (loop, def_bb)
4895 && simple_iv (loop, loop, op, &iv, true))
4897 found = true;
4898 break;
4901 if (found)
4903 if (dump_file && (dump_flags & TDF_DETAILS))
4905 fprintf (dump_file, "Not replacing ");
4906 print_gimple_expr (dump_file, stmt, 0);
4907 fprintf (dump_file, " with ");
4908 print_generic_expr (dump_file, sprime);
4909 fprintf (dump_file, " which would add a loop"
4910 " carried dependence to loop %d\n",
4911 loop->num);
4913 /* Don't keep sprime available. */
4914 sprime = NULL_TREE;
4919 if (sprime)
4921 /* If we can propagate the value computed for LHS into
4922 all uses don't bother doing anything with this stmt. */
4923 if (may_propagate_copy (lhs, sprime))
4925 /* Mark it for removal. */
4926 to_remove.safe_push (stmt);
4928 /* ??? Don't count copy/constant propagations. */
4929 if (gimple_assign_single_p (stmt)
4930 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4931 || gimple_assign_rhs1 (stmt) == sprime))
4932 return;
4934 if (dump_file && (dump_flags & TDF_DETAILS))
4936 fprintf (dump_file, "Replaced ");
4937 print_gimple_expr (dump_file, stmt, 0);
4938 fprintf (dump_file, " with ");
4939 print_generic_expr (dump_file, sprime);
4940 fprintf (dump_file, " in all uses of ");
4941 print_gimple_stmt (dump_file, stmt, 0);
4944 eliminations++;
4945 return;
4948 /* If this is an assignment from our leader (which
4949 happens in the case the value-number is a constant)
4950 then there is nothing to do. */
4951 if (gimple_assign_single_p (stmt)
4952 && sprime == gimple_assign_rhs1 (stmt))
4953 return;
4955 /* Else replace its RHS. */
4956 bool can_make_abnormal_goto
4957 = is_gimple_call (stmt)
4958 && stmt_can_make_abnormal_goto (stmt);
4960 if (dump_file && (dump_flags & TDF_DETAILS))
4962 fprintf (dump_file, "Replaced ");
4963 print_gimple_expr (dump_file, stmt, 0);
4964 fprintf (dump_file, " with ");
4965 print_generic_expr (dump_file, sprime);
4966 fprintf (dump_file, " in ");
4967 print_gimple_stmt (dump_file, stmt, 0);
4970 eliminations++;
4971 gimple *orig_stmt = stmt;
4972 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4973 TREE_TYPE (sprime)))
4974 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4975 tree vdef = gimple_vdef (stmt);
4976 tree vuse = gimple_vuse (stmt);
4977 propagate_tree_value_into_stmt (gsi, sprime);
4978 stmt = gsi_stmt (*gsi);
4979 update_stmt (stmt);
4980 /* In case the VDEF on the original stmt was released, value-number
4981 it to the VUSE. This is to make vuse_ssa_val able to skip
4982 released virtual operands. */
4983 if (vdef != gimple_vdef (stmt))
4985 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
4986 VN_INFO (vdef)->valnum = vuse;
4989 /* If we removed EH side-effects from the statement, clean
4990 its EH information. */
4991 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4993 bitmap_set_bit (need_eh_cleanup,
4994 gimple_bb (stmt)->index);
4995 if (dump_file && (dump_flags & TDF_DETAILS))
4996 fprintf (dump_file, " Removed EH side-effects.\n");
4999 /* Likewise for AB side-effects. */
5000 if (can_make_abnormal_goto
5001 && !stmt_can_make_abnormal_goto (stmt))
5003 bitmap_set_bit (need_ab_cleanup,
5004 gimple_bb (stmt)->index);
5005 if (dump_file && (dump_flags & TDF_DETAILS))
5006 fprintf (dump_file, " Removed AB side-effects.\n");
5009 return;
5013 /* If the statement is a scalar store, see if the expression
5014 has the same value number as its rhs. If so, the store is
5015 dead. */
5016 if (gimple_assign_single_p (stmt)
5017 && !gimple_has_volatile_ops (stmt)
5018 && !is_gimple_reg (gimple_assign_lhs (stmt))
5019 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
5020 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
5022 tree val;
5023 tree rhs = gimple_assign_rhs1 (stmt);
5024 vn_reference_t vnresult;
5025 val = vn_reference_lookup (lhs, gimple_vuse (stmt), VN_WALKREWRITE,
5026 &vnresult, false);
5027 if (TREE_CODE (rhs) == SSA_NAME)
5028 rhs = VN_INFO (rhs)->valnum;
5029 if (val
5030 && operand_equal_p (val, rhs, 0))
5032 /* We can only remove the later store if the former aliases
5033 at least all accesses the later one does or if the store
5034 was to readonly memory storing the same value. */
5035 alias_set_type set = get_alias_set (lhs);
5036 if (! vnresult
5037 || vnresult->set == set
5038 || alias_set_subset_of (set, vnresult->set))
5040 if (dump_file && (dump_flags & TDF_DETAILS))
5042 fprintf (dump_file, "Deleted redundant store ");
5043 print_gimple_stmt (dump_file, stmt, 0);
5046 /* Queue stmt for removal. */
5047 to_remove.safe_push (stmt);
5048 return;
5053 /* If this is a control statement value numbering left edges
5054 unexecuted on force the condition in a way consistent with
5055 that. */
5056 if (gcond *cond = dyn_cast <gcond *> (stmt))
5058 if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
5059 ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
5061 if (dump_file && (dump_flags & TDF_DETAILS))
5063 fprintf (dump_file, "Removing unexecutable edge from ");
5064 print_gimple_stmt (dump_file, stmt, 0);
5066 if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
5067 == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
5068 gimple_cond_make_true (cond);
5069 else
5070 gimple_cond_make_false (cond);
5071 update_stmt (cond);
5072 el_todo |= TODO_cleanup_cfg;
5073 return;
5077 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
5078 bool was_noreturn = (is_gimple_call (stmt)
5079 && gimple_call_noreturn_p (stmt));
5080 tree vdef = gimple_vdef (stmt);
5081 tree vuse = gimple_vuse (stmt);
5083 /* If we didn't replace the whole stmt (or propagate the result
5084 into all uses), replace all uses on this stmt with their
5085 leaders. */
5086 bool modified = false;
5087 use_operand_p use_p;
5088 ssa_op_iter iter;
5089 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
5091 tree use = USE_FROM_PTR (use_p);
5092 /* ??? The call code above leaves stmt operands un-updated. */
5093 if (TREE_CODE (use) != SSA_NAME)
5094 continue;
5095 tree sprime;
5096 if (SSA_NAME_IS_DEFAULT_DEF (use))
5097 /* ??? For default defs BB shouldn't matter, but we have to
5098 solve the inconsistency between rpo eliminate and
5099 dom eliminate avail valueization first. */
5100 sprime = eliminate_avail (b, use);
5101 else
5102 /* Look for sth available at the definition block of the argument.
5103 This avoids inconsistencies between availability there which
5104 decides if the stmt can be removed and availability at the
5105 use site. The SSA property ensures that things available
5106 at the definition are also available at uses. */
5107 sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
5108 if (sprime && sprime != use
5109 && may_propagate_copy (use, sprime)
5110 /* We substitute into debug stmts to avoid excessive
5111 debug temporaries created by removed stmts, but we need
5112 to avoid doing so for inserted sprimes as we never want
5113 to create debug temporaries for them. */
5114 && (!inserted_exprs
5115 || TREE_CODE (sprime) != SSA_NAME
5116 || !is_gimple_debug (stmt)
5117 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
5119 propagate_value (use_p, sprime);
5120 modified = true;
5124 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
5125 into which is a requirement for the IPA devirt machinery. */
5126 gimple *old_stmt = stmt;
5127 if (modified)
5129 /* If a formerly non-invariant ADDR_EXPR is turned into an
5130 invariant one it was on a separate stmt. */
5131 if (gimple_assign_single_p (stmt)
5132 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
5133 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
5134 gimple_stmt_iterator prev = *gsi;
5135 gsi_prev (&prev);
5136 if (fold_stmt (gsi))
5138 /* fold_stmt may have created new stmts inbetween
5139 the previous stmt and the folded stmt. Mark
5140 all defs created there as varying to not confuse
5141 the SCCVN machinery as we're using that even during
5142 elimination. */
5143 if (gsi_end_p (prev))
5144 prev = gsi_start_bb (b);
5145 else
5146 gsi_next (&prev);
5147 if (gsi_stmt (prev) != gsi_stmt (*gsi))
5150 tree def;
5151 ssa_op_iter dit;
5152 FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
5153 dit, SSA_OP_ALL_DEFS)
5154 /* As existing DEFs may move between stmts
5155 only process new ones. */
5156 if (! has_VN_INFO (def))
5158 VN_INFO (def)->valnum = def;
5159 VN_INFO (def)->visited = true;
5161 if (gsi_stmt (prev) == gsi_stmt (*gsi))
5162 break;
5163 gsi_next (&prev);
5165 while (1);
5167 stmt = gsi_stmt (*gsi);
5168 /* In case we folded the stmt away schedule the NOP for removal. */
5169 if (gimple_nop_p (stmt))
5170 to_remove.safe_push (stmt);
5173 /* Visit indirect calls and turn them into direct calls if
5174 possible using the devirtualization machinery. Do this before
5175 checking for required EH/abnormal/noreturn cleanup as devird
5176 may expose more of those. */
5177 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
5179 tree fn = gimple_call_fn (call_stmt);
5180 if (fn
5181 && flag_devirtualize
5182 && virtual_method_call_p (fn))
5184 tree otr_type = obj_type_ref_class (fn);
5185 unsigned HOST_WIDE_INT otr_tok
5186 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
5187 tree instance;
5188 ipa_polymorphic_call_context context (current_function_decl,
5189 fn, stmt, &instance);
5190 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
5191 otr_type, stmt);
5192 bool final;
5193 vec <cgraph_node *> targets
5194 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
5195 otr_tok, context, &final);
5196 if (dump_file)
5197 dump_possible_polymorphic_call_targets (dump_file,
5198 obj_type_ref_class (fn),
5199 otr_tok, context);
5200 if (final && targets.length () <= 1 && dbg_cnt (devirt))
5202 tree fn;
5203 if (targets.length () == 1)
5204 fn = targets[0]->decl;
5205 else
5206 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
5207 if (dump_enabled_p ())
5209 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
5210 "converting indirect call to "
5211 "function %s\n",
5212 lang_hooks.decl_printable_name (fn, 2));
5214 gimple_call_set_fndecl (call_stmt, fn);
5215 /* If changing the call to __builtin_unreachable
5216 or similar noreturn function, adjust gimple_call_fntype
5217 too. */
5218 if (gimple_call_noreturn_p (call_stmt)
5219 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
5220 && TYPE_ARG_TYPES (TREE_TYPE (fn))
5221 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
5222 == void_type_node))
5223 gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
5224 maybe_remove_unused_call_args (cfun, call_stmt);
5225 modified = true;
5230 if (modified)
5232 /* When changing a call into a noreturn call, cfg cleanup
5233 is needed to fix up the noreturn call. */
5234 if (!was_noreturn
5235 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
5236 to_fixup.safe_push (stmt);
5237 /* When changing a condition or switch into one we know what
5238 edge will be executed, schedule a cfg cleanup. */
5239 if ((gimple_code (stmt) == GIMPLE_COND
5240 && (gimple_cond_true_p (as_a <gcond *> (stmt))
5241 || gimple_cond_false_p (as_a <gcond *> (stmt))))
5242 || (gimple_code (stmt) == GIMPLE_SWITCH
5243 && TREE_CODE (gimple_switch_index
5244 (as_a <gswitch *> (stmt))) == INTEGER_CST))
5245 el_todo |= TODO_cleanup_cfg;
5246 /* If we removed EH side-effects from the statement, clean
5247 its EH information. */
5248 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
5250 bitmap_set_bit (need_eh_cleanup,
5251 gimple_bb (stmt)->index);
5252 if (dump_file && (dump_flags & TDF_DETAILS))
5253 fprintf (dump_file, " Removed EH side-effects.\n");
5255 /* Likewise for AB side-effects. */
5256 if (can_make_abnormal_goto
5257 && !stmt_can_make_abnormal_goto (stmt))
5259 bitmap_set_bit (need_ab_cleanup,
5260 gimple_bb (stmt)->index);
5261 if (dump_file && (dump_flags & TDF_DETAILS))
5262 fprintf (dump_file, " Removed AB side-effects.\n");
5264 update_stmt (stmt);
5265 /* In case the VDEF on the original stmt was released, value-number
5266 it to the VUSE. This is to make vuse_ssa_val able to skip
5267 released virtual operands. */
5268 if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
5269 VN_INFO (vdef)->valnum = vuse;
5272 /* Make new values available - for fully redundant LHS we
5273 continue with the next stmt above and skip this. */
5274 def_operand_p defp;
5275 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
5276 eliminate_push_avail (b, DEF_FROM_PTR (defp));
5279 /* Perform elimination for the basic-block B during the domwalk. */
5281 edge
5282 eliminate_dom_walker::before_dom_children (basic_block b)
5284 /* Mark new bb. */
5285 avail_stack.safe_push (NULL_TREE);
5287 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
5288 if (!(b->flags & BB_EXECUTABLE))
5289 return NULL;
5291 vn_context_bb = b;
5293 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
5295 gphi *phi = gsi.phi ();
5296 tree res = PHI_RESULT (phi);
5298 if (virtual_operand_p (res))
5300 gsi_next (&gsi);
5301 continue;
5304 tree sprime = eliminate_avail (b, res);
5305 if (sprime
5306 && sprime != res)
5308 if (dump_file && (dump_flags & TDF_DETAILS))
5310 fprintf (dump_file, "Replaced redundant PHI node defining ");
5311 print_generic_expr (dump_file, res);
5312 fprintf (dump_file, " with ");
5313 print_generic_expr (dump_file, sprime);
5314 fprintf (dump_file, "\n");
5317 /* If we inserted this PHI node ourself, it's not an elimination. */
5318 if (! inserted_exprs
5319 || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
5320 eliminations++;
5322 /* If we will propagate into all uses don't bother to do
5323 anything. */
5324 if (may_propagate_copy (res, sprime))
5326 /* Mark the PHI for removal. */
5327 to_remove.safe_push (phi);
5328 gsi_next (&gsi);
5329 continue;
5332 remove_phi_node (&gsi, false);
5334 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
5335 sprime = fold_convert (TREE_TYPE (res), sprime);
5336 gimple *stmt = gimple_build_assign (res, sprime);
5337 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
5338 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
5339 continue;
5342 eliminate_push_avail (b, res);
5343 gsi_next (&gsi);
5346 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
5347 !gsi_end_p (gsi);
5348 gsi_next (&gsi))
5349 eliminate_stmt (b, &gsi);
5351 /* Replace destination PHI arguments. */
5352 edge_iterator ei;
5353 edge e;
5354 FOR_EACH_EDGE (e, ei, b->succs)
5355 if (e->flags & EDGE_EXECUTABLE)
5356 for (gphi_iterator gsi = gsi_start_phis (e->dest);
5357 !gsi_end_p (gsi);
5358 gsi_next (&gsi))
5360 gphi *phi = gsi.phi ();
5361 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
5362 tree arg = USE_FROM_PTR (use_p);
5363 if (TREE_CODE (arg) != SSA_NAME
5364 || virtual_operand_p (arg))
5365 continue;
5366 tree sprime = eliminate_avail (b, arg);
5367 if (sprime && may_propagate_copy (arg, sprime))
5368 propagate_value (use_p, sprime);
5371 vn_context_bb = NULL;
5373 return NULL;
5376 /* Make no longer available leaders no longer available. */
5378 void
5379 eliminate_dom_walker::after_dom_children (basic_block)
5381 tree entry;
5382 while ((entry = avail_stack.pop ()) != NULL_TREE)
5384 tree valnum = VN_INFO (entry)->valnum;
5385 tree old = avail[SSA_NAME_VERSION (valnum)];
5386 if (old == entry)
5387 avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
5388 else
5389 avail[SSA_NAME_VERSION (valnum)] = entry;
5393 /* Remove queued stmts and perform delayed cleanups. */
5395 unsigned
5396 eliminate_dom_walker::eliminate_cleanup (bool region_p)
5398 statistics_counter_event (cfun, "Eliminated", eliminations);
5399 statistics_counter_event (cfun, "Insertions", insertions);
5401 /* We cannot remove stmts during BB walk, especially not release SSA
5402 names there as this confuses the VN machinery. The stmts ending
5403 up in to_remove are either stores or simple copies.
5404 Remove stmts in reverse order to make debug stmt creation possible. */
5405 while (!to_remove.is_empty ())
5407 bool do_release_defs = true;
5408 gimple *stmt = to_remove.pop ();
5410 /* When we are value-numbering a region we do not require exit PHIs to
5411 be present so we have to make sure to deal with uses outside of the
5412 region of stmts that we thought are eliminated.
5413 ??? Note we may be confused by uses in dead regions we didn't run
5414 elimination on. Rather than checking individual uses we accept
5415 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
5416 contains such example). */
5417 if (region_p)
5419 if (gphi *phi = dyn_cast <gphi *> (stmt))
5421 tree lhs = gimple_phi_result (phi);
5422 if (!has_zero_uses (lhs))
5424 if (dump_file && (dump_flags & TDF_DETAILS))
5425 fprintf (dump_file, "Keeping eliminated stmt live "
5426 "as copy because of out-of-region uses\n");
5427 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
5428 gimple *copy = gimple_build_assign (lhs, sprime);
5429 gimple_stmt_iterator gsi
5430 = gsi_after_labels (gimple_bb (stmt));
5431 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
5432 do_release_defs = false;
5435 else if (tree lhs = gimple_get_lhs (stmt))
5436 if (TREE_CODE (lhs) == SSA_NAME
5437 && !has_zero_uses (lhs))
5439 if (dump_file && (dump_flags & TDF_DETAILS))
5440 fprintf (dump_file, "Keeping eliminated stmt live "
5441 "as copy because of out-of-region uses\n");
5442 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
5443 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5444 if (is_gimple_assign (stmt))
5446 gimple_assign_set_rhs_from_tree (&gsi, sprime);
5447 update_stmt (gsi_stmt (gsi));
5448 continue;
5450 else
5452 gimple *copy = gimple_build_assign (lhs, sprime);
5453 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
5454 do_release_defs = false;
5459 if (dump_file && (dump_flags & TDF_DETAILS))
5461 fprintf (dump_file, "Removing dead stmt ");
5462 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
5465 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5466 if (gimple_code (stmt) == GIMPLE_PHI)
5467 remove_phi_node (&gsi, do_release_defs);
5468 else
5470 basic_block bb = gimple_bb (stmt);
5471 unlink_stmt_vdef (stmt);
5472 if (gsi_remove (&gsi, true))
5473 bitmap_set_bit (need_eh_cleanup, bb->index);
5474 if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
5475 bitmap_set_bit (need_ab_cleanup, bb->index);
5476 if (do_release_defs)
5477 release_defs (stmt);
5480 /* Removing a stmt may expose a forwarder block. */
5481 el_todo |= TODO_cleanup_cfg;
5484 /* Fixup stmts that became noreturn calls. This may require splitting
5485 blocks and thus isn't possible during the dominator walk. Do this
5486 in reverse order so we don't inadvertedly remove a stmt we want to
5487 fixup by visiting a dominating now noreturn call first. */
5488 while (!to_fixup.is_empty ())
5490 gimple *stmt = to_fixup.pop ();
5492 if (dump_file && (dump_flags & TDF_DETAILS))
5494 fprintf (dump_file, "Fixing up noreturn call ");
5495 print_gimple_stmt (dump_file, stmt, 0);
5498 if (fixup_noreturn_call (stmt))
5499 el_todo |= TODO_cleanup_cfg;
5502 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
5503 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
5505 if (do_eh_cleanup)
5506 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
5508 if (do_ab_cleanup)
5509 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
5511 if (do_eh_cleanup || do_ab_cleanup)
5512 el_todo |= TODO_cleanup_cfg;
5514 return el_todo;
5517 /* Eliminate fully redundant computations. */
5519 unsigned
5520 eliminate_with_rpo_vn (bitmap inserted_exprs)
5522 eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
5524 walker.walk (cfun->cfg->x_entry_block_ptr);
5525 return walker.eliminate_cleanup ();
5528 static unsigned
5529 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
5530 bool iterate, bool eliminate);
5532 void
5533 run_rpo_vn (vn_lookup_kind kind)
5535 default_vn_walk_kind = kind;
5536 do_rpo_vn (cfun, NULL, NULL, true, false);
5538 /* ??? Prune requirement of these. */
5539 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
5540 constant_value_ids = BITMAP_ALLOC (NULL);
5542 /* Initialize the value ids and prune out remaining VN_TOPs
5543 from dead code. */
5544 tree name;
5545 unsigned i;
5546 FOR_EACH_SSA_NAME (i, name, cfun)
5548 vn_ssa_aux_t info = VN_INFO (name);
5549 if (!info->visited
5550 || info->valnum == VN_TOP)
5551 info->valnum = name;
5552 if (info->valnum == name)
5553 info->value_id = get_next_value_id ();
5554 else if (is_gimple_min_invariant (info->valnum))
5555 info->value_id = get_or_alloc_constant_value_id (info->valnum);
5558 /* Propagate. */
5559 FOR_EACH_SSA_NAME (i, name, cfun)
5561 vn_ssa_aux_t info = VN_INFO (name);
5562 if (TREE_CODE (info->valnum) == SSA_NAME
5563 && info->valnum != name
5564 && info->value_id != VN_INFO (info->valnum)->value_id)
5565 info->value_id = VN_INFO (info->valnum)->value_id;
5568 set_hashtable_value_ids ();
5570 if (dump_file && (dump_flags & TDF_DETAILS))
5572 fprintf (dump_file, "Value numbers:\n");
5573 FOR_EACH_SSA_NAME (i, name, cfun)
5575 if (VN_INFO (name)->visited
5576 && SSA_VAL (name) != name)
5578 print_generic_expr (dump_file, name);
5579 fprintf (dump_file, " = ");
5580 print_generic_expr (dump_file, SSA_VAL (name));
5581 fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
5587 /* Free VN associated data structures. */
5589 void
5590 free_rpo_vn (void)
5592 free_vn_table (valid_info);
5593 XDELETE (valid_info);
5594 obstack_free (&vn_tables_obstack, NULL);
5595 obstack_free (&vn_tables_insert_obstack, NULL);
5597 vn_ssa_aux_iterator_type it;
5598 vn_ssa_aux_t info;
5599 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
5600 if (info->needs_insertion)
5601 release_ssa_name (info->name);
5602 obstack_free (&vn_ssa_aux_obstack, NULL);
5603 delete vn_ssa_aux_hash;
5605 delete constant_to_value_id;
5606 constant_to_value_id = NULL;
5607 BITMAP_FREE (constant_value_ids);
5610 /* Adaptor to the elimination engine using RPO availability. */
5612 class rpo_elim : public eliminate_dom_walker
5614 public:
5615 rpo_elim(basic_block entry_)
5616 : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_) {}
5617 ~rpo_elim();
5619 virtual tree eliminate_avail (basic_block, tree op);
5621 virtual void eliminate_push_avail (basic_block, tree);
5623 basic_block entry;
5624 /* Instead of having a local availability lattice for each
5625 basic-block and availability at X defined as union of
5626 the local availabilities at X and its dominators we're
5627 turning this upside down and track availability per
5628 value given values are usually made available at very
5629 few points (at least one).
5630 So we have a value -> vec<location, leader> map where
5631 LOCATION is specifying the basic-block LEADER is made
5632 available for VALUE. We push to this vector in RPO
5633 order thus for iteration we can simply pop the last
5634 entries.
5635 LOCATION is the basic-block index and LEADER is its
5636 SSA name version. */
5637 /* ??? We'd like to use auto_vec here with embedded storage
5638 but that doesn't play well until we can provide move
5639 constructors and use std::move on hash-table expansion.
5640 So for now this is a bit more expensive than necessary.
5641 We eventually want to switch to a chaining scheme like
5642 for hashtable entries for unwinding which would make
5643 making the vector part of the vn_ssa_aux structure possible. */
5644 typedef hash_map<tree, vec<std::pair<int, int> > > rpo_avail_t;
5645 rpo_avail_t m_rpo_avail;
5648 /* Global RPO state for access from hooks. */
5649 static rpo_elim *rpo_avail;
5651 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
5653 static tree
5654 vn_lookup_simplify_result (gimple_match_op *res_op)
5656 if (!res_op->code.is_tree_code ())
5657 return NULL_TREE;
5658 tree *ops = res_op->ops;
5659 unsigned int length = res_op->num_ops;
5660 if (res_op->code == CONSTRUCTOR
5661 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
5662 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
5663 && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
5665 length = CONSTRUCTOR_NELTS (res_op->ops[0]);
5666 ops = XALLOCAVEC (tree, length);
5667 for (unsigned i = 0; i < length; ++i)
5668 ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
5670 vn_nary_op_t vnresult = NULL;
5671 tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
5672 res_op->type, ops, &vnresult);
5673 /* If this is used from expression simplification make sure to
5674 return an available expression. */
5675 if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
5676 res = rpo_avail->eliminate_avail (vn_context_bb, res);
5677 return res;
5680 rpo_elim::~rpo_elim ()
5682 /* Release the avail vectors. */
5683 for (rpo_avail_t::iterator i = m_rpo_avail.begin ();
5684 i != m_rpo_avail.end (); ++i)
5685 (*i).second.release ();
5688 /* Return a leader for OPs value that is valid at BB. */
5690 tree
5691 rpo_elim::eliminate_avail (basic_block bb, tree op)
5693 bool visited;
5694 tree valnum = SSA_VAL (op, &visited);
5695 /* If we didn't visit OP then it must be defined outside of the
5696 region we process and also dominate it. So it is available. */
5697 if (!visited)
5698 return op;
5699 if (TREE_CODE (valnum) == SSA_NAME)
5701 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
5702 return valnum;
5703 vec<std::pair<int, int> > *av = m_rpo_avail.get (valnum);
5704 if (!av || av->is_empty ())
5705 return NULL_TREE;
5706 int i = av->length () - 1;
5707 if ((*av)[i].first == bb->index)
5708 /* On tramp3d 90% of the cases are here. */
5709 return ssa_name ((*av)[i].second);
5712 basic_block abb = BASIC_BLOCK_FOR_FN (cfun, (*av)[i].first);
5713 /* ??? During elimination we have to use availability at the
5714 definition site of a use we try to replace. This
5715 is required to not run into inconsistencies because
5716 of dominated_by_p_w_unex behavior and removing a definition
5717 while not replacing all uses.
5718 ??? We could try to consistently walk dominators
5719 ignoring non-executable regions. The nearest common
5720 dominator of bb and abb is where we can stop walking. We
5721 may also be able to "pre-compute" (bits of) the next immediate
5722 (non-)dominator during the RPO walk when marking edges as
5723 executable. */
5724 if (dominated_by_p_w_unex (bb, abb))
5726 tree leader = ssa_name ((*av)[i].second);
5727 /* Prevent eliminations that break loop-closed SSA. */
5728 if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
5729 && ! SSA_NAME_IS_DEFAULT_DEF (leader)
5730 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
5731 (leader))->loop_father,
5732 bb))
5733 return NULL_TREE;
5734 if (dump_file && (dump_flags & TDF_DETAILS))
5736 print_generic_expr (dump_file, leader);
5737 fprintf (dump_file, " is available for ");
5738 print_generic_expr (dump_file, valnum);
5739 fprintf (dump_file, "\n");
5741 /* On tramp3d 99% of the _remaining_ cases succeed at
5742 the first enty. */
5743 return leader;
5745 /* ??? Can we somehow skip to the immediate dominator
5746 RPO index (bb_to_rpo)? Again, maybe not worth, on
5747 tramp3d the worst number of elements in the vector is 9. */
5749 while (--i >= 0);
5751 else if (valnum != VN_TOP)
5752 /* valnum is is_gimple_min_invariant. */
5753 return valnum;
5754 return NULL_TREE;
5757 /* Make LEADER a leader for its value at BB. */
5759 void
5760 rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
5762 tree valnum = VN_INFO (leader)->valnum;
5763 if (valnum == VN_TOP)
5764 return;
5765 if (dump_file && (dump_flags & TDF_DETAILS))
5767 fprintf (dump_file, "Making available beyond BB%d ", bb->index);
5768 print_generic_expr (dump_file, leader);
5769 fprintf (dump_file, " for value ");
5770 print_generic_expr (dump_file, valnum);
5771 fprintf (dump_file, "\n");
5773 bool existed;
5774 vec<std::pair<int, int> > &av = m_rpo_avail.get_or_insert (valnum, &existed);
5775 if (!existed)
5777 new (&av) vec<std::pair<int, int> >;
5778 av.reserve_exact (2);
5780 av.safe_push (std::make_pair (bb->index, SSA_NAME_VERSION (leader)));
5783 /* Valueization hook for RPO VN plus required state. */
5785 tree
5786 rpo_vn_valueize (tree name)
5788 if (TREE_CODE (name) == SSA_NAME)
5790 vn_ssa_aux_t val = VN_INFO (name);
5791 if (val)
5793 tree tem = val->valnum;
5794 if (tem != VN_TOP && tem != name)
5796 if (TREE_CODE (tem) != SSA_NAME)
5797 return tem;
5798 /* For all values we only valueize to an available leader
5799 which means we can use SSA name info without restriction. */
5800 tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
5801 if (tem)
5802 return tem;
5806 return name;
5809 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
5810 inverted condition. */
5812 static void
5813 insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
5815 switch (code)
5817 case LT_EXPR:
5818 /* a < b -> a {!,<}= b */
5819 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
5820 ops, boolean_true_node, 0, pred_e);
5821 vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
5822 ops, boolean_true_node, 0, pred_e);
5823 /* a < b -> ! a {>,=} b */
5824 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
5825 ops, boolean_false_node, 0, pred_e);
5826 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
5827 ops, boolean_false_node, 0, pred_e);
5828 break;
5829 case GT_EXPR:
5830 /* a > b -> a {!,>}= b */
5831 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
5832 ops, boolean_true_node, 0, pred_e);
5833 vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
5834 ops, boolean_true_node, 0, pred_e);
5835 /* a > b -> ! a {<,=} b */
5836 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
5837 ops, boolean_false_node, 0, pred_e);
5838 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
5839 ops, boolean_false_node, 0, pred_e);
5840 break;
5841 case EQ_EXPR:
5842 /* a == b -> ! a {<,>} b */
5843 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
5844 ops, boolean_false_node, 0, pred_e);
5845 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
5846 ops, boolean_false_node, 0, pred_e);
5847 break;
5848 case LE_EXPR:
5849 case GE_EXPR:
5850 case NE_EXPR:
5851 /* Nothing besides inverted condition. */
5852 break;
5853 default:;
5857 /* Main stmt worker for RPO VN, process BB. */
5859 static unsigned
5860 process_bb (rpo_elim &avail, basic_block bb,
5861 bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
5862 bool do_region, bitmap exit_bbs)
5864 unsigned todo = 0;
5865 edge_iterator ei;
5866 edge e;
5868 vn_context_bb = bb;
5870 /* If we are in loop-closed SSA preserve this state. This is
5871 relevant when called on regions from outside of FRE/PRE. */
5872 bool lc_phi_nodes = false;
5873 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
5874 FOR_EACH_EDGE (e, ei, bb->preds)
5875 if (e->src->loop_father != e->dest->loop_father
5876 && flow_loop_nested_p (e->dest->loop_father,
5877 e->src->loop_father))
5879 lc_phi_nodes = true;
5880 break;
5883 /* Value-number all defs in the basic-block. */
5884 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
5885 gsi_next (&gsi))
5887 gphi *phi = gsi.phi ();
5888 tree res = PHI_RESULT (phi);
5889 vn_ssa_aux_t res_info = VN_INFO (res);
5890 if (!bb_visited)
5892 gcc_assert (!res_info->visited);
5893 res_info->valnum = VN_TOP;
5894 res_info->visited = true;
5897 /* When not iterating force backedge values to varying. */
5898 visit_stmt (phi, !iterate_phis);
5899 if (virtual_operand_p (res))
5900 continue;
5902 /* Eliminate */
5903 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
5904 how we handle backedges and availability.
5905 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
5906 tree val = res_info->valnum;
5907 if (res != val && !iterate && eliminate)
5909 if (tree leader = avail.eliminate_avail (bb, res))
5911 if (leader != res
5912 /* Preserve loop-closed SSA form. */
5913 && (! lc_phi_nodes
5914 || is_gimple_min_invariant (leader)))
5916 if (dump_file && (dump_flags & TDF_DETAILS))
5918 fprintf (dump_file, "Replaced redundant PHI node "
5919 "defining ");
5920 print_generic_expr (dump_file, res);
5921 fprintf (dump_file, " with ");
5922 print_generic_expr (dump_file, leader);
5923 fprintf (dump_file, "\n");
5925 avail.eliminations++;
5927 if (may_propagate_copy (res, leader))
5929 /* Schedule for removal. */
5930 avail.to_remove.safe_push (phi);
5931 continue;
5933 /* ??? Else generate a copy stmt. */
5937 /* Only make defs available that not already are. But make
5938 sure loop-closed SSA PHI node defs are picked up for
5939 downstream uses. */
5940 if (lc_phi_nodes
5941 || res == val
5942 || ! avail.eliminate_avail (bb, res))
5943 avail.eliminate_push_avail (bb, res);
5946 /* For empty BBs mark outgoing edges executable. For non-empty BBs
5947 we do this when processing the last stmt as we have to do this
5948 before elimination which otherwise forces GIMPLE_CONDs to
5949 if (1 != 0) style when seeing non-executable edges. */
5950 if (gsi_end_p (gsi_start_bb (bb)))
5952 FOR_EACH_EDGE (e, ei, bb->succs)
5954 if (e->flags & EDGE_EXECUTABLE)
5955 continue;
5956 if (dump_file && (dump_flags & TDF_DETAILS))
5957 fprintf (dump_file,
5958 "marking outgoing edge %d -> %d executable\n",
5959 e->src->index, e->dest->index);
5960 gcc_checking_assert (iterate || !(e->flags & EDGE_DFS_BACK));
5961 e->flags |= EDGE_EXECUTABLE;
5962 e->dest->flags |= BB_EXECUTABLE;
5965 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
5966 !gsi_end_p (gsi); gsi_next (&gsi))
5968 ssa_op_iter i;
5969 tree op;
5970 if (!bb_visited)
5972 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
5974 vn_ssa_aux_t op_info = VN_INFO (op);
5975 gcc_assert (!op_info->visited);
5976 op_info->valnum = VN_TOP;
5977 op_info->visited = true;
5980 /* We somehow have to deal with uses that are not defined
5981 in the processed region. Forcing unvisited uses to
5982 varying here doesn't play well with def-use following during
5983 expression simplification, so we deal with this by checking
5984 the visited flag in SSA_VAL. */
5987 visit_stmt (gsi_stmt (gsi));
5989 gimple *last = gsi_stmt (gsi);
5990 e = NULL;
5991 switch (gimple_code (last))
5993 case GIMPLE_SWITCH:
5994 e = find_taken_edge (bb, vn_valueize (gimple_switch_index
5995 (as_a <gswitch *> (last))));
5996 break;
5997 case GIMPLE_COND:
5999 tree lhs = vn_valueize (gimple_cond_lhs (last));
6000 tree rhs = vn_valueize (gimple_cond_rhs (last));
6001 tree val = gimple_simplify (gimple_cond_code (last),
6002 boolean_type_node, lhs, rhs,
6003 NULL, vn_valueize);
6004 /* If the condition didn't simplfy see if we have recorded
6005 an expression from sofar taken edges. */
6006 if (! val || TREE_CODE (val) != INTEGER_CST)
6008 vn_nary_op_t vnresult;
6009 tree ops[2];
6010 ops[0] = lhs;
6011 ops[1] = rhs;
6012 val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
6013 boolean_type_node, ops,
6014 &vnresult);
6015 /* Did we get a predicated value? */
6016 if (! val && vnresult && vnresult->predicated_values)
6018 val = vn_nary_op_get_predicated_value (vnresult, bb);
6019 if (val && dump_file && (dump_flags & TDF_DETAILS))
6021 fprintf (dump_file, "Got predicated value ");
6022 print_generic_expr (dump_file, val, TDF_NONE);
6023 fprintf (dump_file, " for ");
6024 print_gimple_stmt (dump_file, last, TDF_SLIM);
6028 if (val)
6029 e = find_taken_edge (bb, val);
6030 if (! e)
6032 /* If we didn't manage to compute the taken edge then
6033 push predicated expressions for the condition itself
6034 and related conditions to the hashtables. This allows
6035 simplification of redundant conditions which is
6036 important as early cleanup. */
6037 edge true_e, false_e;
6038 extract_true_false_edges_from_block (bb, &true_e, &false_e);
6039 enum tree_code code = gimple_cond_code (last);
6040 enum tree_code icode
6041 = invert_tree_comparison (code, HONOR_NANS (lhs));
6042 tree ops[2];
6043 ops[0] = lhs;
6044 ops[1] = rhs;
6045 if (do_region
6046 && bitmap_bit_p (exit_bbs, true_e->dest->index))
6047 true_e = NULL;
6048 if (do_region
6049 && bitmap_bit_p (exit_bbs, false_e->dest->index))
6050 false_e = NULL;
6051 if (true_e)
6052 vn_nary_op_insert_pieces_predicated
6053 (2, code, boolean_type_node, ops,
6054 boolean_true_node, 0, true_e);
6055 if (false_e)
6056 vn_nary_op_insert_pieces_predicated
6057 (2, code, boolean_type_node, ops,
6058 boolean_false_node, 0, false_e);
6059 if (icode != ERROR_MARK)
6061 if (true_e)
6062 vn_nary_op_insert_pieces_predicated
6063 (2, icode, boolean_type_node, ops,
6064 boolean_false_node, 0, true_e);
6065 if (false_e)
6066 vn_nary_op_insert_pieces_predicated
6067 (2, icode, boolean_type_node, ops,
6068 boolean_true_node, 0, false_e);
6070 /* Relax for non-integers, inverted condition handled
6071 above. */
6072 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
6074 if (true_e)
6075 insert_related_predicates_on_edge (code, ops, true_e);
6076 if (false_e)
6077 insert_related_predicates_on_edge (icode, ops, false_e);
6080 break;
6082 case GIMPLE_GOTO:
6083 e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
6084 break;
6085 default:
6086 e = NULL;
6088 if (e)
6090 todo = TODO_cleanup_cfg;
6091 if (!(e->flags & EDGE_EXECUTABLE))
6093 if (dump_file && (dump_flags & TDF_DETAILS))
6094 fprintf (dump_file,
6095 "marking known outgoing %sedge %d -> %d executable\n",
6096 e->flags & EDGE_DFS_BACK ? "back-" : "",
6097 e->src->index, e->dest->index);
6098 gcc_checking_assert (iterate || !(e->flags & EDGE_DFS_BACK));
6099 e->flags |= EDGE_EXECUTABLE;
6100 e->dest->flags |= BB_EXECUTABLE;
6103 else if (gsi_one_before_end_p (gsi))
6105 FOR_EACH_EDGE (e, ei, bb->succs)
6107 if (e->flags & EDGE_EXECUTABLE)
6108 continue;
6109 if (dump_file && (dump_flags & TDF_DETAILS))
6110 fprintf (dump_file,
6111 "marking outgoing edge %d -> %d executable\n",
6112 e->src->index, e->dest->index);
6113 gcc_checking_assert (iterate || !(e->flags & EDGE_DFS_BACK));
6114 e->flags |= EDGE_EXECUTABLE;
6115 e->dest->flags |= BB_EXECUTABLE;
6119 /* Eliminate. That also pushes to avail. */
6120 if (eliminate && ! iterate)
6121 avail.eliminate_stmt (bb, &gsi);
6122 else
6123 /* If not eliminating, make all not already available defs
6124 available. */
6125 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
6126 if (! avail.eliminate_avail (bb, op))
6127 avail.eliminate_push_avail (bb, op);
6130 /* Eliminate in destination PHI arguments. Always substitute in dest
6131 PHIs, even for non-executable edges. This handles region
6132 exits PHIs. */
6133 if (!iterate && eliminate)
6134 FOR_EACH_EDGE (e, ei, bb->succs)
6135 for (gphi_iterator gsi = gsi_start_phis (e->dest);
6136 !gsi_end_p (gsi); gsi_next (&gsi))
6138 gphi *phi = gsi.phi ();
6139 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
6140 tree arg = USE_FROM_PTR (use_p);
6141 if (TREE_CODE (arg) != SSA_NAME
6142 || virtual_operand_p (arg))
6143 continue;
6144 tree sprime;
6145 if (SSA_NAME_IS_DEFAULT_DEF (arg))
6147 sprime = SSA_VAL (arg);
6148 gcc_assert (TREE_CODE (sprime) != SSA_NAME
6149 || SSA_NAME_IS_DEFAULT_DEF (sprime));
6151 else
6152 /* Look for sth available at the definition block of the argument.
6153 This avoids inconsistencies between availability there which
6154 decides if the stmt can be removed and availability at the
6155 use site. The SSA property ensures that things available
6156 at the definition are also available at uses. */
6157 sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
6158 arg);
6159 if (sprime
6160 && sprime != arg
6161 && may_propagate_copy (arg, sprime))
6162 propagate_value (use_p, sprime);
6165 vn_context_bb = NULL;
6166 return todo;
6169 /* Unwind state per basic-block. */
6171 struct unwind_state
6173 /* Times this block has been visited. */
6174 unsigned visited;
6175 /* Whether to handle this as iteration point or whether to treat
6176 incoming backedge PHI values as varying. */
6177 bool iterate;
6178 void *ob_top;
6179 vn_reference_t ref_top;
6180 vn_phi_t phi_top;
6181 vn_nary_op_t nary_top;
6184 /* Unwind the RPO VN state for iteration. */
6186 static void
6187 do_unwind (unwind_state *to, int rpo_idx, rpo_elim &avail, int *bb_to_rpo)
6189 gcc_assert (to->iterate);
6190 for (; last_inserted_nary != to->nary_top;
6191 last_inserted_nary = last_inserted_nary->next)
6193 vn_nary_op_t *slot;
6194 slot = valid_info->nary->find_slot_with_hash
6195 (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
6196 /* Predication causes the need to restore previous state. */
6197 if ((*slot)->unwind_to)
6198 *slot = (*slot)->unwind_to;
6199 else
6200 valid_info->nary->clear_slot (slot);
6202 for (; last_inserted_phi != to->phi_top;
6203 last_inserted_phi = last_inserted_phi->next)
6205 vn_phi_t *slot;
6206 slot = valid_info->phis->find_slot_with_hash
6207 (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
6208 valid_info->phis->clear_slot (slot);
6210 for (; last_inserted_ref != to->ref_top;
6211 last_inserted_ref = last_inserted_ref->next)
6213 vn_reference_t *slot;
6214 slot = valid_info->references->find_slot_with_hash
6215 (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
6216 (*slot)->operands.release ();
6217 valid_info->references->clear_slot (slot);
6219 obstack_free (&vn_tables_obstack, to->ob_top);
6221 /* Prune [rpo_idx, ] from avail. */
6222 /* ??? This is O(number-of-values-in-region) which is
6223 O(region-size) rather than O(iteration-piece). */
6224 for (rpo_elim::rpo_avail_t::iterator i
6225 = avail.m_rpo_avail.begin ();
6226 i != avail.m_rpo_avail.end (); ++i)
6228 while (! (*i).second.is_empty ())
6230 if (bb_to_rpo[(*i).second.last ().first] < rpo_idx)
6231 break;
6232 (*i).second.pop ();
6237 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
6238 If ITERATE is true then treat backedges optimistically as not
6239 executed and iterate. If ELIMINATE is true then perform
6240 elimination, otherwise leave that to the caller. */
6242 static unsigned
6243 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
6244 bool iterate, bool eliminate)
6246 unsigned todo = 0;
6248 /* We currently do not support region-based iteration when
6249 elimination is requested. */
6250 gcc_assert (!entry || !iterate || !eliminate);
6251 /* When iterating we need loop info up-to-date. */
6252 gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
6254 bool do_region = entry != NULL;
6255 if (!do_region)
6257 entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
6258 exit_bbs = BITMAP_ALLOC (NULL);
6259 bitmap_set_bit (exit_bbs, EXIT_BLOCK);
6262 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
6263 int n = rev_post_order_and_mark_dfs_back_seme (fn, entry, exit_bbs,
6264 iterate, rpo);
6265 /* rev_post_order_and_mark_dfs_back_seme fills RPO in reverse order. */
6266 for (int i = 0; i < n / 2; ++i)
6267 std::swap (rpo[i], rpo[n-i-1]);
6269 if (!do_region)
6270 BITMAP_FREE (exit_bbs);
6272 int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
6273 for (int i = 0; i < n; ++i)
6274 bb_to_rpo[rpo[i]] = i;
6276 unwind_state *rpo_state = XNEWVEC (unwind_state, n);
6278 rpo_elim avail (entry->dest);
6279 rpo_avail = &avail;
6281 /* Verify we have no extra entries into the region. */
6282 if (flag_checking && do_region)
6284 auto_bb_flag bb_in_region (fn);
6285 for (int i = 0; i < n; ++i)
6287 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6288 bb->flags |= bb_in_region;
6290 /* We can't merge the first two loops because we cannot rely
6291 on EDGE_DFS_BACK for edges not within the region. But if
6292 we decide to always have the bb_in_region flag we can
6293 do the checking during the RPO walk itself (but then it's
6294 also easy to handle MEME conservatively). */
6295 for (int i = 0; i < n; ++i)
6297 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6298 edge e;
6299 edge_iterator ei;
6300 FOR_EACH_EDGE (e, ei, bb->preds)
6301 gcc_assert (e == entry || (e->src->flags & bb_in_region));
6303 for (int i = 0; i < n; ++i)
6305 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6306 bb->flags &= ~bb_in_region;
6310 /* Create the VN state. For the initial size of the various hashtables
6311 use a heuristic based on region size and number of SSA names. */
6312 unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
6313 / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
6314 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
6316 vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
6317 gcc_obstack_init (&vn_ssa_aux_obstack);
6319 gcc_obstack_init (&vn_tables_obstack);
6320 gcc_obstack_init (&vn_tables_insert_obstack);
6321 valid_info = XCNEW (struct vn_tables_s);
6322 allocate_vn_table (valid_info, region_size);
6323 last_inserted_ref = NULL;
6324 last_inserted_phi = NULL;
6325 last_inserted_nary = NULL;
6327 vn_valueize = rpo_vn_valueize;
6329 /* Initialize the unwind state and edge/BB executable state. */
6330 for (int i = 0; i < n; ++i)
6332 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6333 rpo_state[i].visited = 0;
6334 bb->flags &= ~BB_EXECUTABLE;
6335 bool has_backedges = false;
6336 edge e;
6337 edge_iterator ei;
6338 FOR_EACH_EDGE (e, ei, bb->preds)
6340 if (e->flags & EDGE_DFS_BACK)
6341 has_backedges = true;
6342 if (! iterate && (e->flags & EDGE_DFS_BACK))
6344 e->flags |= EDGE_EXECUTABLE;
6345 /* ??? Strictly speaking we only need to unconditionally
6346 process a block when it is in an irreducible region,
6347 thus when it may be reachable via the backedge only. */
6348 bb->flags |= BB_EXECUTABLE;
6350 else
6351 e->flags &= ~EDGE_EXECUTABLE;
6353 rpo_state[i].iterate = iterate && has_backedges;
6355 entry->flags |= EDGE_EXECUTABLE;
6356 entry->dest->flags |= BB_EXECUTABLE;
6358 /* As heuristic to improve compile-time we handle only the N innermost
6359 loops and the outermost one optimistically. */
6360 if (iterate)
6362 loop_p loop;
6363 unsigned max_depth = PARAM_VALUE (PARAM_RPO_VN_MAX_LOOP_DEPTH);
6364 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
6365 if (loop_depth (loop) > max_depth)
6366 for (unsigned i = 2;
6367 i < loop_depth (loop) - max_depth; ++i)
6369 basic_block header = superloop_at_depth (loop, i)->header;
6370 bool non_latch_backedge = false;
6371 edge e;
6372 edge_iterator ei;
6373 FOR_EACH_EDGE (e, ei, header->preds)
6374 if (e->flags & EDGE_DFS_BACK)
6376 e->flags |= EDGE_EXECUTABLE;
6377 e->dest->flags |= BB_EXECUTABLE;
6378 /* There can be a non-latch backedge into the header
6379 which is part of an outer irreducible region. We
6380 cannot avoid iterating this block then. */
6381 if (!dominated_by_p (CDI_DOMINATORS,
6382 e->src, e->dest))
6384 if (dump_file && (dump_flags & TDF_DETAILS))
6385 fprintf (dump_file, "non-latch backedge %d -> %d "
6386 "forces iteration of loop %d\n",
6387 e->src->index, e->dest->index, loop->num);
6388 non_latch_backedge = true;
6391 rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
6395 /* Go and process all blocks, iterating as necessary. */
6396 int idx = 0;
6397 uint64_t nblk = 0;
6400 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
6402 /* If the block has incoming backedges remember unwind state. This
6403 is required even for non-executable blocks since in irreducible
6404 regions we might reach them via the backedge and re-start iterating
6405 from there.
6406 Note we can individually mark blocks with incoming backedges to
6407 not iterate where we then handle PHIs conservatively. We do that
6408 heuristically to reduce compile-time for degenerate cases. */
6409 if (rpo_state[idx].iterate)
6411 rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
6412 rpo_state[idx].ref_top = last_inserted_ref;
6413 rpo_state[idx].phi_top = last_inserted_phi;
6414 rpo_state[idx].nary_top = last_inserted_nary;
6417 if (!(bb->flags & BB_EXECUTABLE))
6419 if (dump_file && (dump_flags & TDF_DETAILS))
6420 fprintf (dump_file, "Block %d: BB%d found not executable\n",
6421 idx, bb->index);
6422 idx++;
6423 continue;
6426 if (dump_file && (dump_flags & TDF_DETAILS))
6427 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
6428 nblk++;
6429 todo |= process_bb (avail, bb,
6430 rpo_state[idx].visited != 0,
6431 rpo_state[idx].iterate,
6432 iterate, eliminate, do_region, exit_bbs);
6433 rpo_state[idx].visited++;
6435 if (iterate)
6437 /* Verify if changed values flow over executable outgoing backedges
6438 and those change destination PHI values (that's the thing we
6439 can easily verify). Reduce over all such edges to the farthest
6440 away PHI. */
6441 int iterate_to = -1;
6442 edge_iterator ei;
6443 edge e;
6444 FOR_EACH_EDGE (e, ei, bb->succs)
6445 if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
6446 == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
6447 && rpo_state[bb_to_rpo[e->dest->index]].iterate)
6449 if (dump_file && (dump_flags & TDF_DETAILS))
6450 fprintf (dump_file, "Looking for changed values of backedge "
6451 "%d->%d destination PHIs\n",
6452 e->src->index, e->dest->index);
6453 vn_context_bb = e->dest;
6454 gphi_iterator gsi;
6455 for (gsi = gsi_start_phis (e->dest);
6456 !gsi_end_p (gsi); gsi_next (&gsi))
6458 bool inserted = false;
6459 /* While we'd ideally just iterate on value changes
6460 we CSE PHIs and do that even across basic-block
6461 boundaries. So even hashtable state changes can
6462 be important (which is roughly equivalent to
6463 PHI argument value changes). To not excessively
6464 iterate because of that we track whether a PHI
6465 was CSEd to with GF_PLF_1. */
6466 bool phival_changed;
6467 if ((phival_changed = visit_phi (gsi.phi (),
6468 &inserted, false))
6469 || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
6471 if (!phival_changed
6472 && dump_file && (dump_flags & TDF_DETAILS))
6473 fprintf (dump_file, "PHI was CSEd and hashtable "
6474 "state (changed)\n");
6475 int destidx = bb_to_rpo[e->dest->index];
6476 if (iterate_to == -1
6477 || destidx < iterate_to)
6478 iterate_to = destidx;
6479 break;
6482 vn_context_bb = NULL;
6484 if (iterate_to != -1)
6486 do_unwind (&rpo_state[iterate_to], iterate_to,
6487 avail, bb_to_rpo);
6488 idx = iterate_to;
6489 if (dump_file && (dump_flags & TDF_DETAILS))
6490 fprintf (dump_file, "Iterating to %d BB%d\n",
6491 iterate_to, rpo[iterate_to]);
6492 continue;
6496 idx++;
6498 while (idx < n);
6500 /* If statistics or dump file active. */
6501 int nex = 0;
6502 unsigned max_visited = 1;
6503 for (int i = 0; i < n; ++i)
6505 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
6506 if (bb->flags & BB_EXECUTABLE)
6507 nex++;
6508 statistics_histogram_event (cfun, "RPO block visited times",
6509 rpo_state[i].visited);
6510 if (rpo_state[i].visited > max_visited)
6511 max_visited = rpo_state[i].visited;
6513 unsigned nvalues = 0, navail = 0;
6514 for (rpo_elim::rpo_avail_t::iterator i = avail.m_rpo_avail.begin ();
6515 i != avail.m_rpo_avail.end (); ++i)
6517 nvalues++;
6518 navail += (*i).second.length ();
6520 statistics_counter_event (cfun, "RPO blocks", n);
6521 statistics_counter_event (cfun, "RPO blocks visited", nblk);
6522 statistics_counter_event (cfun, "RPO blocks executable", nex);
6523 statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
6524 statistics_histogram_event (cfun, "RPO num values", nvalues);
6525 statistics_histogram_event (cfun, "RPO num avail", navail);
6526 statistics_histogram_event (cfun, "RPO num lattice",
6527 vn_ssa_aux_hash->elements ());
6528 if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
6530 fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
6531 " blocks in total discovering %d executable blocks iterating "
6532 "%d.%d times, a block was visited max. %u times\n",
6533 n, nblk, nex,
6534 (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
6535 max_visited);
6536 fprintf (dump_file, "RPO tracked %d values available at %d locations "
6537 "and %" PRIu64 " lattice elements\n",
6538 nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
6541 if (eliminate)
6543 /* When !iterate we already performed elimination during the RPO
6544 walk. */
6545 if (iterate)
6547 /* Elimination for region-based VN needs to be done within the
6548 RPO walk. */
6549 gcc_assert (! do_region);
6550 /* Note we can't use avail.walk here because that gets confused
6551 by the existing availability and it will be less efficient
6552 as well. */
6553 todo |= eliminate_with_rpo_vn (NULL);
6555 else
6556 todo |= avail.eliminate_cleanup (do_region);
6559 vn_valueize = NULL;
6560 rpo_avail = NULL;
6562 XDELETEVEC (bb_to_rpo);
6563 XDELETEVEC (rpo);
6565 return todo;
6568 /* Region-based entry for RPO VN. Performs value-numbering and elimination
6569 on the SEME region specified by ENTRY and EXIT_BBS. */
6571 unsigned
6572 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs)
6574 default_vn_walk_kind = VN_WALKREWRITE;
6575 unsigned todo = do_rpo_vn (fn, entry, exit_bbs, false, true);
6576 free_rpo_vn ();
6577 return todo;
6581 namespace {
6583 const pass_data pass_data_fre =
6585 GIMPLE_PASS, /* type */
6586 "fre", /* name */
6587 OPTGROUP_NONE, /* optinfo_flags */
6588 TV_TREE_FRE, /* tv_id */
6589 ( PROP_cfg | PROP_ssa ), /* properties_required */
6590 0, /* properties_provided */
6591 0, /* properties_destroyed */
6592 0, /* todo_flags_start */
6593 0, /* todo_flags_finish */
6596 class pass_fre : public gimple_opt_pass
6598 public:
6599 pass_fre (gcc::context *ctxt)
6600 : gimple_opt_pass (pass_data_fre, ctxt)
6603 /* opt_pass methods: */
6604 opt_pass * clone () { return new pass_fre (m_ctxt); }
6605 virtual bool gate (function *) { return flag_tree_fre != 0; }
6606 virtual unsigned int execute (function *);
6608 }; // class pass_fre
6610 unsigned int
6611 pass_fre::execute (function *fun)
6613 unsigned todo = 0;
6615 /* At -O[1g] use the cheap non-iterating mode. */
6616 calculate_dominance_info (CDI_DOMINATORS);
6617 if (optimize > 1)
6618 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
6620 default_vn_walk_kind = VN_WALKREWRITE;
6621 todo = do_rpo_vn (fun, NULL, NULL, optimize > 1, true);
6622 free_rpo_vn ();
6624 if (optimize > 1)
6625 loop_optimizer_finalize ();
6627 return todo;
6630 } // anon namespace
6632 gimple_opt_pass *
6633 make_pass_fre (gcc::context *ctxt)
6635 return new pass_fre (ctxt);
6638 #undef BB_EXECUTABLE