Concretize gimple_switch_index and gimple_switch_index_ptr
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob48fb2dd9b74a7f4e56a9c9b84b18d38bd36e6609
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 static 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 static 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 static void
1122 copy_reference_ops_from_call (gimple_call 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 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1164 *I_P to point to the last element of the replacement. */
1165 void
1166 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1167 unsigned int *i_p)
1169 unsigned int i = *i_p;
1170 vn_reference_op_t op = &(*ops)[i];
1171 vn_reference_op_t mem_op = &(*ops)[i - 1];
1172 tree addr_base;
1173 HOST_WIDE_INT addr_offset = 0;
1175 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1176 from .foo.bar to the preceding MEM_REF offset and replace the
1177 address with &OBJ. */
1178 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1179 &addr_offset);
1180 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1181 if (addr_base != TREE_OPERAND (op->op0, 0))
1183 offset_int off = offset_int::from (mem_op->op0, SIGNED);
1184 off += addr_offset;
1185 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1186 op->op0 = build_fold_addr_expr (addr_base);
1187 if (tree_fits_shwi_p (mem_op->op0))
1188 mem_op->off = tree_to_shwi (mem_op->op0);
1189 else
1190 mem_op->off = -1;
1194 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1195 *I_P to point to the last element of the replacement. */
1196 static void
1197 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1198 unsigned int *i_p)
1200 unsigned int i = *i_p;
1201 vn_reference_op_t op = &(*ops)[i];
1202 vn_reference_op_t mem_op = &(*ops)[i - 1];
1203 gimple def_stmt;
1204 enum tree_code code;
1205 offset_int off;
1207 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1208 if (!is_gimple_assign (def_stmt))
1209 return;
1211 code = gimple_assign_rhs_code (def_stmt);
1212 if (code != ADDR_EXPR
1213 && code != POINTER_PLUS_EXPR)
1214 return;
1216 off = offset_int::from (mem_op->op0, SIGNED);
1218 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1219 from .foo.bar to the preceding MEM_REF offset and replace the
1220 address with &OBJ. */
1221 if (code == ADDR_EXPR)
1223 tree addr, addr_base;
1224 HOST_WIDE_INT addr_offset;
1226 addr = gimple_assign_rhs1 (def_stmt);
1227 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1228 &addr_offset);
1229 if (!addr_base
1230 || TREE_CODE (addr_base) != MEM_REF)
1231 return;
1233 off += addr_offset;
1234 off += mem_ref_offset (addr_base);
1235 op->op0 = TREE_OPERAND (addr_base, 0);
1237 else
1239 tree ptr, ptroff;
1240 ptr = gimple_assign_rhs1 (def_stmt);
1241 ptroff = gimple_assign_rhs2 (def_stmt);
1242 if (TREE_CODE (ptr) != SSA_NAME
1243 || TREE_CODE (ptroff) != INTEGER_CST)
1244 return;
1246 off += wi::to_offset (ptroff);
1247 op->op0 = ptr;
1250 mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off);
1251 if (tree_fits_shwi_p (mem_op->op0))
1252 mem_op->off = tree_to_shwi (mem_op->op0);
1253 else
1254 mem_op->off = -1;
1255 if (TREE_CODE (op->op0) == SSA_NAME)
1256 op->op0 = SSA_VAL (op->op0);
1257 if (TREE_CODE (op->op0) != SSA_NAME)
1258 op->opcode = TREE_CODE (op->op0);
1260 /* And recurse. */
1261 if (TREE_CODE (op->op0) == SSA_NAME)
1262 vn_reference_maybe_forwprop_address (ops, i_p);
1263 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1264 vn_reference_fold_indirect (ops, i_p);
1267 /* Optimize the reference REF to a constant if possible or return
1268 NULL_TREE if not. */
1270 tree
1271 fully_constant_vn_reference_p (vn_reference_t ref)
1273 vec<vn_reference_op_s> operands = ref->operands;
1274 vn_reference_op_t op;
1276 /* Try to simplify the translated expression if it is
1277 a call to a builtin function with at most two arguments. */
1278 op = &operands[0];
1279 if (op->opcode == CALL_EXPR
1280 && TREE_CODE (op->op0) == ADDR_EXPR
1281 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1282 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1283 && operands.length () >= 2
1284 && operands.length () <= 3)
1286 vn_reference_op_t arg0, arg1 = NULL;
1287 bool anyconst = false;
1288 arg0 = &operands[1];
1289 if (operands.length () > 2)
1290 arg1 = &operands[2];
1291 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1292 || (arg0->opcode == ADDR_EXPR
1293 && is_gimple_min_invariant (arg0->op0)))
1294 anyconst = true;
1295 if (arg1
1296 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1297 || (arg1->opcode == ADDR_EXPR
1298 && is_gimple_min_invariant (arg1->op0))))
1299 anyconst = true;
1300 if (anyconst)
1302 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1303 arg1 ? 2 : 1,
1304 arg0->op0,
1305 arg1 ? arg1->op0 : NULL);
1306 if (folded
1307 && TREE_CODE (folded) == NOP_EXPR)
1308 folded = TREE_OPERAND (folded, 0);
1309 if (folded
1310 && is_gimple_min_invariant (folded))
1311 return folded;
1315 /* Simplify reads from constant strings. */
1316 else if (op->opcode == ARRAY_REF
1317 && TREE_CODE (op->op0) == INTEGER_CST
1318 && integer_zerop (op->op1)
1319 && operands.length () == 2)
1321 vn_reference_op_t arg0;
1322 arg0 = &operands[1];
1323 if (arg0->opcode == STRING_CST
1324 && (TYPE_MODE (op->type)
1325 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1326 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1327 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1328 && tree_int_cst_sgn (op->op0) >= 0
1329 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1330 return build_int_cst_type (op->type,
1331 (TREE_STRING_POINTER (arg0->op0)
1332 [TREE_INT_CST_LOW (op->op0)]));
1335 return NULL_TREE;
1338 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1339 structures into their value numbers. This is done in-place, and
1340 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1341 whether any operands were valueized. */
1343 static vec<vn_reference_op_s>
1344 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1346 vn_reference_op_t vro;
1347 unsigned int i;
1349 *valueized_anything = false;
1351 FOR_EACH_VEC_ELT (orig, i, vro)
1353 if (vro->opcode == SSA_NAME
1354 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1356 tree tem = SSA_VAL (vro->op0);
1357 if (tem != vro->op0)
1359 *valueized_anything = true;
1360 vro->op0 = tem;
1362 /* If it transforms from an SSA_NAME to a constant, update
1363 the opcode. */
1364 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1365 vro->opcode = TREE_CODE (vro->op0);
1367 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1369 tree tem = SSA_VAL (vro->op1);
1370 if (tem != vro->op1)
1372 *valueized_anything = true;
1373 vro->op1 = tem;
1376 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1378 tree tem = SSA_VAL (vro->op2);
1379 if (tem != vro->op2)
1381 *valueized_anything = true;
1382 vro->op2 = tem;
1385 /* If it transforms from an SSA_NAME to an address, fold with
1386 a preceding indirect reference. */
1387 if (i > 0
1388 && vro->op0
1389 && TREE_CODE (vro->op0) == ADDR_EXPR
1390 && orig[i - 1].opcode == MEM_REF)
1391 vn_reference_fold_indirect (&orig, &i);
1392 else if (i > 0
1393 && vro->opcode == SSA_NAME
1394 && orig[i - 1].opcode == MEM_REF)
1395 vn_reference_maybe_forwprop_address (&orig, &i);
1396 /* If it transforms a non-constant ARRAY_REF into a constant
1397 one, adjust the constant offset. */
1398 else if (vro->opcode == ARRAY_REF
1399 && vro->off == -1
1400 && TREE_CODE (vro->op0) == INTEGER_CST
1401 && TREE_CODE (vro->op1) == INTEGER_CST
1402 && TREE_CODE (vro->op2) == INTEGER_CST)
1404 offset_int off = ((wi::to_offset (vro->op0)
1405 - wi::to_offset (vro->op1))
1406 * wi::to_offset (vro->op2));
1407 if (wi::fits_shwi_p (off))
1408 vro->off = off.to_shwi ();
1412 return orig;
1415 static vec<vn_reference_op_s>
1416 valueize_refs (vec<vn_reference_op_s> orig)
1418 bool tem;
1419 return valueize_refs_1 (orig, &tem);
1422 static vec<vn_reference_op_s> shared_lookup_references;
1424 /* Create a vector of vn_reference_op_s structures from REF, a
1425 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1426 this function. *VALUEIZED_ANYTHING will specify whether any
1427 operands were valueized. */
1429 static vec<vn_reference_op_s>
1430 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1432 if (!ref)
1433 return vNULL;
1434 shared_lookup_references.truncate (0);
1435 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1436 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1437 valueized_anything);
1438 return shared_lookup_references;
1441 /* Create a vector of vn_reference_op_s structures from CALL, a
1442 call statement. The vector is shared among all callers of
1443 this function. */
1445 static vec<vn_reference_op_s>
1446 valueize_shared_reference_ops_from_call (gimple_call call)
1448 if (!call)
1449 return vNULL;
1450 shared_lookup_references.truncate (0);
1451 copy_reference_ops_from_call (call, &shared_lookup_references);
1452 shared_lookup_references = valueize_refs (shared_lookup_references);
1453 return shared_lookup_references;
1456 /* Lookup a SCCVN reference operation VR in the current hash table.
1457 Returns the resulting value number if it exists in the hash table,
1458 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1459 vn_reference_t stored in the hashtable if something is found. */
1461 static tree
1462 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1464 vn_reference_s **slot;
1465 hashval_t hash;
1467 hash = vr->hashcode;
1468 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1469 if (!slot && current_info == optimistic_info)
1470 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1471 if (slot)
1473 if (vnresult)
1474 *vnresult = (vn_reference_t)*slot;
1475 return ((vn_reference_t)*slot)->result;
1478 return NULL_TREE;
1481 static tree *last_vuse_ptr;
1482 static vn_lookup_kind vn_walk_kind;
1483 static vn_lookup_kind default_vn_walk_kind;
1485 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1486 with the current VUSE and performs the expression lookup. */
1488 static void *
1489 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1490 unsigned int cnt, void *vr_)
1492 vn_reference_t vr = (vn_reference_t)vr_;
1493 vn_reference_s **slot;
1494 hashval_t hash;
1496 /* This bounds the stmt walks we perform on reference lookups
1497 to O(1) instead of O(N) where N is the number of dominating
1498 stores. */
1499 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1500 return (void *)-1;
1502 if (last_vuse_ptr)
1503 *last_vuse_ptr = vuse;
1505 /* Fixup vuse and hash. */
1506 if (vr->vuse)
1507 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1508 vr->vuse = vuse_ssa_val (vuse);
1509 if (vr->vuse)
1510 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1512 hash = vr->hashcode;
1513 slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1514 if (!slot && current_info == optimistic_info)
1515 slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT);
1516 if (slot)
1517 return *slot;
1519 return NULL;
1522 /* Lookup an existing or insert a new vn_reference entry into the
1523 value table for the VUSE, SET, TYPE, OPERANDS reference which
1524 has the value VALUE which is either a constant or an SSA name. */
1526 static vn_reference_t
1527 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1528 alias_set_type set,
1529 tree type,
1530 vec<vn_reference_op_s,
1531 va_heap> operands,
1532 tree value)
1534 struct vn_reference_s vr1;
1535 vn_reference_t result;
1536 unsigned value_id;
1537 vr1.vuse = vuse;
1538 vr1.operands = operands;
1539 vr1.type = type;
1540 vr1.set = set;
1541 vr1.hashcode = vn_reference_compute_hash (&vr1);
1542 if (vn_reference_lookup_1 (&vr1, &result))
1543 return result;
1544 if (TREE_CODE (value) == SSA_NAME)
1545 value_id = VN_INFO (value)->value_id;
1546 else
1547 value_id = get_or_alloc_constant_value_id (value);
1548 return vn_reference_insert_pieces (vuse, set, type,
1549 operands.copy (), value, value_id);
1552 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1553 from the statement defining VUSE and if not successful tries to
1554 translate *REFP and VR_ through an aggregate copy at the definition
1555 of VUSE. */
1557 static void *
1558 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_,
1559 bool disambiguate_only)
1561 vn_reference_t vr = (vn_reference_t)vr_;
1562 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1563 tree base;
1564 HOST_WIDE_INT offset, maxsize;
1565 static vec<vn_reference_op_s>
1566 lhs_ops = vNULL;
1567 ao_ref lhs_ref;
1568 bool lhs_ref_ok = false;
1570 /* First try to disambiguate after value-replacing in the definitions LHS. */
1571 if (is_gimple_assign (def_stmt))
1573 vec<vn_reference_op_s> tem;
1574 tree lhs = gimple_assign_lhs (def_stmt);
1575 bool valueized_anything = false;
1576 /* Avoid re-allocation overhead. */
1577 lhs_ops.truncate (0);
1578 copy_reference_ops_from_ref (lhs, &lhs_ops);
1579 tem = lhs_ops;
1580 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1581 gcc_assert (lhs_ops == tem);
1582 if (valueized_anything)
1584 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1585 get_alias_set (lhs),
1586 TREE_TYPE (lhs), lhs_ops);
1587 if (lhs_ref_ok
1588 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1589 return NULL;
1591 else
1593 ao_ref_init (&lhs_ref, lhs);
1594 lhs_ref_ok = true;
1597 else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL)
1598 && gimple_call_num_args (def_stmt) <= 4)
1600 /* For builtin calls valueize its arguments and call the
1601 alias oracle again. Valueization may improve points-to
1602 info of pointers and constify size and position arguments.
1603 Originally this was motivated by PR61034 which has
1604 conditional calls to free falsely clobbering ref because
1605 of imprecise points-to info of the argument. */
1606 tree oldargs[4];
1607 bool valueized_anything = false;
1608 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1610 oldargs[i] = gimple_call_arg (def_stmt, i);
1611 if (TREE_CODE (oldargs[i]) == SSA_NAME
1612 && VN_INFO (oldargs[i])->valnum != oldargs[i])
1614 gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum);
1615 valueized_anything = true;
1618 if (valueized_anything)
1620 bool res = call_may_clobber_ref_p_1 (as_a <gimple_call> (def_stmt),
1621 ref);
1622 for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i)
1623 gimple_call_set_arg (def_stmt, i, oldargs[i]);
1624 if (!res)
1625 return NULL;
1629 if (disambiguate_only)
1630 return (void *)-1;
1632 base = ao_ref_base (ref);
1633 offset = ref->offset;
1634 maxsize = ref->max_size;
1636 /* If we cannot constrain the size of the reference we cannot
1637 test if anything kills it. */
1638 if (maxsize == -1)
1639 return (void *)-1;
1641 /* We can't deduce anything useful from clobbers. */
1642 if (gimple_clobber_p (def_stmt))
1643 return (void *)-1;
1645 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1646 from that definition.
1647 1) Memset. */
1648 if (is_gimple_reg_type (vr->type)
1649 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1650 && integer_zerop (gimple_call_arg (def_stmt, 1))
1651 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))
1652 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1654 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1655 tree base2;
1656 HOST_WIDE_INT offset2, size2, maxsize2;
1657 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1658 size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8;
1659 if ((unsigned HOST_WIDE_INT)size2 / 8
1660 == tree_to_uhwi (gimple_call_arg (def_stmt, 2))
1661 && maxsize2 != -1
1662 && operand_equal_p (base, base2, 0)
1663 && offset2 <= offset
1664 && offset2 + size2 >= offset + maxsize)
1666 tree val = build_zero_cst (vr->type);
1667 return vn_reference_lookup_or_insert_for_pieces
1668 (vuse, vr->set, vr->type, vr->operands, val);
1672 /* 2) Assignment from an empty CONSTRUCTOR. */
1673 else if (is_gimple_reg_type (vr->type)
1674 && gimple_assign_single_p (def_stmt)
1675 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1676 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1678 tree base2;
1679 HOST_WIDE_INT offset2, size2, maxsize2;
1680 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1681 &offset2, &size2, &maxsize2);
1682 if (maxsize2 != -1
1683 && operand_equal_p (base, base2, 0)
1684 && offset2 <= offset
1685 && offset2 + size2 >= offset + maxsize)
1687 tree val = build_zero_cst (vr->type);
1688 return vn_reference_lookup_or_insert_for_pieces
1689 (vuse, vr->set, vr->type, vr->operands, val);
1693 /* 3) Assignment from a constant. We can use folds native encode/interpret
1694 routines to extract the assigned bits. */
1695 else if (vn_walk_kind == VN_WALKREWRITE
1696 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1697 && ref->size == maxsize
1698 && maxsize % BITS_PER_UNIT == 0
1699 && offset % BITS_PER_UNIT == 0
1700 && is_gimple_reg_type (vr->type)
1701 && gimple_assign_single_p (def_stmt)
1702 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1704 tree base2;
1705 HOST_WIDE_INT offset2, size2, maxsize2;
1706 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1707 &offset2, &size2, &maxsize2);
1708 if (maxsize2 != -1
1709 && maxsize2 == size2
1710 && size2 % BITS_PER_UNIT == 0
1711 && offset2 % BITS_PER_UNIT == 0
1712 && operand_equal_p (base, base2, 0)
1713 && offset2 <= offset
1714 && offset2 + size2 >= offset + maxsize)
1716 /* We support up to 512-bit values (for V8DFmode). */
1717 unsigned char buffer[64];
1718 int len;
1720 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1721 buffer, sizeof (buffer));
1722 if (len > 0)
1724 tree val = native_interpret_expr (vr->type,
1725 buffer
1726 + ((offset - offset2)
1727 / BITS_PER_UNIT),
1728 ref->size / BITS_PER_UNIT);
1729 if (val)
1730 return vn_reference_lookup_or_insert_for_pieces
1731 (vuse, vr->set, vr->type, vr->operands, val);
1736 /* 4) Assignment from an SSA name which definition we may be able
1737 to access pieces from. */
1738 else if (ref->size == maxsize
1739 && is_gimple_reg_type (vr->type)
1740 && gimple_assign_single_p (def_stmt)
1741 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1743 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1744 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1745 if (is_gimple_assign (def_stmt2)
1746 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1747 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1748 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1750 tree base2;
1751 HOST_WIDE_INT offset2, size2, maxsize2, off;
1752 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1753 &offset2, &size2, &maxsize2);
1754 off = offset - offset2;
1755 if (maxsize2 != -1
1756 && maxsize2 == size2
1757 && operand_equal_p (base, base2, 0)
1758 && offset2 <= offset
1759 && offset2 + size2 >= offset + maxsize)
1761 tree val = NULL_TREE;
1762 HOST_WIDE_INT elsz
1763 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1764 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1766 if (off == 0)
1767 val = gimple_assign_rhs1 (def_stmt2);
1768 else if (off == elsz)
1769 val = gimple_assign_rhs2 (def_stmt2);
1771 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1772 && off % elsz == 0)
1774 tree ctor = gimple_assign_rhs1 (def_stmt2);
1775 unsigned i = off / elsz;
1776 if (i < CONSTRUCTOR_NELTS (ctor))
1778 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1779 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1781 if (TREE_CODE (TREE_TYPE (elt->value))
1782 != VECTOR_TYPE)
1783 val = elt->value;
1787 if (val)
1788 return vn_reference_lookup_or_insert_for_pieces
1789 (vuse, vr->set, vr->type, vr->operands, val);
1794 /* 5) For aggregate copies translate the reference through them if
1795 the copy kills ref. */
1796 else if (vn_walk_kind == VN_WALKREWRITE
1797 && gimple_assign_single_p (def_stmt)
1798 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1799 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1800 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1802 tree base2;
1803 HOST_WIDE_INT offset2, size2, maxsize2;
1804 int i, j;
1805 auto_vec<vn_reference_op_s> rhs;
1806 vn_reference_op_t vro;
1807 ao_ref r;
1809 if (!lhs_ref_ok)
1810 return (void *)-1;
1812 /* See if the assignment kills REF. */
1813 base2 = ao_ref_base (&lhs_ref);
1814 offset2 = lhs_ref.offset;
1815 size2 = lhs_ref.size;
1816 maxsize2 = lhs_ref.max_size;
1817 if (maxsize2 == -1
1818 || (base != base2 && !operand_equal_p (base, base2, 0))
1819 || offset2 > offset
1820 || offset2 + size2 < offset + maxsize)
1821 return (void *)-1;
1823 /* Find the common base of ref and the lhs. lhs_ops already
1824 contains valueized operands for the lhs. */
1825 i = vr->operands.length () - 1;
1826 j = lhs_ops.length () - 1;
1827 while (j >= 0 && i >= 0
1828 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1830 i--;
1831 j--;
1834 /* ??? The innermost op should always be a MEM_REF and we already
1835 checked that the assignment to the lhs kills vr. Thus for
1836 aggregate copies using char[] types the vn_reference_op_eq
1837 may fail when comparing types for compatibility. But we really
1838 don't care here - further lookups with the rewritten operands
1839 will simply fail if we messed up types too badly. */
1840 if (j == 0 && i >= 0
1841 && lhs_ops[0].opcode == MEM_REF
1842 && lhs_ops[0].off != -1
1843 && (lhs_ops[0].off == vr->operands[i].off))
1844 i--, j--;
1846 /* i now points to the first additional op.
1847 ??? LHS may not be completely contained in VR, one or more
1848 VIEW_CONVERT_EXPRs could be in its way. We could at least
1849 try handling outermost VIEW_CONVERT_EXPRs. */
1850 if (j != -1)
1851 return (void *)-1;
1853 /* Now re-write REF to be based on the rhs of the assignment. */
1854 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1855 /* We need to pre-pend vr->operands[0..i] to rhs. */
1856 vec<vn_reference_op_s> old = vr->operands;
1857 if (i + 1 + rhs.length () > vr->operands.length ())
1859 vr->operands.safe_grow (i + 1 + rhs.length ());
1860 if (old == shared_lookup_references)
1861 shared_lookup_references = vr->operands;
1863 else
1864 vr->operands.truncate (i + 1 + rhs.length ());
1865 FOR_EACH_VEC_ELT (rhs, j, vro)
1866 vr->operands[i + 1 + j] = *vro;
1867 vr->operands = valueize_refs (vr->operands);
1868 if (old == shared_lookup_references)
1869 shared_lookup_references = vr->operands;
1870 vr->hashcode = vn_reference_compute_hash (vr);
1872 /* Adjust *ref from the new operands. */
1873 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1874 return (void *)-1;
1875 /* This can happen with bitfields. */
1876 if (ref->size != r.size)
1877 return (void *)-1;
1878 *ref = r;
1880 /* Do not update last seen VUSE after translating. */
1881 last_vuse_ptr = NULL;
1883 /* Keep looking for the adjusted *REF / VR pair. */
1884 return NULL;
1887 /* 6) For memcpy copies translate the reference through them if
1888 the copy kills ref. */
1889 else if (vn_walk_kind == VN_WALKREWRITE
1890 && is_gimple_reg_type (vr->type)
1891 /* ??? Handle BCOPY as well. */
1892 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1893 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1894 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1895 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1896 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1897 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1898 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1899 && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)))
1901 tree lhs, rhs;
1902 ao_ref r;
1903 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1904 vn_reference_op_s op;
1905 HOST_WIDE_INT at;
1908 /* Only handle non-variable, addressable refs. */
1909 if (ref->size != maxsize
1910 || offset % BITS_PER_UNIT != 0
1911 || ref->size % BITS_PER_UNIT != 0)
1912 return (void *)-1;
1914 /* Extract a pointer base and an offset for the destination. */
1915 lhs = gimple_call_arg (def_stmt, 0);
1916 lhs_offset = 0;
1917 if (TREE_CODE (lhs) == SSA_NAME)
1918 lhs = SSA_VAL (lhs);
1919 if (TREE_CODE (lhs) == ADDR_EXPR)
1921 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1922 &lhs_offset);
1923 if (!tem)
1924 return (void *)-1;
1925 if (TREE_CODE (tem) == MEM_REF
1926 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
1928 lhs = TREE_OPERAND (tem, 0);
1929 lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
1931 else if (DECL_P (tem))
1932 lhs = build_fold_addr_expr (tem);
1933 else
1934 return (void *)-1;
1936 if (TREE_CODE (lhs) != SSA_NAME
1937 && TREE_CODE (lhs) != ADDR_EXPR)
1938 return (void *)-1;
1940 /* Extract a pointer base and an offset for the source. */
1941 rhs = gimple_call_arg (def_stmt, 1);
1942 rhs_offset = 0;
1943 if (TREE_CODE (rhs) == SSA_NAME)
1944 rhs = SSA_VAL (rhs);
1945 if (TREE_CODE (rhs) == ADDR_EXPR)
1947 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1948 &rhs_offset);
1949 if (!tem)
1950 return (void *)-1;
1951 if (TREE_CODE (tem) == MEM_REF
1952 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)))
1954 rhs = TREE_OPERAND (tem, 0);
1955 rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1));
1957 else if (DECL_P (tem))
1958 rhs = build_fold_addr_expr (tem);
1959 else
1960 return (void *)-1;
1962 if (TREE_CODE (rhs) != SSA_NAME
1963 && TREE_CODE (rhs) != ADDR_EXPR)
1964 return (void *)-1;
1966 copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2));
1968 /* The bases of the destination and the references have to agree. */
1969 if ((TREE_CODE (base) != MEM_REF
1970 && !DECL_P (base))
1971 || (TREE_CODE (base) == MEM_REF
1972 && (TREE_OPERAND (base, 0) != lhs
1973 || !tree_fits_uhwi_p (TREE_OPERAND (base, 1))))
1974 || (DECL_P (base)
1975 && (TREE_CODE (lhs) != ADDR_EXPR
1976 || TREE_OPERAND (lhs, 0) != base)))
1977 return (void *)-1;
1979 /* And the access has to be contained within the memcpy destination. */
1980 at = offset / BITS_PER_UNIT;
1981 if (TREE_CODE (base) == MEM_REF)
1982 at += tree_to_uhwi (TREE_OPERAND (base, 1));
1983 if (lhs_offset > at
1984 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1985 return (void *)-1;
1987 /* Make room for 2 operands in the new reference. */
1988 if (vr->operands.length () < 2)
1990 vec<vn_reference_op_s> old = vr->operands;
1991 vr->operands.safe_grow_cleared (2);
1992 if (old == shared_lookup_references
1993 && vr->operands != old)
1994 shared_lookup_references = vr->operands;
1996 else
1997 vr->operands.truncate (2);
1999 /* The looked-through reference is a simple MEM_REF. */
2000 memset (&op, 0, sizeof (op));
2001 op.type = vr->type;
2002 op.opcode = MEM_REF;
2003 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
2004 op.off = at - lhs_offset + rhs_offset;
2005 vr->operands[0] = op;
2006 op.type = TREE_TYPE (rhs);
2007 op.opcode = TREE_CODE (rhs);
2008 op.op0 = rhs;
2009 op.off = -1;
2010 vr->operands[1] = op;
2011 vr->hashcode = vn_reference_compute_hash (vr);
2013 /* Adjust *ref from the new operands. */
2014 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
2015 return (void *)-1;
2016 /* This can happen with bitfields. */
2017 if (ref->size != r.size)
2018 return (void *)-1;
2019 *ref = r;
2021 /* Do not update last seen VUSE after translating. */
2022 last_vuse_ptr = NULL;
2024 /* Keep looking for the adjusted *REF / VR pair. */
2025 return NULL;
2028 /* Bail out and stop walking. */
2029 return (void *)-1;
2032 /* Lookup a reference operation by it's parts, in the current hash table.
2033 Returns the resulting value number if it exists in the hash table,
2034 NULL_TREE otherwise. VNRESULT will be filled in with the actual
2035 vn_reference_t stored in the hashtable if something is found. */
2037 tree
2038 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
2039 vec<vn_reference_op_s> operands,
2040 vn_reference_t *vnresult, vn_lookup_kind kind)
2042 struct vn_reference_s vr1;
2043 vn_reference_t tmp;
2044 tree cst;
2046 if (!vnresult)
2047 vnresult = &tmp;
2048 *vnresult = NULL;
2050 vr1.vuse = vuse_ssa_val (vuse);
2051 shared_lookup_references.truncate (0);
2052 shared_lookup_references.safe_grow (operands.length ());
2053 memcpy (shared_lookup_references.address (),
2054 operands.address (),
2055 sizeof (vn_reference_op_s)
2056 * operands.length ());
2057 vr1.operands = operands = shared_lookup_references
2058 = valueize_refs (shared_lookup_references);
2059 vr1.type = type;
2060 vr1.set = set;
2061 vr1.hashcode = vn_reference_compute_hash (&vr1);
2062 if ((cst = fully_constant_vn_reference_p (&vr1)))
2063 return cst;
2065 vn_reference_lookup_1 (&vr1, vnresult);
2066 if (!*vnresult
2067 && kind != VN_NOWALK
2068 && vr1.vuse)
2070 ao_ref r;
2071 vn_walk_kind = kind;
2072 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2073 *vnresult =
2074 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2075 vn_reference_lookup_2,
2076 vn_reference_lookup_3, &vr1);
2077 gcc_checking_assert (vr1.operands == shared_lookup_references);
2080 if (*vnresult)
2081 return (*vnresult)->result;
2083 return NULL_TREE;
2086 /* Lookup OP in the current hash table, and return the resulting value
2087 number if it exists in the hash table. Return NULL_TREE if it does
2088 not exist in the hash table or if the result field of the structure
2089 was NULL.. VNRESULT will be filled in with the vn_reference_t
2090 stored in the hashtable if one exists. */
2092 tree
2093 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2094 vn_reference_t *vnresult)
2096 vec<vn_reference_op_s> operands;
2097 struct vn_reference_s vr1;
2098 tree cst;
2099 bool valuezied_anything;
2101 if (vnresult)
2102 *vnresult = NULL;
2104 vr1.vuse = vuse_ssa_val (vuse);
2105 vr1.operands = operands
2106 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2107 vr1.type = TREE_TYPE (op);
2108 vr1.set = get_alias_set (op);
2109 vr1.hashcode = vn_reference_compute_hash (&vr1);
2110 if ((cst = fully_constant_vn_reference_p (&vr1)))
2111 return cst;
2113 if (kind != VN_NOWALK
2114 && vr1.vuse)
2116 vn_reference_t wvnresult;
2117 ao_ref r;
2118 /* Make sure to use a valueized reference if we valueized anything.
2119 Otherwise preserve the full reference for advanced TBAA. */
2120 if (!valuezied_anything
2121 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2122 vr1.operands))
2123 ao_ref_init (&r, op);
2124 vn_walk_kind = kind;
2125 wvnresult =
2126 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2127 vn_reference_lookup_2,
2128 vn_reference_lookup_3, &vr1);
2129 gcc_checking_assert (vr1.operands == shared_lookup_references);
2130 if (wvnresult)
2132 if (vnresult)
2133 *vnresult = wvnresult;
2134 return wvnresult->result;
2137 return NULL_TREE;
2140 return vn_reference_lookup_1 (&vr1, vnresult);
2143 /* Lookup CALL in the current hash table and return the entry in
2144 *VNRESULT if found. Populates *VR for the hashtable lookup. */
2146 void
2147 vn_reference_lookup_call (gimple_call call, vn_reference_t *vnresult,
2148 vn_reference_t vr)
2150 if (vnresult)
2151 *vnresult = NULL;
2153 tree vuse = gimple_vuse (call);
2155 vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2156 vr->operands = valueize_shared_reference_ops_from_call (call);
2157 vr->type = gimple_expr_type (call);
2158 vr->set = 0;
2159 vr->hashcode = vn_reference_compute_hash (vr);
2160 vn_reference_lookup_1 (vr, vnresult);
2163 /* Insert OP into the current hash table with a value number of
2164 RESULT, and return the resulting reference structure we created. */
2166 static vn_reference_t
2167 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2169 vn_reference_s **slot;
2170 vn_reference_t vr1;
2171 bool tem;
2173 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2174 if (TREE_CODE (result) == SSA_NAME)
2175 vr1->value_id = VN_INFO (result)->value_id;
2176 else
2177 vr1->value_id = get_or_alloc_constant_value_id (result);
2178 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2179 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2180 vr1->type = TREE_TYPE (op);
2181 vr1->set = get_alias_set (op);
2182 vr1->hashcode = vn_reference_compute_hash (vr1);
2183 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2184 vr1->result_vdef = vdef;
2186 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2187 INSERT);
2189 /* Because we lookup stores using vuses, and value number failures
2190 using the vdefs (see visit_reference_op_store for how and why),
2191 it's possible that on failure we may try to insert an already
2192 inserted store. This is not wrong, there is no ssa name for a
2193 store that we could use as a differentiator anyway. Thus, unlike
2194 the other lookup functions, you cannot gcc_assert (!*slot)
2195 here. */
2197 /* But free the old slot in case of a collision. */
2198 if (*slot)
2199 free_reference (*slot);
2201 *slot = vr1;
2202 return vr1;
2205 /* Insert a reference by it's pieces into the current hash table with
2206 a value number of RESULT. Return the resulting reference
2207 structure we created. */
2209 vn_reference_t
2210 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2211 vec<vn_reference_op_s> operands,
2212 tree result, unsigned int value_id)
2215 vn_reference_s **slot;
2216 vn_reference_t vr1;
2218 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2219 vr1->value_id = value_id;
2220 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2221 vr1->operands = valueize_refs (operands);
2222 vr1->type = type;
2223 vr1->set = set;
2224 vr1->hashcode = vn_reference_compute_hash (vr1);
2225 if (result && TREE_CODE (result) == SSA_NAME)
2226 result = SSA_VAL (result);
2227 vr1->result = result;
2229 slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode,
2230 INSERT);
2232 /* At this point we should have all the things inserted that we have
2233 seen before, and we should never try inserting something that
2234 already exists. */
2235 gcc_assert (!*slot);
2236 if (*slot)
2237 free_reference (*slot);
2239 *slot = vr1;
2240 return vr1;
2243 /* Compute and return the hash value for nary operation VBO1. */
2245 static hashval_t
2246 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2248 inchash::hash hstate;
2249 unsigned i;
2251 for (i = 0; i < vno1->length; ++i)
2252 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2253 vno1->op[i] = SSA_VAL (vno1->op[i]);
2255 if (vno1->length == 2
2256 && commutative_tree_code (vno1->opcode)
2257 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2259 tree temp = vno1->op[0];
2260 vno1->op[0] = vno1->op[1];
2261 vno1->op[1] = temp;
2264 hstate.add_int (vno1->opcode);
2265 for (i = 0; i < vno1->length; ++i)
2266 inchash::add_expr (vno1->op[i], hstate);
2268 return hstate.end ();
2271 /* Compare nary operations VNO1 and VNO2 and return true if they are
2272 equivalent. */
2274 bool
2275 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2277 unsigned i;
2279 if (vno1->hashcode != vno2->hashcode)
2280 return false;
2282 if (vno1->length != vno2->length)
2283 return false;
2285 if (vno1->opcode != vno2->opcode
2286 || !types_compatible_p (vno1->type, vno2->type))
2287 return false;
2289 for (i = 0; i < vno1->length; ++i)
2290 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2291 return false;
2293 return true;
2296 /* Initialize VNO from the pieces provided. */
2298 static void
2299 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2300 enum tree_code code, tree type, tree *ops)
2302 vno->opcode = code;
2303 vno->length = length;
2304 vno->type = type;
2305 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2308 /* Initialize VNO from OP. */
2310 static void
2311 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2313 unsigned i;
2315 vno->opcode = TREE_CODE (op);
2316 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2317 vno->type = TREE_TYPE (op);
2318 for (i = 0; i < vno->length; ++i)
2319 vno->op[i] = TREE_OPERAND (op, i);
2322 /* Return the number of operands for a vn_nary ops structure from STMT. */
2324 static unsigned int
2325 vn_nary_length_from_stmt (gimple stmt)
2327 switch (gimple_assign_rhs_code (stmt))
2329 case REALPART_EXPR:
2330 case IMAGPART_EXPR:
2331 case VIEW_CONVERT_EXPR:
2332 return 1;
2334 case BIT_FIELD_REF:
2335 return 3;
2337 case CONSTRUCTOR:
2338 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2340 default:
2341 return gimple_num_ops (stmt) - 1;
2345 /* Initialize VNO from STMT. */
2347 static void
2348 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2350 unsigned i;
2352 vno->opcode = gimple_assign_rhs_code (stmt);
2353 vno->type = gimple_expr_type (stmt);
2354 switch (vno->opcode)
2356 case REALPART_EXPR:
2357 case IMAGPART_EXPR:
2358 case VIEW_CONVERT_EXPR:
2359 vno->length = 1;
2360 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2361 break;
2363 case BIT_FIELD_REF:
2364 vno->length = 3;
2365 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2366 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2367 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2368 break;
2370 case CONSTRUCTOR:
2371 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2372 for (i = 0; i < vno->length; ++i)
2373 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2374 break;
2376 default:
2377 gcc_checking_assert (!gimple_assign_single_p (stmt));
2378 vno->length = gimple_num_ops (stmt) - 1;
2379 for (i = 0; i < vno->length; ++i)
2380 vno->op[i] = gimple_op (stmt, i + 1);
2384 /* Compute the hashcode for VNO and look for it in the hash table;
2385 return the resulting value number if it exists in the hash table.
2386 Return NULL_TREE if it does not exist in the hash table or if the
2387 result field of the operation is NULL. VNRESULT will contain the
2388 vn_nary_op_t from the hashtable if it exists. */
2390 static tree
2391 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2393 vn_nary_op_s **slot;
2395 if (vnresult)
2396 *vnresult = NULL;
2398 vno->hashcode = vn_nary_op_compute_hash (vno);
2399 slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode,
2400 NO_INSERT);
2401 if (!slot && current_info == optimistic_info)
2402 slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode,
2403 NO_INSERT);
2404 if (!slot)
2405 return NULL_TREE;
2406 if (vnresult)
2407 *vnresult = *slot;
2408 return (*slot)->result;
2411 /* Lookup a n-ary operation by its pieces and return the resulting value
2412 number if it exists in the hash table. Return NULL_TREE if it does
2413 not exist in the hash table or if the result field of the operation
2414 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2415 if it exists. */
2417 tree
2418 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2419 tree type, tree *ops, vn_nary_op_t *vnresult)
2421 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2422 sizeof_vn_nary_op (length));
2423 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2424 return vn_nary_op_lookup_1 (vno1, vnresult);
2427 /* Lookup OP in the current hash table, and return the resulting value
2428 number if it exists in the hash table. Return NULL_TREE if it does
2429 not exist in the hash table or if the result field of the operation
2430 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2431 if it exists. */
2433 tree
2434 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2436 vn_nary_op_t vno1
2437 = XALLOCAVAR (struct vn_nary_op_s,
2438 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2439 init_vn_nary_op_from_op (vno1, op);
2440 return vn_nary_op_lookup_1 (vno1, vnresult);
2443 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2444 value number if it exists in the hash table. Return NULL_TREE if
2445 it does not exist in the hash table. VNRESULT will contain the
2446 vn_nary_op_t from the hashtable if it exists. */
2448 tree
2449 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2451 vn_nary_op_t vno1
2452 = XALLOCAVAR (struct vn_nary_op_s,
2453 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2454 init_vn_nary_op_from_stmt (vno1, stmt);
2455 return vn_nary_op_lookup_1 (vno1, vnresult);
2458 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2460 static vn_nary_op_t
2461 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2463 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2466 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2467 obstack. */
2469 static vn_nary_op_t
2470 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2472 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2473 &current_info->nary_obstack);
2475 vno1->value_id = value_id;
2476 vno1->length = length;
2477 vno1->result = result;
2479 return vno1;
2482 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2483 VNO->HASHCODE first. */
2485 static vn_nary_op_t
2486 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table,
2487 bool compute_hash)
2489 vn_nary_op_s **slot;
2491 if (compute_hash)
2492 vno->hashcode = vn_nary_op_compute_hash (vno);
2494 slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT);
2495 gcc_assert (!*slot);
2497 *slot = vno;
2498 return vno;
2501 /* Insert a n-ary operation into the current hash table using it's
2502 pieces. Return the vn_nary_op_t structure we created and put in
2503 the hashtable. */
2505 vn_nary_op_t
2506 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2507 tree type, tree *ops,
2508 tree result, unsigned int value_id)
2510 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2511 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2512 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2515 /* Insert OP into the current hash table with a value number of
2516 RESULT. Return the vn_nary_op_t structure we created and put in
2517 the hashtable. */
2519 vn_nary_op_t
2520 vn_nary_op_insert (tree op, tree result)
2522 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2523 vn_nary_op_t vno1;
2525 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2526 init_vn_nary_op_from_op (vno1, op);
2527 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2530 /* Insert the rhs of STMT into the current hash table with a value number of
2531 RESULT. */
2533 vn_nary_op_t
2534 vn_nary_op_insert_stmt (gimple stmt, tree result)
2536 vn_nary_op_t vno1
2537 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2538 result, VN_INFO (result)->value_id);
2539 init_vn_nary_op_from_stmt (vno1, stmt);
2540 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2543 /* Compute a hashcode for PHI operation VP1 and return it. */
2545 static inline hashval_t
2546 vn_phi_compute_hash (vn_phi_t vp1)
2548 inchash::hash hstate (vp1->block->index);
2549 int i;
2550 tree phi1op;
2551 tree type;
2553 /* If all PHI arguments are constants we need to distinguish
2554 the PHI node via its type. */
2555 type = vp1->type;
2556 hstate.merge_hash (vn_hash_type (type));
2558 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2560 if (phi1op == VN_TOP)
2561 continue;
2562 inchash::add_expr (phi1op, hstate);
2565 return hstate.end ();
2568 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2570 static int
2571 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2573 if (vp1->hashcode != vp2->hashcode)
2574 return false;
2576 if (vp1->block == vp2->block)
2578 int i;
2579 tree phi1op;
2581 /* If the PHI nodes do not have compatible types
2582 they are not the same. */
2583 if (!types_compatible_p (vp1->type, vp2->type))
2584 return false;
2586 /* Any phi in the same block will have it's arguments in the
2587 same edge order, because of how we store phi nodes. */
2588 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2590 tree phi2op = vp2->phiargs[i];
2591 if (phi1op == VN_TOP || phi2op == VN_TOP)
2592 continue;
2593 if (!expressions_equal_p (phi1op, phi2op))
2594 return false;
2596 return true;
2598 return false;
2601 static vec<tree> shared_lookup_phiargs;
2603 /* Lookup PHI in the current hash table, and return the resulting
2604 value number if it exists in the hash table. Return NULL_TREE if
2605 it does not exist in the hash table. */
2607 static tree
2608 vn_phi_lookup (gimple phi)
2610 vn_phi_s **slot;
2611 struct vn_phi_s vp1;
2612 unsigned i;
2614 shared_lookup_phiargs.truncate (0);
2616 /* Canonicalize the SSA_NAME's to their value number. */
2617 for (i = 0; i < gimple_phi_num_args (phi); i++)
2619 tree def = PHI_ARG_DEF (phi, i);
2620 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2621 shared_lookup_phiargs.safe_push (def);
2623 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2624 vp1.phiargs = shared_lookup_phiargs;
2625 vp1.block = gimple_bb (phi);
2626 vp1.hashcode = vn_phi_compute_hash (&vp1);
2627 slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2628 NO_INSERT);
2629 if (!slot && current_info == optimistic_info)
2630 slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode,
2631 NO_INSERT);
2632 if (!slot)
2633 return NULL_TREE;
2634 return (*slot)->result;
2637 /* Insert PHI into the current hash table with a value number of
2638 RESULT. */
2640 static vn_phi_t
2641 vn_phi_insert (gimple phi, tree result)
2643 vn_phi_s **slot;
2644 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2645 unsigned i;
2646 vec<tree> args = vNULL;
2648 /* Canonicalize the SSA_NAME's to their value number. */
2649 for (i = 0; i < gimple_phi_num_args (phi); i++)
2651 tree def = PHI_ARG_DEF (phi, i);
2652 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2653 args.safe_push (def);
2655 vp1->value_id = VN_INFO (result)->value_id;
2656 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2657 vp1->phiargs = args;
2658 vp1->block = gimple_bb (phi);
2659 vp1->result = result;
2660 vp1->hashcode = vn_phi_compute_hash (vp1);
2662 slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2664 /* Because we iterate over phi operations more than once, it's
2665 possible the slot might already exist here, hence no assert.*/
2666 *slot = vp1;
2667 return vp1;
2671 /* Print set of components in strongly connected component SCC to OUT. */
2673 static void
2674 print_scc (FILE *out, vec<tree> scc)
2676 tree var;
2677 unsigned int i;
2679 fprintf (out, "SCC consists of:");
2680 FOR_EACH_VEC_ELT (scc, i, var)
2682 fprintf (out, " ");
2683 print_generic_expr (out, var, 0);
2685 fprintf (out, "\n");
2688 /* Set the value number of FROM to TO, return true if it has changed
2689 as a result. */
2691 static inline bool
2692 set_ssa_val_to (tree from, tree to)
2694 tree currval = SSA_VAL (from);
2695 HOST_WIDE_INT toff, coff;
2697 /* The only thing we allow as value numbers are ssa_names
2698 and invariants. So assert that here. We don't allow VN_TOP
2699 as visiting a stmt should produce a value-number other than
2700 that.
2701 ??? Still VN_TOP can happen for unreachable code, so force
2702 it to varying in that case. Not all code is prepared to
2703 get VN_TOP on valueization. */
2704 if (to == VN_TOP)
2706 if (dump_file && (dump_flags & TDF_DETAILS))
2707 fprintf (dump_file, "Forcing value number to varying on "
2708 "receiving VN_TOP\n");
2709 to = from;
2712 gcc_assert (to != NULL_TREE
2713 && (TREE_CODE (to) == SSA_NAME
2714 || is_gimple_min_invariant (to)));
2716 if (from != to)
2718 if (currval == from)
2720 if (dump_file && (dump_flags & TDF_DETAILS))
2722 fprintf (dump_file, "Not changing value number of ");
2723 print_generic_expr (dump_file, from, 0);
2724 fprintf (dump_file, " from VARYING to ");
2725 print_generic_expr (dump_file, to, 0);
2726 fprintf (dump_file, "\n");
2728 return false;
2730 else if (TREE_CODE (to) == SSA_NAME
2731 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2732 to = from;
2735 if (dump_file && (dump_flags & TDF_DETAILS))
2737 fprintf (dump_file, "Setting value number of ");
2738 print_generic_expr (dump_file, from, 0);
2739 fprintf (dump_file, " to ");
2740 print_generic_expr (dump_file, to, 0);
2743 if (currval != to
2744 && !operand_equal_p (currval, to, 0)
2745 /* ??? For addresses involving volatile objects or types operand_equal_p
2746 does not reliably detect ADDR_EXPRs as equal. We know we are only
2747 getting invariant gimple addresses here, so can use
2748 get_addr_base_and_unit_offset to do this comparison. */
2749 && !(TREE_CODE (currval) == ADDR_EXPR
2750 && TREE_CODE (to) == ADDR_EXPR
2751 && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff)
2752 == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff))
2753 && coff == toff))
2755 VN_INFO (from)->valnum = to;
2756 if (dump_file && (dump_flags & TDF_DETAILS))
2757 fprintf (dump_file, " (changed)\n");
2758 return true;
2760 if (dump_file && (dump_flags & TDF_DETAILS))
2761 fprintf (dump_file, "\n");
2762 return false;
2765 /* Mark as processed all the definitions in the defining stmt of USE, or
2766 the USE itself. */
2768 static void
2769 mark_use_processed (tree use)
2771 ssa_op_iter iter;
2772 def_operand_p defp;
2773 gimple stmt = SSA_NAME_DEF_STMT (use);
2775 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2777 VN_INFO (use)->use_processed = true;
2778 return;
2781 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2783 tree def = DEF_FROM_PTR (defp);
2785 VN_INFO (def)->use_processed = true;
2789 /* Set all definitions in STMT to value number to themselves.
2790 Return true if a value number changed. */
2792 static bool
2793 defs_to_varying (gimple stmt)
2795 bool changed = false;
2796 ssa_op_iter iter;
2797 def_operand_p defp;
2799 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2801 tree def = DEF_FROM_PTR (defp);
2802 changed |= set_ssa_val_to (def, def);
2804 return changed;
2807 static bool expr_has_constants (tree expr);
2809 /* Visit a copy between LHS and RHS, return true if the value number
2810 changed. */
2812 static bool
2813 visit_copy (tree lhs, tree rhs)
2815 /* The copy may have a more interesting constant filled expression
2816 (we don't, since we know our RHS is just an SSA name). */
2817 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2818 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2820 /* And finally valueize. */
2821 rhs = SSA_VAL (rhs);
2823 return set_ssa_val_to (lhs, rhs);
2826 /* Visit a nary operator RHS, value number it, and return true if the
2827 value number of LHS has changed as a result. */
2829 static bool
2830 visit_nary_op (tree lhs, gimple stmt)
2832 bool changed = false;
2833 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2835 if (result)
2836 changed = set_ssa_val_to (lhs, result);
2837 else
2839 changed = set_ssa_val_to (lhs, lhs);
2840 vn_nary_op_insert_stmt (stmt, lhs);
2843 return changed;
2846 /* Visit a call STMT storing into LHS. Return true if the value number
2847 of the LHS has changed as a result. */
2849 static bool
2850 visit_reference_op_call (tree lhs, gimple_call stmt)
2852 bool changed = false;
2853 struct vn_reference_s vr1;
2854 vn_reference_t vnresult = NULL;
2855 tree vdef = gimple_vdef (stmt);
2857 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2858 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2859 lhs = NULL_TREE;
2861 vn_reference_lookup_call (stmt, &vnresult, &vr1);
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_t vr2;
2881 vn_reference_s **slot;
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 /* As we are not walking the virtual operand chain we know the
2889 shared_lookup_references are still original so we can re-use
2890 them here. */
2891 vr2->operands = vr1.operands.copy ();
2892 vr2->type = vr1.type;
2893 vr2->set = vr1.set;
2894 vr2->hashcode = vr1.hashcode;
2895 vr2->result = lhs;
2896 vr2->result_vdef = vdef;
2897 slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode,
2898 INSERT);
2899 gcc_assert (!*slot);
2900 *slot = vr2;
2903 return changed;
2906 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2907 and return true if the value number of the LHS has changed as a result. */
2909 static bool
2910 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2912 bool changed = false;
2913 tree last_vuse;
2914 tree result;
2916 last_vuse = gimple_vuse (stmt);
2917 last_vuse_ptr = &last_vuse;
2918 result = vn_reference_lookup (op, gimple_vuse (stmt),
2919 default_vn_walk_kind, NULL);
2920 last_vuse_ptr = NULL;
2922 /* We handle type-punning through unions by value-numbering based
2923 on offset and size of the access. Be prepared to handle a
2924 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2925 if (result
2926 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2928 /* We will be setting the value number of lhs to the value number
2929 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2930 So first simplify and lookup this expression to see if it
2931 is already available. */
2932 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2933 if ((CONVERT_EXPR_P (val)
2934 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2935 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2937 tree tem = vn_get_expr_for (TREE_OPERAND (val, 0));
2938 if ((CONVERT_EXPR_P (tem)
2939 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2940 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2941 TREE_TYPE (val), tem)))
2942 val = tem;
2944 result = val;
2945 if (!is_gimple_min_invariant (val)
2946 && TREE_CODE (val) != SSA_NAME)
2947 result = vn_nary_op_lookup (val, NULL);
2948 /* If the expression is not yet available, value-number lhs to
2949 a new SSA_NAME we create. */
2950 if (!result)
2952 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
2953 "vntemp");
2954 /* Initialize value-number information properly. */
2955 VN_INFO_GET (result)->valnum = result;
2956 VN_INFO (result)->value_id = get_next_value_id ();
2957 VN_INFO (result)->expr = val;
2958 VN_INFO (result)->has_constants = expr_has_constants (val);
2959 VN_INFO (result)->needs_insertion = true;
2960 /* As all "inserted" statements are singleton SCCs, insert
2961 to the valid table. This is strictly needed to
2962 avoid re-generating new value SSA_NAMEs for the same
2963 expression during SCC iteration over and over (the
2964 optimistic table gets cleared after each iteration).
2965 We do not need to insert into the optimistic table, as
2966 lookups there will fall back to the valid table. */
2967 if (current_info == optimistic_info)
2969 current_info = valid_info;
2970 vn_nary_op_insert (val, result);
2971 current_info = optimistic_info;
2973 else
2974 vn_nary_op_insert (val, result);
2975 if (dump_file && (dump_flags & TDF_DETAILS))
2977 fprintf (dump_file, "Inserting name ");
2978 print_generic_expr (dump_file, result, 0);
2979 fprintf (dump_file, " for expression ");
2980 print_generic_expr (dump_file, val, 0);
2981 fprintf (dump_file, "\n");
2986 if (result)
2988 changed = set_ssa_val_to (lhs, result);
2989 if (TREE_CODE (result) == SSA_NAME
2990 && VN_INFO (result)->has_constants)
2992 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2993 VN_INFO (lhs)->has_constants = true;
2996 else
2998 changed = set_ssa_val_to (lhs, lhs);
2999 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
3002 return changed;
3006 /* Visit a store to a reference operator LHS, part of STMT, value number it,
3007 and return true if the value number of the LHS has changed as a result. */
3009 static bool
3010 visit_reference_op_store (tree lhs, tree op, gimple stmt)
3012 bool changed = false;
3013 vn_reference_t vnresult = NULL;
3014 tree result, assign;
3015 bool resultsame = false;
3016 tree vuse = gimple_vuse (stmt);
3017 tree vdef = gimple_vdef (stmt);
3019 /* First we want to lookup using the *vuses* from the store and see
3020 if there the last store to this location with the same address
3021 had the same value.
3023 The vuses represent the memory state before the store. If the
3024 memory state, address, and value of the store is the same as the
3025 last store to this location, then this store will produce the
3026 same memory state as that store.
3028 In this case the vdef versions for this store are value numbered to those
3029 vuse versions, since they represent the same memory state after
3030 this store.
3032 Otherwise, the vdefs for the store are used when inserting into
3033 the table, since the store generates a new memory state. */
3035 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
3037 if (result)
3039 if (TREE_CODE (result) == SSA_NAME)
3040 result = SSA_VAL (result);
3041 if (TREE_CODE (op) == SSA_NAME)
3042 op = SSA_VAL (op);
3043 resultsame = expressions_equal_p (result, op);
3046 if ((!result || !resultsame)
3047 /* Only perform the following when being called from PRE
3048 which embeds tail merging. */
3049 && default_vn_walk_kind == VN_WALK)
3051 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3052 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
3053 if (vnresult)
3055 VN_INFO (vdef)->use_processed = true;
3056 return set_ssa_val_to (vdef, vnresult->result_vdef);
3060 if (!result || !resultsame)
3062 if (dump_file && (dump_flags & TDF_DETAILS))
3064 fprintf (dump_file, "No store match\n");
3065 fprintf (dump_file, "Value numbering store ");
3066 print_generic_expr (dump_file, lhs, 0);
3067 fprintf (dump_file, " to ");
3068 print_generic_expr (dump_file, op, 0);
3069 fprintf (dump_file, "\n");
3071 /* Have to set value numbers before insert, since insert is
3072 going to valueize the references in-place. */
3073 if (vdef)
3075 changed |= set_ssa_val_to (vdef, vdef);
3078 /* Do not insert structure copies into the tables. */
3079 if (is_gimple_min_invariant (op)
3080 || is_gimple_reg (op))
3081 vn_reference_insert (lhs, op, vdef, NULL);
3083 /* Only perform the following when being called from PRE
3084 which embeds tail merging. */
3085 if (default_vn_walk_kind == VN_WALK)
3087 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
3088 vn_reference_insert (assign, lhs, vuse, vdef);
3091 else
3093 /* We had a match, so value number the vdef to have the value
3094 number of the vuse it came from. */
3096 if (dump_file && (dump_flags & TDF_DETAILS))
3097 fprintf (dump_file, "Store matched earlier value,"
3098 "value numbering store vdefs to matching vuses.\n");
3100 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
3103 return changed;
3106 /* Visit and value number PHI, return true if the value number
3107 changed. */
3109 static bool
3110 visit_phi (gimple phi)
3112 bool changed = false;
3113 tree result;
3114 tree sameval = VN_TOP;
3115 bool allsame = true;
3117 /* TODO: We could check for this in init_sccvn, and replace this
3118 with a gcc_assert. */
3119 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3120 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3122 /* See if all non-TOP arguments have the same value. TOP is
3123 equivalent to everything, so we can ignore it. */
3124 edge_iterator ei;
3125 edge e;
3126 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
3127 if (e->flags & EDGE_EXECUTABLE)
3129 tree def = PHI_ARG_DEF_FROM_EDGE (phi, e);
3131 if (TREE_CODE (def) == SSA_NAME)
3132 def = SSA_VAL (def);
3133 if (def == VN_TOP)
3134 continue;
3135 if (sameval == VN_TOP)
3137 sameval = def;
3139 else
3141 if (!expressions_equal_p (def, sameval))
3143 allsame = false;
3144 break;
3149 /* If all value numbered to the same value, the phi node has that
3150 value. */
3151 if (allsame)
3152 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3154 /* Otherwise, see if it is equivalent to a phi node in this block. */
3155 result = vn_phi_lookup (phi);
3156 if (result)
3157 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3158 else
3160 vn_phi_insert (phi, PHI_RESULT (phi));
3161 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3162 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3163 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3166 return changed;
3169 /* Return true if EXPR contains constants. */
3171 static bool
3172 expr_has_constants (tree expr)
3174 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3176 case tcc_unary:
3177 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3179 case tcc_binary:
3180 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3181 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3182 /* Constants inside reference ops are rarely interesting, but
3183 it can take a lot of looking to find them. */
3184 case tcc_reference:
3185 case tcc_declaration:
3186 return false;
3187 default:
3188 return is_gimple_min_invariant (expr);
3190 return false;
3193 /* Return true if STMT contains constants. */
3195 static bool
3196 stmt_has_constants (gimple stmt)
3198 tree tem;
3200 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3201 return false;
3203 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3205 case GIMPLE_TERNARY_RHS:
3206 tem = gimple_assign_rhs3 (stmt);
3207 if (TREE_CODE (tem) == SSA_NAME)
3208 tem = SSA_VAL (tem);
3209 if (is_gimple_min_invariant (tem))
3210 return true;
3211 /* Fallthru. */
3213 case GIMPLE_BINARY_RHS:
3214 tem = gimple_assign_rhs2 (stmt);
3215 if (TREE_CODE (tem) == SSA_NAME)
3216 tem = SSA_VAL (tem);
3217 if (is_gimple_min_invariant (tem))
3218 return true;
3219 /* Fallthru. */
3221 case GIMPLE_SINGLE_RHS:
3222 /* Constants inside reference ops are rarely interesting, but
3223 it can take a lot of looking to find them. */
3224 case GIMPLE_UNARY_RHS:
3225 tem = gimple_assign_rhs1 (stmt);
3226 if (TREE_CODE (tem) == SSA_NAME)
3227 tem = SSA_VAL (tem);
3228 return is_gimple_min_invariant (tem);
3230 default:
3231 gcc_unreachable ();
3233 return false;
3236 /* Simplify the binary expression RHS, and return the result if
3237 simplified. */
3239 static tree
3240 simplify_binary_expression (gimple stmt)
3242 tree result = NULL_TREE;
3243 tree op0 = gimple_assign_rhs1 (stmt);
3244 tree op1 = gimple_assign_rhs2 (stmt);
3245 enum tree_code code = gimple_assign_rhs_code (stmt);
3247 /* This will not catch every single case we could combine, but will
3248 catch those with constants. The goal here is to simultaneously
3249 combine constants between expressions, but avoid infinite
3250 expansion of expressions during simplification. */
3251 op0 = vn_valueize (op0);
3252 if (TREE_CODE (op0) == SSA_NAME
3253 && (VN_INFO (op0)->has_constants
3254 || TREE_CODE_CLASS (code) == tcc_comparison
3255 || code == COMPLEX_EXPR))
3256 op0 = vn_get_expr_for (op0);
3258 op1 = vn_valueize (op1);
3259 if (TREE_CODE (op1) == SSA_NAME
3260 && (VN_INFO (op1)->has_constants
3261 || code == COMPLEX_EXPR))
3262 op1 = vn_get_expr_for (op1);
3264 /* Pointer plus constant can be represented as invariant address.
3265 Do so to allow further propatation, see also tree forwprop. */
3266 if (code == POINTER_PLUS_EXPR
3267 && tree_fits_uhwi_p (op1)
3268 && TREE_CODE (op0) == ADDR_EXPR
3269 && is_gimple_min_invariant (op0))
3270 return build_invariant_address (TREE_TYPE (op0),
3271 TREE_OPERAND (op0, 0),
3272 tree_to_uhwi (op1));
3274 /* Avoid folding if nothing changed. */
3275 if (op0 == gimple_assign_rhs1 (stmt)
3276 && op1 == gimple_assign_rhs2 (stmt))
3277 return NULL_TREE;
3279 fold_defer_overflow_warnings ();
3281 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3282 if (result)
3283 STRIP_USELESS_TYPE_CONVERSION (result);
3285 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3286 stmt, 0);
3288 /* Make sure result is not a complex expression consisting
3289 of operators of operators (IE (a + b) + (a + c))
3290 Otherwise, we will end up with unbounded expressions if
3291 fold does anything at all. */
3292 if (result && valid_gimple_rhs_p (result))
3293 return result;
3295 return NULL_TREE;
3298 /* Simplify the unary expression RHS, and return the result if
3299 simplified. */
3301 static tree
3302 simplify_unary_expression (gimple_assign stmt)
3304 tree result = NULL_TREE;
3305 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3306 enum tree_code code = gimple_assign_rhs_code (stmt);
3308 /* We handle some tcc_reference codes here that are all
3309 GIMPLE_ASSIGN_SINGLE codes. */
3310 if (code == REALPART_EXPR
3311 || code == IMAGPART_EXPR
3312 || code == VIEW_CONVERT_EXPR
3313 || code == BIT_FIELD_REF)
3314 op0 = TREE_OPERAND (op0, 0);
3316 orig_op0 = op0;
3317 op0 = vn_valueize (op0);
3318 if (TREE_CODE (op0) == SSA_NAME)
3320 if (VN_INFO (op0)->has_constants)
3321 op0 = vn_get_expr_for (op0);
3322 else if (CONVERT_EXPR_CODE_P (code)
3323 || code == REALPART_EXPR
3324 || code == IMAGPART_EXPR
3325 || code == VIEW_CONVERT_EXPR
3326 || code == BIT_FIELD_REF)
3328 /* We want to do tree-combining on conversion-like expressions.
3329 Make sure we feed only SSA_NAMEs or constants to fold though. */
3330 tree tem = vn_get_expr_for (op0);
3331 if (UNARY_CLASS_P (tem)
3332 || BINARY_CLASS_P (tem)
3333 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3334 || TREE_CODE (tem) == SSA_NAME
3335 || TREE_CODE (tem) == CONSTRUCTOR
3336 || is_gimple_min_invariant (tem))
3337 op0 = tem;
3341 /* Avoid folding if nothing changed, but remember the expression. */
3342 if (op0 == orig_op0)
3343 return NULL_TREE;
3345 if (code == BIT_FIELD_REF)
3347 tree rhs = gimple_assign_rhs1 (stmt);
3348 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3349 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3351 else
3352 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3353 if (result)
3355 STRIP_USELESS_TYPE_CONVERSION (result);
3356 if (valid_gimple_rhs_p (result))
3357 return result;
3360 return NULL_TREE;
3363 /* Try to simplify RHS using equivalences and constant folding. */
3365 static tree
3366 try_to_simplify (gimple_assign stmt)
3368 enum tree_code code = gimple_assign_rhs_code (stmt);
3369 tree tem;
3371 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3372 in this case, there is no point in doing extra work. */
3373 if (code == SSA_NAME)
3374 return NULL_TREE;
3376 /* First try constant folding based on our current lattice. */
3377 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize);
3378 if (tem
3379 && (TREE_CODE (tem) == SSA_NAME
3380 || is_gimple_min_invariant (tem)))
3381 return tem;
3383 /* If that didn't work try combining multiple statements. */
3384 switch (TREE_CODE_CLASS (code))
3386 case tcc_reference:
3387 /* Fallthrough for some unary codes that can operate on registers. */
3388 if (!(code == REALPART_EXPR
3389 || code == IMAGPART_EXPR
3390 || code == VIEW_CONVERT_EXPR
3391 || code == BIT_FIELD_REF))
3392 break;
3393 /* We could do a little more with unary ops, if they expand
3394 into binary ops, but it's debatable whether it is worth it. */
3395 case tcc_unary:
3396 return simplify_unary_expression (stmt);
3398 case tcc_comparison:
3399 case tcc_binary:
3400 return simplify_binary_expression (stmt);
3402 default:
3403 break;
3406 return NULL_TREE;
3409 /* Visit and value number USE, return true if the value number
3410 changed. */
3412 static bool
3413 visit_use (tree use)
3415 bool changed = false;
3416 gimple stmt = SSA_NAME_DEF_STMT (use);
3418 mark_use_processed (use);
3420 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3421 if (dump_file && (dump_flags & TDF_DETAILS)
3422 && !SSA_NAME_IS_DEFAULT_DEF (use))
3424 fprintf (dump_file, "Value numbering ");
3425 print_generic_expr (dump_file, use, 0);
3426 fprintf (dump_file, " stmt = ");
3427 print_gimple_stmt (dump_file, stmt, 0, 0);
3430 /* Handle uninitialized uses. */
3431 if (SSA_NAME_IS_DEFAULT_DEF (use))
3432 changed = set_ssa_val_to (use, use);
3433 else
3435 if (gimple_code (stmt) == GIMPLE_PHI)
3436 changed = visit_phi (stmt);
3437 else if (gimple_has_volatile_ops (stmt))
3438 changed = defs_to_varying (stmt);
3439 else if (is_gimple_assign (stmt))
3441 enum tree_code code = gimple_assign_rhs_code (stmt);
3442 tree lhs = gimple_assign_lhs (stmt);
3443 tree rhs1 = gimple_assign_rhs1 (stmt);
3444 tree simplified;
3446 /* Shortcut for copies. Simplifying copies is pointless,
3447 since we copy the expression and value they represent. */
3448 if (code == SSA_NAME
3449 && TREE_CODE (lhs) == SSA_NAME)
3451 changed = visit_copy (lhs, rhs1);
3452 goto done;
3454 simplified = try_to_simplify (as_a <gimple_assign> (stmt));
3455 if (simplified)
3457 if (dump_file && (dump_flags & TDF_DETAILS))
3459 fprintf (dump_file, "RHS ");
3460 print_gimple_expr (dump_file, stmt, 0, 0);
3461 fprintf (dump_file, " simplified to ");
3462 print_generic_expr (dump_file, simplified, 0);
3463 if (TREE_CODE (lhs) == SSA_NAME)
3464 fprintf (dump_file, " has constants %d\n",
3465 expr_has_constants (simplified));
3466 else
3467 fprintf (dump_file, "\n");
3470 /* Setting value numbers to constants will occasionally
3471 screw up phi congruence because constants are not
3472 uniquely associated with a single ssa name that can be
3473 looked up. */
3474 if (simplified
3475 && is_gimple_min_invariant (simplified)
3476 && TREE_CODE (lhs) == SSA_NAME)
3478 VN_INFO (lhs)->expr = simplified;
3479 VN_INFO (lhs)->has_constants = true;
3480 changed = set_ssa_val_to (lhs, simplified);
3481 goto done;
3483 else if (simplified
3484 && TREE_CODE (simplified) == SSA_NAME
3485 && TREE_CODE (lhs) == SSA_NAME)
3487 changed = visit_copy (lhs, simplified);
3488 goto done;
3490 else if (simplified)
3492 if (TREE_CODE (lhs) == SSA_NAME)
3494 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3495 /* We have to unshare the expression or else
3496 valuizing may change the IL stream. */
3497 VN_INFO (lhs)->expr = unshare_expr (simplified);
3500 else if (stmt_has_constants (stmt)
3501 && TREE_CODE (lhs) == SSA_NAME)
3502 VN_INFO (lhs)->has_constants = true;
3503 else if (TREE_CODE (lhs) == SSA_NAME)
3505 /* We reset expr and constantness here because we may
3506 have been value numbering optimistically, and
3507 iterating. They may become non-constant in this case,
3508 even if they were optimistically constant. */
3510 VN_INFO (lhs)->has_constants = false;
3511 VN_INFO (lhs)->expr = NULL_TREE;
3514 if ((TREE_CODE (lhs) == SSA_NAME
3515 /* We can substitute SSA_NAMEs that are live over
3516 abnormal edges with their constant value. */
3517 && !(gimple_assign_copy_p (stmt)
3518 && is_gimple_min_invariant (rhs1))
3519 && !(simplified
3520 && is_gimple_min_invariant (simplified))
3521 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3522 /* Stores or copies from SSA_NAMEs that are live over
3523 abnormal edges are a problem. */
3524 || (code == SSA_NAME
3525 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3526 changed = defs_to_varying (stmt);
3527 else if (REFERENCE_CLASS_P (lhs)
3528 || DECL_P (lhs))
3529 changed = visit_reference_op_store (lhs, rhs1, stmt);
3530 else if (TREE_CODE (lhs) == SSA_NAME)
3532 if ((gimple_assign_copy_p (stmt)
3533 && is_gimple_min_invariant (rhs1))
3534 || (simplified
3535 && is_gimple_min_invariant (simplified)))
3537 VN_INFO (lhs)->has_constants = true;
3538 if (simplified)
3539 changed = set_ssa_val_to (lhs, simplified);
3540 else
3541 changed = set_ssa_val_to (lhs, rhs1);
3543 else
3545 /* First try to lookup the simplified expression. */
3546 if (simplified)
3548 enum gimple_rhs_class rhs_class;
3551 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3552 if ((rhs_class == GIMPLE_UNARY_RHS
3553 || rhs_class == GIMPLE_BINARY_RHS
3554 || rhs_class == GIMPLE_TERNARY_RHS)
3555 && valid_gimple_rhs_p (simplified))
3557 tree result = vn_nary_op_lookup (simplified, NULL);
3558 if (result)
3560 changed = set_ssa_val_to (lhs, result);
3561 goto done;
3566 /* Otherwise visit the original statement. */
3567 switch (vn_get_stmt_kind (stmt))
3569 case VN_NARY:
3570 changed = visit_nary_op (lhs, stmt);
3571 break;
3572 case VN_REFERENCE:
3573 changed = visit_reference_op_load (lhs, rhs1, stmt);
3574 break;
3575 default:
3576 changed = defs_to_varying (stmt);
3577 break;
3581 else
3582 changed = defs_to_varying (stmt);
3584 else if (gimple_call call_stmt = dyn_cast <gimple_call> (stmt))
3586 tree lhs = gimple_call_lhs (stmt);
3587 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3589 /* Try constant folding based on our current lattice. */
3590 tree simplified = gimple_fold_stmt_to_constant_1 (stmt,
3591 vn_valueize);
3592 if (simplified)
3594 if (dump_file && (dump_flags & TDF_DETAILS))
3596 fprintf (dump_file, "call ");
3597 print_gimple_expr (dump_file, stmt, 0, 0);
3598 fprintf (dump_file, " simplified to ");
3599 print_generic_expr (dump_file, simplified, 0);
3600 if (TREE_CODE (lhs) == SSA_NAME)
3601 fprintf (dump_file, " has constants %d\n",
3602 expr_has_constants (simplified));
3603 else
3604 fprintf (dump_file, "\n");
3607 /* Setting value numbers to constants will occasionally
3608 screw up phi congruence because constants are not
3609 uniquely associated with a single ssa name that can be
3610 looked up. */
3611 if (simplified
3612 && is_gimple_min_invariant (simplified))
3614 VN_INFO (lhs)->expr = simplified;
3615 VN_INFO (lhs)->has_constants = true;
3616 changed = set_ssa_val_to (lhs, simplified);
3617 if (gimple_vdef (stmt))
3618 changed |= set_ssa_val_to (gimple_vdef (stmt),
3619 gimple_vuse (stmt));
3620 goto done;
3622 else if (simplified
3623 && TREE_CODE (simplified) == SSA_NAME)
3625 changed = visit_copy (lhs, simplified);
3626 if (gimple_vdef (stmt))
3627 changed |= set_ssa_val_to (gimple_vdef (stmt),
3628 gimple_vuse (stmt));
3629 goto done;
3631 else
3633 if (stmt_has_constants (stmt))
3634 VN_INFO (lhs)->has_constants = true;
3635 else
3637 /* We reset expr and constantness here because we may
3638 have been value numbering optimistically, and
3639 iterating. They may become non-constant in this case,
3640 even if they were optimistically constant. */
3641 VN_INFO (lhs)->has_constants = false;
3642 VN_INFO (lhs)->expr = NULL_TREE;
3645 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3647 changed = defs_to_varying (stmt);
3648 goto done;
3653 if (!gimple_call_internal_p (stmt)
3654 && (/* Calls to the same function with the same vuse
3655 and the same operands do not necessarily return the same
3656 value, unless they're pure or const. */
3657 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3658 /* If calls have a vdef, subsequent calls won't have
3659 the same incoming vuse. So, if 2 calls with vdef have the
3660 same vuse, we know they're not subsequent.
3661 We can value number 2 calls to the same function with the
3662 same vuse and the same operands which are not subsequent
3663 the same, because there is no code in the program that can
3664 compare the 2 values... */
3665 || (gimple_vdef (stmt)
3666 /* ... unless the call returns a pointer which does
3667 not alias with anything else. In which case the
3668 information that the values are distinct are encoded
3669 in the IL. */
3670 && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS)
3671 /* Only perform the following when being called from PRE
3672 which embeds tail merging. */
3673 && default_vn_walk_kind == VN_WALK)))
3674 changed = visit_reference_op_call (lhs, call_stmt);
3675 else
3676 changed = defs_to_varying (stmt);
3678 else
3679 changed = defs_to_varying (stmt);
3681 done:
3682 return changed;
3685 /* Compare two operands by reverse postorder index */
3687 static int
3688 compare_ops (const void *pa, const void *pb)
3690 const tree opa = *((const tree *)pa);
3691 const tree opb = *((const tree *)pb);
3692 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3693 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3694 basic_block bba;
3695 basic_block bbb;
3697 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3698 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3699 else if (gimple_nop_p (opstmta))
3700 return -1;
3701 else if (gimple_nop_p (opstmtb))
3702 return 1;
3704 bba = gimple_bb (opstmta);
3705 bbb = gimple_bb (opstmtb);
3707 if (!bba && !bbb)
3708 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3709 else if (!bba)
3710 return -1;
3711 else if (!bbb)
3712 return 1;
3714 if (bba == bbb)
3716 if (gimple_code (opstmta) == GIMPLE_PHI
3717 && gimple_code (opstmtb) == GIMPLE_PHI)
3718 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3719 else if (gimple_code (opstmta) == GIMPLE_PHI)
3720 return -1;
3721 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3722 return 1;
3723 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3724 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3725 else
3726 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3728 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3731 /* Sort an array containing members of a strongly connected component
3732 SCC so that the members are ordered by RPO number.
3733 This means that when the sort is complete, iterating through the
3734 array will give you the members in RPO order. */
3736 static void
3737 sort_scc (vec<tree> scc)
3739 scc.qsort (compare_ops);
3742 /* Insert the no longer used nary ONARY to the hash INFO. */
3744 static void
3745 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3747 size_t size = sizeof_vn_nary_op (onary->length);
3748 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3749 &info->nary_obstack);
3750 memcpy (nary, onary, size);
3751 vn_nary_op_insert_into (nary, info->nary, false);
3754 /* Insert the no longer used phi OPHI to the hash INFO. */
3756 static void
3757 copy_phi (vn_phi_t ophi, vn_tables_t info)
3759 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3760 vn_phi_s **slot;
3761 memcpy (phi, ophi, sizeof (*phi));
3762 ophi->phiargs.create (0);
3763 slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT);
3764 gcc_assert (!*slot);
3765 *slot = phi;
3768 /* Insert the no longer used reference OREF to the hash INFO. */
3770 static void
3771 copy_reference (vn_reference_t oref, vn_tables_t info)
3773 vn_reference_t ref;
3774 vn_reference_s **slot;
3775 ref = (vn_reference_t) pool_alloc (info->references_pool);
3776 memcpy (ref, oref, sizeof (*ref));
3777 oref->operands.create (0);
3778 slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT);
3779 if (*slot)
3780 free_reference (*slot);
3781 *slot = ref;
3784 /* Process a strongly connected component in the SSA graph. */
3786 static void
3787 process_scc (vec<tree> scc)
3789 tree var;
3790 unsigned int i;
3791 unsigned int iterations = 0;
3792 bool changed = true;
3793 vn_nary_op_iterator_type hin;
3794 vn_phi_iterator_type hip;
3795 vn_reference_iterator_type hir;
3796 vn_nary_op_t nary;
3797 vn_phi_t phi;
3798 vn_reference_t ref;
3800 /* If the SCC has a single member, just visit it. */
3801 if (scc.length () == 1)
3803 tree use = scc[0];
3804 if (VN_INFO (use)->use_processed)
3805 return;
3806 /* We need to make sure it doesn't form a cycle itself, which can
3807 happen for self-referential PHI nodes. In that case we would
3808 end up inserting an expression with VN_TOP operands into the
3809 valid table which makes us derive bogus equivalences later.
3810 The cheapest way to check this is to assume it for all PHI nodes. */
3811 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3812 /* Fallthru to iteration. */ ;
3813 else
3815 visit_use (use);
3816 return;
3820 if (dump_file && (dump_flags & TDF_DETAILS))
3821 print_scc (dump_file, scc);
3823 /* Iterate over the SCC with the optimistic table until it stops
3824 changing. */
3825 current_info = optimistic_info;
3826 while (changed)
3828 changed = false;
3829 iterations++;
3830 if (dump_file && (dump_flags & TDF_DETAILS))
3831 fprintf (dump_file, "Starting iteration %d\n", iterations);
3832 /* As we are value-numbering optimistically we have to
3833 clear the expression tables and the simplified expressions
3834 in each iteration until we converge. */
3835 optimistic_info->nary->empty ();
3836 optimistic_info->phis->empty ();
3837 optimistic_info->references->empty ();
3838 obstack_free (&optimistic_info->nary_obstack, NULL);
3839 gcc_obstack_init (&optimistic_info->nary_obstack);
3840 empty_alloc_pool (optimistic_info->phis_pool);
3841 empty_alloc_pool (optimistic_info->references_pool);
3842 FOR_EACH_VEC_ELT (scc, i, var)
3843 VN_INFO (var)->expr = NULL_TREE;
3844 FOR_EACH_VEC_ELT (scc, i, var)
3845 changed |= visit_use (var);
3848 if (dump_file && (dump_flags & TDF_DETAILS))
3849 fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations);
3850 statistics_histogram_event (cfun, "SCC iterations", iterations);
3852 /* Finally, copy the contents of the no longer used optimistic
3853 table to the valid table. */
3854 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin)
3855 copy_nary (nary, valid_info);
3856 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip)
3857 copy_phi (phi, valid_info);
3858 FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references,
3859 ref, vn_reference_t, hir)
3860 copy_reference (ref, valid_info);
3862 current_info = valid_info;
3866 /* Pop the components of the found SCC for NAME off the SCC stack
3867 and process them. Returns true if all went well, false if
3868 we run into resource limits. */
3870 static bool
3871 extract_and_process_scc_for_name (tree name)
3873 auto_vec<tree> scc;
3874 tree x;
3876 /* Found an SCC, pop the components off the SCC stack and
3877 process them. */
3880 x = sccstack.pop ();
3882 VN_INFO (x)->on_sccstack = false;
3883 scc.safe_push (x);
3884 } while (x != name);
3886 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3887 if (scc.length ()
3888 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3890 if (dump_file)
3891 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3892 "SCC size %u exceeding %u\n", scc.length (),
3893 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3895 return false;
3898 if (scc.length () > 1)
3899 sort_scc (scc);
3901 process_scc (scc);
3903 return true;
3906 /* Depth first search on NAME to discover and process SCC's in the SSA
3907 graph.
3908 Execution of this algorithm relies on the fact that the SCC's are
3909 popped off the stack in topological order.
3910 Returns true if successful, false if we stopped processing SCC's due
3911 to resource constraints. */
3913 static bool
3914 DFS (tree name)
3916 vec<ssa_op_iter> itervec = vNULL;
3917 vec<tree> namevec = vNULL;
3918 use_operand_p usep = NULL;
3919 gimple defstmt;
3920 tree use;
3921 ssa_op_iter iter;
3923 start_over:
3924 /* SCC info */
3925 VN_INFO (name)->dfsnum = next_dfs_num++;
3926 VN_INFO (name)->visited = true;
3927 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3929 sccstack.safe_push (name);
3930 VN_INFO (name)->on_sccstack = true;
3931 defstmt = SSA_NAME_DEF_STMT (name);
3933 /* Recursively DFS on our operands, looking for SCC's. */
3934 if (!gimple_nop_p (defstmt))
3936 /* Push a new iterator. */
3937 if (gimple_code (defstmt) == GIMPLE_PHI)
3938 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3939 else
3940 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3942 else
3943 clear_and_done_ssa_iter (&iter);
3945 while (1)
3947 /* If we are done processing uses of a name, go up the stack
3948 of iterators and process SCCs as we found them. */
3949 if (op_iter_done (&iter))
3951 /* See if we found an SCC. */
3952 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3953 if (!extract_and_process_scc_for_name (name))
3955 namevec.release ();
3956 itervec.release ();
3957 return false;
3960 /* Check if we are done. */
3961 if (namevec.is_empty ())
3963 namevec.release ();
3964 itervec.release ();
3965 return true;
3968 /* Restore the last use walker and continue walking there. */
3969 use = name;
3970 name = namevec.pop ();
3971 memcpy (&iter, &itervec.last (),
3972 sizeof (ssa_op_iter));
3973 itervec.pop ();
3974 goto continue_walking;
3977 use = USE_FROM_PTR (usep);
3979 /* Since we handle phi nodes, we will sometimes get
3980 invariants in the use expression. */
3981 if (TREE_CODE (use) == SSA_NAME)
3983 if (! (VN_INFO (use)->visited))
3985 /* Recurse by pushing the current use walking state on
3986 the stack and starting over. */
3987 itervec.safe_push (iter);
3988 namevec.safe_push (name);
3989 name = use;
3990 goto start_over;
3992 continue_walking:
3993 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3994 VN_INFO (use)->low);
3996 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3997 && VN_INFO (use)->on_sccstack)
3999 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
4000 VN_INFO (name)->low);
4004 usep = op_iter_next_use (&iter);
4008 /* Allocate a value number table. */
4010 static void
4011 allocate_vn_table (vn_tables_t table)
4013 table->phis = new vn_phi_table_type (23);
4014 table->nary = new vn_nary_op_table_type (23);
4015 table->references = new vn_reference_table_type (23);
4017 gcc_obstack_init (&table->nary_obstack);
4018 table->phis_pool = create_alloc_pool ("VN phis",
4019 sizeof (struct vn_phi_s),
4020 30);
4021 table->references_pool = create_alloc_pool ("VN references",
4022 sizeof (struct vn_reference_s),
4023 30);
4026 /* Free a value number table. */
4028 static void
4029 free_vn_table (vn_tables_t table)
4031 delete table->phis;
4032 table->phis = NULL;
4033 delete table->nary;
4034 table->nary = NULL;
4035 delete table->references;
4036 table->references = NULL;
4037 obstack_free (&table->nary_obstack, NULL);
4038 free_alloc_pool (table->phis_pool);
4039 free_alloc_pool (table->references_pool);
4042 static void
4043 init_scc_vn (void)
4045 size_t i;
4046 int j;
4047 int *rpo_numbers_temp;
4049 calculate_dominance_info (CDI_DOMINATORS);
4050 sccstack.create (0);
4051 constant_to_value_id = new hash_table<vn_constant_hasher> (23);
4053 constant_value_ids = BITMAP_ALLOC (NULL);
4055 next_dfs_num = 1;
4056 next_value_id = 1;
4058 vn_ssa_aux_table.create (num_ssa_names + 1);
4059 /* VEC_alloc doesn't actually grow it to the right size, it just
4060 preallocates the space to do so. */
4061 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
4062 gcc_obstack_init (&vn_ssa_aux_obstack);
4064 shared_lookup_phiargs.create (0);
4065 shared_lookup_references.create (0);
4066 rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun));
4067 rpo_numbers_temp =
4068 XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
4069 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
4071 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
4072 the i'th block in RPO order is bb. We want to map bb's to RPO
4073 numbers, so we need to rearrange this array. */
4074 for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++)
4075 rpo_numbers[rpo_numbers_temp[j]] = j;
4077 XDELETE (rpo_numbers_temp);
4079 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
4081 /* Create the VN_INFO structures, and initialize value numbers to
4082 TOP. */
4083 for (i = 0; i < num_ssa_names; i++)
4085 tree name = ssa_name (i);
4086 if (name)
4088 VN_INFO_GET (name)->valnum = VN_TOP;
4089 VN_INFO (name)->expr = NULL_TREE;
4090 VN_INFO (name)->value_id = 0;
4094 renumber_gimple_stmt_uids ();
4096 /* Create the valid and optimistic value numbering tables. */
4097 valid_info = XCNEW (struct vn_tables_s);
4098 allocate_vn_table (valid_info);
4099 optimistic_info = XCNEW (struct vn_tables_s);
4100 allocate_vn_table (optimistic_info);
4103 void
4104 free_scc_vn (void)
4106 size_t i;
4108 delete constant_to_value_id;
4109 constant_to_value_id = NULL;
4110 BITMAP_FREE (constant_value_ids);
4111 shared_lookup_phiargs.release ();
4112 shared_lookup_references.release ();
4113 XDELETEVEC (rpo_numbers);
4115 for (i = 0; i < num_ssa_names; i++)
4117 tree name = ssa_name (i);
4118 if (name
4119 && VN_INFO (name)->needs_insertion)
4120 release_ssa_name (name);
4122 obstack_free (&vn_ssa_aux_obstack, NULL);
4123 vn_ssa_aux_table.release ();
4125 sccstack.release ();
4126 free_vn_table (valid_info);
4127 XDELETE (valid_info);
4128 free_vn_table (optimistic_info);
4129 XDELETE (optimistic_info);
4132 /* Set *ID according to RESULT. */
4134 static void
4135 set_value_id_for_result (tree result, unsigned int *id)
4137 if (result && TREE_CODE (result) == SSA_NAME)
4138 *id = VN_INFO (result)->value_id;
4139 else if (result && is_gimple_min_invariant (result))
4140 *id = get_or_alloc_constant_value_id (result);
4141 else
4142 *id = get_next_value_id ();
4145 /* Set the value ids in the valid hash tables. */
4147 static void
4148 set_hashtable_value_ids (void)
4150 vn_nary_op_iterator_type hin;
4151 vn_phi_iterator_type hip;
4152 vn_reference_iterator_type hir;
4153 vn_nary_op_t vno;
4154 vn_reference_t vr;
4155 vn_phi_t vp;
4157 /* Now set the value ids of the things we had put in the hash
4158 table. */
4160 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin)
4161 set_value_id_for_result (vno->result, &vno->value_id);
4163 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip)
4164 set_value_id_for_result (vp->result, &vp->value_id);
4166 FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t,
4167 hir)
4168 set_value_id_for_result (vr->result, &vr->value_id);
4171 class cond_dom_walker : public dom_walker
4173 public:
4174 cond_dom_walker () : dom_walker (CDI_DOMINATORS), fail (false) {}
4176 virtual void before_dom_children (basic_block);
4178 bool fail;
4181 void
4182 cond_dom_walker::before_dom_children (basic_block bb)
4184 edge e;
4185 edge_iterator ei;
4187 if (fail)
4188 return;
4190 /* If any of the predecessor edges that do not come from blocks dominated
4191 by us are still marked as possibly executable consider this block
4192 reachable. */
4193 bool reachable = bb == ENTRY_BLOCK_PTR_FOR_FN (cfun);
4194 FOR_EACH_EDGE (e, ei, bb->preds)
4195 if (!dominated_by_p (CDI_DOMINATORS, e->src, bb))
4196 reachable |= (e->flags & EDGE_EXECUTABLE);
4198 /* If the block is not reachable all outgoing edges are not
4199 executable. */
4200 if (!reachable)
4202 if (dump_file && (dump_flags & TDF_DETAILS))
4203 fprintf (dump_file, "Marking all outgoing edges of unreachable "
4204 "BB %d as not executable\n", bb->index);
4206 FOR_EACH_EDGE (e, ei, bb->succs)
4207 e->flags &= ~EDGE_EXECUTABLE;
4208 return;
4211 gimple stmt = last_stmt (bb);
4212 if (!stmt)
4213 return;
4215 enum gimple_code code = gimple_code (stmt);
4216 if (code != GIMPLE_COND
4217 && code != GIMPLE_SWITCH
4218 && code != GIMPLE_GOTO)
4219 return;
4221 if (dump_file && (dump_flags & TDF_DETAILS))
4223 fprintf (dump_file, "Value-numbering operands of stmt ending BB %d: ",
4224 bb->index);
4225 print_gimple_stmt (dump_file, stmt, 0, 0);
4228 /* Value-number the last stmts SSA uses. */
4229 ssa_op_iter i;
4230 tree op;
4231 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
4232 if (VN_INFO (op)->visited == false
4233 && !DFS (op))
4235 fail = true;
4236 return;
4239 /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges
4240 if value-numbering can prove they are not reachable. Handling
4241 computed gotos is also possible. */
4242 tree val;
4243 switch (code)
4245 case GIMPLE_COND:
4247 tree lhs = gimple_cond_lhs (stmt);
4248 tree rhs = gimple_cond_rhs (stmt);
4249 /* Work hard in computing the condition and take into account
4250 the valueization of the defining stmt. */
4251 if (TREE_CODE (lhs) == SSA_NAME)
4252 lhs = vn_get_expr_for (lhs);
4253 if (TREE_CODE (rhs) == SSA_NAME)
4254 rhs = vn_get_expr_for (rhs);
4255 val = fold_binary (gimple_cond_code (stmt),
4256 boolean_type_node, lhs, rhs);
4257 break;
4259 case GIMPLE_SWITCH:
4260 val = gimple_switch_index (as_a <gimple_switch> (stmt));
4261 break;
4262 case GIMPLE_GOTO:
4263 val = gimple_goto_dest (stmt);
4264 break;
4265 default:
4266 gcc_unreachable ();
4268 if (!val)
4269 return;
4271 edge taken = find_taken_edge (bb, vn_valueize (val));
4272 if (!taken)
4273 return;
4275 if (dump_file && (dump_flags & TDF_DETAILS))
4276 fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as "
4277 "not executable\n", bb->index, bb->index, taken->dest->index);
4279 FOR_EACH_EDGE (e, ei, bb->succs)
4280 if (e != taken)
4281 e->flags &= ~EDGE_EXECUTABLE;
4284 /* Do SCCVN. Returns true if it finished, false if we bailed out
4285 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4286 how we use the alias oracle walking during the VN process. */
4288 bool
4289 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4291 basic_block bb;
4292 size_t i;
4293 tree param;
4295 default_vn_walk_kind = default_vn_walk_kind_;
4297 init_scc_vn ();
4298 current_info = valid_info;
4300 for (param = DECL_ARGUMENTS (current_function_decl);
4301 param;
4302 param = DECL_CHAIN (param))
4304 tree def = ssa_default_def (cfun, param);
4305 if (def)
4307 VN_INFO (def)->visited = true;
4308 VN_INFO (def)->valnum = def;
4312 /* Mark all edges as possibly executable. */
4313 FOR_ALL_BB_FN (bb, cfun)
4315 edge_iterator ei;
4316 edge e;
4317 FOR_EACH_EDGE (e, ei, bb->succs)
4318 e->flags |= EDGE_EXECUTABLE;
4321 /* Walk all blocks in dominator order, value-numbering the last stmts
4322 SSA uses and decide whether outgoing edges are not executable. */
4323 cond_dom_walker walker;
4324 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4325 if (walker.fail)
4327 free_scc_vn ();
4328 return false;
4331 /* Value-number remaining SSA names. */
4332 for (i = 1; i < num_ssa_names; ++i)
4334 tree name = ssa_name (i);
4335 if (name
4336 && VN_INFO (name)->visited == false
4337 && !has_zero_uses (name))
4338 if (!DFS (name))
4340 free_scc_vn ();
4341 return false;
4345 /* Initialize the value ids. */
4347 for (i = 1; i < num_ssa_names; ++i)
4349 tree name = ssa_name (i);
4350 vn_ssa_aux_t info;
4351 if (!name)
4352 continue;
4353 info = VN_INFO (name);
4354 if (info->valnum == name
4355 || info->valnum == VN_TOP)
4356 info->value_id = get_next_value_id ();
4357 else if (is_gimple_min_invariant (info->valnum))
4358 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4361 /* Propagate. */
4362 for (i = 1; i < num_ssa_names; ++i)
4364 tree name = ssa_name (i);
4365 vn_ssa_aux_t info;
4366 if (!name)
4367 continue;
4368 info = VN_INFO (name);
4369 if (TREE_CODE (info->valnum) == SSA_NAME
4370 && info->valnum != name
4371 && info->value_id != VN_INFO (info->valnum)->value_id)
4372 info->value_id = VN_INFO (info->valnum)->value_id;
4375 set_hashtable_value_ids ();
4377 if (dump_file && (dump_flags & TDF_DETAILS))
4379 fprintf (dump_file, "Value numbers:\n");
4380 for (i = 0; i < num_ssa_names; i++)
4382 tree name = ssa_name (i);
4383 if (name
4384 && VN_INFO (name)->visited
4385 && SSA_VAL (name) != name)
4387 print_generic_expr (dump_file, name, 0);
4388 fprintf (dump_file, " = ");
4389 print_generic_expr (dump_file, SSA_VAL (name), 0);
4390 fprintf (dump_file, "\n");
4395 return true;
4398 /* Return the maximum value id we have ever seen. */
4400 unsigned int
4401 get_max_value_id (void)
4403 return next_value_id;
4406 /* Return the next unique value id. */
4408 unsigned int
4409 get_next_value_id (void)
4411 return next_value_id++;
4415 /* Compare two expressions E1 and E2 and return true if they are equal. */
4417 bool
4418 expressions_equal_p (tree e1, tree e2)
4420 /* The obvious case. */
4421 if (e1 == e2)
4422 return true;
4424 /* If only one of them is null, they cannot be equal. */
4425 if (!e1 || !e2)
4426 return false;
4428 /* Now perform the actual comparison. */
4429 if (TREE_CODE (e1) == TREE_CODE (e2)
4430 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4431 return true;
4433 return false;
4437 /* Return true if the nary operation NARY may trap. This is a copy
4438 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4440 bool
4441 vn_nary_may_trap (vn_nary_op_t nary)
4443 tree type;
4444 tree rhs2 = NULL_TREE;
4445 bool honor_nans = false;
4446 bool honor_snans = false;
4447 bool fp_operation = false;
4448 bool honor_trapv = false;
4449 bool handled, ret;
4450 unsigned i;
4452 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4453 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4454 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4456 type = nary->type;
4457 fp_operation = FLOAT_TYPE_P (type);
4458 if (fp_operation)
4460 honor_nans = flag_trapping_math && !flag_finite_math_only;
4461 honor_snans = flag_signaling_nans != 0;
4463 else if (INTEGRAL_TYPE_P (type)
4464 && TYPE_OVERFLOW_TRAPS (type))
4465 honor_trapv = true;
4467 if (nary->length >= 2)
4468 rhs2 = nary->op[1];
4469 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4470 honor_trapv,
4471 honor_nans, honor_snans, rhs2,
4472 &handled);
4473 if (handled
4474 && ret)
4475 return true;
4477 for (i = 0; i < nary->length; ++i)
4478 if (tree_could_trap_p (nary->op[i]))
4479 return true;
4481 return false;