intrinsic.h (gfc_check_selected_real_kind, [...]): Update prototypes.
[gcc.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 INDIRECT_REF:
2442 case ALIGN_INDIRECT_REF:
2443 case MISALIGNED_INDIRECT_REF:
2444 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2445 if (!op0)
2446 return NULL;
2447
2448 if (POINTER_TYPE_P (TREE_TYPE (exp)))
2449 {
2450 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
2451 address_mode = targetm.addr_space.address_mode (as);
2452 }
2453 else
2454 {
2455 as = ADDR_SPACE_GENERIC;
2456 address_mode = Pmode;
2457 }
2458
2459 if (TREE_CODE (exp) == ALIGN_INDIRECT_REF)
2460 {
2461 int align = TYPE_ALIGN_UNIT (TREE_TYPE (exp));
2462 op0 = gen_rtx_AND (address_mode, op0, GEN_INT (-align));
2463 }
2464
2465 op0 = gen_rtx_MEM (mode, op0);
2466
2467 set_mem_attributes (op0, exp, 0);
2468 set_mem_addr_space (op0, as);
2469
2470 return op0;
2471
2472 case TARGET_MEM_REF:
2473 if (TMR_SYMBOL (exp) && !DECL_RTL_SET_P (TMR_SYMBOL (exp)))
2474 return NULL;
2475
2476 op0 = expand_debug_expr
2477 (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
2478 if (!op0)
2479 return NULL;
2480
2481 as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
2482
2483 op0 = gen_rtx_MEM (mode, op0);
2484
2485 set_mem_attributes (op0, exp, 0);
2486 set_mem_addr_space (op0, as);
2487
2488 return op0;
2489
2490 case ARRAY_REF:
2491 case ARRAY_RANGE_REF:
2492 case COMPONENT_REF:
2493 case BIT_FIELD_REF:
2494 case REALPART_EXPR:
2495 case IMAGPART_EXPR:
2496 case VIEW_CONVERT_EXPR:
2497 {
2498 enum machine_mode mode1;
2499 HOST_WIDE_INT bitsize, bitpos;
2500 tree offset;
2501 int volatilep = 0;
2502 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
2503 &mode1, &unsignedp, &volatilep, false);
2504 rtx orig_op0;
2505
2506 if (bitsize == 0)
2507 return NULL;
2508
2509 orig_op0 = op0 = expand_debug_expr (tem);
2510
2511 if (!op0)
2512 return NULL;
2513
2514 if (offset)
2515 {
2516 enum machine_mode addrmode, offmode;
2517
2518 if (!MEM_P (op0))
2519 return NULL;
2520
2521 op0 = XEXP (op0, 0);
2522 addrmode = GET_MODE (op0);
2523 if (addrmode == VOIDmode)
2524 addrmode = Pmode;
2525
2526 op1 = expand_debug_expr (offset);
2527 if (!op1)
2528 return NULL;
2529
2530 offmode = GET_MODE (op1);
2531 if (offmode == VOIDmode)
2532 offmode = TYPE_MODE (TREE_TYPE (offset));
2533
2534 if (addrmode != offmode)
2535 op1 = simplify_gen_subreg (addrmode, op1, offmode,
2536 subreg_lowpart_offset (addrmode,
2537 offmode));
2538
2539 /* Don't use offset_address here, we don't need a
2540 recognizable address, and we don't want to generate
2541 code. */
2542 op0 = gen_rtx_MEM (mode, gen_rtx_PLUS (addrmode, op0, op1));
2543 }
2544
2545 if (MEM_P (op0))
2546 {
2547 if (mode1 == VOIDmode)
2548 /* Bitfield. */
2549 mode1 = smallest_mode_for_size (bitsize, MODE_INT);
2550 if (bitpos >= BITS_PER_UNIT)
2551 {
2552 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
2553 bitpos %= BITS_PER_UNIT;
2554 }
2555 else if (bitpos < 0)
2556 {
2557 HOST_WIDE_INT units
2558 = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
2559 op0 = adjust_address_nv (op0, mode1, units);
2560 bitpos += units * BITS_PER_UNIT;
2561 }
2562 else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
2563 op0 = adjust_address_nv (op0, mode, 0);
2564 else if (GET_MODE (op0) != mode1)
2565 op0 = adjust_address_nv (op0, mode1, 0);
2566 else
2567 op0 = copy_rtx (op0);
2568 if (op0 == orig_op0)
2569 op0 = shallow_copy_rtx (op0);
2570 set_mem_attributes (op0, exp, 0);
2571 }
2572
2573 if (bitpos == 0 && mode == GET_MODE (op0))
2574 return op0;
2575
2576 if (bitpos < 0)
2577 return NULL;
2578
2579 if (GET_MODE (op0) == BLKmode)
2580 return NULL;
2581
2582 if ((bitpos % BITS_PER_UNIT) == 0
2583 && bitsize == GET_MODE_BITSIZE (mode1))
2584 {
2585 enum machine_mode opmode = GET_MODE (op0);
2586
2587 if (opmode == VOIDmode)
2588 opmode = mode1;
2589
2590 /* This condition may hold if we're expanding the address
2591 right past the end of an array that turned out not to
2592 be addressable (i.e., the address was only computed in
2593 debug stmts). The gen_subreg below would rightfully
2594 crash, and the address doesn't really exist, so just
2595 drop it. */
2596 if (bitpos >= GET_MODE_BITSIZE (opmode))
2597 return NULL;
2598
2599 if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
2600 return simplify_gen_subreg (mode, op0, opmode,
2601 bitpos / BITS_PER_UNIT);
2602 }
2603
2604 return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
2605 && TYPE_UNSIGNED (TREE_TYPE (exp))
2606 ? SIGN_EXTRACT
2607 : ZERO_EXTRACT, mode,
2608 GET_MODE (op0) != VOIDmode
2609 ? GET_MODE (op0) : mode1,
2610 op0, GEN_INT (bitsize), GEN_INT (bitpos));
2611 }
2612
2613 case ABS_EXPR:
2614 return gen_rtx_ABS (mode, op0);
2615
2616 case NEGATE_EXPR:
2617 return gen_rtx_NEG (mode, op0);
2618
2619 case BIT_NOT_EXPR:
2620 return gen_rtx_NOT (mode, op0);
2621
2622 case FLOAT_EXPR:
2623 if (unsignedp)
2624 return gen_rtx_UNSIGNED_FLOAT (mode, op0);
2625 else
2626 return gen_rtx_FLOAT (mode, op0);
2627
2628 case FIX_TRUNC_EXPR:
2629 if (unsignedp)
2630 return gen_rtx_UNSIGNED_FIX (mode, op0);
2631 else
2632 return gen_rtx_FIX (mode, op0);
2633
2634 case POINTER_PLUS_EXPR:
2635 /* For the rare target where pointers are not the same size as
2636 size_t, we need to check for mis-matched modes and correct
2637 the addend. */
2638 if (op0 && op1
2639 && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
2640 && GET_MODE (op0) != GET_MODE (op1))
2641 {
2642 if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1)))
2643 op1 = gen_rtx_TRUNCATE (GET_MODE (op0), op1);
2644 else
2645 /* We always sign-extend, regardless of the signedness of
2646 the operand, because the operand is always unsigned
2647 here even if the original C expression is signed. */
2648 op1 = gen_rtx_SIGN_EXTEND (GET_MODE (op0), op1);
2649 }
2650 /* Fall through. */
2651 case PLUS_EXPR:
2652 return gen_rtx_PLUS (mode, op0, op1);
2653
2654 case MINUS_EXPR:
2655 return gen_rtx_MINUS (mode, op0, op1);
2656
2657 case MULT_EXPR:
2658 return gen_rtx_MULT (mode, op0, op1);
2659
2660 case RDIV_EXPR:
2661 case TRUNC_DIV_EXPR:
2662 case EXACT_DIV_EXPR:
2663 if (unsignedp)
2664 return gen_rtx_UDIV (mode, op0, op1);
2665 else
2666 return gen_rtx_DIV (mode, op0, op1);
2667
2668 case TRUNC_MOD_EXPR:
2669 if (unsignedp)
2670 return gen_rtx_UMOD (mode, op0, op1);
2671 else
2672 return gen_rtx_MOD (mode, op0, op1);
2673
2674 case FLOOR_DIV_EXPR:
2675 if (unsignedp)
2676 return gen_rtx_UDIV (mode, op0, op1);
2677 else
2678 {
2679 rtx div = gen_rtx_DIV (mode, op0, op1);
2680 rtx mod = gen_rtx_MOD (mode, op0, op1);
2681 rtx adj = floor_sdiv_adjust (mode, mod, op1);
2682 return gen_rtx_PLUS (mode, div, adj);
2683 }
2684
2685 case FLOOR_MOD_EXPR:
2686 if (unsignedp)
2687 return gen_rtx_UMOD (mode, op0, op1);
2688 else
2689 {
2690 rtx mod = gen_rtx_MOD (mode, op0, op1);
2691 rtx adj = floor_sdiv_adjust (mode, mod, op1);
2692 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2693 return gen_rtx_PLUS (mode, mod, adj);
2694 }
2695
2696 case CEIL_DIV_EXPR:
2697 if (unsignedp)
2698 {
2699 rtx div = gen_rtx_UDIV (mode, op0, op1);
2700 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2701 rtx adj = ceil_udiv_adjust (mode, mod, op1);
2702 return gen_rtx_PLUS (mode, div, adj);
2703 }
2704 else
2705 {
2706 rtx div = gen_rtx_DIV (mode, op0, op1);
2707 rtx mod = gen_rtx_MOD (mode, op0, op1);
2708 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2709 return gen_rtx_PLUS (mode, div, adj);
2710 }
2711
2712 case CEIL_MOD_EXPR:
2713 if (unsignedp)
2714 {
2715 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2716 rtx adj = ceil_udiv_adjust (mode, mod, op1);
2717 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2718 return gen_rtx_PLUS (mode, mod, adj);
2719 }
2720 else
2721 {
2722 rtx mod = gen_rtx_MOD (mode, op0, op1);
2723 rtx adj = ceil_sdiv_adjust (mode, mod, op1);
2724 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2725 return gen_rtx_PLUS (mode, mod, adj);
2726 }
2727
2728 case ROUND_DIV_EXPR:
2729 if (unsignedp)
2730 {
2731 rtx div = gen_rtx_UDIV (mode, op0, op1);
2732 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2733 rtx adj = round_udiv_adjust (mode, mod, op1);
2734 return gen_rtx_PLUS (mode, div, adj);
2735 }
2736 else
2737 {
2738 rtx div = gen_rtx_DIV (mode, op0, op1);
2739 rtx mod = gen_rtx_MOD (mode, op0, op1);
2740 rtx adj = round_sdiv_adjust (mode, mod, op1);
2741 return gen_rtx_PLUS (mode, div, adj);
2742 }
2743
2744 case ROUND_MOD_EXPR:
2745 if (unsignedp)
2746 {
2747 rtx mod = gen_rtx_UMOD (mode, op0, op1);
2748 rtx adj = round_udiv_adjust (mode, mod, op1);
2749 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2750 return gen_rtx_PLUS (mode, mod, adj);
2751 }
2752 else
2753 {
2754 rtx mod = gen_rtx_MOD (mode, op0, op1);
2755 rtx adj = round_sdiv_adjust (mode, mod, op1);
2756 adj = gen_rtx_NEG (mode, gen_rtx_MULT (mode, adj, op1));
2757 return gen_rtx_PLUS (mode, mod, adj);
2758 }
2759
2760 case LSHIFT_EXPR:
2761 return gen_rtx_ASHIFT (mode, op0, op1);
2762
2763 case RSHIFT_EXPR:
2764 if (unsignedp)
2765 return gen_rtx_LSHIFTRT (mode, op0, op1);
2766 else
2767 return gen_rtx_ASHIFTRT (mode, op0, op1);
2768
2769 case LROTATE_EXPR:
2770 return gen_rtx_ROTATE (mode, op0, op1);
2771
2772 case RROTATE_EXPR:
2773 return gen_rtx_ROTATERT (mode, op0, op1);
2774
2775 case MIN_EXPR:
2776 if (unsignedp)
2777 return gen_rtx_UMIN (mode, op0, op1);
2778 else
2779 return gen_rtx_SMIN (mode, op0, op1);
2780
2781 case MAX_EXPR:
2782 if (unsignedp)
2783 return gen_rtx_UMAX (mode, op0, op1);
2784 else
2785 return gen_rtx_SMAX (mode, op0, op1);
2786
2787 case BIT_AND_EXPR:
2788 case TRUTH_AND_EXPR:
2789 return gen_rtx_AND (mode, op0, op1);
2790
2791 case BIT_IOR_EXPR:
2792 case TRUTH_OR_EXPR:
2793 return gen_rtx_IOR (mode, op0, op1);
2794
2795 case BIT_XOR_EXPR:
2796 case TRUTH_XOR_EXPR:
2797 return gen_rtx_XOR (mode, op0, op1);
2798
2799 case TRUTH_ANDIF_EXPR:
2800 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
2801
2802 case TRUTH_ORIF_EXPR:
2803 return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
2804
2805 case TRUTH_NOT_EXPR:
2806 return gen_rtx_EQ (mode, op0, const0_rtx);
2807
2808 case LT_EXPR:
2809 if (unsignedp)
2810 return gen_rtx_LTU (mode, op0, op1);
2811 else
2812 return gen_rtx_LT (mode, op0, op1);
2813
2814 case LE_EXPR:
2815 if (unsignedp)
2816 return gen_rtx_LEU (mode, op0, op1);
2817 else
2818 return gen_rtx_LE (mode, op0, op1);
2819
2820 case GT_EXPR:
2821 if (unsignedp)
2822 return gen_rtx_GTU (mode, op0, op1);
2823 else
2824 return gen_rtx_GT (mode, op0, op1);
2825
2826 case GE_EXPR:
2827 if (unsignedp)
2828 return gen_rtx_GEU (mode, op0, op1);
2829 else
2830 return gen_rtx_GE (mode, op0, op1);
2831
2832 case EQ_EXPR:
2833 return gen_rtx_EQ (mode, op0, op1);
2834
2835 case NE_EXPR:
2836 return gen_rtx_NE (mode, op0, op1);
2837
2838 case UNORDERED_EXPR:
2839 return gen_rtx_UNORDERED (mode, op0, op1);
2840
2841 case ORDERED_EXPR:
2842 return gen_rtx_ORDERED (mode, op0, op1);
2843
2844 case UNLT_EXPR:
2845 return gen_rtx_UNLT (mode, op0, op1);
2846
2847 case UNLE_EXPR:
2848 return gen_rtx_UNLE (mode, op0, op1);
2849
2850 case UNGT_EXPR:
2851 return gen_rtx_UNGT (mode, op0, op1);
2852
2853 case UNGE_EXPR:
2854 return gen_rtx_UNGE (mode, op0, op1);
2855
2856 case UNEQ_EXPR:
2857 return gen_rtx_UNEQ (mode, op0, op1);
2858
2859 case LTGT_EXPR:
2860 return gen_rtx_LTGT (mode, op0, op1);
2861
2862 case COND_EXPR:
2863 return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
2864
2865 case COMPLEX_EXPR:
2866 gcc_assert (COMPLEX_MODE_P (mode));
2867 if (GET_MODE (op0) == VOIDmode)
2868 op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
2869 if (GET_MODE (op1) == VOIDmode)
2870 op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
2871 return gen_rtx_CONCAT (mode, op0, op1);
2872
2873 case CONJ_EXPR:
2874 if (GET_CODE (op0) == CONCAT)
2875 return gen_rtx_CONCAT (mode, XEXP (op0, 0),
2876 gen_rtx_NEG (GET_MODE_INNER (mode),
2877 XEXP (op0, 1)));
2878 else
2879 {
2880 enum machine_mode imode = GET_MODE_INNER (mode);
2881 rtx re, im;
2882
2883 if (MEM_P (op0))
2884 {
2885 re = adjust_address_nv (op0, imode, 0);
2886 im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
2887 }
2888 else
2889 {
2890 enum machine_mode ifmode = int_mode_for_mode (mode);
2891 enum machine_mode ihmode = int_mode_for_mode (imode);
2892 rtx halfsize;
2893 if (ifmode == BLKmode || ihmode == BLKmode)
2894 return NULL;
2895 halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
2896 re = op0;
2897 if (mode != ifmode)
2898 re = gen_rtx_SUBREG (ifmode, re, 0);
2899 re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
2900 if (imode != ihmode)
2901 re = gen_rtx_SUBREG (imode, re, 0);
2902 im = copy_rtx (op0);
2903 if (mode != ifmode)
2904 im = gen_rtx_SUBREG (ifmode, im, 0);
2905 im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
2906 if (imode != ihmode)
2907 im = gen_rtx_SUBREG (imode, im, 0);
2908 }
2909 im = gen_rtx_NEG (imode, im);
2910 return gen_rtx_CONCAT (mode, re, im);
2911 }
2912
2913 case ADDR_EXPR:
2914 op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
2915 if (!op0 || !MEM_P (op0))
2916 return NULL;
2917
2918 op0 = convert_debug_memory_address (mode, XEXP (op0, 0));
2919
2920 return op0;
2921
2922 case VECTOR_CST:
2923 exp = build_constructor_from_list (TREE_TYPE (exp),
2924 TREE_VECTOR_CST_ELTS (exp));
2925 /* Fall through. */
2926
2927 case CONSTRUCTOR:
2928 if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
2929 {
2930 unsigned i;
2931 tree val;
2932
2933 op0 = gen_rtx_CONCATN
2934 (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
2935
2936 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
2937 {
2938 op1 = expand_debug_expr (val);
2939 if (!op1)
2940 return NULL;
2941 XVECEXP (op0, 0, i) = op1;
2942 }
2943
2944 if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
2945 {
2946 op1 = expand_debug_expr
2947 (fold_convert (TREE_TYPE (TREE_TYPE (exp)), integer_zero_node));
2948
2949 if (!op1)
2950 return NULL;
2951
2952 for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
2953 XVECEXP (op0, 0, i) = op1;
2954 }
2955
2956 return op0;
2957 }
2958 else
2959 goto flag_unsupported;
2960
2961 case CALL_EXPR:
2962 /* ??? Maybe handle some builtins? */
2963 return NULL;
2964
2965 case SSA_NAME:
2966 {
2967 gimple g = get_gimple_for_ssa_name (exp);
2968 if (g)
2969 {
2970 op0 = expand_debug_expr (gimple_assign_rhs_to_tree (g));
2971 if (!op0)
2972 return NULL;
2973 }
2974 else
2975 {
2976 int part = var_to_partition (SA.map, exp);
2977
2978 if (part == NO_PARTITION)
2979 return NULL;
2980
2981 gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
2982
2983 op0 = SA.partition_to_pseudo[part];
2984 }
2985 goto adjust_mode;
2986 }
2987
2988 case ERROR_MARK:
2989 return NULL;
2990
2991 /* Vector stuff. For most of the codes we don't have rtl codes. */
2992 case REALIGN_LOAD_EXPR:
2993 case REDUC_MAX_EXPR:
2994 case REDUC_MIN_EXPR:
2995 case REDUC_PLUS_EXPR:
2996 case VEC_COND_EXPR:
2997 case VEC_EXTRACT_EVEN_EXPR:
2998 case VEC_EXTRACT_ODD_EXPR:
2999 case VEC_INTERLEAVE_HIGH_EXPR:
3000 case VEC_INTERLEAVE_LOW_EXPR:
3001 case VEC_LSHIFT_EXPR:
3002 case VEC_PACK_FIX_TRUNC_EXPR:
3003 case VEC_PACK_SAT_EXPR:
3004 case VEC_PACK_TRUNC_EXPR:
3005 case VEC_RSHIFT_EXPR:
3006 case VEC_UNPACK_FLOAT_HI_EXPR:
3007 case VEC_UNPACK_FLOAT_LO_EXPR:
3008 case VEC_UNPACK_HI_EXPR:
3009 case VEC_UNPACK_LO_EXPR:
3010 case VEC_WIDEN_MULT_HI_EXPR:
3011 case VEC_WIDEN_MULT_LO_EXPR:
3012 return NULL;
3013
3014 /* Misc codes. */
3015 case ADDR_SPACE_CONVERT_EXPR:
3016 case FIXED_CONVERT_EXPR:
3017 case OBJ_TYPE_REF:
3018 case WITH_SIZE_EXPR:
3019 return NULL;
3020
3021 case DOT_PROD_EXPR:
3022 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3023 && SCALAR_INT_MODE_P (mode))
3024 {
3025 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3026 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3027 else
3028 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3029 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3030 op1 = gen_rtx_ZERO_EXTEND (mode, op1);
3031 else
3032 op1 = gen_rtx_SIGN_EXTEND (mode, op1);
3033 op0 = gen_rtx_MULT (mode, op0, op1);
3034 return gen_rtx_PLUS (mode, op0, op2);
3035 }
3036 return NULL;
3037
3038 case WIDEN_MULT_EXPR:
3039 case WIDEN_MULT_PLUS_EXPR:
3040 case WIDEN_MULT_MINUS_EXPR:
3041 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3042 && SCALAR_INT_MODE_P (mode))
3043 {
3044 enum machine_mode inner_mode = GET_MODE (op0);
3045 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3046 op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
3047 else
3048 op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
3049 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
3050 op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
3051 else
3052 op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
3053 op0 = gen_rtx_MULT (mode, op0, op1);
3054 if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
3055 return op0;
3056 else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
3057 return gen_rtx_PLUS (mode, op0, op2);
3058 else
3059 return gen_rtx_MINUS (mode, op2, op0);
3060 }
3061 return NULL;
3062
3063 case WIDEN_SUM_EXPR:
3064 if (SCALAR_INT_MODE_P (GET_MODE (op0))
3065 && SCALAR_INT_MODE_P (mode))
3066 {
3067 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
3068 op0 = gen_rtx_ZERO_EXTEND (mode, op0);
3069 else
3070 op0 = gen_rtx_SIGN_EXTEND (mode, op0);
3071 return gen_rtx_PLUS (mode, op0, op1);
3072 }
3073 return NULL;
3074
3075 default:
3076 flag_unsupported:
3077 #ifdef ENABLE_CHECKING
3078 debug_tree (exp);
3079 gcc_unreachable ();
3080 #else
3081 return NULL;
3082 #endif
3083 }
3084 }
3085
3086 /* Expand the _LOCs in debug insns. We run this after expanding all
3087 regular insns, so that any variables referenced in the function
3088 will have their DECL_RTLs set. */
3089
3090 static void
3091 expand_debug_locations (void)
3092 {
3093 rtx insn;
3094 rtx last = get_last_insn ();
3095 int save_strict_alias = flag_strict_aliasing;
3096
3097 /* New alias sets while setting up memory attributes cause
3098 -fcompare-debug failures, even though it doesn't bring about any
3099 codegen changes. */
3100 flag_strict_aliasing = 0;
3101
3102 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3103 if (DEBUG_INSN_P (insn))
3104 {
3105 tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
3106 rtx val;
3107 enum machine_mode mode;
3108
3109 if (value == NULL_TREE)
3110 val = NULL_RTX;
3111 else
3112 {
3113 val = expand_debug_expr (value);
3114 gcc_assert (last == get_last_insn ());
3115 }
3116
3117 if (!val)
3118 val = gen_rtx_UNKNOWN_VAR_LOC ();
3119 else
3120 {
3121 mode = GET_MODE (INSN_VAR_LOCATION (insn));
3122
3123 gcc_assert (mode == GET_MODE (val)
3124 || (GET_MODE (val) == VOIDmode
3125 && (CONST_INT_P (val)
3126 || GET_CODE (val) == CONST_FIXED
3127 || GET_CODE (val) == CONST_DOUBLE
3128 || GET_CODE (val) == LABEL_REF)));
3129 }
3130
3131 INSN_VAR_LOCATION_LOC (insn) = val;
3132 }
3133
3134 flag_strict_aliasing = save_strict_alias;
3135 }
3136
3137 /* Expand basic block BB from GIMPLE trees to RTL. */
3138
3139 static basic_block
3140 expand_gimple_basic_block (basic_block bb)
3141 {
3142 gimple_stmt_iterator gsi;
3143 gimple_seq stmts;
3144 gimple stmt = NULL;
3145 rtx note, last;
3146 edge e;
3147 edge_iterator ei;
3148 void **elt;
3149
3150 if (dump_file)
3151 fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
3152 bb->index);
3153
3154 /* Note that since we are now transitioning from GIMPLE to RTL, we
3155 cannot use the gsi_*_bb() routines because they expect the basic
3156 block to be in GIMPLE, instead of RTL. Therefore, we need to
3157 access the BB sequence directly. */
3158 stmts = bb_seq (bb);
3159 bb->il.gimple = NULL;
3160 rtl_profile_for_bb (bb);
3161 init_rtl_bb_info (bb);
3162 bb->flags |= BB_RTL;
3163
3164 /* Remove the RETURN_EXPR if we may fall though to the exit
3165 instead. */
3166 gsi = gsi_last (stmts);
3167 if (!gsi_end_p (gsi)
3168 && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
3169 {
3170 gimple ret_stmt = gsi_stmt (gsi);
3171
3172 gcc_assert (single_succ_p (bb));
3173 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
3174
3175 if (bb->next_bb == EXIT_BLOCK_PTR
3176 && !gimple_return_retval (ret_stmt))
3177 {
3178 gsi_remove (&gsi, false);
3179 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
3180 }
3181 }
3182
3183 gsi = gsi_start (stmts);
3184 if (!gsi_end_p (gsi))
3185 {
3186 stmt = gsi_stmt (gsi);
3187 if (gimple_code (stmt) != GIMPLE_LABEL)
3188 stmt = NULL;
3189 }
3190
3191 elt = pointer_map_contains (lab_rtx_for_bb, bb);
3192
3193 if (stmt || elt)
3194 {
3195 last = get_last_insn ();
3196
3197 if (stmt)
3198 {
3199 expand_gimple_stmt (stmt);
3200 gsi_next (&gsi);
3201 }
3202
3203 if (elt)
3204 emit_label ((rtx) *elt);
3205
3206 /* Java emits line number notes in the top of labels.
3207 ??? Make this go away once line number notes are obsoleted. */
3208 BB_HEAD (bb) = NEXT_INSN (last);
3209 if (NOTE_P (BB_HEAD (bb)))
3210 BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
3211 note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
3212
3213 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3214 }
3215 else
3216 note = BB_HEAD (bb) = emit_note (NOTE_INSN_BASIC_BLOCK);
3217
3218 NOTE_BASIC_BLOCK (note) = bb;
3219
3220 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3221 {
3222 basic_block new_bb;
3223
3224 stmt = gsi_stmt (gsi);
3225
3226 /* If this statement is a non-debug one, and we generate debug
3227 insns, then this one might be the last real use of a TERed
3228 SSA_NAME, but where there are still some debug uses further
3229 down. Expanding the current SSA name in such further debug
3230 uses by their RHS might lead to wrong debug info, as coalescing
3231 might make the operands of such RHS be placed into the same
3232 pseudo as something else. Like so:
3233 a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
3234 use(a_1);
3235 a_2 = ...
3236 #DEBUG ... => a_1
3237 As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
3238 If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
3239 the write to a_2 would actually have clobbered the place which
3240 formerly held a_0.
3241
3242 So, instead of that, we recognize the situation, and generate
3243 debug temporaries at the last real use of TERed SSA names:
3244 a_1 = a_0 + 1;
3245 #DEBUG #D1 => a_1
3246 use(a_1);
3247 a_2 = ...
3248 #DEBUG ... => #D1
3249 */
3250 if (MAY_HAVE_DEBUG_INSNS
3251 && SA.values
3252 && !is_gimple_debug (stmt))
3253 {
3254 ssa_op_iter iter;
3255 tree op;
3256 gimple def;
3257
3258 location_t sloc = get_curr_insn_source_location ();
3259 tree sblock = get_curr_insn_block ();
3260
3261 /* Look for SSA names that have their last use here (TERed
3262 names always have only one real use). */
3263 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3264 if ((def = get_gimple_for_ssa_name (op)))
3265 {
3266 imm_use_iterator imm_iter;
3267 use_operand_p use_p;
3268 bool have_debug_uses = false;
3269
3270 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
3271 {
3272 if (gimple_debug_bind_p (USE_STMT (use_p)))
3273 {
3274 have_debug_uses = true;
3275 break;
3276 }
3277 }
3278
3279 if (have_debug_uses)
3280 {
3281 /* OP is a TERed SSA name, with DEF it's defining
3282 statement, and where OP is used in further debug
3283 instructions. Generate a debug temporary, and
3284 replace all uses of OP in debug insns with that
3285 temporary. */
3286 gimple debugstmt;
3287 tree value = gimple_assign_rhs_to_tree (def);
3288 tree vexpr = make_node (DEBUG_EXPR_DECL);
3289 rtx val;
3290 enum machine_mode mode;
3291
3292 set_curr_insn_source_location (gimple_location (def));
3293 set_curr_insn_block (gimple_block (def));
3294
3295 DECL_ARTIFICIAL (vexpr) = 1;
3296 TREE_TYPE (vexpr) = TREE_TYPE (value);
3297 if (DECL_P (value))
3298 mode = DECL_MODE (value);
3299 else
3300 mode = TYPE_MODE (TREE_TYPE (value));
3301 DECL_MODE (vexpr) = mode;
3302
3303 val = gen_rtx_VAR_LOCATION
3304 (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3305
3306 val = emit_debug_insn (val);
3307
3308 FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
3309 {
3310 if (!gimple_debug_bind_p (debugstmt))
3311 continue;
3312
3313 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
3314 SET_USE (use_p, vexpr);
3315
3316 update_stmt (debugstmt);
3317 }
3318 }
3319 }
3320 set_curr_insn_source_location (sloc);
3321 set_curr_insn_block (sblock);
3322 }
3323
3324 currently_expanding_gimple_stmt = stmt;
3325
3326 /* Expand this statement, then evaluate the resulting RTL and
3327 fixup the CFG accordingly. */
3328 if (gimple_code (stmt) == GIMPLE_COND)
3329 {
3330 new_bb = expand_gimple_cond (bb, stmt);
3331 if (new_bb)
3332 return new_bb;
3333 }
3334 else if (gimple_debug_bind_p (stmt))
3335 {
3336 location_t sloc = get_curr_insn_source_location ();
3337 tree sblock = get_curr_insn_block ();
3338 gimple_stmt_iterator nsi = gsi;
3339
3340 for (;;)
3341 {
3342 tree var = gimple_debug_bind_get_var (stmt);
3343 tree value;
3344 rtx val;
3345 enum machine_mode mode;
3346
3347 if (gimple_debug_bind_has_value_p (stmt))
3348 value = gimple_debug_bind_get_value (stmt);
3349 else
3350 value = NULL_TREE;
3351
3352 last = get_last_insn ();
3353
3354 set_curr_insn_source_location (gimple_location (stmt));
3355 set_curr_insn_block (gimple_block (stmt));
3356
3357 if (DECL_P (var))
3358 mode = DECL_MODE (var);
3359 else
3360 mode = TYPE_MODE (TREE_TYPE (var));
3361
3362 val = gen_rtx_VAR_LOCATION
3363 (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
3364
3365 val = emit_debug_insn (val);
3366
3367 if (dump_file && (dump_flags & TDF_DETAILS))
3368 {
3369 /* We can't dump the insn with a TREE where an RTX
3370 is expected. */
3371 INSN_VAR_LOCATION_LOC (val) = const0_rtx;
3372 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3373 INSN_VAR_LOCATION_LOC (val) = (rtx)value;
3374 }
3375
3376 /* In order not to generate too many debug temporaries,
3377 we delink all uses of debug statements we already expanded.
3378 Therefore debug statements between definition and real
3379 use of TERed SSA names will continue to use the SSA name,
3380 and not be replaced with debug temps. */
3381 delink_stmt_imm_use (stmt);
3382
3383 gsi = nsi;
3384 gsi_next (&nsi);
3385 if (gsi_end_p (nsi))
3386 break;
3387 stmt = gsi_stmt (nsi);
3388 if (!gimple_debug_bind_p (stmt))
3389 break;
3390 }
3391
3392 set_curr_insn_source_location (sloc);
3393 set_curr_insn_block (sblock);
3394 }
3395 else
3396 {
3397 if (is_gimple_call (stmt) && gimple_call_tail_p (stmt))
3398 {
3399 bool can_fallthru;
3400 new_bb = expand_gimple_tailcall (bb, stmt, &can_fallthru);
3401 if (new_bb)
3402 {
3403 if (can_fallthru)
3404 bb = new_bb;
3405 else
3406 return new_bb;
3407 }
3408 }
3409 else
3410 {
3411 def_operand_p def_p;
3412 def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
3413
3414 if (def_p != NULL)
3415 {
3416 /* Ignore this stmt if it is in the list of
3417 replaceable expressions. */
3418 if (SA.values
3419 && bitmap_bit_p (SA.values,
3420 SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
3421 continue;
3422 }
3423 last = expand_gimple_stmt (stmt);
3424 maybe_dump_rtl_for_gimple_stmt (stmt, last);
3425 }
3426 }
3427 }
3428
3429 currently_expanding_gimple_stmt = NULL;
3430
3431 /* Expand implicit goto and convert goto_locus. */
3432 FOR_EACH_EDGE (e, ei, bb->succs)
3433 {
3434 if (e->goto_locus && e->goto_block)
3435 {
3436 set_curr_insn_source_location (e->goto_locus);
3437 set_curr_insn_block (e->goto_block);
3438 e->goto_locus = curr_insn_locator ();
3439 }
3440 e->goto_block = NULL;
3441 if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
3442 {
3443 emit_jump (label_rtx_for_bb (e->dest));
3444 e->flags &= ~EDGE_FALLTHRU;
3445 }
3446 }
3447
3448 /* Expanded RTL can create a jump in the last instruction of block.
3449 This later might be assumed to be a jump to successor and break edge insertion.
3450 We need to insert dummy move to prevent this. PR41440. */
3451 if (single_succ_p (bb)
3452 && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
3453 && (last = get_last_insn ())
3454 && JUMP_P (last))
3455 {
3456 rtx dummy = gen_reg_rtx (SImode);
3457 emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
3458 }
3459
3460 do_pending_stack_adjust ();
3461
3462 /* Find the block tail. The last insn in the block is the insn
3463 before a barrier and/or table jump insn. */
3464 last = get_last_insn ();
3465 if (BARRIER_P (last))
3466 last = PREV_INSN (last);
3467 if (JUMP_TABLE_DATA_P (last))
3468 last = PREV_INSN (PREV_INSN (last));
3469 BB_END (bb) = last;
3470
3471 update_bb_for_insn (bb);
3472
3473 return bb;
3474 }
3475
3476
3477 /* Create a basic block for initialization code. */
3478
3479 static basic_block
3480 construct_init_block (void)
3481 {
3482 basic_block init_block, first_block;
3483 edge e = NULL;
3484 int flags;
3485
3486 /* Multiple entry points not supported yet. */
3487 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1);
3488 init_rtl_bb_info (ENTRY_BLOCK_PTR);
3489 init_rtl_bb_info (EXIT_BLOCK_PTR);
3490 ENTRY_BLOCK_PTR->flags |= BB_RTL;
3491 EXIT_BLOCK_PTR->flags |= BB_RTL;
3492
3493 e = EDGE_SUCC (ENTRY_BLOCK_PTR, 0);
3494
3495 /* When entry edge points to first basic block, we don't need jump,
3496 otherwise we have to jump into proper target. */
3497 if (e && e->dest != ENTRY_BLOCK_PTR->next_bb)
3498 {
3499 tree label = gimple_block_label (e->dest);
3500
3501 emit_jump (label_rtx (label));
3502 flags = 0;
3503 }
3504 else
3505 flags = EDGE_FALLTHRU;
3506
3507 init_block = create_basic_block (NEXT_INSN (get_insns ()),
3508 get_last_insn (),
3509 ENTRY_BLOCK_PTR);
3510 init_block->frequency = ENTRY_BLOCK_PTR->frequency;
3511 init_block->count = ENTRY_BLOCK_PTR->count;
3512 if (e)
3513 {
3514 first_block = e->dest;
3515 redirect_edge_succ (e, init_block);
3516 e = make_edge (init_block, first_block, flags);
3517 }
3518 else
3519 e = make_edge (init_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3520 e->probability = REG_BR_PROB_BASE;
3521 e->count = ENTRY_BLOCK_PTR->count;
3522
3523 update_bb_for_insn (init_block);
3524 return init_block;
3525 }
3526
3527 /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
3528 found in the block tree. */
3529
3530 static void
3531 set_block_levels (tree block, int level)
3532 {
3533 while (block)
3534 {
3535 BLOCK_NUMBER (block) = level;
3536 set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
3537 block = BLOCK_CHAIN (block);
3538 }
3539 }
3540
3541 /* Create a block containing landing pads and similar stuff. */
3542
3543 static void
3544 construct_exit_block (void)
3545 {
3546 rtx head = get_last_insn ();
3547 rtx end;
3548 basic_block exit_block;
3549 edge e, e2;
3550 unsigned ix;
3551 edge_iterator ei;
3552 rtx orig_end = BB_END (EXIT_BLOCK_PTR->prev_bb);
3553
3554 rtl_profile_for_bb (EXIT_BLOCK_PTR);
3555
3556 /* Make sure the locus is set to the end of the function, so that
3557 epilogue line numbers and warnings are set properly. */
3558 if (cfun->function_end_locus != UNKNOWN_LOCATION)
3559 input_location = cfun->function_end_locus;
3560
3561 /* The following insns belong to the top scope. */
3562 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3563
3564 /* Generate rtl for function exit. */
3565 expand_function_end ();
3566
3567 end = get_last_insn ();
3568 if (head == end)
3569 return;
3570 /* While emitting the function end we could move end of the last basic block.
3571 */
3572 BB_END (EXIT_BLOCK_PTR->prev_bb) = orig_end;
3573 while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
3574 head = NEXT_INSN (head);
3575 exit_block = create_basic_block (NEXT_INSN (head), end,
3576 EXIT_BLOCK_PTR->prev_bb);
3577 exit_block->frequency = EXIT_BLOCK_PTR->frequency;
3578 exit_block->count = EXIT_BLOCK_PTR->count;
3579
3580 ix = 0;
3581 while (ix < EDGE_COUNT (EXIT_BLOCK_PTR->preds))
3582 {
3583 e = EDGE_PRED (EXIT_BLOCK_PTR, ix);
3584 if (!(e->flags & EDGE_ABNORMAL))
3585 redirect_edge_succ (e, exit_block);
3586 else
3587 ix++;
3588 }
3589
3590 e = make_edge (exit_block, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
3591 e->probability = REG_BR_PROB_BASE;
3592 e->count = EXIT_BLOCK_PTR->count;
3593 FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR->preds)
3594 if (e2 != e)
3595 {
3596 e->count -= e2->count;
3597 exit_block->count -= e2->count;
3598 exit_block->frequency -= EDGE_FREQUENCY (e2);
3599 }
3600 if (e->count < 0)
3601 e->count = 0;
3602 if (exit_block->count < 0)
3603 exit_block->count = 0;
3604 if (exit_block->frequency < 0)
3605 exit_block->frequency = 0;
3606 update_bb_for_insn (exit_block);
3607 }
3608
3609 /* Helper function for discover_nonconstant_array_refs.
3610 Look for ARRAY_REF nodes with non-constant indexes and mark them
3611 addressable. */
3612
3613 static tree
3614 discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
3615 void *data ATTRIBUTE_UNUSED)
3616 {
3617 tree t = *tp;
3618
3619 if (IS_TYPE_OR_DECL_P (t))
3620 *walk_subtrees = 0;
3621 else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3622 {
3623 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3624 && is_gimple_min_invariant (TREE_OPERAND (t, 1))
3625 && (!TREE_OPERAND (t, 2)
3626 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3627 || (TREE_CODE (t) == COMPONENT_REF
3628 && (!TREE_OPERAND (t,2)
3629 || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
3630 || TREE_CODE (t) == BIT_FIELD_REF
3631 || TREE_CODE (t) == REALPART_EXPR
3632 || TREE_CODE (t) == IMAGPART_EXPR
3633 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3634 || CONVERT_EXPR_P (t))
3635 t = TREE_OPERAND (t, 0);
3636
3637 if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3638 {
3639 t = get_base_address (t);
3640 if (t && DECL_P (t)
3641 && DECL_MODE (t) != BLKmode)
3642 TREE_ADDRESSABLE (t) = 1;
3643 }
3644
3645 *walk_subtrees = 0;
3646 }
3647
3648 return NULL_TREE;
3649 }
3650
3651 /* RTL expansion is not able to compile array references with variable
3652 offsets for arrays stored in single register. Discover such
3653 expressions and mark variables as addressable to avoid this
3654 scenario. */
3655
3656 static void
3657 discover_nonconstant_array_refs (void)
3658 {
3659 basic_block bb;
3660 gimple_stmt_iterator gsi;
3661
3662 FOR_EACH_BB (bb)
3663 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3664 {
3665 gimple stmt = gsi_stmt (gsi);
3666 if (!is_gimple_debug (stmt))
3667 walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
3668 }
3669 }
3670
3671 /* This function sets crtl->args.internal_arg_pointer to a virtual
3672 register if DRAP is needed. Local register allocator will replace
3673 virtual_incoming_args_rtx with the virtual register. */
3674
3675 static void
3676 expand_stack_alignment (void)
3677 {
3678 rtx drap_rtx;
3679 unsigned int preferred_stack_boundary;
3680
3681 if (! SUPPORTS_STACK_ALIGNMENT)
3682 return;
3683
3684 if (cfun->calls_alloca
3685 || cfun->has_nonlocal_label
3686 || crtl->has_nonlocal_goto)
3687 crtl->need_drap = true;
3688
3689 /* Call update_stack_boundary here again to update incoming stack
3690 boundary. It may set incoming stack alignment to a different
3691 value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
3692 use the minimum incoming stack alignment to check if it is OK
3693 to perform sibcall optimization since sibcall optimization will
3694 only align the outgoing stack to incoming stack boundary. */
3695 if (targetm.calls.update_stack_boundary)
3696 targetm.calls.update_stack_boundary ();
3697
3698 /* The incoming stack frame has to be aligned at least at
3699 parm_stack_boundary. */
3700 gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
3701
3702 /* Update crtl->stack_alignment_estimated and use it later to align
3703 stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
3704 exceptions since callgraph doesn't collect incoming stack alignment
3705 in this case. */
3706 if (cfun->can_throw_non_call_exceptions
3707 && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
3708 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3709 else
3710 preferred_stack_boundary = crtl->preferred_stack_boundary;
3711 if (preferred_stack_boundary > crtl->stack_alignment_estimated)
3712 crtl->stack_alignment_estimated = preferred_stack_boundary;
3713 if (preferred_stack_boundary > crtl->stack_alignment_needed)
3714 crtl->stack_alignment_needed = preferred_stack_boundary;
3715
3716 gcc_assert (crtl->stack_alignment_needed
3717 <= crtl->stack_alignment_estimated);
3718
3719 crtl->stack_realign_needed
3720 = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
3721 crtl->stack_realign_tried = crtl->stack_realign_needed;
3722
3723 crtl->stack_realign_processed = true;
3724
3725 /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
3726 alignment. */
3727 gcc_assert (targetm.calls.get_drap_rtx != NULL);
3728 drap_rtx = targetm.calls.get_drap_rtx ();
3729
3730 /* stack_realign_drap and drap_rtx must match. */
3731 gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
3732
3733 /* Do nothing if NULL is returned, which means DRAP is not needed. */
3734 if (NULL != drap_rtx)
3735 {
3736 crtl->args.internal_arg_pointer = drap_rtx;
3737
3738 /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
3739 needed. */
3740 fixup_tail_calls ();
3741 }
3742 }
3743
3744 /* Translate the intermediate representation contained in the CFG
3745 from GIMPLE trees to RTL.
3746
3747 We do conversion per basic block and preserve/update the tree CFG.
3748 This implies we have to do some magic as the CFG can simultaneously
3749 consist of basic blocks containing RTL and GIMPLE trees. This can
3750 confuse the CFG hooks, so be careful to not manipulate CFG during
3751 the expansion. */
3752
3753 static unsigned int
3754 gimple_expand_cfg (void)
3755 {
3756 basic_block bb, init_block;
3757 sbitmap blocks;
3758 edge_iterator ei;
3759 edge e;
3760 unsigned i;
3761
3762 rewrite_out_of_ssa (&SA);
3763 SA.partition_to_pseudo = (rtx *)xcalloc (SA.map->num_partitions,
3764 sizeof (rtx));
3765
3766 /* Some backends want to know that we are expanding to RTL. */
3767 currently_expanding_to_rtl = 1;
3768
3769 rtl_profile_for_bb (ENTRY_BLOCK_PTR);
3770
3771 insn_locators_alloc ();
3772 if (!DECL_IS_BUILTIN (current_function_decl))
3773 {
3774 /* Eventually, all FEs should explicitly set function_start_locus. */
3775 if (cfun->function_start_locus == UNKNOWN_LOCATION)
3776 set_curr_insn_source_location
3777 (DECL_SOURCE_LOCATION (current_function_decl));
3778 else
3779 set_curr_insn_source_location (cfun->function_start_locus);
3780 }
3781 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3782 prologue_locator = curr_insn_locator ();
3783
3784 #ifdef INSN_SCHEDULING
3785 init_sched_attrs ();
3786 #endif
3787
3788 /* Make sure first insn is a note even if we don't want linenums.
3789 This makes sure the first insn will never be deleted.
3790 Also, final expects a note to appear there. */
3791 emit_note (NOTE_INSN_DELETED);
3792
3793 /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
3794 discover_nonconstant_array_refs ();
3795
3796 targetm.expand_to_rtl_hook ();
3797 crtl->stack_alignment_needed = STACK_BOUNDARY;
3798 crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
3799 crtl->stack_alignment_estimated = 0;
3800 crtl->preferred_stack_boundary = STACK_BOUNDARY;
3801 cfun->cfg->max_jumptable_ents = 0;
3802
3803
3804 /* Expand the variables recorded during gimple lowering. */
3805 expand_used_vars ();
3806
3807 /* Honor stack protection warnings. */
3808 if (warn_stack_protect)
3809 {
3810 if (cfun->calls_alloca)
3811 warning (OPT_Wstack_protector,
3812 "stack protector not protecting local variables: "
3813 "variable length buffer");
3814 if (has_short_buffer && !crtl->stack_protect_guard)
3815 warning (OPT_Wstack_protector,
3816 "stack protector not protecting function: "
3817 "all local arrays are less than %d bytes long",
3818 (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
3819 }
3820
3821 /* Set up parameters and prepare for return, for the function. */
3822 expand_function_start (current_function_decl);
3823
3824 /* Now that we also have the parameter RTXs, copy them over to our
3825 partitions. */
3826 for (i = 0; i < SA.map->num_partitions; i++)
3827 {
3828 tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
3829
3830 if (TREE_CODE (var) != VAR_DECL
3831 && !SA.partition_to_pseudo[i])
3832 SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
3833 gcc_assert (SA.partition_to_pseudo[i]);
3834
3835 /* If this decl was marked as living in multiple places, reset
3836 this now to NULL. */
3837 if (DECL_RTL_IF_SET (var) == pc_rtx)
3838 SET_DECL_RTL (var, NULL);
3839
3840 /* Some RTL parts really want to look at DECL_RTL(x) when x
3841 was a decl marked in REG_ATTR or MEM_ATTR. We could use
3842 SET_DECL_RTL here making this available, but that would mean
3843 to select one of the potentially many RTLs for one DECL. Instead
3844 of doing that we simply reset the MEM_EXPR of the RTL in question,
3845 then nobody can get at it and hence nobody can call DECL_RTL on it. */
3846 if (!DECL_RTL_SET_P (var))
3847 {
3848 if (MEM_P (SA.partition_to_pseudo[i]))
3849 set_mem_expr (SA.partition_to_pseudo[i], NULL);
3850 }
3851 }
3852
3853 /* If this function is `main', emit a call to `__main'
3854 to run global initializers, etc. */
3855 if (DECL_NAME (current_function_decl)
3856 && MAIN_NAME_P (DECL_NAME (current_function_decl))
3857 && DECL_FILE_SCOPE_P (current_function_decl))
3858 expand_main_function ();
3859
3860 /* Initialize the stack_protect_guard field. This must happen after the
3861 call to __main (if any) so that the external decl is initialized. */
3862 if (crtl->stack_protect_guard)
3863 stack_protect_prologue ();
3864
3865 expand_phi_nodes (&SA);
3866
3867 /* Register rtl specific functions for cfg. */
3868 rtl_register_cfg_hooks ();
3869
3870 init_block = construct_init_block ();
3871
3872 /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
3873 remaining edges later. */
3874 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
3875 e->flags &= ~EDGE_EXECUTABLE;
3876
3877 lab_rtx_for_bb = pointer_map_create ();
3878 FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR, next_bb)
3879 bb = expand_gimple_basic_block (bb);
3880
3881 if (MAY_HAVE_DEBUG_INSNS)
3882 expand_debug_locations ();
3883
3884 execute_free_datastructures ();
3885 finish_out_of_ssa (&SA);
3886
3887 /* We are no longer in SSA form. */
3888 cfun->gimple_df->in_ssa_p = false;
3889
3890 /* Expansion is used by optimization passes too, set maybe_hot_insn_p
3891 conservatively to true until they are all profile aware. */
3892 pointer_map_destroy (lab_rtx_for_bb);
3893 free_histograms ();
3894
3895 construct_exit_block ();
3896 set_curr_insn_block (DECL_INITIAL (current_function_decl));
3897 insn_locators_finalize ();
3898
3899 /* Zap the tree EH table. */
3900 set_eh_throw_stmt_table (cfun, NULL);
3901
3902 rebuild_jump_labels (get_insns ());
3903
3904 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
3905 {
3906 edge e;
3907 edge_iterator ei;
3908 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3909 {
3910 if (e->insns.r)
3911 commit_one_edge_insertion (e);
3912 else
3913 ei_next (&ei);
3914 }
3915 }
3916
3917 /* We're done expanding trees to RTL. */
3918 currently_expanding_to_rtl = 0;
3919
3920 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR, next_bb)
3921 {
3922 edge e;
3923 edge_iterator ei;
3924 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
3925 {
3926 /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
3927 e->flags &= ~EDGE_EXECUTABLE;
3928
3929 /* At the moment not all abnormal edges match the RTL
3930 representation. It is safe to remove them here as
3931 find_many_sub_basic_blocks will rediscover them.
3932 In the future we should get this fixed properly. */
3933 if ((e->flags & EDGE_ABNORMAL)
3934 && !(e->flags & EDGE_SIBCALL))
3935 remove_edge (e);
3936 else
3937 ei_next (&ei);
3938 }
3939 }
3940
3941 blocks = sbitmap_alloc (last_basic_block);
3942 sbitmap_ones (blocks);
3943 find_many_sub_basic_blocks (blocks);
3944 sbitmap_free (blocks);
3945 purge_all_dead_edges ();
3946
3947 compact_blocks ();
3948
3949 expand_stack_alignment ();
3950
3951 #ifdef ENABLE_CHECKING
3952 verify_flow_info ();
3953 #endif
3954
3955 /* There's no need to defer outputting this function any more; we
3956 know we want to output it. */
3957 DECL_DEFER_OUTPUT (current_function_decl) = 0;
3958
3959 /* Now that we're done expanding trees to RTL, we shouldn't have any
3960 more CONCATs anywhere. */
3961 generating_concat_p = 0;
3962
3963 if (dump_file)
3964 {
3965 fprintf (dump_file,
3966 "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
3967 /* And the pass manager will dump RTL for us. */
3968 }
3969
3970 /* If we're emitting a nested function, make sure its parent gets
3971 emitted as well. Doing otherwise confuses debug info. */
3972 {
3973 tree parent;
3974 for (parent = DECL_CONTEXT (current_function_decl);
3975 parent != NULL_TREE;
3976 parent = get_containing_scope (parent))
3977 if (TREE_CODE (parent) == FUNCTION_DECL)
3978 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
3979 }
3980
3981 /* We are now committed to emitting code for this function. Do any
3982 preparation, such as emitting abstract debug info for the inline
3983 before it gets mangled by optimization. */
3984 if (cgraph_function_possibly_inlined_p (current_function_decl))
3985 (*debug_hooks->outlining_inline_function) (current_function_decl);
3986
3987 TREE_ASM_WRITTEN (current_function_decl) = 1;
3988
3989 /* After expanding, the return labels are no longer needed. */
3990 return_label = NULL;
3991 naked_return_label = NULL;
3992 /* Tag the blocks with a depth number so that change_scope can find
3993 the common parent easily. */
3994 set_block_levels (DECL_INITIAL (cfun->decl), 0);
3995 default_rtl_profile ();
3996 return 0;
3997 }
3998
3999 struct rtl_opt_pass pass_expand =
4000 {
4001 {
4002 RTL_PASS,
4003 "expand", /* name */
4004 NULL, /* gate */
4005 gimple_expand_cfg, /* execute */
4006 NULL, /* sub */
4007 NULL, /* next */
4008 0, /* static_pass_number */
4009 TV_EXPAND, /* tv_id */
4010 PROP_ssa | PROP_gimple_leh | PROP_cfg
4011 | PROP_gimple_lcx, /* properties_required */
4012 PROP_rtl, /* properties_provided */
4013 PROP_ssa | PROP_trees, /* properties_destroyed */
4014 TODO_verify_ssa | TODO_verify_flow
4015 | TODO_verify_stmts, /* todo_flags_start */
4016 TODO_dump_func
4017 | TODO_ggc_collect /* todo_flags_finish */
4018 }
4019 };