OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [tree-ssa-sccvn.c] - Blame information for rev 861

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 280 jeremybenn
/* SCC value numbering for trees
2
   Copyright (C) 2006, 2007, 2008, 2009, 2010
3
   Free Software Foundation, Inc.
4
   Contributed by Daniel Berlin <dan@dberlin.org>
5
 
6
This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3, or (at your option)
11
any later version.
12
 
13
GCC is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING3.  If not see
20
<http://www.gnu.org/licenses/>.  */
21
 
22
#include "config.h"
23
#include "system.h"
24
#include "coretypes.h"
25
#include "tm.h"
26
#include "ggc.h"
27
#include "tree.h"
28
#include "basic-block.h"
29
#include "diagnostic.h"
30
#include "tree-inline.h"
31
#include "tree-flow.h"
32
#include "gimple.h"
33
#include "tree-dump.h"
34
#include "timevar.h"
35
#include "fibheap.h"
36
#include "hashtab.h"
37
#include "tree-iterator.h"
38
#include "real.h"
39
#include "alloc-pool.h"
40
#include "tree-pass.h"
41
#include "flags.h"
42
#include "bitmap.h"
43
#include "langhooks.h"
44
#include "cfgloop.h"
45
#include "params.h"
46
#include "tree-ssa-propagate.h"
47
#include "tree-ssa-sccvn.h"
48
 
49
/* This algorithm is based on the SCC algorithm presented by Keith
50
   Cooper and L. Taylor Simpson in "SCC-Based Value numbering"
51
   (http://citeseer.ist.psu.edu/41805.html).  In
52
   straight line code, it is equivalent to a regular hash based value
53
   numbering that is performed in reverse postorder.
54
 
55
   For code with cycles, there are two alternatives, both of which
56
   require keeping the hashtables separate from the actual list of
57
   value numbers for SSA names.
58
 
59
   1. Iterate value numbering in an RPO walk of the blocks, removing
60
   all the entries from the hashtable after each iteration (but
61
   keeping the SSA name->value number mapping between iterations).
62
   Iterate until it does not change.
63
 
64
   2. Perform value numbering as part of an SCC walk on the SSA graph,
65
   iterating only the cycles in the SSA graph until they do not change
66
   (using a separate, optimistic hashtable for value numbering the SCC
67
   operands).
68
 
69
   The second is not just faster in practice (because most SSA graph
70
   cycles do not involve all the variables in the graph), it also has
71
   some nice properties.
72
 
73
   One of these nice properties is that when we pop an SCC off the
74
   stack, we are guaranteed to have processed all the operands coming from
75
   *outside of that SCC*, so we do not need to do anything special to
76
   ensure they have value numbers.
77
 
78
   Another nice property is that the SCC walk is done as part of a DFS
79
   of the SSA graph, which makes it easy to perform combining and
80
   simplifying operations at the same time.
81
 
82
   The code below is deliberately written in a way that makes it easy
83
   to separate the SCC walk from the other work it does.
84
 
85
   In order to propagate constants through the code, we track which
86
   expressions contain constants, and use those while folding.  In
87
   theory, we could also track expressions whose value numbers are
88
   replaced, in case we end up folding based on expression
89
   identities.
90
 
91
   In order to value number memory, we assign value numbers to vuses.
92
   This enables us to note that, for example, stores to the same
93
   address of the same value from the same starting memory states are
94
   equivalent.
95
   TODO:
96
 
97
   1. We can iterate only the changing portions of the SCC's, but
98
   I have not seen an SCC big enough for this to be a win.
99
   2. If you differentiate between phi nodes for loops and phi nodes
100
   for if-then-else, you can properly consider phi nodes in different
101
   blocks for equivalence.
102
   3. We could value number vuses in more cases, particularly, whole
103
   structure copies.
104
*/
105
 
106
/* The set of hashtables and alloc_pool's for their items.  */
107
 
108
typedef struct vn_tables_s
109
{
110
  htab_t nary;
111
  htab_t phis;
112
  htab_t references;
113
  struct obstack nary_obstack;
114
  alloc_pool phis_pool;
115
  alloc_pool references_pool;
116
} *vn_tables_t;
117
 
118
static htab_t constant_to_value_id;
119
static bitmap constant_value_ids;
120
 
121
 
122
/* Valid hashtables storing information we have proven to be
123
   correct.  */
124
 
125
static vn_tables_t valid_info;
126
 
127
/* Optimistic hashtables storing information we are making assumptions about
128
   during iterations.  */
129
 
130
static vn_tables_t optimistic_info;
131
 
132
/* Pointer to the set of hashtables that is currently being used.
133
   Should always point to either the optimistic_info, or the
134
   valid_info.  */
135
 
136
static vn_tables_t current_info;
137
 
138
 
139
/* Reverse post order index for each basic block.  */
140
 
141
static int *rpo_numbers;
142
 
143
#define SSA_VAL(x) (VN_INFO ((x))->valnum)
144
 
145
/* This represents the top of the VN lattice, which is the universal
146
   value.  */
147
 
148
tree VN_TOP;
149
 
150
/* Unique counter for our value ids.  */
151
 
152
static unsigned int next_value_id;
153
 
154
/* Next DFS number and the stack for strongly connected component
155
   detection. */
156
 
157
static unsigned int next_dfs_num;
158
static VEC (tree, heap) *sccstack;
159
 
160
static bool may_insert;
161
 
162
 
163
DEF_VEC_P(vn_ssa_aux_t);
164
DEF_VEC_ALLOC_P(vn_ssa_aux_t, heap);
165
 
166
/* Table of vn_ssa_aux_t's, one per ssa_name.  The vn_ssa_aux_t objects
167
   are allocated on an obstack for locality reasons, and to free them
168
   without looping over the VEC.  */
169
 
170
static VEC (vn_ssa_aux_t, heap) *vn_ssa_aux_table;
171
static struct obstack vn_ssa_aux_obstack;
172
 
173
/* Return the value numbering information for a given SSA name.  */
174
 
175
vn_ssa_aux_t
176
VN_INFO (tree name)
177
{
178
  vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
179
                                SSA_NAME_VERSION (name));
180
  gcc_assert (res);
181
  return res;
182
}
183
 
184
/* Set the value numbering info for a given SSA name to a given
185
   value.  */
186
 
187
static inline void
188
VN_INFO_SET (tree name, vn_ssa_aux_t value)
189
{
190
  VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
191
               SSA_NAME_VERSION (name), value);
192
}
193
 
194
/* Initialize the value numbering info for a given SSA name.
195
   This should be called just once for every SSA name.  */
196
 
197
vn_ssa_aux_t
198
VN_INFO_GET (tree name)
199
{
200
  vn_ssa_aux_t newinfo;
201
 
202
  newinfo = XOBNEW (&vn_ssa_aux_obstack, struct vn_ssa_aux);
203
  memset (newinfo, 0, sizeof (struct vn_ssa_aux));
204
  if (SSA_NAME_VERSION (name) >= VEC_length (vn_ssa_aux_t, vn_ssa_aux_table))
205
    VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table,
206
                   SSA_NAME_VERSION (name) + 1);
207
  VEC_replace (vn_ssa_aux_t, vn_ssa_aux_table,
208
               SSA_NAME_VERSION (name), newinfo);
209
  return newinfo;
210
}
211
 
212
 
213
/* Get the representative expression for the SSA_NAME NAME.  Returns
214
   the representative SSA_NAME if there is no expression associated with it.  */
215
 
216
tree
217
vn_get_expr_for (tree name)
218
{
219
  vn_ssa_aux_t vn = VN_INFO (name);
220
  gimple def_stmt;
221
  tree expr = NULL_TREE;
222
 
223
  if (vn->valnum == VN_TOP)
224
    return name;
225
 
226
  /* If the value-number is a constant it is the representative
227
     expression.  */
228
  if (TREE_CODE (vn->valnum) != SSA_NAME)
229
    return vn->valnum;
230
 
231
  /* Get to the information of the value of this SSA_NAME.  */
232
  vn = VN_INFO (vn->valnum);
233
 
234
  /* If the value-number is a constant it is the representative
235
     expression.  */
236
  if (TREE_CODE (vn->valnum) != SSA_NAME)
237
    return vn->valnum;
238
 
239
  /* Else if we have an expression, return it.  */
240
  if (vn->expr != NULL_TREE)
241
    return vn->expr;
242
 
243
  /* Otherwise use the defining statement to build the expression.  */
244
  def_stmt = SSA_NAME_DEF_STMT (vn->valnum);
245
 
246
  /* If the value number is a default-definition or a PHI result
247
     use it directly.  */
248
  if (gimple_nop_p (def_stmt)
249
      || gimple_code (def_stmt) == GIMPLE_PHI)
250
    return vn->valnum;
251
 
252
  if (!is_gimple_assign (def_stmt))
253
    return vn->valnum;
254
 
255
  /* FIXME tuples.  This is incomplete and likely will miss some
256
     simplifications.  */
257
  switch (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)))
258
    {
259
    case tcc_reference:
260
      if ((gimple_assign_rhs_code (def_stmt) == VIEW_CONVERT_EXPR
261
           || gimple_assign_rhs_code (def_stmt) == REALPART_EXPR
262
           || gimple_assign_rhs_code (def_stmt) == IMAGPART_EXPR)
263
          && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
264
        expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
265
                            gimple_expr_type (def_stmt),
266
                            TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0));
267
      break;
268
 
269
    case tcc_unary:
270
      expr = fold_build1 (gimple_assign_rhs_code (def_stmt),
271
                          gimple_expr_type (def_stmt),
272
                          gimple_assign_rhs1 (def_stmt));
273
      break;
274
 
275
    case tcc_binary:
276
      expr = fold_build2 (gimple_assign_rhs_code (def_stmt),
277
                          gimple_expr_type (def_stmt),
278
                          gimple_assign_rhs1 (def_stmt),
279
                          gimple_assign_rhs2 (def_stmt));
280
      break;
281
 
282
    default:;
283
    }
284
  if (expr == NULL_TREE)
285
    return vn->valnum;
286
 
287
  /* Cache the expression.  */
288
  vn->expr = expr;
289
 
290
  return expr;
291
}
292
 
293
 
294
/* Free a phi operation structure VP.  */
295
 
296
static void
297
free_phi (void *vp)
298
{
299
  vn_phi_t phi = (vn_phi_t) vp;
300
  VEC_free (tree, heap, phi->phiargs);
301
}
302
 
303
/* Free a reference operation structure VP.  */
304
 
305
static void
306
free_reference (void *vp)
307
{
308
  vn_reference_t vr = (vn_reference_t) vp;
309
  VEC_free (vn_reference_op_s, heap, vr->operands);
310
}
311
 
312
/* Hash table equality function for vn_constant_t.  */
313
 
314
static int
315
vn_constant_eq (const void *p1, const void *p2)
316
{
317
  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
318
  const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
319
 
320
  if (vc1->hashcode != vc2->hashcode)
321
    return false;
322
 
323
  return vn_constant_eq_with_type (vc1->constant, vc2->constant);
324
}
325
 
326
/* Hash table hash function for vn_constant_t.  */
327
 
328
static hashval_t
329
vn_constant_hash (const void *p1)
330
{
331
  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
332
  return vc1->hashcode;
333
}
334
 
335
/* Lookup a value id for CONSTANT and return it.  If it does not
336
   exist returns 0.  */
337
 
338
unsigned int
339
get_constant_value_id (tree constant)
340
{
341
  void **slot;
342
  struct vn_constant_s vc;
343
 
344
  vc.hashcode = vn_hash_constant_with_type (constant);
345
  vc.constant = constant;
346
  slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
347
                                   vc.hashcode, NO_INSERT);
348
  if (slot)
349
    return ((vn_constant_t)*slot)->value_id;
350
  return 0;
351
}
352
 
353
/* Lookup a value id for CONSTANT, and if it does not exist, create a
354
   new one and return it.  If it does exist, return it.  */
355
 
356
unsigned int
357
get_or_alloc_constant_value_id (tree constant)
358
{
359
  void **slot;
360
  struct vn_constant_s vc;
361
  vn_constant_t vcp;
362
 
363
  vc.hashcode = vn_hash_constant_with_type (constant);
364
  vc.constant = constant;
365
  slot = htab_find_slot_with_hash (constant_to_value_id, &vc,
366
                                   vc.hashcode, INSERT);
367
  if (*slot)
368
    return ((vn_constant_t)*slot)->value_id;
369
 
370
  vcp = XNEW (struct vn_constant_s);
371
  vcp->hashcode = vc.hashcode;
372
  vcp->constant = constant;
373
  vcp->value_id = get_next_value_id ();
374
  *slot = (void *) vcp;
375
  bitmap_set_bit (constant_value_ids, vcp->value_id);
376
  return vcp->value_id;
377
}
378
 
379
/* Return true if V is a value id for a constant.  */
380
 
381
bool
382
value_id_constant_p (unsigned int v)
383
{
384
  return bitmap_bit_p (constant_value_ids, v);
385
}
386
 
387
/* Compare two reference operands P1 and P2 for equality.  Return true if
388
   they are equal, and false otherwise.  */
389
 
390
static int
391
vn_reference_op_eq (const void *p1, const void *p2)
392
{
393
  const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
394
  const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
395
 
396
  return vro1->opcode == vro2->opcode
397
    && types_compatible_p (vro1->type, vro2->type)
398
    && expressions_equal_p (vro1->op0, vro2->op0)
399
    && expressions_equal_p (vro1->op1, vro2->op1)
400
    && expressions_equal_p (vro1->op2, vro2->op2);
401
}
402
 
403
/* Compute the hash for a reference operand VRO1.  */
404
 
405
static hashval_t
406
vn_reference_op_compute_hash (const vn_reference_op_t vro1, hashval_t result)
407
{
408
  result = iterative_hash_hashval_t (vro1->opcode, result);
409
  if (vro1->op0)
410
    result = iterative_hash_expr (vro1->op0, result);
411
  if (vro1->op1)
412
    result = iterative_hash_expr (vro1->op1, result);
413
  if (vro1->op2)
414
    result = iterative_hash_expr (vro1->op2, result);
415
  return result;
416
}
417
 
418
/* Return the hashcode for a given reference operation P1.  */
419
 
420
static hashval_t
421
vn_reference_hash (const void *p1)
422
{
423
  const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
424
  return vr1->hashcode;
425
}
426
 
427
/* Compute a hash for the reference operation VR1 and return it.  */
428
 
429
hashval_t
430
vn_reference_compute_hash (const vn_reference_t vr1)
431
{
432
  hashval_t result = 0;
433
  int i;
434
  vn_reference_op_t vro;
435
 
436
  for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
437
    result = vn_reference_op_compute_hash (vro, result);
438
  if (vr1->vuse)
439
    result += SSA_NAME_VERSION (vr1->vuse);
440
 
441
  return result;
442
}
443
 
444
/* Return true if reference operations P1 and P2 are equivalent.  This
445
   means they have the same set of operands and vuses.  */
446
 
447
int
448
vn_reference_eq (const void *p1, const void *p2)
449
{
450
  int i;
451
  vn_reference_op_t vro;
452
 
453
  const_vn_reference_t const vr1 = (const_vn_reference_t) p1;
454
  const_vn_reference_t const vr2 = (const_vn_reference_t) p2;
455
  if (vr1->hashcode != vr2->hashcode)
456
    return false;
457
 
458
  /* Early out if this is not a hash collision.  */
459
  if (vr1->hashcode != vr2->hashcode)
460
    return false;
461
 
462
  /* The VOP needs to be the same.  */
463
  if (vr1->vuse != vr2->vuse)
464
    return false;
465
 
466
  /* If the operands are the same we are done.  */
467
  if (vr1->operands == vr2->operands)
468
    return true;
469
 
470
  /* We require that address operands be canonicalized in a way that
471
     two memory references will have the same operands if they are
472
     equivalent.  */
473
  if (VEC_length (vn_reference_op_s, vr1->operands)
474
      != VEC_length (vn_reference_op_s, vr2->operands))
475
    return false;
476
 
477
  for (i = 0; VEC_iterate (vn_reference_op_s, vr1->operands, i, vro); i++)
478
    if (!vn_reference_op_eq (VEC_index (vn_reference_op_s, vr2->operands, i),
479
                             vro))
480
      return false;
481
 
482
  return true;
483
}
484
 
