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