1 /* SCC value numbering for trees
2 Copyright (C) 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dan@dberlin.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-inline.h"
31 #include "tree-flow.h"
32 #include "tree-gimple.h"
33 #include "tree-dump.h"
37 #include "tree-iterator.h"
39 #include "alloc-pool.h"
40 #include "tree-pass.h"
43 #include "langhooks.h"
46 #include "tree-ssa-propagate.h"
47 #include "tree-ssa-sccvn.h"
49 /* This algorithm is based on the SCC algorithm presented by Keith
50 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
51 (http://citeseer.ist.psu.edu/41805.html). In
52 straight line code, it is equivalent to a regular hash based value
53 numbering that is performed in reverse postorder.
55 For code with cycles, there are two alternatives, both of which
56 require keeping the hashtables separate from the actual list of
57 value numbers for SSA names.
59 1. Iterate value numbering in an RPO walk of the blocks, removing
60 all the entries from the hashtable after each iteration (but
61 keeping the SSA name->value number mapping between iterations).
62 Iterate until it does not change.
64 2. Perform value numbering as part of an SCC walk on the SSA graph,
65 iterating only the cycles in the SSA graph until they do not change
66 (using a separate, optimistic hashtable for value numbering the SCC
69 The second is not just faster in practice (because most SSA graph
70 cycles do not involve all the variables in the graph), it also has
73 One of these nice properties is that when we pop an SCC off the
74 stack, we are guaranteed to have processed all the operands coming from
75 *outside of that SCC*, so we do not need to do anything special to
76 ensure they have value numbers.
78 Another nice property is that the SCC walk is done as part of a DFS
79 of the SSA graph, which makes it easy to perform combining and
80 simplifying operations at the same time.
82 The code below is deliberately written in a way that makes it easy
83 to separate the SCC walk from the other work it does.
85 In order to propagate constants through the code, we track which
86 expressions contain constants, and use those while folding. In
87 theory, we could also track expressions whose value numbers are
88 replaced, in case we end up folding based on expression
91 In order to value number memory, we assign value numbers to vuses.
92 This enables us to note that, for example, stores to the same
93 address of the same value from the same starting memory states are
97 1. We can iterate only the changing portions of the SCC's, but
98 I have not seen an SCC big enough for this to be a win.
99 2. If you differentiate between phi nodes for loops and phi nodes
100 for if-then-else, you can properly consider phi nodes in different
101 blocks for equivalence.
102 3. We could value number vuses in more cases, particularly, whole
106 /* The set of hashtables and alloc_pool's for their items. */
108 typedef struct vn_tables_s
113 struct obstack nary_obstack;
114 alloc_pool phis_pool;
115 alloc_pool references_pool;
118 /* Nary operations in the hashtable consist of length operands, an
119 opcode, and a type. Result is the value number of the operation,
120 and hashcode is stored to avoid having to calculate it
123 typedef struct vn_nary_op_s
125 ENUM_BITFIELD(tree_code) opcode : 16;
126 unsigned length : 16;
132 typedef const struct vn_nary_op_s *const_vn_nary_op_t;
134 /* Phi nodes in the hashtable consist of their non-VN_TOP phi
135 arguments, and the basic block the phi is in. Result is the value
136 number of the operation, and hashcode is stored to avoid having to
137 calculate it repeatedly. Phi nodes not in the same block are never
138 considered equivalent. */
140 typedef struct vn_phi_s
142 VEC (tree, heap) *phiargs;
147 typedef const struct vn_phi_s *const_vn_phi_t;
149 /* Reference operands only exist in reference operations structures.
150 They consist of an opcode, type, and some number of operands. For
151 a given opcode, some, all, or none of the operands may be used.
152 The operands are there to store the information that makes up the
153 portion of the addressing calculation that opcode performs. */
155 typedef struct vn_reference_op_struct
157 enum tree_code opcode;
162 typedef vn_reference_op_s *vn_reference_op_t;
163 typedef const vn_reference_op_s *const_vn_reference_op_t;
165 DEF_VEC_O(vn_reference_op_s);
166 DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
168 /* A reference operation in the hashtable is representation as a
169 collection of vuses, representing the memory state at the time of
170 the operation, and a collection of operands that make up the
171 addressing calculation. If two vn_reference_t's have the same set
172 of operands, they access the same memory location. We also store
173 the resulting value number, and the hashcode. The vuses are
174 always stored in order sorted by ssa name version. */
176 typedef struct vn_reference_s
178 VEC (tree, gc) *vuses;
179 VEC (vn_reference_op_s, heap) *operands;
183 typedef const struct vn_reference_s *const_vn_reference_t;
185 /* Valid hashtables storing information we have proven to be
188 static vn_tables_t valid_info;
190 /* Optimistic hashtables storing information we are making assumptions about
191 during iterations. */
193 static vn_tables_t optimistic_info;
195 /* PRE hashtables storing information about mapping from expressions to
198 static vn_tables_t pre_info;
200 /* Pointer to the set of hashtables that is currently being used.
201 Should always point to either the optimistic_info, or the
204 static vn_tables_t current_info;
207 /* Reverse post order index for each basic block. */
209 static int *rpo_numbers;
211 #define SSA_VAL(x) (VN_INFO ((x))->valnum)
213 /* This represents the top of the VN lattice, which is the universal
218 /* Next DFS number and the stack for strongly connected component
221 static unsigned int next_dfs_num;
222 static VEC (tree, heap) *sccstack;
224 static bool may_insert;
227 DEF_VEC_P(vn_ssa_aux_t);
228 DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
230 /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
231 are allocated on an obstack for locality reasons, and to free them
232 without looping over the VEC. */
234 static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
235 static struct obstack vn_ssa_aux_obstack;
237 /* Return the value numbering information for a given SSA name. */
242 return VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
243 SSA_NAME_VERSION (name));
246 /* Set the value numbering info for a given SSA name to a given
250 VN_INFO_SET (tree name, vn_ssa_aux_t value)
252 VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
253 SSA_NAME_VERSION (name), value);
256 /* Initialize the value numbering info for a given SSA name.
257 This should be called just once for every SSA name. */
260 VN_INFO_GET (tree name)
262 vn_ssa_aux_t newinfo;
264 newinfo = obstack_alloc (&vn_ssa_aux_obstack, sizeof (struct vn_ssa_aux));
265 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
266 if (SSA_NAME_VERSION (name) >= VEC_length (vn_ssa_aux_t, vn_ssa_aux_table))
267 VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table,
268 SSA_NAME_VERSION (name) + 1);
269 VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
270 SSA_NAME_VERSION (name), newinfo);
275 /* Free a phi operation structure VP. */
281 VEC_free (tree, heap, phi->phiargs);
284 /* Free a reference operation structure VP. */
287 free_reference (void *vp)
289 vn_reference_t vr = vp;
290 VEC_free (vn_reference_op_s, heap, vr->operands);
293 /* Compare two reference operands P1 and P2 for equality. return true if
294 they are equal, and false otherwise. */
297 vn_reference_op_eq (const void *p1, const void *p2)
299 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
300 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
301 return vro1->opcode == vro2->opcode
302 && vro1->type == vro2->type
303 && expressions_equal_p (vro1->op0, vro2->op0)
304 && expressions_equal_p (vro1->op1, vro2->op1);
307 /* Compute the hash for a reference operand VRO1 */
310 vn_reference_op_compute_hash (const vn_reference_op_t vro1)
312 return iterative_hash_expr (vro1->op0, vro1->opcode)
313 + iterative_hash_expr (vro1->op1, vro1->opcode);
316 /* Return the hashcode for a given reference operation P1. */
319 vn_reference_hash (const void *p1)
321 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
322 return vr1->hashcode;
325 /* Compute a hash for the reference operation VR1 and return it. */
327 static inline hashval_t
328 vn_reference_compute_hash (const vn_reference_t vr1)
330 hashval_t result = 0;
333 vn_reference_op_t vro;
335 for (i = 0; VEC_iterate (tree, vr1->vuses, i, v); i++)
336 result += iterative_hash_expr (v, 0);
337 for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
338 result += vn_reference_op_compute_hash (vro);
343 /* Return true if reference operations P1 and P2 are equivalent. This
344 means they have the same set of operands and vuses. */
347 vn_reference_eq (const void *p1, const void *p2)
351 vn_reference_op_t vro;
353 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
354 const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
356 if (vr1->vuses == vr2->vuses
357 && vr1->operands == vr2->operands)
360 /* Impossible for them to be equivalent if they have different
362 if (VEC_length (tree, vr1->vuses) != VEC_length (tree, vr2->vuses))
365 /* We require that address operands be canonicalized in a way that
366 two memory references will have the same operands if they are
368 if (VEC_length (vn_reference_op_s, vr1->operands)
369 != VEC_length (vn_reference_op_s, vr2->operands))
372 /* The memory state is more often different than the address of the
373 store/load, so check it first. */
374 for (i = 0; VEC_iterate (tree, vr1->vuses, i, v); i++)
376 if (VEC_index (tree, vr2->vuses, i) != v)
380 for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
382 if (!vn_reference_op_eq (VEC_index (vn_reference_op_s, vr2->operands, i),
389 /* Place the vuses from STMT into *result */
392 vuses_to_vec (tree stmt, VEC (tree, gc) **result)
400 VEC_reserve_exact (tree, gc, *result,
401 num_ssa_operands (stmt, SSA_OP_VIRTUAL_USES));
403 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, iter, SSA_OP_VIRTUAL_USES)
404 VEC_quick_push (tree, *result, vuse);
408 /* Copy the VUSE names in STMT into a vector, and return
412 copy_vuses_from_stmt (tree stmt)
414 VEC (tree, gc) *vuses = NULL;
416 vuses_to_vec (stmt, &vuses);
421 /* Place the vdefs from STMT into *result */
424 vdefs_to_vec (tree stmt, VEC (tree, gc) **result)
432 *result = VEC_alloc (tree, gc, num_ssa_operands (stmt, SSA_OP_VIRTUAL_DEFS));
434 FOR_EACH_SSA_TREE_OPERAND (vdef, stmt, iter, SSA_OP_VIRTUAL_DEFS)
435 VEC_quick_push (tree, *result, vdef);
438 /* Copy the names of vdef results in STMT into a vector, and return
441 static VEC (tree, gc) *
442 copy_vdefs_from_stmt (tree stmt)
444 VEC (tree, gc) *vdefs = NULL;
446 vdefs_to_vec (stmt, &vdefs);
451 /* Place for shared_v{uses/defs}_from_stmt to shove vuses/vdefs. */
452 static VEC (tree, gc) *shared_lookup_vops;
454 /* Copy the virtual uses from STMT into SHARED_LOOKUP_VOPS.
455 This function will overwrite the current SHARED_LOOKUP_VOPS
459 shared_vuses_from_stmt (tree stmt)
461 VEC_truncate (tree, shared_lookup_vops, 0);
462 vuses_to_vec (stmt, &shared_lookup_vops);
464 return shared_lookup_vops;
467 /* Copy the operations present in load/store/call REF into RESULT, a vector of
468 vn_reference_op_s's. */
471 copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
473 /* Calls are different from all other reference operations. */
474 if (TREE_CODE (ref) == CALL_EXPR)
476 vn_reference_op_s temp;
478 call_expr_arg_iterator iter;
481 /* Copy the call_expr opcode, type, function being called, and
483 memset (&temp, 0, sizeof (temp));
484 temp.type = TREE_TYPE (ref);
485 temp.opcode = CALL_EXPR;
486 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
488 callfn = get_callee_fndecl (ref);
490 callfn = CALL_EXPR_FN (ref);
491 temp.type = TREE_TYPE (callfn);
492 temp.opcode = TREE_CODE (callfn);
494 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
496 FOR_EACH_CALL_EXPR_ARG (callarg, iter, ref)
498 memset (&temp, 0, sizeof (temp));
499 temp.type = TREE_TYPE (callarg);
500 temp.opcode = TREE_CODE (callarg);
502 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
507 /* For non-calls, store the information that makes up the address. */
511 vn_reference_op_s temp;
513 memset (&temp, 0, sizeof (temp));
514 temp.type = TREE_TYPE (ref);
515 temp.opcode = TREE_CODE (ref);
519 case ALIGN_INDIRECT_REF:
520 case MISALIGNED_INDIRECT_REF:
522 /* The only operand is the address, which gets its own
523 vn_reference_op_s structure. */
526 /* Record bits and position. */
527 temp.op0 = TREE_OPERAND (ref, 1);
528 temp.op1 = TREE_OPERAND (ref, 2);
531 /* If this is a reference to a union member, record the union
532 member size as operand. Do so only if we are doing
533 expression insertion (during FRE), as PRE currently gets
534 confused with this. */
536 && TREE_CODE (DECL_CONTEXT (TREE_OPERAND (ref, 1))) == UNION_TYPE
537 && integer_zerop (DECL_FIELD_OFFSET (TREE_OPERAND (ref, 1)))
538 && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1))))
540 temp.type = NULL_TREE;
541 temp.op0 = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)));
544 /* Record field as operand. */
545 temp.op0 = TREE_OPERAND (ref, 1);
547 case ARRAY_RANGE_REF:
549 /* Record index as operand. */
550 temp.op0 = TREE_OPERAND (ref, 1);
551 temp.op1 = TREE_OPERAND (ref, 3);
567 /* These are only interesting for their operands, their
568 existence, and their type. They will never be the last
569 ref in the chain of references (IE they require an
570 operand), so we don't have to put anything
571 for op* as it will be handled by the iteration */
574 case VIEW_CONVERT_EXPR:
581 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
583 if (REFERENCE_CLASS_P (ref) || TREE_CODE (ref) == ADDR_EXPR)
584 ref = TREE_OPERAND (ref, 0);
590 /* Create a vector of vn_reference_op_s structures from REF, a
591 REFERENCE_CLASS_P tree. The vector is not shared. */
593 static VEC(vn_reference_op_s, heap) *
594 create_reference_ops_from_ref (tree ref)
596 VEC (vn_reference_op_s, heap) *result = NULL;
598 copy_reference_ops_from_ref (ref, &result);
602 static VEC(vn_reference_op_s, heap) *shared_lookup_references;
604 /* Create a vector of vn_reference_op_s structures from REF, a
605 REFERENCE_CLASS_P tree. The vector is shared among all callers of
608 static VEC(vn_reference_op_s, heap) *
609 shared_reference_ops_from_ref (tree ref)
613 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
614 copy_reference_ops_from_ref (ref, &shared_lookup_references);
615 return shared_lookup_references;
619 /* Transform any SSA_NAME's in a vector of vn_reference_op_s
620 structures into their value numbers. This is done in-place, and
621 the vector passed in is returned. */
623 static VEC (vn_reference_op_s, heap) *
624 valueize_refs (VEC (vn_reference_op_s, heap) *orig)
626 vn_reference_op_t vro;
629 for (i = 0; VEC_iterate (vn_reference_op_s, orig, i, vro); i++)
631 if (vro->opcode == SSA_NAME
632 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
633 vro->op0 = SSA_VAL (vro->op0);
639 /* Transform any SSA_NAME's in ORIG, a vector of vuse trees, into
640 their value numbers. This is done in-place, and the vector passed
643 static VEC (tree, gc) *
644 valueize_vuses (VEC (tree, gc) *orig)
646 bool made_replacement = false;
650 for (i = 0; VEC_iterate (tree, orig, i, vuse); i++)
652 if (vuse != SSA_VAL (vuse))
654 made_replacement = true;
655 VEC_replace (tree, orig, i, SSA_VAL (vuse));
659 if (made_replacement && VEC_length (tree, orig) > 1)
665 /* Return the single reference statement defining all virtual uses
666 in VUSES or NULL_TREE, if there are multiple defining statements.
667 Take into account only definitions that alias REF if following
671 get_def_ref_stmt_vuses (tree ref, VEC (tree, gc) *vuses)
676 gcc_assert (VEC_length (tree, vuses) >= 1);
678 def_stmt = SSA_NAME_DEF_STMT (VEC_index (tree, vuses, 0));
679 if (TREE_CODE (def_stmt) == PHI_NODE)
681 /* We can only handle lookups over PHI nodes for a single
683 if (VEC_length (tree, vuses) == 1)
685 def_stmt = get_single_def_stmt_from_phi (ref, def_stmt);
692 /* Verify each VUSE reaches the same defining stmt. */
693 for (i = 1; VEC_iterate (tree, vuses, i, vuse); ++i)
695 tree tmp = SSA_NAME_DEF_STMT (vuse);
700 /* Now see if the definition aliases ref, and loop until it does. */
703 && TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
704 && !get_call_expr_in (def_stmt)
705 && !refs_may_alias_p (ref, GIMPLE_STMT_OPERAND (def_stmt, 0)))
706 def_stmt = get_single_def_stmt_with_phi (ref, def_stmt);
711 /* Lookup a SCCVN reference operation VR in the current hash table.
712 Returns the resulting value number if it exists in the hash table,
713 NULL_TREE otherwise. */
716 vn_reference_lookup_1 (vn_reference_t vr)
722 slot = htab_find_slot_with_hash (current_info->references, vr,
724 if (!slot && current_info == optimistic_info)
725 slot = htab_find_slot_with_hash (valid_info->references, vr,
728 return ((vn_reference_t)*slot)->result;
733 /* Lookup OP in the current hash table, and return the resulting
734 value number if it exists in the hash table. Return NULL_TREE if
735 it does not exist in the hash table. */
738 vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk)
740 struct vn_reference_s vr1;
741 tree result, def_stmt;
743 vr1.vuses = valueize_vuses (vuses);
744 vr1.operands = valueize_refs (shared_reference_ops_from_ref (op));
745 vr1.hashcode = vn_reference_compute_hash (&vr1);
746 result = vn_reference_lookup_1 (&vr1);
748 /* If there is a single defining statement for all virtual uses, we can
749 use that, following virtual use-def chains. */
753 && VEC_length (tree, vr1.vuses) >= 1
754 && !get_call_expr_in (op)
755 && (def_stmt = get_def_ref_stmt_vuses (op, vr1.vuses))
756 && TREE_CODE (def_stmt) == GIMPLE_MODIFY_STMT
757 /* If there is a call involved, op must be assumed to
759 && !get_call_expr_in (def_stmt))
761 /* We are now at an aliasing definition for the vuses we want to
762 look up. Re-do the lookup with the vdefs for this stmt. */
763 vdefs_to_vec (def_stmt, &vuses);
764 vr1.vuses = valueize_vuses (vuses);
765 vr1.hashcode = vn_reference_compute_hash (&vr1);
766 result = vn_reference_lookup_1 (&vr1);
772 /* Insert OP into the current hash table with a value number of
776 vn_reference_insert (tree op, tree result, VEC (tree, gc) *vuses)
781 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
783 vr1->vuses = valueize_vuses (vuses);
784 vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
785 vr1->hashcode = vn_reference_compute_hash (vr1);
786 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
788 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
791 /* Because we lookup stores using vuses, and value number failures
792 using the vdefs (see visit_reference_op_store for how and why),
793 it's possible that on failure we may try to insert an already
794 inserted store. This is not wrong, there is no ssa name for a
795 store that we could use as a differentiator anyway. Thus, unlike
796 the other lookup functions, you cannot gcc_assert (!*slot)
799 /* But free the old slot in case of a collision. */
801 free_reference (*slot);
806 /* Compute and return the hash value for nary operation VBO1. */
808 static inline hashval_t
809 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
814 for (i = 0; i < vno1->length; ++i)
815 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
816 vno1->op[i] = SSA_VAL (vno1->op[i]);
818 if (vno1->length == 2
819 && commutative_tree_code (vno1->opcode)
820 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
822 tree temp = vno1->op[0];
823 vno1->op[0] = vno1->op[1];
827 for (i = 0; i < vno1->length; ++i)
828 hash += iterative_hash_expr (vno1->op[i], vno1->opcode);
833 /* Return the computed hashcode for nary operation P1. */
836 vn_nary_op_hash (const void *p1)
838 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
839 return vno1->hashcode;
842 /* Compare nary operations P1 and P2 and return true if they are
846 vn_nary_op_eq (const void *p1, const void *p2)
848 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
849 const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
852 if (vno1->opcode != vno2->opcode
853 || vno1->type != vno2->type)
856 for (i = 0; i < vno1->length; ++i)
857 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
863 /* Lookup OP in the current hash table, and return the resulting
864 value number if it exists in the hash table. Return NULL_TREE if
865 it does not exist in the hash table. */
868 vn_nary_op_lookup (tree op)
871 struct vn_nary_op_s vno1;
874 vno1.opcode = TREE_CODE (op);
875 vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
876 vno1.type = TREE_TYPE (op);
877 for (i = 0; i < vno1.length; ++i)
878 vno1.op[i] = TREE_OPERAND (op, i);
879 vno1.hashcode = vn_nary_op_compute_hash (&vno1);
880 slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
882 if (!slot && current_info == optimistic_info)
883 slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
887 return ((vn_nary_op_t)*slot)->result;
890 /* Insert OP into the current hash table with a value number of
894 vn_nary_op_insert (tree op, tree result)
896 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
901 vno1 = obstack_alloc (¤t_info->nary_obstack,
902 (sizeof (struct vn_nary_op_s)
903 - sizeof (tree) * (4 - length)));
904 vno1->opcode = TREE_CODE (op);
905 vno1->length = length;
906 vno1->type = TREE_TYPE (op);
907 for (i = 0; i < vno1->length; ++i)
908 vno1->op[i] = TREE_OPERAND (op, i);
909 vno1->result = result;
910 vno1->hashcode = vn_nary_op_compute_hash (vno1);
911 slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
918 /* Compute a hashcode for PHI operation VP1 and return it. */
920 static inline hashval_t
921 vn_phi_compute_hash (vn_phi_t vp1)
923 hashval_t result = 0;
927 result = vp1->block->index;
929 for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
931 if (phi1op == VN_TOP)
933 result += iterative_hash_expr (phi1op, result);
939 /* Return the computed hashcode for phi operation P1. */
942 vn_phi_hash (const void *p1)
944 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
945 return vp1->hashcode;
948 /* Compare two phi entries for equality, ignoring VN_TOP arguments. */
951 vn_phi_eq (const void *p1, const void *p2)
953 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
954 const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
956 if (vp1->block == vp2->block)
961 /* Any phi in the same block will have it's arguments in the
962 same edge order, because of how we store phi nodes. */
963 for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
965 tree phi2op = VEC_index (tree, vp2->phiargs, i);
966 if (phi1op == VN_TOP || phi2op == VN_TOP)
968 if (!expressions_equal_p (phi1op, phi2op))
976 static VEC(tree, heap) *shared_lookup_phiargs;
978 /* Lookup PHI in the current hash table, and return the resulting
979 value number if it exists in the hash table. Return NULL_TREE if
980 it does not exist in the hash table. */
983 vn_phi_lookup (tree phi)
989 VEC_truncate (tree, shared_lookup_phiargs, 0);
991 /* Canonicalize the SSA_NAME's to their value number. */
992 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
994 tree def = PHI_ARG_DEF (phi, i);
995 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
996 VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
998 vp1.phiargs = shared_lookup_phiargs;
999 vp1.block = bb_for_stmt (phi);
1000 vp1.hashcode = vn_phi_compute_hash (&vp1);
1001 slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
1003 if (!slot && current_info == optimistic_info)
1004 slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
1008 return ((vn_phi_t)*slot)->result;
1011 /* Insert PHI into the current hash table with a value number of
1015 vn_phi_insert (tree phi, tree result)
1018 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
1020 VEC (tree, heap) *args = NULL;
1022 /* Canonicalize the SSA_NAME's to their value number. */
1023 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1025 tree def = PHI_ARG_DEF (phi, i);
1026 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1027 VEC_safe_push (tree, heap, args, def);
1029 vp1->phiargs = args;
1030 vp1->block = bb_for_stmt (phi);
1031 vp1->result = result;
1032 vp1->hashcode = vn_phi_compute_hash (vp1);
1034 slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
1037 /* Because we iterate over phi operations more than once, it's
1038 possible the slot might already exist here, hence no assert.*/
1043 /* Print set of components in strongly connected component SCC to OUT. */
1046 print_scc (FILE *out, VEC (tree, heap) *scc)
1051 fprintf (out, "SCC consists of: ");
1052 for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1054 print_generic_expr (out, var, 0);
1057 fprintf (out, "\n");
1060 /* Set the value number of FROM to TO, return true if it has changed
1064 set_ssa_val_to (tree from, tree to)
1069 && TREE_CODE (to) == SSA_NAME
1070 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
1073 /* The only thing we allow as value numbers are VN_TOP, ssa_names
1074 and invariants. So assert that here. */
1075 gcc_assert (to != NULL_TREE
1077 || TREE_CODE (to) == SSA_NAME
1078 || is_gimple_min_invariant (to)));
1080 if (dump_file && (dump_flags & TDF_DETAILS))
1082 fprintf (dump_file, "Setting value number of ");
1083 print_generic_expr (dump_file, from, 0);
1084 fprintf (dump_file, " to ");
1085 print_generic_expr (dump_file, to, 0);
1086 fprintf (dump_file, "\n");
1089 currval = SSA_VAL (from);
1091 if (currval != to && !operand_equal_p (currval, to, OEP_PURE_SAME))
1093 SSA_VAL (from) = to;
1099 /* Set all definitions in STMT to value number to themselves.
1100 Return true if a value number changed. */
1103 defs_to_varying (tree stmt)
1105 bool changed = false;
1109 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
1111 tree def = DEF_FROM_PTR (defp);
1113 VN_INFO (def)->use_processed = true;
1114 changed |= set_ssa_val_to (def, def);
1120 try_to_simplify (tree stmt, tree rhs);
1122 /* Visit a copy between LHS and RHS, return true if the value number
1126 visit_copy (tree lhs, tree rhs)
1129 /* Follow chains of copies to their destination. */
1130 while (SSA_VAL (rhs) != rhs && TREE_CODE (SSA_VAL (rhs)) == SSA_NAME)
1131 rhs = SSA_VAL (rhs);
1133 /* The copy may have a more interesting constant filled expression
1134 (we don't, since we know our RHS is just an SSA name). */
1135 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
1136 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
1138 return set_ssa_val_to (lhs, rhs);
1141 /* Visit a unary operator RHS, value number it, and return true if the
1142 value number of LHS has changed as a result. */
1145 visit_unary_op (tree lhs, tree op)
1147 bool changed = false;
1148 tree result = vn_nary_op_lookup (op);
1152 changed = set_ssa_val_to (lhs, result);
1156 changed = set_ssa_val_to (lhs, lhs);
1157 vn_nary_op_insert (op, lhs);
1163 /* Visit a binary operator RHS, value number it, and return true if the
1164 value number of LHS has changed as a result. */
1167 visit_binary_op (tree lhs, tree op)
1169 bool changed = false;
1170 tree result = vn_nary_op_lookup (op);
1174 changed = set_ssa_val_to (lhs, result);
1178 changed = set_ssa_val_to (lhs, lhs);
1179 vn_nary_op_insert (op, lhs);
1185 /* Visit a load from a reference operator RHS, part of STMT, value number it,
1186 and return true if the value number of the LHS has changed as a result. */
1189 visit_reference_op_load (tree lhs, tree op, tree stmt)
1191 bool changed = false;
1192 tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true);
1194 /* We handle type-punning through unions by value-numbering based
1195 on offset and size of the access. Be prepared to handle a
1196 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
1198 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
1200 /* We will be setting the value number of lhs to the value number
1201 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
1202 So first simplify and lookup this expression to see if it
1203 is already available. */
1204 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
1206 && !is_gimple_min_invariant (val)
1207 && TREE_CODE (val) != SSA_NAME)
1209 tree tem = try_to_simplify (stmt, val);
1214 if (!is_gimple_min_invariant (val)
1215 && TREE_CODE (val) != SSA_NAME)
1216 result = vn_nary_op_lookup (val);
1217 /* If the expression is not yet available, value-number lhs to
1218 a new SSA_NAME we create. */
1219 if (!result && may_insert)
1221 result = make_ssa_name (SSA_NAME_VAR (lhs), NULL_TREE);
1222 /* Initialize value-number information properly. */
1223 VN_INFO_GET (result)->valnum = result;
1224 VN_INFO (result)->expr = val;
1225 VN_INFO (result)->needs_insertion = true;
1226 /* As all "inserted" statements are singleton SCCs, insert
1227 to the valid table. This is strictly needed to
1228 avoid re-generating new value SSA_NAMEs for the same
1229 expression during SCC iteration over and over (the
1230 optimistic table gets cleared after each iteration).
1231 We do not need to insert into the optimistic table, as
1232 lookups there will fall back to the valid table. */
1233 if (current_info == optimistic_info)
1235 current_info = valid_info;
1236 vn_nary_op_insert (val, result);
1237 current_info = optimistic_info;
1240 vn_nary_op_insert (val, result);
1241 if (dump_file && (dump_flags & TDF_DETAILS))
1243 fprintf (dump_file, "Inserting name ");
1244 print_generic_expr (dump_file, result, 0);
1245 fprintf (dump_file, " for expression ");
1246 print_generic_expr (dump_file, val, 0);
1247 fprintf (dump_file, "\n");
1254 changed = set_ssa_val_to (lhs, result);
1255 if (TREE_CODE (result) == SSA_NAME
1256 && VN_INFO (result)->has_constants)
1258 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
1259 VN_INFO (lhs)->has_constants = true;
1264 changed = set_ssa_val_to (lhs, lhs);
1265 vn_reference_insert (op, lhs, copy_vuses_from_stmt (stmt));
1272 /* Visit a store to a reference operator LHS, part of STMT, value number it,
1273 and return true if the value number of the LHS has changed as a result. */
1276 visit_reference_op_store (tree lhs, tree op, tree stmt)
1278 bool changed = false;
1280 bool resultsame = false;
1282 /* First we want to lookup using the *vuses* from the store and see
1283 if there the last store to this location with the same address
1286 The vuses represent the memory state before the store. If the
1287 memory state, address, and value of the store is the same as the
1288 last store to this location, then this store will produce the
1289 same memory state as that store.
1291 In this case the vdef versions for this store are value numbered to those
1292 vuse versions, since they represent the same memory state after
1295 Otherwise, the vdefs for the store are used when inserting into
1296 the table, since the store generates a new memory state. */
1298 result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false);
1302 if (TREE_CODE (result) == SSA_NAME)
1303 result = SSA_VAL (result);
1304 if (TREE_CODE (op) == SSA_NAME)
1306 resultsame = expressions_equal_p (result, op);
1309 if (!result || !resultsame)
1311 VEC(tree, gc) *vdefs = copy_vdefs_from_stmt (stmt);
1315 if (dump_file && (dump_flags & TDF_DETAILS))
1317 fprintf (dump_file, "No store match\n");
1318 fprintf (dump_file, "Value numbering store ");
1319 print_generic_expr (dump_file, lhs, 0);
1320 fprintf (dump_file, " to ");
1321 print_generic_expr (dump_file, op, 0);
1322 fprintf (dump_file, "\n");
1324 /* Have to set value numbers before insert, since insert is
1325 going to valueize the references in-place. */
1326 for (i = 0; VEC_iterate (tree, vdefs, i, vdef); i++)
1328 VN_INFO (vdef)->use_processed = true;
1329 changed |= set_ssa_val_to (vdef, vdef);
1332 /* Do not insert structure copies into the tables. */
1333 if (is_gimple_min_invariant (op)
1334 || is_gimple_reg (op))
1335 vn_reference_insert (lhs, op, vdefs);
1339 /* We had a match, so value number the vdefs to have the value
1340 number of the vuses they came from. */
1341 ssa_op_iter op_iter;
1345 if (dump_file && (dump_flags & TDF_DETAILS))
1346 fprintf (dump_file, "Store matched earlier value,"
1347 "value numbering store vdefs to matching vuses.\n");
1349 FOR_EACH_SSA_VDEF_OPERAND (var, vv, stmt, op_iter)
1351 tree def = DEF_FROM_PTR (var);
1354 /* Uh, if the vuse is a multiuse, we can't really do much
1355 here, sadly, since we don't know which value number of
1356 which vuse to use. */
1357 if (VUSE_VECT_NUM_ELEM (*vv) != 1)
1360 use = VUSE_ELEMENT_VAR (*vv, 0);
1362 VN_INFO (def)->use_processed = true;
1363 changed |= set_ssa_val_to (def, SSA_VAL (use));
1370 /* Visit and value number PHI, return true if the value number
1374 visit_phi (tree phi)
1376 bool changed = false;
1378 tree sameval = VN_TOP;
1379 bool allsame = true;
1382 /* TODO: We could check for this in init_sccvn, and replace this
1383 with a gcc_assert. */
1384 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
1385 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1387 /* See if all non-TOP arguments have the same value. TOP is
1388 equivalent to everything, so we can ignore it. */
1389 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1391 tree def = PHI_ARG_DEF (phi, i);
1393 if (TREE_CODE (def) == SSA_NAME)
1394 def = SSA_VAL (def);
1397 if (sameval == VN_TOP)
1403 if (!expressions_equal_p (def, sameval))
1411 /* If all value numbered to the same value, the phi node has that
1415 if (is_gimple_min_invariant (sameval))
1417 VN_INFO (PHI_RESULT (phi))->has_constants = true;
1418 VN_INFO (PHI_RESULT (phi))->expr = sameval;
1422 VN_INFO (PHI_RESULT (phi))->has_constants = false;
1423 VN_INFO (PHI_RESULT (phi))->expr = sameval;
1426 if (TREE_CODE (sameval) == SSA_NAME)
1427 return visit_copy (PHI_RESULT (phi), sameval);
1429 return set_ssa_val_to (PHI_RESULT (phi), sameval);
1432 /* Otherwise, see if it is equivalent to a phi node in this block. */
1433 result = vn_phi_lookup (phi);
1436 if (TREE_CODE (result) == SSA_NAME)
1437 changed = visit_copy (PHI_RESULT (phi), result);
1439 changed = set_ssa_val_to (PHI_RESULT (phi), result);
1443 vn_phi_insert (phi, PHI_RESULT (phi));
1444 VN_INFO (PHI_RESULT (phi))->has_constants = false;
1445 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
1446 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
1452 /* Return true if EXPR contains constants. */
1455 expr_has_constants (tree expr)
1457 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1460 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
1463 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
1464 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
1465 /* Constants inside reference ops are rarely interesting, but
1466 it can take a lot of looking to find them. */
1468 case tcc_declaration:
1471 return is_gimple_min_invariant (expr);
1476 /* Replace SSA_NAMES in expr with their value numbers, and return the
1478 This is performed in place. */
1481 valueize_expr (tree expr)
1483 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1486 if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1487 && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1488 TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1491 if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1492 && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
1493 TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
1494 if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
1495 && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
1496 TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
1504 /* Simplify the binary expression RHS, and return the result if
1508 simplify_binary_expression (tree stmt, tree rhs)
1510 tree result = NULL_TREE;
1511 tree op0 = TREE_OPERAND (rhs, 0);
1512 tree op1 = TREE_OPERAND (rhs, 1);
1514 /* This will not catch every single case we could combine, but will
1515 catch those with constants. The goal here is to simultaneously
1516 combine constants between expressions, but avoid infinite
1517 expansion of expressions during simplification. */
1518 if (TREE_CODE (op0) == SSA_NAME)
1520 if (VN_INFO (op0)->has_constants)
1521 op0 = valueize_expr (VN_INFO (op0)->expr);
1522 else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
1523 op0 = SSA_VAL (op0);
1526 if (TREE_CODE (op1) == SSA_NAME)
1528 if (VN_INFO (op1)->has_constants)
1529 op1 = valueize_expr (VN_INFO (op1)->expr);
1530 else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
1531 op1 = SSA_VAL (op1);
1534 /* Avoid folding if nothing changed. */
1535 if (op0 == TREE_OPERAND (rhs, 0)
1536 && op1 == TREE_OPERAND (rhs, 1))
1539 fold_defer_overflow_warnings ();
1541 result = fold_binary (TREE_CODE (rhs), TREE_TYPE (rhs), op0, op1);
1543 fold_undefer_overflow_warnings (result && valid_gimple_expression_p (result),
1546 /* Make sure result is not a complex expression consisting
1547 of operators of operators (IE (a + b) + (a + c))
1548 Otherwise, we will end up with unbounded expressions if
1549 fold does anything at all. */
1550 if (result && valid_gimple_expression_p (result))
1556 /* Simplify the unary expression RHS, and return the result if
1560 simplify_unary_expression (tree rhs)
1562 tree result = NULL_TREE;
1563 tree op0 = TREE_OPERAND (rhs, 0);
1565 if (TREE_CODE (op0) != SSA_NAME)
1568 if (VN_INFO (op0)->has_constants)
1569 op0 = valueize_expr (VN_INFO (op0)->expr);
1570 else if (TREE_CODE (rhs) == NOP_EXPR
1571 || TREE_CODE (rhs) == CONVERT_EXPR
1572 || TREE_CODE (rhs) == REALPART_EXPR
1573 || TREE_CODE (rhs) == IMAGPART_EXPR
1574 || TREE_CODE (rhs) == VIEW_CONVERT_EXPR)
1576 /* We want to do tree-combining on conversion-like expressions.
1577 Make sure we feed only SSA_NAMEs or constants to fold though. */
1578 tree tem = valueize_expr (VN_INFO (op0)->expr);
1579 if (UNARY_CLASS_P (tem)
1580 || BINARY_CLASS_P (tem)
1581 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
1582 || TREE_CODE (tem) == SSA_NAME
1583 || is_gimple_min_invariant (tem))
1587 /* Avoid folding if nothing changed, but remember the expression. */
1588 if (op0 == TREE_OPERAND (rhs, 0))
1591 result = fold_unary (TREE_CODE (rhs), TREE_TYPE (rhs), op0);
1594 STRIP_USELESS_TYPE_CONVERSION (result);
1595 if (valid_gimple_expression_p (result))
1602 /* Try to simplify RHS using equivalences and constant folding. */
1605 try_to_simplify (tree stmt, tree rhs)
1609 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
1610 in this case, there is no point in doing extra work. */
1611 if (TREE_CODE (rhs) == SSA_NAME)
1614 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1616 case tcc_declaration:
1617 tem = get_symbol_constant_value (rhs);
1623 /* Do not do full-blown reference lookup here, but simplify
1624 reads from constant aggregates. */
1625 tem = fold_const_aggregate_ref (rhs);
1629 /* Fallthrough for some codes that can operate on registers. */
1630 if (!(TREE_CODE (rhs) == REALPART_EXPR
1631 || TREE_CODE (rhs) == IMAGPART_EXPR
1632 || TREE_CODE (rhs) == VIEW_CONVERT_EXPR))
1634 /* We could do a little more with unary ops, if they expand
1635 into binary ops, but it's debatable whether it is worth it. */
1637 return simplify_unary_expression (rhs);
1639 case tcc_comparison:
1641 return simplify_binary_expression (stmt, rhs);
1650 /* Visit and value number USE, return true if the value number
1654 visit_use (tree use)
1656 bool changed = false;
1657 tree stmt = SSA_NAME_DEF_STMT (use);
1660 VN_INFO (use)->use_processed = true;
1662 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
1663 if (dump_file && (dump_flags & TDF_DETAILS)
1664 && !IS_EMPTY_STMT (stmt))
1666 fprintf (dump_file, "Value numbering ");
1667 print_generic_expr (dump_file, use, 0);
1668 fprintf (dump_file, " stmt = ");
1669 print_generic_stmt (dump_file, stmt, 0);
1672 /* RETURN_EXPR may have an embedded MODIFY_STMT. */
1673 if (TREE_CODE (stmt) == RETURN_EXPR
1674 && TREE_CODE (TREE_OPERAND (stmt, 0)) == GIMPLE_MODIFY_STMT)
1675 stmt = TREE_OPERAND (stmt, 0);
1677 ann = stmt_ann (stmt);
1679 /* Handle uninitialized uses. */
1680 if (IS_EMPTY_STMT (stmt))
1682 changed = set_ssa_val_to (use, use);
1686 if (TREE_CODE (stmt) == PHI_NODE)
1688 changed = visit_phi (stmt);
1690 else if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT
1691 || (ann && ann->has_volatile_ops)
1692 || tree_could_throw_p (stmt))
1694 changed = defs_to_varying (stmt);
1698 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
1699 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
1702 STRIP_USELESS_TYPE_CONVERSION (rhs);
1704 /* Shortcut for copies. Simplifying copies is pointless,
1705 since we copy the expression and value they represent. */
1706 if (TREE_CODE (rhs) == SSA_NAME && TREE_CODE (lhs) == SSA_NAME)
1708 changed = visit_copy (lhs, rhs);
1711 simplified = try_to_simplify (stmt, rhs);
1712 if (simplified && simplified != rhs)
1714 if (dump_file && (dump_flags & TDF_DETAILS))
1716 fprintf (dump_file, "RHS ");
1717 print_generic_expr (dump_file, rhs, 0);
1718 fprintf (dump_file, " simplified to ");
1719 print_generic_expr (dump_file, simplified, 0);
1720 if (TREE_CODE (lhs) == SSA_NAME)
1721 fprintf (dump_file, " has constants %d\n",
1722 expr_has_constants (simplified));
1724 fprintf (dump_file, "\n");
1727 /* Setting value numbers to constants will occasionally
1728 screw up phi congruence because constants are not
1729 uniquely associated with a single ssa name that can be
1731 if (simplified && is_gimple_min_invariant (simplified)
1732 && TREE_CODE (lhs) == SSA_NAME
1733 && simplified != rhs)
1735 VN_INFO (lhs)->expr = simplified;
1736 VN_INFO (lhs)->has_constants = true;
1737 changed = set_ssa_val_to (lhs, simplified);
1740 else if (simplified && TREE_CODE (simplified) == SSA_NAME
1741 && TREE_CODE (lhs) == SSA_NAME)
1743 changed = visit_copy (lhs, simplified);
1746 else if (simplified)
1748 if (TREE_CODE (lhs) == SSA_NAME)
1750 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
1751 /* We have to unshare the expression or else
1752 valuizing may change the IL stream. */
1753 VN_INFO (lhs)->expr = unshare_expr (simplified);
1757 else if (expr_has_constants (rhs) && TREE_CODE (lhs) == SSA_NAME)
1759 VN_INFO (lhs)->has_constants = true;
1760 VN_INFO (lhs)->expr = unshare_expr (rhs);
1762 else if (TREE_CODE (lhs) == SSA_NAME)
1764 /* We reset expr and constantness here because we may
1765 have been value numbering optimistically, and
1766 iterating. They may become non-constant in this case,
1767 even if they were optimistically constant. */
1769 VN_INFO (lhs)->has_constants = false;
1770 VN_INFO (lhs)->expr = lhs;
1773 if (TREE_CODE (lhs) == SSA_NAME
1774 /* We can substitute SSA_NAMEs that are live over
1775 abnormal edges with their constant value. */
1776 && !is_gimple_min_invariant (rhs)
1777 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1778 changed = defs_to_varying (stmt);
1779 else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
1781 changed = visit_reference_op_store (lhs, rhs, stmt);
1783 else if (TREE_CODE (lhs) == SSA_NAME)
1785 if (is_gimple_min_invariant (rhs))
1787 VN_INFO (lhs)->has_constants = true;
1788 VN_INFO (lhs)->expr = rhs;
1789 changed = set_ssa_val_to (lhs, rhs);
1793 switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
1796 changed = visit_unary_op (lhs, rhs);
1799 changed = visit_binary_op (lhs, rhs);
1801 /* If tcc_vl_expr ever encompasses more than
1802 CALL_EXPR, this will need to be changed. */
1804 if (call_expr_flags (rhs) & (ECF_PURE | ECF_CONST))
1805 changed = visit_reference_op_load (lhs, rhs, stmt);
1807 changed = defs_to_varying (stmt);
1809 case tcc_declaration:
1811 changed = visit_reference_op_load (lhs, rhs, stmt);
1813 case tcc_expression:
1814 if (TREE_CODE (rhs) == ADDR_EXPR)
1816 changed = visit_unary_op (lhs, rhs);
1821 changed = defs_to_varying (stmt);
1827 changed = defs_to_varying (stmt);
1834 /* Compare two operands by reverse postorder index */
1837 compare_ops (const void *pa, const void *pb)
1839 const tree opa = *((const tree *)pa);
1840 const tree opb = *((const tree *)pb);
1841 tree opstmta = SSA_NAME_DEF_STMT (opa);
1842 tree opstmtb = SSA_NAME_DEF_STMT (opb);
1846 if (IS_EMPTY_STMT (opstmta) && IS_EMPTY_STMT (opstmtb))
1848 else if (IS_EMPTY_STMT (opstmta))
1850 else if (IS_EMPTY_STMT (opstmtb))
1853 bba = bb_for_stmt (opstmta);
1854 bbb = bb_for_stmt (opstmtb);
1865 if (TREE_CODE (opstmta) == PHI_NODE && TREE_CODE (opstmtb) == PHI_NODE)
1867 else if (TREE_CODE (opstmta) == PHI_NODE)
1869 else if (TREE_CODE (opstmtb) == PHI_NODE)
1871 return stmt_ann (opstmta)->uid - stmt_ann (opstmtb)->uid;
1873 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
1876 /* Sort an array containing members of a strongly connected component
1877 SCC so that the members are ordered by RPO number.
1878 This means that when the sort is complete, iterating through the
1879 array will give you the members in RPO order. */
1882 sort_scc (VEC (tree, heap) *scc)
1884 qsort (VEC_address (tree, scc),
1885 VEC_length (tree, scc),
1890 /* Process a strongly connected component in the SSA graph. */
1893 process_scc (VEC (tree, heap) *scc)
1895 /* If the SCC has a single member, just visit it. */
1897 if (VEC_length (tree, scc) == 1)
1899 tree use = VEC_index (tree, scc, 0);
1900 if (!VN_INFO (use)->use_processed)
1907 unsigned int iterations = 0;
1908 bool changed = true;
1910 /* Iterate over the SCC with the optimistic table until it stops
1912 current_info = optimistic_info;
1917 htab_empty (optimistic_info->nary);
1918 htab_empty (optimistic_info->phis);
1919 htab_empty (optimistic_info->references);
1920 obstack_free (&optimistic_info->nary_obstack, NULL);
1921 gcc_obstack_init (&optimistic_info->nary_obstack);
1922 empty_alloc_pool (optimistic_info->phis_pool);
1923 empty_alloc_pool (optimistic_info->references_pool);
1924 for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1925 changed |= visit_use (var);
1928 if (dump_file && (dump_flags & TDF_STATS))
1929 fprintf (dump_file, "Processing SCC required %d iterations\n",
1932 /* Finally, visit the SCC once using the valid table. */
1933 current_info = valid_info;
1934 for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1939 /* Depth first search on NAME to discover and process SCC's in the SSA
1941 Execution of this algorithm relies on the fact that the SCC's are
1942 popped off the stack in topological order.
1943 Returns true if successful, false if we stopped processing SCC's due
1944 to ressource constraints. */
1954 VN_INFO (name)->dfsnum = next_dfs_num++;
1955 VN_INFO (name)->visited = true;
1956 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
1958 VEC_safe_push (tree, heap, sccstack, name);
1959 VN_INFO (name)->on_sccstack = true;
1960 defstmt = SSA_NAME_DEF_STMT (name);
1962 /* Recursively DFS on our operands, looking for SCC's. */
1963 if (!IS_EMPTY_STMT (defstmt))
1965 FOR_EACH_PHI_OR_STMT_USE (usep, SSA_NAME_DEF_STMT (name), iter,
1968 tree use = USE_FROM_PTR (usep);
1970 /* Since we handle phi nodes, we will sometimes get
1971 invariants in the use expression. */
1972 if (TREE_CODE (use) != SSA_NAME)
1975 if (! (VN_INFO (use)->visited))
1979 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
1980 VN_INFO (use)->low);
1982 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
1983 && VN_INFO (use)->on_sccstack)
1985 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
1986 VN_INFO (name)->low);
1991 /* See if we found an SCC. */
1992 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
1994 VEC (tree, heap) *scc = NULL;
1997 /* Found an SCC, pop the components off the SCC stack and
2001 x = VEC_pop (tree, sccstack);
2003 VN_INFO (x)->on_sccstack = false;
2004 VEC_safe_push (tree, heap, scc, x);
2005 } while (x != name);
2007 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
2008 if (VEC_length (tree, scc)
2009 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
2012 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
2013 "SCC size %u exceeding %u\n", VEC_length (tree, scc),
2014 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
2018 if (VEC_length (tree, scc) > 1)
2021 if (dump_file && (dump_flags & TDF_DETAILS))
2022 print_scc (dump_file, scc);
2026 VEC_free (tree, heap, scc);
2032 /* Allocate a value number table. */
2035 allocate_vn_table (vn_tables_t table)
2037 table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
2038 table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
2039 table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
2042 gcc_obstack_init (&table->nary_obstack);
2043 table->phis_pool = create_alloc_pool ("VN phis",
2044 sizeof (struct vn_phi_s),
2046 table->references_pool = create_alloc_pool ("VN references",
2047 sizeof (struct vn_reference_s),
2051 /* Free a value number table. */
2054 free_vn_table (vn_tables_t table)
2056 htab_delete (table->phis);
2057 htab_delete (table->nary);
2058 htab_delete (table->references);
2059 obstack_free (&table->nary_obstack, NULL);
2060 free_alloc_pool (table->phis_pool);
2061 free_alloc_pool (table->references_pool);
2069 int *rpo_numbers_temp;
2073 calculate_dominance_info (CDI_DOMINATORS);
2077 vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
2078 /* VEC_alloc doesn't actually grow it to the right size, it just
2079 preallocates the space to do so. */
2080 VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
2081 gcc_obstack_init (&vn_ssa_aux_obstack);
2083 shared_lookup_phiargs = NULL;
2084 shared_lookup_vops = NULL;
2085 shared_lookup_references = NULL;
2086 rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2087 rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
2088 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
2090 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
2091 the i'th block in RPO order is bb. We want to map bb's to RPO
2092 numbers, so we need to rearrange this array. */
2093 for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
2094 rpo_numbers[rpo_numbers_temp[j]] = j;
2096 XDELETE (rpo_numbers_temp);
2098 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
2100 /* Create the VN_INFO structures, and initialize value numbers to
2102 for (i = 0; i < num_ssa_names; i++)
2104 tree name = ssa_name (i);
2107 VN_INFO_GET (name)->valnum = VN_TOP;
2108 VN_INFO (name)->expr = name;
2114 block_stmt_iterator bsi;
2115 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2117 tree stmt = bsi_stmt (bsi);
2118 stmt_ann (stmt)->uid = id++;
2122 /* Create the valid and optimistic value numbering tables. */
2123 valid_info = XCNEW (struct vn_tables_s);
2124 allocate_vn_table (valid_info);
2125 optimistic_info = XCNEW (struct vn_tables_s);
2126 allocate_vn_table (optimistic_info);
2131 switch_to_PRE_table (void)
2133 pre_info = XCNEW (struct vn_tables_s);
2134 allocate_vn_table (pre_info);
2135 current_info = pre_info;
2143 VEC_free (tree, heap, shared_lookup_phiargs);
2144 VEC_free (tree, gc, shared_lookup_vops);
2145 VEC_free (vn_reference_op_s, heap, shared_lookup_references);
2146 XDELETEVEC (rpo_numbers);
2148 for (i = 0; i < num_ssa_names; i++)
2150 tree name = ssa_name (i);
2152 && SSA_NAME_VALUE (name)
2153 && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2154 SSA_NAME_VALUE (name) = NULL;
2156 && VN_INFO (name)->needs_insertion)
2157 release_ssa_name (name);
2159 obstack_free (&vn_ssa_aux_obstack, NULL);
2160 VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
2162 VEC_free (tree, heap, sccstack);
2163 free_vn_table (valid_info);
2164 XDELETE (valid_info);
2165 free_vn_table (optimistic_info);
2166 XDELETE (optimistic_info);
2169 free_vn_table (pre_info);
2174 /* Do SCCVN. Returns true if it finished, false if we bailed out
2175 due to ressource constraints. */
2178 run_scc_vn (bool may_insert_arg)
2183 may_insert = may_insert_arg;
2186 current_info = valid_info;
2188 for (param = DECL_ARGUMENTS (current_function_decl);
2190 param = TREE_CHAIN (param))
2192 if (gimple_default_def (cfun, param) != NULL)
2194 tree def = gimple_default_def (cfun, param);
2195 SSA_VAL (def) = def;
2199 for (i = 1; i < num_ssa_names; ++i)
2201 tree name = ssa_name (i);
2203 && VN_INFO (name)->visited == false
2204 && !has_zero_uses (name))
2213 if (dump_file && (dump_flags & TDF_DETAILS))
2215 fprintf (dump_file, "Value numbers:\n");
2216 for (i = 0; i < num_ssa_names; i++)
2218 tree name = ssa_name (i);
2219 if (name && VN_INFO (name)->visited
2220 && (SSA_VAL (name) != name
2221 || is_gimple_min_invariant (VN_INFO (name)->expr)))
2223 print_generic_expr (dump_file, name, 0);
2224 fprintf (dump_file, " = ");
2225 if (is_gimple_min_invariant (VN_INFO (name)->expr))
2226 print_generic_expr (dump_file, VN_INFO (name)->expr, 0);
2228 print_generic_expr (dump_file, SSA_VAL (name), 0);
2229 fprintf (dump_file, "\n");