OSDN Git Service

gcc/
[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 = TREE_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 = TREE_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 = TREE_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 = TREE_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   /* Set TREE_USED on all variables in the local_decls.  */
1230   for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1231     TREE_USED (TREE_VALUE (t)) = 1;
1232
1233   /* Clear TREE_USED on all variables associated with a block scope.  */
1234   clear_tree_used (DECL_INITIAL (current_function_decl));
1235
1236   /* Initialize local stack smashing state.  */
1237   has_protected_decls = false;
1238   has_short_buffer = false;
1239 }
1240
1241 /* Free up stack variable graph data.  */
1242 static void
1243 fini_vars_expansion (void)
1244 {
1245   size_t i, n = stack_vars_num;
1246   for (i = 0; i < n; i++)
1247     BITMAP_FREE (stack_vars[i].conflicts);
1248   XDELETEVEC (stack_vars);
1249   XDELETEVEC (stack_vars_sorted);
1250   stack_vars = NULL;
1251   stack_vars_alloc = stack_vars_num = 0;
1252 }
1253
1254 /* Make a fair guess for the size of the stack frame of the current
1255    function.  This doesn't have to be exact, the result is only used
1256    in the inline heuristics.  So we don't want to run the full stack
1257    var packing algorithm (which is quadratic in the number of stack
1258    vars).  Instead, we calculate the total size of all stack vars.
1259    This turns out to be a pretty fair estimate -- packing of stack
1260    vars doesn't happen very often.  */
1261
1262 HOST_WIDE_INT
1263 estimated_stack_frame_size (void)
1264 {
1265   HOST_WIDE_INT size = 0;
1266   size_t i;
1267   tree t, outer_block = DECL_INITIAL (current_function_decl);
1268
1269   init_vars_expansion ();
1270
1271   for (t = cfun->local_decls; t; t = TREE_CHAIN (t))
1272     {
1273       tree var = TREE_VALUE (t);
1274
1275       if (TREE_USED (var))
1276         size += expand_one_var (var, true, false);
1277       TREE_USED (var) = 1;
1278     }
1279   size += account_used_vars_for_block (outer_block, true);
1280
1281   if (stack_vars_num > 0)
1282     {
1283       /* Fake sorting the stack vars for account_stack_vars ().  */
1284       stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
1285       for (i = 0; i < stack_vars_num; ++i)
1286         stack_vars_sorted[i] = i;
1287       size += account_stack_vars ();
1288       fini_vars_expansion ();
1289     }
1290
1291   return size;
1292 }
1293
1294 /* Expand all variables used in the function.  */
1295
1296 static void
1297 expand_used_vars (void)
1298 {
1299   tree t, next, outer_block = DECL_INITIAL (current_function_decl);
1300   tree maybe_local_decls = NULL_TREE;
1301   unsigned i;
1302
1303   /* Compute the phase of the stack frame for this function.  */
1304   {
1305     int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1306     int off = STARTING_FRAME_OFFSET % align;
1307     frame_phase = off ? align - off : 0;
1308   }
1309
1310   init_vars_expansion ();
1311
1312   for (i = 0; i < SA.map->num_partitions; i++)
1313     {
1314       tree var = partition_to_var (SA.map, i);
1315
1316       gcc_assert (is_gimple_reg (var));
1317       if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
1318         expand_one_var (var, true, true);
1319       else
1320         {
1321           /* This is a PARM_DECL or RESULT_DECL.  For those partitions that
1322              contain the default def (representing the parm or result itself)
1323              we don't do anything here.  But those which don't contain the
1324              default def (representing a temporary based on the parm/result)
1325              we need to allocate space just like for normal VAR_DECLs.  */
1326           if (!bitmap_bit_p (SA.partition_has_default_def, i))
1327             {
1328               expand_one_var (var, true, true);
1329               gcc_assert (SA.partition_to_pseudo[i]);
1330             }
1331         }
1332     }
1333
1334   /* At this point all variables on the local_decls with TREE_USED
1335      set are not associated with any block scope.  Lay them out.  */
1336   t = cfun->local_decls;
1337   cfun->local_decls = NULL_TREE;
1338   for (; t; t = next)
1339     {
1340       tree var = TREE_VALUE (t);
1341       bool expand_now = false;
1342
1343       next = TREE_CHAIN (t);
1344
1345       /* Expanded above already.  */
1346       if (is_gimple_reg (var))
1347         {
1348           TREE_USED (var) = 0;
1349           goto next;
1350         }
1351       /* We didn't set a block for static or extern because it's hard
1352          to tell the difference between a global variable (re)declared
1353          in a local scope, and one that's really declared there to
1354          begin with.  And it doesn't really matter much, since we're
1355          not giving them stack space.  Expand them now.  */
1356       else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
1357         expand_now = true;
1358
1359       /* If the variable is not associated with any block, then it
1360          was created by the optimizers, and could be live anywhere
1361          in the function.  */
1362       else if (TREE_USED (var))
1363         expand_now = true;
1364
1365       /* Finally, mark all variables on the list as used.  We'll use
1366          this in a moment when we expand those associated with scopes.  */
1367       TREE_USED (var) = 1;
1368
1369       if (expand_now)
1370         expand_one_var (var, true, true);
1371
1372     next:
1373       if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
1374         {
1375           rtx rtl = DECL_RTL_IF_SET (var);
1376
1377           /* Keep artificial non-ignored vars in cfun->local_decls
1378              chain until instantiate_decls.  */
1379           if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1380             {
1381               TREE_CHAIN (t) = cfun->local_decls;
1382               cfun->local_decls = t;
1383               continue;
1384             }
1385           else if (rtl == NULL_RTX)
1386             {
1387               /* If rtl isn't set yet, which can happen e.g. with
1388                  -fstack-protector, retry before returning from this
1389                  function.  */
1390               TREE_CHAIN (t) = maybe_local_decls;
1391               maybe_local_decls = t;
1392               continue;
1393             }
1394         }
1395
1396       ggc_free (t);
1397     }
1398
1399   /* At this point, all variables within the block tree with TREE_USED
1400      set are actually used by the optimized function.  Lay them out.  */
1401   expand_used_vars_for_block (outer_block, true);
1402
1403   if (stack_vars_num > 0)
1404     {
1405       /* Due to the way alias sets work, no variables with non-conflicting
1406          alias sets may be assigned the same address.  Add conflicts to
1407          reflect this.  */
1408       add_alias_set_conflicts ();
1409
1410       /* If stack protection is enabled, we don't share space between
1411          vulnerable data and non-vulnerable data.  */
1412       if (flag_stack_protect)
1413         add_stack_protection_conflicts ();
1414
1415       /* Now that we have collected all stack variables, and have computed a
1416          minimal interference graph, attempt to save some stack space.  */
1417       partition_stack_vars ();
1418       if (dump_file)
1419         dump_stack_var_partition ();
1420     }
1421
1422   /* There are several conditions under which we should create a
1423      stack guard: protect-all, alloca used, protected decls present.  */
1424   if (flag_stack_protect == 2
1425       || (flag_stack_protect
1426           && (cfun->calls_alloca || has_protected_decls)))
1427     create_stack_guard ();
1428
1429   /* Assign rtl to each variable based on these partitions.  */
1430   if (stack_vars_num > 0)
1431     {
1432       /* Reorder decls to be protected by iterating over the variables
1433          array multiple times, and allocating out of each phase in turn.  */
1434       /* ??? We could probably integrate this into the qsort we did
1435          earlier, such that we naturally see these variables first,
1436          and thus naturally allocate things in the right order.  */
1437       if (has_protected_decls)
1438         {
1439           /* Phase 1 contains only character arrays.  */
1440           expand_stack_vars (stack_protect_decl_phase_1);
1441
1442           /* Phase 2 contains other kinds of arrays.  */
1443           if (flag_stack_protect == 2)
1444             expand_stack_vars (stack_protect_decl_phase_2);
1445         }
1446
1447       expand_stack_vars (NULL);
1448
1449       fini_vars_expansion ();
1450     }
1451
1452   /* If there were any artificial non-ignored vars without rtl
1453      found earlier, see if deferred stack allocation hasn't assigned
1454      rtl to them.  */
1455   for (t = maybe_local_decls; t; t = next)
1456     {
1457       tree var = TREE_VALUE (t);
1458       rtx rtl = DECL_RTL_IF_SET (var);
1459
1460       next = TREE_CHAIN (t);
1461
1462       /* Keep artificial non-ignored vars in cfun->local_decls
1463          chain until instantiate_decls.  */
1464       if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
1465         {
1466           TREE_CHAIN (t) = cfun->local_decls;
1467           cfun->local_decls = t;
1468           continue;
1469         }
1470
1471       ggc_free (t);
1472     }
1473
1474   /* If the target requires that FRAME_OFFSET be aligned, do it.  */
1475   if (STACK_ALIGNMENT_NEEDED)
1476     {
1477       HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1478       if (!FRAME_GROWS_DOWNWARD)
1479         frame_offset += align - 1;
1480       frame_offset &= -align;
1481     }
1482 }
1483
1484
1485 /* If we need to produce a detailed dump, print the tree representation
1486    for STMT to the dump file.  SINCE is the last RTX after which the RTL
1487    generated for STMT should have been appended.  */
1488
1489 static void
1490 maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx since)
1491 {
1492   if (dump_file && (dump_flags & TDF_DETAILS))
1493     {
1494       fprintf (dump_file, "\n;; ");
1495       print_gimple_stmt (dump_file, stmt, 0,
1496                          TDF_SLIM | (dump_flags & TDF_LINENO));
1497       fprintf (dump_file, "\n");
1498
1499       print_rtl (dump_file, since ? NEXT_INSN (since) : since);
1500     }
1501 }
1502
1503 /* Maps the blocks that do not contain tree labels to rtx labels.  */
1504
1505 static struct pointer_map_t *lab_rtx_for_bb;
1506
1507 /* Returns the label_rtx expression for a label starting basic block BB.  */
1508
1509 static rtx
1510 label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
1511 {
1512   gimple_stmt_iterator gsi;
1513   tree lab;
1514   gimple lab_stmt;
1515   void **elt;
1516
1517   if (bb->flags & BB_RTL)
1518     return block_label (bb);
1519
1520   elt = pointer_map_contains (lab_rtx_for_bb, bb);
1521   if (elt)
1522     return (rtx) *elt;
1523
1524   /* Find the tree label if it is present.  */
1525
1526   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1527     {
1528       lab_stmt = gsi_stmt (gsi);
1529       if (gimple_code (lab_stmt) != GIMPLE_LABEL)
1530         break;
1531
1532       lab = gimple_label_label (lab_stmt);
1533       if (DECL_NONLOCAL (lab))
1534         break;
1535
1536       return label_rtx (lab);
1537     }
1538
1539   elt = pointer_map_insert (lab_rtx_for_bb, bb);
1540   *elt = gen_label_rtx ();
1541   return (rtx) *elt;
1542 }
1543
1544
1545 /* A subroutine of expand_gimple_cond.  Given E, a fallthrough edge
1546    of a basic block where we just expanded the conditional at the end,
1547    possibly clean up the CFG and instruction sequence.  LAST is the
1548    last instruction before the just emitted jump sequence.  */
1549
1550 static void
1551 maybe_cleanup_end_of_block (edge e, rtx last)
1552 {
1553   /* Special case: when jumpif decides that the condition is
1554      trivial it emits an unconditional jump (and the necessary
1555      barrier).  But we still have two edges, the fallthru one is
1556      wrong.  purge_dead_edges would clean this up later.  Unfortunately
1557      we have to insert insns (and split edges) before
1558      find_many_sub_basic_blocks and hence before purge_dead_edges.
1559      But splitting edges might create new blocks which depend on the
1560      fact that if there are two edges there's no barrier.  So the
1561      barrier would get lost and verify_flow_info would ICE.  Instead
1562      of auditing all edge splitters to care for the barrier (which
1563      normally isn't there in a cleaned CFG), fix it here.  */
1564   if (BARRIER_P (get_last_insn ()))
1565     {
1566       rtx insn;
1567       remove_edge (e);
1568       /* Now, we have a single successor block, if we have insns to
1569          insert on the remaining edge we potentially will insert
1570          it at the end of this block (if the dest block isn't feasible)
1571          in order to avoid splitting the edge.  This insertion will take
1572          place in front of the last jump.  But we might have emitted
1573          multiple jumps (conditional and one unconditional) to the
1574          same destination.  Inserting in front of the last one then
1575          is a problem.  See PR 40021.  We fix this by deleting all
1576          jumps except the last unconditional one.  */
1577       insn = PREV_INSN (get_last_insn ());
1578       /* Make sure we have an unconditional jump.  Otherwise we're
1579          confused.  */
1580       gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
1581       for (insn = PREV_INSN (insn); insn != last;)
1582         {
1583           insn = PREV_INSN (insn);
1584           if (JUMP_P (NEXT_INSN (insn)))
1585             delete_insn (NEXT_INSN (insn));
1586         }
1587     }
1588 }
1589
1590 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_COND.
1591    Returns a new basic block if we've terminated the current basic
1592    block and created a new one.  */
1593
1594 static basic_block
1595 expand_gimple_cond (basic_block bb, gimple stmt)
1596 {
1597   basic_block new_bb, dest;
1598   edge new_edge;
1599   edge true_edge;
1600   edge false_edge;
1601   rtx last2, last;
1602   enum tree_code code;
1603   tree op0, op1;
1604
1605   code = gimple_cond_code (stmt);
1606   op0 = gimple_cond_lhs (stmt);
1607   op1 = gimple_cond_rhs (stmt);
1608   /* We're sometimes presented with such code:
1609        D.123_1 = x < y;
1610        if (D.123_1 != 0)
1611          ...
1612      This would expand to two comparisons which then later might
1613      be cleaned up by combine.  But some pattern matchers like if-conversion
1614      work better when there's only one compare, so make up for this
1615      here as special exception if TER would have made the same change.  */
1616   if (gimple_cond_single_var_p (stmt)
1617       && SA.values
1618       && TREE_CODE (op0) == SSA_NAME
1619       && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
1620     {
1621       gimple second = SSA_NAME_DEF_STMT (op0);
1622       if (gimple_code (second) == GIMPLE_ASSIGN)
1623         {
1624           enum tree_code code2 = gimple_assign_rhs_code (second);
1625           if (TREE_CODE_CLASS (code2) == tcc_comparison)
1626             {
1627               code = code2;
1628               op0 = gimple_assign_rhs1 (second);
1629               op1 = gimple_assign_rhs2 (second);
1630             }
1631           /* If jumps are cheap turn some more codes into
1632              jumpy sequences.  */
1633           else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4)
1634             {
1635               if ((code2 == BIT_AND_EXPR
1636                    && TYPE_PRECISION (TREE_TYPE (op0)) == 1
1637                    && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
1638                   || code2 == TRUTH_AND_EXPR)
1639                 {
1640                   code = TRUTH_ANDIF_EXPR;
1641                   op0 = gimple_assign_rhs1 (second);
1642                   op1 = gimple_assign_rhs2 (second);
1643                 }
1644               else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
1645                 {
1646                   code = TRUTH_ORIF_EXPR;
1647                   op0 = gimple_assign_rhs1 (second);
1648                   op1 = gimple_assign_rhs2 (second);
1649                 }
1650             }
1651         }
1652     }
1653
1654   last2 = last = get_last_insn ();
1655
1656   extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
1657   if (gimple_has_location (stmt))
1658     {
1659       set_curr_insn_source_location (gimple_location (stmt));
1660       set_curr_insn_block (gimple_block (stmt));
1661     }
1662
1663   /* These flags have no purpose in RTL land.  */
1664   true_edge->flags &= ~EDGE_TRUE_VALUE;
1665   false_edge->flags &= ~EDGE_FALSE_VALUE;
1666
1667   /* We can either have a pure conditional jump with one fallthru edge or
1668      two-way jump that needs to be decomposed into two basic blocks.  */
1669   if (false_edge->dest == bb->next_bb)
1670     {
1671       jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1672                 true_edge->probability);
1673       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1674       if (true_edge->goto_locus)
1675         {
1676           set_curr_insn_source_location (true_edge->goto_locus);
1677           set_curr_insn_block (true_edge->goto_block);
1678           true_edge->goto_locus = curr_insn_locator ();
1679         }
1680       true_edge->goto_block = NULL;
1681       false_edge->flags |= EDGE_FALLTHRU;
1682       maybe_cleanup_end_of_block (false_edge, last);
1683       return NULL;
1684     }
1685   if (true_edge->dest == bb->next_bb)
1686     {
1687       jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
1688                    false_edge->probability);
1689       maybe_dump_rtl_for_gimple_stmt (stmt, last);
1690       if (false_edge->goto_locus)
1691         {
1692           set_curr_insn_source_location (false_edge->goto_locus);
1693           set_curr_insn_block (false_edge->goto_block);
1694           false_edge->goto_locus = curr_insn_locator ();
1695         }
1696       false_edge->goto_block = NULL;
1697       true_edge->flags |= EDGE_FALLTHRU;
1698       maybe_cleanup_end_of_block (true_edge, last);
1699       return NULL;
1700     }
1701
1702   jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
1703             true_edge->probability);
1704   last = get_last_insn ();
1705   if (false_edge->goto_locus)
1706     {
1707       set_curr_insn_source_location (false_edge->goto_locus);
1708       set_curr_insn_block (false_edge->goto_block);
1709       false_edge->goto_locus = curr_insn_locator ();
1710     }
1711   false_edge->goto_block = NULL;
1712   emit_jump (label_rtx_for_bb (false_edge->dest));
1713
1714   BB_END (bb) = last;
1715   if (BARRIER_P (BB_END (bb)))
1716     BB_END (bb) = PREV_INSN (BB_END (bb));
1717   update_bb_for_insn (bb);
1718
1719   new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
1720   dest = false_edge->dest;
1721   redirect_edge_succ (false_edge, new_bb);
1722   false_edge->flags |= EDGE_FALLTHRU;
1723   new_bb->count = false_edge->count;
1724   new_bb->frequency = EDGE_FREQUENCY (false_edge);
1725   new_edge = make_edge (new_bb, dest, 0);
1726   new_edge->probability = REG_BR_PROB_BASE;
1727   new_edge->count = new_bb->count;
1728   if (BARRIER_P (BB_END (new_bb)))
1729     BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
1730   update_bb_for_insn (new_bb);
1731
1732   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
1733
1734   if (true_edge->goto_locus)
1735     {
1736       set_curr_insn_source_location (true_edge->goto_locus);
1737       set_curr_insn_block (true_edge->goto_block);
1738       true_edge->goto_locus = curr_insn_locator ();
1739     }
1740   true_edge->goto_block = NULL;
1741
1742   return new_bb;
1743 }
1744
1745 /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
1746    statement STMT.  */
1747
1748 static void
1749 expand_call_stmt (gimple stmt)
1750 {
1751   tree exp;
1752   tree lhs = gimple_call_lhs (stmt);
1753   size_t i;
1754   bool builtin_p;
1755   tree decl;
1756
1757   exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
1758
1759   CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
1760   decl = gimple_call_fndecl (stmt);
1761   builtin_p = decl && DECL_BUILT_IN (decl);
1762
1763   TREE_TYPE (exp) = gimple_call_return_type (stmt);
1764   CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
1765
1766   for (i = 0; i < gimple_call_num_args (stmt); i++)
1767     {
1768       tree arg = gimple_call_arg (stmt, i);
1769       gimple def;
1770       /* TER addresses into arguments of builtin functions so we have a
1771          chance to infer more correct alignment information.  See PR39954.  */
1772       if (builtin_p
1773           && TREE_CODE (arg) == SSA_NAME
1774           && (def = get_gimple_for_ssa_name (arg))
1775           && gimple_assign_rhs_code (def) == ADDR_EXPR)
1776         arg = gimple_assign_rhs1 (def);
1777       CALL_EXPR_ARG (exp, i) = arg;
1778     }
1779
1780   if (gimple_has_side_effects (stmt))
1781     TREE_SIDE_EFFECTS (exp) = 1;
1782
1783   if (gimple_call_nothrow_p (stmt))
1784     TREE_NOTHROW (exp) = 1;
1785
1786   CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
1787   CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
1788   CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
1789   CALL_CANNOT_INLINE_P (exp) = gimple_call_cannot_inline_p (stmt);
1790   CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
1791   SET_EXPR_LOCATION (exp, gimple_location (stmt));
1792   TREE_BLOCK (exp) = gimple_block (stmt);
1793
1794   if (lhs)
1795     expand_assignment (lhs, exp, false);
1796   else
1797     expand_expr_real_1 (exp, const0_rtx, VOIDmode, EXPAND_NORMAL, NULL);
1798 }
1799
1800 /* A subroutine of expand_gimple_stmt, expanding one gimple statement
1801    STMT that doesn't require special handling for outgoing edges.  That
1802    is no tailcalls and no GIMPLE_COND.  */
1803
1804 static void
1805 expand_gimple_stmt_1 (gimple stmt)
1806 {
1807   tree op0;
1808   switch (gimple_code (stmt))
1809     {
1810     case GIMPLE_GOTO:
1811       op0 = gimple_goto_dest (stmt);
1812       if (TREE_CODE (op0) == LABEL_DECL)
1813         expand_goto (op0);
1814       else
1815         expand_computed_goto (op0);
1816       break;
1817     case GIMPLE_LABEL:
1818       expand_label (gimple_label_label (stmt));
1819       break;
1820     case GIMPLE_NOP:
1821     case GIMPLE_PREDICT:
1822       break;
1823     case GIMPLE_SWITCH:
1824       expand_case (stmt);
1825       break;
1826     case GIMPLE_ASM:
1827       expand_asm_stmt (stmt);
1828       break;
1829     case GIMPLE_CALL:
1830       expand_call_stmt (stmt);
1831       break;
1832
1833     case GIMPLE_RETURN:
1834       op0 = gimple_return_retval (stmt);
1835
1836       if (op0 && op0 != error_mark_node)
1837         {
1838           tree result = DECL_RESULT (current_function_decl);
1839
1840           /* If we are not returning the current function's RESULT_DECL,
1841              build an assignment to it.  */
1842           if (op0 != result)
1843             {
1844               /* I believe that a function's RESULT_DECL is unique.  */
1845               gcc_assert (TREE_CODE (op0) != RESULT_DECL);
1846
1847               /* ??? We'd like to use simply expand_assignment here,
1848                  but this fails if the value is of BLKmode but the return
1849                  decl is a register.  expand_return has special handling
1850                  for this combination, which eventually should move
1851                  to common code.  See comments there.  Until then, let's
1852                  build a modify expression :-/  */
1853               op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
1854                             result, op0);
1855             }
1856         }
1857       if (!op0)
1858         expand_null_return ();
1859       else
1860         expand_return (op0);
1861       break;
1862
1863     case GIMPLE_ASSIGN:
1864       {
1865         tree lhs = gimple_assign_lhs (stmt);
1866
1867         /* Tree expand used to fiddle with |= and &= of two bitfield
1868            COMPONENT_REFs here.  This can't happen with gimple, the LHS
1869            of binary assigns must be a gimple reg.  */
1870
1871         if (TREE_CODE (lhs) != SSA_NAME
1872             || get_gimple_rhs_class (gimple_expr_code (stmt))
1873                == GIMPLE_SINGLE_RHS)
1874           {
1875             tree rhs = gimple_assign_rhs1 (stmt);
1876             gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
1877                         == GIMPLE_SINGLE_RHS);
1878             if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
1879               SET_EXPR_LOCATION (rhs, gimple_location (stmt));
1880             expand_assignment (lhs, rhs,
1881                                gimple_assign_nontemporal_move_p (stmt));
1882           }
1883         else
1884           {
1885             rtx target, temp;
1886             bool nontemporal = gimple_assign_nontemporal_move_p (stmt);
1887             struct separate_ops ops;
1888             bool promoted = false;
1889
1890             target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1891             if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
1892               promoted = true;
1893
1894             ops.code = gimple_assign_rhs_code (stmt);
1895             ops.type = TREE_TYPE (lhs);
1896             switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
1897               {
1898                 case GIMPLE_TERNARY_RHS:
1899                   ops.op2 = gimple_assign_rhs3 (stmt);
1900                   /* Fallthru */
1901                 case GIMPLE_BINARY_RHS:
1902                   ops.op1 = gimple_assign_rhs2 (stmt);
1903                   /* Fallthru */
1904                 case GIMPLE_UNARY_RHS:
1905                   ops.op0 = gimple_assign_rhs1 (stmt);
1906                   break;
1907                 default:
1908                   gcc_unreachable ();
1909               }
1910             ops.location = gimple_location (stmt);
1911
1912             /* If we want to use a nontemporal store, force the value to
1913                register first.  If we store into a promoted register,
1914                don't directly expand to target.  */
1915             temp = nontemporal || promoted ? NULL_RTX : target;
1916             temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
1917                                        EXPAND_NORMAL);
1918
1919             if (temp == target)
1920               ;
1921             else if (promoted)
1922               {
1923                 int unsignedp = SUBREG_PROMOTED_UNSIGNED_P (target);
1924                 /* If TEMP is a VOIDmode constant, use convert_modes to make
1925                    sure that we properly convert it.  */
1926                 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
1927                   {
1928                     temp = convert_modes (GET_MODE (target),
1929                                           TYPE_MODE (ops.type),
1930                                           temp, unsignedp);
1931                     temp = convert_modes (GET_MODE (SUBREG_REG (target)),
1932                                           GET_MODE (target), temp, unsignedp);
1933                   }
1934
1935                 convert_move (SUBREG_REG (target), temp, unsignedp);
1936               }
1937             else if (nontemporal && emit_storent_insn (target, temp))
1938               ;
1939             else
1940               {
1941                 temp = force_operand (temp, target);
1942                 if (temp != target)
1943                   emit_move_insn (target, temp);
1944               }
1945           }
1946       }
1947       break;
1948
1949     default:
1950       gcc_unreachable ();
1951     }
1952 }
1953
1954 /* Expand one gimple statement STMT and return the last RTL instruction
1955    before any of the newly generated ones.
1956
1957    In addition to generating the necessary RTL instructions this also
1958    sets REG_EH_REGION notes if necessary and sets the current source
1959    location for diagnostics.  */
1960
1961 static rtx
1962 expand_gimple_stmt (gimple stmt)
1963 {
1964   int lp_nr = 0;
1965   rtx last = NULL;
1966   location_t saved_location = input_location;
1967
1968   last = get_last_insn ();
1969
1970   /* If this is an expression of some kind and it has an associated line
1971      number, then emit the line number before expanding the expression.
1972
1973      We need to save and restore the file and line information so that
1974      errors discovered during expansion are emitted with the right
1975      information.  It would be better of the diagnostic routines
1976      used the file/line information embedded in the tree nodes rather
1977      than globals.  */
1978   gcc_assert (cfun);
1979
1980   if (gimple_has_location (stmt))
1981     {
1982       input_location = gimple_location (stmt);
1983       set_curr_insn_source_location (input_location);
1984
1985       /* Record where the insns produced belong.  */
1986       set_curr_insn_block (gimple_block (stmt));
1987     }
1988
1989   expand_gimple_stmt_1 (stmt);
1990   /* Free any temporaries used to evaluate this statement.  */
1991   free_temp_slots ();
1992
1993   input_location = saved_location;
1994
1995   /* Mark all insns that may trap.  */
1996   lp_nr = lookup_stmt_eh_lp (stmt);
1997   if (lp_nr)
1998     {
1999       rtx insn;
2000       for (insn = next_real_insn (last); insn;
2001            insn = next_real_insn (insn))
2002         {
2003           if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
2004               /* If we want exceptions for non-call insns, any
2005                  may_trap_p instruction may throw.  */
2006               && GET_CODE (PATTERN (insn)) != CLOBBER
2007               && GET_CODE (PATTERN (insn)) != USE
2008               && insn_could_throw_p (insn))
2009             make_reg_eh_region_note (insn, 0, lp_nr);
2010         }
2011     }
2012
2013   return last;
2014 }
2015
2016 /* A subroutine of expand_gimple_basic_block.  Expand one GIMPLE_CALL
2017    that has CALL_EXPR_TAILCALL set.  Returns non-null if we actually
2018    generated a tail call (something that might be denied by the ABI
2019    rules governing the call; see calls.c).
2020
2021    Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
2022    can still reach the rest of BB.  The case here is __builtin_sqrt,
2023    where the NaN result goes through the external function (with a
2024    tailcall) and the normal result happens via a sqrt instruction.  */
2025
2026 static basic_block
2027 expand_gimple_tailcall (basic_block bb, gimple stmt, bool *can_fallthru)
2028 {
2029   rtx last2, last;
2030   edge e;
2031   edge_iterator ei;
2032   int probability;
2033   gcov_type count;
2034
2035   last2 = last = expand_gimple_stmt (stmt);
2036
2037   for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
2038     if (CALL_P (last) && SIBLING_CALL_P (last))
2039       goto found;
2040
2041   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2042
2043   *can_fallthru = true;
2044   return NULL;
2045
2046  found:
2047   /* ??? Wouldn't it be better to just reset any pending stack adjust?
2048      Any instructions emitted here are about to be deleted.  */
2049   do_pending_stack_adjust ();
2050
2051   /* Remove any non-eh, non-abnormal edges that don't go to exit.  */
2052   /* ??? I.e. the fallthrough edge.  HOWEVER!  If there were to be
2053      EH or abnormal edges, we shouldn't have created a tail call in
2054      the first place.  So it seems to me we should just be removing
2055      all edges here, or redirecting the existing fallthru edge to
2056      the exit block.  */
2057
2058   probability = 0;
2059   count = 0;
2060
2061   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2062     {
2063       if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
2064         {
2065           if (e->dest != EXIT_BLOCK_PTR)
2066             {
2067               e->dest->count -= e->count;
2068               e->dest->frequency -= EDGE_FREQUENCY (e);
2069               if (e->dest->count < 0)
2070                 e->dest->count = 0;
2071               if (e->dest->frequency < 0)
2072                 e->dest->frequency = 0;
2073             }
2074           count += e->count;
2075           probability += e->probability;
2076           remove_edge (e);
2077         }
2078       else
2079         ei_next (&ei);
2080     }
2081
2082   /* This is somewhat ugly: the call_expr expander often emits instructions
2083      after the sibcall (to perform the function return).  These confuse the
2084      find_many_sub_basic_blocks code, so we need to get rid of these.  */
2085   last = NEXT_INSN (last);
2086   gcc_assert (BARRIER_P (last));
2087
2088   *can_fallthru = false;
2089   while (NEXT_INSN (last))
2090     {
2091       /* For instance an sqrt builtin expander expands if with
2092          sibcall in the then and label for `else`.  */
2093       if (LABEL_P (NEXT_INSN (last)))
2094         {
2095           *can_fallthru = true;
2096           break;
2097         }
2098       delete_insn (NEXT_INSN (last));
2099     }
2100
2101   e = make_edge (bb, EXIT_BLOCK_PTR, EDGE_ABNORMAL | EDGE_SIBCALL);
2102   e->probability += probability;
2103   e->count += count;
2104   BB_END (bb) = last;
2105   update_bb_for_insn (bb);
2106
2107   if (NEXT_INSN (last))
2108     {
2109       bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
2110
2111       last = BB_END (bb);
2112       if (BARRIER_P (last))
2113         BB_END (bb) = PREV_INSN (last);
2114     }
2115
2116   maybe_dump_rtl_for_gimple_stmt (stmt, last2);
2117
2118   return bb;
2119 }
2120
2121 /* Return the difference between the floor and the truncated result of
2122    a signed division by OP1 with remainder MOD.  */
2123 static rtx
2124 floor_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2125 {
2126   /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
2127   return gen_rtx_IF_THEN_ELSE
2128     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2129      gen_rtx_IF_THEN_ELSE
2130      (mode, gen_rtx_LT (BImode,
2131                         gen_rtx_DIV (mode, op1, mod),
2132                         const0_rtx),
2133       constm1_rtx, const0_rtx),
2134      const0_rtx);
2135 }
2136
2137 /* Return the difference between the ceil and the truncated result of
2138    a signed division by OP1 with remainder MOD.  */
2139 static rtx
2140 ceil_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2141 {
2142   /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
2143   return gen_rtx_IF_THEN_ELSE
2144     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2145      gen_rtx_IF_THEN_ELSE
2146      (mode, gen_rtx_GT (BImode,
2147                         gen_rtx_DIV (mode, op1, mod),
2148                         const0_rtx),
2149       const1_rtx, const0_rtx),
2150      const0_rtx);
2151 }
2152
2153 /* Return the difference between the ceil and the truncated result of
2154    an unsigned division by OP1 with remainder MOD.  */
2155 static rtx
2156 ceil_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
2157 {
2158   /* (mod != 0 ? 1 : 0) */
2159   return gen_rtx_IF_THEN_ELSE
2160     (mode, gen_rtx_NE (BImode, mod, const0_rtx),
2161      const1_rtx, const0_rtx);
2162 }
2163
2164 /* Return the difference between the rounded and the truncated result
2165    of a signed division by OP1 with remainder MOD.  Halfway cases are
2166    rounded away from zero, rather than to the nearest even number.  */
2167 static rtx
2168 round_sdiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2169 {
2170   /* (abs (mod) >= abs (op1) - abs (mod)
2171       ? (op1 / mod > 0 ? 1 : -1)
2172       : 0) */
2173   return gen_rtx_IF_THEN_ELSE
2174     (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
2175                        gen_rtx_MINUS (mode,
2176                                       gen_rtx_ABS (mode, op1),
2177                                       gen_rtx_ABS (mode, mod))),
2178      gen_rtx_IF_THEN_ELSE
2179      (mode, gen_rtx_GT (BImode,
2180                         gen_rtx_DIV (mode, op1, mod),
2181                         const0_rtx),
2182       const1_rtx, constm1_rtx),
2183      const0_rtx);
2184 }
2185
2186 /* Return the difference between the rounded and the truncated result
2187    of a unsigned division by OP1 with remainder MOD.  Halfway cases
2188    are rounded away from zero, rather than to the nearest even
2189    number.  */
2190 static rtx
2191 round_udiv_adjust (enum machine_mode mode, rtx mod, rtx op1)
2192 {
2193   /* (mod >= op1 - mod ? 1 : 0) */
2194   return gen_rtx_IF_THEN_ELSE
2195     (mode, gen_rtx_GE (BImode, mod,
2196                        gen_rtx_MINUS (mode, op1, mod)),
2197      const1_rtx, const0_rtx);
2198 }
2199
2200 /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
2201    any rtl.  */
2202
2203 static rtx
2204 convert_debug_memory_address (enum machine_mode mode, rtx x)
2205 {
2206   enum machine_mode xmode = GET_MODE (x);
2207
2208 #ifndef POINTERS_EXTEND_UNSIGNED
2209   gcc_assert (mode == Pmode);
2210   gcc_assert (xmode == mode || xmode == VOIDmode);
2211 #else
2212   gcc_assert (mode == Pmode || mode == ptr_mode);
2213
2214   if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
2215     return x;
2216
2217   if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (xmode))
2218     x = simplify_gen_subreg (mode, x, xmode,
2219                              subreg_lowpart_offset
2220                              (mode, xmode));
2221   else if (POINTERS_EXTEND_UNSIGNED > 0)
2222     x = gen_rtx_ZERO_EXTEND (mode, x);
2223   else if (!POINTERS_EXTEND_UNSIGNED)
2224     x = gen_rtx_SIGN_EXTEND (mode, x);
2225   else
2226     gcc_unreachable ();
2227 #endif /* POINTERS_EXTEND_UNSIGNED */
2228
2229   return x;
2230 }
2231
2232 /* Return an RTX equivalent to the value of the tree expression
2233    EXP.  */
2234
2235 static rtx
2236 expand_debug_expr (tree exp)
2237 {
2238   rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
2239   enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
2240   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
2241   addr_space_t as;
2242   enum machine_mode address_mode;
2243
2244   switch (TREE_CODE_CLASS (TREE_CODE (exp)))
2245     {
2246     case tcc_expression:
2247       switch (TREE_CODE (exp))
2248         {
2249         case COND_EXPR:
2250         case DOT_PROD_EXPR:
2251         case WIDEN_MULT_PLUS_EXPR:
2252         case WIDEN_MULT_MINUS_EXPR:
2253           goto ternary;
2254
2255         case TRUTH_ANDIF_EXPR:
2256         case TRUTH_ORIF_EXPR:
2257         case TRUTH_AND_EXPR:
2258         case TRUTH_OR_EXPR:
2259         case TRUTH_XOR_EXPR:
2260           goto binary;
2261
2262         case TRUTH_NOT_EXPR:
2263           goto unary;
2264
2265         default:
2266           break;
2267         }
2268       break;
2269
2270     ternary:
2271       op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
2272       if (!op2)
2273         return NULL_RTX;
2274       /* Fall through.  */
2275
2276     binary:
2277     case tcc_binary:
2278     case tcc_comparison:
2279       op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
2280       if (!op1)
2281         return NULL_RTX;
2282       /* Fall through.  */
2283
2284     unary:
2285     case tcc_unary:
2286       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2287       if (!op0)
2288         return NULL_RTX;
2289       break;
2290
2291     case tcc_type:
2292     case tcc_statement:
2293       gcc_unreachable ();
2294
2295     case tcc_constant:
2296     case tcc_exceptional:
2297     case tcc_declaration:
2298     case tcc_reference:
2299     case tcc_vl_exp:
2300       break;
2301     }
2302
2303   switch (TREE_CODE (exp))
2304     {
2305     case STRING_CST:
2306       if (!lookup_constant_def (exp))
2307         {
2308           if (strlen (TREE_STRING_POINTER (exp)) + 1
2309               != (size_t) TREE_STRING_LENGTH (exp))
2310             return NULL_RTX;
2311           op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
2312           op0 = gen_rtx_MEM (BLKmode, op0);
2313           set_mem_attributes (op0, exp, 0);
2314           return op0;
2315         }
2316       /* Fall through...  */
2317
2318     case INTEGER_CST:
2319     case REAL_CST:
2320     case FIXED_CST:
2321       op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
2322       return op0;
2323
2324     case COMPLEX_CST:
2325       gcc_assert (COMPLEX_MODE_P (mode));
2326       op0 = expand_debug_expr (TREE_REALPART (exp));
2327       op1 = expand_debug_expr (TREE_IMAGPART (exp));
2328       return gen_rtx_CONCAT (mode, op0, op1);
2329
2330     case DEBUG_EXPR_DECL:
2331       op0 = DECL_RTL_IF_SET (exp);
2332
2333       if (op0)
2334         return op0;
2335
2336       op0 = gen_rtx_DEBUG_EXPR (mode);
2337       DEBUG_EXPR_TREE_DECL (op0) = exp;
2338       SET_DECL_RTL (exp, op0);
2339
2340       return op0;
2341
2342     case VAR_DECL:
2343     case PARM_DECL:
2344     case FUNCTION_DECL:
2345     case LABEL_DECL:
2346     case CONST_DECL:
2347     case RESULT_DECL:
2348       op0 = DECL_RTL_IF_SET (exp);
2349
2350       /* This decl was probably optimized away.  */
2351       if (!op0)
2352         {
2353           if (TREE_CODE (exp) != VAR_DECL
2354               || DECL_EXTERNAL (exp)
2355               || !TREE_STATIC (exp)
2356               || !DECL_NAME (exp)
2357               || DECL_HARD_REGISTER (exp)
2358               || mode == VOIDmode)
2359             return NULL;
2360
2361           op0 = make_decl_rtl_for_debug (exp);
2362           if (!MEM_P (op0)
2363               || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
2364               || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
2365             return NULL;
2366         }
2367       else
2368         op0 = copy_rtx (op0);
2369
2370       if (GET_MODE (op0) == BLKmode
2371           /* If op0 is not BLKmode, but BLKmode is, adjust_mode
2372              below would ICE.  While it is likely a FE bug,
2373              try to be robust here.  See PR43166.  */
2374           || mode == BLKmode
2375           || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
2376         {
2377           gcc_assert (MEM_P (op0));
2378           op0 = adjust_address_nv (op0, mode, 0);
2379           return op0;
2380         }
2381
2382       /* Fall through.  */
2383
2384     adjust_mode:
2385     case PAREN_EXPR:
2386     case NOP_EXPR:
2387     case CONVERT_EXPR:
2388       {
2389         enum machine_mode inner_mode = GET_MODE (op0);
2390
2391         if (mode == inner_mode)
2392           return op0;
2393
2394         if (inner_mode == VOIDmode)
2395           {
2396             if (TREE_CODE (exp) == SSA_NAME)
2397               inner_mode = TYPE_MODE (TREE_TYPE (exp));
2398             else
2399               inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
2400             if (mode == inner_mode)
2401               return op0;
2402           }
2403
2404         if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
2405           {
2406             if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
2407               op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
2408             else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
2409               op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
2410             else
2411               op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
2412           }
2413         else if (FLOAT_MODE_P (mode))
2414           {
2415             gcc_assert (TREE_CODE (exp) != SSA_NAME);
2416             if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
2417               op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
2418             else
2419               op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
2420           }
2421         else if (FLOAT_MODE_P (inner_mode))
2422           {
2423             if (unsignedp)
2424               op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
2425             else
2426               op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
2427           }
2428         else if (CONSTANT_P (op0)
2429                  || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
2430           op0 = simplify_gen_subreg (mode, op0, inner_mode,
2431                                      subreg_lowpart_offset (mode,
2432                                                             inner_mode));
2433         else if (unsignedp)
2434           op0 = gen_rtx_ZERO_EXTEND (mode, op0);
2435         else
2436           op0 = gen_rtx_SIGN_EXTEND (mode, op0);
2437
2438         return op0;
2439       }
2440
2441     case MEM_REF:
2442       /* ??? FIXME.  */
2443       if (!integer_zerop (TREE_OPERAND (exp, 1)))
2444         return NULL;
2445       /* Fallthru.  */
2446     case INDIRECT_REF:
2447     case ALIGN_INDIRECT_REF:
2448     case MISALIGNED_INDIRECT_REF:
2449       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2450       if (!op0)
2451         return NULL;
2452
2453       if (POINTER_TYPE_P (TREE_TYPE (exp)))
2454         {
2455           as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2456           address_mode = targetm.addr_space.address_mode (as);
2457         }
2458       else
2459         {
2460           as = ADDR_SPACE_GENERIC;
2461           address_mode = Pmode;
2462         }
2463
2464       if (TREE_CODE (exp) == ALIGN_INDIRECT_REF)
2465         {
2466           int align = TYPE_ALIGN_UNIT (TREE_TYPE (exp));
2467           op0 = gen_rtx_AND (address_mode, op0, GEN_INT (-align));
2468         }
2469
2470       op0 = gen_rtx_MEM (mode, op0);
2471
2472       set_mem_attributes (op0, exp, 0);
2473       set_mem_addr_space (op0, as);
2474
2475       return op0;
2476
2477     case TARGET_MEM_REF:
2478       if (TMR_SYMBOL (exp) && !DECL_RTL_SET_P (TMR_SYMBOL (exp)))
2479         return NULL;
2480
2481       op0 = expand_debug_expr
2482             (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
2483       if (!op0)
2484         return NULL;
2485
2486       as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
2487
2488       op0 = gen_rtx_MEM (mode, op0);
2489
2490       set_mem_attributes (op0, exp, 0);
2491       set_mem_addr_space (op0, as);
2492
2493       return op0;
2494
2495     case ARRAY_REF:
2496     case ARRAY_RANGE_REF:
2497     case COMPONENT_REF:
2498     case BIT_FIELD_REF:
2499     case REALPART_EXPR:
2500     case IMAGPART_EXPR:
2501     case VIEW_CONVERT_EXPR:
2502       {
2503         enum machine_mode mode1;
2504         HOST_WIDE_INT bitsize, bitpos;
2505         tree offset;
2506         int volatilep = 0;
2507         tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2508                                         &mode1, &unsignedp, &volatilep, false);
2509         rtx orig_op0;
2510
2511         if (bitsize == 0)
2512           return NULL;
2513
2514         orig_op0 = op0 = expand_debug_expr (tem);
2515
2516         if (!op0)
2517           return NULL;
2518
2519         if (offset)
2520           {
2521             enum machine_mode addrmode, offmode;
2522
2523             if (!MEM_P (op0))
2524               return NULL;
2525
2526             op0 = XEXP (op0, 0);
2527             addrmode = GET_MODE (op0);
2528             if (addrmode == VOIDmode)
2529               addrmode = Pmode;
2530
2531             op1 = expand_debug_expr (offset);
2532             if (!op1)
2533               return NULL;
2534
2535             offmode = GET_MODE (op1);
2536             if (offmode == VOIDmode)
2537               offmode = TYPE_MODE (TREE_TYPE (offset));
2538
2539             if (addrmode != offmode)
2540               op1 = simplify_gen_subreg (addrmode, op1, offmode,
2541                                          subreg_lowpart_offset (addrmode,
2542                                                                 offmode));
2543
2544             /* Don't use offset_address here, we don't need a
2545                recognizable address, and we don't want to generate
2546                code.  */
2547             op0 = gen_rtx_MEM (mode, gen_rtx_PLUS (addrmode, op0, op1));
2548           }
2549
2550         if (MEM_P (op0))
2551           {
2552             if (mode1 == VOIDmode)
2553               /* Bitfield.  */
2554               mode1 = smallest_mode_for_size (bitsize, MODE_INT);
2555             if (bitpos >= BITS_PER_UNIT)
2556               {
2557                 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
2558                 bitpos %= BITS_PER_UNIT;
2559               }
2560             else if (bitpos < 0)
2561               {
2562                 HOST_WIDE_INT units
2563                   = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
2564                 op0 = adjust_address_nv (op0, mode1, units);
2565                 bitpos += units * BITS_PER_UNIT;
2566               }
2567             else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
2568               op0 = adjust_address_nv (op0, mode, 0);
2569             else if (GET_MODE (op0) != mode1)
2570               op0 = adjust_address_nv (op0, mode1, 0);
2571             else
2572               op0 = copy_rtx (op0);
2573             if (op0 == orig_op0)
2574               op0 = shallow_copy_rtx (op0);
2575             set_mem_attributes (op0, exp, 0);
2576           }
2577
2578         if (bitpos == 0 && mode == GET_MODE (op0))
2579           return op0;
2580
2581         if (bitpos < 0)
2582           return NULL;
2583
2584         if (GET_MODE (op0) == BLKmode)
2585           return NULL;
2586
2587         if ((bitpos % BITS_PER_UNIT) == 0
2588             && bitsize == GET_MODE_BITSIZE (mode1))
2589           {
2590             enum machine_mode opmode = GET_MODE (op0);
2591
2592             if (opmode == VOIDmode)
2593               opmode = mode1;
2594
2595             /* This condition may hold if we're expanding the address
2596                right past the end of an array that turned out not to
2597                be addressable (i.e., the address was only computed in
2598                debug stmts).  The gen_subreg below would rightfully
2599                crash, and the address doesn't really exist, so just
2600                drop it.  */
2601             if (bitpos >= GET_MODE_BITSIZE (opmode))
2602               return NULL;
2603
2604             if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
2605               return simplify_gen_subreg (mode, op0, opmode,
2606                                           bitpos / BITS_PER_UNIT);
2607           }
2608
2609         return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
2610                                      && TYPE_UNSIGNED (TREE_TYPE (exp))
2611                                      ? SIGN_EXTRACT
2612                                      : ZERO_EXTRACT, mode,
2613                                      GET_MODE (op0) != VOIDmode
2614                                      ? GET_MODE (op0) : mode1,
2615                                      op0, GEN_INT (bitsize), GEN_INT (bitpos));
2616       }
2617
2618     case ABS_EXPR:
2619       return gen_rtx_ABS (mode, op0);
2620
2621     case NEGATE_EXPR:
2622       return gen_rtx_NEG (mode, op0);
2623
2624     case BIT_NOT_EXPR:
2625       return gen_rtx_NOT (mode, op0);
2626
2627     case FLOAT_EXPR:
2628       if (unsignedp)
2629         return gen_rtx_UNSIGNED_FLOAT (mode, op0);
2630       else
2631         return gen_rtx_FLOAT (mode, op0);
2632
2633     case FIX_TRUNC_EXPR:
2634       if (unsignedp)
2635         return gen_rtx_UNSIGNED_FIX (mode, op0);
2636       else
2637         return gen_rtx_FIX (mode, op0);
2638
2639     case POINTER_PLUS_EXPR:
2640       /* For the rare target where pointers are not the same size as
2641          size_t, we need to check for mis-matched modes and correct
2642          the addend.  */
2643       if (op0 && op1
2644           && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
2645           && GET_MODE (op0) != GET_MODE (op1))
2646         {
2647           if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1)))
2648             op1 = gen_rtx_TRUNCATE (GET_MODE (op0), op1);
2649           else
2650             /* We always sign-extend, regardless of the signedness of
2651                the operand, because the operand is always unsigned
2652                here even if the original C expression is signed.  */
2653             op1 = gen_rtx_SIGN_EXTEND (GET_MODE (op0), op1);
2654         }
2655       /* Fall through.  */
2656     case PLUS_EXPR:
2657       return gen_rtx_PLUS (mode, op0, op1);
2658
2659     case MINUS_EXPR:
2660       return gen_rtx_MINUS (mode, op0, op1);
2661
2662     case MULT_EXPR:
2663       return gen_rtx_MULT (mode, op0, op1);
2664
2665     case RDIV_EXPR:
2666     case TRUNC_DIV_EXPR:
2667     case EXACT_DIV_EXPR:
2668       if (unsignedp)
2669         return gen_rtx_UDIV (mode, op0, op1);
2670       else
2671         return gen_rtx_DIV (mode, op0, op1);
2672
2673     case TRUNC_MOD_EXPR:
2674       if (unsignedp)
2675         return gen_rtx_UMOD (mode, op0, op1);
2676       else
2677         return gen_rtx_MOD (mode, op0, op1);
2678
2679     case FLOOR_DIV_EXPR:
2680       if (unsignedp)
2681         return gen_rtx_UDIV (mode, op0, op1);
2682       else
2683         {
2684           rtx div = gen_rtx_DIV (mode, op0, op1);
2685           rtx mod = gen_rtx_MOD (mode, op0, op1);
2686           rtx adj = floor_sdiv_adjust (mode, mod, op1);
2687           return gen_rtx_PLUS (mode, div, adj);
2688         }
2689
2690     case FLOOR_MOD_EXPR:
2691       if (unsignedp)
2692         return gen_rtx_UMOD (mode, op0, op1);
2693       else
2694         {
2695           rtx mod = gen_rtx_MOD (mode, op0, op1);
2696           rtx adj = floor_sdiv_adjust (mode, mod, op1);
2697           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2698           return gen_rtx_PLUS (mode, mod, adj);
2699         }
2700
2701     case CEIL_DIV_EXPR:
2702       if (unsignedp)
2703         {
2704           rtx div = gen_rtx_UDIV (mode, op0, op1);
2705           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2706           rtx adj = ceil_udiv_adjust (mode, mod, op1);
2707           return gen_rtx_PLUS (mode, div, adj);
2708         }
2709       else
2710         {
2711           rtx div = gen_rtx_DIV (mode, op0, op1);
2712           rtx mod = gen_rtx_MOD (mode, op0, op1);
2713           rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2714           return gen_rtx_PLUS (mode, div, adj);
2715         }
2716
2717     case CEIL_MOD_EXPR:
2718       if (unsignedp)
2719         {
2720           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2721           rtx adj = ceil_udiv_adjust (mode, mod, op1);
2722           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2723           return gen_rtx_PLUS (mode, mod, adj);
2724         }
2725       else
2726         {
2727           rtx mod = gen_rtx_MOD (mode, op0, op1);
2728           rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2729           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2730           return gen_rtx_PLUS (mode, mod, adj);
2731         }
2732
2733     case ROUND_DIV_EXPR:
2734       if (unsignedp)
2735         {
2736           rtx div = gen_rtx_UDIV (mode, op0, op1);
2737           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2738           rtx adj = round_udiv_adjust (mode, mod, op1);
2739           return gen_rtx_PLUS (mode, div, adj);
2740         }
2741       else
2742         {
2743           rtx div = gen_rtx_DIV (mode, op0, op1);
2744           rtx mod = gen_rtx_MOD (mode, op0, op1);
2745           rtx adj = round_sdiv_adjust (mode, mod, op1);
2746           return gen_rtx_PLUS (mode, div, adj);
2747         }
2748
2749     case ROUND_MOD_EXPR:
2750       if (unsignedp)
2751         {
2752           rtx mod = gen_rtx_UMOD (mode, op0, op1);
2753           rtx adj = round_udiv_adjust (mode, mod, op1);
2754           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2755           return gen_rtx_PLUS (mode, mod, adj);
2756         }
2757       else
2758         {
2759           rtx mod = gen_rtx_MOD (mode, op0, op1);
2760           rtx adj = round_sdiv_adjust (mode, mod, op1);
2761           adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2762           return gen_rtx_PLUS (mode, mod, adj);
2763         }
2764
2765     case LSHIFT_EXPR:
2766       return gen_rtx_ASHIFT (mode, op0, op1);
2767
2768     case RSHIFT_EXPR:
2769       if (unsignedp)
2770         return gen_rtx_LSHIFTRT (mode, op0, op1);
2771       else
2772         return gen_rtx_ASHIFTRT (mode, op0, op1);
2773
2774     case LROTATE_EXPR:
2775       return gen_rtx_ROTATE (mode, op0, op1);
2776
2777     case RROTATE_EXPR:
2778       return gen_rtx_ROTATERT (mode, op0, op1);
2779
2780     case MIN_EXPR:
2781       if (unsignedp)
2782         return gen_rtx_UMIN (mode, op0, op1);
2783       else
2784         return gen_rtx_SMIN (mode, op0, op1);
2785
2786     case MAX_EXPR:
2787       if (unsignedp)
2788         return gen_rtx_UMAX (mode, op0, op1);
2789       else
2790         return gen_rtx_SMAX (mode, op0, op1);
2791
2792     case BIT_AND_EXPR:
2793     case TRUTH_AND_EXPR:
2794       return gen_rtx_AND (mode, op0, op1);
2795
2796     case BIT_IOR_EXPR:
2797     case TRUTH_OR_EXPR:
2798       return gen_rtx_IOR (mode, op0, op1);
2799
2800     case BIT_XOR_EXPR:
2801     case TRUTH_XOR_EXPR:
2802       return gen_rtx_XOR (mode, op0, op1);
2803
2804     case TRUTH_ANDIF_EXPR:
2805       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
2806
2807     case TRUTH_ORIF_EXPR:
2808       return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
2809
2810     case TRUTH_NOT_EXPR:
2811       return gen_rtx_EQ (mode, op0, const0_rtx);
2812
2813     case LT_EXPR:
2814       if (unsignedp)
2815         return gen_rtx_LTU (mode, op0, op1);
2816       else
2817         return gen_rtx_LT (mode, op0, op1);
2818
2819     case LE_EXPR:
2820       if (unsignedp)
2821         return gen_rtx_LEU (mode, op0, op1);
2822       else
2823         return gen_rtx_LE (mode, op0, op1);
2824
2825     case GT_EXPR:
2826       if (unsignedp)
2827         return gen_rtx_GTU (mode, op0, op1);
2828       else
2829         return gen_rtx_GT (mode, op0, op1);
2830
2831     case GE_EXPR:
2832       if (unsignedp)
2833         return gen_rtx_GEU (mode, op0, op1);
2834       else
2835         return gen_rtx_GE (mode, op0, op1);
2836
2837     case EQ_EXPR:
2838       return gen_rtx_EQ (mode, op0, op1);
2839
2840     case NE_EXPR:
2841       return gen_rtx_NE (mode, op0, op1);
2842
2843     case UNORDERED_EXPR:
2844       return gen_rtx_UNORDERED (mode, op0, op1);
2845
2846     case ORDERED_EXPR:
2847       return gen_rtx_ORDERED (mode, op0, op1);
2848
2849     case UNLT_EXPR:
2850       return gen_rtx_UNLT (mode, op0, op1);
2851
2852     case UNLE_EXPR:
2853       return gen_rtx_UNLE (mode, op0, op1);
2854
2855     case UNGT_EXPR:
2856       return gen_rtx_UNGT (mode, op0, op1);
2857
2858     case UNGE_EXPR:
2859       return gen_rtx_UNGE (mode, op0, op1);
2860
2861     case UNEQ_EXPR:
2862       return gen_rtx_UNEQ (mode, op0, op1);
2863
2864     case LTGT_EXPR:
2865       return gen_rtx_LTGT (mode, op0, op1);
2866
2867     case COND_EXPR:
2868       return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
2869
2870     case COMPLEX_EXPR:
2871       gcc_assert (COMPLEX_MODE_P (mode));
2872       if (GET_MODE (op0) == VOIDmode)
2873         op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
2874       if (GET_MODE (op1) == VOIDmode)
2875         op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
2876       return gen_rtx_CONCAT (mode, op0, op1);
2877
2878     case CONJ_EXPR:
2879       if (GET_CODE (op0) == CONCAT)
2880         return gen_rtx_CONCAT (mode, XEXP (op0, 0),
2881                                gen_rtx_NEG (GET_MODE_INNER (mode),
2882                                             XEXP (op0, 1)));
2883       else
2884         {
2885           enum machine_mode imode = GET_MODE_INNER (mode);
2886           rtx re, im;
2887
2888           if (MEM_P (op0))
2889             {
2890               re = adjust_address_nv (op0, imode, 0);
2891               im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
2892             }
2893           else
2894             {
2895               enum machine_mode ifmode = int_mode_for_mode (mode);
2896               enum machine_mode ihmode = int_mode_for_mode (imode);
2897               rtx halfsize;
2898               if (ifmode == BLKmode || ihmode == BLKmode)
2899                 return NULL;
2900               halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
2901               re = op0;
2902               if (mode != ifmode)
2903                 re = gen_rtx_SUBREG (ifmode, re, 0);
2904               re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
2905               if (imode != ihmode)
2906                 re = gen_rtx_SUBREG (imode, re, 0);
2907               im = copy_rtx (op0);
2908               if (mode != ifmode)
2909                 im = gen_rtx_SUBREG (ifmode, im, 0);
2910               im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
2911               if (imode != ihmode)
2912                 im = gen_rtx_SUBREG (imode, im, 0);
2913             }
2914           im = gen_rtx_NEG (imode, im);
2915           return gen_rtx_CONCAT (mode, re, im);
2916         }
2917
2918     case ADDR_EXPR:
2919       op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2920       if (!op0 || !MEM_P (op0))
2921         return NULL;
2922
2923       op0 = convert_debug_memory_address (mode, XEXP (op0, 0));
2924
2925       return op0;
2926
2927     case VECTOR_CST:
2928       exp = build_constructor_from_list (TREE_TYPE (exp),
2929                                          TREE_VECTOR_CST_ELTS (exp));
2930       /* Fall through.  */
2931
2932     case CONSTRUCTOR:
2933       if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
2934         {
2935           unsigned i;
2936           tree val;
2937
2938           op0 = gen_rtx_CONCATN
2939             (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
2940
2941           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
2942             {
2943               op1 = expand_debug_expr (val);
2944               if (!op1)
2945                 return NULL;
2946               XVECEXP (op0, 0, i) = op1;
2947             }
2948
2949           if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
2950             {
2951               op1 = expand_debug_expr
2952                 (fold_convert (TREE_TYPE (TREE_TYPE (exp)), integer_zero_node));
2953
2954               if (!op1)
2955                 return NULL;
2956
2957               for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
2958                 XVECEXP (op0, 0, i) = op1;
2959             }
2960
2961           return op0;
2962         }
2963       else
2964         goto flag_unsupported;
2965
2966     case CALL_EXPR:
2967       /* ??? Maybe handle some builtins?  */
2968       return NULL;
2969
2970     case SSA_NAME:
2971       {
2972         gimple g = get_gimple_for_ssa_name (exp);
2973         if (g)
2974           {
2975             op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
2976             if (!op0)
2977               return NULL;
2978           }
2979         else
2980           {
2981             int part = var_to_partition (SA.map, exp);
2982
2983             if (part == NO_PARTITION)
2984               return NULL;
2985
2986             gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
2987
2988             op0 = SA.partition_to_pseudo[part];
2989           }
2990         goto adjust_mode;
2991       }
2992
2993     case ERROR_MARK:
2994       return NULL;
2995
2996     /* Vector stuff.  For most of the codes we don't have rtl codes.  */
2997     case REALIGN_LOAD_EXPR:
2998     case REDUC_MAX_EXPR:
2999     case REDUC_MIN_EXPR:
3000     case REDUC_PLUS_EXPR:
3001     case VEC_COND_EXPR:
3002     case VEC_EXTRACT_EVEN_EXPR:
3003     case VEC_EXTRACT_ODD_EXPR:
3004     case VEC_INTERLEAVE_HIGH_EXPR:
3005     case VEC_INTERLEAVE_LOW_EXPR:
3006     case VEC_LSHIFT_EXPR:
3007     case VEC_PACK_FIX_TRUNC_EXPR:
3008     case VEC_PACK_SAT_EXPR:
3009     case VEC_PACK_TRUNC_EXPR:
3010     case VEC_RSHIFT_EXPR:
3011     case VEC_UNPACK_FLOAT_HI_EXPR:
3012     case VEC_UNPACK_FLOAT_LO_EXPR:
3013     case VEC_UNPACK_HI_EXPR:
3014     case VEC_UNPACK_LO_EXPR:
3015     case VEC_WIDEN_MULT_HI_EXPR:
3016     case VEC_WIDEN_MULT_LO_EXPR:
3017       return NULL;
3018
3019    /* Misc codes.  */
3020     case ADDR_SPACE_CONVERT_EXPR:
3021     case FIXED_CONVERT_EXPR:
3022     case OBJ_TYPE_REF:
3023     case WITH_SIZE_EXPR:
3024       return NULL;
3025
3026     case DOT_PROD_EXPR:
3027       if (SCALAR_INT_MODE_P (GET_MODE (op0))
3028           && SCALAR_INT_MODE_P (mode))
3029         {
3030           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3031             op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3032           else
3033             op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3034           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3035             op1 = gen_rtx_ZERO_EXTEND (mode, op1);
3036           else
3037             op1 = gen_rtx_SIGN_EXTEND (mode, op1);
3038           op0 = gen_rtx_MULT (mode, op0, op1);
3039           return gen_rtx_PLUS (mode, op0, op2);
3040         }
3041       return NULL;
3042
3043     case WIDEN_MULT_EXPR:
3044     case WIDEN_MULT_PLUS_EXPR:
3045     case WIDEN_MULT_MINUS_EXPR:
3046       if (SCALAR_INT_MODE_P (GET_MODE (op0))
3047           && SCALAR_INT_MODE_P (mode))
3048         {
3049           enum machine_mode inner_mode = GET_MODE (op0);
3050           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3051             op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3052           else
3053             op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3054           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3055             op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
3056           else
3057             op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
3058           op0 = gen_rtx_MULT (mode, op0, op1);
3059           if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
3060             return op0;
3061           else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
3062             return gen_rtx_PLUS (mode, op0, op2);
3063           else
3064             return gen_rtx_MINUS (mode, op2, op0);
3065         }
3066       return NULL;
3067
3068     case WIDEN_SUM_EXPR:
3069       if (SCALAR_INT_MODE_P (GET_MODE (op0))
3070           && SCALAR_INT_MODE_P (mode))
3071         {
3072           if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3073             op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3074           else
3075             op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3076           return gen_rtx_PLUS (mode, op0, op1);
3077         }
3078       return NULL;
3079
3080     default:
3081     flag_unsupported:
3082 #ifdef ENABLE_CHECKING
3083       debug_tree (exp);
3084       gcc_unreachable ();
3085 #else
3086       return NULL;
3087 #endif
3088     }
3089 }
3090
3091 /* Expand the _LOCs in debug insns.  We run this after expanding all
3092    regular insns, so that any variables referenced in the function
3093    will have their DECL_RTLs set.  */
3094
3095 static void
3096 expand_debug_locations (void)
3097 {
3098   rtx insn;
3099   rtx last = get_last_insn ();
3100   int save_strict_alias = flag_strict_aliasing;
3101
3102   /* New alias sets while setting up memory attributes cause
3103      -fcompare-debug failures, even though it doesn't bring about any
3104      codegen changes.  */
3105   flag_strict_aliasing = 0;
3106
3107   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3108     if (DEBUG_INSN_P (insn))
3109       {
3110         tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
3111         rtx val;
3112         enum machine_mode mode;
3113
3114         if (value == NULL_TREE)
3115           val = NULL_RTX;
3116         else
3117           {
3118             val = expand_debug_expr (value);
3119             gcc_assert (last == get_last_insn ());
3120           }
3121
3122         if (!val)
3123           val = gen_rtx_UNKNOWN_VAR_LOC ();
3124         else
3125           {
3126             mode = GET_MODE (INSN_VAR_LOCATION (insn));
3127
3128             gcc_assert (mode == GET_MODE (val)
3129                         || (GET_MODE (val) == VOIDmode
3130                             && (CONST_INT_P (val)
3131                                 || GET_CODE (val) == CONST_FIXED
3132                                 || GET_CODE (val) == CONST_DOUBLE
3133                                 || GET_CODE (val) == LABEL_REF)));
3134           }
3135
3136         INSN_VAR_LOCATION_LOC (insn) = val;
3137       }
3138
3139   flag_strict_aliasing = save_strict_alias;
3140 }
3141
3142 /* Expand basic block BB from GIMPLE trees to RTL.  */
3143
3144 static basic_block
3145 expand_gimple_basic_block (basic_block bb)
3146 {
3147   gimple_stmt_iterator gsi;
3148   gimple_seq stmts;
3149   gimple stmt = NULL;
3150   rtx note, last;
3151   edge e;
3152   edge_iterator ei;
3153   void **elt;
3154
3155   if (dump_file)
3156     fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
3157              bb->index);
3158
3159   /* Note that since we are now transitioning from GIMPLE to RTL, we
3160      cannot use the gsi_*_bb() routines because they expect the basic
3161      block to be in GIMPLE, instead of RTL.  Therefore, we need to
3162      access the BB sequence directly.  */
3163   stmts = bb_seq (bb);
3164   bb->il.gimple = NULL;
3165   rtl_profile_for_bb (bb);
3166   init_rtl_bb_info (bb);
3167   bb->flags |= BB_RTL;
3168
3169   /* Remove the RETURN_EXPR if we may fall though to the exit
3170      instead.  */
3171   gsi = gsi_last (stmts);
3172   if (!gsi_end_p (gsi)
3173       && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
3174     {
3175       gimple ret_stmt = gsi_stmt (gsi);
3176
3177       gcc_assert (single_succ_p (bb));
3178       gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
3179
3180       if (bb->next_bb == EXIT_BLOCK_PTR
3181           && !gimple_return_retval (ret_stmt))
3182         {
3183           gsi_remove (&gsi, false);
3184           single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3185         }
3186     }
3187
3188   gsi = gsi_start (stmts);
3189   if (!gsi_end_p (gsi))
3190     {
3191       stmt = gsi_stmt (gsi);
3192       if (gimple_code (stmt) != GIMPLE_LABEL)
3193         stmt = NULL;
3194     }
3195
3196   elt = pointer_map_contains (lab_rtx_for_bb, bb);
3197
3198   if (stmt || elt)
3199     {
3200       last = get_last_insn ();
3201
3202       if (stmt)
3203         {
3204           expand_gimple_stmt (stmt);
3205           gsi_next (&gsi);
3206         }
3207
3208       if (elt)
3209         emit_label ((rtx) *elt);
3210
3211       /* Java emits line number notes in the top of labels.
3212          ??? Make this go away once line number notes are obsoleted.  */
3213       BB_HEAD (bb) = NEXT_INSN (last);
3214       if (NOTE_P (BB_HEAD (bb)))
3215         BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
3216       note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
3217
3218       maybe_dump_rtl_for_gimple_stmt (stmt, last);
3219     }
3220   else
3221     note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
3222
3223   NOTE_BASIC_BLOCK (note) = bb;
3224
3225   for (; !gsi_end_p (gsi); gsi_next (&gsi))
3226     {
3227       basic_block new_bb;
3228
3229       stmt = gsi_stmt (gsi);
3230
3231       /* If this statement is a non-debug one, and we generate debug
3232          insns, then this one might be the last real use of a TERed
3233          SSA_NAME, but where there are still some debug uses further
3234          down.  Expanding the current SSA name in such further debug
3235          uses by their RHS might lead to wrong debug info, as coalescing
3236          might make the operands of such RHS be placed into the same
3237          pseudo as something else.  Like so:
3238            a_1 = a_0 + 1;   // Assume a_1 is TERed and a_0 is dead
3239            use(a_1);
3240            a_2 = ...
3241            #DEBUG ... => a_1
3242          As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
3243          If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
3244          the write to a_2 would actually have clobbered the place which
3245          formerly held a_0.
3246
3247          So, instead of that, we recognize the situation, and generate
3248          debug temporaries at the last real use of TERed SSA names:
3249            a_1 = a_0 + 1;
3250            #DEBUG #D1 => a_1
3251            use(a_1);
3252            a_2 = ...
3253            #DEBUG ... => #D1
3254          */
3255       if (MAY_HAVE_DEBUG_INSNS
3256           && SA.values
3257           && !is_gimple_debug (stmt))
3258         {
3259           ssa_op_iter iter;
3260           tree op;
3261           gimple def;
3262
3263           location_t sloc = get_curr_insn_source_location ();
3264           tree sblock = get_curr_insn_block ();
3265
3266           /* Look for SSA names that have their last use here (TERed
3267              names always have only one real use).  */
3268           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3269             if ((def = get_gimple_for_ssa_name (op)))
3270               {
3271                 imm_use_iterator imm_iter;
3272                 use_operand_p use_p;
3273                 bool have_debug_uses = false;
3274
3275                 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
3276                   {
3277                     if (gimple_debug_bind_p (USE_STMT (use_p)))
3278                       {
3279                         have_debug_uses = true;
3280                         break;
3281                       }
3282                   }
3283
3284                 if (have_debug_uses)
3285                   {
3286                     /* OP is a TERed SSA name, with DEF it's defining
3287                        statement, and where OP is used in further debug
3288                        instructions.  Generate a debug temporary, and
3289                        replace all uses of OP in debug insns with that
3290                        temporary.  */
3291                     gimple debugstmt;
3292                     tree value = gimple_assign_rhs_to_tree (def);
3293                     tree vexpr = make_node (DEBUG_EXPR_DECL);
3294                     rtx val;
3295                     enum machine_mode mode;
3296
3297                     set_curr_insn_source_location (gimple_location (def));
3298                     set_curr_insn_block (gimple_block (def));
3299
3300                     DECL_ARTIFICIAL (vexpr) = 1;
3301                     TREE_TYPE (vexpr) = TREE_TYPE (value);
3302                     if (DECL_P (value))
3303                       mode = DECL_MODE (value);
3304                     else
3305                       mode = TYPE_MODE (TREE_TYPE (value));
3306                     DECL_MODE (vexpr) = mode;
3307
3308                     val = gen_rtx_VAR_LOCATION
3309                         (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3310
3311                     val = emit_debug_insn (val);
3312
3313                     FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
3314                       {
3315                         if (!gimple_debug_bind_p (debugstmt))
3316                           continue;
3317
3318                         FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3319                           SET_USE (use_p, vexpr);
3320
3321                         update_stmt (debugstmt);
3322                       }
3323                   }
3324               }
3325           set_curr_insn_source_location (sloc);
3326           set_curr_insn_block (sblock);
3327         }
3328
3329       currently_expanding_gimple_stmt = stmt;
3330
3331       /* Expand this statement, then evaluate the resulting RTL and
3332          fixup the CFG accordingly.  */
3333       if (gimple_code (stmt) == GIMPLE_COND)
3334         {
3335           new_bb = expand_gimple_cond (bb, stmt);
3336           if (new_bb)
3337             return new_bb;
3338         }
3339       else if (gimple_debug_bind_p (stmt))
3340         {
3341           location_t sloc = get_curr_insn_source_location ();
3342           tree sblock = get_curr_insn_block ();
3343           gimple_stmt_iterator nsi = gsi;
3344
3345           for (;;)
3346             {
3347               tree var = gimple_debug_bind_get_var (stmt);
3348               tree value;
3349               rtx val;
3350               enum machine_mode mode;
3351
3352               if (gimple_debug_bind_has_value_p (stmt))
3353                 value = gimple_debug_bind_get_value (stmt);
3354               else
3355                 value = NULL_TREE;
3356
3357               last = get_last_insn ();
3358
3359               set_curr_insn_source_location (gimple_location (stmt));
3360               set_curr_insn_block (gimple_block (stmt));
3361
3362               if (DECL_P (var))
3363                 mode = DECL_MODE (var);
3364               else
3365                 mode = TYPE_MODE (TREE_TYPE (var));
3366
3367               val = gen_rtx_VAR_LOCATION
3368                 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3369
3370               val = emit_debug_insn (val);
3371
3372               if (dump_file && (dump_flags & TDF_DETAILS))
3373                 {
3374                   /* We can't dump the insn with a TREE where an RTX
3375                      is expected.  */
3376                   INSN_VAR_LOCATION_LOC (val) = const0_rtx;
3377                   maybe_dump_rtl_for_gimple_stmt (stmt, last);
3378                   INSN_VAR_LOCATION_LOC (val) = (rtx)value;
3379                 }
3380
3381               /* In order not to generate too many debug temporaries,
3382                  we delink all uses of debug statements we already expanded.
3383                  Therefore debug statements between definition and real
3384                  use of TERed SSA names will continue to use the SSA name,
3385                  and not be replaced with debug temps.  */
3386               delink_stmt_imm_use (stmt);
3387
3388               gsi = nsi;
3389               gsi_next (&nsi);
3390               if (gsi_end_p (nsi))
3391                 break;
3392               stmt = gsi_stmt (nsi);
3393               if (!gimple_debug_bind_p (stmt))
3394                 break;
3395             }
3396
3397           set_curr_insn_source_location (sloc);
3398           set_curr_insn_block (sblock);
3399         }
3400       else
3401         {
3402           if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
3403             {
3404               bool can_fallthru;
3405               new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
3406               if (new_bb)
3407                 {
3408                   if (can_fallthru)
3409                     bb = new_bb;
3410                   else
3411                     return new_bb;
3412                 }
3413             }
3414           else
3415             {
3416               def_operand_p def_p;
3417               def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
3418
3419               if (def_p != NULL)
3420                 {
3421                   /* Ignore this stmt if it is in the list of
3422                      replaceable expressions.  */
3423                   if (SA.values
3424                       && bitmap_bit_p (SA.values,
3425                                        SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
3426                     continue;
3427                 }
3428               last = expand_gimple_stmt (stmt);
3429               maybe_dump_rtl_for_gimple_stmt (stmt, last);
3430             }
3431         }
3432     }
3433
3434   currently_expanding_gimple_stmt = NULL;
3435
3436   /* Expand implicit goto and convert goto_locus.  */
3437   FOR_EACH_EDGE (e, ei, bb->succs)
3438     {
3439       if (e->goto_locus && e->goto_block)
3440         {
3441           set_curr_insn_source_location (e->goto_locus);
3442           set_curr_insn_block (e->goto_block);
3443           e->goto_locus = curr_insn_locator ();
3444         }
3445       e->goto_block = NULL;
3446       if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
3447         {
3448           emit_jump (label_rtx_for_bb (e->dest));
3449           e->flags &= ~EDGE_FALLTHRU;
3450         }
3451     }
3452
3453   /* Expanded RTL can create a jump in the last instruction of block.
3454      This later might be assumed to be a jump to successor and break edge insertion.
3455      We need to insert dummy move to prevent this. PR41440. */
3456   if (single_succ_p (bb)
3457       && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
3458       && (last = get_last_insn ())
3459       && JUMP_P (last))
3460     {
3461       rtx dummy = gen_reg_rtx (SImode);
3462       emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
3463     }
3464
3465   do_pending_stack_adjust ();
3466
3467   /* Find the block tail.  The last insn in the block is the insn
3468      before a barrier and/or table jump insn.  */
3469   last = get_last_insn ();
3470   if (BARRIER_P (last))
3471     last = PREV_INSN (last);
3472   if (JUMP_TABLE_DATA_P (last))
3473     last = PREV_INSN (PREV_INSN (last));
3474   BB_END (bb) = last;
3475
3476   update_bb_for_insn (bb);
3477
3478   return bb;
3479 }
3480
3481
3482 /* Create a basic block for initialization code.  */
3483
3484 static basic_block
3485 construct_init_block (void)
3486 {
3487   basic_block init_block, first_block;
3488   edge e = NULL;
3489   int flags;
3490
3491   /* Multiple entry points not supported yet.  */
3492   gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
3493   init_rtl_bb_info (ENTRY_BLOCK_PTR);
3494   init_rtl_bb_info (EXIT_BLOCK_PTR);
3495   ENTRY_BLOCK_PTR->flags |= BB_RTL;
3496   EXIT_BLOCK_PTR->flags |= BB_RTL;
3497
3498   e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
3499
3500   /* When entry edge points to first basic block, we don't need jump,
3501      otherwise we have to jump into proper target.  */
3502   if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
3503     {
3504       tree label = gimple_block_label (e->dest);
3505
3506       emit_jump (label_rtx (label));
3507       flags = 0;
3508     }
3509   else
3510     flags = EDGE_FALLTHRU;
3511
3512   init_block = create_basic_block (NEXT_INSN (get_insns ()),
3513                                    get_last_insn (),
3514                                    ENTRY_BLOCK_PTR);
3515   init_block->frequency = ENTRY_BLOCK_PTR->frequency;
3516   init_block->count = ENTRY_BLOCK_PTR->count;
3517   if (e)
3518     {
3519       first_block = e->dest;
3520       redirect_edge_succ (e, init_block);
3521       e = make_edge (init_block, first_block, flags);
3522     }
3523   else
3524     e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3525   e->probability = REG_BR_PROB_BASE;
3526   e->count = ENTRY_BLOCK_PTR->count;
3527
3528   update_bb_for_insn (init_block);
3529   return init_block;
3530 }
3531
3532 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
3533    found in the block tree.  */
3534
3535 static void
3536 set_block_levels (tree block, int level)
3537 {
3538   while (block)
3539     {
3540       BLOCK_NUMBER (block) = level;
3541       set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
3542       block = BLOCK_CHAIN (block);
3543     }
3544 }
3545
3546 /* Create a block containing landing pads and similar stuff.  */
3547
3548 static void
3549 construct_exit_block (void)
3550 {
3551   rtx head = get_last_insn ();
3552   rtx end;
3553   basic_block exit_block;
3554   edge e, e2;
3555   unsigned ix;
3556   edge_iterator ei;
3557   rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
3558
3559   rtl_profile_for_bb (EXIT_BLOCK_PTR);
3560
3561   /* Make sure the locus is set to the end of the function, so that
3562      epilogue line numbers and warnings are set properly.  */
3563   if (cfun->function_end_locus != UNKNOWN_LOCATION)
3564     input_location = cfun->function_end_locus;
3565
3566   /* The following insns belong to the top scope.  */
3567   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3568
3569   /* Generate rtl for function exit.  */
3570   expand_function_end ();
3571
3572   end = get_last_insn ();
3573   if (head == end)
3574     return;
3575   /* While emitting the function end we could move end of the last basic block.
3576    */
3577   BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
3578   while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
3579     head = NEXT_INSN (head);
3580   exit_block = create_basic_block (NEXT_INSN (head), end,
3581                                    EXIT_BLOCK_PTR->prev_bb);
3582   exit_block->frequency = EXIT_BLOCK_PTR->frequency;
3583   exit_block->count = EXIT_BLOCK_PTR->count;
3584
3585   ix = 0;
3586   while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
3587     {
3588       e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
3589       if (!(e->flags & EDGE_ABNORMAL))
3590         redirect_edge_succ (e, exit_block);
3591       else
3592         ix++;
3593     }
3594
3595   e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3596   e->probability = REG_BR_PROB_BASE;
3597   e->count = EXIT_BLOCK_PTR->count;
3598   FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
3599     if (e2 != e)
3600       {
3601         e->count -= e2->count;
3602         exit_block->count -= e2->count;
3603         exit_block->frequency -= EDGE_FREQUENCY (e2);
3604       }
3605   if (e->count < 0)
3606     e->count = 0;
3607   if (exit_block->count < 0)
3608     exit_block->count = 0;
3609   if (exit_block->frequency < 0)
3610     exit_block->frequency = 0;
3611   update_bb_for_insn (exit_block);
3612 }
3613
3614 /* Helper function for discover_nonconstant_array_refs.
3615    Look for ARRAY_REF nodes with non-constant indexes and mark them
3616    addressable.  */
3617
3618 static tree
3619 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
3620                                    void *data ATTRIBUTE_UNUSED)
3621 {
3622   tree t = *tp;
3623
3624   if (IS_TYPE_OR_DECL_P (t))
3625     *walk_subtrees = 0;
3626   else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3627     {
3628       while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3629               && is_gimple_min_invariant (TREE_OPERAND (t, 1))
3630               && (!TREE_OPERAND (t, 2)
3631                   || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3632              || (TREE_CODE (t) == COMPONENT_REF
3633                  && (!TREE_OPERAND (t,2)
3634                      || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3635              || TREE_CODE (t) == BIT_FIELD_REF
3636              || TREE_CODE (t) == REALPART_EXPR
3637              || TREE_CODE (t) == IMAGPART_EXPR
3638              || TREE_CODE (t) == VIEW_CONVERT_EXPR
3639              || CONVERT_EXPR_P (t))
3640         t = TREE_OPERAND (t, 0);
3641
3642       if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3643         {
3644           t = get_base_address (t);
3645           if (t && DECL_P (t)
3646               && DECL_MODE (t) != BLKmode)
3647             TREE_ADDRESSABLE (t) = 1;
3648         }
3649
3650       *walk_subtrees = 0;
3651     }
3652
3653   return NULL_TREE;
3654 }
3655
3656 /* RTL expansion is not able to compile array references with variable
3657    offsets for arrays stored in single register.  Discover such
3658    expressions and mark variables as addressable to avoid this
3659    scenario.  */
3660
3661 static void
3662 discover_nonconstant_array_refs (void)
3663 {
3664   basic_block bb;
3665   gimple_stmt_iterator gsi;
3666
3667   FOR_EACH_BB (bb)
3668     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3669       {
3670         gimple stmt = gsi_stmt (gsi);
3671         if (!is_gimple_debug (stmt))
3672           walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
3673       }
3674 }
3675
3676 /* This function sets crtl->args.internal_arg_pointer to a virtual
3677    register if DRAP is needed.  Local register allocator will replace
3678    virtual_incoming_args_rtx with the virtual register.  */
3679
3680 static void
3681 expand_stack_alignment (void)
3682 {
3683   rtx drap_rtx;
3684   unsigned int preferred_stack_boundary;
3685
3686   if (! SUPPORTS_STACK_ALIGNMENT)
3687     return;
3688
3689   if (cfun->calls_alloca
3690       || cfun->has_nonlocal_label
3691       || crtl->has_nonlocal_goto)
3692     crtl->need_drap = true;
3693
3694   /* Call update_stack_boundary here again to update incoming stack
3695      boundary.  It may set incoming stack alignment to a different
3696      value after RTL expansion.  TARGET_FUNCTION_OK_FOR_SIBCALL may
3697      use the minimum incoming stack alignment to check if it is OK
3698      to perform sibcall optimization since sibcall optimization will
3699      only align the outgoing stack to incoming stack boundary.  */
3700   if (targetm.calls.update_stack_boundary)
3701     targetm.calls.update_stack_boundary ();
3702
3703   /* The incoming stack frame has to be aligned at least at
3704      parm_stack_boundary.  */
3705   gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
3706
3707   /* Update crtl->stack_alignment_estimated and use it later to align
3708      stack.  We check PREFERRED_STACK_BOUNDARY if there may be non-call
3709      exceptions since callgraph doesn't collect incoming stack alignment
3710      in this case.  */
3711   if (cfun->can_throw_non_call_exceptions
3712       && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
3713     preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3714   else
3715     preferred_stack_boundary = crtl->preferred_stack_boundary;
3716   if (preferred_stack_boundary > crtl->stack_alignment_estimated)
3717     crtl->stack_alignment_estimated = preferred_stack_boundary;
3718   if (preferred_stack_boundary > crtl->stack_alignment_needed)
3719     crtl->stack_alignment_needed = preferred_stack_boundary;
3720
3721   gcc_assert (crtl->stack_alignment_needed
3722               <= crtl->stack_alignment_estimated);
3723
3724   crtl->stack_realign_needed
3725     = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
3726   crtl->stack_realign_tried = crtl->stack_realign_needed;
3727
3728   crtl->stack_realign_processed = true;
3729
3730   /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
3731      alignment.  */
3732   gcc_assert (targetm.calls.get_drap_rtx != NULL);
3733   drap_rtx = targetm.calls.get_drap_rtx ();
3734
3735   /* stack_realign_drap and drap_rtx must match.  */
3736   gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
3737
3738   /* Do nothing if NULL is returned, which means DRAP is not needed.  */
3739   if (NULL != drap_rtx)
3740     {
3741       crtl->args.internal_arg_pointer = drap_rtx;
3742
3743       /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
3744          needed. */
3745       fixup_tail_calls ();
3746     }
3747 }
3748
3749 /* Translate the intermediate representation contained in the CFG
3750    from GIMPLE trees to RTL.
3751
3752    We do conversion per basic block and preserve/update the tree CFG.
3753    This implies we have to do some magic as the CFG can simultaneously
3754    consist of basic blocks containing RTL and GIMPLE trees.  This can
3755    confuse the CFG hooks, so be careful to not manipulate CFG during
3756    the expansion.  */
3757
3758 static unsigned int
3759 gimple_expand_cfg (void)
3760 {
3761   basic_block bb, init_block;
3762   sbitmap blocks;
3763   edge_iterator ei;
3764   edge e;
3765   unsigned i;
3766
3767   timevar_push (TV_OUT_OF_SSA);
3768   rewrite_out_of_ssa (&SA);
3769   timevar_pop (TV_OUT_OF_SSA);
3770   SA.partition_to_pseudo = (rtx *)xcalloc (SA.map->num_partitions,
3771                                            sizeof (rtx));
3772
3773   /* Some backends want to know that we are expanding to RTL.  */
3774   currently_expanding_to_rtl = 1;
3775
3776   rtl_profile_for_bb (ENTRY_BLOCK_PTR);
3777
3778   insn_locators_alloc ();
3779   if (!DECL_IS_BUILTIN (current_function_decl))
3780     {
3781       /* Eventually, all FEs should explicitly set function_start_locus.  */
3782       if (cfun->function_start_locus == UNKNOWN_LOCATION)
3783        set_curr_insn_source_location
3784          (DECL_SOURCE_LOCATION (current_function_decl));
3785       else
3786        set_curr_insn_source_location (cfun->function_start_locus);
3787     }
3788   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3789   prologue_locator = curr_insn_locator ();
3790
3791 #ifdef INSN_SCHEDULING
3792   init_sched_attrs ();
3793 #endif
3794
3795   /* Make sure first insn is a note even if we don't want linenums.
3796      This makes sure the first insn will never be deleted.
3797      Also, final expects a note to appear there.  */
3798   emit_note (NOTE_INSN_DELETED);
3799
3800   /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE.  */
3801   discover_nonconstant_array_refs ();
3802
3803   targetm.expand_to_rtl_hook ();
3804   crtl->stack_alignment_needed = STACK_BOUNDARY;
3805   crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
3806   crtl->stack_alignment_estimated = 0;
3807   crtl->preferred_stack_boundary = STACK_BOUNDARY;
3808   cfun->cfg->max_jumptable_ents = 0;
3809
3810
3811   /* Expand the variables recorded during gimple lowering.  */
3812   timevar_push (TV_VAR_EXPAND);
3813   expand_used_vars ();
3814   timevar_pop (TV_VAR_EXPAND);
3815
3816   /* Honor stack protection warnings.  */
3817   if (warn_stack_protect)
3818     {
3819       if (cfun->calls_alloca)
3820         warning (OPT_Wstack_protector,
3821                  "stack protector not protecting local variables: "
3822                  "variable length buffer");
3823       if (has_short_buffer && !crtl->stack_protect_guard)
3824         warning (OPT_Wstack_protector,
3825                  "stack protector not protecting function: "
3826                  "all local arrays are less than %d bytes long",
3827                  (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
3828     }
3829
3830   /* Set up parameters and prepare for return, for the function.  */
3831   expand_function_start (current_function_decl);
3832
3833   /* Now that we also have the parameter RTXs, copy them over to our
3834      partitions.  */
3835   for (i = 0; i < SA.map->num_partitions; i++)
3836     {
3837       tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
3838
3839       if (TREE_CODE (var) != VAR_DECL
3840           && !SA.partition_to_pseudo[i])
3841         SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
3842       gcc_assert (SA.partition_to_pseudo[i]);
3843
3844       /* If this decl was marked as living in multiple places, reset
3845          this now to NULL.  */
3846       if (DECL_RTL_IF_SET (var) == pc_rtx)
3847         SET_DECL_RTL (var, NULL);
3848
3849       /* Some RTL parts really want to look at DECL_RTL(x) when x
3850          was a decl marked in REG_ATTR or MEM_ATTR.  We could use
3851          SET_DECL_RTL here making this available, but that would mean
3852          to select one of the potentially many RTLs for one DECL.  Instead
3853          of doing that we simply reset the MEM_EXPR of the RTL in question,
3854          then nobody can get at it and hence nobody can call DECL_RTL on it.  */
3855       if (!DECL_RTL_SET_P (var))
3856         {
3857           if (MEM_P (SA.partition_to_pseudo[i]))
3858             set_mem_expr (SA.partition_to_pseudo[i], NULL);
3859         }
3860     }
3861
3862   /* If this function is `main', emit a call to `__main'
3863      to run global initializers, etc.  */
3864   if (DECL_NAME (current_function_decl)
3865       && MAIN_NAME_P (DECL_NAME (current_function_decl))
3866       && DECL_FILE_SCOPE_P (current_function_decl))
3867     expand_main_function ();
3868
3869   /* Initialize the stack_protect_guard field.  This must happen after the
3870      call to __main (if any) so that the external decl is initialized.  */
3871   if (crtl->stack_protect_guard)
3872     stack_protect_prologue ();
3873
3874   expand_phi_nodes (&SA);
3875
3876   /* Register rtl specific functions for cfg.  */
3877   rtl_register_cfg_hooks ();
3878
3879   init_block = construct_init_block ();
3880
3881   /* Clear EDGE_EXECUTABLE on the entry edge(s).  It is cleaned from the
3882      remaining edges later.  */
3883   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3884     e->flags &= ~EDGE_EXECUTABLE;
3885
3886   lab_rtx_for_bb = pointer_map_create ();
3887   FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
3888     bb = expand_gimple_basic_block (bb);
3889
3890   if (MAY_HAVE_DEBUG_INSNS)
3891     expand_debug_locations ();
3892
3893   execute_free_datastructures ();
3894   timevar_push (TV_OUT_OF_SSA);
3895   finish_out_of_ssa (&SA);
3896   timevar_pop (TV_OUT_OF_SSA);
3897
3898   timevar_push (TV_POST_EXPAND);
3899   /* We are no longer in SSA form.  */
3900   cfun->gimple_df->in_ssa_p = false;
3901
3902   /* Expansion is used by optimization passes too, set maybe_hot_insn_p
3903      conservatively to true until they are all profile aware.  */
3904   pointer_map_destroy (lab_rtx_for_bb);
3905   free_histograms ();
3906
3907   construct_exit_block ();
3908   set_curr_insn_block (DECL_INITIAL (current_function_decl));
3909   insn_locators_finalize ();
3910
3911   /* Zap the tree EH table.  */
3912   set_eh_throw_stmt_table (cfun, NULL);
3913
3914   rebuild_jump_labels (get_insns ());
3915
3916   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3917     {
3918       edge e;
3919       edge_iterator ei;
3920       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3921         {
3922           if (e->insns.r)
3923             commit_one_edge_insertion (e);
3924           else
3925             ei_next (&ei);
3926         }
3927     }
3928
3929   /* We're done expanding trees to RTL.  */
3930   currently_expanding_to_rtl = 0;
3931
3932   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
3933     {
3934       edge e;
3935       edge_iterator ei;
3936       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3937         {
3938           /* Clear EDGE_EXECUTABLE.  This flag is never used in the backend.  */
3939           e->flags &= ~EDGE_EXECUTABLE;
3940
3941           /* At the moment not all abnormal edges match the RTL
3942              representation.  It is safe to remove them here as
3943              find_many_sub_basic_blocks will rediscover them.
3944              In the future we should get this fixed properly.  */
3945           if ((e->flags & EDGE_ABNORMAL)
3946               && !(e->flags & EDGE_SIBCALL))
3947             remove_edge (e);
3948           else
3949             ei_next (&ei);
3950         }
3951     }
3952
3953   blocks = sbitmap_alloc (last_basic_block);
3954   sbitmap_ones (blocks);
3955   find_many_sub_basic_blocks (blocks);
3956   sbitmap_free (blocks);
3957   purge_all_dead_edges ();
3958
3959   compact_blocks ();
3960
3961   expand_stack_alignment ();
3962
3963 #ifdef ENABLE_CHECKING
3964   verify_flow_info ();
3965 #endif
3966
3967   /* There's no need to defer outputting this function any more; we
3968      know we want to output it.  */
3969   DECL_DEFER_OUTPUT (current_function_decl) = 0;
3970
3971   /* Now that we're done expanding trees to RTL, we shouldn't have any
3972      more CONCATs anywhere.  */
3973   generating_concat_p = 0;
3974
3975   if (dump_file)
3976     {
3977       fprintf (dump_file,
3978                "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
3979       /* And the pass manager will dump RTL for us.  */
3980     }
3981
3982   /* If we're emitting a nested function, make sure its parent gets
3983      emitted as well.  Doing otherwise confuses debug info.  */
3984   {
3985     tree parent;
3986     for (parent = DECL_CONTEXT (current_function_decl);
3987          parent != NULL_TREE;
3988          parent = get_containing_scope (parent))
3989       if (TREE_CODE (parent) == FUNCTION_DECL)
3990         TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
3991   }
3992
3993   /* We are now committed to emitting code for this function.  Do any
3994      preparation, such as emitting abstract debug info for the inline
3995      before it gets mangled by optimization.  */
3996   if (cgraph_function_possibly_inlined_p (current_function_decl))
3997     (*debug_hooks->outlining_inline_function) (current_function_decl);
3998
3999   TREE_ASM_WRITTEN (current_function_decl) = 1;
4000
4001   /* After expanding, the return labels are no longer needed. */
4002   return_label = NULL;
4003   naked_return_label = NULL;
4004   /* Tag the blocks with a depth number so that change_scope can find
4005      the common parent easily.  */
4006   set_block_levels (DECL_INITIAL (cfun->decl), 0);
4007   default_rtl_profile ();
4008   timevar_pop (TV_POST_EXPAND);
4009   return 0;
4010 }
4011
4012 struct rtl_opt_pass pass_expand =
4013 {
4014  {
4015   RTL_PASS,
4016   "expand",                             /* name */
4017   NULL,                                 /* gate */
4018   gimple_expand_cfg,                    /* execute */
4019   NULL,                                 /* sub */
4020   NULL,                                 /* next */
4021   0,                                    /* static_pass_number */
4022   TV_EXPAND,                            /* tv_id */
4023   PROP_ssa | PROP_gimple_leh | PROP_cfg
4024     | PROP_gimple_lcx,                  /* properties_required */
4025   PROP_rtl,                             /* properties_provided */
4026   PROP_ssa | PROP_trees,                /* properties_destroyed */
4027   TODO_verify_ssa | TODO_verify_flow
4028     | TODO_verify_stmts,                /* todo_flags_start */
4029   TODO_dump_func
4030   | TODO_ggc_collect                    /* todo_flags_finish */
4031  }
4032 };