1 /* Tree SCC value numbering
2    Copyright (C) 2007-2016 Free Software Foundation, Inc.
3    Contributed by Daniel Berlin <dberlin@dberlin.org>
4 
5    This file is part of GCC.
6 
7    GCC is free software; you can redistribute it and/or modify
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
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.
16 
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/>.  */
20 
21 #ifndef TREE_SSA_SCCVN_H
22 #define TREE_SSA_SCCVN_H
23 
24 /* In tree-ssa-sccvn.c  */
25 bool expressions_equal_p (tree, tree);
26 
27 
28 /* TOP of the VN lattice.  */
29 extern tree VN_TOP;
30 
31 /* N-ary operations in the hashtable consist of length operands, an
32    opcode, and a type.  Result is the value number of the operation,
33    and hashcode is stored to avoid having to calculate it
34    repeatedly.  */
35 
36 typedef struct vn_nary_op_s
37 {
38   /* Unique identify that all expressions with the same value have. */
39   unsigned int value_id;
40   ENUM_BITFIELD(tree_code) opcode : 16;
41   unsigned length : 16;
42   hashval_t hashcode;
43   tree result;
44   tree type;
45   tree op[1];
46 } *vn_nary_op_t;
47 typedef const struct vn_nary_op_s *const_vn_nary_op_t;
48 
49 /* Return the size of a vn_nary_op_t with LENGTH operands.  */
50 
51 static inline size_t
sizeof_vn_nary_op(unsigned int length)52 sizeof_vn_nary_op (unsigned int length)
53 {
54   return sizeof (struct vn_nary_op_s) + sizeof (tree) * length - sizeof (tree);
55 }
56 
57 /* Phi nodes in the hashtable consist of their non-VN_TOP phi
58    arguments, and the basic block the phi is in. Result is the value
59    number of the operation, and hashcode is stored to avoid having to
60    calculate it repeatedly.  Phi nodes not in the same block are never
61    considered equivalent.  */
62 
63 typedef struct vn_phi_s
64 {
65   /* Unique identifier that all expressions with the same value have. */
66   unsigned int value_id;
67   hashval_t hashcode;
68   vec<tree> phiargs;
69   basic_block block;
70   /* Controlling condition lhs/rhs.  */
71   tree cclhs;
72   tree ccrhs;
73   tree type;
74   tree result;
75 } *vn_phi_t;
76 typedef const struct vn_phi_s *const_vn_phi_t;
77 
78 /* Reference operands only exist in reference operations structures.
79    They consist of an opcode, type, and some number of operands.  For
80    a given opcode, some, all, or none of the operands may be used.
81    The operands are there to store the information that makes up the
82    portion of the addressing calculation that opcode performs.  */
83 
84 typedef struct vn_reference_op_struct
85 {
86   ENUM_BITFIELD(tree_code) opcode : 16;
87   /* 1 for instrumented calls.  */
88   unsigned with_bounds : 1;
89   /* Dependence info, used for [TARGET_]MEM_REF only.  */
90   unsigned short clique;
91   unsigned short base;
92   /* Constant offset this op adds or -1 if it is variable.  */
93   HOST_WIDE_INT off;
94   tree type;
95   tree op0;
96   tree op1;
97   tree op2;
98   bool reverse;
99 } vn_reference_op_s;
100 typedef vn_reference_op_s *vn_reference_op_t;
101 typedef const vn_reference_op_s *const_vn_reference_op_t;
102 
103 
104 /* A reference operation in the hashtable is representation as
105    the vuse, representing the memory state at the time of
106    the operation, and a collection of operands that make up the
107    addressing calculation.  If two vn_reference_t's have the same set
108    of operands, they access the same memory location. We also store
109    the resulting value number, and the hashcode.  */
110 
111 typedef struct vn_reference_s
112 {
113   /* Unique identifier that all expressions with the same value have. */
114   unsigned int value_id;
115   hashval_t hashcode;
116   tree vuse;
117   alias_set_type set;
118   tree type;
119   vec<vn_reference_op_s> operands;
120   tree result;
121   tree result_vdef;
122 } *vn_reference_t;
123 typedef const struct vn_reference_s *const_vn_reference_t;
124 
125 typedef struct vn_constant_s
126 {
127   unsigned int value_id;
128   hashval_t hashcode;
129   tree constant;
130 } *vn_constant_t;
131 
132 enum vn_kind { VN_NONE, VN_CONSTANT, VN_NARY, VN_REFERENCE, VN_PHI };
133 enum vn_kind vn_get_stmt_kind (gimple *);
134 
135 /* Hash the type TYPE using bits that distinguishes it in the
136    types_compatible_p sense.  */
137 
138 static inline hashval_t
vn_hash_type(tree type)139 vn_hash_type (tree type)
140 {
141   return (INTEGRAL_TYPE_P (type)
142 	  + (INTEGRAL_TYPE_P (type)
143 	     ? TYPE_PRECISION (type) + TYPE_UNSIGNED (type) : 0));
144 }
145 
146 /* Hash the constant CONSTANT with distinguishing type incompatible
147    constants in the types_compatible_p sense.  */
148 
149 static inline hashval_t
vn_hash_constant_with_type(tree constant)150 vn_hash_constant_with_type (tree constant)
151 {
152   inchash::hash hstate;
153   inchash::add_expr (constant, hstate);
154   hstate.merge_hash (vn_hash_type (TREE_TYPE (constant)));
155   return hstate.end ();
156 }
157 
158 /* Compare the constants C1 and C2 with distinguishing type incompatible
159    constants in the types_compatible_p sense.  */
160 
161 static inline bool
vn_constant_eq_with_type(tree c1,tree c2)162 vn_constant_eq_with_type (tree c1, tree c2)
163 {
164   return (expressions_equal_p (c1, c2)
165 	  && types_compatible_p (TREE_TYPE (c1), TREE_TYPE (c2)));
166 }
167 
168 typedef struct vn_ssa_aux
169 {
170   /* Value number. This may be an SSA name or a constant.  */
171   tree valnum;
172   /* Statements to insert if needs_insertion is true.  */
173   gimple_seq expr;
174 
175   /* Saved SSA name info.  */
176   tree_ssa_name::ssa_name_info_type info;
177 
178   /* Unique identifier that all expressions with the same value have. */
179   unsigned int value_id;
180 
181   /* SCC information.  */
182   unsigned int dfsnum;
183   unsigned int low;
184   unsigned visited : 1;
185   unsigned on_sccstack : 1;
186 
187   /* Whether the SSA_NAME has been value numbered already.  This is
188      only saying whether visit_use has been called on it at least
189      once.  It cannot be used to avoid visitation for SSA_NAME's
190      involved in non-singleton SCC's.  */
191   unsigned use_processed : 1;
192 
193   /* Whether the SSA_NAME has no defining statement and thus an
194      insertion of such with EXPR as definition is required before
195      a use can be created of it.  */
196   unsigned needs_insertion : 1;
197 
198   /* Whether range-info is anti-range.  */
199   unsigned range_info_anti_range_p : 1;
200 } *vn_ssa_aux_t;
201 
202 enum vn_lookup_kind { VN_NOWALK, VN_WALK, VN_WALKREWRITE };
203 
204 /* Return the value numbering info for an SSA_NAME.  */
205 extern vn_ssa_aux_t VN_INFO (tree);
206 extern vn_ssa_aux_t VN_INFO_GET (tree);
207 tree vn_get_expr_for (tree);
208 bool run_scc_vn (vn_lookup_kind);
209 void free_scc_vn (void);
210 void scc_vn_restore_ssa_info (void);
211 tree vn_nary_op_lookup (tree, vn_nary_op_t *);
212 tree vn_nary_op_lookup_stmt (gimple *, vn_nary_op_t *);
213 tree vn_nary_op_lookup_pieces (unsigned int, enum tree_code,
214 			       tree, tree *, vn_nary_op_t *);
215 vn_nary_op_t vn_nary_op_insert (tree, tree);
216 vn_nary_op_t vn_nary_op_insert_pieces (unsigned int, enum tree_code,
217 				       tree, tree *, tree, unsigned int);
218 bool ao_ref_init_from_vn_reference (ao_ref *, alias_set_type, tree,
219 				    vec<vn_reference_op_s> );
220 tree vn_reference_lookup_pieces (tree, alias_set_type, tree,
221 				 vec<vn_reference_op_s> ,
222 				 vn_reference_t *, vn_lookup_kind);
223 tree vn_reference_lookup (tree, tree, vn_lookup_kind, vn_reference_t *, bool);
224 void vn_reference_lookup_call (gcall *, vn_reference_t *, vn_reference_t);
225 vn_reference_t vn_reference_insert_pieces (tree, alias_set_type, tree,
226 					   vec<vn_reference_op_s> ,
227 					   tree, unsigned int);
228 
229 bool vn_nary_op_eq (const_vn_nary_op_t const vno1,
230 		    const_vn_nary_op_t const vno2);
231 bool vn_nary_may_trap (vn_nary_op_t);
232 bool vn_reference_eq (const_vn_reference_t const, const_vn_reference_t const);
233 unsigned int get_max_value_id (void);
234 unsigned int get_next_value_id (void);
235 unsigned int get_constant_value_id (tree);
236 unsigned int get_or_alloc_constant_value_id (tree);
237 bool value_id_constant_p (unsigned int);
238 tree fully_constant_vn_reference_p (vn_reference_t);
239 
240 /* Valueize NAME if it is an SSA name, otherwise just return it.  */
241 
242 static inline tree
vn_valueize(tree name)243 vn_valueize (tree name)
244 {
245   if (TREE_CODE (name) == SSA_NAME)
246     {
247       tree tem = VN_INFO (name)->valnum;
248       return tem == VN_TOP ? name : tem;
249     }
250   return name;
251 }
252 
253 /* Get at the original range info for NAME.  */
254 
255 inline range_info_def *
VN_INFO_RANGE_INFO(tree name)256 VN_INFO_RANGE_INFO (tree name)
257 {
258   return (VN_INFO (name)->info.range_info
259 	  ? VN_INFO (name)->info.range_info
260 	  : SSA_NAME_RANGE_INFO (name));
261 }
262 
263 /* Whether the original range info of NAME is an anti-range.  */
264 
265 inline bool
VN_INFO_ANTI_RANGE_P(tree name)266 VN_INFO_ANTI_RANGE_P (tree name)
267 {
268   return (VN_INFO (name)->info.range_info
269 	  ? VN_INFO (name)->range_info_anti_range_p
270 	  : SSA_NAME_ANTI_RANGE_P (name));
271 }
272 
273 /* Get at the original range info kind for NAME.  */
274 
275 inline value_range_type
VN_INFO_RANGE_TYPE(tree name)276 VN_INFO_RANGE_TYPE (tree name)
277 {
278   return VN_INFO_ANTI_RANGE_P (name) ? VR_ANTI_RANGE : VR_RANGE;
279 }
280 
281 /* Get at the original pointer info for NAME.  */
282 
283 inline ptr_info_def *
VN_INFO_PTR_INFO(tree name)284 VN_INFO_PTR_INFO (tree name)
285 {
286   return (VN_INFO (name)->info.ptr_info
287 	  ? VN_INFO (name)->info.ptr_info
288 	  : SSA_NAME_PTR_INFO (name));
289 }
290 
291 #endif /* TREE_SSA_SCCVN_H  */
292