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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [tree-dfa.c] - Blame information for rev 816

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 julius
/* Data flow functions for trees.
2
   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
3
   Contributed by Diego Novillo <dnovillo@redhat.com>
4
 
5
This file is part of GCC.
6
 
7
GCC is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 3, or (at your option)
10
any later version.
11
 
12
GCC is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING3.  If not see
19
<http://www.gnu.org/licenses/>.  */
20
 
21
#include "config.h"
22
#include "system.h"
23
#include "coretypes.h"
24
#include "tm.h"
25
#include "hashtab.h"
26
#include "pointer-set.h"
27
#include "tree.h"
28
#include "rtl.h"
29
#include "tm_p.h"
30
#include "hard-reg-set.h"
31
#include "basic-block.h"
32
#include "output.h"
33
#include "timevar.h"
34
#include "expr.h"
35
#include "ggc.h"
36
#include "langhooks.h"
37
#include "flags.h"
38
#include "function.h"
39
#include "diagnostic.h"
40
#include "tree-dump.h"
41
#include "tree-gimple.h"
42
#include "tree-flow.h"
43
#include "tree-inline.h"
44
#include "tree-pass.h"
45
#include "convert.h"
46
#include "params.h"
47
#include "cgraph.h"
48
 
49
/* Build and maintain data flow information for trees.  */
50
 
51
/* Counters used to display DFA and SSA statistics.  */
52
struct dfa_stats_d
53
{
54
  long num_stmt_anns;
55
  long num_var_anns;
56
  long num_defs;
57
  long num_uses;
58
  long num_phis;
59
  long num_phi_args;
60
  int max_num_phi_args;
61
  long num_v_may_defs;
62
  long num_vuses;
63
  long num_v_must_defs;
64
};
65
 
66
 
67
/* Local functions.  */
68
static void collect_dfa_stats (struct dfa_stats_d *);
69
static tree collect_dfa_stats_r (tree *, int *, void *);
70
static tree find_vars_r (tree *, int *, void *);
71
 
72
 
73
/* Global declarations.  */
74
 
75
/* Array of all variables referenced in the function.  */
76
htab_t referenced_vars;
77
 
78
/* Default definition for this symbols.  If set for symbol, it
79
   means that the first reference to this variable in the function is a
80
   USE or a VUSE.  In those cases, the SSA renamer creates an SSA name
81
   for this variable with an empty defining statement.  */
82
htab_t default_defs;
83
 
84
 
85
/*---------------------------------------------------------------------------
86
                        Dataflow analysis (DFA) routines
87
---------------------------------------------------------------------------*/
88
/* Find all the variables referenced in the function.  This function
89
   builds the global arrays REFERENCED_VARS and CALL_CLOBBERED_VARS.
90
 
91
   Note that this function does not look for statement operands, it simply
92
   determines what variables are referenced in the program and detects
93
   various attributes for each variable used by alias analysis and the
94
   optimizer.  */
95
 
96
static unsigned int
97
find_referenced_vars (void)
98
{
99
  basic_block bb;
100
  block_stmt_iterator si;
101
 
102
  FOR_EACH_BB (bb)
103
    for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
104
      {
105
        tree *stmt_p = bsi_stmt_ptr (si);
106
        walk_tree (stmt_p, find_vars_r, NULL, NULL);
107
      }
108
 
109
  return 0;
110
}
111
 
112
struct tree_opt_pass pass_referenced_vars =
113
{
114
  NULL,                                 /* name */
115
  NULL,                                 /* gate */
116
  find_referenced_vars,                 /* execute */
117
  NULL,                                 /* sub */
118
  NULL,                                 /* next */
119
  0,                                     /* static_pass_number */
120
  TV_FIND_REFERENCED_VARS,              /* tv_id */
121
  PROP_gimple_leh | PROP_cfg,           /* properties_required */
122
  PROP_referenced_vars,                 /* properties_provided */
123
  0,                                     /* properties_destroyed */
124
  0,                                     /* todo_flags_start */
125
  0,                                    /* todo_flags_finish */
126
 
127
};
128
 
129
 
130
/*---------------------------------------------------------------------------
131
                            Manage annotations
132
---------------------------------------------------------------------------*/
133
/* Create a new annotation for a _DECL node T.  */
134
 
135
var_ann_t
136
create_var_ann (tree t)
137
{
138
  var_ann_t ann;
139
 
140
  gcc_assert (t);
141
  gcc_assert (DECL_P (t));
142
  gcc_assert (!t->common.ann || t->common.ann->common.type == VAR_ANN);
143
 
144
  ann = GGC_CNEW (struct var_ann_d);
145
 
146
  ann->common.type = VAR_ANN;
147
 
148
  t->common.ann = (tree_ann_t) ann;
149
 
150
  return ann;
151
}
152
 
