* lto-symtab.c (lto_varpool_replace_node): Merge TLS models.
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob25c67d01a7a16d6331d23313ff3c0744d6840b52
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2015 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 "tm.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "predict.h"
38 #include "hard-reg-set.h"
39 #include "function.h"
40 #include "dominance.h"
41 #include "cfg.h"
42 #include "cfganal.h"
43 #include "basic-block.h"
44 #include "gimple-pretty-print.h"
45 #include "tree-inline.h"
46 #include "hash-table.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-fold.h"
50 #include "tree-eh.h"
51 #include "gimple-expr.h"
52 #include "is-a.h"
53 #include "gimple.h"
54 #include "gimplify.h"
55 #include "gimple-ssa.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
58 #include "stringpool.h"
59 #include "tree-ssanames.h"
60 #include "hashtab.h"
61 #include "rtl.h"
62 #include "flags.h"
63 #include "statistics.h"
64 #include "real.h"
65 #include "fixed-value.h"
66 #include "insn-config.h"
67 #include "expmed.h"
68 #include "dojump.h"
69 #include "explow.h"
70 #include "calls.h"
71 #include "emit-rtl.h"
72 #include "varasm.h"
73 #include "stmt.h"
74 #include "expr.h"
75 #include "tree-dfa.h"
76 #include "tree-ssa.h"
77 #include "dumpfile.h"
78 #include "alloc-pool.h"
79 #include "cfgloop.h"
80 #include "params.h"
81 #include "tree-ssa-propagate.h"
82 #include "tree-ssa-sccvn.h"
83 #include "tree-cfg.h"
84 #include "domwalk.h"
85 #include "ipa-ref.h"
86 #include "plugin-api.h"
87 #include "cgraph.h"
89 /* This algorithm is based on the SCC algorithm presented by Keith
90 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
91 (http://citeseer.ist.psu.edu/41805.html). In
92 straight line code, it is equivalent to a regular hash based value
93 numbering that is performed in reverse postorder.
95 For code with cycles, there are two alternatives, both of which
96 require keeping the hashtables separate from the actual list of
97 value numbers for SSA names.
99 1. Iterate value numbering in an RPO walk of the blocks, removing
100 all the entries from the hashtable after each iteration (but
101 keeping the SSA name->value number mapping between iterations).
102 Iterate until it does not change.
104 2. Perform value numbering as part of an SCC walk on the SSA graph,
105 iterating only the cycles in the SSA graph until they do not change
106 (using a separate, optimistic hashtable for value numbering the SCC
107 operands).
109 The second is not just faster in practice (because most SSA graph
110 cycles do not involve all the variables in the graph), it also has
111 some nice properties.
113 One of these nice properties is that when we pop an SCC off the
114 stack, we are guaranteed to have processed all the operands coming from
115 *outside of that SCC*, so we do not need to do anything special to
116 ensure they have value numbers.
118 Another nice property is that the SCC walk is done as part of a DFS
119 of the SSA graph, which makes it easy to perform combining and
120 simplifying operations at the same time.
122 The code below is deliberately written in a way that makes it easy
123 to separate the SCC walk from the other work it does.
125 In order to propagate constants through the code, we track which
126 expressions contain constants, and use those while folding. In
127 theory, we could also track expressions whose value numbers are
128 replaced, in case we end up folding based on expression
129 identities.
131 In order to value number memory, we assign value numbers to vuses.
132 This enables us to note that, for example, stores to the same
133 address of the same value from the same starting memory states are
134 equivalent.
135 TODO:
137 1. We can iterate only the changing portions of the SCC's, but
138 I have not seen an SCC big enough for this to be a win.
139 2. If you differentiate between phi nodes for loops and phi nodes
140 for if-then-else, you can properly consider phi nodes in different
141 blocks for equivalence.
142 3. We could value number vuses in more cases, particularly, whole
143 structure copies.
147 /* vn_nary_op hashtable helpers. */
149 struct vn_nary_op_hasher : typed_noop_remove <vn_nary_op_s>
151 typedef vn_nary_op_s value_type;
152 typedef vn_nary_op_s compare_type;
153 static inline hashval_t hash (const value_type *);
154 static inline bool equal (const value_type *, const compare_type *);
157 /* Return the computed hashcode for nary operation P1. */
159 inline hashval_t
160 vn_nary_op_hasher::hash (const value_type *vno1)
162 return vno1->hashcode;
165 /* Compare nary operations P1 and P2 and return true if they are
166 equivalent. */
168 inline bool
169 vn_nary_op_hasher::equal (const value_type *vno1, const compare_type *vno2)
171 return vn_nary_op_eq (vno1, vno2);
174 typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
175 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
178 /* vn_phi hashtable helpers. */
180 static int
181 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
183 struct vn_phi_hasher
185 typedef vn_phi_s value_type;
186 typedef vn_phi_s compare_type;
187 static inline hashval_t hash (const value_type *);
188 static inline bool equal (const value_type *, const compare_type *);
189 static inline void remove (value_type *);
192 /* Return the computed hashcode for phi operation P1. */
194 inline hashval_t
195 vn_phi_hasher::hash (const value_type *vp1)
197 return vp1->hashcode;
200 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
202 inline bool
203 vn_phi_hasher::equal (const value_type *vp1, const compare_type *vp2)
205 return vn_phi_eq (vp1, vp2);
208 /* Free a phi operation structure VP. */
210 inline void
211 vn_phi_hasher::remove (value_type *phi)
213 phi->phiargs.release ();
216 typedef hash_table<vn_phi_hasher> vn_phi_table_type;
217 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
220 /* Compare two reference operands P1 and P2 for equality. Return true if
221 they are equal, and false otherwise. */
223 static int
224 vn_reference_op_eq (const void *p1, const void *p2)
226 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
227 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
229 return (vro1->opcode == vro2->opcode
230 /* We do not care for differences in type qualification. */
231 && (vro1->type == vro2->type
232 || (vro1->type && vro2->type
233 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
234 TYPE_MAIN_VARIANT (vro2->type))))
235 && expressions_equal_p (vro1->op0, vro2->op0)
236 && expressions_equal_p (vro1->op1, vro2->op1)
237 && expressions_equal_p (vro1->op2, vro2->op2));
240 /* Free a reference operation structure VP. */
242 static inline void
243 free_reference (vn_reference_s *vr)
245 vr->operands.release ();
249 /* vn_reference hashtable helpers. */
251 struct vn_reference_hasher
253 typedef vn_reference_s value_type;
254 typedef vn_reference_s compare_type;
255 static inline hashval_t hash (const value_type *);
256 static inline bool equal (const value_type *, const compare_type *);
257 static inline void remove (value_type *);
260 /* Return the hashcode for a given reference operation P1. */
262 inline hashval_t
263 vn_reference_hasher::hash (const value_type *vr1)
265 return vr1->hashcode;
268 inline bool
269 vn_reference_hasher::equal (const value_type *v, const compare_type *c)
271 return vn_reference_eq (v, c);
274 inline void
275 vn_reference_hasher::remove (value_type *v)
277 free_reference (v);
280 typedef hash_table<vn_reference_hasher> vn_reference_table_type;
281 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
284 /* The set of hashtables and alloc_pool's for their items. */
286 typedef struct vn_tables_s
288 vn_nary_op_table_type *nary;
289 vn_phi_table_type *phis;
290 vn_reference_table_type *references;
291 struct obstack nary_obstack;
292 alloc_pool phis_pool;
293 alloc_pool references_pool;
294 } *vn_tables_t;
297 /* vn_constant hashtable helpers. */
299 struct vn_constant_hasher : typed_free_remove <vn_constant_s>
301 typedef vn_constant_s value_type;
302 typedef vn_constant_s compare_type;
303 static inline hashval_t hash (const value_type *);
304 static inline bool equal (const value_type *, const compare_type *);
307 /* Hash table hash function for vn_constant_t. */
309 inline hashval_t
310 vn_constant_hasher::hash (const value_type *vc1)
312 return vc1->hashcode;
315 /* Hash table equality function for vn_constant_t. */
317 inline bool
318 vn_constant_hasher::equal (const value_type *vc1, const compare_type *vc2)
320 if (vc1->hashcode != vc2->hashcode)
321 return false;
323 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
326 static hash_table<vn_constant_hasher> *constant_to_value_id;
327 static bitmap constant_value_ids;
330 /* Valid hashtables storing information we have proven to be
331 correct. */
333 static vn_tables_t valid_info;
335 /* Optimistic hashtables storing information we are making assumptions about
336 during iterations. */
338 static vn_tables_t optimistic_info;
340 /* Pointer to the set of hashtables that is currently being used.
341 Should always point to either the optimistic_info, or the
342 valid_info. */
344 static vn_tables_t current_info;
347 /* Reverse post order index for each basic block. */
349 static int *rpo_numbers;
351 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
353 /* Return the SSA value of the VUSE x, supporting released VDEFs
354 during elimination which will value-number the VDEF to the
355 associated VUSE (but not substitute in the whole lattice). */
357 static inline tree
358 vuse_ssa_val (tree x)
360 if (!x)
361 return NULL_TREE;
365 x = SSA_VAL (x);
367 while (SSA_NAME_IN_FREE_LIST (x));
369 return x;
372 /* This represents the top of the VN lattice, which is the universal
373 value. */
375 tree VN_TOP;
377 /* Unique counter for our value ids. */
379 static unsigned int next_value_id;
381 /* Next DFS number and the stack for strongly connected component
382 detection. */
384 static unsigned int next_dfs_num;
385 static vec<tree> sccstack;
389 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
390 are allocated on an obstack for locality reasons, and to free them
391 without looping over the vec. */
393 static vec<vn_ssa_aux_t> vn_ssa_aux_table;
394 static struct obstack vn_ssa_aux_obstack;
396 /* Return the value numbering information for a given SSA name. */
398 vn_ssa_aux_t
399 VN_INFO (tree name)
401 vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)];
402 gcc_checking_assert (res);
403 return res;
406 /* Set the value numbering info for a given SSA name to a given
407 value. */
409 static inline void
410 VN_INFO_SET (tree name, vn_ssa_aux_t value)
412 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value;
415 /* Initialize the value numbering info for a given SSA name.
416 This should be called just once for every SSA name. */
418 vn_ssa_aux_t
419 VN_INFO_GET (tree name)
421 vn_ssa_aux_t newinfo;
423 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
424 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
425 if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ())
426 vn_ssa_aux_table.safe_grow (SSA_NAME_VERSION (name) + 1);
427 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo;
428 return newinfo;
432 /* Get the representative expression for the SSA_NAME NAME. Returns
433 the representative SSA_NAME if there is no expression associated with it. */
435 tree
436 vn_get_expr_for (tree name)
438 vn_ssa_aux_t vn = VN_INFO (name);
439 gimple def_stmt;
440 tree expr = NULL_TREE;
441 enum tree_code code;
443 if (vn->valnum == VN_TOP)
444 return name;
446 /* If the value-number is a constant it is the representative
447 expression. */
448 if (TREE_CODE (vn->valnum) != SSA_NAME)
449 return vn->valnum;
451 /* Get to the information of the value of this SSA_NAME. */
452 vn = VN_INFO (vn->valnum);
454 /* If the value-number is a constant it is the representative
455 expression. */
456 if (TREE_CODE (vn->valnum) != SSA_NAME)
457 return vn->valnum;
459 /* Else if we have an expression, return it. */
460 if (vn->expr != NULL_TREE)
461 return vn->expr;
463 /* Otherwise use the defining statement to build the expression. */
464 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
466 /* If the value number is not an assignment use it directly. */
467 if (!is_gimple_assign (def_stmt))
468 return vn->valnum;
470 /* Note that we can valueize here because we clear the cached
471 simplified expressions after each optimistic iteration. */
472 code = gimple_assign_rhs_code (def_stmt);
473 switch (TREE_CODE_CLASS (code))
475 case tcc_reference:
476 if ((code == REALPART_EXPR
477 || code == IMAGPART_EXPR
478 || code == VIEW_CONVERT_EXPR)
479 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
480 0)) == SSA_NAME)
481 expr = fold_build1 (code,
482 gimple_expr_type (def_stmt),
483 vn_valueize (TREE_OPERAND
484 (gimple_assign_rhs1 (def_stmt), 0)));
485 break;
487 case tcc_unary:
488 expr = fold_build1 (code,
489 gimple_expr_type (def_stmt),
490 vn_valueize (gimple_assign_rhs1 (def_stmt)));
491 break;
493 case tcc_binary:
494 expr = fold_build2 (code,
495 gimple_expr_type (def_stmt),
496 vn_valueize (gimple_assign_rhs1 (def_stmt)),
497 vn_valueize (gimple_assign_rhs2 (def_stmt)));
498 break;
500 case tcc_exceptional:
501 if (code == CONSTRUCTOR
502 && TREE_CODE
503 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
504 expr = gimple_assign_rhs1 (def_stmt);
505 break;
507 default:;
509 if (expr == NULL_TREE)
510 return vn->valnum;
512 /* Cache the expression. */
513 vn->expr = expr;
515 return expr;
518 /* Return the vn_kind the expression computed by the stmt should be
519 associated with. */
521 enum vn_kind
522 vn_get_stmt_kind (gimple stmt)
524 switch (gimple_code (stmt))
526 case GIMPLE_CALL:
527 return VN_REFERENCE;
528 case GIMPLE_PHI:
529 return VN_PHI;
530 case GIMPLE_ASSIGN:
532 enum tree_code code = gimple_assign_rhs_code (stmt);
533 tree rhs1 = gimple_assign_rhs1 (stmt);
534 switch (get_gimple_rhs_class (code))
536 case GIMPLE_UNARY_RHS:
537 case GIMPLE_BINARY_RHS:
538 case GIMPLE_TERNARY_RHS:
539 return VN_NARY;
540 case GIMPLE_SINGLE_RHS:
541 switch (TREE_CODE_CLASS (code))
543 case tcc_reference:
544 /* VOP-less references can go through unary case. */
545 if ((code == REALPART_EXPR
546 || code == IMAGPART_EXPR
547 || code == VIEW_CONVERT_EXPR
548 || code == BIT_FIELD_REF)
549 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
550 return VN_NARY;
552 /* Fallthrough. */
553 case tcc_declaration:
554 return VN_REFERENCE;
556 case tcc_constant:
557 return VN_CONSTANT;
559 default:
560 if (code == ADDR_EXPR)
561 return (is_gimple_min_invariant (rhs1)
562 ? VN_CONSTANT : VN_REFERENCE);
563 else if (code == CONSTRUCTOR)
564 return VN_NARY;
565 return VN_NONE;
567 default:
568 return VN_NONE;
571 default:
572 return VN_NONE;
576 /* Lookup a value id for CONSTANT and return it. If it does not
577 exist returns 0. */
579 unsigned int
580 get_constant_value_id (tree constant)
582 vn_constant_s **slot;
583 struct vn_constant_s vc;
585 vc.hashcode = vn_hash_constant_with_type (constant);
586 vc.constant = constant;
587 slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
588 if (slot)
589 return (*slot)->value_id;
590 return 0;
593 /* Lookup a value id for CONSTANT, and if it does not exist, create a
594 new one and return it. If it does exist, return it. */
596 unsigned int
597 get_or_alloc_constant_value_id (tree constant)
599 vn_constant_s **slot;
600 struct vn_constant_s vc;
601 vn_constant_t vcp;
603 vc.hashcode = vn_hash_constant_with_type (constant);
604 vc.constant = constant;
605 slot = constant_to_value_id->find_slot (&vc, INSERT);
606 if (*slot)
607 return (*slot)->value_id;
609 vcp = XNEW (struct vn_constant_s);
610 vcp->hashcode = vc.hashcode;
611 vcp->constant = constant;
612 vcp->value_id = get_next_value_id ();
613 *slot = vcp;
614 bitmap_set_bit (constant_value_ids, vcp->value_id);
615 return vcp->value_id;
618 /* Return true if V is a value id for a constant. */
620 bool
621 value_id_constant_p (unsigned int v)
623 return bitmap_bit_p (constant_value_ids, v);
626 /* Compute the hash for a reference operand VRO1. */
628 static void
629 vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
631 hstate.add_int (vro1->opcode);
632 if (vro1->op0)
633 inchash::add_expr (vro1->op0, hstate);
634 if (vro1->op1)
635 inchash::add_expr (vro1->op1, hstate);
636 if (vro1->op2)
637 inchash::add_expr (vro1->op2, hstate);
640 /* Compute a hash for the reference operation VR1 and return it. */
642 static hashval_t
643 vn_reference_compute_hash (const vn_reference_t vr1)
645 inchash::hash hstate;
646 hashval_t result;
647 int i;
648 vn_reference_op_t vro;
649 HOST_WIDE_INT off = -1;
650 bool deref = false;
652 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
654 if (vro->opcode == MEM_REF)
655 deref = true;
656 else if (vro->opcode != ADDR_EXPR)
657 deref = false;
658 if (vro->off != -1)
660 if (off == -1)
661 off = 0;
662 off += vro->off;
664 else
666 if (off != -1
667 && off != 0)
668 hstate.add_int (off);
669 off = -1;
670 if (deref
671 && vro->opcode == ADDR_EXPR)
673 if (vro->op0)
675 tree op = TREE_OPERAND (vro->op0, 0);
676 hstate.add_int (TREE_CODE (op));
677 inchash::add_expr (op, hstate);
680 else
681 vn_reference_op_compute_hash (vro, hstate);
684 result = hstate.end ();
685 /* ??? We would ICE later if we hash instead of adding that in. */
686 if (vr1->vuse)
687 result += SSA_NAME_VERSION (vr1->vuse);
689 return result;
692 /* Return true if reference operations VR1 and VR2 are equivalent. This
693 means they have the same set of operands and vuses. */
695 bool
696 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
698 unsigned i, j;
700 /* Early out if this is not a hash collision. */
701 if (vr1->hashcode != vr2->hashcode)
702 return false;
704 /* The VOP needs to be the same. */
705 if (vr1->vuse != vr2->vuse)
706 return false;
708 /* If the operands are the same we are done. */
709 if (vr1->operands == vr2->operands)
710 return true;
712 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
713 return false;
715 if (INTEGRAL_TYPE_P (vr1->type)
716 && INTEGRAL_TYPE_P (vr2->type))
718 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
719 return false;
721 else if (INTEGRAL_TYPE_P (vr1->type)
722 && (TYPE_PRECISION (vr1->type)
723 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
724 return false;
725 else if (INTEGRAL_TYPE_P (vr2->type)
726 && (TYPE_PRECISION (vr2->type)
727 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
728 return false;
730 i = 0;
731 j = 0;
734 HOST_WIDE_INT off1 = 0, off2 = 0;
735 vn_reference_op_t vro1, vro2;
736 vn_reference_op_s tem1, tem2;
737 bool deref1 = false, deref2 = false;
738 for (; vr1->operands.iterate (i, &vro1); i++)
740 if (vro1->opcode == MEM_REF)
741 deref1 = true;
742 if (vro1->off == -1)
743 break;
744 off1 += vro1->off;
746 for (; vr2->operands.iterate (j, &vro2); j++)
748 if (vro2->opcode == MEM_REF)
749 deref2 = true;
750 if (vro2->off == -1)
751 break;
752 off2 += vro2->off;
754 if (off1 != off2)
755 return false;
756 if (deref1 && vro1->opcode == ADDR_EXPR)
758 memset (&tem1, 0, sizeof (tem1));
759 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
760 tem1.type = TREE_TYPE (tem1.op0);
761 tem1.opcode = TREE_CODE (tem1.op0);
762 vro1 = &tem1;
763 deref1 = false;
765 if (deref2 && vro2->opcode == ADDR_EXPR)
767 memset (&tem2, 0, sizeof (tem2));
768 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
769 tem2.type = TREE_TYPE (tem2.op0);
770 tem2.opcode = TREE_CODE (tem2.op0);
771 vro2 = &tem2;
772 deref2 = false;
774 if (deref1 != deref2)
775 return false;
776 if (!vn_reference_op_eq (vro1, vro2))
777 return false;
778 ++j;
779 ++i;
781 while (vr1->operands.length () != i
782 || vr2->operands.length () != j);
784 return true;
787 /* Copy the operations present in load/store REF into RESULT, a vector of
788 vn_reference_op_s's. */
790 static void
791 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
793 if (TREE_CODE (ref) == TARGET_MEM_REF)
795 vn_reference_op_s temp;
797 result->reserve (3);
799 memset (&temp, 0, sizeof (temp));
800 temp.type = TREE_TYPE (ref);
801 temp.opcode = TREE_CODE (ref);
802 temp.op0 = TMR_INDEX (ref);
803 temp.op1 = TMR_STEP (ref);
804 temp.op2 = TMR_OFFSET (ref);
805 temp.off = -1;
806 result->quick_push (temp);
808 memset (&temp, 0, sizeof (temp));
809 temp.type = NULL_TREE;
810 temp.opcode = ERROR_MARK;
811 temp.op0 = TMR_INDEX2 (ref);
812 temp.off = -1;
813 result->quick_push (temp);
815 memset (&temp, 0, sizeof (temp));
816 temp.type = NULL_TREE;
817 temp.opcode = TREE_CODE (TMR_BASE (ref));
818 temp.op0 = TMR_BASE (ref);
819 temp.off = -1;
820 result->quick_push (temp);
821 return;
824 /* For non-calls, store the information that makes up the address. */
825 tree orig = ref;
826 while (ref)
828 vn_reference_op_s temp;
830 memset (&temp, 0, sizeof (temp));
831 temp.type = TREE_TYPE (ref);
832 temp.opcode = TREE_CODE (ref);
833 temp.off = -1;
835 switch (temp.opcode)
837 case MODIFY_EXPR:
838 temp.op0 = TREE_OPERAND (ref, 1);
839 break;
840 case WITH_SIZE_EXPR:
841 temp.op0 = TREE_OPERAND (ref, 1);
842 temp.off = 0;
843 break;
844 case MEM_REF:
845 /* The base address gets its own vn_reference_op_s structure. */
846 temp.op0 = TREE_OPERAND (ref, 1);
847 if (tree_fits_shwi_p (TREE_OPERAND (ref, 1)))
848 temp.off = tree_to_shwi (TREE_OPERAND (ref, 1));
849 break;
850 case BIT_FIELD_REF:
851 /* Record bits and position. */
852 temp.op0 = TREE_OPERAND (ref, 1);
853 temp.op1 = TREE_OPERAND (ref, 2);
854 break;
855 case COMPONENT_REF:
856 /* The field decl is enough to unambiguously specify the field,
857 a matching type is not necessary and a mismatching type
858 is always a spurious difference. */
859 temp.type = NULL_TREE;
860 temp.op0 = TREE_OPERAND (ref, 1);
861 temp.op1 = TREE_OPERAND (ref, 2);
863 tree this_offset = component_ref_field_offset (ref);
864 if (this_offset
865 && TREE_CODE (this_offset) == INTEGER_CST)
867 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
868 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
870 offset_int off
871 = (wi::to_offset (this_offset)
872 + wi::lrshift (wi::to_offset (bit_offset),
873 LOG2_BITS_PER_UNIT));
874 if (wi::fits_shwi_p (off)
875 /* Probibit value-numbering zero offset components
876 of addresses the same before the pass folding
877 __builtin_object_size had a chance to run
878 (checking cfun->after_inlining does the
879 trick here). */
880 && (TREE_CODE (orig) != ADDR_EXPR
881 || off != 0
882 || cfun->after_inlining))
883 temp.off = off.to_shwi ();
887 break;
888 case ARRAY_RANGE_REF:
889 case ARRAY_REF:
890 /* Record index as operand. */
891 temp.op0 = TREE_OPERAND (ref, 1);
892 /* Always record lower bounds and element size. */
893 temp.op1 = array_ref_low_bound (ref);
894 temp.op2 = array_ref_element_size (ref);
895 if (TREE_CODE (temp.op0) == INTEGER_CST
896 && TREE_CODE (temp.op1) == INTEGER_CST
897 && TREE_CODE (temp.op2) == INTEGER_CST)
899 offset_int off = ((wi::to_offset (temp.op0)
900 - wi::to_offset (temp.op1))
901 * wi::to_offset (temp.op2));
902 if (wi::fits_shwi_p (off))
903 temp.off = off.to_shwi();
905 break;
906 case VAR_DECL:
907 if (DECL_HARD_REGISTER (ref))
909 temp.op0 = ref;
910 break;
912 /* Fallthru. */
913 case PARM_DECL:
914 case CONST_DECL:
915 case RESULT_DECL:
916 /* Canonicalize decls to MEM[&decl] which is what we end up with
917 when valueizing MEM[ptr] with ptr = &decl. */
918 temp.opcode = MEM_REF;
919 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
920 temp.off = 0;
921 result->safe_push (temp);
922 temp.opcode = ADDR_EXPR;
923 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
924 temp.type = TREE_TYPE (temp.op0);
925 temp.off = -1;
926 break;
927 case STRING_CST:
928 case INTEGER_CST:
929 case COMPLEX_CST:
930 case VECTOR_CST:
931 case REAL_CST:
932 case FIXED_CST:
933 case CONSTRUCTOR:
934 case SSA_NAME:
935 temp.op0 = ref;
936 break;
937 case ADDR_EXPR:
938 if (is_gimple_min_invariant (ref))
940 temp.op0 = ref;
941 break;
943 break;
944 /* These are only interesting for their operands, their
945 existence, and their type. They will never be the last
946 ref in the chain of references (IE they require an
947 operand), so we don't have to put anything
948 for op* as it will be handled by the iteration */
949 case REALPART_EXPR:
950 case VIEW_CONVERT_EXPR:
951 temp.off = 0;
952 break;
953 case IMAGPART_EXPR:
954 /* This is only interesting for its constant offset. */
955 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
956 break;
957 default:
958 gcc_unreachable ();
960 result->safe_push (temp);
962 if (REFERENCE_CLASS_P (ref)
963 || TREE_CODE (ref) == MODIFY_EXPR
964 || TREE_CODE (ref) == WITH_SIZE_EXPR
965 || (TREE_CODE (ref) == ADDR_EXPR
966 && !is_gimple_min_invariant (ref)))
967 ref = TREE_OPERAND (ref, 0);
968 else
969 ref = NULL_TREE;
973 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
974 operands in *OPS, the reference alias set SET and the reference type TYPE.
975 Return true if something useful was produced. */
977 bool
978 ao_ref_init_from_vn_reference (ao_ref *ref,
979 alias_set_type set, tree type,
980 vec<vn_reference_op_s> ops)
982 vn_reference_op_t op;
983 unsigned i;
984 tree base = NULL_TREE;
985 tree *op0_p = &base;
986 HOST_WIDE_INT offset = 0;
987 HOST_WIDE_INT max_size;
988 HOST_WIDE_INT size = -1;
989 tree size_tree = NULL_TREE;
990 alias_set_type base_alias_set = -1;
992 /* First get the final access size from just the outermost expression. */
993 op = &ops[0];
994 if (op->opcode == COMPONENT_REF)
995 size_tree = DECL_SIZE (op->op0);
996 else if (op->opcode == BIT_FIELD_REF)
997 size_tree = op->op0;
998 else
1000 machine_mode mode = TYPE_MODE (type);
1001 if (mode == BLKmode)
1002 size_tree = TYPE_SIZE (type);
1003 else
1004 size = GET_MODE_BITSIZE (mode);
1006 if (size_tree != NULL_TREE)
1008 if (!tree_fits_uhwi_p (size_tree))
1009 size = -1;
1010 else
1011 size = tree_to_uhwi (size_tree);
1014 /* Initially, maxsize is the same as the accessed element size.
1015 In the following it will only grow (or become -1). */
1016 max_size = size;
1018 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1019 and find the ultimate containing object. */
1020 FOR_EACH_VEC_ELT (ops, i, op)
1022 switch (op->opcode)
1024 /* These may be in the reference ops, but we cannot do anything
1025 sensible with them here. */
1026 case ADDR_EXPR:
1027 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1028 if (base != NULL_TREE
1029 && TREE_CODE (base) == MEM_REF
1030 && op->op0
1031 && DECL_P (TREE_OPERAND (op->op0, 0)))
1033 vn_reference_op_t pop = &ops[i-1];
1034 base = TREE_OPERAND (op->op0, 0);
1035 if (pop->off == -1)
1037 max_size = -1;
1038 offset = 0;
1040 else
1041 offset += pop->off * BITS_PER_UNIT;
1042 op0_p = NULL;
1043 break;
1045 /* Fallthru. */
1046 case CALL_EXPR:
1047 return false;
1049 /* Record the base objects. */
1050 case MEM_REF:
1051 base_alias_set = get_deref_alias_set (op->op0);
1052 *op0_p = build2 (MEM_REF, op->type,
1053 NULL_TREE, op->op0);
1054 op0_p = &TREE_OPERAND (*op0_p, 0);
1055 break;
1057 case VAR_DECL:
1058 case PARM_DECL:
1059 case RESULT_DECL:
1060 case SSA_NAME:
1061 *op0_p = op->op0;
1062 op0_p = NULL;
1063 break;
1065 /* And now the usual component-reference style ops. */
1066 case BIT_FIELD_REF:
1067 offset += tree_to_shwi (op->op1);
1068 break;
1070 case COMPONENT_REF:
1072 tree field = op->op0;
1073 /* We do not have a complete COMPONENT_REF tree here so we
1074 cannot use component_ref_field_offset. Do the interesting
1075 parts manually. */
1077 if (op->op1
1078 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
1079 max_size = -1;
1080 else
1082 offset += (tree_to_uhwi (DECL_FIELD_OFFSET (field))
1083 * BITS_PER_UNIT);
1084 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
1086 break;
1089 case ARRAY_RANGE_REF:
1090 case ARRAY_REF:
1091 /* We recorded the lower bound and the element size. */
1092 if (!tree_fits_shwi_p (op->op0)
1093 || !tree_fits_shwi_p (op->op1)
1094 || !tree_fits_shwi_p (op->op2))
1095 max_size = -1;
1096 else
1098 HOST_WIDE_INT hindex = tree_to_shwi (op->op0);
1099 hindex -= tree_to_shwi (op->op1);
1100 hindex *= tree_to_shwi (op->op2);
1101 hindex *= BITS_PER_UNIT;
1102 offset += hindex;
1104 break;
1106 case REALPART_EXPR:
1107 break;
1109 case IMAGPART_EXPR:
1110 offset += size;
1111 break;
1113 case VIEW_CONVERT_EXPR:
1114 break;
1116 case STRING_CST:
1117 case INTEGER_CST:
1118 case COMPLEX_CST:
1119 case VECTOR_CST:
1120 case REAL_CST:
1121 case CONSTRUCTOR:
1122 case CONST_DECL:
1123 return false;
1125 default:
1126 return false;
1130 if (base == NULL_TREE)
1131 return false;
1133 ref->ref = NULL_TREE;
1134 ref->base = base;
1135 ref->offset = offset;
1136 ref->size = size;
1137 ref->max_size = max_size;
1138 ref->ref_alias_set = set;
1139 if (base_alias_set != -1)
1140 ref->base_alias_set = base_alias_set;
1141 else
1142 ref->base_alias_set = get_alias_set (base);
1143 /* We discount volatiles from value-numbering elsewhere. */
1144 ref->volatile_p = false;
1146 return true;
1149 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1150 vn_reference_op_s's. */
1152 static void
1153 copy_reference_ops_from_call (gcall *call,
1154 vec<vn_reference_op_s> *result)
1156 vn_reference_op_s temp;
1157 unsigned i;
1158 tree lhs = gimple_call_lhs (call);
1159 int lr;
1161 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1162 different. By adding the lhs here in the vector, we ensure that the
1163 hashcode is different, guaranteeing a different value number. */
1164 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1166 memset (&temp, 0, sizeof (temp));
1167 temp.opcode = MODIFY_EXPR;
1168 temp.type = TREE_TYPE (lhs);
1169 temp.op0 = lhs;
1170 temp.off = -1;
1171 result->safe_push (temp);
1174 /* Copy the type, opcode, function, static chain and EH region, if any. */
1175 memset (&temp, 0, sizeof (temp));
1176 temp.type = gimple_call_return_type (call);
1177 temp.opcode = CALL_EXPR;
1178 temp.op0 = gimple_call_fn (call);
1179 temp.op1 = gimple_call_chain (call);
1180 if (stmt_could_throw_p (call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1181 temp.op2 = size_int (lr);
1182 temp.off = -1;
1183 if (gimple_call_with_bounds_p (call))
1184 temp.with_bounds = 1;
1185 result->safe_push (temp);
1187 /* Copy the call arguments. As they can be references as well,
1188 just chain them together. */
1189 for (i = 0; i < gimple_call_num_args (call); ++i)
1191 tree callarg = gimple_call_arg (call, i);
1192 copy_reference_ops_from_ref (callarg, result);
1196 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1197 *I_P to point to the last element of the replacement. */
1198 void
1199 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1200 unsigned int *i_p)
1202 unsigned int i = *i_p;
1203 vn_reference_op_t op = &(*ops)[i];
1204 vn_reference_op_t mem_op = &(*ops)[i - 1];
1205 tree addr_base;
1206 HOST_WIDE_INT addr_offset = 0;
1208 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1209 from .foo.bar to the preceding MEM_REF offset and replace the
1210 address with &OBJ. */
1211 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1212 &addr_offset);
1213 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1214 if (addr_base != TREE_OPERAND (op->op0, 0))
1216 offset_int off = offset_int::from (mem_op->op0, SIGNED);
1217 off += addr_offset;
1218 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1219 op->op0 = build_fold_addr_expr (addr_base);
1220 if (tree_fits_shwi_p (mem_op->op0))
1221 mem_op->off = tree_to_shwi (mem_op->op0);
1222 else
1223 mem_op->off = -1;
1227 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1228 *I_P to point to the last element of the replacement. */
1229 static void
1230 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1231 unsigned int *i_p)
1233 unsigned int i = *i_p;
1234 vn_reference_op_t op = &(*ops)[i];
1235 vn_reference_op_t mem_op = &(*ops)[i - 1];
1236 gimple def_stmt;
1237 enum tree_code code;
1238 offset_int off;
1240 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1241 if (!is_gimple_assign (def_stmt))
1242 return;
1244 code = gimple_assign_rhs_code (def_stmt);
1245 if (code != ADDR_EXPR
1246 && code != POINTER_PLUS_EXPR)
1247 return;
1249 off = offset_int::from (mem_op->op0, SIGNED);
1251 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1252 from .foo.bar to the preceding MEM_REF offset and replace the
1253 address with &OBJ. */
1254 if (code == ADDR_EXPR)
1256 tree addr, addr_base;
1257 HOST_WIDE_INT addr_offset;
1259 addr = gimple_assign_rhs1 (def_stmt);
1260 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1261 &addr_offset);
1262 if (!addr_base
1263 || TREE_CODE (addr_base) != MEM_REF)
1264 return;
1266 off += addr_offset;
1267 off += mem_ref_offset (addr_base);
1268 op->op0 = TREE_OPERAND (addr_base, 0);
1270 else
1272 tree ptr, ptroff;
1273 ptr = gimple_assign_rhs1 (def_stmt);
1274 ptroff = gimple_assign_rhs2 (def_stmt);
1275 if (TREE_CODE (ptr) != SSA_NAME
1276 || TREE_CODE (ptroff) != INTEGER_CST)
1277 return;
1279 off += wi::to_offset (ptroff);
1280 op->op0 = ptr;
1283 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1284 if (tree_fits_shwi_p (mem_op->op0))
1285 mem_op->off = tree_to_shwi (mem_op->op0);
1286 else
1287 mem_op->off = -1;
1288 if (TREE_CODE (op->op0) == SSA_NAME)
1289 op->op0 = SSA_VAL (op->op0);
1290 if (TREE_CODE (op->op0) != SSA_NAME)
1291 op->opcode = TREE_CODE (op->op0);
1293 /* And recurse. */
1294 if (TREE_CODE (op->op0) == SSA_NAME)
1295 vn_reference_maybe_forwprop_address (ops, i_p);
1296 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1297 vn_reference_fold_indirect (ops, i_p);
1300 /* Optimize the reference REF to a constant if possible or return
1301 NULL_TREE if not. */
1303 tree
1304 fully_constant_vn_reference_p (vn_reference_t ref)
1306 vec<vn_reference_op_s> operands = ref->operands;
1307 vn_reference_op_t op;
1309 /* Try to simplify the translated expression if it is
1310 a call to a builtin function with at most two arguments. */
1311 op = &operands[0];
1312 if (op->opcode == CALL_EXPR
1313 && TREE_CODE (op->op0) == ADDR_EXPR
1314 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1315 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1316 && operands.length () >= 2
1317 && operands.length () <= 3)
1319 vn_reference_op_t arg0, arg1 = NULL;
1320 bool anyconst = false;
1321 arg0 = &operands[1];
1322 if (operands.length () > 2)
1323 arg1 = &operands[2];
1324 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1325 || (arg0->opcode == ADDR_EXPR
1326 && is_gimple_min_invariant (arg0->op0)))
1327 anyconst = true;
1328 if (arg1
1329 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1330 || (arg1->opcode == ADDR_EXPR
1331 && is_gimple_min_invariant (arg1->op0))))
1332 anyconst = true;
1333 if (anyconst)
1335 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1336 arg1 ? 2 : 1,
1337 arg0->op0,
1338 arg1 ? arg1->op0 : NULL);
1339 if (folded
1340 && TREE_CODE (folded) == NOP_EXPR)
1341 folded = TREE_OPERAND (folded, 0);
1342 if (folded
1343 && is_gimple_min_invariant (folded))
1344 return folded;
1348 /* Simplify reads from constants or constant initializers. */
1349 else if (BITS_PER_UNIT == 8
1350 && is_gimple_reg_type (ref->type)
1351 && (!INTEGRAL_TYPE_P (ref->type)
1352 || TYPE_PRECISION (ref->type) % BITS_PER_UNIT == 0))
1354 HOST_WIDE_INT off = 0;
1355 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (ref->type));
1356 if (size % BITS_PER_UNIT != 0
1357 || size > MAX_BITSIZE_MODE_ANY_MODE)
1358 return NULL_TREE;
1359 size /= BITS_PER_UNIT;
1360 unsigned i;
1361 for (i = 0; i < operands.length (); ++i)
1363 if (operands[i].off == -1)
1364 return NULL_TREE;
1365 off += operands[i].off;
1366 if (operands[i].opcode == MEM_REF)
1368 ++i;
1369 break;
1372 vn_reference_op_t base = &operands[--i];
1373 tree ctor = error_mark_node;
1374 tree decl = NULL_TREE;
1375 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1376 ctor = base->op0;
1377 else if (base->opcode == MEM_REF
1378 && base[1].opcode == ADDR_EXPR
1379 && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
1380 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL))
1382 decl = TREE_OPERAND (base[1].op0, 0);
1383 ctor = ctor_for_folding (decl);
1385 if (ctor == NULL_TREE)
1386 return build_zero_cst (ref->type);
1387 else if (ctor != error_mark_node)
1389 if (decl)
1391 tree res = fold_ctor_reference (ref->type, ctor,
1392 off * BITS_PER_UNIT,
1393 size * BITS_PER_UNIT, decl);
1394 if (res)
1396 STRIP_USELESS_TYPE_CONVERSION (res);
1397 if (is_gimple_min_invariant (res))
1398 return res;
1401 else
1403 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1404 if (native_encode_expr (ctor, buf, size, off) > 0)
1405 return native_interpret_expr (ref->type, buf, size);
1410 return NULL_TREE;
1413 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1414 structures into their value numbers. This is done in-place, and
1415 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1416 whether any operands were valueized. */
1418 static vec<vn_reference_op_s>
1419 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1421 vn_reference_op_t vro;
1422 unsigned int i;
1424 *valueized_anything = false;
1426 FOR_EACH_VEC_ELT (orig, i, vro)
1428 if (vro->opcode == SSA_NAME
1429 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1431 tree tem = SSA_VAL (vro->op0);
1432 if (tem != vro->op0)
1434 *valueized_anything = true;
1435 vro->op0 = tem;
1437 /* If it transforms from an SSA_NAME to a constant, update
1438 the opcode. */
1439 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1440 vro->opcode = TREE_CODE (vro->op0);
1442 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1444 tree tem = SSA_VAL (vro->op1);
1445 if (tem != vro->op1)
1447 *valueized_anything = true;
1448 vro->op1 = tem;
1451 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1453 tree tem = SSA_VAL (vro->op2);
1454 if (tem != vro->op2)
1456 *valueized_anything = true;
1457 vro->op2 = tem;
1460 /* If it transforms from an SSA_NAME to an address, fold with
1461 a preceding indirect reference. */
1462 if (i > 0
1463 && vro->op0
1464 && TREE_CODE (vro->op0) == ADDR_EXPR
1465 && orig[i - 1].opcode == MEM_REF)
1466 vn_reference_fold_indirect (&orig, &i);
1467 else if (i > 0
1468 && vro->opcode == SSA_NAME
1469 && orig[i - 1].opcode == MEM_REF)
1470 vn_reference_maybe_forwprop_address (&orig, &i);
1471 /* If it transforms a non-constant ARRAY_REF into a constant
1472 one, adjust the constant offset. */
1473 else if (vro->opcode == ARRAY_REF
1474 && vro->off == -1
1475 && TREE_CODE (vro->op0) == INTEGER_CST
1476 && TREE_CODE (vro->op1) == INTEGER_CST
1477 && TREE_CODE (vro->op2) == INTEGER_CST)
1479 offset_int off = ((wi::to_offset (vro->op0)
1480 - wi::to_offset (vro->op1))
1481 * wi::to_offset (vro->op2));
1482 if (wi::fits_shwi_p (off))
1483 vro->off = off.to_shwi ();
1487 return orig;
1490 static vec<vn_reference_op_s>
1491 valueize_refs (vec<vn_reference_op_s> orig)
1493 bool tem;
1494 return valueize_refs_1 (orig, &tem);
1497 static vec<vn_reference_op_s> shared_lookup_references;
1499 /* Create a vector of vn_reference_op_s structures from REF, a
1500 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1501 this function. *VALUEIZED_ANYTHING will specify whether any
1502 operands were valueized. */
1504 static vec<vn_reference_op_s>
1505 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1507 if (!ref)
1508 return vNULL;
1509 shared_lookup_references.truncate (0);
1510 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1511 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1512 valueized_anything);
1513 return shared_lookup_references;
1516 /* Create a vector of vn_reference_op_s structures from CALL, a
1517 call statement. The vector is shared among all callers of
1518 this function. */
1520 static vec<vn_reference_op_s>
1521 valueize_shared_reference_ops_from_call (gcall *call)
1523 if (!call)
1524 return vNULL;
1525 shared_lookup_references.truncate (0);
1526 copy_reference_ops_from_call (call, &shared_lookup_references);
1527 shared_lookup_references = valueize_refs (shared_lookup_references);
1528 return shared_lookup_references;
1531 /* Lookup a SCCVN reference operation VR in the current hash table.
1532 Returns the resulting value number if it exists in the hash table,
1533 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1534 vn_reference_t stored in the hashtable if something is found. */
1536 static tree
1537 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1539 vn_reference_s **slot;
1540 hashval_t hash;
1542 hash = vr->hashcode;
1543 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1544 if (!slot && current_info == optimistic_info)
1545 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1546 if (slot)
1548 if (vnresult)
1549 *vnresult = (vn_reference_t)*slot;
1550 return ((vn_reference_t)*slot)->result;
1553 return NULL_TREE;
1556 static tree *last_vuse_ptr;
1557 static vn_lookup_kind vn_walk_kind;
1558 static vn_lookup_kind default_vn_walk_kind;
1560 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1561 with the current VUSE and performs the expression lookup. */
1563 static void *
1564 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1565 unsigned int cnt, void *vr_)
1567 vn_reference_t vr = (vn_reference_t)vr_;
1568 vn_reference_s **slot;
1569 hashval_t hash;
1571 /* This bounds the stmt walks we perform on reference lookups
1572 to O(1) instead of O(N) where N is the number of dominating
1573 stores. */
1574 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1575 return (void *)-1;
1577 if (last_vuse_ptr)
1578 *last_vuse_ptr = vuse;
1580 /* Fixup vuse and hash. */
1581 if (vr->vuse)
1582 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1583 vr->vuse = vuse_ssa_val (vuse);
1584 if (vr->vuse)
1585 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1587 hash = vr->hashcode;
1588 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1589 if (!slot && current_info == optimistic_info)
1590 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1591 if (slot)
1592 return *slot;
1594 return NULL;
1597 /* Lookup an existing or insert a new vn_reference entry into the
1598 value table for the VUSE, SET, TYPE, OPERANDS reference which
1599 has the value VALUE which is either a constant or an SSA name. */
1601 static vn_reference_t
1602 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1603 alias_set_type set,
1604 tree type,
1605 vec<vn_reference_op_s,
1606 va_heap> operands,
1607 tree value)
1609 struct vn_reference_s vr1;
1610 vn_reference_t result;
1611 unsigned value_id;
1612 vr1.vuse = vuse;
1613 vr1.operands = operands;
1614 vr1.type = type;
1615 vr1.set = set;
1616 vr1.hashcode = vn_reference_compute_hash (&vr1);
1617 if (vn_reference_lookup_1 (&vr1, &result))
1618 return result;
1619 if (TREE_CODE (value) == SSA_NAME)
1620 value_id = VN_INFO (value)->value_id;
1621 else
1622 value_id = get_or_alloc_constant_value_id (value);
1623 return vn_reference_insert_pieces (vuse, set, type,
1624 operands.copy (), value, value_id);
1627 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1628 from the statement defining VUSE and if not successful tries to
1629 translate *REFP and VR_ through an aggregate copy at the definition
1630 of VUSE. */
1632 static void *
1633 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1634 bool disambiguate_only)
1636 vn_reference_t vr = (vn_reference_t)vr_;
1637 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1638 tree base;
1639 HOST_WIDE_INT offset, maxsize;
1640 static vec<vn_reference_op_s>
1641 lhs_ops = vNULL;
1642 ao_ref lhs_ref;
1643 bool lhs_ref_ok = false;
1645 /* First try to disambiguate after value-replacing in the definitions LHS. */
1646 if (is_gimple_assign (def_stmt))
1648 vec<vn_reference_op_s> tem;
1649 tree lhs = gimple_assign_lhs (def_stmt);
1650 bool valueized_anything = false;
1651 /* Avoid re-allocation overhead. */
1652 lhs_ops.truncate (0);
1653 copy_reference_ops_from_ref (lhs, &lhs_ops);
1654 tem = lhs_ops;
1655 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1656 gcc_assert (lhs_ops == tem);
1657 if (valueized_anything)
1659 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1660 get_alias_set (lhs),
1661 TREE_TYPE (lhs), lhs_ops);
1662 if (lhs_ref_ok
1663 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1664 return NULL;
1666 else
1668 ao_ref_init (&lhs_ref, lhs);
1669 lhs_ref_ok = true;
1672 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1673 && gimple_call_num_args (def_stmt) <= 4)
1675 /* For builtin calls valueize its arguments and call the
1676 alias oracle again. Valueization may improve points-to
1677 info of pointers and constify size and position arguments.
1678 Originally this was motivated by PR61034 which has
1679 conditional calls to free falsely clobbering ref because
1680 of imprecise points-to info of the argument. */
1681 tree oldargs[4];
1682 bool valueized_anything = false;
1683 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1685 oldargs[i] = gimple_call_arg (def_stmt, i);
1686 if (TREE_CODE (oldargs[i]) == SSA_NAME
1687 && VN_INFO (oldargs[i])->valnum != oldargs[i])
1689 gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum);
1690 valueized_anything = true;
1693 if (valueized_anything)
1695 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
1696 ref);
1697 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1698 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1699 if (!res)
1700 return NULL;
1704 if (disambiguate_only)
1705 return (void *)-1;
1707 base = ao_ref_base (ref);
1708 offset = ref->offset;
1709 maxsize = ref->max_size;
1711 /* If we cannot constrain the size of the reference we cannot
1712 test if anything kills it. */
1713 if (maxsize == -1)
1714 return (void *)-1;
1716 /* We can't deduce anything useful from clobbers. */
1717 if (gimple_clobber_p (def_stmt))
1718 return (void *)-1;
1720 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1721 from that definition.
1722 1) Memset. */
1723 if (is_gimple_reg_type (vr->type)
1724 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1725 && integer_zerop (gimple_call_arg (def_stmt, 1))
1726 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))
1727 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1729 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1730 tree base2;
1731 HOST_WIDE_INT offset2, size2, maxsize2;
1732 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1733 size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8;
1734 if ((unsigned HOST_WIDE_INT)size2 / 8
1735 == tree_to_uhwi (gimple_call_arg (def_stmt, 2))
1736 && maxsize2 != -1
1737 && operand_equal_p (base, base2, 0)
1738 && offset2 <= offset
1739 && offset2 + size2 >= offset + maxsize)
1741 tree val = build_zero_cst (vr->type);
1742 return vn_reference_lookup_or_insert_for_pieces
1743 (vuse, vr->set, vr->type, vr->operands, val);
1747 /* 2) Assignment from an empty CONSTRUCTOR. */
1748 else if (is_gimple_reg_type (vr->type)
1749 && gimple_assign_single_p (def_stmt)
1750 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1751 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1753 tree base2;
1754 HOST_WIDE_INT offset2, size2, maxsize2;
1755 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1756 &offset2, &size2, &maxsize2);
1757 if (maxsize2 != -1
1758 && operand_equal_p (base, base2, 0)
1759 && offset2 <= offset
1760 && offset2 + size2 >= offset + maxsize)
1762 tree val = build_zero_cst (vr->type);
1763 return vn_reference_lookup_or_insert_for_pieces
1764 (vuse, vr->set, vr->type, vr->operands, val);
1768 /* 3) Assignment from a constant. We can use folds native encode/interpret
1769 routines to extract the assigned bits. */
1770 else if (vn_walk_kind == VN_WALKREWRITE
1771 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1772 && ref->size == maxsize
1773 && maxsize % BITS_PER_UNIT == 0
1774 && offset % BITS_PER_UNIT == 0
1775 && is_gimple_reg_type (vr->type)
1776 && gimple_assign_single_p (def_stmt)
1777 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1779 tree base2;
1780 HOST_WIDE_INT offset2, size2, maxsize2;
1781 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1782 &offset2, &size2, &maxsize2);
1783 if (maxsize2 != -1
1784 && maxsize2 == size2
1785 && size2 % BITS_PER_UNIT == 0
1786 && offset2 % BITS_PER_UNIT == 0
1787 && operand_equal_p (base, base2, 0)
1788 && offset2 <= offset
1789 && offset2 + size2 >= offset + maxsize)
1791 /* We support up to 512-bit values (for V8DFmode). */
1792 unsigned char buffer[64];
1793 int len;
1795 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1796 buffer, sizeof (buffer));
1797 if (len > 0)
1799 tree val = native_interpret_expr (vr->type,
1800 buffer
1801 + ((offset - offset2)
1802 / BITS_PER_UNIT),
1803 ref->size / BITS_PER_UNIT);
1804 if (val)
1805 return vn_reference_lookup_or_insert_for_pieces
1806 (vuse, vr->set, vr->type, vr->operands, val);
1811 /* 4) Assignment from an SSA name which definition we may be able
1812 to access pieces from. */
1813 else if (ref->size == maxsize
1814 && is_gimple_reg_type (vr->type)
1815 && gimple_assign_single_p (def_stmt)
1816 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1818 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1819 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1820 if (is_gimple_assign (def_stmt2)
1821 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1822 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1823 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1825 tree base2;
1826 HOST_WIDE_INT offset2, size2, maxsize2, off;
1827 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1828 &offset2, &size2, &maxsize2);
1829 off = offset - offset2;
1830 if (maxsize2 != -1
1831 && maxsize2 == size2
1832 && operand_equal_p (base, base2, 0)
1833 && offset2 <= offset
1834 && offset2 + size2 >= offset + maxsize)
1836 tree val = NULL_TREE;
1837 HOST_WIDE_INT elsz
1838 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1839 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1841 if (off == 0)
1842 val = gimple_assign_rhs1 (def_stmt2);
1843 else if (off == elsz)
1844 val = gimple_assign_rhs2 (def_stmt2);
1846 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1847 && off % elsz == 0)
1849 tree ctor = gimple_assign_rhs1 (def_stmt2);
1850 unsigned i = off / elsz;
1851 if (i < CONSTRUCTOR_NELTS (ctor))
1853 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1854 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1856 if (TREE_CODE (TREE_TYPE (elt->value))
1857 != VECTOR_TYPE)
1858 val = elt->value;
1862 if (val)
1863 return vn_reference_lookup_or_insert_for_pieces
1864 (vuse, vr->set, vr->type, vr->operands, val);
1869 /* 5) For aggregate copies translate the reference through them if
1870 the copy kills ref. */
1871 else if (vn_walk_kind == VN_WALKREWRITE
1872 && gimple_assign_single_p (def_stmt)
1873 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1874 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1875 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1877 tree base2;
1878 HOST_WIDE_INT offset2, size2, maxsize2;
1879 int i, j;
1880 auto_vec<vn_reference_op_s> rhs;
1881 vn_reference_op_t vro;
1882 ao_ref r;
1884 if (!lhs_ref_ok)
1885 return (void *)-1;
1887 /* See if the assignment kills REF. */
1888 base2 = ao_ref_base (&lhs_ref);
1889 offset2 = lhs_ref.offset;
1890 size2 = lhs_ref.size;
1891 maxsize2 = lhs_ref.max_size;
1892 if (maxsize2 == -1
1893 || (base != base2 && !operand_equal_p (base, base2, 0))
1894 || offset2 > offset
1895 || offset2 + size2 < offset + maxsize)
1896 return (void *)-1;
1898 /* Find the common base of ref and the lhs. lhs_ops already
1899 contains valueized operands for the lhs. */
1900 i = vr->operands.length () - 1;
1901 j = lhs_ops.length () - 1;
1902 while (j >= 0 && i >= 0
1903 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1905 i--;
1906 j--;
1909 /* ??? The innermost op should always be a MEM_REF and we already
1910 checked that the assignment to the lhs kills vr. Thus for
1911 aggregate copies using char[] types the vn_reference_op_eq
1912 may fail when comparing types for compatibility. But we really
1913 don't care here - further lookups with the rewritten operands
1914 will simply fail if we messed up types too badly. */
1915 HOST_WIDE_INT extra_off = 0;
1916 if (j == 0 && i >= 0
1917 && lhs_ops[0].opcode == MEM_REF
1918 && lhs_ops[0].off != -1)
1920 if (lhs_ops[0].off == vr->operands[i].off)
1921 i--, j--;
1922 else if (vr->operands[i].opcode == MEM_REF
1923 && vr->operands[i].off != -1)
1925 extra_off = vr->operands[i].off - lhs_ops[0].off;
1926 i--, j--;
1930 /* i now points to the first additional op.
1931 ??? LHS may not be completely contained in VR, one or more
1932 VIEW_CONVERT_EXPRs could be in its way. We could at least
1933 try handling outermost VIEW_CONVERT_EXPRs. */
1934 if (j != -1)
1935 return (void *)-1;
1937 /* Now re-write REF to be based on the rhs of the assignment. */
1938 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1940 /* Apply an extra offset to the inner MEM_REF of the RHS. */
1941 if (extra_off != 0)
1943 if (rhs.length () < 2
1944 || rhs[0].opcode != MEM_REF
1945 || rhs[0].off == -1)
1946 return (void *)-1;
1947 rhs[0].off += extra_off;
1948 rhs[0].op0 = int_const_binop (PLUS_EXPR, rhs[0].op0,
1949 build_int_cst (TREE_TYPE (rhs[0].op0),
1950 extra_off));
1953 /* We need to pre-pend vr->operands[0..i] to rhs. */
1954 vec<vn_reference_op_s> old = vr->operands;
1955 if (i + 1 + rhs.length () > vr->operands.length ())
1957 vr->operands.safe_grow (i + 1 + rhs.length ());
1958 if (old == shared_lookup_references)
1959 shared_lookup_references = vr->operands;
1961 else
1962 vr->operands.truncate (i + 1 + rhs.length ());
1963 FOR_EACH_VEC_ELT (rhs, j, vro)
1964 vr->operands[i + 1 + j] = *vro;
1965 vr->operands = valueize_refs (vr->operands);
1966 if (old == shared_lookup_references)
1967 shared_lookup_references = vr->operands;
1968 vr->hashcode = vn_reference_compute_hash (vr);
1970 /* Try folding the new reference to a constant. */
1971 tree val = fully_constant_vn_reference_p (vr);
1972 if (val)
1973 return vn_reference_lookup_or_insert_for_pieces
1974 (vuse, vr->set, vr->type, vr->operands, val);
1976 /* Adjust *ref from the new operands. */
1977 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1978 return (void *)-1;
1979 /* This can happen with bitfields. */
1980 if (ref->size != r.size)
1981 return (void *)-1;
1982 *ref = r;
1984 /* Do not update last seen VUSE after translating. */
1985 last_vuse_ptr = NULL;
1987 /* Keep looking for the adjusted *REF / VR pair. */
1988 return NULL;
1991 /* 6) For memcpy copies translate the reference through them if
1992 the copy kills ref. */
1993 else if (vn_walk_kind == VN_WALKREWRITE
1994 && is_gimple_reg_type (vr->type)
1995 /* ??? Handle BCOPY as well. */
1996 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1997 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1998 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1999 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2000 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
2001 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
2002 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
2003 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)))
2005 tree lhs, rhs;
2006 ao_ref r;
2007 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
2008 vn_reference_op_s op;
2009 HOST_WIDE_INT at;
2012 /* Only handle non-variable, addressable refs. */
2013 if (ref->size != maxsize
2014 || offset % BITS_PER_UNIT != 0
2015 || ref->size % BITS_PER_UNIT != 0)
2016 return (void *)-1;
2018 /* Extract a pointer base and an offset for the destination. */
2019 lhs = gimple_call_arg (def_stmt, 0);
2020 lhs_offset = 0;
2021 if (TREE_CODE (lhs) == SSA_NAME)
2022 lhs = SSA_VAL (lhs);
2023 if (TREE_CODE (lhs) == ADDR_EXPR)
2025 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
2026 &lhs_offset);
2027 if (!tem)
2028 return (void *)-1;
2029 if (TREE_CODE (tem) == MEM_REF
2030 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2032 lhs = TREE_OPERAND (tem, 0);
2033 lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2035 else if (DECL_P (tem))
2036 lhs = build_fold_addr_expr (tem);
2037 else
2038 return (void *)-1;
2040 if (TREE_CODE (lhs) != SSA_NAME
2041 && TREE_CODE (lhs) != ADDR_EXPR)
2042 return (void *)-1;
2044 /* Extract a pointer base and an offset for the source. */
2045 rhs = gimple_call_arg (def_stmt, 1);
2046 rhs_offset = 0;
2047 if (TREE_CODE (rhs) == SSA_NAME)
2048 rhs = SSA_VAL (rhs);
2049 if (TREE_CODE (rhs) == ADDR_EXPR)
2051 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
2052 &rhs_offset);
2053 if (!tem)
2054 return (void *)-1;
2055 if (TREE_CODE (tem) == MEM_REF
2056 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2058 rhs = TREE_OPERAND (tem, 0);
2059 rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2061 else if (DECL_P (tem))
2062 rhs = build_fold_addr_expr (tem);
2063 else
2064 return (void *)-1;
2066 if (TREE_CODE (rhs) != SSA_NAME
2067 && TREE_CODE (rhs) != ADDR_EXPR)
2068 return (void *)-1;
2070 copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2));
2072 /* The bases of the destination and the references have to agree. */
2073 if ((TREE_CODE (base) != MEM_REF
2074 && !DECL_P (base))
2075 || (TREE_CODE (base) == MEM_REF
2076 && (TREE_OPERAND (base, 0) != lhs
2077 || !tree_fits_uhwi_p (TREE_OPERAND (base, 1))))
2078 || (DECL_P (base)
2079 && (TREE_CODE (lhs) != ADDR_EXPR
2080 || TREE_OPERAND (lhs, 0) != base)))
2081 return (void *)-1;
2083 /* And the access has to be contained within the memcpy destination. */
2084 at = offset / BITS_PER_UNIT;
2085 if (TREE_CODE (base) == MEM_REF)
2086 at += tree_to_uhwi (TREE_OPERAND (base, 1));
2087 if (lhs_offset > at
2088 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
2089 return (void *)-1;
2091 /* Make room for 2 operands in the new reference. */
2092 if (vr->operands.length () < 2)
2094 vec<vn_reference_op_s> old = vr->operands;
2095 vr->operands.safe_grow_cleared (2);
2096 if (old == shared_lookup_references
2097 && vr->operands != old)
2098 shared_lookup_references = vr->operands;
2100 else
2101 vr->operands.truncate (2);
2103 /* The looked-through reference is a simple MEM_REF. */
2104 memset (&op, 0, sizeof (op));
2105 op.type = vr->type;
2106 op.opcode = MEM_REF;
2107 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
2108 op.off = at - lhs_offset + rhs_offset;
2109 vr->operands[0] = op;
2110 op.type = TREE_TYPE (rhs);
2111 op.opcode = TREE_CODE (rhs);
2112 op.op0 = rhs;
2113 op.off = -1;
2114 vr->operands[1] = op;
2115 vr->hashcode = vn_reference_compute_hash (vr);
2117 /* Adjust *ref from the new operands. */
2118 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2119 return (void *)-1;
2120 /* This can happen with bitfields. */
2121 if (ref->size != r.size)
2122 return (void *)-1;
2123 *ref = r;
2125 /* Do not update last seen VUSE after translating. */
2126 last_vuse_ptr = NULL;
2128 /* Keep looking for the adjusted *REF / VR pair. */
2129 return NULL;
2132 /* Bail out and stop walking. */
2133 return (void *)-1;
2136 /* Lookup a reference operation by it's parts, in the current hash table.
2137 Returns the resulting value number if it exists in the hash table,
2138 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2139 vn_reference_t stored in the hashtable if something is found. */
2141 tree
2142 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2143 vec<vn_reference_op_s> operands,
2144 vn_reference_t *vnresult, vn_lookup_kind kind)
2146 struct vn_reference_s vr1;
2147 vn_reference_t tmp;
2148 tree cst;
2150 if (!vnresult)
2151 vnresult = &tmp;
2152 *vnresult = NULL;
2154 vr1.vuse = vuse_ssa_val (vuse);
2155 shared_lookup_references.truncate (0);
2156 shared_lookup_references.safe_grow (operands.length ());
2157 memcpy (shared_lookup_references.address (),
2158 operands.address (),
2159 sizeof (vn_reference_op_s)
2160 * operands.length ());
2161 vr1.operands = operands = shared_lookup_references
2162 = valueize_refs (shared_lookup_references);
2163 vr1.type = type;
2164 vr1.set = set;
2165 vr1.hashcode = vn_reference_compute_hash (&vr1);
2166 if ((cst = fully_constant_vn_reference_p (&vr1)))
2167 return cst;
2169 vn_reference_lookup_1 (&vr1, vnresult);
2170 if (!*vnresult
2171 && kind != VN_NOWALK
2172 && vr1.vuse)
2174 ao_ref r;
2175 vn_walk_kind = kind;
2176 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2177 *vnresult =
2178 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2179 vn_reference_lookup_2,
2180 vn_reference_lookup_3,
2181 vuse_ssa_val, &vr1);
2182 gcc_checking_assert (vr1.operands == shared_lookup_references);
2185 if (*vnresult)
2186 return (*vnresult)->result;
2188 return NULL_TREE;
2191 /* Lookup OP in the current hash table, and return the resulting value
2192 number if it exists in the hash table. Return NULL_TREE if it does
2193 not exist in the hash table or if the result field of the structure
2194 was NULL.. VNRESULT will be filled in with the vn_reference_t
2195 stored in the hashtable if one exists. */
2197 tree
2198 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2199 vn_reference_t *vnresult)
2201 vec<vn_reference_op_s> operands;
2202 struct vn_reference_s vr1;
2203 tree cst;
2204 bool valuezied_anything;
2206 if (vnresult)
2207 *vnresult = NULL;
2209 vr1.vuse = vuse_ssa_val (vuse);
2210 vr1.operands = operands
2211 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2212 vr1.type = TREE_TYPE (op);
2213 vr1.set = get_alias_set (op);
2214 vr1.hashcode = vn_reference_compute_hash (&vr1);
2215 if ((cst = fully_constant_vn_reference_p (&vr1)))
2216 return cst;
2218 if (kind != VN_NOWALK
2219 && vr1.vuse)
2221 vn_reference_t wvnresult;
2222 ao_ref r;
2223 /* Make sure to use a valueized reference if we valueized anything.
2224 Otherwise preserve the full reference for advanced TBAA. */
2225 if (!valuezied_anything
2226 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2227 vr1.operands))
2228 ao_ref_init (&r, op);
2229 vn_walk_kind = kind;
2230 wvnresult =
2231 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2232 vn_reference_lookup_2,
2233 vn_reference_lookup_3,
2234 vuse_ssa_val, &vr1);
2235 gcc_checking_assert (vr1.operands == shared_lookup_references);
2236 if (wvnresult)
2238 if (vnresult)
2239 *vnresult = wvnresult;
2240 return wvnresult->result;
2243 return NULL_TREE;
2246 return vn_reference_lookup_1 (&vr1, vnresult);
2249 /* Lookup CALL in the current hash table and return the entry in
2250 *VNRESULT if found. Populates *VR for the hashtable lookup. */
2252 void
2253 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
2254 vn_reference_t vr)
2256 if (vnresult)
2257 *vnresult = NULL;
2259 tree vuse = gimple_vuse (call);
2261 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2262 vr->operands = valueize_shared_reference_ops_from_call (call);
2263 vr->type = gimple_expr_type (call);
2264 vr->set = 0;
2265 vr->hashcode = vn_reference_compute_hash (vr);
2266 vn_reference_lookup_1 (vr, vnresult);
2269 /* Insert OP into the current hash table with a value number of
2270 RESULT, and return the resulting reference structure we created. */
2272 static vn_reference_t
2273 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2275 vn_reference_s **slot;
2276 vn_reference_t vr1;
2277 bool tem;
2279 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2280 if (TREE_CODE (result) == SSA_NAME)
2281 vr1->value_id = VN_INFO (result)->value_id;
2282 else
2283 vr1->value_id = get_or_alloc_constant_value_id (result);
2284 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2285 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2286 vr1->type = TREE_TYPE (op);
2287 vr1->set = get_alias_set (op);
2288 vr1->hashcode = vn_reference_compute_hash (vr1);
2289 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2290 vr1->result_vdef = vdef;
2292 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2293 INSERT);
2295 /* Because we lookup stores using vuses, and value number failures
2296 using the vdefs (see visit_reference_op_store for how and why),
2297 it's possible that on failure we may try to insert an already
2298 inserted store. This is not wrong, there is no ssa name for a
2299 store that we could use as a differentiator anyway. Thus, unlike
2300 the other lookup functions, you cannot gcc_assert (!*slot)
2301 here. */
2303 /* But free the old slot in case of a collision. */
2304 if (*slot)
2305 free_reference (*slot);
2307 *slot = vr1;
2308 return vr1;
2311 /* Insert a reference by it's pieces into the current hash table with
2312 a value number of RESULT. Return the resulting reference
2313 structure we created. */
2315 vn_reference_t
2316 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2317 vec<vn_reference_op_s> operands,
2318 tree result, unsigned int value_id)
2321 vn_reference_s **slot;
2322 vn_reference_t vr1;
2324 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2325 vr1->value_id = value_id;
2326 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2327 vr1->operands = valueize_refs (operands);
2328 vr1->type = type;
2329 vr1->set = set;
2330 vr1->hashcode = vn_reference_compute_hash (vr1);
2331 if (result && TREE_CODE (result) == SSA_NAME)
2332 result = SSA_VAL (result);
2333 vr1->result = result;
2335 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2336 INSERT);
2338 /* At this point we should have all the things inserted that we have
2339 seen before, and we should never try inserting something that
2340 already exists. */
2341 gcc_assert (!*slot);
2342 if (*slot)
2343 free_reference (*slot);
2345 *slot = vr1;
2346 return vr1;
2349 /* Compute and return the hash value for nary operation VBO1. */
2351 static hashval_t
2352 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2354 inchash::hash hstate;
2355 unsigned i;
2357 for (i = 0; i < vno1->length; ++i)
2358 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2359 vno1->op[i] = SSA_VAL (vno1->op[i]);
2361 if (vno1->length == 2
2362 && commutative_tree_code (vno1->opcode)
2363 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2365 tree temp = vno1->op[0];
2366 vno1->op[0] = vno1->op[1];
2367 vno1->op[1] = temp;
2370 hstate.add_int (vno1->opcode);
2371 for (i = 0; i < vno1->length; ++i)
2372 inchash::add_expr (vno1->op[i], hstate);
2374 return hstate.end ();
2377 /* Compare nary operations VNO1 and VNO2 and return true if they are
2378 equivalent. */
2380 bool
2381 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2383 unsigned i;
2385 if (vno1->hashcode != vno2->hashcode)
2386 return false;
2388 if (vno1->length != vno2->length)
2389 return false;
2391 if (vno1->opcode != vno2->opcode
2392 || !types_compatible_p (vno1->type, vno2->type))
2393 return false;
2395 for (i = 0; i < vno1->length; ++i)
2396 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2397 return false;
2399 return true;
2402 /* Initialize VNO from the pieces provided. */
2404 static void
2405 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2406 enum tree_code code, tree type, tree *ops)
2408 vno->opcode = code;
2409 vno->length = length;
2410 vno->type = type;
2411 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2414 /* Initialize VNO from OP. */
2416 static void
2417 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2419 unsigned i;
2421 vno->opcode = TREE_CODE (op);
2422 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2423 vno->type = TREE_TYPE (op);
2424 for (i = 0; i < vno->length; ++i)
2425 vno->op[i] = TREE_OPERAND (op, i);
2428 /* Return the number of operands for a vn_nary ops structure from STMT. */
2430 static unsigned int
2431 vn_nary_length_from_stmt (gimple stmt)
2433 switch (gimple_assign_rhs_code (stmt))
2435 case REALPART_EXPR:
2436 case IMAGPART_EXPR:
2437 case VIEW_CONVERT_EXPR:
2438 return 1;
2440 case BIT_FIELD_REF:
2441 return 3;
2443 case CONSTRUCTOR:
2444 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2446 default:
2447 return gimple_num_ops (stmt) - 1;
2451 /* Initialize VNO from STMT. */
2453 static void
2454 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2456 unsigned i;
2458 vno->opcode = gimple_assign_rhs_code (stmt);
2459 vno->type = gimple_expr_type (stmt);
2460 switch (vno->opcode)
2462 case REALPART_EXPR:
2463 case IMAGPART_EXPR:
2464 case VIEW_CONVERT_EXPR:
2465 vno->length = 1;
2466 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2467 break;
2469 case BIT_FIELD_REF:
2470 vno->length = 3;
2471 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2472 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2473 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2474 break;
2476 case CONSTRUCTOR:
2477 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2478 for (i = 0; i < vno->length; ++i)
2479 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2480 break;
2482 default:
2483 gcc_checking_assert (!gimple_assign_single_p (stmt));
2484 vno->length = gimple_num_ops (stmt) - 1;
2485 for (i = 0; i < vno->length; ++i)
2486 vno->op[i] = gimple_op (stmt, i + 1);
2490 /* Compute the hashcode for VNO and look for it in the hash table;
2491 return the resulting value number if it exists in the hash table.
2492 Return NULL_TREE if it does not exist in the hash table or if the
2493 result field of the operation is NULL. VNRESULT will contain the
2494 vn_nary_op_t from the hashtable if it exists. */
2496 static tree
2497 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2499 vn_nary_op_s **slot;
2501 if (vnresult)
2502 *vnresult = NULL;
2504 vno->hashcode = vn_nary_op_compute_hash (vno);
2505 slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode,
2506 NO_INSERT);
2507 if (!slot && current_info == optimistic_info)
2508 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode,
2509 NO_INSERT);
2510 if (!slot)
2511 return NULL_TREE;
2512 if (vnresult)
2513 *vnresult = *slot;
2514 return (*slot)->result;
2517 /* Lookup a n-ary operation by its pieces and return the resulting value
2518 number if it exists in the hash table. Return NULL_TREE if it does
2519 not exist in the hash table or if the result field of the operation
2520 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2521 if it exists. */
2523 tree
2524 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2525 tree type, tree *ops, vn_nary_op_t *vnresult)
2527 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2528 sizeof_vn_nary_op (length));
2529 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2530 return vn_nary_op_lookup_1 (vno1, vnresult);
2533 /* Lookup OP in the current hash table, and return the resulting value
2534 number if it exists in the hash table. Return NULL_TREE if it does
2535 not exist in the hash table or if the result field of the operation
2536 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2537 if it exists. */
2539 tree
2540 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2542 vn_nary_op_t vno1
2543 = XALLOCAVAR (struct vn_nary_op_s,
2544 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2545 init_vn_nary_op_from_op (vno1, op);
2546 return vn_nary_op_lookup_1 (vno1, vnresult);
2549 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2550 value number if it exists in the hash table. Return NULL_TREE if
2551 it does not exist in the hash table. VNRESULT will contain the
2552 vn_nary_op_t from the hashtable if it exists. */
2554 tree
2555 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2557 vn_nary_op_t vno1
2558 = XALLOCAVAR (struct vn_nary_op_s,
2559 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2560 init_vn_nary_op_from_stmt (vno1, stmt);
2561 return vn_nary_op_lookup_1 (vno1, vnresult);
2564 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2566 static vn_nary_op_t
2567 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2569 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2572 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2573 obstack. */
2575 static vn_nary_op_t
2576 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2578 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2579 &current_info->nary_obstack);
2581 vno1->value_id = value_id;
2582 vno1->length = length;
2583 vno1->result = result;
2585 return vno1;
2588 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2589 VNO->HASHCODE first. */
2591 static vn_nary_op_t
2592 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
2593 bool compute_hash)
2595 vn_nary_op_s **slot;
2597 if (compute_hash)
2598 vno->hashcode = vn_nary_op_compute_hash (vno);
2600 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
2601 gcc_assert (!*slot);
2603 *slot = vno;
2604 return vno;
2607 /* Insert a n-ary operation into the current hash table using it's
2608 pieces. Return the vn_nary_op_t structure we created and put in
2609 the hashtable. */
2611 vn_nary_op_t
2612 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2613 tree type, tree *ops,
2614 tree result, unsigned int value_id)
2616 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2617 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2618 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2621 /* Insert OP into the current hash table with a value number of
2622 RESULT. Return the vn_nary_op_t structure we created and put in
2623 the hashtable. */
2625 vn_nary_op_t
2626 vn_nary_op_insert (tree op, tree result)
2628 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2629 vn_nary_op_t vno1;
2631 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2632 init_vn_nary_op_from_op (vno1, op);
2633 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2636 /* Insert the rhs of STMT into the current hash table with a value number of
2637 RESULT. */
2639 vn_nary_op_t
2640 vn_nary_op_insert_stmt (gimple stmt, tree result)
2642 vn_nary_op_t vno1
2643 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2644 result, VN_INFO (result)->value_id);
2645 init_vn_nary_op_from_stmt (vno1, stmt);
2646 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2649 /* Compute a hashcode for PHI operation VP1 and return it. */
2651 static inline hashval_t
2652 vn_phi_compute_hash (vn_phi_t vp1)
2654 inchash::hash hstate (vp1->block->index);
2655 int i;
2656 tree phi1op;
2657 tree type;
2659 /* If all PHI arguments are constants we need to distinguish
2660 the PHI node via its type. */
2661 type = vp1->type;
2662 hstate.merge_hash (vn_hash_type (type));
2664 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2666 if (phi1op == VN_TOP)
2667 continue;
2668 inchash::add_expr (phi1op, hstate);
2671 return hstate.end ();
2674 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2676 static int
2677 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2679 if (vp1->hashcode != vp2->hashcode)
2680 return false;
2682 if (vp1->block == vp2->block)
2684 int i;
2685 tree phi1op;
2687 /* If the PHI nodes do not have compatible types
2688 they are not the same. */
2689 if (!types_compatible_p (vp1->type, vp2->type))
2690 return false;
2692 /* Any phi in the same block will have it's arguments in the
2693 same edge order, because of how we store phi nodes. */
2694 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2696 tree phi2op = vp2->phiargs[i];
2697 if (phi1op == VN_TOP || phi2op == VN_TOP)
2698 continue;
2699 if (!expressions_equal_p (phi1op, phi2op))
2700 return false;
2702 return true;
2704 return false;
2707 static vec<tree> shared_lookup_phiargs;
2709 /* Lookup PHI in the current hash table, and return the resulting
2710 value number if it exists in the hash table. Return NULL_TREE if
2711 it does not exist in the hash table. */
2713 static tree
2714 vn_phi_lookup (gimple phi)
2716 vn_phi_s **slot;
2717 struct vn_phi_s vp1;
2718 unsigned i;
2720 shared_lookup_phiargs.truncate (0);
2722 /* Canonicalize the SSA_NAME's to their value number. */
2723 for (i = 0; i < gimple_phi_num_args (phi); i++)
2725 tree def = PHI_ARG_DEF (phi, i);
2726 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2727 shared_lookup_phiargs.safe_push (def);
2729 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2730 vp1.phiargs = shared_lookup_phiargs;
2731 vp1.block = gimple_bb (phi);
2732 vp1.hashcode = vn_phi_compute_hash (&vp1);
2733 slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2734 NO_INSERT);
2735 if (!slot && current_info == optimistic_info)
2736 slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2737 NO_INSERT);
2738 if (!slot)
2739 return NULL_TREE;
2740 return (*slot)->result;
2743 /* Insert PHI into the current hash table with a value number of
2744 RESULT. */
2746 static vn_phi_t
2747 vn_phi_insert (gimple phi, tree result)
2749 vn_phi_s **slot;
2750 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2751 unsigned i;
2752 vec<tree> args = vNULL;
2754 /* Canonicalize the SSA_NAME's to their value number. */
2755 for (i = 0; i < gimple_phi_num_args (phi); i++)
2757 tree def = PHI_ARG_DEF (phi, i);
2758 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2759 args.safe_push (def);
2761 vp1->value_id = VN_INFO (result)->value_id;
2762 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2763 vp1->phiargs = args;
2764 vp1->block = gimple_bb (phi);
2765 vp1->result = result;
2766 vp1->hashcode = vn_phi_compute_hash (vp1);
2768 slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2770 /* Because we iterate over phi operations more than once, it's
2771 possible the slot might already exist here, hence no assert.*/
2772 *slot = vp1;
2773 return vp1;
2777 /* Print set of components in strongly connected component SCC to OUT. */
2779 static void
2780 print_scc (FILE *out, vec<tree> scc)
2782 tree var;
2783 unsigned int i;
2785 fprintf (out, "SCC consists of:");
2786 FOR_EACH_VEC_ELT (scc, i, var)
2788 fprintf (out, " ");
2789 print_generic_expr (out, var, 0);
2791 fprintf (out, "\n");
2794 /* Set the value number of FROM to TO, return true if it has changed
2795 as a result. */
2797 static inline bool
2798 set_ssa_val_to (tree from, tree to)
2800 tree currval = SSA_VAL (from);
2801 HOST_WIDE_INT toff, coff;
2803 /* The only thing we allow as value numbers are ssa_names
2804 and invariants. So assert that here. We don't allow VN_TOP
2805 as visiting a stmt should produce a value-number other than
2806 that.
2807 ??? Still VN_TOP can happen for unreachable code, so force
2808 it to varying in that case. Not all code is prepared to
2809 get VN_TOP on valueization. */
2810 if (to == VN_TOP)
2812 if (dump_file && (dump_flags & TDF_DETAILS))
2813 fprintf (dump_file, "Forcing value number to varying on "
2814 "receiving VN_TOP\n");
2815 to = from;
2818 gcc_assert (to != NULL_TREE
2819 && (TREE_CODE (to) == SSA_NAME
2820 || is_gimple_min_invariant (to)));
2822 if (from != to)
2824 if (currval == from)
2826 if (dump_file && (dump_flags & TDF_DETAILS))
2828 fprintf (dump_file, "Not changing value number of ");
2829 print_generic_expr (dump_file, from, 0);
2830 fprintf (dump_file, " from VARYING to ");
2831 print_generic_expr (dump_file, to, 0);
2832 fprintf (dump_file, "\n");
2834 return false;
2836 else if (TREE_CODE (to) == SSA_NAME
2837 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2838 to = from;
2841 if (dump_file && (dump_flags & TDF_DETAILS))
2843 fprintf (dump_file, "Setting value number of ");
2844 print_generic_expr (dump_file, from, 0);
2845 fprintf (dump_file, " to ");
2846 print_generic_expr (dump_file, to, 0);
2849 if (currval != to
2850 && !operand_equal_p (currval, to, 0)
2851 /* ??? For addresses involving volatile objects or types operand_equal_p
2852 does not reliably detect ADDR_EXPRs as equal. We know we are only
2853 getting invariant gimple addresses here, so can use
2854 get_addr_base_and_unit_offset to do this comparison. */
2855 && !(TREE_CODE (currval) == ADDR_EXPR
2856 && TREE_CODE (to) == ADDR_EXPR
2857 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2858 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2859 && coff == toff))
2861 VN_INFO (from)->valnum = to;
2862 if (dump_file && (dump_flags & TDF_DETAILS))
2863 fprintf (dump_file, " (changed)\n");
2864 return true;
2866 if (dump_file && (dump_flags & TDF_DETAILS))
2867 fprintf (dump_file, "\n");
2868 return false;
2871 /* Mark as processed all the definitions in the defining stmt of USE, or
2872 the USE itself. */
2874 static void
2875 mark_use_processed (tree use)
2877 ssa_op_iter iter;
2878 def_operand_p defp;
2879 gimple stmt = SSA_NAME_DEF_STMT (use);
2881 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2883 VN_INFO (use)->use_processed = true;
2884 return;
2887 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2889 tree def = DEF_FROM_PTR (defp);
2891 VN_INFO (def)->use_processed = true;
2895 /* Set all definitions in STMT to value number to themselves.
2896 Return true if a value number changed. */
2898 static bool
2899 defs_to_varying (gimple stmt)
2901 bool changed = false;
2902 ssa_op_iter iter;
2903 def_operand_p defp;
2905 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2907 tree def = DEF_FROM_PTR (defp);
2908 changed |= set_ssa_val_to (def, def);
2910 return changed;
2913 static bool expr_has_constants (tree expr);
2915 /* Visit a copy between LHS and RHS, return true if the value number
2916 changed. */
2918 static bool
2919 visit_copy (tree lhs, tree rhs)
2921 /* The copy may have a more interesting constant filled expression
2922 (we don't, since we know our RHS is just an SSA name). */
2923 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2924 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2926 /* And finally valueize. */
2927 rhs = SSA_VAL (rhs);
2929 return set_ssa_val_to (lhs, rhs);
2932 /* Visit a nary operator RHS, value number it, and return true if the
2933 value number of LHS has changed as a result. */
2935 static bool
2936 visit_nary_op (tree lhs, gimple stmt)
2938 bool changed = false;
2939 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2941 if (result)
2942 changed = set_ssa_val_to (lhs, result);
2943 else
2945 changed = set_ssa_val_to (lhs, lhs);
2946 vn_nary_op_insert_stmt (stmt, lhs);
2949 return changed;
2952 /* Visit a call STMT storing into LHS. Return true if the value number
2953 of the LHS has changed as a result. */
2955 static bool
2956 visit_reference_op_call (tree lhs, gcall *stmt)
2958 bool changed = false;
2959 struct vn_reference_s vr1;
2960 vn_reference_t vnresult = NULL;
2961 tree vdef = gimple_vdef (stmt);
2963 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2964 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2965 lhs = NULL_TREE;
2967 vn_reference_lookup_call (stmt, &vnresult, &vr1);
2968 if (vnresult)
2970 if (vnresult->result_vdef && vdef)
2971 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2973 if (!vnresult->result && lhs)
2974 vnresult->result = lhs;
2976 if (vnresult->result && lhs)
2978 changed |= set_ssa_val_to (lhs, vnresult->result);
2980 if (VN_INFO (vnresult->result)->has_constants)
2981 VN_INFO (lhs)->has_constants = true;
2984 else
2986 vn_reference_t vr2;
2987 vn_reference_s **slot;
2988 if (vdef)
2989 changed |= set_ssa_val_to (vdef, vdef);
2990 if (lhs)
2991 changed |= set_ssa_val_to (lhs, lhs);
2992 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2993 vr2->vuse = vr1.vuse;
2994 /* As we are not walking the virtual operand chain we know the
2995 shared_lookup_references are still original so we can re-use
2996 them here. */
2997 vr2->operands = vr1.operands.copy ();
2998 vr2->type = vr1.type;
2999 vr2->set = vr1.set;
3000 vr2->hashcode = vr1.hashcode;
3001 vr2->result = lhs;
3002 vr2->result_vdef = vdef;
3003 slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode,
3004 INSERT);
3005 gcc_assert (!*slot);
3006 *slot = vr2;
3009 return changed;
3012 /* Visit a load from a reference operator RHS, part of STMT, value number it,
3013 and return true if the value number of the LHS has changed as a result. */
3015 static bool
3016 visit_reference_op_load (tree lhs, tree op, gimple stmt)
3018 bool changed = false;
3019 tree last_vuse;
3020 tree result;
3022 last_vuse = gimple_vuse (stmt);
3023 last_vuse_ptr = &last_vuse;
3024 result = vn_reference_lookup (op, gimple_vuse (stmt),
3025 default_vn_walk_kind, NULL);
3026 last_vuse_ptr = NULL;
3028 /* We handle type-punning through unions by value-numbering based
3029 on offset and size of the access. Be prepared to handle a
3030 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
3031 if (result
3032 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
3034 /* We will be setting the value number of lhs to the value number
3035 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
3036 So first simplify and lookup this expression to see if it
3037 is already available. */
3038 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
3039 if ((CONVERT_EXPR_P (val)
3040 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
3041 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
3043 tree tem = vn_get_expr_for (TREE_OPERAND (val, 0));
3044 if ((CONVERT_EXPR_P (tem)
3045 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
3046 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
3047 TREE_TYPE (val), tem)))
3048 val = tem;
3050 result = val;
3051 if (!is_gimple_min_invariant (val)
3052 && TREE_CODE (val) != SSA_NAME)
3053 result = vn_nary_op_lookup (val, NULL);
3054 /* If the expression is not yet available, value-number lhs to
3055 a new SSA_NAME we create. */
3056 if (!result)
3058 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
3059 "vntemp");
3060 /* Initialize value-number information properly. */
3061 VN_INFO_GET (result)->valnum = result;
3062 VN_INFO (result)->value_id = get_next_value_id ();
3063 VN_INFO (result)->expr = val;
3064 VN_INFO (result)->has_constants = expr_has_constants (val);
3065 VN_INFO (result)->needs_insertion = true;
3066 /* As all "inserted" statements are singleton SCCs, insert
3067 to the valid table. This is strictly needed to
3068 avoid re-generating new value SSA_NAMEs for the same
3069 expression during SCC iteration over and over (the
3070 optimistic table gets cleared after each iteration).
3071 We do not need to insert into the optimistic table, as
3072 lookups there will fall back to the valid table. */
3073 if (current_info == optimistic_info)
3075 current_info = valid_info;
3076 vn_nary_op_insert (val, result);
3077 current_info = optimistic_info;
3079 else
3080 vn_nary_op_insert (val, result);
3081 if (dump_file && (dump_flags & TDF_DETAILS))
3083 fprintf (dump_file, "Inserting name ");
3084 print_generic_expr (dump_file, result, 0);
3085 fprintf (dump_file, " for expression ");
3086 print_generic_expr (dump_file, val, 0);
3087 fprintf (dump_file, "\n");
3092 if (result)
3094 changed = set_ssa_val_to (lhs, result);
3095 if (TREE_CODE (result) == SSA_NAME
3096 && VN_INFO (result)->has_constants)
3098 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
3099 VN_INFO (lhs)->has_constants = true;
3102 else
3104 changed = set_ssa_val_to (lhs, lhs);
3105 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3108 return changed;
3112 /* Visit a store to a reference operator LHS, part of STMT, value number it,
3113 and return true if the value number of the LHS has changed as a result. */
3115 static bool
3116 visit_reference_op_store (tree lhs, tree op, gimple stmt)
3118 bool changed = false;
3119 vn_reference_t vnresult = NULL;
3120 tree result, assign;
3121 bool resultsame = false;
3122 tree vuse = gimple_vuse (stmt);
3123 tree vdef = gimple_vdef (stmt);
3125 /* First we want to lookup using the *vuses* from the store and see
3126 if there the last store to this location with the same address
3127 had the same value.
3129 The vuses represent the memory state before the store. If the
3130 memory state, address, and value of the store is the same as the
3131 last store to this location, then this store will produce the
3132 same memory state as that store.
3134 In this case the vdef versions for this store are value numbered to those
3135 vuse versions, since they represent the same memory state after
3136 this store.
3138 Otherwise, the vdefs for the store are used when inserting into
3139 the table, since the store generates a new memory state. */
3141 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
3143 if (result)
3145 if (TREE_CODE (result) == SSA_NAME)
3146 result = SSA_VAL (result);
3147 if (TREE_CODE (op) == SSA_NAME)
3148 op = SSA_VAL (op);
3149 resultsame = expressions_equal_p (result, op);
3152 if ((!result || !resultsame)
3153 /* Only perform the following when being called from PRE
3154 which embeds tail merging. */
3155 && default_vn_walk_kind == VN_WALK)
3157 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3158 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
3159 if (vnresult)
3161 VN_INFO (vdef)->use_processed = true;
3162 return set_ssa_val_to (vdef, vnresult->result_vdef);
3166 if (!result || !resultsame)
3168 if (dump_file && (dump_flags & TDF_DETAILS))
3170 fprintf (dump_file, "No store match\n");
3171 fprintf (dump_file, "Value numbering store ");
3172 print_generic_expr (dump_file, lhs, 0);
3173 fprintf (dump_file, " to ");
3174 print_generic_expr (dump_file, op, 0);
3175 fprintf (dump_file, "\n");
3177 /* Have to set value numbers before insert, since insert is
3178 going to valueize the references in-place. */
3179 if (vdef)
3181 changed |= set_ssa_val_to (vdef, vdef);
3184 /* Do not insert structure copies into the tables. */
3185 if (is_gimple_min_invariant (op)
3186 || is_gimple_reg (op))
3187 vn_reference_insert (lhs, op, vdef, NULL);
3189 /* Only perform the following when being called from PRE
3190 which embeds tail merging. */
3191 if (default_vn_walk_kind == VN_WALK)
3193 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3194 vn_reference_insert (assign, lhs, vuse, vdef);
3197 else
3199 /* We had a match, so value number the vdef to have the value
3200 number of the vuse it came from. */
3202 if (dump_file && (dump_flags & TDF_DETAILS))
3203 fprintf (dump_file, "Store matched earlier value,"
3204 "value numbering store vdefs to matching vuses.\n");
3206 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3209 return changed;
3212 /* Visit and value number PHI, return true if the value number
3213 changed. */
3215 static bool
3216 visit_phi (gimple phi)
3218 bool changed = false;
3219 tree result;
3220 tree sameval = VN_TOP;
3221 bool allsame = true;
3223 /* TODO: We could check for this in init_sccvn, and replace this
3224 with a gcc_assert. */
3225 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3226 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3228 /* See if all non-TOP arguments have the same value. TOP is
3229 equivalent to everything, so we can ignore it. */
3230 edge_iterator ei;
3231 edge e;
3232 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3233 if (e->flags & EDGE_EXECUTABLE)
3235 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3237 if (TREE_CODE (def) == SSA_NAME)
3238 def = SSA_VAL (def);
3239 if (def == VN_TOP)
3240 continue;
3241 if (sameval == VN_TOP)
3243 sameval = def;
3245 else
3247 if (!expressions_equal_p (def, sameval))
3249 allsame = false;
3250 break;
3255 /* If all value numbered to the same value, the phi node has that
3256 value. */
3257 if (allsame)
3258 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3260 /* Otherwise, see if it is equivalent to a phi node in this block. */
3261 result = vn_phi_lookup (phi);
3262 if (result)
3263 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3264 else
3266 vn_phi_insert (phi, PHI_RESULT (phi));
3267 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3268 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3269 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3272 return changed;
3275 /* Return true if EXPR contains constants. */
3277 static bool
3278 expr_has_constants (tree expr)
3280 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3282 case tcc_unary:
3283 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3285 case tcc_binary:
3286 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3287 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3288 /* Constants inside reference ops are rarely interesting, but
3289 it can take a lot of looking to find them. */
3290 case tcc_reference:
3291 case tcc_declaration:
3292 return false;
3293 default:
3294 return is_gimple_min_invariant (expr);
3296 return false;
3299 /* Return true if STMT contains constants. */
3301 static bool
3302 stmt_has_constants (gimple stmt)
3304 tree tem;
3306 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3307 return false;
3309 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3311 case GIMPLE_TERNARY_RHS:
3312 tem = gimple_assign_rhs3 (stmt);
3313 if (TREE_CODE (tem) == SSA_NAME)
3314 tem = SSA_VAL (tem);
3315 if (is_gimple_min_invariant (tem))
3316 return true;
3317 /* Fallthru. */
3319 case GIMPLE_BINARY_RHS:
3320 tem = gimple_assign_rhs2 (stmt);
3321 if (TREE_CODE (tem) == SSA_NAME)
3322 tem = SSA_VAL (tem);
3323 if (is_gimple_min_invariant (tem))
3324 return true;
3325 /* Fallthru. */
3327 case GIMPLE_SINGLE_RHS:
3328 /* Constants inside reference ops are rarely interesting, but
3329 it can take a lot of looking to find them. */
3330 case GIMPLE_UNARY_RHS:
3331 tem = gimple_assign_rhs1 (stmt);
3332 if (TREE_CODE (tem) == SSA_NAME)
3333 tem = SSA_VAL (tem);
3334 return is_gimple_min_invariant (tem);
3336 default:
3337 gcc_unreachable ();
3339 return false;
3342 /* Simplify the binary expression RHS, and return the result if
3343 simplified. */
3345 static tree
3346 simplify_binary_expression (gimple stmt)
3348 tree result = NULL_TREE;
3349 tree op0 = gimple_assign_rhs1 (stmt);
3350 tree op1 = gimple_assign_rhs2 (stmt);
3351 enum tree_code code = gimple_assign_rhs_code (stmt);
3353 /* This will not catch every single case we could combine, but will
3354 catch those with constants. The goal here is to simultaneously
3355 combine constants between expressions, but avoid infinite
3356 expansion of expressions during simplification. */
3357 op0 = vn_valueize (op0);
3358 if (TREE_CODE (op0) == SSA_NAME
3359 && (VN_INFO (op0)->has_constants
3360 || TREE_CODE_CLASS (code) == tcc_comparison
3361 || code == COMPLEX_EXPR))
3362 op0 = vn_get_expr_for (op0);
3364 op1 = vn_valueize (op1);
3365 if (TREE_CODE (op1) == SSA_NAME
3366 && (VN_INFO (op1)->has_constants
3367 || code == COMPLEX_EXPR))
3368 op1 = vn_get_expr_for (op1);
3370 /* Pointer plus constant can be represented as invariant address.
3371 Do so to allow further propatation, see also tree forwprop. */
3372 if (code == POINTER_PLUS_EXPR
3373 && tree_fits_uhwi_p (op1)
3374 && TREE_CODE (op0) == ADDR_EXPR
3375 && is_gimple_min_invariant (op0))
3376 return build_invariant_address (TREE_TYPE (op0),
3377 TREE_OPERAND (op0, 0),
3378 tree_to_uhwi (op1));
3380 /* Avoid folding if nothing changed. */
3381 if (op0 == gimple_assign_rhs1 (stmt)
3382 && op1 == gimple_assign_rhs2 (stmt))
3383 return NULL_TREE;
3385 fold_defer_overflow_warnings ();
3387 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3388 if (result)
3389 STRIP_USELESS_TYPE_CONVERSION (result);
3391 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3392 stmt, 0);
3394 /* Make sure result is not a complex expression consisting
3395 of operators of operators (IE (a + b) + (a + c))
3396 Otherwise, we will end up with unbounded expressions if
3397 fold does anything at all. */
3398 if (result && valid_gimple_rhs_p (result))
3399 return result;
3401 return NULL_TREE;
3404 /* Simplify the unary expression RHS, and return the result if
3405 simplified. */
3407 static tree
3408 simplify_unary_expression (gassign *stmt)
3410 tree result = NULL_TREE;
3411 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3412 enum tree_code code = gimple_assign_rhs_code (stmt);
3414 /* We handle some tcc_reference codes here that are all
3415 GIMPLE_ASSIGN_SINGLE codes. */
3416 if (code == REALPART_EXPR
3417 || code == IMAGPART_EXPR
3418 || code == VIEW_CONVERT_EXPR
3419 || code == BIT_FIELD_REF)
3420 op0 = TREE_OPERAND (op0, 0);
3422 orig_op0 = op0;
3423 op0 = vn_valueize (op0);
3424 if (TREE_CODE (op0) == SSA_NAME)
3426 if (VN_INFO (op0)->has_constants)
3427 op0 = vn_get_expr_for (op0);
3428 else if (CONVERT_EXPR_CODE_P (code)
3429 || code == REALPART_EXPR
3430 || code == IMAGPART_EXPR
3431 || code == VIEW_CONVERT_EXPR
3432 || code == BIT_FIELD_REF)
3434 /* We want to do tree-combining on conversion-like expressions.
3435 Make sure we feed only SSA_NAMEs or constants to fold though. */
3436 tree tem = vn_get_expr_for (op0);
3437 if (UNARY_CLASS_P (tem)
3438 || BINARY_CLASS_P (tem)
3439 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3440 || TREE_CODE (tem) == SSA_NAME
3441 || TREE_CODE (tem) == CONSTRUCTOR
3442 || is_gimple_min_invariant (tem))
3443 op0 = tem;
3447 /* Avoid folding if nothing changed, but remember the expression. */
3448 if (op0 == orig_op0)
3449 return NULL_TREE;
3451 if (code == BIT_FIELD_REF)
3453 tree rhs = gimple_assign_rhs1 (stmt);
3454 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3455 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3457 else
3458 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3459 if (result)
3461 STRIP_USELESS_TYPE_CONVERSION (result);
3462 if (valid_gimple_rhs_p (result))
3463 return result;
3466 return NULL_TREE;
3469 /* Try to simplify RHS using equivalences and constant folding. */
3471 static tree
3472 try_to_simplify (gassign *stmt)
3474 enum tree_code code = gimple_assign_rhs_code (stmt);
3475 tree tem;
3477 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3478 in this case, there is no point in doing extra work. */
3479 if (code == SSA_NAME)
3480 return NULL_TREE;
3482 /* First try constant folding based on our current lattice. */
3483 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
3484 if (tem
3485 && (TREE_CODE (tem) == SSA_NAME
3486 || is_gimple_min_invariant (tem)))
3487 return tem;
3489 /* If that didn't work try combining multiple statements. */
3490 switch (TREE_CODE_CLASS (code))
3492 case tcc_reference:
3493 /* Fallthrough for some unary codes that can operate on registers. */
3494 if (!(code == REALPART_EXPR
3495 || code == IMAGPART_EXPR
3496 || code == VIEW_CONVERT_EXPR
3497 || code == BIT_FIELD_REF))
3498 break;
3499 /* We could do a little more with unary ops, if they expand
3500 into binary ops, but it's debatable whether it is worth it. */
3501 case tcc_unary:
3502 return simplify_unary_expression (stmt);
3504 case tcc_comparison:
3505 case tcc_binary:
3506 return simplify_binary_expression (stmt);
3508 default:
3509 break;
3512 return NULL_TREE;
3515 /* Visit and value number USE, return true if the value number
3516 changed. */
3518 static bool
3519 visit_use (tree use)
3521 bool changed = false;
3522 gimple stmt = SSA_NAME_DEF_STMT (use);
3524 mark_use_processed (use);
3526 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3527 if (dump_file && (dump_flags & TDF_DETAILS)
3528 && !SSA_NAME_IS_DEFAULT_DEF (use))
3530 fprintf (dump_file, "Value numbering ");
3531 print_generic_expr (dump_file, use, 0);
3532 fprintf (dump_file, " stmt = ");
3533 print_gimple_stmt (dump_file, stmt, 0, 0);
3536 /* Handle uninitialized uses. */
3537 if (SSA_NAME_IS_DEFAULT_DEF (use))
3538 changed = set_ssa_val_to (use, use);
3539 else
3541 if (gimple_code (stmt) == GIMPLE_PHI)
3542 changed = visit_phi (stmt);
3543 else if (gimple_has_volatile_ops (stmt))
3544 changed = defs_to_varying (stmt);
3545 else if (is_gimple_assign (stmt))
3547 enum tree_code code = gimple_assign_rhs_code (stmt);
3548 tree lhs = gimple_assign_lhs (stmt);
3549 tree rhs1 = gimple_assign_rhs1 (stmt);
3550 tree simplified;
3552 /* Shortcut for copies. Simplifying copies is pointless,
3553 since we copy the expression and value they represent. */
3554 if (code == SSA_NAME
3555 && TREE_CODE (lhs) == SSA_NAME)
3557 changed = visit_copy (lhs, rhs1);
3558 goto done;
3560 simplified = try_to_simplify (as_a <gassign *> (stmt));
3561 if (simplified)
3563 if (dump_file && (dump_flags & TDF_DETAILS))
3565 fprintf (dump_file, "RHS ");
3566 print_gimple_expr (dump_file, stmt, 0, 0);
3567 fprintf (dump_file, " simplified to ");
3568 print_generic_expr (dump_file, simplified, 0);
3569 if (TREE_CODE (lhs) == SSA_NAME)
3570 fprintf (dump_file, " has constants %d\n",
3571 expr_has_constants (simplified));
3572 else
3573 fprintf (dump_file, "\n");
3576 /* Setting value numbers to constants will occasionally
3577 screw up phi congruence because constants are not
3578 uniquely associated with a single ssa name that can be
3579 looked up. */
3580 if (simplified
3581 && is_gimple_min_invariant (simplified)
3582 && TREE_CODE (lhs) == SSA_NAME)
3584 VN_INFO (lhs)->expr = simplified;
3585 VN_INFO (lhs)->has_constants = true;
3586 changed = set_ssa_val_to (lhs, simplified);
3587 goto done;
3589 else if (simplified
3590 && TREE_CODE (simplified) == SSA_NAME
3591 && TREE_CODE (lhs) == SSA_NAME)
3593 changed = visit_copy (lhs, simplified);
3594 goto done;
3596 else if (simplified)
3598 if (TREE_CODE (lhs) == SSA_NAME)
3600 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3601 /* We have to unshare the expression or else
3602 valuizing may change the IL stream. */
3603 VN_INFO (lhs)->expr = unshare_expr (simplified);
3606 else if (stmt_has_constants (stmt)
3607 && TREE_CODE (lhs) == SSA_NAME)
3608 VN_INFO (lhs)->has_constants = true;
3609 else if (TREE_CODE (lhs) == SSA_NAME)
3611 /* We reset expr and constantness here because we may
3612 have been value numbering optimistically, and
3613 iterating. They may become non-constant in this case,
3614 even if they were optimistically constant. */
3616 VN_INFO (lhs)->has_constants = false;
3617 VN_INFO (lhs)->expr = NULL_TREE;
3620 if ((TREE_CODE (lhs) == SSA_NAME
3621 /* We can substitute SSA_NAMEs that are live over
3622 abnormal edges with their constant value. */
3623 && !(gimple_assign_copy_p (stmt)
3624 && is_gimple_min_invariant (rhs1))
3625 && !(simplified
3626 && is_gimple_min_invariant (simplified))
3627 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3628 /* Stores or copies from SSA_NAMEs that are live over
3629 abnormal edges are a problem. */
3630 || (code == SSA_NAME
3631 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3632 changed = defs_to_varying (stmt);
3633 else if (REFERENCE_CLASS_P (lhs)
3634 || DECL_P (lhs))
3635 changed = visit_reference_op_store (lhs, rhs1, stmt);
3636 else if (TREE_CODE (lhs) == SSA_NAME)
3638 if ((gimple_assign_copy_p (stmt)
3639 && is_gimple_min_invariant (rhs1))
3640 || (simplified
3641 && is_gimple_min_invariant (simplified)))
3643 VN_INFO (lhs)->has_constants = true;
3644 if (simplified)
3645 changed = set_ssa_val_to (lhs, simplified);
3646 else
3647 changed = set_ssa_val_to (lhs, rhs1);
3649 else
3651 /* First try to lookup the simplified expression. */
3652 if (simplified)
3654 enum gimple_rhs_class rhs_class;
3657 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3658 if ((rhs_class == GIMPLE_UNARY_RHS
3659 || rhs_class == GIMPLE_BINARY_RHS
3660 || rhs_class == GIMPLE_TERNARY_RHS)
3661 && valid_gimple_rhs_p (simplified))
3663 tree result = vn_nary_op_lookup (simplified, NULL);
3664 if (result)
3666 changed = set_ssa_val_to (lhs, result);
3667 goto done;
3672 /* Otherwise visit the original statement. */
3673 switch (vn_get_stmt_kind (stmt))
3675 case VN_NARY:
3676 changed = visit_nary_op (lhs, stmt);
3677 break;
3678 case VN_REFERENCE:
3679 changed = visit_reference_op_load (lhs, rhs1, stmt);
3680 break;
3681 default:
3682 changed = defs_to_varying (stmt);
3683 break;
3687 else
3688 changed = defs_to_varying (stmt);
3690 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
3692 tree lhs = gimple_call_lhs (stmt);
3693 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3695 /* Try constant folding based on our current lattice. */
3696 tree simplified = gimple_fold_stmt_to_constant_1 (stmt,
3697 vn_valueize);
3698 if (simplified)
3700 if (dump_file && (dump_flags & TDF_DETAILS))
3702 fprintf (dump_file, "call ");
3703 print_gimple_expr (dump_file, stmt, 0, 0);
3704 fprintf (dump_file, " simplified to ");
3705 print_generic_expr (dump_file, simplified, 0);
3706 if (TREE_CODE (lhs) == SSA_NAME)
3707 fprintf (dump_file, " has constants %d\n",
3708 expr_has_constants (simplified));
3709 else
3710 fprintf (dump_file, "\n");
3713 /* Setting value numbers to constants will occasionally
3714 screw up phi congruence because constants are not
3715 uniquely associated with a single ssa name that can be
3716 looked up. */
3717 if (simplified
3718 && is_gimple_min_invariant (simplified))
3720 VN_INFO (lhs)->expr = simplified;
3721 VN_INFO (lhs)->has_constants = true;
3722 changed = set_ssa_val_to (lhs, simplified);
3723 if (gimple_vdef (stmt))
3724 changed |= set_ssa_val_to (gimple_vdef (stmt),
3725 gimple_vuse (stmt));
3726 goto done;
3728 else if (simplified
3729 && TREE_CODE (simplified) == SSA_NAME)
3731 changed = visit_copy (lhs, simplified);
3732 if (gimple_vdef (stmt))
3733 changed |= set_ssa_val_to (gimple_vdef (stmt),
3734 gimple_vuse (stmt));
3735 goto done;
3737 else
3739 if (stmt_has_constants (stmt))
3740 VN_INFO (lhs)->has_constants = true;
3741 else
3743 /* We reset expr and constantness here because we may
3744 have been value numbering optimistically, and
3745 iterating. They may become non-constant in this case,
3746 even if they were optimistically constant. */
3747 VN_INFO (lhs)->has_constants = false;
3748 VN_INFO (lhs)->expr = NULL_TREE;
3751 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3753 changed = defs_to_varying (stmt);
3754 goto done;
3759 if (!gimple_call_internal_p (stmt)
3760 && (/* Calls to the same function with the same vuse
3761 and the same operands do not necessarily return the same
3762 value, unless they're pure or const. */
3763 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3764 /* If calls have a vdef, subsequent calls won't have
3765 the same incoming vuse. So, if 2 calls with vdef have the
3766 same vuse, we know they're not subsequent.
3767 We can value number 2 calls to the same function with the
3768 same vuse and the same operands which are not subsequent
3769 the same, because there is no code in the program that can
3770 compare the 2 values... */
3771 || (gimple_vdef (stmt)
3772 /* ... unless the call returns a pointer which does
3773 not alias with anything else. In which case the
3774 information that the values are distinct are encoded
3775 in the IL. */
3776 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
3777 /* Only perform the following when being called from PRE
3778 which embeds tail merging. */
3779 && default_vn_walk_kind == VN_WALK)))
3780 changed = visit_reference_op_call (lhs, call_stmt);
3781 else
3782 changed = defs_to_varying (stmt);
3784 else
3785 changed = defs_to_varying (stmt);
3787 done:
3788 return changed;
3791 /* Compare two operands by reverse postorder index */
3793 static int
3794 compare_ops (const void *pa, const void *pb)
3796 const tree opa = *((const tree *)pa);
3797 const tree opb = *((const tree *)pb);
3798 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3799 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3800 basic_block bba;
3801 basic_block bbb;
3803 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3804 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3805 else if (gimple_nop_p (opstmta))
3806 return -1;
3807 else if (gimple_nop_p (opstmtb))
3808 return 1;
3810 bba = gimple_bb (opstmta);
3811 bbb = gimple_bb (opstmtb);
3813 if (!bba && !bbb)
3814 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3815 else if (!bba)
3816 return -1;
3817 else if (!bbb)
3818 return 1;
3820 if (bba == bbb)
3822 if (gimple_code (opstmta) == GIMPLE_PHI
3823 && gimple_code (opstmtb) == GIMPLE_PHI)
3824 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3825 else if (gimple_code (opstmta) == GIMPLE_PHI)
3826 return -1;
3827 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3828 return 1;
3829 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3830 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3831 else
3832 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3834 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3837 /* Sort an array containing members of a strongly connected component
3838 SCC so that the members are ordered by RPO number.
3839 This means that when the sort is complete, iterating through the
3840 array will give you the members in RPO order. */
3842 static void
3843 sort_scc (vec<tree> scc)
3845 scc.qsort (compare_ops);
3848 /* Insert the no longer used nary ONARY to the hash INFO. */
3850 static void
3851 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3853 size_t size = sizeof_vn_nary_op (onary->length);
3854 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3855 &info->nary_obstack);
3856 memcpy (nary, onary, size);
3857 vn_nary_op_insert_into (nary, info->nary, false);
3860 /* Insert the no longer used phi OPHI to the hash INFO. */
3862 static void
3863 copy_phi (vn_phi_t ophi, vn_tables_t info)
3865 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3866 vn_phi_s **slot;
3867 memcpy (phi, ophi, sizeof (*phi));
3868 ophi->phiargs.create (0);
3869 slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT);
3870 gcc_assert (!*slot);
3871 *slot = phi;
3874 /* Insert the no longer used reference OREF to the hash INFO. */
3876 static void
3877 copy_reference (vn_reference_t oref, vn_tables_t info)
3879 vn_reference_t ref;
3880 vn_reference_s **slot;
3881 ref = (vn_reference_t) pool_alloc (info->references_pool);
3882 memcpy (ref, oref, sizeof (*ref));
3883 oref->operands.create (0);
3884 slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT);
3885 if (*slot)
3886 free_reference (*slot);
3887 *slot = ref;
3890 /* Process a strongly connected component in the SSA graph. */
3892 static void
3893 process_scc (vec<tree> scc)
3895 tree var;
3896 unsigned int i;
3897 unsigned int iterations = 0;
3898 bool changed = true;
3899 vn_nary_op_iterator_type hin;
3900 vn_phi_iterator_type hip;
3901 vn_reference_iterator_type hir;
3902 vn_nary_op_t nary;
3903 vn_phi_t phi;
3904 vn_reference_t ref;
3906 /* If the SCC has a single member, just visit it. */
3907 if (scc.length () == 1)
3909 tree use = scc[0];
3910 if (VN_INFO (use)->use_processed)
3911 return;
3912 /* We need to make sure it doesn't form a cycle itself, which can
3913 happen for self-referential PHI nodes. In that case we would
3914 end up inserting an expression with VN_TOP operands into the
3915 valid table which makes us derive bogus equivalences later.
3916 The cheapest way to check this is to assume it for all PHI nodes. */
3917 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3918 /* Fallthru to iteration. */ ;
3919 else
3921 visit_use (use);
3922 return;
3926 if (dump_file && (dump_flags & TDF_DETAILS))
3927 print_scc (dump_file, scc);
3929 /* Iterate over the SCC with the optimistic table until it stops
3930 changing. */
3931 current_info = optimistic_info;
3932 while (changed)
3934 changed = false;
3935 iterations++;
3936 if (dump_file && (dump_flags & TDF_DETAILS))
3937 fprintf (dump_file, "Starting iteration %d\n", iterations);
3938 /* As we are value-numbering optimistically we have to
3939 clear the expression tables and the simplified expressions
3940 in each iteration until we converge. */
3941 optimistic_info->nary->empty ();
3942 optimistic_info->phis->empty ();
3943 optimistic_info->references->empty ();
3944 obstack_free (&optimistic_info->nary_obstack, NULL);
3945 gcc_obstack_init (&optimistic_info->nary_obstack);
3946 empty_alloc_pool (optimistic_info->phis_pool);
3947 empty_alloc_pool (optimistic_info->references_pool);
3948 FOR_EACH_VEC_ELT (scc, i, var)
3949 VN_INFO (var)->expr = NULL_TREE;
3950 FOR_EACH_VEC_ELT (scc, i, var)
3951 changed |= visit_use (var);
3954 if (dump_file && (dump_flags & TDF_DETAILS))
3955 fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations);
3956 statistics_histogram_event (cfun, "SCC iterations", iterations);
3958 /* Finally, copy the contents of the no longer used optimistic
3959 table to the valid table. */
3960 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin)
3961 copy_nary (nary, valid_info);
3962 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip)
3963 copy_phi (phi, valid_info);
3964 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references,
3965 ref, vn_reference_t, hir)
3966 copy_reference (ref, valid_info);
3968 current_info = valid_info;
3972 /* Pop the components of the found SCC for NAME off the SCC stack
3973 and process them. Returns true if all went well, false if
3974 we run into resource limits. */
3976 static bool
3977 extract_and_process_scc_for_name (tree name)
3979 auto_vec<tree> scc;
3980 tree x;
3982 /* Found an SCC, pop the components off the SCC stack and
3983 process them. */
3986 x = sccstack.pop ();
3988 VN_INFO (x)->on_sccstack = false;
3989 scc.safe_push (x);
3990 } while (x != name);
3992 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3993 if (scc.length ()
3994 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3996 if (dump_file)
3997 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3998 "SCC size %u exceeding %u\n", scc.length (),
3999 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
4001 return false;
4004 if (scc.length () > 1)
4005 sort_scc (scc);
4007 process_scc (scc);
4009 return true;
4012 /* Depth first search on NAME to discover and process SCC's in the SSA
4013 graph.
4014 Execution of this algorithm relies on the fact that the SCC's are
4015 popped off the stack in topological order.
4016 Returns true if successful, false if we stopped processing SCC's due
4017 to resource constraints. */
4019 static bool
4020 DFS (tree name)
4022 vec<ssa_op_iter> itervec = vNULL;
4023 vec<tree> namevec = vNULL;
4024 use_operand_p usep = NULL;
4025 gimple defstmt;
4026 tree use;
4027 ssa_op_iter iter;
4029 start_over:
4030 /* SCC info */
4031 VN_INFO (name)->dfsnum = next_dfs_num++;
4032 VN_INFO (name)->visited = true;
4033 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
4035 sccstack.safe_push (name);
4036 VN_INFO (name)->on_sccstack = true;
4037 defstmt = SSA_NAME_DEF_STMT (name);
4039 /* Recursively DFS on our operands, looking for SCC's. */
4040 if (!gimple_nop_p (defstmt))
4042 /* Push a new iterator. */
4043 if (gphi *phi = dyn_cast <gphi *> (defstmt))
4044 usep = op_iter_init_phiuse (&iter, phi, SSA_OP_ALL_USES);
4045 else
4046 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
4048 else
4049 clear_and_done_ssa_iter (&iter);
4051 while (1)
4053 /* If we are done processing uses of a name, go up the stack
4054 of iterators and process SCCs as we found them. */
4055 if (op_iter_done (&iter))
4057 /* See if we found an SCC. */
4058 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
4059 if (!extract_and_process_scc_for_name (name))
4061 namevec.release ();
4062 itervec.release ();
4063 return false;
4066 /* Check if we are done. */
4067 if (namevec.is_empty ())
4069 namevec.release ();
4070 itervec.release ();
4071 return true;
4074 /* Restore the last use walker and continue walking there. */
4075 use = name;
4076 name = namevec.pop ();
4077 memcpy (&iter, &itervec.last (),
4078 sizeof (ssa_op_iter));
4079 itervec.pop ();
4080 goto continue_walking;
4083 use = USE_FROM_PTR (usep);
4085 /* Since we handle phi nodes, we will sometimes get
4086 invariants in the use expression. */
4087 if (TREE_CODE (use) == SSA_NAME)
4089 if (! (VN_INFO (use)->visited))
4091 /* Recurse by pushing the current use walking state on
4092 the stack and starting over. */
4093 itervec.safe_push (iter);
4094 namevec.safe_push (name);
4095 name = use;
4096 goto start_over;
4098 continue_walking:
4099 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
4100 VN_INFO (use)->low);
4102 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
4103 && VN_INFO (use)->on_sccstack)
4105 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
4106 VN_INFO (name)->low);
4110 usep = op_iter_next_use (&iter);
4114 /* Allocate a value number table. */
4116 static void
4117 allocate_vn_table (vn_tables_t table)
4119 table->phis = new vn_phi_table_type (23);
4120 table->nary = new vn_nary_op_table_type (23);
4121 table->references = new vn_reference_table_type (23);
4123 gcc_obstack_init (&table->nary_obstack);
4124 table->phis_pool = create_alloc_pool ("VN phis",
4125 sizeof (struct vn_phi_s),
4126 30);
4127 table->references_pool = create_alloc_pool ("VN references",
4128 sizeof (struct vn_reference_s),
4129 30);
4132 /* Free a value number table. */
4134 static void
4135 free_vn_table (vn_tables_t table)
4137 delete table->phis;
4138 table->phis = NULL;
4139 delete table->nary;
4140 table->nary = NULL;
4141 delete table->references;
4142 table->references = NULL;
4143 obstack_free (&table->nary_obstack, NULL);
4144 free_alloc_pool (table->phis_pool);
4145 free_alloc_pool (table->references_pool);
4148 static void
4149 init_scc_vn (void)
4151 size_t i;
4152 int j;
4153 int *rpo_numbers_temp;
4155 calculate_dominance_info (CDI_DOMINATORS);
4156 sccstack.create (0);
4157 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
4159 constant_value_ids = BITMAP_ALLOC (NULL);
4161 next_dfs_num = 1;
4162 next_value_id = 1;
4164 vn_ssa_aux_table.create (num_ssa_names + 1);
4165 /* VEC_alloc doesn't actually grow it to the right size, it just
4166 preallocates the space to do so. */
4167 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
4168 gcc_obstack_init (&vn_ssa_aux_obstack);
4170 shared_lookup_phiargs.create (0);
4171 shared_lookup_references.create (0);
4172 rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun));
4173 rpo_numbers_temp =
4174 XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4175 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
4177 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
4178 the i'th block in RPO order is bb. We want to map bb's to RPO
4179 numbers, so we need to rearrange this array. */
4180 for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++)
4181 rpo_numbers[rpo_numbers_temp[j]] = j;
4183 XDELETE (rpo_numbers_temp);
4185 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
4187 /* Create the VN_INFO structures, and initialize value numbers to
4188 TOP. */
4189 for (i = 0; i < num_ssa_names; i++)
4191 tree name = ssa_name (i);
4192 if (name)
4194 VN_INFO_GET (name)->valnum = VN_TOP;
4195 VN_INFO (name)->expr = NULL_TREE;
4196 VN_INFO (name)->value_id = 0;
4200 renumber_gimple_stmt_uids ();
4202 /* Create the valid and optimistic value numbering tables. */
4203 valid_info = XCNEW (struct vn_tables_s);
4204 allocate_vn_table (valid_info);
4205 optimistic_info = XCNEW (struct vn_tables_s);
4206 allocate_vn_table (optimistic_info);
4209 void
4210 free_scc_vn (void)
4212 size_t i;
4214 delete constant_to_value_id;
4215 constant_to_value_id = NULL;
4216 BITMAP_FREE (constant_value_ids);
4217 shared_lookup_phiargs.release ();
4218 shared_lookup_references.release ();
4219 XDELETEVEC (rpo_numbers);
4221 for (i = 0; i < num_ssa_names; i++)
4223 tree name = ssa_name (i);
4224 if (name
4225 && VN_INFO (name)->needs_insertion)
4226 release_ssa_name (name);
4228 obstack_free (&vn_ssa_aux_obstack, NULL);
4229 vn_ssa_aux_table.release ();
4231 sccstack.release ();
4232 free_vn_table (valid_info);
4233 XDELETE (valid_info);
4234 free_vn_table (optimistic_info);
4235 XDELETE (optimistic_info);
4238 /* Set *ID according to RESULT. */
4240 static void
4241 set_value_id_for_result (tree result, unsigned int *id)
4243 if (result && TREE_CODE (result) == SSA_NAME)
4244 *id = VN_INFO (result)->value_id;
4245 else if (result && is_gimple_min_invariant (result))
4246 *id = get_or_alloc_constant_value_id (result);
4247 else
4248 *id = get_next_value_id ();
4251 /* Set the value ids in the valid hash tables. */
4253 static void
4254 set_hashtable_value_ids (void)
4256 vn_nary_op_iterator_type hin;
4257 vn_phi_iterator_type hip;
4258 vn_reference_iterator_type hir;
4259 vn_nary_op_t vno;
4260 vn_reference_t vr;
4261 vn_phi_t vp;
4263 /* Now set the value ids of the things we had put in the hash
4264 table. */
4266 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4267 set_value_id_for_result (vno->result, &vno->value_id);
4269 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4270 set_value_id_for_result (vp->result, &vp->value_id);
4272 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4273 hir)
4274 set_value_id_for_result (vr->result, &vr->value_id);
4277 class cond_dom_walker : public dom_walker
4279 public:
4280 cond_dom_walker () : dom_walker (CDI_DOMINATORS), fail (false) {}
4282 virtual void before_dom_children (basic_block);
4284 bool fail;
4287 void
4288 cond_dom_walker::before_dom_children (basic_block bb)
4290 edge e;
4291 edge_iterator ei;
4293 if (fail)
4294 return;
4296 /* If any of the predecessor edges that do not come from blocks dominated
4297 by us are still marked as possibly executable consider this block
4298 reachable. */
4299 bool reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (cfun);
4300 FOR_EACH_EDGE (e, ei, bb->preds)
4301 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
4302 reachable |= (e->flags & EDGE_EXECUTABLE);
4304 /* If the block is not reachable all outgoing edges are not
4305 executable. */
4306 if (!reachable)
4308 if (dump_file && (dump_flags & TDF_DETAILS))
4309 fprintf (dump_file, "Marking all outgoing edges of unreachable "
4310 "BB %d as not executable\n", bb->index);
4312 FOR_EACH_EDGE (e, ei, bb->succs)
4313 e->flags &= ~EDGE_EXECUTABLE;
4314 return;
4317 gimple stmt = last_stmt (bb);
4318 if (!stmt)
4319 return;
4321 enum gimple_code code = gimple_code (stmt);
4322 if (code != GIMPLE_COND
4323 && code != GIMPLE_SWITCH
4324 && code != GIMPLE_GOTO)
4325 return;
4327 if (dump_file && (dump_flags & TDF_DETAILS))
4329 fprintf (dump_file, "Value-numbering operands of stmt ending BB %d: ",
4330 bb->index);
4331 print_gimple_stmt (dump_file, stmt, 0, 0);
4334 /* Value-number the last stmts SSA uses. */
4335 ssa_op_iter i;
4336 tree op;
4337 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4338 if (VN_INFO (op)->visited == false
4339 && !DFS (op))
4341 fail = true;
4342 return;
4345 /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges
4346 if value-numbering can prove they are not reachable. Handling
4347 computed gotos is also possible. */
4348 tree val;
4349 switch (code)
4351 case GIMPLE_COND:
4353 tree lhs = gimple_cond_lhs (stmt);
4354 tree rhs = gimple_cond_rhs (stmt);
4355 /* Work hard in computing the condition and take into account
4356 the valueization of the defining stmt. */
4357 if (TREE_CODE (lhs) == SSA_NAME)
4358 lhs = vn_get_expr_for (lhs);
4359 if (TREE_CODE (rhs) == SSA_NAME)
4360 rhs = vn_get_expr_for (rhs);
4361 val = fold_binary (gimple_cond_code (stmt),
4362 boolean_type_node, lhs, rhs);
4363 break;
4365 case GIMPLE_SWITCH:
4366 val = gimple_switch_index (as_a <gswitch *> (stmt));
4367 break;
4368 case GIMPLE_GOTO:
4369 val = gimple_goto_dest (stmt);
4370 break;
4371 default:
4372 gcc_unreachable ();
4374 if (!val)
4375 return;
4377 edge taken = find_taken_edge (bb, vn_valueize (val));
4378 if (!taken)
4379 return;
4381 if (dump_file && (dump_flags & TDF_DETAILS))
4382 fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as "
4383 "not executable\n", bb->index, bb->index, taken->dest->index);
4385 FOR_EACH_EDGE (e, ei, bb->succs)
4386 if (e != taken)
4387 e->flags &= ~EDGE_EXECUTABLE;
4390 /* Do SCCVN. Returns true if it finished, false if we bailed out
4391 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4392 how we use the alias oracle walking during the VN process. */
4394 bool
4395 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4397 basic_block bb;
4398 size_t i;
4399 tree param;
4401 default_vn_walk_kind = default_vn_walk_kind_;
4403 init_scc_vn ();
4404 current_info = valid_info;
4406 for (param = DECL_ARGUMENTS (current_function_decl);
4407 param;
4408 param = DECL_CHAIN (param))
4410 tree def = ssa_default_def (cfun, param);
4411 if (def)
4413 VN_INFO (def)->visited = true;
4414 VN_INFO (def)->valnum = def;
4418 /* Mark all edges as possibly executable. */
4419 FOR_ALL_BB_FN (bb, cfun)
4421 edge_iterator ei;
4422 edge e;
4423 FOR_EACH_EDGE (e, ei, bb->succs)
4424 e->flags |= EDGE_EXECUTABLE;
4427 /* Walk all blocks in dominator order, value-numbering the last stmts
4428 SSA uses and decide whether outgoing edges are not executable. */
4429 cond_dom_walker walker;
4430 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4431 if (walker.fail)
4433 free_scc_vn ();
4434 return false;
4437 /* Value-number remaining SSA names. */
4438 for (i = 1; i < num_ssa_names; ++i)
4440 tree name = ssa_name (i);
4441 if (name
4442 && VN_INFO (name)->visited == false
4443 && !has_zero_uses (name))
4444 if (!DFS (name))
4446 free_scc_vn ();
4447 return false;
4451 /* Initialize the value ids. */
4453 for (i = 1; i < num_ssa_names; ++i)
4455 tree name = ssa_name (i);
4456 vn_ssa_aux_t info;
4457 if (!name)
4458 continue;
4459 info = VN_INFO (name);
4460 if (info->valnum == name
4461 || info->valnum == VN_TOP)
4462 info->value_id = get_next_value_id ();
4463 else if (is_gimple_min_invariant (info->valnum))
4464 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4467 /* Propagate. */
4468 for (i = 1; i < num_ssa_names; ++i)
4470 tree name = ssa_name (i);
4471 vn_ssa_aux_t info;
4472 if (!name)
4473 continue;
4474 info = VN_INFO (name);
4475 if (TREE_CODE (info->valnum) == SSA_NAME
4476 && info->valnum != name
4477 && info->value_id != VN_INFO (info->valnum)->value_id)
4478 info->value_id = VN_INFO (info->valnum)->value_id;
4481 set_hashtable_value_ids ();
4483 if (dump_file && (dump_flags & TDF_DETAILS))
4485 fprintf (dump_file, "Value numbers:\n");
4486 for (i = 0; i < num_ssa_names; i++)
4488 tree name = ssa_name (i);
4489 if (name
4490 && VN_INFO (name)->visited
4491 && SSA_VAL (name) != name)
4493 print_generic_expr (dump_file, name, 0);
4494 fprintf (dump_file, " = ");
4495 print_generic_expr (dump_file, SSA_VAL (name), 0);
4496 fprintf (dump_file, "\n");
4501 return true;
4504 /* Return the maximum value id we have ever seen. */
4506 unsigned int
4507 get_max_value_id (void)
4509 return next_value_id;
4512 /* Return the next unique value id. */
4514 unsigned int
4515 get_next_value_id (void)
4517 return next_value_id++;
4521 /* Compare two expressions E1 and E2 and return true if they are equal. */
4523 bool
4524 expressions_equal_p (tree e1, tree e2)
4526 /* The obvious case. */
4527 if (e1 == e2)
4528 return true;
4530 /* If only one of them is null, they cannot be equal. */
4531 if (!e1 || !e2)
4532 return false;
4534 /* Now perform the actual comparison. */
4535 if (TREE_CODE (e1) == TREE_CODE (e2)
4536 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4537 return true;
4539 return false;
4543 /* Return true if the nary operation NARY may trap. This is a copy
4544 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4546 bool
4547 vn_nary_may_trap (vn_nary_op_t nary)
4549 tree type;
4550 tree rhs2 = NULL_TREE;
4551 bool honor_nans = false;
4552 bool honor_snans = false;
4553 bool fp_operation = false;
4554 bool honor_trapv = false;
4555 bool handled, ret;
4556 unsigned i;
4558 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4559 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4560 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4562 type = nary->type;
4563 fp_operation = FLOAT_TYPE_P (type);
4564 if (fp_operation)
4566 honor_nans = flag_trapping_math && !flag_finite_math_only;
4567 honor_snans = flag_signaling_nans != 0;
4569 else if (INTEGRAL_TYPE_P (type)
4570 && TYPE_OVERFLOW_TRAPS (type))
4571 honor_trapv = true;
4573 if (nary->length >= 2)
4574 rhs2 = nary->op[1];
4575 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4576 honor_trapv,
4577 honor_nans, honor_snans, rhs2,
4578 &handled);
4579 if (handled
4580 && ret)
4581 return true;
4583 for (i = 0; i < nary->length; ++i)
4584 if (tree_could_trap_p (nary->op[i]))
4585 return true;
4587 return false;