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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [cfgexpand.c] - Blame information for rev 858

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

Line No. Rev Author Line
1 38 julius
/* A pass for lowering trees to RTL.
2
   Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
3
 
4
This file is part of GCC.
5
 
6
GCC is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3, or (at your option)
9
any later version.
10
 
11
GCC is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GCC; see the file COPYING3.  If not see
18
<http://www.gnu.org/licenses/>.  */
19
 
20
#include "config.h"
21
#include "system.h"
22
#include "coretypes.h"
23
#include "tm.h"
24
#include "tree.h"
25
#include "rtl.h"
26
#include "tm_p.h"
27
#include "basic-block.h"
28
#include "function.h"
29
#include "expr.h"
30
#include "langhooks.h"
31
#include "tree-flow.h"
32
#include "timevar.h"
33
#include "tree-dump.h"
34
#include "tree-pass.h"
35
#include "except.h"
36
#include "flags.h"
37
#include "diagnostic.h"
38
#include "toplev.h"
39
#include "debug.h"
40
#include "params.h"
41
 
42
/* Verify that there is exactly single jump instruction since last and attach
43
   REG_BR_PROB note specifying probability.
44
   ??? We really ought to pass the probability down to RTL expanders and let it
45
   re-distribute it when the conditional expands into multiple conditionals.
46
   This is however difficult to do.  */
47
static void
48
add_reg_br_prob_note (rtx last, int probability)
49
{
50
  if (profile_status == PROFILE_ABSENT)
51
    return;
52
  for (last = NEXT_INSN (last); last && NEXT_INSN (last); last = NEXT_INSN (last))
53
    if (JUMP_P (last))
54
      {
55
        /* It is common to emit condjump-around-jump sequence when we don't know
56
           how to reverse the conditional.  Special case this.  */
57
        if (!any_condjump_p (last)
58
            || !JUMP_P (NEXT_INSN (last))
59
            || !simplejump_p (NEXT_INSN (last))
60
            || !NEXT_INSN (NEXT_INSN (last))
61
            || !BARRIER_P (NEXT_INSN (NEXT_INSN (last)))
62
            || !NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))
63
            || !LABEL_P (NEXT_INSN (NEXT_INSN (NEXT_INSN (last))))
64
            || NEXT_INSN (NEXT_INSN (NEXT_INSN (NEXT_INSN (last)))))
65
          goto failed;
66
        gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
67
        REG_NOTES (last)
68
          = gen_rtx_EXPR_LIST (REG_BR_PROB,
69
                               GEN_INT (REG_BR_PROB_BASE - probability),
70
                               REG_NOTES (last));
71
        return;
72
      }
73
  if (!last || !JUMP_P (last) || !any_condjump_p (last))
74
    goto failed;
75
  gcc_assert (!find_reg_note (last, REG_BR_PROB, 0));
76
  REG_NOTES (last)
77
    = gen_rtx_EXPR_LIST (REG_BR_PROB,
78
                         GEN_INT (probability), REG_NOTES (last));
79
  return;
80
failed:
81
  if (dump_file)
82
    fprintf (dump_file, "Failed to add probability note\n");
83
}
84
 
85
 
86
#ifndef LOCAL_ALIGNMENT
87
#define LOCAL_ALIGNMENT(TYPE, ALIGNMENT) ALIGNMENT
88
#endif
89
 
90
#ifndef STACK_ALIGNMENT_NEEDED
91
#define STACK_ALIGNMENT_NEEDED 1
92
#endif
93
 
94
 
95
/* This structure holds data relevant to one variable that will be
96
   placed in a stack slot.  */
97
struct stack_var
98
{
99
  /* The Variable.  */
100
  tree decl;
101
 
102
  /* The offset of the variable.  During partitioning, this is the
103
     offset relative to the partition.  After partitioning, this
104
     is relative to the stack frame.  */
105
  HOST_WIDE_INT offset;
106
 
107
  /* Initially, the size of the variable.  Later, the size of the partition,
108
     if this variable becomes it's partition's representative.  */
109
  HOST_WIDE_INT size;
110
 
111
  /* The *byte* alignment required for this variable.  Or as, with the
112
     size, the alignment for this partition.  */
113
  unsigned int alignb;
114
 
115
  /* The partition representative.  */
116
  size_t representative;
117
 
118
  /* The next stack variable in the partition, or EOC.  */
119
  size_t next;
120
};
121
 
122
#define EOC  ((size_t)-1)
123
 
124
/* We have an array of such objects while deciding allocation.  */
125
static struct stack_var *stack_vars;
126
static size_t stack_vars_alloc;
127
static size_t stack_vars_num;
128
 
129
/* An array of indicies such that stack_vars[stack_vars_sorted[i]].size
130
   is non-decreasing.  */
131
static size_t *stack_vars_sorted;
132
 
133
/* We have an interference graph between such objects.  This graph
134
   is lower triangular.  */
135
static bool *stack_vars_conflict;
136
static size_t stack_vars_conflict_alloc;
137
 
138
/* The phase of the stack frame.  This is the known misalignment of
139
   virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY.  That is,
140
   (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0.  */
141
static int frame_phase;
142
 
143
/* Used during expand_used_vars to remember if we saw any decls for
144
   which we'd like to enable stack smashing protection.  */
145
static bool has_protected_decls;
146
 
147
/* Used during expand_used_vars.  Remember if we say a character buffer
148
   smaller than our cutoff threshold.  Used for -Wstack-protector.  */
149
static bool has_short_buffer;
150
 
151
/* Discover the byte alignment to use for DECL.  Ignore alignment
152
   we can't do with expected alignment of the stack boundary.  */
153
 
154
static unsigned int
155
get_decl_align_unit (tree decl)
156
{
157
  unsigned int align;
158
 
159
  align = DECL_ALIGN (decl);
160
  align = LOCAL_ALIGNMENT (TREE_TYPE (decl), align);
161
  if (align > PREFERRED_STACK_BOUNDARY)
162
    align = PREFERRED_STACK_BOUNDARY;
163
  if (cfun->stack_alignment_needed < align)
164
    cfun->stack_alignment_needed = align;
165
 
166
  return align / BITS_PER_UNIT;
167
}
168
 
169
/* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
170
   Return the frame offset.  */
171
 
172
static HOST_WIDE_INT
173
alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
174
{
175
  HOST_WIDE_INT offset, new_frame_offset;
176
 
177
  new_frame_offset = frame_offset;
178
  if (FRAME_GROWS_DOWNWARD)
179
    {
180
      new_frame_offset -= size + frame_phase;
181
      new_frame_offset &= -align;
182
      new_frame_offset += frame_phase;
183
      offset = new_frame_offset;
184
    }
185
  else
186
    {
187
      new_frame_offset -= frame_phase;
188
      new_frame_offset += align - 1;
189
      new_frame_offset &= -align;
190
      new_frame_offset += frame_phase;
191
      offset = new_frame_offset;
192
      new_frame_offset += size;
193
    }
194
  frame_offset = new_frame_offset;
195
 
196
  if (frame_offset_overflow (frame_offset, cfun->decl))
197
    frame_offset = offset = 0;
198
 
199
  return offset;
200
}
201
 
202
/* Accumulate DECL into STACK_VARS.  */
203
 
204
static void
205
add_stack_var (tree decl)
206
{
207
  if (stack_vars_num >= stack_vars_alloc)
208
    {
209
      if (stack_vars_alloc)
210
        stack_vars_alloc = stack_vars_alloc * 3 / 2;
211
      else
212
        stack_vars_alloc = 32;
213
      stack_vars
214
        = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
215
    }
216
  stack_vars[stack_vars_num].decl = decl;
217
  stack_vars[stack_vars_num].offset = 0;
218
  stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
219
  stack_vars[stack_vars_num].alignb = get_decl_align_unit (decl);
220
 
221
  /* All variables are initially in their own partition.  */
222
  stack_vars[stack_vars_num].representative = stack_vars_num;
223
  stack_vars[stack_vars_num].next = EOC;
224
 
225
  /* Ensure that this decl doesn't get put onto the list twice.  */
226
  SET_DECL_RTL (decl, pc_rtx);
227
 
228
  stack_vars_num++;
229
}
230
 