153
/* Create a new annotation for a FUNCTION_DECL node T.  */
154
 
155
function_ann_t
156
create_function_ann (tree t)
157
{
158
  function_ann_t ann;
159
 
160
  gcc_assert (t);
161
  gcc_assert (TREE_CODE (t) == FUNCTION_DECL);
162
  gcc_assert (!t->common.ann || t->common.ann->common.type == FUNCTION_ANN);
163
 
164
  ann = ggc_alloc (sizeof (*ann));
165
  memset ((void *) ann, 0, sizeof (*ann));
166
 
167
  ann->common.type = FUNCTION_ANN;
168
 
169
  t->common.ann = (tree_ann_t) ann;
170
 
171
  return ann;
172
}
173
 
174
/* Create a new annotation for a statement node T.  */
175
 
176
stmt_ann_t
177
create_stmt_ann (tree t)
178
{
179
  stmt_ann_t ann;
180
 
181
  gcc_assert (is_gimple_stmt (t));
182
  gcc_assert (!t->common.ann || t->common.ann->common.type == STMT_ANN);
183
 
184
  ann = GGC_CNEW (struct stmt_ann_d);
185
 
186
  ann->common.type = STMT_ANN;
187
 
188
  /* Since we just created the annotation, mark the statement modified.  */
189
  ann->modified = true;
190
 
191
  t->common.ann = (tree_ann_t) ann;
192
 
193
  return ann;
194
}
195
 
196
/* Create a new annotation for a tree T.  */
197
 
198
tree_ann_common_t
199
create_tree_common_ann (tree t)
200
{
201
  tree_ann_common_t ann;
202
 
203
  gcc_assert (t);
204
  gcc_assert (!t->common.ann || t->common.ann->common.type == TREE_ANN_COMMON);
205
 
206
  ann = GGC_CNEW (struct tree_ann_common_d);
207
 
208
  ann->type = TREE_ANN_COMMON;
209
  t->common.ann = (tree_ann_t) ann;
210
 
211
  return ann;
212
}
213
 
214
/* Build a temporary.  Make sure and register it to be renamed.  */
215
 
216
tree
217
make_rename_temp (tree type, const char *prefix)
218
{
219
  tree t = create_tmp_var (type, prefix);
220
 
221
  if (TREE_CODE (type) == COMPLEX_TYPE)
222
    DECL_COMPLEX_GIMPLE_REG_P (t) = 1;
223
 
224
  if (referenced_vars)
225
    {
226
      add_referenced_var (t);
227
      mark_sym_for_renaming (t);
228
    }
229
 
230
  return t;
231
}
232
 
233
 
234
 
235
/*---------------------------------------------------------------------------
236
                              Debugging functions
237
---------------------------------------------------------------------------*/
238
/* Dump the list of all the referenced variables in the current function to
239
   FILE.  */
240
 
241
void
242
dump_referenced_vars (FILE *file)
243
{
244
  tree var;
245
  referenced_var_iterator rvi;
246
 
247
  fprintf (file, "\nReferenced variables in %s: %u\n\n",
248
           get_name (current_function_decl), (unsigned) num_referenced_vars);
249
 
250
  FOR_EACH_REFERENCED_VAR (var, rvi)
251
    {
252
      fprintf (file, "Variable: ");
253
      dump_variable (file, var);
254
      fprintf (file, "\n");
255
    }
256
}
257
 
258
 
259
/* Dump the list of all the referenced variables to stderr.  */
260
 
261
void
262
debug_referenced_vars (void)
263
{
264
  dump_referenced_vars (stderr);
265
}
266
 
267
 
268
/* Dump sub-variables for VAR to FILE.  */
269
 
270
void
271
dump_subvars_for (FILE *file, tree var)
272
{
273
  subvar_t sv = get_subvars_for_var (var);
274
 
275
  if (!sv)
276
    return;
277
 
278
  fprintf (file, "{ ");
279
 
280
  for (; sv; sv = sv->next)
281
    {
282
      print_generic_expr (file, sv->var, dump_flags);
283
      fprintf (file, " ");
284
    }
285
 
286
  fprintf (file, "}");
287
}
288
 
289
 
290
/* Dumb sub-variables for VAR to stderr.  */
291
 
292
void
293
debug_subvars_for (tree var)
294
{
295
  dump_subvars_for (stderr, var);
296
}
297
 
298
 
