2014-08-27 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blobec0bf6b402d02764f9dec4db58c375adca7d4fac
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 "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
30 #include "hash-table.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "inchash.h"
34 #include "gimple-fold.h"
35 #include "tree-eh.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "gimplify.h"
40 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "expr.h"
46 #include "tree-dfa.h"
47 #include "tree-ssa.h"
48 #include "dumpfile.h"
49 #include "alloc-pool.h"
50 #include "flags.h"
51 #include "cfgloop.h"
52 #include "params.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-ssa-sccvn.h"
55 #include "tree-cfg.h"
56 #include "domwalk.h"
58 /* This algorithm is based on the SCC algorithm presented by Keith
59 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
60 (http://citeseer.ist.psu.edu/41805.html). In
61 straight line code, it is equivalent to a regular hash based value
62 numbering that is performed in reverse postorder.
64 For code with cycles, there are two alternatives, both of which
65 require keeping the hashtables separate from the actual list of
66 value numbers for SSA names.
68 1. Iterate value numbering in an RPO walk of the blocks, removing
69 all the entries from the hashtable after each iteration (but
70 keeping the SSA name->value number mapping between iterations).
71 Iterate until it does not change.
73 2. Perform value numbering as part of an SCC walk on the SSA graph,
74 iterating only the cycles in the SSA graph until they do not change
75 (using a separate, optimistic hashtable for value numbering the SCC
76 operands).
78 The second is not just faster in practice (because most SSA graph
79 cycles do not involve all the variables in the graph), it also has
80 some nice properties.
82 One of these nice properties is that when we pop an SCC off the
83 stack, we are guaranteed to have processed all the operands coming from
84 *outside of that SCC*, so we do not need to do anything special to
85 ensure they have value numbers.
87 Another nice property is that the SCC walk is done as part of a DFS
88 of the SSA graph, which makes it easy to perform combining and
89 simplifying operations at the same time.
91 The code below is deliberately written in a way that makes it easy
92 to separate the SCC walk from the other work it does.
94 In order to propagate constants through the code, we track which
95 expressions contain constants, and use those while folding. In
96 theory, we could also track expressions whose value numbers are
97 replaced, in case we end up folding based on expression
98 identities.
100 In order to value number memory, we assign value numbers to vuses.
101 This enables us to note that, for example, stores to the same
102 address of the same value from the same starting memory states are
103 equivalent.
104 TODO:
106 1. We can iterate only the changing portions of the SCC's, but
107 I have not seen an SCC big enough for this to be a win.
108 2. If you differentiate between phi nodes for loops and phi nodes
109 for if-then-else, you can properly consider phi nodes in different
110 blocks for equivalence.
111 3. We could value number vuses in more cases, particularly, whole
112 structure copies.
116 /* vn_nary_op hashtable helpers. */
118 struct vn_nary_op_hasher : typed_noop_remove <vn_nary_op_s>
120 typedef vn_nary_op_s value_type;
121 typedef vn_nary_op_s compare_type;
122 static inline hashval_t hash (const value_type *);
123 static inline bool equal (const value_type *, const compare_type *);
126 /* Return the computed hashcode for nary operation P1. */
128 inline hashval_t
129 vn_nary_op_hasher::hash (const value_type *vno1)
131 return vno1->hashcode;
134 /* Compare nary operations P1 and P2 and return true if they are
135 equivalent. */
137 inline bool
138 vn_nary_op_hasher::equal (const value_type *vno1, const compare_type *vno2)
140 return vn_nary_op_eq (vno1, vno2);
143 typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type;
144 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
147 /* vn_phi hashtable helpers. */
149 static int
150 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
152 struct vn_phi_hasher
154 typedef vn_phi_s value_type;
155 typedef vn_phi_s compare_type;
156 static inline hashval_t hash (const value_type *);
157 static inline bool equal (const value_type *, const compare_type *);
158 static inline void remove (value_type *);
161 /* Return the computed hashcode for phi operation P1. */
163 inline hashval_t
164 vn_phi_hasher::hash (const value_type *vp1)
166 return vp1->hashcode;
169 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
171 inline bool
172 vn_phi_hasher::equal (const value_type *vp1, const compare_type *vp2)
174 return vn_phi_eq (vp1, vp2);
177 /* Free a phi operation structure VP. */
179 inline void
180 vn_phi_hasher::remove (value_type *phi)
182 phi->phiargs.release ();
185 typedef hash_table<vn_phi_hasher> vn_phi_table_type;
186 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
189 /* Compare two reference operands P1 and P2 for equality. Return true if
190 they are equal, and false otherwise. */
192 static int
193 vn_reference_op_eq (const void *p1, const void *p2)
195 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
196 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
198 return (vro1->opcode == vro2->opcode
199 /* We do not care for differences in type qualification. */
200 && (vro1->type == vro2->type
201 || (vro1->type && vro2->type
202 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
203 TYPE_MAIN_VARIANT (vro2->type))))
204 && expressions_equal_p (vro1->op0, vro2->op0)
205 && expressions_equal_p (vro1->op1, vro2->op1)
206 && expressions_equal_p (vro1->op2, vro2->op2));
209 /* Free a reference operation structure VP. */
211 static inline void
212 free_reference (vn_reference_s *vr)
214 vr->operands.release ();
218 /* vn_reference hashtable helpers. */
220 struct vn_reference_hasher
222 typedef vn_reference_s value_type;
223 typedef vn_reference_s compare_type;
224 static inline hashval_t hash (const value_type *);
225 static inline bool equal (const value_type *, const compare_type *);
226 static inline void remove (value_type *);
229 /* Return the hashcode for a given reference operation P1. */
231 inline hashval_t
232 vn_reference_hasher::hash (const value_type *vr1)
234 return vr1->hashcode;
237 inline bool
238 vn_reference_hasher::equal (const value_type *v, const compare_type *c)
240 return vn_reference_eq (v, c);
243 inline void
244 vn_reference_hasher::remove (value_type *v)
246 free_reference (v);
249 typedef hash_table<vn_reference_hasher> vn_reference_table_type;
250 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
253 /* The set of hashtables and alloc_pool's for their items. */
255 typedef struct vn_tables_s
257 vn_nary_op_table_type *nary;
258 vn_phi_table_type *phis;
259 vn_reference_table_type *references;
260 struct obstack nary_obstack;
261 alloc_pool phis_pool;
262 alloc_pool references_pool;
263 } *vn_tables_t;
266 /* vn_constant hashtable helpers. */
268 struct vn_constant_hasher : typed_free_remove <vn_constant_s>
270 typedef vn_constant_s value_type;
271 typedef vn_constant_s compare_type;
272 static inline hashval_t hash (const value_type *);
273 static inline bool equal (const value_type *, const compare_type *);
276 /* Hash table hash function for vn_constant_t. */
278 inline hashval_t
279 vn_constant_hasher::hash (const value_type *vc1)
281 return vc1->hashcode;
284 /* Hash table equality function for vn_constant_t. */
286 inline bool
287 vn_constant_hasher::equal (const value_type *vc1, const compare_type *vc2)
289 if (vc1->hashcode != vc2->hashcode)
290 return false;
292 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
295 static hash_table<vn_constant_hasher> *constant_to_value_id;
296 static bitmap constant_value_ids;
299 /* Valid hashtables storing information we have proven to be
300 correct. */
302 static vn_tables_t valid_info;
304 /* Optimistic hashtables storing information we are making assumptions about
305 during iterations. */
307 static vn_tables_t optimistic_info;
309 /* Pointer to the set of hashtables that is currently being used.
310 Should always point to either the optimistic_info, or the
311 valid_info. */
313 static vn_tables_t current_info;
316 /* Reverse post order index for each basic block. */
318 static int *rpo_numbers;
320 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
322 /* Return the SSA value of the VUSE x, supporting released VDEFs
323 during elimination which will value-number the VDEF to the
324 associated VUSE (but not substitute in the whole lattice). */
326 static inline tree
327 vuse_ssa_val (tree x)
329 if (!x)
330 return NULL_TREE;
334 x = SSA_VAL (x);
336 while (SSA_NAME_IN_FREE_LIST (x));
338 return x;
341 /* This represents the top of the VN lattice, which is the universal
342 value. */
344 tree VN_TOP;
346 /* Unique counter for our value ids. */
348 static unsigned int next_value_id;
350 /* Next DFS number and the stack for strongly connected component
351 detection. */
353 static unsigned int next_dfs_num;
354 static vec<tree> sccstack;
358 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
359 are allocated on an obstack for locality reasons, and to free them
360 without looping over the vec. */
362 static vec<vn_ssa_aux_t> vn_ssa_aux_table;
363 static struct obstack vn_ssa_aux_obstack;
365 /* Return the value numbering information for a given SSA name. */
367 vn_ssa_aux_t
368 VN_INFO (tree name)
370 vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)];
371 gcc_checking_assert (res);
372 return res;
375 /* Set the value numbering info for a given SSA name to a given
376 value. */
378 static inline void
379 VN_INFO_SET (tree name, vn_ssa_aux_t value)
381 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value;
384 /* Initialize the value numbering info for a given SSA name.
385 This should be called just once for every SSA name. */
387 vn_ssa_aux_t
388 VN_INFO_GET (tree name)
390 vn_ssa_aux_t newinfo;
392 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
393 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
394 if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ())
395 vn_ssa_aux_table.safe_grow (SSA_NAME_VERSION (name) + 1);
396 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo;
397 return newinfo;
401 /* Get the representative expression for the SSA_NAME NAME. Returns
402 the representative SSA_NAME if there is no expression associated with it. */
404 tree
405 vn_get_expr_for (tree name)
407 vn_ssa_aux_t vn = VN_INFO (name);
408 gimple def_stmt;
409 tree expr = NULL_TREE;
410 enum tree_code code;
412 if (vn->valnum == VN_TOP)
413 return name;
415 /* If the value-number is a constant it is the representative
416 expression. */
417 if (TREE_CODE (vn->valnum) != SSA_NAME)
418 return vn->valnum;
420 /* Get to the information of the value of this SSA_NAME. */
421 vn = VN_INFO (vn->valnum);
423 /* If the value-number is a constant it is the representative
424 expression. */
425 if (TREE_CODE (vn->valnum) != SSA_NAME)
426 return vn->valnum;
428 /* Else if we have an expression, return it. */
429 if (vn->expr != NULL_TREE)
430 return vn->expr;
432 /* Otherwise use the defining statement to build the expression. */
433 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
435 /* If the value number is not an assignment use it directly. */
436 if (!is_gimple_assign (def_stmt))
437 return vn->valnum;
439 /* Note that we can valueize here because we clear the cached
440 simplified expressions after each optimistic iteration. */
441 code = gimple_assign_rhs_code (def_stmt);
442 switch (TREE_CODE_CLASS (code))
444 case tcc_reference:
445 if ((code == REALPART_EXPR
446 || code == IMAGPART_EXPR
447 || code == VIEW_CONVERT_EXPR)
448 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
449 0)) == SSA_NAME)
450 expr = fold_build1 (code,
451 gimple_expr_type (def_stmt),
452 vn_valueize (TREE_OPERAND
453 (gimple_assign_rhs1 (def_stmt), 0)));
454 break;
456 case tcc_unary:
457 expr = fold_build1 (code,
458 gimple_expr_type (def_stmt),
459 vn_valueize (gimple_assign_rhs1 (def_stmt)));
460 break;
462 case tcc_binary:
463 expr = fold_build2 (code,
464 gimple_expr_type (def_stmt),
465 vn_valueize (gimple_assign_rhs1 (def_stmt)),
466 vn_valueize (gimple_assign_rhs2 (def_stmt)));
467 break;
469 case tcc_exceptional:
470 if (code == CONSTRUCTOR
471 && TREE_CODE
472 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
473 expr = gimple_assign_rhs1 (def_stmt);
474 break;
476 default:;
478 if (expr == NULL_TREE)
479 return vn->valnum;
481 /* Cache the expression. */
482 vn->expr = expr;
484 return expr;
487 /* Return the vn_kind the expression computed by the stmt should be
488 associated with. */
490 enum vn_kind
491 vn_get_stmt_kind (gimple stmt)
493 switch (gimple_code (stmt))
495 case GIMPLE_CALL:
496 return VN_REFERENCE;
497 case GIMPLE_PHI:
498 return VN_PHI;
499 case GIMPLE_ASSIGN:
501 enum tree_code code = gimple_assign_rhs_code (stmt);
502 tree rhs1 = gimple_assign_rhs1 (stmt);
503 switch (get_gimple_rhs_class (code))
505 case GIMPLE_UNARY_RHS:
506 case GIMPLE_BINARY_RHS:
507 case GIMPLE_TERNARY_RHS:
508 return VN_NARY;
509 case GIMPLE_SINGLE_RHS:
510 switch (TREE_CODE_CLASS (code))
512 case tcc_reference:
513 /* VOP-less references can go through unary case. */
514 if ((code == REALPART_EXPR
515 || code == IMAGPART_EXPR
516 || code == VIEW_CONVERT_EXPR
517 || code == BIT_FIELD_REF)
518 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
519 return VN_NARY;
521 /* Fallthrough. */
522 case tcc_declaration:
523 return VN_REFERENCE;
525 case tcc_constant:
526 return VN_CONSTANT;
528 default:
529 if (code == ADDR_EXPR)
530 return (is_gimple_min_invariant (rhs1)
531 ? VN_CONSTANT : VN_REFERENCE);
532 else if (code == CONSTRUCTOR)
533 return VN_NARY;
534 return VN_NONE;
536 default:
537 return VN_NONE;
540 default:
541 return VN_NONE;
545 /* Lookup a value id for CONSTANT and return it. If it does not
546 exist returns 0. */
548 unsigned int
549 get_constant_value_id (tree constant)
551 vn_constant_s **slot;
552 struct vn_constant_s vc;
554 vc.hashcode = vn_hash_constant_with_type (constant);
555 vc.constant = constant;
556 slot = constant_to_value_id->find_slot (&vc, NO_INSERT);
557 if (slot)
558 return (*slot)->value_id;
559 return 0;
562 /* Lookup a value id for CONSTANT, and if it does not exist, create a
563 new one and return it. If it does exist, return it. */
565 unsigned int
566 get_or_alloc_constant_value_id (tree constant)
568 vn_constant_s **slot;
569 struct vn_constant_s vc;
570 vn_constant_t vcp;
572 vc.hashcode = vn_hash_constant_with_type (constant);
573 vc.constant = constant;
574 slot = constant_to_value_id->find_slot (&vc, INSERT);
575 if (*slot)
576 return (*slot)->value_id;
578 vcp = XNEW (struct vn_constant_s);
579 vcp->hashcode = vc.hashcode;
580 vcp->constant = constant;
581 vcp->value_id = get_next_value_id ();
582 *slot = vcp;
583 bitmap_set_bit (constant_value_ids, vcp->value_id);
584 return vcp->value_id;
587 /* Return true if V is a value id for a constant. */
589 bool
590 value_id_constant_p (unsigned int v)
592 return bitmap_bit_p (constant_value_ids, v);
595 /* Compute the hash for a reference operand VRO1. */
597 static void
598 vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate)
600 hstate.add_int (vro1->opcode);
601 if (vro1->op0)
602 inchash::add_expr (vro1->op0, hstate);
603 if (vro1->op1)
604 inchash::add_expr (vro1->op1, hstate);
605 if (vro1->op2)
606 inchash::add_expr (vro1->op2, hstate);
609 /* Compute a hash for the reference operation VR1 and return it. */
611 hashval_t
612 vn_reference_compute_hash (const vn_reference_t vr1)
614 inchash::hash hstate;
615 hashval_t result;
616 int i;
617 vn_reference_op_t vro;
618 HOST_WIDE_INT off = -1;
619 bool deref = false;
621 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
623 if (vro->opcode == MEM_REF)
624 deref = true;
625 else if (vro->opcode != ADDR_EXPR)
626 deref = false;
627 if (vro->off != -1)
629 if (off == -1)
630 off = 0;
631 off += vro->off;
633 else
635 if (off != -1
636 && off != 0)
637 hstate.add_int (off);
638 off = -1;
639 if (deref
640 && vro->opcode == ADDR_EXPR)
642 if (vro->op0)
644 tree op = TREE_OPERAND (vro->op0, 0);
645 hstate.add_int (TREE_CODE (op));
646 inchash::add_expr (op, hstate);
649 else
650 vn_reference_op_compute_hash (vro, hstate);
653 result = hstate.end ();
654 /* ??? We would ICE later if we hash instead of adding that in. */
655 if (vr1->vuse)
656 result += SSA_NAME_VERSION (vr1->vuse);
658 return result;
661 /* Return true if reference operations VR1 and VR2 are equivalent. This
662 means they have the same set of operands and vuses. */
664 bool
665 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
667 unsigned i, j;
669 /* Early out if this is not a hash collision. */
670 if (vr1->hashcode != vr2->hashcode)
671 return false;
673 /* The VOP needs to be the same. */
674 if (vr1->vuse != vr2->vuse)
675 return false;
677 /* If the operands are the same we are done. */
678 if (vr1->operands == vr2->operands)
679 return true;
681 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
682 return false;
684 if (INTEGRAL_TYPE_P (vr1->type)
685 && INTEGRAL_TYPE_P (vr2->type))
687 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
688 return false;
690 else if (INTEGRAL_TYPE_P (vr1->type)
691 && (TYPE_PRECISION (vr1->type)
692 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
693 return false;
694 else if (INTEGRAL_TYPE_P (vr2->type)
695 && (TYPE_PRECISION (vr2->type)
696 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
697 return false;
699 i = 0;
700 j = 0;
703 HOST_WIDE_INT off1 = 0, off2 = 0;
704 vn_reference_op_t vro1, vro2;
705 vn_reference_op_s tem1, tem2;
706 bool deref1 = false, deref2 = false;
707 for (; vr1->operands.iterate (i, &vro1); i++)
709 if (vro1->opcode == MEM_REF)
710 deref1 = true;
711 if (vro1->off == -1)
712 break;
713 off1 += vro1->off;
715 for (; vr2->operands.iterate (j, &vro2); j++)
717 if (vro2->opcode == MEM_REF)
718 deref2 = true;
719 if (vro2->off == -1)
720 break;
721 off2 += vro2->off;
723 if (off1 != off2)
724 return false;
725 if (deref1 && vro1->opcode == ADDR_EXPR)
727 memset (&tem1, 0, sizeof (tem1));
728 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
729 tem1.type = TREE_TYPE (tem1.op0);
730 tem1.opcode = TREE_CODE (tem1.op0);
731 vro1 = &tem1;
732 deref1 = false;
734 if (deref2 && vro2->opcode == ADDR_EXPR)
736 memset (&tem2, 0, sizeof (tem2));
737 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
738 tem2.type = TREE_TYPE (tem2.op0);
739 tem2.opcode = TREE_CODE (tem2.op0);
740 vro2 = &tem2;
741 deref2 = false;
743 if (deref1 != deref2)
744 return false;
745 if (!vn_reference_op_eq (vro1, vro2))
746 return false;
747 ++j;
748 ++i;
750 while (vr1->operands.length () != i
751 || vr2->operands.length () != j);
753 return true;
756 /* Copy the operations present in load/store REF into RESULT, a vector of
757 vn_reference_op_s's. */
759 void
760 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
762 if (TREE_CODE (ref) == TARGET_MEM_REF)
764 vn_reference_op_s temp;
766 result->reserve (3);
768 memset (&temp, 0, sizeof (temp));
769 temp.type = TREE_TYPE (ref);
770 temp.opcode = TREE_CODE (ref);
771 temp.op0 = TMR_INDEX (ref);
772 temp.op1 = TMR_STEP (ref);
773 temp.op2 = TMR_OFFSET (ref);
774 temp.off = -1;
775 result->quick_push (temp);
777 memset (&temp, 0, sizeof (temp));
778 temp.type = NULL_TREE;
779 temp.opcode = ERROR_MARK;
780 temp.op0 = TMR_INDEX2 (ref);
781 temp.off = -1;
782 result->quick_push (temp);
784 memset (&temp, 0, sizeof (temp));
785 temp.type = NULL_TREE;
786 temp.opcode = TREE_CODE (TMR_BASE (ref));
787 temp.op0 = TMR_BASE (ref);
788 temp.off = -1;
789 result->quick_push (temp);
790 return;
793 /* For non-calls, store the information that makes up the address. */
794 tree orig = ref;
795 while (ref)
797 vn_reference_op_s temp;
799 memset (&temp, 0, sizeof (temp));
800 temp.type = TREE_TYPE (ref);
801 temp.opcode = TREE_CODE (ref);
802 temp.off = -1;
804 switch (temp.opcode)
806 case MODIFY_EXPR:
807 temp.op0 = TREE_OPERAND (ref, 1);
808 break;
809 case WITH_SIZE_EXPR:
810 temp.op0 = TREE_OPERAND (ref, 1);
811 temp.off = 0;
812 break;
813 case MEM_REF:
814 /* The base address gets its own vn_reference_op_s structure. */
815 temp.op0 = TREE_OPERAND (ref, 1);
816 if (tree_fits_shwi_p (TREE_OPERAND (ref, 1)))
817 temp.off = tree_to_shwi (TREE_OPERAND (ref, 1));
818 break;
819 case BIT_FIELD_REF:
820 /* Record bits and position. */
821 temp.op0 = TREE_OPERAND (ref, 1);
822 temp.op1 = TREE_OPERAND (ref, 2);
823 break;
824 case COMPONENT_REF:
825 /* The field decl is enough to unambiguously specify the field,
826 a matching type is not necessary and a mismatching type
827 is always a spurious difference. */
828 temp.type = NULL_TREE;
829 temp.op0 = TREE_OPERAND (ref, 1);
830 temp.op1 = TREE_OPERAND (ref, 2);
832 tree this_offset = component_ref_field_offset (ref);
833 if (this_offset
834 && TREE_CODE (this_offset) == INTEGER_CST)
836 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
837 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
839 offset_int off
840 = (wi::to_offset (this_offset)
841 + wi::lrshift (wi::to_offset (bit_offset),
842 LOG2_BITS_PER_UNIT));
843 if (wi::fits_shwi_p (off)
844 /* Probibit value-numbering zero offset components
845 of addresses the same before the pass folding
846 __builtin_object_size had a chance to run
847 (checking cfun->after_inlining does the
848 trick here). */
849 && (TREE_CODE (orig) != ADDR_EXPR
850 || off != 0
851 || cfun->after_inlining))
852 temp.off = off.to_shwi ();
856 break;
857 case ARRAY_RANGE_REF:
858 case ARRAY_REF:
859 /* Record index as operand. */
860 temp.op0 = TREE_OPERAND (ref, 1);
861 /* Always record lower bounds and element size. */
862 temp.op1 = array_ref_low_bound (ref);
863 temp.op2 = array_ref_element_size (ref);
864 if (TREE_CODE (temp.op0) == INTEGER_CST
865 && TREE_CODE (temp.op1) == INTEGER_CST
866 && TREE_CODE (temp.op2) == INTEGER_CST)
868 offset_int off = ((wi::to_offset (temp.op0)
869 - wi::to_offset (temp.op1))
870 * wi::to_offset (temp.op2));
871 if (wi::fits_shwi_p (off))
872 temp.off = off.to_shwi();
874 break;
875 case VAR_DECL:
876 if (DECL_HARD_REGISTER (ref))
878 temp.op0 = ref;
879 break;
881 /* Fallthru. */
882 case PARM_DECL:
883 case CONST_DECL:
884 case RESULT_DECL:
885 /* Canonicalize decls to MEM[&decl] which is what we end up with
886 when valueizing MEM[ptr] with ptr = &decl. */
887 temp.opcode = MEM_REF;
888 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
889 temp.off = 0;
890 result->safe_push (temp);
891 temp.opcode = ADDR_EXPR;
892 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
893 temp.type = TREE_TYPE (temp.op0);
894 temp.off = -1;
895 break;
896 case STRING_CST:
897 case INTEGER_CST:
898 case COMPLEX_CST:
899 case VECTOR_CST:
900 case REAL_CST:
901 case FIXED_CST:
902 case CONSTRUCTOR:
903 case SSA_NAME:
904 temp.op0 = ref;
905 break;
906 case ADDR_EXPR:
907 if (is_gimple_min_invariant (ref))
909 temp.op0 = ref;
910 break;
912 /* Fallthrough. */
913 /* These are only interesting for their operands, their
914 existence, and their type. They will never be the last
915 ref in the chain of references (IE they require an
916 operand), so we don't have to put anything
917 for op* as it will be handled by the iteration */
918 case REALPART_EXPR:
919 case VIEW_CONVERT_EXPR:
920 temp.off = 0;
921 break;
922 case IMAGPART_EXPR:
923 /* This is only interesting for its constant offset. */
924 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
925 break;
926 default:
927 gcc_unreachable ();
929 result->safe_push (temp);
931 if (REFERENCE_CLASS_P (ref)
932 || TREE_CODE (ref) == MODIFY_EXPR
933 || TREE_CODE (ref) == WITH_SIZE_EXPR
934 || (TREE_CODE (ref) == ADDR_EXPR
935 && !is_gimple_min_invariant (ref)))
936 ref = TREE_OPERAND (ref, 0);
937 else
938 ref = NULL_TREE;
942 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
943 operands in *OPS, the reference alias set SET and the reference type TYPE.
944 Return true if something useful was produced. */
946 bool
947 ao_ref_init_from_vn_reference (ao_ref *ref,
948 alias_set_type set, tree type,
949 vec<vn_reference_op_s> ops)
951 vn_reference_op_t op;
952 unsigned i;
953 tree base = NULL_TREE;
954 tree *op0_p = &base;
955 HOST_WIDE_INT offset = 0;
956 HOST_WIDE_INT max_size;
957 HOST_WIDE_INT size = -1;
958 tree size_tree = NULL_TREE;
959 alias_set_type base_alias_set = -1;
961 /* First get the final access size from just the outermost expression. */
962 op = &ops[0];
963 if (op->opcode == COMPONENT_REF)
964 size_tree = DECL_SIZE (op->op0);
965 else if (op->opcode == BIT_FIELD_REF)
966 size_tree = op->op0;
967 else
969 enum machine_mode mode = TYPE_MODE (type);
970 if (mode == BLKmode)
971 size_tree = TYPE_SIZE (type);
972 else
973 size = GET_MODE_BITSIZE (mode);
975 if (size_tree != NULL_TREE)
977 if (!tree_fits_uhwi_p (size_tree))
978 size = -1;
979 else
980 size = tree_to_uhwi (size_tree);
983 /* Initially, maxsize is the same as the accessed element size.
984 In the following it will only grow (or become -1). */
985 max_size = size;
987 /* Compute cumulative bit-offset for nested component-refs and array-refs,
988 and find the ultimate containing object. */
989 FOR_EACH_VEC_ELT (ops, i, op)
991 switch (op->opcode)
993 /* These may be in the reference ops, but we cannot do anything
994 sensible with them here. */
995 case ADDR_EXPR:
996 /* Apart from ADDR_EXPR arguments to MEM_REF. */
997 if (base != NULL_TREE
998 && TREE_CODE (base) == MEM_REF
999 && op->op0
1000 && DECL_P (TREE_OPERAND (op->op0, 0)))
1002 vn_reference_op_t pop = &ops[i-1];
1003 base = TREE_OPERAND (op->op0, 0);
1004 if (pop->off == -1)
1006 max_size = -1;
1007 offset = 0;
1009 else
1010 offset += pop->off * BITS_PER_UNIT;
1011 op0_p = NULL;
1012 break;
1014 /* Fallthru. */
1015 case CALL_EXPR:
1016 return false;
1018 /* Record the base objects. */
1019 case MEM_REF:
1020 base_alias_set = get_deref_alias_set (op->op0);
1021 *op0_p = build2 (MEM_REF, op->type,
1022 NULL_TREE, op->op0);
1023 op0_p = &TREE_OPERAND (*op0_p, 0);
1024 break;
1026 case VAR_DECL:
1027 case PARM_DECL:
1028 case RESULT_DECL:
1029 case SSA_NAME:
1030 *op0_p = op->op0;
1031 op0_p = NULL;
1032 break;
1034 /* And now the usual component-reference style ops. */
1035 case BIT_FIELD_REF:
1036 offset += tree_to_shwi (op->op1);
1037 break;
1039 case COMPONENT_REF:
1041 tree field = op->op0;
1042 /* We do not have a complete COMPONENT_REF tree here so we
1043 cannot use component_ref_field_offset. Do the interesting
1044 parts manually. */
1046 if (op->op1
1047 || !tree_fits_uhwi_p (DECL_FIELD_OFFSET (field)))
1048 max_size = -1;
1049 else
1051 offset += (tree_to_uhwi (DECL_FIELD_OFFSET (field))
1052 * BITS_PER_UNIT);
1053 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
1055 break;
1058 case ARRAY_RANGE_REF:
1059 case ARRAY_REF:
1060 /* We recorded the lower bound and the element size. */
1061 if (!tree_fits_shwi_p (op->op0)
1062 || !tree_fits_shwi_p (op->op1)
1063 || !tree_fits_shwi_p (op->op2))
1064 max_size = -1;
1065 else
1067 HOST_WIDE_INT hindex = tree_to_shwi (op->op0);
1068 hindex -= tree_to_shwi (op->op1);
1069 hindex *= tree_to_shwi (op->op2);
1070 hindex *= BITS_PER_UNIT;
1071 offset += hindex;
1073 break;
1075 case REALPART_EXPR:
1076 break;
1078 case IMAGPART_EXPR:
1079 offset += size;
1080 break;
1082 case VIEW_CONVERT_EXPR:
1083 break;
1085 case STRING_CST:
1086 case INTEGER_CST:
1087 case COMPLEX_CST:
1088 case VECTOR_CST:
1089 case REAL_CST:
1090 case CONSTRUCTOR:
1091 case CONST_DECL:
1092 return false;
1094 default:
1095 return false;
1099 if (base == NULL_TREE)
1100 return false;
1102 ref->ref = NULL_TREE;
1103 ref->base = base;
1104 ref->offset = offset;
1105 ref->size = size;
1106 ref->max_size = max_size;
1107 ref->ref_alias_set = set;
1108 if (base_alias_set != -1)
1109 ref->base_alias_set = base_alias_set;
1110 else
1111 ref->base_alias_set = get_alias_set (base);
1112 /* We discount volatiles from value-numbering elsewhere. */
1113 ref->volatile_p = false;
1115 return true;
1118 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1119 vn_reference_op_s's. */
1121 void
1122 copy_reference_ops_from_call (gimple call,
1123 vec<vn_reference_op_s> *result)
1125 vn_reference_op_s temp;
1126 unsigned i;
1127 tree lhs = gimple_call_lhs (call);
1128 int lr;
1130 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1131 different. By adding the lhs here in the vector, we ensure that the
1132 hashcode is different, guaranteeing a different value number. */
1133 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1135 memset (&temp, 0, sizeof (temp));
1136 temp.opcode = MODIFY_EXPR;
1137 temp.type = TREE_TYPE (lhs);
1138 temp.op0 = lhs;
1139 temp.off = -1;
1140 result->safe_push (temp);
1143 /* Copy the type, opcode, function, static chain and EH region, if any. */
1144 memset (&temp, 0, sizeof (temp));
1145 temp.type = gimple_call_return_type (call);
1146 temp.opcode = CALL_EXPR;
1147 temp.op0 = gimple_call_fn (call);
1148 temp.op1 = gimple_call_chain (call);
1149 if (stmt_could_throw_p (call) && (lr = lookup_stmt_eh_lp (call)) > 0)
1150 temp.op2 = size_int (lr);
1151 temp.off = -1;
1152 result->safe_push (temp);
1154 /* Copy the call arguments. As they can be references as well,
1155 just chain them together. */
1156 for (i = 0; i < gimple_call_num_args (call); ++i)
1158 tree callarg = gimple_call_arg (call, i);
1159 copy_reference_ops_from_ref (callarg, result);
1163 /* Create a vector of vn_reference_op_s structures from CALL, a
1164 call statement. The vector is not shared. */
1166 static vec<vn_reference_op_s>
1167 create_reference_ops_from_call (gimple call)
1169 vec<vn_reference_op_s> result = vNULL;
1171 copy_reference_ops_from_call (call, &result);
1172 return result;
1175 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1176 *I_P to point to the last element of the replacement. */
1177 void
1178 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1179 unsigned int *i_p)
1181 unsigned int i = *i_p;
1182 vn_reference_op_t op = &(*ops)[i];
1183 vn_reference_op_t mem_op = &(*ops)[i - 1];
1184 tree addr_base;
1185 HOST_WIDE_INT addr_offset = 0;
1187 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1188 from .foo.bar to the preceding MEM_REF offset and replace the
1189 address with &OBJ. */
1190 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1191 &addr_offset);
1192 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1193 if (addr_base != TREE_OPERAND (op->op0, 0))
1195 offset_int off = offset_int::from (mem_op->op0, SIGNED);
1196 off += addr_offset;
1197 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1198 op->op0 = build_fold_addr_expr (addr_base);
1199 if (tree_fits_shwi_p (mem_op->op0))
1200 mem_op->off = tree_to_shwi (mem_op->op0);
1201 else
1202 mem_op->off = -1;
1206 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1207 *I_P to point to the last element of the replacement. */
1208 static void
1209 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1210 unsigned int *i_p)
1212 unsigned int i = *i_p;
1213 vn_reference_op_t op = &(*ops)[i];
1214 vn_reference_op_t mem_op = &(*ops)[i - 1];
1215 gimple def_stmt;
1216 enum tree_code code;
1217 offset_int off;
1219 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1220 if (!is_gimple_assign (def_stmt))
1221 return;
1223 code = gimple_assign_rhs_code (def_stmt);
1224 if (code != ADDR_EXPR
1225 && code != POINTER_PLUS_EXPR)
1226 return;
1228 off = offset_int::from (mem_op->op0, SIGNED);
1230 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1231 from .foo.bar to the preceding MEM_REF offset and replace the
1232 address with &OBJ. */
1233 if (code == ADDR_EXPR)
1235 tree addr, addr_base;
1236 HOST_WIDE_INT addr_offset;
1238 addr = gimple_assign_rhs1 (def_stmt);
1239 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1240 &addr_offset);
1241 if (!addr_base
1242 || TREE_CODE (addr_base) != MEM_REF)
1243 return;
1245 off += addr_offset;
1246 off += mem_ref_offset (addr_base);
1247 op->op0 = TREE_OPERAND (addr_base, 0);
1249 else
1251 tree ptr, ptroff;
1252 ptr = gimple_assign_rhs1 (def_stmt);
1253 ptroff = gimple_assign_rhs2 (def_stmt);
1254 if (TREE_CODE (ptr) != SSA_NAME
1255 || TREE_CODE (ptroff) != INTEGER_CST)
1256 return;
1258 off += wi::to_offset (ptroff);
1259 op->op0 = ptr;
1262 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1263 if (tree_fits_shwi_p (mem_op->op0))
1264 mem_op->off = tree_to_shwi (mem_op->op0);
1265 else
1266 mem_op->off = -1;
1267 if (TREE_CODE (op->op0) == SSA_NAME)
1268 op->op0 = SSA_VAL (op->op0);
1269 if (TREE_CODE (op->op0) != SSA_NAME)
1270 op->opcode = TREE_CODE (op->op0);
1272 /* And recurse. */
1273 if (TREE_CODE (op->op0) == SSA_NAME)
1274 vn_reference_maybe_forwprop_address (ops, i_p);
1275 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1276 vn_reference_fold_indirect (ops, i_p);
1279 /* Optimize the reference REF to a constant if possible or return
1280 NULL_TREE if not. */
1282 tree
1283 fully_constant_vn_reference_p (vn_reference_t ref)
1285 vec<vn_reference_op_s> operands = ref->operands;
1286 vn_reference_op_t op;
1288 /* Try to simplify the translated expression if it is
1289 a call to a builtin function with at most two arguments. */
1290 op = &operands[0];
1291 if (op->opcode == CALL_EXPR
1292 && TREE_CODE (op->op0) == ADDR_EXPR
1293 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1294 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1295 && operands.length () >= 2
1296 && operands.length () <= 3)
1298 vn_reference_op_t arg0, arg1 = NULL;
1299 bool anyconst = false;
1300 arg0 = &operands[1];
1301 if (operands.length () > 2)
1302 arg1 = &operands[2];
1303 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1304 || (arg0->opcode == ADDR_EXPR
1305 && is_gimple_min_invariant (arg0->op0)))
1306 anyconst = true;
1307 if (arg1
1308 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1309 || (arg1->opcode == ADDR_EXPR
1310 && is_gimple_min_invariant (arg1->op0))))
1311 anyconst = true;
1312 if (anyconst)
1314 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1315 arg1 ? 2 : 1,
1316 arg0->op0,
1317 arg1 ? arg1->op0 : NULL);
1318 if (folded
1319 && TREE_CODE (folded) == NOP_EXPR)
1320 folded = TREE_OPERAND (folded, 0);
1321 if (folded
1322 && is_gimple_min_invariant (folded))
1323 return folded;
1327 /* Simplify reads from constant strings. */
1328 else if (op->opcode == ARRAY_REF
1329 && TREE_CODE (op->op0) == INTEGER_CST
1330 && integer_zerop (op->op1)
1331 && operands.length () == 2)
1333 vn_reference_op_t arg0;
1334 arg0 = &operands[1];
1335 if (arg0->opcode == STRING_CST
1336 && (TYPE_MODE (op->type)
1337 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1338 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1339 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1340 && tree_int_cst_sgn (op->op0) >= 0
1341 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1342 return build_int_cst_type (op->type,
1343 (TREE_STRING_POINTER (arg0->op0)
1344 [TREE_INT_CST_LOW (op->op0)]));
1347 return NULL_TREE;
1350 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1351 structures into their value numbers. This is done in-place, and
1352 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1353 whether any operands were valueized. */
1355 static vec<vn_reference_op_s>
1356 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1358 vn_reference_op_t vro;
1359 unsigned int i;
1361 *valueized_anything = false;
1363 FOR_EACH_VEC_ELT (orig, i, vro)
1365 if (vro->opcode == SSA_NAME
1366 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1368 tree tem = SSA_VAL (vro->op0);
1369 if (tem != vro->op0)
1371 *valueized_anything = true;
1372 vro->op0 = tem;
1374 /* If it transforms from an SSA_NAME to a constant, update
1375 the opcode. */
1376 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1377 vro->opcode = TREE_CODE (vro->op0);
1379 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1381 tree tem = SSA_VAL (vro->op1);
1382 if (tem != vro->op1)
1384 *valueized_anything = true;
1385 vro->op1 = tem;
1388 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1390 tree tem = SSA_VAL (vro->op2);
1391 if (tem != vro->op2)
1393 *valueized_anything = true;
1394 vro->op2 = tem;
1397 /* If it transforms from an SSA_NAME to an address, fold with
1398 a preceding indirect reference. */
1399 if (i > 0
1400 && vro->op0
1401 && TREE_CODE (vro->op0) == ADDR_EXPR
1402 && orig[i - 1].opcode == MEM_REF)
1403 vn_reference_fold_indirect (&orig, &i);
1404 else if (i > 0
1405 && vro->opcode == SSA_NAME
1406 && orig[i - 1].opcode == MEM_REF)
1407 vn_reference_maybe_forwprop_address (&orig, &i);
1408 /* If it transforms a non-constant ARRAY_REF into a constant
1409 one, adjust the constant offset. */
1410 else if (vro->opcode == ARRAY_REF
1411 && vro->off == -1
1412 && TREE_CODE (vro->op0) == INTEGER_CST
1413 && TREE_CODE (vro->op1) == INTEGER_CST
1414 && TREE_CODE (vro->op2) == INTEGER_CST)
1416 offset_int off = ((wi::to_offset (vro->op0)
1417 - wi::to_offset (vro->op1))
1418 * wi::to_offset (vro->op2));
1419 if (wi::fits_shwi_p (off))
1420 vro->off = off.to_shwi ();
1424 return orig;
1427 static vec<vn_reference_op_s>
1428 valueize_refs (vec<vn_reference_op_s> orig)
1430 bool tem;
1431 return valueize_refs_1 (orig, &tem);
1434 static vec<vn_reference_op_s> shared_lookup_references;
1436 /* Create a vector of vn_reference_op_s structures from REF, a
1437 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1438 this function. *VALUEIZED_ANYTHING will specify whether any
1439 operands were valueized. */
1441 static vec<vn_reference_op_s>
1442 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1444 if (!ref)
1445 return vNULL;
1446 shared_lookup_references.truncate (0);
1447 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1448 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1449 valueized_anything);
1450 return shared_lookup_references;
1453 /* Create a vector of vn_reference_op_s structures from CALL, a
1454 call statement. The vector is shared among all callers of
1455 this function. */
1457 static vec<vn_reference_op_s>
1458 valueize_shared_reference_ops_from_call (gimple call)
1460 if (!call)
1461 return vNULL;
1462 shared_lookup_references.truncate (0);
1463 copy_reference_ops_from_call (call, &shared_lookup_references);
1464 shared_lookup_references = valueize_refs (shared_lookup_references);
1465 return shared_lookup_references;
1468 /* Lookup a SCCVN reference operation VR in the current hash table.
1469 Returns the resulting value number if it exists in the hash table,
1470 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1471 vn_reference_t stored in the hashtable if something is found. */
1473 static tree
1474 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1476 vn_reference_s **slot;
1477 hashval_t hash;
1479 hash = vr->hashcode;
1480 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1481 if (!slot && current_info == optimistic_info)
1482 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1483 if (slot)
1485 if (vnresult)
1486 *vnresult = (vn_reference_t)*slot;
1487 return ((vn_reference_t)*slot)->result;
1490 return NULL_TREE;
1493 static tree *last_vuse_ptr;
1494 static vn_lookup_kind vn_walk_kind;
1495 static vn_lookup_kind default_vn_walk_kind;
1497 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1498 with the current VUSE and performs the expression lookup. */
1500 static void *
1501 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1502 unsigned int cnt, void *vr_)
1504 vn_reference_t vr = (vn_reference_t)vr_;
1505 vn_reference_s **slot;
1506 hashval_t hash;
1508 /* This bounds the stmt walks we perform on reference lookups
1509 to O(1) instead of O(N) where N is the number of dominating
1510 stores. */
1511 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1512 return (void *)-1;
1514 if (last_vuse_ptr)
1515 *last_vuse_ptr = vuse;
1517 /* Fixup vuse and hash. */
1518 if (vr->vuse)
1519 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1520 vr->vuse = vuse_ssa_val (vuse);
1521 if (vr->vuse)
1522 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1524 hash = vr->hashcode;
1525 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1526 if (!slot && current_info == optimistic_info)
1527 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1528 if (slot)
1529 return *slot;
1531 return NULL;
1534 /* Lookup an existing or insert a new vn_reference entry into the
1535 value table for the VUSE, SET, TYPE, OPERANDS reference which
1536 has the value VALUE which is either a constant or an SSA name. */
1538 static vn_reference_t
1539 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1540 alias_set_type set,
1541 tree type,
1542 vec<vn_reference_op_s,
1543 va_heap> operands,
1544 tree value)
1546 struct vn_reference_s vr1;
1547 vn_reference_t result;
1548 unsigned value_id;
1549 vr1.vuse = vuse;
1550 vr1.operands = operands;
1551 vr1.type = type;
1552 vr1.set = set;
1553 vr1.hashcode = vn_reference_compute_hash (&vr1);
1554 if (vn_reference_lookup_1 (&vr1, &result))
1555 return result;
1556 if (TREE_CODE (value) == SSA_NAME)
1557 value_id = VN_INFO (value)->value_id;
1558 else
1559 value_id = get_or_alloc_constant_value_id (value);
1560 return vn_reference_insert_pieces (vuse, set, type,
1561 operands.copy (), value, value_id);
1564 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1565 from the statement defining VUSE and if not successful tries to
1566 translate *REFP and VR_ through an aggregate copy at the definition
1567 of VUSE. */
1569 static void *
1570 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1571 bool disambiguate_only)
1573 vn_reference_t vr = (vn_reference_t)vr_;
1574 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1575 tree base;
1576 HOST_WIDE_INT offset, maxsize;
1577 static vec<vn_reference_op_s>
1578 lhs_ops = vNULL;
1579 ao_ref lhs_ref;
1580 bool lhs_ref_ok = false;
1582 /* First try to disambiguate after value-replacing in the definitions LHS. */
1583 if (is_gimple_assign (def_stmt))
1585 vec<vn_reference_op_s> tem;
1586 tree lhs = gimple_assign_lhs (def_stmt);
1587 bool valueized_anything = false;
1588 /* Avoid re-allocation overhead. */
1589 lhs_ops.truncate (0);
1590 copy_reference_ops_from_ref (lhs, &lhs_ops);
1591 tem = lhs_ops;
1592 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1593 gcc_assert (lhs_ops == tem);
1594 if (valueized_anything)
1596 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1597 get_alias_set (lhs),
1598 TREE_TYPE (lhs), lhs_ops);
1599 if (lhs_ref_ok
1600 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1601 return NULL;
1603 else
1605 ao_ref_init (&lhs_ref, lhs);
1606 lhs_ref_ok = true;
1609 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1610 && gimple_call_num_args (def_stmt) <= 4)
1612 /* For builtin calls valueize its arguments and call the
1613 alias oracle again. Valueization may improve points-to
1614 info of pointers and constify size and position arguments.
1615 Originally this was motivated by PR61034 which has
1616 conditional calls to free falsely clobbering ref because
1617 of imprecise points-to info of the argument. */
1618 tree oldargs[4];
1619 bool valueized_anything = false;
1620 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1622 oldargs[i] = gimple_call_arg (def_stmt, i);
1623 if (TREE_CODE (oldargs[i]) == SSA_NAME
1624 && VN_INFO (oldargs[i])->valnum != oldargs[i])
1626 gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum);
1627 valueized_anything = true;
1630 if (valueized_anything)
1632 bool res = call_may_clobber_ref_p_1 (def_stmt, ref);
1633 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1634 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1635 if (!res)
1636 return NULL;
1640 if (disambiguate_only)
1641 return (void *)-1;
1643 base = ao_ref_base (ref);
1644 offset = ref->offset;
1645 maxsize = ref->max_size;
1647 /* If we cannot constrain the size of the reference we cannot
1648 test if anything kills it. */
1649 if (maxsize == -1)
1650 return (void *)-1;
1652 /* We can't deduce anything useful from clobbers. */
1653 if (gimple_clobber_p (def_stmt))
1654 return (void *)-1;
1656 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1657 from that definition.
1658 1) Memset. */
1659 if (is_gimple_reg_type (vr->type)
1660 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1661 && integer_zerop (gimple_call_arg (def_stmt, 1))
1662 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))
1663 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1665 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1666 tree base2;
1667 HOST_WIDE_INT offset2, size2, maxsize2;
1668 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1669 size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8;
1670 if ((unsigned HOST_WIDE_INT)size2 / 8
1671 == tree_to_uhwi (gimple_call_arg (def_stmt, 2))
1672 && maxsize2 != -1
1673 && operand_equal_p (base, base2, 0)
1674 && offset2 <= offset
1675 && offset2 + size2 >= offset + maxsize)
1677 tree val = build_zero_cst (vr->type);
1678 return vn_reference_lookup_or_insert_for_pieces
1679 (vuse, vr->set, vr->type, vr->operands, val);
1683 /* 2) Assignment from an empty CONSTRUCTOR. */
1684 else if (is_gimple_reg_type (vr->type)
1685 && gimple_assign_single_p (def_stmt)
1686 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1687 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1689 tree base2;
1690 HOST_WIDE_INT offset2, size2, maxsize2;
1691 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1692 &offset2, &size2, &maxsize2);
1693 if (maxsize2 != -1
1694 && operand_equal_p (base, base2, 0)
1695 && offset2 <= offset
1696 && offset2 + size2 >= offset + maxsize)
1698 tree val = build_zero_cst (vr->type);
1699 return vn_reference_lookup_or_insert_for_pieces
1700 (vuse, vr->set, vr->type, vr->operands, val);
1704 /* 3) Assignment from a constant. We can use folds native encode/interpret
1705 routines to extract the assigned bits. */
1706 else if (vn_walk_kind == VN_WALKREWRITE
1707 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1708 && ref->size == maxsize
1709 && maxsize % BITS_PER_UNIT == 0
1710 && offset % BITS_PER_UNIT == 0
1711 && is_gimple_reg_type (vr->type)
1712 && gimple_assign_single_p (def_stmt)
1713 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1715 tree base2;
1716 HOST_WIDE_INT offset2, size2, maxsize2;
1717 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1718 &offset2, &size2, &maxsize2);
1719 if (maxsize2 != -1
1720 && maxsize2 == size2
1721 && size2 % BITS_PER_UNIT == 0
1722 && offset2 % BITS_PER_UNIT == 0
1723 && operand_equal_p (base, base2, 0)
1724 && offset2 <= offset
1725 && offset2 + size2 >= offset + maxsize)
1727 /* We support up to 512-bit values (for V8DFmode). */
1728 unsigned char buffer[64];
1729 int len;
1731 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1732 buffer, sizeof (buffer));
1733 if (len > 0)
1735 tree val = native_interpret_expr (vr->type,
1736 buffer
1737 + ((offset - offset2)
1738 / BITS_PER_UNIT),
1739 ref->size / BITS_PER_UNIT);
1740 if (val)
1741 return vn_reference_lookup_or_insert_for_pieces
1742 (vuse, vr->set, vr->type, vr->operands, val);
1747 /* 4) Assignment from an SSA name which definition we may be able
1748 to access pieces from. */
1749 else if (ref->size == maxsize
1750 && is_gimple_reg_type (vr->type)
1751 && gimple_assign_single_p (def_stmt)
1752 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1754 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1755 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1756 if (is_gimple_assign (def_stmt2)
1757 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1758 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1759 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1761 tree base2;
1762 HOST_WIDE_INT offset2, size2, maxsize2, off;
1763 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1764 &offset2, &size2, &maxsize2);
1765 off = offset - offset2;
1766 if (maxsize2 != -1
1767 && maxsize2 == size2
1768 && operand_equal_p (base, base2, 0)
1769 && offset2 <= offset
1770 && offset2 + size2 >= offset + maxsize)
1772 tree val = NULL_TREE;
1773 HOST_WIDE_INT elsz
1774 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1775 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1777 if (off == 0)
1778 val = gimple_assign_rhs1 (def_stmt2);
1779 else if (off == elsz)
1780 val = gimple_assign_rhs2 (def_stmt2);
1782 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1783 && off % elsz == 0)
1785 tree ctor = gimple_assign_rhs1 (def_stmt2);
1786 unsigned i = off / elsz;
1787 if (i < CONSTRUCTOR_NELTS (ctor))
1789 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1790 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1792 if (TREE_CODE (TREE_TYPE (elt->value))
1793 != VECTOR_TYPE)
1794 val = elt->value;
1798 if (val)
1799 return vn_reference_lookup_or_insert_for_pieces
1800 (vuse, vr->set, vr->type, vr->operands, val);
1805 /* 5) For aggregate copies translate the reference through them if
1806 the copy kills ref. */
1807 else if (vn_walk_kind == VN_WALKREWRITE
1808 && gimple_assign_single_p (def_stmt)
1809 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1810 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1811 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1813 tree base2;
1814 HOST_WIDE_INT offset2, size2, maxsize2;
1815 int i, j;
1816 auto_vec<vn_reference_op_s> rhs;
1817 vn_reference_op_t vro;
1818 ao_ref r;
1820 if (!lhs_ref_ok)
1821 return (void *)-1;
1823 /* See if the assignment kills REF. */
1824 base2 = ao_ref_base (&lhs_ref);
1825 offset2 = lhs_ref.offset;
1826 size2 = lhs_ref.size;
1827 maxsize2 = lhs_ref.max_size;
1828 if (maxsize2 == -1
1829 || (base != base2 && !operand_equal_p (base, base2, 0))
1830 || offset2 > offset
1831 || offset2 + size2 < offset + maxsize)
1832 return (void *)-1;
1834 /* Find the common base of ref and the lhs. lhs_ops already
1835 contains valueized operands for the lhs. */
1836 i = vr->operands.length () - 1;
1837 j = lhs_ops.length () - 1;
1838 while (j >= 0 && i >= 0
1839 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1841 i--;
1842 j--;
1845 /* ??? The innermost op should always be a MEM_REF and we already
1846 checked that the assignment to the lhs kills vr. Thus for
1847 aggregate copies using char[] types the vn_reference_op_eq
1848 may fail when comparing types for compatibility. But we really
1849 don't care here - further lookups with the rewritten operands
1850 will simply fail if we messed up types too badly. */
1851 if (j == 0 && i >= 0
1852 && lhs_ops[0].opcode == MEM_REF
1853 && lhs_ops[0].off != -1
1854 && (lhs_ops[0].off == vr->operands[i].off))
1855 i--, j--;
1857 /* i now points to the first additional op.
1858 ??? LHS may not be completely contained in VR, one or more
1859 VIEW_CONVERT_EXPRs could be in its way. We could at least
1860 try handling outermost VIEW_CONVERT_EXPRs. */
1861 if (j != -1)
1862 return (void *)-1;
1864 /* Now re-write REF to be based on the rhs of the assignment. */
1865 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1866 /* We need to pre-pend vr->operands[0..i] to rhs. */
1867 if (i + 1 + rhs.length () > vr->operands.length ())
1869 vec<vn_reference_op_s> old = vr->operands;
1870 vr->operands.safe_grow (i + 1 + rhs.length ());
1871 if (old == shared_lookup_references
1872 && vr->operands != old)
1873 shared_lookup_references = vNULL;
1875 else
1876 vr->operands.truncate (i + 1 + rhs.length ());
1877 FOR_EACH_VEC_ELT (rhs, j, vro)
1878 vr->operands[i + 1 + j] = *vro;
1879 vr->operands = valueize_refs (vr->operands);
1880 vr->hashcode = vn_reference_compute_hash (vr);
1882 /* Adjust *ref from the new operands. */
1883 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1884 return (void *)-1;
1885 /* This can happen with bitfields. */
1886 if (ref->size != r.size)
1887 return (void *)-1;
1888 *ref = r;
1890 /* Do not update last seen VUSE after translating. */
1891 last_vuse_ptr = NULL;
1893 /* Keep looking for the adjusted *REF / VR pair. */
1894 return NULL;
1897 /* 6) For memcpy copies translate the reference through them if
1898 the copy kills ref. */
1899 else if (vn_walk_kind == VN_WALKREWRITE
1900 && is_gimple_reg_type (vr->type)
1901 /* ??? Handle BCOPY as well. */
1902 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1903 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1904 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1905 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1906 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1907 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1908 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1909 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)))
1911 tree lhs, rhs;
1912 ao_ref r;
1913 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1914 vn_reference_op_s op;
1915 HOST_WIDE_INT at;
1918 /* Only handle non-variable, addressable refs. */
1919 if (ref->size != maxsize
1920 || offset % BITS_PER_UNIT != 0
1921 || ref->size % BITS_PER_UNIT != 0)
1922 return (void *)-1;
1924 /* Extract a pointer base and an offset for the destination. */
1925 lhs = gimple_call_arg (def_stmt, 0);
1926 lhs_offset = 0;
1927 if (TREE_CODE (lhs) == SSA_NAME)
1928 lhs = SSA_VAL (lhs);
1929 if (TREE_CODE (lhs) == ADDR_EXPR)
1931 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1932 &lhs_offset);
1933 if (!tem)
1934 return (void *)-1;
1935 if (TREE_CODE (tem) == MEM_REF
1936 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
1938 lhs = TREE_OPERAND (tem, 0);
1939 lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
1941 else if (DECL_P (tem))
1942 lhs = build_fold_addr_expr (tem);
1943 else
1944 return (void *)-1;
1946 if (TREE_CODE (lhs) != SSA_NAME
1947 && TREE_CODE (lhs) != ADDR_EXPR)
1948 return (void *)-1;
1950 /* Extract a pointer base and an offset for the source. */
1951 rhs = gimple_call_arg (def_stmt, 1);
1952 rhs_offset = 0;
1953 if (TREE_CODE (rhs) == SSA_NAME)
1954 rhs = SSA_VAL (rhs);
1955 if (TREE_CODE (rhs) == ADDR_EXPR)
1957 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1958 &rhs_offset);
1959 if (!tem)
1960 return (void *)-1;
1961 if (TREE_CODE (tem) == MEM_REF
1962 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
1964 rhs = TREE_OPERAND (tem, 0);
1965 rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
1967 else if (DECL_P (tem))
1968 rhs = build_fold_addr_expr (tem);
1969 else
1970 return (void *)-1;
1972 if (TREE_CODE (rhs) != SSA_NAME
1973 && TREE_CODE (rhs) != ADDR_EXPR)
1974 return (void *)-1;
1976 copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2));
1978 /* The bases of the destination and the references have to agree. */
1979 if ((TREE_CODE (base) != MEM_REF
1980 && !DECL_P (base))
1981 || (TREE_CODE (base) == MEM_REF
1982 && (TREE_OPERAND (base, 0) != lhs
1983 || !tree_fits_uhwi_p (TREE_OPERAND (base, 1))))
1984 || (DECL_P (base)
1985 && (TREE_CODE (lhs) != ADDR_EXPR
1986 || TREE_OPERAND (lhs, 0) != base)))
1987 return (void *)-1;
1989 /* And the access has to be contained within the memcpy destination. */
1990 at = offset / BITS_PER_UNIT;
1991 if (TREE_CODE (base) == MEM_REF)
1992 at += tree_to_uhwi (TREE_OPERAND (base, 1));
1993 if (lhs_offset > at
1994 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1995 return (void *)-1;
1997 /* Make room for 2 operands in the new reference. */
1998 if (vr->operands.length () < 2)
2000 vec<vn_reference_op_s> old = vr->operands;
2001 vr->operands.safe_grow_cleared (2);
2002 if (old == shared_lookup_references
2003 && vr->operands != old)
2004 shared_lookup_references.create (0);
2006 else
2007 vr->operands.truncate (2);
2009 /* The looked-through reference is a simple MEM_REF. */
2010 memset (&op, 0, sizeof (op));
2011 op.type = vr->type;
2012 op.opcode = MEM_REF;
2013 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
2014 op.off = at - lhs_offset + rhs_offset;
2015 vr->operands[0] = op;
2016 op.type = TREE_TYPE (rhs);
2017 op.opcode = TREE_CODE (rhs);
2018 op.op0 = rhs;
2019 op.off = -1;
2020 vr->operands[1] = op;
2021 vr->hashcode = vn_reference_compute_hash (vr);
2023 /* Adjust *ref from the new operands. */
2024 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2025 return (void *)-1;
2026 /* This can happen with bitfields. */
2027 if (ref->size != r.size)
2028 return (void *)-1;
2029 *ref = r;
2031 /* Do not update last seen VUSE after translating. */
2032 last_vuse_ptr = NULL;
2034 /* Keep looking for the adjusted *REF / VR pair. */
2035 return NULL;
2038 /* Bail out and stop walking. */
2039 return (void *)-1;
2042 /* Lookup a reference operation by it's parts, in the current hash table.
2043 Returns the resulting value number if it exists in the hash table,
2044 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2045 vn_reference_t stored in the hashtable if something is found. */
2047 tree
2048 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2049 vec<vn_reference_op_s> operands,
2050 vn_reference_t *vnresult, vn_lookup_kind kind)
2052 struct vn_reference_s vr1;
2053 vn_reference_t tmp;
2054 tree cst;
2056 if (!vnresult)
2057 vnresult = &tmp;
2058 *vnresult = NULL;
2060 vr1.vuse = vuse_ssa_val (vuse);
2061 shared_lookup_references.truncate (0);
2062 shared_lookup_references.safe_grow (operands.length ());
2063 memcpy (shared_lookup_references.address (),
2064 operands.address (),
2065 sizeof (vn_reference_op_s)
2066 * operands.length ());
2067 vr1.operands = operands = shared_lookup_references
2068 = valueize_refs (shared_lookup_references);
2069 vr1.type = type;
2070 vr1.set = set;
2071 vr1.hashcode = vn_reference_compute_hash (&vr1);
2072 if ((cst = fully_constant_vn_reference_p (&vr1)))
2073 return cst;
2075 vn_reference_lookup_1 (&vr1, vnresult);
2076 if (!*vnresult
2077 && kind != VN_NOWALK
2078 && vr1.vuse)
2080 ao_ref r;
2081 vn_walk_kind = kind;
2082 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2083 *vnresult =
2084 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2085 vn_reference_lookup_2,
2086 vn_reference_lookup_3, &vr1);
2087 if (vr1.operands != operands)
2088 vr1.operands.release ();
2091 if (*vnresult)
2092 return (*vnresult)->result;
2094 return NULL_TREE;
2097 /* Lookup OP in the current hash table, and return the resulting value
2098 number if it exists in the hash table. Return NULL_TREE if it does
2099 not exist in the hash table or if the result field of the structure
2100 was NULL.. VNRESULT will be filled in with the vn_reference_t
2101 stored in the hashtable if one exists. */
2103 tree
2104 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2105 vn_reference_t *vnresult)
2107 vec<vn_reference_op_s> operands;
2108 struct vn_reference_s vr1;
2109 tree cst;
2110 bool valuezied_anything;
2112 if (vnresult)
2113 *vnresult = NULL;
2115 vr1.vuse = vuse_ssa_val (vuse);
2116 vr1.operands = operands
2117 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2118 vr1.type = TREE_TYPE (op);
2119 vr1.set = get_alias_set (op);
2120 vr1.hashcode = vn_reference_compute_hash (&vr1);
2121 if ((cst = fully_constant_vn_reference_p (&vr1)))
2122 return cst;
2124 if (kind != VN_NOWALK
2125 && vr1.vuse)
2127 vn_reference_t wvnresult;
2128 ao_ref r;
2129 /* Make sure to use a valueized reference if we valueized anything.
2130 Otherwise preserve the full reference for advanced TBAA. */
2131 if (!valuezied_anything
2132 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2133 vr1.operands))
2134 ao_ref_init (&r, op);
2135 vn_walk_kind = kind;
2136 wvnresult =
2137 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2138 vn_reference_lookup_2,
2139 vn_reference_lookup_3, &vr1);
2140 if (vr1.operands != operands)
2141 vr1.operands.release ();
2142 if (wvnresult)
2144 if (vnresult)
2145 *vnresult = wvnresult;
2146 return wvnresult->result;
2149 return NULL_TREE;
2152 return vn_reference_lookup_1 (&vr1, vnresult);
2156 /* Insert OP into the current hash table with a value number of
2157 RESULT, and return the resulting reference structure we created. */
2159 vn_reference_t
2160 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2162 vn_reference_s **slot;
2163 vn_reference_t vr1;
2164 bool tem;
2166 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2167 if (TREE_CODE (result) == SSA_NAME)
2168 vr1->value_id = VN_INFO (result)->value_id;
2169 else
2170 vr1->value_id = get_or_alloc_constant_value_id (result);
2171 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2172 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2173 vr1->type = TREE_TYPE (op);
2174 vr1->set = get_alias_set (op);
2175 vr1->hashcode = vn_reference_compute_hash (vr1);
2176 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2177 vr1->result_vdef = vdef;
2179 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2180 INSERT);
2182 /* Because we lookup stores using vuses, and value number failures
2183 using the vdefs (see visit_reference_op_store for how and why),
2184 it's possible that on failure we may try to insert an already
2185 inserted store. This is not wrong, there is no ssa name for a
2186 store that we could use as a differentiator anyway. Thus, unlike
2187 the other lookup functions, you cannot gcc_assert (!*slot)
2188 here. */
2190 /* But free the old slot in case of a collision. */
2191 if (*slot)
2192 free_reference (*slot);
2194 *slot = vr1;
2195 return vr1;
2198 /* Insert a reference by it's pieces into the current hash table with
2199 a value number of RESULT. Return the resulting reference
2200 structure we created. */
2202 vn_reference_t
2203 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2204 vec<vn_reference_op_s> operands,
2205 tree result, unsigned int value_id)
2208 vn_reference_s **slot;
2209 vn_reference_t vr1;
2211 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2212 vr1->value_id = value_id;
2213 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2214 vr1->operands = valueize_refs (operands);
2215 vr1->type = type;
2216 vr1->set = set;
2217 vr1->hashcode = vn_reference_compute_hash (vr1);
2218 if (result && TREE_CODE (result) == SSA_NAME)
2219 result = SSA_VAL (result);
2220 vr1->result = result;
2222 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2223 INSERT);
2225 /* At this point we should have all the things inserted that we have
2226 seen before, and we should never try inserting something that
2227 already exists. */
2228 gcc_assert (!*slot);
2229 if (*slot)
2230 free_reference (*slot);
2232 *slot = vr1;
2233 return vr1;
2236 /* Compute and return the hash value for nary operation VBO1. */
2238 hashval_t
2239 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2241 inchash::hash hstate;
2242 unsigned i;
2244 for (i = 0; i < vno1->length; ++i)
2245 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2246 vno1->op[i] = SSA_VAL (vno1->op[i]);
2248 if (vno1->length == 2
2249 && commutative_tree_code (vno1->opcode)
2250 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2252 tree temp = vno1->op[0];
2253 vno1->op[0] = vno1->op[1];
2254 vno1->op[1] = temp;
2257 hstate.add_int (vno1->opcode);
2258 for (i = 0; i < vno1->length; ++i)
2259 inchash::add_expr (vno1->op[i], hstate);
2261 return hstate.end ();
2264 /* Compare nary operations VNO1 and VNO2 and return true if they are
2265 equivalent. */
2267 bool
2268 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2270 unsigned i;
2272 if (vno1->hashcode != vno2->hashcode)
2273 return false;
2275 if (vno1->length != vno2->length)
2276 return false;
2278 if (vno1->opcode != vno2->opcode
2279 || !types_compatible_p (vno1->type, vno2->type))
2280 return false;
2282 for (i = 0; i < vno1->length; ++i)
2283 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2284 return false;
2286 return true;
2289 /* Initialize VNO from the pieces provided. */
2291 static void
2292 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2293 enum tree_code code, tree type, tree *ops)
2295 vno->opcode = code;
2296 vno->length = length;
2297 vno->type = type;
2298 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2301 /* Initialize VNO from OP. */
2303 static void
2304 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2306 unsigned i;
2308 vno->opcode = TREE_CODE (op);
2309 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2310 vno->type = TREE_TYPE (op);
2311 for (i = 0; i < vno->length; ++i)
2312 vno->op[i] = TREE_OPERAND (op, i);
2315 /* Return the number of operands for a vn_nary ops structure from STMT. */
2317 static unsigned int
2318 vn_nary_length_from_stmt (gimple stmt)
2320 switch (gimple_assign_rhs_code (stmt))
2322 case REALPART_EXPR:
2323 case IMAGPART_EXPR:
2324 case VIEW_CONVERT_EXPR:
2325 return 1;
2327 case BIT_FIELD_REF:
2328 return 3;
2330 case CONSTRUCTOR:
2331 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2333 default:
2334 return gimple_num_ops (stmt) - 1;
2338 /* Initialize VNO from STMT. */
2340 static void
2341 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2343 unsigned i;
2345 vno->opcode = gimple_assign_rhs_code (stmt);
2346 vno->type = gimple_expr_type (stmt);
2347 switch (vno->opcode)
2349 case REALPART_EXPR:
2350 case IMAGPART_EXPR:
2351 case VIEW_CONVERT_EXPR:
2352 vno->length = 1;
2353 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2354 break;
2356 case BIT_FIELD_REF:
2357 vno->length = 3;
2358 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2359 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2360 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2361 break;
2363 case CONSTRUCTOR:
2364 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2365 for (i = 0; i < vno->length; ++i)
2366 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2367 break;
2369 default:
2370 gcc_checking_assert (!gimple_assign_single_p (stmt));
2371 vno->length = gimple_num_ops (stmt) - 1;
2372 for (i = 0; i < vno->length; ++i)
2373 vno->op[i] = gimple_op (stmt, i + 1);
2377 /* Compute the hashcode for VNO and look for it in the hash table;
2378 return the resulting value number if it exists in the hash table.
2379 Return NULL_TREE if it does not exist in the hash table or if the
2380 result field of the operation is NULL. VNRESULT will contain the
2381 vn_nary_op_t from the hashtable if it exists. */
2383 static tree
2384 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2386 vn_nary_op_s **slot;
2388 if (vnresult)
2389 *vnresult = NULL;
2391 vno->hashcode = vn_nary_op_compute_hash (vno);
2392 slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode,
2393 NO_INSERT);
2394 if (!slot && current_info == optimistic_info)
2395 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode,
2396 NO_INSERT);
2397 if (!slot)
2398 return NULL_TREE;
2399 if (vnresult)
2400 *vnresult = *slot;
2401 return (*slot)->result;
2404 /* Lookup a n-ary operation by its pieces and return the resulting value
2405 number if it exists in the hash table. Return NULL_TREE if it does
2406 not exist in the hash table or if the result field of the operation
2407 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2408 if it exists. */
2410 tree
2411 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2412 tree type, tree *ops, vn_nary_op_t *vnresult)
2414 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2415 sizeof_vn_nary_op (length));
2416 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2417 return vn_nary_op_lookup_1 (vno1, vnresult);
2420 /* Lookup OP in the current hash table, and return the resulting value
2421 number if it exists in the hash table. Return NULL_TREE if it does
2422 not exist in the hash table or if the result field of the operation
2423 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2424 if it exists. */
2426 tree
2427 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2429 vn_nary_op_t vno1
2430 = XALLOCAVAR (struct vn_nary_op_s,
2431 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2432 init_vn_nary_op_from_op (vno1, op);
2433 return vn_nary_op_lookup_1 (vno1, vnresult);
2436 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2437 value number if it exists in the hash table. Return NULL_TREE if
2438 it does not exist in the hash table. VNRESULT will contain the
2439 vn_nary_op_t from the hashtable if it exists. */
2441 tree
2442 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2444 vn_nary_op_t vno1
2445 = XALLOCAVAR (struct vn_nary_op_s,
2446 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2447 init_vn_nary_op_from_stmt (vno1, stmt);
2448 return vn_nary_op_lookup_1 (vno1, vnresult);
2451 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2453 static vn_nary_op_t
2454 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2456 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2459 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2460 obstack. */
2462 static vn_nary_op_t
2463 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2465 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2466 &current_info->nary_obstack);
2468 vno1->value_id = value_id;
2469 vno1->length = length;
2470 vno1->result = result;
2472 return vno1;
2475 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2476 VNO->HASHCODE first. */
2478 static vn_nary_op_t
2479 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
2480 bool compute_hash)
2482 vn_nary_op_s **slot;
2484 if (compute_hash)
2485 vno->hashcode = vn_nary_op_compute_hash (vno);
2487 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
2488 gcc_assert (!*slot);
2490 *slot = vno;
2491 return vno;
2494 /* Insert a n-ary operation into the current hash table using it's
2495 pieces. Return the vn_nary_op_t structure we created and put in
2496 the hashtable. */
2498 vn_nary_op_t
2499 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2500 tree type, tree *ops,
2501 tree result, unsigned int value_id)
2503 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2504 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2505 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2508 /* Insert OP into the current hash table with a value number of
2509 RESULT. Return the vn_nary_op_t structure we created and put in
2510 the hashtable. */
2512 vn_nary_op_t
2513 vn_nary_op_insert (tree op, tree result)
2515 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2516 vn_nary_op_t vno1;
2518 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2519 init_vn_nary_op_from_op (vno1, op);
2520 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2523 /* Insert the rhs of STMT into the current hash table with a value number of
2524 RESULT. */
2526 vn_nary_op_t
2527 vn_nary_op_insert_stmt (gimple stmt, tree result)
2529 vn_nary_op_t vno1
2530 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2531 result, VN_INFO (result)->value_id);
2532 init_vn_nary_op_from_stmt (vno1, stmt);
2533 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2536 /* Compute a hashcode for PHI operation VP1 and return it. */
2538 static inline hashval_t
2539 vn_phi_compute_hash (vn_phi_t vp1)
2541 inchash::hash hstate (vp1->block->index);
2542 int i;
2543 tree phi1op;
2544 tree type;
2546 /* If all PHI arguments are constants we need to distinguish
2547 the PHI node via its type. */
2548 type = vp1->type;
2549 hstate.merge_hash (vn_hash_type (type));
2551 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2553 if (phi1op == VN_TOP)
2554 continue;
2555 inchash::add_expr (phi1op, hstate);
2558 return hstate.end ();
2561 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2563 static int
2564 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2566 if (vp1->hashcode != vp2->hashcode)
2567 return false;
2569 if (vp1->block == vp2->block)
2571 int i;
2572 tree phi1op;
2574 /* If the PHI nodes do not have compatible types
2575 they are not the same. */
2576 if (!types_compatible_p (vp1->type, vp2->type))
2577 return false;
2579 /* Any phi in the same block will have it's arguments in the
2580 same edge order, because of how we store phi nodes. */
2581 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2583 tree phi2op = vp2->phiargs[i];
2584 if (phi1op == VN_TOP || phi2op == VN_TOP)
2585 continue;
2586 if (!expressions_equal_p (phi1op, phi2op))
2587 return false;
2589 return true;
2591 return false;
2594 static vec<tree> shared_lookup_phiargs;
2596 /* Lookup PHI in the current hash table, and return the resulting
2597 value number if it exists in the hash table. Return NULL_TREE if
2598 it does not exist in the hash table. */
2600 static tree
2601 vn_phi_lookup (gimple phi)
2603 vn_phi_s **slot;
2604 struct vn_phi_s vp1;
2605 unsigned i;
2607 shared_lookup_phiargs.truncate (0);
2609 /* Canonicalize the SSA_NAME's to their value number. */
2610 for (i = 0; i < gimple_phi_num_args (phi); i++)
2612 tree def = PHI_ARG_DEF (phi, i);
2613 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2614 shared_lookup_phiargs.safe_push (def);
2616 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2617 vp1.phiargs = shared_lookup_phiargs;
2618 vp1.block = gimple_bb (phi);
2619 vp1.hashcode = vn_phi_compute_hash (&vp1);
2620 slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2621 NO_INSERT);
2622 if (!slot && current_info == optimistic_info)
2623 slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2624 NO_INSERT);
2625 if (!slot)
2626 return NULL_TREE;
2627 return (*slot)->result;
2630 /* Insert PHI into the current hash table with a value number of
2631 RESULT. */
2633 static vn_phi_t
2634 vn_phi_insert (gimple phi, tree result)
2636 vn_phi_s **slot;
2637 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2638 unsigned i;
2639 vec<tree> args = vNULL;
2641 /* Canonicalize the SSA_NAME's to their value number. */
2642 for (i = 0; i < gimple_phi_num_args (phi); i++)
2644 tree def = PHI_ARG_DEF (phi, i);
2645 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2646 args.safe_push (def);
2648 vp1->value_id = VN_INFO (result)->value_id;
2649 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2650 vp1->phiargs = args;
2651 vp1->block = gimple_bb (phi);
2652 vp1->result = result;
2653 vp1->hashcode = vn_phi_compute_hash (vp1);
2655 slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2657 /* Because we iterate over phi operations more than once, it's
2658 possible the slot might already exist here, hence no assert.*/
2659 *slot = vp1;
2660 return vp1;
2664 /* Print set of components in strongly connected component SCC to OUT. */
2666 static void
2667 print_scc (FILE *out, vec<tree> scc)
2669 tree var;
2670 unsigned int i;
2672 fprintf (out, "SCC consists of:");
2673 FOR_EACH_VEC_ELT (scc, i, var)
2675 fprintf (out, " ");
2676 print_generic_expr (out, var, 0);
2678 fprintf (out, "\n");
2681 /* Set the value number of FROM to TO, return true if it has changed
2682 as a result. */
2684 static inline bool
2685 set_ssa_val_to (tree from, tree to)
2687 tree currval = SSA_VAL (from);
2688 HOST_WIDE_INT toff, coff;
2690 /* The only thing we allow as value numbers are ssa_names
2691 and invariants. So assert that here. We don't allow VN_TOP
2692 as visiting a stmt should produce a value-number other than
2693 that.
2694 ??? Still VN_TOP can happen for unreachable code, so force
2695 it to varying in that case. Not all code is prepared to
2696 get VN_TOP on valueization. */
2697 if (to == VN_TOP)
2699 if (dump_file && (dump_flags & TDF_DETAILS))
2700 fprintf (dump_file, "Forcing value number to varying on "
2701 "receiving VN_TOP\n");
2702 to = from;
2705 gcc_assert (to != NULL_TREE
2706 && (TREE_CODE (to) == SSA_NAME
2707 || is_gimple_min_invariant (to)));
2709 if (from != to)
2711 if (currval == from)
2713 if (dump_file && (dump_flags & TDF_DETAILS))
2715 fprintf (dump_file, "Not changing value number of ");
2716 print_generic_expr (dump_file, from, 0);
2717 fprintf (dump_file, " from VARYING to ");
2718 print_generic_expr (dump_file, to, 0);
2719 fprintf (dump_file, "\n");
2721 return false;
2723 else if (TREE_CODE (to) == SSA_NAME
2724 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2725 to = from;
2728 if (dump_file && (dump_flags & TDF_DETAILS))
2730 fprintf (dump_file, "Setting value number of ");
2731 print_generic_expr (dump_file, from, 0);
2732 fprintf (dump_file, " to ");
2733 print_generic_expr (dump_file, to, 0);
2736 if (currval != to
2737 && !operand_equal_p (currval, to, 0)
2738 /* ??? For addresses involving volatile objects or types operand_equal_p
2739 does not reliably detect ADDR_EXPRs as equal. We know we are only
2740 getting invariant gimple addresses here, so can use
2741 get_addr_base_and_unit_offset to do this comparison. */
2742 && !(TREE_CODE (currval) == ADDR_EXPR
2743 && TREE_CODE (to) == ADDR_EXPR
2744 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2745 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2746 && coff == toff))
2748 VN_INFO (from)->valnum = to;
2749 if (dump_file && (dump_flags & TDF_DETAILS))
2750 fprintf (dump_file, " (changed)\n");
2751 return true;
2753 if (dump_file && (dump_flags & TDF_DETAILS))
2754 fprintf (dump_file, "\n");
2755 return false;
2758 /* Mark as processed all the definitions in the defining stmt of USE, or
2759 the USE itself. */
2761 static void
2762 mark_use_processed (tree use)
2764 ssa_op_iter iter;
2765 def_operand_p defp;
2766 gimple stmt = SSA_NAME_DEF_STMT (use);
2768 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2770 VN_INFO (use)->use_processed = true;
2771 return;
2774 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2776 tree def = DEF_FROM_PTR (defp);
2778 VN_INFO (def)->use_processed = true;
2782 /* Set all definitions in STMT to value number to themselves.
2783 Return true if a value number changed. */
2785 static bool
2786 defs_to_varying (gimple stmt)
2788 bool changed = false;
2789 ssa_op_iter iter;
2790 def_operand_p defp;
2792 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2794 tree def = DEF_FROM_PTR (defp);
2795 changed |= set_ssa_val_to (def, def);
2797 return changed;
2800 static bool expr_has_constants (tree expr);
2802 /* Visit a copy between LHS and RHS, return true if the value number
2803 changed. */
2805 static bool
2806 visit_copy (tree lhs, tree rhs)
2808 /* The copy may have a more interesting constant filled expression
2809 (we don't, since we know our RHS is just an SSA name). */
2810 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2811 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2813 /* And finally valueize. */
2814 rhs = SSA_VAL (rhs);
2816 return set_ssa_val_to (lhs, rhs);
2819 /* Visit a nary operator RHS, value number it, and return true if the
2820 value number of LHS has changed as a result. */
2822 static bool
2823 visit_nary_op (tree lhs, gimple stmt)
2825 bool changed = false;
2826 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2828 if (result)
2829 changed = set_ssa_val_to (lhs, result);
2830 else
2832 changed = set_ssa_val_to (lhs, lhs);
2833 vn_nary_op_insert_stmt (stmt, lhs);
2836 return changed;
2839 /* Visit a call STMT storing into LHS. Return true if the value number
2840 of the LHS has changed as a result. */
2842 static bool
2843 visit_reference_op_call (tree lhs, gimple stmt)
2845 bool changed = false;
2846 struct vn_reference_s vr1;
2847 vn_reference_t vnresult = NULL;
2848 tree vuse = gimple_vuse (stmt);
2849 tree vdef = gimple_vdef (stmt);
2851 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2852 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2853 lhs = NULL_TREE;
2855 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2856 vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2857 vr1.type = gimple_expr_type (stmt);
2858 vr1.set = 0;
2859 vr1.hashcode = vn_reference_compute_hash (&vr1);
2860 vn_reference_lookup_1 (&vr1, &vnresult);
2862 if (vnresult)
2864 if (vnresult->result_vdef && vdef)
2865 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2867 if (!vnresult->result && lhs)
2868 vnresult->result = lhs;
2870 if (vnresult->result && lhs)
2872 changed |= set_ssa_val_to (lhs, vnresult->result);
2874 if (VN_INFO (vnresult->result)->has_constants)
2875 VN_INFO (lhs)->has_constants = true;
2878 else
2880 vn_reference_s **slot;
2881 vn_reference_t vr2;
2882 if (vdef)
2883 changed |= set_ssa_val_to (vdef, vdef);
2884 if (lhs)
2885 changed |= set_ssa_val_to (lhs, lhs);
2886 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2887 vr2->vuse = vr1.vuse;
2888 vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2889 vr2->type = vr1.type;
2890 vr2->set = vr1.set;
2891 vr2->hashcode = vr1.hashcode;
2892 vr2->result = lhs;
2893 vr2->result_vdef = vdef;
2894 slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode,
2895 INSERT);
2896 if (*slot)
2897 free_reference (*slot);
2898 *slot = vr2;
2901 return changed;
2904 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2905 and return true if the value number of the LHS has changed as a result. */
2907 static bool
2908 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2910 bool changed = false;
2911 tree last_vuse;
2912 tree result;
2914 last_vuse = gimple_vuse (stmt);
2915 last_vuse_ptr = &last_vuse;
2916 result = vn_reference_lookup (op, gimple_vuse (stmt),
2917 default_vn_walk_kind, NULL);
2918 last_vuse_ptr = NULL;
2920 /* If we have a VCE, try looking up its operand as it might be stored in
2921 a different type. */
2922 if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2923 result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2924 default_vn_walk_kind, NULL);
2926 /* We handle type-punning through unions by value-numbering based
2927 on offset and size of the access. Be prepared to handle a
2928 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2929 if (result
2930 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2932 /* We will be setting the value number of lhs to the value number
2933 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2934 So first simplify and lookup this expression to see if it
2935 is already available. */
2936 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2937 if ((CONVERT_EXPR_P (val)
2938 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2939 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2941 tree tem = vn_get_expr_for (TREE_OPERAND (val, 0));
2942 if ((CONVERT_EXPR_P (tem)
2943 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2944 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2945 TREE_TYPE (val), tem)))
2946 val = tem;
2948 result = val;
2949 if (!is_gimple_min_invariant (val)
2950 && TREE_CODE (val) != SSA_NAME)
2951 result = vn_nary_op_lookup (val, NULL);
2952 /* If the expression is not yet available, value-number lhs to
2953 a new SSA_NAME we create. */
2954 if (!result)
2956 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
2957 "vntemp");
2958 /* Initialize value-number information properly. */
2959 VN_INFO_GET (result)->valnum = result;
2960 VN_INFO (result)->value_id = get_next_value_id ();
2961 VN_INFO (result)->expr = val;
2962 VN_INFO (result)->has_constants = expr_has_constants (val);
2963 VN_INFO (result)->needs_insertion = true;
2964 /* As all "inserted" statements are singleton SCCs, insert
2965 to the valid table. This is strictly needed to
2966 avoid re-generating new value SSA_NAMEs for the same
2967 expression during SCC iteration over and over (the
2968 optimistic table gets cleared after each iteration).
2969 We do not need to insert into the optimistic table, as
2970 lookups there will fall back to the valid table. */
2971 if (current_info == optimistic_info)
2973 current_info = valid_info;
2974 vn_nary_op_insert (val, result);
2975 current_info = optimistic_info;
2977 else
2978 vn_nary_op_insert (val, result);
2979 if (dump_file && (dump_flags & TDF_DETAILS))
2981 fprintf (dump_file, "Inserting name ");
2982 print_generic_expr (dump_file, result, 0);
2983 fprintf (dump_file, " for expression ");
2984 print_generic_expr (dump_file, val, 0);
2985 fprintf (dump_file, "\n");
2990 if (result)
2992 changed = set_ssa_val_to (lhs, result);
2993 if (TREE_CODE (result) == SSA_NAME
2994 && VN_INFO (result)->has_constants)
2996 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2997 VN_INFO (lhs)->has_constants = true;
3000 else
3002 changed = set_ssa_val_to (lhs, lhs);
3003 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3006 return changed;
3010 /* Visit a store to a reference operator LHS, part of STMT, value number it,
3011 and return true if the value number of the LHS has changed as a result. */
3013 static bool
3014 visit_reference_op_store (tree lhs, tree op, gimple stmt)
3016 bool changed = false;
3017 vn_reference_t vnresult = NULL;
3018 tree result, assign;
3019 bool resultsame = false;
3020 tree vuse = gimple_vuse (stmt);
3021 tree vdef = gimple_vdef (stmt);
3023 /* First we want to lookup using the *vuses* from the store and see
3024 if there the last store to this location with the same address
3025 had the same value.
3027 The vuses represent the memory state before the store. If the
3028 memory state, address, and value of the store is the same as the
3029 last store to this location, then this store will produce the
3030 same memory state as that store.
3032 In this case the vdef versions for this store are value numbered to those
3033 vuse versions, since they represent the same memory state after
3034 this store.
3036 Otherwise, the vdefs for the store are used when inserting into
3037 the table, since the store generates a new memory state. */
3039 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
3041 if (result)
3043 if (TREE_CODE (result) == SSA_NAME)
3044 result = SSA_VAL (result);
3045 if (TREE_CODE (op) == SSA_NAME)
3046 op = SSA_VAL (op);
3047 resultsame = expressions_equal_p (result, op);
3050 if (!result || !resultsame)
3052 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3053 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
3054 if (vnresult)
3056 VN_INFO (vdef)->use_processed = true;
3057 return set_ssa_val_to (vdef, vnresult->result_vdef);
3061 if (!result || !resultsame)
3063 if (dump_file && (dump_flags & TDF_DETAILS))
3065 fprintf (dump_file, "No store match\n");
3066 fprintf (dump_file, "Value numbering store ");
3067 print_generic_expr (dump_file, lhs, 0);
3068 fprintf (dump_file, " to ");
3069 print_generic_expr (dump_file, op, 0);
3070 fprintf (dump_file, "\n");
3072 /* Have to set value numbers before insert, since insert is
3073 going to valueize the references in-place. */
3074 if (vdef)
3076 changed |= set_ssa_val_to (vdef, vdef);
3079 /* Do not insert structure copies into the tables. */
3080 if (is_gimple_min_invariant (op)
3081 || is_gimple_reg (op))
3082 vn_reference_insert (lhs, op, vdef, NULL);
3084 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3085 vn_reference_insert (assign, lhs, vuse, vdef);
3087 else
3089 /* We had a match, so value number the vdef to have the value
3090 number of the vuse it came from. */
3092 if (dump_file && (dump_flags & TDF_DETAILS))
3093 fprintf (dump_file, "Store matched earlier value,"
3094 "value numbering store vdefs to matching vuses.\n");
3096 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3099 return changed;
3102 /* Visit and value number PHI, return true if the value number
3103 changed. */
3105 static bool
3106 visit_phi (gimple phi)
3108 bool changed = false;
3109 tree result;
3110 tree sameval = VN_TOP;
3111 bool allsame = true;
3113 /* TODO: We could check for this in init_sccvn, and replace this
3114 with a gcc_assert. */
3115 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3116 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3118 /* See if all non-TOP arguments have the same value. TOP is
3119 equivalent to everything, so we can ignore it. */
3120 edge_iterator ei;
3121 edge e;
3122 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3123 if (e->flags & EDGE_EXECUTABLE)
3125 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3127 if (TREE_CODE (def) == SSA_NAME)
3128 def = SSA_VAL (def);
3129 if (def == VN_TOP)
3130 continue;
3131 if (sameval == VN_TOP)
3133 sameval = def;
3135 else
3137 if (!expressions_equal_p (def, sameval))
3139 allsame = false;
3140 break;
3145 /* If all value numbered to the same value, the phi node has that
3146 value. */
3147 if (allsame)
3148 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3150 /* Otherwise, see if it is equivalent to a phi node in this block. */
3151 result = vn_phi_lookup (phi);
3152 if (result)
3153 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3154 else
3156 vn_phi_insert (phi, PHI_RESULT (phi));
3157 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3158 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3159 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3162 return changed;
3165 /* Return true if EXPR contains constants. */
3167 static bool
3168 expr_has_constants (tree expr)
3170 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3172 case tcc_unary:
3173 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3175 case tcc_binary:
3176 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3177 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3178 /* Constants inside reference ops are rarely interesting, but
3179 it can take a lot of looking to find them. */
3180 case tcc_reference:
3181 case tcc_declaration:
3182 return false;
3183 default:
3184 return is_gimple_min_invariant (expr);
3186 return false;
3189 /* Return true if STMT contains constants. */
3191 static bool
3192 stmt_has_constants (gimple stmt)
3194 tree tem;
3196 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3197 return false;
3199 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3201 case GIMPLE_TERNARY_RHS:
3202 tem = gimple_assign_rhs3 (stmt);
3203 if (TREE_CODE (tem) == SSA_NAME)
3204 tem = SSA_VAL (tem);
3205 if (is_gimple_min_invariant (tem))
3206 return true;
3207 /* Fallthru. */
3209 case GIMPLE_BINARY_RHS:
3210 tem = gimple_assign_rhs2 (stmt);
3211 if (TREE_CODE (tem) == SSA_NAME)
3212 tem = SSA_VAL (tem);
3213 if (is_gimple_min_invariant (tem))
3214 return true;
3215 /* Fallthru. */
3217 case GIMPLE_SINGLE_RHS:
3218 /* Constants inside reference ops are rarely interesting, but
3219 it can take a lot of looking to find them. */
3220 case GIMPLE_UNARY_RHS:
3221 tem = gimple_assign_rhs1 (stmt);
3222 if (TREE_CODE (tem) == SSA_NAME)
3223 tem = SSA_VAL (tem);
3224 return is_gimple_min_invariant (tem);
3226 default:
3227 gcc_unreachable ();
3229 return false;
3232 /* Simplify the binary expression RHS, and return the result if
3233 simplified. */
3235 static tree
3236 simplify_binary_expression (gimple stmt)
3238 tree result = NULL_TREE;
3239 tree op0 = gimple_assign_rhs1 (stmt);
3240 tree op1 = gimple_assign_rhs2 (stmt);
3241 enum tree_code code = gimple_assign_rhs_code (stmt);
3243 /* This will not catch every single case we could combine, but will
3244 catch those with constants. The goal here is to simultaneously
3245 combine constants between expressions, but avoid infinite
3246 expansion of expressions during simplification. */
3247 op0 = vn_valueize (op0);
3248 if (TREE_CODE (op0) == SSA_NAME
3249 && (VN_INFO (op0)->has_constants
3250 || TREE_CODE_CLASS (code) == tcc_comparison
3251 || code == COMPLEX_EXPR))
3252 op0 = vn_get_expr_for (op0);
3254 op1 = vn_valueize (op1);
3255 if (TREE_CODE (op1) == SSA_NAME
3256 && (VN_INFO (op1)->has_constants
3257 || code == COMPLEX_EXPR))
3258 op1 = vn_get_expr_for (op1);
3260 /* Pointer plus constant can be represented as invariant address.
3261 Do so to allow further propatation, see also tree forwprop. */
3262 if (code == POINTER_PLUS_EXPR
3263 && tree_fits_uhwi_p (op1)
3264 && TREE_CODE (op0) == ADDR_EXPR
3265 && is_gimple_min_invariant (op0))
3266 return build_invariant_address (TREE_TYPE (op0),
3267 TREE_OPERAND (op0, 0),
3268 tree_to_uhwi (op1));
3270 /* Avoid folding if nothing changed. */
3271 if (op0 == gimple_assign_rhs1 (stmt)
3272 && op1 == gimple_assign_rhs2 (stmt))
3273 return NULL_TREE;
3275 fold_defer_overflow_warnings ();
3277 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3278 if (result)
3279 STRIP_USELESS_TYPE_CONVERSION (result);
3281 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3282 stmt, 0);
3284 /* Make sure result is not a complex expression consisting
3285 of operators of operators (IE (a + b) + (a + c))
3286 Otherwise, we will end up with unbounded expressions if
3287 fold does anything at all. */
3288 if (result && valid_gimple_rhs_p (result))
3289 return result;
3291 return NULL_TREE;
3294 /* Simplify the unary expression RHS, and return the result if
3295 simplified. */
3297 static tree
3298 simplify_unary_expression (gimple stmt)
3300 tree result = NULL_TREE;
3301 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3302 enum tree_code code = gimple_assign_rhs_code (stmt);
3304 /* We handle some tcc_reference codes here that are all
3305 GIMPLE_ASSIGN_SINGLE codes. */
3306 if (code == REALPART_EXPR
3307 || code == IMAGPART_EXPR
3308 || code == VIEW_CONVERT_EXPR
3309 || code == BIT_FIELD_REF)
3310 op0 = TREE_OPERAND (op0, 0);
3312 orig_op0 = op0;
3313 op0 = vn_valueize (op0);
3314 if (TREE_CODE (op0) == SSA_NAME)
3316 if (VN_INFO (op0)->has_constants)
3317 op0 = vn_get_expr_for (op0);
3318 else if (CONVERT_EXPR_CODE_P (code)
3319 || code == REALPART_EXPR
3320 || code == IMAGPART_EXPR
3321 || code == VIEW_CONVERT_EXPR
3322 || code == BIT_FIELD_REF)
3324 /* We want to do tree-combining on conversion-like expressions.
3325 Make sure we feed only SSA_NAMEs or constants to fold though. */
3326 tree tem = vn_get_expr_for (op0);
3327 if (UNARY_CLASS_P (tem)
3328 || BINARY_CLASS_P (tem)
3329 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3330 || TREE_CODE (tem) == SSA_NAME
3331 || TREE_CODE (tem) == CONSTRUCTOR
3332 || is_gimple_min_invariant (tem))
3333 op0 = tem;
3337 /* Avoid folding if nothing changed, but remember the expression. */
3338 if (op0 == orig_op0)
3339 return NULL_TREE;
3341 if (code == BIT_FIELD_REF)
3343 tree rhs = gimple_assign_rhs1 (stmt);
3344 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3345 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3347 else
3348 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3349 if (result)
3351 STRIP_USELESS_TYPE_CONVERSION (result);
3352 if (valid_gimple_rhs_p (result))
3353 return result;
3356 return NULL_TREE;
3359 /* Try to simplify RHS using equivalences and constant folding. */
3361 static tree
3362 try_to_simplify (gimple stmt)
3364 enum tree_code code = gimple_assign_rhs_code (stmt);
3365 tree tem;
3367 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3368 in this case, there is no point in doing extra work. */
3369 if (code == SSA_NAME)
3370 return NULL_TREE;
3372 /* First try constant folding based on our current lattice. */
3373 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize);
3374 if (tem
3375 && (TREE_CODE (tem) == SSA_NAME
3376 || is_gimple_min_invariant (tem)))
3377 return tem;
3379 /* If that didn't work try combining multiple statements. */
3380 switch (TREE_CODE_CLASS (code))
3382 case tcc_reference:
3383 /* Fallthrough for some unary codes that can operate on registers. */
3384 if (!(code == REALPART_EXPR
3385 || code == IMAGPART_EXPR
3386 || code == VIEW_CONVERT_EXPR
3387 || code == BIT_FIELD_REF))
3388 break;
3389 /* We could do a little more with unary ops, if they expand
3390 into binary ops, but it's debatable whether it is worth it. */
3391 case tcc_unary:
3392 return simplify_unary_expression (stmt);
3394 case tcc_comparison:
3395 case tcc_binary:
3396 return simplify_binary_expression (stmt);
3398 default:
3399 break;
3402 return NULL_TREE;
3405 /* Visit and value number USE, return true if the value number
3406 changed. */
3408 static bool
3409 visit_use (tree use)
3411 bool changed = false;
3412 gimple stmt = SSA_NAME_DEF_STMT (use);
3414 mark_use_processed (use);
3416 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3417 if (dump_file && (dump_flags & TDF_DETAILS)
3418 && !SSA_NAME_IS_DEFAULT_DEF (use))
3420 fprintf (dump_file, "Value numbering ");
3421 print_generic_expr (dump_file, use, 0);
3422 fprintf (dump_file, " stmt = ");
3423 print_gimple_stmt (dump_file, stmt, 0, 0);
3426 /* Handle uninitialized uses. */
3427 if (SSA_NAME_IS_DEFAULT_DEF (use))
3428 changed = set_ssa_val_to (use, use);
3429 else
3431 if (gimple_code (stmt) == GIMPLE_PHI)
3432 changed = visit_phi (stmt);
3433 else if (gimple_has_volatile_ops (stmt))
3434 changed = defs_to_varying (stmt);
3435 else if (is_gimple_assign (stmt))
3437 enum tree_code code = gimple_assign_rhs_code (stmt);
3438 tree lhs = gimple_assign_lhs (stmt);
3439 tree rhs1 = gimple_assign_rhs1 (stmt);
3440 tree simplified;
3442 /* Shortcut for copies. Simplifying copies is pointless,
3443 since we copy the expression and value they represent. */
3444 if (code == SSA_NAME
3445 && TREE_CODE (lhs) == SSA_NAME)
3447 changed = visit_copy (lhs, rhs1);
3448 goto done;
3450 simplified = try_to_simplify (stmt);
3451 if (simplified)
3453 if (dump_file && (dump_flags & TDF_DETAILS))
3455 fprintf (dump_file, "RHS ");
3456 print_gimple_expr (dump_file, stmt, 0, 0);
3457 fprintf (dump_file, " simplified to ");
3458 print_generic_expr (dump_file, simplified, 0);
3459 if (TREE_CODE (lhs) == SSA_NAME)
3460 fprintf (dump_file, " has constants %d\n",
3461 expr_has_constants (simplified));
3462 else
3463 fprintf (dump_file, "\n");
3466 /* Setting value numbers to constants will occasionally
3467 screw up phi congruence because constants are not
3468 uniquely associated with a single ssa name that can be
3469 looked up. */
3470 if (simplified
3471 && is_gimple_min_invariant (simplified)
3472 && TREE_CODE (lhs) == SSA_NAME)
3474 VN_INFO (lhs)->expr = simplified;
3475 VN_INFO (lhs)->has_constants = true;
3476 changed = set_ssa_val_to (lhs, simplified);
3477 goto done;
3479 else if (simplified
3480 && TREE_CODE (simplified) == SSA_NAME
3481 && TREE_CODE (lhs) == SSA_NAME)
3483 changed = visit_copy (lhs, simplified);
3484 goto done;
3486 else if (simplified)
3488 if (TREE_CODE (lhs) == SSA_NAME)
3490 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3491 /* We have to unshare the expression or else
3492 valuizing may change the IL stream. */
3493 VN_INFO (lhs)->expr = unshare_expr (simplified);
3496 else if (stmt_has_constants (stmt)
3497 && TREE_CODE (lhs) == SSA_NAME)
3498 VN_INFO (lhs)->has_constants = true;
3499 else if (TREE_CODE (lhs) == SSA_NAME)
3501 /* We reset expr and constantness here because we may
3502 have been value numbering optimistically, and
3503 iterating. They may become non-constant in this case,
3504 even if they were optimistically constant. */
3506 VN_INFO (lhs)->has_constants = false;
3507 VN_INFO (lhs)->expr = NULL_TREE;
3510 if ((TREE_CODE (lhs) == SSA_NAME
3511 /* We can substitute SSA_NAMEs that are live over
3512 abnormal edges with their constant value. */
3513 && !(gimple_assign_copy_p (stmt)
3514 && is_gimple_min_invariant (rhs1))
3515 && !(simplified
3516 && is_gimple_min_invariant (simplified))
3517 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3518 /* Stores or copies from SSA_NAMEs that are live over
3519 abnormal edges are a problem. */
3520 || (code == SSA_NAME
3521 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3522 changed = defs_to_varying (stmt);
3523 else if (REFERENCE_CLASS_P (lhs)
3524 || DECL_P (lhs))
3525 changed = visit_reference_op_store (lhs, rhs1, stmt);
3526 else if (TREE_CODE (lhs) == SSA_NAME)
3528 if ((gimple_assign_copy_p (stmt)
3529 && is_gimple_min_invariant (rhs1))
3530 || (simplified
3531 && is_gimple_min_invariant (simplified)))
3533 VN_INFO (lhs)->has_constants = true;
3534 if (simplified)
3535 changed = set_ssa_val_to (lhs, simplified);
3536 else
3537 changed = set_ssa_val_to (lhs, rhs1);
3539 else
3541 /* First try to lookup the simplified expression. */
3542 if (simplified)
3544 enum gimple_rhs_class rhs_class;
3547 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3548 if ((rhs_class == GIMPLE_UNARY_RHS
3549 || rhs_class == GIMPLE_BINARY_RHS
3550 || rhs_class == GIMPLE_TERNARY_RHS)
3551 && valid_gimple_rhs_p (simplified))
3553 tree result = vn_nary_op_lookup (simplified, NULL);
3554 if (result)
3556 changed = set_ssa_val_to (lhs, result);
3557 goto done;
3562 /* Otherwise visit the original statement. */
3563 switch (vn_get_stmt_kind (stmt))
3565 case VN_NARY:
3566 changed = visit_nary_op (lhs, stmt);
3567 break;
3568 case VN_REFERENCE:
3569 changed = visit_reference_op_load (lhs, rhs1, stmt);
3570 break;
3571 default:
3572 changed = defs_to_varying (stmt);
3573 break;
3577 else
3578 changed = defs_to_varying (stmt);
3580 else if (is_gimple_call (stmt))
3582 tree lhs = gimple_call_lhs (stmt);
3583 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3585 /* Try constant folding based on our current lattice. */
3586 tree simplified = gimple_fold_stmt_to_constant_1 (stmt,
3587 vn_valueize);
3588 if (simplified)
3590 if (dump_file && (dump_flags & TDF_DETAILS))
3592 fprintf (dump_file, "call ");
3593 print_gimple_expr (dump_file, stmt, 0, 0);
3594 fprintf (dump_file, " simplified to ");
3595 print_generic_expr (dump_file, simplified, 0);
3596 if (TREE_CODE (lhs) == SSA_NAME)
3597 fprintf (dump_file, " has constants %d\n",
3598 expr_has_constants (simplified));
3599 else
3600 fprintf (dump_file, "\n");
3603 /* Setting value numbers to constants will occasionally
3604 screw up phi congruence because constants are not
3605 uniquely associated with a single ssa name that can be
3606 looked up. */
3607 if (simplified
3608 && is_gimple_min_invariant (simplified))
3610 VN_INFO (lhs)->expr = simplified;
3611 VN_INFO (lhs)->has_constants = true;
3612 changed = set_ssa_val_to (lhs, simplified);
3613 if (gimple_vdef (stmt))
3614 changed |= set_ssa_val_to (gimple_vdef (stmt),
3615 gimple_vuse (stmt));
3616 goto done;
3618 else if (simplified
3619 && TREE_CODE (simplified) == SSA_NAME)
3621 changed = visit_copy (lhs, simplified);
3622 if (gimple_vdef (stmt))
3623 changed |= set_ssa_val_to (gimple_vdef (stmt),
3624 gimple_vuse (stmt));
3625 goto done;
3627 else
3629 if (stmt_has_constants (stmt))
3630 VN_INFO (lhs)->has_constants = true;
3631 else
3633 /* We reset expr and constantness here because we may
3634 have been value numbering optimistically, and
3635 iterating. They may become non-constant in this case,
3636 even if they were optimistically constant. */
3637 VN_INFO (lhs)->has_constants = false;
3638 VN_INFO (lhs)->expr = NULL_TREE;
3641 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3643 changed = defs_to_varying (stmt);
3644 goto done;
3649 if (!gimple_call_internal_p (stmt)
3650 && (/* Calls to the same function with the same vuse
3651 and the same operands do not necessarily return the same
3652 value, unless they're pure or const. */
3653 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3654 /* If calls have a vdef, subsequent calls won't have
3655 the same incoming vuse. So, if 2 calls with vdef have the
3656 same vuse, we know they're not subsequent.
3657 We can value number 2 calls to the same function with the
3658 same vuse and the same operands which are not subsequent
3659 the same, because there is no code in the program that can
3660 compare the 2 values... */
3661 || (gimple_vdef (stmt)
3662 /* ... unless the call returns a pointer which does
3663 not alias with anything else. In which case the
3664 information that the values are distinct are encoded
3665 in the IL. */
3666 && !(gimple_call_return_flags (stmt) & ERF_NOALIAS))))
3667 changed = visit_reference_op_call (lhs, stmt);
3668 else
3669 changed = defs_to_varying (stmt);
3671 else
3672 changed = defs_to_varying (stmt);
3674 done:
3675 return changed;
3678 /* Compare two operands by reverse postorder index */
3680 static int
3681 compare_ops (const void *pa, const void *pb)
3683 const tree opa = *((const tree *)pa);
3684 const tree opb = *((const tree *)pb);
3685 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3686 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3687 basic_block bba;
3688 basic_block bbb;
3690 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3691 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3692 else if (gimple_nop_p (opstmta))
3693 return -1;
3694 else if (gimple_nop_p (opstmtb))
3695 return 1;
3697 bba = gimple_bb (opstmta);
3698 bbb = gimple_bb (opstmtb);
3700 if (!bba && !bbb)
3701 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3702 else if (!bba)
3703 return -1;
3704 else if (!bbb)
3705 return 1;
3707 if (bba == bbb)
3709 if (gimple_code (opstmta) == GIMPLE_PHI
3710 && gimple_code (opstmtb) == GIMPLE_PHI)
3711 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3712 else if (gimple_code (opstmta) == GIMPLE_PHI)
3713 return -1;
3714 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3715 return 1;
3716 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3717 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3718 else
3719 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3721 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3724 /* Sort an array containing members of a strongly connected component
3725 SCC so that the members are ordered by RPO number.
3726 This means that when the sort is complete, iterating through the
3727 array will give you the members in RPO order. */
3729 static void
3730 sort_scc (vec<tree> scc)
3732 scc.qsort (compare_ops);
3735 /* Insert the no longer used nary ONARY to the hash INFO. */
3737 static void
3738 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3740 size_t size = sizeof_vn_nary_op (onary->length);
3741 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3742 &info->nary_obstack);
3743 memcpy (nary, onary, size);
3744 vn_nary_op_insert_into (nary, info->nary, false);
3747 /* Insert the no longer used phi OPHI to the hash INFO. */
3749 static void
3750 copy_phi (vn_phi_t ophi, vn_tables_t info)
3752 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3753 vn_phi_s **slot;
3754 memcpy (phi, ophi, sizeof (*phi));
3755 ophi->phiargs.create (0);
3756 slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT);
3757 gcc_assert (!*slot);
3758 *slot = phi;
3761 /* Insert the no longer used reference OREF to the hash INFO. */
3763 static void
3764 copy_reference (vn_reference_t oref, vn_tables_t info)
3766 vn_reference_t ref;
3767 vn_reference_s **slot;
3768 ref = (vn_reference_t) pool_alloc (info->references_pool);
3769 memcpy (ref, oref, sizeof (*ref));
3770 oref->operands.create (0);
3771 slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT);
3772 if (*slot)
3773 free_reference (*slot);
3774 *slot = ref;
3777 /* Process a strongly connected component in the SSA graph. */
3779 static void
3780 process_scc (vec<tree> scc)
3782 tree var;
3783 unsigned int i;
3784 unsigned int iterations = 0;
3785 bool changed = true;
3786 vn_nary_op_iterator_type hin;
3787 vn_phi_iterator_type hip;
3788 vn_reference_iterator_type hir;
3789 vn_nary_op_t nary;
3790 vn_phi_t phi;
3791 vn_reference_t ref;
3793 /* If the SCC has a single member, just visit it. */
3794 if (scc.length () == 1)
3796 tree use = scc[0];
3797 if (VN_INFO (use)->use_processed)
3798 return;
3799 /* We need to make sure it doesn't form a cycle itself, which can
3800 happen for self-referential PHI nodes. In that case we would
3801 end up inserting an expression with VN_TOP operands into the
3802 valid table which makes us derive bogus equivalences later.
3803 The cheapest way to check this is to assume it for all PHI nodes. */
3804 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3805 /* Fallthru to iteration. */ ;
3806 else
3808 visit_use (use);
3809 return;
3813 if (dump_file && (dump_flags & TDF_DETAILS))
3814 print_scc (dump_file, scc);
3816 /* Iterate over the SCC with the optimistic table until it stops
3817 changing. */
3818 current_info = optimistic_info;
3819 while (changed)
3821 changed = false;
3822 iterations++;
3823 if (dump_file && (dump_flags & TDF_DETAILS))
3824 fprintf (dump_file, "Starting iteration %d\n", iterations);
3825 /* As we are value-numbering optimistically we have to
3826 clear the expression tables and the simplified expressions
3827 in each iteration until we converge. */
3828 optimistic_info->nary->empty ();
3829 optimistic_info->phis->empty ();
3830 optimistic_info->references->empty ();
3831 obstack_free (&optimistic_info->nary_obstack, NULL);
3832 gcc_obstack_init (&optimistic_info->nary_obstack);
3833 empty_alloc_pool (optimistic_info->phis_pool);
3834 empty_alloc_pool (optimistic_info->references_pool);
3835 FOR_EACH_VEC_ELT (scc, i, var)
3836 VN_INFO (var)->expr = NULL_TREE;
3837 FOR_EACH_VEC_ELT (scc, i, var)
3838 changed |= visit_use (var);
3841 if (dump_file && (dump_flags & TDF_DETAILS))
3842 fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations);
3843 statistics_histogram_event (cfun, "SCC iterations", iterations);
3845 /* Finally, copy the contents of the no longer used optimistic
3846 table to the valid table. */
3847 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin)
3848 copy_nary (nary, valid_info);
3849 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip)
3850 copy_phi (phi, valid_info);
3851 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references,
3852 ref, vn_reference_t, hir)
3853 copy_reference (ref, valid_info);
3855 current_info = valid_info;
3859 /* Pop the components of the found SCC for NAME off the SCC stack
3860 and process them. Returns true if all went well, false if
3861 we run into resource limits. */
3863 static bool
3864 extract_and_process_scc_for_name (tree name)
3866 auto_vec<tree> scc;
3867 tree x;
3869 /* Found an SCC, pop the components off the SCC stack and
3870 process them. */
3873 x = sccstack.pop ();
3875 VN_INFO (x)->on_sccstack = false;
3876 scc.safe_push (x);
3877 } while (x != name);
3879 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3880 if (scc.length ()
3881 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3883 if (dump_file)
3884 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3885 "SCC size %u exceeding %u\n", scc.length (),
3886 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3888 return false;
3891 if (scc.length () > 1)
3892 sort_scc (scc);
3894 process_scc (scc);
3896 return true;
3899 /* Depth first search on NAME to discover and process SCC's in the SSA
3900 graph.
3901 Execution of this algorithm relies on the fact that the SCC's are
3902 popped off the stack in topological order.
3903 Returns true if successful, false if we stopped processing SCC's due
3904 to resource constraints. */
3906 static bool
3907 DFS (tree name)
3909 vec<ssa_op_iter> itervec = vNULL;
3910 vec<tree> namevec = vNULL;
3911 use_operand_p usep = NULL;
3912 gimple defstmt;
3913 tree use;
3914 ssa_op_iter iter;
3916 start_over:
3917 /* SCC info */
3918 VN_INFO (name)->dfsnum = next_dfs_num++;
3919 VN_INFO (name)->visited = true;
3920 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3922 sccstack.safe_push (name);
3923 VN_INFO (name)->on_sccstack = true;
3924 defstmt = SSA_NAME_DEF_STMT (name);
3926 /* Recursively DFS on our operands, looking for SCC's. */
3927 if (!gimple_nop_p (defstmt))
3929 /* Push a new iterator. */
3930 if (gimple_code (defstmt) == GIMPLE_PHI)
3931 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3932 else
3933 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3935 else
3936 clear_and_done_ssa_iter (&iter);
3938 while (1)
3940 /* If we are done processing uses of a name, go up the stack
3941 of iterators and process SCCs as we found them. */
3942 if (op_iter_done (&iter))
3944 /* See if we found an SCC. */
3945 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3946 if (!extract_and_process_scc_for_name (name))
3948 namevec.release ();
3949 itervec.release ();
3950 return false;
3953 /* Check if we are done. */
3954 if (namevec.is_empty ())
3956 namevec.release ();
3957 itervec.release ();
3958 return true;
3961 /* Restore the last use walker and continue walking there. */
3962 use = name;
3963 name = namevec.pop ();
3964 memcpy (&iter, &itervec.last (),
3965 sizeof (ssa_op_iter));
3966 itervec.pop ();
3967 goto continue_walking;
3970 use = USE_FROM_PTR (usep);
3972 /* Since we handle phi nodes, we will sometimes get
3973 invariants in the use expression. */
3974 if (TREE_CODE (use) == SSA_NAME)
3976 if (! (VN_INFO (use)->visited))
3978 /* Recurse by pushing the current use walking state on
3979 the stack and starting over. */
3980 itervec.safe_push (iter);
3981 namevec.safe_push (name);
3982 name = use;
3983 goto start_over;
3985 continue_walking:
3986 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3987 VN_INFO (use)->low);
3989 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3990 && VN_INFO (use)->on_sccstack)
3992 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3993 VN_INFO (name)->low);
3997 usep = op_iter_next_use (&iter);
4001 /* Allocate a value number table. */
4003 static void
4004 allocate_vn_table (vn_tables_t table)
4006 table->phis = new vn_phi_table_type (23);
4007 table->nary = new vn_nary_op_table_type (23);
4008 table->references = new vn_reference_table_type (23);
4010 gcc_obstack_init (&table->nary_obstack);
4011 table->phis_pool = create_alloc_pool ("VN phis",
4012 sizeof (struct vn_phi_s),
4013 30);
4014 table->references_pool = create_alloc_pool ("VN references",
4015 sizeof (struct vn_reference_s),
4016 30);
4019 /* Free a value number table. */
4021 static void
4022 free_vn_table (vn_tables_t table)
4024 delete table->phis;
4025 table->phis = NULL;
4026 delete table->nary;
4027 table->nary = NULL;
4028 delete table->references;
4029 table->references = NULL;
4030 obstack_free (&table->nary_obstack, NULL);
4031 free_alloc_pool (table->phis_pool);
4032 free_alloc_pool (table->references_pool);
4035 static void
4036 init_scc_vn (void)
4038 size_t i;
4039 int j;
4040 int *rpo_numbers_temp;
4042 calculate_dominance_info (CDI_DOMINATORS);
4043 sccstack.create (0);
4044 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
4046 constant_value_ids = BITMAP_ALLOC (NULL);
4048 next_dfs_num = 1;
4049 next_value_id = 1;
4051 vn_ssa_aux_table.create (num_ssa_names + 1);
4052 /* VEC_alloc doesn't actually grow it to the right size, it just
4053 preallocates the space to do so. */
4054 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
4055 gcc_obstack_init (&vn_ssa_aux_obstack);
4057 shared_lookup_phiargs.create (0);
4058 shared_lookup_references.create (0);
4059 rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun));
4060 rpo_numbers_temp =
4061 XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4062 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
4064 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
4065 the i'th block in RPO order is bb. We want to map bb's to RPO
4066 numbers, so we need to rearrange this array. */
4067 for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++)
4068 rpo_numbers[rpo_numbers_temp[j]] = j;
4070 XDELETE (rpo_numbers_temp);
4072 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
4074 /* Create the VN_INFO structures, and initialize value numbers to
4075 TOP. */
4076 for (i = 0; i < num_ssa_names; i++)
4078 tree name = ssa_name (i);
4079 if (name)
4081 VN_INFO_GET (name)->valnum = VN_TOP;
4082 VN_INFO (name)->expr = NULL_TREE;
4083 VN_INFO (name)->value_id = 0;
4087 renumber_gimple_stmt_uids ();
4089 /* Create the valid and optimistic value numbering tables. */
4090 valid_info = XCNEW (struct vn_tables_s);
4091 allocate_vn_table (valid_info);
4092 optimistic_info = XCNEW (struct vn_tables_s);
4093 allocate_vn_table (optimistic_info);
4096 void
4097 free_scc_vn (void)
4099 size_t i;
4101 delete constant_to_value_id;
4102 constant_to_value_id = NULL;
4103 BITMAP_FREE (constant_value_ids);
4104 shared_lookup_phiargs.release ();
4105 shared_lookup_references.release ();
4106 XDELETEVEC (rpo_numbers);
4108 for (i = 0; i < num_ssa_names; i++)
4110 tree name = ssa_name (i);
4111 if (name
4112 && VN_INFO (name)->needs_insertion)
4113 release_ssa_name (name);
4115 obstack_free (&vn_ssa_aux_obstack, NULL);
4116 vn_ssa_aux_table.release ();
4118 sccstack.release ();
4119 free_vn_table (valid_info);
4120 XDELETE (valid_info);
4121 free_vn_table (optimistic_info);
4122 XDELETE (optimistic_info);
4125 /* Set *ID according to RESULT. */
4127 static void
4128 set_value_id_for_result (tree result, unsigned int *id)
4130 if (result && TREE_CODE (result) == SSA_NAME)
4131 *id = VN_INFO (result)->value_id;
4132 else if (result && is_gimple_min_invariant (result))
4133 *id = get_or_alloc_constant_value_id (result);
4134 else
4135 *id = get_next_value_id ();
4138 /* Set the value ids in the valid hash tables. */
4140 static void
4141 set_hashtable_value_ids (void)
4143 vn_nary_op_iterator_type hin;
4144 vn_phi_iterator_type hip;
4145 vn_reference_iterator_type hir;
4146 vn_nary_op_t vno;
4147 vn_reference_t vr;
4148 vn_phi_t vp;
4150 /* Now set the value ids of the things we had put in the hash
4151 table. */
4153 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4154 set_value_id_for_result (vno->result, &vno->value_id);
4156 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4157 set_value_id_for_result (vp->result, &vp->value_id);
4159 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4160 hir)
4161 set_value_id_for_result (vr->result, &vr->value_id);
4164 class cond_dom_walker : public dom_walker
4166 public:
4167 cond_dom_walker () : dom_walker (CDI_DOMINATORS), fail (false) {}
4169 virtual void before_dom_children (basic_block);
4171 bool fail;
4174 void
4175 cond_dom_walker::before_dom_children (basic_block bb)
4177 edge e;
4178 edge_iterator ei;
4180 if (fail)
4181 return;
4183 /* If any of the predecessor edges that do not come from blocks dominated
4184 by us are still marked as possibly executable consider this block
4185 reachable. */
4186 bool reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (cfun);
4187 FOR_EACH_EDGE (e, ei, bb->preds)
4188 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
4189 reachable |= (e->flags & EDGE_EXECUTABLE);
4191 /* If the block is not reachable all outgoing edges are not
4192 executable. */
4193 if (!reachable)
4195 if (dump_file && (dump_flags & TDF_DETAILS))
4196 fprintf (dump_file, "Marking all outgoing edges of unreachable "
4197 "BB %d as not executable\n", bb->index);
4199 FOR_EACH_EDGE (e, ei, bb->succs)
4200 e->flags &= ~EDGE_EXECUTABLE;
4201 return;
4204 gimple stmt = last_stmt (bb);
4205 if (!stmt)
4206 return;
4208 enum gimple_code code = gimple_code (stmt);
4209 if (code != GIMPLE_COND
4210 && code != GIMPLE_SWITCH
4211 && code != GIMPLE_GOTO)
4212 return;
4214 if (dump_file && (dump_flags & TDF_DETAILS))
4216 fprintf (dump_file, "Value-numbering operands of stmt ending BB %d: ",
4217 bb->index);
4218 print_gimple_stmt (dump_file, stmt, 0, 0);
4221 /* Value-number the last stmts SSA uses. */
4222 ssa_op_iter i;
4223 tree op;
4224 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4225 if (VN_INFO (op)->visited == false
4226 && !DFS (op))
4228 fail = true;
4229 return;
4232 /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges
4233 if value-numbering can prove they are not reachable. Handling
4234 computed gotos is also possible. */
4235 tree val;
4236 switch (code)
4238 case GIMPLE_COND:
4240 tree lhs = gimple_cond_lhs (stmt);
4241 tree rhs = gimple_cond_rhs (stmt);
4242 /* Work hard in computing the condition and take into account
4243 the valueization of the defining stmt. */
4244 if (TREE_CODE (lhs) == SSA_NAME)
4245 lhs = vn_get_expr_for (lhs);
4246 if (TREE_CODE (rhs) == SSA_NAME)
4247 rhs = vn_get_expr_for (rhs);
4248 val = fold_binary (gimple_cond_code (stmt),
4249 boolean_type_node, lhs, rhs);
4250 break;
4252 case GIMPLE_SWITCH:
4253 val = gimple_switch_index (stmt);
4254 break;
4255 case GIMPLE_GOTO:
4256 val = gimple_goto_dest (stmt);
4257 break;
4258 default:
4259 gcc_unreachable ();
4261 if (!val)
4262 return;
4264 edge taken = find_taken_edge (bb, vn_valueize (val));
4265 if (!taken)
4266 return;
4268 if (dump_file && (dump_flags & TDF_DETAILS))
4269 fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as "
4270 "not executable\n", bb->index, bb->index, taken->dest->index);
4272 FOR_EACH_EDGE (e, ei, bb->succs)
4273 if (e != taken)
4274 e->flags &= ~EDGE_EXECUTABLE;
4277 /* Do SCCVN. Returns true if it finished, false if we bailed out
4278 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4279 how we use the alias oracle walking during the VN process. */
4281 bool
4282 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4284 basic_block bb;
4285 size_t i;
4286 tree param;
4288 default_vn_walk_kind = default_vn_walk_kind_;
4290 init_scc_vn ();
4291 current_info = valid_info;
4293 for (param = DECL_ARGUMENTS (current_function_decl);
4294 param;
4295 param = DECL_CHAIN (param))
4297 tree def = ssa_default_def (cfun, param);
4298 if (def)
4300 VN_INFO (def)->visited = true;
4301 VN_INFO (def)->valnum = def;
4305 /* Mark all edges as possibly executable. */
4306 FOR_ALL_BB_FN (bb, cfun)
4308 edge_iterator ei;
4309 edge e;
4310 FOR_EACH_EDGE (e, ei, bb->succs)
4311 e->flags |= EDGE_EXECUTABLE;
4314 /* Walk all blocks in dominator order, value-numbering the last stmts
4315 SSA uses and decide whether outgoing edges are not executable. */
4316 cond_dom_walker walker;
4317 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4318 if (walker.fail)
4320 free_scc_vn ();
4321 return false;
4324 /* Value-number remaining SSA names. */
4325 for (i = 1; i < num_ssa_names; ++i)
4327 tree name = ssa_name (i);
4328 if (name
4329 && VN_INFO (name)->visited == false
4330 && !has_zero_uses (name))
4331 if (!DFS (name))
4333 free_scc_vn ();
4334 return false;
4338 /* Initialize the value ids. */
4340 for (i = 1; i < num_ssa_names; ++i)
4342 tree name = ssa_name (i);
4343 vn_ssa_aux_t info;
4344 if (!name)
4345 continue;
4346 info = VN_INFO (name);
4347 if (info->valnum == name
4348 || info->valnum == VN_TOP)
4349 info->value_id = get_next_value_id ();
4350 else if (is_gimple_min_invariant (info->valnum))
4351 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4354 /* Propagate. */
4355 for (i = 1; i < num_ssa_names; ++i)
4357 tree name = ssa_name (i);
4358 vn_ssa_aux_t info;
4359 if (!name)
4360 continue;
4361 info = VN_INFO (name);
4362 if (TREE_CODE (info->valnum) == SSA_NAME
4363 && info->valnum != name
4364 && info->value_id != VN_INFO (info->valnum)->value_id)
4365 info->value_id = VN_INFO (info->valnum)->value_id;
4368 set_hashtable_value_ids ();
4370 if (dump_file && (dump_flags & TDF_DETAILS))
4372 fprintf (dump_file, "Value numbers:\n");
4373 for (i = 0; i < num_ssa_names; i++)
4375 tree name = ssa_name (i);
4376 if (name
4377 && VN_INFO (name)->visited
4378 && SSA_VAL (name) != name)
4380 print_generic_expr (dump_file, name, 0);
4381 fprintf (dump_file, " = ");
4382 print_generic_expr (dump_file, SSA_VAL (name), 0);
4383 fprintf (dump_file, "\n");
4388 return true;
4391 /* Return the maximum value id we have ever seen. */
4393 unsigned int
4394 get_max_value_id (void)
4396 return next_value_id;
4399 /* Return the next unique value id. */
4401 unsigned int
4402 get_next_value_id (void)
4404 return next_value_id++;
4408 /* Compare two expressions E1 and E2 and return true if they are equal. */
4410 bool
4411 expressions_equal_p (tree e1, tree e2)
4413 /* The obvious case. */
4414 if (e1 == e2)
4415 return true;
4417 /* If only one of them is null, they cannot be equal. */
4418 if (!e1 || !e2)
4419 return false;
4421 /* Now perform the actual comparison. */
4422 if (TREE_CODE (e1) == TREE_CODE (e2)
4423 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4424 return true;
4426 return false;
4430 /* Return true if the nary operation NARY may trap. This is a copy
4431 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4433 bool
4434 vn_nary_may_trap (vn_nary_op_t nary)
4436 tree type;
4437 tree rhs2 = NULL_TREE;
4438 bool honor_nans = false;
4439 bool honor_snans = false;
4440 bool fp_operation = false;
4441 bool honor_trapv = false;
4442 bool handled, ret;
4443 unsigned i;
4445 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4446 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4447 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4449 type = nary->type;
4450 fp_operation = FLOAT_TYPE_P (type);
4451 if (fp_operation)
4453 honor_nans = flag_trapping_math && !flag_finite_math_only;
4454 honor_snans = flag_signaling_nans != 0;
4456 else if (INTEGRAL_TYPE_P (type)
4457 && TYPE_OVERFLOW_TRAPS (type))
4458 honor_trapv = true;
4460 if (nary->length >= 2)
4461 rhs2 = nary->op[1];
4462 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4463 honor_trapv,
4464 honor_nans, honor_snans, rhs2,
4465 &handled);
4466 if (handled
4467 && ret)
4468 return true;
4470 for (i = 0; i < nary->length; ++i)
4471 if (tree_could_trap_p (nary->op[i]))
4472 return true;
4474 return false;