485
/* Copy the operations present in load/store REF into RESULT, a vector of
486
   vn_reference_op_s's.  */
487
 
488
void
489
copy_reference_ops_from_ref (tree ref, VEC(vn_reference_op_s, heap) **result)
490
{
491
  if (TREE_CODE (ref) == TARGET_MEM_REF)
492
    {
493
      vn_reference_op_s temp;
494
      tree base;
495
 
496
      base = TMR_SYMBOL (ref) ? TMR_SYMBOL (ref) : TMR_BASE (ref);
497
      if (!base)
498
        base = build_int_cst (ptr_type_node, 0);
499
 
500
      memset (&temp, 0, sizeof (temp));
501
      /* We do not care for spurious type qualifications.  */
502
      temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
503
      temp.opcode = TREE_CODE (ref);
504
      temp.op0 = TMR_INDEX (ref);
505
      temp.op1 = TMR_STEP (ref);
506
      temp.op2 = TMR_OFFSET (ref);
507
      VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
508
 
509
      memset (&temp, 0, sizeof (temp));
510
      temp.type = NULL_TREE;
511
      temp.opcode = TREE_CODE (base);
512
      temp.op0 = base;
513
      temp.op1 = TMR_ORIGINAL (ref);
514
      VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
515
      return;
516
    }
517
 
518
  /* For non-calls, store the information that makes up the address.  */
519
 
520
  while (ref)
521
    {
522
      vn_reference_op_s temp;
523
 
524
      memset (&temp, 0, sizeof (temp));
525
      /* We do not care for spurious type qualifications.  */
526
      temp.type = TYPE_MAIN_VARIANT (TREE_TYPE (ref));
527
      temp.opcode = TREE_CODE (ref);
528
 
529
      switch (temp.opcode)
530
        {
531
        case ALIGN_INDIRECT_REF:
532
        case INDIRECT_REF:
533
          /* The only operand is the address, which gets its own
534
             vn_reference_op_s structure.  */
535
          break;
536
        case MISALIGNED_INDIRECT_REF:
537
          temp.op0 = TREE_OPERAND (ref, 1);
538
          break;
539
        case BIT_FIELD_REF:
540
          /* Record bits and position.  */
541
          temp.op0 = TREE_OPERAND (ref, 1);
542
          temp.op1 = TREE_OPERAND (ref, 2);
543
          break;
544
        case COMPONENT_REF:
545
          /* The field decl is enough to unambiguously specify the field,
546
             a matching type is not necessary and a mismatching type
547
             is always a spurious difference.  */
548
          temp.type = NULL_TREE;
549
          temp.op0 = TREE_OPERAND (ref, 1);
550
          temp.op1 = TREE_OPERAND (ref, 2);
551
          /* If this is a reference to a union member, record the union
552
             member size as operand.  Do so only if we are doing
553
             expression insertion (during FRE), as PRE currently gets
554
             confused with this.  */
555
          if (may_insert
556
              && temp.op1 == NULL_TREE
557
              && TREE_CODE (DECL_CONTEXT (temp.op0)) == UNION_TYPE
558
              && integer_zerop (DECL_FIELD_OFFSET (temp.op0))
559
              && integer_zerop (DECL_FIELD_BIT_OFFSET (temp.op0))
560
              && host_integerp (DECL_SIZE (temp.op0), 0))
561
            temp.op0 = DECL_SIZE (temp.op0);
562
          break;
563
        case ARRAY_RANGE_REF:
564
        case ARRAY_REF:
565
          /* Record index as operand.  */
566
          temp.op0 = TREE_OPERAND (ref, 1);
567
          /* Always record lower bounds and element size.  */
568
          temp.op1 = array_ref_low_bound (ref);
569
          temp.op2 = array_ref_element_size (ref);
570
          break;
571
        case STRING_CST:
572
        case INTEGER_CST:
573
        case COMPLEX_CST:
574
        case VECTOR_CST:
575
        case REAL_CST:
576
        case CONSTRUCTOR:
577
        case VAR_DECL:
578
        case PARM_DECL:
579
        case CONST_DECL:
580
        case RESULT_DECL:
581
        case SSA_NAME:
582
          temp.op0 = ref;
583
          break;
584
        case ADDR_EXPR:
585
          if (is_gimple_min_invariant (ref))
586
            {
587
              temp.op0 = ref;
588
              break;
589
            }
590
          /* Fallthrough.  */
591
          /* These are only interesting for their operands, their
592
             existence, and their type.  They will never be the last
593
             ref in the chain of references (IE they require an
594
             operand), so we don't have to put anything
595
             for op* as it will be handled by the iteration  */
596
        case IMAGPART_EXPR:
597
        case REALPART_EXPR:
598
        case VIEW_CONVERT_EXPR:
599
          break;
600
        default:
601
          gcc_unreachable ();
602
        }
603
      VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
604
 
605
      if (REFERENCE_CLASS_P (ref)
606
          || (TREE_CODE (ref) == ADDR_EXPR
607
              && !is_gimple_min_invariant (ref)))
608
        ref = TREE_OPERAND (ref, 0);
609
      else
610
        ref = NULL_TREE;
611
    }
612
}
613
 
614
/* Build a alias-oracle reference abstraction in *REF from the vn_reference
615
   operands in *OPS, the reference alias set SET and the reference type TYPE.
616
   Return true if something useful was produced.  */
617
 
618
bool
619
ao_ref_init_from_vn_reference (ao_ref *ref,
620
                               alias_set_type set, tree type,
621
                               VEC (vn_reference_op_s, heap) *ops)
622
{
623
  vn_reference_op_t op;
624
  unsigned i;
625
  tree base = NULL_TREE;
626
  tree *op0_p = &base;
627
  HOST_WIDE_INT offset = 0;
628
  HOST_WIDE_INT max_size;
629
  HOST_WIDE_INT size = -1;
630
  tree size_tree = NULL_TREE;
631
 
632
  /* First get the final access size from just the outermost expression.  */
633
  op = VEC_index (vn_reference_op_s, ops, 0);
634
  if (op->opcode == COMPONENT_REF)
635
    {
636
      if (TREE_CODE (op->op0) == INTEGER_CST)
637
        size_tree = op->op0;
638
      else
639
        size_tree = DECL_SIZE (op->op0);
640
    }
641
  else if (op->opcode == BIT_FIELD_REF)
642
    size_tree = op->op0;
643
  else
644
    {
645
      enum machine_mode mode = TYPE_MODE (type);
646
      if (mode == BLKmode)
647
        size_tree = TYPE_SIZE (type);
648
      else
649
        size = GET_MODE_BITSIZE (mode);
650
    }
651
  if (size_tree != NULL_TREE)
652
    {
653
      if (!host_integerp (size_tree, 1))
654
        size = -1;
655
      else
656
        size = TREE_INT_CST_LOW (size_tree);
657
    }
658
 
659
  /* Initially, maxsize is the same as the accessed element size.
660
     In the following it will only grow (or become -1).  */
661
  max_size = size;
662
 
663
  /* Compute cumulative bit-offset for nested component-refs and array-refs,
664
     and find the ultimate containing object.  */
665
  for (i = 0; VEC_iterate (vn_reference_op_s, ops, i, op); ++i)
666
    {
667
      switch (op->opcode)
668
        {
669
        /* These may be in the reference ops, but we cannot do anything
670
           sensible with them here.  */
671
        case CALL_EXPR:
672
        case ADDR_EXPR:
673
          return false;
674
 
675
        /* Record the base objects.  */
676
        case ALIGN_INDIRECT_REF:
677
        case INDIRECT_REF:
678
          *op0_p = build1 (op->opcode, op->type, NULL_TREE);
679
          op0_p = &TREE_OPERAND (*op0_p, 0);
680
          break;
681
 
682
        case MISALIGNED_INDIRECT_REF:
683
          *op0_p = build2 (MISALIGNED_INDIRECT_REF, op->type,
684
                           NULL_TREE, op->op0);
685
          op0_p = &TREE_OPERAND (*op0_p, 0);
686
          break;
687
 
688
        case VAR_DECL:
689
        case PARM_DECL:
690
        case RESULT_DECL:
691
        case SSA_NAME:
692
          *op0_p = op->op0;
693
          break;
694
 
695
        /* And now the usual component-reference style ops.  */
696
        case BIT_FIELD_REF:
697
          offset += tree_low_cst (op->op1, 0);
698
          break;
699
 
700
        case COMPONENT_REF:
701
          {
702
            tree field = op->op0;
703
            /* We do not have a complete COMPONENT_REF tree here so we
704
               cannot use component_ref_field_offset.  Do the interesting
705
               parts manually.  */
706
 
707
            /* Our union trick, done for offset zero only.  */
708
            if (TREE_CODE (field) == INTEGER_CST)
709
              ;
710
            else if (op->op1
711
                     || !host_integerp (DECL_FIELD_OFFSET (field), 1))
712
              max_size = -1;
713
            else
714
              {
715
                offset += (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (field))
716
                           * BITS_PER_UNIT);
717
                offset += TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field));
718
              }
719
            break;
720
          }
721
 
722
        case ARRAY_RANGE_REF:
723
        case ARRAY_REF:
724
          /* We recorded the lower bound and the element size.  */
725
          if (!host_integerp (op->op0, 0)
726
              || !host_integerp (op->op1, 0)
727
              || !host_integerp (op->op2, 0))
728
            max_size = -1;
729
          else
730
            {
731
              HOST_WIDE_INT hindex = TREE_INT_CST_LOW (op->op0);
732
              hindex -= TREE_INT_CST_LOW (op->op1);
733
              hindex *= TREE_INT_CST_LOW (op->op2);
734
              hindex *= BITS_PER_UNIT;
735
              offset += hindex;
736
            }
737
          break;
738
 
739
        case REALPART_EXPR:
740
          break;
741
 
742
        case IMAGPART_EXPR:
743
          offset += size;
744
          break;
745
 
746
        case VIEW_CONVERT_EXPR:
747
          break;
748
 
749
        case STRING_CST:
750
        case INTEGER_CST:
751
        case COMPLEX_CST:
752
        case VECTOR_CST:
753
        case REAL_CST:
754
        case CONSTRUCTOR:
755
        case CONST_DECL:
756
          return false;
757
 
758
        default:
759
          return false;
760
        }
761
    }
762
 
763
  if (base == NULL_TREE)
764
    return false;
765
 
766
  ref->ref = NULL_TREE;
767
  ref->base = base;
768
  ref->offset = offset;
769
  ref->size = size;
770
  ref->max_size = max_size;
771
  ref->ref_alias_set = set;
772
  ref->base_alias_set = -1;
773
 
774
  return true;
775
}
776
 
777
/* Copy the operations present in load/store/call REF into RESULT, a vector of
778
   vn_reference_op_s's.  */
779
 
780
void
781
copy_reference_ops_from_call (gimple call,
782
                              VEC(vn_reference_op_s, heap) **result)
783
{
784
  vn_reference_op_s temp;
785
  unsigned i;
786
 
787
  /* Copy the type, opcode, function being called and static chain.  */
788
  memset (&temp, 0, sizeof (temp));
789
  temp.type = gimple_call_return_type (call);
790
  temp.opcode = CALL_EXPR;
791
  temp.op0 = gimple_call_fn (call);
792
  temp.op1 = gimple_call_chain (call);
793
  VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
794
 
795
  /* Copy the call arguments.  As they can be references as well,
796
     just chain them together.  */
797
  for (i = 0; i < gimple_call_num_args (call); ++i)
798
    {
799
      tree callarg = gimple_call_arg (call, i);
800
      copy_reference_ops_from_ref (callarg, result);
801
    }
802
}
803
 
804
/* Create a vector of vn_reference_op_s structures from REF, a
805
   REFERENCE_CLASS_P tree.  The vector is not shared. */
806
 
807
static VEC(vn_reference_op_s, heap) *
808
create_reference_ops_from_ref (tree ref)
809
{
810
  VEC (vn_reference_op_s, heap) *result = NULL;
811
 
812
  copy_reference_ops_from_ref (ref, &result);
813
  return result;
814
}
815
 
816
/* Create a vector of vn_reference_op_s structures from CALL, a
817
   call statement.  The vector is not shared.  */
818
 
819
static VEC(vn_reference_op_s, heap) *
820
create_reference_ops_from_call (gimple call)
821
{
822
  VEC (vn_reference_op_s, heap) *result = NULL;
823
 
824
  copy_reference_ops_from_call (call, &result);
825
  return result;
826
}
827
 
828
/* Fold *& at position *I_P in a vn_reference_op_s vector *OPS.  Updates
829
   *I_P to point to the last element of the replacement.  */
830
void
831
vn_reference_fold_indirect (VEC (vn_reference_op_s, heap) **ops,
832
                            unsigned int *i_p)
833
{
834
  VEC(vn_reference_op_s, heap) *mem = NULL;
835
  vn_reference_op_t op;
836
  unsigned int i = *i_p;
837
  unsigned int j;
838
 
839
  /* Get ops for the addressed object.  */
840
  op = VEC_index (vn_reference_op_s, *ops, i);
841
  /* ???  If this is our usual typeof &ARRAY vs. &ARRAY[0] problem, work
842
     around it to avoid later ICEs.  */
843
  if (TREE_CODE (TREE_TYPE (TREE_OPERAND (op->op0, 0))) == ARRAY_TYPE
844
      && TREE_CODE (TREE_TYPE (TREE_TYPE (op->op0))) != ARRAY_TYPE)
845
    {
846
      vn_reference_op_s aref;
847
      tree dom;
848
      aref.type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (op->op0)));
849
      aref.opcode = ARRAY_REF;
850
      aref.op0 = integer_zero_node;
851
      if ((dom = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (op->op0, 0))))
852
          && TYPE_MIN_VALUE (dom))
853
        aref.op0 = TYPE_MIN_VALUE (dom);
854
      aref.op1 = aref.op0;
855
      aref.op2 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (op->op0)));
856
      VEC_safe_push (vn_reference_op_s, heap, mem, &aref);
857
    }
858
  copy_reference_ops_from_ref (TREE_OPERAND (op->op0, 0), &mem);
859
 
860
  /* Do the replacement - we should have at least one op in mem now.  */
861
  if (VEC_length (vn_reference_op_s, mem) == 1)
862
    {
863
      VEC_replace (vn_reference_op_s, *ops, i - 1,
864
                   VEC_index (vn_reference_op_s, mem, 0));
865
      VEC_ordered_remove (vn_reference_op_s, *ops, i);
866
      i--;
867
    }
868
  else if (VEC_length (vn_reference_op_s, mem) == 2)
869
    {
870
      VEC_replace (vn_reference_op_s, *ops, i - 1,
871
                   VEC_index (vn_reference_op_s, mem, 0));
872
      VEC_replace (vn_reference_op_s, *ops, i,
873
                   VEC_index (vn_reference_op_s, mem, 1));
874
    }
875
  else if (VEC_length (vn_reference_op_s, mem) > 2)
876
    {
877
      VEC_replace (vn_reference_op_s, *ops, i - 1,
878
                   VEC_index (vn_reference_op_s, mem, 0));
879
      VEC_replace (vn_reference_op_s, *ops, i,
880
                   VEC_index (vn_reference_op_s, mem, 1));
881
      /* ???  There is no VEC_splice.  */
882
      for (j = 2; VEC_iterate (vn_reference_op_s, mem, j, op); j++)
883
        VEC_safe_insert (vn_reference_op_s, heap, *ops, ++i, op);
884
    }
885
  else
886
    gcc_unreachable ();
887
 
888
  VEC_free (vn_reference_op_s, heap, mem);
889
  *i_p = i;
890
}
891
 
892
/* Transform any SSA_NAME's in a vector of vn_reference_op_s
893
   structures into their value numbers.  This is done in-place, and
894
   the vector passed in is returned.  */
895
 