299
/* Dump variable VAR and its may-aliases to FILE.  */
300
 
301
void
302
dump_variable (FILE *file, tree var)
303
{
304
  var_ann_t ann;
305
 
306
  if (TREE_CODE (var) == SSA_NAME)
307
    {
308
      if (POINTER_TYPE_P (TREE_TYPE (var)))
309
        dump_points_to_info_for (file, var);
310
      var = SSA_NAME_VAR (var);
311
    }
312
 
313
  if (var == NULL_TREE)
314
    {
315
      fprintf (file, "<nil>");
316
      return;
317
    }
318
 
319
  print_generic_expr (file, var, dump_flags);
320
 
321
  ann = var_ann (var);
322
 
323
  fprintf (file, ", UID %u", (unsigned) DECL_UID (var));
324
 
325
  fprintf (file, ", ");
326
  print_generic_expr (file, TREE_TYPE (var), dump_flags);
327
 
328
  if (ann && ann->symbol_mem_tag)
329
    {
330
      fprintf (file, ", symbol memory tag: ");
331
      print_generic_expr (file, ann->symbol_mem_tag, dump_flags);
332
    }
333
 
334
  if (ann && ann->is_aliased)
335
    fprintf (file, ", is aliased");
336
 
337
  if (TREE_ADDRESSABLE (var))
338
    fprintf (file, ", is addressable");
339
 
340
  if (is_global_var (var))
341
    fprintf (file, ", is global");
342
 
343
  if (TREE_THIS_VOLATILE (var))
344
    fprintf (file, ", is volatile");
345
 
346
  if (is_call_clobbered (var))
347
    {
348
      fprintf (file, ", call clobbered");
349
      if (dump_flags & TDF_DETAILS)
350
        {
351
          var_ann_t va = var_ann (var);
352
          unsigned int escape_mask = va->escape_mask;
353
 
354
          fprintf (file, " (");
355
          if (escape_mask & ESCAPE_STORED_IN_GLOBAL)
356
            fprintf (file, ", stored in global");
357
          if (escape_mask & ESCAPE_TO_ASM)
358
            fprintf (file, ", goes through ASM");
359
          if (escape_mask & ESCAPE_TO_CALL)
360
            fprintf (file, ", passed to call");
361
          if (escape_mask & ESCAPE_BAD_CAST)
362
            fprintf (file, ", bad cast");
363
          if (escape_mask & ESCAPE_TO_RETURN)
364
            fprintf (file, ", returned from func");
365
          if (escape_mask & ESCAPE_TO_PURE_CONST)
366
            fprintf (file, ", passed to pure/const");
367
          if (escape_mask & ESCAPE_IS_GLOBAL)
368
            fprintf (file, ", is global var");
369
          if (escape_mask & ESCAPE_IS_PARM)
370
            fprintf (file, ", is incoming pointer");
371
          if (escape_mask & ESCAPE_UNKNOWN)
372
            fprintf (file, ", unknown escape");
373
          fprintf (file, " )");
374
        }
375
    }
376
 
377
  if (default_def (var))
378
    {
379
      fprintf (file, ", default def: ");
380
      print_generic_expr (file, default_def (var), dump_flags);
381
    }
382
 
383
  if (may_aliases (var))
384
    {
385
      fprintf (file, ", may aliases: ");
386
      dump_may_aliases_for (file, var);
387
    }
388
 
389
  if (get_subvars_for_var (var))
390
    {
391
      fprintf (file, ", sub-vars: ");
392
      dump_subvars_for (file, var);
393
    }
394
 
395
  fprintf (file, "\n");
396
}
397
 
398
 
399
/* Dump variable VAR and its may-aliases to stderr.  */
400
 
401
void
402
debug_variable (tree var)
403
{
404
  dump_variable (stderr, var);
405
}
406
 
407
 
408
/* Dump various DFA statistics to FILE.  */
409
 
