2013-05-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-sccvn.c
blob49d61b0c239647f4ffcd598f1078f0cb5955c1fc
1 /* SCC value numbering for trees
2 Copyright (C) 2006-2013 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 "basic-block.h"
27 #include "gimple-pretty-print.h"
28 #include "tree-inline.h"
29 #include "tree-flow.h"
30 #include "gimple.h"
31 #include "dumpfile.h"
32 #include "hash-table.h"
33 #include "alloc-pool.h"
34 #include "flags.h"
35 #include "bitmap.h"
36 #include "cfgloop.h"
37 #include "params.h"
38 #include "tree-ssa-propagate.h"
39 #include "tree-ssa-sccvn.h"
40 #include "gimple-fold.h"
42 /* This algorithm is based on the SCC algorithm presented by Keith
43 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
44 (http://citeseer.ist.psu.edu/41805.html). In
45 straight line code, it is equivalent to a regular hash based value
46 numbering that is performed in reverse postorder.
48 For code with cycles, there are two alternatives, both of which
49 require keeping the hashtables separate from the actual list of
50 value numbers for SSA names.
52 1. Iterate value numbering in an RPO walk of the blocks, removing
53 all the entries from the hashtable after each iteration (but
54 keeping the SSA name->value number mapping between iterations).
55 Iterate until it does not change.
57 2. Perform value numbering as part of an SCC walk on the SSA graph,
58 iterating only the cycles in the SSA graph until they do not change
59 (using a separate, optimistic hashtable for value numbering the SCC
60 operands).
62 The second is not just faster in practice (because most SSA graph
63 cycles do not involve all the variables in the graph), it also has
64 some nice properties.
66 One of these nice properties is that when we pop an SCC off the
67 stack, we are guaranteed to have processed all the operands coming from
68 *outside of that SCC*, so we do not need to do anything special to
69 ensure they have value numbers.
71 Another nice property is that the SCC walk is done as part of a DFS
72 of the SSA graph, which makes it easy to perform combining and
73 simplifying operations at the same time.
75 The code below is deliberately written in a way that makes it easy
76 to separate the SCC walk from the other work it does.
78 In order to propagate constants through the code, we track which
79 expressions contain constants, and use those while folding. In
80 theory, we could also track expressions whose value numbers are
81 replaced, in case we end up folding based on expression
82 identities.
84 In order to value number memory, we assign value numbers to vuses.
85 This enables us to note that, for example, stores to the same
86 address of the same value from the same starting memory states are
87 equivalent.
88 TODO:
90 1. We can iterate only the changing portions of the SCC's, but
91 I have not seen an SCC big enough for this to be a win.
92 2. If you differentiate between phi nodes for loops and phi nodes
93 for if-then-else, you can properly consider phi nodes in different
94 blocks for equivalence.
95 3. We could value number vuses in more cases, particularly, whole
96 structure copies.
100 /* vn_nary_op hashtable helpers. */
102 struct vn_nary_op_hasher : typed_noop_remove <vn_nary_op_s>
104 typedef vn_nary_op_s value_type;
105 typedef vn_nary_op_s compare_type;
106 static inline hashval_t hash (const value_type *);
107 static inline bool equal (const value_type *, const compare_type *);
110 /* Return the computed hashcode for nary operation P1. */
112 inline hashval_t
113 vn_nary_op_hasher::hash (const value_type *vno1)
115 return vno1->hashcode;
118 /* Compare nary operations P1 and P2 and return true if they are
119 equivalent. */
121 inline bool
122 vn_nary_op_hasher::equal (const value_type *vno1, const compare_type *vno2)
124 return vn_nary_op_eq (vno1, vno2);
127 typedef hash_table <vn_nary_op_hasher> vn_nary_op_table_type;
128 typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type;
131 /* vn_phi hashtable helpers. */
133 static int
134 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2);
136 struct vn_phi_hasher
138 typedef vn_phi_s value_type;
139 typedef vn_phi_s compare_type;
140 static inline hashval_t hash (const value_type *);
141 static inline bool equal (const value_type *, const compare_type *);
142 static inline void remove (value_type *);
145 /* Return the computed hashcode for phi operation P1. */
147 inline hashval_t
148 vn_phi_hasher::hash (const value_type *vp1)
150 return vp1->hashcode;
153 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
155 inline bool
156 vn_phi_hasher::equal (const value_type *vp1, const compare_type *vp2)
158 return vn_phi_eq (vp1, vp2);
161 /* Free a phi operation structure VP. */
163 inline void
164 vn_phi_hasher::remove (value_type *phi)
166 phi->phiargs.release ();
169 typedef hash_table <vn_phi_hasher> vn_phi_table_type;
170 typedef vn_phi_table_type::iterator vn_phi_iterator_type;
173 /* Compare two reference operands P1 and P2 for equality. Return true if
174 they are equal, and false otherwise. */
176 static int
177 vn_reference_op_eq (const void *p1, const void *p2)
179 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
180 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
182 return (vro1->opcode == vro2->opcode
183 /* We do not care for differences in type qualification. */
184 && (vro1->type == vro2->type
185 || (vro1->type && vro2->type
186 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
187 TYPE_MAIN_VARIANT (vro2->type))))
188 && expressions_equal_p (vro1->op0, vro2->op0)
189 && expressions_equal_p (vro1->op1, vro2->op1)
190 && expressions_equal_p (vro1->op2, vro2->op2));
193 /* Free a reference operation structure VP. */
195 static inline void
196 free_reference (vn_reference_s *vr)
198 vr->operands.release ();
202 /* vn_reference hashtable helpers. */
204 struct vn_reference_hasher
206 typedef vn_reference_s value_type;
207 typedef vn_reference_s compare_type;
208 static inline hashval_t hash (const value_type *);
209 static inline bool equal (const value_type *, const compare_type *);
210 static inline void remove (value_type *);
213 /* Return the hashcode for a given reference operation P1. */
215 inline hashval_t
216 vn_reference_hasher::hash (const value_type *vr1)
218 return vr1->hashcode;
221 inline bool
222 vn_reference_hasher::equal (const value_type *v, const compare_type *c)
224 return vn_reference_eq (v, c);
227 inline void
228 vn_reference_hasher::remove (value_type *v)
230 free_reference (v);
233 typedef hash_table <vn_reference_hasher> vn_reference_table_type;
234 typedef vn_reference_table_type::iterator vn_reference_iterator_type;
237 /* The set of hashtables and alloc_pool's for their items. */
239 typedef struct vn_tables_s
241 vn_nary_op_table_type nary;
242 vn_phi_table_type phis;
243 vn_reference_table_type references;
244 struct obstack nary_obstack;
245 alloc_pool phis_pool;
246 alloc_pool references_pool;
247 } *vn_tables_t;
250 /* vn_constant hashtable helpers. */
252 struct vn_constant_hasher : typed_free_remove <vn_constant_s>
254 typedef vn_constant_s value_type;
255 typedef vn_constant_s compare_type;
256 static inline hashval_t hash (const value_type *);
257 static inline bool equal (const value_type *, const compare_type *);
260 /* Hash table hash function for vn_constant_t. */
262 inline hashval_t
263 vn_constant_hasher::hash (const value_type *vc1)
265 return vc1->hashcode;
268 /* Hash table equality function for vn_constant_t. */
270 inline bool
271 vn_constant_hasher::equal (const value_type *vc1, const compare_type *vc2)
273 if (vc1->hashcode != vc2->hashcode)
274 return false;
276 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
279 static hash_table <vn_constant_hasher> constant_to_value_id;
280 static bitmap constant_value_ids;
283 /* Valid hashtables storing information we have proven to be
284 correct. */
286 static vn_tables_t valid_info;
288 /* Optimistic hashtables storing information we are making assumptions about
289 during iterations. */
291 static vn_tables_t optimistic_info;
293 /* Pointer to the set of hashtables that is currently being used.
294 Should always point to either the optimistic_info, or the
295 valid_info. */
297 static vn_tables_t current_info;
300 /* Reverse post order index for each basic block. */
302 static int *rpo_numbers;
304 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
306 /* This represents the top of the VN lattice, which is the universal
307 value. */
309 tree VN_TOP;
311 /* Unique counter for our value ids. */
313 static unsigned int next_value_id;
315 /* Next DFS number and the stack for strongly connected component
316 detection. */
318 static unsigned int next_dfs_num;
319 static vec<tree> sccstack;
323 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
324 are allocated on an obstack for locality reasons, and to free them
325 without looping over the vec. */
327 static vec<vn_ssa_aux_t> vn_ssa_aux_table;
328 static struct obstack vn_ssa_aux_obstack;
330 /* Return the value numbering information for a given SSA name. */
332 vn_ssa_aux_t
333 VN_INFO (tree name)
335 vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)];
336 gcc_checking_assert (res);
337 return res;
340 /* Set the value numbering info for a given SSA name to a given
341 value. */
343 static inline void
344 VN_INFO_SET (tree name, vn_ssa_aux_t value)
346 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value;
349 /* Initialize the value numbering info for a given SSA name.
350 This should be called just once for every SSA name. */
352 vn_ssa_aux_t
353 VN_INFO_GET (tree name)
355 vn_ssa_aux_t newinfo;
357 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
358 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
359 if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ())
360 vn_ssa_aux_table.safe_grow (SSA_NAME_VERSION (name) + 1);
361 vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo;
362 return newinfo;
366 /* Get the representative expression for the SSA_NAME NAME. Returns
367 the representative SSA_NAME if there is no expression associated with it. */
369 tree
370 vn_get_expr_for (tree name)
372 vn_ssa_aux_t vn = VN_INFO (name);
373 gimple def_stmt;
374 tree expr = NULL_TREE;
375 enum tree_code code;
377 if (vn->valnum == VN_TOP)
378 return name;
380 /* If the value-number is a constant it is the representative
381 expression. */
382 if (TREE_CODE (vn->valnum) != SSA_NAME)
383 return vn->valnum;
385 /* Get to the information of the value of this SSA_NAME. */
386 vn = VN_INFO (vn->valnum);
388 /* If the value-number is a constant it is the representative
389 expression. */
390 if (TREE_CODE (vn->valnum) != SSA_NAME)
391 return vn->valnum;
393 /* Else if we have an expression, return it. */
394 if (vn->expr != NULL_TREE)
395 return vn->expr;
397 /* Otherwise use the defining statement to build the expression. */
398 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
400 /* If the value number is not an assignment use it directly. */
401 if (!is_gimple_assign (def_stmt))
402 return vn->valnum;
404 /* FIXME tuples. This is incomplete and likely will miss some
405 simplifications. */
406 code = gimple_assign_rhs_code (def_stmt);
407 switch (TREE_CODE_CLASS (code))
409 case tcc_reference:
410 if ((code == REALPART_EXPR
411 || code == IMAGPART_EXPR
412 || code == VIEW_CONVERT_EXPR)
413 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
414 0)) == SSA_NAME)
415 expr = fold_build1 (code,
416 gimple_expr_type (def_stmt),
417 TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
418 break;
420 case tcc_unary:
421 expr = fold_build1 (code,
422 gimple_expr_type (def_stmt),
423 gimple_assign_rhs1 (def_stmt));
424 break;
426 case tcc_binary:
427 expr = fold_build2 (code,
428 gimple_expr_type (def_stmt),
429 gimple_assign_rhs1 (def_stmt),
430 gimple_assign_rhs2 (def_stmt));
431 break;
433 case tcc_exceptional:
434 if (code == CONSTRUCTOR
435 && TREE_CODE
436 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
437 expr = gimple_assign_rhs1 (def_stmt);
438 break;
440 default:;
442 if (expr == NULL_TREE)
443 return vn->valnum;
445 /* Cache the expression. */
446 vn->expr = expr;
448 return expr;
451 /* Return the vn_kind the expression computed by the stmt should be
452 associated with. */
454 enum vn_kind
455 vn_get_stmt_kind (gimple stmt)
457 switch (gimple_code (stmt))
459 case GIMPLE_CALL:
460 return VN_REFERENCE;
461 case GIMPLE_PHI:
462 return VN_PHI;
463 case GIMPLE_ASSIGN:
465 enum tree_code code = gimple_assign_rhs_code (stmt);
466 tree rhs1 = gimple_assign_rhs1 (stmt);
467 switch (get_gimple_rhs_class (code))
469 case GIMPLE_UNARY_RHS:
470 case GIMPLE_BINARY_RHS:
471 case GIMPLE_TERNARY_RHS:
472 return VN_NARY;
473 case GIMPLE_SINGLE_RHS:
474 switch (TREE_CODE_CLASS (code))
476 case tcc_reference:
477 /* VOP-less references can go through unary case. */
478 if ((code == REALPART_EXPR
479 || code == IMAGPART_EXPR
480 || code == VIEW_CONVERT_EXPR
481 || code == BIT_FIELD_REF)
482 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
483 return VN_NARY;
485 /* Fallthrough. */
486 case tcc_declaration:
487 return VN_REFERENCE;
489 case tcc_constant:
490 return VN_CONSTANT;
492 default:
493 if (code == ADDR_EXPR)
494 return (is_gimple_min_invariant (rhs1)
495 ? VN_CONSTANT : VN_REFERENCE);
496 else if (code == CONSTRUCTOR)
497 return VN_NARY;
498 return VN_NONE;
500 default:
501 return VN_NONE;
504 default:
505 return VN_NONE;
509 /* Lookup a value id for CONSTANT and return it. If it does not
510 exist returns 0. */
512 unsigned int
513 get_constant_value_id (tree constant)
515 vn_constant_s **slot;
516 struct vn_constant_s vc;
518 vc.hashcode = vn_hash_constant_with_type (constant);
519 vc.constant = constant;
520 slot = constant_to_value_id.find_slot_with_hash (&vc, vc.hashcode, NO_INSERT);
521 if (slot)
522 return (*slot)->value_id;
523 return 0;
526 /* Lookup a value id for CONSTANT, and if it does not exist, create a
527 new one and return it. If it does exist, return it. */
529 unsigned int
530 get_or_alloc_constant_value_id (tree constant)
532 vn_constant_s **slot;
533 struct vn_constant_s vc;
534 vn_constant_t vcp;
536 vc.hashcode = vn_hash_constant_with_type (constant);
537 vc.constant = constant;
538 slot = constant_to_value_id.find_slot_with_hash (&vc, vc.hashcode, INSERT);
539 if (*slot)
540 return (*slot)->value_id;
542 vcp = XNEW (struct vn_constant_s);
543 vcp->hashcode = vc.hashcode;
544 vcp->constant = constant;
545 vcp->value_id = get_next_value_id ();
546 *slot = vcp;
547 bitmap_set_bit (constant_value_ids, vcp->value_id);
548 return vcp->value_id;
551 /* Return true if V is a value id for a constant. */
553 bool
554 value_id_constant_p (unsigned int v)
556 return bitmap_bit_p (constant_value_ids, v);
559 /* Compute the hash for a reference operand VRO1. */
561 static hashval_t
562 vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
564 result = iterative_hash_hashval_t (vro1->opcode, result);
565 if (vro1->op0)
566 result = iterative_hash_expr (vro1->op0, result);
567 if (vro1->op1)
568 result = iterative_hash_expr (vro1->op1, result);
569 if (vro1->op2)
570 result = iterative_hash_expr (vro1->op2, result);
571 return result;
574 /* Compute a hash for the reference operation VR1 and return it. */
576 hashval_t
577 vn_reference_compute_hash (const vn_reference_t vr1)
579 hashval_t result = 0;
580 int i;
581 vn_reference_op_t vro;
582 HOST_WIDE_INT off = -1;
583 bool deref = false;
585 FOR_EACH_VEC_ELT (vr1->operands, i, vro)
587 if (vro->opcode == MEM_REF)
588 deref = true;
589 else if (vro->opcode != ADDR_EXPR)
590 deref = false;
591 if (vro->off != -1)
593 if (off == -1)
594 off = 0;
595 off += vro->off;
597 else
599 if (off != -1
600 && off != 0)
601 result = iterative_hash_hashval_t (off, result);
602 off = -1;
603 if (deref
604 && vro->opcode == ADDR_EXPR)
606 if (vro->op0)
608 tree op = TREE_OPERAND (vro->op0, 0);
609 result = iterative_hash_hashval_t (TREE_CODE (op), result);
610 result = iterative_hash_expr (op, result);
613 else
614 result = vn_reference_op_compute_hash (vro, result);
617 if (vr1->vuse)
618 result += SSA_NAME_VERSION (vr1->vuse);
620 return result;
623 /* Return true if reference operations VR1 and VR2 are equivalent. This
624 means they have the same set of operands and vuses. */
626 bool
627 vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2)
629 unsigned i, j;
631 if (vr1->hashcode != vr2->hashcode)
632 return false;
634 /* Early out if this is not a hash collision. */
635 if (vr1->hashcode != vr2->hashcode)
636 return false;
638 /* The VOP needs to be the same. */
639 if (vr1->vuse != vr2->vuse)
640 return false;
642 /* If the operands are the same we are done. */
643 if (vr1->operands == vr2->operands)
644 return true;
646 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
647 return false;
649 if (INTEGRAL_TYPE_P (vr1->type)
650 && INTEGRAL_TYPE_P (vr2->type))
652 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
653 return false;
655 else if (INTEGRAL_TYPE_P (vr1->type)
656 && (TYPE_PRECISION (vr1->type)
657 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
658 return false;
659 else if (INTEGRAL_TYPE_P (vr2->type)
660 && (TYPE_PRECISION (vr2->type)
661 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
662 return false;
664 i = 0;
665 j = 0;
668 HOST_WIDE_INT off1 = 0, off2 = 0;
669 vn_reference_op_t vro1, vro2;
670 vn_reference_op_s tem1, tem2;
671 bool deref1 = false, deref2 = false;
672 for (; vr1->operands.iterate (i, &vro1); i++)
674 if (vro1->opcode == MEM_REF)
675 deref1 = true;
676 if (vro1->off == -1)
677 break;
678 off1 += vro1->off;
680 for (; vr2->operands.iterate (j, &vro2); j++)
682 if (vro2->opcode == MEM_REF)
683 deref2 = true;
684 if (vro2->off == -1)
685 break;
686 off2 += vro2->off;
688 if (off1 != off2)
689 return false;
690 if (deref1 && vro1->opcode == ADDR_EXPR)
692 memset (&tem1, 0, sizeof (tem1));
693 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
694 tem1.type = TREE_TYPE (tem1.op0);
695 tem1.opcode = TREE_CODE (tem1.op0);
696 vro1 = &tem1;
697 deref1 = false;
699 if (deref2 && vro2->opcode == ADDR_EXPR)
701 memset (&tem2, 0, sizeof (tem2));
702 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
703 tem2.type = TREE_TYPE (tem2.op0);
704 tem2.opcode = TREE_CODE (tem2.op0);
705 vro2 = &tem2;
706 deref2 = false;
708 if (deref1 != deref2)
709 return false;
710 if (!vn_reference_op_eq (vro1, vro2))
711 return false;
712 ++j;
713 ++i;
715 while (vr1->operands.length () != i
716 || vr2->operands.length () != j);
718 return true;
721 /* Copy the operations present in load/store REF into RESULT, a vector of
722 vn_reference_op_s's. */
724 void
725 copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result)
727 if (TREE_CODE (ref) == TARGET_MEM_REF)
729 vn_reference_op_s temp;
731 result->reserve (3);
733 memset (&temp, 0, sizeof (temp));
734 temp.type = TREE_TYPE (ref);
735 temp.opcode = TREE_CODE (ref);
736 temp.op0 = TMR_INDEX (ref);
737 temp.op1 = TMR_STEP (ref);
738 temp.op2 = TMR_OFFSET (ref);
739 temp.off = -1;
740 result->quick_push (temp);
742 memset (&temp, 0, sizeof (temp));
743 temp.type = NULL_TREE;
744 temp.opcode = ERROR_MARK;
745 temp.op0 = TMR_INDEX2 (ref);
746 temp.off = -1;
747 result->quick_push (temp);
749 memset (&temp, 0, sizeof (temp));
750 temp.type = NULL_TREE;
751 temp.opcode = TREE_CODE (TMR_BASE (ref));
752 temp.op0 = TMR_BASE (ref);
753 temp.off = -1;
754 result->quick_push (temp);
755 return;
758 /* For non-calls, store the information that makes up the address. */
760 while (ref)
762 vn_reference_op_s temp;
764 memset (&temp, 0, sizeof (temp));
765 temp.type = TREE_TYPE (ref);
766 temp.opcode = TREE_CODE (ref);
767 temp.off = -1;
769 switch (temp.opcode)
771 case MODIFY_EXPR:
772 temp.op0 = TREE_OPERAND (ref, 1);
773 break;
774 case WITH_SIZE_EXPR:
775 temp.op0 = TREE_OPERAND (ref, 1);
776 temp.off = 0;
777 break;
778 case MEM_REF:
779 /* The base address gets its own vn_reference_op_s structure. */
780 temp.op0 = TREE_OPERAND (ref, 1);
781 if (host_integerp (TREE_OPERAND (ref, 1), 0))
782 temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
783 break;
784 case BIT_FIELD_REF:
785 /* Record bits and position. */
786 temp.op0 = TREE_OPERAND (ref, 1);
787 temp.op1 = TREE_OPERAND (ref, 2);
788 break;
789 case COMPONENT_REF:
790 /* The field decl is enough to unambiguously specify the field,
791 a matching type is not necessary and a mismatching type
792 is always a spurious difference. */
793 temp.type = NULL_TREE;
794 temp.op0 = TREE_OPERAND (ref, 1);
795 temp.op1 = TREE_OPERAND (ref, 2);
797 tree this_offset = component_ref_field_offset (ref);
798 if (this_offset
799 && TREE_CODE (this_offset) == INTEGER_CST)
801 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
802 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
804 double_int off
805 = tree_to_double_int (this_offset)
806 + tree_to_double_int (bit_offset)
807 .rshift (BITS_PER_UNIT == 8
808 ? 3 : exact_log2 (BITS_PER_UNIT));
809 if (off.fits_shwi ())
810 temp.off = off.low;
814 break;
815 case ARRAY_RANGE_REF:
816 case ARRAY_REF:
817 /* Record index as operand. */
818 temp.op0 = TREE_OPERAND (ref, 1);
819 /* Always record lower bounds and element size. */
820 temp.op1 = array_ref_low_bound (ref);
821 temp.op2 = array_ref_element_size (ref);
822 if (TREE_CODE (temp.op0) == INTEGER_CST
823 && TREE_CODE (temp.op1) == INTEGER_CST
824 && TREE_CODE (temp.op2) == INTEGER_CST)
826 double_int off = tree_to_double_int (temp.op0);
827 off += -tree_to_double_int (temp.op1);
828 off *= tree_to_double_int (temp.op2);
829 if (off.fits_shwi ())
830 temp.off = off.low;
832 break;
833 case VAR_DECL:
834 if (DECL_HARD_REGISTER (ref))
836 temp.op0 = ref;
837 break;
839 /* Fallthru. */
840 case PARM_DECL:
841 case CONST_DECL:
842 case RESULT_DECL:
843 /* Canonicalize decls to MEM[&decl] which is what we end up with
844 when valueizing MEM[ptr] with ptr = &decl. */
845 temp.opcode = MEM_REF;
846 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
847 temp.off = 0;
848 result->safe_push (temp);
849 temp.opcode = ADDR_EXPR;
850 temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref);
851 temp.type = TREE_TYPE (temp.op0);
852 temp.off = -1;
853 break;
854 case STRING_CST:
855 case INTEGER_CST:
856 case COMPLEX_CST:
857 case VECTOR_CST:
858 case REAL_CST:
859 case FIXED_CST:
860 case CONSTRUCTOR:
861 case SSA_NAME:
862 temp.op0 = ref;
863 break;
864 case ADDR_EXPR:
865 if (is_gimple_min_invariant (ref))
867 temp.op0 = ref;
868 break;
870 /* Fallthrough. */
871 /* These are only interesting for their operands, their
872 existence, and their type. They will never be the last
873 ref in the chain of references (IE they require an
874 operand), so we don't have to put anything
875 for op* as it will be handled by the iteration */
876 case REALPART_EXPR:
877 case VIEW_CONVERT_EXPR:
878 temp.off = 0;
879 break;
880 case IMAGPART_EXPR:
881 /* This is only interesting for its constant offset. */
882 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
883 break;
884 default:
885 gcc_unreachable ();
887 result->safe_push (temp);
889 if (REFERENCE_CLASS_P (ref)
890 || TREE_CODE (ref) == MODIFY_EXPR
891 || TREE_CODE (ref) == WITH_SIZE_EXPR
892 || (TREE_CODE (ref) == ADDR_EXPR
893 && !is_gimple_min_invariant (ref)))
894 ref = TREE_OPERAND (ref, 0);
895 else
896 ref = NULL_TREE;
900 /* Build a alias-oracle reference abstraction in *REF from the vn_reference
901 operands in *OPS, the reference alias set SET and the reference type TYPE.
902 Return true if something useful was produced. */
904 bool
905 ao_ref_init_from_vn_reference (ao_ref *ref,
906 alias_set_type set, tree type,
907 vec<vn_reference_op_s> ops)
909 vn_reference_op_t op;
910 unsigned i;
911 tree base = NULL_TREE;
912 tree *op0_p = &base;
913 HOST_WIDE_INT offset = 0;
914 HOST_WIDE_INT max_size;
915 HOST_WIDE_INT size = -1;
916 tree size_tree = NULL_TREE;
917 alias_set_type base_alias_set = -1;
919 /* First get the final access size from just the outermost expression. */
920 op = &ops[0];
921 if (op->opcode == COMPONENT_REF)
922 size_tree = DECL_SIZE (op->op0);
923 else if (op->opcode == BIT_FIELD_REF)
924 size_tree = op->op0;
925 else
927 enum machine_mode mode = TYPE_MODE (type);
928 if (mode == BLKmode)
929 size_tree = TYPE_SIZE (type);
930 else
931 size = GET_MODE_BITSIZE (mode);
933 if (size_tree != NULL_TREE)
935 if (!host_integerp (size_tree, 1))
936 size = -1;
937 else
938 size = TREE_INT_CST_LOW (size_tree);
941 /* Initially, maxsize is the same as the accessed element size.
942 In the following it will only grow (or become -1). */
943 max_size = size;
945 /* Compute cumulative bit-offset for nested component-refs and array-refs,
946 and find the ultimate containing object. */
947 FOR_EACH_VEC_ELT (ops, i, op)
949 switch (op->opcode)
951 /* These may be in the reference ops, but we cannot do anything
952 sensible with them here. */
953 case ADDR_EXPR:
954 /* Apart from ADDR_EXPR arguments to MEM_REF. */
955 if (base != NULL_TREE
956 && TREE_CODE (base) == MEM_REF
957 && op->op0
958 && DECL_P (TREE_OPERAND (op->op0, 0)))
960 vn_reference_op_t pop = &ops[i-1];
961 base = TREE_OPERAND (op->op0, 0);
962 if (pop->off == -1)
964 max_size = -1;
965 offset = 0;
967 else
968 offset += pop->off * BITS_PER_UNIT;
969 op0_p = NULL;
970 break;
972 /* Fallthru. */
973 case CALL_EXPR:
974 return false;
976 /* Record the base objects. */
977 case MEM_REF:
978 base_alias_set = get_deref_alias_set (op->op0);
979 *op0_p = build2 (MEM_REF, op->type,
980 NULL_TREE, op->op0);
981 op0_p = &TREE_OPERAND (*op0_p, 0);
982 break;
984 case VAR_DECL:
985 case PARM_DECL:
986 case RESULT_DECL:
987 case SSA_NAME:
988 *op0_p = op->op0;
989 op0_p = NULL;
990 break;
992 /* And now the usual component-reference style ops. */
993 case BIT_FIELD_REF:
994 offset += tree_low_cst (op->op1, 0);
995 break;
997 case COMPONENT_REF:
999 tree field = op->op0;
1000 /* We do not have a complete COMPONENT_REF tree here so we
1001 cannot use component_ref_field_offset. Do the interesting
1002 parts manually. */
1004 if (op->op1
1005 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
1006 max_size = -1;
1007 else
1009 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
1010 * BITS_PER_UNIT);
1011 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
1013 break;
1016 case ARRAY_RANGE_REF:
1017 case ARRAY_REF:
1018 /* We recorded the lower bound and the element size. */
1019 if (!host_integerp (op->op0, 0)
1020 || !host_integerp (op->op1, 0)
1021 || !host_integerp (op->op2, 0))
1022 max_size = -1;
1023 else
1025 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
1026 hindex -= TREE_INT_CST_LOW (op->op1);
1027 hindex *= TREE_INT_CST_LOW (op->op2);
1028 hindex *= BITS_PER_UNIT;
1029 offset += hindex;
1031 break;
1033 case REALPART_EXPR:
1034 break;
1036 case IMAGPART_EXPR:
1037 offset += size;
1038 break;
1040 case VIEW_CONVERT_EXPR:
1041 break;
1043 case STRING_CST:
1044 case INTEGER_CST:
1045 case COMPLEX_CST:
1046 case VECTOR_CST:
1047 case REAL_CST:
1048 case CONSTRUCTOR:
1049 case CONST_DECL:
1050 return false;
1052 default:
1053 return false;
1057 if (base == NULL_TREE)
1058 return false;
1060 ref->ref = NULL_TREE;
1061 ref->base = base;
1062 ref->offset = offset;
1063 ref->size = size;
1064 ref->max_size = max_size;
1065 ref->ref_alias_set = set;
1066 if (base_alias_set != -1)
1067 ref->base_alias_set = base_alias_set;
1068 else
1069 ref->base_alias_set = get_alias_set (base);
1070 /* We discount volatiles from value-numbering elsewhere. */
1071 ref->volatile_p = false;
1073 return true;
1076 /* Copy the operations present in load/store/call REF into RESULT, a vector of
1077 vn_reference_op_s's. */
1079 void
1080 copy_reference_ops_from_call (gimple call,
1081 vec<vn_reference_op_s> *result)
1083 vn_reference_op_s temp;
1084 unsigned i;
1085 tree lhs = gimple_call_lhs (call);
1087 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
1088 different. By adding the lhs here in the vector, we ensure that the
1089 hashcode is different, guaranteeing a different value number. */
1090 if (lhs && TREE_CODE (lhs) != SSA_NAME)
1092 memset (&temp, 0, sizeof (temp));
1093 temp.opcode = MODIFY_EXPR;
1094 temp.type = TREE_TYPE (lhs);
1095 temp.op0 = lhs;
1096 temp.off = -1;
1097 result->safe_push (temp);
1100 /* Copy the type, opcode, function being called and static chain. */
1101 memset (&temp, 0, sizeof (temp));
1102 temp.type = gimple_call_return_type (call);
1103 temp.opcode = CALL_EXPR;
1104 temp.op0 = gimple_call_fn (call);
1105 temp.op1 = gimple_call_chain (call);
1106 temp.off = -1;
1107 result->safe_push (temp);
1109 /* Copy the call arguments. As they can be references as well,
1110 just chain them together. */
1111 for (i = 0; i < gimple_call_num_args (call); ++i)
1113 tree callarg = gimple_call_arg (call, i);
1114 copy_reference_ops_from_ref (callarg, result);
1118 /* Create a vector of vn_reference_op_s structures from CALL, a
1119 call statement. The vector is not shared. */
1121 static vec<vn_reference_op_s>
1122 create_reference_ops_from_call (gimple call)
1124 vec<vn_reference_op_s> result = vNULL;
1126 copy_reference_ops_from_call (call, &result);
1127 return result;
1130 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1131 *I_P to point to the last element of the replacement. */
1132 void
1133 vn_reference_fold_indirect (vec<vn_reference_op_s> *ops,
1134 unsigned int *i_p)
1136 unsigned int i = *i_p;
1137 vn_reference_op_t op = &(*ops)[i];
1138 vn_reference_op_t mem_op = &(*ops)[i - 1];
1139 tree addr_base;
1140 HOST_WIDE_INT addr_offset = 0;
1142 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1143 from .foo.bar to the preceding MEM_REF offset and replace the
1144 address with &OBJ. */
1145 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1146 &addr_offset);
1147 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1148 if (addr_base != op->op0)
1150 double_int off = tree_to_double_int (mem_op->op0);
1151 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1152 off += double_int::from_shwi (addr_offset);
1153 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1154 op->op0 = build_fold_addr_expr (addr_base);
1155 if (host_integerp (mem_op->op0, 0))
1156 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1157 else
1158 mem_op->off = -1;
1162 /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1163 *I_P to point to the last element of the replacement. */
1164 static void
1165 vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops,
1166 unsigned int *i_p)
1168 unsigned int i = *i_p;
1169 vn_reference_op_t op = &(*ops)[i];
1170 vn_reference_op_t mem_op = &(*ops)[i - 1];
1171 gimple def_stmt;
1172 enum tree_code code;
1173 double_int off;
1175 def_stmt = SSA_NAME_DEF_STMT (op->op0);
1176 if (!is_gimple_assign (def_stmt))
1177 return;
1179 code = gimple_assign_rhs_code (def_stmt);
1180 if (code != ADDR_EXPR
1181 && code != POINTER_PLUS_EXPR)
1182 return;
1184 off = tree_to_double_int (mem_op->op0);
1185 off = off.sext (TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1187 /* The only thing we have to do is from &OBJ.foo.bar add the offset
1188 from .foo.bar to the preceding MEM_REF offset and replace the
1189 address with &OBJ. */
1190 if (code == ADDR_EXPR)
1192 tree addr, addr_base;
1193 HOST_WIDE_INT addr_offset;
1195 addr = gimple_assign_rhs1 (def_stmt);
1196 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1197 &addr_offset);
1198 if (!addr_base
1199 || TREE_CODE (addr_base) != MEM_REF)
1200 return;
1202 off += double_int::from_shwi (addr_offset);
1203 off += mem_ref_offset (addr_base);
1204 op->op0 = TREE_OPERAND (addr_base, 0);
1206 else
1208 tree ptr, ptroff;
1209 ptr = gimple_assign_rhs1 (def_stmt);
1210 ptroff = gimple_assign_rhs2 (def_stmt);
1211 if (TREE_CODE (ptr) != SSA_NAME
1212 || TREE_CODE (ptroff) != INTEGER_CST)
1213 return;
1215 off += tree_to_double_int (ptroff);
1216 op->op0 = ptr;
1219 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1220 if (host_integerp (mem_op->op0, 0))
1221 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1222 else
1223 mem_op->off = -1;
1224 if (TREE_CODE (op->op0) == SSA_NAME)
1225 op->op0 = SSA_VAL (op->op0);
1226 if (TREE_CODE (op->op0) != SSA_NAME)
1227 op->opcode = TREE_CODE (op->op0);
1229 /* And recurse. */
1230 if (TREE_CODE (op->op0) == SSA_NAME)
1231 vn_reference_maybe_forwprop_address (ops, i_p);
1232 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1233 vn_reference_fold_indirect (ops, i_p);
1236 /* Optimize the reference REF to a constant if possible or return
1237 NULL_TREE if not. */
1239 tree
1240 fully_constant_vn_reference_p (vn_reference_t ref)
1242 vec<vn_reference_op_s> operands = ref->operands;
1243 vn_reference_op_t op;
1245 /* Try to simplify the translated expression if it is
1246 a call to a builtin function with at most two arguments. */
1247 op = &operands[0];
1248 if (op->opcode == CALL_EXPR
1249 && TREE_CODE (op->op0) == ADDR_EXPR
1250 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1251 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1252 && operands.length () >= 2
1253 && operands.length () <= 3)
1255 vn_reference_op_t arg0, arg1 = NULL;
1256 bool anyconst = false;
1257 arg0 = &operands[1];
1258 if (operands.length () > 2)
1259 arg1 = &operands[2];
1260 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1261 || (arg0->opcode == ADDR_EXPR
1262 && is_gimple_min_invariant (arg0->op0)))
1263 anyconst = true;
1264 if (arg1
1265 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1266 || (arg1->opcode == ADDR_EXPR
1267 && is_gimple_min_invariant (arg1->op0))))
1268 anyconst = true;
1269 if (anyconst)
1271 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1272 arg1 ? 2 : 1,
1273 arg0->op0,
1274 arg1 ? arg1->op0 : NULL);
1275 if (folded
1276 && TREE_CODE (folded) == NOP_EXPR)
1277 folded = TREE_OPERAND (folded, 0);
1278 if (folded
1279 && is_gimple_min_invariant (folded))
1280 return folded;
1284 /* Simplify reads from constant strings. */
1285 else if (op->opcode == ARRAY_REF
1286 && TREE_CODE (op->op0) == INTEGER_CST
1287 && integer_zerop (op->op1)
1288 && operands.length () == 2)
1290 vn_reference_op_t arg0;
1291 arg0 = &operands[1];
1292 if (arg0->opcode == STRING_CST
1293 && (TYPE_MODE (op->type)
1294 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1295 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1296 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1297 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1298 return build_int_cst_type (op->type,
1299 (TREE_STRING_POINTER (arg0->op0)
1300 [TREE_INT_CST_LOW (op->op0)]));
1303 return NULL_TREE;
1306 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
1307 structures into their value numbers. This is done in-place, and
1308 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1309 whether any operands were valueized. */
1311 static vec<vn_reference_op_s>
1312 valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything)
1314 vn_reference_op_t vro;
1315 unsigned int i;
1317 *valueized_anything = false;
1319 FOR_EACH_VEC_ELT (orig, i, vro)
1321 if (vro->opcode == SSA_NAME
1322 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
1324 tree tem = SSA_VAL (vro->op0);
1325 if (tem != vro->op0)
1327 *valueized_anything = true;
1328 vro->op0 = tem;
1330 /* If it transforms from an SSA_NAME to a constant, update
1331 the opcode. */
1332 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1333 vro->opcode = TREE_CODE (vro->op0);
1335 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
1337 tree tem = SSA_VAL (vro->op1);
1338 if (tem != vro->op1)
1340 *valueized_anything = true;
1341 vro->op1 = tem;
1344 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
1346 tree tem = SSA_VAL (vro->op2);
1347 if (tem != vro->op2)
1349 *valueized_anything = true;
1350 vro->op2 = tem;
1353 /* If it transforms from an SSA_NAME to an address, fold with
1354 a preceding indirect reference. */
1355 if (i > 0
1356 && vro->op0
1357 && TREE_CODE (vro->op0) == ADDR_EXPR
1358 && orig[i - 1].opcode == MEM_REF)
1359 vn_reference_fold_indirect (&orig, &i);
1360 else if (i > 0
1361 && vro->opcode == SSA_NAME
1362 && orig[i - 1].opcode == MEM_REF)
1363 vn_reference_maybe_forwprop_address (&orig, &i);
1364 /* If it transforms a non-constant ARRAY_REF into a constant
1365 one, adjust the constant offset. */
1366 else if (vro->opcode == ARRAY_REF
1367 && vro->off == -1
1368 && TREE_CODE (vro->op0) == INTEGER_CST
1369 && TREE_CODE (vro->op1) == INTEGER_CST
1370 && TREE_CODE (vro->op2) == INTEGER_CST)
1372 double_int off = tree_to_double_int (vro->op0);
1373 off += -tree_to_double_int (vro->op1);
1374 off *= tree_to_double_int (vro->op2);
1375 if (off.fits_shwi ())
1376 vro->off = off.low;
1380 return orig;
1383 static vec<vn_reference_op_s>
1384 valueize_refs (vec<vn_reference_op_s> orig)
1386 bool tem;
1387 return valueize_refs_1 (orig, &tem);
1390 static vec<vn_reference_op_s> shared_lookup_references;
1392 /* Create a vector of vn_reference_op_s structures from REF, a
1393 REFERENCE_CLASS_P tree. The vector is shared among all callers of
1394 this function. *VALUEIZED_ANYTHING will specify whether any
1395 operands were valueized. */
1397 static vec<vn_reference_op_s>
1398 valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
1400 if (!ref)
1401 return vNULL;
1402 shared_lookup_references.truncate (0);
1403 copy_reference_ops_from_ref (ref, &shared_lookup_references);
1404 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1405 valueized_anything);
1406 return shared_lookup_references;
1409 /* Create a vector of vn_reference_op_s structures from CALL, a
1410 call statement. The vector is shared among all callers of
1411 this function. */
1413 static vec<vn_reference_op_s>
1414 valueize_shared_reference_ops_from_call (gimple call)
1416 if (!call)
1417 return vNULL;
1418 shared_lookup_references.truncate (0);
1419 copy_reference_ops_from_call (call, &shared_lookup_references);
1420 shared_lookup_references = valueize_refs (shared_lookup_references);
1421 return shared_lookup_references;
1424 /* Lookup a SCCVN reference operation VR in the current hash table.
1425 Returns the resulting value number if it exists in the hash table,
1426 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1427 vn_reference_t stored in the hashtable if something is found. */
1429 static tree
1430 vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
1432 vn_reference_s **slot;
1433 hashval_t hash;
1435 hash = vr->hashcode;
1436 slot = current_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1437 if (!slot && current_info == optimistic_info)
1438 slot = valid_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1439 if (slot)
1441 if (vnresult)
1442 *vnresult = (vn_reference_t)*slot;
1443 return ((vn_reference_t)*slot)->result;
1446 return NULL_TREE;
1449 static tree *last_vuse_ptr;
1450 static vn_lookup_kind vn_walk_kind;
1451 static vn_lookup_kind default_vn_walk_kind;
1453 /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1454 with the current VUSE and performs the expression lookup. */
1456 static void *
1457 vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse,
1458 unsigned int cnt, void *vr_)
1460 vn_reference_t vr = (vn_reference_t)vr_;
1461 vn_reference_s **slot;
1462 hashval_t hash;
1464 /* This bounds the stmt walks we perform on reference lookups
1465 to O(1) instead of O(N) where N is the number of dominating
1466 stores. */
1467 if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS))
1468 return (void *)-1;
1470 if (last_vuse_ptr)
1471 *last_vuse_ptr = vuse;
1473 /* Fixup vuse and hash. */
1474 if (vr->vuse)
1475 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1476 vr->vuse = SSA_VAL (vuse);
1477 if (vr->vuse)
1478 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1480 hash = vr->hashcode;
1481 slot = current_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1482 if (!slot && current_info == optimistic_info)
1483 slot = valid_info->references.find_slot_with_hash (vr, hash, NO_INSERT);
1484 if (slot)
1485 return *slot;
1487 return NULL;
1490 /* Lookup an existing or insert a new vn_reference entry into the
1491 value table for the VUSE, SET, TYPE, OPERANDS reference which
1492 has the value VALUE which is either a constant or an SSA name. */
1494 static vn_reference_t
1495 vn_reference_lookup_or_insert_for_pieces (tree vuse,
1496 alias_set_type set,
1497 tree type,
1498 vec<vn_reference_op_s,
1499 va_heap> operands,
1500 tree value)
1502 struct vn_reference_s vr1;
1503 vn_reference_t result;
1504 unsigned value_id;
1505 vr1.vuse = vuse;
1506 vr1.operands = operands;
1507 vr1.type = type;
1508 vr1.set = set;
1509 vr1.hashcode = vn_reference_compute_hash (&vr1);
1510 if (vn_reference_lookup_1 (&vr1, &result))
1511 return result;
1512 if (TREE_CODE (value) == SSA_NAME)
1513 value_id = VN_INFO (value)->value_id;
1514 else
1515 value_id = get_or_alloc_constant_value_id (value);
1516 return vn_reference_insert_pieces (vuse, set, type,
1517 operands.copy (), value, value_id);
1520 /* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1521 from the statement defining VUSE and if not successful tries to
1522 translate *REFP and VR_ through an aggregate copy at the definition
1523 of VUSE. */
1525 static void *
1526 vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1528 vn_reference_t vr = (vn_reference_t)vr_;
1529 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1530 tree base;
1531 HOST_WIDE_INT offset, maxsize;
1532 static vec<vn_reference_op_s>
1533 lhs_ops = vNULL;
1534 ao_ref lhs_ref;
1535 bool lhs_ref_ok = false;
1537 /* First try to disambiguate after value-replacing in the definitions LHS. */
1538 if (is_gimple_assign (def_stmt))
1540 vec<vn_reference_op_s> tem;
1541 tree lhs = gimple_assign_lhs (def_stmt);
1542 bool valueized_anything = false;
1543 /* Avoid re-allocation overhead. */
1544 lhs_ops.truncate (0);
1545 copy_reference_ops_from_ref (lhs, &lhs_ops);
1546 tem = lhs_ops;
1547 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
1548 gcc_assert (lhs_ops == tem);
1549 if (valueized_anything)
1551 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1552 get_alias_set (lhs),
1553 TREE_TYPE (lhs), lhs_ops);
1554 if (lhs_ref_ok
1555 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1556 return NULL;
1558 else
1560 ao_ref_init (&lhs_ref, lhs);
1561 lhs_ref_ok = true;
1565 base = ao_ref_base (ref);
1566 offset = ref->offset;
1567 maxsize = ref->max_size;
1569 /* If we cannot constrain the size of the reference we cannot
1570 test if anything kills it. */
1571 if (maxsize == -1)
1572 return (void *)-1;
1574 /* We can't deduce anything useful from clobbers. */
1575 if (gimple_clobber_p (def_stmt))
1576 return (void *)-1;
1578 /* def_stmt may-defs *ref. See if we can derive a value for *ref
1579 from that definition.
1580 1) Memset. */
1581 if (is_gimple_reg_type (vr->type)
1582 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
1583 && integer_zerop (gimple_call_arg (def_stmt, 1))
1584 && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1585 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1587 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1588 tree base2;
1589 HOST_WIDE_INT offset2, size2, maxsize2;
1590 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1591 size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1592 if ((unsigned HOST_WIDE_INT)size2 / 8
1593 == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1594 && maxsize2 != -1
1595 && operand_equal_p (base, base2, 0)
1596 && offset2 <= offset
1597 && offset2 + size2 >= offset + maxsize)
1599 tree val = build_zero_cst (vr->type);
1600 return vn_reference_lookup_or_insert_for_pieces
1601 (vuse, vr->set, vr->type, vr->operands, val);
1605 /* 2) Assignment from an empty CONSTRUCTOR. */
1606 else if (is_gimple_reg_type (vr->type)
1607 && gimple_assign_single_p (def_stmt)
1608 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1609 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1611 tree base2;
1612 HOST_WIDE_INT offset2, size2, maxsize2;
1613 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1614 &offset2, &size2, &maxsize2);
1615 if (maxsize2 != -1
1616 && operand_equal_p (base, base2, 0)
1617 && offset2 <= offset
1618 && offset2 + size2 >= offset + maxsize)
1620 tree val = build_zero_cst (vr->type);
1621 return vn_reference_lookup_or_insert_for_pieces
1622 (vuse, vr->set, vr->type, vr->operands, val);
1626 /* 3) Assignment from a constant. We can use folds native encode/interpret
1627 routines to extract the assigned bits. */
1628 else if (vn_walk_kind == VN_WALKREWRITE
1629 && CHAR_BIT == 8 && BITS_PER_UNIT == 8
1630 && ref->size == maxsize
1631 && maxsize % BITS_PER_UNIT == 0
1632 && offset % BITS_PER_UNIT == 0
1633 && is_gimple_reg_type (vr->type)
1634 && gimple_assign_single_p (def_stmt)
1635 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1637 tree base2;
1638 HOST_WIDE_INT offset2, size2, maxsize2;
1639 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1640 &offset2, &size2, &maxsize2);
1641 if (maxsize2 != -1
1642 && maxsize2 == size2
1643 && size2 % BITS_PER_UNIT == 0
1644 && offset2 % BITS_PER_UNIT == 0
1645 && operand_equal_p (base, base2, 0)
1646 && offset2 <= offset
1647 && offset2 + size2 >= offset + maxsize)
1649 /* We support up to 512-bit values (for V8DFmode). */
1650 unsigned char buffer[64];
1651 int len;
1653 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1654 buffer, sizeof (buffer));
1655 if (len > 0)
1657 tree val = native_interpret_expr (vr->type,
1658 buffer
1659 + ((offset - offset2)
1660 / BITS_PER_UNIT),
1661 ref->size / BITS_PER_UNIT);
1662 if (val)
1663 return vn_reference_lookup_or_insert_for_pieces
1664 (vuse, vr->set, vr->type, vr->operands, val);
1669 /* 4) Assignment from an SSA name which definition we may be able
1670 to access pieces from. */
1671 else if (ref->size == maxsize
1672 && is_gimple_reg_type (vr->type)
1673 && gimple_assign_single_p (def_stmt)
1674 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1676 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1677 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1678 if (is_gimple_assign (def_stmt2)
1679 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1680 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1681 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1683 tree base2;
1684 HOST_WIDE_INT offset2, size2, maxsize2, off;
1685 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1686 &offset2, &size2, &maxsize2);
1687 off = offset - offset2;
1688 if (maxsize2 != -1
1689 && maxsize2 == size2
1690 && operand_equal_p (base, base2, 0)
1691 && offset2 <= offset
1692 && offset2 + size2 >= offset + maxsize)
1694 tree val = NULL_TREE;
1695 HOST_WIDE_INT elsz
1696 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1697 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1699 if (off == 0)
1700 val = gimple_assign_rhs1 (def_stmt2);
1701 else if (off == elsz)
1702 val = gimple_assign_rhs2 (def_stmt2);
1704 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1705 && off % elsz == 0)
1707 tree ctor = gimple_assign_rhs1 (def_stmt2);
1708 unsigned i = off / elsz;
1709 if (i < CONSTRUCTOR_NELTS (ctor))
1711 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1712 if (TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
1714 if (TREE_CODE (TREE_TYPE (elt->value))
1715 != VECTOR_TYPE)
1716 val = elt->value;
1720 if (val)
1721 return vn_reference_lookup_or_insert_for_pieces
1722 (vuse, vr->set, vr->type, vr->operands, val);
1727 /* 5) For aggregate copies translate the reference through them if
1728 the copy kills ref. */
1729 else if (vn_walk_kind == VN_WALKREWRITE
1730 && gimple_assign_single_p (def_stmt)
1731 && (DECL_P (gimple_assign_rhs1 (def_stmt))
1732 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
1733 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1735 tree base2;
1736 HOST_WIDE_INT offset2, size2, maxsize2;
1737 int i, j;
1738 vec<vn_reference_op_s>
1739 rhs = vNULL;
1740 vn_reference_op_t vro;
1741 ao_ref r;
1743 if (!lhs_ref_ok)
1744 return (void *)-1;
1746 /* See if the assignment kills REF. */
1747 base2 = ao_ref_base (&lhs_ref);
1748 offset2 = lhs_ref.offset;
1749 size2 = lhs_ref.size;
1750 maxsize2 = lhs_ref.max_size;
1751 if (maxsize2 == -1
1752 || (base != base2 && !operand_equal_p (base, base2, 0))
1753 || offset2 > offset
1754 || offset2 + size2 < offset + maxsize)
1755 return (void *)-1;
1757 /* Find the common base of ref and the lhs. lhs_ops already
1758 contains valueized operands for the lhs. */
1759 i = vr->operands.length () - 1;
1760 j = lhs_ops.length () - 1;
1761 while (j >= 0 && i >= 0
1762 && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j]))
1764 i--;
1765 j--;
1768 /* ??? The innermost op should always be a MEM_REF and we already
1769 checked that the assignment to the lhs kills vr. Thus for
1770 aggregate copies using char[] types the vn_reference_op_eq
1771 may fail when comparing types for compatibility. But we really
1772 don't care here - further lookups with the rewritten operands
1773 will simply fail if we messed up types too badly. */
1774 if (j == 0 && i >= 0
1775 && lhs_ops[0].opcode == MEM_REF
1776 && lhs_ops[0].off != -1
1777 && (lhs_ops[0].off == vr->operands[i].off))
1778 i--, j--;
1780 /* i now points to the first additional op.
1781 ??? LHS may not be completely contained in VR, one or more
1782 VIEW_CONVERT_EXPRs could be in its way. We could at least
1783 try handling outermost VIEW_CONVERT_EXPRs. */
1784 if (j != -1)
1785 return (void *)-1;
1787 /* Now re-write REF to be based on the rhs of the assignment. */
1788 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1789 /* We need to pre-pend vr->operands[0..i] to rhs. */
1790 if (i + 1 + rhs.length () > vr->operands.length ())
1792 vec<vn_reference_op_s> old = vr->operands;
1793 vr->operands.safe_grow (i + 1 + rhs.length ());
1794 if (old == shared_lookup_references
1795 && vr->operands != old)
1796 shared_lookup_references = vNULL;
1798 else
1799 vr->operands.truncate (i + 1 + rhs.length ());
1800 FOR_EACH_VEC_ELT (rhs, j, vro)
1801 vr->operands[i + 1 + j] = *vro;
1802 rhs.release ();
1803 vr->operands = valueize_refs (vr->operands);
1804 vr->hashcode = vn_reference_compute_hash (vr);
1806 /* Adjust *ref from the new operands. */
1807 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1808 return (void *)-1;
1809 /* This can happen with bitfields. */
1810 if (ref->size != r.size)
1811 return (void *)-1;
1812 *ref = r;
1814 /* Do not update last seen VUSE after translating. */
1815 last_vuse_ptr = NULL;
1817 /* Keep looking for the adjusted *REF / VR pair. */
1818 return NULL;
1821 /* 6) For memcpy copies translate the reference through them if
1822 the copy kills ref. */
1823 else if (vn_walk_kind == VN_WALKREWRITE
1824 && is_gimple_reg_type (vr->type)
1825 /* ??? Handle BCOPY as well. */
1826 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1827 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1828 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1829 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1830 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1831 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1832 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1833 && host_integerp (gimple_call_arg (def_stmt, 2), 1))
1835 tree lhs, rhs;
1836 ao_ref r;
1837 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1838 vn_reference_op_s op;
1839 HOST_WIDE_INT at;
1842 /* Only handle non-variable, addressable refs. */
1843 if (ref->size != maxsize
1844 || offset % BITS_PER_UNIT != 0
1845 || ref->size % BITS_PER_UNIT != 0)
1846 return (void *)-1;
1848 /* Extract a pointer base and an offset for the destination. */
1849 lhs = gimple_call_arg (def_stmt, 0);
1850 lhs_offset = 0;
1851 if (TREE_CODE (lhs) == SSA_NAME)
1852 lhs = SSA_VAL (lhs);
1853 if (TREE_CODE (lhs) == ADDR_EXPR)
1855 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1856 &lhs_offset);
1857 if (!tem)
1858 return (void *)-1;
1859 if (TREE_CODE (tem) == MEM_REF
1860 && host_integerp (TREE_OPERAND (tem, 1), 1))
1862 lhs = TREE_OPERAND (tem, 0);
1863 lhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1865 else if (DECL_P (tem))
1866 lhs = build_fold_addr_expr (tem);
1867 else
1868 return (void *)-1;
1870 if (TREE_CODE (lhs) != SSA_NAME
1871 && TREE_CODE (lhs) != ADDR_EXPR)
1872 return (void *)-1;
1874 /* Extract a pointer base and an offset for the source. */
1875 rhs = gimple_call_arg (def_stmt, 1);
1876 rhs_offset = 0;
1877 if (TREE_CODE (rhs) == SSA_NAME)
1878 rhs = SSA_VAL (rhs);
1879 if (TREE_CODE (rhs) == ADDR_EXPR)
1881 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1882 &rhs_offset);
1883 if (!tem)
1884 return (void *)-1;
1885 if (TREE_CODE (tem) == MEM_REF
1886 && host_integerp (TREE_OPERAND (tem, 1), 1))
1888 rhs = TREE_OPERAND (tem, 0);
1889 rhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1891 else if (DECL_P (tem))
1892 rhs = build_fold_addr_expr (tem);
1893 else
1894 return (void *)-1;
1896 if (TREE_CODE (rhs) != SSA_NAME
1897 && TREE_CODE (rhs) != ADDR_EXPR)
1898 return (void *)-1;
1900 copy_size = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2));
1902 /* The bases of the destination and the references have to agree. */
1903 if ((TREE_CODE (base) != MEM_REF
1904 && !DECL_P (base))
1905 || (TREE_CODE (base) == MEM_REF
1906 && (TREE_OPERAND (base, 0) != lhs
1907 || !host_integerp (TREE_OPERAND (base, 1), 1)))
1908 || (DECL_P (base)
1909 && (TREE_CODE (lhs) != ADDR_EXPR
1910 || TREE_OPERAND (lhs, 0) != base)))
1911 return (void *)-1;
1913 /* And the access has to be contained within the memcpy destination. */
1914 at = offset / BITS_PER_UNIT;
1915 if (TREE_CODE (base) == MEM_REF)
1916 at += TREE_INT_CST_LOW (TREE_OPERAND (base, 1));
1917 if (lhs_offset > at
1918 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1919 return (void *)-1;
1921 /* Make room for 2 operands in the new reference. */
1922 if (vr->operands.length () < 2)
1924 vec<vn_reference_op_s> old = vr->operands;
1925 vr->operands.safe_grow_cleared (2);
1926 if (old == shared_lookup_references
1927 && vr->operands != old)
1928 shared_lookup_references.create (0);
1930 else
1931 vr->operands.truncate (2);
1933 /* The looked-through reference is a simple MEM_REF. */
1934 memset (&op, 0, sizeof (op));
1935 op.type = vr->type;
1936 op.opcode = MEM_REF;
1937 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
1938 op.off = at - lhs_offset + rhs_offset;
1939 vr->operands[0] = op;
1940 op.type = TREE_TYPE (rhs);
1941 op.opcode = TREE_CODE (rhs);
1942 op.op0 = rhs;
1943 op.off = -1;
1944 vr->operands[1] = op;
1945 vr->hashcode = vn_reference_compute_hash (vr);
1947 /* Adjust *ref from the new operands. */
1948 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1949 return (void *)-1;
1950 /* This can happen with bitfields. */
1951 if (ref->size != r.size)
1952 return (void *)-1;
1953 *ref = r;
1955 /* Do not update last seen VUSE after translating. */
1956 last_vuse_ptr = NULL;
1958 /* Keep looking for the adjusted *REF / VR pair. */
1959 return NULL;
1962 /* Bail out and stop walking. */
1963 return (void *)-1;
1966 /* Lookup a reference operation by it's parts, in the current hash table.
1967 Returns the resulting value number if it exists in the hash table,
1968 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1969 vn_reference_t stored in the hashtable if something is found. */
1971 tree
1972 vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1973 vec<vn_reference_op_s> operands,
1974 vn_reference_t *vnresult, vn_lookup_kind kind)
1976 struct vn_reference_s vr1;
1977 vn_reference_t tmp;
1978 tree cst;
1980 if (!vnresult)
1981 vnresult = &tmp;
1982 *vnresult = NULL;
1984 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1985 shared_lookup_references.truncate (0);
1986 shared_lookup_references.safe_grow (operands.length ());
1987 memcpy (shared_lookup_references.address (),
1988 operands.address (),
1989 sizeof (vn_reference_op_s)
1990 * operands.length ());
1991 vr1.operands = operands = shared_lookup_references
1992 = valueize_refs (shared_lookup_references);
1993 vr1.type = type;
1994 vr1.set = set;
1995 vr1.hashcode = vn_reference_compute_hash (&vr1);
1996 if ((cst = fully_constant_vn_reference_p (&vr1)))
1997 return cst;
1999 vn_reference_lookup_1 (&vr1, vnresult);
2000 if (!*vnresult
2001 && kind != VN_NOWALK
2002 && vr1.vuse)
2004 ao_ref r;
2005 vn_walk_kind = kind;
2006 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
2007 *vnresult =
2008 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2009 vn_reference_lookup_2,
2010 vn_reference_lookup_3, &vr1);
2011 if (vr1.operands != operands)
2012 vr1.operands.release ();
2015 if (*vnresult)
2016 return (*vnresult)->result;
2018 return NULL_TREE;
2021 /* Lookup OP in the current hash table, and return the resulting value
2022 number if it exists in the hash table. Return NULL_TREE if it does
2023 not exist in the hash table or if the result field of the structure
2024 was NULL.. VNRESULT will be filled in with the vn_reference_t
2025 stored in the hashtable if one exists. */
2027 tree
2028 vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
2029 vn_reference_t *vnresult)
2031 vec<vn_reference_op_s> operands;
2032 struct vn_reference_s vr1;
2033 tree cst;
2034 bool valuezied_anything;
2036 if (vnresult)
2037 *vnresult = NULL;
2039 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2040 vr1.operands = operands
2041 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
2042 vr1.type = TREE_TYPE (op);
2043 vr1.set = get_alias_set (op);
2044 vr1.hashcode = vn_reference_compute_hash (&vr1);
2045 if ((cst = fully_constant_vn_reference_p (&vr1)))
2046 return cst;
2048 if (kind != VN_NOWALK
2049 && vr1.vuse)
2051 vn_reference_t wvnresult;
2052 ao_ref r;
2053 /* Make sure to use a valueized reference if we valueized anything.
2054 Otherwise preserve the full reference for advanced TBAA. */
2055 if (!valuezied_anything
2056 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
2057 vr1.operands))
2058 ao_ref_init (&r, op);
2059 vn_walk_kind = kind;
2060 wvnresult =
2061 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
2062 vn_reference_lookup_2,
2063 vn_reference_lookup_3, &vr1);
2064 if (vr1.operands != operands)
2065 vr1.operands.release ();
2066 if (wvnresult)
2068 if (vnresult)
2069 *vnresult = wvnresult;
2070 return wvnresult->result;
2073 return NULL_TREE;
2076 return vn_reference_lookup_1 (&vr1, vnresult);
2080 /* Insert OP into the current hash table with a value number of
2081 RESULT, and return the resulting reference structure we created. */
2083 vn_reference_t
2084 vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
2086 vn_reference_s **slot;
2087 vn_reference_t vr1;
2088 bool tem;
2090 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2091 if (TREE_CODE (result) == SSA_NAME)
2092 vr1->value_id = VN_INFO (result)->value_id;
2093 else
2094 vr1->value_id = get_or_alloc_constant_value_id (result);
2095 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2096 vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy ();
2097 vr1->type = TREE_TYPE (op);
2098 vr1->set = get_alias_set (op);
2099 vr1->hashcode = vn_reference_compute_hash (vr1);
2100 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
2101 vr1->result_vdef = vdef;
2103 slot = current_info->references.find_slot_with_hash (vr1, vr1->hashcode,
2104 INSERT);
2106 /* Because we lookup stores using vuses, and value number failures
2107 using the vdefs (see visit_reference_op_store for how and why),
2108 it's possible that on failure we may try to insert an already
2109 inserted store. This is not wrong, there is no ssa name for a
2110 store that we could use as a differentiator anyway. Thus, unlike
2111 the other lookup functions, you cannot gcc_assert (!*slot)
2112 here. */
2114 /* But free the old slot in case of a collision. */
2115 if (*slot)
2116 free_reference (*slot);
2118 *slot = vr1;
2119 return vr1;
2122 /* Insert a reference by it's pieces into the current hash table with
2123 a value number of RESULT. Return the resulting reference
2124 structure we created. */
2126 vn_reference_t
2127 vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
2128 vec<vn_reference_op_s> operands,
2129 tree result, unsigned int value_id)
2132 vn_reference_s **slot;
2133 vn_reference_t vr1;
2135 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
2136 vr1->value_id = value_id;
2137 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2138 vr1->operands = valueize_refs (operands);
2139 vr1->type = type;
2140 vr1->set = set;
2141 vr1->hashcode = vn_reference_compute_hash (vr1);
2142 if (result && TREE_CODE (result) == SSA_NAME)
2143 result = SSA_VAL (result);
2144 vr1->result = result;
2146 slot = current_info->references.find_slot_with_hash (vr1, vr1->hashcode,
2147 INSERT);
2149 /* At this point we should have all the things inserted that we have
2150 seen before, and we should never try inserting something that
2151 already exists. */
2152 gcc_assert (!*slot);
2153 if (*slot)
2154 free_reference (*slot);
2156 *slot = vr1;
2157 return vr1;
2160 /* Compute and return the hash value for nary operation VBO1. */
2162 hashval_t
2163 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
2165 hashval_t hash;
2166 unsigned i;
2168 for (i = 0; i < vno1->length; ++i)
2169 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2170 vno1->op[i] = SSA_VAL (vno1->op[i]);
2172 if (vno1->length == 2
2173 && commutative_tree_code (vno1->opcode)
2174 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2176 tree temp = vno1->op[0];
2177 vno1->op[0] = vno1->op[1];
2178 vno1->op[1] = temp;
2181 hash = iterative_hash_hashval_t (vno1->opcode, 0);
2182 for (i = 0; i < vno1->length; ++i)
2183 hash = iterative_hash_expr (vno1->op[i], hash);
2185 return hash;
2188 /* Compare nary operations VNO1 and VNO2 and return true if they are
2189 equivalent. */
2191 bool
2192 vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2)
2194 unsigned i;
2196 if (vno1->hashcode != vno2->hashcode)
2197 return false;
2199 if (vno1->length != vno2->length)
2200 return false;
2202 if (vno1->opcode != vno2->opcode
2203 || !types_compatible_p (vno1->type, vno2->type))
2204 return false;
2206 for (i = 0; i < vno1->length; ++i)
2207 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2208 return false;
2210 return true;
2213 /* Initialize VNO from the pieces provided. */
2215 static void
2216 init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
2217 enum tree_code code, tree type, tree *ops)
2219 vno->opcode = code;
2220 vno->length = length;
2221 vno->type = type;
2222 memcpy (&vno->op[0], ops, sizeof (tree) * length);
2225 /* Initialize VNO from OP. */
2227 static void
2228 init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2230 unsigned i;
2232 vno->opcode = TREE_CODE (op);
2233 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2234 vno->type = TREE_TYPE (op);
2235 for (i = 0; i < vno->length; ++i)
2236 vno->op[i] = TREE_OPERAND (op, i);
2239 /* Return the number of operands for a vn_nary ops structure from STMT. */
2241 static unsigned int
2242 vn_nary_length_from_stmt (gimple stmt)
2244 switch (gimple_assign_rhs_code (stmt))
2246 case REALPART_EXPR:
2247 case IMAGPART_EXPR:
2248 case VIEW_CONVERT_EXPR:
2249 return 1;
2251 case BIT_FIELD_REF:
2252 return 3;
2254 case CONSTRUCTOR:
2255 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2257 default:
2258 return gimple_num_ops (stmt) - 1;
2262 /* Initialize VNO from STMT. */
2264 static void
2265 init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2267 unsigned i;
2269 vno->opcode = gimple_assign_rhs_code (stmt);
2270 vno->type = gimple_expr_type (stmt);
2271 switch (vno->opcode)
2273 case REALPART_EXPR:
2274 case IMAGPART_EXPR:
2275 case VIEW_CONVERT_EXPR:
2276 vno->length = 1;
2277 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2278 break;
2280 case BIT_FIELD_REF:
2281 vno->length = 3;
2282 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2283 vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1);
2284 vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2);
2285 break;
2287 case CONSTRUCTOR:
2288 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2289 for (i = 0; i < vno->length; ++i)
2290 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2291 break;
2293 default:
2294 gcc_checking_assert (!gimple_assign_single_p (stmt));
2295 vno->length = gimple_num_ops (stmt) - 1;
2296 for (i = 0; i < vno->length; ++i)
2297 vno->op[i] = gimple_op (stmt, i + 1);
2301 /* Compute the hashcode for VNO and look for it in the hash table;
2302 return the resulting value number if it exists in the hash table.
2303 Return NULL_TREE if it does not exist in the hash table or if the
2304 result field of the operation is NULL. VNRESULT will contain the
2305 vn_nary_op_t from the hashtable if it exists. */
2307 static tree
2308 vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
2310 vn_nary_op_s **slot;
2312 if (vnresult)
2313 *vnresult = NULL;
2315 vno->hashcode = vn_nary_op_compute_hash (vno);
2316 slot = current_info->nary.find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
2317 if (!slot && current_info == optimistic_info)
2318 slot = valid_info->nary.find_slot_with_hash (vno, vno->hashcode, NO_INSERT);
2319 if (!slot)
2320 return NULL_TREE;
2321 if (vnresult)
2322 *vnresult = *slot;
2323 return (*slot)->result;
2326 /* Lookup a n-ary operation by its pieces and return the resulting value
2327 number if it exists in the hash table. Return NULL_TREE if it does
2328 not exist in the hash table or if the result field of the operation
2329 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2330 if it exists. */
2332 tree
2333 vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
2334 tree type, tree *ops, vn_nary_op_t *vnresult)
2336 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2337 sizeof_vn_nary_op (length));
2338 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2339 return vn_nary_op_lookup_1 (vno1, vnresult);
2342 /* Lookup OP in the current hash table, and return the resulting value
2343 number if it exists in the hash table. Return NULL_TREE if it does
2344 not exist in the hash table or if the result field of the operation
2345 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2346 if it exists. */
2348 tree
2349 vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
2351 vn_nary_op_t vno1
2352 = XALLOCAVAR (struct vn_nary_op_s,
2353 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2354 init_vn_nary_op_from_op (vno1, op);
2355 return vn_nary_op_lookup_1 (vno1, vnresult);
2358 /* Lookup the rhs of STMT in the current hash table, and return the resulting
2359 value number if it exists in the hash table. Return NULL_TREE if
2360 it does not exist in the hash table. VNRESULT will contain the
2361 vn_nary_op_t from the hashtable if it exists. */
2363 tree
2364 vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2366 vn_nary_op_t vno1
2367 = XALLOCAVAR (struct vn_nary_op_s,
2368 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2369 init_vn_nary_op_from_stmt (vno1, stmt);
2370 return vn_nary_op_lookup_1 (vno1, vnresult);
2373 /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2375 static vn_nary_op_t
2376 alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2378 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2381 /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2382 obstack. */
2384 static vn_nary_op_t
2385 alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2387 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2388 &current_info->nary_obstack);
2390 vno1->value_id = value_id;
2391 vno1->length = length;
2392 vno1->result = result;
2394 return vno1;
2397 /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2398 VNO->HASHCODE first. */
2400 static vn_nary_op_t
2401 vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type table,
2402 bool compute_hash)
2404 vn_nary_op_s **slot;
2406 if (compute_hash)
2407 vno->hashcode = vn_nary_op_compute_hash (vno);
2409 slot = table.find_slot_with_hash (vno, vno->hashcode, INSERT);
2410 gcc_assert (!*slot);
2412 *slot = vno;
2413 return vno;
2416 /* Insert a n-ary operation into the current hash table using it's
2417 pieces. Return the vn_nary_op_t structure we created and put in
2418 the hashtable. */
2420 vn_nary_op_t
2421 vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
2422 tree type, tree *ops,
2423 tree result, unsigned int value_id)
2425 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2426 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2427 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2430 /* Insert OP into the current hash table with a value number of
2431 RESULT. Return the vn_nary_op_t structure we created and put in
2432 the hashtable. */
2434 vn_nary_op_t
2435 vn_nary_op_insert (tree op, tree result)
2437 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
2438 vn_nary_op_t vno1;
2440 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2441 init_vn_nary_op_from_op (vno1, op);
2442 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2445 /* Insert the rhs of STMT into the current hash table with a value number of
2446 RESULT. */
2448 vn_nary_op_t
2449 vn_nary_op_insert_stmt (gimple stmt, tree result)
2451 vn_nary_op_t vno1
2452 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2453 result, VN_INFO (result)->value_id);
2454 init_vn_nary_op_from_stmt (vno1, stmt);
2455 return vn_nary_op_insert_into (vno1, current_info->nary, true);
2458 /* Compute a hashcode for PHI operation VP1 and return it. */
2460 static inline hashval_t
2461 vn_phi_compute_hash (vn_phi_t vp1)
2463 hashval_t result;
2464 int i;
2465 tree phi1op;
2466 tree type;
2468 result = vp1->block->index;
2470 /* If all PHI arguments are constants we need to distinguish
2471 the PHI node via its type. */
2472 type = vp1->type;
2473 result += vn_hash_type (type);
2475 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2477 if (phi1op == VN_TOP)
2478 continue;
2479 result = iterative_hash_expr (phi1op, result);
2482 return result;
2485 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2487 static int
2488 vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2)
2490 if (vp1->hashcode != vp2->hashcode)
2491 return false;
2493 if (vp1->block == vp2->block)
2495 int i;
2496 tree phi1op;
2498 /* If the PHI nodes do not have compatible types
2499 they are not the same. */
2500 if (!types_compatible_p (vp1->type, vp2->type))
2501 return false;
2503 /* Any phi in the same block will have it's arguments in the
2504 same edge order, because of how we store phi nodes. */
2505 FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op)
2507 tree phi2op = vp2->phiargs[i];
2508 if (phi1op == VN_TOP || phi2op == VN_TOP)
2509 continue;
2510 if (!expressions_equal_p (phi1op, phi2op))
2511 return false;
2513 return true;
2515 return false;
2518 static vec<tree> shared_lookup_phiargs;
2520 /* Lookup PHI in the current hash table, and return the resulting
2521 value number if it exists in the hash table. Return NULL_TREE if
2522 it does not exist in the hash table. */
2524 static tree
2525 vn_phi_lookup (gimple phi)
2527 vn_phi_s **slot;
2528 struct vn_phi_s vp1;
2529 unsigned i;
2531 shared_lookup_phiargs.truncate (0);
2533 /* Canonicalize the SSA_NAME's to their value number. */
2534 for (i = 0; i < gimple_phi_num_args (phi); i++)
2536 tree def = PHI_ARG_DEF (phi, i);
2537 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2538 shared_lookup_phiargs.safe_push (def);
2540 vp1.type = TREE_TYPE (gimple_phi_result (phi));
2541 vp1.phiargs = shared_lookup_phiargs;
2542 vp1.block = gimple_bb (phi);
2543 vp1.hashcode = vn_phi_compute_hash (&vp1);
2544 slot = current_info->phis.find_slot_with_hash (&vp1, vp1.hashcode, NO_INSERT);
2545 if (!slot && current_info == optimistic_info)
2546 slot = valid_info->phis.find_slot_with_hash (&vp1, vp1.hashcode, NO_INSERT);
2547 if (!slot)
2548 return NULL_TREE;
2549 return (*slot)->result;
2552 /* Insert PHI into the current hash table with a value number of
2553 RESULT. */
2555 static vn_phi_t
2556 vn_phi_insert (gimple phi, tree result)
2558 vn_phi_s **slot;
2559 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
2560 unsigned i;
2561 vec<tree> args = vNULL;
2563 /* Canonicalize the SSA_NAME's to their value number. */
2564 for (i = 0; i < gimple_phi_num_args (phi); i++)
2566 tree def = PHI_ARG_DEF (phi, i);
2567 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2568 args.safe_push (def);
2570 vp1->value_id = VN_INFO (result)->value_id;
2571 vp1->type = TREE_TYPE (gimple_phi_result (phi));
2572 vp1->phiargs = args;
2573 vp1->block = gimple_bb (phi);
2574 vp1->result = result;
2575 vp1->hashcode = vn_phi_compute_hash (vp1);
2577 slot = current_info->phis.find_slot_with_hash (vp1, vp1->hashcode, INSERT);
2579 /* Because we iterate over phi operations more than once, it's
2580 possible the slot might already exist here, hence no assert.*/
2581 *slot = vp1;
2582 return vp1;
2586 /* Print set of components in strongly connected component SCC to OUT. */
2588 static void
2589 print_scc (FILE *out, vec<tree> scc)
2591 tree var;
2592 unsigned int i;
2594 fprintf (out, "SCC consists of:");
2595 FOR_EACH_VEC_ELT (scc, i, var)
2597 fprintf (out, " ");
2598 print_generic_expr (out, var, 0);
2600 fprintf (out, "\n");
2603 /* Set the value number of FROM to TO, return true if it has changed
2604 as a result. */
2606 static inline bool
2607 set_ssa_val_to (tree from, tree to)
2609 tree currval = SSA_VAL (from);
2611 if (from != to)
2613 if (currval == from)
2615 if (dump_file && (dump_flags & TDF_DETAILS))
2617 fprintf (dump_file, "Not changing value number of ");
2618 print_generic_expr (dump_file, from, 0);
2619 fprintf (dump_file, " from VARYING to ");
2620 print_generic_expr (dump_file, to, 0);
2621 fprintf (dump_file, "\n");
2623 return false;
2625 else if (TREE_CODE (to) == SSA_NAME
2626 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2627 to = from;
2630 /* The only thing we allow as value numbers are VN_TOP, ssa_names
2631 and invariants. So assert that here. */
2632 gcc_assert (to != NULL_TREE
2633 && (to == VN_TOP
2634 || TREE_CODE (to) == SSA_NAME
2635 || is_gimple_min_invariant (to)));
2637 if (dump_file && (dump_flags & TDF_DETAILS))
2639 fprintf (dump_file, "Setting value number of ");
2640 print_generic_expr (dump_file, from, 0);
2641 fprintf (dump_file, " to ");
2642 print_generic_expr (dump_file, to, 0);
2645 if (currval != to && !operand_equal_p (currval, to, OEP_PURE_SAME))
2647 VN_INFO (from)->valnum = to;
2648 if (dump_file && (dump_flags & TDF_DETAILS))
2649 fprintf (dump_file, " (changed)\n");
2650 return true;
2652 if (dump_file && (dump_flags & TDF_DETAILS))
2653 fprintf (dump_file, "\n");
2654 return false;
2657 /* Mark as processed all the definitions in the defining stmt of USE, or
2658 the USE itself. */
2660 static void
2661 mark_use_processed (tree use)
2663 ssa_op_iter iter;
2664 def_operand_p defp;
2665 gimple stmt = SSA_NAME_DEF_STMT (use);
2667 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2669 VN_INFO (use)->use_processed = true;
2670 return;
2673 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2675 tree def = DEF_FROM_PTR (defp);
2677 VN_INFO (def)->use_processed = true;
2681 /* Set all definitions in STMT to value number to themselves.
2682 Return true if a value number changed. */
2684 static bool
2685 defs_to_varying (gimple stmt)
2687 bool changed = false;
2688 ssa_op_iter iter;
2689 def_operand_p defp;
2691 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2693 tree def = DEF_FROM_PTR (defp);
2694 changed |= set_ssa_val_to (def, def);
2696 return changed;
2699 static bool expr_has_constants (tree expr);
2700 static tree valueize_expr (tree expr);
2702 /* Visit a copy between LHS and RHS, return true if the value number
2703 changed. */
2705 static bool
2706 visit_copy (tree lhs, tree rhs)
2708 /* The copy may have a more interesting constant filled expression
2709 (we don't, since we know our RHS is just an SSA name). */
2710 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2711 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2713 /* And finally valueize. */
2714 rhs = SSA_VAL (rhs);
2716 return set_ssa_val_to (lhs, rhs);
2719 /* Visit a nary operator RHS, value number it, and return true if the
2720 value number of LHS has changed as a result. */
2722 static bool
2723 visit_nary_op (tree lhs, gimple stmt)
2725 bool changed = false;
2726 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
2728 if (result)
2729 changed = set_ssa_val_to (lhs, result);
2730 else
2732 changed = set_ssa_val_to (lhs, lhs);
2733 vn_nary_op_insert_stmt (stmt, lhs);
2736 return changed;
2739 /* Visit a call STMT storing into LHS. Return true if the value number
2740 of the LHS has changed as a result. */
2742 static bool
2743 visit_reference_op_call (tree lhs, gimple stmt)
2745 bool changed = false;
2746 struct vn_reference_s vr1;
2747 vn_reference_t vnresult = NULL;
2748 tree vuse = gimple_vuse (stmt);
2749 tree vdef = gimple_vdef (stmt);
2751 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2752 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2753 lhs = NULL_TREE;
2755 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
2756 vr1.operands = valueize_shared_reference_ops_from_call (stmt);
2757 vr1.type = gimple_expr_type (stmt);
2758 vr1.set = 0;
2759 vr1.hashcode = vn_reference_compute_hash (&vr1);
2760 vn_reference_lookup_1 (&vr1, &vnresult);
2762 if (vnresult)
2764 if (vnresult->result_vdef)
2765 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2767 if (!vnresult->result && lhs)
2768 vnresult->result = lhs;
2770 if (vnresult->result && lhs)
2772 changed |= set_ssa_val_to (lhs, vnresult->result);
2774 if (VN_INFO (vnresult->result)->has_constants)
2775 VN_INFO (lhs)->has_constants = true;
2778 else
2780 vn_reference_s **slot;
2781 vn_reference_t vr2;
2782 if (vdef)
2783 changed |= set_ssa_val_to (vdef, vdef);
2784 if (lhs)
2785 changed |= set_ssa_val_to (lhs, lhs);
2786 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
2787 vr2->vuse = vr1.vuse;
2788 vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
2789 vr2->type = vr1.type;
2790 vr2->set = vr1.set;
2791 vr2->hashcode = vr1.hashcode;
2792 vr2->result = lhs;
2793 vr2->result_vdef = vdef;
2794 slot = current_info->references.find_slot_with_hash (vr2, vr2->hashcode,
2795 INSERT);
2796 if (*slot)
2797 free_reference (*slot);
2798 *slot = vr2;
2801 return changed;
2804 /* Visit a load from a reference operator RHS, part of STMT, value number it,
2805 and return true if the value number of the LHS has changed as a result. */
2807 static bool
2808 visit_reference_op_load (tree lhs, tree op, gimple stmt)
2810 bool changed = false;
2811 tree last_vuse;
2812 tree result;
2814 last_vuse = gimple_vuse (stmt);
2815 last_vuse_ptr = &last_vuse;
2816 result = vn_reference_lookup (op, gimple_vuse (stmt),
2817 default_vn_walk_kind, NULL);
2818 last_vuse_ptr = NULL;
2820 /* If we have a VCE, try looking up its operand as it might be stored in
2821 a different type. */
2822 if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2823 result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
2824 default_vn_walk_kind, NULL);
2826 /* We handle type-punning through unions by value-numbering based
2827 on offset and size of the access. Be prepared to handle a
2828 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2829 if (result
2830 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2832 /* We will be setting the value number of lhs to the value number
2833 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2834 So first simplify and lookup this expression to see if it
2835 is already available. */
2836 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2837 if ((CONVERT_EXPR_P (val)
2838 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2839 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2841 tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2842 if ((CONVERT_EXPR_P (tem)
2843 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2844 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2845 TREE_TYPE (val), tem)))
2846 val = tem;
2848 result = val;
2849 if (!is_gimple_min_invariant (val)
2850 && TREE_CODE (val) != SSA_NAME)
2851 result = vn_nary_op_lookup (val, NULL);
2852 /* If the expression is not yet available, value-number lhs to
2853 a new SSA_NAME we create. */
2854 if (!result)
2856 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
2857 "vntemp");
2858 /* Initialize value-number information properly. */
2859 VN_INFO_GET (result)->valnum = result;
2860 VN_INFO (result)->value_id = get_next_value_id ();
2861 VN_INFO (result)->expr = val;
2862 VN_INFO (result)->has_constants = expr_has_constants (val);
2863 VN_INFO (result)->needs_insertion = true;
2864 /* As all "inserted" statements are singleton SCCs, insert
2865 to the valid table. This is strictly needed to
2866 avoid re-generating new value SSA_NAMEs for the same
2867 expression during SCC iteration over and over (the
2868 optimistic table gets cleared after each iteration).
2869 We do not need to insert into the optimistic table, as
2870 lookups there will fall back to the valid table. */
2871 if (current_info == optimistic_info)
2873 current_info = valid_info;
2874 vn_nary_op_insert (val, result);
2875 current_info = optimistic_info;
2877 else
2878 vn_nary_op_insert (val, result);
2879 if (dump_file && (dump_flags & TDF_DETAILS))
2881 fprintf (dump_file, "Inserting name ");
2882 print_generic_expr (dump_file, result, 0);
2883 fprintf (dump_file, " for expression ");
2884 print_generic_expr (dump_file, val, 0);
2885 fprintf (dump_file, "\n");
2890 if (result)
2892 changed = set_ssa_val_to (lhs, result);
2893 if (TREE_CODE (result) == SSA_NAME
2894 && VN_INFO (result)->has_constants)
2896 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2897 VN_INFO (lhs)->has_constants = true;
2900 else
2902 changed = set_ssa_val_to (lhs, lhs);
2903 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
2906 return changed;
2910 /* Visit a store to a reference operator LHS, part of STMT, value number it,
2911 and return true if the value number of the LHS has changed as a result. */
2913 static bool
2914 visit_reference_op_store (tree lhs, tree op, gimple stmt)
2916 bool changed = false;
2917 vn_reference_t vnresult = NULL;
2918 tree result, assign;
2919 bool resultsame = false;
2920 tree vuse = gimple_vuse (stmt);
2921 tree vdef = gimple_vdef (stmt);
2923 /* First we want to lookup using the *vuses* from the store and see
2924 if there the last store to this location with the same address
2925 had the same value.
2927 The vuses represent the memory state before the store. If the
2928 memory state, address, and value of the store is the same as the
2929 last store to this location, then this store will produce the
2930 same memory state as that store.
2932 In this case the vdef versions for this store are value numbered to those
2933 vuse versions, since they represent the same memory state after
2934 this store.
2936 Otherwise, the vdefs for the store are used when inserting into
2937 the table, since the store generates a new memory state. */
2939 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
2941 if (result)
2943 if (TREE_CODE (result) == SSA_NAME)
2944 result = SSA_VAL (result);
2945 if (TREE_CODE (op) == SSA_NAME)
2946 op = SSA_VAL (op);
2947 resultsame = expressions_equal_p (result, op);
2950 if (!result || !resultsame)
2952 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2953 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
2954 if (vnresult)
2956 VN_INFO (vdef)->use_processed = true;
2957 return set_ssa_val_to (vdef, vnresult->result_vdef);
2961 if (!result || !resultsame)
2963 if (dump_file && (dump_flags & TDF_DETAILS))
2965 fprintf (dump_file, "No store match\n");
2966 fprintf (dump_file, "Value numbering store ");
2967 print_generic_expr (dump_file, lhs, 0);
2968 fprintf (dump_file, " to ");
2969 print_generic_expr (dump_file, op, 0);
2970 fprintf (dump_file, "\n");
2972 /* Have to set value numbers before insert, since insert is
2973 going to valueize the references in-place. */
2974 if (vdef)
2976 changed |= set_ssa_val_to (vdef, vdef);
2979 /* Do not insert structure copies into the tables. */
2980 if (is_gimple_min_invariant (op)
2981 || is_gimple_reg (op))
2982 vn_reference_insert (lhs, op, vdef, NULL);
2984 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2985 vn_reference_insert (assign, lhs, vuse, vdef);
2987 else
2989 /* We had a match, so value number the vdef to have the value
2990 number of the vuse it came from. */
2992 if (dump_file && (dump_flags & TDF_DETAILS))
2993 fprintf (dump_file, "Store matched earlier value,"
2994 "value numbering store vdefs to matching vuses.\n");
2996 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
2999 return changed;
3002 /* Visit and value number PHI, return true if the value number
3003 changed. */
3005 static bool
3006 visit_phi (gimple phi)
3008 bool changed = false;
3009 tree result;
3010 tree sameval = VN_TOP;
3011 bool allsame = true;
3012 unsigned i;
3014 /* TODO: We could check for this in init_sccvn, and replace this
3015 with a gcc_assert. */
3016 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
3017 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3019 /* See if all non-TOP arguments have the same value. TOP is
3020 equivalent to everything, so we can ignore it. */
3021 for (i = 0; i < gimple_phi_num_args (phi); i++)
3023 tree def = PHI_ARG_DEF (phi, i);
3025 if (TREE_CODE (def) == SSA_NAME)
3026 def = SSA_VAL (def);
3027 if (def == VN_TOP)
3028 continue;
3029 if (sameval == VN_TOP)
3031 sameval = def;
3033 else
3035 if (!expressions_equal_p (def, sameval))
3037 allsame = false;
3038 break;
3043 /* If all value numbered to the same value, the phi node has that
3044 value. */
3045 if (allsame)
3047 if (is_gimple_min_invariant (sameval))
3049 VN_INFO (PHI_RESULT (phi))->has_constants = true;
3050 VN_INFO (PHI_RESULT (phi))->expr = sameval;
3052 else
3054 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3055 VN_INFO (PHI_RESULT (phi))->expr = sameval;
3058 if (TREE_CODE (sameval) == SSA_NAME)
3059 return visit_copy (PHI_RESULT (phi), sameval);
3061 return set_ssa_val_to (PHI_RESULT (phi), sameval);
3064 /* Otherwise, see if it is equivalent to a phi node in this block. */
3065 result = vn_phi_lookup (phi);
3066 if (result)
3068 if (TREE_CODE (result) == SSA_NAME)
3069 changed = visit_copy (PHI_RESULT (phi), result);
3070 else
3071 changed = set_ssa_val_to (PHI_RESULT (phi), result);
3073 else
3075 vn_phi_insert (phi, PHI_RESULT (phi));
3076 VN_INFO (PHI_RESULT (phi))->has_constants = false;
3077 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
3078 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
3081 return changed;
3084 /* Return true if EXPR contains constants. */
3086 static bool
3087 expr_has_constants (tree expr)
3089 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3091 case tcc_unary:
3092 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
3094 case tcc_binary:
3095 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
3096 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
3097 /* Constants inside reference ops are rarely interesting, but
3098 it can take a lot of looking to find them. */
3099 case tcc_reference:
3100 case tcc_declaration:
3101 return false;
3102 default:
3103 return is_gimple_min_invariant (expr);
3105 return false;
3108 /* Return true if STMT contains constants. */
3110 static bool
3111 stmt_has_constants (gimple stmt)
3113 tree tem;
3115 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3116 return false;
3118 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3120 case GIMPLE_TERNARY_RHS:
3121 tem = gimple_assign_rhs3 (stmt);
3122 if (TREE_CODE (tem) == SSA_NAME)
3123 tem = SSA_VAL (tem);
3124 if (is_gimple_min_invariant (tem))
3125 return true;
3126 /* Fallthru. */
3128 case GIMPLE_BINARY_RHS:
3129 tem = gimple_assign_rhs2 (stmt);
3130 if (TREE_CODE (tem) == SSA_NAME)
3131 tem = SSA_VAL (tem);
3132 if (is_gimple_min_invariant (tem))
3133 return true;
3134 /* Fallthru. */
3136 case GIMPLE_SINGLE_RHS:
3137 /* Constants inside reference ops are rarely interesting, but
3138 it can take a lot of looking to find them. */
3139 case GIMPLE_UNARY_RHS:
3140 tem = gimple_assign_rhs1 (stmt);
3141 if (TREE_CODE (tem) == SSA_NAME)
3142 tem = SSA_VAL (tem);
3143 return is_gimple_min_invariant (tem);
3145 default:
3146 gcc_unreachable ();
3148 return false;
3151 /* Replace SSA_NAMES in expr with their value numbers, and return the
3152 result.
3153 This is performed in place. */
3155 static tree
3156 valueize_expr (tree expr)
3158 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3160 case tcc_binary:
3161 TREE_OPERAND (expr, 1) = vn_valueize (TREE_OPERAND (expr, 1));
3162 /* Fallthru. */
3163 case tcc_unary:
3164 TREE_OPERAND (expr, 0) = vn_valueize (TREE_OPERAND (expr, 0));
3165 break;
3166 default:;
3168 return expr;
3171 /* Simplify the binary expression RHS, and return the result if
3172 simplified. */
3174 static tree
3175 simplify_binary_expression (gimple stmt)
3177 tree result = NULL_TREE;
3178 tree op0 = gimple_assign_rhs1 (stmt);
3179 tree op1 = gimple_assign_rhs2 (stmt);
3180 enum tree_code code = gimple_assign_rhs_code (stmt);
3182 /* This will not catch every single case we could combine, but will
3183 catch those with constants. The goal here is to simultaneously
3184 combine constants between expressions, but avoid infinite
3185 expansion of expressions during simplification. */
3186 if (TREE_CODE (op0) == SSA_NAME)
3188 if (VN_INFO (op0)->has_constants
3189 || TREE_CODE_CLASS (code) == tcc_comparison
3190 || code == COMPLEX_EXPR)
3191 op0 = valueize_expr (vn_get_expr_for (op0));
3192 else
3193 op0 = vn_valueize (op0);
3196 if (TREE_CODE (op1) == SSA_NAME)
3198 if (VN_INFO (op1)->has_constants
3199 || code == COMPLEX_EXPR)
3200 op1 = valueize_expr (vn_get_expr_for (op1));
3201 else
3202 op1 = vn_valueize (op1);
3205 /* Pointer plus constant can be represented as invariant address.
3206 Do so to allow further propatation, see also tree forwprop. */
3207 if (code == POINTER_PLUS_EXPR
3208 && host_integerp (op1, 1)
3209 && TREE_CODE (op0) == ADDR_EXPR
3210 && is_gimple_min_invariant (op0))
3211 return build_invariant_address (TREE_TYPE (op0),
3212 TREE_OPERAND (op0, 0),
3213 TREE_INT_CST_LOW (op1));
3215 /* Avoid folding if nothing changed. */
3216 if (op0 == gimple_assign_rhs1 (stmt)
3217 && op1 == gimple_assign_rhs2 (stmt))
3218 return NULL_TREE;
3220 fold_defer_overflow_warnings ();
3222 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
3223 if (result)
3224 STRIP_USELESS_TYPE_CONVERSION (result);
3226 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
3227 stmt, 0);
3229 /* Make sure result is not a complex expression consisting
3230 of operators of operators (IE (a + b) + (a + c))
3231 Otherwise, we will end up with unbounded expressions if
3232 fold does anything at all. */
3233 if (result && valid_gimple_rhs_p (result))
3234 return result;
3236 return NULL_TREE;
3239 /* Simplify the unary expression RHS, and return the result if
3240 simplified. */
3242 static tree
3243 simplify_unary_expression (gimple stmt)
3245 tree result = NULL_TREE;
3246 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
3247 enum tree_code code = gimple_assign_rhs_code (stmt);
3249 /* We handle some tcc_reference codes here that are all
3250 GIMPLE_ASSIGN_SINGLE codes. */
3251 if (code == REALPART_EXPR
3252 || code == IMAGPART_EXPR
3253 || code == VIEW_CONVERT_EXPR
3254 || code == BIT_FIELD_REF)
3255 op0 = TREE_OPERAND (op0, 0);
3257 if (TREE_CODE (op0) != SSA_NAME)
3258 return NULL_TREE;
3260 orig_op0 = op0;
3261 if (VN_INFO (op0)->has_constants)
3262 op0 = valueize_expr (vn_get_expr_for (op0));
3263 else if (CONVERT_EXPR_CODE_P (code)
3264 || code == REALPART_EXPR
3265 || code == IMAGPART_EXPR
3266 || code == VIEW_CONVERT_EXPR
3267 || code == BIT_FIELD_REF)
3269 /* We want to do tree-combining on conversion-like expressions.
3270 Make sure we feed only SSA_NAMEs or constants to fold though. */
3271 tree tem = valueize_expr (vn_get_expr_for (op0));
3272 if (UNARY_CLASS_P (tem)
3273 || BINARY_CLASS_P (tem)
3274 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
3275 || TREE_CODE (tem) == SSA_NAME
3276 || TREE_CODE (tem) == CONSTRUCTOR
3277 || is_gimple_min_invariant (tem))
3278 op0 = tem;
3281 /* Avoid folding if nothing changed, but remember the expression. */
3282 if (op0 == orig_op0)
3283 return NULL_TREE;
3285 if (code == BIT_FIELD_REF)
3287 tree rhs = gimple_assign_rhs1 (stmt);
3288 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3289 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3291 else
3292 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
3293 if (result)
3295 STRIP_USELESS_TYPE_CONVERSION (result);
3296 if (valid_gimple_rhs_p (result))
3297 return result;
3300 return NULL_TREE;
3303 /* Try to simplify RHS using equivalences and constant folding. */
3305 static tree
3306 try_to_simplify (gimple stmt)
3308 enum tree_code code = gimple_assign_rhs_code (stmt);
3309 tree tem;
3311 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3312 in this case, there is no point in doing extra work. */
3313 if (code == SSA_NAME)
3314 return NULL_TREE;
3316 /* First try constant folding based on our current lattice. */
3317 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize);
3318 if (tem
3319 && (TREE_CODE (tem) == SSA_NAME
3320 || is_gimple_min_invariant (tem)))
3321 return tem;
3323 /* If that didn't work try combining multiple statements. */
3324 switch (TREE_CODE_CLASS (code))
3326 case tcc_reference:
3327 /* Fallthrough for some unary codes that can operate on registers. */
3328 if (!(code == REALPART_EXPR
3329 || code == IMAGPART_EXPR
3330 || code == VIEW_CONVERT_EXPR
3331 || code == BIT_FIELD_REF))
3332 break;
3333 /* We could do a little more with unary ops, if they expand
3334 into binary ops, but it's debatable whether it is worth it. */
3335 case tcc_unary:
3336 return simplify_unary_expression (stmt);
3338 case tcc_comparison:
3339 case tcc_binary:
3340 return simplify_binary_expression (stmt);
3342 default:
3343 break;
3346 return NULL_TREE;
3349 /* Visit and value number USE, return true if the value number
3350 changed. */
3352 static bool
3353 visit_use (tree use)
3355 bool changed = false;
3356 gimple stmt = SSA_NAME_DEF_STMT (use);
3358 mark_use_processed (use);
3360 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3361 if (dump_file && (dump_flags & TDF_DETAILS)
3362 && !SSA_NAME_IS_DEFAULT_DEF (use))
3364 fprintf (dump_file, "Value numbering ");
3365 print_generic_expr (dump_file, use, 0);
3366 fprintf (dump_file, " stmt = ");
3367 print_gimple_stmt (dump_file, stmt, 0, 0);
3370 /* Handle uninitialized uses. */
3371 if (SSA_NAME_IS_DEFAULT_DEF (use))
3372 changed = set_ssa_val_to (use, use);
3373 else
3375 if (gimple_code (stmt) == GIMPLE_PHI)
3376 changed = visit_phi (stmt);
3377 else if (gimple_has_volatile_ops (stmt))
3378 changed = defs_to_varying (stmt);
3379 else if (is_gimple_assign (stmt))
3381 enum tree_code code = gimple_assign_rhs_code (stmt);
3382 tree lhs = gimple_assign_lhs (stmt);
3383 tree rhs1 = gimple_assign_rhs1 (stmt);
3384 tree simplified;
3386 /* Shortcut for copies. Simplifying copies is pointless,
3387 since we copy the expression and value they represent. */
3388 if (code == SSA_NAME
3389 && TREE_CODE (lhs) == SSA_NAME)
3391 changed = visit_copy (lhs, rhs1);
3392 goto done;
3394 simplified = try_to_simplify (stmt);
3395 if (simplified)
3397 if (dump_file && (dump_flags & TDF_DETAILS))
3399 fprintf (dump_file, "RHS ");
3400 print_gimple_expr (dump_file, stmt, 0, 0);
3401 fprintf (dump_file, " simplified to ");
3402 print_generic_expr (dump_file, simplified, 0);
3403 if (TREE_CODE (lhs) == SSA_NAME)
3404 fprintf (dump_file, " has constants %d\n",
3405 expr_has_constants (simplified));
3406 else
3407 fprintf (dump_file, "\n");
3410 /* Setting value numbers to constants will occasionally
3411 screw up phi congruence because constants are not
3412 uniquely associated with a single ssa name that can be
3413 looked up. */
3414 if (simplified
3415 && is_gimple_min_invariant (simplified)
3416 && TREE_CODE (lhs) == SSA_NAME)
3418 VN_INFO (lhs)->expr = simplified;
3419 VN_INFO (lhs)->has_constants = true;
3420 changed = set_ssa_val_to (lhs, simplified);
3421 goto done;
3423 else if (simplified
3424 && TREE_CODE (simplified) == SSA_NAME
3425 && TREE_CODE (lhs) == SSA_NAME)
3427 changed = visit_copy (lhs, simplified);
3428 goto done;
3430 else if (simplified)
3432 if (TREE_CODE (lhs) == SSA_NAME)
3434 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3435 /* We have to unshare the expression or else
3436 valuizing may change the IL stream. */
3437 VN_INFO (lhs)->expr = unshare_expr (simplified);
3440 else if (stmt_has_constants (stmt)
3441 && TREE_CODE (lhs) == SSA_NAME)
3442 VN_INFO (lhs)->has_constants = true;
3443 else if (TREE_CODE (lhs) == SSA_NAME)
3445 /* We reset expr and constantness here because we may
3446 have been value numbering optimistically, and
3447 iterating. They may become non-constant in this case,
3448 even if they were optimistically constant. */
3450 VN_INFO (lhs)->has_constants = false;
3451 VN_INFO (lhs)->expr = NULL_TREE;
3454 if ((TREE_CODE (lhs) == SSA_NAME
3455 /* We can substitute SSA_NAMEs that are live over
3456 abnormal edges with their constant value. */
3457 && !(gimple_assign_copy_p (stmt)
3458 && is_gimple_min_invariant (rhs1))
3459 && !(simplified
3460 && is_gimple_min_invariant (simplified))
3461 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3462 /* Stores or copies from SSA_NAMEs that are live over
3463 abnormal edges are a problem. */
3464 || (code == SSA_NAME
3465 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
3466 changed = defs_to_varying (stmt);
3467 else if (REFERENCE_CLASS_P (lhs)
3468 || DECL_P (lhs))
3469 changed = visit_reference_op_store (lhs, rhs1, stmt);
3470 else if (TREE_CODE (lhs) == SSA_NAME)
3472 if ((gimple_assign_copy_p (stmt)
3473 && is_gimple_min_invariant (rhs1))
3474 || (simplified
3475 && is_gimple_min_invariant (simplified)))
3477 VN_INFO (lhs)->has_constants = true;
3478 if (simplified)
3479 changed = set_ssa_val_to (lhs, simplified);
3480 else
3481 changed = set_ssa_val_to (lhs, rhs1);
3483 else
3485 /* First try to lookup the simplified expression. */
3486 if (simplified)
3488 enum gimple_rhs_class rhs_class;
3491 rhs_class = get_gimple_rhs_class (TREE_CODE (simplified));
3492 if ((rhs_class == GIMPLE_UNARY_RHS
3493 || rhs_class == GIMPLE_BINARY_RHS
3494 || rhs_class == GIMPLE_TERNARY_RHS)
3495 && valid_gimple_rhs_p (simplified))
3497 tree result = vn_nary_op_lookup (simplified, NULL);
3498 if (result)
3500 changed = set_ssa_val_to (lhs, result);
3501 goto done;
3506 /* Otherwise visit the original statement. */
3507 switch (vn_get_stmt_kind (stmt))
3509 case VN_NARY:
3510 changed = visit_nary_op (lhs, stmt);
3511 break;
3512 case VN_REFERENCE:
3513 changed = visit_reference_op_load (lhs, rhs1, stmt);
3514 break;
3515 default:
3516 changed = defs_to_varying (stmt);
3517 break;
3521 else
3522 changed = defs_to_varying (stmt);
3524 else if (is_gimple_call (stmt))
3526 tree lhs = gimple_call_lhs (stmt);
3528 /* ??? We could try to simplify calls. */
3530 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3532 if (stmt_has_constants (stmt))
3533 VN_INFO (lhs)->has_constants = true;
3534 else
3536 /* We reset expr and constantness here because we may
3537 have been value numbering optimistically, and
3538 iterating. They may become non-constant in this case,
3539 even if they were optimistically constant. */
3540 VN_INFO (lhs)->has_constants = false;
3541 VN_INFO (lhs)->expr = NULL_TREE;
3544 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3546 changed = defs_to_varying (stmt);
3547 goto done;
3551 if (!gimple_call_internal_p (stmt)
3552 && (/* Calls to the same function with the same vuse
3553 and the same operands do not necessarily return the same
3554 value, unless they're pure or const. */
3555 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3556 /* If calls have a vdef, subsequent calls won't have
3557 the same incoming vuse. So, if 2 calls with vdef have the
3558 same vuse, we know they're not subsequent.
3559 We can value number 2 calls to the same function with the
3560 same vuse and the same operands which are not subsequent
3561 the same, because there is no code in the program that can
3562 compare the 2 values... */
3563 || (gimple_vdef (stmt)
3564 /* ... unless the call returns a pointer which does
3565 not alias with anything else. In which case the
3566 information that the values are distinct are encoded
3567 in the IL. */
3568 && !(gimple_call_return_flags (stmt) & ERF_NOALIAS))))
3569 changed = visit_reference_op_call (lhs, stmt);
3570 else
3571 changed = defs_to_varying (stmt);
3573 else
3574 changed = defs_to_varying (stmt);
3576 done:
3577 return changed;
3580 /* Compare two operands by reverse postorder index */
3582 static int
3583 compare_ops (const void *pa, const void *pb)
3585 const tree opa = *((const tree *)pa);
3586 const tree opb = *((const tree *)pb);
3587 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3588 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
3589 basic_block bba;
3590 basic_block bbb;
3592 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3593 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3594 else if (gimple_nop_p (opstmta))
3595 return -1;
3596 else if (gimple_nop_p (opstmtb))
3597 return 1;
3599 bba = gimple_bb (opstmta);
3600 bbb = gimple_bb (opstmtb);
3602 if (!bba && !bbb)
3603 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3604 else if (!bba)
3605 return -1;
3606 else if (!bbb)
3607 return 1;
3609 if (bba == bbb)
3611 if (gimple_code (opstmta) == GIMPLE_PHI
3612 && gimple_code (opstmtb) == GIMPLE_PHI)
3613 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3614 else if (gimple_code (opstmta) == GIMPLE_PHI)
3615 return -1;
3616 else if (gimple_code (opstmtb) == GIMPLE_PHI)
3617 return 1;
3618 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3619 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3620 else
3621 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
3623 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3626 /* Sort an array containing members of a strongly connected component
3627 SCC so that the members are ordered by RPO number.
3628 This means that when the sort is complete, iterating through the
3629 array will give you the members in RPO order. */
3631 static void
3632 sort_scc (vec<tree> scc)
3634 scc.qsort (compare_ops);
3637 /* Insert the no longer used nary ONARY to the hash INFO. */
3639 static void
3640 copy_nary (vn_nary_op_t onary, vn_tables_t info)
3642 size_t size = sizeof_vn_nary_op (onary->length);
3643 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3644 &info->nary_obstack);
3645 memcpy (nary, onary, size);
3646 vn_nary_op_insert_into (nary, info->nary, false);
3649 /* Insert the no longer used phi OPHI to the hash INFO. */
3651 static void
3652 copy_phi (vn_phi_t ophi, vn_tables_t info)
3654 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
3655 vn_phi_s **slot;
3656 memcpy (phi, ophi, sizeof (*phi));
3657 ophi->phiargs.create (0);
3658 slot = info->phis.find_slot_with_hash (phi, phi->hashcode, INSERT);
3659 gcc_assert (!*slot);
3660 *slot = phi;
3663 /* Insert the no longer used reference OREF to the hash INFO. */
3665 static void
3666 copy_reference (vn_reference_t oref, vn_tables_t info)
3668 vn_reference_t ref;
3669 vn_reference_s **slot;
3670 ref = (vn_reference_t) pool_alloc (info->references_pool);
3671 memcpy (ref, oref, sizeof (*ref));
3672 oref->operands.create (0);
3673 slot = info->references.find_slot_with_hash (ref, ref->hashcode, INSERT);
3674 if (*slot)
3675 free_reference (*slot);
3676 *slot = ref;
3679 /* Process a strongly connected component in the SSA graph. */
3681 static void
3682 process_scc (vec<tree> scc)
3684 tree var;
3685 unsigned int i;
3686 unsigned int iterations = 0;
3687 bool changed = true;
3688 vn_nary_op_iterator_type hin;
3689 vn_phi_iterator_type hip;
3690 vn_reference_iterator_type hir;
3691 vn_nary_op_t nary;
3692 vn_phi_t phi;
3693 vn_reference_t ref;
3695 /* If the SCC has a single member, just visit it. */
3696 if (scc.length () == 1)
3698 tree use = scc[0];
3699 if (VN_INFO (use)->use_processed)
3700 return;
3701 /* We need to make sure it doesn't form a cycle itself, which can
3702 happen for self-referential PHI nodes. In that case we would
3703 end up inserting an expression with VN_TOP operands into the
3704 valid table which makes us derive bogus equivalences later.
3705 The cheapest way to check this is to assume it for all PHI nodes. */
3706 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3707 /* Fallthru to iteration. */ ;
3708 else
3710 visit_use (use);
3711 return;
3715 /* Iterate over the SCC with the optimistic table until it stops
3716 changing. */
3717 current_info = optimistic_info;
3718 while (changed)
3720 changed = false;
3721 iterations++;
3722 if (dump_file && (dump_flags & TDF_DETAILS))
3723 fprintf (dump_file, "Starting iteration %d\n", iterations);
3724 /* As we are value-numbering optimistically we have to
3725 clear the expression tables and the simplified expressions
3726 in each iteration until we converge. */
3727 optimistic_info->nary.empty ();
3728 optimistic_info->phis.empty ();
3729 optimistic_info->references.empty ();
3730 obstack_free (&optimistic_info->nary_obstack, NULL);
3731 gcc_obstack_init (&optimistic_info->nary_obstack);
3732 empty_alloc_pool (optimistic_info->phis_pool);
3733 empty_alloc_pool (optimistic_info->references_pool);
3734 FOR_EACH_VEC_ELT (scc, i, var)
3735 VN_INFO (var)->expr = NULL_TREE;
3736 FOR_EACH_VEC_ELT (scc, i, var)
3737 changed |= visit_use (var);
3740 statistics_histogram_event (cfun, "SCC iterations", iterations);
3742 /* Finally, copy the contents of the no longer used optimistic
3743 table to the valid table. */
3744 FOR_EACH_HASH_TABLE_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hin)
3745 copy_nary (nary, valid_info);
3746 FOR_EACH_HASH_TABLE_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hip)
3747 copy_phi (phi, valid_info);
3748 FOR_EACH_HASH_TABLE_ELEMENT (optimistic_info->references,
3749 ref, vn_reference_t, hir)
3750 copy_reference (ref, valid_info);
3752 current_info = valid_info;
3756 /* Pop the components of the found SCC for NAME off the SCC stack
3757 and process them. Returns true if all went well, false if
3758 we run into resource limits. */
3760 static bool
3761 extract_and_process_scc_for_name (tree name)
3763 vec<tree> scc = vNULL;
3764 tree x;
3766 /* Found an SCC, pop the components off the SCC stack and
3767 process them. */
3770 x = sccstack.pop ();
3772 VN_INFO (x)->on_sccstack = false;
3773 scc.safe_push (x);
3774 } while (x != name);
3776 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3777 if (scc.length ()
3778 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3780 if (dump_file)
3781 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3782 "SCC size %u exceeding %u\n", scc.length (),
3783 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3785 scc.release ();
3786 return false;
3789 if (scc.length () > 1)
3790 sort_scc (scc);
3792 if (dump_file && (dump_flags & TDF_DETAILS))
3793 print_scc (dump_file, scc);
3795 process_scc (scc);
3797 scc.release ();
3799 return true;
3802 /* Depth first search on NAME to discover and process SCC's in the SSA
3803 graph.
3804 Execution of this algorithm relies on the fact that the SCC's are
3805 popped off the stack in topological order.
3806 Returns true if successful, false if we stopped processing SCC's due
3807 to resource constraints. */
3809 static bool
3810 DFS (tree name)
3812 vec<ssa_op_iter> itervec = vNULL;
3813 vec<tree> namevec = vNULL;
3814 use_operand_p usep = NULL;
3815 gimple defstmt;
3816 tree use;
3817 ssa_op_iter iter;
3819 start_over:
3820 /* SCC info */
3821 VN_INFO (name)->dfsnum = next_dfs_num++;
3822 VN_INFO (name)->visited = true;
3823 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3825 sccstack.safe_push (name);
3826 VN_INFO (name)->on_sccstack = true;
3827 defstmt = SSA_NAME_DEF_STMT (name);
3829 /* Recursively DFS on our operands, looking for SCC's. */
3830 if (!gimple_nop_p (defstmt))
3832 /* Push a new iterator. */
3833 if (gimple_code (defstmt) == GIMPLE_PHI)
3834 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3835 else
3836 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3838 else
3839 clear_and_done_ssa_iter (&iter);
3841 while (1)
3843 /* If we are done processing uses of a name, go up the stack
3844 of iterators and process SCCs as we found them. */
3845 if (op_iter_done (&iter))
3847 /* See if we found an SCC. */
3848 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3849 if (!extract_and_process_scc_for_name (name))
3851 namevec.release ();
3852 itervec.release ();
3853 return false;
3856 /* Check if we are done. */
3857 if (namevec.is_empty ())
3859 namevec.release ();
3860 itervec.release ();
3861 return true;
3864 /* Restore the last use walker and continue walking there. */
3865 use = name;
3866 name = namevec.pop ();
3867 memcpy (&iter, &itervec.last (),
3868 sizeof (ssa_op_iter));
3869 itervec.pop ();
3870 goto continue_walking;
3873 use = USE_FROM_PTR (usep);
3875 /* Since we handle phi nodes, we will sometimes get
3876 invariants in the use expression. */
3877 if (TREE_CODE (use) == SSA_NAME)
3879 if (! (VN_INFO (use)->visited))
3881 /* Recurse by pushing the current use walking state on
3882 the stack and starting over. */
3883 itervec.safe_push (iter);
3884 namevec.safe_push (name);
3885 name = use;
3886 goto start_over;
3888 continue_walking:
3889 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3890 VN_INFO (use)->low);
3892 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3893 && VN_INFO (use)->on_sccstack)
3895 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3896 VN_INFO (name)->low);
3900 usep = op_iter_next_use (&iter);
3904 /* Allocate a value number table. */
3906 static void
3907 allocate_vn_table (vn_tables_t table)
3909 table->phis.create (23);
3910 table->nary.create (23);
3911 table->references.create (23);
3913 gcc_obstack_init (&table->nary_obstack);
3914 table->phis_pool = create_alloc_pool ("VN phis",
3915 sizeof (struct vn_phi_s),
3916 30);
3917 table->references_pool = create_alloc_pool ("VN references",
3918 sizeof (struct vn_reference_s),
3919 30);
3922 /* Free a value number table. */
3924 static void
3925 free_vn_table (vn_tables_t table)
3927 table->phis.dispose ();
3928 table->nary.dispose ();
3929 table->references.dispose ();
3930 obstack_free (&table->nary_obstack, NULL);
3931 free_alloc_pool (table->phis_pool);
3932 free_alloc_pool (table->references_pool);
3935 static void
3936 init_scc_vn (void)
3938 size_t i;
3939 int j;
3940 int *rpo_numbers_temp;
3942 calculate_dominance_info (CDI_DOMINATORS);
3943 sccstack.create (0);
3944 constant_to_value_id.create (23);
3946 constant_value_ids = BITMAP_ALLOC (NULL);
3948 next_dfs_num = 1;
3949 next_value_id = 1;
3951 vn_ssa_aux_table.create (num_ssa_names + 1);
3952 /* VEC_alloc doesn't actually grow it to the right size, it just
3953 preallocates the space to do so. */
3954 vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1);
3955 gcc_obstack_init (&vn_ssa_aux_obstack);
3957 shared_lookup_phiargs.create (0);
3958 shared_lookup_references.create (0);
3959 rpo_numbers = XNEWVEC (int, last_basic_block);
3960 rpo_numbers_temp = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
3961 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3963 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3964 the i'th block in RPO order is bb. We want to map bb's to RPO
3965 numbers, so we need to rearrange this array. */
3966 for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3967 rpo_numbers[rpo_numbers_temp[j]] = j;
3969 XDELETE (rpo_numbers_temp);
3971 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3973 /* Create the VN_INFO structures, and initialize value numbers to
3974 TOP. */
3975 for (i = 0; i < num_ssa_names; i++)
3977 tree name = ssa_name (i);
3978 if (name)
3980 VN_INFO_GET (name)->valnum = VN_TOP;
3981 VN_INFO (name)->expr = NULL_TREE;
3982 VN_INFO (name)->value_id = 0;
3986 renumber_gimple_stmt_uids ();
3988 /* Create the valid and optimistic value numbering tables. */
3989 valid_info = XCNEW (struct vn_tables_s);
3990 allocate_vn_table (valid_info);
3991 optimistic_info = XCNEW (struct vn_tables_s);
3992 allocate_vn_table (optimistic_info);
3995 void
3996 free_scc_vn (void)
3998 size_t i;
4000 constant_to_value_id.dispose ();
4001 BITMAP_FREE (constant_value_ids);
4002 shared_lookup_phiargs.release ();
4003 shared_lookup_references.release ();
4004 XDELETEVEC (rpo_numbers);
4006 for (i = 0; i < num_ssa_names; i++)
4008 tree name = ssa_name (i);
4009 if (name
4010 && VN_INFO (name)->needs_insertion)
4011 release_ssa_name (name);
4013 obstack_free (&vn_ssa_aux_obstack, NULL);
4014 vn_ssa_aux_table.release ();
4016 sccstack.release ();
4017 free_vn_table (valid_info);
4018 XDELETE (valid_info);
4019 free_vn_table (optimistic_info);
4020 XDELETE (optimistic_info);
4023 /* Set *ID according to RESULT. */
4025 static void
4026 set_value_id_for_result (tree result, unsigned int *id)
4028 if (result && TREE_CODE (result) == SSA_NAME)
4029 *id = VN_INFO (result)->value_id;
4030 else if (result && is_gimple_min_invariant (result))
4031 *id = get_or_alloc_constant_value_id (result);
4032 else
4033 *id = get_next_value_id ();
4036 /* Set the value ids in the valid hash tables. */
4038 static void
4039 set_hashtable_value_ids (void)
4041 vn_nary_op_iterator_type hin;
4042 vn_phi_iterator_type hip;
4043 vn_reference_iterator_type hir;
4044 vn_nary_op_t vno;
4045 vn_reference_t vr;
4046 vn_phi_t vp;
4048 /* Now set the value ids of the things we had put in the hash
4049 table. */
4051 FOR_EACH_HASH_TABLE_ELEMENT (valid_info->nary, vno, vn_nary_op_t, hin)
4052 set_value_id_for_result (vno->result, &vno->value_id);
4054 FOR_EACH_HASH_TABLE_ELEMENT (valid_info->phis, vp, vn_phi_t, hip)
4055 set_value_id_for_result (vp->result, &vp->value_id);
4057 FOR_EACH_HASH_TABLE_ELEMENT (valid_info->references, vr, vn_reference_t, hir)
4058 set_value_id_for_result (vr->result, &vr->value_id);
4061 /* Do SCCVN. Returns true if it finished, false if we bailed out
4062 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
4063 how we use the alias oracle walking during the VN process. */
4065 bool
4066 run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
4068 size_t i;
4069 tree param;
4071 default_vn_walk_kind = default_vn_walk_kind_;
4073 init_scc_vn ();
4074 current_info = valid_info;
4076 for (param = DECL_ARGUMENTS (current_function_decl);
4077 param;
4078 param = DECL_CHAIN (param))
4080 tree def = ssa_default_def (cfun, param);
4081 if (def)
4082 VN_INFO (def)->valnum = def;
4085 for (i = 1; i < num_ssa_names; ++i)
4087 tree name = ssa_name (i);
4088 if (name
4089 && VN_INFO (name)->visited == false
4090 && !has_zero_uses (name))
4091 if (!DFS (name))
4093 free_scc_vn ();
4094 return false;
4098 /* Initialize the value ids. */
4100 for (i = 1; i < num_ssa_names; ++i)
4102 tree name = ssa_name (i);
4103 vn_ssa_aux_t info;
4104 if (!name)
4105 continue;
4106 info = VN_INFO (name);
4107 if (info->valnum == name
4108 || info->valnum == VN_TOP)
4109 info->value_id = get_next_value_id ();
4110 else if (is_gimple_min_invariant (info->valnum))
4111 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4114 /* Propagate. */
4115 for (i = 1; i < num_ssa_names; ++i)
4117 tree name = ssa_name (i);
4118 vn_ssa_aux_t info;
4119 if (!name)
4120 continue;
4121 info = VN_INFO (name);
4122 if (TREE_CODE (info->valnum) == SSA_NAME
4123 && info->valnum != name
4124 && info->value_id != VN_INFO (info->valnum)->value_id)
4125 info->value_id = VN_INFO (info->valnum)->value_id;
4128 set_hashtable_value_ids ();
4130 if (dump_file && (dump_flags & TDF_DETAILS))
4132 fprintf (dump_file, "Value numbers:\n");
4133 for (i = 0; i < num_ssa_names; i++)
4135 tree name = ssa_name (i);
4136 if (name
4137 && VN_INFO (name)->visited
4138 && SSA_VAL (name) != name)
4140 print_generic_expr (dump_file, name, 0);
4141 fprintf (dump_file, " = ");
4142 print_generic_expr (dump_file, SSA_VAL (name), 0);
4143 fprintf (dump_file, "\n");
4148 return true;
4151 /* Return the maximum value id we have ever seen. */
4153 unsigned int
4154 get_max_value_id (void)
4156 return next_value_id;
4159 /* Return the next unique value id. */
4161 unsigned int
4162 get_next_value_id (void)
4164 return next_value_id++;
4168 /* Compare two expressions E1 and E2 and return true if they are equal. */
4170 bool
4171 expressions_equal_p (tree e1, tree e2)
4173 /* The obvious case. */
4174 if (e1 == e2)
4175 return true;
4177 /* If only one of them is null, they cannot be equal. */
4178 if (!e1 || !e2)
4179 return false;
4181 /* Now perform the actual comparison. */
4182 if (TREE_CODE (e1) == TREE_CODE (e2)
4183 && operand_equal_p (e1, e2, OEP_PURE_SAME))
4184 return true;
4186 return false;
4190 /* Return true if the nary operation NARY may trap. This is a copy
4191 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4193 bool
4194 vn_nary_may_trap (vn_nary_op_t nary)
4196 tree type;
4197 tree rhs2 = NULL_TREE;
4198 bool honor_nans = false;
4199 bool honor_snans = false;
4200 bool fp_operation = false;
4201 bool honor_trapv = false;
4202 bool handled, ret;
4203 unsigned i;
4205 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4206 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4207 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4209 type = nary->type;
4210 fp_operation = FLOAT_TYPE_P (type);
4211 if (fp_operation)
4213 honor_nans = flag_trapping_math && !flag_finite_math_only;
4214 honor_snans = flag_signaling_nans != 0;
4216 else if (INTEGRAL_TYPE_P (type)
4217 && TYPE_OVERFLOW_TRAPS (type))
4218 honor_trapv = true;
4220 if (nary->length >= 2)
4221 rhs2 = nary->op[1];
4222 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4223 honor_trapv,
4224 honor_nans, honor_snans, rhs2,
4225 &handled);
4226 if (handled
4227 && ret)
4228 return true;
4230 for (i = 0; i < nary->length; ++i)
4231 if (tree_could_trap_p (nary->op[i]))
4232 return true;
4234 return false;