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