896
static VEC (vn_reference_op_s, heap) *
897
valueize_refs (VEC (vn_reference_op_s, heap) *orig)
898
{
899
  vn_reference_op_t vro;
900
  unsigned int i;
901
 
902
  for (i = 0; VEC_iterate (vn_reference_op_s, orig, i, vro); i++)
903
    {
904
      if (vro->opcode == SSA_NAME
905
          || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
906
        {
907
          vro->op0 = SSA_VAL (vro->op0);
908
          /* If it transforms from an SSA_NAME to a constant, update
909
             the opcode.  */
910
          if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
911
            vro->opcode = TREE_CODE (vro->op0);
912
          /* If it transforms from an SSA_NAME to an address, fold with
913
             a preceding indirect reference.  */
914
          if (i > 0 && TREE_CODE (vro->op0) == ADDR_EXPR
915
              && VEC_index (vn_reference_op_s,
916
                            orig, i - 1)->opcode == INDIRECT_REF)
917
            {
918
              vn_reference_fold_indirect (&orig, &i);
919
              continue;
920
            }
921
        }
922
      if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
923
        vro->op1 = SSA_VAL (vro->op1);
924
      if (vro->op2 && TREE_CODE (vro->op2) == SSA_NAME)
925
        vro->op2 = SSA_VAL (vro->op2);
926
    }
927
 
928
  return orig;
929
}
930
 
931
static VEC(vn_reference_op_s, heap) *shared_lookup_references;
932
 
933
/* Create a vector of vn_reference_op_s structures from REF, a
934
   REFERENCE_CLASS_P tree.  The vector is shared among all callers of
935
   this function.  */
936
 
937
static VEC(vn_reference_op_s, heap) *
938
valueize_shared_reference_ops_from_ref (tree ref)
939
{
940
  if (!ref)
941
    return NULL;
942
  VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
943
  copy_reference_ops_from_ref (ref, &shared_lookup_references);
944
  shared_lookup_references = valueize_refs (shared_lookup_references);
945
  return shared_lookup_references;
946
}
947
 
948
/* Create a vector of vn_reference_op_s structures from CALL, a
949
   call statement.  The vector is shared among all callers of
950
   this function.  */
951
 
952
static VEC(vn_reference_op_s, heap) *
953
valueize_shared_reference_ops_from_call (gimple call)
954
{
955
  if (!call)
956
    return NULL;
957
  VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
958
  copy_reference_ops_from_call (call, &shared_lookup_references);
959
  shared_lookup_references = valueize_refs (shared_lookup_references);
960
  return shared_lookup_references;
961
}
962
 
963
/* Lookup a SCCVN reference operation VR in the current hash table.
964
   Returns the resulting value number if it exists in the hash table,
965
   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
966
   vn_reference_t stored in the hashtable if something is found.  */
967
 
968
static tree
969
vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
970
{
971
  void **slot;
972
  hashval_t hash;
973
 
974
  hash = vr->hashcode;
975
  slot = htab_find_slot_with_hash (current_info->references, vr,
976
                                   hash, NO_INSERT);
977
  if (!slot && current_info == optimistic_info)
978
    slot = htab_find_slot_with_hash (valid_info->references, vr,
979
                                     hash, NO_INSERT);
980
  if (slot)
981
    {
982
      if (vnresult)
983
        *vnresult = (vn_reference_t)*slot;
984
      return ((vn_reference_t)*slot)->result;
985
    }
986
 
987
  return NULL_TREE;
988
}
989
 
990
static tree *last_vuse_ptr;
991
 
992
/* Callback for walk_non_aliased_vuses.  Adjusts the vn_reference_t VR_
993
   with the current VUSE and performs the expression lookup.  */
994
 
995
static void *
996
vn_reference_lookup_2 (ao_ref *op ATTRIBUTE_UNUSED, tree vuse, void *vr_)
997
{
998
  vn_reference_t vr = (vn_reference_t)vr_;
999
  void **slot;
1000
  hashval_t hash;
1001
 
1002
  if (last_vuse_ptr)
1003
    *last_vuse_ptr = vuse;
1004
 
1005
  /* Fixup vuse and hash.  */
1006
  if (vr->vuse)
1007
    vr->hashcode = vr->hashcode - SSA_NAME_VERSION (vr->vuse);
1008
  vr->vuse = SSA_VAL (vuse);
1009
  if (vr->vuse)
1010
    vr->hashcode = vr->hashcode + SSA_NAME_VERSION (vr->vuse);
1011
 
1012
  hash = vr->hashcode;
1013
  slot = htab_find_slot_with_hash (current_info->references, vr,
1014
                                   hash, NO_INSERT);
1015
  if (!slot && current_info == optimistic_info)
1016
    slot = htab_find_slot_with_hash (valid_info->references, vr,
1017
                                     hash, NO_INSERT);
1018
  if (slot)
1019
    return *slot;
1020
 
1021
  return NULL;
1022
}
1023
 
1024
/* Callback for walk_non_aliased_vuses.  Tries to perform a lookup
1025
   from the statement defining VUSE and if not successful tries to
1026
   translate *REFP and VR_ through an aggregate copy at the defintion
1027
   of VUSE.  */
1028
 
1029
static void *
1030
vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *vr_)
1031
{
1032
  vn_reference_t vr = (vn_reference_t)vr_;
1033
  gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1034
  tree fndecl;
1035
  tree base;
1036
  HOST_WIDE_INT offset, maxsize;
1037
 
1038
  base = ao_ref_base (ref);
1039
  offset = ref->offset;
1040
  maxsize = ref->max_size;
1041
 
1042
  /* If we cannot constrain the size of the reference we cannot
1043
     test if anything kills it.  */
1044
  if (maxsize == -1)
1045
    return (void *)-1;
1046
 
1047
  /* def_stmt may-defs *ref.  See if we can derive a value for *ref
1048
     from that defintion.
1049
     1) Memset.  */
1050
  if (is_gimple_reg_type (vr->type)
1051
      && is_gimple_call (def_stmt)
1052
      && (fndecl = gimple_call_fndecl (def_stmt))
1053
      && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
1054
      && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
1055
      && integer_zerop (gimple_call_arg (def_stmt, 1))
1056
      && host_integerp (gimple_call_arg (def_stmt, 2), 1)
1057
      && TREE_CODE (gimple_call_arg (def_stmt, 0)) == ADDR_EXPR)
1058
    {
1059
      tree ref2 = TREE_OPERAND (gimple_call_arg (def_stmt, 0), 0);
1060
      tree base2;
1061
      HOST_WIDE_INT offset2, size2, maxsize2;
1062
      base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &maxsize2);
1063
      size2 = TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2)) * 8;
1064
      if ((unsigned HOST_WIDE_INT)size2 / 8
1065
          == TREE_INT_CST_LOW (gimple_call_arg (def_stmt, 2))
1066
          && operand_equal_p (base, base2, 0)
1067
          && offset2 <= offset
1068
          && offset2 + size2 >= offset + maxsize)
1069
        {
1070
          tree val = fold_convert (vr->type, integer_zero_node);
1071
          unsigned int value_id = get_or_alloc_constant_value_id (val);
1072
          return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1073
                                             VEC_copy (vn_reference_op_s,
1074
                                                       heap, vr->operands),
1075
                                             val, value_id);
1076
        }
1077
    }
1078
 
1079
  /* 2) Assignment from an empty CONSTRUCTOR.  */
1080
  else if (is_gimple_reg_type (vr->type)
1081
           && gimple_assign_single_p (def_stmt)
1082
           && gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR
1083
           && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (def_stmt)) == 0)
1084
    {
1085
      tree base2;
1086
      HOST_WIDE_INT offset2, size2, maxsize2;
1087
      base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1088
                                       &offset2, &size2, &maxsize2);
1089
      if (operand_equal_p (base, base2, 0)
1090
          && offset2 <= offset
1091
          && offset2 + size2 >= offset + maxsize)
1092
        {
1093
          tree val = fold_convert (vr->type, integer_zero_node);
1094
          unsigned int value_id = get_or_alloc_constant_value_id (val);
1095
          return vn_reference_insert_pieces (vuse, vr->set, vr->type,
1096
                                             VEC_copy (vn_reference_op_s,
1097
                                                       heap, vr->operands),
1098
                                             val, value_id);
1099
        }
1100
    }
1101
 
1102
  /* For aggregate copies translate the reference through them if
1103
     the copy kills ref.  */
1104
  else if (gimple_assign_single_p (def_stmt)
1105
           && (DECL_P (gimple_assign_rhs1 (def_stmt))
1106
               || INDIRECT_REF_P (gimple_assign_rhs1 (def_stmt))
1107
               || handled_component_p (gimple_assign_rhs1 (def_stmt))))
1108
    {
1109
      tree base2;
1110
      HOST_WIDE_INT offset2, size2, maxsize2;
1111
      int i, j;
1112
      VEC (vn_reference_op_s, heap) *lhs = NULL, *rhs = NULL;
1113
      vn_reference_op_t vro;
1114
      ao_ref r;
1115
 
1116
      /* See if the assignment kills REF.  */
1117
      base2 = get_ref_base_and_extent (gimple_assign_lhs (def_stmt),
1118
                                       &offset2, &size2, &maxsize2);
1119
      if (!operand_equal_p (base, base2, 0)
1120
          || offset2 > offset
1121
          || offset2 + size2 < offset + maxsize)
1122
        return (void *)-1;
1123
 
1124
      /* Find the common base of ref and the lhs.  */
1125
      copy_reference_ops_from_ref (gimple_assign_lhs (def_stmt), &lhs);
1126
      i = VEC_length (vn_reference_op_s, vr->operands) - 1;
1127
      j = VEC_length (vn_reference_op_s, lhs) - 1;
1128
      while (j >= 0 && i >= 0
1129
             && vn_reference_op_eq (VEC_index (vn_reference_op_s,
1130
                                               vr->operands, i),
1131
                                    VEC_index (vn_reference_op_s, lhs, j)))
1132
        {
1133
          i--;
1134
          j--;
1135
        }
1136
 
1137
      VEC_free (vn_reference_op_s, heap, lhs);
1138
      /* i now points to the first additional op.
1139
         ???  LHS may not be completely contained in VR, one or more
1140
         VIEW_CONVERT_EXPRs could be in its way.  We could at least
1141
         try handling outermost VIEW_CONVERT_EXPRs.  */
1142
      if (j != -1)
1143
        return (void *)-1;
1144
 
1145
      /* Now re-write REF to be based on the rhs of the assignment.  */
1146
      copy_reference_ops_from_ref (gimple_assign_rhs1 (def_stmt), &rhs);
1147
      /* We need to pre-pend vr->operands[0..i] to rhs.  */
1148
      if (i + 1 + VEC_length (vn_reference_op_s, rhs)
1149
          > VEC_length (vn_reference_op_s, vr->operands))
1150
        {
1151
          VEC (vn_reference_op_s, heap) *old = vr->operands;
1152
          VEC_safe_grow (vn_reference_op_s, heap, vr->operands,
1153
                         i + 1 + VEC_length (vn_reference_op_s, rhs));
1154
          if (old == shared_lookup_references
1155
              && vr->operands != old)
1156
            shared_lookup_references = NULL;
1157
        }
1158
      else
1159
        VEC_truncate (vn_reference_op_s, vr->operands,
1160
                      i + 1 + VEC_length (vn_reference_op_s, rhs));
1161
      for (j = 0; VEC_iterate (vn_reference_op_s, rhs, j, vro); ++j)
1162
        VEC_replace (vn_reference_op_s, vr->operands, i + 1 + j, vro);
1163
      VEC_free (vn_reference_op_s, heap, rhs);
1164
      vr->hashcode = vn_reference_compute_hash (vr);
1165
 
1166
      /* Adjust *ref from the new operands.  */
1167
      if (!ao_ref_init_from_vn_reference (&r, vr->set, vr->type, vr->operands))
1168
        return (void *)-1;
1169
      /* This can happen with bitfields.  */
1170
      if (ref->size != r.size)
1171
        return (void *)-1;
1172
      *ref = r;
1173
 
1174
      /* Do not update last seen VUSE after translating.  */
1175
      last_vuse_ptr = NULL;
1176
 
1177
      /* Keep looking for the adjusted *REF / VR pair.  */
1178
      return NULL;
1179
    }
1180
 
1181
  /* Bail out and stop walking.  */
1182
  return (void *)-1;
1183
}
1184
 
1185
/* Lookup a reference operation by it's parts, in the current hash table.
1186
   Returns the resulting value number if it exists in the hash table,
1187
   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
1188
   vn_reference_t stored in the hashtable if something is found.  */
1189
 
1190
tree
1191
vn_reference_lookup_pieces (tree vuse, alias_set_type set, tree type,
1192
                            VEC (vn_reference_op_s, heap) *operands,
1193
                            vn_reference_t *vnresult, bool maywalk)
1194
{
1195
  struct vn_reference_s vr1;
1196
  vn_reference_t tmp;
1197
 
1198
  if (!vnresult)
1199
    vnresult = &tmp;
1200
  *vnresult = NULL;
1201
 
1202
  vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1203
  VEC_truncate (vn_reference_op_s, shared_lookup_references, 0);
1204
  VEC_safe_grow (vn_reference_op_s, heap, shared_lookup_references,
1205
                 VEC_length (vn_reference_op_s, operands));
1206
  memcpy (VEC_address (vn_reference_op_s, shared_lookup_references),
1207
          VEC_address (vn_reference_op_s, operands),
1208
          sizeof (vn_reference_op_s)
1209
          * VEC_length (vn_reference_op_s, operands));
1210
  vr1.operands = operands = shared_lookup_references
1211
    = valueize_refs (shared_lookup_references);
1212
  vr1.type = type;
1213
  vr1.set = set;
1214
  vr1.hashcode = vn_reference_compute_hash (&vr1);
1215
  vn_reference_lookup_1 (&vr1, vnresult);
1216
 
1217
  if (!*vnresult
1218
      && maywalk
1219
      && vr1.vuse)
1220
    {
1221
      ao_ref r;
1222
      if (ao_ref_init_from_vn_reference (&r, set, type, vr1.operands))
1223
        *vnresult =
1224
          (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1225
                                                  vn_reference_lookup_2,
1226
                                                  vn_reference_lookup_3, &vr1);
1227
      if (vr1.operands != operands)
1228
        VEC_free (vn_reference_op_s, heap, vr1.operands);
1229
    }
1230
 
1231
  if (*vnresult)
1232
     return (*vnresult)->result;
1233
 
1234
  return NULL_TREE;
1235
}
1236
 
1237
/* Lookup OP in the current hash table, and return the resulting value
1238
   number if it exists in the hash table.  Return NULL_TREE if it does
1239
   not exist in the hash table or if the result field of the structure
1240
   was NULL..  VNRESULT will be filled in with the vn_reference_t
1241
   stored in the hashtable if one exists.  */
1242
 
1243
tree
1244
vn_reference_lookup (tree op, tree vuse, bool maywalk,
1245
                     vn_reference_t *vnresult)
1246
{
1247
  VEC (vn_reference_op_s, heap) *operands;
1248
  struct vn_reference_s vr1;
1249
 
1250
  if (vnresult)
1251
    *vnresult = NULL;
1252
 
1253
  vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1254
  vr1.operands = operands = valueize_shared_reference_ops_from_ref (op);
1255
  vr1.type = TREE_TYPE (op);
1256
  vr1.set = get_alias_set (op);
1257
  vr1.hashcode = vn_reference_compute_hash (&vr1);
1258
 
1259
  if (maywalk
1260
      && vr1.vuse)
1261
    {
1262
      vn_reference_t wvnresult;
1263
      ao_ref r;
1264
      ao_ref_init (&r, op);
1265
      wvnresult =
1266
        (vn_reference_t)walk_non_aliased_vuses (&r, vr1.vuse,
1267
                                                vn_reference_lookup_2,
1268
                                                vn_reference_lookup_3, &vr1);
1269
      if (vr1.operands != operands)
1270
        VEC_free (vn_reference_op_s, heap, vr1.operands);
1271
      if (wvnresult)
1272
        {
1273
          if (vnresult)
1274
            *vnresult = wvnresult;
1275
          return wvnresult->result;
1276
        }
1277
 
1278
      return NULL_TREE;
1279
    }
1280
 
1281
  return vn_reference_lookup_1 (&vr1, vnresult);
1282
}
1283
 
