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