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