410
void
411
dump_dfa_stats (FILE *file)
412
{
413
  struct dfa_stats_d dfa_stats;
414
 
415
  unsigned long size, total = 0;
416
  const char * const fmt_str   = "%-30s%-13s%12s\n";
417
  const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n";
418
  const char * const fmt_str_3 = "%-43s%11lu%c\n";
419
  const char *funcname
420
    = lang_hooks.decl_printable_name (current_function_decl, 2);
421
 
422
  collect_dfa_stats (&dfa_stats);
423
 
424
  fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
425
 
426
  fprintf (file, "---------------------------------------------------------\n");
427
  fprintf (file, fmt_str, "", "  Number of  ", "Memory");
428
  fprintf (file, fmt_str, "", "  instances  ", "used ");
429
  fprintf (file, "---------------------------------------------------------\n");
430
 
431
  size = num_referenced_vars * sizeof (tree);
432
  total += size;
433
  fprintf (file, fmt_str_1, "Referenced variables", (unsigned long)num_referenced_vars,
434
           SCALE (size), LABEL (size));
435
 
436
  size = dfa_stats.num_stmt_anns * sizeof (struct stmt_ann_d);
437
  total += size;
438
  fprintf (file, fmt_str_1, "Statements annotated", dfa_stats.num_stmt_anns,
439
           SCALE (size), LABEL (size));
440
 
441
  size = dfa_stats.num_var_anns * sizeof (struct var_ann_d);
442
  total += size;
443
  fprintf (file, fmt_str_1, "Variables annotated", dfa_stats.num_var_anns,
444
           SCALE (size), LABEL (size));
445
 
446
  size = dfa_stats.num_uses * sizeof (tree *);
447
  total += size;
448
  fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
449
           SCALE (size), LABEL (size));
450
 
451
  size = dfa_stats.num_defs * sizeof (tree *);
452
  total += size;
453
  fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
454
           SCALE (size), LABEL (size));
455
 
456
  size = dfa_stats.num_vuses * sizeof (tree *);
457
  total += size;
458
  fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
459
           SCALE (size), LABEL (size));
460
 
461
  size = dfa_stats.num_v_may_defs * sizeof (tree *);
462
  total += size;
463
  fprintf (file, fmt_str_1, "V_MAY_DEF operands", dfa_stats.num_v_may_defs,
464
           SCALE (size), LABEL (size));
465
 
466
  size = dfa_stats.num_v_must_defs * sizeof (tree *);
467
  total += size;
468
  fprintf (file, fmt_str_1, "V_MUST_DEF operands", dfa_stats.num_v_must_defs,
469
           SCALE (size), LABEL (size));
470
 
471
  size = dfa_stats.num_phis * sizeof (struct tree_phi_node);
472
  total += size;
473
  fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
474
           SCALE (size), LABEL (size));
475
 
476
  size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
477
  total += size;
478
  fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
479
           SCALE (size), LABEL (size));
480
 
481
  fprintf (file, "---------------------------------------------------------\n");
482
  fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total),
483
           LABEL (total));
484
  fprintf (file, "---------------------------------------------------------\n");
485
  fprintf (file, "\n");
486
 
487
  if (dfa_stats.num_phis)
488
    fprintf (file, "Average number of arguments per PHI node: %.1f (max: %d)\n",
489
             (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
490
             dfa_stats.max_num_phi_args);
491
 
492
  fprintf (file, "\n");
493
}
494
 
495
 
496
/* Dump DFA statistics on stderr.  */
497
 
498
void
499
debug_dfa_stats (void)
500
{
501
  dump_dfa_stats (stderr);
502
}
503
 
504
 
505
/* Collect DFA statistics and store them in the structure pointed to by
506
   DFA_STATS_P.  */
507
 
508
static void
509
collect_dfa_stats (struct dfa_stats_d *dfa_stats_p)
510
{
511
  struct pointer_set_t *pset;
512
  basic_block bb;
513
  block_stmt_iterator i;
514
 
515
  gcc_assert (dfa_stats_p);
516
 
517
  memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
518
 
519
  /* Walk all the trees in the function counting references.  Start at
520
     basic block NUM_FIXED_BLOCKS, but don't stop at block boundaries.  */
521
  pset = pointer_set_create ();
522
 
523
  for (i = bsi_start (BASIC_BLOCK (NUM_FIXED_BLOCKS));
524
       !bsi_end_p (i); bsi_next (&i))
525
    walk_tree (bsi_stmt_ptr (i), collect_dfa_stats_r, (void *) dfa_stats_p,
526
               pset);
527
 
528
  pointer_set_destroy (pset);
529
 
530
  FOR_EACH_BB (bb)
531
    {
532
      tree phi;
533
      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
534
        {
535
          dfa_stats_p->num_phis++;
536
          dfa_stats_p->num_phi_args += PHI_NUM_ARGS (phi);
537
          if (PHI_NUM_ARGS (phi) > dfa_stats_p->max_num_phi_args)
538
            dfa_stats_p->max_num_phi_args = PHI_NUM_ARGS (phi);
539
        }
540
    }
541
}
542
 
543
 
544
/* Callback for walk_tree to collect DFA statistics for a tree and its
545
   children.  */
546
 
547
static tree
548
collect_dfa_stats_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
549
                     void *data)
