* config/linux/x86/futex_bits.h (sys_futex0) [__x86_64__]: Change
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob9d2345f41c406f8f047e33f10d4080373ba8172e
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 vn_nary_op_s *);
154 static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
157 /* Return the computed hashcode for nary operation P1. */
159 inline hashval_t
160 vn_nary_op_hasher::hash (const vn_nary_op_s *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 vn_nary_op_s *vno1, const vn_nary_op_s *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 vn_phi_s *);
188 static inline bool equal (const vn_phi_s *, const vn_phi_s *);
189 static inline void remove (vn_phi_s *);
192 /* Return the computed hashcode for phi operation P1. */
194 inline hashval_t
195 vn_phi_hasher::hash (const vn_phi_s *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 vn_phi_s *vp1, const vn_phi_s *vp2)
205 return vn_phi_eq (vp1, vp2);
208 /* Free a phi operation structure VP. */
210 inline void
211 vn_phi_hasher::remove (vn_phi_s *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 vn_reference_s *);
256 static inline bool equal (const vn_reference_s *, const vn_reference_s *);
257 static inline void remove (vn_reference_s *);
260 /* Return the hashcode for a given reference operation P1. */
262 inline hashval_t
263 vn_reference_hasher::hash (const vn_reference_s *vr1)
265 return vr1->hashcode;
268 inline bool
269 vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
271 return vn_reference_eq (v, c);
274 inline void
275 vn_reference_hasher::remove (vn_reference_s *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 vn_constant_s *);
304 static inline bool equal (const vn_constant_s *, const vn_constant_s *);
307 /* Hash table hash function for vn_constant_t. */
309 inline hashval_t
310 vn_constant_hasher::hash (const vn_constant_s *vc1)
312 return vc1->hashcode;
315 /* Hash table equality function for vn_constant_t. */
317 inline bool
318 vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *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;
1356 if (INTEGRAL_TYPE_P (ref->type))
1357 size = TYPE_PRECISION (ref->type);
1358 else
1359 size = tree_to_shwi (TYPE_SIZE (ref->type));
1360 if (size % BITS_PER_UNIT != 0
1361 || size > MAX_BITSIZE_MODE_ANY_MODE)
1362 return NULL_TREE;
1363 size /= BITS_PER_UNIT;
1364 unsigned i;
1365 for (i = 0; i < operands.length (); ++i)
1367 if (operands[i].off == -1)
1368 return NULL_TREE;
1369 off += operands[i].off;
1370 if (operands[i].opcode == MEM_REF)
1372 ++i;
1373 break;
1376 vn_reference_op_t base = &operands[--i];
1377 tree ctor = error_mark_node;
1378 tree decl = NULL_TREE;
1379 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1380 ctor = base->op0;
1381 else if (base->opcode == MEM_REF
1382 && base[1].opcode == ADDR_EXPR
1383 && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
1384 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL))
1386 decl = TREE_OPERAND (base[1].op0, 0);
1387 ctor = ctor_for_folding (decl);
1389 if (ctor == NULL_TREE)
1390 return build_zero_cst (ref->type);
1391 else if (ctor != error_mark_node)
1393 if (decl)
1395 tree res = fold_ctor_reference (ref->type, ctor,
1396 off * BITS_PER_UNIT,
1397 size * BITS_PER_UNIT, decl);
1398 if (res)
1400 STRIP_USELESS_TYPE_CONVERSION (res);
1401 if (is_gimple_min_invariant (res))
1402 return res;
1405 else
1407 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1408 if (native_encode_expr (ctor, buf, size, off) > 0)
1409 return native_interpret_expr (ref->type, buf, size);
1414 return NULL_TREE;
1417 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1418 structures into their value numbers. This is done in-place, and
1419 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1420 whether any operands were valueized. */
1422 static vec<vn_reference_op_s>
1423 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1425 vn_reference_op_t vro;
1426 unsigned int i;
1428 *valueized_anything = false;
1430 FOR_EACH_VEC_ELT (orig, i, vro)
1432 if (vro->opcode == SSA_NAME
1433 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1435 tree tem = SSA_VAL (vro->op0);
1436 if (tem != vro->op0)
1438 *valueized_anything = true;
1439 vro->op0 = tem;
1441 /* If it transforms from an SSA_NAME to a constant, update
1442 the opcode. */
1443 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1444 vro->opcode = TREE_CODE (vro->op0);
1446 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1448 tree tem = SSA_VAL (vro->op1);
1449 if (tem != vro->op1)
1451 *valueized_anything = true;
1452 vro->op1 = tem;
1455 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1457 tree tem = SSA_VAL (vro->op2);
1458 if (tem != vro->op2)
1460 *valueized_anything = true;
1461 vro->op2 = tem;
1464 /* If it transforms from an SSA_NAME to an address, fold with
1465 a preceding indirect reference. */
1466 if (i > 0
1467 && vro->op0
1468 && TREE_CODE (vro->op0) == ADDR_EXPR
1469 && orig[i - 1].opcode == MEM_REF)
1470 vn_reference_fold_indirect (&orig, &i);
1471 else if (i > 0
1472 && vro->opcode == SSA_NAME
1473 && orig[i - 1].opcode == MEM_REF)
1474 vn_reference_maybe_forwprop_address (&orig, &i);
1475 /* If it transforms a non-constant ARRAY_REF into a constant
1476 one, adjust the constant offset. */
1477 else if (vro->opcode == ARRAY_REF
1478 && vro->off == -1
1479 && TREE_CODE (vro->op0) == INTEGER_CST
1480 && TREE_CODE (vro->op1) == INTEGER_CST
1481 && TREE_CODE (vro->op2) == INTEGER_CST)
1483 offset_int off = ((wi::to_offset (vro->op0)
1484 - wi::to_offset (vro->op1))
1485 * wi::to_offset (vro->op2));
1486 if (wi::fits_shwi_p (off))
1487 vro->off = off.to_shwi ();
1491 return orig;
1494 static vec<vn_reference_op_s>
1495 valueize_refs (vec<vn_reference_op_s> orig)
1497 bool tem;
1498 return valueize_refs_1 (orig, &tem);
1501 static vec<vn_reference_op_s> shared_lookup_references;
1503 /* Create a vector of vn_reference_op_s structures from REF, a
1504 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1505 this function. *VALUEIZED_ANYTHING will specify whether any
1506 operands were valueized. */
1508 static vec<vn_reference_op_s>
1509 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1511 if (!ref)
1512 return vNULL;
1513 shared_lookup_references.truncate (0);
1514 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1515 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1516 valueized_anything);
1517 return shared_lookup_references;
1520 /* Create a vector of vn_reference_op_s structures from CALL, a
1521 call statement. The vector is shared among all callers of
1522 this function. */
1524 static vec<vn_reference_op_s>
1525 valueize_shared_reference_ops_from_call (gcall *call)
1527 if (!call)
1528 return vNULL;
1529 shared_lookup_references.truncate (0);
1530 copy_reference_ops_from_call (call, &shared_lookup_references);
1531 shared_lookup_references = valueize_refs (shared_lookup_references);
1532 return shared_lookup_references;
1535 /* Lookup a SCCVN reference operation VR in the current hash table.
1536 Returns the resulting value number if it exists in the hash table,
1537 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1538 vn_reference_t stored in the hashtable if something is found. */
1540 static tree
1541 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1543 vn_reference_s **slot;
1544 hashval_t hash;
1546 hash = vr->hashcode;
1547 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1548 if (!slot && current_info == optimistic_info)
1549 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1550 if (slot)
1552 if (vnresult)
1553 *vnresult = (vn_reference_t)*slot;
1554 return ((vn_reference_t)*slot)->result;
1557 return NULL_TREE;
1560 static tree *last_vuse_ptr;
1561 static vn_lookup_kind vn_walk_kind;
1562 static vn_lookup_kind default_vn_walk_kind;
1564 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1565 with the current VUSE and performs the expression lookup. */
1567 static void *
1568 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1569 unsigned int cnt, void *vr_)
1571 vn_reference_t vr = (vn_reference_t)vr_;
1572 vn_reference_s **slot;
1573 hashval_t hash;
1575 /* This bounds the stmt walks we perform on reference lookups
1576 to O(1) instead of O(N) where N is the number of dominating
1577 stores. */
1578 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1579 return (void *)-1;
1581 if (last_vuse_ptr)
1582 *last_vuse_ptr = vuse;
1584 /* Fixup vuse and hash. */
1585 if (vr->vuse)
1586 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1587 vr->vuse = vuse_ssa_val (vuse);
1588 if (vr->vuse)
1589 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1591 hash = vr->hashcode;
1592 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1593 if (!slot && current_info == optimistic_info)
1594 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1595 if (slot)
1596 return *slot;
1598 return NULL;
1601 /* Lookup an existing or insert a new vn_reference entry into the
1602 value table for the VUSE, SET, TYPE, OPERANDS reference which
1603 has the value VALUE which is either a constant or an SSA name. */
1605 static vn_reference_t
1606 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1607 alias_set_type set,
1608 tree type,
1609 vec<vn_reference_op_s,
1610 va_heap> operands,
1611 tree value)
1613 vn_reference_s vr1;
1614 vn_reference_t result;
1615 unsigned value_id;
1616 vr1.vuse = vuse;
1617 vr1.operands = operands;
1618 vr1.type = type;
1619 vr1.set = set;
1620 vr1.hashcode = vn_reference_compute_hash (&vr1);
1621 if (vn_reference_lookup_1 (&vr1, &result))
1622 return result;
1623 if (TREE_CODE (value) == SSA_NAME)
1624 value_id = VN_INFO (value)->value_id;
1625 else
1626 value_id = get_or_alloc_constant_value_id (value);
1627 return vn_reference_insert_pieces (vuse, set, type,
1628 operands.copy (), value, value_id);
1631 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1632 from the statement defining VUSE and if not successful tries to
1633 translate *REFP and VR_ through an aggregate copy at the definition
1634 of VUSE. */
1636 static void *
1637 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1638 bool disambiguate_only)
1640 vn_reference_t vr = (vn_reference_t)vr_;
1641 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1642 tree base;
1643 HOST_WIDE_INT offset, maxsize;
1644 static vec<vn_reference_op_s>
1645 lhs_ops = vNULL;
1646 ao_ref lhs_ref;
1647 bool lhs_ref_ok = false;
1649 /* First try to disambiguate after value-replacing in the definitions LHS. */
1650 if (is_gimple_assign (def_stmt))
1652 vec<vn_reference_op_s> tem;
1653 tree lhs = gimple_assign_lhs (def_stmt);
1654 bool valueized_anything = false;
1655 /* Avoid re-allocation overhead. */
1656 lhs_ops.truncate (0);
1657 copy_reference_ops_from_ref (lhs, &lhs_ops);
1658 tem = lhs_ops;
1659 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1660 gcc_assert (lhs_ops == tem);
1661 if (valueized_anything)
1663 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1664 get_alias_set (lhs),
1665 TREE_TYPE (lhs), lhs_ops);
1666 if (lhs_ref_ok
1667 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1668 return NULL;
1670 else
1672 ao_ref_init (&lhs_ref, lhs);
1673 lhs_ref_ok = true;
1676 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1677 && gimple_call_num_args (def_stmt) <= 4)
1679 /* For builtin calls valueize its arguments and call the
1680 alias oracle again. Valueization may improve points-to
1681 info of pointers and constify size and position arguments.
1682 Originally this was motivated by PR61034 which has
1683 conditional calls to free falsely clobbering ref because
1684 of imprecise points-to info of the argument. */
1685 tree oldargs[4];
1686 bool valueized_anything = false;
1687 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1689 oldargs[i] = gimple_call_arg (def_stmt, i);
1690 if (TREE_CODE (oldargs[i]) == SSA_NAME
1691 && VN_INFO (oldargs[i])->valnum != oldargs[i])
1693 gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum);
1694 valueized_anything = true;
1697 if (valueized_anything)
1699 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
1700 ref);
1701 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1702 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1703 if (!res)
1704 return NULL;
1708 if (disambiguate_only)
1709 return (void *)-1;
1711 base = ao_ref_base (ref);
1712 offset = ref->offset;
1713 maxsize = ref->max_size;
1715 /* If we cannot constrain the size of the reference we cannot
1716 test if anything kills it. */
1717 if (maxsize == -1)
1718 return (void *)-1;
1720 /* We can't deduce anything useful from clobbers. */
1721 if (gimple_clobber_p (def_stmt))
1722 return (void *)-1;
1724 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1725 from that definition.
1726 1) Memset. */
1727 if (is_gimple_reg_type (vr->type)
1728 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1729 && integer_zerop (gimple_call_arg (def_stmt, 1))
1730 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))
1731 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1733 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1734 tree base2;
1735 HOST_WIDE_INT offset2, size2, maxsize2;
1736 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1737 size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8;
1738 if ((unsigned HOST_WIDE_INT)size2 / 8
1739 == tree_to_uhwi (gimple_call_arg (def_stmt, 2))
1740 && maxsize2 != -1
1741 && operand_equal_p (base, base2, 0)
1742 && offset2 <= offset
1743 && offset2 + size2 >= offset + maxsize)
1745 tree val = build_zero_cst (vr->type);
1746 return vn_reference_lookup_or_insert_for_pieces
1747 (vuse, vr->set, vr->type, vr->operands, val);
1751 /* 2) Assignment from an empty CONSTRUCTOR. */
1752 else if (is_gimple_reg_type (vr->type)
1753 && gimple_assign_single_p (def_stmt)
1754 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1755 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1757 tree base2;
1758 HOST_WIDE_INT offset2, size2, maxsize2;
1759 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1760 &offset2, &size2, &maxsize2);
1761 if (maxsize2 != -1
1762 && operand_equal_p (base, base2, 0)
1763 && offset2 <= offset
1764 && offset2 + size2 >= offset + maxsize)
1766 tree val = build_zero_cst (vr->type);
1767 return vn_reference_lookup_or_insert_for_pieces
1768 (vuse, vr->set, vr->type, vr->operands, val);
1772 /* 3) Assignment from a constant. We can use folds native encode/interpret
1773 routines to extract the assigned bits. */
1774 else if (vn_walk_kind == VN_WALKREWRITE
1775 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1776 && ref->size == maxsize
1777 && maxsize % BITS_PER_UNIT == 0
1778 && offset % BITS_PER_UNIT == 0
1779 && is_gimple_reg_type (vr->type)
1780 && gimple_assign_single_p (def_stmt)
1781 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1783 tree base2;
1784 HOST_WIDE_INT offset2, size2, maxsize2;
1785 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1786 &offset2, &size2, &maxsize2);
1787 if (maxsize2 != -1
1788 && maxsize2 == size2
1789 && size2 % BITS_PER_UNIT == 0
1790 && offset2 % BITS_PER_UNIT == 0
1791 && operand_equal_p (base, base2, 0)
1792 && offset2 <= offset
1793 && offset2 + size2 >= offset + maxsize)
1795 /* We support up to 512-bit values (for V8DFmode). */
1796 unsigned char buffer[64];
1797 int len;
1799 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1800 buffer, sizeof (buffer));
1801 if (len > 0)
1803 tree val = native_interpret_expr (vr->type,
1804 buffer
1805 + ((offset - offset2)
1806 / BITS_PER_UNIT),
1807 ref->size / BITS_PER_UNIT);
1808 if (val)
1809 return vn_reference_lookup_or_insert_for_pieces
1810 (vuse, vr->set, vr->type, vr->operands, val);
1815 /* 4) Assignment from an SSA name which definition we may be able
1816 to access pieces from. */
1817 else if (ref->size == maxsize
1818 && is_gimple_reg_type (vr->type)
1819 && gimple_assign_single_p (def_stmt)
1820 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1822 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1823 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1824 if (is_gimple_assign (def_stmt2)
1825 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1826 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1827 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1829 tree base2;
1830 HOST_WIDE_INT offset2, size2, maxsize2, off;
1831 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1832 &offset2, &size2, &maxsize2);
1833 off = offset - offset2;
1834 if (maxsize2 != -1
1835 && maxsize2 == size2
1836 && operand_equal_p (base, base2, 0)
1837 && offset2 <= offset
1838 && offset2 + size2 >= offset + maxsize)
1840 tree val = NULL_TREE;
1841 HOST_WIDE_INT elsz
1842 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1843 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1845 if (off == 0)
1846 val = gimple_assign_rhs1 (def_stmt2);
1847 else if (off == elsz)
1848 val = gimple_assign_rhs2 (def_stmt2);
1850 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1851 && off % elsz == 0)
1853 tree ctor = gimple_assign_rhs1 (def_stmt2);
1854 unsigned i = off / elsz;
1855 if (i < CONSTRUCTOR_NELTS (ctor))
1857 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1858 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1860 if (TREE_CODE (TREE_TYPE (elt->value))
1861 != VECTOR_TYPE)
1862 val = elt->value;
1866 if (val)
1867 return vn_reference_lookup_or_insert_for_pieces
1868 (vuse, vr->set, vr->type, vr->operands, val);
1873 /* 5) For aggregate copies translate the reference through them if
1874 the copy kills ref. */
1875 else if (vn_walk_kind == VN_WALKREWRITE
1876 && gimple_assign_single_p (def_stmt)
1877 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1878 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1879 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1881 tree base2;
1882 HOST_WIDE_INT offset2, size2, maxsize2;
1883 int i, j;
1884 auto_vec<vn_reference_op_s> rhs;
1885 vn_reference_op_t vro;
1886 ao_ref r;
1888 if (!lhs_ref_ok)
1889 return (void *)-1;
1891 /* See if the assignment kills REF. */
1892 base2 = ao_ref_base (&lhs_ref);
1893 offset2 = lhs_ref.offset;
1894 size2 = lhs_ref.size;
1895 maxsize2 = lhs_ref.max_size;
1896 if (maxsize2 == -1
1897 || (base != base2
1898 && (TREE_CODE (base) != MEM_REF
1899 || TREE_CODE (base2) != MEM_REF
1900 || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0)
1901 || !tree_int_cst_equal (TREE_OPERAND (base, 1),
1902 TREE_OPERAND (base2, 1))))
1903 || offset2 > offset
1904 || offset2 + size2 < offset + maxsize)
1905 return (void *)-1;
1907 /* Find the common base of ref and the lhs. lhs_ops already
1908 contains valueized operands for the lhs. */
1909 i = vr->operands.length () - 1;
1910 j = lhs_ops.length () - 1;
1911 while (j >= 0 && i >= 0
1912 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1914 i--;
1915 j--;
1918 /* ??? The innermost op should always be a MEM_REF and we already
1919 checked that the assignment to the lhs kills vr. Thus for
1920 aggregate copies using char[] types the vn_reference_op_eq
1921 may fail when comparing types for compatibility. But we really
1922 don't care here - further lookups with the rewritten operands
1923 will simply fail if we messed up types too badly. */
1924 HOST_WIDE_INT extra_off = 0;
1925 if (j == 0 && i >= 0
1926 && lhs_ops[0].opcode == MEM_REF
1927 && lhs_ops[0].off != -1)
1929 if (lhs_ops[0].off == vr->operands[i].off)
1930 i--, j--;
1931 else if (vr->operands[i].opcode == MEM_REF
1932 && vr->operands[i].off != -1)
1934 extra_off = vr->operands[i].off - lhs_ops[0].off;
1935 i--, j--;
1939 /* i now points to the first additional op.
1940 ??? LHS may not be completely contained in VR, one or more
1941 VIEW_CONVERT_EXPRs could be in its way. We could at least
1942 try handling outermost VIEW_CONVERT_EXPRs. */
1943 if (j != -1)
1944 return (void *)-1;
1946 /* Now re-write REF to be based on the rhs of the assignment. */
1947 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1949 /* Apply an extra offset to the inner MEM_REF of the RHS. */
1950 if (extra_off != 0)
1952 if (rhs.length () < 2
1953 || rhs[0].opcode != MEM_REF
1954 || rhs[0].off == -1)
1955 return (void *)-1;
1956 rhs[0].off += extra_off;
1957 rhs[0].op0 = int_const_binop (PLUS_EXPR, rhs[0].op0,
1958 build_int_cst (TREE_TYPE (rhs[0].op0),
1959 extra_off));
1962 /* We need to pre-pend vr->operands[0..i] to rhs. */
1963 vec<vn_reference_op_s> old = vr->operands;
1964 if (i + 1 + rhs.length () > vr->operands.length ())
1966 vr->operands.safe_grow (i + 1 + rhs.length ());
1967 if (old == shared_lookup_references)
1968 shared_lookup_references = vr->operands;
1970 else
1971 vr->operands.truncate (i + 1 + rhs.length ());
1972 FOR_EACH_VEC_ELT (rhs, j, vro)
1973 vr->operands[i + 1 + j] = *vro;
1974 vr->operands = valueize_refs (vr->operands);
1975 if (old == shared_lookup_references)
1976 shared_lookup_references = vr->operands;
1977 vr->hashcode = vn_reference_compute_hash (vr);
1979 /* Try folding the new reference to a constant. */
1980 tree val = fully_constant_vn_reference_p (vr);
1981 if (val)
1982 return vn_reference_lookup_or_insert_for_pieces
1983 (vuse, vr->set, vr->type, vr->operands, val);
1985 /* Adjust *ref from the new operands. */
1986 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1987 return (void *)-1;
1988 /* This can happen with bitfields. */
1989 if (ref->size != r.size)
1990 return (void *)-1;
1991 *ref = r;
1993 /* Do not update last seen VUSE after translating. */
1994 last_vuse_ptr = NULL;
1996 /* Keep looking for the adjusted *REF / VR pair. */
1997 return NULL;
2000 /* 6) For memcpy copies translate the reference through them if
2001 the copy kills ref. */
2002 else if (vn_walk_kind == VN_WALKREWRITE
2003 && is_gimple_reg_type (vr->type)
2004 /* ??? Handle BCOPY as well. */
2005 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
2006 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
2007 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
2008 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2009 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
2010 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
2011 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
2012 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)))
2014 tree lhs, rhs;
2015 ao_ref r;
2016 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
2017 vn_reference_op_s op;
2018 HOST_WIDE_INT at;
2021 /* Only handle non-variable, addressable refs. */
2022 if (ref->size != maxsize
2023 || offset % BITS_PER_UNIT != 0
2024 || ref->size % BITS_PER_UNIT != 0)
2025 return (void *)-1;
2027 /* Extract a pointer base and an offset for the destination. */
2028 lhs = gimple_call_arg (def_stmt, 0);
2029 lhs_offset = 0;
2030 if (TREE_CODE (lhs) == SSA_NAME)
2032 lhs = SSA_VAL (lhs);
2033 if (TREE_CODE (lhs) == SSA_NAME)
2035 gimple def_stmt = SSA_NAME_DEF_STMT (lhs);
2036 if (gimple_assign_single_p (def_stmt)
2037 && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR)
2038 lhs = gimple_assign_rhs1 (def_stmt);
2041 if (TREE_CODE (lhs) == ADDR_EXPR)
2043 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
2044 &lhs_offset);
2045 if (!tem)
2046 return (void *)-1;
2047 if (TREE_CODE (tem) == MEM_REF
2048 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2050 lhs = TREE_OPERAND (tem, 0);
2051 if (TREE_CODE (lhs) == SSA_NAME)
2052 lhs = SSA_VAL (lhs);
2053 lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2055 else if (DECL_P (tem))
2056 lhs = build_fold_addr_expr (tem);
2057 else
2058 return (void *)-1;
2060 if (TREE_CODE (lhs) != SSA_NAME
2061 && TREE_CODE (lhs) != ADDR_EXPR)
2062 return (void *)-1;
2064 /* Extract a pointer base and an offset for the source. */
2065 rhs = gimple_call_arg (def_stmt, 1);
2066 rhs_offset = 0;
2067 if (TREE_CODE (rhs) == SSA_NAME)
2068 rhs = SSA_VAL (rhs);
2069 if (TREE_CODE (rhs) == ADDR_EXPR)
2071 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
2072 &rhs_offset);
2073 if (!tem)
2074 return (void *)-1;
2075 if (TREE_CODE (tem) == MEM_REF
2076 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2078 rhs = TREE_OPERAND (tem, 0);
2079 rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2081 else if (DECL_P (tem))
2082 rhs = build_fold_addr_expr (tem);
2083 else
2084 return (void *)-1;
2086 if (TREE_CODE (rhs) != SSA_NAME
2087 && TREE_CODE (rhs) != ADDR_EXPR)
2088 return (void *)-1;
2090 copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2));
2092 /* The bases of the destination and the references have to agree. */
2093 if ((TREE_CODE (base) != MEM_REF
2094 && !DECL_P (base))
2095 || (TREE_CODE (base) == MEM_REF
2096 && (TREE_OPERAND (base, 0) != lhs
2097 || !tree_fits_uhwi_p (TREE_OPERAND (base, 1))))
2098 || (DECL_P (base)
2099 && (TREE_CODE (lhs) != ADDR_EXPR
2100 || TREE_OPERAND (lhs, 0) != base)))
2101 return (void *)-1;
2103 at = offset / BITS_PER_UNIT;
2104 if (TREE_CODE (base) == MEM_REF)
2105 at += tree_to_uhwi (TREE_OPERAND (base, 1));
2106 /* If the access is completely outside of the memcpy destination
2107 area there is no aliasing. */
2108 if (lhs_offset >= at + maxsize / BITS_PER_UNIT
2109 || lhs_offset + copy_size <= at)
2110 return NULL;
2111 /* And the access has to be contained within the memcpy destination. */
2112 if (lhs_offset > at
2113 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
2114 return (void *)-1;
2116 /* Make room for 2 operands in the new reference. */
2117 if (vr->operands.length () < 2)
2119 vec<vn_reference_op_s> old = vr->operands;
2120 vr->operands.safe_grow_cleared (2);
2121 if (old == shared_lookup_references
2122 && vr->operands != old)
2123 shared_lookup_references = vr->operands;
2125 else
2126 vr->operands.truncate (2);
2128 /* The looked-through reference is a simple MEM_REF. */
2129 memset (&op, 0, sizeof (op));
2130 op.type = vr->type;
2131 op.opcode = MEM_REF;
2132 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
2133 op.off = at - lhs_offset + rhs_offset;
2134 vr->operands[0] = op;
2135 op.type = TREE_TYPE (rhs);
2136 op.opcode = TREE_CODE (rhs);
2137 op.op0 = rhs;
2138 op.off = -1;
2139 vr->operands[1] = op;
2140 vr->hashcode = vn_reference_compute_hash (vr);
2142 /* Adjust *ref from the new operands. */
2143 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2144 return (void *)-1;
2145 /* This can happen with bitfields. */
2146 if (ref->size != r.size)
2147 return (void *)-1;
2148 *ref = r;
2150 /* Do not update last seen VUSE after translating. */
2151 last_vuse_ptr = NULL;
2153 /* Keep looking for the adjusted *REF / VR pair. */
2154 return NULL;
2157 /* Bail out and stop walking. */
2158 return (void *)-1;
2161 /* Lookup a reference operation by it's parts, in the current hash table.
2162 Returns the resulting value number if it exists in the hash table,
2163 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2164 vn_reference_t stored in the hashtable if something is found. */
2166 tree
2167 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2168 vec<vn_reference_op_s> operands,
2169 vn_reference_t *vnresult, vn_lookup_kind kind)
2171 struct vn_reference_s vr1;
2172 vn_reference_t tmp;
2173 tree cst;
2175 if (!vnresult)
2176 vnresult = &tmp;
2177 *vnresult = NULL;
2179 vr1.vuse = vuse_ssa_val (vuse);
2180 shared_lookup_references.truncate (0);
2181 shared_lookup_references.safe_grow (operands.length ());
2182 memcpy (shared_lookup_references.address (),
2183 operands.address (),
2184 sizeof (vn_reference_op_s)
2185 * operands.length ());
2186 vr1.operands = operands = shared_lookup_references
2187 = valueize_refs (shared_lookup_references);
2188 vr1.type = type;
2189 vr1.set = set;
2190 vr1.hashcode = vn_reference_compute_hash (&vr1);
2191 if ((cst = fully_constant_vn_reference_p (&vr1)))
2192 return cst;
2194 vn_reference_lookup_1 (&vr1, vnresult);
2195 if (!*vnresult
2196 && kind != VN_NOWALK
2197 && vr1.vuse)
2199 ao_ref r;
2200 vn_walk_kind = kind;
2201 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2202 *vnresult =
2203 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2204 vn_reference_lookup_2,
2205 vn_reference_lookup_3,
2206 vuse_ssa_val, &vr1);
2207 gcc_checking_assert (vr1.operands == shared_lookup_references);
2210 if (*vnresult)
2211 return (*vnresult)->result;
2213 return NULL_TREE;
2216 /* Lookup OP in the current hash table, and return the resulting value
2217 number if it exists in the hash table. Return NULL_TREE if it does
2218 not exist in the hash table or if the result field of the structure
2219 was NULL.. VNRESULT will be filled in with the vn_reference_t
2220 stored in the hashtable if one exists. */
2222 tree
2223 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2224 vn_reference_t *vnresult)
2226 vec<vn_reference_op_s> operands;
2227 struct vn_reference_s vr1;
2228 tree cst;
2229 bool valuezied_anything;
2231 if (vnresult)
2232 *vnresult = NULL;
2234 vr1.vuse = vuse_ssa_val (vuse);
2235 vr1.operands = operands
2236 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2237 vr1.type = TREE_TYPE (op);
2238 vr1.set = get_alias_set (op);
2239 vr1.hashcode = vn_reference_compute_hash (&vr1);
2240 if ((cst = fully_constant_vn_reference_p (&vr1)))
2241 return cst;
2243 if (kind != VN_NOWALK
2244 && vr1.vuse)
2246 vn_reference_t wvnresult;
2247 ao_ref r;
2248 /* Make sure to use a valueized reference if we valueized anything.
2249 Otherwise preserve the full reference for advanced TBAA. */
2250 if (!valuezied_anything
2251 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2252 vr1.operands))
2253 ao_ref_init (&r, op);
2254 vn_walk_kind = kind;
2255 wvnresult =
2256 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2257 vn_reference_lookup_2,
2258 vn_reference_lookup_3,
2259 vuse_ssa_val, &vr1);
2260 gcc_checking_assert (vr1.operands == shared_lookup_references);
2261 if (wvnresult)
2263 if (vnresult)
2264 *vnresult = wvnresult;
2265 return wvnresult->result;
2268 return NULL_TREE;
2271 return vn_reference_lookup_1 (&vr1, vnresult);
2274 /* Lookup CALL in the current hash table and return the entry in
2275 *VNRESULT if found. Populates *VR for the hashtable lookup. */
2277 void
2278 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
2279 vn_reference_t vr)
2281 if (vnresult)
2282 *vnresult = NULL;
2284 tree vuse = gimple_vuse (call);
2286 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2287 vr->operands = valueize_shared_reference_ops_from_call (call);
2288 vr->type = gimple_expr_type (call);
2289 vr->set = 0;
2290 vr->hashcode = vn_reference_compute_hash (vr);
2291 vn_reference_lookup_1 (vr, vnresult);
2294 /* Insert OP into the current hash table with a value number of
2295 RESULT, and return the resulting reference structure we created. */
2297 static vn_reference_t
2298 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2300 vn_reference_s **slot;
2301 vn_reference_t vr1;
2302 bool tem;
2304 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2305 if (TREE_CODE (result) == SSA_NAME)
2306 vr1->value_id = VN_INFO (result)->value_id;
2307 else
2308 vr1->value_id = get_or_alloc_constant_value_id (result);
2309 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2310 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2311 vr1->type = TREE_TYPE (op);
2312 vr1->set = get_alias_set (op);
2313 vr1->hashcode = vn_reference_compute_hash (vr1);
2314 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2315 vr1->result_vdef = vdef;
2317 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2318 INSERT);
2320 /* Because we lookup stores using vuses, and value number failures
2321 using the vdefs (see visit_reference_op_store for how and why),
2322 it's possible that on failure we may try to insert an already
2323 inserted store. This is not wrong, there is no ssa name for a
2324 store that we could use as a differentiator anyway. Thus, unlike
2325 the other lookup functions, you cannot gcc_assert (!*slot)
2326 here. */
2328 /* But free the old slot in case of a collision. */
2329 if (*slot)
2330 free_reference (*slot);
2332 *slot = vr1;
2333 return vr1;
2336 /* Insert a reference by it's pieces into the current hash table with
2337 a value number of RESULT. Return the resulting reference
2338 structure we created. */
2340 vn_reference_t
2341 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2342 vec<vn_reference_op_s> operands,
2343 tree result, unsigned int value_id)
2346 vn_reference_s **slot;
2347 vn_reference_t vr1;
2349 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2350 vr1->value_id = value_id;
2351 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2352 vr1->operands = valueize_refs (operands);
2353 vr1->type = type;
2354 vr1->set = set;
2355 vr1->hashcode = vn_reference_compute_hash (vr1);
2356 if (result && TREE_CODE (result) == SSA_NAME)
2357 result = SSA_VAL (result);
2358 vr1->result = result;
2360 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2361 INSERT);
2363 /* At this point we should have all the things inserted that we have
2364 seen before, and we should never try inserting something that
2365 already exists. */
2366 gcc_assert (!*slot);
2367 if (*slot)
2368 free_reference (*slot);
2370 *slot = vr1;
2371 return vr1;
2374 /* Compute and return the hash value for nary operation VBO1. */
2376 static hashval_t
2377 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2379 inchash::hash hstate;
2380 unsigned i;
2382 for (i = 0; i < vno1->length; ++i)
2383 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2384 vno1->op[i] = SSA_VAL (vno1->op[i]);
2386 if (vno1->length == 2
2387 && commutative_tree_code (vno1->opcode)
2388 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2390 tree temp = vno1->op[0];
2391 vno1->op[0] = vno1->op[1];
2392 vno1->op[1] = temp;
2395 hstate.add_int (vno1->opcode);
2396 for (i = 0; i < vno1->length; ++i)
2397 inchash::add_expr (vno1->op[i], hstate);
2399 return hstate.end ();
2402 /* Compare nary operations VNO1 and VNO2 and return true if they are
2403 equivalent. */
2405 bool
2406 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2408 unsigned i;
2410 if (vno1->hashcode != vno2->hashcode)
2411 return false;
2413 if (vno1->length != vno2->length)
2414 return false;
2416 if (vno1->opcode != vno2->opcode
2417 || !types_compatible_p (vno1->type, vno2->type))
2418 return false;
2420 for (i = 0; i < vno1->length; ++i)
2421 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2422 return false;
2424 return true;
2427 /* Initialize VNO from the pieces provided. */
2429 static void
2430 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2431 enum tree_code code, tree type, tree *ops)
2433 vno->opcode = code;
2434 vno->length = length;
2435 vno->type = type;
2436 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2439 /* Initialize VNO from OP. */
2441 static void
2442 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2444 unsigned i;
2446 vno->opcode = TREE_CODE (op);
2447 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2448 vno->type = TREE_TYPE (op);
2449 for (i = 0; i < vno->length; ++i)
2450 vno->op[i] = TREE_OPERAND (op, i);
2453 /* Return the number of operands for a vn_nary ops structure from STMT. */
2455 static unsigned int
2456 vn_nary_length_from_stmt (gimple stmt)
2458 switch (gimple_assign_rhs_code (stmt))
2460 case REALPART_EXPR:
2461 case IMAGPART_EXPR:
2462 case VIEW_CONVERT_EXPR:
2463 return 1;
2465 case BIT_FIELD_REF:
2466 return 3;
2468 case CONSTRUCTOR:
2469 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2471 default:
2472 return gimple_num_ops (stmt) - 1;
2476 /* Initialize VNO from STMT. */
2478 static void
2479 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2481 unsigned i;
2483 vno->opcode = gimple_assign_rhs_code (stmt);
2484 vno->type = gimple_expr_type (stmt);
2485 switch (vno->opcode)
2487 case REALPART_EXPR:
2488 case IMAGPART_EXPR:
2489 case VIEW_CONVERT_EXPR:
2490 vno->length = 1;
2491 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2492 break;
2494 case BIT_FIELD_REF:
2495 vno->length = 3;
2496 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2497 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2498 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2499 break;
2501 case CONSTRUCTOR:
2502 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2503 for (i = 0; i < vno->length; ++i)
2504 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2505 break;
2507 default:
2508 gcc_checking_assert (!gimple_assign_single_p (stmt));
2509 vno->length = gimple_num_ops (stmt) - 1;
2510 for (i = 0; i < vno->length; ++i)
2511 vno->op[i] = gimple_op (stmt, i + 1);
2515 /* Compute the hashcode for VNO and look for it in the hash table;
2516 return the resulting value number if it exists in the hash table.
2517 Return NULL_TREE if it does not exist in the hash table or if the
2518 result field of the operation is NULL. VNRESULT will contain the
2519 vn_nary_op_t from the hashtable if it exists. */
2521 static tree
2522 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2524 vn_nary_op_s **slot;
2526 if (vnresult)
2527 *vnresult = NULL;
2529 vno->hashcode = vn_nary_op_compute_hash (vno);
2530 slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode,
2531 NO_INSERT);
2532 if (!slot && current_info == optimistic_info)
2533 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode,
2534 NO_INSERT);
2535 if (!slot)
2536 return NULL_TREE;
2537 if (vnresult)
2538 *vnresult = *slot;
2539 return (*slot)->result;
2542 /* Lookup a n-ary operation by its pieces and return the resulting value
2543 number if it exists in the hash table. Return NULL_TREE if it does
2544 not exist in the hash table or if the result field of the operation
2545 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2546 if it exists. */
2548 tree
2549 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2550 tree type, tree *ops, vn_nary_op_t *vnresult)
2552 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2553 sizeof_vn_nary_op (length));
2554 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2555 return vn_nary_op_lookup_1 (vno1, vnresult);
2558 /* Lookup OP in the current hash table, and return the resulting value
2559 number if it exists in the hash table. Return NULL_TREE if it does
2560 not exist in the hash table or if the result field of the operation
2561 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2562 if it exists. */
2564 tree
2565 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2567 vn_nary_op_t vno1
2568 = XALLOCAVAR (struct vn_nary_op_s,
2569 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2570 init_vn_nary_op_from_op (vno1, op);
2571 return vn_nary_op_lookup_1 (vno1, vnresult);
2574 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2575 value number if it exists in the hash table. Return NULL_TREE if
2576 it does not exist in the hash table. VNRESULT will contain the
2577 vn_nary_op_t from the hashtable if it exists. */
2579 tree
2580 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2582 vn_nary_op_t vno1
2583 = XALLOCAVAR (struct vn_nary_op_s,
2584 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2585 init_vn_nary_op_from_stmt (vno1, stmt);
2586 return vn_nary_op_lookup_1 (vno1, vnresult);
2589 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2591 static vn_nary_op_t
2592 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2594 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2597 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2598 obstack. */
2600 static vn_nary_op_t
2601 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2603 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2604 &current_info->nary_obstack);
2606 vno1->value_id = value_id;
2607 vno1->length = length;
2608 vno1->result = result;
2610 return vno1;
2613 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2614 VNO->HASHCODE first. */
2616 static vn_nary_op_t
2617 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
2618 bool compute_hash)
2620 vn_nary_op_s **slot;
2622 if (compute_hash)
2623 vno->hashcode = vn_nary_op_compute_hash (vno);
2625 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
2626 gcc_assert (!*slot);
2628 *slot = vno;
2629 return vno;
2632 /* Insert a n-ary operation into the current hash table using it's
2633 pieces. Return the vn_nary_op_t structure we created and put in
2634 the hashtable. */
2636 vn_nary_op_t
2637 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2638 tree type, tree *ops,
2639 tree result, unsigned int value_id)
2641 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2642 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2643 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2646 /* Insert OP into the current hash table with a value number of
2647 RESULT. Return the vn_nary_op_t structure we created and put in
2648 the hashtable. */
2650 vn_nary_op_t
2651 vn_nary_op_insert (tree op, tree result)
2653 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2654 vn_nary_op_t vno1;
2656 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2657 init_vn_nary_op_from_op (vno1, op);
2658 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2661 /* Insert the rhs of STMT into the current hash table with a value number of
2662 RESULT. */
2664 vn_nary_op_t
2665 vn_nary_op_insert_stmt (gimple stmt, tree result)
2667 vn_nary_op_t vno1
2668 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2669 result, VN_INFO (result)->value_id);
2670 init_vn_nary_op_from_stmt (vno1, stmt);
2671 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2674 /* Compute a hashcode for PHI operation VP1 and return it. */
2676 static inline hashval_t
2677 vn_phi_compute_hash (vn_phi_t vp1)
2679 inchash::hash hstate (vp1->block->index);
2680 int i;
2681 tree phi1op;
2682 tree type;
2684 /* If all PHI arguments are constants we need to distinguish
2685 the PHI node via its type. */
2686 type = vp1->type;
2687 hstate.merge_hash (vn_hash_type (type));
2689 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2691 if (phi1op == VN_TOP)
2692 continue;
2693 inchash::add_expr (phi1op, hstate);
2696 return hstate.end ();
2699 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2701 static int
2702 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2704 if (vp1->hashcode != vp2->hashcode)
2705 return false;
2707 if (vp1->block == vp2->block)
2709 int i;
2710 tree phi1op;
2712 /* If the PHI nodes do not have compatible types
2713 they are not the same. */
2714 if (!types_compatible_p (vp1->type, vp2->type))
2715 return false;
2717 /* Any phi in the same block will have it's arguments in the
2718 same edge order, because of how we store phi nodes. */
2719 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2721 tree phi2op = vp2->phiargs[i];
2722 if (phi1op == VN_TOP || phi2op == VN_TOP)
2723 continue;
2724 if (!expressions_equal_p (phi1op, phi2op))
2725 return false;
2727 return true;
2729 return false;
2732 static vec<tree> shared_lookup_phiargs;
2734 /* Lookup PHI in the current hash table, and return the resulting
2735 value number if it exists in the hash table. Return NULL_TREE if
2736 it does not exist in the hash table. */
2738 static tree
2739 vn_phi_lookup (gimple phi)
2741 vn_phi_s **slot;
2742 struct vn_phi_s vp1;
2743 unsigned i;
2745 shared_lookup_phiargs.truncate (0);
2747 /* Canonicalize the SSA_NAME's to their value number. */
2748 for (i = 0; i < gimple_phi_num_args (phi); i++)
2750 tree def = PHI_ARG_DEF (phi, i);
2751 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2752 shared_lookup_phiargs.safe_push (def);
2754 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2755 vp1.phiargs = shared_lookup_phiargs;
2756 vp1.block = gimple_bb (phi);
2757 vp1.hashcode = vn_phi_compute_hash (&vp1);
2758 slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2759 NO_INSERT);
2760 if (!slot && current_info == optimistic_info)
2761 slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2762 NO_INSERT);
2763 if (!slot)
2764 return NULL_TREE;
2765 return (*slot)->result;
2768 /* Insert PHI into the current hash table with a value number of
2769 RESULT. */
2771 static vn_phi_t
2772 vn_phi_insert (gimple phi, tree result)
2774 vn_phi_s **slot;
2775 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2776 unsigned i;
2777 vec<tree> args = vNULL;
2779 /* Canonicalize the SSA_NAME's to their value number. */
2780 for (i = 0; i < gimple_phi_num_args (phi); i++)
2782 tree def = PHI_ARG_DEF (phi, i);
2783 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2784 args.safe_push (def);
2786 vp1->value_id = VN_INFO (result)->value_id;
2787 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2788 vp1->phiargs = args;
2789 vp1->block = gimple_bb (phi);
2790 vp1->result = result;
2791 vp1->hashcode = vn_phi_compute_hash (vp1);
2793 slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2795 /* Because we iterate over phi operations more than once, it's
2796 possible the slot might already exist here, hence no assert.*/
2797 *slot = vp1;
2798 return vp1;
2802 /* Print set of components in strongly connected component SCC to OUT. */
2804 static void
2805 print_scc (FILE *out, vec<tree> scc)
2807 tree var;
2808 unsigned int i;
2810 fprintf (out, "SCC consists of:");
2811 FOR_EACH_VEC_ELT (scc, i, var)
2813 fprintf (out, " ");
2814 print_generic_expr (out, var, 0);
2816 fprintf (out, "\n");
2819 /* Set the value number of FROM to TO, return true if it has changed
2820 as a result. */
2822 static inline bool
2823 set_ssa_val_to (tree from, tree to)
2825 tree currval = SSA_VAL (from);
2826 HOST_WIDE_INT toff, coff;
2828 /* The only thing we allow as value numbers are ssa_names
2829 and invariants. So assert that here. We don't allow VN_TOP
2830 as visiting a stmt should produce a value-number other than
2831 that.
2832 ??? Still VN_TOP can happen for unreachable code, so force
2833 it to varying in that case. Not all code is prepared to
2834 get VN_TOP on valueization. */
2835 if (to == VN_TOP)
2837 if (dump_file && (dump_flags & TDF_DETAILS))
2838 fprintf (dump_file, "Forcing value number to varying on "
2839 "receiving VN_TOP\n");
2840 to = from;
2843 gcc_assert (to != NULL_TREE
2844 && ((TREE_CODE (to) == SSA_NAME
2845 && (to == from || SSA_VAL (to) == to))
2846 || is_gimple_min_invariant (to)));
2848 if (from != to)
2850 if (currval == from)
2852 if (dump_file && (dump_flags & TDF_DETAILS))
2854 fprintf (dump_file, "Not changing value number of ");
2855 print_generic_expr (dump_file, from, 0);
2856 fprintf (dump_file, " from VARYING to ");
2857 print_generic_expr (dump_file, to, 0);
2858 fprintf (dump_file, "\n");
2860 return false;
2862 else if (TREE_CODE (to) == SSA_NAME
2863 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2864 to = from;
2867 if (dump_file && (dump_flags & TDF_DETAILS))
2869 fprintf (dump_file, "Setting value number of ");
2870 print_generic_expr (dump_file, from, 0);
2871 fprintf (dump_file, " to ");
2872 print_generic_expr (dump_file, to, 0);
2875 if (currval != to
2876 && !operand_equal_p (currval, to, 0)
2877 /* ??? For addresses involving volatile objects or types operand_equal_p
2878 does not reliably detect ADDR_EXPRs as equal. We know we are only
2879 getting invariant gimple addresses here, so can use
2880 get_addr_base_and_unit_offset to do this comparison. */
2881 && !(TREE_CODE (currval) == ADDR_EXPR
2882 && TREE_CODE (to) == ADDR_EXPR
2883 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2884 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2885 && coff == toff))
2887 VN_INFO (from)->valnum = to;
2888 if (dump_file && (dump_flags & TDF_DETAILS))
2889 fprintf (dump_file, " (changed)\n");
2890 return true;
2892 if (dump_file && (dump_flags & TDF_DETAILS))
2893 fprintf (dump_file, "\n");
2894 return false;
2897 /* Mark as processed all the definitions in the defining stmt of USE, or
2898 the USE itself. */
2900 static void
2901 mark_use_processed (tree use)
2903 ssa_op_iter iter;
2904 def_operand_p defp;
2905 gimple stmt = SSA_NAME_DEF_STMT (use);
2907 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2909 VN_INFO (use)->use_processed = true;
2910 return;
2913 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2915 tree def = DEF_FROM_PTR (defp);
2917 VN_INFO (def)->use_processed = true;
2921 /* Set all definitions in STMT to value number to themselves.
2922 Return true if a value number changed. */
2924 static bool
2925 defs_to_varying (gimple stmt)
2927 bool changed = false;
2928 ssa_op_iter iter;
2929 def_operand_p defp;
2931 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2933 tree def = DEF_FROM_PTR (defp);
2934 changed |= set_ssa_val_to (def, def);
2936 return changed;
2939 static bool expr_has_constants (tree expr);
2941 /* Visit a copy between LHS and RHS, return true if the value number
2942 changed. */
2944 static bool
2945 visit_copy (tree lhs, tree rhs)
2947 /* The copy may have a more interesting constant filled expression
2948 (we don't, since we know our RHS is just an SSA name). */
2949 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2950 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2952 /* And finally valueize. */
2953 rhs = SSA_VAL (rhs);
2955 return set_ssa_val_to (lhs, rhs);
2958 /* Visit a nary operator RHS, value number it, and return true if the
2959 value number of LHS has changed as a result. */
2961 static bool
2962 visit_nary_op (tree lhs, gimple stmt)
2964 bool changed = false;
2965 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2967 if (result)
2968 changed = set_ssa_val_to (lhs, result);
2969 else
2971 changed = set_ssa_val_to (lhs, lhs);
2972 vn_nary_op_insert_stmt (stmt, lhs);
2975 return changed;
2978 /* Visit a call STMT storing into LHS. Return true if the value number
2979 of the LHS has changed as a result. */
2981 static bool
2982 visit_reference_op_call (tree lhs, gcall *stmt)
2984 bool changed = false;
2985 struct vn_reference_s vr1;
2986 vn_reference_t vnresult = NULL;
2987 tree vdef = gimple_vdef (stmt);
2989 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2990 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2991 lhs = NULL_TREE;
2993 vn_reference_lookup_call (stmt, &vnresult, &vr1);
2994 if (vnresult)
2996 if (vnresult->result_vdef && vdef)
2997 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2999 if (!vnresult->result && lhs)
3000 vnresult->result = lhs;
3002 if (vnresult->result && lhs)
3004 changed |= set_ssa_val_to (lhs, vnresult->result);
3006 if (VN_INFO (vnresult->result)->has_constants)
3007 VN_INFO (lhs)->has_constants = true;
3010 else
3012 vn_reference_t vr2;
3013 vn_reference_s **slot;
3014 if (vdef)
3015 changed |= set_ssa_val_to (vdef, vdef);
3016 if (lhs)
3017 changed |= set_ssa_val_to (lhs, lhs);
3018 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
3019 vr2->vuse = vr1.vuse;
3020 /* As we are not walking the virtual operand chain we know the
3021 shared_lookup_references are still original so we can re-use
3022 them here. */
3023 vr2->operands = vr1.operands.copy ();
3024 vr2->type = vr1.type;
3025 vr2->set = vr1.set;
3026 vr2->hashcode = vr1.hashcode;
3027 vr2->result = lhs;
3028 vr2->result_vdef = vdef;
3029 slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode,
3030 INSERT);
3031 gcc_assert (!*slot);
3032 *slot = vr2;
3035 return changed;
3038 /* Visit a load from a reference operator RHS, part of STMT, value number it,
3039 and return true if the value number of the LHS has changed as a result. */
3041 static bool
3042 visit_reference_op_load (tree lhs, tree op, gimple stmt)
3044 bool changed = false;
3045 tree last_vuse;
3046 tree result;
3048 last_vuse = gimple_vuse (stmt);
3049 last_vuse_ptr = &last_vuse;
3050 result = vn_reference_lookup (op, gimple_vuse (stmt),
3051 default_vn_walk_kind, NULL);
3052 last_vuse_ptr = NULL;
3054 /* We handle type-punning through unions by value-numbering based
3055 on offset and size of the access. Be prepared to handle a
3056 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
3057 if (result
3058 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
3060 /* We will be setting the value number of lhs to the value number
3061 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
3062 So first simplify and lookup this expression to see if it
3063 is already available. */
3064 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
3065 if ((CONVERT_EXPR_P (val)
3066 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
3067 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
3069 tree tem = vn_get_expr_for (TREE_OPERAND (val, 0));
3070 if ((CONVERT_EXPR_P (tem)
3071 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
3072 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
3073 TREE_TYPE (val), tem)))
3074 val = tem;
3076 result = val;
3077 if (!is_gimple_min_invariant (val)
3078 && TREE_CODE (val) != SSA_NAME)
3079 result = vn_nary_op_lookup (val, NULL);
3080 /* If the expression is not yet available, value-number lhs to
3081 a new SSA_NAME we create. */
3082 if (!result)
3084 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
3085 "vntemp");
3086 /* Initialize value-number information properly. */
3087 VN_INFO_GET (result)->valnum = result;
3088 VN_INFO (result)->value_id = get_next_value_id ();
3089 VN_INFO (result)->expr = val;
3090 VN_INFO (result)->has_constants = expr_has_constants (val);
3091 VN_INFO (result)->needs_insertion = true;
3092 /* As all "inserted" statements are singleton SCCs, insert
3093 to the valid table. This is strictly needed to
3094 avoid re-generating new value SSA_NAMEs for the same
3095 expression during SCC iteration over and over (the
3096 optimistic table gets cleared after each iteration).
3097 We do not need to insert into the optimistic table, as
3098 lookups there will fall back to the valid table. */
3099 if (current_info == optimistic_info)
3101 current_info = valid_info;
3102 vn_nary_op_insert (val, result);
3103 current_info = optimistic_info;
3105 else
3106 vn_nary_op_insert (val, result);
3107 if (dump_file && (dump_flags & TDF_DETAILS))
3109 fprintf (dump_file, "Inserting name ");
3110 print_generic_expr (dump_file, result, 0);
3111 fprintf (dump_file, " for expression ");
3112 print_generic_expr (dump_file, val, 0);
3113 fprintf (dump_file, "\n");
3118 if (result)
3120 changed = set_ssa_val_to (lhs, result);
3121 if (TREE_CODE (result) == SSA_NAME
3122 && VN_INFO (result)->has_constants)
3124 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
3125 VN_INFO (lhs)->has_constants = true;
3128 else
3130 changed = set_ssa_val_to (lhs, lhs);
3131 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3134 return changed;
3138 /* Visit a store to a reference operator LHS, part of STMT, value number it,
3139 and return true if the value number of the LHS has changed as a result. */
3141 static bool
3142 visit_reference_op_store (tree lhs, tree op, gimple stmt)
3144 bool changed = false;
3145 vn_reference_t vnresult = NULL;
3146 tree result, assign;
3147 bool resultsame = false;
3148 tree vuse = gimple_vuse (stmt);
3149 tree vdef = gimple_vdef (stmt);
3151 if (TREE_CODE (op) == SSA_NAME)
3152 op = SSA_VAL (op);
3154 /* First we want to lookup using the *vuses* from the store and see
3155 if there the last store to this location with the same address
3156 had the same value.
3158 The vuses represent the memory state before the store. If the
3159 memory state, address, and value of the store is the same as the
3160 last store to this location, then this store will produce the
3161 same memory state as that store.
3163 In this case the vdef versions for this store are value numbered to those
3164 vuse versions, since they represent the same memory state after
3165 this store.
3167 Otherwise, the vdefs for the store are used when inserting into
3168 the table, since the store generates a new memory state. */
3170 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
3172 if (result)
3174 if (TREE_CODE (result) == SSA_NAME)
3175 result = SSA_VAL (result);
3176 resultsame = expressions_equal_p (result, op);
3179 if ((!result || !resultsame)
3180 /* Only perform the following when being called from PRE
3181 which embeds tail merging. */
3182 && default_vn_walk_kind == VN_WALK)
3184 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3185 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
3186 if (vnresult)
3188 VN_INFO (vdef)->use_processed = true;
3189 return set_ssa_val_to (vdef, vnresult->result_vdef);
3193 if (!result || !resultsame)
3195 if (dump_file && (dump_flags & TDF_DETAILS))
3197 fprintf (dump_file, "No store match\n");
3198 fprintf (dump_file, "Value numbering store ");
3199 print_generic_expr (dump_file, lhs, 0);
3200 fprintf (dump_file, " to ");
3201 print_generic_expr (dump_file, op, 0);
3202 fprintf (dump_file, "\n");
3204 /* Have to set value numbers before insert, since insert is
3205 going to valueize the references in-place. */
3206 if (vdef)
3208 changed |= set_ssa_val_to (vdef, vdef);
3211 /* Do not insert structure copies into the tables. */
3212 if (is_gimple_min_invariant (op)
3213 || is_gimple_reg (op))
3214 vn_reference_insert (lhs, op, vdef, NULL);
3216 /* Only perform the following when being called from PRE
3217 which embeds tail merging. */
3218 if (default_vn_walk_kind == VN_WALK)
3220 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3221 vn_reference_insert (assign, lhs, vuse, vdef);
3224 else
3226 /* We had a match, so value number the vdef to have the value
3227 number of the vuse it came from. */
3229 if (dump_file && (dump_flags & TDF_DETAILS))
3230 fprintf (dump_file, "Store matched earlier value,"
3231 "value numbering store vdefs to matching vuses.\n");
3233 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3236 return changed;
3239 /* Visit and value number PHI, return true if the value number
3240 changed. */
3242 static bool
3243 visit_phi (gimple phi)
3245 bool changed = false;
3246 tree result;
3247 tree sameval = VN_TOP;
3248 bool allsame = true;
3250 /* TODO: We could check for this in init_sccvn, and replace this
3251 with a gcc_assert. */
3252 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3253 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3255 /* See if all non-TOP arguments have the same value. TOP is
3256 equivalent to everything, so we can ignore it. */
3257 edge_iterator ei;
3258 edge e;
3259 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3260 if (e->flags & EDGE_EXECUTABLE)
3262 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3264 if (TREE_CODE (def) == SSA_NAME)
3265 def = SSA_VAL (def);
3266 if (def == VN_TOP)
3267 continue;
3268 if (sameval == VN_TOP)
3270 sameval = def;
3272 else
3274 if (!expressions_equal_p (def, sameval))
3276 allsame = false;
3277 break;
3282 /* If all value numbered to the same value, the phi node has that
3283 value. */
3284 if (allsame)
3285 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3287 /* Otherwise, see if it is equivalent to a phi node in this block. */
3288 result = vn_phi_lookup (phi);
3289 if (result)
3290 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3291 else
3293 vn_phi_insert (phi, PHI_RESULT (phi));
3294 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3295 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3296 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3299 return changed;
3302 /* Return true if EXPR contains constants. */
3304 static bool
3305 expr_has_constants (tree expr)
3307 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3309 case tcc_unary:
3310 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3312 case tcc_binary:
3313 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3314 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3315 /* Constants inside reference ops are rarely interesting, but
3316 it can take a lot of looking to find them. */
3317 case tcc_reference:
3318 case tcc_declaration:
3319 return false;
3320 default:
3321 return is_gimple_min_invariant (expr);
3323 return false;
3326 /* Return true if STMT contains constants. */
3328 static bool
3329 stmt_has_constants (gimple stmt)
3331 tree tem;
3333 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3334 return false;
3336 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3338 case GIMPLE_TERNARY_RHS:
3339 tem = gimple_assign_rhs3 (stmt);
3340 if (TREE_CODE (tem) == SSA_NAME)
3341 tem = SSA_VAL (tem);
3342 if (is_gimple_min_invariant (tem))
3343 return true;
3344 /* Fallthru. */
3346 case GIMPLE_BINARY_RHS:
3347 tem = gimple_assign_rhs2 (stmt);
3348 if (TREE_CODE (tem) == SSA_NAME)
3349 tem = SSA_VAL (tem);
3350 if (is_gimple_min_invariant (tem))
3351 return true;
3352 /* Fallthru. */
3354 case GIMPLE_SINGLE_RHS:
3355 /* Constants inside reference ops are rarely interesting, but
3356 it can take a lot of looking to find them. */
3357 case GIMPLE_UNARY_RHS:
3358 tem = gimple_assign_rhs1 (stmt);
3359 if (TREE_CODE (tem) == SSA_NAME)
3360 tem = SSA_VAL (tem);
3361 return is_gimple_min_invariant (tem);
3363 default:
3364 gcc_unreachable ();
3366 return false;
3369 /* Simplify the binary expression RHS, and return the result if
3370 simplified. */
3372 static tree
3373 simplify_binary_expression (gimple stmt)
3375 tree result = NULL_TREE;
3376 tree op0 = gimple_assign_rhs1 (stmt);
3377 tree op1 = gimple_assign_rhs2 (stmt);
3378 enum tree_code code = gimple_assign_rhs_code (stmt);
3380 /* This will not catch every single case we could combine, but will
3381 catch those with constants. The goal here is to simultaneously
3382 combine constants between expressions, but avoid infinite
3383 expansion of expressions during simplification. */
3384 op0 = vn_valueize (op0);
3385 if (TREE_CODE (op0) == SSA_NAME
3386 && (VN_INFO (op0)->has_constants
3387 || TREE_CODE_CLASS (code) == tcc_comparison
3388 || code == COMPLEX_EXPR))
3389 op0 = vn_get_expr_for (op0);
3391 op1 = vn_valueize (op1);
3392 if (TREE_CODE (op1) == SSA_NAME
3393 && (VN_INFO (op1)->has_constants
3394 || code == COMPLEX_EXPR))
3395 op1 = vn_get_expr_for (op1);
3397 /* Pointer plus constant can be represented as invariant address.
3398 Do so to allow further propatation, see also tree forwprop. */
3399 if (code == POINTER_PLUS_EXPR
3400 && tree_fits_uhwi_p (op1)
3401 && TREE_CODE (op0) == ADDR_EXPR
3402 && is_gimple_min_invariant (op0))
3403 return build_invariant_address (TREE_TYPE (op0),
3404 TREE_OPERAND (op0, 0),
3405 tree_to_uhwi (op1));
3407 /* Avoid folding if nothing changed. */
3408 if (op0 == gimple_assign_rhs1 (stmt)
3409 && op1 == gimple_assign_rhs2 (stmt))
3410 return NULL_TREE;
3412 fold_defer_overflow_warnings ();
3414 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3415 if (result)
3416 STRIP_USELESS_TYPE_CONVERSION (result);
3418 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3419 stmt, 0);
3421 /* Make sure result is not a complex expression consisting
3422 of operators of operators (IE (a + b) + (a + c))
3423 Otherwise, we will end up with unbounded expressions if
3424 fold does anything at all. */
3425 if (result && valid_gimple_rhs_p (result))
3426 return result;
3428 return NULL_TREE;
3431 /* Simplify the unary expression RHS, and return the result if
3432 simplified. */
3434 static tree
3435 simplify_unary_expression (gassign *stmt)
3437 tree result = NULL_TREE;
3438 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3439 enum tree_code code = gimple_assign_rhs_code (stmt);
3441 /* We handle some tcc_reference codes here that are all
3442 GIMPLE_ASSIGN_SINGLE codes. */
3443 if (code == REALPART_EXPR
3444 || code == IMAGPART_EXPR
3445 || code == VIEW_CONVERT_EXPR
3446 || code == BIT_FIELD_REF)
3447 op0 = TREE_OPERAND (op0, 0);
3449 orig_op0 = op0;
3450 op0 = vn_valueize (op0);
3451 if (TREE_CODE (op0) == SSA_NAME)
3453 if (VN_INFO (op0)->has_constants)
3454 op0 = vn_get_expr_for (op0);
3455 else if (CONVERT_EXPR_CODE_P (code)
3456 || code == REALPART_EXPR
3457 || code == IMAGPART_EXPR
3458 || code == VIEW_CONVERT_EXPR
3459 || code == BIT_FIELD_REF)
3461 /* We want to do tree-combining on conversion-like expressions.
3462 Make sure we feed only SSA_NAMEs or constants to fold though. */
3463 tree tem = vn_get_expr_for (op0);
3464 if (UNARY_CLASS_P (tem)
3465 || BINARY_CLASS_P (tem)
3466 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3467 || TREE_CODE (tem) == SSA_NAME
3468 || TREE_CODE (tem) == CONSTRUCTOR
3469 || is_gimple_min_invariant (tem))
3470 op0 = tem;
3474 /* Avoid folding if nothing changed, but remember the expression. */
3475 if (op0 == orig_op0)
3476 return NULL_TREE;
3478 if (code == BIT_FIELD_REF)
3480 tree rhs = gimple_assign_rhs1 (stmt);
3481 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3482 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3484 else
3485 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3486 if (result)
3488 STRIP_USELESS_TYPE_CONVERSION (result);
3489 if (valid_gimple_rhs_p (result))
3490 return result;
3493 return NULL_TREE;
3496 /* Try to simplify RHS using equivalences and constant folding. */
3498 static tree
3499 try_to_simplify (gassign *stmt)
3501 enum tree_code code = gimple_assign_rhs_code (stmt);
3502 tree tem;
3504 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3505 in this case, there is no point in doing extra work. */
3506 if (code == SSA_NAME)
3507 return NULL_TREE;
3509 /* First try constant folding based on our current lattice. */
3510 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
3511 if (tem
3512 && (TREE_CODE (tem) == SSA_NAME
3513 || is_gimple_min_invariant (tem)))
3514 return tem;
3516 /* If that didn't work try combining multiple statements. */
3517 switch (TREE_CODE_CLASS (code))
3519 case tcc_reference:
3520 /* Fallthrough for some unary codes that can operate on registers. */
3521 if (!(code == REALPART_EXPR
3522 || code == IMAGPART_EXPR
3523 || code == VIEW_CONVERT_EXPR
3524 || code == BIT_FIELD_REF))
3525 break;
3526 /* We could do a little more with unary ops, if they expand
3527 into binary ops, but it's debatable whether it is worth it. */
3528 case tcc_unary:
3529 return simplify_unary_expression (stmt);
3531 case tcc_comparison:
3532 case tcc_binary:
3533 return simplify_binary_expression (stmt);
3535 default:
3536 break;
3539 return NULL_TREE;
3542 /* Visit and value number USE, return true if the value number
3543 changed. */
3545 static bool
3546 visit_use (tree use)
3548 bool changed = false;
3549 gimple stmt = SSA_NAME_DEF_STMT (use);
3551 mark_use_processed (use);
3553 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3554 if (dump_file && (dump_flags & TDF_DETAILS)
3555 && !SSA_NAME_IS_DEFAULT_DEF (use))
3557 fprintf (dump_file, "Value numbering ");
3558 print_generic_expr (dump_file, use, 0);
3559 fprintf (dump_file, " stmt = ");
3560 print_gimple_stmt (dump_file, stmt, 0, 0);
3563 /* Handle uninitialized uses. */
3564 if (SSA_NAME_IS_DEFAULT_DEF (use))
3565 changed = set_ssa_val_to (use, use);
3566 else
3568 if (gimple_code (stmt) == GIMPLE_PHI)
3569 changed = visit_phi (stmt);
3570 else if (gimple_has_volatile_ops (stmt))
3571 changed = defs_to_varying (stmt);
3572 else if (is_gimple_assign (stmt))
3574 enum tree_code code = gimple_assign_rhs_code (stmt);
3575 tree lhs = gimple_assign_lhs (stmt);
3576 tree rhs1 = gimple_assign_rhs1 (stmt);
3577 tree simplified;
3579 /* Shortcut for copies. Simplifying copies is pointless,
3580 since we copy the expression and value they represent. */
3581 if (code == SSA_NAME
3582 && TREE_CODE (lhs) == SSA_NAME)
3584 changed = visit_copy (lhs, rhs1);
3585 goto done;
3587 simplified = try_to_simplify (as_a <gassign *> (stmt));
3588 if (simplified)
3590 if (dump_file && (dump_flags & TDF_DETAILS))
3592 fprintf (dump_file, "RHS ");
3593 print_gimple_expr (dump_file, stmt, 0, 0);
3594 fprintf (dump_file, " simplified to ");
3595 print_generic_expr (dump_file, simplified, 0);
3596 if (TREE_CODE (lhs) == SSA_NAME)
3597 fprintf (dump_file, " has constants %d\n",
3598 expr_has_constants (simplified));
3599 else
3600 fprintf (dump_file, "\n");
3603 /* Setting value numbers to constants will occasionally
3604 screw up phi congruence because constants are not
3605 uniquely associated with a single ssa name that can be
3606 looked up. */
3607 if (simplified
3608 && is_gimple_min_invariant (simplified)
3609 && TREE_CODE (lhs) == SSA_NAME)
3611 VN_INFO (lhs)->expr = simplified;
3612 VN_INFO (lhs)->has_constants = true;
3613 changed = set_ssa_val_to (lhs, simplified);
3614 goto done;
3616 else if (simplified
3617 && TREE_CODE (simplified) == SSA_NAME
3618 && TREE_CODE (lhs) == SSA_NAME)
3620 changed = visit_copy (lhs, simplified);
3621 goto done;
3623 else if (simplified)
3625 if (TREE_CODE (lhs) == SSA_NAME)
3627 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3628 /* We have to unshare the expression or else
3629 valuizing may change the IL stream. */
3630 VN_INFO (lhs)->expr = unshare_expr (simplified);
3633 else if (stmt_has_constants (stmt)
3634 && TREE_CODE (lhs) == SSA_NAME)
3635 VN_INFO (lhs)->has_constants = true;
3636 else if (TREE_CODE (lhs) == SSA_NAME)
3638 /* We reset expr and constantness here because we may
3639 have been value numbering optimistically, and
3640 iterating. They may become non-constant in this case,
3641 even if they were optimistically constant. */
3643 VN_INFO (lhs)->has_constants = false;
3644 VN_INFO (lhs)->expr = NULL_TREE;
3647 if ((TREE_CODE (lhs) == SSA_NAME
3648 /* We can substitute SSA_NAMEs that are live over
3649 abnormal edges with their constant value. */
3650 && !(gimple_assign_copy_p (stmt)
3651 && is_gimple_min_invariant (rhs1))
3652 && !(simplified
3653 && is_gimple_min_invariant (simplified))
3654 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3655 /* Stores or copies from SSA_NAMEs that are live over
3656 abnormal edges are a problem. */
3657 || (code == SSA_NAME
3658 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3659 changed = defs_to_varying (stmt);
3660 else if (REFERENCE_CLASS_P (lhs)
3661 || DECL_P (lhs))
3662 changed = visit_reference_op_store (lhs, rhs1, stmt);
3663 else if (TREE_CODE (lhs) == SSA_NAME)
3665 if ((gimple_assign_copy_p (stmt)
3666 && is_gimple_min_invariant (rhs1))
3667 || (simplified
3668 && is_gimple_min_invariant (simplified)))
3670 VN_INFO (lhs)->has_constants = true;
3671 if (simplified)
3672 changed = set_ssa_val_to (lhs, simplified);
3673 else
3674 changed = set_ssa_val_to (lhs, rhs1);
3676 else
3678 /* First try to lookup the simplified expression. */
3679 if (simplified)
3681 enum gimple_rhs_class rhs_class;
3684 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3685 if ((rhs_class == GIMPLE_UNARY_RHS
3686 || rhs_class == GIMPLE_BINARY_RHS
3687 || rhs_class == GIMPLE_TERNARY_RHS)
3688 && valid_gimple_rhs_p (simplified))
3690 tree result = vn_nary_op_lookup (simplified, NULL);
3691 if (result)
3693 changed = set_ssa_val_to (lhs, result);
3694 goto done;
3699 /* Otherwise visit the original statement. */
3700 switch (vn_get_stmt_kind (stmt))
3702 case VN_NARY:
3703 changed = visit_nary_op (lhs, stmt);
3704 break;
3705 case VN_REFERENCE:
3706 changed = visit_reference_op_load (lhs, rhs1, stmt);
3707 break;
3708 default:
3709 changed = defs_to_varying (stmt);
3710 break;
3714 else
3715 changed = defs_to_varying (stmt);
3717 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
3719 tree lhs = gimple_call_lhs (stmt);
3720 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3722 /* Try constant folding based on our current lattice. */
3723 tree simplified = gimple_fold_stmt_to_constant_1 (stmt,
3724 vn_valueize);
3725 if (simplified)
3727 if (dump_file && (dump_flags & TDF_DETAILS))
3729 fprintf (dump_file, "call ");
3730 print_gimple_expr (dump_file, stmt, 0, 0);
3731 fprintf (dump_file, " simplified to ");
3732 print_generic_expr (dump_file, simplified, 0);
3733 if (TREE_CODE (lhs) == SSA_NAME)
3734 fprintf (dump_file, " has constants %d\n",
3735 expr_has_constants (simplified));
3736 else
3737 fprintf (dump_file, "\n");
3740 /* Setting value numbers to constants will occasionally
3741 screw up phi congruence because constants are not
3742 uniquely associated with a single ssa name that can be
3743 looked up. */
3744 if (simplified
3745 && is_gimple_min_invariant (simplified))
3747 VN_INFO (lhs)->expr = simplified;
3748 VN_INFO (lhs)->has_constants = true;
3749 changed = set_ssa_val_to (lhs, simplified);
3750 if (gimple_vdef (stmt))
3751 changed |= set_ssa_val_to (gimple_vdef (stmt),
3752 SSA_VAL (gimple_vuse (stmt)));
3753 goto done;
3755 else if (simplified
3756 && TREE_CODE (simplified) == SSA_NAME)
3758 changed = visit_copy (lhs, simplified);
3759 if (gimple_vdef (stmt))
3760 changed |= set_ssa_val_to (gimple_vdef (stmt),
3761 SSA_VAL (gimple_vuse (stmt)));
3762 goto done;
3764 else
3766 if (stmt_has_constants (stmt))
3767 VN_INFO (lhs)->has_constants = true;
3768 else
3770 /* We reset expr and constantness here because we may
3771 have been value numbering optimistically, and
3772 iterating. They may become non-constant in this case,
3773 even if they were optimistically constant. */
3774 VN_INFO (lhs)->has_constants = false;
3775 VN_INFO (lhs)->expr = NULL_TREE;
3778 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3780 changed = defs_to_varying (stmt);
3781 goto done;
3786 if (!gimple_call_internal_p (stmt)
3787 && (/* Calls to the same function with the same vuse
3788 and the same operands do not necessarily return the same
3789 value, unless they're pure or const. */
3790 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3791 /* If calls have a vdef, subsequent calls won't have
3792 the same incoming vuse. So, if 2 calls with vdef have the
3793 same vuse, we know they're not subsequent.
3794 We can value number 2 calls to the same function with the
3795 same vuse and the same operands which are not subsequent
3796 the same, because there is no code in the program that can
3797 compare the 2 values... */
3798 || (gimple_vdef (stmt)
3799 /* ... unless the call returns a pointer which does
3800 not alias with anything else. In which case the
3801 information that the values are distinct are encoded
3802 in the IL. */
3803 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
3804 /* Only perform the following when being called from PRE
3805 which embeds tail merging. */
3806 && default_vn_walk_kind == VN_WALK)))
3807 changed = visit_reference_op_call (lhs, call_stmt);
3808 else
3809 changed = defs_to_varying (stmt);
3811 else
3812 changed = defs_to_varying (stmt);
3814 done:
3815 return changed;
3818 /* Compare two operands by reverse postorder index */
3820 static int
3821 compare_ops (const void *pa, const void *pb)
3823 const tree opa = *((const tree *)pa);
3824 const tree opb = *((const tree *)pb);
3825 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3826 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3827 basic_block bba;
3828 basic_block bbb;
3830 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3831 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3832 else if (gimple_nop_p (opstmta))
3833 return -1;
3834 else if (gimple_nop_p (opstmtb))
3835 return 1;
3837 bba = gimple_bb (opstmta);
3838 bbb = gimple_bb (opstmtb);
3840 if (!bba && !bbb)
3841 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3842 else if (!bba)
3843 return -1;
3844 else if (!bbb)
3845 return 1;
3847 if (bba == bbb)
3849 if (gimple_code (opstmta) == GIMPLE_PHI
3850 && gimple_code (opstmtb) == GIMPLE_PHI)
3851 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3852 else if (gimple_code (opstmta) == GIMPLE_PHI)
3853 return -1;
3854 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3855 return 1;
3856 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3857 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3858 else
3859 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3861 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3864 /* Sort an array containing members of a strongly connected component
3865 SCC so that the members are ordered by RPO number.
3866 This means that when the sort is complete, iterating through the
3867 array will give you the members in RPO order. */
3869 static void
3870 sort_scc (vec<tree> scc)
3872 scc.qsort (compare_ops);
3875 /* Insert the no longer used nary ONARY to the hash INFO. */
3877 static void
3878 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3880 size_t size = sizeof_vn_nary_op (onary->length);
3881 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3882 &info->nary_obstack);
3883 memcpy (nary, onary, size);
3884 vn_nary_op_insert_into (nary, info->nary, false);
3887 /* Insert the no longer used phi OPHI to the hash INFO. */
3889 static void
3890 copy_phi (vn_phi_t ophi, vn_tables_t info)
3892 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3893 vn_phi_s **slot;
3894 memcpy (phi, ophi, sizeof (*phi));
3895 ophi->phiargs.create (0);
3896 slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT);
3897 gcc_assert (!*slot);
3898 *slot = phi;
3901 /* Insert the no longer used reference OREF to the hash INFO. */
3903 static void
3904 copy_reference (vn_reference_t oref, vn_tables_t info)
3906 vn_reference_t ref;
3907 vn_reference_s **slot;
3908 ref = (vn_reference_t) pool_alloc (info->references_pool);
3909 memcpy (ref, oref, sizeof (*ref));
3910 oref->operands.create (0);
3911 slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT);
3912 if (*slot)
3913 free_reference (*slot);
3914 *slot = ref;
3917 /* Process a strongly connected component in the SSA graph. */
3919 static void
3920 process_scc (vec<tree> scc)
3922 tree var;
3923 unsigned int i;
3924 unsigned int iterations = 0;
3925 bool changed = true;
3926 vn_nary_op_iterator_type hin;
3927 vn_phi_iterator_type hip;
3928 vn_reference_iterator_type hir;
3929 vn_nary_op_t nary;
3930 vn_phi_t phi;
3931 vn_reference_t ref;
3933 /* If the SCC has a single member, just visit it. */
3934 if (scc.length () == 1)
3936 tree use = scc[0];
3937 if (VN_INFO (use)->use_processed)
3938 return;
3939 /* We need to make sure it doesn't form a cycle itself, which can
3940 happen for self-referential PHI nodes. In that case we would
3941 end up inserting an expression with VN_TOP operands into the
3942 valid table which makes us derive bogus equivalences later.
3943 The cheapest way to check this is to assume it for all PHI nodes. */
3944 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3945 /* Fallthru to iteration. */ ;
3946 else
3948 visit_use (use);
3949 return;
3953 if (dump_file && (dump_flags & TDF_DETAILS))
3954 print_scc (dump_file, scc);
3956 /* Iterate over the SCC with the optimistic table until it stops
3957 changing. */
3958 current_info = optimistic_info;
3959 while (changed)
3961 changed = false;
3962 iterations++;
3963 if (dump_file && (dump_flags & TDF_DETAILS))
3964 fprintf (dump_file, "Starting iteration %d\n", iterations);
3965 /* As we are value-numbering optimistically we have to
3966 clear the expression tables and the simplified expressions
3967 in each iteration until we converge. */
3968 optimistic_info->nary->empty ();
3969 optimistic_info->phis->empty ();
3970 optimistic_info->references->empty ();
3971 obstack_free (&optimistic_info->nary_obstack, NULL);
3972 gcc_obstack_init (&optimistic_info->nary_obstack);
3973 empty_alloc_pool (optimistic_info->phis_pool);
3974 empty_alloc_pool (optimistic_info->references_pool);
3975 FOR_EACH_VEC_ELT (scc, i, var)
3976 VN_INFO (var)->expr = NULL_TREE;
3977 FOR_EACH_VEC_ELT (scc, i, var)
3978 changed |= visit_use (var);
3981 if (dump_file && (dump_flags & TDF_DETAILS))
3982 fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations);
3983 statistics_histogram_event (cfun, "SCC iterations", iterations);
3985 /* Finally, copy the contents of the no longer used optimistic
3986 table to the valid table. */
3987 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin)
3988 copy_nary (nary, valid_info);
3989 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip)
3990 copy_phi (phi, valid_info);
3991 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references,
3992 ref, vn_reference_t, hir)
3993 copy_reference (ref, valid_info);
3995 current_info = valid_info;
3999 /* Pop the components of the found SCC for NAME off the SCC stack
4000 and process them. Returns true if all went well, false if
4001 we run into resource limits. */
4003 static bool
4004 extract_and_process_scc_for_name (tree name)
4006 auto_vec<tree> scc;
4007 tree x;
4009 /* Found an SCC, pop the components off the SCC stack and
4010 process them. */
4013 x = sccstack.pop ();
4015 VN_INFO (x)->on_sccstack = false;
4016 scc.safe_push (x);
4017 } while (x != name);
4019 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
4020 if (scc.length ()
4021 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
4023 if (dump_file)
4024 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
4025 "SCC size %u exceeding %u\n", scc.length (),
4026 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
4028 return false;
4031 if (scc.length () > 1)
4032 sort_scc (scc);
4034 process_scc (scc);
4036 return true;
4039 /* Depth first search on NAME to discover and process SCC's in the SSA
4040 graph.
4041 Execution of this algorithm relies on the fact that the SCC's are
4042 popped off the stack in topological order.
4043 Returns true if successful, false if we stopped processing SCC's due
4044 to resource constraints. */
4046 static bool
4047 DFS (tree name)
4049 vec<ssa_op_iter> itervec = vNULL;
4050 vec<tree> namevec = vNULL;
4051 use_operand_p usep = NULL;
4052 gimple defstmt;
4053 tree use;
4054 ssa_op_iter iter;
4056 start_over:
4057 /* SCC info */
4058 VN_INFO (name)->dfsnum = next_dfs_num++;
4059 VN_INFO (name)->visited = true;
4060 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
4062 sccstack.safe_push (name);
4063 VN_INFO (name)->on_sccstack = true;
4064 defstmt = SSA_NAME_DEF_STMT (name);
4066 /* Recursively DFS on our operands, looking for SCC's. */
4067 if (!gimple_nop_p (defstmt))
4069 /* Push a new iterator. */
4070 if (gphi *phi = dyn_cast <gphi *> (defstmt))
4071 usep = op_iter_init_phiuse (&iter, phi, SSA_OP_ALL_USES);
4072 else
4073 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
4075 else
4076 clear_and_done_ssa_iter (&iter);
4078 while (1)
4080 /* If we are done processing uses of a name, go up the stack
4081 of iterators and process SCCs as we found them. */
4082 if (op_iter_done (&iter))
4084 /* See if we found an SCC. */
4085 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
4086 if (!extract_and_process_scc_for_name (name))
4088 namevec.release ();
4089 itervec.release ();
4090 return false;
4093 /* Check if we are done. */
4094 if (namevec.is_empty ())
4096 namevec.release ();
4097 itervec.release ();
4098 return true;
4101 /* Restore the last use walker and continue walking there. */
4102 use = name;
4103 name = namevec.pop ();
4104 memcpy (&iter, &itervec.last (),
4105 sizeof (ssa_op_iter));
4106 itervec.pop ();
4107 goto continue_walking;
4110 use = USE_FROM_PTR (usep);
4112 /* Since we handle phi nodes, we will sometimes get
4113 invariants in the use expression. */
4114 if (TREE_CODE (use) == SSA_NAME)
4116 if (! (VN_INFO (use)->visited))
4118 /* Recurse by pushing the current use walking state on
4119 the stack and starting over. */
4120 itervec.safe_push (iter);
4121 namevec.safe_push (name);
4122 name = use;
4123 goto start_over;
4125 continue_walking:
4126 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
4127 VN_INFO (use)->low);
4129 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
4130 && VN_INFO (use)->on_sccstack)
4132 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
4133 VN_INFO (name)->low);
4137 usep = op_iter_next_use (&iter);
4141 /* Allocate a value number table. */
4143 static void
4144 allocate_vn_table (vn_tables_t table)
4146 table->phis = new vn_phi_table_type (23);
4147 table->nary = new vn_nary_op_table_type (23);
4148 table->references = new vn_reference_table_type (23);
4150 gcc_obstack_init (&table->nary_obstack);
4151 table->phis_pool = create_alloc_pool ("VN phis",
4152 sizeof (struct vn_phi_s),
4153 30);
4154 table->references_pool = create_alloc_pool ("VN references",
4155 sizeof (struct vn_reference_s),
4156 30);
4159 /* Free a value number table. */
4161 static void
4162 free_vn_table (vn_tables_t table)
4164 delete table->phis;
4165 table->phis = NULL;
4166 delete table->nary;
4167 table->nary = NULL;
4168 delete table->references;
4169 table->references = NULL;
4170 obstack_free (&table->nary_obstack, NULL);
4171 free_alloc_pool (table->phis_pool);
4172 free_alloc_pool (table->references_pool);
4175 static void
4176 init_scc_vn (void)
4178 size_t i;
4179 int j;
4180 int *rpo_numbers_temp;
4182 calculate_dominance_info (CDI_DOMINATORS);
4183 sccstack.create (0);
4184 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
4186 constant_value_ids = BITMAP_ALLOC (NULL);
4188 next_dfs_num = 1;
4189 next_value_id = 1;
4191 vn_ssa_aux_table.create (num_ssa_names + 1);
4192 /* VEC_alloc doesn't actually grow it to the right size, it just
4193 preallocates the space to do so. */
4194 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
4195 gcc_obstack_init (&vn_ssa_aux_obstack);
4197 shared_lookup_phiargs.create (0);
4198 shared_lookup_references.create (0);
4199 rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun));
4200 rpo_numbers_temp =
4201 XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4202 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
4204 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
4205 the i'th block in RPO order is bb. We want to map bb's to RPO
4206 numbers, so we need to rearrange this array. */
4207 for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++)
4208 rpo_numbers[rpo_numbers_temp[j]] = j;
4210 XDELETE (rpo_numbers_temp);
4212 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
4214 /* Create the VN_INFO structures, and initialize value numbers to
4215 TOP. */
4216 for (i = 0; i < num_ssa_names; i++)
4218 tree name = ssa_name (i);
4219 if (name)
4221 VN_INFO_GET (name)->valnum = VN_TOP;
4222 VN_INFO (name)->expr = NULL_TREE;
4223 VN_INFO (name)->value_id = 0;
4227 renumber_gimple_stmt_uids ();
4229 /* Create the valid and optimistic value numbering tables. */
4230 valid_info = XCNEW (struct vn_tables_s);
4231 allocate_vn_table (valid_info);
4232 optimistic_info = XCNEW (struct vn_tables_s);
4233 allocate_vn_table (optimistic_info);
4236 void
4237 free_scc_vn (void)
4239 size_t i;
4241 delete constant_to_value_id;
4242 constant_to_value_id = NULL;
4243 BITMAP_FREE (constant_value_ids);
4244 shared_lookup_phiargs.release ();
4245 shared_lookup_references.release ();
4246 XDELETEVEC (rpo_numbers);
4248 for (i = 0; i < num_ssa_names; i++)
4250 tree name = ssa_name (i);
4251 if (name
4252 && VN_INFO (name)->needs_insertion)
4253 release_ssa_name (name);
4255 obstack_free (&vn_ssa_aux_obstack, NULL);
4256 vn_ssa_aux_table.release ();
4258 sccstack.release ();
4259 free_vn_table (valid_info);
4260 XDELETE (valid_info);
4261 free_vn_table (optimistic_info);
4262 XDELETE (optimistic_info);
4265 /* Set *ID according to RESULT. */
4267 static void
4268 set_value_id_for_result (tree result, unsigned int *id)
4270 if (result && TREE_CODE (result) == SSA_NAME)
4271 *id = VN_INFO (result)->value_id;
4272 else if (result && is_gimple_min_invariant (result))
4273 *id = get_or_alloc_constant_value_id (result);
4274 else
4275 *id = get_next_value_id ();
4278 /* Set the value ids in the valid hash tables. */
4280 static void
4281 set_hashtable_value_ids (void)
4283 vn_nary_op_iterator_type hin;
4284 vn_phi_iterator_type hip;
4285 vn_reference_iterator_type hir;
4286 vn_nary_op_t vno;
4287 vn_reference_t vr;
4288 vn_phi_t vp;
4290 /* Now set the value ids of the things we had put in the hash
4291 table. */
4293 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4294 set_value_id_for_result (vno->result, &vno->value_id);
4296 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4297 set_value_id_for_result (vp->result, &vp->value_id);
4299 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4300 hir)
4301 set_value_id_for_result (vr->result, &vr->value_id);
4304 class cond_dom_walker : public dom_walker
4306 public:
4307 cond_dom_walker () : dom_walker (CDI_DOMINATORS), fail (false) {}
4309 virtual void before_dom_children (basic_block);
4311 bool fail;
4314 void
4315 cond_dom_walker::before_dom_children (basic_block bb)
4317 edge e;
4318 edge_iterator ei;
4320 if (fail)
4321 return;
4323 /* If any of the predecessor edges that do not come from blocks dominated
4324 by us are still marked as possibly executable consider this block
4325 reachable. */
4326 bool reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (cfun);
4327 FOR_EACH_EDGE (e, ei, bb->preds)
4328 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
4329 reachable |= (e->flags & EDGE_EXECUTABLE);
4331 /* If the block is not reachable all outgoing edges are not
4332 executable. */
4333 if (!reachable)
4335 if (dump_file && (dump_flags & TDF_DETAILS))
4336 fprintf (dump_file, "Marking all outgoing edges of unreachable "
4337 "BB %d as not executable\n", bb->index);
4339 FOR_EACH_EDGE (e, ei, bb->succs)
4340 e->flags &= ~EDGE_EXECUTABLE;
4341 return;
4344 gimple stmt = last_stmt (bb);
4345 if (!stmt)
4346 return;
4348 enum gimple_code code = gimple_code (stmt);
4349 if (code != GIMPLE_COND
4350 && code != GIMPLE_SWITCH
4351 && code != GIMPLE_GOTO)
4352 return;
4354 if (dump_file && (dump_flags & TDF_DETAILS))
4356 fprintf (dump_file, "Value-numbering operands of stmt ending BB %d: ",
4357 bb->index);
4358 print_gimple_stmt (dump_file, stmt, 0, 0);
4361 /* Value-number the last stmts SSA uses. */
4362 ssa_op_iter i;
4363 tree op;
4364 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4365 if (VN_INFO (op)->visited == false
4366 && !DFS (op))
4368 fail = true;
4369 return;
4372 /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges
4373 if value-numbering can prove they are not reachable. Handling
4374 computed gotos is also possible. */
4375 tree val;
4376 switch (code)
4378 case GIMPLE_COND:
4380 tree lhs = gimple_cond_lhs (stmt);
4381 tree rhs = gimple_cond_rhs (stmt);
4382 /* Work hard in computing the condition and take into account
4383 the valueization of the defining stmt. */
4384 if (TREE_CODE (lhs) == SSA_NAME)
4385 lhs = vn_get_expr_for (lhs);
4386 if (TREE_CODE (rhs) == SSA_NAME)
4387 rhs = vn_get_expr_for (rhs);
4388 val = fold_binary (gimple_cond_code (stmt),
4389 boolean_type_node, lhs, rhs);
4390 break;
4392 case GIMPLE_SWITCH:
4393 val = gimple_switch_index (as_a <gswitch *> (stmt));
4394 break;
4395 case GIMPLE_GOTO:
4396 val = gimple_goto_dest (stmt);
4397 break;
4398 default:
4399 gcc_unreachable ();
4401 if (!val)
4402 return;
4404 edge taken = find_taken_edge (bb, vn_valueize (val));
4405 if (!taken)
4406 return;
4408 if (dump_file && (dump_flags & TDF_DETAILS))
4409 fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as "
4410 "not executable\n", bb->index, bb->index, taken->dest->index);
4412 FOR_EACH_EDGE (e, ei, bb->succs)
4413 if (e != taken)
4414 e->flags &= ~EDGE_EXECUTABLE;
4417 /* Do SCCVN. Returns true if it finished, false if we bailed out
4418 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4419 how we use the alias oracle walking during the VN process. */
4421 bool
4422 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4424 basic_block bb;
4425 size_t i;
4426 tree param;
4428 default_vn_walk_kind = default_vn_walk_kind_;
4430 init_scc_vn ();
4431 current_info = valid_info;
4433 for (param = DECL_ARGUMENTS (current_function_decl);
4434 param;
4435 param = DECL_CHAIN (param))
4437 tree def = ssa_default_def (cfun, param);
4438 if (def)
4440 VN_INFO (def)->visited = true;
4441 VN_INFO (def)->valnum = def;
4445 /* Mark all edges as possibly executable. */
4446 FOR_ALL_BB_FN (bb, cfun)
4448 edge_iterator ei;
4449 edge e;
4450 FOR_EACH_EDGE (e, ei, bb->succs)
4451 e->flags |= EDGE_EXECUTABLE;
4454 /* Walk all blocks in dominator order, value-numbering the last stmts
4455 SSA uses and decide whether outgoing edges are not executable. */
4456 cond_dom_walker walker;
4457 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4458 if (walker.fail)
4460 free_scc_vn ();
4461 return false;
4464 /* Value-number remaining SSA names. */
4465 for (i = 1; i < num_ssa_names; ++i)
4467 tree name = ssa_name (i);
4468 if (name
4469 && VN_INFO (name)->visited == false
4470 && !has_zero_uses (name))
4471 if (!DFS (name))
4473 free_scc_vn ();
4474 return false;
4478 /* Initialize the value ids. */
4480 for (i = 1; i < num_ssa_names; ++i)
4482 tree name = ssa_name (i);
4483 vn_ssa_aux_t info;
4484 if (!name)
4485 continue;
4486 info = VN_INFO (name);
4487 if (info->valnum == name
4488 || info->valnum == VN_TOP)
4489 info->value_id = get_next_value_id ();
4490 else if (is_gimple_min_invariant (info->valnum))
4491 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4494 /* Propagate. */
4495 for (i = 1; i < num_ssa_names; ++i)
4497 tree name = ssa_name (i);
4498 vn_ssa_aux_t info;
4499 if (!name)
4500 continue;
4501 info = VN_INFO (name);
4502 if (TREE_CODE (info->valnum) == SSA_NAME
4503 && info->valnum != name
4504 && info->value_id != VN_INFO (info->valnum)->value_id)
4505 info->value_id = VN_INFO (info->valnum)->value_id;
4508 set_hashtable_value_ids ();
4510 if (dump_file && (dump_flags & TDF_DETAILS))
4512 fprintf (dump_file, "Value numbers:\n");
4513 for (i = 0; i < num_ssa_names; i++)
4515 tree name = ssa_name (i);
4516 if (name
4517 && VN_INFO (name)->visited
4518 && SSA_VAL (name) != name)
4520 print_generic_expr (dump_file, name, 0);
4521 fprintf (dump_file, " = ");
4522 print_generic_expr (dump_file, SSA_VAL (name), 0);
4523 fprintf (dump_file, "\n");
4528 return true;
4531 /* Return the maximum value id we have ever seen. */
4533 unsigned int
4534 get_max_value_id (void)
4536 return next_value_id;
4539 /* Return the next unique value id. */
4541 unsigned int
4542 get_next_value_id (void)
4544 return next_value_id++;
4548 /* Compare two expressions E1 and E2 and return true if they are equal. */
4550 bool
4551 expressions_equal_p (tree e1, tree e2)
4553 /* The obvious case. */
4554 if (e1 == e2)
4555 return true;
4557 /* If only one of them is null, they cannot be equal. */
4558 if (!e1 || !e2)
4559 return false;
4561 /* Now perform the actual comparison. */
4562 if (TREE_CODE (e1) == TREE_CODE (e2)
4563 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4564 return true;
4566 return false;
4570 /* Return true if the nary operation NARY may trap. This is a copy
4571 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4573 bool
4574 vn_nary_may_trap (vn_nary_op_t nary)
4576 tree type;
4577 tree rhs2 = NULL_TREE;
4578 bool honor_nans = false;
4579 bool honor_snans = false;
4580 bool fp_operation = false;
4581 bool honor_trapv = false;
4582 bool handled, ret;
4583 unsigned i;
4585 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4586 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4587 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4589 type = nary->type;
4590 fp_operation = FLOAT_TYPE_P (type);
4591 if (fp_operation)
4593 honor_nans = flag_trapping_math && !flag_finite_math_only;
4594 honor_snans = flag_signaling_nans != 0;
4596 else if (INTEGRAL_TYPE_P (type)
4597 && TYPE_OVERFLOW_TRAPS (type))
4598 honor_trapv = true;
4600 if (nary->length >= 2)
4601 rhs2 = nary->op[1];
4602 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4603 honor_trapv,
4604 honor_nans, honor_snans, rhs2,
4605 &handled);
4606 if (handled
4607 && ret)
4608 return true;
4610 for (i = 0; i < nary->length; ++i)
4611 if (tree_could_trap_p (nary->op[i]))
4612 return true;
4614 return false;