231
/* Compute the linear index of a lower-triangular coordinate (I, J).  */
232
 
233
static size_t
234
triangular_index (size_t i, size_t j)
235
{
236
  if (i < j)
237
    {
238
      size_t t;
239
      t = i, i = j, j = t;
240
    }
241
  return (i * (i + 1)) / 2 + j;
242
}
243
 
244
/* Ensure that STACK_VARS_CONFLICT is large enough for N objects.  */
245
 
246
static void
247
resize_stack_vars_conflict (size_t n)
248
{
249
  size_t size = triangular_index (n-1, n-1) + 1;
250
 
251
  if (size <= stack_vars_conflict_alloc)
252
    return;
253
 
254
  stack_vars_conflict = XRESIZEVEC (bool, stack_vars_conflict, size);
255
  memset (stack_vars_conflict + stack_vars_conflict_alloc, 0,
256
          (size - stack_vars_conflict_alloc) * sizeof (bool));
257
  stack_vars_conflict_alloc = size;
258
}
259
 
260
/* Make the decls associated with luid's X and Y conflict.  */
261
 
262
static void
263
add_stack_var_conflict (size_t x, size_t y)
264
{
265
  size_t index = triangular_index (x, y);
266
  gcc_assert (index < stack_vars_conflict_alloc);
267
  stack_vars_conflict[index] = true;
268
}
269
 
270
/* Check whether the decls associated with luid's X and Y conflict.  */
271
 
272
static bool
273
stack_var_conflict_p (size_t x, size_t y)
274
{
275
  size_t index = triangular_index (x, y);
276
  gcc_assert (index < stack_vars_conflict_alloc);
277
  return stack_vars_conflict[index];
278
}
279
 
280
/* Returns true if TYPE is or contains a union type.  */
281
 
282
static bool
283
aggregate_contains_union_type (tree type)
284
{
285
  tree field;
286
 
287
  if (TREE_CODE (type) == UNION_TYPE
288
      || TREE_CODE (type) == QUAL_UNION_TYPE)
289
    return true;
290
  if (TREE_CODE (type) == ARRAY_TYPE)
291
    return aggregate_contains_union_type (TREE_TYPE (type));
292
  if (TREE_CODE (type) != RECORD_TYPE)
293
    return false;
294
 
295
  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
296
    if (TREE_CODE (field) == FIELD_DECL)
297
      if (aggregate_contains_union_type (TREE_TYPE (field)))
298
        return true;
299
 
300
  return false;
301
}
302
 
303
/* A subroutine of expand_used_vars.  If two variables X and Y have alias
304
   sets that do not conflict, then do add a conflict for these variables
305
   in the interference graph.  We also need to make sure to add conflicts
306
   for union containing structures.  Else RTL alias analysis comes along
307
   and due to type based aliasing rules decides that for two overlapping
308
   union temporaries { short s; int i; } accesses to the same mem through
309
   different types may not alias and happily reorders stores across
310
   life-time boundaries of the temporaries (See PR25654).
311
   We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P.  */
312
 
313
static void
314
add_alias_set_conflicts (void)
315
{
316
  size_t i, j, n = stack_vars_num;
317
 
318
  for (i = 0; i < n; ++i)
319
    {
320
      tree type_i = TREE_TYPE (stack_vars[i].decl);
321
      bool aggr_i = AGGREGATE_TYPE_P (type_i);
322
      bool contains_union;
323
 
324
      contains_union = aggregate_contains_union_type (type_i);
325
      for (j = 0; j < i; ++j)
326
        {
327
          tree type_j = TREE_TYPE (stack_vars[j].decl);
328
          bool aggr_j = AGGREGATE_TYPE_P (type_j);
329
          if (aggr_i != aggr_j
330
              /* Either the objects conflict by means of type based
331
                 aliasing rules, or we need to add a conflict.  */
332
              || !objects_must_conflict_p (type_i, type_j)
333
              /* In case the types do not conflict ensure that access
334
                 to elements will conflict.  In case of unions we have
335
                 to be careful as type based aliasing rules may say
336
                 access to the same memory does not conflict.  So play
337
                 safe and add a conflict in this case.  */
338
              || contains_union)
339
            add_stack_var_conflict (i, j);
340
        }
341
    }
342
}
343
 
344
/* A subroutine of partition_stack_vars.  A comparison function for qsort,
345
   sorting an array of indicies by the size of the object.  */
346
 
347
static int
348
stack_var_size_cmp (const void *a, const void *b)
349
{
350
  HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
351
  HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
352
  unsigned int uida = DECL_UID (stack_vars[*(const size_t *)a].decl);
353
  unsigned int uidb = DECL_UID (stack_vars[*(const size_t *)b].decl);
354
 
355
  if (sa < sb)
356
    return -1;
357
  if (sa > sb)
358
    return 1;
359
  /* For stack variables of the same size use the uid of the decl
360
     to make the sort stable.  */
361
  if (uida < uidb)
362
    return -1;
363
  if (uida > uidb)
364
    return 1;
365
  return 0;
366
}
367
 
368
/* A subroutine of partition_stack_vars.  The UNION portion of a UNION/FIND
369
   partitioning algorithm.  Partitions A and B are known to be non-conflicting.
370
   Merge them into a single partition A.
371
 
372
   At the same time, add OFFSET to all variables in partition B.  At the end
373
   of the partitioning process we've have a nice block easy to lay out within
374
   the stack frame.  */
375
 
376
static void
377
union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
378
{
379
  size_t i, last;
380
 
381
  /* Update each element of partition B with the given offset,
382
     and merge them into partition A.  */
383
  for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
384
    {
385
      stack_vars[i].offset += offset;
386
      stack_vars[i].representative = a;
387
    }
388
  stack_vars[last].next = stack_vars[a].next;
389
  stack_vars[a].next = b;
390
 
391
  /* Update the required alignment of partition A to account for B.  */
392
  if (stack_vars[a].alignb < stack_vars[b].alignb)
393
    stack_vars[a].alignb = stack_vars[b].alignb;
394
 
395
  /* Update the interference graph and merge the conflicts.  */
396
  for (last = stack_vars_num, i = 0; i < last; ++i)
397
    if (stack_var_conflict_p (b, i))
398
      add_stack_var_conflict (a, i);
399
}
400
 
401
/* A subroutine of expand_used_vars.  Binpack the variables into
402
   partitions constrained by the interference graph.  The overall
403
   algorithm used is as follows:
404
 
405
        Sort the objects by size.
406
        For each object A {
407
          S = size(A)
408
          O = 0
409
          loop {
410
            Look for the largest non-conflicting object B with size <= S.
411
            UNION (A, B)
412
            offset(B) = O
413
            O += size(B)
414
            S -= size(B)
415
          }
416
        }
417
*/
418
 