550
{
551
  tree t = *tp;
552
  struct dfa_stats_d *dfa_stats_p = (struct dfa_stats_d *)data;
553
 
554
  if (t->common.ann)
555
    {
556
      switch (ann_type (t->common.ann))
557
        {
558
        case STMT_ANN:
559
          {
560
            dfa_stats_p->num_stmt_anns++;
561
            dfa_stats_p->num_defs += NUM_SSA_OPERANDS (t, SSA_OP_DEF);
562
            dfa_stats_p->num_uses += NUM_SSA_OPERANDS (t, SSA_OP_USE);
563
            dfa_stats_p->num_v_may_defs += NUM_SSA_OPERANDS (t, SSA_OP_VMAYDEF);
564
            dfa_stats_p->num_vuses += NUM_SSA_OPERANDS (t, SSA_OP_VUSE);
565
            dfa_stats_p->num_v_must_defs +=
566
                                  NUM_SSA_OPERANDS (t, SSA_OP_VMUSTDEF);
567
            break;
568
          }
569
 
570
        case VAR_ANN:
571
          dfa_stats_p->num_var_anns++;
572
          break;
573
 
574
        default:
575
          break;
576
        }
577
    }
578
 
579
  return NULL;
580
}
581
 
582
 
583
/*---------------------------------------------------------------------------
584
                             Miscellaneous helpers
585
---------------------------------------------------------------------------*/
586
/* Callback for walk_tree.  Used to collect variables referenced in
587
   the function.  */
588
 
589
static tree
590
find_vars_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
591
{
592
  /* If T is a regular variable that the optimizers are interested
593
     in, add it to the list of variables.  */
594
  if (SSA_VAR_P (*tp))
595
    add_referenced_var (*tp);
596
 
597
  /* Type, _DECL and constant nodes have no interesting children.
598
     Ignore them.  */
599
  else if (IS_TYPE_OR_DECL_P (*tp) || CONSTANT_CLASS_P (*tp))
600
    *walk_subtrees = 0;
601
 
602
  return NULL_TREE;
603
}
604
 
605
/* Lookup UID in the referenced_vars hashtable and return the associated
606
   variable.  */
607
 
608
tree
609
referenced_var_lookup (unsigned int uid)
610
{
611
  struct int_tree_map *h, in;
612
  in.uid = uid;
613
  h = (struct int_tree_map *) htab_find_with_hash (referenced_vars, &in, uid);
614
  gcc_assert (h || uid == 0);
615
  if (h)
616
    return h->to;
617
  return NULL_TREE;
618
}
619
 
620
/* Check if TO is in the referenced_vars hash table and insert it if not.
621
   Return true if it required insertion.  */
622
 
623
bool
624
referenced_var_check_and_insert (tree to)
625
{
626
  struct int_tree_map *h, in;
627
  void **loc;
628
  unsigned int uid = DECL_UID (to);
629
 
630
  in.uid = uid;
631
  in.to = to;
632
  h = (struct int_tree_map *) htab_find_with_hash (referenced_vars, &in, uid);
633
 
634
  if (h)
635
    {
636
      /* DECL_UID has already been entered in the table.  Verify that it is
637
         the same entry as TO.  See PR 27793.  */
638
      gcc_assert (h->to == to);
639
      return false;
640
    }
641
 
642
  h = GGC_NEW (struct int_tree_map);
643
  h->uid = uid;
644
  h->to = to;
645
  loc = htab_find_slot_with_hash (referenced_vars, h, uid, INSERT);
646
  *(struct int_tree_map **)  loc = h;
647
  return true;
648
}
649
 
650
/* Lookup VAR UID in the default_defs hashtable and return the associated
651
   variable.  */
652
 
653
tree
654
default_def (tree var)
655
{
656
  struct int_tree_map *h, in;
657
  gcc_assert (SSA_VAR_P (var));
658
  in.uid = DECL_UID (var);
659
  h = (struct int_tree_map *) htab_find_with_hash (default_defs, &in,
660
                                                   DECL_UID (var));
661
  if (h)
662
    return h->to;
663
  return NULL_TREE;
664
}
665
 
666
/* Insert the pair VAR's UID, DEF into the default_defs hashtable.  */
667
 