1284
 
1285
/* Insert OP into the current hash table with a value number of
1286
   RESULT, and return the resulting reference structure we created.  */
1287
 
1288
vn_reference_t
1289
vn_reference_insert (tree op, tree result, tree vuse)
1290
{
1291
  void **slot;
1292
  vn_reference_t vr1;
1293
 
1294
  vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1295
  if (TREE_CODE (result) == SSA_NAME)
1296
    vr1->value_id = VN_INFO (result)->value_id;
1297
  else
1298
    vr1->value_id = get_or_alloc_constant_value_id (result);
1299
  vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1300
  vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
1301
  vr1->type = TREE_TYPE (op);
1302
  vr1->set = get_alias_set (op);
1303
  vr1->hashcode = vn_reference_compute_hash (vr1);
1304
  vr1->result = TREE_CODE (result) == SSA_NAME ? SSA_VAL (result) : result;
1305
 
1306
  slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1307
                                   INSERT);
1308
 
1309
  /* Because we lookup stores using vuses, and value number failures
1310
     using the vdefs (see visit_reference_op_store for how and why),
1311
     it's possible that on failure we may try to insert an already
1312
     inserted store.  This is not wrong, there is no ssa name for a
1313
     store that we could use as a differentiator anyway.  Thus, unlike
1314
     the other lookup functions, you cannot gcc_assert (!*slot)
1315
     here.  */
1316
 
1317
  /* But free the old slot in case of a collision.  */
1318
  if (*slot)
1319
    free_reference (*slot);
1320
 
1321
  *slot = vr1;
1322
  return vr1;
1323
}
1324
 
1325
/* Insert a reference by it's pieces into the current hash table with
1326
   a value number of RESULT.  Return the resulting reference
1327
   structure we created.  */
1328
 
1329
vn_reference_t
1330
vn_reference_insert_pieces (tree vuse, alias_set_type set, tree type,
1331
                            VEC (vn_reference_op_s, heap) *operands,
1332
                            tree result, unsigned int value_id)
1333
 
1334
{
1335
  void **slot;
1336
  vn_reference_t vr1;
1337
 
1338
  vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
1339
  vr1->value_id = value_id;
1340
  vr1->vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1341
  vr1->operands = valueize_refs (operands);
1342
  vr1->type = type;
1343
  vr1->set = set;
1344
  vr1->hashcode = vn_reference_compute_hash (vr1);
1345
  if (result && TREE_CODE (result) == SSA_NAME)
1346
    result = SSA_VAL (result);
1347
  vr1->result = result;
1348
 
1349
  slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
1350
                                   INSERT);
1351
 
1352
  /* At this point we should have all the things inserted that we have
1353
     seen before, and we should never try inserting something that
1354
     already exists.  */
1355
  gcc_assert (!*slot);
1356
  if (*slot)
1357
    free_reference (*slot);
1358
 
1359
  *slot = vr1;
1360
  return vr1;
1361
}
1362
 
1363
/* Compute and return the hash value for nary operation VBO1.  */
1364
 
1365
hashval_t
1366
vn_nary_op_compute_hash (const vn_nary_op_t vno1)
1367
{
1368
  hashval_t hash;
1369
  unsigned i;
1370
 
1371
  for (i = 0; i < vno1->length; ++i)
1372
    if (TREE_CODE (vno1->op[i]) == SSA_NAME)
1373
      vno1->op[i] = SSA_VAL (vno1->op[i]);
1374
 
1375
  if (vno1->length == 2
1376
      && commutative_tree_code (vno1->opcode)
1377
      && tree_swap_operands_p (vno1->op[0], vno1->op[1], false))
1378
    {
1379
      tree temp = vno1->op[0];
1380
      vno1->op[0] = vno1->op[1];
1381
      vno1->op[1] = temp;
1382
    }
1383
 
1384
  hash = iterative_hash_hashval_t (vno1->opcode, 0);
1385
  for (i = 0; i < vno1->length; ++i)
1386
    hash = iterative_hash_expr (vno1->op[i], hash);
1387
 
1388
  return hash;
1389
}
1390
 
1391
/* Return the computed hashcode for nary operation P1.  */
1392
 
1393
static hashval_t
1394
vn_nary_op_hash (const void *p1)
1395
{
1396
  const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1397
  return vno1->hashcode;
1398
}
1399
 
1400
/* Compare nary operations P1 and P2 and return true if they are
1401
   equivalent.  */
1402
 
1403
int
1404
vn_nary_op_eq (const void *p1, const void *p2)
1405
{
1406
  const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
1407
  const_vn_nary_op_t const vno2 = (const_vn_nary_op_t) p2;
1408
  unsigned i;
1409
 
1410
  if (vno1->hashcode != vno2->hashcode)
1411
    return false;
1412
 
1413
  if (vno1->opcode != vno2->opcode
1414
      || !types_compatible_p (vno1->type, vno2->type))
1415
    return false;
1416
 
1417
  for (i = 0; i < vno1->length; ++i)
1418
    if (!expressions_equal_p (vno1->op[i], vno2->op[i]))
1419
      return false;
1420
 
1421
  return true;
1422
}
1423
 
1424
/* Lookup a n-ary operation by its pieces and return the resulting value
1425
   number if it exists in the hash table.  Return NULL_TREE if it does
1426
   not exist in the hash table or if the result field of the operation
1427
   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1428
   if it exists.  */
1429
 
1430
tree
1431
vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
1432
                          tree type, tree op0, tree op1, tree op2,
1433
                          tree op3, vn_nary_op_t *vnresult)
1434
{
1435
  void **slot;
1436
  struct vn_nary_op_s vno1;
1437
  if (vnresult)
1438
    *vnresult = NULL;
1439
  vno1.opcode = code;
1440
  vno1.length = length;
1441
  vno1.type = type;
1442
  vno1.op[0] = op0;
1443
  vno1.op[1] = op1;
1444
  vno1.op[2] = op2;
1445
  vno1.op[3] = op3;
1446
  vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1447
  slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1448
                                   NO_INSERT);
1449
  if (!slot && current_info == optimistic_info)
1450
    slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1451
                                     NO_INSERT);
1452
  if (!slot)
1453
    return NULL_TREE;
1454
  if (vnresult)
1455
    *vnresult = (vn_nary_op_t)*slot;
1456
  return ((vn_nary_op_t)*slot)->result;
1457
}
1458
 
1459
/* Lookup OP in the current hash table, and return the resulting value
1460
   number if it exists in the hash table.  Return NULL_TREE if it does
1461
   not exist in the hash table or if the result field of the operation
1462
   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
1463
   if it exists.  */
1464
 
1465
tree
1466
vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
1467
{
1468
  void **slot;
1469
  struct vn_nary_op_s vno1;
1470
  unsigned i;
1471
 
1472
  if (vnresult)
1473
    *vnresult = NULL;
1474
  vno1.opcode = TREE_CODE (op);
1475
  vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
1476
  vno1.type = TREE_TYPE (op);
1477
  for (i = 0; i < vno1.length; ++i)
1478
    vno1.op[i] = TREE_OPERAND (op, i);
1479
  vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1480
  slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1481
                                   NO_INSERT);
1482
  if (!slot && current_info == optimistic_info)
1483
    slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1484
                                     NO_INSERT);
1485
  if (!slot)
1486
    return NULL_TREE;
1487
  if (vnresult)
1488
    *vnresult = (vn_nary_op_t)*slot;
1489
  return ((vn_nary_op_t)*slot)->result;
1490
}
1491
 
1492
/* Lookup the rhs of STMT in the current hash table, and return the resulting
1493
   value number if it exists in the hash table.  Return NULL_TREE if
1494
   it does not exist in the hash table.  VNRESULT will contain the
1495
   vn_nary_op_t from the hashtable if it exists.  */
1496
 
1497
tree
1498
vn_nary_op_lookup_stmt (gimple stmt, vn_nary_op_t *vnresult)
1499
{
1500
  void **slot;
1501
  struct vn_nary_op_s vno1;
1502
  unsigned i;
1503
 
1504
  if (vnresult)
1505
    *vnresult = NULL;
1506
  vno1.opcode = gimple_assign_rhs_code (stmt);
1507
  vno1.length = gimple_num_ops (stmt) - 1;
1508
  vno1.type = gimple_expr_type (stmt);
1509
  for (i = 0; i < vno1.length; ++i)
1510
    vno1.op[i] = gimple_op (stmt, i + 1);
1511
  if (vno1.opcode == REALPART_EXPR
1512
      || vno1.opcode == IMAGPART_EXPR
1513
      || vno1.opcode == VIEW_CONVERT_EXPR)
1514
    vno1.op[0] = TREE_OPERAND (vno1.op[0], 0);
1515
  vno1.hashcode = vn_nary_op_compute_hash (&vno1);
1516
  slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
1517
                                   NO_INSERT);
1518
  if (!slot && current_info == optimistic_info)
1519
    slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
1520
                                     NO_INSERT);
1521
  if (!slot)
1522
    return NULL_TREE;
1523
  if (vnresult)
1524
    *vnresult = (vn_nary_op_t)*slot;
1525
  return ((vn_nary_op_t)*slot)->result;
1526
}
1527
 
1528
/* Insert a n-ary operation into the current hash table using it's
1529
   pieces.  Return the vn_nary_op_t structure we created and put in
1530
   the hashtable.  */
1531
 
1532
vn_nary_op_t
1533
vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
1534
                          tree type, tree op0,
1535
                          tree op1, tree op2, tree op3,
1536
                          tree result,
1537
                          unsigned int value_id)
1538
{
1539
  void **slot;
1540
  vn_nary_op_t vno1;
1541
 
1542
  vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1543
                                       (sizeof (struct vn_nary_op_s)
1544
                                        - sizeof (tree) * (4 - length)));
1545
  vno1->value_id = value_id;
1546
  vno1->opcode = code;
1547
  vno1->length = length;
1548
  vno1->type = type;
1549
  if (length >= 1)
1550
    vno1->op[0] = op0;
1551
  if (length >= 2)
1552
    vno1->op[1] = op1;
1553
  if (length >= 3)
1554
    vno1->op[2] = op2;
1555
  if (length >= 4)
1556
    vno1->op[3] = op3;
1557
  vno1->result = result;
1558
  vno1->hashcode = vn_nary_op_compute_hash (vno1);
1559
  slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1560
                                   INSERT);
1561
  gcc_assert (!*slot);
1562
 
1563
  *slot = vno1;
1564
  return vno1;
1565
 
1566
}
1567
 
1568
/* Insert OP into the current hash table with a value number of
1569
   RESULT.  Return the vn_nary_op_t structure we created and put in
1570
   the hashtable.  */
1571
 
1572
vn_nary_op_t
1573
vn_nary_op_insert (tree op, tree result)
1574
{
1575
  unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
1576
  void **slot;
1577
  vn_nary_op_t vno1;
1578
  unsigned i;
1579
 
1580
  vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1581
                        (sizeof (struct vn_nary_op_s)
1582
                         - sizeof (tree) * (4 - length)));
1583
  vno1->value_id = VN_INFO (result)->value_id;
1584
  vno1->opcode = TREE_CODE (op);
1585
  vno1->length = length;
1586
  vno1->type = TREE_TYPE (op);
1587
  for (i = 0; i < vno1->length; ++i)
1588
    vno1->op[i] = TREE_OPERAND (op, i);
1589
  vno1->result = result;
1590
  vno1->hashcode = vn_nary_op_compute_hash (vno1);
1591
  slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1592
                                   INSERT);
1593
  gcc_assert (!*slot);
1594
 
1595
  *slot = vno1;
1596
  return vno1;
1597
}
1598
 
1599
/* Insert the rhs of STMT into the current hash table with a value number of
1600
   RESULT.  */
1601
 
1602
vn_nary_op_t
1603
vn_nary_op_insert_stmt (gimple stmt, tree result)
1604
{
1605
  unsigned length = gimple_num_ops (stmt) - 1;
1606
  void **slot;
1607
  vn_nary_op_t vno1;
1608
  unsigned i;
1609
 
1610
  vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
1611
                                       (sizeof (struct vn_nary_op_s)
1612
                                        - sizeof (tree) * (4 - length)));
1613
  vno1->value_id = VN_INFO (result)->value_id;
1614
  vno1->opcode = gimple_assign_rhs_code (stmt);
1615
  vno1->length = length;
1616
  vno1->type = gimple_expr_type (stmt);
1617
  for (i = 0; i < vno1->length; ++i)
1618
    vno1->op[i] = gimple_op (stmt, i + 1);
1619
  if (vno1->opcode == REALPART_EXPR
1620
      || vno1->opcode == IMAGPART_EXPR
1621
      || vno1->opcode == VIEW_CONVERT_EXPR)
1622
    vno1->op[0] = TREE_OPERAND (vno1->op[0], 0);
1623
  vno1->result = result;
1624
  vno1->hashcode = vn_nary_op_compute_hash (vno1);
1625
  slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
1626
                                   INSERT);
1627
  gcc_assert (!*slot);
1628
 
1629
  *slot = vno1;
1630
  return vno1;
1631
}
1632
 
1633
/* Compute a hashcode for PHI operation VP1 and return it.  */
1634
 
1635
static inline hashval_t
1636
vn_phi_compute_hash (vn_phi_t vp1)
1637
{
1638
  hashval_t result;
1639
  int i;
1640
  tree phi1op;
1641
  tree type;
1642
 
1643
  result = vp1->block->index;
1644
 
1645
  /* If all PHI arguments are constants we need to distinguish
1646
     the PHI node via its type.  */
1647
  type = TREE_TYPE (VEC_index (tree, vp1->phiargs, 0));
1648
  result += (INTEGRAL_TYPE_P (type)
1649
             + (INTEGRAL_TYPE_P (type)
1650
                ? TYPE_PRECISION (type) + TYPE_UNSIGNED (type) : 0));
1651
 
1652
  for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
1653
    {
1654
      if (phi1op == VN_TOP)
1655
        continue;
1656
      result = iterative_hash_expr (phi1op, result);
1657
    }
1658
 
1659
  return result;
1660
}
1661
 
1662
/* Return the computed hashcode for phi operation P1.  */
1663
 
1664
static hashval_t
1665
vn_phi_hash (const void *p1)
1666
{
1667
  const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1668
  return vp1->hashcode;
1669
}
1670
 
1671
/* Compare two phi entries for equality, ignoring VN_TOP arguments.  */
1672
 
1673
static int
1674
vn_phi_eq (const void *p1, const void *p2)
1675
{
1676
  const_vn_phi_t const vp1 = (const_vn_phi_t) p1;
1677
  const_vn_phi_t const vp2 = (const_vn_phi_t) p2;
1678
 
1679
  if (vp1->hashcode != vp2->hashcode)
1680
    return false;
1681
 
1682
  if (vp1->block == vp2->block)
1683
    {
1684
      int i;
1685
      tree phi1op;
1686
 
1687
      /* If the PHI nodes do not have compatible types
1688
         they are not the same.  */
1689
      if (!types_compatible_p (TREE_TYPE (VEC_index (tree, vp1->phiargs, 0)),
1690
                               TREE_TYPE (VEC_index (tree, vp2->phiargs, 0))))
1691
        return false;
1692
 
1693
      /* Any phi in the same block will have it's arguments in the
1694
         same edge order, because of how we store phi nodes.  */
1695
      for (i = 0; VEC_iterate (tree, vp1->phiargs, i, phi1op); i++)
1696
        {
1697
          tree phi2op = VEC_index (tree, vp2->phiargs, i);
1698
          if (phi1op == VN_TOP || phi2op == VN_TOP)
1699
            continue;
1700
          if (!expressions_equal_p (phi1op, phi2op))
1701
            return false;
1702
        }
1703
      return true;
1704
    }
1705
  return false;
1706
}
1707
 
1708
static VEC(tree, heap) *shared_lookup_phiargs;
1709
 
1710
/* Lookup PHI in the current hash table, and return the resulting
1711
   value number if it exists in the hash table.  Return NULL_TREE if
1712
   it does not exist in the hash table. */