419
static void
420
partition_stack_vars (void)
421
{
422
  size_t si, sj, n = stack_vars_num;
423
 
424
  stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
425
  for (si = 0; si < n; ++si)
426
    stack_vars_sorted[si] = si;
427
 
428
  if (n == 1)
429
    return;
430
 
431
  qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
432
 
433
  /* Special case: detect when all variables conflict, and thus we can't
434
     do anything during the partitioning loop.  It isn't uncommon (with
435
     C code at least) to declare all variables at the top of the function,
436
     and if we're not inlining, then all variables will be in the same scope.
437
     Take advantage of very fast libc routines for this scan.  */
438
  gcc_assert (sizeof(bool) == sizeof(char));
439
  if (memchr (stack_vars_conflict, false, stack_vars_conflict_alloc) == NULL)
440
    return;
441
 
442
  for (si = 0; si < n; ++si)
443
    {
444
      size_t i = stack_vars_sorted[si];
445
      HOST_WIDE_INT isize = stack_vars[i].size;
446
      HOST_WIDE_INT offset = 0;
447
 
448
      for (sj = si; sj-- > 0; )
449
        {
450
          size_t j = stack_vars_sorted[sj];
451
          HOST_WIDE_INT jsize = stack_vars[j].size;
452
          unsigned int jalign = stack_vars[j].alignb;
453
 
454
          /* Ignore objects that aren't partition representatives.  */
455
          if (stack_vars[j].representative != j)
456
            continue;
457
 
458
          /* Ignore objects too large for the remaining space.  */
459
          if (isize < jsize)
460
            continue;
461
 
462
          /* Ignore conflicting objects.  */
463
          if (stack_var_conflict_p (i, j))
464
            continue;
465
 
466
          /* Refine the remaining space check to include alignment.  */
467
          if (offset & (jalign - 1))
468
            {
469
              HOST_WIDE_INT toff = offset;
470
              toff += jalign - 1;
471
              toff &= -(HOST_WIDE_INT)jalign;
472
              if (isize - (toff - offset) < jsize)
473
                continue;
474
 
475
              isize -= toff - offset;
476
              offset = toff;
477
            }
478
 
479
          /* UNION the objects, placing J at OFFSET.  */
480
          union_stack_vars (i, j, offset);
481
 
482
          isize -= jsize;
483
          if (isize == 0)
484
            break;
485
        }
486
    }
487
}
488
 
489
/* A debugging aid for expand_used_vars.  Dump the generated partitions.  */
490
 
491
static void
492
dump_stack_var_partition (void)
493
{
494
  size_t si, i, j, n = stack_vars_num;
495
 
496
  for (si = 0; si < n; ++si)
497
    {
498
      i = stack_vars_sorted[si];
499
 
500
      /* Skip variables that aren't partition representatives, for now.  */
501
      if (stack_vars[i].representative != i)
502
        continue;
503
 
504
      fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
505
               " align %u\n", (unsigned long) i, stack_vars[i].size,
506
               stack_vars[i].alignb);
507
 
508
      for (j = i; j != EOC; j = stack_vars[j].next)
509
        {
510
          fputc ('\t', dump_file);
511
          print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
512
          fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
513
                   stack_vars[i].offset);
514
        }
515
    }
516
}
517
 
518
/* Assign rtl to DECL at frame offset OFFSET.  */
519
 
520
static void
521
expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
522
{
523
  HOST_WIDE_INT align;
524
  rtx x;
525
 
526
  /* If this fails, we've overflowed the stack frame.  Error nicely?  */
527
  gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
528
 
529
  x = plus_constant (virtual_stack_vars_rtx, offset);
530
  x = gen_rtx_MEM (DECL_MODE (decl), x);
531
 
532
  /* Set alignment we actually gave this decl.  */
533
  offset -= frame_phase;
534
  align = offset & -offset;
535
  align *= BITS_PER_UNIT;
536
  if (align > STACK_BOUNDARY || align == 0)
537
    align = STACK_BOUNDARY;
538
  DECL_ALIGN (decl) = align;
539
  DECL_USER_ALIGN (decl) = 0;
540
 
541
  set_mem_attributes (x, decl, true);
542
  SET_DECL_RTL (decl, x);
543
}
544
 
545
/* A subroutine of expand_used_vars.  Give each partition representative
546
   a unique location within the stack frame.  Update each partition member
547
   with that location.  */
548
 
549
static void
550
expand_stack_vars (bool (*pred) (tree))
551
{
552
  size_t si, i, j, n = stack_vars_num;
553
 
554
  for (si = 0; si < n; ++si)
555
    {
556
      HOST_WIDE_INT offset;
557
 
558
      i = stack_vars_sorted[si];
559
 
560
      /* Skip variables that aren't partition representatives, for now.  */
561
      if (stack_vars[i].representative != i)
562
        continue;
563
 
564
      /* Skip variables that have already had rtl assigned.  See also
565
         add_stack_var where we perpetrate this pc_rtx hack.  */
566
      if (DECL_RTL (stack_vars[i].decl) != pc_rtx)
567
        continue;
568
 
569
      /* Check the predicate to see whether this variable should be
570
         allocated in this pass.  */
571
      if (pred && !pred (stack_vars[i].decl))
572
        continue;
573
 
574
      offset = alloc_stack_frame_space (stack_vars[i].size,
575
                                        stack_vars[i].alignb);
576
 
577
      /* Create rtl for each variable based on their location within the
578
         partition.  */
579
      for (j = i; j != EOC; j = stack_vars[j].next)
580
        expand_one_stack_var_at (stack_vars[j].decl,
581
                                 stack_vars[j].offset + offset);
582
    }
583
}
584
 
585
/* A subroutine of expand_one_var.  Called to immediately assign rtl
586
   to a variable to be allocated in the stack frame.  */
587
 
588
static void
589
expand_one_stack_var (tree var)
590
{
591
  HOST_WIDE_INT size, offset, align;
592
 
593
  size = tree_low_cst (DECL_SIZE_UNIT (var), 1);
594
  align = get_decl_align_unit (var);
595
  offset = alloc_stack_frame_space (size, align);
596
 
597
  expand_one_stack_var_at (var, offset);
598
}
599
 
600
/* A subroutine of expand_one_var.  Called to assign rtl
601
   to a TREE_STATIC VAR_DECL.  */
602
 
603
static void
604
expand_one_static_var (tree var)
605
{
606
  /* In unit-at-a-time all the static variables are expanded at the end
607
     of compilation process.  */
608
  if (flag_unit_at_a_time)
609
    return;
610
  /* If this is an inlined copy of a static local variable,
611
     look up the original.  */
612
  var = DECL_ORIGIN (var);
613
 
614
  /* If we've already processed this variable because of that, do nothing.  */
615
  if (TREE_ASM_WRITTEN (var))
616
    return;
617
 
618
  /* Give the front end a chance to do whatever.  In practice, this is
619
     resolving duplicate names for IMA in C.  */
620
  if (lang_hooks.expand_decl (var))
621
    return;
622
 
623
  /* Otherwise, just emit the variable.  */
624
  rest_of_decl_compilation (var, 0, 0);
625
}
626
 
627
/* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
628
   that will reside in a hard register.  */
629
 
630
static void
631
expand_one_hard_reg_var (tree var)
632
{
633
  rest_of_decl_compilation (var, 0, 0);
634
}
635
 
636
/* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
637
   that will reside in a pseudo register.  */
638
 
639
static void
640
expand_one_register_var (tree var)
641
{
642
  tree type = TREE_TYPE (var);
643
  int unsignedp = TYPE_UNSIGNED (type);
644
  enum machine_mode reg_mode
645
    = promote_mode (type, DECL_MODE (var), &unsignedp, 0);
646
  rtx x = gen_reg_rtx (reg_mode);
647
 
648
  SET_DECL_RTL (var, x);
649
 
650
  /* Note if the object is a user variable.  */
651
  if (!DECL_ARTIFICIAL (var))
652
    {
653
      mark_user_reg (x);
654
 
655
      /* Trust user variables which have a pointer type to really
656
         be pointers.  Do not trust compiler generated temporaries
657
         as our type system is totally busted as it relates to
658
         pointer arithmetic which translates into lots of compiler
659
         generated objects with pointer types, but which are not really
660
         pointers.  */
661
      if (POINTER_TYPE_P (type))
662
        mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (var))));
663
    }
664
}
665
 
666
/* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL that
667
   has some associated error, e.g. its type is error-mark.  We just need
668
   to pick something that won't crash the rest of the compiler.  */
669
 
670
static void
671
expand_one_error_var (tree var)
672
{
673
  enum machine_mode mode = DECL_MODE (var);
674
  rtx x;
675
 
676
  if (mode == BLKmode)
677
    x = gen_rtx_MEM (BLKmode, const0_rtx);
678
  else if (mode == VOIDmode)
679
    x = const0_rtx;
680
  else
681
    x = gen_reg_rtx (mode);
682
 
683
  SET_DECL_RTL (var, x);
684
}
685
 
