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