1713
 
1714
static tree
1715
vn_phi_lookup (gimple phi)
1716
{
1717
  void **slot;
1718
  struct vn_phi_s vp1;
1719
  unsigned i;
1720
 
1721
  VEC_truncate (tree, shared_lookup_phiargs, 0);
1722
 
1723
  /* Canonicalize the SSA_NAME's to their value number.  */
1724
  for (i = 0; i < gimple_phi_num_args (phi); i++)
1725
    {
1726
      tree def = PHI_ARG_DEF (phi, i);
1727
      def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1728
      VEC_safe_push (tree, heap, shared_lookup_phiargs, def);
1729
    }
1730
  vp1.phiargs = shared_lookup_phiargs;
1731
  vp1.block = gimple_bb (phi);
1732
  vp1.hashcode = vn_phi_compute_hash (&vp1);
1733
  slot = htab_find_slot_with_hash (current_info->phis, &vp1, vp1.hashcode,
1734
                                   NO_INSERT);
1735
  if (!slot && current_info == optimistic_info)
1736
    slot = htab_find_slot_with_hash (valid_info->phis, &vp1, vp1.hashcode,
1737
                                     NO_INSERT);
1738
  if (!slot)
1739
    return NULL_TREE;
1740
  return ((vn_phi_t)*slot)->result;
1741
}
1742
 
1743
/* Insert PHI into the current hash table with a value number of
1744
   RESULT.  */
1745
 
1746
static vn_phi_t
1747
vn_phi_insert (gimple phi, tree result)
1748
{
1749
  void **slot;
1750
  vn_phi_t vp1 = (vn_phi_t) pool_alloc (current_info->phis_pool);
1751
  unsigned i;
1752
  VEC (tree, heap) *args = NULL;
1753
 
1754
  /* Canonicalize the SSA_NAME's to their value number.  */
1755
  for (i = 0; i < gimple_phi_num_args (phi); i++)
1756
    {
1757
      tree def = PHI_ARG_DEF (phi, i);
1758
      def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
1759
      VEC_safe_push (tree, heap, args, def);
1760
    }
1761
  vp1->value_id = VN_INFO (result)->value_id;
1762
  vp1->phiargs = args;
1763
  vp1->block = gimple_bb (phi);
1764
  vp1->result = result;
1765
  vp1->hashcode = vn_phi_compute_hash (vp1);
1766
 
1767
  slot = htab_find_slot_with_hash (current_info->phis, vp1, vp1->hashcode,
1768
                                   INSERT);
1769
 
1770
  /* Because we iterate over phi operations more than once, it's
1771
     possible the slot might already exist here, hence no assert.*/
1772
  *slot = vp1;
1773
  return vp1;
1774
}
1775
 
1776
 
1777
/* Print set of components in strongly connected component SCC to OUT. */
1778
 
1779
static void
1780
print_scc (FILE *out, VEC (tree, heap) *scc)
1781
{
1782
  tree var;
1783
  unsigned int i;
1784
 
1785
  fprintf (out, "SCC consists of: ");
1786
  for (i = 0; VEC_iterate (tree, scc, i, var); i++)
1787
    {
1788
      print_generic_expr (out, var, 0);
1789
      fprintf (out, " ");
1790
    }
1791
  fprintf (out, "\n");
1792
}
1793
 
1794
/* Set the value number of FROM to TO, return true if it has changed
1795
   as a result.  */
1796
 
1797
static inline bool
1798
set_ssa_val_to (tree from, tree to)
1799
{
1800
  tree currval;
1801
 
1802
  if (from != to
1803
      && TREE_CODE (to) == SSA_NAME
1804
      && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (to))
1805
    to = from;
1806
 
1807
  /* The only thing we allow as value numbers are VN_TOP, ssa_names
1808
     and invariants.  So assert that here.  */
1809
  gcc_assert (to != NULL_TREE
1810
              && (to == VN_TOP
1811
                  || TREE_CODE (to) == SSA_NAME
1812
                  || is_gimple_min_invariant (to)));
1813
 
1814
  if (dump_file && (dump_flags & TDF_DETAILS))
1815
    {
1816
      fprintf (dump_file, "Setting value number of ");
1817
      print_generic_expr (dump_file, from, 0);
1818
      fprintf (dump_file, " to ");
1819
      print_generic_expr (dump_file, to, 0);
1820
    }
1821
 
1822
  currval = SSA_VAL (from);
1823
 
1824
  if (currval != to  && !operand_equal_p (currval, to, OEP_PURE_SAME))
1825
    {
1826
      VN_INFO (from)->valnum = to;
1827
      if (dump_file && (dump_flags & TDF_DETAILS))
1828
        fprintf (dump_file, " (changed)\n");
1829
      return true;
1830
    }
1831
  if (dump_file && (dump_flags & TDF_DETAILS))
1832
    fprintf (dump_file, "\n");
1833
  return false;
1834
}
1835
 
1836
/* Set all definitions in STMT to value number to themselves.
1837
   Return true if a value number changed. */
1838
 
1839
static bool
1840
defs_to_varying (gimple stmt)
1841
{
1842
  bool changed = false;
1843
  ssa_op_iter iter;
1844
  def_operand_p defp;
1845
 
1846
  FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_ALL_DEFS)
1847
    {
1848
      tree def = DEF_FROM_PTR (defp);
1849
 
1850
      VN_INFO (def)->use_processed = true;
1851
      changed |= set_ssa_val_to (def, def);
1852
    }
1853
  return changed;
1854
}
1855
 
1856
static bool expr_has_constants (tree expr);
1857
static tree valueize_expr (tree expr);
1858
 
1859
/* Visit a copy between LHS and RHS, return true if the value number
1860
   changed.  */
1861
 
1862
static bool
1863
visit_copy (tree lhs, tree rhs)
1864
{
1865
  /* Follow chains of copies to their destination.  */
1866
  while (TREE_CODE (rhs) == SSA_NAME
1867
         && SSA_VAL (rhs) != rhs)
1868
    rhs = SSA_VAL (rhs);
1869
 
1870
  /* The copy may have a more interesting constant filled expression
1871
     (we don't, since we know our RHS is just an SSA name).  */
1872
  if (TREE_CODE (rhs) == SSA_NAME)
1873
    {
1874
      VN_INFO (lhs)->has_constants = VN_INFO (rhs)->has_constants;
1875
      VN_INFO (lhs)->expr = VN_INFO (rhs)->expr;
1876
    }
1877
 
1878
  return set_ssa_val_to (lhs, rhs);
1879
}
1880
 
1881
/* Visit a unary operator RHS, value number it, and return true if the
1882
   value number of LHS has changed as a result.  */
1883
 
1884
static bool
1885
visit_unary_op (tree lhs, gimple stmt)
1886
{
1887
  bool changed = false;
1888
  tree result = vn_nary_op_lookup_stmt (stmt, NULL);
1889
 
1890
  if (result)
1891
    {
1892
      changed = set_ssa_val_to (lhs, result);
1893
    }
1894
  else
1895
    {
1896
      changed = set_ssa_val_to (lhs, lhs);
1897
      vn_nary_op_insert_stmt (stmt, lhs);
1898
    }
1899
 
1900
  return changed;
1901
}
1902
 
1903
/* Visit a binary operator RHS, value number it, and return true if the
1904
   value number of LHS has changed as a result.  */
1905
 
1906
static bool
1907
visit_binary_op (tree lhs, gimple stmt)
1908
{
1909
  bool changed = false;
1910
  tree result = vn_nary_op_lookup_stmt (stmt, NULL);
1911
 
1912
  if (result)
1913
    {
1914
      changed = set_ssa_val_to (lhs, result);
1915
    }
1916
  else
1917
    {
1918
      changed = set_ssa_val_to (lhs, lhs);
1919
      vn_nary_op_insert_stmt (stmt, lhs);
1920
    }
1921
 
1922
  return changed;
1923
}
1924
 
1925
/* Visit a call STMT storing into LHS.  Return true if the value number
1926
   of the LHS has changed as a result.  */
1927
 
1928
static bool
1929
visit_reference_op_call (tree lhs, gimple stmt)
1930
{
1931
  bool changed = false;
1932
  struct vn_reference_s vr1;
1933
  tree result;
1934
  tree vuse = gimple_vuse (stmt);
1935
 
1936
  vr1.vuse = vuse ? SSA_VAL (vuse) : NULL_TREE;
1937
  vr1.operands = valueize_shared_reference_ops_from_call (stmt);
1938
  vr1.type = gimple_expr_type (stmt);
1939
  vr1.set = 0;
1940
  vr1.hashcode = vn_reference_compute_hash (&vr1);
1941
  result = vn_reference_lookup_1 (&vr1, NULL);
1942
  if (result)
1943
    {
1944
      changed = set_ssa_val_to (lhs, result);
1945
      if (TREE_CODE (result) == SSA_NAME
1946
          && VN_INFO (result)->has_constants)
1947
        VN_INFO (lhs)->has_constants = true;
1948
    }
1949
  else
1950
    {
1951
      void **slot;
1952
      vn_reference_t vr2;
1953
      changed = set_ssa_val_to (lhs, lhs);
1954
      vr2 = (vn_reference_t) pool_alloc (current_info->references_pool);
1955
      vr2->vuse = vr1.vuse;
1956
      vr2->operands = valueize_refs (create_reference_ops_from_call (stmt));
1957
      vr2->type = vr1.type;
1958
      vr2->set = vr1.set;
1959
      vr2->hashcode = vr1.hashcode;
1960
      vr2->result = lhs;
1961
      slot = htab_find_slot_with_hash (current_info->references,
1962
                                       vr2, vr2->hashcode, INSERT);
1963
      if (*slot)
1964
        free_reference (*slot);
1965
      *slot = vr2;
1966
    }
1967
 
1968
  return changed;
1969
}
1970
 
1971
/* Visit a load from a reference operator RHS, part of STMT, value number it,
1972
   and return true if the value number of the LHS has changed as a result.  */
1973
 
1974
static bool
1975
visit_reference_op_load (tree lhs, tree op, gimple stmt)
1976
{
1977
  bool changed = false;
1978
  tree last_vuse;
1979
  tree result;
1980
 
1981
  last_vuse = gimple_vuse (stmt);
1982
  last_vuse_ptr = &last_vuse;
1983
  result = vn_reference_lookup (op, gimple_vuse (stmt), true, NULL);
1984
  last_vuse_ptr = NULL;
1985
 
1986
  /* If we have a VCE, try looking up its operand as it might be stored in
1987
     a different type.  */
1988
  if (!result && TREE_CODE (op) == VIEW_CONVERT_EXPR)
1989
    result = vn_reference_lookup (TREE_OPERAND (op, 0), gimple_vuse (stmt),
1990
                                  true, NULL);
1991
 
1992
  /* We handle type-punning through unions by value-numbering based
1993
     on offset and size of the access.  Be prepared to handle a
1994
     type-mismatch here via creating a VIEW_CONVERT_EXPR.  */
1995
  if (result
1996
      && !useless_type_conversion_p (TREE_TYPE (result), TREE_TYPE (op)))
1997
    {
1998
      /* We will be setting the value number of lhs to the value number
1999
         of VIEW_CONVERT_EXPR <TREE_TYPE (result)> (result).
2000
         So first simplify and lookup this expression to see if it
2001
         is already available.  */
2002
      tree val = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), result);
2003
      if ((CONVERT_EXPR_P (val)
2004
           || TREE_CODE (val) == VIEW_CONVERT_EXPR)
2005
          && TREE_CODE (TREE_OPERAND (val, 0)) == SSA_NAME)
2006
        {
2007
          tree tem = valueize_expr (vn_get_expr_for (TREE_OPERAND (val, 0)));
2008
          if ((CONVERT_EXPR_P (tem)
2009
               || TREE_CODE (tem) == VIEW_CONVERT_EXPR)
2010
              && (tem = fold_unary_ignore_overflow (TREE_CODE (val),
2011
                                                    TREE_TYPE (val), tem)))
2012
            val = tem;
2013
        }
2014
      result = val;
2015
      if (!is_gimple_min_invariant (val)
2016
          && TREE_CODE (val) != SSA_NAME)
2017
        result = vn_nary_op_lookup (val, NULL);
2018
      /* If the expression is not yet available, value-number lhs to
2019
         a new SSA_NAME we create.  */
2020
      if (!result && may_insert)
2021
        {
2022
          result = make_ssa_name (SSA_NAME_VAR (lhs), NULL);
2023
          /* Initialize value-number information properly.  */
2024
          VN_INFO_GET (result)->valnum = result;
2025
          VN_INFO (result)->value_id = get_next_value_id ();
2026
          VN_INFO (result)->expr = val;
2027
          VN_INFO (result)->has_constants = expr_has_constants (val);
2028
          VN_INFO (result)->needs_insertion = true;
2029
          /* As all "inserted" statements are singleton SCCs, insert
2030
             to the valid table.  This is strictly needed to
2031
             avoid re-generating new value SSA_NAMEs for the same
2032
             expression during SCC iteration over and over (the
2033
             optimistic table gets cleared after each iteration).
2034
             We do not need to insert into the optimistic table, as
2035
             lookups there will fall back to the valid table.  */
2036
          if (current_info == optimistic_info)
2037
            {
2038
              current_info = valid_info;
2039
              vn_nary_op_insert (val, result);
2040
              current_info = optimistic_info;
2041
            }
2042
          else
2043
            vn_nary_op_insert (val, result);
2044
          if (dump_file && (dump_flags & TDF_DETAILS))
2045
            {
2046
              fprintf (dump_file, "Inserting name ");
2047
              print_generic_expr (dump_file, result, 0);
2048
              fprintf (dump_file, " for expression ");
2049
              print_generic_expr (dump_file, val, 0);
2050
              fprintf (dump_file, "\n");
2051
            }
2052
        }
2053
    }
2054
 
2055
  if (result)
2056
    {
2057
      changed = set_ssa_val_to (lhs, result);
2058
      if (TREE_CODE (result) == SSA_NAME
2059
          && VN_INFO (result)->has_constants)
2060
        {
2061
          VN_INFO (lhs)->expr = VN_INFO (result)->expr;
2062
          VN_INFO (lhs)->has_constants = true;
2063
        }
2064
    }
2065
  else
2066
    {
2067
      changed = set_ssa_val_to (lhs, lhs);
2068
      vn_reference_insert (op, lhs, last_vuse);
2069
    }
2070
 
2071
  return changed;
2072
}
2073
 
2074
 
2075
/* Visit a store to a reference operator LHS, part of STMT, value number it,
2076
   and return true if the value number of the LHS has changed as a result.  */
2077
 