686
/* A subroutine of expand_one_var.  VAR is a variable that will be
687
   allocated to the local stack frame.  Return true if we wish to
688
   add VAR to STACK_VARS so that it will be coalesced with other
689
   variables.  Return false to allocate VAR immediately.
690
 
691
   This function is used to reduce the number of variables considered
692
   for coalescing, which reduces the size of the quadratic problem.  */
693
 
694
static bool
695
defer_stack_allocation (tree var, bool toplevel)
696
{
697
  /* If stack protection is enabled, *all* stack variables must be deferred,
698
     so that we can re-order the strings to the top of the frame.  */
699
  if (flag_stack_protect)
700
    return true;
701
 
702
  /* Variables in the outermost scope automatically conflict with
703
     every other variable.  The only reason to want to defer them
704
     at all is that, after sorting, we can more efficiently pack
705
     small variables in the stack frame.  Continue to defer at -O2.  */
706
  if (toplevel && optimize < 2)
707
    return false;
708
 
709
  /* Without optimization, *most* variables are allocated from the
710
     stack, which makes the quadratic problem large exactly when we
711
     want compilation to proceed as quickly as possible.  On the
712
     other hand, we don't want the function's stack frame size to
713
     get completely out of hand.  So we avoid adding scalars and
714
     "small" aggregates to the list at all.  */
715
  if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
716
    return false;
717
 
718
  return true;
719
}
720
 
721
/* A subroutine of expand_used_vars.  Expand one variable according to
722
   its flavor.  Variables to be placed on the stack are not actually
723
   expanded yet, merely recorded.  */
724
 
725
static void
726
expand_one_var (tree var, bool toplevel)
727
{
728
  if (TREE_CODE (var) != VAR_DECL)
729
    lang_hooks.expand_decl (var);
730
  else if (DECL_EXTERNAL (var))
731
    ;
732
  else if (DECL_HAS_VALUE_EXPR_P (var))
733
    ;
734
  else if (TREE_STATIC (var))
735
    expand_one_static_var (var);
736
  else if (DECL_RTL_SET_P (var))
737
    ;
738
  else if (TREE_TYPE (var) == error_mark_node)
739
    expand_one_error_var (var);
740
  else if (DECL_HARD_REGISTER (var))
741
    expand_one_hard_reg_var (var);
742
  else if (use_register_for_decl (var))
743
    expand_one_register_var (var);
744
  else if (defer_stack_allocation (var, toplevel))
745
    add_stack_var (var);
746
  else
747
    expand_one_stack_var (var);
748
}
749
 
750
/* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
751
   expanding variables.  Those variables that can be put into registers
752
   are allocated pseudos; those that can't are put on the stack.
753
 
754
   TOPLEVEL is true if this is the outermost BLOCK.  */
755
 
756
static void
757
expand_used_vars_for_block (tree block, bool toplevel)
758
{
759
  size_t i, j, old_sv_num, this_sv_num, new_sv_num;
760
  tree t;
761
 
762
  old_sv_num = toplevel ? 0 : stack_vars_num;
763
 
764
  /* Expand all variables at this level.  */
765
  for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
766
    if (TREE_USED (t)
767
        /* Force local static variables to be output when marked by
768
           used attribute.  For unit-at-a-time, cgraph code already takes
769
           care of this.  */
770
        || (!flag_unit_at_a_time && TREE_STATIC (t)
771
            && DECL_PRESERVE_P (t)))
772
      expand_one_var (t, toplevel);
773
 
774
  this_sv_num = stack_vars_num;
775
 
776
  /* Expand all variables at containing levels.  */
777
  for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
778
    expand_used_vars_for_block (t, false);
779
 
780
  /* Since we do not track exact variable lifetimes (which is not even
781
     possible for variables whose address escapes), we mirror the block
782
     tree in the interference graph.  Here we cause all variables at this
783
     level, and all sublevels, to conflict.  Do make certain that a
784
     variable conflicts with itself.  */
785
  if (old_sv_num < this_sv_num)
786
    {
787
      new_sv_num = stack_vars_num;
788
      resize_stack_vars_conflict (new_sv_num);
789
 
790
      for (i = old_sv_num; i < new_sv_num; ++i)
791
        for (j = i < this_sv_num ? i+1 : this_sv_num; j-- > old_sv_num ;)
792
          add_stack_var_conflict (i, j);
793
    }
794
}
795
 
796
/* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
797
   and clear TREE_USED on all local variables.  */
798
 
799
static void
800
clear_tree_used (tree block)
801
{
802
  tree t;
803
 
804
  for (t = BLOCK_VARS (block); t ; t = TREE_CHAIN (t))
805
    /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
806
      TREE_USED (t) = 0;
807
 
808
  for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
809
    clear_tree_used (t);
810
}
811
 
812
/* Examine TYPE and determine a bit mask of the following features.  */
813
 
814
#define SPCT_HAS_LARGE_CHAR_ARRAY       1
815
#define SPCT_HAS_SMALL_CHAR_ARRAY       2
816
#define SPCT_HAS_ARRAY                  4
817
#define SPCT_HAS_AGGREGATE              8
818
 
819
static unsigned int
820
stack_protect_classify_type (tree type)
821
{
822
  unsigned int ret = 0;
823
  tree t;
824
 
825
  switch (TREE_CODE (type))
826
    {
827
    case ARRAY_TYPE:
828
      t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
829
      if (t == char_type_node
830
          || t == signed_char_type_node
831
          || t == unsigned_char_type_node)
832
        {
833
          unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
834
          unsigned HOST_WIDE_INT len;
835
 
836
          if (!TYPE_SIZE_UNIT (type)
837
              || !host_integerp (TYPE_SIZE_UNIT (type), 1))
838
            len = max;
839
          else
840
            len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
841
 
842
          if (len < max)
843
            ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
844
          else
845
            ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
846
        }
847
      else
848
        ret = SPCT_HAS_ARRAY;
849
      break;
850
 
851
    case UNION_TYPE:
852
    case QUAL_UNION_TYPE:
853
    case RECORD_TYPE:
854
      ret = SPCT_HAS_AGGREGATE;
855
      for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
856
        if (TREE_CODE (t) == FIELD_DECL)
857
          ret |= stack_protect_classify_type (TREE_TYPE (t));
858
      break;
859
 
860
    default:
861
      break;
862
    }
863
 
864
  return ret;
865
}
866
 
867
/* Return nonzero if DECL should be segregated into the "vulnerable" upper
868
   part of the local stack frame.  Remember if we ever return nonzero for
869
   any variable in this function.  The return value is the phase number in
870
   which the variable should be allocated.  */
871
 
872
static int
873
stack_protect_decl_phase (tree decl)
874
{
875
  unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
876
  int ret = 0;
877
 
878
  if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
879
    has_short_buffer = true;
880
 
881
  if (flag_stack_protect == 2)
882
    {
883
      if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
884
          && !(bits & SPCT_HAS_AGGREGATE))
885
        ret = 1;
886
      else if (bits & SPCT_HAS_ARRAY)
887
        ret = 2;
888
    }
889
  else
890
    ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
891
 
892
  if (ret)
893
    has_protected_decls = true;
894
 
895
  return ret;
896
}
897
 
898
/* Two helper routines that check for phase 1 and phase 2.  These are used
899
   as callbacks for expand_stack_vars.  */
900
 
901
static bool
902
stack_protect_decl_phase_1 (tree decl)
903
{
904
  return stack_protect_decl_phase (decl) == 1;
905
}
906
 
907
static bool
908
stack_protect_decl_phase_2 (tree decl)
909
{
910
  return stack_protect_decl_phase (decl) == 2;
911
}
912
 
913
/* Ensure that variables in different stack protection phases conflict
914
   so that they are not merged and share the same stack slot.  */
