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