Merge from trunk @222673.
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blobf0d1b4edb5e19a7bda7a38ab9c8d7aefa65d3cee
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 /* Do not look through a storage order barrier. */
743 else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse)
744 return false;
745 if (vro1->off == -1)
746 break;
747 off1 += vro1->off;
749 for (; vr2->operands.iterate (j, &vro2); j++)
751 if (vro2->opcode == MEM_REF)
752 deref2 = true;
753 /* Do not look through a storage order barrier. */
754 else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse)
755 return false;
756 if (vro2->off == -1)
757 break;
758 off2 += vro2->off;
760 if (off1 != off2)
761 return false;
762 if (deref1 && vro1->opcode == ADDR_EXPR)
764 memset (&tem1, 0, sizeof (tem1));
765 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
766 tem1.type = TREE_TYPE (tem1.op0);
767 tem1.opcode = TREE_CODE (tem1.op0);
768 vro1 = &tem1;
769 deref1 = false;
771 if (deref2 && vro2->opcode == ADDR_EXPR)
773 memset (&tem2, 0, sizeof (tem2));
774 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
775 tem2.type = TREE_TYPE (tem2.op0);
776 tem2.opcode = TREE_CODE (tem2.op0);
777 vro2 = &tem2;
778 deref2 = false;
780 if (deref1 != deref2)
781 return false;
782 if (!vn_reference_op_eq (vro1, vro2))
783 return false;
784 ++j;
785 ++i;
787 while (vr1->operands.length () != i
788 || vr2->operands.length () != j);
790 return true;
793 /* Copy the operations present in load/store REF into RESULT, a vector of
794 vn_reference_op_s's. */
796 static void
797 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
799 if (TREE_CODE (ref) == TARGET_MEM_REF)
801 vn_reference_op_s temp;
803 result->reserve (3);
805 memset (&temp, 0, sizeof (temp));
806 temp.type = TREE_TYPE (ref);
807 temp.opcode = TREE_CODE (ref);
808 temp.op0 = TMR_INDEX (ref);
809 temp.op1 = TMR_STEP (ref);
810 temp.op2 = TMR_OFFSET (ref);
811 temp.off = -1;
812 result->quick_push (temp);
814 memset (&temp, 0, sizeof (temp));
815 temp.type = NULL_TREE;
816 temp.opcode = ERROR_MARK;
817 temp.op0 = TMR_INDEX2 (ref);
818 temp.off = -1;
819 result->quick_push (temp);
821 memset (&temp, 0, sizeof (temp));
822 temp.type = NULL_TREE;
823 temp.opcode = TREE_CODE (TMR_BASE (ref));
824 temp.op0 = TMR_BASE (ref);
825 temp.off = -1;
826 result->quick_push (temp);
827 return;
830 /* For non-calls, store the information that makes up the address. */
831 tree orig = ref;
832 while (ref)
834 vn_reference_op_s temp;
836 memset (&temp, 0, sizeof (temp));
837 temp.type = TREE_TYPE (ref);
838 temp.opcode = TREE_CODE (ref);
839 temp.off = -1;
841 switch (temp.opcode)
843 case MODIFY_EXPR:
844 temp.op0 = TREE_OPERAND (ref, 1);
845 break;
846 case WITH_SIZE_EXPR:
847 temp.op0 = TREE_OPERAND (ref, 1);
848 temp.off = 0;
849 break;
850 case MEM_REF:
851 /* The base address gets its own vn_reference_op_s structure. */
852 temp.op0 = TREE_OPERAND (ref, 1);
853 if (tree_fits_shwi_p (TREE_OPERAND (ref, 1)))
854 temp.off = tree_to_shwi (TREE_OPERAND (ref, 1));
855 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
856 break;
857 case BIT_FIELD_REF:
858 /* Record bits, position and storage order. */
859 temp.op0 = TREE_OPERAND (ref, 1);
860 temp.op1 = TREE_OPERAND (ref, 2);
861 temp.reverse = REF_REVERSE_STORAGE_ORDER (ref);
862 break;
863 case COMPONENT_REF:
864 /* The field decl is enough to unambiguously specify the field,
865 a matching type is not necessary and a mismatching type
866 is always a spurious difference. */
867 temp.type = NULL_TREE;
868 temp.op0 = TREE_OPERAND (ref, 1);
869 temp.op1 = TREE_OPERAND (ref, 2);
871 tree this_offset = component_ref_field_offset (ref);
872 if (this_offset
873 && TREE_CODE (this_offset) == INTEGER_CST)
875 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
876 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
878 offset_int off
879 = (wi::to_offset (this_offset)
880 + wi::lrshift (wi::to_offset (bit_offset),
881 LOG2_BITS_PER_UNIT));
882 if (wi::fits_shwi_p (off)
883 /* Probibit value-numbering zero offset components
884 of addresses the same before the pass folding
885 __builtin_object_size had a chance to run
886 (checking cfun->after_inlining does the
887 trick here). */
888 && (TREE_CODE (orig) != ADDR_EXPR
889 || off != 0
890 || cfun->after_inlining))
891 temp.off = off.to_shwi ();
895 break;
896 case ARRAY_RANGE_REF:
897 case ARRAY_REF:
898 /* Record index as operand. */
899 temp.op0 = TREE_OPERAND (ref, 1);
900 /* Always record lower bounds and element size. */
901 temp.op1 = array_ref_low_bound (ref);
902 temp.op2 = array_ref_element_size (ref);
903 if (TREE_CODE (temp.op0) == INTEGER_CST
904 && TREE_CODE (temp.op1) == INTEGER_CST
905 && TREE_CODE (temp.op2) == INTEGER_CST)
907 offset_int off = ((wi::to_offset (temp.op0)
908 - wi::to_offset (temp.op1))
909 * wi::to_offset (temp.op2));
910 if (wi::fits_shwi_p (off))
911 temp.off = off.to_shwi();
913 break;
914 case VAR_DECL:
915 if (DECL_HARD_REGISTER (ref))
917 temp.op0 = ref;
918 break;
920 /* Fallthru. */
921 case PARM_DECL:
922 case CONST_DECL:
923 case RESULT_DECL:
924 /* Canonicalize decls to MEM[&decl] which is what we end up with
925 when valueizing MEM[ptr] with ptr = &decl. */
926 temp.opcode = MEM_REF;
927 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
928 temp.off = 0;
929 result->safe_push (temp);
930 temp.opcode = ADDR_EXPR;
931 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
932 temp.type = TREE_TYPE (temp.op0);
933 temp.off = -1;
934 break;
935 case STRING_CST:
936 case INTEGER_CST:
937 case COMPLEX_CST:
938 case VECTOR_CST:
939 case REAL_CST:
940 case FIXED_CST:
941 case CONSTRUCTOR:
942 case SSA_NAME:
943 temp.op0 = ref;
944 break;
945 case ADDR_EXPR:
946 if (is_gimple_min_invariant (ref))
948 temp.op0 = ref;
949 break;
951 break;
952 /* These are only interesting for their operands, their
953 existence, and their type. They will never be the last
954 ref in the chain of references (IE they require an
955 operand), so we don't have to put anything
956 for op* as it will be handled by the iteration */
957 case REALPART_EXPR:
958 temp.off = 0;
959 break;
960 case VIEW_CONVERT_EXPR:
961 temp.off = 0;
962 temp.reverse = storage_order_barrier_p (ref);
963 break;
964 case IMAGPART_EXPR:
965 /* This is only interesting for its constant offset. */
966 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
967 break;
968 default:
969 gcc_unreachable ();
971 result->safe_push (temp);
973 if (REFERENCE_CLASS_P (ref)
974 || TREE_CODE (ref) == MODIFY_EXPR
975 || TREE_CODE (ref) == WITH_SIZE_EXPR
976 || (TREE_CODE (ref) == ADDR_EXPR
977 && !is_gimple_min_invariant (ref)))
978 ref = TREE_OPERAND (ref, 0);
979 else
980 ref = NULL_TREE;
984 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
985 operands in *OPS, the reference alias set SET and the reference type TYPE.
986 Return true if something useful was produced. */
988 bool
989 ao_ref_init_from_vn_reference (ao_ref *ref,
990 alias_set_type set, tree type,
991 vec<vn_reference_op_s> ops)
993 vn_reference_op_t op;
994 unsigned i;
995 tree base = NULL_TREE;
996 tree *op0_p = &base;
997 HOST_WIDE_INT offset = 0;
998 HOST_WIDE_INT max_size;
999 HOST_WIDE_INT size = -1;
1000 tree size_tree = NULL_TREE;
1001 alias_set_type base_alias_set = -1;
1003 /* First get the final access size from just the outermost expression. */
1004 op = &ops[0];
1005 if (op->opcode == COMPONENT_REF)
1006 size_tree = DECL_SIZE (op->op0);
1007 else if (op->opcode == BIT_FIELD_REF)
1008 size_tree = op->op0;
1009 else
1011 machine_mode mode = TYPE_MODE (type);
1012 if (mode == BLKmode)
1013 size_tree = TYPE_SIZE (type);
1014 else
1015 size = GET_MODE_BITSIZE (mode);
1017 if (size_tree != NULL_TREE)
1019 if (!tree_fits_uhwi_p (size_tree))
1020 size = -1;
1021 else
1022 size = tree_to_uhwi (size_tree);
1025 /* Initially, maxsize is the same as the accessed element size.
1026 In the following it will only grow (or become -1). */
1027 max_size = size;
1029 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1030 and find the ultimate containing object. */
1031 FOR_EACH_VEC_ELT (ops, i, op)
1033 switch (op->opcode)
1035 /* These may be in the reference ops, but we cannot do anything
1036 sensible with them here. */
1037 case ADDR_EXPR:
1038 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1039 if (base != NULL_TREE
1040 && TREE_CODE (base) == MEM_REF
1041 && op->op0
1042 && DECL_P (TREE_OPERAND (op->op0, 0)))
1044 vn_reference_op_t pop = &ops[i-1];
1045 base = TREE_OPERAND (op->op0, 0);
1046 if (pop->off == -1)
1048 max_size = -1;
1049 offset = 0;
1051 else
1052 offset += pop->off * BITS_PER_UNIT;
1053 op0_p = NULL;
1054 break;
1056 /* Fallthru. */
1057 case CALL_EXPR:
1058 return false;
1060 /* Record the base objects. */
1061 case MEM_REF:
1062 base_alias_set = get_deref_alias_set (op->op0);
1063 *op0_p = build2 (MEM_REF, op->type,
1064 NULL_TREE, op->op0);
1065 op0_p = &TREE_OPERAND (*op0_p, 0);
1066 break;
1068 case VAR_DECL:
1069 case PARM_DECL:
1070 case RESULT_DECL:
1071 case SSA_NAME:
1072 *op0_p = op->op0;
1073 op0_p = NULL;
1074 break;
1076 /* And now the usual component-reference style ops. */
1077 case BIT_FIELD_REF:
1078 offset += tree_to_shwi (op->op1);
1079 break;
1081 case COMPONENT_REF:
1083 tree field = op->op0;
1084 /* We do not have a complete COMPONENT_REF tree here so we
1085 cannot use component_ref_field_offset. Do the interesting
1086 parts manually. */
1088 if (op->op1
1089 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
1090 max_size = -1;
1091 else
1093 offset += (tree_to_uhwi (DECL_FIELD_OFFSET (field))
1094 * BITS_PER_UNIT);
1095 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
1097 break;
1100 case ARRAY_RANGE_REF:
1101 case ARRAY_REF:
1102 /* We recorded the lower bound and the element size. */
1103 if (!tree_fits_shwi_p (op->op0)
1104 || !tree_fits_shwi_p (op->op1)
1105 || !tree_fits_shwi_p (op->op2))
1106 max_size = -1;
1107 else
1109 HOST_WIDE_INT hindex = tree_to_shwi (op->op0);
1110 hindex -= tree_to_shwi (op->op1);
1111 hindex *= tree_to_shwi (op->op2);
1112 hindex *= BITS_PER_UNIT;
1113 offset += hindex;
1115 break;
1117 case REALPART_EXPR:
1118 break;
1120 case IMAGPART_EXPR:
1121 offset += size;
1122 break;
1124 case VIEW_CONVERT_EXPR:
1125 break;
1127 case STRING_CST:
1128 case INTEGER_CST:
1129 case COMPLEX_CST:
1130 case VECTOR_CST:
1131 case REAL_CST:
1132 case CONSTRUCTOR:
1133 case CONST_DECL:
1134 return false;
1136 default:
1137 return false;
1141 if (base == NULL_TREE)
1142 return false;
1144 ref->ref = NULL_TREE;
1145 ref->base = base;
1146 ref->offset = offset;
1147 ref->size = size;
1148 ref->max_size = max_size;
1149 ref->ref_alias_set = set;
1150 if (base_alias_set != -1)
1151 ref->base_alias_set = base_alias_set;
1152 else
1153 ref->base_alias_set = get_alias_set (base);
1154 /* We discount volatiles from value-numbering elsewhere. */
1155 ref->volatile_p = false;
1157 return true;
1160 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1161 vn_reference_op_s's. */
1163 static void
1164 copy_reference_ops_from_call (gcall *call,
1165 vec<vn_reference_op_s> *result)
1167 vn_reference_op_s temp;
1168 unsigned i;
1169 tree lhs = gimple_call_lhs (call);
1170 int lr;
1172 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1173 different. By adding the lhs here in the vector, we ensure that the
1174 hashcode is different, guaranteeing a different value number. */
1175 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1177 memset (&temp, 0, sizeof (temp));
1178 temp.opcode = MODIFY_EXPR;
1179 temp.type = TREE_TYPE (lhs);
1180 temp.op0 = lhs;
1181 temp.off = -1;
1182 result->safe_push (temp);
1185 /* Copy the type, opcode, function, static chain and EH region, if any. */
1186 memset (&temp, 0, sizeof (temp));
1187 temp.type = gimple_call_return_type (call);
1188 temp.opcode = CALL_EXPR;
1189 temp.op0 = gimple_call_fn (call);
1190 temp.op1 = gimple_call_chain (call);
1191 if (stmt_could_throw_p (call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1192 temp.op2 = size_int (lr);
1193 temp.off = -1;
1194 if (gimple_call_with_bounds_p (call))
1195 temp.with_bounds = 1;
1196 result->safe_push (temp);
1198 /* Copy the call arguments. As they can be references as well,
1199 just chain them together. */
1200 for (i = 0; i < gimple_call_num_args (call); ++i)
1202 tree callarg = gimple_call_arg (call, i);
1203 copy_reference_ops_from_ref (callarg, result);
1207 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1208 *I_P to point to the last element of the replacement. */
1209 void
1210 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1211 unsigned int *i_p)
1213 unsigned int i = *i_p;
1214 vn_reference_op_t op = &(*ops)[i];
1215 vn_reference_op_t mem_op = &(*ops)[i - 1];
1216 tree addr_base;
1217 HOST_WIDE_INT addr_offset = 0;
1219 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1220 from .foo.bar to the preceding MEM_REF offset and replace the
1221 address with &OBJ. */
1222 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1223 &addr_offset);
1224 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1225 if (addr_base != TREE_OPERAND (op->op0, 0))
1227 offset_int off = offset_int::from (mem_op->op0, SIGNED);
1228 off += addr_offset;
1229 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1230 op->op0 = build_fold_addr_expr (addr_base);
1231 if (tree_fits_shwi_p (mem_op->op0))
1232 mem_op->off = tree_to_shwi (mem_op->op0);
1233 else
1234 mem_op->off = -1;
1238 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1239 *I_P to point to the last element of the replacement. */
1240 static void
1241 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1242 unsigned int *i_p)
1244 unsigned int i = *i_p;
1245 vn_reference_op_t op = &(*ops)[i];
1246 vn_reference_op_t mem_op = &(*ops)[i - 1];
1247 gimple def_stmt;
1248 enum tree_code code;
1249 offset_int off;
1251 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1252 if (!is_gimple_assign (def_stmt))
1253 return;
1255 code = gimple_assign_rhs_code (def_stmt);
1256 if (code != ADDR_EXPR
1257 && code != POINTER_PLUS_EXPR)
1258 return;
1260 off = offset_int::from (mem_op->op0, SIGNED);
1262 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1263 from .foo.bar to the preceding MEM_REF offset and replace the
1264 address with &OBJ. */
1265 if (code == ADDR_EXPR)
1267 tree addr, addr_base;
1268 HOST_WIDE_INT addr_offset;
1270 addr = gimple_assign_rhs1 (def_stmt);
1271 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1272 &addr_offset);
1273 if (!addr_base
1274 || TREE_CODE (addr_base) != MEM_REF)
1275 return;
1277 off += addr_offset;
1278 off += mem_ref_offset (addr_base);
1279 op->op0 = TREE_OPERAND (addr_base, 0);
1281 else
1283 tree ptr, ptroff;
1284 ptr = gimple_assign_rhs1 (def_stmt);
1285 ptroff = gimple_assign_rhs2 (def_stmt);
1286 if (TREE_CODE (ptr) != SSA_NAME
1287 || TREE_CODE (ptroff) != INTEGER_CST)
1288 return;
1290 off += wi::to_offset (ptroff);
1291 op->op0 = ptr;
1294 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1295 if (tree_fits_shwi_p (mem_op->op0))
1296 mem_op->off = tree_to_shwi (mem_op->op0);
1297 else
1298 mem_op->off = -1;
1299 if (TREE_CODE (op->op0) == SSA_NAME)
1300 op->op0 = SSA_VAL (op->op0);
1301 if (TREE_CODE (op->op0) != SSA_NAME)
1302 op->opcode = TREE_CODE (op->op0);
1304 /* And recurse. */
1305 if (TREE_CODE (op->op0) == SSA_NAME)
1306 vn_reference_maybe_forwprop_address (ops, i_p);
1307 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1308 vn_reference_fold_indirect (ops, i_p);
1311 /* Optimize the reference REF to a constant if possible or return
1312 NULL_TREE if not. */
1314 tree
1315 fully_constant_vn_reference_p (vn_reference_t ref)
1317 vec<vn_reference_op_s> operands = ref->operands;
1318 vn_reference_op_t op;
1320 /* Try to simplify the translated expression if it is
1321 a call to a builtin function with at most two arguments. */
1322 op = &operands[0];
1323 if (op->opcode == CALL_EXPR
1324 && TREE_CODE (op->op0) == ADDR_EXPR
1325 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1326 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1327 && operands.length () >= 2
1328 && operands.length () <= 3)
1330 vn_reference_op_t arg0, arg1 = NULL;
1331 bool anyconst = false;
1332 arg0 = &operands[1];
1333 if (operands.length () > 2)
1334 arg1 = &operands[2];
1335 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1336 || (arg0->opcode == ADDR_EXPR
1337 && is_gimple_min_invariant (arg0->op0)))
1338 anyconst = true;
1339 if (arg1
1340 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1341 || (arg1->opcode == ADDR_EXPR
1342 && is_gimple_min_invariant (arg1->op0))))
1343 anyconst = true;
1344 if (anyconst)
1346 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1347 arg1 ? 2 : 1,
1348 arg0->op0,
1349 arg1 ? arg1->op0 : NULL);
1350 if (folded
1351 && TREE_CODE (folded) == NOP_EXPR)
1352 folded = TREE_OPERAND (folded, 0);
1353 if (folded
1354 && is_gimple_min_invariant (folded))
1355 return folded;
1359 /* Simplify reads from constants or constant initializers. */
1360 else if (BITS_PER_UNIT == 8
1361 && is_gimple_reg_type (ref->type)
1362 && (!INTEGRAL_TYPE_P (ref->type)
1363 || TYPE_PRECISION (ref->type) % BITS_PER_UNIT == 0))
1365 HOST_WIDE_INT off = 0;
1366 HOST_WIDE_INT size;
1367 if (INTEGRAL_TYPE_P (ref->type))
1368 size = TYPE_PRECISION (ref->type);
1369 else
1370 size = tree_to_shwi (TYPE_SIZE (ref->type));
1371 if (size % BITS_PER_UNIT != 0
1372 || size > MAX_BITSIZE_MODE_ANY_MODE)
1373 return NULL_TREE;
1374 size /= BITS_PER_UNIT;
1375 unsigned i;
1376 for (i = 0; i < operands.length (); ++i)
1378 if (operands[i].off == -1)
1379 return NULL_TREE;
1380 off += operands[i].off;
1381 if (operands[i].opcode == MEM_REF)
1383 ++i;
1384 break;
1387 vn_reference_op_t base = &operands[--i];
1388 tree ctor = error_mark_node;
1389 tree decl = NULL_TREE;
1390 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1391 ctor = base->op0;
1392 else if (base->opcode == MEM_REF
1393 && base[1].opcode == ADDR_EXPR
1394 && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
1395 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL))
1397 decl = TREE_OPERAND (base[1].op0, 0);
1398 ctor = ctor_for_folding (decl);
1400 if (ctor == NULL_TREE)
1401 return build_zero_cst (ref->type);
1402 else if (ctor != error_mark_node)
1404 if (decl)
1406 tree res = fold_ctor_reference (ref->type, ctor,
1407 off * BITS_PER_UNIT,
1408 size * BITS_PER_UNIT, decl);
1409 if (res)
1411 STRIP_USELESS_TYPE_CONVERSION (res);
1412 if (is_gimple_min_invariant (res))
1413 return res;
1416 else
1418 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1419 if (native_encode_expr (ctor, buf, size, off) > 0)
1420 return native_interpret_expr (ref->type, buf, size);
1425 return NULL_TREE;
1428 /* Return true if OPS contain a storage order barrier. */
1430 static bool
1431 contains_storage_order_barrier_p (vec<vn_reference_op_s> ops)
1433 vn_reference_op_t op;
1434 unsigned i;
1436 FOR_EACH_VEC_ELT (ops, i, op)
1437 if (op->opcode == VIEW_CONVERT_EXPR && op->reverse)
1438 return true;
1440 return false;
1443 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1444 structures into their value numbers. This is done in-place, and
1445 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1446 whether any operands were valueized. */
1448 static vec<vn_reference_op_s>
1449 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1451 vn_reference_op_t vro;
1452 unsigned int i;
1454 *valueized_anything = false;
1456 FOR_EACH_VEC_ELT (orig, i, vro)
1458 if (vro->opcode == SSA_NAME
1459 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1461 tree tem = SSA_VAL (vro->op0);
1462 if (tem != vro->op0)
1464 *valueized_anything = true;
1465 vro->op0 = tem;
1467 /* If it transforms from an SSA_NAME to a constant, update
1468 the opcode. */
1469 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1470 vro->opcode = TREE_CODE (vro->op0);
1472 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1474 tree tem = SSA_VAL (vro->op1);
1475 if (tem != vro->op1)
1477 *valueized_anything = true;
1478 vro->op1 = tem;
1481 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1483 tree tem = SSA_VAL (vro->op2);
1484 if (tem != vro->op2)
1486 *valueized_anything = true;
1487 vro->op2 = tem;
1490 /* If it transforms from an SSA_NAME to an address, fold with
1491 a preceding indirect reference. */
1492 if (i > 0
1493 && vro->op0
1494 && TREE_CODE (vro->op0) == ADDR_EXPR
1495 && orig[i - 1].opcode == MEM_REF)
1496 vn_reference_fold_indirect (&orig, &i);
1497 else if (i > 0
1498 && vro->opcode == SSA_NAME
1499 && orig[i - 1].opcode == MEM_REF)
1500 vn_reference_maybe_forwprop_address (&orig, &i);
1501 /* If it transforms a non-constant ARRAY_REF into a constant
1502 one, adjust the constant offset. */
1503 else if (vro->opcode == ARRAY_REF
1504 && vro->off == -1
1505 && TREE_CODE (vro->op0) == INTEGER_CST
1506 && TREE_CODE (vro->op1) == INTEGER_CST
1507 && TREE_CODE (vro->op2) == INTEGER_CST)
1509 offset_int off = ((wi::to_offset (vro->op0)
1510 - wi::to_offset (vro->op1))
1511 * wi::to_offset (vro->op2));
1512 if (wi::fits_shwi_p (off))
1513 vro->off = off.to_shwi ();
1517 return orig;
1520 static vec<vn_reference_op_s>
1521 valueize_refs (vec<vn_reference_op_s> orig)
1523 bool tem;
1524 return valueize_refs_1 (orig, &tem);
1527 static vec<vn_reference_op_s> shared_lookup_references;
1529 /* Create a vector of vn_reference_op_s structures from REF, a
1530 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1531 this function. *VALUEIZED_ANYTHING will specify whether any
1532 operands were valueized. */
1534 static vec<vn_reference_op_s>
1535 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1537 if (!ref)
1538 return vNULL;
1539 shared_lookup_references.truncate (0);
1540 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1541 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1542 valueized_anything);
1543 return shared_lookup_references;
1546 /* Create a vector of vn_reference_op_s structures from CALL, a
1547 call statement. The vector is shared among all callers of
1548 this function. */
1550 static vec<vn_reference_op_s>
1551 valueize_shared_reference_ops_from_call (gcall *call)
1553 if (!call)
1554 return vNULL;
1555 shared_lookup_references.truncate (0);
1556 copy_reference_ops_from_call (call, &shared_lookup_references);
1557 shared_lookup_references = valueize_refs (shared_lookup_references);
1558 return shared_lookup_references;
1561 /* Lookup a SCCVN reference operation VR in the current hash table.
1562 Returns the resulting value number if it exists in the hash table,
1563 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1564 vn_reference_t stored in the hashtable if something is found. */
1566 static tree
1567 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1569 vn_reference_s **slot;
1570 hashval_t hash;
1572 hash = vr->hashcode;
1573 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1574 if (!slot && current_info == optimistic_info)
1575 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1576 if (slot)
1578 if (vnresult)
1579 *vnresult = (vn_reference_t)*slot;
1580 return ((vn_reference_t)*slot)->result;
1583 return NULL_TREE;
1586 static tree *last_vuse_ptr;
1587 static vn_lookup_kind vn_walk_kind;
1588 static vn_lookup_kind default_vn_walk_kind;
1590 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1591 with the current VUSE and performs the expression lookup. */
1593 static void *
1594 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1595 unsigned int cnt, void *vr_)
1597 vn_reference_t vr = (vn_reference_t)vr_;
1598 vn_reference_s **slot;
1599 hashval_t hash;
1601 /* This bounds the stmt walks we perform on reference lookups
1602 to O(1) instead of O(N) where N is the number of dominating
1603 stores. */
1604 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1605 return (void *)-1;
1607 if (last_vuse_ptr)
1608 *last_vuse_ptr = vuse;
1610 /* Fixup vuse and hash. */
1611 if (vr->vuse)
1612 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1613 vr->vuse = vuse_ssa_val (vuse);
1614 if (vr->vuse)
1615 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1617 hash = vr->hashcode;
1618 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1619 if (!slot && current_info == optimistic_info)
1620 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1621 if (slot)
1622 return *slot;
1624 return NULL;
1627 /* Lookup an existing or insert a new vn_reference entry into the
1628 value table for the VUSE, SET, TYPE, OPERANDS reference which
1629 has the value VALUE which is either a constant or an SSA name. */
1631 static vn_reference_t
1632 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1633 alias_set_type set,
1634 tree type,
1635 vec<vn_reference_op_s,
1636 va_heap> operands,
1637 tree value)
1639 vn_reference_s vr1;
1640 vn_reference_t result;
1641 unsigned value_id;
1642 vr1.vuse = vuse;
1643 vr1.operands = operands;
1644 vr1.type = type;
1645 vr1.set = set;
1646 vr1.hashcode = vn_reference_compute_hash (&vr1);
1647 if (vn_reference_lookup_1 (&vr1, &result))
1648 return result;
1649 if (TREE_CODE (value) == SSA_NAME)
1650 value_id = VN_INFO (value)->value_id;
1651 else
1652 value_id = get_or_alloc_constant_value_id (value);
1653 return vn_reference_insert_pieces (vuse, set, type,
1654 operands.copy (), value, value_id);
1657 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1658 from the statement defining VUSE and if not successful tries to
1659 translate *REFP and VR_ through an aggregate copy at the definition
1660 of VUSE. */
1662 static void *
1663 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1664 bool disambiguate_only)
1666 vn_reference_t vr = (vn_reference_t)vr_;
1667 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1668 tree base;
1669 HOST_WIDE_INT offset, maxsize;
1670 static vec<vn_reference_op_s>
1671 lhs_ops = vNULL;
1672 ao_ref lhs_ref;
1673 bool lhs_ref_ok = false;
1675 /* First try to disambiguate after value-replacing in the definitions LHS. */
1676 if (is_gimple_assign (def_stmt))
1678 vec<vn_reference_op_s> tem;
1679 tree lhs = gimple_assign_lhs (def_stmt);
1680 bool valueized_anything = false;
1681 /* Avoid re-allocation overhead. */
1682 lhs_ops.truncate (0);
1683 copy_reference_ops_from_ref (lhs, &lhs_ops);
1684 tem = lhs_ops;
1685 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1686 gcc_assert (lhs_ops == tem);
1687 if (valueized_anything)
1689 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1690 get_alias_set (lhs),
1691 TREE_TYPE (lhs), lhs_ops);
1692 if (lhs_ref_ok
1693 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1694 return NULL;
1696 else
1698 ao_ref_init (&lhs_ref, lhs);
1699 lhs_ref_ok = true;
1702 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1703 && gimple_call_num_args (def_stmt) <= 4)
1705 /* For builtin calls valueize its arguments and call the
1706 alias oracle again. Valueization may improve points-to
1707 info of pointers and constify size and position arguments.
1708 Originally this was motivated by PR61034 which has
1709 conditional calls to free falsely clobbering ref because
1710 of imprecise points-to info of the argument. */
1711 tree oldargs[4];
1712 bool valueized_anything = false;
1713 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1715 oldargs[i] = gimple_call_arg (def_stmt, i);
1716 if (TREE_CODE (oldargs[i]) == SSA_NAME
1717 && VN_INFO (oldargs[i])->valnum != oldargs[i])
1719 gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum);
1720 valueized_anything = true;
1723 if (valueized_anything)
1725 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
1726 ref);
1727 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1728 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1729 if (!res)
1730 return NULL;
1734 if (disambiguate_only)
1735 return (void *)-1;
1737 base = ao_ref_base (ref);
1738 offset = ref->offset;
1739 maxsize = ref->max_size;
1741 /* If we cannot constrain the size of the reference we cannot
1742 test if anything kills it. */
1743 if (maxsize == -1)
1744 return (void *)-1;
1746 /* We can't deduce anything useful from clobbers. */
1747 if (gimple_clobber_p (def_stmt))
1748 return (void *)-1;
1750 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1751 from that definition.
1752 1) Memset. */
1753 if (is_gimple_reg_type (vr->type)
1754 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1755 && integer_zerop (gimple_call_arg (def_stmt, 1))
1756 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))
1757 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1759 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1760 tree base2;
1761 HOST_WIDE_INT offset2, size2, maxsize2;
1762 bool reverse;
1763 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2,
1764 &reverse);
1765 size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8;
1766 if ((unsigned HOST_WIDE_INT)size2 / 8
1767 == tree_to_uhwi (gimple_call_arg (def_stmt, 2))
1768 && maxsize2 != -1
1769 && operand_equal_p (base, base2, 0)
1770 && offset2 <= offset
1771 && offset2 + size2 >= offset + maxsize)
1773 tree val = build_zero_cst (vr->type);
1774 return vn_reference_lookup_or_insert_for_pieces
1775 (vuse, vr->set, vr->type, vr->operands, val);
1779 /* 2) Assignment from an empty CONSTRUCTOR. */
1780 else if (is_gimple_reg_type (vr->type)
1781 && gimple_assign_single_p (def_stmt)
1782 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1783 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1785 tree base2;
1786 HOST_WIDE_INT offset2, size2, maxsize2;
1787 bool reverse;
1788 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1789 &offset2, &size2, &maxsize2, &reverse);
1790 if (maxsize2 != -1
1791 && operand_equal_p (base, base2, 0)
1792 && offset2 <= offset
1793 && offset2 + size2 >= offset + maxsize)
1795 tree val = build_zero_cst (vr->type);
1796 return vn_reference_lookup_or_insert_for_pieces
1797 (vuse, vr->set, vr->type, vr->operands, val);
1801 /* 3) Assignment from a constant. We can use folds native encode/interpret
1802 routines to extract the assigned bits. */
1803 else if (vn_walk_kind == VN_WALKREWRITE
1804 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1805 && ref->size == maxsize
1806 && maxsize % BITS_PER_UNIT == 0
1807 && offset % BITS_PER_UNIT == 0
1808 && is_gimple_reg_type (vr->type)
1809 && !contains_storage_order_barrier_p (vr->operands)
1810 && gimple_assign_single_p (def_stmt)
1811 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1813 tree base2;
1814 HOST_WIDE_INT offset2, size2, maxsize2;
1815 bool reverse;
1816 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1817 &offset2, &size2, &maxsize2, &reverse);
1818 if (maxsize2 != -1
1819 && maxsize2 == size2
1820 && size2 % BITS_PER_UNIT == 0
1821 && offset2 % BITS_PER_UNIT == 0
1822 && operand_equal_p (base, base2, 0)
1823 && offset2 <= offset
1824 && offset2 + size2 >= offset + maxsize)
1826 /* We support up to 512-bit values (for V8DFmode). */
1827 unsigned char buffer[64];
1828 int len;
1830 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1831 buffer, sizeof (buffer));
1832 if (len > 0)
1834 tree val = native_interpret_expr (vr->type,
1835 buffer
1836 + ((offset - offset2)
1837 / BITS_PER_UNIT),
1838 ref->size / BITS_PER_UNIT);
1839 if (val)
1840 return vn_reference_lookup_or_insert_for_pieces
1841 (vuse, vr->set, vr->type, vr->operands, val);
1846 /* 4) Assignment from an SSA name which definition we may be able
1847 to access pieces from. */
1848 else if (ref->size == maxsize
1849 && is_gimple_reg_type (vr->type)
1850 && !contains_storage_order_barrier_p (vr->operands)
1851 && gimple_assign_single_p (def_stmt)
1852 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1854 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1855 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1856 if (is_gimple_assign (def_stmt2)
1857 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1858 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1859 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1861 tree base2;
1862 HOST_WIDE_INT offset2, size2, maxsize2, off;
1863 bool reverse;
1864 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1865 &offset2, &size2, &maxsize2,
1866 &reverse);
1867 off = offset - offset2;
1868 if (maxsize2 != -1
1869 && maxsize2 == size2
1870 && operand_equal_p (base, base2, 0)
1871 && offset2 <= offset
1872 && offset2 + size2 >= offset + maxsize)
1874 tree val = NULL_TREE;
1875 HOST_WIDE_INT elsz
1876 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1877 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1879 if (off == 0)
1880 val = gimple_assign_rhs1 (def_stmt2);
1881 else if (off == elsz)
1882 val = gimple_assign_rhs2 (def_stmt2);
1884 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1885 && off % elsz == 0)
1887 tree ctor = gimple_assign_rhs1 (def_stmt2);
1888 unsigned i = off / elsz;
1889 if (i < CONSTRUCTOR_NELTS (ctor))
1891 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1892 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1894 if (TREE_CODE (TREE_TYPE (elt->value))
1895 != VECTOR_TYPE)
1896 val = elt->value;
1900 if (val)
1901 return vn_reference_lookup_or_insert_for_pieces
1902 (vuse, vr->set, vr->type, vr->operands, val);
1907 /* 5) For aggregate copies translate the reference through them if
1908 the copy kills ref. */
1909 else if (vn_walk_kind == VN_WALKREWRITE
1910 && gimple_assign_single_p (def_stmt)
1911 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1912 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1913 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1915 tree base2;
1916 HOST_WIDE_INT offset2, size2, maxsize2;
1917 int i, j, k;
1918 auto_vec<vn_reference_op_s> rhs;
1919 vn_reference_op_t vro;
1920 ao_ref r;
1922 if (!lhs_ref_ok)
1923 return (void *)-1;
1925 /* See if the assignment kills REF. */
1926 base2 = ao_ref_base (&lhs_ref);
1927 offset2 = lhs_ref.offset;
1928 size2 = lhs_ref.size;
1929 maxsize2 = lhs_ref.max_size;
1930 if (maxsize2 == -1
1931 || (base != base2 && !operand_equal_p (base, base2, 0))
1932 || offset2 > offset
1933 || offset2 + size2 < offset + maxsize)
1934 return (void *)-1;
1936 /* Find the common base of ref and the lhs. lhs_ops already
1937 contains valueized operands for the lhs. */
1938 i = vr->operands.length () - 1;
1939 j = lhs_ops.length () - 1;
1940 while (j >= 0 && i >= 0
1941 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1943 i--;
1944 j--;
1947 /* ??? The innermost op should always be a MEM_REF and we already
1948 checked that the assignment to the lhs kills vr. Thus for
1949 aggregate copies using char[] types the vn_reference_op_eq
1950 may fail when comparing types for compatibility. But we really
1951 don't care here - further lookups with the rewritten operands
1952 will simply fail if we messed up types too badly. */
1953 HOST_WIDE_INT extra_off = 0;
1954 if (j == 0 && i >= 0
1955 && lhs_ops[0].opcode == MEM_REF
1956 && lhs_ops[0].off != -1)
1958 if (lhs_ops[0].off == vr->operands[i].off)
1959 i--, j--;
1960 else if (vr->operands[i].opcode == MEM_REF
1961 && vr->operands[i].off != -1)
1963 extra_off = vr->operands[i].off - lhs_ops[0].off;
1964 i--, j--;
1968 /* i now points to the first additional op.
1969 ??? LHS may not be completely contained in VR, one or more
1970 VIEW_CONVERT_EXPRs could be in its way. We could at least
1971 try handling outermost VIEW_CONVERT_EXPRs. */
1972 if (j != -1)
1973 return (void *)-1;
1975 /* Punt if the additional ops contain a storage order barrier. */
1976 for (k = i; k >= 0; k--)
1978 vro = &vr->operands[k];
1979 if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse)
1980 return (void *)-1;
1983 /* Now re-write REF to be based on the rhs of the assignment. */
1984 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1986 /* Apply an extra offset to the inner MEM_REF of the RHS. */
1987 if (extra_off != 0)
1989 if (rhs.length () < 2
1990 || rhs[0].opcode != MEM_REF
1991 || rhs[0].off == -1)
1992 return (void *)-1;
1993 rhs[0].off += extra_off;
1994 rhs[0].op0 = int_const_binop (PLUS_EXPR, rhs[0].op0,
1995 build_int_cst (TREE_TYPE (rhs[0].op0),
1996 extra_off));
1999 /* We need to pre-pend vr->operands[0..i] to rhs. */
2000 vec<vn_reference_op_s> old = vr->operands;
2001 if (i + 1 + rhs.length () > vr->operands.length ())
2003 vr->operands.safe_grow (i + 1 + rhs.length ());
2004 if (old == shared_lookup_references)
2005 shared_lookup_references = vr->operands;
2007 else
2008 vr->operands.truncate (i + 1 + rhs.length ());
2009 FOR_EACH_VEC_ELT (rhs, j, vro)
2010 vr->operands[i + 1 + j] = *vro;
2011 vr->operands = valueize_refs (vr->operands);
2012 if (old == shared_lookup_references)
2013 shared_lookup_references = vr->operands;
2014 vr->hashcode = vn_reference_compute_hash (vr);
2016 /* Try folding the new reference to a constant. */
2017 tree val = fully_constant_vn_reference_p (vr);
2018 if (val)
2019 return vn_reference_lookup_or_insert_for_pieces
2020 (vuse, vr->set, vr->type, vr->operands, val);
2022 /* Adjust *ref from the new operands. */
2023 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2024 return (void *)-1;
2025 /* This can happen with bitfields. */
2026 if (ref->size != r.size)
2027 return (void *)-1;
2028 *ref = r;
2030 /* Do not update last seen VUSE after translating. */
2031 last_vuse_ptr = NULL;
2033 /* Keep looking for the adjusted *REF / VR pair. */
2034 return NULL;
2037 /* 6) For memcpy copies translate the reference through them if
2038 the copy kills ref. */
2039 else if (vn_walk_kind == VN_WALKREWRITE
2040 && is_gimple_reg_type (vr->type)
2041 /* ??? Handle BCOPY as well. */
2042 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
2043 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
2044 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
2045 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2046 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
2047 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
2048 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
2049 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)))
2051 tree lhs, rhs;
2052 ao_ref r;
2053 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
2054 vn_reference_op_s op;
2055 HOST_WIDE_INT at;
2057 /* Only handle non-variable, addressable refs. */
2058 if (ref->size != maxsize
2059 || offset % BITS_PER_UNIT != 0
2060 || ref->size % BITS_PER_UNIT != 0)
2061 return (void *)-1;
2063 /* Extract a pointer base and an offset for the destination. */
2064 lhs = gimple_call_arg (def_stmt, 0);
2065 lhs_offset = 0;
2066 if (TREE_CODE (lhs) == SSA_NAME)
2067 lhs = SSA_VAL (lhs);
2068 if (TREE_CODE (lhs) == ADDR_EXPR)
2070 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
2071 &lhs_offset);
2072 if (!tem)
2073 return (void *)-1;
2074 if (TREE_CODE (tem) == MEM_REF
2075 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2077 lhs = TREE_OPERAND (tem, 0);
2078 lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2080 else if (DECL_P (tem))
2081 lhs = build_fold_addr_expr (tem);
2082 else
2083 return (void *)-1;
2085 if (TREE_CODE (lhs) != SSA_NAME
2086 && TREE_CODE (lhs) != ADDR_EXPR)
2087 return (void *)-1;
2089 /* Extract a pointer base and an offset for the source. */
2090 rhs = gimple_call_arg (def_stmt, 1);
2091 rhs_offset = 0;
2092 if (TREE_CODE (rhs) == SSA_NAME)
2093 rhs = SSA_VAL (rhs);
2094 if (TREE_CODE (rhs) == ADDR_EXPR)
2096 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
2097 &rhs_offset);
2098 if (!tem)
2099 return (void *)-1;
2100 if (TREE_CODE (tem) == MEM_REF
2101 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2103 rhs = TREE_OPERAND (tem, 0);
2104 rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2106 else if (DECL_P (tem))
2107 rhs = build_fold_addr_expr (tem);
2108 else
2109 return (void *)-1;
2111 if (TREE_CODE (rhs) != SSA_NAME
2112 && TREE_CODE (rhs) != ADDR_EXPR)
2113 return (void *)-1;
2115 copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2));
2117 /* The bases of the destination and the references have to agree. */
2118 if ((TREE_CODE (base) != MEM_REF
2119 && !DECL_P (base))
2120 || (TREE_CODE (base) == MEM_REF
2121 && (TREE_OPERAND (base, 0) != lhs
2122 || !tree_fits_uhwi_p (TREE_OPERAND (base, 1))))
2123 || (DECL_P (base)
2124 && (TREE_CODE (lhs) != ADDR_EXPR
2125 || TREE_OPERAND (lhs, 0) != base)))
2126 return (void *)-1;
2128 /* And the access has to be contained within the memcpy destination. */
2129 at = offset / BITS_PER_UNIT;
2130 if (TREE_CODE (base) == MEM_REF)
2131 at += tree_to_uhwi (TREE_OPERAND (base, 1));
2132 if (lhs_offset > at
2133 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
2134 return (void *)-1;
2136 /* Make room for 2 operands in the new reference. */
2137 if (vr->operands.length () < 2)
2139 vec<vn_reference_op_s> old = vr->operands;
2140 vr->operands.safe_grow_cleared (2);
2141 if (old == shared_lookup_references
2142 && vr->operands != old)
2143 shared_lookup_references = vr->operands;
2145 else
2146 vr->operands.truncate (2);
2148 /* The looked-through reference is a simple MEM_REF. */
2149 memset (&op, 0, sizeof (op));
2150 op.type = vr->type;
2151 op.opcode = MEM_REF;
2152 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
2153 op.off = at - lhs_offset + rhs_offset;
2154 vr->operands[0] = op;
2155 op.type = TREE_TYPE (rhs);
2156 op.opcode = TREE_CODE (rhs);
2157 op.op0 = rhs;
2158 op.off = -1;
2159 vr->operands[1] = op;
2160 vr->hashcode = vn_reference_compute_hash (vr);
2162 /* Adjust *ref from the new operands. */
2163 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2164 return (void *)-1;
2165 /* This can happen with bitfields. */
2166 if (ref->size != r.size)
2167 return (void *)-1;
2168 *ref = r;
2170 /* Do not update last seen VUSE after translating. */
2171 last_vuse_ptr = NULL;
2173 /* Keep looking for the adjusted *REF / VR pair. */
2174 return NULL;
2177 /* Bail out and stop walking. */
2178 return (void *)-1;
2181 /* Lookup a reference operation by it's parts, in the current hash table.
2182 Returns the resulting value number if it exists in the hash table,
2183 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2184 vn_reference_t stored in the hashtable if something is found. */
2186 tree
2187 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2188 vec<vn_reference_op_s> operands,
2189 vn_reference_t *vnresult, vn_lookup_kind kind)
2191 struct vn_reference_s vr1;
2192 vn_reference_t tmp;
2193 tree cst;
2195 if (!vnresult)
2196 vnresult = &tmp;
2197 *vnresult = NULL;
2199 vr1.vuse = vuse_ssa_val (vuse);
2200 shared_lookup_references.truncate (0);
2201 shared_lookup_references.safe_grow (operands.length ());
2202 memcpy (shared_lookup_references.address (),
2203 operands.address (),
2204 sizeof (vn_reference_op_s)
2205 * operands.length ());
2206 vr1.operands = operands = shared_lookup_references
2207 = valueize_refs (shared_lookup_references);
2208 vr1.type = type;
2209 vr1.set = set;
2210 vr1.hashcode = vn_reference_compute_hash (&vr1);
2211 if ((cst = fully_constant_vn_reference_p (&vr1)))
2212 return cst;
2214 vn_reference_lookup_1 (&vr1, vnresult);
2215 if (!*vnresult
2216 && kind != VN_NOWALK
2217 && vr1.vuse)
2219 ao_ref r;
2220 vn_walk_kind = kind;
2221 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2222 *vnresult =
2223 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2224 vn_reference_lookup_2,
2225 vn_reference_lookup_3,
2226 vuse_ssa_val, &vr1);
2227 gcc_checking_assert (vr1.operands == shared_lookup_references);
2230 if (*vnresult)
2231 return (*vnresult)->result;
2233 return NULL_TREE;
2236 /* Lookup OP in the current hash table, and return the resulting value
2237 number if it exists in the hash table. Return NULL_TREE if it does
2238 not exist in the hash table or if the result field of the structure
2239 was NULL.. VNRESULT will be filled in with the vn_reference_t
2240 stored in the hashtable if one exists. */
2242 tree
2243 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2244 vn_reference_t *vnresult)
2246 vec<vn_reference_op_s> operands;
2247 struct vn_reference_s vr1;
2248 tree cst;
2249 bool valuezied_anything;
2251 if (vnresult)
2252 *vnresult = NULL;
2254 vr1.vuse = vuse_ssa_val (vuse);
2255 vr1.operands = operands
2256 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2257 vr1.type = TREE_TYPE (op);
2258 vr1.set = get_alias_set (op);
2259 vr1.hashcode = vn_reference_compute_hash (&vr1);
2260 if ((cst = fully_constant_vn_reference_p (&vr1)))
2261 return cst;
2263 if (kind != VN_NOWALK
2264 && vr1.vuse)
2266 vn_reference_t wvnresult;
2267 ao_ref r;
2268 /* Make sure to use a valueized reference if we valueized anything.
2269 Otherwise preserve the full reference for advanced TBAA. */
2270 if (!valuezied_anything
2271 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2272 vr1.operands))
2273 ao_ref_init (&r, op);
2274 vn_walk_kind = kind;
2275 wvnresult =
2276 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2277 vn_reference_lookup_2,
2278 vn_reference_lookup_3,
2279 vuse_ssa_val, &vr1);
2280 gcc_checking_assert (vr1.operands == shared_lookup_references);
2281 if (wvnresult)
2283 if (vnresult)
2284 *vnresult = wvnresult;
2285 return wvnresult->result;
2288 return NULL_TREE;
2291 return vn_reference_lookup_1 (&vr1, vnresult);
2294 /* Lookup CALL in the current hash table and return the entry in
2295 *VNRESULT if found. Populates *VR for the hashtable lookup. */
2297 void
2298 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
2299 vn_reference_t vr)
2301 if (vnresult)
2302 *vnresult = NULL;
2304 tree vuse = gimple_vuse (call);
2306 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2307 vr->operands = valueize_shared_reference_ops_from_call (call);
2308 vr->type = gimple_expr_type (call);
2309 vr->set = 0;
2310 vr->hashcode = vn_reference_compute_hash (vr);
2311 vn_reference_lookup_1 (vr, vnresult);
2314 /* Insert OP into the current hash table with a value number of
2315 RESULT, and return the resulting reference structure we created. */
2317 static vn_reference_t
2318 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2320 vn_reference_s **slot;
2321 vn_reference_t vr1;
2322 bool tem;
2324 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2325 if (TREE_CODE (result) == SSA_NAME)
2326 vr1->value_id = VN_INFO (result)->value_id;
2327 else
2328 vr1->value_id = get_or_alloc_constant_value_id (result);
2329 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2330 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2331 vr1->type = TREE_TYPE (op);
2332 vr1->set = get_alias_set (op);
2333 vr1->hashcode = vn_reference_compute_hash (vr1);
2334 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2335 vr1->result_vdef = vdef;
2337 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2338 INSERT);
2340 /* Because we lookup stores using vuses, and value number failures
2341 using the vdefs (see visit_reference_op_store for how and why),
2342 it's possible that on failure we may try to insert an already
2343 inserted store. This is not wrong, there is no ssa name for a
2344 store that we could use as a differentiator anyway. Thus, unlike
2345 the other lookup functions, you cannot gcc_assert (!*slot)
2346 here. */
2348 /* But free the old slot in case of a collision. */
2349 if (*slot)
2350 free_reference (*slot);
2352 *slot = vr1;
2353 return vr1;
2356 /* Insert a reference by it's pieces into the current hash table with
2357 a value number of RESULT. Return the resulting reference
2358 structure we created. */
2360 vn_reference_t
2361 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2362 vec<vn_reference_op_s> operands,
2363 tree result, unsigned int value_id)
2366 vn_reference_s **slot;
2367 vn_reference_t vr1;
2369 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2370 vr1->value_id = value_id;
2371 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2372 vr1->operands = valueize_refs (operands);
2373 vr1->type = type;
2374 vr1->set = set;
2375 vr1->hashcode = vn_reference_compute_hash (vr1);
2376 if (result && TREE_CODE (result) == SSA_NAME)
2377 result = SSA_VAL (result);
2378 vr1->result = result;
2380 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2381 INSERT);
2383 /* At this point we should have all the things inserted that we have
2384 seen before, and we should never try inserting something that
2385 already exists. */
2386 gcc_assert (!*slot);
2387 if (*slot)
2388 free_reference (*slot);
2390 *slot = vr1;
2391 return vr1;
2394 /* Compute and return the hash value for nary operation VBO1. */
2396 static hashval_t
2397 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2399 inchash::hash hstate;
2400 unsigned i;
2402 for (i = 0; i < vno1->length; ++i)
2403 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2404 vno1->op[i] = SSA_VAL (vno1->op[i]);
2406 if (vno1->length == 2
2407 && commutative_tree_code (vno1->opcode)
2408 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2410 tree temp = vno1->op[0];
2411 vno1->op[0] = vno1->op[1];
2412 vno1->op[1] = temp;
2415 hstate.add_int (vno1->opcode);
2416 for (i = 0; i < vno1->length; ++i)
2417 inchash::add_expr (vno1->op[i], hstate);
2419 return hstate.end ();
2422 /* Compare nary operations VNO1 and VNO2 and return true if they are
2423 equivalent. */
2425 bool
2426 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2428 unsigned i;
2430 if (vno1->hashcode != vno2->hashcode)
2431 return false;
2433 if (vno1->length != vno2->length)
2434 return false;
2436 if (vno1->opcode != vno2->opcode
2437 || !types_compatible_p (vno1->type, vno2->type))
2438 return false;
2440 for (i = 0; i < vno1->length; ++i)
2441 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2442 return false;
2444 return true;
2447 /* Initialize VNO from the pieces provided. */
2449 static void
2450 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2451 enum tree_code code, tree type, tree *ops)
2453 vno->opcode = code;
2454 vno->length = length;
2455 vno->type = type;
2456 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2459 /* Initialize VNO from OP. */
2461 static void
2462 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2464 unsigned i;
2466 vno->opcode = TREE_CODE (op);
2467 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2468 vno->type = TREE_TYPE (op);
2469 for (i = 0; i < vno->length; ++i)
2470 vno->op[i] = TREE_OPERAND (op, i);
2473 /* Return the number of operands for a vn_nary ops structure from STMT. */
2475 static unsigned int
2476 vn_nary_length_from_stmt (gimple stmt)
2478 switch (gimple_assign_rhs_code (stmt))
2480 case REALPART_EXPR:
2481 case IMAGPART_EXPR:
2482 case VIEW_CONVERT_EXPR:
2483 return 1;
2485 case BIT_FIELD_REF:
2486 return 3;
2488 case CONSTRUCTOR:
2489 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2491 default:
2492 return gimple_num_ops (stmt) - 1;
2496 /* Initialize VNO from STMT. */
2498 static void
2499 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2501 unsigned i;
2503 vno->opcode = gimple_assign_rhs_code (stmt);
2504 vno->type = gimple_expr_type (stmt);
2505 switch (vno->opcode)
2507 case REALPART_EXPR:
2508 case IMAGPART_EXPR:
2509 case VIEW_CONVERT_EXPR:
2510 vno->length = 1;
2511 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2512 break;
2514 case BIT_FIELD_REF:
2515 vno->length = 3;
2516 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2517 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2518 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2519 break;
2521 case CONSTRUCTOR:
2522 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2523 for (i = 0; i < vno->length; ++i)
2524 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2525 break;
2527 default:
2528 gcc_checking_assert (!gimple_assign_single_p (stmt));
2529 vno->length = gimple_num_ops (stmt) - 1;
2530 for (i = 0; i < vno->length; ++i)
2531 vno->op[i] = gimple_op (stmt, i + 1);
2535 /* Compute the hashcode for VNO and look for it in the hash table;
2536 return the resulting value number if it exists in the hash table.
2537 Return NULL_TREE if it does not exist in the hash table or if the
2538 result field of the operation is NULL. VNRESULT will contain the
2539 vn_nary_op_t from the hashtable if it exists. */
2541 static tree
2542 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2544 vn_nary_op_s **slot;
2546 if (vnresult)
2547 *vnresult = NULL;
2549 vno->hashcode = vn_nary_op_compute_hash (vno);
2550 slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode,
2551 NO_INSERT);
2552 if (!slot && current_info == optimistic_info)
2553 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode,
2554 NO_INSERT);
2555 if (!slot)
2556 return NULL_TREE;
2557 if (vnresult)
2558 *vnresult = *slot;
2559 return (*slot)->result;
2562 /* Lookup a n-ary operation by its pieces and return the resulting value
2563 number if it exists in the hash table. Return NULL_TREE if it does
2564 not exist in the hash table or if the result field of the operation
2565 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2566 if it exists. */
2568 tree
2569 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2570 tree type, tree *ops, vn_nary_op_t *vnresult)
2572 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2573 sizeof_vn_nary_op (length));
2574 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2575 return vn_nary_op_lookup_1 (vno1, vnresult);
2578 /* Lookup OP in the current hash table, and return the resulting value
2579 number if it exists in the hash table. Return NULL_TREE if it does
2580 not exist in the hash table or if the result field of the operation
2581 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2582 if it exists. */
2584 tree
2585 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2587 vn_nary_op_t vno1
2588 = XALLOCAVAR (struct vn_nary_op_s,
2589 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2590 init_vn_nary_op_from_op (vno1, op);
2591 return vn_nary_op_lookup_1 (vno1, vnresult);
2594 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2595 value number if it exists in the hash table. Return NULL_TREE if
2596 it does not exist in the hash table. VNRESULT will contain the
2597 vn_nary_op_t from the hashtable if it exists. */
2599 tree
2600 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2602 vn_nary_op_t vno1
2603 = XALLOCAVAR (struct vn_nary_op_s,
2604 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2605 init_vn_nary_op_from_stmt (vno1, stmt);
2606 return vn_nary_op_lookup_1 (vno1, vnresult);
2609 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2611 static vn_nary_op_t
2612 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2614 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2617 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2618 obstack. */
2620 static vn_nary_op_t
2621 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2623 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2624 &current_info->nary_obstack);
2626 vno1->value_id = value_id;
2627 vno1->length = length;
2628 vno1->result = result;
2630 return vno1;
2633 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2634 VNO->HASHCODE first. */
2636 static vn_nary_op_t
2637 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
2638 bool compute_hash)
2640 vn_nary_op_s **slot;
2642 if (compute_hash)
2643 vno->hashcode = vn_nary_op_compute_hash (vno);
2645 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
2646 gcc_assert (!*slot);
2648 *slot = vno;
2649 return vno;
2652 /* Insert a n-ary operation into the current hash table using it's
2653 pieces. Return the vn_nary_op_t structure we created and put in
2654 the hashtable. */
2656 vn_nary_op_t
2657 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2658 tree type, tree *ops,
2659 tree result, unsigned int value_id)
2661 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2662 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2663 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2666 /* Insert OP into the current hash table with a value number of
2667 RESULT. Return the vn_nary_op_t structure we created and put in
2668 the hashtable. */
2670 vn_nary_op_t
2671 vn_nary_op_insert (tree op, tree result)
2673 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2674 vn_nary_op_t vno1;
2676 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2677 init_vn_nary_op_from_op (vno1, op);
2678 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2681 /* Insert the rhs of STMT into the current hash table with a value number of
2682 RESULT. */
2684 vn_nary_op_t
2685 vn_nary_op_insert_stmt (gimple stmt, tree result)
2687 vn_nary_op_t vno1
2688 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2689 result, VN_INFO (result)->value_id);
2690 init_vn_nary_op_from_stmt (vno1, stmt);
2691 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2694 /* Compute a hashcode for PHI operation VP1 and return it. */
2696 static inline hashval_t
2697 vn_phi_compute_hash (vn_phi_t vp1)
2699 inchash::hash hstate (vp1->block->index);
2700 int i;
2701 tree phi1op;
2702 tree type;
2704 /* If all PHI arguments are constants we need to distinguish
2705 the PHI node via its type. */
2706 type = vp1->type;
2707 hstate.merge_hash (vn_hash_type (type));
2709 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2711 if (phi1op == VN_TOP)
2712 continue;
2713 inchash::add_expr (phi1op, hstate);
2716 return hstate.end ();
2719 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2721 static int
2722 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2724 if (vp1->hashcode != vp2->hashcode)
2725 return false;
2727 if (vp1->block == vp2->block)
2729 int i;
2730 tree phi1op;
2732 /* If the PHI nodes do not have compatible types
2733 they are not the same. */
2734 if (!types_compatible_p (vp1->type, vp2->type))
2735 return false;
2737 /* Any phi in the same block will have it's arguments in the
2738 same edge order, because of how we store phi nodes. */
2739 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2741 tree phi2op = vp2->phiargs[i];
2742 if (phi1op == VN_TOP || phi2op == VN_TOP)
2743 continue;
2744 if (!expressions_equal_p (phi1op, phi2op))
2745 return false;
2747 return true;
2749 return false;
2752 static vec<tree> shared_lookup_phiargs;
2754 /* Lookup PHI in the current hash table, and return the resulting
2755 value number if it exists in the hash table. Return NULL_TREE if
2756 it does not exist in the hash table. */
2758 static tree
2759 vn_phi_lookup (gimple phi)
2761 vn_phi_s **slot;
2762 struct vn_phi_s vp1;
2763 unsigned i;
2765 shared_lookup_phiargs.truncate (0);
2767 /* Canonicalize the SSA_NAME's to their value number. */
2768 for (i = 0; i < gimple_phi_num_args (phi); i++)
2770 tree def = PHI_ARG_DEF (phi, i);
2771 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2772 shared_lookup_phiargs.safe_push (def);
2774 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2775 vp1.phiargs = shared_lookup_phiargs;
2776 vp1.block = gimple_bb (phi);
2777 vp1.hashcode = vn_phi_compute_hash (&vp1);
2778 slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2779 NO_INSERT);
2780 if (!slot && current_info == optimistic_info)
2781 slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2782 NO_INSERT);
2783 if (!slot)
2784 return NULL_TREE;
2785 return (*slot)->result;
2788 /* Insert PHI into the current hash table with a value number of
2789 RESULT. */
2791 static vn_phi_t
2792 vn_phi_insert (gimple phi, tree result)
2794 vn_phi_s **slot;
2795 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2796 unsigned i;
2797 vec<tree> args = vNULL;
2799 /* Canonicalize the SSA_NAME's to their value number. */
2800 for (i = 0; i < gimple_phi_num_args (phi); i++)
2802 tree def = PHI_ARG_DEF (phi, i);
2803 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2804 args.safe_push (def);
2806 vp1->value_id = VN_INFO (result)->value_id;
2807 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2808 vp1->phiargs = args;
2809 vp1->block = gimple_bb (phi);
2810 vp1->result = result;
2811 vp1->hashcode = vn_phi_compute_hash (vp1);
2813 slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2815 /* Because we iterate over phi operations more than once, it's
2816 possible the slot might already exist here, hence no assert.*/
2817 *slot = vp1;
2818 return vp1;
2822 /* Print set of components in strongly connected component SCC to OUT. */
2824 static void
2825 print_scc (FILE *out, vec<tree> scc)
2827 tree var;
2828 unsigned int i;
2830 fprintf (out, "SCC consists of:");
2831 FOR_EACH_VEC_ELT (scc, i, var)
2833 fprintf (out, " ");
2834 print_generic_expr (out, var, 0);
2836 fprintf (out, "\n");
2839 /* Set the value number of FROM to TO, return true if it has changed
2840 as a result. */
2842 static inline bool
2843 set_ssa_val_to (tree from, tree to)
2845 tree currval = SSA_VAL (from);
2846 HOST_WIDE_INT toff, coff;
2848 /* The only thing we allow as value numbers are ssa_names
2849 and invariants. So assert that here. We don't allow VN_TOP
2850 as visiting a stmt should produce a value-number other than
2851 that.
2852 ??? Still VN_TOP can happen for unreachable code, so force
2853 it to varying in that case. Not all code is prepared to
2854 get VN_TOP on valueization. */
2855 if (to == VN_TOP)
2857 if (dump_file && (dump_flags & TDF_DETAILS))
2858 fprintf (dump_file, "Forcing value number to varying on "
2859 "receiving VN_TOP\n");
2860 to = from;
2863 gcc_assert (to != NULL_TREE
2864 && ((TREE_CODE (to) == SSA_NAME
2865 && (to == from || SSA_VAL (to) == to))
2866 || is_gimple_min_invariant (to)));
2868 if (from != to)
2870 if (currval == from)
2872 if (dump_file && (dump_flags & TDF_DETAILS))
2874 fprintf (dump_file, "Not changing value number of ");
2875 print_generic_expr (dump_file, from, 0);
2876 fprintf (dump_file, " from VARYING to ");
2877 print_generic_expr (dump_file, to, 0);
2878 fprintf (dump_file, "\n");
2880 return false;
2882 else if (TREE_CODE (to) == SSA_NAME
2883 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2884 to = from;
2887 if (dump_file && (dump_flags & TDF_DETAILS))
2889 fprintf (dump_file, "Setting value number of ");
2890 print_generic_expr (dump_file, from, 0);
2891 fprintf (dump_file, " to ");
2892 print_generic_expr (dump_file, to, 0);
2895 if (currval != to
2896 && !operand_equal_p (currval, to, 0)
2897 /* ??? For addresses involving volatile objects or types operand_equal_p
2898 does not reliably detect ADDR_EXPRs as equal. We know we are only
2899 getting invariant gimple addresses here, so can use
2900 get_addr_base_and_unit_offset to do this comparison. */
2901 && !(TREE_CODE (currval) == ADDR_EXPR
2902 && TREE_CODE (to) == ADDR_EXPR
2903 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2904 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2905 && coff == toff))
2907 VN_INFO (from)->valnum = to;
2908 if (dump_file && (dump_flags & TDF_DETAILS))
2909 fprintf (dump_file, " (changed)\n");
2910 return true;
2912 if (dump_file && (dump_flags & TDF_DETAILS))
2913 fprintf (dump_file, "\n");
2914 return false;
2917 /* Mark as processed all the definitions in the defining stmt of USE, or
2918 the USE itself. */
2920 static void
2921 mark_use_processed (tree use)
2923 ssa_op_iter iter;
2924 def_operand_p defp;
2925 gimple stmt = SSA_NAME_DEF_STMT (use);
2927 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2929 VN_INFO (use)->use_processed = true;
2930 return;
2933 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2935 tree def = DEF_FROM_PTR (defp);
2937 VN_INFO (def)->use_processed = true;
2941 /* Set all definitions in STMT to value number to themselves.
2942 Return true if a value number changed. */
2944 static bool
2945 defs_to_varying (gimple stmt)
2947 bool changed = false;
2948 ssa_op_iter iter;
2949 def_operand_p defp;
2951 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2953 tree def = DEF_FROM_PTR (defp);
2954 changed |= set_ssa_val_to (def, def);
2956 return changed;
2959 static bool expr_has_constants (tree expr);
2961 /* Visit a copy between LHS and RHS, return true if the value number
2962 changed. */
2964 static bool
2965 visit_copy (tree lhs, tree rhs)
2967 /* The copy may have a more interesting constant filled expression
2968 (we don't, since we know our RHS is just an SSA name). */
2969 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2970 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2972 /* And finally valueize. */
2973 rhs = SSA_VAL (rhs);
2975 return set_ssa_val_to (lhs, rhs);
2978 /* Visit a nary operator RHS, value number it, and return true if the
2979 value number of LHS has changed as a result. */
2981 static bool
2982 visit_nary_op (tree lhs, gimple stmt)
2984 bool changed = false;
2985 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2987 if (result)
2988 changed = set_ssa_val_to (lhs, result);
2989 else
2991 changed = set_ssa_val_to (lhs, lhs);
2992 vn_nary_op_insert_stmt (stmt, lhs);
2995 return changed;
2998 /* Visit a call STMT storing into LHS. Return true if the value number
2999 of the LHS has changed as a result. */
3001 static bool
3002 visit_reference_op_call (tree lhs, gcall *stmt)
3004 bool changed = false;
3005 struct vn_reference_s vr1;
3006 vn_reference_t vnresult = NULL;
3007 tree vdef = gimple_vdef (stmt);
3009 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
3010 if (lhs && TREE_CODE (lhs) != SSA_NAME)
3011 lhs = NULL_TREE;
3013 vn_reference_lookup_call (stmt, &vnresult, &vr1);
3014 if (vnresult)
3016 if (vnresult->result_vdef && vdef)
3017 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
3019 if (!vnresult->result && lhs)
3020 vnresult->result = lhs;
3022 if (vnresult->result && lhs)
3024 changed |= set_ssa_val_to (lhs, vnresult->result);
3026 if (VN_INFO (vnresult->result)->has_constants)
3027 VN_INFO (lhs)->has_constants = true;
3030 else
3032 vn_reference_t vr2;
3033 vn_reference_s **slot;
3034 if (vdef)
3035 changed |= set_ssa_val_to (vdef, vdef);
3036 if (lhs)
3037 changed |= set_ssa_val_to (lhs, lhs);
3038 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
3039 vr2->vuse = vr1.vuse;
3040 /* As we are not walking the virtual operand chain we know the
3041 shared_lookup_references are still original so we can re-use
3042 them here. */
3043 vr2->operands = vr1.operands.copy ();
3044 vr2->type = vr1.type;
3045 vr2->set = vr1.set;
3046 vr2->hashcode = vr1.hashcode;
3047 vr2->result = lhs;
3048 vr2->result_vdef = vdef;
3049 slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode,
3050 INSERT);
3051 gcc_assert (!*slot);
3052 *slot = vr2;
3055 return changed;
3058 /* Visit a load from a reference operator RHS, part of STMT, value number it,
3059 and return true if the value number of the LHS has changed as a result. */
3061 static bool
3062 visit_reference_op_load (tree lhs, tree op, gimple stmt)
3064 bool changed = false;
3065 tree last_vuse;
3066 tree result;
3068 last_vuse = gimple_vuse (stmt);
3069 last_vuse_ptr = &last_vuse;
3070 result = vn_reference_lookup (op, gimple_vuse (stmt),
3071 default_vn_walk_kind, NULL);
3072 last_vuse_ptr = NULL;
3074 /* We handle type-punning through unions by value-numbering based
3075 on offset and size of the access. Be prepared to handle a
3076 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
3077 if (result
3078 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
3080 /* We will be setting the value number of lhs to the value number
3081 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
3082 So first simplify and lookup this expression to see if it
3083 is already available. */
3084 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
3085 if ((CONVERT_EXPR_P (val)
3086 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
3087 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
3089 tree tem = vn_get_expr_for (TREE_OPERAND (val, 0));
3090 if ((CONVERT_EXPR_P (tem)
3091 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
3092 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
3093 TREE_TYPE (val), tem)))
3094 val = tem;
3096 result = val;
3097 if (!is_gimple_min_invariant (val)
3098 && TREE_CODE (val) != SSA_NAME)
3099 result = vn_nary_op_lookup (val, NULL);
3100 /* If the expression is not yet available, value-number lhs to
3101 a new SSA_NAME we create. */
3102 if (!result)
3104 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
3105 "vntemp");
3106 /* Initialize value-number information properly. */
3107 VN_INFO_GET (result)->valnum = result;
3108 VN_INFO (result)->value_id = get_next_value_id ();
3109 VN_INFO (result)->expr = val;
3110 VN_INFO (result)->has_constants = expr_has_constants (val);
3111 VN_INFO (result)->needs_insertion = true;
3112 /* As all "inserted" statements are singleton SCCs, insert
3113 to the valid table. This is strictly needed to
3114 avoid re-generating new value SSA_NAMEs for the same
3115 expression during SCC iteration over and over (the
3116 optimistic table gets cleared after each iteration).
3117 We do not need to insert into the optimistic table, as
3118 lookups there will fall back to the valid table. */
3119 if (current_info == optimistic_info)
3121 current_info = valid_info;
3122 vn_nary_op_insert (val, result);
3123 current_info = optimistic_info;
3125 else
3126 vn_nary_op_insert (val, result);
3127 if (dump_file && (dump_flags & TDF_DETAILS))
3129 fprintf (dump_file, "Inserting name ");
3130 print_generic_expr (dump_file, result, 0);
3131 fprintf (dump_file, " for expression ");
3132 print_generic_expr (dump_file, val, 0);
3133 fprintf (dump_file, "\n");
3138 if (result)
3140 changed = set_ssa_val_to (lhs, result);
3141 if (TREE_CODE (result) == SSA_NAME
3142 && VN_INFO (result)->has_constants)
3144 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
3145 VN_INFO (lhs)->has_constants = true;
3148 else
3150 changed = set_ssa_val_to (lhs, lhs);
3151 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3154 return changed;
3158 /* Visit a store to a reference operator LHS, part of STMT, value number it,
3159 and return true if the value number of the LHS has changed as a result. */
3161 static bool
3162 visit_reference_op_store (tree lhs, tree op, gimple stmt)
3164 bool changed = false;
3165 vn_reference_t vnresult = NULL;
3166 tree result, assign;
3167 bool resultsame = false;
3168 tree vuse = gimple_vuse (stmt);
3169 tree vdef = gimple_vdef (stmt);
3171 if (TREE_CODE (op) == SSA_NAME)
3172 op = SSA_VAL (op);
3174 /* First we want to lookup using the *vuses* from the store and see
3175 if there the last store to this location with the same address
3176 had the same value.
3178 The vuses represent the memory state before the store. If the
3179 memory state, address, and value of the store is the same as the
3180 last store to this location, then this store will produce the
3181 same memory state as that store.
3183 In this case the vdef versions for this store are value numbered to those
3184 vuse versions, since they represent the same memory state after
3185 this store.
3187 Otherwise, the vdefs for the store are used when inserting into
3188 the table, since the store generates a new memory state. */
3190 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
3192 if (result)
3194 if (TREE_CODE (result) == SSA_NAME)
3195 result = SSA_VAL (result);
3196 resultsame = expressions_equal_p (result, op);
3199 if ((!result || !resultsame)
3200 /* Only perform the following when being called from PRE
3201 which embeds tail merging. */
3202 && default_vn_walk_kind == VN_WALK)
3204 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3205 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
3206 if (vnresult)
3208 VN_INFO (vdef)->use_processed = true;
3209 return set_ssa_val_to (vdef, vnresult->result_vdef);
3213 if (!result || !resultsame)
3215 if (dump_file && (dump_flags & TDF_DETAILS))
3217 fprintf (dump_file, "No store match\n");
3218 fprintf (dump_file, "Value numbering store ");
3219 print_generic_expr (dump_file, lhs, 0);
3220 fprintf (dump_file, " to ");
3221 print_generic_expr (dump_file, op, 0);
3222 fprintf (dump_file, "\n");
3224 /* Have to set value numbers before insert, since insert is
3225 going to valueize the references in-place. */
3226 if (vdef)
3228 changed |= set_ssa_val_to (vdef, vdef);
3231 /* Do not insert structure copies into the tables. */
3232 if (is_gimple_min_invariant (op)
3233 || is_gimple_reg (op))
3234 vn_reference_insert (lhs, op, vdef, NULL);
3236 /* Only perform the following when being called from PRE
3237 which embeds tail merging. */
3238 if (default_vn_walk_kind == VN_WALK)
3240 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3241 vn_reference_insert (assign, lhs, vuse, vdef);
3244 else
3246 /* We had a match, so value number the vdef to have the value
3247 number of the vuse it came from. */
3249 if (dump_file && (dump_flags & TDF_DETAILS))
3250 fprintf (dump_file, "Store matched earlier value,"
3251 "value numbering store vdefs to matching vuses.\n");
3253 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3256 return changed;
3259 /* Visit and value number PHI, return true if the value number
3260 changed. */
3262 static bool
3263 visit_phi (gimple phi)
3265 bool changed = false;
3266 tree result;
3267 tree sameval = VN_TOP;
3268 bool allsame = true;
3270 /* TODO: We could check for this in init_sccvn, and replace this
3271 with a gcc_assert. */
3272 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3273 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3275 /* See if all non-TOP arguments have the same value. TOP is
3276 equivalent to everything, so we can ignore it. */
3277 edge_iterator ei;
3278 edge e;
3279 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3280 if (e->flags & EDGE_EXECUTABLE)
3282 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3284 if (TREE_CODE (def) == SSA_NAME)
3285 def = SSA_VAL (def);
3286 if (def == VN_TOP)
3287 continue;
3288 if (sameval == VN_TOP)
3290 sameval = def;
3292 else
3294 if (!expressions_equal_p (def, sameval))
3296 allsame = false;
3297 break;
3302 /* If all value numbered to the same value, the phi node has that
3303 value. */
3304 if (allsame)
3305 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3307 /* Otherwise, see if it is equivalent to a phi node in this block. */
3308 result = vn_phi_lookup (phi);
3309 if (result)
3310 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3311 else
3313 vn_phi_insert (phi, PHI_RESULT (phi));
3314 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3315 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3316 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3319 return changed;
3322 /* Return true if EXPR contains constants. */
3324 static bool
3325 expr_has_constants (tree expr)
3327 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3329 case tcc_unary:
3330 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3332 case tcc_binary:
3333 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3334 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3335 /* Constants inside reference ops are rarely interesting, but
3336 it can take a lot of looking to find them. */
3337 case tcc_reference:
3338 case tcc_declaration:
3339 return false;
3340 default:
3341 return is_gimple_min_invariant (expr);
3343 return false;
3346 /* Return true if STMT contains constants. */
3348 static bool
3349 stmt_has_constants (gimple stmt)
3351 tree tem;
3353 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3354 return false;
3356 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3358 case GIMPLE_TERNARY_RHS:
3359 tem = gimple_assign_rhs3 (stmt);
3360 if (TREE_CODE (tem) == SSA_NAME)
3361 tem = SSA_VAL (tem);
3362 if (is_gimple_min_invariant (tem))
3363 return true;
3364 /* Fallthru. */
3366 case GIMPLE_BINARY_RHS:
3367 tem = gimple_assign_rhs2 (stmt);
3368 if (TREE_CODE (tem) == SSA_NAME)
3369 tem = SSA_VAL (tem);
3370 if (is_gimple_min_invariant (tem))
3371 return true;
3372 /* Fallthru. */
3374 case GIMPLE_SINGLE_RHS:
3375 /* Constants inside reference ops are rarely interesting, but
3376 it can take a lot of looking to find them. */
3377 case GIMPLE_UNARY_RHS:
3378 tem = gimple_assign_rhs1 (stmt);
3379 if (TREE_CODE (tem) == SSA_NAME)
3380 tem = SSA_VAL (tem);
3381 return is_gimple_min_invariant (tem);
3383 default:
3384 gcc_unreachable ();
3386 return false;
3389 /* Simplify the binary expression RHS, and return the result if
3390 simplified. */
3392 static tree
3393 simplify_binary_expression (gimple stmt)
3395 tree result = NULL_TREE;
3396 tree op0 = gimple_assign_rhs1 (stmt);
3397 tree op1 = gimple_assign_rhs2 (stmt);
3398 enum tree_code code = gimple_assign_rhs_code (stmt);
3400 /* This will not catch every single case we could combine, but will
3401 catch those with constants. The goal here is to simultaneously
3402 combine constants between expressions, but avoid infinite
3403 expansion of expressions during simplification. */
3404 op0 = vn_valueize (op0);
3405 if (TREE_CODE (op0) == SSA_NAME
3406 && (VN_INFO (op0)->has_constants
3407 || TREE_CODE_CLASS (code) == tcc_comparison
3408 || code == COMPLEX_EXPR))
3409 op0 = vn_get_expr_for (op0);
3411 op1 = vn_valueize (op1);
3412 if (TREE_CODE (op1) == SSA_NAME
3413 && (VN_INFO (op1)->has_constants
3414 || code == COMPLEX_EXPR))
3415 op1 = vn_get_expr_for (op1);
3417 /* Pointer plus constant can be represented as invariant address.
3418 Do so to allow further propatation, see also tree forwprop. */
3419 if (code == POINTER_PLUS_EXPR
3420 && tree_fits_uhwi_p (op1)
3421 && TREE_CODE (op0) == ADDR_EXPR
3422 && is_gimple_min_invariant (op0))
3423 return build_invariant_address (TREE_TYPE (op0),
3424 TREE_OPERAND (op0, 0),
3425 tree_to_uhwi (op1));
3427 /* Avoid folding if nothing changed. */
3428 if (op0 == gimple_assign_rhs1 (stmt)
3429 && op1 == gimple_assign_rhs2 (stmt))
3430 return NULL_TREE;
3432 fold_defer_overflow_warnings ();
3434 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3435 if (result)
3436 STRIP_USELESS_TYPE_CONVERSION (result);
3438 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3439 stmt, 0);
3441 /* Make sure result is not a complex expression consisting
3442 of operators of operators (IE (a + b) + (a + c))
3443 Otherwise, we will end up with unbounded expressions if
3444 fold does anything at all. */
3445 if (result && valid_gimple_rhs_p (result))
3446 return result;
3448 return NULL_TREE;
3451 /* Simplify the unary expression RHS, and return the result if
3452 simplified. */
3454 static tree
3455 simplify_unary_expression (gassign *stmt)
3457 tree result = NULL_TREE;
3458 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3459 enum tree_code code = gimple_assign_rhs_code (stmt);
3461 /* We handle some tcc_reference codes here that are all
3462 GIMPLE_ASSIGN_SINGLE codes. */
3463 if (code == REALPART_EXPR
3464 || code == IMAGPART_EXPR
3465 || code == VIEW_CONVERT_EXPR
3466 || code == BIT_FIELD_REF)
3467 op0 = TREE_OPERAND (op0, 0);
3469 orig_op0 = op0;
3470 op0 = vn_valueize (op0);
3471 if (TREE_CODE (op0) == SSA_NAME)
3473 if (VN_INFO (op0)->has_constants)
3474 op0 = vn_get_expr_for (op0);
3475 else if (CONVERT_EXPR_CODE_P (code)
3476 || code == REALPART_EXPR
3477 || code == IMAGPART_EXPR
3478 || code == VIEW_CONVERT_EXPR
3479 || code == BIT_FIELD_REF)
3481 /* We want to do tree-combining on conversion-like expressions.
3482 Make sure we feed only SSA_NAMEs or constants to fold though. */
3483 tree tem = vn_get_expr_for (op0);
3484 if (UNARY_CLASS_P (tem)
3485 || BINARY_CLASS_P (tem)
3486 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3487 || TREE_CODE (tem) == SSA_NAME
3488 || TREE_CODE (tem) == CONSTRUCTOR
3489 || is_gimple_min_invariant (tem))
3490 op0 = tem;
3494 /* Avoid folding if nothing changed, but remember the expression. */
3495 if (op0 == orig_op0)
3496 return NULL_TREE;
3498 if (code == BIT_FIELD_REF)
3500 tree rhs = gimple_assign_rhs1 (stmt);
3501 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3502 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3504 else
3505 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3506 if (result)
3508 STRIP_USELESS_TYPE_CONVERSION (result);
3509 if (valid_gimple_rhs_p (result))
3510 return result;
3513 return NULL_TREE;
3516 /* Try to simplify RHS using equivalences and constant folding. */
3518 static tree
3519 try_to_simplify (gassign *stmt)
3521 enum tree_code code = gimple_assign_rhs_code (stmt);
3522 tree tem;
3524 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3525 in this case, there is no point in doing extra work. */
3526 if (code == SSA_NAME)
3527 return NULL_TREE;
3529 /* First try constant folding based on our current lattice. */
3530 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
3531 if (tem
3532 && (TREE_CODE (tem) == SSA_NAME
3533 || is_gimple_min_invariant (tem)))
3534 return tem;
3536 /* If that didn't work try combining multiple statements. */
3537 switch (TREE_CODE_CLASS (code))
3539 case tcc_reference:
3540 /* Fallthrough for some unary codes that can operate on registers. */
3541 if (!(code == REALPART_EXPR
3542 || code == IMAGPART_EXPR
3543 || code == VIEW_CONVERT_EXPR
3544 || code == BIT_FIELD_REF))
3545 break;
3546 /* We could do a little more with unary ops, if they expand
3547 into binary ops, but it's debatable whether it is worth it. */
3548 case tcc_unary:
3549 return simplify_unary_expression (stmt);
3551 case tcc_comparison:
3552 case tcc_binary:
3553 return simplify_binary_expression (stmt);
3555 default:
3556 break;
3559 return NULL_TREE;
3562 /* Visit and value number USE, return true if the value number
3563 changed. */
3565 static bool
3566 visit_use (tree use)
3568 bool changed = false;
3569 gimple stmt = SSA_NAME_DEF_STMT (use);
3571 mark_use_processed (use);
3573 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3574 if (dump_file && (dump_flags & TDF_DETAILS)
3575 && !SSA_NAME_IS_DEFAULT_DEF (use))
3577 fprintf (dump_file, "Value numbering ");
3578 print_generic_expr (dump_file, use, 0);
3579 fprintf (dump_file, " stmt = ");
3580 print_gimple_stmt (dump_file, stmt, 0, 0);
3583 /* Handle uninitialized uses. */
3584 if (SSA_NAME_IS_DEFAULT_DEF (use))
3585 changed = set_ssa_val_to (use, use);
3586 else
3588 if (gimple_code (stmt) == GIMPLE_PHI)
3589 changed = visit_phi (stmt);
3590 else if (gimple_has_volatile_ops (stmt))
3591 changed = defs_to_varying (stmt);
3592 else if (is_gimple_assign (stmt))
3594 enum tree_code code = gimple_assign_rhs_code (stmt);
3595 tree lhs = gimple_assign_lhs (stmt);
3596 tree rhs1 = gimple_assign_rhs1 (stmt);
3597 tree simplified;
3599 /* Shortcut for copies. Simplifying copies is pointless,
3600 since we copy the expression and value they represent. */
3601 if (code == SSA_NAME
3602 && TREE_CODE (lhs) == SSA_NAME)
3604 changed = visit_copy (lhs, rhs1);
3605 goto done;
3607 simplified = try_to_simplify (as_a <gassign *> (stmt));
3608 if (simplified)
3610 if (dump_file && (dump_flags & TDF_DETAILS))
3612 fprintf (dump_file, "RHS ");
3613 print_gimple_expr (dump_file, stmt, 0, 0);
3614 fprintf (dump_file, " simplified to ");
3615 print_generic_expr (dump_file, simplified, 0);
3616 if (TREE_CODE (lhs) == SSA_NAME)
3617 fprintf (dump_file, " has constants %d\n",
3618 expr_has_constants (simplified));
3619 else
3620 fprintf (dump_file, "\n");
3623 /* Setting value numbers to constants will occasionally
3624 screw up phi congruence because constants are not
3625 uniquely associated with a single ssa name that can be
3626 looked up. */
3627 if (simplified
3628 && is_gimple_min_invariant (simplified)
3629 && TREE_CODE (lhs) == SSA_NAME)
3631 VN_INFO (lhs)->expr = simplified;
3632 VN_INFO (lhs)->has_constants = true;
3633 changed = set_ssa_val_to (lhs, simplified);
3634 goto done;
3636 else if (simplified
3637 && TREE_CODE (simplified) == SSA_NAME
3638 && TREE_CODE (lhs) == SSA_NAME)
3640 changed = visit_copy (lhs, simplified);
3641 goto done;
3643 else if (simplified)
3645 if (TREE_CODE (lhs) == SSA_NAME)
3647 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3648 /* We have to unshare the expression or else
3649 valuizing may change the IL stream. */
3650 VN_INFO (lhs)->expr = unshare_expr (simplified);
3653 else if (stmt_has_constants (stmt)
3654 && TREE_CODE (lhs) == SSA_NAME)
3655 VN_INFO (lhs)->has_constants = true;
3656 else if (TREE_CODE (lhs) == SSA_NAME)
3658 /* We reset expr and constantness here because we may
3659 have been value numbering optimistically, and
3660 iterating. They may become non-constant in this case,
3661 even if they were optimistically constant. */
3663 VN_INFO (lhs)->has_constants = false;
3664 VN_INFO (lhs)->expr = NULL_TREE;
3667 if ((TREE_CODE (lhs) == SSA_NAME
3668 /* We can substitute SSA_NAMEs that are live over
3669 abnormal edges with their constant value. */
3670 && !(gimple_assign_copy_p (stmt)
3671 && is_gimple_min_invariant (rhs1))
3672 && !(simplified
3673 && is_gimple_min_invariant (simplified))
3674 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3675 /* Stores or copies from SSA_NAMEs that are live over
3676 abnormal edges are a problem. */
3677 || (code == SSA_NAME
3678 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3679 changed = defs_to_varying (stmt);
3680 else if (REFERENCE_CLASS_P (lhs)
3681 || DECL_P (lhs))
3682 changed = visit_reference_op_store (lhs, rhs1, stmt);
3683 else if (TREE_CODE (lhs) == SSA_NAME)
3685 if ((gimple_assign_copy_p (stmt)
3686 && is_gimple_min_invariant (rhs1))
3687 || (simplified
3688 && is_gimple_min_invariant (simplified)))
3690 VN_INFO (lhs)->has_constants = true;
3691 if (simplified)
3692 changed = set_ssa_val_to (lhs, simplified);
3693 else
3694 changed = set_ssa_val_to (lhs, rhs1);
3696 else
3698 /* First try to lookup the simplified expression. */
3699 if (simplified)
3701 enum gimple_rhs_class rhs_class;
3704 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3705 if ((rhs_class == GIMPLE_UNARY_RHS
3706 || rhs_class == GIMPLE_BINARY_RHS
3707 || rhs_class == GIMPLE_TERNARY_RHS)
3708 && valid_gimple_rhs_p (simplified))
3710 tree result = vn_nary_op_lookup (simplified, NULL);
3711 if (result)
3713 changed = set_ssa_val_to (lhs, result);
3714 goto done;
3719 /* Otherwise visit the original statement. */
3720 switch (vn_get_stmt_kind (stmt))
3722 case VN_NARY:
3723 changed = visit_nary_op (lhs, stmt);
3724 break;
3725 case VN_REFERENCE:
3726 changed = visit_reference_op_load (lhs, rhs1, stmt);
3727 break;
3728 default:
3729 changed = defs_to_varying (stmt);
3730 break;
3734 else
3735 changed = defs_to_varying (stmt);
3737 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
3739 tree lhs = gimple_call_lhs (stmt);
3740 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3742 /* Try constant folding based on our current lattice. */
3743 tree simplified = gimple_fold_stmt_to_constant_1 (stmt,
3744 vn_valueize);
3745 if (simplified)
3747 if (dump_file && (dump_flags & TDF_DETAILS))
3749 fprintf (dump_file, "call ");
3750 print_gimple_expr (dump_file, stmt, 0, 0);
3751 fprintf (dump_file, " simplified to ");
3752 print_generic_expr (dump_file, simplified, 0);
3753 if (TREE_CODE (lhs) == SSA_NAME)
3754 fprintf (dump_file, " has constants %d\n",
3755 expr_has_constants (simplified));
3756 else
3757 fprintf (dump_file, "\n");
3760 /* Setting value numbers to constants will occasionally
3761 screw up phi congruence because constants are not
3762 uniquely associated with a single ssa name that can be
3763 looked up. */
3764 if (simplified
3765 && is_gimple_min_invariant (simplified))
3767 VN_INFO (lhs)->expr = simplified;
3768 VN_INFO (lhs)->has_constants = true;
3769 changed = set_ssa_val_to (lhs, simplified);
3770 if (gimple_vdef (stmt))
3771 changed |= set_ssa_val_to (gimple_vdef (stmt),
3772 SSA_VAL (gimple_vuse (stmt)));
3773 goto done;
3775 else if (simplified
3776 && TREE_CODE (simplified) == SSA_NAME)
3778 changed = visit_copy (lhs, simplified);
3779 if (gimple_vdef (stmt))
3780 changed |= set_ssa_val_to (gimple_vdef (stmt),
3781 SSA_VAL (gimple_vuse (stmt)));
3782 goto done;
3784 else
3786 if (stmt_has_constants (stmt))
3787 VN_INFO (lhs)->has_constants = true;
3788 else
3790 /* We reset expr and constantness here because we may
3791 have been value numbering optimistically, and
3792 iterating. They may become non-constant in this case,
3793 even if they were optimistically constant. */
3794 VN_INFO (lhs)->has_constants = false;
3795 VN_INFO (lhs)->expr = NULL_TREE;
3798 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3800 changed = defs_to_varying (stmt);
3801 goto done;
3806 if (!gimple_call_internal_p (stmt)
3807 && (/* Calls to the same function with the same vuse
3808 and the same operands do not necessarily return the same
3809 value, unless they're pure or const. */
3810 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3811 /* If calls have a vdef, subsequent calls won't have
3812 the same incoming vuse. So, if 2 calls with vdef have the
3813 same vuse, we know they're not subsequent.
3814 We can value number 2 calls to the same function with the
3815 same vuse and the same operands which are not subsequent
3816 the same, because there is no code in the program that can
3817 compare the 2 values... */
3818 || (gimple_vdef (stmt)
3819 /* ... unless the call returns a pointer which does
3820 not alias with anything else. In which case the
3821 information that the values are distinct are encoded
3822 in the IL. */
3823 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
3824 /* Only perform the following when being called from PRE
3825 which embeds tail merging. */
3826 && default_vn_walk_kind == VN_WALK)))
3827 changed = visit_reference_op_call (lhs, call_stmt);
3828 else
3829 changed = defs_to_varying (stmt);
3831 else
3832 changed = defs_to_varying (stmt);
3834 done:
3835 return changed;
3838 /* Compare two operands by reverse postorder index */
3840 static int
3841 compare_ops (const void *pa, const void *pb)
3843 const tree opa = *((const tree *)pa);
3844 const tree opb = *((const tree *)pb);
3845 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3846 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3847 basic_block bba;
3848 basic_block bbb;
3850 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3851 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3852 else if (gimple_nop_p (opstmta))
3853 return -1;
3854 else if (gimple_nop_p (opstmtb))
3855 return 1;
3857 bba = gimple_bb (opstmta);
3858 bbb = gimple_bb (opstmtb);
3860 if (!bba && !bbb)
3861 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3862 else if (!bba)
3863 return -1;
3864 else if (!bbb)
3865 return 1;
3867 if (bba == bbb)
3869 if (gimple_code (opstmta) == GIMPLE_PHI
3870 && gimple_code (opstmtb) == GIMPLE_PHI)
3871 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3872 else if (gimple_code (opstmta) == GIMPLE_PHI)
3873 return -1;
3874 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3875 return 1;
3876 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3877 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3878 else
3879 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3881 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3884 /* Sort an array containing members of a strongly connected component
3885 SCC so that the members are ordered by RPO number.
3886 This means that when the sort is complete, iterating through the
3887 array will give you the members in RPO order. */
3889 static void
3890 sort_scc (vec<tree> scc)
3892 scc.qsort (compare_ops);
3895 /* Insert the no longer used nary ONARY to the hash INFO. */
3897 static void
3898 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3900 size_t size = sizeof_vn_nary_op (onary->length);
3901 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3902 &info->nary_obstack);
3903 memcpy (nary, onary, size);
3904 vn_nary_op_insert_into (nary, info->nary, false);
3907 /* Insert the no longer used phi OPHI to the hash INFO. */
3909 static void
3910 copy_phi (vn_phi_t ophi, vn_tables_t info)
3912 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3913 vn_phi_s **slot;
3914 memcpy (phi, ophi, sizeof (*phi));
3915 ophi->phiargs.create (0);
3916 slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT);
3917 gcc_assert (!*slot);
3918 *slot = phi;
3921 /* Insert the no longer used reference OREF to the hash INFO. */
3923 static void
3924 copy_reference (vn_reference_t oref, vn_tables_t info)
3926 vn_reference_t ref;
3927 vn_reference_s **slot;
3928 ref = (vn_reference_t) pool_alloc (info->references_pool);
3929 memcpy (ref, oref, sizeof (*ref));
3930 oref->operands.create (0);
3931 slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT);
3932 if (*slot)
3933 free_reference (*slot);
3934 *slot = ref;
3937 /* Process a strongly connected component in the SSA graph. */
3939 static void
3940 process_scc (vec<tree> scc)
3942 tree var;
3943 unsigned int i;
3944 unsigned int iterations = 0;
3945 bool changed = true;
3946 vn_nary_op_iterator_type hin;
3947 vn_phi_iterator_type hip;
3948 vn_reference_iterator_type hir;
3949 vn_nary_op_t nary;
3950 vn_phi_t phi;
3951 vn_reference_t ref;
3953 /* If the SCC has a single member, just visit it. */
3954 if (scc.length () == 1)
3956 tree use = scc[0];
3957 if (VN_INFO (use)->use_processed)
3958 return;
3959 /* We need to make sure it doesn't form a cycle itself, which can
3960 happen for self-referential PHI nodes. In that case we would
3961 end up inserting an expression with VN_TOP operands into the
3962 valid table which makes us derive bogus equivalences later.
3963 The cheapest way to check this is to assume it for all PHI nodes. */
3964 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3965 /* Fallthru to iteration. */ ;
3966 else
3968 visit_use (use);
3969 return;
3973 if (dump_file && (dump_flags & TDF_DETAILS))
3974 print_scc (dump_file, scc);
3976 /* Iterate over the SCC with the optimistic table until it stops
3977 changing. */
3978 current_info = optimistic_info;
3979 while (changed)
3981 changed = false;
3982 iterations++;
3983 if (dump_file && (dump_flags & TDF_DETAILS))
3984 fprintf (dump_file, "Starting iteration %d\n", iterations);
3985 /* As we are value-numbering optimistically we have to
3986 clear the expression tables and the simplified expressions
3987 in each iteration until we converge. */
3988 optimistic_info->nary->empty ();
3989 optimistic_info->phis->empty ();
3990 optimistic_info->references->empty ();
3991 obstack_free (&optimistic_info->nary_obstack, NULL);
3992 gcc_obstack_init (&optimistic_info->nary_obstack);
3993 empty_alloc_pool (optimistic_info->phis_pool);
3994 empty_alloc_pool (optimistic_info->references_pool);
3995 FOR_EACH_VEC_ELT (scc, i, var)
3996 VN_INFO (var)->expr = NULL_TREE;
3997 FOR_EACH_VEC_ELT (scc, i, var)
3998 changed |= visit_use (var);
4001 if (dump_file && (dump_flags & TDF_DETAILS))
4002 fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations);
4003 statistics_histogram_event (cfun, "SCC iterations", iterations);
4005 /* Finally, copy the contents of the no longer used optimistic
4006 table to the valid table. */
4007 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin)
4008 copy_nary (nary, valid_info);
4009 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip)
4010 copy_phi (phi, valid_info);
4011 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references,
4012 ref, vn_reference_t, hir)
4013 copy_reference (ref, valid_info);
4015 current_info = valid_info;
4019 /* Pop the components of the found SCC for NAME off the SCC stack
4020 and process them. Returns true if all went well, false if
4021 we run into resource limits. */
4023 static bool
4024 extract_and_process_scc_for_name (tree name)
4026 auto_vec<tree> scc;
4027 tree x;
4029 /* Found an SCC, pop the components off the SCC stack and
4030 process them. */
4033 x = sccstack.pop ();
4035 VN_INFO (x)->on_sccstack = false;
4036 scc.safe_push (x);
4037 } while (x != name);
4039 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
4040 if (scc.length ()
4041 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
4043 if (dump_file)
4044 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
4045 "SCC size %u exceeding %u\n", scc.length (),
4046 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
4048 return false;
4051 if (scc.length () > 1)
4052 sort_scc (scc);
4054 process_scc (scc);
4056 return true;
4059 /* Depth first search on NAME to discover and process SCC's in the SSA
4060 graph.
4061 Execution of this algorithm relies on the fact that the SCC's are
4062 popped off the stack in topological order.
4063 Returns true if successful, false if we stopped processing SCC's due
4064 to resource constraints. */
4066 static bool
4067 DFS (tree name)
4069 vec<ssa_op_iter> itervec = vNULL;
4070 vec<tree> namevec = vNULL;
4071 use_operand_p usep = NULL;
4072 gimple defstmt;
4073 tree use;
4074 ssa_op_iter iter;
4076 start_over:
4077 /* SCC info */
4078 VN_INFO (name)->dfsnum = next_dfs_num++;
4079 VN_INFO (name)->visited = true;
4080 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
4082 sccstack.safe_push (name);
4083 VN_INFO (name)->on_sccstack = true;
4084 defstmt = SSA_NAME_DEF_STMT (name);
4086 /* Recursively DFS on our operands, looking for SCC's. */
4087 if (!gimple_nop_p (defstmt))
4089 /* Push a new iterator. */
4090 if (gphi *phi = dyn_cast <gphi *> (defstmt))
4091 usep = op_iter_init_phiuse (&iter, phi, SSA_OP_ALL_USES);
4092 else
4093 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
4095 else
4096 clear_and_done_ssa_iter (&iter);
4098 while (1)
4100 /* If we are done processing uses of a name, go up the stack
4101 of iterators and process SCCs as we found them. */
4102 if (op_iter_done (&iter))
4104 /* See if we found an SCC. */
4105 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
4106 if (!extract_and_process_scc_for_name (name))
4108 namevec.release ();
4109 itervec.release ();
4110 return false;
4113 /* Check if we are done. */
4114 if (namevec.is_empty ())
4116 namevec.release ();
4117 itervec.release ();
4118 return true;
4121 /* Restore the last use walker and continue walking there. */
4122 use = name;
4123 name = namevec.pop ();
4124 memcpy (&iter, &itervec.last (),
4125 sizeof (ssa_op_iter));
4126 itervec.pop ();
4127 goto continue_walking;
4130 use = USE_FROM_PTR (usep);
4132 /* Since we handle phi nodes, we will sometimes get
4133 invariants in the use expression. */
4134 if (TREE_CODE (use) == SSA_NAME)
4136 if (! (VN_INFO (use)->visited))
4138 /* Recurse by pushing the current use walking state on
4139 the stack and starting over. */
4140 itervec.safe_push (iter);
4141 namevec.safe_push (name);
4142 name = use;
4143 goto start_over;
4145 continue_walking:
4146 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
4147 VN_INFO (use)->low);
4149 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
4150 && VN_INFO (use)->on_sccstack)
4152 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
4153 VN_INFO (name)->low);
4157 usep = op_iter_next_use (&iter);
4161 /* Allocate a value number table. */
4163 static void
4164 allocate_vn_table (vn_tables_t table)
4166 table->phis = new vn_phi_table_type (23);
4167 table->nary = new vn_nary_op_table_type (23);
4168 table->references = new vn_reference_table_type (23);
4170 gcc_obstack_init (&table->nary_obstack);
4171 table->phis_pool = create_alloc_pool ("VN phis",
4172 sizeof (struct vn_phi_s),
4173 30);
4174 table->references_pool = create_alloc_pool ("VN references",
4175 sizeof (struct vn_reference_s),
4176 30);
4179 /* Free a value number table. */
4181 static void
4182 free_vn_table (vn_tables_t table)
4184 delete table->phis;
4185 table->phis = NULL;
4186 delete table->nary;
4187 table->nary = NULL;
4188 delete table->references;
4189 table->references = NULL;
4190 obstack_free (&table->nary_obstack, NULL);
4191 free_alloc_pool (table->phis_pool);
4192 free_alloc_pool (table->references_pool);
4195 static void
4196 init_scc_vn (void)
4198 size_t i;
4199 int j;
4200 int *rpo_numbers_temp;
4202 calculate_dominance_info (CDI_DOMINATORS);
4203 sccstack.create (0);
4204 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
4206 constant_value_ids = BITMAP_ALLOC (NULL);
4208 next_dfs_num = 1;
4209 next_value_id = 1;
4211 vn_ssa_aux_table.create (num_ssa_names + 1);
4212 /* VEC_alloc doesn't actually grow it to the right size, it just
4213 preallocates the space to do so. */
4214 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
4215 gcc_obstack_init (&vn_ssa_aux_obstack);
4217 shared_lookup_phiargs.create (0);
4218 shared_lookup_references.create (0);
4219 rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun));
4220 rpo_numbers_temp =
4221 XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4222 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
4224 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
4225 the i'th block in RPO order is bb. We want to map bb's to RPO
4226 numbers, so we need to rearrange this array. */
4227 for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++)
4228 rpo_numbers[rpo_numbers_temp[j]] = j;
4230 XDELETE (rpo_numbers_temp);
4232 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
4234 /* Create the VN_INFO structures, and initialize value numbers to
4235 TOP. */
4236 for (i = 0; i < num_ssa_names; i++)
4238 tree name = ssa_name (i);
4239 if (name)
4241 VN_INFO_GET (name)->valnum = VN_TOP;
4242 VN_INFO (name)->expr = NULL_TREE;
4243 VN_INFO (name)->value_id = 0;
4247 renumber_gimple_stmt_uids ();
4249 /* Create the valid and optimistic value numbering tables. */
4250 valid_info = XCNEW (struct vn_tables_s);
4251 allocate_vn_table (valid_info);
4252 optimistic_info = XCNEW (struct vn_tables_s);
4253 allocate_vn_table (optimistic_info);
4256 void
4257 free_scc_vn (void)
4259 size_t i;
4261 delete constant_to_value_id;
4262 constant_to_value_id = NULL;
4263 BITMAP_FREE (constant_value_ids);
4264 shared_lookup_phiargs.release ();
4265 shared_lookup_references.release ();
4266 XDELETEVEC (rpo_numbers);
4268 for (i = 0; i < num_ssa_names; i++)
4270 tree name = ssa_name (i);
4271 if (name
4272 && VN_INFO (name)->needs_insertion)
4273 release_ssa_name (name);
4275 obstack_free (&vn_ssa_aux_obstack, NULL);
4276 vn_ssa_aux_table.release ();
4278 sccstack.release ();
4279 free_vn_table (valid_info);
4280 XDELETE (valid_info);
4281 free_vn_table (optimistic_info);
4282 XDELETE (optimistic_info);
4285 /* Set *ID according to RESULT. */
4287 static void
4288 set_value_id_for_result (tree result, unsigned int *id)
4290 if (result && TREE_CODE (result) == SSA_NAME)
4291 *id = VN_INFO (result)->value_id;
4292 else if (result && is_gimple_min_invariant (result))
4293 *id = get_or_alloc_constant_value_id (result);
4294 else
4295 *id = get_next_value_id ();
4298 /* Set the value ids in the valid hash tables. */
4300 static void
4301 set_hashtable_value_ids (void)
4303 vn_nary_op_iterator_type hin;
4304 vn_phi_iterator_type hip;
4305 vn_reference_iterator_type hir;
4306 vn_nary_op_t vno;
4307 vn_reference_t vr;
4308 vn_phi_t vp;
4310 /* Now set the value ids of the things we had put in the hash
4311 table. */
4313 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4314 set_value_id_for_result (vno->result, &vno->value_id);
4316 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4317 set_value_id_for_result (vp->result, &vp->value_id);
4319 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4320 hir)
4321 set_value_id_for_result (vr->result, &vr->value_id);
4324 class cond_dom_walker : public dom_walker
4326 public:
4327 cond_dom_walker () : dom_walker (CDI_DOMINATORS), fail (false) {}
4329 virtual void before_dom_children (basic_block);
4331 bool fail;
4334 void
4335 cond_dom_walker::before_dom_children (basic_block bb)
4337 edge e;
4338 edge_iterator ei;
4340 if (fail)
4341 return;
4343 /* If any of the predecessor edges that do not come from blocks dominated
4344 by us are still marked as possibly executable consider this block
4345 reachable. */
4346 bool reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (cfun);
4347 FOR_EACH_EDGE (e, ei, bb->preds)
4348 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
4349 reachable |= (e->flags & EDGE_EXECUTABLE);
4351 /* If the block is not reachable all outgoing edges are not
4352 executable. */
4353 if (!reachable)
4355 if (dump_file && (dump_flags & TDF_DETAILS))
4356 fprintf (dump_file, "Marking all outgoing edges of unreachable "
4357 "BB %d as not executable\n", bb->index);
4359 FOR_EACH_EDGE (e, ei, bb->succs)
4360 e->flags &= ~EDGE_EXECUTABLE;
4361 return;
4364 gimple stmt = last_stmt (bb);
4365 if (!stmt)
4366 return;
4368 enum gimple_code code = gimple_code (stmt);
4369 if (code != GIMPLE_COND
4370 && code != GIMPLE_SWITCH
4371 && code != GIMPLE_GOTO)
4372 return;
4374 if (dump_file && (dump_flags & TDF_DETAILS))
4376 fprintf (dump_file, "Value-numbering operands of stmt ending BB %d: ",
4377 bb->index);
4378 print_gimple_stmt (dump_file, stmt, 0, 0);
4381 /* Value-number the last stmts SSA uses. */
4382 ssa_op_iter i;
4383 tree op;
4384 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4385 if (VN_INFO (op)->visited == false
4386 && !DFS (op))
4388 fail = true;
4389 return;
4392 /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges
4393 if value-numbering can prove they are not reachable. Handling
4394 computed gotos is also possible. */
4395 tree val;
4396 switch (code)
4398 case GIMPLE_COND:
4400 tree lhs = gimple_cond_lhs (stmt);
4401 tree rhs = gimple_cond_rhs (stmt);
4402 /* Work hard in computing the condition and take into account
4403 the valueization of the defining stmt. */
4404 if (TREE_CODE (lhs) == SSA_NAME)
4405 lhs = vn_get_expr_for (lhs);
4406 if (TREE_CODE (rhs) == SSA_NAME)
4407 rhs = vn_get_expr_for (rhs);
4408 val = fold_binary (gimple_cond_code (stmt),
4409 boolean_type_node, lhs, rhs);
4410 break;
4412 case GIMPLE_SWITCH:
4413 val = gimple_switch_index (as_a <gswitch *> (stmt));
4414 break;
4415 case GIMPLE_GOTO:
4416 val = gimple_goto_dest (stmt);
4417 break;
4418 default:
4419 gcc_unreachable ();
4421 if (!val)
4422 return;
4424 edge taken = find_taken_edge (bb, vn_valueize (val));
4425 if (!taken)
4426 return;
4428 if (dump_file && (dump_flags & TDF_DETAILS))
4429 fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as "
4430 "not executable\n", bb->index, bb->index, taken->dest->index);
4432 FOR_EACH_EDGE (e, ei, bb->succs)
4433 if (e != taken)
4434 e->flags &= ~EDGE_EXECUTABLE;
4437 /* Do SCCVN. Returns true if it finished, false if we bailed out
4438 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4439 how we use the alias oracle walking during the VN process. */
4441 bool
4442 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4444 basic_block bb;
4445 size_t i;
4446 tree param;
4448 default_vn_walk_kind = default_vn_walk_kind_;
4450 init_scc_vn ();
4451 current_info = valid_info;
4453 for (param = DECL_ARGUMENTS (current_function_decl);
4454 param;
4455 param = DECL_CHAIN (param))
4457 tree def = ssa_default_def (cfun, param);
4458 if (def)
4460 VN_INFO (def)->visited = true;
4461 VN_INFO (def)->valnum = def;
4465 /* Mark all edges as possibly executable. */
4466 FOR_ALL_BB_FN (bb, cfun)
4468 edge_iterator ei;
4469 edge e;
4470 FOR_EACH_EDGE (e, ei, bb->succs)
4471 e->flags |= EDGE_EXECUTABLE;
4474 /* Walk all blocks in dominator order, value-numbering the last stmts
4475 SSA uses and decide whether outgoing edges are not executable. */
4476 cond_dom_walker walker;
4477 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4478 if (walker.fail)
4480 free_scc_vn ();
4481 return false;
4484 /* Value-number remaining SSA names. */
4485 for (i = 1; i < num_ssa_names; ++i)
4487 tree name = ssa_name (i);
4488 if (name
4489 && VN_INFO (name)->visited == false
4490 && !has_zero_uses (name))
4491 if (!DFS (name))
4493 free_scc_vn ();
4494 return false;
4498 /* Initialize the value ids. */
4500 for (i = 1; i < num_ssa_names; ++i)
4502 tree name = ssa_name (i);
4503 vn_ssa_aux_t info;
4504 if (!name)
4505 continue;
4506 info = VN_INFO (name);
4507 if (info->valnum == name
4508 || info->valnum == VN_TOP)
4509 info->value_id = get_next_value_id ();
4510 else if (is_gimple_min_invariant (info->valnum))
4511 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4514 /* Propagate. */
4515 for (i = 1; i < num_ssa_names; ++i)
4517 tree name = ssa_name (i);
4518 vn_ssa_aux_t info;
4519 if (!name)
4520 continue;
4521 info = VN_INFO (name);
4522 if (TREE_CODE (info->valnum) == SSA_NAME
4523 && info->valnum != name
4524 && info->value_id != VN_INFO (info->valnum)->value_id)
4525 info->value_id = VN_INFO (info->valnum)->value_id;
4528 set_hashtable_value_ids ();
4530 if (dump_file && (dump_flags & TDF_DETAILS))
4532 fprintf (dump_file, "Value numbers:\n");
4533 for (i = 0; i < num_ssa_names; i++)
4535 tree name = ssa_name (i);
4536 if (name
4537 && VN_INFO (name)->visited
4538 && SSA_VAL (name) != name)
4540 print_generic_expr (dump_file, name, 0);
4541 fprintf (dump_file, " = ");
4542 print_generic_expr (dump_file, SSA_VAL (name), 0);
4543 fprintf (dump_file, "\n");
4548 return true;
4551 /* Return the maximum value id we have ever seen. */
4553 unsigned int
4554 get_max_value_id (void)
4556 return next_value_id;
4559 /* Return the next unique value id. */
4561 unsigned int
4562 get_next_value_id (void)
4564 return next_value_id++;
4568 /* Compare two expressions E1 and E2 and return true if they are equal. */
4570 bool
4571 expressions_equal_p (tree e1, tree e2)
4573 /* The obvious case. */
4574 if (e1 == e2)
4575 return true;
4577 /* If only one of them is null, they cannot be equal. */
4578 if (!e1 || !e2)
4579 return false;
4581 /* Now perform the actual comparison. */
4582 if (TREE_CODE (e1) == TREE_CODE (e2)
4583 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4584 return true;
4586 return false;
4590 /* Return true if the nary operation NARY may trap. This is a copy
4591 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4593 bool
4594 vn_nary_may_trap (vn_nary_op_t nary)
4596 tree type;
4597 tree rhs2 = NULL_TREE;
4598 bool honor_nans = false;
4599 bool honor_snans = false;
4600 bool fp_operation = false;
4601 bool honor_trapv = false;
4602 bool handled, ret;
4603 unsigned i;
4605 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4606 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4607 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4609 type = nary->type;
4610 fp_operation = FLOAT_TYPE_P (type);
4611 if (fp_operation)
4613 honor_nans = flag_trapping_math && !flag_finite_math_only;
4614 honor_snans = flag_signaling_nans != 0;
4616 else if (INTEGRAL_TYPE_P (type)
4617 && TYPE_OVERFLOW_TRAPS (type))
4618 honor_trapv = true;
4620 if (nary->length >= 2)
4621 rhs2 = nary->op[1];
4622 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4623 honor_trapv,
4624 honor_nans, honor_snans, rhs2,
4625 &handled);
4626 if (handled
4627 && ret)
4628 return true;
4630 for (i = 0; i < nary->length; ++i)
4631 if (tree_could_trap_p (nary->op[i]))
4632 return true;
4634 return false;