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