]>
Commit | Line | Data |
---|---|---|
89fb70a3 | 1 | /* SCC value numbering for trees |
cbe34bb5 | 2 | Copyright (C) 2006-2017 Free Software Foundation, Inc. |
89fb70a3 DB |
3 | Contributed by Daniel Berlin <dan@dberlin.org> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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 | |
9dcd6f09 | 9 | the Free Software Foundation; either version 3, or (at your option) |
89fb70a3 DB |
10 | 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 | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
89fb70a3 DB |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
c7131fb2 | 24 | #include "backend.h" |
957060b5 | 25 | #include "rtl.h" |
89fb70a3 | 26 | #include "tree.h" |
c7131fb2 | 27 | #include "gimple.h" |
957060b5 | 28 | #include "alloc-pool.h" |
c7131fb2 | 29 | #include "ssa.h" |
957060b5 AM |
30 | #include "expmed.h" |
31 | #include "insn-config.h" | |
4d0cdd0c | 32 | #include "memmodel.h" |
957060b5 AM |
33 | #include "emit-rtl.h" |
34 | #include "cgraph.h" | |
35 | #include "gimple-pretty-print.h" | |
c7131fb2 | 36 | #include "alias.h" |
40e23961 | 37 | #include "fold-const.h" |
d8a2d370 | 38 | #include "stor-layout.h" |
60393bbc | 39 | #include "cfganal.h" |
89fb70a3 | 40 | #include "tree-inline.h" |
2fb9a547 AM |
41 | #include "internal-fn.h" |
42 | #include "gimple-fold.h" | |
43 | #include "tree-eh.h" | |
45b0be94 | 44 | #include "gimplify.h" |
36566b39 | 45 | #include "flags.h" |
36566b39 PK |
46 | #include "dojump.h" |
47 | #include "explow.h" | |
48 | #include "calls.h" | |
36566b39 PK |
49 | #include "varasm.h" |
50 | #include "stmt.h" | |
d8a2d370 | 51 | #include "expr.h" |
442b4905 AM |
52 | #include "tree-dfa.h" |
53 | #include "tree-ssa.h" | |
7ee2468b | 54 | #include "dumpfile.h" |
89fb70a3 | 55 | #include "cfgloop.h" |
863d2a57 | 56 | #include "params.h" |
c2979eaf | 57 | #include "tree-ssa-propagate.h" |
89fb70a3 | 58 | #include "tree-ssa-sccvn.h" |
a764d660 RB |
59 | #include "tree-cfg.h" |
60 | #include "domwalk.h" | |
d2713985 | 61 | #include "gimple-iterator.h" |
34050b6b | 62 | #include "gimple-match.h" |
89fb70a3 DB |
63 | |
64 | /* This algorithm is based on the SCC algorithm presented by Keith | |
65 | Cooper and L. Taylor Simpson in "SCC-Based Value numbering" | |
66 | (http://citeseer.ist.psu.edu/41805.html). In | |
67 | straight line code, it is equivalent to a regular hash based value | |
68 | numbering that is performed in reverse postorder. | |
69 | ||
70 | For code with cycles, there are two alternatives, both of which | |
71 | require keeping the hashtables separate from the actual list of | |
72 | value numbers for SSA names. | |
73 | ||
74 | 1. Iterate value numbering in an RPO walk of the blocks, removing | |
75 | all the entries from the hashtable after each iteration (but | |
76 | keeping the SSA name->value number mapping between iterations). | |
77 | Iterate until it does not change. | |
78 | ||
79 | 2. Perform value numbering as part of an SCC walk on the SSA graph, | |
80 | iterating only the cycles in the SSA graph until they do not change | |
81 | (using a separate, optimistic hashtable for value numbering the SCC | |
82 | operands). | |
83 | ||
84 | The second is not just faster in practice (because most SSA graph | |
85 | cycles do not involve all the variables in the graph), it also has | |
86 | some nice properties. | |
87 | ||
88 | One of these nice properties is that when we pop an SCC off the | |
89 | stack, we are guaranteed to have processed all the operands coming from | |
90 | *outside of that SCC*, so we do not need to do anything special to | |
91 | ensure they have value numbers. | |
92 | ||
93 | Another nice property is that the SCC walk is done as part of a DFS | |
94 | of the SSA graph, which makes it easy to perform combining and | |
95 | simplifying operations at the same time. | |
96 | ||
97 | The code below is deliberately written in a way that makes it easy | |
98 | to separate the SCC walk from the other work it does. | |
99 | ||
100 | In order to propagate constants through the code, we track which | |
101 | expressions contain constants, and use those while folding. In | |
102 | theory, we could also track expressions whose value numbers are | |
103 | replaced, in case we end up folding based on expression | |
104 | identities. | |
105 | ||
106 | In order to value number memory, we assign value numbers to vuses. | |
107 | This enables us to note that, for example, stores to the same | |
108 | address of the same value from the same starting memory states are | |
070b797d | 109 | equivalent. |
89fb70a3 DB |
110 | TODO: |
111 | ||
112 | 1. We can iterate only the changing portions of the SCC's, but | |
113 | I have not seen an SCC big enough for this to be a win. | |
114 | 2. If you differentiate between phi nodes for loops and phi nodes | |
115 | for if-then-else, you can properly consider phi nodes in different | |
116 | blocks for equivalence. | |
117 | 3. We could value number vuses in more cases, particularly, whole | |
118 | structure copies. | |
119 | */ | |
120 | ||
bf190e8d | 121 | |
4da60082 RB |
122 | static tree *last_vuse_ptr; |
123 | static vn_lookup_kind vn_walk_kind; | |
124 | static vn_lookup_kind default_vn_walk_kind; | |
e7cbc096 | 125 | bitmap const_parms; |
4da60082 | 126 | |
bf190e8d LC |
127 | /* vn_nary_op hashtable helpers. */ |
128 | ||
8d67ee55 | 129 | struct vn_nary_op_hasher : nofree_ptr_hash <vn_nary_op_s> |
bf190e8d | 130 | { |
67f58944 TS |
131 | typedef vn_nary_op_s *compare_type; |
132 | static inline hashval_t hash (const vn_nary_op_s *); | |
133 | static inline bool equal (const vn_nary_op_s *, const vn_nary_op_s *); | |
bf190e8d LC |
134 | }; |
135 | ||
136 | /* Return the computed hashcode for nary operation P1. */ | |
137 | ||
138 | inline hashval_t | |
67f58944 | 139 | vn_nary_op_hasher::hash (const vn_nary_op_s *vno1) |
bf190e8d LC |
140 | { |
141 | return vno1->hashcode; | |
142 | } | |
143 | ||
144 | /* Compare nary operations P1 and P2 and return true if they are | |
145 | equivalent. */ | |
146 | ||
147 | inline bool | |
67f58944 | 148 | vn_nary_op_hasher::equal (const vn_nary_op_s *vno1, const vn_nary_op_s *vno2) |
bf190e8d LC |
149 | { |
150 | return vn_nary_op_eq (vno1, vno2); | |
151 | } | |
152 | ||
c203e8a7 | 153 | typedef hash_table<vn_nary_op_hasher> vn_nary_op_table_type; |
bf190e8d LC |
154 | typedef vn_nary_op_table_type::iterator vn_nary_op_iterator_type; |
155 | ||
156 | ||
157 | /* vn_phi hashtable helpers. */ | |
158 | ||
159 | static int | |
160 | vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2); | |
161 | ||
7edd9b15 | 162 | struct vn_phi_hasher : pointer_hash <vn_phi_s> |
bf190e8d | 163 | { |
67f58944 TS |
164 | static inline hashval_t hash (const vn_phi_s *); |
165 | static inline bool equal (const vn_phi_s *, const vn_phi_s *); | |
166 | static inline void remove (vn_phi_s *); | |
bf190e8d LC |
167 | }; |
168 | ||
169 | /* Return the computed hashcode for phi operation P1. */ | |
170 | ||
171 | inline hashval_t | |
67f58944 | 172 | vn_phi_hasher::hash (const vn_phi_s *vp1) |
bf190e8d LC |
173 | { |
174 | return vp1->hashcode; | |
175 | } | |
176 | ||
177 | /* Compare two phi entries for equality, ignoring VN_TOP arguments. */ | |
178 | ||
179 | inline bool | |
67f58944 | 180 | vn_phi_hasher::equal (const vn_phi_s *vp1, const vn_phi_s *vp2) |
bf190e8d LC |
181 | { |
182 | return vn_phi_eq (vp1, vp2); | |
183 | } | |
184 | ||
185 | /* Free a phi operation structure VP. */ | |
186 | ||
187 | inline void | |
67f58944 | 188 | vn_phi_hasher::remove (vn_phi_s *phi) |
bf190e8d LC |
189 | { |
190 | phi->phiargs.release (); | |
191 | } | |
192 | ||
c203e8a7 | 193 | typedef hash_table<vn_phi_hasher> vn_phi_table_type; |
bf190e8d LC |
194 | typedef vn_phi_table_type::iterator vn_phi_iterator_type; |
195 | ||
196 | ||
197 | /* Compare two reference operands P1 and P2 for equality. Return true if | |
198 | they are equal, and false otherwise. */ | |
199 | ||
200 | static int | |
201 | vn_reference_op_eq (const void *p1, const void *p2) | |
202 | { | |
203 | const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1; | |
204 | const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2; | |
205 | ||
206 | return (vro1->opcode == vro2->opcode | |
207 | /* We do not care for differences in type qualification. */ | |
208 | && (vro1->type == vro2->type | |
209 | || (vro1->type && vro2->type | |
210 | && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type), | |
211 | TYPE_MAIN_VARIANT (vro2->type)))) | |
212 | && expressions_equal_p (vro1->op0, vro2->op0) | |
213 | && expressions_equal_p (vro1->op1, vro2->op1) | |
214 | && expressions_equal_p (vro1->op2, vro2->op2)); | |
215 | } | |
216 | ||
217 | /* Free a reference operation structure VP. */ | |
218 | ||
219 | static inline void | |
220 | free_reference (vn_reference_s *vr) | |
221 | { | |
222 | vr->operands.release (); | |
223 | } | |
224 | ||
225 | ||
226 | /* vn_reference hashtable helpers. */ | |
227 | ||
7edd9b15 | 228 | struct vn_reference_hasher : pointer_hash <vn_reference_s> |
bf190e8d | 229 | { |
67f58944 TS |
230 | static inline hashval_t hash (const vn_reference_s *); |
231 | static inline bool equal (const vn_reference_s *, const vn_reference_s *); | |
232 | static inline void remove (vn_reference_s *); | |
bf190e8d LC |
233 | }; |
234 | ||
235 | /* Return the hashcode for a given reference operation P1. */ | |
236 | ||
237 | inline hashval_t | |
67f58944 | 238 | vn_reference_hasher::hash (const vn_reference_s *vr1) |
bf190e8d LC |
239 | { |
240 | return vr1->hashcode; | |
241 | } | |
242 | ||
243 | inline bool | |
67f58944 | 244 | vn_reference_hasher::equal (const vn_reference_s *v, const vn_reference_s *c) |
bf190e8d LC |
245 | { |
246 | return vn_reference_eq (v, c); | |
247 | } | |
248 | ||
249 | inline void | |
67f58944 | 250 | vn_reference_hasher::remove (vn_reference_s *v) |
bf190e8d LC |
251 | { |
252 | free_reference (v); | |
253 | } | |
254 | ||
c203e8a7 | 255 | typedef hash_table<vn_reference_hasher> vn_reference_table_type; |
bf190e8d LC |
256 | typedef vn_reference_table_type::iterator vn_reference_iterator_type; |
257 | ||
258 | ||
89fb70a3 DB |
259 | /* The set of hashtables and alloc_pool's for their items. */ |
260 | ||
261 | typedef struct vn_tables_s | |
262 | { | |
c203e8a7 TS |
263 | vn_nary_op_table_type *nary; |
264 | vn_phi_table_type *phis; | |
265 | vn_reference_table_type *references; | |
49a1fb2d | 266 | struct obstack nary_obstack; |
fb0b2914 ML |
267 | object_allocator<vn_phi_s> *phis_pool; |
268 | object_allocator<vn_reference_s> *references_pool; | |
89fb70a3 DB |
269 | } *vn_tables_t; |
270 | ||
bf190e8d LC |
271 | |
272 | /* vn_constant hashtable helpers. */ | |
273 | ||
95fbe13e | 274 | struct vn_constant_hasher : free_ptr_hash <vn_constant_s> |
bf190e8d | 275 | { |
67f58944 TS |
276 | static inline hashval_t hash (const vn_constant_s *); |
277 | static inline bool equal (const vn_constant_s *, const vn_constant_s *); | |
bf190e8d LC |
278 | }; |
279 | ||
280 | /* Hash table hash function for vn_constant_t. */ | |
281 | ||
282 | inline hashval_t | |
67f58944 | 283 | vn_constant_hasher::hash (const vn_constant_s *vc1) |
bf190e8d LC |
284 | { |
285 | return vc1->hashcode; | |
286 | } | |
287 | ||
288 | /* Hash table equality function for vn_constant_t. */ | |
289 | ||
290 | inline bool | |
67f58944 | 291 | vn_constant_hasher::equal (const vn_constant_s *vc1, const vn_constant_s *vc2) |
bf190e8d LC |
292 | { |
293 | if (vc1->hashcode != vc2->hashcode) | |
294 | return false; | |
295 | ||
296 | return vn_constant_eq_with_type (vc1->constant, vc2->constant); | |
297 | } | |
298 | ||
c203e8a7 | 299 | static hash_table<vn_constant_hasher> *constant_to_value_id; |
c9145754 | 300 | static bitmap constant_value_ids; |
89fb70a3 | 301 | |
89fb70a3 DB |
302 | |
303 | /* Valid hashtables storing information we have proven to be | |
304 | correct. */ | |
305 | ||
306 | static vn_tables_t valid_info; | |
307 | ||
308 | /* Optimistic hashtables storing information we are making assumptions about | |
309 | during iterations. */ | |
310 | ||
311 | static vn_tables_t optimistic_info; | |
312 | ||
89fb70a3 DB |
313 | /* Pointer to the set of hashtables that is currently being used. |
314 | Should always point to either the optimistic_info, or the | |
315 | valid_info. */ | |
316 | ||
317 | static vn_tables_t current_info; | |
318 | ||
319 | ||
320 | /* Reverse post order index for each basic block. */ | |
321 | ||
322 | static int *rpo_numbers; | |
323 | ||
324 | #define SSA_VAL(x) (VN_INFO ((x))->valnum) | |
325 | ||
d1c0308e RB |
326 | /* Return the SSA value of the VUSE x, supporting released VDEFs |
327 | during elimination which will value-number the VDEF to the | |
328 | associated VUSE (but not substitute in the whole lattice). */ | |
329 | ||
330 | static inline tree | |
331 | vuse_ssa_val (tree x) | |
332 | { | |
333 | if (!x) | |
334 | return NULL_TREE; | |
335 | ||
336 | do | |
337 | { | |
338 | x = SSA_VAL (x); | |
339 | } | |
340 | while (SSA_NAME_IN_FREE_LIST (x)); | |
341 | ||
342 | return x; | |
343 | } | |
344 | ||
89fb70a3 DB |
345 | /* This represents the top of the VN lattice, which is the universal |
346 | value. */ | |
347 | ||
348 | tree VN_TOP; | |
349 | ||
c9145754 DB |
350 | /* Unique counter for our value ids. */ |
351 | ||
352 | static unsigned int next_value_id; | |
353 | ||
89fb70a3 DB |
354 | /* Next DFS number and the stack for strongly connected component |
355 | detection. */ | |
356 | ||
357 | static unsigned int next_dfs_num; | |
9771b263 | 358 | static vec<tree> sccstack; |
89fb70a3 | 359 | |
3d45dd59 | 360 | |
89fb70a3 | 361 | |
cbfb21c1 SB |
362 | /* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects |
363 | are allocated on an obstack for locality reasons, and to free them | |
9771b263 | 364 | without looping over the vec. */ |
89fb70a3 | 365 | |
9771b263 | 366 | static vec<vn_ssa_aux_t> vn_ssa_aux_table; |
cbfb21c1 | 367 | static struct obstack vn_ssa_aux_obstack; |
89fb70a3 | 368 | |
6a8b77b2 RB |
369 | /* Return whether there is value numbering information for a given SSA name. */ |
370 | ||
371 | bool | |
372 | has_VN_INFO (tree name) | |
373 | { | |
374 | if (SSA_NAME_VERSION (name) < vn_ssa_aux_table.length ()) | |
375 | return vn_ssa_aux_table[SSA_NAME_VERSION (name)] != NULL; | |
376 | return false; | |
377 | } | |
378 | ||
89fb70a3 DB |
379 | /* Return the value numbering information for a given SSA name. */ |
380 | ||
381 | vn_ssa_aux_t | |
382 | VN_INFO (tree name) | |
383 | { | |
9771b263 | 384 | vn_ssa_aux_t res = vn_ssa_aux_table[SSA_NAME_VERSION (name)]; |
7a40b8b1 | 385 | gcc_checking_assert (res); |
c9145754 | 386 | return res; |
89fb70a3 DB |
387 | } |
388 | ||
389 | /* Set the value numbering info for a given SSA name to a given | |
390 | value. */ | |
391 | ||
392 | static inline void | |
393 | VN_INFO_SET (tree name, vn_ssa_aux_t value) | |
394 | { | |
9771b263 | 395 | vn_ssa_aux_table[SSA_NAME_VERSION (name)] = value; |
89fb70a3 DB |
396 | } |
397 | ||
cbfb21c1 SB |
398 | /* Initialize the value numbering info for a given SSA name. |
399 | This should be called just once for every SSA name. */ | |
89fb70a3 DB |
400 | |
401 | vn_ssa_aux_t | |
402 | VN_INFO_GET (tree name) | |
403 | { | |
cbfb21c1 SB |
404 | vn_ssa_aux_t newinfo; |
405 | ||
34050b6b RB |
406 | gcc_assert (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length () |
407 | || vn_ssa_aux_table[SSA_NAME_VERSION (name)] == NULL); | |
3d9a9f94 | 408 | newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux); |
cbfb21c1 | 409 | memset (newinfo, 0, sizeof (struct vn_ssa_aux)); |
9771b263 | 410 | if (SSA_NAME_VERSION (name) >= vn_ssa_aux_table.length ()) |
511e5c48 | 411 | vn_ssa_aux_table.safe_grow_cleared (SSA_NAME_VERSION (name) + 1); |
9771b263 | 412 | vn_ssa_aux_table[SSA_NAME_VERSION (name)] = newinfo; |
89fb70a3 DB |
413 | return newinfo; |
414 | } | |
415 | ||
416 | ||
17742d62 RG |
417 | /* Return the vn_kind the expression computed by the stmt should be |
418 | associated with. */ | |
419 | ||
420 | enum vn_kind | |
355fe088 | 421 | vn_get_stmt_kind (gimple *stmt) |
17742d62 RG |
422 | { |
423 | switch (gimple_code (stmt)) | |
424 | { | |
425 | case GIMPLE_CALL: | |
426 | return VN_REFERENCE; | |
427 | case GIMPLE_PHI: | |
428 | return VN_PHI; | |
429 | case GIMPLE_ASSIGN: | |
430 | { | |
431 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
432 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
433 | switch (get_gimple_rhs_class (code)) | |
434 | { | |
435 | case GIMPLE_UNARY_RHS: | |
436 | case GIMPLE_BINARY_RHS: | |
437 | case GIMPLE_TERNARY_RHS: | |
438 | return VN_NARY; | |
439 | case GIMPLE_SINGLE_RHS: | |
440 | switch (TREE_CODE_CLASS (code)) | |
441 | { | |
442 | case tcc_reference: | |
443 | /* VOP-less references can go through unary case. */ | |
444 | if ((code == REALPART_EXPR | |
445 | || code == IMAGPART_EXPR | |
446 | || code == VIEW_CONVERT_EXPR | |
447 | || code == BIT_FIELD_REF) | |
448 | && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME) | |
449 | return VN_NARY; | |
450 | ||
451 | /* Fallthrough. */ | |
452 | case tcc_declaration: | |
453 | return VN_REFERENCE; | |
454 | ||
455 | case tcc_constant: | |
456 | return VN_CONSTANT; | |
457 | ||
458 | default: | |
459 | if (code == ADDR_EXPR) | |
460 | return (is_gimple_min_invariant (rhs1) | |
461 | ? VN_CONSTANT : VN_REFERENCE); | |
462 | else if (code == CONSTRUCTOR) | |
463 | return VN_NARY; | |
464 | return VN_NONE; | |
465 | } | |
466 | default: | |
467 | return VN_NONE; | |
468 | } | |
469 | } | |
470 | default: | |
471 | return VN_NONE; | |
472 | } | |
473 | } | |
726a989a | 474 | |
bb9e4199 RG |
475 | /* Lookup a value id for CONSTANT and return it. If it does not |
476 | exist returns 0. */ | |
477 | ||
478 | unsigned int | |
479 | get_constant_value_id (tree constant) | |
480 | { | |
bf190e8d | 481 | vn_constant_s **slot; |
bb9e4199 | 482 | struct vn_constant_s vc; |
726a989a RB |
483 | |
484 | vc.hashcode = vn_hash_constant_with_type (constant); | |
bb9e4199 | 485 | vc.constant = constant; |
c203e8a7 | 486 | slot = constant_to_value_id->find_slot (&vc, NO_INSERT); |
bb9e4199 | 487 | if (slot) |
bf190e8d | 488 | return (*slot)->value_id; |
bb9e4199 RG |
489 | return 0; |
490 | } | |
491 | ||
c9145754 DB |
492 | /* Lookup a value id for CONSTANT, and if it does not exist, create a |
493 | new one and return it. If it does exist, return it. */ | |
494 | ||
495 | unsigned int | |
496 | get_or_alloc_constant_value_id (tree constant) | |
497 | { | |
bf190e8d | 498 | vn_constant_s **slot; |
a7d04a53 RG |
499 | struct vn_constant_s vc; |
500 | vn_constant_t vcp; | |
b8698a0f | 501 | |
a7d04a53 RG |
502 | vc.hashcode = vn_hash_constant_with_type (constant); |
503 | vc.constant = constant; | |
c203e8a7 | 504 | slot = constant_to_value_id->find_slot (&vc, INSERT); |
c9145754 | 505 | if (*slot) |
bf190e8d | 506 | return (*slot)->value_id; |
a7d04a53 RG |
507 | |
508 | vcp = XNEW (struct vn_constant_s); | |
509 | vcp->hashcode = vc.hashcode; | |
510 | vcp->constant = constant; | |
511 | vcp->value_id = get_next_value_id (); | |
bf190e8d | 512 | *slot = vcp; |
a7d04a53 RG |
513 | bitmap_set_bit (constant_value_ids, vcp->value_id); |
514 | return vcp->value_id; | |
c9145754 DB |
515 | } |
516 | ||
517 | /* Return true if V is a value id for a constant. */ | |
518 | ||
519 | bool | |
520 | value_id_constant_p (unsigned int v) | |
521 | { | |
b8698a0f | 522 | return bitmap_bit_p (constant_value_ids, v); |
c9145754 DB |
523 | } |
524 | ||
b40bf772 | 525 | /* Compute the hash for a reference operand VRO1. */ |
89fb70a3 | 526 | |
4e44a6e8 AK |
527 | static void |
528 | vn_reference_op_compute_hash (const vn_reference_op_t vro1, inchash::hash &hstate) | |
89fb70a3 | 529 | { |
4e44a6e8 | 530 | hstate.add_int (vro1->opcode); |
85169114 | 531 | if (vro1->op0) |
4e44a6e8 | 532 | inchash::add_expr (vro1->op0, hstate); |
85169114 | 533 | if (vro1->op1) |
4e44a6e8 | 534 | inchash::add_expr (vro1->op1, hstate); |
85169114 | 535 | if (vro1->op2) |
4e44a6e8 | 536 | inchash::add_expr (vro1->op2, hstate); |
89fb70a3 DB |
537 | } |
538 | ||
89fb70a3 DB |
539 | /* Compute a hash for the reference operation VR1 and return it. */ |
540 | ||
26f3a4e1 | 541 | static hashval_t |
89fb70a3 DB |
542 | vn_reference_compute_hash (const vn_reference_t vr1) |
543 | { | |
4e44a6e8 AK |
544 | inchash::hash hstate; |
545 | hashval_t result; | |
89fb70a3 DB |
546 | int i; |
547 | vn_reference_op_t vro; | |
70f34814 RG |
548 | HOST_WIDE_INT off = -1; |
549 | bool deref = false; | |
89fb70a3 | 550 | |
9771b263 | 551 | FOR_EACH_VEC_ELT (vr1->operands, i, vro) |
70f34814 RG |
552 | { |
553 | if (vro->opcode == MEM_REF) | |
554 | deref = true; | |
555 | else if (vro->opcode != ADDR_EXPR) | |
556 | deref = false; | |
557 | if (vro->off != -1) | |
558 | { | |
559 | if (off == -1) | |
560 | off = 0; | |
561 | off += vro->off; | |
562 | } | |
563 | else | |
564 | { | |
565 | if (off != -1 | |
566 | && off != 0) | |
4e44a6e8 | 567 | hstate.add_int (off); |
70f34814 RG |
568 | off = -1; |
569 | if (deref | |
570 | && vro->opcode == ADDR_EXPR) | |
571 | { | |
572 | if (vro->op0) | |
573 | { | |
574 | tree op = TREE_OPERAND (vro->op0, 0); | |
4e44a6e8 AK |
575 | hstate.add_int (TREE_CODE (op)); |
576 | inchash::add_expr (op, hstate); | |
70f34814 RG |
577 | } |
578 | } | |
579 | else | |
4e44a6e8 | 580 | vn_reference_op_compute_hash (vro, hstate); |
70f34814 RG |
581 | } |
582 | } | |
4e44a6e8 AK |
583 | result = hstate.end (); |
584 | /* ??? We would ICE later if we hash instead of adding that in. */ | |
9708c51d RG |
585 | if (vr1->vuse) |
586 | result += SSA_NAME_VERSION (vr1->vuse); | |
89fb70a3 DB |
587 | |
588 | return result; | |
589 | } | |
590 | ||
bf190e8d | 591 | /* Return true if reference operations VR1 and VR2 are equivalent. This |
89fb70a3 DB |
592 | means they have the same set of operands and vuses. */ |
593 | ||
bf190e8d LC |
594 | bool |
595 | vn_reference_eq (const_vn_reference_t const vr1, const_vn_reference_t const vr2) | |
89fb70a3 | 596 | { |
70f34814 | 597 | unsigned i, j; |
89fb70a3 | 598 | |
5006671f RG |
599 | /* Early out if this is not a hash collision. */ |
600 | if (vr1->hashcode != vr2->hashcode) | |
601 | return false; | |
89fb70a3 | 602 | |
5006671f RG |
603 | /* The VOP needs to be the same. */ |
604 | if (vr1->vuse != vr2->vuse) | |
89fb70a3 DB |
605 | return false; |
606 | ||
5006671f RG |
607 | /* If the operands are the same we are done. */ |
608 | if (vr1->operands == vr2->operands) | |
609 | return true; | |
610 | ||
70f34814 | 611 | if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type))) |
89fb70a3 DB |
612 | return false; |
613 | ||
5ccbfc1f RG |
614 | if (INTEGRAL_TYPE_P (vr1->type) |
615 | && INTEGRAL_TYPE_P (vr2->type)) | |
616 | { | |
617 | if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type)) | |
618 | return false; | |
619 | } | |
620 | else if (INTEGRAL_TYPE_P (vr1->type) | |
621 | && (TYPE_PRECISION (vr1->type) | |
622 | != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type)))) | |
623 | return false; | |
624 | else if (INTEGRAL_TYPE_P (vr2->type) | |
625 | && (TYPE_PRECISION (vr2->type) | |
626 | != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type)))) | |
627 | return false; | |
628 | ||
70f34814 RG |
629 | i = 0; |
630 | j = 0; | |
631 | do | |
632 | { | |
633 | HOST_WIDE_INT off1 = 0, off2 = 0; | |
634 | vn_reference_op_t vro1, vro2; | |
635 | vn_reference_op_s tem1, tem2; | |
636 | bool deref1 = false, deref2 = false; | |
9771b263 | 637 | for (; vr1->operands.iterate (i, &vro1); i++) |
70f34814 RG |
638 | { |
639 | if (vro1->opcode == MEM_REF) | |
640 | deref1 = true; | |
ee45a32d EB |
641 | /* Do not look through a storage order barrier. */ |
642 | else if (vro1->opcode == VIEW_CONVERT_EXPR && vro1->reverse) | |
643 | return false; | |
70f34814 RG |
644 | if (vro1->off == -1) |
645 | break; | |
646 | off1 += vro1->off; | |
647 | } | |
9771b263 | 648 | for (; vr2->operands.iterate (j, &vro2); j++) |
70f34814 RG |
649 | { |
650 | if (vro2->opcode == MEM_REF) | |
651 | deref2 = true; | |
ee45a32d EB |
652 | /* Do not look through a storage order barrier. */ |
653 | else if (vro2->opcode == VIEW_CONVERT_EXPR && vro2->reverse) | |
654 | return false; | |
70f34814 RG |
655 | if (vro2->off == -1) |
656 | break; | |
657 | off2 += vro2->off; | |
658 | } | |
659 | if (off1 != off2) | |
660 | return false; | |
661 | if (deref1 && vro1->opcode == ADDR_EXPR) | |
662 | { | |
663 | memset (&tem1, 0, sizeof (tem1)); | |
664 | tem1.op0 = TREE_OPERAND (vro1->op0, 0); | |
665 | tem1.type = TREE_TYPE (tem1.op0); | |
666 | tem1.opcode = TREE_CODE (tem1.op0); | |
667 | vro1 = &tem1; | |
8bf43909 | 668 | deref1 = false; |
70f34814 RG |
669 | } |
670 | if (deref2 && vro2->opcode == ADDR_EXPR) | |
671 | { | |
672 | memset (&tem2, 0, sizeof (tem2)); | |
673 | tem2.op0 = TREE_OPERAND (vro2->op0, 0); | |
674 | tem2.type = TREE_TYPE (tem2.op0); | |
675 | tem2.opcode = TREE_CODE (tem2.op0); | |
676 | vro2 = &tem2; | |
8bf43909 | 677 | deref2 = false; |
70f34814 | 678 | } |
8bf43909 RG |
679 | if (deref1 != deref2) |
680 | return false; | |
70f34814 RG |
681 | if (!vn_reference_op_eq (vro1, vro2)) |
682 | return false; | |
683 | ++j; | |
684 | ++i; | |
685 | } | |
9771b263 DN |
686 | while (vr1->operands.length () != i |
687 | || vr2->operands.length () != j); | |
89fb70a3 | 688 | |
5006671f | 689 | return true; |
89fb70a3 DB |
690 | } |
691 | ||
726a989a | 692 | /* Copy the operations present in load/store REF into RESULT, a vector of |
89fb70a3 DB |
693 | vn_reference_op_s's. */ |
694 | ||
26f3a4e1 | 695 | static void |
9771b263 | 696 | copy_reference_ops_from_ref (tree ref, vec<vn_reference_op_s> *result) |
89fb70a3 | 697 | { |
9f59420d RG |
698 | if (TREE_CODE (ref) == TARGET_MEM_REF) |
699 | { | |
700 | vn_reference_op_s temp; | |
701 | ||
39e843e8 RB |
702 | result->reserve (3); |
703 | ||
9f59420d | 704 | memset (&temp, 0, sizeof (temp)); |
6d6c9525 | 705 | temp.type = TREE_TYPE (ref); |
9f59420d | 706 | temp.opcode = TREE_CODE (ref); |
150e3929 RG |
707 | temp.op0 = TMR_INDEX (ref); |
708 | temp.op1 = TMR_STEP (ref); | |
709 | temp.op2 = TMR_OFFSET (ref); | |
70f34814 | 710 | temp.off = -1; |
1fefbb66 RB |
711 | temp.clique = MR_DEPENDENCE_CLIQUE (ref); |
712 | temp.base = MR_DEPENDENCE_BASE (ref); | |
39e843e8 | 713 | result->quick_push (temp); |
9f59420d RG |
714 | |
715 | memset (&temp, 0, sizeof (temp)); | |
716 | temp.type = NULL_TREE; | |
4d948885 RG |
717 | temp.opcode = ERROR_MARK; |
718 | temp.op0 = TMR_INDEX2 (ref); | |
719 | temp.off = -1; | |
39e843e8 | 720 | result->quick_push (temp); |
4d948885 RG |
721 | |
722 | memset (&temp, 0, sizeof (temp)); | |
723 | temp.type = NULL_TREE; | |
724 | temp.opcode = TREE_CODE (TMR_BASE (ref)); | |
725 | temp.op0 = TMR_BASE (ref); | |
70f34814 | 726 | temp.off = -1; |
39e843e8 | 727 | result->quick_push (temp); |
9f59420d RG |
728 | return; |
729 | } | |
730 | ||
89fb70a3 | 731 | /* For non-calls, store the information that makes up the address. */ |
1eadb567 | 732 | tree orig = ref; |
89fb70a3 DB |
733 | while (ref) |
734 | { | |
735 | vn_reference_op_s temp; | |
736 | ||
737 | memset (&temp, 0, sizeof (temp)); | |
6d6c9525 | 738 | temp.type = TREE_TYPE (ref); |
89fb70a3 | 739 | temp.opcode = TREE_CODE (ref); |
70f34814 | 740 | temp.off = -1; |
89fb70a3 DB |
741 | |
742 | switch (temp.opcode) | |
743 | { | |
4ec0a198 TV |
744 | case MODIFY_EXPR: |
745 | temp.op0 = TREE_OPERAND (ref, 1); | |
746 | break; | |
842679dc TV |
747 | case WITH_SIZE_EXPR: |
748 | temp.op0 = TREE_OPERAND (ref, 1); | |
749 | temp.off = 0; | |
750 | break; | |
70f34814 RG |
751 | case MEM_REF: |
752 | /* The base address gets its own vn_reference_op_s structure. */ | |
753 | temp.op0 = TREE_OPERAND (ref, 1); | |
8ab1d9d7 RB |
754 | { |
755 | offset_int off = mem_ref_offset (ref); | |
756 | if (wi::fits_shwi_p (off)) | |
757 | temp.off = off.to_shwi (); | |
758 | } | |
1fefbb66 RB |
759 | temp.clique = MR_DEPENDENCE_CLIQUE (ref); |
760 | temp.base = MR_DEPENDENCE_BASE (ref); | |
ee45a32d | 761 | temp.reverse = REF_REVERSE_STORAGE_ORDER (ref); |
70f34814 | 762 | break; |
89fb70a3 | 763 | case BIT_FIELD_REF: |
ee45a32d | 764 | /* Record bits, position and storage order. */ |
89fb70a3 DB |
765 | temp.op0 = TREE_OPERAND (ref, 1); |
766 | temp.op1 = TREE_OPERAND (ref, 2); | |
1fefbb66 RB |
767 | if (tree_fits_shwi_p (TREE_OPERAND (ref, 2))) |
768 | { | |
769 | HOST_WIDE_INT off = tree_to_shwi (TREE_OPERAND (ref, 2)); | |
770 | if (off % BITS_PER_UNIT == 0) | |
771 | temp.off = off / BITS_PER_UNIT; | |
772 | } | |
ee45a32d | 773 | temp.reverse = REF_REVERSE_STORAGE_ORDER (ref); |
89fb70a3 DB |
774 | break; |
775 | case COMPONENT_REF: | |
ba2e1892 RG |
776 | /* The field decl is enough to unambiguously specify the field, |
777 | a matching type is not necessary and a mismatching type | |
778 | is always a spurious difference. */ | |
779 | temp.type = NULL_TREE; | |
b45d2719 RG |
780 | temp.op0 = TREE_OPERAND (ref, 1); |
781 | temp.op1 = TREE_OPERAND (ref, 2); | |
70f34814 RG |
782 | { |
783 | tree this_offset = component_ref_field_offset (ref); | |
784 | if (this_offset | |
785 | && TREE_CODE (this_offset) == INTEGER_CST) | |
786 | { | |
787 | tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1)); | |
788 | if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0) | |
789 | { | |
807e902e KZ |
790 | offset_int off |
791 | = (wi::to_offset (this_offset) | |
8de73453 | 792 | + (wi::to_offset (bit_offset) >> LOG2_BITS_PER_UNIT)); |
807e902e | 793 | if (wi::fits_shwi_p (off) |
1eadb567 RB |
794 | /* Probibit value-numbering zero offset components |
795 | of addresses the same before the pass folding | |
796 | __builtin_object_size had a chance to run | |
797 | (checking cfun->after_inlining does the | |
798 | trick here). */ | |
799 | && (TREE_CODE (orig) != ADDR_EXPR | |
807e902e | 800 | || off != 0 |
1eadb567 | 801 | || cfun->after_inlining)) |
807e902e | 802 | temp.off = off.to_shwi (); |
70f34814 RG |
803 | } |
804 | } | |
805 | } | |
89fb70a3 DB |
806 | break; |
807 | case ARRAY_RANGE_REF: | |
808 | case ARRAY_REF: | |
cef5388d RB |
809 | { |
810 | tree eltype = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref, 0))); | |
811 | /* Record index as operand. */ | |
812 | temp.op0 = TREE_OPERAND (ref, 1); | |
813 | /* Always record lower bounds and element size. */ | |
814 | temp.op1 = array_ref_low_bound (ref); | |
815 | /* But record element size in units of the type alignment. */ | |
816 | temp.op2 = TREE_OPERAND (ref, 3); | |
817 | temp.align = eltype->type_common.align; | |
818 | if (! temp.op2) | |
819 | temp.op2 = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (eltype), | |
820 | size_int (TYPE_ALIGN_UNIT (eltype))); | |
821 | if (TREE_CODE (temp.op0) == INTEGER_CST | |
822 | && TREE_CODE (temp.op1) == INTEGER_CST | |
823 | && TREE_CODE (temp.op2) == INTEGER_CST) | |
824 | { | |
825 | offset_int off = ((wi::to_offset (temp.op0) | |
826 | - wi::to_offset (temp.op1)) | |
827 | * wi::to_offset (temp.op2) | |
828 | * vn_ref_op_align_unit (&temp)); | |
829 | if (wi::fits_shwi_p (off)) | |
830 | temp.off = off.to_shwi(); | |
831 | } | |
832 | } | |
89fb70a3 | 833 | break; |
6d6c9525 RG |
834 | case VAR_DECL: |
835 | if (DECL_HARD_REGISTER (ref)) | |
836 | { | |
837 | temp.op0 = ref; | |
838 | break; | |
839 | } | |
840 | /* Fallthru. */ | |
841 | case PARM_DECL: | |
842 | case CONST_DECL: | |
843 | case RESULT_DECL: | |
844 | /* Canonicalize decls to MEM[&decl] which is what we end up with | |
845 | when valueizing MEM[ptr] with ptr = &decl. */ | |
846 | temp.opcode = MEM_REF; | |
847 | temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0); | |
848 | temp.off = 0; | |
9771b263 | 849 | result->safe_push (temp); |
6d6c9525 | 850 | temp.opcode = ADDR_EXPR; |
39e843e8 | 851 | temp.op0 = build1 (ADDR_EXPR, TREE_TYPE (temp.op0), ref); |
6d6c9525 RG |
852 | temp.type = TREE_TYPE (temp.op0); |
853 | temp.off = -1; | |
854 | break; | |
4794afa5 DB |
855 | case STRING_CST: |
856 | case INTEGER_CST: | |
857 | case COMPLEX_CST: | |
858 | case VECTOR_CST: | |
26b70b9f | 859 | case REAL_CST: |
0747aae4 | 860 | case FIXED_CST: |
bb0c55f6 | 861 | case CONSTRUCTOR: |
89fb70a3 DB |
862 | case SSA_NAME: |
863 | temp.op0 = ref; | |
864 | break; | |
ce94d354 RG |
865 | case ADDR_EXPR: |
866 | if (is_gimple_min_invariant (ref)) | |
867 | { | |
868 | temp.op0 = ref; | |
869 | break; | |
870 | } | |
8403c2cf | 871 | break; |
4794afa5 DB |
872 | /* These are only interesting for their operands, their |
873 | existence, and their type. They will never be the last | |
874 | ref in the chain of references (IE they require an | |
875 | operand), so we don't have to put anything | |
876 | for op* as it will be handled by the iteration */ | |
4794afa5 | 877 | case REALPART_EXPR: |
ee45a32d EB |
878 | temp.off = 0; |
879 | break; | |
4794afa5 | 880 | case VIEW_CONVERT_EXPR: |
70f34814 | 881 | temp.off = 0; |
ee45a32d | 882 | temp.reverse = storage_order_barrier_p (ref); |
70f34814 RG |
883 | break; |
884 | case IMAGPART_EXPR: | |
885 | /* This is only interesting for its constant offset. */ | |
886 | temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref))); | |
89fb70a3 | 887 | break; |
4794afa5 DB |
888 | default: |
889 | gcc_unreachable (); | |
89fb70a3 | 890 | } |
9771b263 | 891 | result->safe_push (temp); |
89fb70a3 | 892 | |
ce94d354 | 893 | if (REFERENCE_CLASS_P (ref) |
4ec0a198 | 894 | || TREE_CODE (ref) == MODIFY_EXPR |
842679dc | 895 | || TREE_CODE (ref) == WITH_SIZE_EXPR |
ce94d354 RG |
896 | || (TREE_CODE (ref) == ADDR_EXPR |
897 | && !is_gimple_min_invariant (ref))) | |
89fb70a3 DB |
898 | ref = TREE_OPERAND (ref, 0); |
899 | else | |
900 | ref = NULL_TREE; | |
901 | } | |
902 | } | |
903 | ||
b45d2719 RG |
904 | /* Build a alias-oracle reference abstraction in *REF from the vn_reference |
905 | operands in *OPS, the reference alias set SET and the reference type TYPE. | |
906 | Return true if something useful was produced. */ | |
53f3815c | 907 | |
b45d2719 RG |
908 | bool |
909 | ao_ref_init_from_vn_reference (ao_ref *ref, | |
910 | alias_set_type set, tree type, | |
9771b263 | 911 | vec<vn_reference_op_s> ops) |
53f3815c RG |
912 | { |
913 | vn_reference_op_t op; | |
914 | unsigned i; | |
b45d2719 RG |
915 | tree base = NULL_TREE; |
916 | tree *op0_p = &base; | |
b0463d3d EB |
917 | offset_int offset = 0; |
918 | offset_int max_size; | |
919 | offset_int size = -1; | |
b45d2719 | 920 | tree size_tree = NULL_TREE; |
70f34814 | 921 | alias_set_type base_alias_set = -1; |
b45d2719 RG |
922 | |
923 | /* First get the final access size from just the outermost expression. */ | |
9771b263 | 924 | op = &ops[0]; |
b45d2719 | 925 | if (op->opcode == COMPONENT_REF) |
70f34814 | 926 | size_tree = DECL_SIZE (op->op0); |
b45d2719 RG |
927 | else if (op->opcode == BIT_FIELD_REF) |
928 | size_tree = op->op0; | |
929 | else | |
930 | { | |
ef4bddc2 | 931 | machine_mode mode = TYPE_MODE (type); |
b45d2719 RG |
932 | if (mode == BLKmode) |
933 | size_tree = TYPE_SIZE (type); | |
934 | else | |
b0463d3d | 935 | size = int (GET_MODE_BITSIZE (mode)); |
b45d2719 | 936 | } |
b0463d3d EB |
937 | if (size_tree != NULL_TREE |
938 | && TREE_CODE (size_tree) == INTEGER_CST) | |
939 | size = wi::to_offset (size_tree); | |
b45d2719 RG |
940 | |
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; | |
53f3815c | 944 | |
b45d2719 RG |
945 | /* Compute cumulative bit-offset for nested component-refs and array-refs, |
946 | and find the ultimate containing object. */ | |
9771b263 | 947 | FOR_EACH_VEC_ELT (ops, i, op) |
53f3815c RG |
948 | { |
949 | switch (op->opcode) | |
950 | { | |
b45d2719 RG |
951 | /* These may be in the reference ops, but we cannot do anything |
952 | sensible with them here. */ | |
b45d2719 | 953 | case ADDR_EXPR: |
70f34814 RG |
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))) | |
959 | { | |
9771b263 | 960 | vn_reference_op_t pop = &ops[i-1]; |
70f34814 RG |
961 | base = TREE_OPERAND (op->op0, 0); |
962 | if (pop->off == -1) | |
963 | { | |
964 | max_size = -1; | |
965 | offset = 0; | |
966 | } | |
967 | else | |
968 | offset += pop->off * BITS_PER_UNIT; | |
969 | op0_p = NULL; | |
970 | break; | |
971 | } | |
972 | /* Fallthru. */ | |
973 | case CALL_EXPR: | |
b45d2719 | 974 | return false; |
53f3815c | 975 | |
b45d2719 | 976 | /* Record the base objects. */ |
70f34814 RG |
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); | |
1fefbb66 RB |
981 | MR_DEPENDENCE_CLIQUE (*op0_p) = op->clique; |
982 | MR_DEPENDENCE_BASE (*op0_p) = op->base; | |
70f34814 RG |
983 | op0_p = &TREE_OPERAND (*op0_p, 0); |
984 | break; | |
985 | ||
b45d2719 RG |
986 | case VAR_DECL: |
987 | case PARM_DECL: | |
988 | case RESULT_DECL: | |
989 | case SSA_NAME: | |
b45d2719 | 990 | *op0_p = op->op0; |
70f34814 | 991 | op0_p = NULL; |
b45d2719 RG |
992 | break; |
993 | ||
994 | /* And now the usual component-reference style ops. */ | |
53f3815c | 995 | case BIT_FIELD_REF: |
b0463d3d | 996 | offset += wi::to_offset (op->op1); |
53f3815c RG |
997 | break; |
998 | ||
999 | case COMPONENT_REF: | |
b45d2719 RG |
1000 | { |
1001 | tree field = op->op0; | |
1002 | /* We do not have a complete COMPONENT_REF tree here so we | |
1003 | cannot use component_ref_field_offset. Do the interesting | |
1004 | parts manually. */ | |
b0463d3d | 1005 | tree this_offset = DECL_FIELD_OFFSET (field); |
b45d2719 | 1006 | |
b0463d3d | 1007 | if (op->op1 || TREE_CODE (this_offset) != INTEGER_CST) |
b45d2719 RG |
1008 | max_size = -1; |
1009 | else | |
1010 | { | |
8de73453 RS |
1011 | offset_int woffset = (wi::to_offset (this_offset) |
1012 | << LOG2_BITS_PER_UNIT); | |
b0463d3d EB |
1013 | woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field)); |
1014 | offset += woffset; | |
b45d2719 RG |
1015 | } |
1016 | break; | |
1017 | } | |
53f3815c RG |
1018 | |
1019 | case ARRAY_RANGE_REF: | |
1020 | case ARRAY_REF: | |
e52201b6 | 1021 | /* We recorded the lower bound and the element size. */ |
b0463d3d EB |
1022 | if (TREE_CODE (op->op0) != INTEGER_CST |
1023 | || TREE_CODE (op->op1) != INTEGER_CST | |
1024 | || TREE_CODE (op->op2) != INTEGER_CST) | |
b45d2719 RG |
1025 | max_size = -1; |
1026 | else | |
1027 | { | |
b0463d3d EB |
1028 | offset_int woffset |
1029 | = wi::sext (wi::to_offset (op->op0) - wi::to_offset (op->op1), | |
1030 | TYPE_PRECISION (TREE_TYPE (op->op0))); | |
cef5388d | 1031 | woffset *= wi::to_offset (op->op2) * vn_ref_op_align_unit (op); |
8de73453 | 1032 | woffset <<= LOG2_BITS_PER_UNIT; |
b0463d3d | 1033 | offset += woffset; |
b45d2719 RG |
1034 | } |
1035 | break; | |
1036 | ||
1037 | case REALPART_EXPR: | |
1038 | break; | |
1039 | ||
1040 | case IMAGPART_EXPR: | |
1041 | offset += size; | |
1042 | break; | |
1043 | ||
1044 | case VIEW_CONVERT_EXPR: | |
53f3815c RG |
1045 | break; |
1046 | ||
1047 | case STRING_CST: | |
1048 | case INTEGER_CST: | |
1049 | case COMPLEX_CST: | |
1050 | case VECTOR_CST: | |
1051 | case REAL_CST: | |
1052 | case CONSTRUCTOR: | |
53f3815c | 1053 | case CONST_DECL: |
b45d2719 | 1054 | return false; |
53f3815c RG |
1055 | |
1056 | default: | |
b45d2719 | 1057 | return false; |
53f3815c RG |
1058 | } |
1059 | } | |
1060 | ||
b45d2719 RG |
1061 | if (base == NULL_TREE) |
1062 | return false; | |
1063 | ||
1064 | ref->ref = NULL_TREE; | |
1065 | ref->base = base; | |
b45d2719 | 1066 | ref->ref_alias_set = set; |
70f34814 RG |
1067 | if (base_alias_set != -1) |
1068 | ref->base_alias_set = base_alias_set; | |
1069 | else | |
1070 | ref->base_alias_set = get_alias_set (base); | |
14cd91f9 RG |
1071 | /* We discount volatiles from value-numbering elsewhere. */ |
1072 | ref->volatile_p = false; | |
b45d2719 | 1073 | |
b0463d3d EB |
1074 | if (!wi::fits_shwi_p (size) || wi::neg_p (size)) |
1075 | { | |
1076 | ref->offset = 0; | |
1077 | ref->size = -1; | |
1078 | ref->max_size = -1; | |
1079 | return true; | |
1080 | } | |
1081 | ||
1082 | ref->size = size.to_shwi (); | |
1083 | ||
1084 | if (!wi::fits_shwi_p (offset)) | |
1085 | { | |
1086 | ref->offset = 0; | |
1087 | ref->max_size = -1; | |
1088 | return true; | |
1089 | } | |
1090 | ||
1091 | ref->offset = offset.to_shwi (); | |
1092 | ||
1093 | if (!wi::fits_shwi_p (max_size) || wi::neg_p (max_size)) | |
1094 | ref->max_size = -1; | |
1095 | else | |
1096 | ref->max_size = max_size.to_shwi (); | |
1097 | ||
b45d2719 | 1098 | return true; |
53f3815c RG |
1099 | } |
1100 | ||
726a989a RB |
1101 | /* Copy the operations present in load/store/call REF into RESULT, a vector of |
1102 | vn_reference_op_s's. */ | |
1103 | ||
26f3a4e1 | 1104 | static void |
538dd0b7 | 1105 | copy_reference_ops_from_call (gcall *call, |
9771b263 | 1106 | vec<vn_reference_op_s> *result) |
726a989a RB |
1107 | { |
1108 | vn_reference_op_s temp; | |
726a989a | 1109 | unsigned i; |
6867d9a9 | 1110 | tree lhs = gimple_call_lhs (call); |
7eab31ed | 1111 | int lr; |
6867d9a9 TV |
1112 | |
1113 | /* If 2 calls have a different non-ssa lhs, vdef value numbers should be | |
1114 | different. By adding the lhs here in the vector, we ensure that the | |
1115 | hashcode is different, guaranteeing a different value number. */ | |
1116 | if (lhs && TREE_CODE (lhs) != SSA_NAME) | |
1117 | { | |
1118 | memset (&temp, 0, sizeof (temp)); | |
1119 | temp.opcode = MODIFY_EXPR; | |
1120 | temp.type = TREE_TYPE (lhs); | |
1121 | temp.op0 = lhs; | |
1122 | temp.off = -1; | |
9771b263 | 1123 | result->safe_push (temp); |
6867d9a9 | 1124 | } |
726a989a | 1125 | |
7eab31ed | 1126 | /* Copy the type, opcode, function, static chain and EH region, if any. */ |
726a989a RB |
1127 | memset (&temp, 0, sizeof (temp)); |
1128 | temp.type = gimple_call_return_type (call); | |
1129 | temp.opcode = CALL_EXPR; | |
ce94d354 | 1130 | temp.op0 = gimple_call_fn (call); |
7aec7a38 | 1131 | temp.op1 = gimple_call_chain (call); |
7eab31ed EB |
1132 | if (stmt_could_throw_p (call) && (lr = lookup_stmt_eh_lp (call)) > 0) |
1133 | temp.op2 = size_int (lr); | |
70f34814 | 1134 | temp.off = -1; |
d5e254e1 IE |
1135 | if (gimple_call_with_bounds_p (call)) |
1136 | temp.with_bounds = 1; | |
9771b263 | 1137 | result->safe_push (temp); |
726a989a | 1138 | |
ce94d354 RG |
1139 | /* Copy the call arguments. As they can be references as well, |
1140 | just chain them together. */ | |
726a989a RB |
1141 | for (i = 0; i < gimple_call_num_args (call); ++i) |
1142 | { | |
1143 | tree callarg = gimple_call_arg (call, i); | |
ce94d354 | 1144 | copy_reference_ops_from_ref (callarg, result); |
726a989a | 1145 | } |
726a989a RB |
1146 | } |
1147 | ||
aa7069aa RG |
1148 | /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates |
1149 | *I_P to point to the last element of the replacement. */ | |
a0f79fcc | 1150 | static bool |
9771b263 | 1151 | vn_reference_fold_indirect (vec<vn_reference_op_s> *ops, |
aa7069aa | 1152 | unsigned int *i_p) |
89fb70a3 | 1153 | { |
aa7069aa | 1154 | unsigned int i = *i_p; |
9771b263 DN |
1155 | vn_reference_op_t op = &(*ops)[i]; |
1156 | vn_reference_op_t mem_op = &(*ops)[i - 1]; | |
70f34814 | 1157 | tree addr_base; |
8b4f7b47 | 1158 | HOST_WIDE_INT addr_offset = 0; |
70f34814 RG |
1159 | |
1160 | /* The only thing we have to do is from &OBJ.foo.bar add the offset | |
073a8998 | 1161 | from .foo.bar to the preceding MEM_REF offset and replace the |
70f34814 RG |
1162 | address with &OBJ. */ |
1163 | addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0), | |
1164 | &addr_offset); | |
1165 | gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF); | |
d1de852b | 1166 | if (addr_base != TREE_OPERAND (op->op0, 0)) |
70f34814 | 1167 | { |
807e902e KZ |
1168 | offset_int off = offset_int::from (mem_op->op0, SIGNED); |
1169 | off += addr_offset; | |
1170 | mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off); | |
70f34814 | 1171 | op->op0 = build_fold_addr_expr (addr_base); |
9541ffee | 1172 | if (tree_fits_shwi_p (mem_op->op0)) |
eb1ce453 | 1173 | mem_op->off = tree_to_shwi (mem_op->op0); |
70f34814 RG |
1174 | else |
1175 | mem_op->off = -1; | |
a0f79fcc | 1176 | return true; |
aa7069aa | 1177 | } |
a0f79fcc | 1178 | return false; |
aa7069aa | 1179 | } |
89fb70a3 | 1180 | |
a03a9774 RG |
1181 | /* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates |
1182 | *I_P to point to the last element of the replacement. */ | |
a0f79fcc | 1183 | static bool |
9771b263 | 1184 | vn_reference_maybe_forwprop_address (vec<vn_reference_op_s> *ops, |
a03a9774 RG |
1185 | unsigned int *i_p) |
1186 | { | |
1187 | unsigned int i = *i_p; | |
9771b263 DN |
1188 | vn_reference_op_t op = &(*ops)[i]; |
1189 | vn_reference_op_t mem_op = &(*ops)[i - 1]; | |
355fe088 | 1190 | gimple *def_stmt; |
a03a9774 | 1191 | enum tree_code code; |
807e902e | 1192 | offset_int off; |
a03a9774 RG |
1193 | |
1194 | def_stmt = SSA_NAME_DEF_STMT (op->op0); | |
d0c422cb | 1195 | if (!is_gimple_assign (def_stmt)) |
a0f79fcc | 1196 | return false; |
a03a9774 RG |
1197 | |
1198 | code = gimple_assign_rhs_code (def_stmt); | |
1199 | if (code != ADDR_EXPR | |
1200 | && code != POINTER_PLUS_EXPR) | |
a0f79fcc | 1201 | return false; |
a03a9774 | 1202 | |
807e902e | 1203 | off = offset_int::from (mem_op->op0, SIGNED); |
a03a9774 RG |
1204 | |
1205 | /* The only thing we have to do is from &OBJ.foo.bar add the offset | |
073a8998 | 1206 | from .foo.bar to the preceding MEM_REF offset and replace the |
a03a9774 RG |
1207 | address with &OBJ. */ |
1208 | if (code == ADDR_EXPR) | |
1209 | { | |
1210 | tree addr, addr_base; | |
1211 | HOST_WIDE_INT addr_offset; | |
1212 | ||
1213 | addr = gimple_assign_rhs1 (def_stmt); | |
1214 | addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0), | |
1215 | &addr_offset); | |
4da60082 RB |
1216 | /* If that didn't work because the address isn't invariant propagate |
1217 | the reference tree from the address operation in case the current | |
1218 | dereference isn't offsetted. */ | |
1219 | if (!addr_base | |
1220 | && *i_p == ops->length () - 1 | |
1221 | && off == 0 | |
1222 | /* This makes us disable this transform for PRE where the | |
1223 | reference ops might be also used for code insertion which | |
1224 | is invalid. */ | |
1225 | && default_vn_walk_kind == VN_WALKREWRITE) | |
1226 | { | |
1227 | auto_vec<vn_reference_op_s, 32> tem; | |
1228 | copy_reference_ops_from_ref (TREE_OPERAND (addr, 0), &tem); | |
e4969090 RB |
1229 | /* Make sure to preserve TBAA info. The only objects not |
1230 | wrapped in MEM_REFs that can have their address taken are | |
1231 | STRING_CSTs. */ | |
1232 | if (tem.length () >= 2 | |
1233 | && tem[tem.length () - 2].opcode == MEM_REF) | |
1234 | { | |
1235 | vn_reference_op_t new_mem_op = &tem[tem.length () - 2]; | |
1236 | new_mem_op->op0 = fold_convert (TREE_TYPE (mem_op->op0), | |
1237 | new_mem_op->op0); | |
1238 | } | |
1239 | else | |
1240 | gcc_assert (tem.last ().opcode == STRING_CST); | |
4da60082 RB |
1241 | ops->pop (); |
1242 | ops->pop (); | |
1243 | ops->safe_splice (tem); | |
1244 | --*i_p; | |
a0f79fcc | 1245 | return true; |
4da60082 | 1246 | } |
a03a9774 RG |
1247 | if (!addr_base |
1248 | || TREE_CODE (addr_base) != MEM_REF) | |
a0f79fcc | 1249 | return false; |
a03a9774 | 1250 | |
807e902e | 1251 | off += addr_offset; |
27bcd47c | 1252 | off += mem_ref_offset (addr_base); |
a03a9774 RG |
1253 | op->op0 = TREE_OPERAND (addr_base, 0); |
1254 | } | |
1255 | else | |
1256 | { | |
1257 | tree ptr, ptroff; | |
1258 | ptr = gimple_assign_rhs1 (def_stmt); | |
1259 | ptroff = gimple_assign_rhs2 (def_stmt); | |
1260 | if (TREE_CODE (ptr) != SSA_NAME | |
1261 | || TREE_CODE (ptroff) != INTEGER_CST) | |
a0f79fcc | 1262 | return false; |
a03a9774 | 1263 | |
807e902e | 1264 | off += wi::to_offset (ptroff); |
d0c422cb | 1265 | op->op0 = ptr; |
a03a9774 RG |
1266 | } |
1267 | ||
807e902e | 1268 | mem_op->op0 = wide_int_to_tree (TREE_TYPE (mem_op->op0), off); |
9541ffee | 1269 | if (tree_fits_shwi_p (mem_op->op0)) |
eb1ce453 | 1270 | mem_op->off = tree_to_shwi (mem_op->op0); |
a03a9774 RG |
1271 | else |
1272 | mem_op->off = -1; | |
1273 | if (TREE_CODE (op->op0) == SSA_NAME) | |
e093ffe3 RG |
1274 | op->op0 = SSA_VAL (op->op0); |
1275 | if (TREE_CODE (op->op0) != SSA_NAME) | |
1276 | op->opcode = TREE_CODE (op->op0); | |
a03a9774 RG |
1277 | |
1278 | /* And recurse. */ | |
1279 | if (TREE_CODE (op->op0) == SSA_NAME) | |
1280 | vn_reference_maybe_forwprop_address (ops, i_p); | |
1281 | else if (TREE_CODE (op->op0) == ADDR_EXPR) | |
1282 | vn_reference_fold_indirect (ops, i_p); | |
a0f79fcc | 1283 | return true; |
a03a9774 RG |
1284 | } |
1285 | ||
12bd5a1e RG |
1286 | /* Optimize the reference REF to a constant if possible or return |
1287 | NULL_TREE if not. */ | |
1288 | ||
1289 | tree | |
1290 | fully_constant_vn_reference_p (vn_reference_t ref) | |
1291 | { | |
9771b263 | 1292 | vec<vn_reference_op_s> operands = ref->operands; |
12bd5a1e RG |
1293 | vn_reference_op_t op; |
1294 | ||
1295 | /* Try to simplify the translated expression if it is | |
1296 | a call to a builtin function with at most two arguments. */ | |
9771b263 | 1297 | op = &operands[0]; |
12bd5a1e RG |
1298 | if (op->opcode == CALL_EXPR |
1299 | && TREE_CODE (op->op0) == ADDR_EXPR | |
1300 | && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL | |
1301 | && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0)) | |
9771b263 DN |
1302 | && operands.length () >= 2 |
1303 | && operands.length () <= 3) | |
12bd5a1e RG |
1304 | { |
1305 | vn_reference_op_t arg0, arg1 = NULL; | |
1306 | bool anyconst = false; | |
9771b263 DN |
1307 | arg0 = &operands[1]; |
1308 | if (operands.length () > 2) | |
1309 | arg1 = &operands[2]; | |
12bd5a1e RG |
1310 | if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant |
1311 | || (arg0->opcode == ADDR_EXPR | |
1312 | && is_gimple_min_invariant (arg0->op0))) | |
1313 | anyconst = true; | |
1314 | if (arg1 | |
1315 | && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant | |
1316 | || (arg1->opcode == ADDR_EXPR | |
1317 | && is_gimple_min_invariant (arg1->op0)))) | |
1318 | anyconst = true; | |
1319 | if (anyconst) | |
1320 | { | |
1321 | tree folded = build_call_expr (TREE_OPERAND (op->op0, 0), | |
1322 | arg1 ? 2 : 1, | |
1323 | arg0->op0, | |
1324 | arg1 ? arg1->op0 : NULL); | |
1325 | if (folded | |
1326 | && TREE_CODE (folded) == NOP_EXPR) | |
1327 | folded = TREE_OPERAND (folded, 0); | |
1328 | if (folded | |
1329 | && is_gimple_min_invariant (folded)) | |
1330 | return folded; | |
1331 | } | |
1332 | } | |
1333 | ||
8403c2cf RB |
1334 | /* Simplify reads from constants or constant initializers. */ |
1335 | else if (BITS_PER_UNIT == 8 | |
1336 | && is_gimple_reg_type (ref->type) | |
1337 | && (!INTEGRAL_TYPE_P (ref->type) | |
1338 | || TYPE_PRECISION (ref->type) % BITS_PER_UNIT == 0)) | |
12bd5a1e | 1339 | { |
8403c2cf | 1340 | HOST_WIDE_INT off = 0; |
552b2afe JJ |
1341 | HOST_WIDE_INT size; |
1342 | if (INTEGRAL_TYPE_P (ref->type)) | |
1343 | size = TYPE_PRECISION (ref->type); | |
1344 | else | |
1345 | size = tree_to_shwi (TYPE_SIZE (ref->type)); | |
8403c2cf RB |
1346 | if (size % BITS_PER_UNIT != 0 |
1347 | || size > MAX_BITSIZE_MODE_ANY_MODE) | |
1348 | return NULL_TREE; | |
1349 | size /= BITS_PER_UNIT; | |
1350 | unsigned i; | |
1351 | for (i = 0; i < operands.length (); ++i) | |
1352 | { | |
13e88953 RB |
1353 | if (TREE_CODE_CLASS (operands[i].opcode) == tcc_constant) |
1354 | { | |
1355 | ++i; | |
1356 | break; | |
1357 | } | |
8403c2cf RB |
1358 | if (operands[i].off == -1) |
1359 | return NULL_TREE; | |
1360 | off += operands[i].off; | |
1361 | if (operands[i].opcode == MEM_REF) | |
1362 | { | |
1363 | ++i; | |
1364 | break; | |
1365 | } | |
1366 | } | |
1367 | vn_reference_op_t base = &operands[--i]; | |
1368 | tree ctor = error_mark_node; | |
1369 | tree decl = NULL_TREE; | |
1370 | if (TREE_CODE_CLASS (base->opcode) == tcc_constant) | |
1371 | ctor = base->op0; | |
1372 | else if (base->opcode == MEM_REF | |
1373 | && base[1].opcode == ADDR_EXPR | |
1374 | && (TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == VAR_DECL | |
1375 | || TREE_CODE (TREE_OPERAND (base[1].op0, 0)) == CONST_DECL)) | |
1376 | { | |
1377 | decl = TREE_OPERAND (base[1].op0, 0); | |
1378 | ctor = ctor_for_folding (decl); | |
1379 | } | |
1380 | if (ctor == NULL_TREE) | |
1381 | return build_zero_cst (ref->type); | |
1382 | else if (ctor != error_mark_node) | |
1383 | { | |
1384 | if (decl) | |
1385 | { | |
1386 | tree res = fold_ctor_reference (ref->type, ctor, | |
1387 | off * BITS_PER_UNIT, | |
1388 | size * BITS_PER_UNIT, decl); | |
1389 | if (res) | |
1390 | { | |
1391 | STRIP_USELESS_TYPE_CONVERSION (res); | |
1392 | if (is_gimple_min_invariant (res)) | |
1393 | return res; | |
1394 | } | |
1395 | } | |
1396 | else | |
1397 | { | |
1398 | unsigned char buf[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT]; | |
1ff0a84c JJ |
1399 | int len = native_encode_expr (ctor, buf, size, off); |
1400 | if (len > 0) | |
1401 | return native_interpret_expr (ref->type, buf, len); | |
8403c2cf RB |
1402 | } |
1403 | } | |
12bd5a1e RG |
1404 | } |
1405 | ||
1406 | return NULL_TREE; | |
1407 | } | |
1408 | ||
ee45a32d EB |
1409 | /* Return true if OPS contain a storage order barrier. */ |
1410 | ||
1411 | static bool | |
1412 | contains_storage_order_barrier_p (vec<vn_reference_op_s> ops) | |
1413 | { | |
1414 | vn_reference_op_t op; | |
1415 | unsigned i; | |
1416 | ||
1417 | FOR_EACH_VEC_ELT (ops, i, op) | |
1418 | if (op->opcode == VIEW_CONVERT_EXPR && op->reverse) | |
1419 | return true; | |
1420 | ||
1421 | return false; | |
1422 | } | |
1423 | ||
89fb70a3 DB |
1424 | /* Transform any SSA_NAME's in a vector of vn_reference_op_s |
1425 | structures into their value numbers. This is done in-place, and | |
3ceaf2f5 RG |
1426 | the vector passed in is returned. *VALUEIZED_ANYTHING will specify |
1427 | whether any operands were valueized. */ | |
89fb70a3 | 1428 | |
9771b263 DN |
1429 | static vec<vn_reference_op_s> |
1430 | valueize_refs_1 (vec<vn_reference_op_s> orig, bool *valueized_anything) | |
89fb70a3 DB |
1431 | { |
1432 | vn_reference_op_t vro; | |
aa7069aa | 1433 | unsigned int i; |
89fb70a3 | 1434 | |
3ceaf2f5 RG |
1435 | *valueized_anything = false; |
1436 | ||
9771b263 | 1437 | FOR_EACH_VEC_ELT (orig, i, vro) |
89fb70a3 DB |
1438 | { |
1439 | if (vro->opcode == SSA_NAME | |
1440 | || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME)) | |
c9145754 | 1441 | { |
3ceaf2f5 RG |
1442 | tree tem = SSA_VAL (vro->op0); |
1443 | if (tem != vro->op0) | |
1444 | { | |
1445 | *valueized_anything = true; | |
1446 | vro->op0 = tem; | |
1447 | } | |
c9145754 DB |
1448 | /* If it transforms from an SSA_NAME to a constant, update |
1449 | the opcode. */ | |
1450 | if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME) | |
1451 | vro->opcode = TREE_CODE (vro->op0); | |
1452 | } | |
aa7069aa | 1453 | if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME) |
3ceaf2f5 RG |
1454 | { |
1455 | tree tem = SSA_VAL (vro->op1); | |
1456 | if (tem != vro->op1) | |
1457 | { | |
1458 | *valueized_anything = true; | |
1459 | vro->op1 = tem; | |
1460 | } | |
1461 | } | |
aa7069aa | 1462 | if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME) |
3ceaf2f5 RG |
1463 | { |
1464 | tree tem = SSA_VAL (vro->op2); | |
1465 | if (tem != vro->op2) | |
1466 | { | |
1467 | *valueized_anything = true; | |
1468 | vro->op2 = tem; | |
1469 | } | |
1470 | } | |
70f34814 RG |
1471 | /* If it transforms from an SSA_NAME to an address, fold with |
1472 | a preceding indirect reference. */ | |
1473 | if (i > 0 | |
1474 | && vro->op0 | |
1475 | && TREE_CODE (vro->op0) == ADDR_EXPR | |
9771b263 | 1476 | && orig[i - 1].opcode == MEM_REF) |
a0f79fcc RB |
1477 | { |
1478 | if (vn_reference_fold_indirect (&orig, &i)) | |
1479 | *valueized_anything = true; | |
1480 | } | |
a03a9774 RG |
1481 | else if (i > 0 |
1482 | && vro->opcode == SSA_NAME | |
9771b263 | 1483 | && orig[i - 1].opcode == MEM_REF) |
a0f79fcc RB |
1484 | { |
1485 | if (vn_reference_maybe_forwprop_address (&orig, &i)) | |
1486 | *valueized_anything = true; | |
1487 | } | |
70f34814 RG |
1488 | /* If it transforms a non-constant ARRAY_REF into a constant |
1489 | one, adjust the constant offset. */ | |
1490 | else if (vro->opcode == ARRAY_REF | |
1491 | && vro->off == -1 | |
1492 | && TREE_CODE (vro->op0) == INTEGER_CST | |
1493 | && TREE_CODE (vro->op1) == INTEGER_CST | |
1494 | && TREE_CODE (vro->op2) == INTEGER_CST) | |
1495 | { | |
807e902e KZ |
1496 | offset_int off = ((wi::to_offset (vro->op0) |
1497 | - wi::to_offset (vro->op1)) | |
cef5388d RB |
1498 | * wi::to_offset (vro->op2) |
1499 | * vn_ref_op_align_unit (vro)); | |
807e902e KZ |
1500 | if (wi::fits_shwi_p (off)) |
1501 | vro->off = off.to_shwi (); | |
70f34814 | 1502 | } |
89fb70a3 DB |
1503 | } |
1504 | ||
1505 | return orig; | |
1506 | } | |
1507 | ||
9771b263 DN |
1508 | static vec<vn_reference_op_s> |
1509 | valueize_refs (vec<vn_reference_op_s> orig) | |
3ceaf2f5 RG |
1510 | { |
1511 | bool tem; | |
1512 | return valueize_refs_1 (orig, &tem); | |
1513 | } | |
1514 | ||
9771b263 | 1515 | static vec<vn_reference_op_s> shared_lookup_references; |
aa7069aa RG |
1516 | |
1517 | /* Create a vector of vn_reference_op_s structures from REF, a | |
1518 | REFERENCE_CLASS_P tree. The vector is shared among all callers of | |
3ceaf2f5 RG |
1519 | this function. *VALUEIZED_ANYTHING will specify whether any |
1520 | operands were valueized. */ | |
aa7069aa | 1521 | |
9771b263 | 1522 | static vec<vn_reference_op_s> |
3ceaf2f5 | 1523 | valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything) |
aa7069aa RG |
1524 | { |
1525 | if (!ref) | |
6e1aa848 | 1526 | return vNULL; |
9771b263 | 1527 | shared_lookup_references.truncate (0); |
aa7069aa | 1528 | copy_reference_ops_from_ref (ref, &shared_lookup_references); |
3ceaf2f5 RG |
1529 | shared_lookup_references = valueize_refs_1 (shared_lookup_references, |
1530 | valueized_anything); | |
aa7069aa RG |
1531 | return shared_lookup_references; |
1532 | } | |
1533 | ||
1534 | /* Create a vector of vn_reference_op_s structures from CALL, a | |
1535 | call statement. The vector is shared among all callers of | |
1536 | this function. */ | |
1537 | ||
9771b263 | 1538 | static vec<vn_reference_op_s> |
538dd0b7 | 1539 | valueize_shared_reference_ops_from_call (gcall *call) |
aa7069aa RG |
1540 | { |
1541 | if (!call) | |
6e1aa848 | 1542 | return vNULL; |
9771b263 | 1543 | shared_lookup_references.truncate (0); |
aa7069aa RG |
1544 | copy_reference_ops_from_call (call, &shared_lookup_references); |
1545 | shared_lookup_references = valueize_refs (shared_lookup_references); | |
1546 | return shared_lookup_references; | |
1547 | } | |
1548 | ||
896c8b96 RG |
1549 | /* Lookup a SCCVN reference operation VR in the current hash table. |
1550 | Returns the resulting value number if it exists in the hash table, | |
c9145754 DB |
1551 | NULL_TREE otherwise. VNRESULT will be filled in with the actual |
1552 | vn_reference_t stored in the hashtable if something is found. */ | |
896c8b96 RG |
1553 | |
1554 | static tree | |
c9145754 | 1555 | vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult) |
896c8b96 | 1556 | { |
bf190e8d | 1557 | vn_reference_s **slot; |
896c8b96 RG |
1558 | hashval_t hash; |
1559 | ||
1560 | hash = vr->hashcode; | |
c203e8a7 | 1561 | slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT); |
896c8b96 | 1562 | if (!slot && current_info == optimistic_info) |
c203e8a7 | 1563 | slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT); |
896c8b96 | 1564 | if (slot) |
c9145754 DB |
1565 | { |
1566 | if (vnresult) | |
1567 | *vnresult = (vn_reference_t)*slot; | |
1568 | return ((vn_reference_t)*slot)->result; | |
1569 | } | |
b8698a0f | 1570 | |
896c8b96 RG |
1571 | return NULL_TREE; |
1572 | } | |
1573 | ||
5006671f RG |
1574 | /* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_ |
1575 | with the current VUSE and performs the expression lookup. */ | |
1576 | ||
1577 | static void * | |
9bb06c2a RG |
1578 | vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, |
1579 | unsigned int cnt, void *vr_) | |
5006671f RG |
1580 | { |
1581 | vn_reference_t vr = (vn_reference_t)vr_; | |
bf190e8d | 1582 | vn_reference_s **slot; |
5006671f RG |
1583 | hashval_t hash; |
1584 | ||
9bb06c2a RG |
1585 | /* This bounds the stmt walks we perform on reference lookups |
1586 | to O(1) instead of O(N) where N is the number of dominating | |
1587 | stores. */ | |
1588 | if (cnt > (unsigned) PARAM_VALUE (PARAM_SCCVN_MAX_ALIAS_QUERIES_PER_ACCESS)) | |
1589 | return (void *)-1; | |
1590 | ||
d0ca0bcb RG |
1591 | if (last_vuse_ptr) |
1592 | *last_vuse_ptr = vuse; | |
1593 | ||
5006671f | 1594 | /* Fixup vuse and hash. */ |
9708c51d RG |
1595 | if (vr->vuse) |
1596 | vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse); | |
d1c0308e | 1597 | vr->vuse = vuse_ssa_val (vuse); |
9708c51d RG |
1598 | if (vr->vuse) |
1599 | vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse); | |
5006671f RG |
1600 | |
1601 | hash = vr->hashcode; | |
c203e8a7 | 1602 | slot = current_info->references->find_slot_with_hash (vr, hash, NO_INSERT); |
5006671f | 1603 | if (!slot && current_info == optimistic_info) |
c203e8a7 | 1604 | slot = valid_info->references->find_slot_with_hash (vr, hash, NO_INSERT); |
5006671f RG |
1605 | if (slot) |
1606 | return *slot; | |
b8698a0f | 1607 | |
5006671f RG |
1608 | return NULL; |
1609 | } | |
c9145754 | 1610 | |
b55eb410 RG |
1611 | /* Lookup an existing or insert a new vn_reference entry into the |
1612 | value table for the VUSE, SET, TYPE, OPERANDS reference which | |
9179ed9d | 1613 | has the value VALUE which is either a constant or an SSA name. */ |
b55eb410 RG |
1614 | |
1615 | static vn_reference_t | |
9179ed9d RG |
1616 | vn_reference_lookup_or_insert_for_pieces (tree vuse, |
1617 | alias_set_type set, | |
1618 | tree type, | |
9771b263 DN |
1619 | vec<vn_reference_op_s, |
1620 | va_heap> operands, | |
9179ed9d | 1621 | tree value) |
b55eb410 | 1622 | { |
703c9ccd | 1623 | vn_reference_s vr1; |
b55eb410 | 1624 | vn_reference_t result; |
9179ed9d | 1625 | unsigned value_id; |
b55eb410 RG |
1626 | vr1.vuse = vuse; |
1627 | vr1.operands = operands; | |
1628 | vr1.type = type; | |
1629 | vr1.set = set; | |
1630 | vr1.hashcode = vn_reference_compute_hash (&vr1); | |
1631 | if (vn_reference_lookup_1 (&vr1, &result)) | |
1632 | return result; | |
9179ed9d RG |
1633 | if (TREE_CODE (value) == SSA_NAME) |
1634 | value_id = VN_INFO (value)->value_id; | |
1635 | else | |
1636 | value_id = get_or_alloc_constant_value_id (value); | |
b55eb410 | 1637 | return vn_reference_insert_pieces (vuse, set, type, |
9771b263 | 1638 | operands.copy (), value, value_id); |
b55eb410 RG |
1639 | } |
1640 | ||
64ea4e15 RB |
1641 | static vn_nary_op_t vn_nary_op_insert_stmt (gimple *stmt, tree result); |
1642 | ||
1643 | /* Hook for maybe_push_res_to_seq, lookup the expression in the VN tables. */ | |
1644 | ||
1645 | static tree | |
1646 | vn_lookup_simplify_result (code_helper rcode, tree type, tree *ops) | |
1647 | { | |
1648 | if (!rcode.is_tree_code ()) | |
1649 | return NULL_TREE; | |
1650 | vn_nary_op_t vnresult = NULL; | |
1651 | return vn_nary_op_lookup_pieces (TREE_CODE_LENGTH ((tree_code) rcode), | |
1652 | (tree_code) rcode, type, ops, &vnresult); | |
1653 | } | |
1654 | ||
1655 | /* Return a value-number for RCODE OPS... either by looking up an existing | |
351168fe RB |
1656 | value-number for the simplified result or by inserting the operation if |
1657 | INSERT is true. */ | |
64ea4e15 RB |
1658 | |
1659 | static tree | |
351168fe RB |
1660 | vn_nary_build_or_lookup_1 (code_helper rcode, tree type, tree *ops, |
1661 | bool insert) | |
64ea4e15 RB |
1662 | { |
1663 | tree result = NULL_TREE; | |
1664 | /* We will be creating a value number for | |
1ef33ef3 | 1665 | RCODE (OPS...). |
64ea4e15 RB |
1666 | So first simplify and lookup this expression to see if it |
1667 | is already available. */ | |
1668 | mprts_hook = vn_lookup_simplify_result; | |
1669 | bool res = false; | |
1670 | switch (TREE_CODE_LENGTH ((tree_code) rcode)) | |
1671 | { | |
1672 | case 1: | |
1673 | res = gimple_resimplify1 (NULL, &rcode, type, ops, vn_valueize); | |
1674 | break; | |
1675 | case 2: | |
1676 | res = gimple_resimplify2 (NULL, &rcode, type, ops, vn_valueize); | |
1677 | break; | |
1678 | case 3: | |
1679 | res = gimple_resimplify3 (NULL, &rcode, type, ops, vn_valueize); | |
1680 | break; | |
1681 | } | |
1682 | mprts_hook = NULL; | |
1683 | gimple *new_stmt = NULL; | |
1684 | if (res | |
1685 | && gimple_simplified_result_is_gimple_val (rcode, ops)) | |
1686 | /* The expression is already available. */ | |
1687 | result = ops[0]; | |
1688 | else | |
1689 | { | |
1690 | tree val = vn_lookup_simplify_result (rcode, type, ops); | |
351168fe | 1691 | if (!val && insert) |
64ea4e15 RB |
1692 | { |
1693 | gimple_seq stmts = NULL; | |
1694 | result = maybe_push_res_to_seq (rcode, type, ops, &stmts); | |
1695 | if (result) | |
1696 | { | |
1697 | gcc_assert (gimple_seq_singleton_p (stmts)); | |
1698 | new_stmt = gimple_seq_first_stmt (stmts); | |
1699 | } | |
1700 | } | |
1701 | else | |
1702 | /* The expression is already available. */ | |
1703 | result = val; | |
1704 | } | |
1705 | if (new_stmt) | |
1706 | { | |
1707 | /* The expression is not yet available, value-number lhs to | |
1708 | the new SSA_NAME we created. */ | |
1709 | /* Initialize value-number information properly. */ | |
1710 | VN_INFO_GET (result)->valnum = result; | |
1711 | VN_INFO (result)->value_id = get_next_value_id (); | |
1712 | gimple_seq_add_stmt_without_update (&VN_INFO (result)->expr, | |
1713 | new_stmt); | |
1714 | VN_INFO (result)->needs_insertion = true; | |
1ef33ef3 RB |
1715 | /* ??? PRE phi-translation inserts NARYs without corresponding |
1716 | SSA name result. Re-use those but set their result according | |
1717 | to the stmt we just built. */ | |
1718 | vn_nary_op_t nary = NULL; | |
1719 | vn_nary_op_lookup_stmt (new_stmt, &nary); | |
1720 | if (nary) | |
1721 | { | |
1722 | gcc_assert (nary->result == NULL_TREE); | |
1723 | nary->result = gimple_assign_lhs (new_stmt); | |
1724 | } | |
64ea4e15 RB |
1725 | /* As all "inserted" statements are singleton SCCs, insert |
1726 | to the valid table. This is strictly needed to | |
1727 | avoid re-generating new value SSA_NAMEs for the same | |
1728 | expression during SCC iteration over and over (the | |
1729 | optimistic table gets cleared after each iteration). | |
1730 | We do not need to insert into the optimistic table, as | |
1731 | lookups there will fall back to the valid table. */ | |
1ef33ef3 | 1732 | else if (current_info == optimistic_info) |
64ea4e15 RB |
1733 | { |
1734 | current_info = valid_info; | |
1735 | vn_nary_op_insert_stmt (new_stmt, result); | |
1736 | current_info = optimistic_info; | |
1737 | } | |
1738 | else | |
1739 | vn_nary_op_insert_stmt (new_stmt, result); | |
1740 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1741 | { | |
1742 | fprintf (dump_file, "Inserting name "); | |
1743 | print_generic_expr (dump_file, result, 0); | |
1744 | fprintf (dump_file, " for expression "); | |
1745 | print_gimple_expr (dump_file, new_stmt, 0, TDF_SLIM); | |
1746 | fprintf (dump_file, "\n"); | |
1747 | } | |
1748 | } | |
1749 | return result; | |
1750 | } | |
1751 | ||
351168fe RB |
1752 | /* Return a value-number for RCODE OPS... either by looking up an existing |
1753 | value-number for the simplified result or by inserting the operation. */ | |
1754 | ||
1755 | static tree | |
1756 | vn_nary_build_or_lookup (code_helper rcode, tree type, tree *ops) | |
1757 | { | |
1758 | return vn_nary_build_or_lookup_1 (rcode, type, ops, true); | |
1759 | } | |
1760 | ||
1761 | /* Try to simplify the expression RCODE OPS... of type TYPE and return | |
1762 | its value if present. */ | |
1763 | ||
1764 | tree | |
1765 | vn_nary_simplify (vn_nary_op_t nary) | |
1766 | { | |
1767 | if (nary->length > 3) | |
1768 | return NULL_TREE; | |
1769 | tree ops[3]; | |
1770 | memcpy (ops, nary->op, sizeof (tree) * nary->length); | |
1771 | return vn_nary_build_or_lookup_1 (nary->opcode, nary->type, ops, false); | |
1772 | } | |
1773 | ||
1774 | ||
01df5c8a RG |
1775 | /* Callback for walk_non_aliased_vuses. Tries to perform a lookup |
1776 | from the statement defining VUSE and if not successful tries to | |
073a8998 | 1777 | translate *REFP and VR_ through an aggregate copy at the definition |
e7cbc096 RB |
1778 | of VUSE. If *DISAMBIGUATE_ONLY is true then do not perform translation |
1779 | of *REF and *VR. If only disambiguation was performed then | |
1780 | *DISAMBIGUATE_ONLY is set to true. */ | |
01df5c8a RG |
1781 | |
1782 | static void * | |
50f0aa20 | 1783 | vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_, |
e7cbc096 | 1784 | bool *disambiguate_only) |
01df5c8a RG |
1785 | { |
1786 | vn_reference_t vr = (vn_reference_t)vr_; | |
355fe088 | 1787 | gimple *def_stmt = SSA_NAME_DEF_STMT (vuse); |
e7cbc096 | 1788 | tree base = ao_ref_base (ref); |
0f900dfa | 1789 | HOST_WIDE_INT offset, maxsize; |
199d1d48 | 1790 | static vec<vn_reference_op_s> lhs_ops; |
8ea34dab RG |
1791 | ao_ref lhs_ref; |
1792 | bool lhs_ref_ok = false; | |
01df5c8a | 1793 | |
e7cbc096 RB |
1794 | /* If the reference is based on a parameter that was determined as |
1795 | pointing to readonly memory it doesn't change. */ | |
1796 | if (TREE_CODE (base) == MEM_REF | |
1797 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME | |
1798 | && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)) | |
1799 | && bitmap_bit_p (const_parms, | |
1800 | SSA_NAME_VERSION (TREE_OPERAND (base, 0)))) | |
1801 | { | |
1802 | *disambiguate_only = true; | |
1803 | return NULL; | |
1804 | } | |
1805 | ||
4fa4929e RG |
1806 | /* First try to disambiguate after value-replacing in the definitions LHS. */ |
1807 | if (is_gimple_assign (def_stmt)) | |
1808 | { | |
1809 | tree lhs = gimple_assign_lhs (def_stmt); | |
25aa059e | 1810 | bool valueized_anything = false; |
8ea34dab | 1811 | /* Avoid re-allocation overhead. */ |
9771b263 | 1812 | lhs_ops.truncate (0); |
8ea34dab | 1813 | copy_reference_ops_from_ref (lhs, &lhs_ops); |
25aa059e | 1814 | lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything); |
25aa059e RG |
1815 | if (valueized_anything) |
1816 | { | |
1817 | lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref, | |
1818 | get_alias_set (lhs), | |
1819 | TREE_TYPE (lhs), lhs_ops); | |
1820 | if (lhs_ref_ok | |
1821 | && !refs_may_alias_p_1 (ref, &lhs_ref, true)) | |
e7cbc096 RB |
1822 | { |
1823 | *disambiguate_only = true; | |
1824 | return NULL; | |
1825 | } | |
25aa059e RG |
1826 | } |
1827 | else | |
1828 | { | |
1829 | ao_ref_init (&lhs_ref, lhs); | |
1830 | lhs_ref_ok = true; | |
1831 | } | |
4fa4929e | 1832 | } |
50f0aa20 RB |
1833 | else if (gimple_call_builtin_p (def_stmt, BUILT_IN_NORMAL) |
1834 | && gimple_call_num_args (def_stmt) <= 4) | |
1835 | { | |
1836 | /* For builtin calls valueize its arguments and call the | |
1837 | alias oracle again. Valueization may improve points-to | |
1838 | info of pointers and constify size and position arguments. | |
1839 | Originally this was motivated by PR61034 which has | |
1840 | conditional calls to free falsely clobbering ref because | |
1841 | of imprecise points-to info of the argument. */ | |
1842 | tree oldargs[4]; | |
61dd7fbc | 1843 | bool valueized_anything = false; |
50f0aa20 RB |
1844 | for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i) |
1845 | { | |
1846 | oldargs[i] = gimple_call_arg (def_stmt, i); | |
1847 | if (TREE_CODE (oldargs[i]) == SSA_NAME | |
1848 | && VN_INFO (oldargs[i])->valnum != oldargs[i]) | |
1849 | { | |
1850 | gimple_call_set_arg (def_stmt, i, VN_INFO (oldargs[i])->valnum); | |
1851 | valueized_anything = true; | |
1852 | } | |
1853 | } | |
1854 | if (valueized_anything) | |
1855 | { | |
538dd0b7 DM |
1856 | bool res = call_may_clobber_ref_p_1 (as_a <gcall *> (def_stmt), |
1857 | ref); | |
50f0aa20 RB |
1858 | for (unsigned i = 0; i < gimple_call_num_args (def_stmt); ++i) |
1859 | gimple_call_set_arg (def_stmt, i, oldargs[i]); | |
1860 | if (!res) | |
e7cbc096 RB |
1861 | { |
1862 | *disambiguate_only = true; | |
1863 | return NULL; | |
1864 | } | |
50f0aa20 RB |
1865 | } |
1866 | } | |
1867 | ||
e7cbc096 | 1868 | if (*disambiguate_only) |
50f0aa20 | 1869 | return (void *)-1; |
4fa4929e | 1870 | |
b45d2719 | 1871 | offset = ref->offset; |
b45d2719 | 1872 | maxsize = ref->max_size; |
01df5c8a RG |
1873 | |
1874 | /* If we cannot constrain the size of the reference we cannot | |
1875 | test if anything kills it. */ | |
1876 | if (maxsize == -1) | |
1877 | return (void *)-1; | |
1878 | ||
47598145 MM |
1879 | /* We can't deduce anything useful from clobbers. */ |
1880 | if (gimple_clobber_p (def_stmt)) | |
1881 | return (void *)-1; | |
1882 | ||
01df5c8a | 1883 | /* def_stmt may-defs *ref. See if we can derive a value for *ref |
47598145 | 1884 | from that definition. |
01df5c8a | 1885 | 1) Memset. */ |
b45d2719 | 1886 | if (is_gimple_reg_type (vr->type) |
c7ee7b45 | 1887 | && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET) |
01df5c8a | 1888 | && integer_zerop (gimple_call_arg (def_stmt, 1)) |
cc269bb6 | 1889 | && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2)) |
01df5c8a RG |
1890 | && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR) |
1891 | { | |
1892 | tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0); | |
1893 | tree base2; | |
1894 | HOST_WIDE_INT offset2, size2, maxsize2; | |
ee45a32d EB |
1895 | bool reverse; |
1896 | base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2, | |
1897 | &reverse); | |
eb1ce453 | 1898 | size2 = tree_to_uhwi (gimple_call_arg (def_stmt, 2)) * 8; |
01df5c8a | 1899 | if ((unsigned HOST_WIDE_INT)size2 / 8 |
eb1ce453 | 1900 | == tree_to_uhwi (gimple_call_arg (def_stmt, 2)) |
9c8cbc74 | 1901 | && maxsize2 != -1 |
01df5c8a RG |
1902 | && operand_equal_p (base, base2, 0) |
1903 | && offset2 <= offset | |
1904 | && offset2 + size2 >= offset + maxsize) | |
b45d2719 | 1905 | { |
e8160c9a | 1906 | tree val = build_zero_cst (vr->type); |
9179ed9d | 1907 | return vn_reference_lookup_or_insert_for_pieces |
b55eb410 | 1908 | (vuse, vr->set, vr->type, vr->operands, val); |
b45d2719 | 1909 | } |
01df5c8a RG |
1910 | } |
1911 | ||
1912 | /* 2) Assignment from an empty CONSTRUCTOR. */ | |
b45d2719 | 1913 | else if (is_gimple_reg_type (vr->type) |
01df5c8a RG |
1914 | && gimple_assign_single_p (def_stmt) |
1915 | && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR | |
1916 | && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0) | |
1917 | { | |
1918 | tree base2; | |
1919 | HOST_WIDE_INT offset2, size2, maxsize2; | |
ee45a32d | 1920 | bool reverse; |
01df5c8a | 1921 | base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt), |
ee45a32d | 1922 | &offset2, &size2, &maxsize2, &reverse); |
9c8cbc74 EB |
1923 | if (maxsize2 != -1 |
1924 | && operand_equal_p (base, base2, 0) | |
01df5c8a RG |
1925 | && offset2 <= offset |
1926 | && offset2 + size2 >= offset + maxsize) | |
b45d2719 | 1927 | { |
e8160c9a | 1928 | tree val = build_zero_cst (vr->type); |
9179ed9d | 1929 | return vn_reference_lookup_or_insert_for_pieces |
b55eb410 | 1930 | (vuse, vr->set, vr->type, vr->operands, val); |
b45d2719 | 1931 | } |
01df5c8a RG |
1932 | } |
1933 | ||
c867aba0 RG |
1934 | /* 3) Assignment from a constant. We can use folds native encode/interpret |
1935 | routines to extract the assigned bits. */ | |
64ea4e15 | 1936 | else if (ref->size == maxsize |
c867aba0 | 1937 | && is_gimple_reg_type (vr->type) |
ee45a32d | 1938 | && !contains_storage_order_barrier_p (vr->operands) |
c867aba0 | 1939 | && gimple_assign_single_p (def_stmt) |
64ea4e15 RB |
1940 | && CHAR_BIT == 8 && BITS_PER_UNIT == 8 |
1941 | && maxsize % BITS_PER_UNIT == 0 | |
1942 | && offset % BITS_PER_UNIT == 0 | |
1943 | && (is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)) | |
1944 | || (TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME | |
1945 | && is_gimple_min_invariant (SSA_VAL (gimple_assign_rhs1 (def_stmt)))))) | |
c867aba0 RG |
1946 | { |
1947 | tree base2; | |
1948 | HOST_WIDE_INT offset2, size2, maxsize2; | |
ee45a32d | 1949 | bool reverse; |
c867aba0 | 1950 | base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt), |
ee45a32d EB |
1951 | &offset2, &size2, &maxsize2, &reverse); |
1952 | if (!reverse | |
1953 | && maxsize2 != -1 | |
c867aba0 RG |
1954 | && maxsize2 == size2 |
1955 | && size2 % BITS_PER_UNIT == 0 | |
1956 | && offset2 % BITS_PER_UNIT == 0 | |
1957 | && operand_equal_p (base, base2, 0) | |
1958 | && offset2 <= offset | |
1959 | && offset2 + size2 >= offset + maxsize) | |
1960 | { | |
1961 | /* We support up to 512-bit values (for V8DFmode). */ | |
1962 | unsigned char buffer[64]; | |
1963 | int len; | |
1964 | ||
64ea4e15 RB |
1965 | tree rhs = gimple_assign_rhs1 (def_stmt); |
1966 | if (TREE_CODE (rhs) == SSA_NAME) | |
1967 | rhs = SSA_VAL (rhs); | |
c867aba0 RG |
1968 | len = native_encode_expr (gimple_assign_rhs1 (def_stmt), |
1969 | buffer, sizeof (buffer)); | |
1970 | if (len > 0) | |
1971 | { | |
f35ea97d RB |
1972 | tree type = vr->type; |
1973 | /* Make sure to interpret in a type that has a range | |
1974 | covering the whole access size. */ | |
1975 | if (INTEGRAL_TYPE_P (vr->type) | |
1976 | && ref->size != TYPE_PRECISION (vr->type)) | |
1977 | type = build_nonstandard_integer_type (ref->size, | |
1978 | TYPE_UNSIGNED (type)); | |
1979 | tree val = native_interpret_expr (type, | |
c867aba0 RG |
1980 | buffer |
1981 | + ((offset - offset2) | |
1982 | / BITS_PER_UNIT), | |
1983 | ref->size / BITS_PER_UNIT); | |
f35ea97d RB |
1984 | /* If we chop off bits because the types precision doesn't |
1985 | match the memory access size this is ok when optimizing | |
1986 | reads but not when called from the DSE code during | |
1987 | elimination. */ | |
1988 | if (val | |
1989 | && type != vr->type) | |
1990 | { | |
1991 | if (! int_fits_type_p (val, vr->type)) | |
1992 | val = NULL_TREE; | |
1993 | else | |
1994 | val = fold_convert (vr->type, val); | |
1995 | } | |
1996 | ||
c867aba0 | 1997 | if (val) |
9179ed9d | 1998 | return vn_reference_lookup_or_insert_for_pieces |
f35ea97d | 1999 | (vuse, vr->set, vr->type, vr->operands, val); |
c867aba0 RG |
2000 | } |
2001 | } | |
2002 | } | |
2003 | ||
0147184e RG |
2004 | /* 4) Assignment from an SSA name which definition we may be able |
2005 | to access pieces from. */ | |
2006 | else if (ref->size == maxsize | |
2007 | && is_gimple_reg_type (vr->type) | |
ee45a32d | 2008 | && !contains_storage_order_barrier_p (vr->operands) |
0147184e RG |
2009 | && gimple_assign_single_p (def_stmt) |
2010 | && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME) | |
2011 | { | |
64ea4e15 RB |
2012 | tree base2; |
2013 | HOST_WIDE_INT offset2, size2, maxsize2; | |
2014 | bool reverse; | |
2015 | base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt), | |
2016 | &offset2, &size2, &maxsize2, | |
2017 | &reverse); | |
2018 | if (!reverse | |
2019 | && maxsize2 != -1 | |
2020 | && maxsize2 == size2 | |
2021 | && operand_equal_p (base, base2, 0) | |
2022 | && offset2 <= offset | |
2023 | && offset2 + size2 >= offset + maxsize | |
2024 | /* ??? We can't handle bitfield precision extracts without | |
2025 | either using an alternate type for the BIT_FIELD_REF and | |
2026 | then doing a conversion or possibly adjusting the offset | |
bd2c6270 | 2027 | according to endianness. */ |
64ea4e15 RB |
2028 | && (! INTEGRAL_TYPE_P (vr->type) |
2029 | || ref->size == TYPE_PRECISION (vr->type)) | |
2030 | && ref->size % BITS_PER_UNIT == 0) | |
0147184e | 2031 | { |
64ea4e15 RB |
2032 | code_helper rcode = BIT_FIELD_REF; |
2033 | tree ops[3]; | |
2034 | ops[0] = SSA_VAL (gimple_assign_rhs1 (def_stmt)); | |
2035 | ops[1] = bitsize_int (ref->size); | |
2036 | ops[2] = bitsize_int (offset - offset2); | |
2037 | tree val = vn_nary_build_or_lookup (rcode, vr->type, ops); | |
2038 | if (val) | |
0147184e | 2039 | { |
64ea4e15 RB |
2040 | vn_reference_t res = vn_reference_lookup_or_insert_for_pieces |
2041 | (vuse, vr->set, vr->type, vr->operands, val); | |
2042 | return res; | |
0147184e RG |
2043 | } |
2044 | } | |
2045 | } | |
2046 | ||
2047 | /* 5) For aggregate copies translate the reference through them if | |
01df5c8a | 2048 | the copy kills ref. */ |
3bc27de7 RG |
2049 | else if (vn_walk_kind == VN_WALKREWRITE |
2050 | && gimple_assign_single_p (def_stmt) | |
01df5c8a | 2051 | && (DECL_P (gimple_assign_rhs1 (def_stmt)) |
70f34814 | 2052 | || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF |
01df5c8a RG |
2053 | || handled_component_p (gimple_assign_rhs1 (def_stmt)))) |
2054 | { | |
2055 | tree base2; | |
a0f79fcc | 2056 | HOST_WIDE_INT maxsize2; |
ee45a32d | 2057 | int i, j, k; |
ef062b13 | 2058 | auto_vec<vn_reference_op_s> rhs; |
01df5c8a | 2059 | vn_reference_op_t vro; |
b45d2719 | 2060 | ao_ref r; |
01df5c8a | 2061 | |
8ea34dab RG |
2062 | if (!lhs_ref_ok) |
2063 | return (void *)-1; | |
2064 | ||
01df5c8a | 2065 | /* See if the assignment kills REF. */ |
8ea34dab | 2066 | base2 = ao_ref_base (&lhs_ref); |
9c8cbc74 EB |
2067 | maxsize2 = lhs_ref.max_size; |
2068 | if (maxsize2 == -1 | |
ea3eac3a RB |
2069 | || (base != base2 |
2070 | && (TREE_CODE (base) != MEM_REF | |
2071 | || TREE_CODE (base2) != MEM_REF | |
2072 | || TREE_OPERAND (base, 0) != TREE_OPERAND (base2, 0) | |
2073 | || !tree_int_cst_equal (TREE_OPERAND (base, 1), | |
2074 | TREE_OPERAND (base2, 1)))) | |
a0f79fcc | 2075 | || !stmt_kills_ref_p (def_stmt, ref)) |
01df5c8a RG |
2076 | return (void *)-1; |
2077 | ||
8ea34dab RG |
2078 | /* Find the common base of ref and the lhs. lhs_ops already |
2079 | contains valueized operands for the lhs. */ | |
9771b263 DN |
2080 | i = vr->operands.length () - 1; |
2081 | j = lhs_ops.length () - 1; | |
35ecd408 | 2082 | while (j >= 0 && i >= 0 |
9771b263 | 2083 | && vn_reference_op_eq (&vr->operands[i], &lhs_ops[j])) |
01df5c8a RG |
2084 | { |
2085 | i--; | |
2086 | j--; | |
2087 | } | |
35ecd408 | 2088 | |
25aa059e RG |
2089 | /* ??? The innermost op should always be a MEM_REF and we already |
2090 | checked that the assignment to the lhs kills vr. Thus for | |
2091 | aggregate copies using char[] types the vn_reference_op_eq | |
2092 | may fail when comparing types for compatibility. But we really | |
2093 | don't care here - further lookups with the rewritten operands | |
2094 | will simply fail if we messed up types too badly. */ | |
8403c2cf | 2095 | HOST_WIDE_INT extra_off = 0; |
4f9dbaaa | 2096 | if (j == 0 && i >= 0 |
9771b263 | 2097 | && lhs_ops[0].opcode == MEM_REF |
8403c2cf RB |
2098 | && lhs_ops[0].off != -1) |
2099 | { | |
2100 | if (lhs_ops[0].off == vr->operands[i].off) | |
2101 | i--, j--; | |
2102 | else if (vr->operands[i].opcode == MEM_REF | |
2103 | && vr->operands[i].off != -1) | |
2104 | { | |
2105 | extra_off = vr->operands[i].off - lhs_ops[0].off; | |
2106 | i--, j--; | |
2107 | } | |
2108 | } | |
25aa059e | 2109 | |
01df5c8a RG |
2110 | /* i now points to the first additional op. |
2111 | ??? LHS may not be completely contained in VR, one or more | |
2112 | VIEW_CONVERT_EXPRs could be in its way. We could at least | |
2113 | try handling outermost VIEW_CONVERT_EXPRs. */ | |
2114 | if (j != -1) | |
2115 | return (void *)-1; | |
01df5c8a | 2116 | |
ee45a32d EB |
2117 | /* Punt if the additional ops contain a storage order barrier. */ |
2118 | for (k = i; k >= 0; k--) | |
2119 | { | |
2120 | vro = &vr->operands[k]; | |
2121 | if (vro->opcode == VIEW_CONVERT_EXPR && vro->reverse) | |
2122 | return (void *)-1; | |
2123 | } | |
2124 | ||
01df5c8a RG |
2125 | /* Now re-write REF to be based on the rhs of the assignment. */ |
2126 | copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs); | |
8403c2cf RB |
2127 | |
2128 | /* Apply an extra offset to the inner MEM_REF of the RHS. */ | |
2129 | if (extra_off != 0) | |
2130 | { | |
2131 | if (rhs.length () < 2 | |
2132 | || rhs[0].opcode != MEM_REF | |
2133 | || rhs[0].off == -1) | |
2134 | return (void *)-1; | |
2135 | rhs[0].off += extra_off; | |
2136 | rhs[0].op0 = int_const_binop (PLUS_EXPR, rhs[0].op0, | |
2137 | build_int_cst (TREE_TYPE (rhs[0].op0), | |
2138 | extra_off)); | |
2139 | } | |
2140 | ||
01df5c8a | 2141 | /* We need to pre-pend vr->operands[0..i] to rhs. */ |
26f3a4e1 | 2142 | vec<vn_reference_op_s> old = vr->operands; |
9771b263 | 2143 | if (i + 1 + rhs.length () > vr->operands.length ()) |
ec67c62e | 2144 | vr->operands.safe_grow (i + 1 + rhs.length ()); |
01df5c8a | 2145 | else |
9771b263 DN |
2146 | vr->operands.truncate (i + 1 + rhs.length ()); |
2147 | FOR_EACH_VEC_ELT (rhs, j, vro) | |
2148 | vr->operands[i + 1 + j] = *vro; | |
b55eb410 | 2149 | vr->operands = valueize_refs (vr->operands); |
26f3a4e1 RB |
2150 | if (old == shared_lookup_references) |
2151 | shared_lookup_references = vr->operands; | |
01df5c8a | 2152 | vr->hashcode = vn_reference_compute_hash (vr); |
c7ee7b45 | 2153 | |
8403c2cf RB |
2154 | /* Try folding the new reference to a constant. */ |
2155 | tree val = fully_constant_vn_reference_p (vr); | |
2156 | if (val) | |
2157 | return vn_reference_lookup_or_insert_for_pieces | |
2158 | (vuse, vr->set, vr->type, vr->operands, val); | |
2159 | ||
c7ee7b45 RG |
2160 | /* Adjust *ref from the new operands. */ |
2161 | if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands)) | |
2162 | return (void *)-1; | |
2163 | /* This can happen with bitfields. */ | |
2164 | if (ref->size != r.size) | |
2165 | return (void *)-1; | |
2166 | *ref = r; | |
2167 | ||
2168 | /* Do not update last seen VUSE after translating. */ | |
2169 | last_vuse_ptr = NULL; | |
2170 | ||
2171 | /* Keep looking for the adjusted *REF / VR pair. */ | |
2172 | return NULL; | |
2173 | } | |
2174 | ||
0147184e | 2175 | /* 6) For memcpy copies translate the reference through them if |
c7ee7b45 RG |
2176 | the copy kills ref. */ |
2177 | else if (vn_walk_kind == VN_WALKREWRITE | |
2178 | && is_gimple_reg_type (vr->type) | |
2179 | /* ??? Handle BCOPY as well. */ | |
2180 | && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY) | |
2181 | || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY) | |
2182 | || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE)) | |
2183 | && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR | |
2184 | || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME) | |
2185 | && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR | |
2186 | || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME) | |
cc269bb6 | 2187 | && tree_fits_uhwi_p (gimple_call_arg (def_stmt, 2))) |
c7ee7b45 RG |
2188 | { |
2189 | tree lhs, rhs; | |
2190 | ao_ref r; | |
2191 | HOST_WIDE_INT rhs_offset, copy_size, lhs_offset; | |
2192 | vn_reference_op_s op; | |
2193 | HOST_WIDE_INT at; | |
2194 | ||
c7ee7b45 RG |
2195 | /* Only handle non-variable, addressable refs. */ |
2196 | if (ref->size != maxsize | |
2197 | || offset % BITS_PER_UNIT != 0 | |
2198 | || ref->size % BITS_PER_UNIT != 0) | |
2199 | return (void *)-1; | |
2200 | ||
2201 | /* Extract a pointer base and an offset for the destination. */ | |
2202 | lhs = gimple_call_arg (def_stmt, 0); | |
2203 | lhs_offset = 0; | |
2204 | if (TREE_CODE (lhs) == SSA_NAME) | |
e65757f3 RB |
2205 | { |
2206 | lhs = SSA_VAL (lhs); | |
2207 | if (TREE_CODE (lhs) == SSA_NAME) | |
2208 | { | |
355fe088 | 2209 | gimple *def_stmt = SSA_NAME_DEF_STMT (lhs); |
e65757f3 RB |
2210 | if (gimple_assign_single_p (def_stmt) |
2211 | && gimple_assign_rhs_code (def_stmt) == ADDR_EXPR) | |
2212 | lhs = gimple_assign_rhs1 (def_stmt); | |
2213 | } | |
2214 | } | |
c7ee7b45 RG |
2215 | if (TREE_CODE (lhs) == ADDR_EXPR) |
2216 | { | |
2217 | tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0), | |
2218 | &lhs_offset); | |
2219 | if (!tem) | |
2220 | return (void *)-1; | |
2221 | if (TREE_CODE (tem) == MEM_REF | |
cc269bb6 | 2222 | && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))) |
c7ee7b45 RG |
2223 | { |
2224 | lhs = TREE_OPERAND (tem, 0); | |
e65757f3 RB |
2225 | if (TREE_CODE (lhs) == SSA_NAME) |
2226 | lhs = SSA_VAL (lhs); | |
eb1ce453 | 2227 | lhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1)); |
c7ee7b45 RG |
2228 | } |
2229 | else if (DECL_P (tem)) | |
2230 | lhs = build_fold_addr_expr (tem); | |
2231 | else | |
2232 | return (void *)-1; | |
2233 | } | |
2234 | if (TREE_CODE (lhs) != SSA_NAME | |
2235 | && TREE_CODE (lhs) != ADDR_EXPR) | |
2236 | return (void *)-1; | |
2237 | ||
2238 | /* Extract a pointer base and an offset for the source. */ | |
2239 | rhs = gimple_call_arg (def_stmt, 1); | |
2240 | rhs_offset = 0; | |
2241 | if (TREE_CODE (rhs) == SSA_NAME) | |
2242 | rhs = SSA_VAL (rhs); | |
2243 | if (TREE_CODE (rhs) == ADDR_EXPR) | |
2244 | { | |
2245 | tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0), | |
2246 | &rhs_offset); | |
2247 | if (!tem) | |
2248 | return (void *)-1; | |
2249 | if (TREE_CODE (tem) == MEM_REF | |
cc269bb6 | 2250 | && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))) |
c7ee7b45 RG |
2251 | { |
2252 | rhs = TREE_OPERAND (tem, 0); | |
eb1ce453 | 2253 | rhs_offset += tree_to_uhwi (TREE_OPERAND (tem, 1)); |
c7ee7b45 RG |
2254 | } |
2255 | else if (DECL_P (tem)) | |
2256 | rhs = build_fold_addr_expr (tem); | |
2257 | else | |
2258 | return (void *)-1; | |
2259 | } | |
2260 | if (TREE_CODE (rhs) != SSA_NAME | |
2261 | && TREE_CODE (rhs) != ADDR_EXPR) | |
2262 | return (void *)-1; | |
2263 | ||
eb1ce453 | 2264 | copy_size = tree_to_uhwi (gimple_call_arg (def_stmt, 2)); |
c7ee7b45 RG |
2265 | |
2266 | /* The bases of the destination and the references have to agree. */ | |
2267 | if ((TREE_CODE (base) != MEM_REF | |
2268 | && !DECL_P (base)) | |
2269 | || (TREE_CODE (base) == MEM_REF | |
2270 | && (TREE_OPERAND (base, 0) != lhs | |
cc269bb6 | 2271 | || !tree_fits_uhwi_p (TREE_OPERAND (base, 1)))) |
c7ee7b45 RG |
2272 | || (DECL_P (base) |
2273 | && (TREE_CODE (lhs) != ADDR_EXPR | |
2274 | || TREE_OPERAND (lhs, 0) != base))) | |
2275 | return (void *)-1; | |
2276 | ||
c7ee7b45 RG |
2277 | at = offset / BITS_PER_UNIT; |
2278 | if (TREE_CODE (base) == MEM_REF) | |
eb1ce453 | 2279 | at += tree_to_uhwi (TREE_OPERAND (base, 1)); |
e65757f3 RB |
2280 | /* If the access is completely outside of the memcpy destination |
2281 | area there is no aliasing. */ | |
2282 | if (lhs_offset >= at + maxsize / BITS_PER_UNIT | |
2283 | || lhs_offset + copy_size <= at) | |
2284 | return NULL; | |
2285 | /* And the access has to be contained within the memcpy destination. */ | |
c7ee7b45 RG |
2286 | if (lhs_offset > at |
2287 | || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT) | |
2288 | return (void *)-1; | |
2289 | ||
2290 | /* Make room for 2 operands in the new reference. */ | |
9771b263 | 2291 | if (vr->operands.length () < 2) |
c7ee7b45 | 2292 | { |
9771b263 DN |
2293 | vec<vn_reference_op_s> old = vr->operands; |
2294 | vr->operands.safe_grow_cleared (2); | |
ec67c62e | 2295 | if (old == shared_lookup_references) |
26f3a4e1 | 2296 | shared_lookup_references = vr->operands; |
c7ee7b45 RG |
2297 | } |
2298 | else | |
9771b263 | 2299 | vr->operands.truncate (2); |
c7ee7b45 RG |
2300 | |
2301 | /* The looked-through reference is a simple MEM_REF. */ | |
2302 | memset (&op, 0, sizeof (op)); | |
2303 | op.type = vr->type; | |
2304 | op.opcode = MEM_REF; | |
2305 | op.op0 = build_int_cst (ptr_type_node, at - rhs_offset); | |
2306 | op.off = at - lhs_offset + rhs_offset; | |
9771b263 | 2307 | vr->operands[0] = op; |
6d6c9525 | 2308 | op.type = TREE_TYPE (rhs); |
c7ee7b45 RG |
2309 | op.opcode = TREE_CODE (rhs); |
2310 | op.op0 = rhs; | |
2311 | op.off = -1; | |
9771b263 | 2312 | vr->operands[1] = op; |
c7ee7b45 | 2313 | vr->hashcode = vn_reference_compute_hash (vr); |
b45d2719 | 2314 | |
edd048e2 RB |
2315 | /* Try folding the new reference to a constant. */ |
2316 | tree val = fully_constant_vn_reference_p (vr); | |
2317 | if (val) | |
2318 | return vn_reference_lookup_or_insert_for_pieces | |
2319 | (vuse, vr->set, vr->type, vr->operands, val); | |
2320 | ||
b45d2719 RG |
2321 | /* Adjust *ref from the new operands. */ |
2322 | if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands)) | |
01df5c8a | 2323 | return (void *)-1; |
03472fdd EB |
2324 | /* This can happen with bitfields. */ |
2325 | if (ref->size != r.size) | |
2326 | return (void *)-1; | |
b45d2719 | 2327 | *ref = r; |
01df5c8a | 2328 | |
d0ca0bcb RG |
2329 | /* Do not update last seen VUSE after translating. */ |
2330 | last_vuse_ptr = NULL; | |
2331 | ||
01df5c8a RG |
2332 | /* Keep looking for the adjusted *REF / VR pair. */ |
2333 | return NULL; | |
2334 | } | |
2335 | ||
2336 | /* Bail out and stop walking. */ | |
2337 | return (void *)-1; | |
2338 | } | |
2339 | ||
3c5b29f5 RB |
2340 | /* Return a reference op vector from OP that can be used for |
2341 | vn_reference_lookup_pieces. The caller is responsible for releasing | |
2342 | the vector. */ | |
2343 | ||
2344 | vec<vn_reference_op_s> | |
2345 | vn_reference_operands_for_lookup (tree op) | |
2346 | { | |
2347 | bool valueized; | |
2348 | return valueize_shared_reference_ops_from_ref (op, &valueized).copy (); | |
2349 | } | |
2350 | ||
c9145754 DB |
2351 | /* Lookup a reference operation by it's parts, in the current hash table. |
2352 | Returns the resulting value number if it exists in the hash table, | |
2353 | NULL_TREE otherwise. VNRESULT will be filled in with the actual | |
2354 | vn_reference_t stored in the hashtable if something is found. */ | |
89fb70a3 DB |
2355 | |
2356 | tree | |
b45d2719 | 2357 | vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type, |
9771b263 | 2358 | vec<vn_reference_op_s> operands, |
3bc27de7 | 2359 | vn_reference_t *vnresult, vn_lookup_kind kind) |
c9145754 DB |
2360 | { |
2361 | struct vn_reference_s vr1; | |
5006671f | 2362 | vn_reference_t tmp; |
12bd5a1e | 2363 | tree cst; |
5006671f RG |
2364 | |
2365 | if (!vnresult) | |
2366 | vnresult = &tmp; | |
2367 | *vnresult = NULL; | |
01df5c8a | 2368 | |
d1c0308e | 2369 | vr1.vuse = vuse_ssa_val (vuse); |
9771b263 DN |
2370 | shared_lookup_references.truncate (0); |
2371 | shared_lookup_references.safe_grow (operands.length ()); | |
2372 | memcpy (shared_lookup_references.address (), | |
2373 | operands.address (), | |
01df5c8a | 2374 | sizeof (vn_reference_op_s) |
9771b263 | 2375 | * operands.length ()); |
01df5c8a RG |
2376 | vr1.operands = operands = shared_lookup_references |
2377 | = valueize_refs (shared_lookup_references); | |
b45d2719 RG |
2378 | vr1.type = type; |
2379 | vr1.set = set; | |
c9145754 | 2380 | vr1.hashcode = vn_reference_compute_hash (&vr1); |
12bd5a1e RG |
2381 | if ((cst = fully_constant_vn_reference_p (&vr1))) |
2382 | return cst; | |
c9145754 | 2383 | |
12bd5a1e | 2384 | vn_reference_lookup_1 (&vr1, vnresult); |
5006671f | 2385 | if (!*vnresult |
3bc27de7 | 2386 | && kind != VN_NOWALK |
5006671f | 2387 | && vr1.vuse) |
53f3815c | 2388 | { |
b45d2719 | 2389 | ao_ref r; |
3bc27de7 | 2390 | vn_walk_kind = kind; |
b45d2719 | 2391 | if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands)) |
01df5c8a | 2392 | *vnresult = |
b45d2719 | 2393 | (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse, |
01df5c8a | 2394 | vn_reference_lookup_2, |
92a5094e | 2395 | vn_reference_lookup_3, |
76be46db | 2396 | vuse_ssa_val, &vr1); |
26f3a4e1 | 2397 | gcc_checking_assert (vr1.operands == shared_lookup_references); |
53f3815c RG |
2398 | } |
2399 | ||
5006671f RG |
2400 | if (*vnresult) |
2401 | return (*vnresult)->result; | |
2402 | ||
2403 | return NULL_TREE; | |
c9145754 DB |
2404 | } |
2405 | ||
2406 | /* Lookup OP in the current hash table, and return the resulting value | |
2407 | number if it exists in the hash table. Return NULL_TREE if it does | |
2408 | not exist in the hash table or if the result field of the structure | |
2409 | was NULL.. VNRESULT will be filled in with the vn_reference_t | |
1c48bff1 RB |
2410 | stored in the hashtable if one exists. When TBAA_P is false assume |
2411 | we are looking up a store and treat it as having alias-set zero. */ | |
c9145754 DB |
2412 | |
2413 | tree | |
3bc27de7 | 2414 | vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind, |
1c48bff1 | 2415 | vn_reference_t *vnresult, bool tbaa_p) |
89fb70a3 | 2416 | { |
9771b263 | 2417 | vec<vn_reference_op_s> operands; |
89fb70a3 | 2418 | struct vn_reference_s vr1; |
12bd5a1e | 2419 | tree cst; |
3ceaf2f5 | 2420 | bool valuezied_anything; |
5006671f | 2421 | |
c9145754 DB |
2422 | if (vnresult) |
2423 | *vnresult = NULL; | |
89fb70a3 | 2424 | |
d1c0308e | 2425 | vr1.vuse = vuse_ssa_val (vuse); |
3ceaf2f5 RG |
2426 | vr1.operands = operands |
2427 | = valueize_shared_reference_ops_from_ref (op, &valuezied_anything); | |
b45d2719 | 2428 | vr1.type = TREE_TYPE (op); |
87440c29 | 2429 | vr1.set = tbaa_p ? get_alias_set (op) : 0; |
89fb70a3 | 2430 | vr1.hashcode = vn_reference_compute_hash (&vr1); |
12bd5a1e RG |
2431 | if ((cst = fully_constant_vn_reference_p (&vr1))) |
2432 | return cst; | |
896c8b96 | 2433 | |
3bc27de7 | 2434 | if (kind != VN_NOWALK |
5006671f RG |
2435 | && vr1.vuse) |
2436 | { | |
2437 | vn_reference_t wvnresult; | |
b45d2719 | 2438 | ao_ref r; |
3ceaf2f5 RG |
2439 | /* Make sure to use a valueized reference if we valueized anything. |
2440 | Otherwise preserve the full reference for advanced TBAA. */ | |
2441 | if (!valuezied_anything | |
2442 | || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type, | |
2443 | vr1.operands)) | |
6d6c9525 | 2444 | ao_ref_init (&r, op); |
1c48bff1 RB |
2445 | if (! tbaa_p) |
2446 | r.ref_alias_set = r.base_alias_set = 0; | |
3bc27de7 | 2447 | vn_walk_kind = kind; |
5006671f | 2448 | wvnresult = |
b45d2719 | 2449 | (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse, |
01df5c8a | 2450 | vn_reference_lookup_2, |
92a5094e | 2451 | vn_reference_lookup_3, |
76be46db | 2452 | vuse_ssa_val, &vr1); |
26f3a4e1 | 2453 | gcc_checking_assert (vr1.operands == shared_lookup_references); |
5006671f RG |
2454 | if (wvnresult) |
2455 | { | |
2456 | if (vnresult) | |
2457 | *vnresult = wvnresult; | |
2458 | return wvnresult->result; | |
2459 | } | |
2460 | ||
2461 | return NULL_TREE; | |
896c8b96 | 2462 | } |
89fb70a3 | 2463 | |
5006671f | 2464 | return vn_reference_lookup_1 (&vr1, vnresult); |
89fb70a3 DB |
2465 | } |
2466 | ||
26f3a4e1 RB |
2467 | /* Lookup CALL in the current hash table and return the entry in |
2468 | *VNRESULT if found. Populates *VR for the hashtable lookup. */ | |
2469 | ||
2470 | void | |
538dd0b7 | 2471 | vn_reference_lookup_call (gcall *call, vn_reference_t *vnresult, |
26f3a4e1 RB |
2472 | vn_reference_t vr) |
2473 | { | |
27732ffd ML |
2474 | if (vnresult) |
2475 | *vnresult = NULL; | |
2476 | ||
26f3a4e1 RB |
2477 | tree vuse = gimple_vuse (call); |
2478 | ||
2479 | vr->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE; | |
2480 | vr->operands = valueize_shared_reference_ops_from_call (call); | |
2481 | vr->type = gimple_expr_type (call); | |
2482 | vr->set = 0; | |
2483 | vr->hashcode = vn_reference_compute_hash (vr); | |
2484 | vn_reference_lookup_1 (vr, vnresult); | |
2485 | } | |
c9145754 | 2486 | |
89fb70a3 | 2487 | /* Insert OP into the current hash table with a value number of |
c9145754 | 2488 | RESULT, and return the resulting reference structure we created. */ |
89fb70a3 | 2489 | |
26f3a4e1 | 2490 | static vn_reference_t |
4ec0a198 | 2491 | vn_reference_insert (tree op, tree result, tree vuse, tree vdef) |
89fb70a3 | 2492 | { |
bf190e8d | 2493 | vn_reference_s **slot; |
89fb70a3 | 2494 | vn_reference_t vr1; |
39e843e8 | 2495 | bool tem; |
89fb70a3 | 2496 | |
af6a6eec | 2497 | vr1 = current_info->references_pool->allocate (); |
c9145754 DB |
2498 | if (TREE_CODE (result) == SSA_NAME) |
2499 | vr1->value_id = VN_INFO (result)->value_id; | |
2500 | else | |
2501 | vr1->value_id = get_or_alloc_constant_value_id (result); | |
5006671f | 2502 | vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE; |
39e843e8 | 2503 | vr1->operands = valueize_shared_reference_ops_from_ref (op, &tem).copy (); |
b45d2719 RG |
2504 | vr1->type = TREE_TYPE (op); |
2505 | vr1->set = get_alias_set (op); | |
89fb70a3 DB |
2506 | vr1->hashcode = vn_reference_compute_hash (vr1); |
2507 | vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result; | |
4ec0a198 | 2508 | vr1->result_vdef = vdef; |
89fb70a3 | 2509 | |
c203e8a7 TS |
2510 | slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode, |
2511 | INSERT); | |
89fb70a3 DB |
2512 | |
2513 | /* Because we lookup stores using vuses, and value number failures | |
2514 | using the vdefs (see visit_reference_op_store for how and why), | |
2515 | it's possible that on failure we may try to insert an already | |
2516 | inserted store. This is not wrong, there is no ssa name for a | |
2517 | store that we could use as a differentiator anyway. Thus, unlike | |
2518 | the other lookup functions, you cannot gcc_assert (!*slot) | |
2519 | here. */ | |
2520 | ||
8d0eca24 RG |
2521 | /* But free the old slot in case of a collision. */ |
2522 | if (*slot) | |
2523 | free_reference (*slot); | |
89fb70a3 DB |
2524 | |
2525 | *slot = vr1; | |
c9145754 DB |
2526 | return vr1; |
2527 | } | |
2528 | ||
2529 | /* Insert a reference by it's pieces into the current hash table with | |
2530 | a value number of RESULT. Return the resulting reference | |
2531 | structure we created. */ | |
2532 | ||
2533 | vn_reference_t | |
b45d2719 | 2534 | vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type, |
9771b263 | 2535 | vec<vn_reference_op_s> operands, |
c9145754 DB |
2536 | tree result, unsigned int value_id) |
2537 | ||
2538 | { | |
bf190e8d | 2539 | vn_reference_s **slot; |
c9145754 DB |
2540 | vn_reference_t vr1; |
2541 | ||
af6a6eec | 2542 | vr1 = current_info->references_pool->allocate (); |
5006671f RG |
2543 | vr1->value_id = value_id; |
2544 | vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE; | |
c9145754 | 2545 | vr1->operands = valueize_refs (operands); |
b45d2719 RG |
2546 | vr1->type = type; |
2547 | vr1->set = set; | |
c9145754 DB |
2548 | vr1->hashcode = vn_reference_compute_hash (vr1); |
2549 | if (result && TREE_CODE (result) == SSA_NAME) | |
2550 | result = SSA_VAL (result); | |
2551 | vr1->result = result; | |
2552 | ||
c203e8a7 TS |
2553 | slot = current_info->references->find_slot_with_hash (vr1, vr1->hashcode, |
2554 | INSERT); | |
b8698a0f | 2555 | |
c9145754 | 2556 | /* At this point we should have all the things inserted that we have |
5006671f RG |
2557 | seen before, and we should never try inserting something that |
2558 | already exists. */ | |
c9145754 DB |
2559 | gcc_assert (!*slot); |
2560 | if (*slot) | |
2561 | free_reference (*slot); | |
2562 | ||
2563 | *slot = vr1; | |
2564 | return vr1; | |
89fb70a3 DB |
2565 | } |
2566 | ||
49a1fb2d | 2567 | /* Compute and return the hash value for nary operation VBO1. */ |
89fb70a3 | 2568 | |
26f3a4e1 | 2569 | static hashval_t |
49a1fb2d | 2570 | vn_nary_op_compute_hash (const vn_nary_op_t vno1) |
89fb70a3 | 2571 | { |
4e44a6e8 | 2572 | inchash::hash hstate; |
49a1fb2d | 2573 | unsigned i; |
89fb70a3 | 2574 | |
49a1fb2d RG |
2575 | for (i = 0; i < vno1->length; ++i) |
2576 | if (TREE_CODE (vno1->op[i]) == SSA_NAME) | |
2577 | vno1->op[i] = SSA_VAL (vno1->op[i]); | |
89fb70a3 | 2578 | |
7fd9012e RB |
2579 | if (((vno1->length == 2 |
2580 | && commutative_tree_code (vno1->opcode)) | |
2581 | || (vno1->length == 3 | |
2582 | && commutative_ternary_tree_code (vno1->opcode))) | |
14e72812 | 2583 | && tree_swap_operands_p (vno1->op[0], vno1->op[1])) |
6b4db501 | 2584 | std::swap (vno1->op[0], vno1->op[1]); |
7fd9012e | 2585 | else if (TREE_CODE_CLASS (vno1->opcode) == tcc_comparison |
14e72812 | 2586 | && tree_swap_operands_p (vno1->op[0], vno1->op[1])) |
7fd9012e RB |
2587 | { |
2588 | std::swap (vno1->op[0], vno1->op[1]); | |
2589 | vno1->opcode = swap_tree_comparison (vno1->opcode); | |
2590 | } | |
89fb70a3 | 2591 | |
4e44a6e8 | 2592 | hstate.add_int (vno1->opcode); |
49a1fb2d | 2593 | for (i = 0; i < vno1->length; ++i) |
4e44a6e8 | 2594 | inchash::add_expr (vno1->op[i], hstate); |
89fb70a3 | 2595 | |
4e44a6e8 | 2596 | return hstate.end (); |
89fb70a3 DB |
2597 | } |
2598 | ||
bf190e8d | 2599 | /* Compare nary operations VNO1 and VNO2 and return true if they are |
89fb70a3 DB |
2600 | equivalent. */ |
2601 | ||
bf190e8d LC |
2602 | bool |
2603 | vn_nary_op_eq (const_vn_nary_op_t const vno1, const_vn_nary_op_t const vno2) | |
89fb70a3 | 2604 | { |
49a1fb2d RG |
2605 | unsigned i; |
2606 | ||
85169114 PB |
2607 | if (vno1->hashcode != vno2->hashcode) |
2608 | return false; | |
2609 | ||
5a7d7f9c RG |
2610 | if (vno1->length != vno2->length) |
2611 | return false; | |
2612 | ||
49a1fb2d | 2613 | if (vno1->opcode != vno2->opcode |
63a14fa3 | 2614 | || !types_compatible_p (vno1->type, vno2->type)) |
49a1fb2d RG |
2615 | return false; |
2616 | ||
2617 | for (i = 0; i < vno1->length; ++i) | |
2618 | if (!expressions_equal_p (vno1->op[i], vno2->op[i])) | |
2619 | return false; | |
2620 | ||
2621 | return true; | |
89fb70a3 DB |
2622 | } |
2623 | ||
9ad6bebe | 2624 | /* Initialize VNO from the pieces provided. */ |
89fb70a3 | 2625 | |
9ad6bebe NF |
2626 | static void |
2627 | init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length, | |
5a7d7f9c | 2628 | enum tree_code code, tree type, tree *ops) |
9ad6bebe NF |
2629 | { |
2630 | vno->opcode = code; | |
2631 | vno->length = length; | |
2632 | vno->type = type; | |
5a7d7f9c | 2633 | memcpy (&vno->op[0], ops, sizeof (tree) * length); |
9ad6bebe NF |
2634 | } |
2635 | ||
2636 | /* Initialize VNO from OP. */ | |
2637 | ||
2638 | static void | |
2639 | init_vn_nary_op_from_op (vn_nary_op_t vno, tree op) | |
2640 | { | |
2641 | unsigned i; | |
2642 | ||
2643 | vno->opcode = TREE_CODE (op); | |
2644 | vno->length = TREE_CODE_LENGTH (TREE_CODE (op)); | |
2645 | vno->type = TREE_TYPE (op); | |
2646 | for (i = 0; i < vno->length; ++i) | |
2647 | vno->op[i] = TREE_OPERAND (op, i); | |
2648 | } | |
2649 | ||
5a7d7f9c RG |
2650 | /* Return the number of operands for a vn_nary ops structure from STMT. */ |
2651 | ||
2652 | static unsigned int | |
355fe088 | 2653 | vn_nary_length_from_stmt (gimple *stmt) |
5a7d7f9c RG |
2654 | { |
2655 | switch (gimple_assign_rhs_code (stmt)) | |
2656 | { | |
2657 | case REALPART_EXPR: | |
2658 | case IMAGPART_EXPR: | |
2659 | case VIEW_CONVERT_EXPR: | |
2660 | return 1; | |
2661 | ||
91af9dc9 RG |
2662 | case BIT_FIELD_REF: |
2663 | return 3; | |
2664 | ||
5a7d7f9c RG |
2665 | case CONSTRUCTOR: |
2666 | return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt)); | |
2667 | ||
2668 | default: | |
2669 | return gimple_num_ops (stmt) - 1; | |
2670 | } | |
2671 | } | |
2672 | ||
9ad6bebe NF |
2673 | /* Initialize VNO from STMT. */ |
2674 | ||
2675 | static void | |
355fe088 | 2676 | init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple *stmt) |
9ad6bebe NF |
2677 | { |
2678 | unsigned i; | |
2679 | ||
2680 | vno->opcode = gimple_assign_rhs_code (stmt); | |
9ad6bebe | 2681 | vno->type = gimple_expr_type (stmt); |
5a7d7f9c RG |
2682 | switch (vno->opcode) |
2683 | { | |
2684 | case REALPART_EXPR: | |
2685 | case IMAGPART_EXPR: | |
2686 | case VIEW_CONVERT_EXPR: | |
2687 | vno->length = 1; | |
2688 | vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0); | |
2689 | break; | |
2690 | ||
91af9dc9 RG |
2691 | case BIT_FIELD_REF: |
2692 | vno->length = 3; | |
2693 | vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0); | |
2694 | vno->op[1] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 1); | |
2695 | vno->op[2] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 2); | |
2696 | break; | |
2697 | ||
5a7d7f9c RG |
2698 | case CONSTRUCTOR: |
2699 | vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt)); | |
2700 | for (i = 0; i < vno->length; ++i) | |
2701 | vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value; | |
2702 | break; | |
2703 | ||
2704 | default: | |
91af9dc9 | 2705 | gcc_checking_assert (!gimple_assign_single_p (stmt)); |
5a7d7f9c RG |
2706 | vno->length = gimple_num_ops (stmt) - 1; |
2707 | for (i = 0; i < vno->length; ++i) | |
2708 | vno->op[i] = gimple_op (stmt, i + 1); | |
2709 | } | |
9ad6bebe NF |
2710 | } |
2711 | ||
2712 | /* Compute the hashcode for VNO and look for it in the hash table; | |
2713 | return the resulting value number if it exists in the hash table. | |
2714 | Return NULL_TREE if it does not exist in the hash table or if the | |
2715 | result field of the operation is NULL. VNRESULT will contain the | |
2716 | vn_nary_op_t from the hashtable if it exists. */ | |
2717 | ||
2718 | static tree | |
2719 | vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult) | |
c9145754 | 2720 | { |
bf190e8d | 2721 | vn_nary_op_s **slot; |
9ad6bebe | 2722 | |
c9145754 DB |
2723 | if (vnresult) |
2724 | *vnresult = NULL; | |
9ad6bebe NF |
2725 | |
2726 | vno->hashcode = vn_nary_op_compute_hash (vno); | |
c203e8a7 TS |
2727 | slot = current_info->nary->find_slot_with_hash (vno, vno->hashcode, |
2728 | NO_INSERT); | |
c9145754 | 2729 | if (!slot && current_info == optimistic_info) |
c203e8a7 TS |
2730 | slot = valid_info->nary->find_slot_with_hash (vno, vno->hashcode, |
2731 | NO_INSERT); | |
c9145754 DB |
2732 | if (!slot) |
2733 | return NULL_TREE; | |
2734 | if (vnresult) | |
bf190e8d LC |
2735 | *vnresult = *slot; |
2736 | return (*slot)->result; | |
c9145754 DB |
2737 | } |
2738 | ||
9ad6bebe NF |
2739 | /* Lookup a n-ary operation by its pieces and return the resulting value |
2740 | number if it exists in the hash table. Return NULL_TREE if it does | |
2741 | not exist in the hash table or if the result field of the operation | |
2742 | is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable | |
2743 | if it exists. */ | |
2744 | ||
2745 | tree | |
2746 | vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code, | |
5a7d7f9c | 2747 | tree type, tree *ops, vn_nary_op_t *vnresult) |
9ad6bebe | 2748 | { |
5a7d7f9c RG |
2749 | vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s, |
2750 | sizeof_vn_nary_op (length)); | |
2751 | init_vn_nary_op_from_pieces (vno1, length, code, type, ops); | |
2752 | return vn_nary_op_lookup_1 (vno1, vnresult); | |
9ad6bebe NF |
2753 | } |
2754 | ||
c9145754 DB |
2755 | /* Lookup OP in the current hash table, and return the resulting value |
2756 | number if it exists in the hash table. Return NULL_TREE if it does | |
2757 | not exist in the hash table or if the result field of the operation | |
2758 | is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable | |
2759 | if it exists. */ | |
2760 | ||
2761 | tree | |
2762 | vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult) | |
89fb70a3 | 2763 | { |
5a7d7f9c RG |
2764 | vn_nary_op_t vno1 |
2765 | = XALLOCAVAR (struct vn_nary_op_s, | |
2766 | sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op)))); | |
2767 | init_vn_nary_op_from_op (vno1, op); | |
2768 | return vn_nary_op_lookup_1 (vno1, vnresult); | |
89fb70a3 DB |
2769 | } |
2770 | ||
726a989a RB |
2771 | /* Lookup the rhs of STMT in the current hash table, and return the resulting |
2772 | value number if it exists in the hash table. Return NULL_TREE if | |
2773 | it does not exist in the hash table. VNRESULT will contain the | |
2774 | vn_nary_op_t from the hashtable if it exists. */ | |
2775 | ||
2776 | tree | |
355fe088 | 2777 | vn_nary_op_lookup_stmt (gimple *stmt, vn_nary_op_t *vnresult) |
726a989a | 2778 | { |
5a7d7f9c RG |
2779 | vn_nary_op_t vno1 |
2780 | = XALLOCAVAR (struct vn_nary_op_s, | |
2781 | sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt))); | |
2782 | init_vn_nary_op_from_stmt (vno1, stmt); | |
2783 | return vn_nary_op_lookup_1 (vno1, vnresult); | |
9ad6bebe NF |
2784 | } |
2785 | ||
2786 | /* Allocate a vn_nary_op_t with LENGTH operands on STACK. */ | |
2787 | ||
2788 | static vn_nary_op_t | |
2789 | alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack) | |
2790 | { | |
2791 | return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length)); | |
2792 | } | |
2793 | ||
2794 | /* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's | |
2795 | obstack. */ | |
2796 | ||
2797 | static vn_nary_op_t | |
2798 | alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id) | |
2799 | { | |
2800 | vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length, | |
2801 | ¤t_info->nary_obstack); | |
2802 | ||
2803 | vno1->value_id = value_id; | |
2804 | vno1->length = length; | |
2805 | vno1->result = result; | |
2806 | ||
2807 | return vno1; | |
2808 | } | |
2809 | ||
2810 | /* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute | |
2811 | VNO->HASHCODE first. */ | |
2812 | ||
2813 | static vn_nary_op_t | |
c203e8a7 | 2814 | vn_nary_op_insert_into (vn_nary_op_t vno, vn_nary_op_table_type *table, |
bf190e8d | 2815 | bool compute_hash) |
9ad6bebe | 2816 | { |
bf190e8d | 2817 | vn_nary_op_s **slot; |
9ad6bebe NF |
2818 | |
2819 | if (compute_hash) | |
2820 | vno->hashcode = vn_nary_op_compute_hash (vno); | |
2821 | ||
c203e8a7 | 2822 | slot = table->find_slot_with_hash (vno, vno->hashcode, INSERT); |
9ad6bebe NF |
2823 | gcc_assert (!*slot); |
2824 | ||
2825 | *slot = vno; | |
2826 | return vno; | |
726a989a RB |
2827 | } |
2828 | ||
c9145754 DB |
2829 | /* Insert a n-ary operation into the current hash table using it's |
2830 | pieces. Return the vn_nary_op_t structure we created and put in | |
2831 | the hashtable. */ | |
2832 | ||
2833 | vn_nary_op_t | |
2834 | vn_nary_op_insert_pieces (unsigned int length, enum tree_code code, | |
5a7d7f9c RG |
2835 | tree type, tree *ops, |
2836 | tree result, unsigned int value_id) | |
c9145754 | 2837 | { |
5a7d7f9c RG |
2838 | vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id); |
2839 | init_vn_nary_op_from_pieces (vno1, length, code, type, ops); | |
9ad6bebe | 2840 | return vn_nary_op_insert_into (vno1, current_info->nary, true); |
c9145754 DB |
2841 | } |
2842 | ||
89fb70a3 | 2843 | /* Insert OP into the current hash table with a value number of |
c9145754 DB |
2844 | RESULT. Return the vn_nary_op_t structure we created and put in |
2845 | the hashtable. */ | |
89fb70a3 | 2846 | |
c9145754 | 2847 | vn_nary_op_t |
49a1fb2d | 2848 | vn_nary_op_insert (tree op, tree result) |
89fb70a3 | 2849 | { |
49a1fb2d | 2850 | unsigned length = TREE_CODE_LENGTH (TREE_CODE (op)); |
49a1fb2d | 2851 | vn_nary_op_t vno1; |
49a1fb2d | 2852 | |
9ad6bebe NF |
2853 | vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id); |
2854 | init_vn_nary_op_from_op (vno1, op); | |
2855 | return vn_nary_op_insert_into (vno1, current_info->nary, true); | |
89fb70a3 DB |
2856 | } |
2857 | ||
726a989a RB |
2858 | /* Insert the rhs of STMT into the current hash table with a value number of |
2859 | RESULT. */ | |
2860 | ||
60dd79ca | 2861 | static vn_nary_op_t |
355fe088 | 2862 | vn_nary_op_insert_stmt (gimple *stmt, tree result) |
726a989a | 2863 | { |
5a7d7f9c RG |
2864 | vn_nary_op_t vno1 |
2865 | = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt), | |
2866 | result, VN_INFO (result)->value_id); | |
9ad6bebe NF |
2867 | init_vn_nary_op_from_stmt (vno1, stmt); |
2868 | return vn_nary_op_insert_into (vno1, current_info->nary, true); | |
726a989a RB |
2869 | } |
2870 | ||
89fb70a3 DB |
2871 | /* Compute a hashcode for PHI operation VP1 and return it. */ |
2872 | ||
2873 | static inline hashval_t | |
2874 | vn_phi_compute_hash (vn_phi_t vp1) | |
2875 | { | |
e6503e0a RB |
2876 | inchash::hash hstate (vp1->phiargs.length () > 2 |
2877 | ? vp1->block->index : vp1->phiargs.length ()); | |
89fb70a3 | 2878 | tree phi1op; |
1d295886 | 2879 | tree type; |
9fe4f60a RB |
2880 | edge e; |
2881 | edge_iterator ei; | |
89fb70a3 | 2882 | |
1d295886 RG |
2883 | /* If all PHI arguments are constants we need to distinguish |
2884 | the PHI node via its type. */ | |
24d63016 | 2885 | type = vp1->type; |
4e44a6e8 | 2886 | hstate.merge_hash (vn_hash_type (type)); |
1d295886 | 2887 | |
9fe4f60a | 2888 | FOR_EACH_EDGE (e, ei, vp1->block->preds) |
89fb70a3 | 2889 | { |
9fe4f60a RB |
2890 | /* Don't hash backedge values they need to be handled as VN_TOP |
2891 | for optimistic value-numbering. */ | |
2892 | if (e->flags & EDGE_DFS_BACK) | |
2893 | continue; | |
2894 | ||
2895 | phi1op = vp1->phiargs[e->dest_idx]; | |
89fb70a3 DB |
2896 | if (phi1op == VN_TOP) |
2897 | continue; | |
4e44a6e8 | 2898 | inchash::add_expr (phi1op, hstate); |
89fb70a3 DB |
2899 | } |
2900 | ||
4e44a6e8 | 2901 | return hstate.end (); |
89fb70a3 DB |
2902 | } |
2903 | ||
e6503e0a | 2904 | |
87961d1b RB |
2905 | /* Return true if COND1 and COND2 represent the same condition, set |
2906 | *INVERTED_P if one needs to be inverted to make it the same as | |
2907 | the other. */ | |
2908 | ||
2909 | static bool | |
2910 | cond_stmts_equal_p (gcond *cond1, gcond *cond2, bool *inverted_p) | |
2911 | { | |
2912 | enum tree_code code1 = gimple_cond_code (cond1); | |
2913 | enum tree_code code2 = gimple_cond_code (cond2); | |
2914 | tree lhs1 = gimple_cond_lhs (cond1); | |
2915 | tree lhs2 = gimple_cond_lhs (cond2); | |
2916 | tree rhs1 = gimple_cond_rhs (cond1); | |
2917 | tree rhs2 = gimple_cond_rhs (cond2); | |
2918 | ||
2919 | *inverted_p = false; | |
2920 | if (code1 == code2) | |
2921 | ; | |
2922 | else if (code1 == swap_tree_comparison (code2)) | |
2923 | std::swap (lhs2, rhs2); | |
2924 | else if (code1 == invert_tree_comparison (code2, HONOR_NANS (lhs2))) | |
2925 | *inverted_p = true; | |
2926 | else if (code1 == invert_tree_comparison | |
2927 | (swap_tree_comparison (code2), HONOR_NANS (lhs2))) | |
2928 | { | |
2929 | std::swap (lhs2, rhs2); | |
2930 | *inverted_p = true; | |
2931 | } | |
2932 | else | |
2933 | return false; | |
2934 | ||
94852c8e RB |
2935 | lhs1 = vn_valueize (lhs1); |
2936 | rhs1 = vn_valueize (rhs1); | |
2937 | lhs2 = vn_valueize (lhs2); | |
2938 | rhs2 = vn_valueize (rhs2); | |
2939 | return ((expressions_equal_p (lhs1, lhs2) | |
2940 | && expressions_equal_p (rhs1, rhs2)) | |
2941 | || (commutative_tree_code (code1) | |
2942 | && expressions_equal_p (lhs1, rhs2) | |
2943 | && expressions_equal_p (rhs1, lhs2))); | |
87961d1b RB |
2944 | } |
2945 | ||
89fb70a3 DB |
2946 | /* Compare two phi entries for equality, ignoring VN_TOP arguments. */ |
2947 | ||
2948 | static int | |
bf190e8d | 2949 | vn_phi_eq (const_vn_phi_t const vp1, const_vn_phi_t const vp2) |
89fb70a3 | 2950 | { |
85169114 PB |
2951 | if (vp1->hashcode != vp2->hashcode) |
2952 | return false; | |
2953 | ||
e6503e0a | 2954 | if (vp1->block != vp2->block) |
89fb70a3 | 2955 | { |
e6503e0a | 2956 | if (vp1->phiargs.length () != vp2->phiargs.length ()) |
1d295886 RG |
2957 | return false; |
2958 | ||
e6503e0a | 2959 | switch (vp1->phiargs.length ()) |
89fb70a3 | 2960 | { |
e6503e0a RB |
2961 | case 1: |
2962 | /* Single-arg PHIs are just copies. */ | |
2963 | break; | |
2964 | ||
2965 | case 2: | |
2966 | { | |
2967 | /* Rule out backedges into the PHI. */ | |
2968 | if (vp1->block->loop_father->header == vp1->block | |
2969 | || vp2->block->loop_father->header == vp2->block) | |
2970 | return false; | |
2971 | ||
2972 | /* If the PHI nodes do not have compatible types | |
2973 | they are not the same. */ | |
2974 | if (!types_compatible_p (vp1->type, vp2->type)) | |
2975 | return false; | |
2976 | ||
2977 | basic_block idom1 | |
2978 | = get_immediate_dominator (CDI_DOMINATORS, vp1->block); | |
2979 | basic_block idom2 | |
2980 | = get_immediate_dominator (CDI_DOMINATORS, vp2->block); | |
2981 | /* If the immediate dominator end in switch stmts multiple | |
2982 | values may end up in the same PHI arg via intermediate | |
2983 | CFG merges. */ | |
2984 | if (EDGE_COUNT (idom1->succs) != 2 | |
2985 | || EDGE_COUNT (idom2->succs) != 2) | |
2986 | return false; | |
2987 | ||
2988 | /* Verify the controlling stmt is the same. */ | |
2989 | gimple *last1 = last_stmt (idom1); | |
2990 | gimple *last2 = last_stmt (idom2); | |
2991 | if (gimple_code (last1) != GIMPLE_COND | |
2992 | || gimple_code (last2) != GIMPLE_COND) | |
2993 | return false; | |
87961d1b RB |
2994 | bool inverted_p; |
2995 | if (! cond_stmts_equal_p (as_a <gcond *> (last1), | |
2996 | as_a <gcond *> (last2), &inverted_p)) | |
e6503e0a RB |
2997 | return false; |
2998 | ||
2999 | /* Get at true/false controlled edges into the PHI. */ | |
3000 | edge te1, te2, fe1, fe2; | |
3001 | if (! extract_true_false_controlled_edges (idom1, vp1->block, | |
3002 | &te1, &fe1) | |
3003 | || ! extract_true_false_controlled_edges (idom2, vp2->block, | |
3004 | &te2, &fe2)) | |
3005 | return false; | |
3006 | ||
87961d1b RB |
3007 | /* Swap edges if the second condition is the inverted of the |
3008 | first. */ | |
3009 | if (inverted_p) | |
3010 | std::swap (te2, fe2); | |
3011 | ||
e6503e0a RB |
3012 | /* ??? Handle VN_TOP specially. */ |
3013 | if (! expressions_equal_p (vp1->phiargs[te1->dest_idx], | |
3014 | vp2->phiargs[te2->dest_idx]) | |
3015 | || ! expressions_equal_p (vp1->phiargs[fe1->dest_idx], | |
3016 | vp2->phiargs[fe2->dest_idx])) | |
3017 | return false; | |
3018 | ||
3019 | return true; | |
3020 | } | |
3021 | ||
3022 | default: | |
3023 | return false; | |
89fb70a3 | 3024 | } |
89fb70a3 | 3025 | } |
e6503e0a RB |
3026 | |
3027 | /* If the PHI nodes do not have compatible types | |
3028 | they are not the same. */ | |
3029 | if (!types_compatible_p (vp1->type, vp2->type)) | |
3030 | return false; | |
3031 | ||
3032 | /* Any phi in the same block will have it's arguments in the | |
3033 | same edge order, because of how we store phi nodes. */ | |
3034 | int i; | |
3035 | tree phi1op; | |
3036 | FOR_EACH_VEC_ELT (vp1->phiargs, i, phi1op) | |
3037 | { | |
3038 | tree phi2op = vp2->phiargs[i]; | |
3039 | if (phi1op == VN_TOP || phi2op == VN_TOP) | |
3040 | continue; | |
3041 | if (!expressions_equal_p (phi1op, phi2op)) | |
3042 | return false; | |
3043 | } | |
3044 | ||
3045 | return true; | |
89fb70a3 DB |
3046 | } |
3047 | ||
9771b263 | 3048 | static vec<tree> shared_lookup_phiargs; |
89fb70a3 DB |
3049 | |
3050 | /* Lookup PHI in the current hash table, and return the resulting | |
3051 | value number if it exists in the hash table. Return NULL_TREE if | |
3052 | it does not exist in the hash table. */ | |
3053 | ||
de081cfd | 3054 | static tree |
355fe088 | 3055 | vn_phi_lookup (gimple *phi) |
89fb70a3 | 3056 | { |
bf190e8d | 3057 | vn_phi_s **slot; |
89fb70a3 | 3058 | struct vn_phi_s vp1; |
9fe4f60a RB |
3059 | edge e; |
3060 | edge_iterator ei; | |
89fb70a3 | 3061 | |
9771b263 | 3062 | shared_lookup_phiargs.truncate (0); |
9fe4f60a | 3063 | shared_lookup_phiargs.safe_grow (gimple_phi_num_args (phi)); |
89fb70a3 DB |
3064 | |
3065 | /* Canonicalize the SSA_NAME's to their value number. */ | |
9fe4f60a | 3066 | FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds) |
89fb70a3 | 3067 | { |
9fe4f60a | 3068 | tree def = PHI_ARG_DEF_FROM_EDGE (phi, e); |
89fb70a3 | 3069 | def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def; |
9fe4f60a | 3070 | shared_lookup_phiargs[e->dest_idx] = def; |
89fb70a3 | 3071 | } |
24d63016 | 3072 | vp1.type = TREE_TYPE (gimple_phi_result (phi)); |
89fb70a3 | 3073 | vp1.phiargs = shared_lookup_phiargs; |
726a989a | 3074 | vp1.block = gimple_bb (phi); |
89fb70a3 | 3075 | vp1.hashcode = vn_phi_compute_hash (&vp1); |
c203e8a7 TS |
3076 | slot = current_info->phis->find_slot_with_hash (&vp1, vp1.hashcode, |
3077 | NO_INSERT); | |
27fa4044 | 3078 | if (!slot && current_info == optimistic_info) |
c203e8a7 TS |
3079 | slot = valid_info->phis->find_slot_with_hash (&vp1, vp1.hashcode, |
3080 | NO_INSERT); | |
89fb70a3 DB |
3081 | if (!slot) |
3082 | return NULL_TREE; | |
bf190e8d | 3083 | return (*slot)->result; |
89fb70a3 DB |
3084 | } |
3085 | ||
3086 | /* Insert PHI into the current hash table with a value number of | |
3087 | RESULT. */ | |
3088 | ||
c9145754 | 3089 | static vn_phi_t |
355fe088 | 3090 | vn_phi_insert (gimple *phi, tree result) |
89fb70a3 | 3091 | { |
bf190e8d | 3092 | vn_phi_s **slot; |
af6a6eec | 3093 | vn_phi_t vp1 = current_info->phis_pool->allocate (); |
6e1aa848 | 3094 | vec<tree> args = vNULL; |
9fe4f60a RB |
3095 | edge e; |
3096 | edge_iterator ei; | |
3097 | ||
3098 | args.safe_grow (gimple_phi_num_args (phi)); | |
89fb70a3 DB |
3099 | |
3100 | /* Canonicalize the SSA_NAME's to their value number. */ | |
9fe4f60a | 3101 | FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds) |
89fb70a3 | 3102 | { |
9fe4f60a | 3103 | tree def = PHI_ARG_DEF_FROM_EDGE (phi, e); |
89fb70a3 | 3104 | def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def; |
9fe4f60a | 3105 | args[e->dest_idx] = def; |
89fb70a3 | 3106 | } |
c9145754 | 3107 | vp1->value_id = VN_INFO (result)->value_id; |
24d63016 | 3108 | vp1->type = TREE_TYPE (gimple_phi_result (phi)); |
89fb70a3 | 3109 | vp1->phiargs = args; |
726a989a | 3110 | vp1->block = gimple_bb (phi); |
89fb70a3 DB |
3111 | vp1->result = result; |
3112 | vp1->hashcode = vn_phi_compute_hash (vp1); | |
3113 | ||
c203e8a7 | 3114 | slot = current_info->phis->find_slot_with_hash (vp1, vp1->hashcode, INSERT); |
89fb70a3 DB |
3115 | |
3116 | /* Because we iterate over phi operations more than once, it's | |
3117 | possible the slot might already exist here, hence no assert.*/ | |
3118 | *slot = vp1; | |
c9145754 | 3119 | return vp1; |
89fb70a3 DB |
3120 | } |
3121 | ||
3122 | ||
3123 | /* Print set of components in strongly connected component SCC to OUT. */ | |
3124 | ||
3125 | static void | |
9771b263 | 3126 | print_scc (FILE *out, vec<tree> scc) |
89fb70a3 DB |
3127 | { |
3128 | tree var; | |
3129 | unsigned int i; | |
3130 | ||
0eb09f31 | 3131 | fprintf (out, "SCC consists of:"); |
9771b263 | 3132 | FOR_EACH_VEC_ELT (scc, i, var) |
89fb70a3 | 3133 | { |
89fb70a3 | 3134 | fprintf (out, " "); |
0eb09f31 | 3135 | print_generic_expr (out, var, 0); |
89fb70a3 DB |
3136 | } |
3137 | fprintf (out, "\n"); | |
3138 | } | |
3139 | ||
fac40b02 RB |
3140 | /* Return true if BB1 is dominated by BB2 taking into account edges |
3141 | that are not executable. */ | |
3142 | ||
3143 | static bool | |
3144 | dominated_by_p_w_unex (basic_block bb1, basic_block bb2) | |
3145 | { | |
3146 | edge_iterator ei; | |
3147 | edge e; | |
3148 | ||
3149 | if (dominated_by_p (CDI_DOMINATORS, bb1, bb2)) | |
3150 | return true; | |
3151 | ||
3152 | /* Before iterating we'd like to know if there exists a | |
3153 | (executable) path from bb2 to bb1 at all, if not we can | |
3154 | directly return false. For now simply iterate once. */ | |
3155 | ||
3156 | /* Iterate to the single executable bb1 predecessor. */ | |
3157 | if (EDGE_COUNT (bb1->preds) > 1) | |
3158 | { | |
3159 | edge prede = NULL; | |
3160 | FOR_EACH_EDGE (e, ei, bb1->preds) | |
3161 | if (e->flags & EDGE_EXECUTABLE) | |
3162 | { | |
3163 | if (prede) | |
3164 | { | |
3165 | prede = NULL; | |
3166 | break; | |
3167 | } | |
3168 | prede = e; | |
3169 | } | |
3170 | if (prede) | |
3171 | { | |
3172 | bb1 = prede->src; | |
3173 | ||
3174 | /* Re-do the dominance check with changed bb1. */ | |
3175 | if (dominated_by_p (CDI_DOMINATORS, bb1, bb2)) | |
3176 | return true; | |
3177 | } | |
3178 | } | |
3179 | ||
3180 | /* Iterate to the single executable bb2 successor. */ | |
3181 | edge succe = NULL; | |
3182 | FOR_EACH_EDGE (e, ei, bb2->succs) | |
3183 | if (e->flags & EDGE_EXECUTABLE) | |
3184 | { | |
3185 | if (succe) | |
3186 | { | |
3187 | succe = NULL; | |
3188 | break; | |
3189 | } | |
3190 | succe = e; | |
3191 | } | |
3192 | if (succe) | |
3193 | { | |
3194 | /* Verify the reached block is only reached through succe. | |
3195 | If there is only one edge we can spare us the dominator | |
3196 | check and iterate directly. */ | |
3197 | if (EDGE_COUNT (succe->dest->preds) > 1) | |
3198 | { | |
3199 | FOR_EACH_EDGE (e, ei, succe->dest->preds) | |
3200 | if (e != succe | |
3201 | && (e->flags & EDGE_EXECUTABLE)) | |
3202 | { | |
3203 | succe = NULL; | |
3204 | break; | |
3205 | } | |
3206 | } | |
3207 | if (succe) | |
3208 | { | |
3209 | bb2 = succe->dest; | |
3210 | ||
3211 | /* Re-do the dominance check with changed bb2. */ | |
3212 | if (dominated_by_p (CDI_DOMINATORS, bb1, bb2)) | |
3213 | return true; | |
3214 | } | |
3215 | } | |
3216 | ||
3217 | /* We could now iterate updating bb1 / bb2. */ | |
3218 | return false; | |
3219 | } | |
3220 | ||
89fb70a3 DB |
3221 | /* Set the value number of FROM to TO, return true if it has changed |
3222 | as a result. */ | |
3223 | ||
3224 | static inline bool | |
3225 | set_ssa_val_to (tree from, tree to) | |
3226 | { | |
90bc4623 | 3227 | tree currval = SSA_VAL (from); |
d1de852b | 3228 | HOST_WIDE_INT toff, coff; |
89fb70a3 | 3229 | |
a764d660 RB |
3230 | /* The only thing we allow as value numbers are ssa_names |
3231 | and invariants. So assert that here. We don't allow VN_TOP | |
3232 | as visiting a stmt should produce a value-number other than | |
3233 | that. | |
3234 | ??? Still VN_TOP can happen for unreachable code, so force | |
3235 | it to varying in that case. Not all code is prepared to | |
3236 | get VN_TOP on valueization. */ | |
3237 | if (to == VN_TOP) | |
3238 | { | |
3239 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3240 | fprintf (dump_file, "Forcing value number to varying on " | |
3241 | "receiving VN_TOP\n"); | |
3242 | to = from; | |
3243 | } | |
3244 | ||
3245 | gcc_assert (to != NULL_TREE | |
703c9ccd RB |
3246 | && ((TREE_CODE (to) == SSA_NAME |
3247 | && (to == from || SSA_VAL (to) == to)) | |
a764d660 RB |
3248 | || is_gimple_min_invariant (to))); |
3249 | ||
90bc4623 RG |
3250 | if (from != to) |
3251 | { | |
3252 | if (currval == from) | |
3253 | { | |
3254 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3255 | { | |
3256 | fprintf (dump_file, "Not changing value number of "); | |
3257 | print_generic_expr (dump_file, from, 0); | |
3258 | fprintf (dump_file, " from VARYING to "); | |
3259 | print_generic_expr (dump_file, to, 0); | |
3260 | fprintf (dump_file, "\n"); | |
3261 | } | |
3262 | return false; | |
3263 | } | |
37f6a157 RB |
3264 | else if (currval != VN_TOP |
3265 | && ! is_gimple_min_invariant (currval) | |
3266 | && is_gimple_min_invariant (to)) | |
3267 | { | |
3268 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3269 | { | |
3270 | fprintf (dump_file, "Forcing VARYING instead of changing " | |
3271 | "value number of "); | |
3272 | print_generic_expr (dump_file, from, 0); | |
3273 | fprintf (dump_file, " from "); | |
3274 | print_generic_expr (dump_file, currval, 0); | |
3275 | fprintf (dump_file, " (non-constant) to "); | |
3276 | print_generic_expr (dump_file, to, 0); | |
3277 | fprintf (dump_file, " (constant)\n"); | |
3278 | } | |
3279 | to = from; | |
3280 | } | |
90bc4623 RG |
3281 | else if (TREE_CODE (to) == SSA_NAME |
3282 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to)) | |
3283 | to = from; | |
3284 | } | |
fe4fefa0 | 3285 | |
89fb70a3 DB |
3286 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3287 | { | |
3288 | fprintf (dump_file, "Setting value number of "); | |
3289 | print_generic_expr (dump_file, from, 0); | |
3290 | fprintf (dump_file, " to "); | |
3291 | print_generic_expr (dump_file, to, 0); | |
89fb70a3 DB |
3292 | } |
3293 | ||
d1de852b RB |
3294 | if (currval != to |
3295 | && !operand_equal_p (currval, to, 0) | |
3296 | /* ??? For addresses involving volatile objects or types operand_equal_p | |
3297 | does not reliably detect ADDR_EXPRs as equal. We know we are only | |
3298 | getting invariant gimple addresses here, so can use | |
3299 | get_addr_base_and_unit_offset to do this comparison. */ | |
3300 | && !(TREE_CODE (currval) == ADDR_EXPR | |
3301 | && TREE_CODE (to) == ADDR_EXPR | |
3302 | && (get_addr_base_and_unit_offset (TREE_OPERAND (currval, 0), &coff) | |
3303 | == get_addr_base_and_unit_offset (TREE_OPERAND (to, 0), &toff)) | |
3304 | && coff == toff)) | |
89fb70a3 | 3305 | { |
e93c66bc RB |
3306 | /* If we equate two SSA names we have to make the side-band info |
3307 | of the leader conservative (and remember whatever original value | |
3308 | was present). */ | |
3309 | if (TREE_CODE (to) == SSA_NAME) | |
3310 | { | |
3311 | if (INTEGRAL_TYPE_P (TREE_TYPE (to)) | |
3312 | && SSA_NAME_RANGE_INFO (to)) | |
3313 | { | |
3314 | if (SSA_NAME_IS_DEFAULT_DEF (to) | |
fac40b02 RB |
3315 | || dominated_by_p_w_unex |
3316 | (gimple_bb (SSA_NAME_DEF_STMT (from)), | |
3317 | gimple_bb (SSA_NAME_DEF_STMT (to)))) | |
e93c66bc RB |
3318 | /* Keep the info from the dominator. */ |
3319 | ; | |
3320 | else if (SSA_NAME_IS_DEFAULT_DEF (from) | |
fac40b02 RB |
3321 | || dominated_by_p_w_unex |
3322 | (gimple_bb (SSA_NAME_DEF_STMT (to)), | |
3323 | gimple_bb (SSA_NAME_DEF_STMT (from)))) | |
e93c66bc RB |
3324 | { |
3325 | /* Save old info. */ | |
3326 | if (! VN_INFO (to)->info.range_info) | |
fa4511c2 RB |
3327 | { |
3328 | VN_INFO (to)->info.range_info = SSA_NAME_RANGE_INFO (to); | |
3329 | VN_INFO (to)->range_info_anti_range_p | |
3330 | = SSA_NAME_ANTI_RANGE_P (to); | |
3331 | } | |
e93c66bc RB |
3332 | /* Use that from the dominator. */ |
3333 | SSA_NAME_RANGE_INFO (to) = SSA_NAME_RANGE_INFO (from); | |
fa4511c2 | 3334 | SSA_NAME_ANTI_RANGE_P (to) = SSA_NAME_ANTI_RANGE_P (from); |
e93c66bc RB |
3335 | } |
3336 | else | |
3337 | { | |
3338 | /* Save old info. */ | |
3339 | if (! VN_INFO (to)->info.range_info) | |
fa4511c2 RB |
3340 | { |
3341 | VN_INFO (to)->info.range_info = SSA_NAME_RANGE_INFO (to); | |
3342 | VN_INFO (to)->range_info_anti_range_p | |
3343 | = SSA_NAME_ANTI_RANGE_P (to); | |
3344 | } | |
e93c66bc RB |
3345 | /* Rather than allocating memory and unioning the info |
3346 | just clear it. */ | |
3347 | SSA_NAME_RANGE_INFO (to) = NULL; | |
3348 | } | |
3349 | } | |
3350 | else if (POINTER_TYPE_P (TREE_TYPE (to)) | |
3351 | && SSA_NAME_PTR_INFO (to)) | |
3352 | { | |
3353 | if (SSA_NAME_IS_DEFAULT_DEF (to) | |
fac40b02 RB |
3354 | || dominated_by_p_w_unex |
3355 | (gimple_bb (SSA_NAME_DEF_STMT (from)), | |
3356 | gimple_bb (SSA_NAME_DEF_STMT (to)))) | |
e93c66bc RB |
3357 | /* Keep the info from the dominator. */ |
3358 | ; | |
3359 | else if (SSA_NAME_IS_DEFAULT_DEF (from) | |
fac40b02 RB |
3360 | || dominated_by_p_w_unex |
3361 | (gimple_bb (SSA_NAME_DEF_STMT (to)), | |
3362 | gimple_bb (SSA_NAME_DEF_STMT (from)))) | |
e93c66bc RB |
3363 | { |
3364 | /* Save old info. */ | |
3365 | if (! VN_INFO (to)->info.ptr_info) | |
3366 | VN_INFO (to)->info.ptr_info = SSA_NAME_PTR_INFO (to); | |
3367 | /* Use that from the dominator. */ | |
3368 | SSA_NAME_PTR_INFO (to) = SSA_NAME_PTR_INFO (from); | |
3369 | } | |
dd6f2cf9 RB |
3370 | else if (! SSA_NAME_PTR_INFO (from) |
3371 | /* Handle the case of trivially equivalent info. */ | |
3372 | || memcmp (SSA_NAME_PTR_INFO (to), | |
3373 | SSA_NAME_PTR_INFO (from), | |
3374 | sizeof (ptr_info_def)) != 0) | |
e93c66bc RB |
3375 | { |
3376 | /* Save old info. */ | |
3377 | if (! VN_INFO (to)->info.ptr_info) | |
3378 | VN_INFO (to)->info.ptr_info = SSA_NAME_PTR_INFO (to); | |
3379 | /* Rather than allocating memory and unioning the info | |
3380 | just clear it. */ | |
3381 | SSA_NAME_PTR_INFO (to) = NULL; | |
3382 | } | |
3383 | } | |
3384 | } | |
3385 | ||
5006671f | 3386 | VN_INFO (from)->valnum = to; |
8495c94f RG |
3387 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3388 | fprintf (dump_file, " (changed)\n"); | |
89fb70a3 DB |
3389 | return true; |
3390 | } | |
8495c94f RG |
3391 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3392 | fprintf (dump_file, "\n"); | |
89fb70a3 DB |
3393 | return false; |
3394 | } | |
3395 | ||
00115921 TV |
3396 | /* Mark as processed all the definitions in the defining stmt of USE, or |
3397 | the USE itself. */ | |
3398 | ||
3399 | static void | |
3400 | mark_use_processed (tree use) | |
3401 | { | |
3402 | ssa_op_iter iter; | |
3403 | def_operand_p defp; | |
355fe088 | 3404 | gimple *stmt = SSA_NAME_DEF_STMT (use); |
00115921 TV |
3405 | |
3406 | if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI) | |
3407 | { | |
3408 | VN_INFO (use)->use_processed = true; | |
3409 | return; | |
3410 | } | |
3411 | ||
3412 | FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS) | |
3413 | { | |
3414 | tree def = DEF_FROM_PTR (defp); | |
3415 | ||
3416 | VN_INFO (def)->use_processed = true; | |
3417 | } | |
3418 | } | |
3419 | ||
89fb70a3 DB |
3420 | /* Set all definitions in STMT to value number to themselves. |
3421 | Return true if a value number changed. */ | |
3422 | ||
3423 | static bool | |
355fe088 | 3424 | defs_to_varying (gimple *stmt) |
89fb70a3 DB |
3425 | { |
3426 | bool changed = false; | |
3427 | ssa_op_iter iter; | |
3428 | def_operand_p defp; | |
3429 | ||
3430 | FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS) | |
3431 | { | |
3432 | tree def = DEF_FROM_PTR (defp); | |
89fb70a3 DB |
3433 | changed |= set_ssa_val_to (def, def); |
3434 | } | |
3435 | return changed; | |
3436 | } | |
3437 | ||
3438 | /* Visit a copy between LHS and RHS, return true if the value number | |
3439 | changed. */ | |
3440 | ||
3441 | static bool | |
3442 | visit_copy (tree lhs, tree rhs) | |
3443 | { | |
34050b6b | 3444 | /* Valueize. */ |
0d5a1b56 | 3445 | rhs = SSA_VAL (rhs); |
89fb70a3 DB |
3446 | |
3447 | return set_ssa_val_to (lhs, rhs); | |
3448 | } | |
3449 | ||
2262707f | 3450 | /* Visit a nary operator RHS, value number it, and return true if the |
89fb70a3 DB |
3451 | value number of LHS has changed as a result. */ |
3452 | ||
3453 | static bool | |
355fe088 | 3454 | visit_nary_op (tree lhs, gimple *stmt) |
89fb70a3 DB |
3455 | { |
3456 | bool changed = false; | |
726a989a | 3457 | tree result = vn_nary_op_lookup_stmt (stmt, NULL); |
89fb70a3 DB |
3458 | |
3459 | if (result) | |
2262707f | 3460 | changed = set_ssa_val_to (lhs, result); |
726a989a RB |
3461 | else |
3462 | { | |
3463 | changed = set_ssa_val_to (lhs, lhs); | |
3464 | vn_nary_op_insert_stmt (stmt, lhs); | |
3465 | } | |
3466 | ||
3467 | return changed; | |
3468 | } | |
3469 | ||
3470 | /* Visit a call STMT storing into LHS. Return true if the value number | |
3471 | of the LHS has changed as a result. */ | |
3472 | ||
3473 | static bool | |
538dd0b7 | 3474 | visit_reference_op_call (tree lhs, gcall *stmt) |
89fb70a3 DB |
3475 | { |
3476 | bool changed = false; | |
726a989a | 3477 | struct vn_reference_s vr1; |
00115921 | 3478 | vn_reference_t vnresult = NULL; |
00115921 | 3479 | tree vdef = gimple_vdef (stmt); |
89fb70a3 | 3480 | |
6867d9a9 TV |
3481 | /* Non-ssa lhs is handled in copy_reference_ops_from_call. */ |
3482 | if (lhs && TREE_CODE (lhs) != SSA_NAME) | |
3483 | lhs = NULL_TREE; | |
3484 | ||
26f3a4e1 | 3485 | vn_reference_lookup_call (stmt, &vnresult, &vr1); |
00115921 | 3486 | if (vnresult) |
89fb70a3 | 3487 | { |
4583fada | 3488 | if (vnresult->result_vdef && vdef) |
00115921 | 3489 | changed |= set_ssa_val_to (vdef, vnresult->result_vdef); |
b5bbe47b RB |
3490 | else if (vdef) |
3491 | /* If the call was discovered to be pure or const reflect | |
3492 | that as far as possible. */ | |
3493 | changed |= set_ssa_val_to (vdef, vuse_ssa_val (gimple_vuse (stmt))); | |
00115921 TV |
3494 | |
3495 | if (!vnresult->result && lhs) | |
3496 | vnresult->result = lhs; | |
3497 | ||
3498 | if (vnresult->result && lhs) | |
34050b6b | 3499 | changed |= set_ssa_val_to (lhs, vnresult->result); |
89fb70a3 DB |
3500 | } |
3501 | else | |
3502 | { | |
726a989a | 3503 | vn_reference_t vr2; |
26f3a4e1 | 3504 | vn_reference_s **slot; |
113d06a4 | 3505 | tree vdef_val = vdef; |
00115921 | 3506 | if (vdef) |
113d06a4 RB |
3507 | { |
3508 | /* If we value numbered an indirect functions function to | |
3509 | one not clobbering memory value number its VDEF to its | |
3510 | VUSE. */ | |
3511 | tree fn = gimple_call_fn (stmt); | |
3512 | if (fn && TREE_CODE (fn) == SSA_NAME) | |
3513 | { | |
3514 | fn = SSA_VAL (fn); | |
3515 | if (TREE_CODE (fn) == ADDR_EXPR | |
3516 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL | |
3517 | && (flags_from_decl_or_type (TREE_OPERAND (fn, 0)) | |
3518 | & (ECF_CONST | ECF_PURE))) | |
3519 | vdef_val = vuse_ssa_val (gimple_vuse (stmt)); | |
3520 | } | |
3521 | changed |= set_ssa_val_to (vdef, vdef_val); | |
3522 | } | |
00115921 TV |
3523 | if (lhs) |
3524 | changed |= set_ssa_val_to (lhs, lhs); | |
af6a6eec | 3525 | vr2 = current_info->references_pool->allocate (); |
5006671f | 3526 | vr2->vuse = vr1.vuse; |
26f3a4e1 RB |
3527 | /* As we are not walking the virtual operand chain we know the |
3528 | shared_lookup_references are still original so we can re-use | |
3529 | them here. */ | |
3530 | vr2->operands = vr1.operands.copy (); | |
b45d2719 RG |
3531 | vr2->type = vr1.type; |
3532 | vr2->set = vr1.set; | |
726a989a RB |
3533 | vr2->hashcode = vr1.hashcode; |
3534 | vr2->result = lhs; | |
113d06a4 | 3535 | vr2->result_vdef = vdef_val; |
c203e8a7 TS |
3536 | slot = current_info->references->find_slot_with_hash (vr2, vr2->hashcode, |
3537 | INSERT); | |
26f3a4e1 | 3538 | gcc_assert (!*slot); |
726a989a | 3539 | *slot = vr2; |
89fb70a3 DB |
3540 | } |
3541 | ||
3542 | return changed; | |
3543 | } | |
3544 | ||
3545 | /* Visit a load from a reference operator RHS, part of STMT, value number it, | |
3546 | and return true if the value number of the LHS has changed as a result. */ | |
3547 | ||
3548 | static bool | |
355fe088 | 3549 | visit_reference_op_load (tree lhs, tree op, gimple *stmt) |
89fb70a3 DB |
3550 | { |
3551 | bool changed = false; | |
d0ca0bcb RG |
3552 | tree last_vuse; |
3553 | tree result; | |
3554 | ||
3555 | last_vuse = gimple_vuse (stmt); | |
3556 | last_vuse_ptr = &last_vuse; | |
1ec87690 | 3557 | result = vn_reference_lookup (op, gimple_vuse (stmt), |
1c48bff1 | 3558 | default_vn_walk_kind, NULL, true); |
d0ca0bcb | 3559 | last_vuse_ptr = NULL; |
89fb70a3 | 3560 | |
3d45dd59 RG |
3561 | /* We handle type-punning through unions by value-numbering based |
3562 | on offset and size of the access. Be prepared to handle a | |
3563 | type-mismatch here via creating a VIEW_CONVERT_EXPR. */ | |
3564 | if (result | |
3565 | && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op))) | |
3566 | { | |
3567 | /* We will be setting the value number of lhs to the value number | |
3568 | of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result). | |
3569 | So first simplify and lookup this expression to see if it | |
3570 | is already available. */ | |
c0f62740 RB |
3571 | code_helper rcode = VIEW_CONVERT_EXPR; |
3572 | tree ops[3] = { result }; | |
64ea4e15 | 3573 | result = vn_nary_build_or_lookup (rcode, TREE_TYPE (op), ops); |
3d45dd59 RG |
3574 | } |
3575 | ||
89fb70a3 | 3576 | if (result) |
34050b6b | 3577 | changed = set_ssa_val_to (lhs, result); |
89fb70a3 DB |
3578 | else |
3579 | { | |
3580 | changed = set_ssa_val_to (lhs, lhs); | |
4ec0a198 | 3581 | vn_reference_insert (op, lhs, last_vuse, NULL_TREE); |
89fb70a3 DB |
3582 | } |
3583 | ||
3584 | return changed; | |
3585 | } | |
3586 | ||
3587 | ||
3588 | /* Visit a store to a reference operator LHS, part of STMT, value number it, | |
3589 | and return true if the value number of the LHS has changed as a result. */ | |
3590 | ||
3591 | static bool | |
355fe088 | 3592 | visit_reference_op_store (tree lhs, tree op, gimple *stmt) |
89fb70a3 DB |
3593 | { |
3594 | bool changed = false; | |
4ec0a198 | 3595 | vn_reference_t vnresult = NULL; |
ca0e1607 | 3596 | tree assign; |
89fb70a3 | 3597 | bool resultsame = false; |
4ec0a198 TV |
3598 | tree vuse = gimple_vuse (stmt); |
3599 | tree vdef = gimple_vdef (stmt); | |
89fb70a3 | 3600 | |
703c9ccd RB |
3601 | if (TREE_CODE (op) == SSA_NAME) |
3602 | op = SSA_VAL (op); | |
3603 | ||
89fb70a3 DB |
3604 | /* First we want to lookup using the *vuses* from the store and see |
3605 | if there the last store to this location with the same address | |
3606 | had the same value. | |
3607 | ||
3608 | The vuses represent the memory state before the store. If the | |
3609 | memory state, address, and value of the store is the same as the | |
3610 | last store to this location, then this store will produce the | |
3611 | same memory state as that store. | |
3612 | ||
3613 | In this case the vdef versions for this store are value numbered to those | |
3614 | vuse versions, since they represent the same memory state after | |
3615 | this store. | |
3616 | ||
3617 | Otherwise, the vdefs for the store are used when inserting into | |
3618 | the table, since the store generates a new memory state. */ | |
3619 | ||
ca0e1607 RB |
3620 | vn_reference_lookup (lhs, vuse, VN_NOWALK, &vnresult, false); |
3621 | if (vnresult | |
3622 | && vnresult->result) | |
89fb70a3 | 3623 | { |
ca0e1607 | 3624 | tree result = vnresult->result; |
89fb70a3 DB |
3625 | if (TREE_CODE (result) == SSA_NAME) |
3626 | result = SSA_VAL (result); | |
3627 | resultsame = expressions_equal_p (result, op); | |
59896334 RB |
3628 | if (resultsame) |
3629 | { | |
3630 | /* If the TBAA state isn't compatible for downstream reads | |
3631 | we cannot value-number the VDEFs the same. */ | |
3632 | alias_set_type set = get_alias_set (lhs); | |
3633 | if (vnresult->set != set | |
3634 | && ! alias_set_subset_of (set, vnresult->set)) | |
3635 | resultsame = false; | |
3636 | } | |
89fb70a3 DB |
3637 | } |
3638 | ||
ca0e1607 RB |
3639 | if (!resultsame) |
3640 | { | |
26f3a4e1 RB |
3641 | /* Only perform the following when being called from PRE |
3642 | which embeds tail merging. */ | |
ca0e1607 | 3643 | if (default_vn_walk_kind == VN_WALK) |
4ec0a198 | 3644 | { |
ca0e1607 RB |
3645 | assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op); |
3646 | vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult, false); | |
3647 | if (vnresult) | |
3648 | { | |
3649 | VN_INFO (vdef)->use_processed = true; | |
3650 | return set_ssa_val_to (vdef, vnresult->result_vdef); | |
3651 | } | |
4ec0a198 | 3652 | } |
89fb70a3 DB |
3653 | |
3654 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3655 | { | |
3656 | fprintf (dump_file, "No store match\n"); | |
3657 | fprintf (dump_file, "Value numbering store "); | |
3658 | print_generic_expr (dump_file, lhs, 0); | |
3659 | fprintf (dump_file, " to "); | |
3660 | print_generic_expr (dump_file, op, 0); | |
3661 | fprintf (dump_file, "\n"); | |
3662 | } | |
3663 | /* Have to set value numbers before insert, since insert is | |
3664 | going to valueize the references in-place. */ | |
4ec0a198 | 3665 | if (vdef) |
ca0e1607 | 3666 | changed |= set_ssa_val_to (vdef, vdef); |
89fb70a3 | 3667 | |
9a327766 RG |
3668 | /* Do not insert structure copies into the tables. */ |
3669 | if (is_gimple_min_invariant (op) | |
3670 | || is_gimple_reg (op)) | |
4ec0a198 TV |
3671 | vn_reference_insert (lhs, op, vdef, NULL); |
3672 | ||
26f3a4e1 RB |
3673 | /* Only perform the following when being called from PRE |
3674 | which embeds tail merging. */ | |
3675 | if (default_vn_walk_kind == VN_WALK) | |
3676 | { | |
3677 | assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op); | |
3678 | vn_reference_insert (assign, lhs, vuse, vdef); | |
3679 | } | |
89fb70a3 DB |
3680 | } |
3681 | else | |
3682 | { | |
5006671f RG |
3683 | /* We had a match, so value number the vdef to have the value |
3684 | number of the vuse it came from. */ | |
89fb70a3 DB |
3685 | |
3686 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3687 | fprintf (dump_file, "Store matched earlier value," | |
3688 | "value numbering store vdefs to matching vuses.\n"); | |
3689 | ||
4ec0a198 | 3690 | changed |= set_ssa_val_to (vdef, SSA_VAL (vuse)); |
89fb70a3 DB |
3691 | } |
3692 | ||
3693 | return changed; | |
3694 | } | |
3695 | ||
3696 | /* Visit and value number PHI, return true if the value number | |
3697 | changed. */ | |
3698 | ||
3699 | static bool | |
355fe088 | 3700 | visit_phi (gimple *phi) |
89fb70a3 DB |
3701 | { |
3702 | bool changed = false; | |
3703 | tree result; | |
3704 | tree sameval = VN_TOP; | |
3705 | bool allsame = true; | |
94852c8e | 3706 | unsigned n_executable = 0; |
89fb70a3 | 3707 | |
62b0d9ec DJ |
3708 | /* TODO: We could check for this in init_sccvn, and replace this |
3709 | with a gcc_assert. */ | |
3710 | if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi))) | |
3711 | return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi)); | |
3712 | ||
89fb70a3 DB |
3713 | /* See if all non-TOP arguments have the same value. TOP is |
3714 | equivalent to everything, so we can ignore it. */ | |
a764d660 RB |
3715 | edge_iterator ei; |
3716 | edge e; | |
3717 | FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds) | |
3718 | if (e->flags & EDGE_EXECUTABLE) | |
3719 | { | |
3720 | tree def = PHI_ARG_DEF_FROM_EDGE (phi, e); | |
89fb70a3 | 3721 | |
94852c8e | 3722 | ++n_executable; |
a764d660 RB |
3723 | if (TREE_CODE (def) == SSA_NAME) |
3724 | def = SSA_VAL (def); | |
3725 | if (def == VN_TOP) | |
3726 | continue; | |
3727 | if (sameval == VN_TOP) | |
9fe4f60a RB |
3728 | sameval = def; |
3729 | else if (!expressions_equal_p (def, sameval)) | |
a764d660 | 3730 | { |
9fe4f60a RB |
3731 | allsame = false; |
3732 | break; | |
a764d660 RB |
3733 | } |
3734 | } | |
28251e2c RB |
3735 | |
3736 | /* If none of the edges was executable or all incoming values are | |
94852c8e RB |
3737 | undefined keep the value-number at VN_TOP. If only a single edge |
3738 | is exectuable use its value. */ | |
3739 | if (sameval == VN_TOP | |
3740 | || n_executable == 1) | |
3741 | return set_ssa_val_to (PHI_RESULT (phi), sameval); | |
89fb70a3 | 3742 | |
9fe4f60a RB |
3743 | /* First see if it is equivalent to a phi node in this block. We prefer |
3744 | this as it allows IV elimination - see PRs 66502 and 67167. */ | |
89fb70a3 DB |
3745 | result = vn_phi_lookup (phi); |
3746 | if (result) | |
c1604254 | 3747 | changed = set_ssa_val_to (PHI_RESULT (phi), result); |
9fe4f60a RB |
3748 | /* Otherwise all value numbered to the same value, the phi node has that |
3749 | value. */ | |
3750 | else if (allsame) | |
3751 | changed = set_ssa_val_to (PHI_RESULT (phi), sameval); | |
89fb70a3 DB |
3752 | else |
3753 | { | |
3754 | vn_phi_insert (phi, PHI_RESULT (phi)); | |
89fb70a3 DB |
3755 | changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi)); |
3756 | } | |
3757 | ||
3758 | return changed; | |
3759 | } | |
3760 | ||
89fb70a3 DB |
3761 | /* Try to simplify RHS using equivalences and constant folding. */ |
3762 | ||
3763 | static tree | |
538dd0b7 | 3764 | try_to_simplify (gassign *stmt) |
89fb70a3 | 3765 | { |
d3878abf | 3766 | enum tree_code code = gimple_assign_rhs_code (stmt); |
ed97ddc6 RG |
3767 | tree tem; |
3768 | ||
5be891a4 RG |
3769 | /* For stores we can end up simplifying a SSA_NAME rhs. Just return |
3770 | in this case, there is no point in doing extra work. */ | |
d3878abf | 3771 | if (code == SSA_NAME) |
726a989a | 3772 | return NULL_TREE; |
ed97ddc6 | 3773 | |
cfef45c8 | 3774 | /* First try constant folding based on our current lattice. */ |
34050b6b | 3775 | mprts_hook = vn_lookup_simplify_result; |
ff734093 | 3776 | tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize, vn_valueize); |
34050b6b | 3777 | mprts_hook = NULL; |
d3878abf RG |
3778 | if (tem |
3779 | && (TREE_CODE (tem) == SSA_NAME | |
3780 | || is_gimple_min_invariant (tem))) | |
cfef45c8 RG |
3781 | return tem; |
3782 | ||
726a989a | 3783 | return NULL_TREE; |
89fb70a3 DB |
3784 | } |
3785 | ||
3786 | /* Visit and value number USE, return true if the value number | |
3787 | changed. */ | |
3788 | ||
3789 | static bool | |
3790 | visit_use (tree use) | |
3791 | { | |
3792 | bool changed = false; | |
355fe088 | 3793 | gimple *stmt = SSA_NAME_DEF_STMT (use); |
89fb70a3 | 3794 | |
00115921 | 3795 | mark_use_processed (use); |
89fb70a3 DB |
3796 | |
3797 | gcc_assert (!SSA_NAME_IN_FREE_LIST (use)); | |
3d45dd59 | 3798 | if (dump_file && (dump_flags & TDF_DETAILS) |
726a989a | 3799 | && !SSA_NAME_IS_DEFAULT_DEF (use)) |
89fb70a3 DB |
3800 | { |
3801 | fprintf (dump_file, "Value numbering "); | |
3802 | print_generic_expr (dump_file, use, 0); | |
3803 | fprintf (dump_file, " stmt = "); | |
726a989a | 3804 | print_gimple_stmt (dump_file, stmt, 0, 0); |
89fb70a3 DB |
3805 | } |
3806 | ||
89fb70a3 | 3807 | /* Handle uninitialized uses. */ |
726a989a RB |
3808 | if (SSA_NAME_IS_DEFAULT_DEF (use)) |
3809 | changed = set_ssa_val_to (use, use); | |
6a8b77b2 RB |
3810 | else if (gimple_code (stmt) == GIMPLE_PHI) |
3811 | changed = visit_phi (stmt); | |
3812 | else if (gimple_has_volatile_ops (stmt)) | |
3813 | changed = defs_to_varying (stmt); | |
3814 | else if (gassign *ass = dyn_cast <gassign *> (stmt)) | |
3815 | { | |
3816 | enum tree_code code = gimple_assign_rhs_code (ass); | |
3817 | tree lhs = gimple_assign_lhs (ass); | |
3818 | tree rhs1 = gimple_assign_rhs1 (ass); | |
3819 | tree simplified; | |
3820 | ||
3821 | /* Shortcut for copies. Simplifying copies is pointless, | |
3822 | since we copy the expression and value they represent. */ | |
3823 | if (code == SSA_NAME | |
3824 | && TREE_CODE (lhs) == SSA_NAME) | |
3825 | { | |
3826 | changed = visit_copy (lhs, rhs1); | |
3827 | goto done; | |
3828 | } | |
3829 | simplified = try_to_simplify (ass); | |
3830 | if (simplified) | |
89fb70a3 | 3831 | { |
6a8b77b2 | 3832 | if (dump_file && (dump_flags & TDF_DETAILS)) |
8b0a5125 | 3833 | { |
6a8b77b2 RB |
3834 | fprintf (dump_file, "RHS "); |
3835 | print_gimple_expr (dump_file, ass, 0, 0); | |
3836 | fprintf (dump_file, " simplified to "); | |
3837 | print_generic_expr (dump_file, simplified, 0); | |
3838 | fprintf (dump_file, "\n"); | |
3839 | } | |
3840 | } | |
3841 | /* Setting value numbers to constants will occasionally | |
3842 | screw up phi congruence because constants are not | |
3843 | uniquely associated with a single ssa name that can be | |
3844 | looked up. */ | |
3845 | if (simplified | |
3846 | && is_gimple_min_invariant (simplified) | |
3847 | && TREE_CODE (lhs) == SSA_NAME) | |
3848 | { | |
3849 | changed = set_ssa_val_to (lhs, simplified); | |
3850 | goto done; | |
3851 | } | |
3852 | else if (simplified | |
3853 | && TREE_CODE (simplified) == SSA_NAME | |
3854 | && TREE_CODE (lhs) == SSA_NAME) | |
3855 | { | |
3856 | changed = visit_copy (lhs, simplified); | |
3857 | goto done; | |
3858 | } | |
3859 | ||
3860 | if ((TREE_CODE (lhs) == SSA_NAME | |
3861 | /* We can substitute SSA_NAMEs that are live over | |
3862 | abnormal edges with their constant value. */ | |
3863 | && !(gimple_assign_copy_p (ass) | |
3864 | && is_gimple_min_invariant (rhs1)) | |
3865 | && !(simplified | |
3866 | && is_gimple_min_invariant (simplified)) | |
3867 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)) | |
3868 | /* Stores or copies from SSA_NAMEs that are live over | |
3869 | abnormal edges are a problem. */ | |
3870 | || (code == SSA_NAME | |
3871 | && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))) | |
3872 | changed = defs_to_varying (ass); | |
3873 | else if (REFERENCE_CLASS_P (lhs) | |
3874 | || DECL_P (lhs)) | |
3875 | changed = visit_reference_op_store (lhs, rhs1, ass); | |
3876 | else if (TREE_CODE (lhs) == SSA_NAME) | |
3877 | { | |
3878 | if ((gimple_assign_copy_p (ass) | |
3879 | && is_gimple_min_invariant (rhs1)) | |
3880 | || (simplified | |
3881 | && is_gimple_min_invariant (simplified))) | |
3882 | { | |
3883 | if (simplified) | |
3884 | changed = set_ssa_val_to (lhs, simplified); | |
3885 | else | |
3886 | changed = set_ssa_val_to (lhs, rhs1); | |
3887 | } | |
3888 | else | |
3889 | { | |
3890 | /* Visit the original statement. */ | |
3891 | switch (vn_get_stmt_kind (ass)) | |
3892 | { | |
3893 | case VN_NARY: | |
3894 | changed = visit_nary_op (lhs, ass); | |
3895 | break; | |
3896 | case VN_REFERENCE: | |
3897 | changed = visit_reference_op_load (lhs, rhs1, ass); | |
3898 | break; | |
3899 | default: | |
3900 | changed = defs_to_varying (ass); | |
3901 | break; | |
3902 | } | |
8b0a5125 | 3903 | } |
6a8b77b2 RB |
3904 | } |
3905 | else | |
3906 | changed = defs_to_varying (ass); | |
3907 | } | |
3908 | else if (gcall *call_stmt = dyn_cast <gcall *> (stmt)) | |
3909 | { | |
3910 | tree lhs = gimple_call_lhs (call_stmt); | |
3911 | if (lhs && TREE_CODE (lhs) == SSA_NAME) | |
3912 | { | |
3913 | /* Try constant folding based on our current lattice. */ | |
3914 | tree simplified = gimple_fold_stmt_to_constant_1 (call_stmt, | |
3915 | vn_valueize); | |
726a989a | 3916 | if (simplified) |
89fb70a3 DB |
3917 | { |
3918 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3919 | { | |
6a8b77b2 RB |
3920 | fprintf (dump_file, "call "); |
3921 | print_gimple_expr (dump_file, call_stmt, 0, 0); | |
89fb70a3 DB |
3922 | fprintf (dump_file, " simplified to "); |
3923 | print_generic_expr (dump_file, simplified, 0); | |
34050b6b | 3924 | fprintf (dump_file, "\n"); |
89fb70a3 DB |
3925 | } |
3926 | } | |
3927 | /* Setting value numbers to constants will occasionally | |
3928 | screw up phi congruence because constants are not | |
3929 | uniquely associated with a single ssa name that can be | |
3930 | looked up. */ | |
726a989a | 3931 | if (simplified |
6a8b77b2 | 3932 | && is_gimple_min_invariant (simplified)) |
89fb70a3 | 3933 | { |
89fb70a3 | 3934 | changed = set_ssa_val_to (lhs, simplified); |
6a8b77b2 RB |
3935 | if (gimple_vdef (call_stmt)) |
3936 | changed |= set_ssa_val_to (gimple_vdef (call_stmt), | |
3937 | SSA_VAL (gimple_vuse (call_stmt))); | |
89fb70a3 DB |
3938 | goto done; |
3939 | } | |
726a989a | 3940 | else if (simplified |
6a8b77b2 | 3941 | && TREE_CODE (simplified) == SSA_NAME) |
89fb70a3 DB |
3942 | { |
3943 | changed = visit_copy (lhs, simplified); | |
6a8b77b2 RB |
3944 | if (gimple_vdef (call_stmt)) |
3945 | changed |= set_ssa_val_to (gimple_vdef (call_stmt), | |
3946 | SSA_VAL (gimple_vuse (call_stmt))); | |
89fb70a3 DB |
3947 | goto done; |
3948 | } | |
6a8b77b2 | 3949 | else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)) |
89fb70a3 | 3950 | { |
6a8b77b2 RB |
3951 | changed = defs_to_varying (call_stmt); |
3952 | goto done; | |
89fb70a3 | 3953 | } |
89fb70a3 | 3954 | } |
726a989a | 3955 | |
113d06a4 RB |
3956 | /* Pick up flags from a devirtualization target. */ |
3957 | tree fn = gimple_call_fn (stmt); | |
3958 | int extra_fnflags = 0; | |
3959 | if (fn && TREE_CODE (fn) == SSA_NAME) | |
3960 | { | |
3961 | fn = SSA_VAL (fn); | |
3962 | if (TREE_CODE (fn) == ADDR_EXPR | |
3963 | && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL) | |
3964 | extra_fnflags = flags_from_decl_or_type (TREE_OPERAND (fn, 0)); | |
3965 | } | |
6a8b77b2 RB |
3966 | if (!gimple_call_internal_p (call_stmt) |
3967 | && (/* Calls to the same function with the same vuse | |
3968 | and the same operands do not necessarily return the same | |
3969 | value, unless they're pure or const. */ | |
113d06a4 RB |
3970 | ((gimple_call_flags (call_stmt) | extra_fnflags) |
3971 | & (ECF_PURE | ECF_CONST)) | |
6a8b77b2 RB |
3972 | /* If calls have a vdef, subsequent calls won't have |
3973 | the same incoming vuse. So, if 2 calls with vdef have the | |
3974 | same vuse, we know they're not subsequent. | |
3975 | We can value number 2 calls to the same function with the | |
3976 | same vuse and the same operands which are not subsequent | |
3977 | the same, because there is no code in the program that can | |
3978 | compare the 2 values... */ | |
3979 | || (gimple_vdef (call_stmt) | |
3980 | /* ... unless the call returns a pointer which does | |
3981 | not alias with anything else. In which case the | |
3982 | information that the values are distinct are encoded | |
3983 | in the IL. */ | |
3984 | && !(gimple_call_return_flags (call_stmt) & ERF_NOALIAS) | |
3985 | /* Only perform the following when being called from PRE | |
3986 | which embeds tail merging. */ | |
3987 | && default_vn_walk_kind == VN_WALK))) | |
3988 | changed = visit_reference_op_call (lhs, call_stmt); | |
00115921 | 3989 | else |
6a8b77b2 | 3990 | changed = defs_to_varying (call_stmt); |
89fb70a3 | 3991 | } |
6a8b77b2 RB |
3992 | else |
3993 | changed = defs_to_varying (stmt); | |
89fb70a3 DB |
3994 | done: |
3995 | return changed; | |
3996 | } | |
3997 | ||
3998 | /* Compare two operands by reverse postorder index */ | |
3999 | ||
4000 | static int | |
4001 | compare_ops (const void *pa, const void *pb) | |
4002 | { | |
4003 | const tree opa = *((const tree *)pa); | |
4004 | const tree opb = *((const tree *)pb); | |
355fe088 TS |
4005 | gimple *opstmta = SSA_NAME_DEF_STMT (opa); |
4006 | gimple *opstmtb = SSA_NAME_DEF_STMT (opb); | |
89fb70a3 DB |
4007 | basic_block bba; |
4008 | basic_block bbb; | |
4009 | ||
726a989a | 4010 | if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb)) |
3d8fa148 | 4011 | return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb); |
726a989a | 4012 | else if (gimple_nop_p (opstmta)) |
89fb70a3 | 4013 | return -1; |
726a989a | 4014 | else if (gimple_nop_p (opstmtb)) |
89fb70a3 DB |
4015 | return 1; |
4016 | ||
726a989a RB |
4017 | bba = gimple_bb (opstmta); |
4018 | bbb = gimple_bb (opstmtb); | |
89fb70a3 DB |
4019 | |
4020 | if (!bba && !bbb) | |
3d8fa148 | 4021 | return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb); |
89fb70a3 DB |
4022 | else if (!bba) |
4023 | return -1; | |
4024 | else if (!bbb) | |
4025 | return 1; | |
4026 | ||
4027 | if (bba == bbb) | |
4028 | { | |
726a989a RB |
4029 | if (gimple_code (opstmta) == GIMPLE_PHI |
4030 | && gimple_code (opstmtb) == GIMPLE_PHI) | |
3d8fa148 | 4031 | return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb); |
726a989a | 4032 | else if (gimple_code (opstmta) == GIMPLE_PHI) |
89fb70a3 | 4033 | return -1; |
726a989a | 4034 | else if (gimple_code (opstmtb) == GIMPLE_PHI) |
89fb70a3 | 4035 | return 1; |
3d8fa148 DK |
4036 | else if (gimple_uid (opstmta) != gimple_uid (opstmtb)) |
4037 | return gimple_uid (opstmta) - gimple_uid (opstmtb); | |
4038 | else | |
4039 | return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb); | |
89fb70a3 DB |
4040 | } |
4041 | return rpo_numbers[bba->index] - rpo_numbers[bbb->index]; | |
4042 | } | |
4043 | ||
4044 | /* Sort an array containing members of a strongly connected component | |
4045 | SCC so that the members are ordered by RPO number. | |
4046 | This means that when the sort is complete, iterating through the | |
4047 | array will give you the members in RPO order. */ | |
4048 | ||
4049 | static void | |
9771b263 | 4050 | sort_scc (vec<tree> scc) |
89fb70a3 | 4051 | { |
9771b263 | 4052 | scc.qsort (compare_ops); |
89fb70a3 DB |
4053 | } |
4054 | ||
880ad25f | 4055 | /* Insert the no longer used nary ONARY to the hash INFO. */ |
c82e0b3b | 4056 | |
880ad25f RG |
4057 | static void |
4058 | copy_nary (vn_nary_op_t onary, vn_tables_t info) | |
c82e0b3b | 4059 | { |
9ad6bebe NF |
4060 | size_t size = sizeof_vn_nary_op (onary->length); |
4061 | vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length, | |
4062 | &info->nary_obstack); | |
c82e0b3b | 4063 | memcpy (nary, onary, size); |
9ad6bebe | 4064 | vn_nary_op_insert_into (nary, info->nary, false); |
c82e0b3b RG |
4065 | } |
4066 | ||
880ad25f | 4067 | /* Insert the no longer used phi OPHI to the hash INFO. */ |
c82e0b3b | 4068 | |
880ad25f RG |
4069 | static void |
4070 | copy_phi (vn_phi_t ophi, vn_tables_t info) | |
c82e0b3b | 4071 | { |
af6a6eec | 4072 | vn_phi_t phi = info->phis_pool->allocate (); |
bf190e8d | 4073 | vn_phi_s **slot; |
c82e0b3b | 4074 | memcpy (phi, ophi, sizeof (*phi)); |
9771b263 | 4075 | ophi->phiargs.create (0); |
c203e8a7 | 4076 | slot = info->phis->find_slot_with_hash (phi, phi->hashcode, INSERT); |
880ad25f | 4077 | gcc_assert (!*slot); |
c82e0b3b | 4078 | *slot = phi; |
c82e0b3b RG |
4079 | } |
4080 | ||
880ad25f | 4081 | /* Insert the no longer used reference OREF to the hash INFO. */ |
c82e0b3b | 4082 | |
880ad25f RG |
4083 | static void |
4084 | copy_reference (vn_reference_t oref, vn_tables_t info) | |
c82e0b3b | 4085 | { |
c82e0b3b | 4086 | vn_reference_t ref; |
bf190e8d | 4087 | vn_reference_s **slot; |
af6a6eec | 4088 | ref = info->references_pool->allocate (); |
c82e0b3b | 4089 | memcpy (ref, oref, sizeof (*ref)); |
9771b263 | 4090 | oref->operands.create (0); |
c203e8a7 | 4091 | slot = info->references->find_slot_with_hash (ref, ref->hashcode, INSERT); |
c82e0b3b RG |
4092 | if (*slot) |
4093 | free_reference (*slot); | |
4094 | *slot = ref; | |
c82e0b3b RG |
4095 | } |
4096 | ||
89fb70a3 DB |
4097 | /* Process a strongly connected component in the SSA graph. */ |
4098 | ||
4099 | static void | |
9771b263 | 4100 | process_scc (vec<tree> scc) |
89fb70a3 | 4101 | { |
880ad25f RG |
4102 | tree var; |
4103 | unsigned int i; | |
4104 | unsigned int iterations = 0; | |
4105 | bool changed = true; | |
bf190e8d LC |
4106 | vn_nary_op_iterator_type hin; |
4107 | vn_phi_iterator_type hip; | |
4108 | vn_reference_iterator_type hir; | |
880ad25f RG |
4109 | vn_nary_op_t nary; |
4110 | vn_phi_t phi; | |
4111 | vn_reference_t ref; | |
89fb70a3 | 4112 | |
880ad25f | 4113 | /* If the SCC has a single member, just visit it. */ |
9771b263 | 4114 | if (scc.length () == 1) |
89fb70a3 | 4115 | { |
9771b263 | 4116 | tree use = scc[0]; |
72a07d9b RB |
4117 | if (VN_INFO (use)->use_processed) |
4118 | return; | |
4119 | /* We need to make sure it doesn't form a cycle itself, which can | |
4120 | happen for self-referential PHI nodes. In that case we would | |
4121 | end up inserting an expression with VN_TOP operands into the | |
4122 | valid table which makes us derive bogus equivalences later. | |
4123 | The cheapest way to check this is to assume it for all PHI nodes. */ | |
4124 | if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI) | |
4125 | /* Fallthru to iteration. */ ; | |
4126 | else | |
4127 | { | |
4128 | visit_use (use); | |
4129 | return; | |
4130 | } | |
89fb70a3 | 4131 | } |
880ad25f | 4132 | |
b2b222b3 RB |
4133 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4134 | print_scc (dump_file, scc); | |
4135 | ||
880ad25f RG |
4136 | /* Iterate over the SCC with the optimistic table until it stops |
4137 | changing. */ | |
4138 | current_info = optimistic_info; | |
4139 | while (changed) | |
89fb70a3 | 4140 | { |
880ad25f RG |
4141 | changed = false; |
4142 | iterations++; | |
90bc4623 RG |
4143 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4144 | fprintf (dump_file, "Starting iteration %d\n", iterations); | |
880ad25f RG |
4145 | /* As we are value-numbering optimistically we have to |
4146 | clear the expression tables and the simplified expressions | |
4147 | in each iteration until we converge. */ | |
c203e8a7 TS |
4148 | optimistic_info->nary->empty (); |
4149 | optimistic_info->phis->empty (); | |
4150 | optimistic_info->references->empty (); | |
880ad25f RG |
4151 | obstack_free (&optimistic_info->nary_obstack, NULL); |
4152 | gcc_obstack_init (&optimistic_info->nary_obstack); | |
af6a6eec ML |
4153 | optimistic_info->phis_pool->release (); |
4154 | optimistic_info->references_pool->release (); | |
9771b263 | 4155 | FOR_EACH_VEC_ELT (scc, i, var) |
34050b6b RB |
4156 | gcc_assert (!VN_INFO (var)->needs_insertion |
4157 | && VN_INFO (var)->expr == NULL); | |
9771b263 | 4158 | FOR_EACH_VEC_ELT (scc, i, var) |
880ad25f RG |
4159 | changed |= visit_use (var); |
4160 | } | |
89fb70a3 | 4161 | |
b2b222b3 RB |
4162 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4163 | fprintf (dump_file, "Processing SCC needed %d iterations\n", iterations); | |
880ad25f | 4164 | statistics_histogram_event (cfun, "SCC iterations", iterations); |
89fb70a3 | 4165 | |
880ad25f RG |
4166 | /* Finally, copy the contents of the no longer used optimistic |
4167 | table to the valid table. */ | |
c203e8a7 | 4168 | FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->nary, nary, vn_nary_op_t, hin) |
880ad25f | 4169 | copy_nary (nary, valid_info); |
c203e8a7 | 4170 | FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->phis, phi, vn_phi_t, hip) |
880ad25f | 4171 | copy_phi (phi, valid_info); |
c203e8a7 | 4172 | FOR_EACH_HASH_TABLE_ELEMENT (*optimistic_info->references, |
bf190e8d | 4173 | ref, vn_reference_t, hir) |
880ad25f RG |
4174 | copy_reference (ref, valid_info); |
4175 | ||
4176 | current_info = valid_info; | |
89fb70a3 DB |
4177 | } |
4178 | ||
6be34936 RG |
4179 | |
4180 | /* Pop the components of the found SCC for NAME off the SCC stack | |
4181 | and process them. Returns true if all went well, false if | |
4182 | we run into resource limits. */ | |
4183 | ||
4184 | static bool | |
4185 | extract_and_process_scc_for_name (tree name) | |
4186 | { | |
ef062b13 | 4187 | auto_vec<tree> scc; |
6be34936 RG |
4188 | tree x; |
4189 | ||
4190 | /* Found an SCC, pop the components off the SCC stack and | |
4191 | process them. */ | |
4192 | do | |
4193 | { | |
9771b263 | 4194 | x = sccstack.pop (); |
6be34936 RG |
4195 | |
4196 | VN_INFO (x)->on_sccstack = false; | |
9771b263 | 4197 | scc.safe_push (x); |
6be34936 RG |
4198 | } while (x != name); |
4199 | ||
4200 | /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */ | |
9771b263 | 4201 | if (scc.length () |
6be34936 RG |
4202 | > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE)) |
4203 | { | |
4204 | if (dump_file) | |
4205 | fprintf (dump_file, "WARNING: Giving up with SCCVN due to " | |
9771b263 | 4206 | "SCC size %u exceeding %u\n", scc.length (), |
6be34936 | 4207 | (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE)); |
f5843d08 | 4208 | |
6be34936 RG |
4209 | return false; |
4210 | } | |
4211 | ||
9771b263 | 4212 | if (scc.length () > 1) |
6be34936 RG |
4213 | sort_scc (scc); |
4214 | ||
6be34936 RG |
4215 | process_scc (scc); |
4216 | ||
6be34936 RG |
4217 | return true; |
4218 | } | |
4219 | ||
89fb70a3 DB |
4220 | /* Depth first search on NAME to discover and process SCC's in the SSA |
4221 | graph. | |
4222 | Execution of this algorithm relies on the fact that the SCC's are | |
863d2a57 RG |
4223 | popped off the stack in topological order. |
4224 | Returns true if successful, false if we stopped processing SCC's due | |
fa10beec | 4225 | to resource constraints. */ |
89fb70a3 | 4226 | |
863d2a57 | 4227 | static bool |
89fb70a3 DB |
4228 | DFS (tree name) |
4229 | { | |
8c681247 TS |
4230 | auto_vec<ssa_op_iter> itervec; |
4231 | auto_vec<tree> namevec; | |
6be34936 | 4232 | use_operand_p usep = NULL; |
355fe088 | 4233 | gimple *defstmt; |
726a989a | 4234 | tree use; |
89fb70a3 | 4235 | ssa_op_iter iter; |
89fb70a3 | 4236 | |
6be34936 | 4237 | start_over: |
89fb70a3 DB |
4238 | /* SCC info */ |
4239 | VN_INFO (name)->dfsnum = next_dfs_num++; | |
4240 | VN_INFO (name)->visited = true; | |
4241 | VN_INFO (name)->low = VN_INFO (name)->dfsnum; | |
4242 | ||
9771b263 | 4243 | sccstack.safe_push (name); |
89fb70a3 DB |
4244 | VN_INFO (name)->on_sccstack = true; |
4245 | defstmt = SSA_NAME_DEF_STMT (name); | |
4246 | ||
4247 | /* Recursively DFS on our operands, looking for SCC's. */ | |
726a989a | 4248 | if (!gimple_nop_p (defstmt)) |
89fb70a3 | 4249 | { |
6be34936 | 4250 | /* Push a new iterator. */ |
538dd0b7 DM |
4251 | if (gphi *phi = dyn_cast <gphi *> (defstmt)) |
4252 | usep = op_iter_init_phiuse (&iter, phi, SSA_OP_ALL_USES); | |
6be34936 RG |
4253 | else |
4254 | usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES); | |
4255 | } | |
4256 | else | |
81f5094d | 4257 | clear_and_done_ssa_iter (&iter); |
6be34936 RG |
4258 | |
4259 | while (1) | |
4260 | { | |
4261 | /* If we are done processing uses of a name, go up the stack | |
4262 | of iterators and process SCCs as we found them. */ | |
4263 | if (op_iter_done (&iter)) | |
89fb70a3 | 4264 | { |
6be34936 RG |
4265 | /* See if we found an SCC. */ |
4266 | if (VN_INFO (name)->low == VN_INFO (name)->dfsnum) | |
4267 | if (!extract_and_process_scc_for_name (name)) | |
8c681247 | 4268 | return false; |
89fb70a3 | 4269 | |
6be34936 | 4270 | /* Check if we are done. */ |
9771b263 | 4271 | if (namevec.is_empty ()) |
8c681247 | 4272 | return true; |
6be34936 RG |
4273 | |
4274 | /* Restore the last use walker and continue walking there. */ | |
4275 | use = name; | |
9771b263 DN |
4276 | name = namevec.pop (); |
4277 | memcpy (&iter, &itervec.last (), | |
6be34936 | 4278 | sizeof (ssa_op_iter)); |
9771b263 | 4279 | itervec.pop (); |
6be34936 RG |
4280 | goto continue_walking; |
4281 | } | |
89fb70a3 | 4282 | |
6be34936 RG |
4283 | use = USE_FROM_PTR (usep); |
4284 | ||
4285 | /* Since we handle phi nodes, we will sometimes get | |
4286 | invariants in the use expression. */ | |
4287 | if (TREE_CODE (use) == SSA_NAME) | |
4288 | { | |
89fb70a3 DB |
4289 | if (! (VN_INFO (use)->visited)) |
4290 | { | |
6be34936 RG |
4291 | /* Recurse by pushing the current use walking state on |
4292 | the stack and starting over. */ | |
9771b263 DN |
4293 | itervec.safe_push (iter); |
4294 | namevec.safe_push (name); | |
6be34936 RG |
4295 | name = use; |
4296 | goto start_over; | |
4297 | ||
4298 | continue_walking: | |
89fb70a3 DB |
4299 | VN_INFO (name)->low = MIN (VN_INFO (name)->low, |
4300 | VN_INFO (use)->low); | |
4301 | } | |
4302 | if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum | |
4303 | && VN_INFO (use)->on_sccstack) | |
4304 | { | |
4305 | VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum, | |
4306 | VN_INFO (name)->low); | |
4307 | } | |
4308 | } | |
863d2a57 | 4309 | |
6be34936 | 4310 | usep = op_iter_next_use (&iter); |
89fb70a3 DB |
4311 | } |
4312 | } | |
4313 | ||
89fb70a3 DB |
4314 | /* Allocate a value number table. */ |
4315 | ||
4316 | static void | |
4317 | allocate_vn_table (vn_tables_t table) | |
4318 | { | |
c203e8a7 TS |
4319 | table->phis = new vn_phi_table_type (23); |
4320 | table->nary = new vn_nary_op_table_type (23); | |
4321 | table->references = new vn_reference_table_type (23); | |
89fb70a3 | 4322 | |
49a1fb2d | 4323 | gcc_obstack_init (&table->nary_obstack); |
fcb87c50 | 4324 | table->phis_pool = new object_allocator<vn_phi_s> ("VN phis"); |
fb0b2914 | 4325 | table->references_pool = new object_allocator<vn_reference_s> |
fcb87c50 | 4326 | ("VN references"); |
89fb70a3 DB |
4327 | } |
4328 | ||
4329 | /* Free a value number table. */ | |
4330 | ||
4331 | static void | |
4332 | free_vn_table (vn_tables_t table) | |
4333 | { | |
c203e8a7 TS |
4334 | delete table->phis; |
4335 | table->phis = NULL; | |
4336 | delete table->nary; | |
4337 | table->nary = NULL; | |
4338 | delete table->references; | |
4339 | table->references = NULL; | |
49a1fb2d | 4340 | obstack_free (&table->nary_obstack, NULL); |
af6a6eec ML |
4341 | delete table->phis_pool; |
4342 | delete table->references_pool; | |
89fb70a3 DB |
4343 | } |
4344 | ||
4345 | static void | |
4346 | init_scc_vn (void) | |
4347 | { | |
89fb70a3 DB |
4348 | int j; |
4349 | int *rpo_numbers_temp; | |
89fb70a3 DB |
4350 | |
4351 | calculate_dominance_info (CDI_DOMINATORS); | |
9fe4f60a RB |
4352 | mark_dfs_back_edges (); |
4353 | ||
9771b263 | 4354 | sccstack.create (0); |
c203e8a7 | 4355 | constant_to_value_id = new hash_table<vn_constant_hasher> (23); |
b8698a0f | 4356 | |
c9145754 | 4357 | constant_value_ids = BITMAP_ALLOC (NULL); |
b8698a0f | 4358 | |
89fb70a3 | 4359 | next_dfs_num = 1; |
c9145754 | 4360 | next_value_id = 1; |
b8698a0f | 4361 | |
9771b263 | 4362 | vn_ssa_aux_table.create (num_ssa_names + 1); |
89fb70a3 DB |
4363 | /* VEC_alloc doesn't actually grow it to the right size, it just |
4364 | preallocates the space to do so. */ | |
9771b263 | 4365 | vn_ssa_aux_table.safe_grow_cleared (num_ssa_names + 1); |
cbfb21c1 SB |
4366 | gcc_obstack_init (&vn_ssa_aux_obstack); |
4367 | ||
9771b263 DN |
4368 | shared_lookup_phiargs.create (0); |
4369 | shared_lookup_references.create (0); | |
8b1c6fd7 | 4370 | rpo_numbers = XNEWVEC (int, last_basic_block_for_fn (cfun)); |
0cae8d31 DM |
4371 | rpo_numbers_temp = |
4372 | XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS); | |
89fb70a3 DB |
4373 | pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false); |
4374 | ||
4375 | /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that | |
4376 | the i'th block in RPO order is bb. We want to map bb's to RPO | |
4377 | numbers, so we need to rearrange this array. */ | |
0cae8d31 | 4378 | for (j = 0; j < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; j++) |
89fb70a3 DB |
4379 | rpo_numbers[rpo_numbers_temp[j]] = j; |
4380 | ||
cbfb21c1 | 4381 | XDELETE (rpo_numbers_temp); |
89fb70a3 DB |
4382 | |
4383 | VN_TOP = create_tmp_var_raw (void_type_node, "vn_top"); | |
4384 | ||
908ff6a3 | 4385 | renumber_gimple_stmt_uids (); |
89fb70a3 DB |
4386 | |
4387 | /* Create the valid and optimistic value numbering tables. */ | |
4388 | valid_info = XCNEW (struct vn_tables_s); | |
4389 | allocate_vn_table (valid_info); | |
4390 | optimistic_info = XCNEW (struct vn_tables_s); | |
4391 | allocate_vn_table (optimistic_info); | |
34c89697 RB |
4392 | current_info = valid_info; |
4393 | ||
4394 | /* Create the VN_INFO structures, and initialize value numbers to | |
4395 | TOP or VARYING for parameters. */ | |
46aa019a KV |
4396 | size_t i; |
4397 | tree name; | |
34c89697 | 4398 | |
46aa019a KV |
4399 | FOR_EACH_SSA_NAME (i, name, cfun) |
4400 | { | |
34c89697 | 4401 | VN_INFO_GET (name)->valnum = VN_TOP; |
34050b6b RB |
4402 | VN_INFO (name)->needs_insertion = false; |
4403 | VN_INFO (name)->expr = NULL; | |
34c89697 RB |
4404 | VN_INFO (name)->value_id = 0; |
4405 | ||
4406 | if (!SSA_NAME_IS_DEFAULT_DEF (name)) | |
4407 | continue; | |
4408 | ||
4409 | switch (TREE_CODE (SSA_NAME_VAR (name))) | |
4410 | { | |
4411 | case VAR_DECL: | |
4412 | /* Undefined vars keep TOP. */ | |
4413 | break; | |
4414 | ||
4415 | case PARM_DECL: | |
4416 | /* Parameters are VARYING but we can record a condition | |
4417 | if we know it is a non-NULL pointer. */ | |
4418 | VN_INFO (name)->visited = true; | |
4419 | VN_INFO (name)->valnum = name; | |
4420 | if (POINTER_TYPE_P (TREE_TYPE (name)) | |
4421 | && nonnull_arg_p (SSA_NAME_VAR (name))) | |
4422 | { | |
4423 | tree ops[2]; | |
4424 | ops[0] = name; | |
4425 | ops[1] = build_int_cst (TREE_TYPE (name), 0); | |
4426 | vn_nary_op_insert_pieces (2, NE_EXPR, boolean_type_node, ops, | |
4427 | boolean_true_node, 0); | |
4428 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4429 | { | |
4430 | fprintf (dump_file, "Recording "); | |
4431 | print_generic_expr (dump_file, name, TDF_SLIM); | |
4432 | fprintf (dump_file, " != 0\n"); | |
4433 | } | |
4434 | } | |
4435 | break; | |
4436 | ||
4437 | case RESULT_DECL: | |
4438 | /* If the result is passed by invisible reference the default | |
4439 | def is initialized, otherwise it's uninitialized. */ | |
4440 | if (DECL_BY_REFERENCE (SSA_NAME_VAR (name))) | |
4441 | { | |
4442 | VN_INFO (name)->visited = true; | |
4443 | VN_INFO (name)->valnum = name; | |
4444 | } | |
4445 | break; | |
4446 | ||
4447 | default: | |
4448 | gcc_unreachable (); | |
4449 | } | |
4450 | } | |
89fb70a3 DB |
4451 | } |
4452 | ||
65f52ee9 RB |
4453 | /* Restore SSA info that has been reset on value leaders. */ |
4454 | ||
89fb70a3 | 4455 | void |
65f52ee9 | 4456 | scc_vn_restore_ssa_info (void) |
89fb70a3 | 4457 | { |
46aa019a KV |
4458 | unsigned i; |
4459 | tree name; | |
4460 | ||
4461 | FOR_EACH_SSA_NAME (i, name, cfun) | |
89fb70a3 | 4462 | { |
46aa019a | 4463 | if (has_VN_INFO (name)) |
e93c66bc RB |
4464 | { |
4465 | if (VN_INFO (name)->needs_insertion) | |
65f52ee9 | 4466 | ; |
e93c66bc RB |
4467 | else if (POINTER_TYPE_P (TREE_TYPE (name)) |
4468 | && VN_INFO (name)->info.ptr_info) | |
4469 | SSA_NAME_PTR_INFO (name) = VN_INFO (name)->info.ptr_info; | |
4470 | else if (INTEGRAL_TYPE_P (TREE_TYPE (name)) | |
4471 | && VN_INFO (name)->info.range_info) | |
fa4511c2 RB |
4472 | { |
4473 | SSA_NAME_RANGE_INFO (name) = VN_INFO (name)->info.range_info; | |
4474 | SSA_NAME_ANTI_RANGE_P (name) | |
4475 | = VN_INFO (name)->range_info_anti_range_p; | |
4476 | } | |
e93c66bc | 4477 | } |
89fb70a3 | 4478 | } |
65f52ee9 RB |
4479 | } |
4480 | ||
4481 | void | |
4482 | free_scc_vn (void) | |
4483 | { | |
4484 | size_t i; | |
46aa019a | 4485 | tree name; |
65f52ee9 RB |
4486 | |
4487 | delete constant_to_value_id; | |
4488 | constant_to_value_id = NULL; | |
4489 | BITMAP_FREE (constant_value_ids); | |
4490 | shared_lookup_phiargs.release (); | |
4491 | shared_lookup_references.release (); | |
4492 | XDELETEVEC (rpo_numbers); | |
4493 | ||
46aa019a | 4494 | FOR_EACH_SSA_NAME (i, name, cfun) |
65f52ee9 | 4495 | { |
46aa019a | 4496 | if (has_VN_INFO (name) |
65f52ee9 RB |
4497 | && VN_INFO (name)->needs_insertion) |
4498 | release_ssa_name (name); | |
4499 | } | |
cbfb21c1 | 4500 | obstack_free (&vn_ssa_aux_obstack, NULL); |
9771b263 | 4501 | vn_ssa_aux_table.release (); |
cbfb21c1 | 4502 | |
9771b263 | 4503 | sccstack.release (); |
89fb70a3 DB |
4504 | free_vn_table (valid_info); |
4505 | XDELETE (valid_info); | |
4506 | free_vn_table (optimistic_info); | |
4507 | XDELETE (optimistic_info); | |
e7cbc096 RB |
4508 | |
4509 | BITMAP_FREE (const_parms); | |
89fb70a3 DB |
4510 | } |
4511 | ||
9ca966ca | 4512 | /* Set *ID according to RESULT. */ |
9ad6bebe NF |
4513 | |
4514 | static void | |
4515 | set_value_id_for_result (tree result, unsigned int *id) | |
4516 | { | |
9ca966ca RB |
4517 | if (result && TREE_CODE (result) == SSA_NAME) |
4518 | *id = VN_INFO (result)->value_id; | |
4519 | else if (result && is_gimple_min_invariant (result)) | |
4520 | *id = get_or_alloc_constant_value_id (result); | |
4521 | else | |
4522 | *id = get_next_value_id (); | |
9ad6bebe NF |
4523 | } |
4524 | ||
caf55296 | 4525 | /* Set the value ids in the valid hash tables. */ |
c9145754 DB |
4526 | |
4527 | static void | |
4528 | set_hashtable_value_ids (void) | |
4529 | { | |
bf190e8d LC |
4530 | vn_nary_op_iterator_type hin; |
4531 | vn_phi_iterator_type hip; | |
4532 | vn_reference_iterator_type hir; | |
c9145754 DB |
4533 | vn_nary_op_t vno; |
4534 | vn_reference_t vr; | |
4535 | vn_phi_t vp; | |
caf55296 | 4536 | |
c9145754 DB |
4537 | /* Now set the value ids of the things we had put in the hash |
4538 | table. */ | |
4539 | ||
c203e8a7 | 4540 | FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->nary, vno, vn_nary_op_t, hin) |
9ad6bebe | 4541 | set_value_id_for_result (vno->result, &vno->value_id); |
c9145754 | 4542 | |
c203e8a7 | 4543 | FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->phis, vp, vn_phi_t, hip) |
9ad6bebe | 4544 | set_value_id_for_result (vp->result, &vp->value_id); |
c9145754 | 4545 | |
c203e8a7 TS |
4546 | FOR_EACH_HASH_TABLE_ELEMENT (*valid_info->references, vr, vn_reference_t, |
4547 | hir) | |
9ad6bebe | 4548 | set_value_id_for_result (vr->result, &vr->value_id); |
c9145754 DB |
4549 | } |
4550 | ||
d2713985 | 4551 | class sccvn_dom_walker : public dom_walker |
a764d660 RB |
4552 | { |
4553 | public: | |
7fd9012e | 4554 | sccvn_dom_walker () |
fcd21591 | 4555 | : dom_walker (CDI_DOMINATORS, true), fail (false), cond_stack (0) {} |
a764d660 | 4556 | |
3daacdcd | 4557 | virtual edge before_dom_children (basic_block); |
7fd9012e RB |
4558 | virtual void after_dom_children (basic_block); |
4559 | ||
4560 | void record_cond (basic_block, | |
4561 | enum tree_code code, tree lhs, tree rhs, bool value); | |
4562 | void record_conds (basic_block, | |
4563 | enum tree_code code, tree lhs, tree rhs, bool value); | |
a764d660 RB |
4564 | |
4565 | bool fail; | |
fcd21591 | 4566 | auto_vec<std::pair <basic_block, std::pair <vn_nary_op_t, vn_nary_op_t> > > |
7fd9012e | 4567 | cond_stack; |
a764d660 RB |
4568 | }; |
4569 | ||
7fd9012e RB |
4570 | /* Record a temporary condition for the BB and its dominated blocks. */ |
4571 | ||
4572 | void | |
4573 | sccvn_dom_walker::record_cond (basic_block bb, | |
4574 | enum tree_code code, tree lhs, tree rhs, | |
4575 | bool value) | |
4576 | { | |
4577 | tree ops[2] = { lhs, rhs }; | |
4578 | vn_nary_op_t old = NULL; | |
4579 | if (vn_nary_op_lookup_pieces (2, code, boolean_type_node, ops, &old)) | |
4580 | current_info->nary->remove_elt_with_hash (old, old->hashcode); | |
4581 | vn_nary_op_t cond | |
4582 | = vn_nary_op_insert_pieces (2, code, boolean_type_node, ops, | |
4583 | value | |
4584 | ? boolean_true_node | |
4585 | : boolean_false_node, 0); | |
4586 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4587 | { | |
4588 | fprintf (dump_file, "Recording temporarily "); | |
4589 | print_generic_expr (dump_file, ops[0], TDF_SLIM); | |
4590 | fprintf (dump_file, " %s ", get_tree_code_name (code)); | |
4591 | print_generic_expr (dump_file, ops[1], TDF_SLIM); | |
4592 | fprintf (dump_file, " == %s%s\n", | |
4593 | value ? "true" : "false", | |
4594 | old ? " (old entry saved)" : ""); | |
4595 | } | |
4596 | cond_stack.safe_push (std::make_pair (bb, std::make_pair (cond, old))); | |
4597 | } | |
4598 | ||
4599 | /* Record temporary conditions for the BB and its dominated blocks | |
4600 | according to LHS CODE RHS == VALUE and its dominated conditions. */ | |
4601 | ||
4602 | void | |
4603 | sccvn_dom_walker::record_conds (basic_block bb, | |
4604 | enum tree_code code, tree lhs, tree rhs, | |
4605 | bool value) | |
4606 | { | |
4607 | /* Record the original condition. */ | |
4608 | record_cond (bb, code, lhs, rhs, value); | |
4609 | ||
4610 | if (!value) | |
4611 | return; | |
4612 | ||
4613 | /* Record dominated conditions if the condition is true. Note that | |
4614 | the inversion is already recorded. */ | |
4615 | switch (code) | |
4616 | { | |
4617 | case LT_EXPR: | |
4618 | case GT_EXPR: | |
4619 | record_cond (bb, code == LT_EXPR ? LE_EXPR : GE_EXPR, lhs, rhs, true); | |
4620 | record_cond (bb, NE_EXPR, lhs, rhs, true); | |
4621 | record_cond (bb, EQ_EXPR, lhs, rhs, false); | |
4622 | break; | |
4623 | ||
4624 | case EQ_EXPR: | |
4625 | record_cond (bb, LE_EXPR, lhs, rhs, true); | |
4626 | record_cond (bb, GE_EXPR, lhs, rhs, true); | |
4627 | record_cond (bb, LT_EXPR, lhs, rhs, false); | |
4628 | record_cond (bb, GT_EXPR, lhs, rhs, false); | |
4629 | break; | |
4630 | ||
4631 | default: | |
4632 | break; | |
4633 | } | |
4634 | } | |
4635 | ||
4636 | /* Restore expressions and values derived from conditionals. */ | |
4637 | ||
4638 | void | |
4639 | sccvn_dom_walker::after_dom_children (basic_block bb) | |
4640 | { | |
4641 | while (!cond_stack.is_empty () | |
4642 | && cond_stack.last ().first == bb) | |
4643 | { | |
4644 | vn_nary_op_t cond = cond_stack.last ().second.first; | |
4645 | vn_nary_op_t old = cond_stack.last ().second.second; | |
4646 | current_info->nary->remove_elt_with_hash (cond, cond->hashcode); | |
4647 | if (old) | |
4648 | vn_nary_op_insert_into (old, current_info->nary, false); | |
4649 | cond_stack.pop (); | |
4650 | } | |
4651 | } | |
4652 | ||
d2713985 RB |
4653 | /* Value number all statements in BB. */ |
4654 | ||
3daacdcd | 4655 | edge |
d2713985 | 4656 | sccvn_dom_walker::before_dom_children (basic_block bb) |
a764d660 RB |
4657 | { |
4658 | edge e; | |
4659 | edge_iterator ei; | |
4660 | ||
4661 | if (fail) | |
3daacdcd | 4662 | return NULL; |
a764d660 | 4663 | |
310d5e7d RB |
4664 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4665 | fprintf (dump_file, "Visiting BB %d\n", bb->index); | |
4666 | ||
7fd9012e RB |
4667 | /* If we have a single predecessor record the equivalence from a |
4668 | possible condition on the predecessor edge. */ | |
3789bf84 RB |
4669 | edge pred_e = NULL; |
4670 | FOR_EACH_EDGE (e, ei, bb->preds) | |
4671 | { | |
4672 | /* Ignore simple backedges from this to allow recording conditions | |
4673 | in loop headers. */ | |
4674 | if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest)) | |
4675 | continue; | |
4676 | if (! pred_e) | |
4677 | pred_e = e; | |
4678 | else | |
4679 | { | |
4680 | pred_e = NULL; | |
4681 | break; | |
4682 | } | |
4683 | } | |
4684 | if (pred_e) | |
7fd9012e | 4685 | { |
7fd9012e RB |
4686 | /* Check if there are multiple executable successor edges in |
4687 | the source block. Otherwise there is no additional info | |
4688 | to be recorded. */ | |
4689 | edge e2; | |
3789bf84 RB |
4690 | FOR_EACH_EDGE (e2, ei, pred_e->src->succs) |
4691 | if (e2 != pred_e | |
7fd9012e RB |
4692 | && e2->flags & EDGE_EXECUTABLE) |
4693 | break; | |
4694 | if (e2 && (e2->flags & EDGE_EXECUTABLE)) | |
4695 | { | |
3789bf84 | 4696 | gimple *stmt = last_stmt (pred_e->src); |
7fd9012e RB |
4697 | if (stmt |
4698 | && gimple_code (stmt) == GIMPLE_COND) | |
4699 | { | |
4700 | enum tree_code code = gimple_cond_code (stmt); | |
4701 | tree lhs = gimple_cond_lhs (stmt); | |
4702 | tree rhs = gimple_cond_rhs (stmt); | |
4703 | record_conds (bb, code, lhs, rhs, | |
3789bf84 | 4704 | (pred_e->flags & EDGE_TRUE_VALUE) != 0); |
7fd9012e RB |
4705 | code = invert_tree_comparison (code, HONOR_NANS (lhs)); |
4706 | if (code != ERROR_MARK) | |
4707 | record_conds (bb, code, lhs, rhs, | |
3789bf84 | 4708 | (pred_e->flags & EDGE_TRUE_VALUE) == 0); |
7fd9012e RB |
4709 | } |
4710 | } | |
4711 | } | |
4712 | ||
d2713985 RB |
4713 | /* Value-number all defs in the basic-block. */ |
4714 | for (gphi_iterator gsi = gsi_start_phis (bb); | |
4715 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
4716 | { | |
4717 | gphi *phi = gsi.phi (); | |
4718 | tree res = PHI_RESULT (phi); | |
4719 | if (!VN_INFO (res)->visited | |
4720 | && !DFS (res)) | |
4721 | { | |
4722 | fail = true; | |
3daacdcd | 4723 | return NULL; |
d2713985 RB |
4724 | } |
4725 | } | |
4726 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); | |
4727 | !gsi_end_p (gsi); gsi_next (&gsi)) | |
4728 | { | |
4729 | ssa_op_iter i; | |
4730 | tree op; | |
4731 | FOR_EACH_SSA_TREE_OPERAND (op, gsi_stmt (gsi), i, SSA_OP_ALL_DEFS) | |
4732 | if (!VN_INFO (op)->visited | |
4733 | && !DFS (op)) | |
4734 | { | |
4735 | fail = true; | |
3daacdcd | 4736 | return NULL; |
d2713985 RB |
4737 | } |
4738 | } | |
4739 | ||
4740 | /* Finally look at the last stmt. */ | |
355fe088 | 4741 | gimple *stmt = last_stmt (bb); |
a764d660 | 4742 | if (!stmt) |
3daacdcd | 4743 | return NULL; |
a764d660 | 4744 | |
b2b222b3 RB |
4745 | enum gimple_code code = gimple_code (stmt); |
4746 | if (code != GIMPLE_COND | |
4747 | && code != GIMPLE_SWITCH | |
4748 | && code != GIMPLE_GOTO) | |
3daacdcd | 4749 | return NULL; |
b2b222b3 RB |
4750 | |
4751 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4752 | { | |
310d5e7d | 4753 | fprintf (dump_file, "Visiting control stmt ending BB %d: ", bb->index); |
b2b222b3 RB |
4754 | print_gimple_stmt (dump_file, stmt, 0, 0); |
4755 | } | |
4756 | ||
a764d660 RB |
4757 | /* ??? We can even handle stmts with outgoing EH or ABNORMAL edges |
4758 | if value-numbering can prove they are not reachable. Handling | |
4759 | computed gotos is also possible. */ | |
4760 | tree val; | |
b2b222b3 | 4761 | switch (code) |
a764d660 RB |
4762 | { |
4763 | case GIMPLE_COND: | |
4764 | { | |
34050b6b RB |
4765 | tree lhs = vn_valueize (gimple_cond_lhs (stmt)); |
4766 | tree rhs = vn_valueize (gimple_cond_rhs (stmt)); | |
4767 | val = gimple_simplify (gimple_cond_code (stmt), | |
4768 | boolean_type_node, lhs, rhs, | |
4769 | NULL, vn_valueize); | |
7fd9012e RB |
4770 | /* If that didn't simplify to a constant see if we have recorded |
4771 | temporary expressions from taken edges. */ | |
4772 | if (!val || TREE_CODE (val) != INTEGER_CST) | |
4773 | { | |
4774 | tree ops[2]; | |
34050b6b RB |
4775 | ops[0] = lhs; |
4776 | ops[1] = rhs; | |
7fd9012e RB |
4777 | val = vn_nary_op_lookup_pieces (2, gimple_cond_code (stmt), |
4778 | boolean_type_node, ops, NULL); | |
4779 | } | |
a764d660 RB |
4780 | break; |
4781 | } | |
4782 | case GIMPLE_SWITCH: | |
538dd0b7 | 4783 | val = gimple_switch_index (as_a <gswitch *> (stmt)); |
a764d660 RB |
4784 | break; |
4785 | case GIMPLE_GOTO: | |
4786 | val = gimple_goto_dest (stmt); | |
4787 | break; | |
4788 | default: | |
b2b222b3 | 4789 | gcc_unreachable (); |
a764d660 RB |
4790 | } |
4791 | if (!val) | |
3daacdcd | 4792 | return NULL; |
a764d660 RB |
4793 | |
4794 | edge taken = find_taken_edge (bb, vn_valueize (val)); | |
4795 | if (!taken) | |
3daacdcd | 4796 | return NULL; |
a764d660 RB |
4797 | |
4798 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4799 | fprintf (dump_file, "Marking all edges out of BB %d but (%d -> %d) as " | |
4800 | "not executable\n", bb->index, bb->index, taken->dest->index); | |
4801 | ||
3daacdcd | 4802 | return taken; |
a764d660 RB |
4803 | } |
4804 | ||
863d2a57 | 4805 | /* Do SCCVN. Returns true if it finished, false if we bailed out |
1ec87690 RG |
4806 | due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies |
4807 | how we use the alias oracle walking during the VN process. */ | |
863d2a57 RG |
4808 | |
4809 | bool | |
1ec87690 | 4810 | run_scc_vn (vn_lookup_kind default_vn_walk_kind_) |
89fb70a3 DB |
4811 | { |
4812 | size_t i; | |
b8698a0f | 4813 | |
1ec87690 RG |
4814 | default_vn_walk_kind = default_vn_walk_kind_; |
4815 | ||
89fb70a3 | 4816 | init_scc_vn (); |
89fb70a3 | 4817 | |
e7cbc096 RB |
4818 | /* Collect pointers we know point to readonly memory. */ |
4819 | const_parms = BITMAP_ALLOC (NULL); | |
4820 | tree fnspec = lookup_attribute ("fn spec", | |
4821 | TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl))); | |
4822 | if (fnspec) | |
4823 | { | |
4824 | fnspec = TREE_VALUE (TREE_VALUE (fnspec)); | |
4825 | i = 1; | |
4826 | for (tree arg = DECL_ARGUMENTS (cfun->decl); | |
4827 | arg; arg = DECL_CHAIN (arg), ++i) | |
4828 | { | |
4829 | if (i >= (unsigned) TREE_STRING_LENGTH (fnspec)) | |
4830 | break; | |
4831 | if (TREE_STRING_POINTER (fnspec)[i] == 'R' | |
4832 | || TREE_STRING_POINTER (fnspec)[i] == 'r') | |
4833 | { | |
4834 | tree name = ssa_default_def (cfun, arg); | |
4835 | if (name) | |
4836 | bitmap_set_bit (const_parms, SSA_NAME_VERSION (name)); | |
4837 | } | |
4838 | } | |
4839 | } | |
4840 | ||
d2713985 RB |
4841 | /* Walk all blocks in dominator order, value-numbering stmts |
4842 | SSA defs and decide whether outgoing edges are not executable. */ | |
4843 | sccvn_dom_walker walker; | |
a764d660 RB |
4844 | walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
4845 | if (walker.fail) | |
4846 | { | |
4847 | free_scc_vn (); | |
4848 | return false; | |
4849 | } | |
4850 | ||
d2713985 RB |
4851 | /* Initialize the value ids and prune out remaining VN_TOPs |
4852 | from dead code. */ | |
46aa019a KV |
4853 | tree name; |
4854 | ||
4855 | FOR_EACH_SSA_NAME (i, name, cfun) | |
c9145754 | 4856 | { |
46aa019a | 4857 | vn_ssa_aux_t info = VN_INFO (name); |
d2713985 RB |
4858 | if (!info->visited) |
4859 | info->valnum = name; | |
f116fecf RG |
4860 | if (info->valnum == name |
4861 | || info->valnum == VN_TOP) | |
c9145754 DB |
4862 | info->value_id = get_next_value_id (); |
4863 | else if (is_gimple_min_invariant (info->valnum)) | |
4864 | info->value_id = get_or_alloc_constant_value_id (info->valnum); | |
4865 | } | |
b8698a0f | 4866 | |
bb35348a | 4867 | /* Propagate. */ |
46aa019a | 4868 | FOR_EACH_SSA_NAME (i, name, cfun) |
c9145754 | 4869 | { |
46aa019a | 4870 | vn_ssa_aux_t info = VN_INFO (name); |
bb35348a RB |
4871 | if (TREE_CODE (info->valnum) == SSA_NAME |
4872 | && info->valnum != name | |
4873 | && info->value_id != VN_INFO (info->valnum)->value_id) | |
4874 | info->value_id = VN_INFO (info->valnum)->value_id; | |
c9145754 | 4875 | } |
b8698a0f | 4876 | |
c9145754 | 4877 | set_hashtable_value_ids (); |
b8698a0f | 4878 | |
89fb70a3 DB |
4879 | if (dump_file && (dump_flags & TDF_DETAILS)) |
4880 | { | |
4881 | fprintf (dump_file, "Value numbers:\n"); | |
46aa019a | 4882 | FOR_EACH_SSA_NAME (i, name, cfun) |
89fb70a3 | 4883 | { |
46aa019a | 4884 | if (VN_INFO (name)->visited |
caf55296 | 4885 | && SSA_VAL (name) != name) |
89fb70a3 DB |
4886 | { |
4887 | print_generic_expr (dump_file, name, 0); | |
4888 | fprintf (dump_file, " = "); | |
caf55296 | 4889 | print_generic_expr (dump_file, SSA_VAL (name), 0); |
89fb70a3 DB |
4890 | fprintf (dump_file, "\n"); |
4891 | } | |
4892 | } | |
4893 | } | |
863d2a57 RG |
4894 | |
4895 | return true; | |
89fb70a3 | 4896 | } |
c9145754 DB |
4897 | |
4898 | /* Return the maximum value id we have ever seen. */ | |
4899 | ||
4900 | unsigned int | |
b8698a0f | 4901 | get_max_value_id (void) |
c9145754 DB |
4902 | { |
4903 | return next_value_id; | |
4904 | } | |
4905 | ||
4906 | /* Return the next unique value id. */ | |
4907 | ||
4908 | unsigned int | |
4909 | get_next_value_id (void) | |
4910 | { | |
4911 | return next_value_id++; | |
4912 | } | |
4913 | ||
4914 | ||
330e765e | 4915 | /* Compare two expressions E1 and E2 and return true if they are equal. */ |
c9145754 DB |
4916 | |
4917 | bool | |
4918 | expressions_equal_p (tree e1, tree e2) | |
4919 | { | |
330e765e | 4920 | /* The obvious case. */ |
c9145754 DB |
4921 | if (e1 == e2) |
4922 | return true; | |
4923 | ||
94852c8e RB |
4924 | /* If either one is VN_TOP consider them equal. */ |
4925 | if (e1 == VN_TOP || e2 == VN_TOP) | |
4926 | return true; | |
4927 | ||
330e765e EB |
4928 | /* If only one of them is null, they cannot be equal. */ |
4929 | if (!e1 || !e2) | |
4930 | return false; | |
4931 | ||
330e765e EB |
4932 | /* Now perform the actual comparison. */ |
4933 | if (TREE_CODE (e1) == TREE_CODE (e2) | |
4934 | && operand_equal_p (e1, e2, OEP_PURE_SAME)) | |
c9145754 DB |
4935 | return true; |
4936 | ||
4937 | return false; | |
4938 | } | |
4939 | ||
890065bf RG |
4940 | |
4941 | /* Return true if the nary operation NARY may trap. This is a copy | |
4942 | of stmt_could_throw_1_p adjusted to the SCCVN IL. */ | |
4943 | ||
4944 | bool | |
4945 | vn_nary_may_trap (vn_nary_op_t nary) | |
4946 | { | |
4947 | tree type; | |
6141b7db | 4948 | tree rhs2 = NULL_TREE; |
890065bf RG |
4949 | bool honor_nans = false; |
4950 | bool honor_snans = false; | |
4951 | bool fp_operation = false; | |
4952 | bool honor_trapv = false; | |
4953 | bool handled, ret; | |
4954 | unsigned i; | |
4955 | ||
4956 | if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison | |
4957 | || TREE_CODE_CLASS (nary->opcode) == tcc_unary | |
4958 | || TREE_CODE_CLASS (nary->opcode) == tcc_binary) | |
4959 | { | |
4960 | type = nary->type; | |
4961 | fp_operation = FLOAT_TYPE_P (type); | |
4962 | if (fp_operation) | |
4963 | { | |
4964 | honor_nans = flag_trapping_math && !flag_finite_math_only; | |
4965 | honor_snans = flag_signaling_nans != 0; | |
4966 | } | |
4967 | else if (INTEGRAL_TYPE_P (type) | |
4968 | && TYPE_OVERFLOW_TRAPS (type)) | |
4969 | honor_trapv = true; | |
4970 | } | |
6141b7db RG |
4971 | if (nary->length >= 2) |
4972 | rhs2 = nary->op[1]; | |
890065bf RG |
4973 | ret = operation_could_trap_helper_p (nary->opcode, fp_operation, |
4974 | honor_trapv, | |
4975 | honor_nans, honor_snans, rhs2, | |
4976 | &handled); | |
4977 | if (handled | |
4978 | && ret) | |
4979 | return true; | |
4980 | ||
4981 | for (i = 0; i < nary->length; ++i) | |
4982 | if (tree_could_trap_p (nary->op[i])) | |
4983 | return true; | |
4984 | ||
4985 | return false; | |
4986 | } |