2078
static bool
2079
visit_reference_op_store (tree lhs, tree op, gimple stmt)
2080
{
2081
  bool changed = false;
2082
  tree result;
2083
  bool resultsame = false;
2084
 
2085
  /* First we want to lookup using the *vuses* from the store and see
2086
     if there the last store to this location with the same address
2087
     had the same value.
2088
 
2089
     The vuses represent the memory state before the store.  If the
2090
     memory state, address, and value of the store is the same as the
2091
     last store to this location, then this store will produce the
2092
     same memory state as that store.
2093
 
2094
     In this case the vdef versions for this store are value numbered to those
2095
     vuse versions, since they represent the same memory state after
2096
     this store.
2097
 
2098
     Otherwise, the vdefs for the store are used when inserting into
2099
     the table, since the store generates a new memory state.  */
2100
 
2101
  result = vn_reference_lookup (lhs, gimple_vuse (stmt), false, NULL);
2102
 
2103
  if (result)
2104
    {
2105
      if (TREE_CODE (result) == SSA_NAME)
2106
        result = SSA_VAL (result);
2107
      if (TREE_CODE (op) == SSA_NAME)
2108
        op = SSA_VAL (op);
2109
      resultsame = expressions_equal_p (result, op);
2110
    }
2111
 
2112
  if (!result || !resultsame)
2113
    {
2114
      tree vdef;
2115
 
2116
      if (dump_file && (dump_flags & TDF_DETAILS))
2117
        {
2118
          fprintf (dump_file, "No store match\n");
2119
          fprintf (dump_file, "Value numbering store ");
2120
          print_generic_expr (dump_file, lhs, 0);
2121
          fprintf (dump_file, " to ");
2122
          print_generic_expr (dump_file, op, 0);
2123
          fprintf (dump_file, "\n");
2124
        }
2125
      /* Have to set value numbers before insert, since insert is
2126
         going to valueize the references in-place.  */
2127
      if ((vdef = gimple_vdef (stmt)))
2128
        {
2129
          VN_INFO (vdef)->use_processed = true;
2130
          changed |= set_ssa_val_to (vdef, vdef);
2131
        }
2132
 
2133
      /* Do not insert structure copies into the tables.  */
2134
      if (is_gimple_min_invariant (op)
2135
          || is_gimple_reg (op))
2136
        vn_reference_insert (lhs, op, vdef);
2137
    }
2138
  else
2139
    {
2140
      /* We had a match, so value number the vdef to have the value
2141
         number of the vuse it came from.  */
2142
      tree def, use;
2143
 
2144
      if (dump_file && (dump_flags & TDF_DETAILS))
2145
        fprintf (dump_file, "Store matched earlier value,"
2146
                 "value numbering store vdefs to matching vuses.\n");
2147
 
2148
      def = gimple_vdef (stmt);
2149
      use = gimple_vuse (stmt);
2150
 
2151
      VN_INFO (def)->use_processed = true;
2152
      changed |= set_ssa_val_to (def, SSA_VAL (use));
2153
    }
2154
 
2155
  return changed;
2156
}
2157
 
2158
/* Visit and value number PHI, return true if the value number
2159
   changed.  */
2160
 
2161
static bool
2162
visit_phi (gimple phi)
2163
{
2164
  bool changed = false;
2165
  tree result;
2166
  tree sameval = VN_TOP;
2167
  bool allsame = true;
2168
  unsigned i;
2169
 
2170
  /* TODO: We could check for this in init_sccvn, and replace this
2171
     with a gcc_assert.  */
2172
  if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
2173
    return set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2174
 
2175
  /* See if all non-TOP arguments have the same value.  TOP is
2176
     equivalent to everything, so we can ignore it.  */
2177
  for (i = 0; i < gimple_phi_num_args (phi); i++)
2178
    {
2179
      tree def = PHI_ARG_DEF (phi, i);
2180
 
2181
      if (TREE_CODE (def) == SSA_NAME)
2182
        def = SSA_VAL (def);
2183
      if (def == VN_TOP)
2184
        continue;
2185
      if (sameval == VN_TOP)
2186
        {
2187
          sameval = def;
2188
        }
2189
      else
2190
        {
2191
          if (!expressions_equal_p (def, sameval))
2192
            {
2193
              allsame = false;
2194
              break;
2195
            }
2196
        }
2197
    }
2198
 
2199
  /* If all value numbered to the same value, the phi node has that
2200
     value.  */
2201
  if (allsame)
2202
    {
2203
      if (is_gimple_min_invariant (sameval))
2204
        {
2205
          VN_INFO (PHI_RESULT (phi))->has_constants = true;
2206
          VN_INFO (PHI_RESULT (phi))->expr = sameval;
2207
        }
2208
      else
2209
        {
2210
          VN_INFO (PHI_RESULT (phi))->has_constants = false;
2211
          VN_INFO (PHI_RESULT (phi))->expr = sameval;
2212
        }
2213
 
2214
      if (TREE_CODE (sameval) == SSA_NAME)
2215
        return visit_copy (PHI_RESULT (phi), sameval);
2216
 
2217
      return set_ssa_val_to (PHI_RESULT (phi), sameval);
2218
    }
2219
 
2220
  /* Otherwise, see if it is equivalent to a phi node in this block.  */
2221
  result = vn_phi_lookup (phi);
2222
  if (result)
2223
    {
2224
      if (TREE_CODE (result) == SSA_NAME)
2225
        changed = visit_copy (PHI_RESULT (phi), result);
2226
      else
2227
        changed = set_ssa_val_to (PHI_RESULT (phi), result);
2228
    }
2229
  else
2230
    {
2231
      vn_phi_insert (phi, PHI_RESULT (phi));
2232
      VN_INFO (PHI_RESULT (phi))->has_constants = false;
2233
      VN_INFO (PHI_RESULT (phi))->expr = PHI_RESULT (phi);
2234
      changed = set_ssa_val_to (PHI_RESULT (phi), PHI_RESULT (phi));
2235
    }
2236
 
2237
  return changed;
2238
}
2239
 
2240
/* Return true if EXPR contains constants.  */
2241
 
2242
static bool
2243
expr_has_constants (tree expr)
2244
{
2245
  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2246
    {
2247
    case tcc_unary:
2248
      return is_gimple_min_invariant (TREE_OPERAND (expr, 0));
2249
 
2250
    case tcc_binary:
2251
      return is_gimple_min_invariant (TREE_OPERAND (expr, 0))
2252
        || is_gimple_min_invariant (TREE_OPERAND (expr, 1));
2253
      /* Constants inside reference ops are rarely interesting, but
2254
         it can take a lot of looking to find them.  */
2255
    case tcc_reference:
2256
    case tcc_declaration:
2257
      return false;
2258
    default:
2259
      return is_gimple_min_invariant (expr);
2260
    }
2261
  return false;
2262
}
2263
 
2264
/* Return true if STMT contains constants.  */
2265
 
2266
static bool
2267
stmt_has_constants (gimple stmt)
2268
{
2269
  if (gimple_code (stmt) != GIMPLE_ASSIGN)
2270
    return false;
2271
 
2272
  switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2273
    {
2274
    case GIMPLE_UNARY_RHS:
2275
      return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2276
 
2277
    case GIMPLE_BINARY_RHS:
2278
      return (is_gimple_min_invariant (gimple_assign_rhs1 (stmt))
2279
              || is_gimple_min_invariant (gimple_assign_rhs2 (stmt)));
2280
    case GIMPLE_SINGLE_RHS:
2281
      /* Constants inside reference ops are rarely interesting, but
2282
         it can take a lot of looking to find them.  */
2283
      return is_gimple_min_invariant (gimple_assign_rhs1 (stmt));
2284
    default:
2285
      gcc_unreachable ();
2286
    }
2287
  return false;
2288
}
2289
 
2290
/* Replace SSA_NAMES in expr with their value numbers, and return the
2291
   result.
2292
   This is performed in place. */
2293
 
2294
static tree
2295
valueize_expr (tree expr)
2296
{
2297
  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2298
    {
2299
    case tcc_unary:
2300
      if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2301
          && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2302
        TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2303
      break;
2304
    case tcc_binary:
2305
      if (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2306
          && SSA_VAL (TREE_OPERAND (expr, 0)) != VN_TOP)
2307
        TREE_OPERAND (expr, 0) = SSA_VAL (TREE_OPERAND (expr, 0));
2308
      if (TREE_CODE (TREE_OPERAND (expr, 1)) == SSA_NAME
2309
          && SSA_VAL (TREE_OPERAND (expr, 1)) != VN_TOP)
2310
        TREE_OPERAND (expr, 1) = SSA_VAL (TREE_OPERAND (expr, 1));
2311
      break;
2312
    default:
2313
      break;
2314
    }
2315
  return expr;
2316
}
2317
 
2318
/* Simplify the binary expression RHS, and return the result if
2319
   simplified. */
2320
 