915
 
916
static void
917
add_stack_protection_conflicts (void)
918
{
919
  size_t i, j, n = stack_vars_num;
920
  unsigned char *phase;
921
 
922
  phase = XNEWVEC (unsigned char, n);
923
  for (i = 0; i < n; ++i)
924
    phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
925
 
926
  for (i = 0; i < n; ++i)
927
    {
928
      unsigned char ph_i = phase[i];
929
      for (j = 0; j < i; ++j)
930
        if (ph_i != phase[j])
931
          add_stack_var_conflict (i, j);
932
    }
933
 
934
  XDELETEVEC (phase);
935
}
936
 
937
/* Create a decl for the guard at the top of the stack frame.  */
938
 
939
static void
940
create_stack_guard (void)
941
{
942
  tree guard = build_decl (VAR_DECL, NULL, ptr_type_node);
943
  TREE_THIS_VOLATILE (guard) = 1;
944
  TREE_USED (guard) = 1;
945
  expand_one_stack_var (guard);
946
  cfun->stack_protect_guard = guard;
947
}
948
 
949
/* Expand all variables used in the function.  */
950
 
951
static void
952
expand_used_vars (void)
953
{
954
  tree t, outer_block = DECL_INITIAL (current_function_decl);
955
 
956
  /* Compute the phase of the stack frame for this function.  */
957
  {
958
    int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
959
    int off = STARTING_FRAME_OFFSET % align;
960
    frame_phase = off ? align - off : 0;
961
  }
962
 
963
  /* Set TREE_USED on all variables in the unexpanded_var_list.  */
964
  for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
965
    TREE_USED (TREE_VALUE (t)) = 1;
966
 
967
  /* Clear TREE_USED on all variables associated with a block scope.  */
968
  clear_tree_used (outer_block);
969
 
970
  /* Initialize local stack smashing state.  */
971
  has_protected_decls = false;
972
  has_short_buffer = false;
973
 
974
  /* At this point all variables on the unexpanded_var_list with TREE_USED
975
     set are not associated with any block scope.  Lay them out.  */
976
  for (t = cfun->unexpanded_var_list; t; t = TREE_CHAIN (t))
977
    {
978
      tree var = TREE_VALUE (t);
979
      bool expand_now = false;
980
 
981
      /* We didn't set a block for static or extern because it's hard
982
         to tell the difference between a global variable (re)declared
983
         in a local scope, and one that's really declared there to
984
         begin with.  And it doesn't really matter much, since we're
985
         not giving them stack space.  Expand them now.  */
986
      if (TREE_STATIC (var) || DECL_EXTERNAL (var))
987
        expand_now = true;
988
 
989
      /* Any variable that could have been hoisted into an SSA_NAME
990
         will have been propagated anywhere the optimizers chose,
991
         i.e. not confined to their original block.  Allocate them
992
         as if they were defined in the outermost scope.  */
993
      else if (is_gimple_reg (var))
994
        expand_now = true;
995
 
996
      /* If the variable is not associated with any block, then it
997
         was created by the optimizers, and could be live anywhere
998
         in the function.  */
999
      else if (TREE_USED (var))
1000
        expand_now = true;
1001
 
1002
      /* Finally, mark all variables on the list as used.  We'll use
1003
         this in a moment when we expand those associated with scopes.  */
1004
      TREE_USED (var) = 1;
1005
 
1006
      if (expand_now)
1007
        expand_one_var (var, true);
1008
    }
1009
  cfun->unexpanded_var_list = NULL_TREE;
1010
 
1011
  /* At this point, all variables within the block tree with TREE_USED
1012
     set are actually used by the optimized function.  Lay them out.  */
1013
  expand_used_vars_for_block (outer_block, true);
1014
 
1015
  if (stack_vars_num > 0)
1016
    {
1017
      /* Due to the way alias sets work, no variables with non-conflicting
1018
         alias sets may be assigned the same address.  Add conflicts to
1019
         reflect this.  */
1020
      add_alias_set_conflicts ();
1021
 
1022
      /* If stack protection is enabled, we don't share space between
1023
         vulnerable data and non-vulnerable data.  */
1024
      if (flag_stack_protect)
1025
        add_stack_protection_conflicts ();
1026
 
1027
      /* Now that we have collected all stack variables, and have computed a
1028
         minimal interference graph, attempt to save some stack space.  */
1029
      partition_stack_vars ();
1030
      if (dump_file)
1031
        dump_stack_var_partition ();
1032
    }
1033
 
1034
  /* There are several conditions under which we should create a
1035
     stack guard: protect-all, alloca used, protected decls present.  */
1036
  if (flag_stack_protect == 2
1037
      || (flag_stack_protect
1038
          && (current_function_calls_alloca || has_protected_decls)))
1039
    create_stack_guard ();
1040
 
1041
  /* Assign rtl to each variable based on these partitions.  */
1042
  if (stack_vars_num > 0)
1043
    {
1044
      /* Reorder decls to be protected by iterating over the variables
1045
         array multiple times, and allocating out of each phase in turn.  */
1046
      /* ??? We could probably integrate this into the qsort we did
1047
         earlier, such that we naturally see these variables first,
1048
         and thus naturally allocate things in the right order.  */
1049
      if (has_protected_decls)
1050
        {
1051
          /* Phase 1 contains only character arrays.  */
1052
          expand_stack_vars (stack_protect_decl_phase_1);
1053
 
1054
          /* Phase 2 contains other kinds of arrays.  */
1055
          if (flag_stack_protect == 2)
1056
            expand_stack_vars (stack_protect_decl_phase_2);
1057
        }
1058
 
1059
      expand_stack_vars (NULL);
1060
 
1061
      /* Free up stack variable graph data.  */
1062
      XDELETEVEC (stack_vars);
1063
      XDELETEVEC (stack_vars_sorted);
1064
      XDELETEVEC (stack_vars_conflict);
1065
      stack_vars = NULL;
1066
      stack_vars_alloc = stack_vars_num = 0;
1067
      stack_vars_conflict = NULL;
1068
      stack_vars_conflict_alloc = 0;
1069
    }
1070
 
1071
  /* If the target requires that FRAME_OFFSET be aligned, do it.  */
1072
  if (STACK_ALIGNMENT_NEEDED)
1073
    {
1074
      HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1075
      if (!FRAME_GROWS_DOWNWARD)
1076
        frame_offset += align - 1;
1077
      frame_offset &= -align;
1078
    }
1079
}
1080
 
1081
 
1082
/* If we need to produce a detailed dump, print the tree representation
1083
   for STMT to the dump file.  SINCE is the last RTX after which the RTL
1084
   generated for STMT should have been appended.  */
1085
 
1086
static void
1087
maybe_dump_rtl_for_tree_stmt (tree stmt, rtx since)
1088
{
1089
  if (dump_file && (dump_flags & TDF_DETAILS))
1090
    {
1091
      fprintf (dump_file, "\n;; ");
1092
      print_generic_expr (dump_file, stmt, TDF_SLIM);
1093
      fprintf (dump_file, "\n");
1094
 
1095
      print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1096
    }
1097
}
1098
 
1099
/* A subroutine of expand_gimple_basic_block.  Expand one COND_EXPR.
1100
   Returns a new basic block if we've terminated the current basic
1101
   block and created a new one.  */
1102
 
