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