OSDN Git Service

* ira-build.c (ira_create_object): New arg SUBWORD; all callers changed.
[pf3gnuchains/gcc-fork.git] / gcc / cfgexpand.c
1 /* A pass for lowering trees to RTL.
2    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
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 "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "langhooks.h"
32 #include "tree-flow.h"
33 #include "timevar.h"
34 #include "tree-dump.h"
35 #include "tree-pass.h"
36 #include "except.h"
37 #include "flags.h"
38 #include "diagnostic.h"
39 #include "tree-pretty-print.h"
40 #include "gimple-pretty-print.h"
41 #include "toplev.h"
42 #include "debug.h"
43 #include "params.h"
44 #include "tree-inline.h"
45 #include "value-prof.h"
46 #include "target.h"
47 #include "ssaexpand.h"
48 #include "bitmap.h"
49 #include "sbitmap.h"
50 #include "insn-attr.h" /* For INSN_SCHEDULING.  */
51
52 /* This variable holds information helping the rewriting of SSA trees
53    into RTL.  */
54 struct ssaexpand SA;
55
56 /* This variable holds the currently expanded gimple statement for purposes
57    of comminucating the profile info to the builtin expanders.  */
58 gimple currently_expanding_gimple_stmt;
59
60 /* Return an expression tree corresponding to the RHS of GIMPLE
61    statement STMT.  */
62
63 tree
64 gimple_assign_rhs_to_tree (gimple stmt)
65 {
66   tree t;
67   enum gimple_rhs_class grhs_class;
68
69   grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
70
71   if (grhs_class == GIMPLE_TERNARY_RHS)
72     t = build3 (gimple_assign_rhs_code (stmt),
73                 TREE_TYPE (gimple_assign_lhs (stmt)),
74                 gimple_assign_rhs1 (stmt),
75                 gimple_assign_rhs2 (stmt),
76                 gimple_assign_rhs3 (stmt));
77   else if (grhs_class == GIMPLE_BINARY_RHS)
78     t = build2 (gimple_assign_rhs_code (stmt),
79                 TREE_TYPE (gimple_assign_lhs (stmt)),
80                 gimple_assign_rhs1 (stmt),
81                 gimple_assign_rhs2 (stmt));
82   else if (grhs_class == GIMPLE_UNARY_RHS)
83     t = build1 (gimple_assign_rhs_code (stmt),
84                 TREE_TYPE (gimple_assign_lhs (stmt)),
85                 gimple_assign_rhs1 (stmt));
86   else if (grhs_class == GIMPLE_SINGLE_RHS)
87     {
88       t = gimple_assign_rhs1 (stmt);
89       /* Avoid modifying this tree in place below.  */
90       if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
91            && gimple_location (stmt) != EXPR_LOCATION (t))
92           || (gimple_block (stmt)
93               && currently_expanding_to_rtl
94               && EXPR_P (t)
95               && gimple_block (stmt) != TREE_BLOCK (t)))
96         t = copy_node (t);
97     }
98   else
99     gcc_unreachable ();
100
101   if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
102     SET_EXPR_LOCATION (t, gimple_location (stmt));
103   if (gimple_block (stmt) && currently_expanding_to_rtl && EXPR_P (t))
104     TREE_BLOCK (t) = gimple_block (stmt);
105
106   return t;
107 }
108
109
110 #ifndef STACK_ALIGNMENT_NEEDED
111 #define STACK_ALIGNMENT_NEEDED 1
112 #endif
113
114 #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
115
116 /* Associate declaration T with storage space X.  If T is no
117    SSA name this is exactly SET_DECL_RTL, otherwise make the
118    partition of T associated with X.  */
119 static inline void
120 set_rtl (tree t, rtx x)
121 {
122   if (TREE_CODE (t) == SSA_NAME)
123     {
124       SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
125       if (x && !MEM_P (x))
126         set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
127       /* For the benefit of debug information at -O0 (where vartracking
128          doesn't run) record the place also in the base DECL if it's
129          a normal variable (not a parameter).  */
130       if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
131         {
132           tree var = SSA_NAME_VAR (t);
133           /* If we don't yet have something recorded, just record it now.  */
134           if (!DECL_RTL_SET_P (var))
135             SET_DECL_RTL (var, x);
136           /* If we have it set alrady to "multiple places" don't
137              change this.  */
138           else if (DECL_RTL (var) == pc_rtx)
139             ;
140           /* If we have something recorded and it's not the same place
141              as we want to record now, we have multiple partitions for the
142              same base variable, with different places.  We can't just
143              randomly chose one, hence we have to say that we don't know.
144              This only happens with optimization, and there var-tracking
145              will figure out the right thing.  */
146           else if (DECL_RTL (var) != x)
147             SET_DECL_RTL (var, pc_rtx);
148         }
149     }
150   else
151     SET_DECL_RTL (t, x);
152 }
153
154 /* This structure holds data relevant to one variable that will be
155    placed in a stack slot.  */
156 struct stack_var
157 {
158   /* The Variable.  */
159   tree decl;
160
161   /* The offset of the variable.  During partitioning, this is the
162      offset relative to the partition.  After partitioning, this
163      is relative to the stack frame.  */
164   HOST_WIDE_INT offset;
165
166   /* Initially, the size of the variable.  Later, the size of the partition,
167      if this variable becomes it's partition's representative.  */
168   HOST_WIDE_INT size;
169
170   /* The *byte* alignment required for this variable.  Or as, with the
171      size, the alignment for this partition.  */
172   unsigned int alignb;
173
174   /* The partition representative.  */
175   size_t representative;
176
177   /* The next stack variable in the partition, or EOC.  */
178   size_t next;
179
180   /* The numbers of conflicting stack variables.  */
181   bitmap conflicts;
182 };
183
184 #define EOC  ((size_t)-1)
185
186 /* We have an array of such objects while deciding allocation.  */
187 static struct stack_var *stack_vars;
188 static size_t stack_vars_alloc;
189 static size_t stack_vars_num;
190
191 /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
192    is non-decreasing.  */
193 static size_t *stack_vars_sorted;
194
195 /* The phase of the stack frame.  This is the known misalignment of
196    virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY.  That is,
197    (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0.  */
198 static int frame_phase;
199
200 /* Used during expand_used_vars to remember if we saw any decls for
201    which we'd like to enable stack smashing protection.  */
202 static bool has_protected_decls;
203
204 /* Used during expand_used_vars.  Remember if we say a character buffer
205    smaller than our cutoff threshold.  Used for -Wstack-protector.  */
206 static bool has_short_buffer;
207
208 /* Discover the byte alignment to use for DECL.  Ignore alignment
209    we can't do with expected alignment of the stack boundary.  */
210
211 static unsigned int
212 get_decl_align_unit (tree decl)
213 {
214   unsigned int align;
215
216   align = LOCAL_DECL_ALIGNMENT (decl);
217
218   if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
219     align = MAX_SUPPORTED_STACK_ALIGNMENT;
220
221   if (SUPPORTS_STACK_ALIGNMENT)
222     {
223       if (crtl->stack_alignment_estimated < align)
224         {
225           gcc_assert(!crtl->stack_realign_processed);
226           crtl->stack_alignment_estimated = align;
227         }
228     }
229
230   /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
231      So here we only make sure stack_alignment_needed >= align.  */
232   if (crtl->stack_alignment_needed < align)
233     crtl->stack_alignment_needed = align;
234   if (crtl->max_used_stack_slot_alignment < align)
235     crtl->max_used_stack_slot_alignment = align;
236
237   return align / BITS_PER_UNIT;
238 }
239
240 /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
241    Return the frame offset.  */
242
243 static HOST_WIDE_INT
244 alloc_stack_frame_space (HOST_WIDE_INT size, HOST_WIDE_INT align)
245 {
246   HOST_WIDE_INT offset, new_frame_offset;
247
248   new_frame_offset = frame_offset;
249   if (FRAME_GROWS_DOWNWARD)
250     {
251       new_frame_offset -= size + frame_phase;
252       new_frame_offset &= -align;
253       new_frame_offset += frame_phase;
254       offset = new_frame_offset;
255     }
256   else
257     {
258       new_frame_offset -= frame_phase;
259       new_frame_offset += align - 1;
260       new_frame_offset &= -align;
261       new_frame_offset += frame_phase;
262       offset = new_frame_offset;
263       new_frame_offset += size;
264     }
265   frame_offset = new_frame_offset;
266
267   if (frame_offset_overflow (frame_offset, cfun->decl))
268     frame_offset = offset = 0;
269
270   return offset;
271 }
272
273 /* Accumulate DECL into STACK_VARS.  */
274
275 static void
276 add_stack_var (tree decl)
277 {
278   if (stack_vars_num >= stack_vars_alloc)
279     {
280       if (stack_vars_alloc)
281         stack_vars_alloc = stack_vars_alloc * 3 / 2;
282       else
283         stack_vars_alloc = 32;
284       stack_vars
285         = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
286     }
287   stack_vars[stack_vars_num].decl = decl;
288   stack_vars[stack_vars_num].offset = 0;
289   stack_vars[stack_vars_num].size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (decl)), 1);
290   stack_vars[stack_vars_num].alignb = get_decl_align_unit (SSAVAR (decl));
291
292   /* All variables are initially in their own partition.  */
293   stack_vars[stack_vars_num].representative = stack_vars_num;
294   stack_vars[stack_vars_num].next = EOC;
295
296   /* All variables initially conflict with no other.  */
297   stack_vars[stack_vars_num].conflicts = NULL;
298
299   /* Ensure that this decl doesn't get put onto the list twice.  */
300   set_rtl (decl, pc_rtx);
301
302   stack_vars_num++;
303 }
304
305 /* Make the decls associated with luid's X and Y conflict.  */
306
307 static void
308 add_stack_var_conflict (size_t x, size_t y)
309 {
310   struct stack_var *a = &stack_vars[x];
311   struct stack_var *b = &stack_vars[y];
312   if (!a->conflicts)
313     a->conflicts = BITMAP_ALLOC (NULL);
314   if (!b->conflicts)
315     b->conflicts = BITMAP_ALLOC (NULL);
316   bitmap_set_bit (a->conflicts, y);
317   bitmap_set_bit (b->conflicts, x);
318 }
319
320 /* Check whether the decls associated with luid's X and Y conflict.  */
321
322 static bool
323 stack_var_conflict_p (size_t x, size_t y)
324 {
325   struct stack_var *a = &stack_vars[x];
326   struct stack_var *b = &stack_vars[y];
327   if (!a->conflicts || !b->conflicts)
328     return false;
329   return bitmap_bit_p (a->conflicts, y);
330 }
331
332 /* Returns true if TYPE is or contains a union type.  */
333
334 static bool
335 aggregate_contains_union_type (tree type)
336 {
337   tree field;
338
339   if (TREE_CODE (type) == UNION_TYPE
340       || TREE_CODE (type) == QUAL_UNION_TYPE)
341     return true;
342   if (TREE_CODE (type) == ARRAY_TYPE)
343     return aggregate_contains_union_type (TREE_TYPE (type));
344   if (TREE_CODE (type) != RECORD_TYPE)
345     return false;
346
347   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
348     if (TREE_CODE (field) == FIELD_DECL)
349       if (aggregate_contains_union_type (TREE_TYPE (field)))
350         return true;
351
352   return false;
353 }
354
355 /* A subroutine of expand_used_vars.  If two variables X and Y have alias
356    sets that do not conflict, then do add a conflict for these variables
357    in the interference graph.  We also need to make sure to add conflicts
358    for union containing structures.  Else RTL alias analysis comes along
359    and due to type based aliasing rules decides that for two overlapping
360    union temporaries { short s; int i; } accesses to the same mem through
361    different types may not alias and happily reorders stores across
362    life-time boundaries of the temporaries (See PR25654).
363    We also have to mind MEM_IN_STRUCT_P and MEM_SCALAR_P.  */
364
365 static void
366 add_alias_set_conflicts (void)
367 {
368   size_t i, j, n = stack_vars_num;
369
370   for (i = 0; i < n; ++i)
371     {
372       tree type_i = TREE_TYPE (stack_vars[i].decl);
373       bool aggr_i = AGGREGATE_TYPE_P (type_i);
374       bool contains_union;
375
376       contains_union = aggregate_contains_union_type (type_i);
377       for (j = 0; j < i; ++j)
378         {
379           tree type_j = TREE_TYPE (stack_vars[j].decl);
380           bool aggr_j = AGGREGATE_TYPE_P (type_j);
381           if (aggr_i != aggr_j
382               /* Either the objects conflict by means of type based
383                  aliasing rules, or we need to add a conflict.  */
384               || !objects_must_conflict_p (type_i, type_j)
385               /* In case the types do not conflict ensure that access
386                  to elements will conflict.  In case of unions we have
387                  to be careful as type based aliasing rules may say
388                  access to the same memory does not conflict.  So play
389                  safe and add a conflict in this case.  */
390               || contains_union)
391             add_stack_var_conflict (i, j);
392         }
393     }
394 }
395
396 /* A subroutine of partition_stack_vars.  A comparison function for qsort,
397    sorting an array of indices by the size and type of the object.  */
398
399 static int
400 stack_var_size_cmp (const void *a, const void *b)
401 {
402   HOST_WIDE_INT sa = stack_vars[*(const size_t *)a].size;
403   HOST_WIDE_INT sb = stack_vars[*(const size_t *)b].size;
404   tree decla, declb;
405   unsigned int uida, uidb;
406
407   if (sa < sb)
408     return -1;
409   if (sa > sb)
410     return 1;
411   decla = stack_vars[*(const size_t *)a].decl;
412   declb = stack_vars[*(const size_t *)b].decl;
413   /* For stack variables of the same size use and id of the decls
414      to make the sort stable.  Two SSA names are compared by their
415      version, SSA names come before non-SSA names, and two normal
416      decls are compared by their DECL_UID.  */
417   if (TREE_CODE (decla) == SSA_NAME)
418     {
419       if (TREE_CODE (declb) == SSA_NAME)
420         uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
421       else
422         return -1;
423     }
424   else if (TREE_CODE (declb) == SSA_NAME)
425     return 1;
426   else
427     uida = DECL_UID (decla), uidb = DECL_UID (declb);
428   if (uida < uidb)
429     return -1;
430   if (uida > uidb)
431     return 1;
432   return 0;
433 }
434
435
436 /* If the points-to solution *PI points to variables that are in a partition
437    together with other variables add all partition members to the pointed-to
438    variables bitmap.  */
439
440 static void
441 add_partitioned_vars_to_ptset (struct pt_solution *pt,
442                                struct pointer_map_t *decls_to_partitions,
443                                struct pointer_set_t *visited, bitmap temp)
444 {
445   bitmap_iterator bi;
446   unsigned i;
447   bitmap *part;
448
449   if (pt->anything
450       || pt->vars == NULL
451       /* The pointed-to vars bitmap is shared, it is enough to
452          visit it once.  */
453       || pointer_set_insert(visited, pt->vars))
454     return;
455
456   bitmap_clear (temp);
457
458   /* By using a temporary bitmap to store all members of the partitions
459      we have to add we make sure to visit each of the partitions only
460      once.  */
461   EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
462     if ((!temp
463          || !bitmap_bit_p (temp, i))
464         && (part = (bitmap *) pointer_map_contains (decls_to_partitions,
465                                                     (void *)(size_t) i)))
466       bitmap_ior_into (temp, *part);
467   if (!bitmap_empty_p (temp))
468     bitmap_ior_into (pt->vars, temp);
469 }
470
471 /* Update points-to sets based on partition info, so we can use them on RTL.
472    The bitmaps representing stack partitions will be saved until expand,
473    where partitioned decls used as bases in memory expressions will be
474    rewritten.  */
475
476 static void
477 update_alias_info_with_stack_vars (void)
478 {
479   struct pointer_map_t *decls_to_partitions = NULL;
480   size_t i, j;
481   tree var = NULL_TREE;
482
483   for (i = 0; i < stack_vars_num; i++)
484     {
485       bitmap part = NULL;
486       tree name;
487       struct ptr_info_def *pi;
488
489       /* Not interested in partitions with single variable.  */
490       if (stack_vars[i].representative != i
491           || stack_vars[i].next == EOC)
492         continue;
493
494       if (!decls_to_partitions)
495         {
496           decls_to_partitions = pointer_map_create ();
497           cfun->gimple_df->decls_to_pointers = pointer_map_create ();
498         }
499
500       /* Create an SSA_NAME that points to the partition for use
501          as base during alias-oracle queries on RTL for bases that
502          have been partitioned.  */
503       if (var == NULL_TREE)
504         var = create_tmp_var (ptr_type_node, NULL);
505       name = make_ssa_name (var, NULL);
506
507       /* Create bitmaps representing partitions.  They will be used for
508          points-to sets later, so use GGC alloc.  */
509       part = BITMAP_GGC_ALLOC ();
510       for (j = i; j != EOC; j = stack_vars[j].next)
511         {
512           tree decl = stack_vars[j].decl;
513           unsigned int uid = DECL_PT_UID (decl);
514           /* We should never end up partitioning SSA names (though they
515              may end up on the stack).  Neither should we allocate stack
516              space to something that is unused and thus unreferenced.  */
517           gcc_assert (DECL_P (decl)
518                       && referenced_var_lookup (DECL_UID (decl)));
519           bitmap_set_bit (part, uid);
520           *((bitmap *) pointer_map_insert (decls_to_partitions,
521                                            (void *)(size_t) uid)) = part;
522           *((tree *) pointer_map_insert (cfun->gimple_df->decls_to_pointers,
523                                          decl)) = name;
524         }
525
526       /* Make the SSA name point to all partition members.  */
527       pi = get_ptr_info (name);
528       pt_solution_set (&pi->pt, part, false, false);
529     }
530
531   /* Make all points-to sets that contain one member of a partition
532      contain all members of the partition.  */
533   if (decls_to_partitions)
534     {
535       unsigned i;
536       struct pointer_set_t *visited = pointer_set_create ();
537       bitmap temp = BITMAP_ALLOC (NULL);
538
539       for (i = 1; i < num_ssa_names; i++)
540         {
541           tree name = ssa_name (i);
542           struct ptr_info_def *pi;
543
544           if (name
545               && POINTER_TYPE_P (TREE_TYPE (name))
546               && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
547             add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
548                                            visited, temp);
549         }
550
551       add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
552                                      decls_to_partitions, visited, temp);
553
554       pointer_set_destroy (visited);
555       pointer_map_destroy (decls_to_partitions);
556       BITMAP_FREE (temp);
557     }
558 }
559
560 /* A subroutine of partition_stack_vars.  The UNION portion of a UNION/FIND
561    partitioning algorithm.  Partitions A and B are known to be non-conflicting.
562    Merge them into a single partition A.
563
564    At the same time, add OFFSET to all variables in partition B.  At the end
565    of the partitioning process we've have a nice block easy to lay out within
566    the stack frame.  */
567
568 static void
569 union_stack_vars (size_t a, size_t b, HOST_WIDE_INT offset)
570 {
571   size_t i, last;
572   struct stack_var *vb = &stack_vars[b];
573   bitmap_iterator bi;
574   unsigned u;
575
576   /* Update each element of partition B with the given offset,
577      and merge them into partition A.  */
578   for (last = i = b; i != EOC; last = i, i = stack_vars[i].next)
579     {
580       stack_vars[i].offset += offset;
581       stack_vars[i].representative = a;
582     }
583   stack_vars[last].next = stack_vars[a].next;
584   stack_vars[a].next = b;
585
586   /* Update the required alignment of partition A to account for B.  */
587   if (stack_vars[a].alignb < stack_vars[b].alignb)
588     stack_vars[a].alignb = stack_vars[b].alignb;
589
590   /* Update the interference graph and merge the conflicts.  */
591   if (vb->conflicts)
592     {
593       EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
594         add_stack_var_conflict (a, stack_vars[u].representative);
595       BITMAP_FREE (vb->conflicts);
596     }
597 }
598
599 /* A subroutine of expand_used_vars.  Binpack the variables into
600    partitions constrained by the interference graph.  The overall
601    algorithm used is as follows:
602
603         Sort the objects by size.
604         For each object A {
605           S = size(A)
606           O = 0
607           loop {
608             Look for the largest non-conflicting object B with size <= S.
609             UNION (A, B)
610             offset(B) = O
611             O += size(B)
612             S -= size(B)
613           }
614         }
615 */
616
617 static void
618 partition_stack_vars (void)
619 {
620   size_t si, sj, n = stack_vars_num;
621
622   stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
623   for (si = 0; si < n; ++si)
624     stack_vars_sorted[si] = si;
625
626   if (n == 1)
627     return;
628
629   qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_size_cmp);
630
631   for (si = 0; si < n; ++si)
632     {
633       size_t i = stack_vars_sorted[si];
634       HOST_WIDE_INT isize = stack_vars[i].size;
635       HOST_WIDE_INT offset = 0;
636
637       for (sj = si; sj-- > 0; )
638         {
639           size_t j = stack_vars_sorted[sj];
640           HOST_WIDE_INT jsize = stack_vars[j].size;
641           unsigned int jalign = stack_vars[j].alignb;
642
643           /* Ignore objects that aren't partition representatives.  */
644           if (stack_vars[j].representative != j)
645             continue;
646
647           /* Ignore objects too large for the remaining space.  */
648           if (isize < jsize)
649             continue;
650
651           /* Ignore conflicting objects.  */
652           if (stack_var_conflict_p (i, j))
653             continue;
654
655           /* Refine the remaining space check to include alignment.  */
656           if (offset & (jalign - 1))
657             {
658               HOST_WIDE_INT toff = offset;
659               toff += jalign - 1;
660               toff &= -(HOST_WIDE_INT)jalign;
661               if (isize - (toff - offset) < jsize)
662                 continue;
663
664               isize -= toff - offset;
665               offset = toff;
666             }
667
668           /* UNION the objects, placing J at OFFSET.  */
669           union_stack_vars (i, j, offset);
670
671           isize -= jsize;
672           if (isize == 0)
673             break;
674         }
675     }
676
677   if (optimize)
678     update_alias_info_with_stack_vars ();
679 }
680
681 /* A debugging aid for expand_used_vars.  Dump the generated partitions.  */
682
683 static void
684 dump_stack_var_partition (void)
685 {
686   size_t si, i, j, n = stack_vars_num;
687
688   for (si = 0; si < n; ++si)
689     {
690       i = stack_vars_sorted[si];
691
692       /* Skip variables that aren't partition representatives, for now.  */
693       if (stack_vars[i].representative != i)
694         continue;
695
696       fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
697                " align %u\n", (unsigned long) i, stack_vars[i].size,
698                stack_vars[i].alignb);
699
700       for (j = i; j != EOC; j = stack_vars[j].next)
701         {
702           fputc ('\t', dump_file);
703           print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
704           fprintf (dump_file, ", offset " HOST_WIDE_INT_PRINT_DEC "\n",
705                    stack_vars[j].offset);
706         }
707     }
708 }
709
710 /* Assign rtl to DECL at frame offset OFFSET.  */
711
712 static void
713 expand_one_stack_var_at (tree decl, HOST_WIDE_INT offset)
714 {
715   /* Alignment is unsigned.   */
716   unsigned HOST_WIDE_INT align;
717   rtx x;
718
719   /* If this fails, we've overflowed the stack frame.  Error nicely?  */
720   gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
721
722   x = plus_constant (virtual_stack_vars_rtx, offset);
723   x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
724
725   if (TREE_CODE (decl) != SSA_NAME)
726     {
727       /* Set alignment we actually gave this decl if it isn't an SSA name.
728          If it is we generate stack slots only accidentally so it isn't as
729          important, we'll simply use the alignment that is already set.  */
730       offset -= frame_phase;
731       align = offset & -offset;
732       align *= BITS_PER_UNIT;
733       if (align == 0)
734         align = STACK_BOUNDARY;
735       else if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
736         align = MAX_SUPPORTED_STACK_ALIGNMENT;
737
738       DECL_ALIGN (decl) = align;
739       DECL_USER_ALIGN (decl) = 0;
740     }
741
742   set_mem_attributes (x, SSAVAR (decl), true);
743   set_rtl (decl, x);
744 }
745
746 /* A subroutine of expand_used_vars.  Give each partition representative
747    a unique location within the stack frame.  Update each partition member
748    with that location.  */
749
750 static void
751 expand_stack_vars (bool (*pred) (tree))
752 {
753   size_t si, i, j, n = stack_vars_num;
754
755   for (si = 0; si < n; ++si)
756     {
757       HOST_WIDE_INT offset;
758
759       i = stack_vars_sorted[si];
760
761       /* Skip variables that aren't partition representatives, for now.  */
762       if (stack_vars[i].representative != i)
763         continue;
764
765       /* Skip variables that have already had rtl assigned.  See also
766          add_stack_var where we perpetrate this pc_rtx hack.  */
767       if ((TREE_CODE (stack_vars[i].decl) == SSA_NAME
768            ? SA.partition_to_pseudo[var_to_partition (SA.map, stack_vars[i].decl)]
769            : DECL_RTL (stack_vars[i].decl)) != pc_rtx)
770         continue;
771
772       /* Check the predicate to see whether this variable should be
773          allocated in this pass.  */
774       if (pred && !pred (stack_vars[i].decl))
775         continue;
776
777       offset = alloc_stack_frame_space (stack_vars[i].size,
778                                         stack_vars[i].alignb);
779
780       /* Create rtl for each variable based on their location within the
781          partition.  */
782       for (j = i; j != EOC; j = stack_vars[j].next)
783         {
784           gcc_assert (stack_vars[j].offset <= stack_vars[i].size);
785           expand_one_stack_var_at (stack_vars[j].decl,
786                                    stack_vars[j].offset + offset);
787         }
788     }
789 }
790
791 /* Take into account all sizes of partitions and reset DECL_RTLs.  */
792 static HOST_WIDE_INT
793 account_stack_vars (void)
794 {
795   size_t si, j, i, n = stack_vars_num;
796   HOST_WIDE_INT size = 0;
797
798   for (si = 0; si < n; ++si)
799     {
800       i = stack_vars_sorted[si];
801
802       /* Skip variables that aren't partition representatives, for now.  */
803       if (stack_vars[i].representative != i)
804         continue;
805
806       size += stack_vars[i].size;
807       for (j = i; j != EOC; j = stack_vars[j].next)
808         set_rtl (stack_vars[j].decl, NULL);
809     }
810   return size;
811 }
812
813 /* A subroutine of expand_one_var.  Called to immediately assign rtl
814    to a variable to be allocated in the stack frame.  */
815
816 static void
817 expand_one_stack_var (tree var)
818 {
819   HOST_WIDE_INT size, offset, align;
820
821   size = tree_low_cst (DECL_SIZE_UNIT (SSAVAR (var)), 1);
822   align = get_decl_align_unit (SSAVAR (var));
823   offset = alloc_stack_frame_space (size, align);
824
825   expand_one_stack_var_at (var, offset);
826 }
827
828 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
829    that will reside in a hard register.  */
830
831 static void
832 expand_one_hard_reg_var (tree var)
833 {
834   rest_of_decl_compilation (var, 0, 0);
835 }
836
837 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL
838    that will reside in a pseudo register.  */
839
840 static void
841 expand_one_register_var (tree var)
842 {
843   tree decl = SSAVAR (var);
844   tree type = TREE_TYPE (decl);
845   enum machine_mode reg_mode = promote_decl_mode (decl, NULL);
846   rtx x = gen_reg_rtx (reg_mode);
847
848   set_rtl (var, x);
849
850   /* Note if the object is a user variable.  */
851   if (!DECL_ARTIFICIAL (decl))
852     mark_user_reg (x);
853
854   if (POINTER_TYPE_P (type))
855     mark_reg_pointer (x, TYPE_ALIGN (TREE_TYPE (type)));
856 }
857
858 /* A subroutine of expand_one_var.  Called to assign rtl to a VAR_DECL that
859    has some associated error, e.g. its type is error-mark.  We just need
860    to pick something that won't crash the rest of the compiler.  */
861
862 static void
863 expand_one_error_var (tree var)
864 {
865   enum machine_mode mode = DECL_MODE (var);
866   rtx x;
867
868   if (mode == BLKmode)
869     x = gen_rtx_MEM (BLKmode, const0_rtx);
870   else if (mode == VOIDmode)
871     x = const0_rtx;
872   else
873     x = gen_reg_rtx (mode);
874
875   SET_DECL_RTL (var, x);
876 }
877
878 /* A subroutine of expand_one_var.  VAR is a variable that will be
879    allocated to the local stack frame.  Return true if we wish to
880    add VAR to STACK_VARS so that it will be coalesced with other
881    variables.  Return false to allocate VAR immediately.
882
883    This function is used to reduce the number of variables considered
884    for coalescing, which reduces the size of the quadratic problem.  */
885
886 static bool
887 defer_stack_allocation (tree var, bool toplevel)
888 {
889   /* If stack protection is enabled, *all* stack variables must be deferred,
890      so that we can re-order the strings to the top of the frame.  */
891   if (flag_stack_protect)
892     return true;
893
894   /* Variables in the outermost scope automatically conflict with
895      every other variable.  The only reason to want to defer them
896      at all is that, after sorting, we can more efficiently pack
897      small variables in the stack frame.  Continue to defer at -O2.  */
898   if (toplevel && optimize < 2)
899     return false;
900
901   /* Without optimization, *most* variables are allocated from the
902      stack, which makes the quadratic problem large exactly when we
903      want compilation to proceed as quickly as possible.  On the
904      other hand, we don't want the function's stack frame size to
905      get completely out of hand.  So we avoid adding scalars and
906      "small" aggregates to the list at all.  */
907   if (optimize == 0 && tree_low_cst (DECL_SIZE_UNIT (var), 1) < 32)
908     return false;
909
910   return true;
911 }
912
913 /* A subroutine of expand_used_vars.  Expand one variable according to
914    its flavor.  Variables to be placed on the stack are not actually
915    expanded yet, merely recorded.
916    When REALLY_EXPAND is false, only add stack values to be allocated.
917    Return stack usage this variable is supposed to take.
918 */
919
920 static HOST_WIDE_INT
921 expand_one_var (tree var, bool toplevel, bool really_expand)
922 {
923   tree origvar = var;
924   var = SSAVAR (var);
925
926   if (SUPPORTS_STACK_ALIGNMENT
927       && TREE_TYPE (var) != error_mark_node
928       && TREE_CODE (var) == VAR_DECL)
929     {
930       unsigned int align;
931
932       /* Because we don't know if VAR will be in register or on stack,
933          we conservatively assume it will be on stack even if VAR is
934          eventually put into register after RA pass.  For non-automatic
935          variables, which won't be on stack, we collect alignment of
936          type and ignore user specified alignment.  */
937       if (TREE_STATIC (var) || DECL_EXTERNAL (var))
938         align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
939                                    TYPE_MODE (TREE_TYPE (var)),
940                                    TYPE_ALIGN (TREE_TYPE (var)));
941       else
942         align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
943
944       if (crtl->stack_alignment_estimated < align)
945         {
946           /* stack_alignment_estimated shouldn't change after stack
947              realign decision made */
948           gcc_assert(!crtl->stack_realign_processed);
949           crtl->stack_alignment_estimated = align;
950         }
951     }
952
953   if (TREE_CODE (origvar) == SSA_NAME)
954     {
955       gcc_assert (TREE_CODE (var) != VAR_DECL
956                   || (!DECL_EXTERNAL (var)
957                       && !DECL_HAS_VALUE_EXPR_P (var)
958                       && !TREE_STATIC (var)
959                       && TREE_TYPE (var) != error_mark_node
960                       && !DECL_HARD_REGISTER (var)
961                       && really_expand));
962     }
963   if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
964     ;
965   else if (DECL_EXTERNAL (var))
966     ;
967   else if (DECL_HAS_VALUE_EXPR_P (var))
968     ;
969   else if (TREE_STATIC (var))
970     ;
971   else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
972     ;
973   else if (TREE_TYPE (var) == error_mark_node)
974     {
975       if (really_expand)
976         expand_one_error_var (var);
977     }
978   else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
979     {
980       if (really_expand)
981         expand_one_hard_reg_var (var);
982     }
983   else if (use_register_for_decl (var))
984     {
985       if (really_expand)
986         expand_one_register_var (origvar);
987     }
988   else if (!host_integerp (DECL_SIZE_UNIT (var), 1))
989     {
990       if (really_expand)
991         {
992           error ("size of variable %q+D is too large", var);
993           expand_one_error_var (var);
994         }
995     }
996   else if (defer_stack_allocation (var, toplevel))
997     add_stack_var (origvar);
998   else
999     {
1000       if (really_expand)
1001         expand_one_stack_var (origvar);
1002       return tree_low_cst (DECL_SIZE_UNIT (var), 1);
1003     }
1004   return 0;
1005 }
1006
1007 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1008    expanding variables.  Those variables that can be put into registers
1009    are allocated pseudos; those that can't are put on the stack.
1010
1011    TOPLEVEL is true if this is the outermost BLOCK.  */
1012
1013 static void
1014 expand_used_vars_for_block (tree block, bool toplevel)
1015 {
1016   size_t i, j, old_sv_num, this_sv_num, new_sv_num;
1017   tree t;
1018
1019   old_sv_num = toplevel ? 0 : stack_vars_num;
1020
1021   /* Expand all variables at this level.  */
1022   for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1023     if (TREE_USED (t))
1024       expand_one_var (t, toplevel, true);
1025
1026   this_sv_num = stack_vars_num;
1027
1028   /* Expand all variables at containing levels.  */
1029   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1030     expand_used_vars_for_block (t, false);
1031
1032   /* Since we do not track exact variable lifetimes (which is not even
1033      possible for variables whose address escapes), we mirror the block
1034      tree in the interference graph.  Here we cause all variables at this
1035      level, and all sublevels, to conflict.  */
1036   if (old_sv_num < this_sv_num)
1037     {
1038       new_sv_num = stack_vars_num;
1039
1040       for (i = old_sv_num; i < new_sv_num; ++i)
1041         for (j = i < this_sv_num ? i : this_sv_num; j-- > old_sv_num ;)
1042           add_stack_var_conflict (i, j);
1043     }
1044 }
1045
1046 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1047    and clear TREE_USED on all local variables.  */
1048
1049 static void
1050 clear_tree_used (tree block)
1051 {
1052   tree t;
1053
1054   for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1055     /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
1056       TREE_USED (t) = 0;
1057
1058   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1059     clear_tree_used (t);
1060 }
1061
1062 /* Examine TYPE and determine a bit mask of the following features.  */
1063
1064 #define SPCT_HAS_LARGE_CHAR_ARRAY       1
1065 #define SPCT_HAS_SMALL_CHAR_ARRAY       2
1066 #define SPCT_HAS_ARRAY                  4
1067 #define SPCT_HAS_AGGREGATE              8
1068
1069 static unsigned int
1070 stack_protect_classify_type (tree type)
1071 {
1072   unsigned int ret = 0;
1073   tree t;
1074
1075   switch (TREE_CODE (type))
1076     {
1077     case ARRAY_TYPE:
1078       t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1079       if (t == char_type_node
1080           || t == signed_char_type_node
1081           || t == unsigned_char_type_node)
1082         {
1083           unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
1084           unsigned HOST_WIDE_INT len;
1085
1086           if (!TYPE_SIZE_UNIT (type)
1087               || !host_integerp (TYPE_SIZE_UNIT (type), 1))
1088             len = max;
1089           else
1090             len = tree_low_cst (TYPE_SIZE_UNIT (type), 1);
1091
1092           if (len < max)
1093             ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
1094           else
1095             ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
1096         }
1097       else
1098         ret = SPCT_HAS_ARRAY;
1099       break;
1100
1101     case UNION_TYPE:
1102     case QUAL_UNION_TYPE:
1103     case RECORD_TYPE:
1104       ret = SPCT_HAS_AGGREGATE;
1105       for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
1106         if (TREE_CODE (t) == FIELD_DECL)
1107           ret |= stack_protect_classify_type (TREE_TYPE (t));
1108       break;
1109
1110     default:
1111       break;
1112     }
1113
1114   return ret;
1115 }
1116
1117 /* Return nonzero if DECL should be segregated into the "vulnerable" upper
1118    part of the local stack frame.  Remember if we ever return nonzero for
1119    any variable in this function.  The return value is the phase number in
1120    which the variable should be allocated.  */
1121
1122 static int
1123 stack_protect_decl_phase (tree decl)
1124 {
1125   unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
1126   int ret = 0;
1127
1128   if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
1129     has_short_buffer = true;
1130
1131   if (flag_stack_protect == 2)
1132     {
1133       if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
1134           && !(bits & SPCT_HAS_AGGREGATE))
1135         ret = 1;
1136       else if (bits & SPCT_HAS_ARRAY)
1137         ret = 2;
1138     }
1139   else
1140     ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
1141
1142   if (ret)
1143     has_protected_decls = true;
1144
1145   return ret;
1146 }
1147
1148 /* Two helper routines that check for phase 1 and phase 2.  These are used
1149    as callbacks for expand_stack_vars.  */
1150
1151 static bool
1152 stack_protect_decl_phase_1 (tree decl)
1153 {
1154   return stack_protect_decl_phase (decl) == 1;
1155 }
1156
1157 static bool
1158 stack_protect_decl_phase_2 (tree decl)
1159 {
1160   return stack_protect_decl_phase (decl) == 2;
1161 }
1162
1163 /* Ensure that variables in different stack protection phases conflict
1164    so that they are not merged and share the same stack slot.  */
1165
1166 static void
1167 add_stack_protection_conflicts (void)
1168 {
1169   size_t i, j, n = stack_vars_num;
1170   unsigned char *phase;
1171
1172   phase = XNEWVEC (unsigned char, n);
1173   for (i = 0; i < n; ++i)
1174     phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
1175
1176   for (i = 0; i < n; ++i)
1177     {
1178       unsigned char ph_i = phase[i];
1179       for (j = 0; j < i; ++j)
1180         if (ph_i != phase[j])
1181           add_stack_var_conflict (i, j);
1182     }
1183
1184   XDELETEVEC (phase);
1185 }
1186
1187 /* Create a decl for the guard at the top of the stack frame.  */
1188
1189 static void
1190 create_stack_guard (void)
1191 {
1192   tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
1193                            VAR_DECL, NULL, ptr_type_node);
1194   TREE_THIS_VOLATILE (guard) = 1;
1195   TREE_USED (guard) = 1;
1196   expand_one_stack_var (guard);
1197   crtl->stack_protect_guard = guard;
1198 }
1199
1200 /* A subroutine of expand_used_vars.  Walk down through the BLOCK tree
1201    expanding variables.  Those variables that can be put into registers
1202    are allocated pseudos; those that can't are put on the stack.
1203
1204    TOPLEVEL is true if this is the outermost BLOCK.  */
1205
1206 static HOST_WIDE_INT
1207 account_used_vars_for_block (tree block, bool toplevel)
1208 {
1209   tree t;
1210   HOST_WIDE_INT size = 0;
1211
1212   /* Expand all variables at this level.  */
1213   for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
1214     if (TREE_USED (t))
1215       size += expand_one_var (t, toplevel, false);
1216
1217   /* Expand all variables at containing levels.  */
1218   for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
1219     size += account_used_vars_for_block (t, false);
1220
1221   return size;
1222 }
1223
1224 /* Prepare for expanding variables.  */
1225 static void
1226 init_vars_expansion (void)
1227 {
1228   tree t;
1229   unsigned ix;
1230   /* Set TREE_USED on all variables in the local_decls.  */
1231   FOR_EACH_LOCAL_DECL (cfun, ix, t)
1232     TREE_USED (t) = 1;
1233
1234   /* Clear TREE_USED on all variables associated with a block scope.  */
1235   clear_tree_used (DECL_INITIAL (current_function_decl));
1236
1237   /* Initialize local stack smashing state.  */
1238   has_protected_decls = false;
1239   has_short_buffer = false;
1240 }
1241
1242 /* Free up stack variable graph data.  */
1243 static void
1244 fini_vars_expansion (void)
1245 {
1246   size_t i, n = stack_vars_num;
1247   for (i = 0; i < n; i++)
1248     BITMAP_FREE (stack_vars[i].conflicts);
1249   XDELETEVEC (stack_vars);
1250   XDELETEVEC (stack_vars_sorted);
1251   stack_vars = NULL;
1252   stack_vars_alloc = stack_vars_num = 0;
1253 }
1254
1255 /* Make a fair guess for the size of the stack frame of the decl
1256    passed.  This doesn't have to be exact, the result is only used
1257    in the inline heuristics.  So we don't want to run the full stack
1258    var packing algorithm (which is quadratic in the number of stack
1259    vars).  Instead, we calculate the total size of all stack vars.
1260    This turns out to be a pretty fair estimate -- packing of stack
1261    vars doesn't happen very often.  */
1262
1263 HOST_WIDE_INT
1264 estimated_stack_frame_size (tree decl)
1265 {
1266   HOST_WIDE_INT size = 0;
1267   size_t i;
1268   tree var, outer_block = DECL_INITIAL (current_function_decl);
1269   unsigned ix;
1270   tree old_cur_fun_decl = current_function_decl;
1271   current_function_decl = decl;
1272   push_cfun (DECL_STRUCT_FUNCTION (decl));
1273
1274   init_vars_expansion ();
1275
1276   FOR_EACH_LOCAL_DECL (cfun, ix, var)
1277     {
1278       if (TREE_USED (var))
1279         size += expand_one_var (var, true, false);
1280       TREE_USED (var) = 1;
1281     }
1282   size += account_used_vars_for_block (outer_block, true);
1283
1284   if (stack_vars_num > 0)
1285     {
1286       /* Fake sorting the stack vars for account_stack_vars ().  */
1287       stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1288       for (i = 0; i < stack_vars_num; ++i)
1289         stack_vars_sorted[i] = i;
1290       size += account_stack_vars ();
1291       fini_vars_expansion ();
1292     }
1293   pop_cfun ();
1294   current_function_decl = old_cur_fun_decl;
1295   return size;
1296 }
1297
1298 /* Expand all variables used in the function.  */
1299
1300 static void
1301 expand_used_vars (void)
1302 {
1303   tree var, outer_block = DECL_INITIAL (current_function_decl);
1304   VEC(tree,heap) *maybe_local_decls = NULL;
1305   unsigned i;
1306   unsigned len;
1307
1308   /* Compute the phase of the stack frame for this function.  */
1309   {
1310     int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1311     int off = STARTING_FRAME_OFFSET % align;
1312     frame_phase = off ? align - off : 0;
1313   }
1314
1315   init_vars_expansion ();
1316
1317   for (i = 0; i < SA.map->num_partitions; i++)
1318     {
1319       tree var = partition_to_var (SA.map, i);
1320
1321       gcc_assert (is_gimple_reg (var));
1322       if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1323         expand_one_var (var, true, true);
1324       else
1325         {
1326           /* This is a PARM_DECL or RESULT_DECL.  For those partitions that
1327              contain the default def (representing the parm or result itself)
1328              we don't do anything here.  But those which don't contain the
1329              default def (representing a temporary based on the parm/result)
1330              we need to allocate space just like for normal VAR_DECLs.  */
1331           if (!bitmap_bit_p (SA.partition_has_default_def, i))
1332             {
1333               expand_one_var (var, true, true);
1334               gcc_assert (SA.partition_to_pseudo[i]);
1335             }
1336         }
1337     }
1338
1339   /* At this point all variables on the local_decls with TREE_USED
1340      set are not associated with any block scope.  Lay them out.  */
1341
1342   len = VEC_length (tree, cfun->local_decls);
1343   FOR_EACH_LOCAL_DECL (cfun, i, var)
1344     {
1345       bool expand_now = false;
1346
1347       /* Expanded above already.  */
1348       if (is_gimple_reg (var))
1349         {
1350           TREE_USED (var) = 0;
1351           goto next;
1352         }
1353       /* We didn't set a block for static or extern because it's hard
1354          to tell the difference between a global variable (re)declared
1355          in a local scope, and one that's really declared there to
1356          begin with.  And it doesn't really matter much, since we're
1357          not giving them stack space.  Expand them now.  */
1358       else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1359         expand_now = true;
1360
1361       /* If the variable is not associated with any block, then it
1362          was created by the optimizers, and could be live anywhere
1363          in the function.  */
1364       else if (TREE_USED (var))
1365         expand_now = true;
1366
1367       /* Finally, mark all variables on the list as used.  We'll use
1368          this in a moment when we expand those associated with scopes.  */
1369       TREE_USED (var) = 1;
1370
1371       if (expand_now)
1372         expand_one_var (var, true, true);
1373
1374     next:
1375       if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1376         {
1377           rtx rtl = DECL_RTL_IF_SET (var);
1378
1379           /* Keep artificial non-ignored vars in cfun->local_decls
1380              chain until instantiate_decls.  */
1381           if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1382             add_local_decl (cfun, var);
1383           else if (rtl == NULL_RTX)
1384             /* If rtl isn't set yet, which can happen e.g. with
1385                -fstack-protector, retry before returning from this
1386                function.  */
1387             VEC_safe_push (tree, heap, maybe_local_decls, var);
1388         }
1389     }
1390
1391   /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
1392
1393      +-----------------+-----------------+
1394      | ...processed... | ...duplicates...|
1395      +-----------------+-----------------+
1396                        ^
1397                        +-- LEN points here.
1398
1399      We just want the duplicates, as those are the artificial
1400      non-ignored vars that we want to keep until instantiate_decls.
1401      Move them down and truncate the array.  */
1402   if (!VEC_empty (tree, cfun->local_decls))
1403     VEC_block_remove (tree, cfun->local_decls, 0, len);
1404
1405   /* At this point, all variables within the block tree with TREE_USED
1406      set are actually used by the optimized function.  Lay them out.  */
1407   expand_used_vars_for_block (outer_block, true);
1408
1409   if (stack_vars_num > 0)
1410     {
1411       /* Due to the way alias sets work, no variables with non-conflicting
1412          alias sets may be assigned the same address.  Add conflicts to
1413          reflect this.  */
1414       add_alias_set_conflicts ();
1415
1416       /* If stack protection is enabled, we don't share space between
1417          vulnerable data and non-vulnerable data.  */
1418       if (flag_stack_protect)
1419         add_stack_protection_conflicts ();
1420
1421       /* Now that we have collected all stack variables, and have computed a
1422          minimal interference graph, attempt to save some stack space.  */
1423       partition_stack_vars ();
1424       if (dump_file)
1425         dump_stack_var_partition ();
1426     }
1427
1428   /* There are several conditions under which we should create a
1429      stack guard: protect-all, alloca used, protected decls present.  */
1430   if (flag_stack_protect == 2
1431       || (flag_stack_protect
1432           && (cfun->calls_alloca || has_protected_decls)))
1433     create_stack_guard ();
1434
1435   /* Assign rtl to each variable based on these partitions.  */
1436   if (stack_vars_num > 0)
1437     {
1438       /* Reorder decls to be protected by iterating over the variables
1439          array multiple times, and allocating out of each phase in turn.  */
1440       /* ??? We could probably integrate this into the qsort we did
1441          earlier, such that we naturally see these variables first,
1442          and thus naturally allocate things in the right order.  */
1443       if (has_protected_decls)
1444         {
1445           /* Phase 1 contains only character arrays.  */
1446           expand_stack_vars (stack_protect_decl_phase_1);
1447
1448           /* Phase 2 contains other kinds of arrays.  */
1449           if (flag_stack_protect == 2)
1450             expand_stack_vars (stack_protect_decl_phase_2);
1451         }
1452
1453       expand_stack_vars (NULL);
1454
1455       fini_vars_expansion ();
1456     }
1457
1458   /* If there were any artificial non-ignored vars without rtl
1459      found earlier, see if deferred stack allocation hasn't assigned
1460      rtl to them.  */
1461   FOR_EACH_VEC_ELT_REVERSE (tree, maybe_local_decls, i, var)
1462     {
1463       rtx rtl = DECL_RTL_IF_SET (var);
1464
1465       /* Keep artificial non-ignored vars in cfun->local_decls
1466          chain until instantiate_decls.  */
1467       if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1468         add_local_decl (cfun, var);
1469     }
1470   VEC_free (tree, heap, maybe_local_decls);
1471
1472   /* If the target requires that FRAME_OFFSET be aligned, do it.  */
1473   if (STACK_ALIGNMENT_NEEDED)
1474     {
1475       HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1476       if (!FRAME_GROWS_DOWNWARD)
1477         frame_offset += align - 1;
1478       frame_offset &= -align;
1479     }
1480 }
1481
1482
1483 /* If we need to produce a detailed dump, print the tree representation
1484    for STMT to the dump file.  SINCE is the last RTX after which the RTL
1485    generated for STMT should have been appended.  */
1486
1487 static void
1488 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1489 {
1490   if (dump_file && (dump_flags & TDF_DETAILS))
1491     {
1492       fprintf (dump_file, "\n;; ");
1493       print_gimple_stmt (dump_file, stmt, 0,
1494                          TDF_SLIM | (dump_flags & TDF_LINENO));
1495       fprintf (dump_file, "\n");
1496
1497       print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1498     }
1499 }
1500
1501 /* Maps the blocks that do not contain tree labels to rtx labels.  */
1502
1503 static struct pointer_map_t *lab_rtx_for_bb;
1504
1505 /* Returns the label_rtx expression for a label starting basic block BB.  */
1506
1507 static rtx
1508 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1509 {
1510   gimple_stmt_iterator gsi;
1511   tree lab;
1512   gimple lab_stmt;
1513   void **elt;
1514
1515   if (bb->flags & BB_RTL)
1516     return block_label (bb);
1517
1518   elt = pointer_map_contains (lab_rtx_for_bb, bb);
1519   if (elt)
1520     return (rtx) *elt;
1521
1522   /* Find the tree label if it is present.  */
1523
1524   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1525     {
1526       lab_stmt = gsi_stmt (gsi);
1527       if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1528         break;
1529
1530       lab = gimple_label_label (lab_stmt);
1531       if (DECL_NONLOCAL (lab))
1532         break;
1533
1534       return label_rtx (lab);
1535     }
1536
1537   elt = pointer_map_insert (lab_rtx_for_bb, bb);
1538   *elt = gen_label_rtx ();
1539   return (rtx) *elt;
1540 }
1541
1542
1543 /* A subroutine of expand_gimple_cond.  Given E, a fallthrough edge
1544    of a basic block where we just expanded the conditional at the end,
1545    possibly clean up the CFG and instruction sequence.  LAST is the
1546    last instruction before the just emitted jump sequence.  */
1547
1548 static void
1549 maybe_cleanup_end_of_block (edge e, rtx last)
1550 {
1551   /* Special case: when jumpif decides that the condition is
1552      trivial it emits an unconditional jump (and the necessary
1553      barrier).  But we still have two edges, the fallthru one is
1554      wrong.  purge_dead_edges would clean this up later.  Unfortunately
1555      we have to insert insns (and split edges) before
1556      find_many_sub_basic_blocks and hence before purge_dead_edges.
1557      But splitting edges might create new blocks which depend on the
1558      fact that if there are two edges there's no barrier.  So the
1559      barrier would get lost and verify_flow_info would ICE.  Instead
1560      of auditing all edge splitters to care for the barrier (which
1561      normally isn't there in a cleaned CFG), fix it here.  */
1562   if (BARRIER_P (get_last_insn ()))
1563     {
1564       rtx insn;
1565       remove_edge (e);
1566       /* Now, we have a single successor block, if we have insns to
1567          insert on the remaining edge we potentially will insert
1568          it at the end of this block (if the dest block isn't feasible)
1569          in order to avoid splitting the edge.  This insertion will take
1570          place in front of the last jump.  But we might have emitted
1571          multiple jumps (conditional and one unconditional) to the
1572          same destination.  Inserting in front of the last one then
1573          is a problem.  See PR 40021.  We fix this by deleting all
1574          jumps except the last unconditional one.  */
1575       insn = PREV_INSN (get_last_insn ());
1576       /* Make sure we have an unconditional jump.  Otherwise we're
1577          confused.  */
1578       gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
1579       for (insn = PREV_INSN (insn); insn != last;)
1580         {
1581           insn = PREV_INSN (insn);
1582           if (JUMP_P (NEXT_INSN (insn)))
1583             delete_insn (NEXT_INSN (insn));
1584         }
1585     }
1586 }
1587
1588 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_COND.
1589    Returns a new basic block if we've terminated the current basic
1590    block and created a new one.  */
1591
1592 static basic_block
1593 expand_gimple_cond (basic_block bb, gimple stmt)
1594 {
1595   basic_block new_bb, dest;
1596   edge new_edge;
1597   edge true_edge;
1598   edge false_edge;
1599   rtx last2, last;
1600   enum tree_code code;
1601   tree op0, op1;
1602
1603   code = gimple_cond_code (stmt);
1604   op0 = gimple_cond_lhs (stmt);
1605   op1 = gimple_cond_rhs (stmt);
1606   /* We're sometimes presented with such code:
1607        D.123_1 = x < y;
1608        if (D.123_1 != 0)
1609          ...
1610      This would expand to two comparisons which then later might
1611      be cleaned up by combine.  But some pattern matchers like if-conversion
1612      work better when there's only one compare, so make up for this
1613      here as special exception if TER would have made the same change.  */
1614   if (gimple_cond_single_var_p (stmt)
1615       && SA.values
1616       && TREE_CODE (op0) == SSA_NAME
1617       && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
1618     {
1619       gimple second = SSA_NAME_DEF_STMT (op0);
1620       if (gimple_code (second) == GIMPLE_ASSIGN)
1621         {
1622           enum tree_code code2 = gimple_assign_rhs_code (second);
1623           if (TREE_CODE_CLASS (code2) == tcc_comparison)
1624             {
1625               code = code2;
1626               op0 = gimple_assign_rhs1 (second);
1627               op1 = gimple_assign_rhs2 (second);
1628             }
1629           /* If jumps are cheap turn some more codes into
1630              jumpy sequences.  */
1631           else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
1632             {
1633               if ((code2 == BIT_AND_EXPR
1634                    && TYPE_PRECISION (TREE_TYPE (op0)) == 1
1635                    && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
1636                   || code2 == TRUTH_AND_EXPR)
1637                 {
1638                   code = TRUTH_ANDIF_EXPR;
1639                   op0 = gimple_assign_rhs1 (second);
1640                   op1 = gimple_assign_rhs2 (second);
1641                 }
1642               else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
1643                 {
1644                   code = TRUTH_ORIF_EXPR;
1645                   op0 = gimple_assign_rhs1 (second);
1646                   op1 = gimple_assign_rhs2 (second);
1647                 }
1648             }
1649         }
1650     }
1651
1652   last2 = last = get_last_insn ();
1653
1654   extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1655   if (gimple_has_location (stmt))
1656     {
1657       set_curr_insn_source_location (gimple_location (stmt));
1658       set_curr_insn_block (gimple_block (stmt));
1659     }
1660
1661   /* These flags have no purpose in RTL land.  */
1662   true_edge->flags &= ~EDGE_TRUE_VALUE;
1663   false_edge->flags &= ~EDGE_FALSE_VALUE;
1664
1665   /* We can either have a pure conditional jump with one fallthru edge or
1666      two-way jump that needs to be decomposed into two basic blocks.  */
1667   if (false_edge->dest == bb->next_bb)
1668     {
1669       jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1670                 true_edge->probability);
1671       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1672       if (true_edge->goto_locus)
1673         {
1674           set_curr_insn_source_location (true_edge->goto_locus);
1675           set_curr_insn_block (true_edge->goto_block);
1676           true_edge->goto_locus = curr_insn_locator ();
1677         }
1678       true_edge->goto_block = NULL;
1679       false_edge->flags |= EDGE_FALLTHRU;
1680       maybe_cleanup_end_of_block (false_edge, last);
1681       return NULL;
1682     }
1683   if (true_edge->dest == bb->next_bb)
1684     {
1685       jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
1686                    false_edge->probability);
1687       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1688       if (false_edge->goto_locus)
1689         {
1690           set_curr_insn_source_location (false_edge->goto_locus);
1691           set_curr_insn_block (false_edge->goto_block);
1692           false_edge->goto_locus = curr_insn_locator ();
1693         }
1694       false_edge->goto_block = NULL;
1695       true_edge->flags |= EDGE_FALLTHRU;
1696       maybe_cleanup_end_of_block (true_edge, last);
1697       return NULL;
1698     }
1699
1700   jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1701             true_edge->probability);
1702   last = get_last_insn ();
1703   if (false_edge->goto_locus)
1704     {
1705       set_curr_insn_source_location (false_edge->goto_locus);
1706       set_curr_insn_block (false_edge->goto_block);
1707       false_edge->goto_locus = curr_insn_locator ();
1708     }
1709   false_edge->goto_block = NULL;
1710   emit_jump (label_rtx_for_bb (false_edge->dest));
1711
1712   BB_END (bb) = last;
1713   if (BARRIER_P (BB_END (bb)))
1714     BB_END (bb) = PREV_INSN (BB_END (bb));
1715   update_bb_for_insn (bb);
1716
1717   new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1718   dest = false_edge->dest;
1719   redirect_edge_succ (false_edge, new_bb);
1720   false_edge->flags |= EDGE_FALLTHRU;
1721   new_bb->count = false_edge->count;
1722   new_bb->frequency = EDGE_FREQUENCY (false_edge);
1723   new_edge = make_edge (new_bb, dest, 0);
1724   new_edge->probability = REG_BR_PROB_BASE;
1725   new_edge->count = new_bb->count;
1726   if (BARRIER_P (BB_END (new_bb)))
1727     BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1728   update_bb_for_insn (new_bb);
1729
1730   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1731
1732   if (true_edge->goto_locus)
1733     {
1734       set_curr_insn_source_location (true_edge->goto_locus);
1735       set_curr_insn_block (true_edge->goto_block);
1736       true_edge->goto_locus = curr_insn_locator ();
1737     }
1738   true_edge->goto_block = NULL;
1739
1740   return new_bb;
1741 }
1742
1743 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
1744    statement STMT.  */
1745
1746 static void
1747 expand_call_stmt (gimple stmt)
1748 {
1749   tree exp;
1750   tree lhs = gimple_call_lhs (stmt);
1751   size_t i;
1752   bool builtin_p;
1753   tree decl;
1754
1755   exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
1756
1757   CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
1758   decl = gimple_call_fndecl (stmt);
1759   builtin_p = decl && DECL_BUILT_IN (decl);
1760
1761   TREE_TYPE (exp) = gimple_call_return_type (stmt);
1762   CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
1763
1764   for (i = 0; i < gimple_call_num_args (stmt); i++)
1765     {
1766       tree arg = gimple_call_arg (stmt, i);
1767       gimple def;
1768       /* TER addresses into arguments of builtin functions so we have a
1769          chance to infer more correct alignment information.  See PR39954.  */
1770       if (builtin_p
1771           && TREE_CODE (arg) == SSA_NAME
1772           && (def = get_gimple_for_ssa_name (arg))
1773           && gimple_assign_rhs_code (def) == ADDR_EXPR)
1774         arg = gimple_assign_rhs1 (def);
1775       CALL_EXPR_ARG (exp, i) = arg;
1776     }
1777
1778   if (gimple_has_side_effects (stmt))
1779     TREE_SIDE_EFFECTS (exp) = 1;
1780
1781   if (gimple_call_nothrow_p (stmt))
1782     TREE_NOTHROW (exp) = 1;
1783
1784   CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
1785   CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
1786   CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
1787   CALL_CANNOT_INLINE_P (exp) = gimple_call_cannot_inline_p (stmt);
1788   CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
1789   SET_EXPR_LOCATION (exp, gimple_location (stmt));
1790   TREE_BLOCK (exp) = gimple_block (stmt);
1791
1792   if (lhs)
1793     expand_assignment (lhs, exp, false);
1794   else
1795     expand_expr_real_1 (exp, const0_rtx, VOIDmode, EXPAND_NORMAL, NULL);
1796 }
1797
1798 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
1799    STMT that doesn't require special handling for outgoing edges.  That
1800    is no tailcalls and no GIMPLE_COND.  */
1801
1802 static void
1803 expand_gimple_stmt_1 (gimple stmt)
1804 {
1805   tree op0;
1806   switch (gimple_code (stmt))
1807     {
1808     case GIMPLE_GOTO:
1809       op0 = gimple_goto_dest (stmt);
1810       if (TREE_CODE (op0) == LABEL_DECL)
1811         expand_goto (op0);
1812       else
1813         expand_computed_goto (op0);
1814       break;
1815     case GIMPLE_LABEL:
1816       expand_label (gimple_label_label (stmt));
1817       break;
1818     case GIMPLE_NOP:
1819     case GIMPLE_PREDICT:
1820       break;
1821     case GIMPLE_SWITCH:
1822       expand_case (stmt);
1823       break;
1824     case GIMPLE_ASM:
1825       expand_asm_stmt (stmt);
1826       break;
1827     case GIMPLE_CALL:
1828       expand_call_stmt (stmt);
1829       break;
1830
1831     case GIMPLE_RETURN:
1832       op0 = gimple_return_retval (stmt);
1833
1834       if (op0 && op0 != error_mark_node)
1835         {
1836           tree result = DECL_RESULT (current_function_decl);
1837
1838           /* If we are not returning the current function's RESULT_DECL,
1839              build an assignment to it.  */
1840           if (op0 != result)
1841             {
1842               /* I believe that a function's RESULT_DECL is unique.  */
1843               gcc_assert (TREE_CODE (op0) != RESULT_DECL);
1844
1845               /* ??? We'd like to use simply expand_assignment here,
1846                  but this fails if the value is of BLKmode but the return
1847                  decl is a register.  expand_return has special handling
1848                  for this combination, which eventually should move
1849                  to common code.  See comments there.  Until then, let's
1850                  build a modify expression :-/  */
1851               op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
1852                             result, op0);
1853             }
1854         }
1855       if (!op0)
1856         expand_null_return ();
1857       else
1858         expand_return (op0);
1859       break;
1860
1861     case GIMPLE_ASSIGN:
1862       {
1863         tree lhs = gimple_assign_lhs (stmt);
1864
1865         /* Tree expand used to fiddle with |= and &= of two bitfield
1866            COMPONENT_REFs here.  This can't happen with gimple, the LHS
1867            of binary assigns must be a gimple reg.  */
1868
1869         if (TREE_CODE (lhs) != SSA_NAME
1870             || get_gimple_rhs_class (gimple_expr_code (stmt))
1871                == GIMPLE_SINGLE_RHS)
1872           {
1873             tree rhs = gimple_assign_rhs1 (stmt);
1874             gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
1875                         == GIMPLE_SINGLE_RHS);
1876             if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
1877               SET_EXPR_LOCATION (rhs, gimple_location (stmt));
1878             expand_assignment (lhs, rhs,
1879                                gimple_assign_nontemporal_move_p (stmt));
1880           }
1881         else
1882           {
1883             rtx target, temp;
1884             bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
1885             struct separate_ops ops;
1886             bool promoted = false;
1887
1888             target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1889             if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
1890               promoted = true;
1891
1892             ops.code = gimple_assign_rhs_code (stmt);
1893             ops.type = TREE_TYPE (lhs);
1894             switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
1895               {
1896                 case GIMPLE_TERNARY_RHS:
1897                   ops.op2 = gimple_assign_rhs3 (stmt);
1898                   /* Fallthru */
1899                 case GIMPLE_BINARY_RHS:
1900                   ops.op1 = gimple_assign_rhs2 (stmt);
1901                   /* Fallthru */
1902                 case GIMPLE_UNARY_RHS:
1903                   ops.op0 = gimple_assign_rhs1 (stmt);
1904                   break;
1905                 default:
1906                   gcc_unreachable ();
1907               }
1908             ops.location = gimple_location (stmt);
1909
1910             /* If we want to use a nontemporal store, force the value to
1911                register first.  If we store into a promoted register,
1912                don't directly expand to target.  */
1913             temp = nontemporal || promoted ? NULL_RTX : target;
1914             temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
1915                                        EXPAND_NORMAL);
1916
1917             if (temp == target)
1918               ;
1919             else if (promoted)
1920               {
1921                 int unsignedp = SUBREG_PROMOTED_UNSIGNED_P (target);
1922                 /* If TEMP is a VOIDmode constant, use convert_modes to make
1923                    sure that we properly convert it.  */
1924                 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
1925                   {
1926                     temp = convert_modes (GET_MODE (target),
1927                                           TYPE_MODE (ops.type),
1928                                           temp, unsignedp);
1929                     temp = convert_modes (GET_MODE (SUBREG_REG (target)),
1930                                           GET_MODE (target), temp, unsignedp);
1931                   }
1932
1933                 convert_move (SUBREG_REG (target), temp, unsignedp);
1934               }
1935             else if (nontemporal && emit_storent_insn (target, temp))
1936               ;
1937             else
1938               {
1939                 temp = force_operand (temp, target);
1940                 if (temp != target)
1941                   emit_move_insn (target, temp);
1942               }
1943           }
1944       }
1945       break;
1946
1947     default:
1948       gcc_unreachable ();
1949     }
1950 }
1951
1952 /* Expand one gimple statement STMT and return the last RTL instruction
1953    before any of the newly generated ones.
1954
1955    In addition to generating the necessary RTL instructions this also
1956    sets REG_EH_REGION notes if necessary and sets the current source
1957    location for diagnostics.  */
1958
1959 static rtx
1960 expand_gimple_stmt (gimple stmt)
1961 {
1962   int lp_nr = 0;
1963   rtx last = NULL;
1964   location_t saved_location = input_location;
1965
1966   last = get_last_insn ();
1967
1968   /* If this is an expression of some kind and it has an associated line
1969      number, then emit the line number before expanding the expression.
1970
1971      We need to save and restore the file and line information so that
1972      errors discovered during expansion are emitted with the right
1973      information.  It would be better of the diagnostic routines
1974      used the file/line information embedded in the tree nodes rather
1975      than globals.  */
1976   gcc_assert (cfun);
1977
1978   if (gimple_has_location (stmt))
1979     {
1980       input_location = gimple_location (stmt);
1981       set_curr_insn_source_location (input_location);
1982
1983       /* Record where the insns produced belong.  */
1984       set_curr_insn_block (gimple_block (stmt));
1985     }
1986
1987   expand_gimple_stmt_1 (stmt);
1988   /* Free any temporaries used to evaluate this statement.  */
1989   free_temp_slots ();
1990
1991   input_location = saved_location;
1992
1993   /* Mark all insns that may trap.  */
1994   lp_nr = lookup_stmt_eh_lp (stmt);
1995   if (lp_nr)
1996     {
1997       rtx insn;
1998       for (insn = next_real_insn (last); insn;
1999            insn = next_real_insn (insn))
2000         {
2001           if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
2002               /* If we want exceptions for non-call insns, any
2003                  may_trap_p instruction may throw.  */
2004               && GET_CODE (PATTERN (insn)) != CLOBBER
2005               && GET_CODE (PATTERN (insn)) != USE
2006               && insn_could_throw_p (insn))
2007             make_reg_eh_region_note (insn, 0, lp_nr);
2008         }
2009     }
2010
2011   return last;
2012 }
2013
2014 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_CALL
2015    that has CALL_EXPR_TAILCALL set.  Returns non-null if we actually
2016    generated a tail call (something that might be denied by the ABI
2017    rules governing the call; see calls.c).
2018
2019    Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
2020    can still reach the rest of BB.  The case here is __builtin_sqrt,
2021    where the NaN result goes through the external function (with a
2022    tailcall) and the normal result happens via a sqrt instruction.  */
2023
2024 static basic_block
2025 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
2026 {
2027   rtx last2, last;
2028   edge e;
2029   edge_iterator ei;
2030   int probability;
2031   gcov_type count;
2032
2033   last2 = last = expand_gimple_stmt (stmt);
2034
2035   for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
2036     if (CALL_P (last) && SIBLING_CALL_P (last))
2037       goto found;
2038
2039   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2040
2041   *can_fallthru = true;
2042   return NULL;
2043
2044  found:
2045   /* ??? Wouldn't it be better to just reset any pending stack adjust?
2046      Any instructions emitted here are about to be deleted.  */
2047   do_pending_stack_adjust ();
2048
2049   /* Remove any non-eh, non-abnormal edges that don't go to exit.  */
2050   /* ??? I.e. the fallthrough edge.  HOWEVER!  If there were to be
2051      EH or abnormal edges, we shouldn't have created a tail call in
2052      the first place.  So it seems to me we should just be removing
2053      all edges here, or redirecting the existing fallthru edge to
2054      the exit block.  */
2055
2056   probability = 0;
2057   count = 0;
2058
2059   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2060     {
2061       if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
2062         {
2063           if (e->dest != EXIT_BLOCK_PTR)
2064             {
2065               e->dest->count -= e->count;
2066               e->dest->frequency -= EDGE_FREQUENCY (e);
2067               if (e->dest->count < 0)
2068                 e->dest->count = 0;
2069               if (e->dest->frequency < 0)
2070                 e->dest->frequency = 0;
2071             }
2072           count += e->count;
2073           probability += e->probability;
2074           remove_edge (e);
2075         }
2076       else
2077         ei_next (&ei);
2078     }
2079
2080   /* This is somewhat ugly: the call_expr expander often emits instructions
2081      after the sibcall (to perform the function return).  These confuse the
2082      find_many_sub_basic_blocks code, so we need to get rid of these.  */
2083   last = NEXT_INSN (last);
2084   gcc_assert (BARRIER_P (last));
2085
2086   *can_fallthru = false;
2087   while (NEXT_INSN (last))
2088     {
2089       /* For instance an sqrt builtin expander expands if with
2090          sibcall in the then and label for `else`.  */
2091       if (LABEL_P (NEXT_INSN (last)))
2092         {
2093           *can_fallthru = true;
2094           break;
2095         }
2096       delete_insn (NEXT_INSN (last));
2097     }
2098
2099   e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
2100   e->probability += probability;
2101   e->count += count;
2102   BB_END (bb) = last;
2103   update_bb_for_insn (bb);
2104
2105   if (NEXT_INSN (last))
2106     {
2107       bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2108
2109       last = BB_END (bb);
2110       if (BARRIER_P (last))
2111         BB_END (bb) = PREV_INSN (last);
2112     }
2113
2114   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2115
2116   return bb;
2117 }
2118
2119 /* Return the difference between the floor and the truncated result of
2120    a signed division by OP1 with remainder MOD.  */
2121 static rtx
2122 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2123 {
2124   /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
2125   return gen_rtx_IF_THEN_ELSE
2126     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2127      gen_rtx_IF_THEN_ELSE
2128      (mode, gen_rtx_LT (BImode,
2129                         gen_rtx_DIV (mode, op1, mod),
2130                         const0_rtx),
2131       constm1_rtx, const0_rtx),
2132      const0_rtx);
2133 }
2134
2135 /* Return the difference between the ceil and the truncated result of
2136    a signed division by OP1 with remainder MOD.  */
2137 static rtx
2138 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2139 {
2140   /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
2141   return gen_rtx_IF_THEN_ELSE
2142     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2143      gen_rtx_IF_THEN_ELSE
2144      (mode, gen_rtx_GT (BImode,
2145                         gen_rtx_DIV (mode, op1, mod),
2146                         const0_rtx),
2147       const1_rtx, const0_rtx),
2148      const0_rtx);
2149 }
2150
2151 /* Return the difference between the ceil and the truncated result of
2152    an unsigned division by OP1 with remainder MOD.  */
2153 static rtx
2154 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
2155 {
2156   /* (mod != 0 ? 1 : 0) */
2157   return gen_rtx_IF_THEN_ELSE
2158     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2159      const1_rtx, const0_rtx);
2160 }
2161
2162 /* Return the difference between the rounded and the truncated result
2163    of a signed division by OP1 with remainder MOD.  Halfway cases are
2164    rounded away from zero, rather than to the nearest even number.  */
2165 static rtx
2166 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2167 {
2168   /* (abs (mod) >= abs (op1) - abs (mod)
2169       ? (op1 / mod > 0 ? 1 : -1)
2170       : 0) */
2171   return gen_rtx_IF_THEN_ELSE
2172     (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
2173                        gen_rtx_MINUS (mode,
2174                                       gen_rtx_ABS (mode, op1),
2175                                       gen_rtx_ABS (mode, mod))),
2176      gen_rtx_IF_THEN_ELSE
2177      (mode, gen_rtx_GT (BImode,
2178                         gen_rtx_DIV (mode, op1, mod),
2179                         const0_rtx),
2180       const1_rtx, constm1_rtx),
2181      const0_rtx);
2182 }
2183
2184 /* Return the difference between the rounded and the truncated result
2185    of a unsigned division by OP1 with remainder MOD.  Halfway cases
2186    are rounded away from zero, rather than to the nearest even
2187    number.  */
2188 static rtx
2189 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2190 {
2191   /* (mod >= op1 - mod ? 1 : 0) */
2192   return gen_rtx_IF_THEN_ELSE
2193     (mode, gen_rtx_GE (BImode, mod,
2194                        gen_rtx_MINUS (mode, op1, mod)),
2195      const1_rtx, const0_rtx);
2196 }
2197
2198 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
2199    any rtl.  */
2200
2201 static rtx
2202 convert_debug_memory_address (enum machine_mode mode, rtx x)
2203 {
2204   enum machine_mode xmode = GET_MODE (x);
2205
2206 #ifndef POINTERS_EXTEND_UNSIGNED
2207   gcc_assert (mode == Pmode);
2208   gcc_assert (xmode == mode || xmode == VOIDmode);
2209 #else
2210   gcc_assert (mode == Pmode || mode == ptr_mode);
2211
2212   if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
2213     return x;
2214
2215   if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (xmode))
2216     x = simplify_gen_subreg (mode, x, xmode,
2217                              subreg_lowpart_offset
2218                              (mode, xmode));
2219   else if (POINTERS_EXTEND_UNSIGNED > 0)
2220     x = gen_rtx_ZERO_EXTEND (mode, x);
2221   else if (!POINTERS_EXTEND_UNSIGNED)
2222     x = gen_rtx_SIGN_EXTEND (mode, x);
2223   else
2224     gcc_unreachable ();
2225 #endif /* POINTERS_EXTEND_UNSIGNED */
2226
2227   return x;
2228 }
2229
2230 /* Return an RTX equivalent to the value of the tree expression
2231    EXP.  */
2232
2233 static rtx
2234 expand_debug_expr (tree exp)
2235 {
2236   rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
2237   enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2238   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
2239   addr_space_t as;
2240
2241   switch (TREE_CODE_CLASS (TREE_CODE (exp)))
2242     {
2243     case tcc_expression:
2244       switch (TREE_CODE (exp))
2245         {
2246         case COND_EXPR:
2247         case DOT_PROD_EXPR:
2248         case WIDEN_MULT_PLUS_EXPR:
2249         case WIDEN_MULT_MINUS_EXPR:
2250           goto ternary;
2251
2252         case TRUTH_ANDIF_EXPR:
2253         case TRUTH_ORIF_EXPR:
2254         case TRUTH_AND_EXPR:
2255         case TRUTH_OR_EXPR:
2256         case TRUTH_XOR_EXPR:
2257           goto binary;
2258
2259         case TRUTH_NOT_EXPR:
2260           goto unary;
2261
2262         default:
2263           break;
2264         }
2265       break;
2266
2267     ternary:
2268       op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
2269       if (!op2)
2270         return NULL_RTX;
2271       /* Fall through.  */
2272
2273     binary:
2274     case tcc_binary:
2275     case tcc_comparison:
2276       op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
2277       if (!op1)
2278         return NULL_RTX;
2279       /* Fall through.  */
2280
2281     unary:
2282     case tcc_unary:
2283       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2284       if (!op0)
2285         return NULL_RTX;
2286       break;
2287
2288     case tcc_type:
2289     case tcc_statement:
2290       gcc_unreachable ();
2291
2292     case tcc_constant:
2293     case tcc_exceptional:
2294     case tcc_declaration:
2295     case tcc_reference:
2296     case tcc_vl_exp:
2297       break;
2298     }
2299
2300   switch (TREE_CODE (exp))
2301     {
2302     case STRING_CST:
2303       if (!lookup_constant_def (exp))
2304         {
2305           if (strlen (TREE_STRING_POINTER (exp)) + 1
2306               != (size_t) TREE_STRING_LENGTH (exp))
2307             return NULL_RTX;
2308           op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
2309           op0 = gen_rtx_MEM (BLKmode, op0);
2310           set_mem_attributes (op0, exp, 0);
2311           return op0;
2312         }
2313       /* Fall through...  */
2314
2315     case INTEGER_CST:
2316     case REAL_CST:
2317     case FIXED_CST:
2318       op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
2319       return op0;
2320
2321     case COMPLEX_CST:
2322       gcc_assert (COMPLEX_MODE_P (mode));
2323       op0 = expand_debug_expr (TREE_REALPART (exp));
2324       op1 = expand_debug_expr (TREE_IMAGPART (exp));
2325       return gen_rtx_CONCAT (mode, op0, op1);
2326
2327     case DEBUG_EXPR_DECL:
2328       op0 = DECL_RTL_IF_SET (exp);
2329
2330       if (op0)
2331         return op0;
2332
2333       op0 = gen_rtx_DEBUG_EXPR (mode);
2334       DEBUG_EXPR_TREE_DECL (op0) = exp;
2335       SET_DECL_RTL (exp, op0);
2336
2337       return op0;
2338
2339     case VAR_DECL:
2340     case PARM_DECL:
2341     case FUNCTION_DECL:
2342     case LABEL_DECL:
2343     case CONST_DECL:
2344     case RESULT_DECL:
2345       op0 = DECL_RTL_IF_SET (exp);
2346
2347       /* This decl was probably optimized away.  */
2348       if (!op0)
2349         {
2350           if (TREE_CODE (exp) != VAR_DECL
2351               || DECL_EXTERNAL (exp)
2352               || !TREE_STATIC (exp)
2353               || !DECL_NAME (exp)
2354               || DECL_HARD_REGISTER (exp)
2355               || mode == VOIDmode)
2356             return NULL;
2357
2358           op0 = make_decl_rtl_for_debug (exp);
2359           if (!MEM_P (op0)
2360               || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
2361               || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
2362             return NULL;
2363         }
2364       else
2365         op0 = copy_rtx (op0);
2366
2367       if (GET_MODE (op0) == BLKmode
2368           /* If op0 is not BLKmode, but BLKmode is, adjust_mode
2369              below would ICE.  While it is likely a FE bug,
2370              try to be robust here.  See PR43166.  */
2371           || mode == BLKmode
2372           || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
2373         {
2374           gcc_assert (MEM_P (op0));
2375           op0 = adjust_address_nv (op0, mode, 0);
2376           return op0;
2377         }
2378
2379       /* Fall through.  */
2380
2381     adjust_mode:
2382     case PAREN_EXPR:
2383     case NOP_EXPR:
2384     case CONVERT_EXPR:
2385       {
2386         enum machine_mode inner_mode = GET_MODE (op0);
2387
2388         if (mode == inner_mode)
2389           return op0;
2390
2391         if (inner_mode == VOIDmode)
2392           {
2393             if (TREE_CODE (exp) == SSA_NAME)
2394               inner_mode = TYPE_MODE (TREE_TYPE (exp));
2395             else
2396               inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2397             if (mode == inner_mode)
2398               return op0;
2399           }
2400
2401         if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
2402           {
2403             if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
2404               op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
2405             else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
2406               op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
2407             else
2408               op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
2409           }
2410         else if (FLOAT_MODE_P (mode))
2411           {
2412             gcc_assert (TREE_CODE (exp) != SSA_NAME);
2413             if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
2414               op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
2415             else
2416               op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
2417           }
2418         else if (FLOAT_MODE_P (inner_mode))
2419           {
2420             if (unsignedp)
2421               op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
2422             else
2423               op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
2424           }
2425         else if (CONSTANT_P (op0)
2426                  || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
2427           op0 = simplify_gen_subreg (mode, op0, inner_mode,
2428                                      subreg_lowpart_offset (mode,
2429                                                             inner_mode));
2430         else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
2431                  ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
2432                  : unsignedp)
2433           op0 = gen_rtx_ZERO_EXTEND (mode, op0);
2434         else
2435           op0 = gen_rtx_SIGN_EXTEND (mode, op0);
2436
2437         return op0;
2438       }
2439
2440     case MEM_REF:
2441       /* ??? FIXME.  */
2442       if (!integer_zerop (TREE_OPERAND (exp, 1)))
2443         return NULL;
2444       /* Fallthru.  */
2445     case INDIRECT_REF:
2446     case MISALIGNED_INDIRECT_REF:
2447       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2448       if (!op0)
2449         return NULL;
2450
2451       if (POINTER_TYPE_P (TREE_TYPE (exp)))
2452         as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2453       else
2454         as = ADDR_SPACE_GENERIC;
2455
2456       op0 = gen_rtx_MEM (mode, op0);
2457
2458       set_mem_attributes (op0, exp, 0);
2459       set_mem_addr_space (op0, as);
2460
2461       return op0;
2462
2463     case TARGET_MEM_REF:
2464       if (TMR_SYMBOL (exp) && !DECL_RTL_SET_P (TMR_SYMBOL (exp)))
2465         return NULL;
2466
2467       op0 = expand_debug_expr
2468             (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
2469       if (!op0)
2470         return NULL;
2471
2472       as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
2473
2474       op0 = gen_rtx_MEM (mode, op0);
2475
2476       set_mem_attributes (op0, exp, 0);
2477       set_mem_addr_space (op0, as);
2478
2479       return op0;
2480
2481     case ARRAY_REF:
2482     case ARRAY_RANGE_REF:
2483     case COMPONENT_REF:
2484     case BIT_FIELD_REF:
2485     case REALPART_EXPR:
2486     case IMAGPART_EXPR:
2487     case VIEW_CONVERT_EXPR:
2488       {
2489         enum machine_mode mode1;
2490         HOST_WIDE_INT bitsize, bitpos;
2491         tree offset;
2492         int volatilep = 0;
2493         tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2494                                         &mode1, &unsignedp, &volatilep, false);
2495         rtx orig_op0;
2496
2497         if (bitsize == 0)
2498           return NULL;
2499
2500         orig_op0 = op0 = expand_debug_expr (tem);
2501
2502         if (!op0)
2503           return NULL;
2504
2505         if (offset)
2506           {
2507             enum machine_mode addrmode, offmode;
2508
2509             if (!MEM_P (op0))
2510               return NULL;
2511
2512             op0 = XEXP (op0, 0);
2513             addrmode = GET_MODE (op0);
2514             if (addrmode == VOIDmode)
2515               addrmode = Pmode;
2516
2517             op1 = expand_debug_expr (offset);
2518             if (!op1)
2519               return NULL;
2520
2521             offmode = GET_MODE (op1);
2522             if (offmode == VOIDmode)
2523               offmode = TYPE_MODE (TREE_TYPE (offset));
2524
2525             if (addrmode != offmode)
2526               op1 = simplify_gen_subreg (addrmode, op1, offmode,
2527                                          subreg_lowpart_offset (addrmode,
2528                                                                 offmode));
2529
2530             /* Don't use offset_address here, we don't need a
2531                recognizable address, and we don't want to generate
2532                code.  */
2533             op0 = gen_rtx_MEM (mode, gen_rtx_PLUS (addrmode, op0, op1));
2534           }
2535
2536         if (MEM_P (op0))
2537           {
2538             if (mode1 == VOIDmode)
2539               /* Bitfield.  */
2540               mode1 = smallest_mode_for_size (bitsize, MODE_INT);
2541             if (bitpos >= BITS_PER_UNIT)
2542               {
2543                 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
2544                 bitpos %= BITS_PER_UNIT;
2545               }
2546             else if (bitpos < 0)
2547               {
2548                 HOST_WIDE_INT units
2549                   = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
2550                 op0 = adjust_address_nv (op0, mode1, units);
2551                 bitpos += units * BITS_PER_UNIT;
2552               }
2553             else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
2554               op0 = adjust_address_nv (op0, mode, 0);
2555             else if (GET_MODE (op0) != mode1)
2556               op0 = adjust_address_nv (op0, mode1, 0);
2557             else
2558               op0 = copy_rtx (op0);
2559             if (op0 == orig_op0)
2560               op0 = shallow_copy_rtx (op0);
2561             set_mem_attributes (op0, exp, 0);
2562           }
2563
2564         if (bitpos == 0 && mode == GET_MODE (op0))
2565           return op0;
2566
2567         if (bitpos < 0)
2568           return NULL;
2569
2570         if (GET_MODE (op0) == BLKmode)
2571           return NULL;
2572
2573         if ((bitpos % BITS_PER_UNIT) == 0
2574             && bitsize == GET_MODE_BITSIZE (mode1))
2575           {
2576             enum machine_mode opmode = GET_MODE (op0);
2577
2578             if (opmode == VOIDmode)
2579               opmode = mode1;
2580
2581             /* This condition may hold if we're expanding the address
2582                right past the end of an array that turned out not to
2583                be addressable (i.e., the address was only computed in
2584                debug stmts).  The gen_subreg below would rightfully
2585                crash, and the address doesn't really exist, so just
2586                drop it.  */
2587             if (bitpos >= GET_MODE_BITSIZE (opmode))
2588               return NULL;
2589
2590             if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
2591               return simplify_gen_subreg (mode, op0, opmode,
2592                                           bitpos / BITS_PER_UNIT);
2593           }
2594
2595         return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
2596                                      && TYPE_UNSIGNED (TREE_TYPE (exp))
2597                                      ? SIGN_EXTRACT
2598                                      : ZERO_EXTRACT, mode,
2599                                      GET_MODE (op0) != VOIDmode
2600                                      ? GET_MODE (op0) : mode1,
2601                                      op0, GEN_INT (bitsize), GEN_INT (bitpos));
2602       }
2603
2604     case ABS_EXPR:
2605       return gen_rtx_ABS (mode, op0);
2606
2607     case NEGATE_EXPR:
2608       return gen_rtx_NEG (mode, op0);
2609
2610     case BIT_NOT_EXPR:
2611       return gen_rtx_NOT (mode, op0);
2612
2613     case FLOAT_EXPR:
2614       if (unsignedp)
2615         return gen_rtx_UNSIGNED_FLOAT (mode, op0);
2616       else
2617         return gen_rtx_FLOAT (mode, op0);
2618
2619     case FIX_TRUNC_EXPR:
2620       if (unsignedp)
2621         return gen_rtx_UNSIGNED_FIX (mode, op0);
2622       else
2623         return gen_rtx_FIX (mode, op0);
2624
2625     case POINTER_PLUS_EXPR:
2626       /* For the rare target where pointers are not the same size as
2627          size_t, we need to check for mis-matched modes and correct
2628          the addend.  */
2629       if (op0 && op1
2630           && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
2631           && GET_MODE (op0) != GET_MODE (op1))
2632         {
2633           if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1)))
2634             op1 = gen_rtx_TRUNCATE (GET_MODE (op0), op1);
2635           else
2636             /* We always sign-extend, regardless of the signedness of
2637                the operand, because the operand is always unsigned
2638                here even if the original C expression is signed.  */
2639             op1 = gen_rtx_SIGN_EXTEND (GET_MODE (op0), op1);
2640         }
2641       /* Fall through.  */
2642     case PLUS_EXPR:
2643       return gen_rtx_PLUS (mode, op0, op1);
2644
2645     case MINUS_EXPR:
2646       return gen_rtx_MINUS (mode, op0, op1);
2647
2648     case MULT_EXPR:
2649       return gen_rtx_MULT (mode, op0, op1);
2650
2651     case RDIV_EXPR:
2652     case TRUNC_DIV_EXPR:
2653     case EXACT_DIV_EXPR:
2654       if (unsignedp)
2655         return gen_rtx_UDIV (mode, op0, op1);
2656       else
2657         return gen_rtx_DIV (mode, op0, op1);
2658
2659     case TRUNC_MOD_EXPR:
2660       if (unsignedp)
2661         return gen_rtx_UMOD (mode, op0, op1);
2662       else
2663         return gen_rtx_MOD (mode, op0, op1);
2664
2665     case FLOOR_DIV_EXPR:
2666       if (unsignedp)
2667         return gen_rtx_UDIV (mode, op0, op1);
2668       else
2669         {
2670           rtx div = gen_rtx_DIV (mode, op0, op1);
2671           rtx mod = gen_rtx_MOD (mode, op0, op1);
2672           rtx adj = floor_sdiv_adjust (mode, mod, op1);
2673           return gen_rtx_PLUS (mode, div, adj);
2674         }
2675
2676     case FLOOR_MOD_EXPR:
2677       if (unsignedp)
2678         return gen_rtx_UMOD (mode, op0, op1);
2679       else
2680         {
2681           rtx mod = gen_rtx_MOD (mode, op0, op1);
2682           rtx adj = floor_sdiv_adjust (mode, mod, op1);
2683           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2684           return gen_rtx_PLUS (mode, mod, adj);
2685         }
2686
2687     case CEIL_DIV_EXPR:
2688       if (unsignedp)
2689         {
2690           rtx div = gen_rtx_UDIV (mode, op0, op1);
2691           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2692           rtx adj = ceil_udiv_adjust (mode, mod, op1);
2693           return gen_rtx_PLUS (mode, div, adj);
2694         }
2695       else
2696         {
2697           rtx div = gen_rtx_DIV (mode, op0, op1);
2698           rtx mod = gen_rtx_MOD (mode, op0, op1);
2699           rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2700           return gen_rtx_PLUS (mode, div, adj);
2701         }
2702
2703     case CEIL_MOD_EXPR:
2704       if (unsignedp)
2705         {
2706           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2707           rtx adj = ceil_udiv_adjust (mode, mod, op1);
2708           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2709           return gen_rtx_PLUS (mode, mod, adj);
2710         }
2711       else
2712         {
2713           rtx mod = gen_rtx_MOD (mode, op0, op1);
2714           rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2715           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2716           return gen_rtx_PLUS (mode, mod, adj);
2717         }
2718
2719     case ROUND_DIV_EXPR:
2720       if (unsignedp)
2721         {
2722           rtx div = gen_rtx_UDIV (mode, op0, op1);
2723           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2724           rtx adj = round_udiv_adjust (mode, mod, op1);
2725           return gen_rtx_PLUS (mode, div, adj);
2726         }
2727       else
2728         {
2729           rtx div = gen_rtx_DIV (mode, op0, op1);
2730           rtx mod = gen_rtx_MOD (mode, op0, op1);
2731           rtx adj = round_sdiv_adjust (mode, mod, op1);
2732           return gen_rtx_PLUS (mode, div, adj);
2733         }
2734
2735     case ROUND_MOD_EXPR:
2736       if (unsignedp)
2737         {
2738           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2739           rtx adj = round_udiv_adjust (mode, mod, op1);
2740           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2741           return gen_rtx_PLUS (mode, mod, adj);
2742         }
2743       else
2744         {
2745           rtx mod = gen_rtx_MOD (mode, op0, op1);
2746           rtx adj = round_sdiv_adjust (mode, mod, op1);
2747           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2748           return gen_rtx_PLUS (mode, mod, adj);
2749         }
2750
2751     case LSHIFT_EXPR:
2752       return gen_rtx_ASHIFT (mode, op0, op1);
2753
2754     case RSHIFT_EXPR:
2755       if (unsignedp)
2756         return gen_rtx_LSHIFTRT (mode, op0, op1);
2757       else
2758         return gen_rtx_ASHIFTRT (mode, op0, op1);
2759
2760     case LROTATE_EXPR:
2761       return gen_rtx_ROTATE (mode, op0, op1);
2762
2763     case RROTATE_EXPR:
2764       return gen_rtx_ROTATERT (mode, op0, op1);
2765
2766     case MIN_EXPR:
2767       if (unsignedp)
2768         return gen_rtx_UMIN (mode, op0, op1);
2769       else
2770         return gen_rtx_SMIN (mode, op0, op1);
2771
2772     case MAX_EXPR:
2773       if (unsignedp)
2774         return gen_rtx_UMAX (mode, op0, op1);
2775       else
2776         return gen_rtx_SMAX (mode, op0, op1);
2777
2778     case BIT_AND_EXPR:
2779     case TRUTH_AND_EXPR:
2780       return gen_rtx_AND (mode, op0, op1);
2781
2782     case BIT_IOR_EXPR:
2783     case TRUTH_OR_EXPR:
2784       return gen_rtx_IOR (mode, op0, op1);
2785
2786     case BIT_XOR_EXPR:
2787     case TRUTH_XOR_EXPR:
2788       return gen_rtx_XOR (mode, op0, op1);
2789
2790     case TRUTH_ANDIF_EXPR:
2791       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
2792
2793     case TRUTH_ORIF_EXPR:
2794       return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
2795
2796     case TRUTH_NOT_EXPR:
2797       return gen_rtx_EQ (mode, op0, const0_rtx);
2798
2799     case LT_EXPR:
2800       if (unsignedp)
2801         return gen_rtx_LTU (mode, op0, op1);
2802       else
2803         return gen_rtx_LT (mode, op0, op1);
2804
2805     case LE_EXPR:
2806       if (unsignedp)
2807         return gen_rtx_LEU (mode, op0, op1);
2808       else
2809         return gen_rtx_LE (mode, op0, op1);
2810
2811     case GT_EXPR:
2812       if (unsignedp)
2813         return gen_rtx_GTU (mode, op0, op1);
2814       else
2815         return gen_rtx_GT (mode, op0, op1);
2816
2817     case GE_EXPR:
2818       if (unsignedp)
2819         return gen_rtx_GEU (mode, op0, op1);
2820       else
2821         return gen_rtx_GE (mode, op0, op1);
2822
2823     case EQ_EXPR:
2824       return gen_rtx_EQ (mode, op0, op1);
2825
2826     case NE_EXPR:
2827       return gen_rtx_NE (mode, op0, op1);
2828
2829     case UNORDERED_EXPR:
2830       return gen_rtx_UNORDERED (mode, op0, op1);
2831
2832     case ORDERED_EXPR:
2833       return gen_rtx_ORDERED (mode, op0, op1);
2834
2835     case UNLT_EXPR:
2836       return gen_rtx_UNLT (mode, op0, op1);
2837
2838     case UNLE_EXPR:
2839       return gen_rtx_UNLE (mode, op0, op1);
2840
2841     case UNGT_EXPR:
2842       return gen_rtx_UNGT (mode, op0, op1);
2843
2844     case UNGE_EXPR:
2845       return gen_rtx_UNGE (mode, op0, op1);
2846
2847     case UNEQ_EXPR:
2848       return gen_rtx_UNEQ (mode, op0, op1);
2849
2850     case LTGT_EXPR:
2851       return gen_rtx_LTGT (mode, op0, op1);
2852
2853     case COND_EXPR:
2854       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
2855
2856     case COMPLEX_EXPR:
2857       gcc_assert (COMPLEX_MODE_P (mode));
2858       if (GET_MODE (op0) == VOIDmode)
2859         op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
2860       if (GET_MODE (op1) == VOIDmode)
2861         op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
2862       return gen_rtx_CONCAT (mode, op0, op1);
2863
2864     case CONJ_EXPR:
2865       if (GET_CODE (op0) == CONCAT)
2866         return gen_rtx_CONCAT (mode, XEXP (op0, 0),
2867                                gen_rtx_NEG (GET_MODE_INNER (mode),
2868                                             XEXP (op0, 1)));
2869       else
2870         {
2871           enum machine_mode imode = GET_MODE_INNER (mode);
2872           rtx re, im;
2873
2874           if (MEM_P (op0))
2875             {
2876               re = adjust_address_nv (op0, imode, 0);
2877               im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
2878             }
2879           else
2880             {
2881               enum machine_mode ifmode = int_mode_for_mode (mode);
2882               enum machine_mode ihmode = int_mode_for_mode (imode);
2883               rtx halfsize;
2884               if (ifmode == BLKmode || ihmode == BLKmode)
2885                 return NULL;
2886               halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
2887               re = op0;
2888               if (mode != ifmode)
2889                 re = gen_rtx_SUBREG (ifmode, re, 0);
2890               re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
2891               if (imode != ihmode)
2892                 re = gen_rtx_SUBREG (imode, re, 0);
2893               im = copy_rtx (op0);
2894               if (mode != ifmode)
2895                 im = gen_rtx_SUBREG (ifmode, im, 0);
2896               im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
2897               if (imode != ihmode)
2898                 im = gen_rtx_SUBREG (imode, im, 0);
2899             }
2900           im = gen_rtx_NEG (imode, im);
2901           return gen_rtx_CONCAT (mode, re, im);
2902         }
2903
2904     case ADDR_EXPR:
2905       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2906       if (!op0 || !MEM_P (op0))
2907         return NULL;
2908
2909       op0 = convert_debug_memory_address (mode, XEXP (op0, 0));
2910
2911       return op0;
2912
2913     case VECTOR_CST:
2914       exp = build_constructor_from_list (TREE_TYPE (exp),
2915                                          TREE_VECTOR_CST_ELTS (exp));
2916       /* Fall through.  */
2917
2918     case CONSTRUCTOR:
2919       if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
2920         {
2921           unsigned i;
2922           tree val;
2923
2924           op0 = gen_rtx_CONCATN
2925             (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
2926
2927           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
2928             {
2929               op1 = expand_debug_expr (val);
2930               if (!op1)
2931                 return NULL;
2932               XVECEXP (op0, 0, i) = op1;
2933             }
2934
2935           if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
2936             {
2937               op1 = expand_debug_expr
2938                 (fold_convert (TREE_TYPE (TREE_TYPE (exp)), integer_zero_node));
2939
2940               if (!op1)
2941                 return NULL;
2942
2943               for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
2944                 XVECEXP (op0, 0, i) = op1;
2945             }
2946
2947           return op0;
2948         }
2949       else
2950         goto flag_unsupported;
2951
2952     case CALL_EXPR:
2953       /* ??? Maybe handle some builtins?  */
2954       return NULL;
2955
2956     case SSA_NAME:
2957       {
2958         gimple g = get_gimple_for_ssa_name (exp);
2959         if (g)
2960           {
2961             op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
2962             if (!op0)
2963               return NULL;
2964           }
2965         else
2966           {
2967             int part = var_to_partition (SA.map, exp);
2968
2969             if (part == NO_PARTITION)
2970               return NULL;
2971
2972             gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
2973
2974             op0 = SA.partition_to_pseudo[part];
2975           }
2976         goto adjust_mode;
2977       }
2978
2979     case ERROR_MARK:
2980       return NULL;
2981
2982     /* Vector stuff.  For most of the codes we don't have rtl codes.  */
2983     case REALIGN_LOAD_EXPR:
2984     case REDUC_MAX_EXPR:
2985     case REDUC_MIN_EXPR:
2986     case REDUC_PLUS_EXPR:
2987     case VEC_COND_EXPR:
2988     case VEC_EXTRACT_EVEN_EXPR:
2989     case VEC_EXTRACT_ODD_EXPR:
2990     case VEC_INTERLEAVE_HIGH_EXPR:
2991     case VEC_INTERLEAVE_LOW_EXPR:
2992     case VEC_LSHIFT_EXPR:
2993     case VEC_PACK_FIX_TRUNC_EXPR:
2994     case VEC_PACK_SAT_EXPR:
2995     case VEC_PACK_TRUNC_EXPR:
2996     case VEC_RSHIFT_EXPR:
2997     case VEC_UNPACK_FLOAT_HI_EXPR:
2998     case VEC_UNPACK_FLOAT_LO_EXPR:
2999     case VEC_UNPACK_HI_EXPR:
3000     case VEC_UNPACK_LO_EXPR:
3001     case VEC_WIDEN_MULT_HI_EXPR:
3002     case VEC_WIDEN_MULT_LO_EXPR:
3003       return NULL;
3004
3005    /* Misc codes.  */
3006     case ADDR_SPACE_CONVERT_EXPR:
3007     case FIXED_CONVERT_EXPR:
3008     case OBJ_TYPE_REF:
3009     case WITH_SIZE_EXPR:
3010       return NULL;
3011
3012     case DOT_PROD_EXPR:
3013       if (SCALAR_INT_MODE_P (GET_MODE (op0))
3014           && SCALAR_INT_MODE_P (mode))
3015         {
3016           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3017             op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3018           else
3019             op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3020           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3021             op1 = gen_rtx_ZERO_EXTEND (mode, op1);
3022           else
3023             op1 = gen_rtx_SIGN_EXTEND (mode, op1);
3024           op0 = gen_rtx_MULT (mode, op0, op1);
3025           return gen_rtx_PLUS (mode, op0, op2);
3026         }
3027       return NULL;
3028
3029     case WIDEN_MULT_EXPR:
3030     case WIDEN_MULT_PLUS_EXPR:
3031     case WIDEN_MULT_MINUS_EXPR:
3032       if (SCALAR_INT_MODE_P (GET_MODE (op0))
3033           && SCALAR_INT_MODE_P (mode))
3034         {
3035           enum machine_mode inner_mode = GET_MODE (op0);
3036           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3037             op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3038           else
3039             op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3040           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3041             op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
3042           else
3043             op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
3044           op0 = gen_rtx_MULT (mode, op0, op1);
3045           if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
3046             return op0;
3047           else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
3048             return gen_rtx_PLUS (mode, op0, op2);
3049           else
3050             return gen_rtx_MINUS (mode, op2, op0);
3051         }
3052       return NULL;
3053
3054     case WIDEN_SUM_EXPR:
3055       if (SCALAR_INT_MODE_P (GET_MODE (op0))
3056           && SCALAR_INT_MODE_P (mode))
3057         {
3058           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3059             op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3060           else
3061             op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3062           return gen_rtx_PLUS (mode, op0, op1);
3063         }
3064       return NULL;
3065
3066     default:
3067     flag_unsupported:
3068 #ifdef ENABLE_CHECKING
3069       debug_tree (exp);
3070       gcc_unreachable ();
3071 #else
3072       return NULL;
3073 #endif
3074     }
3075 }
3076
3077 /* Expand the _LOCs in debug insns.  We run this after expanding all
3078    regular insns, so that any variables referenced in the function
3079    will have their DECL_RTLs set.  */
3080
3081 static void
3082 expand_debug_locations (void)
3083 {
3084   rtx insn;
3085   rtx last = get_last_insn ();
3086   int save_strict_alias = flag_strict_aliasing;
3087
3088   /* New alias sets while setting up memory attributes cause
3089      -fcompare-debug failures, even though it doesn't bring about any
3090      codegen changes.  */
3091   flag_strict_aliasing = 0;
3092
3093   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3094     if (DEBUG_INSN_P (insn))
3095       {
3096         tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
3097         rtx val;
3098         enum machine_mode mode;
3099
3100         if (value == NULL_TREE)
3101           val = NULL_RTX;
3102         else
3103           {
3104             val = expand_debug_expr (value);
3105             gcc_assert (last == get_last_insn ());
3106           }
3107
3108         if (!val)
3109           val = gen_rtx_UNKNOWN_VAR_LOC ();
3110         else
3111           {
3112             mode = GET_MODE (INSN_VAR_LOCATION (insn));
3113
3114             gcc_assert (mode == GET_MODE (val)
3115                         || (GET_MODE (val) == VOIDmode
3116                             && (CONST_INT_P (val)
3117                                 || GET_CODE (val) == CONST_FIXED
3118                                 || GET_CODE (val) == CONST_DOUBLE
3119                                 || GET_CODE (val) == LABEL_REF)));
3120           }
3121
3122         INSN_VAR_LOCATION_LOC (insn) = val;
3123       }
3124
3125   flag_strict_aliasing = save_strict_alias;
3126 }
3127
3128 /* Expand basic block BB from GIMPLE trees to RTL.  */
3129
3130 static basic_block
3131 expand_gimple_basic_block (basic_block bb)
3132 {
3133   gimple_stmt_iterator gsi;
3134   gimple_seq stmts;
3135   gimple stmt = NULL;
3136   rtx note, last;
3137   edge e;
3138   edge_iterator ei;
3139   void **elt;
3140
3141   if (dump_file)
3142     fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
3143              bb->index);
3144
3145   /* Note that since we are now transitioning from GIMPLE to RTL, we
3146      cannot use the gsi_*_bb() routines because they expect the basic
3147      block to be in GIMPLE, instead of RTL.  Therefore, we need to
3148      access the BB sequence directly.  */
3149   stmts = bb_seq (bb);
3150   bb->il.gimple = NULL;
3151   rtl_profile_for_bb (bb);
3152   init_rtl_bb_info (bb);
3153   bb->flags |= BB_RTL;
3154
3155   /* Remove the RETURN_EXPR if we may fall though to the exit
3156      instead.  */
3157   gsi = gsi_last (stmts);
3158   if (!gsi_end_p (gsi)
3159       && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
3160     {
3161       gimple ret_stmt = gsi_stmt (gsi);
3162
3163       gcc_assert (single_succ_p (bb));
3164       gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
3165
3166       if (bb->next_bb == EXIT_BLOCK_PTR
3167           && !gimple_return_retval (ret_stmt))
3168         {
3169           gsi_remove (&gsi, false);
3170           single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3171         }
3172     }
3173
3174   gsi = gsi_start (stmts);
3175   if (!gsi_end_p (gsi))
3176     {
3177       stmt = gsi_stmt (gsi);
3178       if (gimple_code (stmt) != GIMPLE_LABEL)
3179         stmt = NULL;
3180     }
3181
3182   elt = pointer_map_contains (lab_rtx_for_bb, bb);
3183
3184   if (stmt || elt)
3185     {
3186       last = get_last_insn ();
3187
3188       if (stmt)
3189         {
3190           expand_gimple_stmt (stmt);
3191           gsi_next (&gsi);
3192         }
3193
3194       if (elt)
3195         emit_label ((rtx) *elt);
3196
3197       /* Java emits line number notes in the top of labels.
3198          ??? Make this go away once line number notes are obsoleted.  */
3199       BB_HEAD (bb) = NEXT_INSN (last);
3200       if (NOTE_P (BB_HEAD (bb)))
3201         BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
3202       note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
3203
3204       maybe_dump_rtl_for_gimple_stmt (stmt, last);
3205     }
3206   else
3207     note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
3208
3209   NOTE_BASIC_BLOCK (note) = bb;
3210
3211   for (; !gsi_end_p (gsi); gsi_next (&gsi))
3212     {
3213       basic_block new_bb;
3214
3215       stmt = gsi_stmt (gsi);
3216
3217       /* If this statement is a non-debug one, and we generate debug
3218          insns, then this one might be the last real use of a TERed
3219          SSA_NAME, but where there are still some debug uses further
3220          down.  Expanding the current SSA name in such further debug
3221          uses by their RHS might lead to wrong debug info, as coalescing
3222          might make the operands of such RHS be placed into the same
3223          pseudo as something else.  Like so:
3224            a_1 = a_0 + 1;   // Assume a_1 is TERed and a_0 is dead
3225            use(a_1);
3226            a_2 = ...
3227            #DEBUG ... => a_1
3228          As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
3229          If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
3230          the write to a_2 would actually have clobbered the place which
3231          formerly held a_0.
3232
3233          So, instead of that, we recognize the situation, and generate
3234          debug temporaries at the last real use of TERed SSA names:
3235            a_1 = a_0 + 1;
3236            #DEBUG #D1 => a_1
3237            use(a_1);
3238            a_2 = ...
3239            #DEBUG ... => #D1
3240          */
3241       if (MAY_HAVE_DEBUG_INSNS
3242           && SA.values
3243           && !is_gimple_debug (stmt))
3244         {
3245           ssa_op_iter iter;
3246           tree op;
3247           gimple def;
3248
3249           location_t sloc = get_curr_insn_source_location ();
3250           tree sblock = get_curr_insn_block ();
3251
3252           /* Look for SSA names that have their last use here (TERed
3253              names always have only one real use).  */
3254           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3255             if ((def = get_gimple_for_ssa_name (op)))
3256               {
3257                 imm_use_iterator imm_iter;
3258                 use_operand_p use_p;
3259                 bool have_debug_uses = false;
3260
3261                 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
3262                   {
3263                     if (gimple_debug_bind_p (USE_STMT (use_p)))
3264                       {
3265                         have_debug_uses = true;
3266                         break;
3267                       }
3268                   }
3269
3270                 if (have_debug_uses)
3271                   {
3272                     /* OP is a TERed SSA name, with DEF it's defining
3273                        statement, and where OP is used in further debug
3274                        instructions.  Generate a debug temporary, and
3275                        replace all uses of OP in debug insns with that
3276                        temporary.  */
3277                     gimple debugstmt;
3278                     tree value = gimple_assign_rhs_to_tree (def);
3279                     tree vexpr = make_node (DEBUG_EXPR_DECL);
3280                     rtx val;
3281                     enum machine_mode mode;
3282
3283                     set_curr_insn_source_location (gimple_location (def));
3284                     set_curr_insn_block (gimple_block (def));
3285
3286                     DECL_ARTIFICIAL (vexpr) = 1;
3287                     TREE_TYPE (vexpr) = TREE_TYPE (value);
3288                     if (DECL_P (value))
3289                       mode = DECL_MODE (value);
3290                     else
3291                       mode = TYPE_MODE (TREE_TYPE (value));
3292                     DECL_MODE (vexpr) = mode;
3293
3294                     val = gen_rtx_VAR_LOCATION
3295                         (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3296
3297                     val = emit_debug_insn (val);
3298
3299                     FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
3300                       {
3301                         if (!gimple_debug_bind_p (debugstmt))
3302                           continue;
3303
3304                         FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3305                           SET_USE (use_p, vexpr);
3306
3307                         update_stmt (debugstmt);
3308                       }
3309                   }
3310               }
3311           set_curr_insn_source_location (sloc);
3312           set_curr_insn_block (sblock);
3313         }
3314
3315       currently_expanding_gimple_stmt = stmt;
3316
3317       /* Expand this statement, then evaluate the resulting RTL and
3318          fixup the CFG accordingly.  */
3319       if (gimple_code (stmt) == GIMPLE_COND)
3320         {
3321           new_bb = expand_gimple_cond (bb, stmt);
3322           if (new_bb)
3323             return new_bb;
3324         }
3325       else if (gimple_debug_bind_p (stmt))
3326         {
3327           location_t sloc = get_curr_insn_source_location ();
3328           tree sblock = get_curr_insn_block ();
3329           gimple_stmt_iterator nsi = gsi;
3330
3331           for (;;)
3332             {
3333               tree var = gimple_debug_bind_get_var (stmt);
3334               tree value;
3335               rtx val;
3336               enum machine_mode mode;
3337
3338               if (gimple_debug_bind_has_value_p (stmt))
3339                 value = gimple_debug_bind_get_value (stmt);
3340               else
3341                 value = NULL_TREE;
3342
3343               last = get_last_insn ();
3344
3345               set_curr_insn_source_location (gimple_location (stmt));
3346               set_curr_insn_block (gimple_block (stmt));
3347
3348               if (DECL_P (var))
3349                 mode = DECL_MODE (var);
3350               else
3351                 mode = TYPE_MODE (TREE_TYPE (var));
3352
3353               val = gen_rtx_VAR_LOCATION
3354                 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3355
3356               val = emit_debug_insn (val);
3357
3358               if (dump_file && (dump_flags & TDF_DETAILS))
3359                 {
3360                   /* We can't dump the insn with a TREE where an RTX
3361                      is expected.  */
3362                   INSN_VAR_LOCATION_LOC (val) = const0_rtx;
3363                   maybe_dump_rtl_for_gimple_stmt (stmt, last);
3364                   INSN_VAR_LOCATION_LOC (val) = (rtx)value;
3365                 }
3366
3367               /* In order not to generate too many debug temporaries,
3368                  we delink all uses of debug statements we already expanded.
3369                  Therefore debug statements between definition and real
3370                  use of TERed SSA names will continue to use the SSA name,
3371                  and not be replaced with debug temps.  */
3372               delink_stmt_imm_use (stmt);
3373
3374               gsi = nsi;
3375               gsi_next (&nsi);
3376               if (gsi_end_p (nsi))
3377                 break;
3378               stmt = gsi_stmt (nsi);
3379               if (!gimple_debug_bind_p (stmt))
3380                 break;
3381             }
3382
3383           set_curr_insn_source_location (sloc);
3384           set_curr_insn_block (sblock);
3385         }
3386       else
3387         {
3388           if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
3389             {
3390               bool can_fallthru;
3391               new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
3392               if (new_bb)
3393                 {
3394                   if (can_fallthru)
3395                     bb = new_bb;
3396                   else
3397                     return new_bb;
3398                 }
3399             }
3400           else
3401             {
3402               def_operand_p def_p;
3403               def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
3404
3405               if (def_p != NULL)
3406                 {
3407                   /* Ignore this stmt if it is in the list of
3408                      replaceable expressions.  */
3409                   if (SA.values
3410                       && bitmap_bit_p (SA.values,
3411                                        SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
3412                     continue;
3413                 }
3414               last = expand_gimple_stmt (stmt);
3415               maybe_dump_rtl_for_gimple_stmt (stmt, last);
3416             }
3417         }
3418     }
3419
3420   currently_expanding_gimple_stmt = NULL;
3421
3422   /* Expand implicit goto and convert goto_locus.  */
3423   FOR_EACH_EDGE (e, ei, bb->succs)
3424     {
3425       if (e->goto_locus && e->goto_block)
3426         {
3427           set_curr_insn_source_location (e->goto_locus);
3428           set_curr_insn_block (e->goto_block);
3429           e->goto_locus = curr_insn_locator ();
3430         }
3431       e->goto_block = NULL;
3432       if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
3433         {
3434           emit_jump (label_rtx_for_bb (e->dest));
3435           e->flags &= ~EDGE_FALLTHRU;
3436         }
3437     }
3438
3439   /* Expanded RTL can create a jump in the last instruction of block.
3440      This later might be assumed to be a jump to successor and break edge insertion.
3441      We need to insert dummy move to prevent this. PR41440. */
3442   if (single_succ_p (bb)
3443       && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
3444       && (last = get_last_insn ())
3445       && JUMP_P (last))
3446     {
3447       rtx dummy = gen_reg_rtx (SImode);
3448       emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
3449     }
3450
3451   do_pending_stack_adjust ();
3452
3453   /* Find the block tail.  The last insn in the block is the insn
3454      before a barrier and/or table jump insn.  */
3455   last = get_last_insn ();
3456   if (BARRIER_P (last))
3457     last = PREV_INSN (last);
3458   if (JUMP_TABLE_DATA_P (last))
3459     last = PREV_INSN (PREV_INSN (last));
3460   BB_END (bb) = last;
3461
3462   update_bb_for_insn (bb);
3463
3464   return bb;
3465 }
3466
3467
3468 /* Create a basic block for initialization code.  */
3469
3470 static basic_block
3471 construct_init_block (void)
3472 {
3473   basic_block init_block, first_block;
3474   edge e = NULL;
3475   int flags;
3476
3477   /* Multiple entry points not supported yet.  */
3478   gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
3479   init_rtl_bb_info (ENTRY_BLOCK_PTR);
3480   init_rtl_bb_info (EXIT_BLOCK_PTR);
3481   ENTRY_BLOCK_PTR->flags |= BB_RTL;
3482   EXIT_BLOCK_PTR->flags |= BB_RTL;
3483
3484   e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
3485
3486   /* When entry edge points to first basic block, we don't need jump,
3487      otherwise we have to jump into proper target.  */
3488   if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
3489     {
3490       tree label = gimple_block_label (e->dest);
3491
3492       emit_jump (label_rtx (label));
3493       flags = 0;
3494     }
3495   else
3496     flags = EDGE_FALLTHRU;
3497
3498   init_block = create_basic_block (NEXT_INSN (get_insns ()),
3499                                    get_last_insn (),
3500                                    ENTRY_BLOCK_PTR);
3501   init_block->frequency = ENTRY_BLOCK_PTR->frequency;
3502   init_block->count = ENTRY_BLOCK_PTR->count;
3503   if (e)
3504     {
3505       first_block = e->dest;
3506       redirect_edge_succ (e, init_block);
3507       e = make_edge (init_block, first_block, flags);
3508     }
3509   else
3510     e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3511   e->probability = REG_BR_PROB_BASE;
3512   e->count = ENTRY_BLOCK_PTR->count;
3513
3514   update_bb_for_insn (init_block);
3515   return init_block;
3516 }
3517
3518 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
3519    found in the block tree.  */
3520
3521 static void
3522 set_block_levels (tree block, int level)
3523 {
3524   while (block)
3525     {
3526       BLOCK_NUMBER (block) = level;
3527       set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
3528       block = BLOCK_CHAIN (block);
3529     }
3530 }
3531
3532 /* Create a block containing landing pads and similar stuff.  */
3533
3534 static void
3535 construct_exit_block (void)
3536 {
3537   rtx head = get_last_insn ();
3538   rtx end;
3539   basic_block exit_block;
3540   edge e, e2;
3541   unsigned ix;
3542   edge_iterator ei;
3543   rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
3544
3545   rtl_profile_for_bb (EXIT_BLOCK_PTR);
3546
3547   /* Make sure the locus is set to the end of the function, so that
3548      epilogue line numbers and warnings are set properly.  */
3549   if (cfun->function_end_locus != UNKNOWN_LOCATION)
3550     input_location = cfun->function_end_locus;
3551
3552   /* The following insns belong to the top scope.  */
3553   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3554
3555   /* Generate rtl for function exit.  */
3556   expand_function_end ();
3557
3558   end = get_last_insn ();
3559   if (head == end)
3560     return;
3561   /* While emitting the function end we could move end of the last basic block.
3562    */
3563   BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
3564   while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
3565     head = NEXT_INSN (head);
3566   exit_block = create_basic_block (NEXT_INSN (head), end,
3567                                    EXIT_BLOCK_PTR->prev_bb);
3568   exit_block->frequency = EXIT_BLOCK_PTR->frequency;
3569   exit_block->count = EXIT_BLOCK_PTR->count;
3570
3571   ix = 0;
3572   while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
3573     {
3574       e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
3575       if (!(e->flags & EDGE_ABNORMAL))
3576         redirect_edge_succ (e, exit_block);
3577       else
3578         ix++;
3579     }
3580
3581   e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3582   e->probability = REG_BR_PROB_BASE;
3583   e->count = EXIT_BLOCK_PTR->count;
3584   FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
3585     if (e2 != e)
3586       {
3587         e->count -= e2->count;
3588         exit_block->count -= e2->count;
3589         exit_block->frequency -= EDGE_FREQUENCY (e2);
3590       }
3591   if (e->count < 0)
3592     e->count = 0;
3593   if (exit_block->count < 0)
3594     exit_block->count = 0;
3595   if (exit_block->frequency < 0)
3596     exit_block->frequency = 0;
3597   update_bb_for_insn (exit_block);
3598 }
3599
3600 /* Helper function for discover_nonconstant_array_refs.
3601    Look for ARRAY_REF nodes with non-constant indexes and mark them
3602    addressable.  */
3603
3604 static tree
3605 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
3606                                    void *data ATTRIBUTE_UNUSED)
3607 {
3608   tree t = *tp;
3609
3610   if (IS_TYPE_OR_DECL_P (t))
3611     *walk_subtrees = 0;
3612   else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3613     {
3614       while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3615               && is_gimple_min_invariant (TREE_OPERAND (t, 1))
3616               && (!TREE_OPERAND (t, 2)
3617                   || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3618              || (TREE_CODE (t) == COMPONENT_REF
3619                  && (!TREE_OPERAND (t,2)
3620                      || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3621              || TREE_CODE (t) == BIT_FIELD_REF
3622              || TREE_CODE (t) == REALPART_EXPR
3623              || TREE_CODE (t) == IMAGPART_EXPR
3624              || TREE_CODE (t) == VIEW_CONVERT_EXPR
3625              || CONVERT_EXPR_P (t))
3626         t = TREE_OPERAND (t, 0);
3627
3628       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3629         {
3630           t = get_base_address (t);
3631           if (t && DECL_P (t)
3632               && DECL_MODE (t) != BLKmode)
3633             TREE_ADDRESSABLE (t) = 1;
3634         }
3635
3636       *walk_subtrees = 0;
3637     }
3638
3639   return NULL_TREE;
3640 }
3641
3642 /* RTL expansion is not able to compile array references with variable
3643    offsets for arrays stored in single register.  Discover such
3644    expressions and mark variables as addressable to avoid this
3645    scenario.  */
3646
3647 static void
3648 discover_nonconstant_array_refs (void)
3649 {
3650   basic_block bb;
3651   gimple_stmt_iterator gsi;
3652
3653   FOR_EACH_BB (bb)
3654     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3655       {
3656         gimple stmt = gsi_stmt (gsi);
3657         if (!is_gimple_debug (stmt))
3658           walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
3659       }
3660 }
3661
3662 /* This function sets crtl->args.internal_arg_pointer to a virtual
3663    register if DRAP is needed.  Local register allocator will replace
3664    virtual_incoming_args_rtx with the virtual register.  */
3665
3666 static void
3667 expand_stack_alignment (void)
3668 {
3669   rtx drap_rtx;
3670   unsigned int preferred_stack_boundary;
3671
3672   if (! SUPPORTS_STACK_ALIGNMENT)
3673     return;
3674
3675   if (cfun->calls_alloca
3676       || cfun->has_nonlocal_label
3677       || crtl->has_nonlocal_goto)
3678     crtl->need_drap = true;
3679
3680   /* Call update_stack_boundary here again to update incoming stack
3681      boundary.  It may set incoming stack alignment to a different
3682      value after RTL expansion.  TARGET_FUNCTION_OK_FOR_SIBCALL may
3683      use the minimum incoming stack alignment to check if it is OK
3684      to perform sibcall optimization since sibcall optimization will
3685      only align the outgoing stack to incoming stack boundary.  */
3686   if (targetm.calls.update_stack_boundary)
3687     targetm.calls.update_stack_boundary ();
3688
3689   /* The incoming stack frame has to be aligned at least at
3690      parm_stack_boundary.  */
3691   gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
3692
3693   /* Update crtl->stack_alignment_estimated and use it later to align
3694      stack.  We check PREFERRED_STACK_BOUNDARY if there may be non-call
3695      exceptions since callgraph doesn't collect incoming stack alignment
3696      in this case.  */
3697   if (cfun->can_throw_non_call_exceptions
3698       && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
3699     preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3700   else
3701     preferred_stack_boundary = crtl->preferred_stack_boundary;
3702   if (preferred_stack_boundary > crtl->stack_alignment_estimated)
3703     crtl->stack_alignment_estimated = preferred_stack_boundary;
3704   if (preferred_stack_boundary > crtl->stack_alignment_needed)
3705     crtl->stack_alignment_needed = preferred_stack_boundary;
3706
3707   gcc_assert (crtl->stack_alignment_needed
3708               <= crtl->stack_alignment_estimated);
3709
3710   crtl->stack_realign_needed
3711     = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
3712   crtl->stack_realign_tried = crtl->stack_realign_needed;
3713
3714   crtl->stack_realign_processed = true;
3715
3716   /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
3717      alignment.  */
3718   gcc_assert (targetm.calls.get_drap_rtx != NULL);
3719   drap_rtx = targetm.calls.get_drap_rtx ();
3720
3721   /* stack_realign_drap and drap_rtx must match.  */
3722   gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
3723
3724   /* Do nothing if NULL is returned, which means DRAP is not needed.  */
3725   if (NULL != drap_rtx)
3726     {
3727       crtl->args.internal_arg_pointer = drap_rtx;
3728
3729       /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
3730          needed. */
3731       fixup_tail_calls ();
3732     }
3733 }
3734
3735 /* Translate the intermediate representation contained in the CFG
3736    from GIMPLE trees to RTL.
3737
3738    We do conversion per basic block and preserve/update the tree CFG.
3739    This implies we have to do some magic as the CFG can simultaneously
3740    consist of basic blocks containing RTL and GIMPLE trees.  This can
3741    confuse the CFG hooks, so be careful to not manipulate CFG during
3742    the expansion.  */
3743
3744 static unsigned int
3745 gimple_expand_cfg (void)
3746 {
3747   basic_block bb, init_block;
3748   sbitmap blocks;
3749   edge_iterator ei;
3750   edge e;
3751   unsigned i;
3752
3753   timevar_push (TV_OUT_OF_SSA);
3754   rewrite_out_of_ssa (&SA);
3755   timevar_pop (TV_OUT_OF_SSA);
3756   SA.partition_to_pseudo = (rtx *)xcalloc (SA.map->num_partitions,
3757                                            sizeof (rtx));
3758
3759   /* Some backends want to know that we are expanding to RTL.  */
3760   currently_expanding_to_rtl = 1;
3761
3762   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
3763
3764   insn_locators_alloc ();
3765   if (!DECL_IS_BUILTIN (current_function_decl))
3766     {
3767       /* Eventually, all FEs should explicitly set function_start_locus.  */
3768       if (cfun->function_start_locus == UNKNOWN_LOCATION)
3769        set_curr_insn_source_location
3770          (DECL_SOURCE_LOCATION (current_function_decl));
3771       else
3772        set_curr_insn_source_location (cfun->function_start_locus);
3773     }
3774   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3775   prologue_locator = curr_insn_locator ();
3776
3777 #ifdef INSN_SCHEDULING
3778   init_sched_attrs ();
3779 #endif
3780
3781   /* Make sure first insn is a note even if we don't want linenums.
3782      This makes sure the first insn will never be deleted.
3783      Also, final expects a note to appear there.  */
3784   emit_note (NOTE_INSN_DELETED);
3785
3786   /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE.  */
3787   discover_nonconstant_array_refs ();
3788
3789   targetm.expand_to_rtl_hook ();
3790   crtl->stack_alignment_needed = STACK_BOUNDARY;
3791   crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
3792   crtl->stack_alignment_estimated = 0;
3793   crtl->preferred_stack_boundary = STACK_BOUNDARY;
3794   cfun->cfg->max_jumptable_ents = 0;
3795
3796
3797   /* Expand the variables recorded during gimple lowering.  */
3798   timevar_push (TV_VAR_EXPAND);
3799   expand_used_vars ();
3800   timevar_pop (TV_VAR_EXPAND);
3801
3802   /* Honor stack protection warnings.  */
3803   if (warn_stack_protect)
3804     {
3805       if (cfun->calls_alloca)
3806         warning (OPT_Wstack_protector,
3807                  "stack protector not protecting local variables: "
3808                  "variable length buffer");
3809       if (has_short_buffer && !crtl->stack_protect_guard)
3810         warning (OPT_Wstack_protector,
3811                  "stack protector not protecting function: "
3812                  "all local arrays are less than %d bytes long",
3813                  (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
3814     }
3815
3816   /* Set up parameters and prepare for return, for the function.  */
3817   expand_function_start (current_function_decl);
3818
3819   /* Now that we also have the parameter RTXs, copy them over to our
3820      partitions.  */
3821   for (i = 0; i < SA.map->num_partitions; i++)
3822     {
3823       tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
3824
3825       if (TREE_CODE (var) != VAR_DECL
3826           && !SA.partition_to_pseudo[i])
3827         SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
3828       gcc_assert (SA.partition_to_pseudo[i]);
3829
3830       /* If this decl was marked as living in multiple places, reset
3831          this now to NULL.  */
3832       if (DECL_RTL_IF_SET (var) == pc_rtx)
3833         SET_DECL_RTL (var, NULL);
3834
3835       /* Some RTL parts really want to look at DECL_RTL(x) when x
3836          was a decl marked in REG_ATTR or MEM_ATTR.  We could use
3837          SET_DECL_RTL here making this available, but that would mean
3838          to select one of the potentially many RTLs for one DECL.  Instead
3839          of doing that we simply reset the MEM_EXPR of the RTL in question,
3840          then nobody can get at it and hence nobody can call DECL_RTL on it.  */
3841       if (!DECL_RTL_SET_P (var))
3842         {
3843           if (MEM_P (SA.partition_to_pseudo[i]))
3844             set_mem_expr (SA.partition_to_pseudo[i], NULL);
3845         }
3846     }
3847
3848   /* If this function is `main', emit a call to `__main'
3849      to run global initializers, etc.  */
3850   if (DECL_NAME (current_function_decl)
3851       && MAIN_NAME_P (DECL_NAME (current_function_decl))
3852       && DECL_FILE_SCOPE_P (current_function_decl))
3853     expand_main_function ();
3854
3855   /* Initialize the stack_protect_guard field.  This must happen after the
3856      call to __main (if any) so that the external decl is initialized.  */
3857   if (crtl->stack_protect_guard)
3858     stack_protect_prologue ();
3859
3860   expand_phi_nodes (&SA);
3861
3862   /* Register rtl specific functions for cfg.  */
3863   rtl_register_cfg_hooks ();
3864
3865   init_block = construct_init_block ();
3866
3867   /* Clear EDGE_EXECUTABLE on the entry edge(s).  It is cleaned from the
3868      remaining edges later.  */
3869   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3870     e->flags &= ~EDGE_EXECUTABLE;
3871
3872   lab_rtx_for_bb = pointer_map_create ();
3873   FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
3874     bb = expand_gimple_basic_block (bb);
3875
3876   if (MAY_HAVE_DEBUG_INSNS)
3877     expand_debug_locations ();
3878
3879   execute_free_datastructures ();
3880   timevar_push (TV_OUT_OF_SSA);
3881   finish_out_of_ssa (&SA);
3882   timevar_pop (TV_OUT_OF_SSA);
3883
3884   timevar_push (TV_POST_EXPAND);
3885   /* We are no longer in SSA form.  */
3886   cfun->gimple_df->in_ssa_p = false;
3887
3888   /* Expansion is used by optimization passes too, set maybe_hot_insn_p
3889      conservatively to true until they are all profile aware.  */
3890   pointer_map_destroy (lab_rtx_for_bb);
3891   free_histograms ();
3892
3893   construct_exit_block ();
3894   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3895   insn_locators_finalize ();
3896
3897   /* Zap the tree EH table.  */
3898   set_eh_throw_stmt_table (cfun, NULL);
3899
3900   rebuild_jump_labels (get_insns ());
3901
3902   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3903     {
3904       edge e;
3905       edge_iterator ei;
3906       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3907         {
3908           if (e->insns.r)
3909             commit_one_edge_insertion (e);
3910           else
3911             ei_next (&ei);
3912         }
3913     }
3914
3915   /* We're done expanding trees to RTL.  */
3916   currently_expanding_to_rtl = 0;
3917
3918   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
3919     {
3920       edge e;
3921       edge_iterator ei;
3922       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3923         {
3924           /* Clear EDGE_EXECUTABLE.  This flag is never used in the backend.  */
3925           e->flags &= ~EDGE_EXECUTABLE;
3926
3927           /* At the moment not all abnormal edges match the RTL
3928              representation.  It is safe to remove them here as
3929              find_many_sub_basic_blocks will rediscover them.
3930              In the future we should get this fixed properly.  */
3931           if ((e->flags & EDGE_ABNORMAL)
3932               && !(e->flags & EDGE_SIBCALL))
3933             remove_edge (e);
3934           else
3935             ei_next (&ei);
3936         }
3937     }
3938
3939   blocks = sbitmap_alloc (last_basic_block);
3940   sbitmap_ones (blocks);
3941   find_many_sub_basic_blocks (blocks);
3942   sbitmap_free (blocks);
3943   purge_all_dead_edges ();
3944
3945   compact_blocks ();
3946
3947   expand_stack_alignment ();
3948
3949 #ifdef ENABLE_CHECKING
3950   verify_flow_info ();
3951 #endif
3952
3953   /* There's no need to defer outputting this function any more; we
3954      know we want to output it.  */
3955   DECL_DEFER_OUTPUT (current_function_decl) = 0;
3956
3957   /* Now that we're done expanding trees to RTL, we shouldn't have any
3958      more CONCATs anywhere.  */
3959   generating_concat_p = 0;
3960
3961   if (dump_file)
3962     {
3963       fprintf (dump_file,
3964                "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
3965       /* And the pass manager will dump RTL for us.  */
3966     }
3967
3968   /* If we're emitting a nested function, make sure its parent gets
3969      emitted as well.  Doing otherwise confuses debug info.  */
3970   {
3971     tree parent;
3972     for (parent = DECL_CONTEXT (current_function_decl);
3973          parent != NULL_TREE;
3974          parent = get_containing_scope (parent))
3975       if (TREE_CODE (parent) == FUNCTION_DECL)
3976         TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
3977   }
3978
3979   /* We are now committed to emitting code for this function.  Do any
3980      preparation, such as emitting abstract debug info for the inline
3981      before it gets mangled by optimization.  */
3982   if (cgraph_function_possibly_inlined_p (current_function_decl))
3983     (*debug_hooks->outlining_inline_function) (current_function_decl);
3984
3985   TREE_ASM_WRITTEN (current_function_decl) = 1;
3986
3987   /* After expanding, the return labels are no longer needed. */
3988   return_label = NULL;
3989   naked_return_label = NULL;
3990   /* Tag the blocks with a depth number so that change_scope can find
3991      the common parent easily.  */
3992   set_block_levels (DECL_INITIAL (cfun->decl), 0);
3993   default_rtl_profile ();
3994   timevar_pop (TV_POST_EXPAND);
3995   return 0;
3996 }
3997
3998 struct rtl_opt_pass pass_expand =
3999 {
4000  {
4001   RTL_PASS,
4002   "expand",                             /* name */
4003   NULL,                                 /* gate */
4004   gimple_expand_cfg,                    /* execute */
4005   NULL,                                 /* sub */
4006   NULL,                                 /* next */
4007   0,                                    /* static_pass_number */
4008   TV_EXPAND,                            /* tv_id */
4009   PROP_ssa | PROP_gimple_leh | PROP_cfg
4010     | PROP_gimple_lcx,                  /* properties_required */
4011   PROP_rtl,                             /* properties_provided */
4012   PROP_ssa | PROP_trees,                /* properties_destroyed */
4013   TODO_verify_ssa | TODO_verify_flow
4014     | TODO_verify_stmts,                /* todo_flags_start */
4015   TODO_dump_func
4016   | TODO_ggc_collect                    /* todo_flags_finish */
4017  }
4018 };