1103
static basic_block
1104
expand_gimple_cond_expr (basic_block bb, tree stmt)
1105
{
1106
  basic_block new_bb, dest;
1107
  edge new_edge;
1108
  edge true_edge;
1109
  edge false_edge;
1110
  tree pred = COND_EXPR_COND (stmt);
1111
  tree then_exp = COND_EXPR_THEN (stmt);
1112
  tree else_exp = COND_EXPR_ELSE (stmt);
1113
  rtx last2, last;
1114
 
1115
  last2 = last = get_last_insn ();
1116
 
1117
  extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1118
  if (EXPR_LOCUS (stmt))
1119
    {
1120
      emit_line_note (*(EXPR_LOCUS (stmt)));
1121
      record_block_change (TREE_BLOCK (stmt));
1122
    }
1123
 
1124
  /* These flags have no purpose in RTL land.  */
1125
  true_edge->flags &= ~EDGE_TRUE_VALUE;
1126
  false_edge->flags &= ~EDGE_FALSE_VALUE;
1127
 
1128
  /* We can either have a pure conditional jump with one fallthru edge or
1129
     two-way jump that needs to be decomposed into two basic blocks.  */
1130
  if (TREE_CODE (then_exp) == GOTO_EXPR && IS_EMPTY_STMT (else_exp))
1131
    {
1132
      jumpif (pred, label_rtx (GOTO_DESTINATION (then_exp)));
1133
      add_reg_br_prob_note (last, true_edge->probability);
1134
      maybe_dump_rtl_for_tree_stmt (stmt, last);
1135
      if (EXPR_LOCUS (then_exp))
1136
        emit_line_note (*(EXPR_LOCUS (then_exp)));
1137
      return NULL;
1138
    }
1139
  if (TREE_CODE (else_exp) == GOTO_EXPR && IS_EMPTY_STMT (then_exp))
1140
    {
1141
      jumpifnot (pred, label_rtx (GOTO_DESTINATION (else_exp)));
1142
      add_reg_br_prob_note (last, false_edge->probability);
1143
      maybe_dump_rtl_for_tree_stmt (stmt, last);
1144
      if (EXPR_LOCUS (else_exp))
1145
        emit_line_note (*(EXPR_LOCUS (else_exp)));
1146
      return NULL;
1147
    }
1148
  gcc_assert (TREE_CODE (then_exp) == GOTO_EXPR
1149
              && TREE_CODE (else_exp) == GOTO_EXPR);
1150
 
1151
  jumpif (pred, label_rtx (GOTO_DESTINATION (then_exp)));
1152
  add_reg_br_prob_note (last, true_edge->probability);
1153
  last = get_last_insn ();
1154
  expand_expr (else_exp, const0_rtx, VOIDmode, 0);
1155
 
1156
  BB_END (bb) = last;
1157
  if (BARRIER_P (BB_END (bb)))
1158
    BB_END (bb) = PREV_INSN (BB_END (bb));
1159
  update_bb_for_insn (bb);
1160
 
1161
  new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1162
  dest = false_edge->dest;
1163
  redirect_edge_succ (false_edge, new_bb);
1164
  false_edge->flags |= EDGE_FALLTHRU;
1165
  new_bb->count = false_edge->count;
1166
  new_bb->frequency = EDGE_FREQUENCY (false_edge);
1167
  new_edge = make_edge (new_bb, dest, 0);
1168
  new_edge->probability = REG_BR_PROB_BASE;
1169
  new_edge->count = new_bb->count;
1170
  if (BARRIER_P (BB_END (new_bb)))
1171
    BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1172
  update_bb_for_insn (new_bb);
1173
 
1174
  maybe_dump_rtl_for_tree_stmt (stmt, last2);
1175
 
1176
  if (EXPR_LOCUS (else_exp))
1177
    emit_line_note (*(EXPR_LOCUS (else_exp)));
1178
 
1179
  return new_bb;
1180
}
1181
 
1182
/* A subroutine of expand_gimple_basic_block.  Expand one CALL_EXPR
1183
   that has CALL_EXPR_TAILCALL set.  Returns non-null if we actually
1184
   generated a tail call (something that might be denied by the ABI
1185
   rules governing the call; see calls.c).
1186
 
1187
   Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
1188
   can still reach the rest of BB.  The case here is __builtin_sqrt,
1189
   where the NaN result goes through the external function (with a
1190
   tailcall) and the normal result happens via a sqrt instruction.  */
1191
 
1192
static basic_block
1193
expand_gimple_tailcall (basic_block bb, tree stmt, bool *can_fallthru)
1194
{
1195
  rtx last2, last;
1196
  edge e;
1197
  edge_iterator ei;
1198
  int probability;
1199
  gcov_type count;
1200
 
1201
  last2 = last = get_last_insn ();
1202
 
1203
  expand_expr_stmt (stmt);
1204
 
1205
  for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
1206
    if (CALL_P (last) && SIBLING_CALL_P (last))
1207
      goto found;
1208
 
1209
  maybe_dump_rtl_for_tree_stmt (stmt, last2);
1210
 
1211
  *can_fallthru = true;
1212
  return NULL;
1213
 
1214
 found:
1215
  /* ??? Wouldn't it be better to just reset any pending stack adjust?
1216
     Any instructions emitted here are about to be deleted.  */
1217
  do_pending_stack_adjust ();
1218
 
1219
  /* Remove any non-eh, non-abnormal edges that don't go to exit.  */
1220
  /* ??? I.e. the fallthrough edge.  HOWEVER!  If there were to be
1221
     EH or abnormal edges, we shouldn't have created a tail call in
1222
     the first place.  So it seems to me we should just be removing
1223
     all edges here, or redirecting the existing fallthru edge to
1224
     the exit block.  */
1225
 
1226
  probability = 0;
1227
  count = 0;
1228
 
1229
  for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1230
    {
1231
      if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
1232
        {
1233
          if (e->dest != EXIT_BLOCK_PTR)
1234
            {
1235
              e->dest->count -= e->count;
1236
              e->dest->frequency -= EDGE_FREQUENCY (e);
1237
              if (e->dest->count < 0)
1238
                e->dest->count = 0;
1239
              if (e->dest->frequency < 0)
1240
                e->dest->frequency = 0;
1241
            }
1242
          count += e->count;
1243
          probability += e->probability;
1244
          remove_edge (e);
1245
        }
1246
      else
1247
        ei_next (&ei);
1248
    }
1249
 
1250
  /* This is somewhat ugly: the call_expr expander often emits instructions
1251
     after the sibcall (to perform the function return).  These confuse the
1252
     find_many_sub_basic_blocks code, so we need to get rid of these.  */
1253
  last = NEXT_INSN (last);
1254
  gcc_assert (BARRIER_P (last));
1255
 
1256
  *can_fallthru = false;
1257
  while (NEXT_INSN (last))
1258
    {
1259
      /* For instance an sqrt builtin expander expands if with
1260
         sibcall in the then and label for `else`.  */
1261
      if (LABEL_P (NEXT_INSN (last)))
1262
        {
1263
          *can_fallthru = true;
1264
          break;
1265
        }
1266
      delete_insn (NEXT_INSN (last));
1267
    }
1268
 
1269
  e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
1270
  e->probability += probability;
1271
  e->count += count;
1272
  BB_END (bb) = last;
1273
  update_bb_for_insn (bb);
1274
 
1275
  if (NEXT_INSN (last))
1276
    {
1277
      bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1278
 
1279
      last = BB_END (bb);
1280
      if (BARRIER_P (last))
1281
        BB_END (bb) = PREV_INSN (last);
1282
    }
1283
 
1284
  maybe_dump_rtl_for_tree_stmt (stmt, last2);
1285
 
1286
  return bb;
1287
}
1288
 
1289
/* Expand basic block BB from GIMPLE trees to RTL.  */
1290
 
