Default to dwarf version 4 on hppa64-hpux
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob416a5252144b49b660dcb87a655dc8d5c76e7fc1
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "splay-tree.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "ssa.h"
30 #include "expmed.h"
31 #include "insn-config.h"
32 #include "memmodel.h"
33 #include "emit-rtl.h"
34 #include "cgraph.h"
35 #include "gimple-pretty-print.h"
36 #include "alias.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "cfganal.h"
40 #include "tree-inline.h"
41 #include "internal-fn.h"
42 #include "gimple-fold.h"
43 #include "tree-eh.h"
44 #include "gimplify.h"
45 #include "flags.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "varasm.h"
50 #include "stmt.h"
51 #include "expr.h"
52 #include "tree-dfa.h"
53 #include "tree-ssa.h"
54 #include "dumpfile.h"
55 #include "cfgloop.h"
56 #include "tree-ssa-propagate.h"
57 #include "tree-cfg.h"
58 #include "domwalk.h"
59 #include "gimple-iterator.h"
60 #include "gimple-match.h"
61 #include "stringpool.h"
62 #include "attribs.h"
63 #include "tree-pass.h"
64 #include "statistics.h"
65 #include "langhooks.h"
66 #include "ipa-utils.h"
67 #include "dbgcnt.h"
68 #include "tree-cfgcleanup.h"
69 #include "tree-ssa-loop.h"
70 #include "tree-scalar-evolution.h"
71 #include "tree-ssa-loop-niter.h"
72 #include "builtins.h"
73 #include "tree-ssa-sccvn.h"
75 /* This algorithm is based on the SCC algorithm presented by Keith
76 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
77 (http://citeseer.ist.psu.edu/41805.html). In
78 straight line code, it is equivalent to a regular hash based value
79 numbering that is performed in reverse postorder.
81 For code with cycles, there are two alternatives, both of which
82 require keeping the hashtables separate from the actual list of
83 value numbers for SSA names.
85 1. Iterate value numbering in an RPO walk of the blocks, removing
86 all the entries from the hashtable after each iteration (but
87 keeping the SSA name->value number mapping between iterations).
88 Iterate until it does not change.
90 2. Perform value numbering as part of an SCC walk on the SSA graph,
91 iterating only the cycles in the SSA graph until they do not change
92 (using a separate, optimistic hashtable for value numbering the SCC
93 operands).
95 The second is not just faster in practice (because most SSA graph
96 cycles do not involve all the variables in the graph), it also has
97 some nice properties.
99 One of these nice properties is that when we pop an SCC off the
100 stack, we are guaranteed to have processed all the operands coming from
101 *outside of that SCC*, so we do not need to do anything special to
102 ensure they have value numbers.
104 Another nice property is that the SCC walk is done as part of a DFS
105 of the SSA graph, which makes it easy to perform combining and
106 simplifying operations at the same time.
108 The code below is deliberately written in a way that makes it easy
109 to separate the SCC walk from the other work it does.
111 In order to propagate constants through the code, we track which
112 expressions contain constants, and use those while folding. In
113 theory, we could also track expressions whose value numbers are
114 replaced, in case we end up folding based on expression
115 identities.
117 In order to value number memory, we assign value numbers to vuses.
118 This enables us to note that, for example, stores to the same
119 address of the same value from the same starting memory states are
120 equivalent.
121 TODO:
123 1. We can iterate only the changing portions of the SCC's, but
124 I have not seen an SCC big enough for this to be a win.
125 2. If you differentiate between phi nodes for loops and phi nodes
126 for if-then-else, you can properly consider phi nodes in different
127 blocks for equivalence.
128 3. We could value number vuses in more cases, particularly, whole
129 structure copies.
132 /* There's no BB_EXECUTABLE but we can use BB_VISITED. */
133 #define BB_EXECUTABLE BB_VISITED
135 static vn_lookup_kind default_vn_walk_kind;
137 /* vn_nary_op hashtable helpers. */
139 struct vn_nary_op_hasher : nofree_ptr_hash <vn_nary_op_s>
141 typedef vn_nary_op_s *compare_type;
142 static inline hashval_t hash (const vn_nary_op_s *);
143 static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
146 /* Return the computed hashcode for nary operation P1. */
148 inline hashval_t
149 vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
151 return vno1->hashcode;
154 /* Compare nary operations P1 and P2 and return true if they are
155 equivalent. */
157 inline bool
158 vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
160 return vno1 == vno2 || vn_nary_op_eq (vno1, vno2);
163 typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
164 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
167 /* vn_phi hashtable helpers. */
169 static int
170 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
172 struct vn_phi_hasher : nofree_ptr_hash <vn_phi_s>
174 static inline hashval_t hash (const vn_phi_s *);
175 static inline bool equal (const vn_phi_s *, const vn_phi_s *);
178 /* Return the computed hashcode for phi operation P1. */
180 inline hashval_t
181 vn_phi_hasher::hash (const vn_phi_s *vp1)
183 return vp1->hashcode;
186 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
188 inline bool
189 vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
191 return vp1 == vp2 || vn_phi_eq (vp1, vp2);
194 typedef hash_table<vn_phi_hasher> vn_phi_table_type;
195 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
198 /* Compare two reference operands P1 and P2 for equality. Return true if
199 they are equal, and false otherwise. */
201 static int
202 vn_reference_op_eq (const void *p1, const void *p2)
204 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
205 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
207 return (vro1->opcode == vro2->opcode
208 /* We do not care for differences in type qualification. */
209 && (vro1->type == vro2->type
210 || (vro1->type && vro2->type
211 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
212 TYPE_MAIN_VARIANT (vro2->type))))
213 && expressions_equal_p (vro1->op0, vro2->op0)
214 && expressions_equal_p (vro1->op1, vro2->op1)
215 && expressions_equal_p (vro1->op2, vro2->op2));
218 /* Free a reference operation structure VP. */
220 static inline void
221 free_reference (vn_reference_s *vr)
223 vr->operands.release ();
227 /* vn_reference hashtable helpers. */
229 struct vn_reference_hasher : nofree_ptr_hash <vn_reference_s>
231 static inline hashval_t hash (const vn_reference_s *);
232 static inline bool equal (const vn_reference_s *, const vn_reference_s *);
235 /* Return the hashcode for a given reference operation P1. */
237 inline hashval_t
238 vn_reference_hasher::hash (const vn_reference_s *vr1)
240 return vr1->hashcode;
243 inline bool
244 vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
246 return v == c || vn_reference_eq (v, c);
249 typedef hash_table<vn_reference_hasher> vn_reference_table_type;
250 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
252 /* Pretty-print OPS to OUTFILE. */
254 void
255 print_vn_reference_ops (FILE *outfile, const vec<vn_reference_op_s> ops)
257 vn_reference_op_t vro;
258 unsigned int i;
259 fprintf (outfile, "{");
260 for (i = 0; ops.iterate (i, &vro); i++)
262 bool closebrace = false;
263 if (vro->opcode != SSA_NAME
264 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
266 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
267 if (vro->op0)
269 fprintf (outfile, "<");
270 closebrace = true;
273 if (vro->op0)
275 print_generic_expr (outfile, vro->op0);
276 if (vro->op1)
278 fprintf (outfile, ",");
279 print_generic_expr (outfile, vro->op1);
281 if (vro->op2)
283 fprintf (outfile, ",");
284 print_generic_expr (outfile, vro->op2);
287 if (closebrace)
288 fprintf (outfile, ">");
289 if (i != ops.length () - 1)
290 fprintf (outfile, ",");
292 fprintf (outfile, "}");
295 DEBUG_FUNCTION void
296 debug_vn_reference_ops (const vec<vn_reference_op_s> ops)
298 print_vn_reference_ops (stderr, ops);
299 fputc ('\n', stderr);
302 /* The set of VN hashtables. */
304 typedef struct vn_tables_s
306 vn_nary_op_table_type *nary;
307 vn_phi_table_type *phis;
308 vn_reference_table_type *references;
309 } *vn_tables_t;
312 /* vn_constant hashtable helpers. */
314 struct vn_constant_hasher : free_ptr_hash <vn_constant_s>
316 static inline hashval_t hash (const vn_constant_s *);
317 static inline bool equal (const vn_constant_s *, const vn_constant_s *);
320 /* Hash table hash function for vn_constant_t. */
322 inline hashval_t
323 vn_constant_hasher::hash (const vn_constant_s *vc1)
325 return vc1->hashcode;
328 /* Hash table equality function for vn_constant_t. */
330 inline bool
331 vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
333 if (vc1->hashcode != vc2->hashcode)
334 return false;
336 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
339 static hash_table<vn_constant_hasher> *constant_to_value_id;
342 /* Obstack we allocate the vn-tables elements from. */
343 static obstack vn_tables_obstack;
344 /* Special obstack we never unwind. */
345 static obstack vn_tables_insert_obstack;
347 static vn_reference_t last_inserted_ref;
348 static vn_phi_t last_inserted_phi;
349 static vn_nary_op_t last_inserted_nary;
350 static vn_ssa_aux_t last_pushed_avail;
352 /* Valid hashtables storing information we have proven to be
353 correct. */
354 static vn_tables_t valid_info;
357 /* Valueization hook for simplify_replace_tree. Valueize NAME if it is
358 an SSA name, otherwise just return it. */
359 tree (*vn_valueize) (tree);
360 static tree
361 vn_valueize_for_srt (tree t, void* context ATTRIBUTE_UNUSED)
363 basic_block saved_vn_context_bb = vn_context_bb;
364 /* Look for sth available at the definition block of the argument.
365 This avoids inconsistencies between availability there which
366 decides if the stmt can be removed and availability at the
367 use site. The SSA property ensures that things available
368 at the definition are also available at uses. */
369 if (!SSA_NAME_IS_DEFAULT_DEF (t))
370 vn_context_bb = gimple_bb (SSA_NAME_DEF_STMT (t));
371 tree res = vn_valueize (t);
372 vn_context_bb = saved_vn_context_bb;
373 return res;
377 /* This represents the top of the VN lattice, which is the universal
378 value. */
380 tree VN_TOP;
382 /* Unique counter for our value ids. */
384 static unsigned int next_value_id;
385 static int next_constant_value_id;
388 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
389 are allocated on an obstack for locality reasons, and to free them
390 without looping over the vec. */
392 struct vn_ssa_aux_hasher : typed_noop_remove <vn_ssa_aux_t>
394 typedef vn_ssa_aux_t value_type;
395 typedef tree compare_type;
396 static inline hashval_t hash (const value_type &);
397 static inline bool equal (const value_type &, const compare_type &);
398 static inline void mark_deleted (value_type &) {}
399 static const bool empty_zero_p = true;
400 static inline void mark_empty (value_type &e) { e = NULL; }
401 static inline bool is_deleted (value_type &) { return false; }
402 static inline bool is_empty (value_type &e) { return e == NULL; }
405 hashval_t
406 vn_ssa_aux_hasher::hash (const value_type &entry)
408 return SSA_NAME_VERSION (entry->name);
411 bool
412 vn_ssa_aux_hasher::equal (const value_type &entry, const compare_type &name)
414 return name == entry->name;
417 static hash_table<vn_ssa_aux_hasher> *vn_ssa_aux_hash;
418 typedef hash_table<vn_ssa_aux_hasher>::iterator vn_ssa_aux_iterator_type;
419 static struct obstack vn_ssa_aux_obstack;
421 static vn_nary_op_t vn_nary_op_insert_stmt (gimple *, tree);
422 static unsigned int vn_nary_length_from_stmt (gimple *);
423 static vn_nary_op_t alloc_vn_nary_op_noinit (unsigned int, obstack *);
424 static vn_nary_op_t vn_nary_op_insert_into (vn_nary_op_t,
425 vn_nary_op_table_type *, bool);
426 static void init_vn_nary_op_from_stmt (vn_nary_op_t, gassign *);
427 static void init_vn_nary_op_from_pieces (vn_nary_op_t, unsigned int,
428 enum tree_code, tree, tree *);
429 static tree vn_lookup_simplify_result (gimple_match_op *);
430 static vn_reference_t vn_reference_lookup_or_insert_for_pieces
431 (tree, alias_set_type, alias_set_type, tree,
432 vec<vn_reference_op_s, va_heap>, tree);
434 /* Return whether there is value numbering information for a given SSA name. */
436 bool
437 has_VN_INFO (tree name)
439 return vn_ssa_aux_hash->find_with_hash (name, SSA_NAME_VERSION (name));
442 vn_ssa_aux_t
443 VN_INFO (tree name)
445 vn_ssa_aux_t *res
446 = vn_ssa_aux_hash->find_slot_with_hash (name, SSA_NAME_VERSION (name),
447 INSERT);
448 if (*res != NULL)
449 return *res;
451 vn_ssa_aux_t newinfo = *res = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
452 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
453 newinfo->name = name;
454 newinfo->valnum = VN_TOP;
455 /* We are using the visited flag to handle uses with defs not within the
456 region being value-numbered. */
457 newinfo->visited = false;
459 /* Given we create the VN_INFOs on-demand now we have to do initialization
460 different than VN_TOP here. */
461 if (SSA_NAME_IS_DEFAULT_DEF (name))
462 switch (TREE_CODE (SSA_NAME_VAR (name)))
464 case VAR_DECL:
465 /* All undefined vars are VARYING. */
466 newinfo->valnum = name;
467 newinfo->visited = true;
468 break;
470 case PARM_DECL:
471 /* Parameters are VARYING but we can record a condition
472 if we know it is a non-NULL pointer. */
473 newinfo->visited = true;
474 newinfo->valnum = name;
475 if (POINTER_TYPE_P (TREE_TYPE (name))
476 && nonnull_arg_p (SSA_NAME_VAR (name)))
478 tree ops[2];
479 ops[0] = name;
480 ops[1] = build_int_cst (TREE_TYPE (name), 0);
481 vn_nary_op_t nary;
482 /* Allocate from non-unwinding stack. */
483 nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
484 init_vn_nary_op_from_pieces (nary, 2, NE_EXPR,
485 boolean_type_node, ops);
486 nary->predicated_values = 0;
487 nary->u.result = boolean_true_node;
488 vn_nary_op_insert_into (nary, valid_info->nary, true);
489 gcc_assert (nary->unwind_to == NULL);
490 /* Also do not link it into the undo chain. */
491 last_inserted_nary = nary->next;
492 nary->next = (vn_nary_op_t)(void *)-1;
493 nary = alloc_vn_nary_op_noinit (2, &vn_tables_insert_obstack);
494 init_vn_nary_op_from_pieces (nary, 2, EQ_EXPR,
495 boolean_type_node, ops);
496 nary->predicated_values = 0;
497 nary->u.result = boolean_false_node;
498 vn_nary_op_insert_into (nary, valid_info->nary, true);
499 gcc_assert (nary->unwind_to == NULL);
500 last_inserted_nary = nary->next;
501 nary->next = (vn_nary_op_t)(void *)-1;
502 if (dump_file && (dump_flags & TDF_DETAILS))
504 fprintf (dump_file, "Recording ");
505 print_generic_expr (dump_file, name, TDF_SLIM);
506 fprintf (dump_file, " != 0\n");
509 break;
511 case RESULT_DECL:
512 /* If the result is passed by invisible reference the default
513 def is initialized, otherwise it's uninitialized. Still
514 undefined is varying. */
515 newinfo->visited = true;
516 newinfo->valnum = name;
517 break;
519 default:
520 gcc_unreachable ();
522 return newinfo;
525 /* Return the SSA value of X. */
527 inline tree
528 SSA_VAL (tree x, bool *visited = NULL)
530 vn_ssa_aux_t tem = vn_ssa_aux_hash->find_with_hash (x, SSA_NAME_VERSION (x));
531 if (visited)
532 *visited = tem && tem->visited;
533 return tem && tem->visited ? tem->valnum : x;
536 /* Return the SSA value of the VUSE x, supporting released VDEFs
537 during elimination which will value-number the VDEF to the
538 associated VUSE (but not substitute in the whole lattice). */
540 static inline tree
541 vuse_ssa_val (tree x)
543 if (!x)
544 return NULL_TREE;
548 x = SSA_VAL (x);
549 gcc_assert (x != VN_TOP);
551 while (SSA_NAME_IN_FREE_LIST (x));
553 return x;
556 /* Similar to the above but used as callback for walk_non_aliased_vuses
557 and thus should stop at unvisited VUSE to not walk across region
558 boundaries. */
560 static tree
561 vuse_valueize (tree vuse)
565 bool visited;
566 vuse = SSA_VAL (vuse, &visited);
567 if (!visited)
568 return NULL_TREE;
569 gcc_assert (vuse != VN_TOP);
571 while (SSA_NAME_IN_FREE_LIST (vuse));
572 return vuse;
576 /* Return the vn_kind the expression computed by the stmt should be
577 associated with. */
579 enum vn_kind
580 vn_get_stmt_kind (gimple *stmt)
582 switch (gimple_code (stmt))
584 case GIMPLE_CALL:
585 return VN_REFERENCE;
586 case GIMPLE_PHI:
587 return VN_PHI;
588 case GIMPLE_ASSIGN:
590 enum tree_code code = gimple_assign_rhs_code (stmt);
591 tree rhs1 = gimple_assign_rhs1 (stmt);
592 switch (get_gimple_rhs_class (code))
594 case GIMPLE_UNARY_RHS:
595 case GIMPLE_BINARY_RHS:
596 case GIMPLE_TERNARY_RHS:
597 return VN_NARY;
598 case GIMPLE_SINGLE_RHS:
599 switch (TREE_CODE_CLASS (code))
601 case tcc_reference:
602 /* VOP-less references can go through unary case. */
603 if ((code == REALPART_EXPR
604 || code == IMAGPART_EXPR
605 || code == VIEW_CONVERT_EXPR
606 || code == BIT_FIELD_REF)
607 && (TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME
608 || is_gimple_min_invariant (TREE_OPERAND (rhs1, 0))))
609 return VN_NARY;
611 /* Fallthrough. */
612 case tcc_declaration:
613 return VN_REFERENCE;
615 case tcc_constant:
616 return VN_CONSTANT;
618 default:
619 if (code == ADDR_EXPR)
620 return (is_gimple_min_invariant (rhs1)
621 ? VN_CONSTANT : VN_REFERENCE);
622 else if (code == CONSTRUCTOR)
623 return VN_NARY;
624 return VN_NONE;
626 default:
627 return VN_NONE;
630 default:
631 return VN_NONE;
635 /* Lookup a value id for CONSTANT and return it. If it does not
636 exist returns 0. */
638 unsigned int
639 get_constant_value_id (tree constant)
641 vn_constant_s **slot;
642 struct vn_constant_s vc;
644 vc.hashcode = vn_hash_constant_with_type (constant);
645 vc.constant = constant;
646 slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
647 if (slot)
648 return (*slot)->value_id;
649 return 0;
652 /* Lookup a value id for CONSTANT, and if it does not exist, create a
653 new one and return it. If it does exist, return it. */
655 unsigned int
656 get_or_alloc_constant_value_id (tree constant)
658 vn_constant_s **slot;
659 struct vn_constant_s vc;
660 vn_constant_t vcp;
662 /* If the hashtable isn't initialized we're not running from PRE and thus
663 do not need value-ids. */
664 if (!constant_to_value_id)
665 return 0;
667 vc.hashcode = vn_hash_constant_with_type (constant);
668 vc.constant = constant;
669 slot = constant_to_value_id->find_slot (&vc, INSERT);
670 if (*slot)
671 return (*slot)->value_id;
673 vcp = XNEW (struct vn_constant_s);
674 vcp->hashcode = vc.hashcode;
675 vcp->constant = constant;
676 vcp->value_id = get_next_constant_value_id ();
677 *slot = vcp;
678 return vcp->value_id;
681 /* Compute the hash for a reference operand VRO1. */
683 static void
684 vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
686 hstate.add_int (vro1->opcode);
687 if (vro1->op0)
688 inchash::add_expr (vro1->op0, hstate);
689 if (vro1->op1)
690 inchash::add_expr (vro1->op1, hstate);
691 if (vro1->op2)
692 inchash::add_expr (vro1->op2, hstate);
695 /* Compute a hash for the reference operation VR1 and return it. */
697 static hashval_t
698 vn_reference_compute_hash (const vn_reference_t vr1)
700 inchash::hash hstate;
701 hashval_t result;
702 int i;
703 vn_reference_op_t vro;
704 poly_int64 off = -1;
705 bool deref = false;
707 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
709 if (vro->opcode == MEM_REF)
710 deref = true;
711 else if (vro->opcode != ADDR_EXPR)
712 deref = false;
713 if (maybe_ne (vro->off, -1))
715 if (known_eq (off, -1))
716 off = 0;
717 off += vro->off;
719 else
721 if (maybe_ne (off, -1)
722 && maybe_ne (off, 0))
723 hstate.add_poly_int (off);
724 off = -1;
725 if (deref
726 && vro->opcode == ADDR_EXPR)
728 if (vro->op0)
730 tree op = TREE_OPERAND (vro->op0, 0);
731 hstate.add_int (TREE_CODE (op));
732 inchash::add_expr (op, hstate);
735 else
736 vn_reference_op_compute_hash (vro, hstate);
739 result = hstate.end ();
740 /* ??? We would ICE later if we hash instead of adding that in. */
741 if (vr1->vuse)
742 result += SSA_NAME_VERSION (vr1->vuse);
744 return result;
747 /* Return true if reference operations VR1 and VR2 are equivalent. This
748 means they have the same set of operands and vuses. */
750 bool
751 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
753 unsigned i, j;
755 /* Early out if this is not a hash collision. */
756 if (vr1->hashcode != vr2->hashcode)
757 return false;
759 /* The VOP needs to be the same. */
760 if (vr1->vuse != vr2->vuse)
761 return false;
763 /* If the operands are the same we are done. */
764 if (vr1->operands == vr2->operands)
765 return true;
767 if (!vr1->type || !vr2->type)
769 if (vr1->type != vr2->type)
770 return false;
772 else if (COMPLETE_TYPE_P (vr1->type) != COMPLETE_TYPE_P (vr2->type)
773 || (COMPLETE_TYPE_P (vr1->type)
774 && !expressions_equal_p (TYPE_SIZE (vr1->type),
775 TYPE_SIZE (vr2->type))))
776 return false;
777 else if (INTEGRAL_TYPE_P (vr1->type)
778 && INTEGRAL_TYPE_P (vr2->type))
780 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
781 return false;
783 else if (INTEGRAL_TYPE_P (vr1->type)
784 && (TYPE_PRECISION (vr1->type)
785 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
786 return false;
787 else if (INTEGRAL_TYPE_P (vr2->type)
788 && (TYPE_PRECISION (vr2->type)
789 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
790 return false;
792 i = 0;
793 j = 0;
796 poly_int64 off1 = 0, off2 = 0;
797 vn_reference_op_t vro1, vro2;
798 vn_reference_op_s tem1, tem2;
799 bool deref1 = false, deref2 = false;
800 bool reverse1 = false, reverse2 = false;
801 for (; vr1->operands.iterate (i, &vro1); i++)
803 if (vro1->opcode == MEM_REF)
804 deref1 = true;
805 /* Do not look through a storage order barrier. */
806 else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
807 return false;
808 reverse1 |= vro1->reverse;
809 if (known_eq (vro1->off, -1))
810 break;
811 off1 += vro1->off;
813 for (; vr2->operands.iterate (j, &vro2); j++)
815 if (vro2->opcode == MEM_REF)
816 deref2 = true;
817 /* Do not look through a storage order barrier. */
818 else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
819 return false;
820 reverse2 |= vro2->reverse;
821 if (known_eq (vro2->off, -1))
822 break;
823 off2 += vro2->off;
825 if (maybe_ne (off1, off2) || reverse1 != reverse2)
826 return false;
827 if (deref1 && vro1->opcode == ADDR_EXPR)
829 memset (&tem1, 0, sizeof (tem1));
830 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
831 tem1.type = TREE_TYPE (tem1.op0);
832 tem1.opcode = TREE_CODE (tem1.op0);
833 vro1 = &tem1;
834 deref1 = false;
836 if (deref2 && vro2->opcode == ADDR_EXPR)
838 memset (&tem2, 0, sizeof (tem2));
839 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
840 tem2.type = TREE_TYPE (tem2.op0);
841 tem2.opcode = TREE_CODE (tem2.op0);
842 vro2 = &tem2;
843 deref2 = false;
845 if (deref1 != deref2)
846 return false;
847 if (!vn_reference_op_eq (vro1, vro2))
848 return false;
849 ++j;
850 ++i;
852 while (vr1->operands.length () != i
853 || vr2->operands.length () != j);
855 return true;
858 /* Copy the operations present in load/store REF into RESULT, a vector of
859 vn_reference_op_s's. */
861 static void
862 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
864 /* For non-calls, store the information that makes up the address. */
865 tree orig = ref;
866 while (ref)
868 vn_reference_op_s temp;
870 memset (&temp, 0, sizeof (temp));
871 temp.type = TREE_TYPE (ref);
872 temp.opcode = TREE_CODE (ref);
873 temp.off = -1;
875 switch (temp.opcode)
877 case MODIFY_EXPR:
878 temp.op0 = TREE_OPERAND (ref, 1);
879 break;
880 case WITH_SIZE_EXPR:
881 temp.op0 = TREE_OPERAND (ref, 1);
882 temp.off = 0;
883 break;
884 case MEM_REF:
885 /* The base address gets its own vn_reference_op_s structure. */
886 temp.op0 = TREE_OPERAND (ref, 1);
887 if (!mem_ref_offset (ref).to_shwi (&temp.off))
888 temp.off = -1;
889 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
890 temp.base = MR_DEPENDENCE_BASE (ref);
891 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
892 break;
893 case TARGET_MEM_REF:
894 /* The base address gets its own vn_reference_op_s structure. */
895 temp.op0 = TMR_INDEX (ref);
896 temp.op1 = TMR_STEP (ref);
897 temp.op2 = TMR_OFFSET (ref);
898 temp.clique = MR_DEPENDENCE_CLIQUE (ref);
899 temp.base = MR_DEPENDENCE_BASE (ref);
900 result->safe_push (temp);
901 memset (&temp, 0, sizeof (temp));
902 temp.type = NULL_TREE;
903 temp.opcode = ERROR_MARK;
904 temp.op0 = TMR_INDEX2 (ref);
905 temp.off = -1;
906 break;
907 case BIT_FIELD_REF:
908 /* Record bits, position and storage order. */
909 temp.op0 = TREE_OPERAND (ref, 1);
910 temp.op1 = TREE_OPERAND (ref, 2);
911 if (!multiple_p (bit_field_offset (ref), BITS_PER_UNIT, &temp.off))
912 temp.off = -1;
913 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
914 break;
915 case COMPONENT_REF:
916 /* The field decl is enough to unambiguously specify the field,
917 so use its type here. */
918 temp.type = TREE_TYPE (TREE_OPERAND (ref, 1));
919 temp.op0 = TREE_OPERAND (ref, 1);
920 temp.op1 = TREE_OPERAND (ref, 2);
921 temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
922 && TYPE_REVERSE_STORAGE_ORDER
923 (TREE_TYPE (TREE_OPERAND (ref, 0))));
925 tree this_offset = component_ref_field_offset (ref);
926 if (this_offset
927 && poly_int_tree_p (this_offset))
929 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
930 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
932 poly_offset_int off
933 = (wi::to_poly_offset (this_offset)
934 + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT));
935 /* Probibit value-numbering zero offset components
936 of addresses the same before the pass folding
937 __builtin_object_size had a chance to run. */
938 if (TREE_CODE (orig) != ADDR_EXPR
939 || maybe_ne (off, 0)
940 || (cfun->curr_properties & PROP_objsz))
941 off.to_shwi (&temp.off);
945 break;
946 case ARRAY_RANGE_REF:
947 case ARRAY_REF:
949 tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0)));
950 /* Record index as operand. */
951 temp.op0 = TREE_OPERAND (ref, 1);
952 /* Always record lower bounds and element size. */
953 temp.op1 = array_ref_low_bound (ref);
954 /* But record element size in units of the type alignment. */
955 temp.op2 = TREE_OPERAND (ref, 3);
956 temp.align = eltype->type_common.align;
957 if (! temp.op2)
958 temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype),
959 size_int (TYPE_ALIGN_UNIT (eltype)));
960 if (poly_int_tree_p (temp.op0)
961 && poly_int_tree_p (temp.op1)
962 && TREE_CODE (temp.op2) == INTEGER_CST)
964 poly_offset_int off = ((wi::to_poly_offset (temp.op0)
965 - wi::to_poly_offset (temp.op1))
966 * wi::to_offset (temp.op2)
967 * vn_ref_op_align_unit (&temp));
968 off.to_shwi (&temp.off);
970 temp.reverse = (AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0)))
971 && TYPE_REVERSE_STORAGE_ORDER
972 (TREE_TYPE (TREE_OPERAND (ref, 0))));
974 break;
975 case VAR_DECL:
976 if (DECL_HARD_REGISTER (ref))
978 temp.op0 = ref;
979 break;
981 /* Fallthru. */
982 case PARM_DECL:
983 case CONST_DECL:
984 case RESULT_DECL:
985 /* Canonicalize decls to MEM[&decl] which is what we end up with
986 when valueizing MEM[ptr] with ptr = &decl. */
987 temp.opcode = MEM_REF;
988 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
989 temp.off = 0;
990 result->safe_push (temp);
991 temp.opcode = ADDR_EXPR;
992 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
993 temp.type = TREE_TYPE (temp.op0);
994 temp.off = -1;
995 break;
996 case STRING_CST:
997 case INTEGER_CST:
998 case POLY_INT_CST:
999 case COMPLEX_CST:
1000 case VECTOR_CST:
1001 case REAL_CST:
1002 case FIXED_CST:
1003 case CONSTRUCTOR:
1004 case SSA_NAME:
1005 temp.op0 = ref;
1006 break;
1007 case ADDR_EXPR:
1008 if (is_gimple_min_invariant (ref))
1010 temp.op0 = ref;
1011 break;
1013 break;
1014 /* These are only interesting for their operands, their
1015 existence, and their type. They will never be the last
1016 ref in the chain of references (IE they require an
1017 operand), so we don't have to put anything
1018 for op* as it will be handled by the iteration */
1019 case REALPART_EXPR:
1020 temp.off = 0;
1021 break;
1022 case VIEW_CONVERT_EXPR:
1023 temp.off = 0;
1024 temp.reverse = storage_order_barrier_p (ref);
1025 break;
1026 case IMAGPART_EXPR:
1027 /* This is only interesting for its constant offset. */
1028 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
1029 break;
1030 default:
1031 gcc_unreachable ();
1033 result->safe_push (temp);
1035 if (REFERENCE_CLASS_P (ref)
1036 || TREE_CODE (ref) == MODIFY_EXPR
1037 || TREE_CODE (ref) == WITH_SIZE_EXPR
1038 || (TREE_CODE (ref) == ADDR_EXPR
1039 && !is_gimple_min_invariant (ref)))
1040 ref = TREE_OPERAND (ref, 0);
1041 else
1042 ref = NULL_TREE;
1046 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
1047 operands in *OPS, the reference alias set SET and the reference type TYPE.
1048 Return true if something useful was produced. */
1050 bool
1051 ao_ref_init_from_vn_reference (ao_ref *ref,
1052 alias_set_type set, alias_set_type base_set,
1053 tree type, const vec<vn_reference_op_s> &ops)
1055 unsigned i;
1056 tree base = NULL_TREE;
1057 tree *op0_p = &base;
1058 poly_offset_int offset = 0;
1059 poly_offset_int max_size;
1060 poly_offset_int size = -1;
1061 tree size_tree = NULL_TREE;
1063 /* We don't handle calls. */
1064 if (!type)
1065 return false;
1067 machine_mode mode = TYPE_MODE (type);
1068 if (mode == BLKmode)
1069 size_tree = TYPE_SIZE (type);
1070 else
1071 size = GET_MODE_BITSIZE (mode);
1072 if (size_tree != NULL_TREE
1073 && poly_int_tree_p (size_tree))
1074 size = wi::to_poly_offset (size_tree);
1076 /* Lower the final access size from the outermost expression. */
1077 const_vn_reference_op_t cst_op = &ops[0];
1078 /* Cast away constness for the sake of the const-unsafe
1079 FOR_EACH_VEC_ELT(). */
1080 vn_reference_op_t op = const_cast<vn_reference_op_t>(cst_op);
1081 size_tree = NULL_TREE;
1082 if (op->opcode == COMPONENT_REF)
1083 size_tree = DECL_SIZE (op->op0);
1084 else if (op->opcode == BIT_FIELD_REF)
1085 size_tree = op->op0;
1086 if (size_tree != NULL_TREE
1087 && poly_int_tree_p (size_tree)
1088 && (!known_size_p (size)
1089 || known_lt (wi::to_poly_offset (size_tree), size)))
1090 size = wi::to_poly_offset (size_tree);
1092 /* Initially, maxsize is the same as the accessed element size.
1093 In the following it will only grow (or become -1). */
1094 max_size = size;
1096 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1097 and find the ultimate containing object. */
1098 FOR_EACH_VEC_ELT (ops, i, op)
1100 switch (op->opcode)
1102 /* These may be in the reference ops, but we cannot do anything
1103 sensible with them here. */
1104 case ADDR_EXPR:
1105 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1106 if (base != NULL_TREE
1107 && TREE_CODE (base) == MEM_REF
1108 && op->op0
1109 && DECL_P (TREE_OPERAND (op->op0, 0)))
1111 const_vn_reference_op_t pop = &ops[i-1];
1112 base = TREE_OPERAND (op->op0, 0);
1113 if (known_eq (pop->off, -1))
1115 max_size = -1;
1116 offset = 0;
1118 else
1119 offset += pop->off * BITS_PER_UNIT;
1120 op0_p = NULL;
1121 break;
1123 /* Fallthru. */
1124 case CALL_EXPR:
1125 return false;
1127 /* Record the base objects. */
1128 case MEM_REF:
1129 *op0_p = build2 (MEM_REF, op->type,
1130 NULL_TREE, op->op0);
1131 MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique;
1132 MR_DEPENDENCE_BASE (*op0_p) = op->base;
1133 op0_p = &TREE_OPERAND (*op0_p, 0);
1134 break;
1136 case VAR_DECL:
1137 case PARM_DECL:
1138 case RESULT_DECL:
1139 case SSA_NAME:
1140 *op0_p = op->op0;
1141 op0_p = NULL;
1142 break;
1144 /* And now the usual component-reference style ops. */
1145 case BIT_FIELD_REF:
1146 offset += wi::to_poly_offset (op->op1);
1147 break;
1149 case COMPONENT_REF:
1151 tree field = op->op0;
1152 /* We do not have a complete COMPONENT_REF tree here so we
1153 cannot use component_ref_field_offset. Do the interesting
1154 parts manually. */
1155 tree this_offset = DECL_FIELD_OFFSET (field);
1157 if (op->op1 || !poly_int_tree_p (this_offset))
1158 max_size = -1;
1159 else
1161 poly_offset_int woffset = (wi::to_poly_offset (this_offset)
1162 << LOG2_BITS_PER_UNIT);
1163 woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
1164 offset += woffset;
1166 break;
1169 case ARRAY_RANGE_REF:
1170 case ARRAY_REF:
1171 /* We recorded the lower bound and the element size. */
1172 if (!poly_int_tree_p (op->op0)
1173 || !poly_int_tree_p (op->op1)
1174 || TREE_CODE (op->op2) != INTEGER_CST)
1175 max_size = -1;
1176 else
1178 poly_offset_int woffset
1179 = wi::sext (wi::to_poly_offset (op->op0)
1180 - wi::to_poly_offset (op->op1),
1181 TYPE_PRECISION (sizetype));
1182 woffset *= wi::to_offset (op->op2) * vn_ref_op_align_unit (op);
1183 woffset <<= LOG2_BITS_PER_UNIT;
1184 offset += woffset;
1186 break;
1188 case REALPART_EXPR:
1189 break;
1191 case IMAGPART_EXPR:
1192 offset += size;
1193 break;
1195 case VIEW_CONVERT_EXPR:
1196 break;
1198 case STRING_CST:
1199 case INTEGER_CST:
1200 case COMPLEX_CST:
1201 case VECTOR_CST:
1202 case REAL_CST:
1203 case CONSTRUCTOR:
1204 case CONST_DECL:
1205 return false;
1207 default:
1208 return false;
1212 if (base == NULL_TREE)
1213 return false;
1215 ref->ref = NULL_TREE;
1216 ref->base = base;
1217 ref->ref_alias_set = set;
1218 ref->base_alias_set = base_set;
1219 /* We discount volatiles from value-numbering elsewhere. */
1220 ref->volatile_p = false;
1222 if (!size.to_shwi (&ref->size) || maybe_lt (ref->size, 0))
1224 ref->offset = 0;
1225 ref->size = -1;
1226 ref->max_size = -1;
1227 return true;
1230 if (!offset.to_shwi (&ref->offset))
1232 ref->offset = 0;
1233 ref->max_size = -1;
1234 return true;
1237 if (!max_size.to_shwi (&ref->max_size) || maybe_lt (ref->max_size, 0))
1238 ref->max_size = -1;
1240 return true;
1243 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1244 vn_reference_op_s's. */
1246 static void
1247 copy_reference_ops_from_call (gcall *call,
1248 vec<vn_reference_op_s> *result)
1250 vn_reference_op_s temp;
1251 unsigned i;
1252 tree lhs = gimple_call_lhs (call);
1253 int lr;
1255 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1256 different. By adding the lhs here in the vector, we ensure that the
1257 hashcode is different, guaranteeing a different value number. */
1258 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1260 memset (&temp, 0, sizeof (temp));
1261 temp.opcode = MODIFY_EXPR;
1262 temp.type = TREE_TYPE (lhs);
1263 temp.op0 = lhs;
1264 temp.off = -1;
1265 result->safe_push (temp);
1268 /* Copy the type, opcode, function, static chain and EH region, if any. */
1269 memset (&temp, 0, sizeof (temp));
1270 temp.type = gimple_call_fntype (call);
1271 temp.opcode = CALL_EXPR;
1272 temp.op0 = gimple_call_fn (call);
1273 temp.op1 = gimple_call_chain (call);
1274 if (stmt_could_throw_p (cfun, call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1275 temp.op2 = size_int (lr);
1276 temp.off = -1;
1277 result->safe_push (temp);
1279 /* Copy the call arguments. As they can be references as well,
1280 just chain them together. */
1281 for (i = 0; i < gimple_call_num_args (call); ++i)
1283 tree callarg = gimple_call_arg (call, i);
1284 copy_reference_ops_from_ref (callarg, result);
1288 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1289 *I_P to point to the last element of the replacement. */
1290 static bool
1291 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1292 unsigned int *i_p)
1294 unsigned int i = *i_p;
1295 vn_reference_op_t op = &(*ops)[i];
1296 vn_reference_op_t mem_op = &(*ops)[i - 1];
1297 tree addr_base;
1298 poly_int64 addr_offset = 0;
1300 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1301 from .foo.bar to the preceding MEM_REF offset and replace the
1302 address with &OBJ. */
1303 addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (op->op0, 0),
1304 &addr_offset, vn_valueize);
1305 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1306 if (addr_base != TREE_OPERAND (op->op0, 0))
1308 poly_offset_int off
1309 = (poly_offset_int::from (wi::to_poly_wide (mem_op->op0),
1310 SIGNED)
1311 + addr_offset);
1312 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1313 op->op0 = build_fold_addr_expr (addr_base);
1314 if (tree_fits_shwi_p (mem_op->op0))
1315 mem_op->off = tree_to_shwi (mem_op->op0);
1316 else
1317 mem_op->off = -1;
1318 return true;
1320 return false;
1323 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1324 *I_P to point to the last element of the replacement. */
1325 static bool
1326 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1327 unsigned int *i_p)
1329 bool changed = false;
1330 vn_reference_op_t op;
1334 unsigned int i = *i_p;
1335 op = &(*ops)[i];
1336 vn_reference_op_t mem_op = &(*ops)[i - 1];
1337 gimple *def_stmt;
1338 enum tree_code code;
1339 poly_offset_int off;
1341 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1342 if (!is_gimple_assign (def_stmt))
1343 return changed;
1345 code = gimple_assign_rhs_code (def_stmt);
1346 if (code != ADDR_EXPR
1347 && code != POINTER_PLUS_EXPR)
1348 return changed;
1350 off = poly_offset_int::from (wi::to_poly_wide (mem_op->op0), SIGNED);
1352 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1353 from .foo.bar to the preceding MEM_REF offset and replace the
1354 address with &OBJ. */
1355 if (code == ADDR_EXPR)
1357 tree addr, addr_base;
1358 poly_int64 addr_offset;
1360 addr = gimple_assign_rhs1 (def_stmt);
1361 addr_base = get_addr_base_and_unit_offset_1 (TREE_OPERAND (addr, 0),
1362 &addr_offset,
1363 vn_valueize);
1364 /* If that didn't work because the address isn't invariant propagate
1365 the reference tree from the address operation in case the current
1366 dereference isn't offsetted. */
1367 if (!addr_base
1368 && *i_p == ops->length () - 1
1369 && known_eq (off, 0)
1370 /* This makes us disable this transform for PRE where the
1371 reference ops might be also used for code insertion which
1372 is invalid. */
1373 && default_vn_walk_kind == VN_WALKREWRITE)
1375 auto_vec<vn_reference_op_s, 32> tem;
1376 copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem);
1377 /* Make sure to preserve TBAA info. The only objects not
1378 wrapped in MEM_REFs that can have their address taken are
1379 STRING_CSTs. */
1380 if (tem.length () >= 2
1381 && tem[tem.length () - 2].opcode == MEM_REF)
1383 vn_reference_op_t new_mem_op = &tem[tem.length () - 2];
1384 new_mem_op->op0
1385 = wide_int_to_tree (TREE_TYPE (mem_op->op0),
1386 wi::to_poly_wide (new_mem_op->op0));
1388 else
1389 gcc_assert (tem.last ().opcode == STRING_CST);
1390 ops->pop ();
1391 ops->pop ();
1392 ops->safe_splice (tem);
1393 --*i_p;
1394 return true;
1396 if (!addr_base
1397 || TREE_CODE (addr_base) != MEM_REF
1398 || (TREE_CODE (TREE_OPERAND (addr_base, 0)) == SSA_NAME
1399 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (addr_base,
1400 0))))
1401 return changed;
1403 off += addr_offset;
1404 off += mem_ref_offset (addr_base);
1405 op->op0 = TREE_OPERAND (addr_base, 0);
1407 else
1409 tree ptr, ptroff;
1410 ptr = gimple_assign_rhs1 (def_stmt);
1411 ptroff = gimple_assign_rhs2 (def_stmt);
1412 if (TREE_CODE (ptr) != SSA_NAME
1413 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr)
1414 /* Make sure to not endlessly recurse.
1415 See gcc.dg/tree-ssa/20040408-1.c for an example. Can easily
1416 happen when we value-number a PHI to its backedge value. */
1417 || SSA_VAL (ptr) == op->op0
1418 || !poly_int_tree_p (ptroff))
1419 return changed;
1421 off += wi::to_poly_offset (ptroff);
1422 op->op0 = ptr;
1425 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1426 if (tree_fits_shwi_p (mem_op->op0))
1427 mem_op->off = tree_to_shwi (mem_op->op0);
1428 else
1429 mem_op->off = -1;
1430 /* ??? Can end up with endless recursion here!?
1431 gcc.c-torture/execute/strcmp-1.c */
1432 if (TREE_CODE (op->op0) == SSA_NAME)
1433 op->op0 = SSA_VAL (op->op0);
1434 if (TREE_CODE (op->op0) != SSA_NAME)
1435 op->opcode = TREE_CODE (op->op0);
1437 changed = true;
1439 /* Tail-recurse. */
1440 while (TREE_CODE (op->op0) == SSA_NAME);
1442 /* Fold a remaining *&. */
1443 if (TREE_CODE (op->op0) == ADDR_EXPR)
1444 vn_reference_fold_indirect (ops, i_p);
1446 return changed;
1449 /* Optimize the reference REF to a constant if possible or return
1450 NULL_TREE if not. */
1452 tree
1453 fully_constant_vn_reference_p (vn_reference_t ref)
1455 vec<vn_reference_op_s> operands = ref->operands;
1456 vn_reference_op_t op;
1458 /* Try to simplify the translated expression if it is
1459 a call to a builtin function with at most two arguments. */
1460 op = &operands[0];
1461 if (op->opcode == CALL_EXPR
1462 && TREE_CODE (op->op0) == ADDR_EXPR
1463 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1464 && fndecl_built_in_p (TREE_OPERAND (op->op0, 0))
1465 && operands.length () >= 2
1466 && operands.length () <= 3)
1468 vn_reference_op_t arg0, arg1 = NULL;
1469 bool anyconst = false;
1470 arg0 = &operands[1];
1471 if (operands.length () > 2)
1472 arg1 = &operands[2];
1473 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1474 || (arg0->opcode == ADDR_EXPR
1475 && is_gimple_min_invariant (arg0->op0)))
1476 anyconst = true;
1477 if (arg1
1478 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1479 || (arg1->opcode == ADDR_EXPR
1480 && is_gimple_min_invariant (arg1->op0))))
1481 anyconst = true;
1482 if (anyconst)
1484 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1485 arg1 ? 2 : 1,
1486 arg0->op0,
1487 arg1 ? arg1->op0 : NULL);
1488 if (folded
1489 && TREE_CODE (folded) == NOP_EXPR)
1490 folded = TREE_OPERAND (folded, 0);
1491 if (folded
1492 && is_gimple_min_invariant (folded))
1493 return folded;
1497 /* Simplify reads from constants or constant initializers. */
1498 else if (BITS_PER_UNIT == 8
1499 && ref->type
1500 && COMPLETE_TYPE_P (ref->type)
1501 && is_gimple_reg_type (ref->type))
1503 poly_int64 off = 0;
1504 HOST_WIDE_INT size;
1505 if (INTEGRAL_TYPE_P (ref->type))
1506 size = TYPE_PRECISION (ref->type);
1507 else if (tree_fits_shwi_p (TYPE_SIZE (ref->type)))
1508 size = tree_to_shwi (TYPE_SIZE (ref->type));
1509 else
1510 return NULL_TREE;
1511 if (size % BITS_PER_UNIT != 0
1512 || size > MAX_BITSIZE_MODE_ANY_MODE)
1513 return NULL_TREE;
1514 size /= BITS_PER_UNIT;
1515 unsigned i;
1516 for (i = 0; i < operands.length (); ++i)
1518 if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant)
1520 ++i;
1521 break;
1523 if (known_eq (operands[i].off, -1))
1524 return NULL_TREE;
1525 off += operands[i].off;
1526 if (operands[i].opcode == MEM_REF)
1528 ++i;
1529 break;
1532 vn_reference_op_t base = &operands[--i];
1533 tree ctor = error_mark_node;
1534 tree decl = NULL_TREE;
1535 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1536 ctor = base->op0;
1537 else if (base->opcode == MEM_REF
1538 && base[1].opcode == ADDR_EXPR
1539 && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
1540 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL
1541 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == STRING_CST))
1543 decl = TREE_OPERAND (base[1].op0, 0);
1544 if (TREE_CODE (decl) == STRING_CST)
1545 ctor = decl;
1546 else
1547 ctor = ctor_for_folding (decl);
1549 if (ctor == NULL_TREE)
1550 return build_zero_cst (ref->type);
1551 else if (ctor != error_mark_node)
1553 HOST_WIDE_INT const_off;
1554 if (decl)
1556 tree res = fold_ctor_reference (ref->type, ctor,
1557 off * BITS_PER_UNIT,
1558 size * BITS_PER_UNIT, decl);
1559 if (res)
1561 STRIP_USELESS_TYPE_CONVERSION (res);
1562 if (is_gimple_min_invariant (res))
1563 return res;
1566 else if (off.is_constant (&const_off))
1568 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1569 int len = native_encode_expr (ctor, buf, size, const_off);
1570 if (len > 0)
1571 return native_interpret_expr (ref->type, buf, len);
1576 return NULL_TREE;
1579 /* Return true if OPS contain a storage order barrier. */
1581 static bool
1582 contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1584 vn_reference_op_t op;
1585 unsigned i;
1587 FOR_EACH_VEC_ELT (ops, i, op)
1588 if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1589 return true;
1591 return false;
1594 /* Return true if OPS represent an access with reverse storage order. */
1596 static bool
1597 reverse_storage_order_for_component_p (vec<vn_reference_op_s> ops)
1599 unsigned i = 0;
1600 if (ops[i].opcode == REALPART_EXPR || ops[i].opcode == IMAGPART_EXPR)
1601 ++i;
1602 switch (ops[i].opcode)
1604 case ARRAY_REF:
1605 case COMPONENT_REF:
1606 case BIT_FIELD_REF:
1607 case MEM_REF:
1608 return ops[i].reverse;
1609 default:
1610 return false;
1614 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1615 structures into their value numbers. This is done in-place, and
1616 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1617 whether any operands were valueized. */
1619 static void
1620 valueize_refs_1 (vec<vn_reference_op_s> *orig, bool *valueized_anything,
1621 bool with_avail = false)
1623 vn_reference_op_t vro;
1624 unsigned int i;
1626 *valueized_anything = false;
1628 FOR_EACH_VEC_ELT (*orig, i, vro)
1630 if (vro->opcode == SSA_NAME
1631 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1633 tree tem = with_avail ? vn_valueize (vro->op0) : SSA_VAL (vro->op0);
1634 if (tem != vro->op0)
1636 *valueized_anything = true;
1637 vro->op0 = tem;
1639 /* If it transforms from an SSA_NAME to a constant, update
1640 the opcode. */
1641 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1642 vro->opcode = TREE_CODE (vro->op0);
1644 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1646 tree tem = with_avail ? vn_valueize (vro->op1) : SSA_VAL (vro->op1);
1647 if (tem != vro->op1)
1649 *valueized_anything = true;
1650 vro->op1 = tem;
1653 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1655 tree tem = with_avail ? vn_valueize (vro->op2) : SSA_VAL (vro->op2);
1656 if (tem != vro->op2)
1658 *valueized_anything = true;
1659 vro->op2 = tem;
1662 /* If it transforms from an SSA_NAME to an address, fold with
1663 a preceding indirect reference. */
1664 if (i > 0
1665 && vro->op0
1666 && TREE_CODE (vro->op0) == ADDR_EXPR
1667 && (*orig)[i - 1].opcode == MEM_REF)
1669 if (vn_reference_fold_indirect (orig, &i))
1670 *valueized_anything = true;
1672 else if (i > 0
1673 && vro->opcode == SSA_NAME
1674 && (*orig)[i - 1].opcode == MEM_REF)
1676 if (vn_reference_maybe_forwprop_address (orig, &i))
1677 *valueized_anything = true;
1679 /* If it transforms a non-constant ARRAY_REF into a constant
1680 one, adjust the constant offset. */
1681 else if (vro->opcode == ARRAY_REF
1682 && known_eq (vro->off, -1)
1683 && poly_int_tree_p (vro->op0)
1684 && poly_int_tree_p (vro->op1)
1685 && TREE_CODE (vro->op2) == INTEGER_CST)
1687 poly_offset_int off = ((wi::to_poly_offset (vro->op0)
1688 - wi::to_poly_offset (vro->op1))
1689 * wi::to_offset (vro->op2)
1690 * vn_ref_op_align_unit (vro));
1691 off.to_shwi (&vro->off);
1696 static void
1697 valueize_refs (vec<vn_reference_op_s> *orig)
1699 bool tem;
1700 valueize_refs_1 (orig, &tem);
1703 static vec<vn_reference_op_s> shared_lookup_references;
1705 /* Create a vector of vn_reference_op_s structures from REF, a
1706 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1707 this function. *VALUEIZED_ANYTHING will specify whether any
1708 operands were valueized. */
1710 static vec<vn_reference_op_s>
1711 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1713 if (!ref)
1714 return vNULL;
1715 shared_lookup_references.truncate (0);
1716 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1717 valueize_refs_1 (&shared_lookup_references, valueized_anything);
1718 return shared_lookup_references;
1721 /* Create a vector of vn_reference_op_s structures from CALL, a
1722 call statement. The vector is shared among all callers of
1723 this function. */
1725 static vec<vn_reference_op_s>
1726 valueize_shared_reference_ops_from_call (gcall *call)
1728 if (!call)
1729 return vNULL;
1730 shared_lookup_references.truncate (0);
1731 copy_reference_ops_from_call (call, &shared_lookup_references);
1732 valueize_refs (&shared_lookup_references);
1733 return shared_lookup_references;
1736 /* Lookup a SCCVN reference operation VR in the current hash table.
1737 Returns the resulting value number if it exists in the hash table,
1738 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1739 vn_reference_t stored in the hashtable if something is found. */
1741 static tree
1742 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1744 vn_reference_s **slot;
1745 hashval_t hash;
1747 hash = vr->hashcode;
1748 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1749 if (slot)
1751 if (vnresult)
1752 *vnresult = (vn_reference_t)*slot;
1753 return ((vn_reference_t)*slot)->result;
1756 return NULL_TREE;
1760 /* Partial definition tracking support. */
1762 struct pd_range
1764 HOST_WIDE_INT offset;
1765 HOST_WIDE_INT size;
1768 struct pd_data
1770 tree rhs;
1771 HOST_WIDE_INT offset;
1772 HOST_WIDE_INT size;
1775 /* Context for alias walking. */
1777 struct vn_walk_cb_data
1779 vn_walk_cb_data (vn_reference_t vr_, tree orig_ref_, tree *last_vuse_ptr_,
1780 vn_lookup_kind vn_walk_kind_, bool tbaa_p_, tree mask_)
1781 : vr (vr_), last_vuse_ptr (last_vuse_ptr_), last_vuse (NULL_TREE),
1782 mask (mask_), masked_result (NULL_TREE), vn_walk_kind (vn_walk_kind_),
1783 tbaa_p (tbaa_p_), saved_operands (vNULL), first_set (-2),
1784 first_base_set (-2), known_ranges (NULL)
1786 if (!last_vuse_ptr)
1787 last_vuse_ptr = &last_vuse;
1788 ao_ref_init (&orig_ref, orig_ref_);
1789 if (mask)
1791 wide_int w = wi::to_wide (mask);
1792 unsigned int pos = 0, prec = w.get_precision ();
1793 pd_data pd;
1794 pd.rhs = build_constructor (NULL_TREE, NULL);
1795 /* When bitwise and with a constant is done on a memory load,
1796 we don't really need all the bits to be defined or defined
1797 to constants, we don't really care what is in the position
1798 corresponding to 0 bits in the mask.
1799 So, push the ranges of those 0 bits in the mask as artificial
1800 zero stores and let the partial def handling code do the
1801 rest. */
1802 while (pos < prec)
1804 int tz = wi::ctz (w);
1805 if (pos + tz > prec)
1806 tz = prec - pos;
1807 if (tz)
1809 if (BYTES_BIG_ENDIAN)
1810 pd.offset = prec - pos - tz;
1811 else
1812 pd.offset = pos;
1813 pd.size = tz;
1814 void *r = push_partial_def (pd, 0, 0, 0, prec);
1815 gcc_assert (r == NULL_TREE);
1817 pos += tz;
1818 if (pos == prec)
1819 break;
1820 w = wi::lrshift (w, tz);
1821 tz = wi::ctz (wi::bit_not (w));
1822 if (pos + tz > prec)
1823 tz = prec - pos;
1824 pos += tz;
1825 w = wi::lrshift (w, tz);
1829 ~vn_walk_cb_data ();
1830 void *finish (alias_set_type, alias_set_type, tree);
1831 void *push_partial_def (pd_data pd,
1832 alias_set_type, alias_set_type, HOST_WIDE_INT,
1833 HOST_WIDE_INT);
1835 vn_reference_t vr;
1836 ao_ref orig_ref;
1837 tree *last_vuse_ptr;
1838 tree last_vuse;
1839 tree mask;
1840 tree masked_result;
1841 vn_lookup_kind vn_walk_kind;
1842 bool tbaa_p;
1843 vec<vn_reference_op_s> saved_operands;
1845 /* The VDEFs of partial defs we come along. */
1846 auto_vec<pd_data, 2> partial_defs;
1847 /* The first defs range to avoid splay tree setup in most cases. */
1848 pd_range first_range;
1849 alias_set_type first_set;
1850 alias_set_type first_base_set;
1851 splay_tree known_ranges;
1852 obstack ranges_obstack;
1855 vn_walk_cb_data::~vn_walk_cb_data ()
1857 if (known_ranges)
1859 splay_tree_delete (known_ranges);
1860 obstack_free (&ranges_obstack, NULL);
1862 saved_operands.release ();
1865 void *
1866 vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
1868 if (first_set != -2)
1870 set = first_set;
1871 base_set = first_base_set;
1873 if (mask)
1875 masked_result = val;
1876 return (void *) -1;
1878 vec<vn_reference_op_s> &operands
1879 = saved_operands.exists () ? saved_operands : vr->operands;
1880 return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
1881 vr->type, operands, val);
1884 /* pd_range splay-tree helpers. */
1886 static int
1887 pd_range_compare (splay_tree_key offset1p, splay_tree_key offset2p)
1889 HOST_WIDE_INT offset1 = *(HOST_WIDE_INT *)offset1p;
1890 HOST_WIDE_INT offset2 = *(HOST_WIDE_INT *)offset2p;
1891 if (offset1 < offset2)
1892 return -1;
1893 else if (offset1 > offset2)
1894 return 1;
1895 return 0;
1898 static void *
1899 pd_tree_alloc (int size, void *data_)
1901 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
1902 return obstack_alloc (&data->ranges_obstack, size);
1905 static void
1906 pd_tree_dealloc (void *, void *)
1910 /* Push PD to the vector of partial definitions returning a
1911 value when we are ready to combine things with VUSE, SET and MAXSIZEI,
1912 NULL when we want to continue looking for partial defs or -1
1913 on failure. */
1915 void *
1916 vn_walk_cb_data::push_partial_def (pd_data pd,
1917 alias_set_type set, alias_set_type base_set,
1918 HOST_WIDE_INT offseti,
1919 HOST_WIDE_INT maxsizei)
1921 const HOST_WIDE_INT bufsize = 64;
1922 /* We're using a fixed buffer for encoding so fail early if the object
1923 we want to interpret is bigger. */
1924 if (maxsizei > bufsize * BITS_PER_UNIT
1925 || CHAR_BIT != 8
1926 || BITS_PER_UNIT != 8
1927 /* Not prepared to handle PDP endian. */
1928 || BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1929 return (void *)-1;
1931 /* Turn too large constant stores into non-constant stores. */
1932 if (CONSTANT_CLASS_P (pd.rhs) && pd.size > bufsize * BITS_PER_UNIT)
1933 pd.rhs = error_mark_node;
1935 /* And for non-constant or CONSTRUCTOR stores shrink them to only keep at
1936 most a partial byte before and/or after the region. */
1937 if (!CONSTANT_CLASS_P (pd.rhs))
1939 if (pd.offset < offseti)
1941 HOST_WIDE_INT o = ROUND_DOWN (offseti - pd.offset, BITS_PER_UNIT);
1942 gcc_assert (pd.size > o);
1943 pd.size -= o;
1944 pd.offset += o;
1946 if (pd.size > maxsizei)
1947 pd.size = maxsizei + ((pd.size - maxsizei) % BITS_PER_UNIT);
1950 pd.offset -= offseti;
1952 bool pd_constant_p = (TREE_CODE (pd.rhs) == CONSTRUCTOR
1953 || CONSTANT_CLASS_P (pd.rhs));
1954 if (partial_defs.is_empty ())
1956 /* If we get a clobber upfront, fail. */
1957 if (TREE_CLOBBER_P (pd.rhs))
1958 return (void *)-1;
1959 if (!pd_constant_p)
1960 return (void *)-1;
1961 partial_defs.safe_push (pd);
1962 first_range.offset = pd.offset;
1963 first_range.size = pd.size;
1964 first_set = set;
1965 first_base_set = base_set;
1966 last_vuse_ptr = NULL;
1967 /* Continue looking for partial defs. */
1968 return NULL;
1971 if (!known_ranges)
1973 /* ??? Optimize the case where the 2nd partial def completes things. */
1974 gcc_obstack_init (&ranges_obstack);
1975 known_ranges = splay_tree_new_with_allocator (pd_range_compare, 0, 0,
1976 pd_tree_alloc,
1977 pd_tree_dealloc, this);
1978 splay_tree_insert (known_ranges,
1979 (splay_tree_key)&first_range.offset,
1980 (splay_tree_value)&first_range);
1983 pd_range newr = { pd.offset, pd.size };
1984 splay_tree_node n;
1985 pd_range *r;
1986 /* Lookup the predecessor of offset + 1 and see if we need to merge. */
1987 HOST_WIDE_INT loffset = newr.offset + 1;
1988 if ((n = splay_tree_predecessor (known_ranges, (splay_tree_key)&loffset))
1989 && ((r = (pd_range *)n->value), true)
1990 && ranges_known_overlap_p (r->offset, r->size + 1,
1991 newr.offset, newr.size))
1993 /* Ignore partial defs already covered. Here we also drop shadowed
1994 clobbers arriving here at the floor. */
1995 if (known_subrange_p (newr.offset, newr.size, r->offset, r->size))
1996 return NULL;
1997 r->size = MAX (r->offset + r->size, newr.offset + newr.size) - r->offset;
1999 else
2001 /* newr.offset wasn't covered yet, insert the range. */
2002 r = XOBNEW (&ranges_obstack, pd_range);
2003 *r = newr;
2004 splay_tree_insert (known_ranges, (splay_tree_key)&r->offset,
2005 (splay_tree_value)r);
2007 /* Merge r which now contains newr and is a member of the splay tree with
2008 adjacent overlapping ranges. */
2009 pd_range *rafter;
2010 while ((n = splay_tree_successor (known_ranges, (splay_tree_key)&r->offset))
2011 && ((rafter = (pd_range *)n->value), true)
2012 && ranges_known_overlap_p (r->offset, r->size + 1,
2013 rafter->offset, rafter->size))
2015 r->size = MAX (r->offset + r->size,
2016 rafter->offset + rafter->size) - r->offset;
2017 splay_tree_remove (known_ranges, (splay_tree_key)&rafter->offset);
2019 /* If we get a clobber, fail. */
2020 if (TREE_CLOBBER_P (pd.rhs))
2021 return (void *)-1;
2022 /* Non-constants are OK as long as they are shadowed by a constant. */
2023 if (!pd_constant_p)
2024 return (void *)-1;
2025 partial_defs.safe_push (pd);
2027 /* Now we have merged newr into the range tree. When we have covered
2028 [offseti, sizei] then the tree will contain exactly one node which has
2029 the desired properties and it will be 'r'. */
2030 if (!known_subrange_p (0, maxsizei, r->offset, r->size))
2031 /* Continue looking for partial defs. */
2032 return NULL;
2034 /* Now simply native encode all partial defs in reverse order. */
2035 unsigned ndefs = partial_defs.length ();
2036 /* We support up to 512-bit values (for V8DFmode). */
2037 unsigned char buffer[bufsize + 1];
2038 unsigned char this_buffer[bufsize + 1];
2039 int len;
2041 memset (buffer, 0, bufsize + 1);
2042 unsigned needed_len = ROUND_UP (maxsizei, BITS_PER_UNIT) / BITS_PER_UNIT;
2043 while (!partial_defs.is_empty ())
2045 pd_data pd = partial_defs.pop ();
2046 unsigned int amnt;
2047 if (TREE_CODE (pd.rhs) == CONSTRUCTOR)
2049 /* Empty CONSTRUCTOR. */
2050 if (pd.size >= needed_len * BITS_PER_UNIT)
2051 len = needed_len;
2052 else
2053 len = ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT;
2054 memset (this_buffer, 0, len);
2056 else
2058 len = native_encode_expr (pd.rhs, this_buffer, bufsize,
2059 MAX (0, -pd.offset) / BITS_PER_UNIT);
2060 if (len <= 0
2061 || len < (ROUND_UP (pd.size, BITS_PER_UNIT) / BITS_PER_UNIT
2062 - MAX (0, -pd.offset) / BITS_PER_UNIT))
2064 if (dump_file && (dump_flags & TDF_DETAILS))
2065 fprintf (dump_file, "Failed to encode %u "
2066 "partial definitions\n", ndefs);
2067 return (void *)-1;
2071 unsigned char *p = buffer;
2072 HOST_WIDE_INT size = pd.size;
2073 if (pd.offset < 0)
2074 size -= ROUND_DOWN (-pd.offset, BITS_PER_UNIT);
2075 this_buffer[len] = 0;
2076 if (BYTES_BIG_ENDIAN)
2078 /* LSB of this_buffer[len - 1] byte should be at
2079 pd.offset + pd.size - 1 bits in buffer. */
2080 amnt = ((unsigned HOST_WIDE_INT) pd.offset
2081 + pd.size) % BITS_PER_UNIT;
2082 if (amnt)
2083 shift_bytes_in_array_right (this_buffer, len + 1, amnt);
2084 unsigned char *q = this_buffer;
2085 unsigned int off = 0;
2086 if (pd.offset >= 0)
2088 unsigned int msk;
2089 off = pd.offset / BITS_PER_UNIT;
2090 gcc_assert (off < needed_len);
2091 p = buffer + off;
2092 if (size <= amnt)
2094 msk = ((1 << size) - 1) << (BITS_PER_UNIT - amnt);
2095 *p = (*p & ~msk) | (this_buffer[len] & msk);
2096 size = 0;
2098 else
2100 if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2101 q = (this_buffer + len
2102 - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2103 / BITS_PER_UNIT));
2104 if (pd.offset % BITS_PER_UNIT)
2106 msk = -1U << (BITS_PER_UNIT
2107 - (pd.offset % BITS_PER_UNIT));
2108 *p = (*p & msk) | (*q & ~msk);
2109 p++;
2110 q++;
2111 off++;
2112 size -= BITS_PER_UNIT - (pd.offset % BITS_PER_UNIT);
2113 gcc_assert (size >= 0);
2117 else if (TREE_CODE (pd.rhs) != CONSTRUCTOR)
2119 q = (this_buffer + len
2120 - (ROUND_UP (size - amnt, BITS_PER_UNIT)
2121 / BITS_PER_UNIT));
2122 if (pd.offset % BITS_PER_UNIT)
2124 q++;
2125 size -= BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) pd.offset
2126 % BITS_PER_UNIT);
2127 gcc_assert (size >= 0);
2130 if ((unsigned HOST_WIDE_INT) size / BITS_PER_UNIT + off
2131 > needed_len)
2132 size = (needed_len - off) * BITS_PER_UNIT;
2133 memcpy (p, q, size / BITS_PER_UNIT);
2134 if (size % BITS_PER_UNIT)
2136 unsigned int msk
2137 = -1U << (BITS_PER_UNIT - (size % BITS_PER_UNIT));
2138 p += size / BITS_PER_UNIT;
2139 q += size / BITS_PER_UNIT;
2140 *p = (*q & msk) | (*p & ~msk);
2143 else
2145 if (pd.offset >= 0)
2147 /* LSB of this_buffer[0] byte should be at pd.offset bits
2148 in buffer. */
2149 unsigned int msk;
2150 size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2151 amnt = pd.offset % BITS_PER_UNIT;
2152 if (amnt)
2153 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2154 unsigned int off = pd.offset / BITS_PER_UNIT;
2155 gcc_assert (off < needed_len);
2156 size = MIN (size,
2157 (HOST_WIDE_INT) (needed_len - off) * BITS_PER_UNIT);
2158 p = buffer + off;
2159 if (amnt + size < BITS_PER_UNIT)
2161 /* Low amnt bits come from *p, then size bits
2162 from this_buffer[0] and the remaining again from
2163 *p. */
2164 msk = ((1 << size) - 1) << amnt;
2165 *p = (*p & ~msk) | (this_buffer[0] & msk);
2166 size = 0;
2168 else if (amnt)
2170 msk = -1U << amnt;
2171 *p = (*p & ~msk) | (this_buffer[0] & msk);
2172 p++;
2173 size -= (BITS_PER_UNIT - amnt);
2176 else
2178 amnt = (unsigned HOST_WIDE_INT) pd.offset % BITS_PER_UNIT;
2179 if (amnt)
2180 size -= BITS_PER_UNIT - amnt;
2181 size = MIN (size, (HOST_WIDE_INT) needed_len * BITS_PER_UNIT);
2182 if (amnt)
2183 shift_bytes_in_array_left (this_buffer, len + 1, amnt);
2185 memcpy (p, this_buffer + (amnt != 0), size / BITS_PER_UNIT);
2186 p += size / BITS_PER_UNIT;
2187 if (size % BITS_PER_UNIT)
2189 unsigned int msk = -1U << (size % BITS_PER_UNIT);
2190 *p = (this_buffer[(amnt != 0) + size / BITS_PER_UNIT]
2191 & ~msk) | (*p & msk);
2196 tree type = vr->type;
2197 /* Make sure to interpret in a type that has a range covering the whole
2198 access size. */
2199 if (INTEGRAL_TYPE_P (vr->type) && maxsizei != TYPE_PRECISION (vr->type))
2200 type = build_nonstandard_integer_type (maxsizei, TYPE_UNSIGNED (type));
2201 tree val;
2202 if (BYTES_BIG_ENDIAN)
2204 unsigned sz = needed_len;
2205 if (maxsizei % BITS_PER_UNIT)
2206 shift_bytes_in_array_right (buffer, needed_len,
2207 BITS_PER_UNIT
2208 - (maxsizei % BITS_PER_UNIT));
2209 if (INTEGRAL_TYPE_P (type))
2210 sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
2211 if (sz > needed_len)
2213 memcpy (this_buffer + (sz - needed_len), buffer, needed_len);
2214 val = native_interpret_expr (type, this_buffer, sz);
2216 else
2217 val = native_interpret_expr (type, buffer, needed_len);
2219 else
2220 val = native_interpret_expr (type, buffer, bufsize);
2221 /* If we chop off bits because the types precision doesn't match the memory
2222 access size this is ok when optimizing reads but not when called from
2223 the DSE code during elimination. */
2224 if (val && type != vr->type)
2226 if (! int_fits_type_p (val, vr->type))
2227 val = NULL_TREE;
2228 else
2229 val = fold_convert (vr->type, val);
2232 if (val)
2234 if (dump_file && (dump_flags & TDF_DETAILS))
2235 fprintf (dump_file,
2236 "Successfully combined %u partial definitions\n", ndefs);
2237 /* We are using the alias-set of the first store we encounter which
2238 should be appropriate here. */
2239 return finish (first_set, first_base_set, val);
2241 else
2243 if (dump_file && (dump_flags & TDF_DETAILS))
2244 fprintf (dump_file,
2245 "Failed to interpret %u encoded partial definitions\n", ndefs);
2246 return (void *)-1;
2250 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
2251 with the current VUSE and performs the expression lookup. */
2253 static void *
2254 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, void *data_)
2256 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2257 vn_reference_t vr = data->vr;
2258 vn_reference_s **slot;
2259 hashval_t hash;
2261 /* If we have partial definitions recorded we have to go through
2262 vn_reference_lookup_3. */
2263 if (!data->partial_defs.is_empty ())
2264 return NULL;
2266 if (data->last_vuse_ptr)
2268 *data->last_vuse_ptr = vuse;
2269 data->last_vuse = vuse;
2272 /* Fixup vuse and hash. */
2273 if (vr->vuse)
2274 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
2275 vr->vuse = vuse_ssa_val (vuse);
2276 if (vr->vuse)
2277 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
2279 hash = vr->hashcode;
2280 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
2281 if (slot)
2283 if ((*slot)->result && data->saved_operands.exists ())
2284 return data->finish (vr->set, vr->base_set, (*slot)->result);
2285 return *slot;
2288 return NULL;
2291 /* Lookup an existing or insert a new vn_reference entry into the
2292 value table for the VUSE, SET, TYPE, OPERANDS reference which
2293 has the value VALUE which is either a constant or an SSA name. */
2295 static vn_reference_t
2296 vn_reference_lookup_or_insert_for_pieces (tree vuse,
2297 alias_set_type set,
2298 alias_set_type base_set,
2299 tree type,
2300 vec<vn_reference_op_s,
2301 va_heap> operands,
2302 tree value)
2304 vn_reference_s vr1;
2305 vn_reference_t result;
2306 unsigned value_id;
2307 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2308 vr1.operands = operands;
2309 vr1.type = type;
2310 vr1.set = set;
2311 vr1.base_set = base_set;
2312 vr1.hashcode = vn_reference_compute_hash (&vr1);
2313 if (vn_reference_lookup_1 (&vr1, &result))
2314 return result;
2315 if (TREE_CODE (value) == SSA_NAME)
2316 value_id = VN_INFO (value)->value_id;
2317 else
2318 value_id = get_or_alloc_constant_value_id (value);
2319 return vn_reference_insert_pieces (vuse, set, base_set, type,
2320 operands.copy (), value, value_id);
2323 /* Return a value-number for RCODE OPS... either by looking up an existing
2324 value-number for the possibly simplified result or by inserting the
2325 operation if INSERT is true. If SIMPLIFY is false, return a value
2326 number for the unsimplified expression. */
2328 static tree
2329 vn_nary_build_or_lookup_1 (gimple_match_op *res_op, bool insert,
2330 bool simplify)
2332 tree result = NULL_TREE;
2333 /* We will be creating a value number for
2334 RCODE (OPS...).
2335 So first simplify and lookup this expression to see if it
2336 is already available. */
2337 /* For simplification valueize. */
2338 unsigned i = 0;
2339 if (simplify)
2340 for (i = 0; i < res_op->num_ops; ++i)
2341 if (TREE_CODE (res_op->ops[i]) == SSA_NAME)
2343 tree tem = vn_valueize (res_op->ops[i]);
2344 if (!tem)
2345 break;
2346 res_op->ops[i] = tem;
2348 /* If valueization of an operand fails (it is not available), skip
2349 simplification. */
2350 bool res = false;
2351 if (i == res_op->num_ops)
2353 mprts_hook = vn_lookup_simplify_result;
2354 res = res_op->resimplify (NULL, vn_valueize);
2355 mprts_hook = NULL;
2357 gimple *new_stmt = NULL;
2358 if (res
2359 && gimple_simplified_result_is_gimple_val (res_op))
2361 /* The expression is already available. */
2362 result = res_op->ops[0];
2363 /* Valueize it, simplification returns sth in AVAIL only. */
2364 if (TREE_CODE (result) == SSA_NAME)
2365 result = SSA_VAL (result);
2367 else
2369 tree val = vn_lookup_simplify_result (res_op);
2370 if (!val && insert)
2372 gimple_seq stmts = NULL;
2373 result = maybe_push_res_to_seq (res_op, &stmts);
2374 if (result)
2376 gcc_assert (gimple_seq_singleton_p (stmts));
2377 new_stmt = gimple_seq_first_stmt (stmts);
2380 else
2381 /* The expression is already available. */
2382 result = val;
2384 if (new_stmt)
2386 /* The expression is not yet available, value-number lhs to
2387 the new SSA_NAME we created. */
2388 /* Initialize value-number information properly. */
2389 vn_ssa_aux_t result_info = VN_INFO (result);
2390 result_info->valnum = result;
2391 result_info->value_id = get_next_value_id ();
2392 result_info->visited = 1;
2393 gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr,
2394 new_stmt);
2395 result_info->needs_insertion = true;
2396 /* ??? PRE phi-translation inserts NARYs without corresponding
2397 SSA name result. Re-use those but set their result according
2398 to the stmt we just built. */
2399 vn_nary_op_t nary = NULL;
2400 vn_nary_op_lookup_stmt (new_stmt, &nary);
2401 if (nary)
2403 gcc_assert (! nary->predicated_values && nary->u.result == NULL_TREE);
2404 nary->u.result = gimple_assign_lhs (new_stmt);
2406 /* As all "inserted" statements are singleton SCCs, insert
2407 to the valid table. This is strictly needed to
2408 avoid re-generating new value SSA_NAMEs for the same
2409 expression during SCC iteration over and over (the
2410 optimistic table gets cleared after each iteration).
2411 We do not need to insert into the optimistic table, as
2412 lookups there will fall back to the valid table. */
2413 else
2415 unsigned int length = vn_nary_length_from_stmt (new_stmt);
2416 vn_nary_op_t vno1
2417 = alloc_vn_nary_op_noinit (length, &vn_tables_insert_obstack);
2418 vno1->value_id = result_info->value_id;
2419 vno1->length = length;
2420 vno1->predicated_values = 0;
2421 vno1->u.result = result;
2422 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (new_stmt));
2423 vn_nary_op_insert_into (vno1, valid_info->nary, true);
2424 /* Also do not link it into the undo chain. */
2425 last_inserted_nary = vno1->next;
2426 vno1->next = (vn_nary_op_t)(void *)-1;
2428 if (dump_file && (dump_flags & TDF_DETAILS))
2430 fprintf (dump_file, "Inserting name ");
2431 print_generic_expr (dump_file, result);
2432 fprintf (dump_file, " for expression ");
2433 print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM);
2434 fprintf (dump_file, "\n");
2437 return result;
2440 /* Return a value-number for RCODE OPS... either by looking up an existing
2441 value-number for the simplified result or by inserting the operation. */
2443 static tree
2444 vn_nary_build_or_lookup (gimple_match_op *res_op)
2446 return vn_nary_build_or_lookup_1 (res_op, true, true);
2449 /* Try to simplify the expression RCODE OPS... of type TYPE and return
2450 its value if present. */
2452 tree
2453 vn_nary_simplify (vn_nary_op_t nary)
2455 if (nary->length > gimple_match_op::MAX_NUM_OPS)
2456 return NULL_TREE;
2457 gimple_match_op op (gimple_match_cond::UNCOND, nary->opcode,
2458 nary->type, nary->length);
2459 memcpy (op.ops, nary->op, sizeof (tree) * nary->length);
2460 return vn_nary_build_or_lookup_1 (&op, false, true);
2463 /* Elimination engine. */
2465 class eliminate_dom_walker : public dom_walker
2467 public:
2468 eliminate_dom_walker (cdi_direction, bitmap);
2469 ~eliminate_dom_walker ();
2471 virtual edge before_dom_children (basic_block);
2472 virtual void after_dom_children (basic_block);
2474 virtual tree eliminate_avail (basic_block, tree op);
2475 virtual void eliminate_push_avail (basic_block, tree op);
2476 tree eliminate_insert (basic_block, gimple_stmt_iterator *gsi, tree val);
2478 void eliminate_stmt (basic_block, gimple_stmt_iterator *);
2480 unsigned eliminate_cleanup (bool region_p = false);
2482 bool do_pre;
2483 unsigned int el_todo;
2484 unsigned int eliminations;
2485 unsigned int insertions;
2487 /* SSA names that had their defs inserted by PRE if do_pre. */
2488 bitmap inserted_exprs;
2490 /* Blocks with statements that have had their EH properties changed. */
2491 bitmap need_eh_cleanup;
2493 /* Blocks with statements that have had their AB properties changed. */
2494 bitmap need_ab_cleanup;
2496 /* Local state for the eliminate domwalk. */
2497 auto_vec<gimple *> to_remove;
2498 auto_vec<gimple *> to_fixup;
2499 auto_vec<tree> avail;
2500 auto_vec<tree> avail_stack;
2503 /* Adaptor to the elimination engine using RPO availability. */
2505 class rpo_elim : public eliminate_dom_walker
2507 public:
2508 rpo_elim(basic_block entry_)
2509 : eliminate_dom_walker (CDI_DOMINATORS, NULL), entry (entry_),
2510 m_avail_freelist (NULL) {}
2512 virtual tree eliminate_avail (basic_block, tree op);
2514 virtual void eliminate_push_avail (basic_block, tree);
2516 basic_block entry;
2517 /* Freelist of avail entries which are allocated from the vn_ssa_aux
2518 obstack. */
2519 vn_avail *m_avail_freelist;
2522 /* Global RPO state for access from hooks. */
2523 static eliminate_dom_walker *rpo_avail;
2524 basic_block vn_context_bb;
2526 /* Return true if BASE1 and BASE2 can be adjusted so they have the
2527 same address and adjust *OFFSET1 and *OFFSET2 accordingly.
2528 Otherwise return false. */
2530 static bool
2531 adjust_offsets_for_equal_base_address (tree base1, poly_int64 *offset1,
2532 tree base2, poly_int64 *offset2)
2534 poly_int64 soff;
2535 if (TREE_CODE (base1) == MEM_REF
2536 && TREE_CODE (base2) == MEM_REF)
2538 if (mem_ref_offset (base1).to_shwi (&soff))
2540 base1 = TREE_OPERAND (base1, 0);
2541 *offset1 += soff * BITS_PER_UNIT;
2543 if (mem_ref_offset (base2).to_shwi (&soff))
2545 base2 = TREE_OPERAND (base2, 0);
2546 *offset2 += soff * BITS_PER_UNIT;
2548 return operand_equal_p (base1, base2, 0);
2550 return operand_equal_p (base1, base2, OEP_ADDRESS_OF);
2553 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
2554 from the statement defining VUSE and if not successful tries to
2555 translate *REFP and VR_ through an aggregate copy at the definition
2556 of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation
2557 of *REF and *VR. If only disambiguation was performed then
2558 *DISAMBIGUATE_ONLY is set to true. */
2560 static void *
2561 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
2562 translate_flags *disambiguate_only)
2564 vn_walk_cb_data *data = (vn_walk_cb_data *)data_;
2565 vn_reference_t vr = data->vr;
2566 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2567 tree base = ao_ref_base (ref);
2568 HOST_WIDE_INT offseti = 0, maxsizei, sizei = 0;
2569 static vec<vn_reference_op_s> lhs_ops;
2570 ao_ref lhs_ref;
2571 bool lhs_ref_ok = false;
2572 poly_int64 copy_size;
2574 /* First try to disambiguate after value-replacing in the definitions LHS. */
2575 if (is_gimple_assign (def_stmt))
2577 tree lhs = gimple_assign_lhs (def_stmt);
2578 bool valueized_anything = false;
2579 /* Avoid re-allocation overhead. */
2580 lhs_ops.truncate (0);
2581 basic_block saved_rpo_bb = vn_context_bb;
2582 vn_context_bb = gimple_bb (def_stmt);
2583 if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE)
2585 copy_reference_ops_from_ref (lhs, &lhs_ops);
2586 valueize_refs_1 (&lhs_ops, &valueized_anything, true);
2588 vn_context_bb = saved_rpo_bb;
2589 ao_ref_init (&lhs_ref, lhs);
2590 lhs_ref_ok = true;
2591 if (valueized_anything
2592 && ao_ref_init_from_vn_reference
2593 (&lhs_ref, ao_ref_alias_set (&lhs_ref),
2594 ao_ref_base_alias_set (&lhs_ref), TREE_TYPE (lhs), lhs_ops)
2595 && !refs_may_alias_p_1 (ref, &lhs_ref, data->tbaa_p))
2597 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2598 return NULL;
2601 /* Besides valueizing the LHS we can also use access-path based
2602 disambiguation on the original non-valueized ref. */
2603 if (!ref->ref
2604 && lhs_ref_ok
2605 && data->orig_ref.ref)
2607 /* We want to use the non-valueized LHS for this, but avoid redundant
2608 work. */
2609 ao_ref *lref = &lhs_ref;
2610 ao_ref lref_alt;
2611 if (valueized_anything)
2613 ao_ref_init (&lref_alt, lhs);
2614 lref = &lref_alt;
2616 if (!refs_may_alias_p_1 (&data->orig_ref, lref, data->tbaa_p))
2618 *disambiguate_only = (valueized_anything
2619 ? TR_VALUEIZE_AND_DISAMBIGUATE
2620 : TR_DISAMBIGUATE);
2621 return NULL;
2625 /* If we reach a clobbering statement try to skip it and see if
2626 we find a VN result with exactly the same value as the
2627 possible clobber. In this case we can ignore the clobber
2628 and return the found value. */
2629 if (is_gimple_reg_type (TREE_TYPE (lhs))
2630 && types_compatible_p (TREE_TYPE (lhs), vr->type)
2631 && (ref->ref || data->orig_ref.ref))
2633 tree *saved_last_vuse_ptr = data->last_vuse_ptr;
2634 /* Do not update last_vuse_ptr in vn_reference_lookup_2. */
2635 data->last_vuse_ptr = NULL;
2636 tree saved_vuse = vr->vuse;
2637 hashval_t saved_hashcode = vr->hashcode;
2638 void *res = vn_reference_lookup_2 (ref, gimple_vuse (def_stmt), data);
2639 /* Need to restore vr->vuse and vr->hashcode. */
2640 vr->vuse = saved_vuse;
2641 vr->hashcode = saved_hashcode;
2642 data->last_vuse_ptr = saved_last_vuse_ptr;
2643 if (res && res != (void *)-1)
2645 vn_reference_t vnresult = (vn_reference_t) res;
2646 tree rhs = gimple_assign_rhs1 (def_stmt);
2647 if (TREE_CODE (rhs) == SSA_NAME)
2648 rhs = SSA_VAL (rhs);
2649 if (vnresult->result
2650 && operand_equal_p (vnresult->result, rhs, 0)
2651 /* We have to honor our promise about union type punning
2652 and also support arbitrary overlaps with
2653 -fno-strict-aliasing. So simply resort to alignment to
2654 rule out overlaps. Do this check last because it is
2655 quite expensive compared to the hash-lookup above. */
2656 && multiple_p (get_object_alignment
2657 (ref->ref ? ref->ref : data->orig_ref.ref),
2658 ref->size)
2659 && multiple_p (get_object_alignment (lhs), ref->size))
2660 return res;
2664 else if (*disambiguate_only <= TR_VALUEIZE_AND_DISAMBIGUATE
2665 && gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
2666 && gimple_call_num_args (def_stmt) <= 4)
2668 /* For builtin calls valueize its arguments and call the
2669 alias oracle again. Valueization may improve points-to
2670 info of pointers and constify size and position arguments.
2671 Originally this was motivated by PR61034 which has
2672 conditional calls to free falsely clobbering ref because
2673 of imprecise points-to info of the argument. */
2674 tree oldargs[4];
2675 bool valueized_anything = false;
2676 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2678 oldargs[i] = gimple_call_arg (def_stmt, i);
2679 tree val = vn_valueize (oldargs[i]);
2680 if (val != oldargs[i])
2682 gimple_call_set_arg (def_stmt, i, val);
2683 valueized_anything = true;
2686 if (valueized_anything)
2688 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
2689 ref, data->tbaa_p);
2690 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
2691 gimple_call_set_arg (def_stmt, i, oldargs[i]);
2692 if (!res)
2694 *disambiguate_only = TR_VALUEIZE_AND_DISAMBIGUATE;
2695 return NULL;
2700 if (*disambiguate_only > TR_TRANSLATE)
2701 return (void *)-1;
2703 /* If we cannot constrain the size of the reference we cannot
2704 test if anything kills it. */
2705 if (!ref->max_size_known_p ())
2706 return (void *)-1;
2708 poly_int64 offset = ref->offset;
2709 poly_int64 maxsize = ref->max_size;
2711 /* def_stmt may-defs *ref. See if we can derive a value for *ref
2712 from that definition.
2713 1) Memset. */
2714 if (is_gimple_reg_type (vr->type)
2715 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
2716 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET_CHK))
2717 && (integer_zerop (gimple_call_arg (def_stmt, 1))
2718 || ((TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST
2719 || (INTEGRAL_TYPE_P (vr->type) && known_eq (ref->size, 8)))
2720 && CHAR_BIT == 8
2721 && BITS_PER_UNIT == 8
2722 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2723 && offset.is_constant (&offseti)
2724 && ref->size.is_constant (&sizei)
2725 && (offseti % BITS_PER_UNIT == 0
2726 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == INTEGER_CST)))
2727 && (poly_int_tree_p (gimple_call_arg (def_stmt, 2))
2728 || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
2729 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)))))
2730 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2731 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME))
2733 tree base2;
2734 poly_int64 offset2, size2, maxsize2;
2735 bool reverse;
2736 tree ref2 = gimple_call_arg (def_stmt, 0);
2737 if (TREE_CODE (ref2) == SSA_NAME)
2739 ref2 = SSA_VAL (ref2);
2740 if (TREE_CODE (ref2) == SSA_NAME
2741 && (TREE_CODE (base) != MEM_REF
2742 || TREE_OPERAND (base, 0) != ref2))
2744 gimple *def_stmt = SSA_NAME_DEF_STMT (ref2);
2745 if (gimple_assign_single_p (def_stmt)
2746 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2747 ref2 = gimple_assign_rhs1 (def_stmt);
2750 if (TREE_CODE (ref2) == ADDR_EXPR)
2752 ref2 = TREE_OPERAND (ref2, 0);
2753 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
2754 &reverse);
2755 if (!known_size_p (maxsize2)
2756 || !known_eq (maxsize2, size2)
2757 || !operand_equal_p (base, base2, OEP_ADDRESS_OF))
2758 return (void *)-1;
2760 else if (TREE_CODE (ref2) == SSA_NAME)
2762 poly_int64 soff;
2763 if (TREE_CODE (base) != MEM_REF
2764 || !(mem_ref_offset (base)
2765 << LOG2_BITS_PER_UNIT).to_shwi (&soff))
2766 return (void *)-1;
2767 offset += soff;
2768 offset2 = 0;
2769 if (TREE_OPERAND (base, 0) != ref2)
2771 gimple *def = SSA_NAME_DEF_STMT (ref2);
2772 if (is_gimple_assign (def)
2773 && gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
2774 && gimple_assign_rhs1 (def) == TREE_OPERAND (base, 0)
2775 && poly_int_tree_p (gimple_assign_rhs2 (def)))
2777 tree rhs2 = gimple_assign_rhs2 (def);
2778 if (!(poly_offset_int::from (wi::to_poly_wide (rhs2),
2779 SIGNED)
2780 << LOG2_BITS_PER_UNIT).to_shwi (&offset2))
2781 return (void *)-1;
2782 ref2 = gimple_assign_rhs1 (def);
2783 if (TREE_CODE (ref2) == SSA_NAME)
2784 ref2 = SSA_VAL (ref2);
2786 else
2787 return (void *)-1;
2790 else
2791 return (void *)-1;
2792 tree len = gimple_call_arg (def_stmt, 2);
2793 HOST_WIDE_INT leni, offset2i;
2794 if (TREE_CODE (len) == SSA_NAME)
2795 len = SSA_VAL (len);
2796 /* Sometimes the above trickery is smarter than alias analysis. Take
2797 advantage of that. */
2798 if (!ranges_maybe_overlap_p (offset, maxsize, offset2,
2799 (wi::to_poly_offset (len)
2800 << LOG2_BITS_PER_UNIT)))
2801 return NULL;
2802 if (data->partial_defs.is_empty ()
2803 && known_subrange_p (offset, maxsize, offset2,
2804 wi::to_poly_offset (len) << LOG2_BITS_PER_UNIT))
2806 tree val;
2807 if (integer_zerop (gimple_call_arg (def_stmt, 1)))
2808 val = build_zero_cst (vr->type);
2809 else if (INTEGRAL_TYPE_P (vr->type)
2810 && known_eq (ref->size, 8)
2811 && offseti % BITS_PER_UNIT == 0)
2813 gimple_match_op res_op (gimple_match_cond::UNCOND, NOP_EXPR,
2814 vr->type, gimple_call_arg (def_stmt, 1));
2815 val = vn_nary_build_or_lookup (&res_op);
2816 if (!val
2817 || (TREE_CODE (val) == SSA_NAME
2818 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
2819 return (void *)-1;
2821 else
2823 unsigned buflen = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (vr->type)) + 1;
2824 if (INTEGRAL_TYPE_P (vr->type))
2825 buflen = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (vr->type)) + 1;
2826 unsigned char *buf = XALLOCAVEC (unsigned char, buflen);
2827 memset (buf, TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 1)),
2828 buflen);
2829 if (BYTES_BIG_ENDIAN)
2831 unsigned int amnt
2832 = (((unsigned HOST_WIDE_INT) offseti + sizei)
2833 % BITS_PER_UNIT);
2834 if (amnt)
2836 shift_bytes_in_array_right (buf, buflen,
2837 BITS_PER_UNIT - amnt);
2838 buf++;
2839 buflen--;
2842 else if (offseti % BITS_PER_UNIT != 0)
2844 unsigned int amnt
2845 = BITS_PER_UNIT - ((unsigned HOST_WIDE_INT) offseti
2846 % BITS_PER_UNIT);
2847 shift_bytes_in_array_left (buf, buflen, amnt);
2848 buf++;
2849 buflen--;
2851 val = native_interpret_expr (vr->type, buf, buflen);
2852 if (!val)
2853 return (void *)-1;
2855 return data->finish (0, 0, val);
2857 /* For now handle clearing memory with partial defs. */
2858 else if (known_eq (ref->size, maxsize)
2859 && integer_zerop (gimple_call_arg (def_stmt, 1))
2860 && tree_fits_poly_int64_p (len)
2861 && tree_to_poly_int64 (len).is_constant (&leni)
2862 && leni <= INTTYPE_MAXIMUM (HOST_WIDE_INT) / BITS_PER_UNIT
2863 && offset.is_constant (&offseti)
2864 && offset2.is_constant (&offset2i)
2865 && maxsize.is_constant (&maxsizei)
2866 && ranges_known_overlap_p (offseti, maxsizei, offset2i,
2867 leni << LOG2_BITS_PER_UNIT))
2869 pd_data pd;
2870 pd.rhs = build_constructor (NULL_TREE, NULL);
2871 pd.offset = offset2i;
2872 pd.size = leni << LOG2_BITS_PER_UNIT;
2873 return data->push_partial_def (pd, 0, 0, offseti, maxsizei);
2877 /* 2) Assignment from an empty CONSTRUCTOR. */
2878 else if (is_gimple_reg_type (vr->type)
2879 && gimple_assign_single_p (def_stmt)
2880 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
2881 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
2883 tree base2;
2884 poly_int64 offset2, size2, maxsize2;
2885 HOST_WIDE_INT offset2i, size2i;
2886 gcc_assert (lhs_ref_ok);
2887 base2 = ao_ref_base (&lhs_ref);
2888 offset2 = lhs_ref.offset;
2889 size2 = lhs_ref.size;
2890 maxsize2 = lhs_ref.max_size;
2891 if (known_size_p (maxsize2)
2892 && known_eq (maxsize2, size2)
2893 && adjust_offsets_for_equal_base_address (base, &offset,
2894 base2, &offset2))
2896 if (data->partial_defs.is_empty ()
2897 && known_subrange_p (offset, maxsize, offset2, size2))
2899 /* While technically undefined behavior do not optimize
2900 a full read from a clobber. */
2901 if (gimple_clobber_p (def_stmt))
2902 return (void *)-1;
2903 tree val = build_zero_cst (vr->type);
2904 return data->finish (ao_ref_alias_set (&lhs_ref),
2905 ao_ref_base_alias_set (&lhs_ref), val);
2907 else if (known_eq (ref->size, maxsize)
2908 && maxsize.is_constant (&maxsizei)
2909 && offset.is_constant (&offseti)
2910 && offset2.is_constant (&offset2i)
2911 && size2.is_constant (&size2i)
2912 && ranges_known_overlap_p (offseti, maxsizei,
2913 offset2i, size2i))
2915 /* Let clobbers be consumed by the partial-def tracker
2916 which can choose to ignore them if they are shadowed
2917 by a later def. */
2918 pd_data pd;
2919 pd.rhs = gimple_assign_rhs1 (def_stmt);
2920 pd.offset = offset2i;
2921 pd.size = size2i;
2922 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
2923 ao_ref_base_alias_set (&lhs_ref),
2924 offseti, maxsizei);
2929 /* 3) Assignment from a constant. We can use folds native encode/interpret
2930 routines to extract the assigned bits. */
2931 else if (known_eq (ref->size, maxsize)
2932 && is_gimple_reg_type (vr->type)
2933 && !reverse_storage_order_for_component_p (vr->operands)
2934 && !contains_storage_order_barrier_p (vr->operands)
2935 && gimple_assign_single_p (def_stmt)
2936 && CHAR_BIT == 8
2937 && BITS_PER_UNIT == 8
2938 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
2939 /* native_encode and native_decode operate on arrays of bytes
2940 and so fundamentally need a compile-time size and offset. */
2941 && maxsize.is_constant (&maxsizei)
2942 && offset.is_constant (&offseti)
2943 && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt))
2944 || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
2945 && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt))))))
2947 tree lhs = gimple_assign_lhs (def_stmt);
2948 tree base2;
2949 poly_int64 offset2, size2, maxsize2;
2950 HOST_WIDE_INT offset2i, size2i;
2951 bool reverse;
2952 gcc_assert (lhs_ref_ok);
2953 base2 = ao_ref_base (&lhs_ref);
2954 offset2 = lhs_ref.offset;
2955 size2 = lhs_ref.size;
2956 maxsize2 = lhs_ref.max_size;
2957 reverse = reverse_storage_order_for_component_p (lhs);
2958 if (base2
2959 && !reverse
2960 && !storage_order_barrier_p (lhs)
2961 && known_eq (maxsize2, size2)
2962 && adjust_offsets_for_equal_base_address (base, &offset,
2963 base2, &offset2)
2964 && offset.is_constant (&offseti)
2965 && offset2.is_constant (&offset2i)
2966 && size2.is_constant (&size2i))
2968 if (data->partial_defs.is_empty ()
2969 && known_subrange_p (offseti, maxsizei, offset2, size2))
2971 /* We support up to 512-bit values (for V8DFmode). */
2972 unsigned char buffer[65];
2973 int len;
2975 tree rhs = gimple_assign_rhs1 (def_stmt);
2976 if (TREE_CODE (rhs) == SSA_NAME)
2977 rhs = SSA_VAL (rhs);
2978 len = native_encode_expr (rhs,
2979 buffer, sizeof (buffer) - 1,
2980 (offseti - offset2i) / BITS_PER_UNIT);
2981 if (len > 0 && len * BITS_PER_UNIT >= maxsizei)
2983 tree type = vr->type;
2984 unsigned char *buf = buffer;
2985 unsigned int amnt = 0;
2986 /* Make sure to interpret in a type that has a range
2987 covering the whole access size. */
2988 if (INTEGRAL_TYPE_P (vr->type)
2989 && maxsizei != TYPE_PRECISION (vr->type))
2990 type = build_nonstandard_integer_type (maxsizei,
2991 TYPE_UNSIGNED (type));
2992 if (BYTES_BIG_ENDIAN)
2994 /* For big-endian native_encode_expr stored the rhs
2995 such that the LSB of it is the LSB of buffer[len - 1].
2996 That bit is stored into memory at position
2997 offset2 + size2 - 1, i.e. in byte
2998 base + (offset2 + size2 - 1) / BITS_PER_UNIT.
2999 E.g. for offset2 1 and size2 14, rhs -1 and memory
3000 previously cleared that is:
3002 01111111|11111110
3003 Now, if we want to extract offset 2 and size 12 from
3004 it using native_interpret_expr (which actually works
3005 for integral bitfield types in terms of byte size of
3006 the mode), the native_encode_expr stored the value
3007 into buffer as
3008 XX111111|11111111
3009 and returned len 2 (the X bits are outside of
3010 precision).
3011 Let sz be maxsize / BITS_PER_UNIT if not extracting
3012 a bitfield, and GET_MODE_SIZE otherwise.
3013 We need to align the LSB of the value we want to
3014 extract as the LSB of buf[sz - 1].
3015 The LSB from memory we need to read is at position
3016 offset + maxsize - 1. */
3017 HOST_WIDE_INT sz = maxsizei / BITS_PER_UNIT;
3018 if (INTEGRAL_TYPE_P (type))
3019 sz = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
3020 amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3021 - offseti - maxsizei) % BITS_PER_UNIT;
3022 if (amnt)
3023 shift_bytes_in_array_right (buffer, len, amnt);
3024 amnt = ((unsigned HOST_WIDE_INT) offset2i + size2i
3025 - offseti - maxsizei - amnt) / BITS_PER_UNIT;
3026 if ((unsigned HOST_WIDE_INT) sz + amnt > (unsigned) len)
3027 len = 0;
3028 else
3030 buf = buffer + len - sz - amnt;
3031 len -= (buf - buffer);
3034 else
3036 amnt = ((unsigned HOST_WIDE_INT) offset2i
3037 - offseti) % BITS_PER_UNIT;
3038 if (amnt)
3040 buffer[len] = 0;
3041 shift_bytes_in_array_left (buffer, len + 1, amnt);
3042 buf = buffer + 1;
3045 tree val = native_interpret_expr (type, buf, len);
3046 /* If we chop off bits because the types precision doesn't
3047 match the memory access size this is ok when optimizing
3048 reads but not when called from the DSE code during
3049 elimination. */
3050 if (val
3051 && type != vr->type)
3053 if (! int_fits_type_p (val, vr->type))
3054 val = NULL_TREE;
3055 else
3056 val = fold_convert (vr->type, val);
3059 if (val)
3060 return data->finish (ao_ref_alias_set (&lhs_ref),
3061 ao_ref_base_alias_set (&lhs_ref), val);
3064 else if (ranges_known_overlap_p (offseti, maxsizei, offset2i,
3065 size2i))
3067 pd_data pd;
3068 tree rhs = gimple_assign_rhs1 (def_stmt);
3069 if (TREE_CODE (rhs) == SSA_NAME)
3070 rhs = SSA_VAL (rhs);
3071 pd.rhs = rhs;
3072 pd.offset = offset2i;
3073 pd.size = size2i;
3074 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3075 ao_ref_base_alias_set (&lhs_ref),
3076 offseti, maxsizei);
3081 /* 4) Assignment from an SSA name which definition we may be able
3082 to access pieces from or we can combine to a larger entity. */
3083 else if (known_eq (ref->size, maxsize)
3084 && is_gimple_reg_type (vr->type)
3085 && !reverse_storage_order_for_component_p (vr->operands)
3086 && !contains_storage_order_barrier_p (vr->operands)
3087 && gimple_assign_single_p (def_stmt)
3088 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
3090 tree lhs = gimple_assign_lhs (def_stmt);
3091 tree base2;
3092 poly_int64 offset2, size2, maxsize2;
3093 HOST_WIDE_INT offset2i, size2i, offseti;
3094 bool reverse;
3095 gcc_assert (lhs_ref_ok);
3096 base2 = ao_ref_base (&lhs_ref);
3097 offset2 = lhs_ref.offset;
3098 size2 = lhs_ref.size;
3099 maxsize2 = lhs_ref.max_size;
3100 reverse = reverse_storage_order_for_component_p (lhs);
3101 tree def_rhs = gimple_assign_rhs1 (def_stmt);
3102 if (!reverse
3103 && !storage_order_barrier_p (lhs)
3104 && known_size_p (maxsize2)
3105 && known_eq (maxsize2, size2)
3106 && adjust_offsets_for_equal_base_address (base, &offset,
3107 base2, &offset2))
3109 if (data->partial_defs.is_empty ()
3110 && known_subrange_p (offset, maxsize, offset2, size2)
3111 /* ??? We can't handle bitfield precision extracts without
3112 either using an alternate type for the BIT_FIELD_REF and
3113 then doing a conversion or possibly adjusting the offset
3114 according to endianness. */
3115 && (! INTEGRAL_TYPE_P (vr->type)
3116 || known_eq (ref->size, TYPE_PRECISION (vr->type)))
3117 && multiple_p (ref->size, BITS_PER_UNIT))
3119 tree val = NULL_TREE;
3120 if (! INTEGRAL_TYPE_P (TREE_TYPE (def_rhs))
3121 || type_has_mode_precision_p (TREE_TYPE (def_rhs)))
3123 gimple_match_op op (gimple_match_cond::UNCOND,
3124 BIT_FIELD_REF, vr->type,
3125 SSA_VAL (def_rhs),
3126 bitsize_int (ref->size),
3127 bitsize_int (offset - offset2));
3128 val = vn_nary_build_or_lookup (&op);
3130 else if (known_eq (ref->size, size2))
3132 gimple_match_op op (gimple_match_cond::UNCOND,
3133 VIEW_CONVERT_EXPR, vr->type,
3134 SSA_VAL (def_rhs));
3135 val = vn_nary_build_or_lookup (&op);
3137 if (val
3138 && (TREE_CODE (val) != SSA_NAME
3139 || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val)))
3140 return data->finish (ao_ref_alias_set (&lhs_ref),
3141 ao_ref_base_alias_set (&lhs_ref), val);
3143 else if (maxsize.is_constant (&maxsizei)
3144 && offset.is_constant (&offseti)
3145 && offset2.is_constant (&offset2i)
3146 && size2.is_constant (&size2i)
3147 && ranges_known_overlap_p (offset, maxsize, offset2, size2))
3149 pd_data pd;
3150 pd.rhs = SSA_VAL (def_rhs);
3151 pd.offset = offset2i;
3152 pd.size = size2i;
3153 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3154 ao_ref_base_alias_set (&lhs_ref),
3155 offseti, maxsizei);
3160 /* 5) For aggregate copies translate the reference through them if
3161 the copy kills ref. */
3162 else if (data->vn_walk_kind == VN_WALKREWRITE
3163 && gimple_assign_single_p (def_stmt)
3164 && (DECL_P (gimple_assign_rhs1 (def_stmt))
3165 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
3166 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
3168 tree base2;
3169 int i, j, k;
3170 auto_vec<vn_reference_op_s> rhs;
3171 vn_reference_op_t vro;
3172 ao_ref r;
3174 gcc_assert (lhs_ref_ok);
3176 /* See if the assignment kills REF. */
3177 base2 = ao_ref_base (&lhs_ref);
3178 if (!lhs_ref.max_size_known_p ()
3179 || (base != base2
3180 && (TREE_CODE (base) != MEM_REF
3181 || TREE_CODE (base2) != MEM_REF
3182 || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
3183 || !tree_int_cst_equal (TREE_OPERAND (base, 1),
3184 TREE_OPERAND (base2, 1))))
3185 || !stmt_kills_ref_p (def_stmt, ref))
3186 return (void *)-1;
3188 /* Find the common base of ref and the lhs. lhs_ops already
3189 contains valueized operands for the lhs. */
3190 i = vr->operands.length () - 1;
3191 j = lhs_ops.length () - 1;
3192 while (j >= 0 && i >= 0
3193 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
3195 i--;
3196 j--;
3199 /* ??? The innermost op should always be a MEM_REF and we already
3200 checked that the assignment to the lhs kills vr. Thus for
3201 aggregate copies using char[] types the vn_reference_op_eq
3202 may fail when comparing types for compatibility. But we really
3203 don't care here - further lookups with the rewritten operands
3204 will simply fail if we messed up types too badly. */
3205 poly_int64 extra_off = 0;
3206 if (j == 0 && i >= 0
3207 && lhs_ops[0].opcode == MEM_REF
3208 && maybe_ne (lhs_ops[0].off, -1))
3210 if (known_eq (lhs_ops[0].off, vr->operands[i].off))
3211 i--, j--;
3212 else if (vr->operands[i].opcode == MEM_REF
3213 && maybe_ne (vr->operands[i].off, -1))
3215 extra_off = vr->operands[i].off - lhs_ops[0].off;
3216 i--, j--;
3220 /* i now points to the first additional op.
3221 ??? LHS may not be completely contained in VR, one or more
3222 VIEW_CONVERT_EXPRs could be in its way. We could at least
3223 try handling outermost VIEW_CONVERT_EXPRs. */
3224 if (j != -1)
3225 return (void *)-1;
3227 /* Punt if the additional ops contain a storage order barrier. */
3228 for (k = i; k >= 0; k--)
3230 vro = &vr->operands[k];
3231 if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
3232 return (void *)-1;
3235 /* Now re-write REF to be based on the rhs of the assignment. */
3236 tree rhs1 = gimple_assign_rhs1 (def_stmt);
3237 copy_reference_ops_from_ref (rhs1, &rhs);
3239 /* Apply an extra offset to the inner MEM_REF of the RHS. */
3240 if (maybe_ne (extra_off, 0))
3242 if (rhs.length () < 2)
3243 return (void *)-1;
3244 int ix = rhs.length () - 2;
3245 if (rhs[ix].opcode != MEM_REF
3246 || known_eq (rhs[ix].off, -1))
3247 return (void *)-1;
3248 rhs[ix].off += extra_off;
3249 rhs[ix].op0 = int_const_binop (PLUS_EXPR, rhs[ix].op0,
3250 build_int_cst (TREE_TYPE (rhs[ix].op0),
3251 extra_off));
3254 /* Save the operands since we need to use the original ones for
3255 the hash entry we use. */
3256 if (!data->saved_operands.exists ())
3257 data->saved_operands = vr->operands.copy ();
3259 /* We need to pre-pend vr->operands[0..i] to rhs. */
3260 vec<vn_reference_op_s> old = vr->operands;
3261 if (i + 1 + rhs.length () > vr->operands.length ())
3262 vr->operands.safe_grow (i + 1 + rhs.length (), true);
3263 else
3264 vr->operands.truncate (i + 1 + rhs.length ());
3265 FOR_EACH_VEC_ELT (rhs, j, vro)
3266 vr->operands[i + 1 + j] = *vro;
3267 valueize_refs (&vr->operands);
3268 if (old == shared_lookup_references)
3269 shared_lookup_references = vr->operands;
3270 vr->hashcode = vn_reference_compute_hash (vr);
3272 /* Try folding the new reference to a constant. */
3273 tree val = fully_constant_vn_reference_p (vr);
3274 if (val)
3276 if (data->partial_defs.is_empty ())
3277 return data->finish (ao_ref_alias_set (&lhs_ref),
3278 ao_ref_base_alias_set (&lhs_ref), val);
3279 /* This is the only interesting case for partial-def handling
3280 coming from targets that like to gimplify init-ctors as
3281 aggregate copies from constant data like aarch64 for
3282 PR83518. */
3283 if (maxsize.is_constant (&maxsizei) && known_eq (ref->size, maxsize))
3285 pd_data pd;
3286 pd.rhs = val;
3287 pd.offset = 0;
3288 pd.size = maxsizei;
3289 return data->push_partial_def (pd, ao_ref_alias_set (&lhs_ref),
3290 ao_ref_base_alias_set (&lhs_ref),
3291 0, maxsizei);
3295 /* Continuing with partial defs isn't easily possible here, we
3296 have to find a full def from further lookups from here. Probably
3297 not worth the special-casing everywhere. */
3298 if (!data->partial_defs.is_empty ())
3299 return (void *)-1;
3301 /* Adjust *ref from the new operands. */
3302 ao_ref rhs1_ref;
3303 ao_ref_init (&rhs1_ref, rhs1);
3304 if (!ao_ref_init_from_vn_reference (&r, ao_ref_alias_set (&rhs1_ref),
3305 ao_ref_base_alias_set (&rhs1_ref),
3306 vr->type, vr->operands))
3307 return (void *)-1;
3308 /* This can happen with bitfields. */
3309 if (maybe_ne (ref->size, r.size))
3311 /* If the access lacks some subsetting simply apply that by
3312 shortening it. That in the end can only be successful
3313 if we can pun the lookup result which in turn requires
3314 exact offsets. */
3315 if (known_eq (r.size, r.max_size)
3316 && known_lt (ref->size, r.size))
3317 r.size = r.max_size = ref->size;
3318 else
3319 return (void *)-1;
3321 *ref = r;
3323 /* Do not update last seen VUSE after translating. */
3324 data->last_vuse_ptr = NULL;
3325 /* Invalidate the original access path since it now contains
3326 the wrong base. */
3327 data->orig_ref.ref = NULL_TREE;
3328 /* Use the alias-set of this LHS for recording an eventual result. */
3329 if (data->first_set == -2)
3331 data->first_set = ao_ref_alias_set (&lhs_ref);
3332 data->first_base_set = ao_ref_base_alias_set (&lhs_ref);
3335 /* Keep looking for the adjusted *REF / VR pair. */
3336 return NULL;
3339 /* 6) For memcpy copies translate the reference through them if the copy
3340 kills ref. But we cannot (easily) do this translation if the memcpy is
3341 a storage order barrier, i.e. is equivalent to a VIEW_CONVERT_EXPR that
3342 can modify the storage order of objects (see storage_order_barrier_p). */
3343 else if (data->vn_walk_kind == VN_WALKREWRITE
3344 && is_gimple_reg_type (vr->type)
3345 /* ??? Handle BCOPY as well. */
3346 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
3347 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY_CHK)
3348 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
3349 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY_CHK)
3350 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)
3351 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE_CHK))
3352 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
3353 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
3354 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
3355 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
3356 && (poly_int_tree_p (gimple_call_arg (def_stmt, 2), &copy_size)
3357 || (TREE_CODE (gimple_call_arg (def_stmt, 2)) == SSA_NAME
3358 && poly_int_tree_p (SSA_VAL (gimple_call_arg (def_stmt, 2)),
3359 &copy_size)))
3360 /* Handling this is more complicated, give up for now. */
3361 && data->partial_defs.is_empty ())
3363 tree lhs, rhs;
3364 ao_ref r;
3365 poly_int64 rhs_offset, lhs_offset;
3366 vn_reference_op_s op;
3367 poly_uint64 mem_offset;
3368 poly_int64 at, byte_maxsize;
3370 /* Only handle non-variable, addressable refs. */
3371 if (maybe_ne (ref->size, maxsize)
3372 || !multiple_p (offset, BITS_PER_UNIT, &at)
3373 || !multiple_p (maxsize, BITS_PER_UNIT, &byte_maxsize))
3374 return (void *)-1;
3376 /* Extract a pointer base and an offset for the destination. */
3377 lhs = gimple_call_arg (def_stmt, 0);
3378 lhs_offset = 0;
3379 if (TREE_CODE (lhs) == SSA_NAME)
3381 lhs = vn_valueize (lhs);
3382 if (TREE_CODE (lhs) == SSA_NAME)
3384 gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
3385 if (gimple_assign_single_p (def_stmt)
3386 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
3387 lhs = gimple_assign_rhs1 (def_stmt);
3390 if (TREE_CODE (lhs) == ADDR_EXPR)
3392 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (lhs)))
3393 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (lhs))))
3394 return (void *)-1;
3395 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
3396 &lhs_offset);
3397 if (!tem)
3398 return (void *)-1;
3399 if (TREE_CODE (tem) == MEM_REF
3400 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3402 lhs = TREE_OPERAND (tem, 0);
3403 if (TREE_CODE (lhs) == SSA_NAME)
3404 lhs = vn_valueize (lhs);
3405 lhs_offset += mem_offset;
3407 else if (DECL_P (tem))
3408 lhs = build_fold_addr_expr (tem);
3409 else
3410 return (void *)-1;
3412 if (TREE_CODE (lhs) != SSA_NAME
3413 && TREE_CODE (lhs) != ADDR_EXPR)
3414 return (void *)-1;
3416 /* Extract a pointer base and an offset for the source. */
3417 rhs = gimple_call_arg (def_stmt, 1);
3418 rhs_offset = 0;
3419 if (TREE_CODE (rhs) == SSA_NAME)
3420 rhs = vn_valueize (rhs);
3421 if (TREE_CODE (rhs) == ADDR_EXPR)
3423 if (AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (rhs)))
3424 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_TYPE (rhs))))
3425 return (void *)-1;
3426 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
3427 &rhs_offset);
3428 if (!tem)
3429 return (void *)-1;
3430 if (TREE_CODE (tem) == MEM_REF
3431 && poly_int_tree_p (TREE_OPERAND (tem, 1), &mem_offset))
3433 rhs = TREE_OPERAND (tem, 0);
3434 rhs_offset += mem_offset;
3436 else if (DECL_P (tem)
3437 || TREE_CODE (tem) == STRING_CST)
3438 rhs = build_fold_addr_expr (tem);
3439 else
3440 return (void *)-1;
3442 if (TREE_CODE (rhs) == SSA_NAME)
3443 rhs = SSA_VAL (rhs);
3444 else if (TREE_CODE (rhs) != ADDR_EXPR)
3445 return (void *)-1;
3447 /* The bases of the destination and the references have to agree. */
3448 if (TREE_CODE (base) == MEM_REF)
3450 if (TREE_OPERAND (base, 0) != lhs
3451 || !poly_int_tree_p (TREE_OPERAND (base, 1), &mem_offset))
3452 return (void *) -1;
3453 at += mem_offset;
3455 else if (!DECL_P (base)
3456 || TREE_CODE (lhs) != ADDR_EXPR
3457 || TREE_OPERAND (lhs, 0) != base)
3458 return (void *)-1;
3460 /* If the access is completely outside of the memcpy destination
3461 area there is no aliasing. */
3462 if (!ranges_maybe_overlap_p (lhs_offset, copy_size, at, byte_maxsize))
3463 return NULL;
3464 /* And the access has to be contained within the memcpy destination. */
3465 if (!known_subrange_p (at, byte_maxsize, lhs_offset, copy_size))
3466 return (void *)-1;
3468 /* Save the operands since we need to use the original ones for
3469 the hash entry we use. */
3470 if (!data->saved_operands.exists ())
3471 data->saved_operands = vr->operands.copy ();
3473 /* Make room for 2 operands in the new reference. */
3474 if (vr->operands.length () < 2)
3476 vec<vn_reference_op_s> old = vr->operands;
3477 vr->operands.safe_grow_cleared (2, true);
3478 if (old == shared_lookup_references)
3479 shared_lookup_references = vr->operands;
3481 else
3482 vr->operands.truncate (2);
3484 /* The looked-through reference is a simple MEM_REF. */
3485 memset (&op, 0, sizeof (op));
3486 op.type = vr->type;
3487 op.opcode = MEM_REF;
3488 op.op0 = build_int_cst (ptr_type_node, at - lhs_offset + rhs_offset);
3489 op.off = at - lhs_offset + rhs_offset;
3490 vr->operands[0] = op;
3491 op.type = TREE_TYPE (rhs);
3492 op.opcode = TREE_CODE (rhs);
3493 op.op0 = rhs;
3494 op.off = -1;
3495 vr->operands[1] = op;
3496 vr->hashcode = vn_reference_compute_hash (vr);
3498 /* Try folding the new reference to a constant. */
3499 tree val = fully_constant_vn_reference_p (vr);
3500 if (val)
3501 return data->finish (0, 0, val);
3503 /* Adjust *ref from the new operands. */
3504 if (!ao_ref_init_from_vn_reference (&r, 0, 0, vr->type, vr->operands))
3505 return (void *)-1;
3506 /* This can happen with bitfields. */
3507 if (maybe_ne (ref->size, r.size))
3508 return (void *)-1;
3509 *ref = r;
3511 /* Do not update last seen VUSE after translating. */
3512 data->last_vuse_ptr = NULL;
3513 /* Invalidate the original access path since it now contains
3514 the wrong base. */
3515 data->orig_ref.ref = NULL_TREE;
3516 /* Use the alias-set of this stmt for recording an eventual result. */
3517 if (data->first_set == -2)
3519 data->first_set = 0;
3520 data->first_base_set = 0;
3523 /* Keep looking for the adjusted *REF / VR pair. */
3524 return NULL;
3527 /* Bail out and stop walking. */
3528 return (void *)-1;
3531 /* Return a reference op vector from OP that can be used for
3532 vn_reference_lookup_pieces. The caller is responsible for releasing
3533 the vector. */
3535 vec<vn_reference_op_s>
3536 vn_reference_operands_for_lookup (tree op)
3538 bool valueized;
3539 return valueize_shared_reference_ops_from_ref (op, &valueized).copy ();
3542 /* Lookup a reference operation by it's parts, in the current hash table.
3543 Returns the resulting value number if it exists in the hash table,
3544 NULL_TREE otherwise. VNRESULT will be filled in with the actual
3545 vn_reference_t stored in the hashtable if something is found. */
3547 tree
3548 vn_reference_lookup_pieces (tree vuse, alias_set_type set,
3549 alias_set_type base_set, tree type,
3550 vec<vn_reference_op_s> operands,
3551 vn_reference_t *vnresult, vn_lookup_kind kind)
3553 struct vn_reference_s vr1;
3554 vn_reference_t tmp;
3555 tree cst;
3557 if (!vnresult)
3558 vnresult = &tmp;
3559 *vnresult = NULL;
3561 vr1.vuse = vuse_ssa_val (vuse);
3562 shared_lookup_references.truncate (0);
3563 shared_lookup_references.safe_grow (operands.length (), true);
3564 memcpy (shared_lookup_references.address (),
3565 operands.address (),
3566 sizeof (vn_reference_op_s)
3567 * operands.length ());
3568 bool valueized_p;
3569 valueize_refs_1 (&shared_lookup_references, &valueized_p);
3570 vr1.operands = shared_lookup_references;
3571 vr1.type = type;
3572 vr1.set = set;
3573 vr1.base_set = base_set;
3574 vr1.hashcode = vn_reference_compute_hash (&vr1);
3575 if ((cst = fully_constant_vn_reference_p (&vr1)))
3576 return cst;
3578 vn_reference_lookup_1 (&vr1, vnresult);
3579 if (!*vnresult
3580 && kind != VN_NOWALK
3581 && vr1.vuse)
3583 ao_ref r;
3584 unsigned limit = param_sccvn_max_alias_queries_per_access;
3585 vn_walk_cb_data data (&vr1, NULL_TREE, NULL, kind, true, NULL_TREE);
3586 vec<vn_reference_op_s> ops_for_ref;
3587 if (!valueized_p)
3588 ops_for_ref = vr1.operands;
3589 else
3591 /* For ao_ref_from_mem we have to ensure only available SSA names
3592 end up in base and the only convenient way to make this work
3593 for PRE is to re-valueize with that in mind. */
3594 ops_for_ref.create (operands.length ());
3595 ops_for_ref.quick_grow (operands.length ());
3596 memcpy (ops_for_ref.address (),
3597 operands.address (),
3598 sizeof (vn_reference_op_s)
3599 * operands.length ());
3600 valueize_refs_1 (&ops_for_ref, &valueized_p, true);
3602 if (ao_ref_init_from_vn_reference (&r, set, base_set, type,
3603 ops_for_ref))
3604 *vnresult
3605 = ((vn_reference_t)
3606 walk_non_aliased_vuses (&r, vr1.vuse, true, vn_reference_lookup_2,
3607 vn_reference_lookup_3, vuse_valueize,
3608 limit, &data));
3609 if (ops_for_ref != shared_lookup_references)
3610 ops_for_ref.release ();
3611 gcc_checking_assert (vr1.operands == shared_lookup_references);
3614 if (*vnresult)
3615 return (*vnresult)->result;
3617 return NULL_TREE;
3620 /* Lookup OP in the current hash table, and return the resulting value
3621 number if it exists in the hash table. Return NULL_TREE if it does
3622 not exist in the hash table or if the result field of the structure
3623 was NULL.. VNRESULT will be filled in with the vn_reference_t
3624 stored in the hashtable if one exists. When TBAA_P is false assume
3625 we are looking up a store and treat it as having alias-set zero.
3626 *LAST_VUSE_PTR will be updated with the VUSE the value lookup succeeded.
3627 MASK is either NULL_TREE, or can be an INTEGER_CST if the result of the
3628 load is bitwise anded with MASK and so we are only interested in a subset
3629 of the bits and can ignore if the other bits are uninitialized or
3630 not initialized with constants. */
3632 tree
3633 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
3634 vn_reference_t *vnresult, bool tbaa_p,
3635 tree *last_vuse_ptr, tree mask)
3637 vec<vn_reference_op_s> operands;
3638 struct vn_reference_s vr1;
3639 bool valueized_anything;
3641 if (vnresult)
3642 *vnresult = NULL;
3644 vr1.vuse = vuse_ssa_val (vuse);
3645 vr1.operands = operands
3646 = valueize_shared_reference_ops_from_ref (op, &valueized_anything);
3647 vr1.type = TREE_TYPE (op);
3648 ao_ref op_ref;
3649 ao_ref_init (&op_ref, op);
3650 vr1.set = ao_ref_alias_set (&op_ref);
3651 vr1.base_set = ao_ref_base_alias_set (&op_ref);
3652 vr1.hashcode = vn_reference_compute_hash (&vr1);
3653 if (mask == NULL_TREE)
3654 if (tree cst = fully_constant_vn_reference_p (&vr1))
3655 return cst;
3657 if (kind != VN_NOWALK && vr1.vuse)
3659 vn_reference_t wvnresult;
3660 ao_ref r;
3661 unsigned limit = param_sccvn_max_alias_queries_per_access;
3662 auto_vec<vn_reference_op_s> ops_for_ref;
3663 if (valueized_anything)
3665 copy_reference_ops_from_ref (op, &ops_for_ref);
3666 bool tem;
3667 valueize_refs_1 (&ops_for_ref, &tem, true);
3669 /* Make sure to use a valueized reference if we valueized anything.
3670 Otherwise preserve the full reference for advanced TBAA. */
3671 if (!valueized_anything
3672 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.base_set,
3673 vr1.type, ops_for_ref))
3674 ao_ref_init (&r, op);
3675 vn_walk_cb_data data (&vr1, r.ref ? NULL_TREE : op,
3676 last_vuse_ptr, kind, tbaa_p, mask);
3678 wvnresult
3679 = ((vn_reference_t)
3680 walk_non_aliased_vuses (&r, vr1.vuse, tbaa_p, vn_reference_lookup_2,
3681 vn_reference_lookup_3, vuse_valueize, limit,
3682 &data));
3683 gcc_checking_assert (vr1.operands == shared_lookup_references);
3684 if (wvnresult)
3686 gcc_assert (mask == NULL_TREE);
3687 if (vnresult)
3688 *vnresult = wvnresult;
3689 return wvnresult->result;
3691 else if (mask)
3692 return data.masked_result;
3694 return NULL_TREE;
3697 if (last_vuse_ptr)
3698 *last_vuse_ptr = vr1.vuse;
3699 if (mask)
3700 return NULL_TREE;
3701 return vn_reference_lookup_1 (&vr1, vnresult);
3704 /* Lookup CALL in the current hash table and return the entry in
3705 *VNRESULT if found. Populates *VR for the hashtable lookup. */
3707 void
3708 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
3709 vn_reference_t vr)
3711 if (vnresult)
3712 *vnresult = NULL;
3714 tree vuse = gimple_vuse (call);
3716 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
3717 vr->operands = valueize_shared_reference_ops_from_call (call);
3718 tree lhs = gimple_call_lhs (call);
3719 /* For non-SSA return values the referece ops contain the LHS. */
3720 vr->type = ((lhs && TREE_CODE (lhs) == SSA_NAME)
3721 ? TREE_TYPE (lhs) : NULL_TREE);
3722 vr->punned = false;
3723 vr->set = 0;
3724 vr->base_set = 0;
3725 vr->hashcode = vn_reference_compute_hash (vr);
3726 vn_reference_lookup_1 (vr, vnresult);
3729 /* Insert OP into the current hash table with a value number of RESULT. */
3731 static void
3732 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
3734 vn_reference_s **slot;
3735 vn_reference_t vr1;
3736 bool tem;
3738 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
3739 if (TREE_CODE (result) == SSA_NAME)
3740 vr1->value_id = VN_INFO (result)->value_id;
3741 else
3742 vr1->value_id = get_or_alloc_constant_value_id (result);
3743 vr1->vuse = vuse_ssa_val (vuse);
3744 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
3745 vr1->type = TREE_TYPE (op);
3746 vr1->punned = false;
3747 ao_ref op_ref;
3748 ao_ref_init (&op_ref, op);
3749 vr1->set = ao_ref_alias_set (&op_ref);
3750 vr1->base_set = ao_ref_base_alias_set (&op_ref);
3751 vr1->hashcode = vn_reference_compute_hash (vr1);
3752 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
3753 vr1->result_vdef = vdef;
3755 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
3756 INSERT);
3758 /* Because IL walking on reference lookup can end up visiting
3759 a def that is only to be visited later in iteration order
3760 when we are about to make an irreducible region reducible
3761 the def can be effectively processed and its ref being inserted
3762 by vn_reference_lookup_3 already. So we cannot assert (!*slot)
3763 but save a lookup if we deal with already inserted refs here. */
3764 if (*slot)
3766 /* We cannot assert that we have the same value either because
3767 when disentangling an irreducible region we may end up visiting
3768 a use before the corresponding def. That's a missed optimization
3769 only though. See gcc.dg/tree-ssa/pr87126.c for example. */
3770 if (dump_file && (dump_flags & TDF_DETAILS)
3771 && !operand_equal_p ((*slot)->result, vr1->result, 0))
3773 fprintf (dump_file, "Keeping old value ");
3774 print_generic_expr (dump_file, (*slot)->result);
3775 fprintf (dump_file, " because of collision\n");
3777 free_reference (vr1);
3778 obstack_free (&vn_tables_obstack, vr1);
3779 return;
3782 *slot = vr1;
3783 vr1->next = last_inserted_ref;
3784 last_inserted_ref = vr1;
3787 /* Insert a reference by it's pieces into the current hash table with
3788 a value number of RESULT. Return the resulting reference
3789 structure we created. */
3791 vn_reference_t
3792 vn_reference_insert_pieces (tree vuse, alias_set_type set,
3793 alias_set_type base_set, tree type,
3794 vec<vn_reference_op_s> operands,
3795 tree result, unsigned int value_id)
3798 vn_reference_s **slot;
3799 vn_reference_t vr1;
3801 vr1 = XOBNEW (&vn_tables_obstack, vn_reference_s);
3802 vr1->value_id = value_id;
3803 vr1->vuse = vuse_ssa_val (vuse);
3804 vr1->operands = operands;
3805 valueize_refs (&vr1->operands);
3806 vr1->type = type;
3807 vr1->punned = false;
3808 vr1->set = set;
3809 vr1->base_set = base_set;
3810 vr1->hashcode = vn_reference_compute_hash (vr1);
3811 if (result && TREE_CODE (result) == SSA_NAME)
3812 result = SSA_VAL (result);
3813 vr1->result = result;
3814 vr1->result_vdef = NULL_TREE;
3816 slot = valid_info->references->find_slot_with_hash (vr1, vr1->hashcode,
3817 INSERT);
3819 /* At this point we should have all the things inserted that we have
3820 seen before, and we should never try inserting something that
3821 already exists. */
3822 gcc_assert (!*slot);
3824 *slot = vr1;
3825 vr1->next = last_inserted_ref;
3826 last_inserted_ref = vr1;
3827 return vr1;
3830 /* Compute and return the hash value for nary operation VBO1. */
3832 static hashval_t
3833 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
3835 inchash::hash hstate;
3836 unsigned i;
3838 for (i = 0; i < vno1->length; ++i)
3839 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
3840 vno1->op[i] = SSA_VAL (vno1->op[i]);
3842 if (((vno1->length == 2
3843 && commutative_tree_code (vno1->opcode))
3844 || (vno1->length == 3
3845 && commutative_ternary_tree_code (vno1->opcode)))
3846 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
3847 std::swap (vno1->op[0], vno1->op[1]);
3848 else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison
3849 && tree_swap_operands_p (vno1->op[0], vno1->op[1]))
3851 std::swap (vno1->op[0], vno1->op[1]);
3852 vno1->opcode = swap_tree_comparison (vno1->opcode);
3855 hstate.add_int (vno1->opcode);
3856 for (i = 0; i < vno1->length; ++i)
3857 inchash::add_expr (vno1->op[i], hstate);
3859 return hstate.end ();
3862 /* Compare nary operations VNO1 and VNO2 and return true if they are
3863 equivalent. */
3865 bool
3866 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
3868 unsigned i;
3870 if (vno1->hashcode != vno2->hashcode)
3871 return false;
3873 if (vno1->length != vno2->length)
3874 return false;
3876 if (vno1->opcode != vno2->opcode
3877 || !types_compatible_p (vno1->type, vno2->type))
3878 return false;
3880 for (i = 0; i < vno1->length; ++i)
3881 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
3882 return false;
3884 /* BIT_INSERT_EXPR has an implict operand as the type precision
3885 of op1. Need to check to make sure they are the same. */
3886 if (vno1->opcode == BIT_INSERT_EXPR
3887 && TREE_CODE (vno1->op[1]) == INTEGER_CST
3888 && TYPE_PRECISION (TREE_TYPE (vno1->op[1]))
3889 != TYPE_PRECISION (TREE_TYPE (vno2->op[1])))
3890 return false;
3892 return true;
3895 /* Initialize VNO from the pieces provided. */
3897 static void
3898 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
3899 enum tree_code code, tree type, tree *ops)
3901 vno->opcode = code;
3902 vno->length = length;
3903 vno->type = type;
3904 memcpy (&vno->op[0], ops, sizeof (tree) * length);
3907 /* Return the number of operands for a vn_nary ops structure from STMT. */
3909 static unsigned int
3910 vn_nary_length_from_stmt (gimple *stmt)
3912 switch (gimple_assign_rhs_code (stmt))
3914 case REALPART_EXPR:
3915 case IMAGPART_EXPR:
3916 case VIEW_CONVERT_EXPR:
3917 return 1;
3919 case BIT_FIELD_REF:
3920 return 3;
3922 case CONSTRUCTOR:
3923 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
3925 default:
3926 return gimple_num_ops (stmt) - 1;
3930 /* Initialize VNO from STMT. */
3932 static void
3933 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gassign *stmt)
3935 unsigned i;
3937 vno->opcode = gimple_assign_rhs_code (stmt);
3938 vno->type = TREE_TYPE (gimple_assign_lhs (stmt));
3939 switch (vno->opcode)
3941 case REALPART_EXPR:
3942 case IMAGPART_EXPR:
3943 case VIEW_CONVERT_EXPR:
3944 vno->length = 1;
3945 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
3946 break;
3948 case BIT_FIELD_REF:
3949 vno->length = 3;
3950 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
3951 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
3952 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
3953 break;
3955 case CONSTRUCTOR:
3956 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
3957 for (i = 0; i < vno->length; ++i)
3958 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
3959 break;
3961 default:
3962 gcc_checking_assert (!gimple_assign_single_p (stmt));
3963 vno->length = gimple_num_ops (stmt) - 1;
3964 for (i = 0; i < vno->length; ++i)
3965 vno->op[i] = gimple_op (stmt, i + 1);
3969 /* Compute the hashcode for VNO and look for it in the hash table;
3970 return the resulting value number if it exists in the hash table.
3971 Return NULL_TREE if it does not exist in the hash table or if the
3972 result field of the operation is NULL. VNRESULT will contain the
3973 vn_nary_op_t from the hashtable if it exists. */
3975 static tree
3976 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
3978 vn_nary_op_s **slot;
3980 if (vnresult)
3981 *vnresult = NULL;
3983 vno->hashcode = vn_nary_op_compute_hash (vno);
3984 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
3985 if (!slot)
3986 return NULL_TREE;
3987 if (vnresult)
3988 *vnresult = *slot;
3989 return (*slot)->predicated_values ? NULL_TREE : (*slot)->u.result;
3992 /* Lookup a n-ary operation by its pieces and return the resulting value
3993 number if it exists in the hash table. Return NULL_TREE if it does
3994 not exist in the hash table or if the result field of the operation
3995 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
3996 if it exists. */
3998 tree
3999 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
4000 tree type, tree *ops, vn_nary_op_t *vnresult)
4002 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
4003 sizeof_vn_nary_op (length));
4004 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4005 return vn_nary_op_lookup_1 (vno1, vnresult);
4008 /* Lookup the rhs of STMT in the current hash table, and return the resulting
4009 value number if it exists in the hash table. Return NULL_TREE if
4010 it does not exist in the hash table. VNRESULT will contain the
4011 vn_nary_op_t from the hashtable if it exists. */
4013 tree
4014 vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult)
4016 vn_nary_op_t vno1
4017 = XALLOCAVAR (struct vn_nary_op_s,
4018 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
4019 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4020 return vn_nary_op_lookup_1 (vno1, vnresult);
4023 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
4025 static vn_nary_op_t
4026 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
4028 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
4031 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
4032 obstack. */
4034 static vn_nary_op_t
4035 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
4037 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, &vn_tables_obstack);
4039 vno1->value_id = value_id;
4040 vno1->length = length;
4041 vno1->predicated_values = 0;
4042 vno1->u.result = result;
4044 return vno1;
4047 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
4048 VNO->HASHCODE first. */
4050 static vn_nary_op_t
4051 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
4052 bool compute_hash)
4054 vn_nary_op_s **slot;
4056 if (compute_hash)
4058 vno->hashcode = vn_nary_op_compute_hash (vno);
4059 gcc_assert (! vno->predicated_values
4060 || (! vno->u.values->next
4061 && vno->u.values->n == 1));
4064 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
4065 vno->unwind_to = *slot;
4066 if (*slot)
4068 /* Prefer non-predicated values.
4069 ??? Only if those are constant, otherwise, with constant predicated
4070 value, turn them into predicated values with entry-block validity
4071 (??? but we always find the first valid result currently). */
4072 if ((*slot)->predicated_values
4073 && ! vno->predicated_values)
4075 /* ??? We cannot remove *slot from the unwind stack list.
4076 For the moment we deal with this by skipping not found
4077 entries but this isn't ideal ... */
4078 *slot = vno;
4079 /* ??? Maintain a stack of states we can unwind in
4080 vn_nary_op_s? But how far do we unwind? In reality
4081 we need to push change records somewhere... Or not
4082 unwind vn_nary_op_s and linking them but instead
4083 unwind the results "list", linking that, which also
4084 doesn't move on hashtable resize. */
4085 /* We can also have a ->unwind_to recording *slot there.
4086 That way we can make u.values a fixed size array with
4087 recording the number of entries but of course we then
4088 have always N copies for each unwind_to-state. Or we
4089 make sure to only ever append and each unwinding will
4090 pop off one entry (but how to deal with predicated
4091 replaced with non-predicated here?) */
4092 vno->next = last_inserted_nary;
4093 last_inserted_nary = vno;
4094 return vno;
4096 else if (vno->predicated_values
4097 && ! (*slot)->predicated_values)
4098 return *slot;
4099 else if (vno->predicated_values
4100 && (*slot)->predicated_values)
4102 /* ??? Factor this all into a insert_single_predicated_value
4103 routine. */
4104 gcc_assert (!vno->u.values->next && vno->u.values->n == 1);
4105 basic_block vno_bb
4106 = BASIC_BLOCK_FOR_FN (cfun, vno->u.values->valid_dominated_by_p[0]);
4107 vn_pval *nval = vno->u.values;
4108 vn_pval **next = &vno->u.values;
4109 bool found = false;
4110 for (vn_pval *val = (*slot)->u.values; val; val = val->next)
4112 if (expressions_equal_p (val->result, nval->result))
4114 found = true;
4115 for (unsigned i = 0; i < val->n; ++i)
4117 basic_block val_bb
4118 = BASIC_BLOCK_FOR_FN (cfun,
4119 val->valid_dominated_by_p[i]);
4120 if (dominated_by_p (CDI_DOMINATORS, vno_bb, val_bb))
4121 /* Value registered with more generic predicate. */
4122 return *slot;
4123 else if (dominated_by_p (CDI_DOMINATORS, val_bb, vno_bb))
4124 /* Shouldn't happen, we insert in RPO order. */
4125 gcc_unreachable ();
4127 /* Append value. */
4128 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4129 sizeof (vn_pval)
4130 + val->n * sizeof (int));
4131 (*next)->next = NULL;
4132 (*next)->result = val->result;
4133 (*next)->n = val->n + 1;
4134 memcpy ((*next)->valid_dominated_by_p,
4135 val->valid_dominated_by_p,
4136 val->n * sizeof (int));
4137 (*next)->valid_dominated_by_p[val->n] = vno_bb->index;
4138 next = &(*next)->next;
4139 if (dump_file && (dump_flags & TDF_DETAILS))
4140 fprintf (dump_file, "Appending predicate to value.\n");
4141 continue;
4143 /* Copy other predicated values. */
4144 *next = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4145 sizeof (vn_pval)
4146 + (val->n-1) * sizeof (int));
4147 memcpy (*next, val, sizeof (vn_pval) + (val->n-1) * sizeof (int));
4148 (*next)->next = NULL;
4149 next = &(*next)->next;
4151 if (!found)
4152 *next = nval;
4154 *slot = vno;
4155 vno->next = last_inserted_nary;
4156 last_inserted_nary = vno;
4157 return vno;
4160 /* While we do not want to insert things twice it's awkward to
4161 avoid it in the case where visit_nary_op pattern-matches stuff
4162 and ends up simplifying the replacement to itself. We then
4163 get two inserts, one from visit_nary_op and one from
4164 vn_nary_build_or_lookup.
4165 So allow inserts with the same value number. */
4166 if ((*slot)->u.result == vno->u.result)
4167 return *slot;
4170 /* ??? There's also optimistic vs. previous commited state merging
4171 that is problematic for the case of unwinding. */
4173 /* ??? We should return NULL if we do not use 'vno' and have the
4174 caller release it. */
4175 gcc_assert (!*slot);
4177 *slot = vno;
4178 vno->next = last_inserted_nary;
4179 last_inserted_nary = vno;
4180 return vno;
4183 /* Insert a n-ary operation into the current hash table using it's
4184 pieces. Return the vn_nary_op_t structure we created and put in
4185 the hashtable. */
4187 vn_nary_op_t
4188 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
4189 tree type, tree *ops,
4190 tree result, unsigned int value_id)
4192 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
4193 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4194 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
4197 static vn_nary_op_t
4198 vn_nary_op_insert_pieces_predicated (unsigned int length, enum tree_code code,
4199 tree type, tree *ops,
4200 tree result, unsigned int value_id,
4201 edge pred_e)
4203 /* ??? Currently tracking BBs. */
4204 if (! single_pred_p (pred_e->dest))
4206 /* Never record for backedges. */
4207 if (pred_e->flags & EDGE_DFS_BACK)
4208 return NULL;
4209 edge_iterator ei;
4210 edge e;
4211 int cnt = 0;
4212 /* Ignore backedges. */
4213 FOR_EACH_EDGE (e, ei, pred_e->dest->preds)
4214 if (! dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
4215 cnt++;
4216 if (cnt != 1)
4217 return NULL;
4219 if (dump_file && (dump_flags & TDF_DETAILS)
4220 /* ??? Fix dumping, but currently we only get comparisons. */
4221 && TREE_CODE_CLASS (code) == tcc_comparison)
4223 fprintf (dump_file, "Recording on edge %d->%d ", pred_e->src->index,
4224 pred_e->dest->index);
4225 print_generic_expr (dump_file, ops[0], TDF_SLIM);
4226 fprintf (dump_file, " %s ", get_tree_code_name (code));
4227 print_generic_expr (dump_file, ops[1], TDF_SLIM);
4228 fprintf (dump_file, " == %s\n",
4229 integer_zerop (result) ? "false" : "true");
4231 vn_nary_op_t vno1 = alloc_vn_nary_op (length, NULL_TREE, value_id);
4232 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
4233 vno1->predicated_values = 1;
4234 vno1->u.values = (vn_pval *) obstack_alloc (&vn_tables_obstack,
4235 sizeof (vn_pval));
4236 vno1->u.values->next = NULL;
4237 vno1->u.values->result = result;
4238 vno1->u.values->n = 1;
4239 vno1->u.values->valid_dominated_by_p[0] = pred_e->dest->index;
4240 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
4243 static bool
4244 dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool);
4246 static tree
4247 vn_nary_op_get_predicated_value (vn_nary_op_t vno, basic_block bb)
4249 if (! vno->predicated_values)
4250 return vno->u.result;
4251 for (vn_pval *val = vno->u.values; val; val = val->next)
4252 for (unsigned i = 0; i < val->n; ++i)
4253 /* Do not handle backedge executability optimistically since
4254 when figuring out whether to iterate we do not consider
4255 changed predication. */
4256 if (dominated_by_p_w_unex
4257 (bb, BASIC_BLOCK_FOR_FN (cfun, val->valid_dominated_by_p[i]),
4258 false))
4259 return val->result;
4260 return NULL_TREE;
4263 /* Insert the rhs of STMT into the current hash table with a value number of
4264 RESULT. */
4266 static vn_nary_op_t
4267 vn_nary_op_insert_stmt (gimple *stmt, tree result)
4269 vn_nary_op_t vno1
4270 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
4271 result, VN_INFO (result)->value_id);
4272 init_vn_nary_op_from_stmt (vno1, as_a <gassign *> (stmt));
4273 return vn_nary_op_insert_into (vno1, valid_info->nary, true);
4276 /* Compute a hashcode for PHI operation VP1 and return it. */
4278 static inline hashval_t
4279 vn_phi_compute_hash (vn_phi_t vp1)
4281 inchash::hash hstate;
4282 tree phi1op;
4283 tree type;
4284 edge e;
4285 edge_iterator ei;
4287 hstate.add_int (EDGE_COUNT (vp1->block->preds));
4288 switch (EDGE_COUNT (vp1->block->preds))
4290 case 1:
4291 break;
4292 case 2:
4293 if (vp1->block->loop_father->header == vp1->block)
4295 else
4296 break;
4297 /* Fallthru. */
4298 default:
4299 hstate.add_int (vp1->block->index);
4302 /* If all PHI arguments are constants we need to distinguish
4303 the PHI node via its type. */
4304 type = vp1->type;
4305 hstate.merge_hash (vn_hash_type (type));
4307 FOR_EACH_EDGE (e, ei, vp1->block->preds)
4309 /* Don't hash backedge values they need to be handled as VN_TOP
4310 for optimistic value-numbering. */
4311 if (e->flags & EDGE_DFS_BACK)
4312 continue;
4314 phi1op = vp1->phiargs[e->dest_idx];
4315 if (phi1op == VN_TOP)
4316 continue;
4317 inchash::add_expr (phi1op, hstate);
4320 return hstate.end ();
4324 /* Return true if COND1 and COND2 represent the same condition, set
4325 *INVERTED_P if one needs to be inverted to make it the same as
4326 the other. */
4328 static bool
4329 cond_stmts_equal_p (gcond *cond1, tree lhs1, tree rhs1,
4330 gcond *cond2, tree lhs2, tree rhs2, bool *inverted_p)
4332 enum tree_code code1 = gimple_cond_code (cond1);
4333 enum tree_code code2 = gimple_cond_code (cond2);
4335 *inverted_p = false;
4336 if (code1 == code2)
4338 else if (code1 == swap_tree_comparison (code2))
4339 std::swap (lhs2, rhs2);
4340 else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2)))
4341 *inverted_p = true;
4342 else if (code1 == invert_tree_comparison
4343 (swap_tree_comparison (code2), HONOR_NANS (lhs2)))
4345 std::swap (lhs2, rhs2);
4346 *inverted_p = true;
4348 else
4349 return false;
4351 return ((expressions_equal_p (lhs1, lhs2)
4352 && expressions_equal_p (rhs1, rhs2))
4353 || (commutative_tree_code (code1)
4354 && expressions_equal_p (lhs1, rhs2)
4355 && expressions_equal_p (rhs1, lhs2)));
4358 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
4360 static int
4361 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
4363 if (vp1->hashcode != vp2->hashcode)
4364 return false;
4366 if (vp1->block != vp2->block)
4368 if (EDGE_COUNT (vp1->block->preds) != EDGE_COUNT (vp2->block->preds))
4369 return false;
4371 switch (EDGE_COUNT (vp1->block->preds))
4373 case 1:
4374 /* Single-arg PHIs are just copies. */
4375 break;
4377 case 2:
4379 /* Rule out backedges into the PHI. */
4380 if (vp1->block->loop_father->header == vp1->block
4381 || vp2->block->loop_father->header == vp2->block)
4382 return false;
4384 /* If the PHI nodes do not have compatible types
4385 they are not the same. */
4386 if (!types_compatible_p (vp1->type, vp2->type))
4387 return false;
4389 basic_block idom1
4390 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4391 basic_block idom2
4392 = get_immediate_dominator (CDI_DOMINATORS, vp2->block);
4393 /* If the immediate dominator end in switch stmts multiple
4394 values may end up in the same PHI arg via intermediate
4395 CFG merges. */
4396 if (EDGE_COUNT (idom1->succs) != 2
4397 || EDGE_COUNT (idom2->succs) != 2)
4398 return false;
4400 /* Verify the controlling stmt is the same. */
4401 gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1));
4402 gcond *last2 = safe_dyn_cast <gcond *> (last_stmt (idom2));
4403 if (! last1 || ! last2)
4404 return false;
4405 bool inverted_p;
4406 if (! cond_stmts_equal_p (last1, vp1->cclhs, vp1->ccrhs,
4407 last2, vp2->cclhs, vp2->ccrhs,
4408 &inverted_p))
4409 return false;
4411 /* Get at true/false controlled edges into the PHI. */
4412 edge te1, te2, fe1, fe2;
4413 if (! extract_true_false_controlled_edges (idom1, vp1->block,
4414 &te1, &fe1)
4415 || ! extract_true_false_controlled_edges (idom2, vp2->block,
4416 &te2, &fe2))
4417 return false;
4419 /* Swap edges if the second condition is the inverted of the
4420 first. */
4421 if (inverted_p)
4422 std::swap (te2, fe2);
4424 /* ??? Handle VN_TOP specially. */
4425 if (! expressions_equal_p (vp1->phiargs[te1->dest_idx],
4426 vp2->phiargs[te2->dest_idx])
4427 || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx],
4428 vp2->phiargs[fe2->dest_idx]))
4429 return false;
4431 return true;
4434 default:
4435 return false;
4439 /* If the PHI nodes do not have compatible types
4440 they are not the same. */
4441 if (!types_compatible_p (vp1->type, vp2->type))
4442 return false;
4444 /* Any phi in the same block will have it's arguments in the
4445 same edge order, because of how we store phi nodes. */
4446 unsigned nargs = EDGE_COUNT (vp1->block->preds);
4447 for (unsigned i = 0; i < nargs; ++i)
4449 tree phi1op = vp1->phiargs[i];
4450 tree phi2op = vp2->phiargs[i];
4451 if (phi1op == phi2op)
4452 continue;
4453 if (!expressions_equal_p (phi1op, phi2op))
4454 return false;
4457 return true;
4460 /* Lookup PHI in the current hash table, and return the resulting
4461 value number if it exists in the hash table. Return NULL_TREE if
4462 it does not exist in the hash table. */
4464 static tree
4465 vn_phi_lookup (gimple *phi, bool backedges_varying_p)
4467 vn_phi_s **slot;
4468 struct vn_phi_s *vp1;
4469 edge e;
4470 edge_iterator ei;
4472 vp1 = XALLOCAVAR (struct vn_phi_s,
4473 sizeof (struct vn_phi_s)
4474 + (gimple_phi_num_args (phi) - 1) * sizeof (tree));
4476 /* Canonicalize the SSA_NAME's to their value number. */
4477 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
4479 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4480 if (TREE_CODE (def) == SSA_NAME
4481 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
4482 def = SSA_VAL (def);
4483 vp1->phiargs[e->dest_idx] = def;
4485 vp1->type = TREE_TYPE (gimple_phi_result (phi));
4486 vp1->block = gimple_bb (phi);
4487 /* Extract values of the controlling condition. */
4488 vp1->cclhs = NULL_TREE;
4489 vp1->ccrhs = NULL_TREE;
4490 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4491 if (EDGE_COUNT (idom1->succs) == 2)
4492 if (gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1)))
4494 /* ??? We want to use SSA_VAL here. But possibly not
4495 allow VN_TOP. */
4496 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
4497 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
4499 vp1->hashcode = vn_phi_compute_hash (vp1);
4500 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, NO_INSERT);
4501 if (!slot)
4502 return NULL_TREE;
4503 return (*slot)->result;
4506 /* Insert PHI into the current hash table with a value number of
4507 RESULT. */
4509 static vn_phi_t
4510 vn_phi_insert (gimple *phi, tree result, bool backedges_varying_p)
4512 vn_phi_s **slot;
4513 vn_phi_t vp1 = (vn_phi_t) obstack_alloc (&vn_tables_obstack,
4514 sizeof (vn_phi_s)
4515 + ((gimple_phi_num_args (phi) - 1)
4516 * sizeof (tree)));
4517 edge e;
4518 edge_iterator ei;
4520 /* Canonicalize the SSA_NAME's to their value number. */
4521 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
4523 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4524 if (TREE_CODE (def) == SSA_NAME
4525 && (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK)))
4526 def = SSA_VAL (def);
4527 vp1->phiargs[e->dest_idx] = def;
4529 vp1->value_id = VN_INFO (result)->value_id;
4530 vp1->type = TREE_TYPE (gimple_phi_result (phi));
4531 vp1->block = gimple_bb (phi);
4532 /* Extract values of the controlling condition. */
4533 vp1->cclhs = NULL_TREE;
4534 vp1->ccrhs = NULL_TREE;
4535 basic_block idom1 = get_immediate_dominator (CDI_DOMINATORS, vp1->block);
4536 if (EDGE_COUNT (idom1->succs) == 2)
4537 if (gcond *last1 = safe_dyn_cast <gcond *> (last_stmt (idom1)))
4539 /* ??? We want to use SSA_VAL here. But possibly not
4540 allow VN_TOP. */
4541 vp1->cclhs = vn_valueize (gimple_cond_lhs (last1));
4542 vp1->ccrhs = vn_valueize (gimple_cond_rhs (last1));
4544 vp1->result = result;
4545 vp1->hashcode = vn_phi_compute_hash (vp1);
4547 slot = valid_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
4548 gcc_assert (!*slot);
4550 *slot = vp1;
4551 vp1->next = last_inserted_phi;
4552 last_inserted_phi = vp1;
4553 return vp1;
4557 /* Return true if BB1 is dominated by BB2 taking into account edges
4558 that are not executable. When ALLOW_BACK is false consider not
4559 executable backedges as executable. */
4561 static bool
4562 dominated_by_p_w_unex (basic_block bb1, basic_block bb2, bool allow_back)
4564 edge_iterator ei;
4565 edge e;
4567 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
4568 return true;
4570 /* Before iterating we'd like to know if there exists a
4571 (executable) path from bb2 to bb1 at all, if not we can
4572 directly return false. For now simply iterate once. */
4574 /* Iterate to the single executable bb1 predecessor. */
4575 if (EDGE_COUNT (bb1->preds) > 1)
4577 edge prede = NULL;
4578 FOR_EACH_EDGE (e, ei, bb1->preds)
4579 if ((e->flags & EDGE_EXECUTABLE)
4580 || (!allow_back && (e->flags & EDGE_DFS_BACK)))
4582 if (prede)
4584 prede = NULL;
4585 break;
4587 prede = e;
4589 if (prede)
4591 bb1 = prede->src;
4593 /* Re-do the dominance check with changed bb1. */
4594 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
4595 return true;
4599 /* Iterate to the single executable bb2 successor. */
4600 edge succe = NULL;
4601 FOR_EACH_EDGE (e, ei, bb2->succs)
4602 if ((e->flags & EDGE_EXECUTABLE)
4603 || (!allow_back && (e->flags & EDGE_DFS_BACK)))
4605 if (succe)
4607 succe = NULL;
4608 break;
4610 succe = e;
4612 if (succe)
4614 /* Verify the reached block is only reached through succe.
4615 If there is only one edge we can spare us the dominator
4616 check and iterate directly. */
4617 if (EDGE_COUNT (succe->dest->preds) > 1)
4619 FOR_EACH_EDGE (e, ei, succe->dest->preds)
4620 if (e != succe
4621 && ((e->flags & EDGE_EXECUTABLE)
4622 || (!allow_back && (e->flags & EDGE_DFS_BACK))))
4624 succe = NULL;
4625 break;
4628 if (succe)
4630 bb2 = succe->dest;
4632 /* Re-do the dominance check with changed bb2. */
4633 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
4634 return true;
4638 /* We could now iterate updating bb1 / bb2. */
4639 return false;
4642 /* Set the value number of FROM to TO, return true if it has changed
4643 as a result. */
4645 static inline bool
4646 set_ssa_val_to (tree from, tree to)
4648 vn_ssa_aux_t from_info = VN_INFO (from);
4649 tree currval = from_info->valnum; // SSA_VAL (from)
4650 poly_int64 toff, coff;
4651 bool curr_undefined = false;
4652 bool curr_invariant = false;
4654 /* The only thing we allow as value numbers are ssa_names
4655 and invariants. So assert that here. We don't allow VN_TOP
4656 as visiting a stmt should produce a value-number other than
4657 that.
4658 ??? Still VN_TOP can happen for unreachable code, so force
4659 it to varying in that case. Not all code is prepared to
4660 get VN_TOP on valueization. */
4661 if (to == VN_TOP)
4663 /* ??? When iterating and visiting PHI <undef, backedge-value>
4664 for the first time we rightfully get VN_TOP and we need to
4665 preserve that to optimize for example gcc.dg/tree-ssa/ssa-sccvn-2.c.
4666 With SCCVN we were simply lucky we iterated the other PHI
4667 cycles first and thus visited the backedge-value DEF. */
4668 if (currval == VN_TOP)
4669 goto set_and_exit;
4670 if (dump_file && (dump_flags & TDF_DETAILS))
4671 fprintf (dump_file, "Forcing value number to varying on "
4672 "receiving VN_TOP\n");
4673 to = from;
4676 gcc_checking_assert (to != NULL_TREE
4677 && ((TREE_CODE (to) == SSA_NAME
4678 && (to == from || SSA_VAL (to) == to))
4679 || is_gimple_min_invariant (to)));
4681 if (from != to)
4683 if (currval == from)
4685 if (dump_file && (dump_flags & TDF_DETAILS))
4687 fprintf (dump_file, "Not changing value number of ");
4688 print_generic_expr (dump_file, from);
4689 fprintf (dump_file, " from VARYING to ");
4690 print_generic_expr (dump_file, to);
4691 fprintf (dump_file, "\n");
4693 return false;
4695 curr_invariant = is_gimple_min_invariant (currval);
4696 curr_undefined = (TREE_CODE (currval) == SSA_NAME
4697 && ssa_undefined_value_p (currval, false));
4698 if (currval != VN_TOP
4699 && !curr_invariant
4700 && !curr_undefined
4701 && is_gimple_min_invariant (to))
4703 if (dump_file && (dump_flags & TDF_DETAILS))
4705 fprintf (dump_file, "Forcing VARYING instead of changing "
4706 "value number of ");
4707 print_generic_expr (dump_file, from);
4708 fprintf (dump_file, " from ");
4709 print_generic_expr (dump_file, currval);
4710 fprintf (dump_file, " (non-constant) to ");
4711 print_generic_expr (dump_file, to);
4712 fprintf (dump_file, " (constant)\n");
4714 to = from;
4716 else if (currval != VN_TOP
4717 && !curr_undefined
4718 && TREE_CODE (to) == SSA_NAME
4719 && ssa_undefined_value_p (to, false))
4721 if (dump_file && (dump_flags & TDF_DETAILS))
4723 fprintf (dump_file, "Forcing VARYING instead of changing "
4724 "value number of ");
4725 print_generic_expr (dump_file, from);
4726 fprintf (dump_file, " from ");
4727 print_generic_expr (dump_file, currval);
4728 fprintf (dump_file, " (non-undefined) to ");
4729 print_generic_expr (dump_file, to);
4730 fprintf (dump_file, " (undefined)\n");
4732 to = from;
4734 else if (TREE_CODE (to) == SSA_NAME
4735 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
4736 to = from;
4739 set_and_exit:
4740 if (dump_file && (dump_flags & TDF_DETAILS))
4742 fprintf (dump_file, "Setting value number of ");
4743 print_generic_expr (dump_file, from);
4744 fprintf (dump_file, " to ");
4745 print_generic_expr (dump_file, to);
4748 if (currval != to
4749 && !operand_equal_p (currval, to, 0)
4750 /* Different undefined SSA names are not actually different. See
4751 PR82320 for a testcase were we'd otherwise not terminate iteration. */
4752 && !(curr_undefined
4753 && TREE_CODE (to) == SSA_NAME
4754 && ssa_undefined_value_p (to, false))
4755 /* ??? For addresses involving volatile objects or types operand_equal_p
4756 does not reliably detect ADDR_EXPRs as equal. We know we are only
4757 getting invariant gimple addresses here, so can use
4758 get_addr_base_and_unit_offset to do this comparison. */
4759 && !(TREE_CODE (currval) == ADDR_EXPR
4760 && TREE_CODE (to) == ADDR_EXPR
4761 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
4762 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
4763 && known_eq (coff, toff)))
4765 if (to != from
4766 && currval != VN_TOP
4767 && !curr_undefined
4768 /* We do not want to allow lattice transitions from one value
4769 to another since that may lead to not terminating iteration
4770 (see PR95049). Since there's no convenient way to check
4771 for the allowed transition of VAL -> PHI (loop entry value,
4772 same on two PHIs, to same PHI result) we restrict the check
4773 to invariants. */
4774 && curr_invariant
4775 && is_gimple_min_invariant (to))
4777 if (dump_file && (dump_flags & TDF_DETAILS))
4778 fprintf (dump_file, " forced VARYING");
4779 to = from;
4781 if (dump_file && (dump_flags & TDF_DETAILS))
4782 fprintf (dump_file, " (changed)\n");
4783 from_info->valnum = to;
4784 return true;
4786 if (dump_file && (dump_flags & TDF_DETAILS))
4787 fprintf (dump_file, "\n");
4788 return false;
4791 /* Set all definitions in STMT to value number to themselves.
4792 Return true if a value number changed. */
4794 static bool
4795 defs_to_varying (gimple *stmt)
4797 bool changed = false;
4798 ssa_op_iter iter;
4799 def_operand_p defp;
4801 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
4803 tree def = DEF_FROM_PTR (defp);
4804 changed |= set_ssa_val_to (def, def);
4806 return changed;
4809 /* Visit a copy between LHS and RHS, return true if the value number
4810 changed. */
4812 static bool
4813 visit_copy (tree lhs, tree rhs)
4815 /* Valueize. */
4816 rhs = SSA_VAL (rhs);
4818 return set_ssa_val_to (lhs, rhs);
4821 /* Lookup a value for OP in type WIDE_TYPE where the value in type of OP
4822 is the same. */
4824 static tree
4825 valueized_wider_op (tree wide_type, tree op, bool allow_truncate)
4827 if (TREE_CODE (op) == SSA_NAME)
4828 op = vn_valueize (op);
4830 /* Either we have the op widened available. */
4831 tree ops[3] = {};
4832 ops[0] = op;
4833 tree tem = vn_nary_op_lookup_pieces (1, NOP_EXPR,
4834 wide_type, ops, NULL);
4835 if (tem)
4836 return tem;
4838 /* Or the op is truncated from some existing value. */
4839 if (allow_truncate && TREE_CODE (op) == SSA_NAME)
4841 gimple *def = SSA_NAME_DEF_STMT (op);
4842 if (is_gimple_assign (def)
4843 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def)))
4845 tem = gimple_assign_rhs1 (def);
4846 if (useless_type_conversion_p (wide_type, TREE_TYPE (tem)))
4848 if (TREE_CODE (tem) == SSA_NAME)
4849 tem = vn_valueize (tem);
4850 return tem;
4855 /* For constants simply extend it. */
4856 if (TREE_CODE (op) == INTEGER_CST)
4857 return wide_int_to_tree (wide_type, wi::to_wide (op));
4859 return NULL_TREE;
4862 /* Visit a nary operator RHS, value number it, and return true if the
4863 value number of LHS has changed as a result. */
4865 static bool
4866 visit_nary_op (tree lhs, gassign *stmt)
4868 vn_nary_op_t vnresult;
4869 tree result = vn_nary_op_lookup_stmt (stmt, &vnresult);
4870 if (! result && vnresult)
4871 result = vn_nary_op_get_predicated_value (vnresult, gimple_bb (stmt));
4872 if (result)
4873 return set_ssa_val_to (lhs, result);
4875 /* Do some special pattern matching for redundancies of operations
4876 in different types. */
4877 enum tree_code code = gimple_assign_rhs_code (stmt);
4878 tree type = TREE_TYPE (lhs);
4879 tree rhs1 = gimple_assign_rhs1 (stmt);
4880 switch (code)
4882 CASE_CONVERT:
4883 /* Match arithmetic done in a different type where we can easily
4884 substitute the result from some earlier sign-changed or widened
4885 operation. */
4886 if (INTEGRAL_TYPE_P (type)
4887 && TREE_CODE (rhs1) == SSA_NAME
4888 /* We only handle sign-changes, zero-extension -> & mask or
4889 sign-extension if we know the inner operation doesn't
4890 overflow. */
4891 && (((TYPE_UNSIGNED (TREE_TYPE (rhs1))
4892 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4893 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
4894 && TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (rhs1)))
4895 || TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (rhs1))))
4897 gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
4898 if (def
4899 && (gimple_assign_rhs_code (def) == PLUS_EXPR
4900 || gimple_assign_rhs_code (def) == MINUS_EXPR
4901 || gimple_assign_rhs_code (def) == MULT_EXPR))
4903 tree ops[3] = {};
4904 /* When requiring a sign-extension we cannot model a
4905 previous truncation with a single op so don't bother. */
4906 bool allow_truncate = TYPE_UNSIGNED (TREE_TYPE (rhs1));
4907 /* Either we have the op widened available. */
4908 ops[0] = valueized_wider_op (type, gimple_assign_rhs1 (def),
4909 allow_truncate);
4910 if (ops[0])
4911 ops[1] = valueized_wider_op (type, gimple_assign_rhs2 (def),
4912 allow_truncate);
4913 if (ops[0] && ops[1])
4915 ops[0] = vn_nary_op_lookup_pieces
4916 (2, gimple_assign_rhs_code (def), type, ops, NULL);
4917 /* We have wider operation available. */
4918 if (ops[0]
4919 /* If the leader is a wrapping operation we can
4920 insert it for code hoisting w/o introducing
4921 undefined overflow. If it is not it has to
4922 be available. See PR86554. */
4923 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (ops[0]))
4924 || (rpo_avail && vn_context_bb
4925 && rpo_avail->eliminate_avail (vn_context_bb,
4926 ops[0]))))
4928 unsigned lhs_prec = TYPE_PRECISION (type);
4929 unsigned rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs1));
4930 if (lhs_prec == rhs_prec
4931 || (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
4932 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (rhs1))))
4934 gimple_match_op match_op (gimple_match_cond::UNCOND,
4935 NOP_EXPR, type, ops[0]);
4936 result = vn_nary_build_or_lookup (&match_op);
4937 if (result)
4939 bool changed = set_ssa_val_to (lhs, result);
4940 vn_nary_op_insert_stmt (stmt, result);
4941 return changed;
4944 else
4946 tree mask = wide_int_to_tree
4947 (type, wi::mask (rhs_prec, false, lhs_prec));
4948 gimple_match_op match_op (gimple_match_cond::UNCOND,
4949 BIT_AND_EXPR,
4950 TREE_TYPE (lhs),
4951 ops[0], mask);
4952 result = vn_nary_build_or_lookup (&match_op);
4953 if (result)
4955 bool changed = set_ssa_val_to (lhs, result);
4956 vn_nary_op_insert_stmt (stmt, result);
4957 return changed;
4964 break;
4965 case BIT_AND_EXPR:
4966 if (INTEGRAL_TYPE_P (type)
4967 && TREE_CODE (rhs1) == SSA_NAME
4968 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST
4969 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)
4970 && default_vn_walk_kind != VN_NOWALK
4971 && CHAR_BIT == 8
4972 && BITS_PER_UNIT == 8
4973 && BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
4974 && !integer_all_onesp (gimple_assign_rhs2 (stmt))
4975 && !integer_zerop (gimple_assign_rhs2 (stmt)))
4977 gassign *ass = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (rhs1));
4978 if (ass
4979 && !gimple_has_volatile_ops (ass)
4980 && vn_get_stmt_kind (ass) == VN_REFERENCE)
4982 tree last_vuse = gimple_vuse (ass);
4983 tree op = gimple_assign_rhs1 (ass);
4984 tree result = vn_reference_lookup (op, gimple_vuse (ass),
4985 default_vn_walk_kind,
4986 NULL, true, &last_vuse,
4987 gimple_assign_rhs2 (stmt));
4988 if (result
4989 && useless_type_conversion_p (TREE_TYPE (result),
4990 TREE_TYPE (op)))
4991 return set_ssa_val_to (lhs, result);
4994 break;
4995 case TRUNC_DIV_EXPR:
4996 if (TYPE_UNSIGNED (type))
4997 break;
4998 /* Fallthru. */
4999 case RDIV_EXPR:
5000 case MULT_EXPR:
5001 /* Match up ([-]a){/,*}([-])b with v=a{/,*}b, replacing it with -v. */
5002 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
5004 tree rhs[2];
5005 rhs[0] = rhs1;
5006 rhs[1] = gimple_assign_rhs2 (stmt);
5007 for (unsigned i = 0; i <= 1; ++i)
5009 unsigned j = i == 0 ? 1 : 0;
5010 tree ops[2];
5011 gimple_match_op match_op (gimple_match_cond::UNCOND,
5012 NEGATE_EXPR, type, rhs[i]);
5013 ops[i] = vn_nary_build_or_lookup_1 (&match_op, false, true);
5014 ops[j] = rhs[j];
5015 if (ops[i]
5016 && (ops[0] = vn_nary_op_lookup_pieces (2, code,
5017 type, ops, NULL)))
5019 gimple_match_op match_op (gimple_match_cond::UNCOND,
5020 NEGATE_EXPR, type, ops[0]);
5021 result = vn_nary_build_or_lookup_1 (&match_op, true, false);
5022 if (result)
5024 bool changed = set_ssa_val_to (lhs, result);
5025 vn_nary_op_insert_stmt (stmt, result);
5026 return changed;
5031 break;
5032 default:
5033 break;
5036 bool changed = set_ssa_val_to (lhs, lhs);
5037 vn_nary_op_insert_stmt (stmt, lhs);
5038 return changed;
5041 /* Visit a call STMT storing into LHS. Return true if the value number
5042 of the LHS has changed as a result. */
5044 static bool
5045 visit_reference_op_call (tree lhs, gcall *stmt)
5047 bool changed = false;
5048 struct vn_reference_s vr1;
5049 vn_reference_t vnresult = NULL;
5050 tree vdef = gimple_vdef (stmt);
5052 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
5053 if (lhs && TREE_CODE (lhs) != SSA_NAME)
5054 lhs = NULL_TREE;
5056 vn_reference_lookup_call (stmt, &vnresult, &vr1);
5057 if (vnresult)
5059 if (vnresult->result_vdef && vdef)
5060 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
5061 else if (vdef)
5062 /* If the call was discovered to be pure or const reflect
5063 that as far as possible. */
5064 changed |= set_ssa_val_to (vdef, vuse_ssa_val (gimple_vuse (stmt)));
5066 if (!vnresult->result && lhs)
5067 vnresult->result = lhs;
5069 if (vnresult->result && lhs)
5070 changed |= set_ssa_val_to (lhs, vnresult->result);
5072 else
5074 vn_reference_t vr2;
5075 vn_reference_s **slot;
5076 tree vdef_val = vdef;
5077 if (vdef)
5079 /* If we value numbered an indirect functions function to
5080 one not clobbering memory value number its VDEF to its
5081 VUSE. */
5082 tree fn = gimple_call_fn (stmt);
5083 if (fn && TREE_CODE (fn) == SSA_NAME)
5085 fn = SSA_VAL (fn);
5086 if (TREE_CODE (fn) == ADDR_EXPR
5087 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
5088 && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
5089 & (ECF_CONST | ECF_PURE)))
5090 vdef_val = vuse_ssa_val (gimple_vuse (stmt));
5092 changed |= set_ssa_val_to (vdef, vdef_val);
5094 if (lhs)
5095 changed |= set_ssa_val_to (lhs, lhs);
5096 vr2 = XOBNEW (&vn_tables_obstack, vn_reference_s);
5097 vr2->vuse = vr1.vuse;
5098 /* As we are not walking the virtual operand chain we know the
5099 shared_lookup_references are still original so we can re-use
5100 them here. */
5101 vr2->operands = vr1.operands.copy ();
5102 vr2->type = vr1.type;
5103 vr2->punned = vr1.punned;
5104 vr2->set = vr1.set;
5105 vr2->base_set = vr1.base_set;
5106 vr2->hashcode = vr1.hashcode;
5107 vr2->result = lhs;
5108 vr2->result_vdef = vdef_val;
5109 vr2->value_id = 0;
5110 slot = valid_info->references->find_slot_with_hash (vr2, vr2->hashcode,
5111 INSERT);
5112 gcc_assert (!*slot);
5113 *slot = vr2;
5114 vr2->next = last_inserted_ref;
5115 last_inserted_ref = vr2;
5118 return changed;
5121 /* Visit a load from a reference operator RHS, part of STMT, value number it,
5122 and return true if the value number of the LHS has changed as a result. */
5124 static bool
5125 visit_reference_op_load (tree lhs, tree op, gimple *stmt)
5127 bool changed = false;
5128 tree result;
5129 vn_reference_t res;
5131 tree vuse = gimple_vuse (stmt);
5132 tree last_vuse = vuse;
5133 result = vn_reference_lookup (op, vuse, default_vn_walk_kind, &res, true, &last_vuse);
5135 /* We handle type-punning through unions by value-numbering based
5136 on offset and size of the access. Be prepared to handle a
5137 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
5138 if (result
5139 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
5141 /* Avoid the type punning in case the result mode has padding where
5142 the op we lookup has not. */
5143 if (maybe_lt (GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (result))),
5144 GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (op)))))
5145 result = NULL_TREE;
5146 else
5148 /* We will be setting the value number of lhs to the value number
5149 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
5150 So first simplify and lookup this expression to see if it
5151 is already available. */
5152 gimple_match_op res_op (gimple_match_cond::UNCOND,
5153 VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
5154 result = vn_nary_build_or_lookup (&res_op);
5155 if (result
5156 && TREE_CODE (result) == SSA_NAME
5157 && VN_INFO (result)->needs_insertion)
5158 /* Track whether this is the canonical expression for different
5159 typed loads. We use that as a stopgap measure for code
5160 hoisting when dealing with floating point loads. */
5161 res->punned = true;
5164 /* When building the conversion fails avoid inserting the reference
5165 again. */
5166 if (!result)
5167 return set_ssa_val_to (lhs, lhs);
5170 if (result)
5171 changed = set_ssa_val_to (lhs, result);
5172 else
5174 changed = set_ssa_val_to (lhs, lhs);
5175 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
5176 if (vuse && SSA_VAL (last_vuse) != SSA_VAL (vuse))
5178 if (dump_file && (dump_flags & TDF_DETAILS))
5180 fprintf (dump_file, "Using extra use virtual operand ");
5181 print_generic_expr (dump_file, last_vuse);
5182 fprintf (dump_file, "\n");
5184 vn_reference_insert (op, lhs, vuse, NULL_TREE);
5188 return changed;
5192 /* Visit a store to a reference operator LHS, part of STMT, value number it,
5193 and return true if the value number of the LHS has changed as a result. */
5195 static bool
5196 visit_reference_op_store (tree lhs, tree op, gimple *stmt)
5198 bool changed = false;
5199 vn_reference_t vnresult = NULL;
5200 tree assign;
5201 bool resultsame = false;
5202 tree vuse = gimple_vuse (stmt);
5203 tree vdef = gimple_vdef (stmt);
5205 if (TREE_CODE (op) == SSA_NAME)
5206 op = SSA_VAL (op);
5208 /* First we want to lookup using the *vuses* from the store and see
5209 if there the last store to this location with the same address
5210 had the same value.
5212 The vuses represent the memory state before the store. If the
5213 memory state, address, and value of the store is the same as the
5214 last store to this location, then this store will produce the
5215 same memory state as that store.
5217 In this case the vdef versions for this store are value numbered to those
5218 vuse versions, since they represent the same memory state after
5219 this store.
5221 Otherwise, the vdefs for the store are used when inserting into
5222 the table, since the store generates a new memory state. */
5224 vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false);
5225 if (vnresult
5226 && vnresult->result)
5228 tree result = vnresult->result;
5229 gcc_checking_assert (TREE_CODE (result) != SSA_NAME
5230 || result == SSA_VAL (result));
5231 resultsame = expressions_equal_p (result, op);
5232 if (resultsame)
5234 /* If the TBAA state isn't compatible for downstream reads
5235 we cannot value-number the VDEFs the same. */
5236 ao_ref lhs_ref;
5237 ao_ref_init (&lhs_ref, lhs);
5238 alias_set_type set = ao_ref_alias_set (&lhs_ref);
5239 alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
5240 if ((vnresult->set != set
5241 && ! alias_set_subset_of (set, vnresult->set))
5242 || (vnresult->base_set != base_set
5243 && ! alias_set_subset_of (base_set, vnresult->base_set)))
5244 resultsame = false;
5248 if (!resultsame)
5250 /* Only perform the following when being called from PRE
5251 which embeds tail merging. */
5252 if (default_vn_walk_kind == VN_WALK)
5254 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
5255 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false);
5256 if (vnresult)
5258 VN_INFO (vdef)->visited = true;
5259 return set_ssa_val_to (vdef, vnresult->result_vdef);
5263 if (dump_file && (dump_flags & TDF_DETAILS))
5265 fprintf (dump_file, "No store match\n");
5266 fprintf (dump_file, "Value numbering store ");
5267 print_generic_expr (dump_file, lhs);
5268 fprintf (dump_file, " to ");
5269 print_generic_expr (dump_file, op);
5270 fprintf (dump_file, "\n");
5272 /* Have to set value numbers before insert, since insert is
5273 going to valueize the references in-place. */
5274 if (vdef)
5275 changed |= set_ssa_val_to (vdef, vdef);
5277 /* Do not insert structure copies into the tables. */
5278 if (is_gimple_min_invariant (op)
5279 || is_gimple_reg (op))
5280 vn_reference_insert (lhs, op, vdef, NULL);
5282 /* Only perform the following when being called from PRE
5283 which embeds tail merging. */
5284 if (default_vn_walk_kind == VN_WALK)
5286 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
5287 vn_reference_insert (assign, lhs, vuse, vdef);
5290 else
5292 /* We had a match, so value number the vdef to have the value
5293 number of the vuse it came from. */
5295 if (dump_file && (dump_flags & TDF_DETAILS))
5296 fprintf (dump_file, "Store matched earlier value, "
5297 "value numbering store vdefs to matching vuses.\n");
5299 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
5302 return changed;
5305 /* Visit and value number PHI, return true if the value number
5306 changed. When BACKEDGES_VARYING_P is true then assume all
5307 backedge values are varying. When INSERTED is not NULL then
5308 this is just a ahead query for a possible iteration, set INSERTED
5309 to true if we'd insert into the hashtable. */
5311 static bool
5312 visit_phi (gimple *phi, bool *inserted, bool backedges_varying_p)
5314 tree result, sameval = VN_TOP, seen_undef = NULL_TREE;
5315 tree backedge_val = NULL_TREE;
5316 bool seen_non_backedge = false;
5317 tree sameval_base = NULL_TREE;
5318 poly_int64 soff, doff;
5319 unsigned n_executable = 0;
5320 edge_iterator ei;
5321 edge e;
5323 /* TODO: We could check for this in initialization, and replace this
5324 with a gcc_assert. */
5325 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
5326 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
5328 /* We track whether a PHI was CSEd to to avoid excessive iterations
5329 that would be necessary only because the PHI changed arguments
5330 but not value. */
5331 if (!inserted)
5332 gimple_set_plf (phi, GF_PLF_1, false);
5334 /* See if all non-TOP arguments have the same value. TOP is
5335 equivalent to everything, so we can ignore it. */
5336 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
5337 if (e->flags & EDGE_EXECUTABLE)
5339 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
5341 if (def == PHI_RESULT (phi))
5342 continue;
5343 ++n_executable;
5344 if (TREE_CODE (def) == SSA_NAME)
5346 if (!backedges_varying_p || !(e->flags & EDGE_DFS_BACK))
5347 def = SSA_VAL (def);
5348 if (e->flags & EDGE_DFS_BACK)
5349 backedge_val = def;
5351 if (!(e->flags & EDGE_DFS_BACK))
5352 seen_non_backedge = true;
5353 if (def == VN_TOP)
5355 /* Ignore undefined defs for sameval but record one. */
5356 else if (TREE_CODE (def) == SSA_NAME
5357 && ! virtual_operand_p (def)
5358 && ssa_undefined_value_p (def, false))
5359 seen_undef = def;
5360 else if (sameval == VN_TOP)
5361 sameval = def;
5362 else if (!expressions_equal_p (def, sameval))
5364 /* We know we're arriving only with invariant addresses here,
5365 try harder comparing them. We can do some caching here
5366 which we cannot do in expressions_equal_p. */
5367 if (TREE_CODE (def) == ADDR_EXPR
5368 && TREE_CODE (sameval) == ADDR_EXPR
5369 && sameval_base != (void *)-1)
5371 if (!sameval_base)
5372 sameval_base = get_addr_base_and_unit_offset
5373 (TREE_OPERAND (sameval, 0), &soff);
5374 if (!sameval_base)
5375 sameval_base = (tree)(void *)-1;
5376 else if ((get_addr_base_and_unit_offset
5377 (TREE_OPERAND (def, 0), &doff) == sameval_base)
5378 && known_eq (soff, doff))
5379 continue;
5381 sameval = NULL_TREE;
5382 break;
5386 /* If the value we want to use is flowing over the backedge and we
5387 should take it as VARYING but it has a non-VARYING value drop to
5388 VARYING.
5389 If we value-number a virtual operand never value-number to the
5390 value from the backedge as that confuses the alias-walking code.
5391 See gcc.dg/torture/pr87176.c. If the value is the same on a
5392 non-backedge everything is OK though. */
5393 bool visited_p;
5394 if ((backedge_val
5395 && !seen_non_backedge
5396 && TREE_CODE (backedge_val) == SSA_NAME
5397 && sameval == backedge_val
5398 && (SSA_NAME_IS_VIRTUAL_OPERAND (backedge_val)
5399 || SSA_VAL (backedge_val) != backedge_val))
5400 /* Do not value-number a virtual operand to sth not visited though
5401 given that allows us to escape a region in alias walking. */
5402 || (sameval
5403 && TREE_CODE (sameval) == SSA_NAME
5404 && !SSA_NAME_IS_DEFAULT_DEF (sameval)
5405 && SSA_NAME_IS_VIRTUAL_OPERAND (sameval)
5406 && (SSA_VAL (sameval, &visited_p), !visited_p)))
5407 /* Note this just drops to VARYING without inserting the PHI into
5408 the hashes. */
5409 result = PHI_RESULT (phi);
5410 /* If none of the edges was executable keep the value-number at VN_TOP,
5411 if only a single edge is exectuable use its value. */
5412 else if (n_executable <= 1)
5413 result = seen_undef ? seen_undef : sameval;
5414 /* If we saw only undefined values and VN_TOP use one of the
5415 undefined values. */
5416 else if (sameval == VN_TOP)
5417 result = seen_undef ? seen_undef : sameval;
5418 /* First see if it is equivalent to a phi node in this block. We prefer
5419 this as it allows IV elimination - see PRs 66502 and 67167. */
5420 else if ((result = vn_phi_lookup (phi, backedges_varying_p)))
5422 if (!inserted
5423 && TREE_CODE (result) == SSA_NAME
5424 && gimple_code (SSA_NAME_DEF_STMT (result)) == GIMPLE_PHI)
5426 gimple_set_plf (SSA_NAME_DEF_STMT (result), GF_PLF_1, true);
5427 if (dump_file && (dump_flags & TDF_DETAILS))
5429 fprintf (dump_file, "Marking CSEd to PHI node ");
5430 print_gimple_expr (dump_file, SSA_NAME_DEF_STMT (result),
5431 0, TDF_SLIM);
5432 fprintf (dump_file, "\n");
5436 /* If all values are the same use that, unless we've seen undefined
5437 values as well and the value isn't constant.
5438 CCP/copyprop have the same restriction to not remove uninit warnings. */
5439 else if (sameval
5440 && (! seen_undef || is_gimple_min_invariant (sameval)))
5441 result = sameval;
5442 else
5444 result = PHI_RESULT (phi);
5445 /* Only insert PHIs that are varying, for constant value numbers
5446 we mess up equivalences otherwise as we are only comparing
5447 the immediate controlling predicates. */
5448 vn_phi_insert (phi, result, backedges_varying_p);
5449 if (inserted)
5450 *inserted = true;
5453 return set_ssa_val_to (PHI_RESULT (phi), result);
5456 /* Try to simplify RHS using equivalences and constant folding. */
5458 static tree
5459 try_to_simplify (gassign *stmt)
5461 enum tree_code code = gimple_assign_rhs_code (stmt);
5462 tree tem;
5464 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
5465 in this case, there is no point in doing extra work. */
5466 if (code == SSA_NAME)
5467 return NULL_TREE;
5469 /* First try constant folding based on our current lattice. */
5470 mprts_hook = vn_lookup_simplify_result;
5471 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
5472 mprts_hook = NULL;
5473 if (tem
5474 && (TREE_CODE (tem) == SSA_NAME
5475 || is_gimple_min_invariant (tem)))
5476 return tem;
5478 return NULL_TREE;
5481 /* Visit and value number STMT, return true if the value number
5482 changed. */
5484 static bool
5485 visit_stmt (gimple *stmt, bool backedges_varying_p = false)
5487 bool changed = false;
5489 if (dump_file && (dump_flags & TDF_DETAILS))
5491 fprintf (dump_file, "Value numbering stmt = ");
5492 print_gimple_stmt (dump_file, stmt, 0);
5495 if (gimple_code (stmt) == GIMPLE_PHI)
5496 changed = visit_phi (stmt, NULL, backedges_varying_p);
5497 else if (gimple_has_volatile_ops (stmt))
5498 changed = defs_to_varying (stmt);
5499 else if (gassign *ass = dyn_cast <gassign *> (stmt))
5501 enum tree_code code = gimple_assign_rhs_code (ass);
5502 tree lhs = gimple_assign_lhs (ass);
5503 tree rhs1 = gimple_assign_rhs1 (ass);
5504 tree simplified;
5506 /* Shortcut for copies. Simplifying copies is pointless,
5507 since we copy the expression and value they represent. */
5508 if (code == SSA_NAME
5509 && TREE_CODE (lhs) == SSA_NAME)
5511 changed = visit_copy (lhs, rhs1);
5512 goto done;
5514 simplified = try_to_simplify (ass);
5515 if (simplified)
5517 if (dump_file && (dump_flags & TDF_DETAILS))
5519 fprintf (dump_file, "RHS ");
5520 print_gimple_expr (dump_file, ass, 0);
5521 fprintf (dump_file, " simplified to ");
5522 print_generic_expr (dump_file, simplified);
5523 fprintf (dump_file, "\n");
5526 /* Setting value numbers to constants will occasionally
5527 screw up phi congruence because constants are not
5528 uniquely associated with a single ssa name that can be
5529 looked up. */
5530 if (simplified
5531 && is_gimple_min_invariant (simplified)
5532 && TREE_CODE (lhs) == SSA_NAME)
5534 changed = set_ssa_val_to (lhs, simplified);
5535 goto done;
5537 else if (simplified
5538 && TREE_CODE (simplified) == SSA_NAME
5539 && TREE_CODE (lhs) == SSA_NAME)
5541 changed = visit_copy (lhs, simplified);
5542 goto done;
5545 if ((TREE_CODE (lhs) == SSA_NAME
5546 /* We can substitute SSA_NAMEs that are live over
5547 abnormal edges with their constant value. */
5548 && !(gimple_assign_copy_p (ass)
5549 && is_gimple_min_invariant (rhs1))
5550 && !(simplified
5551 && is_gimple_min_invariant (simplified))
5552 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
5553 /* Stores or copies from SSA_NAMEs that are live over
5554 abnormal edges are a problem. */
5555 || (code == SSA_NAME
5556 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
5557 changed = defs_to_varying (ass);
5558 else if (REFERENCE_CLASS_P (lhs)
5559 || DECL_P (lhs))
5560 changed = visit_reference_op_store (lhs, rhs1, ass);
5561 else if (TREE_CODE (lhs) == SSA_NAME)
5563 if ((gimple_assign_copy_p (ass)
5564 && is_gimple_min_invariant (rhs1))
5565 || (simplified
5566 && is_gimple_min_invariant (simplified)))
5568 if (simplified)
5569 changed = set_ssa_val_to (lhs, simplified);
5570 else
5571 changed = set_ssa_val_to (lhs, rhs1);
5573 else
5575 /* Visit the original statement. */
5576 switch (vn_get_stmt_kind (ass))
5578 case VN_NARY:
5579 changed = visit_nary_op (lhs, ass);
5580 break;
5581 case VN_REFERENCE:
5582 changed = visit_reference_op_load (lhs, rhs1, ass);
5583 break;
5584 default:
5585 changed = defs_to_varying (ass);
5586 break;
5590 else
5591 changed = defs_to_varying (ass);
5593 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
5595 tree lhs = gimple_call_lhs (call_stmt);
5596 if (lhs && TREE_CODE (lhs) == SSA_NAME)
5598 /* Try constant folding based on our current lattice. */
5599 tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt,
5600 vn_valueize);
5601 if (simplified)
5603 if (dump_file && (dump_flags & TDF_DETAILS))
5605 fprintf (dump_file, "call ");
5606 print_gimple_expr (dump_file, call_stmt, 0);
5607 fprintf (dump_file, " simplified to ");
5608 print_generic_expr (dump_file, simplified);
5609 fprintf (dump_file, "\n");
5612 /* Setting value numbers to constants will occasionally
5613 screw up phi congruence because constants are not
5614 uniquely associated with a single ssa name that can be
5615 looked up. */
5616 if (simplified
5617 && is_gimple_min_invariant (simplified))
5619 changed = set_ssa_val_to (lhs, simplified);
5620 if (gimple_vdef (call_stmt))
5621 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
5622 SSA_VAL (gimple_vuse (call_stmt)));
5623 goto done;
5625 else if (simplified
5626 && TREE_CODE (simplified) == SSA_NAME)
5628 changed = visit_copy (lhs, simplified);
5629 if (gimple_vdef (call_stmt))
5630 changed |= set_ssa_val_to (gimple_vdef (call_stmt),
5631 SSA_VAL (gimple_vuse (call_stmt)));
5632 goto done;
5634 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
5636 changed = defs_to_varying (call_stmt);
5637 goto done;
5641 /* Pick up flags from a devirtualization target. */
5642 tree fn = gimple_call_fn (stmt);
5643 int extra_fnflags = 0;
5644 if (fn && TREE_CODE (fn) == SSA_NAME)
5646 fn = SSA_VAL (fn);
5647 if (TREE_CODE (fn) == ADDR_EXPR
5648 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL)
5649 extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0));
5651 if (!gimple_call_internal_p (call_stmt)
5652 && (/* Calls to the same function with the same vuse
5653 and the same operands do not necessarily return the same
5654 value, unless they're pure or const. */
5655 ((gimple_call_flags (call_stmt) | extra_fnflags)
5656 & (ECF_PURE | ECF_CONST))
5657 /* If calls have a vdef, subsequent calls won't have
5658 the same incoming vuse. So, if 2 calls with vdef have the
5659 same vuse, we know they're not subsequent.
5660 We can value number 2 calls to the same function with the
5661 same vuse and the same operands which are not subsequent
5662 the same, because there is no code in the program that can
5663 compare the 2 values... */
5664 || (gimple_vdef (call_stmt)
5665 /* ... unless the call returns a pointer which does
5666 not alias with anything else. In which case the
5667 information that the values are distinct are encoded
5668 in the IL. */
5669 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
5670 /* Only perform the following when being called from PRE
5671 which embeds tail merging. */
5672 && default_vn_walk_kind == VN_WALK)))
5673 changed = visit_reference_op_call (lhs, call_stmt);
5674 else
5675 changed = defs_to_varying (call_stmt);
5677 else
5678 changed = defs_to_varying (stmt);
5679 done:
5680 return changed;
5684 /* Allocate a value number table. */
5686 static void
5687 allocate_vn_table (vn_tables_t table, unsigned size)
5689 table->phis = new vn_phi_table_type (size);
5690 table->nary = new vn_nary_op_table_type (size);
5691 table->references = new vn_reference_table_type (size);
5694 /* Free a value number table. */
5696 static void
5697 free_vn_table (vn_tables_t table)
5699 /* Walk over elements and release vectors. */
5700 vn_reference_iterator_type hir;
5701 vn_reference_t vr;
5702 FOR_EACH_HASH_TABLE_ELEMENT (*table->references, vr, vn_reference_t, hir)
5703 vr->operands.release ();
5704 delete table->phis;
5705 table->phis = NULL;
5706 delete table->nary;
5707 table->nary = NULL;
5708 delete table->references;
5709 table->references = NULL;
5712 /* Set *ID according to RESULT. */
5714 static void
5715 set_value_id_for_result (tree result, unsigned int *id)
5717 if (result && TREE_CODE (result) == SSA_NAME)
5718 *id = VN_INFO (result)->value_id;
5719 else if (result && is_gimple_min_invariant (result))
5720 *id = get_or_alloc_constant_value_id (result);
5721 else
5722 *id = get_next_value_id ();
5725 /* Set the value ids in the valid hash tables. */
5727 static void
5728 set_hashtable_value_ids (void)
5730 vn_nary_op_iterator_type hin;
5731 vn_phi_iterator_type hip;
5732 vn_reference_iterator_type hir;
5733 vn_nary_op_t vno;
5734 vn_reference_t vr;
5735 vn_phi_t vp;
5737 /* Now set the value ids of the things we had put in the hash
5738 table. */
5740 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
5741 if (! vno->predicated_values)
5742 set_value_id_for_result (vno->u.result, &vno->value_id);
5744 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
5745 set_value_id_for_result (vp->result, &vp->value_id);
5747 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
5748 hir)
5749 set_value_id_for_result (vr->result, &vr->value_id);
5752 /* Return the maximum value id we have ever seen. */
5754 unsigned int
5755 get_max_value_id (void)
5757 return next_value_id;
5760 /* Return the maximum constant value id we have ever seen. */
5762 unsigned int
5763 get_max_constant_value_id (void)
5765 return -next_constant_value_id;
5768 /* Return the next unique value id. */
5770 unsigned int
5771 get_next_value_id (void)
5773 gcc_checking_assert ((int)next_value_id > 0);
5774 return next_value_id++;
5777 /* Return the next unique value id for constants. */
5779 unsigned int
5780 get_next_constant_value_id (void)
5782 gcc_checking_assert (next_constant_value_id < 0);
5783 return next_constant_value_id--;
5787 /* Compare two expressions E1 and E2 and return true if they are equal. */
5789 bool
5790 expressions_equal_p (tree e1, tree e2)
5792 /* The obvious case. */
5793 if (e1 == e2)
5794 return true;
5796 /* If either one is VN_TOP consider them equal. */
5797 if (e1 == VN_TOP || e2 == VN_TOP)
5798 return true;
5800 /* SSA_NAME compare pointer equal. */
5801 if (TREE_CODE (e1) == SSA_NAME || TREE_CODE (e2) == SSA_NAME)
5802 return false;
5804 /* Now perform the actual comparison. */
5805 if (TREE_CODE (e1) == TREE_CODE (e2)
5806 && operand_equal_p (e1, e2, OEP_PURE_SAME))
5807 return true;
5809 return false;
5813 /* Return true if the nary operation NARY may trap. This is a copy
5814 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
5816 bool
5817 vn_nary_may_trap (vn_nary_op_t nary)
5819 tree type;
5820 tree rhs2 = NULL_TREE;
5821 bool honor_nans = false;
5822 bool honor_snans = false;
5823 bool fp_operation = false;
5824 bool honor_trapv = false;
5825 bool handled, ret;
5826 unsigned i;
5828 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
5829 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
5830 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
5832 type = nary->type;
5833 fp_operation = FLOAT_TYPE_P (type);
5834 if (fp_operation)
5836 honor_nans = flag_trapping_math && !flag_finite_math_only;
5837 honor_snans = flag_signaling_nans != 0;
5839 else if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type))
5840 honor_trapv = true;
5842 if (nary->length >= 2)
5843 rhs2 = nary->op[1];
5844 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
5845 honor_trapv, honor_nans, honor_snans,
5846 rhs2, &handled);
5847 if (handled && ret)
5848 return true;
5850 for (i = 0; i < nary->length; ++i)
5851 if (tree_could_trap_p (nary->op[i]))
5852 return true;
5854 return false;
5857 /* Return true if the reference operation REF may trap. */
5859 bool
5860 vn_reference_may_trap (vn_reference_t ref)
5862 switch (ref->operands[0].opcode)
5864 case MODIFY_EXPR:
5865 case CALL_EXPR:
5866 /* We do not handle calls. */
5867 return true;
5868 case ADDR_EXPR:
5869 /* And toplevel address computations never trap. */
5870 return false;
5871 default:;
5874 vn_reference_op_t op;
5875 unsigned i;
5876 FOR_EACH_VEC_ELT (ref->operands, i, op)
5878 switch (op->opcode)
5880 case WITH_SIZE_EXPR:
5881 case TARGET_MEM_REF:
5882 /* Always variable. */
5883 return true;
5884 case COMPONENT_REF:
5885 if (op->op1 && TREE_CODE (op->op1) == SSA_NAME)
5886 return true;
5887 break;
5888 case ARRAY_RANGE_REF:
5889 if (TREE_CODE (op->op0) == SSA_NAME)
5890 return true;
5891 break;
5892 case ARRAY_REF:
5894 if (TREE_CODE (op->op0) != INTEGER_CST)
5895 return true;
5897 /* !in_array_bounds */
5898 tree domain_type = TYPE_DOMAIN (ref->operands[i+1].type);
5899 if (!domain_type)
5900 return true;
5902 tree min = op->op1;
5903 tree max = TYPE_MAX_VALUE (domain_type);
5904 if (!min
5905 || !max
5906 || TREE_CODE (min) != INTEGER_CST
5907 || TREE_CODE (max) != INTEGER_CST)
5908 return true;
5910 if (tree_int_cst_lt (op->op0, min)
5911 || tree_int_cst_lt (max, op->op0))
5912 return true;
5914 break;
5916 case MEM_REF:
5917 /* Nothing interesting in itself, the base is separate. */
5918 break;
5919 /* The following are the address bases. */
5920 case SSA_NAME:
5921 return true;
5922 case ADDR_EXPR:
5923 if (op->op0)
5924 return tree_could_trap_p (TREE_OPERAND (op->op0, 0));
5925 return false;
5926 default:;
5929 return false;
5932 eliminate_dom_walker::eliminate_dom_walker (cdi_direction direction,
5933 bitmap inserted_exprs_)
5934 : dom_walker (direction), do_pre (inserted_exprs_ != NULL),
5935 el_todo (0), eliminations (0), insertions (0),
5936 inserted_exprs (inserted_exprs_)
5938 need_eh_cleanup = BITMAP_ALLOC (NULL);
5939 need_ab_cleanup = BITMAP_ALLOC (NULL);
5942 eliminate_dom_walker::~eliminate_dom_walker ()
5944 BITMAP_FREE (need_eh_cleanup);
5945 BITMAP_FREE (need_ab_cleanup);
5948 /* Return a leader for OP that is available at the current point of the
5949 eliminate domwalk. */
5951 tree
5952 eliminate_dom_walker::eliminate_avail (basic_block, tree op)
5954 tree valnum = VN_INFO (op)->valnum;
5955 if (TREE_CODE (valnum) == SSA_NAME)
5957 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
5958 return valnum;
5959 if (avail.length () > SSA_NAME_VERSION (valnum))
5960 return avail[SSA_NAME_VERSION (valnum)];
5962 else if (is_gimple_min_invariant (valnum))
5963 return valnum;
5964 return NULL_TREE;
5967 /* At the current point of the eliminate domwalk make OP available. */
5969 void
5970 eliminate_dom_walker::eliminate_push_avail (basic_block, tree op)
5972 tree valnum = VN_INFO (op)->valnum;
5973 if (TREE_CODE (valnum) == SSA_NAME)
5975 if (avail.length () <= SSA_NAME_VERSION (valnum))
5976 avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1, true);
5977 tree pushop = op;
5978 if (avail[SSA_NAME_VERSION (valnum)])
5979 pushop = avail[SSA_NAME_VERSION (valnum)];
5980 avail_stack.safe_push (pushop);
5981 avail[SSA_NAME_VERSION (valnum)] = op;
5985 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
5986 the leader for the expression if insertion was successful. */
5988 tree
5989 eliminate_dom_walker::eliminate_insert (basic_block bb,
5990 gimple_stmt_iterator *gsi, tree val)
5992 /* We can insert a sequence with a single assignment only. */
5993 gimple_seq stmts = VN_INFO (val)->expr;
5994 if (!gimple_seq_singleton_p (stmts))
5995 return NULL_TREE;
5996 gassign *stmt = dyn_cast <gassign *> (gimple_seq_first_stmt (stmts));
5997 if (!stmt
5998 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
5999 && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
6000 && gimple_assign_rhs_code (stmt) != NEGATE_EXPR
6001 && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF
6002 && (gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6003 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)))
6004 return NULL_TREE;
6006 tree op = gimple_assign_rhs1 (stmt);
6007 if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
6008 || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6009 op = TREE_OPERAND (op, 0);
6010 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (bb, op) : op;
6011 if (!leader)
6012 return NULL_TREE;
6014 tree res;
6015 stmts = NULL;
6016 if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
6017 res = gimple_build (&stmts, BIT_FIELD_REF,
6018 TREE_TYPE (val), leader,
6019 TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
6020 TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
6021 else if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR)
6022 res = gimple_build (&stmts, BIT_AND_EXPR,
6023 TREE_TYPE (val), leader, gimple_assign_rhs2 (stmt));
6024 else
6025 res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
6026 TREE_TYPE (val), leader);
6027 if (TREE_CODE (res) != SSA_NAME
6028 || SSA_NAME_IS_DEFAULT_DEF (res)
6029 || gimple_bb (SSA_NAME_DEF_STMT (res)))
6031 gimple_seq_discard (stmts);
6033 /* During propagation we have to treat SSA info conservatively
6034 and thus we can end up simplifying the inserted expression
6035 at elimination time to sth not defined in stmts. */
6036 /* But then this is a redundancy we failed to detect. Which means
6037 res now has two values. That doesn't play well with how
6038 we track availability here, so give up. */
6039 if (dump_file && (dump_flags & TDF_DETAILS))
6041 if (TREE_CODE (res) == SSA_NAME)
6042 res = eliminate_avail (bb, res);
6043 if (res)
6045 fprintf (dump_file, "Failed to insert expression for value ");
6046 print_generic_expr (dump_file, val);
6047 fprintf (dump_file, " which is really fully redundant to ");
6048 print_generic_expr (dump_file, res);
6049 fprintf (dump_file, "\n");
6053 return NULL_TREE;
6055 else
6057 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
6058 vn_ssa_aux_t vn_info = VN_INFO (res);
6059 vn_info->valnum = val;
6060 vn_info->visited = true;
6063 insertions++;
6064 if (dump_file && (dump_flags & TDF_DETAILS))
6066 fprintf (dump_file, "Inserted ");
6067 print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0);
6070 return res;
6073 void
6074 eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
6076 tree sprime = NULL_TREE;
6077 gimple *stmt = gsi_stmt (*gsi);
6078 tree lhs = gimple_get_lhs (stmt);
6079 if (lhs && TREE_CODE (lhs) == SSA_NAME
6080 && !gimple_has_volatile_ops (stmt)
6081 /* See PR43491. Do not replace a global register variable when
6082 it is a the RHS of an assignment. Do replace local register
6083 variables since gcc does not guarantee a local variable will
6084 be allocated in register.
6085 ??? The fix isn't effective here. This should instead
6086 be ensured by not value-numbering them the same but treating
6087 them like volatiles? */
6088 && !(gimple_assign_single_p (stmt)
6089 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
6090 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
6091 && is_global_var (gimple_assign_rhs1 (stmt)))))
6093 sprime = eliminate_avail (b, lhs);
6094 if (!sprime)
6096 /* If there is no existing usable leader but SCCVN thinks
6097 it has an expression it wants to use as replacement,
6098 insert that. */
6099 tree val = VN_INFO (lhs)->valnum;
6100 vn_ssa_aux_t vn_info;
6101 if (val != VN_TOP
6102 && TREE_CODE (val) == SSA_NAME
6103 && (vn_info = VN_INFO (val), true)
6104 && vn_info->needs_insertion
6105 && vn_info->expr != NULL
6106 && (sprime = eliminate_insert (b, gsi, val)) != NULL_TREE)
6107 eliminate_push_avail (b, sprime);
6110 /* If this now constitutes a copy duplicate points-to
6111 and range info appropriately. This is especially
6112 important for inserted code. See tree-ssa-copy.c
6113 for similar code. */
6114 if (sprime
6115 && TREE_CODE (sprime) == SSA_NAME)
6117 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
6118 if (POINTER_TYPE_P (TREE_TYPE (lhs))
6119 && SSA_NAME_PTR_INFO (lhs)
6120 && ! SSA_NAME_PTR_INFO (sprime))
6122 duplicate_ssa_name_ptr_info (sprime,
6123 SSA_NAME_PTR_INFO (lhs));
6124 if (b != sprime_b)
6125 reset_flow_sensitive_info (sprime);
6127 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6128 && SSA_NAME_RANGE_INFO (lhs)
6129 && ! SSA_NAME_RANGE_INFO (sprime)
6130 && b == sprime_b)
6131 duplicate_ssa_name_range_info (sprime,
6132 SSA_NAME_RANGE_TYPE (lhs),
6133 SSA_NAME_RANGE_INFO (lhs));
6136 /* Inhibit the use of an inserted PHI on a loop header when
6137 the address of the memory reference is a simple induction
6138 variable. In other cases the vectorizer won't do anything
6139 anyway (either it's loop invariant or a complicated
6140 expression). */
6141 if (sprime
6142 && TREE_CODE (sprime) == SSA_NAME
6143 && do_pre
6144 && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
6145 && loop_outer (b->loop_father)
6146 && has_zero_uses (sprime)
6147 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
6148 && gimple_assign_load_p (stmt))
6150 gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
6151 basic_block def_bb = gimple_bb (def_stmt);
6152 if (gimple_code (def_stmt) == GIMPLE_PHI
6153 && def_bb->loop_father->header == def_bb)
6155 loop_p loop = def_bb->loop_father;
6156 ssa_op_iter iter;
6157 tree op;
6158 bool found = false;
6159 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
6161 affine_iv iv;
6162 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
6163 if (def_bb
6164 && flow_bb_inside_loop_p (loop, def_bb)
6165 && simple_iv (loop, loop, op, &iv, true))
6167 found = true;
6168 break;
6171 if (found)
6173 if (dump_file && (dump_flags & TDF_DETAILS))
6175 fprintf (dump_file, "Not replacing ");
6176 print_gimple_expr (dump_file, stmt, 0);
6177 fprintf (dump_file, " with ");
6178 print_generic_expr (dump_file, sprime);
6179 fprintf (dump_file, " which would add a loop"
6180 " carried dependence to loop %d\n",
6181 loop->num);
6183 /* Don't keep sprime available. */
6184 sprime = NULL_TREE;
6189 if (sprime)
6191 /* If we can propagate the value computed for LHS into
6192 all uses don't bother doing anything with this stmt. */
6193 if (may_propagate_copy (lhs, sprime))
6195 /* Mark it for removal. */
6196 to_remove.safe_push (stmt);
6198 /* ??? Don't count copy/constant propagations. */
6199 if (gimple_assign_single_p (stmt)
6200 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
6201 || gimple_assign_rhs1 (stmt) == sprime))
6202 return;
6204 if (dump_file && (dump_flags & TDF_DETAILS))
6206 fprintf (dump_file, "Replaced ");
6207 print_gimple_expr (dump_file, stmt, 0);
6208 fprintf (dump_file, " with ");
6209 print_generic_expr (dump_file, sprime);
6210 fprintf (dump_file, " in all uses of ");
6211 print_gimple_stmt (dump_file, stmt, 0);
6214 eliminations++;
6215 return;
6218 /* If this is an assignment from our leader (which
6219 happens in the case the value-number is a constant)
6220 then there is nothing to do. Likewise if we run into
6221 inserted code that needed a conversion because of
6222 our type-agnostic value-numbering of loads. */
6223 if ((gimple_assign_single_p (stmt)
6224 || (is_gimple_assign (stmt)
6225 && (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
6226 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)))
6227 && sprime == gimple_assign_rhs1 (stmt))
6228 return;
6230 /* Else replace its RHS. */
6231 if (dump_file && (dump_flags & TDF_DETAILS))
6233 fprintf (dump_file, "Replaced ");
6234 print_gimple_expr (dump_file, stmt, 0);
6235 fprintf (dump_file, " with ");
6236 print_generic_expr (dump_file, sprime);
6237 fprintf (dump_file, " in ");
6238 print_gimple_stmt (dump_file, stmt, 0);
6240 eliminations++;
6242 bool can_make_abnormal_goto = (is_gimple_call (stmt)
6243 && stmt_can_make_abnormal_goto (stmt));
6244 gimple *orig_stmt = stmt;
6245 if (!useless_type_conversion_p (TREE_TYPE (lhs),
6246 TREE_TYPE (sprime)))
6248 /* We preserve conversions to but not from function or method
6249 types. This asymmetry makes it necessary to re-instantiate
6250 conversions here. */
6251 if (POINTER_TYPE_P (TREE_TYPE (lhs))
6252 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (TREE_TYPE (lhs))))
6253 sprime = fold_convert (TREE_TYPE (lhs), sprime);
6254 else
6255 gcc_unreachable ();
6257 tree vdef = gimple_vdef (stmt);
6258 tree vuse = gimple_vuse (stmt);
6259 propagate_tree_value_into_stmt (gsi, sprime);
6260 stmt = gsi_stmt (*gsi);
6261 update_stmt (stmt);
6262 /* In case the VDEF on the original stmt was released, value-number
6263 it to the VUSE. This is to make vuse_ssa_val able to skip
6264 released virtual operands. */
6265 if (vdef != gimple_vdef (stmt))
6267 gcc_assert (SSA_NAME_IN_FREE_LIST (vdef));
6268 VN_INFO (vdef)->valnum = vuse;
6271 /* If we removed EH side-effects from the statement, clean
6272 its EH information. */
6273 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
6275 bitmap_set_bit (need_eh_cleanup,
6276 gimple_bb (stmt)->index);
6277 if (dump_file && (dump_flags & TDF_DETAILS))
6278 fprintf (dump_file, " Removed EH side-effects.\n");
6281 /* Likewise for AB side-effects. */
6282 if (can_make_abnormal_goto
6283 && !stmt_can_make_abnormal_goto (stmt))
6285 bitmap_set_bit (need_ab_cleanup,
6286 gimple_bb (stmt)->index);
6287 if (dump_file && (dump_flags & TDF_DETAILS))
6288 fprintf (dump_file, " Removed AB side-effects.\n");
6291 return;
6295 /* If the statement is a scalar store, see if the expression
6296 has the same value number as its rhs. If so, the store is
6297 dead. */
6298 if (gimple_assign_single_p (stmt)
6299 && !gimple_has_volatile_ops (stmt)
6300 && !is_gimple_reg (gimple_assign_lhs (stmt))
6301 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
6302 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
6304 tree rhs = gimple_assign_rhs1 (stmt);
6305 vn_reference_t vnresult;
6306 /* ??? gcc.dg/torture/pr91445.c shows that we lookup a boolean
6307 typed load of a byte known to be 0x11 as 1 so a store of
6308 a boolean 1 is detected as redundant. Because of this we
6309 have to make sure to lookup with a ref where its size
6310 matches the precision. */
6311 tree lookup_lhs = lhs;
6312 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6313 && (TREE_CODE (lhs) != COMPONENT_REF
6314 || !DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
6315 && !type_has_mode_precision_p (TREE_TYPE (lhs)))
6317 if (TREE_CODE (lhs) == COMPONENT_REF
6318 || TREE_CODE (lhs) == MEM_REF)
6320 tree ltype = build_nonstandard_integer_type
6321 (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (lhs))),
6322 TYPE_UNSIGNED (TREE_TYPE (lhs)));
6323 if (TREE_CODE (lhs) == COMPONENT_REF)
6325 tree foff = component_ref_field_offset (lhs);
6326 tree f = TREE_OPERAND (lhs, 1);
6327 if (!poly_int_tree_p (foff))
6328 lookup_lhs = NULL_TREE;
6329 else
6330 lookup_lhs = build3 (BIT_FIELD_REF, ltype,
6331 TREE_OPERAND (lhs, 0),
6332 TYPE_SIZE (TREE_TYPE (lhs)),
6333 bit_from_pos
6334 (foff, DECL_FIELD_BIT_OFFSET (f)));
6336 else
6337 lookup_lhs = build2 (MEM_REF, ltype,
6338 TREE_OPERAND (lhs, 0),
6339 TREE_OPERAND (lhs, 1));
6341 else
6342 lookup_lhs = NULL_TREE;
6344 tree val = NULL_TREE;
6345 if (lookup_lhs)
6346 val = vn_reference_lookup (lookup_lhs, gimple_vuse (stmt),
6347 VN_WALKREWRITE, &vnresult, false);
6348 if (TREE_CODE (rhs) == SSA_NAME)
6349 rhs = VN_INFO (rhs)->valnum;
6350 if (val
6351 && (operand_equal_p (val, rhs, 0)
6352 /* Due to the bitfield lookups above we can get bit
6353 interpretations of the same RHS as values here. Those
6354 are redundant as well. */
6355 || (TREE_CODE (val) == SSA_NAME
6356 && gimple_assign_single_p (SSA_NAME_DEF_STMT (val))
6357 && (val = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val)))
6358 && TREE_CODE (val) == VIEW_CONVERT_EXPR
6359 && TREE_OPERAND (val, 0) == rhs)))
6361 /* We can only remove the later store if the former aliases
6362 at least all accesses the later one does or if the store
6363 was to readonly memory storing the same value. */
6364 ao_ref lhs_ref;
6365 ao_ref_init (&lhs_ref, lhs);
6366 alias_set_type set = ao_ref_alias_set (&lhs_ref);
6367 alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
6368 if (! vnresult
6369 || ((vnresult->set == set
6370 || alias_set_subset_of (set, vnresult->set))
6371 && (vnresult->base_set == base_set
6372 || alias_set_subset_of (base_set, vnresult->base_set))))
6374 if (dump_file && (dump_flags & TDF_DETAILS))
6376 fprintf (dump_file, "Deleted redundant store ");
6377 print_gimple_stmt (dump_file, stmt, 0);
6380 /* Queue stmt for removal. */
6381 to_remove.safe_push (stmt);
6382 return;
6387 /* If this is a control statement value numbering left edges
6388 unexecuted on force the condition in a way consistent with
6389 that. */
6390 if (gcond *cond = dyn_cast <gcond *> (stmt))
6392 if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
6393 ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
6395 if (dump_file && (dump_flags & TDF_DETAILS))
6397 fprintf (dump_file, "Removing unexecutable edge from ");
6398 print_gimple_stmt (dump_file, stmt, 0);
6400 if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
6401 == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
6402 gimple_cond_make_true (cond);
6403 else
6404 gimple_cond_make_false (cond);
6405 update_stmt (cond);
6406 el_todo |= TODO_cleanup_cfg;
6407 return;
6411 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
6412 bool was_noreturn = (is_gimple_call (stmt)
6413 && gimple_call_noreturn_p (stmt));
6414 tree vdef = gimple_vdef (stmt);
6415 tree vuse = gimple_vuse (stmt);
6417 /* If we didn't replace the whole stmt (or propagate the result
6418 into all uses), replace all uses on this stmt with their
6419 leaders. */
6420 bool modified = false;
6421 use_operand_p use_p;
6422 ssa_op_iter iter;
6423 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
6425 tree use = USE_FROM_PTR (use_p);
6426 /* ??? The call code above leaves stmt operands un-updated. */
6427 if (TREE_CODE (use) != SSA_NAME)
6428 continue;
6429 tree sprime;
6430 if (SSA_NAME_IS_DEFAULT_DEF (use))
6431 /* ??? For default defs BB shouldn't matter, but we have to
6432 solve the inconsistency between rpo eliminate and
6433 dom eliminate avail valueization first. */
6434 sprime = eliminate_avail (b, use);
6435 else
6436 /* Look for sth available at the definition block of the argument.
6437 This avoids inconsistencies between availability there which
6438 decides if the stmt can be removed and availability at the
6439 use site. The SSA property ensures that things available
6440 at the definition are also available at uses. */
6441 sprime = eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (use)), use);
6442 if (sprime && sprime != use
6443 && may_propagate_copy (use, sprime)
6444 /* We substitute into debug stmts to avoid excessive
6445 debug temporaries created by removed stmts, but we need
6446 to avoid doing so for inserted sprimes as we never want
6447 to create debug temporaries for them. */
6448 && (!inserted_exprs
6449 || TREE_CODE (sprime) != SSA_NAME
6450 || !is_gimple_debug (stmt)
6451 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
6453 propagate_value (use_p, sprime);
6454 modified = true;
6458 /* Fold the stmt if modified, this canonicalizes MEM_REFs we propagated
6459 into which is a requirement for the IPA devirt machinery. */
6460 gimple *old_stmt = stmt;
6461 if (modified)
6463 /* If a formerly non-invariant ADDR_EXPR is turned into an
6464 invariant one it was on a separate stmt. */
6465 if (gimple_assign_single_p (stmt)
6466 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
6467 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
6468 gimple_stmt_iterator prev = *gsi;
6469 gsi_prev (&prev);
6470 if (fold_stmt (gsi, follow_all_ssa_edges))
6472 /* fold_stmt may have created new stmts inbetween
6473 the previous stmt and the folded stmt. Mark
6474 all defs created there as varying to not confuse
6475 the SCCVN machinery as we're using that even during
6476 elimination. */
6477 if (gsi_end_p (prev))
6478 prev = gsi_start_bb (b);
6479 else
6480 gsi_next (&prev);
6481 if (gsi_stmt (prev) != gsi_stmt (*gsi))
6484 tree def;
6485 ssa_op_iter dit;
6486 FOR_EACH_SSA_TREE_OPERAND (def, gsi_stmt (prev),
6487 dit, SSA_OP_ALL_DEFS)
6488 /* As existing DEFs may move between stmts
6489 only process new ones. */
6490 if (! has_VN_INFO (def))
6492 vn_ssa_aux_t vn_info = VN_INFO (def);
6493 vn_info->valnum = def;
6494 vn_info->visited = true;
6496 if (gsi_stmt (prev) == gsi_stmt (*gsi))
6497 break;
6498 gsi_next (&prev);
6500 while (1);
6502 stmt = gsi_stmt (*gsi);
6503 /* In case we folded the stmt away schedule the NOP for removal. */
6504 if (gimple_nop_p (stmt))
6505 to_remove.safe_push (stmt);
6508 /* Visit indirect calls and turn them into direct calls if
6509 possible using the devirtualization machinery. Do this before
6510 checking for required EH/abnormal/noreturn cleanup as devird
6511 may expose more of those. */
6512 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
6514 tree fn = gimple_call_fn (call_stmt);
6515 if (fn
6516 && flag_devirtualize
6517 && virtual_method_call_p (fn))
6519 tree otr_type = obj_type_ref_class (fn);
6520 unsigned HOST_WIDE_INT otr_tok
6521 = tree_to_uhwi (OBJ_TYPE_REF_TOKEN (fn));
6522 tree instance;
6523 ipa_polymorphic_call_context context (current_function_decl,
6524 fn, stmt, &instance);
6525 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn),
6526 otr_type, stmt, NULL);
6527 bool final;
6528 vec <cgraph_node *> targets
6529 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
6530 otr_tok, context, &final);
6531 if (dump_file)
6532 dump_possible_polymorphic_call_targets (dump_file,
6533 obj_type_ref_class (fn),
6534 otr_tok, context);
6535 if (final && targets.length () <= 1 && dbg_cnt (devirt))
6537 tree fn;
6538 if (targets.length () == 1)
6539 fn = targets[0]->decl;
6540 else
6541 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
6542 if (dump_enabled_p ())
6544 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, stmt,
6545 "converting indirect call to "
6546 "function %s\n",
6547 lang_hooks.decl_printable_name (fn, 2));
6549 gimple_call_set_fndecl (call_stmt, fn);
6550 /* If changing the call to __builtin_unreachable
6551 or similar noreturn function, adjust gimple_call_fntype
6552 too. */
6553 if (gimple_call_noreturn_p (call_stmt)
6554 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
6555 && TYPE_ARG_TYPES (TREE_TYPE (fn))
6556 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
6557 == void_type_node))
6558 gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
6559 maybe_remove_unused_call_args (cfun, call_stmt);
6560 modified = true;
6565 if (modified)
6567 /* When changing a call into a noreturn call, cfg cleanup
6568 is needed to fix up the noreturn call. */
6569 if (!was_noreturn
6570 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
6571 to_fixup.safe_push (stmt);
6572 /* When changing a condition or switch into one we know what
6573 edge will be executed, schedule a cfg cleanup. */
6574 if ((gimple_code (stmt) == GIMPLE_COND
6575 && (gimple_cond_true_p (as_a <gcond *> (stmt))
6576 || gimple_cond_false_p (as_a <gcond *> (stmt))))
6577 || (gimple_code (stmt) == GIMPLE_SWITCH
6578 && TREE_CODE (gimple_switch_index
6579 (as_a <gswitch *> (stmt))) == INTEGER_CST))
6580 el_todo |= TODO_cleanup_cfg;
6581 /* If we removed EH side-effects from the statement, clean
6582 its EH information. */
6583 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
6585 bitmap_set_bit (need_eh_cleanup,
6586 gimple_bb (stmt)->index);
6587 if (dump_file && (dump_flags & TDF_DETAILS))
6588 fprintf (dump_file, " Removed EH side-effects.\n");
6590 /* Likewise for AB side-effects. */
6591 if (can_make_abnormal_goto
6592 && !stmt_can_make_abnormal_goto (stmt))
6594 bitmap_set_bit (need_ab_cleanup,
6595 gimple_bb (stmt)->index);
6596 if (dump_file && (dump_flags & TDF_DETAILS))
6597 fprintf (dump_file, " Removed AB side-effects.\n");
6599 update_stmt (stmt);
6600 /* In case the VDEF on the original stmt was released, value-number
6601 it to the VUSE. This is to make vuse_ssa_val able to skip
6602 released virtual operands. */
6603 if (vdef && SSA_NAME_IN_FREE_LIST (vdef))
6604 VN_INFO (vdef)->valnum = vuse;
6607 /* Make new values available - for fully redundant LHS we
6608 continue with the next stmt above and skip this. */
6609 def_operand_p defp;
6610 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
6611 eliminate_push_avail (b, DEF_FROM_PTR (defp));
6614 /* Perform elimination for the basic-block B during the domwalk. */
6616 edge
6617 eliminate_dom_walker::before_dom_children (basic_block b)
6619 /* Mark new bb. */
6620 avail_stack.safe_push (NULL_TREE);
6622 /* Skip unreachable blocks marked unreachable during the SCCVN domwalk. */
6623 if (!(b->flags & BB_EXECUTABLE))
6624 return NULL;
6626 vn_context_bb = b;
6628 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
6630 gphi *phi = gsi.phi ();
6631 tree res = PHI_RESULT (phi);
6633 if (virtual_operand_p (res))
6635 gsi_next (&gsi);
6636 continue;
6639 tree sprime = eliminate_avail (b, res);
6640 if (sprime
6641 && sprime != res)
6643 if (dump_file && (dump_flags & TDF_DETAILS))
6645 fprintf (dump_file, "Replaced redundant PHI node defining ");
6646 print_generic_expr (dump_file, res);
6647 fprintf (dump_file, " with ");
6648 print_generic_expr (dump_file, sprime);
6649 fprintf (dump_file, "\n");
6652 /* If we inserted this PHI node ourself, it's not an elimination. */
6653 if (! inserted_exprs
6654 || ! bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
6655 eliminations++;
6657 /* If we will propagate into all uses don't bother to do
6658 anything. */
6659 if (may_propagate_copy (res, sprime))
6661 /* Mark the PHI for removal. */
6662 to_remove.safe_push (phi);
6663 gsi_next (&gsi);
6664 continue;
6667 remove_phi_node (&gsi, false);
6669 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
6670 sprime = fold_convert (TREE_TYPE (res), sprime);
6671 gimple *stmt = gimple_build_assign (res, sprime);
6672 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
6673 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
6674 continue;
6677 eliminate_push_avail (b, res);
6678 gsi_next (&gsi);
6681 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
6682 !gsi_end_p (gsi);
6683 gsi_next (&gsi))
6684 eliminate_stmt (b, &gsi);
6686 /* Replace destination PHI arguments. */
6687 edge_iterator ei;
6688 edge e;
6689 FOR_EACH_EDGE (e, ei, b->succs)
6690 if (e->flags & EDGE_EXECUTABLE)
6691 for (gphi_iterator gsi = gsi_start_phis (e->dest);
6692 !gsi_end_p (gsi);
6693 gsi_next (&gsi))
6695 gphi *phi = gsi.phi ();
6696 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
6697 tree arg = USE_FROM_PTR (use_p);
6698 if (TREE_CODE (arg) != SSA_NAME
6699 || virtual_operand_p (arg))
6700 continue;
6701 tree sprime = eliminate_avail (b, arg);
6702 if (sprime && may_propagate_copy (arg, sprime))
6703 propagate_value (use_p, sprime);
6706 vn_context_bb = NULL;
6708 return NULL;
6711 /* Make no longer available leaders no longer available. */
6713 void
6714 eliminate_dom_walker::after_dom_children (basic_block)
6716 tree entry;
6717 while ((entry = avail_stack.pop ()) != NULL_TREE)
6719 tree valnum = VN_INFO (entry)->valnum;
6720 tree old = avail[SSA_NAME_VERSION (valnum)];
6721 if (old == entry)
6722 avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
6723 else
6724 avail[SSA_NAME_VERSION (valnum)] = entry;
6728 /* Remove queued stmts and perform delayed cleanups. */
6730 unsigned
6731 eliminate_dom_walker::eliminate_cleanup (bool region_p)
6733 statistics_counter_event (cfun, "Eliminated", eliminations);
6734 statistics_counter_event (cfun, "Insertions", insertions);
6736 /* We cannot remove stmts during BB walk, especially not release SSA
6737 names there as this confuses the VN machinery. The stmts ending
6738 up in to_remove are either stores or simple copies.
6739 Remove stmts in reverse order to make debug stmt creation possible. */
6740 while (!to_remove.is_empty ())
6742 bool do_release_defs = true;
6743 gimple *stmt = to_remove.pop ();
6745 /* When we are value-numbering a region we do not require exit PHIs to
6746 be present so we have to make sure to deal with uses outside of the
6747 region of stmts that we thought are eliminated.
6748 ??? Note we may be confused by uses in dead regions we didn't run
6749 elimination on. Rather than checking individual uses we accept
6750 dead copies to be generated here (gcc.c-torture/execute/20060905-1.c
6751 contains such example). */
6752 if (region_p)
6754 if (gphi *phi = dyn_cast <gphi *> (stmt))
6756 tree lhs = gimple_phi_result (phi);
6757 if (!has_zero_uses (lhs))
6759 if (dump_file && (dump_flags & TDF_DETAILS))
6760 fprintf (dump_file, "Keeping eliminated stmt live "
6761 "as copy because of out-of-region uses\n");
6762 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
6763 gimple *copy = gimple_build_assign (lhs, sprime);
6764 gimple_stmt_iterator gsi
6765 = gsi_after_labels (gimple_bb (stmt));
6766 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
6767 do_release_defs = false;
6770 else if (tree lhs = gimple_get_lhs (stmt))
6771 if (TREE_CODE (lhs) == SSA_NAME
6772 && !has_zero_uses (lhs))
6774 if (dump_file && (dump_flags & TDF_DETAILS))
6775 fprintf (dump_file, "Keeping eliminated stmt live "
6776 "as copy because of out-of-region uses\n");
6777 tree sprime = eliminate_avail (gimple_bb (stmt), lhs);
6778 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
6779 if (is_gimple_assign (stmt))
6781 gimple_assign_set_rhs_from_tree (&gsi, sprime);
6782 stmt = gsi_stmt (gsi);
6783 update_stmt (stmt);
6784 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
6785 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
6786 continue;
6788 else
6790 gimple *copy = gimple_build_assign (lhs, sprime);
6791 gsi_insert_before (&gsi, copy, GSI_SAME_STMT);
6792 do_release_defs = false;
6797 if (dump_file && (dump_flags & TDF_DETAILS))
6799 fprintf (dump_file, "Removing dead stmt ");
6800 print_gimple_stmt (dump_file, stmt, 0, TDF_NONE);
6803 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
6804 if (gimple_code (stmt) == GIMPLE_PHI)
6805 remove_phi_node (&gsi, do_release_defs);
6806 else
6808 basic_block bb = gimple_bb (stmt);
6809 unlink_stmt_vdef (stmt);
6810 if (gsi_remove (&gsi, true))
6811 bitmap_set_bit (need_eh_cleanup, bb->index);
6812 if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
6813 bitmap_set_bit (need_ab_cleanup, bb->index);
6814 if (do_release_defs)
6815 release_defs (stmt);
6818 /* Removing a stmt may expose a forwarder block. */
6819 el_todo |= TODO_cleanup_cfg;
6822 /* Fixup stmts that became noreturn calls. This may require splitting
6823 blocks and thus isn't possible during the dominator walk. Do this
6824 in reverse order so we don't inadvertedly remove a stmt we want to
6825 fixup by visiting a dominating now noreturn call first. */
6826 while (!to_fixup.is_empty ())
6828 gimple *stmt = to_fixup.pop ();
6830 if (dump_file && (dump_flags & TDF_DETAILS))
6832 fprintf (dump_file, "Fixing up noreturn call ");
6833 print_gimple_stmt (dump_file, stmt, 0);
6836 if (fixup_noreturn_call (stmt))
6837 el_todo |= TODO_cleanup_cfg;
6840 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
6841 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
6843 if (do_eh_cleanup)
6844 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
6846 if (do_ab_cleanup)
6847 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
6849 if (do_eh_cleanup || do_ab_cleanup)
6850 el_todo |= TODO_cleanup_cfg;
6852 return el_todo;
6855 /* Eliminate fully redundant computations. */
6857 unsigned
6858 eliminate_with_rpo_vn (bitmap inserted_exprs)
6860 eliminate_dom_walker walker (CDI_DOMINATORS, inserted_exprs);
6862 eliminate_dom_walker *saved_rpo_avail = rpo_avail;
6863 rpo_avail = &walker;
6864 walker.walk (cfun->cfg->x_entry_block_ptr);
6865 rpo_avail = saved_rpo_avail;
6867 return walker.eliminate_cleanup ();
6870 static unsigned
6871 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
6872 bool iterate, bool eliminate);
6874 void
6875 run_rpo_vn (vn_lookup_kind kind)
6877 default_vn_walk_kind = kind;
6878 do_rpo_vn (cfun, NULL, NULL, true, false);
6880 /* ??? Prune requirement of these. */
6881 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
6883 /* Initialize the value ids and prune out remaining VN_TOPs
6884 from dead code. */
6885 tree name;
6886 unsigned i;
6887 FOR_EACH_SSA_NAME (i, name, cfun)
6889 vn_ssa_aux_t info = VN_INFO (name);
6890 if (!info->visited
6891 || info->valnum == VN_TOP)
6892 info->valnum = name;
6893 if (info->valnum == name)
6894 info->value_id = get_next_value_id ();
6895 else if (is_gimple_min_invariant (info->valnum))
6896 info->value_id = get_or_alloc_constant_value_id (info->valnum);
6899 /* Propagate. */
6900 FOR_EACH_SSA_NAME (i, name, cfun)
6902 vn_ssa_aux_t info = VN_INFO (name);
6903 if (TREE_CODE (info->valnum) == SSA_NAME
6904 && info->valnum != name
6905 && info->value_id != VN_INFO (info->valnum)->value_id)
6906 info->value_id = VN_INFO (info->valnum)->value_id;
6909 set_hashtable_value_ids ();
6911 if (dump_file && (dump_flags & TDF_DETAILS))
6913 fprintf (dump_file, "Value numbers:\n");
6914 FOR_EACH_SSA_NAME (i, name, cfun)
6916 if (VN_INFO (name)->visited
6917 && SSA_VAL (name) != name)
6919 print_generic_expr (dump_file, name);
6920 fprintf (dump_file, " = ");
6921 print_generic_expr (dump_file, SSA_VAL (name));
6922 fprintf (dump_file, " (%04d)\n", VN_INFO (name)->value_id);
6928 /* Free VN associated data structures. */
6930 void
6931 free_rpo_vn (void)
6933 free_vn_table (valid_info);
6934 XDELETE (valid_info);
6935 obstack_free (&vn_tables_obstack, NULL);
6936 obstack_free (&vn_tables_insert_obstack, NULL);
6938 vn_ssa_aux_iterator_type it;
6939 vn_ssa_aux_t info;
6940 FOR_EACH_HASH_TABLE_ELEMENT (*vn_ssa_aux_hash, info, vn_ssa_aux_t, it)
6941 if (info->needs_insertion)
6942 release_ssa_name (info->name);
6943 obstack_free (&vn_ssa_aux_obstack, NULL);
6944 delete vn_ssa_aux_hash;
6946 delete constant_to_value_id;
6947 constant_to_value_id = NULL;
6950 /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */
6952 static tree
6953 vn_lookup_simplify_result (gimple_match_op *res_op)
6955 if (!res_op->code.is_tree_code ())
6956 return NULL_TREE;
6957 tree *ops = res_op->ops;
6958 unsigned int length = res_op->num_ops;
6959 if (res_op->code == CONSTRUCTOR
6960 /* ??? We're arriving here with SCCVNs view, decomposed CONSTRUCTOR
6961 and GIMPLEs / match-and-simplifies, CONSTRUCTOR as GENERIC tree. */
6962 && TREE_CODE (res_op->ops[0]) == CONSTRUCTOR)
6964 length = CONSTRUCTOR_NELTS (res_op->ops[0]);
6965 ops = XALLOCAVEC (tree, length);
6966 for (unsigned i = 0; i < length; ++i)
6967 ops[i] = CONSTRUCTOR_ELT (res_op->ops[0], i)->value;
6969 vn_nary_op_t vnresult = NULL;
6970 tree res = vn_nary_op_lookup_pieces (length, (tree_code) res_op->code,
6971 res_op->type, ops, &vnresult);
6972 /* If this is used from expression simplification make sure to
6973 return an available expression. */
6974 if (res && TREE_CODE (res) == SSA_NAME && mprts_hook && rpo_avail)
6975 res = rpo_avail->eliminate_avail (vn_context_bb, res);
6976 return res;
6979 /* Return a leader for OPs value that is valid at BB. */
6981 tree
6982 rpo_elim::eliminate_avail (basic_block bb, tree op)
6984 bool visited;
6985 tree valnum = SSA_VAL (op, &visited);
6986 /* If we didn't visit OP then it must be defined outside of the
6987 region we process and also dominate it. So it is available. */
6988 if (!visited)
6989 return op;
6990 if (TREE_CODE (valnum) == SSA_NAME)
6992 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
6993 return valnum;
6994 vn_avail *av = VN_INFO (valnum)->avail;
6995 if (!av)
6996 return NULL_TREE;
6997 if (av->location == bb->index)
6998 /* On tramp3d 90% of the cases are here. */
6999 return ssa_name (av->leader);
7002 basic_block abb = BASIC_BLOCK_FOR_FN (cfun, av->location);
7003 /* ??? During elimination we have to use availability at the
7004 definition site of a use we try to replace. This
7005 is required to not run into inconsistencies because
7006 of dominated_by_p_w_unex behavior and removing a definition
7007 while not replacing all uses.
7008 ??? We could try to consistently walk dominators
7009 ignoring non-executable regions. The nearest common
7010 dominator of bb and abb is where we can stop walking. We
7011 may also be able to "pre-compute" (bits of) the next immediate
7012 (non-)dominator during the RPO walk when marking edges as
7013 executable. */
7014 if (dominated_by_p_w_unex (bb, abb, true))
7016 tree leader = ssa_name (av->leader);
7017 /* Prevent eliminations that break loop-closed SSA. */
7018 if (loops_state_satisfies_p (LOOP_CLOSED_SSA)
7019 && ! SSA_NAME_IS_DEFAULT_DEF (leader)
7020 && ! flow_bb_inside_loop_p (gimple_bb (SSA_NAME_DEF_STMT
7021 (leader))->loop_father,
7022 bb))
7023 return NULL_TREE;
7024 if (dump_file && (dump_flags & TDF_DETAILS))
7026 print_generic_expr (dump_file, leader);
7027 fprintf (dump_file, " is available for ");
7028 print_generic_expr (dump_file, valnum);
7029 fprintf (dump_file, "\n");
7031 /* On tramp3d 99% of the _remaining_ cases succeed at
7032 the first enty. */
7033 return leader;
7035 /* ??? Can we somehow skip to the immediate dominator
7036 RPO index (bb_to_rpo)? Again, maybe not worth, on
7037 tramp3d the worst number of elements in the vector is 9. */
7038 av = av->next;
7040 while (av);
7042 else if (valnum != VN_TOP)
7043 /* valnum is is_gimple_min_invariant. */
7044 return valnum;
7045 return NULL_TREE;
7048 /* Make LEADER a leader for its value at BB. */
7050 void
7051 rpo_elim::eliminate_push_avail (basic_block bb, tree leader)
7053 tree valnum = VN_INFO (leader)->valnum;
7054 if (valnum == VN_TOP
7055 || is_gimple_min_invariant (valnum))
7056 return;
7057 if (dump_file && (dump_flags & TDF_DETAILS))
7059 fprintf (dump_file, "Making available beyond BB%d ", bb->index);
7060 print_generic_expr (dump_file, leader);
7061 fprintf (dump_file, " for value ");
7062 print_generic_expr (dump_file, valnum);
7063 fprintf (dump_file, "\n");
7065 vn_ssa_aux_t value = VN_INFO (valnum);
7066 vn_avail *av;
7067 if (m_avail_freelist)
7069 av = m_avail_freelist;
7070 m_avail_freelist = m_avail_freelist->next;
7072 else
7073 av = XOBNEW (&vn_ssa_aux_obstack, vn_avail);
7074 av->location = bb->index;
7075 av->leader = SSA_NAME_VERSION (leader);
7076 av->next = value->avail;
7077 av->next_undo = last_pushed_avail;
7078 last_pushed_avail = value;
7079 value->avail = av;
7082 /* Valueization hook for RPO VN plus required state. */
7084 tree
7085 rpo_vn_valueize (tree name)
7087 if (TREE_CODE (name) == SSA_NAME)
7089 vn_ssa_aux_t val = VN_INFO (name);
7090 if (val)
7092 tree tem = val->valnum;
7093 if (tem != VN_TOP && tem != name)
7095 if (TREE_CODE (tem) != SSA_NAME)
7096 return tem;
7097 /* For all values we only valueize to an available leader
7098 which means we can use SSA name info without restriction. */
7099 tem = rpo_avail->eliminate_avail (vn_context_bb, tem);
7100 if (tem)
7101 return tem;
7105 return name;
7108 /* Insert on PRED_E predicates derived from CODE OPS being true besides the
7109 inverted condition. */
7111 static void
7112 insert_related_predicates_on_edge (enum tree_code code, tree *ops, edge pred_e)
7114 switch (code)
7116 case LT_EXPR:
7117 /* a < b -> a {!,<}= b */
7118 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7119 ops, boolean_true_node, 0, pred_e);
7120 vn_nary_op_insert_pieces_predicated (2, LE_EXPR, boolean_type_node,
7121 ops, boolean_true_node, 0, pred_e);
7122 /* a < b -> ! a {>,=} b */
7123 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7124 ops, boolean_false_node, 0, pred_e);
7125 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7126 ops, boolean_false_node, 0, pred_e);
7127 break;
7128 case GT_EXPR:
7129 /* a > b -> a {!,>}= b */
7130 vn_nary_op_insert_pieces_predicated (2, NE_EXPR, boolean_type_node,
7131 ops, boolean_true_node, 0, pred_e);
7132 vn_nary_op_insert_pieces_predicated (2, GE_EXPR, boolean_type_node,
7133 ops, boolean_true_node, 0, pred_e);
7134 /* a > b -> ! a {<,=} b */
7135 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7136 ops, boolean_false_node, 0, pred_e);
7137 vn_nary_op_insert_pieces_predicated (2, EQ_EXPR, boolean_type_node,
7138 ops, boolean_false_node, 0, pred_e);
7139 break;
7140 case EQ_EXPR:
7141 /* a == b -> ! a {<,>} b */
7142 vn_nary_op_insert_pieces_predicated (2, LT_EXPR, boolean_type_node,
7143 ops, boolean_false_node, 0, pred_e);
7144 vn_nary_op_insert_pieces_predicated (2, GT_EXPR, boolean_type_node,
7145 ops, boolean_false_node, 0, pred_e);
7146 break;
7147 case LE_EXPR:
7148 case GE_EXPR:
7149 case NE_EXPR:
7150 /* Nothing besides inverted condition. */
7151 break;
7152 default:;
7156 /* Main stmt worker for RPO VN, process BB. */
7158 static unsigned
7159 process_bb (rpo_elim &avail, basic_block bb,
7160 bool bb_visited, bool iterate_phis, bool iterate, bool eliminate,
7161 bool do_region, bitmap exit_bbs, bool skip_phis)
7163 unsigned todo = 0;
7164 edge_iterator ei;
7165 edge e;
7167 vn_context_bb = bb;
7169 /* If we are in loop-closed SSA preserve this state. This is
7170 relevant when called on regions from outside of FRE/PRE. */
7171 bool lc_phi_nodes = false;
7172 if (!skip_phis
7173 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
7174 FOR_EACH_EDGE (e, ei, bb->preds)
7175 if (e->src->loop_father != e->dest->loop_father
7176 && flow_loop_nested_p (e->dest->loop_father,
7177 e->src->loop_father))
7179 lc_phi_nodes = true;
7180 break;
7183 /* When we visit a loop header substitute into loop info. */
7184 if (!iterate && eliminate && bb->loop_father->header == bb)
7186 /* Keep fields in sync with substitute_in_loop_info. */
7187 if (bb->loop_father->nb_iterations)
7188 bb->loop_father->nb_iterations
7189 = simplify_replace_tree (bb->loop_father->nb_iterations,
7190 NULL_TREE, NULL_TREE, &vn_valueize_for_srt);
7193 /* Value-number all defs in the basic-block. */
7194 if (!skip_phis)
7195 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7196 gsi_next (&gsi))
7198 gphi *phi = gsi.phi ();
7199 tree res = PHI_RESULT (phi);
7200 vn_ssa_aux_t res_info = VN_INFO (res);
7201 if (!bb_visited)
7203 gcc_assert (!res_info->visited);
7204 res_info->valnum = VN_TOP;
7205 res_info->visited = true;
7208 /* When not iterating force backedge values to varying. */
7209 visit_stmt (phi, !iterate_phis);
7210 if (virtual_operand_p (res))
7211 continue;
7213 /* Eliminate */
7214 /* The interesting case is gcc.dg/tree-ssa/pr22230.c for correctness
7215 how we handle backedges and availability.
7216 And gcc.dg/tree-ssa/ssa-sccvn-2.c for optimization. */
7217 tree val = res_info->valnum;
7218 if (res != val && !iterate && eliminate)
7220 if (tree leader = avail.eliminate_avail (bb, res))
7222 if (leader != res
7223 /* Preserve loop-closed SSA form. */
7224 && (! lc_phi_nodes
7225 || is_gimple_min_invariant (leader)))
7227 if (dump_file && (dump_flags & TDF_DETAILS))
7229 fprintf (dump_file, "Replaced redundant PHI node "
7230 "defining ");
7231 print_generic_expr (dump_file, res);
7232 fprintf (dump_file, " with ");
7233 print_generic_expr (dump_file, leader);
7234 fprintf (dump_file, "\n");
7236 avail.eliminations++;
7238 if (may_propagate_copy (res, leader))
7240 /* Schedule for removal. */
7241 avail.to_remove.safe_push (phi);
7242 continue;
7244 /* ??? Else generate a copy stmt. */
7248 /* Only make defs available that not already are. But make
7249 sure loop-closed SSA PHI node defs are picked up for
7250 downstream uses. */
7251 if (lc_phi_nodes
7252 || res == val
7253 || ! avail.eliminate_avail (bb, res))
7254 avail.eliminate_push_avail (bb, res);
7257 /* For empty BBs mark outgoing edges executable. For non-empty BBs
7258 we do this when processing the last stmt as we have to do this
7259 before elimination which otherwise forces GIMPLE_CONDs to
7260 if (1 != 0) style when seeing non-executable edges. */
7261 if (gsi_end_p (gsi_start_bb (bb)))
7263 FOR_EACH_EDGE (e, ei, bb->succs)
7265 if (!(e->flags & EDGE_EXECUTABLE))
7267 if (dump_file && (dump_flags & TDF_DETAILS))
7268 fprintf (dump_file,
7269 "marking outgoing edge %d -> %d executable\n",
7270 e->src->index, e->dest->index);
7271 e->flags |= EDGE_EXECUTABLE;
7272 e->dest->flags |= BB_EXECUTABLE;
7274 else if (!(e->dest->flags & BB_EXECUTABLE))
7276 if (dump_file && (dump_flags & TDF_DETAILS))
7277 fprintf (dump_file,
7278 "marking destination block %d reachable\n",
7279 e->dest->index);
7280 e->dest->flags |= BB_EXECUTABLE;
7284 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7285 !gsi_end_p (gsi); gsi_next (&gsi))
7287 ssa_op_iter i;
7288 tree op;
7289 if (!bb_visited)
7291 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS)
7293 vn_ssa_aux_t op_info = VN_INFO (op);
7294 gcc_assert (!op_info->visited);
7295 op_info->valnum = VN_TOP;
7296 op_info->visited = true;
7299 /* We somehow have to deal with uses that are not defined
7300 in the processed region. Forcing unvisited uses to
7301 varying here doesn't play well with def-use following during
7302 expression simplification, so we deal with this by checking
7303 the visited flag in SSA_VAL. */
7306 visit_stmt (gsi_stmt (gsi));
7308 gimple *last = gsi_stmt (gsi);
7309 e = NULL;
7310 switch (gimple_code (last))
7312 case GIMPLE_SWITCH:
7313 e = find_taken_edge (bb, vn_valueize (gimple_switch_index
7314 (as_a <gswitch *> (last))));
7315 break;
7316 case GIMPLE_COND:
7318 tree lhs = vn_valueize (gimple_cond_lhs (last));
7319 tree rhs = vn_valueize (gimple_cond_rhs (last));
7320 tree val = gimple_simplify (gimple_cond_code (last),
7321 boolean_type_node, lhs, rhs,
7322 NULL, vn_valueize);
7323 /* If the condition didn't simplfy see if we have recorded
7324 an expression from sofar taken edges. */
7325 if (! val || TREE_CODE (val) != INTEGER_CST)
7327 vn_nary_op_t vnresult;
7328 tree ops[2];
7329 ops[0] = lhs;
7330 ops[1] = rhs;
7331 val = vn_nary_op_lookup_pieces (2, gimple_cond_code (last),
7332 boolean_type_node, ops,
7333 &vnresult);
7334 /* Did we get a predicated value? */
7335 if (! val && vnresult && vnresult->predicated_values)
7337 val = vn_nary_op_get_predicated_value (vnresult, bb);
7338 if (val && dump_file && (dump_flags & TDF_DETAILS))
7340 fprintf (dump_file, "Got predicated value ");
7341 print_generic_expr (dump_file, val, TDF_NONE);
7342 fprintf (dump_file, " for ");
7343 print_gimple_stmt (dump_file, last, TDF_SLIM);
7347 if (val)
7348 e = find_taken_edge (bb, val);
7349 if (! e)
7351 /* If we didn't manage to compute the taken edge then
7352 push predicated expressions for the condition itself
7353 and related conditions to the hashtables. This allows
7354 simplification of redundant conditions which is
7355 important as early cleanup. */
7356 edge true_e, false_e;
7357 extract_true_false_edges_from_block (bb, &true_e, &false_e);
7358 enum tree_code code = gimple_cond_code (last);
7359 enum tree_code icode
7360 = invert_tree_comparison (code, HONOR_NANS (lhs));
7361 tree ops[2];
7362 ops[0] = lhs;
7363 ops[1] = rhs;
7364 if (do_region
7365 && bitmap_bit_p (exit_bbs, true_e->dest->index))
7366 true_e = NULL;
7367 if (do_region
7368 && bitmap_bit_p (exit_bbs, false_e->dest->index))
7369 false_e = NULL;
7370 if (true_e)
7371 vn_nary_op_insert_pieces_predicated
7372 (2, code, boolean_type_node, ops,
7373 boolean_true_node, 0, true_e);
7374 if (false_e)
7375 vn_nary_op_insert_pieces_predicated
7376 (2, code, boolean_type_node, ops,
7377 boolean_false_node, 0, false_e);
7378 if (icode != ERROR_MARK)
7380 if (true_e)
7381 vn_nary_op_insert_pieces_predicated
7382 (2, icode, boolean_type_node, ops,
7383 boolean_false_node, 0, true_e);
7384 if (false_e)
7385 vn_nary_op_insert_pieces_predicated
7386 (2, icode, boolean_type_node, ops,
7387 boolean_true_node, 0, false_e);
7389 /* Relax for non-integers, inverted condition handled
7390 above. */
7391 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
7393 if (true_e)
7394 insert_related_predicates_on_edge (code, ops, true_e);
7395 if (false_e)
7396 insert_related_predicates_on_edge (icode, ops, false_e);
7399 break;
7401 case GIMPLE_GOTO:
7402 e = find_taken_edge (bb, vn_valueize (gimple_goto_dest (last)));
7403 break;
7404 default:
7405 e = NULL;
7407 if (e)
7409 todo = TODO_cleanup_cfg;
7410 if (!(e->flags & EDGE_EXECUTABLE))
7412 if (dump_file && (dump_flags & TDF_DETAILS))
7413 fprintf (dump_file,
7414 "marking known outgoing %sedge %d -> %d executable\n",
7415 e->flags & EDGE_DFS_BACK ? "back-" : "",
7416 e->src->index, e->dest->index);
7417 e->flags |= EDGE_EXECUTABLE;
7418 e->dest->flags |= BB_EXECUTABLE;
7420 else if (!(e->dest->flags & BB_EXECUTABLE))
7422 if (dump_file && (dump_flags & TDF_DETAILS))
7423 fprintf (dump_file,
7424 "marking destination block %d reachable\n",
7425 e->dest->index);
7426 e->dest->flags |= BB_EXECUTABLE;
7429 else if (gsi_one_before_end_p (gsi))
7431 FOR_EACH_EDGE (e, ei, bb->succs)
7433 if (!(e->flags & EDGE_EXECUTABLE))
7435 if (dump_file && (dump_flags & TDF_DETAILS))
7436 fprintf (dump_file,
7437 "marking outgoing edge %d -> %d executable\n",
7438 e->src->index, e->dest->index);
7439 e->flags |= EDGE_EXECUTABLE;
7440 e->dest->flags |= BB_EXECUTABLE;
7442 else if (!(e->dest->flags & BB_EXECUTABLE))
7444 if (dump_file && (dump_flags & TDF_DETAILS))
7445 fprintf (dump_file,
7446 "marking destination block %d reachable\n",
7447 e->dest->index);
7448 e->dest->flags |= BB_EXECUTABLE;
7453 /* Eliminate. That also pushes to avail. */
7454 if (eliminate && ! iterate)
7455 avail.eliminate_stmt (bb, &gsi);
7456 else
7457 /* If not eliminating, make all not already available defs
7458 available. */
7459 FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_DEF)
7460 if (! avail.eliminate_avail (bb, op))
7461 avail.eliminate_push_avail (bb, op);
7464 /* Eliminate in destination PHI arguments. Always substitute in dest
7465 PHIs, even for non-executable edges. This handles region
7466 exits PHIs. */
7467 if (!iterate && eliminate)
7468 FOR_EACH_EDGE (e, ei, bb->succs)
7469 for (gphi_iterator gsi = gsi_start_phis (e->dest);
7470 !gsi_end_p (gsi); gsi_next (&gsi))
7472 gphi *phi = gsi.phi ();
7473 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
7474 tree arg = USE_FROM_PTR (use_p);
7475 if (TREE_CODE (arg) != SSA_NAME
7476 || virtual_operand_p (arg))
7477 continue;
7478 tree sprime;
7479 if (SSA_NAME_IS_DEFAULT_DEF (arg))
7481 sprime = SSA_VAL (arg);
7482 gcc_assert (TREE_CODE (sprime) != SSA_NAME
7483 || SSA_NAME_IS_DEFAULT_DEF (sprime));
7485 else
7486 /* Look for sth available at the definition block of the argument.
7487 This avoids inconsistencies between availability there which
7488 decides if the stmt can be removed and availability at the
7489 use site. The SSA property ensures that things available
7490 at the definition are also available at uses. */
7491 sprime = avail.eliminate_avail (gimple_bb (SSA_NAME_DEF_STMT (arg)),
7492 arg);
7493 if (sprime
7494 && sprime != arg
7495 && may_propagate_copy (arg, sprime))
7496 propagate_value (use_p, sprime);
7499 vn_context_bb = NULL;
7500 return todo;
7503 /* Unwind state per basic-block. */
7505 struct unwind_state
7507 /* Times this block has been visited. */
7508 unsigned visited;
7509 /* Whether to handle this as iteration point or whether to treat
7510 incoming backedge PHI values as varying. */
7511 bool iterate;
7512 /* Maximum RPO index this block is reachable from. */
7513 int max_rpo;
7514 /* Unwind state. */
7515 void *ob_top;
7516 vn_reference_t ref_top;
7517 vn_phi_t phi_top;
7518 vn_nary_op_t nary_top;
7519 vn_avail *avail_top;
7522 /* Unwind the RPO VN state for iteration. */
7524 static void
7525 do_unwind (unwind_state *to, rpo_elim &avail)
7527 gcc_assert (to->iterate);
7528 for (; last_inserted_nary != to->nary_top;
7529 last_inserted_nary = last_inserted_nary->next)
7531 vn_nary_op_t *slot;
7532 slot = valid_info->nary->find_slot_with_hash
7533 (last_inserted_nary, last_inserted_nary->hashcode, NO_INSERT);
7534 /* Predication causes the need to restore previous state. */
7535 if ((*slot)->unwind_to)
7536 *slot = (*slot)->unwind_to;
7537 else
7538 valid_info->nary->clear_slot (slot);
7540 for (; last_inserted_phi != to->phi_top;
7541 last_inserted_phi = last_inserted_phi->next)
7543 vn_phi_t *slot;
7544 slot = valid_info->phis->find_slot_with_hash
7545 (last_inserted_phi, last_inserted_phi->hashcode, NO_INSERT);
7546 valid_info->phis->clear_slot (slot);
7548 for (; last_inserted_ref != to->ref_top;
7549 last_inserted_ref = last_inserted_ref->next)
7551 vn_reference_t *slot;
7552 slot = valid_info->references->find_slot_with_hash
7553 (last_inserted_ref, last_inserted_ref->hashcode, NO_INSERT);
7554 (*slot)->operands.release ();
7555 valid_info->references->clear_slot (slot);
7557 obstack_free (&vn_tables_obstack, to->ob_top);
7559 /* Prune [rpo_idx, ] from avail. */
7560 for (; last_pushed_avail && last_pushed_avail->avail != to->avail_top;)
7562 vn_ssa_aux_t val = last_pushed_avail;
7563 vn_avail *av = val->avail;
7564 val->avail = av->next;
7565 last_pushed_avail = av->next_undo;
7566 av->next = avail.m_avail_freelist;
7567 avail.m_avail_freelist = av;
7571 /* Do VN on a SEME region specified by ENTRY and EXIT_BBS in FN.
7572 If ITERATE is true then treat backedges optimistically as not
7573 executed and iterate. If ELIMINATE is true then perform
7574 elimination, otherwise leave that to the caller. */
7576 static unsigned
7577 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs,
7578 bool iterate, bool eliminate)
7580 unsigned todo = 0;
7582 /* We currently do not support region-based iteration when
7583 elimination is requested. */
7584 gcc_assert (!entry || !iterate || !eliminate);
7585 /* When iterating we need loop info up-to-date. */
7586 gcc_assert (!iterate || !loops_state_satisfies_p (LOOPS_NEED_FIXUP));
7588 bool do_region = entry != NULL;
7589 if (!do_region)
7591 entry = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (fn));
7592 exit_bbs = BITMAP_ALLOC (NULL);
7593 bitmap_set_bit (exit_bbs, EXIT_BLOCK);
7596 /* Clear EDGE_DFS_BACK on "all" entry edges, RPO order compute will
7597 re-mark those that are contained in the region. */
7598 edge_iterator ei;
7599 edge e;
7600 FOR_EACH_EDGE (e, ei, entry->dest->preds)
7601 e->flags &= ~EDGE_DFS_BACK;
7603 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS);
7604 auto_vec<std::pair<int, int> > toplevel_scc_extents;
7605 int n = rev_post_order_and_mark_dfs_back_seme
7606 (fn, entry, exit_bbs, true, rpo, !iterate ? &toplevel_scc_extents : NULL);
7608 if (!do_region)
7609 BITMAP_FREE (exit_bbs);
7611 /* If there are any non-DFS_BACK edges into entry->dest skip
7612 processing PHI nodes for that block. This supports
7613 value-numbering loop bodies w/o the actual loop. */
7614 FOR_EACH_EDGE (e, ei, entry->dest->preds)
7615 if (e != entry
7616 && !(e->flags & EDGE_DFS_BACK))
7617 break;
7618 bool skip_entry_phis = e != NULL;
7619 if (skip_entry_phis && dump_file && (dump_flags & TDF_DETAILS))
7620 fprintf (dump_file, "Region does not contain all edges into "
7621 "the entry block, skipping its PHIs.\n");
7623 int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fn));
7624 for (int i = 0; i < n; ++i)
7625 bb_to_rpo[rpo[i]] = i;
7627 unwind_state *rpo_state = XNEWVEC (unwind_state, n);
7629 rpo_elim avail (entry->dest);
7630 rpo_avail = &avail;
7632 /* Verify we have no extra entries into the region. */
7633 if (flag_checking && do_region)
7635 auto_bb_flag bb_in_region (fn);
7636 for (int i = 0; i < n; ++i)
7638 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
7639 bb->flags |= bb_in_region;
7641 /* We can't merge the first two loops because we cannot rely
7642 on EDGE_DFS_BACK for edges not within the region. But if
7643 we decide to always have the bb_in_region flag we can
7644 do the checking during the RPO walk itself (but then it's
7645 also easy to handle MEME conservatively). */
7646 for (int i = 0; i < n; ++i)
7648 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
7649 edge e;
7650 edge_iterator ei;
7651 FOR_EACH_EDGE (e, ei, bb->preds)
7652 gcc_assert (e == entry
7653 || (skip_entry_phis && bb == entry->dest)
7654 || (e->src->flags & bb_in_region));
7656 for (int i = 0; i < n; ++i)
7658 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
7659 bb->flags &= ~bb_in_region;
7663 /* Create the VN state. For the initial size of the various hashtables
7664 use a heuristic based on region size and number of SSA names. */
7665 unsigned region_size = (((unsigned HOST_WIDE_INT)n * num_ssa_names)
7666 / (n_basic_blocks_for_fn (fn) - NUM_FIXED_BLOCKS));
7667 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
7668 next_value_id = 1;
7669 next_constant_value_id = -1;
7671 vn_ssa_aux_hash = new hash_table <vn_ssa_aux_hasher> (region_size * 2);
7672 gcc_obstack_init (&vn_ssa_aux_obstack);
7674 gcc_obstack_init (&vn_tables_obstack);
7675 gcc_obstack_init (&vn_tables_insert_obstack);
7676 valid_info = XCNEW (struct vn_tables_s);
7677 allocate_vn_table (valid_info, region_size);
7678 last_inserted_ref = NULL;
7679 last_inserted_phi = NULL;
7680 last_inserted_nary = NULL;
7681 last_pushed_avail = NULL;
7683 vn_valueize = rpo_vn_valueize;
7685 /* Initialize the unwind state and edge/BB executable state. */
7686 unsigned curr_scc = 0;
7687 for (int i = 0; i < n; ++i)
7689 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
7690 rpo_state[i].visited = 0;
7691 rpo_state[i].max_rpo = i;
7692 if (!iterate && curr_scc < toplevel_scc_extents.length ())
7694 if (i >= toplevel_scc_extents[curr_scc].first
7695 && i <= toplevel_scc_extents[curr_scc].second)
7696 rpo_state[i].max_rpo = toplevel_scc_extents[curr_scc].second;
7697 if (i == toplevel_scc_extents[curr_scc].second)
7698 curr_scc++;
7700 bb->flags &= ~BB_EXECUTABLE;
7701 bool has_backedges = false;
7702 edge e;
7703 edge_iterator ei;
7704 FOR_EACH_EDGE (e, ei, bb->preds)
7706 if (e->flags & EDGE_DFS_BACK)
7707 has_backedges = true;
7708 e->flags &= ~EDGE_EXECUTABLE;
7709 if (iterate || e == entry || (skip_entry_phis && bb == entry->dest))
7710 continue;
7712 rpo_state[i].iterate = iterate && has_backedges;
7714 entry->flags |= EDGE_EXECUTABLE;
7715 entry->dest->flags |= BB_EXECUTABLE;
7717 /* As heuristic to improve compile-time we handle only the N innermost
7718 loops and the outermost one optimistically. */
7719 if (iterate)
7721 unsigned max_depth = param_rpo_vn_max_loop_depth;
7722 for (auto loop : loops_list (cfun, LI_ONLY_INNERMOST))
7723 if (loop_depth (loop) > max_depth)
7724 for (unsigned i = 2;
7725 i < loop_depth (loop) - max_depth; ++i)
7727 basic_block header = superloop_at_depth (loop, i)->header;
7728 bool non_latch_backedge = false;
7729 edge e;
7730 edge_iterator ei;
7731 FOR_EACH_EDGE (e, ei, header->preds)
7732 if (e->flags & EDGE_DFS_BACK)
7734 /* There can be a non-latch backedge into the header
7735 which is part of an outer irreducible region. We
7736 cannot avoid iterating this block then. */
7737 if (!dominated_by_p (CDI_DOMINATORS,
7738 e->src, e->dest))
7740 if (dump_file && (dump_flags & TDF_DETAILS))
7741 fprintf (dump_file, "non-latch backedge %d -> %d "
7742 "forces iteration of loop %d\n",
7743 e->src->index, e->dest->index, loop->num);
7744 non_latch_backedge = true;
7746 else
7747 e->flags |= EDGE_EXECUTABLE;
7749 rpo_state[bb_to_rpo[header->index]].iterate = non_latch_backedge;
7753 uint64_t nblk = 0;
7754 int idx = 0;
7755 if (iterate)
7756 /* Go and process all blocks, iterating as necessary. */
7759 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
7761 /* If the block has incoming backedges remember unwind state. This
7762 is required even for non-executable blocks since in irreducible
7763 regions we might reach them via the backedge and re-start iterating
7764 from there.
7765 Note we can individually mark blocks with incoming backedges to
7766 not iterate where we then handle PHIs conservatively. We do that
7767 heuristically to reduce compile-time for degenerate cases. */
7768 if (rpo_state[idx].iterate)
7770 rpo_state[idx].ob_top = obstack_alloc (&vn_tables_obstack, 0);
7771 rpo_state[idx].ref_top = last_inserted_ref;
7772 rpo_state[idx].phi_top = last_inserted_phi;
7773 rpo_state[idx].nary_top = last_inserted_nary;
7774 rpo_state[idx].avail_top
7775 = last_pushed_avail ? last_pushed_avail->avail : NULL;
7778 if (!(bb->flags & BB_EXECUTABLE))
7780 if (dump_file && (dump_flags & TDF_DETAILS))
7781 fprintf (dump_file, "Block %d: BB%d found not executable\n",
7782 idx, bb->index);
7783 idx++;
7784 continue;
7787 if (dump_file && (dump_flags & TDF_DETAILS))
7788 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
7789 nblk++;
7790 todo |= process_bb (avail, bb,
7791 rpo_state[idx].visited != 0,
7792 rpo_state[idx].iterate,
7793 iterate, eliminate, do_region, exit_bbs, false);
7794 rpo_state[idx].visited++;
7796 /* Verify if changed values flow over executable outgoing backedges
7797 and those change destination PHI values (that's the thing we
7798 can easily verify). Reduce over all such edges to the farthest
7799 away PHI. */
7800 int iterate_to = -1;
7801 edge_iterator ei;
7802 edge e;
7803 FOR_EACH_EDGE (e, ei, bb->succs)
7804 if ((e->flags & (EDGE_DFS_BACK|EDGE_EXECUTABLE))
7805 == (EDGE_DFS_BACK|EDGE_EXECUTABLE)
7806 && rpo_state[bb_to_rpo[e->dest->index]].iterate)
7808 int destidx = bb_to_rpo[e->dest->index];
7809 if (!rpo_state[destidx].visited)
7811 if (dump_file && (dump_flags & TDF_DETAILS))
7812 fprintf (dump_file, "Unvisited destination %d\n",
7813 e->dest->index);
7814 if (iterate_to == -1 || destidx < iterate_to)
7815 iterate_to = destidx;
7816 continue;
7818 if (dump_file && (dump_flags & TDF_DETAILS))
7819 fprintf (dump_file, "Looking for changed values of backedge"
7820 " %d->%d destination PHIs\n",
7821 e->src->index, e->dest->index);
7822 vn_context_bb = e->dest;
7823 gphi_iterator gsi;
7824 for (gsi = gsi_start_phis (e->dest);
7825 !gsi_end_p (gsi); gsi_next (&gsi))
7827 bool inserted = false;
7828 /* While we'd ideally just iterate on value changes
7829 we CSE PHIs and do that even across basic-block
7830 boundaries. So even hashtable state changes can
7831 be important (which is roughly equivalent to
7832 PHI argument value changes). To not excessively
7833 iterate because of that we track whether a PHI
7834 was CSEd to with GF_PLF_1. */
7835 bool phival_changed;
7836 if ((phival_changed = visit_phi (gsi.phi (),
7837 &inserted, false))
7838 || (inserted && gimple_plf (gsi.phi (), GF_PLF_1)))
7840 if (!phival_changed
7841 && dump_file && (dump_flags & TDF_DETAILS))
7842 fprintf (dump_file, "PHI was CSEd and hashtable "
7843 "state (changed)\n");
7844 if (iterate_to == -1 || destidx < iterate_to)
7845 iterate_to = destidx;
7846 break;
7849 vn_context_bb = NULL;
7851 if (iterate_to != -1)
7853 do_unwind (&rpo_state[iterate_to], avail);
7854 idx = iterate_to;
7855 if (dump_file && (dump_flags & TDF_DETAILS))
7856 fprintf (dump_file, "Iterating to %d BB%d\n",
7857 iterate_to, rpo[iterate_to]);
7858 continue;
7861 idx++;
7863 while (idx < n);
7865 else /* !iterate */
7867 /* Process all blocks greedily with a worklist that enforces RPO
7868 processing of reachable blocks. */
7869 auto_bitmap worklist;
7870 bitmap_set_bit (worklist, 0);
7871 while (!bitmap_empty_p (worklist))
7873 int idx = bitmap_first_set_bit (worklist);
7874 bitmap_clear_bit (worklist, idx);
7875 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[idx]);
7876 gcc_assert ((bb->flags & BB_EXECUTABLE)
7877 && !rpo_state[idx].visited);
7879 if (dump_file && (dump_flags & TDF_DETAILS))
7880 fprintf (dump_file, "Processing block %d: BB%d\n", idx, bb->index);
7882 /* When we run into predecessor edges where we cannot trust its
7883 executable state mark them executable so PHI processing will
7884 be conservative.
7885 ??? Do we need to force arguments flowing over that edge
7886 to be varying or will they even always be? */
7887 edge_iterator ei;
7888 edge e;
7889 FOR_EACH_EDGE (e, ei, bb->preds)
7890 if (!(e->flags & EDGE_EXECUTABLE)
7891 && (bb == entry->dest
7892 || (!rpo_state[bb_to_rpo[e->src->index]].visited
7893 && (rpo_state[bb_to_rpo[e->src->index]].max_rpo
7894 >= (int)idx))))
7896 if (dump_file && (dump_flags & TDF_DETAILS))
7897 fprintf (dump_file, "Cannot trust state of predecessor "
7898 "edge %d -> %d, marking executable\n",
7899 e->src->index, e->dest->index);
7900 e->flags |= EDGE_EXECUTABLE;
7903 nblk++;
7904 todo |= process_bb (avail, bb, false, false, false, eliminate,
7905 do_region, exit_bbs,
7906 skip_entry_phis && bb == entry->dest);
7907 rpo_state[idx].visited++;
7909 FOR_EACH_EDGE (e, ei, bb->succs)
7910 if ((e->flags & EDGE_EXECUTABLE)
7911 && e->dest->index != EXIT_BLOCK
7912 && (!do_region || !bitmap_bit_p (exit_bbs, e->dest->index))
7913 && !rpo_state[bb_to_rpo[e->dest->index]].visited)
7914 bitmap_set_bit (worklist, bb_to_rpo[e->dest->index]);
7918 /* If statistics or dump file active. */
7919 int nex = 0;
7920 unsigned max_visited = 1;
7921 for (int i = 0; i < n; ++i)
7923 basic_block bb = BASIC_BLOCK_FOR_FN (fn, rpo[i]);
7924 if (bb->flags & BB_EXECUTABLE)
7925 nex++;
7926 statistics_histogram_event (cfun, "RPO block visited times",
7927 rpo_state[i].visited);
7928 if (rpo_state[i].visited > max_visited)
7929 max_visited = rpo_state[i].visited;
7931 unsigned nvalues = 0, navail = 0;
7932 for (hash_table<vn_ssa_aux_hasher>::iterator i = vn_ssa_aux_hash->begin ();
7933 i != vn_ssa_aux_hash->end (); ++i)
7935 nvalues++;
7936 vn_avail *av = (*i)->avail;
7937 while (av)
7939 navail++;
7940 av = av->next;
7943 statistics_counter_event (cfun, "RPO blocks", n);
7944 statistics_counter_event (cfun, "RPO blocks visited", nblk);
7945 statistics_counter_event (cfun, "RPO blocks executable", nex);
7946 statistics_histogram_event (cfun, "RPO iterations", 10*nblk / nex);
7947 statistics_histogram_event (cfun, "RPO num values", nvalues);
7948 statistics_histogram_event (cfun, "RPO num avail", navail);
7949 statistics_histogram_event (cfun, "RPO num lattice",
7950 vn_ssa_aux_hash->elements ());
7951 if (dump_file && (dump_flags & (TDF_DETAILS|TDF_STATS)))
7953 fprintf (dump_file, "RPO iteration over %d blocks visited %" PRIu64
7954 " blocks in total discovering %d executable blocks iterating "
7955 "%d.%d times, a block was visited max. %u times\n",
7956 n, nblk, nex,
7957 (int)((10*nblk / nex)/10), (int)((10*nblk / nex)%10),
7958 max_visited);
7959 fprintf (dump_file, "RPO tracked %d values available at %d locations "
7960 "and %" PRIu64 " lattice elements\n",
7961 nvalues, navail, (uint64_t) vn_ssa_aux_hash->elements ());
7964 if (eliminate)
7966 /* When !iterate we already performed elimination during the RPO
7967 walk. */
7968 if (iterate)
7970 /* Elimination for region-based VN needs to be done within the
7971 RPO walk. */
7972 gcc_assert (! do_region);
7973 /* Note we can't use avail.walk here because that gets confused
7974 by the existing availability and it will be less efficient
7975 as well. */
7976 todo |= eliminate_with_rpo_vn (NULL);
7978 else
7979 todo |= avail.eliminate_cleanup (do_region);
7982 vn_valueize = NULL;
7983 rpo_avail = NULL;
7985 XDELETEVEC (bb_to_rpo);
7986 XDELETEVEC (rpo);
7987 XDELETEVEC (rpo_state);
7989 return todo;
7992 /* Region-based entry for RPO VN. Performs value-numbering and elimination
7993 on the SEME region specified by ENTRY and EXIT_BBS. If ENTRY is not
7994 the only edge into the region at ENTRY->dest PHI nodes in ENTRY->dest
7995 are not considered. */
7997 unsigned
7998 do_rpo_vn (function *fn, edge entry, bitmap exit_bbs)
8000 default_vn_walk_kind = VN_WALKREWRITE;
8001 unsigned todo = do_rpo_vn (fn, entry, exit_bbs, false, true);
8002 free_rpo_vn ();
8003 return todo;
8007 namespace {
8009 const pass_data pass_data_fre =
8011 GIMPLE_PASS, /* type */
8012 "fre", /* name */
8013 OPTGROUP_NONE, /* optinfo_flags */
8014 TV_TREE_FRE, /* tv_id */
8015 ( PROP_cfg | PROP_ssa ), /* properties_required */
8016 0, /* properties_provided */
8017 0, /* properties_destroyed */
8018 0, /* todo_flags_start */
8019 0, /* todo_flags_finish */
8022 class pass_fre : public gimple_opt_pass
8024 public:
8025 pass_fre (gcc::context *ctxt)
8026 : gimple_opt_pass (pass_data_fre, ctxt), may_iterate (true)
8029 /* opt_pass methods: */
8030 opt_pass * clone () { return new pass_fre (m_ctxt); }
8031 void set_pass_param (unsigned int n, bool param)
8033 gcc_assert (n == 0);
8034 may_iterate = param;
8036 virtual bool gate (function *)
8038 return flag_tree_fre != 0 && (may_iterate || optimize > 1);
8040 virtual unsigned int execute (function *);
8042 private:
8043 bool may_iterate;
8044 }; // class pass_fre
8046 unsigned int
8047 pass_fre::execute (function *fun)
8049 unsigned todo = 0;
8051 /* At -O[1g] use the cheap non-iterating mode. */
8052 bool iterate_p = may_iterate && (optimize > 1);
8053 calculate_dominance_info (CDI_DOMINATORS);
8054 if (iterate_p)
8055 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
8057 default_vn_walk_kind = VN_WALKREWRITE;
8058 todo = do_rpo_vn (fun, NULL, NULL, iterate_p, true);
8059 free_rpo_vn ();
8061 if (iterate_p)
8062 loop_optimizer_finalize ();
8064 if (scev_initialized_p ())
8065 scev_reset_htab ();
8067 /* For late FRE after IVOPTs and unrolling, see if we can
8068 remove some TREE_ADDRESSABLE and rewrite stuff into SSA. */
8069 if (!may_iterate)
8070 todo |= TODO_update_address_taken;
8072 return todo;
8075 } // anon namespace
8077 gimple_opt_pass *
8078 make_pass_fre (gcc::context *ctxt)
8080 return new pass_fre (ctxt);
8083 #undef BB_EXECUTABLE