* inclhack.def (aix_externc): New fix.
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blobc6a16bc87902dc705fbf23150408850ae0e91a2a
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "predict.h"
38 #include "hard-reg-set.h"
39 #include "function.h"
40 #include "dominance.h"
41 #include "cfg.h"
42 #include "cfganal.h"
43 #include "basic-block.h"
44 #include "gimple-pretty-print.h"
45 #include "tree-inline.h"
46 #include "hash-table.h"
47 #include "tree-ssa-alias.h"
48 #include "internal-fn.h"
49 #include "gimple-fold.h"
50 #include "tree-eh.h"
51 #include "gimple-expr.h"
52 #include "is-a.h"
53 #include "gimple.h"
54 #include "gimplify.h"
55 #include "gimple-ssa.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
58 #include "stringpool.h"
59 #include "tree-ssanames.h"
60 #include "hashtab.h"
61 #include "rtl.h"
62 #include "flags.h"
63 #include "statistics.h"
64 #include "real.h"
65 #include "fixed-value.h"
66 #include "insn-config.h"
67 #include "expmed.h"
68 #include "dojump.h"
69 #include "explow.h"
70 #include "calls.h"
71 #include "emit-rtl.h"
72 #include "varasm.h"
73 #include "stmt.h"
74 #include "expr.h"
75 #include "tree-dfa.h"
76 #include "tree-ssa.h"
77 #include "dumpfile.h"
78 #include "alloc-pool.h"
79 #include "cfgloop.h"
80 #include "params.h"
81 #include "tree-ssa-propagate.h"
82 #include "tree-ssa-sccvn.h"
83 #include "tree-cfg.h"
84 #include "domwalk.h"
85 #include "ipa-ref.h"
86 #include "plugin-api.h"
87 #include "cgraph.h"
89 /* This algorithm is based on the SCC algorithm presented by Keith
90 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
91 (http://citeseer.ist.psu.edu/41805.html). In
92 straight line code, it is equivalent to a regular hash based value
93 numbering that is performed in reverse postorder.
95 For code with cycles, there are two alternatives, both of which
96 require keeping the hashtables separate from the actual list of
97 value numbers for SSA names.
99 1. Iterate value numbering in an RPO walk of the blocks, removing
100 all the entries from the hashtable after each iteration (but
101 keeping the SSA name->value number mapping between iterations).
102 Iterate until it does not change.
104 2. Perform value numbering as part of an SCC walk on the SSA graph,
105 iterating only the cycles in the SSA graph until they do not change
106 (using a separate, optimistic hashtable for value numbering the SCC
107 operands).
109 The second is not just faster in practice (because most SSA graph
110 cycles do not involve all the variables in the graph), it also has
111 some nice properties.
113 One of these nice properties is that when we pop an SCC off the
114 stack, we are guaranteed to have processed all the operands coming from
115 *outside of that SCC*, so we do not need to do anything special to
116 ensure they have value numbers.
118 Another nice property is that the SCC walk is done as part of a DFS
119 of the SSA graph, which makes it easy to perform combining and
120 simplifying operations at the same time.
122 The code below is deliberately written in a way that makes it easy
123 to separate the SCC walk from the other work it does.
125 In order to propagate constants through the code, we track which
126 expressions contain constants, and use those while folding. In
127 theory, we could also track expressions whose value numbers are
128 replaced, in case we end up folding based on expression
129 identities.
131 In order to value number memory, we assign value numbers to vuses.
132 This enables us to note that, for example, stores to the same
133 address of the same value from the same starting memory states are
134 equivalent.
135 TODO:
137 1. We can iterate only the changing portions of the SCC's, but
138 I have not seen an SCC big enough for this to be a win.
139 2. If you differentiate between phi nodes for loops and phi nodes
140 for if-then-else, you can properly consider phi nodes in different
141 blocks for equivalence.
142 3. We could value number vuses in more cases, particularly, whole
143 structure copies.
147 /* vn_nary_op hashtable helpers. */
149 struct vn_nary_op_hasher : typed_noop_remove <vn_nary_op_s>
151 typedef vn_nary_op_s *value_type;
152 typedef vn_nary_op_s *compare_type;
153 static inline hashval_t hash (const vn_nary_op_s *);
154 static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *);
157 /* Return the computed hashcode for nary operation P1. */
159 inline hashval_t
160 vn_nary_op_hasher::hash (const vn_nary_op_s *vno1)
162 return vno1->hashcode;
165 /* Compare nary operations P1 and P2 and return true if they are
166 equivalent. */
168 inline bool
169 vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2)
171 return vn_nary_op_eq (vno1, vno2);
174 typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
175 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
178 /* vn_phi hashtable helpers. */
180 static int
181 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
183 struct vn_phi_hasher
185 typedef vn_phi_s *value_type;
186 typedef vn_phi_s *compare_type;
187 static inline hashval_t hash (const vn_phi_s *);
188 static inline bool equal (const vn_phi_s *, const vn_phi_s *);
189 static inline void remove (vn_phi_s *);
192 /* Return the computed hashcode for phi operation P1. */
194 inline hashval_t
195 vn_phi_hasher::hash (const vn_phi_s *vp1)
197 return vp1->hashcode;
200 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
202 inline bool
203 vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2)
205 return vn_phi_eq (vp1, vp2);
208 /* Free a phi operation structure VP. */
210 inline void
211 vn_phi_hasher::remove (vn_phi_s *phi)
213 phi->phiargs.release ();
216 typedef hash_table<vn_phi_hasher> vn_phi_table_type;
217 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
220 /* Compare two reference operands P1 and P2 for equality. Return true if
221 they are equal, and false otherwise. */
223 static int
224 vn_reference_op_eq (const void *p1, const void *p2)
226 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
227 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
229 return (vro1->opcode == vro2->opcode
230 /* We do not care for differences in type qualification. */
231 && (vro1->type == vro2->type
232 || (vro1->type && vro2->type
233 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
234 TYPE_MAIN_VARIANT (vro2->type))))
235 && expressions_equal_p (vro1->op0, vro2->op0)
236 && expressions_equal_p (vro1->op1, vro2->op1)
237 && expressions_equal_p (vro1->op2, vro2->op2));
240 /* Free a reference operation structure VP. */
242 static inline void
243 free_reference (vn_reference_s *vr)
245 vr->operands.release ();
249 /* vn_reference hashtable helpers. */
251 struct vn_reference_hasher
253 typedef vn_reference_s *value_type;
254 typedef vn_reference_s *compare_type;
255 static inline hashval_t hash (const vn_reference_s *);
256 static inline bool equal (const vn_reference_s *, const vn_reference_s *);
257 static inline void remove (vn_reference_s *);
260 /* Return the hashcode for a given reference operation P1. */
262 inline hashval_t
263 vn_reference_hasher::hash (const vn_reference_s *vr1)
265 return vr1->hashcode;
268 inline bool
269 vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c)
271 return vn_reference_eq (v, c);
274 inline void
275 vn_reference_hasher::remove (vn_reference_s *v)
277 free_reference (v);
280 typedef hash_table<vn_reference_hasher> vn_reference_table_type;
281 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
284 /* The set of hashtables and alloc_pool's for their items. */
286 typedef struct vn_tables_s
288 vn_nary_op_table_type *nary;
289 vn_phi_table_type *phis;
290 vn_reference_table_type *references;
291 struct obstack nary_obstack;
292 alloc_pool phis_pool;
293 alloc_pool references_pool;
294 } *vn_tables_t;
297 /* vn_constant hashtable helpers. */
299 struct vn_constant_hasher : typed_free_remove <vn_constant_s>
301 typedef vn_constant_s *value_type;
302 typedef vn_constant_s *compare_type;
303 static inline hashval_t hash (const vn_constant_s *);
304 static inline bool equal (const vn_constant_s *, const vn_constant_s *);
307 /* Hash table hash function for vn_constant_t. */
309 inline hashval_t
310 vn_constant_hasher::hash (const vn_constant_s *vc1)
312 return vc1->hashcode;
315 /* Hash table equality function for vn_constant_t. */
317 inline bool
318 vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2)
320 if (vc1->hashcode != vc2->hashcode)
321 return false;
323 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
326 static hash_table<vn_constant_hasher> *constant_to_value_id;
327 static bitmap constant_value_ids;
330 /* Valid hashtables storing information we have proven to be
331 correct. */
333 static vn_tables_t valid_info;
335 /* Optimistic hashtables storing information we are making assumptions about
336 during iterations. */
338 static vn_tables_t optimistic_info;
340 /* Pointer to the set of hashtables that is currently being used.
341 Should always point to either the optimistic_info, or the
342 valid_info. */
344 static vn_tables_t current_info;
347 /* Reverse post order index for each basic block. */
349 static int *rpo_numbers;
351 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
353 /* Return the SSA value of the VUSE x, supporting released VDEFs
354 during elimination which will value-number the VDEF to the
355 associated VUSE (but not substitute in the whole lattice). */
357 static inline tree
358 vuse_ssa_val (tree x)
360 if (!x)
361 return NULL_TREE;
365 x = SSA_VAL (x);
367 while (SSA_NAME_IN_FREE_LIST (x));
369 return x;
372 /* This represents the top of the VN lattice, which is the universal
373 value. */
375 tree VN_TOP;
377 /* Unique counter for our value ids. */
379 static unsigned int next_value_id;
381 /* Next DFS number and the stack for strongly connected component
382 detection. */
384 static unsigned int next_dfs_num;
385 static vec<tree> sccstack;
389 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
390 are allocated on an obstack for locality reasons, and to free them
391 without looping over the vec. */
393 static vec<vn_ssa_aux_t> vn_ssa_aux_table;
394 static struct obstack vn_ssa_aux_obstack;
396 /* Return the value numbering information for a given SSA name. */
398 vn_ssa_aux_t
399 VN_INFO (tree name)
401 vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)];
402 gcc_checking_assert (res);
403 return res;
406 /* Set the value numbering info for a given SSA name to a given
407 value. */
409 static inline void
410 VN_INFO_SET (tree name, vn_ssa_aux_t value)
412 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value;
415 /* Initialize the value numbering info for a given SSA name.
416 This should be called just once for every SSA name. */
418 vn_ssa_aux_t
419 VN_INFO_GET (tree name)
421 vn_ssa_aux_t newinfo;
423 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
424 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
425 if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ())
426 vn_ssa_aux_table.safe_grow (SSA_NAME_VERSION (name) + 1);
427 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo;
428 return newinfo;
432 /* Get the representative expression for the SSA_NAME NAME. Returns
433 the representative SSA_NAME if there is no expression associated with it. */
435 tree
436 vn_get_expr_for (tree name)
438 vn_ssa_aux_t vn = VN_INFO (name);
439 gimple def_stmt;
440 tree expr = NULL_TREE;
441 enum tree_code code;
443 if (vn->valnum == VN_TOP)
444 return name;
446 /* If the value-number is a constant it is the representative
447 expression. */
448 if (TREE_CODE (vn->valnum) != SSA_NAME)
449 return vn->valnum;
451 /* Get to the information of the value of this SSA_NAME. */
452 vn = VN_INFO (vn->valnum);
454 /* If the value-number is a constant it is the representative
455 expression. */
456 if (TREE_CODE (vn->valnum) != SSA_NAME)
457 return vn->valnum;
459 /* Else if we have an expression, return it. */
460 if (vn->expr != NULL_TREE)
461 return vn->expr;
463 /* Otherwise use the defining statement to build the expression. */
464 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
466 /* If the value number is not an assignment use it directly. */
467 if (!is_gimple_assign (def_stmt))
468 return vn->valnum;
470 /* Note that we can valueize here because we clear the cached
471 simplified expressions after each optimistic iteration. */
472 code = gimple_assign_rhs_code (def_stmt);
473 switch (TREE_CODE_CLASS (code))
475 case tcc_reference:
476 if ((code == REALPART_EXPR
477 || code == IMAGPART_EXPR
478 || code == VIEW_CONVERT_EXPR)
479 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
480 0)) == SSA_NAME)
481 expr = fold_build1 (code,
482 gimple_expr_type (def_stmt),
483 vn_valueize (TREE_OPERAND
484 (gimple_assign_rhs1 (def_stmt), 0)));
485 break;
487 case tcc_unary:
488 expr = fold_build1 (code,
489 gimple_expr_type (def_stmt),
490 vn_valueize (gimple_assign_rhs1 (def_stmt)));
491 break;
493 case tcc_binary:
494 expr = fold_build2 (code,
495 gimple_expr_type (def_stmt),
496 vn_valueize (gimple_assign_rhs1 (def_stmt)),
497 vn_valueize (gimple_assign_rhs2 (def_stmt)));
498 break;
500 case tcc_exceptional:
501 if (code == CONSTRUCTOR
502 && TREE_CODE
503 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
504 expr = gimple_assign_rhs1 (def_stmt);
505 break;
507 default:;
509 if (expr == NULL_TREE)
510 return vn->valnum;
512 /* Cache the expression. */
513 vn->expr = expr;
515 return expr;
518 /* Return the vn_kind the expression computed by the stmt should be
519 associated with. */
521 enum vn_kind
522 vn_get_stmt_kind (gimple stmt)
524 switch (gimple_code (stmt))
526 case GIMPLE_CALL:
527 return VN_REFERENCE;
528 case GIMPLE_PHI:
529 return VN_PHI;
530 case GIMPLE_ASSIGN:
532 enum tree_code code = gimple_assign_rhs_code (stmt);
533 tree rhs1 = gimple_assign_rhs1 (stmt);
534 switch (get_gimple_rhs_class (code))
536 case GIMPLE_UNARY_RHS:
537 case GIMPLE_BINARY_RHS:
538 case GIMPLE_TERNARY_RHS:
539 return VN_NARY;
540 case GIMPLE_SINGLE_RHS:
541 switch (TREE_CODE_CLASS (code))
543 case tcc_reference:
544 /* VOP-less references can go through unary case. */
545 if ((code == REALPART_EXPR
546 || code == IMAGPART_EXPR
547 || code == VIEW_CONVERT_EXPR
548 || code == BIT_FIELD_REF)
549 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
550 return VN_NARY;
552 /* Fallthrough. */
553 case tcc_declaration:
554 return VN_REFERENCE;
556 case tcc_constant:
557 return VN_CONSTANT;
559 default:
560 if (code == ADDR_EXPR)
561 return (is_gimple_min_invariant (rhs1)
562 ? VN_CONSTANT : VN_REFERENCE);
563 else if (code == CONSTRUCTOR)
564 return VN_NARY;
565 return VN_NONE;
567 default:
568 return VN_NONE;
571 default:
572 return VN_NONE;
576 /* Lookup a value id for CONSTANT and return it. If it does not
577 exist returns 0. */
579 unsigned int
580 get_constant_value_id (tree constant)
582 vn_constant_s **slot;
583 struct vn_constant_s vc;
585 vc.hashcode = vn_hash_constant_with_type (constant);
586 vc.constant = constant;
587 slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
588 if (slot)
589 return (*slot)->value_id;
590 return 0;
593 /* Lookup a value id for CONSTANT, and if it does not exist, create a
594 new one and return it. If it does exist, return it. */
596 unsigned int
597 get_or_alloc_constant_value_id (tree constant)
599 vn_constant_s **slot;
600 struct vn_constant_s vc;
601 vn_constant_t vcp;
603 vc.hashcode = vn_hash_constant_with_type (constant);
604 vc.constant = constant;
605 slot = constant_to_value_id->find_slot (&vc, INSERT);
606 if (*slot)
607 return (*slot)->value_id;
609 vcp = XNEW (struct vn_constant_s);
610 vcp->hashcode = vc.hashcode;
611 vcp->constant = constant;
612 vcp->value_id = get_next_value_id ();
613 *slot = vcp;
614 bitmap_set_bit (constant_value_ids, vcp->value_id);
615 return vcp->value_id;
618 /* Return true if V is a value id for a constant. */
620 bool
621 value_id_constant_p (unsigned int v)
623 return bitmap_bit_p (constant_value_ids, v);
626 /* Compute the hash for a reference operand VRO1. */
628 static void
629 vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
631 hstate.add_int (vro1->opcode);
632 if (vro1->op0)
633 inchash::add_expr (vro1->op0, hstate);
634 if (vro1->op1)
635 inchash::add_expr (vro1->op1, hstate);
636 if (vro1->op2)
637 inchash::add_expr (vro1->op2, hstate);
640 /* Compute a hash for the reference operation VR1 and return it. */
642 static hashval_t
643 vn_reference_compute_hash (const vn_reference_t vr1)
645 inchash::hash hstate;
646 hashval_t result;
647 int i;
648 vn_reference_op_t vro;
649 HOST_WIDE_INT off = -1;
650 bool deref = false;
652 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
654 if (vro->opcode == MEM_REF)
655 deref = true;
656 else if (vro->opcode != ADDR_EXPR)
657 deref = false;
658 if (vro->off != -1)
660 if (off == -1)
661 off = 0;
662 off += vro->off;
664 else
666 if (off != -1
667 && off != 0)
668 hstate.add_int (off);
669 off = -1;
670 if (deref
671 && vro->opcode == ADDR_EXPR)
673 if (vro->op0)
675 tree op = TREE_OPERAND (vro->op0, 0);
676 hstate.add_int (TREE_CODE (op));
677 inchash::add_expr (op, hstate);
680 else
681 vn_reference_op_compute_hash (vro, hstate);
684 result = hstate.end ();
685 /* ??? We would ICE later if we hash instead of adding that in. */
686 if (vr1->vuse)
687 result += SSA_NAME_VERSION (vr1->vuse);
689 return result;
692 /* Return true if reference operations VR1 and VR2 are equivalent. This
693 means they have the same set of operands and vuses. */
695 bool
696 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
698 unsigned i, j;
700 /* Early out if this is not a hash collision. */
701 if (vr1->hashcode != vr2->hashcode)
702 return false;
704 /* The VOP needs to be the same. */
705 if (vr1->vuse != vr2->vuse)
706 return false;
708 /* If the operands are the same we are done. */
709 if (vr1->operands == vr2->operands)
710 return true;
712 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
713 return false;
715 if (INTEGRAL_TYPE_P (vr1->type)
716 && INTEGRAL_TYPE_P (vr2->type))
718 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
719 return false;
721 else if (INTEGRAL_TYPE_P (vr1->type)
722 && (TYPE_PRECISION (vr1->type)
723 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
724 return false;
725 else if (INTEGRAL_TYPE_P (vr2->type)
726 && (TYPE_PRECISION (vr2->type)
727 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
728 return false;
730 i = 0;
731 j = 0;
734 HOST_WIDE_INT off1 = 0, off2 = 0;
735 vn_reference_op_t vro1, vro2;
736 vn_reference_op_s tem1, tem2;
737 bool deref1 = false, deref2 = false;
738 for (; vr1->operands.iterate (i, &vro1); i++)
740 if (vro1->opcode == MEM_REF)
741 deref1 = true;
742 if (vro1->off == -1)
743 break;
744 off1 += vro1->off;
746 for (; vr2->operands.iterate (j, &vro2); j++)
748 if (vro2->opcode == MEM_REF)
749 deref2 = true;
750 if (vro2->off == -1)
751 break;
752 off2 += vro2->off;
754 if (off1 != off2)
755 return false;
756 if (deref1 && vro1->opcode == ADDR_EXPR)
758 memset (&tem1, 0, sizeof (tem1));
759 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
760 tem1.type = TREE_TYPE (tem1.op0);
761 tem1.opcode = TREE_CODE (tem1.op0);
762 vro1 = &tem1;
763 deref1 = false;
765 if (deref2 && vro2->opcode == ADDR_EXPR)
767 memset (&tem2, 0, sizeof (tem2));
768 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
769 tem2.type = TREE_TYPE (tem2.op0);
770 tem2.opcode = TREE_CODE (tem2.op0);
771 vro2 = &tem2;
772 deref2 = false;
774 if (deref1 != deref2)
775 return false;
776 if (!vn_reference_op_eq (vro1, vro2))
777 return false;
778 ++j;
779 ++i;
781 while (vr1->operands.length () != i
782 || vr2->operands.length () != j);
784 return true;
787 /* Copy the operations present in load/store REF into RESULT, a vector of
788 vn_reference_op_s's. */
790 static void
791 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
793 if (TREE_CODE (ref) == TARGET_MEM_REF)
795 vn_reference_op_s temp;
797 result->reserve (3);
799 memset (&temp, 0, sizeof (temp));
800 temp.type = TREE_TYPE (ref);
801 temp.opcode = TREE_CODE (ref);
802 temp.op0 = TMR_INDEX (ref);
803 temp.op1 = TMR_STEP (ref);
804 temp.op2 = TMR_OFFSET (ref);
805 temp.off = -1;
806 result->quick_push (temp);
808 memset (&temp, 0, sizeof (temp));
809 temp.type = NULL_TREE;
810 temp.opcode = ERROR_MARK;
811 temp.op0 = TMR_INDEX2 (ref);
812 temp.off = -1;
813 result->quick_push (temp);
815 memset (&temp, 0, sizeof (temp));
816 temp.type = NULL_TREE;
817 temp.opcode = TREE_CODE (TMR_BASE (ref));
818 temp.op0 = TMR_BASE (ref);
819 temp.off = -1;
820 result->quick_push (temp);
821 return;
824 /* For non-calls, store the information that makes up the address. */
825 tree orig = ref;
826 while (ref)
828 vn_reference_op_s temp;
830 memset (&temp, 0, sizeof (temp));
831 temp.type = TREE_TYPE (ref);
832 temp.opcode = TREE_CODE (ref);
833 temp.off = -1;
835 switch (temp.opcode)
837 case MODIFY_EXPR:
838 temp.op0 = TREE_OPERAND (ref, 1);
839 break;
840 case WITH_SIZE_EXPR:
841 temp.op0 = TREE_OPERAND (ref, 1);
842 temp.off = 0;
843 break;
844 case MEM_REF:
845 /* The base address gets its own vn_reference_op_s structure. */
846 temp.op0 = TREE_OPERAND (ref, 1);
847 if (tree_fits_shwi_p (TREE_OPERAND (ref, 1)))
848 temp.off = tree_to_shwi (TREE_OPERAND (ref, 1));
849 break;
850 case BIT_FIELD_REF:
851 /* Record bits and position. */
852 temp.op0 = TREE_OPERAND (ref, 1);
853 temp.op1 = TREE_OPERAND (ref, 2);
854 break;
855 case COMPONENT_REF:
856 /* The field decl is enough to unambiguously specify the field,
857 a matching type is not necessary and a mismatching type
858 is always a spurious difference. */
859 temp.type = NULL_TREE;
860 temp.op0 = TREE_OPERAND (ref, 1);
861 temp.op1 = TREE_OPERAND (ref, 2);
863 tree this_offset = component_ref_field_offset (ref);
864 if (this_offset
865 && TREE_CODE (this_offset) == INTEGER_CST)
867 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
868 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
870 offset_int off
871 = (wi::to_offset (this_offset)
872 + wi::lrshift (wi::to_offset (bit_offset),
873 LOG2_BITS_PER_UNIT));
874 if (wi::fits_shwi_p (off)
875 /* Probibit value-numbering zero offset components
876 of addresses the same before the pass folding
877 __builtin_object_size had a chance to run
878 (checking cfun->after_inlining does the
879 trick here). */
880 && (TREE_CODE (orig) != ADDR_EXPR
881 || off != 0
882 || cfun->after_inlining))
883 temp.off = off.to_shwi ();
887 break;
888 case ARRAY_RANGE_REF:
889 case ARRAY_REF:
890 /* Record index as operand. */
891 temp.op0 = TREE_OPERAND (ref, 1);
892 /* Always record lower bounds and element size. */
893 temp.op1 = array_ref_low_bound (ref);
894 temp.op2 = array_ref_element_size (ref);
895 if (TREE_CODE (temp.op0) == INTEGER_CST
896 && TREE_CODE (temp.op1) == INTEGER_CST
897 && TREE_CODE (temp.op2) == INTEGER_CST)
899 offset_int off = ((wi::to_offset (temp.op0)
900 - wi::to_offset (temp.op1))
901 * wi::to_offset (temp.op2));
902 if (wi::fits_shwi_p (off))
903 temp.off = off.to_shwi();
905 break;
906 case VAR_DECL:
907 if (DECL_HARD_REGISTER (ref))
909 temp.op0 = ref;
910 break;
912 /* Fallthru. */
913 case PARM_DECL:
914 case CONST_DECL:
915 case RESULT_DECL:
916 /* Canonicalize decls to MEM[&decl] which is what we end up with
917 when valueizing MEM[ptr] with ptr = &decl. */
918 temp.opcode = MEM_REF;
919 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
920 temp.off = 0;
921 result->safe_push (temp);
922 temp.opcode = ADDR_EXPR;
923 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
924 temp.type = TREE_TYPE (temp.op0);
925 temp.off = -1;
926 break;
927 case STRING_CST:
928 case INTEGER_CST:
929 case COMPLEX_CST:
930 case VECTOR_CST:
931 case REAL_CST:
932 case FIXED_CST:
933 case CONSTRUCTOR:
934 case SSA_NAME:
935 temp.op0 = ref;
936 break;
937 case ADDR_EXPR:
938 if (is_gimple_min_invariant (ref))
940 temp.op0 = ref;
941 break;
943 break;
944 /* These are only interesting for their operands, their
945 existence, and their type. They will never be the last
946 ref in the chain of references (IE they require an
947 operand), so we don't have to put anything
948 for op* as it will be handled by the iteration */
949 case REALPART_EXPR:
950 case VIEW_CONVERT_EXPR:
951 temp.off = 0;
952 break;
953 case IMAGPART_EXPR:
954 /* This is only interesting for its constant offset. */
955 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
956 break;
957 default:
958 gcc_unreachable ();
960 result->safe_push (temp);
962 if (REFERENCE_CLASS_P (ref)
963 || TREE_CODE (ref) == MODIFY_EXPR
964 || TREE_CODE (ref) == WITH_SIZE_EXPR
965 || (TREE_CODE (ref) == ADDR_EXPR
966 && !is_gimple_min_invariant (ref)))
967 ref = TREE_OPERAND (ref, 0);
968 else
969 ref = NULL_TREE;
973 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
974 operands in *OPS, the reference alias set SET and the reference type TYPE.
975 Return true if something useful was produced. */
977 bool
978 ao_ref_init_from_vn_reference (ao_ref *ref,
979 alias_set_type set, tree type,
980 vec<vn_reference_op_s> ops)
982 vn_reference_op_t op;
983 unsigned i;
984 tree base = NULL_TREE;
985 tree *op0_p = &base;
986 HOST_WIDE_INT offset = 0;
987 HOST_WIDE_INT max_size;
988 HOST_WIDE_INT size = -1;
989 tree size_tree = NULL_TREE;
990 alias_set_type base_alias_set = -1;
992 /* First get the final access size from just the outermost expression. */
993 op = &ops[0];
994 if (op->opcode == COMPONENT_REF)
995 size_tree = DECL_SIZE (op->op0);
996 else if (op->opcode == BIT_FIELD_REF)
997 size_tree = op->op0;
998 else
1000 machine_mode mode = TYPE_MODE (type);
1001 if (mode == BLKmode)
1002 size_tree = TYPE_SIZE (type);
1003 else
1004 size = GET_MODE_BITSIZE (mode);
1006 if (size_tree != NULL_TREE)
1008 if (!tree_fits_uhwi_p (size_tree))
1009 size = -1;
1010 else
1011 size = tree_to_uhwi (size_tree);
1014 /* Initially, maxsize is the same as the accessed element size.
1015 In the following it will only grow (or become -1). */
1016 max_size = size;
1018 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1019 and find the ultimate containing object. */
1020 FOR_EACH_VEC_ELT (ops, i, op)
1022 switch (op->opcode)
1024 /* These may be in the reference ops, but we cannot do anything
1025 sensible with them here. */
1026 case ADDR_EXPR:
1027 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1028 if (base != NULL_TREE
1029 && TREE_CODE (base) == MEM_REF
1030 && op->op0
1031 && DECL_P (TREE_OPERAND (op->op0, 0)))
1033 vn_reference_op_t pop = &ops[i-1];
1034 base = TREE_OPERAND (op->op0, 0);
1035 if (pop->off == -1)
1037 max_size = -1;
1038 offset = 0;
1040 else
1041 offset += pop->off * BITS_PER_UNIT;
1042 op0_p = NULL;
1043 break;
1045 /* Fallthru. */
1046 case CALL_EXPR:
1047 return false;
1049 /* Record the base objects. */
1050 case MEM_REF:
1051 base_alias_set = get_deref_alias_set (op->op0);
1052 *op0_p = build2 (MEM_REF, op->type,
1053 NULL_TREE, op->op0);
1054 op0_p = &TREE_OPERAND (*op0_p, 0);
1055 break;
1057 case VAR_DECL:
1058 case PARM_DECL:
1059 case RESULT_DECL:
1060 case SSA_NAME:
1061 *op0_p = op->op0;
1062 op0_p = NULL;
1063 break;
1065 /* And now the usual component-reference style ops. */
1066 case BIT_FIELD_REF:
1067 offset += tree_to_shwi (op->op1);
1068 break;
1070 case COMPONENT_REF:
1072 tree field = op->op0;
1073 /* We do not have a complete COMPONENT_REF tree here so we
1074 cannot use component_ref_field_offset. Do the interesting
1075 parts manually. */
1077 if (op->op1
1078 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
1079 max_size = -1;
1080 else
1082 offset += (tree_to_uhwi (DECL_FIELD_OFFSET (field))
1083 * BITS_PER_UNIT);
1084 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
1086 break;
1089 case ARRAY_RANGE_REF:
1090 case ARRAY_REF:
1091 /* We recorded the lower bound and the element size. */
1092 if (!tree_fits_shwi_p (op->op0)
1093 || !tree_fits_shwi_p (op->op1)
1094 || !tree_fits_shwi_p (op->op2))
1095 max_size = -1;
1096 else
1098 HOST_WIDE_INT hindex = tree_to_shwi (op->op0);
1099 hindex -= tree_to_shwi (op->op1);
1100 hindex *= tree_to_shwi (op->op2);
1101 hindex *= BITS_PER_UNIT;
1102 offset += hindex;
1104 break;
1106 case REALPART_EXPR:
1107 break;
1109 case IMAGPART_EXPR:
1110 offset += size;
1111 break;
1113 case VIEW_CONVERT_EXPR:
1114 break;
1116 case STRING_CST:
1117 case INTEGER_CST:
1118 case COMPLEX_CST:
1119 case VECTOR_CST:
1120 case REAL_CST:
1121 case CONSTRUCTOR:
1122 case CONST_DECL:
1123 return false;
1125 default:
1126 return false;
1130 if (base == NULL_TREE)
1131 return false;
1133 ref->ref = NULL_TREE;
1134 ref->base = base;
1135 ref->offset = offset;
1136 ref->size = size;
1137 ref->max_size = max_size;
1138 ref->ref_alias_set = set;
1139 if (base_alias_set != -1)
1140 ref->base_alias_set = base_alias_set;
1141 else
1142 ref->base_alias_set = get_alias_set (base);
1143 /* We discount volatiles from value-numbering elsewhere. */
1144 ref->volatile_p = false;
1146 return true;
1149 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1150 vn_reference_op_s's. */
1152 static void
1153 copy_reference_ops_from_call (gcall *call,
1154 vec<vn_reference_op_s> *result)
1156 vn_reference_op_s temp;
1157 unsigned i;
1158 tree lhs = gimple_call_lhs (call);
1159 int lr;
1161 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1162 different. By adding the lhs here in the vector, we ensure that the
1163 hashcode is different, guaranteeing a different value number. */
1164 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1166 memset (&temp, 0, sizeof (temp));
1167 temp.opcode = MODIFY_EXPR;
1168 temp.type = TREE_TYPE (lhs);
1169 temp.op0 = lhs;
1170 temp.off = -1;
1171 result->safe_push (temp);
1174 /* Copy the type, opcode, function, static chain and EH region, if any. */
1175 memset (&temp, 0, sizeof (temp));
1176 temp.type = gimple_call_return_type (call);
1177 temp.opcode = CALL_EXPR;
1178 temp.op0 = gimple_call_fn (call);
1179 temp.op1 = gimple_call_chain (call);
1180 if (stmt_could_throw_p (call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1181 temp.op2 = size_int (lr);
1182 temp.off = -1;
1183 if (gimple_call_with_bounds_p (call))
1184 temp.with_bounds = 1;
1185 result->safe_push (temp);
1187 /* Copy the call arguments. As they can be references as well,
1188 just chain them together. */
1189 for (i = 0; i < gimple_call_num_args (call); ++i)
1191 tree callarg = gimple_call_arg (call, i);
1192 copy_reference_ops_from_ref (callarg, result);
1196 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1197 *I_P to point to the last element of the replacement. */
1198 void
1199 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1200 unsigned int *i_p)
1202 unsigned int i = *i_p;
1203 vn_reference_op_t op = &(*ops)[i];
1204 vn_reference_op_t mem_op = &(*ops)[i - 1];
1205 tree addr_base;
1206 HOST_WIDE_INT addr_offset = 0;
1208 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1209 from .foo.bar to the preceding MEM_REF offset and replace the
1210 address with &OBJ. */
1211 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1212 &addr_offset);
1213 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1214 if (addr_base != TREE_OPERAND (op->op0, 0))
1216 offset_int off = offset_int::from (mem_op->op0, SIGNED);
1217 off += addr_offset;
1218 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1219 op->op0 = build_fold_addr_expr (addr_base);
1220 if (tree_fits_shwi_p (mem_op->op0))
1221 mem_op->off = tree_to_shwi (mem_op->op0);
1222 else
1223 mem_op->off = -1;
1227 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1228 *I_P to point to the last element of the replacement. */
1229 static void
1230 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1231 unsigned int *i_p)
1233 unsigned int i = *i_p;
1234 vn_reference_op_t op = &(*ops)[i];
1235 vn_reference_op_t mem_op = &(*ops)[i - 1];
1236 gimple def_stmt;
1237 enum tree_code code;
1238 offset_int off;
1240 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1241 if (!is_gimple_assign (def_stmt))
1242 return;
1244 code = gimple_assign_rhs_code (def_stmt);
1245 if (code != ADDR_EXPR
1246 && code != POINTER_PLUS_EXPR)
1247 return;
1249 off = offset_int::from (mem_op->op0, SIGNED);
1251 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1252 from .foo.bar to the preceding MEM_REF offset and replace the
1253 address with &OBJ. */
1254 if (code == ADDR_EXPR)
1256 tree addr, addr_base;
1257 HOST_WIDE_INT addr_offset;
1259 addr = gimple_assign_rhs1 (def_stmt);
1260 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1261 &addr_offset);
1262 if (!addr_base
1263 || TREE_CODE (addr_base) != MEM_REF)
1264 return;
1266 off += addr_offset;
1267 off += mem_ref_offset (addr_base);
1268 op->op0 = TREE_OPERAND (addr_base, 0);
1270 else
1272 tree ptr, ptroff;
1273 ptr = gimple_assign_rhs1 (def_stmt);
1274 ptroff = gimple_assign_rhs2 (def_stmt);
1275 if (TREE_CODE (ptr) != SSA_NAME
1276 || TREE_CODE (ptroff) != INTEGER_CST)
1277 return;
1279 off += wi::to_offset (ptroff);
1280 op->op0 = ptr;
1283 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1284 if (tree_fits_shwi_p (mem_op->op0))
1285 mem_op->off = tree_to_shwi (mem_op->op0);
1286 else
1287 mem_op->off = -1;
1288 if (TREE_CODE (op->op0) == SSA_NAME)
1289 op->op0 = SSA_VAL (op->op0);
1290 if (TREE_CODE (op->op0) != SSA_NAME)
1291 op->opcode = TREE_CODE (op->op0);
1293 /* And recurse. */
1294 if (TREE_CODE (op->op0) == SSA_NAME)
1295 vn_reference_maybe_forwprop_address (ops, i_p);
1296 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1297 vn_reference_fold_indirect (ops, i_p);
1300 /* Optimize the reference REF to a constant if possible or return
1301 NULL_TREE if not. */
1303 tree
1304 fully_constant_vn_reference_p (vn_reference_t ref)
1306 vec<vn_reference_op_s> operands = ref->operands;
1307 vn_reference_op_t op;
1309 /* Try to simplify the translated expression if it is
1310 a call to a builtin function with at most two arguments. */
1311 op = &operands[0];
1312 if (op->opcode == CALL_EXPR
1313 && TREE_CODE (op->op0) == ADDR_EXPR
1314 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1315 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1316 && operands.length () >= 2
1317 && operands.length () <= 3)
1319 vn_reference_op_t arg0, arg1 = NULL;
1320 bool anyconst = false;
1321 arg0 = &operands[1];
1322 if (operands.length () > 2)
1323 arg1 = &operands[2];
1324 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1325 || (arg0->opcode == ADDR_EXPR
1326 && is_gimple_min_invariant (arg0->op0)))
1327 anyconst = true;
1328 if (arg1
1329 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1330 || (arg1->opcode == ADDR_EXPR
1331 && is_gimple_min_invariant (arg1->op0))))
1332 anyconst = true;
1333 if (anyconst)
1335 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1336 arg1 ? 2 : 1,
1337 arg0->op0,
1338 arg1 ? arg1->op0 : NULL);
1339 if (folded
1340 && TREE_CODE (folded) == NOP_EXPR)
1341 folded = TREE_OPERAND (folded, 0);
1342 if (folded
1343 && is_gimple_min_invariant (folded))
1344 return folded;
1348 /* Simplify reads from constants or constant initializers. */
1349 else if (BITS_PER_UNIT == 8
1350 && is_gimple_reg_type (ref->type)
1351 && (!INTEGRAL_TYPE_P (ref->type)
1352 || TYPE_PRECISION (ref->type) % BITS_PER_UNIT == 0))
1354 HOST_WIDE_INT off = 0;
1355 HOST_WIDE_INT size;
1356 if (INTEGRAL_TYPE_P (ref->type))
1357 size = TYPE_PRECISION (ref->type);
1358 else
1359 size = tree_to_shwi (TYPE_SIZE (ref->type));
1360 if (size % BITS_PER_UNIT != 0
1361 || size > MAX_BITSIZE_MODE_ANY_MODE)
1362 return NULL_TREE;
1363 size /= BITS_PER_UNIT;
1364 unsigned i;
1365 for (i = 0; i < operands.length (); ++i)
1367 if (operands[i].off == -1)
1368 return NULL_TREE;
1369 off += operands[i].off;
1370 if (operands[i].opcode == MEM_REF)
1372 ++i;
1373 break;
1376 vn_reference_op_t base = &operands[--i];
1377 tree ctor = error_mark_node;
1378 tree decl = NULL_TREE;
1379 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1380 ctor = base->op0;
1381 else if (base->opcode == MEM_REF
1382 && base[1].opcode == ADDR_EXPR
1383 && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
1384 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL))
1386 decl = TREE_OPERAND (base[1].op0, 0);
1387 ctor = ctor_for_folding (decl);
1389 if (ctor == NULL_TREE)
1390 return build_zero_cst (ref->type);
1391 else if (ctor != error_mark_node)
1393 if (decl)
1395 tree res = fold_ctor_reference (ref->type, ctor,
1396 off * BITS_PER_UNIT,
1397 size * BITS_PER_UNIT, decl);
1398 if (res)
1400 STRIP_USELESS_TYPE_CONVERSION (res);
1401 if (is_gimple_min_invariant (res))
1402 return res;
1405 else
1407 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1408 if (native_encode_expr (ctor, buf, size, off) > 0)
1409 return native_interpret_expr (ref->type, buf, size);
1414 return NULL_TREE;
1417 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1418 structures into their value numbers. This is done in-place, and
1419 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1420 whether any operands were valueized. */
1422 static vec<vn_reference_op_s>
1423 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1425 vn_reference_op_t vro;
1426 unsigned int i;
1428 *valueized_anything = false;
1430 FOR_EACH_VEC_ELT (orig, i, vro)
1432 if (vro->opcode == SSA_NAME
1433 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1435 tree tem = SSA_VAL (vro->op0);
1436 if (tem != vro->op0)
1438 *valueized_anything = true;
1439 vro->op0 = tem;
1441 /* If it transforms from an SSA_NAME to a constant, update
1442 the opcode. */
1443 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1444 vro->opcode = TREE_CODE (vro->op0);
1446 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1448 tree tem = SSA_VAL (vro->op1);
1449 if (tem != vro->op1)
1451 *valueized_anything = true;
1452 vro->op1 = tem;
1455 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1457 tree tem = SSA_VAL (vro->op2);
1458 if (tem != vro->op2)
1460 *valueized_anything = true;
1461 vro->op2 = tem;
1464 /* If it transforms from an SSA_NAME to an address, fold with
1465 a preceding indirect reference. */
1466 if (i > 0
1467 && vro->op0
1468 && TREE_CODE (vro->op0) == ADDR_EXPR
1469 && orig[i - 1].opcode == MEM_REF)
1470 vn_reference_fold_indirect (&orig, &i);
1471 else if (i > 0
1472 && vro->opcode == SSA_NAME
1473 && orig[i - 1].opcode == MEM_REF)
1474 vn_reference_maybe_forwprop_address (&orig, &i);
1475 /* If it transforms a non-constant ARRAY_REF into a constant
1476 one, adjust the constant offset. */
1477 else if (vro->opcode == ARRAY_REF
1478 && vro->off == -1
1479 && TREE_CODE (vro->op0) == INTEGER_CST
1480 && TREE_CODE (vro->op1) == INTEGER_CST
1481 && TREE_CODE (vro->op2) == INTEGER_CST)
1483 offset_int off = ((wi::to_offset (vro->op0)
1484 - wi::to_offset (vro->op1))
1485 * wi::to_offset (vro->op2));
1486 if (wi::fits_shwi_p (off))
1487 vro->off = off.to_shwi ();
1491 return orig;
1494 static vec<vn_reference_op_s>
1495 valueize_refs (vec<vn_reference_op_s> orig)
1497 bool tem;
1498 return valueize_refs_1 (orig, &tem);
1501 static vec<vn_reference_op_s> shared_lookup_references;
1503 /* Create a vector of vn_reference_op_s structures from REF, a
1504 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1505 this function. *VALUEIZED_ANYTHING will specify whether any
1506 operands were valueized. */
1508 static vec<vn_reference_op_s>
1509 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1511 if (!ref)
1512 return vNULL;
1513 shared_lookup_references.truncate (0);
1514 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1515 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1516 valueized_anything);
1517 return shared_lookup_references;
1520 /* Create a vector of vn_reference_op_s structures from CALL, a
1521 call statement. The vector is shared among all callers of
1522 this function. */
1524 static vec<vn_reference_op_s>
1525 valueize_shared_reference_ops_from_call (gcall *call)
1527 if (!call)
1528 return vNULL;
1529 shared_lookup_references.truncate (0);
1530 copy_reference_ops_from_call (call, &shared_lookup_references);
1531 shared_lookup_references = valueize_refs (shared_lookup_references);
1532 return shared_lookup_references;
1535 /* Lookup a SCCVN reference operation VR in the current hash table.
1536 Returns the resulting value number if it exists in the hash table,
1537 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1538 vn_reference_t stored in the hashtable if something is found. */
1540 static tree
1541 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1543 vn_reference_s **slot;
1544 hashval_t hash;
1546 hash = vr->hashcode;
1547 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1548 if (!slot && current_info == optimistic_info)
1549 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1550 if (slot)
1552 if (vnresult)
1553 *vnresult = (vn_reference_t)*slot;
1554 return ((vn_reference_t)*slot)->result;
1557 return NULL_TREE;
1560 static tree *last_vuse_ptr;
1561 static vn_lookup_kind vn_walk_kind;
1562 static vn_lookup_kind default_vn_walk_kind;
1564 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1565 with the current VUSE and performs the expression lookup. */
1567 static void *
1568 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1569 unsigned int cnt, void *vr_)
1571 vn_reference_t vr = (vn_reference_t)vr_;
1572 vn_reference_s **slot;
1573 hashval_t hash;
1575 /* This bounds the stmt walks we perform on reference lookups
1576 to O(1) instead of O(N) where N is the number of dominating
1577 stores. */
1578 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1579 return (void *)-1;
1581 if (last_vuse_ptr)
1582 *last_vuse_ptr = vuse;
1584 /* Fixup vuse and hash. */
1585 if (vr->vuse)
1586 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1587 vr->vuse = vuse_ssa_val (vuse);
1588 if (vr->vuse)
1589 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1591 hash = vr->hashcode;
1592 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1593 if (!slot && current_info == optimistic_info)
1594 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1595 if (slot)
1596 return *slot;
1598 return NULL;
1601 /* Lookup an existing or insert a new vn_reference entry into the
1602 value table for the VUSE, SET, TYPE, OPERANDS reference which
1603 has the value VALUE which is either a constant or an SSA name. */
1605 static vn_reference_t
1606 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1607 alias_set_type set,
1608 tree type,
1609 vec<vn_reference_op_s,
1610 va_heap> operands,
1611 tree value)
1613 vn_reference_s vr1;
1614 vn_reference_t result;
1615 unsigned value_id;
1616 vr1.vuse = vuse;
1617 vr1.operands = operands;
1618 vr1.type = type;
1619 vr1.set = set;
1620 vr1.hashcode = vn_reference_compute_hash (&vr1);
1621 if (vn_reference_lookup_1 (&vr1, &result))
1622 return result;
1623 if (TREE_CODE (value) == SSA_NAME)
1624 value_id = VN_INFO (value)->value_id;
1625 else
1626 value_id = get_or_alloc_constant_value_id (value);
1627 return vn_reference_insert_pieces (vuse, set, type,
1628 operands.copy (), value, value_id);
1631 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1632 from the statement defining VUSE and if not successful tries to
1633 translate *REFP and VR_ through an aggregate copy at the definition
1634 of VUSE. */
1636 static void *
1637 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1638 bool disambiguate_only)
1640 vn_reference_t vr = (vn_reference_t)vr_;
1641 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1642 tree base;
1643 HOST_WIDE_INT offset, maxsize;
1644 static vec<vn_reference_op_s>
1645 lhs_ops = vNULL;
1646 ao_ref lhs_ref;
1647 bool lhs_ref_ok = false;
1649 /* First try to disambiguate after value-replacing in the definitions LHS. */
1650 if (is_gimple_assign (def_stmt))
1652 vec<vn_reference_op_s> tem;
1653 tree lhs = gimple_assign_lhs (def_stmt);
1654 bool valueized_anything = false;
1655 /* Avoid re-allocation overhead. */
1656 lhs_ops.truncate (0);
1657 copy_reference_ops_from_ref (lhs, &lhs_ops);
1658 tem = lhs_ops;
1659 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1660 gcc_assert (lhs_ops == tem);
1661 if (valueized_anything)
1663 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1664 get_alias_set (lhs),
1665 TREE_TYPE (lhs), lhs_ops);
1666 if (lhs_ref_ok
1667 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1668 return NULL;
1670 else
1672 ao_ref_init (&lhs_ref, lhs);
1673 lhs_ref_ok = true;
1676 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1677 && gimple_call_num_args (def_stmt) <= 4)
1679 /* For builtin calls valueize its arguments and call the
1680 alias oracle again. Valueization may improve points-to
1681 info of pointers and constify size and position arguments.
1682 Originally this was motivated by PR61034 which has
1683 conditional calls to free falsely clobbering ref because
1684 of imprecise points-to info of the argument. */
1685 tree oldargs[4];
1686 bool valueized_anything = false;
1687 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1689 oldargs[i] = gimple_call_arg (def_stmt, i);
1690 if (TREE_CODE (oldargs[i]) == SSA_NAME
1691 && VN_INFO (oldargs[i])->valnum != oldargs[i])
1693 gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum);
1694 valueized_anything = true;
1697 if (valueized_anything)
1699 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
1700 ref);
1701 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1702 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1703 if (!res)
1704 return NULL;
1708 if (disambiguate_only)
1709 return (void *)-1;
1711 base = ao_ref_base (ref);
1712 offset = ref->offset;
1713 maxsize = ref->max_size;
1715 /* If we cannot constrain the size of the reference we cannot
1716 test if anything kills it. */
1717 if (maxsize == -1)
1718 return (void *)-1;
1720 /* We can't deduce anything useful from clobbers. */
1721 if (gimple_clobber_p (def_stmt))
1722 return (void *)-1;
1724 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1725 from that definition.
1726 1) Memset. */
1727 if (is_gimple_reg_type (vr->type)
1728 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1729 && integer_zerop (gimple_call_arg (def_stmt, 1))
1730 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))
1731 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1733 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1734 tree base2;
1735 HOST_WIDE_INT offset2, size2, maxsize2;
1736 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1737 size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8;
1738 if ((unsigned HOST_WIDE_INT)size2 / 8
1739 == tree_to_uhwi (gimple_call_arg (def_stmt, 2))
1740 && maxsize2 != -1
1741 && operand_equal_p (base, base2, 0)
1742 && offset2 <= offset
1743 && offset2 + size2 >= offset + maxsize)
1745 tree val = build_zero_cst (vr->type);
1746 return vn_reference_lookup_or_insert_for_pieces
1747 (vuse, vr->set, vr->type, vr->operands, val);
1751 /* 2) Assignment from an empty CONSTRUCTOR. */
1752 else if (is_gimple_reg_type (vr->type)
1753 && gimple_assign_single_p (def_stmt)
1754 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1755 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1757 tree base2;
1758 HOST_WIDE_INT offset2, size2, maxsize2;
1759 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1760 &offset2, &size2, &maxsize2);
1761 if (maxsize2 != -1
1762 && operand_equal_p (base, base2, 0)
1763 && offset2 <= offset
1764 && offset2 + size2 >= offset + maxsize)
1766 tree val = build_zero_cst (vr->type);
1767 return vn_reference_lookup_or_insert_for_pieces
1768 (vuse, vr->set, vr->type, vr->operands, val);
1772 /* 3) Assignment from a constant. We can use folds native encode/interpret
1773 routines to extract the assigned bits. */
1774 else if (vn_walk_kind == VN_WALKREWRITE
1775 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1776 && ref->size == maxsize
1777 && maxsize % BITS_PER_UNIT == 0
1778 && offset % BITS_PER_UNIT == 0
1779 && is_gimple_reg_type (vr->type)
1780 && gimple_assign_single_p (def_stmt)
1781 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1783 tree base2;
1784 HOST_WIDE_INT offset2, size2, maxsize2;
1785 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1786 &offset2, &size2, &maxsize2);
1787 if (maxsize2 != -1
1788 && maxsize2 == size2
1789 && size2 % BITS_PER_UNIT == 0
1790 && offset2 % BITS_PER_UNIT == 0
1791 && operand_equal_p (base, base2, 0)
1792 && offset2 <= offset
1793 && offset2 + size2 >= offset + maxsize)
1795 /* We support up to 512-bit values (for V8DFmode). */
1796 unsigned char buffer[64];
1797 int len;
1799 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1800 buffer, sizeof (buffer));
1801 if (len > 0)
1803 tree val = native_interpret_expr (vr->type,
1804 buffer
1805 + ((offset - offset2)
1806 / BITS_PER_UNIT),
1807 ref->size / BITS_PER_UNIT);
1808 if (val)
1809 return vn_reference_lookup_or_insert_for_pieces
1810 (vuse, vr->set, vr->type, vr->operands, val);
1815 /* 4) Assignment from an SSA name which definition we may be able
1816 to access pieces from. */
1817 else if (ref->size == maxsize
1818 && is_gimple_reg_type (vr->type)
1819 && gimple_assign_single_p (def_stmt)
1820 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1822 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1823 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1824 if (is_gimple_assign (def_stmt2)
1825 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1826 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1827 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1829 tree base2;
1830 HOST_WIDE_INT offset2, size2, maxsize2, off;
1831 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1832 &offset2, &size2, &maxsize2);
1833 off = offset - offset2;
1834 if (maxsize2 != -1
1835 && maxsize2 == size2
1836 && operand_equal_p (base, base2, 0)
1837 && offset2 <= offset
1838 && offset2 + size2 >= offset + maxsize)
1840 tree val = NULL_TREE;
1841 HOST_WIDE_INT elsz
1842 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1843 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1845 if (off == 0)
1846 val = gimple_assign_rhs1 (def_stmt2);
1847 else if (off == elsz)
1848 val = gimple_assign_rhs2 (def_stmt2);
1850 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1851 && off % elsz == 0)
1853 tree ctor = gimple_assign_rhs1 (def_stmt2);
1854 unsigned i = off / elsz;
1855 if (i < CONSTRUCTOR_NELTS (ctor))
1857 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1858 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1860 if (TREE_CODE (TREE_TYPE (elt->value))
1861 != VECTOR_TYPE)
1862 val = elt->value;
1866 if (val)
1867 return vn_reference_lookup_or_insert_for_pieces
1868 (vuse, vr->set, vr->type, vr->operands, val);
1873 /* 5) For aggregate copies translate the reference through them if
1874 the copy kills ref. */
1875 else if (vn_walk_kind == VN_WALKREWRITE
1876 && gimple_assign_single_p (def_stmt)
1877 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1878 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1879 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1881 tree base2;
1882 HOST_WIDE_INT offset2, size2, maxsize2;
1883 int i, j;
1884 auto_vec<vn_reference_op_s> rhs;
1885 vn_reference_op_t vro;
1886 ao_ref r;
1888 if (!lhs_ref_ok)
1889 return (void *)-1;
1891 /* See if the assignment kills REF. */
1892 base2 = ao_ref_base (&lhs_ref);
1893 offset2 = lhs_ref.offset;
1894 size2 = lhs_ref.size;
1895 maxsize2 = lhs_ref.max_size;
1896 if (maxsize2 == -1
1897 || (base != base2 && !operand_equal_p (base, base2, 0))
1898 || offset2 > offset
1899 || offset2 + size2 < offset + maxsize)
1900 return (void *)-1;
1902 /* Find the common base of ref and the lhs. lhs_ops already
1903 contains valueized operands for the lhs. */
1904 i = vr->operands.length () - 1;
1905 j = lhs_ops.length () - 1;
1906 while (j >= 0 && i >= 0
1907 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1909 i--;
1910 j--;
1913 /* ??? The innermost op should always be a MEM_REF and we already
1914 checked that the assignment to the lhs kills vr. Thus for
1915 aggregate copies using char[] types the vn_reference_op_eq
1916 may fail when comparing types for compatibility. But we really
1917 don't care here - further lookups with the rewritten operands
1918 will simply fail if we messed up types too badly. */
1919 HOST_WIDE_INT extra_off = 0;
1920 if (j == 0 && i >= 0
1921 && lhs_ops[0].opcode == MEM_REF
1922 && lhs_ops[0].off != -1)
1924 if (lhs_ops[0].off == vr->operands[i].off)
1925 i--, j--;
1926 else if (vr->operands[i].opcode == MEM_REF
1927 && vr->operands[i].off != -1)
1929 extra_off = vr->operands[i].off - lhs_ops[0].off;
1930 i--, j--;
1934 /* i now points to the first additional op.
1935 ??? LHS may not be completely contained in VR, one or more
1936 VIEW_CONVERT_EXPRs could be in its way. We could at least
1937 try handling outermost VIEW_CONVERT_EXPRs. */
1938 if (j != -1)
1939 return (void *)-1;
1941 /* Now re-write REF to be based on the rhs of the assignment. */
1942 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1944 /* Apply an extra offset to the inner MEM_REF of the RHS. */
1945 if (extra_off != 0)
1947 if (rhs.length () < 2
1948 || rhs[0].opcode != MEM_REF
1949 || rhs[0].off == -1)
1950 return (void *)-1;
1951 rhs[0].off += extra_off;
1952 rhs[0].op0 = int_const_binop (PLUS_EXPR, rhs[0].op0,
1953 build_int_cst (TREE_TYPE (rhs[0].op0),
1954 extra_off));
1957 /* We need to pre-pend vr->operands[0..i] to rhs. */
1958 vec<vn_reference_op_s> old = vr->operands;
1959 if (i + 1 + rhs.length () > vr->operands.length ())
1961 vr->operands.safe_grow (i + 1 + rhs.length ());
1962 if (old == shared_lookup_references)
1963 shared_lookup_references = vr->operands;
1965 else
1966 vr->operands.truncate (i + 1 + rhs.length ());
1967 FOR_EACH_VEC_ELT (rhs, j, vro)
1968 vr->operands[i + 1 + j] = *vro;
1969 vr->operands = valueize_refs (vr->operands);
1970 if (old == shared_lookup_references)
1971 shared_lookup_references = vr->operands;
1972 vr->hashcode = vn_reference_compute_hash (vr);
1974 /* Try folding the new reference to a constant. */
1975 tree val = fully_constant_vn_reference_p (vr);
1976 if (val)
1977 return vn_reference_lookup_or_insert_for_pieces
1978 (vuse, vr->set, vr->type, vr->operands, val);
1980 /* Adjust *ref from the new operands. */
1981 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1982 return (void *)-1;
1983 /* This can happen with bitfields. */
1984 if (ref->size != r.size)
1985 return (void *)-1;
1986 *ref = r;
1988 /* Do not update last seen VUSE after translating. */
1989 last_vuse_ptr = NULL;
1991 /* Keep looking for the adjusted *REF / VR pair. */
1992 return NULL;
1995 /* 6) For memcpy copies translate the reference through them if
1996 the copy kills ref. */
1997 else if (vn_walk_kind == VN_WALKREWRITE
1998 && is_gimple_reg_type (vr->type)
1999 /* ??? Handle BCOPY as well. */
2000 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
2001 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
2002 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
2003 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
2004 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
2005 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
2006 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
2007 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)))
2009 tree lhs, rhs;
2010 ao_ref r;
2011 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
2012 vn_reference_op_s op;
2013 HOST_WIDE_INT at;
2016 /* Only handle non-variable, addressable refs. */
2017 if (ref->size != maxsize
2018 || offset % BITS_PER_UNIT != 0
2019 || ref->size % BITS_PER_UNIT != 0)
2020 return (void *)-1;
2022 /* Extract a pointer base and an offset for the destination. */
2023 lhs = gimple_call_arg (def_stmt, 0);
2024 lhs_offset = 0;
2025 if (TREE_CODE (lhs) == SSA_NAME)
2026 lhs = SSA_VAL (lhs);
2027 if (TREE_CODE (lhs) == ADDR_EXPR)
2029 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
2030 &lhs_offset);
2031 if (!tem)
2032 return (void *)-1;
2033 if (TREE_CODE (tem) == MEM_REF
2034 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2036 lhs = TREE_OPERAND (tem, 0);
2037 lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2039 else if (DECL_P (tem))
2040 lhs = build_fold_addr_expr (tem);
2041 else
2042 return (void *)-1;
2044 if (TREE_CODE (lhs) != SSA_NAME
2045 && TREE_CODE (lhs) != ADDR_EXPR)
2046 return (void *)-1;
2048 /* Extract a pointer base and an offset for the source. */
2049 rhs = gimple_call_arg (def_stmt, 1);
2050 rhs_offset = 0;
2051 if (TREE_CODE (rhs) == SSA_NAME)
2052 rhs = SSA_VAL (rhs);
2053 if (TREE_CODE (rhs) == ADDR_EXPR)
2055 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
2056 &rhs_offset);
2057 if (!tem)
2058 return (void *)-1;
2059 if (TREE_CODE (tem) == MEM_REF
2060 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2062 rhs = TREE_OPERAND (tem, 0);
2063 rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2065 else if (DECL_P (tem))
2066 rhs = build_fold_addr_expr (tem);
2067 else
2068 return (void *)-1;
2070 if (TREE_CODE (rhs) != SSA_NAME
2071 && TREE_CODE (rhs) != ADDR_EXPR)
2072 return (void *)-1;
2074 copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2));
2076 /* The bases of the destination and the references have to agree. */
2077 if ((TREE_CODE (base) != MEM_REF
2078 && !DECL_P (base))
2079 || (TREE_CODE (base) == MEM_REF
2080 && (TREE_OPERAND (base, 0) != lhs
2081 || !tree_fits_uhwi_p (TREE_OPERAND (base, 1))))
2082 || (DECL_P (base)
2083 && (TREE_CODE (lhs) != ADDR_EXPR
2084 || TREE_OPERAND (lhs, 0) != base)))
2085 return (void *)-1;
2087 /* And the access has to be contained within the memcpy destination. */
2088 at = offset / BITS_PER_UNIT;
2089 if (TREE_CODE (base) == MEM_REF)
2090 at += tree_to_uhwi (TREE_OPERAND (base, 1));
2091 if (lhs_offset > at
2092 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
2093 return (void *)-1;
2095 /* Make room for 2 operands in the new reference. */
2096 if (vr->operands.length () < 2)
2098 vec<vn_reference_op_s> old = vr->operands;
2099 vr->operands.safe_grow_cleared (2);
2100 if (old == shared_lookup_references
2101 && vr->operands != old)
2102 shared_lookup_references = vr->operands;
2104 else
2105 vr->operands.truncate (2);
2107 /* The looked-through reference is a simple MEM_REF. */
2108 memset (&op, 0, sizeof (op));
2109 op.type = vr->type;
2110 op.opcode = MEM_REF;
2111 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
2112 op.off = at - lhs_offset + rhs_offset;
2113 vr->operands[0] = op;
2114 op.type = TREE_TYPE (rhs);
2115 op.opcode = TREE_CODE (rhs);
2116 op.op0 = rhs;
2117 op.off = -1;
2118 vr->operands[1] = op;
2119 vr->hashcode = vn_reference_compute_hash (vr);
2121 /* Adjust *ref from the new operands. */
2122 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2123 return (void *)-1;
2124 /* This can happen with bitfields. */
2125 if (ref->size != r.size)
2126 return (void *)-1;
2127 *ref = r;
2129 /* Do not update last seen VUSE after translating. */
2130 last_vuse_ptr = NULL;
2132 /* Keep looking for the adjusted *REF / VR pair. */
2133 return NULL;
2136 /* Bail out and stop walking. */
2137 return (void *)-1;
2140 /* Lookup a reference operation by it's parts, in the current hash table.
2141 Returns the resulting value number if it exists in the hash table,
2142 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2143 vn_reference_t stored in the hashtable if something is found. */
2145 tree
2146 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2147 vec<vn_reference_op_s> operands,
2148 vn_reference_t *vnresult, vn_lookup_kind kind)
2150 struct vn_reference_s vr1;
2151 vn_reference_t tmp;
2152 tree cst;
2154 if (!vnresult)
2155 vnresult = &tmp;
2156 *vnresult = NULL;
2158 vr1.vuse = vuse_ssa_val (vuse);
2159 shared_lookup_references.truncate (0);
2160 shared_lookup_references.safe_grow (operands.length ());
2161 memcpy (shared_lookup_references.address (),
2162 operands.address (),
2163 sizeof (vn_reference_op_s)
2164 * operands.length ());
2165 vr1.operands = operands = shared_lookup_references
2166 = valueize_refs (shared_lookup_references);
2167 vr1.type = type;
2168 vr1.set = set;
2169 vr1.hashcode = vn_reference_compute_hash (&vr1);
2170 if ((cst = fully_constant_vn_reference_p (&vr1)))
2171 return cst;
2173 vn_reference_lookup_1 (&vr1, vnresult);
2174 if (!*vnresult
2175 && kind != VN_NOWALK
2176 && vr1.vuse)
2178 ao_ref r;
2179 vn_walk_kind = kind;
2180 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2181 *vnresult =
2182 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2183 vn_reference_lookup_2,
2184 vn_reference_lookup_3,
2185 vuse_ssa_val, &vr1);
2186 gcc_checking_assert (vr1.operands == shared_lookup_references);
2189 if (*vnresult)
2190 return (*vnresult)->result;
2192 return NULL_TREE;
2195 /* Lookup OP in the current hash table, and return the resulting value
2196 number if it exists in the hash table. Return NULL_TREE if it does
2197 not exist in the hash table or if the result field of the structure
2198 was NULL.. VNRESULT will be filled in with the vn_reference_t
2199 stored in the hashtable if one exists. */
2201 tree
2202 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2203 vn_reference_t *vnresult)
2205 vec<vn_reference_op_s> operands;
2206 struct vn_reference_s vr1;
2207 tree cst;
2208 bool valuezied_anything;
2210 if (vnresult)
2211 *vnresult = NULL;
2213 vr1.vuse = vuse_ssa_val (vuse);
2214 vr1.operands = operands
2215 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2216 vr1.type = TREE_TYPE (op);
2217 vr1.set = get_alias_set (op);
2218 vr1.hashcode = vn_reference_compute_hash (&vr1);
2219 if ((cst = fully_constant_vn_reference_p (&vr1)))
2220 return cst;
2222 if (kind != VN_NOWALK
2223 && vr1.vuse)
2225 vn_reference_t wvnresult;
2226 ao_ref r;
2227 /* Make sure to use a valueized reference if we valueized anything.
2228 Otherwise preserve the full reference for advanced TBAA. */
2229 if (!valuezied_anything
2230 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2231 vr1.operands))
2232 ao_ref_init (&r, op);
2233 vn_walk_kind = kind;
2234 wvnresult =
2235 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2236 vn_reference_lookup_2,
2237 vn_reference_lookup_3,
2238 vuse_ssa_val, &vr1);
2239 gcc_checking_assert (vr1.operands == shared_lookup_references);
2240 if (wvnresult)
2242 if (vnresult)
2243 *vnresult = wvnresult;
2244 return wvnresult->result;
2247 return NULL_TREE;
2250 return vn_reference_lookup_1 (&vr1, vnresult);
2253 /* Lookup CALL in the current hash table and return the entry in
2254 *VNRESULT if found. Populates *VR for the hashtable lookup. */
2256 void
2257 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
2258 vn_reference_t vr)
2260 if (vnresult)
2261 *vnresult = NULL;
2263 tree vuse = gimple_vuse (call);
2265 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2266 vr->operands = valueize_shared_reference_ops_from_call (call);
2267 vr->type = gimple_expr_type (call);
2268 vr->set = 0;
2269 vr->hashcode = vn_reference_compute_hash (vr);
2270 vn_reference_lookup_1 (vr, vnresult);
2273 /* Insert OP into the current hash table with a value number of
2274 RESULT, and return the resulting reference structure we created. */
2276 static vn_reference_t
2277 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2279 vn_reference_s **slot;
2280 vn_reference_t vr1;
2281 bool tem;
2283 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2284 if (TREE_CODE (result) == SSA_NAME)
2285 vr1->value_id = VN_INFO (result)->value_id;
2286 else
2287 vr1->value_id = get_or_alloc_constant_value_id (result);
2288 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2289 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2290 vr1->type = TREE_TYPE (op);
2291 vr1->set = get_alias_set (op);
2292 vr1->hashcode = vn_reference_compute_hash (vr1);
2293 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2294 vr1->result_vdef = vdef;
2296 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2297 INSERT);
2299 /* Because we lookup stores using vuses, and value number failures
2300 using the vdefs (see visit_reference_op_store for how and why),
2301 it's possible that on failure we may try to insert an already
2302 inserted store. This is not wrong, there is no ssa name for a
2303 store that we could use as a differentiator anyway. Thus, unlike
2304 the other lookup functions, you cannot gcc_assert (!*slot)
2305 here. */
2307 /* But free the old slot in case of a collision. */
2308 if (*slot)
2309 free_reference (*slot);
2311 *slot = vr1;
2312 return vr1;
2315 /* Insert a reference by it's pieces into the current hash table with
2316 a value number of RESULT. Return the resulting reference
2317 structure we created. */
2319 vn_reference_t
2320 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2321 vec<vn_reference_op_s> operands,
2322 tree result, unsigned int value_id)
2325 vn_reference_s **slot;
2326 vn_reference_t vr1;
2328 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2329 vr1->value_id = value_id;
2330 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2331 vr1->operands = valueize_refs (operands);
2332 vr1->type = type;
2333 vr1->set = set;
2334 vr1->hashcode = vn_reference_compute_hash (vr1);
2335 if (result && TREE_CODE (result) == SSA_NAME)
2336 result = SSA_VAL (result);
2337 vr1->result = result;
2339 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2340 INSERT);
2342 /* At this point we should have all the things inserted that we have
2343 seen before, and we should never try inserting something that
2344 already exists. */
2345 gcc_assert (!*slot);
2346 if (*slot)
2347 free_reference (*slot);
2349 *slot = vr1;
2350 return vr1;
2353 /* Compute and return the hash value for nary operation VBO1. */
2355 static hashval_t
2356 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2358 inchash::hash hstate;
2359 unsigned i;
2361 for (i = 0; i < vno1->length; ++i)
2362 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2363 vno1->op[i] = SSA_VAL (vno1->op[i]);
2365 if (vno1->length == 2
2366 && commutative_tree_code (vno1->opcode)
2367 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2369 tree temp = vno1->op[0];
2370 vno1->op[0] = vno1->op[1];
2371 vno1->op[1] = temp;
2374 hstate.add_int (vno1->opcode);
2375 for (i = 0; i < vno1->length; ++i)
2376 inchash::add_expr (vno1->op[i], hstate);
2378 return hstate.end ();
2381 /* Compare nary operations VNO1 and VNO2 and return true if they are
2382 equivalent. */
2384 bool
2385 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2387 unsigned i;
2389 if (vno1->hashcode != vno2->hashcode)
2390 return false;
2392 if (vno1->length != vno2->length)
2393 return false;
2395 if (vno1->opcode != vno2->opcode
2396 || !types_compatible_p (vno1->type, vno2->type))
2397 return false;
2399 for (i = 0; i < vno1->length; ++i)
2400 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2401 return false;
2403 return true;
2406 /* Initialize VNO from the pieces provided. */
2408 static void
2409 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2410 enum tree_code code, tree type, tree *ops)
2412 vno->opcode = code;
2413 vno->length = length;
2414 vno->type = type;
2415 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2418 /* Initialize VNO from OP. */
2420 static void
2421 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2423 unsigned i;
2425 vno->opcode = TREE_CODE (op);
2426 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2427 vno->type = TREE_TYPE (op);
2428 for (i = 0; i < vno->length; ++i)
2429 vno->op[i] = TREE_OPERAND (op, i);
2432 /* Return the number of operands for a vn_nary ops structure from STMT. */
2434 static unsigned int
2435 vn_nary_length_from_stmt (gimple stmt)
2437 switch (gimple_assign_rhs_code (stmt))
2439 case REALPART_EXPR:
2440 case IMAGPART_EXPR:
2441 case VIEW_CONVERT_EXPR:
2442 return 1;
2444 case BIT_FIELD_REF:
2445 return 3;
2447 case CONSTRUCTOR:
2448 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2450 default:
2451 return gimple_num_ops (stmt) - 1;
2455 /* Initialize VNO from STMT. */
2457 static void
2458 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2460 unsigned i;
2462 vno->opcode = gimple_assign_rhs_code (stmt);
2463 vno->type = gimple_expr_type (stmt);
2464 switch (vno->opcode)
2466 case REALPART_EXPR:
2467 case IMAGPART_EXPR:
2468 case VIEW_CONVERT_EXPR:
2469 vno->length = 1;
2470 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2471 break;
2473 case BIT_FIELD_REF:
2474 vno->length = 3;
2475 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2476 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2477 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2478 break;
2480 case CONSTRUCTOR:
2481 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2482 for (i = 0; i < vno->length; ++i)
2483 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2484 break;
2486 default:
2487 gcc_checking_assert (!gimple_assign_single_p (stmt));
2488 vno->length = gimple_num_ops (stmt) - 1;
2489 for (i = 0; i < vno->length; ++i)
2490 vno->op[i] = gimple_op (stmt, i + 1);
2494 /* Compute the hashcode for VNO and look for it in the hash table;
2495 return the resulting value number if it exists in the hash table.
2496 Return NULL_TREE if it does not exist in the hash table or if the
2497 result field of the operation is NULL. VNRESULT will contain the
2498 vn_nary_op_t from the hashtable if it exists. */
2500 static tree
2501 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2503 vn_nary_op_s **slot;
2505 if (vnresult)
2506 *vnresult = NULL;
2508 vno->hashcode = vn_nary_op_compute_hash (vno);
2509 slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode,
2510 NO_INSERT);
2511 if (!slot && current_info == optimistic_info)
2512 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode,
2513 NO_INSERT);
2514 if (!slot)
2515 return NULL_TREE;
2516 if (vnresult)
2517 *vnresult = *slot;
2518 return (*slot)->result;
2521 /* Lookup a n-ary operation by its pieces and return the resulting value
2522 number if it exists in the hash table. Return NULL_TREE if it does
2523 not exist in the hash table or if the result field of the operation
2524 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2525 if it exists. */
2527 tree
2528 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2529 tree type, tree *ops, vn_nary_op_t *vnresult)
2531 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2532 sizeof_vn_nary_op (length));
2533 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2534 return vn_nary_op_lookup_1 (vno1, vnresult);
2537 /* Lookup OP in the current hash table, and return the resulting value
2538 number if it exists in the hash table. Return NULL_TREE if it does
2539 not exist in the hash table or if the result field of the operation
2540 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2541 if it exists. */
2543 tree
2544 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2546 vn_nary_op_t vno1
2547 = XALLOCAVAR (struct vn_nary_op_s,
2548 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2549 init_vn_nary_op_from_op (vno1, op);
2550 return vn_nary_op_lookup_1 (vno1, vnresult);
2553 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2554 value number if it exists in the hash table. Return NULL_TREE if
2555 it does not exist in the hash table. VNRESULT will contain the
2556 vn_nary_op_t from the hashtable if it exists. */
2558 tree
2559 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2561 vn_nary_op_t vno1
2562 = XALLOCAVAR (struct vn_nary_op_s,
2563 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2564 init_vn_nary_op_from_stmt (vno1, stmt);
2565 return vn_nary_op_lookup_1 (vno1, vnresult);
2568 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2570 static vn_nary_op_t
2571 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2573 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2576 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2577 obstack. */
2579 static vn_nary_op_t
2580 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2582 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2583 &current_info->nary_obstack);
2585 vno1->value_id = value_id;
2586 vno1->length = length;
2587 vno1->result = result;
2589 return vno1;
2592 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2593 VNO->HASHCODE first. */
2595 static vn_nary_op_t
2596 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
2597 bool compute_hash)
2599 vn_nary_op_s **slot;
2601 if (compute_hash)
2602 vno->hashcode = vn_nary_op_compute_hash (vno);
2604 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
2605 gcc_assert (!*slot);
2607 *slot = vno;
2608 return vno;
2611 /* Insert a n-ary operation into the current hash table using it's
2612 pieces. Return the vn_nary_op_t structure we created and put in
2613 the hashtable. */
2615 vn_nary_op_t
2616 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2617 tree type, tree *ops,
2618 tree result, unsigned int value_id)
2620 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2621 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2622 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2625 /* Insert OP into the current hash table with a value number of
2626 RESULT. Return the vn_nary_op_t structure we created and put in
2627 the hashtable. */
2629 vn_nary_op_t
2630 vn_nary_op_insert (tree op, tree result)
2632 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2633 vn_nary_op_t vno1;
2635 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2636 init_vn_nary_op_from_op (vno1, op);
2637 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2640 /* Insert the rhs of STMT into the current hash table with a value number of
2641 RESULT. */
2643 vn_nary_op_t
2644 vn_nary_op_insert_stmt (gimple stmt, tree result)
2646 vn_nary_op_t vno1
2647 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2648 result, VN_INFO (result)->value_id);
2649 init_vn_nary_op_from_stmt (vno1, stmt);
2650 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2653 /* Compute a hashcode for PHI operation VP1 and return it. */
2655 static inline hashval_t
2656 vn_phi_compute_hash (vn_phi_t vp1)
2658 inchash::hash hstate (vp1->block->index);
2659 int i;
2660 tree phi1op;
2661 tree type;
2663 /* If all PHI arguments are constants we need to distinguish
2664 the PHI node via its type. */
2665 type = vp1->type;
2666 hstate.merge_hash (vn_hash_type (type));
2668 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2670 if (phi1op == VN_TOP)
2671 continue;
2672 inchash::add_expr (phi1op, hstate);
2675 return hstate.end ();
2678 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2680 static int
2681 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2683 if (vp1->hashcode != vp2->hashcode)
2684 return false;
2686 if (vp1->block == vp2->block)
2688 int i;
2689 tree phi1op;
2691 /* If the PHI nodes do not have compatible types
2692 they are not the same. */
2693 if (!types_compatible_p (vp1->type, vp2->type))
2694 return false;
2696 /* Any phi in the same block will have it's arguments in the
2697 same edge order, because of how we store phi nodes. */
2698 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2700 tree phi2op = vp2->phiargs[i];
2701 if (phi1op == VN_TOP || phi2op == VN_TOP)
2702 continue;
2703 if (!expressions_equal_p (phi1op, phi2op))
2704 return false;
2706 return true;
2708 return false;
2711 static vec<tree> shared_lookup_phiargs;
2713 /* Lookup PHI in the current hash table, and return the resulting
2714 value number if it exists in the hash table. Return NULL_TREE if
2715 it does not exist in the hash table. */
2717 static tree
2718 vn_phi_lookup (gimple phi)
2720 vn_phi_s **slot;
2721 struct vn_phi_s vp1;
2722 unsigned i;
2724 shared_lookup_phiargs.truncate (0);
2726 /* Canonicalize the SSA_NAME's to their value number. */
2727 for (i = 0; i < gimple_phi_num_args (phi); i++)
2729 tree def = PHI_ARG_DEF (phi, i);
2730 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2731 shared_lookup_phiargs.safe_push (def);
2733 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2734 vp1.phiargs = shared_lookup_phiargs;
2735 vp1.block = gimple_bb (phi);
2736 vp1.hashcode = vn_phi_compute_hash (&vp1);
2737 slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2738 NO_INSERT);
2739 if (!slot && current_info == optimistic_info)
2740 slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2741 NO_INSERT);
2742 if (!slot)
2743 return NULL_TREE;
2744 return (*slot)->result;
2747 /* Insert PHI into the current hash table with a value number of
2748 RESULT. */
2750 static vn_phi_t
2751 vn_phi_insert (gimple phi, tree result)
2753 vn_phi_s **slot;
2754 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2755 unsigned i;
2756 vec<tree> args = vNULL;
2758 /* Canonicalize the SSA_NAME's to their value number. */
2759 for (i = 0; i < gimple_phi_num_args (phi); i++)
2761 tree def = PHI_ARG_DEF (phi, i);
2762 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2763 args.safe_push (def);
2765 vp1->value_id = VN_INFO (result)->value_id;
2766 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2767 vp1->phiargs = args;
2768 vp1->block = gimple_bb (phi);
2769 vp1->result = result;
2770 vp1->hashcode = vn_phi_compute_hash (vp1);
2772 slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2774 /* Because we iterate over phi operations more than once, it's
2775 possible the slot might already exist here, hence no assert.*/
2776 *slot = vp1;
2777 return vp1;
2781 /* Print set of components in strongly connected component SCC to OUT. */
2783 static void
2784 print_scc (FILE *out, vec<tree> scc)
2786 tree var;
2787 unsigned int i;
2789 fprintf (out, "SCC consists of:");
2790 FOR_EACH_VEC_ELT (scc, i, var)
2792 fprintf (out, " ");
2793 print_generic_expr (out, var, 0);
2795 fprintf (out, "\n");
2798 /* Set the value number of FROM to TO, return true if it has changed
2799 as a result. */
2801 static inline bool
2802 set_ssa_val_to (tree from, tree to)
2804 tree currval = SSA_VAL (from);
2805 HOST_WIDE_INT toff, coff;
2807 /* The only thing we allow as value numbers are ssa_names
2808 and invariants. So assert that here. We don't allow VN_TOP
2809 as visiting a stmt should produce a value-number other than
2810 that.
2811 ??? Still VN_TOP can happen for unreachable code, so force
2812 it to varying in that case. Not all code is prepared to
2813 get VN_TOP on valueization. */
2814 if (to == VN_TOP)
2816 if (dump_file && (dump_flags & TDF_DETAILS))
2817 fprintf (dump_file, "Forcing value number to varying on "
2818 "receiving VN_TOP\n");
2819 to = from;
2822 gcc_assert (to != NULL_TREE
2823 && ((TREE_CODE (to) == SSA_NAME
2824 && (to == from || SSA_VAL (to) == to))
2825 || is_gimple_min_invariant (to)));
2827 if (from != to)
2829 if (currval == from)
2831 if (dump_file && (dump_flags & TDF_DETAILS))
2833 fprintf (dump_file, "Not changing value number of ");
2834 print_generic_expr (dump_file, from, 0);
2835 fprintf (dump_file, " from VARYING to ");
2836 print_generic_expr (dump_file, to, 0);
2837 fprintf (dump_file, "\n");
2839 return false;
2841 else if (TREE_CODE (to) == SSA_NAME
2842 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2843 to = from;
2846 if (dump_file && (dump_flags & TDF_DETAILS))
2848 fprintf (dump_file, "Setting value number of ");
2849 print_generic_expr (dump_file, from, 0);
2850 fprintf (dump_file, " to ");
2851 print_generic_expr (dump_file, to, 0);
2854 if (currval != to
2855 && !operand_equal_p (currval, to, 0)
2856 /* ??? For addresses involving volatile objects or types operand_equal_p
2857 does not reliably detect ADDR_EXPRs as equal. We know we are only
2858 getting invariant gimple addresses here, so can use
2859 get_addr_base_and_unit_offset to do this comparison. */
2860 && !(TREE_CODE (currval) == ADDR_EXPR
2861 && TREE_CODE (to) == ADDR_EXPR
2862 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2863 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2864 && coff == toff))
2866 VN_INFO (from)->valnum = to;
2867 if (dump_file && (dump_flags & TDF_DETAILS))
2868 fprintf (dump_file, " (changed)\n");
2869 return true;
2871 if (dump_file && (dump_flags & TDF_DETAILS))
2872 fprintf (dump_file, "\n");
2873 return false;
2876 /* Mark as processed all the definitions in the defining stmt of USE, or
2877 the USE itself. */
2879 static void
2880 mark_use_processed (tree use)
2882 ssa_op_iter iter;
2883 def_operand_p defp;
2884 gimple stmt = SSA_NAME_DEF_STMT (use);
2886 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2888 VN_INFO (use)->use_processed = true;
2889 return;
2892 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2894 tree def = DEF_FROM_PTR (defp);
2896 VN_INFO (def)->use_processed = true;
2900 /* Set all definitions in STMT to value number to themselves.
2901 Return true if a value number changed. */
2903 static bool
2904 defs_to_varying (gimple stmt)
2906 bool changed = false;
2907 ssa_op_iter iter;
2908 def_operand_p defp;
2910 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2912 tree def = DEF_FROM_PTR (defp);
2913 changed |= set_ssa_val_to (def, def);
2915 return changed;
2918 static bool expr_has_constants (tree expr);
2920 /* Visit a copy between LHS and RHS, return true if the value number
2921 changed. */
2923 static bool
2924 visit_copy (tree lhs, tree rhs)
2926 /* The copy may have a more interesting constant filled expression
2927 (we don't, since we know our RHS is just an SSA name). */
2928 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2929 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2931 /* And finally valueize. */
2932 rhs = SSA_VAL (rhs);
2934 return set_ssa_val_to (lhs, rhs);
2937 /* Visit a nary operator RHS, value number it, and return true if the
2938 value number of LHS has changed as a result. */
2940 static bool
2941 visit_nary_op (tree lhs, gimple stmt)
2943 bool changed = false;
2944 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2946 if (result)
2947 changed = set_ssa_val_to (lhs, result);
2948 else
2950 changed = set_ssa_val_to (lhs, lhs);
2951 vn_nary_op_insert_stmt (stmt, lhs);
2954 return changed;
2957 /* Visit a call STMT storing into LHS. Return true if the value number
2958 of the LHS has changed as a result. */
2960 static bool
2961 visit_reference_op_call (tree lhs, gcall *stmt)
2963 bool changed = false;
2964 struct vn_reference_s vr1;
2965 vn_reference_t vnresult = NULL;
2966 tree vdef = gimple_vdef (stmt);
2968 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2969 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2970 lhs = NULL_TREE;
2972 vn_reference_lookup_call (stmt, &vnresult, &vr1);
2973 if (vnresult)
2975 if (vnresult->result_vdef && vdef)
2976 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2978 if (!vnresult->result && lhs)
2979 vnresult->result = lhs;
2981 if (vnresult->result && lhs)
2983 changed |= set_ssa_val_to (lhs, vnresult->result);
2985 if (VN_INFO (vnresult->result)->has_constants)
2986 VN_INFO (lhs)->has_constants = true;
2989 else
2991 vn_reference_t vr2;
2992 vn_reference_s **slot;
2993 if (vdef)
2994 changed |= set_ssa_val_to (vdef, vdef);
2995 if (lhs)
2996 changed |= set_ssa_val_to (lhs, lhs);
2997 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2998 vr2->vuse = vr1.vuse;
2999 /* As we are not walking the virtual operand chain we know the
3000 shared_lookup_references are still original so we can re-use
3001 them here. */
3002 vr2->operands = vr1.operands.copy ();
3003 vr2->type = vr1.type;
3004 vr2->set = vr1.set;
3005 vr2->hashcode = vr1.hashcode;
3006 vr2->result = lhs;
3007 vr2->result_vdef = vdef;
3008 slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode,
3009 INSERT);
3010 gcc_assert (!*slot);
3011 *slot = vr2;
3014 return changed;
3017 /* Visit a load from a reference operator RHS, part of STMT, value number it,
3018 and return true if the value number of the LHS has changed as a result. */
3020 static bool
3021 visit_reference_op_load (tree lhs, tree op, gimple stmt)
3023 bool changed = false;
3024 tree last_vuse;
3025 tree result;
3027 last_vuse = gimple_vuse (stmt);
3028 last_vuse_ptr = &last_vuse;
3029 result = vn_reference_lookup (op, gimple_vuse (stmt),
3030 default_vn_walk_kind, NULL);
3031 last_vuse_ptr = NULL;
3033 /* We handle type-punning through unions by value-numbering based
3034 on offset and size of the access. Be prepared to handle a
3035 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
3036 if (result
3037 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
3039 /* We will be setting the value number of lhs to the value number
3040 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
3041 So first simplify and lookup this expression to see if it
3042 is already available. */
3043 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
3044 if ((CONVERT_EXPR_P (val)
3045 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
3046 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
3048 tree tem = vn_get_expr_for (TREE_OPERAND (val, 0));
3049 if ((CONVERT_EXPR_P (tem)
3050 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
3051 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
3052 TREE_TYPE (val), tem)))
3053 val = tem;
3055 result = val;
3056 if (!is_gimple_min_invariant (val)
3057 && TREE_CODE (val) != SSA_NAME)
3058 result = vn_nary_op_lookup (val, NULL);
3059 /* If the expression is not yet available, value-number lhs to
3060 a new SSA_NAME we create. */
3061 if (!result)
3063 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
3064 "vntemp");
3065 /* Initialize value-number information properly. */
3066 VN_INFO_GET (result)->valnum = result;
3067 VN_INFO (result)->value_id = get_next_value_id ();
3068 VN_INFO (result)->expr = val;
3069 VN_INFO (result)->has_constants = expr_has_constants (val);
3070 VN_INFO (result)->needs_insertion = true;
3071 /* As all "inserted" statements are singleton SCCs, insert
3072 to the valid table. This is strictly needed to
3073 avoid re-generating new value SSA_NAMEs for the same
3074 expression during SCC iteration over and over (the
3075 optimistic table gets cleared after each iteration).
3076 We do not need to insert into the optimistic table, as
3077 lookups there will fall back to the valid table. */
3078 if (current_info == optimistic_info)
3080 current_info = valid_info;
3081 vn_nary_op_insert (val, result);
3082 current_info = optimistic_info;
3084 else
3085 vn_nary_op_insert (val, result);
3086 if (dump_file && (dump_flags & TDF_DETAILS))
3088 fprintf (dump_file, "Inserting name ");
3089 print_generic_expr (dump_file, result, 0);
3090 fprintf (dump_file, " for expression ");
3091 print_generic_expr (dump_file, val, 0);
3092 fprintf (dump_file, "\n");
3097 if (result)
3099 changed = set_ssa_val_to (lhs, result);
3100 if (TREE_CODE (result) == SSA_NAME
3101 && VN_INFO (result)->has_constants)
3103 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
3104 VN_INFO (lhs)->has_constants = true;
3107 else
3109 changed = set_ssa_val_to (lhs, lhs);
3110 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3113 return changed;
3117 /* Visit a store to a reference operator LHS, part of STMT, value number it,
3118 and return true if the value number of the LHS has changed as a result. */
3120 static bool
3121 visit_reference_op_store (tree lhs, tree op, gimple stmt)
3123 bool changed = false;
3124 vn_reference_t vnresult = NULL;
3125 tree result, assign;
3126 bool resultsame = false;
3127 tree vuse = gimple_vuse (stmt);
3128 tree vdef = gimple_vdef (stmt);
3130 if (TREE_CODE (op) == SSA_NAME)
3131 op = SSA_VAL (op);
3133 /* First we want to lookup using the *vuses* from the store and see
3134 if there the last store to this location with the same address
3135 had the same value.
3137 The vuses represent the memory state before the store. If the
3138 memory state, address, and value of the store is the same as the
3139 last store to this location, then this store will produce the
3140 same memory state as that store.
3142 In this case the vdef versions for this store are value numbered to those
3143 vuse versions, since they represent the same memory state after
3144 this store.
3146 Otherwise, the vdefs for the store are used when inserting into
3147 the table, since the store generates a new memory state. */
3149 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
3151 if (result)
3153 if (TREE_CODE (result) == SSA_NAME)
3154 result = SSA_VAL (result);
3155 resultsame = expressions_equal_p (result, op);
3158 if ((!result || !resultsame)
3159 /* Only perform the following when being called from PRE
3160 which embeds tail merging. */
3161 && default_vn_walk_kind == VN_WALK)
3163 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3164 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
3165 if (vnresult)
3167 VN_INFO (vdef)->use_processed = true;
3168 return set_ssa_val_to (vdef, vnresult->result_vdef);
3172 if (!result || !resultsame)
3174 if (dump_file && (dump_flags & TDF_DETAILS))
3176 fprintf (dump_file, "No store match\n");
3177 fprintf (dump_file, "Value numbering store ");
3178 print_generic_expr (dump_file, lhs, 0);
3179 fprintf (dump_file, " to ");
3180 print_generic_expr (dump_file, op, 0);
3181 fprintf (dump_file, "\n");
3183 /* Have to set value numbers before insert, since insert is
3184 going to valueize the references in-place. */
3185 if (vdef)
3187 changed |= set_ssa_val_to (vdef, vdef);
3190 /* Do not insert structure copies into the tables. */
3191 if (is_gimple_min_invariant (op)
3192 || is_gimple_reg (op))
3193 vn_reference_insert (lhs, op, vdef, NULL);
3195 /* Only perform the following when being called from PRE
3196 which embeds tail merging. */
3197 if (default_vn_walk_kind == VN_WALK)
3199 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3200 vn_reference_insert (assign, lhs, vuse, vdef);
3203 else
3205 /* We had a match, so value number the vdef to have the value
3206 number of the vuse it came from. */
3208 if (dump_file && (dump_flags & TDF_DETAILS))
3209 fprintf (dump_file, "Store matched earlier value,"
3210 "value numbering store vdefs to matching vuses.\n");
3212 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3215 return changed;
3218 /* Visit and value number PHI, return true if the value number
3219 changed. */
3221 static bool
3222 visit_phi (gimple phi)
3224 bool changed = false;
3225 tree result;
3226 tree sameval = VN_TOP;
3227 bool allsame = true;
3229 /* TODO: We could check for this in init_sccvn, and replace this
3230 with a gcc_assert. */
3231 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3232 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3234 /* See if all non-TOP arguments have the same value. TOP is
3235 equivalent to everything, so we can ignore it. */
3236 edge_iterator ei;
3237 edge e;
3238 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3239 if (e->flags & EDGE_EXECUTABLE)
3241 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3243 if (TREE_CODE (def) == SSA_NAME)
3244 def = SSA_VAL (def);
3245 if (def == VN_TOP)
3246 continue;
3247 if (sameval == VN_TOP)
3249 sameval = def;
3251 else
3253 if (!expressions_equal_p (def, sameval))
3255 allsame = false;
3256 break;
3261 /* If all value numbered to the same value, the phi node has that
3262 value. */
3263 if (allsame)
3264 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3266 /* Otherwise, see if it is equivalent to a phi node in this block. */
3267 result = vn_phi_lookup (phi);
3268 if (result)
3269 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3270 else
3272 vn_phi_insert (phi, PHI_RESULT (phi));
3273 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3274 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3275 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3278 return changed;
3281 /* Return true if EXPR contains constants. */
3283 static bool
3284 expr_has_constants (tree expr)
3286 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3288 case tcc_unary:
3289 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3291 case tcc_binary:
3292 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3293 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3294 /* Constants inside reference ops are rarely interesting, but
3295 it can take a lot of looking to find them. */
3296 case tcc_reference:
3297 case tcc_declaration:
3298 return false;
3299 default:
3300 return is_gimple_min_invariant (expr);
3302 return false;
3305 /* Return true if STMT contains constants. */
3307 static bool
3308 stmt_has_constants (gimple stmt)
3310 tree tem;
3312 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3313 return false;
3315 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3317 case GIMPLE_TERNARY_RHS:
3318 tem = gimple_assign_rhs3 (stmt);
3319 if (TREE_CODE (tem) == SSA_NAME)
3320 tem = SSA_VAL (tem);
3321 if (is_gimple_min_invariant (tem))
3322 return true;
3323 /* Fallthru. */
3325 case GIMPLE_BINARY_RHS:
3326 tem = gimple_assign_rhs2 (stmt);
3327 if (TREE_CODE (tem) == SSA_NAME)
3328 tem = SSA_VAL (tem);
3329 if (is_gimple_min_invariant (tem))
3330 return true;
3331 /* Fallthru. */
3333 case GIMPLE_SINGLE_RHS:
3334 /* Constants inside reference ops are rarely interesting, but
3335 it can take a lot of looking to find them. */
3336 case GIMPLE_UNARY_RHS:
3337 tem = gimple_assign_rhs1 (stmt);
3338 if (TREE_CODE (tem) == SSA_NAME)
3339 tem = SSA_VAL (tem);
3340 return is_gimple_min_invariant (tem);
3342 default:
3343 gcc_unreachable ();
3345 return false;
3348 /* Simplify the binary expression RHS, and return the result if
3349 simplified. */
3351 static tree
3352 simplify_binary_expression (gimple stmt)
3354 tree result = NULL_TREE;
3355 tree op0 = gimple_assign_rhs1 (stmt);
3356 tree op1 = gimple_assign_rhs2 (stmt);
3357 enum tree_code code = gimple_assign_rhs_code (stmt);
3359 /* This will not catch every single case we could combine, but will
3360 catch those with constants. The goal here is to simultaneously
3361 combine constants between expressions, but avoid infinite
3362 expansion of expressions during simplification. */
3363 op0 = vn_valueize (op0);
3364 if (TREE_CODE (op0) == SSA_NAME
3365 && (VN_INFO (op0)->has_constants
3366 || TREE_CODE_CLASS (code) == tcc_comparison
3367 || code == COMPLEX_EXPR))
3368 op0 = vn_get_expr_for (op0);
3370 op1 = vn_valueize (op1);
3371 if (TREE_CODE (op1) == SSA_NAME
3372 && (VN_INFO (op1)->has_constants
3373 || code == COMPLEX_EXPR))
3374 op1 = vn_get_expr_for (op1);
3376 /* Pointer plus constant can be represented as invariant address.
3377 Do so to allow further propatation, see also tree forwprop. */
3378 if (code == POINTER_PLUS_EXPR
3379 && tree_fits_uhwi_p (op1)
3380 && TREE_CODE (op0) == ADDR_EXPR
3381 && is_gimple_min_invariant (op0))
3382 return build_invariant_address (TREE_TYPE (op0),
3383 TREE_OPERAND (op0, 0),
3384 tree_to_uhwi (op1));
3386 /* Avoid folding if nothing changed. */
3387 if (op0 == gimple_assign_rhs1 (stmt)
3388 && op1 == gimple_assign_rhs2 (stmt))
3389 return NULL_TREE;
3391 fold_defer_overflow_warnings ();
3393 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3394 if (result)
3395 STRIP_USELESS_TYPE_CONVERSION (result);
3397 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3398 stmt, 0);
3400 /* Make sure result is not a complex expression consisting
3401 of operators of operators (IE (a + b) + (a + c))
3402 Otherwise, we will end up with unbounded expressions if
3403 fold does anything at all. */
3404 if (result && valid_gimple_rhs_p (result))
3405 return result;
3407 return NULL_TREE;
3410 /* Simplify the unary expression RHS, and return the result if
3411 simplified. */
3413 static tree
3414 simplify_unary_expression (gassign *stmt)
3416 tree result = NULL_TREE;
3417 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3418 enum tree_code code = gimple_assign_rhs_code (stmt);
3420 /* We handle some tcc_reference codes here that are all
3421 GIMPLE_ASSIGN_SINGLE codes. */
3422 if (code == REALPART_EXPR
3423 || code == IMAGPART_EXPR
3424 || code == VIEW_CONVERT_EXPR
3425 || code == BIT_FIELD_REF)
3426 op0 = TREE_OPERAND (op0, 0);
3428 orig_op0 = op0;
3429 op0 = vn_valueize (op0);
3430 if (TREE_CODE (op0) == SSA_NAME)
3432 if (VN_INFO (op0)->has_constants)
3433 op0 = vn_get_expr_for (op0);
3434 else if (CONVERT_EXPR_CODE_P (code)
3435 || code == REALPART_EXPR
3436 || code == IMAGPART_EXPR
3437 || code == VIEW_CONVERT_EXPR
3438 || code == BIT_FIELD_REF)
3440 /* We want to do tree-combining on conversion-like expressions.
3441 Make sure we feed only SSA_NAMEs or constants to fold though. */
3442 tree tem = vn_get_expr_for (op0);
3443 if (UNARY_CLASS_P (tem)
3444 || BINARY_CLASS_P (tem)
3445 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3446 || TREE_CODE (tem) == SSA_NAME
3447 || TREE_CODE (tem) == CONSTRUCTOR
3448 || is_gimple_min_invariant (tem))
3449 op0 = tem;
3453 /* Avoid folding if nothing changed, but remember the expression. */
3454 if (op0 == orig_op0)
3455 return NULL_TREE;
3457 if (code == BIT_FIELD_REF)
3459 tree rhs = gimple_assign_rhs1 (stmt);
3460 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3461 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3463 else
3464 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3465 if (result)
3467 STRIP_USELESS_TYPE_CONVERSION (result);
3468 if (valid_gimple_rhs_p (result))
3469 return result;
3472 return NULL_TREE;
3475 /* Try to simplify RHS using equivalences and constant folding. */
3477 static tree
3478 try_to_simplify (gassign *stmt)
3480 enum tree_code code = gimple_assign_rhs_code (stmt);
3481 tree tem;
3483 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3484 in this case, there is no point in doing extra work. */
3485 if (code == SSA_NAME)
3486 return NULL_TREE;
3488 /* First try constant folding based on our current lattice. */
3489 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
3490 if (tem
3491 && (TREE_CODE (tem) == SSA_NAME
3492 || is_gimple_min_invariant (tem)))
3493 return tem;
3495 /* If that didn't work try combining multiple statements. */
3496 switch (TREE_CODE_CLASS (code))
3498 case tcc_reference:
3499 /* Fallthrough for some unary codes that can operate on registers. */
3500 if (!(code == REALPART_EXPR
3501 || code == IMAGPART_EXPR
3502 || code == VIEW_CONVERT_EXPR
3503 || code == BIT_FIELD_REF))
3504 break;
3505 /* We could do a little more with unary ops, if they expand
3506 into binary ops, but it's debatable whether it is worth it. */
3507 case tcc_unary:
3508 return simplify_unary_expression (stmt);
3510 case tcc_comparison:
3511 case tcc_binary:
3512 return simplify_binary_expression (stmt);
3514 default:
3515 break;
3518 return NULL_TREE;
3521 /* Visit and value number USE, return true if the value number
3522 changed. */
3524 static bool
3525 visit_use (tree use)
3527 bool changed = false;
3528 gimple stmt = SSA_NAME_DEF_STMT (use);
3530 mark_use_processed (use);
3532 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3533 if (dump_file && (dump_flags & TDF_DETAILS)
3534 && !SSA_NAME_IS_DEFAULT_DEF (use))
3536 fprintf (dump_file, "Value numbering ");
3537 print_generic_expr (dump_file, use, 0);
3538 fprintf (dump_file, " stmt = ");
3539 print_gimple_stmt (dump_file, stmt, 0, 0);
3542 /* Handle uninitialized uses. */
3543 if (SSA_NAME_IS_DEFAULT_DEF (use))
3544 changed = set_ssa_val_to (use, use);
3545 else
3547 if (gimple_code (stmt) == GIMPLE_PHI)
3548 changed = visit_phi (stmt);
3549 else if (gimple_has_volatile_ops (stmt))
3550 changed = defs_to_varying (stmt);
3551 else if (is_gimple_assign (stmt))
3553 enum tree_code code = gimple_assign_rhs_code (stmt);
3554 tree lhs = gimple_assign_lhs (stmt);
3555 tree rhs1 = gimple_assign_rhs1 (stmt);
3556 tree simplified;
3558 /* Shortcut for copies. Simplifying copies is pointless,
3559 since we copy the expression and value they represent. */
3560 if (code == SSA_NAME
3561 && TREE_CODE (lhs) == SSA_NAME)
3563 changed = visit_copy (lhs, rhs1);
3564 goto done;
3566 simplified = try_to_simplify (as_a <gassign *> (stmt));
3567 if (simplified)
3569 if (dump_file && (dump_flags & TDF_DETAILS))
3571 fprintf (dump_file, "RHS ");
3572 print_gimple_expr (dump_file, stmt, 0, 0);
3573 fprintf (dump_file, " simplified to ");
3574 print_generic_expr (dump_file, simplified, 0);
3575 if (TREE_CODE (lhs) == SSA_NAME)
3576 fprintf (dump_file, " has constants %d\n",
3577 expr_has_constants (simplified));
3578 else
3579 fprintf (dump_file, "\n");
3582 /* Setting value numbers to constants will occasionally
3583 screw up phi congruence because constants are not
3584 uniquely associated with a single ssa name that can be
3585 looked up. */
3586 if (simplified
3587 && is_gimple_min_invariant (simplified)
3588 && TREE_CODE (lhs) == SSA_NAME)
3590 VN_INFO (lhs)->expr = simplified;
3591 VN_INFO (lhs)->has_constants = true;
3592 changed = set_ssa_val_to (lhs, simplified);
3593 goto done;
3595 else if (simplified
3596 && TREE_CODE (simplified) == SSA_NAME
3597 && TREE_CODE (lhs) == SSA_NAME)
3599 changed = visit_copy (lhs, simplified);
3600 goto done;
3602 else if (simplified)
3604 if (TREE_CODE (lhs) == SSA_NAME)
3606 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3607 /* We have to unshare the expression or else
3608 valuizing may change the IL stream. */
3609 VN_INFO (lhs)->expr = unshare_expr (simplified);
3612 else if (stmt_has_constants (stmt)
3613 && TREE_CODE (lhs) == SSA_NAME)
3614 VN_INFO (lhs)->has_constants = true;
3615 else if (TREE_CODE (lhs) == SSA_NAME)
3617 /* We reset expr and constantness here because we may
3618 have been value numbering optimistically, and
3619 iterating. They may become non-constant in this case,
3620 even if they were optimistically constant. */
3622 VN_INFO (lhs)->has_constants = false;
3623 VN_INFO (lhs)->expr = NULL_TREE;
3626 if ((TREE_CODE (lhs) == SSA_NAME
3627 /* We can substitute SSA_NAMEs that are live over
3628 abnormal edges with their constant value. */
3629 && !(gimple_assign_copy_p (stmt)
3630 && is_gimple_min_invariant (rhs1))
3631 && !(simplified
3632 && is_gimple_min_invariant (simplified))
3633 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3634 /* Stores or copies from SSA_NAMEs that are live over
3635 abnormal edges are a problem. */
3636 || (code == SSA_NAME
3637 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3638 changed = defs_to_varying (stmt);
3639 else if (REFERENCE_CLASS_P (lhs)
3640 || DECL_P (lhs))
3641 changed = visit_reference_op_store (lhs, rhs1, stmt);
3642 else if (TREE_CODE (lhs) == SSA_NAME)
3644 if ((gimple_assign_copy_p (stmt)
3645 && is_gimple_min_invariant (rhs1))
3646 || (simplified
3647 && is_gimple_min_invariant (simplified)))
3649 VN_INFO (lhs)->has_constants = true;
3650 if (simplified)
3651 changed = set_ssa_val_to (lhs, simplified);
3652 else
3653 changed = set_ssa_val_to (lhs, rhs1);
3655 else
3657 /* First try to lookup the simplified expression. */
3658 if (simplified)
3660 enum gimple_rhs_class rhs_class;
3663 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3664 if ((rhs_class == GIMPLE_UNARY_RHS
3665 || rhs_class == GIMPLE_BINARY_RHS
3666 || rhs_class == GIMPLE_TERNARY_RHS)
3667 && valid_gimple_rhs_p (simplified))
3669 tree result = vn_nary_op_lookup (simplified, NULL);
3670 if (result)
3672 changed = set_ssa_val_to (lhs, result);
3673 goto done;
3678 /* Otherwise visit the original statement. */
3679 switch (vn_get_stmt_kind (stmt))
3681 case VN_NARY:
3682 changed = visit_nary_op (lhs, stmt);
3683 break;
3684 case VN_REFERENCE:
3685 changed = visit_reference_op_load (lhs, rhs1, stmt);
3686 break;
3687 default:
3688 changed = defs_to_varying (stmt);
3689 break;
3693 else
3694 changed = defs_to_varying (stmt);
3696 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
3698 tree lhs = gimple_call_lhs (stmt);
3699 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3701 /* Try constant folding based on our current lattice. */
3702 tree simplified = gimple_fold_stmt_to_constant_1 (stmt,
3703 vn_valueize);
3704 if (simplified)
3706 if (dump_file && (dump_flags & TDF_DETAILS))
3708 fprintf (dump_file, "call ");
3709 print_gimple_expr (dump_file, stmt, 0, 0);
3710 fprintf (dump_file, " simplified to ");
3711 print_generic_expr (dump_file, simplified, 0);
3712 if (TREE_CODE (lhs) == SSA_NAME)
3713 fprintf (dump_file, " has constants %d\n",
3714 expr_has_constants (simplified));
3715 else
3716 fprintf (dump_file, "\n");
3719 /* Setting value numbers to constants will occasionally
3720 screw up phi congruence because constants are not
3721 uniquely associated with a single ssa name that can be
3722 looked up. */
3723 if (simplified
3724 && is_gimple_min_invariant (simplified))
3726 VN_INFO (lhs)->expr = simplified;
3727 VN_INFO (lhs)->has_constants = true;
3728 changed = set_ssa_val_to (lhs, simplified);
3729 if (gimple_vdef (stmt))
3730 changed |= set_ssa_val_to (gimple_vdef (stmt),
3731 SSA_VAL (gimple_vuse (stmt)));
3732 goto done;
3734 else if (simplified
3735 && TREE_CODE (simplified) == SSA_NAME)
3737 changed = visit_copy (lhs, simplified);
3738 if (gimple_vdef (stmt))
3739 changed |= set_ssa_val_to (gimple_vdef (stmt),
3740 SSA_VAL (gimple_vuse (stmt)));
3741 goto done;
3743 else
3745 if (stmt_has_constants (stmt))
3746 VN_INFO (lhs)->has_constants = true;
3747 else
3749 /* We reset expr and constantness here because we may
3750 have been value numbering optimistically, and
3751 iterating. They may become non-constant in this case,
3752 even if they were optimistically constant. */
3753 VN_INFO (lhs)->has_constants = false;
3754 VN_INFO (lhs)->expr = NULL_TREE;
3757 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3759 changed = defs_to_varying (stmt);
3760 goto done;
3765 if (!gimple_call_internal_p (stmt)
3766 && (/* Calls to the same function with the same vuse
3767 and the same operands do not necessarily return the same
3768 value, unless they're pure or const. */
3769 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3770 /* If calls have a vdef, subsequent calls won't have
3771 the same incoming vuse. So, if 2 calls with vdef have the
3772 same vuse, we know they're not subsequent.
3773 We can value number 2 calls to the same function with the
3774 same vuse and the same operands which are not subsequent
3775 the same, because there is no code in the program that can
3776 compare the 2 values... */
3777 || (gimple_vdef (stmt)
3778 /* ... unless the call returns a pointer which does
3779 not alias with anything else. In which case the
3780 information that the values are distinct are encoded
3781 in the IL. */
3782 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
3783 /* Only perform the following when being called from PRE
3784 which embeds tail merging. */
3785 && default_vn_walk_kind == VN_WALK)))
3786 changed = visit_reference_op_call (lhs, call_stmt);
3787 else
3788 changed = defs_to_varying (stmt);
3790 else
3791 changed = defs_to_varying (stmt);
3793 done:
3794 return changed;
3797 /* Compare two operands by reverse postorder index */
3799 static int
3800 compare_ops (const void *pa, const void *pb)
3802 const tree opa = *((const tree *)pa);
3803 const tree opb = *((const tree *)pb);
3804 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3805 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3806 basic_block bba;
3807 basic_block bbb;
3809 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3810 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3811 else if (gimple_nop_p (opstmta))
3812 return -1;
3813 else if (gimple_nop_p (opstmtb))
3814 return 1;
3816 bba = gimple_bb (opstmta);
3817 bbb = gimple_bb (opstmtb);
3819 if (!bba && !bbb)
3820 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3821 else if (!bba)
3822 return -1;
3823 else if (!bbb)
3824 return 1;
3826 if (bba == bbb)
3828 if (gimple_code (opstmta) == GIMPLE_PHI
3829 && gimple_code (opstmtb) == GIMPLE_PHI)
3830 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3831 else if (gimple_code (opstmta) == GIMPLE_PHI)
3832 return -1;
3833 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3834 return 1;
3835 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3836 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3837 else
3838 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3840 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3843 /* Sort an array containing members of a strongly connected component
3844 SCC so that the members are ordered by RPO number.
3845 This means that when the sort is complete, iterating through the
3846 array will give you the members in RPO order. */
3848 static void
3849 sort_scc (vec<tree> scc)
3851 scc.qsort (compare_ops);
3854 /* Insert the no longer used nary ONARY to the hash INFO. */
3856 static void
3857 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3859 size_t size = sizeof_vn_nary_op (onary->length);
3860 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3861 &info->nary_obstack);
3862 memcpy (nary, onary, size);
3863 vn_nary_op_insert_into (nary, info->nary, false);
3866 /* Insert the no longer used phi OPHI to the hash INFO. */
3868 static void
3869 copy_phi (vn_phi_t ophi, vn_tables_t info)
3871 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3872 vn_phi_s **slot;
3873 memcpy (phi, ophi, sizeof (*phi));
3874 ophi->phiargs.create (0);
3875 slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT);
3876 gcc_assert (!*slot);
3877 *slot = phi;
3880 /* Insert the no longer used reference OREF to the hash INFO. */
3882 static void
3883 copy_reference (vn_reference_t oref, vn_tables_t info)
3885 vn_reference_t ref;
3886 vn_reference_s **slot;
3887 ref = (vn_reference_t) pool_alloc (info->references_pool);
3888 memcpy (ref, oref, sizeof (*ref));
3889 oref->operands.create (0);
3890 slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT);
3891 if (*slot)
3892 free_reference (*slot);
3893 *slot = ref;
3896 /* Process a strongly connected component in the SSA graph. */
3898 static void
3899 process_scc (vec<tree> scc)
3901 tree var;
3902 unsigned int i;
3903 unsigned int iterations = 0;
3904 bool changed = true;
3905 vn_nary_op_iterator_type hin;
3906 vn_phi_iterator_type hip;
3907 vn_reference_iterator_type hir;
3908 vn_nary_op_t nary;
3909 vn_phi_t phi;
3910 vn_reference_t ref;
3912 /* If the SCC has a single member, just visit it. */
3913 if (scc.length () == 1)
3915 tree use = scc[0];
3916 if (VN_INFO (use)->use_processed)
3917 return;
3918 /* We need to make sure it doesn't form a cycle itself, which can
3919 happen for self-referential PHI nodes. In that case we would
3920 end up inserting an expression with VN_TOP operands into the
3921 valid table which makes us derive bogus equivalences later.
3922 The cheapest way to check this is to assume it for all PHI nodes. */
3923 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3924 /* Fallthru to iteration. */ ;
3925 else
3927 visit_use (use);
3928 return;
3932 if (dump_file && (dump_flags & TDF_DETAILS))
3933 print_scc (dump_file, scc);
3935 /* Iterate over the SCC with the optimistic table until it stops
3936 changing. */
3937 current_info = optimistic_info;
3938 while (changed)
3940 changed = false;
3941 iterations++;
3942 if (dump_file && (dump_flags & TDF_DETAILS))
3943 fprintf (dump_file, "Starting iteration %d\n", iterations);
3944 /* As we are value-numbering optimistically we have to
3945 clear the expression tables and the simplified expressions
3946 in each iteration until we converge. */
3947 optimistic_info->nary->empty ();
3948 optimistic_info->phis->empty ();
3949 optimistic_info->references->empty ();
3950 obstack_free (&optimistic_info->nary_obstack, NULL);
3951 gcc_obstack_init (&optimistic_info->nary_obstack);
3952 empty_alloc_pool (optimistic_info->phis_pool);
3953 empty_alloc_pool (optimistic_info->references_pool);
3954 FOR_EACH_VEC_ELT (scc, i, var)
3955 VN_INFO (var)->expr = NULL_TREE;
3956 FOR_EACH_VEC_ELT (scc, i, var)
3957 changed |= visit_use (var);
3960 if (dump_file && (dump_flags & TDF_DETAILS))
3961 fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations);
3962 statistics_histogram_event (cfun, "SCC iterations", iterations);
3964 /* Finally, copy the contents of the no longer used optimistic
3965 table to the valid table. */
3966 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin)
3967 copy_nary (nary, valid_info);
3968 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip)
3969 copy_phi (phi, valid_info);
3970 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references,
3971 ref, vn_reference_t, hir)
3972 copy_reference (ref, valid_info);
3974 current_info = valid_info;
3978 /* Pop the components of the found SCC for NAME off the SCC stack
3979 and process them. Returns true if all went well, false if
3980 we run into resource limits. */
3982 static bool
3983 extract_and_process_scc_for_name (tree name)
3985 auto_vec<tree> scc;
3986 tree x;
3988 /* Found an SCC, pop the components off the SCC stack and
3989 process them. */
3992 x = sccstack.pop ();
3994 VN_INFO (x)->on_sccstack = false;
3995 scc.safe_push (x);
3996 } while (x != name);
3998 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3999 if (scc.length ()
4000 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
4002 if (dump_file)
4003 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
4004 "SCC size %u exceeding %u\n", scc.length (),
4005 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
4007 return false;
4010 if (scc.length () > 1)
4011 sort_scc (scc);
4013 process_scc (scc);
4015 return true;
4018 /* Depth first search on NAME to discover and process SCC's in the SSA
4019 graph.
4020 Execution of this algorithm relies on the fact that the SCC's are
4021 popped off the stack in topological order.
4022 Returns true if successful, false if we stopped processing SCC's due
4023 to resource constraints. */
4025 static bool
4026 DFS (tree name)
4028 vec<ssa_op_iter> itervec = vNULL;
4029 vec<tree> namevec = vNULL;
4030 use_operand_p usep = NULL;
4031 gimple defstmt;
4032 tree use;
4033 ssa_op_iter iter;
4035 start_over:
4036 /* SCC info */
4037 VN_INFO (name)->dfsnum = next_dfs_num++;
4038 VN_INFO (name)->visited = true;
4039 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
4041 sccstack.safe_push (name);
4042 VN_INFO (name)->on_sccstack = true;
4043 defstmt = SSA_NAME_DEF_STMT (name);
4045 /* Recursively DFS on our operands, looking for SCC's. */
4046 if (!gimple_nop_p (defstmt))
4048 /* Push a new iterator. */
4049 if (gphi *phi = dyn_cast <gphi *> (defstmt))
4050 usep = op_iter_init_phiuse (&iter, phi, SSA_OP_ALL_USES);
4051 else
4052 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
4054 else
4055 clear_and_done_ssa_iter (&iter);
4057 while (1)
4059 /* If we are done processing uses of a name, go up the stack
4060 of iterators and process SCCs as we found them. */
4061 if (op_iter_done (&iter))
4063 /* See if we found an SCC. */
4064 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
4065 if (!extract_and_process_scc_for_name (name))
4067 namevec.release ();
4068 itervec.release ();
4069 return false;
4072 /* Check if we are done. */
4073 if (namevec.is_empty ())
4075 namevec.release ();
4076 itervec.release ();
4077 return true;
4080 /* Restore the last use walker and continue walking there. */
4081 use = name;
4082 name = namevec.pop ();
4083 memcpy (&iter, &itervec.last (),
4084 sizeof (ssa_op_iter));
4085 itervec.pop ();
4086 goto continue_walking;
4089 use = USE_FROM_PTR (usep);
4091 /* Since we handle phi nodes, we will sometimes get
4092 invariants in the use expression. */
4093 if (TREE_CODE (use) == SSA_NAME)
4095 if (! (VN_INFO (use)->visited))
4097 /* Recurse by pushing the current use walking state on
4098 the stack and starting over. */
4099 itervec.safe_push (iter);
4100 namevec.safe_push (name);
4101 name = use;
4102 goto start_over;
4104 continue_walking:
4105 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
4106 VN_INFO (use)->low);
4108 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
4109 && VN_INFO (use)->on_sccstack)
4111 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
4112 VN_INFO (name)->low);
4116 usep = op_iter_next_use (&iter);
4120 /* Allocate a value number table. */
4122 static void
4123 allocate_vn_table (vn_tables_t table)
4125 table->phis = new vn_phi_table_type (23);
4126 table->nary = new vn_nary_op_table_type (23);
4127 table->references = new vn_reference_table_type (23);
4129 gcc_obstack_init (&table->nary_obstack);
4130 table->phis_pool = create_alloc_pool ("VN phis",
4131 sizeof (struct vn_phi_s),
4132 30);
4133 table->references_pool = create_alloc_pool ("VN references",
4134 sizeof (struct vn_reference_s),
4135 30);
4138 /* Free a value number table. */
4140 static void
4141 free_vn_table (vn_tables_t table)
4143 delete table->phis;
4144 table->phis = NULL;
4145 delete table->nary;
4146 table->nary = NULL;
4147 delete table->references;
4148 table->references = NULL;
4149 obstack_free (&table->nary_obstack, NULL);
4150 free_alloc_pool (table->phis_pool);
4151 free_alloc_pool (table->references_pool);
4154 static void
4155 init_scc_vn (void)
4157 size_t i;
4158 int j;
4159 int *rpo_numbers_temp;
4161 calculate_dominance_info (CDI_DOMINATORS);
4162 sccstack.create (0);
4163 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
4165 constant_value_ids = BITMAP_ALLOC (NULL);
4167 next_dfs_num = 1;
4168 next_value_id = 1;
4170 vn_ssa_aux_table.create (num_ssa_names + 1);
4171 /* VEC_alloc doesn't actually grow it to the right size, it just
4172 preallocates the space to do so. */
4173 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
4174 gcc_obstack_init (&vn_ssa_aux_obstack);
4176 shared_lookup_phiargs.create (0);
4177 shared_lookup_references.create (0);
4178 rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun));
4179 rpo_numbers_temp =
4180 XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4181 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
4183 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
4184 the i'th block in RPO order is bb. We want to map bb's to RPO
4185 numbers, so we need to rearrange this array. */
4186 for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++)
4187 rpo_numbers[rpo_numbers_temp[j]] = j;
4189 XDELETE (rpo_numbers_temp);
4191 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
4193 /* Create the VN_INFO structures, and initialize value numbers to
4194 TOP. */
4195 for (i = 0; i < num_ssa_names; i++)
4197 tree name = ssa_name (i);
4198 if (name)
4200 VN_INFO_GET (name)->valnum = VN_TOP;
4201 VN_INFO (name)->expr = NULL_TREE;
4202 VN_INFO (name)->value_id = 0;
4206 renumber_gimple_stmt_uids ();
4208 /* Create the valid and optimistic value numbering tables. */
4209 valid_info = XCNEW (struct vn_tables_s);
4210 allocate_vn_table (valid_info);
4211 optimistic_info = XCNEW (struct vn_tables_s);
4212 allocate_vn_table (optimistic_info);
4215 void
4216 free_scc_vn (void)
4218 size_t i;
4220 delete constant_to_value_id;
4221 constant_to_value_id = NULL;
4222 BITMAP_FREE (constant_value_ids);
4223 shared_lookup_phiargs.release ();
4224 shared_lookup_references.release ();
4225 XDELETEVEC (rpo_numbers);
4227 for (i = 0; i < num_ssa_names; i++)
4229 tree name = ssa_name (i);
4230 if (name
4231 && VN_INFO (name)->needs_insertion)
4232 release_ssa_name (name);
4234 obstack_free (&vn_ssa_aux_obstack, NULL);
4235 vn_ssa_aux_table.release ();
4237 sccstack.release ();
4238 free_vn_table (valid_info);
4239 XDELETE (valid_info);
4240 free_vn_table (optimistic_info);
4241 XDELETE (optimistic_info);
4244 /* Set *ID according to RESULT. */
4246 static void
4247 set_value_id_for_result (tree result, unsigned int *id)
4249 if (result && TREE_CODE (result) == SSA_NAME)
4250 *id = VN_INFO (result)->value_id;
4251 else if (result && is_gimple_min_invariant (result))
4252 *id = get_or_alloc_constant_value_id (result);
4253 else
4254 *id = get_next_value_id ();
4257 /* Set the value ids in the valid hash tables. */
4259 static void
4260 set_hashtable_value_ids (void)
4262 vn_nary_op_iterator_type hin;
4263 vn_phi_iterator_type hip;
4264 vn_reference_iterator_type hir;
4265 vn_nary_op_t vno;
4266 vn_reference_t vr;
4267 vn_phi_t vp;
4269 /* Now set the value ids of the things we had put in the hash
4270 table. */
4272 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4273 set_value_id_for_result (vno->result, &vno->value_id);
4275 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4276 set_value_id_for_result (vp->result, &vp->value_id);
4278 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4279 hir)
4280 set_value_id_for_result (vr->result, &vr->value_id);
4283 class cond_dom_walker : public dom_walker
4285 public:
4286 cond_dom_walker () : dom_walker (CDI_DOMINATORS), fail (false) {}
4288 virtual void before_dom_children (basic_block);
4290 bool fail;
4293 void
4294 cond_dom_walker::before_dom_children (basic_block bb)
4296 edge e;
4297 edge_iterator ei;
4299 if (fail)
4300 return;
4302 /* If any of the predecessor edges that do not come from blocks dominated
4303 by us are still marked as possibly executable consider this block
4304 reachable. */
4305 bool reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (cfun);
4306 FOR_EACH_EDGE (e, ei, bb->preds)
4307 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
4308 reachable |= (e->flags & EDGE_EXECUTABLE);
4310 /* If the block is not reachable all outgoing edges are not
4311 executable. */
4312 if (!reachable)
4314 if (dump_file && (dump_flags & TDF_DETAILS))
4315 fprintf (dump_file, "Marking all outgoing edges of unreachable "
4316 "BB %d as not executable\n", bb->index);
4318 FOR_EACH_EDGE (e, ei, bb->succs)
4319 e->flags &= ~EDGE_EXECUTABLE;
4320 return;
4323 gimple stmt = last_stmt (bb);
4324 if (!stmt)
4325 return;
4327 enum gimple_code code = gimple_code (stmt);
4328 if (code != GIMPLE_COND
4329 && code != GIMPLE_SWITCH
4330 && code != GIMPLE_GOTO)
4331 return;
4333 if (dump_file && (dump_flags & TDF_DETAILS))
4335 fprintf (dump_file, "Value-numbering operands of stmt ending BB %d: ",
4336 bb->index);
4337 print_gimple_stmt (dump_file, stmt, 0, 0);
4340 /* Value-number the last stmts SSA uses. */
4341 ssa_op_iter i;
4342 tree op;
4343 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4344 if (VN_INFO (op)->visited == false
4345 && !DFS (op))
4347 fail = true;
4348 return;
4351 /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges
4352 if value-numbering can prove they are not reachable. Handling
4353 computed gotos is also possible. */
4354 tree val;
4355 switch (code)
4357 case GIMPLE_COND:
4359 tree lhs = gimple_cond_lhs (stmt);
4360 tree rhs = gimple_cond_rhs (stmt);
4361 /* Work hard in computing the condition and take into account
4362 the valueization of the defining stmt. */
4363 if (TREE_CODE (lhs) == SSA_NAME)
4364 lhs = vn_get_expr_for (lhs);
4365 if (TREE_CODE (rhs) == SSA_NAME)
4366 rhs = vn_get_expr_for (rhs);
4367 val = fold_binary (gimple_cond_code (stmt),
4368 boolean_type_node, lhs, rhs);
4369 break;
4371 case GIMPLE_SWITCH:
4372 val = gimple_switch_index (as_a <gswitch *> (stmt));
4373 break;
4374 case GIMPLE_GOTO:
4375 val = gimple_goto_dest (stmt);
4376 break;
4377 default:
4378 gcc_unreachable ();
4380 if (!val)
4381 return;
4383 edge taken = find_taken_edge (bb, vn_valueize (val));
4384 if (!taken)
4385 return;
4387 if (dump_file && (dump_flags & TDF_DETAILS))
4388 fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as "
4389 "not executable\n", bb->index, bb->index, taken->dest->index);
4391 FOR_EACH_EDGE (e, ei, bb->succs)
4392 if (e != taken)
4393 e->flags &= ~EDGE_EXECUTABLE;
4396 /* Do SCCVN. Returns true if it finished, false if we bailed out
4397 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4398 how we use the alias oracle walking during the VN process. */
4400 bool
4401 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4403 basic_block bb;
4404 size_t i;
4405 tree param;
4407 default_vn_walk_kind = default_vn_walk_kind_;
4409 init_scc_vn ();
4410 current_info = valid_info;
4412 for (param = DECL_ARGUMENTS (current_function_decl);
4413 param;
4414 param = DECL_CHAIN (param))
4416 tree def = ssa_default_def (cfun, param);
4417 if (def)
4419 VN_INFO (def)->visited = true;
4420 VN_INFO (def)->valnum = def;
4424 /* Mark all edges as possibly executable. */
4425 FOR_ALL_BB_FN (bb, cfun)
4427 edge_iterator ei;
4428 edge e;
4429 FOR_EACH_EDGE (e, ei, bb->succs)
4430 e->flags |= EDGE_EXECUTABLE;
4433 /* Walk all blocks in dominator order, value-numbering the last stmts
4434 SSA uses and decide whether outgoing edges are not executable. */
4435 cond_dom_walker walker;
4436 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4437 if (walker.fail)
4439 free_scc_vn ();
4440 return false;
4443 /* Value-number remaining SSA names. */
4444 for (i = 1; i < num_ssa_names; ++i)
4446 tree name = ssa_name (i);
4447 if (name
4448 && VN_INFO (name)->visited == false
4449 && !has_zero_uses (name))
4450 if (!DFS (name))
4452 free_scc_vn ();
4453 return false;
4457 /* Initialize the value ids. */
4459 for (i = 1; i < num_ssa_names; ++i)
4461 tree name = ssa_name (i);
4462 vn_ssa_aux_t info;
4463 if (!name)
4464 continue;
4465 info = VN_INFO (name);
4466 if (info->valnum == name
4467 || info->valnum == VN_TOP)
4468 info->value_id = get_next_value_id ();
4469 else if (is_gimple_min_invariant (info->valnum))
4470 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4473 /* Propagate. */
4474 for (i = 1; i < num_ssa_names; ++i)
4476 tree name = ssa_name (i);
4477 vn_ssa_aux_t info;
4478 if (!name)
4479 continue;
4480 info = VN_INFO (name);
4481 if (TREE_CODE (info->valnum) == SSA_NAME
4482 && info->valnum != name
4483 && info->value_id != VN_INFO (info->valnum)->value_id)
4484 info->value_id = VN_INFO (info->valnum)->value_id;
4487 set_hashtable_value_ids ();
4489 if (dump_file && (dump_flags & TDF_DETAILS))
4491 fprintf (dump_file, "Value numbers:\n");
4492 for (i = 0; i < num_ssa_names; i++)
4494 tree name = ssa_name (i);
4495 if (name
4496 && VN_INFO (name)->visited
4497 && SSA_VAL (name) != name)
4499 print_generic_expr (dump_file, name, 0);
4500 fprintf (dump_file, " = ");
4501 print_generic_expr (dump_file, SSA_VAL (name), 0);
4502 fprintf (dump_file, "\n");
4507 return true;
4510 /* Return the maximum value id we have ever seen. */
4512 unsigned int
4513 get_max_value_id (void)
4515 return next_value_id;
4518 /* Return the next unique value id. */
4520 unsigned int
4521 get_next_value_id (void)
4523 return next_value_id++;
4527 /* Compare two expressions E1 and E2 and return true if they are equal. */
4529 bool
4530 expressions_equal_p (tree e1, tree e2)
4532 /* The obvious case. */
4533 if (e1 == e2)
4534 return true;
4536 /* If only one of them is null, they cannot be equal. */
4537 if (!e1 || !e2)
4538 return false;
4540 /* Now perform the actual comparison. */
4541 if (TREE_CODE (e1) == TREE_CODE (e2)
4542 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4543 return true;
4545 return false;
4549 /* Return true if the nary operation NARY may trap. This is a copy
4550 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4552 bool
4553 vn_nary_may_trap (vn_nary_op_t nary)
4555 tree type;
4556 tree rhs2 = NULL_TREE;
4557 bool honor_nans = false;
4558 bool honor_snans = false;
4559 bool fp_operation = false;
4560 bool honor_trapv = false;
4561 bool handled, ret;
4562 unsigned i;
4564 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4565 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4566 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4568 type = nary->type;
4569 fp_operation = FLOAT_TYPE_P (type);
4570 if (fp_operation)
4572 honor_nans = flag_trapping_math && !flag_finite_math_only;
4573 honor_snans = flag_signaling_nans != 0;
4575 else if (INTEGRAL_TYPE_P (type)
4576 && TYPE_OVERFLOW_TRAPS (type))
4577 honor_trapv = true;
4579 if (nary->length >= 2)
4580 rhs2 = nary->op[1];
4581 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4582 honor_trapv,
4583 honor_nans, honor_snans, rhs2,
4584 &handled);
4585 if (handled
4586 && ret)
4587 return true;
4589 for (i = 0; i < nary->length; ++i)
4590 if (tree_could_trap_p (nary->op[i]))
4591 return true;
4593 return false;