668
void
669
set_default_def (tree var, tree def)
670
{
671
  struct int_tree_map in;
672
  struct int_tree_map *h;
673
  void **loc;
674
 
675
  gcc_assert (SSA_VAR_P (var));
676
  in.uid = DECL_UID (var);
677
  if (!def && default_def (var))
678
    {
679
      loc = htab_find_slot_with_hash (default_defs, &in, DECL_UID (var), INSERT);
680
      htab_remove_elt (default_defs, *loc);
681
      return;
682
    }
683
  gcc_assert (TREE_CODE (def) == SSA_NAME);
684
  loc = htab_find_slot_with_hash (default_defs, &in, DECL_UID (var), INSERT);
685
  /* Default definition might be changed by tail call optimization.  */
686
  if (!*loc)
687
    {
688
      h = GGC_NEW (struct int_tree_map);
689
      h->uid = DECL_UID (var);
690
      h->to = def;
691
      *(struct int_tree_map **)  loc = h;
692
    }
693
   else
694
    {
695
      h = (struct int_tree_map *) *loc;
696
      h->to = def;
697
    }
698
}
699
 
700
/* Add VAR to the list of referenced variables if it isn't already there.  */
701
 
702
void
703
add_referenced_var (tree var)
704
{
705
  var_ann_t v_ann;
706
 
707
  v_ann = get_var_ann (var);
708
  gcc_assert (DECL_P (var));
709
 
710
  /* Insert VAR into the referenced_vars has table if it isn't present.  */
711
  if (referenced_var_check_and_insert (var))
712
    {
713
      /* This is the first time we found this variable, annotate it with
714
         attributes that are intrinsic to the variable.  */
715
 
716
      /* Tag's don't have DECL_INITIAL.  */
717
      if (MTAG_P (var))
718
        return;
719
 
720
      /* Scan DECL_INITIAL for pointer variables as they may contain
721
         address arithmetic referencing the address of other
722
         variables.  */
723
      if (DECL_INITIAL (var)
724
          /* Initializers of external variables are not useful to the
725
             optimizers.  */
726
          && !DECL_EXTERNAL (var)
727
          /* It's not necessary to walk the initial value of non-constant
728
             variables because it cannot be propagated by the
729
             optimizers.  */
730
          && (TREE_CONSTANT (var) || TREE_READONLY (var)))
731
        walk_tree (&DECL_INITIAL (var), find_vars_r, NULL, 0);
732
    }
733
}
734
 
735
 
736
/* Return the virtual variable associated to the non-scalar variable VAR.  */
737
 
738
tree
739
get_virtual_var (tree var)
740
{
741
  STRIP_NOPS (var);
742
 
743
  if (TREE_CODE (var) == SSA_NAME)
744
    var = SSA_NAME_VAR (var);
745
 
746
  while (TREE_CODE (var) == REALPART_EXPR || TREE_CODE (var) == IMAGPART_EXPR
747
         || handled_component_p (var))
748
    var = TREE_OPERAND (var, 0);
749
 
750
  /* Treating GIMPLE registers as virtual variables makes no sense.
751
     Also complain if we couldn't extract a _DECL out of the original
752
     expression.  */
753
  gcc_assert (SSA_VAR_P (var));
754
  gcc_assert (!is_gimple_reg (var));
755
 
756
  return var;
757
}
758
 
759
/* Mark all the non-SSA variables found in STMT's operands to be
760
   processed by update_ssa.  */
761
 
762
void
763
mark_new_vars_to_rename (tree stmt)
764
{
765
  ssa_op_iter iter;
766
  tree val;
767
  bitmap vars_in_vops_to_rename;
768
  bool found_exposed_symbol = false;
769
  int v_may_defs_before, v_may_defs_after;
770
  int v_must_defs_before, v_must_defs_after;
771
 
772
  if (TREE_CODE (stmt) == PHI_NODE)
773
    return;
774
 
775
  get_stmt_ann (stmt);
776
  vars_in_vops_to_rename = BITMAP_ALLOC (NULL);
777
 
778
  /* Before re-scanning the statement for operands, mark the existing
779
     virtual operands to be renamed again.  We do this because when new
780
     symbols are exposed, the virtual operands that were here before due to
781
     aliasing will probably be removed by the call to get_stmt_operand.
782
     Therefore, we need to flag them to be renamed beforehand.
783
 
784
     We flag them in a separate bitmap because we don't really want to
785
     rename them if there are not any newly exposed symbols in the
786
     statement operands.  */
787
  v_may_defs_before = NUM_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF);
788
  v_must_defs_before = NUM_SSA_OPERANDS (stmt, SSA_OP_VMUSTDEF);
789
 
790
  FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter,
791
                             SSA_OP_VMAYDEF | SSA_OP_VUSE | SSA_OP_VMUSTDEF)
792
    {
793
      if (!DECL_P (val))
794
        val = SSA_NAME_VAR (val);
795
      bitmap_set_bit (vars_in_vops_to_rename, DECL_UID (val));
796
    }
