[ARM][doc] Remove mention of Advanced RISC Machines
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob9902723b6732102c0d1e07edd516ea731a3f8631
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2014 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 "tree.h"
26 #include "stor-layout.h"
27 #include "predict.h"
28 #include "vec.h"
29 #include "hashtab.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "hard-reg-set.h"
33 #include "input.h"
34 #include "function.h"
35 #include "dominance.h"
36 #include "cfg.h"
37 #include "cfganal.h"
38 #include "basic-block.h"
39 #include "gimple-pretty-print.h"
40 #include "tree-inline.h"
41 #include "hash-table.h"
42 #include "tree-ssa-alias.h"
43 #include "internal-fn.h"
44 #include "inchash.h"
45 #include "gimple-fold.h"
46 #include "tree-eh.h"
47 #include "gimple-expr.h"
48 #include "is-a.h"
49 #include "gimple.h"
50 #include "gimplify.h"
51 #include "gimple-ssa.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "expr.h"
57 #include "tree-dfa.h"
58 #include "tree-ssa.h"
59 #include "dumpfile.h"
60 #include "alloc-pool.h"
61 #include "flags.h"
62 #include "cfgloop.h"
63 #include "params.h"
64 #include "tree-ssa-propagate.h"
65 #include "tree-ssa-sccvn.h"
66 #include "tree-cfg.h"
67 #include "domwalk.h"
68 #include "ipa-ref.h"
69 #include "plugin-api.h"
70 #include "cgraph.h"
72 /* This algorithm is based on the SCC algorithm presented by Keith
73 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
74 (http://citeseer.ist.psu.edu/41805.html). In
75 straight line code, it is equivalent to a regular hash based value
76 numbering that is performed in reverse postorder.
78 For code with cycles, there are two alternatives, both of which
79 require keeping the hashtables separate from the actual list of
80 value numbers for SSA names.
82 1. Iterate value numbering in an RPO walk of the blocks, removing
83 all the entries from the hashtable after each iteration (but
84 keeping the SSA name->value number mapping between iterations).
85 Iterate until it does not change.
87 2. Perform value numbering as part of an SCC walk on the SSA graph,
88 iterating only the cycles in the SSA graph until they do not change
89 (using a separate, optimistic hashtable for value numbering the SCC
90 operands).
92 The second is not just faster in practice (because most SSA graph
93 cycles do not involve all the variables in the graph), it also has
94 some nice properties.
96 One of these nice properties is that when we pop an SCC off the
97 stack, we are guaranteed to have processed all the operands coming from
98 *outside of that SCC*, so we do not need to do anything special to
99 ensure they have value numbers.
101 Another nice property is that the SCC walk is done as part of a DFS
102 of the SSA graph, which makes it easy to perform combining and
103 simplifying operations at the same time.
105 The code below is deliberately written in a way that makes it easy
106 to separate the SCC walk from the other work it does.
108 In order to propagate constants through the code, we track which
109 expressions contain constants, and use those while folding. In
110 theory, we could also track expressions whose value numbers are
111 replaced, in case we end up folding based on expression
112 identities.
114 In order to value number memory, we assign value numbers to vuses.
115 This enables us to note that, for example, stores to the same
116 address of the same value from the same starting memory states are
117 equivalent.
118 TODO:
120 1. We can iterate only the changing portions of the SCC's, but
121 I have not seen an SCC big enough for this to be a win.
122 2. If you differentiate between phi nodes for loops and phi nodes
123 for if-then-else, you can properly consider phi nodes in different
124 blocks for equivalence.
125 3. We could value number vuses in more cases, particularly, whole
126 structure copies.
130 /* vn_nary_op hashtable helpers. */
132 struct vn_nary_op_hasher : typed_noop_remove <vn_nary_op_s>
134 typedef vn_nary_op_s value_type;
135 typedef vn_nary_op_s compare_type;
136 static inline hashval_t hash (const value_type *);
137 static inline bool equal (const value_type *, const compare_type *);
140 /* Return the computed hashcode for nary operation P1. */
142 inline hashval_t
143 vn_nary_op_hasher::hash (const value_type *vno1)
145 return vno1->hashcode;
148 /* Compare nary operations P1 and P2 and return true if they are
149 equivalent. */
151 inline bool
152 vn_nary_op_hasher::equal (const value_type *vno1, const compare_type *vno2)
154 return vn_nary_op_eq (vno1, vno2);
157 typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
158 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
161 /* vn_phi hashtable helpers. */
163 static int
164 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
166 struct vn_phi_hasher
168 typedef vn_phi_s value_type;
169 typedef vn_phi_s compare_type;
170 static inline hashval_t hash (const value_type *);
171 static inline bool equal (const value_type *, const compare_type *);
172 static inline void remove (value_type *);
175 /* Return the computed hashcode for phi operation P1. */
177 inline hashval_t
178 vn_phi_hasher::hash (const value_type *vp1)
180 return vp1->hashcode;
183 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
185 inline bool
186 vn_phi_hasher::equal (const value_type *vp1, const compare_type *vp2)
188 return vn_phi_eq (vp1, vp2);
191 /* Free a phi operation structure VP. */
193 inline void
194 vn_phi_hasher::remove (value_type *phi)
196 phi->phiargs.release ();
199 typedef hash_table<vn_phi_hasher> vn_phi_table_type;
200 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
203 /* Compare two reference operands P1 and P2 for equality. Return true if
204 they are equal, and false otherwise. */
206 static int
207 vn_reference_op_eq (const void *p1, const void *p2)
209 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
210 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
212 return (vro1->opcode == vro2->opcode
213 /* We do not care for differences in type qualification. */
214 && (vro1->type == vro2->type
215 || (vro1->type && vro2->type
216 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
217 TYPE_MAIN_VARIANT (vro2->type))))
218 && expressions_equal_p (vro1->op0, vro2->op0)
219 && expressions_equal_p (vro1->op1, vro2->op1)
220 && expressions_equal_p (vro1->op2, vro2->op2));
223 /* Free a reference operation structure VP. */
225 static inline void
226 free_reference (vn_reference_s *vr)
228 vr->operands.release ();
232 /* vn_reference hashtable helpers. */
234 struct vn_reference_hasher
236 typedef vn_reference_s value_type;
237 typedef vn_reference_s compare_type;
238 static inline hashval_t hash (const value_type *);
239 static inline bool equal (const value_type *, const compare_type *);
240 static inline void remove (value_type *);
243 /* Return the hashcode for a given reference operation P1. */
245 inline hashval_t
246 vn_reference_hasher::hash (const value_type *vr1)
248 return vr1->hashcode;
251 inline bool
252 vn_reference_hasher::equal (const value_type *v, const compare_type *c)
254 return vn_reference_eq (v, c);
257 inline void
258 vn_reference_hasher::remove (value_type *v)
260 free_reference (v);
263 typedef hash_table<vn_reference_hasher> vn_reference_table_type;
264 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
267 /* The set of hashtables and alloc_pool's for their items. */
269 typedef struct vn_tables_s
271 vn_nary_op_table_type *nary;
272 vn_phi_table_type *phis;
273 vn_reference_table_type *references;
274 struct obstack nary_obstack;
275 alloc_pool phis_pool;
276 alloc_pool references_pool;
277 } *vn_tables_t;
280 /* vn_constant hashtable helpers. */
282 struct vn_constant_hasher : typed_free_remove <vn_constant_s>
284 typedef vn_constant_s value_type;
285 typedef vn_constant_s compare_type;
286 static inline hashval_t hash (const value_type *);
287 static inline bool equal (const value_type *, const compare_type *);
290 /* Hash table hash function for vn_constant_t. */
292 inline hashval_t
293 vn_constant_hasher::hash (const value_type *vc1)
295 return vc1->hashcode;
298 /* Hash table equality function for vn_constant_t. */
300 inline bool
301 vn_constant_hasher::equal (const value_type *vc1, const compare_type *vc2)
303 if (vc1->hashcode != vc2->hashcode)
304 return false;
306 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
309 static hash_table<vn_constant_hasher> *constant_to_value_id;
310 static bitmap constant_value_ids;
313 /* Valid hashtables storing information we have proven to be
314 correct. */
316 static vn_tables_t valid_info;
318 /* Optimistic hashtables storing information we are making assumptions about
319 during iterations. */
321 static vn_tables_t optimistic_info;
323 /* Pointer to the set of hashtables that is currently being used.
324 Should always point to either the optimistic_info, or the
325 valid_info. */
327 static vn_tables_t current_info;
330 /* Reverse post order index for each basic block. */
332 static int *rpo_numbers;
334 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
336 /* Return the SSA value of the VUSE x, supporting released VDEFs
337 during elimination which will value-number the VDEF to the
338 associated VUSE (but not substitute in the whole lattice). */
340 static inline tree
341 vuse_ssa_val (tree x)
343 if (!x)
344 return NULL_TREE;
348 x = SSA_VAL (x);
350 while (SSA_NAME_IN_FREE_LIST (x));
352 return x;
355 /* This represents the top of the VN lattice, which is the universal
356 value. */
358 tree VN_TOP;
360 /* Unique counter for our value ids. */
362 static unsigned int next_value_id;
364 /* Next DFS number and the stack for strongly connected component
365 detection. */
367 static unsigned int next_dfs_num;
368 static vec<tree> sccstack;
372 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
373 are allocated on an obstack for locality reasons, and to free them
374 without looping over the vec. */
376 static vec<vn_ssa_aux_t> vn_ssa_aux_table;
377 static struct obstack vn_ssa_aux_obstack;
379 /* Return the value numbering information for a given SSA name. */
381 vn_ssa_aux_t
382 VN_INFO (tree name)
384 vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)];
385 gcc_checking_assert (res);
386 return res;
389 /* Set the value numbering info for a given SSA name to a given
390 value. */
392 static inline void
393 VN_INFO_SET (tree name, vn_ssa_aux_t value)
395 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value;
398 /* Initialize the value numbering info for a given SSA name.
399 This should be called just once for every SSA name. */
401 vn_ssa_aux_t
402 VN_INFO_GET (tree name)
404 vn_ssa_aux_t newinfo;
406 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
407 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
408 if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ())
409 vn_ssa_aux_table.safe_grow (SSA_NAME_VERSION (name) + 1);
410 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo;
411 return newinfo;
415 /* Get the representative expression for the SSA_NAME NAME. Returns
416 the representative SSA_NAME if there is no expression associated with it. */
418 tree
419 vn_get_expr_for (tree name)
421 vn_ssa_aux_t vn = VN_INFO (name);
422 gimple def_stmt;
423 tree expr = NULL_TREE;
424 enum tree_code code;
426 if (vn->valnum == VN_TOP)
427 return name;
429 /* If the value-number is a constant it is the representative
430 expression. */
431 if (TREE_CODE (vn->valnum) != SSA_NAME)
432 return vn->valnum;
434 /* Get to the information of the value of this SSA_NAME. */
435 vn = VN_INFO (vn->valnum);
437 /* If the value-number is a constant it is the representative
438 expression. */
439 if (TREE_CODE (vn->valnum) != SSA_NAME)
440 return vn->valnum;
442 /* Else if we have an expression, return it. */
443 if (vn->expr != NULL_TREE)
444 return vn->expr;
446 /* Otherwise use the defining statement to build the expression. */
447 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
449 /* If the value number is not an assignment use it directly. */
450 if (!is_gimple_assign (def_stmt))
451 return vn->valnum;
453 /* Note that we can valueize here because we clear the cached
454 simplified expressions after each optimistic iteration. */
455 code = gimple_assign_rhs_code (def_stmt);
456 switch (TREE_CODE_CLASS (code))
458 case tcc_reference:
459 if ((code == REALPART_EXPR
460 || code == IMAGPART_EXPR
461 || code == VIEW_CONVERT_EXPR)
462 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
463 0)) == SSA_NAME)
464 expr = fold_build1 (code,
465 gimple_expr_type (def_stmt),
466 vn_valueize (TREE_OPERAND
467 (gimple_assign_rhs1 (def_stmt), 0)));
468 break;
470 case tcc_unary:
471 expr = fold_build1 (code,
472 gimple_expr_type (def_stmt),
473 vn_valueize (gimple_assign_rhs1 (def_stmt)));
474 break;
476 case tcc_binary:
477 expr = fold_build2 (code,
478 gimple_expr_type (def_stmt),
479 vn_valueize (gimple_assign_rhs1 (def_stmt)),
480 vn_valueize (gimple_assign_rhs2 (def_stmt)));
481 break;
483 case tcc_exceptional:
484 if (code == CONSTRUCTOR
485 && TREE_CODE
486 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
487 expr = gimple_assign_rhs1 (def_stmt);
488 break;
490 default:;
492 if (expr == NULL_TREE)
493 return vn->valnum;
495 /* Cache the expression. */
496 vn->expr = expr;
498 return expr;
501 /* Return the vn_kind the expression computed by the stmt should be
502 associated with. */
504 enum vn_kind
505 vn_get_stmt_kind (gimple stmt)
507 switch (gimple_code (stmt))
509 case GIMPLE_CALL:
510 return VN_REFERENCE;
511 case GIMPLE_PHI:
512 return VN_PHI;
513 case GIMPLE_ASSIGN:
515 enum tree_code code = gimple_assign_rhs_code (stmt);
516 tree rhs1 = gimple_assign_rhs1 (stmt);
517 switch (get_gimple_rhs_class (code))
519 case GIMPLE_UNARY_RHS:
520 case GIMPLE_BINARY_RHS:
521 case GIMPLE_TERNARY_RHS:
522 return VN_NARY;
523 case GIMPLE_SINGLE_RHS:
524 switch (TREE_CODE_CLASS (code))
526 case tcc_reference:
527 /* VOP-less references can go through unary case. */
528 if ((code == REALPART_EXPR
529 || code == IMAGPART_EXPR
530 || code == VIEW_CONVERT_EXPR
531 || code == BIT_FIELD_REF)
532 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
533 return VN_NARY;
535 /* Fallthrough. */
536 case tcc_declaration:
537 return VN_REFERENCE;
539 case tcc_constant:
540 return VN_CONSTANT;
542 default:
543 if (code == ADDR_EXPR)
544 return (is_gimple_min_invariant (rhs1)
545 ? VN_CONSTANT : VN_REFERENCE);
546 else if (code == CONSTRUCTOR)
547 return VN_NARY;
548 return VN_NONE;
550 default:
551 return VN_NONE;
554 default:
555 return VN_NONE;
559 /* Lookup a value id for CONSTANT and return it. If it does not
560 exist returns 0. */
562 unsigned int
563 get_constant_value_id (tree constant)
565 vn_constant_s **slot;
566 struct vn_constant_s vc;
568 vc.hashcode = vn_hash_constant_with_type (constant);
569 vc.constant = constant;
570 slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
571 if (slot)
572 return (*slot)->value_id;
573 return 0;
576 /* Lookup a value id for CONSTANT, and if it does not exist, create a
577 new one and return it. If it does exist, return it. */
579 unsigned int
580 get_or_alloc_constant_value_id (tree constant)
582 vn_constant_s **slot;
583 struct vn_constant_s vc;
584 vn_constant_t vcp;
586 vc.hashcode = vn_hash_constant_with_type (constant);
587 vc.constant = constant;
588 slot = constant_to_value_id->find_slot (&vc, INSERT);
589 if (*slot)
590 return (*slot)->value_id;
592 vcp = XNEW (struct vn_constant_s);
593 vcp->hashcode = vc.hashcode;
594 vcp->constant = constant;
595 vcp->value_id = get_next_value_id ();
596 *slot = vcp;
597 bitmap_set_bit (constant_value_ids, vcp->value_id);
598 return vcp->value_id;
601 /* Return true if V is a value id for a constant. */
603 bool
604 value_id_constant_p (unsigned int v)
606 return bitmap_bit_p (constant_value_ids, v);
609 /* Compute the hash for a reference operand VRO1. */
611 static void
612 vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
614 hstate.add_int (vro1->opcode);
615 if (vro1->op0)
616 inchash::add_expr (vro1->op0, hstate);
617 if (vro1->op1)
618 inchash::add_expr (vro1->op1, hstate);
619 if (vro1->op2)
620 inchash::add_expr (vro1->op2, hstate);
623 /* Compute a hash for the reference operation VR1 and return it. */
625 static hashval_t
626 vn_reference_compute_hash (const vn_reference_t vr1)
628 inchash::hash hstate;
629 hashval_t result;
630 int i;
631 vn_reference_op_t vro;
632 HOST_WIDE_INT off = -1;
633 bool deref = false;
635 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
637 if (vro->opcode == MEM_REF)
638 deref = true;
639 else if (vro->opcode != ADDR_EXPR)
640 deref = false;
641 if (vro->off != -1)
643 if (off == -1)
644 off = 0;
645 off += vro->off;
647 else
649 if (off != -1
650 && off != 0)
651 hstate.add_int (off);
652 off = -1;
653 if (deref
654 && vro->opcode == ADDR_EXPR)
656 if (vro->op0)
658 tree op = TREE_OPERAND (vro->op0, 0);
659 hstate.add_int (TREE_CODE (op));
660 inchash::add_expr (op, hstate);
663 else
664 vn_reference_op_compute_hash (vro, hstate);
667 result = hstate.end ();
668 /* ??? We would ICE later if we hash instead of adding that in. */
669 if (vr1->vuse)
670 result += SSA_NAME_VERSION (vr1->vuse);
672 return result;
675 /* Return true if reference operations VR1 and VR2 are equivalent. This
676 means they have the same set of operands and vuses. */
678 bool
679 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
681 unsigned i, j;
683 /* Early out if this is not a hash collision. */
684 if (vr1->hashcode != vr2->hashcode)
685 return false;
687 /* The VOP needs to be the same. */
688 if (vr1->vuse != vr2->vuse)
689 return false;
691 /* If the operands are the same we are done. */
692 if (vr1->operands == vr2->operands)
693 return true;
695 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
696 return false;
698 if (INTEGRAL_TYPE_P (vr1->type)
699 && INTEGRAL_TYPE_P (vr2->type))
701 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
702 return false;
704 else if (INTEGRAL_TYPE_P (vr1->type)
705 && (TYPE_PRECISION (vr1->type)
706 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
707 return false;
708 else if (INTEGRAL_TYPE_P (vr2->type)
709 && (TYPE_PRECISION (vr2->type)
710 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
711 return false;
713 i = 0;
714 j = 0;
717 HOST_WIDE_INT off1 = 0, off2 = 0;
718 vn_reference_op_t vro1, vro2;
719 vn_reference_op_s tem1, tem2;
720 bool deref1 = false, deref2 = false;
721 for (; vr1->operands.iterate (i, &vro1); i++)
723 if (vro1->opcode == MEM_REF)
724 deref1 = true;
725 if (vro1->off == -1)
726 break;
727 off1 += vro1->off;
729 for (; vr2->operands.iterate (j, &vro2); j++)
731 if (vro2->opcode == MEM_REF)
732 deref2 = true;
733 if (vro2->off == -1)
734 break;
735 off2 += vro2->off;
737 if (off1 != off2)
738 return false;
739 if (deref1 && vro1->opcode == ADDR_EXPR)
741 memset (&tem1, 0, sizeof (tem1));
742 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
743 tem1.type = TREE_TYPE (tem1.op0);
744 tem1.opcode = TREE_CODE (tem1.op0);
745 vro1 = &tem1;
746 deref1 = false;
748 if (deref2 && vro2->opcode == ADDR_EXPR)
750 memset (&tem2, 0, sizeof (tem2));
751 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
752 tem2.type = TREE_TYPE (tem2.op0);
753 tem2.opcode = TREE_CODE (tem2.op0);
754 vro2 = &tem2;
755 deref2 = false;
757 if (deref1 != deref2)
758 return false;
759 if (!vn_reference_op_eq (vro1, vro2))
760 return false;
761 ++j;
762 ++i;
764 while (vr1->operands.length () != i
765 || vr2->operands.length () != j);
767 return true;
770 /* Copy the operations present in load/store REF into RESULT, a vector of
771 vn_reference_op_s's. */
773 static void
774 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
776 if (TREE_CODE (ref) == TARGET_MEM_REF)
778 vn_reference_op_s temp;
780 result->reserve (3);
782 memset (&temp, 0, sizeof (temp));
783 temp.type = TREE_TYPE (ref);
784 temp.opcode = TREE_CODE (ref);
785 temp.op0 = TMR_INDEX (ref);
786 temp.op1 = TMR_STEP (ref);
787 temp.op2 = TMR_OFFSET (ref);
788 temp.off = -1;
789 result->quick_push (temp);
791 memset (&temp, 0, sizeof (temp));
792 temp.type = NULL_TREE;
793 temp.opcode = ERROR_MARK;
794 temp.op0 = TMR_INDEX2 (ref);
795 temp.off = -1;
796 result->quick_push (temp);
798 memset (&temp, 0, sizeof (temp));
799 temp.type = NULL_TREE;
800 temp.opcode = TREE_CODE (TMR_BASE (ref));
801 temp.op0 = TMR_BASE (ref);
802 temp.off = -1;
803 result->quick_push (temp);
804 return;
807 /* For non-calls, store the information that makes up the address. */
808 tree orig = ref;
809 while (ref)
811 vn_reference_op_s temp;
813 memset (&temp, 0, sizeof (temp));
814 temp.type = TREE_TYPE (ref);
815 temp.opcode = TREE_CODE (ref);
816 temp.off = -1;
818 switch (temp.opcode)
820 case MODIFY_EXPR:
821 temp.op0 = TREE_OPERAND (ref, 1);
822 break;
823 case WITH_SIZE_EXPR:
824 temp.op0 = TREE_OPERAND (ref, 1);
825 temp.off = 0;
826 break;
827 case MEM_REF:
828 /* The base address gets its own vn_reference_op_s structure. */
829 temp.op0 = TREE_OPERAND (ref, 1);
830 if (tree_fits_shwi_p (TREE_OPERAND (ref, 1)))
831 temp.off = tree_to_shwi (TREE_OPERAND (ref, 1));
832 break;
833 case BIT_FIELD_REF:
834 /* Record bits and position. */
835 temp.op0 = TREE_OPERAND (ref, 1);
836 temp.op1 = TREE_OPERAND (ref, 2);
837 break;
838 case COMPONENT_REF:
839 /* The field decl is enough to unambiguously specify the field,
840 a matching type is not necessary and a mismatching type
841 is always a spurious difference. */
842 temp.type = NULL_TREE;
843 temp.op0 = TREE_OPERAND (ref, 1);
844 temp.op1 = TREE_OPERAND (ref, 2);
846 tree this_offset = component_ref_field_offset (ref);
847 if (this_offset
848 && TREE_CODE (this_offset) == INTEGER_CST)
850 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
851 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
853 offset_int off
854 = (wi::to_offset (this_offset)
855 + wi::lrshift (wi::to_offset (bit_offset),
856 LOG2_BITS_PER_UNIT));
857 if (wi::fits_shwi_p (off)
858 /* Probibit value-numbering zero offset components
859 of addresses the same before the pass folding
860 __builtin_object_size had a chance to run
861 (checking cfun->after_inlining does the
862 trick here). */
863 && (TREE_CODE (orig) != ADDR_EXPR
864 || off != 0
865 || cfun->after_inlining))
866 temp.off = off.to_shwi ();
870 break;
871 case ARRAY_RANGE_REF:
872 case ARRAY_REF:
873 /* Record index as operand. */
874 temp.op0 = TREE_OPERAND (ref, 1);
875 /* Always record lower bounds and element size. */
876 temp.op1 = array_ref_low_bound (ref);
877 temp.op2 = array_ref_element_size (ref);
878 if (TREE_CODE (temp.op0) == INTEGER_CST
879 && TREE_CODE (temp.op1) == INTEGER_CST
880 && TREE_CODE (temp.op2) == INTEGER_CST)
882 offset_int off = ((wi::to_offset (temp.op0)
883 - wi::to_offset (temp.op1))
884 * wi::to_offset (temp.op2));
885 if (wi::fits_shwi_p (off))
886 temp.off = off.to_shwi();
888 break;
889 case VAR_DECL:
890 if (DECL_HARD_REGISTER (ref))
892 temp.op0 = ref;
893 break;
895 /* Fallthru. */
896 case PARM_DECL:
897 case CONST_DECL:
898 case RESULT_DECL:
899 /* Canonicalize decls to MEM[&decl] which is what we end up with
900 when valueizing MEM[ptr] with ptr = &decl. */
901 temp.opcode = MEM_REF;
902 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
903 temp.off = 0;
904 result->safe_push (temp);
905 temp.opcode = ADDR_EXPR;
906 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
907 temp.type = TREE_TYPE (temp.op0);
908 temp.off = -1;
909 break;
910 case STRING_CST:
911 case INTEGER_CST:
912 case COMPLEX_CST:
913 case VECTOR_CST:
914 case REAL_CST:
915 case FIXED_CST:
916 case CONSTRUCTOR:
917 case SSA_NAME:
918 temp.op0 = ref;
919 break;
920 case ADDR_EXPR:
921 if (is_gimple_min_invariant (ref))
923 temp.op0 = ref;
924 break;
926 break;
927 /* These are only interesting for their operands, their
928 existence, and their type. They will never be the last
929 ref in the chain of references (IE they require an
930 operand), so we don't have to put anything
931 for op* as it will be handled by the iteration */
932 case REALPART_EXPR:
933 case VIEW_CONVERT_EXPR:
934 temp.off = 0;
935 break;
936 case IMAGPART_EXPR:
937 /* This is only interesting for its constant offset. */
938 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
939 break;
940 default:
941 gcc_unreachable ();
943 result->safe_push (temp);
945 if (REFERENCE_CLASS_P (ref)
946 || TREE_CODE (ref) == MODIFY_EXPR
947 || TREE_CODE (ref) == WITH_SIZE_EXPR
948 || (TREE_CODE (ref) == ADDR_EXPR
949 && !is_gimple_min_invariant (ref)))
950 ref = TREE_OPERAND (ref, 0);
951 else
952 ref = NULL_TREE;
956 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
957 operands in *OPS, the reference alias set SET and the reference type TYPE.
958 Return true if something useful was produced. */
960 bool
961 ao_ref_init_from_vn_reference (ao_ref *ref,
962 alias_set_type set, tree type,
963 vec<vn_reference_op_s> ops)
965 vn_reference_op_t op;
966 unsigned i;
967 tree base = NULL_TREE;
968 tree *op0_p = &base;
969 HOST_WIDE_INT offset = 0;
970 HOST_WIDE_INT max_size;
971 HOST_WIDE_INT size = -1;
972 tree size_tree = NULL_TREE;
973 alias_set_type base_alias_set = -1;
975 /* First get the final access size from just the outermost expression. */
976 op = &ops[0];
977 if (op->opcode == COMPONENT_REF)
978 size_tree = DECL_SIZE (op->op0);
979 else if (op->opcode == BIT_FIELD_REF)
980 size_tree = op->op0;
981 else
983 machine_mode mode = TYPE_MODE (type);
984 if (mode == BLKmode)
985 size_tree = TYPE_SIZE (type);
986 else
987 size = GET_MODE_BITSIZE (mode);
989 if (size_tree != NULL_TREE)
991 if (!tree_fits_uhwi_p (size_tree))
992 size = -1;
993 else
994 size = tree_to_uhwi (size_tree);
997 /* Initially, maxsize is the same as the accessed element size.
998 In the following it will only grow (or become -1). */
999 max_size = size;
1001 /* Compute cumulative bit-offset for nested component-refs and array-refs,
1002 and find the ultimate containing object. */
1003 FOR_EACH_VEC_ELT (ops, i, op)
1005 switch (op->opcode)
1007 /* These may be in the reference ops, but we cannot do anything
1008 sensible with them here. */
1009 case ADDR_EXPR:
1010 /* Apart from ADDR_EXPR arguments to MEM_REF. */
1011 if (base != NULL_TREE
1012 && TREE_CODE (base) == MEM_REF
1013 && op->op0
1014 && DECL_P (TREE_OPERAND (op->op0, 0)))
1016 vn_reference_op_t pop = &ops[i-1];
1017 base = TREE_OPERAND (op->op0, 0);
1018 if (pop->off == -1)
1020 max_size = -1;
1021 offset = 0;
1023 else
1024 offset += pop->off * BITS_PER_UNIT;
1025 op0_p = NULL;
1026 break;
1028 /* Fallthru. */
1029 case CALL_EXPR:
1030 return false;
1032 /* Record the base objects. */
1033 case MEM_REF:
1034 base_alias_set = get_deref_alias_set (op->op0);
1035 *op0_p = build2 (MEM_REF, op->type,
1036 NULL_TREE, op->op0);
1037 op0_p = &TREE_OPERAND (*op0_p, 0);
1038 break;
1040 case VAR_DECL:
1041 case PARM_DECL:
1042 case RESULT_DECL:
1043 case SSA_NAME:
1044 *op0_p = op->op0;
1045 op0_p = NULL;
1046 break;
1048 /* And now the usual component-reference style ops. */
1049 case BIT_FIELD_REF:
1050 offset += tree_to_shwi (op->op1);
1051 break;
1053 case COMPONENT_REF:
1055 tree field = op->op0;
1056 /* We do not have a complete COMPONENT_REF tree here so we
1057 cannot use component_ref_field_offset. Do the interesting
1058 parts manually. */
1060 if (op->op1
1061 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
1062 max_size = -1;
1063 else
1065 offset += (tree_to_uhwi (DECL_FIELD_OFFSET (field))
1066 * BITS_PER_UNIT);
1067 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
1069 break;
1072 case ARRAY_RANGE_REF:
1073 case ARRAY_REF:
1074 /* We recorded the lower bound and the element size. */
1075 if (!tree_fits_shwi_p (op->op0)
1076 || !tree_fits_shwi_p (op->op1)
1077 || !tree_fits_shwi_p (op->op2))
1078 max_size = -1;
1079 else
1081 HOST_WIDE_INT hindex = tree_to_shwi (op->op0);
1082 hindex -= tree_to_shwi (op->op1);
1083 hindex *= tree_to_shwi (op->op2);
1084 hindex *= BITS_PER_UNIT;
1085 offset += hindex;
1087 break;
1089 case REALPART_EXPR:
1090 break;
1092 case IMAGPART_EXPR:
1093 offset += size;
1094 break;
1096 case VIEW_CONVERT_EXPR:
1097 break;
1099 case STRING_CST:
1100 case INTEGER_CST:
1101 case COMPLEX_CST:
1102 case VECTOR_CST:
1103 case REAL_CST:
1104 case CONSTRUCTOR:
1105 case CONST_DECL:
1106 return false;
1108 default:
1109 return false;
1113 if (base == NULL_TREE)
1114 return false;
1116 ref->ref = NULL_TREE;
1117 ref->base = base;
1118 ref->offset = offset;
1119 ref->size = size;
1120 ref->max_size = max_size;
1121 ref->ref_alias_set = set;
1122 if (base_alias_set != -1)
1123 ref->base_alias_set = base_alias_set;
1124 else
1125 ref->base_alias_set = get_alias_set (base);
1126 /* We discount volatiles from value-numbering elsewhere. */
1127 ref->volatile_p = false;
1129 return true;
1132 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1133 vn_reference_op_s's. */
1135 static void
1136 copy_reference_ops_from_call (gcall *call,
1137 vec<vn_reference_op_s> *result)
1139 vn_reference_op_s temp;
1140 unsigned i;
1141 tree lhs = gimple_call_lhs (call);
1142 int lr;
1144 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1145 different. By adding the lhs here in the vector, we ensure that the
1146 hashcode is different, guaranteeing a different value number. */
1147 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1149 memset (&temp, 0, sizeof (temp));
1150 temp.opcode = MODIFY_EXPR;
1151 temp.type = TREE_TYPE (lhs);
1152 temp.op0 = lhs;
1153 temp.off = -1;
1154 result->safe_push (temp);
1157 /* Copy the type, opcode, function, static chain and EH region, if any. */
1158 memset (&temp, 0, sizeof (temp));
1159 temp.type = gimple_call_return_type (call);
1160 temp.opcode = CALL_EXPR;
1161 temp.op0 = gimple_call_fn (call);
1162 temp.op1 = gimple_call_chain (call);
1163 if (stmt_could_throw_p (call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1164 temp.op2 = size_int (lr);
1165 temp.off = -1;
1166 if (gimple_call_with_bounds_p (call))
1167 temp.with_bounds = 1;
1168 result->safe_push (temp);
1170 /* Copy the call arguments. As they can be references as well,
1171 just chain them together. */
1172 for (i = 0; i < gimple_call_num_args (call); ++i)
1174 tree callarg = gimple_call_arg (call, i);
1175 copy_reference_ops_from_ref (callarg, result);
1179 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1180 *I_P to point to the last element of the replacement. */
1181 void
1182 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1183 unsigned int *i_p)
1185 unsigned int i = *i_p;
1186 vn_reference_op_t op = &(*ops)[i];
1187 vn_reference_op_t mem_op = &(*ops)[i - 1];
1188 tree addr_base;
1189 HOST_WIDE_INT addr_offset = 0;
1191 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1192 from .foo.bar to the preceding MEM_REF offset and replace the
1193 address with &OBJ. */
1194 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1195 &addr_offset);
1196 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1197 if (addr_base != TREE_OPERAND (op->op0, 0))
1199 offset_int off = offset_int::from (mem_op->op0, SIGNED);
1200 off += addr_offset;
1201 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1202 op->op0 = build_fold_addr_expr (addr_base);
1203 if (tree_fits_shwi_p (mem_op->op0))
1204 mem_op->off = tree_to_shwi (mem_op->op0);
1205 else
1206 mem_op->off = -1;
1210 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1211 *I_P to point to the last element of the replacement. */
1212 static void
1213 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1214 unsigned int *i_p)
1216 unsigned int i = *i_p;
1217 vn_reference_op_t op = &(*ops)[i];
1218 vn_reference_op_t mem_op = &(*ops)[i - 1];
1219 gimple def_stmt;
1220 enum tree_code code;
1221 offset_int off;
1223 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1224 if (!is_gimple_assign (def_stmt))
1225 return;
1227 code = gimple_assign_rhs_code (def_stmt);
1228 if (code != ADDR_EXPR
1229 && code != POINTER_PLUS_EXPR)
1230 return;
1232 off = offset_int::from (mem_op->op0, SIGNED);
1234 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1235 from .foo.bar to the preceding MEM_REF offset and replace the
1236 address with &OBJ. */
1237 if (code == ADDR_EXPR)
1239 tree addr, addr_base;
1240 HOST_WIDE_INT addr_offset;
1242 addr = gimple_assign_rhs1 (def_stmt);
1243 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1244 &addr_offset);
1245 if (!addr_base
1246 || TREE_CODE (addr_base) != MEM_REF)
1247 return;
1249 off += addr_offset;
1250 off += mem_ref_offset (addr_base);
1251 op->op0 = TREE_OPERAND (addr_base, 0);
1253 else
1255 tree ptr, ptroff;
1256 ptr = gimple_assign_rhs1 (def_stmt);
1257 ptroff = gimple_assign_rhs2 (def_stmt);
1258 if (TREE_CODE (ptr) != SSA_NAME
1259 || TREE_CODE (ptroff) != INTEGER_CST)
1260 return;
1262 off += wi::to_offset (ptroff);
1263 op->op0 = ptr;
1266 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1267 if (tree_fits_shwi_p (mem_op->op0))
1268 mem_op->off = tree_to_shwi (mem_op->op0);
1269 else
1270 mem_op->off = -1;
1271 if (TREE_CODE (op->op0) == SSA_NAME)
1272 op->op0 = SSA_VAL (op->op0);
1273 if (TREE_CODE (op->op0) != SSA_NAME)
1274 op->opcode = TREE_CODE (op->op0);
1276 /* And recurse. */
1277 if (TREE_CODE (op->op0) == SSA_NAME)
1278 vn_reference_maybe_forwprop_address (ops, i_p);
1279 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1280 vn_reference_fold_indirect (ops, i_p);
1283 /* Optimize the reference REF to a constant if possible or return
1284 NULL_TREE if not. */
1286 tree
1287 fully_constant_vn_reference_p (vn_reference_t ref)
1289 vec<vn_reference_op_s> operands = ref->operands;
1290 vn_reference_op_t op;
1292 /* Try to simplify the translated expression if it is
1293 a call to a builtin function with at most two arguments. */
1294 op = &operands[0];
1295 if (op->opcode == CALL_EXPR
1296 && TREE_CODE (op->op0) == ADDR_EXPR
1297 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1298 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1299 && operands.length () >= 2
1300 && operands.length () <= 3)
1302 vn_reference_op_t arg0, arg1 = NULL;
1303 bool anyconst = false;
1304 arg0 = &operands[1];
1305 if (operands.length () > 2)
1306 arg1 = &operands[2];
1307 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1308 || (arg0->opcode == ADDR_EXPR
1309 && is_gimple_min_invariant (arg0->op0)))
1310 anyconst = true;
1311 if (arg1
1312 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1313 || (arg1->opcode == ADDR_EXPR
1314 && is_gimple_min_invariant (arg1->op0))))
1315 anyconst = true;
1316 if (anyconst)
1318 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1319 arg1 ? 2 : 1,
1320 arg0->op0,
1321 arg1 ? arg1->op0 : NULL);
1322 if (folded
1323 && TREE_CODE (folded) == NOP_EXPR)
1324 folded = TREE_OPERAND (folded, 0);
1325 if (folded
1326 && is_gimple_min_invariant (folded))
1327 return folded;
1331 /* Simplify reads from constants or constant initializers. */
1332 else if (BITS_PER_UNIT == 8
1333 && is_gimple_reg_type (ref->type)
1334 && (!INTEGRAL_TYPE_P (ref->type)
1335 || TYPE_PRECISION (ref->type) % BITS_PER_UNIT == 0))
1337 HOST_WIDE_INT off = 0;
1338 HOST_WIDE_INT size = tree_to_shwi (TYPE_SIZE (ref->type));
1339 if (size % BITS_PER_UNIT != 0
1340 || size > MAX_BITSIZE_MODE_ANY_MODE)
1341 return NULL_TREE;
1342 size /= BITS_PER_UNIT;
1343 unsigned i;
1344 for (i = 0; i < operands.length (); ++i)
1346 if (operands[i].off == -1)
1347 return NULL_TREE;
1348 off += operands[i].off;
1349 if (operands[i].opcode == MEM_REF)
1351 ++i;
1352 break;
1355 vn_reference_op_t base = &operands[--i];
1356 tree ctor = error_mark_node;
1357 tree decl = NULL_TREE;
1358 if (TREE_CODE_CLASS (base->opcode) == tcc_constant)
1359 ctor = base->op0;
1360 else if (base->opcode == MEM_REF
1361 && base[1].opcode == ADDR_EXPR
1362 && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL
1363 || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL))
1365 decl = TREE_OPERAND (base[1].op0, 0);
1366 ctor = ctor_for_folding (decl);
1368 if (ctor == NULL_TREE)
1369 return build_zero_cst (ref->type);
1370 else if (ctor != error_mark_node)
1372 if (decl)
1374 tree res = fold_ctor_reference (ref->type, ctor,
1375 off * BITS_PER_UNIT,
1376 size * BITS_PER_UNIT, decl);
1377 if (res)
1379 STRIP_USELESS_TYPE_CONVERSION (res);
1380 if (is_gimple_min_invariant (res))
1381 return res;
1384 else
1386 unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
1387 if (native_encode_expr (ctor, buf, size, off) > 0)
1388 return native_interpret_expr (ref->type, buf, size);
1393 return NULL_TREE;
1396 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1397 structures into their value numbers. This is done in-place, and
1398 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1399 whether any operands were valueized. */
1401 static vec<vn_reference_op_s>
1402 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1404 vn_reference_op_t vro;
1405 unsigned int i;
1407 *valueized_anything = false;
1409 FOR_EACH_VEC_ELT (orig, i, vro)
1411 if (vro->opcode == SSA_NAME
1412 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1414 tree tem = SSA_VAL (vro->op0);
1415 if (tem != vro->op0)
1417 *valueized_anything = true;
1418 vro->op0 = tem;
1420 /* If it transforms from an SSA_NAME to a constant, update
1421 the opcode. */
1422 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1423 vro->opcode = TREE_CODE (vro->op0);
1425 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1427 tree tem = SSA_VAL (vro->op1);
1428 if (tem != vro->op1)
1430 *valueized_anything = true;
1431 vro->op1 = tem;
1434 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1436 tree tem = SSA_VAL (vro->op2);
1437 if (tem != vro->op2)
1439 *valueized_anything = true;
1440 vro->op2 = tem;
1443 /* If it transforms from an SSA_NAME to an address, fold with
1444 a preceding indirect reference. */
1445 if (i > 0
1446 && vro->op0
1447 && TREE_CODE (vro->op0) == ADDR_EXPR
1448 && orig[i - 1].opcode == MEM_REF)
1449 vn_reference_fold_indirect (&orig, &i);
1450 else if (i > 0
1451 && vro->opcode == SSA_NAME
1452 && orig[i - 1].opcode == MEM_REF)
1453 vn_reference_maybe_forwprop_address (&orig, &i);
1454 /* If it transforms a non-constant ARRAY_REF into a constant
1455 one, adjust the constant offset. */
1456 else if (vro->opcode == ARRAY_REF
1457 && vro->off == -1
1458 && TREE_CODE (vro->op0) == INTEGER_CST
1459 && TREE_CODE (vro->op1) == INTEGER_CST
1460 && TREE_CODE (vro->op2) == INTEGER_CST)
1462 offset_int off = ((wi::to_offset (vro->op0)
1463 - wi::to_offset (vro->op1))
1464 * wi::to_offset (vro->op2));
1465 if (wi::fits_shwi_p (off))
1466 vro->off = off.to_shwi ();
1470 return orig;
1473 static vec<vn_reference_op_s>
1474 valueize_refs (vec<vn_reference_op_s> orig)
1476 bool tem;
1477 return valueize_refs_1 (orig, &tem);
1480 static vec<vn_reference_op_s> shared_lookup_references;
1482 /* Create a vector of vn_reference_op_s structures from REF, a
1483 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1484 this function. *VALUEIZED_ANYTHING will specify whether any
1485 operands were valueized. */
1487 static vec<vn_reference_op_s>
1488 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1490 if (!ref)
1491 return vNULL;
1492 shared_lookup_references.truncate (0);
1493 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1494 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1495 valueized_anything);
1496 return shared_lookup_references;
1499 /* Create a vector of vn_reference_op_s structures from CALL, a
1500 call statement. The vector is shared among all callers of
1501 this function. */
1503 static vec<vn_reference_op_s>
1504 valueize_shared_reference_ops_from_call (gcall *call)
1506 if (!call)
1507 return vNULL;
1508 shared_lookup_references.truncate (0);
1509 copy_reference_ops_from_call (call, &shared_lookup_references);
1510 shared_lookup_references = valueize_refs (shared_lookup_references);
1511 return shared_lookup_references;
1514 /* Lookup a SCCVN reference operation VR in the current hash table.
1515 Returns the resulting value number if it exists in the hash table,
1516 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1517 vn_reference_t stored in the hashtable if something is found. */
1519 static tree
1520 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1522 vn_reference_s **slot;
1523 hashval_t hash;
1525 hash = vr->hashcode;
1526 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1527 if (!slot && current_info == optimistic_info)
1528 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1529 if (slot)
1531 if (vnresult)
1532 *vnresult = (vn_reference_t)*slot;
1533 return ((vn_reference_t)*slot)->result;
1536 return NULL_TREE;
1539 static tree *last_vuse_ptr;
1540 static vn_lookup_kind vn_walk_kind;
1541 static vn_lookup_kind default_vn_walk_kind;
1543 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1544 with the current VUSE and performs the expression lookup. */
1546 static void *
1547 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1548 unsigned int cnt, void *vr_)
1550 vn_reference_t vr = (vn_reference_t)vr_;
1551 vn_reference_s **slot;
1552 hashval_t hash;
1554 /* This bounds the stmt walks we perform on reference lookups
1555 to O(1) instead of O(N) where N is the number of dominating
1556 stores. */
1557 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1558 return (void *)-1;
1560 if (last_vuse_ptr)
1561 *last_vuse_ptr = vuse;
1563 /* Fixup vuse and hash. */
1564 if (vr->vuse)
1565 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1566 vr->vuse = vuse_ssa_val (vuse);
1567 if (vr->vuse)
1568 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1570 hash = vr->hashcode;
1571 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1572 if (!slot && current_info == optimistic_info)
1573 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1574 if (slot)
1575 return *slot;
1577 return NULL;
1580 /* Lookup an existing or insert a new vn_reference entry into the
1581 value table for the VUSE, SET, TYPE, OPERANDS reference which
1582 has the value VALUE which is either a constant or an SSA name. */
1584 static vn_reference_t
1585 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1586 alias_set_type set,
1587 tree type,
1588 vec<vn_reference_op_s,
1589 va_heap> operands,
1590 tree value)
1592 struct vn_reference_s vr1;
1593 vn_reference_t result;
1594 unsigned value_id;
1595 vr1.vuse = vuse;
1596 vr1.operands = operands;
1597 vr1.type = type;
1598 vr1.set = set;
1599 vr1.hashcode = vn_reference_compute_hash (&vr1);
1600 if (vn_reference_lookup_1 (&vr1, &result))
1601 return result;
1602 if (TREE_CODE (value) == SSA_NAME)
1603 value_id = VN_INFO (value)->value_id;
1604 else
1605 value_id = get_or_alloc_constant_value_id (value);
1606 return vn_reference_insert_pieces (vuse, set, type,
1607 operands.copy (), value, value_id);
1610 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1611 from the statement defining VUSE and if not successful tries to
1612 translate *REFP and VR_ through an aggregate copy at the definition
1613 of VUSE. */
1615 static void *
1616 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1617 bool disambiguate_only)
1619 vn_reference_t vr = (vn_reference_t)vr_;
1620 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1621 tree base;
1622 HOST_WIDE_INT offset, maxsize;
1623 static vec<vn_reference_op_s>
1624 lhs_ops = vNULL;
1625 ao_ref lhs_ref;
1626 bool lhs_ref_ok = false;
1628 /* First try to disambiguate after value-replacing in the definitions LHS. */
1629 if (is_gimple_assign (def_stmt))
1631 vec<vn_reference_op_s> tem;
1632 tree lhs = gimple_assign_lhs (def_stmt);
1633 bool valueized_anything = false;
1634 /* Avoid re-allocation overhead. */
1635 lhs_ops.truncate (0);
1636 copy_reference_ops_from_ref (lhs, &lhs_ops);
1637 tem = lhs_ops;
1638 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1639 gcc_assert (lhs_ops == tem);
1640 if (valueized_anything)
1642 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1643 get_alias_set (lhs),
1644 TREE_TYPE (lhs), lhs_ops);
1645 if (lhs_ref_ok
1646 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1647 return NULL;
1649 else
1651 ao_ref_init (&lhs_ref, lhs);
1652 lhs_ref_ok = true;
1655 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1656 && gimple_call_num_args (def_stmt) <= 4)
1658 /* For builtin calls valueize its arguments and call the
1659 alias oracle again. Valueization may improve points-to
1660 info of pointers and constify size and position arguments.
1661 Originally this was motivated by PR61034 which has
1662 conditional calls to free falsely clobbering ref because
1663 of imprecise points-to info of the argument. */
1664 tree oldargs[4];
1665 bool valueized_anything = false;
1666 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1668 oldargs[i] = gimple_call_arg (def_stmt, i);
1669 if (TREE_CODE (oldargs[i]) == SSA_NAME
1670 && VN_INFO (oldargs[i])->valnum != oldargs[i])
1672 gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum);
1673 valueized_anything = true;
1676 if (valueized_anything)
1678 bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt),
1679 ref);
1680 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1681 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1682 if (!res)
1683 return NULL;
1687 if (disambiguate_only)
1688 return (void *)-1;
1690 base = ao_ref_base (ref);
1691 offset = ref->offset;
1692 maxsize = ref->max_size;
1694 /* If we cannot constrain the size of the reference we cannot
1695 test if anything kills it. */
1696 if (maxsize == -1)
1697 return (void *)-1;
1699 /* We can't deduce anything useful from clobbers. */
1700 if (gimple_clobber_p (def_stmt))
1701 return (void *)-1;
1703 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1704 from that definition.
1705 1) Memset. */
1706 if (is_gimple_reg_type (vr->type)
1707 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1708 && integer_zerop (gimple_call_arg (def_stmt, 1))
1709 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))
1710 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1712 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1713 tree base2;
1714 HOST_WIDE_INT offset2, size2, maxsize2;
1715 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1716 size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8;
1717 if ((unsigned HOST_WIDE_INT)size2 / 8
1718 == tree_to_uhwi (gimple_call_arg (def_stmt, 2))
1719 && maxsize2 != -1
1720 && operand_equal_p (base, base2, 0)
1721 && offset2 <= offset
1722 && offset2 + size2 >= offset + maxsize)
1724 tree val = build_zero_cst (vr->type);
1725 return vn_reference_lookup_or_insert_for_pieces
1726 (vuse, vr->set, vr->type, vr->operands, val);
1730 /* 2) Assignment from an empty CONSTRUCTOR. */
1731 else if (is_gimple_reg_type (vr->type)
1732 && gimple_assign_single_p (def_stmt)
1733 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1734 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1736 tree base2;
1737 HOST_WIDE_INT offset2, size2, maxsize2;
1738 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1739 &offset2, &size2, &maxsize2);
1740 if (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 /* 3) Assignment from a constant. We can use folds native encode/interpret
1752 routines to extract the assigned bits. */
1753 else if (vn_walk_kind == VN_WALKREWRITE
1754 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1755 && ref->size == maxsize
1756 && maxsize % BITS_PER_UNIT == 0
1757 && offset % BITS_PER_UNIT == 0
1758 && is_gimple_reg_type (vr->type)
1759 && gimple_assign_single_p (def_stmt)
1760 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1762 tree base2;
1763 HOST_WIDE_INT offset2, size2, maxsize2;
1764 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1765 &offset2, &size2, &maxsize2);
1766 if (maxsize2 != -1
1767 && maxsize2 == size2
1768 && size2 % BITS_PER_UNIT == 0
1769 && offset2 % BITS_PER_UNIT == 0
1770 && operand_equal_p (base, base2, 0)
1771 && offset2 <= offset
1772 && offset2 + size2 >= offset + maxsize)
1774 /* We support up to 512-bit values (for V8DFmode). */
1775 unsigned char buffer[64];
1776 int len;
1778 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1779 buffer, sizeof (buffer));
1780 if (len > 0)
1782 tree val = native_interpret_expr (vr->type,
1783 buffer
1784 + ((offset - offset2)
1785 / BITS_PER_UNIT),
1786 ref->size / BITS_PER_UNIT);
1787 if (val)
1788 return vn_reference_lookup_or_insert_for_pieces
1789 (vuse, vr->set, vr->type, vr->operands, val);
1794 /* 4) Assignment from an SSA name which definition we may be able
1795 to access pieces from. */
1796 else if (ref->size == maxsize
1797 && is_gimple_reg_type (vr->type)
1798 && gimple_assign_single_p (def_stmt)
1799 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1801 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1802 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1803 if (is_gimple_assign (def_stmt2)
1804 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1805 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1806 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1808 tree base2;
1809 HOST_WIDE_INT offset2, size2, maxsize2, off;
1810 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1811 &offset2, &size2, &maxsize2);
1812 off = offset - offset2;
1813 if (maxsize2 != -1
1814 && maxsize2 == size2
1815 && operand_equal_p (base, base2, 0)
1816 && offset2 <= offset
1817 && offset2 + size2 >= offset + maxsize)
1819 tree val = NULL_TREE;
1820 HOST_WIDE_INT elsz
1821 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1822 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1824 if (off == 0)
1825 val = gimple_assign_rhs1 (def_stmt2);
1826 else if (off == elsz)
1827 val = gimple_assign_rhs2 (def_stmt2);
1829 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1830 && off % elsz == 0)
1832 tree ctor = gimple_assign_rhs1 (def_stmt2);
1833 unsigned i = off / elsz;
1834 if (i < CONSTRUCTOR_NELTS (ctor))
1836 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1837 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1839 if (TREE_CODE (TREE_TYPE (elt->value))
1840 != VECTOR_TYPE)
1841 val = elt->value;
1845 if (val)
1846 return vn_reference_lookup_or_insert_for_pieces
1847 (vuse, vr->set, vr->type, vr->operands, val);
1852 /* 5) For aggregate copies translate the reference through them if
1853 the copy kills ref. */
1854 else if (vn_walk_kind == VN_WALKREWRITE
1855 && gimple_assign_single_p (def_stmt)
1856 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1857 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1858 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1860 tree base2;
1861 HOST_WIDE_INT offset2, size2, maxsize2;
1862 int i, j;
1863 auto_vec<vn_reference_op_s> rhs;
1864 vn_reference_op_t vro;
1865 ao_ref r;
1867 if (!lhs_ref_ok)
1868 return (void *)-1;
1870 /* See if the assignment kills REF. */
1871 base2 = ao_ref_base (&lhs_ref);
1872 offset2 = lhs_ref.offset;
1873 size2 = lhs_ref.size;
1874 maxsize2 = lhs_ref.max_size;
1875 if (maxsize2 == -1
1876 || (base != base2 && !operand_equal_p (base, base2, 0))
1877 || offset2 > offset
1878 || offset2 + size2 < offset + maxsize)
1879 return (void *)-1;
1881 /* Find the common base of ref and the lhs. lhs_ops already
1882 contains valueized operands for the lhs. */
1883 i = vr->operands.length () - 1;
1884 j = lhs_ops.length () - 1;
1885 while (j >= 0 && i >= 0
1886 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1888 i--;
1889 j--;
1892 /* ??? The innermost op should always be a MEM_REF and we already
1893 checked that the assignment to the lhs kills vr. Thus for
1894 aggregate copies using char[] types the vn_reference_op_eq
1895 may fail when comparing types for compatibility. But we really
1896 don't care here - further lookups with the rewritten operands
1897 will simply fail if we messed up types too badly. */
1898 HOST_WIDE_INT extra_off = 0;
1899 if (j == 0 && i >= 0
1900 && lhs_ops[0].opcode == MEM_REF
1901 && lhs_ops[0].off != -1)
1903 if (lhs_ops[0].off == vr->operands[i].off)
1904 i--, j--;
1905 else if (vr->operands[i].opcode == MEM_REF
1906 && vr->operands[i].off != -1)
1908 extra_off = vr->operands[i].off - lhs_ops[0].off;
1909 i--, j--;
1913 /* i now points to the first additional op.
1914 ??? LHS may not be completely contained in VR, one or more
1915 VIEW_CONVERT_EXPRs could be in its way. We could at least
1916 try handling outermost VIEW_CONVERT_EXPRs. */
1917 if (j != -1)
1918 return (void *)-1;
1920 /* Now re-write REF to be based on the rhs of the assignment. */
1921 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1923 /* Apply an extra offset to the inner MEM_REF of the RHS. */
1924 if (extra_off != 0)
1926 if (rhs.length () < 2
1927 || rhs[0].opcode != MEM_REF
1928 || rhs[0].off == -1)
1929 return (void *)-1;
1930 rhs[0].off += extra_off;
1931 rhs[0].op0 = int_const_binop (PLUS_EXPR, rhs[0].op0,
1932 build_int_cst (TREE_TYPE (rhs[0].op0),
1933 extra_off));
1936 /* We need to pre-pend vr->operands[0..i] to rhs. */
1937 vec<vn_reference_op_s> old = vr->operands;
1938 if (i + 1 + rhs.length () > vr->operands.length ())
1940 vr->operands.safe_grow (i + 1 + rhs.length ());
1941 if (old == shared_lookup_references)
1942 shared_lookup_references = vr->operands;
1944 else
1945 vr->operands.truncate (i + 1 + rhs.length ());
1946 FOR_EACH_VEC_ELT (rhs, j, vro)
1947 vr->operands[i + 1 + j] = *vro;
1948 vr->operands = valueize_refs (vr->operands);
1949 if (old == shared_lookup_references)
1950 shared_lookup_references = vr->operands;
1951 vr->hashcode = vn_reference_compute_hash (vr);
1953 /* Try folding the new reference to a constant. */
1954 tree val = fully_constant_vn_reference_p (vr);
1955 if (val)
1956 return vn_reference_lookup_or_insert_for_pieces
1957 (vuse, vr->set, vr->type, vr->operands, val);
1959 /* Adjust *ref from the new operands. */
1960 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1961 return (void *)-1;
1962 /* This can happen with bitfields. */
1963 if (ref->size != r.size)
1964 return (void *)-1;
1965 *ref = r;
1967 /* Do not update last seen VUSE after translating. */
1968 last_vuse_ptr = NULL;
1970 /* Keep looking for the adjusted *REF / VR pair. */
1971 return NULL;
1974 /* 6) For memcpy copies translate the reference through them if
1975 the copy kills ref. */
1976 else if (vn_walk_kind == VN_WALKREWRITE
1977 && is_gimple_reg_type (vr->type)
1978 /* ??? Handle BCOPY as well. */
1979 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1980 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1981 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1982 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1983 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1984 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1985 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1986 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)))
1988 tree lhs, rhs;
1989 ao_ref r;
1990 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1991 vn_reference_op_s op;
1992 HOST_WIDE_INT at;
1995 /* Only handle non-variable, addressable refs. */
1996 if (ref->size != maxsize
1997 || offset % BITS_PER_UNIT != 0
1998 || ref->size % BITS_PER_UNIT != 0)
1999 return (void *)-1;
2001 /* Extract a pointer base and an offset for the destination. */
2002 lhs = gimple_call_arg (def_stmt, 0);
2003 lhs_offset = 0;
2004 if (TREE_CODE (lhs) == SSA_NAME)
2005 lhs = SSA_VAL (lhs);
2006 if (TREE_CODE (lhs) == ADDR_EXPR)
2008 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
2009 &lhs_offset);
2010 if (!tem)
2011 return (void *)-1;
2012 if (TREE_CODE (tem) == MEM_REF
2013 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2015 lhs = TREE_OPERAND (tem, 0);
2016 lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2018 else if (DECL_P (tem))
2019 lhs = build_fold_addr_expr (tem);
2020 else
2021 return (void *)-1;
2023 if (TREE_CODE (lhs) != SSA_NAME
2024 && TREE_CODE (lhs) != ADDR_EXPR)
2025 return (void *)-1;
2027 /* Extract a pointer base and an offset for the source. */
2028 rhs = gimple_call_arg (def_stmt, 1);
2029 rhs_offset = 0;
2030 if (TREE_CODE (rhs) == SSA_NAME)
2031 rhs = SSA_VAL (rhs);
2032 if (TREE_CODE (rhs) == ADDR_EXPR)
2034 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
2035 &rhs_offset);
2036 if (!tem)
2037 return (void *)-1;
2038 if (TREE_CODE (tem) == MEM_REF
2039 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
2041 rhs = TREE_OPERAND (tem, 0);
2042 rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
2044 else if (DECL_P (tem))
2045 rhs = build_fold_addr_expr (tem);
2046 else
2047 return (void *)-1;
2049 if (TREE_CODE (rhs) != SSA_NAME
2050 && TREE_CODE (rhs) != ADDR_EXPR)
2051 return (void *)-1;
2053 copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2));
2055 /* The bases of the destination and the references have to agree. */
2056 if ((TREE_CODE (base) != MEM_REF
2057 && !DECL_P (base))
2058 || (TREE_CODE (base) == MEM_REF
2059 && (TREE_OPERAND (base, 0) != lhs
2060 || !tree_fits_uhwi_p (TREE_OPERAND (base, 1))))
2061 || (DECL_P (base)
2062 && (TREE_CODE (lhs) != ADDR_EXPR
2063 || TREE_OPERAND (lhs, 0) != base)))
2064 return (void *)-1;
2066 /* And the access has to be contained within the memcpy destination. */
2067 at = offset / BITS_PER_UNIT;
2068 if (TREE_CODE (base) == MEM_REF)
2069 at += tree_to_uhwi (TREE_OPERAND (base, 1));
2070 if (lhs_offset > at
2071 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
2072 return (void *)-1;
2074 /* Make room for 2 operands in the new reference. */
2075 if (vr->operands.length () < 2)
2077 vec<vn_reference_op_s> old = vr->operands;
2078 vr->operands.safe_grow_cleared (2);
2079 if (old == shared_lookup_references
2080 && vr->operands != old)
2081 shared_lookup_references = vr->operands;
2083 else
2084 vr->operands.truncate (2);
2086 /* The looked-through reference is a simple MEM_REF. */
2087 memset (&op, 0, sizeof (op));
2088 op.type = vr->type;
2089 op.opcode = MEM_REF;
2090 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
2091 op.off = at - lhs_offset + rhs_offset;
2092 vr->operands[0] = op;
2093 op.type = TREE_TYPE (rhs);
2094 op.opcode = TREE_CODE (rhs);
2095 op.op0 = rhs;
2096 op.off = -1;
2097 vr->operands[1] = op;
2098 vr->hashcode = vn_reference_compute_hash (vr);
2100 /* Adjust *ref from the new operands. */
2101 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2102 return (void *)-1;
2103 /* This can happen with bitfields. */
2104 if (ref->size != r.size)
2105 return (void *)-1;
2106 *ref = r;
2108 /* Do not update last seen VUSE after translating. */
2109 last_vuse_ptr = NULL;
2111 /* Keep looking for the adjusted *REF / VR pair. */
2112 return NULL;
2115 /* Bail out and stop walking. */
2116 return (void *)-1;
2119 /* Lookup a reference operation by it's parts, in the current hash table.
2120 Returns the resulting value number if it exists in the hash table,
2121 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2122 vn_reference_t stored in the hashtable if something is found. */
2124 tree
2125 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2126 vec<vn_reference_op_s> operands,
2127 vn_reference_t *vnresult, vn_lookup_kind kind)
2129 struct vn_reference_s vr1;
2130 vn_reference_t tmp;
2131 tree cst;
2133 if (!vnresult)
2134 vnresult = &tmp;
2135 *vnresult = NULL;
2137 vr1.vuse = vuse_ssa_val (vuse);
2138 shared_lookup_references.truncate (0);
2139 shared_lookup_references.safe_grow (operands.length ());
2140 memcpy (shared_lookup_references.address (),
2141 operands.address (),
2142 sizeof (vn_reference_op_s)
2143 * operands.length ());
2144 vr1.operands = operands = shared_lookup_references
2145 = valueize_refs (shared_lookup_references);
2146 vr1.type = type;
2147 vr1.set = set;
2148 vr1.hashcode = vn_reference_compute_hash (&vr1);
2149 if ((cst = fully_constant_vn_reference_p (&vr1)))
2150 return cst;
2152 vn_reference_lookup_1 (&vr1, vnresult);
2153 if (!*vnresult
2154 && kind != VN_NOWALK
2155 && vr1.vuse)
2157 ao_ref r;
2158 vn_walk_kind = kind;
2159 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2160 *vnresult =
2161 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2162 vn_reference_lookup_2,
2163 vn_reference_lookup_3,
2164 vuse_ssa_val, &vr1);
2165 gcc_checking_assert (vr1.operands == shared_lookup_references);
2168 if (*vnresult)
2169 return (*vnresult)->result;
2171 return NULL_TREE;
2174 /* Lookup OP in the current hash table, and return the resulting value
2175 number if it exists in the hash table. Return NULL_TREE if it does
2176 not exist in the hash table or if the result field of the structure
2177 was NULL.. VNRESULT will be filled in with the vn_reference_t
2178 stored in the hashtable if one exists. */
2180 tree
2181 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2182 vn_reference_t *vnresult)
2184 vec<vn_reference_op_s> operands;
2185 struct vn_reference_s vr1;
2186 tree cst;
2187 bool valuezied_anything;
2189 if (vnresult)
2190 *vnresult = NULL;
2192 vr1.vuse = vuse_ssa_val (vuse);
2193 vr1.operands = operands
2194 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2195 vr1.type = TREE_TYPE (op);
2196 vr1.set = get_alias_set (op);
2197 vr1.hashcode = vn_reference_compute_hash (&vr1);
2198 if ((cst = fully_constant_vn_reference_p (&vr1)))
2199 return cst;
2201 if (kind != VN_NOWALK
2202 && vr1.vuse)
2204 vn_reference_t wvnresult;
2205 ao_ref r;
2206 /* Make sure to use a valueized reference if we valueized anything.
2207 Otherwise preserve the full reference for advanced TBAA. */
2208 if (!valuezied_anything
2209 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2210 vr1.operands))
2211 ao_ref_init (&r, op);
2212 vn_walk_kind = kind;
2213 wvnresult =
2214 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2215 vn_reference_lookup_2,
2216 vn_reference_lookup_3,
2217 vuse_ssa_val, &vr1);
2218 gcc_checking_assert (vr1.operands == shared_lookup_references);
2219 if (wvnresult)
2221 if (vnresult)
2222 *vnresult = wvnresult;
2223 return wvnresult->result;
2226 return NULL_TREE;
2229 return vn_reference_lookup_1 (&vr1, vnresult);
2232 /* Lookup CALL in the current hash table and return the entry in
2233 *VNRESULT if found. Populates *VR for the hashtable lookup. */
2235 void
2236 vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult,
2237 vn_reference_t vr)
2239 if (vnresult)
2240 *vnresult = NULL;
2242 tree vuse = gimple_vuse (call);
2244 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2245 vr->operands = valueize_shared_reference_ops_from_call (call);
2246 vr->type = gimple_expr_type (call);
2247 vr->set = 0;
2248 vr->hashcode = vn_reference_compute_hash (vr);
2249 vn_reference_lookup_1 (vr, vnresult);
2252 /* Insert OP into the current hash table with a value number of
2253 RESULT, and return the resulting reference structure we created. */
2255 static vn_reference_t
2256 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2258 vn_reference_s **slot;
2259 vn_reference_t vr1;
2260 bool tem;
2262 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2263 if (TREE_CODE (result) == SSA_NAME)
2264 vr1->value_id = VN_INFO (result)->value_id;
2265 else
2266 vr1->value_id = get_or_alloc_constant_value_id (result);
2267 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2268 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2269 vr1->type = TREE_TYPE (op);
2270 vr1->set = get_alias_set (op);
2271 vr1->hashcode = vn_reference_compute_hash (vr1);
2272 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2273 vr1->result_vdef = vdef;
2275 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2276 INSERT);
2278 /* Because we lookup stores using vuses, and value number failures
2279 using the vdefs (see visit_reference_op_store for how and why),
2280 it's possible that on failure we may try to insert an already
2281 inserted store. This is not wrong, there is no ssa name for a
2282 store that we could use as a differentiator anyway. Thus, unlike
2283 the other lookup functions, you cannot gcc_assert (!*slot)
2284 here. */
2286 /* But free the old slot in case of a collision. */
2287 if (*slot)
2288 free_reference (*slot);
2290 *slot = vr1;
2291 return vr1;
2294 /* Insert a reference by it's pieces into the current hash table with
2295 a value number of RESULT. Return the resulting reference
2296 structure we created. */
2298 vn_reference_t
2299 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2300 vec<vn_reference_op_s> operands,
2301 tree result, unsigned int value_id)
2304 vn_reference_s **slot;
2305 vn_reference_t vr1;
2307 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2308 vr1->value_id = value_id;
2309 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2310 vr1->operands = valueize_refs (operands);
2311 vr1->type = type;
2312 vr1->set = set;
2313 vr1->hashcode = vn_reference_compute_hash (vr1);
2314 if (result && TREE_CODE (result) == SSA_NAME)
2315 result = SSA_VAL (result);
2316 vr1->result = result;
2318 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2319 INSERT);
2321 /* At this point we should have all the things inserted that we have
2322 seen before, and we should never try inserting something that
2323 already exists. */
2324 gcc_assert (!*slot);
2325 if (*slot)
2326 free_reference (*slot);
2328 *slot = vr1;
2329 return vr1;
2332 /* Compute and return the hash value for nary operation VBO1. */
2334 static hashval_t
2335 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2337 inchash::hash hstate;
2338 unsigned i;
2340 for (i = 0; i < vno1->length; ++i)
2341 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2342 vno1->op[i] = SSA_VAL (vno1->op[i]);
2344 if (vno1->length == 2
2345 && commutative_tree_code (vno1->opcode)
2346 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2348 tree temp = vno1->op[0];
2349 vno1->op[0] = vno1->op[1];
2350 vno1->op[1] = temp;
2353 hstate.add_int (vno1->opcode);
2354 for (i = 0; i < vno1->length; ++i)
2355 inchash::add_expr (vno1->op[i], hstate);
2357 return hstate.end ();
2360 /* Compare nary operations VNO1 and VNO2 and return true if they are
2361 equivalent. */
2363 bool
2364 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2366 unsigned i;
2368 if (vno1->hashcode != vno2->hashcode)
2369 return false;
2371 if (vno1->length != vno2->length)
2372 return false;
2374 if (vno1->opcode != vno2->opcode
2375 || !types_compatible_p (vno1->type, vno2->type))
2376 return false;
2378 for (i = 0; i < vno1->length; ++i)
2379 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2380 return false;
2382 return true;
2385 /* Initialize VNO from the pieces provided. */
2387 static void
2388 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2389 enum tree_code code, tree type, tree *ops)
2391 vno->opcode = code;
2392 vno->length = length;
2393 vno->type = type;
2394 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2397 /* Initialize VNO from OP. */
2399 static void
2400 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2402 unsigned i;
2404 vno->opcode = TREE_CODE (op);
2405 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2406 vno->type = TREE_TYPE (op);
2407 for (i = 0; i < vno->length; ++i)
2408 vno->op[i] = TREE_OPERAND (op, i);
2411 /* Return the number of operands for a vn_nary ops structure from STMT. */
2413 static unsigned int
2414 vn_nary_length_from_stmt (gimple stmt)
2416 switch (gimple_assign_rhs_code (stmt))
2418 case REALPART_EXPR:
2419 case IMAGPART_EXPR:
2420 case VIEW_CONVERT_EXPR:
2421 return 1;
2423 case BIT_FIELD_REF:
2424 return 3;
2426 case CONSTRUCTOR:
2427 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2429 default:
2430 return gimple_num_ops (stmt) - 1;
2434 /* Initialize VNO from STMT. */
2436 static void
2437 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2439 unsigned i;
2441 vno->opcode = gimple_assign_rhs_code (stmt);
2442 vno->type = gimple_expr_type (stmt);
2443 switch (vno->opcode)
2445 case REALPART_EXPR:
2446 case IMAGPART_EXPR:
2447 case VIEW_CONVERT_EXPR:
2448 vno->length = 1;
2449 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2450 break;
2452 case BIT_FIELD_REF:
2453 vno->length = 3;
2454 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2455 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2456 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2457 break;
2459 case CONSTRUCTOR:
2460 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2461 for (i = 0; i < vno->length; ++i)
2462 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2463 break;
2465 default:
2466 gcc_checking_assert (!gimple_assign_single_p (stmt));
2467 vno->length = gimple_num_ops (stmt) - 1;
2468 for (i = 0; i < vno->length; ++i)
2469 vno->op[i] = gimple_op (stmt, i + 1);
2473 /* Compute the hashcode for VNO and look for it in the hash table;
2474 return the resulting value number if it exists in the hash table.
2475 Return NULL_TREE if it does not exist in the hash table or if the
2476 result field of the operation is NULL. VNRESULT will contain the
2477 vn_nary_op_t from the hashtable if it exists. */
2479 static tree
2480 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2482 vn_nary_op_s **slot;
2484 if (vnresult)
2485 *vnresult = NULL;
2487 vno->hashcode = vn_nary_op_compute_hash (vno);
2488 slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode,
2489 NO_INSERT);
2490 if (!slot && current_info == optimistic_info)
2491 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode,
2492 NO_INSERT);
2493 if (!slot)
2494 return NULL_TREE;
2495 if (vnresult)
2496 *vnresult = *slot;
2497 return (*slot)->result;
2500 /* Lookup a n-ary operation by its pieces and return the resulting value
2501 number if it exists in the hash table. Return NULL_TREE if it does
2502 not exist in the hash table or if the result field of the operation
2503 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2504 if it exists. */
2506 tree
2507 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2508 tree type, tree *ops, vn_nary_op_t *vnresult)
2510 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2511 sizeof_vn_nary_op (length));
2512 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2513 return vn_nary_op_lookup_1 (vno1, vnresult);
2516 /* Lookup OP in the current hash table, and return the resulting value
2517 number if it exists in the hash table. Return NULL_TREE if it does
2518 not exist in the hash table or if the result field of the operation
2519 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2520 if it exists. */
2522 tree
2523 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2525 vn_nary_op_t vno1
2526 = XALLOCAVAR (struct vn_nary_op_s,
2527 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2528 init_vn_nary_op_from_op (vno1, op);
2529 return vn_nary_op_lookup_1 (vno1, vnresult);
2532 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2533 value number if it exists in the hash table. Return NULL_TREE if
2534 it does not exist in the hash table. VNRESULT will contain the
2535 vn_nary_op_t from the hashtable if it exists. */
2537 tree
2538 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2540 vn_nary_op_t vno1
2541 = XALLOCAVAR (struct vn_nary_op_s,
2542 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2543 init_vn_nary_op_from_stmt (vno1, stmt);
2544 return vn_nary_op_lookup_1 (vno1, vnresult);
2547 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2549 static vn_nary_op_t
2550 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2552 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2555 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2556 obstack. */
2558 static vn_nary_op_t
2559 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2561 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2562 &current_info->nary_obstack);
2564 vno1->value_id = value_id;
2565 vno1->length = length;
2566 vno1->result = result;
2568 return vno1;
2571 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2572 VNO->HASHCODE first. */
2574 static vn_nary_op_t
2575 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
2576 bool compute_hash)
2578 vn_nary_op_s **slot;
2580 if (compute_hash)
2581 vno->hashcode = vn_nary_op_compute_hash (vno);
2583 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
2584 gcc_assert (!*slot);
2586 *slot = vno;
2587 return vno;
2590 /* Insert a n-ary operation into the current hash table using it's
2591 pieces. Return the vn_nary_op_t structure we created and put in
2592 the hashtable. */
2594 vn_nary_op_t
2595 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2596 tree type, tree *ops,
2597 tree result, unsigned int value_id)
2599 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2600 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2601 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2604 /* Insert OP into the current hash table with a value number of
2605 RESULT. Return the vn_nary_op_t structure we created and put in
2606 the hashtable. */
2608 vn_nary_op_t
2609 vn_nary_op_insert (tree op, tree result)
2611 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2612 vn_nary_op_t vno1;
2614 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2615 init_vn_nary_op_from_op (vno1, op);
2616 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2619 /* Insert the rhs of STMT into the current hash table with a value number of
2620 RESULT. */
2622 vn_nary_op_t
2623 vn_nary_op_insert_stmt (gimple stmt, tree result)
2625 vn_nary_op_t vno1
2626 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2627 result, VN_INFO (result)->value_id);
2628 init_vn_nary_op_from_stmt (vno1, stmt);
2629 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2632 /* Compute a hashcode for PHI operation VP1 and return it. */
2634 static inline hashval_t
2635 vn_phi_compute_hash (vn_phi_t vp1)
2637 inchash::hash hstate (vp1->block->index);
2638 int i;
2639 tree phi1op;
2640 tree type;
2642 /* If all PHI arguments are constants we need to distinguish
2643 the PHI node via its type. */
2644 type = vp1->type;
2645 hstate.merge_hash (vn_hash_type (type));
2647 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2649 if (phi1op == VN_TOP)
2650 continue;
2651 inchash::add_expr (phi1op, hstate);
2654 return hstate.end ();
2657 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2659 static int
2660 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2662 if (vp1->hashcode != vp2->hashcode)
2663 return false;
2665 if (vp1->block == vp2->block)
2667 int i;
2668 tree phi1op;
2670 /* If the PHI nodes do not have compatible types
2671 they are not the same. */
2672 if (!types_compatible_p (vp1->type, vp2->type))
2673 return false;
2675 /* Any phi in the same block will have it's arguments in the
2676 same edge order, because of how we store phi nodes. */
2677 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2679 tree phi2op = vp2->phiargs[i];
2680 if (phi1op == VN_TOP || phi2op == VN_TOP)
2681 continue;
2682 if (!expressions_equal_p (phi1op, phi2op))
2683 return false;
2685 return true;
2687 return false;
2690 static vec<tree> shared_lookup_phiargs;
2692 /* Lookup PHI in the current hash table, and return the resulting
2693 value number if it exists in the hash table. Return NULL_TREE if
2694 it does not exist in the hash table. */
2696 static tree
2697 vn_phi_lookup (gimple phi)
2699 vn_phi_s **slot;
2700 struct vn_phi_s vp1;
2701 unsigned i;
2703 shared_lookup_phiargs.truncate (0);
2705 /* Canonicalize the SSA_NAME's to their value number. */
2706 for (i = 0; i < gimple_phi_num_args (phi); i++)
2708 tree def = PHI_ARG_DEF (phi, i);
2709 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2710 shared_lookup_phiargs.safe_push (def);
2712 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2713 vp1.phiargs = shared_lookup_phiargs;
2714 vp1.block = gimple_bb (phi);
2715 vp1.hashcode = vn_phi_compute_hash (&vp1);
2716 slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2717 NO_INSERT);
2718 if (!slot && current_info == optimistic_info)
2719 slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2720 NO_INSERT);
2721 if (!slot)
2722 return NULL_TREE;
2723 return (*slot)->result;
2726 /* Insert PHI into the current hash table with a value number of
2727 RESULT. */
2729 static vn_phi_t
2730 vn_phi_insert (gimple phi, tree result)
2732 vn_phi_s **slot;
2733 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2734 unsigned i;
2735 vec<tree> args = vNULL;
2737 /* Canonicalize the SSA_NAME's to their value number. */
2738 for (i = 0; i < gimple_phi_num_args (phi); i++)
2740 tree def = PHI_ARG_DEF (phi, i);
2741 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2742 args.safe_push (def);
2744 vp1->value_id = VN_INFO (result)->value_id;
2745 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2746 vp1->phiargs = args;
2747 vp1->block = gimple_bb (phi);
2748 vp1->result = result;
2749 vp1->hashcode = vn_phi_compute_hash (vp1);
2751 slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2753 /* Because we iterate over phi operations more than once, it's
2754 possible the slot might already exist here, hence no assert.*/
2755 *slot = vp1;
2756 return vp1;
2760 /* Print set of components in strongly connected component SCC to OUT. */
2762 static void
2763 print_scc (FILE *out, vec<tree> scc)
2765 tree var;
2766 unsigned int i;
2768 fprintf (out, "SCC consists of:");
2769 FOR_EACH_VEC_ELT (scc, i, var)
2771 fprintf (out, " ");
2772 print_generic_expr (out, var, 0);
2774 fprintf (out, "\n");
2777 /* Set the value number of FROM to TO, return true if it has changed
2778 as a result. */
2780 static inline bool
2781 set_ssa_val_to (tree from, tree to)
2783 tree currval = SSA_VAL (from);
2784 HOST_WIDE_INT toff, coff;
2786 /* The only thing we allow as value numbers are ssa_names
2787 and invariants. So assert that here. We don't allow VN_TOP
2788 as visiting a stmt should produce a value-number other than
2789 that.
2790 ??? Still VN_TOP can happen for unreachable code, so force
2791 it to varying in that case. Not all code is prepared to
2792 get VN_TOP on valueization. */
2793 if (to == VN_TOP)
2795 if (dump_file && (dump_flags & TDF_DETAILS))
2796 fprintf (dump_file, "Forcing value number to varying on "
2797 "receiving VN_TOP\n");
2798 to = from;
2801 gcc_assert (to != NULL_TREE
2802 && (TREE_CODE (to) == SSA_NAME
2803 || is_gimple_min_invariant (to)));
2805 if (from != to)
2807 if (currval == from)
2809 if (dump_file && (dump_flags & TDF_DETAILS))
2811 fprintf (dump_file, "Not changing value number of ");
2812 print_generic_expr (dump_file, from, 0);
2813 fprintf (dump_file, " from VARYING to ");
2814 print_generic_expr (dump_file, to, 0);
2815 fprintf (dump_file, "\n");
2817 return false;
2819 else if (TREE_CODE (to) == SSA_NAME
2820 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2821 to = from;
2824 if (dump_file && (dump_flags & TDF_DETAILS))
2826 fprintf (dump_file, "Setting value number of ");
2827 print_generic_expr (dump_file, from, 0);
2828 fprintf (dump_file, " to ");
2829 print_generic_expr (dump_file, to, 0);
2832 if (currval != to
2833 && !operand_equal_p (currval, to, 0)
2834 /* ??? For addresses involving volatile objects or types operand_equal_p
2835 does not reliably detect ADDR_EXPRs as equal. We know we are only
2836 getting invariant gimple addresses here, so can use
2837 get_addr_base_and_unit_offset to do this comparison. */
2838 && !(TREE_CODE (currval) == ADDR_EXPR
2839 && TREE_CODE (to) == ADDR_EXPR
2840 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2841 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2842 && coff == toff))
2844 VN_INFO (from)->valnum = to;
2845 if (dump_file && (dump_flags & TDF_DETAILS))
2846 fprintf (dump_file, " (changed)\n");
2847 return true;
2849 if (dump_file && (dump_flags & TDF_DETAILS))
2850 fprintf (dump_file, "\n");
2851 return false;
2854 /* Mark as processed all the definitions in the defining stmt of USE, or
2855 the USE itself. */
2857 static void
2858 mark_use_processed (tree use)
2860 ssa_op_iter iter;
2861 def_operand_p defp;
2862 gimple stmt = SSA_NAME_DEF_STMT (use);
2864 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2866 VN_INFO (use)->use_processed = true;
2867 return;
2870 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2872 tree def = DEF_FROM_PTR (defp);
2874 VN_INFO (def)->use_processed = true;
2878 /* Set all definitions in STMT to value number to themselves.
2879 Return true if a value number changed. */
2881 static bool
2882 defs_to_varying (gimple stmt)
2884 bool changed = false;
2885 ssa_op_iter iter;
2886 def_operand_p defp;
2888 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2890 tree def = DEF_FROM_PTR (defp);
2891 changed |= set_ssa_val_to (def, def);
2893 return changed;
2896 static bool expr_has_constants (tree expr);
2898 /* Visit a copy between LHS and RHS, return true if the value number
2899 changed. */
2901 static bool
2902 visit_copy (tree lhs, tree rhs)
2904 /* The copy may have a more interesting constant filled expression
2905 (we don't, since we know our RHS is just an SSA name). */
2906 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2907 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2909 /* And finally valueize. */
2910 rhs = SSA_VAL (rhs);
2912 return set_ssa_val_to (lhs, rhs);
2915 /* Visit a nary operator RHS, value number it, and return true if the
2916 value number of LHS has changed as a result. */
2918 static bool
2919 visit_nary_op (tree lhs, gimple stmt)
2921 bool changed = false;
2922 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2924 if (result)
2925 changed = set_ssa_val_to (lhs, result);
2926 else
2928 changed = set_ssa_val_to (lhs, lhs);
2929 vn_nary_op_insert_stmt (stmt, lhs);
2932 return changed;
2935 /* Visit a call STMT storing into LHS. Return true if the value number
2936 of the LHS has changed as a result. */
2938 static bool
2939 visit_reference_op_call (tree lhs, gcall *stmt)
2941 bool changed = false;
2942 struct vn_reference_s vr1;
2943 vn_reference_t vnresult = NULL;
2944 tree vdef = gimple_vdef (stmt);
2946 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2947 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2948 lhs = NULL_TREE;
2950 vn_reference_lookup_call (stmt, &vnresult, &vr1);
2951 if (vnresult)
2953 if (vnresult->result_vdef && vdef)
2954 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2956 if (!vnresult->result && lhs)
2957 vnresult->result = lhs;
2959 if (vnresult->result && lhs)
2961 changed |= set_ssa_val_to (lhs, vnresult->result);
2963 if (VN_INFO (vnresult->result)->has_constants)
2964 VN_INFO (lhs)->has_constants = true;
2967 else
2969 vn_reference_t vr2;
2970 vn_reference_s **slot;
2971 if (vdef)
2972 changed |= set_ssa_val_to (vdef, vdef);
2973 if (lhs)
2974 changed |= set_ssa_val_to (lhs, lhs);
2975 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2976 vr2->vuse = vr1.vuse;
2977 /* As we are not walking the virtual operand chain we know the
2978 shared_lookup_references are still original so we can re-use
2979 them here. */
2980 vr2->operands = vr1.operands.copy ();
2981 vr2->type = vr1.type;
2982 vr2->set = vr1.set;
2983 vr2->hashcode = vr1.hashcode;
2984 vr2->result = lhs;
2985 vr2->result_vdef = vdef;
2986 slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode,
2987 INSERT);
2988 gcc_assert (!*slot);
2989 *slot = vr2;
2992 return changed;
2995 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2996 and return true if the value number of the LHS has changed as a result. */
2998 static bool
2999 visit_reference_op_load (tree lhs, tree op, gimple stmt)
3001 bool changed = false;
3002 tree last_vuse;
3003 tree result;
3005 last_vuse = gimple_vuse (stmt);
3006 last_vuse_ptr = &last_vuse;
3007 result = vn_reference_lookup (op, gimple_vuse (stmt),
3008 default_vn_walk_kind, NULL);
3009 last_vuse_ptr = NULL;
3011 /* We handle type-punning through unions by value-numbering based
3012 on offset and size of the access. Be prepared to handle a
3013 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
3014 if (result
3015 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
3017 /* We will be setting the value number of lhs to the value number
3018 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
3019 So first simplify and lookup this expression to see if it
3020 is already available. */
3021 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
3022 if ((CONVERT_EXPR_P (val)
3023 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
3024 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
3026 tree tem = vn_get_expr_for (TREE_OPERAND (val, 0));
3027 if ((CONVERT_EXPR_P (tem)
3028 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
3029 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
3030 TREE_TYPE (val), tem)))
3031 val = tem;
3033 result = val;
3034 if (!is_gimple_min_invariant (val)
3035 && TREE_CODE (val) != SSA_NAME)
3036 result = vn_nary_op_lookup (val, NULL);
3037 /* If the expression is not yet available, value-number lhs to
3038 a new SSA_NAME we create. */
3039 if (!result)
3041 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
3042 "vntemp");
3043 /* Initialize value-number information properly. */
3044 VN_INFO_GET (result)->valnum = result;
3045 VN_INFO (result)->value_id = get_next_value_id ();
3046 VN_INFO (result)->expr = val;
3047 VN_INFO (result)->has_constants = expr_has_constants (val);
3048 VN_INFO (result)->needs_insertion = true;
3049 /* As all "inserted" statements are singleton SCCs, insert
3050 to the valid table. This is strictly needed to
3051 avoid re-generating new value SSA_NAMEs for the same
3052 expression during SCC iteration over and over (the
3053 optimistic table gets cleared after each iteration).
3054 We do not need to insert into the optimistic table, as
3055 lookups there will fall back to the valid table. */
3056 if (current_info == optimistic_info)
3058 current_info = valid_info;
3059 vn_nary_op_insert (val, result);
3060 current_info = optimistic_info;
3062 else
3063 vn_nary_op_insert (val, result);
3064 if (dump_file && (dump_flags & TDF_DETAILS))
3066 fprintf (dump_file, "Inserting name ");
3067 print_generic_expr (dump_file, result, 0);
3068 fprintf (dump_file, " for expression ");
3069 print_generic_expr (dump_file, val, 0);
3070 fprintf (dump_file, "\n");
3075 if (result)
3077 changed = set_ssa_val_to (lhs, result);
3078 if (TREE_CODE (result) == SSA_NAME
3079 && VN_INFO (result)->has_constants)
3081 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
3082 VN_INFO (lhs)->has_constants = true;
3085 else
3087 changed = set_ssa_val_to (lhs, lhs);
3088 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3091 return changed;
3095 /* Visit a store to a reference operator LHS, part of STMT, value number it,
3096 and return true if the value number of the LHS has changed as a result. */
3098 static bool
3099 visit_reference_op_store (tree lhs, tree op, gimple stmt)
3101 bool changed = false;
3102 vn_reference_t vnresult = NULL;
3103 tree result, assign;
3104 bool resultsame = false;
3105 tree vuse = gimple_vuse (stmt);
3106 tree vdef = gimple_vdef (stmt);
3108 /* First we want to lookup using the *vuses* from the store and see
3109 if there the last store to this location with the same address
3110 had the same value.
3112 The vuses represent the memory state before the store. If the
3113 memory state, address, and value of the store is the same as the
3114 last store to this location, then this store will produce the
3115 same memory state as that store.
3117 In this case the vdef versions for this store are value numbered to those
3118 vuse versions, since they represent the same memory state after
3119 this store.
3121 Otherwise, the vdefs for the store are used when inserting into
3122 the table, since the store generates a new memory state. */
3124 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
3126 if (result)
3128 if (TREE_CODE (result) == SSA_NAME)
3129 result = SSA_VAL (result);
3130 if (TREE_CODE (op) == SSA_NAME)
3131 op = SSA_VAL (op);
3132 resultsame = expressions_equal_p (result, op);
3135 if ((!result || !resultsame)
3136 /* Only perform the following when being called from PRE
3137 which embeds tail merging. */
3138 && default_vn_walk_kind == VN_WALK)
3140 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3141 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
3142 if (vnresult)
3144 VN_INFO (vdef)->use_processed = true;
3145 return set_ssa_val_to (vdef, vnresult->result_vdef);
3149 if (!result || !resultsame)
3151 if (dump_file && (dump_flags & TDF_DETAILS))
3153 fprintf (dump_file, "No store match\n");
3154 fprintf (dump_file, "Value numbering store ");
3155 print_generic_expr (dump_file, lhs, 0);
3156 fprintf (dump_file, " to ");
3157 print_generic_expr (dump_file, op, 0);
3158 fprintf (dump_file, "\n");
3160 /* Have to set value numbers before insert, since insert is
3161 going to valueize the references in-place. */
3162 if (vdef)
3164 changed |= set_ssa_val_to (vdef, vdef);
3167 /* Do not insert structure copies into the tables. */
3168 if (is_gimple_min_invariant (op)
3169 || is_gimple_reg (op))
3170 vn_reference_insert (lhs, op, vdef, NULL);
3172 /* Only perform the following when being called from PRE
3173 which embeds tail merging. */
3174 if (default_vn_walk_kind == VN_WALK)
3176 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3177 vn_reference_insert (assign, lhs, vuse, vdef);
3180 else
3182 /* We had a match, so value number the vdef to have the value
3183 number of the vuse it came from. */
3185 if (dump_file && (dump_flags & TDF_DETAILS))
3186 fprintf (dump_file, "Store matched earlier value,"
3187 "value numbering store vdefs to matching vuses.\n");
3189 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3192 return changed;
3195 /* Visit and value number PHI, return true if the value number
3196 changed. */
3198 static bool
3199 visit_phi (gimple phi)
3201 bool changed = false;
3202 tree result;
3203 tree sameval = VN_TOP;
3204 bool allsame = true;
3206 /* TODO: We could check for this in init_sccvn, and replace this
3207 with a gcc_assert. */
3208 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3209 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3211 /* See if all non-TOP arguments have the same value. TOP is
3212 equivalent to everything, so we can ignore it. */
3213 edge_iterator ei;
3214 edge e;
3215 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3216 if (e->flags & EDGE_EXECUTABLE)
3218 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3220 if (TREE_CODE (def) == SSA_NAME)
3221 def = SSA_VAL (def);
3222 if (def == VN_TOP)
3223 continue;
3224 if (sameval == VN_TOP)
3226 sameval = def;
3228 else
3230 if (!expressions_equal_p (def, sameval))
3232 allsame = false;
3233 break;
3238 /* If all value numbered to the same value, the phi node has that
3239 value. */
3240 if (allsame)
3241 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3243 /* Otherwise, see if it is equivalent to a phi node in this block. */
3244 result = vn_phi_lookup (phi);
3245 if (result)
3246 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3247 else
3249 vn_phi_insert (phi, PHI_RESULT (phi));
3250 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3251 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3252 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3255 return changed;
3258 /* Return true if EXPR contains constants. */
3260 static bool
3261 expr_has_constants (tree expr)
3263 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3265 case tcc_unary:
3266 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3268 case tcc_binary:
3269 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3270 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3271 /* Constants inside reference ops are rarely interesting, but
3272 it can take a lot of looking to find them. */
3273 case tcc_reference:
3274 case tcc_declaration:
3275 return false;
3276 default:
3277 return is_gimple_min_invariant (expr);
3279 return false;
3282 /* Return true if STMT contains constants. */
3284 static bool
3285 stmt_has_constants (gimple stmt)
3287 tree tem;
3289 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3290 return false;
3292 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3294 case GIMPLE_TERNARY_RHS:
3295 tem = gimple_assign_rhs3 (stmt);
3296 if (TREE_CODE (tem) == SSA_NAME)
3297 tem = SSA_VAL (tem);
3298 if (is_gimple_min_invariant (tem))
3299 return true;
3300 /* Fallthru. */
3302 case GIMPLE_BINARY_RHS:
3303 tem = gimple_assign_rhs2 (stmt);
3304 if (TREE_CODE (tem) == SSA_NAME)
3305 tem = SSA_VAL (tem);
3306 if (is_gimple_min_invariant (tem))
3307 return true;
3308 /* Fallthru. */
3310 case GIMPLE_SINGLE_RHS:
3311 /* Constants inside reference ops are rarely interesting, but
3312 it can take a lot of looking to find them. */
3313 case GIMPLE_UNARY_RHS:
3314 tem = gimple_assign_rhs1 (stmt);
3315 if (TREE_CODE (tem) == SSA_NAME)
3316 tem = SSA_VAL (tem);
3317 return is_gimple_min_invariant (tem);
3319 default:
3320 gcc_unreachable ();
3322 return false;
3325 /* Simplify the binary expression RHS, and return the result if
3326 simplified. */
3328 static tree
3329 simplify_binary_expression (gimple stmt)
3331 tree result = NULL_TREE;
3332 tree op0 = gimple_assign_rhs1 (stmt);
3333 tree op1 = gimple_assign_rhs2 (stmt);
3334 enum tree_code code = gimple_assign_rhs_code (stmt);
3336 /* This will not catch every single case we could combine, but will
3337 catch those with constants. The goal here is to simultaneously
3338 combine constants between expressions, but avoid infinite
3339 expansion of expressions during simplification. */
3340 op0 = vn_valueize (op0);
3341 if (TREE_CODE (op0) == SSA_NAME
3342 && (VN_INFO (op0)->has_constants
3343 || TREE_CODE_CLASS (code) == tcc_comparison
3344 || code == COMPLEX_EXPR))
3345 op0 = vn_get_expr_for (op0);
3347 op1 = vn_valueize (op1);
3348 if (TREE_CODE (op1) == SSA_NAME
3349 && (VN_INFO (op1)->has_constants
3350 || code == COMPLEX_EXPR))
3351 op1 = vn_get_expr_for (op1);
3353 /* Pointer plus constant can be represented as invariant address.
3354 Do so to allow further propatation, see also tree forwprop. */
3355 if (code == POINTER_PLUS_EXPR
3356 && tree_fits_uhwi_p (op1)
3357 && TREE_CODE (op0) == ADDR_EXPR
3358 && is_gimple_min_invariant (op0))
3359 return build_invariant_address (TREE_TYPE (op0),
3360 TREE_OPERAND (op0, 0),
3361 tree_to_uhwi (op1));
3363 /* Avoid folding if nothing changed. */
3364 if (op0 == gimple_assign_rhs1 (stmt)
3365 && op1 == gimple_assign_rhs2 (stmt))
3366 return NULL_TREE;
3368 fold_defer_overflow_warnings ();
3370 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3371 if (result)
3372 STRIP_USELESS_TYPE_CONVERSION (result);
3374 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3375 stmt, 0);
3377 /* Make sure result is not a complex expression consisting
3378 of operators of operators (IE (a + b) + (a + c))
3379 Otherwise, we will end up with unbounded expressions if
3380 fold does anything at all. */
3381 if (result && valid_gimple_rhs_p (result))
3382 return result;
3384 return NULL_TREE;
3387 /* Simplify the unary expression RHS, and return the result if
3388 simplified. */
3390 static tree
3391 simplify_unary_expression (gassign *stmt)
3393 tree result = NULL_TREE;
3394 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3395 enum tree_code code = gimple_assign_rhs_code (stmt);
3397 /* We handle some tcc_reference codes here that are all
3398 GIMPLE_ASSIGN_SINGLE codes. */
3399 if (code == REALPART_EXPR
3400 || code == IMAGPART_EXPR
3401 || code == VIEW_CONVERT_EXPR
3402 || code == BIT_FIELD_REF)
3403 op0 = TREE_OPERAND (op0, 0);
3405 orig_op0 = op0;
3406 op0 = vn_valueize (op0);
3407 if (TREE_CODE (op0) == SSA_NAME)
3409 if (VN_INFO (op0)->has_constants)
3410 op0 = vn_get_expr_for (op0);
3411 else if (CONVERT_EXPR_CODE_P (code)
3412 || code == REALPART_EXPR
3413 || code == IMAGPART_EXPR
3414 || code == VIEW_CONVERT_EXPR
3415 || code == BIT_FIELD_REF)
3417 /* We want to do tree-combining on conversion-like expressions.
3418 Make sure we feed only SSA_NAMEs or constants to fold though. */
3419 tree tem = vn_get_expr_for (op0);
3420 if (UNARY_CLASS_P (tem)
3421 || BINARY_CLASS_P (tem)
3422 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3423 || TREE_CODE (tem) == SSA_NAME
3424 || TREE_CODE (tem) == CONSTRUCTOR
3425 || is_gimple_min_invariant (tem))
3426 op0 = tem;
3430 /* Avoid folding if nothing changed, but remember the expression. */
3431 if (op0 == orig_op0)
3432 return NULL_TREE;
3434 if (code == BIT_FIELD_REF)
3436 tree rhs = gimple_assign_rhs1 (stmt);
3437 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3438 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3440 else
3441 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3442 if (result)
3444 STRIP_USELESS_TYPE_CONVERSION (result);
3445 if (valid_gimple_rhs_p (result))
3446 return result;
3449 return NULL_TREE;
3452 /* Try to simplify RHS using equivalences and constant folding. */
3454 static tree
3455 try_to_simplify (gassign *stmt)
3457 enum tree_code code = gimple_assign_rhs_code (stmt);
3458 tree tem;
3460 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3461 in this case, there is no point in doing extra work. */
3462 if (code == SSA_NAME)
3463 return NULL_TREE;
3465 /* First try constant folding based on our current lattice. */
3466 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize);
3467 if (tem
3468 && (TREE_CODE (tem) == SSA_NAME
3469 || is_gimple_min_invariant (tem)))
3470 return tem;
3472 /* If that didn't work try combining multiple statements. */
3473 switch (TREE_CODE_CLASS (code))
3475 case tcc_reference:
3476 /* Fallthrough for some unary codes that can operate on registers. */
3477 if (!(code == REALPART_EXPR
3478 || code == IMAGPART_EXPR
3479 || code == VIEW_CONVERT_EXPR
3480 || code == BIT_FIELD_REF))
3481 break;
3482 /* We could do a little more with unary ops, if they expand
3483 into binary ops, but it's debatable whether it is worth it. */
3484 case tcc_unary:
3485 return simplify_unary_expression (stmt);
3487 case tcc_comparison:
3488 case tcc_binary:
3489 return simplify_binary_expression (stmt);
3491 default:
3492 break;
3495 return NULL_TREE;
3498 /* Visit and value number USE, return true if the value number
3499 changed. */
3501 static bool
3502 visit_use (tree use)
3504 bool changed = false;
3505 gimple stmt = SSA_NAME_DEF_STMT (use);
3507 mark_use_processed (use);
3509 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3510 if (dump_file && (dump_flags & TDF_DETAILS)
3511 && !SSA_NAME_IS_DEFAULT_DEF (use))
3513 fprintf (dump_file, "Value numbering ");
3514 print_generic_expr (dump_file, use, 0);
3515 fprintf (dump_file, " stmt = ");
3516 print_gimple_stmt (dump_file, stmt, 0, 0);
3519 /* Handle uninitialized uses. */
3520 if (SSA_NAME_IS_DEFAULT_DEF (use))
3521 changed = set_ssa_val_to (use, use);
3522 else
3524 if (gimple_code (stmt) == GIMPLE_PHI)
3525 changed = visit_phi (stmt);
3526 else if (gimple_has_volatile_ops (stmt))
3527 changed = defs_to_varying (stmt);
3528 else if (is_gimple_assign (stmt))
3530 enum tree_code code = gimple_assign_rhs_code (stmt);
3531 tree lhs = gimple_assign_lhs (stmt);
3532 tree rhs1 = gimple_assign_rhs1 (stmt);
3533 tree simplified;
3535 /* Shortcut for copies. Simplifying copies is pointless,
3536 since we copy the expression and value they represent. */
3537 if (code == SSA_NAME
3538 && TREE_CODE (lhs) == SSA_NAME)
3540 changed = visit_copy (lhs, rhs1);
3541 goto done;
3543 simplified = try_to_simplify (as_a <gassign *> (stmt));
3544 if (simplified)
3546 if (dump_file && (dump_flags & TDF_DETAILS))
3548 fprintf (dump_file, "RHS ");
3549 print_gimple_expr (dump_file, stmt, 0, 0);
3550 fprintf (dump_file, " simplified to ");
3551 print_generic_expr (dump_file, simplified, 0);
3552 if (TREE_CODE (lhs) == SSA_NAME)
3553 fprintf (dump_file, " has constants %d\n",
3554 expr_has_constants (simplified));
3555 else
3556 fprintf (dump_file, "\n");
3559 /* Setting value numbers to constants will occasionally
3560 screw up phi congruence because constants are not
3561 uniquely associated with a single ssa name that can be
3562 looked up. */
3563 if (simplified
3564 && is_gimple_min_invariant (simplified)
3565 && TREE_CODE (lhs) == SSA_NAME)
3567 VN_INFO (lhs)->expr = simplified;
3568 VN_INFO (lhs)->has_constants = true;
3569 changed = set_ssa_val_to (lhs, simplified);
3570 goto done;
3572 else if (simplified
3573 && TREE_CODE (simplified) == SSA_NAME
3574 && TREE_CODE (lhs) == SSA_NAME)
3576 changed = visit_copy (lhs, simplified);
3577 goto done;
3579 else if (simplified)
3581 if (TREE_CODE (lhs) == SSA_NAME)
3583 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3584 /* We have to unshare the expression or else
3585 valuizing may change the IL stream. */
3586 VN_INFO (lhs)->expr = unshare_expr (simplified);
3589 else if (stmt_has_constants (stmt)
3590 && TREE_CODE (lhs) == SSA_NAME)
3591 VN_INFO (lhs)->has_constants = true;
3592 else if (TREE_CODE (lhs) == SSA_NAME)
3594 /* We reset expr and constantness here because we may
3595 have been value numbering optimistically, and
3596 iterating. They may become non-constant in this case,
3597 even if they were optimistically constant. */
3599 VN_INFO (lhs)->has_constants = false;
3600 VN_INFO (lhs)->expr = NULL_TREE;
3603 if ((TREE_CODE (lhs) == SSA_NAME
3604 /* We can substitute SSA_NAMEs that are live over
3605 abnormal edges with their constant value. */
3606 && !(gimple_assign_copy_p (stmt)
3607 && is_gimple_min_invariant (rhs1))
3608 && !(simplified
3609 && is_gimple_min_invariant (simplified))
3610 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3611 /* Stores or copies from SSA_NAMEs that are live over
3612 abnormal edges are a problem. */
3613 || (code == SSA_NAME
3614 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3615 changed = defs_to_varying (stmt);
3616 else if (REFERENCE_CLASS_P (lhs)
3617 || DECL_P (lhs))
3618 changed = visit_reference_op_store (lhs, rhs1, stmt);
3619 else if (TREE_CODE (lhs) == SSA_NAME)
3621 if ((gimple_assign_copy_p (stmt)
3622 && is_gimple_min_invariant (rhs1))
3623 || (simplified
3624 && is_gimple_min_invariant (simplified)))
3626 VN_INFO (lhs)->has_constants = true;
3627 if (simplified)
3628 changed = set_ssa_val_to (lhs, simplified);
3629 else
3630 changed = set_ssa_val_to (lhs, rhs1);
3632 else
3634 /* First try to lookup the simplified expression. */
3635 if (simplified)
3637 enum gimple_rhs_class rhs_class;
3640 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3641 if ((rhs_class == GIMPLE_UNARY_RHS
3642 || rhs_class == GIMPLE_BINARY_RHS
3643 || rhs_class == GIMPLE_TERNARY_RHS)
3644 && valid_gimple_rhs_p (simplified))
3646 tree result = vn_nary_op_lookup (simplified, NULL);
3647 if (result)
3649 changed = set_ssa_val_to (lhs, result);
3650 goto done;
3655 /* Otherwise visit the original statement. */
3656 switch (vn_get_stmt_kind (stmt))
3658 case VN_NARY:
3659 changed = visit_nary_op (lhs, stmt);
3660 break;
3661 case VN_REFERENCE:
3662 changed = visit_reference_op_load (lhs, rhs1, stmt);
3663 break;
3664 default:
3665 changed = defs_to_varying (stmt);
3666 break;
3670 else
3671 changed = defs_to_varying (stmt);
3673 else if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
3675 tree lhs = gimple_call_lhs (stmt);
3676 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3678 /* Try constant folding based on our current lattice. */
3679 tree simplified = gimple_fold_stmt_to_constant_1 (stmt,
3680 vn_valueize);
3681 if (simplified)
3683 if (dump_file && (dump_flags & TDF_DETAILS))
3685 fprintf (dump_file, "call ");
3686 print_gimple_expr (dump_file, stmt, 0, 0);
3687 fprintf (dump_file, " simplified to ");
3688 print_generic_expr (dump_file, simplified, 0);
3689 if (TREE_CODE (lhs) == SSA_NAME)
3690 fprintf (dump_file, " has constants %d\n",
3691 expr_has_constants (simplified));
3692 else
3693 fprintf (dump_file, "\n");
3696 /* Setting value numbers to constants will occasionally
3697 screw up phi congruence because constants are not
3698 uniquely associated with a single ssa name that can be
3699 looked up. */
3700 if (simplified
3701 && is_gimple_min_invariant (simplified))
3703 VN_INFO (lhs)->expr = simplified;
3704 VN_INFO (lhs)->has_constants = true;
3705 changed = set_ssa_val_to (lhs, simplified);
3706 if (gimple_vdef (stmt))
3707 changed |= set_ssa_val_to (gimple_vdef (stmt),
3708 gimple_vuse (stmt));
3709 goto done;
3711 else if (simplified
3712 && TREE_CODE (simplified) == SSA_NAME)
3714 changed = visit_copy (lhs, simplified);
3715 if (gimple_vdef (stmt))
3716 changed |= set_ssa_val_to (gimple_vdef (stmt),
3717 gimple_vuse (stmt));
3718 goto done;
3720 else
3722 if (stmt_has_constants (stmt))
3723 VN_INFO (lhs)->has_constants = true;
3724 else
3726 /* We reset expr and constantness here because we may
3727 have been value numbering optimistically, and
3728 iterating. They may become non-constant in this case,
3729 even if they were optimistically constant. */
3730 VN_INFO (lhs)->has_constants = false;
3731 VN_INFO (lhs)->expr = NULL_TREE;
3734 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3736 changed = defs_to_varying (stmt);
3737 goto done;
3742 if (!gimple_call_internal_p (stmt)
3743 && (/* Calls to the same function with the same vuse
3744 and the same operands do not necessarily return the same
3745 value, unless they're pure or const. */
3746 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3747 /* If calls have a vdef, subsequent calls won't have
3748 the same incoming vuse. So, if 2 calls with vdef have the
3749 same vuse, we know they're not subsequent.
3750 We can value number 2 calls to the same function with the
3751 same vuse and the same operands which are not subsequent
3752 the same, because there is no code in the program that can
3753 compare the 2 values... */
3754 || (gimple_vdef (stmt)
3755 /* ... unless the call returns a pointer which does
3756 not alias with anything else. In which case the
3757 information that the values are distinct are encoded
3758 in the IL. */
3759 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
3760 /* Only perform the following when being called from PRE
3761 which embeds tail merging. */
3762 && default_vn_walk_kind == VN_WALK)))
3763 changed = visit_reference_op_call (lhs, call_stmt);
3764 else
3765 changed = defs_to_varying (stmt);
3767 else
3768 changed = defs_to_varying (stmt);
3770 done:
3771 return changed;
3774 /* Compare two operands by reverse postorder index */
3776 static int
3777 compare_ops (const void *pa, const void *pb)
3779 const tree opa = *((const tree *)pa);
3780 const tree opb = *((const tree *)pb);
3781 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3782 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3783 basic_block bba;
3784 basic_block bbb;
3786 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3787 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3788 else if (gimple_nop_p (opstmta))
3789 return -1;
3790 else if (gimple_nop_p (opstmtb))
3791 return 1;
3793 bba = gimple_bb (opstmta);
3794 bbb = gimple_bb (opstmtb);
3796 if (!bba && !bbb)
3797 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3798 else if (!bba)
3799 return -1;
3800 else if (!bbb)
3801 return 1;
3803 if (bba == bbb)
3805 if (gimple_code (opstmta) == GIMPLE_PHI
3806 && gimple_code (opstmtb) == GIMPLE_PHI)
3807 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3808 else if (gimple_code (opstmta) == GIMPLE_PHI)
3809 return -1;
3810 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3811 return 1;
3812 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3813 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3814 else
3815 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3817 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3820 /* Sort an array containing members of a strongly connected component
3821 SCC so that the members are ordered by RPO number.
3822 This means that when the sort is complete, iterating through the
3823 array will give you the members in RPO order. */
3825 static void
3826 sort_scc (vec<tree> scc)
3828 scc.qsort (compare_ops);
3831 /* Insert the no longer used nary ONARY to the hash INFO. */
3833 static void
3834 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3836 size_t size = sizeof_vn_nary_op (onary->length);
3837 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3838 &info->nary_obstack);
3839 memcpy (nary, onary, size);
3840 vn_nary_op_insert_into (nary, info->nary, false);
3843 /* Insert the no longer used phi OPHI to the hash INFO. */
3845 static void
3846 copy_phi (vn_phi_t ophi, vn_tables_t info)
3848 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3849 vn_phi_s **slot;
3850 memcpy (phi, ophi, sizeof (*phi));
3851 ophi->phiargs.create (0);
3852 slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT);
3853 gcc_assert (!*slot);
3854 *slot = phi;
3857 /* Insert the no longer used reference OREF to the hash INFO. */
3859 static void
3860 copy_reference (vn_reference_t oref, vn_tables_t info)
3862 vn_reference_t ref;
3863 vn_reference_s **slot;
3864 ref = (vn_reference_t) pool_alloc (info->references_pool);
3865 memcpy (ref, oref, sizeof (*ref));
3866 oref->operands.create (0);
3867 slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT);
3868 if (*slot)
3869 free_reference (*slot);
3870 *slot = ref;
3873 /* Process a strongly connected component in the SSA graph. */
3875 static void
3876 process_scc (vec<tree> scc)
3878 tree var;
3879 unsigned int i;
3880 unsigned int iterations = 0;
3881 bool changed = true;
3882 vn_nary_op_iterator_type hin;
3883 vn_phi_iterator_type hip;
3884 vn_reference_iterator_type hir;
3885 vn_nary_op_t nary;
3886 vn_phi_t phi;
3887 vn_reference_t ref;
3889 /* If the SCC has a single member, just visit it. */
3890 if (scc.length () == 1)
3892 tree use = scc[0];
3893 if (VN_INFO (use)->use_processed)
3894 return;
3895 /* We need to make sure it doesn't form a cycle itself, which can
3896 happen for self-referential PHI nodes. In that case we would
3897 end up inserting an expression with VN_TOP operands into the
3898 valid table which makes us derive bogus equivalences later.
3899 The cheapest way to check this is to assume it for all PHI nodes. */
3900 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3901 /* Fallthru to iteration. */ ;
3902 else
3904 visit_use (use);
3905 return;
3909 if (dump_file && (dump_flags & TDF_DETAILS))
3910 print_scc (dump_file, scc);
3912 /* Iterate over the SCC with the optimistic table until it stops
3913 changing. */
3914 current_info = optimistic_info;
3915 while (changed)
3917 changed = false;
3918 iterations++;
3919 if (dump_file && (dump_flags & TDF_DETAILS))
3920 fprintf (dump_file, "Starting iteration %d\n", iterations);
3921 /* As we are value-numbering optimistically we have to
3922 clear the expression tables and the simplified expressions
3923 in each iteration until we converge. */
3924 optimistic_info->nary->empty ();
3925 optimistic_info->phis->empty ();
3926 optimistic_info->references->empty ();
3927 obstack_free (&optimistic_info->nary_obstack, NULL);
3928 gcc_obstack_init (&optimistic_info->nary_obstack);
3929 empty_alloc_pool (optimistic_info->phis_pool);
3930 empty_alloc_pool (optimistic_info->references_pool);
3931 FOR_EACH_VEC_ELT (scc, i, var)
3932 VN_INFO (var)->expr = NULL_TREE;
3933 FOR_EACH_VEC_ELT (scc, i, var)
3934 changed |= visit_use (var);
3937 if (dump_file && (dump_flags & TDF_DETAILS))
3938 fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations);
3939 statistics_histogram_event (cfun, "SCC iterations", iterations);
3941 /* Finally, copy the contents of the no longer used optimistic
3942 table to the valid table. */
3943 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin)
3944 copy_nary (nary, valid_info);
3945 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip)
3946 copy_phi (phi, valid_info);
3947 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references,
3948 ref, vn_reference_t, hir)
3949 copy_reference (ref, valid_info);
3951 current_info = valid_info;
3955 /* Pop the components of the found SCC for NAME off the SCC stack
3956 and process them. Returns true if all went well, false if
3957 we run into resource limits. */
3959 static bool
3960 extract_and_process_scc_for_name (tree name)
3962 auto_vec<tree> scc;
3963 tree x;
3965 /* Found an SCC, pop the components off the SCC stack and
3966 process them. */
3969 x = sccstack.pop ();
3971 VN_INFO (x)->on_sccstack = false;
3972 scc.safe_push (x);
3973 } while (x != name);
3975 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3976 if (scc.length ()
3977 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3979 if (dump_file)
3980 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3981 "SCC size %u exceeding %u\n", scc.length (),
3982 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3984 return false;
3987 if (scc.length () > 1)
3988 sort_scc (scc);
3990 process_scc (scc);
3992 return true;
3995 /* Depth first search on NAME to discover and process SCC's in the SSA
3996 graph.
3997 Execution of this algorithm relies on the fact that the SCC's are
3998 popped off the stack in topological order.
3999 Returns true if successful, false if we stopped processing SCC's due
4000 to resource constraints. */
4002 static bool
4003 DFS (tree name)
4005 vec<ssa_op_iter> itervec = vNULL;
4006 vec<tree> namevec = vNULL;
4007 use_operand_p usep = NULL;
4008 gimple defstmt;
4009 tree use;
4010 ssa_op_iter iter;
4012 start_over:
4013 /* SCC info */
4014 VN_INFO (name)->dfsnum = next_dfs_num++;
4015 VN_INFO (name)->visited = true;
4016 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
4018 sccstack.safe_push (name);
4019 VN_INFO (name)->on_sccstack = true;
4020 defstmt = SSA_NAME_DEF_STMT (name);
4022 /* Recursively DFS on our operands, looking for SCC's. */
4023 if (!gimple_nop_p (defstmt))
4025 /* Push a new iterator. */
4026 if (gphi *phi = dyn_cast <gphi *> (defstmt))
4027 usep = op_iter_init_phiuse (&iter, phi, SSA_OP_ALL_USES);
4028 else
4029 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
4031 else
4032 clear_and_done_ssa_iter (&iter);
4034 while (1)
4036 /* If we are done processing uses of a name, go up the stack
4037 of iterators and process SCCs as we found them. */
4038 if (op_iter_done (&iter))
4040 /* See if we found an SCC. */
4041 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
4042 if (!extract_and_process_scc_for_name (name))
4044 namevec.release ();
4045 itervec.release ();
4046 return false;
4049 /* Check if we are done. */
4050 if (namevec.is_empty ())
4052 namevec.release ();
4053 itervec.release ();
4054 return true;
4057 /* Restore the last use walker and continue walking there. */
4058 use = name;
4059 name = namevec.pop ();
4060 memcpy (&iter, &itervec.last (),
4061 sizeof (ssa_op_iter));
4062 itervec.pop ();
4063 goto continue_walking;
4066 use = USE_FROM_PTR (usep);
4068 /* Since we handle phi nodes, we will sometimes get
4069 invariants in the use expression. */
4070 if (TREE_CODE (use) == SSA_NAME)
4072 if (! (VN_INFO (use)->visited))
4074 /* Recurse by pushing the current use walking state on
4075 the stack and starting over. */
4076 itervec.safe_push (iter);
4077 namevec.safe_push (name);
4078 name = use;
4079 goto start_over;
4081 continue_walking:
4082 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
4083 VN_INFO (use)->low);
4085 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
4086 && VN_INFO (use)->on_sccstack)
4088 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
4089 VN_INFO (name)->low);
4093 usep = op_iter_next_use (&iter);
4097 /* Allocate a value number table. */
4099 static void
4100 allocate_vn_table (vn_tables_t table)
4102 table->phis = new vn_phi_table_type (23);
4103 table->nary = new vn_nary_op_table_type (23);
4104 table->references = new vn_reference_table_type (23);
4106 gcc_obstack_init (&table->nary_obstack);
4107 table->phis_pool = create_alloc_pool ("VN phis",
4108 sizeof (struct vn_phi_s),
4109 30);
4110 table->references_pool = create_alloc_pool ("VN references",
4111 sizeof (struct vn_reference_s),
4112 30);
4115 /* Free a value number table. */
4117 static void
4118 free_vn_table (vn_tables_t table)
4120 delete table->phis;
4121 table->phis = NULL;
4122 delete table->nary;
4123 table->nary = NULL;
4124 delete table->references;
4125 table->references = NULL;
4126 obstack_free (&table->nary_obstack, NULL);
4127 free_alloc_pool (table->phis_pool);
4128 free_alloc_pool (table->references_pool);
4131 static void
4132 init_scc_vn (void)
4134 size_t i;
4135 int j;
4136 int *rpo_numbers_temp;
4138 calculate_dominance_info (CDI_DOMINATORS);
4139 sccstack.create (0);
4140 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
4142 constant_value_ids = BITMAP_ALLOC (NULL);
4144 next_dfs_num = 1;
4145 next_value_id = 1;
4147 vn_ssa_aux_table.create (num_ssa_names + 1);
4148 /* VEC_alloc doesn't actually grow it to the right size, it just
4149 preallocates the space to do so. */
4150 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
4151 gcc_obstack_init (&vn_ssa_aux_obstack);
4153 shared_lookup_phiargs.create (0);
4154 shared_lookup_references.create (0);
4155 rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun));
4156 rpo_numbers_temp =
4157 XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4158 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
4160 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
4161 the i'th block in RPO order is bb. We want to map bb's to RPO
4162 numbers, so we need to rearrange this array. */
4163 for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++)
4164 rpo_numbers[rpo_numbers_temp[j]] = j;
4166 XDELETE (rpo_numbers_temp);
4168 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
4170 /* Create the VN_INFO structures, and initialize value numbers to
4171 TOP. */
4172 for (i = 0; i < num_ssa_names; i++)
4174 tree name = ssa_name (i);
4175 if (name)
4177 VN_INFO_GET (name)->valnum = VN_TOP;
4178 VN_INFO (name)->expr = NULL_TREE;
4179 VN_INFO (name)->value_id = 0;
4183 renumber_gimple_stmt_uids ();
4185 /* Create the valid and optimistic value numbering tables. */
4186 valid_info = XCNEW (struct vn_tables_s);
4187 allocate_vn_table (valid_info);
4188 optimistic_info = XCNEW (struct vn_tables_s);
4189 allocate_vn_table (optimistic_info);
4192 void
4193 free_scc_vn (void)
4195 size_t i;
4197 delete constant_to_value_id;
4198 constant_to_value_id = NULL;
4199 BITMAP_FREE (constant_value_ids);
4200 shared_lookup_phiargs.release ();
4201 shared_lookup_references.release ();
4202 XDELETEVEC (rpo_numbers);
4204 for (i = 0; i < num_ssa_names; i++)
4206 tree name = ssa_name (i);
4207 if (name
4208 && VN_INFO (name)->needs_insertion)
4209 release_ssa_name (name);
4211 obstack_free (&vn_ssa_aux_obstack, NULL);
4212 vn_ssa_aux_table.release ();
4214 sccstack.release ();
4215 free_vn_table (valid_info);
4216 XDELETE (valid_info);
4217 free_vn_table (optimistic_info);
4218 XDELETE (optimistic_info);
4221 /* Set *ID according to RESULT. */
4223 static void
4224 set_value_id_for_result (tree result, unsigned int *id)
4226 if (result && TREE_CODE (result) == SSA_NAME)
4227 *id = VN_INFO (result)->value_id;
4228 else if (result && is_gimple_min_invariant (result))
4229 *id = get_or_alloc_constant_value_id (result);
4230 else
4231 *id = get_next_value_id ();
4234 /* Set the value ids in the valid hash tables. */
4236 static void
4237 set_hashtable_value_ids (void)
4239 vn_nary_op_iterator_type hin;
4240 vn_phi_iterator_type hip;
4241 vn_reference_iterator_type hir;
4242 vn_nary_op_t vno;
4243 vn_reference_t vr;
4244 vn_phi_t vp;
4246 /* Now set the value ids of the things we had put in the hash
4247 table. */
4249 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4250 set_value_id_for_result (vno->result, &vno->value_id);
4252 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4253 set_value_id_for_result (vp->result, &vp->value_id);
4255 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4256 hir)
4257 set_value_id_for_result (vr->result, &vr->value_id);
4260 class cond_dom_walker : public dom_walker
4262 public:
4263 cond_dom_walker () : dom_walker (CDI_DOMINATORS), fail (false) {}
4265 virtual void before_dom_children (basic_block);
4267 bool fail;
4270 void
4271 cond_dom_walker::before_dom_children (basic_block bb)
4273 edge e;
4274 edge_iterator ei;
4276 if (fail)
4277 return;
4279 /* If any of the predecessor edges that do not come from blocks dominated
4280 by us are still marked as possibly executable consider this block
4281 reachable. */
4282 bool reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (cfun);
4283 FOR_EACH_EDGE (e, ei, bb->preds)
4284 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
4285 reachable |= (e->flags & EDGE_EXECUTABLE);
4287 /* If the block is not reachable all outgoing edges are not
4288 executable. */
4289 if (!reachable)
4291 if (dump_file && (dump_flags & TDF_DETAILS))
4292 fprintf (dump_file, "Marking all outgoing edges of unreachable "
4293 "BB %d as not executable\n", bb->index);
4295 FOR_EACH_EDGE (e, ei, bb->succs)
4296 e->flags &= ~EDGE_EXECUTABLE;
4297 return;
4300 gimple stmt = last_stmt (bb);
4301 if (!stmt)
4302 return;
4304 enum gimple_code code = gimple_code (stmt);
4305 if (code != GIMPLE_COND
4306 && code != GIMPLE_SWITCH
4307 && code != GIMPLE_GOTO)
4308 return;
4310 if (dump_file && (dump_flags & TDF_DETAILS))
4312 fprintf (dump_file, "Value-numbering operands of stmt ending BB %d: ",
4313 bb->index);
4314 print_gimple_stmt (dump_file, stmt, 0, 0);
4317 /* Value-number the last stmts SSA uses. */
4318 ssa_op_iter i;
4319 tree op;
4320 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4321 if (VN_INFO (op)->visited == false
4322 && !DFS (op))
4324 fail = true;
4325 return;
4328 /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges
4329 if value-numbering can prove they are not reachable. Handling
4330 computed gotos is also possible. */
4331 tree val;
4332 switch (code)
4334 case GIMPLE_COND:
4336 tree lhs = gimple_cond_lhs (stmt);
4337 tree rhs = gimple_cond_rhs (stmt);
4338 /* Work hard in computing the condition and take into account
4339 the valueization of the defining stmt. */
4340 if (TREE_CODE (lhs) == SSA_NAME)
4341 lhs = vn_get_expr_for (lhs);
4342 if (TREE_CODE (rhs) == SSA_NAME)
4343 rhs = vn_get_expr_for (rhs);
4344 val = fold_binary (gimple_cond_code (stmt),
4345 boolean_type_node, lhs, rhs);
4346 break;
4348 case GIMPLE_SWITCH:
4349 val = gimple_switch_index (as_a <gswitch *> (stmt));
4350 break;
4351 case GIMPLE_GOTO:
4352 val = gimple_goto_dest (stmt);
4353 break;
4354 default:
4355 gcc_unreachable ();
4357 if (!val)
4358 return;
4360 edge taken = find_taken_edge (bb, vn_valueize (val));
4361 if (!taken)
4362 return;
4364 if (dump_file && (dump_flags & TDF_DETAILS))
4365 fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as "
4366 "not executable\n", bb->index, bb->index, taken->dest->index);
4368 FOR_EACH_EDGE (e, ei, bb->succs)
4369 if (e != taken)
4370 e->flags &= ~EDGE_EXECUTABLE;
4373 /* Do SCCVN. Returns true if it finished, false if we bailed out
4374 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4375 how we use the alias oracle walking during the VN process. */
4377 bool
4378 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4380 basic_block bb;
4381 size_t i;
4382 tree param;
4384 default_vn_walk_kind = default_vn_walk_kind_;
4386 init_scc_vn ();
4387 current_info = valid_info;
4389 for (param = DECL_ARGUMENTS (current_function_decl);
4390 param;
4391 param = DECL_CHAIN (param))
4393 tree def = ssa_default_def (cfun, param);
4394 if (def)
4396 VN_INFO (def)->visited = true;
4397 VN_INFO (def)->valnum = def;
4401 /* Mark all edges as possibly executable. */
4402 FOR_ALL_BB_FN (bb, cfun)
4404 edge_iterator ei;
4405 edge e;
4406 FOR_EACH_EDGE (e, ei, bb->succs)
4407 e->flags |= EDGE_EXECUTABLE;
4410 /* Walk all blocks in dominator order, value-numbering the last stmts
4411 SSA uses and decide whether outgoing edges are not executable. */
4412 cond_dom_walker walker;
4413 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4414 if (walker.fail)
4416 free_scc_vn ();
4417 return false;
4420 /* Value-number remaining SSA names. */
4421 for (i = 1; i < num_ssa_names; ++i)
4423 tree name = ssa_name (i);
4424 if (name
4425 && VN_INFO (name)->visited == false
4426 && !has_zero_uses (name))
4427 if (!DFS (name))
4429 free_scc_vn ();
4430 return false;
4434 /* Initialize the value ids. */
4436 for (i = 1; i < num_ssa_names; ++i)
4438 tree name = ssa_name (i);
4439 vn_ssa_aux_t info;
4440 if (!name)
4441 continue;
4442 info = VN_INFO (name);
4443 if (info->valnum == name
4444 || info->valnum == VN_TOP)
4445 info->value_id = get_next_value_id ();
4446 else if (is_gimple_min_invariant (info->valnum))
4447 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4450 /* Propagate. */
4451 for (i = 1; i < num_ssa_names; ++i)
4453 tree name = ssa_name (i);
4454 vn_ssa_aux_t info;
4455 if (!name)
4456 continue;
4457 info = VN_INFO (name);
4458 if (TREE_CODE (info->valnum) == SSA_NAME
4459 && info->valnum != name
4460 && info->value_id != VN_INFO (info->valnum)->value_id)
4461 info->value_id = VN_INFO (info->valnum)->value_id;
4464 set_hashtable_value_ids ();
4466 if (dump_file && (dump_flags & TDF_DETAILS))
4468 fprintf (dump_file, "Value numbers:\n");
4469 for (i = 0; i < num_ssa_names; i++)
4471 tree name = ssa_name (i);
4472 if (name
4473 && VN_INFO (name)->visited
4474 && SSA_VAL (name) != name)
4476 print_generic_expr (dump_file, name, 0);
4477 fprintf (dump_file, " = ");
4478 print_generic_expr (dump_file, SSA_VAL (name), 0);
4479 fprintf (dump_file, "\n");
4484 return true;
4487 /* Return the maximum value id we have ever seen. */
4489 unsigned int
4490 get_max_value_id (void)
4492 return next_value_id;
4495 /* Return the next unique value id. */
4497 unsigned int
4498 get_next_value_id (void)
4500 return next_value_id++;
4504 /* Compare two expressions E1 and E2 and return true if they are equal. */
4506 bool
4507 expressions_equal_p (tree e1, tree e2)
4509 /* The obvious case. */
4510 if (e1 == e2)
4511 return true;
4513 /* If only one of them is null, they cannot be equal. */
4514 if (!e1 || !e2)
4515 return false;
4517 /* Now perform the actual comparison. */
4518 if (TREE_CODE (e1) == TREE_CODE (e2)
4519 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4520 return true;
4522 return false;
4526 /* Return true if the nary operation NARY may trap. This is a copy
4527 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4529 bool
4530 vn_nary_may_trap (vn_nary_op_t nary)
4532 tree type;
4533 tree rhs2 = NULL_TREE;
4534 bool honor_nans = false;
4535 bool honor_snans = false;
4536 bool fp_operation = false;
4537 bool honor_trapv = false;
4538 bool handled, ret;
4539 unsigned i;
4541 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4542 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4543 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4545 type = nary->type;
4546 fp_operation = FLOAT_TYPE_P (type);
4547 if (fp_operation)
4549 honor_nans = flag_trapping_math && !flag_finite_math_only;
4550 honor_snans = flag_signaling_nans != 0;
4552 else if (INTEGRAL_TYPE_P (type)
4553 && TYPE_OVERFLOW_TRAPS (type))
4554 honor_trapv = true;
4556 if (nary->length >= 2)
4557 rhs2 = nary->op[1];
4558 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4559 honor_trapv,
4560 honor_nans, honor_snans, rhs2,
4561 &handled);
4562 if (handled
4563 && ret)
4564 return true;
4566 for (i = 0; i < nary->length; ++i)
4567 if (tree_could_trap_p (nary->op[i]))
4568 return true;
4570 return false;