2321
static tree
2322
simplify_binary_expression (gimple stmt)
2323
{
2324
  tree result = NULL_TREE;
2325
  tree op0 = gimple_assign_rhs1 (stmt);
2326
  tree op1 = gimple_assign_rhs2 (stmt);
2327
 
2328
  /* This will not catch every single case we could combine, but will
2329
     catch those with constants.  The goal here is to simultaneously
2330
     combine constants between expressions, but avoid infinite
2331
     expansion of expressions during simplification.  */
2332
  if (TREE_CODE (op0) == SSA_NAME)
2333
    {
2334
      if (VN_INFO (op0)->has_constants
2335
          || TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
2336
        op0 = valueize_expr (vn_get_expr_for (op0));
2337
      else if (SSA_VAL (op0) != VN_TOP && SSA_VAL (op0) != op0)
2338
        op0 = SSA_VAL (op0);
2339
    }
2340
 
2341
  if (TREE_CODE (op1) == SSA_NAME)
2342
    {
2343
      if (VN_INFO (op1)->has_constants)
2344
        op1 = valueize_expr (vn_get_expr_for (op1));
2345
      else if (SSA_VAL (op1) != VN_TOP && SSA_VAL (op1) != op1)
2346
        op1 = SSA_VAL (op1);
2347
    }
2348
 
2349
  /* Avoid folding if nothing changed.  */
2350
  if (op0 == gimple_assign_rhs1 (stmt)
2351
      && op1 == gimple_assign_rhs2 (stmt))
2352
    return NULL_TREE;
2353
 
2354
  fold_defer_overflow_warnings ();
2355
 
2356
  result = fold_binary (gimple_assign_rhs_code (stmt),
2357
                        gimple_expr_type (stmt), op0, op1);
2358
  if (result)
2359
    STRIP_USELESS_TYPE_CONVERSION (result);
2360
 
2361
  fold_undefer_overflow_warnings (result && valid_gimple_rhs_p (result),
2362
                                  stmt, 0);
2363
 
2364
  /* Make sure result is not a complex expression consisting
2365
     of operators of operators (IE (a + b) + (a + c))
2366
     Otherwise, we will end up with unbounded expressions if
2367
     fold does anything at all.  */
2368
  if (result && valid_gimple_rhs_p (result))
2369
    return result;
2370
 
2371
  return NULL_TREE;
2372
}
2373
 
2374
/* Simplify the unary expression RHS, and return the result if
2375
   simplified. */
2376
 
2377
static tree
2378
simplify_unary_expression (gimple stmt)
2379
{
2380
  tree result = NULL_TREE;
2381
  tree orig_op0, op0 = gimple_assign_rhs1 (stmt);
2382
 
2383
  /* We handle some tcc_reference codes here that are all
2384
     GIMPLE_ASSIGN_SINGLE codes.  */
2385
  if (gimple_assign_rhs_code (stmt) == REALPART_EXPR
2386
      || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2387
      || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2388
    op0 = TREE_OPERAND (op0, 0);
2389
 
2390
  if (TREE_CODE (op0) != SSA_NAME)
2391
    return NULL_TREE;
2392
 
2393
  orig_op0 = op0;
2394
  if (VN_INFO (op0)->has_constants)
2395
    op0 = valueize_expr (vn_get_expr_for (op0));
2396
  else if (gimple_assign_cast_p (stmt)
2397
           || gimple_assign_rhs_code (stmt) == REALPART_EXPR
2398
           || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2399
           || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
2400
    {
2401
      /* We want to do tree-combining on conversion-like expressions.
2402
         Make sure we feed only SSA_NAMEs or constants to fold though.  */
2403
      tree tem = valueize_expr (vn_get_expr_for (op0));
2404
      if (UNARY_CLASS_P (tem)
2405
          || BINARY_CLASS_P (tem)
2406
          || TREE_CODE (tem) == VIEW_CONVERT_EXPR
2407
          || TREE_CODE (tem) == SSA_NAME
2408
          || is_gimple_min_invariant (tem))
2409
        op0 = tem;
2410
    }
2411
 
2412
  /* Avoid folding if nothing changed, but remember the expression.  */
2413
  if (op0 == orig_op0)
2414
    return NULL_TREE;
2415
 
2416
  result = fold_unary_ignore_overflow (gimple_assign_rhs_code (stmt),
2417
                                       gimple_expr_type (stmt), op0);
2418
  if (result)
2419
    {
2420
      STRIP_USELESS_TYPE_CONVERSION (result);
2421
      if (valid_gimple_rhs_p (result))
2422
        return result;
2423
    }
2424
 
2425
  return NULL_TREE;
2426
}
2427
 
2428
/* Try to simplify RHS using equivalences and constant folding.  */
2429
 
2430
static tree
2431
try_to_simplify (gimple stmt)
2432
{
2433
  tree tem;
2434
 
2435
  /* For stores we can end up simplifying a SSA_NAME rhs.  Just return
2436
     in this case, there is no point in doing extra work.  */
2437
  if (gimple_assign_copy_p (stmt)
2438
      && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2439
    return NULL_TREE;
2440
 
2441
  switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2442
    {
2443
    case tcc_declaration:
2444
      tem = get_symbol_constant_value (gimple_assign_rhs1 (stmt));
2445
      if (tem)
2446
        return tem;
2447
      break;
2448
 
2449
    case tcc_reference:
2450
      /* Do not do full-blown reference lookup here, but simplify
2451
         reads from constant aggregates.  */
2452
      tem = fold_const_aggregate_ref (gimple_assign_rhs1 (stmt));
2453
      if (tem)
2454
        return tem;
2455
 
2456
      /* Fallthrough for some codes that can operate on registers.  */
2457
      if (!(TREE_CODE (gimple_assign_rhs1 (stmt)) == REALPART_EXPR
2458
            || TREE_CODE (gimple_assign_rhs1 (stmt)) == IMAGPART_EXPR
2459
            || TREE_CODE (gimple_assign_rhs1 (stmt)) == VIEW_CONVERT_EXPR))
2460
        break;
2461
      /* We could do a little more with unary ops, if they expand
2462
         into binary ops, but it's debatable whether it is worth it. */
2463
    case tcc_unary:
2464
      return simplify_unary_expression (stmt);
2465
      break;
2466
    case tcc_comparison:
2467
    case tcc_binary:
2468
      return simplify_binary_expression (stmt);
2469
      break;
2470
    default:
2471
      break;
2472
    }
2473
 
2474
  return NULL_TREE;
2475
}
2476
 
2477
/* Visit and value number USE, return true if the value number
2478
   changed. */
2479
 
2480
static bool
2481
visit_use (tree use)
2482
{
2483
  bool changed = false;
2484
  gimple stmt = SSA_NAME_DEF_STMT (use);
2485
 
2486
  VN_INFO (use)->use_processed = true;
2487
 
2488
  gcc_assert (!SSA_NAME_IN_FREE_LIST (use));
2489
  if (dump_file && (dump_flags & TDF_DETAILS)
2490
      && !SSA_NAME_IS_DEFAULT_DEF (use))
2491
    {
2492
      fprintf (dump_file, "Value numbering ");
2493
      print_generic_expr (dump_file, use, 0);
2494
      fprintf (dump_file, " stmt = ");
2495
      print_gimple_stmt (dump_file, stmt, 0, 0);
2496
    }
2497
 
2498
  /* Handle uninitialized uses.  */
2499
  if (SSA_NAME_IS_DEFAULT_DEF (use))
2500
    changed = set_ssa_val_to (use, use);
2501
  else
2502
    {
2503
      if (gimple_code (stmt) == GIMPLE_PHI)
2504
        changed = visit_phi (stmt);
2505
      else if (!gimple_has_lhs (stmt)
2506
               || gimple_has_volatile_ops (stmt)
2507
               || stmt_could_throw_p (stmt))
2508
        changed = defs_to_varying (stmt);
2509
      else if (is_gimple_assign (stmt))
2510
        {
2511
          tree lhs = gimple_assign_lhs (stmt);
2512
          tree simplified;
2513
 
2514
          /* Shortcut for copies. Simplifying copies is pointless,
2515
             since we copy the expression and value they represent.  */
2516
          if (gimple_assign_copy_p (stmt)
2517
              && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2518
              && TREE_CODE (lhs) == SSA_NAME)
2519
            {
2520
              changed = visit_copy (lhs, gimple_assign_rhs1 (stmt));
2521
              goto done;
2522
            }
2523
          simplified = try_to_simplify (stmt);
2524
          if (simplified)
2525
            {
2526
              if (dump_file && (dump_flags & TDF_DETAILS))
2527
                {
2528
                  fprintf (dump_file, "RHS ");
2529
                  print_gimple_expr (dump_file, stmt, 0, 0);
2530
                  fprintf (dump_file, " simplified to ");
2531
                  print_generic_expr (dump_file, simplified, 0);
2532
                  if (TREE_CODE (lhs) == SSA_NAME)
2533
                    fprintf (dump_file, " has constants %d\n",
2534
                             expr_has_constants (simplified));
2535
                  else
2536
                    fprintf (dump_file, "\n");
2537
                }
2538
            }
2539
          /* Setting value numbers to constants will occasionally
2540
             screw up phi congruence because constants are not
2541
             uniquely associated with a single ssa name that can be
2542
             looked up.  */
2543
          if (simplified
2544
              && is_gimple_min_invariant (simplified)
2545
              && TREE_CODE (lhs) == SSA_NAME)
2546
            {
2547
              VN_INFO (lhs)->expr = simplified;
2548
              VN_INFO (lhs)->has_constants = true;
2549
              changed = set_ssa_val_to (lhs, simplified);
2550
              goto done;
2551
            }
2552
          else if (simplified
2553
                   && TREE_CODE (simplified) == SSA_NAME
2554
                   && TREE_CODE (lhs) == SSA_NAME)
2555
            {
2556
              changed = visit_copy (lhs, simplified);
2557
              goto done;
2558
            }
2559
          else if (simplified)
2560
            {
2561
              if (TREE_CODE (lhs) == SSA_NAME)
2562
                {
2563
                  VN_INFO (lhs)->has_constants = expr_has_constants (simplified);
2564
                  /* We have to unshare the expression or else
2565
                     valuizing may change the IL stream.  */
2566
                  VN_INFO (lhs)->expr = unshare_expr (simplified);
2567
                }
2568
            }
2569
          else if (stmt_has_constants (stmt)
2570
                   && TREE_CODE (lhs) == SSA_NAME)
2571
            VN_INFO (lhs)->has_constants = true;
2572
          else if (TREE_CODE (lhs) == SSA_NAME)
2573
            {
2574
              /* We reset expr and constantness here because we may
2575
                 have been value numbering optimistically, and
2576
                 iterating. They may become non-constant in this case,
2577
                 even if they were optimistically constant. */
2578
 
2579
              VN_INFO (lhs)->has_constants = false;
2580
              VN_INFO (lhs)->expr = NULL_TREE;
2581
            }
2582
 
2583
          if ((TREE_CODE (lhs) == SSA_NAME
2584
               /* We can substitute SSA_NAMEs that are live over
2585
                  abnormal edges with their constant value.  */
2586
               && !(gimple_assign_copy_p (stmt)
2587
                    && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2588
               && !(simplified
2589
                    && is_gimple_min_invariant (simplified))
2590
               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2591
              /* Stores or copies from SSA_NAMEs that are live over
2592
                 abnormal edges are a problem.  */
2593
              || (gimple_assign_single_p (stmt)
2594
                  && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
2595
                  && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt))))
2596
            changed = defs_to_varying (stmt);
2597
          else if (REFERENCE_CLASS_P (lhs) || DECL_P (lhs))
2598
            {
2599
              changed = visit_reference_op_store (lhs, gimple_assign_rhs1 (stmt), stmt);
2600
            }
2601
          else if (TREE_CODE (lhs) == SSA_NAME)
2602
            {
2603
              if ((gimple_assign_copy_p (stmt)
2604
                   && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
2605
                  || (simplified
2606
                      && is_gimple_min_invariant (simplified)))
2607
                {
2608
                  VN_INFO (lhs)->has_constants = true;
2609
                  if (simplified)
2610
                    changed = set_ssa_val_to (lhs, simplified);
2611
                  else
2612
                    changed = set_ssa_val_to (lhs, gimple_assign_rhs1 (stmt));
2613
                }
2614
              else
2615
                {
2616
                  switch (get_gimple_rhs_class (gimple_assign_rhs_code (stmt)))
2617
                    {
2618
                    case GIMPLE_UNARY_RHS:
2619
                      changed = visit_unary_op (lhs, stmt);
2620
                      break;
2621
                    case GIMPLE_BINARY_RHS:
2622
                      changed = visit_binary_op (lhs, stmt);
2623
                      break;
2624
                    case GIMPLE_SINGLE_RHS:
2625
                      switch (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)))
2626
                        {
2627
                        case tcc_reference:
2628
                          /* VOP-less references can go through unary case.  */
2629
                          if ((gimple_assign_rhs_code (stmt) == REALPART_EXPR
2630
                               || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR
2631
                               || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR )
2632
                              && TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (stmt), 0)) == SSA_NAME)
2633
                            {
2634
                              changed = visit_unary_op (lhs, stmt);
2635
                              break;
2636
                            }
2637
                          /* Fallthrough.  */
2638
                        case tcc_declaration:
2639
                          changed = visit_reference_op_load
2640
                              (lhs, gimple_assign_rhs1 (stmt), stmt);
2641
                          break;
2642
                        case tcc_expression:
2643
                          if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
2644
                            {
2645
                              changed = visit_unary_op (lhs, stmt);
2646
                              break;
2647
                            }
2648
                          /* Fallthrough.  */
2649
                        default:
2650
                          changed = defs_to_varying (stmt);
2651
                        }
2652
                      break;
2653
                    default:
2654
                      changed = defs_to_varying (stmt);
2655
                      break;
2656
                    }
2657
                }
2658
            }
2659
          else
2660
            changed = defs_to_varying (stmt);
2661
        }
2662
      else if (is_gimple_call (stmt))
2663
        {
2664
          tree lhs = gimple_call_lhs (stmt);
2665
 
2666
          /* ???  We could try to simplify calls.  */
2667
 
2668
          if (stmt_has_constants (stmt)
2669
              && TREE_CODE (lhs) == SSA_NAME)
2670
            VN_INFO (lhs)->has_constants = true;
2671
          else if (TREE_CODE (lhs) == SSA_NAME)
2672
            {
2673
              /* We reset expr and constantness here because we may
2674
                 have been value numbering optimistically, and
2675
                 iterating. They may become non-constant in this case,
2676
                 even if they were optimistically constant. */
2677
              VN_INFO (lhs)->has_constants = false;
2678
              VN_INFO (lhs)->expr = NULL_TREE;
2679
            }
2680
 
2681
          if (TREE_CODE (lhs) == SSA_NAME
2682
              && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2683
            changed = defs_to_varying (stmt);
2684
          /* ???  We should handle stores from calls.  */
2685
          else if (TREE_CODE (lhs) == SSA_NAME)
2686
            {
2687
              if (gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST))
2688
                changed = visit_reference_op_call (lhs, stmt);
2689
              else
2690
                changed = defs_to_varying (stmt);
2691
            }
2692
          else
2693
            changed = defs_to_varying (stmt);
2694
        }
2695
    }
2696
 done:
2697
  return changed;
2698
}
2699
 
2700
/* Compare two operands by reverse postorder index */
2701
 
2702
static int
2703
compare_ops (const void *pa, const void *pb)
2704
{
2705
  const tree opa = *((const tree *)pa);
2706
  const tree opb = *((const tree *)pb);
2707
  gimple opstmta = SSA_NAME_DEF_STMT (opa);
2708
  gimple opstmtb = SSA_NAME_DEF_STMT (opb);
2709
  basic_block bba;
2710
  basic_block bbb;
2711
 
2712
  if (gimple_nop_p (opstmta) && gimple_nop_p (opstmtb))
2713
    return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2714
  else if (gimple_nop_p (opstmta))
2715
    return -1;
2716
  else if (gimple_nop_p (opstmtb))
2717
    return 1;
2718
 
2719
  bba = gimple_bb (opstmta);
2720
  bbb = gimple_bb (opstmtb);
2721
 
2722
  if (!bba && !bbb)
2723
    return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2724
  else if (!bba)
2725
    return -1;
2726
  else if (!bbb)
2727
    return 1;
2728
 
2729
  if (bba == bbb)
2730
    {
2731
      if (gimple_code (opstmta) == GIMPLE_PHI
2732
          && gimple_code (opstmtb) == GIMPLE_PHI)
2733
        return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2734
      else if (gimple_code (opstmta) == GIMPLE_PHI)
2735
        return -1;
2736
      else if (gimple_code (opstmtb) == GIMPLE_PHI)
2737
        return 1;
2738
      else if (gimple_uid (opstmta) != gimple_uid (opstmtb))
2739
        return gimple_uid (opstmta) - gimple_uid (opstmtb);
2740
      else
2741
        return SSA_NAME_VERSION (opa) - SSA_NAME_VERSION (opb);
2742
    }
2743
  return rpo_numbers[bba->index] - rpo_numbers[bbb->index];
2744
}
2745
 
2746
/* Sort an array containing members of a strongly connected component
2747
   SCC so that the members are ordered by RPO number.
2748
   This means that when the sort is complete, iterating through the
2749
   array will give you the members in RPO order.  */
2750
 
2751
static void
2752
sort_scc (VEC (tree, heap) *scc)
2753
{
2754
  qsort (VEC_address (tree, scc),
2755
         VEC_length (tree, scc),
2756
         sizeof (tree),
2757
         compare_ops);
2758
}
2759
 
2760
/* Insert the no longer used nary *ENTRY to the current hash.  */
2761
 
2762
static int
2763
copy_nary (void **entry, void *data ATTRIBUTE_UNUSED)
2764
{
2765
  vn_nary_op_t onary = (vn_nary_op_t) *entry;
2766
  size_t size = (sizeof (struct vn_nary_op_s)
2767
                 - sizeof (tree) * (4 - onary->length));
2768
  vn_nary_op_t nary = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
2769
                                                    size);
2770
  void **slot;
2771
  memcpy (nary, onary, size);
2772
  slot = htab_find_slot_with_hash (current_info->nary, nary, nary->hashcode,
2773
                                   INSERT);
2774
  gcc_assert (!*slot);
2775
  *slot = nary;
2776
  return 1;
2777
}
2778
 
2779
/* Insert the no longer used phi *ENTRY to the current hash.  */
2780
 
2781
static int
2782
copy_phis (void **entry, void *data ATTRIBUTE_UNUSED)
2783
{
2784
  vn_phi_t ophi = (vn_phi_t) *entry;
2785
  vn_phi_t phi = (vn_phi_t) pool_alloc (current_info->phis_pool);
2786
  void **slot;
2787
  memcpy (phi, ophi, sizeof (*phi));
2788
  ophi->phiargs = NULL;
2789
  slot = htab_find_slot_with_hash (current_info->phis, phi, phi->hashcode,
2790
                                   INSERT);
2791
  *slot = phi;
2792
  return 1;
2793
}
2794
 
2795
/* Insert the no longer used reference *ENTRY to the current hash.  */
2796
 
2797
static int
2798
copy_references (void **entry, void *data ATTRIBUTE_UNUSED)
2799
{
2800
  vn_reference_t oref = (vn_reference_t) *entry;
2801
  vn_reference_t ref;
2802
  void **slot;
2803
  ref = (vn_reference_t) pool_alloc (current_info->references_pool);
2804
  memcpy (ref, oref, sizeof (*ref));
2805
  oref->operands = NULL;
2806
  slot = htab_find_slot_with_hash (current_info->references, ref, ref->hashcode,
2807
                                   INSERT);
2808
  if (*slot)
2809
    free_reference (*slot);
2810
  *slot = ref;
2811
  return 1;
2812
}
2813
 
2814
/* Process a strongly connected component in the SSA graph.  */
2815
 
2816
static void
2817
process_scc (VEC (tree, heap) *scc)
2818
{
2819
  /* If the SCC has a single member, just visit it.  */
2820
 
2821
  if (VEC_length (tree, scc) == 1)
2822
    {
2823
      tree use = VEC_index (tree, scc, 0);
2824
      if (!VN_INFO (use)->use_processed)
2825
        visit_use (use);
2826
    }
2827
  else
2828
    {
2829
      tree var;
2830
      unsigned int i;
2831
      unsigned int iterations = 0;
2832
      bool changed = true;
2833
 
2834
      /* Iterate over the SCC with the optimistic table until it stops
2835
         changing.  */
2836
      current_info = optimistic_info;
2837
      while (changed)
2838
        {
2839
          changed = false;
2840
          iterations++;
2841
          /* As we are value-numbering optimistically we have to
2842
             clear the expression tables and the simplified expressions
2843
             in each iteration until we converge.  */
2844
          htab_empty (optimistic_info->nary);
2845
          htab_empty (optimistic_info->phis);
2846
          htab_empty (optimistic_info->references);
2847
          obstack_free (&optimistic_info->nary_obstack, NULL);
2848
          gcc_obstack_init (&optimistic_info->nary_obstack);
2849
          empty_alloc_pool (optimistic_info->phis_pool);
2850
          empty_alloc_pool (optimistic_info->references_pool);
2851
          for (i = 0; VEC_iterate (tree, scc, i, var); i++)
2852
            VN_INFO (var)->expr = NULL_TREE;
2853
          for (i = 0; VEC_iterate (tree, scc, i, var); i++)
2854
            changed |= visit_use (var);
2855
        }
2856
 
2857
      statistics_histogram_event (cfun, "SCC iterations", iterations);
2858
 
2859
      /* Finally, copy the contents of the no longer used optimistic
2860
         table to the valid table.  */
2861
      current_info = valid_info;
2862
      htab_traverse (optimistic_info->nary, copy_nary, NULL);
2863
      htab_traverse (optimistic_info->phis, copy_phis, NULL);
2864
      htab_traverse (optimistic_info->references, copy_references, NULL);
2865
    }
2866
}
2867
 
2868
DEF_VEC_O(ssa_op_iter);
2869
DEF_VEC_ALLOC_O(ssa_op_iter,heap);
2870
 
2871
/* Pop the components of the found SCC for NAME off the SCC stack
2872
   and process them.  Returns true if all went well, false if
2873
   we run into resource limits.  */
2874
 
2875
static bool
2876
extract_and_process_scc_for_name (tree name)
2877
{
2878
  VEC (tree, heap) *scc = NULL;
2879
  tree x;
2880
 
2881
  /* Found an SCC, pop the components off the SCC stack and
2882
     process them.  */
2883
  do
2884
    {
2885
      x = VEC_pop (tree, sccstack);
2886
 
2887
      VN_INFO (x)->on_sccstack = false;
2888
      VEC_safe_push (tree, heap, scc, x);
2889
    } while (x != name);
2890
 
2891
  /* Bail out of SCCVN in case a SCC turns out to be incredibly large.  */
2892
  if (VEC_length (tree, scc)
2893
      > (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE))
2894
    {
2895
      if (dump_file)
2896
        fprintf (dump_file, "WARNING: Giving up with SCCVN due to "
2897
                 "SCC size %u exceeding %u\n", VEC_length (tree, scc),
2898
                 (unsigned)PARAM_VALUE (PARAM_SCCVN_MAX_SCC_SIZE));
2899
      return false;
2900
    }
2901
 
2902
  if (VEC_length (tree, scc) > 1)
2903
    sort_scc (scc);
2904
 
2905
  if (dump_file && (dump_flags & TDF_DETAILS))
2906
    print_scc (dump_file, scc);
2907
 
2908
  process_scc (scc);
2909
 
2910
  VEC_free (tree, heap, scc);
2911
 
2912
  return true;
2913
}
2914
 
2915
/* Depth first search on NAME to discover and process SCC's in the SSA
2916
   graph.
2917
   Execution of this algorithm relies on the fact that the SCC's are
2918
   popped off the stack in topological order.
2919
   Returns true if successful, false if we stopped processing SCC's due
2920
   to resource constraints.  */
2921
 