1291
static basic_block
1292
expand_gimple_basic_block (basic_block bb)
1293
{
1294
  block_stmt_iterator bsi = bsi_start (bb);
1295
  tree stmt = NULL;
1296
  rtx note, last;
1297
  edge e;
1298
  edge_iterator ei;
1299
 
1300
  if (dump_file)
1301
    {
1302
      fprintf (dump_file,
1303
               "\n;; Generating RTL for tree basic block %d\n",
1304
               bb->index);
1305
    }
1306
 
1307
  init_rtl_bb_info (bb);
1308
  bb->flags |= BB_RTL;
1309
 
1310
  if (!bsi_end_p (bsi))
1311
    stmt = bsi_stmt (bsi);
1312
 
1313
  if (stmt && TREE_CODE (stmt) == LABEL_EXPR)
1314
    {
1315
      last = get_last_insn ();
1316
 
1317
      expand_expr_stmt (stmt);
1318
 
1319
      /* Java emits line number notes in the top of labels.
1320
         ??? Make this go away once line number notes are obsoleted.  */
1321
      BB_HEAD (bb) = NEXT_INSN (last);
1322
      if (NOTE_P (BB_HEAD (bb)))
1323
        BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
1324
      bsi_next (&bsi);
1325
      note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
1326
 
1327
      maybe_dump_rtl_for_tree_stmt (stmt, last);
1328
    }
1329
  else
1330
    note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
1331
 
1332
  NOTE_BASIC_BLOCK (note) = bb;
1333
 
1334
  for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
1335
    {
1336
      /* Clear EDGE_EXECUTABLE.  This flag is never used in the backend.  */
1337
      e->flags &= ~EDGE_EXECUTABLE;
1338
 
1339
      /* At the moment not all abnormal edges match the RTL representation.
1340
         It is safe to remove them here as find_many_sub_basic_blocks will
1341
         rediscover them.  In the future we should get this fixed properly.  */
1342
      if (e->flags & EDGE_ABNORMAL)
1343
        remove_edge (e);
1344
      else
1345
        ei_next (&ei);
1346
    }
1347
 
1348
  for (; !bsi_end_p (bsi); bsi_next (&bsi))
1349
    {
1350
      tree stmt = bsi_stmt (bsi);
1351
      basic_block new_bb;
1352
 
1353
      if (!stmt)
1354
        continue;
1355
 
1356
      /* Expand this statement, then evaluate the resulting RTL and
1357
         fixup the CFG accordingly.  */
1358
      if (TREE_CODE (stmt) == COND_EXPR)
1359
        {
1360
          new_bb = expand_gimple_cond_expr (bb, stmt);
1361
          if (new_bb)
1362
            return new_bb;
1363
        }
1364
      else
1365
        {
1366
          tree call = get_call_expr_in (stmt);
1367
          if (call && CALL_EXPR_TAILCALL (call))
1368
            {
1369
              bool can_fallthru;
1370
              new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
1371
              if (new_bb)
1372
                {
1373
                  if (can_fallthru)
1374
                    bb = new_bb;
1375
                  else
1376
                    return new_bb;
1377
                }
1378
            }
1379
          else
1380
            {
1381
              last = get_last_insn ();
1382
              expand_expr_stmt (stmt);
1383
              maybe_dump_rtl_for_tree_stmt (stmt, last);
1384
            }
1385
        }
1386
    }
1387
 
1388
  do_pending_stack_adjust ();
1389
 
1390
  /* Find the block tail.  The last insn in the block is the insn
1391
     before a barrier and/or table jump insn.  */
1392
  last = get_last_insn ();
1393
  if (BARRIER_P (last))
1394
    last = PREV_INSN (last);
1395
  if (JUMP_TABLE_DATA_P (last))
1396
    last = PREV_INSN (PREV_INSN (last));
1397
  BB_END (bb) = last;
1398
 
1399
  update_bb_for_insn (bb);
1400
 
1401
  return bb;
1402
}
1403
 
1404
 
1405
/* Create a basic block for initialization code.  */
1406
 
1407
static basic_block
1408
construct_init_block (void)
1409
{
1410
  basic_block init_block, first_block;
1411
  edge e = NULL;
1412
  int flags;
1413
 
1414
  /* Multiple entry points not supported yet.  */
1415
  gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
1416
  init_rtl_bb_info (ENTRY_BLOCK_PTR);
1417
  init_rtl_bb_info (EXIT_BLOCK_PTR);
1418
  ENTRY_BLOCK_PTR->flags |= BB_RTL;
1419
  EXIT_BLOCK_PTR->flags |= BB_RTL;
1420
 
1421
  e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
1422
 
1423
  /* When entry edge points to first basic block, we don't need jump,
1424
     otherwise we have to jump into proper target.  */
1425
  if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
1426
    {
1427
      tree label = tree_block_label (e->dest);
1428
 
1429
      emit_jump (label_rtx (label));
1430
      flags = 0;
1431
    }
1432
  else
1433
    flags = EDGE_FALLTHRU;
1434
 
1435
  init_block = create_basic_block (NEXT_INSN (get_insns ()),
1436
                                   get_last_insn (),
1437
                                   ENTRY_BLOCK_PTR);
1438
  init_block->frequency = ENTRY_BLOCK_PTR->frequency;
1439
  init_block->count = ENTRY_BLOCK_PTR->count;
1440
  if (e)
1441
    {
1442
      first_block = e->dest;
1443
      redirect_edge_succ (e, init_block);
1444
      e = make_edge (init_block, first_block, flags);
1445
    }
1446
  else
1447
    e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1448
  e->probability = REG_BR_PROB_BASE;
1449
  e->count = ENTRY_BLOCK_PTR->count;
1450
 
1451
  update_bb_for_insn (init_block);
1452
  return init_block;
1453
}
1454
 
1455
 
1456
/* Create a block containing landing pads and similar stuff.  */
1457
 
1458
static void
1459
construct_exit_block (void)
1460
{
1461
  rtx head = get_last_insn ();
1462
  rtx end;
1463
  basic_block exit_block;
1464
  edge e, e2;
1465
  unsigned ix;
1466
  edge_iterator ei;
1467
 
1468
  /* Make sure the locus is set to the end of the function, so that
1469
     epilogue line numbers and warnings are set properly.  */
1470
#ifdef USE_MAPPED_LOCATION
1471
  if (cfun->function_end_locus != UNKNOWN_LOCATION)
1472
#else
1473
  if (cfun->function_end_locus.file)
1474
#endif
1475
    input_location = cfun->function_end_locus;
1476
 
1477
  /* The following insns belong to the top scope.  */
1478
  record_block_change (DECL_INITIAL (current_function_decl));
1479
 
1480
  /* Generate rtl for function exit.  */
1481
  expand_function_end ();
1482
 
1483
  end = get_last_insn ();
1484
  if (head == end)
1485
    return;
1486
  while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
1487
    head = NEXT_INSN (head);
1488
  exit_block = create_basic_block (NEXT_INSN (head), end,
1489
                                   EXIT_BLOCK_PTR->prev_bb);
1490
  exit_block->frequency = EXIT_BLOCK_PTR->frequency;
1491
  exit_block->count = EXIT_BLOCK_PTR->count;
1492
 
1493
  ix = 0;
1494
  while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
1495
    {
1496
      e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
1497
      if (!(e->flags & EDGE_ABNORMAL))
1498
        redirect_edge_succ (e, exit_block);
1499
      else
1500
        ix++;
1501
    }
1502
 
1503
  e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
1504
  e->probability = REG_BR_PROB_BASE;
1505
  e->count = EXIT_BLOCK_PTR->count;
1506
  FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
1507
    if (e2 != e)
1508
      {
1509
        e->count -= e2->count;
1510
        exit_block->count -= e2->count;
1511
        exit_block->frequency -= EDGE_FREQUENCY (e2);
1512
      }
1513
  if (e->count < 0)
1514
    e->count = 0;
1515
  if (exit_block->count < 0)
1516
    exit_block->count = 0;
1517
  if (exit_block->frequency < 0)
1518
    exit_block->frequency = 0;
1519
  update_bb_for_insn (exit_block);
1520
}
1521
 
1522
/* Helper function for discover_nonconstant_array_refs.
1523
   Look for ARRAY_REF nodes with non-constant indexes and mark them
1524
   addressable.  */
1525
 
1526
static tree
1527
discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
1528
                                   void *data ATTRIBUTE_UNUSED)
