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