2922
static bool
2923
DFS (tree name)
2924
{
2925
  VEC(ssa_op_iter, heap) *itervec = NULL;
2926
  VEC(tree, heap) *namevec = NULL;
2927
  use_operand_p usep = NULL;
2928
  gimple defstmt;
2929
  tree use;
2930
  ssa_op_iter iter;
2931
 
2932
start_over:
2933
  /* SCC info */
2934
  VN_INFO (name)->dfsnum = next_dfs_num++;
2935
  VN_INFO (name)->visited = true;
2936
  VN_INFO (name)->low = VN_INFO (name)->dfsnum;
2937
 
2938
  VEC_safe_push (tree, heap, sccstack, name);
2939
  VN_INFO (name)->on_sccstack = true;
2940
  defstmt = SSA_NAME_DEF_STMT (name);
2941
 
2942
  /* Recursively DFS on our operands, looking for SCC's.  */
2943
  if (!gimple_nop_p (defstmt))
2944
    {
2945
      /* Push a new iterator.  */
2946
      if (gimple_code (defstmt) == GIMPLE_PHI)
2947
        usep = op_iter_init_phiuse (&iter, defstmt, SSA_OP_ALL_USES);
2948
      else
2949
        usep = op_iter_init_use (&iter, defstmt, SSA_OP_ALL_USES);
2950
    }
2951
  else
2952
    clear_and_done_ssa_iter (&iter);
2953
 
2954
  while (1)
2955
    {
2956
      /* If we are done processing uses of a name, go up the stack
2957
         of iterators and process SCCs as we found them.  */
2958
      if (op_iter_done (&iter))
2959
        {
2960
          /* See if we found an SCC.  */
2961
          if (VN_INFO (name)->low == VN_INFO (name)->dfsnum)
2962
            if (!extract_and_process_scc_for_name (name))
2963
              {
2964
                VEC_free (tree, heap, namevec);
2965
                VEC_free (ssa_op_iter, heap, itervec);
2966
                return false;
2967
              }
2968
 
2969
          /* Check if we are done.  */
2970
          if (VEC_empty (tree, namevec))
2971
            {
2972
              VEC_free (tree, heap, namevec);
2973
              VEC_free (ssa_op_iter, heap, itervec);
2974
              return true;
2975
            }
2976
 
2977
          /* Restore the last use walker and continue walking there.  */
2978
          use = name;
2979
          name = VEC_pop (tree, namevec);
2980
          memcpy (&iter, VEC_last (ssa_op_iter, itervec),
2981
                  sizeof (ssa_op_iter));
2982
          VEC_pop (ssa_op_iter, itervec);
2983
          goto continue_walking;
2984
        }
2985
 
2986
      use = USE_FROM_PTR (usep);
2987
 
2988
      /* Since we handle phi nodes, we will sometimes get
2989
         invariants in the use expression.  */
2990
      if (TREE_CODE (use) == SSA_NAME)
2991
        {
2992
          if (! (VN_INFO (use)->visited))
2993
            {
2994
              /* Recurse by pushing the current use walking state on
2995
                 the stack and starting over.  */
2996
              VEC_safe_push(ssa_op_iter, heap, itervec, &iter);
2997
              VEC_safe_push(tree, heap, namevec, name);
2998
              name = use;
2999
              goto start_over;
3000
 
3001
continue_walking:
3002
              VN_INFO (name)->low = MIN (VN_INFO (name)->low,
3003
                                         VN_INFO (use)->low);
3004
            }
3005
          if (VN_INFO (use)->dfsnum < VN_INFO (name)->dfsnum
3006
              && VN_INFO (use)->on_sccstack)
3007
            {
3008
              VN_INFO (name)->low = MIN (VN_INFO (use)->dfsnum,
3009
                                         VN_INFO (name)->low);
3010
            }
3011
        }
3012
 
3013
      usep = op_iter_next_use (&iter);
3014
    }
3015
}
3016
 
3017
/* Allocate a value number table.  */
3018
 
3019
static void
3020
allocate_vn_table (vn_tables_t table)
3021
{
3022
  table->phis = htab_create (23, vn_phi_hash, vn_phi_eq, free_phi);
3023
  table->nary = htab_create (23, vn_nary_op_hash, vn_nary_op_eq, NULL);
3024
  table->references = htab_create (23, vn_reference_hash, vn_reference_eq,
3025
                                   free_reference);
3026
 
3027
  gcc_obstack_init (&table->nary_obstack);
3028
  table->phis_pool = create_alloc_pool ("VN phis",
3029
                                        sizeof (struct vn_phi_s),
3030
                                        30);
3031
  table->references_pool = create_alloc_pool ("VN references",
3032
                                              sizeof (struct vn_reference_s),
3033
                                              30);
3034
}
3035
 
3036
/* Free a value number table.  */
3037
 
3038
static void
3039
free_vn_table (vn_tables_t table)
3040
{
3041
  htab_delete (table->phis);
3042
  htab_delete (table->nary);
3043
  htab_delete (table->references);
3044
  obstack_free (&table->nary_obstack, NULL);
3045
  free_alloc_pool (table->phis_pool);
3046
  free_alloc_pool (table->references_pool);
3047
}
3048
 
3049
static void
3050
init_scc_vn (void)
3051
{
3052
  size_t i;
3053
  int j;
3054
  int *rpo_numbers_temp;
3055
 
3056
  calculate_dominance_info (CDI_DOMINATORS);
3057
  sccstack = NULL;
3058
  constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
3059
                                  free);
3060
 
3061
  constant_value_ids = BITMAP_ALLOC (NULL);
3062
 
3063
  next_dfs_num = 1;
3064
  next_value_id = 1;
3065
 
3066
  vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
3067
  /* VEC_alloc doesn't actually grow it to the right size, it just
3068
     preallocates the space to do so.  */
3069
  VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
3070
  gcc_obstack_init (&vn_ssa_aux_obstack);
3071
 
3072
  shared_lookup_phiargs = NULL;
3073
  shared_lookup_references = NULL;
3074
  rpo_numbers = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3075
  rpo_numbers_temp = XCNEWVEC (int, last_basic_block + NUM_FIXED_BLOCKS);
3076
  pre_and_rev_post_order_compute (NULL, rpo_numbers_temp, false);
3077
 
3078
  /* RPO numbers is an array of rpo ordering, rpo[i] = bb means that
3079
     the i'th block in RPO order is bb.  We want to map bb's to RPO
3080
     numbers, so we need to rearrange this array.  */
3081
  for (j = 0; j < n_basic_blocks - NUM_FIXED_BLOCKS; j++)
3082
    rpo_numbers[rpo_numbers_temp[j]] = j;
3083
 
3084
  XDELETE (rpo_numbers_temp);
3085
 
3086
  VN_TOP = create_tmp_var_raw (void_type_node, "vn_top");
3087
 
3088
  /* Create the VN_INFO structures, and initialize value numbers to
3089
     TOP.  */
3090
  for (i = 0; i < num_ssa_names; i++)
3091
    {
3092
      tree name = ssa_name (i);
3093
      if (name)
3094
        {
3095
          VN_INFO_GET (name)->valnum = VN_TOP;
3096
          VN_INFO (name)->expr = NULL_TREE;
3097
          VN_INFO (name)->value_id = 0;
3098
        }
3099
    }
3100
 
3101
  renumber_gimple_stmt_uids ();
3102
 
3103
  /* Create the valid and optimistic value numbering tables.  */
3104
  valid_info = XCNEW (struct vn_tables_s);
3105
  allocate_vn_table (valid_info);
3106
  optimistic_info = XCNEW (struct vn_tables_s);
3107
  allocate_vn_table (optimistic_info);
3108
}
3109
 
3110
void
3111
free_scc_vn (void)
3112
{
3113
  size_t i;
3114
 
3115
  htab_delete (constant_to_value_id);
3116
  BITMAP_FREE (constant_value_ids);
3117
  VEC_free (tree, heap, shared_lookup_phiargs);
3118
  VEC_free (vn_reference_op_s, heap, shared_lookup_references);
3119
  XDELETEVEC (rpo_numbers);
3120
 
3121
  for (i = 0; i < num_ssa_names; i++)
3122
    {
3123
      tree name = ssa_name (i);
3124
      if (name
3125
          && VN_INFO (name)->needs_insertion)
3126
        release_ssa_name (name);
3127
    }
3128
  obstack_free (&vn_ssa_aux_obstack, NULL);
3129
  VEC_free (vn_ssa_aux_t, heap, vn_ssa_aux_table);
3130
 
3131
  VEC_free (tree, heap, sccstack);
3132
  free_vn_table (valid_info);
3133
  XDELETE (valid_info);
3134
  free_vn_table (optimistic_info);
3135
  XDELETE (optimistic_info);
3136
}
3137
 
3138
/* Set the value ids in the valid hash tables.  */
3139
 
3140
static void
3141
set_hashtable_value_ids (void)
3142
{
3143
  htab_iterator hi;
3144
  vn_nary_op_t vno;
3145
  vn_reference_t vr;
3146
  vn_phi_t vp;
3147
 
3148
  /* Now set the value ids of the things we had put in the hash
3149
     table.  */
3150
 
3151
  FOR_EACH_HTAB_ELEMENT (valid_info->nary,
3152
                         vno, vn_nary_op_t, hi)
3153
    {
3154
      if (vno->result)
3155
        {
3156
          if (TREE_CODE (vno->result) == SSA_NAME)
3157
            vno->value_id = VN_INFO (vno->result)->value_id;
3158
          else if (is_gimple_min_invariant (vno->result))
3159
            vno->value_id = get_or_alloc_constant_value_id (vno->result);
3160
        }
3161
    }
3162
 
3163
  FOR_EACH_HTAB_ELEMENT (valid_info->phis,
3164
                         vp, vn_phi_t, hi)
3165
    {
3166
      if (vp->result)
3167
        {
3168
          if (TREE_CODE (vp->result) == SSA_NAME)
3169
            vp->value_id = VN_INFO (vp->result)->value_id;
3170
          else if (is_gimple_min_invariant (vp->result))
3171
            vp->value_id = get_or_alloc_constant_value_id (vp->result);
3172
        }
3173
    }
3174
 
3175
  FOR_EACH_HTAB_ELEMENT (valid_info->references,
3176
                         vr, vn_reference_t, hi)
3177
    {
3178
      if (vr->result)
3179
        {
3180
          if (TREE_CODE (vr->result) == SSA_NAME)
3181
            vr->value_id = VN_INFO (vr->result)->value_id;
3182
          else if (is_gimple_min_invariant (vr->result))
3183
            vr->value_id = get_or_alloc_constant_value_id (vr->result);
3184
        }
3185
    }
3186
}
3187
 
3188
/* Do SCCVN.  Returns true if it finished, false if we bailed out
3189
   due to resource constraints.  */
3190
 
3191
bool
3192
run_scc_vn (bool may_insert_arg)
3193
{
3194
  size_t i;
3195
  tree param;
3196
  bool changed = true;
3197
 
3198
  may_insert = may_insert_arg;
3199
 
3200
  init_scc_vn ();
3201
  current_info = valid_info;
3202
 
3203
  for (param = DECL_ARGUMENTS (current_function_decl);
3204
       param;
3205
       param = TREE_CHAIN (param))
3206
    {
3207
      if (gimple_default_def (cfun, param) != NULL)
3208
        {
3209
          tree def = gimple_default_def (cfun, param);
3210
          VN_INFO (def)->valnum = def;
3211
        }
3212
    }
3213
 
3214
  for (i = 1; i < num_ssa_names; ++i)
3215
    {
3216
      tree name = ssa_name (i);
3217
      if (name
3218
          && VN_INFO (name)->visited == false
3219
          && !has_zero_uses (name))
3220
        if (!DFS (name))
3221
          {
3222
            free_scc_vn ();
3223
            may_insert = false;
3224
            return false;
3225
          }
3226
    }
3227
 
3228
  /* Initialize the value ids.  */
3229
 
3230
  for (i = 1; i < num_ssa_names; ++i)
3231
    {
3232
      tree name = ssa_name (i);
3233
      vn_ssa_aux_t info;
3234
      if (!name)
3235
        continue;
3236
      info = VN_INFO (name);
3237
      if (info->valnum == name
3238
          || info->valnum == VN_TOP)
3239
        info->value_id = get_next_value_id ();
3240
      else if (is_gimple_min_invariant (info->valnum))
3241
        info->value_id = get_or_alloc_constant_value_id (info->valnum);
3242
    }
3243
 
3244
  /* Propagate until they stop changing.  */
3245
  while (changed)
3246
    {
3247
      changed = false;
3248
      for (i = 1; i < num_ssa_names; ++i)
3249
        {
3250
          tree name = ssa_name (i);
3251
          vn_ssa_aux_t info;
3252
          if (!name)
3253
            continue;
3254
          info = VN_INFO (name);
3255
          if (TREE_CODE (info->valnum) == SSA_NAME
3256
              && info->valnum != name
3257
              && info->value_id != VN_INFO (info->valnum)->value_id)
3258
            {
3259
              changed = true;
3260
              info->value_id = VN_INFO (info->valnum)->value_id;
3261
            }
3262
        }
3263
    }
3264
 
3265
  set_hashtable_value_ids ();
3266
 
3267
  if (dump_file && (dump_flags & TDF_DETAILS))
3268
    {
3269
      fprintf (dump_file, "Value numbers:\n");
3270
      for (i = 0; i < num_ssa_names; i++)
3271
        {
3272
          tree name = ssa_name (i);
3273
          if (name
3274
              && VN_INFO (name)->visited
3275
              && SSA_VAL (name) != name)
3276
            {
3277
              print_generic_expr (dump_file, name, 0);
3278
              fprintf (dump_file, " = ");
3279
              print_generic_expr (dump_file, SSA_VAL (name), 0);
3280
              fprintf (dump_file, "\n");
3281
            }
3282
        }
3283
    }
3284
 
3285
  may_insert = false;
3286
  return true;
3287
}
3288
 
3289
/* Return the maximum value id we have ever seen.  */
3290
 
3291
unsigned int
3292
get_max_value_id (void)
3293
{
3294
  return next_value_id;
3295
}
3296
 
3297
/* Return the next unique value id.  */
3298
 
3299
unsigned int
3300
get_next_value_id (void)
3301
{
3302
  return next_value_id++;
3303
}
3304
 
3305
 
3306
/* Compare two expressions E1 and E2 and return true if they are equal.  */
3307
 
3308
bool
3309
expressions_equal_p (tree e1, tree e2)
3310
{
3311
  /* The obvious case.  */
3312
  if (e1 == e2)
3313
    return true;
3314
 
3315
  /* If only one of them is null, they cannot be equal.  */
3316
  if (!e1 || !e2)
3317
    return false;
3318
 
3319
  /* Now perform the actual comparison.  */
3320
  if (TREE_CODE (e1) == TREE_CODE (e2)
3321
      && operand_equal_p (e1, e2, OEP_PURE_SAME))
3322
    return true;
3323
 
3324
  return false;
3325
}
3326
 
3327
 
3328
/* Return true if the nary operation NARY may trap.  This is a copy
3329
   of stmt_could_throw_1_p adjusted to the SCCVN IL.  */
3330
 
3331
bool
3332
vn_nary_may_trap (vn_nary_op_t nary)
3333
{
3334
  tree type;
3335
  tree rhs2 = NULL_TREE;
3336
  bool honor_nans = false;
3337
  bool honor_snans = false;
3338
  bool fp_operation = false;
3339
  bool honor_trapv = false;
3340
  bool handled, ret;
3341
  unsigned i;
3342
 
3343
  if (TREE_CODE_CLASS (nary->opcode) == tcc_comparison
3344
      || TREE_CODE_CLASS (nary->opcode) == tcc_unary
3345
      || TREE_CODE_CLASS (nary->opcode) == tcc_binary)
3346
    {
3347
      type = nary->type;
3348
      fp_operation = FLOAT_TYPE_P (type);
3349
      if (fp_operation)
3350
        {
3351
          honor_nans = flag_trapping_math && !flag_finite_math_only;
3352
          honor_snans = flag_signaling_nans != 0;
3353
        }
3354
      else if (INTEGRAL_TYPE_P (type)
3355
               && TYPE_OVERFLOW_TRAPS (type))
3356
        honor_trapv = true;
3357
    }
3358
  if (nary->length >= 2)
3359
    rhs2 = nary->op[1];
3360
  ret = operation_could_trap_helper_p (nary->opcode, fp_operation,
3361
                                       honor_trapv,
3362
                                       honor_nans, honor_snans, rhs2,
3363
                                       &handled);
3364
  if (handled
3365
      && ret)
3366
    return true;
3367
 
3368
  for (i = 0; i < nary->length; ++i)
3369
    if (tree_could_trap_p (nary->op[i]))
3370
      return true;
3371
 
3372
  return false;
3373
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.