1529
{
1530
  tree t = *tp;
1531
 
1532
  if (IS_TYPE_OR_DECL_P (t))
1533
    *walk_subtrees = 0;
1534
  else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1535
    {
1536
      while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1537
              && is_gimple_min_invariant (TREE_OPERAND (t, 1))
1538
              && (!TREE_OPERAND (t, 2)
1539
                  || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1540
             || (TREE_CODE (t) == COMPONENT_REF
1541
                 && (!TREE_OPERAND (t,2)
1542
                     || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
1543
             || TREE_CODE (t) == BIT_FIELD_REF
1544
             || TREE_CODE (t) == REALPART_EXPR
1545
             || TREE_CODE (t) == IMAGPART_EXPR
1546
             || TREE_CODE (t) == VIEW_CONVERT_EXPR
1547
             || TREE_CODE (t) == NOP_EXPR
1548
             || TREE_CODE (t) == CONVERT_EXPR)
1549
        t = TREE_OPERAND (t, 0);
1550
 
1551
      if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
1552
        {
1553
          t = get_base_address (t);
1554
          if (t && DECL_P (t))
1555
            TREE_ADDRESSABLE (t) = 1;
1556
        }
1557
 
1558
      *walk_subtrees = 0;
1559
    }
1560
 
1561
  return NULL_TREE;
1562
}
1563
 
1564
/* RTL expansion is not able to compile array references with variable
1565
   offsets for arrays stored in single register.  Discover such
1566
   expressions and mark variables as addressable to avoid this
1567
   scenario.  */
1568
 
1569
static void
1570
discover_nonconstant_array_refs (void)
1571
{
1572
  basic_block bb;
1573
  block_stmt_iterator bsi;
1574
 
1575
  FOR_EACH_BB (bb)
1576
    {
1577
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1578
        walk_tree (bsi_stmt_ptr (bsi), discover_nonconstant_array_refs_r,
1579
                   NULL , NULL);
1580
    }
1581
}
1582
 
1583
/* Translate the intermediate representation contained in the CFG
1584
   from GIMPLE trees to RTL.
1585
 
1586
   We do conversion per basic block and preserve/update the tree CFG.
1587
   This implies we have to do some magic as the CFG can simultaneously
1588
   consist of basic blocks containing RTL and GIMPLE trees.  This can
1589
   confuse the CFG hooks, so be careful to not manipulate CFG during
1590
   the expansion.  */
1591
 
1592
static unsigned int
1593
tree_expand_cfg (void)
1594
{
1595
  basic_block bb, init_block;
1596
  sbitmap blocks;
1597
  edge_iterator ei;
1598
  edge e;
1599
 
1600
  /* Some backends want to know that we are expanding to RTL.  */
1601
  currently_expanding_to_rtl = 1;
1602
 
1603
  /* Prepare the rtl middle end to start recording block changes.  */
1604
  reset_block_changes ();
1605
 
1606
  /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE.  */
1607
  discover_nonconstant_array_refs ();
1608
 
1609
  /* Expand the variables recorded during gimple lowering.  */
1610
  expand_used_vars ();
1611
 
1612
  /* Honor stack protection warnings.  */
1613
  if (warn_stack_protect)
1614
    {
1615
      if (current_function_calls_alloca)
1616
        warning (0, "not protecting local variables: variable length buffer");
1617
      if (has_short_buffer && !cfun->stack_protect_guard)
1618
        warning (0, "not protecting function: no buffer at least %d bytes long",
1619
                 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
1620
    }
1621
 
1622
  /* Set up parameters and prepare for return, for the function.  */
1623
  expand_function_start (current_function_decl);
1624
 
1625
  /* If this function is `main', emit a call to `__main'
1626
     to run global initializers, etc.  */
1627
  if (DECL_NAME (current_function_decl)
1628
      && MAIN_NAME_P (DECL_NAME (current_function_decl))
1629
      && DECL_FILE_SCOPE_P (current_function_decl))
1630
    expand_main_function ();
1631
 
1632
  /* Initialize the stack_protect_guard field.  This must happen after the
1633
     call to __main (if any) so that the external decl is initialized.  */
1634
  if (cfun->stack_protect_guard)
1635
    stack_protect_prologue ();
1636
 
1637
  /* Register rtl specific functions for cfg.  */
1638
  rtl_register_cfg_hooks ();
1639
 
1640
  init_block = construct_init_block ();
1641
 
1642
  /* Clear EDGE_EXECUTABLE on the entry edge(s).  It is cleaned from the
1643
     remaining edges in expand_gimple_basic_block.  */
1644
  FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
1645
    e->flags &= ~EDGE_EXECUTABLE;
1646
 
1647
  FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
1648
    bb = expand_gimple_basic_block (bb);
1649
 
1650
  construct_exit_block ();
1651
 
1652
  /* We're done expanding trees to RTL.  */
1653
  currently_expanding_to_rtl = 0;
1654
 
1655
  /* Convert tree EH labels to RTL EH labels, and clean out any unreachable
1656
     EH regions.  */
1657
  convert_from_eh_region_ranges ();
1658
 
1659
  rebuild_jump_labels (get_insns ());
1660
  find_exception_handler_labels ();
1661
 
1662
  blocks = sbitmap_alloc (last_basic_block);
1663
  sbitmap_ones (blocks);
1664
  find_many_sub_basic_blocks (blocks);
1665
  purge_all_dead_edges ();
1666
  sbitmap_free (blocks);
1667
 
1668
  compact_blocks ();
1669
#ifdef ENABLE_CHECKING
1670
  verify_flow_info();
1671
#endif
1672
 
1673
  /* There's no need to defer outputting this function any more; we
1674
     know we want to output it.  */
1675
  DECL_DEFER_OUTPUT (current_function_decl) = 0;
1676
 
1677
  /* Now that we're done expanding trees to RTL, we shouldn't have any
1678
     more CONCATs anywhere.  */
1679
  generating_concat_p = 0;
1680
 
1681
  finalize_block_changes ();
1682
 
1683
  if (dump_file)
1684
    {
1685
      fprintf (dump_file,
1686
               "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
1687
      /* And the pass manager will dump RTL for us.  */
1688
    }
1689
 
1690
  /* If we're emitting a nested function, make sure its parent gets
1691
     emitted as well.  Doing otherwise confuses debug info.  */
1692
  {
1693
    tree parent;
1694
    for (parent = DECL_CONTEXT (current_function_decl);
1695
         parent != NULL_TREE;
1696
         parent = get_containing_scope (parent))
1697
      if (TREE_CODE (parent) == FUNCTION_DECL)
1698
        TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
1699
  }
1700
 
1701
  /* We are now committed to emitting code for this function.  Do any
1702
     preparation, such as emitting abstract debug info for the inline
1703
     before it gets mangled by optimization.  */
1704
  if (cgraph_function_possibly_inlined_p (current_function_decl))
1705
    (*debug_hooks->outlining_inline_function) (current_function_decl);
1706
 
1707
  TREE_ASM_WRITTEN (current_function_decl) = 1;
1708
 
1709
  /* After expanding, the return labels are no longer needed. */
1710
  return_label = NULL;
1711
  naked_return_label = NULL;
1712
  return 0;
1713
}
1714
 
1715
struct tree_opt_pass pass_expand =
1716
{
1717
  "expand",                             /* name */
1718
  NULL,                                 /* gate */
1719
  tree_expand_cfg,                      /* execute */
1720
  NULL,                                 /* sub */
1721
  NULL,                                 /* next */
1722
  0,                                    /* static_pass_number */
1723
  TV_EXPAND,                            /* tv_id */
1724
  /* ??? If TER is enabled, we actually receive GENERIC.  */
1725
  PROP_gimple_leh | PROP_cfg,           /* properties_required */
1726
  PROP_rtl,                             /* properties_provided */
1727
  PROP_trees,                           /* properties_destroyed */
1728
  0,                                    /* todo_flags_start */
1729
  TODO_dump_func,                       /* todo_flags_finish */
1730
  'r'                                   /* letter */
1731
};

powered by: WebSVN 2.1.0

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