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