797
 
798
  /* Now force an operand re-scan on the statement and mark any newly
799
     exposed variables.  */
800
  update_stmt (stmt);
801
 
802
  v_may_defs_after = NUM_SSA_OPERANDS (stmt, SSA_OP_VMAYDEF);
803
  v_must_defs_after = NUM_SSA_OPERANDS (stmt, SSA_OP_VMUSTDEF);
804
 
805
  FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_ALL_OPERANDS)
806
    if (DECL_P (val))
807
      {
808
        found_exposed_symbol = true;
809
        mark_sym_for_renaming (val);
810
      }
811
 
812
  /* If we found any newly exposed symbols, or if there are fewer VDEF
813
     operands in the statement, add the variables we had set in
814
     VARS_IN_VOPS_TO_RENAME to VARS_TO_RENAME.  We need to check for
815
     vanishing VDEFs because in those cases, the names that were formerly
816
     generated by this statement are not going to be available anymore.  */
817
  if (found_exposed_symbol
818
      || v_may_defs_before > v_may_defs_after
819
      || v_must_defs_before > v_must_defs_after)
820
    mark_set_for_renaming (vars_in_vops_to_rename);
821
 
822
  BITMAP_FREE (vars_in_vops_to_rename);
823
}
824
 
825
/* Find all variables within the gimplified statement that were not previously
826
   visible to the function and add them to the referenced variables list.  */
827
 
828
static tree
829
find_new_referenced_vars_1 (tree *tp, int *walk_subtrees,
830
                            void *data ATTRIBUTE_UNUSED)
831
{
832
  tree t = *tp;
833
 
834
  if (TREE_CODE (t) == VAR_DECL && !var_ann (t))
835
    {
836
      add_referenced_var (t);
837
      mark_sym_for_renaming (t);
838
    }
839
 
840
  if (IS_TYPE_OR_DECL_P (t))
841
    *walk_subtrees = 0;
842
 
843
  return NULL;
844
}
845
 
846
void
847
find_new_referenced_vars (tree *stmt_p)
848
{
849
  walk_tree (stmt_p, find_new_referenced_vars_1, NULL, NULL);
850
}
851
 
852
 
853
/* If REF is a handled component reference for a structure, return the
854
   base variable.  The access range is delimited by bit positions *POFFSET and
855
   *POFFSET + *PMAX_SIZE.  The access size is *PSIZE bits.  If either
856
   *PSIZE or *PMAX_SIZE is -1, they could not be determined.  If *PSIZE
857
   and *PMAX_SIZE are equal, the access is non-variable.  */
858
 
859
tree
860
get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
861
                         HOST_WIDE_INT *psize,
862
                         HOST_WIDE_INT *pmax_size)
