]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-sccvn.c
backport: As described in http://gcc.gnu.org/ml/gcc/2012-08/msg00015.html...
[thirdparty/gcc.git] / gcc / tree-ssa-sccvn.c
CommitLineData
89fb70a3 1/* SCC value numbering for trees
c75c517d 2 Copyright (C) 2006, 2007, 2008, 2009, 2010
89fb70a3
DB
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dan@dberlin.org>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
9dcd6f09 10the Free Software Foundation; either version 3, or (at your option)
89fb70a3
DB
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
89fb70a3
DB
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
89fb70a3
DB
26#include "tree.h"
27#include "basic-block.h"
cf835838 28#include "gimple-pretty-print.h"
89fb70a3
DB
29#include "tree-inline.h"
30#include "tree-flow.h"
726a989a 31#include "gimple.h"
7ee2468b 32#include "dumpfile.h"
89fb70a3 33#include "hashtab.h"
89fb70a3 34#include "alloc-pool.h"
89fb70a3
DB
35#include "flags.h"
36#include "bitmap.h"
89fb70a3 37#include "cfgloop.h"
863d2a57 38#include "params.h"
c2979eaf 39#include "tree-ssa-propagate.h"
89fb70a3 40#include "tree-ssa-sccvn.h"
cfef45c8 41#include "gimple-fold.h"
89fb70a3
DB
42
43/* This algorithm is based on the SCC algorithm presented by Keith
44 Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
45 (http://citeseer.ist.psu.edu/41805.html). In
46 straight line code, it is equivalent to a regular hash based value
47 numbering that is performed in reverse postorder.
48
49 For code with cycles, there are two alternatives, both of which
50 require keeping the hashtables separate from the actual list of
51 value numbers for SSA names.
52
53 1. Iterate value numbering in an RPO walk of the blocks, removing
54 all the entries from the hashtable after each iteration (but
55 keeping the SSA name->value number mapping between iterations).
56 Iterate until it does not change.
57
58 2. Perform value numbering as part of an SCC walk on the SSA graph,
59 iterating only the cycles in the SSA graph until they do not change
60 (using a separate, optimistic hashtable for value numbering the SCC
61 operands).
62
63 The second is not just faster in practice (because most SSA graph
64 cycles do not involve all the variables in the graph), it also has
65 some nice properties.
66
67 One of these nice properties is that when we pop an SCC off the
68 stack, we are guaranteed to have processed all the operands coming from
69 *outside of that SCC*, so we do not need to do anything special to
70 ensure they have value numbers.
71
72 Another nice property is that the SCC walk is done as part of a DFS
73 of the SSA graph, which makes it easy to perform combining and
74 simplifying operations at the same time.
75
76 The code below is deliberately written in a way that makes it easy
77 to separate the SCC walk from the other work it does.
78
79 In order to propagate constants through the code, we track which
80 expressions contain constants, and use those while folding. In
81 theory, we could also track expressions whose value numbers are
82 replaced, in case we end up folding based on expression
83 identities.
84
85 In order to value number memory, we assign value numbers to vuses.
86 This enables us to note that, for example, stores to the same
87 address of the same value from the same starting memory states are
070b797d 88 equivalent.
89fb70a3
DB
89 TODO:
90
91 1. We can iterate only the changing portions of the SCC's, but
92 I have not seen an SCC big enough for this to be a win.
93 2. If you differentiate between phi nodes for loops and phi nodes
94 for if-then-else, you can properly consider phi nodes in different
95 blocks for equivalence.
96 3. We could value number vuses in more cases, particularly, whole
97 structure copies.
98*/
99
100/* The set of hashtables and alloc_pool's for their items. */
101
102typedef struct vn_tables_s
103{
49a1fb2d 104 htab_t nary;
89fb70a3
DB
105 htab_t phis;
106 htab_t references;
49a1fb2d 107 struct obstack nary_obstack;
89fb70a3
DB
108 alloc_pool phis_pool;
109 alloc_pool references_pool;
110} *vn_tables_t;
111
c9145754
DB
112static htab_t constant_to_value_id;
113static bitmap constant_value_ids;
89fb70a3 114
89fb70a3
DB
115
116/* Valid hashtables storing information we have proven to be
117 correct. */
118
119static vn_tables_t valid_info;
120
121/* Optimistic hashtables storing information we are making assumptions about
122 during iterations. */
123
124static vn_tables_t optimistic_info;
125
89fb70a3
DB
126/* Pointer to the set of hashtables that is currently being used.
127 Should always point to either the optimistic_info, or the
128 valid_info. */
129
130static vn_tables_t current_info;
131
132
133/* Reverse post order index for each basic block. */
134
135static int *rpo_numbers;
136
137#define SSA_VAL(x) (VN_INFO ((x))->valnum)
138
139/* This represents the top of the VN lattice, which is the universal
140 value. */
141
142tree VN_TOP;
143
c9145754
DB
144/* Unique counter for our value ids. */
145
146static unsigned int next_value_id;
147
89fb70a3
DB
148/* Next DFS number and the stack for strongly connected component
149 detection. */
150
151static unsigned int next_dfs_num;
152static VEC (tree, heap) *sccstack;
153
3d45dd59 154
89fb70a3
DB
155DEF_VEC_P(vn_ssa_aux_t);
156DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
157
cbfb21c1
SB
158/* Table of vn_ssa_aux_t's, one per ssa_name. The vn_ssa_aux_t objects
159 are allocated on an obstack for locality reasons, and to free them
160 without looping over the VEC. */
89fb70a3
DB
161
162static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
cbfb21c1 163static struct obstack vn_ssa_aux_obstack;
89fb70a3
DB
164
165/* Return the value numbering information for a given SSA name. */
166
167vn_ssa_aux_t
168VN_INFO (tree name)
169{
c9145754
DB
170 vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
171 SSA_NAME_VERSION (name));
7a40b8b1 172 gcc_checking_assert (res);
c9145754 173 return res;
89fb70a3
DB
174}
175
176/* Set the value numbering info for a given SSA name to a given
177 value. */
178
179static inline void
180VN_INFO_SET (tree name, vn_ssa_aux_t value)
181{
182 VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
183 SSA_NAME_VERSION (name), value);
184}
185
cbfb21c1
SB
186/* Initialize the value numbering info for a given SSA name.
187 This should be called just once for every SSA name. */
89fb70a3
DB
188
189vn_ssa_aux_t
190VN_INFO_GET (tree name)
191{
cbfb21c1
SB
192 vn_ssa_aux_t newinfo;
193
3d9a9f94 194 newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
cbfb21c1 195 memset (newinfo, 0, sizeof (struct vn_ssa_aux));
89fb70a3
DB
196 if (SSA_NAME_VERSION (name) >= VEC_length (vn_ssa_aux_t, vn_ssa_aux_table))
197 VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table,
198 SSA_NAME_VERSION (name) + 1);
199 VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
200 SSA_NAME_VERSION (name), newinfo);
201 return newinfo;
202}
203
204
726a989a
RB
205/* Get the representative expression for the SSA_NAME NAME. Returns
206 the representative SSA_NAME if there is no expression associated with it. */
207
208tree
209vn_get_expr_for (tree name)
210{
211 vn_ssa_aux_t vn = VN_INFO (name);
212 gimple def_stmt;
213 tree expr = NULL_TREE;
1a60c352 214 enum tree_code code;
726a989a
RB
215
216 if (vn->valnum == VN_TOP)
217 return name;
218
219 /* If the value-number is a constant it is the representative
220 expression. */
221 if (TREE_CODE (vn->valnum) != SSA_NAME)
222 return vn->valnum;
223
224 /* Get to the information of the value of this SSA_NAME. */
225 vn = VN_INFO (vn->valnum);
226
227 /* If the value-number is a constant it is the representative
228 expression. */
229 if (TREE_CODE (vn->valnum) != SSA_NAME)
230 return vn->valnum;
231
232 /* Else if we have an expression, return it. */
233 if (vn->expr != NULL_TREE)
234 return vn->expr;
235
236 /* Otherwise use the defining statement to build the expression. */
237 def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
238
1a60c352 239 /* If the value number is not an assignment use it directly. */
726a989a
RB
240 if (!is_gimple_assign (def_stmt))
241 return vn->valnum;
242
243 /* FIXME tuples. This is incomplete and likely will miss some
244 simplifications. */
1a60c352
RG
245 code = gimple_assign_rhs_code (def_stmt);
246 switch (TREE_CODE_CLASS (code))
726a989a
RB
247 {
248 case tcc_reference:
1a60c352
RG
249 if ((code == REALPART_EXPR
250 || code == IMAGPART_EXPR
251 || code == VIEW_CONVERT_EXPR)
252 && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (def_stmt),
253 0)) == SSA_NAME)
254 expr = fold_build1 (code,
726a989a
RB
255 gimple_expr_type (def_stmt),
256 TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
257 break;
258
259 case tcc_unary:
1a60c352 260 expr = fold_build1 (code,
726a989a
RB
261 gimple_expr_type (def_stmt),
262 gimple_assign_rhs1 (def_stmt));
263 break;
264
265 case tcc_binary:
1a60c352 266 expr = fold_build2 (code,
726a989a
RB
267 gimple_expr_type (def_stmt),
268 gimple_assign_rhs1 (def_stmt),
269 gimple_assign_rhs2 (def_stmt));
270 break;
271
18474649
RG
272 case tcc_exceptional:
273 if (code == CONSTRUCTOR
274 && TREE_CODE
275 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))) == VECTOR_TYPE)
276 expr = gimple_assign_rhs1 (def_stmt);
277 break;
278
726a989a
RB
279 default:;
280 }
281 if (expr == NULL_TREE)
282 return vn->valnum;
283
284 /* Cache the expression. */
285 vn->expr = expr;
286
287 return expr;
288}
289
290
8d0eca24
RG
291/* Free a phi operation structure VP. */
292
293static void
294free_phi (void *vp)
295{
3d9a9f94 296 vn_phi_t phi = (vn_phi_t) vp;
8d0eca24
RG
297 VEC_free (tree, heap, phi->phiargs);
298}
299
300/* Free a reference operation structure VP. */
301
302static void
303free_reference (void *vp)
304{
3d9a9f94 305 vn_reference_t vr = (vn_reference_t) vp;
8d0eca24
RG
306 VEC_free (vn_reference_op_s, heap, vr->operands);
307}
308
c9145754
DB
309/* Hash table equality function for vn_constant_t. */
310
311static int
312vn_constant_eq (const void *p1, const void *p2)
313{
314 const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
315 const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
316
85169114
PB
317 if (vc1->hashcode != vc2->hashcode)
318 return false;
319
726a989a 320 return vn_constant_eq_with_type (vc1->constant, vc2->constant);
c9145754
DB
321}
322
323/* Hash table hash function for vn_constant_t. */
b8698a0f 324
c9145754
DB
325static hashval_t
326vn_constant_hash (const void *p1)
327{
328 const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
329 return vc1->hashcode;
330}
331
bb9e4199
RG
332/* Lookup a value id for CONSTANT and return it. If it does not
333 exist returns 0. */
334
335unsigned int
336get_constant_value_id (tree constant)
337{
338 void **slot;
339 struct vn_constant_s vc;
726a989a
RB
340
341 vc.hashcode = vn_hash_constant_with_type (constant);
bb9e4199
RG
342 vc.constant = constant;
343 slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
344 vc.hashcode, NO_INSERT);
345 if (slot)
346 return ((vn_constant_t)*slot)->value_id;
347 return 0;
348}
349
c9145754
DB
350/* Lookup a value id for CONSTANT, and if it does not exist, create a
351 new one and return it. If it does exist, return it. */
352
353unsigned int
354get_or_alloc_constant_value_id (tree constant)
355{
356 void **slot;
a7d04a53
RG
357 struct vn_constant_s vc;
358 vn_constant_t vcp;
b8698a0f 359
a7d04a53
RG
360 vc.hashcode = vn_hash_constant_with_type (constant);
361 vc.constant = constant;
362 slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
363 vc.hashcode, INSERT);
c9145754 364 if (*slot)
a7d04a53
RG
365 return ((vn_constant_t)*slot)->value_id;
366
367 vcp = XNEW (struct vn_constant_s);
368 vcp->hashcode = vc.hashcode;
369 vcp->constant = constant;
370 vcp->value_id = get_next_value_id ();
371 *slot = (void *) vcp;
372 bitmap_set_bit (constant_value_ids, vcp->value_id);
373 return vcp->value_id;
c9145754
DB
374}
375
376/* Return true if V is a value id for a constant. */
377
378bool
379value_id_constant_p (unsigned int v)
380{
b8698a0f 381 return bitmap_bit_p (constant_value_ids, v);
c9145754
DB
382}
383
b40bf772 384/* Compare two reference operands P1 and P2 for equality. Return true if
89fb70a3
DB
385 they are equal, and false otherwise. */
386
387static int
388vn_reference_op_eq (const void *p1, const void *p2)
389{
741ac903
KG
390 const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
391 const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
85169114 392
6d6c9525
RG
393 return (vro1->opcode == vro2->opcode
394 /* We do not care for differences in type qualification. */
395 && (vro1->type == vro2->type
396 || (vro1->type && vro2->type
397 && types_compatible_p (TYPE_MAIN_VARIANT (vro1->type),
398 TYPE_MAIN_VARIANT (vro2->type))))
399 && expressions_equal_p (vro1->op0, vro2->op0)
400 && expressions_equal_p (vro1->op1, vro2->op1)
401 && expressions_equal_p (vro1->op2, vro2->op2));
89fb70a3
DB
402}
403
b40bf772 404/* Compute the hash for a reference operand VRO1. */
89fb70a3
DB
405
406static hashval_t
9708c51d 407vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
89fb70a3 408{
9708c51d 409 result = iterative_hash_hashval_t (vro1->opcode, result);
85169114 410 if (vro1->op0)
9708c51d 411 result = iterative_hash_expr (vro1->op0, result);
85169114 412 if (vro1->op1)
9708c51d 413 result = iterative_hash_expr (vro1->op1, result);
85169114 414 if (vro1->op2)
9708c51d 415 result = iterative_hash_expr (vro1->op2, result);
85169114 416 return result;
89fb70a3
DB
417}
418
419/* Return the hashcode for a given reference operation P1. */
420
421static hashval_t
422vn_reference_hash (const void *p1)
423{
741ac903 424 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
89fb70a3
DB
425 return vr1->hashcode;
426}
427
428/* Compute a hash for the reference operation VR1 and return it. */
429
c9145754 430hashval_t
89fb70a3
DB
431vn_reference_compute_hash (const vn_reference_t vr1)
432{
9708c51d 433 hashval_t result = 0;
89fb70a3
DB
434 int i;
435 vn_reference_op_t vro;
70f34814
RG
436 HOST_WIDE_INT off = -1;
437 bool deref = false;
89fb70a3 438
ac47786e 439 FOR_EACH_VEC_ELT (vn_reference_op_s, vr1->operands, i, vro)
70f34814
RG
440 {
441 if (vro->opcode == MEM_REF)
442 deref = true;
443 else if (vro->opcode != ADDR_EXPR)
444 deref = false;
445 if (vro->off != -1)
446 {
447 if (off == -1)
448 off = 0;
449 off += vro->off;
450 }
451 else
452 {
453 if (off != -1
454 && off != 0)
455 result = iterative_hash_hashval_t (off, result);
456 off = -1;
457 if (deref
458 && vro->opcode == ADDR_EXPR)
459 {
460 if (vro->op0)
461 {
462 tree op = TREE_OPERAND (vro->op0, 0);
463 result = iterative_hash_hashval_t (TREE_CODE (op), result);
464 result = iterative_hash_expr (op, result);
465 }
466 }
467 else
468 result = vn_reference_op_compute_hash (vro, result);
469 }
470 }
9708c51d
RG
471 if (vr1->vuse)
472 result += SSA_NAME_VERSION (vr1->vuse);
89fb70a3
DB
473
474 return result;
475}
476
477/* Return true if reference operations P1 and P2 are equivalent. This
478 means they have the same set of operands and vuses. */
479
c9145754 480int
89fb70a3
DB
481vn_reference_eq (const void *p1, const void *p2)
482{
70f34814 483 unsigned i, j;
89fb70a3 484
741ac903
KG
485 const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
486 const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
85169114
PB
487 if (vr1->hashcode != vr2->hashcode)
488 return false;
89fb70a3 489
5006671f
RG
490 /* Early out if this is not a hash collision. */
491 if (vr1->hashcode != vr2->hashcode)
492 return false;
89fb70a3 493
5006671f
RG
494 /* The VOP needs to be the same. */
495 if (vr1->vuse != vr2->vuse)
89fb70a3
DB
496 return false;
497
5006671f
RG
498 /* If the operands are the same we are done. */
499 if (vr1->operands == vr2->operands)
500 return true;
501
70f34814 502 if (!expressions_equal_p (TYPE_SIZE (vr1->type), TYPE_SIZE (vr2->type)))
89fb70a3
DB
503 return false;
504
5ccbfc1f
RG
505 if (INTEGRAL_TYPE_P (vr1->type)
506 && INTEGRAL_TYPE_P (vr2->type))
507 {
508 if (TYPE_PRECISION (vr1->type) != TYPE_PRECISION (vr2->type))
509 return false;
510 }
511 else if (INTEGRAL_TYPE_P (vr1->type)
512 && (TYPE_PRECISION (vr1->type)
513 != TREE_INT_CST_LOW (TYPE_SIZE (vr1->type))))
514 return false;
515 else if (INTEGRAL_TYPE_P (vr2->type)
516 && (TYPE_PRECISION (vr2->type)
517 != TREE_INT_CST_LOW (TYPE_SIZE (vr2->type))))
518 return false;
519
70f34814
RG
520 i = 0;
521 j = 0;
522 do
523 {
524 HOST_WIDE_INT off1 = 0, off2 = 0;
525 vn_reference_op_t vro1, vro2;
526 vn_reference_op_s tem1, tem2;
527 bool deref1 = false, deref2 = false;
528 for (; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro1); i++)
529 {
530 if (vro1->opcode == MEM_REF)
531 deref1 = true;
532 if (vro1->off == -1)
533 break;
534 off1 += vro1->off;
535 }
536 for (; VEC_iterate (vn_reference_op_s, vr2->operands, j, vro2); j++)
537 {
538 if (vro2->opcode == MEM_REF)
539 deref2 = true;
540 if (vro2->off == -1)
541 break;
542 off2 += vro2->off;
543 }
544 if (off1 != off2)
545 return false;
546 if (deref1 && vro1->opcode == ADDR_EXPR)
547 {
548 memset (&tem1, 0, sizeof (tem1));
549 tem1.op0 = TREE_OPERAND (vro1->op0, 0);
550 tem1.type = TREE_TYPE (tem1.op0);
551 tem1.opcode = TREE_CODE (tem1.op0);
552 vro1 = &tem1;
8bf43909 553 deref1 = false;
70f34814
RG
554 }
555 if (deref2 && vro2->opcode == ADDR_EXPR)
556 {
557 memset (&tem2, 0, sizeof (tem2));
558 tem2.op0 = TREE_OPERAND (vro2->op0, 0);
559 tem2.type = TREE_TYPE (tem2.op0);
560 tem2.opcode = TREE_CODE (tem2.op0);
561 vro2 = &tem2;
8bf43909 562 deref2 = false;
70f34814 563 }
8bf43909
RG
564 if (deref1 != deref2)
565 return false;
70f34814
RG
566 if (!vn_reference_op_eq (vro1, vro2))
567 return false;
568 ++j;
569 ++i;
570 }
571 while (VEC_length (vn_reference_op_s, vr1->operands) != i
572 || VEC_length (vn_reference_op_s, vr2->operands) != j);
89fb70a3 573
5006671f 574 return true;
89fb70a3
DB
575}
576
726a989a 577/* Copy the operations present in load/store REF into RESULT, a vector of
89fb70a3
DB
578 vn_reference_op_s's. */
579
ce94d354 580void
89fb70a3
DB
581copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
582{
9f59420d
RG
583 if (TREE_CODE (ref) == TARGET_MEM_REF)
584 {
585 vn_reference_op_s temp;
586
587 memset (&temp, 0, sizeof (temp));
6d6c9525 588 temp.type = TREE_TYPE (ref);
9f59420d 589 temp.opcode = TREE_CODE (ref);
150e3929
RG
590 temp.op0 = TMR_INDEX (ref);
591 temp.op1 = TMR_STEP (ref);
592 temp.op2 = TMR_OFFSET (ref);
70f34814 593 temp.off = -1;
9f59420d
RG
594 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
595
596 memset (&temp, 0, sizeof (temp));
597 temp.type = NULL_TREE;
4d948885
RG
598 temp.opcode = ERROR_MARK;
599 temp.op0 = TMR_INDEX2 (ref);
600 temp.off = -1;
601 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
602
603 memset (&temp, 0, sizeof (temp));
604 temp.type = NULL_TREE;
605 temp.opcode = TREE_CODE (TMR_BASE (ref));
606 temp.op0 = TMR_BASE (ref);
70f34814 607 temp.off = -1;
9f59420d
RG
608 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
609 return;
610 }
611
89fb70a3
DB
612 /* For non-calls, store the information that makes up the address. */
613
614 while (ref)
615 {
616 vn_reference_op_s temp;
617
618 memset (&temp, 0, sizeof (temp));
6d6c9525 619 temp.type = TREE_TYPE (ref);
89fb70a3 620 temp.opcode = TREE_CODE (ref);
70f34814 621 temp.off = -1;
89fb70a3
DB
622
623 switch (temp.opcode)
624 {
4ec0a198
TV
625 case MODIFY_EXPR:
626 temp.op0 = TREE_OPERAND (ref, 1);
627 break;
842679dc
TV
628 case WITH_SIZE_EXPR:
629 temp.op0 = TREE_OPERAND (ref, 1);
630 temp.off = 0;
631 break;
70f34814
RG
632 case MEM_REF:
633 /* The base address gets its own vn_reference_op_s structure. */
634 temp.op0 = TREE_OPERAND (ref, 1);
635 if (host_integerp (TREE_OPERAND (ref, 1), 0))
636 temp.off = TREE_INT_CST_LOW (TREE_OPERAND (ref, 1));
637 break;
89fb70a3
DB
638 case BIT_FIELD_REF:
639 /* Record bits and position. */
640 temp.op0 = TREE_OPERAND (ref, 1);
641 temp.op1 = TREE_OPERAND (ref, 2);
642 break;
643 case COMPONENT_REF:
ba2e1892
RG
644 /* The field decl is enough to unambiguously specify the field,
645 a matching type is not necessary and a mismatching type
646 is always a spurious difference. */
647 temp.type = NULL_TREE;
b45d2719
RG
648 temp.op0 = TREE_OPERAND (ref, 1);
649 temp.op1 = TREE_OPERAND (ref, 2);
70f34814
RG
650 {
651 tree this_offset = component_ref_field_offset (ref);
652 if (this_offset
653 && TREE_CODE (this_offset) == INTEGER_CST)
654 {
655 tree bit_offset = DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1));
656 if (TREE_INT_CST_LOW (bit_offset) % BITS_PER_UNIT == 0)
657 {
658 double_int off
659 = double_int_add (tree_to_double_int (this_offset),
afca0898 660 double_int_rshift
70f34814 661 (tree_to_double_int (bit_offset),
afca0898
RG
662 BITS_PER_UNIT == 8
663 ? 3 : exact_log2 (BITS_PER_UNIT),
664 HOST_BITS_PER_DOUBLE_INT, true));
70f34814
RG
665 if (double_int_fits_in_shwi_p (off))
666 temp.off = off.low;
667 }
668 }
669 }
89fb70a3
DB
670 break;
671 case ARRAY_RANGE_REF:
672 case ARRAY_REF:
673 /* Record index as operand. */
674 temp.op0 = TREE_OPERAND (ref, 1);
e52201b6
RG
675 /* Always record lower bounds and element size. */
676 temp.op1 = array_ref_low_bound (ref);
677 temp.op2 = array_ref_element_size (ref);
70f34814
RG
678 if (TREE_CODE (temp.op0) == INTEGER_CST
679 && TREE_CODE (temp.op1) == INTEGER_CST
680 && TREE_CODE (temp.op2) == INTEGER_CST)
681 {
682 double_int off = tree_to_double_int (temp.op0);
683 off = double_int_add (off,
684 double_int_neg
685 (tree_to_double_int (temp.op1)));
686 off = double_int_mul (off, tree_to_double_int (temp.op2));
687 if (double_int_fits_in_shwi_p (off))
688 temp.off = off.low;
689 }
89fb70a3 690 break;
6d6c9525
RG
691 case VAR_DECL:
692 if (DECL_HARD_REGISTER (ref))
693 {
694 temp.op0 = ref;
695 break;
696 }
697 /* Fallthru. */
698 case PARM_DECL:
699 case CONST_DECL:
700 case RESULT_DECL:
701 /* Canonicalize decls to MEM[&decl] which is what we end up with
702 when valueizing MEM[ptr] with ptr = &decl. */
703 temp.opcode = MEM_REF;
704 temp.op0 = build_int_cst (build_pointer_type (TREE_TYPE (ref)), 0);
705 temp.off = 0;
706 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
707 temp.opcode = ADDR_EXPR;
708 temp.op0 = build_fold_addr_expr (ref);
709 temp.type = TREE_TYPE (temp.op0);
710 temp.off = -1;
711 break;
4794afa5
DB
712 case STRING_CST:
713 case INTEGER_CST:
714 case COMPLEX_CST:
715 case VECTOR_CST:
26b70b9f 716 case REAL_CST:
0747aae4 717 case FIXED_CST:
bb0c55f6 718 case CONSTRUCTOR:
89fb70a3
DB
719 case SSA_NAME:
720 temp.op0 = ref;
721 break;
ce94d354
RG
722 case ADDR_EXPR:
723 if (is_gimple_min_invariant (ref))
724 {
725 temp.op0 = ref;
726 break;
727 }
728 /* Fallthrough. */
4794afa5
DB
729 /* These are only interesting for their operands, their
730 existence, and their type. They will never be the last
731 ref in the chain of references (IE they require an
732 operand), so we don't have to put anything
733 for op* as it will be handled by the iteration */
4794afa5
DB
734 case REALPART_EXPR:
735 case VIEW_CONVERT_EXPR:
70f34814
RG
736 temp.off = 0;
737 break;
738 case IMAGPART_EXPR:
739 /* This is only interesting for its constant offset. */
740 temp.off = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (ref)));
89fb70a3 741 break;
4794afa5
DB
742 default:
743 gcc_unreachable ();
89fb70a3
DB
744 }
745 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
746
ce94d354 747 if (REFERENCE_CLASS_P (ref)
4ec0a198 748 || TREE_CODE (ref) == MODIFY_EXPR
842679dc 749 || TREE_CODE (ref) == WITH_SIZE_EXPR
ce94d354
RG
750 || (TREE_CODE (ref) == ADDR_EXPR
751 && !is_gimple_min_invariant (ref)))
89fb70a3
DB
752 ref = TREE_OPERAND (ref, 0);
753 else
754 ref = NULL_TREE;
755 }
756}
757
b45d2719
RG
758/* Build a alias-oracle reference abstraction in *REF from the vn_reference
759 operands in *OPS, the reference alias set SET and the reference type TYPE.
760 Return true if something useful was produced. */
53f3815c 761
b45d2719
RG
762bool
763ao_ref_init_from_vn_reference (ao_ref *ref,
764 alias_set_type set, tree type,
765 VEC (vn_reference_op_s, heap) *ops)
53f3815c
RG
766{
767 vn_reference_op_t op;
768 unsigned i;
b45d2719
RG
769 tree base = NULL_TREE;
770 tree *op0_p = &base;
771 HOST_WIDE_INT offset = 0;
772 HOST_WIDE_INT max_size;
773 HOST_WIDE_INT size = -1;
774 tree size_tree = NULL_TREE;
70f34814 775 alias_set_type base_alias_set = -1;
b45d2719
RG
776
777 /* First get the final access size from just the outermost expression. */
0823efed 778 op = &VEC_index (vn_reference_op_s, ops, 0);
b45d2719 779 if (op->opcode == COMPONENT_REF)
70f34814 780 size_tree = DECL_SIZE (op->op0);
b45d2719
RG
781 else if (op->opcode == BIT_FIELD_REF)
782 size_tree = op->op0;
783 else
784 {
785 enum machine_mode mode = TYPE_MODE (type);
786 if (mode == BLKmode)
787 size_tree = TYPE_SIZE (type);
788 else
789 size = GET_MODE_BITSIZE (mode);
790 }
791 if (size_tree != NULL_TREE)
792 {
793 if (!host_integerp (size_tree, 1))
794 size = -1;
795 else
796 size = TREE_INT_CST_LOW (size_tree);
797 }
798
799 /* Initially, maxsize is the same as the accessed element size.
800 In the following it will only grow (or become -1). */
801 max_size = size;
53f3815c 802
b45d2719
RG
803 /* Compute cumulative bit-offset for nested component-refs and array-refs,
804 and find the ultimate containing object. */
ac47786e 805 FOR_EACH_VEC_ELT (vn_reference_op_s, ops, i, op)
53f3815c
RG
806 {
807 switch (op->opcode)
808 {
b45d2719
RG
809 /* These may be in the reference ops, but we cannot do anything
810 sensible with them here. */
b45d2719 811 case ADDR_EXPR:
70f34814
RG
812 /* Apart from ADDR_EXPR arguments to MEM_REF. */
813 if (base != NULL_TREE
814 && TREE_CODE (base) == MEM_REF
815 && op->op0
816 && DECL_P (TREE_OPERAND (op->op0, 0)))
817 {
0823efed 818 vn_reference_op_t pop = &VEC_index (vn_reference_op_s, ops, i-1);
70f34814
RG
819 base = TREE_OPERAND (op->op0, 0);
820 if (pop->off == -1)
821 {
822 max_size = -1;
823 offset = 0;
824 }
825 else
826 offset += pop->off * BITS_PER_UNIT;
827 op0_p = NULL;
828 break;
829 }
830 /* Fallthru. */
831 case CALL_EXPR:
b45d2719 832 return false;
53f3815c 833
b45d2719 834 /* Record the base objects. */
70f34814
RG
835 case MEM_REF:
836 base_alias_set = get_deref_alias_set (op->op0);
837 *op0_p = build2 (MEM_REF, op->type,
838 NULL_TREE, op->op0);
839 op0_p = &TREE_OPERAND (*op0_p, 0);
840 break;
841
b45d2719
RG
842 case VAR_DECL:
843 case PARM_DECL:
844 case RESULT_DECL:
845 case SSA_NAME:
b45d2719 846 *op0_p = op->op0;
70f34814 847 op0_p = NULL;
b45d2719
RG
848 break;
849
850 /* And now the usual component-reference style ops. */
53f3815c 851 case BIT_FIELD_REF:
b45d2719 852 offset += tree_low_cst (op->op1, 0);
53f3815c
RG
853 break;
854
855 case COMPONENT_REF:
b45d2719
RG
856 {
857 tree field = op->op0;
858 /* We do not have a complete COMPONENT_REF tree here so we
859 cannot use component_ref_field_offset. Do the interesting
860 parts manually. */
861
70f34814
RG
862 if (op->op1
863 || !host_integerp (DECL_FIELD_OFFSET (field), 1))
b45d2719
RG
864 max_size = -1;
865 else
866 {
867 offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
868 * BITS_PER_UNIT);
869 offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
870 }
871 break;
872 }
53f3815c
RG
873
874 case ARRAY_RANGE_REF:
875 case ARRAY_REF:
e52201b6
RG
876 /* We recorded the lower bound and the element size. */
877 if (!host_integerp (op->op0, 0)
878 || !host_integerp (op->op1, 0)
879 || !host_integerp (op->op2, 0))
b45d2719
RG
880 max_size = -1;
881 else
882 {
883 HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
e52201b6
RG
884 hindex -= TREE_INT_CST_LOW (op->op1);
885 hindex *= TREE_INT_CST_LOW (op->op2);
886 hindex *= BITS_PER_UNIT;
b45d2719
RG
887 offset += hindex;
888 }
889 break;
890
891 case REALPART_EXPR:
892 break;
893
894 case IMAGPART_EXPR:
895 offset += size;
896 break;
897
898 case VIEW_CONVERT_EXPR:
53f3815c
RG
899 break;
900
901 case STRING_CST:
902 case INTEGER_CST:
903 case COMPLEX_CST:
904 case VECTOR_CST:
905 case REAL_CST:
906 case CONSTRUCTOR:
53f3815c 907 case CONST_DECL:
b45d2719 908 return false;
53f3815c
RG
909
910 default:
b45d2719 911 return false;
53f3815c
RG
912 }
913 }
914
b45d2719
RG
915 if (base == NULL_TREE)
916 return false;
917
918 ref->ref = NULL_TREE;
919 ref->base = base;
920 ref->offset = offset;
921 ref->size = size;
922 ref->max_size = max_size;
923 ref->ref_alias_set = set;
70f34814
RG
924 if (base_alias_set != -1)
925 ref->base_alias_set = base_alias_set;
926 else
927 ref->base_alias_set = get_alias_set (base);
14cd91f9
RG
928 /* We discount volatiles from value-numbering elsewhere. */
929 ref->volatile_p = false;
b45d2719
RG
930
931 return true;
53f3815c
RG
932}
933
726a989a
RB
934/* Copy the operations present in load/store/call REF into RESULT, a vector of
935 vn_reference_op_s's. */
936
937void
938copy_reference_ops_from_call (gimple call,
939 VEC(vn_reference_op_s, heap) **result)
940{
941 vn_reference_op_s temp;
726a989a 942 unsigned i;
6867d9a9
TV
943 tree lhs = gimple_call_lhs (call);
944
945 /* If 2 calls have a different non-ssa lhs, vdef value numbers should be
946 different. By adding the lhs here in the vector, we ensure that the
947 hashcode is different, guaranteeing a different value number. */
948 if (lhs && TREE_CODE (lhs) != SSA_NAME)
949 {
950 memset (&temp, 0, sizeof (temp));
951 temp.opcode = MODIFY_EXPR;
952 temp.type = TREE_TYPE (lhs);
953 temp.op0 = lhs;
954 temp.off = -1;
955 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
956 }
726a989a 957
7aec7a38 958 /* Copy the type, opcode, function being called and static chain. */
726a989a
RB
959 memset (&temp, 0, sizeof (temp));
960 temp.type = gimple_call_return_type (call);
961 temp.opcode = CALL_EXPR;
ce94d354 962 temp.op0 = gimple_call_fn (call);
7aec7a38 963 temp.op1 = gimple_call_chain (call);
70f34814 964 temp.off = -1;
726a989a
RB
965 VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
966
ce94d354
RG
967 /* Copy the call arguments. As they can be references as well,
968 just chain them together. */
726a989a
RB
969 for (i = 0; i < gimple_call_num_args (call); ++i)
970 {
971 tree callarg = gimple_call_arg (call, i);
ce94d354 972 copy_reference_ops_from_ref (callarg, result);
726a989a 973 }
726a989a
RB
974}
975
89fb70a3
DB
976/* Create a vector of vn_reference_op_s structures from REF, a
977 REFERENCE_CLASS_P tree. The vector is not shared. */
978
979static VEC(vn_reference_op_s, heap) *
980create_reference_ops_from_ref (tree ref)
981{
982 VEC (vn_reference_op_s, heap) *result = NULL;
983
984 copy_reference_ops_from_ref (ref, &result);
985 return result;
986}
987
726a989a
RB
988/* Create a vector of vn_reference_op_s structures from CALL, a
989 call statement. The vector is not shared. */
990
991static VEC(vn_reference_op_s, heap) *
992create_reference_ops_from_call (gimple call)
993{
994 VEC (vn_reference_op_s, heap) *result = NULL;
995
996 copy_reference_ops_from_call (call, &result);
997 return result;
998}
999
aa7069aa
RG
1000/* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1001 *I_P to point to the last element of the replacement. */
1002void
1003vn_reference_fold_indirect (VEC (vn_reference_op_s, heap) **ops,
1004 unsigned int *i_p)
89fb70a3 1005{
aa7069aa 1006 unsigned int i = *i_p;
0823efed
DN
1007 vn_reference_op_t op = &VEC_index (vn_reference_op_s, *ops, i);
1008 vn_reference_op_t mem_op = &VEC_index (vn_reference_op_s, *ops, i - 1);
70f34814
RG
1009 tree addr_base;
1010 HOST_WIDE_INT addr_offset;
1011
1012 /* The only thing we have to do is from &OBJ.foo.bar add the offset
073a8998 1013 from .foo.bar to the preceding MEM_REF offset and replace the
70f34814
RG
1014 address with &OBJ. */
1015 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (op->op0, 0),
1016 &addr_offset);
1017 gcc_checking_assert (addr_base && TREE_CODE (addr_base) != MEM_REF);
1018 if (addr_base != op->op0)
1019 {
1020 double_int off = tree_to_double_int (mem_op->op0);
1021 off = double_int_sext (off, TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1022 off = double_int_add (off, shwi_to_double_int (addr_offset));
1023 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1024 op->op0 = build_fold_addr_expr (addr_base);
1025 if (host_integerp (mem_op->op0, 0))
1026 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1027 else
1028 mem_op->off = -1;
aa7069aa 1029 }
aa7069aa 1030}
89fb70a3 1031
a03a9774
RG
1032/* Fold *& at position *I_P in a vn_reference_op_s vector *OPS. Updates
1033 *I_P to point to the last element of the replacement. */
1034static void
1035vn_reference_maybe_forwprop_address (VEC (vn_reference_op_s, heap) **ops,
1036 unsigned int *i_p)
1037{
1038 unsigned int i = *i_p;
0823efed
DN
1039 vn_reference_op_t op = &VEC_index (vn_reference_op_s, *ops, i);
1040 vn_reference_op_t mem_op = &VEC_index (vn_reference_op_s, *ops, i - 1);
a03a9774
RG
1041 gimple def_stmt;
1042 enum tree_code code;
1043 double_int off;
1044
1045 def_stmt = SSA_NAME_DEF_STMT (op->op0);
d0c422cb 1046 if (!is_gimple_assign (def_stmt))
a03a9774
RG
1047 return;
1048
1049 code = gimple_assign_rhs_code (def_stmt);
1050 if (code != ADDR_EXPR
1051 && code != POINTER_PLUS_EXPR)
1052 return;
1053
1054 off = tree_to_double_int (mem_op->op0);
1055 off = double_int_sext (off, TYPE_PRECISION (TREE_TYPE (mem_op->op0)));
1056
1057 /* The only thing we have to do is from &OBJ.foo.bar add the offset
073a8998 1058 from .foo.bar to the preceding MEM_REF offset and replace the
a03a9774
RG
1059 address with &OBJ. */
1060 if (code == ADDR_EXPR)
1061 {
1062 tree addr, addr_base;
1063 HOST_WIDE_INT addr_offset;
1064
1065 addr = gimple_assign_rhs1 (def_stmt);
1066 addr_base = get_addr_base_and_unit_offset (TREE_OPERAND (addr, 0),
1067 &addr_offset);
1068 if (!addr_base
1069 || TREE_CODE (addr_base) != MEM_REF)
1070 return;
1071
1072 off = double_int_add (off, shwi_to_double_int (addr_offset));
1073 off = double_int_add (off, mem_ref_offset (addr_base));
1074 op->op0 = TREE_OPERAND (addr_base, 0);
1075 }
1076 else
1077 {
1078 tree ptr, ptroff;
1079 ptr = gimple_assign_rhs1 (def_stmt);
1080 ptroff = gimple_assign_rhs2 (def_stmt);
1081 if (TREE_CODE (ptr) != SSA_NAME
1082 || TREE_CODE (ptroff) != INTEGER_CST)
1083 return;
1084
1085 off = double_int_add (off, tree_to_double_int (ptroff));
d0c422cb 1086 op->op0 = ptr;
a03a9774
RG
1087 }
1088
1089 mem_op->op0 = double_int_to_tree (TREE_TYPE (mem_op->op0), off);
1090 if (host_integerp (mem_op->op0, 0))
1091 mem_op->off = TREE_INT_CST_LOW (mem_op->op0);
1092 else
1093 mem_op->off = -1;
1094 if (TREE_CODE (op->op0) == SSA_NAME)
e093ffe3
RG
1095 op->op0 = SSA_VAL (op->op0);
1096 if (TREE_CODE (op->op0) != SSA_NAME)
1097 op->opcode = TREE_CODE (op->op0);
a03a9774
RG
1098
1099 /* And recurse. */
1100 if (TREE_CODE (op->op0) == SSA_NAME)
1101 vn_reference_maybe_forwprop_address (ops, i_p);
1102 else if (TREE_CODE (op->op0) == ADDR_EXPR)
1103 vn_reference_fold_indirect (ops, i_p);
1104}
1105
12bd5a1e
RG
1106/* Optimize the reference REF to a constant if possible or return
1107 NULL_TREE if not. */
1108
1109tree
1110fully_constant_vn_reference_p (vn_reference_t ref)
1111{
1112 VEC (vn_reference_op_s, heap) *operands = ref->operands;
1113 vn_reference_op_t op;
1114
1115 /* Try to simplify the translated expression if it is
1116 a call to a builtin function with at most two arguments. */
0823efed 1117 op = &VEC_index (vn_reference_op_s, operands, 0);
12bd5a1e
RG
1118 if (op->opcode == CALL_EXPR
1119 && TREE_CODE (op->op0) == ADDR_EXPR
1120 && TREE_CODE (TREE_OPERAND (op->op0, 0)) == FUNCTION_DECL
1121 && DECL_BUILT_IN (TREE_OPERAND (op->op0, 0))
1122 && VEC_length (vn_reference_op_s, operands) >= 2
1123 && VEC_length (vn_reference_op_s, operands) <= 3)
1124 {
1125 vn_reference_op_t arg0, arg1 = NULL;
1126 bool anyconst = false;
0823efed 1127 arg0 = &VEC_index (vn_reference_op_s, operands, 1);
12bd5a1e 1128 if (VEC_length (vn_reference_op_s, operands) > 2)
0823efed 1129 arg1 = &VEC_index (vn_reference_op_s, operands, 2);
12bd5a1e
RG
1130 if (TREE_CODE_CLASS (arg0->opcode) == tcc_constant
1131 || (arg0->opcode == ADDR_EXPR
1132 && is_gimple_min_invariant (arg0->op0)))
1133 anyconst = true;
1134 if (arg1
1135 && (TREE_CODE_CLASS (arg1->opcode) == tcc_constant
1136 || (arg1->opcode == ADDR_EXPR
1137 && is_gimple_min_invariant (arg1->op0))))
1138 anyconst = true;
1139 if (anyconst)
1140 {
1141 tree folded = build_call_expr (TREE_OPERAND (op->op0, 0),
1142 arg1 ? 2 : 1,
1143 arg0->op0,
1144 arg1 ? arg1->op0 : NULL);
1145 if (folded
1146 && TREE_CODE (folded) == NOP_EXPR)
1147 folded = TREE_OPERAND (folded, 0);
1148 if (folded
1149 && is_gimple_min_invariant (folded))
1150 return folded;
1151 }
1152 }
1153
1154 /* Simplify reads from constant strings. */
1155 else if (op->opcode == ARRAY_REF
1156 && TREE_CODE (op->op0) == INTEGER_CST
1157 && integer_zerop (op->op1)
1158 && VEC_length (vn_reference_op_s, operands) == 2)
1159 {
1160 vn_reference_op_t arg0;
0823efed 1161 arg0 = &VEC_index (vn_reference_op_s, operands, 1);
12bd5a1e
RG
1162 if (arg0->opcode == STRING_CST
1163 && (TYPE_MODE (op->type)
1164 == TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0->op0))))
1165 && GET_MODE_CLASS (TYPE_MODE (op->type)) == MODE_INT
1166 && GET_MODE_SIZE (TYPE_MODE (op->type)) == 1
1167 && compare_tree_int (op->op0, TREE_STRING_LENGTH (arg0->op0)) < 0)
1168 return build_int_cst_type (op->type,
1169 (TREE_STRING_POINTER (arg0->op0)
1170 [TREE_INT_CST_LOW (op->op0)]));
1171 }
1172
1173 return NULL_TREE;
1174}
1175
89fb70a3
DB
1176/* Transform any SSA_NAME's in a vector of vn_reference_op_s
1177 structures into their value numbers. This is done in-place, and
3ceaf2f5
RG
1178 the vector passed in is returned. *VALUEIZED_ANYTHING will specify
1179 whether any operands were valueized. */
89fb70a3
DB
1180
1181static VEC (vn_reference_op_s, heap) *
3ceaf2f5 1182valueize_refs_1 (VEC (vn_reference_op_s, heap) *orig, bool *valueized_anything)
89fb70a3
DB
1183{
1184 vn_reference_op_t vro;
aa7069aa 1185 unsigned int i;
89fb70a3 1186
3ceaf2f5
RG
1187 *valueized_anything = false;
1188
ac47786e 1189 FOR_EACH_VEC_ELT (vn_reference_op_s, orig, i, vro)
89fb70a3
DB
1190 {
1191 if (vro->opcode == SSA_NAME
1192 || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
c9145754 1193 {
3ceaf2f5
RG
1194 tree tem = SSA_VAL (vro->op0);
1195 if (tem != vro->op0)
1196 {
1197 *valueized_anything = true;
1198 vro->op0 = tem;
1199 }
c9145754
DB
1200 /* If it transforms from an SSA_NAME to a constant, update
1201 the opcode. */
1202 if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
1203 vro->opcode = TREE_CODE (vro->op0);
1204 }
aa7069aa 1205 if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
3ceaf2f5
RG
1206 {
1207 tree tem = SSA_VAL (vro->op1);
1208 if (tem != vro->op1)
1209 {
1210 *valueized_anything = true;
1211 vro->op1 = tem;
1212 }
1213 }
aa7069aa 1214 if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
3ceaf2f5
RG
1215 {
1216 tree tem = SSA_VAL (vro->op2);
1217 if (tem != vro->op2)
1218 {
1219 *valueized_anything = true;
1220 vro->op2 = tem;
1221 }
1222 }
70f34814
RG
1223 /* If it transforms from an SSA_NAME to an address, fold with
1224 a preceding indirect reference. */
1225 if (i > 0
1226 && vro->op0
1227 && TREE_CODE (vro->op0) == ADDR_EXPR
1228 && VEC_index (vn_reference_op_s,
0823efed 1229 orig, i - 1).opcode == MEM_REF)
70f34814 1230 vn_reference_fold_indirect (&orig, &i);
a03a9774
RG
1231 else if (i > 0
1232 && vro->opcode == SSA_NAME
1233 && VEC_index (vn_reference_op_s,
0823efed 1234 orig, i - 1).opcode == MEM_REF)
a03a9774 1235 vn_reference_maybe_forwprop_address (&orig, &i);
70f34814
RG
1236 /* If it transforms a non-constant ARRAY_REF into a constant
1237 one, adjust the constant offset. */
1238 else if (vro->opcode == ARRAY_REF
1239 && vro->off == -1
1240 && TREE_CODE (vro->op0) == INTEGER_CST
1241 && TREE_CODE (vro->op1) == INTEGER_CST
1242 && TREE_CODE (vro->op2) == INTEGER_CST)
1243 {
1244 double_int off = tree_to_double_int (vro->op0);
1245 off = double_int_add (off,
1246 double_int_neg
1247 (tree_to_double_int (vro->op1)));
1248 off = double_int_mul (off, tree_to_double_int (vro->op2));
1249 if (double_int_fits_in_shwi_p (off))
1250 vro->off = off.low;
1251 }
89fb70a3
DB
1252 }
1253
1254 return orig;
1255}
1256
3ceaf2f5
RG
1257static VEC (vn_reference_op_s, heap) *
1258valueize_refs (VEC (vn_reference_op_s, heap) *orig)
1259{
1260 bool tem;
1261 return valueize_refs_1 (orig, &tem);
1262}
1263
aa7069aa
RG
1264static VEC(vn_reference_op_s, heap) *shared_lookup_references;
1265
1266/* Create a vector of vn_reference_op_s structures from REF, a
1267 REFERENCE_CLASS_P tree. The vector is shared among all callers of
3ceaf2f5
RG
1268 this function. *VALUEIZED_ANYTHING will specify whether any
1269 operands were valueized. */
aa7069aa
RG
1270
1271static VEC(vn_reference_op_s, heap) *
3ceaf2f5 1272valueize_shared_reference_ops_from_ref (tree ref, bool *valueized_anything)
aa7069aa
RG
1273{
1274 if (!ref)
1275 return NULL;
1276 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1277 copy_reference_ops_from_ref (ref, &shared_lookup_references);
3ceaf2f5
RG
1278 shared_lookup_references = valueize_refs_1 (shared_lookup_references,
1279 valueized_anything);
aa7069aa
RG
1280 return shared_lookup_references;
1281}
1282
1283/* Create a vector of vn_reference_op_s structures from CALL, a
1284 call statement. The vector is shared among all callers of
1285 this function. */
1286
1287static VEC(vn_reference_op_s, heap) *
1288valueize_shared_reference_ops_from_call (gimple call)
1289{
1290 if (!call)
1291 return NULL;
1292 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1293 copy_reference_ops_from_call (call, &shared_lookup_references);
1294 shared_lookup_references = valueize_refs (shared_lookup_references);
1295 return shared_lookup_references;
1296}
1297
896c8b96
RG
1298/* Lookup a SCCVN reference operation VR in the current hash table.
1299 Returns the resulting value number if it exists in the hash table,
c9145754
DB
1300 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1301 vn_reference_t stored in the hashtable if something is found. */
896c8b96
RG
1302
1303static tree
c9145754 1304vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
896c8b96
RG
1305{
1306 void **slot;
1307 hashval_t hash;
1308
1309 hash = vr->hashcode;
1310 slot = htab_find_slot_with_hash (current_info->references, vr,
1311 hash, NO_INSERT);
1312 if (!slot && current_info == optimistic_info)
1313 slot = htab_find_slot_with_hash (valid_info->references, vr,
1314 hash, NO_INSERT);
1315 if (slot)
c9145754
DB
1316 {
1317 if (vnresult)
1318 *vnresult = (vn_reference_t)*slot;
1319 return ((vn_reference_t)*slot)->result;
1320 }
b8698a0f 1321
896c8b96
RG
1322 return NULL_TREE;
1323}
1324
d0ca0bcb 1325static tree *last_vuse_ptr;
3bc27de7 1326static vn_lookup_kind vn_walk_kind;
1ec87690 1327static vn_lookup_kind default_vn_walk_kind;
d0ca0bcb 1328
5006671f
RG
1329/* Callback for walk_non_aliased_vuses. Adjusts the vn_reference_t VR_
1330 with the current VUSE and performs the expression lookup. */
1331
1332static void *
b45d2719 1333vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, void *vr_)
5006671f
RG
1334{
1335 vn_reference_t vr = (vn_reference_t)vr_;
1336 void **slot;
1337 hashval_t hash;
1338
d0ca0bcb
RG
1339 if (last_vuse_ptr)
1340 *last_vuse_ptr = vuse;
1341
5006671f 1342 /* Fixup vuse and hash. */
9708c51d
RG
1343 if (vr->vuse)
1344 vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
5006671f 1345 vr->vuse = SSA_VAL (vuse);
9708c51d
RG
1346 if (vr->vuse)
1347 vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
5006671f
RG
1348
1349 hash = vr->hashcode;
1350 slot = htab_find_slot_with_hash (current_info->references, vr,
1351 hash, NO_INSERT);
1352 if (!slot && current_info == optimistic_info)
1353 slot = htab_find_slot_with_hash (valid_info->references, vr,
1354 hash, NO_INSERT);
1355 if (slot)
1356 return *slot;
b8698a0f 1357
5006671f
RG
1358 return NULL;
1359}
c9145754 1360
b55eb410
RG
1361/* Lookup an existing or insert a new vn_reference entry into the
1362 value table for the VUSE, SET, TYPE, OPERANDS reference which
9179ed9d 1363 has the value VALUE which is either a constant or an SSA name. */
b55eb410
RG
1364
1365static vn_reference_t
9179ed9d
RG
1366vn_reference_lookup_or_insert_for_pieces (tree vuse,
1367 alias_set_type set,
1368 tree type,
1369 VEC (vn_reference_op_s,
1370 heap) *operands,
1371 tree value)
b55eb410
RG
1372{
1373 struct vn_reference_s vr1;
1374 vn_reference_t result;
9179ed9d 1375 unsigned value_id;
b55eb410
RG
1376 vr1.vuse = vuse;
1377 vr1.operands = operands;
1378 vr1.type = type;
1379 vr1.set = set;
1380 vr1.hashcode = vn_reference_compute_hash (&vr1);
1381 if (vn_reference_lookup_1 (&vr1, &result))
1382 return result;
9179ed9d
RG
1383 if (TREE_CODE (value) == SSA_NAME)
1384 value_id = VN_INFO (value)->value_id;
1385 else
1386 value_id = get_or_alloc_constant_value_id (value);
b55eb410
RG
1387 return vn_reference_insert_pieces (vuse, set, type,
1388 VEC_copy (vn_reference_op_s, heap,
9179ed9d 1389 operands), value, value_id);
b55eb410
RG
1390}
1391
01df5c8a
RG
1392/* Callback for walk_non_aliased_vuses. Tries to perform a lookup
1393 from the statement defining VUSE and if not successful tries to
073a8998 1394 translate *REFP and VR_ through an aggregate copy at the definition
01df5c8a
RG
1395 of VUSE. */
1396
1397static void *
b45d2719 1398vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
01df5c8a
RG
1399{
1400 vn_reference_t vr = (vn_reference_t)vr_;
1401 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
01df5c8a 1402 tree base;
0f900dfa 1403 HOST_WIDE_INT offset, maxsize;
8ea34dab
RG
1404 static VEC (vn_reference_op_s, heap) *lhs_ops = NULL;
1405 ao_ref lhs_ref;
1406 bool lhs_ref_ok = false;
01df5c8a 1407
4fa4929e
RG
1408 /* First try to disambiguate after value-replacing in the definitions LHS. */
1409 if (is_gimple_assign (def_stmt))
1410 {
8ea34dab 1411 VEC (vn_reference_op_s, heap) *tem;
4fa4929e 1412 tree lhs = gimple_assign_lhs (def_stmt);
25aa059e 1413 bool valueized_anything = false;
8ea34dab
RG
1414 /* Avoid re-allocation overhead. */
1415 VEC_truncate (vn_reference_op_s, lhs_ops, 0);
1416 copy_reference_ops_from_ref (lhs, &lhs_ops);
1417 tem = lhs_ops;
25aa059e 1418 lhs_ops = valueize_refs_1 (lhs_ops, &valueized_anything);
8ea34dab 1419 gcc_assert (lhs_ops == tem);
25aa059e
RG
1420 if (valueized_anything)
1421 {
1422 lhs_ref_ok = ao_ref_init_from_vn_reference (&lhs_ref,
1423 get_alias_set (lhs),
1424 TREE_TYPE (lhs), lhs_ops);
1425 if (lhs_ref_ok
1426 && !refs_may_alias_p_1 (ref, &lhs_ref, true))
1427 return NULL;
1428 }
1429 else
1430 {
1431 ao_ref_init (&lhs_ref, lhs);
1432 lhs_ref_ok = true;
1433 }
4fa4929e
RG
1434 }
1435
b45d2719
RG
1436 base = ao_ref_base (ref);
1437 offset = ref->offset;
b45d2719 1438 maxsize = ref->max_size;
01df5c8a
RG
1439
1440 /* If we cannot constrain the size of the reference we cannot
1441 test if anything kills it. */
1442 if (maxsize == -1)
1443 return (void *)-1;
1444
47598145
MM
1445 /* We can't deduce anything useful from clobbers. */
1446 if (gimple_clobber_p (def_stmt))
1447 return (void *)-1;
1448
01df5c8a 1449 /* def_stmt may-defs *ref. See if we can derive a value for *ref
47598145 1450 from that definition.
01df5c8a 1451 1) Memset. */
b45d2719 1452 if (is_gimple_reg_type (vr->type)
c7ee7b45 1453 && gimple_call_builtin_p (def_stmt, BUILT_IN_MEMSET)
01df5c8a
RG
1454 && integer_zerop (gimple_call_arg (def_stmt, 1))
1455 && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1456 && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1457 {
1458 tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1459 tree base2;
1460 HOST_WIDE_INT offset2, size2, maxsize2;
1461 base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1462 size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1463 if ((unsigned HOST_WIDE_INT)size2 / 8
1464 == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
9c8cbc74 1465 && maxsize2 != -1
01df5c8a
RG
1466 && operand_equal_p (base, base2, 0)
1467 && offset2 <= offset
1468 && offset2 + size2 >= offset + maxsize)
b45d2719 1469 {
e8160c9a 1470 tree val = build_zero_cst (vr->type);
9179ed9d 1471 return vn_reference_lookup_or_insert_for_pieces
b55eb410 1472 (vuse, vr->set, vr->type, vr->operands, val);
b45d2719 1473 }
01df5c8a
RG
1474 }
1475
1476 /* 2) Assignment from an empty CONSTRUCTOR. */
b45d2719 1477 else if (is_gimple_reg_type (vr->type)
01df5c8a
RG
1478 && gimple_assign_single_p (def_stmt)
1479 && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1480 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1481 {
1482 tree base2;
1483 HOST_WIDE_INT offset2, size2, maxsize2;
1484 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1485 &offset2, &size2, &maxsize2);
9c8cbc74
EB
1486 if (maxsize2 != -1
1487 && operand_equal_p (base, base2, 0)
01df5c8a
RG
1488 && offset2 <= offset
1489 && offset2 + size2 >= offset + maxsize)
b45d2719 1490 {
e8160c9a 1491 tree val = build_zero_cst (vr->type);
9179ed9d 1492 return vn_reference_lookup_or_insert_for_pieces
b55eb410 1493 (vuse, vr->set, vr->type, vr->operands, val);
b45d2719 1494 }
01df5c8a
RG
1495 }
1496
c867aba0
RG
1497 /* 3) Assignment from a constant. We can use folds native encode/interpret
1498 routines to extract the assigned bits. */
1499 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
1500 && ref->size == maxsize
1501 && maxsize % BITS_PER_UNIT == 0
1502 && offset % BITS_PER_UNIT == 0
1503 && is_gimple_reg_type (vr->type)
1504 && gimple_assign_single_p (def_stmt)
1505 && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
1506 {
1507 tree base2;
1508 HOST_WIDE_INT offset2, size2, maxsize2;
1509 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1510 &offset2, &size2, &maxsize2);
1511 if (maxsize2 != -1
1512 && maxsize2 == size2
1513 && size2 % BITS_PER_UNIT == 0
1514 && offset2 % BITS_PER_UNIT == 0
1515 && operand_equal_p (base, base2, 0)
1516 && offset2 <= offset
1517 && offset2 + size2 >= offset + maxsize)
1518 {
1519 /* We support up to 512-bit values (for V8DFmode). */
1520 unsigned char buffer[64];
1521 int len;
1522
1523 len = native_encode_expr (gimple_assign_rhs1 (def_stmt),
1524 buffer, sizeof (buffer));
1525 if (len > 0)
1526 {
1527 tree val = native_interpret_expr (vr->type,
1528 buffer
1529 + ((offset - offset2)
1530 / BITS_PER_UNIT),
1531 ref->size / BITS_PER_UNIT);
1532 if (val)
9179ed9d 1533 return vn_reference_lookup_or_insert_for_pieces
b55eb410 1534 (vuse, vr->set, vr->type, vr->operands, val);
c867aba0
RG
1535 }
1536 }
1537 }
1538
0147184e
RG
1539 /* 4) Assignment from an SSA name which definition we may be able
1540 to access pieces from. */
1541 else if (ref->size == maxsize
1542 && is_gimple_reg_type (vr->type)
1543 && gimple_assign_single_p (def_stmt)
1544 && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
1545 {
1546 tree rhs1 = gimple_assign_rhs1 (def_stmt);
1547 gimple def_stmt2 = SSA_NAME_DEF_STMT (rhs1);
1548 if (is_gimple_assign (def_stmt2)
1549 && (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR
1550 || gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR)
1551 && types_compatible_p (vr->type, TREE_TYPE (TREE_TYPE (rhs1))))
1552 {
1553 tree base2;
1554 HOST_WIDE_INT offset2, size2, maxsize2, off;
1555 base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1556 &offset2, &size2, &maxsize2);
1557 off = offset - offset2;
1558 if (maxsize2 != -1
1559 && maxsize2 == size2
1560 && operand_equal_p (base, base2, 0)
1561 && offset2 <= offset
1562 && offset2 + size2 >= offset + maxsize)
1563 {
1564 tree val = NULL_TREE;
1565 HOST_WIDE_INT elsz
1566 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (rhs1))));
1567 if (gimple_assign_rhs_code (def_stmt2) == COMPLEX_EXPR)
1568 {
1569 if (off == 0)
1570 val = gimple_assign_rhs1 (def_stmt2);
1571 else if (off == elsz)
1572 val = gimple_assign_rhs2 (def_stmt2);
1573 }
1574 else if (gimple_assign_rhs_code (def_stmt2) == CONSTRUCTOR
1575 && off % elsz == 0)
1576 {
1577 tree ctor = gimple_assign_rhs1 (def_stmt2);
1578 unsigned i = off / elsz;
1579 if (i < CONSTRUCTOR_NELTS (ctor))
1580 {
1581 constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i);
1582 if (compare_tree_int (elt->index, i) == 0)
1583 val = elt->value;
1584 }
1585 }
1586 if (val)
9179ed9d 1587 return vn_reference_lookup_or_insert_for_pieces
b55eb410 1588 (vuse, vr->set, vr->type, vr->operands, val);
0147184e
RG
1589 }
1590 }
1591 }
1592
1593 /* 5) For aggregate copies translate the reference through them if
01df5c8a 1594 the copy kills ref. */
3bc27de7
RG
1595 else if (vn_walk_kind == VN_WALKREWRITE
1596 && gimple_assign_single_p (def_stmt)
01df5c8a 1597 && (DECL_P (gimple_assign_rhs1 (def_stmt))
70f34814 1598 || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == MEM_REF
01df5c8a
RG
1599 || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1600 {
1601 tree base2;
9c8cbc74 1602 HOST_WIDE_INT offset2, size2, maxsize2;
01df5c8a 1603 int i, j;
8ea34dab 1604 VEC (vn_reference_op_s, heap) *rhs = NULL;
01df5c8a 1605 vn_reference_op_t vro;
b45d2719 1606 ao_ref r;
01df5c8a 1607
8ea34dab
RG
1608 if (!lhs_ref_ok)
1609 return (void *)-1;
1610
01df5c8a 1611 /* See if the assignment kills REF. */
8ea34dab
RG
1612 base2 = ao_ref_base (&lhs_ref);
1613 offset2 = lhs_ref.offset;
1614 size2 = lhs_ref.size;
9c8cbc74
EB
1615 maxsize2 = lhs_ref.max_size;
1616 if (maxsize2 == -1
1617 || (base != base2 && !operand_equal_p (base, base2, 0))
01df5c8a
RG
1618 || offset2 > offset
1619 || offset2 + size2 < offset + maxsize)
1620 return (void *)-1;
1621
8ea34dab
RG
1622 /* Find the common base of ref and the lhs. lhs_ops already
1623 contains valueized operands for the lhs. */
01df5c8a 1624 i = VEC_length (vn_reference_op_s, vr->operands) - 1;
8ea34dab 1625 j = VEC_length (vn_reference_op_s, lhs_ops) - 1;
35ecd408 1626 while (j >= 0 && i >= 0
0823efed
DN
1627 && vn_reference_op_eq (&VEC_index (vn_reference_op_s,
1628 vr->operands, i),
1629 &VEC_index (vn_reference_op_s, lhs_ops, j)))
01df5c8a
RG
1630 {
1631 i--;
1632 j--;
1633 }
35ecd408 1634
25aa059e
RG
1635 /* ??? The innermost op should always be a MEM_REF and we already
1636 checked that the assignment to the lhs kills vr. Thus for
1637 aggregate copies using char[] types the vn_reference_op_eq
1638 may fail when comparing types for compatibility. But we really
1639 don't care here - further lookups with the rewritten operands
1640 will simply fail if we messed up types too badly. */
4f9dbaaa 1641 if (j == 0 && i >= 0
0823efed
DN
1642 && VEC_index (vn_reference_op_s, lhs_ops, 0).opcode == MEM_REF
1643 && VEC_index (vn_reference_op_s, lhs_ops, 0).off != -1
1644 && (VEC_index (vn_reference_op_s, lhs_ops, 0).off
1645 == VEC_index (vn_reference_op_s, vr->operands, i).off))
25aa059e
RG
1646 i--, j--;
1647
01df5c8a
RG
1648 /* i now points to the first additional op.
1649 ??? LHS may not be completely contained in VR, one or more
1650 VIEW_CONVERT_EXPRs could be in its way. We could at least
1651 try handling outermost VIEW_CONVERT_EXPRs. */
1652 if (j != -1)
1653 return (void *)-1;
01df5c8a
RG
1654
1655 /* Now re-write REF to be based on the rhs of the assignment. */
1656 copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1657 /* We need to pre-pend vr->operands[0..i] to rhs. */
1658 if (i + 1 + VEC_length (vn_reference_op_s, rhs)
1659 > VEC_length (vn_reference_op_s, vr->operands))
1660 {
1661 VEC (vn_reference_op_s, heap) *old = vr->operands;
1662 VEC_safe_grow (vn_reference_op_s, heap, vr->operands,
1663 i + 1 + VEC_length (vn_reference_op_s, rhs));
1664 if (old == shared_lookup_references
1665 && vr->operands != old)
1666 shared_lookup_references = NULL;
1667 }
1668 else
1669 VEC_truncate (vn_reference_op_s, vr->operands,
1670 i + 1 + VEC_length (vn_reference_op_s, rhs));
ac47786e 1671 FOR_EACH_VEC_ELT (vn_reference_op_s, rhs, j, vro)
0823efed 1672 VEC_replace (vn_reference_op_s, vr->operands, i + 1 + j, *vro);
01df5c8a 1673 VEC_free (vn_reference_op_s, heap, rhs);
b55eb410 1674 vr->operands = valueize_refs (vr->operands);
01df5c8a 1675 vr->hashcode = vn_reference_compute_hash (vr);
c7ee7b45
RG
1676
1677 /* Adjust *ref from the new operands. */
1678 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1679 return (void *)-1;
1680 /* This can happen with bitfields. */
1681 if (ref->size != r.size)
1682 return (void *)-1;
1683 *ref = r;
1684
1685 /* Do not update last seen VUSE after translating. */
1686 last_vuse_ptr = NULL;
1687
1688 /* Keep looking for the adjusted *REF / VR pair. */
1689 return NULL;
1690 }
1691
0147184e 1692 /* 6) For memcpy copies translate the reference through them if
c7ee7b45
RG
1693 the copy kills ref. */
1694 else if (vn_walk_kind == VN_WALKREWRITE
1695 && is_gimple_reg_type (vr->type)
1696 /* ??? Handle BCOPY as well. */
1697 && (gimple_call_builtin_p (def_stmt, BUILT_IN_MEMCPY)
1698 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMPCPY)
1699 || gimple_call_builtin_p (def_stmt, BUILT_IN_MEMMOVE))
1700 && (TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR
1701 || TREE_CODE (gimple_call_arg (def_stmt, 0)) == SSA_NAME)
1702 && (TREE_CODE (gimple_call_arg (def_stmt, 1)) == ADDR_EXPR
1703 || TREE_CODE (gimple_call_arg (def_stmt, 1)) == SSA_NAME)
1704 && host_integerp (gimple_call_arg (def_stmt, 2), 1))
1705 {
1706 tree lhs, rhs;
1707 ao_ref r;
1708 HOST_WIDE_INT rhs_offset, copy_size, lhs_offset;
1709 vn_reference_op_s op;
1710 HOST_WIDE_INT at;
1711
1712
1713 /* Only handle non-variable, addressable refs. */
1714 if (ref->size != maxsize
1715 || offset % BITS_PER_UNIT != 0
1716 || ref->size % BITS_PER_UNIT != 0)
1717 return (void *)-1;
1718
1719 /* Extract a pointer base and an offset for the destination. */
1720 lhs = gimple_call_arg (def_stmt, 0);
1721 lhs_offset = 0;
1722 if (TREE_CODE (lhs) == SSA_NAME)
1723 lhs = SSA_VAL (lhs);
1724 if (TREE_CODE (lhs) == ADDR_EXPR)
1725 {
1726 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (lhs, 0),
1727 &lhs_offset);
1728 if (!tem)
1729 return (void *)-1;
1730 if (TREE_CODE (tem) == MEM_REF
1731 && host_integerp (TREE_OPERAND (tem, 1), 1))
1732 {
1733 lhs = TREE_OPERAND (tem, 0);
1734 lhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1735 }
1736 else if (DECL_P (tem))
1737 lhs = build_fold_addr_expr (tem);
1738 else
1739 return (void *)-1;
1740 }
1741 if (TREE_CODE (lhs) != SSA_NAME
1742 && TREE_CODE (lhs) != ADDR_EXPR)
1743 return (void *)-1;
1744
1745 /* Extract a pointer base and an offset for the source. */
1746 rhs = gimple_call_arg (def_stmt, 1);
1747 rhs_offset = 0;
1748 if (TREE_CODE (rhs) == SSA_NAME)
1749 rhs = SSA_VAL (rhs);
1750 if (TREE_CODE (rhs) == ADDR_EXPR)
1751 {
1752 tree tem = get_addr_base_and_unit_offset (TREE_OPERAND (rhs, 0),
1753 &rhs_offset);
1754 if (!tem)
1755 return (void *)-1;
1756 if (TREE_CODE (tem) == MEM_REF
1757 && host_integerp (TREE_OPERAND (tem, 1), 1))
1758 {
1759 rhs = TREE_OPERAND (tem, 0);
1760 rhs_offset += TREE_INT_CST_LOW (TREE_OPERAND (tem, 1));
1761 }
1762 else if (DECL_P (tem))
1763 rhs = build_fold_addr_expr (tem);
1764 else
1765 return (void *)-1;
1766 }
1767 if (TREE_CODE (rhs) != SSA_NAME
1768 && TREE_CODE (rhs) != ADDR_EXPR)
1769 return (void *)-1;
1770
1771 copy_size = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2));
1772
1773 /* The bases of the destination and the references have to agree. */
1774 if ((TREE_CODE (base) != MEM_REF
1775 && !DECL_P (base))
1776 || (TREE_CODE (base) == MEM_REF
1777 && (TREE_OPERAND (base, 0) != lhs
1778 || !host_integerp (TREE_OPERAND (base, 1), 1)))
1779 || (DECL_P (base)
1780 && (TREE_CODE (lhs) != ADDR_EXPR
1781 || TREE_OPERAND (lhs, 0) != base)))
1782 return (void *)-1;
1783
1784 /* And the access has to be contained within the memcpy destination. */
1785 at = offset / BITS_PER_UNIT;
1786 if (TREE_CODE (base) == MEM_REF)
1787 at += TREE_INT_CST_LOW (TREE_OPERAND (base, 1));
1788 if (lhs_offset > at
1789 || lhs_offset + copy_size < at + maxsize / BITS_PER_UNIT)
1790 return (void *)-1;
1791
1792 /* Make room for 2 operands in the new reference. */
1793 if (VEC_length (vn_reference_op_s, vr->operands) < 2)
1794 {
1795 VEC (vn_reference_op_s, heap) *old = vr->operands;
1796 VEC_safe_grow (vn_reference_op_s, heap, vr->operands, 2);
1797 if (old == shared_lookup_references
1798 && vr->operands != old)
1799 shared_lookup_references = NULL;
1800 }
1801 else
1802 VEC_truncate (vn_reference_op_s, vr->operands, 2);
1803
1804 /* The looked-through reference is a simple MEM_REF. */
1805 memset (&op, 0, sizeof (op));
1806 op.type = vr->type;
1807 op.opcode = MEM_REF;
1808 op.op0 = build_int_cst (ptr_type_node, at - rhs_offset);
1809 op.off = at - lhs_offset + rhs_offset;
0823efed 1810 VEC_replace (vn_reference_op_s, vr->operands, 0, op);
6d6c9525 1811 op.type = TREE_TYPE (rhs);
c7ee7b45
RG
1812 op.opcode = TREE_CODE (rhs);
1813 op.op0 = rhs;
1814 op.off = -1;
0823efed 1815 VEC_replace (vn_reference_op_s, vr->operands, 1, op);
c7ee7b45 1816 vr->hashcode = vn_reference_compute_hash (vr);
b45d2719
RG
1817
1818 /* Adjust *ref from the new operands. */
1819 if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
01df5c8a 1820 return (void *)-1;
03472fdd
EB
1821 /* This can happen with bitfields. */
1822 if (ref->size != r.size)
1823 return (void *)-1;
b45d2719 1824 *ref = r;
01df5c8a 1825
d0ca0bcb
RG
1826 /* Do not update last seen VUSE after translating. */
1827 last_vuse_ptr = NULL;
1828
01df5c8a
RG
1829 /* Keep looking for the adjusted *REF / VR pair. */
1830 return NULL;
1831 }
1832
1833 /* Bail out and stop walking. */
1834 return (void *)-1;
1835}
1836
c9145754
DB
1837/* Lookup a reference operation by it's parts, in the current hash table.
1838 Returns the resulting value number if it exists in the hash table,
1839 NULL_TREE otherwise. VNRESULT will be filled in with the actual
1840 vn_reference_t stored in the hashtable if something is found. */
89fb70a3
DB
1841
1842tree
b45d2719 1843vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
c9145754 1844 VEC (vn_reference_op_s, heap) *operands,
3bc27de7 1845 vn_reference_t *vnresult, vn_lookup_kind kind)
c9145754
DB
1846{
1847 struct vn_reference_s vr1;
5006671f 1848 vn_reference_t tmp;
12bd5a1e 1849 tree cst;
5006671f
RG
1850
1851 if (!vnresult)
1852 vnresult = &tmp;
1853 *vnresult = NULL;
01df5c8a 1854
5006671f 1855 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
01df5c8a
RG
1856 VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1857 VEC_safe_grow (vn_reference_op_s, heap, shared_lookup_references,
1858 VEC_length (vn_reference_op_s, operands));
1859 memcpy (VEC_address (vn_reference_op_s, shared_lookup_references),
1860 VEC_address (vn_reference_op_s, operands),
1861 sizeof (vn_reference_op_s)
1862 * VEC_length (vn_reference_op_s, operands));
1863 vr1.operands = operands = shared_lookup_references
1864 = valueize_refs (shared_lookup_references);
b45d2719
RG
1865 vr1.type = type;
1866 vr1.set = set;
c9145754 1867 vr1.hashcode = vn_reference_compute_hash (&vr1);
12bd5a1e
RG
1868 if ((cst = fully_constant_vn_reference_p (&vr1)))
1869 return cst;
c9145754 1870
12bd5a1e 1871 vn_reference_lookup_1 (&vr1, vnresult);
5006671f 1872 if (!*vnresult
3bc27de7 1873 && kind != VN_NOWALK
5006671f 1874 && vr1.vuse)
53f3815c 1875 {
b45d2719 1876 ao_ref r;
3bc27de7 1877 vn_walk_kind = kind;
b45d2719 1878 if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
01df5c8a 1879 *vnresult =
b45d2719 1880 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
01df5c8a
RG
1881 vn_reference_lookup_2,
1882 vn_reference_lookup_3, &vr1);
1883 if (vr1.operands != operands)
1884 VEC_free (vn_reference_op_s, heap, vr1.operands);
53f3815c
RG
1885 }
1886
5006671f
RG
1887 if (*vnresult)
1888 return (*vnresult)->result;
1889
1890 return NULL_TREE;
c9145754
DB
1891}
1892
1893/* Lookup OP in the current hash table, and return the resulting value
1894 number if it exists in the hash table. Return NULL_TREE if it does
1895 not exist in the hash table or if the result field of the structure
1896 was NULL.. VNRESULT will be filled in with the vn_reference_t
1897 stored in the hashtable if one exists. */
1898
1899tree
3bc27de7 1900vn_reference_lookup (tree op, tree vuse, vn_lookup_kind kind,
c9145754 1901 vn_reference_t *vnresult)
89fb70a3 1902{
01df5c8a 1903 VEC (vn_reference_op_s, heap) *operands;
89fb70a3 1904 struct vn_reference_s vr1;
12bd5a1e 1905 tree cst;
3ceaf2f5 1906 bool valuezied_anything;
5006671f 1907
c9145754
DB
1908 if (vnresult)
1909 *vnresult = NULL;
89fb70a3 1910
5006671f 1911 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
3ceaf2f5
RG
1912 vr1.operands = operands
1913 = valueize_shared_reference_ops_from_ref (op, &valuezied_anything);
b45d2719
RG
1914 vr1.type = TREE_TYPE (op);
1915 vr1.set = get_alias_set (op);
89fb70a3 1916 vr1.hashcode = vn_reference_compute_hash (&vr1);
12bd5a1e
RG
1917 if ((cst = fully_constant_vn_reference_p (&vr1)))
1918 return cst;
896c8b96 1919
3bc27de7 1920 if (kind != VN_NOWALK
5006671f
RG
1921 && vr1.vuse)
1922 {
1923 vn_reference_t wvnresult;
b45d2719 1924 ao_ref r;
3ceaf2f5
RG
1925 /* Make sure to use a valueized reference if we valueized anything.
1926 Otherwise preserve the full reference for advanced TBAA. */
1927 if (!valuezied_anything
1928 || !ao_ref_init_from_vn_reference (&r, vr1.set, vr1.type,
1929 vr1.operands))
6d6c9525 1930 ao_ref_init (&r, op);
3bc27de7 1931 vn_walk_kind = kind;
5006671f 1932 wvnresult =
b45d2719 1933 (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
01df5c8a
RG
1934 vn_reference_lookup_2,
1935 vn_reference_lookup_3, &vr1);
1936 if (vr1.operands != operands)
1937 VEC_free (vn_reference_op_s, heap, vr1.operands);
5006671f
RG
1938 if (wvnresult)
1939 {
1940 if (vnresult)
1941 *vnresult = wvnresult;
1942 return wvnresult->result;
1943 }
1944
1945 return NULL_TREE;
896c8b96 1946 }
89fb70a3 1947
5006671f 1948 return vn_reference_lookup_1 (&vr1, vnresult);
89fb70a3
DB
1949}
1950
c9145754 1951
89fb70a3 1952/* Insert OP into the current hash table with a value number of
c9145754 1953 RESULT, and return the resulting reference structure we created. */
89fb70a3 1954
c9145754 1955vn_reference_t
4ec0a198 1956vn_reference_insert (tree op, tree result, tree vuse, tree vdef)
89fb70a3
DB
1957{
1958 void **slot;
1959 vn_reference_t vr1;
1960
1961 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
c9145754
DB
1962 if (TREE_CODE (result) == SSA_NAME)
1963 vr1->value_id = VN_INFO (result)->value_id;
1964 else
1965 vr1->value_id = get_or_alloc_constant_value_id (result);
5006671f 1966 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
89fb70a3 1967 vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
b45d2719
RG
1968 vr1->type = TREE_TYPE (op);
1969 vr1->set = get_alias_set (op);
89fb70a3
DB
1970 vr1->hashcode = vn_reference_compute_hash (vr1);
1971 vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
4ec0a198 1972 vr1->result_vdef = vdef;
89fb70a3
DB
1973
1974 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1975 INSERT);
1976
1977 /* Because we lookup stores using vuses, and value number failures
1978 using the vdefs (see visit_reference_op_store for how and why),
1979 it's possible that on failure we may try to insert an already
1980 inserted store. This is not wrong, there is no ssa name for a
1981 store that we could use as a differentiator anyway. Thus, unlike
1982 the other lookup functions, you cannot gcc_assert (!*slot)
1983 here. */
1984
8d0eca24
RG
1985 /* But free the old slot in case of a collision. */
1986 if (*slot)
1987 free_reference (*slot);
89fb70a3
DB
1988
1989 *slot = vr1;
c9145754
DB
1990 return vr1;
1991}
1992
1993/* Insert a reference by it's pieces into the current hash table with
1994 a value number of RESULT. Return the resulting reference
1995 structure we created. */
1996
1997vn_reference_t
b45d2719 1998vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
c9145754
DB
1999 VEC (vn_reference_op_s, heap) *operands,
2000 tree result, unsigned int value_id)
2001
2002{
2003 void **slot;
2004 vn_reference_t vr1;
2005
2006 vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
5006671f
RG
2007 vr1->value_id = value_id;
2008 vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
c9145754 2009 vr1->operands = valueize_refs (operands);
b45d2719
RG
2010 vr1->type = type;
2011 vr1->set = set;
c9145754
DB
2012 vr1->hashcode = vn_reference_compute_hash (vr1);
2013 if (result && TREE_CODE (result) == SSA_NAME)
2014 result = SSA_VAL (result);
2015 vr1->result = result;
2016
2017 slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
2018 INSERT);
b8698a0f 2019
c9145754 2020 /* At this point we should have all the things inserted that we have
5006671f
RG
2021 seen before, and we should never try inserting something that
2022 already exists. */
c9145754
DB
2023 gcc_assert (!*slot);
2024 if (*slot)
2025 free_reference (*slot);
2026
2027 *slot = vr1;
2028 return vr1;
89fb70a3
DB
2029}
2030
49a1fb2d 2031/* Compute and return the hash value for nary operation VBO1. */
89fb70a3 2032
5fd8300b 2033hashval_t
49a1fb2d 2034vn_nary_op_compute_hash (const vn_nary_op_t vno1)
89fb70a3 2035{
9708c51d 2036 hashval_t hash;
49a1fb2d 2037 unsigned i;
89fb70a3 2038
49a1fb2d
RG
2039 for (i = 0; i < vno1->length; ++i)
2040 if (TREE_CODE (vno1->op[i]) == SSA_NAME)
2041 vno1->op[i] = SSA_VAL (vno1->op[i]);
89fb70a3 2042
49a1fb2d
RG
2043 if (vno1->length == 2
2044 && commutative_tree_code (vno1->opcode)
2045 && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
2046 {
2047 tree temp = vno1->op[0];
2048 vno1->op[0] = vno1->op[1];
2049 vno1->op[1] = temp;
2050 }
89fb70a3 2051
9708c51d 2052 hash = iterative_hash_hashval_t (vno1->opcode, 0);
49a1fb2d 2053 for (i = 0; i < vno1->length; ++i)
9708c51d 2054 hash = iterative_hash_expr (vno1->op[i], hash);
89fb70a3 2055
49a1fb2d 2056 return hash;
89fb70a3
DB
2057}
2058
49a1fb2d 2059/* Return the computed hashcode for nary operation P1. */
89fb70a3
DB
2060
2061static hashval_t
49a1fb2d 2062vn_nary_op_hash (const void *p1)
89fb70a3 2063{
49a1fb2d
RG
2064 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
2065 return vno1->hashcode;
89fb70a3
DB
2066}
2067
49a1fb2d 2068/* Compare nary operations P1 and P2 and return true if they are
89fb70a3
DB
2069 equivalent. */
2070
c9145754 2071int
49a1fb2d 2072vn_nary_op_eq (const void *p1, const void *p2)
89fb70a3 2073{
49a1fb2d
RG
2074 const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
2075 const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
2076 unsigned i;
2077
85169114
PB
2078 if (vno1->hashcode != vno2->hashcode)
2079 return false;
2080
5a7d7f9c
RG
2081 if (vno1->length != vno2->length)
2082 return false;
2083
49a1fb2d 2084 if (vno1->opcode != vno2->opcode
63a14fa3 2085 || !types_compatible_p (vno1->type, vno2->type))
49a1fb2d
RG
2086 return false;
2087
2088 for (i = 0; i < vno1->length; ++i)
2089 if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
2090 return false;
2091
2092 return true;
89fb70a3
DB
2093}
2094
9ad6bebe 2095/* Initialize VNO from the pieces provided. */
89fb70a3 2096
9ad6bebe
NF
2097static void
2098init_vn_nary_op_from_pieces (vn_nary_op_t vno, unsigned int length,
5a7d7f9c 2099 enum tree_code code, tree type, tree *ops)
9ad6bebe
NF
2100{
2101 vno->opcode = code;
2102 vno->length = length;
2103 vno->type = type;
5a7d7f9c 2104 memcpy (&vno->op[0], ops, sizeof (tree) * length);
9ad6bebe
NF
2105}
2106
2107/* Initialize VNO from OP. */
2108
2109static void
2110init_vn_nary_op_from_op (vn_nary_op_t vno, tree op)
2111{
2112 unsigned i;
2113
2114 vno->opcode = TREE_CODE (op);
2115 vno->length = TREE_CODE_LENGTH (TREE_CODE (op));
2116 vno->type = TREE_TYPE (op);
2117 for (i = 0; i < vno->length; ++i)
2118 vno->op[i] = TREE_OPERAND (op, i);
2119}
2120
5a7d7f9c
RG
2121/* Return the number of operands for a vn_nary ops structure from STMT. */
2122
2123static unsigned int
2124vn_nary_length_from_stmt (gimple stmt)
2125{
2126 switch (gimple_assign_rhs_code (stmt))
2127 {
2128 case REALPART_EXPR:
2129 case IMAGPART_EXPR:
2130 case VIEW_CONVERT_EXPR:
2131 return 1;
2132
2133 case CONSTRUCTOR:
2134 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2135
2136 default:
2137 return gimple_num_ops (stmt) - 1;
2138 }
2139}
2140
9ad6bebe
NF
2141/* Initialize VNO from STMT. */
2142
2143static void
2144init_vn_nary_op_from_stmt (vn_nary_op_t vno, gimple stmt)
2145{
2146 unsigned i;
2147
2148 vno->opcode = gimple_assign_rhs_code (stmt);
9ad6bebe 2149 vno->type = gimple_expr_type (stmt);
5a7d7f9c
RG
2150 switch (vno->opcode)
2151 {
2152 case REALPART_EXPR:
2153 case IMAGPART_EXPR:
2154 case VIEW_CONVERT_EXPR:
2155 vno->length = 1;
2156 vno->op[0] = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
2157 break;
2158
2159 case CONSTRUCTOR:
2160 vno->length = CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
2161 for (i = 0; i < vno->length; ++i)
2162 vno->op[i] = CONSTRUCTOR_ELT (gimple_assign_rhs1 (stmt), i)->value;
2163 break;
2164
2165 default:
2166 vno->length = gimple_num_ops (stmt) - 1;
2167 for (i = 0; i < vno->length; ++i)
2168 vno->op[i] = gimple_op (stmt, i + 1);
2169 }
9ad6bebe
NF
2170}
2171
2172/* Compute the hashcode for VNO and look for it in the hash table;
2173 return the resulting value number if it exists in the hash table.
2174 Return NULL_TREE if it does not exist in the hash table or if the
2175 result field of the operation is NULL. VNRESULT will contain the
2176 vn_nary_op_t from the hashtable if it exists. */
2177
2178static tree
2179vn_nary_op_lookup_1 (vn_nary_op_t vno, vn_nary_op_t *vnresult)
c9145754
DB
2180{
2181 void **slot;
9ad6bebe 2182
c9145754
DB
2183 if (vnresult)
2184 *vnresult = NULL;
9ad6bebe
NF
2185
2186 vno->hashcode = vn_nary_op_compute_hash (vno);
2187 slot = htab_find_slot_with_hash (current_info->nary, vno, vno->hashcode,
c9145754
DB
2188 NO_INSERT);
2189 if (!slot && current_info == optimistic_info)
9ad6bebe 2190 slot = htab_find_slot_with_hash (valid_info->nary, vno, vno->hashcode,
c9145754
DB
2191 NO_INSERT);
2192 if (!slot)
2193 return NULL_TREE;
2194 if (vnresult)
2195 *vnresult = (vn_nary_op_t)*slot;
2196 return ((vn_nary_op_t)*slot)->result;
2197}
2198
9ad6bebe
NF
2199/* Lookup a n-ary operation by its pieces and return the resulting value
2200 number if it exists in the hash table. Return NULL_TREE if it does
2201 not exist in the hash table or if the result field of the operation
2202 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2203 if it exists. */
2204
2205tree
2206vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
5a7d7f9c 2207 tree type, tree *ops, vn_nary_op_t *vnresult)
9ad6bebe 2208{
5a7d7f9c
RG
2209 vn_nary_op_t vno1 = XALLOCAVAR (struct vn_nary_op_s,
2210 sizeof_vn_nary_op (length));
2211 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
2212 return vn_nary_op_lookup_1 (vno1, vnresult);
9ad6bebe
NF
2213}
2214
c9145754
DB
2215/* Lookup OP in the current hash table, and return the resulting value
2216 number if it exists in the hash table. Return NULL_TREE if it does
2217 not exist in the hash table or if the result field of the operation
2218 is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
2219 if it exists. */
2220
2221tree
2222vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
89fb70a3 2223{
5a7d7f9c
RG
2224 vn_nary_op_t vno1
2225 = XALLOCAVAR (struct vn_nary_op_s,
2226 sizeof_vn_nary_op (TREE_CODE_LENGTH (TREE_CODE (op))));
2227 init_vn_nary_op_from_op (vno1, op);
2228 return vn_nary_op_lookup_1 (vno1, vnresult);
89fb70a3
DB
2229}
2230
726a989a
RB
2231/* Lookup the rhs of STMT in the current hash table, and return the resulting
2232 value number if it exists in the hash table. Return NULL_TREE if
2233 it does not exist in the hash table. VNRESULT will contain the
2234 vn_nary_op_t from the hashtable if it exists. */
2235
2236tree
2237vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
2238{
5a7d7f9c
RG
2239 vn_nary_op_t vno1
2240 = XALLOCAVAR (struct vn_nary_op_s,
2241 sizeof_vn_nary_op (vn_nary_length_from_stmt (stmt)));
2242 init_vn_nary_op_from_stmt (vno1, stmt);
2243 return vn_nary_op_lookup_1 (vno1, vnresult);
9ad6bebe
NF
2244}
2245
2246/* Allocate a vn_nary_op_t with LENGTH operands on STACK. */
2247
2248static vn_nary_op_t
2249alloc_vn_nary_op_noinit (unsigned int length, struct obstack *stack)
2250{
2251 return (vn_nary_op_t) obstack_alloc (stack, sizeof_vn_nary_op (length));
2252}
2253
2254/* Allocate and initialize a vn_nary_op_t on CURRENT_INFO's
2255 obstack. */
2256
2257static vn_nary_op_t
2258alloc_vn_nary_op (unsigned int length, tree result, unsigned int value_id)
2259{
2260 vn_nary_op_t vno1 = alloc_vn_nary_op_noinit (length,
2261 &current_info->nary_obstack);
2262
2263 vno1->value_id = value_id;
2264 vno1->length = length;
2265 vno1->result = result;
2266
2267 return vno1;
2268}
2269
2270/* Insert VNO into TABLE. If COMPUTE_HASH is true, then compute
2271 VNO->HASHCODE first. */
2272
2273static vn_nary_op_t
2274vn_nary_op_insert_into (vn_nary_op_t vno, htab_t table, bool compute_hash)
2275{
2276 void **slot;
2277
2278 if (compute_hash)
2279 vno->hashcode = vn_nary_op_compute_hash (vno);
2280
2281 slot = htab_find_slot_with_hash (table, vno, vno->hashcode, INSERT);
2282 gcc_assert (!*slot);
2283
2284 *slot = vno;
2285 return vno;
726a989a
RB
2286}
2287
c9145754
DB
2288/* Insert a n-ary operation into the current hash table using it's
2289 pieces. Return the vn_nary_op_t structure we created and put in
2290 the hashtable. */
2291
2292vn_nary_op_t
2293vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
5a7d7f9c
RG
2294 tree type, tree *ops,
2295 tree result, unsigned int value_id)
c9145754 2296{
5a7d7f9c
RG
2297 vn_nary_op_t vno1 = alloc_vn_nary_op (length, result, value_id);
2298 init_vn_nary_op_from_pieces (vno1, length, code, type, ops);
9ad6bebe 2299 return vn_nary_op_insert_into (vno1, current_info->nary, true);
c9145754
DB
2300}
2301
89fb70a3 2302/* Insert OP into the current hash table with a value number of
c9145754
DB
2303 RESULT. Return the vn_nary_op_t structure we created and put in
2304 the hashtable. */
89fb70a3 2305
c9145754 2306vn_nary_op_t
49a1fb2d 2307vn_nary_op_insert (tree op, tree result)
89fb70a3 2308{
49a1fb2d 2309 unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
49a1fb2d 2310 vn_nary_op_t vno1;
49a1fb2d 2311
9ad6bebe
NF
2312 vno1 = alloc_vn_nary_op (length, result, VN_INFO (result)->value_id);
2313 init_vn_nary_op_from_op (vno1, op);
2314 return vn_nary_op_insert_into (vno1, current_info->nary, true);
89fb70a3
DB
2315}
2316
726a989a
RB
2317/* Insert the rhs of STMT into the current hash table with a value number of
2318 RESULT. */
2319
2320vn_nary_op_t
2321vn_nary_op_insert_stmt (gimple stmt, tree result)
2322{
5a7d7f9c
RG
2323 vn_nary_op_t vno1
2324 = alloc_vn_nary_op (vn_nary_length_from_stmt (stmt),
2325 result, VN_INFO (result)->value_id);
9ad6bebe
NF
2326 init_vn_nary_op_from_stmt (vno1, stmt);
2327 return vn_nary_op_insert_into (vno1, current_info->nary, true);
726a989a
RB
2328}
2329
89fb70a3
DB
2330/* Compute a hashcode for PHI operation VP1 and return it. */
2331
2332static inline hashval_t
2333vn_phi_compute_hash (vn_phi_t vp1)
2334{
9708c51d 2335 hashval_t result;
89fb70a3
DB
2336 int i;
2337 tree phi1op;
1d295886 2338 tree type;
89fb70a3
DB
2339
2340 result = vp1->block->index;
2341
1d295886
RG
2342 /* If all PHI arguments are constants we need to distinguish
2343 the PHI node via its type. */
2344 type = TREE_TYPE (VEC_index (tree, vp1->phiargs, 0));
2345 result += (INTEGRAL_TYPE_P (type)
2346 + (INTEGRAL_TYPE_P (type)
2347 ? TYPE_PRECISION (type) + TYPE_UNSIGNED (type) : 0));
2348
ac47786e 2349 FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
89fb70a3
DB
2350 {
2351 if (phi1op == VN_TOP)
2352 continue;
9708c51d 2353 result = iterative_hash_expr (phi1op, result);
89fb70a3
DB
2354 }
2355
2356 return result;
2357}
2358
2359/* Return the computed hashcode for phi operation P1. */
2360
2361static hashval_t
2362vn_phi_hash (const void *p1)
2363{
741ac903 2364 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
89fb70a3
DB
2365 return vp1->hashcode;
2366}
2367
2368/* Compare two phi entries for equality, ignoring VN_TOP arguments. */
2369
2370static int
2371vn_phi_eq (const void *p1, const void *p2)
2372{
741ac903
KG
2373 const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
2374 const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
89fb70a3 2375
85169114
PB
2376 if (vp1->hashcode != vp2->hashcode)
2377 return false;
2378
89fb70a3
DB
2379 if (vp1->block == vp2->block)
2380 {
2381 int i;
2382 tree phi1op;
2383
1d295886
RG
2384 /* If the PHI nodes do not have compatible types
2385 they are not the same. */
2386 if (!types_compatible_p (TREE_TYPE (VEC_index (tree, vp1->phiargs, 0)),
2387 TREE_TYPE (VEC_index (tree, vp2->phiargs, 0))))
2388 return false;
2389
89fb70a3
DB
2390 /* Any phi in the same block will have it's arguments in the
2391 same edge order, because of how we store phi nodes. */
ac47786e 2392 FOR_EACH_VEC_ELT (tree, vp1->phiargs, i, phi1op)
89fb70a3
DB
2393 {
2394 tree phi2op = VEC_index (tree, vp2->phiargs, i);
2395 if (phi1op == VN_TOP || phi2op == VN_TOP)
2396 continue;
2397 if (!expressions_equal_p (phi1op, phi2op))
2398 return false;
2399 }
2400 return true;
2401 }
2402 return false;
2403}
2404
2405static VEC(tree, heap) *shared_lookup_phiargs;
2406
2407/* Lookup PHI in the current hash table, and return the resulting
2408 value number if it exists in the hash table. Return NULL_TREE if
2409 it does not exist in the hash table. */
2410
de081cfd 2411static tree
726a989a 2412vn_phi_lookup (gimple phi)
89fb70a3
DB
2413{
2414 void **slot;
2415 struct vn_phi_s vp1;
726a989a 2416 unsigned i;
89fb70a3
DB
2417
2418 VEC_truncate (tree, shared_lookup_phiargs, 0);
2419
2420 /* Canonicalize the SSA_NAME's to their value number. */
726a989a 2421 for (i = 0; i < gimple_phi_num_args (phi); i++)
89fb70a3
DB
2422 {
2423 tree def = PHI_ARG_DEF (phi, i);
2424 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2425 VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
2426 }
2427 vp1.phiargs = shared_lookup_phiargs;
726a989a 2428 vp1.block = gimple_bb (phi);
89fb70a3
DB
2429 vp1.hashcode = vn_phi_compute_hash (&vp1);
2430 slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
2431 NO_INSERT);
27fa4044
RG
2432 if (!slot && current_info == optimistic_info)
2433 slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
2434 NO_INSERT);
89fb70a3
DB
2435 if (!slot)
2436 return NULL_TREE;
2437 return ((vn_phi_t)*slot)->result;
2438}
2439
2440/* Insert PHI into the current hash table with a value number of
2441 RESULT. */
2442
c9145754 2443static vn_phi_t
726a989a 2444vn_phi_insert (gimple phi, tree result)
89fb70a3
DB
2445{
2446 void **slot;
2447 vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
726a989a 2448 unsigned i;
89fb70a3
DB
2449 VEC (tree, heap) *args = NULL;
2450
2451 /* Canonicalize the SSA_NAME's to their value number. */
726a989a 2452 for (i = 0; i < gimple_phi_num_args (phi); i++)
89fb70a3
DB
2453 {
2454 tree def = PHI_ARG_DEF (phi, i);
2455 def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
2456 VEC_safe_push (tree, heap, args, def);
2457 }
c9145754 2458 vp1->value_id = VN_INFO (result)->value_id;
89fb70a3 2459 vp1->phiargs = args;
726a989a 2460 vp1->block = gimple_bb (phi);
89fb70a3
DB
2461 vp1->result = result;
2462 vp1->hashcode = vn_phi_compute_hash (vp1);
2463
2464 slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
2465 INSERT);
2466
2467 /* Because we iterate over phi operations more than once, it's
2468 possible the slot might already exist here, hence no assert.*/
2469 *slot = vp1;
c9145754 2470 return vp1;
89fb70a3
DB
2471}
2472
2473
2474/* Print set of components in strongly connected component SCC to OUT. */
2475
2476static void
2477print_scc (FILE *out, VEC (tree, heap) *scc)
2478{
2479 tree var;
2480 unsigned int i;
2481
0eb09f31 2482 fprintf (out, "SCC consists of:");
ac47786e 2483 FOR_EACH_VEC_ELT (tree, scc, i, var)
89fb70a3 2484 {
89fb70a3 2485 fprintf (out, " ");
0eb09f31 2486 print_generic_expr (out, var, 0);
89fb70a3
DB
2487 }
2488 fprintf (out, "\n");
2489}
2490
2491/* Set the value number of FROM to TO, return true if it has changed
2492 as a result. */
2493
2494static inline bool
2495set_ssa_val_to (tree from, tree to)
2496{
90bc4623 2497 tree currval = SSA_VAL (from);
89fb70a3 2498
90bc4623
RG
2499 if (from != to)
2500 {
2501 if (currval == from)
2502 {
2503 if (dump_file && (dump_flags & TDF_DETAILS))
2504 {
2505 fprintf (dump_file, "Not changing value number of ");
2506 print_generic_expr (dump_file, from, 0);
2507 fprintf (dump_file, " from VARYING to ");
2508 print_generic_expr (dump_file, to, 0);
2509 fprintf (dump_file, "\n");
2510 }
2511 return false;
2512 }
2513 else if (TREE_CODE (to) == SSA_NAME
2514 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
2515 to = from;
2516 }
fe4fefa0 2517
8d1341f7
EB
2518 /* The only thing we allow as value numbers are VN_TOP, ssa_names
2519 and invariants. So assert that here. */
2520 gcc_assert (to != NULL_TREE
2521 && (to == VN_TOP
2522 || TREE_CODE (to) == SSA_NAME
2523 || is_gimple_min_invariant (to)));
89fb70a3
DB
2524
2525 if (dump_file && (dump_flags & TDF_DETAILS))
2526 {
2527 fprintf (dump_file, "Setting value number of ");
2528 print_generic_expr (dump_file, from, 0);
2529 fprintf (dump_file, " to ");
2530 print_generic_expr (dump_file, to, 0);
89fb70a3
DB
2531 }
2532
78d3c323 2533 if (currval != to && !operand_equal_p (currval, to, OEP_PURE_SAME))
89fb70a3 2534 {
5006671f 2535 VN_INFO (from)->valnum = to;
8495c94f
RG
2536 if (dump_file && (dump_flags & TDF_DETAILS))
2537 fprintf (dump_file, " (changed)\n");
89fb70a3
DB
2538 return true;
2539 }
8495c94f
RG
2540 if (dump_file && (dump_flags & TDF_DETAILS))
2541 fprintf (dump_file, "\n");
89fb70a3
DB
2542 return false;
2543}
2544
00115921
TV
2545/* Mark as processed all the definitions in the defining stmt of USE, or
2546 the USE itself. */
2547
2548static void
2549mark_use_processed (tree use)
2550{
2551 ssa_op_iter iter;
2552 def_operand_p defp;
2553 gimple stmt = SSA_NAME_DEF_STMT (use);
2554
2555 if (SSA_NAME_IS_DEFAULT_DEF (use) || gimple_code (stmt) == GIMPLE_PHI)
2556 {
2557 VN_INFO (use)->use_processed = true;
2558 return;
2559 }
2560
2561 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2562 {
2563 tree def = DEF_FROM_PTR (defp);
2564
2565 VN_INFO (def)->use_processed = true;
2566 }
2567}
2568
89fb70a3
DB
2569/* Set all definitions in STMT to value number to themselves.
2570 Return true if a value number changed. */
2571
2572static bool
726a989a 2573defs_to_varying (gimple stmt)
89fb70a3
DB
2574{
2575 bool changed = false;
2576 ssa_op_iter iter;
2577 def_operand_p defp;
2578
2579 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
2580 {
2581 tree def = DEF_FROM_PTR (defp);
89fb70a3
DB
2582 changed |= set_ssa_val_to (def, def);
2583 }
2584 return changed;
2585}
2586
26fa9076 2587static bool expr_has_constants (tree expr);
ecb4e37b 2588static tree valueize_expr (tree expr);
3d45dd59 2589
89fb70a3
DB
2590/* Visit a copy between LHS and RHS, return true if the value number
2591 changed. */
2592
2593static bool
2594visit_copy (tree lhs, tree rhs)
2595{
89fb70a3 2596 /* Follow chains of copies to their destination. */
7220ca2b
RG
2597 while (TREE_CODE (rhs) == SSA_NAME
2598 && SSA_VAL (rhs) != rhs)
89fb70a3 2599 rhs = SSA_VAL (rhs);
070b797d 2600
89fb70a3
DB
2601 /* The copy may have a more interesting constant filled expression
2602 (we don't, since we know our RHS is just an SSA name). */
7220ca2b
RG
2603 if (TREE_CODE (rhs) == SSA_NAME)
2604 {
2605 VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
2606 VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
2607 }
89fb70a3
DB
2608
2609 return set_ssa_val_to (lhs, rhs);
2610}
2611
2262707f 2612/* Visit a nary operator RHS, value number it, and return true if the
89fb70a3
DB
2613 value number of LHS has changed as a result. */
2614
2615static bool
2262707f 2616visit_nary_op (tree lhs, gimple stmt)
89fb70a3
DB
2617{
2618 bool changed = false;
726a989a 2619 tree result = vn_nary_op_lookup_stmt (stmt, NULL);
89fb70a3
DB
2620
2621 if (result)
2262707f 2622 changed = set_ssa_val_to (lhs, result);
726a989a
RB
2623 else
2624 {
2625 changed = set_ssa_val_to (lhs, lhs);
2626 vn_nary_op_insert_stmt (stmt, lhs);
2627 }
2628
2629 return changed;
2630}
2631
2632/* Visit a call STMT storing into LHS. Return true if the value number
2633 of the LHS has changed as a result. */
2634
2635static bool
2636visit_reference_op_call (tree lhs, gimple stmt)
89fb70a3
DB
2637{
2638 bool changed = false;
726a989a 2639 struct vn_reference_s vr1;
00115921 2640 vn_reference_t vnresult = NULL;
5006671f 2641 tree vuse = gimple_vuse (stmt);
00115921 2642 tree vdef = gimple_vdef (stmt);
89fb70a3 2643
6867d9a9
TV
2644 /* Non-ssa lhs is handled in copy_reference_ops_from_call. */
2645 if (lhs && TREE_CODE (lhs) != SSA_NAME)
2646 lhs = NULL_TREE;
2647
5006671f 2648 vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
aa7069aa 2649 vr1.operands = valueize_shared_reference_ops_from_call (stmt);
b45d2719
RG
2650 vr1.type = gimple_expr_type (stmt);
2651 vr1.set = 0;
726a989a 2652 vr1.hashcode = vn_reference_compute_hash (&vr1);
00115921
TV
2653 vn_reference_lookup_1 (&vr1, &vnresult);
2654
2655 if (vnresult)
89fb70a3 2656 {
00115921
TV
2657 if (vnresult->result_vdef)
2658 changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
2659
2660 if (!vnresult->result && lhs)
2661 vnresult->result = lhs;
2662
2663 if (vnresult->result && lhs)
2664 {
2665 changed |= set_ssa_val_to (lhs, vnresult->result);
2666
2667 if (VN_INFO (vnresult->result)->has_constants)
2668 VN_INFO (lhs)->has_constants = true;
2669 }
89fb70a3
DB
2670 }
2671 else
2672 {
726a989a
RB
2673 void **slot;
2674 vn_reference_t vr2;
00115921
TV
2675 if (vdef)
2676 changed |= set_ssa_val_to (vdef, vdef);
2677 if (lhs)
2678 changed |= set_ssa_val_to (lhs, lhs);
726a989a 2679 vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
5006671f 2680 vr2->vuse = vr1.vuse;
726a989a 2681 vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
b45d2719
RG
2682 vr2->type = vr1.type;
2683 vr2->set = vr1.set;
726a989a
RB
2684 vr2->hashcode = vr1.hashcode;
2685 vr2->result = lhs;
00115921 2686 vr2->result_vdef = vdef;
726a989a
RB
2687 slot = htab_find_slot_with_hash (current_info->references,
2688 vr2, vr2->hashcode, INSERT);
2689 if (*slot)
2690 free_reference (*slot);
2691 *slot = vr2;
89fb70a3
DB
2692 }
2693
2694 return changed;
2695}
2696
2697/* Visit a load from a reference operator RHS, part of STMT, value number it,
2698 and return true if the value number of the LHS has changed as a result. */
2699
2700static bool
726a989a 2701visit_reference_op_load (tree lhs, tree op, gimple stmt)
89fb70a3
DB
2702{
2703 bool changed = false;
d0ca0bcb
RG
2704 tree last_vuse;
2705 tree result;
2706
2707 last_vuse = gimple_vuse (stmt);
2708 last_vuse_ptr = &last_vuse;
1ec87690
RG
2709 result = vn_reference_lookup (op, gimple_vuse (stmt),
2710 default_vn_walk_kind, NULL);
d0ca0bcb 2711 last_vuse_ptr = NULL;
89fb70a3 2712
737142ce
AP
2713 /* If we have a VCE, try looking up its operand as it might be stored in
2714 a different type. */
2715 if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
2716 result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
1ec87690 2717 default_vn_walk_kind, NULL);
737142ce 2718
3d45dd59
RG
2719 /* We handle type-punning through unions by value-numbering based
2720 on offset and size of the access. Be prepared to handle a
2721 type-mismatch here via creating a VIEW_CONVERT_EXPR. */
2722 if (result
2723 && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
2724 {
2725 /* We will be setting the value number of lhs to the value number
2726 of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2727 So first simplify and lookup this expression to see if it
2728 is already available. */
2729 tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
ecb4e37b
RG
2730 if ((CONVERT_EXPR_P (val)
2731 || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2732 && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
3d45dd59 2733 {
ecb4e37b
RG
2734 tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2735 if ((CONVERT_EXPR_P (tem)
2736 || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
9bacafeb
PB
2737 && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2738 TREE_TYPE (val), tem)))
3d45dd59
RG
2739 val = tem;
2740 }
2741 result = val;
2742 if (!is_gimple_min_invariant (val)
2743 && TREE_CODE (val) != SSA_NAME)
c9145754 2744 result = vn_nary_op_lookup (val, NULL);
3d45dd59
RG
2745 /* If the expression is not yet available, value-number lhs to
2746 a new SSA_NAME we create. */
70f34814 2747 if (!result)
3d45dd59 2748 {
70b5e7dc
RG
2749 result = make_temp_ssa_name (TREE_TYPE (lhs), gimple_build_nop (),
2750 "vntemp");
3d45dd59
RG
2751 /* Initialize value-number information properly. */
2752 VN_INFO_GET (result)->valnum = result;
726a989a 2753 VN_INFO (result)->value_id = get_next_value_id ();
3d45dd59 2754 VN_INFO (result)->expr = val;
26fa9076 2755 VN_INFO (result)->has_constants = expr_has_constants (val);
3d45dd59
RG
2756 VN_INFO (result)->needs_insertion = true;
2757 /* As all "inserted" statements are singleton SCCs, insert
2758 to the valid table. This is strictly needed to
2759 avoid re-generating new value SSA_NAMEs for the same
2760 expression during SCC iteration over and over (the
2761 optimistic table gets cleared after each iteration).
2762 We do not need to insert into the optimistic table, as
2763 lookups there will fall back to the valid table. */
2764 if (current_info == optimistic_info)
2765 {
2766 current_info = valid_info;
2767 vn_nary_op_insert (val, result);
2768 current_info = optimistic_info;
2769 }
2770 else
2771 vn_nary_op_insert (val, result);
2772 if (dump_file && (dump_flags & TDF_DETAILS))
2773 {
2774 fprintf (dump_file, "Inserting name ");
2775 print_generic_expr (dump_file, result, 0);
2776 fprintf (dump_file, " for expression ");
2777 print_generic_expr (dump_file, val, 0);
2778 fprintf (dump_file, "\n");
2779 }
2780 }
2781 }
2782
89fb70a3
DB
2783 if (result)
2784 {
2785 changed = set_ssa_val_to (lhs, result);
b80280f2
RG
2786 if (TREE_CODE (result) == SSA_NAME
2787 && VN_INFO (result)->has_constants)
2788 {
2789 VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2790 VN_INFO (lhs)->has_constants = true;
2791 }
89fb70a3
DB
2792 }
2793 else
2794 {
2795 changed = set_ssa_val_to (lhs, lhs);
4ec0a198 2796 vn_reference_insert (op, lhs, last_vuse, NULL_TREE);
89fb70a3
DB
2797 }
2798
2799 return changed;
2800}
2801
2802
2803/* Visit a store to a reference operator LHS, part of STMT, value number it,
2804 and return true if the value number of the LHS has changed as a result. */
2805
2806static bool
726a989a 2807visit_reference_op_store (tree lhs, tree op, gimple stmt)
89fb70a3
DB
2808{
2809 bool changed = false;
4ec0a198
TV
2810 vn_reference_t vnresult = NULL;
2811 tree result, assign;
89fb70a3 2812 bool resultsame = false;
4ec0a198
TV
2813 tree vuse = gimple_vuse (stmt);
2814 tree vdef = gimple_vdef (stmt);
89fb70a3
DB
2815
2816 /* First we want to lookup using the *vuses* from the store and see
2817 if there the last store to this location with the same address
2818 had the same value.
2819
2820 The vuses represent the memory state before the store. If the
2821 memory state, address, and value of the store is the same as the
2822 last store to this location, then this store will produce the
2823 same memory state as that store.
2824
2825 In this case the vdef versions for this store are value numbered to those
2826 vuse versions, since they represent the same memory state after
2827 this store.
2828
2829 Otherwise, the vdefs for the store are used when inserting into
2830 the table, since the store generates a new memory state. */
2831
4ec0a198 2832 result = vn_reference_lookup (lhs, vuse, VN_NOWALK, NULL);
89fb70a3
DB
2833
2834 if (result)
2835 {
2836 if (TREE_CODE (result) == SSA_NAME)
2837 result = SSA_VAL (result);
5be891a4
RG
2838 if (TREE_CODE (op) == SSA_NAME)
2839 op = SSA_VAL (op);
89fb70a3
DB
2840 resultsame = expressions_equal_p (result, op);
2841 }
2842
2843 if (!result || !resultsame)
2844 {
4ec0a198
TV
2845 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2846 vn_reference_lookup (assign, vuse, VN_NOWALK, &vnresult);
2847 if (vnresult)
2848 {
2849 VN_INFO (vdef)->use_processed = true;
2850 return set_ssa_val_to (vdef, vnresult->result_vdef);
2851 }
2852 }
89fb70a3 2853
4ec0a198
TV
2854 if (!result || !resultsame)
2855 {
89fb70a3
DB
2856 if (dump_file && (dump_flags & TDF_DETAILS))
2857 {
2858 fprintf (dump_file, "No store match\n");
2859 fprintf (dump_file, "Value numbering store ");
2860 print_generic_expr (dump_file, lhs, 0);
2861 fprintf (dump_file, " to ");
2862 print_generic_expr (dump_file, op, 0);
2863 fprintf (dump_file, "\n");
2864 }
2865 /* Have to set value numbers before insert, since insert is
2866 going to valueize the references in-place. */
4ec0a198 2867 if (vdef)
89fb70a3 2868 {
89fb70a3
DB
2869 changed |= set_ssa_val_to (vdef, vdef);
2870 }
2871
9a327766
RG
2872 /* Do not insert structure copies into the tables. */
2873 if (is_gimple_min_invariant (op)
2874 || is_gimple_reg (op))
4ec0a198
TV
2875 vn_reference_insert (lhs, op, vdef, NULL);
2876
2877 assign = build2 (MODIFY_EXPR, TREE_TYPE (lhs), lhs, op);
2878 vn_reference_insert (assign, lhs, vuse, vdef);
89fb70a3
DB
2879 }
2880 else
2881 {
5006671f
RG
2882 /* We had a match, so value number the vdef to have the value
2883 number of the vuse it came from. */
89fb70a3
DB
2884
2885 if (dump_file && (dump_flags & TDF_DETAILS))
2886 fprintf (dump_file, "Store matched earlier value,"
2887 "value numbering store vdefs to matching vuses.\n");
2888
4ec0a198 2889 changed |= set_ssa_val_to (vdef, SSA_VAL (vuse));
89fb70a3
DB
2890 }
2891
2892 return changed;
2893}
2894
2895/* Visit and value number PHI, return true if the value number
2896 changed. */
2897
2898static bool
726a989a 2899visit_phi (gimple phi)
89fb70a3
DB
2900{
2901 bool changed = false;
2902 tree result;
2903 tree sameval = VN_TOP;
2904 bool allsame = true;
726a989a 2905 unsigned i;
89fb70a3 2906
62b0d9ec
DJ
2907 /* TODO: We could check for this in init_sccvn, and replace this
2908 with a gcc_assert. */
2909 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
2910 return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2911
89fb70a3
DB
2912 /* See if all non-TOP arguments have the same value. TOP is
2913 equivalent to everything, so we can ignore it. */
726a989a 2914 for (i = 0; i < gimple_phi_num_args (phi); i++)
89fb70a3
DB
2915 {
2916 tree def = PHI_ARG_DEF (phi, i);
2917
2918 if (TREE_CODE (def) == SSA_NAME)
2919 def = SSA_VAL (def);
2920 if (def == VN_TOP)
2921 continue;
2922 if (sameval == VN_TOP)
2923 {
2924 sameval = def;
2925 }
2926 else
2927 {
2928 if (!expressions_equal_p (def, sameval))
2929 {
2930 allsame = false;
2931 break;
2932 }
2933 }
2934 }
2935
2936 /* If all value numbered to the same value, the phi node has that
2937 value. */
2938 if (allsame)
2939 {
2940 if (is_gimple_min_invariant (sameval))
2941 {
2942 VN_INFO (PHI_RESULT (phi))->has_constants = true;
2943 VN_INFO (PHI_RESULT (phi))->expr = sameval;
2944 }
2945 else
2946 {
2947 VN_INFO (PHI_RESULT (phi))->has_constants = false;
2948 VN_INFO (PHI_RESULT (phi))->expr = sameval;
2949 }
070b797d 2950
89fb70a3
DB
2951 if (TREE_CODE (sameval) == SSA_NAME)
2952 return visit_copy (PHI_RESULT (phi), sameval);
070b797d 2953
89fb70a3
DB
2954 return set_ssa_val_to (PHI_RESULT (phi), sameval);
2955 }
2956
2957 /* Otherwise, see if it is equivalent to a phi node in this block. */
2958 result = vn_phi_lookup (phi);
2959 if (result)
2960 {
2961 if (TREE_CODE (result) == SSA_NAME)
2962 changed = visit_copy (PHI_RESULT (phi), result);
2963 else
2964 changed = set_ssa_val_to (PHI_RESULT (phi), result);
2965 }
2966 else
2967 {
2968 vn_phi_insert (phi, PHI_RESULT (phi));
2969 VN_INFO (PHI_RESULT (phi))->has_constants = false;
2970 VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
2971 changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2972 }
2973
2974 return changed;
2975}
2976
2977/* Return true if EXPR contains constants. */
2978
2979static bool
2980expr_has_constants (tree expr)
2981{
2982 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2983 {
2984 case tcc_unary:
2985 return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
2986
2987 case tcc_binary:
2988 return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
2989 || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
2990 /* Constants inside reference ops are rarely interesting, but
2991 it can take a lot of looking to find them. */
2992 case tcc_reference:
e9bd9cf3 2993 case tcc_declaration:
89fb70a3
DB
2994 return false;
2995 default:
2996 return is_gimple_min_invariant (expr);
2997 }
2998 return false;
2999}
3000
726a989a
RB
3001/* Return true if STMT contains constants. */
3002
3003static bool
3004stmt_has_constants (gimple stmt)
3005{
3006 if (gimple_code (stmt) != GIMPLE_ASSIGN)
3007 return false;
3008
3009 switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
3010 {
3011 case GIMPLE_UNARY_RHS:
3012 return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
3013
3014 case GIMPLE_BINARY_RHS:
3015 return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
3016 || is_gimple_min_invariant (gimple_assign_rhs2 (stmt)));
0354c0c7
BS
3017 case GIMPLE_TERNARY_RHS:
3018 return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
3019 || is_gimple_min_invariant (gimple_assign_rhs2 (stmt))
3020 || is_gimple_min_invariant (gimple_assign_rhs3 (stmt)));
726a989a
RB
3021 case GIMPLE_SINGLE_RHS:
3022 /* Constants inside reference ops are rarely interesting, but
3023 it can take a lot of looking to find them. */
3024 return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
3025 default:
3026 gcc_unreachable ();
3027 }
3028 return false;
3029}
3030
89fb70a3
DB
3031/* Replace SSA_NAMES in expr with their value numbers, and return the
3032 result.
3033 This is performed in place. */
3034
3035static tree
3036valueize_expr (tree expr)
3037{
3038 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
3039 {
89fb70a3 3040 case tcc_binary:
1a60c352
RG
3041 TREE_OPERAND (expr, 1) = vn_valueize (TREE_OPERAND (expr, 1));
3042 /* Fallthru. */
3043 case tcc_unary:
3044 TREE_OPERAND (expr, 0) = vn_valueize (TREE_OPERAND (expr, 0));
89fb70a3 3045 break;
1a60c352 3046 default:;
89fb70a3
DB
3047 }
3048 return expr;
3049}
3050
3051/* Simplify the binary expression RHS, and return the result if
3052 simplified. */
3053
3054static tree
726a989a 3055simplify_binary_expression (gimple stmt)
89fb70a3
DB
3056{
3057 tree result = NULL_TREE;
726a989a
RB
3058 tree op0 = gimple_assign_rhs1 (stmt);
3059 tree op1 = gimple_assign_rhs2 (stmt);
1a60c352 3060 enum tree_code code = gimple_assign_rhs_code (stmt);
89fb70a3
DB
3061
3062 /* This will not catch every single case we could combine, but will
3063 catch those with constants. The goal here is to simultaneously
3064 combine constants between expressions, but avoid infinite
3065 expansion of expressions during simplification. */
3066 if (TREE_CODE (op0) == SSA_NAME)
3067 {
726a989a 3068 if (VN_INFO (op0)->has_constants
1a60c352
RG
3069 || TREE_CODE_CLASS (code) == tcc_comparison
3070 || code == COMPLEX_EXPR)
726a989a 3071 op0 = valueize_expr (vn_get_expr_for (op0));
1a60c352
RG
3072 else
3073 op0 = vn_valueize (op0);
89fb70a3
DB
3074 }
3075
3076 if (TREE_CODE (op1) == SSA_NAME)
3077 {
1a60c352
RG
3078 if (VN_INFO (op1)->has_constants
3079 || code == COMPLEX_EXPR)
726a989a 3080 op1 = valueize_expr (vn_get_expr_for (op1));
1a60c352
RG
3081 else
3082 op1 = vn_valueize (op1);
89fb70a3 3083 }
c2979eaf 3084
cfef45c8
RG
3085 /* Pointer plus constant can be represented as invariant address.
3086 Do so to allow further propatation, see also tree forwprop. */
1a60c352 3087 if (code == POINTER_PLUS_EXPR
cfef45c8
RG
3088 && host_integerp (op1, 1)
3089 && TREE_CODE (op0) == ADDR_EXPR
3090 && is_gimple_min_invariant (op0))
3091 return build_invariant_address (TREE_TYPE (op0),
3092 TREE_OPERAND (op0, 0),
3093 TREE_INT_CST_LOW (op1));
3094
eb2c3940 3095 /* Avoid folding if nothing changed. */
726a989a
RB
3096 if (op0 == gimple_assign_rhs1 (stmt)
3097 && op1 == gimple_assign_rhs2 (stmt))
eb2c3940
RG
3098 return NULL_TREE;
3099
e233ac97
ILT
3100 fold_defer_overflow_warnings ();
3101
1a60c352 3102 result = fold_binary (code, gimple_expr_type (stmt), op0, op1);
8495c94f
RG
3103 if (result)
3104 STRIP_USELESS_TYPE_CONVERSION (result);
89fb70a3 3105
726a989a 3106 fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
e233ac97
ILT
3107 stmt, 0);
3108
6ed3da00 3109 /* Make sure result is not a complex expression consisting
89fb70a3
DB
3110 of operators of operators (IE (a + b) + (a + c))
3111 Otherwise, we will end up with unbounded expressions if
3112 fold does anything at all. */
726a989a 3113 if (result && valid_gimple_rhs_p (result))
c2979eaf
EB
3114 return result;
3115
89fb70a3
DB
3116 return NULL_TREE;
3117}
3118
eb2c3940
RG
3119/* Simplify the unary expression RHS, and return the result if
3120 simplified. */
3121
3122static tree
726a989a 3123simplify_unary_expression (gimple stmt)
eb2c3940
RG
3124{
3125 tree result = NULL_TREE;
726a989a 3126 tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
1a60c352 3127 enum tree_code code = gimple_assign_rhs_code (stmt);
726a989a
RB
3128
3129 /* We handle some tcc_reference codes here that are all
3130 GIMPLE_ASSIGN_SINGLE codes. */
1a60c352
RG
3131 if (code == REALPART_EXPR
3132 || code == IMAGPART_EXPR
18474649
RG
3133 || code == VIEW_CONVERT_EXPR
3134 || code == BIT_FIELD_REF)
726a989a 3135 op0 = TREE_OPERAND (op0, 0);
eb2c3940
RG
3136
3137 if (TREE_CODE (op0) != SSA_NAME)
3138 return NULL_TREE;
3139
726a989a 3140 orig_op0 = op0;
eb2c3940 3141 if (VN_INFO (op0)->has_constants)
726a989a 3142 op0 = valueize_expr (vn_get_expr_for (op0));
1a60c352
RG
3143 else if (CONVERT_EXPR_CODE_P (code)
3144 || code == REALPART_EXPR
3145 || code == IMAGPART_EXPR
18474649
RG
3146 || code == VIEW_CONVERT_EXPR
3147 || code == BIT_FIELD_REF)
eb2c3940
RG
3148 {
3149 /* We want to do tree-combining on conversion-like expressions.
3150 Make sure we feed only SSA_NAMEs or constants to fold though. */
726a989a 3151 tree tem = valueize_expr (vn_get_expr_for (op0));
eb2c3940
RG
3152 if (UNARY_CLASS_P (tem)
3153 || BINARY_CLASS_P (tem)
9a327766 3154 || TREE_CODE (tem) == VIEW_CONVERT_EXPR
eb2c3940 3155 || TREE_CODE (tem) == SSA_NAME
18474649 3156 || TREE_CODE (tem) == CONSTRUCTOR
eb2c3940
RG
3157 || is_gimple_min_invariant (tem))
3158 op0 = tem;
3159 }
3160
3161 /* Avoid folding if nothing changed, but remember the expression. */
726a989a
RB
3162 if (op0 == orig_op0)
3163 return NULL_TREE;
eb2c3940 3164
18474649
RG
3165 if (code == BIT_FIELD_REF)
3166 {
3167 tree rhs = gimple_assign_rhs1 (stmt);
3168 result = fold_ternary (BIT_FIELD_REF, TREE_TYPE (rhs),
3169 op0, TREE_OPERAND (rhs, 1), TREE_OPERAND (rhs, 2));
3170 }
3171 else
3172 result = fold_unary_ignore_overflow (code, gimple_expr_type (stmt), op0);
eb2c3940
RG
3173 if (result)
3174 {
3175 STRIP_USELESS_TYPE_CONVERSION (result);
726a989a 3176 if (valid_gimple_rhs_p (result))
eb2c3940
RG
3177 return result;
3178 }
3179
726a989a 3180 return NULL_TREE;
eb2c3940
RG
3181}
3182
89fb70a3
DB
3183/* Try to simplify RHS using equivalences and constant folding. */
3184
3185static tree
726a989a 3186try_to_simplify (gimple stmt)
89fb70a3 3187{
d3878abf 3188 enum tree_code code = gimple_assign_rhs_code (stmt);
ed97ddc6
RG
3189 tree tem;
3190
5be891a4
RG
3191 /* For stores we can end up simplifying a SSA_NAME rhs. Just return
3192 in this case, there is no point in doing extra work. */
d3878abf 3193 if (code == SSA_NAME)
726a989a 3194 return NULL_TREE;
ed97ddc6 3195
cfef45c8 3196 /* First try constant folding based on our current lattice. */
d3878abf
RG
3197 tem = gimple_fold_stmt_to_constant_1 (stmt, vn_valueize);
3198 if (tem
3199 && (TREE_CODE (tem) == SSA_NAME
3200 || is_gimple_min_invariant (tem)))
cfef45c8
RG
3201 return tem;
3202
3203 /* If that didn't work try combining multiple statements. */
d3878abf 3204 switch (TREE_CODE_CLASS (code))
89fb70a3 3205 {
ed97ddc6 3206 case tcc_reference:
d3878abf
RG
3207 /* Fallthrough for some unary codes that can operate on registers. */
3208 if (!(code == REALPART_EXPR
3209 || code == IMAGPART_EXPR
18474649
RG
3210 || code == VIEW_CONVERT_EXPR
3211 || code == BIT_FIELD_REF))
ed97ddc6
RG
3212 break;
3213 /* We could do a little more with unary ops, if they expand
3214 into binary ops, but it's debatable whether it is worth it. */
3215 case tcc_unary:
726a989a 3216 return simplify_unary_expression (stmt);
cfef45c8 3217
ed97ddc6
RG
3218 case tcc_comparison:
3219 case tcc_binary:
726a989a 3220 return simplify_binary_expression (stmt);
cfef45c8 3221
ed97ddc6
RG
3222 default:
3223 break;
89fb70a3 3224 }
ed97ddc6 3225
726a989a 3226 return NULL_TREE;
89fb70a3
DB
3227}
3228
3229/* Visit and value number USE, return true if the value number
3230 changed. */
3231
3232static bool
3233visit_use (tree use)
3234{
3235 bool changed = false;
726a989a 3236 gimple stmt = SSA_NAME_DEF_STMT (use);
89fb70a3 3237
00115921 3238 mark_use_processed (use);
89fb70a3
DB
3239
3240 gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
3d45dd59 3241 if (dump_file && (dump_flags & TDF_DETAILS)
726a989a 3242 && !SSA_NAME_IS_DEFAULT_DEF (use))
89fb70a3
DB
3243 {
3244 fprintf (dump_file, "Value numbering ");
3245 print_generic_expr (dump_file, use, 0);
3246 fprintf (dump_file, " stmt = ");
726a989a 3247 print_gimple_stmt (dump_file, stmt, 0, 0);
89fb70a3
DB
3248 }
3249
89fb70a3 3250 /* Handle uninitialized uses. */
726a989a
RB
3251 if (SSA_NAME_IS_DEFAULT_DEF (use))
3252 changed = set_ssa_val_to (use, use);
89fb70a3
DB
3253 else
3254 {
726a989a
RB
3255 if (gimple_code (stmt) == GIMPLE_PHI)
3256 changed = visit_phi (stmt);
00115921 3257 else if (gimple_has_volatile_ops (stmt))
726a989a
RB
3258 changed = defs_to_varying (stmt);
3259 else if (is_gimple_assign (stmt))
89fb70a3 3260 {
32dba5ef 3261 enum tree_code code = gimple_assign_rhs_code (stmt);
726a989a 3262 tree lhs = gimple_assign_lhs (stmt);
32dba5ef 3263 tree rhs1 = gimple_assign_rhs1 (stmt);
89fb70a3
DB
3264 tree simplified;
3265
8b0a5125
DB
3266 /* Shortcut for copies. Simplifying copies is pointless,
3267 since we copy the expression and value they represent. */
32dba5ef 3268 if (code == SSA_NAME
726a989a 3269 && TREE_CODE (lhs) == SSA_NAME)
8b0a5125 3270 {
32dba5ef 3271 changed = visit_copy (lhs, rhs1);
8b0a5125
DB
3272 goto done;
3273 }
726a989a
RB
3274 simplified = try_to_simplify (stmt);
3275 if (simplified)
89fb70a3
DB
3276 {
3277 if (dump_file && (dump_flags & TDF_DETAILS))
3278 {
3279 fprintf (dump_file, "RHS ");
726a989a 3280 print_gimple_expr (dump_file, stmt, 0, 0);
89fb70a3
DB
3281 fprintf (dump_file, " simplified to ");
3282 print_generic_expr (dump_file, simplified, 0);
3283 if (TREE_CODE (lhs) == SSA_NAME)
3284 fprintf (dump_file, " has constants %d\n",
896c8b96 3285 expr_has_constants (simplified));
89fb70a3
DB
3286 else
3287 fprintf (dump_file, "\n");
89fb70a3
DB
3288 }
3289 }
3290 /* Setting value numbers to constants will occasionally
3291 screw up phi congruence because constants are not
3292 uniquely associated with a single ssa name that can be
3293 looked up. */
726a989a
RB
3294 if (simplified
3295 && is_gimple_min_invariant (simplified)
3296 && TREE_CODE (lhs) == SSA_NAME)
89fb70a3
DB
3297 {
3298 VN_INFO (lhs)->expr = simplified;
3299 VN_INFO (lhs)->has_constants = true;
3300 changed = set_ssa_val_to (lhs, simplified);
3301 goto done;
3302 }
726a989a
RB
3303 else if (simplified
3304 && TREE_CODE (simplified) == SSA_NAME
89fb70a3
DB
3305 && TREE_CODE (lhs) == SSA_NAME)
3306 {
3307 changed = visit_copy (lhs, simplified);
3308 goto done;
3309 }
3310 else if (simplified)
3311 {
3312 if (TREE_CODE (lhs) == SSA_NAME)
3313 {
3314 VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
3315 /* We have to unshare the expression or else
3316 valuizing may change the IL stream. */
3317 VN_INFO (lhs)->expr = unshare_expr (simplified);
3318 }
89fb70a3 3319 }
726a989a
RB
3320 else if (stmt_has_constants (stmt)
3321 && TREE_CODE (lhs) == SSA_NAME)
3322 VN_INFO (lhs)->has_constants = true;
89fb70a3
DB
3323 else if (TREE_CODE (lhs) == SSA_NAME)
3324 {
3325 /* We reset expr and constantness here because we may
3326 have been value numbering optimistically, and
3327 iterating. They may become non-constant in this case,
3328 even if they were optimistically constant. */
070b797d 3329
89fb70a3 3330 VN_INFO (lhs)->has_constants = false;
726a989a 3331 VN_INFO (lhs)->expr = NULL_TREE;
89fb70a3
DB
3332 }
3333
b505e785
RG
3334 if ((TREE_CODE (lhs) == SSA_NAME
3335 /* We can substitute SSA_NAMEs that are live over
3336 abnormal edges with their constant value. */
3337 && !(gimple_assign_copy_p (stmt)
32dba5ef 3338 && is_gimple_min_invariant (rhs1))
b505e785
RG
3339 && !(simplified
3340 && is_gimple_min_invariant (simplified))
3341 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3342 /* Stores or copies from SSA_NAMEs that are live over
3343 abnormal edges are a problem. */
32dba5ef
RG
3344 || (code == SSA_NAME
3345 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
89fb70a3 3346 changed = defs_to_varying (stmt);
32dba5ef
RG
3347 else if (REFERENCE_CLASS_P (lhs)
3348 || DECL_P (lhs))
3349 changed = visit_reference_op_store (lhs, rhs1, stmt);
89fb70a3
DB
3350 else if (TREE_CODE (lhs) == SSA_NAME)
3351 {
726a989a 3352 if ((gimple_assign_copy_p (stmt)
32dba5ef 3353 && is_gimple_min_invariant (rhs1))
726a989a
RB
3354 || (simplified
3355 && is_gimple_min_invariant (simplified)))
89fb70a3
DB
3356 {
3357 VN_INFO (lhs)->has_constants = true;
726a989a
RB
3358 if (simplified)
3359 changed = set_ssa_val_to (lhs, simplified);
3360 else
32dba5ef 3361 changed = set_ssa_val_to (lhs, rhs1);
89fb70a3 3362 }
89fb70a3
DB
3363 else
3364 {
32dba5ef 3365 switch (get_gimple_rhs_class (code))
89fb70a3 3366 {
726a989a 3367 case GIMPLE_UNARY_RHS:
726a989a 3368 case GIMPLE_BINARY_RHS:
2262707f
RG
3369 case GIMPLE_TERNARY_RHS:
3370 changed = visit_nary_op (lhs, stmt);
89fb70a3 3371 break;
726a989a 3372 case GIMPLE_SINGLE_RHS:
32dba5ef 3373 switch (TREE_CODE_CLASS (code))
89fb70a3 3374 {
726a989a 3375 case tcc_reference:
40f64141 3376 /* VOP-less references can go through unary case. */
32dba5ef
RG
3377 if ((code == REALPART_EXPR
3378 || code == IMAGPART_EXPR
18474649
RG
3379 || code == VIEW_CONVERT_EXPR
3380 || code == BIT_FIELD_REF)
32dba5ef 3381 && TREE_CODE (TREE_OPERAND (rhs1, 0)) == SSA_NAME)
40f64141 3382 {
2262707f 3383 changed = visit_nary_op (lhs, stmt);
40f64141
RG
3384 break;
3385 }
3386 /* Fallthrough. */
3387 case tcc_declaration:
32dba5ef 3388 changed = visit_reference_op_load (lhs, rhs1, stmt);
726a989a 3389 break;
5a7d7f9c 3390 default:
32dba5ef 3391 if (code == ADDR_EXPR)
726a989a 3392 {
2262707f 3393 changed = visit_nary_op (lhs, stmt);
726a989a
RB
3394 break;
3395 }
5a7d7f9c
RG
3396 else if (code == CONSTRUCTOR)
3397 {
3398 changed = visit_nary_op (lhs, stmt);
3399 break;
3400 }
726a989a 3401 changed = defs_to_varying (stmt);
89fb70a3 3402 }
726a989a 3403 break;
89fb70a3
DB
3404 default:
3405 changed = defs_to_varying (stmt);
3406 break;
3407 }
3408 }
3409 }
3410 else
3411 changed = defs_to_varying (stmt);
3412 }
726a989a
RB
3413 else if (is_gimple_call (stmt))
3414 {
3415 tree lhs = gimple_call_lhs (stmt);
3416
3417 /* ??? We could try to simplify calls. */
3418
00115921 3419 if (lhs && TREE_CODE (lhs) == SSA_NAME)
726a989a 3420 {
00115921
TV
3421 if (stmt_has_constants (stmt))
3422 VN_INFO (lhs)->has_constants = true;
3423 else
3424 {
3425 /* We reset expr and constantness here because we may
3426 have been value numbering optimistically, and
3427 iterating. They may become non-constant in this case,
3428 even if they were optimistically constant. */
3429 VN_INFO (lhs)->has_constants = false;
3430 VN_INFO (lhs)->expr = NULL_TREE;
3431 }
3432
3433 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3434 {
3435 changed = defs_to_varying (stmt);
3436 goto done;
3437 }
726a989a
RB
3438 }
3439
00115921 3440 if (!gimple_call_internal_p (stmt)
6867d9a9
TV
3441 && (/* Calls to the same function with the same vuse
3442 and the same operands do not necessarily return the same
3443 value, unless they're pure or const. */
3444 gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST)
3445 /* If calls have a vdef, subsequent calls won't have
3446 the same incoming vuse. So, if 2 calls with vdef have the
3447 same vuse, we know they're not subsequent.
3448 We can value number 2 calls to the same function with the
3449 same vuse and the same operands which are not subsequent
3450 the same, because there is no code in the program that can
3451 compare the 2 values. */
3452 || gimple_vdef (stmt)))
3453 changed = visit_reference_op_call (lhs, stmt);
726a989a
RB
3454 else
3455 changed = defs_to_varying (stmt);
3456 }
00115921
TV
3457 else
3458 changed = defs_to_varying (stmt);
89fb70a3
DB
3459 }
3460 done:
3461 return changed;
3462}
3463
3464/* Compare two operands by reverse postorder index */
3465
3466static int
3467compare_ops (const void *pa, const void *pb)
3468{
3469 const tree opa = *((const tree *)pa);
3470 const tree opb = *((const tree *)pb);
726a989a
RB
3471 gimple opstmta = SSA_NAME_DEF_STMT (opa);
3472 gimple opstmtb = SSA_NAME_DEF_STMT (opb);
89fb70a3
DB
3473 basic_block bba;
3474 basic_block bbb;
3475
726a989a 3476 if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
3d8fa148 3477 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
726a989a 3478 else if (gimple_nop_p (opstmta))
89fb70a3 3479 return -1;
726a989a 3480 else if (gimple_nop_p (opstmtb))
89fb70a3
DB
3481 return 1;
3482
726a989a
RB
3483 bba = gimple_bb (opstmta);
3484 bbb = gimple_bb (opstmtb);
89fb70a3
DB
3485
3486 if (!bba && !bbb)
3d8fa148 3487 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
89fb70a3
DB
3488 else if (!bba)
3489 return -1;
3490 else if (!bbb)
3491 return 1;
3492
3493 if (bba == bbb)
3494 {
726a989a
RB
3495 if (gimple_code (opstmta) == GIMPLE_PHI
3496 && gimple_code (opstmtb) == GIMPLE_PHI)
3d8fa148 3497 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
726a989a 3498 else if (gimple_code (opstmta) == GIMPLE_PHI)
89fb70a3 3499 return -1;
726a989a 3500 else if (gimple_code (opstmtb) == GIMPLE_PHI)
89fb70a3 3501 return 1;
3d8fa148
DK
3502 else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
3503 return gimple_uid (opstmta) - gimple_uid (opstmtb);
3504 else
3505 return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
89fb70a3
DB
3506 }
3507 return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
3508}
3509
3510/* Sort an array containing members of a strongly connected component
3511 SCC so that the members are ordered by RPO number.
3512 This means that when the sort is complete, iterating through the
3513 array will give you the members in RPO order. */
3514
3515static void
3516sort_scc (VEC (tree, heap) *scc)
3517{
5095da95 3518 VEC_qsort (tree, scc, compare_ops);
89fb70a3
DB
3519}
3520
880ad25f 3521/* Insert the no longer used nary ONARY to the hash INFO. */
c82e0b3b 3522
880ad25f
RG
3523static void
3524copy_nary (vn_nary_op_t onary, vn_tables_t info)
c82e0b3b 3525{
9ad6bebe
NF
3526 size_t size = sizeof_vn_nary_op (onary->length);
3527 vn_nary_op_t nary = alloc_vn_nary_op_noinit (onary->length,
3528 &info->nary_obstack);
c82e0b3b 3529 memcpy (nary, onary, size);
9ad6bebe 3530 vn_nary_op_insert_into (nary, info->nary, false);
c82e0b3b
RG
3531}
3532
880ad25f 3533/* Insert the no longer used phi OPHI to the hash INFO. */
c82e0b3b 3534
880ad25f
RG
3535static void
3536copy_phi (vn_phi_t ophi, vn_tables_t info)
c82e0b3b 3537{
880ad25f 3538 vn_phi_t phi = (vn_phi_t) pool_alloc (info->phis_pool);
c82e0b3b
RG
3539 void **slot;
3540 memcpy (phi, ophi, sizeof (*phi));
3541 ophi->phiargs = NULL;
880ad25f
RG
3542 slot = htab_find_slot_with_hash (info->phis, phi, phi->hashcode, INSERT);
3543 gcc_assert (!*slot);
c82e0b3b 3544 *slot = phi;
c82e0b3b
RG
3545}
3546
880ad25f 3547/* Insert the no longer used reference OREF to the hash INFO. */
c82e0b3b 3548
880ad25f
RG
3549static void
3550copy_reference (vn_reference_t oref, vn_tables_t info)
c82e0b3b 3551{
c82e0b3b
RG
3552 vn_reference_t ref;
3553 void **slot;
880ad25f 3554 ref = (vn_reference_t) pool_alloc (info->references_pool);
c82e0b3b
RG
3555 memcpy (ref, oref, sizeof (*ref));
3556 oref->operands = NULL;
880ad25f 3557 slot = htab_find_slot_with_hash (info->references, ref, ref->hashcode,
c82e0b3b
RG
3558 INSERT);
3559 if (*slot)
3560 free_reference (*slot);
3561 *slot = ref;
c82e0b3b
RG
3562}
3563
89fb70a3
DB
3564/* Process a strongly connected component in the SSA graph. */
3565
3566static void
3567process_scc (VEC (tree, heap) *scc)
3568{
880ad25f
RG
3569 tree var;
3570 unsigned int i;
3571 unsigned int iterations = 0;
3572 bool changed = true;
3573 htab_iterator hi;
3574 vn_nary_op_t nary;
3575 vn_phi_t phi;
3576 vn_reference_t ref;
89fb70a3 3577
880ad25f 3578 /* If the SCC has a single member, just visit it. */
89fb70a3
DB
3579 if (VEC_length (tree, scc) == 1)
3580 {
3581 tree use = VEC_index (tree, scc, 0);
72a07d9b
RB
3582 if (VN_INFO (use)->use_processed)
3583 return;
3584 /* We need to make sure it doesn't form a cycle itself, which can
3585 happen for self-referential PHI nodes. In that case we would
3586 end up inserting an expression with VN_TOP operands into the
3587 valid table which makes us derive bogus equivalences later.
3588 The cheapest way to check this is to assume it for all PHI nodes. */
3589 if (gimple_code (SSA_NAME_DEF_STMT (use)) == GIMPLE_PHI)
3590 /* Fallthru to iteration. */ ;
3591 else
3592 {
3593 visit_use (use);
3594 return;
3595 }
89fb70a3 3596 }
880ad25f
RG
3597
3598 /* Iterate over the SCC with the optimistic table until it stops
3599 changing. */
3600 current_info = optimistic_info;
3601 while (changed)
89fb70a3 3602 {
880ad25f
RG
3603 changed = false;
3604 iterations++;
90bc4623
RG
3605 if (dump_file && (dump_flags & TDF_DETAILS))
3606 fprintf (dump_file, "Starting iteration %d\n", iterations);
880ad25f
RG
3607 /* As we are value-numbering optimistically we have to
3608 clear the expression tables and the simplified expressions
3609 in each iteration until we converge. */
3610 htab_empty (optimistic_info->nary);
3611 htab_empty (optimistic_info->phis);
3612 htab_empty (optimistic_info->references);
3613 obstack_free (&optimistic_info->nary_obstack, NULL);
3614 gcc_obstack_init (&optimistic_info->nary_obstack);
3615 empty_alloc_pool (optimistic_info->phis_pool);
3616 empty_alloc_pool (optimistic_info->references_pool);
ac47786e 3617 FOR_EACH_VEC_ELT (tree, scc, i, var)
880ad25f 3618 VN_INFO (var)->expr = NULL_TREE;
ac47786e 3619 FOR_EACH_VEC_ELT (tree, scc, i, var)
880ad25f
RG
3620 changed |= visit_use (var);
3621 }
89fb70a3 3622
880ad25f 3623 statistics_histogram_event (cfun, "SCC iterations", iterations);
89fb70a3 3624
880ad25f
RG
3625 /* Finally, copy the contents of the no longer used optimistic
3626 table to the valid table. */
3627 FOR_EACH_HTAB_ELEMENT (optimistic_info->nary, nary, vn_nary_op_t, hi)
3628 copy_nary (nary, valid_info);
3629 FOR_EACH_HTAB_ELEMENT (optimistic_info->phis, phi, vn_phi_t, hi)
3630 copy_phi (phi, valid_info);
3631 FOR_EACH_HTAB_ELEMENT (optimistic_info->references, ref, vn_reference_t, hi)
3632 copy_reference (ref, valid_info);
3633
3634 current_info = valid_info;
89fb70a3
DB
3635}
3636
6be34936
RG
3637DEF_VEC_O(ssa_op_iter);
3638DEF_VEC_ALLOC_O(ssa_op_iter,heap);
3639
3640/* Pop the components of the found SCC for NAME off the SCC stack
3641 and process them. Returns true if all went well, false if
3642 we run into resource limits. */
3643
3644static bool
3645extract_and_process_scc_for_name (tree name)
3646{
3647 VEC (tree, heap) *scc = NULL;
3648 tree x;
3649
3650 /* Found an SCC, pop the components off the SCC stack and
3651 process them. */
3652 do
3653 {
3654 x = VEC_pop (tree, sccstack);
3655
3656 VN_INFO (x)->on_sccstack = false;
3657 VEC_safe_push (tree, heap, scc, x);
3658 } while (x != name);
3659
3660 /* Bail out of SCCVN in case a SCC turns out to be incredibly large. */
3661 if (VEC_length (tree, scc)
3662 > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
3663 {
3664 if (dump_file)
3665 fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
3666 "SCC size %u exceeding %u\n", VEC_length (tree, scc),
3667 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
3668 return false;
3669 }
3670
3671 if (VEC_length (tree, scc) > 1)
3672 sort_scc (scc);
3673
3674 if (dump_file && (dump_flags & TDF_DETAILS))
3675 print_scc (dump_file, scc);
3676
3677 process_scc (scc);
3678
3679 VEC_free (tree, heap, scc);
3680
3681 return true;
3682}
3683
89fb70a3
DB
3684/* Depth first search on NAME to discover and process SCC's in the SSA
3685 graph.
3686 Execution of this algorithm relies on the fact that the SCC's are
863d2a57
RG
3687 popped off the stack in topological order.
3688 Returns true if successful, false if we stopped processing SCC's due
fa10beec 3689 to resource constraints. */
89fb70a3 3690
863d2a57 3691static bool
89fb70a3
DB
3692DFS (tree name)
3693{
6be34936
RG
3694 VEC(ssa_op_iter, heap) *itervec = NULL;
3695 VEC(tree, heap) *namevec = NULL;
3696 use_operand_p usep = NULL;
726a989a
RB
3697 gimple defstmt;
3698 tree use;
89fb70a3 3699 ssa_op_iter iter;
89fb70a3 3700
6be34936 3701start_over:
89fb70a3
DB
3702 /* SCC info */
3703 VN_INFO (name)->dfsnum = next_dfs_num++;
3704 VN_INFO (name)->visited = true;
3705 VN_INFO (name)->low = VN_INFO (name)->dfsnum;
3706
3707 VEC_safe_push (tree, heap, sccstack, name);
3708 VN_INFO (name)->on_sccstack = true;
3709 defstmt = SSA_NAME_DEF_STMT (name);
3710
3711 /* Recursively DFS on our operands, looking for SCC's. */
726a989a 3712 if (!gimple_nop_p (defstmt))
89fb70a3 3713 {
6be34936 3714 /* Push a new iterator. */
726a989a 3715 if (gimple_code (defstmt) == GIMPLE_PHI)
6be34936
RG
3716 usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
3717 else
3718 usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
3719 }
3720 else
81f5094d 3721 clear_and_done_ssa_iter (&iter);
6be34936
RG
3722
3723 while (1)
3724 {
3725 /* If we are done processing uses of a name, go up the stack
3726 of iterators and process SCCs as we found them. */
3727 if (op_iter_done (&iter))
89fb70a3 3728 {
6be34936
RG
3729 /* See if we found an SCC. */
3730 if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
3731 if (!extract_and_process_scc_for_name (name))
3732 {
3733 VEC_free (tree, heap, namevec);
3734 VEC_free (ssa_op_iter, heap, itervec);
3735 return false;
3736 }
89fb70a3 3737
6be34936
RG
3738 /* Check if we are done. */
3739 if (VEC_empty (tree, namevec))
3740 {
3741 VEC_free (tree, heap, namevec);
3742 VEC_free (ssa_op_iter, heap, itervec);
3743 return true;
3744 }
3745
3746 /* Restore the last use walker and continue walking there. */
3747 use = name;
3748 name = VEC_pop (tree, namevec);
0823efed 3749 memcpy (&iter, &VEC_last (ssa_op_iter, itervec),
6be34936
RG
3750 sizeof (ssa_op_iter));
3751 VEC_pop (ssa_op_iter, itervec);
3752 goto continue_walking;
3753 }
89fb70a3 3754
6be34936
RG
3755 use = USE_FROM_PTR (usep);
3756
3757 /* Since we handle phi nodes, we will sometimes get
3758 invariants in the use expression. */
3759 if (TREE_CODE (use) == SSA_NAME)
3760 {
89fb70a3
DB
3761 if (! (VN_INFO (use)->visited))
3762 {
6be34936
RG
3763 /* Recurse by pushing the current use walking state on
3764 the stack and starting over. */
3765 VEC_safe_push(ssa_op_iter, heap, itervec, &iter);
3766 VEC_safe_push(tree, heap, namevec, name);
3767 name = use;
3768 goto start_over;
3769
3770continue_walking:
89fb70a3
DB
3771 VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3772 VN_INFO (use)->low);
3773 }
3774 if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3775 && VN_INFO (use)->on_sccstack)
3776 {
3777 VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3778 VN_INFO (name)->low);
3779 }
3780 }
863d2a57 3781
6be34936 3782 usep = op_iter_next_use (&iter);
89fb70a3
DB
3783 }
3784}
3785
89fb70a3
DB
3786/* Allocate a value number table. */
3787
3788static void
3789allocate_vn_table (vn_tables_t table)
3790{
3791 table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
49a1fb2d 3792 table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
89fb70a3
DB
3793 table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
3794 free_reference);
3795
49a1fb2d 3796 gcc_obstack_init (&table->nary_obstack);
89fb70a3
DB
3797 table->phis_pool = create_alloc_pool ("VN phis",
3798 sizeof (struct vn_phi_s),
3799 30);
3800 table->references_pool = create_alloc_pool ("VN references",
3801 sizeof (struct vn_reference_s),
3802 30);
3803}
3804
3805/* Free a value number table. */
3806
3807static void
3808free_vn_table (vn_tables_t table)
3809{
3810 htab_delete (table->phis);
49a1fb2d 3811 htab_delete (table->nary);
89fb70a3 3812 htab_delete (table->references);
49a1fb2d 3813 obstack_free (&table->nary_obstack, NULL);
89fb70a3
DB
3814 free_alloc_pool (table->phis_pool);
3815 free_alloc_pool (table->references_pool);
3816}
3817
3818static void
3819init_scc_vn (void)
3820{
3821 size_t i;
3822 int j;
3823 int *rpo_numbers_temp;
89fb70a3
DB
3824
3825 calculate_dominance_info (CDI_DOMINATORS);
3826 sccstack = NULL;
c9145754
DB
3827 constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
3828 free);
b8698a0f 3829
c9145754 3830 constant_value_ids = BITMAP_ALLOC (NULL);
b8698a0f 3831
89fb70a3 3832 next_dfs_num = 1;
c9145754 3833 next_value_id = 1;
b8698a0f 3834
89fb70a3
DB
3835 vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
3836 /* VEC_alloc doesn't actually grow it to the right size, it just
3837 preallocates the space to do so. */
c302207e
SB
3838 VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table,
3839 num_ssa_names + 1);
cbfb21c1
SB
3840 gcc_obstack_init (&vn_ssa_aux_obstack);
3841
89fb70a3 3842 shared_lookup_phiargs = NULL;
89fb70a3 3843 shared_lookup_references = NULL;
c302207e
SB
3844 rpo_numbers = XNEWVEC (int, last_basic_block);
3845 rpo_numbers_temp = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
89fb70a3
DB
3846 pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3847
3848 /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3849 the i'th block in RPO order is bb. We want to map bb's to RPO
3850 numbers, so we need to rearrange this array. */
3851 for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3852 rpo_numbers[rpo_numbers_temp[j]] = j;
3853
cbfb21c1 3854 XDELETE (rpo_numbers_temp);
89fb70a3
DB
3855
3856 VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3857
3858 /* Create the VN_INFO structures, and initialize value numbers to
3859 TOP. */
3860 for (i = 0; i < num_ssa_names; i++)
3861 {
3862 tree name = ssa_name (i);
3863 if (name)
3864 {
3865 VN_INFO_GET (name)->valnum = VN_TOP;
726a989a 3866 VN_INFO (name)->expr = NULL_TREE;
c9145754 3867 VN_INFO (name)->value_id = 0;
89fb70a3
DB
3868 }
3869 }
3870
908ff6a3 3871 renumber_gimple_stmt_uids ();
89fb70a3
DB
3872
3873 /* Create the valid and optimistic value numbering tables. */
3874 valid_info = XCNEW (struct vn_tables_s);
3875 allocate_vn_table (valid_info);
3876 optimistic_info = XCNEW (struct vn_tables_s);
3877 allocate_vn_table (optimistic_info);
89fb70a3
DB
3878}
3879
3880void
3881free_scc_vn (void)
3882{
3883 size_t i;
3884
c9145754
DB
3885 htab_delete (constant_to_value_id);
3886 BITMAP_FREE (constant_value_ids);
89fb70a3 3887 VEC_free (tree, heap, shared_lookup_phiargs);
89fb70a3
DB
3888 VEC_free (vn_reference_op_s, heap, shared_lookup_references);
3889 XDELETEVEC (rpo_numbers);
cbfb21c1 3890
89fb70a3
DB
3891 for (i = 0; i < num_ssa_names; i++)
3892 {
3893 tree name = ssa_name (i);
3d45dd59
RG
3894 if (name
3895 && VN_INFO (name)->needs_insertion)
3896 release_ssa_name (name);
89fb70a3 3897 }
cbfb21c1 3898 obstack_free (&vn_ssa_aux_obstack, NULL);
89fb70a3 3899 VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
cbfb21c1 3900
89fb70a3
DB
3901 VEC_free (tree, heap, sccstack);
3902 free_vn_table (valid_info);
3903 XDELETE (valid_info);
3904 free_vn_table (optimistic_info);
3905 XDELETE (optimistic_info);
89fb70a3
DB
3906}
3907
9ad6bebe
NF
3908/* Set *ID if we computed something useful in RESULT. */
3909
3910static void
3911set_value_id_for_result (tree result, unsigned int *id)
3912{
3913 if (result)
3914 {
3915 if (TREE_CODE (result) == SSA_NAME)
3916 *id = VN_INFO (result)->value_id;
3917 else if (is_gimple_min_invariant (result))
3918 *id = get_or_alloc_constant_value_id (result);
3919 }
3920}
3921
caf55296 3922/* Set the value ids in the valid hash tables. */
c9145754
DB
3923
3924static void
3925set_hashtable_value_ids (void)
3926{
3927 htab_iterator hi;
3928 vn_nary_op_t vno;
3929 vn_reference_t vr;
3930 vn_phi_t vp;
caf55296 3931
c9145754
DB
3932 /* Now set the value ids of the things we had put in the hash
3933 table. */
3934
3935 FOR_EACH_HTAB_ELEMENT (valid_info->nary,
b8698a0f 3936 vno, vn_nary_op_t, hi)
9ad6bebe 3937 set_value_id_for_result (vno->result, &vno->value_id);
c9145754 3938
c9145754 3939 FOR_EACH_HTAB_ELEMENT (valid_info->phis,
b8698a0f 3940 vp, vn_phi_t, hi)
9ad6bebe 3941 set_value_id_for_result (vp->result, &vp->value_id);
c9145754
DB
3942
3943 FOR_EACH_HTAB_ELEMENT (valid_info->references,
b8698a0f 3944 vr, vn_reference_t, hi)
9ad6bebe 3945 set_value_id_for_result (vr->result, &vr->value_id);
c9145754
DB
3946}
3947
863d2a57 3948/* Do SCCVN. Returns true if it finished, false if we bailed out
1ec87690
RG
3949 due to resource constraints. DEFAULT_VN_WALK_KIND_ specifies
3950 how we use the alias oracle walking during the VN process. */
863d2a57
RG
3951
3952bool
1ec87690 3953run_scc_vn (vn_lookup_kind default_vn_walk_kind_)
89fb70a3
DB
3954{
3955 size_t i;
3956 tree param;
c9145754 3957 bool changed = true;
b8698a0f 3958
1ec87690
RG
3959 default_vn_walk_kind = default_vn_walk_kind_;
3960
89fb70a3
DB
3961 init_scc_vn ();
3962 current_info = valid_info;
3963
3964 for (param = DECL_ARGUMENTS (current_function_decl);
3965 param;
910ad8de 3966 param = DECL_CHAIN (param))
89fb70a3 3967 {
32244553
RG
3968 tree def = ssa_default_def (cfun, param);
3969 if (def)
3970 VN_INFO (def)->valnum = def;
89fb70a3
DB
3971 }
3972
3d45dd59 3973 for (i = 1; i < num_ssa_names; ++i)
89fb70a3
DB
3974 {
3975 tree name = ssa_name (i);
3976 if (name
3977 && VN_INFO (name)->visited == false
3978 && !has_zero_uses (name))
863d2a57
RG
3979 if (!DFS (name))
3980 {
3981 free_scc_vn ();
3982 return false;
3983 }
89fb70a3
DB
3984 }
3985
c9145754 3986 /* Initialize the value ids. */
b8698a0f 3987
c9145754
DB
3988 for (i = 1; i < num_ssa_names; ++i)
3989 {
3990 tree name = ssa_name (i);
3991 vn_ssa_aux_t info;
3992 if (!name)
3993 continue;
3994 info = VN_INFO (name);
f116fecf
RG
3995 if (info->valnum == name
3996 || info->valnum == VN_TOP)
c9145754
DB
3997 info->value_id = get_next_value_id ();
3998 else if (is_gimple_min_invariant (info->valnum))
3999 info->value_id = get_or_alloc_constant_value_id (info->valnum);
4000 }
b8698a0f 4001
c9145754
DB
4002 /* Propagate until they stop changing. */
4003 while (changed)
4004 {
4005 changed = false;
4006 for (i = 1; i < num_ssa_names; ++i)
4007 {
4008 tree name = ssa_name (i);
4009 vn_ssa_aux_t info;
4010 if (!name)
4011 continue;
4012 info = VN_INFO (name);
4013 if (TREE_CODE (info->valnum) == SSA_NAME
4014 && info->valnum != name
c9145754
DB
4015 && info->value_id != VN_INFO (info->valnum)->value_id)
4016 {
4017 changed = true;
4018 info->value_id = VN_INFO (info->valnum)->value_id;
4019 }
4020 }
4021 }
b8698a0f 4022
c9145754 4023 set_hashtable_value_ids ();
b8698a0f 4024
89fb70a3
DB
4025 if (dump_file && (dump_flags & TDF_DETAILS))
4026 {
4027 fprintf (dump_file, "Value numbers:\n");
4028 for (i = 0; i < num_ssa_names; i++)
4029 {
4030 tree name = ssa_name (i);
caf55296
RG
4031 if (name
4032 && VN_INFO (name)->visited
4033 && SSA_VAL (name) != name)
89fb70a3
DB
4034 {
4035 print_generic_expr (dump_file, name, 0);
4036 fprintf (dump_file, " = ");
caf55296 4037 print_generic_expr (dump_file, SSA_VAL (name), 0);
89fb70a3
DB
4038 fprintf (dump_file, "\n");
4039 }
4040 }
4041 }
863d2a57
RG
4042
4043 return true;
89fb70a3 4044}
c9145754
DB
4045
4046/* Return the maximum value id we have ever seen. */
4047
4048unsigned int
b8698a0f 4049get_max_value_id (void)
c9145754
DB
4050{
4051 return next_value_id;
4052}
4053
4054/* Return the next unique value id. */
4055
4056unsigned int
4057get_next_value_id (void)
4058{
4059 return next_value_id++;
4060}
4061
4062
330e765e 4063/* Compare two expressions E1 and E2 and return true if they are equal. */
c9145754
DB
4064
4065bool
4066expressions_equal_p (tree e1, tree e2)
4067{
330e765e 4068 /* The obvious case. */
c9145754
DB
4069 if (e1 == e2)
4070 return true;
4071
330e765e
EB
4072 /* If only one of them is null, they cannot be equal. */
4073 if (!e1 || !e2)
4074 return false;
4075
330e765e
EB
4076 /* Now perform the actual comparison. */
4077 if (TREE_CODE (e1) == TREE_CODE (e2)
4078 && operand_equal_p (e1, e2, OEP_PURE_SAME))
c9145754
DB
4079 return true;
4080
4081 return false;
4082}
4083
890065bf
RG
4084
4085/* Return true if the nary operation NARY may trap. This is a copy
4086 of stmt_could_throw_1_p adjusted to the SCCVN IL. */
4087
4088bool
4089vn_nary_may_trap (vn_nary_op_t nary)
4090{
4091 tree type;
6141b7db 4092 tree rhs2 = NULL_TREE;
890065bf
RG
4093 bool honor_nans = false;
4094 bool honor_snans = false;
4095 bool fp_operation = false;
4096 bool honor_trapv = false;
4097 bool handled, ret;
4098 unsigned i;
4099
4100 if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
4101 || TREE_CODE_CLASS (nary->opcode) == tcc_unary
4102 || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
4103 {
4104 type = nary->type;
4105 fp_operation = FLOAT_TYPE_P (type);
4106 if (fp_operation)
4107 {
4108 honor_nans = flag_trapping_math && !flag_finite_math_only;
4109 honor_snans = flag_signaling_nans != 0;
4110 }
4111 else if (INTEGRAL_TYPE_P (type)
4112 && TYPE_OVERFLOW_TRAPS (type))
4113 honor_trapv = true;
4114 }
6141b7db
RG
4115 if (nary->length >= 2)
4116 rhs2 = nary->op[1];
890065bf
RG
4117 ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
4118 honor_trapv,
4119 honor_nans, honor_snans, rhs2,
4120 &handled);
4121 if (handled
4122 && ret)
4123 return true;
4124
4125 for (i = 0; i < nary->length; ++i)
4126 if (tree_could_trap_p (nary->op[i]))
4127 return true;
4128
4129 return false;
4130}