863
{
864
  HOST_WIDE_INT bitsize = -1;
865
  HOST_WIDE_INT maxsize = -1;
866
  tree size_tree = NULL_TREE;
867
  tree bit_offset = bitsize_zero_node;
868
  bool seen_variable_array_ref = false;
869
 
870
  gcc_assert (!SSA_VAR_P (exp));
871
 
872
  /* First get the final access size from just the outermost expression.  */
873
  if (TREE_CODE (exp) == COMPONENT_REF)
874
    size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
875
  else if (TREE_CODE (exp) == BIT_FIELD_REF)
876
    size_tree = TREE_OPERAND (exp, 1);
877
  else
878
    {
879
      enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
880
      if (mode == BLKmode)
881
        size_tree = TYPE_SIZE (TREE_TYPE (exp));
882
      else
883
        bitsize = GET_MODE_BITSIZE (mode);
884
    }
885
  if (size_tree != NULL_TREE)
886
    {
887
      if (! host_integerp (size_tree, 1))
888
        bitsize = -1;
889
      else
890
        bitsize = TREE_INT_CST_LOW (size_tree);
891
    }
892
 
893
  /* Initially, maxsize is the same as the accessed element size.
894
     In the following it will only grow (or become -1).  */
895
  maxsize = bitsize;
896
 
897
  /* Compute cumulative bit-offset for nested component-refs and array-refs,
898
     and find the ultimate containing object.  */
899
  while (1)
900
    {
901
      switch (TREE_CODE (exp))
902
        {
903
        case BIT_FIELD_REF:
904
          bit_offset = size_binop (PLUS_EXPR, bit_offset,
905
                                   TREE_OPERAND (exp, 2));
906
          break;
907
 
908
        case COMPONENT_REF:
909
          {
910
            tree field = TREE_OPERAND (exp, 1);
911
            tree this_offset = component_ref_field_offset (exp);
912
 
913
            if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
914
              {
915
                this_offset = size_binop (MULT_EXPR,
916
                                          fold_convert (bitsizetype,
917
                                                        this_offset),
918
                                          bitsize_unit_node);
919
                bit_offset = size_binop (PLUS_EXPR,
920
                                         bit_offset, this_offset);
921
                bit_offset = size_binop (PLUS_EXPR, bit_offset,
922
                                         DECL_FIELD_BIT_OFFSET (field));
923
              }
924
            else
925
              {
926
                tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
927
                /* We need to adjust maxsize to the whole structure bitsize.
928
                   But we can subtract any constant offset seen sofar,
929
                   because that would get us out of the structure otherwise.  */
930
                if (maxsize != -1
931
                    && csize && host_integerp (csize, 1))
932
                  {
933
                    maxsize = (TREE_INT_CST_LOW (csize)
934
                               - TREE_INT_CST_LOW (bit_offset));
935
                  }
936
                else
937
                  maxsize = -1;
938
              }
939
          }
940
          break;
941
 
942
        case ARRAY_REF:
943
        case ARRAY_RANGE_REF:
944
          {
945
            tree index = TREE_OPERAND (exp, 1);
946
            tree low_bound = array_ref_low_bound (exp);
947
            tree unit_size = array_ref_element_size (exp);
948
 
949
            if (! integer_zerop (low_bound))
950
              index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
951
                                   index, low_bound);
952
            index = size_binop (MULT_EXPR,
953
                                fold_convert (sizetype, index), unit_size);
954
            if (TREE_CODE (index) == INTEGER_CST)
955
              {
956
                index = size_binop (MULT_EXPR,
957
                                    fold_convert (bitsizetype, index),
958
                                    bitsize_unit_node);
959
                bit_offset = size_binop (PLUS_EXPR, bit_offset, index);
960
 
961
                /* An array ref with a constant index up in the structure
962
                   hierarchy will constrain the size of any variable array ref
963
                   lower in the access hierarchy.  */
964
                seen_variable_array_ref = false;
965
              }
966
            else
967
              {
968
                tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
969
                /* We need to adjust maxsize to the whole array bitsize.
970
                   But we can subtract any constant offset seen sofar,
971
                   because that would get us outside of the array otherwise.  */
972
                if (maxsize != -1
973
                    && asize && host_integerp (asize, 1))
974
                  {
975
                    maxsize = (TREE_INT_CST_LOW (asize)
976
                               - TREE_INT_CST_LOW (bit_offset));
977
                  }
978
                else
979
                  maxsize = -1;
980
 
981
                /* Remember that we have seen an array ref with a variable
982
                   index.  */
983
                seen_variable_array_ref = true;
984
              }
985
          }
986
          break;
987
 
988
        case REALPART_EXPR:
989
          break;
990
 
991
        case IMAGPART_EXPR:
992
          bit_offset = size_binop (PLUS_EXPR, bit_offset,
993
                                   bitsize_int (bitsize));
994
          break;
995
 
996
        case VIEW_CONVERT_EXPR:
997
          /* ???  We probably should give up here and bail out.  */
998
          break;
999
 
1000
        default:
1001
          goto done;
1002
        }
1003
 
1004
      exp = TREE_OPERAND (exp, 0);
1005
    }
1006
 done:
1007
 
1008
  /* We need to deal with variable arrays ending structures such as
1009
       struct { int length; int a[1]; } x;           x.a[d]
1010
       struct { struct { int a; int b; } a[1]; } x;  x.a[d].a
1011
       struct { struct { int a[1]; } a[1]; } x;      x.a[0][d], x.a[d][0]
1012
     where we do not know maxsize for variable index accesses to
1013
     the array.  The simplest way to conservatively deal with this
1014
     is to punt in the case that offset + maxsize reaches the
1015
     base type boundary.  */
1016
  if (seen_variable_array_ref
1017
      && maxsize != -1
1018
      && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1)
1019
      && TREE_INT_CST_LOW (bit_offset) + maxsize
1020
         == TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp))))
1021
    maxsize = -1;
1022
 
1023
  /* ???  Due to negative offsets in ARRAY_REF we can end up with
1024
     negative bit_offset here.  We might want to store a zero offset
1025
     in this case.  */
1026
  *poffset = TREE_INT_CST_LOW (bit_offset);
1027
  *psize = bitsize;
1028
  *pmax_size = maxsize;
1029
 
1030
  return exp;
1031
}

powered by: WebSVN 2.1.0

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